event-parse.c 106 KB

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