event-parse.c 107 KB

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