event-parse.c 142 KB

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