event-parse.c 148 KB

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