event-parse.c 109 KB

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