builtin-trace.c 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148
  1. /*
  2. * builtin-trace.c
  3. *
  4. * Builtin 'trace' command:
  5. *
  6. * Display a continuously updated trace of any workload, CPU, specific PID,
  7. * system wide, etc. Default format is loosely strace like, but any other
  8. * event may be specified using --event.
  9. *
  10. * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  11. *
  12. * Initially based on the 'trace' prototype by Thomas Gleixner:
  13. *
  14. * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
  15. */
  16. #include "util/record.h"
  17. #include <traceevent/event-parse.h>
  18. #include <api/fs/tracing_path.h>
  19. #include <bpf/bpf.h>
  20. #include "util/bpf_map.h"
  21. #include "util/rlimit.h"
  22. #include "builtin.h"
  23. #include "util/cgroup.h"
  24. #include "util/color.h"
  25. #include "util/config.h"
  26. #include "util/debug.h"
  27. #include "util/dso.h"
  28. #include "util/env.h"
  29. #include "util/event.h"
  30. #include "util/evsel.h"
  31. #include "util/evsel_fprintf.h"
  32. #include "util/synthetic-events.h"
  33. #include "util/evlist.h"
  34. #include "util/evswitch.h"
  35. #include "util/mmap.h"
  36. #include <subcmd/pager.h>
  37. #include <subcmd/exec-cmd.h>
  38. #include "util/machine.h"
  39. #include "util/map.h"
  40. #include "util/symbol.h"
  41. #include "util/path.h"
  42. #include "util/session.h"
  43. #include "util/thread.h"
  44. #include <subcmd/parse-options.h>
  45. #include "util/strlist.h"
  46. #include "util/intlist.h"
  47. #include "util/thread_map.h"
  48. #include "util/stat.h"
  49. #include "util/tool.h"
  50. #include "util/util.h"
  51. #include "trace/beauty/beauty.h"
  52. #include "trace-event.h"
  53. #include "util/parse-events.h"
  54. #include "util/bpf-loader.h"
  55. #include "callchain.h"
  56. #include "print_binary.h"
  57. #include "string2.h"
  58. #include "syscalltbl.h"
  59. #include "rb_resort.h"
  60. #include "../perf.h"
  61. #include <errno.h>
  62. #include <inttypes.h>
  63. #include <poll.h>
  64. #include <signal.h>
  65. #include <stdlib.h>
  66. #include <string.h>
  67. #include <linux/err.h>
  68. #include <linux/filter.h>
  69. #include <linux/kernel.h>
  70. #include <linux/random.h>
  71. #include <linux/stringify.h>
  72. #include <linux/time64.h>
  73. #include <linux/zalloc.h>
  74. #include <fcntl.h>
  75. #include <sys/sysmacros.h>
  76. #include <linux/ctype.h>
  77. #include <perf/mmap.h>
  78. #ifndef O_CLOEXEC
  79. # define O_CLOEXEC 02000000
  80. #endif
  81. #ifndef F_LINUX_SPECIFIC_BASE
  82. # define F_LINUX_SPECIFIC_BASE 1024
  83. #endif
  84. /*
  85. * strtoul: Go from a string to a value, i.e. for msr: MSR_FS_BASE to 0xc0000100
  86. */
  87. struct syscall_arg_fmt {
  88. size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  89. bool (*strtoul)(char *bf, size_t size, struct syscall_arg *arg, u64 *val);
  90. unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
  91. void *parm;
  92. const char *name;
  93. u16 nr_entries; // for arrays
  94. bool show_zero;
  95. };
  96. struct syscall_fmt {
  97. const char *name;
  98. const char *alias;
  99. struct {
  100. const char *sys_enter,
  101. *sys_exit;
  102. } bpf_prog_name;
  103. struct syscall_arg_fmt arg[6];
  104. u8 nr_args;
  105. bool errpid;
  106. bool timeout;
  107. bool hexret;
  108. };
  109. struct trace {
  110. struct perf_tool tool;
  111. struct syscalltbl *sctbl;
  112. struct {
  113. struct syscall *table;
  114. struct bpf_map *map;
  115. struct { // per syscall BPF_MAP_TYPE_PROG_ARRAY
  116. struct bpf_map *sys_enter,
  117. *sys_exit;
  118. } prog_array;
  119. struct {
  120. struct evsel *sys_enter,
  121. *sys_exit,
  122. *augmented;
  123. } events;
  124. struct bpf_program *unaugmented_prog;
  125. } syscalls;
  126. struct {
  127. struct bpf_map *map;
  128. } dump;
  129. struct record_opts opts;
  130. struct evlist *evlist;
  131. struct machine *host;
  132. struct thread *current;
  133. struct bpf_object *bpf_obj;
  134. struct cgroup *cgroup;
  135. u64 base_time;
  136. FILE *output;
  137. unsigned long nr_events;
  138. unsigned long nr_events_printed;
  139. unsigned long max_events;
  140. struct evswitch evswitch;
  141. struct strlist *ev_qualifier;
  142. struct {
  143. size_t nr;
  144. int *entries;
  145. } ev_qualifier_ids;
  146. struct {
  147. size_t nr;
  148. pid_t *entries;
  149. struct bpf_map *map;
  150. } filter_pids;
  151. double duration_filter;
  152. double runtime_ms;
  153. struct {
  154. u64 vfs_getname,
  155. proc_getname;
  156. } stats;
  157. unsigned int max_stack;
  158. unsigned int min_stack;
  159. int raw_augmented_syscalls_args_size;
  160. bool raw_augmented_syscalls;
  161. bool fd_path_disabled;
  162. bool sort_events;
  163. bool not_ev_qualifier;
  164. bool live;
  165. bool full_time;
  166. bool sched;
  167. bool multiple_threads;
  168. bool summary;
  169. bool summary_only;
  170. bool errno_summary;
  171. bool failure_only;
  172. bool show_comm;
  173. bool print_sample;
  174. bool show_tool_stats;
  175. bool trace_syscalls;
  176. bool libtraceevent_print;
  177. bool kernel_syscallchains;
  178. s16 args_alignment;
  179. bool show_tstamp;
  180. bool show_duration;
  181. bool show_zeros;
  182. bool show_arg_names;
  183. bool show_string_prefix;
  184. bool force;
  185. bool vfs_getname;
  186. int trace_pgfaults;
  187. char *perfconfig_events;
  188. struct {
  189. struct ordered_events data;
  190. u64 last;
  191. } oe;
  192. };
  193. struct tp_field {
  194. int offset;
  195. union {
  196. u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
  197. void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
  198. };
  199. };
  200. #define TP_UINT_FIELD(bits) \
  201. static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
  202. { \
  203. u##bits value; \
  204. memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
  205. return value; \
  206. }
  207. TP_UINT_FIELD(8);
  208. TP_UINT_FIELD(16);
  209. TP_UINT_FIELD(32);
  210. TP_UINT_FIELD(64);
  211. #define TP_UINT_FIELD__SWAPPED(bits) \
  212. static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
  213. { \
  214. u##bits value; \
  215. memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
  216. return bswap_##bits(value);\
  217. }
  218. TP_UINT_FIELD__SWAPPED(16);
  219. TP_UINT_FIELD__SWAPPED(32);
  220. TP_UINT_FIELD__SWAPPED(64);
  221. static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
  222. {
  223. field->offset = offset;
  224. switch (size) {
  225. case 1:
  226. field->integer = tp_field__u8;
  227. break;
  228. case 2:
  229. field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
  230. break;
  231. case 4:
  232. field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
  233. break;
  234. case 8:
  235. field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
  236. break;
  237. default:
  238. return -1;
  239. }
  240. return 0;
  241. }
  242. static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap)
  243. {
  244. return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
  245. }
  246. static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
  247. {
  248. return sample->raw_data + field->offset;
  249. }
  250. static int __tp_field__init_ptr(struct tp_field *field, int offset)
  251. {
  252. field->offset = offset;
  253. field->pointer = tp_field__ptr;
  254. return 0;
  255. }
  256. static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
  257. {
  258. return __tp_field__init_ptr(field, format_field->offset);
  259. }
  260. struct syscall_tp {
  261. struct tp_field id;
  262. union {
  263. struct tp_field args, ret;
  264. };
  265. };
  266. /*
  267. * The evsel->priv as used by 'perf trace'
  268. * sc: for raw_syscalls:sys_{enter,exit} and syscalls:sys_{enter,exit}_SYSCALLNAME
  269. * fmt: for all the other tracepoints
  270. */
  271. struct evsel_trace {
  272. struct syscall_tp sc;
  273. struct syscall_arg_fmt *fmt;
  274. };
  275. static struct evsel_trace *evsel_trace__new(void)
  276. {
  277. return zalloc(sizeof(struct evsel_trace));
  278. }
  279. static void evsel_trace__delete(struct evsel_trace *et)
  280. {
  281. if (et == NULL)
  282. return;
  283. zfree(&et->fmt);
  284. free(et);
  285. }
  286. /*
  287. * Used with raw_syscalls:sys_{enter,exit} and with the
  288. * syscalls:sys_{enter,exit}_SYSCALL tracepoints
  289. */
  290. static inline struct syscall_tp *__evsel__syscall_tp(struct evsel *evsel)
  291. {
  292. struct evsel_trace *et = evsel->priv;
  293. return &et->sc;
  294. }
  295. static struct syscall_tp *evsel__syscall_tp(struct evsel *evsel)
  296. {
  297. if (evsel->priv == NULL) {
  298. evsel->priv = evsel_trace__new();
  299. if (evsel->priv == NULL)
  300. return NULL;
  301. }
  302. return __evsel__syscall_tp(evsel);
  303. }
  304. /*
  305. * Used with all the other tracepoints.
  306. */
  307. static inline struct syscall_arg_fmt *__evsel__syscall_arg_fmt(struct evsel *evsel)
  308. {
  309. struct evsel_trace *et = evsel->priv;
  310. return et->fmt;
  311. }
  312. static struct syscall_arg_fmt *evsel__syscall_arg_fmt(struct evsel *evsel)
  313. {
  314. struct evsel_trace *et = evsel->priv;
  315. if (evsel->priv == NULL) {
  316. et = evsel->priv = evsel_trace__new();
  317. if (et == NULL)
  318. return NULL;
  319. }
  320. if (et->fmt == NULL) {
  321. et->fmt = calloc(evsel->tp_format->format.nr_fields, sizeof(struct syscall_arg_fmt));
  322. if (et->fmt == NULL)
  323. goto out_delete;
  324. }
  325. return __evsel__syscall_arg_fmt(evsel);
  326. out_delete:
  327. evsel_trace__delete(evsel->priv);
  328. evsel->priv = NULL;
  329. return NULL;
  330. }
  331. static int evsel__init_tp_uint_field(struct evsel *evsel, struct tp_field *field, const char *name)
  332. {
  333. struct tep_format_field *format_field = evsel__field(evsel, name);
  334. if (format_field == NULL)
  335. return -1;
  336. return tp_field__init_uint(field, format_field, evsel->needs_swap);
  337. }
  338. #define perf_evsel__init_sc_tp_uint_field(evsel, name) \
  339. ({ struct syscall_tp *sc = __evsel__syscall_tp(evsel);\
  340. evsel__init_tp_uint_field(evsel, &sc->name, #name); })
  341. static int evsel__init_tp_ptr_field(struct evsel *evsel, struct tp_field *field, const char *name)
  342. {
  343. struct tep_format_field *format_field = evsel__field(evsel, name);
  344. if (format_field == NULL)
  345. return -1;
  346. return tp_field__init_ptr(field, format_field);
  347. }
  348. #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
  349. ({ struct syscall_tp *sc = __evsel__syscall_tp(evsel);\
  350. evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
  351. static void evsel__delete_priv(struct evsel *evsel)
  352. {
  353. zfree(&evsel->priv);
  354. evsel__delete(evsel);
  355. }
  356. static int evsel__init_syscall_tp(struct evsel *evsel)
  357. {
  358. struct syscall_tp *sc = evsel__syscall_tp(evsel);
  359. if (sc != NULL) {
  360. if (evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
  361. evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
  362. return -ENOENT;
  363. return 0;
  364. }
  365. return -ENOMEM;
  366. }
  367. static int evsel__init_augmented_syscall_tp(struct evsel *evsel, struct evsel *tp)
  368. {
  369. struct syscall_tp *sc = evsel__syscall_tp(evsel);
  370. if (sc != NULL) {
  371. struct tep_format_field *syscall_id = evsel__field(tp, "id");
  372. if (syscall_id == NULL)
  373. syscall_id = evsel__field(tp, "__syscall_nr");
  374. if (syscall_id == NULL ||
  375. __tp_field__init_uint(&sc->id, syscall_id->size, syscall_id->offset, evsel->needs_swap))
  376. return -EINVAL;
  377. return 0;
  378. }
  379. return -ENOMEM;
  380. }
  381. static int evsel__init_augmented_syscall_tp_args(struct evsel *evsel)
  382. {
  383. struct syscall_tp *sc = __evsel__syscall_tp(evsel);
  384. return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
  385. }
  386. static int evsel__init_augmented_syscall_tp_ret(struct evsel *evsel)
  387. {
  388. struct syscall_tp *sc = __evsel__syscall_tp(evsel);
  389. return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
  390. }
  391. static int evsel__init_raw_syscall_tp(struct evsel *evsel, void *handler)
  392. {
  393. if (evsel__syscall_tp(evsel) != NULL) {
  394. if (perf_evsel__init_sc_tp_uint_field(evsel, id))
  395. return -ENOENT;
  396. evsel->handler = handler;
  397. return 0;
  398. }
  399. return -ENOMEM;
  400. }
  401. static struct evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
  402. {
  403. struct evsel *evsel = evsel__newtp("raw_syscalls", direction);
  404. /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
  405. if (IS_ERR(evsel))
  406. evsel = evsel__newtp("syscalls", direction);
  407. if (IS_ERR(evsel))
  408. return NULL;
  409. if (evsel__init_raw_syscall_tp(evsel, handler))
  410. goto out_delete;
  411. return evsel;
  412. out_delete:
  413. evsel__delete_priv(evsel);
  414. return NULL;
  415. }
  416. #define perf_evsel__sc_tp_uint(evsel, name, sample) \
  417. ({ struct syscall_tp *fields = __evsel__syscall_tp(evsel); \
  418. fields->name.integer(&fields->name, sample); })
  419. #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
  420. ({ struct syscall_tp *fields = __evsel__syscall_tp(evsel); \
  421. fields->name.pointer(&fields->name, sample); })
  422. size_t strarray__scnprintf_suffix(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_suffix, int val)
  423. {
  424. int idx = val - sa->offset;
  425. if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
  426. size_t printed = scnprintf(bf, size, intfmt, val);
  427. if (show_suffix)
  428. printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
  429. return printed;
  430. }
  431. return scnprintf(bf, size, "%s%s", sa->entries[idx], show_suffix ? sa->prefix : "");
  432. }
  433. size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
  434. {
  435. int idx = val - sa->offset;
  436. if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
  437. size_t printed = scnprintf(bf, size, intfmt, val);
  438. if (show_prefix)
  439. printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
  440. return printed;
  441. }
  442. return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
  443. }
  444. static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
  445. const char *intfmt,
  446. struct syscall_arg *arg)
  447. {
  448. return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
  449. }
  450. static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
  451. struct syscall_arg *arg)
  452. {
  453. return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
  454. }
  455. #define SCA_STRARRAY syscall_arg__scnprintf_strarray
  456. bool syscall_arg__strtoul_strarray(char *bf, size_t size, struct syscall_arg *arg, u64 *ret)
  457. {
  458. return strarray__strtoul(arg->parm, bf, size, ret);
  459. }
  460. bool syscall_arg__strtoul_strarray_flags(char *bf, size_t size, struct syscall_arg *arg, u64 *ret)
  461. {
  462. return strarray__strtoul_flags(arg->parm, bf, size, ret);
  463. }
  464. bool syscall_arg__strtoul_strarrays(char *bf, size_t size, struct syscall_arg *arg, u64 *ret)
  465. {
  466. return strarrays__strtoul(arg->parm, bf, size, ret);
  467. }
  468. size_t syscall_arg__scnprintf_strarray_flags(char *bf, size_t size, struct syscall_arg *arg)
  469. {
  470. return strarray__scnprintf_flags(arg->parm, bf, size, arg->show_string_prefix, arg->val);
  471. }
  472. size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
  473. {
  474. size_t printed;
  475. int i;
  476. for (i = 0; i < sas->nr_entries; ++i) {
  477. struct strarray *sa = sas->entries[i];
  478. int idx = val - sa->offset;
  479. if (idx >= 0 && idx < sa->nr_entries) {
  480. if (sa->entries[idx] == NULL)
  481. break;
  482. return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
  483. }
  484. }
  485. printed = scnprintf(bf, size, intfmt, val);
  486. if (show_prefix)
  487. printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
  488. return printed;
  489. }
  490. bool strarray__strtoul(struct strarray *sa, char *bf, size_t size, u64 *ret)
  491. {
  492. int i;
  493. for (i = 0; i < sa->nr_entries; ++i) {
  494. if (sa->entries[i] && strncmp(sa->entries[i], bf, size) == 0 && sa->entries[i][size] == '\0') {
  495. *ret = sa->offset + i;
  496. return true;
  497. }
  498. }
  499. return false;
  500. }
  501. bool strarray__strtoul_flags(struct strarray *sa, char *bf, size_t size, u64 *ret)
  502. {
  503. u64 val = 0;
  504. char *tok = bf, *sep, *end;
  505. *ret = 0;
  506. while (size != 0) {
  507. int toklen = size;
  508. sep = memchr(tok, '|', size);
  509. if (sep != NULL) {
  510. size -= sep - tok + 1;
  511. end = sep - 1;
  512. while (end > tok && isspace(*end))
  513. --end;
  514. toklen = end - tok + 1;
  515. }
  516. while (isspace(*tok))
  517. ++tok;
  518. if (isalpha(*tok) || *tok == '_') {
  519. if (!strarray__strtoul(sa, tok, toklen, &val))
  520. return false;
  521. } else {
  522. bool is_hexa = tok[0] == 0 && (tok[1] = 'x' || tok[1] == 'X');
  523. val = strtoul(tok, NULL, is_hexa ? 16 : 0);
  524. }
  525. *ret |= (1 << (val - 1));
  526. if (sep == NULL)
  527. break;
  528. tok = sep + 1;
  529. }
  530. return true;
  531. }
  532. bool strarrays__strtoul(struct strarrays *sas, char *bf, size_t size, u64 *ret)
  533. {
  534. int i;
  535. for (i = 0; i < sas->nr_entries; ++i) {
  536. struct strarray *sa = sas->entries[i];
  537. if (strarray__strtoul(sa, bf, size, ret))
  538. return true;
  539. }
  540. return false;
  541. }
  542. size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
  543. struct syscall_arg *arg)
  544. {
  545. return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
  546. }
  547. #ifndef AT_FDCWD
  548. #define AT_FDCWD -100
  549. #endif
  550. static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
  551. struct syscall_arg *arg)
  552. {
  553. int fd = arg->val;
  554. const char *prefix = "AT_FD";
  555. if (fd == AT_FDCWD)
  556. return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
  557. return syscall_arg__scnprintf_fd(bf, size, arg);
  558. }
  559. #define SCA_FDAT syscall_arg__scnprintf_fd_at
  560. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  561. struct syscall_arg *arg);
  562. #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
  563. size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
  564. {
  565. return scnprintf(bf, size, "%#lx", arg->val);
  566. }
  567. size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
  568. {
  569. if (arg->val == 0)
  570. return scnprintf(bf, size, "NULL");
  571. return syscall_arg__scnprintf_hex(bf, size, arg);
  572. }
  573. size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
  574. {
  575. return scnprintf(bf, size, "%d", arg->val);
  576. }
  577. size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
  578. {
  579. return scnprintf(bf, size, "%ld", arg->val);
  580. }
  581. static size_t syscall_arg__scnprintf_char_array(char *bf, size_t size, struct syscall_arg *arg)
  582. {
  583. // XXX Hey, maybe for sched:sched_switch prev/next comm fields we can
  584. // fill missing comms using thread__set_comm()...
  585. // here or in a special syscall_arg__scnprintf_pid_sched_tp...
  586. return scnprintf(bf, size, "\"%-.*s\"", arg->fmt->nr_entries ?: arg->len, arg->val);
  587. }
  588. #define SCA_CHAR_ARRAY syscall_arg__scnprintf_char_array
  589. static const char *bpf_cmd[] = {
  590. "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
  591. "MAP_GET_NEXT_KEY", "PROG_LOAD",
  592. };
  593. static DEFINE_STRARRAY(bpf_cmd, "BPF_");
  594. static const char *fsmount_flags[] = {
  595. [1] = "CLOEXEC",
  596. };
  597. static DEFINE_STRARRAY(fsmount_flags, "FSMOUNT_");
  598. #include "trace/beauty/generated/fsconfig_arrays.c"
  599. static DEFINE_STRARRAY(fsconfig_cmds, "FSCONFIG_");
  600. static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
  601. static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
  602. static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
  603. static DEFINE_STRARRAY(itimers, "ITIMER_");
  604. static const char *keyctl_options[] = {
  605. "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
  606. "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
  607. "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
  608. "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
  609. "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
  610. };
  611. static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
  612. static const char *whences[] = { "SET", "CUR", "END",
  613. #ifdef SEEK_DATA
  614. "DATA",
  615. #endif
  616. #ifdef SEEK_HOLE
  617. "HOLE",
  618. #endif
  619. };
  620. static DEFINE_STRARRAY(whences, "SEEK_");
  621. static const char *fcntl_cmds[] = {
  622. "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
  623. "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
  624. "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
  625. "GETOWNER_UIDS",
  626. };
  627. static DEFINE_STRARRAY(fcntl_cmds, "F_");
  628. static const char *fcntl_linux_specific_cmds[] = {
  629. "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
  630. "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
  631. "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
  632. };
  633. static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
  634. static struct strarray *fcntl_cmds_arrays[] = {
  635. &strarray__fcntl_cmds,
  636. &strarray__fcntl_linux_specific_cmds,
  637. };
  638. static DEFINE_STRARRAYS(fcntl_cmds_arrays);
  639. static const char *rlimit_resources[] = {
  640. "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
  641. "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
  642. "RTTIME",
  643. };
  644. static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
  645. static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
  646. static DEFINE_STRARRAY(sighow, "SIG_");
  647. static const char *clockid[] = {
  648. "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
  649. "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
  650. "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
  651. };
  652. static DEFINE_STRARRAY(clockid, "CLOCK_");
  653. static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
  654. struct syscall_arg *arg)
  655. {
  656. bool show_prefix = arg->show_string_prefix;
  657. const char *suffix = "_OK";
  658. size_t printed = 0;
  659. int mode = arg->val;
  660. if (mode == F_OK) /* 0 */
  661. return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
  662. #define P_MODE(n) \
  663. if (mode & n##_OK) { \
  664. printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
  665. mode &= ~n##_OK; \
  666. }
  667. P_MODE(R);
  668. P_MODE(W);
  669. P_MODE(X);
  670. #undef P_MODE
  671. if (mode)
  672. printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
  673. return printed;
  674. }
  675. #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
  676. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  677. struct syscall_arg *arg);
  678. #define SCA_FILENAME syscall_arg__scnprintf_filename
  679. static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
  680. struct syscall_arg *arg)
  681. {
  682. bool show_prefix = arg->show_string_prefix;
  683. const char *prefix = "O_";
  684. int printed = 0, flags = arg->val;
  685. #define P_FLAG(n) \
  686. if (flags & O_##n) { \
  687. printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
  688. flags &= ~O_##n; \
  689. }
  690. P_FLAG(CLOEXEC);
  691. P_FLAG(NONBLOCK);
  692. #undef P_FLAG
  693. if (flags)
  694. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  695. return printed;
  696. }
  697. #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
  698. #ifndef GRND_NONBLOCK
  699. #define GRND_NONBLOCK 0x0001
  700. #endif
  701. #ifndef GRND_RANDOM
  702. #define GRND_RANDOM 0x0002
  703. #endif
  704. static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
  705. struct syscall_arg *arg)
  706. {
  707. bool show_prefix = arg->show_string_prefix;
  708. const char *prefix = "GRND_";
  709. int printed = 0, flags = arg->val;
  710. #define P_FLAG(n) \
  711. if (flags & GRND_##n) { \
  712. printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
  713. flags &= ~GRND_##n; \
  714. }
  715. P_FLAG(RANDOM);
  716. P_FLAG(NONBLOCK);
  717. #undef P_FLAG
  718. if (flags)
  719. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  720. return printed;
  721. }
  722. #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
  723. #define STRARRAY(name, array) \
  724. { .scnprintf = SCA_STRARRAY, \
  725. .strtoul = STUL_STRARRAY, \
  726. .parm = &strarray__##array, }
  727. #define STRARRAY_FLAGS(name, array) \
  728. { .scnprintf = SCA_STRARRAY_FLAGS, \
  729. .strtoul = STUL_STRARRAY_FLAGS, \
  730. .parm = &strarray__##array, }
  731. #include "trace/beauty/arch_errno_names.c"
  732. #include "trace/beauty/eventfd.c"
  733. #include "trace/beauty/futex_op.c"
  734. #include "trace/beauty/futex_val3.c"
  735. #include "trace/beauty/mmap.c"
  736. #include "trace/beauty/mode_t.c"
  737. #include "trace/beauty/msg_flags.c"
  738. #include "trace/beauty/open_flags.c"
  739. #include "trace/beauty/perf_event_open.c"
  740. #include "trace/beauty/pid.c"
  741. #include "trace/beauty/sched_policy.c"
  742. #include "trace/beauty/seccomp.c"
  743. #include "trace/beauty/signum.c"
  744. #include "trace/beauty/socket_type.c"
  745. #include "trace/beauty/waitid_options.c"
  746. static struct syscall_fmt syscall_fmts[] = {
  747. { .name = "access",
  748. .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
  749. { .name = "arch_prctl",
  750. .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
  751. [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
  752. { .name = "bind",
  753. .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ },
  754. [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ },
  755. [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, },
  756. { .name = "bpf",
  757. .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
  758. { .name = "brk", .hexret = true,
  759. .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
  760. { .name = "clock_gettime",
  761. .arg = { [0] = STRARRAY(clk_id, clockid), }, },
  762. { .name = "clone", .errpid = true, .nr_args = 5,
  763. .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
  764. [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
  765. [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
  766. [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
  767. [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
  768. { .name = "close",
  769. .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
  770. { .name = "connect",
  771. .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ },
  772. [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ },
  773. [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, },
  774. { .name = "epoll_ctl",
  775. .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
  776. { .name = "eventfd2",
  777. .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
  778. { .name = "fchmodat",
  779. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  780. { .name = "fchownat",
  781. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  782. { .name = "fcntl",
  783. .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
  784. .strtoul = STUL_STRARRAYS,
  785. .parm = &strarrays__fcntl_cmds_arrays,
  786. .show_zero = true, },
  787. [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
  788. { .name = "flock",
  789. .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
  790. { .name = "fsconfig",
  791. .arg = { [1] = STRARRAY(cmd, fsconfig_cmds), }, },
  792. { .name = "fsmount",
  793. .arg = { [1] = STRARRAY_FLAGS(flags, fsmount_flags),
  794. [2] = { .scnprintf = SCA_FSMOUNT_ATTR_FLAGS, /* attr_flags */ }, }, },
  795. { .name = "fspick",
  796. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  797. [1] = { .scnprintf = SCA_FILENAME, /* path */ },
  798. [2] = { .scnprintf = SCA_FSPICK_FLAGS, /* flags */ }, }, },
  799. { .name = "fstat", .alias = "newfstat", },
  800. { .name = "fstatat", .alias = "newfstatat", },
  801. { .name = "futex",
  802. .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
  803. [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
  804. { .name = "futimesat",
  805. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  806. { .name = "getitimer",
  807. .arg = { [0] = STRARRAY(which, itimers), }, },
  808. { .name = "getpid", .errpid = true, },
  809. { .name = "getpgid", .errpid = true, },
  810. { .name = "getppid", .errpid = true, },
  811. { .name = "getrandom",
  812. .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
  813. { .name = "getrlimit",
  814. .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
  815. { .name = "gettid", .errpid = true, },
  816. { .name = "ioctl",
  817. .arg = {
  818. #if defined(__i386__) || defined(__x86_64__)
  819. /*
  820. * FIXME: Make this available to all arches.
  821. */
  822. [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
  823. [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
  824. #else
  825. [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
  826. #endif
  827. { .name = "kcmp", .nr_args = 5,
  828. .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
  829. [1] = { .name = "pid2", .scnprintf = SCA_PID, },
  830. [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
  831. [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
  832. [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
  833. { .name = "keyctl",
  834. .arg = { [0] = STRARRAY(option, keyctl_options), }, },
  835. { .name = "kill",
  836. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  837. { .name = "linkat",
  838. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  839. { .name = "lseek",
  840. .arg = { [2] = STRARRAY(whence, whences), }, },
  841. { .name = "lstat", .alias = "newlstat", },
  842. { .name = "madvise",
  843. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  844. [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
  845. { .name = "mkdirat",
  846. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  847. { .name = "mknodat",
  848. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  849. { .name = "mmap", .hexret = true,
  850. /* The standard mmap maps to old_mmap on s390x */
  851. #if defined(__s390x__)
  852. .alias = "old_mmap",
  853. #endif
  854. .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
  855. [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */
  856. .strtoul = STUL_STRARRAY_FLAGS,
  857. .parm = &strarray__mmap_flags, },
  858. [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
  859. { .name = "mount",
  860. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
  861. [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
  862. .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
  863. { .name = "move_mount",
  864. .arg = { [0] = { .scnprintf = SCA_FDAT, /* from_dfd */ },
  865. [1] = { .scnprintf = SCA_FILENAME, /* from_pathname */ },
  866. [2] = { .scnprintf = SCA_FDAT, /* to_dfd */ },
  867. [3] = { .scnprintf = SCA_FILENAME, /* to_pathname */ },
  868. [4] = { .scnprintf = SCA_MOVE_MOUNT_FLAGS, /* flags */ }, }, },
  869. { .name = "mprotect",
  870. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  871. [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
  872. { .name = "mq_unlink",
  873. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
  874. { .name = "mremap", .hexret = true,
  875. .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
  876. { .name = "name_to_handle_at",
  877. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  878. { .name = "newfstatat",
  879. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  880. { .name = "open",
  881. .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  882. { .name = "open_by_handle_at",
  883. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  884. [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  885. { .name = "openat",
  886. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  887. [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  888. { .name = "perf_event_open",
  889. .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
  890. [3] = { .scnprintf = SCA_FD, /* group_fd */ },
  891. [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
  892. { .name = "pipe2",
  893. .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
  894. { .name = "pkey_alloc",
  895. .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
  896. { .name = "pkey_free",
  897. .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
  898. { .name = "pkey_mprotect",
  899. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  900. [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
  901. [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
  902. { .name = "poll", .timeout = true, },
  903. { .name = "ppoll", .timeout = true, },
  904. { .name = "prctl",
  905. .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */
  906. .strtoul = STUL_STRARRAY,
  907. .parm = &strarray__prctl_options, },
  908. [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
  909. [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
  910. { .name = "pread", .alias = "pread64", },
  911. { .name = "preadv", .alias = "pread", },
  912. { .name = "prlimit64",
  913. .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
  914. { .name = "pwrite", .alias = "pwrite64", },
  915. { .name = "readlinkat",
  916. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  917. { .name = "recvfrom",
  918. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  919. { .name = "recvmmsg",
  920. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  921. { .name = "recvmsg",
  922. .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  923. { .name = "renameat",
  924. .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
  925. [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
  926. { .name = "renameat2",
  927. .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
  928. [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
  929. [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
  930. { .name = "rt_sigaction",
  931. .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  932. { .name = "rt_sigprocmask",
  933. .arg = { [0] = STRARRAY(how, sighow), }, },
  934. { .name = "rt_sigqueueinfo",
  935. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  936. { .name = "rt_tgsigqueueinfo",
  937. .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  938. { .name = "sched_setscheduler",
  939. .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
  940. { .name = "seccomp",
  941. .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
  942. [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
  943. { .name = "select", .timeout = true, },
  944. { .name = "sendfile", .alias = "sendfile64", },
  945. { .name = "sendmmsg",
  946. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  947. { .name = "sendmsg",
  948. .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  949. { .name = "sendto",
  950. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
  951. [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
  952. { .name = "set_tid_address", .errpid = true, },
  953. { .name = "setitimer",
  954. .arg = { [0] = STRARRAY(which, itimers), }, },
  955. { .name = "setrlimit",
  956. .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
  957. { .name = "socket",
  958. .arg = { [0] = STRARRAY(family, socket_families),
  959. [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
  960. [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
  961. { .name = "socketpair",
  962. .arg = { [0] = STRARRAY(family, socket_families),
  963. [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
  964. [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
  965. { .name = "stat", .alias = "newstat", },
  966. { .name = "statx",
  967. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
  968. [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
  969. [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
  970. { .name = "swapoff",
  971. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
  972. { .name = "swapon",
  973. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
  974. { .name = "symlinkat",
  975. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  976. { .name = "sync_file_range",
  977. .arg = { [3] = { .scnprintf = SCA_SYNC_FILE_RANGE_FLAGS, /* flags */ }, }, },
  978. { .name = "tgkill",
  979. .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  980. { .name = "tkill",
  981. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  982. { .name = "umount2", .alias = "umount",
  983. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
  984. { .name = "uname", .alias = "newuname", },
  985. { .name = "unlinkat",
  986. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  987. { .name = "utimensat",
  988. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
  989. { .name = "wait4", .errpid = true,
  990. .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
  991. { .name = "waitid", .errpid = true,
  992. .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
  993. };
  994. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  995. {
  996. const struct syscall_fmt *fmt = fmtp;
  997. return strcmp(name, fmt->name);
  998. }
  999. static struct syscall_fmt *__syscall_fmt__find(struct syscall_fmt *fmts, const int nmemb, const char *name)
  1000. {
  1001. return bsearch(name, fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  1002. }
  1003. static struct syscall_fmt *syscall_fmt__find(const char *name)
  1004. {
  1005. const int nmemb = ARRAY_SIZE(syscall_fmts);
  1006. return __syscall_fmt__find(syscall_fmts, nmemb, name);
  1007. }
  1008. static struct syscall_fmt *__syscall_fmt__find_by_alias(struct syscall_fmt *fmts, const int nmemb, const char *alias)
  1009. {
  1010. int i;
  1011. for (i = 0; i < nmemb; ++i) {
  1012. if (fmts[i].alias && strcmp(fmts[i].alias, alias) == 0)
  1013. return &fmts[i];
  1014. }
  1015. return NULL;
  1016. }
  1017. static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
  1018. {
  1019. const int nmemb = ARRAY_SIZE(syscall_fmts);
  1020. return __syscall_fmt__find_by_alias(syscall_fmts, nmemb, alias);
  1021. }
  1022. /*
  1023. * is_exit: is this "exit" or "exit_group"?
  1024. * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter.
  1025. * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc.
  1026. * nonexistent: Just a hole in the syscall table, syscall id not allocated
  1027. */
  1028. struct syscall {
  1029. struct tep_event *tp_format;
  1030. int nr_args;
  1031. int args_size;
  1032. struct {
  1033. struct bpf_program *sys_enter,
  1034. *sys_exit;
  1035. } bpf_prog;
  1036. bool is_exit;
  1037. bool is_open;
  1038. bool nonexistent;
  1039. struct tep_format_field *args;
  1040. const char *name;
  1041. struct syscall_fmt *fmt;
  1042. struct syscall_arg_fmt *arg_fmt;
  1043. };
  1044. /*
  1045. * Must match what is in the BPF program:
  1046. *
  1047. * tools/perf/examples/bpf/augmented_raw_syscalls.c
  1048. */
  1049. struct bpf_map_syscall_entry {
  1050. bool enabled;
  1051. u16 string_args_len[6];
  1052. };
  1053. /*
  1054. * We need to have this 'calculated' boolean because in some cases we really
  1055. * don't know what is the duration of a syscall, for instance, when we start
  1056. * a session and some threads are waiting for a syscall to finish, say 'poll',
  1057. * in which case all we can do is to print "( ? ) for duration and for the
  1058. * start timestamp.
  1059. */
  1060. static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
  1061. {
  1062. double duration = (double)t / NSEC_PER_MSEC;
  1063. size_t printed = fprintf(fp, "(");
  1064. if (!calculated)
  1065. printed += fprintf(fp, " ");
  1066. else if (duration >= 1.0)
  1067. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  1068. else if (duration >= 0.01)
  1069. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  1070. else
  1071. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  1072. return printed + fprintf(fp, "): ");
  1073. }
  1074. /**
  1075. * filename.ptr: The filename char pointer that will be vfs_getname'd
  1076. * filename.entry_str_pos: Where to insert the string translated from
  1077. * filename.ptr by the vfs_getname tracepoint/kprobe.
  1078. * ret_scnprintf: syscall args may set this to a different syscall return
  1079. * formatter, for instance, fcntl may return fds, file flags, etc.
  1080. */
  1081. struct thread_trace {
  1082. u64 entry_time;
  1083. bool entry_pending;
  1084. unsigned long nr_events;
  1085. unsigned long pfmaj, pfmin;
  1086. char *entry_str;
  1087. double runtime_ms;
  1088. size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  1089. struct {
  1090. unsigned long ptr;
  1091. short int entry_str_pos;
  1092. bool pending_open;
  1093. unsigned int namelen;
  1094. char *name;
  1095. } filename;
  1096. struct {
  1097. int max;
  1098. struct file *table;
  1099. } files;
  1100. struct intlist *syscall_stats;
  1101. };
  1102. static struct thread_trace *thread_trace__new(void)
  1103. {
  1104. struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
  1105. if (ttrace) {
  1106. ttrace->files.max = -1;
  1107. ttrace->syscall_stats = intlist__new(NULL);
  1108. }
  1109. return ttrace;
  1110. }
  1111. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  1112. {
  1113. struct thread_trace *ttrace;
  1114. if (thread == NULL)
  1115. goto fail;
  1116. if (thread__priv(thread) == NULL)
  1117. thread__set_priv(thread, thread_trace__new());
  1118. if (thread__priv(thread) == NULL)
  1119. goto fail;
  1120. ttrace = thread__priv(thread);
  1121. ++ttrace->nr_events;
  1122. return ttrace;
  1123. fail:
  1124. color_fprintf(fp, PERF_COLOR_RED,
  1125. "WARNING: not enough memory, dropping samples!\n");
  1126. return NULL;
  1127. }
  1128. void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
  1129. size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
  1130. {
  1131. struct thread_trace *ttrace = thread__priv(arg->thread);
  1132. ttrace->ret_scnprintf = ret_scnprintf;
  1133. }
  1134. #define TRACE_PFMAJ (1 << 0)
  1135. #define TRACE_PFMIN (1 << 1)
  1136. static const size_t trace__entry_str_size = 2048;
  1137. static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd)
  1138. {
  1139. if (fd < 0)
  1140. return NULL;
  1141. if (fd > ttrace->files.max) {
  1142. struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file));
  1143. if (nfiles == NULL)
  1144. return NULL;
  1145. if (ttrace->files.max != -1) {
  1146. memset(nfiles + ttrace->files.max + 1, 0,
  1147. (fd - ttrace->files.max) * sizeof(struct file));
  1148. } else {
  1149. memset(nfiles, 0, (fd + 1) * sizeof(struct file));
  1150. }
  1151. ttrace->files.table = nfiles;
  1152. ttrace->files.max = fd;
  1153. }
  1154. return ttrace->files.table + fd;
  1155. }
  1156. struct file *thread__files_entry(struct thread *thread, int fd)
  1157. {
  1158. return thread_trace__files_entry(thread__priv(thread), fd);
  1159. }
  1160. static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
  1161. {
  1162. struct thread_trace *ttrace = thread__priv(thread);
  1163. struct file *file = thread_trace__files_entry(ttrace, fd);
  1164. if (file != NULL) {
  1165. struct stat st;
  1166. if (stat(pathname, &st) == 0)
  1167. file->dev_maj = major(st.st_rdev);
  1168. file->pathname = strdup(pathname);
  1169. if (file->pathname)
  1170. return 0;
  1171. }
  1172. return -1;
  1173. }
  1174. static int thread__read_fd_path(struct thread *thread, int fd)
  1175. {
  1176. char linkname[PATH_MAX], pathname[PATH_MAX];
  1177. struct stat st;
  1178. int ret;
  1179. if (thread->pid_ == thread->tid) {
  1180. scnprintf(linkname, sizeof(linkname),
  1181. "/proc/%d/fd/%d", thread->pid_, fd);
  1182. } else {
  1183. scnprintf(linkname, sizeof(linkname),
  1184. "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
  1185. }
  1186. if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
  1187. return -1;
  1188. ret = readlink(linkname, pathname, sizeof(pathname));
  1189. if (ret < 0 || ret > st.st_size)
  1190. return -1;
  1191. pathname[ret] = '\0';
  1192. return trace__set_fd_pathname(thread, fd, pathname);
  1193. }
  1194. static const char *thread__fd_path(struct thread *thread, int fd,
  1195. struct trace *trace)
  1196. {
  1197. struct thread_trace *ttrace = thread__priv(thread);
  1198. if (ttrace == NULL || trace->fd_path_disabled)
  1199. return NULL;
  1200. if (fd < 0)
  1201. return NULL;
  1202. if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
  1203. if (!trace->live)
  1204. return NULL;
  1205. ++trace->stats.proc_getname;
  1206. if (thread__read_fd_path(thread, fd))
  1207. return NULL;
  1208. }
  1209. return ttrace->files.table[fd].pathname;
  1210. }
  1211. size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
  1212. {
  1213. int fd = arg->val;
  1214. size_t printed = scnprintf(bf, size, "%d", fd);
  1215. const char *path = thread__fd_path(arg->thread, fd, arg->trace);
  1216. if (path)
  1217. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  1218. return printed;
  1219. }
  1220. size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
  1221. {
  1222. size_t printed = scnprintf(bf, size, "%d", fd);
  1223. struct thread *thread = machine__find_thread(trace->host, pid, pid);
  1224. if (thread) {
  1225. const char *path = thread__fd_path(thread, fd, trace);
  1226. if (path)
  1227. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  1228. thread__put(thread);
  1229. }
  1230. return printed;
  1231. }
  1232. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  1233. struct syscall_arg *arg)
  1234. {
  1235. int fd = arg->val;
  1236. size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
  1237. struct thread_trace *ttrace = thread__priv(arg->thread);
  1238. if (ttrace && fd >= 0 && fd <= ttrace->files.max)
  1239. zfree(&ttrace->files.table[fd].pathname);
  1240. return printed;
  1241. }
  1242. static void thread__set_filename_pos(struct thread *thread, const char *bf,
  1243. unsigned long ptr)
  1244. {
  1245. struct thread_trace *ttrace = thread__priv(thread);
  1246. ttrace->filename.ptr = ptr;
  1247. ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
  1248. }
  1249. static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
  1250. {
  1251. struct augmented_arg *augmented_arg = arg->augmented.args;
  1252. size_t printed = scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
  1253. /*
  1254. * So that the next arg with a payload can consume its augmented arg, i.e. for rename* syscalls
  1255. * we would have two strings, each prefixed by its size.
  1256. */
  1257. int consumed = sizeof(*augmented_arg) + augmented_arg->size;
  1258. arg->augmented.args = ((void *)arg->augmented.args) + consumed;
  1259. arg->augmented.size -= consumed;
  1260. return printed;
  1261. }
  1262. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  1263. struct syscall_arg *arg)
  1264. {
  1265. unsigned long ptr = arg->val;
  1266. if (arg->augmented.args)
  1267. return syscall_arg__scnprintf_augmented_string(arg, bf, size);
  1268. if (!arg->trace->vfs_getname)
  1269. return scnprintf(bf, size, "%#x", ptr);
  1270. thread__set_filename_pos(arg->thread, bf, ptr);
  1271. return 0;
  1272. }
  1273. static bool trace__filter_duration(struct trace *trace, double t)
  1274. {
  1275. return t < (trace->duration_filter * NSEC_PER_MSEC);
  1276. }
  1277. static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  1278. {
  1279. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  1280. return fprintf(fp, "%10.3f ", ts);
  1281. }
  1282. /*
  1283. * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
  1284. * using ttrace->entry_time for a thread that receives a sys_exit without
  1285. * first having received a sys_enter ("poll" issued before tracing session
  1286. * starts, lost sys_enter exit due to ring buffer overflow).
  1287. */
  1288. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  1289. {
  1290. if (tstamp > 0)
  1291. return __trace__fprintf_tstamp(trace, tstamp, fp);
  1292. return fprintf(fp, " ? ");
  1293. }
  1294. static bool done = false;
  1295. static bool interrupted = false;
  1296. static void sig_handler(int sig)
  1297. {
  1298. done = true;
  1299. interrupted = sig == SIGINT;
  1300. }
  1301. static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
  1302. {
  1303. size_t printed = 0;
  1304. if (trace->multiple_threads) {
  1305. if (trace->show_comm)
  1306. printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
  1307. printed += fprintf(fp, "%d ", thread->tid);
  1308. }
  1309. return printed;
  1310. }
  1311. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  1312. u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
  1313. {
  1314. size_t printed = 0;
  1315. if (trace->show_tstamp)
  1316. printed = trace__fprintf_tstamp(trace, tstamp, fp);
  1317. if (trace->show_duration)
  1318. printed += fprintf_duration(duration, duration_calculated, fp);
  1319. return printed + trace__fprintf_comm_tid(trace, thread, fp);
  1320. }
  1321. static int trace__process_event(struct trace *trace, struct machine *machine,
  1322. union perf_event *event, struct perf_sample *sample)
  1323. {
  1324. int ret = 0;
  1325. switch (event->header.type) {
  1326. case PERF_RECORD_LOST:
  1327. color_fprintf(trace->output, PERF_COLOR_RED,
  1328. "LOST %" PRIu64 " events!\n", event->lost.lost);
  1329. ret = machine__process_lost_event(machine, event, sample);
  1330. break;
  1331. default:
  1332. ret = machine__process_event(machine, event, sample);
  1333. break;
  1334. }
  1335. return ret;
  1336. }
  1337. static int trace__tool_process(struct perf_tool *tool,
  1338. union perf_event *event,
  1339. struct perf_sample *sample,
  1340. struct machine *machine)
  1341. {
  1342. struct trace *trace = container_of(tool, struct trace, tool);
  1343. return trace__process_event(trace, machine, event, sample);
  1344. }
  1345. static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  1346. {
  1347. struct machine *machine = vmachine;
  1348. if (machine->kptr_restrict_warned)
  1349. return NULL;
  1350. if (symbol_conf.kptr_restrict) {
  1351. pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  1352. "Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
  1353. "Kernel samples will not be resolved.\n");
  1354. machine->kptr_restrict_warned = true;
  1355. return NULL;
  1356. }
  1357. return machine__resolve_kernel_addr(vmachine, addrp, modp);
  1358. }
  1359. static int trace__symbols_init(struct trace *trace, struct evlist *evlist)
  1360. {
  1361. int err = symbol__init(NULL);
  1362. if (err)
  1363. return err;
  1364. trace->host = machine__new_host();
  1365. if (trace->host == NULL)
  1366. return -ENOMEM;
  1367. err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
  1368. if (err < 0)
  1369. goto out;
  1370. err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
  1371. evlist->core.threads, trace__tool_process, false,
  1372. 1);
  1373. out:
  1374. if (err)
  1375. symbol__exit();
  1376. return err;
  1377. }
  1378. static void trace__symbols__exit(struct trace *trace)
  1379. {
  1380. machine__exit(trace->host);
  1381. trace->host = NULL;
  1382. symbol__exit();
  1383. }
  1384. static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
  1385. {
  1386. int idx;
  1387. if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
  1388. nr_args = sc->fmt->nr_args;
  1389. sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
  1390. if (sc->arg_fmt == NULL)
  1391. return -1;
  1392. for (idx = 0; idx < nr_args; ++idx) {
  1393. if (sc->fmt)
  1394. sc->arg_fmt[idx] = sc->fmt->arg[idx];
  1395. }
  1396. sc->nr_args = nr_args;
  1397. return 0;
  1398. }
  1399. static struct syscall_arg_fmt syscall_arg_fmts__by_name[] = {
  1400. { .name = "msr", .scnprintf = SCA_X86_MSR, .strtoul = STUL_X86_MSR, },
  1401. { .name = "vector", .scnprintf = SCA_X86_IRQ_VECTORS, .strtoul = STUL_X86_IRQ_VECTORS, },
  1402. };
  1403. static int syscall_arg_fmt__cmp(const void *name, const void *fmtp)
  1404. {
  1405. const struct syscall_arg_fmt *fmt = fmtp;
  1406. return strcmp(name, fmt->name);
  1407. }
  1408. static struct syscall_arg_fmt *
  1409. __syscall_arg_fmt__find_by_name(struct syscall_arg_fmt *fmts, const int nmemb, const char *name)
  1410. {
  1411. return bsearch(name, fmts, nmemb, sizeof(struct syscall_arg_fmt), syscall_arg_fmt__cmp);
  1412. }
  1413. static struct syscall_arg_fmt *syscall_arg_fmt__find_by_name(const char *name)
  1414. {
  1415. const int nmemb = ARRAY_SIZE(syscall_arg_fmts__by_name);
  1416. return __syscall_arg_fmt__find_by_name(syscall_arg_fmts__by_name, nmemb, name);
  1417. }
  1418. static struct tep_format_field *
  1419. syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field *field)
  1420. {
  1421. struct tep_format_field *last_field = NULL;
  1422. int len;
  1423. for (; field; field = field->next, ++arg) {
  1424. last_field = field;
  1425. if (arg->scnprintf)
  1426. continue;
  1427. len = strlen(field->name);
  1428. if (strcmp(field->type, "const char *") == 0 &&
  1429. ((len >= 4 && strcmp(field->name + len - 4, "name") == 0) ||
  1430. strstr(field->name, "path") != NULL))
  1431. arg->scnprintf = SCA_FILENAME;
  1432. else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
  1433. arg->scnprintf = SCA_PTR;
  1434. else if (strcmp(field->type, "pid_t") == 0)
  1435. arg->scnprintf = SCA_PID;
  1436. else if (strcmp(field->type, "umode_t") == 0)
  1437. arg->scnprintf = SCA_MODE_T;
  1438. else if ((field->flags & TEP_FIELD_IS_ARRAY) && strstr(field->type, "char")) {
  1439. arg->scnprintf = SCA_CHAR_ARRAY;
  1440. arg->nr_entries = field->arraylen;
  1441. } else if ((strcmp(field->type, "int") == 0 ||
  1442. strcmp(field->type, "unsigned int") == 0 ||
  1443. strcmp(field->type, "long") == 0) &&
  1444. len >= 2 && strcmp(field->name + len - 2, "fd") == 0) {
  1445. /*
  1446. * /sys/kernel/tracing/events/syscalls/sys_enter*
  1447. * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
  1448. * 65 int
  1449. * 23 unsigned int
  1450. * 7 unsigned long
  1451. */
  1452. arg->scnprintf = SCA_FD;
  1453. } else {
  1454. struct syscall_arg_fmt *fmt = syscall_arg_fmt__find_by_name(field->name);
  1455. if (fmt) {
  1456. arg->scnprintf = fmt->scnprintf;
  1457. arg->strtoul = fmt->strtoul;
  1458. }
  1459. }
  1460. }
  1461. return last_field;
  1462. }
  1463. static int syscall__set_arg_fmts(struct syscall *sc)
  1464. {
  1465. struct tep_format_field *last_field = syscall_arg_fmt__init_array(sc->arg_fmt, sc->args);
  1466. if (last_field)
  1467. sc->args_size = last_field->offset + last_field->size;
  1468. return 0;
  1469. }
  1470. static int trace__read_syscall_info(struct trace *trace, int id)
  1471. {
  1472. char tp_name[128];
  1473. struct syscall *sc;
  1474. const char *name = syscalltbl__name(trace->sctbl, id);
  1475. #ifdef HAVE_SYSCALL_TABLE_SUPPORT
  1476. if (trace->syscalls.table == NULL) {
  1477. trace->syscalls.table = calloc(trace->sctbl->syscalls.max_id + 1, sizeof(*sc));
  1478. if (trace->syscalls.table == NULL)
  1479. return -ENOMEM;
  1480. }
  1481. #else
  1482. if (id > trace->sctbl->syscalls.max_id || (id == 0 && trace->syscalls.table == NULL)) {
  1483. // When using libaudit we don't know beforehand what is the max syscall id
  1484. struct syscall *table = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  1485. if (table == NULL)
  1486. return -ENOMEM;
  1487. // Need to memset from offset 0 and +1 members if brand new
  1488. if (trace->syscalls.table == NULL)
  1489. memset(table, 0, (id + 1) * sizeof(*sc));
  1490. else
  1491. memset(table + trace->sctbl->syscalls.max_id + 1, 0, (id - trace->sctbl->syscalls.max_id) * sizeof(*sc));
  1492. trace->syscalls.table = table;
  1493. trace->sctbl->syscalls.max_id = id;
  1494. }
  1495. #endif
  1496. sc = trace->syscalls.table + id;
  1497. if (sc->nonexistent)
  1498. return 0;
  1499. if (name == NULL) {
  1500. sc->nonexistent = true;
  1501. return 0;
  1502. }
  1503. sc->name = name;
  1504. sc->fmt = syscall_fmt__find(sc->name);
  1505. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  1506. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1507. if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
  1508. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  1509. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1510. }
  1511. if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
  1512. return -ENOMEM;
  1513. if (IS_ERR(sc->tp_format))
  1514. return PTR_ERR(sc->tp_format);
  1515. sc->args = sc->tp_format->format.fields;
  1516. /*
  1517. * We need to check and discard the first variable '__syscall_nr'
  1518. * or 'nr' that mean the syscall number. It is needless here.
  1519. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
  1520. */
  1521. if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
  1522. sc->args = sc->args->next;
  1523. --sc->nr_args;
  1524. }
  1525. sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
  1526. sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
  1527. return syscall__set_arg_fmts(sc);
  1528. }
  1529. static int evsel__init_tp_arg_scnprintf(struct evsel *evsel)
  1530. {
  1531. struct syscall_arg_fmt *fmt = evsel__syscall_arg_fmt(evsel);
  1532. if (fmt != NULL) {
  1533. syscall_arg_fmt__init_array(fmt, evsel->tp_format->format.fields);
  1534. return 0;
  1535. }
  1536. return -ENOMEM;
  1537. }
  1538. static int intcmp(const void *a, const void *b)
  1539. {
  1540. const int *one = a, *another = b;
  1541. return *one - *another;
  1542. }
  1543. static int trace__validate_ev_qualifier(struct trace *trace)
  1544. {
  1545. int err = 0;
  1546. bool printed_invalid_prefix = false;
  1547. struct str_node *pos;
  1548. size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier);
  1549. trace->ev_qualifier_ids.entries = malloc(nr_allocated *
  1550. sizeof(trace->ev_qualifier_ids.entries[0]));
  1551. if (trace->ev_qualifier_ids.entries == NULL) {
  1552. fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
  1553. trace->output);
  1554. err = -EINVAL;
  1555. goto out;
  1556. }
  1557. strlist__for_each_entry(pos, trace->ev_qualifier) {
  1558. const char *sc = pos->s;
  1559. int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
  1560. if (id < 0) {
  1561. id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
  1562. if (id >= 0)
  1563. goto matches;
  1564. if (!printed_invalid_prefix) {
  1565. pr_debug("Skipping unknown syscalls: ");
  1566. printed_invalid_prefix = true;
  1567. } else {
  1568. pr_debug(", ");
  1569. }
  1570. pr_debug("%s", sc);
  1571. continue;
  1572. }
  1573. matches:
  1574. trace->ev_qualifier_ids.entries[nr_used++] = id;
  1575. if (match_next == -1)
  1576. continue;
  1577. while (1) {
  1578. id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
  1579. if (id < 0)
  1580. break;
  1581. if (nr_allocated == nr_used) {
  1582. void *entries;
  1583. nr_allocated += 8;
  1584. entries = realloc(trace->ev_qualifier_ids.entries,
  1585. nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
  1586. if (entries == NULL) {
  1587. err = -ENOMEM;
  1588. fputs("\nError:\t Not enough memory for parsing\n", trace->output);
  1589. goto out_free;
  1590. }
  1591. trace->ev_qualifier_ids.entries = entries;
  1592. }
  1593. trace->ev_qualifier_ids.entries[nr_used++] = id;
  1594. }
  1595. }
  1596. trace->ev_qualifier_ids.nr = nr_used;
  1597. qsort(trace->ev_qualifier_ids.entries, nr_used, sizeof(int), intcmp);
  1598. out:
  1599. if (printed_invalid_prefix)
  1600. pr_debug("\n");
  1601. return err;
  1602. out_free:
  1603. zfree(&trace->ev_qualifier_ids.entries);
  1604. trace->ev_qualifier_ids.nr = 0;
  1605. goto out;
  1606. }
  1607. static __maybe_unused bool trace__syscall_enabled(struct trace *trace, int id)
  1608. {
  1609. bool in_ev_qualifier;
  1610. if (trace->ev_qualifier_ids.nr == 0)
  1611. return true;
  1612. in_ev_qualifier = bsearch(&id, trace->ev_qualifier_ids.entries,
  1613. trace->ev_qualifier_ids.nr, sizeof(int), intcmp) != NULL;
  1614. if (in_ev_qualifier)
  1615. return !trace->not_ev_qualifier;
  1616. return trace->not_ev_qualifier;
  1617. }
  1618. /*
  1619. * args is to be interpreted as a series of longs but we need to handle
  1620. * 8-byte unaligned accesses. args points to raw_data within the event
  1621. * and raw_data is guaranteed to be 8-byte unaligned because it is
  1622. * preceded by raw_size which is a u32. So we need to copy args to a temp
  1623. * variable to read it. Most notably this avoids extended load instructions
  1624. * on unaligned addresses
  1625. */
  1626. unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
  1627. {
  1628. unsigned long val;
  1629. unsigned char *p = arg->args + sizeof(unsigned long) * idx;
  1630. memcpy(&val, p, sizeof(val));
  1631. return val;
  1632. }
  1633. static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
  1634. struct syscall_arg *arg)
  1635. {
  1636. if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
  1637. return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
  1638. return scnprintf(bf, size, "arg%d: ", arg->idx);
  1639. }
  1640. /*
  1641. * Check if the value is in fact zero, i.e. mask whatever needs masking, such
  1642. * as mount 'flags' argument that needs ignoring some magic flag, see comment
  1643. * in tools/perf/trace/beauty/mount_flags.c
  1644. */
  1645. static unsigned long syscall_arg_fmt__mask_val(struct syscall_arg_fmt *fmt, struct syscall_arg *arg, unsigned long val)
  1646. {
  1647. if (fmt && fmt->mask_val)
  1648. return fmt->mask_val(arg, val);
  1649. return val;
  1650. }
  1651. static size_t syscall_arg_fmt__scnprintf_val(struct syscall_arg_fmt *fmt, char *bf, size_t size,
  1652. struct syscall_arg *arg, unsigned long val)
  1653. {
  1654. if (fmt && fmt->scnprintf) {
  1655. arg->val = val;
  1656. if (fmt->parm)
  1657. arg->parm = fmt->parm;
  1658. return fmt->scnprintf(bf, size, arg);
  1659. }
  1660. return scnprintf(bf, size, "%ld", val);
  1661. }
  1662. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  1663. unsigned char *args, void *augmented_args, int augmented_args_size,
  1664. struct trace *trace, struct thread *thread)
  1665. {
  1666. size_t printed = 0;
  1667. unsigned long val;
  1668. u8 bit = 1;
  1669. struct syscall_arg arg = {
  1670. .args = args,
  1671. .augmented = {
  1672. .size = augmented_args_size,
  1673. .args = augmented_args,
  1674. },
  1675. .idx = 0,
  1676. .mask = 0,
  1677. .trace = trace,
  1678. .thread = thread,
  1679. .show_string_prefix = trace->show_string_prefix,
  1680. };
  1681. struct thread_trace *ttrace = thread__priv(thread);
  1682. /*
  1683. * Things like fcntl will set this in its 'cmd' formatter to pick the
  1684. * right formatter for the return value (an fd? file flags?), which is
  1685. * not needed for syscalls that always return a given type, say an fd.
  1686. */
  1687. ttrace->ret_scnprintf = NULL;
  1688. if (sc->args != NULL) {
  1689. struct tep_format_field *field;
  1690. for (field = sc->args; field;
  1691. field = field->next, ++arg.idx, bit <<= 1) {
  1692. if (arg.mask & bit)
  1693. continue;
  1694. arg.fmt = &sc->arg_fmt[arg.idx];
  1695. val = syscall_arg__val(&arg, arg.idx);
  1696. /*
  1697. * Some syscall args need some mask, most don't and
  1698. * return val untouched.
  1699. */
  1700. val = syscall_arg_fmt__mask_val(&sc->arg_fmt[arg.idx], &arg, val);
  1701. /*
  1702. * Suppress this argument if its value is zero and
  1703. * and we don't have a string associated in an
  1704. * strarray for it.
  1705. */
  1706. if (val == 0 &&
  1707. !trace->show_zeros &&
  1708. !(sc->arg_fmt &&
  1709. (sc->arg_fmt[arg.idx].show_zero ||
  1710. sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
  1711. sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
  1712. sc->arg_fmt[arg.idx].parm))
  1713. continue;
  1714. printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
  1715. if (trace->show_arg_names)
  1716. printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
  1717. printed += syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx],
  1718. bf + printed, size - printed, &arg, val);
  1719. }
  1720. } else if (IS_ERR(sc->tp_format)) {
  1721. /*
  1722. * If we managed to read the tracepoint /format file, then we
  1723. * may end up not having any args, like with gettid(), so only
  1724. * print the raw args when we didn't manage to read it.
  1725. */
  1726. while (arg.idx < sc->nr_args) {
  1727. if (arg.mask & bit)
  1728. goto next_arg;
  1729. val = syscall_arg__val(&arg, arg.idx);
  1730. if (printed)
  1731. printed += scnprintf(bf + printed, size - printed, ", ");
  1732. printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
  1733. printed += syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val);
  1734. next_arg:
  1735. ++arg.idx;
  1736. bit <<= 1;
  1737. }
  1738. }
  1739. return printed;
  1740. }
  1741. typedef int (*tracepoint_handler)(struct trace *trace, struct evsel *evsel,
  1742. union perf_event *event,
  1743. struct perf_sample *sample);
  1744. static struct syscall *trace__syscall_info(struct trace *trace,
  1745. struct evsel *evsel, int id)
  1746. {
  1747. int err = 0;
  1748. if (id < 0) {
  1749. /*
  1750. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  1751. * before that, leaving at a higher verbosity level till that is
  1752. * explained. Reproduced with plain ftrace with:
  1753. *
  1754. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  1755. * grep "NR -1 " /t/trace_pipe
  1756. *
  1757. * After generating some load on the machine.
  1758. */
  1759. if (verbose > 1) {
  1760. static u64 n;
  1761. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  1762. id, evsel__name(evsel), ++n);
  1763. }
  1764. return NULL;
  1765. }
  1766. err = -EINVAL;
  1767. #ifdef HAVE_SYSCALL_TABLE_SUPPORT
  1768. if (id > trace->sctbl->syscalls.max_id) {
  1769. #else
  1770. if (id >= trace->sctbl->syscalls.max_id) {
  1771. /*
  1772. * With libaudit we don't know beforehand what is the max_id,
  1773. * so we let trace__read_syscall_info() figure that out as we
  1774. * go on reading syscalls.
  1775. */
  1776. err = trace__read_syscall_info(trace, id);
  1777. if (err)
  1778. #endif
  1779. goto out_cant_read;
  1780. }
  1781. if ((trace->syscalls.table == NULL || trace->syscalls.table[id].name == NULL) &&
  1782. (err = trace__read_syscall_info(trace, id)) != 0)
  1783. goto out_cant_read;
  1784. if (trace->syscalls.table[id].name == NULL) {
  1785. if (trace->syscalls.table[id].nonexistent)
  1786. return NULL;
  1787. goto out_cant_read;
  1788. }
  1789. return &trace->syscalls.table[id];
  1790. out_cant_read:
  1791. if (verbose > 0) {
  1792. char sbuf[STRERR_BUFSIZE];
  1793. fprintf(trace->output, "Problems reading syscall %d: %d (%s)", id, -err, str_error_r(-err, sbuf, sizeof(sbuf)));
  1794. if (id <= trace->sctbl->syscalls.max_id && trace->syscalls.table[id].name != NULL)
  1795. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  1796. fputs(" information\n", trace->output);
  1797. }
  1798. return NULL;
  1799. }
  1800. struct syscall_stats {
  1801. struct stats stats;
  1802. u64 nr_failures;
  1803. int max_errno;
  1804. u32 *errnos;
  1805. };
  1806. static void thread__update_stats(struct thread *thread, struct thread_trace *ttrace,
  1807. int id, struct perf_sample *sample, long err, bool errno_summary)
  1808. {
  1809. struct int_node *inode;
  1810. struct syscall_stats *stats;
  1811. u64 duration = 0;
  1812. inode = intlist__findnew(ttrace->syscall_stats, id);
  1813. if (inode == NULL)
  1814. return;
  1815. stats = inode->priv;
  1816. if (stats == NULL) {
  1817. stats = malloc(sizeof(*stats));
  1818. if (stats == NULL)
  1819. return;
  1820. stats->nr_failures = 0;
  1821. stats->max_errno = 0;
  1822. stats->errnos = NULL;
  1823. init_stats(&stats->stats);
  1824. inode->priv = stats;
  1825. }
  1826. if (ttrace->entry_time && sample->time > ttrace->entry_time)
  1827. duration = sample->time - ttrace->entry_time;
  1828. update_stats(&stats->stats, duration);
  1829. if (err < 0) {
  1830. ++stats->nr_failures;
  1831. if (!errno_summary)
  1832. return;
  1833. err = -err;
  1834. if (err > stats->max_errno) {
  1835. u32 *new_errnos = realloc(stats->errnos, err * sizeof(u32));
  1836. if (new_errnos) {
  1837. memset(new_errnos + stats->max_errno, 0, (err - stats->max_errno) * sizeof(u32));
  1838. } else {
  1839. pr_debug("Not enough memory for errno stats for thread \"%s\"(%d/%d), results will be incomplete\n",
  1840. thread__comm_str(thread), thread->pid_, thread->tid);
  1841. return;
  1842. }
  1843. stats->errnos = new_errnos;
  1844. stats->max_errno = err;
  1845. }
  1846. ++stats->errnos[err - 1];
  1847. }
  1848. }
  1849. static int trace__printf_interrupted_entry(struct trace *trace)
  1850. {
  1851. struct thread_trace *ttrace;
  1852. size_t printed;
  1853. int len;
  1854. if (trace->failure_only || trace->current == NULL)
  1855. return 0;
  1856. ttrace = thread__priv(trace->current);
  1857. if (!ttrace->entry_pending)
  1858. return 0;
  1859. printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
  1860. printed += len = fprintf(trace->output, "%s)", ttrace->entry_str);
  1861. if (len < trace->args_alignment - 4)
  1862. printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " ");
  1863. printed += fprintf(trace->output, " ...\n");
  1864. ttrace->entry_pending = false;
  1865. ++trace->nr_events_printed;
  1866. return printed;
  1867. }
  1868. static int trace__fprintf_sample(struct trace *trace, struct evsel *evsel,
  1869. struct perf_sample *sample, struct thread *thread)
  1870. {
  1871. int printed = 0;
  1872. if (trace->print_sample) {
  1873. double ts = (double)sample->time / NSEC_PER_MSEC;
  1874. printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
  1875. evsel__name(evsel), ts,
  1876. thread__comm_str(thread),
  1877. sample->pid, sample->tid, sample->cpu);
  1878. }
  1879. return printed;
  1880. }
  1881. static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, int raw_augmented_args_size)
  1882. {
  1883. void *augmented_args = NULL;
  1884. /*
  1885. * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
  1886. * and there we get all 6 syscall args plus the tracepoint common fields
  1887. * that gets calculated at the start and the syscall_nr (another long).
  1888. * So we check if that is the case and if so don't look after the
  1889. * sc->args_size but always after the full raw_syscalls:sys_enter payload,
  1890. * which is fixed.
  1891. *
  1892. * We'll revisit this later to pass s->args_size to the BPF augmenter
  1893. * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
  1894. * copies only what we need for each syscall, like what happens when we
  1895. * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
  1896. * traffic to just what is needed for each syscall.
  1897. */
  1898. int args_size = raw_augmented_args_size ?: sc->args_size;
  1899. *augmented_args_size = sample->raw_size - args_size;
  1900. if (*augmented_args_size > 0)
  1901. augmented_args = sample->raw_data + args_size;
  1902. return augmented_args;
  1903. }
  1904. static int trace__sys_enter(struct trace *trace, struct evsel *evsel,
  1905. union perf_event *event __maybe_unused,
  1906. struct perf_sample *sample)
  1907. {
  1908. char *msg;
  1909. void *args;
  1910. int printed = 0;
  1911. struct thread *thread;
  1912. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  1913. int augmented_args_size = 0;
  1914. void *augmented_args = NULL;
  1915. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1916. struct thread_trace *ttrace;
  1917. if (sc == NULL)
  1918. return -1;
  1919. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1920. ttrace = thread__trace(thread, trace->output);
  1921. if (ttrace == NULL)
  1922. goto out_put;
  1923. trace__fprintf_sample(trace, evsel, sample, thread);
  1924. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  1925. if (ttrace->entry_str == NULL) {
  1926. ttrace->entry_str = malloc(trace__entry_str_size);
  1927. if (!ttrace->entry_str)
  1928. goto out_put;
  1929. }
  1930. if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
  1931. trace__printf_interrupted_entry(trace);
  1932. /*
  1933. * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
  1934. * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
  1935. * this breaks syscall__augmented_args() check for augmented args, as we calculate
  1936. * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
  1937. * so when handling, say the openat syscall, we end up getting 6 args for the
  1938. * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
  1939. * thinking that the extra 2 u64 args are the augmented filename, so just check
  1940. * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
  1941. */
  1942. if (evsel != trace->syscalls.events.sys_enter)
  1943. augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
  1944. ttrace->entry_time = sample->time;
  1945. msg = ttrace->entry_str;
  1946. printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
  1947. printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
  1948. args, augmented_args, augmented_args_size, trace, thread);
  1949. if (sc->is_exit) {
  1950. if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
  1951. int alignment = 0;
  1952. trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
  1953. printed = fprintf(trace->output, "%s)", ttrace->entry_str);
  1954. if (trace->args_alignment > printed)
  1955. alignment = trace->args_alignment - printed;
  1956. fprintf(trace->output, "%*s= ?\n", alignment, " ");
  1957. }
  1958. } else {
  1959. ttrace->entry_pending = true;
  1960. /* See trace__vfs_getname & trace__sys_exit */
  1961. ttrace->filename.pending_open = false;
  1962. }
  1963. if (trace->current != thread) {
  1964. thread__put(trace->current);
  1965. trace->current = thread__get(thread);
  1966. }
  1967. err = 0;
  1968. out_put:
  1969. thread__put(thread);
  1970. return err;
  1971. }
  1972. static int trace__fprintf_sys_enter(struct trace *trace, struct evsel *evsel,
  1973. struct perf_sample *sample)
  1974. {
  1975. struct thread_trace *ttrace;
  1976. struct thread *thread;
  1977. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  1978. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1979. char msg[1024];
  1980. void *args, *augmented_args = NULL;
  1981. int augmented_args_size;
  1982. if (sc == NULL)
  1983. return -1;
  1984. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1985. ttrace = thread__trace(thread, trace->output);
  1986. /*
  1987. * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
  1988. * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
  1989. */
  1990. if (ttrace == NULL)
  1991. goto out_put;
  1992. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  1993. augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
  1994. syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
  1995. fprintf(trace->output, "%s", msg);
  1996. err = 0;
  1997. out_put:
  1998. thread__put(thread);
  1999. return err;
  2000. }
  2001. static int trace__resolve_callchain(struct trace *trace, struct evsel *evsel,
  2002. struct perf_sample *sample,
  2003. struct callchain_cursor *cursor)
  2004. {
  2005. struct addr_location al;
  2006. int max_stack = evsel->core.attr.sample_max_stack ?
  2007. evsel->core.attr.sample_max_stack :
  2008. trace->max_stack;
  2009. int err;
  2010. if (machine__resolve(trace->host, &al, sample) < 0)
  2011. return -1;
  2012. err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
  2013. addr_location__put(&al);
  2014. return err;
  2015. }
  2016. static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
  2017. {
  2018. /* TODO: user-configurable print_opts */
  2019. const unsigned int print_opts = EVSEL__PRINT_SYM |
  2020. EVSEL__PRINT_DSO |
  2021. EVSEL__PRINT_UNKNOWN_AS_ADDR;
  2022. return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, symbol_conf.bt_stop_list, trace->output);
  2023. }
  2024. static const char *errno_to_name(struct evsel *evsel, int err)
  2025. {
  2026. struct perf_env *env = evsel__env(evsel);
  2027. const char *arch_name = perf_env__arch(env);
  2028. return arch_syscalls__strerrno(arch_name, err);
  2029. }
  2030. static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
  2031. union perf_event *event __maybe_unused,
  2032. struct perf_sample *sample)
  2033. {
  2034. long ret;
  2035. u64 duration = 0;
  2036. bool duration_calculated = false;
  2037. struct thread *thread;
  2038. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
  2039. int alignment = trace->args_alignment;
  2040. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  2041. struct thread_trace *ttrace;
  2042. if (sc == NULL)
  2043. return -1;
  2044. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2045. ttrace = thread__trace(thread, trace->output);
  2046. if (ttrace == NULL)
  2047. goto out_put;
  2048. trace__fprintf_sample(trace, evsel, sample, thread);
  2049. ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
  2050. if (trace->summary)
  2051. thread__update_stats(thread, ttrace, id, sample, ret, trace->errno_summary);
  2052. if (!trace->fd_path_disabled && sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
  2053. trace__set_fd_pathname(thread, ret, ttrace->filename.name);
  2054. ttrace->filename.pending_open = false;
  2055. ++trace->stats.vfs_getname;
  2056. }
  2057. if (ttrace->entry_time) {
  2058. duration = sample->time - ttrace->entry_time;
  2059. if (trace__filter_duration(trace, duration))
  2060. goto out;
  2061. duration_calculated = true;
  2062. } else if (trace->duration_filter)
  2063. goto out;
  2064. if (sample->callchain) {
  2065. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  2066. if (callchain_ret == 0) {
  2067. if (callchain_cursor.nr < trace->min_stack)
  2068. goto out;
  2069. callchain_ret = 1;
  2070. }
  2071. }
  2072. if (trace->summary_only || (ret >= 0 && trace->failure_only))
  2073. goto out;
  2074. trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
  2075. if (ttrace->entry_pending) {
  2076. printed = fprintf(trace->output, "%s", ttrace->entry_str);
  2077. } else {
  2078. printed += fprintf(trace->output, " ... [");
  2079. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  2080. printed += 9;
  2081. printed += fprintf(trace->output, "]: %s()", sc->name);
  2082. }
  2083. printed++; /* the closing ')' */
  2084. if (alignment > printed)
  2085. alignment -= printed;
  2086. else
  2087. alignment = 0;
  2088. fprintf(trace->output, ")%*s= ", alignment, " ");
  2089. if (sc->fmt == NULL) {
  2090. if (ret < 0)
  2091. goto errno_print;
  2092. signed_print:
  2093. fprintf(trace->output, "%ld", ret);
  2094. } else if (ret < 0) {
  2095. errno_print: {
  2096. char bf[STRERR_BUFSIZE];
  2097. const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
  2098. *e = errno_to_name(evsel, -ret);
  2099. fprintf(trace->output, "-1 %s (%s)", e, emsg);
  2100. }
  2101. } else if (ret == 0 && sc->fmt->timeout)
  2102. fprintf(trace->output, "0 (Timeout)");
  2103. else if (ttrace->ret_scnprintf) {
  2104. char bf[1024];
  2105. struct syscall_arg arg = {
  2106. .val = ret,
  2107. .thread = thread,
  2108. .trace = trace,
  2109. };
  2110. ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
  2111. ttrace->ret_scnprintf = NULL;
  2112. fprintf(trace->output, "%s", bf);
  2113. } else if (sc->fmt->hexret)
  2114. fprintf(trace->output, "%#lx", ret);
  2115. else if (sc->fmt->errpid) {
  2116. struct thread *child = machine__find_thread(trace->host, ret, ret);
  2117. if (child != NULL) {
  2118. fprintf(trace->output, "%ld", ret);
  2119. if (child->comm_set)
  2120. fprintf(trace->output, " (%s)", thread__comm_str(child));
  2121. thread__put(child);
  2122. }
  2123. } else
  2124. goto signed_print;
  2125. fputc('\n', trace->output);
  2126. /*
  2127. * We only consider an 'event' for the sake of --max-events a non-filtered
  2128. * sys_enter + sys_exit and other tracepoint events.
  2129. */
  2130. if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
  2131. interrupted = true;
  2132. if (callchain_ret > 0)
  2133. trace__fprintf_callchain(trace, sample);
  2134. else if (callchain_ret < 0)
  2135. pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel));
  2136. out:
  2137. ttrace->entry_pending = false;
  2138. err = 0;
  2139. out_put:
  2140. thread__put(thread);
  2141. return err;
  2142. }
  2143. static int trace__vfs_getname(struct trace *trace, struct evsel *evsel,
  2144. union perf_event *event __maybe_unused,
  2145. struct perf_sample *sample)
  2146. {
  2147. struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2148. struct thread_trace *ttrace;
  2149. size_t filename_len, entry_str_len, to_move;
  2150. ssize_t remaining_space;
  2151. char *pos;
  2152. const char *filename = evsel__rawptr(evsel, sample, "pathname");
  2153. if (!thread)
  2154. goto out;
  2155. ttrace = thread__priv(thread);
  2156. if (!ttrace)
  2157. goto out_put;
  2158. filename_len = strlen(filename);
  2159. if (filename_len == 0)
  2160. goto out_put;
  2161. if (ttrace->filename.namelen < filename_len) {
  2162. char *f = realloc(ttrace->filename.name, filename_len + 1);
  2163. if (f == NULL)
  2164. goto out_put;
  2165. ttrace->filename.namelen = filename_len;
  2166. ttrace->filename.name = f;
  2167. }
  2168. strcpy(ttrace->filename.name, filename);
  2169. ttrace->filename.pending_open = true;
  2170. if (!ttrace->filename.ptr)
  2171. goto out_put;
  2172. entry_str_len = strlen(ttrace->entry_str);
  2173. remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
  2174. if (remaining_space <= 0)
  2175. goto out_put;
  2176. if (filename_len > (size_t)remaining_space) {
  2177. filename += filename_len - remaining_space;
  2178. filename_len = remaining_space;
  2179. }
  2180. to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
  2181. pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
  2182. memmove(pos + filename_len, pos, to_move);
  2183. memcpy(pos, filename, filename_len);
  2184. ttrace->filename.ptr = 0;
  2185. ttrace->filename.entry_str_pos = 0;
  2186. out_put:
  2187. thread__put(thread);
  2188. out:
  2189. return 0;
  2190. }
  2191. static int trace__sched_stat_runtime(struct trace *trace, struct evsel *evsel,
  2192. union perf_event *event __maybe_unused,
  2193. struct perf_sample *sample)
  2194. {
  2195. u64 runtime = evsel__intval(evsel, sample, "runtime");
  2196. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  2197. struct thread *thread = machine__findnew_thread(trace->host,
  2198. sample->pid,
  2199. sample->tid);
  2200. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  2201. if (ttrace == NULL)
  2202. goto out_dump;
  2203. ttrace->runtime_ms += runtime_ms;
  2204. trace->runtime_ms += runtime_ms;
  2205. out_put:
  2206. thread__put(thread);
  2207. return 0;
  2208. out_dump:
  2209. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  2210. evsel->name,
  2211. evsel__strval(evsel, sample, "comm"),
  2212. (pid_t)evsel__intval(evsel, sample, "pid"),
  2213. runtime,
  2214. evsel__intval(evsel, sample, "vruntime"));
  2215. goto out_put;
  2216. }
  2217. static int bpf_output__printer(enum binary_printer_ops op,
  2218. unsigned int val, void *extra __maybe_unused, FILE *fp)
  2219. {
  2220. unsigned char ch = (unsigned char)val;
  2221. switch (op) {
  2222. case BINARY_PRINT_CHAR_DATA:
  2223. return fprintf(fp, "%c", isprint(ch) ? ch : '.');
  2224. case BINARY_PRINT_DATA_BEGIN:
  2225. case BINARY_PRINT_LINE_BEGIN:
  2226. case BINARY_PRINT_ADDR:
  2227. case BINARY_PRINT_NUM_DATA:
  2228. case BINARY_PRINT_NUM_PAD:
  2229. case BINARY_PRINT_SEP:
  2230. case BINARY_PRINT_CHAR_PAD:
  2231. case BINARY_PRINT_LINE_END:
  2232. case BINARY_PRINT_DATA_END:
  2233. default:
  2234. break;
  2235. }
  2236. return 0;
  2237. }
  2238. static void bpf_output__fprintf(struct trace *trace,
  2239. struct perf_sample *sample)
  2240. {
  2241. binary__fprintf(sample->raw_data, sample->raw_size, 8,
  2242. bpf_output__printer, NULL, trace->output);
  2243. ++trace->nr_events_printed;
  2244. }
  2245. static size_t trace__fprintf_tp_fields(struct trace *trace, struct evsel *evsel, struct perf_sample *sample,
  2246. struct thread *thread, void *augmented_args, int augmented_args_size)
  2247. {
  2248. char bf[2048];
  2249. size_t size = sizeof(bf);
  2250. struct tep_format_field *field = evsel->tp_format->format.fields;
  2251. struct syscall_arg_fmt *arg = __evsel__syscall_arg_fmt(evsel);
  2252. size_t printed = 0;
  2253. unsigned long val;
  2254. u8 bit = 1;
  2255. struct syscall_arg syscall_arg = {
  2256. .augmented = {
  2257. .size = augmented_args_size,
  2258. .args = augmented_args,
  2259. },
  2260. .idx = 0,
  2261. .mask = 0,
  2262. .trace = trace,
  2263. .thread = thread,
  2264. .show_string_prefix = trace->show_string_prefix,
  2265. };
  2266. for (; field && arg; field = field->next, ++syscall_arg.idx, bit <<= 1, ++arg) {
  2267. if (syscall_arg.mask & bit)
  2268. continue;
  2269. syscall_arg.len = 0;
  2270. syscall_arg.fmt = arg;
  2271. if (field->flags & TEP_FIELD_IS_ARRAY) {
  2272. int offset = field->offset;
  2273. if (field->flags & TEP_FIELD_IS_DYNAMIC) {
  2274. offset = format_field__intval(field, sample, evsel->needs_swap);
  2275. syscall_arg.len = offset >> 16;
  2276. offset &= 0xffff;
  2277. }
  2278. val = (uintptr_t)(sample->raw_data + offset);
  2279. } else
  2280. val = format_field__intval(field, sample, evsel->needs_swap);
  2281. /*
  2282. * Some syscall args need some mask, most don't and
  2283. * return val untouched.
  2284. */
  2285. val = syscall_arg_fmt__mask_val(arg, &syscall_arg, val);
  2286. /*
  2287. * Suppress this argument if its value is zero and
  2288. * and we don't have a string associated in an
  2289. * strarray for it.
  2290. */
  2291. if (val == 0 &&
  2292. !trace->show_zeros &&
  2293. !((arg->show_zero ||
  2294. arg->scnprintf == SCA_STRARRAY ||
  2295. arg->scnprintf == SCA_STRARRAYS) &&
  2296. arg->parm))
  2297. continue;
  2298. printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
  2299. /*
  2300. * XXX Perhaps we should have a show_tp_arg_names,
  2301. * leaving show_arg_names just for syscalls?
  2302. */
  2303. if (1 || trace->show_arg_names)
  2304. printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
  2305. printed += syscall_arg_fmt__scnprintf_val(arg, bf + printed, size - printed, &syscall_arg, val);
  2306. }
  2307. return printed + fprintf(trace->output, "%s", bf);
  2308. }
  2309. static int trace__event_handler(struct trace *trace, struct evsel *evsel,
  2310. union perf_event *event __maybe_unused,
  2311. struct perf_sample *sample)
  2312. {
  2313. struct thread *thread;
  2314. int callchain_ret = 0;
  2315. /*
  2316. * Check if we called perf_evsel__disable(evsel) due to, for instance,
  2317. * this event's max_events having been hit and this is an entry coming
  2318. * from the ring buffer that we should discard, since the max events
  2319. * have already been considered/printed.
  2320. */
  2321. if (evsel->disabled)
  2322. return 0;
  2323. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2324. if (sample->callchain) {
  2325. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  2326. if (callchain_ret == 0) {
  2327. if (callchain_cursor.nr < trace->min_stack)
  2328. goto out;
  2329. callchain_ret = 1;
  2330. }
  2331. }
  2332. trace__printf_interrupted_entry(trace);
  2333. trace__fprintf_tstamp(trace, sample->time, trace->output);
  2334. if (trace->trace_syscalls && trace->show_duration)
  2335. fprintf(trace->output, "( ): ");
  2336. if (thread)
  2337. trace__fprintf_comm_tid(trace, thread, trace->output);
  2338. if (evsel == trace->syscalls.events.augmented) {
  2339. int id = perf_evsel__sc_tp_uint(evsel, id, sample);
  2340. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  2341. if (sc) {
  2342. fprintf(trace->output, "%s(", sc->name);
  2343. trace__fprintf_sys_enter(trace, evsel, sample);
  2344. fputc(')', trace->output);
  2345. goto newline;
  2346. }
  2347. /*
  2348. * XXX: Not having the associated syscall info or not finding/adding
  2349. * the thread should never happen, but if it does...
  2350. * fall thru and print it as a bpf_output event.
  2351. */
  2352. }
  2353. fprintf(trace->output, "%s(", evsel->name);
  2354. if (evsel__is_bpf_output(evsel)) {
  2355. bpf_output__fprintf(trace, sample);
  2356. } else if (evsel->tp_format) {
  2357. if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
  2358. trace__fprintf_sys_enter(trace, evsel, sample)) {
  2359. if (trace->libtraceevent_print) {
  2360. event_format__fprintf(evsel->tp_format, sample->cpu,
  2361. sample->raw_data, sample->raw_size,
  2362. trace->output);
  2363. } else {
  2364. trace__fprintf_tp_fields(trace, evsel, sample, thread, NULL, 0);
  2365. }
  2366. }
  2367. }
  2368. newline:
  2369. fprintf(trace->output, ")\n");
  2370. if (callchain_ret > 0)
  2371. trace__fprintf_callchain(trace, sample);
  2372. else if (callchain_ret < 0)
  2373. pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel));
  2374. ++trace->nr_events_printed;
  2375. if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
  2376. evsel__disable(evsel);
  2377. evsel__close(evsel);
  2378. }
  2379. out:
  2380. thread__put(thread);
  2381. return 0;
  2382. }
  2383. static void print_location(FILE *f, struct perf_sample *sample,
  2384. struct addr_location *al,
  2385. bool print_dso, bool print_sym)
  2386. {
  2387. if ((verbose > 0 || print_dso) && al->map)
  2388. fprintf(f, "%s@", al->map->dso->long_name);
  2389. if ((verbose > 0 || print_sym) && al->sym)
  2390. fprintf(f, "%s+0x%" PRIx64, al->sym->name,
  2391. al->addr - al->sym->start);
  2392. else if (al->map)
  2393. fprintf(f, "0x%" PRIx64, al->addr);
  2394. else
  2395. fprintf(f, "0x%" PRIx64, sample->addr);
  2396. }
  2397. static int trace__pgfault(struct trace *trace,
  2398. struct evsel *evsel,
  2399. union perf_event *event __maybe_unused,
  2400. struct perf_sample *sample)
  2401. {
  2402. struct thread *thread;
  2403. struct addr_location al;
  2404. char map_type = 'd';
  2405. struct thread_trace *ttrace;
  2406. int err = -1;
  2407. int callchain_ret = 0;
  2408. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2409. if (sample->callchain) {
  2410. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  2411. if (callchain_ret == 0) {
  2412. if (callchain_cursor.nr < trace->min_stack)
  2413. goto out_put;
  2414. callchain_ret = 1;
  2415. }
  2416. }
  2417. ttrace = thread__trace(thread, trace->output);
  2418. if (ttrace == NULL)
  2419. goto out_put;
  2420. if (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
  2421. ttrace->pfmaj++;
  2422. else
  2423. ttrace->pfmin++;
  2424. if (trace->summary_only)
  2425. goto out;
  2426. thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
  2427. trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
  2428. fprintf(trace->output, "%sfault [",
  2429. evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
  2430. "maj" : "min");
  2431. print_location(trace->output, sample, &al, false, true);
  2432. fprintf(trace->output, "] => ");
  2433. thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
  2434. if (!al.map) {
  2435. thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
  2436. if (al.map)
  2437. map_type = 'x';
  2438. else
  2439. map_type = '?';
  2440. }
  2441. print_location(trace->output, sample, &al, true, false);
  2442. fprintf(trace->output, " (%c%c)\n", map_type, al.level);
  2443. if (callchain_ret > 0)
  2444. trace__fprintf_callchain(trace, sample);
  2445. else if (callchain_ret < 0)
  2446. pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel));
  2447. ++trace->nr_events_printed;
  2448. out:
  2449. err = 0;
  2450. out_put:
  2451. thread__put(thread);
  2452. return err;
  2453. }
  2454. static void trace__set_base_time(struct trace *trace,
  2455. struct evsel *evsel,
  2456. struct perf_sample *sample)
  2457. {
  2458. /*
  2459. * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
  2460. * and don't use sample->time unconditionally, we may end up having
  2461. * some other event in the future without PERF_SAMPLE_TIME for good
  2462. * reason, i.e. we may not be interested in its timestamps, just in
  2463. * it taking place, picking some piece of information when it
  2464. * appears in our event stream (vfs_getname comes to mind).
  2465. */
  2466. if (trace->base_time == 0 && !trace->full_time &&
  2467. (evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
  2468. trace->base_time = sample->time;
  2469. }
  2470. static int trace__process_sample(struct perf_tool *tool,
  2471. union perf_event *event,
  2472. struct perf_sample *sample,
  2473. struct evsel *evsel,
  2474. struct machine *machine __maybe_unused)
  2475. {
  2476. struct trace *trace = container_of(tool, struct trace, tool);
  2477. struct thread *thread;
  2478. int err = 0;
  2479. tracepoint_handler handler = evsel->handler;
  2480. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2481. if (thread && thread__is_filtered(thread))
  2482. goto out;
  2483. trace__set_base_time(trace, evsel, sample);
  2484. if (handler) {
  2485. ++trace->nr_events;
  2486. handler(trace, evsel, event, sample);
  2487. }
  2488. out:
  2489. thread__put(thread);
  2490. return err;
  2491. }
  2492. static int trace__record(struct trace *trace, int argc, const char **argv)
  2493. {
  2494. unsigned int rec_argc, i, j;
  2495. const char **rec_argv;
  2496. const char * const record_args[] = {
  2497. "record",
  2498. "-R",
  2499. "-m", "1024",
  2500. "-c", "1",
  2501. };
  2502. pid_t pid = getpid();
  2503. char *filter = asprintf__tp_filter_pids(1, &pid);
  2504. const char * const sc_args[] = { "-e", };
  2505. unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
  2506. const char * const majpf_args[] = { "-e", "major-faults" };
  2507. unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
  2508. const char * const minpf_args[] = { "-e", "minor-faults" };
  2509. unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
  2510. int err = -1;
  2511. /* +3 is for the event string below and the pid filter */
  2512. rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 3 +
  2513. majpf_args_nr + minpf_args_nr + argc;
  2514. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  2515. if (rec_argv == NULL || filter == NULL)
  2516. goto out_free;
  2517. j = 0;
  2518. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  2519. rec_argv[j++] = record_args[i];
  2520. if (trace->trace_syscalls) {
  2521. for (i = 0; i < sc_args_nr; i++)
  2522. rec_argv[j++] = sc_args[i];
  2523. /* event string may be different for older kernels - e.g., RHEL6 */
  2524. if (is_valid_tracepoint("raw_syscalls:sys_enter"))
  2525. rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
  2526. else if (is_valid_tracepoint("syscalls:sys_enter"))
  2527. rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
  2528. else {
  2529. pr_err("Neither raw_syscalls nor syscalls events exist.\n");
  2530. goto out_free;
  2531. }
  2532. }
  2533. rec_argv[j++] = "--filter";
  2534. rec_argv[j++] = filter;
  2535. if (trace->trace_pgfaults & TRACE_PFMAJ)
  2536. for (i = 0; i < majpf_args_nr; i++)
  2537. rec_argv[j++] = majpf_args[i];
  2538. if (trace->trace_pgfaults & TRACE_PFMIN)
  2539. for (i = 0; i < minpf_args_nr; i++)
  2540. rec_argv[j++] = minpf_args[i];
  2541. for (i = 0; i < (unsigned int)argc; i++)
  2542. rec_argv[j++] = argv[i];
  2543. err = cmd_record(j, rec_argv);
  2544. out_free:
  2545. free(filter);
  2546. free(rec_argv);
  2547. return err;
  2548. }
  2549. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
  2550. static bool evlist__add_vfs_getname(struct evlist *evlist)
  2551. {
  2552. bool found = false;
  2553. struct evsel *evsel, *tmp;
  2554. struct parse_events_error err;
  2555. int ret;
  2556. bzero(&err, sizeof(err));
  2557. ret = parse_events(evlist, "probe:vfs_getname*", &err);
  2558. if (ret) {
  2559. free(err.str);
  2560. free(err.help);
  2561. free(err.first_str);
  2562. free(err.first_help);
  2563. return false;
  2564. }
  2565. evlist__for_each_entry_safe(evlist, evsel, tmp) {
  2566. if (!strstarts(evsel__name(evsel), "probe:vfs_getname"))
  2567. continue;
  2568. if (evsel__field(evsel, "pathname")) {
  2569. evsel->handler = trace__vfs_getname;
  2570. found = true;
  2571. continue;
  2572. }
  2573. list_del_init(&evsel->core.node);
  2574. evsel->evlist = NULL;
  2575. evsel__delete(evsel);
  2576. }
  2577. return found;
  2578. }
  2579. static struct evsel *evsel__new_pgfault(u64 config)
  2580. {
  2581. struct evsel *evsel;
  2582. struct perf_event_attr attr = {
  2583. .type = PERF_TYPE_SOFTWARE,
  2584. .mmap_data = 1,
  2585. };
  2586. attr.config = config;
  2587. attr.sample_period = 1;
  2588. event_attr_init(&attr);
  2589. evsel = evsel__new(&attr);
  2590. if (evsel)
  2591. evsel->handler = trace__pgfault;
  2592. return evsel;
  2593. }
  2594. static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
  2595. {
  2596. const u32 type = event->header.type;
  2597. struct evsel *evsel;
  2598. if (type != PERF_RECORD_SAMPLE) {
  2599. trace__process_event(trace, trace->host, event, sample);
  2600. return;
  2601. }
  2602. evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
  2603. if (evsel == NULL) {
  2604. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
  2605. return;
  2606. }
  2607. if (evswitch__discard(&trace->evswitch, evsel))
  2608. return;
  2609. trace__set_base_time(trace, evsel, sample);
  2610. if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
  2611. sample->raw_data == NULL) {
  2612. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  2613. evsel__name(evsel), sample->tid,
  2614. sample->cpu, sample->raw_size);
  2615. } else {
  2616. tracepoint_handler handler = evsel->handler;
  2617. handler(trace, evsel, event, sample);
  2618. }
  2619. if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
  2620. interrupted = true;
  2621. }
  2622. static int trace__add_syscall_newtp(struct trace *trace)
  2623. {
  2624. int ret = -1;
  2625. struct evlist *evlist = trace->evlist;
  2626. struct evsel *sys_enter, *sys_exit;
  2627. sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
  2628. if (sys_enter == NULL)
  2629. goto out;
  2630. if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
  2631. goto out_delete_sys_enter;
  2632. sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
  2633. if (sys_exit == NULL)
  2634. goto out_delete_sys_enter;
  2635. if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
  2636. goto out_delete_sys_exit;
  2637. evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
  2638. evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
  2639. evlist__add(evlist, sys_enter);
  2640. evlist__add(evlist, sys_exit);
  2641. if (callchain_param.enabled && !trace->kernel_syscallchains) {
  2642. /*
  2643. * We're interested only in the user space callchain
  2644. * leading to the syscall, allow overriding that for
  2645. * debugging reasons using --kernel_syscall_callchains
  2646. */
  2647. sys_exit->core.attr.exclude_callchain_kernel = 1;
  2648. }
  2649. trace->syscalls.events.sys_enter = sys_enter;
  2650. trace->syscalls.events.sys_exit = sys_exit;
  2651. ret = 0;
  2652. out:
  2653. return ret;
  2654. out_delete_sys_exit:
  2655. evsel__delete_priv(sys_exit);
  2656. out_delete_sys_enter:
  2657. evsel__delete_priv(sys_enter);
  2658. goto out;
  2659. }
  2660. static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
  2661. {
  2662. int err = -1;
  2663. struct evsel *sys_exit;
  2664. char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
  2665. trace->ev_qualifier_ids.nr,
  2666. trace->ev_qualifier_ids.entries);
  2667. if (filter == NULL)
  2668. goto out_enomem;
  2669. if (!evsel__append_tp_filter(trace->syscalls.events.sys_enter, filter)) {
  2670. sys_exit = trace->syscalls.events.sys_exit;
  2671. err = evsel__append_tp_filter(sys_exit, filter);
  2672. }
  2673. free(filter);
  2674. out:
  2675. return err;
  2676. out_enomem:
  2677. errno = ENOMEM;
  2678. goto out;
  2679. }
  2680. #ifdef HAVE_LIBBPF_SUPPORT
  2681. static struct bpf_map *trace__find_bpf_map_by_name(struct trace *trace, const char *name)
  2682. {
  2683. if (trace->bpf_obj == NULL)
  2684. return NULL;
  2685. return bpf_object__find_map_by_name(trace->bpf_obj, name);
  2686. }
  2687. static void trace__set_bpf_map_filtered_pids(struct trace *trace)
  2688. {
  2689. trace->filter_pids.map = trace__find_bpf_map_by_name(trace, "pids_filtered");
  2690. }
  2691. static void trace__set_bpf_map_syscalls(struct trace *trace)
  2692. {
  2693. trace->syscalls.map = trace__find_bpf_map_by_name(trace, "syscalls");
  2694. trace->syscalls.prog_array.sys_enter = trace__find_bpf_map_by_name(trace, "syscalls_sys_enter");
  2695. trace->syscalls.prog_array.sys_exit = trace__find_bpf_map_by_name(trace, "syscalls_sys_exit");
  2696. }
  2697. static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace, const char *name)
  2698. {
  2699. if (trace->bpf_obj == NULL)
  2700. return NULL;
  2701. return bpf_object__find_program_by_title(trace->bpf_obj, name);
  2702. }
  2703. static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace, struct syscall *sc,
  2704. const char *prog_name, const char *type)
  2705. {
  2706. struct bpf_program *prog;
  2707. if (prog_name == NULL) {
  2708. char default_prog_name[256];
  2709. scnprintf(default_prog_name, sizeof(default_prog_name), "!syscalls:sys_%s_%s", type, sc->name);
  2710. prog = trace__find_bpf_program_by_title(trace, default_prog_name);
  2711. if (prog != NULL)
  2712. goto out_found;
  2713. if (sc->fmt && sc->fmt->alias) {
  2714. scnprintf(default_prog_name, sizeof(default_prog_name), "!syscalls:sys_%s_%s", type, sc->fmt->alias);
  2715. prog = trace__find_bpf_program_by_title(trace, default_prog_name);
  2716. if (prog != NULL)
  2717. goto out_found;
  2718. }
  2719. goto out_unaugmented;
  2720. }
  2721. prog = trace__find_bpf_program_by_title(trace, prog_name);
  2722. if (prog != NULL) {
  2723. out_found:
  2724. return prog;
  2725. }
  2726. pr_debug("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n",
  2727. prog_name, type, sc->name);
  2728. out_unaugmented:
  2729. return trace->syscalls.unaugmented_prog;
  2730. }
  2731. static void trace__init_syscall_bpf_progs(struct trace *trace, int id)
  2732. {
  2733. struct syscall *sc = trace__syscall_info(trace, NULL, id);
  2734. if (sc == NULL)
  2735. return;
  2736. sc->bpf_prog.sys_enter = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_enter : NULL, "enter");
  2737. sc->bpf_prog.sys_exit = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_exit : NULL, "exit");
  2738. }
  2739. static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int id)
  2740. {
  2741. struct syscall *sc = trace__syscall_info(trace, NULL, id);
  2742. return sc ? bpf_program__fd(sc->bpf_prog.sys_enter) : bpf_program__fd(trace->syscalls.unaugmented_prog);
  2743. }
  2744. static int trace__bpf_prog_sys_exit_fd(struct trace *trace, int id)
  2745. {
  2746. struct syscall *sc = trace__syscall_info(trace, NULL, id);
  2747. return sc ? bpf_program__fd(sc->bpf_prog.sys_exit) : bpf_program__fd(trace->syscalls.unaugmented_prog);
  2748. }
  2749. static void trace__init_bpf_map_syscall_args(struct trace *trace, int id, struct bpf_map_syscall_entry *entry)
  2750. {
  2751. struct syscall *sc = trace__syscall_info(trace, NULL, id);
  2752. int arg = 0;
  2753. if (sc == NULL)
  2754. goto out;
  2755. for (; arg < sc->nr_args; ++arg) {
  2756. entry->string_args_len[arg] = 0;
  2757. if (sc->arg_fmt[arg].scnprintf == SCA_FILENAME) {
  2758. /* Should be set like strace -s strsize */
  2759. entry->string_args_len[arg] = PATH_MAX;
  2760. }
  2761. }
  2762. out:
  2763. for (; arg < 6; ++arg)
  2764. entry->string_args_len[arg] = 0;
  2765. }
  2766. static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
  2767. {
  2768. int fd = bpf_map__fd(trace->syscalls.map);
  2769. struct bpf_map_syscall_entry value = {
  2770. .enabled = !trace->not_ev_qualifier,
  2771. };
  2772. int err = 0;
  2773. size_t i;
  2774. for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
  2775. int key = trace->ev_qualifier_ids.entries[i];
  2776. if (value.enabled) {
  2777. trace__init_bpf_map_syscall_args(trace, key, &value);
  2778. trace__init_syscall_bpf_progs(trace, key);
  2779. }
  2780. err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
  2781. if (err)
  2782. break;
  2783. }
  2784. return err;
  2785. }
  2786. static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
  2787. {
  2788. int fd = bpf_map__fd(trace->syscalls.map);
  2789. struct bpf_map_syscall_entry value = {
  2790. .enabled = enabled,
  2791. };
  2792. int err = 0, key;
  2793. for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
  2794. if (enabled)
  2795. trace__init_bpf_map_syscall_args(trace, key, &value);
  2796. err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
  2797. if (err)
  2798. break;
  2799. }
  2800. return err;
  2801. }
  2802. static int trace__init_syscalls_bpf_map(struct trace *trace)
  2803. {
  2804. bool enabled = true;
  2805. if (trace->ev_qualifier_ids.nr)
  2806. enabled = trace->not_ev_qualifier;
  2807. return __trace__init_syscalls_bpf_map(trace, enabled);
  2808. }
  2809. static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace, struct syscall *sc)
  2810. {
  2811. struct tep_format_field *field, *candidate_field;
  2812. int id;
  2813. /*
  2814. * We're only interested in syscalls that have a pointer:
  2815. */
  2816. for (field = sc->args; field; field = field->next) {
  2817. if (field->flags & TEP_FIELD_IS_POINTER)
  2818. goto try_to_find_pair;
  2819. }
  2820. return NULL;
  2821. try_to_find_pair:
  2822. for (id = 0; id < trace->sctbl->syscalls.nr_entries; ++id) {
  2823. struct syscall *pair = trace__syscall_info(trace, NULL, id);
  2824. struct bpf_program *pair_prog;
  2825. bool is_candidate = false;
  2826. if (pair == NULL || pair == sc ||
  2827. pair->bpf_prog.sys_enter == trace->syscalls.unaugmented_prog)
  2828. continue;
  2829. for (field = sc->args, candidate_field = pair->args;
  2830. field && candidate_field; field = field->next, candidate_field = candidate_field->next) {
  2831. bool is_pointer = field->flags & TEP_FIELD_IS_POINTER,
  2832. candidate_is_pointer = candidate_field->flags & TEP_FIELD_IS_POINTER;
  2833. if (is_pointer) {
  2834. if (!candidate_is_pointer) {
  2835. // The candidate just doesn't copies our pointer arg, might copy other pointers we want.
  2836. continue;
  2837. }
  2838. } else {
  2839. if (candidate_is_pointer) {
  2840. // The candidate might copy a pointer we don't have, skip it.
  2841. goto next_candidate;
  2842. }
  2843. continue;
  2844. }
  2845. if (strcmp(field->type, candidate_field->type))
  2846. goto next_candidate;
  2847. is_candidate = true;
  2848. }
  2849. if (!is_candidate)
  2850. goto next_candidate;
  2851. /*
  2852. * Check if the tentative pair syscall augmenter has more pointers, if it has,
  2853. * then it may be collecting that and we then can't use it, as it would collect
  2854. * more than what is common to the two syscalls.
  2855. */
  2856. if (candidate_field) {
  2857. for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next)
  2858. if (candidate_field->flags & TEP_FIELD_IS_POINTER)
  2859. goto next_candidate;
  2860. }
  2861. pair_prog = pair->bpf_prog.sys_enter;
  2862. /*
  2863. * If the pair isn't enabled, then its bpf_prog.sys_enter will not
  2864. * have been searched for, so search it here and if it returns the
  2865. * unaugmented one, then ignore it, otherwise we'll reuse that BPF
  2866. * program for a filtered syscall on a non-filtered one.
  2867. *
  2868. * For instance, we have "!syscalls:sys_enter_renameat" and that is
  2869. * useful for "renameat2".
  2870. */
  2871. if (pair_prog == NULL) {
  2872. pair_prog = trace__find_syscall_bpf_prog(trace, pair, pair->fmt ? pair->fmt->bpf_prog_name.sys_enter : NULL, "enter");
  2873. if (pair_prog == trace->syscalls.unaugmented_prog)
  2874. goto next_candidate;
  2875. }
  2876. pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair->name, sc->name);
  2877. return pair_prog;
  2878. next_candidate:
  2879. continue;
  2880. }
  2881. return NULL;
  2882. }
  2883. static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace)
  2884. {
  2885. int map_enter_fd = bpf_map__fd(trace->syscalls.prog_array.sys_enter),
  2886. map_exit_fd = bpf_map__fd(trace->syscalls.prog_array.sys_exit);
  2887. int err = 0, key;
  2888. for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
  2889. int prog_fd;
  2890. if (!trace__syscall_enabled(trace, key))
  2891. continue;
  2892. trace__init_syscall_bpf_progs(trace, key);
  2893. // It'll get at least the "!raw_syscalls:unaugmented"
  2894. prog_fd = trace__bpf_prog_sys_enter_fd(trace, key);
  2895. err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY);
  2896. if (err)
  2897. break;
  2898. prog_fd = trace__bpf_prog_sys_exit_fd(trace, key);
  2899. err = bpf_map_update_elem(map_exit_fd, &key, &prog_fd, BPF_ANY);
  2900. if (err)
  2901. break;
  2902. }
  2903. /*
  2904. * Now lets do a second pass looking for enabled syscalls without
  2905. * an augmenter that have a signature that is a superset of another
  2906. * syscall with an augmenter so that we can auto-reuse it.
  2907. *
  2908. * I.e. if we have an augmenter for the "open" syscall that has
  2909. * this signature:
  2910. *
  2911. * int open(const char *pathname, int flags, mode_t mode);
  2912. *
  2913. * I.e. that will collect just the first string argument, then we
  2914. * can reuse it for the 'creat' syscall, that has this signature:
  2915. *
  2916. * int creat(const char *pathname, mode_t mode);
  2917. *
  2918. * and for:
  2919. *
  2920. * int stat(const char *pathname, struct stat *statbuf);
  2921. * int lstat(const char *pathname, struct stat *statbuf);
  2922. *
  2923. * Because the 'open' augmenter will collect the first arg as a string,
  2924. * and leave alone all the other args, which already helps with
  2925. * beautifying 'stat' and 'lstat''s pathname arg.
  2926. *
  2927. * Then, in time, when 'stat' gets an augmenter that collects both
  2928. * first and second arg (this one on the raw_syscalls:sys_exit prog
  2929. * array tail call, then that one will be used.
  2930. */
  2931. for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
  2932. struct syscall *sc = trace__syscall_info(trace, NULL, key);
  2933. struct bpf_program *pair_prog;
  2934. int prog_fd;
  2935. if (sc == NULL || sc->bpf_prog.sys_enter == NULL)
  2936. continue;
  2937. /*
  2938. * For now we're just reusing the sys_enter prog, and if it
  2939. * already has an augmenter, we don't need to find one.
  2940. */
  2941. if (sc->bpf_prog.sys_enter != trace->syscalls.unaugmented_prog)
  2942. continue;
  2943. /*
  2944. * Look at all the other syscalls for one that has a signature
  2945. * that is close enough that we can share:
  2946. */
  2947. pair_prog = trace__find_usable_bpf_prog_entry(trace, sc);
  2948. if (pair_prog == NULL)
  2949. continue;
  2950. sc->bpf_prog.sys_enter = pair_prog;
  2951. /*
  2952. * Update the BPF_MAP_TYPE_PROG_SHARED for raw_syscalls:sys_enter
  2953. * with the fd for the program we're reusing:
  2954. */
  2955. prog_fd = bpf_program__fd(sc->bpf_prog.sys_enter);
  2956. err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY);
  2957. if (err)
  2958. break;
  2959. }
  2960. return err;
  2961. }
  2962. static void trace__delete_augmented_syscalls(struct trace *trace)
  2963. {
  2964. struct evsel *evsel, *tmp;
  2965. evlist__remove(trace->evlist, trace->syscalls.events.augmented);
  2966. evsel__delete(trace->syscalls.events.augmented);
  2967. trace->syscalls.events.augmented = NULL;
  2968. evlist__for_each_entry_safe(trace->evlist, tmp, evsel) {
  2969. if (evsel->bpf_obj == trace->bpf_obj) {
  2970. evlist__remove(trace->evlist, evsel);
  2971. evsel__delete(evsel);
  2972. }
  2973. }
  2974. bpf_object__close(trace->bpf_obj);
  2975. trace->bpf_obj = NULL;
  2976. }
  2977. #else // HAVE_LIBBPF_SUPPORT
  2978. static struct bpf_map *trace__find_bpf_map_by_name(struct trace *trace __maybe_unused,
  2979. const char *name __maybe_unused)
  2980. {
  2981. return NULL;
  2982. }
  2983. static void trace__set_bpf_map_filtered_pids(struct trace *trace __maybe_unused)
  2984. {
  2985. }
  2986. static void trace__set_bpf_map_syscalls(struct trace *trace __maybe_unused)
  2987. {
  2988. }
  2989. static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
  2990. {
  2991. return 0;
  2992. }
  2993. static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
  2994. {
  2995. return 0;
  2996. }
  2997. static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace __maybe_unused,
  2998. const char *name __maybe_unused)
  2999. {
  3000. return NULL;
  3001. }
  3002. static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace __maybe_unused)
  3003. {
  3004. return 0;
  3005. }
  3006. static void trace__delete_augmented_syscalls(struct trace *trace __maybe_unused)
  3007. {
  3008. }
  3009. #endif // HAVE_LIBBPF_SUPPORT
  3010. static bool trace__only_augmented_syscalls_evsels(struct trace *trace)
  3011. {
  3012. struct evsel *evsel;
  3013. evlist__for_each_entry(trace->evlist, evsel) {
  3014. if (evsel == trace->syscalls.events.augmented ||
  3015. evsel->bpf_obj == trace->bpf_obj)
  3016. continue;
  3017. return false;
  3018. }
  3019. return true;
  3020. }
  3021. static int trace__set_ev_qualifier_filter(struct trace *trace)
  3022. {
  3023. if (trace->syscalls.map)
  3024. return trace__set_ev_qualifier_bpf_filter(trace);
  3025. if (trace->syscalls.events.sys_enter)
  3026. return trace__set_ev_qualifier_tp_filter(trace);
  3027. return 0;
  3028. }
  3029. static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
  3030. size_t npids __maybe_unused, pid_t *pids __maybe_unused)
  3031. {
  3032. int err = 0;
  3033. #ifdef HAVE_LIBBPF_SUPPORT
  3034. bool value = true;
  3035. int map_fd = bpf_map__fd(map);
  3036. size_t i;
  3037. for (i = 0; i < npids; ++i) {
  3038. err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
  3039. if (err)
  3040. break;
  3041. }
  3042. #endif
  3043. return err;
  3044. }
  3045. static int trace__set_filter_loop_pids(struct trace *trace)
  3046. {
  3047. unsigned int nr = 1, err;
  3048. pid_t pids[32] = {
  3049. getpid(),
  3050. };
  3051. struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
  3052. while (thread && nr < ARRAY_SIZE(pids)) {
  3053. struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
  3054. if (parent == NULL)
  3055. break;
  3056. if (!strcmp(thread__comm_str(parent), "sshd") ||
  3057. strstarts(thread__comm_str(parent), "gnome-terminal")) {
  3058. pids[nr++] = parent->tid;
  3059. break;
  3060. }
  3061. thread = parent;
  3062. }
  3063. err = perf_evlist__append_tp_filter_pids(trace->evlist, nr, pids);
  3064. if (!err && trace->filter_pids.map)
  3065. err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
  3066. return err;
  3067. }
  3068. static int trace__set_filter_pids(struct trace *trace)
  3069. {
  3070. int err = 0;
  3071. /*
  3072. * Better not use !target__has_task() here because we need to cover the
  3073. * case where no threads were specified in the command line, but a
  3074. * workload was, and in that case we will fill in the thread_map when
  3075. * we fork the workload in perf_evlist__prepare_workload.
  3076. */
  3077. if (trace->filter_pids.nr > 0) {
  3078. err = perf_evlist__append_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
  3079. trace->filter_pids.entries);
  3080. if (!err && trace->filter_pids.map) {
  3081. err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
  3082. trace->filter_pids.entries);
  3083. }
  3084. } else if (perf_thread_map__pid(trace->evlist->core.threads, 0) == -1) {
  3085. err = trace__set_filter_loop_pids(trace);
  3086. }
  3087. return err;
  3088. }
  3089. static int __trace__deliver_event(struct trace *trace, union perf_event *event)
  3090. {
  3091. struct evlist *evlist = trace->evlist;
  3092. struct perf_sample sample;
  3093. int err;
  3094. err = perf_evlist__parse_sample(evlist, event, &sample);
  3095. if (err)
  3096. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  3097. else
  3098. trace__handle_event(trace, event, &sample);
  3099. return 0;
  3100. }
  3101. static int __trace__flush_events(struct trace *trace)
  3102. {
  3103. u64 first = ordered_events__first_time(&trace->oe.data);
  3104. u64 flush = trace->oe.last - NSEC_PER_SEC;
  3105. /* Is there some thing to flush.. */
  3106. if (first && first < flush)
  3107. return ordered_events__flush_time(&trace->oe.data, flush);
  3108. return 0;
  3109. }
  3110. static int trace__flush_events(struct trace *trace)
  3111. {
  3112. return !trace->sort_events ? 0 : __trace__flush_events(trace);
  3113. }
  3114. static int trace__deliver_event(struct trace *trace, union perf_event *event)
  3115. {
  3116. int err;
  3117. if (!trace->sort_events)
  3118. return __trace__deliver_event(trace, event);
  3119. err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
  3120. if (err && err != -1)
  3121. return err;
  3122. err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
  3123. if (err)
  3124. return err;
  3125. return trace__flush_events(trace);
  3126. }
  3127. static int ordered_events__deliver_event(struct ordered_events *oe,
  3128. struct ordered_event *event)
  3129. {
  3130. struct trace *trace = container_of(oe, struct trace, oe.data);
  3131. return __trace__deliver_event(trace, event->event);
  3132. }
  3133. static struct syscall_arg_fmt *evsel__find_syscall_arg_fmt_by_name(struct evsel *evsel, char *arg)
  3134. {
  3135. struct tep_format_field *field;
  3136. struct syscall_arg_fmt *fmt = __evsel__syscall_arg_fmt(evsel);
  3137. if (evsel->tp_format == NULL || fmt == NULL)
  3138. return NULL;
  3139. for (field = evsel->tp_format->format.fields; field; field = field->next, ++fmt)
  3140. if (strcmp(field->name, arg) == 0)
  3141. return fmt;
  3142. return NULL;
  3143. }
  3144. static int trace__expand_filter(struct trace *trace __maybe_unused, struct evsel *evsel)
  3145. {
  3146. char *tok, *left = evsel->filter, *new_filter = evsel->filter;
  3147. while ((tok = strpbrk(left, "=<>!")) != NULL) {
  3148. char *right = tok + 1, *right_end;
  3149. if (*right == '=')
  3150. ++right;
  3151. while (isspace(*right))
  3152. ++right;
  3153. if (*right == '\0')
  3154. break;
  3155. while (!isalpha(*left))
  3156. if (++left == tok) {
  3157. /*
  3158. * Bail out, can't find the name of the argument that is being
  3159. * used in the filter, let it try to set this filter, will fail later.
  3160. */
  3161. return 0;
  3162. }
  3163. right_end = right + 1;
  3164. while (isalnum(*right_end) || *right_end == '_' || *right_end == '|')
  3165. ++right_end;
  3166. if (isalpha(*right)) {
  3167. struct syscall_arg_fmt *fmt;
  3168. int left_size = tok - left,
  3169. right_size = right_end - right;
  3170. char arg[128];
  3171. while (isspace(left[left_size - 1]))
  3172. --left_size;
  3173. scnprintf(arg, sizeof(arg), "%.*s", left_size, left);
  3174. fmt = evsel__find_syscall_arg_fmt_by_name(evsel, arg);
  3175. if (fmt == NULL) {
  3176. pr_err("\"%s\" not found in \"%s\", can't set filter \"%s\"\n",
  3177. arg, evsel->name, evsel->filter);
  3178. return -1;
  3179. }
  3180. pr_debug2("trying to expand \"%s\" \"%.*s\" \"%.*s\" -> ",
  3181. arg, (int)(right - tok), tok, right_size, right);
  3182. if (fmt->strtoul) {
  3183. u64 val;
  3184. struct syscall_arg syscall_arg = {
  3185. .parm = fmt->parm,
  3186. };
  3187. if (fmt->strtoul(right, right_size, &syscall_arg, &val)) {
  3188. char *n, expansion[19];
  3189. int expansion_lenght = scnprintf(expansion, sizeof(expansion), "%#" PRIx64, val);
  3190. int expansion_offset = right - new_filter;
  3191. pr_debug("%s", expansion);
  3192. if (asprintf(&n, "%.*s%s%s", expansion_offset, new_filter, expansion, right_end) < 0) {
  3193. pr_debug(" out of memory!\n");
  3194. free(new_filter);
  3195. return -1;
  3196. }
  3197. if (new_filter != evsel->filter)
  3198. free(new_filter);
  3199. left = n + expansion_offset + expansion_lenght;
  3200. new_filter = n;
  3201. } else {
  3202. pr_err("\"%.*s\" not found for \"%s\" in \"%s\", can't set filter \"%s\"\n",
  3203. right_size, right, arg, evsel->name, evsel->filter);
  3204. return -1;
  3205. }
  3206. } else {
  3207. pr_err("No resolver (strtoul) for \"%s\" in \"%s\", can't set filter \"%s\"\n",
  3208. arg, evsel->name, evsel->filter);
  3209. return -1;
  3210. }
  3211. pr_debug("\n");
  3212. } else {
  3213. left = right_end;
  3214. }
  3215. }
  3216. if (new_filter != evsel->filter) {
  3217. pr_debug("New filter for %s: %s\n", evsel->name, new_filter);
  3218. evsel__set_filter(evsel, new_filter);
  3219. free(new_filter);
  3220. }
  3221. return 0;
  3222. }
  3223. static int trace__expand_filters(struct trace *trace, struct evsel **err_evsel)
  3224. {
  3225. struct evlist *evlist = trace->evlist;
  3226. struct evsel *evsel;
  3227. evlist__for_each_entry(evlist, evsel) {
  3228. if (evsel->filter == NULL)
  3229. continue;
  3230. if (trace__expand_filter(trace, evsel)) {
  3231. *err_evsel = evsel;
  3232. return -1;
  3233. }
  3234. }
  3235. return 0;
  3236. }
  3237. static int trace__run(struct trace *trace, int argc, const char **argv)
  3238. {
  3239. struct evlist *evlist = trace->evlist;
  3240. struct evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
  3241. int err = -1, i;
  3242. unsigned long before;
  3243. const bool forks = argc > 0;
  3244. bool draining = false;
  3245. trace->live = true;
  3246. if (!trace->raw_augmented_syscalls) {
  3247. if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
  3248. goto out_error_raw_syscalls;
  3249. if (trace->trace_syscalls)
  3250. trace->vfs_getname = evlist__add_vfs_getname(evlist);
  3251. }
  3252. if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
  3253. pgfault_maj = evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
  3254. if (pgfault_maj == NULL)
  3255. goto out_error_mem;
  3256. evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
  3257. evlist__add(evlist, pgfault_maj);
  3258. }
  3259. if ((trace->trace_pgfaults & TRACE_PFMIN)) {
  3260. pgfault_min = evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
  3261. if (pgfault_min == NULL)
  3262. goto out_error_mem;
  3263. evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
  3264. evlist__add(evlist, pgfault_min);
  3265. }
  3266. if (trace->sched &&
  3267. evlist__add_newtp(evlist, "sched", "sched_stat_runtime", trace__sched_stat_runtime))
  3268. goto out_error_sched_stat_runtime;
  3269. /*
  3270. * If a global cgroup was set, apply it to all the events without an
  3271. * explicit cgroup. I.e.:
  3272. *
  3273. * trace -G A -e sched:*switch
  3274. *
  3275. * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
  3276. * _and_ sched:sched_switch to the 'A' cgroup, while:
  3277. *
  3278. * trace -e sched:*switch -G A
  3279. *
  3280. * will only set the sched:sched_switch event to the 'A' cgroup, all the
  3281. * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
  3282. * a cgroup (on the root cgroup, sys wide, etc).
  3283. *
  3284. * Multiple cgroups:
  3285. *
  3286. * trace -G A -e sched:*switch -G B
  3287. *
  3288. * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
  3289. * to the 'B' cgroup.
  3290. *
  3291. * evlist__set_default_cgroup() grabs a reference of the passed cgroup
  3292. * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
  3293. */
  3294. if (trace->cgroup)
  3295. evlist__set_default_cgroup(trace->evlist, trace->cgroup);
  3296. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  3297. if (err < 0) {
  3298. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  3299. goto out_delete_evlist;
  3300. }
  3301. err = trace__symbols_init(trace, evlist);
  3302. if (err < 0) {
  3303. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  3304. goto out_delete_evlist;
  3305. }
  3306. perf_evlist__config(evlist, &trace->opts, &callchain_param);
  3307. signal(SIGCHLD, sig_handler);
  3308. signal(SIGINT, sig_handler);
  3309. if (forks) {
  3310. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  3311. argv, false, NULL);
  3312. if (err < 0) {
  3313. fprintf(trace->output, "Couldn't run the workload!\n");
  3314. goto out_delete_evlist;
  3315. }
  3316. }
  3317. err = evlist__open(evlist);
  3318. if (err < 0)
  3319. goto out_error_open;
  3320. err = bpf__apply_obj_config();
  3321. if (err) {
  3322. char errbuf[BUFSIZ];
  3323. bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
  3324. pr_err("ERROR: Apply config to BPF failed: %s\n",
  3325. errbuf);
  3326. goto out_error_open;
  3327. }
  3328. err = trace__set_filter_pids(trace);
  3329. if (err < 0)
  3330. goto out_error_mem;
  3331. if (trace->syscalls.map)
  3332. trace__init_syscalls_bpf_map(trace);
  3333. if (trace->syscalls.prog_array.sys_enter)
  3334. trace__init_syscalls_bpf_prog_array_maps(trace);
  3335. if (trace->ev_qualifier_ids.nr > 0) {
  3336. err = trace__set_ev_qualifier_filter(trace);
  3337. if (err < 0)
  3338. goto out_errno;
  3339. if (trace->syscalls.events.sys_exit) {
  3340. pr_debug("event qualifier tracepoint filter: %s\n",
  3341. trace->syscalls.events.sys_exit->filter);
  3342. }
  3343. }
  3344. /*
  3345. * If the "close" syscall is not traced, then we will not have the
  3346. * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
  3347. * fd->pathname table and were ending up showing the last value set by
  3348. * syscalls opening a pathname and associating it with a descriptor or
  3349. * reading it from /proc/pid/fd/ in cases where that doesn't make
  3350. * sense.
  3351. *
  3352. * So just disable this beautifier (SCA_FD, SCA_FDAT) when 'close' is
  3353. * not in use.
  3354. */
  3355. trace->fd_path_disabled = !trace__syscall_enabled(trace, syscalltbl__id(trace->sctbl, "close"));
  3356. err = trace__expand_filters(trace, &evsel);
  3357. if (err)
  3358. goto out_delete_evlist;
  3359. err = perf_evlist__apply_filters(evlist, &evsel);
  3360. if (err < 0)
  3361. goto out_error_apply_filters;
  3362. if (trace->dump.map)
  3363. bpf_map__fprintf(trace->dump.map, trace->output);
  3364. err = evlist__mmap(evlist, trace->opts.mmap_pages);
  3365. if (err < 0)
  3366. goto out_error_mmap;
  3367. if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
  3368. evlist__enable(evlist);
  3369. if (forks)
  3370. perf_evlist__start_workload(evlist);
  3371. if (trace->opts.initial_delay) {
  3372. usleep(trace->opts.initial_delay * 1000);
  3373. evlist__enable(evlist);
  3374. }
  3375. trace->multiple_threads = perf_thread_map__pid(evlist->core.threads, 0) == -1 ||
  3376. evlist->core.threads->nr > 1 ||
  3377. evlist__first(evlist)->core.attr.inherit;
  3378. /*
  3379. * Now that we already used evsel->core.attr to ask the kernel to setup the
  3380. * events, lets reuse evsel->core.attr.sample_max_stack as the limit in
  3381. * trace__resolve_callchain(), allowing per-event max-stack settings
  3382. * to override an explicitly set --max-stack global setting.
  3383. */
  3384. evlist__for_each_entry(evlist, evsel) {
  3385. if (evsel__has_callchain(evsel) &&
  3386. evsel->core.attr.sample_max_stack == 0)
  3387. evsel->core.attr.sample_max_stack = trace->max_stack;
  3388. }
  3389. again:
  3390. before = trace->nr_events;
  3391. for (i = 0; i < evlist->core.nr_mmaps; i++) {
  3392. union perf_event *event;
  3393. struct mmap *md;
  3394. md = &evlist->mmap[i];
  3395. if (perf_mmap__read_init(&md->core) < 0)
  3396. continue;
  3397. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  3398. ++trace->nr_events;
  3399. err = trace__deliver_event(trace, event);
  3400. if (err)
  3401. goto out_disable;
  3402. perf_mmap__consume(&md->core);
  3403. if (interrupted)
  3404. goto out_disable;
  3405. if (done && !draining) {
  3406. evlist__disable(evlist);
  3407. draining = true;
  3408. }
  3409. }
  3410. perf_mmap__read_done(&md->core);
  3411. }
  3412. if (trace->nr_events == before) {
  3413. int timeout = done ? 100 : -1;
  3414. if (!draining && evlist__poll(evlist, timeout) > 0) {
  3415. if (evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
  3416. draining = true;
  3417. goto again;
  3418. } else {
  3419. if (trace__flush_events(trace))
  3420. goto out_disable;
  3421. }
  3422. } else {
  3423. goto again;
  3424. }
  3425. out_disable:
  3426. thread__zput(trace->current);
  3427. evlist__disable(evlist);
  3428. if (trace->sort_events)
  3429. ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
  3430. if (!err) {
  3431. if (trace->summary)
  3432. trace__fprintf_thread_summary(trace, trace->output);
  3433. if (trace->show_tool_stats) {
  3434. fprintf(trace->output, "Stats:\n "
  3435. " vfs_getname : %" PRIu64 "\n"
  3436. " proc_getname: %" PRIu64 "\n",
  3437. trace->stats.vfs_getname,
  3438. trace->stats.proc_getname);
  3439. }
  3440. }
  3441. out_delete_evlist:
  3442. trace__symbols__exit(trace);
  3443. evlist__delete(evlist);
  3444. cgroup__put(trace->cgroup);
  3445. trace->evlist = NULL;
  3446. trace->live = false;
  3447. return err;
  3448. {
  3449. char errbuf[BUFSIZ];
  3450. out_error_sched_stat_runtime:
  3451. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
  3452. goto out_error;
  3453. out_error_raw_syscalls:
  3454. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
  3455. goto out_error;
  3456. out_error_mmap:
  3457. evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
  3458. goto out_error;
  3459. out_error_open:
  3460. evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  3461. out_error:
  3462. fprintf(trace->output, "%s\n", errbuf);
  3463. goto out_delete_evlist;
  3464. out_error_apply_filters:
  3465. fprintf(trace->output,
  3466. "Failed to set filter \"%s\" on event %s with %d (%s)\n",
  3467. evsel->filter, evsel__name(evsel), errno,
  3468. str_error_r(errno, errbuf, sizeof(errbuf)));
  3469. goto out_delete_evlist;
  3470. }
  3471. out_error_mem:
  3472. fprintf(trace->output, "Not enough memory to run!\n");
  3473. goto out_delete_evlist;
  3474. out_errno:
  3475. fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
  3476. goto out_delete_evlist;
  3477. }
  3478. static int trace__replay(struct trace *trace)
  3479. {
  3480. const struct evsel_str_handler handlers[] = {
  3481. { "probe:vfs_getname", trace__vfs_getname, },
  3482. };
  3483. struct perf_data data = {
  3484. .path = input_name,
  3485. .mode = PERF_DATA_MODE_READ,
  3486. .force = trace->force,
  3487. };
  3488. struct perf_session *session;
  3489. struct evsel *evsel;
  3490. int err = -1;
  3491. trace->tool.sample = trace__process_sample;
  3492. trace->tool.mmap = perf_event__process_mmap;
  3493. trace->tool.mmap2 = perf_event__process_mmap2;
  3494. trace->tool.comm = perf_event__process_comm;
  3495. trace->tool.exit = perf_event__process_exit;
  3496. trace->tool.fork = perf_event__process_fork;
  3497. trace->tool.attr = perf_event__process_attr;
  3498. trace->tool.tracing_data = perf_event__process_tracing_data;
  3499. trace->tool.build_id = perf_event__process_build_id;
  3500. trace->tool.namespaces = perf_event__process_namespaces;
  3501. trace->tool.ordered_events = true;
  3502. trace->tool.ordering_requires_timestamps = true;
  3503. /* add tid to output */
  3504. trace->multiple_threads = true;
  3505. session = perf_session__new(&data, false, &trace->tool);
  3506. if (IS_ERR(session))
  3507. return PTR_ERR(session);
  3508. if (trace->opts.target.pid)
  3509. symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
  3510. if (trace->opts.target.tid)
  3511. symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
  3512. if (symbol__init(&session->header.env) < 0)
  3513. goto out;
  3514. trace->host = &session->machines.host;
  3515. err = perf_session__set_tracepoints_handlers(session, handlers);
  3516. if (err)
  3517. goto out;
  3518. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  3519. "raw_syscalls:sys_enter");
  3520. /* older kernels have syscalls tp versus raw_syscalls */
  3521. if (evsel == NULL)
  3522. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  3523. "syscalls:sys_enter");
  3524. if (evsel &&
  3525. (evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
  3526. perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
  3527. pr_err("Error during initialize raw_syscalls:sys_enter event\n");
  3528. goto out;
  3529. }
  3530. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  3531. "raw_syscalls:sys_exit");
  3532. if (evsel == NULL)
  3533. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  3534. "syscalls:sys_exit");
  3535. if (evsel &&
  3536. (evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
  3537. perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
  3538. pr_err("Error during initialize raw_syscalls:sys_exit event\n");
  3539. goto out;
  3540. }
  3541. evlist__for_each_entry(session->evlist, evsel) {
  3542. if (evsel->core.attr.type == PERF_TYPE_SOFTWARE &&
  3543. (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
  3544. evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  3545. evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS))
  3546. evsel->handler = trace__pgfault;
  3547. }
  3548. setup_pager();
  3549. err = perf_session__process_events(session);
  3550. if (err)
  3551. pr_err("Failed to process events, error %d", err);
  3552. else if (trace->summary)
  3553. trace__fprintf_thread_summary(trace, trace->output);
  3554. out:
  3555. perf_session__delete(session);
  3556. return err;
  3557. }
  3558. static size_t trace__fprintf_threads_header(FILE *fp)
  3559. {
  3560. size_t printed;
  3561. printed = fprintf(fp, "\n Summary of events:\n\n");
  3562. return printed;
  3563. }
  3564. DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
  3565. struct syscall_stats *stats;
  3566. double msecs;
  3567. int syscall;
  3568. )
  3569. {
  3570. struct int_node *source = rb_entry(nd, struct int_node, rb_node);
  3571. struct syscall_stats *stats = source->priv;
  3572. entry->syscall = source->i;
  3573. entry->stats = stats;
  3574. entry->msecs = stats ? (u64)stats->stats.n * (avg_stats(&stats->stats) / NSEC_PER_MSEC) : 0;
  3575. }
  3576. static size_t thread__dump_stats(struct thread_trace *ttrace,
  3577. struct trace *trace, FILE *fp)
  3578. {
  3579. size_t printed = 0;
  3580. struct syscall *sc;
  3581. struct rb_node *nd;
  3582. DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
  3583. if (syscall_stats == NULL)
  3584. return 0;
  3585. printed += fprintf(fp, "\n");
  3586. printed += fprintf(fp, " syscall calls errors total min avg max stddev\n");
  3587. printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
  3588. printed += fprintf(fp, " --------------- -------- ------ -------- --------- --------- --------- ------\n");
  3589. resort_rb__for_each_entry(nd, syscall_stats) {
  3590. struct syscall_stats *stats = syscall_stats_entry->stats;
  3591. if (stats) {
  3592. double min = (double)(stats->stats.min) / NSEC_PER_MSEC;
  3593. double max = (double)(stats->stats.max) / NSEC_PER_MSEC;
  3594. double avg = avg_stats(&stats->stats);
  3595. double pct;
  3596. u64 n = (u64)stats->stats.n;
  3597. pct = avg ? 100.0 * stddev_stats(&stats->stats) / avg : 0.0;
  3598. avg /= NSEC_PER_MSEC;
  3599. sc = &trace->syscalls.table[syscall_stats_entry->syscall];
  3600. printed += fprintf(fp, " %-15s", sc->name);
  3601. printed += fprintf(fp, " %8" PRIu64 " %6" PRIu64 " %9.3f %9.3f %9.3f",
  3602. n, stats->nr_failures, syscall_stats_entry->msecs, min, avg);
  3603. printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
  3604. if (trace->errno_summary && stats->nr_failures) {
  3605. const char *arch_name = perf_env__arch(trace->host->env);
  3606. int e;
  3607. for (e = 0; e < stats->max_errno; ++e) {
  3608. if (stats->errnos[e] != 0)
  3609. fprintf(fp, "\t\t\t\t%s: %d\n", arch_syscalls__strerrno(arch_name, e + 1), stats->errnos[e]);
  3610. }
  3611. }
  3612. }
  3613. }
  3614. resort_rb__delete(syscall_stats);
  3615. printed += fprintf(fp, "\n\n");
  3616. return printed;
  3617. }
  3618. static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
  3619. {
  3620. size_t printed = 0;
  3621. struct thread_trace *ttrace = thread__priv(thread);
  3622. double ratio;
  3623. if (ttrace == NULL)
  3624. return 0;
  3625. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  3626. printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
  3627. printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
  3628. printed += fprintf(fp, "%.1f%%", ratio);
  3629. if (ttrace->pfmaj)
  3630. printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
  3631. if (ttrace->pfmin)
  3632. printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
  3633. if (trace->sched)
  3634. printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
  3635. else if (fputc('\n', fp) != EOF)
  3636. ++printed;
  3637. printed += thread__dump_stats(ttrace, trace, fp);
  3638. return printed;
  3639. }
  3640. static unsigned long thread__nr_events(struct thread_trace *ttrace)
  3641. {
  3642. return ttrace ? ttrace->nr_events : 0;
  3643. }
  3644. DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
  3645. struct thread *thread;
  3646. )
  3647. {
  3648. entry->thread = rb_entry(nd, struct thread, rb_node);
  3649. }
  3650. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  3651. {
  3652. size_t printed = trace__fprintf_threads_header(fp);
  3653. struct rb_node *nd;
  3654. int i;
  3655. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  3656. DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
  3657. if (threads == NULL) {
  3658. fprintf(fp, "%s", "Error sorting output by nr_events!\n");
  3659. return 0;
  3660. }
  3661. resort_rb__for_each_entry(nd, threads)
  3662. printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
  3663. resort_rb__delete(threads);
  3664. }
  3665. return printed;
  3666. }
  3667. static int trace__set_duration(const struct option *opt, const char *str,
  3668. int unset __maybe_unused)
  3669. {
  3670. struct trace *trace = opt->value;
  3671. trace->duration_filter = atof(str);
  3672. return 0;
  3673. }
  3674. static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
  3675. int unset __maybe_unused)
  3676. {
  3677. int ret = -1;
  3678. size_t i;
  3679. struct trace *trace = opt->value;
  3680. /*
  3681. * FIXME: introduce a intarray class, plain parse csv and create a
  3682. * { int nr, int entries[] } struct...
  3683. */
  3684. struct intlist *list = intlist__new(str);
  3685. if (list == NULL)
  3686. return -1;
  3687. i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
  3688. trace->filter_pids.entries = calloc(i, sizeof(pid_t));
  3689. if (trace->filter_pids.entries == NULL)
  3690. goto out;
  3691. trace->filter_pids.entries[0] = getpid();
  3692. for (i = 1; i < trace->filter_pids.nr; ++i)
  3693. trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
  3694. intlist__delete(list);
  3695. ret = 0;
  3696. out:
  3697. return ret;
  3698. }
  3699. static int trace__open_output(struct trace *trace, const char *filename)
  3700. {
  3701. struct stat st;
  3702. if (!stat(filename, &st) && st.st_size) {
  3703. char oldname[PATH_MAX];
  3704. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  3705. unlink(oldname);
  3706. rename(filename, oldname);
  3707. }
  3708. trace->output = fopen(filename, "w");
  3709. return trace->output == NULL ? -errno : 0;
  3710. }
  3711. static int parse_pagefaults(const struct option *opt, const char *str,
  3712. int unset __maybe_unused)
  3713. {
  3714. int *trace_pgfaults = opt->value;
  3715. if (strcmp(str, "all") == 0)
  3716. *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
  3717. else if (strcmp(str, "maj") == 0)
  3718. *trace_pgfaults |= TRACE_PFMAJ;
  3719. else if (strcmp(str, "min") == 0)
  3720. *trace_pgfaults |= TRACE_PFMIN;
  3721. else
  3722. return -1;
  3723. return 0;
  3724. }
  3725. static void evlist__set_default_evsel_handler(struct evlist *evlist, void *handler)
  3726. {
  3727. struct evsel *evsel;
  3728. evlist__for_each_entry(evlist, evsel) {
  3729. if (evsel->handler == NULL)
  3730. evsel->handler = handler;
  3731. }
  3732. }
  3733. static void evsel__set_syscall_arg_fmt(struct evsel *evsel, const char *name)
  3734. {
  3735. struct syscall_arg_fmt *fmt = evsel__syscall_arg_fmt(evsel);
  3736. if (fmt) {
  3737. struct syscall_fmt *scfmt = syscall_fmt__find(name);
  3738. if (scfmt) {
  3739. int skip = 0;
  3740. if (strcmp(evsel->tp_format->format.fields->name, "__syscall_nr") == 0 ||
  3741. strcmp(evsel->tp_format->format.fields->name, "nr") == 0)
  3742. ++skip;
  3743. memcpy(fmt + skip, scfmt->arg, (evsel->tp_format->format.nr_fields - skip) * sizeof(*fmt));
  3744. }
  3745. }
  3746. }
  3747. static int evlist__set_syscall_tp_fields(struct evlist *evlist)
  3748. {
  3749. struct evsel *evsel;
  3750. evlist__for_each_entry(evlist, evsel) {
  3751. if (evsel->priv || !evsel->tp_format)
  3752. continue;
  3753. if (strcmp(evsel->tp_format->system, "syscalls")) {
  3754. evsel__init_tp_arg_scnprintf(evsel);
  3755. continue;
  3756. }
  3757. if (evsel__init_syscall_tp(evsel))
  3758. return -1;
  3759. if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
  3760. struct syscall_tp *sc = __evsel__syscall_tp(evsel);
  3761. if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
  3762. return -1;
  3763. evsel__set_syscall_arg_fmt(evsel, evsel->tp_format->name + sizeof("sys_enter_") - 1);
  3764. } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
  3765. struct syscall_tp *sc = __evsel__syscall_tp(evsel);
  3766. if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
  3767. return -1;
  3768. evsel__set_syscall_arg_fmt(evsel, evsel->tp_format->name + sizeof("sys_exit_") - 1);
  3769. }
  3770. }
  3771. return 0;
  3772. }
  3773. /*
  3774. * XXX: Hackish, just splitting the combined -e+--event (syscalls
  3775. * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
  3776. * existing facilities unchanged (trace->ev_qualifier + parse_options()).
  3777. *
  3778. * It'd be better to introduce a parse_options() variant that would return a
  3779. * list with the terms it didn't match to an event...
  3780. */
  3781. static int trace__parse_events_option(const struct option *opt, const char *str,
  3782. int unset __maybe_unused)
  3783. {
  3784. struct trace *trace = (struct trace *)opt->value;
  3785. const char *s = str;
  3786. char *sep = NULL, *lists[2] = { NULL, NULL, };
  3787. int len = strlen(str) + 1, err = -1, list, idx;
  3788. char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
  3789. char group_name[PATH_MAX];
  3790. struct syscall_fmt *fmt;
  3791. if (strace_groups_dir == NULL)
  3792. return -1;
  3793. if (*s == '!') {
  3794. ++s;
  3795. trace->not_ev_qualifier = true;
  3796. }
  3797. while (1) {
  3798. if ((sep = strchr(s, ',')) != NULL)
  3799. *sep = '\0';
  3800. list = 0;
  3801. if (syscalltbl__id(trace->sctbl, s) >= 0 ||
  3802. syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
  3803. list = 1;
  3804. goto do_concat;
  3805. }
  3806. fmt = syscall_fmt__find_by_alias(s);
  3807. if (fmt != NULL) {
  3808. list = 1;
  3809. s = fmt->name;
  3810. } else {
  3811. path__join(group_name, sizeof(group_name), strace_groups_dir, s);
  3812. if (access(group_name, R_OK) == 0)
  3813. list = 1;
  3814. }
  3815. do_concat:
  3816. if (lists[list]) {
  3817. sprintf(lists[list] + strlen(lists[list]), ",%s", s);
  3818. } else {
  3819. lists[list] = malloc(len);
  3820. if (lists[list] == NULL)
  3821. goto out;
  3822. strcpy(lists[list], s);
  3823. }
  3824. if (!sep)
  3825. break;
  3826. *sep = ',';
  3827. s = sep + 1;
  3828. }
  3829. if (lists[1] != NULL) {
  3830. struct strlist_config slist_config = {
  3831. .dirname = strace_groups_dir,
  3832. };
  3833. trace->ev_qualifier = strlist__new(lists[1], &slist_config);
  3834. if (trace->ev_qualifier == NULL) {
  3835. fputs("Not enough memory to parse event qualifier", trace->output);
  3836. goto out;
  3837. }
  3838. if (trace__validate_ev_qualifier(trace))
  3839. goto out;
  3840. trace->trace_syscalls = true;
  3841. }
  3842. err = 0;
  3843. if (lists[0]) {
  3844. struct option o = {
  3845. .value = &trace->evlist,
  3846. };
  3847. err = parse_events_option(&o, lists[0], 0);
  3848. }
  3849. out:
  3850. if (sep)
  3851. *sep = ',';
  3852. return err;
  3853. }
  3854. static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
  3855. {
  3856. struct trace *trace = opt->value;
  3857. if (!list_empty(&trace->evlist->core.entries)) {
  3858. struct option o = {
  3859. .value = &trace->evlist,
  3860. };
  3861. return parse_cgroups(&o, str, unset);
  3862. }
  3863. trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
  3864. return 0;
  3865. }
  3866. static int trace__config(const char *var, const char *value, void *arg)
  3867. {
  3868. struct trace *trace = arg;
  3869. int err = 0;
  3870. if (!strcmp(var, "trace.add_events")) {
  3871. trace->perfconfig_events = strdup(value);
  3872. if (trace->perfconfig_events == NULL) {
  3873. pr_err("Not enough memory for %s\n", "trace.add_events");
  3874. return -1;
  3875. }
  3876. } else if (!strcmp(var, "trace.show_timestamp")) {
  3877. trace->show_tstamp = perf_config_bool(var, value);
  3878. } else if (!strcmp(var, "trace.show_duration")) {
  3879. trace->show_duration = perf_config_bool(var, value);
  3880. } else if (!strcmp(var, "trace.show_arg_names")) {
  3881. trace->show_arg_names = perf_config_bool(var, value);
  3882. if (!trace->show_arg_names)
  3883. trace->show_zeros = true;
  3884. } else if (!strcmp(var, "trace.show_zeros")) {
  3885. bool new_show_zeros = perf_config_bool(var, value);
  3886. if (!trace->show_arg_names && !new_show_zeros) {
  3887. pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
  3888. goto out;
  3889. }
  3890. trace->show_zeros = new_show_zeros;
  3891. } else if (!strcmp(var, "trace.show_prefix")) {
  3892. trace->show_string_prefix = perf_config_bool(var, value);
  3893. } else if (!strcmp(var, "trace.no_inherit")) {
  3894. trace->opts.no_inherit = perf_config_bool(var, value);
  3895. } else if (!strcmp(var, "trace.args_alignment")) {
  3896. int args_alignment = 0;
  3897. if (perf_config_int(&args_alignment, var, value) == 0)
  3898. trace->args_alignment = args_alignment;
  3899. } else if (!strcmp(var, "trace.tracepoint_beautifiers")) {
  3900. if (strcasecmp(value, "libtraceevent") == 0)
  3901. trace->libtraceevent_print = true;
  3902. else if (strcasecmp(value, "libbeauty") == 0)
  3903. trace->libtraceevent_print = false;
  3904. }
  3905. out:
  3906. return err;
  3907. }
  3908. int cmd_trace(int argc, const char **argv)
  3909. {
  3910. const char *trace_usage[] = {
  3911. "perf trace [<options>] [<command>]",
  3912. "perf trace [<options>] -- <command> [<options>]",
  3913. "perf trace record [<options>] [<command>]",
  3914. "perf trace record [<options>] -- <command> [<options>]",
  3915. NULL
  3916. };
  3917. struct trace trace = {
  3918. .opts = {
  3919. .target = {
  3920. .uid = UINT_MAX,
  3921. .uses_mmap = true,
  3922. },
  3923. .user_freq = UINT_MAX,
  3924. .user_interval = ULLONG_MAX,
  3925. .no_buffering = true,
  3926. .mmap_pages = UINT_MAX,
  3927. },
  3928. .output = stderr,
  3929. .show_comm = true,
  3930. .show_tstamp = true,
  3931. .show_duration = true,
  3932. .show_arg_names = true,
  3933. .args_alignment = 70,
  3934. .trace_syscalls = false,
  3935. .kernel_syscallchains = false,
  3936. .max_stack = UINT_MAX,
  3937. .max_events = ULONG_MAX,
  3938. };
  3939. const char *map_dump_str = NULL;
  3940. const char *output_name = NULL;
  3941. const struct option trace_options[] = {
  3942. OPT_CALLBACK('e', "event", &trace, "event",
  3943. "event/syscall selector. use 'perf list' to list available events",
  3944. trace__parse_events_option),
  3945. OPT_CALLBACK(0, "filter", &trace.evlist, "filter",
  3946. "event filter", parse_filter),
  3947. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  3948. "show the thread COMM next to its id"),
  3949. OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
  3950. OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
  3951. trace__parse_events_option),
  3952. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  3953. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  3954. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  3955. "trace events on existing process id"),
  3956. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  3957. "trace events on existing thread id"),
  3958. OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
  3959. "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
  3960. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  3961. "system-wide collection from all CPUs"),
  3962. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  3963. "list of cpus to monitor"),
  3964. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  3965. "child tasks do not inherit counters"),
  3966. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  3967. "number of mmap data pages",
  3968. perf_evlist__parse_mmap_pages),
  3969. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  3970. "user to profile"),
  3971. OPT_CALLBACK(0, "duration", &trace, "float",
  3972. "show only events with duration > N.M ms",
  3973. trace__set_duration),
  3974. #ifdef HAVE_LIBBPF_SUPPORT
  3975. OPT_STRING(0, "map-dump", &map_dump_str, "BPF map", "BPF map to periodically dump"),
  3976. #endif
  3977. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  3978. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  3979. OPT_BOOLEAN('T', "time", &trace.full_time,
  3980. "Show full timestamp, not time relative to first start"),
  3981. OPT_BOOLEAN(0, "failure", &trace.failure_only,
  3982. "Show only syscalls that failed"),
  3983. OPT_BOOLEAN('s', "summary", &trace.summary_only,
  3984. "Show only syscall summary with statistics"),
  3985. OPT_BOOLEAN('S', "with-summary", &trace.summary,
  3986. "Show all syscalls and summary with statistics"),
  3987. OPT_BOOLEAN(0, "errno-summary", &trace.errno_summary,
  3988. "Show errno stats per syscall, use with -s or -S"),
  3989. OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
  3990. "Trace pagefaults", parse_pagefaults, "maj"),
  3991. OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
  3992. OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
  3993. OPT_CALLBACK(0, "call-graph", &trace.opts,
  3994. "record_mode[,record_size]", record_callchain_help,
  3995. &record_parse_callchain_opt),
  3996. OPT_BOOLEAN(0, "libtraceevent_print", &trace.libtraceevent_print,
  3997. "Use libtraceevent to print the tracepoint arguments."),
  3998. OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
  3999. "Show the kernel callchains on the syscall exit path"),
  4000. OPT_ULONG(0, "max-events", &trace.max_events,
  4001. "Set the maximum number of events to print, exit after that is reached. "),
  4002. OPT_UINTEGER(0, "min-stack", &trace.min_stack,
  4003. "Set the minimum stack depth when parsing the callchain, "
  4004. "anything below the specified depth will be ignored."),
  4005. OPT_UINTEGER(0, "max-stack", &trace.max_stack,
  4006. "Set the maximum stack depth when parsing the callchain, "
  4007. "anything beyond the specified depth will be ignored. "
  4008. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  4009. OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
  4010. "Sort batch of events before processing, use if getting out of order events"),
  4011. OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
  4012. "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
  4013. OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
  4014. "per thread proc mmap processing timeout in ms"),
  4015. OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
  4016. trace__parse_cgroups),
  4017. OPT_INTEGER('D', "delay", &trace.opts.initial_delay,
  4018. "ms to wait before starting measurement after program "
  4019. "start"),
  4020. OPTS_EVSWITCH(&trace.evswitch),
  4021. OPT_END()
  4022. };
  4023. bool __maybe_unused max_stack_user_set = true;
  4024. bool mmap_pages_user_set = true;
  4025. struct evsel *evsel;
  4026. const char * const trace_subcommands[] = { "record", NULL };
  4027. int err = -1;
  4028. char bf[BUFSIZ];
  4029. signal(SIGSEGV, sighandler_dump_stack);
  4030. signal(SIGFPE, sighandler_dump_stack);
  4031. trace.evlist = evlist__new();
  4032. trace.sctbl = syscalltbl__new();
  4033. if (trace.evlist == NULL || trace.sctbl == NULL) {
  4034. pr_err("Not enough memory to run!\n");
  4035. err = -ENOMEM;
  4036. goto out;
  4037. }
  4038. /*
  4039. * Parsing .perfconfig may entail creating a BPF event, that may need
  4040. * to create BPF maps, so bump RLIM_MEMLOCK as the default 64K setting
  4041. * is too small. This affects just this process, not touching the
  4042. * global setting. If it fails we'll get something in 'perf trace -v'
  4043. * to help diagnose the problem.
  4044. */
  4045. rlimit__bump_memlock();
  4046. err = perf_config(trace__config, &trace);
  4047. if (err)
  4048. goto out;
  4049. argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
  4050. trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  4051. /*
  4052. * Here we already passed thru trace__parse_events_option() and it has
  4053. * already figured out if -e syscall_name, if not but if --event
  4054. * foo:bar was used, the user is interested _just_ in those, say,
  4055. * tracepoint events, not in the strace-like syscall-name-based mode.
  4056. *
  4057. * This is important because we need to check if strace-like mode is
  4058. * needed to decided if we should filter out the eBPF
  4059. * __augmented_syscalls__ code, if it is in the mix, say, via
  4060. * .perfconfig trace.add_events, and filter those out.
  4061. */
  4062. if (!trace.trace_syscalls && !trace.trace_pgfaults &&
  4063. trace.evlist->core.nr_entries == 0 /* Was --events used? */) {
  4064. trace.trace_syscalls = true;
  4065. }
  4066. /*
  4067. * Now that we have --verbose figured out, lets see if we need to parse
  4068. * events from .perfconfig, so that if those events fail parsing, say some
  4069. * BPF program fails, then we'll be able to use --verbose to see what went
  4070. * wrong in more detail.
  4071. */
  4072. if (trace.perfconfig_events != NULL) {
  4073. struct parse_events_error parse_err;
  4074. bzero(&parse_err, sizeof(parse_err));
  4075. err = parse_events(trace.evlist, trace.perfconfig_events, &parse_err);
  4076. if (err) {
  4077. parse_events_print_error(&parse_err, trace.perfconfig_events);
  4078. goto out;
  4079. }
  4080. }
  4081. if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
  4082. usage_with_options_msg(trace_usage, trace_options,
  4083. "cgroup monitoring only available in system-wide mode");
  4084. }
  4085. evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
  4086. if (IS_ERR(evsel)) {
  4087. bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
  4088. pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
  4089. goto out;
  4090. }
  4091. if (evsel) {
  4092. trace.syscalls.events.augmented = evsel;
  4093. evsel = perf_evlist__find_tracepoint_by_name(trace.evlist, "raw_syscalls:sys_enter");
  4094. if (evsel == NULL) {
  4095. pr_err("ERROR: raw_syscalls:sys_enter not found in the augmented BPF object\n");
  4096. goto out;
  4097. }
  4098. if (evsel->bpf_obj == NULL) {
  4099. pr_err("ERROR: raw_syscalls:sys_enter not associated to a BPF object\n");
  4100. goto out;
  4101. }
  4102. trace.bpf_obj = evsel->bpf_obj;
  4103. /*
  4104. * If we have _just_ the augmenter event but don't have a
  4105. * explicit --syscalls, then assume we want all strace-like
  4106. * syscalls:
  4107. */
  4108. if (!trace.trace_syscalls && trace__only_augmented_syscalls_evsels(&trace))
  4109. trace.trace_syscalls = true;
  4110. /*
  4111. * So, if we have a syscall augmenter, but trace_syscalls, aka
  4112. * strace-like syscall tracing is not set, then we need to trow
  4113. * away the augmenter, i.e. all the events that were created
  4114. * from that BPF object file.
  4115. *
  4116. * This is more to fix the current .perfconfig trace.add_events
  4117. * style of setting up the strace-like eBPF based syscall point
  4118. * payload augmenter.
  4119. *
  4120. * All this complexity will be avoided by adding an alternative
  4121. * to trace.add_events in the form of
  4122. * trace.bpf_augmented_syscalls, that will be only parsed if we
  4123. * need it.
  4124. *
  4125. * .perfconfig trace.add_events is still useful if we want, for
  4126. * instance, have msr_write.msr in some .perfconfig profile based
  4127. * 'perf trace --config determinism.profile' mode, where for some
  4128. * particular goal/workload type we want a set of events and
  4129. * output mode (with timings, etc) instead of having to add
  4130. * all via the command line.
  4131. *
  4132. * Also --config to specify an alternate .perfconfig file needs
  4133. * to be implemented.
  4134. */
  4135. if (!trace.trace_syscalls) {
  4136. trace__delete_augmented_syscalls(&trace);
  4137. } else {
  4138. trace__set_bpf_map_filtered_pids(&trace);
  4139. trace__set_bpf_map_syscalls(&trace);
  4140. trace.syscalls.unaugmented_prog = trace__find_bpf_program_by_title(&trace, "!raw_syscalls:unaugmented");
  4141. }
  4142. }
  4143. err = bpf__setup_stdout(trace.evlist);
  4144. if (err) {
  4145. bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
  4146. pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
  4147. goto out;
  4148. }
  4149. err = -1;
  4150. if (map_dump_str) {
  4151. trace.dump.map = trace__find_bpf_map_by_name(&trace, map_dump_str);
  4152. if (trace.dump.map == NULL) {
  4153. pr_err("ERROR: BPF map \"%s\" not found\n", map_dump_str);
  4154. goto out;
  4155. }
  4156. }
  4157. if (trace.trace_pgfaults) {
  4158. trace.opts.sample_address = true;
  4159. trace.opts.sample_time = true;
  4160. }
  4161. if (trace.opts.mmap_pages == UINT_MAX)
  4162. mmap_pages_user_set = false;
  4163. if (trace.max_stack == UINT_MAX) {
  4164. trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
  4165. max_stack_user_set = false;
  4166. }
  4167. #ifdef HAVE_DWARF_UNWIND_SUPPORT
  4168. if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
  4169. record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
  4170. }
  4171. #endif
  4172. if (callchain_param.enabled) {
  4173. if (!mmap_pages_user_set && geteuid() == 0)
  4174. trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
  4175. symbol_conf.use_callchain = true;
  4176. }
  4177. if (trace.evlist->core.nr_entries > 0) {
  4178. evlist__set_default_evsel_handler(trace.evlist, trace__event_handler);
  4179. if (evlist__set_syscall_tp_fields(trace.evlist)) {
  4180. perror("failed to set syscalls:* tracepoint fields");
  4181. goto out;
  4182. }
  4183. }
  4184. if (trace.sort_events) {
  4185. ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
  4186. ordered_events__set_copy_on_queue(&trace.oe.data, true);
  4187. }
  4188. /*
  4189. * If we are augmenting syscalls, then combine what we put in the
  4190. * __augmented_syscalls__ BPF map with what is in the
  4191. * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
  4192. * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
  4193. *
  4194. * We'll switch to look at two BPF maps, one for sys_enter and the
  4195. * other for sys_exit when we start augmenting the sys_exit paths with
  4196. * buffers that are being copied from kernel to userspace, think 'read'
  4197. * syscall.
  4198. */
  4199. if (trace.syscalls.events.augmented) {
  4200. evlist__for_each_entry(trace.evlist, evsel) {
  4201. bool raw_syscalls_sys_exit = strcmp(evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
  4202. if (raw_syscalls_sys_exit) {
  4203. trace.raw_augmented_syscalls = true;
  4204. goto init_augmented_syscall_tp;
  4205. }
  4206. if (trace.syscalls.events.augmented->priv == NULL &&
  4207. strstr(evsel__name(evsel), "syscalls:sys_enter")) {
  4208. struct evsel *augmented = trace.syscalls.events.augmented;
  4209. if (evsel__init_augmented_syscall_tp(augmented, evsel) ||
  4210. evsel__init_augmented_syscall_tp_args(augmented))
  4211. goto out;
  4212. /*
  4213. * Augmented is __augmented_syscalls__ BPF_OUTPUT event
  4214. * Above we made sure we can get from the payload the tp fields
  4215. * that we get from syscalls:sys_enter tracefs format file.
  4216. */
  4217. augmented->handler = trace__sys_enter;
  4218. /*
  4219. * Now we do the same for the *syscalls:sys_enter event so that
  4220. * if we handle it directly, i.e. if the BPF prog returns 0 so
  4221. * as not to filter it, then we'll handle it just like we would
  4222. * for the BPF_OUTPUT one:
  4223. */
  4224. if (evsel__init_augmented_syscall_tp(evsel, evsel) ||
  4225. evsel__init_augmented_syscall_tp_args(evsel))
  4226. goto out;
  4227. evsel->handler = trace__sys_enter;
  4228. }
  4229. if (strstarts(evsel__name(evsel), "syscalls:sys_exit_")) {
  4230. struct syscall_tp *sc;
  4231. init_augmented_syscall_tp:
  4232. if (evsel__init_augmented_syscall_tp(evsel, evsel))
  4233. goto out;
  4234. sc = __evsel__syscall_tp(evsel);
  4235. /*
  4236. * For now with BPF raw_augmented we hook into
  4237. * raw_syscalls:sys_enter and there we get all
  4238. * 6 syscall args plus the tracepoint common
  4239. * fields and the syscall_nr (another long).
  4240. * So we check if that is the case and if so
  4241. * don't look after the sc->args_size but
  4242. * always after the full raw_syscalls:sys_enter
  4243. * payload, which is fixed.
  4244. *
  4245. * We'll revisit this later to pass
  4246. * s->args_size to the BPF augmenter (now
  4247. * tools/perf/examples/bpf/augmented_raw_syscalls.c,
  4248. * so that it copies only what we need for each
  4249. * syscall, like what happens when we use
  4250. * syscalls:sys_enter_NAME, so that we reduce
  4251. * the kernel/userspace traffic to just what is
  4252. * needed for each syscall.
  4253. */
  4254. if (trace.raw_augmented_syscalls)
  4255. trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset;
  4256. evsel__init_augmented_syscall_tp_ret(evsel);
  4257. evsel->handler = trace__sys_exit;
  4258. }
  4259. }
  4260. }
  4261. if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
  4262. return trace__record(&trace, argc-1, &argv[1]);
  4263. /* Using just --errno-summary will trigger --summary */
  4264. if (trace.errno_summary && !trace.summary && !trace.summary_only)
  4265. trace.summary_only = true;
  4266. /* summary_only implies summary option, but don't overwrite summary if set */
  4267. if (trace.summary_only)
  4268. trace.summary = trace.summary_only;
  4269. if (output_name != NULL) {
  4270. err = trace__open_output(&trace, output_name);
  4271. if (err < 0) {
  4272. perror("failed to create output file");
  4273. goto out;
  4274. }
  4275. }
  4276. err = evswitch__init(&trace.evswitch, trace.evlist, stderr);
  4277. if (err)
  4278. goto out_close;
  4279. err = target__validate(&trace.opts.target);
  4280. if (err) {
  4281. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  4282. fprintf(trace.output, "%s", bf);
  4283. goto out_close;
  4284. }
  4285. err = target__parse_uid(&trace.opts.target);
  4286. if (err) {
  4287. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  4288. fprintf(trace.output, "%s", bf);
  4289. goto out_close;
  4290. }
  4291. if (!argc && target__none(&trace.opts.target))
  4292. trace.opts.target.system_wide = true;
  4293. if (input_name)
  4294. err = trace__replay(&trace);
  4295. else
  4296. err = trace__run(&trace, argc, argv);
  4297. out_close:
  4298. if (output_name != NULL)
  4299. fclose(trace.output);
  4300. out:
  4301. zfree(&trace.perfconfig_events);
  4302. return err;
  4303. }