event-parse.c 110 KB

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