event-parse.c 125 KB

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