event-parse.c 116 KB

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