event-parse.c 149 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. #include "trace-seq.h"
  27. static const char *input_buf;
  28. static unsigned long long input_buf_ptr;
  29. static unsigned long long input_buf_siz;
  30. static int is_flag_field;
  31. static int is_symbolic_field;
  32. static int show_warning = 1;
  33. #define do_warning(fmt, ...) \
  34. do { \
  35. if (show_warning) \
  36. warning(fmt, ##__VA_ARGS__); \
  37. } while (0)
  38. #define do_warning_event(event, fmt, ...) \
  39. do { \
  40. if (!show_warning) \
  41. continue; \
  42. \
  43. if (event) \
  44. warning("[%s:%s] " fmt, event->system, \
  45. event->name, ##__VA_ARGS__); \
  46. else \
  47. warning(fmt, ##__VA_ARGS__); \
  48. } while (0)
  49. static void init_input_buf(const char *buf, unsigned long long size)
  50. {
  51. input_buf = buf;
  52. input_buf_siz = size;
  53. input_buf_ptr = 0;
  54. }
  55. const char *tep_get_input_buf(void)
  56. {
  57. return input_buf;
  58. }
  59. unsigned long long tep_get_input_buf_ptr(void)
  60. {
  61. return input_buf_ptr;
  62. }
  63. struct event_handler {
  64. struct event_handler *next;
  65. int id;
  66. const char *sys_name;
  67. const char *event_name;
  68. tep_event_handler_func func;
  69. void *context;
  70. };
  71. struct func_params {
  72. struct func_params *next;
  73. enum tep_func_arg_type type;
  74. };
  75. struct tep_function_handler {
  76. struct tep_function_handler *next;
  77. enum tep_func_arg_type ret_type;
  78. char *name;
  79. tep_func_handler func;
  80. struct func_params *params;
  81. int nr_args;
  82. };
  83. static unsigned long long
  84. process_defined_func(struct trace_seq *s, void *data, int size,
  85. struct tep_event_format *event, struct tep_print_arg *arg);
  86. static void free_func_handle(struct tep_function_handler *func);
  87. /**
  88. * tep_buffer_init - init buffer for parsing
  89. * @buf: buffer to parse
  90. * @size: the size of the buffer
  91. *
  92. * For use with tep_read_token(), this initializes the internal
  93. * buffer that tep_read_token() will parse.
  94. */
  95. void tep_buffer_init(const char *buf, unsigned long long size)
  96. {
  97. init_input_buf(buf, size);
  98. }
  99. void breakpoint(void)
  100. {
  101. static int x;
  102. x++;
  103. }
  104. struct tep_print_arg *alloc_arg(void)
  105. {
  106. return calloc(1, sizeof(struct tep_print_arg));
  107. }
  108. struct cmdline {
  109. char *comm;
  110. int pid;
  111. };
  112. static int cmdline_cmp(const void *a, const void *b)
  113. {
  114. const struct cmdline *ca = a;
  115. const struct cmdline *cb = b;
  116. if (ca->pid < cb->pid)
  117. return -1;
  118. if (ca->pid > cb->pid)
  119. return 1;
  120. return 0;
  121. }
  122. struct cmdline_list {
  123. struct cmdline_list *next;
  124. char *comm;
  125. int pid;
  126. };
  127. static int cmdline_init(struct tep_handle *pevent)
  128. {
  129. struct cmdline_list *cmdlist = pevent->cmdlist;
  130. struct cmdline_list *item;
  131. struct cmdline *cmdlines;
  132. int i;
  133. cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
  134. if (!cmdlines)
  135. return -1;
  136. i = 0;
  137. while (cmdlist) {
  138. cmdlines[i].pid = cmdlist->pid;
  139. cmdlines[i].comm = cmdlist->comm;
  140. i++;
  141. item = cmdlist;
  142. cmdlist = cmdlist->next;
  143. free(item);
  144. }
  145. qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  146. pevent->cmdlines = cmdlines;
  147. pevent->cmdlist = NULL;
  148. return 0;
  149. }
  150. static const char *find_cmdline(struct tep_handle *pevent, int pid)
  151. {
  152. const struct cmdline *comm;
  153. struct cmdline key;
  154. if (!pid)
  155. return "<idle>";
  156. if (!pevent->cmdlines && cmdline_init(pevent))
  157. return "<not enough memory for cmdlines!>";
  158. key.pid = pid;
  159. comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  160. sizeof(*pevent->cmdlines), cmdline_cmp);
  161. if (comm)
  162. return comm->comm;
  163. return "<...>";
  164. }
  165. /**
  166. * tep_pid_is_registered - return if a pid has a cmdline registered
  167. * @pevent: handle for the pevent
  168. * @pid: The pid to check if it has a cmdline registered with.
  169. *
  170. * Returns 1 if the pid has a cmdline mapped to it
  171. * 0 otherwise.
  172. */
  173. int tep_pid_is_registered(struct tep_handle *pevent, int pid)
  174. {
  175. const struct cmdline *comm;
  176. struct cmdline key;
  177. if (!pid)
  178. return 1;
  179. if (!pevent->cmdlines && cmdline_init(pevent))
  180. return 0;
  181. key.pid = pid;
  182. comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  183. sizeof(*pevent->cmdlines), cmdline_cmp);
  184. if (comm)
  185. return 1;
  186. return 0;
  187. }
  188. /*
  189. * If the command lines have been converted to an array, then
  190. * we must add this pid. This is much slower than when cmdlines
  191. * are added before the array is initialized.
  192. */
  193. static int add_new_comm(struct tep_handle *pevent, const char *comm, int pid)
  194. {
  195. struct cmdline *cmdlines = pevent->cmdlines;
  196. const struct cmdline *cmdline;
  197. struct cmdline key;
  198. if (!pid)
  199. return 0;
  200. /* avoid duplicates */
  201. key.pid = pid;
  202. cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  203. sizeof(*pevent->cmdlines), cmdline_cmp);
  204. if (cmdline) {
  205. errno = EEXIST;
  206. return -1;
  207. }
  208. cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
  209. if (!cmdlines) {
  210. errno = ENOMEM;
  211. return -1;
  212. }
  213. cmdlines[pevent->cmdline_count].comm = strdup(comm);
  214. if (!cmdlines[pevent->cmdline_count].comm) {
  215. free(cmdlines);
  216. errno = ENOMEM;
  217. return -1;
  218. }
  219. cmdlines[pevent->cmdline_count].pid = pid;
  220. if (cmdlines[pevent->cmdline_count].comm)
  221. pevent->cmdline_count++;
  222. qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  223. pevent->cmdlines = cmdlines;
  224. return 0;
  225. }
  226. /**
  227. * tep_register_comm - register a pid / comm mapping
  228. * @pevent: handle for the pevent
  229. * @comm: the command line to register
  230. * @pid: the pid to map the command line to
  231. *
  232. * This adds a mapping to search for command line names with
  233. * a given pid. The comm is duplicated.
  234. */
  235. int tep_register_comm(struct tep_handle *pevent, const char *comm, int pid)
  236. {
  237. struct cmdline_list *item;
  238. if (pevent->cmdlines)
  239. return add_new_comm(pevent, comm, pid);
  240. item = malloc(sizeof(*item));
  241. if (!item)
  242. return -1;
  243. if (comm)
  244. item->comm = strdup(comm);
  245. else
  246. item->comm = strdup("<...>");
  247. if (!item->comm) {
  248. free(item);
  249. return -1;
  250. }
  251. item->pid = pid;
  252. item->next = pevent->cmdlist;
  253. pevent->cmdlist = item;
  254. pevent->cmdline_count++;
  255. return 0;
  256. }
  257. int tep_register_trace_clock(struct tep_handle *pevent, const char *trace_clock)
  258. {
  259. pevent->trace_clock = strdup(trace_clock);
  260. if (!pevent->trace_clock) {
  261. errno = ENOMEM;
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. struct func_map {
  267. unsigned long long addr;
  268. char *func;
  269. char *mod;
  270. };
  271. struct func_list {
  272. struct func_list *next;
  273. unsigned long long addr;
  274. char *func;
  275. char *mod;
  276. };
  277. static int func_cmp(const void *a, const void *b)
  278. {
  279. const struct func_map *fa = a;
  280. const struct func_map *fb = b;
  281. if (fa->addr < fb->addr)
  282. return -1;
  283. if (fa->addr > fb->addr)
  284. return 1;
  285. return 0;
  286. }
  287. /*
  288. * We are searching for a record in between, not an exact
  289. * match.
  290. */
  291. static int func_bcmp(const void *a, const void *b)
  292. {
  293. const struct func_map *fa = a;
  294. const struct func_map *fb = b;
  295. if ((fa->addr == fb->addr) ||
  296. (fa->addr > fb->addr &&
  297. fa->addr < (fb+1)->addr))
  298. return 0;
  299. if (fa->addr < fb->addr)
  300. return -1;
  301. return 1;
  302. }
  303. static int func_map_init(struct tep_handle *pevent)
  304. {
  305. struct func_list *funclist;
  306. struct func_list *item;
  307. struct func_map *func_map;
  308. int i;
  309. func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
  310. if (!func_map)
  311. return -1;
  312. funclist = pevent->funclist;
  313. i = 0;
  314. while (funclist) {
  315. func_map[i].func = funclist->func;
  316. func_map[i].addr = funclist->addr;
  317. func_map[i].mod = funclist->mod;
  318. i++;
  319. item = funclist;
  320. funclist = funclist->next;
  321. free(item);
  322. }
  323. qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
  324. /*
  325. * Add a special record at the end.
  326. */
  327. func_map[pevent->func_count].func = NULL;
  328. func_map[pevent->func_count].addr = 0;
  329. func_map[pevent->func_count].mod = NULL;
  330. pevent->func_map = func_map;
  331. pevent->funclist = NULL;
  332. return 0;
  333. }
  334. static struct func_map *
  335. __find_func(struct tep_handle *pevent, unsigned long long addr)
  336. {
  337. struct func_map *func;
  338. struct func_map key;
  339. if (!pevent->func_map)
  340. func_map_init(pevent);
  341. key.addr = addr;
  342. func = bsearch(&key, pevent->func_map, pevent->func_count,
  343. sizeof(*pevent->func_map), func_bcmp);
  344. return func;
  345. }
  346. struct func_resolver {
  347. tep_func_resolver_t *func;
  348. void *priv;
  349. struct func_map map;
  350. };
  351. /**
  352. * tep_set_function_resolver - set an alternative function resolver
  353. * @pevent: handle for the pevent
  354. * @resolver: function to be used
  355. * @priv: resolver function private state.
  356. *
  357. * Some tools may have already a way to resolve kernel functions, allow them to
  358. * keep using it instead of duplicating all the entries inside
  359. * pevent->funclist.
  360. */
  361. int tep_set_function_resolver(struct tep_handle *pevent,
  362. tep_func_resolver_t *func, void *priv)
  363. {
  364. struct func_resolver *resolver = malloc(sizeof(*resolver));
  365. if (resolver == NULL)
  366. return -1;
  367. resolver->func = func;
  368. resolver->priv = priv;
  369. free(pevent->func_resolver);
  370. pevent->func_resolver = resolver;
  371. return 0;
  372. }
  373. /**
  374. * tep_reset_function_resolver - reset alternative function resolver
  375. * @pevent: handle for the pevent
  376. *
  377. * Stop using whatever alternative resolver was set, use the default
  378. * one instead.
  379. */
  380. void tep_reset_function_resolver(struct tep_handle *pevent)
  381. {
  382. free(pevent->func_resolver);
  383. pevent->func_resolver = NULL;
  384. }
  385. static struct func_map *
  386. find_func(struct tep_handle *pevent, unsigned long long addr)
  387. {
  388. struct func_map *map;
  389. if (!pevent->func_resolver)
  390. return __find_func(pevent, addr);
  391. map = &pevent->func_resolver->map;
  392. map->mod = NULL;
  393. map->addr = addr;
  394. map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
  395. &map->addr, &map->mod);
  396. if (map->func == NULL)
  397. return NULL;
  398. return map;
  399. }
  400. /**
  401. * tep_find_function - find a function by a given address
  402. * @pevent: handle for the pevent
  403. * @addr: the address to find the function with
  404. *
  405. * Returns a pointer to the function stored that has the given
  406. * address. Note, the address does not have to be exact, it
  407. * will select the function that would contain the address.
  408. */
  409. const char *tep_find_function(struct tep_handle *pevent, unsigned long long addr)
  410. {
  411. struct func_map *map;
  412. map = find_func(pevent, addr);
  413. if (!map)
  414. return NULL;
  415. return map->func;
  416. }
  417. /**
  418. * tep_find_function_address - find a function address by a given address
  419. * @pevent: handle for the pevent
  420. * @addr: the address to find the function with
  421. *
  422. * Returns the address the function starts at. This can be used in
  423. * conjunction with tep_find_function to print both the function
  424. * name and the function offset.
  425. */
  426. unsigned long long
  427. tep_find_function_address(struct tep_handle *pevent, unsigned long long addr)
  428. {
  429. struct func_map *map;
  430. map = find_func(pevent, addr);
  431. if (!map)
  432. return 0;
  433. return map->addr;
  434. }
  435. /**
  436. * tep_register_function - register a function with a given address
  437. * @pevent: handle for the pevent
  438. * @function: the function name to register
  439. * @addr: the address the function starts at
  440. * @mod: the kernel module the function may be in (NULL for none)
  441. *
  442. * This registers a function name with an address and module.
  443. * The @func passed in is duplicated.
  444. */
  445. int tep_register_function(struct tep_handle *pevent, char *func,
  446. unsigned long long addr, char *mod)
  447. {
  448. struct func_list *item = malloc(sizeof(*item));
  449. if (!item)
  450. return -1;
  451. item->next = pevent->funclist;
  452. item->func = strdup(func);
  453. if (!item->func)
  454. goto out_free;
  455. if (mod) {
  456. item->mod = strdup(mod);
  457. if (!item->mod)
  458. goto out_free_func;
  459. } else
  460. item->mod = NULL;
  461. item->addr = addr;
  462. pevent->funclist = item;
  463. pevent->func_count++;
  464. return 0;
  465. out_free_func:
  466. free(item->func);
  467. item->func = NULL;
  468. out_free:
  469. free(item);
  470. errno = ENOMEM;
  471. return -1;
  472. }
  473. /**
  474. * tep_print_funcs - print out the stored functions
  475. * @pevent: handle for the pevent
  476. *
  477. * This prints out the stored functions.
  478. */
  479. void tep_print_funcs(struct tep_handle *pevent)
  480. {
  481. int i;
  482. if (!pevent->func_map)
  483. func_map_init(pevent);
  484. for (i = 0; i < (int)pevent->func_count; i++) {
  485. printf("%016llx %s",
  486. pevent->func_map[i].addr,
  487. pevent->func_map[i].func);
  488. if (pevent->func_map[i].mod)
  489. printf(" [%s]\n", pevent->func_map[i].mod);
  490. else
  491. printf("\n");
  492. }
  493. }
  494. struct printk_map {
  495. unsigned long long addr;
  496. char *printk;
  497. };
  498. struct printk_list {
  499. struct printk_list *next;
  500. unsigned long long addr;
  501. char *printk;
  502. };
  503. static int printk_cmp(const void *a, const void *b)
  504. {
  505. const struct printk_map *pa = a;
  506. const struct printk_map *pb = b;
  507. if (pa->addr < pb->addr)
  508. return -1;
  509. if (pa->addr > pb->addr)
  510. return 1;
  511. return 0;
  512. }
  513. static int printk_map_init(struct tep_handle *pevent)
  514. {
  515. struct printk_list *printklist;
  516. struct printk_list *item;
  517. struct printk_map *printk_map;
  518. int i;
  519. printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
  520. if (!printk_map)
  521. return -1;
  522. printklist = pevent->printklist;
  523. i = 0;
  524. while (printklist) {
  525. printk_map[i].printk = printklist->printk;
  526. printk_map[i].addr = printklist->addr;
  527. i++;
  528. item = printklist;
  529. printklist = printklist->next;
  530. free(item);
  531. }
  532. qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
  533. pevent->printk_map = printk_map;
  534. pevent->printklist = NULL;
  535. return 0;
  536. }
  537. static struct printk_map *
  538. find_printk(struct tep_handle *pevent, unsigned long long addr)
  539. {
  540. struct printk_map *printk;
  541. struct printk_map key;
  542. if (!pevent->printk_map && printk_map_init(pevent))
  543. return NULL;
  544. key.addr = addr;
  545. printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
  546. sizeof(*pevent->printk_map), printk_cmp);
  547. return printk;
  548. }
  549. /**
  550. * tep_register_print_string - register a string by its address
  551. * @pevent: handle for the pevent
  552. * @fmt: the string format to register
  553. * @addr: the address the string was located at
  554. *
  555. * This registers a string by the address it was stored in the kernel.
  556. * The @fmt passed in is duplicated.
  557. */
  558. int tep_register_print_string(struct tep_handle *pevent, const char *fmt,
  559. unsigned long long addr)
  560. {
  561. struct printk_list *item = malloc(sizeof(*item));
  562. char *p;
  563. if (!item)
  564. return -1;
  565. item->next = pevent->printklist;
  566. item->addr = addr;
  567. /* Strip off quotes and '\n' from the end */
  568. if (fmt[0] == '"')
  569. fmt++;
  570. item->printk = strdup(fmt);
  571. if (!item->printk)
  572. goto out_free;
  573. p = item->printk + strlen(item->printk) - 1;
  574. if (*p == '"')
  575. *p = 0;
  576. p -= 2;
  577. if (strcmp(p, "\\n") == 0)
  578. *p = 0;
  579. pevent->printklist = item;
  580. pevent->printk_count++;
  581. return 0;
  582. out_free:
  583. free(item);
  584. errno = ENOMEM;
  585. return -1;
  586. }
  587. /**
  588. * tep_print_printk - print out the stored strings
  589. * @pevent: handle for the pevent
  590. *
  591. * This prints the string formats that were stored.
  592. */
  593. void tep_print_printk(struct tep_handle *pevent)
  594. {
  595. int i;
  596. if (!pevent->printk_map)
  597. printk_map_init(pevent);
  598. for (i = 0; i < (int)pevent->printk_count; i++) {
  599. printf("%016llx %s\n",
  600. pevent->printk_map[i].addr,
  601. pevent->printk_map[i].printk);
  602. }
  603. }
  604. static struct tep_event_format *alloc_event(void)
  605. {
  606. return calloc(1, sizeof(struct tep_event_format));
  607. }
  608. static int add_event(struct tep_handle *pevent, struct tep_event_format *event)
  609. {
  610. int i;
  611. struct tep_event_format **events = realloc(pevent->events, sizeof(event) *
  612. (pevent->nr_events + 1));
  613. if (!events)
  614. return -1;
  615. pevent->events = events;
  616. for (i = 0; i < pevent->nr_events; i++) {
  617. if (pevent->events[i]->id > event->id)
  618. break;
  619. }
  620. if (i < pevent->nr_events)
  621. memmove(&pevent->events[i + 1],
  622. &pevent->events[i],
  623. sizeof(event) * (pevent->nr_events - i));
  624. pevent->events[i] = event;
  625. pevent->nr_events++;
  626. event->pevent = pevent;
  627. return 0;
  628. }
  629. static int event_item_type(enum tep_event_type type)
  630. {
  631. switch (type) {
  632. case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
  633. return 1;
  634. case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
  635. default:
  636. return 0;
  637. }
  638. }
  639. static void free_flag_sym(struct tep_print_flag_sym *fsym)
  640. {
  641. struct tep_print_flag_sym *next;
  642. while (fsym) {
  643. next = fsym->next;
  644. free(fsym->value);
  645. free(fsym->str);
  646. free(fsym);
  647. fsym = next;
  648. }
  649. }
  650. static void free_arg(struct tep_print_arg *arg)
  651. {
  652. struct tep_print_arg *farg;
  653. if (!arg)
  654. return;
  655. switch (arg->type) {
  656. case TEP_PRINT_ATOM:
  657. free(arg->atom.atom);
  658. break;
  659. case TEP_PRINT_FIELD:
  660. free(arg->field.name);
  661. break;
  662. case TEP_PRINT_FLAGS:
  663. free_arg(arg->flags.field);
  664. free(arg->flags.delim);
  665. free_flag_sym(arg->flags.flags);
  666. break;
  667. case TEP_PRINT_SYMBOL:
  668. free_arg(arg->symbol.field);
  669. free_flag_sym(arg->symbol.symbols);
  670. break;
  671. case TEP_PRINT_HEX:
  672. case TEP_PRINT_HEX_STR:
  673. free_arg(arg->hex.field);
  674. free_arg(arg->hex.size);
  675. break;
  676. case TEP_PRINT_INT_ARRAY:
  677. free_arg(arg->int_array.field);
  678. free_arg(arg->int_array.count);
  679. free_arg(arg->int_array.el_size);
  680. break;
  681. case TEP_PRINT_TYPE:
  682. free(arg->typecast.type);
  683. free_arg(arg->typecast.item);
  684. break;
  685. case TEP_PRINT_STRING:
  686. case TEP_PRINT_BSTRING:
  687. free(arg->string.string);
  688. break;
  689. case TEP_PRINT_BITMASK:
  690. free(arg->bitmask.bitmask);
  691. break;
  692. case TEP_PRINT_DYNAMIC_ARRAY:
  693. case TEP_PRINT_DYNAMIC_ARRAY_LEN:
  694. free(arg->dynarray.index);
  695. break;
  696. case TEP_PRINT_OP:
  697. free(arg->op.op);
  698. free_arg(arg->op.left);
  699. free_arg(arg->op.right);
  700. break;
  701. case TEP_PRINT_FUNC:
  702. while (arg->func.args) {
  703. farg = arg->func.args;
  704. arg->func.args = farg->next;
  705. free_arg(farg);
  706. }
  707. break;
  708. case TEP_PRINT_NULL:
  709. default:
  710. break;
  711. }
  712. free(arg);
  713. }
  714. static enum tep_event_type get_type(int ch)
  715. {
  716. if (ch == '\n')
  717. return TEP_EVENT_NEWLINE;
  718. if (isspace(ch))
  719. return TEP_EVENT_SPACE;
  720. if (isalnum(ch) || ch == '_')
  721. return TEP_EVENT_ITEM;
  722. if (ch == '\'')
  723. return TEP_EVENT_SQUOTE;
  724. if (ch == '"')
  725. return TEP_EVENT_DQUOTE;
  726. if (!isprint(ch))
  727. return TEP_EVENT_NONE;
  728. if (ch == '(' || ch == ')' || ch == ',')
  729. return TEP_EVENT_DELIM;
  730. return TEP_EVENT_OP;
  731. }
  732. static int __read_char(void)
  733. {
  734. if (input_buf_ptr >= input_buf_siz)
  735. return -1;
  736. return input_buf[input_buf_ptr++];
  737. }
  738. static int __peek_char(void)
  739. {
  740. if (input_buf_ptr >= input_buf_siz)
  741. return -1;
  742. return input_buf[input_buf_ptr];
  743. }
  744. /**
  745. * tep_peek_char - peek at the next character that will be read
  746. *
  747. * Returns the next character read, or -1 if end of buffer.
  748. */
  749. int tep_peek_char(void)
  750. {
  751. return __peek_char();
  752. }
  753. static int extend_token(char **tok, char *buf, int size)
  754. {
  755. char *newtok = realloc(*tok, size);
  756. if (!newtok) {
  757. free(*tok);
  758. *tok = NULL;
  759. return -1;
  760. }
  761. if (!*tok)
  762. strcpy(newtok, buf);
  763. else
  764. strcat(newtok, buf);
  765. *tok = newtok;
  766. return 0;
  767. }
  768. static enum tep_event_type force_token(const char *str, char **tok);
  769. static enum tep_event_type __read_token(char **tok)
  770. {
  771. char buf[BUFSIZ];
  772. int ch, last_ch, quote_ch, next_ch;
  773. int i = 0;
  774. int tok_size = 0;
  775. enum tep_event_type type;
  776. *tok = NULL;
  777. ch = __read_char();
  778. if (ch < 0)
  779. return TEP_EVENT_NONE;
  780. type = get_type(ch);
  781. if (type == TEP_EVENT_NONE)
  782. return type;
  783. buf[i++] = ch;
  784. switch (type) {
  785. case TEP_EVENT_NEWLINE:
  786. case TEP_EVENT_DELIM:
  787. if (asprintf(tok, "%c", ch) < 0)
  788. return TEP_EVENT_ERROR;
  789. return type;
  790. case TEP_EVENT_OP:
  791. switch (ch) {
  792. case '-':
  793. next_ch = __peek_char();
  794. if (next_ch == '>') {
  795. buf[i++] = __read_char();
  796. break;
  797. }
  798. /* fall through */
  799. case '+':
  800. case '|':
  801. case '&':
  802. case '>':
  803. case '<':
  804. last_ch = ch;
  805. ch = __peek_char();
  806. if (ch != last_ch)
  807. goto test_equal;
  808. buf[i++] = __read_char();
  809. switch (last_ch) {
  810. case '>':
  811. case '<':
  812. goto test_equal;
  813. default:
  814. break;
  815. }
  816. break;
  817. case '!':
  818. case '=':
  819. goto test_equal;
  820. default: /* what should we do instead? */
  821. break;
  822. }
  823. buf[i] = 0;
  824. *tok = strdup(buf);
  825. return type;
  826. test_equal:
  827. ch = __peek_char();
  828. if (ch == '=')
  829. buf[i++] = __read_char();
  830. goto out;
  831. case TEP_EVENT_DQUOTE:
  832. case TEP_EVENT_SQUOTE:
  833. /* don't keep quotes */
  834. i--;
  835. quote_ch = ch;
  836. last_ch = 0;
  837. concat:
  838. do {
  839. if (i == (BUFSIZ - 1)) {
  840. buf[i] = 0;
  841. tok_size += BUFSIZ;
  842. if (extend_token(tok, buf, tok_size) < 0)
  843. return TEP_EVENT_NONE;
  844. i = 0;
  845. }
  846. last_ch = ch;
  847. ch = __read_char();
  848. buf[i++] = ch;
  849. /* the '\' '\' will cancel itself */
  850. if (ch == '\\' && last_ch == '\\')
  851. last_ch = 0;
  852. } while (ch != quote_ch || last_ch == '\\');
  853. /* remove the last quote */
  854. i--;
  855. /*
  856. * For strings (double quotes) check the next token.
  857. * If it is another string, concatinate the two.
  858. */
  859. if (type == TEP_EVENT_DQUOTE) {
  860. unsigned long long save_input_buf_ptr = input_buf_ptr;
  861. do {
  862. ch = __read_char();
  863. } while (isspace(ch));
  864. if (ch == '"')
  865. goto concat;
  866. input_buf_ptr = save_input_buf_ptr;
  867. }
  868. goto out;
  869. case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
  870. case TEP_EVENT_ITEM:
  871. default:
  872. break;
  873. }
  874. while (get_type(__peek_char()) == type) {
  875. if (i == (BUFSIZ - 1)) {
  876. buf[i] = 0;
  877. tok_size += BUFSIZ;
  878. if (extend_token(tok, buf, tok_size) < 0)
  879. return TEP_EVENT_NONE;
  880. i = 0;
  881. }
  882. ch = __read_char();
  883. buf[i++] = ch;
  884. }
  885. out:
  886. buf[i] = 0;
  887. if (extend_token(tok, buf, tok_size + i + 1) < 0)
  888. return TEP_EVENT_NONE;
  889. if (type == TEP_EVENT_ITEM) {
  890. /*
  891. * Older versions of the kernel has a bug that
  892. * creates invalid symbols and will break the mac80211
  893. * parsing. This is a work around to that bug.
  894. *
  895. * See Linux kernel commit:
  896. * 811cb50baf63461ce0bdb234927046131fc7fa8b
  897. */
  898. if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
  899. free(*tok);
  900. *tok = NULL;
  901. return force_token("\"%s\" ", tok);
  902. } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
  903. free(*tok);
  904. *tok = NULL;
  905. return force_token("\" sta:%pM\" ", tok);
  906. } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
  907. free(*tok);
  908. *tok = NULL;
  909. return force_token("\" vif:%p(%d)\" ", tok);
  910. }
  911. }
  912. return type;
  913. }
  914. static enum tep_event_type force_token(const char *str, char **tok)
  915. {
  916. const char *save_input_buf;
  917. unsigned long long save_input_buf_ptr;
  918. unsigned long long save_input_buf_siz;
  919. enum tep_event_type type;
  920. /* save off the current input pointers */
  921. save_input_buf = input_buf;
  922. save_input_buf_ptr = input_buf_ptr;
  923. save_input_buf_siz = input_buf_siz;
  924. init_input_buf(str, strlen(str));
  925. type = __read_token(tok);
  926. /* reset back to original token */
  927. input_buf = save_input_buf;
  928. input_buf_ptr = save_input_buf_ptr;
  929. input_buf_siz = save_input_buf_siz;
  930. return type;
  931. }
  932. static void free_token(char *tok)
  933. {
  934. if (tok)
  935. free(tok);
  936. }
  937. static enum tep_event_type read_token(char **tok)
  938. {
  939. enum tep_event_type type;
  940. for (;;) {
  941. type = __read_token(tok);
  942. if (type != TEP_EVENT_SPACE)
  943. return type;
  944. free_token(*tok);
  945. }
  946. /* not reached */
  947. *tok = NULL;
  948. return TEP_EVENT_NONE;
  949. }
  950. /**
  951. * tep_read_token - access to utilites to use the pevent parser
  952. * @tok: The token to return
  953. *
  954. * This will parse tokens from the string given by
  955. * tep_init_data().
  956. *
  957. * Returns the token type.
  958. */
  959. enum tep_event_type tep_read_token(char **tok)
  960. {
  961. return read_token(tok);
  962. }
  963. /**
  964. * tep_free_token - free a token returned by tep_read_token
  965. * @token: the token to free
  966. */
  967. void tep_free_token(char *token)
  968. {
  969. free_token(token);
  970. }
  971. /* no newline */
  972. static enum tep_event_type read_token_item(char **tok)
  973. {
  974. enum tep_event_type type;
  975. for (;;) {
  976. type = __read_token(tok);
  977. if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
  978. return type;
  979. free_token(*tok);
  980. *tok = NULL;
  981. }
  982. /* not reached */
  983. *tok = NULL;
  984. return TEP_EVENT_NONE;
  985. }
  986. static int test_type(enum tep_event_type type, enum tep_event_type expect)
  987. {
  988. if (type != expect) {
  989. do_warning("Error: expected type %d but read %d",
  990. expect, type);
  991. return -1;
  992. }
  993. return 0;
  994. }
  995. static int test_type_token(enum tep_event_type type, const char *token,
  996. enum tep_event_type expect, const char *expect_tok)
  997. {
  998. if (type != expect) {
  999. do_warning("Error: expected type %d but read %d",
  1000. expect, type);
  1001. return -1;
  1002. }
  1003. if (strcmp(token, expect_tok) != 0) {
  1004. do_warning("Error: expected '%s' but read '%s'",
  1005. expect_tok, token);
  1006. return -1;
  1007. }
  1008. return 0;
  1009. }
  1010. static int __read_expect_type(enum tep_event_type expect, char **tok, int newline_ok)
  1011. {
  1012. enum tep_event_type type;
  1013. if (newline_ok)
  1014. type = read_token(tok);
  1015. else
  1016. type = read_token_item(tok);
  1017. return test_type(type, expect);
  1018. }
  1019. static int read_expect_type(enum tep_event_type expect, char **tok)
  1020. {
  1021. return __read_expect_type(expect, tok, 1);
  1022. }
  1023. static int __read_expected(enum tep_event_type expect, const char *str,
  1024. int newline_ok)
  1025. {
  1026. enum tep_event_type type;
  1027. char *token;
  1028. int ret;
  1029. if (newline_ok)
  1030. type = read_token(&token);
  1031. else
  1032. type = read_token_item(&token);
  1033. ret = test_type_token(type, token, expect, str);
  1034. free_token(token);
  1035. return ret;
  1036. }
  1037. static int read_expected(enum tep_event_type expect, const char *str)
  1038. {
  1039. return __read_expected(expect, str, 1);
  1040. }
  1041. static int read_expected_item(enum tep_event_type expect, const char *str)
  1042. {
  1043. return __read_expected(expect, str, 0);
  1044. }
  1045. static char *event_read_name(void)
  1046. {
  1047. char *token;
  1048. if (read_expected(TEP_EVENT_ITEM, "name") < 0)
  1049. return NULL;
  1050. if (read_expected(TEP_EVENT_OP, ":") < 0)
  1051. return NULL;
  1052. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  1053. goto fail;
  1054. return token;
  1055. fail:
  1056. free_token(token);
  1057. return NULL;
  1058. }
  1059. static int event_read_id(void)
  1060. {
  1061. char *token;
  1062. int id;
  1063. if (read_expected_item(TEP_EVENT_ITEM, "ID") < 0)
  1064. return -1;
  1065. if (read_expected(TEP_EVENT_OP, ":") < 0)
  1066. return -1;
  1067. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  1068. goto fail;
  1069. id = strtoul(token, NULL, 0);
  1070. free_token(token);
  1071. return id;
  1072. fail:
  1073. free_token(token);
  1074. return -1;
  1075. }
  1076. static int field_is_string(struct tep_format_field *field)
  1077. {
  1078. if ((field->flags & TEP_FIELD_IS_ARRAY) &&
  1079. (strstr(field->type, "char") || strstr(field->type, "u8") ||
  1080. strstr(field->type, "s8")))
  1081. return 1;
  1082. return 0;
  1083. }
  1084. static int field_is_dynamic(struct tep_format_field *field)
  1085. {
  1086. if (strncmp(field->type, "__data_loc", 10) == 0)
  1087. return 1;
  1088. return 0;
  1089. }
  1090. static int field_is_long(struct tep_format_field *field)
  1091. {
  1092. /* includes long long */
  1093. if (strstr(field->type, "long"))
  1094. return 1;
  1095. return 0;
  1096. }
  1097. static unsigned int type_size(const char *name)
  1098. {
  1099. /* This covers all TEP_FIELD_IS_STRING types. */
  1100. static struct {
  1101. const char *type;
  1102. unsigned int size;
  1103. } table[] = {
  1104. { "u8", 1 },
  1105. { "u16", 2 },
  1106. { "u32", 4 },
  1107. { "u64", 8 },
  1108. { "s8", 1 },
  1109. { "s16", 2 },
  1110. { "s32", 4 },
  1111. { "s64", 8 },
  1112. { "char", 1 },
  1113. { },
  1114. };
  1115. int i;
  1116. for (i = 0; table[i].type; i++) {
  1117. if (!strcmp(table[i].type, name))
  1118. return table[i].size;
  1119. }
  1120. return 0;
  1121. }
  1122. static int event_read_fields(struct tep_event_format *event, struct tep_format_field **fields)
  1123. {
  1124. struct tep_format_field *field = NULL;
  1125. enum tep_event_type type;
  1126. char *token;
  1127. char *last_token;
  1128. int count = 0;
  1129. do {
  1130. unsigned int size_dynamic = 0;
  1131. type = read_token(&token);
  1132. if (type == TEP_EVENT_NEWLINE) {
  1133. free_token(token);
  1134. return count;
  1135. }
  1136. count++;
  1137. if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
  1138. goto fail;
  1139. free_token(token);
  1140. type = read_token(&token);
  1141. /*
  1142. * The ftrace fields may still use the "special" name.
  1143. * Just ignore it.
  1144. */
  1145. if (event->flags & TEP_EVENT_FL_ISFTRACE &&
  1146. type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
  1147. free_token(token);
  1148. type = read_token(&token);
  1149. }
  1150. if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
  1151. goto fail;
  1152. free_token(token);
  1153. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  1154. goto fail;
  1155. last_token = token;
  1156. field = calloc(1, sizeof(*field));
  1157. if (!field)
  1158. goto fail;
  1159. field->event = event;
  1160. /* read the rest of the type */
  1161. for (;;) {
  1162. type = read_token(&token);
  1163. if (type == TEP_EVENT_ITEM ||
  1164. (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
  1165. /*
  1166. * Some of the ftrace fields are broken and have
  1167. * an illegal "." in them.
  1168. */
  1169. (event->flags & TEP_EVENT_FL_ISFTRACE &&
  1170. type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
  1171. if (strcmp(token, "*") == 0)
  1172. field->flags |= TEP_FIELD_IS_POINTER;
  1173. if (field->type) {
  1174. char *new_type;
  1175. new_type = realloc(field->type,
  1176. strlen(field->type) +
  1177. strlen(last_token) + 2);
  1178. if (!new_type) {
  1179. free(last_token);
  1180. goto fail;
  1181. }
  1182. field->type = new_type;
  1183. strcat(field->type, " ");
  1184. strcat(field->type, last_token);
  1185. free(last_token);
  1186. } else
  1187. field->type = last_token;
  1188. last_token = token;
  1189. continue;
  1190. }
  1191. break;
  1192. }
  1193. if (!field->type) {
  1194. do_warning_event(event, "%s: no type found", __func__);
  1195. goto fail;
  1196. }
  1197. field->name = field->alias = last_token;
  1198. if (test_type(type, TEP_EVENT_OP))
  1199. goto fail;
  1200. if (strcmp(token, "[") == 0) {
  1201. enum tep_event_type last_type = type;
  1202. char *brackets = token;
  1203. char *new_brackets;
  1204. int len;
  1205. field->flags |= TEP_FIELD_IS_ARRAY;
  1206. type = read_token(&token);
  1207. if (type == TEP_EVENT_ITEM)
  1208. field->arraylen = strtoul(token, NULL, 0);
  1209. else
  1210. field->arraylen = 0;
  1211. while (strcmp(token, "]") != 0) {
  1212. if (last_type == TEP_EVENT_ITEM &&
  1213. type == TEP_EVENT_ITEM)
  1214. len = 2;
  1215. else
  1216. len = 1;
  1217. last_type = type;
  1218. new_brackets = realloc(brackets,
  1219. strlen(brackets) +
  1220. strlen(token) + len);
  1221. if (!new_brackets) {
  1222. free(brackets);
  1223. goto fail;
  1224. }
  1225. brackets = new_brackets;
  1226. if (len == 2)
  1227. strcat(brackets, " ");
  1228. strcat(brackets, token);
  1229. /* We only care about the last token */
  1230. field->arraylen = strtoul(token, NULL, 0);
  1231. free_token(token);
  1232. type = read_token(&token);
  1233. if (type == TEP_EVENT_NONE) {
  1234. do_warning_event(event, "failed to find token");
  1235. goto fail;
  1236. }
  1237. }
  1238. free_token(token);
  1239. new_brackets = realloc(brackets, strlen(brackets) + 2);
  1240. if (!new_brackets) {
  1241. free(brackets);
  1242. goto fail;
  1243. }
  1244. brackets = new_brackets;
  1245. strcat(brackets, "]");
  1246. /* add brackets to type */
  1247. type = read_token(&token);
  1248. /*
  1249. * If the next token is not an OP, then it is of
  1250. * the format: type [] item;
  1251. */
  1252. if (type == TEP_EVENT_ITEM) {
  1253. char *new_type;
  1254. new_type = realloc(field->type,
  1255. strlen(field->type) +
  1256. strlen(field->name) +
  1257. strlen(brackets) + 2);
  1258. if (!new_type) {
  1259. free(brackets);
  1260. goto fail;
  1261. }
  1262. field->type = new_type;
  1263. strcat(field->type, " ");
  1264. strcat(field->type, field->name);
  1265. size_dynamic = type_size(field->name);
  1266. free_token(field->name);
  1267. strcat(field->type, brackets);
  1268. field->name = field->alias = token;
  1269. type = read_token(&token);
  1270. } else {
  1271. char *new_type;
  1272. new_type = realloc(field->type,
  1273. strlen(field->type) +
  1274. strlen(brackets) + 1);
  1275. if (!new_type) {
  1276. free(brackets);
  1277. goto fail;
  1278. }
  1279. field->type = new_type;
  1280. strcat(field->type, brackets);
  1281. }
  1282. free(brackets);
  1283. }
  1284. if (field_is_string(field))
  1285. field->flags |= TEP_FIELD_IS_STRING;
  1286. if (field_is_dynamic(field))
  1287. field->flags |= TEP_FIELD_IS_DYNAMIC;
  1288. if (field_is_long(field))
  1289. field->flags |= TEP_FIELD_IS_LONG;
  1290. if (test_type_token(type, token, TEP_EVENT_OP, ";"))
  1291. goto fail;
  1292. free_token(token);
  1293. if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
  1294. goto fail_expect;
  1295. if (read_expected(TEP_EVENT_OP, ":") < 0)
  1296. goto fail_expect;
  1297. if (read_expect_type(TEP_EVENT_ITEM, &token))
  1298. goto fail;
  1299. field->offset = strtoul(token, NULL, 0);
  1300. free_token(token);
  1301. if (read_expected(TEP_EVENT_OP, ";") < 0)
  1302. goto fail_expect;
  1303. if (read_expected(TEP_EVENT_ITEM, "size") < 0)
  1304. goto fail_expect;
  1305. if (read_expected(TEP_EVENT_OP, ":") < 0)
  1306. goto fail_expect;
  1307. if (read_expect_type(TEP_EVENT_ITEM, &token))
  1308. goto fail;
  1309. field->size = strtoul(token, NULL, 0);
  1310. free_token(token);
  1311. if (read_expected(TEP_EVENT_OP, ";") < 0)
  1312. goto fail_expect;
  1313. type = read_token(&token);
  1314. if (type != TEP_EVENT_NEWLINE) {
  1315. /* newer versions of the kernel have a "signed" type */
  1316. if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
  1317. goto fail;
  1318. free_token(token);
  1319. if (read_expected(TEP_EVENT_OP, ":") < 0)
  1320. goto fail_expect;
  1321. if (read_expect_type(TEP_EVENT_ITEM, &token))
  1322. goto fail;
  1323. if (strtoul(token, NULL, 0))
  1324. field->flags |= TEP_FIELD_IS_SIGNED;
  1325. free_token(token);
  1326. if (read_expected(TEP_EVENT_OP, ";") < 0)
  1327. goto fail_expect;
  1328. if (read_expect_type(TEP_EVENT_NEWLINE, &token))
  1329. goto fail;
  1330. }
  1331. free_token(token);
  1332. if (field->flags & TEP_FIELD_IS_ARRAY) {
  1333. if (field->arraylen)
  1334. field->elementsize = field->size / field->arraylen;
  1335. else if (field->flags & TEP_FIELD_IS_DYNAMIC)
  1336. field->elementsize = size_dynamic;
  1337. else if (field->flags & TEP_FIELD_IS_STRING)
  1338. field->elementsize = 1;
  1339. else if (field->flags & TEP_FIELD_IS_LONG)
  1340. field->elementsize = event->pevent ?
  1341. event->pevent->long_size :
  1342. sizeof(long);
  1343. } else
  1344. field->elementsize = field->size;
  1345. *fields = field;
  1346. fields = &field->next;
  1347. } while (1);
  1348. return 0;
  1349. fail:
  1350. free_token(token);
  1351. fail_expect:
  1352. if (field) {
  1353. free(field->type);
  1354. free(field->name);
  1355. free(field);
  1356. }
  1357. return -1;
  1358. }
  1359. static int event_read_format(struct tep_event_format *event)
  1360. {
  1361. char *token;
  1362. int ret;
  1363. if (read_expected_item(TEP_EVENT_ITEM, "format") < 0)
  1364. return -1;
  1365. if (read_expected(TEP_EVENT_OP, ":") < 0)
  1366. return -1;
  1367. if (read_expect_type(TEP_EVENT_NEWLINE, &token))
  1368. goto fail;
  1369. free_token(token);
  1370. ret = event_read_fields(event, &event->format.common_fields);
  1371. if (ret < 0)
  1372. return ret;
  1373. event->format.nr_common = ret;
  1374. ret = event_read_fields(event, &event->format.fields);
  1375. if (ret < 0)
  1376. return ret;
  1377. event->format.nr_fields = ret;
  1378. return 0;
  1379. fail:
  1380. free_token(token);
  1381. return -1;
  1382. }
  1383. static enum tep_event_type
  1384. process_arg_token(struct tep_event_format *event, struct tep_print_arg *arg,
  1385. char **tok, enum tep_event_type type);
  1386. static enum tep_event_type
  1387. process_arg(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  1388. {
  1389. enum tep_event_type type;
  1390. char *token;
  1391. type = read_token(&token);
  1392. *tok = token;
  1393. return process_arg_token(event, arg, tok, type);
  1394. }
  1395. static enum tep_event_type
  1396. process_op(struct tep_event_format *event, struct tep_print_arg *arg, char **tok);
  1397. /*
  1398. * For __print_symbolic() and __print_flags, we need to completely
  1399. * evaluate the first argument, which defines what to print next.
  1400. */
  1401. static enum tep_event_type
  1402. process_field_arg(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  1403. {
  1404. enum tep_event_type type;
  1405. type = process_arg(event, arg, tok);
  1406. while (type == TEP_EVENT_OP) {
  1407. type = process_op(event, arg, tok);
  1408. }
  1409. return type;
  1410. }
  1411. static enum tep_event_type
  1412. process_cond(struct tep_event_format *event, struct tep_print_arg *top, char **tok)
  1413. {
  1414. struct tep_print_arg *arg, *left, *right;
  1415. enum tep_event_type type;
  1416. char *token = NULL;
  1417. arg = alloc_arg();
  1418. left = alloc_arg();
  1419. right = alloc_arg();
  1420. if (!arg || !left || !right) {
  1421. do_warning_event(event, "%s: not enough memory!", __func__);
  1422. /* arg will be freed at out_free */
  1423. free_arg(left);
  1424. free_arg(right);
  1425. goto out_free;
  1426. }
  1427. arg->type = TEP_PRINT_OP;
  1428. arg->op.left = left;
  1429. arg->op.right = right;
  1430. *tok = NULL;
  1431. type = process_arg(event, left, &token);
  1432. again:
  1433. if (type == TEP_EVENT_ERROR)
  1434. goto out_free;
  1435. /* Handle other operations in the arguments */
  1436. if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
  1437. type = process_op(event, left, &token);
  1438. goto again;
  1439. }
  1440. if (test_type_token(type, token, TEP_EVENT_OP, ":"))
  1441. goto out_free;
  1442. arg->op.op = token;
  1443. type = process_arg(event, right, &token);
  1444. top->op.right = arg;
  1445. *tok = token;
  1446. return type;
  1447. out_free:
  1448. /* Top may point to itself */
  1449. top->op.right = NULL;
  1450. free_token(token);
  1451. free_arg(arg);
  1452. return TEP_EVENT_ERROR;
  1453. }
  1454. static enum tep_event_type
  1455. process_array(struct tep_event_format *event, struct tep_print_arg *top, char **tok)
  1456. {
  1457. struct tep_print_arg *arg;
  1458. enum tep_event_type type;
  1459. char *token = NULL;
  1460. arg = alloc_arg();
  1461. if (!arg) {
  1462. do_warning_event(event, "%s: not enough memory!", __func__);
  1463. /* '*tok' is set to top->op.op. No need to free. */
  1464. *tok = NULL;
  1465. return TEP_EVENT_ERROR;
  1466. }
  1467. *tok = NULL;
  1468. type = process_arg(event, arg, &token);
  1469. if (test_type_token(type, token, TEP_EVENT_OP, "]"))
  1470. goto out_free;
  1471. top->op.right = arg;
  1472. free_token(token);
  1473. type = read_token_item(&token);
  1474. *tok = token;
  1475. return type;
  1476. out_free:
  1477. free_token(token);
  1478. free_arg(arg);
  1479. return TEP_EVENT_ERROR;
  1480. }
  1481. static int get_op_prio(char *op)
  1482. {
  1483. if (!op[1]) {
  1484. switch (op[0]) {
  1485. case '~':
  1486. case '!':
  1487. return 4;
  1488. case '*':
  1489. case '/':
  1490. case '%':
  1491. return 6;
  1492. case '+':
  1493. case '-':
  1494. return 7;
  1495. /* '>>' and '<<' are 8 */
  1496. case '<':
  1497. case '>':
  1498. return 9;
  1499. /* '==' and '!=' are 10 */
  1500. case '&':
  1501. return 11;
  1502. case '^':
  1503. return 12;
  1504. case '|':
  1505. return 13;
  1506. case '?':
  1507. return 16;
  1508. default:
  1509. do_warning("unknown op '%c'", op[0]);
  1510. return -1;
  1511. }
  1512. } else {
  1513. if (strcmp(op, "++") == 0 ||
  1514. strcmp(op, "--") == 0) {
  1515. return 3;
  1516. } else if (strcmp(op, ">>") == 0 ||
  1517. strcmp(op, "<<") == 0) {
  1518. return 8;
  1519. } else if (strcmp(op, ">=") == 0 ||
  1520. strcmp(op, "<=") == 0) {
  1521. return 9;
  1522. } else if (strcmp(op, "==") == 0 ||
  1523. strcmp(op, "!=") == 0) {
  1524. return 10;
  1525. } else if (strcmp(op, "&&") == 0) {
  1526. return 14;
  1527. } else if (strcmp(op, "||") == 0) {
  1528. return 15;
  1529. } else {
  1530. do_warning("unknown op '%s'", op);
  1531. return -1;
  1532. }
  1533. }
  1534. }
  1535. static int set_op_prio(struct tep_print_arg *arg)
  1536. {
  1537. /* single ops are the greatest */
  1538. if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
  1539. arg->op.prio = 0;
  1540. else
  1541. arg->op.prio = get_op_prio(arg->op.op);
  1542. return arg->op.prio;
  1543. }
  1544. /* Note, *tok does not get freed, but will most likely be saved */
  1545. static enum tep_event_type
  1546. process_op(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  1547. {
  1548. struct tep_print_arg *left, *right = NULL;
  1549. enum tep_event_type type;
  1550. char *token;
  1551. /* the op is passed in via tok */
  1552. token = *tok;
  1553. if (arg->type == TEP_PRINT_OP && !arg->op.left) {
  1554. /* handle single op */
  1555. if (token[1]) {
  1556. do_warning_event(event, "bad op token %s", token);
  1557. goto out_free;
  1558. }
  1559. switch (token[0]) {
  1560. case '~':
  1561. case '!':
  1562. case '+':
  1563. case '-':
  1564. break;
  1565. default:
  1566. do_warning_event(event, "bad op token %s", token);
  1567. goto out_free;
  1568. }
  1569. /* make an empty left */
  1570. left = alloc_arg();
  1571. if (!left)
  1572. goto out_warn_free;
  1573. left->type = TEP_PRINT_NULL;
  1574. arg->op.left = left;
  1575. right = alloc_arg();
  1576. if (!right)
  1577. goto out_warn_free;
  1578. arg->op.right = right;
  1579. /* do not free the token, it belongs to an op */
  1580. *tok = NULL;
  1581. type = process_arg(event, right, tok);
  1582. } else if (strcmp(token, "?") == 0) {
  1583. left = alloc_arg();
  1584. if (!left)
  1585. goto out_warn_free;
  1586. /* copy the top arg to the left */
  1587. *left = *arg;
  1588. arg->type = TEP_PRINT_OP;
  1589. arg->op.op = token;
  1590. arg->op.left = left;
  1591. arg->op.prio = 0;
  1592. /* it will set arg->op.right */
  1593. type = process_cond(event, arg, tok);
  1594. } else if (strcmp(token, ">>") == 0 ||
  1595. strcmp(token, "<<") == 0 ||
  1596. strcmp(token, "&") == 0 ||
  1597. strcmp(token, "|") == 0 ||
  1598. strcmp(token, "&&") == 0 ||
  1599. strcmp(token, "||") == 0 ||
  1600. strcmp(token, "-") == 0 ||
  1601. strcmp(token, "+") == 0 ||
  1602. strcmp(token, "*") == 0 ||
  1603. strcmp(token, "^") == 0 ||
  1604. strcmp(token, "/") == 0 ||
  1605. strcmp(token, "%") == 0 ||
  1606. strcmp(token, "<") == 0 ||
  1607. strcmp(token, ">") == 0 ||
  1608. strcmp(token, "<=") == 0 ||
  1609. strcmp(token, ">=") == 0 ||
  1610. strcmp(token, "==") == 0 ||
  1611. strcmp(token, "!=") == 0) {
  1612. left = alloc_arg();
  1613. if (!left)
  1614. goto out_warn_free;
  1615. /* copy the top arg to the left */
  1616. *left = *arg;
  1617. arg->type = TEP_PRINT_OP;
  1618. arg->op.op = token;
  1619. arg->op.left = left;
  1620. arg->op.right = NULL;
  1621. if (set_op_prio(arg) == -1) {
  1622. event->flags |= TEP_EVENT_FL_FAILED;
  1623. /* arg->op.op (= token) will be freed at out_free */
  1624. arg->op.op = NULL;
  1625. goto out_free;
  1626. }
  1627. type = read_token_item(&token);
  1628. *tok = token;
  1629. /* could just be a type pointer */
  1630. if ((strcmp(arg->op.op, "*") == 0) &&
  1631. type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
  1632. char *new_atom;
  1633. if (left->type != TEP_PRINT_ATOM) {
  1634. do_warning_event(event, "bad pointer type");
  1635. goto out_free;
  1636. }
  1637. new_atom = realloc(left->atom.atom,
  1638. strlen(left->atom.atom) + 3);
  1639. if (!new_atom)
  1640. goto out_warn_free;
  1641. left->atom.atom = new_atom;
  1642. strcat(left->atom.atom, " *");
  1643. free(arg->op.op);
  1644. *arg = *left;
  1645. free(left);
  1646. return type;
  1647. }
  1648. right = alloc_arg();
  1649. if (!right)
  1650. goto out_warn_free;
  1651. type = process_arg_token(event, right, tok, type);
  1652. if (type == TEP_EVENT_ERROR) {
  1653. free_arg(right);
  1654. /* token was freed in process_arg_token() via *tok */
  1655. token = NULL;
  1656. goto out_free;
  1657. }
  1658. if (right->type == TEP_PRINT_OP &&
  1659. get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
  1660. struct tep_print_arg tmp;
  1661. /* rotate ops according to the priority */
  1662. arg->op.right = right->op.left;
  1663. tmp = *arg;
  1664. *arg = *right;
  1665. *right = tmp;
  1666. arg->op.left = right;
  1667. } else {
  1668. arg->op.right = right;
  1669. }
  1670. } else if (strcmp(token, "[") == 0) {
  1671. left = alloc_arg();
  1672. if (!left)
  1673. goto out_warn_free;
  1674. *left = *arg;
  1675. arg->type = TEP_PRINT_OP;
  1676. arg->op.op = token;
  1677. arg->op.left = left;
  1678. arg->op.prio = 0;
  1679. /* it will set arg->op.right */
  1680. type = process_array(event, arg, tok);
  1681. } else {
  1682. do_warning_event(event, "unknown op '%s'", token);
  1683. event->flags |= TEP_EVENT_FL_FAILED;
  1684. /* the arg is now the left side */
  1685. goto out_free;
  1686. }
  1687. if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
  1688. int prio;
  1689. /* higher prios need to be closer to the root */
  1690. prio = get_op_prio(*tok);
  1691. if (prio > arg->op.prio)
  1692. return process_op(event, arg, tok);
  1693. return process_op(event, right, tok);
  1694. }
  1695. return type;
  1696. out_warn_free:
  1697. do_warning_event(event, "%s: not enough memory!", __func__);
  1698. out_free:
  1699. free_token(token);
  1700. *tok = NULL;
  1701. return TEP_EVENT_ERROR;
  1702. }
  1703. static enum tep_event_type
  1704. process_entry(struct tep_event_format *event __maybe_unused, struct tep_print_arg *arg,
  1705. char **tok)
  1706. {
  1707. enum tep_event_type type;
  1708. char *field;
  1709. char *token;
  1710. if (read_expected(TEP_EVENT_OP, "->") < 0)
  1711. goto out_err;
  1712. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  1713. goto out_free;
  1714. field = token;
  1715. arg->type = TEP_PRINT_FIELD;
  1716. arg->field.name = field;
  1717. if (is_flag_field) {
  1718. arg->field.field = tep_find_any_field(event, arg->field.name);
  1719. arg->field.field->flags |= TEP_FIELD_IS_FLAG;
  1720. is_flag_field = 0;
  1721. } else if (is_symbolic_field) {
  1722. arg->field.field = tep_find_any_field(event, arg->field.name);
  1723. arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
  1724. is_symbolic_field = 0;
  1725. }
  1726. type = read_token(&token);
  1727. *tok = token;
  1728. return type;
  1729. out_free:
  1730. free_token(token);
  1731. out_err:
  1732. *tok = NULL;
  1733. return TEP_EVENT_ERROR;
  1734. }
  1735. static int alloc_and_process_delim(struct tep_event_format *event, char *next_token,
  1736. struct tep_print_arg **print_arg)
  1737. {
  1738. struct tep_print_arg *field;
  1739. enum tep_event_type type;
  1740. char *token;
  1741. int ret = 0;
  1742. field = alloc_arg();
  1743. if (!field) {
  1744. do_warning_event(event, "%s: not enough memory!", __func__);
  1745. errno = ENOMEM;
  1746. return -1;
  1747. }
  1748. type = process_arg(event, field, &token);
  1749. if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) {
  1750. errno = EINVAL;
  1751. ret = -1;
  1752. free_arg(field);
  1753. goto out_free_token;
  1754. }
  1755. *print_arg = field;
  1756. out_free_token:
  1757. free_token(token);
  1758. return ret;
  1759. }
  1760. static char *arg_eval (struct tep_print_arg *arg);
  1761. static unsigned long long
  1762. eval_type_str(unsigned long long val, const char *type, int pointer)
  1763. {
  1764. int sign = 0;
  1765. char *ref;
  1766. int len;
  1767. len = strlen(type);
  1768. if (pointer) {
  1769. if (type[len-1] != '*') {
  1770. do_warning("pointer expected with non pointer type");
  1771. return val;
  1772. }
  1773. ref = malloc(len);
  1774. if (!ref) {
  1775. do_warning("%s: not enough memory!", __func__);
  1776. return val;
  1777. }
  1778. memcpy(ref, type, len);
  1779. /* chop off the " *" */
  1780. ref[len - 2] = 0;
  1781. val = eval_type_str(val, ref, 0);
  1782. free(ref);
  1783. return val;
  1784. }
  1785. /* check if this is a pointer */
  1786. if (type[len - 1] == '*')
  1787. return val;
  1788. /* Try to figure out the arg size*/
  1789. if (strncmp(type, "struct", 6) == 0)
  1790. /* all bets off */
  1791. return val;
  1792. if (strcmp(type, "u8") == 0)
  1793. return val & 0xff;
  1794. if (strcmp(type, "u16") == 0)
  1795. return val & 0xffff;
  1796. if (strcmp(type, "u32") == 0)
  1797. return val & 0xffffffff;
  1798. if (strcmp(type, "u64") == 0 ||
  1799. strcmp(type, "s64"))
  1800. return val;
  1801. if (strcmp(type, "s8") == 0)
  1802. return (unsigned long long)(char)val & 0xff;
  1803. if (strcmp(type, "s16") == 0)
  1804. return (unsigned long long)(short)val & 0xffff;
  1805. if (strcmp(type, "s32") == 0)
  1806. return (unsigned long long)(int)val & 0xffffffff;
  1807. if (strncmp(type, "unsigned ", 9) == 0) {
  1808. sign = 0;
  1809. type += 9;
  1810. }
  1811. if (strcmp(type, "char") == 0) {
  1812. if (sign)
  1813. return (unsigned long long)(char)val & 0xff;
  1814. else
  1815. return val & 0xff;
  1816. }
  1817. if (strcmp(type, "short") == 0) {
  1818. if (sign)
  1819. return (unsigned long long)(short)val & 0xffff;
  1820. else
  1821. return val & 0xffff;
  1822. }
  1823. if (strcmp(type, "int") == 0) {
  1824. if (sign)
  1825. return (unsigned long long)(int)val & 0xffffffff;
  1826. else
  1827. return val & 0xffffffff;
  1828. }
  1829. return val;
  1830. }
  1831. /*
  1832. * Try to figure out the type.
  1833. */
  1834. static unsigned long long
  1835. eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
  1836. {
  1837. if (arg->type != TEP_PRINT_TYPE) {
  1838. do_warning("expected type argument");
  1839. return 0;
  1840. }
  1841. return eval_type_str(val, arg->typecast.type, pointer);
  1842. }
  1843. static int arg_num_eval(struct tep_print_arg *arg, long long *val)
  1844. {
  1845. long long left, right;
  1846. int ret = 1;
  1847. switch (arg->type) {
  1848. case TEP_PRINT_ATOM:
  1849. *val = strtoll(arg->atom.atom, NULL, 0);
  1850. break;
  1851. case TEP_PRINT_TYPE:
  1852. ret = arg_num_eval(arg->typecast.item, val);
  1853. if (!ret)
  1854. break;
  1855. *val = eval_type(*val, arg, 0);
  1856. break;
  1857. case TEP_PRINT_OP:
  1858. switch (arg->op.op[0]) {
  1859. case '|':
  1860. ret = arg_num_eval(arg->op.left, &left);
  1861. if (!ret)
  1862. break;
  1863. ret = arg_num_eval(arg->op.right, &right);
  1864. if (!ret)
  1865. break;
  1866. if (arg->op.op[1])
  1867. *val = left || right;
  1868. else
  1869. *val = left | right;
  1870. break;
  1871. case '&':
  1872. ret = arg_num_eval(arg->op.left, &left);
  1873. if (!ret)
  1874. break;
  1875. ret = arg_num_eval(arg->op.right, &right);
  1876. if (!ret)
  1877. break;
  1878. if (arg->op.op[1])
  1879. *val = left && right;
  1880. else
  1881. *val = left & right;
  1882. break;
  1883. case '<':
  1884. ret = arg_num_eval(arg->op.left, &left);
  1885. if (!ret)
  1886. break;
  1887. ret = arg_num_eval(arg->op.right, &right);
  1888. if (!ret)
  1889. break;
  1890. switch (arg->op.op[1]) {
  1891. case 0:
  1892. *val = left < right;
  1893. break;
  1894. case '<':
  1895. *val = left << right;
  1896. break;
  1897. case '=':
  1898. *val = left <= right;
  1899. break;
  1900. default:
  1901. do_warning("unknown op '%s'", arg->op.op);
  1902. ret = 0;
  1903. }
  1904. break;
  1905. case '>':
  1906. ret = arg_num_eval(arg->op.left, &left);
  1907. if (!ret)
  1908. break;
  1909. ret = arg_num_eval(arg->op.right, &right);
  1910. if (!ret)
  1911. break;
  1912. switch (arg->op.op[1]) {
  1913. case 0:
  1914. *val = left > right;
  1915. break;
  1916. case '>':
  1917. *val = left >> right;
  1918. break;
  1919. case '=':
  1920. *val = left >= right;
  1921. break;
  1922. default:
  1923. do_warning("unknown op '%s'", arg->op.op);
  1924. ret = 0;
  1925. }
  1926. break;
  1927. case '=':
  1928. ret = arg_num_eval(arg->op.left, &left);
  1929. if (!ret)
  1930. break;
  1931. ret = arg_num_eval(arg->op.right, &right);
  1932. if (!ret)
  1933. break;
  1934. if (arg->op.op[1] != '=') {
  1935. do_warning("unknown op '%s'", arg->op.op);
  1936. ret = 0;
  1937. } else
  1938. *val = left == right;
  1939. break;
  1940. case '!':
  1941. ret = arg_num_eval(arg->op.left, &left);
  1942. if (!ret)
  1943. break;
  1944. ret = arg_num_eval(arg->op.right, &right);
  1945. if (!ret)
  1946. break;
  1947. switch (arg->op.op[1]) {
  1948. case '=':
  1949. *val = left != right;
  1950. break;
  1951. default:
  1952. do_warning("unknown op '%s'", arg->op.op);
  1953. ret = 0;
  1954. }
  1955. break;
  1956. case '-':
  1957. /* check for negative */
  1958. if (arg->op.left->type == TEP_PRINT_NULL)
  1959. left = 0;
  1960. else
  1961. ret = arg_num_eval(arg->op.left, &left);
  1962. if (!ret)
  1963. break;
  1964. ret = arg_num_eval(arg->op.right, &right);
  1965. if (!ret)
  1966. break;
  1967. *val = left - right;
  1968. break;
  1969. case '+':
  1970. if (arg->op.left->type == TEP_PRINT_NULL)
  1971. left = 0;
  1972. else
  1973. ret = arg_num_eval(arg->op.left, &left);
  1974. if (!ret)
  1975. break;
  1976. ret = arg_num_eval(arg->op.right, &right);
  1977. if (!ret)
  1978. break;
  1979. *val = left + right;
  1980. break;
  1981. case '~':
  1982. ret = arg_num_eval(arg->op.right, &right);
  1983. if (!ret)
  1984. break;
  1985. *val = ~right;
  1986. break;
  1987. default:
  1988. do_warning("unknown op '%s'", arg->op.op);
  1989. ret = 0;
  1990. }
  1991. break;
  1992. case TEP_PRINT_NULL:
  1993. case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
  1994. case TEP_PRINT_STRING:
  1995. case TEP_PRINT_BSTRING:
  1996. case TEP_PRINT_BITMASK:
  1997. default:
  1998. do_warning("invalid eval type %d", arg->type);
  1999. ret = 0;
  2000. }
  2001. return ret;
  2002. }
  2003. static char *arg_eval (struct tep_print_arg *arg)
  2004. {
  2005. long long val;
  2006. static char buf[20];
  2007. switch (arg->type) {
  2008. case TEP_PRINT_ATOM:
  2009. return arg->atom.atom;
  2010. case TEP_PRINT_TYPE:
  2011. return arg_eval(arg->typecast.item);
  2012. case TEP_PRINT_OP:
  2013. if (!arg_num_eval(arg, &val))
  2014. break;
  2015. sprintf(buf, "%lld", val);
  2016. return buf;
  2017. case TEP_PRINT_NULL:
  2018. case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
  2019. case TEP_PRINT_STRING:
  2020. case TEP_PRINT_BSTRING:
  2021. case TEP_PRINT_BITMASK:
  2022. default:
  2023. do_warning("invalid eval type %d", arg->type);
  2024. break;
  2025. }
  2026. return NULL;
  2027. }
  2028. static enum tep_event_type
  2029. process_fields(struct tep_event_format *event, struct tep_print_flag_sym **list, char **tok)
  2030. {
  2031. enum tep_event_type type;
  2032. struct tep_print_arg *arg = NULL;
  2033. struct tep_print_flag_sym *field;
  2034. char *token = *tok;
  2035. char *value;
  2036. do {
  2037. free_token(token);
  2038. type = read_token_item(&token);
  2039. if (test_type_token(type, token, TEP_EVENT_OP, "{"))
  2040. break;
  2041. arg = alloc_arg();
  2042. if (!arg)
  2043. goto out_free;
  2044. free_token(token);
  2045. type = process_arg(event, arg, &token);
  2046. if (type == TEP_EVENT_OP)
  2047. type = process_op(event, arg, &token);
  2048. if (type == TEP_EVENT_ERROR)
  2049. goto out_free;
  2050. if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
  2051. goto out_free;
  2052. field = calloc(1, sizeof(*field));
  2053. if (!field)
  2054. goto out_free;
  2055. value = arg_eval(arg);
  2056. if (value == NULL)
  2057. goto out_free_field;
  2058. field->value = strdup(value);
  2059. if (field->value == NULL)
  2060. goto out_free_field;
  2061. free_arg(arg);
  2062. arg = alloc_arg();
  2063. if (!arg)
  2064. goto out_free;
  2065. free_token(token);
  2066. type = process_arg(event, arg, &token);
  2067. if (test_type_token(type, token, TEP_EVENT_OP, "}"))
  2068. goto out_free_field;
  2069. value = arg_eval(arg);
  2070. if (value == NULL)
  2071. goto out_free_field;
  2072. field->str = strdup(value);
  2073. if (field->str == NULL)
  2074. goto out_free_field;
  2075. free_arg(arg);
  2076. arg = NULL;
  2077. *list = field;
  2078. list = &field->next;
  2079. free_token(token);
  2080. type = read_token_item(&token);
  2081. } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
  2082. *tok = token;
  2083. return type;
  2084. out_free_field:
  2085. free_flag_sym(field);
  2086. out_free:
  2087. free_arg(arg);
  2088. free_token(token);
  2089. *tok = NULL;
  2090. return TEP_EVENT_ERROR;
  2091. }
  2092. static enum tep_event_type
  2093. process_flags(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  2094. {
  2095. struct tep_print_arg *field;
  2096. enum tep_event_type type;
  2097. char *token = NULL;
  2098. memset(arg, 0, sizeof(*arg));
  2099. arg->type = TEP_PRINT_FLAGS;
  2100. field = alloc_arg();
  2101. if (!field) {
  2102. do_warning_event(event, "%s: not enough memory!", __func__);
  2103. goto out_free;
  2104. }
  2105. type = process_field_arg(event, field, &token);
  2106. /* Handle operations in the first argument */
  2107. while (type == TEP_EVENT_OP)
  2108. type = process_op(event, field, &token);
  2109. if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
  2110. goto out_free_field;
  2111. free_token(token);
  2112. arg->flags.field = field;
  2113. type = read_token_item(&token);
  2114. if (event_item_type(type)) {
  2115. arg->flags.delim = token;
  2116. type = read_token_item(&token);
  2117. }
  2118. if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
  2119. goto out_free;
  2120. type = process_fields(event, &arg->flags.flags, &token);
  2121. if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
  2122. goto out_free;
  2123. free_token(token);
  2124. type = read_token_item(tok);
  2125. return type;
  2126. out_free_field:
  2127. free_arg(field);
  2128. out_free:
  2129. free_token(token);
  2130. *tok = NULL;
  2131. return TEP_EVENT_ERROR;
  2132. }
  2133. static enum tep_event_type
  2134. process_symbols(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  2135. {
  2136. struct tep_print_arg *field;
  2137. enum tep_event_type type;
  2138. char *token = NULL;
  2139. memset(arg, 0, sizeof(*arg));
  2140. arg->type = TEP_PRINT_SYMBOL;
  2141. field = alloc_arg();
  2142. if (!field) {
  2143. do_warning_event(event, "%s: not enough memory!", __func__);
  2144. goto out_free;
  2145. }
  2146. type = process_field_arg(event, field, &token);
  2147. if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
  2148. goto out_free_field;
  2149. arg->symbol.field = field;
  2150. type = process_fields(event, &arg->symbol.symbols, &token);
  2151. if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
  2152. goto out_free;
  2153. free_token(token);
  2154. type = read_token_item(tok);
  2155. return type;
  2156. out_free_field:
  2157. free_arg(field);
  2158. out_free:
  2159. free_token(token);
  2160. *tok = NULL;
  2161. return TEP_EVENT_ERROR;
  2162. }
  2163. static enum tep_event_type
  2164. process_hex_common(struct tep_event_format *event, struct tep_print_arg *arg,
  2165. char **tok, enum tep_print_arg_type type)
  2166. {
  2167. memset(arg, 0, sizeof(*arg));
  2168. arg->type = type;
  2169. if (alloc_and_process_delim(event, ",", &arg->hex.field))
  2170. goto out;
  2171. if (alloc_and_process_delim(event, ")", &arg->hex.size))
  2172. goto free_field;
  2173. return read_token_item(tok);
  2174. free_field:
  2175. free_arg(arg->hex.field);
  2176. arg->hex.field = NULL;
  2177. out:
  2178. *tok = NULL;
  2179. return TEP_EVENT_ERROR;
  2180. }
  2181. static enum tep_event_type
  2182. process_hex(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  2183. {
  2184. return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
  2185. }
  2186. static enum tep_event_type
  2187. process_hex_str(struct tep_event_format *event, struct tep_print_arg *arg,
  2188. char **tok)
  2189. {
  2190. return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
  2191. }
  2192. static enum tep_event_type
  2193. process_int_array(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  2194. {
  2195. memset(arg, 0, sizeof(*arg));
  2196. arg->type = TEP_PRINT_INT_ARRAY;
  2197. if (alloc_and_process_delim(event, ",", &arg->int_array.field))
  2198. goto out;
  2199. if (alloc_and_process_delim(event, ",", &arg->int_array.count))
  2200. goto free_field;
  2201. if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
  2202. goto free_size;
  2203. return read_token_item(tok);
  2204. free_size:
  2205. free_arg(arg->int_array.count);
  2206. arg->int_array.count = NULL;
  2207. free_field:
  2208. free_arg(arg->int_array.field);
  2209. arg->int_array.field = NULL;
  2210. out:
  2211. *tok = NULL;
  2212. return TEP_EVENT_ERROR;
  2213. }
  2214. static enum tep_event_type
  2215. process_dynamic_array(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  2216. {
  2217. struct tep_format_field *field;
  2218. enum tep_event_type type;
  2219. char *token;
  2220. memset(arg, 0, sizeof(*arg));
  2221. arg->type = TEP_PRINT_DYNAMIC_ARRAY;
  2222. /*
  2223. * The item within the parenthesis is another field that holds
  2224. * the index into where the array starts.
  2225. */
  2226. type = read_token(&token);
  2227. *tok = token;
  2228. if (type != TEP_EVENT_ITEM)
  2229. goto out_free;
  2230. /* Find the field */
  2231. field = tep_find_field(event, token);
  2232. if (!field)
  2233. goto out_free;
  2234. arg->dynarray.field = field;
  2235. arg->dynarray.index = 0;
  2236. if (read_expected(TEP_EVENT_DELIM, ")") < 0)
  2237. goto out_free;
  2238. free_token(token);
  2239. type = read_token_item(&token);
  2240. *tok = token;
  2241. if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
  2242. return type;
  2243. free_token(token);
  2244. arg = alloc_arg();
  2245. if (!arg) {
  2246. do_warning_event(event, "%s: not enough memory!", __func__);
  2247. *tok = NULL;
  2248. return TEP_EVENT_ERROR;
  2249. }
  2250. type = process_arg(event, arg, &token);
  2251. if (type == TEP_EVENT_ERROR)
  2252. goto out_free_arg;
  2253. if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
  2254. goto out_free_arg;
  2255. free_token(token);
  2256. type = read_token_item(tok);
  2257. return type;
  2258. out_free_arg:
  2259. free_arg(arg);
  2260. out_free:
  2261. free_token(token);
  2262. *tok = NULL;
  2263. return TEP_EVENT_ERROR;
  2264. }
  2265. static enum tep_event_type
  2266. process_dynamic_array_len(struct tep_event_format *event, struct tep_print_arg *arg,
  2267. char **tok)
  2268. {
  2269. struct tep_format_field *field;
  2270. enum tep_event_type type;
  2271. char *token;
  2272. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  2273. goto out_free;
  2274. arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
  2275. /* Find the field */
  2276. field = tep_find_field(event, token);
  2277. if (!field)
  2278. goto out_free;
  2279. arg->dynarray.field = field;
  2280. arg->dynarray.index = 0;
  2281. if (read_expected(TEP_EVENT_DELIM, ")") < 0)
  2282. goto out_err;
  2283. type = read_token(&token);
  2284. *tok = token;
  2285. return type;
  2286. out_free:
  2287. free_token(token);
  2288. out_err:
  2289. *tok = NULL;
  2290. return TEP_EVENT_ERROR;
  2291. }
  2292. static enum tep_event_type
  2293. process_paren(struct tep_event_format *event, struct tep_print_arg *arg, char **tok)
  2294. {
  2295. struct tep_print_arg *item_arg;
  2296. enum tep_event_type type;
  2297. char *token;
  2298. type = process_arg(event, arg, &token);
  2299. if (type == TEP_EVENT_ERROR)
  2300. goto out_free;
  2301. if (type == TEP_EVENT_OP)
  2302. type = process_op(event, arg, &token);
  2303. if (type == TEP_EVENT_ERROR)
  2304. goto out_free;
  2305. if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
  2306. goto out_free;
  2307. free_token(token);
  2308. type = read_token_item(&token);
  2309. /*
  2310. * If the next token is an item or another open paren, then
  2311. * this was a typecast.
  2312. */
  2313. if (event_item_type(type) ||
  2314. (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
  2315. /* make this a typecast and contine */
  2316. /* prevous must be an atom */
  2317. if (arg->type != TEP_PRINT_ATOM) {
  2318. do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
  2319. goto out_free;
  2320. }
  2321. item_arg = alloc_arg();
  2322. if (!item_arg) {
  2323. do_warning_event(event, "%s: not enough memory!",
  2324. __func__);
  2325. goto out_free;
  2326. }
  2327. arg->type = TEP_PRINT_TYPE;
  2328. arg->typecast.type = arg->atom.atom;
  2329. arg->typecast.item = item_arg;
  2330. type = process_arg_token(event, item_arg, &token, type);
  2331. }
  2332. *tok = token;
  2333. return type;
  2334. out_free:
  2335. free_token(token);
  2336. *tok = NULL;
  2337. return TEP_EVENT_ERROR;
  2338. }
  2339. static enum tep_event_type
  2340. process_str(struct tep_event_format *event __maybe_unused, struct tep_print_arg *arg,
  2341. char **tok)
  2342. {
  2343. enum tep_event_type type;
  2344. char *token;
  2345. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  2346. goto out_free;
  2347. arg->type = TEP_PRINT_STRING;
  2348. arg->string.string = token;
  2349. arg->string.offset = -1;
  2350. if (read_expected(TEP_EVENT_DELIM, ")") < 0)
  2351. goto out_err;
  2352. type = read_token(&token);
  2353. *tok = token;
  2354. return type;
  2355. out_free:
  2356. free_token(token);
  2357. out_err:
  2358. *tok = NULL;
  2359. return TEP_EVENT_ERROR;
  2360. }
  2361. static enum tep_event_type
  2362. process_bitmask(struct tep_event_format *event __maybe_unused, struct tep_print_arg *arg,
  2363. char **tok)
  2364. {
  2365. enum tep_event_type type;
  2366. char *token;
  2367. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  2368. goto out_free;
  2369. arg->type = TEP_PRINT_BITMASK;
  2370. arg->bitmask.bitmask = token;
  2371. arg->bitmask.offset = -1;
  2372. if (read_expected(TEP_EVENT_DELIM, ")") < 0)
  2373. goto out_err;
  2374. type = read_token(&token);
  2375. *tok = token;
  2376. return type;
  2377. out_free:
  2378. free_token(token);
  2379. out_err:
  2380. *tok = NULL;
  2381. return TEP_EVENT_ERROR;
  2382. }
  2383. static struct tep_function_handler *
  2384. find_func_handler(struct tep_handle *pevent, char *func_name)
  2385. {
  2386. struct tep_function_handler *func;
  2387. if (!pevent)
  2388. return NULL;
  2389. for (func = pevent->func_handlers; func; func = func->next) {
  2390. if (strcmp(func->name, func_name) == 0)
  2391. break;
  2392. }
  2393. return func;
  2394. }
  2395. static void remove_func_handler(struct tep_handle *pevent, char *func_name)
  2396. {
  2397. struct tep_function_handler *func;
  2398. struct tep_function_handler **next;
  2399. next = &pevent->func_handlers;
  2400. while ((func = *next)) {
  2401. if (strcmp(func->name, func_name) == 0) {
  2402. *next = func->next;
  2403. free_func_handle(func);
  2404. break;
  2405. }
  2406. next = &func->next;
  2407. }
  2408. }
  2409. static enum tep_event_type
  2410. process_func_handler(struct tep_event_format *event, struct tep_function_handler *func,
  2411. struct tep_print_arg *arg, char **tok)
  2412. {
  2413. struct tep_print_arg **next_arg;
  2414. struct tep_print_arg *farg;
  2415. enum tep_event_type type;
  2416. char *token;
  2417. int i;
  2418. arg->type = TEP_PRINT_FUNC;
  2419. arg->func.func = func;
  2420. *tok = NULL;
  2421. next_arg = &(arg->func.args);
  2422. for (i = 0; i < func->nr_args; i++) {
  2423. farg = alloc_arg();
  2424. if (!farg) {
  2425. do_warning_event(event, "%s: not enough memory!",
  2426. __func__);
  2427. return TEP_EVENT_ERROR;
  2428. }
  2429. type = process_arg(event, farg, &token);
  2430. if (i < (func->nr_args - 1)) {
  2431. if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
  2432. do_warning_event(event,
  2433. "Error: function '%s()' expects %d arguments but event %s only uses %d",
  2434. func->name, func->nr_args,
  2435. event->name, i + 1);
  2436. goto err;
  2437. }
  2438. } else {
  2439. if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
  2440. do_warning_event(event,
  2441. "Error: function '%s()' only expects %d arguments but event %s has more",
  2442. func->name, func->nr_args, event->name);
  2443. goto err;
  2444. }
  2445. }
  2446. *next_arg = farg;
  2447. next_arg = &(farg->next);
  2448. free_token(token);
  2449. }
  2450. type = read_token(&token);
  2451. *tok = token;
  2452. return type;
  2453. err:
  2454. free_arg(farg);
  2455. free_token(token);
  2456. return TEP_EVENT_ERROR;
  2457. }
  2458. static enum tep_event_type
  2459. process_function(struct tep_event_format *event, struct tep_print_arg *arg,
  2460. char *token, char **tok)
  2461. {
  2462. struct tep_function_handler *func;
  2463. if (strcmp(token, "__print_flags") == 0) {
  2464. free_token(token);
  2465. is_flag_field = 1;
  2466. return process_flags(event, arg, tok);
  2467. }
  2468. if (strcmp(token, "__print_symbolic") == 0) {
  2469. free_token(token);
  2470. is_symbolic_field = 1;
  2471. return process_symbols(event, arg, tok);
  2472. }
  2473. if (strcmp(token, "__print_hex") == 0) {
  2474. free_token(token);
  2475. return process_hex(event, arg, tok);
  2476. }
  2477. if (strcmp(token, "__print_hex_str") == 0) {
  2478. free_token(token);
  2479. return process_hex_str(event, arg, tok);
  2480. }
  2481. if (strcmp(token, "__print_array") == 0) {
  2482. free_token(token);
  2483. return process_int_array(event, arg, tok);
  2484. }
  2485. if (strcmp(token, "__get_str") == 0) {
  2486. free_token(token);
  2487. return process_str(event, arg, tok);
  2488. }
  2489. if (strcmp(token, "__get_bitmask") == 0) {
  2490. free_token(token);
  2491. return process_bitmask(event, arg, tok);
  2492. }
  2493. if (strcmp(token, "__get_dynamic_array") == 0) {
  2494. free_token(token);
  2495. return process_dynamic_array(event, arg, tok);
  2496. }
  2497. if (strcmp(token, "__get_dynamic_array_len") == 0) {
  2498. free_token(token);
  2499. return process_dynamic_array_len(event, arg, tok);
  2500. }
  2501. func = find_func_handler(event->pevent, token);
  2502. if (func) {
  2503. free_token(token);
  2504. return process_func_handler(event, func, arg, tok);
  2505. }
  2506. do_warning_event(event, "function %s not defined", token);
  2507. free_token(token);
  2508. return TEP_EVENT_ERROR;
  2509. }
  2510. static enum tep_event_type
  2511. process_arg_token(struct tep_event_format *event, struct tep_print_arg *arg,
  2512. char **tok, enum tep_event_type type)
  2513. {
  2514. char *token;
  2515. char *atom;
  2516. token = *tok;
  2517. switch (type) {
  2518. case TEP_EVENT_ITEM:
  2519. if (strcmp(token, "REC") == 0) {
  2520. free_token(token);
  2521. type = process_entry(event, arg, &token);
  2522. break;
  2523. }
  2524. atom = token;
  2525. /* test the next token */
  2526. type = read_token_item(&token);
  2527. /*
  2528. * If the next token is a parenthesis, then this
  2529. * is a function.
  2530. */
  2531. if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
  2532. free_token(token);
  2533. token = NULL;
  2534. /* this will free atom. */
  2535. type = process_function(event, arg, atom, &token);
  2536. break;
  2537. }
  2538. /* atoms can be more than one token long */
  2539. while (type == TEP_EVENT_ITEM) {
  2540. char *new_atom;
  2541. new_atom = realloc(atom,
  2542. strlen(atom) + strlen(token) + 2);
  2543. if (!new_atom) {
  2544. free(atom);
  2545. *tok = NULL;
  2546. free_token(token);
  2547. return TEP_EVENT_ERROR;
  2548. }
  2549. atom = new_atom;
  2550. strcat(atom, " ");
  2551. strcat(atom, token);
  2552. free_token(token);
  2553. type = read_token_item(&token);
  2554. }
  2555. arg->type = TEP_PRINT_ATOM;
  2556. arg->atom.atom = atom;
  2557. break;
  2558. case TEP_EVENT_DQUOTE:
  2559. case TEP_EVENT_SQUOTE:
  2560. arg->type = TEP_PRINT_ATOM;
  2561. arg->atom.atom = token;
  2562. type = read_token_item(&token);
  2563. break;
  2564. case TEP_EVENT_DELIM:
  2565. if (strcmp(token, "(") == 0) {
  2566. free_token(token);
  2567. type = process_paren(event, arg, &token);
  2568. break;
  2569. }
  2570. case TEP_EVENT_OP:
  2571. /* handle single ops */
  2572. arg->type = TEP_PRINT_OP;
  2573. arg->op.op = token;
  2574. arg->op.left = NULL;
  2575. type = process_op(event, arg, &token);
  2576. /* On error, the op is freed */
  2577. if (type == TEP_EVENT_ERROR)
  2578. arg->op.op = NULL;
  2579. /* return error type if errored */
  2580. break;
  2581. case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
  2582. default:
  2583. do_warning_event(event, "unexpected type %d", type);
  2584. return TEP_EVENT_ERROR;
  2585. }
  2586. *tok = token;
  2587. return type;
  2588. }
  2589. static int event_read_print_args(struct tep_event_format *event, struct tep_print_arg **list)
  2590. {
  2591. enum tep_event_type type = TEP_EVENT_ERROR;
  2592. struct tep_print_arg *arg;
  2593. char *token;
  2594. int args = 0;
  2595. do {
  2596. if (type == TEP_EVENT_NEWLINE) {
  2597. type = read_token_item(&token);
  2598. continue;
  2599. }
  2600. arg = alloc_arg();
  2601. if (!arg) {
  2602. do_warning_event(event, "%s: not enough memory!",
  2603. __func__);
  2604. return -1;
  2605. }
  2606. type = process_arg(event, arg, &token);
  2607. if (type == TEP_EVENT_ERROR) {
  2608. free_token(token);
  2609. free_arg(arg);
  2610. return -1;
  2611. }
  2612. *list = arg;
  2613. args++;
  2614. if (type == TEP_EVENT_OP) {
  2615. type = process_op(event, arg, &token);
  2616. free_token(token);
  2617. if (type == TEP_EVENT_ERROR) {
  2618. *list = NULL;
  2619. free_arg(arg);
  2620. return -1;
  2621. }
  2622. list = &arg->next;
  2623. continue;
  2624. }
  2625. if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
  2626. free_token(token);
  2627. *list = arg;
  2628. list = &arg->next;
  2629. continue;
  2630. }
  2631. break;
  2632. } while (type != TEP_EVENT_NONE);
  2633. if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
  2634. free_token(token);
  2635. return args;
  2636. }
  2637. static int event_read_print(struct tep_event_format *event)
  2638. {
  2639. enum tep_event_type type;
  2640. char *token;
  2641. int ret;
  2642. if (read_expected_item(TEP_EVENT_ITEM, "print") < 0)
  2643. return -1;
  2644. if (read_expected(TEP_EVENT_ITEM, "fmt") < 0)
  2645. return -1;
  2646. if (read_expected(TEP_EVENT_OP, ":") < 0)
  2647. return -1;
  2648. if (read_expect_type(TEP_EVENT_DQUOTE, &token) < 0)
  2649. goto fail;
  2650. concat:
  2651. event->print_fmt.format = token;
  2652. event->print_fmt.args = NULL;
  2653. /* ok to have no arg */
  2654. type = read_token_item(&token);
  2655. if (type == TEP_EVENT_NONE)
  2656. return 0;
  2657. /* Handle concatenation of print lines */
  2658. if (type == TEP_EVENT_DQUOTE) {
  2659. char *cat;
  2660. if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
  2661. goto fail;
  2662. free_token(token);
  2663. free_token(event->print_fmt.format);
  2664. event->print_fmt.format = NULL;
  2665. token = cat;
  2666. goto concat;
  2667. }
  2668. if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
  2669. goto fail;
  2670. free_token(token);
  2671. ret = event_read_print_args(event, &event->print_fmt.args);
  2672. if (ret < 0)
  2673. return -1;
  2674. return ret;
  2675. fail:
  2676. free_token(token);
  2677. return -1;
  2678. }
  2679. /**
  2680. * tep_find_common_field - return a common field by event
  2681. * @event: handle for the event
  2682. * @name: the name of the common field to return
  2683. *
  2684. * Returns a common field from the event by the given @name.
  2685. * This only searchs the common fields and not all field.
  2686. */
  2687. struct tep_format_field *
  2688. tep_find_common_field(struct tep_event_format *event, const char *name)
  2689. {
  2690. struct tep_format_field *format;
  2691. for (format = event->format.common_fields;
  2692. format; format = format->next) {
  2693. if (strcmp(format->name, name) == 0)
  2694. break;
  2695. }
  2696. return format;
  2697. }
  2698. /**
  2699. * tep_find_field - find a non-common field
  2700. * @event: handle for the event
  2701. * @name: the name of the non-common field
  2702. *
  2703. * Returns a non-common field by the given @name.
  2704. * This does not search common fields.
  2705. */
  2706. struct tep_format_field *
  2707. tep_find_field(struct tep_event_format *event, const char *name)
  2708. {
  2709. struct tep_format_field *format;
  2710. for (format = event->format.fields;
  2711. format; format = format->next) {
  2712. if (strcmp(format->name, name) == 0)
  2713. break;
  2714. }
  2715. return format;
  2716. }
  2717. /**
  2718. * tep_find_any_field - find any field by name
  2719. * @event: handle for the event
  2720. * @name: the name of the field
  2721. *
  2722. * Returns a field by the given @name.
  2723. * This searchs the common field names first, then
  2724. * the non-common ones if a common one was not found.
  2725. */
  2726. struct tep_format_field *
  2727. tep_find_any_field(struct tep_event_format *event, const char *name)
  2728. {
  2729. struct tep_format_field *format;
  2730. format = tep_find_common_field(event, name);
  2731. if (format)
  2732. return format;
  2733. return tep_find_field(event, name);
  2734. }
  2735. /**
  2736. * tep_read_number - read a number from data
  2737. * @pevent: handle for the pevent
  2738. * @ptr: the raw data
  2739. * @size: the size of the data that holds the number
  2740. *
  2741. * Returns the number (converted to host) from the
  2742. * raw data.
  2743. */
  2744. unsigned long long tep_read_number(struct tep_handle *pevent,
  2745. const void *ptr, int size)
  2746. {
  2747. switch (size) {
  2748. case 1:
  2749. return *(unsigned char *)ptr;
  2750. case 2:
  2751. return data2host2(pevent, ptr);
  2752. case 4:
  2753. return data2host4(pevent, ptr);
  2754. case 8:
  2755. return data2host8(pevent, ptr);
  2756. default:
  2757. /* BUG! */
  2758. return 0;
  2759. }
  2760. }
  2761. /**
  2762. * tep_read_number_field - read a number from data
  2763. * @field: a handle to the field
  2764. * @data: the raw data to read
  2765. * @value: the value to place the number in
  2766. *
  2767. * Reads raw data according to a field offset and size,
  2768. * and translates it into @value.
  2769. *
  2770. * Returns 0 on success, -1 otherwise.
  2771. */
  2772. int tep_read_number_field(struct tep_format_field *field, const void *data,
  2773. unsigned long long *value)
  2774. {
  2775. if (!field)
  2776. return -1;
  2777. switch (field->size) {
  2778. case 1:
  2779. case 2:
  2780. case 4:
  2781. case 8:
  2782. *value = tep_read_number(field->event->pevent,
  2783. data + field->offset, field->size);
  2784. return 0;
  2785. default:
  2786. return -1;
  2787. }
  2788. }
  2789. static int get_common_info(struct tep_handle *pevent,
  2790. const char *type, int *offset, int *size)
  2791. {
  2792. struct tep_event_format *event;
  2793. struct tep_format_field *field;
  2794. /*
  2795. * All events should have the same common elements.
  2796. * Pick any event to find where the type is;
  2797. */
  2798. if (!pevent->events) {
  2799. do_warning("no event_list!");
  2800. return -1;
  2801. }
  2802. event = pevent->events[0];
  2803. field = tep_find_common_field(event, type);
  2804. if (!field)
  2805. return -1;
  2806. *offset = field->offset;
  2807. *size = field->size;
  2808. return 0;
  2809. }
  2810. static int __parse_common(struct tep_handle *pevent, void *data,
  2811. int *size, int *offset, const char *name)
  2812. {
  2813. int ret;
  2814. if (!*size) {
  2815. ret = get_common_info(pevent, name, offset, size);
  2816. if (ret < 0)
  2817. return ret;
  2818. }
  2819. return tep_read_number(pevent, data + *offset, *size);
  2820. }
  2821. static int trace_parse_common_type(struct tep_handle *pevent, void *data)
  2822. {
  2823. return __parse_common(pevent, data,
  2824. &pevent->type_size, &pevent->type_offset,
  2825. "common_type");
  2826. }
  2827. static int parse_common_pid(struct tep_handle *pevent, void *data)
  2828. {
  2829. return __parse_common(pevent, data,
  2830. &pevent->pid_size, &pevent->pid_offset,
  2831. "common_pid");
  2832. }
  2833. static int parse_common_pc(struct tep_handle *pevent, void *data)
  2834. {
  2835. return __parse_common(pevent, data,
  2836. &pevent->pc_size, &pevent->pc_offset,
  2837. "common_preempt_count");
  2838. }
  2839. static int parse_common_flags(struct tep_handle *pevent, void *data)
  2840. {
  2841. return __parse_common(pevent, data,
  2842. &pevent->flags_size, &pevent->flags_offset,
  2843. "common_flags");
  2844. }
  2845. static int parse_common_lock_depth(struct tep_handle *pevent, void *data)
  2846. {
  2847. return __parse_common(pevent, data,
  2848. &pevent->ld_size, &pevent->ld_offset,
  2849. "common_lock_depth");
  2850. }
  2851. static int parse_common_migrate_disable(struct tep_handle *pevent, void *data)
  2852. {
  2853. return __parse_common(pevent, data,
  2854. &pevent->ld_size, &pevent->ld_offset,
  2855. "common_migrate_disable");
  2856. }
  2857. static int events_id_cmp(const void *a, const void *b);
  2858. /**
  2859. * tep_find_event - find an event by given id
  2860. * @pevent: a handle to the pevent
  2861. * @id: the id of the event
  2862. *
  2863. * Returns an event that has a given @id.
  2864. */
  2865. struct tep_event_format *tep_find_event(struct tep_handle *pevent, int id)
  2866. {
  2867. struct tep_event_format **eventptr;
  2868. struct tep_event_format key;
  2869. struct tep_event_format *pkey = &key;
  2870. /* Check cache first */
  2871. if (pevent->last_event && pevent->last_event->id == id)
  2872. return pevent->last_event;
  2873. key.id = id;
  2874. eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
  2875. sizeof(*pevent->events), events_id_cmp);
  2876. if (eventptr) {
  2877. pevent->last_event = *eventptr;
  2878. return *eventptr;
  2879. }
  2880. return NULL;
  2881. }
  2882. /**
  2883. * tep_find_event_by_name - find an event by given name
  2884. * @pevent: a handle to the pevent
  2885. * @sys: the system name to search for
  2886. * @name: the name of the event to search for
  2887. *
  2888. * This returns an event with a given @name and under the system
  2889. * @sys. If @sys is NULL the first event with @name is returned.
  2890. */
  2891. struct tep_event_format *
  2892. tep_find_event_by_name(struct tep_handle *pevent,
  2893. const char *sys, const char *name)
  2894. {
  2895. struct tep_event_format *event;
  2896. int i;
  2897. if (pevent->last_event &&
  2898. strcmp(pevent->last_event->name, name) == 0 &&
  2899. (!sys || strcmp(pevent->last_event->system, sys) == 0))
  2900. return pevent->last_event;
  2901. for (i = 0; i < pevent->nr_events; i++) {
  2902. event = pevent->events[i];
  2903. if (strcmp(event->name, name) == 0) {
  2904. if (!sys)
  2905. break;
  2906. if (strcmp(event->system, sys) == 0)
  2907. break;
  2908. }
  2909. }
  2910. if (i == pevent->nr_events)
  2911. event = NULL;
  2912. pevent->last_event = event;
  2913. return event;
  2914. }
  2915. static unsigned long long
  2916. eval_num_arg(void *data, int size, struct tep_event_format *event, struct tep_print_arg *arg)
  2917. {
  2918. struct tep_handle *pevent = event->pevent;
  2919. unsigned long long val = 0;
  2920. unsigned long long left, right;
  2921. struct tep_print_arg *typearg = NULL;
  2922. struct tep_print_arg *larg;
  2923. unsigned long offset;
  2924. unsigned int field_size;
  2925. switch (arg->type) {
  2926. case TEP_PRINT_NULL:
  2927. /* ?? */
  2928. return 0;
  2929. case TEP_PRINT_ATOM:
  2930. return strtoull(arg->atom.atom, NULL, 0);
  2931. case TEP_PRINT_FIELD:
  2932. if (!arg->field.field) {
  2933. arg->field.field = tep_find_any_field(event, arg->field.name);
  2934. if (!arg->field.field)
  2935. goto out_warning_field;
  2936. }
  2937. /* must be a number */
  2938. val = tep_read_number(pevent, data + arg->field.field->offset,
  2939. arg->field.field->size);
  2940. break;
  2941. case TEP_PRINT_FLAGS:
  2942. case TEP_PRINT_SYMBOL:
  2943. case TEP_PRINT_INT_ARRAY:
  2944. case TEP_PRINT_HEX:
  2945. case TEP_PRINT_HEX_STR:
  2946. break;
  2947. case TEP_PRINT_TYPE:
  2948. val = eval_num_arg(data, size, event, arg->typecast.item);
  2949. return eval_type(val, arg, 0);
  2950. case TEP_PRINT_STRING:
  2951. case TEP_PRINT_BSTRING:
  2952. case TEP_PRINT_BITMASK:
  2953. return 0;
  2954. case TEP_PRINT_FUNC: {
  2955. struct trace_seq s;
  2956. trace_seq_init(&s);
  2957. val = process_defined_func(&s, data, size, event, arg);
  2958. trace_seq_destroy(&s);
  2959. return val;
  2960. }
  2961. case TEP_PRINT_OP:
  2962. if (strcmp(arg->op.op, "[") == 0) {
  2963. /*
  2964. * Arrays are special, since we don't want
  2965. * to read the arg as is.
  2966. */
  2967. right = eval_num_arg(data, size, event, arg->op.right);
  2968. /* handle typecasts */
  2969. larg = arg->op.left;
  2970. while (larg->type == TEP_PRINT_TYPE) {
  2971. if (!typearg)
  2972. typearg = larg;
  2973. larg = larg->typecast.item;
  2974. }
  2975. /* Default to long size */
  2976. field_size = pevent->long_size;
  2977. switch (larg->type) {
  2978. case TEP_PRINT_DYNAMIC_ARRAY:
  2979. offset = tep_read_number(pevent,
  2980. data + larg->dynarray.field->offset,
  2981. larg->dynarray.field->size);
  2982. if (larg->dynarray.field->elementsize)
  2983. field_size = larg->dynarray.field->elementsize;
  2984. /*
  2985. * The actual length of the dynamic array is stored
  2986. * in the top half of the field, and the offset
  2987. * is in the bottom half of the 32 bit field.
  2988. */
  2989. offset &= 0xffff;
  2990. offset += right;
  2991. break;
  2992. case TEP_PRINT_FIELD:
  2993. if (!larg->field.field) {
  2994. larg->field.field =
  2995. tep_find_any_field(event, larg->field.name);
  2996. if (!larg->field.field) {
  2997. arg = larg;
  2998. goto out_warning_field;
  2999. }
  3000. }
  3001. field_size = larg->field.field->elementsize;
  3002. offset = larg->field.field->offset +
  3003. right * larg->field.field->elementsize;
  3004. break;
  3005. default:
  3006. goto default_op; /* oops, all bets off */
  3007. }
  3008. val = tep_read_number(pevent,
  3009. data + offset, field_size);
  3010. if (typearg)
  3011. val = eval_type(val, typearg, 1);
  3012. break;
  3013. } else if (strcmp(arg->op.op, "?") == 0) {
  3014. left = eval_num_arg(data, size, event, arg->op.left);
  3015. arg = arg->op.right;
  3016. if (left)
  3017. val = eval_num_arg(data, size, event, arg->op.left);
  3018. else
  3019. val = eval_num_arg(data, size, event, arg->op.right);
  3020. break;
  3021. }
  3022. default_op:
  3023. left = eval_num_arg(data, size, event, arg->op.left);
  3024. right = eval_num_arg(data, size, event, arg->op.right);
  3025. switch (arg->op.op[0]) {
  3026. case '!':
  3027. switch (arg->op.op[1]) {
  3028. case 0:
  3029. val = !right;
  3030. break;
  3031. case '=':
  3032. val = left != right;
  3033. break;
  3034. default:
  3035. goto out_warning_op;
  3036. }
  3037. break;
  3038. case '~':
  3039. val = ~right;
  3040. break;
  3041. case '|':
  3042. if (arg->op.op[1])
  3043. val = left || right;
  3044. else
  3045. val = left | right;
  3046. break;
  3047. case '&':
  3048. if (arg->op.op[1])
  3049. val = left && right;
  3050. else
  3051. val = left & right;
  3052. break;
  3053. case '<':
  3054. switch (arg->op.op[1]) {
  3055. case 0:
  3056. val = left < right;
  3057. break;
  3058. case '<':
  3059. val = left << right;
  3060. break;
  3061. case '=':
  3062. val = left <= right;
  3063. break;
  3064. default:
  3065. goto out_warning_op;
  3066. }
  3067. break;
  3068. case '>':
  3069. switch (arg->op.op[1]) {
  3070. case 0:
  3071. val = left > right;
  3072. break;
  3073. case '>':
  3074. val = left >> right;
  3075. break;
  3076. case '=':
  3077. val = left >= right;
  3078. break;
  3079. default:
  3080. goto out_warning_op;
  3081. }
  3082. break;
  3083. case '=':
  3084. if (arg->op.op[1] != '=')
  3085. goto out_warning_op;
  3086. val = left == right;
  3087. break;
  3088. case '-':
  3089. val = left - right;
  3090. break;
  3091. case '+':
  3092. val = left + right;
  3093. break;
  3094. case '/':
  3095. val = left / right;
  3096. break;
  3097. case '%':
  3098. val = left % right;
  3099. break;
  3100. case '*':
  3101. val = left * right;
  3102. break;
  3103. default:
  3104. goto out_warning_op;
  3105. }
  3106. break;
  3107. case TEP_PRINT_DYNAMIC_ARRAY_LEN:
  3108. offset = tep_read_number(pevent,
  3109. data + arg->dynarray.field->offset,
  3110. arg->dynarray.field->size);
  3111. /*
  3112. * The total allocated length of the dynamic array is
  3113. * stored in the top half of the field, and the offset
  3114. * is in the bottom half of the 32 bit field.
  3115. */
  3116. val = (unsigned long long)(offset >> 16);
  3117. break;
  3118. case TEP_PRINT_DYNAMIC_ARRAY:
  3119. /* Without [], we pass the address to the dynamic data */
  3120. offset = tep_read_number(pevent,
  3121. data + arg->dynarray.field->offset,
  3122. arg->dynarray.field->size);
  3123. /*
  3124. * The total allocated length of the dynamic array is
  3125. * stored in the top half of the field, and the offset
  3126. * is in the bottom half of the 32 bit field.
  3127. */
  3128. offset &= 0xffff;
  3129. val = (unsigned long long)((unsigned long)data + offset);
  3130. break;
  3131. default: /* not sure what to do there */
  3132. return 0;
  3133. }
  3134. return val;
  3135. out_warning_op:
  3136. do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
  3137. return 0;
  3138. out_warning_field:
  3139. do_warning_event(event, "%s: field %s not found",
  3140. __func__, arg->field.name);
  3141. return 0;
  3142. }
  3143. struct flag {
  3144. const char *name;
  3145. unsigned long long value;
  3146. };
  3147. static const struct flag flags[] = {
  3148. { "HI_SOFTIRQ", 0 },
  3149. { "TIMER_SOFTIRQ", 1 },
  3150. { "NET_TX_SOFTIRQ", 2 },
  3151. { "NET_RX_SOFTIRQ", 3 },
  3152. { "BLOCK_SOFTIRQ", 4 },
  3153. { "IRQ_POLL_SOFTIRQ", 5 },
  3154. { "TASKLET_SOFTIRQ", 6 },
  3155. { "SCHED_SOFTIRQ", 7 },
  3156. { "HRTIMER_SOFTIRQ", 8 },
  3157. { "RCU_SOFTIRQ", 9 },
  3158. { "HRTIMER_NORESTART", 0 },
  3159. { "HRTIMER_RESTART", 1 },
  3160. };
  3161. static long long eval_flag(const char *flag)
  3162. {
  3163. int i;
  3164. /*
  3165. * Some flags in the format files do not get converted.
  3166. * If the flag is not numeric, see if it is something that
  3167. * we already know about.
  3168. */
  3169. if (isdigit(flag[0]))
  3170. return strtoull(flag, NULL, 0);
  3171. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  3172. if (strcmp(flags[i].name, flag) == 0)
  3173. return flags[i].value;
  3174. return -1LL;
  3175. }
  3176. static void print_str_to_seq(struct trace_seq *s, const char *format,
  3177. int len_arg, const char *str)
  3178. {
  3179. if (len_arg >= 0)
  3180. trace_seq_printf(s, format, len_arg, str);
  3181. else
  3182. trace_seq_printf(s, format, str);
  3183. }
  3184. static void print_bitmask_to_seq(struct tep_handle *pevent,
  3185. struct trace_seq *s, const char *format,
  3186. int len_arg, const void *data, int size)
  3187. {
  3188. int nr_bits = size * 8;
  3189. int str_size = (nr_bits + 3) / 4;
  3190. int len = 0;
  3191. char buf[3];
  3192. char *str;
  3193. int index;
  3194. int i;
  3195. /*
  3196. * The kernel likes to put in commas every 32 bits, we
  3197. * can do the same.
  3198. */
  3199. str_size += (nr_bits - 1) / 32;
  3200. str = malloc(str_size + 1);
  3201. if (!str) {
  3202. do_warning("%s: not enough memory!", __func__);
  3203. return;
  3204. }
  3205. str[str_size] = 0;
  3206. /* Start out with -2 for the two chars per byte */
  3207. for (i = str_size - 2; i >= 0; i -= 2) {
  3208. /*
  3209. * data points to a bit mask of size bytes.
  3210. * In the kernel, this is an array of long words, thus
  3211. * endianess is very important.
  3212. */
  3213. if (pevent->file_bigendian)
  3214. index = size - (len + 1);
  3215. else
  3216. index = len;
  3217. snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
  3218. memcpy(str + i, buf, 2);
  3219. len++;
  3220. if (!(len & 3) && i > 0) {
  3221. i--;
  3222. str[i] = ',';
  3223. }
  3224. }
  3225. if (len_arg >= 0)
  3226. trace_seq_printf(s, format, len_arg, str);
  3227. else
  3228. trace_seq_printf(s, format, str);
  3229. free(str);
  3230. }
  3231. static void print_str_arg(struct trace_seq *s, void *data, int size,
  3232. struct tep_event_format *event, const char *format,
  3233. int len_arg, struct tep_print_arg *arg)
  3234. {
  3235. struct tep_handle *pevent = event->pevent;
  3236. struct tep_print_flag_sym *flag;
  3237. struct tep_format_field *field;
  3238. struct printk_map *printk;
  3239. long long val, fval;
  3240. unsigned long long addr;
  3241. char *str;
  3242. unsigned char *hex;
  3243. int print;
  3244. int i, len;
  3245. switch (arg->type) {
  3246. case TEP_PRINT_NULL:
  3247. /* ?? */
  3248. return;
  3249. case TEP_PRINT_ATOM:
  3250. print_str_to_seq(s, format, len_arg, arg->atom.atom);
  3251. return;
  3252. case TEP_PRINT_FIELD:
  3253. field = arg->field.field;
  3254. if (!field) {
  3255. field = tep_find_any_field(event, arg->field.name);
  3256. if (!field) {
  3257. str = arg->field.name;
  3258. goto out_warning_field;
  3259. }
  3260. arg->field.field = field;
  3261. }
  3262. /* Zero sized fields, mean the rest of the data */
  3263. len = field->size ? : size - field->offset;
  3264. /*
  3265. * Some events pass in pointers. If this is not an array
  3266. * and the size is the same as long_size, assume that it
  3267. * is a pointer.
  3268. */
  3269. if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
  3270. field->size == pevent->long_size) {
  3271. /* Handle heterogeneous recording and processing
  3272. * architectures
  3273. *
  3274. * CASE I:
  3275. * Traces recorded on 32-bit devices (32-bit
  3276. * addressing) and processed on 64-bit devices:
  3277. * In this case, only 32 bits should be read.
  3278. *
  3279. * CASE II:
  3280. * Traces recorded on 64 bit devices and processed
  3281. * on 32-bit devices:
  3282. * In this case, 64 bits must be read.
  3283. */
  3284. addr = (pevent->long_size == 8) ?
  3285. *(unsigned long long *)(data + field->offset) :
  3286. (unsigned long long)*(unsigned int *)(data + field->offset);
  3287. /* Check if it matches a print format */
  3288. printk = find_printk(pevent, addr);
  3289. if (printk)
  3290. trace_seq_puts(s, printk->printk);
  3291. else
  3292. trace_seq_printf(s, "%llx", addr);
  3293. break;
  3294. }
  3295. str = malloc(len + 1);
  3296. if (!str) {
  3297. do_warning_event(event, "%s: not enough memory!",
  3298. __func__);
  3299. return;
  3300. }
  3301. memcpy(str, data + field->offset, len);
  3302. str[len] = 0;
  3303. print_str_to_seq(s, format, len_arg, str);
  3304. free(str);
  3305. break;
  3306. case TEP_PRINT_FLAGS:
  3307. val = eval_num_arg(data, size, event, arg->flags.field);
  3308. print = 0;
  3309. for (flag = arg->flags.flags; flag; flag = flag->next) {
  3310. fval = eval_flag(flag->value);
  3311. if (!val && fval < 0) {
  3312. print_str_to_seq(s, format, len_arg, flag->str);
  3313. break;
  3314. }
  3315. if (fval > 0 && (val & fval) == fval) {
  3316. if (print && arg->flags.delim)
  3317. trace_seq_puts(s, arg->flags.delim);
  3318. print_str_to_seq(s, format, len_arg, flag->str);
  3319. print = 1;
  3320. val &= ~fval;
  3321. }
  3322. }
  3323. if (val) {
  3324. if (print && arg->flags.delim)
  3325. trace_seq_puts(s, arg->flags.delim);
  3326. trace_seq_printf(s, "0x%llx", val);
  3327. }
  3328. break;
  3329. case TEP_PRINT_SYMBOL:
  3330. val = eval_num_arg(data, size, event, arg->symbol.field);
  3331. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  3332. fval = eval_flag(flag->value);
  3333. if (val == fval) {
  3334. print_str_to_seq(s, format, len_arg, flag->str);
  3335. break;
  3336. }
  3337. }
  3338. if (!flag)
  3339. trace_seq_printf(s, "0x%llx", val);
  3340. break;
  3341. case TEP_PRINT_HEX:
  3342. case TEP_PRINT_HEX_STR:
  3343. if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
  3344. unsigned long offset;
  3345. offset = tep_read_number(pevent,
  3346. data + arg->hex.field->dynarray.field->offset,
  3347. arg->hex.field->dynarray.field->size);
  3348. hex = data + (offset & 0xffff);
  3349. } else {
  3350. field = arg->hex.field->field.field;
  3351. if (!field) {
  3352. str = arg->hex.field->field.name;
  3353. field = tep_find_any_field(event, str);
  3354. if (!field)
  3355. goto out_warning_field;
  3356. arg->hex.field->field.field = field;
  3357. }
  3358. hex = data + field->offset;
  3359. }
  3360. len = eval_num_arg(data, size, event, arg->hex.size);
  3361. for (i = 0; i < len; i++) {
  3362. if (i && arg->type == TEP_PRINT_HEX)
  3363. trace_seq_putc(s, ' ');
  3364. trace_seq_printf(s, "%02x", hex[i]);
  3365. }
  3366. break;
  3367. case TEP_PRINT_INT_ARRAY: {
  3368. void *num;
  3369. int el_size;
  3370. if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
  3371. unsigned long offset;
  3372. struct tep_format_field *field =
  3373. arg->int_array.field->dynarray.field;
  3374. offset = tep_read_number(pevent,
  3375. data + field->offset,
  3376. field->size);
  3377. num = data + (offset & 0xffff);
  3378. } else {
  3379. field = arg->int_array.field->field.field;
  3380. if (!field) {
  3381. str = arg->int_array.field->field.name;
  3382. field = tep_find_any_field(event, str);
  3383. if (!field)
  3384. goto out_warning_field;
  3385. arg->int_array.field->field.field = field;
  3386. }
  3387. num = data + field->offset;
  3388. }
  3389. len = eval_num_arg(data, size, event, arg->int_array.count);
  3390. el_size = eval_num_arg(data, size, event,
  3391. arg->int_array.el_size);
  3392. for (i = 0; i < len; i++) {
  3393. if (i)
  3394. trace_seq_putc(s, ' ');
  3395. if (el_size == 1) {
  3396. trace_seq_printf(s, "%u", *(uint8_t *)num);
  3397. } else if (el_size == 2) {
  3398. trace_seq_printf(s, "%u", *(uint16_t *)num);
  3399. } else if (el_size == 4) {
  3400. trace_seq_printf(s, "%u", *(uint32_t *)num);
  3401. } else if (el_size == 8) {
  3402. trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
  3403. } else {
  3404. trace_seq_printf(s, "BAD SIZE:%d 0x%x",
  3405. el_size, *(uint8_t *)num);
  3406. el_size = 1;
  3407. }
  3408. num += el_size;
  3409. }
  3410. break;
  3411. }
  3412. case TEP_PRINT_TYPE:
  3413. break;
  3414. case TEP_PRINT_STRING: {
  3415. int str_offset;
  3416. if (arg->string.offset == -1) {
  3417. struct tep_format_field *f;
  3418. f = tep_find_any_field(event, arg->string.string);
  3419. arg->string.offset = f->offset;
  3420. }
  3421. str_offset = data2host4(pevent, data + arg->string.offset);
  3422. str_offset &= 0xffff;
  3423. print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
  3424. break;
  3425. }
  3426. case TEP_PRINT_BSTRING:
  3427. print_str_to_seq(s, format, len_arg, arg->string.string);
  3428. break;
  3429. case TEP_PRINT_BITMASK: {
  3430. int bitmask_offset;
  3431. int bitmask_size;
  3432. if (arg->bitmask.offset == -1) {
  3433. struct tep_format_field *f;
  3434. f = tep_find_any_field(event, arg->bitmask.bitmask);
  3435. arg->bitmask.offset = f->offset;
  3436. }
  3437. bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
  3438. bitmask_size = bitmask_offset >> 16;
  3439. bitmask_offset &= 0xffff;
  3440. print_bitmask_to_seq(pevent, s, format, len_arg,
  3441. data + bitmask_offset, bitmask_size);
  3442. break;
  3443. }
  3444. case TEP_PRINT_OP:
  3445. /*
  3446. * The only op for string should be ? :
  3447. */
  3448. if (arg->op.op[0] != '?')
  3449. return;
  3450. val = eval_num_arg(data, size, event, arg->op.left);
  3451. if (val)
  3452. print_str_arg(s, data, size, event,
  3453. format, len_arg, arg->op.right->op.left);
  3454. else
  3455. print_str_arg(s, data, size, event,
  3456. format, len_arg, arg->op.right->op.right);
  3457. break;
  3458. case TEP_PRINT_FUNC:
  3459. process_defined_func(s, data, size, event, arg);
  3460. break;
  3461. default:
  3462. /* well... */
  3463. break;
  3464. }
  3465. return;
  3466. out_warning_field:
  3467. do_warning_event(event, "%s: field %s not found",
  3468. __func__, arg->field.name);
  3469. }
  3470. static unsigned long long
  3471. process_defined_func(struct trace_seq *s, void *data, int size,
  3472. struct tep_event_format *event, struct tep_print_arg *arg)
  3473. {
  3474. struct tep_function_handler *func_handle = arg->func.func;
  3475. struct func_params *param;
  3476. unsigned long long *args;
  3477. unsigned long long ret;
  3478. struct tep_print_arg *farg;
  3479. struct trace_seq str;
  3480. struct save_str {
  3481. struct save_str *next;
  3482. char *str;
  3483. } *strings = NULL, *string;
  3484. int i;
  3485. if (!func_handle->nr_args) {
  3486. ret = (*func_handle->func)(s, NULL);
  3487. goto out;
  3488. }
  3489. farg = arg->func.args;
  3490. param = func_handle->params;
  3491. ret = ULLONG_MAX;
  3492. args = malloc(sizeof(*args) * func_handle->nr_args);
  3493. if (!args)
  3494. goto out;
  3495. for (i = 0; i < func_handle->nr_args; i++) {
  3496. switch (param->type) {
  3497. case TEP_FUNC_ARG_INT:
  3498. case TEP_FUNC_ARG_LONG:
  3499. case TEP_FUNC_ARG_PTR:
  3500. args[i] = eval_num_arg(data, size, event, farg);
  3501. break;
  3502. case TEP_FUNC_ARG_STRING:
  3503. trace_seq_init(&str);
  3504. print_str_arg(&str, data, size, event, "%s", -1, farg);
  3505. trace_seq_terminate(&str);
  3506. string = malloc(sizeof(*string));
  3507. if (!string) {
  3508. do_warning_event(event, "%s(%d): malloc str",
  3509. __func__, __LINE__);
  3510. goto out_free;
  3511. }
  3512. string->next = strings;
  3513. string->str = strdup(str.buffer);
  3514. if (!string->str) {
  3515. free(string);
  3516. do_warning_event(event, "%s(%d): malloc str",
  3517. __func__, __LINE__);
  3518. goto out_free;
  3519. }
  3520. args[i] = (uintptr_t)string->str;
  3521. strings = string;
  3522. trace_seq_destroy(&str);
  3523. break;
  3524. default:
  3525. /*
  3526. * Something went totally wrong, this is not
  3527. * an input error, something in this code broke.
  3528. */
  3529. do_warning_event(event, "Unexpected end of arguments\n");
  3530. goto out_free;
  3531. }
  3532. farg = farg->next;
  3533. param = param->next;
  3534. }
  3535. ret = (*func_handle->func)(s, args);
  3536. out_free:
  3537. free(args);
  3538. while (strings) {
  3539. string = strings;
  3540. strings = string->next;
  3541. free(string->str);
  3542. free(string);
  3543. }
  3544. out:
  3545. /* TBD : handle return type here */
  3546. return ret;
  3547. }
  3548. static void free_args(struct tep_print_arg *args)
  3549. {
  3550. struct tep_print_arg *next;
  3551. while (args) {
  3552. next = args->next;
  3553. free_arg(args);
  3554. args = next;
  3555. }
  3556. }
  3557. static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event_format *event)
  3558. {
  3559. struct tep_handle *pevent = event->pevent;
  3560. struct tep_format_field *field, *ip_field;
  3561. struct tep_print_arg *args, *arg, **next;
  3562. unsigned long long ip, val;
  3563. char *ptr;
  3564. void *bptr;
  3565. int vsize;
  3566. field = pevent->bprint_buf_field;
  3567. ip_field = pevent->bprint_ip_field;
  3568. if (!field) {
  3569. field = tep_find_field(event, "buf");
  3570. if (!field) {
  3571. do_warning_event(event, "can't find buffer field for binary printk");
  3572. return NULL;
  3573. }
  3574. ip_field = tep_find_field(event, "ip");
  3575. if (!ip_field) {
  3576. do_warning_event(event, "can't find ip field for binary printk");
  3577. return NULL;
  3578. }
  3579. pevent->bprint_buf_field = field;
  3580. pevent->bprint_ip_field = ip_field;
  3581. }
  3582. ip = tep_read_number(pevent, data + ip_field->offset, ip_field->size);
  3583. /*
  3584. * The first arg is the IP pointer.
  3585. */
  3586. args = alloc_arg();
  3587. if (!args) {
  3588. do_warning_event(event, "%s(%d): not enough memory!",
  3589. __func__, __LINE__);
  3590. return NULL;
  3591. }
  3592. arg = args;
  3593. arg->next = NULL;
  3594. next = &arg->next;
  3595. arg->type = TEP_PRINT_ATOM;
  3596. if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
  3597. goto out_free;
  3598. /* skip the first "%ps: " */
  3599. for (ptr = fmt + 5, bptr = data + field->offset;
  3600. bptr < data + size && *ptr; ptr++) {
  3601. int ls = 0;
  3602. if (*ptr == '%') {
  3603. process_again:
  3604. ptr++;
  3605. switch (*ptr) {
  3606. case '%':
  3607. break;
  3608. case 'l':
  3609. ls++;
  3610. goto process_again;
  3611. case 'L':
  3612. ls = 2;
  3613. goto process_again;
  3614. case '0' ... '9':
  3615. goto process_again;
  3616. case '.':
  3617. goto process_again;
  3618. case 'z':
  3619. case 'Z':
  3620. ls = 1;
  3621. goto process_again;
  3622. case 'p':
  3623. ls = 1;
  3624. if (isalnum(ptr[1])) {
  3625. ptr++;
  3626. /* Check for special pointers */
  3627. switch (*ptr) {
  3628. case 's':
  3629. case 'S':
  3630. case 'f':
  3631. case 'F':
  3632. break;
  3633. default:
  3634. /*
  3635. * Older kernels do not process
  3636. * dereferenced pointers.
  3637. * Only process if the pointer
  3638. * value is a printable.
  3639. */
  3640. if (isprint(*(char *)bptr))
  3641. goto process_string;
  3642. }
  3643. }
  3644. /* fall through */
  3645. case 'd':
  3646. case 'u':
  3647. case 'x':
  3648. case 'i':
  3649. switch (ls) {
  3650. case 0:
  3651. vsize = 4;
  3652. break;
  3653. case 1:
  3654. vsize = pevent->long_size;
  3655. break;
  3656. case 2:
  3657. vsize = 8;
  3658. break;
  3659. default:
  3660. vsize = ls; /* ? */
  3661. break;
  3662. }
  3663. /* fall through */
  3664. case '*':
  3665. if (*ptr == '*')
  3666. vsize = 4;
  3667. /* the pointers are always 4 bytes aligned */
  3668. bptr = (void *)(((unsigned long)bptr + 3) &
  3669. ~3);
  3670. val = tep_read_number(pevent, bptr, vsize);
  3671. bptr += vsize;
  3672. arg = alloc_arg();
  3673. if (!arg) {
  3674. do_warning_event(event, "%s(%d): not enough memory!",
  3675. __func__, __LINE__);
  3676. goto out_free;
  3677. }
  3678. arg->next = NULL;
  3679. arg->type = TEP_PRINT_ATOM;
  3680. if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
  3681. free(arg);
  3682. goto out_free;
  3683. }
  3684. *next = arg;
  3685. next = &arg->next;
  3686. /*
  3687. * The '*' case means that an arg is used as the length.
  3688. * We need to continue to figure out for what.
  3689. */
  3690. if (*ptr == '*')
  3691. goto process_again;
  3692. break;
  3693. case 's':
  3694. process_string:
  3695. arg = alloc_arg();
  3696. if (!arg) {
  3697. do_warning_event(event, "%s(%d): not enough memory!",
  3698. __func__, __LINE__);
  3699. goto out_free;
  3700. }
  3701. arg->next = NULL;
  3702. arg->type = TEP_PRINT_BSTRING;
  3703. arg->string.string = strdup(bptr);
  3704. if (!arg->string.string)
  3705. goto out_free;
  3706. bptr += strlen(bptr) + 1;
  3707. *next = arg;
  3708. next = &arg->next;
  3709. default:
  3710. break;
  3711. }
  3712. }
  3713. }
  3714. return args;
  3715. out_free:
  3716. free_args(args);
  3717. return NULL;
  3718. }
  3719. static char *
  3720. get_bprint_format(void *data, int size __maybe_unused,
  3721. struct tep_event_format *event)
  3722. {
  3723. struct tep_handle *pevent = event->pevent;
  3724. unsigned long long addr;
  3725. struct tep_format_field *field;
  3726. struct printk_map *printk;
  3727. char *format;
  3728. field = pevent->bprint_fmt_field;
  3729. if (!field) {
  3730. field = tep_find_field(event, "fmt");
  3731. if (!field) {
  3732. do_warning_event(event, "can't find format field for binary printk");
  3733. return NULL;
  3734. }
  3735. pevent->bprint_fmt_field = field;
  3736. }
  3737. addr = tep_read_number(pevent, data + field->offset, field->size);
  3738. printk = find_printk(pevent, addr);
  3739. if (!printk) {
  3740. if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
  3741. return NULL;
  3742. return format;
  3743. }
  3744. if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
  3745. return NULL;
  3746. return format;
  3747. }
  3748. static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
  3749. struct tep_event_format *event, struct tep_print_arg *arg)
  3750. {
  3751. unsigned char *buf;
  3752. const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
  3753. if (arg->type == TEP_PRINT_FUNC) {
  3754. process_defined_func(s, data, size, event, arg);
  3755. return;
  3756. }
  3757. if (arg->type != TEP_PRINT_FIELD) {
  3758. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
  3759. arg->type);
  3760. return;
  3761. }
  3762. if (mac == 'm')
  3763. fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
  3764. if (!arg->field.field) {
  3765. arg->field.field =
  3766. tep_find_any_field(event, arg->field.name);
  3767. if (!arg->field.field) {
  3768. do_warning_event(event, "%s: field %s not found",
  3769. __func__, arg->field.name);
  3770. return;
  3771. }
  3772. }
  3773. if (arg->field.field->size != 6) {
  3774. trace_seq_printf(s, "INVALIDMAC");
  3775. return;
  3776. }
  3777. buf = data + arg->field.field->offset;
  3778. trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  3779. }
  3780. static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
  3781. {
  3782. const char *fmt;
  3783. if (i == 'i')
  3784. fmt = "%03d.%03d.%03d.%03d";
  3785. else
  3786. fmt = "%d.%d.%d.%d";
  3787. trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
  3788. }
  3789. static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
  3790. {
  3791. return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
  3792. (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
  3793. }
  3794. static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
  3795. {
  3796. return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
  3797. }
  3798. static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
  3799. {
  3800. int i, j, range;
  3801. unsigned char zerolength[8];
  3802. int longest = 1;
  3803. int colonpos = -1;
  3804. uint16_t word;
  3805. uint8_t hi, lo;
  3806. bool needcolon = false;
  3807. bool useIPv4;
  3808. struct in6_addr in6;
  3809. memcpy(&in6, addr, sizeof(struct in6_addr));
  3810. useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
  3811. memset(zerolength, 0, sizeof(zerolength));
  3812. if (useIPv4)
  3813. range = 6;
  3814. else
  3815. range = 8;
  3816. /* find position of longest 0 run */
  3817. for (i = 0; i < range; i++) {
  3818. for (j = i; j < range; j++) {
  3819. if (in6.s6_addr16[j] != 0)
  3820. break;
  3821. zerolength[i]++;
  3822. }
  3823. }
  3824. for (i = 0; i < range; i++) {
  3825. if (zerolength[i] > longest) {
  3826. longest = zerolength[i];
  3827. colonpos = i;
  3828. }
  3829. }
  3830. if (longest == 1) /* don't compress a single 0 */
  3831. colonpos = -1;
  3832. /* emit address */
  3833. for (i = 0; i < range; i++) {
  3834. if (i == colonpos) {
  3835. if (needcolon || i == 0)
  3836. trace_seq_printf(s, ":");
  3837. trace_seq_printf(s, ":");
  3838. needcolon = false;
  3839. i += longest - 1;
  3840. continue;
  3841. }
  3842. if (needcolon) {
  3843. trace_seq_printf(s, ":");
  3844. needcolon = false;
  3845. }
  3846. /* hex u16 without leading 0s */
  3847. word = ntohs(in6.s6_addr16[i]);
  3848. hi = word >> 8;
  3849. lo = word & 0xff;
  3850. if (hi)
  3851. trace_seq_printf(s, "%x%02x", hi, lo);
  3852. else
  3853. trace_seq_printf(s, "%x", lo);
  3854. needcolon = true;
  3855. }
  3856. if (useIPv4) {
  3857. if (needcolon)
  3858. trace_seq_printf(s, ":");
  3859. print_ip4_addr(s, 'I', &in6.s6_addr[12]);
  3860. }
  3861. return;
  3862. }
  3863. static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
  3864. {
  3865. int j;
  3866. for (j = 0; j < 16; j += 2) {
  3867. trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
  3868. if (i == 'I' && j < 14)
  3869. trace_seq_printf(s, ":");
  3870. }
  3871. }
  3872. /*
  3873. * %pi4 print an IPv4 address with leading zeros
  3874. * %pI4 print an IPv4 address without leading zeros
  3875. * %pi6 print an IPv6 address without colons
  3876. * %pI6 print an IPv6 address with colons
  3877. * %pI6c print an IPv6 address in compressed form with colons
  3878. * %pISpc print an IP address based on sockaddr; p adds port.
  3879. */
  3880. static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
  3881. void *data, int size, struct tep_event_format *event,
  3882. struct tep_print_arg *arg)
  3883. {
  3884. unsigned char *buf;
  3885. if (arg->type == TEP_PRINT_FUNC) {
  3886. process_defined_func(s, data, size, event, arg);
  3887. return 0;
  3888. }
  3889. if (arg->type != TEP_PRINT_FIELD) {
  3890. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
  3891. return 0;
  3892. }
  3893. if (!arg->field.field) {
  3894. arg->field.field =
  3895. tep_find_any_field(event, arg->field.name);
  3896. if (!arg->field.field) {
  3897. do_warning("%s: field %s not found",
  3898. __func__, arg->field.name);
  3899. return 0;
  3900. }
  3901. }
  3902. buf = data + arg->field.field->offset;
  3903. if (arg->field.field->size != 4) {
  3904. trace_seq_printf(s, "INVALIDIPv4");
  3905. return 0;
  3906. }
  3907. print_ip4_addr(s, i, buf);
  3908. return 0;
  3909. }
  3910. static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
  3911. void *data, int size, struct tep_event_format *event,
  3912. struct tep_print_arg *arg)
  3913. {
  3914. char have_c = 0;
  3915. unsigned char *buf;
  3916. int rc = 0;
  3917. /* pI6c */
  3918. if (i == 'I' && *ptr == 'c') {
  3919. have_c = 1;
  3920. ptr++;
  3921. rc++;
  3922. }
  3923. if (arg->type == TEP_PRINT_FUNC) {
  3924. process_defined_func(s, data, size, event, arg);
  3925. return rc;
  3926. }
  3927. if (arg->type != TEP_PRINT_FIELD) {
  3928. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
  3929. return rc;
  3930. }
  3931. if (!arg->field.field) {
  3932. arg->field.field =
  3933. tep_find_any_field(event, arg->field.name);
  3934. if (!arg->field.field) {
  3935. do_warning("%s: field %s not found",
  3936. __func__, arg->field.name);
  3937. return rc;
  3938. }
  3939. }
  3940. buf = data + arg->field.field->offset;
  3941. if (arg->field.field->size != 16) {
  3942. trace_seq_printf(s, "INVALIDIPv6");
  3943. return rc;
  3944. }
  3945. if (have_c)
  3946. print_ip6c_addr(s, buf);
  3947. else
  3948. print_ip6_addr(s, i, buf);
  3949. return rc;
  3950. }
  3951. static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
  3952. void *data, int size, struct tep_event_format *event,
  3953. struct tep_print_arg *arg)
  3954. {
  3955. char have_c = 0, have_p = 0;
  3956. unsigned char *buf;
  3957. struct sockaddr_storage *sa;
  3958. int rc = 0;
  3959. /* pISpc */
  3960. if (i == 'I') {
  3961. if (*ptr == 'p') {
  3962. have_p = 1;
  3963. ptr++;
  3964. rc++;
  3965. }
  3966. if (*ptr == 'c') {
  3967. have_c = 1;
  3968. ptr++;
  3969. rc++;
  3970. }
  3971. }
  3972. if (arg->type == TEP_PRINT_FUNC) {
  3973. process_defined_func(s, data, size, event, arg);
  3974. return rc;
  3975. }
  3976. if (arg->type != TEP_PRINT_FIELD) {
  3977. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
  3978. return rc;
  3979. }
  3980. if (!arg->field.field) {
  3981. arg->field.field =
  3982. tep_find_any_field(event, arg->field.name);
  3983. if (!arg->field.field) {
  3984. do_warning("%s: field %s not found",
  3985. __func__, arg->field.name);
  3986. return rc;
  3987. }
  3988. }
  3989. sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
  3990. if (sa->ss_family == AF_INET) {
  3991. struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
  3992. if (arg->field.field->size < sizeof(struct sockaddr_in)) {
  3993. trace_seq_printf(s, "INVALIDIPv4");
  3994. return rc;
  3995. }
  3996. print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
  3997. if (have_p)
  3998. trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
  3999. } else if (sa->ss_family == AF_INET6) {
  4000. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
  4001. if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
  4002. trace_seq_printf(s, "INVALIDIPv6");
  4003. return rc;
  4004. }
  4005. if (have_p)
  4006. trace_seq_printf(s, "[");
  4007. buf = (unsigned char *) &sa6->sin6_addr;
  4008. if (have_c)
  4009. print_ip6c_addr(s, buf);
  4010. else
  4011. print_ip6_addr(s, i, buf);
  4012. if (have_p)
  4013. trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
  4014. }
  4015. return rc;
  4016. }
  4017. static int print_ip_arg(struct trace_seq *s, const char *ptr,
  4018. void *data, int size, struct tep_event_format *event,
  4019. struct tep_print_arg *arg)
  4020. {
  4021. char i = *ptr; /* 'i' or 'I' */
  4022. char ver;
  4023. int rc = 0;
  4024. ptr++;
  4025. rc++;
  4026. ver = *ptr;
  4027. ptr++;
  4028. rc++;
  4029. switch (ver) {
  4030. case '4':
  4031. rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
  4032. break;
  4033. case '6':
  4034. rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
  4035. break;
  4036. case 'S':
  4037. rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
  4038. break;
  4039. default:
  4040. return 0;
  4041. }
  4042. return rc;
  4043. }
  4044. static int is_printable_array(char *p, unsigned int len)
  4045. {
  4046. unsigned int i;
  4047. for (i = 0; i < len && p[i]; i++)
  4048. if (!isprint(p[i]) && !isspace(p[i]))
  4049. return 0;
  4050. return 1;
  4051. }
  4052. void tep_print_field(struct trace_seq *s, void *data,
  4053. struct tep_format_field *field)
  4054. {
  4055. unsigned long long val;
  4056. unsigned int offset, len, i;
  4057. struct tep_handle *pevent = field->event->pevent;
  4058. if (field->flags & TEP_FIELD_IS_ARRAY) {
  4059. offset = field->offset;
  4060. len = field->size;
  4061. if (field->flags & TEP_FIELD_IS_DYNAMIC) {
  4062. val = tep_read_number(pevent, data + offset, len);
  4063. offset = val;
  4064. len = offset >> 16;
  4065. offset &= 0xffff;
  4066. }
  4067. if (field->flags & TEP_FIELD_IS_STRING &&
  4068. is_printable_array(data + offset, len)) {
  4069. trace_seq_printf(s, "%s", (char *)data + offset);
  4070. } else {
  4071. trace_seq_puts(s, "ARRAY[");
  4072. for (i = 0; i < len; i++) {
  4073. if (i)
  4074. trace_seq_puts(s, ", ");
  4075. trace_seq_printf(s, "%02x",
  4076. *((unsigned char *)data + offset + i));
  4077. }
  4078. trace_seq_putc(s, ']');
  4079. field->flags &= ~TEP_FIELD_IS_STRING;
  4080. }
  4081. } else {
  4082. val = tep_read_number(pevent, data + field->offset,
  4083. field->size);
  4084. if (field->flags & TEP_FIELD_IS_POINTER) {
  4085. trace_seq_printf(s, "0x%llx", val);
  4086. } else if (field->flags & TEP_FIELD_IS_SIGNED) {
  4087. switch (field->size) {
  4088. case 4:
  4089. /*
  4090. * If field is long then print it in hex.
  4091. * A long usually stores pointers.
  4092. */
  4093. if (field->flags & TEP_FIELD_IS_LONG)
  4094. trace_seq_printf(s, "0x%x", (int)val);
  4095. else
  4096. trace_seq_printf(s, "%d", (int)val);
  4097. break;
  4098. case 2:
  4099. trace_seq_printf(s, "%2d", (short)val);
  4100. break;
  4101. case 1:
  4102. trace_seq_printf(s, "%1d", (char)val);
  4103. break;
  4104. default:
  4105. trace_seq_printf(s, "%lld", val);
  4106. }
  4107. } else {
  4108. if (field->flags & TEP_FIELD_IS_LONG)
  4109. trace_seq_printf(s, "0x%llx", val);
  4110. else
  4111. trace_seq_printf(s, "%llu", val);
  4112. }
  4113. }
  4114. }
  4115. void tep_print_fields(struct trace_seq *s, void *data,
  4116. int size __maybe_unused, struct tep_event_format *event)
  4117. {
  4118. struct tep_format_field *field;
  4119. field = event->format.fields;
  4120. while (field) {
  4121. trace_seq_printf(s, " %s=", field->name);
  4122. tep_print_field(s, data, field);
  4123. field = field->next;
  4124. }
  4125. }
  4126. static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event_format *event)
  4127. {
  4128. struct tep_handle *pevent = event->pevent;
  4129. struct tep_print_fmt *print_fmt = &event->print_fmt;
  4130. struct tep_print_arg *arg = print_fmt->args;
  4131. struct tep_print_arg *args = NULL;
  4132. const char *ptr = print_fmt->format;
  4133. unsigned long long val;
  4134. struct func_map *func;
  4135. const char *saveptr;
  4136. struct trace_seq p;
  4137. char *bprint_fmt = NULL;
  4138. char format[32];
  4139. int show_func;
  4140. int len_as_arg;
  4141. int len_arg;
  4142. int len;
  4143. int ls;
  4144. if (event->flags & TEP_EVENT_FL_FAILED) {
  4145. trace_seq_printf(s, "[FAILED TO PARSE]");
  4146. tep_print_fields(s, data, size, event);
  4147. return;
  4148. }
  4149. if (event->flags & TEP_EVENT_FL_ISBPRINT) {
  4150. bprint_fmt = get_bprint_format(data, size, event);
  4151. args = make_bprint_args(bprint_fmt, data, size, event);
  4152. arg = args;
  4153. ptr = bprint_fmt;
  4154. }
  4155. for (; *ptr; ptr++) {
  4156. ls = 0;
  4157. if (*ptr == '\\') {
  4158. ptr++;
  4159. switch (*ptr) {
  4160. case 'n':
  4161. trace_seq_putc(s, '\n');
  4162. break;
  4163. case 't':
  4164. trace_seq_putc(s, '\t');
  4165. break;
  4166. case 'r':
  4167. trace_seq_putc(s, '\r');
  4168. break;
  4169. case '\\':
  4170. trace_seq_putc(s, '\\');
  4171. break;
  4172. default:
  4173. trace_seq_putc(s, *ptr);
  4174. break;
  4175. }
  4176. } else if (*ptr == '%') {
  4177. saveptr = ptr;
  4178. show_func = 0;
  4179. len_as_arg = 0;
  4180. cont_process:
  4181. ptr++;
  4182. switch (*ptr) {
  4183. case '%':
  4184. trace_seq_putc(s, '%');
  4185. break;
  4186. case '#':
  4187. /* FIXME: need to handle properly */
  4188. goto cont_process;
  4189. case 'h':
  4190. ls--;
  4191. goto cont_process;
  4192. case 'l':
  4193. ls++;
  4194. goto cont_process;
  4195. case 'L':
  4196. ls = 2;
  4197. goto cont_process;
  4198. case '*':
  4199. /* The argument is the length. */
  4200. if (!arg) {
  4201. do_warning_event(event, "no argument match");
  4202. event->flags |= TEP_EVENT_FL_FAILED;
  4203. goto out_failed;
  4204. }
  4205. len_arg = eval_num_arg(data, size, event, arg);
  4206. len_as_arg = 1;
  4207. arg = arg->next;
  4208. goto cont_process;
  4209. case '.':
  4210. case 'z':
  4211. case 'Z':
  4212. case '0' ... '9':
  4213. case '-':
  4214. goto cont_process;
  4215. case 'p':
  4216. if (pevent->long_size == 4)
  4217. ls = 1;
  4218. else
  4219. ls = 2;
  4220. if (isalnum(ptr[1]))
  4221. ptr++;
  4222. if (arg->type == TEP_PRINT_BSTRING) {
  4223. trace_seq_puts(s, arg->string.string);
  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 |= TEP_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 |= TEP_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 |= TEP_EVENT_FL_FAILED;
  4321. }
  4322. break;
  4323. case 's':
  4324. if (!arg) {
  4325. do_warning_event(event, "no matching argument");
  4326. event->flags |= TEP_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 |= TEP_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 & TEP_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 tep_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 tep_event_format *event,
  4597. struct tep_record *record)
  4598. {
  4599. int print_pretty = 1;
  4600. if (event->pevent->print_raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
  4601. tep_print_fields(s, record->data, record->size, event);
  4602. else {
  4603. if (event->handler && !(event->flags & TEP_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 tep_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 tep_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 tep_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 tep_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 tep_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 tep_event_format * const * ea = a;
  4754. struct tep_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 tep_event_format * const * ea = a;
  4764. struct tep_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 tep_event_format * const * ea = a;
  4777. struct tep_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 tep_event_format **tep_list_events(struct tep_handle *pevent, enum tep_event_sort_type sort_type)
  4788. {
  4789. struct tep_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 == TEP_EVENT_SORT_ID) {
  4803. pevent->last_type = sort_type;
  4804. return events;
  4805. }
  4806. }
  4807. switch (sort_type) {
  4808. case TEP_EVENT_SORT_ID:
  4809. sort = events_id_cmp;
  4810. break;
  4811. case TEP_EVENT_SORT_NAME:
  4812. sort = events_name_cmp;
  4813. break;
  4814. case TEP_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 tep_format_field **
  4825. get_event_fields(const char *type, const char *name,
  4826. int count, struct tep_format_field *list)
  4827. {
  4828. struct tep_format_field **fields;
  4829. struct tep_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 tep_format_field **tep_event_common_fields(struct tep_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 tep_format_field **tep_event_fields(struct tep_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 tep_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 tep_print_arg *args)
  4885. {
  4886. int print_paren = 1;
  4887. struct trace_seq s;
  4888. switch (args->type) {
  4889. case TEP_PRINT_NULL:
  4890. printf("null");
  4891. break;
  4892. case TEP_PRINT_ATOM:
  4893. printf("%s", args->atom.atom);
  4894. break;
  4895. case TEP_PRINT_FIELD:
  4896. printf("REC->%s", args->field.name);
  4897. break;
  4898. case TEP_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 TEP_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 TEP_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 TEP_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 TEP_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 TEP_PRINT_STRING:
  4942. case TEP_PRINT_BSTRING:
  4943. printf("__get_str(%s)", args->string.string);
  4944. break;
  4945. case TEP_PRINT_BITMASK:
  4946. printf("__get_bitmask(%s)", args->bitmask.bitmask);
  4947. break;
  4948. case TEP_PRINT_TYPE:
  4949. printf("(%s)", args->typecast.type);
  4950. print_args(args->typecast.item);
  4951. break;
  4952. case TEP_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(TEP_EVENT_ITEM, "field") < 0)
  4982. return;
  4983. if (read_expected(TEP_EVENT_OP, ":") < 0)
  4984. return;
  4985. /* type */
  4986. if (read_expect_type(TEP_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(TEP_EVENT_ITEM, field) < 0)
  4994. return;
  4995. } else {
  4996. if (read_expect_type(TEP_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(TEP_EVENT_OP, ";") < 0)
  5003. return;
  5004. if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
  5005. return;
  5006. if (read_expected(TEP_EVENT_OP, ":") < 0)
  5007. return;
  5008. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  5009. goto fail;
  5010. *offset = atoi(token);
  5011. free_token(token);
  5012. if (read_expected(TEP_EVENT_OP, ";") < 0)
  5013. return;
  5014. if (read_expected(TEP_EVENT_ITEM, "size") < 0)
  5015. return;
  5016. if (read_expected(TEP_EVENT_OP, ":") < 0)
  5017. return;
  5018. if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
  5019. goto fail;
  5020. *size = atoi(token);
  5021. free_token(token);
  5022. if (read_expected(TEP_EVENT_OP, ";") < 0)
  5023. return;
  5024. type = read_token(&token);
  5025. if (type != TEP_EVENT_NEWLINE) {
  5026. /* newer versions of the kernel have a "signed" type */
  5027. if (type != TEP_EVENT_ITEM)
  5028. goto fail;
  5029. if (strcmp(token, "signed") != 0)
  5030. goto fail;
  5031. free_token(token);
  5032. if (read_expected(TEP_EVENT_OP, ":") < 0)
  5033. return;
  5034. if (read_expect_type(TEP_EVENT_ITEM, &token))
  5035. goto fail;
  5036. free_token(token);
  5037. if (read_expected(TEP_EVENT_OP, ";") < 0)
  5038. return;
  5039. if (read_expect_type(TEP_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 tep_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 tep_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 tep_event_format **eventp,
  5143. struct tep_handle *pevent, const char *buf,
  5144. unsigned long size, const char *sys)
  5145. {
  5146. struct tep_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 |= TEP_EVENT_FL_ISFTRACE;
  5160. if (strcmp(event->name, "bprint") == 0)
  5161. event->flags |= TEP_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 & TEP_EVENT_FL_ISFTRACE)) {
  5197. struct tep_format_field *field;
  5198. struct tep_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 |= TEP_EVENT_FL_FAILED;
  5205. return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
  5206. }
  5207. arg->type = TEP_PRINT_FIELD;
  5208. arg->field.name = strdup(field->name);
  5209. if (!arg->field.name) {
  5210. event->flags |= TEP_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 |= TEP_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 tep_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 tep_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 tep_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 tep_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 tep_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 tep_event_format *event,
  5349. const char *name, struct tep_record *record,
  5350. int *len, int err)
  5351. {
  5352. struct tep_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 & TEP_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 tep_event_format *event,
  5389. const char *name, struct tep_record *record,
  5390. unsigned long long *val, int err)
  5391. {
  5392. struct tep_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 tep_event_format *event,
  5410. const char *name, struct tep_record *record,
  5411. unsigned long long *val, int err)
  5412. {
  5413. struct tep_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 tep_event_format *event,
  5431. const char *name, struct tep_record *record,
  5432. unsigned long long *val, int err)
  5433. {
  5434. struct tep_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 tep_event_format *event, const char *name,
  5453. struct tep_record *record, int err)
  5454. {
  5455. struct tep_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 tep_event_format *event, const char *name,
  5480. struct tep_record *record, int err)
  5481. {
  5482. struct tep_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 tep_event_format *search_event(struct tep_handle *pevent, int id,
  5616. const char *sys_name,
  5617. const char *event_name)
  5618. {
  5619. struct tep_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 tep_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 tep_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 tep_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 tep_format_field *field)
  5776. {
  5777. struct tep_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 tep_format *format)
  5785. {
  5786. free_format_fields(format->common_fields);
  5787. free_format_fields(format->fields);
  5788. }
  5789. void tep_free_format(struct tep_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. }