event-parse.c 138 KB

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