event-parse.c 146 KB

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