event-parse.c 118 KB

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