event-parse.c 128 KB

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