event-parse.c 107 KB

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