event-parse.c 113 KB

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