event-parse.c 146 KB

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