builtin-trace.c 148 KB

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