event-parse.c 113 KB

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