event-parse.c 135 KB

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