event-parse.c 144 KB

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