event-parse.c 113 KB

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