event-parse.c 147 KB

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