event-parse.c 146 KB

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