event-parse.c 135 KB

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