event-parse.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169
  1. /*
  2. * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  3. *
  4. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation;
  8. * version 2.1 of the License (not later!)
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. *
  21. * The parts for function graph printing was taken and modified from the
  22. * Linux Kernel that were written by
  23. * - Copyright (C) 2009 Frederic Weisbecker,
  24. * Frederic Weisbecker gave his permission to relicense the code to
  25. * the Lesser General Public License.
  26. */
  27. #define _GNU_SOURCE
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #include "event-parse.h"
  35. #include "event-utils.h"
  36. static const char *input_buf;
  37. static unsigned long long input_buf_ptr;
  38. static unsigned long long input_buf_siz;
  39. static int is_flag_field;
  40. static int is_symbolic_field;
  41. static int show_warning = 1;
  42. #define do_warning(fmt, ...) \
  43. do { \
  44. if (show_warning) \
  45. warning(fmt, ##__VA_ARGS__); \
  46. } while (0)
  47. static void init_input_buf(const char *buf, unsigned long long size)
  48. {
  49. input_buf = buf;
  50. input_buf_siz = size;
  51. input_buf_ptr = 0;
  52. }
  53. const char *pevent_get_input_buf(void)
  54. {
  55. return input_buf;
  56. }
  57. unsigned long long pevent_get_input_buf_ptr(void)
  58. {
  59. return input_buf_ptr;
  60. }
  61. struct event_handler {
  62. struct event_handler *next;
  63. int id;
  64. const char *sys_name;
  65. const char *event_name;
  66. pevent_event_handler_func func;
  67. void *context;
  68. };
  69. struct pevent_func_params {
  70. struct pevent_func_params *next;
  71. enum pevent_func_arg_type type;
  72. };
  73. struct pevent_function_handler {
  74. struct pevent_function_handler *next;
  75. enum pevent_func_arg_type ret_type;
  76. char *name;
  77. pevent_func_handler func;
  78. struct pevent_func_params *params;
  79. int nr_args;
  80. };
  81. static unsigned long long
  82. process_defined_func(struct trace_seq *s, void *data, int size,
  83. struct event_format *event, struct print_arg *arg);
  84. static void free_func_handle(struct pevent_function_handler *func);
  85. /**
  86. * pevent_buffer_init - init buffer for parsing
  87. * @buf: buffer to parse
  88. * @size: the size of the buffer
  89. *
  90. * For use with pevent_read_token(), this initializes the internal
  91. * buffer that pevent_read_token() will parse.
  92. */
  93. void pevent_buffer_init(const char *buf, unsigned long long size)
  94. {
  95. init_input_buf(buf, size);
  96. }
  97. void breakpoint(void)
  98. {
  99. static int x;
  100. x++;
  101. }
  102. struct print_arg *alloc_arg(void)
  103. {
  104. struct print_arg *arg;
  105. arg = malloc_or_die(sizeof(*arg));
  106. if (!arg)
  107. return NULL;
  108. memset(arg, 0, sizeof(*arg));
  109. return arg;
  110. }
  111. struct cmdline {
  112. char *comm;
  113. int pid;
  114. };
  115. static int cmdline_cmp(const void *a, const void *b)
  116. {
  117. const struct cmdline *ca = a;
  118. const struct cmdline *cb = b;
  119. if (ca->pid < cb->pid)
  120. return -1;
  121. if (ca->pid > cb->pid)
  122. return 1;
  123. return 0;
  124. }
  125. struct cmdline_list {
  126. struct cmdline_list *next;
  127. char *comm;
  128. int pid;
  129. };
  130. static int cmdline_init(struct pevent *pevent)
  131. {
  132. struct cmdline_list *cmdlist = pevent->cmdlist;
  133. struct cmdline_list *item;
  134. struct cmdline *cmdlines;
  135. int i;
  136. cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count);
  137. i = 0;
  138. while (cmdlist) {
  139. cmdlines[i].pid = cmdlist->pid;
  140. cmdlines[i].comm = cmdlist->comm;
  141. i++;
  142. item = cmdlist;
  143. cmdlist = cmdlist->next;
  144. free(item);
  145. }
  146. qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  147. pevent->cmdlines = cmdlines;
  148. pevent->cmdlist = NULL;
  149. return 0;
  150. }
  151. static char *find_cmdline(struct pevent *pevent, int pid)
  152. {
  153. const struct cmdline *comm;
  154. struct cmdline key;
  155. if (!pid)
  156. return "<idle>";
  157. if (!pevent->cmdlines)
  158. cmdline_init(pevent);
  159. key.pid = pid;
  160. comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  161. sizeof(*pevent->cmdlines), cmdline_cmp);
  162. if (comm)
  163. return comm->comm;
  164. return "<...>";
  165. }
  166. /**
  167. * pevent_pid_is_registered - return if a pid has a cmdline registered
  168. * @pevent: handle for the pevent
  169. * @pid: The pid to check if it has a cmdline registered with.
  170. *
  171. * Returns 1 if the pid has a cmdline mapped to it
  172. * 0 otherwise.
  173. */
  174. int pevent_pid_is_registered(struct pevent *pevent, int pid)
  175. {
  176. const struct cmdline *comm;
  177. struct cmdline key;
  178. if (!pid)
  179. return 1;
  180. if (!pevent->cmdlines)
  181. cmdline_init(pevent);
  182. key.pid = pid;
  183. comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  184. sizeof(*pevent->cmdlines), cmdline_cmp);
  185. if (comm)
  186. return 1;
  187. return 0;
  188. }
  189. /*
  190. * If the command lines have been converted to an array, then
  191. * we must add this pid. This is much slower than when cmdlines
  192. * are added before the array is initialized.
  193. */
  194. static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
  195. {
  196. struct cmdline *cmdlines = pevent->cmdlines;
  197. const struct cmdline *cmdline;
  198. struct cmdline key;
  199. if (!pid)
  200. return 0;
  201. /* avoid duplicates */
  202. key.pid = pid;
  203. cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  204. sizeof(*pevent->cmdlines), cmdline_cmp);
  205. if (cmdline) {
  206. errno = EEXIST;
  207. return -1;
  208. }
  209. cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
  210. if (!cmdlines) {
  211. errno = ENOMEM;
  212. return -1;
  213. }
  214. cmdlines[pevent->cmdline_count].pid = pid;
  215. cmdlines[pevent->cmdline_count].comm = strdup(comm);
  216. if (!cmdlines[pevent->cmdline_count].comm)
  217. die("malloc comm");
  218. if (cmdlines[pevent->cmdline_count].comm)
  219. pevent->cmdline_count++;
  220. qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  221. pevent->cmdlines = cmdlines;
  222. return 0;
  223. }
  224. /**
  225. * pevent_register_comm - register a pid / comm mapping
  226. * @pevent: handle for the pevent
  227. * @comm: the command line to register
  228. * @pid: the pid to map the command line to
  229. *
  230. * This adds a mapping to search for command line names with
  231. * a given pid. The comm is duplicated.
  232. */
  233. int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
  234. {
  235. struct cmdline_list *item;
  236. if (pevent->cmdlines)
  237. return add_new_comm(pevent, comm, pid);
  238. item = malloc_or_die(sizeof(*item));
  239. item->comm = strdup(comm);
  240. if (!item->comm)
  241. die("malloc comm");
  242. item->pid = pid;
  243. item->next = pevent->cmdlist;
  244. pevent->cmdlist = item;
  245. pevent->cmdline_count++;
  246. return 0;
  247. }
  248. struct func_map {
  249. unsigned long long addr;
  250. char *func;
  251. char *mod;
  252. };
  253. struct func_list {
  254. struct func_list *next;
  255. unsigned long long addr;
  256. char *func;
  257. char *mod;
  258. };
  259. static int func_cmp(const void *a, const void *b)
  260. {
  261. const struct func_map *fa = a;
  262. const struct func_map *fb = b;
  263. if (fa->addr < fb->addr)
  264. return -1;
  265. if (fa->addr > fb->addr)
  266. return 1;
  267. return 0;
  268. }
  269. /*
  270. * We are searching for a record in between, not an exact
  271. * match.
  272. */
  273. static int func_bcmp(const void *a, const void *b)
  274. {
  275. const struct func_map *fa = a;
  276. const struct func_map *fb = b;
  277. if ((fa->addr == fb->addr) ||
  278. (fa->addr > fb->addr &&
  279. fa->addr < (fb+1)->addr))
  280. return 0;
  281. if (fa->addr < fb->addr)
  282. return -1;
  283. return 1;
  284. }
  285. static int func_map_init(struct pevent *pevent)
  286. {
  287. struct func_list *funclist;
  288. struct func_list *item;
  289. struct func_map *func_map;
  290. int i;
  291. func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1));
  292. funclist = pevent->funclist;
  293. i = 0;
  294. while (funclist) {
  295. func_map[i].func = funclist->func;
  296. func_map[i].addr = funclist->addr;
  297. func_map[i].mod = funclist->mod;
  298. i++;
  299. item = funclist;
  300. funclist = funclist->next;
  301. free(item);
  302. }
  303. qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
  304. /*
  305. * Add a special record at the end.
  306. */
  307. func_map[pevent->func_count].func = NULL;
  308. func_map[pevent->func_count].addr = 0;
  309. func_map[pevent->func_count].mod = NULL;
  310. pevent->func_map = func_map;
  311. pevent->funclist = NULL;
  312. return 0;
  313. }
  314. static struct func_map *
  315. find_func(struct pevent *pevent, unsigned long long addr)
  316. {
  317. struct func_map *func;
  318. struct func_map key;
  319. if (!pevent->func_map)
  320. func_map_init(pevent);
  321. key.addr = addr;
  322. func = bsearch(&key, pevent->func_map, pevent->func_count,
  323. sizeof(*pevent->func_map), func_bcmp);
  324. return func;
  325. }
  326. /**
  327. * pevent_find_function - find a function by a given address
  328. * @pevent: handle for the pevent
  329. * @addr: the address to find the function with
  330. *
  331. * Returns a pointer to the function stored that has the given
  332. * address. Note, the address does not have to be exact, it
  333. * will select the function that would contain the address.
  334. */
  335. const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
  336. {
  337. struct func_map *map;
  338. map = find_func(pevent, addr);
  339. if (!map)
  340. return NULL;
  341. return map->func;
  342. }
  343. /**
  344. * pevent_find_function_address - find a function address by a given address
  345. * @pevent: handle for the pevent
  346. * @addr: the address to find the function with
  347. *
  348. * Returns the address the function starts at. This can be used in
  349. * conjunction with pevent_find_function to print both the function
  350. * name and the function offset.
  351. */
  352. unsigned long long
  353. pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
  354. {
  355. struct func_map *map;
  356. map = find_func(pevent, addr);
  357. if (!map)
  358. return 0;
  359. return map->addr;
  360. }
  361. /**
  362. * pevent_register_function - register a function with a given address
  363. * @pevent: handle for the pevent
  364. * @function: the function name to register
  365. * @addr: the address the function starts at
  366. * @mod: the kernel module the function may be in (NULL for none)
  367. *
  368. * This registers a function name with an address and module.
  369. * The @func passed in is duplicated.
  370. */
  371. int pevent_register_function(struct pevent *pevent, char *func,
  372. unsigned long long addr, char *mod)
  373. {
  374. struct func_list *item;
  375. item = malloc_or_die(sizeof(*item));
  376. item->next = pevent->funclist;
  377. item->func = strdup(func);
  378. if (mod)
  379. item->mod = strdup(mod);
  380. else
  381. item->mod = NULL;
  382. item->addr = addr;
  383. pevent->funclist = item;
  384. pevent->func_count++;
  385. return 0;
  386. }
  387. /**
  388. * pevent_print_funcs - print out the stored functions
  389. * @pevent: handle for the pevent
  390. *
  391. * This prints out the stored functions.
  392. */
  393. void pevent_print_funcs(struct pevent *pevent)
  394. {
  395. int i;
  396. if (!pevent->func_map)
  397. func_map_init(pevent);
  398. for (i = 0; i < (int)pevent->func_count; i++) {
  399. printf("%016llx %s",
  400. pevent->func_map[i].addr,
  401. pevent->func_map[i].func);
  402. if (pevent->func_map[i].mod)
  403. printf(" [%s]\n", pevent->func_map[i].mod);
  404. else
  405. printf("\n");
  406. }
  407. }
  408. struct printk_map {
  409. unsigned long long addr;
  410. char *printk;
  411. };
  412. struct printk_list {
  413. struct printk_list *next;
  414. unsigned long long addr;
  415. char *printk;
  416. };
  417. static int printk_cmp(const void *a, const void *b)
  418. {
  419. const struct func_map *fa = a;
  420. const struct func_map *fb = b;
  421. if (fa->addr < fb->addr)
  422. return -1;
  423. if (fa->addr > fb->addr)
  424. return 1;
  425. return 0;
  426. }
  427. static void printk_map_init(struct pevent *pevent)
  428. {
  429. struct printk_list *printklist;
  430. struct printk_list *item;
  431. struct printk_map *printk_map;
  432. int i;
  433. printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
  434. printklist = pevent->printklist;
  435. i = 0;
  436. while (printklist) {
  437. printk_map[i].printk = printklist->printk;
  438. printk_map[i].addr = printklist->addr;
  439. i++;
  440. item = printklist;
  441. printklist = printklist->next;
  442. free(item);
  443. }
  444. qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
  445. pevent->printk_map = printk_map;
  446. pevent->printklist = NULL;
  447. }
  448. static struct printk_map *
  449. find_printk(struct pevent *pevent, unsigned long long addr)
  450. {
  451. struct printk_map *printk;
  452. struct printk_map key;
  453. if (!pevent->printk_map)
  454. printk_map_init(pevent);
  455. key.addr = addr;
  456. printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
  457. sizeof(*pevent->printk_map), printk_cmp);
  458. return printk;
  459. }
  460. /**
  461. * pevent_register_print_string - register a string by its address
  462. * @pevent: handle for the pevent
  463. * @fmt: the string format to register
  464. * @addr: the address the string was located at
  465. *
  466. * This registers a string by the address it was stored in the kernel.
  467. * The @fmt passed in is duplicated.
  468. */
  469. int pevent_register_print_string(struct pevent *pevent, char *fmt,
  470. unsigned long long addr)
  471. {
  472. struct printk_list *item;
  473. item = malloc_or_die(sizeof(*item));
  474. item->next = pevent->printklist;
  475. pevent->printklist = item;
  476. item->printk = strdup(fmt);
  477. item->addr = addr;
  478. pevent->printk_count++;
  479. return 0;
  480. }
  481. /**
  482. * pevent_print_printk - print out the stored strings
  483. * @pevent: handle for the pevent
  484. *
  485. * This prints the string formats that were stored.
  486. */
  487. void pevent_print_printk(struct pevent *pevent)
  488. {
  489. int i;
  490. if (!pevent->printk_map)
  491. printk_map_init(pevent);
  492. for (i = 0; i < (int)pevent->printk_count; i++) {
  493. printf("%016llx %s\n",
  494. pevent->printk_map[i].addr,
  495. pevent->printk_map[i].printk);
  496. }
  497. }
  498. static struct event_format *alloc_event(void)
  499. {
  500. struct event_format *event;
  501. event = malloc(sizeof(*event));
  502. if (!event)
  503. return NULL;
  504. memset(event, 0, sizeof(*event));
  505. return event;
  506. }
  507. static void add_event(struct pevent *pevent, struct event_format *event)
  508. {
  509. int i;
  510. if (!pevent->events)
  511. pevent->events = malloc_or_die(sizeof(event));
  512. else
  513. pevent->events =
  514. realloc(pevent->events, sizeof(event) *
  515. (pevent->nr_events + 1));
  516. if (!pevent->events)
  517. die("Can not allocate events");
  518. for (i = 0; i < pevent->nr_events; i++) {
  519. if (pevent->events[i]->id > event->id)
  520. break;
  521. }
  522. if (i < pevent->nr_events)
  523. memmove(&pevent->events[i + 1],
  524. &pevent->events[i],
  525. sizeof(event) * (pevent->nr_events - i));
  526. pevent->events[i] = event;
  527. pevent->nr_events++;
  528. event->pevent = pevent;
  529. }
  530. static int event_item_type(enum event_type type)
  531. {
  532. switch (type) {
  533. case EVENT_ITEM ... EVENT_SQUOTE:
  534. return 1;
  535. case EVENT_ERROR ... EVENT_DELIM:
  536. default:
  537. return 0;
  538. }
  539. }
  540. static void free_flag_sym(struct print_flag_sym *fsym)
  541. {
  542. struct print_flag_sym *next;
  543. while (fsym) {
  544. next = fsym->next;
  545. free(fsym->value);
  546. free(fsym->str);
  547. free(fsym);
  548. fsym = next;
  549. }
  550. }
  551. static void free_arg(struct print_arg *arg)
  552. {
  553. struct print_arg *farg;
  554. if (!arg)
  555. return;
  556. switch (arg->type) {
  557. case PRINT_ATOM:
  558. free(arg->atom.atom);
  559. break;
  560. case PRINT_FIELD:
  561. free(arg->field.name);
  562. break;
  563. case PRINT_FLAGS:
  564. free_arg(arg->flags.field);
  565. free(arg->flags.delim);
  566. free_flag_sym(arg->flags.flags);
  567. break;
  568. case PRINT_SYMBOL:
  569. free_arg(arg->symbol.field);
  570. free_flag_sym(arg->symbol.symbols);
  571. break;
  572. case PRINT_HEX:
  573. free_arg(arg->hex.field);
  574. free_arg(arg->hex.size);
  575. break;
  576. case PRINT_TYPE:
  577. free(arg->typecast.type);
  578. free_arg(arg->typecast.item);
  579. break;
  580. case PRINT_STRING:
  581. case PRINT_BSTRING:
  582. free(arg->string.string);
  583. break;
  584. case PRINT_DYNAMIC_ARRAY:
  585. free(arg->dynarray.index);
  586. break;
  587. case PRINT_OP:
  588. free(arg->op.op);
  589. free_arg(arg->op.left);
  590. free_arg(arg->op.right);
  591. break;
  592. case PRINT_FUNC:
  593. while (arg->func.args) {
  594. farg = arg->func.args;
  595. arg->func.args = farg->next;
  596. free_arg(farg);
  597. }
  598. break;
  599. case PRINT_NULL:
  600. default:
  601. break;
  602. }
  603. free(arg);
  604. }
  605. static enum event_type get_type(int ch)
  606. {
  607. if (ch == '\n')
  608. return EVENT_NEWLINE;
  609. if (isspace(ch))
  610. return EVENT_SPACE;
  611. if (isalnum(ch) || ch == '_')
  612. return EVENT_ITEM;
  613. if (ch == '\'')
  614. return EVENT_SQUOTE;
  615. if (ch == '"')
  616. return EVENT_DQUOTE;
  617. if (!isprint(ch))
  618. return EVENT_NONE;
  619. if (ch == '(' || ch == ')' || ch == ',')
  620. return EVENT_DELIM;
  621. return EVENT_OP;
  622. }
  623. static int __read_char(void)
  624. {
  625. if (input_buf_ptr >= input_buf_siz)
  626. return -1;
  627. return input_buf[input_buf_ptr++];
  628. }
  629. static int __peek_char(void)
  630. {
  631. if (input_buf_ptr >= input_buf_siz)
  632. return -1;
  633. return input_buf[input_buf_ptr];
  634. }
  635. /**
  636. * pevent_peek_char - peek at the next character that will be read
  637. *
  638. * Returns the next character read, or -1 if end of buffer.
  639. */
  640. int pevent_peek_char(void)
  641. {
  642. return __peek_char();
  643. }
  644. static enum event_type force_token(const char *str, char **tok);
  645. static enum event_type __read_token(char **tok)
  646. {
  647. char buf[BUFSIZ];
  648. int ch, last_ch, quote_ch, next_ch;
  649. int i = 0;
  650. int tok_size = 0;
  651. enum event_type type;
  652. *tok = NULL;
  653. ch = __read_char();
  654. if (ch < 0)
  655. return EVENT_NONE;
  656. type = get_type(ch);
  657. if (type == EVENT_NONE)
  658. return type;
  659. buf[i++] = ch;
  660. switch (type) {
  661. case EVENT_NEWLINE:
  662. case EVENT_DELIM:
  663. *tok = malloc_or_die(2);
  664. (*tok)[0] = ch;
  665. (*tok)[1] = 0;
  666. return type;
  667. case EVENT_OP:
  668. switch (ch) {
  669. case '-':
  670. next_ch = __peek_char();
  671. if (next_ch == '>') {
  672. buf[i++] = __read_char();
  673. break;
  674. }
  675. /* fall through */
  676. case '+':
  677. case '|':
  678. case '&':
  679. case '>':
  680. case '<':
  681. last_ch = ch;
  682. ch = __peek_char();
  683. if (ch != last_ch)
  684. goto test_equal;
  685. buf[i++] = __read_char();
  686. switch (last_ch) {
  687. case '>':
  688. case '<':
  689. goto test_equal;
  690. default:
  691. break;
  692. }
  693. break;
  694. case '!':
  695. case '=':
  696. goto test_equal;
  697. default: /* what should we do instead? */
  698. break;
  699. }
  700. buf[i] = 0;
  701. *tok = strdup(buf);
  702. return type;
  703. test_equal:
  704. ch = __peek_char();
  705. if (ch == '=')
  706. buf[i++] = __read_char();
  707. goto out;
  708. case EVENT_DQUOTE:
  709. case EVENT_SQUOTE:
  710. /* don't keep quotes */
  711. i--;
  712. quote_ch = ch;
  713. last_ch = 0;
  714. concat:
  715. do {
  716. if (i == (BUFSIZ - 1)) {
  717. buf[i] = 0;
  718. if (*tok) {
  719. *tok = realloc(*tok, tok_size + BUFSIZ);
  720. if (!*tok)
  721. return EVENT_NONE;
  722. strcat(*tok, buf);
  723. } else
  724. *tok = strdup(buf);
  725. if (!*tok)
  726. return EVENT_NONE;
  727. tok_size += BUFSIZ;
  728. i = 0;
  729. }
  730. last_ch = ch;
  731. ch = __read_char();
  732. buf[i++] = ch;
  733. /* the '\' '\' will cancel itself */
  734. if (ch == '\\' && last_ch == '\\')
  735. last_ch = 0;
  736. } while (ch != quote_ch || last_ch == '\\');
  737. /* remove the last quote */
  738. i--;
  739. /*
  740. * For strings (double quotes) check the next token.
  741. * If it is another string, concatinate the two.
  742. */
  743. if (type == EVENT_DQUOTE) {
  744. unsigned long long save_input_buf_ptr = input_buf_ptr;
  745. do {
  746. ch = __read_char();
  747. } while (isspace(ch));
  748. if (ch == '"')
  749. goto concat;
  750. input_buf_ptr = save_input_buf_ptr;
  751. }
  752. goto out;
  753. case EVENT_ERROR ... EVENT_SPACE:
  754. case EVENT_ITEM:
  755. default:
  756. break;
  757. }
  758. while (get_type(__peek_char()) == type) {
  759. if (i == (BUFSIZ - 1)) {
  760. buf[i] = 0;
  761. if (*tok) {
  762. *tok = realloc(*tok, tok_size + BUFSIZ);
  763. if (!*tok)
  764. return EVENT_NONE;
  765. strcat(*tok, buf);
  766. } else
  767. *tok = strdup(buf);
  768. if (!*tok)
  769. return EVENT_NONE;
  770. tok_size += BUFSIZ;
  771. i = 0;
  772. }
  773. ch = __read_char();
  774. buf[i++] = ch;
  775. }
  776. out:
  777. buf[i] = 0;
  778. if (*tok) {
  779. *tok = realloc(*tok, tok_size + i);
  780. if (!*tok)
  781. return EVENT_NONE;
  782. strcat(*tok, buf);
  783. } else
  784. *tok = strdup(buf);
  785. if (!*tok)
  786. return EVENT_NONE;
  787. if (type == EVENT_ITEM) {
  788. /*
  789. * Older versions of the kernel has a bug that
  790. * creates invalid symbols and will break the mac80211
  791. * parsing. This is a work around to that bug.
  792. *
  793. * See Linux kernel commit:
  794. * 811cb50baf63461ce0bdb234927046131fc7fa8b
  795. */
  796. if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
  797. free(*tok);
  798. *tok = NULL;
  799. return force_token("\"\%s\" ", tok);
  800. } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
  801. free(*tok);
  802. *tok = NULL;
  803. return force_token("\" sta:%pM\" ", tok);
  804. } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
  805. free(*tok);
  806. *tok = NULL;
  807. return force_token("\" vif:%p(%d)\" ", tok);
  808. }
  809. }
  810. return type;
  811. }
  812. static enum event_type force_token(const char *str, char **tok)
  813. {
  814. const char *save_input_buf;
  815. unsigned long long save_input_buf_ptr;
  816. unsigned long long save_input_buf_siz;
  817. enum event_type type;
  818. /* save off the current input pointers */
  819. save_input_buf = input_buf;
  820. save_input_buf_ptr = input_buf_ptr;
  821. save_input_buf_siz = input_buf_siz;
  822. init_input_buf(str, strlen(str));
  823. type = __read_token(tok);
  824. /* reset back to original token */
  825. input_buf = save_input_buf;
  826. input_buf_ptr = save_input_buf_ptr;
  827. input_buf_siz = save_input_buf_siz;
  828. return type;
  829. }
  830. static void free_token(char *tok)
  831. {
  832. if (tok)
  833. free(tok);
  834. }
  835. static enum event_type read_token(char **tok)
  836. {
  837. enum event_type type;
  838. for (;;) {
  839. type = __read_token(tok);
  840. if (type != EVENT_SPACE)
  841. return type;
  842. free_token(*tok);
  843. }
  844. /* not reached */
  845. *tok = NULL;
  846. return EVENT_NONE;
  847. }
  848. /**
  849. * pevent_read_token - access to utilites to use the pevent parser
  850. * @tok: The token to return
  851. *
  852. * This will parse tokens from the string given by
  853. * pevent_init_data().
  854. *
  855. * Returns the token type.
  856. */
  857. enum event_type pevent_read_token(char **tok)
  858. {
  859. return read_token(tok);
  860. }
  861. /**
  862. * pevent_free_token - free a token returned by pevent_read_token
  863. * @token: the token to free
  864. */
  865. void pevent_free_token(char *token)
  866. {
  867. free_token(token);
  868. }
  869. /* no newline */
  870. static enum event_type read_token_item(char **tok)
  871. {
  872. enum event_type type;
  873. for (;;) {
  874. type = __read_token(tok);
  875. if (type != EVENT_SPACE && type != EVENT_NEWLINE)
  876. return type;
  877. free_token(*tok);
  878. *tok = NULL;
  879. }
  880. /* not reached */
  881. *tok = NULL;
  882. return EVENT_NONE;
  883. }
  884. static int test_type(enum event_type type, enum event_type expect)
  885. {
  886. if (type != expect) {
  887. do_warning("Error: expected type %d but read %d",
  888. expect, type);
  889. return -1;
  890. }
  891. return 0;
  892. }
  893. static int test_type_token(enum event_type type, const char *token,
  894. enum event_type expect, const char *expect_tok)
  895. {
  896. if (type != expect) {
  897. do_warning("Error: expected type %d but read %d",
  898. expect, type);
  899. return -1;
  900. }
  901. if (strcmp(token, expect_tok) != 0) {
  902. do_warning("Error: expected '%s' but read '%s'",
  903. expect_tok, token);
  904. return -1;
  905. }
  906. return 0;
  907. }
  908. static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
  909. {
  910. enum event_type type;
  911. if (newline_ok)
  912. type = read_token(tok);
  913. else
  914. type = read_token_item(tok);
  915. return test_type(type, expect);
  916. }
  917. static int read_expect_type(enum event_type expect, char **tok)
  918. {
  919. return __read_expect_type(expect, tok, 1);
  920. }
  921. static int __read_expected(enum event_type expect, const char *str,
  922. int newline_ok)
  923. {
  924. enum event_type type;
  925. char *token;
  926. int ret;
  927. if (newline_ok)
  928. type = read_token(&token);
  929. else
  930. type = read_token_item(&token);
  931. ret = test_type_token(type, token, expect, str);
  932. free_token(token);
  933. return ret;
  934. }
  935. static int read_expected(enum event_type expect, const char *str)
  936. {
  937. return __read_expected(expect, str, 1);
  938. }
  939. static int read_expected_item(enum event_type expect, const char *str)
  940. {
  941. return __read_expected(expect, str, 0);
  942. }
  943. static char *event_read_name(void)
  944. {
  945. char *token;
  946. if (read_expected(EVENT_ITEM, "name") < 0)
  947. return NULL;
  948. if (read_expected(EVENT_OP, ":") < 0)
  949. return NULL;
  950. if (read_expect_type(EVENT_ITEM, &token) < 0)
  951. goto fail;
  952. return token;
  953. fail:
  954. free_token(token);
  955. return NULL;
  956. }
  957. static int event_read_id(void)
  958. {
  959. char *token;
  960. int id;
  961. if (read_expected_item(EVENT_ITEM, "ID") < 0)
  962. return -1;
  963. if (read_expected(EVENT_OP, ":") < 0)
  964. return -1;
  965. if (read_expect_type(EVENT_ITEM, &token) < 0)
  966. goto fail;
  967. id = strtoul(token, NULL, 0);
  968. free_token(token);
  969. return id;
  970. fail:
  971. free_token(token);
  972. return -1;
  973. }
  974. static int field_is_string(struct format_field *field)
  975. {
  976. if ((field->flags & FIELD_IS_ARRAY) &&
  977. (strstr(field->type, "char") || strstr(field->type, "u8") ||
  978. strstr(field->type, "s8")))
  979. return 1;
  980. return 0;
  981. }
  982. static int field_is_dynamic(struct format_field *field)
  983. {
  984. if (strncmp(field->type, "__data_loc", 10) == 0)
  985. return 1;
  986. return 0;
  987. }
  988. static int field_is_long(struct format_field *field)
  989. {
  990. /* includes long long */
  991. if (strstr(field->type, "long"))
  992. return 1;
  993. return 0;
  994. }
  995. static int event_read_fields(struct event_format *event, struct format_field **fields)
  996. {
  997. struct format_field *field = NULL;
  998. enum event_type type;
  999. char *token;
  1000. char *last_token;
  1001. int count = 0;
  1002. do {
  1003. type = read_token(&token);
  1004. if (type == EVENT_NEWLINE) {
  1005. free_token(token);
  1006. return count;
  1007. }
  1008. count++;
  1009. if (test_type_token(type, token, EVENT_ITEM, "field"))
  1010. goto fail;
  1011. free_token(token);
  1012. type = read_token(&token);
  1013. /*
  1014. * The ftrace fields may still use the "special" name.
  1015. * Just ignore it.
  1016. */
  1017. if (event->flags & EVENT_FL_ISFTRACE &&
  1018. type == EVENT_ITEM && strcmp(token, "special") == 0) {
  1019. free_token(token);
  1020. type = read_token(&token);
  1021. }
  1022. if (test_type_token(type, token, EVENT_OP, ":") < 0)
  1023. goto fail;
  1024. free_token(token);
  1025. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1026. goto fail;
  1027. last_token = token;
  1028. field = malloc_or_die(sizeof(*field));
  1029. memset(field, 0, sizeof(*field));
  1030. field->event = event;
  1031. /* read the rest of the type */
  1032. for (;;) {
  1033. type = read_token(&token);
  1034. if (type == EVENT_ITEM ||
  1035. (type == EVENT_OP && strcmp(token, "*") == 0) ||
  1036. /*
  1037. * Some of the ftrace fields are broken and have
  1038. * an illegal "." in them.
  1039. */
  1040. (event->flags & EVENT_FL_ISFTRACE &&
  1041. type == EVENT_OP && strcmp(token, ".") == 0)) {
  1042. if (strcmp(token, "*") == 0)
  1043. field->flags |= FIELD_IS_POINTER;
  1044. if (field->type) {
  1045. field->type = realloc(field->type,
  1046. strlen(field->type) +
  1047. strlen(last_token) + 2);
  1048. strcat(field->type, " ");
  1049. strcat(field->type, last_token);
  1050. free(last_token);
  1051. } else
  1052. field->type = last_token;
  1053. last_token = token;
  1054. continue;
  1055. }
  1056. break;
  1057. }
  1058. if (!field->type) {
  1059. die("no type found");
  1060. goto fail;
  1061. }
  1062. field->name = last_token;
  1063. if (test_type(type, EVENT_OP))
  1064. goto fail;
  1065. if (strcmp(token, "[") == 0) {
  1066. enum event_type last_type = type;
  1067. char *brackets = token;
  1068. int len;
  1069. field->flags |= FIELD_IS_ARRAY;
  1070. type = read_token(&token);
  1071. if (type == EVENT_ITEM)
  1072. field->arraylen = strtoul(token, NULL, 0);
  1073. else
  1074. field->arraylen = 0;
  1075. while (strcmp(token, "]") != 0) {
  1076. if (last_type == EVENT_ITEM &&
  1077. type == EVENT_ITEM)
  1078. len = 2;
  1079. else
  1080. len = 1;
  1081. last_type = type;
  1082. brackets = realloc(brackets,
  1083. strlen(brackets) +
  1084. strlen(token) + len);
  1085. if (len == 2)
  1086. strcat(brackets, " ");
  1087. strcat(brackets, token);
  1088. /* We only care about the last token */
  1089. field->arraylen = strtoul(token, NULL, 0);
  1090. free_token(token);
  1091. type = read_token(&token);
  1092. if (type == EVENT_NONE) {
  1093. die("failed to find token");
  1094. goto fail;
  1095. }
  1096. }
  1097. free_token(token);
  1098. brackets = realloc(brackets, strlen(brackets) + 2);
  1099. strcat(brackets, "]");
  1100. /* add brackets to type */
  1101. type = read_token(&token);
  1102. /*
  1103. * If the next token is not an OP, then it is of
  1104. * the format: type [] item;
  1105. */
  1106. if (type == EVENT_ITEM) {
  1107. field->type = realloc(field->type,
  1108. strlen(field->type) +
  1109. strlen(field->name) +
  1110. strlen(brackets) + 2);
  1111. strcat(field->type, " ");
  1112. strcat(field->type, field->name);
  1113. free_token(field->name);
  1114. strcat(field->type, brackets);
  1115. field->name = token;
  1116. type = read_token(&token);
  1117. } else {
  1118. field->type = realloc(field->type,
  1119. strlen(field->type) +
  1120. strlen(brackets) + 1);
  1121. strcat(field->type, brackets);
  1122. }
  1123. free(brackets);
  1124. }
  1125. if (field_is_string(field))
  1126. field->flags |= FIELD_IS_STRING;
  1127. if (field_is_dynamic(field))
  1128. field->flags |= FIELD_IS_DYNAMIC;
  1129. if (field_is_long(field))
  1130. field->flags |= FIELD_IS_LONG;
  1131. if (test_type_token(type, token, EVENT_OP, ";"))
  1132. goto fail;
  1133. free_token(token);
  1134. if (read_expected(EVENT_ITEM, "offset") < 0)
  1135. goto fail_expect;
  1136. if (read_expected(EVENT_OP, ":") < 0)
  1137. goto fail_expect;
  1138. if (read_expect_type(EVENT_ITEM, &token))
  1139. goto fail;
  1140. field->offset = strtoul(token, NULL, 0);
  1141. free_token(token);
  1142. if (read_expected(EVENT_OP, ";") < 0)
  1143. goto fail_expect;
  1144. if (read_expected(EVENT_ITEM, "size") < 0)
  1145. goto fail_expect;
  1146. if (read_expected(EVENT_OP, ":") < 0)
  1147. goto fail_expect;
  1148. if (read_expect_type(EVENT_ITEM, &token))
  1149. goto fail;
  1150. field->size = strtoul(token, NULL, 0);
  1151. free_token(token);
  1152. if (read_expected(EVENT_OP, ";") < 0)
  1153. goto fail_expect;
  1154. type = read_token(&token);
  1155. if (type != EVENT_NEWLINE) {
  1156. /* newer versions of the kernel have a "signed" type */
  1157. if (test_type_token(type, token, EVENT_ITEM, "signed"))
  1158. goto fail;
  1159. free_token(token);
  1160. if (read_expected(EVENT_OP, ":") < 0)
  1161. goto fail_expect;
  1162. if (read_expect_type(EVENT_ITEM, &token))
  1163. goto fail;
  1164. /* add signed type */
  1165. free_token(token);
  1166. if (read_expected(EVENT_OP, ";") < 0)
  1167. goto fail_expect;
  1168. if (read_expect_type(EVENT_NEWLINE, &token))
  1169. goto fail;
  1170. }
  1171. free_token(token);
  1172. if (field->flags & FIELD_IS_ARRAY) {
  1173. if (field->arraylen)
  1174. field->elementsize = field->size / field->arraylen;
  1175. else if (field->flags & FIELD_IS_STRING)
  1176. field->elementsize = 1;
  1177. else
  1178. field->elementsize = event->pevent->long_size;
  1179. } else
  1180. field->elementsize = field->size;
  1181. *fields = field;
  1182. fields = &field->next;
  1183. } while (1);
  1184. return 0;
  1185. fail:
  1186. free_token(token);
  1187. fail_expect:
  1188. if (field) {
  1189. free(field->type);
  1190. free(field->name);
  1191. free(field);
  1192. }
  1193. return -1;
  1194. }
  1195. static int event_read_format(struct event_format *event)
  1196. {
  1197. char *token;
  1198. int ret;
  1199. if (read_expected_item(EVENT_ITEM, "format") < 0)
  1200. return -1;
  1201. if (read_expected(EVENT_OP, ":") < 0)
  1202. return -1;
  1203. if (read_expect_type(EVENT_NEWLINE, &token))
  1204. goto fail;
  1205. free_token(token);
  1206. ret = event_read_fields(event, &event->format.common_fields);
  1207. if (ret < 0)
  1208. return ret;
  1209. event->format.nr_common = ret;
  1210. ret = event_read_fields(event, &event->format.fields);
  1211. if (ret < 0)
  1212. return ret;
  1213. event->format.nr_fields = ret;
  1214. return 0;
  1215. fail:
  1216. free_token(token);
  1217. return -1;
  1218. }
  1219. static enum event_type
  1220. process_arg_token(struct event_format *event, struct print_arg *arg,
  1221. char **tok, enum event_type type);
  1222. static enum event_type
  1223. process_arg(struct event_format *event, struct print_arg *arg, char **tok)
  1224. {
  1225. enum event_type type;
  1226. char *token;
  1227. type = read_token(&token);
  1228. *tok = token;
  1229. return process_arg_token(event, arg, tok, type);
  1230. }
  1231. static enum event_type
  1232. process_op(struct event_format *event, struct print_arg *arg, char **tok);
  1233. static enum event_type
  1234. process_cond(struct event_format *event, struct print_arg *top, char **tok)
  1235. {
  1236. struct print_arg *arg, *left, *right;
  1237. enum event_type type;
  1238. char *token = NULL;
  1239. arg = alloc_arg();
  1240. left = alloc_arg();
  1241. right = alloc_arg();
  1242. arg->type = PRINT_OP;
  1243. arg->op.left = left;
  1244. arg->op.right = right;
  1245. *tok = NULL;
  1246. type = process_arg(event, left, &token);
  1247. again:
  1248. /* Handle other operations in the arguments */
  1249. if (type == EVENT_OP && strcmp(token, ":") != 0) {
  1250. type = process_op(event, left, &token);
  1251. goto again;
  1252. }
  1253. if (test_type_token(type, token, EVENT_OP, ":"))
  1254. goto out_free;
  1255. arg->op.op = token;
  1256. type = process_arg(event, right, &token);
  1257. top->op.right = arg;
  1258. *tok = token;
  1259. return type;
  1260. out_free:
  1261. /* Top may point to itself */
  1262. top->op.right = NULL;
  1263. free_token(token);
  1264. free_arg(arg);
  1265. return EVENT_ERROR;
  1266. }
  1267. static enum event_type
  1268. process_array(struct event_format *event, struct print_arg *top, char **tok)
  1269. {
  1270. struct print_arg *arg;
  1271. enum event_type type;
  1272. char *token = NULL;
  1273. arg = alloc_arg();
  1274. *tok = NULL;
  1275. type = process_arg(event, arg, &token);
  1276. if (test_type_token(type, token, EVENT_OP, "]"))
  1277. goto out_free;
  1278. top->op.right = arg;
  1279. free_token(token);
  1280. type = read_token_item(&token);
  1281. *tok = token;
  1282. return type;
  1283. out_free:
  1284. free_token(*tok);
  1285. *tok = NULL;
  1286. free_arg(arg);
  1287. return EVENT_ERROR;
  1288. }
  1289. static int get_op_prio(char *op)
  1290. {
  1291. if (!op[1]) {
  1292. switch (op[0]) {
  1293. case '~':
  1294. case '!':
  1295. return 4;
  1296. case '*':
  1297. case '/':
  1298. case '%':
  1299. return 6;
  1300. case '+':
  1301. case '-':
  1302. return 7;
  1303. /* '>>' and '<<' are 8 */
  1304. case '<':
  1305. case '>':
  1306. return 9;
  1307. /* '==' and '!=' are 10 */
  1308. case '&':
  1309. return 11;
  1310. case '^':
  1311. return 12;
  1312. case '|':
  1313. return 13;
  1314. case '?':
  1315. return 16;
  1316. default:
  1317. do_warning("unknown op '%c'", op[0]);
  1318. return -1;
  1319. }
  1320. } else {
  1321. if (strcmp(op, "++") == 0 ||
  1322. strcmp(op, "--") == 0) {
  1323. return 3;
  1324. } else if (strcmp(op, ">>") == 0 ||
  1325. strcmp(op, "<<") == 0) {
  1326. return 8;
  1327. } else if (strcmp(op, ">=") == 0 ||
  1328. strcmp(op, "<=") == 0) {
  1329. return 9;
  1330. } else if (strcmp(op, "==") == 0 ||
  1331. strcmp(op, "!=") == 0) {
  1332. return 10;
  1333. } else if (strcmp(op, "&&") == 0) {
  1334. return 14;
  1335. } else if (strcmp(op, "||") == 0) {
  1336. return 15;
  1337. } else {
  1338. do_warning("unknown op '%s'", op);
  1339. return -1;
  1340. }
  1341. }
  1342. }
  1343. static int set_op_prio(struct print_arg *arg)
  1344. {
  1345. /* single ops are the greatest */
  1346. if (!arg->op.left || arg->op.left->type == PRINT_NULL)
  1347. arg->op.prio = 0;
  1348. else
  1349. arg->op.prio = get_op_prio(arg->op.op);
  1350. return arg->op.prio;
  1351. }
  1352. /* Note, *tok does not get freed, but will most likely be saved */
  1353. static enum event_type
  1354. process_op(struct event_format *event, struct print_arg *arg, char **tok)
  1355. {
  1356. struct print_arg *left, *right = NULL;
  1357. enum event_type type;
  1358. char *token;
  1359. /* the op is passed in via tok */
  1360. token = *tok;
  1361. if (arg->type == PRINT_OP && !arg->op.left) {
  1362. /* handle single op */
  1363. if (token[1]) {
  1364. die("bad op token %s", token);
  1365. goto out_free;
  1366. }
  1367. switch (token[0]) {
  1368. case '~':
  1369. case '!':
  1370. case '+':
  1371. case '-':
  1372. break;
  1373. default:
  1374. do_warning("bad op token %s", token);
  1375. goto out_free;
  1376. }
  1377. /* make an empty left */
  1378. left = alloc_arg();
  1379. left->type = PRINT_NULL;
  1380. arg->op.left = left;
  1381. right = alloc_arg();
  1382. arg->op.right = right;
  1383. /* do not free the token, it belongs to an op */
  1384. *tok = NULL;
  1385. type = process_arg(event, right, tok);
  1386. } else if (strcmp(token, "?") == 0) {
  1387. left = alloc_arg();
  1388. /* copy the top arg to the left */
  1389. *left = *arg;
  1390. arg->type = PRINT_OP;
  1391. arg->op.op = token;
  1392. arg->op.left = left;
  1393. arg->op.prio = 0;
  1394. type = process_cond(event, arg, tok);
  1395. } else if (strcmp(token, ">>") == 0 ||
  1396. strcmp(token, "<<") == 0 ||
  1397. strcmp(token, "&") == 0 ||
  1398. strcmp(token, "|") == 0 ||
  1399. strcmp(token, "&&") == 0 ||
  1400. strcmp(token, "||") == 0 ||
  1401. strcmp(token, "-") == 0 ||
  1402. strcmp(token, "+") == 0 ||
  1403. strcmp(token, "*") == 0 ||
  1404. strcmp(token, "^") == 0 ||
  1405. strcmp(token, "/") == 0 ||
  1406. strcmp(token, "<") == 0 ||
  1407. strcmp(token, ">") == 0 ||
  1408. strcmp(token, "==") == 0 ||
  1409. strcmp(token, "!=") == 0) {
  1410. left = alloc_arg();
  1411. /* copy the top arg to the left */
  1412. *left = *arg;
  1413. arg->type = PRINT_OP;
  1414. arg->op.op = token;
  1415. arg->op.left = left;
  1416. if (set_op_prio(arg) == -1) {
  1417. event->flags |= EVENT_FL_FAILED;
  1418. /* arg->op.op (= token) will be freed at out_free */
  1419. arg->op.op = NULL;
  1420. goto out_free;
  1421. }
  1422. type = read_token_item(&token);
  1423. *tok = token;
  1424. /* could just be a type pointer */
  1425. if ((strcmp(arg->op.op, "*") == 0) &&
  1426. type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
  1427. if (left->type != PRINT_ATOM)
  1428. die("bad pointer type");
  1429. left->atom.atom = realloc(left->atom.atom,
  1430. strlen(left->atom.atom) + 3);
  1431. strcat(left->atom.atom, " *");
  1432. free(arg->op.op);
  1433. *arg = *left;
  1434. free(left);
  1435. return type;
  1436. }
  1437. right = alloc_arg();
  1438. type = process_arg_token(event, right, tok, type);
  1439. arg->op.right = right;
  1440. } else if (strcmp(token, "[") == 0) {
  1441. left = alloc_arg();
  1442. *left = *arg;
  1443. arg->type = PRINT_OP;
  1444. arg->op.op = token;
  1445. arg->op.left = left;
  1446. arg->op.prio = 0;
  1447. type = process_array(event, arg, tok);
  1448. } else {
  1449. do_warning("unknown op '%s'", token);
  1450. event->flags |= EVENT_FL_FAILED;
  1451. /* the arg is now the left side */
  1452. goto out_free;
  1453. }
  1454. if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
  1455. int prio;
  1456. /* higher prios need to be closer to the root */
  1457. prio = get_op_prio(*tok);
  1458. if (prio > arg->op.prio)
  1459. return process_op(event, arg, tok);
  1460. return process_op(event, right, tok);
  1461. }
  1462. return type;
  1463. out_free:
  1464. free_token(token);
  1465. *tok = NULL;
  1466. return EVENT_ERROR;
  1467. }
  1468. static enum event_type
  1469. process_entry(struct event_format *event __unused, struct print_arg *arg,
  1470. char **tok)
  1471. {
  1472. enum event_type type;
  1473. char *field;
  1474. char *token;
  1475. if (read_expected(EVENT_OP, "->") < 0)
  1476. goto out_err;
  1477. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1478. goto out_free;
  1479. field = token;
  1480. arg->type = PRINT_FIELD;
  1481. arg->field.name = field;
  1482. if (is_flag_field) {
  1483. arg->field.field = pevent_find_any_field(event, arg->field.name);
  1484. arg->field.field->flags |= FIELD_IS_FLAG;
  1485. is_flag_field = 0;
  1486. } else if (is_symbolic_field) {
  1487. arg->field.field = pevent_find_any_field(event, arg->field.name);
  1488. arg->field.field->flags |= FIELD_IS_SYMBOLIC;
  1489. is_symbolic_field = 0;
  1490. }
  1491. type = read_token(&token);
  1492. *tok = token;
  1493. return type;
  1494. out_free:
  1495. free_token(token);
  1496. out_err:
  1497. *tok = NULL;
  1498. return EVENT_ERROR;
  1499. }
  1500. static char *arg_eval (struct print_arg *arg);
  1501. static unsigned long long
  1502. eval_type_str(unsigned long long val, const char *type, int pointer)
  1503. {
  1504. int sign = 0;
  1505. char *ref;
  1506. int len;
  1507. len = strlen(type);
  1508. if (pointer) {
  1509. if (type[len-1] != '*') {
  1510. do_warning("pointer expected with non pointer type");
  1511. return val;
  1512. }
  1513. ref = malloc_or_die(len);
  1514. memcpy(ref, type, len);
  1515. /* chop off the " *" */
  1516. ref[len - 2] = 0;
  1517. val = eval_type_str(val, ref, 0);
  1518. free(ref);
  1519. return val;
  1520. }
  1521. /* check if this is a pointer */
  1522. if (type[len - 1] == '*')
  1523. return val;
  1524. /* Try to figure out the arg size*/
  1525. if (strncmp(type, "struct", 6) == 0)
  1526. /* all bets off */
  1527. return val;
  1528. if (strcmp(type, "u8") == 0)
  1529. return val & 0xff;
  1530. if (strcmp(type, "u16") == 0)
  1531. return val & 0xffff;
  1532. if (strcmp(type, "u32") == 0)
  1533. return val & 0xffffffff;
  1534. if (strcmp(type, "u64") == 0 ||
  1535. strcmp(type, "s64"))
  1536. return val;
  1537. if (strcmp(type, "s8") == 0)
  1538. return (unsigned long long)(char)val & 0xff;
  1539. if (strcmp(type, "s16") == 0)
  1540. return (unsigned long long)(short)val & 0xffff;
  1541. if (strcmp(type, "s32") == 0)
  1542. return (unsigned long long)(int)val & 0xffffffff;
  1543. if (strncmp(type, "unsigned ", 9) == 0) {
  1544. sign = 0;
  1545. type += 9;
  1546. }
  1547. if (strcmp(type, "char") == 0) {
  1548. if (sign)
  1549. return (unsigned long long)(char)val & 0xff;
  1550. else
  1551. return val & 0xff;
  1552. }
  1553. if (strcmp(type, "short") == 0) {
  1554. if (sign)
  1555. return (unsigned long long)(short)val & 0xffff;
  1556. else
  1557. return val & 0xffff;
  1558. }
  1559. if (strcmp(type, "int") == 0) {
  1560. if (sign)
  1561. return (unsigned long long)(int)val & 0xffffffff;
  1562. else
  1563. return val & 0xffffffff;
  1564. }
  1565. return val;
  1566. }
  1567. /*
  1568. * Try to figure out the type.
  1569. */
  1570. static unsigned long long
  1571. eval_type(unsigned long long val, struct print_arg *arg, int pointer)
  1572. {
  1573. if (arg->type != PRINT_TYPE)
  1574. die("expected type argument");
  1575. return eval_type_str(val, arg->typecast.type, pointer);
  1576. }
  1577. static int arg_num_eval(struct print_arg *arg, long long *val)
  1578. {
  1579. long long left, right;
  1580. int ret = 1;
  1581. switch (arg->type) {
  1582. case PRINT_ATOM:
  1583. *val = strtoll(arg->atom.atom, NULL, 0);
  1584. break;
  1585. case PRINT_TYPE:
  1586. ret = arg_num_eval(arg->typecast.item, val);
  1587. if (!ret)
  1588. break;
  1589. *val = eval_type(*val, arg, 0);
  1590. break;
  1591. case PRINT_OP:
  1592. switch (arg->op.op[0]) {
  1593. case '|':
  1594. ret = arg_num_eval(arg->op.left, &left);
  1595. if (!ret)
  1596. break;
  1597. ret = arg_num_eval(arg->op.right, &right);
  1598. if (!ret)
  1599. break;
  1600. if (arg->op.op[1])
  1601. *val = left || right;
  1602. else
  1603. *val = left | right;
  1604. break;
  1605. case '&':
  1606. ret = arg_num_eval(arg->op.left, &left);
  1607. if (!ret)
  1608. break;
  1609. ret = arg_num_eval(arg->op.right, &right);
  1610. if (!ret)
  1611. break;
  1612. if (arg->op.op[1])
  1613. *val = left && right;
  1614. else
  1615. *val = left & right;
  1616. break;
  1617. case '<':
  1618. ret = arg_num_eval(arg->op.left, &left);
  1619. if (!ret)
  1620. break;
  1621. ret = arg_num_eval(arg->op.right, &right);
  1622. if (!ret)
  1623. break;
  1624. switch (arg->op.op[1]) {
  1625. case 0:
  1626. *val = left < right;
  1627. break;
  1628. case '<':
  1629. *val = left << right;
  1630. break;
  1631. case '=':
  1632. *val = left <= right;
  1633. break;
  1634. default:
  1635. do_warning("unknown op '%s'", arg->op.op);
  1636. ret = 0;
  1637. }
  1638. break;
  1639. case '>':
  1640. ret = arg_num_eval(arg->op.left, &left);
  1641. if (!ret)
  1642. break;
  1643. ret = arg_num_eval(arg->op.right, &right);
  1644. if (!ret)
  1645. break;
  1646. switch (arg->op.op[1]) {
  1647. case 0:
  1648. *val = left > right;
  1649. break;
  1650. case '>':
  1651. *val = left >> right;
  1652. break;
  1653. case '=':
  1654. *val = left >= right;
  1655. break;
  1656. default:
  1657. do_warning("unknown op '%s'", arg->op.op);
  1658. ret = 0;
  1659. }
  1660. break;
  1661. case '=':
  1662. ret = arg_num_eval(arg->op.left, &left);
  1663. if (!ret)
  1664. break;
  1665. ret = arg_num_eval(arg->op.right, &right);
  1666. if (!ret)
  1667. break;
  1668. if (arg->op.op[1] != '=') {
  1669. do_warning("unknown op '%s'", arg->op.op);
  1670. ret = 0;
  1671. } else
  1672. *val = left == right;
  1673. break;
  1674. case '!':
  1675. ret = arg_num_eval(arg->op.left, &left);
  1676. if (!ret)
  1677. break;
  1678. ret = arg_num_eval(arg->op.right, &right);
  1679. if (!ret)
  1680. break;
  1681. switch (arg->op.op[1]) {
  1682. case '=':
  1683. *val = left != right;
  1684. break;
  1685. default:
  1686. do_warning("unknown op '%s'", arg->op.op);
  1687. ret = 0;
  1688. }
  1689. break;
  1690. case '-':
  1691. /* check for negative */
  1692. if (arg->op.left->type == PRINT_NULL)
  1693. left = 0;
  1694. else
  1695. ret = arg_num_eval(arg->op.left, &left);
  1696. if (!ret)
  1697. break;
  1698. ret = arg_num_eval(arg->op.right, &right);
  1699. if (!ret)
  1700. break;
  1701. *val = left - right;
  1702. break;
  1703. case '+':
  1704. if (arg->op.left->type == PRINT_NULL)
  1705. left = 0;
  1706. else
  1707. ret = arg_num_eval(arg->op.left, &left);
  1708. if (!ret)
  1709. break;
  1710. ret = arg_num_eval(arg->op.right, &right);
  1711. if (!ret)
  1712. break;
  1713. *val = left + right;
  1714. break;
  1715. default:
  1716. do_warning("unknown op '%s'", arg->op.op);
  1717. ret = 0;
  1718. }
  1719. break;
  1720. case PRINT_NULL:
  1721. case PRINT_FIELD ... PRINT_SYMBOL:
  1722. case PRINT_STRING:
  1723. case PRINT_BSTRING:
  1724. default:
  1725. do_warning("invalid eval type %d", arg->type);
  1726. ret = 0;
  1727. }
  1728. return ret;
  1729. }
  1730. static char *arg_eval (struct print_arg *arg)
  1731. {
  1732. long long val;
  1733. static char buf[20];
  1734. switch (arg->type) {
  1735. case PRINT_ATOM:
  1736. return arg->atom.atom;
  1737. case PRINT_TYPE:
  1738. return arg_eval(arg->typecast.item);
  1739. case PRINT_OP:
  1740. if (!arg_num_eval(arg, &val))
  1741. break;
  1742. sprintf(buf, "%lld", val);
  1743. return buf;
  1744. case PRINT_NULL:
  1745. case PRINT_FIELD ... PRINT_SYMBOL:
  1746. case PRINT_STRING:
  1747. case PRINT_BSTRING:
  1748. default:
  1749. die("invalid eval type %d", arg->type);
  1750. break;
  1751. }
  1752. return NULL;
  1753. }
  1754. static enum event_type
  1755. process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
  1756. {
  1757. enum event_type type;
  1758. struct print_arg *arg = NULL;
  1759. struct print_flag_sym *field;
  1760. char *token = *tok;
  1761. char *value;
  1762. do {
  1763. free_token(token);
  1764. type = read_token_item(&token);
  1765. if (test_type_token(type, token, EVENT_OP, "{"))
  1766. break;
  1767. arg = alloc_arg();
  1768. free_token(token);
  1769. type = process_arg(event, arg, &token);
  1770. if (type == EVENT_OP)
  1771. type = process_op(event, arg, &token);
  1772. if (type == EVENT_ERROR)
  1773. goto out_free;
  1774. if (test_type_token(type, token, EVENT_DELIM, ","))
  1775. goto out_free;
  1776. field = malloc_or_die(sizeof(*field));
  1777. memset(field, 0, sizeof(*field));
  1778. value = arg_eval(arg);
  1779. if (value == NULL)
  1780. goto out_free;
  1781. field->value = strdup(value);
  1782. free_arg(arg);
  1783. arg = alloc_arg();
  1784. free_token(token);
  1785. type = process_arg(event, arg, &token);
  1786. if (test_type_token(type, token, EVENT_OP, "}"))
  1787. goto out_free;
  1788. value = arg_eval(arg);
  1789. if (value == NULL)
  1790. goto out_free;
  1791. field->str = strdup(value);
  1792. free_arg(arg);
  1793. arg = NULL;
  1794. *list = field;
  1795. list = &field->next;
  1796. free_token(token);
  1797. type = read_token_item(&token);
  1798. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  1799. *tok = token;
  1800. return type;
  1801. out_free:
  1802. free_arg(arg);
  1803. free_token(token);
  1804. *tok = NULL;
  1805. return EVENT_ERROR;
  1806. }
  1807. static enum event_type
  1808. process_flags(struct event_format *event, struct print_arg *arg, char **tok)
  1809. {
  1810. struct print_arg *field;
  1811. enum event_type type;
  1812. char *token;
  1813. memset(arg, 0, sizeof(*arg));
  1814. arg->type = PRINT_FLAGS;
  1815. field = alloc_arg();
  1816. type = process_arg(event, field, &token);
  1817. /* Handle operations in the first argument */
  1818. while (type == EVENT_OP)
  1819. type = process_op(event, field, &token);
  1820. if (test_type_token(type, token, EVENT_DELIM, ","))
  1821. goto out_free;
  1822. free_token(token);
  1823. arg->flags.field = field;
  1824. type = read_token_item(&token);
  1825. if (event_item_type(type)) {
  1826. arg->flags.delim = token;
  1827. type = read_token_item(&token);
  1828. }
  1829. if (test_type_token(type, token, EVENT_DELIM, ","))
  1830. goto out_free;
  1831. type = process_fields(event, &arg->flags.flags, &token);
  1832. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1833. goto out_free;
  1834. free_token(token);
  1835. type = read_token_item(tok);
  1836. return type;
  1837. out_free:
  1838. free_token(token);
  1839. *tok = NULL;
  1840. return EVENT_ERROR;
  1841. }
  1842. static enum event_type
  1843. process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
  1844. {
  1845. struct print_arg *field;
  1846. enum event_type type;
  1847. char *token;
  1848. memset(arg, 0, sizeof(*arg));
  1849. arg->type = PRINT_SYMBOL;
  1850. field = alloc_arg();
  1851. type = process_arg(event, field, &token);
  1852. if (test_type_token(type, token, EVENT_DELIM, ","))
  1853. goto out_free;
  1854. arg->symbol.field = field;
  1855. type = process_fields(event, &arg->symbol.symbols, &token);
  1856. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1857. goto out_free;
  1858. free_token(token);
  1859. type = read_token_item(tok);
  1860. return type;
  1861. out_free:
  1862. free_token(token);
  1863. *tok = NULL;
  1864. return EVENT_ERROR;
  1865. }
  1866. static enum event_type
  1867. process_hex(struct event_format *event, struct print_arg *arg, char **tok)
  1868. {
  1869. struct print_arg *field;
  1870. enum event_type type;
  1871. char *token;
  1872. memset(arg, 0, sizeof(*arg));
  1873. arg->type = PRINT_HEX;
  1874. field = alloc_arg();
  1875. type = process_arg(event, field, &token);
  1876. if (test_type_token(type, token, EVENT_DELIM, ","))
  1877. goto out_free;
  1878. arg->hex.field = field;
  1879. free_token(token);
  1880. field = alloc_arg();
  1881. type = process_arg(event, field, &token);
  1882. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1883. goto out_free;
  1884. arg->hex.size = field;
  1885. free_token(token);
  1886. type = read_token_item(tok);
  1887. return type;
  1888. out_free:
  1889. free_arg(field);
  1890. free_token(token);
  1891. *tok = NULL;
  1892. return EVENT_ERROR;
  1893. }
  1894. static enum event_type
  1895. process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
  1896. {
  1897. struct format_field *field;
  1898. enum event_type type;
  1899. char *token;
  1900. memset(arg, 0, sizeof(*arg));
  1901. arg->type = PRINT_DYNAMIC_ARRAY;
  1902. /*
  1903. * The item within the parenthesis is another field that holds
  1904. * the index into where the array starts.
  1905. */
  1906. type = read_token(&token);
  1907. *tok = token;
  1908. if (type != EVENT_ITEM)
  1909. goto out_free;
  1910. /* Find the field */
  1911. field = pevent_find_field(event, token);
  1912. if (!field)
  1913. goto out_free;
  1914. arg->dynarray.field = field;
  1915. arg->dynarray.index = 0;
  1916. if (read_expected(EVENT_DELIM, ")") < 0)
  1917. goto out_free;
  1918. free_token(token);
  1919. type = read_token_item(&token);
  1920. *tok = token;
  1921. if (type != EVENT_OP || strcmp(token, "[") != 0)
  1922. return type;
  1923. free_token(token);
  1924. arg = alloc_arg();
  1925. type = process_arg(event, arg, &token);
  1926. if (type == EVENT_ERROR)
  1927. goto out_free_arg;
  1928. if (!test_type_token(type, token, EVENT_OP, "]"))
  1929. goto out_free_arg;
  1930. free_token(token);
  1931. type = read_token_item(tok);
  1932. return type;
  1933. out_free_arg:
  1934. free_arg(arg);
  1935. out_free:
  1936. free_token(token);
  1937. *tok = NULL;
  1938. return EVENT_ERROR;
  1939. }
  1940. static enum event_type
  1941. process_paren(struct event_format *event, struct print_arg *arg, char **tok)
  1942. {
  1943. struct print_arg *item_arg;
  1944. enum event_type type;
  1945. char *token;
  1946. type = process_arg(event, arg, &token);
  1947. if (type == EVENT_ERROR)
  1948. goto out_free;
  1949. if (type == EVENT_OP)
  1950. type = process_op(event, arg, &token);
  1951. if (type == EVENT_ERROR)
  1952. goto out_free;
  1953. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1954. goto out_free;
  1955. free_token(token);
  1956. type = read_token_item(&token);
  1957. /*
  1958. * If the next token is an item or another open paren, then
  1959. * this was a typecast.
  1960. */
  1961. if (event_item_type(type) ||
  1962. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  1963. /* make this a typecast and contine */
  1964. /* prevous must be an atom */
  1965. if (arg->type != PRINT_ATOM)
  1966. die("previous needed to be PRINT_ATOM");
  1967. item_arg = alloc_arg();
  1968. arg->type = PRINT_TYPE;
  1969. arg->typecast.type = arg->atom.atom;
  1970. arg->typecast.item = item_arg;
  1971. type = process_arg_token(event, item_arg, &token, type);
  1972. }
  1973. *tok = token;
  1974. return type;
  1975. out_free:
  1976. free_token(token);
  1977. *tok = NULL;
  1978. return EVENT_ERROR;
  1979. }
  1980. static enum event_type
  1981. process_str(struct event_format *event __unused, struct print_arg *arg, char **tok)
  1982. {
  1983. enum event_type type;
  1984. char *token;
  1985. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1986. goto out_free;
  1987. arg->type = PRINT_STRING;
  1988. arg->string.string = token;
  1989. arg->string.offset = -1;
  1990. if (read_expected(EVENT_DELIM, ")") < 0)
  1991. goto out_err;
  1992. type = read_token(&token);
  1993. *tok = token;
  1994. return type;
  1995. out_free:
  1996. free_token(token);
  1997. out_err:
  1998. *tok = NULL;
  1999. return EVENT_ERROR;
  2000. }
  2001. static struct pevent_function_handler *
  2002. find_func_handler(struct pevent *pevent, char *func_name)
  2003. {
  2004. struct pevent_function_handler *func;
  2005. for (func = pevent->func_handlers; func; func = func->next) {
  2006. if (strcmp(func->name, func_name) == 0)
  2007. break;
  2008. }
  2009. return func;
  2010. }
  2011. static void remove_func_handler(struct pevent *pevent, char *func_name)
  2012. {
  2013. struct pevent_function_handler *func;
  2014. struct pevent_function_handler **next;
  2015. next = &pevent->func_handlers;
  2016. while ((func = *next)) {
  2017. if (strcmp(func->name, func_name) == 0) {
  2018. *next = func->next;
  2019. free_func_handle(func);
  2020. break;
  2021. }
  2022. next = &func->next;
  2023. }
  2024. }
  2025. static enum event_type
  2026. process_func_handler(struct event_format *event, struct pevent_function_handler *func,
  2027. struct print_arg *arg, char **tok)
  2028. {
  2029. struct print_arg **next_arg;
  2030. struct print_arg *farg;
  2031. enum event_type type;
  2032. char *token;
  2033. char *test;
  2034. int i;
  2035. arg->type = PRINT_FUNC;
  2036. arg->func.func = func;
  2037. *tok = NULL;
  2038. next_arg = &(arg->func.args);
  2039. for (i = 0; i < func->nr_args; i++) {
  2040. farg = alloc_arg();
  2041. type = process_arg(event, farg, &token);
  2042. if (i < (func->nr_args - 1))
  2043. test = ",";
  2044. else
  2045. test = ")";
  2046. if (test_type_token(type, token, EVENT_DELIM, test)) {
  2047. free_arg(farg);
  2048. free_token(token);
  2049. return EVENT_ERROR;
  2050. }
  2051. *next_arg = farg;
  2052. next_arg = &(farg->next);
  2053. free_token(token);
  2054. }
  2055. type = read_token(&token);
  2056. *tok = token;
  2057. return type;
  2058. }
  2059. static enum event_type
  2060. process_function(struct event_format *event, struct print_arg *arg,
  2061. char *token, char **tok)
  2062. {
  2063. struct pevent_function_handler *func;
  2064. if (strcmp(token, "__print_flags") == 0) {
  2065. free_token(token);
  2066. is_flag_field = 1;
  2067. return process_flags(event, arg, tok);
  2068. }
  2069. if (strcmp(token, "__print_symbolic") == 0) {
  2070. free_token(token);
  2071. is_symbolic_field = 1;
  2072. return process_symbols(event, arg, tok);
  2073. }
  2074. if (strcmp(token, "__print_hex") == 0) {
  2075. free_token(token);
  2076. return process_hex(event, arg, tok);
  2077. }
  2078. if (strcmp(token, "__get_str") == 0) {
  2079. free_token(token);
  2080. return process_str(event, arg, tok);
  2081. }
  2082. if (strcmp(token, "__get_dynamic_array") == 0) {
  2083. free_token(token);
  2084. return process_dynamic_array(event, arg, tok);
  2085. }
  2086. func = find_func_handler(event->pevent, token);
  2087. if (func) {
  2088. free_token(token);
  2089. return process_func_handler(event, func, arg, tok);
  2090. }
  2091. do_warning("function %s not defined", token);
  2092. free_token(token);
  2093. return EVENT_ERROR;
  2094. }
  2095. static enum event_type
  2096. process_arg_token(struct event_format *event, struct print_arg *arg,
  2097. char **tok, enum event_type type)
  2098. {
  2099. char *token;
  2100. char *atom;
  2101. token = *tok;
  2102. switch (type) {
  2103. case EVENT_ITEM:
  2104. if (strcmp(token, "REC") == 0) {
  2105. free_token(token);
  2106. type = process_entry(event, arg, &token);
  2107. break;
  2108. }
  2109. atom = token;
  2110. /* test the next token */
  2111. type = read_token_item(&token);
  2112. /*
  2113. * If the next token is a parenthesis, then this
  2114. * is a function.
  2115. */
  2116. if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
  2117. free_token(token);
  2118. token = NULL;
  2119. /* this will free atom. */
  2120. type = process_function(event, arg, atom, &token);
  2121. break;
  2122. }
  2123. /* atoms can be more than one token long */
  2124. while (type == EVENT_ITEM) {
  2125. atom = realloc(atom, strlen(atom) + strlen(token) + 2);
  2126. strcat(atom, " ");
  2127. strcat(atom, token);
  2128. free_token(token);
  2129. type = read_token_item(&token);
  2130. }
  2131. arg->type = PRINT_ATOM;
  2132. arg->atom.atom = atom;
  2133. break;
  2134. case EVENT_DQUOTE:
  2135. case EVENT_SQUOTE:
  2136. arg->type = PRINT_ATOM;
  2137. arg->atom.atom = token;
  2138. type = read_token_item(&token);
  2139. break;
  2140. case EVENT_DELIM:
  2141. if (strcmp(token, "(") == 0) {
  2142. free_token(token);
  2143. type = process_paren(event, arg, &token);
  2144. break;
  2145. }
  2146. case EVENT_OP:
  2147. /* handle single ops */
  2148. arg->type = PRINT_OP;
  2149. arg->op.op = token;
  2150. arg->op.left = NULL;
  2151. type = process_op(event, arg, &token);
  2152. /* On error, the op is freed */
  2153. if (type == EVENT_ERROR)
  2154. arg->op.op = NULL;
  2155. /* return error type if errored */
  2156. break;
  2157. case EVENT_ERROR ... EVENT_NEWLINE:
  2158. default:
  2159. die("unexpected type %d", type);
  2160. }
  2161. *tok = token;
  2162. return type;
  2163. }
  2164. static int event_read_print_args(struct event_format *event, struct print_arg **list)
  2165. {
  2166. enum event_type type = EVENT_ERROR;
  2167. struct print_arg *arg;
  2168. char *token;
  2169. int args = 0;
  2170. do {
  2171. if (type == EVENT_NEWLINE) {
  2172. type = read_token_item(&token);
  2173. continue;
  2174. }
  2175. arg = alloc_arg();
  2176. type = process_arg(event, arg, &token);
  2177. if (type == EVENT_ERROR) {
  2178. free_token(token);
  2179. free_arg(arg);
  2180. return -1;
  2181. }
  2182. *list = arg;
  2183. args++;
  2184. if (type == EVENT_OP) {
  2185. type = process_op(event, arg, &token);
  2186. free_token(token);
  2187. if (type == EVENT_ERROR) {
  2188. *list = NULL;
  2189. free_arg(arg);
  2190. return -1;
  2191. }
  2192. list = &arg->next;
  2193. continue;
  2194. }
  2195. if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
  2196. free_token(token);
  2197. *list = arg;
  2198. list = &arg->next;
  2199. continue;
  2200. }
  2201. break;
  2202. } while (type != EVENT_NONE);
  2203. if (type != EVENT_NONE && type != EVENT_ERROR)
  2204. free_token(token);
  2205. return args;
  2206. }
  2207. static int event_read_print(struct event_format *event)
  2208. {
  2209. enum event_type type;
  2210. char *token;
  2211. int ret;
  2212. if (read_expected_item(EVENT_ITEM, "print") < 0)
  2213. return -1;
  2214. if (read_expected(EVENT_ITEM, "fmt") < 0)
  2215. return -1;
  2216. if (read_expected(EVENT_OP, ":") < 0)
  2217. return -1;
  2218. if (read_expect_type(EVENT_DQUOTE, &token) < 0)
  2219. goto fail;
  2220. concat:
  2221. event->print_fmt.format = token;
  2222. event->print_fmt.args = NULL;
  2223. /* ok to have no arg */
  2224. type = read_token_item(&token);
  2225. if (type == EVENT_NONE)
  2226. return 0;
  2227. /* Handle concatenation of print lines */
  2228. if (type == EVENT_DQUOTE) {
  2229. char *cat;
  2230. cat = malloc_or_die(strlen(event->print_fmt.format) +
  2231. strlen(token) + 1);
  2232. strcpy(cat, event->print_fmt.format);
  2233. strcat(cat, token);
  2234. free_token(token);
  2235. free_token(event->print_fmt.format);
  2236. event->print_fmt.format = NULL;
  2237. token = cat;
  2238. goto concat;
  2239. }
  2240. if (test_type_token(type, token, EVENT_DELIM, ","))
  2241. goto fail;
  2242. free_token(token);
  2243. ret = event_read_print_args(event, &event->print_fmt.args);
  2244. if (ret < 0)
  2245. return -1;
  2246. return ret;
  2247. fail:
  2248. free_token(token);
  2249. return -1;
  2250. }
  2251. /**
  2252. * pevent_find_common_field - return a common field by event
  2253. * @event: handle for the event
  2254. * @name: the name of the common field to return
  2255. *
  2256. * Returns a common field from the event by the given @name.
  2257. * This only searchs the common fields and not all field.
  2258. */
  2259. struct format_field *
  2260. pevent_find_common_field(struct event_format *event, const char *name)
  2261. {
  2262. struct format_field *format;
  2263. for (format = event->format.common_fields;
  2264. format; format = format->next) {
  2265. if (strcmp(format->name, name) == 0)
  2266. break;
  2267. }
  2268. return format;
  2269. }
  2270. /**
  2271. * pevent_find_field - find a non-common field
  2272. * @event: handle for the event
  2273. * @name: the name of the non-common field
  2274. *
  2275. * Returns a non-common field by the given @name.
  2276. * This does not search common fields.
  2277. */
  2278. struct format_field *
  2279. pevent_find_field(struct event_format *event, const char *name)
  2280. {
  2281. struct format_field *format;
  2282. for (format = event->format.fields;
  2283. format; format = format->next) {
  2284. if (strcmp(format->name, name) == 0)
  2285. break;
  2286. }
  2287. return format;
  2288. }
  2289. /**
  2290. * pevent_find_any_field - find any field by name
  2291. * @event: handle for the event
  2292. * @name: the name of the field
  2293. *
  2294. * Returns a field by the given @name.
  2295. * This searchs the common field names first, then
  2296. * the non-common ones if a common one was not found.
  2297. */
  2298. struct format_field *
  2299. pevent_find_any_field(struct event_format *event, const char *name)
  2300. {
  2301. struct format_field *format;
  2302. format = pevent_find_common_field(event, name);
  2303. if (format)
  2304. return format;
  2305. return pevent_find_field(event, name);
  2306. }
  2307. /**
  2308. * pevent_read_number - read a number from data
  2309. * @pevent: handle for the pevent
  2310. * @ptr: the raw data
  2311. * @size: the size of the data that holds the number
  2312. *
  2313. * Returns the number (converted to host) from the
  2314. * raw data.
  2315. */
  2316. unsigned long long pevent_read_number(struct pevent *pevent,
  2317. const void *ptr, int size)
  2318. {
  2319. switch (size) {
  2320. case 1:
  2321. return *(unsigned char *)ptr;
  2322. case 2:
  2323. return data2host2(pevent, ptr);
  2324. case 4:
  2325. return data2host4(pevent, ptr);
  2326. case 8:
  2327. return data2host8(pevent, ptr);
  2328. default:
  2329. /* BUG! */
  2330. return 0;
  2331. }
  2332. }
  2333. /**
  2334. * pevent_read_number_field - read a number from data
  2335. * @field: a handle to the field
  2336. * @data: the raw data to read
  2337. * @value: the value to place the number in
  2338. *
  2339. * Reads raw data according to a field offset and size,
  2340. * and translates it into @value.
  2341. *
  2342. * Returns 0 on success, -1 otherwise.
  2343. */
  2344. int pevent_read_number_field(struct format_field *field, const void *data,
  2345. unsigned long long *value)
  2346. {
  2347. if (!field)
  2348. return -1;
  2349. switch (field->size) {
  2350. case 1:
  2351. case 2:
  2352. case 4:
  2353. case 8:
  2354. *value = pevent_read_number(field->event->pevent,
  2355. data + field->offset, field->size);
  2356. return 0;
  2357. default:
  2358. return -1;
  2359. }
  2360. }
  2361. static int get_common_info(struct pevent *pevent,
  2362. const char *type, int *offset, int *size)
  2363. {
  2364. struct event_format *event;
  2365. struct format_field *field;
  2366. /*
  2367. * All events should have the same common elements.
  2368. * Pick any event to find where the type is;
  2369. */
  2370. if (!pevent->events)
  2371. die("no event_list!");
  2372. event = pevent->events[0];
  2373. field = pevent_find_common_field(event, type);
  2374. if (!field)
  2375. die("field '%s' not found", type);
  2376. *offset = field->offset;
  2377. *size = field->size;
  2378. return 0;
  2379. }
  2380. static int __parse_common(struct pevent *pevent, void *data,
  2381. int *size, int *offset, const char *name)
  2382. {
  2383. int ret;
  2384. if (!*size) {
  2385. ret = get_common_info(pevent, name, offset, size);
  2386. if (ret < 0)
  2387. return ret;
  2388. }
  2389. return pevent_read_number(pevent, data + *offset, *size);
  2390. }
  2391. static int trace_parse_common_type(struct pevent *pevent, void *data)
  2392. {
  2393. return __parse_common(pevent, data,
  2394. &pevent->type_size, &pevent->type_offset,
  2395. "common_type");
  2396. }
  2397. static int parse_common_pid(struct pevent *pevent, void *data)
  2398. {
  2399. return __parse_common(pevent, data,
  2400. &pevent->pid_size, &pevent->pid_offset,
  2401. "common_pid");
  2402. }
  2403. static int parse_common_pc(struct pevent *pevent, void *data)
  2404. {
  2405. return __parse_common(pevent, data,
  2406. &pevent->pc_size, &pevent->pc_offset,
  2407. "common_preempt_count");
  2408. }
  2409. static int parse_common_flags(struct pevent *pevent, void *data)
  2410. {
  2411. return __parse_common(pevent, data,
  2412. &pevent->flags_size, &pevent->flags_offset,
  2413. "common_flags");
  2414. }
  2415. static int parse_common_lock_depth(struct pevent *pevent, void *data)
  2416. {
  2417. int ret;
  2418. ret = __parse_common(pevent, data,
  2419. &pevent->ld_size, &pevent->ld_offset,
  2420. "common_lock_depth");
  2421. if (ret < 0)
  2422. return -1;
  2423. return ret;
  2424. }
  2425. static int events_id_cmp(const void *a, const void *b);
  2426. /**
  2427. * pevent_find_event - find an event by given id
  2428. * @pevent: a handle to the pevent
  2429. * @id: the id of the event
  2430. *
  2431. * Returns an event that has a given @id.
  2432. */
  2433. struct event_format *pevent_find_event(struct pevent *pevent, int id)
  2434. {
  2435. struct event_format **eventptr;
  2436. struct event_format key;
  2437. struct event_format *pkey = &key;
  2438. /* Check cache first */
  2439. if (pevent->last_event && pevent->last_event->id == id)
  2440. return pevent->last_event;
  2441. key.id = id;
  2442. eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
  2443. sizeof(*pevent->events), events_id_cmp);
  2444. if (eventptr) {
  2445. pevent->last_event = *eventptr;
  2446. return *eventptr;
  2447. }
  2448. return NULL;
  2449. }
  2450. /**
  2451. * pevent_find_event_by_name - find an event by given name
  2452. * @pevent: a handle to the pevent
  2453. * @sys: the system name to search for
  2454. * @name: the name of the event to search for
  2455. *
  2456. * This returns an event with a given @name and under the system
  2457. * @sys. If @sys is NULL the first event with @name is returned.
  2458. */
  2459. struct event_format *
  2460. pevent_find_event_by_name(struct pevent *pevent,
  2461. const char *sys, const char *name)
  2462. {
  2463. struct event_format *event;
  2464. int i;
  2465. if (pevent->last_event &&
  2466. strcmp(pevent->last_event->name, name) == 0 &&
  2467. (!sys || strcmp(pevent->last_event->system, sys) == 0))
  2468. return pevent->last_event;
  2469. for (i = 0; i < pevent->nr_events; i++) {
  2470. event = pevent->events[i];
  2471. if (strcmp(event->name, name) == 0) {
  2472. if (!sys)
  2473. break;
  2474. if (strcmp(event->system, sys) == 0)
  2475. break;
  2476. }
  2477. }
  2478. if (i == pevent->nr_events)
  2479. event = NULL;
  2480. pevent->last_event = event;
  2481. return event;
  2482. }
  2483. static unsigned long long
  2484. eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
  2485. {
  2486. struct pevent *pevent = event->pevent;
  2487. unsigned long long val = 0;
  2488. unsigned long long left, right;
  2489. struct print_arg *typearg = NULL;
  2490. struct print_arg *larg;
  2491. unsigned long offset;
  2492. unsigned int field_size;
  2493. switch (arg->type) {
  2494. case PRINT_NULL:
  2495. /* ?? */
  2496. return 0;
  2497. case PRINT_ATOM:
  2498. return strtoull(arg->atom.atom, NULL, 0);
  2499. case PRINT_FIELD:
  2500. if (!arg->field.field) {
  2501. arg->field.field = pevent_find_any_field(event, arg->field.name);
  2502. if (!arg->field.field)
  2503. die("field %s not found", arg->field.name);
  2504. }
  2505. /* must be a number */
  2506. val = pevent_read_number(pevent, data + arg->field.field->offset,
  2507. arg->field.field->size);
  2508. break;
  2509. case PRINT_FLAGS:
  2510. case PRINT_SYMBOL:
  2511. case PRINT_HEX:
  2512. break;
  2513. case PRINT_TYPE:
  2514. val = eval_num_arg(data, size, event, arg->typecast.item);
  2515. return eval_type(val, arg, 0);
  2516. case PRINT_STRING:
  2517. case PRINT_BSTRING:
  2518. return 0;
  2519. case PRINT_FUNC: {
  2520. struct trace_seq s;
  2521. trace_seq_init(&s);
  2522. val = process_defined_func(&s, data, size, event, arg);
  2523. trace_seq_destroy(&s);
  2524. return val;
  2525. }
  2526. case PRINT_OP:
  2527. if (strcmp(arg->op.op, "[") == 0) {
  2528. /*
  2529. * Arrays are special, since we don't want
  2530. * to read the arg as is.
  2531. */
  2532. right = eval_num_arg(data, size, event, arg->op.right);
  2533. /* handle typecasts */
  2534. larg = arg->op.left;
  2535. while (larg->type == PRINT_TYPE) {
  2536. if (!typearg)
  2537. typearg = larg;
  2538. larg = larg->typecast.item;
  2539. }
  2540. /* Default to long size */
  2541. field_size = pevent->long_size;
  2542. switch (larg->type) {
  2543. case PRINT_DYNAMIC_ARRAY:
  2544. offset = pevent_read_number(pevent,
  2545. data + larg->dynarray.field->offset,
  2546. larg->dynarray.field->size);
  2547. if (larg->dynarray.field->elementsize)
  2548. field_size = larg->dynarray.field->elementsize;
  2549. /*
  2550. * The actual length of the dynamic array is stored
  2551. * in the top half of the field, and the offset
  2552. * is in the bottom half of the 32 bit field.
  2553. */
  2554. offset &= 0xffff;
  2555. offset += right;
  2556. break;
  2557. case PRINT_FIELD:
  2558. if (!larg->field.field) {
  2559. larg->field.field =
  2560. pevent_find_any_field(event, larg->field.name);
  2561. if (!larg->field.field)
  2562. die("field %s not found", larg->field.name);
  2563. }
  2564. field_size = larg->field.field->elementsize;
  2565. offset = larg->field.field->offset +
  2566. right * larg->field.field->elementsize;
  2567. break;
  2568. default:
  2569. goto default_op; /* oops, all bets off */
  2570. }
  2571. val = pevent_read_number(pevent,
  2572. data + offset, field_size);
  2573. if (typearg)
  2574. val = eval_type(val, typearg, 1);
  2575. break;
  2576. } else if (strcmp(arg->op.op, "?") == 0) {
  2577. left = eval_num_arg(data, size, event, arg->op.left);
  2578. arg = arg->op.right;
  2579. if (left)
  2580. val = eval_num_arg(data, size, event, arg->op.left);
  2581. else
  2582. val = eval_num_arg(data, size, event, arg->op.right);
  2583. break;
  2584. }
  2585. default_op:
  2586. left = eval_num_arg(data, size, event, arg->op.left);
  2587. right = eval_num_arg(data, size, event, arg->op.right);
  2588. switch (arg->op.op[0]) {
  2589. case '!':
  2590. switch (arg->op.op[1]) {
  2591. case 0:
  2592. val = !right;
  2593. break;
  2594. case '=':
  2595. val = left != right;
  2596. break;
  2597. default:
  2598. die("unknown op '%s'", arg->op.op);
  2599. }
  2600. break;
  2601. case '~':
  2602. val = ~right;
  2603. break;
  2604. case '|':
  2605. if (arg->op.op[1])
  2606. val = left || right;
  2607. else
  2608. val = left | right;
  2609. break;
  2610. case '&':
  2611. if (arg->op.op[1])
  2612. val = left && right;
  2613. else
  2614. val = left & right;
  2615. break;
  2616. case '<':
  2617. switch (arg->op.op[1]) {
  2618. case 0:
  2619. val = left < right;
  2620. break;
  2621. case '<':
  2622. val = left << right;
  2623. break;
  2624. case '=':
  2625. val = left <= right;
  2626. break;
  2627. default:
  2628. die("unknown op '%s'", arg->op.op);
  2629. }
  2630. break;
  2631. case '>':
  2632. switch (arg->op.op[1]) {
  2633. case 0:
  2634. val = left > right;
  2635. break;
  2636. case '>':
  2637. val = left >> right;
  2638. break;
  2639. case '=':
  2640. val = left >= right;
  2641. break;
  2642. default:
  2643. die("unknown op '%s'", arg->op.op);
  2644. }
  2645. break;
  2646. case '=':
  2647. if (arg->op.op[1] != '=')
  2648. die("unknown op '%s'", arg->op.op);
  2649. val = left == right;
  2650. break;
  2651. case '-':
  2652. val = left - right;
  2653. break;
  2654. case '+':
  2655. val = left + right;
  2656. break;
  2657. case '/':
  2658. val = left / right;
  2659. break;
  2660. case '*':
  2661. val = left * right;
  2662. break;
  2663. default:
  2664. die("unknown op '%s'", arg->op.op);
  2665. }
  2666. break;
  2667. default: /* not sure what to do there */
  2668. return 0;
  2669. }
  2670. return val;
  2671. }
  2672. struct flag {
  2673. const char *name;
  2674. unsigned long long value;
  2675. };
  2676. static const struct flag flags[] = {
  2677. { "HI_SOFTIRQ", 0 },
  2678. { "TIMER_SOFTIRQ", 1 },
  2679. { "NET_TX_SOFTIRQ", 2 },
  2680. { "NET_RX_SOFTIRQ", 3 },
  2681. { "BLOCK_SOFTIRQ", 4 },
  2682. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  2683. { "TASKLET_SOFTIRQ", 6 },
  2684. { "SCHED_SOFTIRQ", 7 },
  2685. { "HRTIMER_SOFTIRQ", 8 },
  2686. { "RCU_SOFTIRQ", 9 },
  2687. { "HRTIMER_NORESTART", 0 },
  2688. { "HRTIMER_RESTART", 1 },
  2689. };
  2690. static unsigned long long eval_flag(const char *flag)
  2691. {
  2692. int i;
  2693. /*
  2694. * Some flags in the format files do not get converted.
  2695. * If the flag is not numeric, see if it is something that
  2696. * we already know about.
  2697. */
  2698. if (isdigit(flag[0]))
  2699. return strtoull(flag, NULL, 0);
  2700. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  2701. if (strcmp(flags[i].name, flag) == 0)
  2702. return flags[i].value;
  2703. return 0;
  2704. }
  2705. static void print_str_to_seq(struct trace_seq *s, const char *format,
  2706. int len_arg, const char *str)
  2707. {
  2708. if (len_arg >= 0)
  2709. trace_seq_printf(s, format, len_arg, str);
  2710. else
  2711. trace_seq_printf(s, format, str);
  2712. }
  2713. static void print_str_arg(struct trace_seq *s, void *data, int size,
  2714. struct event_format *event, const char *format,
  2715. int len_arg, struct print_arg *arg)
  2716. {
  2717. struct pevent *pevent = event->pevent;
  2718. struct print_flag_sym *flag;
  2719. struct format_field *field;
  2720. unsigned long long val, fval;
  2721. unsigned long addr;
  2722. char *str;
  2723. unsigned char *hex;
  2724. int print;
  2725. int i, len;
  2726. switch (arg->type) {
  2727. case PRINT_NULL:
  2728. /* ?? */
  2729. return;
  2730. case PRINT_ATOM:
  2731. print_str_to_seq(s, format, len_arg, arg->atom.atom);
  2732. return;
  2733. case PRINT_FIELD:
  2734. field = arg->field.field;
  2735. if (!field) {
  2736. field = pevent_find_any_field(event, arg->field.name);
  2737. if (!field)
  2738. die("field %s not found", arg->field.name);
  2739. arg->field.field = field;
  2740. }
  2741. /* Zero sized fields, mean the rest of the data */
  2742. len = field->size ? : size - field->offset;
  2743. /*
  2744. * Some events pass in pointers. If this is not an array
  2745. * and the size is the same as long_size, assume that it
  2746. * is a pointer.
  2747. */
  2748. if (!(field->flags & FIELD_IS_ARRAY) &&
  2749. field->size == pevent->long_size) {
  2750. addr = *(unsigned long *)(data + field->offset);
  2751. trace_seq_printf(s, "%lx", addr);
  2752. break;
  2753. }
  2754. str = malloc_or_die(len + 1);
  2755. memcpy(str, data + field->offset, len);
  2756. str[len] = 0;
  2757. print_str_to_seq(s, format, len_arg, str);
  2758. free(str);
  2759. break;
  2760. case PRINT_FLAGS:
  2761. val = eval_num_arg(data, size, event, arg->flags.field);
  2762. print = 0;
  2763. for (flag = arg->flags.flags; flag; flag = flag->next) {
  2764. fval = eval_flag(flag->value);
  2765. if (!val && !fval) {
  2766. print_str_to_seq(s, format, len_arg, flag->str);
  2767. break;
  2768. }
  2769. if (fval && (val & fval) == fval) {
  2770. if (print && arg->flags.delim)
  2771. trace_seq_puts(s, arg->flags.delim);
  2772. print_str_to_seq(s, format, len_arg, flag->str);
  2773. print = 1;
  2774. val &= ~fval;
  2775. }
  2776. }
  2777. break;
  2778. case PRINT_SYMBOL:
  2779. val = eval_num_arg(data, size, event, arg->symbol.field);
  2780. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  2781. fval = eval_flag(flag->value);
  2782. if (val == fval) {
  2783. print_str_to_seq(s, format, len_arg, flag->str);
  2784. break;
  2785. }
  2786. }
  2787. break;
  2788. case PRINT_HEX:
  2789. field = arg->hex.field->field.field;
  2790. if (!field) {
  2791. str = arg->hex.field->field.name;
  2792. field = pevent_find_any_field(event, str);
  2793. if (!field)
  2794. die("field %s not found", str);
  2795. arg->hex.field->field.field = field;
  2796. }
  2797. hex = data + field->offset;
  2798. len = eval_num_arg(data, size, event, arg->hex.size);
  2799. for (i = 0; i < len; i++) {
  2800. if (i)
  2801. trace_seq_putc(s, ' ');
  2802. trace_seq_printf(s, "%02x", hex[i]);
  2803. }
  2804. break;
  2805. case PRINT_TYPE:
  2806. break;
  2807. case PRINT_STRING: {
  2808. int str_offset;
  2809. if (arg->string.offset == -1) {
  2810. struct format_field *f;
  2811. f = pevent_find_any_field(event, arg->string.string);
  2812. arg->string.offset = f->offset;
  2813. }
  2814. str_offset = data2host4(pevent, data + arg->string.offset);
  2815. str_offset &= 0xffff;
  2816. print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
  2817. break;
  2818. }
  2819. case PRINT_BSTRING:
  2820. trace_seq_printf(s, format, arg->string.string);
  2821. break;
  2822. case PRINT_OP:
  2823. /*
  2824. * The only op for string should be ? :
  2825. */
  2826. if (arg->op.op[0] != '?')
  2827. return;
  2828. val = eval_num_arg(data, size, event, arg->op.left);
  2829. if (val)
  2830. print_str_arg(s, data, size, event,
  2831. format, len_arg, arg->op.right->op.left);
  2832. else
  2833. print_str_arg(s, data, size, event,
  2834. format, len_arg, arg->op.right->op.right);
  2835. break;
  2836. case PRINT_FUNC:
  2837. process_defined_func(s, data, size, event, arg);
  2838. break;
  2839. default:
  2840. /* well... */
  2841. break;
  2842. }
  2843. }
  2844. static unsigned long long
  2845. process_defined_func(struct trace_seq *s, void *data, int size,
  2846. struct event_format *event, struct print_arg *arg)
  2847. {
  2848. struct pevent_function_handler *func_handle = arg->func.func;
  2849. struct pevent_func_params *param;
  2850. unsigned long long *args;
  2851. unsigned long long ret;
  2852. struct print_arg *farg;
  2853. struct trace_seq str;
  2854. struct save_str {
  2855. struct save_str *next;
  2856. char *str;
  2857. } *strings = NULL, *string;
  2858. int i;
  2859. if (!func_handle->nr_args) {
  2860. ret = (*func_handle->func)(s, NULL);
  2861. goto out;
  2862. }
  2863. farg = arg->func.args;
  2864. param = func_handle->params;
  2865. args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
  2866. for (i = 0; i < func_handle->nr_args; i++) {
  2867. switch (param->type) {
  2868. case PEVENT_FUNC_ARG_INT:
  2869. case PEVENT_FUNC_ARG_LONG:
  2870. case PEVENT_FUNC_ARG_PTR:
  2871. args[i] = eval_num_arg(data, size, event, farg);
  2872. break;
  2873. case PEVENT_FUNC_ARG_STRING:
  2874. trace_seq_init(&str);
  2875. print_str_arg(&str, data, size, event, "%s", -1, farg);
  2876. trace_seq_terminate(&str);
  2877. string = malloc_or_die(sizeof(*string));
  2878. string->next = strings;
  2879. string->str = strdup(str.buffer);
  2880. strings = string;
  2881. trace_seq_destroy(&str);
  2882. break;
  2883. default:
  2884. /*
  2885. * Something went totally wrong, this is not
  2886. * an input error, something in this code broke.
  2887. */
  2888. die("Unexpected end of arguments\n");
  2889. break;
  2890. }
  2891. farg = farg->next;
  2892. param = param->next;
  2893. }
  2894. ret = (*func_handle->func)(s, args);
  2895. free(args);
  2896. while (strings) {
  2897. string = strings;
  2898. strings = string->next;
  2899. free(string->str);
  2900. free(string);
  2901. }
  2902. out:
  2903. /* TBD : handle return type here */
  2904. return ret;
  2905. }
  2906. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
  2907. {
  2908. struct pevent *pevent = event->pevent;
  2909. struct format_field *field, *ip_field;
  2910. struct print_arg *args, *arg, **next;
  2911. unsigned long long ip, val;
  2912. char *ptr;
  2913. void *bptr;
  2914. field = pevent->bprint_buf_field;
  2915. ip_field = pevent->bprint_ip_field;
  2916. if (!field) {
  2917. field = pevent_find_field(event, "buf");
  2918. if (!field)
  2919. die("can't find buffer field for binary printk");
  2920. ip_field = pevent_find_field(event, "ip");
  2921. if (!ip_field)
  2922. die("can't find ip field for binary printk");
  2923. pevent->bprint_buf_field = field;
  2924. pevent->bprint_ip_field = ip_field;
  2925. }
  2926. ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
  2927. /*
  2928. * The first arg is the IP pointer.
  2929. */
  2930. args = alloc_arg();
  2931. arg = args;
  2932. arg->next = NULL;
  2933. next = &arg->next;
  2934. arg->type = PRINT_ATOM;
  2935. arg->atom.atom = malloc_or_die(32);
  2936. sprintf(arg->atom.atom, "%lld", ip);
  2937. /* skip the first "%pf : " */
  2938. for (ptr = fmt + 6, bptr = data + field->offset;
  2939. bptr < data + size && *ptr; ptr++) {
  2940. int ls = 0;
  2941. if (*ptr == '%') {
  2942. process_again:
  2943. ptr++;
  2944. switch (*ptr) {
  2945. case '%':
  2946. break;
  2947. case 'l':
  2948. ls++;
  2949. goto process_again;
  2950. case 'L':
  2951. ls = 2;
  2952. goto process_again;
  2953. case '0' ... '9':
  2954. goto process_again;
  2955. case 'p':
  2956. ls = 1;
  2957. /* fall through */
  2958. case 'd':
  2959. case 'u':
  2960. case 'x':
  2961. case 'i':
  2962. /* the pointers are always 4 bytes aligned */
  2963. bptr = (void *)(((unsigned long)bptr + 3) &
  2964. ~3);
  2965. switch (ls) {
  2966. case 0:
  2967. ls = 4;
  2968. break;
  2969. case 1:
  2970. ls = pevent->long_size;
  2971. break;
  2972. case 2:
  2973. ls = 8;
  2974. default:
  2975. break;
  2976. }
  2977. val = pevent_read_number(pevent, bptr, ls);
  2978. bptr += ls;
  2979. arg = alloc_arg();
  2980. arg->next = NULL;
  2981. arg->type = PRINT_ATOM;
  2982. arg->atom.atom = malloc_or_die(32);
  2983. sprintf(arg->atom.atom, "%lld", val);
  2984. *next = arg;
  2985. next = &arg->next;
  2986. break;
  2987. case 's':
  2988. arg = alloc_arg();
  2989. arg->next = NULL;
  2990. arg->type = PRINT_BSTRING;
  2991. arg->string.string = strdup(bptr);
  2992. bptr += strlen(bptr) + 1;
  2993. *next = arg;
  2994. next = &arg->next;
  2995. default:
  2996. break;
  2997. }
  2998. }
  2999. }
  3000. return args;
  3001. }
  3002. static void free_args(struct print_arg *args)
  3003. {
  3004. struct print_arg *next;
  3005. while (args) {
  3006. next = args->next;
  3007. free_arg(args);
  3008. args = next;
  3009. }
  3010. }
  3011. static char *
  3012. get_bprint_format(void *data, int size __unused, struct event_format *event)
  3013. {
  3014. struct pevent *pevent = event->pevent;
  3015. unsigned long long addr;
  3016. struct format_field *field;
  3017. struct printk_map *printk;
  3018. char *format;
  3019. char *p;
  3020. field = pevent->bprint_fmt_field;
  3021. if (!field) {
  3022. field = pevent_find_field(event, "fmt");
  3023. if (!field)
  3024. die("can't find format field for binary printk");
  3025. pevent->bprint_fmt_field = field;
  3026. }
  3027. addr = pevent_read_number(pevent, data + field->offset, field->size);
  3028. printk = find_printk(pevent, addr);
  3029. if (!printk) {
  3030. format = malloc_or_die(45);
  3031. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  3032. addr);
  3033. return format;
  3034. }
  3035. p = printk->printk;
  3036. /* Remove any quotes. */
  3037. if (*p == '"')
  3038. p++;
  3039. format = malloc_or_die(strlen(p) + 10);
  3040. sprintf(format, "%s : %s", "%pf", p);
  3041. /* remove ending quotes and new line since we will add one too */
  3042. p = format + strlen(format) - 1;
  3043. if (*p == '"')
  3044. *p = 0;
  3045. p -= 2;
  3046. if (strcmp(p, "\\n") == 0)
  3047. *p = 0;
  3048. return format;
  3049. }
  3050. static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
  3051. struct event_format *event, struct print_arg *arg)
  3052. {
  3053. unsigned char *buf;
  3054. char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
  3055. if (arg->type == PRINT_FUNC) {
  3056. process_defined_func(s, data, size, event, arg);
  3057. return;
  3058. }
  3059. if (arg->type != PRINT_FIELD) {
  3060. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
  3061. arg->type);
  3062. return;
  3063. }
  3064. if (mac == 'm')
  3065. fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
  3066. if (!arg->field.field) {
  3067. arg->field.field =
  3068. pevent_find_any_field(event, arg->field.name);
  3069. if (!arg->field.field)
  3070. die("field %s not found", arg->field.name);
  3071. }
  3072. if (arg->field.field->size != 6) {
  3073. trace_seq_printf(s, "INVALIDMAC");
  3074. return;
  3075. }
  3076. buf = data + arg->field.field->offset;
  3077. trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  3078. }
  3079. static int is_printable_array(char *p, unsigned int len)
  3080. {
  3081. unsigned int i;
  3082. for (i = 0; i < len && p[i]; i++)
  3083. if (!isprint(p[i]))
  3084. return 0;
  3085. return 1;
  3086. }
  3087. static void print_event_fields(struct trace_seq *s, void *data, int size,
  3088. struct event_format *event)
  3089. {
  3090. struct format_field *field;
  3091. unsigned long long val;
  3092. unsigned int offset, len, i;
  3093. field = event->format.fields;
  3094. while (field) {
  3095. trace_seq_printf(s, " %s=", field->name);
  3096. if (field->flags & FIELD_IS_ARRAY) {
  3097. offset = field->offset;
  3098. len = field->size;
  3099. if (field->flags & FIELD_IS_DYNAMIC) {
  3100. val = pevent_read_number(event->pevent, data + offset, len);
  3101. offset = val;
  3102. len = offset >> 16;
  3103. offset &= 0xffff;
  3104. }
  3105. if (field->flags & FIELD_IS_STRING &&
  3106. is_printable_array(data + offset, len)) {
  3107. trace_seq_printf(s, "%s", (char *)data + offset);
  3108. } else {
  3109. trace_seq_puts(s, "ARRAY[");
  3110. for (i = 0; i < len; i++) {
  3111. if (i)
  3112. trace_seq_puts(s, ", ");
  3113. trace_seq_printf(s, "%02x",
  3114. *((unsigned char *)data + offset + i));
  3115. }
  3116. trace_seq_putc(s, ']');
  3117. field->flags &= ~FIELD_IS_STRING;
  3118. }
  3119. } else {
  3120. val = pevent_read_number(event->pevent, data + field->offset,
  3121. field->size);
  3122. if (field->flags & FIELD_IS_POINTER) {
  3123. trace_seq_printf(s, "0x%llx", val);
  3124. } else if (field->flags & FIELD_IS_SIGNED) {
  3125. switch (field->size) {
  3126. case 4:
  3127. /*
  3128. * If field is long then print it in hex.
  3129. * A long usually stores pointers.
  3130. */
  3131. if (field->flags & FIELD_IS_LONG)
  3132. trace_seq_printf(s, "0x%x", (int)val);
  3133. else
  3134. trace_seq_printf(s, "%d", (int)val);
  3135. break;
  3136. case 2:
  3137. trace_seq_printf(s, "%2d", (short)val);
  3138. break;
  3139. case 1:
  3140. trace_seq_printf(s, "%1d", (char)val);
  3141. break;
  3142. default:
  3143. trace_seq_printf(s, "%lld", val);
  3144. }
  3145. } else {
  3146. if (field->flags & FIELD_IS_LONG)
  3147. trace_seq_printf(s, "0x%llx", val);
  3148. else
  3149. trace_seq_printf(s, "%llu", val);
  3150. }
  3151. }
  3152. field = field->next;
  3153. }
  3154. }
  3155. static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
  3156. {
  3157. struct pevent *pevent = event->pevent;
  3158. struct print_fmt *print_fmt = &event->print_fmt;
  3159. struct print_arg *arg = print_fmt->args;
  3160. struct print_arg *args = NULL;
  3161. const char *ptr = print_fmt->format;
  3162. unsigned long long val;
  3163. struct func_map *func;
  3164. const char *saveptr;
  3165. char *bprint_fmt = NULL;
  3166. char format[32];
  3167. int show_func;
  3168. int len_as_arg;
  3169. int len_arg;
  3170. int len;
  3171. int ls;
  3172. if (event->flags & EVENT_FL_FAILED) {
  3173. trace_seq_printf(s, "[FAILED TO PARSE]");
  3174. print_event_fields(s, data, size, event);
  3175. return;
  3176. }
  3177. if (event->flags & EVENT_FL_ISBPRINT) {
  3178. bprint_fmt = get_bprint_format(data, size, event);
  3179. args = make_bprint_args(bprint_fmt, data, size, event);
  3180. arg = args;
  3181. ptr = bprint_fmt;
  3182. }
  3183. for (; *ptr; ptr++) {
  3184. ls = 0;
  3185. if (*ptr == '\\') {
  3186. ptr++;
  3187. switch (*ptr) {
  3188. case 'n':
  3189. trace_seq_putc(s, '\n');
  3190. break;
  3191. case 't':
  3192. trace_seq_putc(s, '\t');
  3193. break;
  3194. case 'r':
  3195. trace_seq_putc(s, '\r');
  3196. break;
  3197. case '\\':
  3198. trace_seq_putc(s, '\\');
  3199. break;
  3200. default:
  3201. trace_seq_putc(s, *ptr);
  3202. break;
  3203. }
  3204. } else if (*ptr == '%') {
  3205. saveptr = ptr;
  3206. show_func = 0;
  3207. len_as_arg = 0;
  3208. cont_process:
  3209. ptr++;
  3210. switch (*ptr) {
  3211. case '%':
  3212. trace_seq_putc(s, '%');
  3213. break;
  3214. case '#':
  3215. /* FIXME: need to handle properly */
  3216. goto cont_process;
  3217. case 'h':
  3218. ls--;
  3219. goto cont_process;
  3220. case 'l':
  3221. ls++;
  3222. goto cont_process;
  3223. case 'L':
  3224. ls = 2;
  3225. goto cont_process;
  3226. case '*':
  3227. /* The argument is the length. */
  3228. if (!arg)
  3229. die("no argument match");
  3230. len_arg = eval_num_arg(data, size, event, arg);
  3231. len_as_arg = 1;
  3232. arg = arg->next;
  3233. goto cont_process;
  3234. case '.':
  3235. case 'z':
  3236. case 'Z':
  3237. case '0' ... '9':
  3238. goto cont_process;
  3239. case 'p':
  3240. if (pevent->long_size == 4)
  3241. ls = 1;
  3242. else
  3243. ls = 2;
  3244. if (*(ptr+1) == 'F' ||
  3245. *(ptr+1) == 'f') {
  3246. ptr++;
  3247. show_func = *ptr;
  3248. } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
  3249. print_mac_arg(s, *(ptr+1), data, size, event, arg);
  3250. ptr++;
  3251. break;
  3252. }
  3253. /* fall through */
  3254. case 'd':
  3255. case 'i':
  3256. case 'x':
  3257. case 'X':
  3258. case 'u':
  3259. if (!arg)
  3260. die("no argument match");
  3261. len = ((unsigned long)ptr + 1) -
  3262. (unsigned long)saveptr;
  3263. /* should never happen */
  3264. if (len > 31)
  3265. die("bad format!");
  3266. memcpy(format, saveptr, len);
  3267. format[len] = 0;
  3268. val = eval_num_arg(data, size, event, arg);
  3269. arg = arg->next;
  3270. if (show_func) {
  3271. func = find_func(pevent, val);
  3272. if (func) {
  3273. trace_seq_puts(s, func->func);
  3274. if (show_func == 'F')
  3275. trace_seq_printf(s,
  3276. "+0x%llx",
  3277. val - func->addr);
  3278. break;
  3279. }
  3280. }
  3281. if (pevent->long_size == 8 && ls) {
  3282. char *p;
  3283. ls = 2;
  3284. /* make %l into %ll */
  3285. p = strchr(format, 'l');
  3286. if (p)
  3287. memmove(p, p+1, strlen(p)+1);
  3288. else if (strcmp(format, "%p") == 0)
  3289. strcpy(format, "0x%llx");
  3290. }
  3291. switch (ls) {
  3292. case -2:
  3293. if (len_as_arg)
  3294. trace_seq_printf(s, format, len_arg, (char)val);
  3295. else
  3296. trace_seq_printf(s, format, (char)val);
  3297. break;
  3298. case -1:
  3299. if (len_as_arg)
  3300. trace_seq_printf(s, format, len_arg, (short)val);
  3301. else
  3302. trace_seq_printf(s, format, (short)val);
  3303. break;
  3304. case 0:
  3305. if (len_as_arg)
  3306. trace_seq_printf(s, format, len_arg, (int)val);
  3307. else
  3308. trace_seq_printf(s, format, (int)val);
  3309. break;
  3310. case 1:
  3311. if (len_as_arg)
  3312. trace_seq_printf(s, format, len_arg, (long)val);
  3313. else
  3314. trace_seq_printf(s, format, (long)val);
  3315. break;
  3316. case 2:
  3317. if (len_as_arg)
  3318. trace_seq_printf(s, format, len_arg,
  3319. (long long)val);
  3320. else
  3321. trace_seq_printf(s, format, (long long)val);
  3322. break;
  3323. default:
  3324. die("bad count (%d)", ls);
  3325. }
  3326. break;
  3327. case 's':
  3328. if (!arg)
  3329. die("no matching argument");
  3330. len = ((unsigned long)ptr + 1) -
  3331. (unsigned long)saveptr;
  3332. /* should never happen */
  3333. if (len > 31)
  3334. die("bad format!");
  3335. memcpy(format, saveptr, len);
  3336. format[len] = 0;
  3337. if (!len_as_arg)
  3338. len_arg = -1;
  3339. print_str_arg(s, data, size, event,
  3340. format, len_arg, arg);
  3341. arg = arg->next;
  3342. break;
  3343. default:
  3344. trace_seq_printf(s, ">%c<", *ptr);
  3345. }
  3346. } else
  3347. trace_seq_putc(s, *ptr);
  3348. }
  3349. if (args) {
  3350. free_args(args);
  3351. free(bprint_fmt);
  3352. }
  3353. }
  3354. /**
  3355. * pevent_data_lat_fmt - parse the data for the latency format
  3356. * @pevent: a handle to the pevent
  3357. * @s: the trace_seq to write to
  3358. * @data: the raw data to read from
  3359. * @size: currently unused.
  3360. *
  3361. * This parses out the Latency format (interrupts disabled,
  3362. * need rescheduling, in hard/soft interrupt, preempt count
  3363. * and lock depth) and places it into the trace_seq.
  3364. */
  3365. void pevent_data_lat_fmt(struct pevent *pevent,
  3366. struct trace_seq *s, struct pevent_record *record)
  3367. {
  3368. static int check_lock_depth = 1;
  3369. static int lock_depth_exists;
  3370. unsigned int lat_flags;
  3371. unsigned int pc;
  3372. int lock_depth;
  3373. int hardirq;
  3374. int softirq;
  3375. void *data = record->data;
  3376. lat_flags = parse_common_flags(pevent, data);
  3377. pc = parse_common_pc(pevent, data);
  3378. /* lock_depth may not always exist */
  3379. if (check_lock_depth) {
  3380. struct format_field *field;
  3381. struct event_format *event;
  3382. check_lock_depth = 0;
  3383. event = pevent->events[0];
  3384. field = pevent_find_common_field(event, "common_lock_depth");
  3385. if (field)
  3386. lock_depth_exists = 1;
  3387. }
  3388. if (lock_depth_exists)
  3389. lock_depth = parse_common_lock_depth(pevent, data);
  3390. hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
  3391. softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
  3392. trace_seq_printf(s, "%c%c%c",
  3393. (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  3394. (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  3395. 'X' : '.',
  3396. (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
  3397. 'N' : '.',
  3398. (hardirq && softirq) ? 'H' :
  3399. hardirq ? 'h' : softirq ? 's' : '.');
  3400. if (pc)
  3401. trace_seq_printf(s, "%x", pc);
  3402. else
  3403. trace_seq_putc(s, '.');
  3404. if (lock_depth_exists) {
  3405. if (lock_depth < 0)
  3406. trace_seq_putc(s, '.');
  3407. else
  3408. trace_seq_printf(s, "%d", lock_depth);
  3409. }
  3410. trace_seq_terminate(s);
  3411. }
  3412. /**
  3413. * pevent_data_type - parse out the given event type
  3414. * @pevent: a handle to the pevent
  3415. * @rec: the record to read from
  3416. *
  3417. * This returns the event id from the @rec.
  3418. */
  3419. int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
  3420. {
  3421. return trace_parse_common_type(pevent, rec->data);
  3422. }
  3423. /**
  3424. * pevent_data_event_from_type - find the event by a given type
  3425. * @pevent: a handle to the pevent
  3426. * @type: the type of the event.
  3427. *
  3428. * This returns the event form a given @type;
  3429. */
  3430. struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
  3431. {
  3432. return pevent_find_event(pevent, type);
  3433. }
  3434. /**
  3435. * pevent_data_pid - parse the PID from raw data
  3436. * @pevent: a handle to the pevent
  3437. * @rec: the record to parse
  3438. *
  3439. * This returns the PID from a raw data.
  3440. */
  3441. int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
  3442. {
  3443. return parse_common_pid(pevent, rec->data);
  3444. }
  3445. /**
  3446. * pevent_data_comm_from_pid - return the command line from PID
  3447. * @pevent: a handle to the pevent
  3448. * @pid: the PID of the task to search for
  3449. *
  3450. * This returns a pointer to the command line that has the given
  3451. * @pid.
  3452. */
  3453. const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
  3454. {
  3455. const char *comm;
  3456. comm = find_cmdline(pevent, pid);
  3457. return comm;
  3458. }
  3459. /**
  3460. * pevent_data_comm_from_pid - parse the data into the print format
  3461. * @s: the trace_seq to write to
  3462. * @event: the handle to the event
  3463. * @cpu: the cpu the event was recorded on
  3464. * @data: the raw data
  3465. * @size: the size of the raw data
  3466. * @nsecs: the timestamp of the event
  3467. *
  3468. * This parses the raw @data using the given @event information and
  3469. * writes the print format into the trace_seq.
  3470. */
  3471. void pevent_event_info(struct trace_seq *s, struct event_format *event,
  3472. struct pevent_record *record)
  3473. {
  3474. int print_pretty = 1;
  3475. if (event->pevent->print_raw)
  3476. print_event_fields(s, record->data, record->size, event);
  3477. else {
  3478. if (event->handler)
  3479. print_pretty = event->handler(s, record, event,
  3480. event->context);
  3481. if (print_pretty)
  3482. pretty_print(s, record->data, record->size, event);
  3483. }
  3484. trace_seq_terminate(s);
  3485. }
  3486. void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
  3487. struct pevent_record *record)
  3488. {
  3489. static char *spaces = " "; /* 20 spaces */
  3490. struct event_format *event;
  3491. unsigned long secs;
  3492. unsigned long usecs;
  3493. unsigned long nsecs;
  3494. const char *comm;
  3495. void *data = record->data;
  3496. int type;
  3497. int pid;
  3498. int len;
  3499. int p;
  3500. secs = record->ts / NSECS_PER_SEC;
  3501. nsecs = record->ts - secs * NSECS_PER_SEC;
  3502. if (record->size < 0) {
  3503. do_warning("ug! negative record size %d", record->size);
  3504. return;
  3505. }
  3506. type = trace_parse_common_type(pevent, data);
  3507. event = pevent_find_event(pevent, type);
  3508. if (!event) {
  3509. do_warning("ug! no event found for type %d", type);
  3510. return;
  3511. }
  3512. pid = parse_common_pid(pevent, data);
  3513. comm = find_cmdline(pevent, pid);
  3514. if (pevent->latency_format) {
  3515. trace_seq_printf(s, "%8.8s-%-5d %3d",
  3516. comm, pid, record->cpu);
  3517. pevent_data_lat_fmt(pevent, s, record);
  3518. } else
  3519. trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
  3520. if (pevent->flags & PEVENT_NSEC_OUTPUT) {
  3521. usecs = nsecs;
  3522. p = 9;
  3523. } else {
  3524. usecs = (nsecs + 500) / NSECS_PER_USEC;
  3525. p = 6;
  3526. }
  3527. trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
  3528. /* Space out the event names evenly. */
  3529. len = strlen(event->name);
  3530. if (len < 20)
  3531. trace_seq_printf(s, "%.*s", 20 - len, spaces);
  3532. pevent_event_info(s, event, record);
  3533. }
  3534. static int events_id_cmp(const void *a, const void *b)
  3535. {
  3536. struct event_format * const * ea = a;
  3537. struct event_format * const * eb = b;
  3538. if ((*ea)->id < (*eb)->id)
  3539. return -1;
  3540. if ((*ea)->id > (*eb)->id)
  3541. return 1;
  3542. return 0;
  3543. }
  3544. static int events_name_cmp(const void *a, const void *b)
  3545. {
  3546. struct event_format * const * ea = a;
  3547. struct event_format * const * eb = b;
  3548. int res;
  3549. res = strcmp((*ea)->name, (*eb)->name);
  3550. if (res)
  3551. return res;
  3552. res = strcmp((*ea)->system, (*eb)->system);
  3553. if (res)
  3554. return res;
  3555. return events_id_cmp(a, b);
  3556. }
  3557. static int events_system_cmp(const void *a, const void *b)
  3558. {
  3559. struct event_format * const * ea = a;
  3560. struct event_format * const * eb = b;
  3561. int res;
  3562. res = strcmp((*ea)->system, (*eb)->system);
  3563. if (res)
  3564. return res;
  3565. res = strcmp((*ea)->name, (*eb)->name);
  3566. if (res)
  3567. return res;
  3568. return events_id_cmp(a, b);
  3569. }
  3570. struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
  3571. {
  3572. struct event_format **events;
  3573. int (*sort)(const void *a, const void *b);
  3574. events = pevent->sort_events;
  3575. if (events && pevent->last_type == sort_type)
  3576. return events;
  3577. if (!events) {
  3578. events = malloc(sizeof(*events) * (pevent->nr_events + 1));
  3579. if (!events)
  3580. return NULL;
  3581. memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
  3582. events[pevent->nr_events] = NULL;
  3583. pevent->sort_events = events;
  3584. /* the internal events are sorted by id */
  3585. if (sort_type == EVENT_SORT_ID) {
  3586. pevent->last_type = sort_type;
  3587. return events;
  3588. }
  3589. }
  3590. switch (sort_type) {
  3591. case EVENT_SORT_ID:
  3592. sort = events_id_cmp;
  3593. break;
  3594. case EVENT_SORT_NAME:
  3595. sort = events_name_cmp;
  3596. break;
  3597. case EVENT_SORT_SYSTEM:
  3598. sort = events_system_cmp;
  3599. break;
  3600. default:
  3601. return events;
  3602. }
  3603. qsort(events, pevent->nr_events, sizeof(*events), sort);
  3604. pevent->last_type = sort_type;
  3605. return events;
  3606. }
  3607. static struct format_field **
  3608. get_event_fields(const char *type, const char *name,
  3609. int count, struct format_field *list)
  3610. {
  3611. struct format_field **fields;
  3612. struct format_field *field;
  3613. int i = 0;
  3614. fields = malloc_or_die(sizeof(*fields) * (count + 1));
  3615. for (field = list; field; field = field->next) {
  3616. fields[i++] = field;
  3617. if (i == count + 1) {
  3618. do_warning("event %s has more %s fields than specified",
  3619. name, type);
  3620. i--;
  3621. break;
  3622. }
  3623. }
  3624. if (i != count)
  3625. do_warning("event %s has less %s fields than specified",
  3626. name, type);
  3627. fields[i] = NULL;
  3628. return fields;
  3629. }
  3630. /**
  3631. * pevent_event_common_fields - return a list of common fields for an event
  3632. * @event: the event to return the common fields of.
  3633. *
  3634. * Returns an allocated array of fields. The last item in the array is NULL.
  3635. * The array must be freed with free().
  3636. */
  3637. struct format_field **pevent_event_common_fields(struct event_format *event)
  3638. {
  3639. return get_event_fields("common", event->name,
  3640. event->format.nr_common,
  3641. event->format.common_fields);
  3642. }
  3643. /**
  3644. * pevent_event_fields - return a list of event specific fields for an event
  3645. * @event: the event to return the fields of.
  3646. *
  3647. * Returns an allocated array of fields. The last item in the array is NULL.
  3648. * The array must be freed with free().
  3649. */
  3650. struct format_field **pevent_event_fields(struct event_format *event)
  3651. {
  3652. return get_event_fields("event", event->name,
  3653. event->format.nr_fields,
  3654. event->format.fields);
  3655. }
  3656. static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
  3657. {
  3658. trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
  3659. if (field->next) {
  3660. trace_seq_puts(s, ", ");
  3661. print_fields(s, field->next);
  3662. }
  3663. }
  3664. /* for debugging */
  3665. static void print_args(struct print_arg *args)
  3666. {
  3667. int print_paren = 1;
  3668. struct trace_seq s;
  3669. switch (args->type) {
  3670. case PRINT_NULL:
  3671. printf("null");
  3672. break;
  3673. case PRINT_ATOM:
  3674. printf("%s", args->atom.atom);
  3675. break;
  3676. case PRINT_FIELD:
  3677. printf("REC->%s", args->field.name);
  3678. break;
  3679. case PRINT_FLAGS:
  3680. printf("__print_flags(");
  3681. print_args(args->flags.field);
  3682. printf(", %s, ", args->flags.delim);
  3683. trace_seq_init(&s);
  3684. print_fields(&s, args->flags.flags);
  3685. trace_seq_do_printf(&s);
  3686. trace_seq_destroy(&s);
  3687. printf(")");
  3688. break;
  3689. case PRINT_SYMBOL:
  3690. printf("__print_symbolic(");
  3691. print_args(args->symbol.field);
  3692. printf(", ");
  3693. trace_seq_init(&s);
  3694. print_fields(&s, args->symbol.symbols);
  3695. trace_seq_do_printf(&s);
  3696. trace_seq_destroy(&s);
  3697. printf(")");
  3698. break;
  3699. case PRINT_HEX:
  3700. printf("__print_hex(");
  3701. print_args(args->hex.field);
  3702. printf(", ");
  3703. print_args(args->hex.size);
  3704. printf(")");
  3705. break;
  3706. case PRINT_STRING:
  3707. case PRINT_BSTRING:
  3708. printf("__get_str(%s)", args->string.string);
  3709. break;
  3710. case PRINT_TYPE:
  3711. printf("(%s)", args->typecast.type);
  3712. print_args(args->typecast.item);
  3713. break;
  3714. case PRINT_OP:
  3715. if (strcmp(args->op.op, ":") == 0)
  3716. print_paren = 0;
  3717. if (print_paren)
  3718. printf("(");
  3719. print_args(args->op.left);
  3720. printf(" %s ", args->op.op);
  3721. print_args(args->op.right);
  3722. if (print_paren)
  3723. printf(")");
  3724. break;
  3725. default:
  3726. /* we should warn... */
  3727. return;
  3728. }
  3729. if (args->next) {
  3730. printf("\n");
  3731. print_args(args->next);
  3732. }
  3733. }
  3734. static void parse_header_field(const char *field,
  3735. int *offset, int *size, int mandatory)
  3736. {
  3737. unsigned long long save_input_buf_ptr;
  3738. unsigned long long save_input_buf_siz;
  3739. char *token;
  3740. int type;
  3741. save_input_buf_ptr = input_buf_ptr;
  3742. save_input_buf_siz = input_buf_siz;
  3743. if (read_expected(EVENT_ITEM, "field") < 0)
  3744. return;
  3745. if (read_expected(EVENT_OP, ":") < 0)
  3746. return;
  3747. /* type */
  3748. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3749. goto fail;
  3750. free_token(token);
  3751. /*
  3752. * If this is not a mandatory field, then test it first.
  3753. */
  3754. if (mandatory) {
  3755. if (read_expected(EVENT_ITEM, field) < 0)
  3756. return;
  3757. } else {
  3758. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3759. goto fail;
  3760. if (strcmp(token, field) != 0)
  3761. goto discard;
  3762. free_token(token);
  3763. }
  3764. if (read_expected(EVENT_OP, ";") < 0)
  3765. return;
  3766. if (read_expected(EVENT_ITEM, "offset") < 0)
  3767. return;
  3768. if (read_expected(EVENT_OP, ":") < 0)
  3769. return;
  3770. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3771. goto fail;
  3772. *offset = atoi(token);
  3773. free_token(token);
  3774. if (read_expected(EVENT_OP, ";") < 0)
  3775. return;
  3776. if (read_expected(EVENT_ITEM, "size") < 0)
  3777. return;
  3778. if (read_expected(EVENT_OP, ":") < 0)
  3779. return;
  3780. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3781. goto fail;
  3782. *size = atoi(token);
  3783. free_token(token);
  3784. if (read_expected(EVENT_OP, ";") < 0)
  3785. return;
  3786. type = read_token(&token);
  3787. if (type != EVENT_NEWLINE) {
  3788. /* newer versions of the kernel have a "signed" type */
  3789. if (type != EVENT_ITEM)
  3790. goto fail;
  3791. if (strcmp(token, "signed") != 0)
  3792. goto fail;
  3793. free_token(token);
  3794. if (read_expected(EVENT_OP, ":") < 0)
  3795. return;
  3796. if (read_expect_type(EVENT_ITEM, &token))
  3797. goto fail;
  3798. free_token(token);
  3799. if (read_expected(EVENT_OP, ";") < 0)
  3800. return;
  3801. if (read_expect_type(EVENT_NEWLINE, &token))
  3802. goto fail;
  3803. }
  3804. fail:
  3805. free_token(token);
  3806. return;
  3807. discard:
  3808. input_buf_ptr = save_input_buf_ptr;
  3809. input_buf_siz = save_input_buf_siz;
  3810. *offset = 0;
  3811. *size = 0;
  3812. free_token(token);
  3813. }
  3814. /**
  3815. * pevent_parse_header_page - parse the data stored in the header page
  3816. * @pevent: the handle to the pevent
  3817. * @buf: the buffer storing the header page format string
  3818. * @size: the size of @buf
  3819. * @long_size: the long size to use if there is no header
  3820. *
  3821. * This parses the header page format for information on the
  3822. * ring buffer used. The @buf should be copied from
  3823. *
  3824. * /sys/kernel/debug/tracing/events/header_page
  3825. */
  3826. int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
  3827. int long_size)
  3828. {
  3829. int ignore;
  3830. if (!size) {
  3831. /*
  3832. * Old kernels did not have header page info.
  3833. * Sorry but we just use what we find here in user space.
  3834. */
  3835. pevent->header_page_ts_size = sizeof(long long);
  3836. pevent->header_page_size_size = long_size;
  3837. pevent->header_page_data_offset = sizeof(long long) + long_size;
  3838. pevent->old_format = 1;
  3839. return -1;
  3840. }
  3841. init_input_buf(buf, size);
  3842. parse_header_field("timestamp", &pevent->header_page_ts_offset,
  3843. &pevent->header_page_ts_size, 1);
  3844. parse_header_field("commit", &pevent->header_page_size_offset,
  3845. &pevent->header_page_size_size, 1);
  3846. parse_header_field("overwrite", &pevent->header_page_overwrite,
  3847. &ignore, 0);
  3848. parse_header_field("data", &pevent->header_page_data_offset,
  3849. &pevent->header_page_data_size, 1);
  3850. return 0;
  3851. }
  3852. static int event_matches(struct event_format *event,
  3853. int id, const char *sys_name,
  3854. const char *event_name)
  3855. {
  3856. if (id >= 0 && id != event->id)
  3857. return 0;
  3858. if (event_name && (strcmp(event_name, event->name) != 0))
  3859. return 0;
  3860. if (sys_name && (strcmp(sys_name, event->system) != 0))
  3861. return 0;
  3862. return 1;
  3863. }
  3864. static void free_handler(struct event_handler *handle)
  3865. {
  3866. free((void *)handle->sys_name);
  3867. free((void *)handle->event_name);
  3868. free(handle);
  3869. }
  3870. static int find_event_handle(struct pevent *pevent, struct event_format *event)
  3871. {
  3872. struct event_handler *handle, **next;
  3873. for (next = &pevent->handlers; *next;
  3874. next = &(*next)->next) {
  3875. handle = *next;
  3876. if (event_matches(event, handle->id,
  3877. handle->sys_name,
  3878. handle->event_name))
  3879. break;
  3880. }
  3881. if (!(*next))
  3882. return 0;
  3883. pr_stat("overriding event (%d) %s:%s with new print handler",
  3884. event->id, event->system, event->name);
  3885. event->handler = handle->func;
  3886. event->context = handle->context;
  3887. *next = handle->next;
  3888. free_handler(handle);
  3889. return 1;
  3890. }
  3891. /**
  3892. * pevent_parse_event - parse the event format
  3893. * @pevent: the handle to the pevent
  3894. * @buf: the buffer storing the event format string
  3895. * @size: the size of @buf
  3896. * @sys: the system the event belongs to
  3897. *
  3898. * This parses the event format and creates an event structure
  3899. * to quickly parse raw data for a given event.
  3900. *
  3901. * These files currently come from:
  3902. *
  3903. * /sys/kernel/debug/tracing/events/.../.../format
  3904. */
  3905. int pevent_parse_event(struct pevent *pevent,
  3906. const char *buf, unsigned long size,
  3907. const char *sys)
  3908. {
  3909. struct event_format *event;
  3910. int ret;
  3911. init_input_buf(buf, size);
  3912. event = alloc_event();
  3913. if (!event)
  3914. return -ENOMEM;
  3915. event->name = event_read_name();
  3916. if (!event->name) {
  3917. /* Bad event? */
  3918. free(event);
  3919. return -1;
  3920. }
  3921. if (strcmp(sys, "ftrace") == 0) {
  3922. event->flags |= EVENT_FL_ISFTRACE;
  3923. if (strcmp(event->name, "bprint") == 0)
  3924. event->flags |= EVENT_FL_ISBPRINT;
  3925. }
  3926. event->id = event_read_id();
  3927. if (event->id < 0)
  3928. die("failed to read event id");
  3929. event->system = strdup(sys);
  3930. /* Add pevent to event so that it can be referenced */
  3931. event->pevent = pevent;
  3932. ret = event_read_format(event);
  3933. if (ret < 0) {
  3934. do_warning("failed to read event format for %s", event->name);
  3935. goto event_failed;
  3936. }
  3937. /*
  3938. * If the event has an override, don't print warnings if the event
  3939. * print format fails to parse.
  3940. */
  3941. if (find_event_handle(pevent, event))
  3942. show_warning = 0;
  3943. ret = event_read_print(event);
  3944. if (ret < 0) {
  3945. do_warning("failed to read event print fmt for %s",
  3946. event->name);
  3947. show_warning = 1;
  3948. goto event_failed;
  3949. }
  3950. show_warning = 1;
  3951. add_event(pevent, event);
  3952. if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
  3953. struct format_field *field;
  3954. struct print_arg *arg, **list;
  3955. /* old ftrace had no args */
  3956. list = &event->print_fmt.args;
  3957. for (field = event->format.fields; field; field = field->next) {
  3958. arg = alloc_arg();
  3959. *list = arg;
  3960. list = &arg->next;
  3961. arg->type = PRINT_FIELD;
  3962. arg->field.name = strdup(field->name);
  3963. arg->field.field = field;
  3964. }
  3965. return 0;
  3966. }
  3967. #define PRINT_ARGS 0
  3968. if (PRINT_ARGS && event->print_fmt.args)
  3969. print_args(event->print_fmt.args);
  3970. return 0;
  3971. event_failed:
  3972. event->flags |= EVENT_FL_FAILED;
  3973. /* still add it even if it failed */
  3974. add_event(pevent, event);
  3975. return -1;
  3976. }
  3977. int get_field_val(struct trace_seq *s, struct format_field *field,
  3978. const char *name, struct pevent_record *record,
  3979. unsigned long long *val, int err)
  3980. {
  3981. if (!field) {
  3982. if (err)
  3983. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  3984. return -1;
  3985. }
  3986. if (pevent_read_number_field(field, record->data, val)) {
  3987. if (err)
  3988. trace_seq_printf(s, " %s=INVALID", name);
  3989. return -1;
  3990. }
  3991. return 0;
  3992. }
  3993. /**
  3994. * pevent_get_field_raw - return the raw pointer into the data field
  3995. * @s: The seq to print to on error
  3996. * @event: the event that the field is for
  3997. * @name: The name of the field
  3998. * @record: The record with the field name.
  3999. * @len: place to store the field length.
  4000. * @err: print default error if failed.
  4001. *
  4002. * Returns a pointer into record->data of the field and places
  4003. * the length of the field in @len.
  4004. *
  4005. * On failure, it returns NULL.
  4006. */
  4007. void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
  4008. const char *name, struct pevent_record *record,
  4009. int *len, int err)
  4010. {
  4011. struct format_field *field;
  4012. void *data = record->data;
  4013. unsigned offset;
  4014. int dummy;
  4015. if (!event)
  4016. return NULL;
  4017. field = pevent_find_field(event, name);
  4018. if (!field) {
  4019. if (err)
  4020. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  4021. return NULL;
  4022. }
  4023. /* Allow @len to be NULL */
  4024. if (!len)
  4025. len = &dummy;
  4026. offset = field->offset;
  4027. if (field->flags & FIELD_IS_DYNAMIC) {
  4028. offset = pevent_read_number(event->pevent,
  4029. data + offset, field->size);
  4030. *len = offset >> 16;
  4031. offset &= 0xffff;
  4032. } else
  4033. *len = field->size;
  4034. return data + offset;
  4035. }
  4036. /**
  4037. * pevent_get_field_val - find a field and return its value
  4038. * @s: The seq to print to on error
  4039. * @event: the event that the field is for
  4040. * @name: The name of the field
  4041. * @record: The record with the field name.
  4042. * @val: place to store the value of the field.
  4043. * @err: print default error if failed.
  4044. *
  4045. * Returns 0 on success -1 on field not found.
  4046. */
  4047. int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
  4048. const char *name, struct pevent_record *record,
  4049. unsigned long long *val, int err)
  4050. {
  4051. struct format_field *field;
  4052. if (!event)
  4053. return -1;
  4054. field = pevent_find_field(event, name);
  4055. return get_field_val(s, field, name, record, val, err);
  4056. }
  4057. /**
  4058. * pevent_get_common_field_val - find a common field and return its value
  4059. * @s: The seq to print to on error
  4060. * @event: the event that the field is for
  4061. * @name: The name of the field
  4062. * @record: The record with the field name.
  4063. * @val: place to store the value of the field.
  4064. * @err: print default error if failed.
  4065. *
  4066. * Returns 0 on success -1 on field not found.
  4067. */
  4068. int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
  4069. const char *name, struct pevent_record *record,
  4070. unsigned long long *val, int err)
  4071. {
  4072. struct format_field *field;
  4073. if (!event)
  4074. return -1;
  4075. field = pevent_find_common_field(event, name);
  4076. return get_field_val(s, field, name, record, val, err);
  4077. }
  4078. /**
  4079. * pevent_get_any_field_val - find a any field and return its value
  4080. * @s: The seq to print to on error
  4081. * @event: the event that the field is for
  4082. * @name: The name of the field
  4083. * @record: The record with the field name.
  4084. * @val: place to store the value of the field.
  4085. * @err: print default error if failed.
  4086. *
  4087. * Returns 0 on success -1 on field not found.
  4088. */
  4089. int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
  4090. const char *name, struct pevent_record *record,
  4091. unsigned long long *val, int err)
  4092. {
  4093. struct format_field *field;
  4094. if (!event)
  4095. return -1;
  4096. field = pevent_find_any_field(event, name);
  4097. return get_field_val(s, field, name, record, val, err);
  4098. }
  4099. /**
  4100. * pevent_print_num_field - print a field and a format
  4101. * @s: The seq to print to
  4102. * @fmt: The printf format to print the field with.
  4103. * @event: the event that the field is for
  4104. * @name: The name of the field
  4105. * @record: The record with the field name.
  4106. * @err: print default error if failed.
  4107. *
  4108. * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
  4109. */
  4110. int pevent_print_num_field(struct trace_seq *s, const char *fmt,
  4111. struct event_format *event, const char *name,
  4112. struct pevent_record *record, int err)
  4113. {
  4114. struct format_field *field = pevent_find_field(event, name);
  4115. unsigned long long val;
  4116. if (!field)
  4117. goto failed;
  4118. if (pevent_read_number_field(field, record->data, &val))
  4119. goto failed;
  4120. return trace_seq_printf(s, fmt, val);
  4121. failed:
  4122. if (err)
  4123. trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
  4124. return -1;
  4125. }
  4126. static void free_func_handle(struct pevent_function_handler *func)
  4127. {
  4128. struct pevent_func_params *params;
  4129. free(func->name);
  4130. while (func->params) {
  4131. params = func->params;
  4132. func->params = params->next;
  4133. free(params);
  4134. }
  4135. free(func);
  4136. }
  4137. /**
  4138. * pevent_register_print_function - register a helper function
  4139. * @pevent: the handle to the pevent
  4140. * @func: the function to process the helper function
  4141. * @name: the name of the helper function
  4142. * @parameters: A list of enum pevent_func_arg_type
  4143. *
  4144. * Some events may have helper functions in the print format arguments.
  4145. * This allows a plugin to dynmically create a way to process one
  4146. * of these functions.
  4147. *
  4148. * The @parameters is a variable list of pevent_func_arg_type enums that
  4149. * must end with PEVENT_FUNC_ARG_VOID.
  4150. */
  4151. int pevent_register_print_function(struct pevent *pevent,
  4152. pevent_func_handler func,
  4153. enum pevent_func_arg_type ret_type,
  4154. char *name, ...)
  4155. {
  4156. struct pevent_function_handler *func_handle;
  4157. struct pevent_func_params **next_param;
  4158. struct pevent_func_params *param;
  4159. enum pevent_func_arg_type type;
  4160. va_list ap;
  4161. func_handle = find_func_handler(pevent, name);
  4162. if (func_handle) {
  4163. /*
  4164. * This is most like caused by the users own
  4165. * plugins updating the function. This overrides the
  4166. * system defaults.
  4167. */
  4168. pr_stat("override of function helper '%s'", name);
  4169. remove_func_handler(pevent, name);
  4170. }
  4171. func_handle = malloc_or_die(sizeof(*func_handle));
  4172. memset(func_handle, 0, sizeof(*func_handle));
  4173. func_handle->ret_type = ret_type;
  4174. func_handle->name = strdup(name);
  4175. func_handle->func = func;
  4176. if (!func_handle->name)
  4177. die("Failed to allocate function name");
  4178. next_param = &(func_handle->params);
  4179. va_start(ap, name);
  4180. for (;;) {
  4181. type = va_arg(ap, enum pevent_func_arg_type);
  4182. if (type == PEVENT_FUNC_ARG_VOID)
  4183. break;
  4184. if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
  4185. warning("Invalid argument type %d", type);
  4186. goto out_free;
  4187. }
  4188. param = malloc_or_die(sizeof(*param));
  4189. param->type = type;
  4190. param->next = NULL;
  4191. *next_param = param;
  4192. next_param = &(param->next);
  4193. func_handle->nr_args++;
  4194. }
  4195. va_end(ap);
  4196. func_handle->next = pevent->func_handlers;
  4197. pevent->func_handlers = func_handle;
  4198. return 0;
  4199. out_free:
  4200. va_end(ap);
  4201. free_func_handle(func_handle);
  4202. return -1;
  4203. }
  4204. /**
  4205. * pevent_register_event_handle - register a way to parse an event
  4206. * @pevent: the handle to the pevent
  4207. * @id: the id of the event to register
  4208. * @sys_name: the system name the event belongs to
  4209. * @event_name: the name of the event
  4210. * @func: the function to call to parse the event information
  4211. *
  4212. * This function allows a developer to override the parsing of
  4213. * a given event. If for some reason the default print format
  4214. * is not sufficient, this function will register a function
  4215. * for an event to be used to parse the data instead.
  4216. *
  4217. * If @id is >= 0, then it is used to find the event.
  4218. * else @sys_name and @event_name are used.
  4219. */
  4220. int pevent_register_event_handler(struct pevent *pevent,
  4221. int id, char *sys_name, char *event_name,
  4222. pevent_event_handler_func func,
  4223. void *context)
  4224. {
  4225. struct event_format *event;
  4226. struct event_handler *handle;
  4227. if (id >= 0) {
  4228. /* search by id */
  4229. event = pevent_find_event(pevent, id);
  4230. if (!event)
  4231. goto not_found;
  4232. if (event_name && (strcmp(event_name, event->name) != 0))
  4233. goto not_found;
  4234. if (sys_name && (strcmp(sys_name, event->system) != 0))
  4235. goto not_found;
  4236. } else {
  4237. event = pevent_find_event_by_name(pevent, sys_name, event_name);
  4238. if (!event)
  4239. goto not_found;
  4240. }
  4241. pr_stat("overriding event (%d) %s:%s with new print handler",
  4242. event->id, event->system, event->name);
  4243. event->handler = func;
  4244. event->context = context;
  4245. return 0;
  4246. not_found:
  4247. /* Save for later use. */
  4248. handle = malloc_or_die(sizeof(*handle));
  4249. memset(handle, 0, sizeof(*handle));
  4250. handle->id = id;
  4251. if (event_name)
  4252. handle->event_name = strdup(event_name);
  4253. if (sys_name)
  4254. handle->sys_name = strdup(sys_name);
  4255. handle->func = func;
  4256. handle->next = pevent->handlers;
  4257. pevent->handlers = handle;
  4258. handle->context = context;
  4259. return -1;
  4260. }
  4261. /**
  4262. * pevent_alloc - create a pevent handle
  4263. */
  4264. struct pevent *pevent_alloc(void)
  4265. {
  4266. struct pevent *pevent;
  4267. pevent = malloc(sizeof(*pevent));
  4268. if (!pevent)
  4269. return NULL;
  4270. memset(pevent, 0, sizeof(*pevent));
  4271. pevent->ref_count = 1;
  4272. return pevent;
  4273. }
  4274. void pevent_ref(struct pevent *pevent)
  4275. {
  4276. pevent->ref_count++;
  4277. }
  4278. static void free_format_fields(struct format_field *field)
  4279. {
  4280. struct format_field *next;
  4281. while (field) {
  4282. next = field->next;
  4283. free(field->type);
  4284. free(field->name);
  4285. free(field);
  4286. field = next;
  4287. }
  4288. }
  4289. static void free_formats(struct format *format)
  4290. {
  4291. free_format_fields(format->common_fields);
  4292. free_format_fields(format->fields);
  4293. }
  4294. static void free_event(struct event_format *event)
  4295. {
  4296. free(event->name);
  4297. free(event->system);
  4298. free_formats(&event->format);
  4299. free(event->print_fmt.format);
  4300. free_args(event->print_fmt.args);
  4301. free(event);
  4302. }
  4303. /**
  4304. * pevent_free - free a pevent handle
  4305. * @pevent: the pevent handle to free
  4306. */
  4307. void pevent_free(struct pevent *pevent)
  4308. {
  4309. struct cmdline_list *cmdlist, *cmdnext;
  4310. struct func_list *funclist, *funcnext;
  4311. struct printk_list *printklist, *printknext;
  4312. struct pevent_function_handler *func_handler;
  4313. struct event_handler *handle;
  4314. int i;
  4315. if (!pevent)
  4316. return;
  4317. cmdlist = pevent->cmdlist;
  4318. funclist = pevent->funclist;
  4319. printklist = pevent->printklist;
  4320. pevent->ref_count--;
  4321. if (pevent->ref_count)
  4322. return;
  4323. if (pevent->cmdlines) {
  4324. for (i = 0; i < pevent->cmdline_count; i++)
  4325. free(pevent->cmdlines[i].comm);
  4326. free(pevent->cmdlines);
  4327. }
  4328. while (cmdlist) {
  4329. cmdnext = cmdlist->next;
  4330. free(cmdlist->comm);
  4331. free(cmdlist);
  4332. cmdlist = cmdnext;
  4333. }
  4334. if (pevent->func_map) {
  4335. for (i = 0; i < pevent->func_count; i++) {
  4336. free(pevent->func_map[i].func);
  4337. free(pevent->func_map[i].mod);
  4338. }
  4339. free(pevent->func_map);
  4340. }
  4341. while (funclist) {
  4342. funcnext = funclist->next;
  4343. free(funclist->func);
  4344. free(funclist->mod);
  4345. free(funclist);
  4346. funclist = funcnext;
  4347. }
  4348. while (pevent->func_handlers) {
  4349. func_handler = pevent->func_handlers;
  4350. pevent->func_handlers = func_handler->next;
  4351. free_func_handle(func_handler);
  4352. }
  4353. if (pevent->printk_map) {
  4354. for (i = 0; i < pevent->printk_count; i++)
  4355. free(pevent->printk_map[i].printk);
  4356. free(pevent->printk_map);
  4357. }
  4358. while (printklist) {
  4359. printknext = printklist->next;
  4360. free(printklist->printk);
  4361. free(printklist);
  4362. printklist = printknext;
  4363. }
  4364. for (i = 0; i < pevent->nr_events; i++)
  4365. free_event(pevent->events[i]);
  4366. while (pevent->handlers) {
  4367. handle = pevent->handlers;
  4368. pevent->handlers = handle->next;
  4369. free_handler(handle);
  4370. }
  4371. free(pevent->events);
  4372. free(pevent->sort_events);
  4373. free(pevent);
  4374. }
  4375. void pevent_unref(struct pevent *pevent)
  4376. {
  4377. pevent_free(pevent);
  4378. }