event-parse.c 107 KB

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