event-parse.c 118 KB

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