event-parse.c 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837
  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 <inttypes.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <ctype.h>
  32. #include <errno.h>
  33. #include <stdint.h>
  34. #include <limits.h>
  35. #include <linux/string.h>
  36. #include <linux/time64.h>
  37. #include <netinet/in.h>
  38. #include "event-parse.h"
  39. #include "event-utils.h"
  40. static const char *input_buf;
  41. static unsigned long long input_buf_ptr;
  42. static unsigned long long input_buf_siz;
  43. static int is_flag_field;
  44. static int is_symbolic_field;
  45. static int show_warning = 1;
  46. #define do_warning(fmt, ...) \
  47. do { \
  48. if (show_warning) \
  49. warning(fmt, ##__VA_ARGS__); \
  50. } while (0)
  51. #define do_warning_event(event, fmt, ...) \
  52. do { \
  53. if (!show_warning) \
  54. continue; \
  55. \
  56. if (event) \
  57. warning("[%s:%s] " fmt, event->system, \
  58. event->name, ##__VA_ARGS__); \
  59. else \
  60. warning(fmt, ##__VA_ARGS__); \
  61. } while (0)
  62. static void init_input_buf(const char *buf, unsigned long long size)
  63. {
  64. input_buf = buf;
  65. input_buf_siz = size;
  66. input_buf_ptr = 0;
  67. }
  68. const char *pevent_get_input_buf(void)
  69. {
  70. return input_buf;
  71. }
  72. unsigned long long pevent_get_input_buf_ptr(void)
  73. {
  74. return input_buf_ptr;
  75. }
  76. struct event_handler {
  77. struct event_handler *next;
  78. int id;
  79. const char *sys_name;
  80. const char *event_name;
  81. pevent_event_handler_func func;
  82. void *context;
  83. };
  84. struct pevent_func_params {
  85. struct pevent_func_params *next;
  86. enum pevent_func_arg_type type;
  87. };
  88. struct pevent_function_handler {
  89. struct pevent_function_handler *next;
  90. enum pevent_func_arg_type ret_type;
  91. char *name;
  92. pevent_func_handler func;
  93. struct pevent_func_params *params;
  94. int nr_args;
  95. };
  96. static unsigned long long
  97. process_defined_func(struct trace_seq *s, void *data, int size,
  98. struct event_format *event, struct print_arg *arg);
  99. static void free_func_handle(struct pevent_function_handler *func);
  100. /**
  101. * pevent_buffer_init - init buffer for parsing
  102. * @buf: buffer to parse
  103. * @size: the size of the buffer
  104. *
  105. * For use with pevent_read_token(), this initializes the internal
  106. * buffer that pevent_read_token() will parse.
  107. */
  108. void pevent_buffer_init(const char *buf, unsigned long long size)
  109. {
  110. init_input_buf(buf, size);
  111. }
  112. void breakpoint(void)
  113. {
  114. static int x;
  115. x++;
  116. }
  117. struct print_arg *alloc_arg(void)
  118. {
  119. return calloc(1, sizeof(struct print_arg));
  120. }
  121. struct cmdline {
  122. char *comm;
  123. int pid;
  124. };
  125. static int cmdline_cmp(const void *a, const void *b)
  126. {
  127. const struct cmdline *ca = a;
  128. const struct cmdline *cb = b;
  129. if (ca->pid < cb->pid)
  130. return -1;
  131. if (ca->pid > cb->pid)
  132. return 1;
  133. return 0;
  134. }
  135. struct cmdline_list {
  136. struct cmdline_list *next;
  137. char *comm;
  138. int pid;
  139. };
  140. static int cmdline_init(struct pevent *pevent)
  141. {
  142. struct cmdline_list *cmdlist = pevent->cmdlist;
  143. struct cmdline_list *item;
  144. struct cmdline *cmdlines;
  145. int i;
  146. cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
  147. if (!cmdlines)
  148. return -1;
  149. i = 0;
  150. while (cmdlist) {
  151. cmdlines[i].pid = cmdlist->pid;
  152. cmdlines[i].comm = cmdlist->comm;
  153. i++;
  154. item = cmdlist;
  155. cmdlist = cmdlist->next;
  156. free(item);
  157. }
  158. qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  159. pevent->cmdlines = cmdlines;
  160. pevent->cmdlist = NULL;
  161. return 0;
  162. }
  163. static const char *find_cmdline(struct pevent *pevent, int pid)
  164. {
  165. const struct cmdline *comm;
  166. struct cmdline key;
  167. if (!pid)
  168. return "<idle>";
  169. if (!pevent->cmdlines && cmdline_init(pevent))
  170. return "<not enough memory for cmdlines!>";
  171. key.pid = pid;
  172. comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  173. sizeof(*pevent->cmdlines), cmdline_cmp);
  174. if (comm)
  175. return comm->comm;
  176. return "<...>";
  177. }
  178. /**
  179. * pevent_pid_is_registered - return if a pid has a cmdline registered
  180. * @pevent: handle for the pevent
  181. * @pid: The pid to check if it has a cmdline registered with.
  182. *
  183. * Returns 1 if the pid has a cmdline mapped to it
  184. * 0 otherwise.
  185. */
  186. int pevent_pid_is_registered(struct pevent *pevent, int pid)
  187. {
  188. const struct cmdline *comm;
  189. struct cmdline key;
  190. if (!pid)
  191. return 1;
  192. if (!pevent->cmdlines && cmdline_init(pevent))
  193. return 0;
  194. key.pid = pid;
  195. comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  196. sizeof(*pevent->cmdlines), cmdline_cmp);
  197. if (comm)
  198. return 1;
  199. return 0;
  200. }
  201. /*
  202. * If the command lines have been converted to an array, then
  203. * we must add this pid. This is much slower than when cmdlines
  204. * are added before the array is initialized.
  205. */
  206. static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
  207. {
  208. struct cmdline *cmdlines = pevent->cmdlines;
  209. const struct cmdline *cmdline;
  210. struct cmdline key;
  211. if (!pid)
  212. return 0;
  213. /* avoid duplicates */
  214. key.pid = pid;
  215. cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  216. sizeof(*pevent->cmdlines), cmdline_cmp);
  217. if (cmdline) {
  218. errno = EEXIST;
  219. return -1;
  220. }
  221. cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
  222. if (!cmdlines) {
  223. errno = ENOMEM;
  224. return -1;
  225. }
  226. cmdlines[pevent->cmdline_count].comm = strdup(comm);
  227. if (!cmdlines[pevent->cmdline_count].comm) {
  228. free(cmdlines);
  229. errno = ENOMEM;
  230. return -1;
  231. }
  232. cmdlines[pevent->cmdline_count].pid = pid;
  233. if (cmdlines[pevent->cmdline_count].comm)
  234. pevent->cmdline_count++;
  235. qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  236. pevent->cmdlines = cmdlines;
  237. return 0;
  238. }
  239. /**
  240. * pevent_register_comm - register a pid / comm mapping
  241. * @pevent: handle for the pevent
  242. * @comm: the command line to register
  243. * @pid: the pid to map the command line to
  244. *
  245. * This adds a mapping to search for command line names with
  246. * a given pid. The comm is duplicated.
  247. */
  248. int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
  249. {
  250. struct cmdline_list *item;
  251. if (pevent->cmdlines)
  252. return add_new_comm(pevent, comm, pid);
  253. item = malloc(sizeof(*item));
  254. if (!item)
  255. return -1;
  256. if (comm)
  257. item->comm = strdup(comm);
  258. else
  259. item->comm = strdup("<...>");
  260. if (!item->comm) {
  261. free(item);
  262. return -1;
  263. }
  264. item->pid = pid;
  265. item->next = pevent->cmdlist;
  266. pevent->cmdlist = item;
  267. pevent->cmdline_count++;
  268. return 0;
  269. }
  270. int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
  271. {
  272. pevent->trace_clock = strdup(trace_clock);
  273. if (!pevent->trace_clock) {
  274. errno = ENOMEM;
  275. return -1;
  276. }
  277. return 0;
  278. }
  279. struct func_map {
  280. unsigned long long addr;
  281. char *func;
  282. char *mod;
  283. };
  284. struct func_list {
  285. struct func_list *next;
  286. unsigned long long addr;
  287. char *func;
  288. char *mod;
  289. };
  290. static int func_cmp(const void *a, const void *b)
  291. {
  292. const struct func_map *fa = a;
  293. const struct func_map *fb = b;
  294. if (fa->addr < fb->addr)
  295. return -1;
  296. if (fa->addr > fb->addr)
  297. return 1;
  298. return 0;
  299. }
  300. /*
  301. * We are searching for a record in between, not an exact
  302. * match.
  303. */
  304. static int func_bcmp(const void *a, const void *b)
  305. {
  306. const struct func_map *fa = a;
  307. const struct func_map *fb = b;
  308. if ((fa->addr == fb->addr) ||
  309. (fa->addr > fb->addr &&
  310. fa->addr < (fb+1)->addr))
  311. return 0;
  312. if (fa->addr < fb->addr)
  313. return -1;
  314. return 1;
  315. }
  316. static int func_map_init(struct pevent *pevent)
  317. {
  318. struct func_list *funclist;
  319. struct func_list *item;
  320. struct func_map *func_map;
  321. int i;
  322. func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
  323. if (!func_map)
  324. return -1;
  325. funclist = pevent->funclist;
  326. i = 0;
  327. while (funclist) {
  328. func_map[i].func = funclist->func;
  329. func_map[i].addr = funclist->addr;
  330. func_map[i].mod = funclist->mod;
  331. i++;
  332. item = funclist;
  333. funclist = funclist->next;
  334. free(item);
  335. }
  336. qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
  337. /*
  338. * Add a special record at the end.
  339. */
  340. func_map[pevent->func_count].func = NULL;
  341. func_map[pevent->func_count].addr = 0;
  342. func_map[pevent->func_count].mod = NULL;
  343. pevent->func_map = func_map;
  344. pevent->funclist = NULL;
  345. return 0;
  346. }
  347. static struct func_map *
  348. __find_func(struct pevent *pevent, unsigned long long addr)
  349. {
  350. struct func_map *func;
  351. struct func_map key;
  352. if (!pevent->func_map)
  353. func_map_init(pevent);
  354. key.addr = addr;
  355. func = bsearch(&key, pevent->func_map, pevent->func_count,
  356. sizeof(*pevent->func_map), func_bcmp);
  357. return func;
  358. }
  359. struct func_resolver {
  360. pevent_func_resolver_t *func;
  361. void *priv;
  362. struct func_map map;
  363. };
  364. /**
  365. * pevent_set_function_resolver - set an alternative function resolver
  366. * @pevent: handle for the pevent
  367. * @resolver: function to be used
  368. * @priv: resolver function private state.
  369. *
  370. * Some tools may have already a way to resolve kernel functions, allow them to
  371. * keep using it instead of duplicating all the entries inside
  372. * pevent->funclist.
  373. */
  374. int pevent_set_function_resolver(struct pevent *pevent,
  375. pevent_func_resolver_t *func, void *priv)
  376. {
  377. struct func_resolver *resolver = malloc(sizeof(*resolver));
  378. if (resolver == NULL)
  379. return -1;
  380. resolver->func = func;
  381. resolver->priv = priv;
  382. free(pevent->func_resolver);
  383. pevent->func_resolver = resolver;
  384. return 0;
  385. }
  386. /**
  387. * pevent_reset_function_resolver - reset alternative function resolver
  388. * @pevent: handle for the pevent
  389. *
  390. * Stop using whatever alternative resolver was set, use the default
  391. * one instead.
  392. */
  393. void pevent_reset_function_resolver(struct pevent *pevent)
  394. {
  395. free(pevent->func_resolver);
  396. pevent->func_resolver = NULL;
  397. }
  398. static struct func_map *
  399. find_func(struct pevent *pevent, unsigned long long addr)
  400. {
  401. struct func_map *map;
  402. if (!pevent->func_resolver)
  403. return __find_func(pevent, addr);
  404. map = &pevent->func_resolver->map;
  405. map->mod = NULL;
  406. map->addr = addr;
  407. map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
  408. &map->addr, &map->mod);
  409. if (map->func == NULL)
  410. return NULL;
  411. return map;
  412. }
  413. /**
  414. * pevent_find_function - find a function by a given address
  415. * @pevent: handle for the pevent
  416. * @addr: the address to find the function with
  417. *
  418. * Returns a pointer to the function stored that has the given
  419. * address. Note, the address does not have to be exact, it
  420. * will select the function that would contain the address.
  421. */
  422. const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
  423. {
  424. struct func_map *map;
  425. map = find_func(pevent, addr);
  426. if (!map)
  427. return NULL;
  428. return map->func;
  429. }
  430. /**
  431. * pevent_find_function_address - find a function address by a given address
  432. * @pevent: handle for the pevent
  433. * @addr: the address to find the function with
  434. *
  435. * Returns the address the function starts at. This can be used in
  436. * conjunction with pevent_find_function to print both the function
  437. * name and the function offset.
  438. */
  439. unsigned long long
  440. pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
  441. {
  442. struct func_map *map;
  443. map = find_func(pevent, addr);
  444. if (!map)
  445. return 0;
  446. return map->addr;
  447. }
  448. /**
  449. * pevent_register_function - register a function with a given address
  450. * @pevent: handle for the pevent
  451. * @function: the function name to register
  452. * @addr: the address the function starts at
  453. * @mod: the kernel module the function may be in (NULL for none)
  454. *
  455. * This registers a function name with an address and module.
  456. * The @func passed in is duplicated.
  457. */
  458. int pevent_register_function(struct pevent *pevent, char *func,
  459. unsigned long long addr, char *mod)
  460. {
  461. struct func_list *item = malloc(sizeof(*item));
  462. if (!item)
  463. return -1;
  464. item->next = pevent->funclist;
  465. item->func = strdup(func);
  466. if (!item->func)
  467. goto out_free;
  468. if (mod) {
  469. item->mod = strdup(mod);
  470. if (!item->mod)
  471. goto out_free_func;
  472. } else
  473. item->mod = NULL;
  474. item->addr = addr;
  475. pevent->funclist = item;
  476. pevent->func_count++;
  477. return 0;
  478. out_free_func:
  479. free(item->func);
  480. item->func = NULL;
  481. out_free:
  482. free(item);
  483. errno = ENOMEM;
  484. return -1;
  485. }
  486. /**
  487. * pevent_print_funcs - print out the stored functions
  488. * @pevent: handle for the pevent
  489. *
  490. * This prints out the stored functions.
  491. */
  492. void pevent_print_funcs(struct pevent *pevent)
  493. {
  494. int i;
  495. if (!pevent->func_map)
  496. func_map_init(pevent);
  497. for (i = 0; i < (int)pevent->func_count; i++) {
  498. printf("%016llx %s",
  499. pevent->func_map[i].addr,
  500. pevent->func_map[i].func);
  501. if (pevent->func_map[i].mod)
  502. printf(" [%s]\n", pevent->func_map[i].mod);
  503. else
  504. printf("\n");
  505. }
  506. }
  507. struct printk_map {
  508. unsigned long long addr;
  509. char *printk;
  510. };
  511. struct printk_list {
  512. struct printk_list *next;
  513. unsigned long long addr;
  514. char *printk;
  515. };
  516. static int printk_cmp(const void *a, const void *b)
  517. {
  518. const struct printk_map *pa = a;
  519. const struct printk_map *pb = b;
  520. if (pa->addr < pb->addr)
  521. return -1;
  522. if (pa->addr > pb->addr)
  523. return 1;
  524. return 0;
  525. }
  526. static int printk_map_init(struct pevent *pevent)
  527. {
  528. struct printk_list *printklist;
  529. struct printk_list *item;
  530. struct printk_map *printk_map;
  531. int i;
  532. printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
  533. if (!printk_map)
  534. return -1;
  535. printklist = pevent->printklist;
  536. i = 0;
  537. while (printklist) {
  538. printk_map[i].printk = printklist->printk;
  539. printk_map[i].addr = printklist->addr;
  540. i++;
  541. item = printklist;
  542. printklist = printklist->next;
  543. free(item);
  544. }
  545. qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
  546. pevent->printk_map = printk_map;
  547. pevent->printklist = NULL;
  548. return 0;
  549. }
  550. static struct printk_map *
  551. find_printk(struct pevent *pevent, unsigned long long addr)
  552. {
  553. struct printk_map *printk;
  554. struct printk_map key;
  555. if (!pevent->printk_map && printk_map_init(pevent))
  556. return NULL;
  557. key.addr = addr;
  558. printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
  559. sizeof(*pevent->printk_map), printk_cmp);
  560. return printk;
  561. }
  562. /**
  563. * pevent_register_print_string - register a string by its address
  564. * @pevent: handle for the pevent
  565. * @fmt: the string format to register
  566. * @addr: the address the string was located at
  567. *
  568. * This registers a string by the address it was stored in the kernel.
  569. * The @fmt passed in is duplicated.
  570. */
  571. int pevent_register_print_string(struct pevent *pevent, const char *fmt,
  572. unsigned long long addr)
  573. {
  574. struct printk_list *item = malloc(sizeof(*item));
  575. char *p;
  576. if (!item)
  577. return -1;
  578. item->next = pevent->printklist;
  579. item->addr = addr;
  580. /* Strip off quotes and '\n' from the end */
  581. if (fmt[0] == '"')
  582. fmt++;
  583. item->printk = strdup(fmt);
  584. if (!item->printk)
  585. goto out_free;
  586. p = item->printk + strlen(item->printk) - 1;
  587. if (*p == '"')
  588. *p = 0;
  589. p -= 2;
  590. if (strcmp(p, "\\n") == 0)
  591. *p = 0;
  592. pevent->printklist = item;
  593. pevent->printk_count++;
  594. return 0;
  595. out_free:
  596. free(item);
  597. errno = ENOMEM;
  598. return -1;
  599. }
  600. /**
  601. * pevent_print_printk - print out the stored strings
  602. * @pevent: handle for the pevent
  603. *
  604. * This prints the string formats that were stored.
  605. */
  606. void pevent_print_printk(struct pevent *pevent)
  607. {
  608. int i;
  609. if (!pevent->printk_map)
  610. printk_map_init(pevent);
  611. for (i = 0; i < (int)pevent->printk_count; i++) {
  612. printf("%016llx %s\n",
  613. pevent->printk_map[i].addr,
  614. pevent->printk_map[i].printk);
  615. }
  616. }
  617. static struct event_format *alloc_event(void)
  618. {
  619. return calloc(1, sizeof(struct event_format));
  620. }
  621. static int add_event(struct pevent *pevent, struct event_format *event)
  622. {
  623. int i;
  624. struct event_format **events = realloc(pevent->events, sizeof(event) *
  625. (pevent->nr_events + 1));
  626. if (!events)
  627. return -1;
  628. pevent->events = events;
  629. for (i = 0; i < pevent->nr_events; i++) {
  630. if (pevent->events[i]->id > event->id)
  631. break;
  632. }
  633. if (i < pevent->nr_events)
  634. memmove(&pevent->events[i + 1],
  635. &pevent->events[i],
  636. sizeof(event) * (pevent->nr_events - i));
  637. pevent->events[i] = event;
  638. pevent->nr_events++;
  639. event->pevent = pevent;
  640. return 0;
  641. }
  642. static int event_item_type(enum event_type type)
  643. {
  644. switch (type) {
  645. case EVENT_ITEM ... EVENT_SQUOTE:
  646. return 1;
  647. case EVENT_ERROR ... EVENT_DELIM:
  648. default:
  649. return 0;
  650. }
  651. }
  652. static void free_flag_sym(struct print_flag_sym *fsym)
  653. {
  654. struct print_flag_sym *next;
  655. while (fsym) {
  656. next = fsym->next;
  657. free(fsym->value);
  658. free(fsym->str);
  659. free(fsym);
  660. fsym = next;
  661. }
  662. }
  663. static void free_arg(struct print_arg *arg)
  664. {
  665. struct print_arg *farg;
  666. if (!arg)
  667. return;
  668. switch (arg->type) {
  669. case PRINT_ATOM:
  670. free(arg->atom.atom);
  671. break;
  672. case PRINT_FIELD:
  673. free(arg->field.name);
  674. break;
  675. case PRINT_FLAGS:
  676. free_arg(arg->flags.field);
  677. free(arg->flags.delim);
  678. free_flag_sym(arg->flags.flags);
  679. break;
  680. case PRINT_SYMBOL:
  681. free_arg(arg->symbol.field);
  682. free_flag_sym(arg->symbol.symbols);
  683. break;
  684. case PRINT_HEX:
  685. free_arg(arg->hex.field);
  686. free_arg(arg->hex.size);
  687. break;
  688. case PRINT_INT_ARRAY:
  689. free_arg(arg->int_array.field);
  690. free_arg(arg->int_array.count);
  691. free_arg(arg->int_array.el_size);
  692. break;
  693. case PRINT_TYPE:
  694. free(arg->typecast.type);
  695. free_arg(arg->typecast.item);
  696. break;
  697. case PRINT_STRING:
  698. case PRINT_BSTRING:
  699. free(arg->string.string);
  700. break;
  701. case PRINT_BITMASK:
  702. free(arg->bitmask.bitmask);
  703. break;
  704. case PRINT_DYNAMIC_ARRAY:
  705. case PRINT_DYNAMIC_ARRAY_LEN:
  706. free(arg->dynarray.index);
  707. break;
  708. case PRINT_OP:
  709. free(arg->op.op);
  710. free_arg(arg->op.left);
  711. free_arg(arg->op.right);
  712. break;
  713. case PRINT_FUNC:
  714. while (arg->func.args) {
  715. farg = arg->func.args;
  716. arg->func.args = farg->next;
  717. free_arg(farg);
  718. }
  719. break;
  720. case PRINT_NULL:
  721. default:
  722. break;
  723. }
  724. free(arg);
  725. }
  726. static enum event_type get_type(int ch)
  727. {
  728. if (ch == '\n')
  729. return EVENT_NEWLINE;
  730. if (isspace(ch))
  731. return EVENT_SPACE;
  732. if (isalnum(ch) || ch == '_')
  733. return EVENT_ITEM;
  734. if (ch == '\'')
  735. return EVENT_SQUOTE;
  736. if (ch == '"')
  737. return EVENT_DQUOTE;
  738. if (!isprint(ch))
  739. return EVENT_NONE;
  740. if (ch == '(' || ch == ')' || ch == ',')
  741. return EVENT_DELIM;
  742. return EVENT_OP;
  743. }
  744. static int __read_char(void)
  745. {
  746. if (input_buf_ptr >= input_buf_siz)
  747. return -1;
  748. return input_buf[input_buf_ptr++];
  749. }
  750. static int __peek_char(void)
  751. {
  752. if (input_buf_ptr >= input_buf_siz)
  753. return -1;
  754. return input_buf[input_buf_ptr];
  755. }
  756. /**
  757. * pevent_peek_char - peek at the next character that will be read
  758. *
  759. * Returns the next character read, or -1 if end of buffer.
  760. */
  761. int pevent_peek_char(void)
  762. {
  763. return __peek_char();
  764. }
  765. static int extend_token(char **tok, char *buf, int size)
  766. {
  767. char *newtok = realloc(*tok, size);
  768. if (!newtok) {
  769. free(*tok);
  770. *tok = NULL;
  771. return -1;
  772. }
  773. if (!*tok)
  774. strcpy(newtok, buf);
  775. else
  776. strcat(newtok, buf);
  777. *tok = newtok;
  778. return 0;
  779. }
  780. static enum event_type force_token(const char *str, char **tok);
  781. static enum event_type __read_token(char **tok)
  782. {
  783. char buf[BUFSIZ];
  784. int ch, last_ch, quote_ch, next_ch;
  785. int i = 0;
  786. int tok_size = 0;
  787. enum event_type type;
  788. *tok = NULL;
  789. ch = __read_char();
  790. if (ch < 0)
  791. return EVENT_NONE;
  792. type = get_type(ch);
  793. if (type == EVENT_NONE)
  794. return type;
  795. buf[i++] = ch;
  796. switch (type) {
  797. case EVENT_NEWLINE:
  798. case EVENT_DELIM:
  799. if (asprintf(tok, "%c", ch) < 0)
  800. return EVENT_ERROR;
  801. return type;
  802. case EVENT_OP:
  803. switch (ch) {
  804. case '-':
  805. next_ch = __peek_char();
  806. if (next_ch == '>') {
  807. buf[i++] = __read_char();
  808. break;
  809. }
  810. /* fall through */
  811. case '+':
  812. case '|':
  813. case '&':
  814. case '>':
  815. case '<':
  816. last_ch = ch;
  817. ch = __peek_char();
  818. if (ch != last_ch)
  819. goto test_equal;
  820. buf[i++] = __read_char();
  821. switch (last_ch) {
  822. case '>':
  823. case '<':
  824. goto test_equal;
  825. default:
  826. break;
  827. }
  828. break;
  829. case '!':
  830. case '=':
  831. goto test_equal;
  832. default: /* what should we do instead? */
  833. break;
  834. }
  835. buf[i] = 0;
  836. *tok = strdup(buf);
  837. return type;
  838. test_equal:
  839. ch = __peek_char();
  840. if (ch == '=')
  841. buf[i++] = __read_char();
  842. goto out;
  843. case EVENT_DQUOTE:
  844. case EVENT_SQUOTE:
  845. /* don't keep quotes */
  846. i--;
  847. quote_ch = ch;
  848. last_ch = 0;
  849. concat:
  850. do {
  851. if (i == (BUFSIZ - 1)) {
  852. buf[i] = 0;
  853. tok_size += BUFSIZ;
  854. if (extend_token(tok, buf, tok_size) < 0)
  855. return EVENT_NONE;
  856. i = 0;
  857. }
  858. last_ch = ch;
  859. ch = __read_char();
  860. buf[i++] = ch;
  861. /* the '\' '\' will cancel itself */
  862. if (ch == '\\' && last_ch == '\\')
  863. last_ch = 0;
  864. } while (ch != quote_ch || last_ch == '\\');
  865. /* remove the last quote */
  866. i--;
  867. /*
  868. * For strings (double quotes) check the next token.
  869. * If it is another string, concatinate the two.
  870. */
  871. if (type == EVENT_DQUOTE) {
  872. unsigned long long save_input_buf_ptr = input_buf_ptr;
  873. do {
  874. ch = __read_char();
  875. } while (isspace(ch));
  876. if (ch == '"')
  877. goto concat;
  878. input_buf_ptr = save_input_buf_ptr;
  879. }
  880. goto out;
  881. case EVENT_ERROR ... EVENT_SPACE:
  882. case EVENT_ITEM:
  883. default:
  884. break;
  885. }
  886. while (get_type(__peek_char()) == type) {
  887. if (i == (BUFSIZ - 1)) {
  888. buf[i] = 0;
  889. tok_size += BUFSIZ;
  890. if (extend_token(tok, buf, tok_size) < 0)
  891. return EVENT_NONE;
  892. i = 0;
  893. }
  894. ch = __read_char();
  895. buf[i++] = ch;
  896. }
  897. out:
  898. buf[i] = 0;
  899. if (extend_token(tok, buf, tok_size + i + 1) < 0)
  900. return EVENT_NONE;
  901. if (type == EVENT_ITEM) {
  902. /*
  903. * Older versions of the kernel has a bug that
  904. * creates invalid symbols and will break the mac80211
  905. * parsing. This is a work around to that bug.
  906. *
  907. * See Linux kernel commit:
  908. * 811cb50baf63461ce0bdb234927046131fc7fa8b
  909. */
  910. if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
  911. free(*tok);
  912. *tok = NULL;
  913. return force_token("\"\%s\" ", tok);
  914. } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
  915. free(*tok);
  916. *tok = NULL;
  917. return force_token("\" sta:%pM\" ", tok);
  918. } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
  919. free(*tok);
  920. *tok = NULL;
  921. return force_token("\" vif:%p(%d)\" ", tok);
  922. }
  923. }
  924. return type;
  925. }
  926. static enum event_type force_token(const char *str, char **tok)
  927. {
  928. const char *save_input_buf;
  929. unsigned long long save_input_buf_ptr;
  930. unsigned long long save_input_buf_siz;
  931. enum event_type type;
  932. /* save off the current input pointers */
  933. save_input_buf = input_buf;
  934. save_input_buf_ptr = input_buf_ptr;
  935. save_input_buf_siz = input_buf_siz;
  936. init_input_buf(str, strlen(str));
  937. type = __read_token(tok);
  938. /* reset back to original token */
  939. input_buf = save_input_buf;
  940. input_buf_ptr = save_input_buf_ptr;
  941. input_buf_siz = save_input_buf_siz;
  942. return type;
  943. }
  944. static void free_token(char *tok)
  945. {
  946. if (tok)
  947. free(tok);
  948. }
  949. static enum event_type read_token(char **tok)
  950. {
  951. enum event_type type;
  952. for (;;) {
  953. type = __read_token(tok);
  954. if (type != EVENT_SPACE)
  955. return type;
  956. free_token(*tok);
  957. }
  958. /* not reached */
  959. *tok = NULL;
  960. return EVENT_NONE;
  961. }
  962. /**
  963. * pevent_read_token - access to utilites to use the pevent parser
  964. * @tok: The token to return
  965. *
  966. * This will parse tokens from the string given by
  967. * pevent_init_data().
  968. *
  969. * Returns the token type.
  970. */
  971. enum event_type pevent_read_token(char **tok)
  972. {
  973. return read_token(tok);
  974. }
  975. /**
  976. * pevent_free_token - free a token returned by pevent_read_token
  977. * @token: the token to free
  978. */
  979. void pevent_free_token(char *token)
  980. {
  981. free_token(token);
  982. }
  983. /* no newline */
  984. static enum event_type read_token_item(char **tok)
  985. {
  986. enum event_type type;
  987. for (;;) {
  988. type = __read_token(tok);
  989. if (type != EVENT_SPACE && type != EVENT_NEWLINE)
  990. return type;
  991. free_token(*tok);
  992. *tok = NULL;
  993. }
  994. /* not reached */
  995. *tok = NULL;
  996. return EVENT_NONE;
  997. }
  998. static int test_type(enum event_type type, enum event_type expect)
  999. {
  1000. if (type != expect) {
  1001. do_warning("Error: expected type %d but read %d",
  1002. expect, type);
  1003. return -1;
  1004. }
  1005. return 0;
  1006. }
  1007. static int test_type_token(enum event_type type, const char *token,
  1008. enum event_type expect, const char *expect_tok)
  1009. {
  1010. if (type != expect) {
  1011. do_warning("Error: expected type %d but read %d",
  1012. expect, type);
  1013. return -1;
  1014. }
  1015. if (strcmp(token, expect_tok) != 0) {
  1016. do_warning("Error: expected '%s' but read '%s'",
  1017. expect_tok, token);
  1018. return -1;
  1019. }
  1020. return 0;
  1021. }
  1022. static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
  1023. {
  1024. enum event_type type;
  1025. if (newline_ok)
  1026. type = read_token(tok);
  1027. else
  1028. type = read_token_item(tok);
  1029. return test_type(type, expect);
  1030. }
  1031. static int read_expect_type(enum event_type expect, char **tok)
  1032. {
  1033. return __read_expect_type(expect, tok, 1);
  1034. }
  1035. static int __read_expected(enum event_type expect, const char *str,
  1036. int newline_ok)
  1037. {
  1038. enum event_type type;
  1039. char *token;
  1040. int ret;
  1041. if (newline_ok)
  1042. type = read_token(&token);
  1043. else
  1044. type = read_token_item(&token);
  1045. ret = test_type_token(type, token, expect, str);
  1046. free_token(token);
  1047. return ret;
  1048. }
  1049. static int read_expected(enum event_type expect, const char *str)
  1050. {
  1051. return __read_expected(expect, str, 1);
  1052. }
  1053. static int read_expected_item(enum event_type expect, const char *str)
  1054. {
  1055. return __read_expected(expect, str, 0);
  1056. }
  1057. static char *event_read_name(void)
  1058. {
  1059. char *token;
  1060. if (read_expected(EVENT_ITEM, "name") < 0)
  1061. return NULL;
  1062. if (read_expected(EVENT_OP, ":") < 0)
  1063. return NULL;
  1064. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1065. goto fail;
  1066. return token;
  1067. fail:
  1068. free_token(token);
  1069. return NULL;
  1070. }
  1071. static int event_read_id(void)
  1072. {
  1073. char *token;
  1074. int id;
  1075. if (read_expected_item(EVENT_ITEM, "ID") < 0)
  1076. return -1;
  1077. if (read_expected(EVENT_OP, ":") < 0)
  1078. return -1;
  1079. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1080. goto fail;
  1081. id = strtoul(token, NULL, 0);
  1082. free_token(token);
  1083. return id;
  1084. fail:
  1085. free_token(token);
  1086. return -1;
  1087. }
  1088. static int field_is_string(struct format_field *field)
  1089. {
  1090. if ((field->flags & FIELD_IS_ARRAY) &&
  1091. (strstr(field->type, "char") || strstr(field->type, "u8") ||
  1092. strstr(field->type, "s8")))
  1093. return 1;
  1094. return 0;
  1095. }
  1096. static int field_is_dynamic(struct format_field *field)
  1097. {
  1098. if (strncmp(field->type, "__data_loc", 10) == 0)
  1099. return 1;
  1100. return 0;
  1101. }
  1102. static int field_is_long(struct format_field *field)
  1103. {
  1104. /* includes long long */
  1105. if (strstr(field->type, "long"))
  1106. return 1;
  1107. return 0;
  1108. }
  1109. static unsigned int type_size(const char *name)
  1110. {
  1111. /* This covers all FIELD_IS_STRING types. */
  1112. static struct {
  1113. const char *type;
  1114. unsigned int size;
  1115. } table[] = {
  1116. { "u8", 1 },
  1117. { "u16", 2 },
  1118. { "u32", 4 },
  1119. { "u64", 8 },
  1120. { "s8", 1 },
  1121. { "s16", 2 },
  1122. { "s32", 4 },
  1123. { "s64", 8 },
  1124. { "char", 1 },
  1125. { },
  1126. };
  1127. int i;
  1128. for (i = 0; table[i].type; i++) {
  1129. if (!strcmp(table[i].type, name))
  1130. return table[i].size;
  1131. }
  1132. return 0;
  1133. }
  1134. static int event_read_fields(struct event_format *event, struct format_field **fields)
  1135. {
  1136. struct format_field *field = NULL;
  1137. enum event_type type;
  1138. char *token;
  1139. char *last_token;
  1140. int count = 0;
  1141. do {
  1142. unsigned int size_dynamic = 0;
  1143. type = read_token(&token);
  1144. if (type == EVENT_NEWLINE) {
  1145. free_token(token);
  1146. return count;
  1147. }
  1148. count++;
  1149. if (test_type_token(type, token, EVENT_ITEM, "field"))
  1150. goto fail;
  1151. free_token(token);
  1152. type = read_token(&token);
  1153. /*
  1154. * The ftrace fields may still use the "special" name.
  1155. * Just ignore it.
  1156. */
  1157. if (event->flags & EVENT_FL_ISFTRACE &&
  1158. type == EVENT_ITEM && strcmp(token, "special") == 0) {
  1159. free_token(token);
  1160. type = read_token(&token);
  1161. }
  1162. if (test_type_token(type, token, EVENT_OP, ":") < 0)
  1163. goto fail;
  1164. free_token(token);
  1165. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1166. goto fail;
  1167. last_token = token;
  1168. field = calloc(1, sizeof(*field));
  1169. if (!field)
  1170. goto fail;
  1171. field->event = event;
  1172. /* read the rest of the type */
  1173. for (;;) {
  1174. type = read_token(&token);
  1175. if (type == EVENT_ITEM ||
  1176. (type == EVENT_OP && strcmp(token, "*") == 0) ||
  1177. /*
  1178. * Some of the ftrace fields are broken and have
  1179. * an illegal "." in them.
  1180. */
  1181. (event->flags & EVENT_FL_ISFTRACE &&
  1182. type == EVENT_OP && strcmp(token, ".") == 0)) {
  1183. if (strcmp(token, "*") == 0)
  1184. field->flags |= FIELD_IS_POINTER;
  1185. if (field->type) {
  1186. char *new_type;
  1187. new_type = realloc(field->type,
  1188. strlen(field->type) +
  1189. strlen(last_token) + 2);
  1190. if (!new_type) {
  1191. free(last_token);
  1192. goto fail;
  1193. }
  1194. field->type = new_type;
  1195. strcat(field->type, " ");
  1196. strcat(field->type, last_token);
  1197. free(last_token);
  1198. } else
  1199. field->type = last_token;
  1200. last_token = token;
  1201. continue;
  1202. }
  1203. break;
  1204. }
  1205. if (!field->type) {
  1206. do_warning_event(event, "%s: no type found", __func__);
  1207. goto fail;
  1208. }
  1209. field->name = field->alias = last_token;
  1210. if (test_type(type, EVENT_OP))
  1211. goto fail;
  1212. if (strcmp(token, "[") == 0) {
  1213. enum event_type last_type = type;
  1214. char *brackets = token;
  1215. char *new_brackets;
  1216. int len;
  1217. field->flags |= FIELD_IS_ARRAY;
  1218. type = read_token(&token);
  1219. if (type == EVENT_ITEM)
  1220. field->arraylen = strtoul(token, NULL, 0);
  1221. else
  1222. field->arraylen = 0;
  1223. while (strcmp(token, "]") != 0) {
  1224. if (last_type == EVENT_ITEM &&
  1225. type == EVENT_ITEM)
  1226. len = 2;
  1227. else
  1228. len = 1;
  1229. last_type = type;
  1230. new_brackets = realloc(brackets,
  1231. strlen(brackets) +
  1232. strlen(token) + len);
  1233. if (!new_brackets) {
  1234. free(brackets);
  1235. goto fail;
  1236. }
  1237. brackets = new_brackets;
  1238. if (len == 2)
  1239. strcat(brackets, " ");
  1240. strcat(brackets, token);
  1241. /* We only care about the last token */
  1242. field->arraylen = strtoul(token, NULL, 0);
  1243. free_token(token);
  1244. type = read_token(&token);
  1245. if (type == EVENT_NONE) {
  1246. do_warning_event(event, "failed to find token");
  1247. goto fail;
  1248. }
  1249. }
  1250. free_token(token);
  1251. new_brackets = realloc(brackets, strlen(brackets) + 2);
  1252. if (!new_brackets) {
  1253. free(brackets);
  1254. goto fail;
  1255. }
  1256. brackets = new_brackets;
  1257. strcat(brackets, "]");
  1258. /* add brackets to type */
  1259. type = read_token(&token);
  1260. /*
  1261. * If the next token is not an OP, then it is of
  1262. * the format: type [] item;
  1263. */
  1264. if (type == EVENT_ITEM) {
  1265. char *new_type;
  1266. new_type = realloc(field->type,
  1267. strlen(field->type) +
  1268. strlen(field->name) +
  1269. strlen(brackets) + 2);
  1270. if (!new_type) {
  1271. free(brackets);
  1272. goto fail;
  1273. }
  1274. field->type = new_type;
  1275. strcat(field->type, " ");
  1276. strcat(field->type, field->name);
  1277. size_dynamic = type_size(field->name);
  1278. free_token(field->name);
  1279. strcat(field->type, brackets);
  1280. field->name = field->alias = token;
  1281. type = read_token(&token);
  1282. } else {
  1283. char *new_type;
  1284. new_type = realloc(field->type,
  1285. strlen(field->type) +
  1286. strlen(brackets) + 1);
  1287. if (!new_type) {
  1288. free(brackets);
  1289. goto fail;
  1290. }
  1291. field->type = new_type;
  1292. strcat(field->type, brackets);
  1293. }
  1294. free(brackets);
  1295. }
  1296. if (field_is_string(field))
  1297. field->flags |= FIELD_IS_STRING;
  1298. if (field_is_dynamic(field))
  1299. field->flags |= FIELD_IS_DYNAMIC;
  1300. if (field_is_long(field))
  1301. field->flags |= FIELD_IS_LONG;
  1302. if (test_type_token(type, token, EVENT_OP, ";"))
  1303. goto fail;
  1304. free_token(token);
  1305. if (read_expected(EVENT_ITEM, "offset") < 0)
  1306. goto fail_expect;
  1307. if (read_expected(EVENT_OP, ":") < 0)
  1308. goto fail_expect;
  1309. if (read_expect_type(EVENT_ITEM, &token))
  1310. goto fail;
  1311. field->offset = strtoul(token, NULL, 0);
  1312. free_token(token);
  1313. if (read_expected(EVENT_OP, ";") < 0)
  1314. goto fail_expect;
  1315. if (read_expected(EVENT_ITEM, "size") < 0)
  1316. goto fail_expect;
  1317. if (read_expected(EVENT_OP, ":") < 0)
  1318. goto fail_expect;
  1319. if (read_expect_type(EVENT_ITEM, &token))
  1320. goto fail;
  1321. field->size = strtoul(token, NULL, 0);
  1322. free_token(token);
  1323. if (read_expected(EVENT_OP, ";") < 0)
  1324. goto fail_expect;
  1325. type = read_token(&token);
  1326. if (type != EVENT_NEWLINE) {
  1327. /* newer versions of the kernel have a "signed" type */
  1328. if (test_type_token(type, token, EVENT_ITEM, "signed"))
  1329. goto fail;
  1330. free_token(token);
  1331. if (read_expected(EVENT_OP, ":") < 0)
  1332. goto fail_expect;
  1333. if (read_expect_type(EVENT_ITEM, &token))
  1334. goto fail;
  1335. if (strtoul(token, NULL, 0))
  1336. field->flags |= FIELD_IS_SIGNED;
  1337. free_token(token);
  1338. if (read_expected(EVENT_OP, ";") < 0)
  1339. goto fail_expect;
  1340. if (read_expect_type(EVENT_NEWLINE, &token))
  1341. goto fail;
  1342. }
  1343. free_token(token);
  1344. if (field->flags & FIELD_IS_ARRAY) {
  1345. if (field->arraylen)
  1346. field->elementsize = field->size / field->arraylen;
  1347. else if (field->flags & FIELD_IS_DYNAMIC)
  1348. field->elementsize = size_dynamic;
  1349. else if (field->flags & FIELD_IS_STRING)
  1350. field->elementsize = 1;
  1351. else if (field->flags & FIELD_IS_LONG)
  1352. field->elementsize = event->pevent ?
  1353. event->pevent->long_size :
  1354. sizeof(long);
  1355. } else
  1356. field->elementsize = field->size;
  1357. *fields = field;
  1358. fields = &field->next;
  1359. } while (1);
  1360. return 0;
  1361. fail:
  1362. free_token(token);
  1363. fail_expect:
  1364. if (field) {
  1365. free(field->type);
  1366. free(field->name);
  1367. free(field);
  1368. }
  1369. return -1;
  1370. }
  1371. static int event_read_format(struct event_format *event)
  1372. {
  1373. char *token;
  1374. int ret;
  1375. if (read_expected_item(EVENT_ITEM, "format") < 0)
  1376. return -1;
  1377. if (read_expected(EVENT_OP, ":") < 0)
  1378. return -1;
  1379. if (read_expect_type(EVENT_NEWLINE, &token))
  1380. goto fail;
  1381. free_token(token);
  1382. ret = event_read_fields(event, &event->format.common_fields);
  1383. if (ret < 0)
  1384. return ret;
  1385. event->format.nr_common = ret;
  1386. ret = event_read_fields(event, &event->format.fields);
  1387. if (ret < 0)
  1388. return ret;
  1389. event->format.nr_fields = ret;
  1390. return 0;
  1391. fail:
  1392. free_token(token);
  1393. return -1;
  1394. }
  1395. static enum event_type
  1396. process_arg_token(struct event_format *event, struct print_arg *arg,
  1397. char **tok, enum event_type type);
  1398. static enum event_type
  1399. process_arg(struct event_format *event, struct print_arg *arg, char **tok)
  1400. {
  1401. enum event_type type;
  1402. char *token;
  1403. type = read_token(&token);
  1404. *tok = token;
  1405. return process_arg_token(event, arg, tok, type);
  1406. }
  1407. static enum event_type
  1408. process_op(struct event_format *event, struct print_arg *arg, char **tok);
  1409. /*
  1410. * For __print_symbolic() and __print_flags, we need to completely
  1411. * evaluate the first argument, which defines what to print next.
  1412. */
  1413. static enum event_type
  1414. process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
  1415. {
  1416. enum event_type type;
  1417. type = process_arg(event, arg, tok);
  1418. while (type == EVENT_OP) {
  1419. type = process_op(event, arg, tok);
  1420. }
  1421. return type;
  1422. }
  1423. static enum event_type
  1424. process_cond(struct event_format *event, struct print_arg *top, char **tok)
  1425. {
  1426. struct print_arg *arg, *left, *right;
  1427. enum event_type type;
  1428. char *token = NULL;
  1429. arg = alloc_arg();
  1430. left = alloc_arg();
  1431. right = alloc_arg();
  1432. if (!arg || !left || !right) {
  1433. do_warning_event(event, "%s: not enough memory!", __func__);
  1434. /* arg will be freed at out_free */
  1435. free_arg(left);
  1436. free_arg(right);
  1437. goto out_free;
  1438. }
  1439. arg->type = PRINT_OP;
  1440. arg->op.left = left;
  1441. arg->op.right = right;
  1442. *tok = NULL;
  1443. type = process_arg(event, left, &token);
  1444. again:
  1445. if (type == EVENT_ERROR)
  1446. goto out_free;
  1447. /* Handle other operations in the arguments */
  1448. if (type == EVENT_OP && strcmp(token, ":") != 0) {
  1449. type = process_op(event, left, &token);
  1450. goto again;
  1451. }
  1452. if (test_type_token(type, token, EVENT_OP, ":"))
  1453. goto out_free;
  1454. arg->op.op = token;
  1455. type = process_arg(event, right, &token);
  1456. top->op.right = arg;
  1457. *tok = token;
  1458. return type;
  1459. out_free:
  1460. /* Top may point to itself */
  1461. top->op.right = NULL;
  1462. free_token(token);
  1463. free_arg(arg);
  1464. return EVENT_ERROR;
  1465. }
  1466. static enum event_type
  1467. process_array(struct event_format *event, struct print_arg *top, char **tok)
  1468. {
  1469. struct print_arg *arg;
  1470. enum event_type type;
  1471. char *token = NULL;
  1472. arg = alloc_arg();
  1473. if (!arg) {
  1474. do_warning_event(event, "%s: not enough memory!", __func__);
  1475. /* '*tok' is set to top->op.op. No need to free. */
  1476. *tok = NULL;
  1477. return EVENT_ERROR;
  1478. }
  1479. *tok = NULL;
  1480. type = process_arg(event, arg, &token);
  1481. if (test_type_token(type, token, EVENT_OP, "]"))
  1482. goto out_free;
  1483. top->op.right = arg;
  1484. free_token(token);
  1485. type = read_token_item(&token);
  1486. *tok = token;
  1487. return type;
  1488. out_free:
  1489. free_token(token);
  1490. free_arg(arg);
  1491. return EVENT_ERROR;
  1492. }
  1493. static int get_op_prio(char *op)
  1494. {
  1495. if (!op[1]) {
  1496. switch (op[0]) {
  1497. case '~':
  1498. case '!':
  1499. return 4;
  1500. case '*':
  1501. case '/':
  1502. case '%':
  1503. return 6;
  1504. case '+':
  1505. case '-':
  1506. return 7;
  1507. /* '>>' and '<<' are 8 */
  1508. case '<':
  1509. case '>':
  1510. return 9;
  1511. /* '==' and '!=' are 10 */
  1512. case '&':
  1513. return 11;
  1514. case '^':
  1515. return 12;
  1516. case '|':
  1517. return 13;
  1518. case '?':
  1519. return 16;
  1520. default:
  1521. do_warning("unknown op '%c'", op[0]);
  1522. return -1;
  1523. }
  1524. } else {
  1525. if (strcmp(op, "++") == 0 ||
  1526. strcmp(op, "--") == 0) {
  1527. return 3;
  1528. } else if (strcmp(op, ">>") == 0 ||
  1529. strcmp(op, "<<") == 0) {
  1530. return 8;
  1531. } else if (strcmp(op, ">=") == 0 ||
  1532. strcmp(op, "<=") == 0) {
  1533. return 9;
  1534. } else if (strcmp(op, "==") == 0 ||
  1535. strcmp(op, "!=") == 0) {
  1536. return 10;
  1537. } else if (strcmp(op, "&&") == 0) {
  1538. return 14;
  1539. } else if (strcmp(op, "||") == 0) {
  1540. return 15;
  1541. } else {
  1542. do_warning("unknown op '%s'", op);
  1543. return -1;
  1544. }
  1545. }
  1546. }
  1547. static int set_op_prio(struct print_arg *arg)
  1548. {
  1549. /* single ops are the greatest */
  1550. if (!arg->op.left || arg->op.left->type == PRINT_NULL)
  1551. arg->op.prio = 0;
  1552. else
  1553. arg->op.prio = get_op_prio(arg->op.op);
  1554. return arg->op.prio;
  1555. }
  1556. /* Note, *tok does not get freed, but will most likely be saved */
  1557. static enum event_type
  1558. process_op(struct event_format *event, struct print_arg *arg, char **tok)
  1559. {
  1560. struct print_arg *left, *right = NULL;
  1561. enum event_type type;
  1562. char *token;
  1563. /* the op is passed in via tok */
  1564. token = *tok;
  1565. if (arg->type == PRINT_OP && !arg->op.left) {
  1566. /* handle single op */
  1567. if (token[1]) {
  1568. do_warning_event(event, "bad op token %s", token);
  1569. goto out_free;
  1570. }
  1571. switch (token[0]) {
  1572. case '~':
  1573. case '!':
  1574. case '+':
  1575. case '-':
  1576. break;
  1577. default:
  1578. do_warning_event(event, "bad op token %s", token);
  1579. goto out_free;
  1580. }
  1581. /* make an empty left */
  1582. left = alloc_arg();
  1583. if (!left)
  1584. goto out_warn_free;
  1585. left->type = PRINT_NULL;
  1586. arg->op.left = left;
  1587. right = alloc_arg();
  1588. if (!right)
  1589. goto out_warn_free;
  1590. arg->op.right = right;
  1591. /* do not free the token, it belongs to an op */
  1592. *tok = NULL;
  1593. type = process_arg(event, right, tok);
  1594. } else if (strcmp(token, "?") == 0) {
  1595. left = alloc_arg();
  1596. if (!left)
  1597. goto out_warn_free;
  1598. /* copy the top arg to the left */
  1599. *left = *arg;
  1600. arg->type = PRINT_OP;
  1601. arg->op.op = token;
  1602. arg->op.left = left;
  1603. arg->op.prio = 0;
  1604. /* it will set arg->op.right */
  1605. type = process_cond(event, arg, tok);
  1606. } else if (strcmp(token, ">>") == 0 ||
  1607. strcmp(token, "<<") == 0 ||
  1608. strcmp(token, "&") == 0 ||
  1609. strcmp(token, "|") == 0 ||
  1610. strcmp(token, "&&") == 0 ||
  1611. strcmp(token, "||") == 0 ||
  1612. strcmp(token, "-") == 0 ||
  1613. strcmp(token, "+") == 0 ||
  1614. strcmp(token, "*") == 0 ||
  1615. strcmp(token, "^") == 0 ||
  1616. strcmp(token, "/") == 0 ||
  1617. strcmp(token, "%") == 0 ||
  1618. strcmp(token, "<") == 0 ||
  1619. strcmp(token, ">") == 0 ||
  1620. strcmp(token, "<=") == 0 ||
  1621. strcmp(token, ">=") == 0 ||
  1622. strcmp(token, "==") == 0 ||
  1623. strcmp(token, "!=") == 0) {
  1624. left = alloc_arg();
  1625. if (!left)
  1626. goto out_warn_free;
  1627. /* copy the top arg to the left */
  1628. *left = *arg;
  1629. arg->type = PRINT_OP;
  1630. arg->op.op = token;
  1631. arg->op.left = left;
  1632. arg->op.right = NULL;
  1633. if (set_op_prio(arg) == -1) {
  1634. event->flags |= EVENT_FL_FAILED;
  1635. /* arg->op.op (= token) will be freed at out_free */
  1636. arg->op.op = NULL;
  1637. goto out_free;
  1638. }
  1639. type = read_token_item(&token);
  1640. *tok = token;
  1641. /* could just be a type pointer */
  1642. if ((strcmp(arg->op.op, "*") == 0) &&
  1643. type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
  1644. char *new_atom;
  1645. if (left->type != PRINT_ATOM) {
  1646. do_warning_event(event, "bad pointer type");
  1647. goto out_free;
  1648. }
  1649. new_atom = realloc(left->atom.atom,
  1650. strlen(left->atom.atom) + 3);
  1651. if (!new_atom)
  1652. goto out_warn_free;
  1653. left->atom.atom = new_atom;
  1654. strcat(left->atom.atom, " *");
  1655. free(arg->op.op);
  1656. *arg = *left;
  1657. free(left);
  1658. return type;
  1659. }
  1660. right = alloc_arg();
  1661. if (!right)
  1662. goto out_warn_free;
  1663. type = process_arg_token(event, right, tok, type);
  1664. if (type == EVENT_ERROR) {
  1665. free_arg(right);
  1666. /* token was freed in process_arg_token() via *tok */
  1667. token = NULL;
  1668. goto out_free;
  1669. }
  1670. if (right->type == PRINT_OP &&
  1671. get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
  1672. struct print_arg tmp;
  1673. /* rotate ops according to the priority */
  1674. arg->op.right = right->op.left;
  1675. tmp = *arg;
  1676. *arg = *right;
  1677. *right = tmp;
  1678. arg->op.left = right;
  1679. } else {
  1680. arg->op.right = right;
  1681. }
  1682. } else if (strcmp(token, "[") == 0) {
  1683. left = alloc_arg();
  1684. if (!left)
  1685. goto out_warn_free;
  1686. *left = *arg;
  1687. arg->type = PRINT_OP;
  1688. arg->op.op = token;
  1689. arg->op.left = left;
  1690. arg->op.prio = 0;
  1691. /* it will set arg->op.right */
  1692. type = process_array(event, arg, tok);
  1693. } else {
  1694. do_warning_event(event, "unknown op '%s'", token);
  1695. event->flags |= EVENT_FL_FAILED;
  1696. /* the arg is now the left side */
  1697. goto out_free;
  1698. }
  1699. if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
  1700. int prio;
  1701. /* higher prios need to be closer to the root */
  1702. prio = get_op_prio(*tok);
  1703. if (prio > arg->op.prio)
  1704. return process_op(event, arg, tok);
  1705. return process_op(event, right, tok);
  1706. }
  1707. return type;
  1708. out_warn_free:
  1709. do_warning_event(event, "%s: not enough memory!", __func__);
  1710. out_free:
  1711. free_token(token);
  1712. *tok = NULL;
  1713. return EVENT_ERROR;
  1714. }
  1715. static enum event_type
  1716. process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
  1717. char **tok)
  1718. {
  1719. enum event_type type;
  1720. char *field;
  1721. char *token;
  1722. if (read_expected(EVENT_OP, "->") < 0)
  1723. goto out_err;
  1724. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1725. goto out_free;
  1726. field = token;
  1727. arg->type = PRINT_FIELD;
  1728. arg->field.name = field;
  1729. if (is_flag_field) {
  1730. arg->field.field = pevent_find_any_field(event, arg->field.name);
  1731. arg->field.field->flags |= FIELD_IS_FLAG;
  1732. is_flag_field = 0;
  1733. } else if (is_symbolic_field) {
  1734. arg->field.field = pevent_find_any_field(event, arg->field.name);
  1735. arg->field.field->flags |= FIELD_IS_SYMBOLIC;
  1736. is_symbolic_field = 0;
  1737. }
  1738. type = read_token(&token);
  1739. *tok = token;
  1740. return type;
  1741. out_free:
  1742. free_token(token);
  1743. out_err:
  1744. *tok = NULL;
  1745. return EVENT_ERROR;
  1746. }
  1747. static int alloc_and_process_delim(struct event_format *event, char *next_token,
  1748. struct print_arg **print_arg)
  1749. {
  1750. struct print_arg *field;
  1751. enum event_type type;
  1752. char *token;
  1753. int ret = 0;
  1754. field = alloc_arg();
  1755. if (!field) {
  1756. do_warning_event(event, "%s: not enough memory!", __func__);
  1757. errno = ENOMEM;
  1758. return -1;
  1759. }
  1760. type = process_arg(event, field, &token);
  1761. if (test_type_token(type, token, EVENT_DELIM, next_token)) {
  1762. errno = EINVAL;
  1763. ret = -1;
  1764. free_arg(field);
  1765. goto out_free_token;
  1766. }
  1767. *print_arg = field;
  1768. out_free_token:
  1769. free_token(token);
  1770. return ret;
  1771. }
  1772. static char *arg_eval (struct print_arg *arg);
  1773. static unsigned long long
  1774. eval_type_str(unsigned long long val, const char *type, int pointer)
  1775. {
  1776. int sign = 0;
  1777. char *ref;
  1778. int len;
  1779. len = strlen(type);
  1780. if (pointer) {
  1781. if (type[len-1] != '*') {
  1782. do_warning("pointer expected with non pointer type");
  1783. return val;
  1784. }
  1785. ref = malloc(len);
  1786. if (!ref) {
  1787. do_warning("%s: not enough memory!", __func__);
  1788. return val;
  1789. }
  1790. memcpy(ref, type, len);
  1791. /* chop off the " *" */
  1792. ref[len - 2] = 0;
  1793. val = eval_type_str(val, ref, 0);
  1794. free(ref);
  1795. return val;
  1796. }
  1797. /* check if this is a pointer */
  1798. if (type[len - 1] == '*')
  1799. return val;
  1800. /* Try to figure out the arg size*/
  1801. if (strncmp(type, "struct", 6) == 0)
  1802. /* all bets off */
  1803. return val;
  1804. if (strcmp(type, "u8") == 0)
  1805. return val & 0xff;
  1806. if (strcmp(type, "u16") == 0)
  1807. return val & 0xffff;
  1808. if (strcmp(type, "u32") == 0)
  1809. return val & 0xffffffff;
  1810. if (strcmp(type, "u64") == 0 ||
  1811. strcmp(type, "s64"))
  1812. return val;
  1813. if (strcmp(type, "s8") == 0)
  1814. return (unsigned long long)(char)val & 0xff;
  1815. if (strcmp(type, "s16") == 0)
  1816. return (unsigned long long)(short)val & 0xffff;
  1817. if (strcmp(type, "s32") == 0)
  1818. return (unsigned long long)(int)val & 0xffffffff;
  1819. if (strncmp(type, "unsigned ", 9) == 0) {
  1820. sign = 0;
  1821. type += 9;
  1822. }
  1823. if (strcmp(type, "char") == 0) {
  1824. if (sign)
  1825. return (unsigned long long)(char)val & 0xff;
  1826. else
  1827. return val & 0xff;
  1828. }
  1829. if (strcmp(type, "short") == 0) {
  1830. if (sign)
  1831. return (unsigned long long)(short)val & 0xffff;
  1832. else
  1833. return val & 0xffff;
  1834. }
  1835. if (strcmp(type, "int") == 0) {
  1836. if (sign)
  1837. return (unsigned long long)(int)val & 0xffffffff;
  1838. else
  1839. return val & 0xffffffff;
  1840. }
  1841. return val;
  1842. }
  1843. /*
  1844. * Try to figure out the type.
  1845. */
  1846. static unsigned long long
  1847. eval_type(unsigned long long val, struct print_arg *arg, int pointer)
  1848. {
  1849. if (arg->type != PRINT_TYPE) {
  1850. do_warning("expected type argument");
  1851. return 0;
  1852. }
  1853. return eval_type_str(val, arg->typecast.type, pointer);
  1854. }
  1855. static int arg_num_eval(struct print_arg *arg, long long *val)
  1856. {
  1857. long long left, right;
  1858. int ret = 1;
  1859. switch (arg->type) {
  1860. case PRINT_ATOM:
  1861. *val = strtoll(arg->atom.atom, NULL, 0);
  1862. break;
  1863. case PRINT_TYPE:
  1864. ret = arg_num_eval(arg->typecast.item, val);
  1865. if (!ret)
  1866. break;
  1867. *val = eval_type(*val, arg, 0);
  1868. break;
  1869. case PRINT_OP:
  1870. switch (arg->op.op[0]) {
  1871. case '|':
  1872. ret = arg_num_eval(arg->op.left, &left);
  1873. if (!ret)
  1874. break;
  1875. ret = arg_num_eval(arg->op.right, &right);
  1876. if (!ret)
  1877. break;
  1878. if (arg->op.op[1])
  1879. *val = left || right;
  1880. else
  1881. *val = left | right;
  1882. break;
  1883. case '&':
  1884. ret = arg_num_eval(arg->op.left, &left);
  1885. if (!ret)
  1886. break;
  1887. ret = arg_num_eval(arg->op.right, &right);
  1888. if (!ret)
  1889. break;
  1890. if (arg->op.op[1])
  1891. *val = left && right;
  1892. else
  1893. *val = left & right;
  1894. break;
  1895. case '<':
  1896. ret = arg_num_eval(arg->op.left, &left);
  1897. if (!ret)
  1898. break;
  1899. ret = arg_num_eval(arg->op.right, &right);
  1900. if (!ret)
  1901. break;
  1902. switch (arg->op.op[1]) {
  1903. case 0:
  1904. *val = left < right;
  1905. break;
  1906. case '<':
  1907. *val = left << right;
  1908. break;
  1909. case '=':
  1910. *val = left <= right;
  1911. break;
  1912. default:
  1913. do_warning("unknown op '%s'", arg->op.op);
  1914. ret = 0;
  1915. }
  1916. break;
  1917. case '>':
  1918. ret = arg_num_eval(arg->op.left, &left);
  1919. if (!ret)
  1920. break;
  1921. ret = arg_num_eval(arg->op.right, &right);
  1922. if (!ret)
  1923. break;
  1924. switch (arg->op.op[1]) {
  1925. case 0:
  1926. *val = left > right;
  1927. break;
  1928. case '>':
  1929. *val = left >> right;
  1930. break;
  1931. case '=':
  1932. *val = left >= right;
  1933. break;
  1934. default:
  1935. do_warning("unknown op '%s'", arg->op.op);
  1936. ret = 0;
  1937. }
  1938. break;
  1939. case '=':
  1940. ret = arg_num_eval(arg->op.left, &left);
  1941. if (!ret)
  1942. break;
  1943. ret = arg_num_eval(arg->op.right, &right);
  1944. if (!ret)
  1945. break;
  1946. if (arg->op.op[1] != '=') {
  1947. do_warning("unknown op '%s'", arg->op.op);
  1948. ret = 0;
  1949. } else
  1950. *val = left == right;
  1951. break;
  1952. case '!':
  1953. ret = arg_num_eval(arg->op.left, &left);
  1954. if (!ret)
  1955. break;
  1956. ret = arg_num_eval(arg->op.right, &right);
  1957. if (!ret)
  1958. break;
  1959. switch (arg->op.op[1]) {
  1960. case '=':
  1961. *val = left != right;
  1962. break;
  1963. default:
  1964. do_warning("unknown op '%s'", arg->op.op);
  1965. ret = 0;
  1966. }
  1967. break;
  1968. case '-':
  1969. /* check for negative */
  1970. if (arg->op.left->type == PRINT_NULL)
  1971. left = 0;
  1972. else
  1973. ret = arg_num_eval(arg->op.left, &left);
  1974. if (!ret)
  1975. break;
  1976. ret = arg_num_eval(arg->op.right, &right);
  1977. if (!ret)
  1978. break;
  1979. *val = left - right;
  1980. break;
  1981. case '+':
  1982. if (arg->op.left->type == PRINT_NULL)
  1983. left = 0;
  1984. else
  1985. ret = arg_num_eval(arg->op.left, &left);
  1986. if (!ret)
  1987. break;
  1988. ret = arg_num_eval(arg->op.right, &right);
  1989. if (!ret)
  1990. break;
  1991. *val = left + right;
  1992. break;
  1993. case '~':
  1994. ret = arg_num_eval(arg->op.right, &right);
  1995. if (!ret)
  1996. break;
  1997. *val = ~right;
  1998. break;
  1999. default:
  2000. do_warning("unknown op '%s'", arg->op.op);
  2001. ret = 0;
  2002. }
  2003. break;
  2004. case PRINT_NULL:
  2005. case PRINT_FIELD ... PRINT_SYMBOL:
  2006. case PRINT_STRING:
  2007. case PRINT_BSTRING:
  2008. case PRINT_BITMASK:
  2009. default:
  2010. do_warning("invalid eval type %d", arg->type);
  2011. ret = 0;
  2012. }
  2013. return ret;
  2014. }
  2015. static char *arg_eval (struct print_arg *arg)
  2016. {
  2017. long long val;
  2018. static char buf[20];
  2019. switch (arg->type) {
  2020. case PRINT_ATOM:
  2021. return arg->atom.atom;
  2022. case PRINT_TYPE:
  2023. return arg_eval(arg->typecast.item);
  2024. case PRINT_OP:
  2025. if (!arg_num_eval(arg, &val))
  2026. break;
  2027. sprintf(buf, "%lld", val);
  2028. return buf;
  2029. case PRINT_NULL:
  2030. case PRINT_FIELD ... PRINT_SYMBOL:
  2031. case PRINT_STRING:
  2032. case PRINT_BSTRING:
  2033. case PRINT_BITMASK:
  2034. default:
  2035. do_warning("invalid eval type %d", arg->type);
  2036. break;
  2037. }
  2038. return NULL;
  2039. }
  2040. static enum event_type
  2041. process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
  2042. {
  2043. enum event_type type;
  2044. struct print_arg *arg = NULL;
  2045. struct print_flag_sym *field;
  2046. char *token = *tok;
  2047. char *value;
  2048. do {
  2049. free_token(token);
  2050. type = read_token_item(&token);
  2051. if (test_type_token(type, token, EVENT_OP, "{"))
  2052. break;
  2053. arg = alloc_arg();
  2054. if (!arg)
  2055. goto out_free;
  2056. free_token(token);
  2057. type = process_arg(event, arg, &token);
  2058. if (type == EVENT_OP)
  2059. type = process_op(event, arg, &token);
  2060. if (type == EVENT_ERROR)
  2061. goto out_free;
  2062. if (test_type_token(type, token, EVENT_DELIM, ","))
  2063. goto out_free;
  2064. field = calloc(1, sizeof(*field));
  2065. if (!field)
  2066. goto out_free;
  2067. value = arg_eval(arg);
  2068. if (value == NULL)
  2069. goto out_free_field;
  2070. field->value = strdup(value);
  2071. if (field->value == NULL)
  2072. goto out_free_field;
  2073. free_arg(arg);
  2074. arg = alloc_arg();
  2075. if (!arg)
  2076. goto out_free;
  2077. free_token(token);
  2078. type = process_arg(event, arg, &token);
  2079. if (test_type_token(type, token, EVENT_OP, "}"))
  2080. goto out_free_field;
  2081. value = arg_eval(arg);
  2082. if (value == NULL)
  2083. goto out_free_field;
  2084. field->str = strdup(value);
  2085. if (field->str == NULL)
  2086. goto out_free_field;
  2087. free_arg(arg);
  2088. arg = NULL;
  2089. *list = field;
  2090. list = &field->next;
  2091. free_token(token);
  2092. type = read_token_item(&token);
  2093. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  2094. *tok = token;
  2095. return type;
  2096. out_free_field:
  2097. free_flag_sym(field);
  2098. out_free:
  2099. free_arg(arg);
  2100. free_token(token);
  2101. *tok = NULL;
  2102. return EVENT_ERROR;
  2103. }
  2104. static enum event_type
  2105. process_flags(struct event_format *event, struct print_arg *arg, char **tok)
  2106. {
  2107. struct print_arg *field;
  2108. enum event_type type;
  2109. char *token = NULL;
  2110. memset(arg, 0, sizeof(*arg));
  2111. arg->type = PRINT_FLAGS;
  2112. field = alloc_arg();
  2113. if (!field) {
  2114. do_warning_event(event, "%s: not enough memory!", __func__);
  2115. goto out_free;
  2116. }
  2117. type = process_field_arg(event, field, &token);
  2118. /* Handle operations in the first argument */
  2119. while (type == EVENT_OP)
  2120. type = process_op(event, field, &token);
  2121. if (test_type_token(type, token, EVENT_DELIM, ","))
  2122. goto out_free_field;
  2123. free_token(token);
  2124. arg->flags.field = field;
  2125. type = read_token_item(&token);
  2126. if (event_item_type(type)) {
  2127. arg->flags.delim = token;
  2128. type = read_token_item(&token);
  2129. }
  2130. if (test_type_token(type, token, EVENT_DELIM, ","))
  2131. goto out_free;
  2132. type = process_fields(event, &arg->flags.flags, &token);
  2133. if (test_type_token(type, token, EVENT_DELIM, ")"))
  2134. goto out_free;
  2135. free_token(token);
  2136. type = read_token_item(tok);
  2137. return type;
  2138. out_free_field:
  2139. free_arg(field);
  2140. out_free:
  2141. free_token(token);
  2142. *tok = NULL;
  2143. return EVENT_ERROR;
  2144. }
  2145. static enum event_type
  2146. process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
  2147. {
  2148. struct print_arg *field;
  2149. enum event_type type;
  2150. char *token = NULL;
  2151. memset(arg, 0, sizeof(*arg));
  2152. arg->type = PRINT_SYMBOL;
  2153. field = alloc_arg();
  2154. if (!field) {
  2155. do_warning_event(event, "%s: not enough memory!", __func__);
  2156. goto out_free;
  2157. }
  2158. type = process_field_arg(event, field, &token);
  2159. if (test_type_token(type, token, EVENT_DELIM, ","))
  2160. goto out_free_field;
  2161. arg->symbol.field = field;
  2162. type = process_fields(event, &arg->symbol.symbols, &token);
  2163. if (test_type_token(type, token, EVENT_DELIM, ")"))
  2164. goto out_free;
  2165. free_token(token);
  2166. type = read_token_item(tok);
  2167. return type;
  2168. out_free_field:
  2169. free_arg(field);
  2170. out_free:
  2171. free_token(token);
  2172. *tok = NULL;
  2173. return EVENT_ERROR;
  2174. }
  2175. static enum event_type
  2176. process_hex(struct event_format *event, struct print_arg *arg, char **tok)
  2177. {
  2178. memset(arg, 0, sizeof(*arg));
  2179. arg->type = PRINT_HEX;
  2180. if (alloc_and_process_delim(event, ",", &arg->hex.field))
  2181. goto out;
  2182. if (alloc_and_process_delim(event, ")", &arg->hex.size))
  2183. goto free_field;
  2184. return read_token_item(tok);
  2185. free_field:
  2186. free_arg(arg->hex.field);
  2187. arg->hex.field = NULL;
  2188. out:
  2189. *tok = NULL;
  2190. return EVENT_ERROR;
  2191. }
  2192. static enum event_type
  2193. process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
  2194. {
  2195. memset(arg, 0, sizeof(*arg));
  2196. arg->type = PRINT_INT_ARRAY;
  2197. if (alloc_and_process_delim(event, ",", &arg->int_array.field))
  2198. goto out;
  2199. if (alloc_and_process_delim(event, ",", &arg->int_array.count))
  2200. goto free_field;
  2201. if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
  2202. goto free_size;
  2203. return read_token_item(tok);
  2204. free_size:
  2205. free_arg(arg->int_array.count);
  2206. arg->int_array.count = NULL;
  2207. free_field:
  2208. free_arg(arg->int_array.field);
  2209. arg->int_array.field = NULL;
  2210. out:
  2211. *tok = NULL;
  2212. return EVENT_ERROR;
  2213. }
  2214. static enum event_type
  2215. process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
  2216. {
  2217. struct format_field *field;
  2218. enum event_type type;
  2219. char *token;
  2220. memset(arg, 0, sizeof(*arg));
  2221. arg->type = PRINT_DYNAMIC_ARRAY;
  2222. /*
  2223. * The item within the parenthesis is another field that holds
  2224. * the index into where the array starts.
  2225. */
  2226. type = read_token(&token);
  2227. *tok = token;
  2228. if (type != EVENT_ITEM)
  2229. goto out_free;
  2230. /* Find the field */
  2231. field = pevent_find_field(event, token);
  2232. if (!field)
  2233. goto out_free;
  2234. arg->dynarray.field = field;
  2235. arg->dynarray.index = 0;
  2236. if (read_expected(EVENT_DELIM, ")") < 0)
  2237. goto out_free;
  2238. free_token(token);
  2239. type = read_token_item(&token);
  2240. *tok = token;
  2241. if (type != EVENT_OP || strcmp(token, "[") != 0)
  2242. return type;
  2243. free_token(token);
  2244. arg = alloc_arg();
  2245. if (!arg) {
  2246. do_warning_event(event, "%s: not enough memory!", __func__);
  2247. *tok = NULL;
  2248. return EVENT_ERROR;
  2249. }
  2250. type = process_arg(event, arg, &token);
  2251. if (type == EVENT_ERROR)
  2252. goto out_free_arg;
  2253. if (!test_type_token(type, token, EVENT_OP, "]"))
  2254. goto out_free_arg;
  2255. free_token(token);
  2256. type = read_token_item(tok);
  2257. return type;
  2258. out_free_arg:
  2259. free_arg(arg);
  2260. out_free:
  2261. free_token(token);
  2262. *tok = NULL;
  2263. return EVENT_ERROR;
  2264. }
  2265. static enum event_type
  2266. process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
  2267. char **tok)
  2268. {
  2269. struct format_field *field;
  2270. enum event_type type;
  2271. char *token;
  2272. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2273. goto out_free;
  2274. arg->type = PRINT_DYNAMIC_ARRAY_LEN;
  2275. /* Find the field */
  2276. field = pevent_find_field(event, token);
  2277. if (!field)
  2278. goto out_free;
  2279. arg->dynarray.field = field;
  2280. arg->dynarray.index = 0;
  2281. if (read_expected(EVENT_DELIM, ")") < 0)
  2282. goto out_err;
  2283. type = read_token(&token);
  2284. *tok = token;
  2285. return type;
  2286. out_free:
  2287. free_token(token);
  2288. out_err:
  2289. *tok = NULL;
  2290. return EVENT_ERROR;
  2291. }
  2292. static enum event_type
  2293. process_paren(struct event_format *event, struct print_arg *arg, char **tok)
  2294. {
  2295. struct print_arg *item_arg;
  2296. enum event_type type;
  2297. char *token;
  2298. type = process_arg(event, arg, &token);
  2299. if (type == EVENT_ERROR)
  2300. goto out_free;
  2301. if (type == EVENT_OP)
  2302. type = process_op(event, arg, &token);
  2303. if (type == EVENT_ERROR)
  2304. goto out_free;
  2305. if (test_type_token(type, token, EVENT_DELIM, ")"))
  2306. goto out_free;
  2307. free_token(token);
  2308. type = read_token_item(&token);
  2309. /*
  2310. * If the next token is an item or another open paren, then
  2311. * this was a typecast.
  2312. */
  2313. if (event_item_type(type) ||
  2314. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  2315. /* make this a typecast and contine */
  2316. /* prevous must be an atom */
  2317. if (arg->type != PRINT_ATOM) {
  2318. do_warning_event(event, "previous needed to be PRINT_ATOM");
  2319. goto out_free;
  2320. }
  2321. item_arg = alloc_arg();
  2322. if (!item_arg) {
  2323. do_warning_event(event, "%s: not enough memory!",
  2324. __func__);
  2325. goto out_free;
  2326. }
  2327. arg->type = PRINT_TYPE;
  2328. arg->typecast.type = arg->atom.atom;
  2329. arg->typecast.item = item_arg;
  2330. type = process_arg_token(event, item_arg, &token, type);
  2331. }
  2332. *tok = token;
  2333. return type;
  2334. out_free:
  2335. free_token(token);
  2336. *tok = NULL;
  2337. return EVENT_ERROR;
  2338. }
  2339. static enum event_type
  2340. process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
  2341. char **tok)
  2342. {
  2343. enum event_type type;
  2344. char *token;
  2345. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2346. goto out_free;
  2347. arg->type = PRINT_STRING;
  2348. arg->string.string = token;
  2349. arg->string.offset = -1;
  2350. if (read_expected(EVENT_DELIM, ")") < 0)
  2351. goto out_err;
  2352. type = read_token(&token);
  2353. *tok = token;
  2354. return type;
  2355. out_free:
  2356. free_token(token);
  2357. out_err:
  2358. *tok = NULL;
  2359. return EVENT_ERROR;
  2360. }
  2361. static enum event_type
  2362. process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
  2363. char **tok)
  2364. {
  2365. enum event_type type;
  2366. char *token;
  2367. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2368. goto out_free;
  2369. arg->type = PRINT_BITMASK;
  2370. arg->bitmask.bitmask = token;
  2371. arg->bitmask.offset = -1;
  2372. if (read_expected(EVENT_DELIM, ")") < 0)
  2373. goto out_err;
  2374. type = read_token(&token);
  2375. *tok = token;
  2376. return type;
  2377. out_free:
  2378. free_token(token);
  2379. out_err:
  2380. *tok = NULL;
  2381. return EVENT_ERROR;
  2382. }
  2383. static struct pevent_function_handler *
  2384. find_func_handler(struct pevent *pevent, char *func_name)
  2385. {
  2386. struct pevent_function_handler *func;
  2387. if (!pevent)
  2388. return NULL;
  2389. for (func = pevent->func_handlers; func; func = func->next) {
  2390. if (strcmp(func->name, func_name) == 0)
  2391. break;
  2392. }
  2393. return func;
  2394. }
  2395. static void remove_func_handler(struct pevent *pevent, char *func_name)
  2396. {
  2397. struct pevent_function_handler *func;
  2398. struct pevent_function_handler **next;
  2399. next = &pevent->func_handlers;
  2400. while ((func = *next)) {
  2401. if (strcmp(func->name, func_name) == 0) {
  2402. *next = func->next;
  2403. free_func_handle(func);
  2404. break;
  2405. }
  2406. next = &func->next;
  2407. }
  2408. }
  2409. static enum event_type
  2410. process_func_handler(struct event_format *event, struct pevent_function_handler *func,
  2411. struct print_arg *arg, char **tok)
  2412. {
  2413. struct print_arg **next_arg;
  2414. struct print_arg *farg;
  2415. enum event_type type;
  2416. char *token;
  2417. int i;
  2418. arg->type = PRINT_FUNC;
  2419. arg->func.func = func;
  2420. *tok = NULL;
  2421. next_arg = &(arg->func.args);
  2422. for (i = 0; i < func->nr_args; i++) {
  2423. farg = alloc_arg();
  2424. if (!farg) {
  2425. do_warning_event(event, "%s: not enough memory!",
  2426. __func__);
  2427. return EVENT_ERROR;
  2428. }
  2429. type = process_arg(event, farg, &token);
  2430. if (i < (func->nr_args - 1)) {
  2431. if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
  2432. do_warning_event(event,
  2433. "Error: function '%s()' expects %d arguments but event %s only uses %d",
  2434. func->name, func->nr_args,
  2435. event->name, i + 1);
  2436. goto err;
  2437. }
  2438. } else {
  2439. if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
  2440. do_warning_event(event,
  2441. "Error: function '%s()' only expects %d arguments but event %s has more",
  2442. func->name, func->nr_args, event->name);
  2443. goto err;
  2444. }
  2445. }
  2446. *next_arg = farg;
  2447. next_arg = &(farg->next);
  2448. free_token(token);
  2449. }
  2450. type = read_token(&token);
  2451. *tok = token;
  2452. return type;
  2453. err:
  2454. free_arg(farg);
  2455. free_token(token);
  2456. return EVENT_ERROR;
  2457. }
  2458. static enum event_type
  2459. process_function(struct event_format *event, struct print_arg *arg,
  2460. char *token, char **tok)
  2461. {
  2462. struct pevent_function_handler *func;
  2463. if (strcmp(token, "__print_flags") == 0) {
  2464. free_token(token);
  2465. is_flag_field = 1;
  2466. return process_flags(event, arg, tok);
  2467. }
  2468. if (strcmp(token, "__print_symbolic") == 0) {
  2469. free_token(token);
  2470. is_symbolic_field = 1;
  2471. return process_symbols(event, arg, tok);
  2472. }
  2473. if (strcmp(token, "__print_hex") == 0) {
  2474. free_token(token);
  2475. return process_hex(event, arg, tok);
  2476. }
  2477. if (strcmp(token, "__print_array") == 0) {
  2478. free_token(token);
  2479. return process_int_array(event, arg, tok);
  2480. }
  2481. if (strcmp(token, "__get_str") == 0) {
  2482. free_token(token);
  2483. return process_str(event, arg, tok);
  2484. }
  2485. if (strcmp(token, "__get_bitmask") == 0) {
  2486. free_token(token);
  2487. return process_bitmask(event, arg, tok);
  2488. }
  2489. if (strcmp(token, "__get_dynamic_array") == 0) {
  2490. free_token(token);
  2491. return process_dynamic_array(event, arg, tok);
  2492. }
  2493. if (strcmp(token, "__get_dynamic_array_len") == 0) {
  2494. free_token(token);
  2495. return process_dynamic_array_len(event, arg, tok);
  2496. }
  2497. func = find_func_handler(event->pevent, token);
  2498. if (func) {
  2499. free_token(token);
  2500. return process_func_handler(event, func, arg, tok);
  2501. }
  2502. do_warning_event(event, "function %s not defined", token);
  2503. free_token(token);
  2504. return EVENT_ERROR;
  2505. }
  2506. static enum event_type
  2507. process_arg_token(struct event_format *event, struct print_arg *arg,
  2508. char **tok, enum event_type type)
  2509. {
  2510. char *token;
  2511. char *atom;
  2512. token = *tok;
  2513. switch (type) {
  2514. case EVENT_ITEM:
  2515. if (strcmp(token, "REC") == 0) {
  2516. free_token(token);
  2517. type = process_entry(event, arg, &token);
  2518. break;
  2519. }
  2520. atom = token;
  2521. /* test the next token */
  2522. type = read_token_item(&token);
  2523. /*
  2524. * If the next token is a parenthesis, then this
  2525. * is a function.
  2526. */
  2527. if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
  2528. free_token(token);
  2529. token = NULL;
  2530. /* this will free atom. */
  2531. type = process_function(event, arg, atom, &token);
  2532. break;
  2533. }
  2534. /* atoms can be more than one token long */
  2535. while (type == EVENT_ITEM) {
  2536. char *new_atom;
  2537. new_atom = realloc(atom,
  2538. strlen(atom) + strlen(token) + 2);
  2539. if (!new_atom) {
  2540. free(atom);
  2541. *tok = NULL;
  2542. free_token(token);
  2543. return EVENT_ERROR;
  2544. }
  2545. atom = new_atom;
  2546. strcat(atom, " ");
  2547. strcat(atom, token);
  2548. free_token(token);
  2549. type = read_token_item(&token);
  2550. }
  2551. arg->type = PRINT_ATOM;
  2552. arg->atom.atom = atom;
  2553. break;
  2554. case EVENT_DQUOTE:
  2555. case EVENT_SQUOTE:
  2556. arg->type = PRINT_ATOM;
  2557. arg->atom.atom = token;
  2558. type = read_token_item(&token);
  2559. break;
  2560. case EVENT_DELIM:
  2561. if (strcmp(token, "(") == 0) {
  2562. free_token(token);
  2563. type = process_paren(event, arg, &token);
  2564. break;
  2565. }
  2566. case EVENT_OP:
  2567. /* handle single ops */
  2568. arg->type = PRINT_OP;
  2569. arg->op.op = token;
  2570. arg->op.left = NULL;
  2571. type = process_op(event, arg, &token);
  2572. /* On error, the op is freed */
  2573. if (type == EVENT_ERROR)
  2574. arg->op.op = NULL;
  2575. /* return error type if errored */
  2576. break;
  2577. case EVENT_ERROR ... EVENT_NEWLINE:
  2578. default:
  2579. do_warning_event(event, "unexpected type %d", type);
  2580. return EVENT_ERROR;
  2581. }
  2582. *tok = token;
  2583. return type;
  2584. }
  2585. static int event_read_print_args(struct event_format *event, struct print_arg **list)
  2586. {
  2587. enum event_type type = EVENT_ERROR;
  2588. struct print_arg *arg;
  2589. char *token;
  2590. int args = 0;
  2591. do {
  2592. if (type == EVENT_NEWLINE) {
  2593. type = read_token_item(&token);
  2594. continue;
  2595. }
  2596. arg = alloc_arg();
  2597. if (!arg) {
  2598. do_warning_event(event, "%s: not enough memory!",
  2599. __func__);
  2600. return -1;
  2601. }
  2602. type = process_arg(event, arg, &token);
  2603. if (type == EVENT_ERROR) {
  2604. free_token(token);
  2605. free_arg(arg);
  2606. return -1;
  2607. }
  2608. *list = arg;
  2609. args++;
  2610. if (type == EVENT_OP) {
  2611. type = process_op(event, arg, &token);
  2612. free_token(token);
  2613. if (type == EVENT_ERROR) {
  2614. *list = NULL;
  2615. free_arg(arg);
  2616. return -1;
  2617. }
  2618. list = &arg->next;
  2619. continue;
  2620. }
  2621. if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
  2622. free_token(token);
  2623. *list = arg;
  2624. list = &arg->next;
  2625. continue;
  2626. }
  2627. break;
  2628. } while (type != EVENT_NONE);
  2629. if (type != EVENT_NONE && type != EVENT_ERROR)
  2630. free_token(token);
  2631. return args;
  2632. }
  2633. static int event_read_print(struct event_format *event)
  2634. {
  2635. enum event_type type;
  2636. char *token;
  2637. int ret;
  2638. if (read_expected_item(EVENT_ITEM, "print") < 0)
  2639. return -1;
  2640. if (read_expected(EVENT_ITEM, "fmt") < 0)
  2641. return -1;
  2642. if (read_expected(EVENT_OP, ":") < 0)
  2643. return -1;
  2644. if (read_expect_type(EVENT_DQUOTE, &token) < 0)
  2645. goto fail;
  2646. concat:
  2647. event->print_fmt.format = token;
  2648. event->print_fmt.args = NULL;
  2649. /* ok to have no arg */
  2650. type = read_token_item(&token);
  2651. if (type == EVENT_NONE)
  2652. return 0;
  2653. /* Handle concatenation of print lines */
  2654. if (type == EVENT_DQUOTE) {
  2655. char *cat;
  2656. if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
  2657. goto fail;
  2658. free_token(token);
  2659. free_token(event->print_fmt.format);
  2660. event->print_fmt.format = NULL;
  2661. token = cat;
  2662. goto concat;
  2663. }
  2664. if (test_type_token(type, token, EVENT_DELIM, ","))
  2665. goto fail;
  2666. free_token(token);
  2667. ret = event_read_print_args(event, &event->print_fmt.args);
  2668. if (ret < 0)
  2669. return -1;
  2670. return ret;
  2671. fail:
  2672. free_token(token);
  2673. return -1;
  2674. }
  2675. /**
  2676. * pevent_find_common_field - return a common field by event
  2677. * @event: handle for the event
  2678. * @name: the name of the common field to return
  2679. *
  2680. * Returns a common field from the event by the given @name.
  2681. * This only searchs the common fields and not all field.
  2682. */
  2683. struct format_field *
  2684. pevent_find_common_field(struct event_format *event, const char *name)
  2685. {
  2686. struct format_field *format;
  2687. for (format = event->format.common_fields;
  2688. format; format = format->next) {
  2689. if (strcmp(format->name, name) == 0)
  2690. break;
  2691. }
  2692. return format;
  2693. }
  2694. /**
  2695. * pevent_find_field - find a non-common field
  2696. * @event: handle for the event
  2697. * @name: the name of the non-common field
  2698. *
  2699. * Returns a non-common field by the given @name.
  2700. * This does not search common fields.
  2701. */
  2702. struct format_field *
  2703. pevent_find_field(struct event_format *event, const char *name)
  2704. {
  2705. struct format_field *format;
  2706. for (format = event->format.fields;
  2707. format; format = format->next) {
  2708. if (strcmp(format->name, name) == 0)
  2709. break;
  2710. }
  2711. return format;
  2712. }
  2713. /**
  2714. * pevent_find_any_field - find any field by name
  2715. * @event: handle for the event
  2716. * @name: the name of the field
  2717. *
  2718. * Returns a field by the given @name.
  2719. * This searchs the common field names first, then
  2720. * the non-common ones if a common one was not found.
  2721. */
  2722. struct format_field *
  2723. pevent_find_any_field(struct event_format *event, const char *name)
  2724. {
  2725. struct format_field *format;
  2726. format = pevent_find_common_field(event, name);
  2727. if (format)
  2728. return format;
  2729. return pevent_find_field(event, name);
  2730. }
  2731. /**
  2732. * pevent_read_number - read a number from data
  2733. * @pevent: handle for the pevent
  2734. * @ptr: the raw data
  2735. * @size: the size of the data that holds the number
  2736. *
  2737. * Returns the number (converted to host) from the
  2738. * raw data.
  2739. */
  2740. unsigned long long pevent_read_number(struct pevent *pevent,
  2741. const void *ptr, int size)
  2742. {
  2743. switch (size) {
  2744. case 1:
  2745. return *(unsigned char *)ptr;
  2746. case 2:
  2747. return data2host2(pevent, ptr);
  2748. case 4:
  2749. return data2host4(pevent, ptr);
  2750. case 8:
  2751. return data2host8(pevent, ptr);
  2752. default:
  2753. /* BUG! */
  2754. return 0;
  2755. }
  2756. }
  2757. /**
  2758. * pevent_read_number_field - read a number from data
  2759. * @field: a handle to the field
  2760. * @data: the raw data to read
  2761. * @value: the value to place the number in
  2762. *
  2763. * Reads raw data according to a field offset and size,
  2764. * and translates it into @value.
  2765. *
  2766. * Returns 0 on success, -1 otherwise.
  2767. */
  2768. int pevent_read_number_field(struct format_field *field, const void *data,
  2769. unsigned long long *value)
  2770. {
  2771. if (!field)
  2772. return -1;
  2773. switch (field->size) {
  2774. case 1:
  2775. case 2:
  2776. case 4:
  2777. case 8:
  2778. *value = pevent_read_number(field->event->pevent,
  2779. data + field->offset, field->size);
  2780. return 0;
  2781. default:
  2782. return -1;
  2783. }
  2784. }
  2785. static int get_common_info(struct pevent *pevent,
  2786. const char *type, int *offset, int *size)
  2787. {
  2788. struct event_format *event;
  2789. struct format_field *field;
  2790. /*
  2791. * All events should have the same common elements.
  2792. * Pick any event to find where the type is;
  2793. */
  2794. if (!pevent->events) {
  2795. do_warning("no event_list!");
  2796. return -1;
  2797. }
  2798. event = pevent->events[0];
  2799. field = pevent_find_common_field(event, type);
  2800. if (!field)
  2801. return -1;
  2802. *offset = field->offset;
  2803. *size = field->size;
  2804. return 0;
  2805. }
  2806. static int __parse_common(struct pevent *pevent, void *data,
  2807. int *size, int *offset, const char *name)
  2808. {
  2809. int ret;
  2810. if (!*size) {
  2811. ret = get_common_info(pevent, name, offset, size);
  2812. if (ret < 0)
  2813. return ret;
  2814. }
  2815. return pevent_read_number(pevent, data + *offset, *size);
  2816. }
  2817. static int trace_parse_common_type(struct pevent *pevent, void *data)
  2818. {
  2819. return __parse_common(pevent, data,
  2820. &pevent->type_size, &pevent->type_offset,
  2821. "common_type");
  2822. }
  2823. static int parse_common_pid(struct pevent *pevent, void *data)
  2824. {
  2825. return __parse_common(pevent, data,
  2826. &pevent->pid_size, &pevent->pid_offset,
  2827. "common_pid");
  2828. }
  2829. static int parse_common_pc(struct pevent *pevent, void *data)
  2830. {
  2831. return __parse_common(pevent, data,
  2832. &pevent->pc_size, &pevent->pc_offset,
  2833. "common_preempt_count");
  2834. }
  2835. static int parse_common_flags(struct pevent *pevent, void *data)
  2836. {
  2837. return __parse_common(pevent, data,
  2838. &pevent->flags_size, &pevent->flags_offset,
  2839. "common_flags");
  2840. }
  2841. static int parse_common_lock_depth(struct pevent *pevent, void *data)
  2842. {
  2843. return __parse_common(pevent, data,
  2844. &pevent->ld_size, &pevent->ld_offset,
  2845. "common_lock_depth");
  2846. }
  2847. static int parse_common_migrate_disable(struct pevent *pevent, void *data)
  2848. {
  2849. return __parse_common(pevent, data,
  2850. &pevent->ld_size, &pevent->ld_offset,
  2851. "common_migrate_disable");
  2852. }
  2853. static int events_id_cmp(const void *a, const void *b);
  2854. /**
  2855. * pevent_find_event - find an event by given id
  2856. * @pevent: a handle to the pevent
  2857. * @id: the id of the event
  2858. *
  2859. * Returns an event that has a given @id.
  2860. */
  2861. struct event_format *pevent_find_event(struct pevent *pevent, int id)
  2862. {
  2863. struct event_format **eventptr;
  2864. struct event_format key;
  2865. struct event_format *pkey = &key;
  2866. /* Check cache first */
  2867. if (pevent->last_event && pevent->last_event->id == id)
  2868. return pevent->last_event;
  2869. key.id = id;
  2870. eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
  2871. sizeof(*pevent->events), events_id_cmp);
  2872. if (eventptr) {
  2873. pevent->last_event = *eventptr;
  2874. return *eventptr;
  2875. }
  2876. return NULL;
  2877. }
  2878. /**
  2879. * pevent_find_event_by_name - find an event by given name
  2880. * @pevent: a handle to the pevent
  2881. * @sys: the system name to search for
  2882. * @name: the name of the event to search for
  2883. *
  2884. * This returns an event with a given @name and under the system
  2885. * @sys. If @sys is NULL the first event with @name is returned.
  2886. */
  2887. struct event_format *
  2888. pevent_find_event_by_name(struct pevent *pevent,
  2889. const char *sys, const char *name)
  2890. {
  2891. struct event_format *event;
  2892. int i;
  2893. if (pevent->last_event &&
  2894. strcmp(pevent->last_event->name, name) == 0 &&
  2895. (!sys || strcmp(pevent->last_event->system, sys) == 0))
  2896. return pevent->last_event;
  2897. for (i = 0; i < pevent->nr_events; i++) {
  2898. event = pevent->events[i];
  2899. if (strcmp(event->name, name) == 0) {
  2900. if (!sys)
  2901. break;
  2902. if (strcmp(event->system, sys) == 0)
  2903. break;
  2904. }
  2905. }
  2906. if (i == pevent->nr_events)
  2907. event = NULL;
  2908. pevent->last_event = event;
  2909. return event;
  2910. }
  2911. static unsigned long long
  2912. eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
  2913. {
  2914. struct pevent *pevent = event->pevent;
  2915. unsigned long long val = 0;
  2916. unsigned long long left, right;
  2917. struct print_arg *typearg = NULL;
  2918. struct print_arg *larg;
  2919. unsigned long offset;
  2920. unsigned int field_size;
  2921. switch (arg->type) {
  2922. case PRINT_NULL:
  2923. /* ?? */
  2924. return 0;
  2925. case PRINT_ATOM:
  2926. return strtoull(arg->atom.atom, NULL, 0);
  2927. case PRINT_FIELD:
  2928. if (!arg->field.field) {
  2929. arg->field.field = pevent_find_any_field(event, arg->field.name);
  2930. if (!arg->field.field)
  2931. goto out_warning_field;
  2932. }
  2933. /* must be a number */
  2934. val = pevent_read_number(pevent, data + arg->field.field->offset,
  2935. arg->field.field->size);
  2936. break;
  2937. case PRINT_FLAGS:
  2938. case PRINT_SYMBOL:
  2939. case PRINT_INT_ARRAY:
  2940. case PRINT_HEX:
  2941. break;
  2942. case PRINT_TYPE:
  2943. val = eval_num_arg(data, size, event, arg->typecast.item);
  2944. return eval_type(val, arg, 0);
  2945. case PRINT_STRING:
  2946. case PRINT_BSTRING:
  2947. case PRINT_BITMASK:
  2948. return 0;
  2949. case PRINT_FUNC: {
  2950. struct trace_seq s;
  2951. trace_seq_init(&s);
  2952. val = process_defined_func(&s, data, size, event, arg);
  2953. trace_seq_destroy(&s);
  2954. return val;
  2955. }
  2956. case PRINT_OP:
  2957. if (strcmp(arg->op.op, "[") == 0) {
  2958. /*
  2959. * Arrays are special, since we don't want
  2960. * to read the arg as is.
  2961. */
  2962. right = eval_num_arg(data, size, event, arg->op.right);
  2963. /* handle typecasts */
  2964. larg = arg->op.left;
  2965. while (larg->type == PRINT_TYPE) {
  2966. if (!typearg)
  2967. typearg = larg;
  2968. larg = larg->typecast.item;
  2969. }
  2970. /* Default to long size */
  2971. field_size = pevent->long_size;
  2972. switch (larg->type) {
  2973. case PRINT_DYNAMIC_ARRAY:
  2974. offset = pevent_read_number(pevent,
  2975. data + larg->dynarray.field->offset,
  2976. larg->dynarray.field->size);
  2977. if (larg->dynarray.field->elementsize)
  2978. field_size = larg->dynarray.field->elementsize;
  2979. /*
  2980. * The actual length of the dynamic array is stored
  2981. * in the top half of the field, and the offset
  2982. * is in the bottom half of the 32 bit field.
  2983. */
  2984. offset &= 0xffff;
  2985. offset += right;
  2986. break;
  2987. case PRINT_FIELD:
  2988. if (!larg->field.field) {
  2989. larg->field.field =
  2990. pevent_find_any_field(event, larg->field.name);
  2991. if (!larg->field.field) {
  2992. arg = larg;
  2993. goto out_warning_field;
  2994. }
  2995. }
  2996. field_size = larg->field.field->elementsize;
  2997. offset = larg->field.field->offset +
  2998. right * larg->field.field->elementsize;
  2999. break;
  3000. default:
  3001. goto default_op; /* oops, all bets off */
  3002. }
  3003. val = pevent_read_number(pevent,
  3004. data + offset, field_size);
  3005. if (typearg)
  3006. val = eval_type(val, typearg, 1);
  3007. break;
  3008. } else if (strcmp(arg->op.op, "?") == 0) {
  3009. left = eval_num_arg(data, size, event, arg->op.left);
  3010. arg = arg->op.right;
  3011. if (left)
  3012. val = eval_num_arg(data, size, event, arg->op.left);
  3013. else
  3014. val = eval_num_arg(data, size, event, arg->op.right);
  3015. break;
  3016. }
  3017. default_op:
  3018. left = eval_num_arg(data, size, event, arg->op.left);
  3019. right = eval_num_arg(data, size, event, arg->op.right);
  3020. switch (arg->op.op[0]) {
  3021. case '!':
  3022. switch (arg->op.op[1]) {
  3023. case 0:
  3024. val = !right;
  3025. break;
  3026. case '=':
  3027. val = left != right;
  3028. break;
  3029. default:
  3030. goto out_warning_op;
  3031. }
  3032. break;
  3033. case '~':
  3034. val = ~right;
  3035. break;
  3036. case '|':
  3037. if (arg->op.op[1])
  3038. val = left || right;
  3039. else
  3040. val = left | right;
  3041. break;
  3042. case '&':
  3043. if (arg->op.op[1])
  3044. val = left && right;
  3045. else
  3046. val = left & right;
  3047. break;
  3048. case '<':
  3049. switch (arg->op.op[1]) {
  3050. case 0:
  3051. val = left < right;
  3052. break;
  3053. case '<':
  3054. val = left << right;
  3055. break;
  3056. case '=':
  3057. val = left <= right;
  3058. break;
  3059. default:
  3060. goto out_warning_op;
  3061. }
  3062. break;
  3063. case '>':
  3064. switch (arg->op.op[1]) {
  3065. case 0:
  3066. val = left > right;
  3067. break;
  3068. case '>':
  3069. val = left >> right;
  3070. break;
  3071. case '=':
  3072. val = left >= right;
  3073. break;
  3074. default:
  3075. goto out_warning_op;
  3076. }
  3077. break;
  3078. case '=':
  3079. if (arg->op.op[1] != '=')
  3080. goto out_warning_op;
  3081. val = left == right;
  3082. break;
  3083. case '-':
  3084. val = left - right;
  3085. break;
  3086. case '+':
  3087. val = left + right;
  3088. break;
  3089. case '/':
  3090. val = left / right;
  3091. break;
  3092. case '%':
  3093. val = left % right;
  3094. break;
  3095. case '*':
  3096. val = left * right;
  3097. break;
  3098. default:
  3099. goto out_warning_op;
  3100. }
  3101. break;
  3102. case PRINT_DYNAMIC_ARRAY_LEN:
  3103. offset = pevent_read_number(pevent,
  3104. data + arg->dynarray.field->offset,
  3105. arg->dynarray.field->size);
  3106. /*
  3107. * The total allocated length of the dynamic array is
  3108. * stored in the top half of the field, and the offset
  3109. * is in the bottom half of the 32 bit field.
  3110. */
  3111. val = (unsigned long long)(offset >> 16);
  3112. break;
  3113. case PRINT_DYNAMIC_ARRAY:
  3114. /* Without [], we pass the address to the dynamic data */
  3115. offset = pevent_read_number(pevent,
  3116. data + arg->dynarray.field->offset,
  3117. arg->dynarray.field->size);
  3118. /*
  3119. * The total allocated length of the dynamic array is
  3120. * stored in the top half of the field, and the offset
  3121. * is in the bottom half of the 32 bit field.
  3122. */
  3123. offset &= 0xffff;
  3124. val = (unsigned long long)((unsigned long)data + offset);
  3125. break;
  3126. default: /* not sure what to do there */
  3127. return 0;
  3128. }
  3129. return val;
  3130. out_warning_op:
  3131. do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
  3132. return 0;
  3133. out_warning_field:
  3134. do_warning_event(event, "%s: field %s not found",
  3135. __func__, arg->field.name);
  3136. return 0;
  3137. }
  3138. struct flag {
  3139. const char *name;
  3140. unsigned long long value;
  3141. };
  3142. static const struct flag flags[] = {
  3143. { "HI_SOFTIRQ", 0 },
  3144. { "TIMER_SOFTIRQ", 1 },
  3145. { "NET_TX_SOFTIRQ", 2 },
  3146. { "NET_RX_SOFTIRQ", 3 },
  3147. { "BLOCK_SOFTIRQ", 4 },
  3148. { "IRQ_POLL_SOFTIRQ", 5 },
  3149. { "TASKLET_SOFTIRQ", 6 },
  3150. { "SCHED_SOFTIRQ", 7 },
  3151. { "HRTIMER_SOFTIRQ", 8 },
  3152. { "RCU_SOFTIRQ", 9 },
  3153. { "HRTIMER_NORESTART", 0 },
  3154. { "HRTIMER_RESTART", 1 },
  3155. };
  3156. static long long eval_flag(const char *flag)
  3157. {
  3158. int i;
  3159. /*
  3160. * Some flags in the format files do not get converted.
  3161. * If the flag is not numeric, see if it is something that
  3162. * we already know about.
  3163. */
  3164. if (isdigit(flag[0]))
  3165. return strtoull(flag, NULL, 0);
  3166. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  3167. if (strcmp(flags[i].name, flag) == 0)
  3168. return flags[i].value;
  3169. return -1LL;
  3170. }
  3171. static void print_str_to_seq(struct trace_seq *s, const char *format,
  3172. int len_arg, const char *str)
  3173. {
  3174. if (len_arg >= 0)
  3175. trace_seq_printf(s, format, len_arg, str);
  3176. else
  3177. trace_seq_printf(s, format, str);
  3178. }
  3179. static void print_bitmask_to_seq(struct pevent *pevent,
  3180. struct trace_seq *s, const char *format,
  3181. int len_arg, const void *data, int size)
  3182. {
  3183. int nr_bits = size * 8;
  3184. int str_size = (nr_bits + 3) / 4;
  3185. int len = 0;
  3186. char buf[3];
  3187. char *str;
  3188. int index;
  3189. int i;
  3190. /*
  3191. * The kernel likes to put in commas every 32 bits, we
  3192. * can do the same.
  3193. */
  3194. str_size += (nr_bits - 1) / 32;
  3195. str = malloc(str_size + 1);
  3196. if (!str) {
  3197. do_warning("%s: not enough memory!", __func__);
  3198. return;
  3199. }
  3200. str[str_size] = 0;
  3201. /* Start out with -2 for the two chars per byte */
  3202. for (i = str_size - 2; i >= 0; i -= 2) {
  3203. /*
  3204. * data points to a bit mask of size bytes.
  3205. * In the kernel, this is an array of long words, thus
  3206. * endianess is very important.
  3207. */
  3208. if (pevent->file_bigendian)
  3209. index = size - (len + 1);
  3210. else
  3211. index = len;
  3212. snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
  3213. memcpy(str + i, buf, 2);
  3214. len++;
  3215. if (!(len & 3) && i > 0) {
  3216. i--;
  3217. str[i] = ',';
  3218. }
  3219. }
  3220. if (len_arg >= 0)
  3221. trace_seq_printf(s, format, len_arg, str);
  3222. else
  3223. trace_seq_printf(s, format, str);
  3224. free(str);
  3225. }
  3226. static void print_str_arg(struct trace_seq *s, void *data, int size,
  3227. struct event_format *event, const char *format,
  3228. int len_arg, struct print_arg *arg)
  3229. {
  3230. struct pevent *pevent = event->pevent;
  3231. struct print_flag_sym *flag;
  3232. struct format_field *field;
  3233. struct printk_map *printk;
  3234. long long val, fval;
  3235. unsigned long long addr;
  3236. char *str;
  3237. unsigned char *hex;
  3238. int print;
  3239. int i, len;
  3240. switch (arg->type) {
  3241. case PRINT_NULL:
  3242. /* ?? */
  3243. return;
  3244. case PRINT_ATOM:
  3245. print_str_to_seq(s, format, len_arg, arg->atom.atom);
  3246. return;
  3247. case PRINT_FIELD:
  3248. field = arg->field.field;
  3249. if (!field) {
  3250. field = pevent_find_any_field(event, arg->field.name);
  3251. if (!field) {
  3252. str = arg->field.name;
  3253. goto out_warning_field;
  3254. }
  3255. arg->field.field = field;
  3256. }
  3257. /* Zero sized fields, mean the rest of the data */
  3258. len = field->size ? : size - field->offset;
  3259. /*
  3260. * Some events pass in pointers. If this is not an array
  3261. * and the size is the same as long_size, assume that it
  3262. * is a pointer.
  3263. */
  3264. if (!(field->flags & FIELD_IS_ARRAY) &&
  3265. field->size == pevent->long_size) {
  3266. /* Handle heterogeneous recording and processing
  3267. * architectures
  3268. *
  3269. * CASE I:
  3270. * Traces recorded on 32-bit devices (32-bit
  3271. * addressing) and processed on 64-bit devices:
  3272. * In this case, only 32 bits should be read.
  3273. *
  3274. * CASE II:
  3275. * Traces recorded on 64 bit devices and processed
  3276. * on 32-bit devices:
  3277. * In this case, 64 bits must be read.
  3278. */
  3279. addr = (pevent->long_size == 8) ?
  3280. *(unsigned long long *)(data + field->offset) :
  3281. (unsigned long long)*(unsigned int *)(data + field->offset);
  3282. /* Check if it matches a print format */
  3283. printk = find_printk(pevent, addr);
  3284. if (printk)
  3285. trace_seq_puts(s, printk->printk);
  3286. else
  3287. trace_seq_printf(s, "%llx", addr);
  3288. break;
  3289. }
  3290. str = malloc(len + 1);
  3291. if (!str) {
  3292. do_warning_event(event, "%s: not enough memory!",
  3293. __func__);
  3294. return;
  3295. }
  3296. memcpy(str, data + field->offset, len);
  3297. str[len] = 0;
  3298. print_str_to_seq(s, format, len_arg, str);
  3299. free(str);
  3300. break;
  3301. case PRINT_FLAGS:
  3302. val = eval_num_arg(data, size, event, arg->flags.field);
  3303. print = 0;
  3304. for (flag = arg->flags.flags; flag; flag = flag->next) {
  3305. fval = eval_flag(flag->value);
  3306. if (!val && fval < 0) {
  3307. print_str_to_seq(s, format, len_arg, flag->str);
  3308. break;
  3309. }
  3310. if (fval > 0 && (val & fval) == fval) {
  3311. if (print && arg->flags.delim)
  3312. trace_seq_puts(s, arg->flags.delim);
  3313. print_str_to_seq(s, format, len_arg, flag->str);
  3314. print = 1;
  3315. val &= ~fval;
  3316. }
  3317. }
  3318. break;
  3319. case PRINT_SYMBOL:
  3320. val = eval_num_arg(data, size, event, arg->symbol.field);
  3321. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  3322. fval = eval_flag(flag->value);
  3323. if (val == fval) {
  3324. print_str_to_seq(s, format, len_arg, flag->str);
  3325. break;
  3326. }
  3327. }
  3328. break;
  3329. case PRINT_HEX:
  3330. if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
  3331. unsigned long offset;
  3332. offset = pevent_read_number(pevent,
  3333. data + arg->hex.field->dynarray.field->offset,
  3334. arg->hex.field->dynarray.field->size);
  3335. hex = data + (offset & 0xffff);
  3336. } else {
  3337. field = arg->hex.field->field.field;
  3338. if (!field) {
  3339. str = arg->hex.field->field.name;
  3340. field = pevent_find_any_field(event, str);
  3341. if (!field)
  3342. goto out_warning_field;
  3343. arg->hex.field->field.field = field;
  3344. }
  3345. hex = data + field->offset;
  3346. }
  3347. len = eval_num_arg(data, size, event, arg->hex.size);
  3348. for (i = 0; i < len; i++) {
  3349. if (i)
  3350. trace_seq_putc(s, ' ');
  3351. trace_seq_printf(s, "%02x", hex[i]);
  3352. }
  3353. break;
  3354. case PRINT_INT_ARRAY: {
  3355. void *num;
  3356. int el_size;
  3357. if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
  3358. unsigned long offset;
  3359. struct format_field *field =
  3360. arg->int_array.field->dynarray.field;
  3361. offset = pevent_read_number(pevent,
  3362. data + field->offset,
  3363. field->size);
  3364. num = data + (offset & 0xffff);
  3365. } else {
  3366. field = arg->int_array.field->field.field;
  3367. if (!field) {
  3368. str = arg->int_array.field->field.name;
  3369. field = pevent_find_any_field(event, str);
  3370. if (!field)
  3371. goto out_warning_field;
  3372. arg->int_array.field->field.field = field;
  3373. }
  3374. num = data + field->offset;
  3375. }
  3376. len = eval_num_arg(data, size, event, arg->int_array.count);
  3377. el_size = eval_num_arg(data, size, event,
  3378. arg->int_array.el_size);
  3379. for (i = 0; i < len; i++) {
  3380. if (i)
  3381. trace_seq_putc(s, ' ');
  3382. if (el_size == 1) {
  3383. trace_seq_printf(s, "%u", *(uint8_t *)num);
  3384. } else if (el_size == 2) {
  3385. trace_seq_printf(s, "%u", *(uint16_t *)num);
  3386. } else if (el_size == 4) {
  3387. trace_seq_printf(s, "%u", *(uint32_t *)num);
  3388. } else if (el_size == 8) {
  3389. trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
  3390. } else {
  3391. trace_seq_printf(s, "BAD SIZE:%d 0x%x",
  3392. el_size, *(uint8_t *)num);
  3393. el_size = 1;
  3394. }
  3395. num += el_size;
  3396. }
  3397. break;
  3398. }
  3399. case PRINT_TYPE:
  3400. break;
  3401. case PRINT_STRING: {
  3402. int str_offset;
  3403. if (arg->string.offset == -1) {
  3404. struct format_field *f;
  3405. f = pevent_find_any_field(event, arg->string.string);
  3406. arg->string.offset = f->offset;
  3407. }
  3408. str_offset = data2host4(pevent, data + arg->string.offset);
  3409. str_offset &= 0xffff;
  3410. print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
  3411. break;
  3412. }
  3413. case PRINT_BSTRING:
  3414. print_str_to_seq(s, format, len_arg, arg->string.string);
  3415. break;
  3416. case PRINT_BITMASK: {
  3417. int bitmask_offset;
  3418. int bitmask_size;
  3419. if (arg->bitmask.offset == -1) {
  3420. struct format_field *f;
  3421. f = pevent_find_any_field(event, arg->bitmask.bitmask);
  3422. arg->bitmask.offset = f->offset;
  3423. }
  3424. bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
  3425. bitmask_size = bitmask_offset >> 16;
  3426. bitmask_offset &= 0xffff;
  3427. print_bitmask_to_seq(pevent, s, format, len_arg,
  3428. data + bitmask_offset, bitmask_size);
  3429. break;
  3430. }
  3431. case PRINT_OP:
  3432. /*
  3433. * The only op for string should be ? :
  3434. */
  3435. if (arg->op.op[0] != '?')
  3436. return;
  3437. val = eval_num_arg(data, size, event, arg->op.left);
  3438. if (val)
  3439. print_str_arg(s, data, size, event,
  3440. format, len_arg, arg->op.right->op.left);
  3441. else
  3442. print_str_arg(s, data, size, event,
  3443. format, len_arg, arg->op.right->op.right);
  3444. break;
  3445. case PRINT_FUNC:
  3446. process_defined_func(s, data, size, event, arg);
  3447. break;
  3448. default:
  3449. /* well... */
  3450. break;
  3451. }
  3452. return;
  3453. out_warning_field:
  3454. do_warning_event(event, "%s: field %s not found",
  3455. __func__, arg->field.name);
  3456. }
  3457. static unsigned long long
  3458. process_defined_func(struct trace_seq *s, void *data, int size,
  3459. struct event_format *event, struct print_arg *arg)
  3460. {
  3461. struct pevent_function_handler *func_handle = arg->func.func;
  3462. struct pevent_func_params *param;
  3463. unsigned long long *args;
  3464. unsigned long long ret;
  3465. struct print_arg *farg;
  3466. struct trace_seq str;
  3467. struct save_str {
  3468. struct save_str *next;
  3469. char *str;
  3470. } *strings = NULL, *string;
  3471. int i;
  3472. if (!func_handle->nr_args) {
  3473. ret = (*func_handle->func)(s, NULL);
  3474. goto out;
  3475. }
  3476. farg = arg->func.args;
  3477. param = func_handle->params;
  3478. ret = ULLONG_MAX;
  3479. args = malloc(sizeof(*args) * func_handle->nr_args);
  3480. if (!args)
  3481. goto out;
  3482. for (i = 0; i < func_handle->nr_args; i++) {
  3483. switch (param->type) {
  3484. case PEVENT_FUNC_ARG_INT:
  3485. case PEVENT_FUNC_ARG_LONG:
  3486. case PEVENT_FUNC_ARG_PTR:
  3487. args[i] = eval_num_arg(data, size, event, farg);
  3488. break;
  3489. case PEVENT_FUNC_ARG_STRING:
  3490. trace_seq_init(&str);
  3491. print_str_arg(&str, data, size, event, "%s", -1, farg);
  3492. trace_seq_terminate(&str);
  3493. string = malloc(sizeof(*string));
  3494. if (!string) {
  3495. do_warning_event(event, "%s(%d): malloc str",
  3496. __func__, __LINE__);
  3497. goto out_free;
  3498. }
  3499. string->next = strings;
  3500. string->str = strdup(str.buffer);
  3501. if (!string->str) {
  3502. free(string);
  3503. do_warning_event(event, "%s(%d): malloc str",
  3504. __func__, __LINE__);
  3505. goto out_free;
  3506. }
  3507. args[i] = (uintptr_t)string->str;
  3508. strings = string;
  3509. trace_seq_destroy(&str);
  3510. break;
  3511. default:
  3512. /*
  3513. * Something went totally wrong, this is not
  3514. * an input error, something in this code broke.
  3515. */
  3516. do_warning_event(event, "Unexpected end of arguments\n");
  3517. goto out_free;
  3518. }
  3519. farg = farg->next;
  3520. param = param->next;
  3521. }
  3522. ret = (*func_handle->func)(s, args);
  3523. out_free:
  3524. free(args);
  3525. while (strings) {
  3526. string = strings;
  3527. strings = string->next;
  3528. free(string->str);
  3529. free(string);
  3530. }
  3531. out:
  3532. /* TBD : handle return type here */
  3533. return ret;
  3534. }
  3535. static void free_args(struct print_arg *args)
  3536. {
  3537. struct print_arg *next;
  3538. while (args) {
  3539. next = args->next;
  3540. free_arg(args);
  3541. args = next;
  3542. }
  3543. }
  3544. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
  3545. {
  3546. struct pevent *pevent = event->pevent;
  3547. struct format_field *field, *ip_field;
  3548. struct print_arg *args, *arg, **next;
  3549. unsigned long long ip, val;
  3550. char *ptr;
  3551. void *bptr;
  3552. int vsize;
  3553. field = pevent->bprint_buf_field;
  3554. ip_field = pevent->bprint_ip_field;
  3555. if (!field) {
  3556. field = pevent_find_field(event, "buf");
  3557. if (!field) {
  3558. do_warning_event(event, "can't find buffer field for binary printk");
  3559. return NULL;
  3560. }
  3561. ip_field = pevent_find_field(event, "ip");
  3562. if (!ip_field) {
  3563. do_warning_event(event, "can't find ip field for binary printk");
  3564. return NULL;
  3565. }
  3566. pevent->bprint_buf_field = field;
  3567. pevent->bprint_ip_field = ip_field;
  3568. }
  3569. ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
  3570. /*
  3571. * The first arg is the IP pointer.
  3572. */
  3573. args = alloc_arg();
  3574. if (!args) {
  3575. do_warning_event(event, "%s(%d): not enough memory!",
  3576. __func__, __LINE__);
  3577. return NULL;
  3578. }
  3579. arg = args;
  3580. arg->next = NULL;
  3581. next = &arg->next;
  3582. arg->type = PRINT_ATOM;
  3583. if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
  3584. goto out_free;
  3585. /* skip the first "%ps: " */
  3586. for (ptr = fmt + 5, bptr = data + field->offset;
  3587. bptr < data + size && *ptr; ptr++) {
  3588. int ls = 0;
  3589. if (*ptr == '%') {
  3590. process_again:
  3591. ptr++;
  3592. switch (*ptr) {
  3593. case '%':
  3594. break;
  3595. case 'l':
  3596. ls++;
  3597. goto process_again;
  3598. case 'L':
  3599. ls = 2;
  3600. goto process_again;
  3601. case '0' ... '9':
  3602. goto process_again;
  3603. case '.':
  3604. goto process_again;
  3605. case 'z':
  3606. case 'Z':
  3607. ls = 1;
  3608. goto process_again;
  3609. case 'p':
  3610. ls = 1;
  3611. /* fall through */
  3612. case 'd':
  3613. case 'u':
  3614. case 'x':
  3615. case 'i':
  3616. switch (ls) {
  3617. case 0:
  3618. vsize = 4;
  3619. break;
  3620. case 1:
  3621. vsize = pevent->long_size;
  3622. break;
  3623. case 2:
  3624. vsize = 8;
  3625. break;
  3626. default:
  3627. vsize = ls; /* ? */
  3628. break;
  3629. }
  3630. /* fall through */
  3631. case '*':
  3632. if (*ptr == '*')
  3633. vsize = 4;
  3634. /* the pointers are always 4 bytes aligned */
  3635. bptr = (void *)(((unsigned long)bptr + 3) &
  3636. ~3);
  3637. val = pevent_read_number(pevent, bptr, vsize);
  3638. bptr += vsize;
  3639. arg = alloc_arg();
  3640. if (!arg) {
  3641. do_warning_event(event, "%s(%d): not enough memory!",
  3642. __func__, __LINE__);
  3643. goto out_free;
  3644. }
  3645. arg->next = NULL;
  3646. arg->type = PRINT_ATOM;
  3647. if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
  3648. free(arg);
  3649. goto out_free;
  3650. }
  3651. *next = arg;
  3652. next = &arg->next;
  3653. /*
  3654. * The '*' case means that an arg is used as the length.
  3655. * We need to continue to figure out for what.
  3656. */
  3657. if (*ptr == '*')
  3658. goto process_again;
  3659. break;
  3660. case 's':
  3661. arg = alloc_arg();
  3662. if (!arg) {
  3663. do_warning_event(event, "%s(%d): not enough memory!",
  3664. __func__, __LINE__);
  3665. goto out_free;
  3666. }
  3667. arg->next = NULL;
  3668. arg->type = PRINT_BSTRING;
  3669. arg->string.string = strdup(bptr);
  3670. if (!arg->string.string)
  3671. goto out_free;
  3672. bptr += strlen(bptr) + 1;
  3673. *next = arg;
  3674. next = &arg->next;
  3675. default:
  3676. break;
  3677. }
  3678. }
  3679. }
  3680. return args;
  3681. out_free:
  3682. free_args(args);
  3683. return NULL;
  3684. }
  3685. static char *
  3686. get_bprint_format(void *data, int size __maybe_unused,
  3687. struct event_format *event)
  3688. {
  3689. struct pevent *pevent = event->pevent;
  3690. unsigned long long addr;
  3691. struct format_field *field;
  3692. struct printk_map *printk;
  3693. char *format;
  3694. field = pevent->bprint_fmt_field;
  3695. if (!field) {
  3696. field = pevent_find_field(event, "fmt");
  3697. if (!field) {
  3698. do_warning_event(event, "can't find format field for binary printk");
  3699. return NULL;
  3700. }
  3701. pevent->bprint_fmt_field = field;
  3702. }
  3703. addr = pevent_read_number(pevent, data + field->offset, field->size);
  3704. printk = find_printk(pevent, addr);
  3705. if (!printk) {
  3706. if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
  3707. return NULL;
  3708. return format;
  3709. }
  3710. if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
  3711. return NULL;
  3712. return format;
  3713. }
  3714. static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
  3715. struct event_format *event, struct print_arg *arg)
  3716. {
  3717. unsigned char *buf;
  3718. const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
  3719. if (arg->type == PRINT_FUNC) {
  3720. process_defined_func(s, data, size, event, arg);
  3721. return;
  3722. }
  3723. if (arg->type != PRINT_FIELD) {
  3724. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
  3725. arg->type);
  3726. return;
  3727. }
  3728. if (mac == 'm')
  3729. fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
  3730. if (!arg->field.field) {
  3731. arg->field.field =
  3732. pevent_find_any_field(event, arg->field.name);
  3733. if (!arg->field.field) {
  3734. do_warning_event(event, "%s: field %s not found",
  3735. __func__, arg->field.name);
  3736. return;
  3737. }
  3738. }
  3739. if (arg->field.field->size != 6) {
  3740. trace_seq_printf(s, "INVALIDMAC");
  3741. return;
  3742. }
  3743. buf = data + arg->field.field->offset;
  3744. trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  3745. }
  3746. static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
  3747. {
  3748. const char *fmt;
  3749. if (i == 'i')
  3750. fmt = "%03d.%03d.%03d.%03d";
  3751. else
  3752. fmt = "%d.%d.%d.%d";
  3753. trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
  3754. }
  3755. static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
  3756. {
  3757. return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
  3758. (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
  3759. }
  3760. static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
  3761. {
  3762. return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
  3763. }
  3764. static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
  3765. {
  3766. int i, j, range;
  3767. unsigned char zerolength[8];
  3768. int longest = 1;
  3769. int colonpos = -1;
  3770. uint16_t word;
  3771. uint8_t hi, lo;
  3772. bool needcolon = false;
  3773. bool useIPv4;
  3774. struct in6_addr in6;
  3775. memcpy(&in6, addr, sizeof(struct in6_addr));
  3776. useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
  3777. memset(zerolength, 0, sizeof(zerolength));
  3778. if (useIPv4)
  3779. range = 6;
  3780. else
  3781. range = 8;
  3782. /* find position of longest 0 run */
  3783. for (i = 0; i < range; i++) {
  3784. for (j = i; j < range; j++) {
  3785. if (in6.s6_addr16[j] != 0)
  3786. break;
  3787. zerolength[i]++;
  3788. }
  3789. }
  3790. for (i = 0; i < range; i++) {
  3791. if (zerolength[i] > longest) {
  3792. longest = zerolength[i];
  3793. colonpos = i;
  3794. }
  3795. }
  3796. if (longest == 1) /* don't compress a single 0 */
  3797. colonpos = -1;
  3798. /* emit address */
  3799. for (i = 0; i < range; i++) {
  3800. if (i == colonpos) {
  3801. if (needcolon || i == 0)
  3802. trace_seq_printf(s, ":");
  3803. trace_seq_printf(s, ":");
  3804. needcolon = false;
  3805. i += longest - 1;
  3806. continue;
  3807. }
  3808. if (needcolon) {
  3809. trace_seq_printf(s, ":");
  3810. needcolon = false;
  3811. }
  3812. /* hex u16 without leading 0s */
  3813. word = ntohs(in6.s6_addr16[i]);
  3814. hi = word >> 8;
  3815. lo = word & 0xff;
  3816. if (hi)
  3817. trace_seq_printf(s, "%x%02x", hi, lo);
  3818. else
  3819. trace_seq_printf(s, "%x", lo);
  3820. needcolon = true;
  3821. }
  3822. if (useIPv4) {
  3823. if (needcolon)
  3824. trace_seq_printf(s, ":");
  3825. print_ip4_addr(s, 'I', &in6.s6_addr[12]);
  3826. }
  3827. return;
  3828. }
  3829. static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
  3830. {
  3831. int j;
  3832. for (j = 0; j < 16; j += 2) {
  3833. trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
  3834. if (i == 'I' && j < 14)
  3835. trace_seq_printf(s, ":");
  3836. }
  3837. }
  3838. /*
  3839. * %pi4 print an IPv4 address with leading zeros
  3840. * %pI4 print an IPv4 address without leading zeros
  3841. * %pi6 print an IPv6 address without colons
  3842. * %pI6 print an IPv6 address with colons
  3843. * %pI6c print an IPv6 address in compressed form with colons
  3844. * %pISpc print an IP address based on sockaddr; p adds port.
  3845. */
  3846. static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
  3847. void *data, int size, struct event_format *event,
  3848. struct print_arg *arg)
  3849. {
  3850. unsigned char *buf;
  3851. if (arg->type == PRINT_FUNC) {
  3852. process_defined_func(s, data, size, event, arg);
  3853. return 0;
  3854. }
  3855. if (arg->type != PRINT_FIELD) {
  3856. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
  3857. return 0;
  3858. }
  3859. if (!arg->field.field) {
  3860. arg->field.field =
  3861. pevent_find_any_field(event, arg->field.name);
  3862. if (!arg->field.field) {
  3863. do_warning("%s: field %s not found",
  3864. __func__, arg->field.name);
  3865. return 0;
  3866. }
  3867. }
  3868. buf = data + arg->field.field->offset;
  3869. if (arg->field.field->size != 4) {
  3870. trace_seq_printf(s, "INVALIDIPv4");
  3871. return 0;
  3872. }
  3873. print_ip4_addr(s, i, buf);
  3874. return 0;
  3875. }
  3876. static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
  3877. void *data, int size, struct event_format *event,
  3878. struct print_arg *arg)
  3879. {
  3880. char have_c = 0;
  3881. unsigned char *buf;
  3882. int rc = 0;
  3883. /* pI6c */
  3884. if (i == 'I' && *ptr == 'c') {
  3885. have_c = 1;
  3886. ptr++;
  3887. rc++;
  3888. }
  3889. if (arg->type == PRINT_FUNC) {
  3890. process_defined_func(s, data, size, event, arg);
  3891. return rc;
  3892. }
  3893. if (arg->type != PRINT_FIELD) {
  3894. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
  3895. return rc;
  3896. }
  3897. if (!arg->field.field) {
  3898. arg->field.field =
  3899. pevent_find_any_field(event, arg->field.name);
  3900. if (!arg->field.field) {
  3901. do_warning("%s: field %s not found",
  3902. __func__, arg->field.name);
  3903. return rc;
  3904. }
  3905. }
  3906. buf = data + arg->field.field->offset;
  3907. if (arg->field.field->size != 16) {
  3908. trace_seq_printf(s, "INVALIDIPv6");
  3909. return rc;
  3910. }
  3911. if (have_c)
  3912. print_ip6c_addr(s, buf);
  3913. else
  3914. print_ip6_addr(s, i, buf);
  3915. return rc;
  3916. }
  3917. static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
  3918. void *data, int size, struct event_format *event,
  3919. struct print_arg *arg)
  3920. {
  3921. char have_c = 0, have_p = 0;
  3922. unsigned char *buf;
  3923. struct sockaddr_storage *sa;
  3924. int rc = 0;
  3925. /* pISpc */
  3926. if (i == 'I') {
  3927. if (*ptr == 'p') {
  3928. have_p = 1;
  3929. ptr++;
  3930. rc++;
  3931. }
  3932. if (*ptr == 'c') {
  3933. have_c = 1;
  3934. ptr++;
  3935. rc++;
  3936. }
  3937. }
  3938. if (arg->type == PRINT_FUNC) {
  3939. process_defined_func(s, data, size, event, arg);
  3940. return rc;
  3941. }
  3942. if (arg->type != PRINT_FIELD) {
  3943. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
  3944. return rc;
  3945. }
  3946. if (!arg->field.field) {
  3947. arg->field.field =
  3948. pevent_find_any_field(event, arg->field.name);
  3949. if (!arg->field.field) {
  3950. do_warning("%s: field %s not found",
  3951. __func__, arg->field.name);
  3952. return rc;
  3953. }
  3954. }
  3955. sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
  3956. if (sa->ss_family == AF_INET) {
  3957. struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
  3958. if (arg->field.field->size < sizeof(struct sockaddr_in)) {
  3959. trace_seq_printf(s, "INVALIDIPv4");
  3960. return rc;
  3961. }
  3962. print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
  3963. if (have_p)
  3964. trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
  3965. } else if (sa->ss_family == AF_INET6) {
  3966. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
  3967. if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
  3968. trace_seq_printf(s, "INVALIDIPv6");
  3969. return rc;
  3970. }
  3971. if (have_p)
  3972. trace_seq_printf(s, "[");
  3973. buf = (unsigned char *) &sa6->sin6_addr;
  3974. if (have_c)
  3975. print_ip6c_addr(s, buf);
  3976. else
  3977. print_ip6_addr(s, i, buf);
  3978. if (have_p)
  3979. trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
  3980. }
  3981. return rc;
  3982. }
  3983. static int print_ip_arg(struct trace_seq *s, const char *ptr,
  3984. void *data, int size, struct event_format *event,
  3985. struct print_arg *arg)
  3986. {
  3987. char i = *ptr; /* 'i' or 'I' */
  3988. char ver;
  3989. int rc = 0;
  3990. ptr++;
  3991. rc++;
  3992. ver = *ptr;
  3993. ptr++;
  3994. rc++;
  3995. switch (ver) {
  3996. case '4':
  3997. rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
  3998. break;
  3999. case '6':
  4000. rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
  4001. break;
  4002. case 'S':
  4003. rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
  4004. break;
  4005. default:
  4006. return 0;
  4007. }
  4008. return rc;
  4009. }
  4010. static int is_printable_array(char *p, unsigned int len)
  4011. {
  4012. unsigned int i;
  4013. for (i = 0; i < len && p[i]; i++)
  4014. if (!isprint(p[i]) && !isspace(p[i]))
  4015. return 0;
  4016. return 1;
  4017. }
  4018. void pevent_print_field(struct trace_seq *s, void *data,
  4019. struct format_field *field)
  4020. {
  4021. unsigned long long val;
  4022. unsigned int offset, len, i;
  4023. struct pevent *pevent = field->event->pevent;
  4024. if (field->flags & FIELD_IS_ARRAY) {
  4025. offset = field->offset;
  4026. len = field->size;
  4027. if (field->flags & FIELD_IS_DYNAMIC) {
  4028. val = pevent_read_number(pevent, data + offset, len);
  4029. offset = val;
  4030. len = offset >> 16;
  4031. offset &= 0xffff;
  4032. }
  4033. if (field->flags & FIELD_IS_STRING &&
  4034. is_printable_array(data + offset, len)) {
  4035. trace_seq_printf(s, "%s", (char *)data + offset);
  4036. } else {
  4037. trace_seq_puts(s, "ARRAY[");
  4038. for (i = 0; i < len; i++) {
  4039. if (i)
  4040. trace_seq_puts(s, ", ");
  4041. trace_seq_printf(s, "%02x",
  4042. *((unsigned char *)data + offset + i));
  4043. }
  4044. trace_seq_putc(s, ']');
  4045. field->flags &= ~FIELD_IS_STRING;
  4046. }
  4047. } else {
  4048. val = pevent_read_number(pevent, data + field->offset,
  4049. field->size);
  4050. if (field->flags & FIELD_IS_POINTER) {
  4051. trace_seq_printf(s, "0x%llx", val);
  4052. } else if (field->flags & FIELD_IS_SIGNED) {
  4053. switch (field->size) {
  4054. case 4:
  4055. /*
  4056. * If field is long then print it in hex.
  4057. * A long usually stores pointers.
  4058. */
  4059. if (field->flags & FIELD_IS_LONG)
  4060. trace_seq_printf(s, "0x%x", (int)val);
  4061. else
  4062. trace_seq_printf(s, "%d", (int)val);
  4063. break;
  4064. case 2:
  4065. trace_seq_printf(s, "%2d", (short)val);
  4066. break;
  4067. case 1:
  4068. trace_seq_printf(s, "%1d", (char)val);
  4069. break;
  4070. default:
  4071. trace_seq_printf(s, "%lld", val);
  4072. }
  4073. } else {
  4074. if (field->flags & FIELD_IS_LONG)
  4075. trace_seq_printf(s, "0x%llx", val);
  4076. else
  4077. trace_seq_printf(s, "%llu", val);
  4078. }
  4079. }
  4080. }
  4081. void pevent_print_fields(struct trace_seq *s, void *data,
  4082. int size __maybe_unused, struct event_format *event)
  4083. {
  4084. struct format_field *field;
  4085. field = event->format.fields;
  4086. while (field) {
  4087. trace_seq_printf(s, " %s=", field->name);
  4088. pevent_print_field(s, data, field);
  4089. field = field->next;
  4090. }
  4091. }
  4092. static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
  4093. {
  4094. struct pevent *pevent = event->pevent;
  4095. struct print_fmt *print_fmt = &event->print_fmt;
  4096. struct print_arg *arg = print_fmt->args;
  4097. struct print_arg *args = NULL;
  4098. const char *ptr = print_fmt->format;
  4099. unsigned long long val;
  4100. struct func_map *func;
  4101. const char *saveptr;
  4102. struct trace_seq p;
  4103. char *bprint_fmt = NULL;
  4104. char format[32];
  4105. int show_func;
  4106. int len_as_arg;
  4107. int len_arg;
  4108. int len;
  4109. int ls;
  4110. if (event->flags & EVENT_FL_FAILED) {
  4111. trace_seq_printf(s, "[FAILED TO PARSE]");
  4112. pevent_print_fields(s, data, size, event);
  4113. return;
  4114. }
  4115. if (event->flags & EVENT_FL_ISBPRINT) {
  4116. bprint_fmt = get_bprint_format(data, size, event);
  4117. args = make_bprint_args(bprint_fmt, data, size, event);
  4118. arg = args;
  4119. ptr = bprint_fmt;
  4120. }
  4121. for (; *ptr; ptr++) {
  4122. ls = 0;
  4123. if (*ptr == '\\') {
  4124. ptr++;
  4125. switch (*ptr) {
  4126. case 'n':
  4127. trace_seq_putc(s, '\n');
  4128. break;
  4129. case 't':
  4130. trace_seq_putc(s, '\t');
  4131. break;
  4132. case 'r':
  4133. trace_seq_putc(s, '\r');
  4134. break;
  4135. case '\\':
  4136. trace_seq_putc(s, '\\');
  4137. break;
  4138. default:
  4139. trace_seq_putc(s, *ptr);
  4140. break;
  4141. }
  4142. } else if (*ptr == '%') {
  4143. saveptr = ptr;
  4144. show_func = 0;
  4145. len_as_arg = 0;
  4146. cont_process:
  4147. ptr++;
  4148. switch (*ptr) {
  4149. case '%':
  4150. trace_seq_putc(s, '%');
  4151. break;
  4152. case '#':
  4153. /* FIXME: need to handle properly */
  4154. goto cont_process;
  4155. case 'h':
  4156. ls--;
  4157. goto cont_process;
  4158. case 'l':
  4159. ls++;
  4160. goto cont_process;
  4161. case 'L':
  4162. ls = 2;
  4163. goto cont_process;
  4164. case '*':
  4165. /* The argument is the length. */
  4166. if (!arg) {
  4167. do_warning_event(event, "no argument match");
  4168. event->flags |= EVENT_FL_FAILED;
  4169. goto out_failed;
  4170. }
  4171. len_arg = eval_num_arg(data, size, event, arg);
  4172. len_as_arg = 1;
  4173. arg = arg->next;
  4174. goto cont_process;
  4175. case '.':
  4176. case 'z':
  4177. case 'Z':
  4178. case '0' ... '9':
  4179. case '-':
  4180. goto cont_process;
  4181. case 'p':
  4182. if (pevent->long_size == 4)
  4183. ls = 1;
  4184. else
  4185. ls = 2;
  4186. if (*(ptr+1) == 'F' || *(ptr+1) == 'f' ||
  4187. *(ptr+1) == 'S' || *(ptr+1) == 's') {
  4188. ptr++;
  4189. show_func = *ptr;
  4190. } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
  4191. print_mac_arg(s, *(ptr+1), data, size, event, arg);
  4192. ptr++;
  4193. arg = arg->next;
  4194. break;
  4195. } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
  4196. int n;
  4197. n = print_ip_arg(s, ptr+1, data, size, event, arg);
  4198. if (n > 0) {
  4199. ptr += n;
  4200. arg = arg->next;
  4201. break;
  4202. }
  4203. }
  4204. /* fall through */
  4205. case 'd':
  4206. case 'i':
  4207. case 'x':
  4208. case 'X':
  4209. case 'u':
  4210. if (!arg) {
  4211. do_warning_event(event, "no argument match");
  4212. event->flags |= EVENT_FL_FAILED;
  4213. goto out_failed;
  4214. }
  4215. len = ((unsigned long)ptr + 1) -
  4216. (unsigned long)saveptr;
  4217. /* should never happen */
  4218. if (len > 31) {
  4219. do_warning_event(event, "bad format!");
  4220. event->flags |= EVENT_FL_FAILED;
  4221. len = 31;
  4222. }
  4223. memcpy(format, saveptr, len);
  4224. format[len] = 0;
  4225. val = eval_num_arg(data, size, event, arg);
  4226. arg = arg->next;
  4227. if (show_func) {
  4228. func = find_func(pevent, val);
  4229. if (func) {
  4230. trace_seq_puts(s, func->func);
  4231. if (show_func == 'F')
  4232. trace_seq_printf(s,
  4233. "+0x%llx",
  4234. val - func->addr);
  4235. break;
  4236. }
  4237. }
  4238. if (pevent->long_size == 8 && ls == 1 &&
  4239. sizeof(long) != 8) {
  4240. char *p;
  4241. /* make %l into %ll */
  4242. if (ls == 1 && (p = strchr(format, 'l')))
  4243. memmove(p+1, p, strlen(p)+1);
  4244. else if (strcmp(format, "%p") == 0)
  4245. strcpy(format, "0x%llx");
  4246. ls = 2;
  4247. }
  4248. switch (ls) {
  4249. case -2:
  4250. if (len_as_arg)
  4251. trace_seq_printf(s, format, len_arg, (char)val);
  4252. else
  4253. trace_seq_printf(s, format, (char)val);
  4254. break;
  4255. case -1:
  4256. if (len_as_arg)
  4257. trace_seq_printf(s, format, len_arg, (short)val);
  4258. else
  4259. trace_seq_printf(s, format, (short)val);
  4260. break;
  4261. case 0:
  4262. if (len_as_arg)
  4263. trace_seq_printf(s, format, len_arg, (int)val);
  4264. else
  4265. trace_seq_printf(s, format, (int)val);
  4266. break;
  4267. case 1:
  4268. if (len_as_arg)
  4269. trace_seq_printf(s, format, len_arg, (long)val);
  4270. else
  4271. trace_seq_printf(s, format, (long)val);
  4272. break;
  4273. case 2:
  4274. if (len_as_arg)
  4275. trace_seq_printf(s, format, len_arg,
  4276. (long long)val);
  4277. else
  4278. trace_seq_printf(s, format, (long long)val);
  4279. break;
  4280. default:
  4281. do_warning_event(event, "bad count (%d)", ls);
  4282. event->flags |= EVENT_FL_FAILED;
  4283. }
  4284. break;
  4285. case 's':
  4286. if (!arg) {
  4287. do_warning_event(event, "no matching argument");
  4288. event->flags |= EVENT_FL_FAILED;
  4289. goto out_failed;
  4290. }
  4291. len = ((unsigned long)ptr + 1) -
  4292. (unsigned long)saveptr;
  4293. /* should never happen */
  4294. if (len > 31) {
  4295. do_warning_event(event, "bad format!");
  4296. event->flags |= EVENT_FL_FAILED;
  4297. len = 31;
  4298. }
  4299. memcpy(format, saveptr, len);
  4300. format[len] = 0;
  4301. if (!len_as_arg)
  4302. len_arg = -1;
  4303. /* Use helper trace_seq */
  4304. trace_seq_init(&p);
  4305. print_str_arg(&p, data, size, event,
  4306. format, len_arg, arg);
  4307. trace_seq_terminate(&p);
  4308. trace_seq_puts(s, p.buffer);
  4309. trace_seq_destroy(&p);
  4310. arg = arg->next;
  4311. break;
  4312. default:
  4313. trace_seq_printf(s, ">%c<", *ptr);
  4314. }
  4315. } else
  4316. trace_seq_putc(s, *ptr);
  4317. }
  4318. if (event->flags & EVENT_FL_FAILED) {
  4319. out_failed:
  4320. trace_seq_printf(s, "[FAILED TO PARSE]");
  4321. }
  4322. if (args) {
  4323. free_args(args);
  4324. free(bprint_fmt);
  4325. }
  4326. }
  4327. /**
  4328. * pevent_data_lat_fmt - parse the data for the latency format
  4329. * @pevent: a handle to the pevent
  4330. * @s: the trace_seq to write to
  4331. * @record: the record to read from
  4332. *
  4333. * This parses out the Latency format (interrupts disabled,
  4334. * need rescheduling, in hard/soft interrupt, preempt count
  4335. * and lock depth) and places it into the trace_seq.
  4336. */
  4337. void pevent_data_lat_fmt(struct pevent *pevent,
  4338. struct trace_seq *s, struct pevent_record *record)
  4339. {
  4340. static int check_lock_depth = 1;
  4341. static int check_migrate_disable = 1;
  4342. static int lock_depth_exists;
  4343. static int migrate_disable_exists;
  4344. unsigned int lat_flags;
  4345. unsigned int pc;
  4346. int lock_depth;
  4347. int migrate_disable;
  4348. int hardirq;
  4349. int softirq;
  4350. void *data = record->data;
  4351. lat_flags = parse_common_flags(pevent, data);
  4352. pc = parse_common_pc(pevent, data);
  4353. /* lock_depth may not always exist */
  4354. if (lock_depth_exists)
  4355. lock_depth = parse_common_lock_depth(pevent, data);
  4356. else if (check_lock_depth) {
  4357. lock_depth = parse_common_lock_depth(pevent, data);
  4358. if (lock_depth < 0)
  4359. check_lock_depth = 0;
  4360. else
  4361. lock_depth_exists = 1;
  4362. }
  4363. /* migrate_disable may not always exist */
  4364. if (migrate_disable_exists)
  4365. migrate_disable = parse_common_migrate_disable(pevent, data);
  4366. else if (check_migrate_disable) {
  4367. migrate_disable = parse_common_migrate_disable(pevent, data);
  4368. if (migrate_disable < 0)
  4369. check_migrate_disable = 0;
  4370. else
  4371. migrate_disable_exists = 1;
  4372. }
  4373. hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
  4374. softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
  4375. trace_seq_printf(s, "%c%c%c",
  4376. (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  4377. (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  4378. 'X' : '.',
  4379. (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
  4380. 'N' : '.',
  4381. (hardirq && softirq) ? 'H' :
  4382. hardirq ? 'h' : softirq ? 's' : '.');
  4383. if (pc)
  4384. trace_seq_printf(s, "%x", pc);
  4385. else
  4386. trace_seq_putc(s, '.');
  4387. if (migrate_disable_exists) {
  4388. if (migrate_disable < 0)
  4389. trace_seq_putc(s, '.');
  4390. else
  4391. trace_seq_printf(s, "%d", migrate_disable);
  4392. }
  4393. if (lock_depth_exists) {
  4394. if (lock_depth < 0)
  4395. trace_seq_putc(s, '.');
  4396. else
  4397. trace_seq_printf(s, "%d", lock_depth);
  4398. }
  4399. trace_seq_terminate(s);
  4400. }
  4401. /**
  4402. * pevent_data_type - parse out the given event type
  4403. * @pevent: a handle to the pevent
  4404. * @rec: the record to read from
  4405. *
  4406. * This returns the event id from the @rec.
  4407. */
  4408. int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
  4409. {
  4410. return trace_parse_common_type(pevent, rec->data);
  4411. }
  4412. /**
  4413. * pevent_data_event_from_type - find the event by a given type
  4414. * @pevent: a handle to the pevent
  4415. * @type: the type of the event.
  4416. *
  4417. * This returns the event form a given @type;
  4418. */
  4419. struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
  4420. {
  4421. return pevent_find_event(pevent, type);
  4422. }
  4423. /**
  4424. * pevent_data_pid - parse the PID from record
  4425. * @pevent: a handle to the pevent
  4426. * @rec: the record to parse
  4427. *
  4428. * This returns the PID from a record.
  4429. */
  4430. int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
  4431. {
  4432. return parse_common_pid(pevent, rec->data);
  4433. }
  4434. /**
  4435. * pevent_data_prempt_count - parse the preempt count from the record
  4436. * @pevent: a handle to the pevent
  4437. * @rec: the record to parse
  4438. *
  4439. * This returns the preempt count from a record.
  4440. */
  4441. int pevent_data_prempt_count(struct pevent *pevent, struct pevent_record *rec)
  4442. {
  4443. return parse_common_pc(pevent, rec->data);
  4444. }
  4445. /**
  4446. * pevent_data_flags - parse the latency flags from the record
  4447. * @pevent: a handle to the pevent
  4448. * @rec: the record to parse
  4449. *
  4450. * This returns the latency flags from a record.
  4451. *
  4452. * Use trace_flag_type enum for the flags (see event-parse.h).
  4453. */
  4454. int pevent_data_flags(struct pevent *pevent, struct pevent_record *rec)
  4455. {
  4456. return parse_common_flags(pevent, rec->data);
  4457. }
  4458. /**
  4459. * pevent_data_comm_from_pid - return the command line from PID
  4460. * @pevent: a handle to the pevent
  4461. * @pid: the PID of the task to search for
  4462. *
  4463. * This returns a pointer to the command line that has the given
  4464. * @pid.
  4465. */
  4466. const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
  4467. {
  4468. const char *comm;
  4469. comm = find_cmdline(pevent, pid);
  4470. return comm;
  4471. }
  4472. static struct cmdline *
  4473. pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
  4474. {
  4475. struct cmdline_list *cmdlist = (struct cmdline_list *)next;
  4476. if (cmdlist)
  4477. cmdlist = cmdlist->next;
  4478. else
  4479. cmdlist = pevent->cmdlist;
  4480. while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
  4481. cmdlist = cmdlist->next;
  4482. return (struct cmdline *)cmdlist;
  4483. }
  4484. /**
  4485. * pevent_data_pid_from_comm - return the pid from a given comm
  4486. * @pevent: a handle to the pevent
  4487. * @comm: the cmdline to find the pid from
  4488. * @next: the cmdline structure to find the next comm
  4489. *
  4490. * This returns the cmdline structure that holds a pid for a given
  4491. * comm, or NULL if none found. As there may be more than one pid for
  4492. * a given comm, the result of this call can be passed back into
  4493. * a recurring call in the @next paramater, and then it will find the
  4494. * next pid.
  4495. * Also, it does a linear seach, so it may be slow.
  4496. */
  4497. struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
  4498. struct cmdline *next)
  4499. {
  4500. struct cmdline *cmdline;
  4501. /*
  4502. * If the cmdlines have not been converted yet, then use
  4503. * the list.
  4504. */
  4505. if (!pevent->cmdlines)
  4506. return pid_from_cmdlist(pevent, comm, next);
  4507. if (next) {
  4508. /*
  4509. * The next pointer could have been still from
  4510. * a previous call before cmdlines were created
  4511. */
  4512. if (next < pevent->cmdlines ||
  4513. next >= pevent->cmdlines + pevent->cmdline_count)
  4514. next = NULL;
  4515. else
  4516. cmdline = next++;
  4517. }
  4518. if (!next)
  4519. cmdline = pevent->cmdlines;
  4520. while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
  4521. if (strcmp(cmdline->comm, comm) == 0)
  4522. return cmdline;
  4523. cmdline++;
  4524. }
  4525. return NULL;
  4526. }
  4527. /**
  4528. * pevent_cmdline_pid - return the pid associated to a given cmdline
  4529. * @cmdline: The cmdline structure to get the pid from
  4530. *
  4531. * Returns the pid for a give cmdline. If @cmdline is NULL, then
  4532. * -1 is returned.
  4533. */
  4534. int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
  4535. {
  4536. struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
  4537. if (!cmdline)
  4538. return -1;
  4539. /*
  4540. * If cmdlines have not been created yet, or cmdline is
  4541. * not part of the array, then treat it as a cmdlist instead.
  4542. */
  4543. if (!pevent->cmdlines ||
  4544. cmdline < pevent->cmdlines ||
  4545. cmdline >= pevent->cmdlines + pevent->cmdline_count)
  4546. return cmdlist->pid;
  4547. return cmdline->pid;
  4548. }
  4549. /**
  4550. * pevent_data_comm_from_pid - parse the data into the print format
  4551. * @s: the trace_seq to write to
  4552. * @event: the handle to the event
  4553. * @record: the record to read from
  4554. *
  4555. * This parses the raw @data using the given @event information and
  4556. * writes the print format into the trace_seq.
  4557. */
  4558. void pevent_event_info(struct trace_seq *s, struct event_format *event,
  4559. struct pevent_record *record)
  4560. {
  4561. int print_pretty = 1;
  4562. if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
  4563. pevent_print_fields(s, record->data, record->size, event);
  4564. else {
  4565. if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
  4566. print_pretty = event->handler(s, record, event,
  4567. event->context);
  4568. if (print_pretty)
  4569. pretty_print(s, record->data, record->size, event);
  4570. }
  4571. trace_seq_terminate(s);
  4572. }
  4573. static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
  4574. {
  4575. if (!use_trace_clock)
  4576. return true;
  4577. if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
  4578. || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
  4579. return true;
  4580. /* trace_clock is setting in tsc or counter mode */
  4581. return false;
  4582. }
  4583. /**
  4584. * pevent_find_event_by_record - return the event from a given record
  4585. * @pevent: a handle to the pevent
  4586. * @record: The record to get the event from
  4587. *
  4588. * Returns the associated event for a given record, or NULL if non is
  4589. * is found.
  4590. */
  4591. struct event_format *
  4592. pevent_find_event_by_record(struct pevent *pevent, struct pevent_record *record)
  4593. {
  4594. int type;
  4595. if (record->size < 0) {
  4596. do_warning("ug! negative record size %d", record->size);
  4597. return NULL;
  4598. }
  4599. type = trace_parse_common_type(pevent, record->data);
  4600. return pevent_find_event(pevent, type);
  4601. }
  4602. /**
  4603. * pevent_print_event_task - Write the event task comm, pid and CPU
  4604. * @pevent: a handle to the pevent
  4605. * @s: the trace_seq to write to
  4606. * @event: the handle to the record's event
  4607. * @record: The record to get the event from
  4608. *
  4609. * Writes the tasks comm, pid and CPU to @s.
  4610. */
  4611. void pevent_print_event_task(struct pevent *pevent, struct trace_seq *s,
  4612. struct event_format *event,
  4613. struct pevent_record *record)
  4614. {
  4615. void *data = record->data;
  4616. const char *comm;
  4617. int pid;
  4618. pid = parse_common_pid(pevent, data);
  4619. comm = find_cmdline(pevent, pid);
  4620. if (pevent->latency_format) {
  4621. trace_seq_printf(s, "%8.8s-%-5d %3d",
  4622. comm, pid, record->cpu);
  4623. } else
  4624. trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
  4625. }
  4626. /**
  4627. * pevent_print_event_time - Write the event timestamp
  4628. * @pevent: a handle to the pevent
  4629. * @s: the trace_seq to write to
  4630. * @event: the handle to the record's event
  4631. * @record: The record to get the event from
  4632. * @use_trace_clock: Set to parse according to the @pevent->trace_clock
  4633. *
  4634. * Writes the timestamp of the record into @s.
  4635. */
  4636. void pevent_print_event_time(struct pevent *pevent, struct trace_seq *s,
  4637. struct event_format *event,
  4638. struct pevent_record *record,
  4639. bool use_trace_clock)
  4640. {
  4641. unsigned long secs;
  4642. unsigned long usecs;
  4643. unsigned long nsecs;
  4644. int p;
  4645. bool use_usec_format;
  4646. use_usec_format = is_timestamp_in_us(pevent->trace_clock,
  4647. use_trace_clock);
  4648. if (use_usec_format) {
  4649. secs = record->ts / NSEC_PER_SEC;
  4650. nsecs = record->ts - secs * NSEC_PER_SEC;
  4651. }
  4652. if (pevent->latency_format) {
  4653. pevent_data_lat_fmt(pevent, s, record);
  4654. }
  4655. if (use_usec_format) {
  4656. if (pevent->flags & PEVENT_NSEC_OUTPUT) {
  4657. usecs = nsecs;
  4658. p = 9;
  4659. } else {
  4660. usecs = (nsecs + 500) / NSEC_PER_USEC;
  4661. /* To avoid usecs larger than 1 sec */
  4662. if (usecs >= USEC_PER_SEC) {
  4663. usecs -= USEC_PER_SEC;
  4664. secs++;
  4665. }
  4666. p = 6;
  4667. }
  4668. trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
  4669. } else
  4670. trace_seq_printf(s, " %12llu:", record->ts);
  4671. }
  4672. /**
  4673. * pevent_print_event_data - Write the event data section
  4674. * @pevent: a handle to the pevent
  4675. * @s: the trace_seq to write to
  4676. * @event: the handle to the record's event
  4677. * @record: The record to get the event from
  4678. *
  4679. * Writes the parsing of the record's data to @s.
  4680. */
  4681. void pevent_print_event_data(struct pevent *pevent, struct trace_seq *s,
  4682. struct event_format *event,
  4683. struct pevent_record *record)
  4684. {
  4685. static const char *spaces = " "; /* 20 spaces */
  4686. int len;
  4687. trace_seq_printf(s, " %s: ", event->name);
  4688. /* Space out the event names evenly. */
  4689. len = strlen(event->name);
  4690. if (len < 20)
  4691. trace_seq_printf(s, "%.*s", 20 - len, spaces);
  4692. pevent_event_info(s, event, record);
  4693. }
  4694. void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
  4695. struct pevent_record *record, bool use_trace_clock)
  4696. {
  4697. struct event_format *event;
  4698. event = pevent_find_event_by_record(pevent, record);
  4699. if (!event) {
  4700. do_warning("ug! no event found for type %d",
  4701. trace_parse_common_type(pevent, record->data));
  4702. return;
  4703. }
  4704. pevent_print_event_task(pevent, s, event, record);
  4705. pevent_print_event_time(pevent, s, event, record, use_trace_clock);
  4706. pevent_print_event_data(pevent, s, event, record);
  4707. }
  4708. static int events_id_cmp(const void *a, const void *b)
  4709. {
  4710. struct event_format * const * ea = a;
  4711. struct event_format * const * eb = b;
  4712. if ((*ea)->id < (*eb)->id)
  4713. return -1;
  4714. if ((*ea)->id > (*eb)->id)
  4715. return 1;
  4716. return 0;
  4717. }
  4718. static int events_name_cmp(const void *a, const void *b)
  4719. {
  4720. struct event_format * const * ea = a;
  4721. struct event_format * const * eb = b;
  4722. int res;
  4723. res = strcmp((*ea)->name, (*eb)->name);
  4724. if (res)
  4725. return res;
  4726. res = strcmp((*ea)->system, (*eb)->system);
  4727. if (res)
  4728. return res;
  4729. return events_id_cmp(a, b);
  4730. }
  4731. static int events_system_cmp(const void *a, const void *b)
  4732. {
  4733. struct event_format * const * ea = a;
  4734. struct event_format * const * eb = b;
  4735. int res;
  4736. res = strcmp((*ea)->system, (*eb)->system);
  4737. if (res)
  4738. return res;
  4739. res = strcmp((*ea)->name, (*eb)->name);
  4740. if (res)
  4741. return res;
  4742. return events_id_cmp(a, b);
  4743. }
  4744. struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
  4745. {
  4746. struct event_format **events;
  4747. int (*sort)(const void *a, const void *b);
  4748. events = pevent->sort_events;
  4749. if (events && pevent->last_type == sort_type)
  4750. return events;
  4751. if (!events) {
  4752. events = malloc(sizeof(*events) * (pevent->nr_events + 1));
  4753. if (!events)
  4754. return NULL;
  4755. memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
  4756. events[pevent->nr_events] = NULL;
  4757. pevent->sort_events = events;
  4758. /* the internal events are sorted by id */
  4759. if (sort_type == EVENT_SORT_ID) {
  4760. pevent->last_type = sort_type;
  4761. return events;
  4762. }
  4763. }
  4764. switch (sort_type) {
  4765. case EVENT_SORT_ID:
  4766. sort = events_id_cmp;
  4767. break;
  4768. case EVENT_SORT_NAME:
  4769. sort = events_name_cmp;
  4770. break;
  4771. case EVENT_SORT_SYSTEM:
  4772. sort = events_system_cmp;
  4773. break;
  4774. default:
  4775. return events;
  4776. }
  4777. qsort(events, pevent->nr_events, sizeof(*events), sort);
  4778. pevent->last_type = sort_type;
  4779. return events;
  4780. }
  4781. static struct format_field **
  4782. get_event_fields(const char *type, const char *name,
  4783. int count, struct format_field *list)
  4784. {
  4785. struct format_field **fields;
  4786. struct format_field *field;
  4787. int i = 0;
  4788. fields = malloc(sizeof(*fields) * (count + 1));
  4789. if (!fields)
  4790. return NULL;
  4791. for (field = list; field; field = field->next) {
  4792. fields[i++] = field;
  4793. if (i == count + 1) {
  4794. do_warning("event %s has more %s fields than specified",
  4795. name, type);
  4796. i--;
  4797. break;
  4798. }
  4799. }
  4800. if (i != count)
  4801. do_warning("event %s has less %s fields than specified",
  4802. name, type);
  4803. fields[i] = NULL;
  4804. return fields;
  4805. }
  4806. /**
  4807. * pevent_event_common_fields - return a list of common fields for an event
  4808. * @event: the event to return the common fields of.
  4809. *
  4810. * Returns an allocated array of fields. The last item in the array is NULL.
  4811. * The array must be freed with free().
  4812. */
  4813. struct format_field **pevent_event_common_fields(struct event_format *event)
  4814. {
  4815. return get_event_fields("common", event->name,
  4816. event->format.nr_common,
  4817. event->format.common_fields);
  4818. }
  4819. /**
  4820. * pevent_event_fields - return a list of event specific fields for an event
  4821. * @event: the event to return the fields of.
  4822. *
  4823. * Returns an allocated array of fields. The last item in the array is NULL.
  4824. * The array must be freed with free().
  4825. */
  4826. struct format_field **pevent_event_fields(struct event_format *event)
  4827. {
  4828. return get_event_fields("event", event->name,
  4829. event->format.nr_fields,
  4830. event->format.fields);
  4831. }
  4832. static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
  4833. {
  4834. trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
  4835. if (field->next) {
  4836. trace_seq_puts(s, ", ");
  4837. print_fields(s, field->next);
  4838. }
  4839. }
  4840. /* for debugging */
  4841. static void print_args(struct print_arg *args)
  4842. {
  4843. int print_paren = 1;
  4844. struct trace_seq s;
  4845. switch (args->type) {
  4846. case PRINT_NULL:
  4847. printf("null");
  4848. break;
  4849. case PRINT_ATOM:
  4850. printf("%s", args->atom.atom);
  4851. break;
  4852. case PRINT_FIELD:
  4853. printf("REC->%s", args->field.name);
  4854. break;
  4855. case PRINT_FLAGS:
  4856. printf("__print_flags(");
  4857. print_args(args->flags.field);
  4858. printf(", %s, ", args->flags.delim);
  4859. trace_seq_init(&s);
  4860. print_fields(&s, args->flags.flags);
  4861. trace_seq_do_printf(&s);
  4862. trace_seq_destroy(&s);
  4863. printf(")");
  4864. break;
  4865. case PRINT_SYMBOL:
  4866. printf("__print_symbolic(");
  4867. print_args(args->symbol.field);
  4868. printf(", ");
  4869. trace_seq_init(&s);
  4870. print_fields(&s, args->symbol.symbols);
  4871. trace_seq_do_printf(&s);
  4872. trace_seq_destroy(&s);
  4873. printf(")");
  4874. break;
  4875. case PRINT_HEX:
  4876. printf("__print_hex(");
  4877. print_args(args->hex.field);
  4878. printf(", ");
  4879. print_args(args->hex.size);
  4880. printf(")");
  4881. break;
  4882. case PRINT_INT_ARRAY:
  4883. printf("__print_array(");
  4884. print_args(args->int_array.field);
  4885. printf(", ");
  4886. print_args(args->int_array.count);
  4887. printf(", ");
  4888. print_args(args->int_array.el_size);
  4889. printf(")");
  4890. break;
  4891. case PRINT_STRING:
  4892. case PRINT_BSTRING:
  4893. printf("__get_str(%s)", args->string.string);
  4894. break;
  4895. case PRINT_BITMASK:
  4896. printf("__get_bitmask(%s)", args->bitmask.bitmask);
  4897. break;
  4898. case PRINT_TYPE:
  4899. printf("(%s)", args->typecast.type);
  4900. print_args(args->typecast.item);
  4901. break;
  4902. case PRINT_OP:
  4903. if (strcmp(args->op.op, ":") == 0)
  4904. print_paren = 0;
  4905. if (print_paren)
  4906. printf("(");
  4907. print_args(args->op.left);
  4908. printf(" %s ", args->op.op);
  4909. print_args(args->op.right);
  4910. if (print_paren)
  4911. printf(")");
  4912. break;
  4913. default:
  4914. /* we should warn... */
  4915. return;
  4916. }
  4917. if (args->next) {
  4918. printf("\n");
  4919. print_args(args->next);
  4920. }
  4921. }
  4922. static void parse_header_field(const char *field,
  4923. int *offset, int *size, int mandatory)
  4924. {
  4925. unsigned long long save_input_buf_ptr;
  4926. unsigned long long save_input_buf_siz;
  4927. char *token;
  4928. int type;
  4929. save_input_buf_ptr = input_buf_ptr;
  4930. save_input_buf_siz = input_buf_siz;
  4931. if (read_expected(EVENT_ITEM, "field") < 0)
  4932. return;
  4933. if (read_expected(EVENT_OP, ":") < 0)
  4934. return;
  4935. /* type */
  4936. if (read_expect_type(EVENT_ITEM, &token) < 0)
  4937. goto fail;
  4938. free_token(token);
  4939. /*
  4940. * If this is not a mandatory field, then test it first.
  4941. */
  4942. if (mandatory) {
  4943. if (read_expected(EVENT_ITEM, field) < 0)
  4944. return;
  4945. } else {
  4946. if (read_expect_type(EVENT_ITEM, &token) < 0)
  4947. goto fail;
  4948. if (strcmp(token, field) != 0)
  4949. goto discard;
  4950. free_token(token);
  4951. }
  4952. if (read_expected(EVENT_OP, ";") < 0)
  4953. return;
  4954. if (read_expected(EVENT_ITEM, "offset") < 0)
  4955. return;
  4956. if (read_expected(EVENT_OP, ":") < 0)
  4957. return;
  4958. if (read_expect_type(EVENT_ITEM, &token) < 0)
  4959. goto fail;
  4960. *offset = atoi(token);
  4961. free_token(token);
  4962. if (read_expected(EVENT_OP, ";") < 0)
  4963. return;
  4964. if (read_expected(EVENT_ITEM, "size") < 0)
  4965. return;
  4966. if (read_expected(EVENT_OP, ":") < 0)
  4967. return;
  4968. if (read_expect_type(EVENT_ITEM, &token) < 0)
  4969. goto fail;
  4970. *size = atoi(token);
  4971. free_token(token);
  4972. if (read_expected(EVENT_OP, ";") < 0)
  4973. return;
  4974. type = read_token(&token);
  4975. if (type != EVENT_NEWLINE) {
  4976. /* newer versions of the kernel have a "signed" type */
  4977. if (type != EVENT_ITEM)
  4978. goto fail;
  4979. if (strcmp(token, "signed") != 0)
  4980. goto fail;
  4981. free_token(token);
  4982. if (read_expected(EVENT_OP, ":") < 0)
  4983. return;
  4984. if (read_expect_type(EVENT_ITEM, &token))
  4985. goto fail;
  4986. free_token(token);
  4987. if (read_expected(EVENT_OP, ";") < 0)
  4988. return;
  4989. if (read_expect_type(EVENT_NEWLINE, &token))
  4990. goto fail;
  4991. }
  4992. fail:
  4993. free_token(token);
  4994. return;
  4995. discard:
  4996. input_buf_ptr = save_input_buf_ptr;
  4997. input_buf_siz = save_input_buf_siz;
  4998. *offset = 0;
  4999. *size = 0;
  5000. free_token(token);
  5001. }
  5002. /**
  5003. * pevent_parse_header_page - parse the data stored in the header page
  5004. * @pevent: the handle to the pevent
  5005. * @buf: the buffer storing the header page format string
  5006. * @size: the size of @buf
  5007. * @long_size: the long size to use if there is no header
  5008. *
  5009. * This parses the header page format for information on the
  5010. * ring buffer used. The @buf should be copied from
  5011. *
  5012. * /sys/kernel/debug/tracing/events/header_page
  5013. */
  5014. int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
  5015. int long_size)
  5016. {
  5017. int ignore;
  5018. if (!size) {
  5019. /*
  5020. * Old kernels did not have header page info.
  5021. * Sorry but we just use what we find here in user space.
  5022. */
  5023. pevent->header_page_ts_size = sizeof(long long);
  5024. pevent->header_page_size_size = long_size;
  5025. pevent->header_page_data_offset = sizeof(long long) + long_size;
  5026. pevent->old_format = 1;
  5027. return -1;
  5028. }
  5029. init_input_buf(buf, size);
  5030. parse_header_field("timestamp", &pevent->header_page_ts_offset,
  5031. &pevent->header_page_ts_size, 1);
  5032. parse_header_field("commit", &pevent->header_page_size_offset,
  5033. &pevent->header_page_size_size, 1);
  5034. parse_header_field("overwrite", &pevent->header_page_overwrite,
  5035. &ignore, 0);
  5036. parse_header_field("data", &pevent->header_page_data_offset,
  5037. &pevent->header_page_data_size, 1);
  5038. return 0;
  5039. }
  5040. static int event_matches(struct event_format *event,
  5041. int id, const char *sys_name,
  5042. const char *event_name)
  5043. {
  5044. if (id >= 0 && id != event->id)
  5045. return 0;
  5046. if (event_name && (strcmp(event_name, event->name) != 0))
  5047. return 0;
  5048. if (sys_name && (strcmp(sys_name, event->system) != 0))
  5049. return 0;
  5050. return 1;
  5051. }
  5052. static void free_handler(struct event_handler *handle)
  5053. {
  5054. free((void *)handle->sys_name);
  5055. free((void *)handle->event_name);
  5056. free(handle);
  5057. }
  5058. static int find_event_handle(struct pevent *pevent, struct event_format *event)
  5059. {
  5060. struct event_handler *handle, **next;
  5061. for (next = &pevent->handlers; *next;
  5062. next = &(*next)->next) {
  5063. handle = *next;
  5064. if (event_matches(event, handle->id,
  5065. handle->sys_name,
  5066. handle->event_name))
  5067. break;
  5068. }
  5069. if (!(*next))
  5070. return 0;
  5071. pr_stat("overriding event (%d) %s:%s with new print handler",
  5072. event->id, event->system, event->name);
  5073. event->handler = handle->func;
  5074. event->context = handle->context;
  5075. *next = handle->next;
  5076. free_handler(handle);
  5077. return 1;
  5078. }
  5079. /**
  5080. * __pevent_parse_format - parse the event format
  5081. * @buf: the buffer storing the event format string
  5082. * @size: the size of @buf
  5083. * @sys: the system the event belongs to
  5084. *
  5085. * This parses the event format and creates an event structure
  5086. * to quickly parse raw data for a given event.
  5087. *
  5088. * These files currently come from:
  5089. *
  5090. * /sys/kernel/debug/tracing/events/.../.../format
  5091. */
  5092. enum pevent_errno __pevent_parse_format(struct event_format **eventp,
  5093. struct pevent *pevent, const char *buf,
  5094. unsigned long size, const char *sys)
  5095. {
  5096. struct event_format *event;
  5097. int ret;
  5098. init_input_buf(buf, size);
  5099. *eventp = event = alloc_event();
  5100. if (!event)
  5101. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5102. event->name = event_read_name();
  5103. if (!event->name) {
  5104. /* Bad event? */
  5105. ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5106. goto event_alloc_failed;
  5107. }
  5108. if (strcmp(sys, "ftrace") == 0) {
  5109. event->flags |= EVENT_FL_ISFTRACE;
  5110. if (strcmp(event->name, "bprint") == 0)
  5111. event->flags |= EVENT_FL_ISBPRINT;
  5112. }
  5113. event->id = event_read_id();
  5114. if (event->id < 0) {
  5115. ret = PEVENT_ERRNO__READ_ID_FAILED;
  5116. /*
  5117. * This isn't an allocation error actually.
  5118. * But as the ID is critical, just bail out.
  5119. */
  5120. goto event_alloc_failed;
  5121. }
  5122. event->system = strdup(sys);
  5123. if (!event->system) {
  5124. ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5125. goto event_alloc_failed;
  5126. }
  5127. /* Add pevent to event so that it can be referenced */
  5128. event->pevent = pevent;
  5129. ret = event_read_format(event);
  5130. if (ret < 0) {
  5131. ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
  5132. goto event_parse_failed;
  5133. }
  5134. /*
  5135. * If the event has an override, don't print warnings if the event
  5136. * print format fails to parse.
  5137. */
  5138. if (pevent && find_event_handle(pevent, event))
  5139. show_warning = 0;
  5140. ret = event_read_print(event);
  5141. show_warning = 1;
  5142. if (ret < 0) {
  5143. ret = PEVENT_ERRNO__READ_PRINT_FAILED;
  5144. goto event_parse_failed;
  5145. }
  5146. if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
  5147. struct format_field *field;
  5148. struct print_arg *arg, **list;
  5149. /* old ftrace had no args */
  5150. list = &event->print_fmt.args;
  5151. for (field = event->format.fields; field; field = field->next) {
  5152. arg = alloc_arg();
  5153. if (!arg) {
  5154. event->flags |= EVENT_FL_FAILED;
  5155. return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
  5156. }
  5157. arg->type = PRINT_FIELD;
  5158. arg->field.name = strdup(field->name);
  5159. if (!arg->field.name) {
  5160. event->flags |= EVENT_FL_FAILED;
  5161. free_arg(arg);
  5162. return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
  5163. }
  5164. arg->field.field = field;
  5165. *list = arg;
  5166. list = &arg->next;
  5167. }
  5168. return 0;
  5169. }
  5170. return 0;
  5171. event_parse_failed:
  5172. event->flags |= EVENT_FL_FAILED;
  5173. return ret;
  5174. event_alloc_failed:
  5175. free(event->system);
  5176. free(event->name);
  5177. free(event);
  5178. *eventp = NULL;
  5179. return ret;
  5180. }
  5181. static enum pevent_errno
  5182. __pevent_parse_event(struct pevent *pevent,
  5183. struct event_format **eventp,
  5184. const char *buf, unsigned long size,
  5185. const char *sys)
  5186. {
  5187. int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
  5188. struct event_format *event = *eventp;
  5189. if (event == NULL)
  5190. return ret;
  5191. if (pevent && add_event(pevent, event)) {
  5192. ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5193. goto event_add_failed;
  5194. }
  5195. #define PRINT_ARGS 0
  5196. if (PRINT_ARGS && event->print_fmt.args)
  5197. print_args(event->print_fmt.args);
  5198. return 0;
  5199. event_add_failed:
  5200. pevent_free_format(event);
  5201. return ret;
  5202. }
  5203. /**
  5204. * pevent_parse_format - parse the event format
  5205. * @pevent: the handle to the pevent
  5206. * @eventp: returned format
  5207. * @buf: the buffer storing the event format string
  5208. * @size: the size of @buf
  5209. * @sys: the system the event belongs to
  5210. *
  5211. * This parses the event format and creates an event structure
  5212. * to quickly parse raw data for a given event.
  5213. *
  5214. * These files currently come from:
  5215. *
  5216. * /sys/kernel/debug/tracing/events/.../.../format
  5217. */
  5218. enum pevent_errno pevent_parse_format(struct pevent *pevent,
  5219. struct event_format **eventp,
  5220. const char *buf,
  5221. unsigned long size, const char *sys)
  5222. {
  5223. return __pevent_parse_event(pevent, eventp, buf, size, sys);
  5224. }
  5225. /**
  5226. * pevent_parse_event - parse the event format
  5227. * @pevent: the handle to the pevent
  5228. * @buf: the buffer storing the event format string
  5229. * @size: the size of @buf
  5230. * @sys: the system the event belongs to
  5231. *
  5232. * This parses the event format and creates an event structure
  5233. * to quickly parse raw data for a given event.
  5234. *
  5235. * These files currently come from:
  5236. *
  5237. * /sys/kernel/debug/tracing/events/.../.../format
  5238. */
  5239. enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
  5240. unsigned long size, const char *sys)
  5241. {
  5242. struct event_format *event = NULL;
  5243. return __pevent_parse_event(pevent, &event, buf, size, sys);
  5244. }
  5245. #undef _PE
  5246. #define _PE(code, str) str
  5247. static const char * const pevent_error_str[] = {
  5248. PEVENT_ERRORS
  5249. };
  5250. #undef _PE
  5251. int pevent_strerror(struct pevent *pevent __maybe_unused,
  5252. enum pevent_errno errnum, char *buf, size_t buflen)
  5253. {
  5254. int idx;
  5255. const char *msg;
  5256. if (errnum >= 0) {
  5257. str_error_r(errnum, buf, buflen);
  5258. return 0;
  5259. }
  5260. if (errnum <= __PEVENT_ERRNO__START ||
  5261. errnum >= __PEVENT_ERRNO__END)
  5262. return -1;
  5263. idx = errnum - __PEVENT_ERRNO__START - 1;
  5264. msg = pevent_error_str[idx];
  5265. snprintf(buf, buflen, "%s", msg);
  5266. return 0;
  5267. }
  5268. int get_field_val(struct trace_seq *s, struct format_field *field,
  5269. const char *name, struct pevent_record *record,
  5270. unsigned long long *val, int err)
  5271. {
  5272. if (!field) {
  5273. if (err)
  5274. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  5275. return -1;
  5276. }
  5277. if (pevent_read_number_field(field, record->data, val)) {
  5278. if (err)
  5279. trace_seq_printf(s, " %s=INVALID", name);
  5280. return -1;
  5281. }
  5282. return 0;
  5283. }
  5284. /**
  5285. * pevent_get_field_raw - return the raw pointer into the data field
  5286. * @s: The seq to print to on error
  5287. * @event: the event that the field is for
  5288. * @name: The name of the field
  5289. * @record: The record with the field name.
  5290. * @len: place to store the field length.
  5291. * @err: print default error if failed.
  5292. *
  5293. * Returns a pointer into record->data of the field and places
  5294. * the length of the field in @len.
  5295. *
  5296. * On failure, it returns NULL.
  5297. */
  5298. void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
  5299. const char *name, struct pevent_record *record,
  5300. int *len, int err)
  5301. {
  5302. struct format_field *field;
  5303. void *data = record->data;
  5304. unsigned offset;
  5305. int dummy;
  5306. if (!event)
  5307. return NULL;
  5308. field = pevent_find_field(event, name);
  5309. if (!field) {
  5310. if (err)
  5311. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  5312. return NULL;
  5313. }
  5314. /* Allow @len to be NULL */
  5315. if (!len)
  5316. len = &dummy;
  5317. offset = field->offset;
  5318. if (field->flags & FIELD_IS_DYNAMIC) {
  5319. offset = pevent_read_number(event->pevent,
  5320. data + offset, field->size);
  5321. *len = offset >> 16;
  5322. offset &= 0xffff;
  5323. } else
  5324. *len = field->size;
  5325. return data + offset;
  5326. }
  5327. /**
  5328. * pevent_get_field_val - find a field and return its value
  5329. * @s: The seq to print to on error
  5330. * @event: the event that the field is for
  5331. * @name: The name of the field
  5332. * @record: The record with the field name.
  5333. * @val: place to store the value of the field.
  5334. * @err: print default error if failed.
  5335. *
  5336. * Returns 0 on success -1 on field not found.
  5337. */
  5338. int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
  5339. const char *name, struct pevent_record *record,
  5340. unsigned long long *val, int err)
  5341. {
  5342. struct format_field *field;
  5343. if (!event)
  5344. return -1;
  5345. field = pevent_find_field(event, name);
  5346. return get_field_val(s, field, name, record, val, err);
  5347. }
  5348. /**
  5349. * pevent_get_common_field_val - find a common field and return its value
  5350. * @s: The seq to print to on error
  5351. * @event: the event that the field is for
  5352. * @name: The name of the field
  5353. * @record: The record with the field name.
  5354. * @val: place to store the value of the field.
  5355. * @err: print default error if failed.
  5356. *
  5357. * Returns 0 on success -1 on field not found.
  5358. */
  5359. int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
  5360. const char *name, struct pevent_record *record,
  5361. unsigned long long *val, int err)
  5362. {
  5363. struct format_field *field;
  5364. if (!event)
  5365. return -1;
  5366. field = pevent_find_common_field(event, name);
  5367. return get_field_val(s, field, name, record, val, err);
  5368. }
  5369. /**
  5370. * pevent_get_any_field_val - find a any field and return its value
  5371. * @s: The seq to print to on error
  5372. * @event: the event that the field is for
  5373. * @name: The name of the field
  5374. * @record: The record with the field name.
  5375. * @val: place to store the value of the field.
  5376. * @err: print default error if failed.
  5377. *
  5378. * Returns 0 on success -1 on field not found.
  5379. */
  5380. int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
  5381. const char *name, struct pevent_record *record,
  5382. unsigned long long *val, int err)
  5383. {
  5384. struct format_field *field;
  5385. if (!event)
  5386. return -1;
  5387. field = pevent_find_any_field(event, name);
  5388. return get_field_val(s, field, name, record, val, err);
  5389. }
  5390. /**
  5391. * pevent_print_num_field - print a field and a format
  5392. * @s: The seq to print to
  5393. * @fmt: The printf format to print the field with.
  5394. * @event: the event that the field is for
  5395. * @name: The name of the field
  5396. * @record: The record with the field name.
  5397. * @err: print default error if failed.
  5398. *
  5399. * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
  5400. */
  5401. int pevent_print_num_field(struct trace_seq *s, const char *fmt,
  5402. struct event_format *event, const char *name,
  5403. struct pevent_record *record, int err)
  5404. {
  5405. struct format_field *field = pevent_find_field(event, name);
  5406. unsigned long long val;
  5407. if (!field)
  5408. goto failed;
  5409. if (pevent_read_number_field(field, record->data, &val))
  5410. goto failed;
  5411. return trace_seq_printf(s, fmt, val);
  5412. failed:
  5413. if (err)
  5414. trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
  5415. return -1;
  5416. }
  5417. /**
  5418. * pevent_print_func_field - print a field and a format for function pointers
  5419. * @s: The seq to print to
  5420. * @fmt: The printf format to print the field with.
  5421. * @event: the event that the field is for
  5422. * @name: The name of the field
  5423. * @record: The record with the field name.
  5424. * @err: print default error if failed.
  5425. *
  5426. * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
  5427. */
  5428. int pevent_print_func_field(struct trace_seq *s, const char *fmt,
  5429. struct event_format *event, const char *name,
  5430. struct pevent_record *record, int err)
  5431. {
  5432. struct format_field *field = pevent_find_field(event, name);
  5433. struct pevent *pevent = event->pevent;
  5434. unsigned long long val;
  5435. struct func_map *func;
  5436. char tmp[128];
  5437. if (!field)
  5438. goto failed;
  5439. if (pevent_read_number_field(field, record->data, &val))
  5440. goto failed;
  5441. func = find_func(pevent, val);
  5442. if (func)
  5443. snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
  5444. else
  5445. sprintf(tmp, "0x%08llx", val);
  5446. return trace_seq_printf(s, fmt, tmp);
  5447. failed:
  5448. if (err)
  5449. trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
  5450. return -1;
  5451. }
  5452. static void free_func_handle(struct pevent_function_handler *func)
  5453. {
  5454. struct pevent_func_params *params;
  5455. free(func->name);
  5456. while (func->params) {
  5457. params = func->params;
  5458. func->params = params->next;
  5459. free(params);
  5460. }
  5461. free(func);
  5462. }
  5463. /**
  5464. * pevent_register_print_function - register a helper function
  5465. * @pevent: the handle to the pevent
  5466. * @func: the function to process the helper function
  5467. * @ret_type: the return type of the helper function
  5468. * @name: the name of the helper function
  5469. * @parameters: A list of enum pevent_func_arg_type
  5470. *
  5471. * Some events may have helper functions in the print format arguments.
  5472. * This allows a plugin to dynamically create a way to process one
  5473. * of these functions.
  5474. *
  5475. * The @parameters is a variable list of pevent_func_arg_type enums that
  5476. * must end with PEVENT_FUNC_ARG_VOID.
  5477. */
  5478. int pevent_register_print_function(struct pevent *pevent,
  5479. pevent_func_handler func,
  5480. enum pevent_func_arg_type ret_type,
  5481. char *name, ...)
  5482. {
  5483. struct pevent_function_handler *func_handle;
  5484. struct pevent_func_params **next_param;
  5485. struct pevent_func_params *param;
  5486. enum pevent_func_arg_type type;
  5487. va_list ap;
  5488. int ret;
  5489. func_handle = find_func_handler(pevent, name);
  5490. if (func_handle) {
  5491. /*
  5492. * This is most like caused by the users own
  5493. * plugins updating the function. This overrides the
  5494. * system defaults.
  5495. */
  5496. pr_stat("override of function helper '%s'", name);
  5497. remove_func_handler(pevent, name);
  5498. }
  5499. func_handle = calloc(1, sizeof(*func_handle));
  5500. if (!func_handle) {
  5501. do_warning("Failed to allocate function handler");
  5502. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5503. }
  5504. func_handle->ret_type = ret_type;
  5505. func_handle->name = strdup(name);
  5506. func_handle->func = func;
  5507. if (!func_handle->name) {
  5508. do_warning("Failed to allocate function name");
  5509. free(func_handle);
  5510. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5511. }
  5512. next_param = &(func_handle->params);
  5513. va_start(ap, name);
  5514. for (;;) {
  5515. type = va_arg(ap, enum pevent_func_arg_type);
  5516. if (type == PEVENT_FUNC_ARG_VOID)
  5517. break;
  5518. if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
  5519. do_warning("Invalid argument type %d", type);
  5520. ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
  5521. goto out_free;
  5522. }
  5523. param = malloc(sizeof(*param));
  5524. if (!param) {
  5525. do_warning("Failed to allocate function param");
  5526. ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5527. goto out_free;
  5528. }
  5529. param->type = type;
  5530. param->next = NULL;
  5531. *next_param = param;
  5532. next_param = &(param->next);
  5533. func_handle->nr_args++;
  5534. }
  5535. va_end(ap);
  5536. func_handle->next = pevent->func_handlers;
  5537. pevent->func_handlers = func_handle;
  5538. return 0;
  5539. out_free:
  5540. va_end(ap);
  5541. free_func_handle(func_handle);
  5542. return ret;
  5543. }
  5544. /**
  5545. * pevent_unregister_print_function - unregister a helper function
  5546. * @pevent: the handle to the pevent
  5547. * @func: the function to process the helper function
  5548. * @name: the name of the helper function
  5549. *
  5550. * This function removes existing print handler for function @name.
  5551. *
  5552. * Returns 0 if the handler was removed successully, -1 otherwise.
  5553. */
  5554. int pevent_unregister_print_function(struct pevent *pevent,
  5555. pevent_func_handler func, char *name)
  5556. {
  5557. struct pevent_function_handler *func_handle;
  5558. func_handle = find_func_handler(pevent, name);
  5559. if (func_handle && func_handle->func == func) {
  5560. remove_func_handler(pevent, name);
  5561. return 0;
  5562. }
  5563. return -1;
  5564. }
  5565. static struct event_format *pevent_search_event(struct pevent *pevent, int id,
  5566. const char *sys_name,
  5567. const char *event_name)
  5568. {
  5569. struct event_format *event;
  5570. if (id >= 0) {
  5571. /* search by id */
  5572. event = pevent_find_event(pevent, id);
  5573. if (!event)
  5574. return NULL;
  5575. if (event_name && (strcmp(event_name, event->name) != 0))
  5576. return NULL;
  5577. if (sys_name && (strcmp(sys_name, event->system) != 0))
  5578. return NULL;
  5579. } else {
  5580. event = pevent_find_event_by_name(pevent, sys_name, event_name);
  5581. if (!event)
  5582. return NULL;
  5583. }
  5584. return event;
  5585. }
  5586. /**
  5587. * pevent_register_event_handler - register a way to parse an event
  5588. * @pevent: the handle to the pevent
  5589. * @id: the id of the event to register
  5590. * @sys_name: the system name the event belongs to
  5591. * @event_name: the name of the event
  5592. * @func: the function to call to parse the event information
  5593. * @context: the data to be passed to @func
  5594. *
  5595. * This function allows a developer to override the parsing of
  5596. * a given event. If for some reason the default print format
  5597. * is not sufficient, this function will register a function
  5598. * for an event to be used to parse the data instead.
  5599. *
  5600. * If @id is >= 0, then it is used to find the event.
  5601. * else @sys_name and @event_name are used.
  5602. */
  5603. int pevent_register_event_handler(struct pevent *pevent, int id,
  5604. const char *sys_name, const char *event_name,
  5605. pevent_event_handler_func func, void *context)
  5606. {
  5607. struct event_format *event;
  5608. struct event_handler *handle;
  5609. event = pevent_search_event(pevent, id, sys_name, event_name);
  5610. if (event == NULL)
  5611. goto not_found;
  5612. pr_stat("overriding event (%d) %s:%s with new print handler",
  5613. event->id, event->system, event->name);
  5614. event->handler = func;
  5615. event->context = context;
  5616. return 0;
  5617. not_found:
  5618. /* Save for later use. */
  5619. handle = calloc(1, sizeof(*handle));
  5620. if (!handle) {
  5621. do_warning("Failed to allocate event handler");
  5622. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5623. }
  5624. handle->id = id;
  5625. if (event_name)
  5626. handle->event_name = strdup(event_name);
  5627. if (sys_name)
  5628. handle->sys_name = strdup(sys_name);
  5629. if ((event_name && !handle->event_name) ||
  5630. (sys_name && !handle->sys_name)) {
  5631. do_warning("Failed to allocate event/sys name");
  5632. free((void *)handle->event_name);
  5633. free((void *)handle->sys_name);
  5634. free(handle);
  5635. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  5636. }
  5637. handle->func = func;
  5638. handle->next = pevent->handlers;
  5639. pevent->handlers = handle;
  5640. handle->context = context;
  5641. return -1;
  5642. }
  5643. static int handle_matches(struct event_handler *handler, int id,
  5644. const char *sys_name, const char *event_name,
  5645. pevent_event_handler_func func, void *context)
  5646. {
  5647. if (id >= 0 && id != handler->id)
  5648. return 0;
  5649. if (event_name && (strcmp(event_name, handler->event_name) != 0))
  5650. return 0;
  5651. if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
  5652. return 0;
  5653. if (func != handler->func || context != handler->context)
  5654. return 0;
  5655. return 1;
  5656. }
  5657. /**
  5658. * pevent_unregister_event_handler - unregister an existing event handler
  5659. * @pevent: the handle to the pevent
  5660. * @id: the id of the event to unregister
  5661. * @sys_name: the system name the handler belongs to
  5662. * @event_name: the name of the event handler
  5663. * @func: the function to call to parse the event information
  5664. * @context: the data to be passed to @func
  5665. *
  5666. * This function removes existing event handler (parser).
  5667. *
  5668. * If @id is >= 0, then it is used to find the event.
  5669. * else @sys_name and @event_name are used.
  5670. *
  5671. * Returns 0 if handler was removed successfully, -1 if event was not found.
  5672. */
  5673. int pevent_unregister_event_handler(struct pevent *pevent, int id,
  5674. const char *sys_name, const char *event_name,
  5675. pevent_event_handler_func func, void *context)
  5676. {
  5677. struct event_format *event;
  5678. struct event_handler *handle;
  5679. struct event_handler **next;
  5680. event = pevent_search_event(pevent, id, sys_name, event_name);
  5681. if (event == NULL)
  5682. goto not_found;
  5683. if (event->handler == func && event->context == context) {
  5684. pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
  5685. event->id, event->system, event->name);
  5686. event->handler = NULL;
  5687. event->context = NULL;
  5688. return 0;
  5689. }
  5690. not_found:
  5691. for (next = &pevent->handlers; *next; next = &(*next)->next) {
  5692. handle = *next;
  5693. if (handle_matches(handle, id, sys_name, event_name,
  5694. func, context))
  5695. break;
  5696. }
  5697. if (!(*next))
  5698. return -1;
  5699. *next = handle->next;
  5700. free_handler(handle);
  5701. return 0;
  5702. }
  5703. /**
  5704. * pevent_alloc - create a pevent handle
  5705. */
  5706. struct pevent *pevent_alloc(void)
  5707. {
  5708. struct pevent *pevent = calloc(1, sizeof(*pevent));
  5709. if (pevent)
  5710. pevent->ref_count = 1;
  5711. return pevent;
  5712. }
  5713. void pevent_ref(struct pevent *pevent)
  5714. {
  5715. pevent->ref_count++;
  5716. }
  5717. void pevent_free_format_field(struct format_field *field)
  5718. {
  5719. free(field->type);
  5720. if (field->alias != field->name)
  5721. free(field->alias);
  5722. free(field->name);
  5723. free(field);
  5724. }
  5725. static void free_format_fields(struct format_field *field)
  5726. {
  5727. struct format_field *next;
  5728. while (field) {
  5729. next = field->next;
  5730. pevent_free_format_field(field);
  5731. field = next;
  5732. }
  5733. }
  5734. static void free_formats(struct format *format)
  5735. {
  5736. free_format_fields(format->common_fields);
  5737. free_format_fields(format->fields);
  5738. }
  5739. void pevent_free_format(struct event_format *event)
  5740. {
  5741. free(event->name);
  5742. free(event->system);
  5743. free_formats(&event->format);
  5744. free(event->print_fmt.format);
  5745. free_args(event->print_fmt.args);
  5746. free(event);
  5747. }
  5748. /**
  5749. * pevent_free - free a pevent handle
  5750. * @pevent: the pevent handle to free
  5751. */
  5752. void pevent_free(struct pevent *pevent)
  5753. {
  5754. struct cmdline_list *cmdlist, *cmdnext;
  5755. struct func_list *funclist, *funcnext;
  5756. struct printk_list *printklist, *printknext;
  5757. struct pevent_function_handler *func_handler;
  5758. struct event_handler *handle;
  5759. int i;
  5760. if (!pevent)
  5761. return;
  5762. cmdlist = pevent->cmdlist;
  5763. funclist = pevent->funclist;
  5764. printklist = pevent->printklist;
  5765. pevent->ref_count--;
  5766. if (pevent->ref_count)
  5767. return;
  5768. if (pevent->cmdlines) {
  5769. for (i = 0; i < pevent->cmdline_count; i++)
  5770. free(pevent->cmdlines[i].comm);
  5771. free(pevent->cmdlines);
  5772. }
  5773. while (cmdlist) {
  5774. cmdnext = cmdlist->next;
  5775. free(cmdlist->comm);
  5776. free(cmdlist);
  5777. cmdlist = cmdnext;
  5778. }
  5779. if (pevent->func_map) {
  5780. for (i = 0; i < (int)pevent->func_count; i++) {
  5781. free(pevent->func_map[i].func);
  5782. free(pevent->func_map[i].mod);
  5783. }
  5784. free(pevent->func_map);
  5785. }
  5786. while (funclist) {
  5787. funcnext = funclist->next;
  5788. free(funclist->func);
  5789. free(funclist->mod);
  5790. free(funclist);
  5791. funclist = funcnext;
  5792. }
  5793. while (pevent->func_handlers) {
  5794. func_handler = pevent->func_handlers;
  5795. pevent->func_handlers = func_handler->next;
  5796. free_func_handle(func_handler);
  5797. }
  5798. if (pevent->printk_map) {
  5799. for (i = 0; i < (int)pevent->printk_count; i++)
  5800. free(pevent->printk_map[i].printk);
  5801. free(pevent->printk_map);
  5802. }
  5803. while (printklist) {
  5804. printknext = printklist->next;
  5805. free(printklist->printk);
  5806. free(printklist);
  5807. printklist = printknext;
  5808. }
  5809. for (i = 0; i < pevent->nr_events; i++)
  5810. pevent_free_format(pevent->events[i]);
  5811. while (pevent->handlers) {
  5812. handle = pevent->handlers;
  5813. pevent->handlers = handle->next;
  5814. free_handler(handle);
  5815. }
  5816. free(pevent->trace_clock);
  5817. free(pevent->events);
  5818. free(pevent->sort_events);
  5819. free(pevent->func_resolver);
  5820. free(pevent);
  5821. }
  5822. void pevent_unref(struct pevent *pevent)
  5823. {
  5824. pevent_free(pevent);
  5825. }