builtin-trace.c 157 KB

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