event-parse.c 111 KB

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