event-parse.c 120 KB

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