event-parse.c 126 KB

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