event-parse.c 106 KB

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