event-parse.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374
  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. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <ctype.h>
  32. #include <errno.h>
  33. #include <stdint.h>
  34. #include "event-parse.h"
  35. #include "event-utils.h"
  36. static const char *input_buf;
  37. static unsigned long long input_buf_ptr;
  38. static unsigned long long input_buf_siz;
  39. static int is_flag_field;
  40. static int is_symbolic_field;
  41. static int show_warning = 1;
  42. #define do_warning(fmt, ...) \
  43. do { \
  44. if (show_warning) \
  45. warning(fmt, ##__VA_ARGS__); \
  46. } while (0)
  47. static void init_input_buf(const char *buf, unsigned long long size)
  48. {
  49. input_buf = buf;
  50. input_buf_siz = size;
  51. input_buf_ptr = 0;
  52. }
  53. const char *pevent_get_input_buf(void)
  54. {
  55. return input_buf;
  56. }
  57. unsigned long long pevent_get_input_buf_ptr(void)
  58. {
  59. return input_buf_ptr;
  60. }
  61. struct event_handler {
  62. struct event_handler *next;
  63. int id;
  64. const char *sys_name;
  65. const char *event_name;
  66. pevent_event_handler_func func;
  67. void *context;
  68. };
  69. struct pevent_func_params {
  70. struct pevent_func_params *next;
  71. enum pevent_func_arg_type type;
  72. };
  73. struct pevent_function_handler {
  74. struct pevent_function_handler *next;
  75. enum pevent_func_arg_type ret_type;
  76. char *name;
  77. pevent_func_handler func;
  78. struct pevent_func_params *params;
  79. int nr_args;
  80. };
  81. static unsigned long long
  82. process_defined_func(struct trace_seq *s, void *data, int size,
  83. struct event_format *event, struct print_arg *arg);
  84. static void free_func_handle(struct pevent_function_handler *func);
  85. /**
  86. * pevent_buffer_init - init buffer for parsing
  87. * @buf: buffer to parse
  88. * @size: the size of the buffer
  89. *
  90. * For use with pevent_read_token(), this initializes the internal
  91. * buffer that pevent_read_token() will parse.
  92. */
  93. void pevent_buffer_init(const char *buf, unsigned long long size)
  94. {
  95. init_input_buf(buf, size);
  96. }
  97. void breakpoint(void)
  98. {
  99. static int x;
  100. x++;
  101. }
  102. struct print_arg *alloc_arg(void)
  103. {
  104. struct print_arg *arg;
  105. arg = malloc_or_die(sizeof(*arg));
  106. if (!arg)
  107. return NULL;
  108. memset(arg, 0, sizeof(*arg));
  109. return arg;
  110. }
  111. struct cmdline {
  112. char *comm;
  113. int pid;
  114. };
  115. static int cmdline_cmp(const void *a, const void *b)
  116. {
  117. const struct cmdline *ca = a;
  118. const struct cmdline *cb = b;
  119. if (ca->pid < cb->pid)
  120. return -1;
  121. if (ca->pid > cb->pid)
  122. return 1;
  123. return 0;
  124. }
  125. struct cmdline_list {
  126. struct cmdline_list *next;
  127. char *comm;
  128. int pid;
  129. };
  130. static int cmdline_init(struct pevent *pevent)
  131. {
  132. struct cmdline_list *cmdlist = pevent->cmdlist;
  133. struct cmdline_list *item;
  134. struct cmdline *cmdlines;
  135. int i;
  136. cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count);
  137. i = 0;
  138. while (cmdlist) {
  139. cmdlines[i].pid = cmdlist->pid;
  140. cmdlines[i].comm = cmdlist->comm;
  141. i++;
  142. item = cmdlist;
  143. cmdlist = cmdlist->next;
  144. free(item);
  145. }
  146. qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  147. pevent->cmdlines = cmdlines;
  148. pevent->cmdlist = NULL;
  149. return 0;
  150. }
  151. static char *find_cmdline(struct pevent *pevent, int pid)
  152. {
  153. const struct cmdline *comm;
  154. struct cmdline key;
  155. if (!pid)
  156. return "<idle>";
  157. if (!pevent->cmdlines)
  158. cmdline_init(pevent);
  159. key.pid = pid;
  160. comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  161. sizeof(*pevent->cmdlines), cmdline_cmp);
  162. if (comm)
  163. return comm->comm;
  164. return "<...>";
  165. }
  166. /**
  167. * pevent_pid_is_registered - return if a pid has a cmdline registered
  168. * @pevent: handle for the pevent
  169. * @pid: The pid to check if it has a cmdline registered with.
  170. *
  171. * Returns 1 if the pid has a cmdline mapped to it
  172. * 0 otherwise.
  173. */
  174. int pevent_pid_is_registered(struct pevent *pevent, int pid)
  175. {
  176. const struct cmdline *comm;
  177. struct cmdline key;
  178. if (!pid)
  179. return 1;
  180. if (!pevent->cmdlines)
  181. cmdline_init(pevent);
  182. key.pid = pid;
  183. comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  184. sizeof(*pevent->cmdlines), cmdline_cmp);
  185. if (comm)
  186. return 1;
  187. return 0;
  188. }
  189. /*
  190. * If the command lines have been converted to an array, then
  191. * we must add this pid. This is much slower than when cmdlines
  192. * are added before the array is initialized.
  193. */
  194. static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
  195. {
  196. struct cmdline *cmdlines = pevent->cmdlines;
  197. const struct cmdline *cmdline;
  198. struct cmdline key;
  199. if (!pid)
  200. return 0;
  201. /* avoid duplicates */
  202. key.pid = pid;
  203. cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
  204. sizeof(*pevent->cmdlines), cmdline_cmp);
  205. if (cmdline) {
  206. errno = EEXIST;
  207. return -1;
  208. }
  209. cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
  210. if (!cmdlines) {
  211. errno = ENOMEM;
  212. return -1;
  213. }
  214. cmdlines[pevent->cmdline_count].pid = pid;
  215. cmdlines[pevent->cmdline_count].comm = strdup(comm);
  216. if (!cmdlines[pevent->cmdline_count].comm)
  217. die("malloc comm");
  218. if (cmdlines[pevent->cmdline_count].comm)
  219. pevent->cmdline_count++;
  220. qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
  221. pevent->cmdlines = cmdlines;
  222. return 0;
  223. }
  224. /**
  225. * pevent_register_comm - register a pid / comm mapping
  226. * @pevent: handle for the pevent
  227. * @comm: the command line to register
  228. * @pid: the pid to map the command line to
  229. *
  230. * This adds a mapping to search for command line names with
  231. * a given pid. The comm is duplicated.
  232. */
  233. int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
  234. {
  235. struct cmdline_list *item;
  236. if (pevent->cmdlines)
  237. return add_new_comm(pevent, comm, pid);
  238. item = malloc_or_die(sizeof(*item));
  239. item->comm = strdup(comm);
  240. if (!item->comm)
  241. die("malloc comm");
  242. item->pid = pid;
  243. item->next = pevent->cmdlist;
  244. pevent->cmdlist = item;
  245. pevent->cmdline_count++;
  246. return 0;
  247. }
  248. struct func_map {
  249. unsigned long long addr;
  250. char *func;
  251. char *mod;
  252. };
  253. struct func_list {
  254. struct func_list *next;
  255. unsigned long long addr;
  256. char *func;
  257. char *mod;
  258. };
  259. static int func_cmp(const void *a, const void *b)
  260. {
  261. const struct func_map *fa = a;
  262. const struct func_map *fb = b;
  263. if (fa->addr < fb->addr)
  264. return -1;
  265. if (fa->addr > fb->addr)
  266. return 1;
  267. return 0;
  268. }
  269. /*
  270. * We are searching for a record in between, not an exact
  271. * match.
  272. */
  273. static int func_bcmp(const void *a, const void *b)
  274. {
  275. const struct func_map *fa = a;
  276. const struct func_map *fb = b;
  277. if ((fa->addr == fb->addr) ||
  278. (fa->addr > fb->addr &&
  279. fa->addr < (fb+1)->addr))
  280. return 0;
  281. if (fa->addr < fb->addr)
  282. return -1;
  283. return 1;
  284. }
  285. static int func_map_init(struct pevent *pevent)
  286. {
  287. struct func_list *funclist;
  288. struct func_list *item;
  289. struct func_map *func_map;
  290. int i;
  291. func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1));
  292. funclist = pevent->funclist;
  293. i = 0;
  294. while (funclist) {
  295. func_map[i].func = funclist->func;
  296. func_map[i].addr = funclist->addr;
  297. func_map[i].mod = funclist->mod;
  298. i++;
  299. item = funclist;
  300. funclist = funclist->next;
  301. free(item);
  302. }
  303. qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
  304. /*
  305. * Add a special record at the end.
  306. */
  307. func_map[pevent->func_count].func = NULL;
  308. func_map[pevent->func_count].addr = 0;
  309. func_map[pevent->func_count].mod = NULL;
  310. pevent->func_map = func_map;
  311. pevent->funclist = NULL;
  312. return 0;
  313. }
  314. static struct func_map *
  315. find_func(struct pevent *pevent, unsigned long long addr)
  316. {
  317. struct func_map *func;
  318. struct func_map key;
  319. if (!pevent->func_map)
  320. func_map_init(pevent);
  321. key.addr = addr;
  322. func = bsearch(&key, pevent->func_map, pevent->func_count,
  323. sizeof(*pevent->func_map), func_bcmp);
  324. return func;
  325. }
  326. /**
  327. * pevent_find_function - find a function by a given address
  328. * @pevent: handle for the pevent
  329. * @addr: the address to find the function with
  330. *
  331. * Returns a pointer to the function stored that has the given
  332. * address. Note, the address does not have to be exact, it
  333. * will select the function that would contain the address.
  334. */
  335. const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
  336. {
  337. struct func_map *map;
  338. map = find_func(pevent, addr);
  339. if (!map)
  340. return NULL;
  341. return map->func;
  342. }
  343. /**
  344. * pevent_find_function_address - find a function address by a given address
  345. * @pevent: handle for the pevent
  346. * @addr: the address to find the function with
  347. *
  348. * Returns the address the function starts at. This can be used in
  349. * conjunction with pevent_find_function to print both the function
  350. * name and the function offset.
  351. */
  352. unsigned long long
  353. pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
  354. {
  355. struct func_map *map;
  356. map = find_func(pevent, addr);
  357. if (!map)
  358. return 0;
  359. return map->addr;
  360. }
  361. /**
  362. * pevent_register_function - register a function with a given address
  363. * @pevent: handle for the pevent
  364. * @function: the function name to register
  365. * @addr: the address the function starts at
  366. * @mod: the kernel module the function may be in (NULL for none)
  367. *
  368. * This registers a function name with an address and module.
  369. * The @func passed in is duplicated.
  370. */
  371. int pevent_register_function(struct pevent *pevent, char *func,
  372. unsigned long long addr, char *mod)
  373. {
  374. struct func_list *item;
  375. item = malloc_or_die(sizeof(*item));
  376. item->next = pevent->funclist;
  377. item->func = strdup(func);
  378. if (mod)
  379. item->mod = strdup(mod);
  380. else
  381. item->mod = NULL;
  382. item->addr = addr;
  383. if (!item->func || (mod && !item->mod))
  384. die("malloc func");
  385. pevent->funclist = item;
  386. pevent->func_count++;
  387. return 0;
  388. }
  389. /**
  390. * pevent_print_funcs - print out the stored functions
  391. * @pevent: handle for the pevent
  392. *
  393. * This prints out the stored functions.
  394. */
  395. void pevent_print_funcs(struct pevent *pevent)
  396. {
  397. int i;
  398. if (!pevent->func_map)
  399. func_map_init(pevent);
  400. for (i = 0; i < (int)pevent->func_count; i++) {
  401. printf("%016llx %s",
  402. pevent->func_map[i].addr,
  403. pevent->func_map[i].func);
  404. if (pevent->func_map[i].mod)
  405. printf(" [%s]\n", pevent->func_map[i].mod);
  406. else
  407. printf("\n");
  408. }
  409. }
  410. struct printk_map {
  411. unsigned long long addr;
  412. char *printk;
  413. };
  414. struct printk_list {
  415. struct printk_list *next;
  416. unsigned long long addr;
  417. char *printk;
  418. };
  419. static int printk_cmp(const void *a, const void *b)
  420. {
  421. const struct printk_map *pa = a;
  422. const struct printk_map *pb = b;
  423. if (pa->addr < pb->addr)
  424. return -1;
  425. if (pa->addr > pb->addr)
  426. return 1;
  427. return 0;
  428. }
  429. static void printk_map_init(struct pevent *pevent)
  430. {
  431. struct printk_list *printklist;
  432. struct printk_list *item;
  433. struct printk_map *printk_map;
  434. int i;
  435. printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
  436. printklist = pevent->printklist;
  437. i = 0;
  438. while (printklist) {
  439. printk_map[i].printk = printklist->printk;
  440. printk_map[i].addr = printklist->addr;
  441. i++;
  442. item = printklist;
  443. printklist = printklist->next;
  444. free(item);
  445. }
  446. qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
  447. pevent->printk_map = printk_map;
  448. pevent->printklist = NULL;
  449. }
  450. static struct printk_map *
  451. find_printk(struct pevent *pevent, unsigned long long addr)
  452. {
  453. struct printk_map *printk;
  454. struct printk_map key;
  455. if (!pevent->printk_map)
  456. printk_map_init(pevent);
  457. key.addr = addr;
  458. printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
  459. sizeof(*pevent->printk_map), printk_cmp);
  460. return printk;
  461. }
  462. /**
  463. * pevent_register_print_string - register a string by its address
  464. * @pevent: handle for the pevent
  465. * @fmt: the string format to register
  466. * @addr: the address the string was located at
  467. *
  468. * This registers a string by the address it was stored in the kernel.
  469. * The @fmt passed in is duplicated.
  470. */
  471. int pevent_register_print_string(struct pevent *pevent, char *fmt,
  472. unsigned long long addr)
  473. {
  474. struct printk_list *item;
  475. item = malloc_or_die(sizeof(*item));
  476. item->next = pevent->printklist;
  477. item->printk = strdup(fmt);
  478. item->addr = addr;
  479. if (!item->printk)
  480. die("malloc fmt");
  481. pevent->printklist = item;
  482. pevent->printk_count++;
  483. return 0;
  484. }
  485. /**
  486. * pevent_print_printk - print out the stored strings
  487. * @pevent: handle for the pevent
  488. *
  489. * This prints the string formats that were stored.
  490. */
  491. void pevent_print_printk(struct pevent *pevent)
  492. {
  493. int i;
  494. if (!pevent->printk_map)
  495. printk_map_init(pevent);
  496. for (i = 0; i < (int)pevent->printk_count; i++) {
  497. printf("%016llx %s\n",
  498. pevent->printk_map[i].addr,
  499. pevent->printk_map[i].printk);
  500. }
  501. }
  502. static struct event_format *alloc_event(void)
  503. {
  504. struct event_format *event;
  505. event = malloc(sizeof(*event));
  506. if (!event)
  507. return NULL;
  508. memset(event, 0, sizeof(*event));
  509. return event;
  510. }
  511. static void add_event(struct pevent *pevent, struct event_format *event)
  512. {
  513. int i;
  514. pevent->events = realloc(pevent->events, sizeof(event) *
  515. (pevent->nr_events + 1));
  516. if (!pevent->events)
  517. die("Can not allocate events");
  518. for (i = 0; i < pevent->nr_events; i++) {
  519. if (pevent->events[i]->id > event->id)
  520. break;
  521. }
  522. if (i < pevent->nr_events)
  523. memmove(&pevent->events[i + 1],
  524. &pevent->events[i],
  525. sizeof(event) * (pevent->nr_events - i));
  526. pevent->events[i] = event;
  527. pevent->nr_events++;
  528. event->pevent = pevent;
  529. }
  530. static int event_item_type(enum event_type type)
  531. {
  532. switch (type) {
  533. case EVENT_ITEM ... EVENT_SQUOTE:
  534. return 1;
  535. case EVENT_ERROR ... EVENT_DELIM:
  536. default:
  537. return 0;
  538. }
  539. }
  540. static void free_flag_sym(struct print_flag_sym *fsym)
  541. {
  542. struct print_flag_sym *next;
  543. while (fsym) {
  544. next = fsym->next;
  545. free(fsym->value);
  546. free(fsym->str);
  547. free(fsym);
  548. fsym = next;
  549. }
  550. }
  551. static void free_arg(struct print_arg *arg)
  552. {
  553. struct print_arg *farg;
  554. if (!arg)
  555. return;
  556. switch (arg->type) {
  557. case PRINT_ATOM:
  558. free(arg->atom.atom);
  559. break;
  560. case PRINT_FIELD:
  561. free(arg->field.name);
  562. break;
  563. case PRINT_FLAGS:
  564. free_arg(arg->flags.field);
  565. free(arg->flags.delim);
  566. free_flag_sym(arg->flags.flags);
  567. break;
  568. case PRINT_SYMBOL:
  569. free_arg(arg->symbol.field);
  570. free_flag_sym(arg->symbol.symbols);
  571. break;
  572. case PRINT_HEX:
  573. free_arg(arg->hex.field);
  574. free_arg(arg->hex.size);
  575. break;
  576. case PRINT_TYPE:
  577. free(arg->typecast.type);
  578. free_arg(arg->typecast.item);
  579. break;
  580. case PRINT_STRING:
  581. case PRINT_BSTRING:
  582. free(arg->string.string);
  583. break;
  584. case PRINT_DYNAMIC_ARRAY:
  585. free(arg->dynarray.index);
  586. break;
  587. case PRINT_OP:
  588. free(arg->op.op);
  589. free_arg(arg->op.left);
  590. free_arg(arg->op.right);
  591. break;
  592. case PRINT_FUNC:
  593. while (arg->func.args) {
  594. farg = arg->func.args;
  595. arg->func.args = farg->next;
  596. free_arg(farg);
  597. }
  598. break;
  599. case PRINT_NULL:
  600. default:
  601. break;
  602. }
  603. free(arg);
  604. }
  605. static enum event_type get_type(int ch)
  606. {
  607. if (ch == '\n')
  608. return EVENT_NEWLINE;
  609. if (isspace(ch))
  610. return EVENT_SPACE;
  611. if (isalnum(ch) || ch == '_')
  612. return EVENT_ITEM;
  613. if (ch == '\'')
  614. return EVENT_SQUOTE;
  615. if (ch == '"')
  616. return EVENT_DQUOTE;
  617. if (!isprint(ch))
  618. return EVENT_NONE;
  619. if (ch == '(' || ch == ')' || ch == ',')
  620. return EVENT_DELIM;
  621. return EVENT_OP;
  622. }
  623. static int __read_char(void)
  624. {
  625. if (input_buf_ptr >= input_buf_siz)
  626. return -1;
  627. return input_buf[input_buf_ptr++];
  628. }
  629. static int __peek_char(void)
  630. {
  631. if (input_buf_ptr >= input_buf_siz)
  632. return -1;
  633. return input_buf[input_buf_ptr];
  634. }
  635. /**
  636. * pevent_peek_char - peek at the next character that will be read
  637. *
  638. * Returns the next character read, or -1 if end of buffer.
  639. */
  640. int pevent_peek_char(void)
  641. {
  642. return __peek_char();
  643. }
  644. static int extend_token(char **tok, char *buf, int size)
  645. {
  646. char *newtok = realloc(*tok, size);
  647. if (!newtok) {
  648. free(*tok);
  649. *tok = NULL;
  650. return -1;
  651. }
  652. if (!*tok)
  653. strcpy(newtok, buf);
  654. else
  655. strcat(newtok, buf);
  656. *tok = newtok;
  657. return 0;
  658. }
  659. static enum event_type force_token(const char *str, char **tok);
  660. static enum event_type __read_token(char **tok)
  661. {
  662. char buf[BUFSIZ];
  663. int ch, last_ch, quote_ch, next_ch;
  664. int i = 0;
  665. int tok_size = 0;
  666. enum event_type type;
  667. *tok = NULL;
  668. ch = __read_char();
  669. if (ch < 0)
  670. return EVENT_NONE;
  671. type = get_type(ch);
  672. if (type == EVENT_NONE)
  673. return type;
  674. buf[i++] = ch;
  675. switch (type) {
  676. case EVENT_NEWLINE:
  677. case EVENT_DELIM:
  678. *tok = malloc_or_die(2);
  679. (*tok)[0] = ch;
  680. (*tok)[1] = 0;
  681. return type;
  682. case EVENT_OP:
  683. switch (ch) {
  684. case '-':
  685. next_ch = __peek_char();
  686. if (next_ch == '>') {
  687. buf[i++] = __read_char();
  688. break;
  689. }
  690. /* fall through */
  691. case '+':
  692. case '|':
  693. case '&':
  694. case '>':
  695. case '<':
  696. last_ch = ch;
  697. ch = __peek_char();
  698. if (ch != last_ch)
  699. goto test_equal;
  700. buf[i++] = __read_char();
  701. switch (last_ch) {
  702. case '>':
  703. case '<':
  704. goto test_equal;
  705. default:
  706. break;
  707. }
  708. break;
  709. case '!':
  710. case '=':
  711. goto test_equal;
  712. default: /* what should we do instead? */
  713. break;
  714. }
  715. buf[i] = 0;
  716. *tok = strdup(buf);
  717. return type;
  718. test_equal:
  719. ch = __peek_char();
  720. if (ch == '=')
  721. buf[i++] = __read_char();
  722. goto out;
  723. case EVENT_DQUOTE:
  724. case EVENT_SQUOTE:
  725. /* don't keep quotes */
  726. i--;
  727. quote_ch = ch;
  728. last_ch = 0;
  729. concat:
  730. do {
  731. if (i == (BUFSIZ - 1)) {
  732. buf[i] = 0;
  733. tok_size += BUFSIZ;
  734. if (extend_token(tok, buf, tok_size) < 0)
  735. return EVENT_NONE;
  736. i = 0;
  737. }
  738. last_ch = ch;
  739. ch = __read_char();
  740. buf[i++] = ch;
  741. /* the '\' '\' will cancel itself */
  742. if (ch == '\\' && last_ch == '\\')
  743. last_ch = 0;
  744. } while (ch != quote_ch || last_ch == '\\');
  745. /* remove the last quote */
  746. i--;
  747. /*
  748. * For strings (double quotes) check the next token.
  749. * If it is another string, concatinate the two.
  750. */
  751. if (type == EVENT_DQUOTE) {
  752. unsigned long long save_input_buf_ptr = input_buf_ptr;
  753. do {
  754. ch = __read_char();
  755. } while (isspace(ch));
  756. if (ch == '"')
  757. goto concat;
  758. input_buf_ptr = save_input_buf_ptr;
  759. }
  760. goto out;
  761. case EVENT_ERROR ... EVENT_SPACE:
  762. case EVENT_ITEM:
  763. default:
  764. break;
  765. }
  766. while (get_type(__peek_char()) == type) {
  767. if (i == (BUFSIZ - 1)) {
  768. buf[i] = 0;
  769. tok_size += BUFSIZ;
  770. if (extend_token(tok, buf, tok_size) < 0)
  771. return EVENT_NONE;
  772. i = 0;
  773. }
  774. ch = __read_char();
  775. buf[i++] = ch;
  776. }
  777. out:
  778. buf[i] = 0;
  779. if (extend_token(tok, buf, tok_size + i + 1) < 0)
  780. return EVENT_NONE;
  781. if (type == EVENT_ITEM) {
  782. /*
  783. * Older versions of the kernel has a bug that
  784. * creates invalid symbols and will break the mac80211
  785. * parsing. This is a work around to that bug.
  786. *
  787. * See Linux kernel commit:
  788. * 811cb50baf63461ce0bdb234927046131fc7fa8b
  789. */
  790. if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
  791. free(*tok);
  792. *tok = NULL;
  793. return force_token("\"\%s\" ", tok);
  794. } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
  795. free(*tok);
  796. *tok = NULL;
  797. return force_token("\" sta:%pM\" ", tok);
  798. } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
  799. free(*tok);
  800. *tok = NULL;
  801. return force_token("\" vif:%p(%d)\" ", tok);
  802. }
  803. }
  804. return type;
  805. }
  806. static enum event_type force_token(const char *str, char **tok)
  807. {
  808. const char *save_input_buf;
  809. unsigned long long save_input_buf_ptr;
  810. unsigned long long save_input_buf_siz;
  811. enum event_type type;
  812. /* save off the current input pointers */
  813. save_input_buf = input_buf;
  814. save_input_buf_ptr = input_buf_ptr;
  815. save_input_buf_siz = input_buf_siz;
  816. init_input_buf(str, strlen(str));
  817. type = __read_token(tok);
  818. /* reset back to original token */
  819. input_buf = save_input_buf;
  820. input_buf_ptr = save_input_buf_ptr;
  821. input_buf_siz = save_input_buf_siz;
  822. return type;
  823. }
  824. static void free_token(char *tok)
  825. {
  826. if (tok)
  827. free(tok);
  828. }
  829. static enum event_type read_token(char **tok)
  830. {
  831. enum event_type type;
  832. for (;;) {
  833. type = __read_token(tok);
  834. if (type != EVENT_SPACE)
  835. return type;
  836. free_token(*tok);
  837. }
  838. /* not reached */
  839. *tok = NULL;
  840. return EVENT_NONE;
  841. }
  842. /**
  843. * pevent_read_token - access to utilites to use the pevent parser
  844. * @tok: The token to return
  845. *
  846. * This will parse tokens from the string given by
  847. * pevent_init_data().
  848. *
  849. * Returns the token type.
  850. */
  851. enum event_type pevent_read_token(char **tok)
  852. {
  853. return read_token(tok);
  854. }
  855. /**
  856. * pevent_free_token - free a token returned by pevent_read_token
  857. * @token: the token to free
  858. */
  859. void pevent_free_token(char *token)
  860. {
  861. free_token(token);
  862. }
  863. /* no newline */
  864. static enum event_type read_token_item(char **tok)
  865. {
  866. enum event_type type;
  867. for (;;) {
  868. type = __read_token(tok);
  869. if (type != EVENT_SPACE && type != EVENT_NEWLINE)
  870. return type;
  871. free_token(*tok);
  872. *tok = NULL;
  873. }
  874. /* not reached */
  875. *tok = NULL;
  876. return EVENT_NONE;
  877. }
  878. static int test_type(enum event_type type, enum event_type expect)
  879. {
  880. if (type != expect) {
  881. do_warning("Error: expected type %d but read %d",
  882. expect, type);
  883. return -1;
  884. }
  885. return 0;
  886. }
  887. static int test_type_token(enum event_type type, const char *token,
  888. enum event_type expect, const char *expect_tok)
  889. {
  890. if (type != expect) {
  891. do_warning("Error: expected type %d but read %d",
  892. expect, type);
  893. return -1;
  894. }
  895. if (strcmp(token, expect_tok) != 0) {
  896. do_warning("Error: expected '%s' but read '%s'",
  897. expect_tok, token);
  898. return -1;
  899. }
  900. return 0;
  901. }
  902. static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
  903. {
  904. enum event_type type;
  905. if (newline_ok)
  906. type = read_token(tok);
  907. else
  908. type = read_token_item(tok);
  909. return test_type(type, expect);
  910. }
  911. static int read_expect_type(enum event_type expect, char **tok)
  912. {
  913. return __read_expect_type(expect, tok, 1);
  914. }
  915. static int __read_expected(enum event_type expect, const char *str,
  916. int newline_ok)
  917. {
  918. enum event_type type;
  919. char *token;
  920. int ret;
  921. if (newline_ok)
  922. type = read_token(&token);
  923. else
  924. type = read_token_item(&token);
  925. ret = test_type_token(type, token, expect, str);
  926. free_token(token);
  927. return ret;
  928. }
  929. static int read_expected(enum event_type expect, const char *str)
  930. {
  931. return __read_expected(expect, str, 1);
  932. }
  933. static int read_expected_item(enum event_type expect, const char *str)
  934. {
  935. return __read_expected(expect, str, 0);
  936. }
  937. static char *event_read_name(void)
  938. {
  939. char *token;
  940. if (read_expected(EVENT_ITEM, "name") < 0)
  941. return NULL;
  942. if (read_expected(EVENT_OP, ":") < 0)
  943. return NULL;
  944. if (read_expect_type(EVENT_ITEM, &token) < 0)
  945. goto fail;
  946. return token;
  947. fail:
  948. free_token(token);
  949. return NULL;
  950. }
  951. static int event_read_id(void)
  952. {
  953. char *token;
  954. int id;
  955. if (read_expected_item(EVENT_ITEM, "ID") < 0)
  956. return -1;
  957. if (read_expected(EVENT_OP, ":") < 0)
  958. return -1;
  959. if (read_expect_type(EVENT_ITEM, &token) < 0)
  960. goto fail;
  961. id = strtoul(token, NULL, 0);
  962. free_token(token);
  963. return id;
  964. fail:
  965. free_token(token);
  966. return -1;
  967. }
  968. static int field_is_string(struct format_field *field)
  969. {
  970. if ((field->flags & FIELD_IS_ARRAY) &&
  971. (strstr(field->type, "char") || strstr(field->type, "u8") ||
  972. strstr(field->type, "s8")))
  973. return 1;
  974. return 0;
  975. }
  976. static int field_is_dynamic(struct format_field *field)
  977. {
  978. if (strncmp(field->type, "__data_loc", 10) == 0)
  979. return 1;
  980. return 0;
  981. }
  982. static int field_is_long(struct format_field *field)
  983. {
  984. /* includes long long */
  985. if (strstr(field->type, "long"))
  986. return 1;
  987. return 0;
  988. }
  989. static int event_read_fields(struct event_format *event, struct format_field **fields)
  990. {
  991. struct format_field *field = NULL;
  992. enum event_type type;
  993. char *token;
  994. char *last_token;
  995. int count = 0;
  996. do {
  997. type = read_token(&token);
  998. if (type == EVENT_NEWLINE) {
  999. free_token(token);
  1000. return count;
  1001. }
  1002. count++;
  1003. if (test_type_token(type, token, EVENT_ITEM, "field"))
  1004. goto fail;
  1005. free_token(token);
  1006. type = read_token(&token);
  1007. /*
  1008. * The ftrace fields may still use the "special" name.
  1009. * Just ignore it.
  1010. */
  1011. if (event->flags & EVENT_FL_ISFTRACE &&
  1012. type == EVENT_ITEM && strcmp(token, "special") == 0) {
  1013. free_token(token);
  1014. type = read_token(&token);
  1015. }
  1016. if (test_type_token(type, token, EVENT_OP, ":") < 0)
  1017. goto fail;
  1018. free_token(token);
  1019. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1020. goto fail;
  1021. last_token = token;
  1022. field = malloc_or_die(sizeof(*field));
  1023. memset(field, 0, sizeof(*field));
  1024. field->event = event;
  1025. /* read the rest of the type */
  1026. for (;;) {
  1027. type = read_token(&token);
  1028. if (type == EVENT_ITEM ||
  1029. (type == EVENT_OP && strcmp(token, "*") == 0) ||
  1030. /*
  1031. * Some of the ftrace fields are broken and have
  1032. * an illegal "." in them.
  1033. */
  1034. (event->flags & EVENT_FL_ISFTRACE &&
  1035. type == EVENT_OP && strcmp(token, ".") == 0)) {
  1036. if (strcmp(token, "*") == 0)
  1037. field->flags |= FIELD_IS_POINTER;
  1038. if (field->type) {
  1039. char *new_type;
  1040. new_type = realloc(field->type,
  1041. strlen(field->type) +
  1042. strlen(last_token) + 2);
  1043. if (!new_type) {
  1044. free(last_token);
  1045. goto fail;
  1046. }
  1047. field->type = new_type;
  1048. strcat(field->type, " ");
  1049. strcat(field->type, last_token);
  1050. free(last_token);
  1051. } else
  1052. field->type = last_token;
  1053. last_token = token;
  1054. continue;
  1055. }
  1056. break;
  1057. }
  1058. if (!field->type) {
  1059. die("no type found");
  1060. goto fail;
  1061. }
  1062. field->name = last_token;
  1063. if (test_type(type, EVENT_OP))
  1064. goto fail;
  1065. if (strcmp(token, "[") == 0) {
  1066. enum event_type last_type = type;
  1067. char *brackets = token;
  1068. char *new_brackets;
  1069. int len;
  1070. field->flags |= FIELD_IS_ARRAY;
  1071. type = read_token(&token);
  1072. if (type == EVENT_ITEM)
  1073. field->arraylen = strtoul(token, NULL, 0);
  1074. else
  1075. field->arraylen = 0;
  1076. while (strcmp(token, "]") != 0) {
  1077. if (last_type == EVENT_ITEM &&
  1078. type == EVENT_ITEM)
  1079. len = 2;
  1080. else
  1081. len = 1;
  1082. last_type = type;
  1083. new_brackets = realloc(brackets,
  1084. strlen(brackets) +
  1085. strlen(token) + len);
  1086. if (!new_brackets) {
  1087. free(brackets);
  1088. goto fail;
  1089. }
  1090. brackets = new_brackets;
  1091. if (len == 2)
  1092. strcat(brackets, " ");
  1093. strcat(brackets, token);
  1094. /* We only care about the last token */
  1095. field->arraylen = strtoul(token, NULL, 0);
  1096. free_token(token);
  1097. type = read_token(&token);
  1098. if (type == EVENT_NONE) {
  1099. die("failed to find token");
  1100. goto fail;
  1101. }
  1102. }
  1103. free_token(token);
  1104. new_brackets = realloc(brackets, strlen(brackets) + 2);
  1105. if (!new_brackets) {
  1106. free(brackets);
  1107. goto fail;
  1108. }
  1109. brackets = new_brackets;
  1110. strcat(brackets, "]");
  1111. /* add brackets to type */
  1112. type = read_token(&token);
  1113. /*
  1114. * If the next token is not an OP, then it is of
  1115. * the format: type [] item;
  1116. */
  1117. if (type == EVENT_ITEM) {
  1118. char *new_type;
  1119. new_type = realloc(field->type,
  1120. strlen(field->type) +
  1121. strlen(field->name) +
  1122. strlen(brackets) + 2);
  1123. if (!new_type) {
  1124. free(brackets);
  1125. goto fail;
  1126. }
  1127. field->type = new_type;
  1128. strcat(field->type, " ");
  1129. strcat(field->type, field->name);
  1130. free_token(field->name);
  1131. strcat(field->type, brackets);
  1132. field->name = token;
  1133. type = read_token(&token);
  1134. } else {
  1135. char *new_type;
  1136. new_type = realloc(field->type,
  1137. strlen(field->type) +
  1138. strlen(brackets) + 1);
  1139. if (!new_type) {
  1140. free(brackets);
  1141. goto fail;
  1142. }
  1143. field->type = new_type;
  1144. strcat(field->type, brackets);
  1145. }
  1146. free(brackets);
  1147. }
  1148. if (field_is_string(field))
  1149. field->flags |= FIELD_IS_STRING;
  1150. if (field_is_dynamic(field))
  1151. field->flags |= FIELD_IS_DYNAMIC;
  1152. if (field_is_long(field))
  1153. field->flags |= FIELD_IS_LONG;
  1154. if (test_type_token(type, token, EVENT_OP, ";"))
  1155. goto fail;
  1156. free_token(token);
  1157. if (read_expected(EVENT_ITEM, "offset") < 0)
  1158. goto fail_expect;
  1159. if (read_expected(EVENT_OP, ":") < 0)
  1160. goto fail_expect;
  1161. if (read_expect_type(EVENT_ITEM, &token))
  1162. goto fail;
  1163. field->offset = strtoul(token, NULL, 0);
  1164. free_token(token);
  1165. if (read_expected(EVENT_OP, ";") < 0)
  1166. goto fail_expect;
  1167. if (read_expected(EVENT_ITEM, "size") < 0)
  1168. goto fail_expect;
  1169. if (read_expected(EVENT_OP, ":") < 0)
  1170. goto fail_expect;
  1171. if (read_expect_type(EVENT_ITEM, &token))
  1172. goto fail;
  1173. field->size = strtoul(token, NULL, 0);
  1174. free_token(token);
  1175. if (read_expected(EVENT_OP, ";") < 0)
  1176. goto fail_expect;
  1177. type = read_token(&token);
  1178. if (type != EVENT_NEWLINE) {
  1179. /* newer versions of the kernel have a "signed" type */
  1180. if (test_type_token(type, token, EVENT_ITEM, "signed"))
  1181. goto fail;
  1182. free_token(token);
  1183. if (read_expected(EVENT_OP, ":") < 0)
  1184. goto fail_expect;
  1185. if (read_expect_type(EVENT_ITEM, &token))
  1186. goto fail;
  1187. /* add signed type */
  1188. free_token(token);
  1189. if (read_expected(EVENT_OP, ";") < 0)
  1190. goto fail_expect;
  1191. if (read_expect_type(EVENT_NEWLINE, &token))
  1192. goto fail;
  1193. }
  1194. free_token(token);
  1195. if (field->flags & FIELD_IS_ARRAY) {
  1196. if (field->arraylen)
  1197. field->elementsize = field->size / field->arraylen;
  1198. else if (field->flags & FIELD_IS_STRING)
  1199. field->elementsize = 1;
  1200. else
  1201. field->elementsize = event->pevent->long_size;
  1202. } else
  1203. field->elementsize = field->size;
  1204. *fields = field;
  1205. fields = &field->next;
  1206. } while (1);
  1207. return 0;
  1208. fail:
  1209. free_token(token);
  1210. fail_expect:
  1211. if (field) {
  1212. free(field->type);
  1213. free(field->name);
  1214. free(field);
  1215. }
  1216. return -1;
  1217. }
  1218. static int event_read_format(struct event_format *event)
  1219. {
  1220. char *token;
  1221. int ret;
  1222. if (read_expected_item(EVENT_ITEM, "format") < 0)
  1223. return -1;
  1224. if (read_expected(EVENT_OP, ":") < 0)
  1225. return -1;
  1226. if (read_expect_type(EVENT_NEWLINE, &token))
  1227. goto fail;
  1228. free_token(token);
  1229. ret = event_read_fields(event, &event->format.common_fields);
  1230. if (ret < 0)
  1231. return ret;
  1232. event->format.nr_common = ret;
  1233. ret = event_read_fields(event, &event->format.fields);
  1234. if (ret < 0)
  1235. return ret;
  1236. event->format.nr_fields = ret;
  1237. return 0;
  1238. fail:
  1239. free_token(token);
  1240. return -1;
  1241. }
  1242. static enum event_type
  1243. process_arg_token(struct event_format *event, struct print_arg *arg,
  1244. char **tok, enum event_type type);
  1245. static enum event_type
  1246. process_arg(struct event_format *event, struct print_arg *arg, char **tok)
  1247. {
  1248. enum event_type type;
  1249. char *token;
  1250. type = read_token(&token);
  1251. *tok = token;
  1252. return process_arg_token(event, arg, tok, type);
  1253. }
  1254. static enum event_type
  1255. process_op(struct event_format *event, struct print_arg *arg, char **tok);
  1256. static enum event_type
  1257. process_cond(struct event_format *event, struct print_arg *top, char **tok)
  1258. {
  1259. struct print_arg *arg, *left, *right;
  1260. enum event_type type;
  1261. char *token = NULL;
  1262. arg = alloc_arg();
  1263. left = alloc_arg();
  1264. right = alloc_arg();
  1265. arg->type = PRINT_OP;
  1266. arg->op.left = left;
  1267. arg->op.right = right;
  1268. *tok = NULL;
  1269. type = process_arg(event, left, &token);
  1270. again:
  1271. /* Handle other operations in the arguments */
  1272. if (type == EVENT_OP && strcmp(token, ":") != 0) {
  1273. type = process_op(event, left, &token);
  1274. goto again;
  1275. }
  1276. if (test_type_token(type, token, EVENT_OP, ":"))
  1277. goto out_free;
  1278. arg->op.op = token;
  1279. type = process_arg(event, right, &token);
  1280. top->op.right = arg;
  1281. *tok = token;
  1282. return type;
  1283. out_free:
  1284. /* Top may point to itself */
  1285. top->op.right = NULL;
  1286. free_token(token);
  1287. free_arg(arg);
  1288. return EVENT_ERROR;
  1289. }
  1290. static enum event_type
  1291. process_array(struct event_format *event, struct print_arg *top, char **tok)
  1292. {
  1293. struct print_arg *arg;
  1294. enum event_type type;
  1295. char *token = NULL;
  1296. arg = alloc_arg();
  1297. *tok = NULL;
  1298. type = process_arg(event, arg, &token);
  1299. if (test_type_token(type, token, EVENT_OP, "]"))
  1300. goto out_free;
  1301. top->op.right = arg;
  1302. free_token(token);
  1303. type = read_token_item(&token);
  1304. *tok = token;
  1305. return type;
  1306. out_free:
  1307. free_token(*tok);
  1308. *tok = NULL;
  1309. free_arg(arg);
  1310. return EVENT_ERROR;
  1311. }
  1312. static int get_op_prio(char *op)
  1313. {
  1314. if (!op[1]) {
  1315. switch (op[0]) {
  1316. case '~':
  1317. case '!':
  1318. return 4;
  1319. case '*':
  1320. case '/':
  1321. case '%':
  1322. return 6;
  1323. case '+':
  1324. case '-':
  1325. return 7;
  1326. /* '>>' and '<<' are 8 */
  1327. case '<':
  1328. case '>':
  1329. return 9;
  1330. /* '==' and '!=' are 10 */
  1331. case '&':
  1332. return 11;
  1333. case '^':
  1334. return 12;
  1335. case '|':
  1336. return 13;
  1337. case '?':
  1338. return 16;
  1339. default:
  1340. do_warning("unknown op '%c'", op[0]);
  1341. return -1;
  1342. }
  1343. } else {
  1344. if (strcmp(op, "++") == 0 ||
  1345. strcmp(op, "--") == 0) {
  1346. return 3;
  1347. } else if (strcmp(op, ">>") == 0 ||
  1348. strcmp(op, "<<") == 0) {
  1349. return 8;
  1350. } else if (strcmp(op, ">=") == 0 ||
  1351. strcmp(op, "<=") == 0) {
  1352. return 9;
  1353. } else if (strcmp(op, "==") == 0 ||
  1354. strcmp(op, "!=") == 0) {
  1355. return 10;
  1356. } else if (strcmp(op, "&&") == 0) {
  1357. return 14;
  1358. } else if (strcmp(op, "||") == 0) {
  1359. return 15;
  1360. } else {
  1361. do_warning("unknown op '%s'", op);
  1362. return -1;
  1363. }
  1364. }
  1365. }
  1366. static int set_op_prio(struct print_arg *arg)
  1367. {
  1368. /* single ops are the greatest */
  1369. if (!arg->op.left || arg->op.left->type == PRINT_NULL)
  1370. arg->op.prio = 0;
  1371. else
  1372. arg->op.prio = get_op_prio(arg->op.op);
  1373. return arg->op.prio;
  1374. }
  1375. /* Note, *tok does not get freed, but will most likely be saved */
  1376. static enum event_type
  1377. process_op(struct event_format *event, struct print_arg *arg, char **tok)
  1378. {
  1379. struct print_arg *left, *right = NULL;
  1380. enum event_type type;
  1381. char *token;
  1382. /* the op is passed in via tok */
  1383. token = *tok;
  1384. if (arg->type == PRINT_OP && !arg->op.left) {
  1385. /* handle single op */
  1386. if (token[1]) {
  1387. die("bad op token %s", token);
  1388. goto out_free;
  1389. }
  1390. switch (token[0]) {
  1391. case '~':
  1392. case '!':
  1393. case '+':
  1394. case '-':
  1395. break;
  1396. default:
  1397. do_warning("bad op token %s", token);
  1398. goto out_free;
  1399. }
  1400. /* make an empty left */
  1401. left = alloc_arg();
  1402. left->type = PRINT_NULL;
  1403. arg->op.left = left;
  1404. right = alloc_arg();
  1405. arg->op.right = right;
  1406. /* do not free the token, it belongs to an op */
  1407. *tok = NULL;
  1408. type = process_arg(event, right, tok);
  1409. } else if (strcmp(token, "?") == 0) {
  1410. left = alloc_arg();
  1411. /* copy the top arg to the left */
  1412. *left = *arg;
  1413. arg->type = PRINT_OP;
  1414. arg->op.op = token;
  1415. arg->op.left = left;
  1416. arg->op.prio = 0;
  1417. type = process_cond(event, arg, tok);
  1418. } else if (strcmp(token, ">>") == 0 ||
  1419. 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. left = alloc_arg();
  1434. /* copy the top arg to the left */
  1435. *left = *arg;
  1436. arg->type = PRINT_OP;
  1437. arg->op.op = token;
  1438. arg->op.left = left;
  1439. if (set_op_prio(arg) == -1) {
  1440. event->flags |= EVENT_FL_FAILED;
  1441. /* arg->op.op (= token) will be freed at out_free */
  1442. arg->op.op = NULL;
  1443. goto out_free;
  1444. }
  1445. type = read_token_item(&token);
  1446. *tok = token;
  1447. /* could just be a type pointer */
  1448. if ((strcmp(arg->op.op, "*") == 0) &&
  1449. type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
  1450. char *new_atom;
  1451. if (left->type != PRINT_ATOM)
  1452. die("bad pointer type");
  1453. new_atom = realloc(left->atom.atom,
  1454. strlen(left->atom.atom) + 3);
  1455. if (!new_atom)
  1456. goto out_free;
  1457. left->atom.atom = new_atom;
  1458. strcat(left->atom.atom, " *");
  1459. free(arg->op.op);
  1460. *arg = *left;
  1461. free(left);
  1462. return type;
  1463. }
  1464. right = alloc_arg();
  1465. type = process_arg_token(event, right, tok, type);
  1466. arg->op.right = right;
  1467. } else if (strcmp(token, "[") == 0) {
  1468. left = alloc_arg();
  1469. *left = *arg;
  1470. arg->type = PRINT_OP;
  1471. arg->op.op = token;
  1472. arg->op.left = left;
  1473. arg->op.prio = 0;
  1474. type = process_array(event, arg, tok);
  1475. } else {
  1476. do_warning("unknown op '%s'", token);
  1477. event->flags |= EVENT_FL_FAILED;
  1478. /* the arg is now the left side */
  1479. goto out_free;
  1480. }
  1481. if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
  1482. int prio;
  1483. /* higher prios need to be closer to the root */
  1484. prio = get_op_prio(*tok);
  1485. if (prio > arg->op.prio)
  1486. return process_op(event, arg, tok);
  1487. return process_op(event, right, tok);
  1488. }
  1489. return type;
  1490. out_free:
  1491. free_token(token);
  1492. *tok = NULL;
  1493. return EVENT_ERROR;
  1494. }
  1495. static enum event_type
  1496. process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
  1497. char **tok)
  1498. {
  1499. enum event_type type;
  1500. char *field;
  1501. char *token;
  1502. if (read_expected(EVENT_OP, "->") < 0)
  1503. goto out_err;
  1504. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1505. goto out_free;
  1506. field = token;
  1507. arg->type = PRINT_FIELD;
  1508. arg->field.name = field;
  1509. if (is_flag_field) {
  1510. arg->field.field = pevent_find_any_field(event, arg->field.name);
  1511. arg->field.field->flags |= FIELD_IS_FLAG;
  1512. is_flag_field = 0;
  1513. } else if (is_symbolic_field) {
  1514. arg->field.field = pevent_find_any_field(event, arg->field.name);
  1515. arg->field.field->flags |= FIELD_IS_SYMBOLIC;
  1516. is_symbolic_field = 0;
  1517. }
  1518. type = read_token(&token);
  1519. *tok = token;
  1520. return type;
  1521. out_free:
  1522. free_token(token);
  1523. out_err:
  1524. *tok = NULL;
  1525. return EVENT_ERROR;
  1526. }
  1527. static char *arg_eval (struct print_arg *arg);
  1528. static unsigned long long
  1529. eval_type_str(unsigned long long val, const char *type, int pointer)
  1530. {
  1531. int sign = 0;
  1532. char *ref;
  1533. int len;
  1534. len = strlen(type);
  1535. if (pointer) {
  1536. if (type[len-1] != '*') {
  1537. do_warning("pointer expected with non pointer type");
  1538. return val;
  1539. }
  1540. ref = malloc_or_die(len);
  1541. memcpy(ref, type, len);
  1542. /* chop off the " *" */
  1543. ref[len - 2] = 0;
  1544. val = eval_type_str(val, ref, 0);
  1545. free(ref);
  1546. return val;
  1547. }
  1548. /* check if this is a pointer */
  1549. if (type[len - 1] == '*')
  1550. return val;
  1551. /* Try to figure out the arg size*/
  1552. if (strncmp(type, "struct", 6) == 0)
  1553. /* all bets off */
  1554. return val;
  1555. if (strcmp(type, "u8") == 0)
  1556. return val & 0xff;
  1557. if (strcmp(type, "u16") == 0)
  1558. return val & 0xffff;
  1559. if (strcmp(type, "u32") == 0)
  1560. return val & 0xffffffff;
  1561. if (strcmp(type, "u64") == 0 ||
  1562. strcmp(type, "s64"))
  1563. return val;
  1564. if (strcmp(type, "s8") == 0)
  1565. return (unsigned long long)(char)val & 0xff;
  1566. if (strcmp(type, "s16") == 0)
  1567. return (unsigned long long)(short)val & 0xffff;
  1568. if (strcmp(type, "s32") == 0)
  1569. return (unsigned long long)(int)val & 0xffffffff;
  1570. if (strncmp(type, "unsigned ", 9) == 0) {
  1571. sign = 0;
  1572. type += 9;
  1573. }
  1574. if (strcmp(type, "char") == 0) {
  1575. if (sign)
  1576. return (unsigned long long)(char)val & 0xff;
  1577. else
  1578. return val & 0xff;
  1579. }
  1580. if (strcmp(type, "short") == 0) {
  1581. if (sign)
  1582. return (unsigned long long)(short)val & 0xffff;
  1583. else
  1584. return val & 0xffff;
  1585. }
  1586. if (strcmp(type, "int") == 0) {
  1587. if (sign)
  1588. return (unsigned long long)(int)val & 0xffffffff;
  1589. else
  1590. return val & 0xffffffff;
  1591. }
  1592. return val;
  1593. }
  1594. /*
  1595. * Try to figure out the type.
  1596. */
  1597. static unsigned long long
  1598. eval_type(unsigned long long val, struct print_arg *arg, int pointer)
  1599. {
  1600. if (arg->type != PRINT_TYPE)
  1601. die("expected type argument");
  1602. return eval_type_str(val, arg->typecast.type, pointer);
  1603. }
  1604. static int arg_num_eval(struct print_arg *arg, long long *val)
  1605. {
  1606. long long left, right;
  1607. int ret = 1;
  1608. switch (arg->type) {
  1609. case PRINT_ATOM:
  1610. *val = strtoll(arg->atom.atom, NULL, 0);
  1611. break;
  1612. case PRINT_TYPE:
  1613. ret = arg_num_eval(arg->typecast.item, val);
  1614. if (!ret)
  1615. break;
  1616. *val = eval_type(*val, arg, 0);
  1617. break;
  1618. case PRINT_OP:
  1619. switch (arg->op.op[0]) {
  1620. case '|':
  1621. ret = arg_num_eval(arg->op.left, &left);
  1622. if (!ret)
  1623. break;
  1624. ret = arg_num_eval(arg->op.right, &right);
  1625. if (!ret)
  1626. break;
  1627. if (arg->op.op[1])
  1628. *val = left || right;
  1629. else
  1630. *val = left | right;
  1631. break;
  1632. case '&':
  1633. ret = arg_num_eval(arg->op.left, &left);
  1634. if (!ret)
  1635. break;
  1636. ret = arg_num_eval(arg->op.right, &right);
  1637. if (!ret)
  1638. break;
  1639. if (arg->op.op[1])
  1640. *val = left && right;
  1641. else
  1642. *val = left & right;
  1643. break;
  1644. case '<':
  1645. ret = arg_num_eval(arg->op.left, &left);
  1646. if (!ret)
  1647. break;
  1648. ret = arg_num_eval(arg->op.right, &right);
  1649. if (!ret)
  1650. break;
  1651. switch (arg->op.op[1]) {
  1652. case 0:
  1653. *val = left < right;
  1654. break;
  1655. case '<':
  1656. *val = left << right;
  1657. break;
  1658. case '=':
  1659. *val = left <= right;
  1660. break;
  1661. default:
  1662. do_warning("unknown op '%s'", arg->op.op);
  1663. ret = 0;
  1664. }
  1665. break;
  1666. case '>':
  1667. ret = arg_num_eval(arg->op.left, &left);
  1668. if (!ret)
  1669. break;
  1670. ret = arg_num_eval(arg->op.right, &right);
  1671. if (!ret)
  1672. break;
  1673. switch (arg->op.op[1]) {
  1674. case 0:
  1675. *val = left > right;
  1676. break;
  1677. case '>':
  1678. *val = left >> right;
  1679. break;
  1680. case '=':
  1681. *val = left >= right;
  1682. break;
  1683. default:
  1684. do_warning("unknown op '%s'", arg->op.op);
  1685. ret = 0;
  1686. }
  1687. break;
  1688. case '=':
  1689. ret = arg_num_eval(arg->op.left, &left);
  1690. if (!ret)
  1691. break;
  1692. ret = arg_num_eval(arg->op.right, &right);
  1693. if (!ret)
  1694. break;
  1695. if (arg->op.op[1] != '=') {
  1696. do_warning("unknown op '%s'", arg->op.op);
  1697. ret = 0;
  1698. } else
  1699. *val = left == right;
  1700. break;
  1701. case '!':
  1702. ret = arg_num_eval(arg->op.left, &left);
  1703. if (!ret)
  1704. break;
  1705. ret = arg_num_eval(arg->op.right, &right);
  1706. if (!ret)
  1707. break;
  1708. switch (arg->op.op[1]) {
  1709. case '=':
  1710. *val = left != right;
  1711. break;
  1712. default:
  1713. do_warning("unknown op '%s'", arg->op.op);
  1714. ret = 0;
  1715. }
  1716. break;
  1717. case '-':
  1718. /* check for negative */
  1719. if (arg->op.left->type == PRINT_NULL)
  1720. left = 0;
  1721. else
  1722. ret = arg_num_eval(arg->op.left, &left);
  1723. if (!ret)
  1724. break;
  1725. ret = arg_num_eval(arg->op.right, &right);
  1726. if (!ret)
  1727. break;
  1728. *val = left - right;
  1729. break;
  1730. case '+':
  1731. if (arg->op.left->type == PRINT_NULL)
  1732. left = 0;
  1733. else
  1734. ret = arg_num_eval(arg->op.left, &left);
  1735. if (!ret)
  1736. break;
  1737. ret = arg_num_eval(arg->op.right, &right);
  1738. if (!ret)
  1739. break;
  1740. *val = left + right;
  1741. break;
  1742. default:
  1743. do_warning("unknown op '%s'", arg->op.op);
  1744. ret = 0;
  1745. }
  1746. break;
  1747. case PRINT_NULL:
  1748. case PRINT_FIELD ... PRINT_SYMBOL:
  1749. case PRINT_STRING:
  1750. case PRINT_BSTRING:
  1751. default:
  1752. do_warning("invalid eval type %d", arg->type);
  1753. ret = 0;
  1754. }
  1755. return ret;
  1756. }
  1757. static char *arg_eval (struct print_arg *arg)
  1758. {
  1759. long long val;
  1760. static char buf[20];
  1761. switch (arg->type) {
  1762. case PRINT_ATOM:
  1763. return arg->atom.atom;
  1764. case PRINT_TYPE:
  1765. return arg_eval(arg->typecast.item);
  1766. case PRINT_OP:
  1767. if (!arg_num_eval(arg, &val))
  1768. break;
  1769. sprintf(buf, "%lld", val);
  1770. return buf;
  1771. case PRINT_NULL:
  1772. case PRINT_FIELD ... PRINT_SYMBOL:
  1773. case PRINT_STRING:
  1774. case PRINT_BSTRING:
  1775. default:
  1776. die("invalid eval type %d", arg->type);
  1777. break;
  1778. }
  1779. return NULL;
  1780. }
  1781. static enum event_type
  1782. process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
  1783. {
  1784. enum event_type type;
  1785. struct print_arg *arg = NULL;
  1786. struct print_flag_sym *field;
  1787. char *token = *tok;
  1788. char *value;
  1789. do {
  1790. free_token(token);
  1791. type = read_token_item(&token);
  1792. if (test_type_token(type, token, EVENT_OP, "{"))
  1793. break;
  1794. arg = alloc_arg();
  1795. free_token(token);
  1796. type = process_arg(event, arg, &token);
  1797. if (type == EVENT_OP)
  1798. type = process_op(event, arg, &token);
  1799. if (type == EVENT_ERROR)
  1800. goto out_free;
  1801. if (test_type_token(type, token, EVENT_DELIM, ","))
  1802. goto out_free;
  1803. field = malloc_or_die(sizeof(*field));
  1804. memset(field, 0, sizeof(*field));
  1805. value = arg_eval(arg);
  1806. if (value == NULL)
  1807. goto out_free;
  1808. field->value = strdup(value);
  1809. if (field->value == NULL)
  1810. goto out_free;
  1811. free_arg(arg);
  1812. arg = alloc_arg();
  1813. free_token(token);
  1814. type = process_arg(event, arg, &token);
  1815. if (test_type_token(type, token, EVENT_OP, "}"))
  1816. goto out_free;
  1817. value = arg_eval(arg);
  1818. if (value == NULL)
  1819. goto out_free;
  1820. field->str = strdup(value);
  1821. if (field->str == NULL)
  1822. goto out_free;
  1823. free_arg(arg);
  1824. arg = NULL;
  1825. *list = field;
  1826. list = &field->next;
  1827. free_token(token);
  1828. type = read_token_item(&token);
  1829. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  1830. *tok = token;
  1831. return type;
  1832. out_free:
  1833. free_arg(arg);
  1834. free_token(token);
  1835. *tok = NULL;
  1836. return EVENT_ERROR;
  1837. }
  1838. static enum event_type
  1839. process_flags(struct event_format *event, struct print_arg *arg, char **tok)
  1840. {
  1841. struct print_arg *field;
  1842. enum event_type type;
  1843. char *token;
  1844. memset(arg, 0, sizeof(*arg));
  1845. arg->type = PRINT_FLAGS;
  1846. field = alloc_arg();
  1847. type = process_arg(event, field, &token);
  1848. /* Handle operations in the first argument */
  1849. while (type == EVENT_OP)
  1850. type = process_op(event, field, &token);
  1851. if (test_type_token(type, token, EVENT_DELIM, ","))
  1852. goto out_free;
  1853. free_token(token);
  1854. arg->flags.field = field;
  1855. type = read_token_item(&token);
  1856. if (event_item_type(type)) {
  1857. arg->flags.delim = token;
  1858. type = read_token_item(&token);
  1859. }
  1860. if (test_type_token(type, token, EVENT_DELIM, ","))
  1861. goto out_free;
  1862. type = process_fields(event, &arg->flags.flags, &token);
  1863. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1864. goto out_free;
  1865. free_token(token);
  1866. type = read_token_item(tok);
  1867. return type;
  1868. out_free:
  1869. free_token(token);
  1870. *tok = NULL;
  1871. return EVENT_ERROR;
  1872. }
  1873. static enum event_type
  1874. process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
  1875. {
  1876. struct print_arg *field;
  1877. enum event_type type;
  1878. char *token;
  1879. memset(arg, 0, sizeof(*arg));
  1880. arg->type = PRINT_SYMBOL;
  1881. field = alloc_arg();
  1882. type = process_arg(event, field, &token);
  1883. if (test_type_token(type, token, EVENT_DELIM, ","))
  1884. goto out_free;
  1885. arg->symbol.field = field;
  1886. type = process_fields(event, &arg->symbol.symbols, &token);
  1887. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1888. goto out_free;
  1889. free_token(token);
  1890. type = read_token_item(tok);
  1891. return type;
  1892. out_free:
  1893. free_token(token);
  1894. *tok = NULL;
  1895. return EVENT_ERROR;
  1896. }
  1897. static enum event_type
  1898. process_hex(struct event_format *event, struct print_arg *arg, char **tok)
  1899. {
  1900. struct print_arg *field;
  1901. enum event_type type;
  1902. char *token;
  1903. memset(arg, 0, sizeof(*arg));
  1904. arg->type = PRINT_HEX;
  1905. field = alloc_arg();
  1906. type = process_arg(event, field, &token);
  1907. if (test_type_token(type, token, EVENT_DELIM, ","))
  1908. goto out_free;
  1909. arg->hex.field = field;
  1910. free_token(token);
  1911. field = alloc_arg();
  1912. type = process_arg(event, field, &token);
  1913. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1914. goto out_free;
  1915. arg->hex.size = field;
  1916. free_token(token);
  1917. type = read_token_item(tok);
  1918. return type;
  1919. out_free:
  1920. free_arg(field);
  1921. free_token(token);
  1922. *tok = NULL;
  1923. return EVENT_ERROR;
  1924. }
  1925. static enum event_type
  1926. process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
  1927. {
  1928. struct format_field *field;
  1929. enum event_type type;
  1930. char *token;
  1931. memset(arg, 0, sizeof(*arg));
  1932. arg->type = PRINT_DYNAMIC_ARRAY;
  1933. /*
  1934. * The item within the parenthesis is another field that holds
  1935. * the index into where the array starts.
  1936. */
  1937. type = read_token(&token);
  1938. *tok = token;
  1939. if (type != EVENT_ITEM)
  1940. goto out_free;
  1941. /* Find the field */
  1942. field = pevent_find_field(event, token);
  1943. if (!field)
  1944. goto out_free;
  1945. arg->dynarray.field = field;
  1946. arg->dynarray.index = 0;
  1947. if (read_expected(EVENT_DELIM, ")") < 0)
  1948. goto out_free;
  1949. free_token(token);
  1950. type = read_token_item(&token);
  1951. *tok = token;
  1952. if (type != EVENT_OP || strcmp(token, "[") != 0)
  1953. return type;
  1954. free_token(token);
  1955. arg = alloc_arg();
  1956. type = process_arg(event, arg, &token);
  1957. if (type == EVENT_ERROR)
  1958. goto out_free_arg;
  1959. if (!test_type_token(type, token, EVENT_OP, "]"))
  1960. goto out_free_arg;
  1961. free_token(token);
  1962. type = read_token_item(tok);
  1963. return type;
  1964. out_free_arg:
  1965. free_arg(arg);
  1966. out_free:
  1967. free_token(token);
  1968. *tok = NULL;
  1969. return EVENT_ERROR;
  1970. }
  1971. static enum event_type
  1972. process_paren(struct event_format *event, struct print_arg *arg, char **tok)
  1973. {
  1974. struct print_arg *item_arg;
  1975. enum event_type type;
  1976. char *token;
  1977. type = process_arg(event, arg, &token);
  1978. if (type == EVENT_ERROR)
  1979. goto out_free;
  1980. if (type == EVENT_OP)
  1981. type = process_op(event, arg, &token);
  1982. if (type == EVENT_ERROR)
  1983. goto out_free;
  1984. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1985. goto out_free;
  1986. free_token(token);
  1987. type = read_token_item(&token);
  1988. /*
  1989. * If the next token is an item or another open paren, then
  1990. * this was a typecast.
  1991. */
  1992. if (event_item_type(type) ||
  1993. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  1994. /* make this a typecast and contine */
  1995. /* prevous must be an atom */
  1996. if (arg->type != PRINT_ATOM)
  1997. die("previous needed to be PRINT_ATOM");
  1998. item_arg = alloc_arg();
  1999. arg->type = PRINT_TYPE;
  2000. arg->typecast.type = arg->atom.atom;
  2001. arg->typecast.item = item_arg;
  2002. type = process_arg_token(event, item_arg, &token, type);
  2003. }
  2004. *tok = token;
  2005. return type;
  2006. out_free:
  2007. free_token(token);
  2008. *tok = NULL;
  2009. return EVENT_ERROR;
  2010. }
  2011. static enum event_type
  2012. process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
  2013. 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 __maybe_unused,
  3076. struct event_format *event)
  3077. {
  3078. struct pevent *pevent = event->pevent;
  3079. unsigned long long addr;
  3080. struct format_field *field;
  3081. struct printk_map *printk;
  3082. char *format;
  3083. char *p;
  3084. field = pevent->bprint_fmt_field;
  3085. if (!field) {
  3086. field = pevent_find_field(event, "fmt");
  3087. if (!field)
  3088. die("can't find format field for binary printk");
  3089. pevent->bprint_fmt_field = field;
  3090. }
  3091. addr = pevent_read_number(pevent, data + field->offset, field->size);
  3092. printk = find_printk(pevent, addr);
  3093. if (!printk) {
  3094. format = malloc_or_die(45);
  3095. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  3096. addr);
  3097. return format;
  3098. }
  3099. p = printk->printk;
  3100. /* Remove any quotes. */
  3101. if (*p == '"')
  3102. p++;
  3103. format = malloc_or_die(strlen(p) + 10);
  3104. sprintf(format, "%s : %s", "%pf", p);
  3105. /* remove ending quotes and new line since we will add one too */
  3106. p = format + strlen(format) - 1;
  3107. if (*p == '"')
  3108. *p = 0;
  3109. p -= 2;
  3110. if (strcmp(p, "\\n") == 0)
  3111. *p = 0;
  3112. return format;
  3113. }
  3114. static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
  3115. struct event_format *event, struct print_arg *arg)
  3116. {
  3117. unsigned char *buf;
  3118. char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
  3119. if (arg->type == PRINT_FUNC) {
  3120. process_defined_func(s, data, size, event, arg);
  3121. return;
  3122. }
  3123. if (arg->type != PRINT_FIELD) {
  3124. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
  3125. arg->type);
  3126. return;
  3127. }
  3128. if (mac == 'm')
  3129. fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
  3130. if (!arg->field.field) {
  3131. arg->field.field =
  3132. pevent_find_any_field(event, arg->field.name);
  3133. if (!arg->field.field)
  3134. die("field %s not found", arg->field.name);
  3135. }
  3136. if (arg->field.field->size != 6) {
  3137. trace_seq_printf(s, "INVALIDMAC");
  3138. return;
  3139. }
  3140. buf = data + arg->field.field->offset;
  3141. trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  3142. }
  3143. static int is_printable_array(char *p, unsigned int len)
  3144. {
  3145. unsigned int i;
  3146. for (i = 0; i < len && p[i]; i++)
  3147. if (!isprint(p[i]))
  3148. return 0;
  3149. return 1;
  3150. }
  3151. static void print_event_fields(struct trace_seq *s, void *data, int size,
  3152. struct event_format *event)
  3153. {
  3154. struct format_field *field;
  3155. unsigned long long val;
  3156. unsigned int offset, len, i;
  3157. field = event->format.fields;
  3158. while (field) {
  3159. trace_seq_printf(s, " %s=", field->name);
  3160. if (field->flags & FIELD_IS_ARRAY) {
  3161. offset = field->offset;
  3162. len = field->size;
  3163. if (field->flags & FIELD_IS_DYNAMIC) {
  3164. val = pevent_read_number(event->pevent, data + offset, len);
  3165. offset = val;
  3166. len = offset >> 16;
  3167. offset &= 0xffff;
  3168. }
  3169. if (field->flags & FIELD_IS_STRING &&
  3170. is_printable_array(data + offset, len)) {
  3171. trace_seq_printf(s, "%s", (char *)data + offset);
  3172. } else {
  3173. trace_seq_puts(s, "ARRAY[");
  3174. for (i = 0; i < len; i++) {
  3175. if (i)
  3176. trace_seq_puts(s, ", ");
  3177. trace_seq_printf(s, "%02x",
  3178. *((unsigned char *)data + offset + i));
  3179. }
  3180. trace_seq_putc(s, ']');
  3181. field->flags &= ~FIELD_IS_STRING;
  3182. }
  3183. } else {
  3184. val = pevent_read_number(event->pevent, data + field->offset,
  3185. field->size);
  3186. if (field->flags & FIELD_IS_POINTER) {
  3187. trace_seq_printf(s, "0x%llx", val);
  3188. } else if (field->flags & FIELD_IS_SIGNED) {
  3189. switch (field->size) {
  3190. case 4:
  3191. /*
  3192. * If field is long then print it in hex.
  3193. * A long usually stores pointers.
  3194. */
  3195. if (field->flags & FIELD_IS_LONG)
  3196. trace_seq_printf(s, "0x%x", (int)val);
  3197. else
  3198. trace_seq_printf(s, "%d", (int)val);
  3199. break;
  3200. case 2:
  3201. trace_seq_printf(s, "%2d", (short)val);
  3202. break;
  3203. case 1:
  3204. trace_seq_printf(s, "%1d", (char)val);
  3205. break;
  3206. default:
  3207. trace_seq_printf(s, "%lld", val);
  3208. }
  3209. } else {
  3210. if (field->flags & FIELD_IS_LONG)
  3211. trace_seq_printf(s, "0x%llx", val);
  3212. else
  3213. trace_seq_printf(s, "%llu", val);
  3214. }
  3215. }
  3216. field = field->next;
  3217. }
  3218. }
  3219. static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
  3220. {
  3221. struct pevent *pevent = event->pevent;
  3222. struct print_fmt *print_fmt = &event->print_fmt;
  3223. struct print_arg *arg = print_fmt->args;
  3224. struct print_arg *args = NULL;
  3225. const char *ptr = print_fmt->format;
  3226. unsigned long long val;
  3227. struct func_map *func;
  3228. const char *saveptr;
  3229. char *bprint_fmt = NULL;
  3230. char format[32];
  3231. int show_func;
  3232. int len_as_arg;
  3233. int len_arg;
  3234. int len;
  3235. int ls;
  3236. if (event->flags & EVENT_FL_FAILED) {
  3237. trace_seq_printf(s, "[FAILED TO PARSE]");
  3238. print_event_fields(s, data, size, event);
  3239. return;
  3240. }
  3241. if (event->flags & EVENT_FL_ISBPRINT) {
  3242. bprint_fmt = get_bprint_format(data, size, event);
  3243. args = make_bprint_args(bprint_fmt, data, size, event);
  3244. arg = args;
  3245. ptr = bprint_fmt;
  3246. }
  3247. for (; *ptr; ptr++) {
  3248. ls = 0;
  3249. if (*ptr == '\\') {
  3250. ptr++;
  3251. switch (*ptr) {
  3252. case 'n':
  3253. trace_seq_putc(s, '\n');
  3254. break;
  3255. case 't':
  3256. trace_seq_putc(s, '\t');
  3257. break;
  3258. case 'r':
  3259. trace_seq_putc(s, '\r');
  3260. break;
  3261. case '\\':
  3262. trace_seq_putc(s, '\\');
  3263. break;
  3264. default:
  3265. trace_seq_putc(s, *ptr);
  3266. break;
  3267. }
  3268. } else if (*ptr == '%') {
  3269. saveptr = ptr;
  3270. show_func = 0;
  3271. len_as_arg = 0;
  3272. cont_process:
  3273. ptr++;
  3274. switch (*ptr) {
  3275. case '%':
  3276. trace_seq_putc(s, '%');
  3277. break;
  3278. case '#':
  3279. /* FIXME: need to handle properly */
  3280. goto cont_process;
  3281. case 'h':
  3282. ls--;
  3283. goto cont_process;
  3284. case 'l':
  3285. ls++;
  3286. goto cont_process;
  3287. case 'L':
  3288. ls = 2;
  3289. goto cont_process;
  3290. case '*':
  3291. /* The argument is the length. */
  3292. if (!arg) {
  3293. do_warning("no argument match");
  3294. event->flags |= EVENT_FL_FAILED;
  3295. goto out_failed;
  3296. }
  3297. len_arg = eval_num_arg(data, size, event, arg);
  3298. len_as_arg = 1;
  3299. arg = arg->next;
  3300. goto cont_process;
  3301. case '.':
  3302. case 'z':
  3303. case 'Z':
  3304. case '0' ... '9':
  3305. goto cont_process;
  3306. case 'p':
  3307. if (pevent->long_size == 4)
  3308. ls = 1;
  3309. else
  3310. ls = 2;
  3311. if (*(ptr+1) == 'F' ||
  3312. *(ptr+1) == 'f') {
  3313. ptr++;
  3314. show_func = *ptr;
  3315. } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
  3316. print_mac_arg(s, *(ptr+1), data, size, event, arg);
  3317. ptr++;
  3318. arg = arg->next;
  3319. break;
  3320. }
  3321. /* fall through */
  3322. case 'd':
  3323. case 'i':
  3324. case 'x':
  3325. case 'X':
  3326. case 'u':
  3327. if (!arg) {
  3328. do_warning("no argument match");
  3329. event->flags |= EVENT_FL_FAILED;
  3330. goto out_failed;
  3331. }
  3332. len = ((unsigned long)ptr + 1) -
  3333. (unsigned long)saveptr;
  3334. /* should never happen */
  3335. if (len > 31) {
  3336. do_warning("bad format!");
  3337. event->flags |= EVENT_FL_FAILED;
  3338. len = 31;
  3339. }
  3340. memcpy(format, saveptr, len);
  3341. format[len] = 0;
  3342. val = eval_num_arg(data, size, event, arg);
  3343. arg = arg->next;
  3344. if (show_func) {
  3345. func = find_func(pevent, val);
  3346. if (func) {
  3347. trace_seq_puts(s, func->func);
  3348. if (show_func == 'F')
  3349. trace_seq_printf(s,
  3350. "+0x%llx",
  3351. val - func->addr);
  3352. break;
  3353. }
  3354. }
  3355. if (pevent->long_size == 8 && ls &&
  3356. sizeof(long) != 8) {
  3357. char *p;
  3358. ls = 2;
  3359. /* make %l into %ll */
  3360. p = strchr(format, 'l');
  3361. if (p)
  3362. memmove(p+1, p, strlen(p)+1);
  3363. else if (strcmp(format, "%p") == 0)
  3364. strcpy(format, "0x%llx");
  3365. }
  3366. switch (ls) {
  3367. case -2:
  3368. if (len_as_arg)
  3369. trace_seq_printf(s, format, len_arg, (char)val);
  3370. else
  3371. trace_seq_printf(s, format, (char)val);
  3372. break;
  3373. case -1:
  3374. if (len_as_arg)
  3375. trace_seq_printf(s, format, len_arg, (short)val);
  3376. else
  3377. trace_seq_printf(s, format, (short)val);
  3378. break;
  3379. case 0:
  3380. if (len_as_arg)
  3381. trace_seq_printf(s, format, len_arg, (int)val);
  3382. else
  3383. trace_seq_printf(s, format, (int)val);
  3384. break;
  3385. case 1:
  3386. if (len_as_arg)
  3387. trace_seq_printf(s, format, len_arg, (long)val);
  3388. else
  3389. trace_seq_printf(s, format, (long)val);
  3390. break;
  3391. case 2:
  3392. if (len_as_arg)
  3393. trace_seq_printf(s, format, len_arg,
  3394. (long long)val);
  3395. else
  3396. trace_seq_printf(s, format, (long long)val);
  3397. break;
  3398. default:
  3399. do_warning("bad count (%d)", ls);
  3400. event->flags |= EVENT_FL_FAILED;
  3401. }
  3402. break;
  3403. case 's':
  3404. if (!arg) {
  3405. do_warning("no matching argument");
  3406. event->flags |= EVENT_FL_FAILED;
  3407. goto out_failed;
  3408. }
  3409. len = ((unsigned long)ptr + 1) -
  3410. (unsigned long)saveptr;
  3411. /* should never happen */
  3412. if (len > 31) {
  3413. do_warning("bad format!");
  3414. event->flags |= EVENT_FL_FAILED;
  3415. len = 31;
  3416. }
  3417. memcpy(format, saveptr, len);
  3418. format[len] = 0;
  3419. if (!len_as_arg)
  3420. len_arg = -1;
  3421. print_str_arg(s, data, size, event,
  3422. format, len_arg, arg);
  3423. arg = arg->next;
  3424. break;
  3425. default:
  3426. trace_seq_printf(s, ">%c<", *ptr);
  3427. }
  3428. } else
  3429. trace_seq_putc(s, *ptr);
  3430. }
  3431. if (event->flags & EVENT_FL_FAILED) {
  3432. out_failed:
  3433. trace_seq_printf(s, "[FAILED TO PARSE]");
  3434. }
  3435. if (args) {
  3436. free_args(args);
  3437. free(bprint_fmt);
  3438. }
  3439. }
  3440. /**
  3441. * pevent_data_lat_fmt - parse the data for the latency format
  3442. * @pevent: a handle to the pevent
  3443. * @s: the trace_seq to write to
  3444. * @record: the record to read from
  3445. *
  3446. * This parses out the Latency format (interrupts disabled,
  3447. * need rescheduling, in hard/soft interrupt, preempt count
  3448. * and lock depth) and places it into the trace_seq.
  3449. */
  3450. void pevent_data_lat_fmt(struct pevent *pevent,
  3451. struct trace_seq *s, struct pevent_record *record)
  3452. {
  3453. static int check_lock_depth = 1;
  3454. static int check_migrate_disable = 1;
  3455. static int lock_depth_exists;
  3456. static int migrate_disable_exists;
  3457. unsigned int lat_flags;
  3458. unsigned int pc;
  3459. int lock_depth;
  3460. int migrate_disable;
  3461. int hardirq;
  3462. int softirq;
  3463. void *data = record->data;
  3464. lat_flags = parse_common_flags(pevent, data);
  3465. pc = parse_common_pc(pevent, data);
  3466. /* lock_depth may not always exist */
  3467. if (lock_depth_exists)
  3468. lock_depth = parse_common_lock_depth(pevent, data);
  3469. else if (check_lock_depth) {
  3470. lock_depth = parse_common_lock_depth(pevent, data);
  3471. if (lock_depth < 0)
  3472. check_lock_depth = 0;
  3473. else
  3474. lock_depth_exists = 1;
  3475. }
  3476. /* migrate_disable may not always exist */
  3477. if (migrate_disable_exists)
  3478. migrate_disable = parse_common_migrate_disable(pevent, data);
  3479. else if (check_migrate_disable) {
  3480. migrate_disable = parse_common_migrate_disable(pevent, data);
  3481. if (migrate_disable < 0)
  3482. check_migrate_disable = 0;
  3483. else
  3484. migrate_disable_exists = 1;
  3485. }
  3486. hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
  3487. softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
  3488. trace_seq_printf(s, "%c%c%c",
  3489. (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  3490. (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  3491. 'X' : '.',
  3492. (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
  3493. 'N' : '.',
  3494. (hardirq && softirq) ? 'H' :
  3495. hardirq ? 'h' : softirq ? 's' : '.');
  3496. if (pc)
  3497. trace_seq_printf(s, "%x", pc);
  3498. else
  3499. trace_seq_putc(s, '.');
  3500. if (migrate_disable_exists) {
  3501. if (migrate_disable < 0)
  3502. trace_seq_putc(s, '.');
  3503. else
  3504. trace_seq_printf(s, "%d", migrate_disable);
  3505. }
  3506. if (lock_depth_exists) {
  3507. if (lock_depth < 0)
  3508. trace_seq_putc(s, '.');
  3509. else
  3510. trace_seq_printf(s, "%d", lock_depth);
  3511. }
  3512. trace_seq_terminate(s);
  3513. }
  3514. /**
  3515. * pevent_data_type - parse out the given event type
  3516. * @pevent: a handle to the pevent
  3517. * @rec: the record to read from
  3518. *
  3519. * This returns the event id from the @rec.
  3520. */
  3521. int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
  3522. {
  3523. return trace_parse_common_type(pevent, rec->data);
  3524. }
  3525. /**
  3526. * pevent_data_event_from_type - find the event by a given type
  3527. * @pevent: a handle to the pevent
  3528. * @type: the type of the event.
  3529. *
  3530. * This returns the event form a given @type;
  3531. */
  3532. struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
  3533. {
  3534. return pevent_find_event(pevent, type);
  3535. }
  3536. /**
  3537. * pevent_data_pid - parse the PID from raw data
  3538. * @pevent: a handle to the pevent
  3539. * @rec: the record to parse
  3540. *
  3541. * This returns the PID from a raw data.
  3542. */
  3543. int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
  3544. {
  3545. return parse_common_pid(pevent, rec->data);
  3546. }
  3547. /**
  3548. * pevent_data_comm_from_pid - return the command line from PID
  3549. * @pevent: a handle to the pevent
  3550. * @pid: the PID of the task to search for
  3551. *
  3552. * This returns a pointer to the command line that has the given
  3553. * @pid.
  3554. */
  3555. const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
  3556. {
  3557. const char *comm;
  3558. comm = find_cmdline(pevent, pid);
  3559. return comm;
  3560. }
  3561. /**
  3562. * pevent_data_comm_from_pid - parse the data into the print format
  3563. * @s: the trace_seq to write to
  3564. * @event: the handle to the event
  3565. * @record: the record to read from
  3566. *
  3567. * This parses the raw @data using the given @event information and
  3568. * writes the print format into the trace_seq.
  3569. */
  3570. void pevent_event_info(struct trace_seq *s, struct event_format *event,
  3571. struct pevent_record *record)
  3572. {
  3573. int print_pretty = 1;
  3574. if (event->pevent->print_raw)
  3575. print_event_fields(s, record->data, record->size, event);
  3576. else {
  3577. if (event->handler)
  3578. print_pretty = event->handler(s, record, event,
  3579. event->context);
  3580. if (print_pretty)
  3581. pretty_print(s, record->data, record->size, event);
  3582. }
  3583. trace_seq_terminate(s);
  3584. }
  3585. void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
  3586. struct pevent_record *record)
  3587. {
  3588. static char *spaces = " "; /* 20 spaces */
  3589. struct event_format *event;
  3590. unsigned long secs;
  3591. unsigned long usecs;
  3592. unsigned long nsecs;
  3593. const char *comm;
  3594. void *data = record->data;
  3595. int type;
  3596. int pid;
  3597. int len;
  3598. int p;
  3599. secs = record->ts / NSECS_PER_SEC;
  3600. nsecs = record->ts - secs * NSECS_PER_SEC;
  3601. if (record->size < 0) {
  3602. do_warning("ug! negative record size %d", record->size);
  3603. return;
  3604. }
  3605. type = trace_parse_common_type(pevent, data);
  3606. event = pevent_find_event(pevent, type);
  3607. if (!event) {
  3608. do_warning("ug! no event found for type %d", type);
  3609. return;
  3610. }
  3611. pid = parse_common_pid(pevent, data);
  3612. comm = find_cmdline(pevent, pid);
  3613. if (pevent->latency_format) {
  3614. trace_seq_printf(s, "%8.8s-%-5d %3d",
  3615. comm, pid, record->cpu);
  3616. pevent_data_lat_fmt(pevent, s, record);
  3617. } else
  3618. trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
  3619. if (pevent->flags & PEVENT_NSEC_OUTPUT) {
  3620. usecs = nsecs;
  3621. p = 9;
  3622. } else {
  3623. usecs = (nsecs + 500) / NSECS_PER_USEC;
  3624. p = 6;
  3625. }
  3626. trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
  3627. /* Space out the event names evenly. */
  3628. len = strlen(event->name);
  3629. if (len < 20)
  3630. trace_seq_printf(s, "%.*s", 20 - len, spaces);
  3631. pevent_event_info(s, event, record);
  3632. }
  3633. static int events_id_cmp(const void *a, const void *b)
  3634. {
  3635. struct event_format * const * ea = a;
  3636. struct event_format * const * eb = b;
  3637. if ((*ea)->id < (*eb)->id)
  3638. return -1;
  3639. if ((*ea)->id > (*eb)->id)
  3640. return 1;
  3641. return 0;
  3642. }
  3643. static int events_name_cmp(const void *a, const void *b)
  3644. {
  3645. struct event_format * const * ea = a;
  3646. struct event_format * const * eb = b;
  3647. int res;
  3648. res = strcmp((*ea)->name, (*eb)->name);
  3649. if (res)
  3650. return res;
  3651. res = strcmp((*ea)->system, (*eb)->system);
  3652. if (res)
  3653. return res;
  3654. return events_id_cmp(a, b);
  3655. }
  3656. static int events_system_cmp(const void *a, const void *b)
  3657. {
  3658. struct event_format * const * ea = a;
  3659. struct event_format * const * eb = b;
  3660. int res;
  3661. res = strcmp((*ea)->system, (*eb)->system);
  3662. if (res)
  3663. return res;
  3664. res = strcmp((*ea)->name, (*eb)->name);
  3665. if (res)
  3666. return res;
  3667. return events_id_cmp(a, b);
  3668. }
  3669. struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
  3670. {
  3671. struct event_format **events;
  3672. int (*sort)(const void *a, const void *b);
  3673. events = pevent->sort_events;
  3674. if (events && pevent->last_type == sort_type)
  3675. return events;
  3676. if (!events) {
  3677. events = malloc(sizeof(*events) * (pevent->nr_events + 1));
  3678. if (!events)
  3679. return NULL;
  3680. memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
  3681. events[pevent->nr_events] = NULL;
  3682. pevent->sort_events = events;
  3683. /* the internal events are sorted by id */
  3684. if (sort_type == EVENT_SORT_ID) {
  3685. pevent->last_type = sort_type;
  3686. return events;
  3687. }
  3688. }
  3689. switch (sort_type) {
  3690. case EVENT_SORT_ID:
  3691. sort = events_id_cmp;
  3692. break;
  3693. case EVENT_SORT_NAME:
  3694. sort = events_name_cmp;
  3695. break;
  3696. case EVENT_SORT_SYSTEM:
  3697. sort = events_system_cmp;
  3698. break;
  3699. default:
  3700. return events;
  3701. }
  3702. qsort(events, pevent->nr_events, sizeof(*events), sort);
  3703. pevent->last_type = sort_type;
  3704. return events;
  3705. }
  3706. static struct format_field **
  3707. get_event_fields(const char *type, const char *name,
  3708. int count, struct format_field *list)
  3709. {
  3710. struct format_field **fields;
  3711. struct format_field *field;
  3712. int i = 0;
  3713. fields = malloc_or_die(sizeof(*fields) * (count + 1));
  3714. for (field = list; field; field = field->next) {
  3715. fields[i++] = field;
  3716. if (i == count + 1) {
  3717. do_warning("event %s has more %s fields than specified",
  3718. name, type);
  3719. i--;
  3720. break;
  3721. }
  3722. }
  3723. if (i != count)
  3724. do_warning("event %s has less %s fields than specified",
  3725. name, type);
  3726. fields[i] = NULL;
  3727. return fields;
  3728. }
  3729. /**
  3730. * pevent_event_common_fields - return a list of common fields for an event
  3731. * @event: the event to return the common fields of.
  3732. *
  3733. * Returns an allocated array of fields. The last item in the array is NULL.
  3734. * The array must be freed with free().
  3735. */
  3736. struct format_field **pevent_event_common_fields(struct event_format *event)
  3737. {
  3738. return get_event_fields("common", event->name,
  3739. event->format.nr_common,
  3740. event->format.common_fields);
  3741. }
  3742. /**
  3743. * pevent_event_fields - return a list of event specific fields for an event
  3744. * @event: the event to return the fields of.
  3745. *
  3746. * Returns an allocated array of fields. The last item in the array is NULL.
  3747. * The array must be freed with free().
  3748. */
  3749. struct format_field **pevent_event_fields(struct event_format *event)
  3750. {
  3751. return get_event_fields("event", event->name,
  3752. event->format.nr_fields,
  3753. event->format.fields);
  3754. }
  3755. static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
  3756. {
  3757. trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
  3758. if (field->next) {
  3759. trace_seq_puts(s, ", ");
  3760. print_fields(s, field->next);
  3761. }
  3762. }
  3763. /* for debugging */
  3764. static void print_args(struct print_arg *args)
  3765. {
  3766. int print_paren = 1;
  3767. struct trace_seq s;
  3768. switch (args->type) {
  3769. case PRINT_NULL:
  3770. printf("null");
  3771. break;
  3772. case PRINT_ATOM:
  3773. printf("%s", args->atom.atom);
  3774. break;
  3775. case PRINT_FIELD:
  3776. printf("REC->%s", args->field.name);
  3777. break;
  3778. case PRINT_FLAGS:
  3779. printf("__print_flags(");
  3780. print_args(args->flags.field);
  3781. printf(", %s, ", args->flags.delim);
  3782. trace_seq_init(&s);
  3783. print_fields(&s, args->flags.flags);
  3784. trace_seq_do_printf(&s);
  3785. trace_seq_destroy(&s);
  3786. printf(")");
  3787. break;
  3788. case PRINT_SYMBOL:
  3789. printf("__print_symbolic(");
  3790. print_args(args->symbol.field);
  3791. printf(", ");
  3792. trace_seq_init(&s);
  3793. print_fields(&s, args->symbol.symbols);
  3794. trace_seq_do_printf(&s);
  3795. trace_seq_destroy(&s);
  3796. printf(")");
  3797. break;
  3798. case PRINT_HEX:
  3799. printf("__print_hex(");
  3800. print_args(args->hex.field);
  3801. printf(", ");
  3802. print_args(args->hex.size);
  3803. printf(")");
  3804. break;
  3805. case PRINT_STRING:
  3806. case PRINT_BSTRING:
  3807. printf("__get_str(%s)", args->string.string);
  3808. break;
  3809. case PRINT_TYPE:
  3810. printf("(%s)", args->typecast.type);
  3811. print_args(args->typecast.item);
  3812. break;
  3813. case PRINT_OP:
  3814. if (strcmp(args->op.op, ":") == 0)
  3815. print_paren = 0;
  3816. if (print_paren)
  3817. printf("(");
  3818. print_args(args->op.left);
  3819. printf(" %s ", args->op.op);
  3820. print_args(args->op.right);
  3821. if (print_paren)
  3822. printf(")");
  3823. break;
  3824. default:
  3825. /* we should warn... */
  3826. return;
  3827. }
  3828. if (args->next) {
  3829. printf("\n");
  3830. print_args(args->next);
  3831. }
  3832. }
  3833. static void parse_header_field(const char *field,
  3834. int *offset, int *size, int mandatory)
  3835. {
  3836. unsigned long long save_input_buf_ptr;
  3837. unsigned long long save_input_buf_siz;
  3838. char *token;
  3839. int type;
  3840. save_input_buf_ptr = input_buf_ptr;
  3841. save_input_buf_siz = input_buf_siz;
  3842. if (read_expected(EVENT_ITEM, "field") < 0)
  3843. return;
  3844. if (read_expected(EVENT_OP, ":") < 0)
  3845. return;
  3846. /* type */
  3847. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3848. goto fail;
  3849. free_token(token);
  3850. /*
  3851. * If this is not a mandatory field, then test it first.
  3852. */
  3853. if (mandatory) {
  3854. if (read_expected(EVENT_ITEM, field) < 0)
  3855. return;
  3856. } else {
  3857. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3858. goto fail;
  3859. if (strcmp(token, field) != 0)
  3860. goto discard;
  3861. free_token(token);
  3862. }
  3863. if (read_expected(EVENT_OP, ";") < 0)
  3864. return;
  3865. if (read_expected(EVENT_ITEM, "offset") < 0)
  3866. return;
  3867. if (read_expected(EVENT_OP, ":") < 0)
  3868. return;
  3869. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3870. goto fail;
  3871. *offset = atoi(token);
  3872. free_token(token);
  3873. if (read_expected(EVENT_OP, ";") < 0)
  3874. return;
  3875. if (read_expected(EVENT_ITEM, "size") < 0)
  3876. return;
  3877. if (read_expected(EVENT_OP, ":") < 0)
  3878. return;
  3879. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3880. goto fail;
  3881. *size = atoi(token);
  3882. free_token(token);
  3883. if (read_expected(EVENT_OP, ";") < 0)
  3884. return;
  3885. type = read_token(&token);
  3886. if (type != EVENT_NEWLINE) {
  3887. /* newer versions of the kernel have a "signed" type */
  3888. if (type != EVENT_ITEM)
  3889. goto fail;
  3890. if (strcmp(token, "signed") != 0)
  3891. goto fail;
  3892. free_token(token);
  3893. if (read_expected(EVENT_OP, ":") < 0)
  3894. return;
  3895. if (read_expect_type(EVENT_ITEM, &token))
  3896. goto fail;
  3897. free_token(token);
  3898. if (read_expected(EVENT_OP, ";") < 0)
  3899. return;
  3900. if (read_expect_type(EVENT_NEWLINE, &token))
  3901. goto fail;
  3902. }
  3903. fail:
  3904. free_token(token);
  3905. return;
  3906. discard:
  3907. input_buf_ptr = save_input_buf_ptr;
  3908. input_buf_siz = save_input_buf_siz;
  3909. *offset = 0;
  3910. *size = 0;
  3911. free_token(token);
  3912. }
  3913. /**
  3914. * pevent_parse_header_page - parse the data stored in the header page
  3915. * @pevent: the handle to the pevent
  3916. * @buf: the buffer storing the header page format string
  3917. * @size: the size of @buf
  3918. * @long_size: the long size to use if there is no header
  3919. *
  3920. * This parses the header page format for information on the
  3921. * ring buffer used. The @buf should be copied from
  3922. *
  3923. * /sys/kernel/debug/tracing/events/header_page
  3924. */
  3925. int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
  3926. int long_size)
  3927. {
  3928. int ignore;
  3929. if (!size) {
  3930. /*
  3931. * Old kernels did not have header page info.
  3932. * Sorry but we just use what we find here in user space.
  3933. */
  3934. pevent->header_page_ts_size = sizeof(long long);
  3935. pevent->header_page_size_size = long_size;
  3936. pevent->header_page_data_offset = sizeof(long long) + long_size;
  3937. pevent->old_format = 1;
  3938. return -1;
  3939. }
  3940. init_input_buf(buf, size);
  3941. parse_header_field("timestamp", &pevent->header_page_ts_offset,
  3942. &pevent->header_page_ts_size, 1);
  3943. parse_header_field("commit", &pevent->header_page_size_offset,
  3944. &pevent->header_page_size_size, 1);
  3945. parse_header_field("overwrite", &pevent->header_page_overwrite,
  3946. &ignore, 0);
  3947. parse_header_field("data", &pevent->header_page_data_offset,
  3948. &pevent->header_page_data_size, 1);
  3949. return 0;
  3950. }
  3951. static int event_matches(struct event_format *event,
  3952. int id, const char *sys_name,
  3953. const char *event_name)
  3954. {
  3955. if (id >= 0 && id != event->id)
  3956. return 0;
  3957. if (event_name && (strcmp(event_name, event->name) != 0))
  3958. return 0;
  3959. if (sys_name && (strcmp(sys_name, event->system) != 0))
  3960. return 0;
  3961. return 1;
  3962. }
  3963. static void free_handler(struct event_handler *handle)
  3964. {
  3965. free((void *)handle->sys_name);
  3966. free((void *)handle->event_name);
  3967. free(handle);
  3968. }
  3969. static int find_event_handle(struct pevent *pevent, struct event_format *event)
  3970. {
  3971. struct event_handler *handle, **next;
  3972. for (next = &pevent->handlers; *next;
  3973. next = &(*next)->next) {
  3974. handle = *next;
  3975. if (event_matches(event, handle->id,
  3976. handle->sys_name,
  3977. handle->event_name))
  3978. break;
  3979. }
  3980. if (!(*next))
  3981. return 0;
  3982. pr_stat("overriding event (%d) %s:%s with new print handler",
  3983. event->id, event->system, event->name);
  3984. event->handler = handle->func;
  3985. event->context = handle->context;
  3986. *next = handle->next;
  3987. free_handler(handle);
  3988. return 1;
  3989. }
  3990. /**
  3991. * pevent_parse_event - parse the event format
  3992. * @pevent: the handle to the pevent
  3993. * @buf: the buffer storing the event format string
  3994. * @size: the size of @buf
  3995. * @sys: the system the event belongs to
  3996. *
  3997. * This parses the event format and creates an event structure
  3998. * to quickly parse raw data for a given event.
  3999. *
  4000. * These files currently come from:
  4001. *
  4002. * /sys/kernel/debug/tracing/events/.../.../format
  4003. */
  4004. enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
  4005. unsigned long size, const char *sys)
  4006. {
  4007. struct event_format *event;
  4008. int ret;
  4009. init_input_buf(buf, size);
  4010. event = alloc_event();
  4011. if (!event)
  4012. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  4013. event->name = event_read_name();
  4014. if (!event->name) {
  4015. /* Bad event? */
  4016. ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
  4017. goto event_alloc_failed;
  4018. }
  4019. if (strcmp(sys, "ftrace") == 0) {
  4020. event->flags |= EVENT_FL_ISFTRACE;
  4021. if (strcmp(event->name, "bprint") == 0)
  4022. event->flags |= EVENT_FL_ISBPRINT;
  4023. }
  4024. event->id = event_read_id();
  4025. if (event->id < 0) {
  4026. ret = PEVENT_ERRNO__READ_ID_FAILED;
  4027. /*
  4028. * This isn't an allocation error actually.
  4029. * But as the ID is critical, just bail out.
  4030. */
  4031. goto event_alloc_failed;
  4032. }
  4033. event->system = strdup(sys);
  4034. if (!event->system) {
  4035. ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
  4036. goto event_alloc_failed;
  4037. }
  4038. /* Add pevent to event so that it can be referenced */
  4039. event->pevent = pevent;
  4040. ret = event_read_format(event);
  4041. if (ret < 0) {
  4042. ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
  4043. goto event_parse_failed;
  4044. }
  4045. /*
  4046. * If the event has an override, don't print warnings if the event
  4047. * print format fails to parse.
  4048. */
  4049. if (find_event_handle(pevent, event))
  4050. show_warning = 0;
  4051. ret = event_read_print(event);
  4052. if (ret < 0) {
  4053. show_warning = 1;
  4054. ret = PEVENT_ERRNO__READ_PRINT_FAILED;
  4055. goto event_parse_failed;
  4056. }
  4057. show_warning = 1;
  4058. add_event(pevent, event);
  4059. if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
  4060. struct format_field *field;
  4061. struct print_arg *arg, **list;
  4062. /* old ftrace had no args */
  4063. list = &event->print_fmt.args;
  4064. for (field = event->format.fields; field; field = field->next) {
  4065. arg = alloc_arg();
  4066. arg->type = PRINT_FIELD;
  4067. arg->field.name = strdup(field->name);
  4068. if (!arg->field.name) {
  4069. event->flags |= EVENT_FL_FAILED;
  4070. free_arg(arg);
  4071. return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
  4072. }
  4073. arg->field.field = field;
  4074. *list = arg;
  4075. list = &arg->next;
  4076. }
  4077. return 0;
  4078. }
  4079. #define PRINT_ARGS 0
  4080. if (PRINT_ARGS && event->print_fmt.args)
  4081. print_args(event->print_fmt.args);
  4082. return 0;
  4083. event_parse_failed:
  4084. event->flags |= EVENT_FL_FAILED;
  4085. /* still add it even if it failed */
  4086. add_event(pevent, event);
  4087. return ret;
  4088. event_alloc_failed:
  4089. free(event->system);
  4090. free(event->name);
  4091. free(event);
  4092. return ret;
  4093. }
  4094. #undef _PE
  4095. #define _PE(code, str) str
  4096. static const char * const pevent_error_str[] = {
  4097. PEVENT_ERRORS
  4098. };
  4099. #undef _PE
  4100. int pevent_strerror(struct pevent *pevent, enum pevent_errno errnum,
  4101. char *buf, size_t buflen)
  4102. {
  4103. int idx;
  4104. const char *msg;
  4105. if (errnum >= 0) {
  4106. msg = strerror_r(errnum, buf, buflen);
  4107. if (msg != buf) {
  4108. size_t len = strlen(msg);
  4109. memcpy(buf, msg, min(buflen - 1, len));
  4110. *(buf + min(buflen - 1, len)) = '\0';
  4111. }
  4112. return 0;
  4113. }
  4114. if (errnum <= __PEVENT_ERRNO__START ||
  4115. errnum >= __PEVENT_ERRNO__END)
  4116. return -1;
  4117. idx = errnum - __PEVENT_ERRNO__START - 1;
  4118. msg = pevent_error_str[idx];
  4119. switch (errnum) {
  4120. case PEVENT_ERRNO__MEM_ALLOC_FAILED:
  4121. case PEVENT_ERRNO__PARSE_EVENT_FAILED:
  4122. case PEVENT_ERRNO__READ_ID_FAILED:
  4123. case PEVENT_ERRNO__READ_FORMAT_FAILED:
  4124. case PEVENT_ERRNO__READ_PRINT_FAILED:
  4125. case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED:
  4126. snprintf(buf, buflen, "%s", msg);
  4127. break;
  4128. default:
  4129. /* cannot reach here */
  4130. break;
  4131. }
  4132. return 0;
  4133. }
  4134. int get_field_val(struct trace_seq *s, struct format_field *field,
  4135. const char *name, struct pevent_record *record,
  4136. unsigned long long *val, int err)
  4137. {
  4138. if (!field) {
  4139. if (err)
  4140. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  4141. return -1;
  4142. }
  4143. if (pevent_read_number_field(field, record->data, val)) {
  4144. if (err)
  4145. trace_seq_printf(s, " %s=INVALID", name);
  4146. return -1;
  4147. }
  4148. return 0;
  4149. }
  4150. /**
  4151. * pevent_get_field_raw - return the raw pointer into the data field
  4152. * @s: The seq to print to on error
  4153. * @event: the event that the field is for
  4154. * @name: The name of the field
  4155. * @record: The record with the field name.
  4156. * @len: place to store the field length.
  4157. * @err: print default error if failed.
  4158. *
  4159. * Returns a pointer into record->data of the field and places
  4160. * the length of the field in @len.
  4161. *
  4162. * On failure, it returns NULL.
  4163. */
  4164. void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
  4165. const char *name, struct pevent_record *record,
  4166. int *len, int err)
  4167. {
  4168. struct format_field *field;
  4169. void *data = record->data;
  4170. unsigned offset;
  4171. int dummy;
  4172. if (!event)
  4173. return NULL;
  4174. field = pevent_find_field(event, name);
  4175. if (!field) {
  4176. if (err)
  4177. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  4178. return NULL;
  4179. }
  4180. /* Allow @len to be NULL */
  4181. if (!len)
  4182. len = &dummy;
  4183. offset = field->offset;
  4184. if (field->flags & FIELD_IS_DYNAMIC) {
  4185. offset = pevent_read_number(event->pevent,
  4186. data + offset, field->size);
  4187. *len = offset >> 16;
  4188. offset &= 0xffff;
  4189. } else
  4190. *len = field->size;
  4191. return data + offset;
  4192. }
  4193. /**
  4194. * pevent_get_field_val - find a 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_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_field(event, name);
  4212. return get_field_val(s, field, name, record, val, err);
  4213. }
  4214. /**
  4215. * pevent_get_common_field_val - find a common 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_common_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_common_field(event, name);
  4233. return get_field_val(s, field, name, record, val, err);
  4234. }
  4235. /**
  4236. * pevent_get_any_field_val - find a any field and return its value
  4237. * @s: The seq to print to on error
  4238. * @event: the event that the field is for
  4239. * @name: The name of the field
  4240. * @record: The record with the field name.
  4241. * @val: place to store the value of the field.
  4242. * @err: print default error if failed.
  4243. *
  4244. * Returns 0 on success -1 on field not found.
  4245. */
  4246. int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
  4247. const char *name, struct pevent_record *record,
  4248. unsigned long long *val, int err)
  4249. {
  4250. struct format_field *field;
  4251. if (!event)
  4252. return -1;
  4253. field = pevent_find_any_field(event, name);
  4254. return get_field_val(s, field, name, record, val, err);
  4255. }
  4256. /**
  4257. * pevent_print_num_field - print a field and a format
  4258. * @s: The seq to print to
  4259. * @fmt: The printf format to print the field with.
  4260. * @event: the event that the field is for
  4261. * @name: The name of the field
  4262. * @record: The record with the field name.
  4263. * @err: print default error if failed.
  4264. *
  4265. * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
  4266. */
  4267. int pevent_print_num_field(struct trace_seq *s, const char *fmt,
  4268. struct event_format *event, const char *name,
  4269. struct pevent_record *record, int err)
  4270. {
  4271. struct format_field *field = pevent_find_field(event, name);
  4272. unsigned long long val;
  4273. if (!field)
  4274. goto failed;
  4275. if (pevent_read_number_field(field, record->data, &val))
  4276. goto failed;
  4277. return trace_seq_printf(s, fmt, val);
  4278. failed:
  4279. if (err)
  4280. trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
  4281. return -1;
  4282. }
  4283. static void free_func_handle(struct pevent_function_handler *func)
  4284. {
  4285. struct pevent_func_params *params;
  4286. free(func->name);
  4287. while (func->params) {
  4288. params = func->params;
  4289. func->params = params->next;
  4290. free(params);
  4291. }
  4292. free(func);
  4293. }
  4294. /**
  4295. * pevent_register_print_function - register a helper function
  4296. * @pevent: the handle to the pevent
  4297. * @func: the function to process the helper function
  4298. * @ret_type: the return type of the helper function
  4299. * @name: the name of the helper function
  4300. * @parameters: A list of enum pevent_func_arg_type
  4301. *
  4302. * Some events may have helper functions in the print format arguments.
  4303. * This allows a plugin to dynamically create a way to process one
  4304. * of these functions.
  4305. *
  4306. * The @parameters is a variable list of pevent_func_arg_type enums that
  4307. * must end with PEVENT_FUNC_ARG_VOID.
  4308. */
  4309. int pevent_register_print_function(struct pevent *pevent,
  4310. pevent_func_handler func,
  4311. enum pevent_func_arg_type ret_type,
  4312. char *name, ...)
  4313. {
  4314. struct pevent_function_handler *func_handle;
  4315. struct pevent_func_params **next_param;
  4316. struct pevent_func_params *param;
  4317. enum pevent_func_arg_type type;
  4318. va_list ap;
  4319. int ret;
  4320. func_handle = find_func_handler(pevent, name);
  4321. if (func_handle) {
  4322. /*
  4323. * This is most like caused by the users own
  4324. * plugins updating the function. This overrides the
  4325. * system defaults.
  4326. */
  4327. pr_stat("override of function helper '%s'", name);
  4328. remove_func_handler(pevent, name);
  4329. }
  4330. func_handle = malloc(sizeof(*func_handle));
  4331. if (!func_handle) {
  4332. do_warning("Failed to allocate function handler");
  4333. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  4334. }
  4335. memset(func_handle, 0, sizeof(*func_handle));
  4336. func_handle->ret_type = ret_type;
  4337. func_handle->name = strdup(name);
  4338. func_handle->func = func;
  4339. if (!func_handle->name) {
  4340. do_warning("Failed to allocate function name");
  4341. free(func_handle);
  4342. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  4343. }
  4344. next_param = &(func_handle->params);
  4345. va_start(ap, name);
  4346. for (;;) {
  4347. type = va_arg(ap, enum pevent_func_arg_type);
  4348. if (type == PEVENT_FUNC_ARG_VOID)
  4349. break;
  4350. if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
  4351. do_warning("Invalid argument type %d", type);
  4352. ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
  4353. goto out_free;
  4354. }
  4355. param = malloc(sizeof(*param));
  4356. if (!param) {
  4357. do_warning("Failed to allocate function param");
  4358. ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
  4359. goto out_free;
  4360. }
  4361. param->type = type;
  4362. param->next = NULL;
  4363. *next_param = param;
  4364. next_param = &(param->next);
  4365. func_handle->nr_args++;
  4366. }
  4367. va_end(ap);
  4368. func_handle->next = pevent->func_handlers;
  4369. pevent->func_handlers = func_handle;
  4370. return 0;
  4371. out_free:
  4372. va_end(ap);
  4373. free_func_handle(func_handle);
  4374. return ret;
  4375. }
  4376. /**
  4377. * pevent_register_event_handler - register a way to parse an event
  4378. * @pevent: the handle to the pevent
  4379. * @id: the id of the event to register
  4380. * @sys_name: the system name the event belongs to
  4381. * @event_name: the name of the event
  4382. * @func: the function to call to parse the event information
  4383. * @context: the data to be passed to @func
  4384. *
  4385. * This function allows a developer to override the parsing of
  4386. * a given event. If for some reason the default print format
  4387. * is not sufficient, this function will register a function
  4388. * for an event to be used to parse the data instead.
  4389. *
  4390. * If @id is >= 0, then it is used to find the event.
  4391. * else @sys_name and @event_name are used.
  4392. */
  4393. int pevent_register_event_handler(struct pevent *pevent,
  4394. int id, char *sys_name, char *event_name,
  4395. pevent_event_handler_func func,
  4396. void *context)
  4397. {
  4398. struct event_format *event;
  4399. struct event_handler *handle;
  4400. if (id >= 0) {
  4401. /* search by id */
  4402. event = pevent_find_event(pevent, id);
  4403. if (!event)
  4404. goto not_found;
  4405. if (event_name && (strcmp(event_name, event->name) != 0))
  4406. goto not_found;
  4407. if (sys_name && (strcmp(sys_name, event->system) != 0))
  4408. goto not_found;
  4409. } else {
  4410. event = pevent_find_event_by_name(pevent, sys_name, event_name);
  4411. if (!event)
  4412. goto not_found;
  4413. }
  4414. pr_stat("overriding event (%d) %s:%s with new print handler",
  4415. event->id, event->system, event->name);
  4416. event->handler = func;
  4417. event->context = context;
  4418. return 0;
  4419. not_found:
  4420. /* Save for later use. */
  4421. handle = malloc(sizeof(*handle));
  4422. if (!handle) {
  4423. do_warning("Failed to allocate event handler");
  4424. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  4425. }
  4426. memset(handle, 0, sizeof(*handle));
  4427. handle->id = id;
  4428. if (event_name)
  4429. handle->event_name = strdup(event_name);
  4430. if (sys_name)
  4431. handle->sys_name = strdup(sys_name);
  4432. if ((event_name && !handle->event_name) ||
  4433. (sys_name && !handle->sys_name)) {
  4434. do_warning("Failed to allocate event/sys name");
  4435. free((void *)handle->event_name);
  4436. free((void *)handle->sys_name);
  4437. free(handle);
  4438. return PEVENT_ERRNO__MEM_ALLOC_FAILED;
  4439. }
  4440. handle->func = func;
  4441. handle->next = pevent->handlers;
  4442. pevent->handlers = handle;
  4443. handle->context = context;
  4444. return -1;
  4445. }
  4446. /**
  4447. * pevent_alloc - create a pevent handle
  4448. */
  4449. struct pevent *pevent_alloc(void)
  4450. {
  4451. struct pevent *pevent;
  4452. pevent = malloc(sizeof(*pevent));
  4453. if (!pevent)
  4454. return NULL;
  4455. memset(pevent, 0, sizeof(*pevent));
  4456. pevent->ref_count = 1;
  4457. return pevent;
  4458. }
  4459. void pevent_ref(struct pevent *pevent)
  4460. {
  4461. pevent->ref_count++;
  4462. }
  4463. static void free_format_fields(struct format_field *field)
  4464. {
  4465. struct format_field *next;
  4466. while (field) {
  4467. next = field->next;
  4468. free(field->type);
  4469. free(field->name);
  4470. free(field);
  4471. field = next;
  4472. }
  4473. }
  4474. static void free_formats(struct format *format)
  4475. {
  4476. free_format_fields(format->common_fields);
  4477. free_format_fields(format->fields);
  4478. }
  4479. static void free_event(struct event_format *event)
  4480. {
  4481. free(event->name);
  4482. free(event->system);
  4483. free_formats(&event->format);
  4484. free(event->print_fmt.format);
  4485. free_args(event->print_fmt.args);
  4486. free(event);
  4487. }
  4488. /**
  4489. * pevent_free - free a pevent handle
  4490. * @pevent: the pevent handle to free
  4491. */
  4492. void pevent_free(struct pevent *pevent)
  4493. {
  4494. struct cmdline_list *cmdlist, *cmdnext;
  4495. struct func_list *funclist, *funcnext;
  4496. struct printk_list *printklist, *printknext;
  4497. struct pevent_function_handler *func_handler;
  4498. struct event_handler *handle;
  4499. int i;
  4500. if (!pevent)
  4501. return;
  4502. cmdlist = pevent->cmdlist;
  4503. funclist = pevent->funclist;
  4504. printklist = pevent->printklist;
  4505. pevent->ref_count--;
  4506. if (pevent->ref_count)
  4507. return;
  4508. if (pevent->cmdlines) {
  4509. for (i = 0; i < pevent->cmdline_count; i++)
  4510. free(pevent->cmdlines[i].comm);
  4511. free(pevent->cmdlines);
  4512. }
  4513. while (cmdlist) {
  4514. cmdnext = cmdlist->next;
  4515. free(cmdlist->comm);
  4516. free(cmdlist);
  4517. cmdlist = cmdnext;
  4518. }
  4519. if (pevent->func_map) {
  4520. for (i = 0; i < pevent->func_count; i++) {
  4521. free(pevent->func_map[i].func);
  4522. free(pevent->func_map[i].mod);
  4523. }
  4524. free(pevent->func_map);
  4525. }
  4526. while (funclist) {
  4527. funcnext = funclist->next;
  4528. free(funclist->func);
  4529. free(funclist->mod);
  4530. free(funclist);
  4531. funclist = funcnext;
  4532. }
  4533. while (pevent->func_handlers) {
  4534. func_handler = pevent->func_handlers;
  4535. pevent->func_handlers = func_handler->next;
  4536. free_func_handle(func_handler);
  4537. }
  4538. if (pevent->printk_map) {
  4539. for (i = 0; i < pevent->printk_count; i++)
  4540. free(pevent->printk_map[i].printk);
  4541. free(pevent->printk_map);
  4542. }
  4543. while (printklist) {
  4544. printknext = printklist->next;
  4545. free(printklist->printk);
  4546. free(printklist);
  4547. printklist = printknext;
  4548. }
  4549. for (i = 0; i < pevent->nr_events; i++)
  4550. free_event(pevent->events[i]);
  4551. while (pevent->handlers) {
  4552. handle = pevent->handlers;
  4553. pevent->handlers = handle->next;
  4554. free_handler(handle);
  4555. }
  4556. free(pevent->events);
  4557. free(pevent->sort_events);
  4558. free(pevent);
  4559. }
  4560. void pevent_unref(struct pevent *pevent)
  4561. {
  4562. pevent_free(pevent);
  4563. }