event-parse.c 126 KB

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