event-parse.c 109 KB

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