event-parse.c 121 KB

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