event-parse.c 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892
  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. pevent->cmdlines = cmdlines;
  213. cmdlines[pevent->cmdline_count].comm = strdup(comm);
  214. if (!cmdlines[pevent->cmdline_count].comm) {
  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. return 0;
  223. }
  224. /**
  225. * tep_register_comm - register a pid / comm mapping
  226. * @pevent: handle for the pevent
  227. * @comm: the command line to register
  228. * @pid: the pid to map the command line to
  229. *
  230. * This adds a mapping to search for command line names with
  231. * a given pid. The comm is duplicated.
  232. */
  233. int tep_register_comm(struct tep_handle *pevent, const char *comm, int pid)
  234. {
  235. struct cmdline_list *item;
  236. if (pevent->cmdlines)
  237. return add_new_comm(pevent, comm, pid);
  238. item = malloc(sizeof(*item));
  239. if (!item)
  240. return -1;
  241. if (comm)
  242. item->comm = strdup(comm);
  243. else
  244. item->comm = strdup("<...>");
  245. if (!item->comm) {
  246. free(item);
  247. return -1;
  248. }
  249. item->pid = pid;
  250. item->next = pevent->cmdlist;
  251. pevent->cmdlist = item;
  252. pevent->cmdline_count++;
  253. return 0;
  254. }
  255. int tep_register_trace_clock(struct tep_handle *pevent, const char *trace_clock)
  256. {
  257. pevent->trace_clock = strdup(trace_clock);
  258. if (!pevent->trace_clock) {
  259. errno = ENOMEM;
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. struct func_map {
  265. unsigned long long addr;
  266. char *func;
  267. char *mod;
  268. };
  269. struct func_list {
  270. struct func_list *next;
  271. unsigned long long addr;
  272. char *func;
  273. char *mod;
  274. };
  275. static int func_cmp(const void *a, const void *b)
  276. {
  277. const struct func_map *fa = a;
  278. const struct func_map *fb = b;
  279. if (fa->addr < fb->addr)
  280. return -1;
  281. if (fa->addr > fb->addr)
  282. return 1;
  283. return 0;
  284. }
  285. /*
  286. * We are searching for a record in between, not an exact
  287. * match.
  288. */
  289. static int func_bcmp(const void *a, const void *b)
  290. {
  291. const struct func_map *fa = a;
  292. const struct func_map *fb = b;
  293. if ((fa->addr == fb->addr) ||
  294. (fa->addr > fb->addr &&
  295. fa->addr < (fb+1)->addr))
  296. return 0;
  297. if (fa->addr < fb->addr)
  298. return -1;
  299. return 1;
  300. }
  301. static int func_map_init(struct tep_handle *pevent)
  302. {
  303. struct func_list *funclist;
  304. struct func_list *item;
  305. struct func_map *func_map;
  306. int i;
  307. func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
  308. if (!func_map)
  309. return -1;
  310. funclist = pevent->funclist;
  311. i = 0;
  312. while (funclist) {
  313. func_map[i].func = funclist->func;
  314. func_map[i].addr = funclist->addr;
  315. func_map[i].mod = funclist->mod;
  316. i++;
  317. item = funclist;
  318. funclist = funclist->next;
  319. free(item);
  320. }
  321. qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
  322. /*
  323. * Add a special record at the end.
  324. */
  325. func_map[pevent->func_count].func = NULL;
  326. func_map[pevent->func_count].addr = 0;
  327. func_map[pevent->func_count].mod = NULL;
  328. pevent->func_map = func_map;
  329. pevent->funclist = NULL;
  330. return 0;
  331. }
  332. static struct func_map *
  333. __find_func(struct tep_handle *pevent, unsigned long long addr)
  334. {
  335. struct func_map *func;
  336. struct func_map key;
  337. if (!pevent->func_map)
  338. func_map_init(pevent);
  339. key.addr = addr;
  340. func = bsearch(&key, pevent->func_map, pevent->func_count,
  341. sizeof(*pevent->func_map), func_bcmp);
  342. return func;
  343. }
  344. struct func_resolver {
  345. tep_func_resolver_t *func;
  346. void *priv;
  347. struct func_map map;
  348. };
  349. /**
  350. * tep_set_function_resolver - set an alternative function resolver
  351. * @pevent: handle for the pevent
  352. * @resolver: function to be used
  353. * @priv: resolver function private state.
  354. *
  355. * Some tools may have already a way to resolve kernel functions, allow them to
  356. * keep using it instead of duplicating all the entries inside
  357. * pevent->funclist.
  358. */
  359. int tep_set_function_resolver(struct tep_handle *pevent,
  360. tep_func_resolver_t *func, void *priv)
  361. {
  362. struct func_resolver *resolver = malloc(sizeof(*resolver));
  363. if (resolver == NULL)
  364. return -1;
  365. resolver->func = func;
  366. resolver->priv = priv;
  367. free(pevent->func_resolver);
  368. pevent->func_resolver = resolver;
  369. return 0;
  370. }
  371. /**
  372. * tep_reset_function_resolver - reset alternative function resolver
  373. * @pevent: handle for the pevent
  374. *
  375. * Stop using whatever alternative resolver was set, use the default
  376. * one instead.
  377. */
  378. void tep_reset_function_resolver(struct tep_handle *pevent)
  379. {
  380. free(pevent->func_resolver);
  381. pevent->func_resolver = NULL;
  382. }
  383. static struct func_map *
  384. find_func(struct tep_handle *pevent, unsigned long long addr)
  385. {
  386. struct func_map *map;
  387. if (!pevent->func_resolver)
  388. return __find_func(pevent, addr);
  389. map = &pevent->func_resolver->map;
  390. map->mod = NULL;
  391. map->addr = addr;
  392. map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
  393. &map->addr, &map->mod);
  394. if (map->func == NULL)
  395. return NULL;
  396. return map;
  397. }
  398. /**
  399. * tep_find_function - find a function by a given address
  400. * @pevent: handle for the pevent
  401. * @addr: the address to find the function with
  402. *
  403. * Returns a pointer to the function stored that has the given
  404. * address. Note, the address does not have to be exact, it
  405. * will select the function that would contain the address.
  406. */
  407. const char *tep_find_function(struct tep_handle *pevent, unsigned long long addr)
  408. {
  409. struct func_map *map;
  410. map = find_func(pevent, addr);
  411. if (!map)
  412. return NULL;
  413. return map->func;
  414. }
  415. /**
  416. * tep_find_function_address - find a function address by a given address
  417. * @pevent: handle for the pevent
  418. * @addr: the address to find the function with
  419. *
  420. * Returns the address the function starts at. This can be used in
  421. * conjunction with tep_find_function to print both the function
  422. * name and the function offset.
  423. */
  424. unsigned long long
  425. tep_find_function_address(struct tep_handle *pevent, unsigned long long addr)
  426. {
  427. struct func_map *map;
  428. map = find_func(pevent, addr);
  429. if (!map)
  430. return 0;
  431. return map->addr;
  432. }
  433. /**
  434. * tep_register_function - register a function with a given address
  435. * @pevent: handle for the pevent
  436. * @function: the function name to register
  437. * @addr: the address the function starts at
  438. * @mod: the kernel module the function may be in (NULL for none)
  439. *
  440. * This registers a function name with an address and module.
  441. * The @func passed in is duplicated.
  442. */
  443. int tep_register_function(struct tep_handle *pevent, char *func,
  444. unsigned long long addr, char *mod)
  445. {
  446. struct func_list *item = malloc(sizeof(*item));
  447. if (!item)
  448. return -1;
  449. item->next = pevent->funclist;
  450. item->func = strdup(func);
  451. if (!item->func)
  452. goto out_free;
  453. if (mod) {
  454. item->mod = strdup(mod);
  455. if (!item->mod)
  456. goto out_free_func;
  457. } else
  458. item->mod = NULL;
  459. item->addr = addr;
  460. pevent->funclist = item;
  461. pevent->func_count++;
  462. return 0;
  463. out_free_func:
  464. free(item->func);
  465. item->func = NULL;
  466. out_free:
  467. free(item);
  468. errno = ENOMEM;
  469. return -1;
  470. }
  471. /**
  472. * tep_print_funcs - print out the stored functions
  473. * @pevent: handle for the pevent
  474. *
  475. * This prints out the stored functions.
  476. */
  477. void tep_print_funcs(struct tep_handle *pevent)
  478. {
  479. int i;
  480. if (!pevent->func_map)
  481. func_map_init(pevent);
  482. for (i = 0; i < (int)pevent->func_count; i++) {
  483. printf("%016llx %s",
  484. pevent->func_map[i].addr,
  485. pevent->func_map[i].func);
  486. if (pevent->func_map[i].mod)
  487. printf(" [%s]\n", pevent->func_map[i].mod);
  488. else
  489. printf("\n");
  490. }
  491. }
  492. struct printk_map {
  493. unsigned long long addr;
  494. char *printk;
  495. };
  496. struct printk_list {
  497. struct printk_list *next;
  498. unsigned long long addr;
  499. char *printk;
  500. };
  501. static int printk_cmp(const void *a, const void *b)
  502. {
  503. const struct printk_map *pa = a;
  504. const struct printk_map *pb = b;
  505. if (pa->addr < pb->addr)
  506. return -1;
  507. if (pa->addr > pb->addr)
  508. return 1;
  509. return 0;
  510. }
  511. static int printk_map_init(struct tep_handle *pevent)
  512. {
  513. struct printk_list *printklist;
  514. struct printk_list *item;
  515. struct printk_map *printk_map;
  516. int i;
  517. printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
  518. if (!printk_map)
  519. return -1;
  520. printklist = pevent->printklist;
  521. i = 0;
  522. while (printklist) {
  523. printk_map[i].printk = printklist->printk;
  524. printk_map[i].addr = printklist->addr;
  525. i++;
  526. item = printklist;
  527. printklist = printklist->next;
  528. free(item);
  529. }
  530. qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
  531. pevent->printk_map = printk_map;
  532. pevent->printklist = NULL;
  533. return 0;
  534. }
  535. static struct printk_map *
  536. find_printk(struct tep_handle *pevent, unsigned long long addr)
  537. {
  538. struct printk_map *printk;
  539. struct printk_map key;
  540. if (!pevent->printk_map && printk_map_init(pevent))
  541. return NULL;
  542. key.addr = addr;
  543. printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
  544. sizeof(*pevent->printk_map), printk_cmp);
  545. return printk;
  546. }
  547. /**
  548. * tep_register_print_string - register a string by its address
  549. * @pevent: handle for the pevent
  550. * @fmt: the string format to register
  551. * @addr: the address the string was located at
  552. *
  553. * This registers a string by the address it was stored in the kernel.
  554. * The @fmt passed in is duplicated.
  555. */
  556. int tep_register_print_string(struct tep_handle *pevent, const char *fmt,
  557. unsigned long long addr)
  558. {
  559. struct printk_list *item = malloc(sizeof(*item));
  560. char *p;
  561. if (!item)
  562. return -1;
  563. item->next = pevent->printklist;
  564. item->addr = addr;
  565. /* Strip off quotes and '\n' from the end */
  566. if (fmt[0] == '"')
  567. fmt++;
  568. item->printk = strdup(fmt);
  569. if (!item->printk)
  570. goto out_free;
  571. p = item->printk + strlen(item->printk) - 1;
  572. if (*p == '"')
  573. *p = 0;
  574. p -= 2;
  575. if (strcmp(p, "\\n") == 0)
  576. *p = 0;
  577. pevent->printklist = item;
  578. pevent->printk_count++;
  579. return 0;
  580. out_free:
  581. free(item);
  582. errno = ENOMEM;
  583. return -1;
  584. }
  585. /**
  586. * tep_print_printk - print out the stored strings
  587. * @pevent: handle for the pevent
  588. *
  589. * This prints the string formats that were stored.
  590. */
  591. void tep_print_printk(struct tep_handle *pevent)
  592. {
  593. int i;
  594. if (!pevent->printk_map)
  595. printk_map_init(pevent);
  596. for (i = 0; i < (int)pevent->printk_count; i++) {
  597. printf("%016llx %s\n",
  598. pevent->printk_map[i].addr,
  599. pevent->printk_map[i].printk);
  600. }
  601. }
  602. static struct event_format *alloc_event(void)
  603. {
  604. return calloc(1, sizeof(struct event_format));
  605. }
  606. static int add_event(struct tep_handle *pevent, struct event_format *event)
  607. {
  608. int i;
  609. struct event_format **events = realloc(pevent->events, sizeof(event) *
  610. (pevent->nr_events + 1));
  611. if (!events)
  612. return -1;
  613. pevent->events = events;
  614. for (i = 0; i < pevent->nr_events; i++) {
  615. if (pevent->events[i]->id > event->id)
  616. break;
  617. }
  618. if (i < pevent->nr_events)
  619. memmove(&pevent->events[i + 1],
  620. &pevent->events[i],
  621. sizeof(event) * (pevent->nr_events - i));
  622. pevent->events[i] = event;
  623. pevent->nr_events++;
  624. event->pevent = pevent;
  625. return 0;
  626. }
  627. static int event_item_type(enum event_type type)
  628. {
  629. switch (type) {
  630. case EVENT_ITEM ... EVENT_SQUOTE:
  631. return 1;
  632. case EVENT_ERROR ... EVENT_DELIM:
  633. default:
  634. return 0;
  635. }
  636. }
  637. static void free_flag_sym(struct print_flag_sym *fsym)
  638. {
  639. struct print_flag_sym *next;
  640. while (fsym) {
  641. next = fsym->next;
  642. free(fsym->value);
  643. free(fsym->str);
  644. free(fsym);
  645. fsym = next;
  646. }
  647. }
  648. static void free_arg(struct print_arg *arg)
  649. {
  650. struct print_arg *farg;
  651. if (!arg)
  652. return;
  653. switch (arg->type) {
  654. case PRINT_ATOM:
  655. free(arg->atom.atom);
  656. break;
  657. case PRINT_FIELD:
  658. free(arg->field.name);
  659. break;
  660. case PRINT_FLAGS:
  661. free_arg(arg->flags.field);
  662. free(arg->flags.delim);
  663. free_flag_sym(arg->flags.flags);
  664. break;
  665. case PRINT_SYMBOL:
  666. free_arg(arg->symbol.field);
  667. free_flag_sym(arg->symbol.symbols);
  668. break;
  669. case PRINT_HEX:
  670. case PRINT_HEX_STR:
  671. free_arg(arg->hex.field);
  672. free_arg(arg->hex.size);
  673. break;
  674. case PRINT_INT_ARRAY:
  675. free_arg(arg->int_array.field);
  676. free_arg(arg->int_array.count);
  677. free_arg(arg->int_array.el_size);
  678. break;
  679. case PRINT_TYPE:
  680. free(arg->typecast.type);
  681. free_arg(arg->typecast.item);
  682. break;
  683. case PRINT_STRING:
  684. case PRINT_BSTRING:
  685. free(arg->string.string);
  686. break;
  687. case PRINT_BITMASK:
  688. free(arg->bitmask.bitmask);
  689. break;
  690. case PRINT_DYNAMIC_ARRAY:
  691. case PRINT_DYNAMIC_ARRAY_LEN:
  692. free(arg->dynarray.index);
  693. break;
  694. case PRINT_OP:
  695. free(arg->op.op);
  696. free_arg(arg->op.left);
  697. free_arg(arg->op.right);
  698. break;
  699. case PRINT_FUNC:
  700. while (arg->func.args) {
  701. farg = arg->func.args;
  702. arg->func.args = farg->next;
  703. free_arg(farg);
  704. }
  705. break;
  706. case PRINT_NULL:
  707. default:
  708. break;
  709. }
  710. free(arg);
  711. }
  712. static enum event_type get_type(int ch)
  713. {
  714. if (ch == '\n')
  715. return EVENT_NEWLINE;
  716. if (isspace(ch))
  717. return EVENT_SPACE;
  718. if (isalnum(ch) || ch == '_')
  719. return EVENT_ITEM;
  720. if (ch == '\'')
  721. return EVENT_SQUOTE;
  722. if (ch == '"')
  723. return EVENT_DQUOTE;
  724. if (!isprint(ch))
  725. return EVENT_NONE;
  726. if (ch == '(' || ch == ')' || ch == ',')
  727. return EVENT_DELIM;
  728. return EVENT_OP;
  729. }
  730. static int __read_char(void)
  731. {
  732. if (input_buf_ptr >= input_buf_siz)
  733. return -1;
  734. return input_buf[input_buf_ptr++];
  735. }
  736. static int __peek_char(void)
  737. {
  738. if (input_buf_ptr >= input_buf_siz)
  739. return -1;
  740. return input_buf[input_buf_ptr];
  741. }
  742. /**
  743. * tep_peek_char - peek at the next character that will be read
  744. *
  745. * Returns the next character read, or -1 if end of buffer.
  746. */
  747. int tep_peek_char(void)
  748. {
  749. return __peek_char();
  750. }
  751. static int extend_token(char **tok, char *buf, int size)
  752. {
  753. char *newtok = realloc(*tok, size);
  754. if (!newtok) {
  755. free(*tok);
  756. *tok = NULL;
  757. return -1;
  758. }
  759. if (!*tok)
  760. strcpy(newtok, buf);
  761. else
  762. strcat(newtok, buf);
  763. *tok = newtok;
  764. return 0;
  765. }
  766. static enum event_type force_token(const char *str, char **tok);
  767. static enum event_type __read_token(char **tok)
  768. {
  769. char buf[BUFSIZ];
  770. int ch, last_ch, quote_ch, next_ch;
  771. int i = 0;
  772. int tok_size = 0;
  773. enum event_type type;
  774. *tok = NULL;
  775. ch = __read_char();
  776. if (ch < 0)
  777. return EVENT_NONE;
  778. type = get_type(ch);
  779. if (type == EVENT_NONE)
  780. return type;
  781. buf[i++] = ch;
  782. switch (type) {
  783. case EVENT_NEWLINE:
  784. case EVENT_DELIM:
  785. if (asprintf(tok, "%c", ch) < 0)
  786. return EVENT_ERROR;
  787. return type;
  788. case EVENT_OP:
  789. switch (ch) {
  790. case '-':
  791. next_ch = __peek_char();
  792. if (next_ch == '>') {
  793. buf[i++] = __read_char();
  794. break;
  795. }
  796. /* fall through */
  797. case '+':
  798. case '|':
  799. case '&':
  800. case '>':
  801. case '<':
  802. last_ch = ch;
  803. ch = __peek_char();
  804. if (ch != last_ch)
  805. goto test_equal;
  806. buf[i++] = __read_char();
  807. switch (last_ch) {
  808. case '>':
  809. case '<':
  810. goto test_equal;
  811. default:
  812. break;
  813. }
  814. break;
  815. case '!':
  816. case '=':
  817. goto test_equal;
  818. default: /* what should we do instead? */
  819. break;
  820. }
  821. buf[i] = 0;
  822. *tok = strdup(buf);
  823. return type;
  824. test_equal:
  825. ch = __peek_char();
  826. if (ch == '=')
  827. buf[i++] = __read_char();
  828. goto out;
  829. case EVENT_DQUOTE:
  830. case EVENT_SQUOTE:
  831. /* don't keep quotes */
  832. i--;
  833. quote_ch = ch;
  834. last_ch = 0;
  835. concat:
  836. do {
  837. if (i == (BUFSIZ - 1)) {
  838. buf[i] = 0;
  839. tok_size += BUFSIZ;
  840. if (extend_token(tok, buf, tok_size) < 0)
  841. return EVENT_NONE;
  842. i = 0;
  843. }
  844. last_ch = ch;
  845. ch = __read_char();
  846. buf[i++] = ch;
  847. /* the '\' '\' will cancel itself */
  848. if (ch == '\\' && last_ch == '\\')
  849. last_ch = 0;
  850. } while (ch != quote_ch || last_ch == '\\');
  851. /* remove the last quote */
  852. i--;
  853. /*
  854. * For strings (double quotes) check the next token.
  855. * If it is another string, concatinate the two.
  856. */
  857. if (type == EVENT_DQUOTE) {
  858. unsigned long long save_input_buf_ptr = input_buf_ptr;
  859. do {
  860. ch = __read_char();
  861. } while (isspace(ch));
  862. if (ch == '"')
  863. goto concat;
  864. input_buf_ptr = save_input_buf_ptr;
  865. }
  866. goto out;
  867. case EVENT_ERROR ... EVENT_SPACE:
  868. case EVENT_ITEM:
  869. default:
  870. break;
  871. }
  872. while (get_type(__peek_char()) == type) {
  873. if (i == (BUFSIZ - 1)) {
  874. buf[i] = 0;
  875. tok_size += BUFSIZ;
  876. if (extend_token(tok, buf, tok_size) < 0)
  877. return EVENT_NONE;
  878. i = 0;
  879. }
  880. ch = __read_char();
  881. buf[i++] = ch;
  882. }
  883. out:
  884. buf[i] = 0;
  885. if (extend_token(tok, buf, tok_size + i + 1) < 0)
  886. return EVENT_NONE;
  887. if (type == EVENT_ITEM) {
  888. /*
  889. * Older versions of the kernel has a bug that
  890. * creates invalid symbols and will break the mac80211
  891. * parsing. This is a work around to that bug.
  892. *
  893. * See Linux kernel commit:
  894. * 811cb50baf63461ce0bdb234927046131fc7fa8b
  895. */
  896. if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
  897. free(*tok);
  898. *tok = NULL;
  899. return force_token("\"%s\" ", tok);
  900. } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
  901. free(*tok);
  902. *tok = NULL;
  903. return force_token("\" sta:%pM\" ", tok);
  904. } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
  905. free(*tok);
  906. *tok = NULL;
  907. return force_token("\" vif:%p(%d)\" ", tok);
  908. }
  909. }
  910. return type;
  911. }
  912. static enum event_type force_token(const char *str, char **tok)
  913. {
  914. const char *save_input_buf;
  915. unsigned long long save_input_buf_ptr;
  916. unsigned long long save_input_buf_siz;
  917. enum event_type type;
  918. /* save off the current input pointers */
  919. save_input_buf = input_buf;
  920. save_input_buf_ptr = input_buf_ptr;
  921. save_input_buf_siz = input_buf_siz;
  922. init_input_buf(str, strlen(str));
  923. type = __read_token(tok);
  924. /* reset back to original token */
  925. input_buf = save_input_buf;
  926. input_buf_ptr = save_input_buf_ptr;
  927. input_buf_siz = save_input_buf_siz;
  928. return type;
  929. }
  930. static void free_token(char *tok)
  931. {
  932. if (tok)
  933. free(tok);
  934. }
  935. static enum event_type read_token(char **tok)
  936. {
  937. enum event_type type;
  938. for (;;) {
  939. type = __read_token(tok);
  940. if (type != EVENT_SPACE)
  941. return type;
  942. free_token(*tok);
  943. }
  944. /* not reached */
  945. *tok = NULL;
  946. return EVENT_NONE;
  947. }
  948. /**
  949. * tep_read_token - access to utilites to use the pevent parser
  950. * @tok: The token to return
  951. *
  952. * This will parse tokens from the string given by
  953. * tep_init_data().
  954. *
  955. * Returns the token type.
  956. */
  957. enum event_type tep_read_token(char **tok)
  958. {
  959. return read_token(tok);
  960. }
  961. /**
  962. * tep_free_token - free a token returned by tep_read_token
  963. * @token: the token to free
  964. */
  965. void tep_free_token(char *token)
  966. {
  967. free_token(token);
  968. }
  969. /* no newline */
  970. static enum event_type read_token_item(char **tok)
  971. {
  972. enum event_type type;
  973. for (;;) {
  974. type = __read_token(tok);
  975. if (type != EVENT_SPACE && type != EVENT_NEWLINE)
  976. return type;
  977. free_token(*tok);
  978. *tok = NULL;
  979. }
  980. /* not reached */
  981. *tok = NULL;
  982. return EVENT_NONE;
  983. }
  984. static int test_type(enum event_type type, enum event_type expect)
  985. {
  986. if (type != expect) {
  987. do_warning("Error: expected type %d but read %d",
  988. expect, type);
  989. return -1;
  990. }
  991. return 0;
  992. }
  993. static int test_type_token(enum event_type type, const char *token,
  994. enum event_type expect, const char *expect_tok)
  995. {
  996. if (type != expect) {
  997. do_warning("Error: expected type %d but read %d",
  998. expect, type);
  999. return -1;
  1000. }
  1001. if (strcmp(token, expect_tok) != 0) {
  1002. do_warning("Error: expected '%s' but read '%s'",
  1003. expect_tok, token);
  1004. return -1;
  1005. }
  1006. return 0;
  1007. }
  1008. static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
  1009. {
  1010. enum event_type type;
  1011. if (newline_ok)
  1012. type = read_token(tok);
  1013. else
  1014. type = read_token_item(tok);
  1015. return test_type(type, expect);
  1016. }
  1017. static int read_expect_type(enum event_type expect, char **tok)
  1018. {
  1019. return __read_expect_type(expect, tok, 1);
  1020. }
  1021. static int __read_expected(enum event_type expect, const char *str,
  1022. int newline_ok)
  1023. {
  1024. enum event_type type;
  1025. char *token;
  1026. int ret;
  1027. if (newline_ok)
  1028. type = read_token(&token);
  1029. else
  1030. type = read_token_item(&token);
  1031. ret = test_type_token(type, token, expect, str);
  1032. free_token(token);
  1033. return ret;
  1034. }
  1035. static int read_expected(enum event_type expect, const char *str)
  1036. {
  1037. return __read_expected(expect, str, 1);
  1038. }
  1039. static int read_expected_item(enum event_type expect, const char *str)
  1040. {
  1041. return __read_expected(expect, str, 0);
  1042. }
  1043. static char *event_read_name(void)
  1044. {
  1045. char *token;
  1046. if (read_expected(EVENT_ITEM, "name") < 0)
  1047. return NULL;
  1048. if (read_expected(EVENT_OP, ":") < 0)
  1049. return NULL;
  1050. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1051. goto fail;
  1052. return token;
  1053. fail:
  1054. free_token(token);
  1055. return NULL;
  1056. }
  1057. static int event_read_id(void)
  1058. {
  1059. char *token;
  1060. int id;
  1061. if (read_expected_item(EVENT_ITEM, "ID") < 0)
  1062. return -1;
  1063. if (read_expected(EVENT_OP, ":") < 0)
  1064. return -1;
  1065. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1066. goto fail;
  1067. id = strtoul(token, NULL, 0);
  1068. free_token(token);
  1069. return id;
  1070. fail:
  1071. free_token(token);
  1072. return -1;
  1073. }
  1074. static int field_is_string(struct format_field *field)
  1075. {
  1076. if ((field->flags & FIELD_IS_ARRAY) &&
  1077. (strstr(field->type, "char") || strstr(field->type, "u8") ||
  1078. strstr(field->type, "s8")))
  1079. return 1;
  1080. return 0;
  1081. }
  1082. static int field_is_dynamic(struct format_field *field)
  1083. {
  1084. if (strncmp(field->type, "__data_loc", 10) == 0)
  1085. return 1;
  1086. return 0;
  1087. }
  1088. static int field_is_long(struct format_field *field)
  1089. {
  1090. /* includes long long */
  1091. if (strstr(field->type, "long"))
  1092. return 1;
  1093. return 0;
  1094. }
  1095. static unsigned int type_size(const char *name)
  1096. {
  1097. /* This covers all FIELD_IS_STRING types. */
  1098. static struct {
  1099. const char *type;
  1100. unsigned int size;
  1101. } table[] = {
  1102. { "u8", 1 },
  1103. { "u16", 2 },
  1104. { "u32", 4 },
  1105. { "u64", 8 },
  1106. { "s8", 1 },
  1107. { "s16", 2 },
  1108. { "s32", 4 },
  1109. { "s64", 8 },
  1110. { "char", 1 },
  1111. { },
  1112. };
  1113. int i;
  1114. for (i = 0; table[i].type; i++) {
  1115. if (!strcmp(table[i].type, name))
  1116. return table[i].size;
  1117. }
  1118. return 0;
  1119. }
  1120. static int event_read_fields(struct event_format *event, struct format_field **fields)
  1121. {
  1122. struct format_field *field = NULL;
  1123. enum event_type type;
  1124. char *token;
  1125. char *last_token;
  1126. int count = 0;
  1127. do {
  1128. unsigned int size_dynamic = 0;
  1129. type = read_token(&token);
  1130. if (type == EVENT_NEWLINE) {
  1131. free_token(token);
  1132. return count;
  1133. }
  1134. count++;
  1135. if (test_type_token(type, token, EVENT_ITEM, "field"))
  1136. goto fail;
  1137. free_token(token);
  1138. type = read_token(&token);
  1139. /*
  1140. * The ftrace fields may still use the "special" name.
  1141. * Just ignore it.
  1142. */
  1143. if (event->flags & EVENT_FL_ISFTRACE &&
  1144. type == EVENT_ITEM && strcmp(token, "special") == 0) {
  1145. free_token(token);
  1146. type = read_token(&token);
  1147. }
  1148. if (test_type_token(type, token, EVENT_OP, ":") < 0)
  1149. goto fail;
  1150. free_token(token);
  1151. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1152. goto fail;
  1153. last_token = token;
  1154. field = calloc(1, sizeof(*field));
  1155. if (!field)
  1156. goto fail;
  1157. field->event = event;
  1158. /* read the rest of the type */
  1159. for (;;) {
  1160. type = read_token(&token);
  1161. if (type == EVENT_ITEM ||
  1162. (type == EVENT_OP && strcmp(token, "*") == 0) ||
  1163. /*
  1164. * Some of the ftrace fields are broken and have
  1165. * an illegal "." in them.
  1166. */
  1167. (event->flags & EVENT_FL_ISFTRACE &&
  1168. type == EVENT_OP && strcmp(token, ".") == 0)) {
  1169. if (strcmp(token, "*") == 0)
  1170. field->flags |= FIELD_IS_POINTER;
  1171. if (field->type) {
  1172. char *new_type;
  1173. new_type = realloc(field->type,
  1174. strlen(field->type) +
  1175. strlen(last_token) + 2);
  1176. if (!new_type) {
  1177. free(last_token);
  1178. goto fail;
  1179. }
  1180. field->type = new_type;
  1181. strcat(field->type, " ");
  1182. strcat(field->type, last_token);
  1183. free(last_token);
  1184. } else
  1185. field->type = last_token;
  1186. last_token = token;
  1187. continue;
  1188. }
  1189. break;
  1190. }
  1191. if (!field->type) {
  1192. do_warning_event(event, "%s: no type found", __func__);
  1193. goto fail;
  1194. }
  1195. field->name = field->alias = last_token;
  1196. if (test_type(type, EVENT_OP))
  1197. goto fail;
  1198. if (strcmp(token, "[") == 0) {
  1199. enum event_type last_type = type;
  1200. char *brackets = token;
  1201. char *new_brackets;
  1202. int len;
  1203. field->flags |= FIELD_IS_ARRAY;
  1204. type = read_token(&token);
  1205. if (type == EVENT_ITEM)
  1206. field->arraylen = strtoul(token, NULL, 0);
  1207. else
  1208. field->arraylen = 0;
  1209. while (strcmp(token, "]") != 0) {
  1210. if (last_type == EVENT_ITEM &&
  1211. type == EVENT_ITEM)
  1212. len = 2;
  1213. else
  1214. len = 1;
  1215. last_type = type;
  1216. new_brackets = realloc(brackets,
  1217. strlen(brackets) +
  1218. strlen(token) + len);
  1219. if (!new_brackets) {
  1220. free(brackets);
  1221. goto fail;
  1222. }
  1223. brackets = new_brackets;
  1224. if (len == 2)
  1225. strcat(brackets, " ");
  1226. strcat(brackets, token);
  1227. /* We only care about the last token */
  1228. field->arraylen = strtoul(token, NULL, 0);
  1229. free_token(token);
  1230. type = read_token(&token);
  1231. if (type == EVENT_NONE) {
  1232. do_warning_event(event, "failed to find token");
  1233. goto fail;
  1234. }
  1235. }
  1236. free_token(token);
  1237. new_brackets = realloc(brackets, strlen(brackets) + 2);
  1238. if (!new_brackets) {
  1239. free(brackets);
  1240. goto fail;
  1241. }
  1242. brackets = new_brackets;
  1243. strcat(brackets, "]");
  1244. /* add brackets to type */
  1245. type = read_token(&token);
  1246. /*
  1247. * If the next token is not an OP, then it is of
  1248. * the format: type [] item;
  1249. */
  1250. if (type == EVENT_ITEM) {
  1251. char *new_type;
  1252. new_type = realloc(field->type,
  1253. strlen(field->type) +
  1254. strlen(field->name) +
  1255. strlen(brackets) + 2);
  1256. if (!new_type) {
  1257. free(brackets);
  1258. goto fail;
  1259. }
  1260. field->type = new_type;
  1261. strcat(field->type, " ");
  1262. strcat(field->type, field->name);
  1263. size_dynamic = type_size(field->name);
  1264. free_token(field->name);
  1265. strcat(field->type, brackets);
  1266. field->name = field->alias = token;
  1267. type = read_token(&token);
  1268. } else {
  1269. char *new_type;
  1270. new_type = realloc(field->type,
  1271. strlen(field->type) +
  1272. strlen(brackets) + 1);
  1273. if (!new_type) {
  1274. free(brackets);
  1275. goto fail;
  1276. }
  1277. field->type = new_type;
  1278. strcat(field->type, brackets);
  1279. }
  1280. free(brackets);
  1281. }
  1282. if (field_is_string(field))
  1283. field->flags |= FIELD_IS_STRING;
  1284. if (field_is_dynamic(field))
  1285. field->flags |= FIELD_IS_DYNAMIC;
  1286. if (field_is_long(field))
  1287. field->flags |= FIELD_IS_LONG;
  1288. if (test_type_token(type, token, EVENT_OP, ";"))
  1289. goto fail;
  1290. free_token(token);
  1291. if (read_expected(EVENT_ITEM, "offset") < 0)
  1292. goto fail_expect;
  1293. if (read_expected(EVENT_OP, ":") < 0)
  1294. goto fail_expect;
  1295. if (read_expect_type(EVENT_ITEM, &token))
  1296. goto fail;
  1297. field->offset = strtoul(token, NULL, 0);
  1298. free_token(token);
  1299. if (read_expected(EVENT_OP, ";") < 0)
  1300. goto fail_expect;
  1301. if (read_expected(EVENT_ITEM, "size") < 0)
  1302. goto fail_expect;
  1303. if (read_expected(EVENT_OP, ":") < 0)
  1304. goto fail_expect;
  1305. if (read_expect_type(EVENT_ITEM, &token))
  1306. goto fail;
  1307. field->size = strtoul(token, NULL, 0);
  1308. free_token(token);
  1309. if (read_expected(EVENT_OP, ";") < 0)
  1310. goto fail_expect;
  1311. type = read_token(&token);
  1312. if (type != EVENT_NEWLINE) {
  1313. /* newer versions of the kernel have a "signed" type */
  1314. if (test_type_token(type, token, EVENT_ITEM, "signed"))
  1315. goto fail;
  1316. free_token(token);
  1317. if (read_expected(EVENT_OP, ":") < 0)
  1318. goto fail_expect;
  1319. if (read_expect_type(EVENT_ITEM, &token))
  1320. goto fail;
  1321. if (strtoul(token, NULL, 0))
  1322. field->flags |= FIELD_IS_SIGNED;
  1323. free_token(token);
  1324. if (read_expected(EVENT_OP, ";") < 0)
  1325. goto fail_expect;
  1326. if (read_expect_type(EVENT_NEWLINE, &token))
  1327. goto fail;
  1328. }
  1329. free_token(token);
  1330. if (field->flags & FIELD_IS_ARRAY) {
  1331. if (field->arraylen)
  1332. field->elementsize = field->size / field->arraylen;
  1333. else if (field->flags & FIELD_IS_DYNAMIC)
  1334. field->elementsize = size_dynamic;
  1335. else if (field->flags & FIELD_IS_STRING)
  1336. field->elementsize = 1;
  1337. else if (field->flags & FIELD_IS_LONG)
  1338. field->elementsize = event->pevent ?
  1339. event->pevent->long_size :
  1340. sizeof(long);
  1341. } else
  1342. field->elementsize = field->size;
  1343. *fields = field;
  1344. fields = &field->next;
  1345. } while (1);
  1346. return 0;
  1347. fail:
  1348. free_token(token);
  1349. fail_expect:
  1350. if (field) {
  1351. free(field->type);
  1352. free(field->name);
  1353. free(field);
  1354. }
  1355. return -1;
  1356. }
  1357. static int event_read_format(struct event_format *event)
  1358. {
  1359. char *token;
  1360. int ret;
  1361. if (read_expected_item(EVENT_ITEM, "format") < 0)
  1362. return -1;
  1363. if (read_expected(EVENT_OP, ":") < 0)
  1364. return -1;
  1365. if (read_expect_type(EVENT_NEWLINE, &token))
  1366. goto fail;
  1367. free_token(token);
  1368. ret = event_read_fields(event, &event->format.common_fields);
  1369. if (ret < 0)
  1370. return ret;
  1371. event->format.nr_common = ret;
  1372. ret = event_read_fields(event, &event->format.fields);
  1373. if (ret < 0)
  1374. return ret;
  1375. event->format.nr_fields = ret;
  1376. return 0;
  1377. fail:
  1378. free_token(token);
  1379. return -1;
  1380. }
  1381. static enum event_type
  1382. process_arg_token(struct event_format *event, struct print_arg *arg,
  1383. char **tok, enum event_type type);
  1384. static enum event_type
  1385. process_arg(struct event_format *event, struct print_arg *arg, char **tok)
  1386. {
  1387. enum event_type type;
  1388. char *token;
  1389. type = read_token(&token);
  1390. *tok = token;
  1391. return process_arg_token(event, arg, tok, type);
  1392. }
  1393. static enum event_type
  1394. process_op(struct event_format *event, struct print_arg *arg, char **tok);
  1395. /*
  1396. * For __print_symbolic() and __print_flags, we need to completely
  1397. * evaluate the first argument, which defines what to print next.
  1398. */
  1399. static enum event_type
  1400. process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
  1401. {
  1402. enum event_type type;
  1403. type = process_arg(event, arg, tok);
  1404. while (type == EVENT_OP) {
  1405. type = process_op(event, arg, tok);
  1406. }
  1407. return type;
  1408. }
  1409. static enum event_type
  1410. process_cond(struct event_format *event, struct print_arg *top, char **tok)
  1411. {
  1412. struct print_arg *arg, *left, *right;
  1413. enum event_type type;
  1414. char *token = NULL;
  1415. arg = alloc_arg();
  1416. left = alloc_arg();
  1417. right = alloc_arg();
  1418. if (!arg || !left || !right) {
  1419. do_warning_event(event, "%s: not enough memory!", __func__);
  1420. /* arg will be freed at out_free */
  1421. free_arg(left);
  1422. free_arg(right);
  1423. goto out_free;
  1424. }
  1425. arg->type = PRINT_OP;
  1426. arg->op.left = left;
  1427. arg->op.right = right;
  1428. *tok = NULL;
  1429. type = process_arg(event, left, &token);
  1430. again:
  1431. if (type == EVENT_ERROR)
  1432. goto out_free;
  1433. /* Handle other operations in the arguments */
  1434. if (type == EVENT_OP && strcmp(token, ":") != 0) {
  1435. type = process_op(event, left, &token);
  1436. goto again;
  1437. }
  1438. if (test_type_token(type, token, EVENT_OP, ":"))
  1439. goto out_free;
  1440. arg->op.op = token;
  1441. type = process_arg(event, right, &token);
  1442. top->op.right = arg;
  1443. *tok = token;
  1444. return type;
  1445. out_free:
  1446. /* Top may point to itself */
  1447. top->op.right = NULL;
  1448. free_token(token);
  1449. free_arg(arg);
  1450. return EVENT_ERROR;
  1451. }
  1452. static enum event_type
  1453. process_array(struct event_format *event, struct print_arg *top, char **tok)
  1454. {
  1455. struct print_arg *arg;
  1456. enum event_type type;
  1457. char *token = NULL;
  1458. arg = alloc_arg();
  1459. if (!arg) {
  1460. do_warning_event(event, "%s: not enough memory!", __func__);
  1461. /* '*tok' is set to top->op.op. No need to free. */
  1462. *tok = NULL;
  1463. return EVENT_ERROR;
  1464. }
  1465. *tok = NULL;
  1466. type = process_arg(event, arg, &token);
  1467. if (test_type_token(type, token, EVENT_OP, "]"))
  1468. goto out_free;
  1469. top->op.right = arg;
  1470. free_token(token);
  1471. type = read_token_item(&token);
  1472. *tok = token;
  1473. return type;
  1474. out_free:
  1475. free_token(token);
  1476. free_arg(arg);
  1477. return EVENT_ERROR;
  1478. }
  1479. static int get_op_prio(char *op)
  1480. {
  1481. if (!op[1]) {
  1482. switch (op[0]) {
  1483. case '~':
  1484. case '!':
  1485. return 4;
  1486. case '*':
  1487. case '/':
  1488. case '%':
  1489. return 6;
  1490. case '+':
  1491. case '-':
  1492. return 7;
  1493. /* '>>' and '<<' are 8 */
  1494. case '<':
  1495. case '>':
  1496. return 9;
  1497. /* '==' and '!=' are 10 */
  1498. case '&':
  1499. return 11;
  1500. case '^':
  1501. return 12;
  1502. case '|':
  1503. return 13;
  1504. case '?':
  1505. return 16;
  1506. default:
  1507. do_warning("unknown op '%c'", op[0]);
  1508. return -1;
  1509. }
  1510. } else {
  1511. if (strcmp(op, "++") == 0 ||
  1512. strcmp(op, "--") == 0) {
  1513. return 3;
  1514. } else if (strcmp(op, ">>") == 0 ||
  1515. strcmp(op, "<<") == 0) {
  1516. return 8;
  1517. } else if (strcmp(op, ">=") == 0 ||
  1518. strcmp(op, "<=") == 0) {
  1519. return 9;
  1520. } else if (strcmp(op, "==") == 0 ||
  1521. strcmp(op, "!=") == 0) {
  1522. return 10;
  1523. } else if (strcmp(op, "&&") == 0) {
  1524. return 14;
  1525. } else if (strcmp(op, "||") == 0) {
  1526. return 15;
  1527. } else {
  1528. do_warning("unknown op '%s'", op);
  1529. return -1;
  1530. }
  1531. }
  1532. }
  1533. static int set_op_prio(struct print_arg *arg)
  1534. {
  1535. /* single ops are the greatest */
  1536. if (!arg->op.left || arg->op.left->type == PRINT_NULL)
  1537. arg->op.prio = 0;
  1538. else
  1539. arg->op.prio = get_op_prio(arg->op.op);
  1540. return arg->op.prio;
  1541. }
  1542. /* Note, *tok does not get freed, but will most likely be saved */
  1543. static enum event_type
  1544. process_op(struct event_format *event, struct print_arg *arg, char **tok)
  1545. {
  1546. struct print_arg *left, *right = NULL;
  1547. enum event_type type;
  1548. char *token;
  1549. /* the op is passed in via tok */
  1550. token = *tok;
  1551. if (arg->type == PRINT_OP && !arg->op.left) {
  1552. /* handle single op */
  1553. if (token[1]) {
  1554. do_warning_event(event, "bad op token %s", token);
  1555. goto out_free;
  1556. }
  1557. switch (token[0]) {
  1558. case '~':
  1559. case '!':
  1560. case '+':
  1561. case '-':
  1562. break;
  1563. default:
  1564. do_warning_event(event, "bad op token %s", token);
  1565. goto out_free;
  1566. }
  1567. /* make an empty left */
  1568. left = alloc_arg();
  1569. if (!left)
  1570. goto out_warn_free;
  1571. left->type = PRINT_NULL;
  1572. arg->op.left = left;
  1573. right = alloc_arg();
  1574. if (!right)
  1575. goto out_warn_free;
  1576. arg->op.right = right;
  1577. /* do not free the token, it belongs to an op */
  1578. *tok = NULL;
  1579. type = process_arg(event, right, tok);
  1580. } else if (strcmp(token, "?") == 0) {
  1581. left = alloc_arg();
  1582. if (!left)
  1583. goto out_warn_free;
  1584. /* copy the top arg to the left */
  1585. *left = *arg;
  1586. arg->type = PRINT_OP;
  1587. arg->op.op = token;
  1588. arg->op.left = left;
  1589. arg->op.prio = 0;
  1590. /* it will set arg->op.right */
  1591. type = process_cond(event, arg, tok);
  1592. } else if (strcmp(token, ">>") == 0 ||
  1593. 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. left = alloc_arg();
  1611. if (!left)
  1612. goto out_warn_free;
  1613. /* copy the top arg to the left */
  1614. *left = *arg;
  1615. arg->type = PRINT_OP;
  1616. arg->op.op = token;
  1617. arg->op.left = left;
  1618. arg->op.right = NULL;
  1619. if (set_op_prio(arg) == -1) {
  1620. event->flags |= EVENT_FL_FAILED;
  1621. /* arg->op.op (= token) will be freed at out_free */
  1622. arg->op.op = NULL;
  1623. goto out_free;
  1624. }
  1625. type = read_token_item(&token);
  1626. *tok = token;
  1627. /* could just be a type pointer */
  1628. if ((strcmp(arg->op.op, "*") == 0) &&
  1629. type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
  1630. char *new_atom;
  1631. if (left->type != PRINT_ATOM) {
  1632. do_warning_event(event, "bad pointer type");
  1633. goto out_free;
  1634. }
  1635. new_atom = realloc(left->atom.atom,
  1636. strlen(left->atom.atom) + 3);
  1637. if (!new_atom)
  1638. goto out_warn_free;
  1639. left->atom.atom = new_atom;
  1640. strcat(left->atom.atom, " *");
  1641. free(arg->op.op);
  1642. *arg = *left;
  1643. free(left);
  1644. return type;
  1645. }
  1646. right = alloc_arg();
  1647. if (!right)
  1648. goto out_warn_free;
  1649. type = process_arg_token(event, right, tok, type);
  1650. if (type == EVENT_ERROR) {
  1651. free_arg(right);
  1652. /* token was freed in process_arg_token() via *tok */
  1653. token = NULL;
  1654. goto out_free;
  1655. }
  1656. if (right->type == PRINT_OP &&
  1657. get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
  1658. struct print_arg tmp;
  1659. /* rotate ops according to the priority */
  1660. arg->op.right = right->op.left;
  1661. tmp = *arg;
  1662. *arg = *right;
  1663. *right = tmp;
  1664. arg->op.left = right;
  1665. } else {
  1666. arg->op.right = right;
  1667. }
  1668. } else if (strcmp(token, "[") == 0) {
  1669. left = alloc_arg();
  1670. if (!left)
  1671. goto out_warn_free;
  1672. *left = *arg;
  1673. arg->type = PRINT_OP;
  1674. arg->op.op = token;
  1675. arg->op.left = left;
  1676. arg->op.prio = 0;
  1677. /* it will set arg->op.right */
  1678. type = process_array(event, arg, tok);
  1679. } else {
  1680. do_warning_event(event, "unknown op '%s'", token);
  1681. event->flags |= EVENT_FL_FAILED;
  1682. /* the arg is now the left side */
  1683. goto out_free;
  1684. }
  1685. if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
  1686. int prio;
  1687. /* higher prios need to be closer to the root */
  1688. prio = get_op_prio(*tok);
  1689. if (prio > arg->op.prio)
  1690. return process_op(event, arg, tok);
  1691. return process_op(event, right, tok);
  1692. }
  1693. return type;
  1694. out_warn_free:
  1695. do_warning_event(event, "%s: not enough memory!", __func__);
  1696. out_free:
  1697. free_token(token);
  1698. *tok = NULL;
  1699. return EVENT_ERROR;
  1700. }
  1701. static enum event_type
  1702. process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
  1703. char **tok)
  1704. {
  1705. enum event_type type;
  1706. char *field;
  1707. char *token;
  1708. if (read_expected(EVENT_OP, "->") < 0)
  1709. goto out_err;
  1710. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1711. goto out_free;
  1712. field = token;
  1713. arg->type = PRINT_FIELD;
  1714. arg->field.name = field;
  1715. if (is_flag_field) {
  1716. arg->field.field = tep_find_any_field(event, arg->field.name);
  1717. arg->field.field->flags |= FIELD_IS_FLAG;
  1718. is_flag_field = 0;
  1719. } else if (is_symbolic_field) {
  1720. arg->field.field = tep_find_any_field(event, arg->field.name);
  1721. arg->field.field->flags |= FIELD_IS_SYMBOLIC;
  1722. is_symbolic_field = 0;
  1723. }
  1724. type = read_token(&token);
  1725. *tok = token;
  1726. return type;
  1727. out_free:
  1728. free_token(token);
  1729. out_err:
  1730. *tok = NULL;
  1731. return EVENT_ERROR;
  1732. }
  1733. static int alloc_and_process_delim(struct event_format *event, char *next_token,
  1734. struct print_arg **print_arg)
  1735. {
  1736. struct print_arg *field;
  1737. enum event_type type;
  1738. char *token;
  1739. int ret = 0;
  1740. field = alloc_arg();
  1741. if (!field) {
  1742. do_warning_event(event, "%s: not enough memory!", __func__);
  1743. errno = ENOMEM;
  1744. return -1;
  1745. }
  1746. type = process_arg(event, field, &token);
  1747. if (test_type_token(type, token, EVENT_DELIM, next_token)) {
  1748. errno = EINVAL;
  1749. ret = -1;
  1750. free_arg(field);
  1751. goto out_free_token;
  1752. }
  1753. *print_arg = field;
  1754. out_free_token:
  1755. free_token(token);
  1756. return ret;
  1757. }
  1758. static char *arg_eval (struct print_arg *arg);
  1759. static unsigned long long
  1760. eval_type_str(unsigned long long val, const char *type, int pointer)
  1761. {
  1762. int sign = 0;
  1763. char *ref;
  1764. int len;
  1765. len = strlen(type);
  1766. if (pointer) {
  1767. if (type[len-1] != '*') {
  1768. do_warning("pointer expected with non pointer type");
  1769. return val;
  1770. }
  1771. ref = malloc(len);
  1772. if (!ref) {
  1773. do_warning("%s: not enough memory!", __func__);
  1774. return val;
  1775. }
  1776. memcpy(ref, type, len);
  1777. /* chop off the " *" */
  1778. ref[len - 2] = 0;
  1779. val = eval_type_str(val, ref, 0);
  1780. free(ref);
  1781. return val;
  1782. }
  1783. /* check if this is a pointer */
  1784. if (type[len - 1] == '*')
  1785. return val;
  1786. /* Try to figure out the arg size*/
  1787. if (strncmp(type, "struct", 6) == 0)
  1788. /* all bets off */
  1789. return val;
  1790. if (strcmp(type, "u8") == 0)
  1791. return val & 0xff;
  1792. if (strcmp(type, "u16") == 0)
  1793. return val & 0xffff;
  1794. if (strcmp(type, "u32") == 0)
  1795. return val & 0xffffffff;
  1796. if (strcmp(type, "u64") == 0 ||
  1797. strcmp(type, "s64") == 0)
  1798. return val;
  1799. if (strcmp(type, "s8") == 0)
  1800. return (unsigned long long)(char)val & 0xff;
  1801. if (strcmp(type, "s16") == 0)
  1802. return (unsigned long long)(short)val & 0xffff;
  1803. if (strcmp(type, "s32") == 0)
  1804. return (unsigned long long)(int)val & 0xffffffff;
  1805. if (strncmp(type, "unsigned ", 9) == 0) {
  1806. sign = 0;
  1807. type += 9;
  1808. }
  1809. if (strcmp(type, "char") == 0) {
  1810. if (sign)
  1811. return (unsigned long long)(char)val & 0xff;
  1812. else
  1813. return val & 0xff;
  1814. }
  1815. if (strcmp(type, "short") == 0) {
  1816. if (sign)
  1817. return (unsigned long long)(short)val & 0xffff;
  1818. else
  1819. return val & 0xffff;
  1820. }
  1821. if (strcmp(type, "int") == 0) {
  1822. if (sign)
  1823. return (unsigned long long)(int)val & 0xffffffff;
  1824. else
  1825. return val & 0xffffffff;
  1826. }
  1827. return val;
  1828. }
  1829. /*
  1830. * Try to figure out the type.
  1831. */
  1832. static unsigned long long
  1833. eval_type(unsigned long long val, struct print_arg *arg, int pointer)
  1834. {
  1835. if (arg->type != PRINT_TYPE) {
  1836. do_warning("expected type argument");
  1837. return 0;
  1838. }
  1839. return eval_type_str(val, arg->typecast.type, pointer);
  1840. }
  1841. static int arg_num_eval(struct print_arg *arg, long long *val)
  1842. {
  1843. long long left, right;
  1844. int ret = 1;
  1845. switch (arg->type) {
  1846. case PRINT_ATOM:
  1847. *val = strtoll(arg->atom.atom, NULL, 0);
  1848. break;
  1849. case PRINT_TYPE:
  1850. ret = arg_num_eval(arg->typecast.item, val);
  1851. if (!ret)
  1852. break;
  1853. *val = eval_type(*val, arg, 0);
  1854. break;
  1855. case PRINT_OP:
  1856. switch (arg->op.op[0]) {
  1857. case '|':
  1858. ret = arg_num_eval(arg->op.left, &left);
  1859. if (!ret)
  1860. break;
  1861. ret = arg_num_eval(arg->op.right, &right);
  1862. if (!ret)
  1863. break;
  1864. if (arg->op.op[1])
  1865. *val = left || right;
  1866. else
  1867. *val = left | right;
  1868. break;
  1869. case '&':
  1870. ret = arg_num_eval(arg->op.left, &left);
  1871. if (!ret)
  1872. break;
  1873. ret = arg_num_eval(arg->op.right, &right);
  1874. if (!ret)
  1875. break;
  1876. if (arg->op.op[1])
  1877. *val = left && right;
  1878. else
  1879. *val = left & right;
  1880. break;
  1881. case '<':
  1882. ret = arg_num_eval(arg->op.left, &left);
  1883. if (!ret)
  1884. break;
  1885. ret = arg_num_eval(arg->op.right, &right);
  1886. if (!ret)
  1887. break;
  1888. switch (arg->op.op[1]) {
  1889. case 0:
  1890. *val = left < right;
  1891. break;
  1892. case '<':
  1893. *val = left << right;
  1894. break;
  1895. case '=':
  1896. *val = left <= right;
  1897. break;
  1898. default:
  1899. do_warning("unknown op '%s'", arg->op.op);
  1900. ret = 0;
  1901. }
  1902. break;
  1903. case '>':
  1904. ret = arg_num_eval(arg->op.left, &left);
  1905. if (!ret)
  1906. break;
  1907. ret = arg_num_eval(arg->op.right, &right);
  1908. if (!ret)
  1909. break;
  1910. switch (arg->op.op[1]) {
  1911. case 0:
  1912. *val = left > right;
  1913. break;
  1914. case '>':
  1915. *val = left >> right;
  1916. break;
  1917. case '=':
  1918. *val = left >= right;
  1919. break;
  1920. default:
  1921. do_warning("unknown op '%s'", arg->op.op);
  1922. ret = 0;
  1923. }
  1924. break;
  1925. case '=':
  1926. ret = arg_num_eval(arg->op.left, &left);
  1927. if (!ret)
  1928. break;
  1929. ret = arg_num_eval(arg->op.right, &right);
  1930. if (!ret)
  1931. break;
  1932. if (arg->op.op[1] != '=') {
  1933. do_warning("unknown op '%s'", arg->op.op);
  1934. ret = 0;
  1935. } else
  1936. *val = left == right;
  1937. break;
  1938. case '!':
  1939. ret = arg_num_eval(arg->op.left, &left);
  1940. if (!ret)
  1941. break;
  1942. ret = arg_num_eval(arg->op.right, &right);
  1943. if (!ret)
  1944. break;
  1945. switch (arg->op.op[1]) {
  1946. case '=':
  1947. *val = left != right;
  1948. break;
  1949. default:
  1950. do_warning("unknown op '%s'", arg->op.op);
  1951. ret = 0;
  1952. }
  1953. break;
  1954. case '-':
  1955. /* check for negative */
  1956. if (arg->op.left->type == PRINT_NULL)
  1957. left = 0;
  1958. else
  1959. ret = arg_num_eval(arg->op.left, &left);
  1960. if (!ret)
  1961. break;
  1962. ret = arg_num_eval(arg->op.right, &right);
  1963. if (!ret)
  1964. break;
  1965. *val = left - right;
  1966. break;
  1967. case '+':
  1968. if (arg->op.left->type == PRINT_NULL)
  1969. left = 0;
  1970. else
  1971. ret = arg_num_eval(arg->op.left, &left);
  1972. if (!ret)
  1973. break;
  1974. ret = arg_num_eval(arg->op.right, &right);
  1975. if (!ret)
  1976. break;
  1977. *val = left + right;
  1978. break;
  1979. case '~':
  1980. ret = arg_num_eval(arg->op.right, &right);
  1981. if (!ret)
  1982. break;
  1983. *val = ~right;
  1984. break;
  1985. default:
  1986. do_warning("unknown op '%s'", arg->op.op);
  1987. ret = 0;
  1988. }
  1989. break;
  1990. case PRINT_NULL:
  1991. case PRINT_FIELD ... PRINT_SYMBOL:
  1992. case PRINT_STRING:
  1993. case PRINT_BSTRING:
  1994. case PRINT_BITMASK:
  1995. default:
  1996. do_warning("invalid eval type %d", arg->type);
  1997. ret = 0;
  1998. }
  1999. return ret;
  2000. }
  2001. static char *arg_eval (struct print_arg *arg)
  2002. {
  2003. long long val;
  2004. static char buf[24];
  2005. switch (arg->type) {
  2006. case PRINT_ATOM:
  2007. return arg->atom.atom;
  2008. case PRINT_TYPE:
  2009. return arg_eval(arg->typecast.item);
  2010. case PRINT_OP:
  2011. if (!arg_num_eval(arg, &val))
  2012. break;
  2013. sprintf(buf, "%lld", val);
  2014. return buf;
  2015. case PRINT_NULL:
  2016. case PRINT_FIELD ... PRINT_SYMBOL:
  2017. case PRINT_STRING:
  2018. case PRINT_BSTRING:
  2019. case PRINT_BITMASK:
  2020. default:
  2021. do_warning("invalid eval type %d", arg->type);
  2022. break;
  2023. }
  2024. return NULL;
  2025. }
  2026. static enum event_type
  2027. process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
  2028. {
  2029. enum event_type type;
  2030. struct print_arg *arg = NULL;
  2031. struct print_flag_sym *field;
  2032. char *token = *tok;
  2033. char *value;
  2034. do {
  2035. free_token(token);
  2036. type = read_token_item(&token);
  2037. if (test_type_token(type, token, EVENT_OP, "{"))
  2038. break;
  2039. arg = alloc_arg();
  2040. if (!arg)
  2041. goto out_free;
  2042. free_token(token);
  2043. type = process_arg(event, arg, &token);
  2044. if (type == EVENT_OP)
  2045. type = process_op(event, arg, &token);
  2046. if (type == EVENT_ERROR)
  2047. goto out_free;
  2048. if (test_type_token(type, token, EVENT_DELIM, ","))
  2049. goto out_free;
  2050. field = calloc(1, sizeof(*field));
  2051. if (!field)
  2052. goto out_free;
  2053. value = arg_eval(arg);
  2054. if (value == NULL)
  2055. goto out_free_field;
  2056. field->value = strdup(value);
  2057. if (field->value == NULL)
  2058. goto out_free_field;
  2059. free_arg(arg);
  2060. arg = alloc_arg();
  2061. if (!arg)
  2062. goto out_free;
  2063. free_token(token);
  2064. type = process_arg(event, arg, &token);
  2065. if (test_type_token(type, token, EVENT_OP, "}"))
  2066. goto out_free_field;
  2067. value = arg_eval(arg);
  2068. if (value == NULL)
  2069. goto out_free_field;
  2070. field->str = strdup(value);
  2071. if (field->str == NULL)
  2072. goto out_free_field;
  2073. free_arg(arg);
  2074. arg = NULL;
  2075. *list = field;
  2076. list = &field->next;
  2077. free_token(token);
  2078. type = read_token_item(&token);
  2079. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  2080. *tok = token;
  2081. return type;
  2082. out_free_field:
  2083. free_flag_sym(field);
  2084. out_free:
  2085. free_arg(arg);
  2086. free_token(token);
  2087. *tok = NULL;
  2088. return EVENT_ERROR;
  2089. }
  2090. static enum event_type
  2091. process_flags(struct event_format *event, struct print_arg *arg, char **tok)
  2092. {
  2093. struct print_arg *field;
  2094. enum event_type type;
  2095. char *token = NULL;
  2096. memset(arg, 0, sizeof(*arg));
  2097. arg->type = PRINT_FLAGS;
  2098. field = alloc_arg();
  2099. if (!field) {
  2100. do_warning_event(event, "%s: not enough memory!", __func__);
  2101. goto out_free;
  2102. }
  2103. type = process_field_arg(event, field, &token);
  2104. /* Handle operations in the first argument */
  2105. while (type == EVENT_OP)
  2106. type = process_op(event, field, &token);
  2107. if (test_type_token(type, token, EVENT_DELIM, ","))
  2108. goto out_free_field;
  2109. free_token(token);
  2110. arg->flags.field = field;
  2111. type = read_token_item(&token);
  2112. if (event_item_type(type)) {
  2113. arg->flags.delim = token;
  2114. type = read_token_item(&token);
  2115. }
  2116. if (test_type_token(type, token, EVENT_DELIM, ","))
  2117. goto out_free;
  2118. type = process_fields(event, &arg->flags.flags, &token);
  2119. if (test_type_token(type, token, EVENT_DELIM, ")"))
  2120. goto out_free;
  2121. free_token(token);
  2122. type = read_token_item(tok);
  2123. return type;
  2124. out_free_field:
  2125. free_arg(field);
  2126. out_free:
  2127. free_token(token);
  2128. *tok = NULL;
  2129. return EVENT_ERROR;
  2130. }
  2131. static enum event_type
  2132. process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
  2133. {
  2134. struct print_arg *field;
  2135. enum event_type type;
  2136. char *token = NULL;
  2137. memset(arg, 0, sizeof(*arg));
  2138. arg->type = PRINT_SYMBOL;
  2139. field = alloc_arg();
  2140. if (!field) {
  2141. do_warning_event(event, "%s: not enough memory!", __func__);
  2142. goto out_free;
  2143. }
  2144. type = process_field_arg(event, field, &token);
  2145. if (test_type_token(type, token, EVENT_DELIM, ","))
  2146. goto out_free_field;
  2147. arg->symbol.field = field;
  2148. type = process_fields(event, &arg->symbol.symbols, &token);
  2149. if (test_type_token(type, token, EVENT_DELIM, ")"))
  2150. goto out_free;
  2151. free_token(token);
  2152. type = read_token_item(tok);
  2153. return type;
  2154. out_free_field:
  2155. free_arg(field);
  2156. out_free:
  2157. free_token(token);
  2158. *tok = NULL;
  2159. return EVENT_ERROR;
  2160. }
  2161. static enum event_type
  2162. process_hex_common(struct event_format *event, struct print_arg *arg,
  2163. char **tok, enum print_arg_type type)
  2164. {
  2165. memset(arg, 0, sizeof(*arg));
  2166. arg->type = type;
  2167. if (alloc_and_process_delim(event, ",", &arg->hex.field))
  2168. goto out;
  2169. if (alloc_and_process_delim(event, ")", &arg->hex.size))
  2170. goto free_field;
  2171. return read_token_item(tok);
  2172. free_field:
  2173. free_arg(arg->hex.field);
  2174. arg->hex.field = NULL;
  2175. out:
  2176. *tok = NULL;
  2177. return EVENT_ERROR;
  2178. }
  2179. static enum event_type
  2180. process_hex(struct event_format *event, struct print_arg *arg, char **tok)
  2181. {
  2182. return process_hex_common(event, arg, tok, PRINT_HEX);
  2183. }
  2184. static enum event_type
  2185. process_hex_str(struct event_format *event, struct print_arg *arg,
  2186. char **tok)
  2187. {
  2188. return process_hex_common(event, arg, tok, PRINT_HEX_STR);
  2189. }
  2190. static enum event_type
  2191. process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
  2192. {
  2193. memset(arg, 0, sizeof(*arg));
  2194. arg->type = PRINT_INT_ARRAY;
  2195. if (alloc_and_process_delim(event, ",", &arg->int_array.field))
  2196. goto out;
  2197. if (alloc_and_process_delim(event, ",", &arg->int_array.count))
  2198. goto free_field;
  2199. if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
  2200. goto free_size;
  2201. return read_token_item(tok);
  2202. free_size:
  2203. free_arg(arg->int_array.count);
  2204. arg->int_array.count = NULL;
  2205. free_field:
  2206. free_arg(arg->int_array.field);
  2207. arg->int_array.field = NULL;
  2208. out:
  2209. *tok = NULL;
  2210. return EVENT_ERROR;
  2211. }
  2212. static enum event_type
  2213. process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
  2214. {
  2215. struct format_field *field;
  2216. enum event_type type;
  2217. char *token;
  2218. memset(arg, 0, sizeof(*arg));
  2219. arg->type = PRINT_DYNAMIC_ARRAY;
  2220. /*
  2221. * The item within the parenthesis is another field that holds
  2222. * the index into where the array starts.
  2223. */
  2224. type = read_token(&token);
  2225. *tok = token;
  2226. if (type != EVENT_ITEM)
  2227. goto out_free;
  2228. /* Find the field */
  2229. field = tep_find_field(event, token);
  2230. if (!field)
  2231. goto out_free;
  2232. arg->dynarray.field = field;
  2233. arg->dynarray.index = 0;
  2234. if (read_expected(EVENT_DELIM, ")") < 0)
  2235. goto out_free;
  2236. free_token(token);
  2237. type = read_token_item(&token);
  2238. *tok = token;
  2239. if (type != EVENT_OP || strcmp(token, "[") != 0)
  2240. return type;
  2241. free_token(token);
  2242. arg = alloc_arg();
  2243. if (!arg) {
  2244. do_warning_event(event, "%s: not enough memory!", __func__);
  2245. *tok = NULL;
  2246. return EVENT_ERROR;
  2247. }
  2248. type = process_arg(event, arg, &token);
  2249. if (type == EVENT_ERROR)
  2250. goto out_free_arg;
  2251. if (!test_type_token(type, token, EVENT_OP, "]"))
  2252. goto out_free_arg;
  2253. free_token(token);
  2254. type = read_token_item(tok);
  2255. return type;
  2256. out_free_arg:
  2257. free_arg(arg);
  2258. out_free:
  2259. free_token(token);
  2260. *tok = NULL;
  2261. return EVENT_ERROR;
  2262. }
  2263. static enum event_type
  2264. process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
  2265. char **tok)
  2266. {
  2267. struct format_field *field;
  2268. enum event_type type;
  2269. char *token;
  2270. if (read_expect_type(EVENT_ITEM, &token) < 0)
  2271. goto out_free;
  2272. arg->type = PRINT_DYNAMIC_ARRAY_LEN;
  2273. /* Find the field */
  2274. field = tep_find_field(event, token);
  2275. if (!field)
  2276. goto out_free;
  2277. arg->dynarray.field = field;
  2278. arg->dynarray.index = 0;
  2279. if (read_expected(EVENT_DELIM, ")") < 0)
  2280. goto out_err;
  2281. free_token(token);
  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. arg = arg->next;
  4224. break;
  4225. }
  4226. if (*ptr == 'F' || *ptr == 'f' ||
  4227. *ptr == 'S' || *ptr == 's') {
  4228. show_func = *ptr;
  4229. } else if (*ptr == 'M' || *ptr == 'm') {
  4230. print_mac_arg(s, *ptr, data, size, event, arg);
  4231. arg = arg->next;
  4232. break;
  4233. } else if (*ptr == 'I' || *ptr == 'i') {
  4234. int n;
  4235. n = print_ip_arg(s, ptr, data, size, event, arg);
  4236. if (n > 0) {
  4237. ptr += n - 1;
  4238. arg = arg->next;
  4239. break;
  4240. }
  4241. }
  4242. /* fall through */
  4243. case 'd':
  4244. case 'i':
  4245. case 'x':
  4246. case 'X':
  4247. case 'u':
  4248. if (!arg) {
  4249. do_warning_event(event, "no argument match");
  4250. event->flags |= EVENT_FL_FAILED;
  4251. goto out_failed;
  4252. }
  4253. len = ((unsigned long)ptr + 1) -
  4254. (unsigned long)saveptr;
  4255. /* should never happen */
  4256. if (len > 31) {
  4257. do_warning_event(event, "bad format!");
  4258. event->flags |= EVENT_FL_FAILED;
  4259. len = 31;
  4260. }
  4261. memcpy(format, saveptr, len);
  4262. format[len] = 0;
  4263. val = eval_num_arg(data, size, event, arg);
  4264. arg = arg->next;
  4265. if (show_func) {
  4266. func = find_func(pevent, val);
  4267. if (func) {
  4268. trace_seq_puts(s, func->func);
  4269. if (show_func == 'F')
  4270. trace_seq_printf(s,
  4271. "+0x%llx",
  4272. val - func->addr);
  4273. break;
  4274. }
  4275. }
  4276. if (pevent->long_size == 8 && ls == 1 &&
  4277. sizeof(long) != 8) {
  4278. char *p;
  4279. /* make %l into %ll */
  4280. if (ls == 1 && (p = strchr(format, 'l')))
  4281. memmove(p+1, p, strlen(p)+1);
  4282. else if (strcmp(format, "%p") == 0)
  4283. strcpy(format, "0x%llx");
  4284. ls = 2;
  4285. }
  4286. switch (ls) {
  4287. case -2:
  4288. if (len_as_arg)
  4289. trace_seq_printf(s, format, len_arg, (char)val);
  4290. else
  4291. trace_seq_printf(s, format, (char)val);
  4292. break;
  4293. case -1:
  4294. if (len_as_arg)
  4295. trace_seq_printf(s, format, len_arg, (short)val);
  4296. else
  4297. trace_seq_printf(s, format, (short)val);
  4298. break;
  4299. case 0:
  4300. if (len_as_arg)
  4301. trace_seq_printf(s, format, len_arg, (int)val);
  4302. else
  4303. trace_seq_printf(s, format, (int)val);
  4304. break;
  4305. case 1:
  4306. if (len_as_arg)
  4307. trace_seq_printf(s, format, len_arg, (long)val);
  4308. else
  4309. trace_seq_printf(s, format, (long)val);
  4310. break;
  4311. case 2:
  4312. if (len_as_arg)
  4313. trace_seq_printf(s, format, len_arg,
  4314. (long long)val);
  4315. else
  4316. trace_seq_printf(s, format, (long long)val);
  4317. break;
  4318. default:
  4319. do_warning_event(event, "bad count (%d)", ls);
  4320. event->flags |= EVENT_FL_FAILED;
  4321. }
  4322. break;
  4323. case 's':
  4324. if (!arg) {
  4325. do_warning_event(event, "no matching argument");
  4326. event->flags |= EVENT_FL_FAILED;
  4327. goto out_failed;
  4328. }
  4329. len = ((unsigned long)ptr + 1) -
  4330. (unsigned long)saveptr;
  4331. /* should never happen */
  4332. if (len > 31) {
  4333. do_warning_event(event, "bad format!");
  4334. event->flags |= EVENT_FL_FAILED;
  4335. len = 31;
  4336. }
  4337. memcpy(format, saveptr, len);
  4338. format[len] = 0;
  4339. if (!len_as_arg)
  4340. len_arg = -1;
  4341. /* Use helper trace_seq */
  4342. trace_seq_init(&p);
  4343. print_str_arg(&p, data, size, event,
  4344. format, len_arg, arg);
  4345. trace_seq_terminate(&p);
  4346. trace_seq_puts(s, p.buffer);
  4347. trace_seq_destroy(&p);
  4348. arg = arg->next;
  4349. break;
  4350. default:
  4351. trace_seq_printf(s, ">%c<", *ptr);
  4352. }
  4353. } else
  4354. trace_seq_putc(s, *ptr);
  4355. }
  4356. if (event->flags & EVENT_FL_FAILED) {
  4357. out_failed:
  4358. trace_seq_printf(s, "[FAILED TO PARSE]");
  4359. }
  4360. if (args) {
  4361. free_args(args);
  4362. free(bprint_fmt);
  4363. }
  4364. }
  4365. /**
  4366. * tep_data_lat_fmt - parse the data for the latency format
  4367. * @pevent: a handle to the pevent
  4368. * @s: the trace_seq to write to
  4369. * @record: the record to read from
  4370. *
  4371. * This parses out the Latency format (interrupts disabled,
  4372. * need rescheduling, in hard/soft interrupt, preempt count
  4373. * and lock depth) and places it into the trace_seq.
  4374. */
  4375. void tep_data_lat_fmt(struct tep_handle *pevent,
  4376. struct trace_seq *s, struct tep_record *record)
  4377. {
  4378. static int check_lock_depth = 1;
  4379. static int check_migrate_disable = 1;
  4380. static int lock_depth_exists;
  4381. static int migrate_disable_exists;
  4382. unsigned int lat_flags;
  4383. unsigned int pc;
  4384. int lock_depth;
  4385. int migrate_disable;
  4386. int hardirq;
  4387. int softirq;
  4388. void *data = record->data;
  4389. lat_flags = parse_common_flags(pevent, data);
  4390. pc = parse_common_pc(pevent, data);
  4391. /* lock_depth may not always exist */
  4392. if (lock_depth_exists)
  4393. lock_depth = parse_common_lock_depth(pevent, data);
  4394. else if (check_lock_depth) {
  4395. lock_depth = parse_common_lock_depth(pevent, data);
  4396. if (lock_depth < 0)
  4397. check_lock_depth = 0;
  4398. else
  4399. lock_depth_exists = 1;
  4400. }
  4401. /* migrate_disable may not always exist */
  4402. if (migrate_disable_exists)
  4403. migrate_disable = parse_common_migrate_disable(pevent, data);
  4404. else if (check_migrate_disable) {
  4405. migrate_disable = parse_common_migrate_disable(pevent, data);
  4406. if (migrate_disable < 0)
  4407. check_migrate_disable = 0;
  4408. else
  4409. migrate_disable_exists = 1;
  4410. }
  4411. hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
  4412. softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
  4413. trace_seq_printf(s, "%c%c%c",
  4414. (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  4415. (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  4416. 'X' : '.',
  4417. (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
  4418. 'N' : '.',
  4419. (hardirq && softirq) ? 'H' :
  4420. hardirq ? 'h' : softirq ? 's' : '.');
  4421. if (pc)
  4422. trace_seq_printf(s, "%x", pc);
  4423. else
  4424. trace_seq_putc(s, '.');
  4425. if (migrate_disable_exists) {
  4426. if (migrate_disable < 0)
  4427. trace_seq_putc(s, '.');
  4428. else
  4429. trace_seq_printf(s, "%d", migrate_disable);
  4430. }
  4431. if (lock_depth_exists) {
  4432. if (lock_depth < 0)
  4433. trace_seq_putc(s, '.');
  4434. else
  4435. trace_seq_printf(s, "%d", lock_depth);
  4436. }
  4437. trace_seq_terminate(s);
  4438. }
  4439. /**
  4440. * tep_data_type - parse out the given event type
  4441. * @pevent: a handle to the pevent
  4442. * @rec: the record to read from
  4443. *
  4444. * This returns the event id from the @rec.
  4445. */
  4446. int tep_data_type(struct tep_handle *pevent, struct tep_record *rec)
  4447. {
  4448. return trace_parse_common_type(pevent, rec->data);
  4449. }
  4450. /**
  4451. * tep_data_event_from_type - find the event by a given type
  4452. * @pevent: a handle to the pevent
  4453. * @type: the type of the event.
  4454. *
  4455. * This returns the event form a given @type;
  4456. */
  4457. struct event_format *tep_data_event_from_type(struct tep_handle *pevent, int type)
  4458. {
  4459. return tep_find_event(pevent, type);
  4460. }
  4461. /**
  4462. * tep_data_pid - parse the PID from record
  4463. * @pevent: a handle to the pevent
  4464. * @rec: the record to parse
  4465. *
  4466. * This returns the PID from a record.
  4467. */
  4468. int tep_data_pid(struct tep_handle *pevent, struct tep_record *rec)
  4469. {
  4470. return parse_common_pid(pevent, rec->data);
  4471. }
  4472. /**
  4473. * tep_data_preempt_count - parse the preempt count from the record
  4474. * @pevent: a handle to the pevent
  4475. * @rec: the record to parse
  4476. *
  4477. * This returns the preempt count from a record.
  4478. */
  4479. int tep_data_preempt_count(struct tep_handle *pevent, struct tep_record *rec)
  4480. {
  4481. return parse_common_pc(pevent, rec->data);
  4482. }
  4483. /**
  4484. * tep_data_flags - parse the latency flags from the record
  4485. * @pevent: a handle to the pevent
  4486. * @rec: the record to parse
  4487. *
  4488. * This returns the latency flags from a record.
  4489. *
  4490. * Use trace_flag_type enum for the flags (see event-parse.h).
  4491. */
  4492. int tep_data_flags(struct tep_handle *pevent, struct tep_record *rec)
  4493. {
  4494. return parse_common_flags(pevent, rec->data);
  4495. }
  4496. /**
  4497. * tep_data_comm_from_pid - return the command line from PID
  4498. * @pevent: a handle to the pevent
  4499. * @pid: the PID of the task to search for
  4500. *
  4501. * This returns a pointer to the command line that has the given
  4502. * @pid.
  4503. */
  4504. const char *tep_data_comm_from_pid(struct tep_handle *pevent, int pid)
  4505. {
  4506. const char *comm;
  4507. comm = find_cmdline(pevent, pid);
  4508. return comm;
  4509. }
  4510. static struct cmdline *
  4511. pid_from_cmdlist(struct tep_handle *pevent, const char *comm, struct cmdline *next)
  4512. {
  4513. struct cmdline_list *cmdlist = (struct cmdline_list *)next;
  4514. if (cmdlist)
  4515. cmdlist = cmdlist->next;
  4516. else
  4517. cmdlist = pevent->cmdlist;
  4518. while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
  4519. cmdlist = cmdlist->next;
  4520. return (struct cmdline *)cmdlist;
  4521. }
  4522. /**
  4523. * tep_data_pid_from_comm - return the pid from a given comm
  4524. * @pevent: a handle to the pevent
  4525. * @comm: the cmdline to find the pid from
  4526. * @next: the cmdline structure to find the next comm
  4527. *
  4528. * This returns the cmdline structure that holds a pid for a given
  4529. * comm, or NULL if none found. As there may be more than one pid for
  4530. * a given comm, the result of this call can be passed back into
  4531. * a recurring call in the @next paramater, and then it will find the
  4532. * next pid.
  4533. * Also, it does a linear seach, so it may be slow.
  4534. */
  4535. struct cmdline *tep_data_pid_from_comm(struct tep_handle *pevent, const char *comm,
  4536. struct cmdline *next)
  4537. {
  4538. struct cmdline *cmdline;
  4539. /*
  4540. * If the cmdlines have not been converted yet, then use
  4541. * the list.
  4542. */
  4543. if (!pevent->cmdlines)
  4544. return pid_from_cmdlist(pevent, comm, next);
  4545. if (next) {
  4546. /*
  4547. * The next pointer could have been still from
  4548. * a previous call before cmdlines were created
  4549. */
  4550. if (next < pevent->cmdlines ||
  4551. next >= pevent->cmdlines + pevent->cmdline_count)
  4552. next = NULL;
  4553. else
  4554. cmdline = next++;
  4555. }
  4556. if (!next)
  4557. cmdline = pevent->cmdlines;
  4558. while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
  4559. if (strcmp(cmdline->comm, comm) == 0)
  4560. return cmdline;
  4561. cmdline++;
  4562. }
  4563. return NULL;
  4564. }
  4565. /**
  4566. * tep_cmdline_pid - return the pid associated to a given cmdline
  4567. * @cmdline: The cmdline structure to get the pid from
  4568. *
  4569. * Returns the pid for a give cmdline. If @cmdline is NULL, then
  4570. * -1 is returned.
  4571. */
  4572. int tep_cmdline_pid(struct tep_handle *pevent, struct cmdline *cmdline)
  4573. {
  4574. struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
  4575. if (!cmdline)
  4576. return -1;
  4577. /*
  4578. * If cmdlines have not been created yet, or cmdline is
  4579. * not part of the array, then treat it as a cmdlist instead.
  4580. */
  4581. if (!pevent->cmdlines ||
  4582. cmdline < pevent->cmdlines ||
  4583. cmdline >= pevent->cmdlines + pevent->cmdline_count)
  4584. return cmdlist->pid;
  4585. return cmdline->pid;
  4586. }
  4587. /**
  4588. * tep_event_info - parse the data into the print format
  4589. * @s: the trace_seq to write to
  4590. * @event: the handle to the event
  4591. * @record: the record to read from
  4592. *
  4593. * This parses the raw @data using the given @event information and
  4594. * writes the print format into the trace_seq.
  4595. */
  4596. void tep_event_info(struct trace_seq *s, struct event_format *event,
  4597. struct tep_record *record)
  4598. {
  4599. int print_pretty = 1;
  4600. if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
  4601. tep_print_fields(s, record->data, record->size, event);
  4602. else {
  4603. if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
  4604. print_pretty = event->handler(s, record, event,
  4605. event->context);
  4606. if (print_pretty)
  4607. pretty_print(s, record->data, record->size, event);
  4608. }
  4609. trace_seq_terminate(s);
  4610. }
  4611. static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
  4612. {
  4613. if (!use_trace_clock)
  4614. return true;
  4615. if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
  4616. || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
  4617. return true;
  4618. /* trace_clock is setting in tsc or counter mode */
  4619. return false;
  4620. }
  4621. /**
  4622. * tep_find_event_by_record - return the event from a given record
  4623. * @pevent: a handle to the pevent
  4624. * @record: The record to get the event from
  4625. *
  4626. * Returns the associated event for a given record, or NULL if non is
  4627. * is found.
  4628. */
  4629. struct event_format *
  4630. tep_find_event_by_record(struct tep_handle *pevent, struct tep_record *record)
  4631. {
  4632. int type;
  4633. if (record->size < 0) {
  4634. do_warning("ug! negative record size %d", record->size);
  4635. return NULL;
  4636. }
  4637. type = trace_parse_common_type(pevent, record->data);
  4638. return tep_find_event(pevent, type);
  4639. }
  4640. /**
  4641. * tep_print_event_task - Write the event task comm, pid and CPU
  4642. * @pevent: a handle to the pevent
  4643. * @s: the trace_seq to write to
  4644. * @event: the handle to the record's event
  4645. * @record: The record to get the event from
  4646. *
  4647. * Writes the tasks comm, pid and CPU to @s.
  4648. */
  4649. void tep_print_event_task(struct tep_handle *pevent, struct trace_seq *s,
  4650. struct event_format *event,
  4651. struct tep_record *record)
  4652. {
  4653. void *data = record->data;
  4654. const char *comm;
  4655. int pid;
  4656. pid = parse_common_pid(pevent, data);
  4657. comm = find_cmdline(pevent, pid);
  4658. if (pevent->latency_format) {
  4659. trace_seq_printf(s, "%8.8s-%-5d %3d",
  4660. comm, pid, record->cpu);
  4661. } else
  4662. trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
  4663. }
  4664. /**
  4665. * tep_print_event_time - Write the event timestamp
  4666. * @pevent: a handle to the pevent
  4667. * @s: the trace_seq to write to
  4668. * @event: the handle to the record's event
  4669. * @record: The record to get the event from
  4670. * @use_trace_clock: Set to parse according to the @pevent->trace_clock
  4671. *
  4672. * Writes the timestamp of the record into @s.
  4673. */
  4674. void tep_print_event_time(struct tep_handle *pevent, struct trace_seq *s,
  4675. struct event_format *event,
  4676. struct tep_record *record,
  4677. bool use_trace_clock)
  4678. {
  4679. unsigned long secs;
  4680. unsigned long usecs;
  4681. unsigned long nsecs;
  4682. int p;
  4683. bool use_usec_format;
  4684. use_usec_format = is_timestamp_in_us(pevent->trace_clock,
  4685. use_trace_clock);
  4686. if (use_usec_format) {
  4687. secs = record->ts / NSEC_PER_SEC;
  4688. nsecs = record->ts - secs * NSEC_PER_SEC;
  4689. }
  4690. if (pevent->latency_format) {
  4691. tep_data_lat_fmt(pevent, s, record);
  4692. }
  4693. if (use_usec_format) {
  4694. if (pevent->flags & TEP_NSEC_OUTPUT) {
  4695. usecs = nsecs;
  4696. p = 9;
  4697. } else {
  4698. usecs = (nsecs + 500) / NSEC_PER_USEC;
  4699. /* To avoid usecs larger than 1 sec */
  4700. if (usecs >= USEC_PER_SEC) {
  4701. usecs -= USEC_PER_SEC;
  4702. secs++;
  4703. }
  4704. p = 6;
  4705. }
  4706. trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
  4707. } else
  4708. trace_seq_printf(s, " %12llu:", record->ts);
  4709. }
  4710. /**
  4711. * tep_print_event_data - Write the event data section
  4712. * @pevent: a handle to the pevent
  4713. * @s: the trace_seq to write to
  4714. * @event: the handle to the record's event
  4715. * @record: The record to get the event from
  4716. *
  4717. * Writes the parsing of the record's data to @s.
  4718. */
  4719. void tep_print_event_data(struct tep_handle *pevent, struct trace_seq *s,
  4720. struct event_format *event,
  4721. struct tep_record *record)
  4722. {
  4723. static const char *spaces = " "; /* 20 spaces */
  4724. int len;
  4725. trace_seq_printf(s, " %s: ", event->name);
  4726. /* Space out the event names evenly. */
  4727. len = strlen(event->name);
  4728. if (len < 20)
  4729. trace_seq_printf(s, "%.*s", 20 - len, spaces);
  4730. tep_event_info(s, event, record);
  4731. }
  4732. void tep_print_event(struct tep_handle *pevent, struct trace_seq *s,
  4733. struct tep_record *record, bool use_trace_clock)
  4734. {
  4735. struct event_format *event;
  4736. event = tep_find_event_by_record(pevent, record);
  4737. if (!event) {
  4738. int i;
  4739. int type = trace_parse_common_type(pevent, record->data);
  4740. do_warning("ug! no event found for type %d", type);
  4741. trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
  4742. for (i = 0; i < record->size; i++)
  4743. trace_seq_printf(s, " %02x",
  4744. ((unsigned char *)record->data)[i]);
  4745. return;
  4746. }
  4747. tep_print_event_task(pevent, s, event, record);
  4748. tep_print_event_time(pevent, s, event, record, use_trace_clock);
  4749. tep_print_event_data(pevent, s, event, record);
  4750. }
  4751. static int events_id_cmp(const void *a, const void *b)
  4752. {
  4753. struct event_format * const * ea = a;
  4754. struct event_format * const * eb = b;
  4755. if ((*ea)->id < (*eb)->id)
  4756. return -1;
  4757. if ((*ea)->id > (*eb)->id)
  4758. return 1;
  4759. return 0;
  4760. }
  4761. static int events_name_cmp(const void *a, const void *b)
  4762. {
  4763. struct event_format * const * ea = a;
  4764. struct event_format * const * eb = b;
  4765. int res;
  4766. res = strcmp((*ea)->name, (*eb)->name);
  4767. if (res)
  4768. return res;
  4769. res = strcmp((*ea)->system, (*eb)->system);
  4770. if (res)
  4771. return res;
  4772. return events_id_cmp(a, b);
  4773. }
  4774. static int events_system_cmp(const void *a, const void *b)
  4775. {
  4776. struct event_format * const * ea = a;
  4777. struct event_format * const * eb = b;
  4778. int res;
  4779. res = strcmp((*ea)->system, (*eb)->system);
  4780. if (res)
  4781. return res;
  4782. res = strcmp((*ea)->name, (*eb)->name);
  4783. if (res)
  4784. return res;
  4785. return events_id_cmp(a, b);
  4786. }
  4787. struct event_format **tep_list_events(struct tep_handle *pevent, enum event_sort_type sort_type)
  4788. {
  4789. struct event_format **events;
  4790. int (*sort)(const void *a, const void *b);
  4791. events = pevent->sort_events;
  4792. if (events && pevent->last_type == sort_type)
  4793. return events;
  4794. if (!events) {
  4795. events = malloc(sizeof(*events) * (pevent->nr_events + 1));
  4796. if (!events)
  4797. return NULL;
  4798. memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
  4799. events[pevent->nr_events] = NULL;
  4800. pevent->sort_events = events;
  4801. /* the internal events are sorted by id */
  4802. if (sort_type == EVENT_SORT_ID) {
  4803. pevent->last_type = sort_type;
  4804. return events;
  4805. }
  4806. }
  4807. switch (sort_type) {
  4808. case EVENT_SORT_ID:
  4809. sort = events_id_cmp;
  4810. break;
  4811. case EVENT_SORT_NAME:
  4812. sort = events_name_cmp;
  4813. break;
  4814. case EVENT_SORT_SYSTEM:
  4815. sort = events_system_cmp;
  4816. break;
  4817. default:
  4818. return events;
  4819. }
  4820. qsort(events, pevent->nr_events, sizeof(*events), sort);
  4821. pevent->last_type = sort_type;
  4822. return events;
  4823. }
  4824. static struct format_field **
  4825. get_event_fields(const char *type, const char *name,
  4826. int count, struct format_field *list)
  4827. {
  4828. struct format_field **fields;
  4829. struct format_field *field;
  4830. int i = 0;
  4831. fields = malloc(sizeof(*fields) * (count + 1));
  4832. if (!fields)
  4833. return NULL;
  4834. for (field = list; field; field = field->next) {
  4835. fields[i++] = field;
  4836. if (i == count + 1) {
  4837. do_warning("event %s has more %s fields than specified",
  4838. name, type);
  4839. i--;
  4840. break;
  4841. }
  4842. }
  4843. if (i != count)
  4844. do_warning("event %s has less %s fields than specified",
  4845. name, type);
  4846. fields[i] = NULL;
  4847. return fields;
  4848. }
  4849. /**
  4850. * tep_event_common_fields - return a list of common fields for an event
  4851. * @event: the event to return the common fields of.
  4852. *
  4853. * Returns an allocated array of fields. The last item in the array is NULL.
  4854. * The array must be freed with free().
  4855. */
  4856. struct format_field **tep_event_common_fields(struct event_format *event)
  4857. {
  4858. return get_event_fields("common", event->name,
  4859. event->format.nr_common,
  4860. event->format.common_fields);
  4861. }
  4862. /**
  4863. * tep_event_fields - return a list of event specific fields for an event
  4864. * @event: the event to return the fields of.
  4865. *
  4866. * Returns an allocated array of fields. The last item in the array is NULL.
  4867. * The array must be freed with free().
  4868. */
  4869. struct format_field **tep_event_fields(struct event_format *event)
  4870. {
  4871. return get_event_fields("event", event->name,
  4872. event->format.nr_fields,
  4873. event->format.fields);
  4874. }
  4875. static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
  4876. {
  4877. trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
  4878. if (field->next) {
  4879. trace_seq_puts(s, ", ");
  4880. print_fields(s, field->next);
  4881. }
  4882. }
  4883. /* for debugging */
  4884. static void print_args(struct print_arg *args)
  4885. {
  4886. int print_paren = 1;
  4887. struct trace_seq s;
  4888. switch (args->type) {
  4889. case PRINT_NULL:
  4890. printf("null");
  4891. break;
  4892. case PRINT_ATOM:
  4893. printf("%s", args->atom.atom);
  4894. break;
  4895. case PRINT_FIELD:
  4896. printf("REC->%s", args->field.name);
  4897. break;
  4898. case PRINT_FLAGS:
  4899. printf("__print_flags(");
  4900. print_args(args->flags.field);
  4901. printf(", %s, ", args->flags.delim);
  4902. trace_seq_init(&s);
  4903. print_fields(&s, args->flags.flags);
  4904. trace_seq_do_printf(&s);
  4905. trace_seq_destroy(&s);
  4906. printf(")");
  4907. break;
  4908. case PRINT_SYMBOL:
  4909. printf("__print_symbolic(");
  4910. print_args(args->symbol.field);
  4911. printf(", ");
  4912. trace_seq_init(&s);
  4913. print_fields(&s, args->symbol.symbols);
  4914. trace_seq_do_printf(&s);
  4915. trace_seq_destroy(&s);
  4916. printf(")");
  4917. break;
  4918. case PRINT_HEX:
  4919. printf("__print_hex(");
  4920. print_args(args->hex.field);
  4921. printf(", ");
  4922. print_args(args->hex.size);
  4923. printf(")");
  4924. break;
  4925. case PRINT_HEX_STR:
  4926. printf("__print_hex_str(");
  4927. print_args(args->hex.field);
  4928. printf(", ");
  4929. print_args(args->hex.size);
  4930. printf(")");
  4931. break;
  4932. case PRINT_INT_ARRAY:
  4933. printf("__print_array(");
  4934. print_args(args->int_array.field);
  4935. printf(", ");
  4936. print_args(args->int_array.count);
  4937. printf(", ");
  4938. print_args(args->int_array.el_size);
  4939. printf(")");
  4940. break;
  4941. case PRINT_STRING:
  4942. case PRINT_BSTRING:
  4943. printf("__get_str(%s)", args->string.string);
  4944. break;
  4945. case PRINT_BITMASK:
  4946. printf("__get_bitmask(%s)", args->bitmask.bitmask);
  4947. break;
  4948. case PRINT_TYPE:
  4949. printf("(%s)", args->typecast.type);
  4950. print_args(args->typecast.item);
  4951. break;
  4952. case PRINT_OP:
  4953. if (strcmp(args->op.op, ":") == 0)
  4954. print_paren = 0;
  4955. if (print_paren)
  4956. printf("(");
  4957. print_args(args->op.left);
  4958. printf(" %s ", args->op.op);
  4959. print_args(args->op.right);
  4960. if (print_paren)
  4961. printf(")");
  4962. break;
  4963. default:
  4964. /* we should warn... */
  4965. return;
  4966. }
  4967. if (args->next) {
  4968. printf("\n");
  4969. print_args(args->next);
  4970. }
  4971. }
  4972. static void parse_header_field(const char *field,
  4973. int *offset, int *size, int mandatory)
  4974. {
  4975. unsigned long long save_input_buf_ptr;
  4976. unsigned long long save_input_buf_siz;
  4977. char *token;
  4978. int type;
  4979. save_input_buf_ptr = input_buf_ptr;
  4980. save_input_buf_siz = input_buf_siz;
  4981. if (read_expected(EVENT_ITEM, "field") < 0)
  4982. return;
  4983. if (read_expected(EVENT_OP, ":") < 0)
  4984. return;
  4985. /* type */
  4986. if (read_expect_type(EVENT_ITEM, &token) < 0)
  4987. goto fail;
  4988. free_token(token);
  4989. /*
  4990. * If this is not a mandatory field, then test it first.
  4991. */
  4992. if (mandatory) {
  4993. if (read_expected(EVENT_ITEM, field) < 0)
  4994. return;
  4995. } else {
  4996. if (read_expect_type(EVENT_ITEM, &token) < 0)
  4997. goto fail;
  4998. if (strcmp(token, field) != 0)
  4999. goto discard;
  5000. free_token(token);
  5001. }
  5002. if (read_expected(EVENT_OP, ";") < 0)
  5003. return;
  5004. if (read_expected(EVENT_ITEM, "offset") < 0)
  5005. return;
  5006. if (read_expected(EVENT_OP, ":") < 0)
  5007. return;
  5008. if (read_expect_type(EVENT_ITEM, &token) < 0)
  5009. goto fail;
  5010. *offset = atoi(token);
  5011. free_token(token);
  5012. if (read_expected(EVENT_OP, ";") < 0)
  5013. return;
  5014. if (read_expected(EVENT_ITEM, "size") < 0)
  5015. return;
  5016. if (read_expected(EVENT_OP, ":") < 0)
  5017. return;
  5018. if (read_expect_type(EVENT_ITEM, &token) < 0)
  5019. goto fail;
  5020. *size = atoi(token);
  5021. free_token(token);
  5022. if (read_expected(EVENT_OP, ";") < 0)
  5023. return;
  5024. type = read_token(&token);
  5025. if (type != EVENT_NEWLINE) {
  5026. /* newer versions of the kernel have a "signed" type */
  5027. if (type != EVENT_ITEM)
  5028. goto fail;
  5029. if (strcmp(token, "signed") != 0)
  5030. goto fail;
  5031. free_token(token);
  5032. if (read_expected(EVENT_OP, ":") < 0)
  5033. return;
  5034. if (read_expect_type(EVENT_ITEM, &token))
  5035. goto fail;
  5036. free_token(token);
  5037. if (read_expected(EVENT_OP, ";") < 0)
  5038. return;
  5039. if (read_expect_type(EVENT_NEWLINE, &token))
  5040. goto fail;
  5041. }
  5042. fail:
  5043. free_token(token);
  5044. return;
  5045. discard:
  5046. input_buf_ptr = save_input_buf_ptr;
  5047. input_buf_siz = save_input_buf_siz;
  5048. *offset = 0;
  5049. *size = 0;
  5050. free_token(token);
  5051. }
  5052. /**
  5053. * tep_parse_header_page - parse the data stored in the header page
  5054. * @pevent: the handle to the pevent
  5055. * @buf: the buffer storing the header page format string
  5056. * @size: the size of @buf
  5057. * @long_size: the long size to use if there is no header
  5058. *
  5059. * This parses the header page format for information on the
  5060. * ring buffer used. The @buf should be copied from
  5061. *
  5062. * /sys/kernel/debug/tracing/events/header_page
  5063. */
  5064. int tep_parse_header_page(struct tep_handle *pevent, char *buf, unsigned long size,
  5065. int long_size)
  5066. {
  5067. int ignore;
  5068. if (!size) {
  5069. /*
  5070. * Old kernels did not have header page info.
  5071. * Sorry but we just use what we find here in user space.
  5072. */
  5073. pevent->header_page_ts_size = sizeof(long long);
  5074. pevent->header_page_size_size = long_size;
  5075. pevent->header_page_data_offset = sizeof(long long) + long_size;
  5076. pevent->old_format = 1;
  5077. return -1;
  5078. }
  5079. init_input_buf(buf, size);
  5080. parse_header_field("timestamp", &pevent->header_page_ts_offset,
  5081. &pevent->header_page_ts_size, 1);
  5082. parse_header_field("commit", &pevent->header_page_size_offset,
  5083. &pevent->header_page_size_size, 1);
  5084. parse_header_field("overwrite", &pevent->header_page_overwrite,
  5085. &ignore, 0);
  5086. parse_header_field("data", &pevent->header_page_data_offset,
  5087. &pevent->header_page_data_size, 1);
  5088. return 0;
  5089. }
  5090. static int event_matches(struct event_format *event,
  5091. int id, const char *sys_name,
  5092. const char *event_name)
  5093. {
  5094. if (id >= 0 && id != event->id)
  5095. return 0;
  5096. if (event_name && (strcmp(event_name, event->name) != 0))
  5097. return 0;
  5098. if (sys_name && (strcmp(sys_name, event->system) != 0))
  5099. return 0;
  5100. return 1;
  5101. }
  5102. static void free_handler(struct event_handler *handle)
  5103. {
  5104. free((void *)handle->sys_name);
  5105. free((void *)handle->event_name);
  5106. free(handle);
  5107. }
  5108. static int find_event_handle(struct tep_handle *pevent, struct event_format *event)
  5109. {
  5110. struct event_handler *handle, **next;
  5111. for (next = &pevent->handlers; *next;
  5112. next = &(*next)->next) {
  5113. handle = *next;
  5114. if (event_matches(event, handle->id,
  5115. handle->sys_name,
  5116. handle->event_name))
  5117. break;
  5118. }
  5119. if (!(*next))
  5120. return 0;
  5121. pr_stat("overriding event (%d) %s:%s with new print handler",
  5122. event->id, event->system, event->name);
  5123. event->handler = handle->func;
  5124. event->context = handle->context;
  5125. *next = handle->next;
  5126. free_handler(handle);
  5127. return 1;
  5128. }
  5129. /**
  5130. * __tep_parse_format - parse the event format
  5131. * @buf: the buffer storing the event format string
  5132. * @size: the size of @buf
  5133. * @sys: the system the event belongs to
  5134. *
  5135. * This parses the event format and creates an event structure
  5136. * to quickly parse raw data for a given event.
  5137. *
  5138. * These files currently come from:
  5139. *
  5140. * /sys/kernel/debug/tracing/events/.../.../format
  5141. */
  5142. enum tep_errno __tep_parse_format(struct event_format **eventp,
  5143. struct tep_handle *pevent, const char *buf,
  5144. unsigned long size, const char *sys)
  5145. {
  5146. struct event_format *event;
  5147. int ret;
  5148. init_input_buf(buf, size);
  5149. *eventp = event = alloc_event();
  5150. if (!event)
  5151. return TEP_ERRNO__MEM_ALLOC_FAILED;
  5152. event->name = event_read_name();
  5153. if (!event->name) {
  5154. /* Bad event? */
  5155. ret = TEP_ERRNO__MEM_ALLOC_FAILED;
  5156. goto event_alloc_failed;
  5157. }
  5158. if (strcmp(sys, "ftrace") == 0) {
  5159. event->flags |= EVENT_FL_ISFTRACE;
  5160. if (strcmp(event->name, "bprint") == 0)
  5161. event->flags |= EVENT_FL_ISBPRINT;
  5162. }
  5163. event->id = event_read_id();
  5164. if (event->id < 0) {
  5165. ret = TEP_ERRNO__READ_ID_FAILED;
  5166. /*
  5167. * This isn't an allocation error actually.
  5168. * But as the ID is critical, just bail out.
  5169. */
  5170. goto event_alloc_failed;
  5171. }
  5172. event->system = strdup(sys);
  5173. if (!event->system) {
  5174. ret = TEP_ERRNO__MEM_ALLOC_FAILED;
  5175. goto event_alloc_failed;
  5176. }
  5177. /* Add pevent to event so that it can be referenced */
  5178. event->pevent = pevent;
  5179. ret = event_read_format(event);
  5180. if (ret < 0) {
  5181. ret = TEP_ERRNO__READ_FORMAT_FAILED;
  5182. goto event_parse_failed;
  5183. }
  5184. /*
  5185. * If the event has an override, don't print warnings if the event
  5186. * print format fails to parse.
  5187. */
  5188. if (pevent && find_event_handle(pevent, event))
  5189. show_warning = 0;
  5190. ret = event_read_print(event);
  5191. show_warning = 1;
  5192. if (ret < 0) {
  5193. ret = TEP_ERRNO__READ_PRINT_FAILED;
  5194. goto event_parse_failed;
  5195. }
  5196. if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
  5197. struct format_field *field;
  5198. struct print_arg *arg, **list;
  5199. /* old ftrace had no args */
  5200. list = &event->print_fmt.args;
  5201. for (field = event->format.fields; field; field = field->next) {
  5202. arg = alloc_arg();
  5203. if (!arg) {
  5204. event->flags |= EVENT_FL_FAILED;
  5205. return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
  5206. }
  5207. arg->type = PRINT_FIELD;
  5208. arg->field.name = strdup(field->name);
  5209. if (!arg->field.name) {
  5210. event->flags |= EVENT_FL_FAILED;
  5211. free_arg(arg);
  5212. return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
  5213. }
  5214. arg->field.field = field;
  5215. *list = arg;
  5216. list = &arg->next;
  5217. }
  5218. return 0;
  5219. }
  5220. return 0;
  5221. event_parse_failed:
  5222. event->flags |= EVENT_FL_FAILED;
  5223. return ret;
  5224. event_alloc_failed:
  5225. free(event->system);
  5226. free(event->name);
  5227. free(event);
  5228. *eventp = NULL;
  5229. return ret;
  5230. }
  5231. static enum tep_errno
  5232. __parse_event(struct tep_handle *pevent,
  5233. struct event_format **eventp,
  5234. const char *buf, unsigned long size,
  5235. const char *sys)
  5236. {
  5237. int ret = __tep_parse_format(eventp, pevent, buf, size, sys);
  5238. struct event_format *event = *eventp;
  5239. if (event == NULL)
  5240. return ret;
  5241. if (pevent && add_event(pevent, event)) {
  5242. ret = TEP_ERRNO__MEM_ALLOC_FAILED;
  5243. goto event_add_failed;
  5244. }
  5245. #define PRINT_ARGS 0
  5246. if (PRINT_ARGS && event->print_fmt.args)
  5247. print_args(event->print_fmt.args);
  5248. return 0;
  5249. event_add_failed:
  5250. tep_free_format(event);
  5251. return ret;
  5252. }
  5253. /**
  5254. * tep_parse_format - parse the event format
  5255. * @pevent: the handle to the pevent
  5256. * @eventp: returned format
  5257. * @buf: the buffer storing the event format string
  5258. * @size: the size of @buf
  5259. * @sys: the system the event belongs to
  5260. *
  5261. * This parses the event format and creates an event structure
  5262. * to quickly parse raw data for a given event.
  5263. *
  5264. * These files currently come from:
  5265. *
  5266. * /sys/kernel/debug/tracing/events/.../.../format
  5267. */
  5268. enum tep_errno tep_parse_format(struct tep_handle *pevent,
  5269. struct event_format **eventp,
  5270. const char *buf,
  5271. unsigned long size, const char *sys)
  5272. {
  5273. return __parse_event(pevent, eventp, buf, size, sys);
  5274. }
  5275. /**
  5276. * tep_parse_event - parse the event format
  5277. * @pevent: the handle to the pevent
  5278. * @buf: the buffer storing the event format string
  5279. * @size: the size of @buf
  5280. * @sys: the system the event belongs to
  5281. *
  5282. * This parses the event format and creates an event structure
  5283. * to quickly parse raw data for a given event.
  5284. *
  5285. * These files currently come from:
  5286. *
  5287. * /sys/kernel/debug/tracing/events/.../.../format
  5288. */
  5289. enum tep_errno tep_parse_event(struct tep_handle *pevent, const char *buf,
  5290. unsigned long size, const char *sys)
  5291. {
  5292. struct event_format *event = NULL;
  5293. return __parse_event(pevent, &event, buf, size, sys);
  5294. }
  5295. #undef _PE
  5296. #define _PE(code, str) str
  5297. static const char * const tep_error_str[] = {
  5298. TEP_ERRORS
  5299. };
  5300. #undef _PE
  5301. int tep_strerror(struct tep_handle *pevent __maybe_unused,
  5302. enum tep_errno errnum, char *buf, size_t buflen)
  5303. {
  5304. int idx;
  5305. const char *msg;
  5306. if (errnum >= 0) {
  5307. str_error_r(errnum, buf, buflen);
  5308. return 0;
  5309. }
  5310. if (errnum <= __TEP_ERRNO__START ||
  5311. errnum >= __TEP_ERRNO__END)
  5312. return -1;
  5313. idx = errnum - __TEP_ERRNO__START - 1;
  5314. msg = tep_error_str[idx];
  5315. snprintf(buf, buflen, "%s", msg);
  5316. return 0;
  5317. }
  5318. int get_field_val(struct trace_seq *s, struct format_field *field,
  5319. const char *name, struct tep_record *record,
  5320. unsigned long long *val, int err)
  5321. {
  5322. if (!field) {
  5323. if (err)
  5324. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  5325. return -1;
  5326. }
  5327. if (tep_read_number_field(field, record->data, val)) {
  5328. if (err)
  5329. trace_seq_printf(s, " %s=INVALID", name);
  5330. return -1;
  5331. }
  5332. return 0;
  5333. }
  5334. /**
  5335. * tep_get_field_raw - return the raw pointer into the data field
  5336. * @s: The seq to print to on error
  5337. * @event: the event that the field is for
  5338. * @name: The name of the field
  5339. * @record: The record with the field name.
  5340. * @len: place to store the field length.
  5341. * @err: print default error if failed.
  5342. *
  5343. * Returns a pointer into record->data of the field and places
  5344. * the length of the field in @len.
  5345. *
  5346. * On failure, it returns NULL.
  5347. */
  5348. void *tep_get_field_raw(struct trace_seq *s, struct event_format *event,
  5349. const char *name, struct tep_record *record,
  5350. int *len, int err)
  5351. {
  5352. struct format_field *field;
  5353. void *data = record->data;
  5354. unsigned offset;
  5355. int dummy;
  5356. if (!event)
  5357. return NULL;
  5358. field = tep_find_field(event, name);
  5359. if (!field) {
  5360. if (err)
  5361. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  5362. return NULL;
  5363. }
  5364. /* Allow @len to be NULL */
  5365. if (!len)
  5366. len = &dummy;
  5367. offset = field->offset;
  5368. if (field->flags & FIELD_IS_DYNAMIC) {
  5369. offset = tep_read_number(event->pevent,
  5370. data + offset, field->size);
  5371. *len = offset >> 16;
  5372. offset &= 0xffff;
  5373. } else
  5374. *len = field->size;
  5375. return data + offset;
  5376. }
  5377. /**
  5378. * tep_get_field_val - find a field and return its value
  5379. * @s: The seq to print to on error
  5380. * @event: the event that the field is for
  5381. * @name: The name of the field
  5382. * @record: The record with the field name.
  5383. * @val: place to store the value of the field.
  5384. * @err: print default error if failed.
  5385. *
  5386. * Returns 0 on success -1 on field not found.
  5387. */
  5388. int tep_get_field_val(struct trace_seq *s, struct event_format *event,
  5389. const char *name, struct tep_record *record,
  5390. unsigned long long *val, int err)
  5391. {
  5392. struct format_field *field;
  5393. if (!event)
  5394. return -1;
  5395. field = tep_find_field(event, name);
  5396. return get_field_val(s, field, name, record, val, err);
  5397. }
  5398. /**
  5399. * tep_get_common_field_val - find a common field and return its value
  5400. * @s: The seq to print to on error
  5401. * @event: the event that the field is for
  5402. * @name: The name of the field
  5403. * @record: The record with the field name.
  5404. * @val: place to store the value of the field.
  5405. * @err: print default error if failed.
  5406. *
  5407. * Returns 0 on success -1 on field not found.
  5408. */
  5409. int tep_get_common_field_val(struct trace_seq *s, struct event_format *event,
  5410. const char *name, struct tep_record *record,
  5411. unsigned long long *val, int err)
  5412. {
  5413. struct format_field *field;
  5414. if (!event)
  5415. return -1;
  5416. field = tep_find_common_field(event, name);
  5417. return get_field_val(s, field, name, record, val, err);
  5418. }
  5419. /**
  5420. * tep_get_any_field_val - find a any field and return its value
  5421. * @s: The seq to print to on error
  5422. * @event: the event that the field is for
  5423. * @name: The name of the field
  5424. * @record: The record with the field name.
  5425. * @val: place to store the value of the field.
  5426. * @err: print default error if failed.
  5427. *
  5428. * Returns 0 on success -1 on field not found.
  5429. */
  5430. int tep_get_any_field_val(struct trace_seq *s, struct event_format *event,
  5431. const char *name, struct tep_record *record,
  5432. unsigned long long *val, int err)
  5433. {
  5434. struct format_field *field;
  5435. if (!event)
  5436. return -1;
  5437. field = tep_find_any_field(event, name);
  5438. return get_field_val(s, field, name, record, val, err);
  5439. }
  5440. /**
  5441. * tep_print_num_field - print a field and a format
  5442. * @s: The seq to print to
  5443. * @fmt: The printf format to print the field with.
  5444. * @event: the event that the field is for
  5445. * @name: The name of the field
  5446. * @record: The record with the field name.
  5447. * @err: print default error if failed.
  5448. *
  5449. * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
  5450. */
  5451. int tep_print_num_field(struct trace_seq *s, const char *fmt,
  5452. struct event_format *event, const char *name,
  5453. struct tep_record *record, int err)
  5454. {
  5455. struct format_field *field = tep_find_field(event, name);
  5456. unsigned long long val;
  5457. if (!field)
  5458. goto failed;
  5459. if (tep_read_number_field(field, record->data, &val))
  5460. goto failed;
  5461. return trace_seq_printf(s, fmt, val);
  5462. failed:
  5463. if (err)
  5464. trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
  5465. return -1;
  5466. }
  5467. /**
  5468. * tep_print_func_field - print a field and a format for function pointers
  5469. * @s: The seq to print to
  5470. * @fmt: The printf format to print the field with.
  5471. * @event: the event that the field is for
  5472. * @name: The name of the field
  5473. * @record: The record with the field name.
  5474. * @err: print default error if failed.
  5475. *
  5476. * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
  5477. */
  5478. int tep_print_func_field(struct trace_seq *s, const char *fmt,
  5479. struct event_format *event, const char *name,
  5480. struct tep_record *record, int err)
  5481. {
  5482. struct format_field *field = tep_find_field(event, name);
  5483. struct tep_handle *pevent = event->pevent;
  5484. unsigned long long val;
  5485. struct func_map *func;
  5486. char tmp[128];
  5487. if (!field)
  5488. goto failed;
  5489. if (tep_read_number_field(field, record->data, &val))
  5490. goto failed;
  5491. func = find_func(pevent, val);
  5492. if (func)
  5493. snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
  5494. else
  5495. sprintf(tmp, "0x%08llx", val);
  5496. return trace_seq_printf(s, fmt, tmp);
  5497. failed:
  5498. if (err)
  5499. trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
  5500. return -1;
  5501. }
  5502. static void free_func_handle(struct tep_function_handler *func)
  5503. {
  5504. struct func_params *params;
  5505. free(func->name);
  5506. while (func->params) {
  5507. params = func->params;
  5508. func->params = params->next;
  5509. free(params);
  5510. }
  5511. free(func);
  5512. }
  5513. /**
  5514. * tep_register_print_function - register a helper function
  5515. * @pevent: the handle to the pevent
  5516. * @func: the function to process the helper function
  5517. * @ret_type: the return type of the helper function
  5518. * @name: the name of the helper function
  5519. * @parameters: A list of enum tep_func_arg_type
  5520. *
  5521. * Some events may have helper functions in the print format arguments.
  5522. * This allows a plugin to dynamically create a way to process one
  5523. * of these functions.
  5524. *
  5525. * The @parameters is a variable list of tep_func_arg_type enums that
  5526. * must end with TEP_FUNC_ARG_VOID.
  5527. */
  5528. int tep_register_print_function(struct tep_handle *pevent,
  5529. tep_func_handler func,
  5530. enum tep_func_arg_type ret_type,
  5531. char *name, ...)
  5532. {
  5533. struct tep_function_handler *func_handle;
  5534. struct func_params **next_param;
  5535. struct func_params *param;
  5536. enum tep_func_arg_type type;
  5537. va_list ap;
  5538. int ret;
  5539. func_handle = find_func_handler(pevent, name);
  5540. if (func_handle) {
  5541. /*
  5542. * This is most like caused by the users own
  5543. * plugins updating the function. This overrides the
  5544. * system defaults.
  5545. */
  5546. pr_stat("override of function helper '%s'", name);
  5547. remove_func_handler(pevent, name);
  5548. }
  5549. func_handle = calloc(1, sizeof(*func_handle));
  5550. if (!func_handle) {
  5551. do_warning("Failed to allocate function handler");
  5552. return TEP_ERRNO__MEM_ALLOC_FAILED;
  5553. }
  5554. func_handle->ret_type = ret_type;
  5555. func_handle->name = strdup(name);
  5556. func_handle->func = func;
  5557. if (!func_handle->name) {
  5558. do_warning("Failed to allocate function name");
  5559. free(func_handle);
  5560. return TEP_ERRNO__MEM_ALLOC_FAILED;
  5561. }
  5562. next_param = &(func_handle->params);
  5563. va_start(ap, name);
  5564. for (;;) {
  5565. type = va_arg(ap, enum tep_func_arg_type);
  5566. if (type == TEP_FUNC_ARG_VOID)
  5567. break;
  5568. if (type >= TEP_FUNC_ARG_MAX_TYPES) {
  5569. do_warning("Invalid argument type %d", type);
  5570. ret = TEP_ERRNO__INVALID_ARG_TYPE;
  5571. goto out_free;
  5572. }
  5573. param = malloc(sizeof(*param));
  5574. if (!param) {
  5575. do_warning("Failed to allocate function param");
  5576. ret = TEP_ERRNO__MEM_ALLOC_FAILED;
  5577. goto out_free;
  5578. }
  5579. param->type = type;
  5580. param->next = NULL;
  5581. *next_param = param;
  5582. next_param = &(param->next);
  5583. func_handle->nr_args++;
  5584. }
  5585. va_end(ap);
  5586. func_handle->next = pevent->func_handlers;
  5587. pevent->func_handlers = func_handle;
  5588. return 0;
  5589. out_free:
  5590. va_end(ap);
  5591. free_func_handle(func_handle);
  5592. return ret;
  5593. }
  5594. /**
  5595. * tep_unregister_print_function - unregister a helper function
  5596. * @pevent: the handle to the pevent
  5597. * @func: the function to process the helper function
  5598. * @name: the name of the helper function
  5599. *
  5600. * This function removes existing print handler for function @name.
  5601. *
  5602. * Returns 0 if the handler was removed successully, -1 otherwise.
  5603. */
  5604. int tep_unregister_print_function(struct tep_handle *pevent,
  5605. tep_func_handler func, char *name)
  5606. {
  5607. struct tep_function_handler *func_handle;
  5608. func_handle = find_func_handler(pevent, name);
  5609. if (func_handle && func_handle->func == func) {
  5610. remove_func_handler(pevent, name);
  5611. return 0;
  5612. }
  5613. return -1;
  5614. }
  5615. static struct event_format *search_event(struct tep_handle *pevent, int id,
  5616. const char *sys_name,
  5617. const char *event_name)
  5618. {
  5619. struct event_format *event;
  5620. if (id >= 0) {
  5621. /* search by id */
  5622. event = tep_find_event(pevent, id);
  5623. if (!event)
  5624. return NULL;
  5625. if (event_name && (strcmp(event_name, event->name) != 0))
  5626. return NULL;
  5627. if (sys_name && (strcmp(sys_name, event->system) != 0))
  5628. return NULL;
  5629. } else {
  5630. event = tep_find_event_by_name(pevent, sys_name, event_name);
  5631. if (!event)
  5632. return NULL;
  5633. }
  5634. return event;
  5635. }
  5636. /**
  5637. * tep_register_event_handler - register a way to parse an event
  5638. * @pevent: the handle to the pevent
  5639. * @id: the id of the event to register
  5640. * @sys_name: the system name the event belongs to
  5641. * @event_name: the name of the event
  5642. * @func: the function to call to parse the event information
  5643. * @context: the data to be passed to @func
  5644. *
  5645. * This function allows a developer to override the parsing of
  5646. * a given event. If for some reason the default print format
  5647. * is not sufficient, this function will register a function
  5648. * for an event to be used to parse the data instead.
  5649. *
  5650. * If @id is >= 0, then it is used to find the event.
  5651. * else @sys_name and @event_name are used.
  5652. */
  5653. int tep_register_event_handler(struct tep_handle *pevent, int id,
  5654. const char *sys_name, const char *event_name,
  5655. tep_event_handler_func func, void *context)
  5656. {
  5657. struct event_format *event;
  5658. struct event_handler *handle;
  5659. event = search_event(pevent, id, sys_name, event_name);
  5660. if (event == NULL)
  5661. goto not_found;
  5662. pr_stat("overriding event (%d) %s:%s with new print handler",
  5663. event->id, event->system, event->name);
  5664. event->handler = func;
  5665. event->context = context;
  5666. return 0;
  5667. not_found:
  5668. /* Save for later use. */
  5669. handle = calloc(1, sizeof(*handle));
  5670. if (!handle) {
  5671. do_warning("Failed to allocate event handler");
  5672. return TEP_ERRNO__MEM_ALLOC_FAILED;
  5673. }
  5674. handle->id = id;
  5675. if (event_name)
  5676. handle->event_name = strdup(event_name);
  5677. if (sys_name)
  5678. handle->sys_name = strdup(sys_name);
  5679. if ((event_name && !handle->event_name) ||
  5680. (sys_name && !handle->sys_name)) {
  5681. do_warning("Failed to allocate event/sys name");
  5682. free((void *)handle->event_name);
  5683. free((void *)handle->sys_name);
  5684. free(handle);
  5685. return TEP_ERRNO__MEM_ALLOC_FAILED;
  5686. }
  5687. handle->func = func;
  5688. handle->next = pevent->handlers;
  5689. pevent->handlers = handle;
  5690. handle->context = context;
  5691. return -1;
  5692. }
  5693. static int handle_matches(struct event_handler *handler, int id,
  5694. const char *sys_name, const char *event_name,
  5695. tep_event_handler_func func, void *context)
  5696. {
  5697. if (id >= 0 && id != handler->id)
  5698. return 0;
  5699. if (event_name && (strcmp(event_name, handler->event_name) != 0))
  5700. return 0;
  5701. if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
  5702. return 0;
  5703. if (func != handler->func || context != handler->context)
  5704. return 0;
  5705. return 1;
  5706. }
  5707. /**
  5708. * tep_unregister_event_handler - unregister an existing event handler
  5709. * @pevent: the handle to the pevent
  5710. * @id: the id of the event to unregister
  5711. * @sys_name: the system name the handler belongs to
  5712. * @event_name: the name of the event handler
  5713. * @func: the function to call to parse the event information
  5714. * @context: the data to be passed to @func
  5715. *
  5716. * This function removes existing event handler (parser).
  5717. *
  5718. * If @id is >= 0, then it is used to find the event.
  5719. * else @sys_name and @event_name are used.
  5720. *
  5721. * Returns 0 if handler was removed successfully, -1 if event was not found.
  5722. */
  5723. int tep_unregister_event_handler(struct tep_handle *pevent, int id,
  5724. const char *sys_name, const char *event_name,
  5725. tep_event_handler_func func, void *context)
  5726. {
  5727. struct event_format *event;
  5728. struct event_handler *handle;
  5729. struct event_handler **next;
  5730. event = search_event(pevent, id, sys_name, event_name);
  5731. if (event == NULL)
  5732. goto not_found;
  5733. if (event->handler == func && event->context == context) {
  5734. pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
  5735. event->id, event->system, event->name);
  5736. event->handler = NULL;
  5737. event->context = NULL;
  5738. return 0;
  5739. }
  5740. not_found:
  5741. for (next = &pevent->handlers; *next; next = &(*next)->next) {
  5742. handle = *next;
  5743. if (handle_matches(handle, id, sys_name, event_name,
  5744. func, context))
  5745. break;
  5746. }
  5747. if (!(*next))
  5748. return -1;
  5749. *next = handle->next;
  5750. free_handler(handle);
  5751. return 0;
  5752. }
  5753. /**
  5754. * tep_alloc - create a pevent handle
  5755. */
  5756. struct tep_handle *tep_alloc(void)
  5757. {
  5758. struct tep_handle *pevent = calloc(1, sizeof(*pevent));
  5759. if (pevent)
  5760. pevent->ref_count = 1;
  5761. return pevent;
  5762. }
  5763. void tep_ref(struct tep_handle *pevent)
  5764. {
  5765. pevent->ref_count++;
  5766. }
  5767. void tep_free_format_field(struct format_field *field)
  5768. {
  5769. free(field->type);
  5770. if (field->alias != field->name)
  5771. free(field->alias);
  5772. free(field->name);
  5773. free(field);
  5774. }
  5775. static void free_format_fields(struct format_field *field)
  5776. {
  5777. struct format_field *next;
  5778. while (field) {
  5779. next = field->next;
  5780. tep_free_format_field(field);
  5781. field = next;
  5782. }
  5783. }
  5784. static void free_formats(struct format *format)
  5785. {
  5786. free_format_fields(format->common_fields);
  5787. free_format_fields(format->fields);
  5788. }
  5789. void tep_free_format(struct event_format *event)
  5790. {
  5791. free(event->name);
  5792. free(event->system);
  5793. free_formats(&event->format);
  5794. free(event->print_fmt.format);
  5795. free_args(event->print_fmt.args);
  5796. free(event);
  5797. }
  5798. /**
  5799. * tep_free - free a pevent handle
  5800. * @pevent: the pevent handle to free
  5801. */
  5802. void tep_free(struct tep_handle *pevent)
  5803. {
  5804. struct cmdline_list *cmdlist, *cmdnext;
  5805. struct func_list *funclist, *funcnext;
  5806. struct printk_list *printklist, *printknext;
  5807. struct tep_function_handler *func_handler;
  5808. struct event_handler *handle;
  5809. int i;
  5810. if (!pevent)
  5811. return;
  5812. cmdlist = pevent->cmdlist;
  5813. funclist = pevent->funclist;
  5814. printklist = pevent->printklist;
  5815. pevent->ref_count--;
  5816. if (pevent->ref_count)
  5817. return;
  5818. if (pevent->cmdlines) {
  5819. for (i = 0; i < pevent->cmdline_count; i++)
  5820. free(pevent->cmdlines[i].comm);
  5821. free(pevent->cmdlines);
  5822. }
  5823. while (cmdlist) {
  5824. cmdnext = cmdlist->next;
  5825. free(cmdlist->comm);
  5826. free(cmdlist);
  5827. cmdlist = cmdnext;
  5828. }
  5829. if (pevent->func_map) {
  5830. for (i = 0; i < (int)pevent->func_count; i++) {
  5831. free(pevent->func_map[i].func);
  5832. free(pevent->func_map[i].mod);
  5833. }
  5834. free(pevent->func_map);
  5835. }
  5836. while (funclist) {
  5837. funcnext = funclist->next;
  5838. free(funclist->func);
  5839. free(funclist->mod);
  5840. free(funclist);
  5841. funclist = funcnext;
  5842. }
  5843. while (pevent->func_handlers) {
  5844. func_handler = pevent->func_handlers;
  5845. pevent->func_handlers = func_handler->next;
  5846. free_func_handle(func_handler);
  5847. }
  5848. if (pevent->printk_map) {
  5849. for (i = 0; i < (int)pevent->printk_count; i++)
  5850. free(pevent->printk_map[i].printk);
  5851. free(pevent->printk_map);
  5852. }
  5853. while (printklist) {
  5854. printknext = printklist->next;
  5855. free(printklist->printk);
  5856. free(printklist);
  5857. printklist = printknext;
  5858. }
  5859. for (i = 0; i < pevent->nr_events; i++)
  5860. tep_free_format(pevent->events[i]);
  5861. while (pevent->handlers) {
  5862. handle = pevent->handlers;
  5863. pevent->handlers = handle->next;
  5864. free_handler(handle);
  5865. }
  5866. free(pevent->trace_clock);
  5867. free(pevent->events);
  5868. free(pevent->sort_events);
  5869. free(pevent->func_resolver);
  5870. free(pevent);
  5871. }
  5872. void tep_unref(struct tep_handle *pevent)
  5873. {
  5874. tep_free(pevent);
  5875. }