event-parse.c 147 KB

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