event-parse.c 118 KB

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