event-parse.c 105 KB

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