event-parse.c 111 KB

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