event-parse.c 113 KB

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