event-parse.c 140 KB

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