event-parse.c 140 KB

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