event-parse.c 147 KB

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