event-parse.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078
  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. /* arg->op.op (= token) will be freed at out_free */
  1413. arg->op.op = NULL;
  1414. goto out_free;
  1415. }
  1416. type = read_token_item(&token);
  1417. *tok = token;
  1418. /* could just be a type pointer */
  1419. if ((strcmp(arg->op.op, "*") == 0) &&
  1420. type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
  1421. if (left->type != PRINT_ATOM)
  1422. die("bad pointer type");
  1423. left->atom.atom = realloc(left->atom.atom,
  1424. strlen(left->atom.atom) + 3);
  1425. strcat(left->atom.atom, " *");
  1426. free(arg->op.op);
  1427. *arg = *left;
  1428. free(left);
  1429. return type;
  1430. }
  1431. right = alloc_arg();
  1432. type = process_arg_token(event, right, tok, type);
  1433. arg->op.right = right;
  1434. } else if (strcmp(token, "[") == 0) {
  1435. left = alloc_arg();
  1436. *left = *arg;
  1437. arg->type = PRINT_OP;
  1438. arg->op.op = token;
  1439. arg->op.left = left;
  1440. arg->op.prio = 0;
  1441. type = process_array(event, arg, tok);
  1442. } else {
  1443. do_warning("unknown op '%s'", token);
  1444. event->flags |= EVENT_FL_FAILED;
  1445. /* the arg is now the left side */
  1446. goto out_free;
  1447. }
  1448. if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
  1449. int prio;
  1450. /* higher prios need to be closer to the root */
  1451. prio = get_op_prio(*tok);
  1452. if (prio > arg->op.prio)
  1453. return process_op(event, arg, tok);
  1454. return process_op(event, right, tok);
  1455. }
  1456. return type;
  1457. out_free:
  1458. free_token(token);
  1459. *tok = NULL;
  1460. return EVENT_ERROR;
  1461. }
  1462. static enum event_type
  1463. process_entry(struct event_format *event __unused, struct print_arg *arg,
  1464. char **tok)
  1465. {
  1466. enum event_type type;
  1467. char *field;
  1468. char *token;
  1469. if (read_expected(EVENT_OP, "->") < 0)
  1470. goto out_err;
  1471. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1472. goto out_free;
  1473. field = token;
  1474. arg->type = PRINT_FIELD;
  1475. arg->field.name = field;
  1476. if (is_flag_field) {
  1477. arg->field.field = pevent_find_any_field(event, arg->field.name);
  1478. arg->field.field->flags |= FIELD_IS_FLAG;
  1479. is_flag_field = 0;
  1480. } else if (is_symbolic_field) {
  1481. arg->field.field = pevent_find_any_field(event, arg->field.name);
  1482. arg->field.field->flags |= FIELD_IS_SYMBOLIC;
  1483. is_symbolic_field = 0;
  1484. }
  1485. type = read_token(&token);
  1486. *tok = token;
  1487. return type;
  1488. out_free:
  1489. free_token(token);
  1490. out_err:
  1491. *tok = NULL;
  1492. return EVENT_ERROR;
  1493. }
  1494. static char *arg_eval (struct print_arg *arg);
  1495. static unsigned long long
  1496. eval_type_str(unsigned long long val, const char *type, int pointer)
  1497. {
  1498. int sign = 0;
  1499. char *ref;
  1500. int len;
  1501. len = strlen(type);
  1502. if (pointer) {
  1503. if (type[len-1] != '*') {
  1504. do_warning("pointer expected with non pointer type");
  1505. return val;
  1506. }
  1507. ref = malloc_or_die(len);
  1508. memcpy(ref, type, len);
  1509. /* chop off the " *" */
  1510. ref[len - 2] = 0;
  1511. val = eval_type_str(val, ref, 0);
  1512. free(ref);
  1513. return val;
  1514. }
  1515. /* check if this is a pointer */
  1516. if (type[len - 1] == '*')
  1517. return val;
  1518. /* Try to figure out the arg size*/
  1519. if (strncmp(type, "struct", 6) == 0)
  1520. /* all bets off */
  1521. return val;
  1522. if (strcmp(type, "u8") == 0)
  1523. return val & 0xff;
  1524. if (strcmp(type, "u16") == 0)
  1525. return val & 0xffff;
  1526. if (strcmp(type, "u32") == 0)
  1527. return val & 0xffffffff;
  1528. if (strcmp(type, "u64") == 0 ||
  1529. strcmp(type, "s64"))
  1530. return val;
  1531. if (strcmp(type, "s8") == 0)
  1532. return (unsigned long long)(char)val & 0xff;
  1533. if (strcmp(type, "s16") == 0)
  1534. return (unsigned long long)(short)val & 0xffff;
  1535. if (strcmp(type, "s32") == 0)
  1536. return (unsigned long long)(int)val & 0xffffffff;
  1537. if (strncmp(type, "unsigned ", 9) == 0) {
  1538. sign = 0;
  1539. type += 9;
  1540. }
  1541. if (strcmp(type, "char") == 0) {
  1542. if (sign)
  1543. return (unsigned long long)(char)val & 0xff;
  1544. else
  1545. return val & 0xff;
  1546. }
  1547. if (strcmp(type, "short") == 0) {
  1548. if (sign)
  1549. return (unsigned long long)(short)val & 0xffff;
  1550. else
  1551. return val & 0xffff;
  1552. }
  1553. if (strcmp(type, "int") == 0) {
  1554. if (sign)
  1555. return (unsigned long long)(int)val & 0xffffffff;
  1556. else
  1557. return val & 0xffffffff;
  1558. }
  1559. return val;
  1560. }
  1561. /*
  1562. * Try to figure out the type.
  1563. */
  1564. static unsigned long long
  1565. eval_type(unsigned long long val, struct print_arg *arg, int pointer)
  1566. {
  1567. if (arg->type != PRINT_TYPE)
  1568. die("expected type argument");
  1569. return eval_type_str(val, arg->typecast.type, pointer);
  1570. }
  1571. static int arg_num_eval(struct print_arg *arg, long long *val)
  1572. {
  1573. long long left, right;
  1574. int ret = 1;
  1575. switch (arg->type) {
  1576. case PRINT_ATOM:
  1577. *val = strtoll(arg->atom.atom, NULL, 0);
  1578. break;
  1579. case PRINT_TYPE:
  1580. ret = arg_num_eval(arg->typecast.item, val);
  1581. if (!ret)
  1582. break;
  1583. *val = eval_type(*val, arg, 0);
  1584. break;
  1585. case PRINT_OP:
  1586. switch (arg->op.op[0]) {
  1587. case '|':
  1588. ret = arg_num_eval(arg->op.left, &left);
  1589. if (!ret)
  1590. break;
  1591. ret = arg_num_eval(arg->op.right, &right);
  1592. if (!ret)
  1593. break;
  1594. if (arg->op.op[1])
  1595. *val = left || right;
  1596. else
  1597. *val = left | right;
  1598. break;
  1599. case '&':
  1600. ret = arg_num_eval(arg->op.left, &left);
  1601. if (!ret)
  1602. break;
  1603. ret = arg_num_eval(arg->op.right, &right);
  1604. if (!ret)
  1605. break;
  1606. if (arg->op.op[1])
  1607. *val = left && right;
  1608. else
  1609. *val = left & right;
  1610. break;
  1611. case '<':
  1612. ret = arg_num_eval(arg->op.left, &left);
  1613. if (!ret)
  1614. break;
  1615. ret = arg_num_eval(arg->op.right, &right);
  1616. if (!ret)
  1617. break;
  1618. switch (arg->op.op[1]) {
  1619. case 0:
  1620. *val = left < right;
  1621. break;
  1622. case '<':
  1623. *val = left << right;
  1624. break;
  1625. case '=':
  1626. *val = left <= right;
  1627. break;
  1628. default:
  1629. do_warning("unknown op '%s'", arg->op.op);
  1630. ret = 0;
  1631. }
  1632. break;
  1633. case '>':
  1634. ret = arg_num_eval(arg->op.left, &left);
  1635. if (!ret)
  1636. break;
  1637. ret = arg_num_eval(arg->op.right, &right);
  1638. if (!ret)
  1639. break;
  1640. switch (arg->op.op[1]) {
  1641. case 0:
  1642. *val = left > right;
  1643. break;
  1644. case '>':
  1645. *val = left >> right;
  1646. break;
  1647. case '=':
  1648. *val = left >= right;
  1649. break;
  1650. default:
  1651. do_warning("unknown op '%s'", arg->op.op);
  1652. ret = 0;
  1653. }
  1654. break;
  1655. case '=':
  1656. ret = arg_num_eval(arg->op.left, &left);
  1657. if (!ret)
  1658. break;
  1659. ret = arg_num_eval(arg->op.right, &right);
  1660. if (!ret)
  1661. break;
  1662. if (arg->op.op[1] != '=') {
  1663. do_warning("unknown op '%s'", arg->op.op);
  1664. ret = 0;
  1665. } else
  1666. *val = left == right;
  1667. break;
  1668. case '!':
  1669. ret = arg_num_eval(arg->op.left, &left);
  1670. if (!ret)
  1671. break;
  1672. ret = arg_num_eval(arg->op.right, &right);
  1673. if (!ret)
  1674. break;
  1675. switch (arg->op.op[1]) {
  1676. case '=':
  1677. *val = left != right;
  1678. break;
  1679. default:
  1680. do_warning("unknown op '%s'", arg->op.op);
  1681. ret = 0;
  1682. }
  1683. break;
  1684. case '-':
  1685. /* check for negative */
  1686. if (arg->op.left->type == PRINT_NULL)
  1687. left = 0;
  1688. else
  1689. ret = arg_num_eval(arg->op.left, &left);
  1690. if (!ret)
  1691. break;
  1692. ret = arg_num_eval(arg->op.right, &right);
  1693. if (!ret)
  1694. break;
  1695. *val = left - right;
  1696. break;
  1697. case '+':
  1698. if (arg->op.left->type == PRINT_NULL)
  1699. left = 0;
  1700. else
  1701. ret = arg_num_eval(arg->op.left, &left);
  1702. if (!ret)
  1703. break;
  1704. ret = arg_num_eval(arg->op.right, &right);
  1705. if (!ret)
  1706. break;
  1707. *val = left + right;
  1708. break;
  1709. default:
  1710. do_warning("unknown op '%s'", arg->op.op);
  1711. ret = 0;
  1712. }
  1713. break;
  1714. case PRINT_NULL:
  1715. case PRINT_FIELD ... PRINT_SYMBOL:
  1716. case PRINT_STRING:
  1717. case PRINT_BSTRING:
  1718. default:
  1719. do_warning("invalid eval type %d", arg->type);
  1720. ret = 0;
  1721. }
  1722. return ret;
  1723. }
  1724. static char *arg_eval (struct print_arg *arg)
  1725. {
  1726. long long val;
  1727. static char buf[20];
  1728. switch (arg->type) {
  1729. case PRINT_ATOM:
  1730. return arg->atom.atom;
  1731. case PRINT_TYPE:
  1732. return arg_eval(arg->typecast.item);
  1733. case PRINT_OP:
  1734. if (!arg_num_eval(arg, &val))
  1735. break;
  1736. sprintf(buf, "%lld", val);
  1737. return buf;
  1738. case PRINT_NULL:
  1739. case PRINT_FIELD ... PRINT_SYMBOL:
  1740. case PRINT_STRING:
  1741. case PRINT_BSTRING:
  1742. default:
  1743. die("invalid eval type %d", arg->type);
  1744. break;
  1745. }
  1746. return NULL;
  1747. }
  1748. static enum event_type
  1749. process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
  1750. {
  1751. enum event_type type;
  1752. struct print_arg *arg = NULL;
  1753. struct print_flag_sym *field;
  1754. char *token = *tok;
  1755. char *value;
  1756. do {
  1757. free_token(token);
  1758. type = read_token_item(&token);
  1759. if (test_type_token(type, token, EVENT_OP, "{"))
  1760. break;
  1761. arg = alloc_arg();
  1762. free_token(token);
  1763. type = process_arg(event, arg, &token);
  1764. if (type == EVENT_OP)
  1765. type = process_op(event, arg, &token);
  1766. if (type == EVENT_ERROR)
  1767. goto out_free;
  1768. if (test_type_token(type, token, EVENT_DELIM, ","))
  1769. goto out_free;
  1770. field = malloc_or_die(sizeof(*field));
  1771. memset(field, 0, sizeof(*field));
  1772. value = arg_eval(arg);
  1773. if (value == NULL)
  1774. goto out_free;
  1775. field->value = strdup(value);
  1776. free_arg(arg);
  1777. arg = alloc_arg();
  1778. free_token(token);
  1779. type = process_arg(event, arg, &token);
  1780. if (test_type_token(type, token, EVENT_OP, "}"))
  1781. goto out_free;
  1782. value = arg_eval(arg);
  1783. if (value == NULL)
  1784. goto out_free;
  1785. field->str = strdup(value);
  1786. free_arg(arg);
  1787. arg = NULL;
  1788. *list = field;
  1789. list = &field->next;
  1790. free_token(token);
  1791. type = read_token_item(&token);
  1792. } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
  1793. *tok = token;
  1794. return type;
  1795. out_free:
  1796. free_arg(arg);
  1797. free_token(token);
  1798. *tok = NULL;
  1799. return EVENT_ERROR;
  1800. }
  1801. static enum event_type
  1802. process_flags(struct event_format *event, struct print_arg *arg, char **tok)
  1803. {
  1804. struct print_arg *field;
  1805. enum event_type type;
  1806. char *token;
  1807. memset(arg, 0, sizeof(*arg));
  1808. arg->type = PRINT_FLAGS;
  1809. field = alloc_arg();
  1810. type = process_arg(event, field, &token);
  1811. /* Handle operations in the first argument */
  1812. while (type == EVENT_OP)
  1813. type = process_op(event, field, &token);
  1814. if (test_type_token(type, token, EVENT_DELIM, ","))
  1815. goto out_free;
  1816. free_token(token);
  1817. arg->flags.field = field;
  1818. type = read_token_item(&token);
  1819. if (event_item_type(type)) {
  1820. arg->flags.delim = token;
  1821. type = read_token_item(&token);
  1822. }
  1823. if (test_type_token(type, token, EVENT_DELIM, ","))
  1824. goto out_free;
  1825. type = process_fields(event, &arg->flags.flags, &token);
  1826. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1827. goto out_free;
  1828. free_token(token);
  1829. type = read_token_item(tok);
  1830. return type;
  1831. out_free:
  1832. free_token(token);
  1833. *tok = NULL;
  1834. return EVENT_ERROR;
  1835. }
  1836. static enum event_type
  1837. process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
  1838. {
  1839. struct print_arg *field;
  1840. enum event_type type;
  1841. char *token;
  1842. memset(arg, 0, sizeof(*arg));
  1843. arg->type = PRINT_SYMBOL;
  1844. field = alloc_arg();
  1845. type = process_arg(event, field, &token);
  1846. if (test_type_token(type, token, EVENT_DELIM, ","))
  1847. goto out_free;
  1848. arg->symbol.field = field;
  1849. type = process_fields(event, &arg->symbol.symbols, &token);
  1850. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1851. goto out_free;
  1852. free_token(token);
  1853. type = read_token_item(tok);
  1854. return type;
  1855. out_free:
  1856. free_token(token);
  1857. *tok = NULL;
  1858. return EVENT_ERROR;
  1859. }
  1860. static enum event_type
  1861. process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
  1862. {
  1863. struct format_field *field;
  1864. enum event_type type;
  1865. char *token;
  1866. memset(arg, 0, sizeof(*arg));
  1867. arg->type = PRINT_DYNAMIC_ARRAY;
  1868. /*
  1869. * The item within the parenthesis is another field that holds
  1870. * the index into where the array starts.
  1871. */
  1872. type = read_token(&token);
  1873. *tok = token;
  1874. if (type != EVENT_ITEM)
  1875. goto out_free;
  1876. /* Find the field */
  1877. field = pevent_find_field(event, token);
  1878. if (!field)
  1879. goto out_free;
  1880. arg->dynarray.field = field;
  1881. arg->dynarray.index = 0;
  1882. if (read_expected(EVENT_DELIM, ")") < 0)
  1883. goto out_free;
  1884. free_token(token);
  1885. type = read_token_item(&token);
  1886. *tok = token;
  1887. if (type != EVENT_OP || strcmp(token, "[") != 0)
  1888. return type;
  1889. free_token(token);
  1890. arg = alloc_arg();
  1891. type = process_arg(event, arg, &token);
  1892. if (type == EVENT_ERROR)
  1893. goto out_free_arg;
  1894. if (!test_type_token(type, token, EVENT_OP, "]"))
  1895. goto out_free_arg;
  1896. free_token(token);
  1897. type = read_token_item(tok);
  1898. return type;
  1899. out_free_arg:
  1900. free_arg(arg);
  1901. out_free:
  1902. free_token(token);
  1903. *tok = NULL;
  1904. return EVENT_ERROR;
  1905. }
  1906. static enum event_type
  1907. process_paren(struct event_format *event, struct print_arg *arg, char **tok)
  1908. {
  1909. struct print_arg *item_arg;
  1910. enum event_type type;
  1911. char *token;
  1912. type = process_arg(event, arg, &token);
  1913. if (type == EVENT_ERROR)
  1914. goto out_free;
  1915. if (type == EVENT_OP)
  1916. type = process_op(event, arg, &token);
  1917. if (type == EVENT_ERROR)
  1918. goto out_free;
  1919. if (test_type_token(type, token, EVENT_DELIM, ")"))
  1920. goto out_free;
  1921. free_token(token);
  1922. type = read_token_item(&token);
  1923. /*
  1924. * If the next token is an item or another open paren, then
  1925. * this was a typecast.
  1926. */
  1927. if (event_item_type(type) ||
  1928. (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
  1929. /* make this a typecast and contine */
  1930. /* prevous must be an atom */
  1931. if (arg->type != PRINT_ATOM)
  1932. die("previous needed to be PRINT_ATOM");
  1933. item_arg = alloc_arg();
  1934. arg->type = PRINT_TYPE;
  1935. arg->typecast.type = arg->atom.atom;
  1936. arg->typecast.item = item_arg;
  1937. type = process_arg_token(event, item_arg, &token, type);
  1938. }
  1939. *tok = token;
  1940. return type;
  1941. out_free:
  1942. free_token(token);
  1943. *tok = NULL;
  1944. return EVENT_ERROR;
  1945. }
  1946. static enum event_type
  1947. process_str(struct event_format *event __unused, struct print_arg *arg, char **tok)
  1948. {
  1949. enum event_type type;
  1950. char *token;
  1951. if (read_expect_type(EVENT_ITEM, &token) < 0)
  1952. goto out_free;
  1953. arg->type = PRINT_STRING;
  1954. arg->string.string = token;
  1955. arg->string.offset = -1;
  1956. if (read_expected(EVENT_DELIM, ")") < 0)
  1957. goto out_err;
  1958. type = read_token(&token);
  1959. *tok = token;
  1960. return type;
  1961. out_free:
  1962. free_token(token);
  1963. out_err:
  1964. *tok = NULL;
  1965. return EVENT_ERROR;
  1966. }
  1967. static struct pevent_function_handler *
  1968. find_func_handler(struct pevent *pevent, char *func_name)
  1969. {
  1970. struct pevent_function_handler *func;
  1971. for (func = pevent->func_handlers; func; func = func->next) {
  1972. if (strcmp(func->name, func_name) == 0)
  1973. break;
  1974. }
  1975. return func;
  1976. }
  1977. static void remove_func_handler(struct pevent *pevent, char *func_name)
  1978. {
  1979. struct pevent_function_handler *func;
  1980. struct pevent_function_handler **next;
  1981. next = &pevent->func_handlers;
  1982. while ((func = *next)) {
  1983. if (strcmp(func->name, func_name) == 0) {
  1984. *next = func->next;
  1985. free_func_handle(func);
  1986. break;
  1987. }
  1988. next = &func->next;
  1989. }
  1990. }
  1991. static enum event_type
  1992. process_func_handler(struct event_format *event, struct pevent_function_handler *func,
  1993. struct print_arg *arg, char **tok)
  1994. {
  1995. struct print_arg **next_arg;
  1996. struct print_arg *farg;
  1997. enum event_type type;
  1998. char *token;
  1999. char *test;
  2000. int i;
  2001. arg->type = PRINT_FUNC;
  2002. arg->func.func = func;
  2003. *tok = NULL;
  2004. next_arg = &(arg->func.args);
  2005. for (i = 0; i < func->nr_args; i++) {
  2006. farg = alloc_arg();
  2007. type = process_arg(event, farg, &token);
  2008. if (i < (func->nr_args - 1))
  2009. test = ",";
  2010. else
  2011. test = ")";
  2012. if (test_type_token(type, token, EVENT_DELIM, test)) {
  2013. free_arg(farg);
  2014. free_token(token);
  2015. return EVENT_ERROR;
  2016. }
  2017. *next_arg = farg;
  2018. next_arg = &(farg->next);
  2019. free_token(token);
  2020. }
  2021. type = read_token(&token);
  2022. *tok = token;
  2023. return type;
  2024. }
  2025. static enum event_type
  2026. process_function(struct event_format *event, struct print_arg *arg,
  2027. char *token, char **tok)
  2028. {
  2029. struct pevent_function_handler *func;
  2030. if (strcmp(token, "__print_flags") == 0) {
  2031. free_token(token);
  2032. is_flag_field = 1;
  2033. return process_flags(event, arg, tok);
  2034. }
  2035. if (strcmp(token, "__print_symbolic") == 0) {
  2036. free_token(token);
  2037. is_symbolic_field = 1;
  2038. return process_symbols(event, arg, tok);
  2039. }
  2040. if (strcmp(token, "__get_str") == 0) {
  2041. free_token(token);
  2042. return process_str(event, arg, tok);
  2043. }
  2044. if (strcmp(token, "__get_dynamic_array") == 0) {
  2045. free_token(token);
  2046. return process_dynamic_array(event, arg, tok);
  2047. }
  2048. func = find_func_handler(event->pevent, token);
  2049. if (func) {
  2050. free_token(token);
  2051. return process_func_handler(event, func, arg, tok);
  2052. }
  2053. do_warning("function %s not defined", token);
  2054. free_token(token);
  2055. return EVENT_ERROR;
  2056. }
  2057. static enum event_type
  2058. process_arg_token(struct event_format *event, struct print_arg *arg,
  2059. char **tok, enum event_type type)
  2060. {
  2061. char *token;
  2062. char *atom;
  2063. token = *tok;
  2064. switch (type) {
  2065. case EVENT_ITEM:
  2066. if (strcmp(token, "REC") == 0) {
  2067. free_token(token);
  2068. type = process_entry(event, arg, &token);
  2069. break;
  2070. }
  2071. atom = token;
  2072. /* test the next token */
  2073. type = read_token_item(&token);
  2074. /*
  2075. * If the next token is a parenthesis, then this
  2076. * is a function.
  2077. */
  2078. if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
  2079. free_token(token);
  2080. token = NULL;
  2081. /* this will free atom. */
  2082. type = process_function(event, arg, atom, &token);
  2083. break;
  2084. }
  2085. /* atoms can be more than one token long */
  2086. while (type == EVENT_ITEM) {
  2087. atom = realloc(atom, strlen(atom) + strlen(token) + 2);
  2088. strcat(atom, " ");
  2089. strcat(atom, token);
  2090. free_token(token);
  2091. type = read_token_item(&token);
  2092. }
  2093. arg->type = PRINT_ATOM;
  2094. arg->atom.atom = atom;
  2095. break;
  2096. case EVENT_DQUOTE:
  2097. case EVENT_SQUOTE:
  2098. arg->type = PRINT_ATOM;
  2099. arg->atom.atom = token;
  2100. type = read_token_item(&token);
  2101. break;
  2102. case EVENT_DELIM:
  2103. if (strcmp(token, "(") == 0) {
  2104. free_token(token);
  2105. type = process_paren(event, arg, &token);
  2106. break;
  2107. }
  2108. case EVENT_OP:
  2109. /* handle single ops */
  2110. arg->type = PRINT_OP;
  2111. arg->op.op = token;
  2112. arg->op.left = NULL;
  2113. type = process_op(event, arg, &token);
  2114. /* On error, the op is freed */
  2115. if (type == EVENT_ERROR)
  2116. arg->op.op = NULL;
  2117. /* return error type if errored */
  2118. break;
  2119. case EVENT_ERROR ... EVENT_NEWLINE:
  2120. default:
  2121. die("unexpected type %d", type);
  2122. }
  2123. *tok = token;
  2124. return type;
  2125. }
  2126. static int event_read_print_args(struct event_format *event, struct print_arg **list)
  2127. {
  2128. enum event_type type = EVENT_ERROR;
  2129. struct print_arg *arg;
  2130. char *token;
  2131. int args = 0;
  2132. do {
  2133. if (type == EVENT_NEWLINE) {
  2134. type = read_token_item(&token);
  2135. continue;
  2136. }
  2137. arg = alloc_arg();
  2138. type = process_arg(event, arg, &token);
  2139. if (type == EVENT_ERROR) {
  2140. free_token(token);
  2141. free_arg(arg);
  2142. return -1;
  2143. }
  2144. *list = arg;
  2145. args++;
  2146. if (type == EVENT_OP) {
  2147. type = process_op(event, arg, &token);
  2148. free_token(token);
  2149. if (type == EVENT_ERROR) {
  2150. *list = NULL;
  2151. free_arg(arg);
  2152. return -1;
  2153. }
  2154. list = &arg->next;
  2155. continue;
  2156. }
  2157. if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
  2158. free_token(token);
  2159. *list = arg;
  2160. list = &arg->next;
  2161. continue;
  2162. }
  2163. break;
  2164. } while (type != EVENT_NONE);
  2165. if (type != EVENT_NONE && type != EVENT_ERROR)
  2166. free_token(token);
  2167. return args;
  2168. }
  2169. static int event_read_print(struct event_format *event)
  2170. {
  2171. enum event_type type;
  2172. char *token;
  2173. int ret;
  2174. if (read_expected_item(EVENT_ITEM, "print") < 0)
  2175. return -1;
  2176. if (read_expected(EVENT_ITEM, "fmt") < 0)
  2177. return -1;
  2178. if (read_expected(EVENT_OP, ":") < 0)
  2179. return -1;
  2180. if (read_expect_type(EVENT_DQUOTE, &token) < 0)
  2181. goto fail;
  2182. concat:
  2183. event->print_fmt.format = token;
  2184. event->print_fmt.args = NULL;
  2185. /* ok to have no arg */
  2186. type = read_token_item(&token);
  2187. if (type == EVENT_NONE)
  2188. return 0;
  2189. /* Handle concatenation of print lines */
  2190. if (type == EVENT_DQUOTE) {
  2191. char *cat;
  2192. cat = malloc_or_die(strlen(event->print_fmt.format) +
  2193. strlen(token) + 1);
  2194. strcpy(cat, event->print_fmt.format);
  2195. strcat(cat, token);
  2196. free_token(token);
  2197. free_token(event->print_fmt.format);
  2198. event->print_fmt.format = NULL;
  2199. token = cat;
  2200. goto concat;
  2201. }
  2202. if (test_type_token(type, token, EVENT_DELIM, ","))
  2203. goto fail;
  2204. free_token(token);
  2205. ret = event_read_print_args(event, &event->print_fmt.args);
  2206. if (ret < 0)
  2207. return -1;
  2208. return ret;
  2209. fail:
  2210. free_token(token);
  2211. return -1;
  2212. }
  2213. /**
  2214. * pevent_find_common_field - return a common field by event
  2215. * @event: handle for the event
  2216. * @name: the name of the common field to return
  2217. *
  2218. * Returns a common field from the event by the given @name.
  2219. * This only searchs the common fields and not all field.
  2220. */
  2221. struct format_field *
  2222. pevent_find_common_field(struct event_format *event, const char *name)
  2223. {
  2224. struct format_field *format;
  2225. for (format = event->format.common_fields;
  2226. format; format = format->next) {
  2227. if (strcmp(format->name, name) == 0)
  2228. break;
  2229. }
  2230. return format;
  2231. }
  2232. /**
  2233. * pevent_find_field - find a non-common field
  2234. * @event: handle for the event
  2235. * @name: the name of the non-common field
  2236. *
  2237. * Returns a non-common field by the given @name.
  2238. * This does not search common fields.
  2239. */
  2240. struct format_field *
  2241. pevent_find_field(struct event_format *event, const char *name)
  2242. {
  2243. struct format_field *format;
  2244. for (format = event->format.fields;
  2245. format; format = format->next) {
  2246. if (strcmp(format->name, name) == 0)
  2247. break;
  2248. }
  2249. return format;
  2250. }
  2251. /**
  2252. * pevent_find_any_field - find any field by name
  2253. * @event: handle for the event
  2254. * @name: the name of the field
  2255. *
  2256. * Returns a field by the given @name.
  2257. * This searchs the common field names first, then
  2258. * the non-common ones if a common one was not found.
  2259. */
  2260. struct format_field *
  2261. pevent_find_any_field(struct event_format *event, const char *name)
  2262. {
  2263. struct format_field *format;
  2264. format = pevent_find_common_field(event, name);
  2265. if (format)
  2266. return format;
  2267. return pevent_find_field(event, name);
  2268. }
  2269. /**
  2270. * pevent_read_number - read a number from data
  2271. * @pevent: handle for the pevent
  2272. * @ptr: the raw data
  2273. * @size: the size of the data that holds the number
  2274. *
  2275. * Returns the number (converted to host) from the
  2276. * raw data.
  2277. */
  2278. unsigned long long pevent_read_number(struct pevent *pevent,
  2279. const void *ptr, int size)
  2280. {
  2281. switch (size) {
  2282. case 1:
  2283. return *(unsigned char *)ptr;
  2284. case 2:
  2285. return data2host2(pevent, ptr);
  2286. case 4:
  2287. return data2host4(pevent, ptr);
  2288. case 8:
  2289. return data2host8(pevent, ptr);
  2290. default:
  2291. /* BUG! */
  2292. return 0;
  2293. }
  2294. }
  2295. /**
  2296. * pevent_read_number_field - read a number from data
  2297. * @field: a handle to the field
  2298. * @data: the raw data to read
  2299. * @value: the value to place the number in
  2300. *
  2301. * Reads raw data according to a field offset and size,
  2302. * and translates it into @value.
  2303. *
  2304. * Returns 0 on success, -1 otherwise.
  2305. */
  2306. int pevent_read_number_field(struct format_field *field, const void *data,
  2307. unsigned long long *value)
  2308. {
  2309. if (!field)
  2310. return -1;
  2311. switch (field->size) {
  2312. case 1:
  2313. case 2:
  2314. case 4:
  2315. case 8:
  2316. *value = pevent_read_number(field->event->pevent,
  2317. data + field->offset, field->size);
  2318. return 0;
  2319. default:
  2320. return -1;
  2321. }
  2322. }
  2323. static int get_common_info(struct pevent *pevent,
  2324. const char *type, int *offset, int *size)
  2325. {
  2326. struct event_format *event;
  2327. struct format_field *field;
  2328. /*
  2329. * All events should have the same common elements.
  2330. * Pick any event to find where the type is;
  2331. */
  2332. if (!pevent->events)
  2333. die("no event_list!");
  2334. event = pevent->events[0];
  2335. field = pevent_find_common_field(event, type);
  2336. if (!field)
  2337. die("field '%s' not found", type);
  2338. *offset = field->offset;
  2339. *size = field->size;
  2340. return 0;
  2341. }
  2342. static int __parse_common(struct pevent *pevent, void *data,
  2343. int *size, int *offset, const char *name)
  2344. {
  2345. int ret;
  2346. if (!*size) {
  2347. ret = get_common_info(pevent, name, offset, size);
  2348. if (ret < 0)
  2349. return ret;
  2350. }
  2351. return pevent_read_number(pevent, data + *offset, *size);
  2352. }
  2353. static int trace_parse_common_type(struct pevent *pevent, void *data)
  2354. {
  2355. return __parse_common(pevent, data,
  2356. &pevent->type_size, &pevent->type_offset,
  2357. "common_type");
  2358. }
  2359. static int parse_common_pid(struct pevent *pevent, void *data)
  2360. {
  2361. return __parse_common(pevent, data,
  2362. &pevent->pid_size, &pevent->pid_offset,
  2363. "common_pid");
  2364. }
  2365. static int parse_common_pc(struct pevent *pevent, void *data)
  2366. {
  2367. return __parse_common(pevent, data,
  2368. &pevent->pc_size, &pevent->pc_offset,
  2369. "common_preempt_count");
  2370. }
  2371. static int parse_common_flags(struct pevent *pevent, void *data)
  2372. {
  2373. return __parse_common(pevent, data,
  2374. &pevent->flags_size, &pevent->flags_offset,
  2375. "common_flags");
  2376. }
  2377. static int parse_common_lock_depth(struct pevent *pevent, void *data)
  2378. {
  2379. int ret;
  2380. ret = __parse_common(pevent, data,
  2381. &pevent->ld_size, &pevent->ld_offset,
  2382. "common_lock_depth");
  2383. if (ret < 0)
  2384. return -1;
  2385. return ret;
  2386. }
  2387. static int events_id_cmp(const void *a, const void *b);
  2388. /**
  2389. * pevent_find_event - find an event by given id
  2390. * @pevent: a handle to the pevent
  2391. * @id: the id of the event
  2392. *
  2393. * Returns an event that has a given @id.
  2394. */
  2395. struct event_format *pevent_find_event(struct pevent *pevent, int id)
  2396. {
  2397. struct event_format **eventptr;
  2398. struct event_format key;
  2399. struct event_format *pkey = &key;
  2400. /* Check cache first */
  2401. if (pevent->last_event && pevent->last_event->id == id)
  2402. return pevent->last_event;
  2403. key.id = id;
  2404. eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
  2405. sizeof(*pevent->events), events_id_cmp);
  2406. if (eventptr) {
  2407. pevent->last_event = *eventptr;
  2408. return *eventptr;
  2409. }
  2410. return NULL;
  2411. }
  2412. /**
  2413. * pevent_find_event_by_name - find an event by given name
  2414. * @pevent: a handle to the pevent
  2415. * @sys: the system name to search for
  2416. * @name: the name of the event to search for
  2417. *
  2418. * This returns an event with a given @name and under the system
  2419. * @sys. If @sys is NULL the first event with @name is returned.
  2420. */
  2421. struct event_format *
  2422. pevent_find_event_by_name(struct pevent *pevent,
  2423. const char *sys, const char *name)
  2424. {
  2425. struct event_format *event;
  2426. int i;
  2427. if (pevent->last_event &&
  2428. strcmp(pevent->last_event->name, name) == 0 &&
  2429. (!sys || strcmp(pevent->last_event->system, sys) == 0))
  2430. return pevent->last_event;
  2431. for (i = 0; i < pevent->nr_events; i++) {
  2432. event = pevent->events[i];
  2433. if (strcmp(event->name, name) == 0) {
  2434. if (!sys)
  2435. break;
  2436. if (strcmp(event->system, sys) == 0)
  2437. break;
  2438. }
  2439. }
  2440. if (i == pevent->nr_events)
  2441. event = NULL;
  2442. pevent->last_event = event;
  2443. return event;
  2444. }
  2445. static unsigned long long
  2446. eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
  2447. {
  2448. struct pevent *pevent = event->pevent;
  2449. unsigned long long val = 0;
  2450. unsigned long long left, right;
  2451. struct print_arg *typearg = NULL;
  2452. struct print_arg *larg;
  2453. unsigned long offset;
  2454. unsigned int field_size;
  2455. switch (arg->type) {
  2456. case PRINT_NULL:
  2457. /* ?? */
  2458. return 0;
  2459. case PRINT_ATOM:
  2460. return strtoull(arg->atom.atom, NULL, 0);
  2461. case PRINT_FIELD:
  2462. if (!arg->field.field) {
  2463. arg->field.field = pevent_find_any_field(event, arg->field.name);
  2464. if (!arg->field.field)
  2465. die("field %s not found", arg->field.name);
  2466. }
  2467. /* must be a number */
  2468. val = pevent_read_number(pevent, data + arg->field.field->offset,
  2469. arg->field.field->size);
  2470. break;
  2471. case PRINT_FLAGS:
  2472. case PRINT_SYMBOL:
  2473. break;
  2474. case PRINT_TYPE:
  2475. val = eval_num_arg(data, size, event, arg->typecast.item);
  2476. return eval_type(val, arg, 0);
  2477. case PRINT_STRING:
  2478. case PRINT_BSTRING:
  2479. return 0;
  2480. case PRINT_FUNC: {
  2481. struct trace_seq s;
  2482. trace_seq_init(&s);
  2483. val = process_defined_func(&s, data, size, event, arg);
  2484. trace_seq_destroy(&s);
  2485. return val;
  2486. }
  2487. case PRINT_OP:
  2488. if (strcmp(arg->op.op, "[") == 0) {
  2489. /*
  2490. * Arrays are special, since we don't want
  2491. * to read the arg as is.
  2492. */
  2493. right = eval_num_arg(data, size, event, arg->op.right);
  2494. /* handle typecasts */
  2495. larg = arg->op.left;
  2496. while (larg->type == PRINT_TYPE) {
  2497. if (!typearg)
  2498. typearg = larg;
  2499. larg = larg->typecast.item;
  2500. }
  2501. /* Default to long size */
  2502. field_size = pevent->long_size;
  2503. switch (larg->type) {
  2504. case PRINT_DYNAMIC_ARRAY:
  2505. offset = pevent_read_number(pevent,
  2506. data + larg->dynarray.field->offset,
  2507. larg->dynarray.field->size);
  2508. if (larg->dynarray.field->elementsize)
  2509. field_size = larg->dynarray.field->elementsize;
  2510. /*
  2511. * The actual length of the dynamic array is stored
  2512. * in the top half of the field, and the offset
  2513. * is in the bottom half of the 32 bit field.
  2514. */
  2515. offset &= 0xffff;
  2516. offset += right;
  2517. break;
  2518. case PRINT_FIELD:
  2519. if (!larg->field.field) {
  2520. larg->field.field =
  2521. pevent_find_any_field(event, larg->field.name);
  2522. if (!larg->field.field)
  2523. die("field %s not found", larg->field.name);
  2524. }
  2525. field_size = larg->field.field->elementsize;
  2526. offset = larg->field.field->offset +
  2527. right * larg->field.field->elementsize;
  2528. break;
  2529. default:
  2530. goto default_op; /* oops, all bets off */
  2531. }
  2532. val = pevent_read_number(pevent,
  2533. data + offset, field_size);
  2534. if (typearg)
  2535. val = eval_type(val, typearg, 1);
  2536. break;
  2537. } else if (strcmp(arg->op.op, "?") == 0) {
  2538. left = eval_num_arg(data, size, event, arg->op.left);
  2539. arg = arg->op.right;
  2540. if (left)
  2541. val = eval_num_arg(data, size, event, arg->op.left);
  2542. else
  2543. val = eval_num_arg(data, size, event, arg->op.right);
  2544. break;
  2545. }
  2546. default_op:
  2547. left = eval_num_arg(data, size, event, arg->op.left);
  2548. right = eval_num_arg(data, size, event, arg->op.right);
  2549. switch (arg->op.op[0]) {
  2550. case '!':
  2551. switch (arg->op.op[1]) {
  2552. case 0:
  2553. val = !right;
  2554. break;
  2555. case '=':
  2556. val = left != right;
  2557. break;
  2558. default:
  2559. die("unknown op '%s'", arg->op.op);
  2560. }
  2561. break;
  2562. case '~':
  2563. val = ~right;
  2564. break;
  2565. case '|':
  2566. if (arg->op.op[1])
  2567. val = left || right;
  2568. else
  2569. val = left | right;
  2570. break;
  2571. case '&':
  2572. if (arg->op.op[1])
  2573. val = left && right;
  2574. else
  2575. val = left & right;
  2576. break;
  2577. case '<':
  2578. switch (arg->op.op[1]) {
  2579. case 0:
  2580. val = left < right;
  2581. break;
  2582. case '<':
  2583. val = left << right;
  2584. break;
  2585. case '=':
  2586. val = left <= right;
  2587. break;
  2588. default:
  2589. die("unknown op '%s'", arg->op.op);
  2590. }
  2591. break;
  2592. case '>':
  2593. switch (arg->op.op[1]) {
  2594. case 0:
  2595. val = left > right;
  2596. break;
  2597. case '>':
  2598. val = left >> right;
  2599. break;
  2600. case '=':
  2601. val = left >= right;
  2602. break;
  2603. default:
  2604. die("unknown op '%s'", arg->op.op);
  2605. }
  2606. break;
  2607. case '=':
  2608. if (arg->op.op[1] != '=')
  2609. die("unknown op '%s'", arg->op.op);
  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. case '*':
  2622. val = left * right;
  2623. break;
  2624. default:
  2625. die("unknown op '%s'", arg->op.op);
  2626. }
  2627. break;
  2628. default: /* not sure what to do there */
  2629. return 0;
  2630. }
  2631. return val;
  2632. }
  2633. struct flag {
  2634. const char *name;
  2635. unsigned long long value;
  2636. };
  2637. static const struct flag flags[] = {
  2638. { "HI_SOFTIRQ", 0 },
  2639. { "TIMER_SOFTIRQ", 1 },
  2640. { "NET_TX_SOFTIRQ", 2 },
  2641. { "NET_RX_SOFTIRQ", 3 },
  2642. { "BLOCK_SOFTIRQ", 4 },
  2643. { "BLOCK_IOPOLL_SOFTIRQ", 5 },
  2644. { "TASKLET_SOFTIRQ", 6 },
  2645. { "SCHED_SOFTIRQ", 7 },
  2646. { "HRTIMER_SOFTIRQ", 8 },
  2647. { "RCU_SOFTIRQ", 9 },
  2648. { "HRTIMER_NORESTART", 0 },
  2649. { "HRTIMER_RESTART", 1 },
  2650. };
  2651. static unsigned long long eval_flag(const char *flag)
  2652. {
  2653. int i;
  2654. /*
  2655. * Some flags in the format files do not get converted.
  2656. * If the flag is not numeric, see if it is something that
  2657. * we already know about.
  2658. */
  2659. if (isdigit(flag[0]))
  2660. return strtoull(flag, NULL, 0);
  2661. for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
  2662. if (strcmp(flags[i].name, flag) == 0)
  2663. return flags[i].value;
  2664. return 0;
  2665. }
  2666. static void print_str_to_seq(struct trace_seq *s, const char *format,
  2667. int len_arg, const char *str)
  2668. {
  2669. if (len_arg >= 0)
  2670. trace_seq_printf(s, format, len_arg, str);
  2671. else
  2672. trace_seq_printf(s, format, str);
  2673. }
  2674. static void print_str_arg(struct trace_seq *s, void *data, int size,
  2675. struct event_format *event, const char *format,
  2676. int len_arg, struct print_arg *arg)
  2677. {
  2678. struct pevent *pevent = event->pevent;
  2679. struct print_flag_sym *flag;
  2680. unsigned long long val, fval;
  2681. unsigned long addr;
  2682. char *str;
  2683. int print;
  2684. int len;
  2685. switch (arg->type) {
  2686. case PRINT_NULL:
  2687. /* ?? */
  2688. return;
  2689. case PRINT_ATOM:
  2690. print_str_to_seq(s, format, len_arg, arg->atom.atom);
  2691. return;
  2692. case PRINT_FIELD:
  2693. if (!arg->field.field) {
  2694. arg->field.field = pevent_find_any_field(event, arg->field.name);
  2695. if (!arg->field.field)
  2696. die("field %s not found", arg->field.name);
  2697. }
  2698. /* Zero sized fields, mean the rest of the data */
  2699. len = arg->field.field->size ? : size - arg->field.field->offset;
  2700. /*
  2701. * Some events pass in pointers. If this is not an array
  2702. * and the size is the same as long_size, assume that it
  2703. * is a pointer.
  2704. */
  2705. if (!(arg->field.field->flags & FIELD_IS_ARRAY) &&
  2706. arg->field.field->size == pevent->long_size) {
  2707. addr = *(unsigned long *)(data + arg->field.field->offset);
  2708. trace_seq_printf(s, "%lx", addr);
  2709. break;
  2710. }
  2711. str = malloc_or_die(len + 1);
  2712. memcpy(str, data + arg->field.field->offset, len);
  2713. str[len] = 0;
  2714. print_str_to_seq(s, format, len_arg, str);
  2715. free(str);
  2716. break;
  2717. case PRINT_FLAGS:
  2718. val = eval_num_arg(data, size, event, arg->flags.field);
  2719. print = 0;
  2720. for (flag = arg->flags.flags; flag; flag = flag->next) {
  2721. fval = eval_flag(flag->value);
  2722. if (!val && !fval) {
  2723. print_str_to_seq(s, format, len_arg, flag->str);
  2724. break;
  2725. }
  2726. if (fval && (val & fval) == fval) {
  2727. if (print && arg->flags.delim)
  2728. trace_seq_puts(s, arg->flags.delim);
  2729. print_str_to_seq(s, format, len_arg, flag->str);
  2730. print = 1;
  2731. val &= ~fval;
  2732. }
  2733. }
  2734. break;
  2735. case PRINT_SYMBOL:
  2736. val = eval_num_arg(data, size, event, arg->symbol.field);
  2737. for (flag = arg->symbol.symbols; flag; flag = flag->next) {
  2738. fval = eval_flag(flag->value);
  2739. if (val == fval) {
  2740. print_str_to_seq(s, format, len_arg, flag->str);
  2741. break;
  2742. }
  2743. }
  2744. break;
  2745. case PRINT_TYPE:
  2746. break;
  2747. case PRINT_STRING: {
  2748. int str_offset;
  2749. if (arg->string.offset == -1) {
  2750. struct format_field *f;
  2751. f = pevent_find_any_field(event, arg->string.string);
  2752. arg->string.offset = f->offset;
  2753. }
  2754. str_offset = data2host4(pevent, data + arg->string.offset);
  2755. str_offset &= 0xffff;
  2756. print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
  2757. break;
  2758. }
  2759. case PRINT_BSTRING:
  2760. trace_seq_printf(s, format, arg->string.string);
  2761. break;
  2762. case PRINT_OP:
  2763. /*
  2764. * The only op for string should be ? :
  2765. */
  2766. if (arg->op.op[0] != '?')
  2767. return;
  2768. val = eval_num_arg(data, size, event, arg->op.left);
  2769. if (val)
  2770. print_str_arg(s, data, size, event,
  2771. format, len_arg, arg->op.right->op.left);
  2772. else
  2773. print_str_arg(s, data, size, event,
  2774. format, len_arg, arg->op.right->op.right);
  2775. break;
  2776. case PRINT_FUNC:
  2777. process_defined_func(s, data, size, event, arg);
  2778. break;
  2779. default:
  2780. /* well... */
  2781. break;
  2782. }
  2783. }
  2784. static unsigned long long
  2785. process_defined_func(struct trace_seq *s, void *data, int size,
  2786. struct event_format *event, struct print_arg *arg)
  2787. {
  2788. struct pevent_function_handler *func_handle = arg->func.func;
  2789. struct pevent_func_params *param;
  2790. unsigned long long *args;
  2791. unsigned long long ret;
  2792. struct print_arg *farg;
  2793. struct trace_seq str;
  2794. struct save_str {
  2795. struct save_str *next;
  2796. char *str;
  2797. } *strings = NULL, *string;
  2798. int i;
  2799. if (!func_handle->nr_args) {
  2800. ret = (*func_handle->func)(s, NULL);
  2801. goto out;
  2802. }
  2803. farg = arg->func.args;
  2804. param = func_handle->params;
  2805. args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
  2806. for (i = 0; i < func_handle->nr_args; i++) {
  2807. switch (param->type) {
  2808. case PEVENT_FUNC_ARG_INT:
  2809. case PEVENT_FUNC_ARG_LONG:
  2810. case PEVENT_FUNC_ARG_PTR:
  2811. args[i] = eval_num_arg(data, size, event, farg);
  2812. break;
  2813. case PEVENT_FUNC_ARG_STRING:
  2814. trace_seq_init(&str);
  2815. print_str_arg(&str, data, size, event, "%s", -1, farg);
  2816. trace_seq_terminate(&str);
  2817. string = malloc_or_die(sizeof(*string));
  2818. string->next = strings;
  2819. string->str = strdup(str.buffer);
  2820. strings = string;
  2821. trace_seq_destroy(&str);
  2822. break;
  2823. default:
  2824. /*
  2825. * Something went totally wrong, this is not
  2826. * an input error, something in this code broke.
  2827. */
  2828. die("Unexpected end of arguments\n");
  2829. break;
  2830. }
  2831. farg = farg->next;
  2832. }
  2833. ret = (*func_handle->func)(s, args);
  2834. free(args);
  2835. while (strings) {
  2836. string = strings;
  2837. strings = string->next;
  2838. free(string->str);
  2839. free(string);
  2840. }
  2841. out:
  2842. /* TBD : handle return type here */
  2843. return ret;
  2844. }
  2845. static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
  2846. {
  2847. struct pevent *pevent = event->pevent;
  2848. struct format_field *field, *ip_field;
  2849. struct print_arg *args, *arg, **next;
  2850. unsigned long long ip, val;
  2851. char *ptr;
  2852. void *bptr;
  2853. field = pevent->bprint_buf_field;
  2854. ip_field = pevent->bprint_ip_field;
  2855. if (!field) {
  2856. field = pevent_find_field(event, "buf");
  2857. if (!field)
  2858. die("can't find buffer field for binary printk");
  2859. ip_field = pevent_find_field(event, "ip");
  2860. if (!ip_field)
  2861. die("can't find ip field for binary printk");
  2862. pevent->bprint_buf_field = field;
  2863. pevent->bprint_ip_field = ip_field;
  2864. }
  2865. ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
  2866. /*
  2867. * The first arg is the IP pointer.
  2868. */
  2869. args = alloc_arg();
  2870. arg = args;
  2871. arg->next = NULL;
  2872. next = &arg->next;
  2873. arg->type = PRINT_ATOM;
  2874. arg->atom.atom = malloc_or_die(32);
  2875. sprintf(arg->atom.atom, "%lld", ip);
  2876. /* skip the first "%pf : " */
  2877. for (ptr = fmt + 6, bptr = data + field->offset;
  2878. bptr < data + size && *ptr; ptr++) {
  2879. int ls = 0;
  2880. if (*ptr == '%') {
  2881. process_again:
  2882. ptr++;
  2883. switch (*ptr) {
  2884. case '%':
  2885. break;
  2886. case 'l':
  2887. ls++;
  2888. goto process_again;
  2889. case 'L':
  2890. ls = 2;
  2891. goto process_again;
  2892. case '0' ... '9':
  2893. goto process_again;
  2894. case 'p':
  2895. ls = 1;
  2896. /* fall through */
  2897. case 'd':
  2898. case 'u':
  2899. case 'x':
  2900. case 'i':
  2901. /* the pointers are always 4 bytes aligned */
  2902. bptr = (void *)(((unsigned long)bptr + 3) &
  2903. ~3);
  2904. switch (ls) {
  2905. case 0:
  2906. ls = 4;
  2907. break;
  2908. case 1:
  2909. ls = pevent->long_size;
  2910. break;
  2911. case 2:
  2912. ls = 8;
  2913. default:
  2914. break;
  2915. }
  2916. val = pevent_read_number(pevent, bptr, ls);
  2917. bptr += ls;
  2918. arg = alloc_arg();
  2919. arg->next = NULL;
  2920. arg->type = PRINT_ATOM;
  2921. arg->atom.atom = malloc_or_die(32);
  2922. sprintf(arg->atom.atom, "%lld", val);
  2923. *next = arg;
  2924. next = &arg->next;
  2925. break;
  2926. case 's':
  2927. arg = alloc_arg();
  2928. arg->next = NULL;
  2929. arg->type = PRINT_BSTRING;
  2930. arg->string.string = strdup(bptr);
  2931. bptr += strlen(bptr) + 1;
  2932. *next = arg;
  2933. next = &arg->next;
  2934. default:
  2935. break;
  2936. }
  2937. }
  2938. }
  2939. return args;
  2940. }
  2941. static void free_args(struct print_arg *args)
  2942. {
  2943. struct print_arg *next;
  2944. while (args) {
  2945. next = args->next;
  2946. free_arg(args);
  2947. args = next;
  2948. }
  2949. }
  2950. static char *
  2951. get_bprint_format(void *data, int size __unused, struct event_format *event)
  2952. {
  2953. struct pevent *pevent = event->pevent;
  2954. unsigned long long addr;
  2955. struct format_field *field;
  2956. struct printk_map *printk;
  2957. char *format;
  2958. char *p;
  2959. field = pevent->bprint_fmt_field;
  2960. if (!field) {
  2961. field = pevent_find_field(event, "fmt");
  2962. if (!field)
  2963. die("can't find format field for binary printk");
  2964. pevent->bprint_fmt_field = field;
  2965. }
  2966. addr = pevent_read_number(pevent, data + field->offset, field->size);
  2967. printk = find_printk(pevent, addr);
  2968. if (!printk) {
  2969. format = malloc_or_die(45);
  2970. sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
  2971. addr);
  2972. return format;
  2973. }
  2974. p = printk->printk;
  2975. /* Remove any quotes. */
  2976. if (*p == '"')
  2977. p++;
  2978. format = malloc_or_die(strlen(p) + 10);
  2979. sprintf(format, "%s : %s", "%pf", p);
  2980. /* remove ending quotes and new line since we will add one too */
  2981. p = format + strlen(format) - 1;
  2982. if (*p == '"')
  2983. *p = 0;
  2984. p -= 2;
  2985. if (strcmp(p, "\\n") == 0)
  2986. *p = 0;
  2987. return format;
  2988. }
  2989. static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
  2990. struct event_format *event, struct print_arg *arg)
  2991. {
  2992. unsigned char *buf;
  2993. char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
  2994. if (arg->type == PRINT_FUNC) {
  2995. process_defined_func(s, data, size, event, arg);
  2996. return;
  2997. }
  2998. if (arg->type != PRINT_FIELD) {
  2999. trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
  3000. arg->type);
  3001. return;
  3002. }
  3003. if (mac == 'm')
  3004. fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
  3005. if (!arg->field.field) {
  3006. arg->field.field =
  3007. pevent_find_any_field(event, arg->field.name);
  3008. if (!arg->field.field)
  3009. die("field %s not found", arg->field.name);
  3010. }
  3011. if (arg->field.field->size != 6) {
  3012. trace_seq_printf(s, "INVALIDMAC");
  3013. return;
  3014. }
  3015. buf = data + arg->field.field->offset;
  3016. trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  3017. }
  3018. static void print_event_fields(struct trace_seq *s, void *data, int size,
  3019. struct event_format *event)
  3020. {
  3021. struct format_field *field;
  3022. unsigned long long val;
  3023. unsigned int offset, len, i;
  3024. field = event->format.fields;
  3025. while (field) {
  3026. trace_seq_printf(s, " %s=", field->name);
  3027. if (field->flags & FIELD_IS_ARRAY) {
  3028. offset = field->offset;
  3029. len = field->size;
  3030. if (field->flags & FIELD_IS_DYNAMIC) {
  3031. val = pevent_read_number(event->pevent, data + offset, len);
  3032. offset = val;
  3033. len = offset >> 16;
  3034. offset &= 0xffff;
  3035. }
  3036. if (field->flags & FIELD_IS_STRING) {
  3037. trace_seq_printf(s, "%s", (char *)data + offset);
  3038. } else {
  3039. trace_seq_puts(s, "ARRAY[");
  3040. for (i = 0; i < len; i++) {
  3041. if (i)
  3042. trace_seq_puts(s, ", ");
  3043. trace_seq_printf(s, "%02x",
  3044. *((unsigned char *)data + offset + i));
  3045. }
  3046. trace_seq_putc(s, ']');
  3047. }
  3048. } else {
  3049. val = pevent_read_number(event->pevent, data + field->offset,
  3050. field->size);
  3051. if (field->flags & FIELD_IS_POINTER) {
  3052. trace_seq_printf(s, "0x%llx", val);
  3053. } else if (field->flags & FIELD_IS_SIGNED) {
  3054. switch (field->size) {
  3055. case 4:
  3056. /*
  3057. * If field is long then print it in hex.
  3058. * A long usually stores pointers.
  3059. */
  3060. if (field->flags & FIELD_IS_LONG)
  3061. trace_seq_printf(s, "0x%x", (int)val);
  3062. else
  3063. trace_seq_printf(s, "%d", (int)val);
  3064. break;
  3065. case 2:
  3066. trace_seq_printf(s, "%2d", (short)val);
  3067. break;
  3068. case 1:
  3069. trace_seq_printf(s, "%1d", (char)val);
  3070. break;
  3071. default:
  3072. trace_seq_printf(s, "%lld", val);
  3073. }
  3074. } else {
  3075. if (field->flags & FIELD_IS_LONG)
  3076. trace_seq_printf(s, "0x%llx", val);
  3077. else
  3078. trace_seq_printf(s, "%llu", val);
  3079. }
  3080. }
  3081. field = field->next;
  3082. }
  3083. }
  3084. static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
  3085. {
  3086. struct pevent *pevent = event->pevent;
  3087. struct print_fmt *print_fmt = &event->print_fmt;
  3088. struct print_arg *arg = print_fmt->args;
  3089. struct print_arg *args = NULL;
  3090. const char *ptr = print_fmt->format;
  3091. unsigned long long val;
  3092. struct func_map *func;
  3093. const char *saveptr;
  3094. char *bprint_fmt = NULL;
  3095. char format[32];
  3096. int show_func;
  3097. int len_as_arg;
  3098. int len_arg;
  3099. int len;
  3100. int ls;
  3101. if (event->flags & EVENT_FL_FAILED) {
  3102. trace_seq_printf(s, "[FAILED TO PARSE]");
  3103. print_event_fields(s, data, size, event);
  3104. return;
  3105. }
  3106. if (event->flags & EVENT_FL_ISBPRINT) {
  3107. bprint_fmt = get_bprint_format(data, size, event);
  3108. args = make_bprint_args(bprint_fmt, data, size, event);
  3109. arg = args;
  3110. ptr = bprint_fmt;
  3111. }
  3112. for (; *ptr; ptr++) {
  3113. ls = 0;
  3114. if (*ptr == '\\') {
  3115. ptr++;
  3116. switch (*ptr) {
  3117. case 'n':
  3118. trace_seq_putc(s, '\n');
  3119. break;
  3120. case 't':
  3121. trace_seq_putc(s, '\t');
  3122. break;
  3123. case 'r':
  3124. trace_seq_putc(s, '\r');
  3125. break;
  3126. case '\\':
  3127. trace_seq_putc(s, '\\');
  3128. break;
  3129. default:
  3130. trace_seq_putc(s, *ptr);
  3131. break;
  3132. }
  3133. } else if (*ptr == '%') {
  3134. saveptr = ptr;
  3135. show_func = 0;
  3136. len_as_arg = 0;
  3137. cont_process:
  3138. ptr++;
  3139. switch (*ptr) {
  3140. case '%':
  3141. trace_seq_putc(s, '%');
  3142. break;
  3143. case '#':
  3144. /* FIXME: need to handle properly */
  3145. goto cont_process;
  3146. case 'h':
  3147. ls--;
  3148. goto cont_process;
  3149. case 'l':
  3150. ls++;
  3151. goto cont_process;
  3152. case 'L':
  3153. ls = 2;
  3154. goto cont_process;
  3155. case '*':
  3156. /* The argument is the length. */
  3157. if (!arg)
  3158. die("no argument match");
  3159. len_arg = eval_num_arg(data, size, event, arg);
  3160. len_as_arg = 1;
  3161. arg = arg->next;
  3162. goto cont_process;
  3163. case '.':
  3164. case 'z':
  3165. case 'Z':
  3166. case '0' ... '9':
  3167. goto cont_process;
  3168. case 'p':
  3169. if (pevent->long_size == 4)
  3170. ls = 1;
  3171. else
  3172. ls = 2;
  3173. if (*(ptr+1) == 'F' ||
  3174. *(ptr+1) == 'f') {
  3175. ptr++;
  3176. show_func = *ptr;
  3177. } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
  3178. print_mac_arg(s, *(ptr+1), data, size, event, arg);
  3179. ptr++;
  3180. break;
  3181. }
  3182. /* fall through */
  3183. case 'd':
  3184. case 'i':
  3185. case 'x':
  3186. case 'X':
  3187. case 'u':
  3188. if (!arg)
  3189. die("no argument match");
  3190. len = ((unsigned long)ptr + 1) -
  3191. (unsigned long)saveptr;
  3192. /* should never happen */
  3193. if (len > 31)
  3194. die("bad format!");
  3195. memcpy(format, saveptr, len);
  3196. format[len] = 0;
  3197. val = eval_num_arg(data, size, event, arg);
  3198. arg = arg->next;
  3199. if (show_func) {
  3200. func = find_func(pevent, val);
  3201. if (func) {
  3202. trace_seq_puts(s, func->func);
  3203. if (show_func == 'F')
  3204. trace_seq_printf(s,
  3205. "+0x%llx",
  3206. val - func->addr);
  3207. break;
  3208. }
  3209. }
  3210. if (pevent->long_size == 8 && ls) {
  3211. char *p;
  3212. ls = 2;
  3213. /* make %l into %ll */
  3214. p = strchr(format, 'l');
  3215. if (p)
  3216. memmove(p, p+1, strlen(p)+1);
  3217. else if (strcmp(format, "%p") == 0)
  3218. strcpy(format, "0x%llx");
  3219. }
  3220. switch (ls) {
  3221. case -2:
  3222. if (len_as_arg)
  3223. trace_seq_printf(s, format, len_arg, (char)val);
  3224. else
  3225. trace_seq_printf(s, format, (char)val);
  3226. break;
  3227. case -1:
  3228. if (len_as_arg)
  3229. trace_seq_printf(s, format, len_arg, (short)val);
  3230. else
  3231. trace_seq_printf(s, format, (short)val);
  3232. break;
  3233. case 0:
  3234. if (len_as_arg)
  3235. trace_seq_printf(s, format, len_arg, (int)val);
  3236. else
  3237. trace_seq_printf(s, format, (int)val);
  3238. break;
  3239. case 1:
  3240. if (len_as_arg)
  3241. trace_seq_printf(s, format, len_arg, (long)val);
  3242. else
  3243. trace_seq_printf(s, format, (long)val);
  3244. break;
  3245. case 2:
  3246. if (len_as_arg)
  3247. trace_seq_printf(s, format, len_arg,
  3248. (long long)val);
  3249. else
  3250. trace_seq_printf(s, format, (long long)val);
  3251. break;
  3252. default:
  3253. die("bad count (%d)", ls);
  3254. }
  3255. break;
  3256. case 's':
  3257. if (!arg)
  3258. die("no matching argument");
  3259. len = ((unsigned long)ptr + 1) -
  3260. (unsigned long)saveptr;
  3261. /* should never happen */
  3262. if (len > 31)
  3263. die("bad format!");
  3264. memcpy(format, saveptr, len);
  3265. format[len] = 0;
  3266. if (!len_as_arg)
  3267. len_arg = -1;
  3268. print_str_arg(s, data, size, event,
  3269. format, len_arg, arg);
  3270. arg = arg->next;
  3271. break;
  3272. default:
  3273. trace_seq_printf(s, ">%c<", *ptr);
  3274. }
  3275. } else
  3276. trace_seq_putc(s, *ptr);
  3277. }
  3278. if (args) {
  3279. free_args(args);
  3280. free(bprint_fmt);
  3281. }
  3282. }
  3283. /**
  3284. * pevent_data_lat_fmt - parse the data for the latency format
  3285. * @pevent: a handle to the pevent
  3286. * @s: the trace_seq to write to
  3287. * @data: the raw data to read from
  3288. * @size: currently unused.
  3289. *
  3290. * This parses out the Latency format (interrupts disabled,
  3291. * need rescheduling, in hard/soft interrupt, preempt count
  3292. * and lock depth) and places it into the trace_seq.
  3293. */
  3294. void pevent_data_lat_fmt(struct pevent *pevent,
  3295. struct trace_seq *s, struct pevent_record *record)
  3296. {
  3297. static int check_lock_depth = 1;
  3298. static int lock_depth_exists;
  3299. unsigned int lat_flags;
  3300. unsigned int pc;
  3301. int lock_depth;
  3302. int hardirq;
  3303. int softirq;
  3304. void *data = record->data;
  3305. lat_flags = parse_common_flags(pevent, data);
  3306. pc = parse_common_pc(pevent, data);
  3307. /* lock_depth may not always exist */
  3308. if (check_lock_depth) {
  3309. struct format_field *field;
  3310. struct event_format *event;
  3311. check_lock_depth = 0;
  3312. event = pevent->events[0];
  3313. field = pevent_find_common_field(event, "common_lock_depth");
  3314. if (field)
  3315. lock_depth_exists = 1;
  3316. }
  3317. if (lock_depth_exists)
  3318. lock_depth = parse_common_lock_depth(pevent, data);
  3319. hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
  3320. softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
  3321. trace_seq_printf(s, "%c%c%c",
  3322. (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  3323. (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  3324. 'X' : '.',
  3325. (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
  3326. 'N' : '.',
  3327. (hardirq && softirq) ? 'H' :
  3328. hardirq ? 'h' : softirq ? 's' : '.');
  3329. if (pc)
  3330. trace_seq_printf(s, "%x", pc);
  3331. else
  3332. trace_seq_putc(s, '.');
  3333. if (lock_depth_exists) {
  3334. if (lock_depth < 0)
  3335. trace_seq_putc(s, '.');
  3336. else
  3337. trace_seq_printf(s, "%d", lock_depth);
  3338. }
  3339. trace_seq_terminate(s);
  3340. }
  3341. /**
  3342. * pevent_data_type - parse out the given event type
  3343. * @pevent: a handle to the pevent
  3344. * @rec: the record to read from
  3345. *
  3346. * This returns the event id from the @rec.
  3347. */
  3348. int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
  3349. {
  3350. return trace_parse_common_type(pevent, rec->data);
  3351. }
  3352. /**
  3353. * pevent_data_event_from_type - find the event by a given type
  3354. * @pevent: a handle to the pevent
  3355. * @type: the type of the event.
  3356. *
  3357. * This returns the event form a given @type;
  3358. */
  3359. struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
  3360. {
  3361. return pevent_find_event(pevent, type);
  3362. }
  3363. /**
  3364. * pevent_data_pid - parse the PID from raw data
  3365. * @pevent: a handle to the pevent
  3366. * @rec: the record to parse
  3367. *
  3368. * This returns the PID from a raw data.
  3369. */
  3370. int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
  3371. {
  3372. return parse_common_pid(pevent, rec->data);
  3373. }
  3374. /**
  3375. * pevent_data_comm_from_pid - return the command line from PID
  3376. * @pevent: a handle to the pevent
  3377. * @pid: the PID of the task to search for
  3378. *
  3379. * This returns a pointer to the command line that has the given
  3380. * @pid.
  3381. */
  3382. const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
  3383. {
  3384. const char *comm;
  3385. comm = find_cmdline(pevent, pid);
  3386. return comm;
  3387. }
  3388. /**
  3389. * pevent_data_comm_from_pid - parse the data into the print format
  3390. * @s: the trace_seq to write to
  3391. * @event: the handle to the event
  3392. * @cpu: the cpu the event was recorded on
  3393. * @data: the raw data
  3394. * @size: the size of the raw data
  3395. * @nsecs: the timestamp of the event
  3396. *
  3397. * This parses the raw @data using the given @event information and
  3398. * writes the print format into the trace_seq.
  3399. */
  3400. void pevent_event_info(struct trace_seq *s, struct event_format *event,
  3401. struct pevent_record *record)
  3402. {
  3403. int print_pretty = 1;
  3404. if (event->pevent->print_raw)
  3405. print_event_fields(s, record->data, record->size, event);
  3406. else {
  3407. if (event->handler)
  3408. print_pretty = event->handler(s, record, event,
  3409. event->context);
  3410. if (print_pretty)
  3411. pretty_print(s, record->data, record->size, event);
  3412. }
  3413. trace_seq_terminate(s);
  3414. }
  3415. void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
  3416. struct pevent_record *record)
  3417. {
  3418. static char *spaces = " "; /* 20 spaces */
  3419. struct event_format *event;
  3420. unsigned long secs;
  3421. unsigned long usecs;
  3422. unsigned long nsecs;
  3423. const char *comm;
  3424. void *data = record->data;
  3425. int type;
  3426. int pid;
  3427. int len;
  3428. int p;
  3429. secs = record->ts / NSECS_PER_SEC;
  3430. nsecs = record->ts - secs * NSECS_PER_SEC;
  3431. if (record->size < 0) {
  3432. do_warning("ug! negative record size %d", record->size);
  3433. return;
  3434. }
  3435. type = trace_parse_common_type(pevent, data);
  3436. event = pevent_find_event(pevent, type);
  3437. if (!event) {
  3438. do_warning("ug! no event found for type %d", type);
  3439. return;
  3440. }
  3441. pid = parse_common_pid(pevent, data);
  3442. comm = find_cmdline(pevent, pid);
  3443. if (pevent->latency_format) {
  3444. trace_seq_printf(s, "%8.8s-%-5d %3d",
  3445. comm, pid, record->cpu);
  3446. pevent_data_lat_fmt(pevent, s, record);
  3447. } else
  3448. trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
  3449. if (pevent->flags & PEVENT_NSEC_OUTPUT) {
  3450. usecs = nsecs;
  3451. p = 9;
  3452. } else {
  3453. usecs = (nsecs + 500) / NSECS_PER_USEC;
  3454. p = 6;
  3455. }
  3456. trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
  3457. /* Space out the event names evenly. */
  3458. len = strlen(event->name);
  3459. if (len < 20)
  3460. trace_seq_printf(s, "%.*s", 20 - len, spaces);
  3461. pevent_event_info(s, event, record);
  3462. }
  3463. static int events_id_cmp(const void *a, const void *b)
  3464. {
  3465. struct event_format * const * ea = a;
  3466. struct event_format * const * eb = b;
  3467. if ((*ea)->id < (*eb)->id)
  3468. return -1;
  3469. if ((*ea)->id > (*eb)->id)
  3470. return 1;
  3471. return 0;
  3472. }
  3473. static int events_name_cmp(const void *a, const void *b)
  3474. {
  3475. struct event_format * const * ea = a;
  3476. struct event_format * const * eb = b;
  3477. int res;
  3478. res = strcmp((*ea)->name, (*eb)->name);
  3479. if (res)
  3480. return res;
  3481. res = strcmp((*ea)->system, (*eb)->system);
  3482. if (res)
  3483. return res;
  3484. return events_id_cmp(a, b);
  3485. }
  3486. static int events_system_cmp(const void *a, const void *b)
  3487. {
  3488. struct event_format * const * ea = a;
  3489. struct event_format * const * eb = b;
  3490. int res;
  3491. res = strcmp((*ea)->system, (*eb)->system);
  3492. if (res)
  3493. return res;
  3494. res = strcmp((*ea)->name, (*eb)->name);
  3495. if (res)
  3496. return res;
  3497. return events_id_cmp(a, b);
  3498. }
  3499. struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
  3500. {
  3501. struct event_format **events;
  3502. int (*sort)(const void *a, const void *b);
  3503. events = pevent->sort_events;
  3504. if (events && pevent->last_type == sort_type)
  3505. return events;
  3506. if (!events) {
  3507. events = malloc(sizeof(*events) * (pevent->nr_events + 1));
  3508. if (!events)
  3509. return NULL;
  3510. memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
  3511. events[pevent->nr_events] = NULL;
  3512. pevent->sort_events = events;
  3513. /* the internal events are sorted by id */
  3514. if (sort_type == EVENT_SORT_ID) {
  3515. pevent->last_type = sort_type;
  3516. return events;
  3517. }
  3518. }
  3519. switch (sort_type) {
  3520. case EVENT_SORT_ID:
  3521. sort = events_id_cmp;
  3522. break;
  3523. case EVENT_SORT_NAME:
  3524. sort = events_name_cmp;
  3525. break;
  3526. case EVENT_SORT_SYSTEM:
  3527. sort = events_system_cmp;
  3528. break;
  3529. default:
  3530. return events;
  3531. }
  3532. qsort(events, pevent->nr_events, sizeof(*events), sort);
  3533. pevent->last_type = sort_type;
  3534. return events;
  3535. }
  3536. static struct format_field **
  3537. get_event_fields(const char *type, const char *name,
  3538. int count, struct format_field *list)
  3539. {
  3540. struct format_field **fields;
  3541. struct format_field *field;
  3542. int i = 0;
  3543. fields = malloc_or_die(sizeof(*fields) * (count + 1));
  3544. for (field = list; field; field = field->next) {
  3545. fields[i++] = field;
  3546. if (i == count + 1) {
  3547. do_warning("event %s has more %s fields than specified",
  3548. name, type);
  3549. i--;
  3550. break;
  3551. }
  3552. }
  3553. if (i != count)
  3554. do_warning("event %s has less %s fields than specified",
  3555. name, type);
  3556. fields[i] = NULL;
  3557. return fields;
  3558. }
  3559. /**
  3560. * pevent_event_common_fields - return a list of common fields for an event
  3561. * @event: the event to return the common fields of.
  3562. *
  3563. * Returns an allocated array of fields. The last item in the array is NULL.
  3564. * The array must be freed with free().
  3565. */
  3566. struct format_field **pevent_event_common_fields(struct event_format *event)
  3567. {
  3568. return get_event_fields("common", event->name,
  3569. event->format.nr_common,
  3570. event->format.common_fields);
  3571. }
  3572. /**
  3573. * pevent_event_fields - return a list of event specific fields for an event
  3574. * @event: the event to return the fields of.
  3575. *
  3576. * Returns an allocated array of fields. The last item in the array is NULL.
  3577. * The array must be freed with free().
  3578. */
  3579. struct format_field **pevent_event_fields(struct event_format *event)
  3580. {
  3581. return get_event_fields("event", event->name,
  3582. event->format.nr_fields,
  3583. event->format.fields);
  3584. }
  3585. static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
  3586. {
  3587. trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
  3588. if (field->next) {
  3589. trace_seq_puts(s, ", ");
  3590. print_fields(s, field->next);
  3591. }
  3592. }
  3593. /* for debugging */
  3594. static void print_args(struct print_arg *args)
  3595. {
  3596. int print_paren = 1;
  3597. struct trace_seq s;
  3598. switch (args->type) {
  3599. case PRINT_NULL:
  3600. printf("null");
  3601. break;
  3602. case PRINT_ATOM:
  3603. printf("%s", args->atom.atom);
  3604. break;
  3605. case PRINT_FIELD:
  3606. printf("REC->%s", args->field.name);
  3607. break;
  3608. case PRINT_FLAGS:
  3609. printf("__print_flags(");
  3610. print_args(args->flags.field);
  3611. printf(", %s, ", args->flags.delim);
  3612. trace_seq_init(&s);
  3613. print_fields(&s, args->flags.flags);
  3614. trace_seq_do_printf(&s);
  3615. trace_seq_destroy(&s);
  3616. printf(")");
  3617. break;
  3618. case PRINT_SYMBOL:
  3619. printf("__print_symbolic(");
  3620. print_args(args->symbol.field);
  3621. printf(", ");
  3622. trace_seq_init(&s);
  3623. print_fields(&s, args->symbol.symbols);
  3624. trace_seq_do_printf(&s);
  3625. trace_seq_destroy(&s);
  3626. printf(")");
  3627. break;
  3628. case PRINT_STRING:
  3629. case PRINT_BSTRING:
  3630. printf("__get_str(%s)", args->string.string);
  3631. break;
  3632. case PRINT_TYPE:
  3633. printf("(%s)", args->typecast.type);
  3634. print_args(args->typecast.item);
  3635. break;
  3636. case PRINT_OP:
  3637. if (strcmp(args->op.op, ":") == 0)
  3638. print_paren = 0;
  3639. if (print_paren)
  3640. printf("(");
  3641. print_args(args->op.left);
  3642. printf(" %s ", args->op.op);
  3643. print_args(args->op.right);
  3644. if (print_paren)
  3645. printf(")");
  3646. break;
  3647. default:
  3648. /* we should warn... */
  3649. return;
  3650. }
  3651. if (args->next) {
  3652. printf("\n");
  3653. print_args(args->next);
  3654. }
  3655. }
  3656. static void parse_header_field(const char *field,
  3657. int *offset, int *size, int mandatory)
  3658. {
  3659. unsigned long long save_input_buf_ptr;
  3660. unsigned long long save_input_buf_siz;
  3661. char *token;
  3662. int type;
  3663. save_input_buf_ptr = input_buf_ptr;
  3664. save_input_buf_siz = input_buf_siz;
  3665. if (read_expected(EVENT_ITEM, "field") < 0)
  3666. return;
  3667. if (read_expected(EVENT_OP, ":") < 0)
  3668. return;
  3669. /* type */
  3670. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3671. goto fail;
  3672. free_token(token);
  3673. /*
  3674. * If this is not a mandatory field, then test it first.
  3675. */
  3676. if (mandatory) {
  3677. if (read_expected(EVENT_ITEM, field) < 0)
  3678. return;
  3679. } else {
  3680. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3681. goto fail;
  3682. if (strcmp(token, field) != 0)
  3683. goto discard;
  3684. free_token(token);
  3685. }
  3686. if (read_expected(EVENT_OP, ";") < 0)
  3687. return;
  3688. if (read_expected(EVENT_ITEM, "offset") < 0)
  3689. return;
  3690. if (read_expected(EVENT_OP, ":") < 0)
  3691. return;
  3692. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3693. goto fail;
  3694. *offset = atoi(token);
  3695. free_token(token);
  3696. if (read_expected(EVENT_OP, ";") < 0)
  3697. return;
  3698. if (read_expected(EVENT_ITEM, "size") < 0)
  3699. return;
  3700. if (read_expected(EVENT_OP, ":") < 0)
  3701. return;
  3702. if (read_expect_type(EVENT_ITEM, &token) < 0)
  3703. goto fail;
  3704. *size = atoi(token);
  3705. free_token(token);
  3706. if (read_expected(EVENT_OP, ";") < 0)
  3707. return;
  3708. type = read_token(&token);
  3709. if (type != EVENT_NEWLINE) {
  3710. /* newer versions of the kernel have a "signed" type */
  3711. if (type != EVENT_ITEM)
  3712. goto fail;
  3713. if (strcmp(token, "signed") != 0)
  3714. goto fail;
  3715. free_token(token);
  3716. if (read_expected(EVENT_OP, ":") < 0)
  3717. return;
  3718. if (read_expect_type(EVENT_ITEM, &token))
  3719. goto fail;
  3720. free_token(token);
  3721. if (read_expected(EVENT_OP, ";") < 0)
  3722. return;
  3723. if (read_expect_type(EVENT_NEWLINE, &token))
  3724. goto fail;
  3725. }
  3726. fail:
  3727. free_token(token);
  3728. return;
  3729. discard:
  3730. input_buf_ptr = save_input_buf_ptr;
  3731. input_buf_siz = save_input_buf_siz;
  3732. *offset = 0;
  3733. *size = 0;
  3734. free_token(token);
  3735. }
  3736. /**
  3737. * pevent_parse_header_page - parse the data stored in the header page
  3738. * @pevent: the handle to the pevent
  3739. * @buf: the buffer storing the header page format string
  3740. * @size: the size of @buf
  3741. * @long_size: the long size to use if there is no header
  3742. *
  3743. * This parses the header page format for information on the
  3744. * ring buffer used. The @buf should be copied from
  3745. *
  3746. * /sys/kernel/debug/tracing/events/header_page
  3747. */
  3748. int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
  3749. int long_size)
  3750. {
  3751. int ignore;
  3752. if (!size) {
  3753. /*
  3754. * Old kernels did not have header page info.
  3755. * Sorry but we just use what we find here in user space.
  3756. */
  3757. pevent->header_page_ts_size = sizeof(long long);
  3758. pevent->header_page_size_size = long_size;
  3759. pevent->header_page_data_offset = sizeof(long long) + long_size;
  3760. pevent->old_format = 1;
  3761. return -1;
  3762. }
  3763. init_input_buf(buf, size);
  3764. parse_header_field("timestamp", &pevent->header_page_ts_offset,
  3765. &pevent->header_page_ts_size, 1);
  3766. parse_header_field("commit", &pevent->header_page_size_offset,
  3767. &pevent->header_page_size_size, 1);
  3768. parse_header_field("overwrite", &pevent->header_page_overwrite,
  3769. &ignore, 0);
  3770. parse_header_field("data", &pevent->header_page_data_offset,
  3771. &pevent->header_page_data_size, 1);
  3772. return 0;
  3773. }
  3774. static int event_matches(struct event_format *event,
  3775. int id, const char *sys_name,
  3776. const char *event_name)
  3777. {
  3778. if (id >= 0 && id != event->id)
  3779. return 0;
  3780. if (event_name && (strcmp(event_name, event->name) != 0))
  3781. return 0;
  3782. if (sys_name && (strcmp(sys_name, event->system) != 0))
  3783. return 0;
  3784. return 1;
  3785. }
  3786. static void free_handler(struct event_handler *handle)
  3787. {
  3788. free((void *)handle->sys_name);
  3789. free((void *)handle->event_name);
  3790. free(handle);
  3791. }
  3792. static int find_event_handle(struct pevent *pevent, struct event_format *event)
  3793. {
  3794. struct event_handler *handle, **next;
  3795. for (next = &pevent->handlers; *next;
  3796. next = &(*next)->next) {
  3797. handle = *next;
  3798. if (event_matches(event, handle->id,
  3799. handle->sys_name,
  3800. handle->event_name))
  3801. break;
  3802. }
  3803. if (!(*next))
  3804. return 0;
  3805. pr_stat("overriding event (%d) %s:%s with new print handler",
  3806. event->id, event->system, event->name);
  3807. event->handler = handle->func;
  3808. event->context = handle->context;
  3809. *next = handle->next;
  3810. free_handler(handle);
  3811. return 1;
  3812. }
  3813. /**
  3814. * pevent_parse_event - parse the event format
  3815. * @pevent: the handle to the pevent
  3816. * @buf: the buffer storing the event format string
  3817. * @size: the size of @buf
  3818. * @sys: the system the event belongs to
  3819. *
  3820. * This parses the event format and creates an event structure
  3821. * to quickly parse raw data for a given event.
  3822. *
  3823. * These files currently come from:
  3824. *
  3825. * /sys/kernel/debug/tracing/events/.../.../format
  3826. */
  3827. int pevent_parse_event(struct pevent *pevent,
  3828. const char *buf, unsigned long size,
  3829. const char *sys)
  3830. {
  3831. struct event_format *event;
  3832. int ret;
  3833. init_input_buf(buf, size);
  3834. event = alloc_event();
  3835. if (!event)
  3836. return -ENOMEM;
  3837. event->name = event_read_name();
  3838. if (!event->name) {
  3839. /* Bad event? */
  3840. free(event);
  3841. return -1;
  3842. }
  3843. if (strcmp(sys, "ftrace") == 0) {
  3844. event->flags |= EVENT_FL_ISFTRACE;
  3845. if (strcmp(event->name, "bprint") == 0)
  3846. event->flags |= EVENT_FL_ISBPRINT;
  3847. }
  3848. event->id = event_read_id();
  3849. if (event->id < 0)
  3850. die("failed to read event id");
  3851. event->system = strdup(sys);
  3852. /* Add pevent to event so that it can be referenced */
  3853. event->pevent = pevent;
  3854. ret = event_read_format(event);
  3855. if (ret < 0) {
  3856. do_warning("failed to read event format for %s", event->name);
  3857. goto event_failed;
  3858. }
  3859. /*
  3860. * If the event has an override, don't print warnings if the event
  3861. * print format fails to parse.
  3862. */
  3863. if (find_event_handle(pevent, event))
  3864. show_warning = 0;
  3865. ret = event_read_print(event);
  3866. if (ret < 0) {
  3867. do_warning("failed to read event print fmt for %s",
  3868. event->name);
  3869. show_warning = 1;
  3870. goto event_failed;
  3871. }
  3872. show_warning = 1;
  3873. add_event(pevent, event);
  3874. if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
  3875. struct format_field *field;
  3876. struct print_arg *arg, **list;
  3877. /* old ftrace had no args */
  3878. list = &event->print_fmt.args;
  3879. for (field = event->format.fields; field; field = field->next) {
  3880. arg = alloc_arg();
  3881. *list = arg;
  3882. list = &arg->next;
  3883. arg->type = PRINT_FIELD;
  3884. arg->field.name = strdup(field->name);
  3885. arg->field.field = field;
  3886. }
  3887. return 0;
  3888. }
  3889. #define PRINT_ARGS 0
  3890. if (PRINT_ARGS && event->print_fmt.args)
  3891. print_args(event->print_fmt.args);
  3892. return 0;
  3893. event_failed:
  3894. event->flags |= EVENT_FL_FAILED;
  3895. /* still add it even if it failed */
  3896. add_event(pevent, event);
  3897. return -1;
  3898. }
  3899. int get_field_val(struct trace_seq *s, struct format_field *field,
  3900. const char *name, struct pevent_record *record,
  3901. unsigned long long *val, int err)
  3902. {
  3903. if (!field) {
  3904. if (err)
  3905. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  3906. return -1;
  3907. }
  3908. if (pevent_read_number_field(field, record->data, val)) {
  3909. if (err)
  3910. trace_seq_printf(s, " %s=INVALID", name);
  3911. return -1;
  3912. }
  3913. return 0;
  3914. }
  3915. /**
  3916. * pevent_get_field_raw - return the raw pointer into the data field
  3917. * @s: The seq to print to on error
  3918. * @event: the event that the field is for
  3919. * @name: The name of the field
  3920. * @record: The record with the field name.
  3921. * @len: place to store the field length.
  3922. * @err: print default error if failed.
  3923. *
  3924. * Returns a pointer into record->data of the field and places
  3925. * the length of the field in @len.
  3926. *
  3927. * On failure, it returns NULL.
  3928. */
  3929. void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
  3930. const char *name, struct pevent_record *record,
  3931. int *len, int err)
  3932. {
  3933. struct format_field *field;
  3934. void *data = record->data;
  3935. unsigned offset;
  3936. int dummy;
  3937. if (!event)
  3938. return NULL;
  3939. field = pevent_find_field(event, name);
  3940. if (!field) {
  3941. if (err)
  3942. trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
  3943. return NULL;
  3944. }
  3945. /* Allow @len to be NULL */
  3946. if (!len)
  3947. len = &dummy;
  3948. offset = field->offset;
  3949. if (field->flags & FIELD_IS_DYNAMIC) {
  3950. offset = pevent_read_number(event->pevent,
  3951. data + offset, field->size);
  3952. *len = offset >> 16;
  3953. offset &= 0xffff;
  3954. } else
  3955. *len = field->size;
  3956. return data + offset;
  3957. }
  3958. /**
  3959. * pevent_get_field_val - find a field and return its value
  3960. * @s: The seq to print to on error
  3961. * @event: the event that the field is for
  3962. * @name: The name of the field
  3963. * @record: The record with the field name.
  3964. * @val: place to store the value of the field.
  3965. * @err: print default error if failed.
  3966. *
  3967. * Returns 0 on success -1 on field not found.
  3968. */
  3969. int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
  3970. const char *name, struct pevent_record *record,
  3971. unsigned long long *val, int err)
  3972. {
  3973. struct format_field *field;
  3974. if (!event)
  3975. return -1;
  3976. field = pevent_find_field(event, name);
  3977. return get_field_val(s, field, name, record, val, err);
  3978. }
  3979. /**
  3980. * pevent_get_common_field_val - find a common field and return its value
  3981. * @s: The seq to print to on error
  3982. * @event: the event that the field is for
  3983. * @name: The name of the field
  3984. * @record: The record with the field name.
  3985. * @val: place to store the value of the field.
  3986. * @err: print default error if failed.
  3987. *
  3988. * Returns 0 on success -1 on field not found.
  3989. */
  3990. int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
  3991. const char *name, struct pevent_record *record,
  3992. unsigned long long *val, int err)
  3993. {
  3994. struct format_field *field;
  3995. if (!event)
  3996. return -1;
  3997. field = pevent_find_common_field(event, name);
  3998. return get_field_val(s, field, name, record, val, err);
  3999. }
  4000. /**
  4001. * pevent_get_any_field_val - find a any field and return its value
  4002. * @s: The seq to print to on error
  4003. * @event: the event that the field is for
  4004. * @name: The name of the field
  4005. * @record: The record with the field name.
  4006. * @val: place to store the value of the field.
  4007. * @err: print default error if failed.
  4008. *
  4009. * Returns 0 on success -1 on field not found.
  4010. */
  4011. int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
  4012. const char *name, struct pevent_record *record,
  4013. unsigned long long *val, int err)
  4014. {
  4015. struct format_field *field;
  4016. if (!event)
  4017. return -1;
  4018. field = pevent_find_any_field(event, name);
  4019. return get_field_val(s, field, name, record, val, err);
  4020. }
  4021. /**
  4022. * pevent_print_num_field - print a field and a format
  4023. * @s: The seq to print to
  4024. * @fmt: The printf format to print the field with.
  4025. * @event: the event that the field is for
  4026. * @name: The name of the field
  4027. * @record: The record with the field name.
  4028. * @err: print default error if failed.
  4029. *
  4030. * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
  4031. */
  4032. int pevent_print_num_field(struct trace_seq *s, const char *fmt,
  4033. struct event_format *event, const char *name,
  4034. struct pevent_record *record, int err)
  4035. {
  4036. struct format_field *field = pevent_find_field(event, name);
  4037. unsigned long long val;
  4038. if (!field)
  4039. goto failed;
  4040. if (pevent_read_number_field(field, record->data, &val))
  4041. goto failed;
  4042. return trace_seq_printf(s, fmt, val);
  4043. failed:
  4044. if (err)
  4045. trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
  4046. return -1;
  4047. }
  4048. static void free_func_handle(struct pevent_function_handler *func)
  4049. {
  4050. struct pevent_func_params *params;
  4051. free(func->name);
  4052. while (func->params) {
  4053. params = func->params;
  4054. func->params = params->next;
  4055. free(params);
  4056. }
  4057. free(func);
  4058. }
  4059. /**
  4060. * pevent_register_print_function - register a helper function
  4061. * @pevent: the handle to the pevent
  4062. * @func: the function to process the helper function
  4063. * @name: the name of the helper function
  4064. * @parameters: A list of enum pevent_func_arg_type
  4065. *
  4066. * Some events may have helper functions in the print format arguments.
  4067. * This allows a plugin to dynmically create a way to process one
  4068. * of these functions.
  4069. *
  4070. * The @parameters is a variable list of pevent_func_arg_type enums that
  4071. * must end with PEVENT_FUNC_ARG_VOID.
  4072. */
  4073. int pevent_register_print_function(struct pevent *pevent,
  4074. pevent_func_handler func,
  4075. enum pevent_func_arg_type ret_type,
  4076. char *name, ...)
  4077. {
  4078. struct pevent_function_handler *func_handle;
  4079. struct pevent_func_params **next_param;
  4080. struct pevent_func_params *param;
  4081. enum pevent_func_arg_type type;
  4082. va_list ap;
  4083. func_handle = find_func_handler(pevent, name);
  4084. if (func_handle) {
  4085. /*
  4086. * This is most like caused by the users own
  4087. * plugins updating the function. This overrides the
  4088. * system defaults.
  4089. */
  4090. pr_stat("override of function helper '%s'", name);
  4091. remove_func_handler(pevent, name);
  4092. }
  4093. func_handle = malloc_or_die(sizeof(*func_handle));
  4094. memset(func_handle, 0, sizeof(*func_handle));
  4095. func_handle->ret_type = ret_type;
  4096. func_handle->name = strdup(name);
  4097. func_handle->func = func;
  4098. if (!func_handle->name)
  4099. die("Failed to allocate function name");
  4100. next_param = &(func_handle->params);
  4101. va_start(ap, name);
  4102. for (;;) {
  4103. type = va_arg(ap, enum pevent_func_arg_type);
  4104. if (type == PEVENT_FUNC_ARG_VOID)
  4105. break;
  4106. if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
  4107. warning("Invalid argument type %d", type);
  4108. goto out_free;
  4109. }
  4110. param = malloc_or_die(sizeof(*param));
  4111. param->type = type;
  4112. param->next = NULL;
  4113. *next_param = param;
  4114. next_param = &(param->next);
  4115. func_handle->nr_args++;
  4116. }
  4117. va_end(ap);
  4118. func_handle->next = pevent->func_handlers;
  4119. pevent->func_handlers = func_handle;
  4120. return 0;
  4121. out_free:
  4122. va_end(ap);
  4123. free_func_handle(func_handle);
  4124. return -1;
  4125. }
  4126. /**
  4127. * pevent_register_event_handle - register a way to parse an event
  4128. * @pevent: the handle to the pevent
  4129. * @id: the id of the event to register
  4130. * @sys_name: the system name the event belongs to
  4131. * @event_name: the name of the event
  4132. * @func: the function to call to parse the event information
  4133. *
  4134. * This function allows a developer to override the parsing of
  4135. * a given event. If for some reason the default print format
  4136. * is not sufficient, this function will register a function
  4137. * for an event to be used to parse the data instead.
  4138. *
  4139. * If @id is >= 0, then it is used to find the event.
  4140. * else @sys_name and @event_name are used.
  4141. */
  4142. int pevent_register_event_handler(struct pevent *pevent,
  4143. int id, char *sys_name, char *event_name,
  4144. pevent_event_handler_func func,
  4145. void *context)
  4146. {
  4147. struct event_format *event;
  4148. struct event_handler *handle;
  4149. if (id >= 0) {
  4150. /* search by id */
  4151. event = pevent_find_event(pevent, id);
  4152. if (!event)
  4153. goto not_found;
  4154. if (event_name && (strcmp(event_name, event->name) != 0))
  4155. goto not_found;
  4156. if (sys_name && (strcmp(sys_name, event->system) != 0))
  4157. goto not_found;
  4158. } else {
  4159. event = pevent_find_event_by_name(pevent, sys_name, event_name);
  4160. if (!event)
  4161. goto not_found;
  4162. }
  4163. pr_stat("overriding event (%d) %s:%s with new print handler",
  4164. event->id, event->system, event->name);
  4165. event->handler = func;
  4166. event->context = context;
  4167. return 0;
  4168. not_found:
  4169. /* Save for later use. */
  4170. handle = malloc_or_die(sizeof(*handle));
  4171. memset(handle, 0, sizeof(*handle));
  4172. handle->id = id;
  4173. if (event_name)
  4174. handle->event_name = strdup(event_name);
  4175. if (sys_name)
  4176. handle->sys_name = strdup(sys_name);
  4177. handle->func = func;
  4178. handle->next = pevent->handlers;
  4179. pevent->handlers = handle;
  4180. handle->context = context;
  4181. return -1;
  4182. }
  4183. /**
  4184. * pevent_alloc - create a pevent handle
  4185. */
  4186. struct pevent *pevent_alloc(void)
  4187. {
  4188. struct pevent *pevent;
  4189. pevent = malloc(sizeof(*pevent));
  4190. if (!pevent)
  4191. return NULL;
  4192. memset(pevent, 0, sizeof(*pevent));
  4193. pevent->ref_count = 1;
  4194. return pevent;
  4195. }
  4196. void pevent_ref(struct pevent *pevent)
  4197. {
  4198. pevent->ref_count++;
  4199. }
  4200. static void free_format_fields(struct format_field *field)
  4201. {
  4202. struct format_field *next;
  4203. while (field) {
  4204. next = field->next;
  4205. free(field->type);
  4206. free(field->name);
  4207. free(field);
  4208. field = next;
  4209. }
  4210. }
  4211. static void free_formats(struct format *format)
  4212. {
  4213. free_format_fields(format->common_fields);
  4214. free_format_fields(format->fields);
  4215. }
  4216. static void free_event(struct event_format *event)
  4217. {
  4218. free(event->name);
  4219. free(event->system);
  4220. free_formats(&event->format);
  4221. free(event->print_fmt.format);
  4222. free_args(event->print_fmt.args);
  4223. free(event);
  4224. }
  4225. /**
  4226. * pevent_free - free a pevent handle
  4227. * @pevent: the pevent handle to free
  4228. */
  4229. void pevent_free(struct pevent *pevent)
  4230. {
  4231. struct cmdline_list *cmdlist, *cmdnext;
  4232. struct func_list *funclist, *funcnext;
  4233. struct printk_list *printklist, *printknext;
  4234. struct pevent_function_handler *func_handler;
  4235. struct event_handler *handle;
  4236. int i;
  4237. if (!pevent)
  4238. return;
  4239. cmdlist = pevent->cmdlist;
  4240. funclist = pevent->funclist;
  4241. printklist = pevent->printklist;
  4242. pevent->ref_count--;
  4243. if (pevent->ref_count)
  4244. return;
  4245. if (pevent->cmdlines) {
  4246. for (i = 0; i < pevent->cmdline_count; i++)
  4247. free(pevent->cmdlines[i].comm);
  4248. free(pevent->cmdlines);
  4249. }
  4250. while (cmdlist) {
  4251. cmdnext = cmdlist->next;
  4252. free(cmdlist->comm);
  4253. free(cmdlist);
  4254. cmdlist = cmdnext;
  4255. }
  4256. if (pevent->func_map) {
  4257. for (i = 0; i < pevent->func_count; i++) {
  4258. free(pevent->func_map[i].func);
  4259. free(pevent->func_map[i].mod);
  4260. }
  4261. free(pevent->func_map);
  4262. }
  4263. while (funclist) {
  4264. funcnext = funclist->next;
  4265. free(funclist->func);
  4266. free(funclist->mod);
  4267. free(funclist);
  4268. funclist = funcnext;
  4269. }
  4270. while (pevent->func_handlers) {
  4271. func_handler = pevent->func_handlers;
  4272. pevent->func_handlers = func_handler->next;
  4273. free_func_handle(func_handler);
  4274. }
  4275. if (pevent->printk_map) {
  4276. for (i = 0; i < pevent->printk_count; i++)
  4277. free(pevent->printk_map[i].printk);
  4278. free(pevent->printk_map);
  4279. }
  4280. while (printklist) {
  4281. printknext = printklist->next;
  4282. free(printklist->printk);
  4283. free(printklist);
  4284. printklist = printknext;
  4285. }
  4286. for (i = 0; i < pevent->nr_events; i++)
  4287. free_event(pevent->events[i]);
  4288. while (pevent->handlers) {
  4289. handle = pevent->handlers;
  4290. pevent->handlers = handle->next;
  4291. free_handler(handle);
  4292. }
  4293. free(pevent->events);
  4294. free(pevent->sort_events);
  4295. free(pevent);
  4296. }
  4297. void pevent_unref(struct pevent *pevent)
  4298. {
  4299. pevent_free(pevent);
  4300. }