event-parse.c 113 KB

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