event-parse.c 146 KB

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