event-parse.c 105 KB

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