event-parse.c 118 KB

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