event-parse.c 112 KB

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