event-parse.c 118 KB

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