event-parse.c 111 KB

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