event-parse.c 140 KB

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