event-parse.c 122 KB

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