event-parse.c 142 KB

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