event-parse.c 144 KB

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