event-parse.c 107 KB

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