builtin-trace.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289
  1. /*
  2. * builtin-trace.c
  3. *
  4. * Builtin 'trace' command:
  5. *
  6. * Display a continuously updated trace of any workload, CPU, specific PID,
  7. * system wide, etc. Default format is loosely strace like, but any other
  8. * event may be specified using --event.
  9. *
  10. * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  11. *
  12. * Initially based on the 'trace' prototype by Thomas Gleixner:
  13. *
  14. * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
  15. *
  16. * Released under the GPL v2. (and only v2, not any later version)
  17. */
  18. #include <traceevent/event-parse.h>
  19. #include <api/fs/tracing_path.h>
  20. #include "builtin.h"
  21. #include "util/cgroup.h"
  22. #include "util/color.h"
  23. #include "util/debug.h"
  24. #include "util/env.h"
  25. #include "util/event.h"
  26. #include "util/evlist.h"
  27. #include <subcmd/exec-cmd.h>
  28. #include "util/machine.h"
  29. #include "util/path.h"
  30. #include "util/session.h"
  31. #include "util/thread.h"
  32. #include <subcmd/parse-options.h>
  33. #include "util/strlist.h"
  34. #include "util/intlist.h"
  35. #include "util/thread_map.h"
  36. #include "util/stat.h"
  37. #include "trace/beauty/beauty.h"
  38. #include "trace-event.h"
  39. #include "util/parse-events.h"
  40. #include "util/bpf-loader.h"
  41. #include "callchain.h"
  42. #include "print_binary.h"
  43. #include "string2.h"
  44. #include "syscalltbl.h"
  45. #include "rb_resort.h"
  46. #include <errno.h>
  47. #include <inttypes.h>
  48. #include <poll.h>
  49. #include <signal.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <linux/err.h>
  53. #include <linux/filter.h>
  54. #include <linux/kernel.h>
  55. #include <linux/random.h>
  56. #include <linux/stringify.h>
  57. #include <linux/time64.h>
  58. #include <fcntl.h>
  59. #include "sane_ctype.h"
  60. #ifndef O_CLOEXEC
  61. # define O_CLOEXEC 02000000
  62. #endif
  63. #ifndef F_LINUX_SPECIFIC_BASE
  64. # define F_LINUX_SPECIFIC_BASE 1024
  65. #endif
  66. struct trace {
  67. struct perf_tool tool;
  68. struct syscalltbl *sctbl;
  69. struct {
  70. int max;
  71. struct syscall *table;
  72. struct {
  73. struct perf_evsel *sys_enter,
  74. *sys_exit;
  75. } events;
  76. } syscalls;
  77. struct record_opts opts;
  78. struct perf_evlist *evlist;
  79. struct machine *host;
  80. struct thread *current;
  81. struct cgroup *cgroup;
  82. u64 base_time;
  83. FILE *output;
  84. unsigned long nr_events;
  85. struct strlist *ev_qualifier;
  86. struct {
  87. size_t nr;
  88. int *entries;
  89. } ev_qualifier_ids;
  90. struct {
  91. size_t nr;
  92. pid_t *entries;
  93. } filter_pids;
  94. double duration_filter;
  95. double runtime_ms;
  96. struct {
  97. u64 vfs_getname,
  98. proc_getname;
  99. } stats;
  100. unsigned int max_stack;
  101. unsigned int min_stack;
  102. bool not_ev_qualifier;
  103. bool live;
  104. bool full_time;
  105. bool sched;
  106. bool multiple_threads;
  107. bool summary;
  108. bool summary_only;
  109. bool failure_only;
  110. bool show_comm;
  111. bool print_sample;
  112. bool show_tool_stats;
  113. bool trace_syscalls;
  114. bool kernel_syscallchains;
  115. bool force;
  116. bool vfs_getname;
  117. int trace_pgfaults;
  118. };
  119. struct tp_field {
  120. int offset;
  121. union {
  122. u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
  123. void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
  124. };
  125. };
  126. #define TP_UINT_FIELD(bits) \
  127. static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
  128. { \
  129. u##bits value; \
  130. memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
  131. return value; \
  132. }
  133. TP_UINT_FIELD(8);
  134. TP_UINT_FIELD(16);
  135. TP_UINT_FIELD(32);
  136. TP_UINT_FIELD(64);
  137. #define TP_UINT_FIELD__SWAPPED(bits) \
  138. static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
  139. { \
  140. u##bits value; \
  141. memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
  142. return bswap_##bits(value);\
  143. }
  144. TP_UINT_FIELD__SWAPPED(16);
  145. TP_UINT_FIELD__SWAPPED(32);
  146. TP_UINT_FIELD__SWAPPED(64);
  147. static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
  148. {
  149. field->offset = offset;
  150. switch (size) {
  151. case 1:
  152. field->integer = tp_field__u8;
  153. break;
  154. case 2:
  155. field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
  156. break;
  157. case 4:
  158. field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
  159. break;
  160. case 8:
  161. field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
  162. break;
  163. default:
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. static int tp_field__init_uint(struct tp_field *field, struct format_field *format_field, bool needs_swap)
  169. {
  170. return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
  171. }
  172. static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
  173. {
  174. return sample->raw_data + field->offset;
  175. }
  176. static int __tp_field__init_ptr(struct tp_field *field, int offset)
  177. {
  178. field->offset = offset;
  179. field->pointer = tp_field__ptr;
  180. return 0;
  181. }
  182. static int tp_field__init_ptr(struct tp_field *field, struct format_field *format_field)
  183. {
  184. return __tp_field__init_ptr(field, format_field->offset);
  185. }
  186. struct syscall_tp {
  187. struct tp_field id;
  188. union {
  189. struct tp_field args, ret;
  190. };
  191. };
  192. static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
  193. struct tp_field *field,
  194. const char *name)
  195. {
  196. struct format_field *format_field = perf_evsel__field(evsel, name);
  197. if (format_field == NULL)
  198. return -1;
  199. return tp_field__init_uint(field, format_field, evsel->needs_swap);
  200. }
  201. #define perf_evsel__init_sc_tp_uint_field(evsel, name) \
  202. ({ struct syscall_tp *sc = evsel->priv;\
  203. perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
  204. static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
  205. struct tp_field *field,
  206. const char *name)
  207. {
  208. struct format_field *format_field = perf_evsel__field(evsel, name);
  209. if (format_field == NULL)
  210. return -1;
  211. return tp_field__init_ptr(field, format_field);
  212. }
  213. #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
  214. ({ struct syscall_tp *sc = evsel->priv;\
  215. perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
  216. static void perf_evsel__delete_priv(struct perf_evsel *evsel)
  217. {
  218. zfree(&evsel->priv);
  219. perf_evsel__delete(evsel);
  220. }
  221. static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
  222. {
  223. evsel->priv = malloc(sizeof(struct syscall_tp));
  224. if (evsel->priv != NULL) {
  225. if (perf_evsel__init_sc_tp_uint_field(evsel, id))
  226. goto out_delete;
  227. evsel->handler = handler;
  228. return 0;
  229. }
  230. return -ENOMEM;
  231. out_delete:
  232. zfree(&evsel->priv);
  233. return -ENOENT;
  234. }
  235. static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
  236. {
  237. struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
  238. /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
  239. if (IS_ERR(evsel))
  240. evsel = perf_evsel__newtp("syscalls", direction);
  241. if (IS_ERR(evsel))
  242. return NULL;
  243. if (perf_evsel__init_raw_syscall_tp(evsel, handler))
  244. goto out_delete;
  245. return evsel;
  246. out_delete:
  247. perf_evsel__delete_priv(evsel);
  248. return NULL;
  249. }
  250. #define perf_evsel__sc_tp_uint(evsel, name, sample) \
  251. ({ struct syscall_tp *fields = evsel->priv; \
  252. fields->name.integer(&fields->name, sample); })
  253. #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
  254. ({ struct syscall_tp *fields = evsel->priv; \
  255. fields->name.pointer(&fields->name, sample); })
  256. size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, int val)
  257. {
  258. int idx = val - sa->offset;
  259. if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL)
  260. return scnprintf(bf, size, intfmt, val);
  261. return scnprintf(bf, size, "%s", sa->entries[idx]);
  262. }
  263. static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
  264. const char *intfmt,
  265. struct syscall_arg *arg)
  266. {
  267. return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->val);
  268. }
  269. static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
  270. struct syscall_arg *arg)
  271. {
  272. return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
  273. }
  274. #define SCA_STRARRAY syscall_arg__scnprintf_strarray
  275. struct strarrays {
  276. int nr_entries;
  277. struct strarray **entries;
  278. };
  279. #define DEFINE_STRARRAYS(array) struct strarrays strarrays__##array = { \
  280. .nr_entries = ARRAY_SIZE(array), \
  281. .entries = array, \
  282. }
  283. size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
  284. struct syscall_arg *arg)
  285. {
  286. struct strarrays *sas = arg->parm;
  287. int i;
  288. for (i = 0; i < sas->nr_entries; ++i) {
  289. struct strarray *sa = sas->entries[i];
  290. int idx = arg->val - sa->offset;
  291. if (idx >= 0 && idx < sa->nr_entries) {
  292. if (sa->entries[idx] == NULL)
  293. break;
  294. return scnprintf(bf, size, "%s", sa->entries[idx]);
  295. }
  296. }
  297. return scnprintf(bf, size, "%d", arg->val);
  298. }
  299. #ifndef AT_FDCWD
  300. #define AT_FDCWD -100
  301. #endif
  302. static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
  303. struct syscall_arg *arg)
  304. {
  305. int fd = arg->val;
  306. if (fd == AT_FDCWD)
  307. return scnprintf(bf, size, "CWD");
  308. return syscall_arg__scnprintf_fd(bf, size, arg);
  309. }
  310. #define SCA_FDAT syscall_arg__scnprintf_fd_at
  311. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  312. struct syscall_arg *arg);
  313. #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
  314. size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
  315. {
  316. return scnprintf(bf, size, "%#lx", arg->val);
  317. }
  318. size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
  319. {
  320. return scnprintf(bf, size, "%d", arg->val);
  321. }
  322. size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
  323. {
  324. return scnprintf(bf, size, "%ld", arg->val);
  325. }
  326. static const char *bpf_cmd[] = {
  327. "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
  328. "MAP_GET_NEXT_KEY", "PROG_LOAD",
  329. };
  330. static DEFINE_STRARRAY(bpf_cmd);
  331. static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
  332. static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1);
  333. static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
  334. static DEFINE_STRARRAY(itimers);
  335. static const char *keyctl_options[] = {
  336. "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
  337. "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
  338. "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
  339. "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
  340. "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
  341. };
  342. static DEFINE_STRARRAY(keyctl_options);
  343. static const char *whences[] = { "SET", "CUR", "END",
  344. #ifdef SEEK_DATA
  345. "DATA",
  346. #endif
  347. #ifdef SEEK_HOLE
  348. "HOLE",
  349. #endif
  350. };
  351. static DEFINE_STRARRAY(whences);
  352. static const char *fcntl_cmds[] = {
  353. "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
  354. "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
  355. "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
  356. "GETOWNER_UIDS",
  357. };
  358. static DEFINE_STRARRAY(fcntl_cmds);
  359. static const char *fcntl_linux_specific_cmds[] = {
  360. "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
  361. "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
  362. "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
  363. };
  364. static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, F_LINUX_SPECIFIC_BASE);
  365. static struct strarray *fcntl_cmds_arrays[] = {
  366. &strarray__fcntl_cmds,
  367. &strarray__fcntl_linux_specific_cmds,
  368. };
  369. static DEFINE_STRARRAYS(fcntl_cmds_arrays);
  370. static const char *rlimit_resources[] = {
  371. "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
  372. "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
  373. "RTTIME",
  374. };
  375. static DEFINE_STRARRAY(rlimit_resources);
  376. static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
  377. static DEFINE_STRARRAY(sighow);
  378. static const char *clockid[] = {
  379. "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
  380. "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
  381. "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
  382. };
  383. static DEFINE_STRARRAY(clockid);
  384. static const char *socket_families[] = {
  385. "UNSPEC", "LOCAL", "INET", "AX25", "IPX", "APPLETALK", "NETROM",
  386. "BRIDGE", "ATMPVC", "X25", "INET6", "ROSE", "DECnet", "NETBEUI",
  387. "SECURITY", "KEY", "NETLINK", "PACKET", "ASH", "ECONET", "ATMSVC",
  388. "RDS", "SNA", "IRDA", "PPPOX", "WANPIPE", "LLC", "IB", "CAN", "TIPC",
  389. "BLUETOOTH", "IUCV", "RXRPC", "ISDN", "PHONET", "IEEE802154", "CAIF",
  390. "ALG", "NFC", "VSOCK",
  391. };
  392. static DEFINE_STRARRAY(socket_families);
  393. static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
  394. struct syscall_arg *arg)
  395. {
  396. size_t printed = 0;
  397. int mode = arg->val;
  398. if (mode == F_OK) /* 0 */
  399. return scnprintf(bf, size, "F");
  400. #define P_MODE(n) \
  401. if (mode & n##_OK) { \
  402. printed += scnprintf(bf + printed, size - printed, "%s", #n); \
  403. mode &= ~n##_OK; \
  404. }
  405. P_MODE(R);
  406. P_MODE(W);
  407. P_MODE(X);
  408. #undef P_MODE
  409. if (mode)
  410. printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
  411. return printed;
  412. }
  413. #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
  414. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  415. struct syscall_arg *arg);
  416. #define SCA_FILENAME syscall_arg__scnprintf_filename
  417. static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
  418. struct syscall_arg *arg)
  419. {
  420. int printed = 0, flags = arg->val;
  421. #define P_FLAG(n) \
  422. if (flags & O_##n) { \
  423. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  424. flags &= ~O_##n; \
  425. }
  426. P_FLAG(CLOEXEC);
  427. P_FLAG(NONBLOCK);
  428. #undef P_FLAG
  429. if (flags)
  430. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  431. return printed;
  432. }
  433. #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
  434. #ifndef GRND_NONBLOCK
  435. #define GRND_NONBLOCK 0x0001
  436. #endif
  437. #ifndef GRND_RANDOM
  438. #define GRND_RANDOM 0x0002
  439. #endif
  440. static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
  441. struct syscall_arg *arg)
  442. {
  443. int printed = 0, flags = arg->val;
  444. #define P_FLAG(n) \
  445. if (flags & GRND_##n) { \
  446. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  447. flags &= ~GRND_##n; \
  448. }
  449. P_FLAG(RANDOM);
  450. P_FLAG(NONBLOCK);
  451. #undef P_FLAG
  452. if (flags)
  453. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  454. return printed;
  455. }
  456. #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
  457. #define STRARRAY(name, array) \
  458. { .scnprintf = SCA_STRARRAY, \
  459. .parm = &strarray__##array, }
  460. #include "trace/beauty/arch_errno_names.c"
  461. #include "trace/beauty/eventfd.c"
  462. #include "trace/beauty/futex_op.c"
  463. #include "trace/beauty/futex_val3.c"
  464. #include "trace/beauty/mmap.c"
  465. #include "trace/beauty/mode_t.c"
  466. #include "trace/beauty/msg_flags.c"
  467. #include "trace/beauty/open_flags.c"
  468. #include "trace/beauty/perf_event_open.c"
  469. #include "trace/beauty/pid.c"
  470. #include "trace/beauty/sched_policy.c"
  471. #include "trace/beauty/seccomp.c"
  472. #include "trace/beauty/signum.c"
  473. #include "trace/beauty/socket_type.c"
  474. #include "trace/beauty/waitid_options.c"
  475. struct syscall_arg_fmt {
  476. size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  477. void *parm;
  478. const char *name;
  479. bool show_zero;
  480. };
  481. static struct syscall_fmt {
  482. const char *name;
  483. const char *alias;
  484. struct syscall_arg_fmt arg[6];
  485. u8 nr_args;
  486. bool errpid;
  487. bool timeout;
  488. bool hexret;
  489. } syscall_fmts[] = {
  490. { .name = "access",
  491. .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
  492. { .name = "bpf",
  493. .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
  494. { .name = "brk", .hexret = true,
  495. .arg = { [0] = { .scnprintf = SCA_HEX, /* brk */ }, }, },
  496. { .name = "clock_gettime",
  497. .arg = { [0] = STRARRAY(clk_id, clockid), }, },
  498. { .name = "clone", .errpid = true, .nr_args = 5,
  499. .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
  500. [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
  501. [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
  502. [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
  503. [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
  504. { .name = "close",
  505. .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
  506. { .name = "epoll_ctl",
  507. .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
  508. { .name = "eventfd2",
  509. .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
  510. { .name = "fchmodat",
  511. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  512. { .name = "fchownat",
  513. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  514. { .name = "fcntl",
  515. .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
  516. .parm = &strarrays__fcntl_cmds_arrays,
  517. .show_zero = true, },
  518. [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
  519. { .name = "flock",
  520. .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
  521. { .name = "fstat", .alias = "newfstat", },
  522. { .name = "fstatat", .alias = "newfstatat", },
  523. { .name = "futex",
  524. .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
  525. [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
  526. { .name = "futimesat",
  527. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  528. { .name = "getitimer",
  529. .arg = { [0] = STRARRAY(which, itimers), }, },
  530. { .name = "getpid", .errpid = true, },
  531. { .name = "getpgid", .errpid = true, },
  532. { .name = "getppid", .errpid = true, },
  533. { .name = "getrandom",
  534. .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
  535. { .name = "getrlimit",
  536. .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
  537. { .name = "gettid", .errpid = true, },
  538. { .name = "ioctl",
  539. .arg = {
  540. #if defined(__i386__) || defined(__x86_64__)
  541. /*
  542. * FIXME: Make this available to all arches.
  543. */
  544. [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
  545. [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
  546. #else
  547. [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
  548. #endif
  549. { .name = "kcmp", .nr_args = 5,
  550. .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
  551. [1] = { .name = "pid2", .scnprintf = SCA_PID, },
  552. [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
  553. [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
  554. [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
  555. { .name = "keyctl",
  556. .arg = { [0] = STRARRAY(option, keyctl_options), }, },
  557. { .name = "kill",
  558. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  559. { .name = "linkat",
  560. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  561. { .name = "lseek",
  562. .arg = { [2] = STRARRAY(whence, whences), }, },
  563. { .name = "lstat", .alias = "newlstat", },
  564. { .name = "madvise",
  565. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  566. [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
  567. { .name = "mkdirat",
  568. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  569. { .name = "mknodat",
  570. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  571. { .name = "mlock",
  572. .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
  573. { .name = "mlockall",
  574. .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
  575. { .name = "mmap", .hexret = true,
  576. /* The standard mmap maps to old_mmap on s390x */
  577. #if defined(__s390x__)
  578. .alias = "old_mmap",
  579. #endif
  580. .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
  581. [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
  582. [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ }, }, },
  583. { .name = "mprotect",
  584. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  585. [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
  586. { .name = "mq_unlink",
  587. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
  588. { .name = "mremap", .hexret = true,
  589. .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
  590. [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ },
  591. [4] = { .scnprintf = SCA_HEX, /* new_addr */ }, }, },
  592. { .name = "munlock",
  593. .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
  594. { .name = "munmap",
  595. .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
  596. { .name = "name_to_handle_at",
  597. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  598. { .name = "newfstatat",
  599. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  600. { .name = "open",
  601. .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  602. { .name = "open_by_handle_at",
  603. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  604. [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  605. { .name = "openat",
  606. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  607. [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  608. { .name = "perf_event_open",
  609. .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
  610. [3] = { .scnprintf = SCA_FD, /* group_fd */ },
  611. [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
  612. { .name = "pipe2",
  613. .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
  614. { .name = "pkey_alloc",
  615. .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
  616. { .name = "pkey_free",
  617. .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
  618. { .name = "pkey_mprotect",
  619. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  620. [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
  621. [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
  622. { .name = "poll", .timeout = true, },
  623. { .name = "ppoll", .timeout = true, },
  624. { .name = "prctl", .alias = "arch_prctl",
  625. .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
  626. [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
  627. [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
  628. { .name = "pread", .alias = "pread64", },
  629. { .name = "preadv", .alias = "pread", },
  630. { .name = "prlimit64",
  631. .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
  632. { .name = "pwrite", .alias = "pwrite64", },
  633. { .name = "readlinkat",
  634. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  635. { .name = "recvfrom",
  636. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  637. { .name = "recvmmsg",
  638. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  639. { .name = "recvmsg",
  640. .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  641. { .name = "renameat",
  642. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  643. { .name = "rt_sigaction",
  644. .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  645. { .name = "rt_sigprocmask",
  646. .arg = { [0] = STRARRAY(how, sighow), }, },
  647. { .name = "rt_sigqueueinfo",
  648. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  649. { .name = "rt_tgsigqueueinfo",
  650. .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  651. { .name = "sched_setscheduler",
  652. .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
  653. { .name = "seccomp",
  654. .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
  655. [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
  656. { .name = "select", .timeout = true, },
  657. { .name = "sendmmsg",
  658. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  659. { .name = "sendmsg",
  660. .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  661. { .name = "sendto",
  662. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  663. { .name = "set_tid_address", .errpid = true, },
  664. { .name = "setitimer",
  665. .arg = { [0] = STRARRAY(which, itimers), }, },
  666. { .name = "setrlimit",
  667. .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
  668. { .name = "socket",
  669. .arg = { [0] = STRARRAY(family, socket_families),
  670. [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
  671. [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
  672. { .name = "socketpair",
  673. .arg = { [0] = STRARRAY(family, socket_families),
  674. [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
  675. [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
  676. { .name = "stat", .alias = "newstat", },
  677. { .name = "statx",
  678. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
  679. [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
  680. [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
  681. { .name = "swapoff",
  682. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
  683. { .name = "swapon",
  684. .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
  685. { .name = "symlinkat",
  686. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  687. { .name = "tgkill",
  688. .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  689. { .name = "tkill",
  690. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  691. { .name = "uname", .alias = "newuname", },
  692. { .name = "unlinkat",
  693. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  694. { .name = "utimensat",
  695. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
  696. { .name = "wait4", .errpid = true,
  697. .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
  698. { .name = "waitid", .errpid = true,
  699. .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
  700. };
  701. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  702. {
  703. const struct syscall_fmt *fmt = fmtp;
  704. return strcmp(name, fmt->name);
  705. }
  706. static struct syscall_fmt *syscall_fmt__find(const char *name)
  707. {
  708. const int nmemb = ARRAY_SIZE(syscall_fmts);
  709. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  710. }
  711. /*
  712. * is_exit: is this "exit" or "exit_group"?
  713. * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter.
  714. */
  715. struct syscall {
  716. struct event_format *tp_format;
  717. int nr_args;
  718. bool is_exit;
  719. bool is_open;
  720. struct format_field *args;
  721. const char *name;
  722. struct syscall_fmt *fmt;
  723. struct syscall_arg_fmt *arg_fmt;
  724. };
  725. /*
  726. * We need to have this 'calculated' boolean because in some cases we really
  727. * don't know what is the duration of a syscall, for instance, when we start
  728. * a session and some threads are waiting for a syscall to finish, say 'poll',
  729. * in which case all we can do is to print "( ? ) for duration and for the
  730. * start timestamp.
  731. */
  732. static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
  733. {
  734. double duration = (double)t / NSEC_PER_MSEC;
  735. size_t printed = fprintf(fp, "(");
  736. if (!calculated)
  737. printed += fprintf(fp, " ");
  738. else if (duration >= 1.0)
  739. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  740. else if (duration >= 0.01)
  741. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  742. else
  743. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  744. return printed + fprintf(fp, "): ");
  745. }
  746. /**
  747. * filename.ptr: The filename char pointer that will be vfs_getname'd
  748. * filename.entry_str_pos: Where to insert the string translated from
  749. * filename.ptr by the vfs_getname tracepoint/kprobe.
  750. * ret_scnprintf: syscall args may set this to a different syscall return
  751. * formatter, for instance, fcntl may return fds, file flags, etc.
  752. */
  753. struct thread_trace {
  754. u64 entry_time;
  755. bool entry_pending;
  756. unsigned long nr_events;
  757. unsigned long pfmaj, pfmin;
  758. char *entry_str;
  759. double runtime_ms;
  760. size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  761. struct {
  762. unsigned long ptr;
  763. short int entry_str_pos;
  764. bool pending_open;
  765. unsigned int namelen;
  766. char *name;
  767. } filename;
  768. struct {
  769. int max;
  770. char **table;
  771. } paths;
  772. struct intlist *syscall_stats;
  773. };
  774. static struct thread_trace *thread_trace__new(void)
  775. {
  776. struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
  777. if (ttrace)
  778. ttrace->paths.max = -1;
  779. ttrace->syscall_stats = intlist__new(NULL);
  780. return ttrace;
  781. }
  782. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  783. {
  784. struct thread_trace *ttrace;
  785. if (thread == NULL)
  786. goto fail;
  787. if (thread__priv(thread) == NULL)
  788. thread__set_priv(thread, thread_trace__new());
  789. if (thread__priv(thread) == NULL)
  790. goto fail;
  791. ttrace = thread__priv(thread);
  792. ++ttrace->nr_events;
  793. return ttrace;
  794. fail:
  795. color_fprintf(fp, PERF_COLOR_RED,
  796. "WARNING: not enough memory, dropping samples!\n");
  797. return NULL;
  798. }
  799. void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
  800. size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
  801. {
  802. struct thread_trace *ttrace = thread__priv(arg->thread);
  803. ttrace->ret_scnprintf = ret_scnprintf;
  804. }
  805. #define TRACE_PFMAJ (1 << 0)
  806. #define TRACE_PFMIN (1 << 1)
  807. static const size_t trace__entry_str_size = 2048;
  808. static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
  809. {
  810. struct thread_trace *ttrace = thread__priv(thread);
  811. if (fd > ttrace->paths.max) {
  812. char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
  813. if (npath == NULL)
  814. return -1;
  815. if (ttrace->paths.max != -1) {
  816. memset(npath + ttrace->paths.max + 1, 0,
  817. (fd - ttrace->paths.max) * sizeof(char *));
  818. } else {
  819. memset(npath, 0, (fd + 1) * sizeof(char *));
  820. }
  821. ttrace->paths.table = npath;
  822. ttrace->paths.max = fd;
  823. }
  824. ttrace->paths.table[fd] = strdup(pathname);
  825. return ttrace->paths.table[fd] != NULL ? 0 : -1;
  826. }
  827. static int thread__read_fd_path(struct thread *thread, int fd)
  828. {
  829. char linkname[PATH_MAX], pathname[PATH_MAX];
  830. struct stat st;
  831. int ret;
  832. if (thread->pid_ == thread->tid) {
  833. scnprintf(linkname, sizeof(linkname),
  834. "/proc/%d/fd/%d", thread->pid_, fd);
  835. } else {
  836. scnprintf(linkname, sizeof(linkname),
  837. "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
  838. }
  839. if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
  840. return -1;
  841. ret = readlink(linkname, pathname, sizeof(pathname));
  842. if (ret < 0 || ret > st.st_size)
  843. return -1;
  844. pathname[ret] = '\0';
  845. return trace__set_fd_pathname(thread, fd, pathname);
  846. }
  847. static const char *thread__fd_path(struct thread *thread, int fd,
  848. struct trace *trace)
  849. {
  850. struct thread_trace *ttrace = thread__priv(thread);
  851. if (ttrace == NULL)
  852. return NULL;
  853. if (fd < 0)
  854. return NULL;
  855. if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
  856. if (!trace->live)
  857. return NULL;
  858. ++trace->stats.proc_getname;
  859. if (thread__read_fd_path(thread, fd))
  860. return NULL;
  861. }
  862. return ttrace->paths.table[fd];
  863. }
  864. size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
  865. {
  866. int fd = arg->val;
  867. size_t printed = scnprintf(bf, size, "%d", fd);
  868. const char *path = thread__fd_path(arg->thread, fd, arg->trace);
  869. if (path)
  870. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  871. return printed;
  872. }
  873. size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
  874. {
  875. size_t printed = scnprintf(bf, size, "%d", fd);
  876. struct thread *thread = machine__find_thread(trace->host, pid, pid);
  877. if (thread) {
  878. const char *path = thread__fd_path(thread, fd, trace);
  879. if (path)
  880. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  881. thread__put(thread);
  882. }
  883. return printed;
  884. }
  885. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  886. struct syscall_arg *arg)
  887. {
  888. int fd = arg->val;
  889. size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
  890. struct thread_trace *ttrace = thread__priv(arg->thread);
  891. if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
  892. zfree(&ttrace->paths.table[fd]);
  893. return printed;
  894. }
  895. static void thread__set_filename_pos(struct thread *thread, const char *bf,
  896. unsigned long ptr)
  897. {
  898. struct thread_trace *ttrace = thread__priv(thread);
  899. ttrace->filename.ptr = ptr;
  900. ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
  901. }
  902. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  903. struct syscall_arg *arg)
  904. {
  905. unsigned long ptr = arg->val;
  906. if (!arg->trace->vfs_getname)
  907. return scnprintf(bf, size, "%#x", ptr);
  908. thread__set_filename_pos(arg->thread, bf, ptr);
  909. return 0;
  910. }
  911. static bool trace__filter_duration(struct trace *trace, double t)
  912. {
  913. return t < (trace->duration_filter * NSEC_PER_MSEC);
  914. }
  915. static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  916. {
  917. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  918. return fprintf(fp, "%10.3f ", ts);
  919. }
  920. /*
  921. * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
  922. * using ttrace->entry_time for a thread that receives a sys_exit without
  923. * first having received a sys_enter ("poll" issued before tracing session
  924. * starts, lost sys_enter exit due to ring buffer overflow).
  925. */
  926. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  927. {
  928. if (tstamp > 0)
  929. return __trace__fprintf_tstamp(trace, tstamp, fp);
  930. return fprintf(fp, " ? ");
  931. }
  932. static bool done = false;
  933. static bool interrupted = false;
  934. static void sig_handler(int sig)
  935. {
  936. done = true;
  937. interrupted = sig == SIGINT;
  938. }
  939. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  940. u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
  941. {
  942. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  943. printed += fprintf_duration(duration, duration_calculated, fp);
  944. if (trace->multiple_threads) {
  945. if (trace->show_comm)
  946. printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
  947. printed += fprintf(fp, "%d ", thread->tid);
  948. }
  949. return printed;
  950. }
  951. static int trace__process_event(struct trace *trace, struct machine *machine,
  952. union perf_event *event, struct perf_sample *sample)
  953. {
  954. int ret = 0;
  955. switch (event->header.type) {
  956. case PERF_RECORD_LOST:
  957. color_fprintf(trace->output, PERF_COLOR_RED,
  958. "LOST %" PRIu64 " events!\n", event->lost.lost);
  959. ret = machine__process_lost_event(machine, event, sample);
  960. break;
  961. default:
  962. ret = machine__process_event(machine, event, sample);
  963. break;
  964. }
  965. return ret;
  966. }
  967. static int trace__tool_process(struct perf_tool *tool,
  968. union perf_event *event,
  969. struct perf_sample *sample,
  970. struct machine *machine)
  971. {
  972. struct trace *trace = container_of(tool, struct trace, tool);
  973. return trace__process_event(trace, machine, event, sample);
  974. }
  975. static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  976. {
  977. struct machine *machine = vmachine;
  978. if (machine->kptr_restrict_warned)
  979. return NULL;
  980. if (symbol_conf.kptr_restrict) {
  981. pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  982. "Check /proc/sys/kernel/kptr_restrict.\n\n"
  983. "Kernel samples will not be resolved.\n");
  984. machine->kptr_restrict_warned = true;
  985. return NULL;
  986. }
  987. return machine__resolve_kernel_addr(vmachine, addrp, modp);
  988. }
  989. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  990. {
  991. int err = symbol__init(NULL);
  992. if (err)
  993. return err;
  994. trace->host = machine__new_host();
  995. if (trace->host == NULL)
  996. return -ENOMEM;
  997. err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
  998. if (err < 0)
  999. goto out;
  1000. err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
  1001. evlist->threads, trace__tool_process, false,
  1002. trace->opts.proc_map_timeout, 1);
  1003. out:
  1004. if (err)
  1005. symbol__exit();
  1006. return err;
  1007. }
  1008. static void trace__symbols__exit(struct trace *trace)
  1009. {
  1010. machine__exit(trace->host);
  1011. trace->host = NULL;
  1012. symbol__exit();
  1013. }
  1014. static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
  1015. {
  1016. int idx;
  1017. if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
  1018. nr_args = sc->fmt->nr_args;
  1019. sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
  1020. if (sc->arg_fmt == NULL)
  1021. return -1;
  1022. for (idx = 0; idx < nr_args; ++idx) {
  1023. if (sc->fmt)
  1024. sc->arg_fmt[idx] = sc->fmt->arg[idx];
  1025. }
  1026. sc->nr_args = nr_args;
  1027. return 0;
  1028. }
  1029. static int syscall__set_arg_fmts(struct syscall *sc)
  1030. {
  1031. struct format_field *field;
  1032. int idx = 0, len;
  1033. for (field = sc->args; field; field = field->next, ++idx) {
  1034. if (sc->fmt && sc->fmt->arg[idx].scnprintf)
  1035. continue;
  1036. if (strcmp(field->type, "const char *") == 0 &&
  1037. (strcmp(field->name, "filename") == 0 ||
  1038. strcmp(field->name, "path") == 0 ||
  1039. strcmp(field->name, "pathname") == 0))
  1040. sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
  1041. else if (field->flags & FIELD_IS_POINTER)
  1042. sc->arg_fmt[idx].scnprintf = syscall_arg__scnprintf_hex;
  1043. else if (strcmp(field->type, "pid_t") == 0)
  1044. sc->arg_fmt[idx].scnprintf = SCA_PID;
  1045. else if (strcmp(field->type, "umode_t") == 0)
  1046. sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
  1047. else if ((strcmp(field->type, "int") == 0 ||
  1048. strcmp(field->type, "unsigned int") == 0 ||
  1049. strcmp(field->type, "long") == 0) &&
  1050. (len = strlen(field->name)) >= 2 &&
  1051. strcmp(field->name + len - 2, "fd") == 0) {
  1052. /*
  1053. * /sys/kernel/tracing/events/syscalls/sys_enter*
  1054. * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
  1055. * 65 int
  1056. * 23 unsigned int
  1057. * 7 unsigned long
  1058. */
  1059. sc->arg_fmt[idx].scnprintf = SCA_FD;
  1060. }
  1061. }
  1062. return 0;
  1063. }
  1064. static int trace__read_syscall_info(struct trace *trace, int id)
  1065. {
  1066. char tp_name[128];
  1067. struct syscall *sc;
  1068. const char *name = syscalltbl__name(trace->sctbl, id);
  1069. if (name == NULL)
  1070. return -1;
  1071. if (id > trace->syscalls.max) {
  1072. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  1073. if (nsyscalls == NULL)
  1074. return -1;
  1075. if (trace->syscalls.max != -1) {
  1076. memset(nsyscalls + trace->syscalls.max + 1, 0,
  1077. (id - trace->syscalls.max) * sizeof(*sc));
  1078. } else {
  1079. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  1080. }
  1081. trace->syscalls.table = nsyscalls;
  1082. trace->syscalls.max = id;
  1083. }
  1084. sc = trace->syscalls.table + id;
  1085. sc->name = name;
  1086. sc->fmt = syscall_fmt__find(sc->name);
  1087. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  1088. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1089. if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
  1090. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  1091. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1092. }
  1093. if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
  1094. return -1;
  1095. if (IS_ERR(sc->tp_format))
  1096. return -1;
  1097. sc->args = sc->tp_format->format.fields;
  1098. /*
  1099. * We need to check and discard the first variable '__syscall_nr'
  1100. * or 'nr' that mean the syscall number. It is needless here.
  1101. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
  1102. */
  1103. if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
  1104. sc->args = sc->args->next;
  1105. --sc->nr_args;
  1106. }
  1107. sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
  1108. sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
  1109. return syscall__set_arg_fmts(sc);
  1110. }
  1111. static int trace__validate_ev_qualifier(struct trace *trace)
  1112. {
  1113. int err = 0, i;
  1114. size_t nr_allocated;
  1115. struct str_node *pos;
  1116. trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
  1117. trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
  1118. sizeof(trace->ev_qualifier_ids.entries[0]));
  1119. if (trace->ev_qualifier_ids.entries == NULL) {
  1120. fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
  1121. trace->output);
  1122. err = -EINVAL;
  1123. goto out;
  1124. }
  1125. nr_allocated = trace->ev_qualifier_ids.nr;
  1126. i = 0;
  1127. strlist__for_each_entry(pos, trace->ev_qualifier) {
  1128. const char *sc = pos->s;
  1129. int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
  1130. if (id < 0) {
  1131. id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
  1132. if (id >= 0)
  1133. goto matches;
  1134. if (err == 0) {
  1135. fputs("Error:\tInvalid syscall ", trace->output);
  1136. err = -EINVAL;
  1137. } else {
  1138. fputs(", ", trace->output);
  1139. }
  1140. fputs(sc, trace->output);
  1141. }
  1142. matches:
  1143. trace->ev_qualifier_ids.entries[i++] = id;
  1144. if (match_next == -1)
  1145. continue;
  1146. while (1) {
  1147. id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
  1148. if (id < 0)
  1149. break;
  1150. if (nr_allocated == trace->ev_qualifier_ids.nr) {
  1151. void *entries;
  1152. nr_allocated += 8;
  1153. entries = realloc(trace->ev_qualifier_ids.entries,
  1154. nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
  1155. if (entries == NULL) {
  1156. err = -ENOMEM;
  1157. fputs("\nError:\t Not enough memory for parsing\n", trace->output);
  1158. goto out_free;
  1159. }
  1160. trace->ev_qualifier_ids.entries = entries;
  1161. }
  1162. trace->ev_qualifier_ids.nr++;
  1163. trace->ev_qualifier_ids.entries[i++] = id;
  1164. }
  1165. }
  1166. if (err < 0) {
  1167. fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
  1168. "\nHint:\tand: 'man syscalls'\n", trace->output);
  1169. out_free:
  1170. zfree(&trace->ev_qualifier_ids.entries);
  1171. trace->ev_qualifier_ids.nr = 0;
  1172. }
  1173. out:
  1174. return err;
  1175. }
  1176. /*
  1177. * args is to be interpreted as a series of longs but we need to handle
  1178. * 8-byte unaligned accesses. args points to raw_data within the event
  1179. * and raw_data is guaranteed to be 8-byte unaligned because it is
  1180. * preceded by raw_size which is a u32. So we need to copy args to a temp
  1181. * variable to read it. Most notably this avoids extended load instructions
  1182. * on unaligned addresses
  1183. */
  1184. unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
  1185. {
  1186. unsigned long val;
  1187. unsigned char *p = arg->args + sizeof(unsigned long) * idx;
  1188. memcpy(&val, p, sizeof(val));
  1189. return val;
  1190. }
  1191. static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
  1192. struct syscall_arg *arg)
  1193. {
  1194. if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
  1195. return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
  1196. return scnprintf(bf, size, "arg%d: ", arg->idx);
  1197. }
  1198. static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
  1199. struct syscall_arg *arg, unsigned long val)
  1200. {
  1201. if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
  1202. arg->val = val;
  1203. if (sc->arg_fmt[arg->idx].parm)
  1204. arg->parm = sc->arg_fmt[arg->idx].parm;
  1205. return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
  1206. }
  1207. return scnprintf(bf, size, "%ld", val);
  1208. }
  1209. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  1210. unsigned char *args, struct trace *trace,
  1211. struct thread *thread)
  1212. {
  1213. size_t printed = 0;
  1214. unsigned long val;
  1215. u8 bit = 1;
  1216. struct syscall_arg arg = {
  1217. .args = args,
  1218. .idx = 0,
  1219. .mask = 0,
  1220. .trace = trace,
  1221. .thread = thread,
  1222. };
  1223. struct thread_trace *ttrace = thread__priv(thread);
  1224. /*
  1225. * Things like fcntl will set this in its 'cmd' formatter to pick the
  1226. * right formatter for the return value (an fd? file flags?), which is
  1227. * not needed for syscalls that always return a given type, say an fd.
  1228. */
  1229. ttrace->ret_scnprintf = NULL;
  1230. if (sc->args != NULL) {
  1231. struct format_field *field;
  1232. for (field = sc->args; field;
  1233. field = field->next, ++arg.idx, bit <<= 1) {
  1234. if (arg.mask & bit)
  1235. continue;
  1236. val = syscall_arg__val(&arg, arg.idx);
  1237. /*
  1238. * Suppress this argument if its value is zero and
  1239. * and we don't have a string associated in an
  1240. * strarray for it.
  1241. */
  1242. if (val == 0 &&
  1243. !(sc->arg_fmt &&
  1244. (sc->arg_fmt[arg.idx].show_zero ||
  1245. sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
  1246. sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
  1247. sc->arg_fmt[arg.idx].parm))
  1248. continue;
  1249. printed += scnprintf(bf + printed, size - printed,
  1250. "%s%s: ", printed ? ", " : "", field->name);
  1251. printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
  1252. }
  1253. } else if (IS_ERR(sc->tp_format)) {
  1254. /*
  1255. * If we managed to read the tracepoint /format file, then we
  1256. * may end up not having any args, like with gettid(), so only
  1257. * print the raw args when we didn't manage to read it.
  1258. */
  1259. while (arg.idx < sc->nr_args) {
  1260. if (arg.mask & bit)
  1261. goto next_arg;
  1262. val = syscall_arg__val(&arg, arg.idx);
  1263. if (printed)
  1264. printed += scnprintf(bf + printed, size - printed, ", ");
  1265. printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
  1266. printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
  1267. next_arg:
  1268. ++arg.idx;
  1269. bit <<= 1;
  1270. }
  1271. }
  1272. return printed;
  1273. }
  1274. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  1275. union perf_event *event,
  1276. struct perf_sample *sample);
  1277. static struct syscall *trace__syscall_info(struct trace *trace,
  1278. struct perf_evsel *evsel, int id)
  1279. {
  1280. if (id < 0) {
  1281. /*
  1282. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  1283. * before that, leaving at a higher verbosity level till that is
  1284. * explained. Reproduced with plain ftrace with:
  1285. *
  1286. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  1287. * grep "NR -1 " /t/trace_pipe
  1288. *
  1289. * After generating some load on the machine.
  1290. */
  1291. if (verbose > 1) {
  1292. static u64 n;
  1293. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  1294. id, perf_evsel__name(evsel), ++n);
  1295. }
  1296. return NULL;
  1297. }
  1298. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  1299. trace__read_syscall_info(trace, id))
  1300. goto out_cant_read;
  1301. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  1302. goto out_cant_read;
  1303. return &trace->syscalls.table[id];
  1304. out_cant_read:
  1305. if (verbose > 0) {
  1306. fprintf(trace->output, "Problems reading syscall %d", id);
  1307. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  1308. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  1309. fputs(" information\n", trace->output);
  1310. }
  1311. return NULL;
  1312. }
  1313. static void thread__update_stats(struct thread_trace *ttrace,
  1314. int id, struct perf_sample *sample)
  1315. {
  1316. struct int_node *inode;
  1317. struct stats *stats;
  1318. u64 duration = 0;
  1319. inode = intlist__findnew(ttrace->syscall_stats, id);
  1320. if (inode == NULL)
  1321. return;
  1322. stats = inode->priv;
  1323. if (stats == NULL) {
  1324. stats = malloc(sizeof(struct stats));
  1325. if (stats == NULL)
  1326. return;
  1327. init_stats(stats);
  1328. inode->priv = stats;
  1329. }
  1330. if (ttrace->entry_time && sample->time > ttrace->entry_time)
  1331. duration = sample->time - ttrace->entry_time;
  1332. update_stats(stats, duration);
  1333. }
  1334. static int trace__printf_interrupted_entry(struct trace *trace)
  1335. {
  1336. struct thread_trace *ttrace;
  1337. size_t printed;
  1338. if (trace->failure_only || trace->current == NULL)
  1339. return 0;
  1340. ttrace = thread__priv(trace->current);
  1341. if (!ttrace->entry_pending)
  1342. return 0;
  1343. printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
  1344. printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
  1345. ttrace->entry_pending = false;
  1346. return printed;
  1347. }
  1348. static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
  1349. struct perf_sample *sample, struct thread *thread)
  1350. {
  1351. int printed = 0;
  1352. if (trace->print_sample) {
  1353. double ts = (double)sample->time / NSEC_PER_MSEC;
  1354. printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
  1355. perf_evsel__name(evsel), ts,
  1356. thread__comm_str(thread),
  1357. sample->pid, sample->tid, sample->cpu);
  1358. }
  1359. return printed;
  1360. }
  1361. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  1362. union perf_event *event __maybe_unused,
  1363. struct perf_sample *sample)
  1364. {
  1365. char *msg;
  1366. void *args;
  1367. size_t printed = 0;
  1368. struct thread *thread;
  1369. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  1370. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1371. struct thread_trace *ttrace;
  1372. if (sc == NULL)
  1373. return -1;
  1374. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1375. ttrace = thread__trace(thread, trace->output);
  1376. if (ttrace == NULL)
  1377. goto out_put;
  1378. trace__fprintf_sample(trace, evsel, sample, thread);
  1379. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  1380. if (ttrace->entry_str == NULL) {
  1381. ttrace->entry_str = malloc(trace__entry_str_size);
  1382. if (!ttrace->entry_str)
  1383. goto out_put;
  1384. }
  1385. if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
  1386. trace__printf_interrupted_entry(trace);
  1387. ttrace->entry_time = sample->time;
  1388. msg = ttrace->entry_str;
  1389. printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
  1390. printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
  1391. args, trace, thread);
  1392. if (sc->is_exit) {
  1393. if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
  1394. trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
  1395. fprintf(trace->output, "%-70s)\n", ttrace->entry_str);
  1396. }
  1397. } else {
  1398. ttrace->entry_pending = true;
  1399. /* See trace__vfs_getname & trace__sys_exit */
  1400. ttrace->filename.pending_open = false;
  1401. }
  1402. if (trace->current != thread) {
  1403. thread__put(trace->current);
  1404. trace->current = thread__get(thread);
  1405. }
  1406. err = 0;
  1407. out_put:
  1408. thread__put(thread);
  1409. return err;
  1410. }
  1411. static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
  1412. struct perf_sample *sample)
  1413. {
  1414. struct format_field *field = perf_evsel__field(evsel, "__syscall_nr");
  1415. struct thread_trace *ttrace;
  1416. struct thread *thread;
  1417. struct syscall *sc;
  1418. char msg[1024];
  1419. int id, err = -1;
  1420. void *args;
  1421. if (field == NULL)
  1422. return -1;
  1423. id = format_field__intval(field, sample, evsel->needs_swap);
  1424. sc = trace__syscall_info(trace, evsel, id);
  1425. if (sc == NULL)
  1426. return -1;
  1427. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1428. ttrace = thread__trace(thread, trace->output);
  1429. /*
  1430. * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
  1431. * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
  1432. */
  1433. if (ttrace == NULL)
  1434. goto out_put;
  1435. args = sample->raw_data + field->offset + sizeof(u64); /* skip __syscall_nr, there is where args are */
  1436. syscall__scnprintf_args(sc, msg, sizeof(msg), args, trace, thread);
  1437. fprintf(trace->output, "%s", msg);
  1438. err = 0;
  1439. out_put:
  1440. thread__put(thread);
  1441. return err;
  1442. }
  1443. static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
  1444. struct perf_sample *sample,
  1445. struct callchain_cursor *cursor)
  1446. {
  1447. struct addr_location al;
  1448. int max_stack = evsel->attr.sample_max_stack ?
  1449. evsel->attr.sample_max_stack :
  1450. trace->max_stack;
  1451. if (machine__resolve(trace->host, &al, sample) < 0 ||
  1452. thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack))
  1453. return -1;
  1454. return 0;
  1455. }
  1456. static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
  1457. {
  1458. /* TODO: user-configurable print_opts */
  1459. const unsigned int print_opts = EVSEL__PRINT_SYM |
  1460. EVSEL__PRINT_DSO |
  1461. EVSEL__PRINT_UNKNOWN_AS_ADDR;
  1462. return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
  1463. }
  1464. static const char *errno_to_name(struct perf_evsel *evsel, int err)
  1465. {
  1466. struct perf_env *env = perf_evsel__env(evsel);
  1467. const char *arch_name = perf_env__arch(env);
  1468. return arch_syscalls__strerrno(arch_name, err);
  1469. }
  1470. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  1471. union perf_event *event __maybe_unused,
  1472. struct perf_sample *sample)
  1473. {
  1474. long ret;
  1475. u64 duration = 0;
  1476. bool duration_calculated = false;
  1477. struct thread *thread;
  1478. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0;
  1479. struct syscall *sc = trace__syscall_info(trace, evsel, id);
  1480. struct thread_trace *ttrace;
  1481. if (sc == NULL)
  1482. return -1;
  1483. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1484. ttrace = thread__trace(thread, trace->output);
  1485. if (ttrace == NULL)
  1486. goto out_put;
  1487. trace__fprintf_sample(trace, evsel, sample, thread);
  1488. if (trace->summary)
  1489. thread__update_stats(ttrace, id, sample);
  1490. ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
  1491. if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
  1492. trace__set_fd_pathname(thread, ret, ttrace->filename.name);
  1493. ttrace->filename.pending_open = false;
  1494. ++trace->stats.vfs_getname;
  1495. }
  1496. if (ttrace->entry_time) {
  1497. duration = sample->time - ttrace->entry_time;
  1498. if (trace__filter_duration(trace, duration))
  1499. goto out;
  1500. duration_calculated = true;
  1501. } else if (trace->duration_filter)
  1502. goto out;
  1503. if (sample->callchain) {
  1504. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1505. if (callchain_ret == 0) {
  1506. if (callchain_cursor.nr < trace->min_stack)
  1507. goto out;
  1508. callchain_ret = 1;
  1509. }
  1510. }
  1511. if (trace->summary_only || (ret >= 0 && trace->failure_only))
  1512. goto out;
  1513. trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
  1514. if (ttrace->entry_pending) {
  1515. fprintf(trace->output, "%-70s", ttrace->entry_str);
  1516. } else {
  1517. fprintf(trace->output, " ... [");
  1518. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  1519. fprintf(trace->output, "]: %s()", sc->name);
  1520. }
  1521. if (sc->fmt == NULL) {
  1522. if (ret < 0)
  1523. goto errno_print;
  1524. signed_print:
  1525. fprintf(trace->output, ") = %ld", ret);
  1526. } else if (ret < 0) {
  1527. errno_print: {
  1528. char bf[STRERR_BUFSIZE];
  1529. const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
  1530. *e = errno_to_name(evsel, -ret);
  1531. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  1532. }
  1533. } else if (ret == 0 && sc->fmt->timeout)
  1534. fprintf(trace->output, ") = 0 Timeout");
  1535. else if (ttrace->ret_scnprintf) {
  1536. char bf[1024];
  1537. struct syscall_arg arg = {
  1538. .val = ret,
  1539. .thread = thread,
  1540. .trace = trace,
  1541. };
  1542. ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
  1543. ttrace->ret_scnprintf = NULL;
  1544. fprintf(trace->output, ") = %s", bf);
  1545. } else if (sc->fmt->hexret)
  1546. fprintf(trace->output, ") = %#lx", ret);
  1547. else if (sc->fmt->errpid) {
  1548. struct thread *child = machine__find_thread(trace->host, ret, ret);
  1549. if (child != NULL) {
  1550. fprintf(trace->output, ") = %ld", ret);
  1551. if (child->comm_set)
  1552. fprintf(trace->output, " (%s)", thread__comm_str(child));
  1553. thread__put(child);
  1554. }
  1555. } else
  1556. goto signed_print;
  1557. fputc('\n', trace->output);
  1558. if (callchain_ret > 0)
  1559. trace__fprintf_callchain(trace, sample);
  1560. else if (callchain_ret < 0)
  1561. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1562. out:
  1563. ttrace->entry_pending = false;
  1564. err = 0;
  1565. out_put:
  1566. thread__put(thread);
  1567. return err;
  1568. }
  1569. static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
  1570. union perf_event *event __maybe_unused,
  1571. struct perf_sample *sample)
  1572. {
  1573. struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1574. struct thread_trace *ttrace;
  1575. size_t filename_len, entry_str_len, to_move;
  1576. ssize_t remaining_space;
  1577. char *pos;
  1578. const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
  1579. if (!thread)
  1580. goto out;
  1581. ttrace = thread__priv(thread);
  1582. if (!ttrace)
  1583. goto out_put;
  1584. filename_len = strlen(filename);
  1585. if (filename_len == 0)
  1586. goto out_put;
  1587. if (ttrace->filename.namelen < filename_len) {
  1588. char *f = realloc(ttrace->filename.name, filename_len + 1);
  1589. if (f == NULL)
  1590. goto out_put;
  1591. ttrace->filename.namelen = filename_len;
  1592. ttrace->filename.name = f;
  1593. }
  1594. strcpy(ttrace->filename.name, filename);
  1595. ttrace->filename.pending_open = true;
  1596. if (!ttrace->filename.ptr)
  1597. goto out_put;
  1598. entry_str_len = strlen(ttrace->entry_str);
  1599. remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
  1600. if (remaining_space <= 0)
  1601. goto out_put;
  1602. if (filename_len > (size_t)remaining_space) {
  1603. filename += filename_len - remaining_space;
  1604. filename_len = remaining_space;
  1605. }
  1606. to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
  1607. pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
  1608. memmove(pos + filename_len, pos, to_move);
  1609. memcpy(pos, filename, filename_len);
  1610. ttrace->filename.ptr = 0;
  1611. ttrace->filename.entry_str_pos = 0;
  1612. out_put:
  1613. thread__put(thread);
  1614. out:
  1615. return 0;
  1616. }
  1617. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  1618. union perf_event *event __maybe_unused,
  1619. struct perf_sample *sample)
  1620. {
  1621. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  1622. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  1623. struct thread *thread = machine__findnew_thread(trace->host,
  1624. sample->pid,
  1625. sample->tid);
  1626. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  1627. if (ttrace == NULL)
  1628. goto out_dump;
  1629. ttrace->runtime_ms += runtime_ms;
  1630. trace->runtime_ms += runtime_ms;
  1631. out_put:
  1632. thread__put(thread);
  1633. return 0;
  1634. out_dump:
  1635. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  1636. evsel->name,
  1637. perf_evsel__strval(evsel, sample, "comm"),
  1638. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  1639. runtime,
  1640. perf_evsel__intval(evsel, sample, "vruntime"));
  1641. goto out_put;
  1642. }
  1643. static int bpf_output__printer(enum binary_printer_ops op,
  1644. unsigned int val, void *extra __maybe_unused, FILE *fp)
  1645. {
  1646. unsigned char ch = (unsigned char)val;
  1647. switch (op) {
  1648. case BINARY_PRINT_CHAR_DATA:
  1649. return fprintf(fp, "%c", isprint(ch) ? ch : '.');
  1650. case BINARY_PRINT_DATA_BEGIN:
  1651. case BINARY_PRINT_LINE_BEGIN:
  1652. case BINARY_PRINT_ADDR:
  1653. case BINARY_PRINT_NUM_DATA:
  1654. case BINARY_PRINT_NUM_PAD:
  1655. case BINARY_PRINT_SEP:
  1656. case BINARY_PRINT_CHAR_PAD:
  1657. case BINARY_PRINT_LINE_END:
  1658. case BINARY_PRINT_DATA_END:
  1659. default:
  1660. break;
  1661. }
  1662. return 0;
  1663. }
  1664. static void bpf_output__fprintf(struct trace *trace,
  1665. struct perf_sample *sample)
  1666. {
  1667. binary__fprintf(sample->raw_data, sample->raw_size, 8,
  1668. bpf_output__printer, NULL, trace->output);
  1669. }
  1670. static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
  1671. union perf_event *event __maybe_unused,
  1672. struct perf_sample *sample)
  1673. {
  1674. int callchain_ret = 0;
  1675. if (sample->callchain) {
  1676. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1677. if (callchain_ret == 0) {
  1678. if (callchain_cursor.nr < trace->min_stack)
  1679. goto out;
  1680. callchain_ret = 1;
  1681. }
  1682. }
  1683. trace__printf_interrupted_entry(trace);
  1684. trace__fprintf_tstamp(trace, sample->time, trace->output);
  1685. if (trace->trace_syscalls)
  1686. fprintf(trace->output, "( ): ");
  1687. fprintf(trace->output, "%s:", evsel->name);
  1688. if (perf_evsel__is_bpf_output(evsel)) {
  1689. bpf_output__fprintf(trace, sample);
  1690. } else if (evsel->tp_format) {
  1691. if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
  1692. trace__fprintf_sys_enter(trace, evsel, sample)) {
  1693. event_format__fprintf(evsel->tp_format, sample->cpu,
  1694. sample->raw_data, sample->raw_size,
  1695. trace->output);
  1696. }
  1697. }
  1698. fprintf(trace->output, "\n");
  1699. if (callchain_ret > 0)
  1700. trace__fprintf_callchain(trace, sample);
  1701. else if (callchain_ret < 0)
  1702. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1703. out:
  1704. return 0;
  1705. }
  1706. static void print_location(FILE *f, struct perf_sample *sample,
  1707. struct addr_location *al,
  1708. bool print_dso, bool print_sym)
  1709. {
  1710. if ((verbose > 0 || print_dso) && al->map)
  1711. fprintf(f, "%s@", al->map->dso->long_name);
  1712. if ((verbose > 0 || print_sym) && al->sym)
  1713. fprintf(f, "%s+0x%" PRIx64, al->sym->name,
  1714. al->addr - al->sym->start);
  1715. else if (al->map)
  1716. fprintf(f, "0x%" PRIx64, al->addr);
  1717. else
  1718. fprintf(f, "0x%" PRIx64, sample->addr);
  1719. }
  1720. static int trace__pgfault(struct trace *trace,
  1721. struct perf_evsel *evsel,
  1722. union perf_event *event __maybe_unused,
  1723. struct perf_sample *sample)
  1724. {
  1725. struct thread *thread;
  1726. struct addr_location al;
  1727. char map_type = 'd';
  1728. struct thread_trace *ttrace;
  1729. int err = -1;
  1730. int callchain_ret = 0;
  1731. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1732. if (sample->callchain) {
  1733. callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
  1734. if (callchain_ret == 0) {
  1735. if (callchain_cursor.nr < trace->min_stack)
  1736. goto out_put;
  1737. callchain_ret = 1;
  1738. }
  1739. }
  1740. ttrace = thread__trace(thread, trace->output);
  1741. if (ttrace == NULL)
  1742. goto out_put;
  1743. if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
  1744. ttrace->pfmaj++;
  1745. else
  1746. ttrace->pfmin++;
  1747. if (trace->summary_only)
  1748. goto out;
  1749. thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
  1750. trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
  1751. fprintf(trace->output, "%sfault [",
  1752. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
  1753. "maj" : "min");
  1754. print_location(trace->output, sample, &al, false, true);
  1755. fprintf(trace->output, "] => ");
  1756. thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
  1757. if (!al.map) {
  1758. thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
  1759. if (al.map)
  1760. map_type = 'x';
  1761. else
  1762. map_type = '?';
  1763. }
  1764. print_location(trace->output, sample, &al, true, false);
  1765. fprintf(trace->output, " (%c%c)\n", map_type, al.level);
  1766. if (callchain_ret > 0)
  1767. trace__fprintf_callchain(trace, sample);
  1768. else if (callchain_ret < 0)
  1769. pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
  1770. out:
  1771. err = 0;
  1772. out_put:
  1773. thread__put(thread);
  1774. return err;
  1775. }
  1776. static void trace__set_base_time(struct trace *trace,
  1777. struct perf_evsel *evsel,
  1778. struct perf_sample *sample)
  1779. {
  1780. /*
  1781. * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
  1782. * and don't use sample->time unconditionally, we may end up having
  1783. * some other event in the future without PERF_SAMPLE_TIME for good
  1784. * reason, i.e. we may not be interested in its timestamps, just in
  1785. * it taking place, picking some piece of information when it
  1786. * appears in our event stream (vfs_getname comes to mind).
  1787. */
  1788. if (trace->base_time == 0 && !trace->full_time &&
  1789. (evsel->attr.sample_type & PERF_SAMPLE_TIME))
  1790. trace->base_time = sample->time;
  1791. }
  1792. static int trace__process_sample(struct perf_tool *tool,
  1793. union perf_event *event,
  1794. struct perf_sample *sample,
  1795. struct perf_evsel *evsel,
  1796. struct machine *machine __maybe_unused)
  1797. {
  1798. struct trace *trace = container_of(tool, struct trace, tool);
  1799. struct thread *thread;
  1800. int err = 0;
  1801. tracepoint_handler handler = evsel->handler;
  1802. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  1803. if (thread && thread__is_filtered(thread))
  1804. goto out;
  1805. trace__set_base_time(trace, evsel, sample);
  1806. if (handler) {
  1807. ++trace->nr_events;
  1808. handler(trace, evsel, event, sample);
  1809. }
  1810. out:
  1811. thread__put(thread);
  1812. return err;
  1813. }
  1814. static int trace__record(struct trace *trace, int argc, const char **argv)
  1815. {
  1816. unsigned int rec_argc, i, j;
  1817. const char **rec_argv;
  1818. const char * const record_args[] = {
  1819. "record",
  1820. "-R",
  1821. "-m", "1024",
  1822. "-c", "1",
  1823. };
  1824. const char * const sc_args[] = { "-e", };
  1825. unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
  1826. const char * const majpf_args[] = { "-e", "major-faults" };
  1827. unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
  1828. const char * const minpf_args[] = { "-e", "minor-faults" };
  1829. unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
  1830. /* +1 is for the event string below */
  1831. rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
  1832. majpf_args_nr + minpf_args_nr + argc;
  1833. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  1834. if (rec_argv == NULL)
  1835. return -ENOMEM;
  1836. j = 0;
  1837. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  1838. rec_argv[j++] = record_args[i];
  1839. if (trace->trace_syscalls) {
  1840. for (i = 0; i < sc_args_nr; i++)
  1841. rec_argv[j++] = sc_args[i];
  1842. /* event string may be different for older kernels - e.g., RHEL6 */
  1843. if (is_valid_tracepoint("raw_syscalls:sys_enter"))
  1844. rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
  1845. else if (is_valid_tracepoint("syscalls:sys_enter"))
  1846. rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
  1847. else {
  1848. pr_err("Neither raw_syscalls nor syscalls events exist.\n");
  1849. free(rec_argv);
  1850. return -1;
  1851. }
  1852. }
  1853. if (trace->trace_pgfaults & TRACE_PFMAJ)
  1854. for (i = 0; i < majpf_args_nr; i++)
  1855. rec_argv[j++] = majpf_args[i];
  1856. if (trace->trace_pgfaults & TRACE_PFMIN)
  1857. for (i = 0; i < minpf_args_nr; i++)
  1858. rec_argv[j++] = minpf_args[i];
  1859. for (i = 0; i < (unsigned int)argc; i++)
  1860. rec_argv[j++] = argv[i];
  1861. return cmd_record(j, rec_argv);
  1862. }
  1863. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
  1864. static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
  1865. {
  1866. struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
  1867. if (IS_ERR(evsel))
  1868. return false;
  1869. if (perf_evsel__field(evsel, "pathname") == NULL) {
  1870. perf_evsel__delete(evsel);
  1871. return false;
  1872. }
  1873. evsel->handler = trace__vfs_getname;
  1874. perf_evlist__add(evlist, evsel);
  1875. return true;
  1876. }
  1877. static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
  1878. {
  1879. struct perf_evsel *evsel;
  1880. struct perf_event_attr attr = {
  1881. .type = PERF_TYPE_SOFTWARE,
  1882. .mmap_data = 1,
  1883. };
  1884. attr.config = config;
  1885. attr.sample_period = 1;
  1886. event_attr_init(&attr);
  1887. evsel = perf_evsel__new(&attr);
  1888. if (evsel)
  1889. evsel->handler = trace__pgfault;
  1890. return evsel;
  1891. }
  1892. static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
  1893. {
  1894. const u32 type = event->header.type;
  1895. struct perf_evsel *evsel;
  1896. if (type != PERF_RECORD_SAMPLE) {
  1897. trace__process_event(trace, trace->host, event, sample);
  1898. return;
  1899. }
  1900. evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
  1901. if (evsel == NULL) {
  1902. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
  1903. return;
  1904. }
  1905. trace__set_base_time(trace, evsel, sample);
  1906. if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
  1907. sample->raw_data == NULL) {
  1908. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  1909. perf_evsel__name(evsel), sample->tid,
  1910. sample->cpu, sample->raw_size);
  1911. } else {
  1912. tracepoint_handler handler = evsel->handler;
  1913. handler(trace, evsel, event, sample);
  1914. }
  1915. }
  1916. static int trace__add_syscall_newtp(struct trace *trace)
  1917. {
  1918. int ret = -1;
  1919. struct perf_evlist *evlist = trace->evlist;
  1920. struct perf_evsel *sys_enter, *sys_exit;
  1921. sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
  1922. if (sys_enter == NULL)
  1923. goto out;
  1924. if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
  1925. goto out_delete_sys_enter;
  1926. sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
  1927. if (sys_exit == NULL)
  1928. goto out_delete_sys_enter;
  1929. if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
  1930. goto out_delete_sys_exit;
  1931. perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
  1932. perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
  1933. perf_evlist__add(evlist, sys_enter);
  1934. perf_evlist__add(evlist, sys_exit);
  1935. if (callchain_param.enabled && !trace->kernel_syscallchains) {
  1936. /*
  1937. * We're interested only in the user space callchain
  1938. * leading to the syscall, allow overriding that for
  1939. * debugging reasons using --kernel_syscall_callchains
  1940. */
  1941. sys_exit->attr.exclude_callchain_kernel = 1;
  1942. }
  1943. trace->syscalls.events.sys_enter = sys_enter;
  1944. trace->syscalls.events.sys_exit = sys_exit;
  1945. ret = 0;
  1946. out:
  1947. return ret;
  1948. out_delete_sys_exit:
  1949. perf_evsel__delete_priv(sys_exit);
  1950. out_delete_sys_enter:
  1951. perf_evsel__delete_priv(sys_enter);
  1952. goto out;
  1953. }
  1954. static int trace__set_ev_qualifier_filter(struct trace *trace)
  1955. {
  1956. int err = -1;
  1957. struct perf_evsel *sys_exit;
  1958. char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
  1959. trace->ev_qualifier_ids.nr,
  1960. trace->ev_qualifier_ids.entries);
  1961. if (filter == NULL)
  1962. goto out_enomem;
  1963. if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
  1964. filter)) {
  1965. sys_exit = trace->syscalls.events.sys_exit;
  1966. err = perf_evsel__append_tp_filter(sys_exit, filter);
  1967. }
  1968. free(filter);
  1969. out:
  1970. return err;
  1971. out_enomem:
  1972. errno = ENOMEM;
  1973. goto out;
  1974. }
  1975. static int trace__set_filter_loop_pids(struct trace *trace)
  1976. {
  1977. unsigned int nr = 1;
  1978. pid_t pids[32] = {
  1979. getpid(),
  1980. };
  1981. struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
  1982. while (thread && nr < ARRAY_SIZE(pids)) {
  1983. struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
  1984. if (parent == NULL)
  1985. break;
  1986. if (!strcmp(thread__comm_str(parent), "sshd")) {
  1987. pids[nr++] = parent->tid;
  1988. break;
  1989. }
  1990. thread = parent;
  1991. }
  1992. return perf_evlist__set_filter_pids(trace->evlist, nr, pids);
  1993. }
  1994. static int trace__run(struct trace *trace, int argc, const char **argv)
  1995. {
  1996. struct perf_evlist *evlist = trace->evlist;
  1997. struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
  1998. int err = -1, i;
  1999. unsigned long before;
  2000. const bool forks = argc > 0;
  2001. bool draining = false;
  2002. trace->live = true;
  2003. if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
  2004. goto out_error_raw_syscalls;
  2005. if (trace->trace_syscalls)
  2006. trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
  2007. if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
  2008. pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
  2009. if (pgfault_maj == NULL)
  2010. goto out_error_mem;
  2011. perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
  2012. perf_evlist__add(evlist, pgfault_maj);
  2013. }
  2014. if ((trace->trace_pgfaults & TRACE_PFMIN)) {
  2015. pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
  2016. if (pgfault_min == NULL)
  2017. goto out_error_mem;
  2018. perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
  2019. perf_evlist__add(evlist, pgfault_min);
  2020. }
  2021. if (trace->sched &&
  2022. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  2023. trace__sched_stat_runtime))
  2024. goto out_error_sched_stat_runtime;
  2025. /*
  2026. * If a global cgroup was set, apply it to all the events without an
  2027. * explicit cgroup. I.e.:
  2028. *
  2029. * trace -G A -e sched:*switch
  2030. *
  2031. * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
  2032. * _and_ sched:sched_switch to the 'A' cgroup, while:
  2033. *
  2034. * trace -e sched:*switch -G A
  2035. *
  2036. * will only set the sched:sched_switch event to the 'A' cgroup, all the
  2037. * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
  2038. * a cgroup (on the root cgroup, sys wide, etc).
  2039. *
  2040. * Multiple cgroups:
  2041. *
  2042. * trace -G A -e sched:*switch -G B
  2043. *
  2044. * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
  2045. * to the 'B' cgroup.
  2046. *
  2047. * evlist__set_default_cgroup() grabs a reference of the passed cgroup
  2048. * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
  2049. */
  2050. if (trace->cgroup)
  2051. evlist__set_default_cgroup(trace->evlist, trace->cgroup);
  2052. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  2053. if (err < 0) {
  2054. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  2055. goto out_delete_evlist;
  2056. }
  2057. err = trace__symbols_init(trace, evlist);
  2058. if (err < 0) {
  2059. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  2060. goto out_delete_evlist;
  2061. }
  2062. perf_evlist__config(evlist, &trace->opts, &callchain_param);
  2063. signal(SIGCHLD, sig_handler);
  2064. signal(SIGINT, sig_handler);
  2065. if (forks) {
  2066. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  2067. argv, false, NULL);
  2068. if (err < 0) {
  2069. fprintf(trace->output, "Couldn't run the workload!\n");
  2070. goto out_delete_evlist;
  2071. }
  2072. }
  2073. err = perf_evlist__open(evlist);
  2074. if (err < 0)
  2075. goto out_error_open;
  2076. err = bpf__apply_obj_config();
  2077. if (err) {
  2078. char errbuf[BUFSIZ];
  2079. bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
  2080. pr_err("ERROR: Apply config to BPF failed: %s\n",
  2081. errbuf);
  2082. goto out_error_open;
  2083. }
  2084. /*
  2085. * Better not use !target__has_task() here because we need to cover the
  2086. * case where no threads were specified in the command line, but a
  2087. * workload was, and in that case we will fill in the thread_map when
  2088. * we fork the workload in perf_evlist__prepare_workload.
  2089. */
  2090. if (trace->filter_pids.nr > 0)
  2091. err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
  2092. else if (thread_map__pid(evlist->threads, 0) == -1)
  2093. err = trace__set_filter_loop_pids(trace);
  2094. if (err < 0)
  2095. goto out_error_mem;
  2096. if (trace->ev_qualifier_ids.nr > 0) {
  2097. err = trace__set_ev_qualifier_filter(trace);
  2098. if (err < 0)
  2099. goto out_errno;
  2100. pr_debug("event qualifier tracepoint filter: %s\n",
  2101. trace->syscalls.events.sys_exit->filter);
  2102. }
  2103. err = perf_evlist__apply_filters(evlist, &evsel);
  2104. if (err < 0)
  2105. goto out_error_apply_filters;
  2106. err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
  2107. if (err < 0)
  2108. goto out_error_mmap;
  2109. if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
  2110. perf_evlist__enable(evlist);
  2111. if (forks)
  2112. perf_evlist__start_workload(evlist);
  2113. if (trace->opts.initial_delay) {
  2114. usleep(trace->opts.initial_delay * 1000);
  2115. perf_evlist__enable(evlist);
  2116. }
  2117. trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
  2118. evlist->threads->nr > 1 ||
  2119. perf_evlist__first(evlist)->attr.inherit;
  2120. /*
  2121. * Now that we already used evsel->attr to ask the kernel to setup the
  2122. * events, lets reuse evsel->attr.sample_max_stack as the limit in
  2123. * trace__resolve_callchain(), allowing per-event max-stack settings
  2124. * to override an explicitely set --max-stack global setting.
  2125. */
  2126. evlist__for_each_entry(evlist, evsel) {
  2127. if (evsel__has_callchain(evsel) &&
  2128. evsel->attr.sample_max_stack == 0)
  2129. evsel->attr.sample_max_stack = trace->max_stack;
  2130. }
  2131. again:
  2132. before = trace->nr_events;
  2133. for (i = 0; i < evlist->nr_mmaps; i++) {
  2134. union perf_event *event;
  2135. struct perf_mmap *md;
  2136. md = &evlist->mmap[i];
  2137. if (perf_mmap__read_init(md) < 0)
  2138. continue;
  2139. while ((event = perf_mmap__read_event(md)) != NULL) {
  2140. struct perf_sample sample;
  2141. ++trace->nr_events;
  2142. err = perf_evlist__parse_sample(evlist, event, &sample);
  2143. if (err) {
  2144. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  2145. goto next_event;
  2146. }
  2147. trace__handle_event(trace, event, &sample);
  2148. next_event:
  2149. perf_mmap__consume(md);
  2150. if (interrupted)
  2151. goto out_disable;
  2152. if (done && !draining) {
  2153. perf_evlist__disable(evlist);
  2154. draining = true;
  2155. }
  2156. }
  2157. perf_mmap__read_done(md);
  2158. }
  2159. if (trace->nr_events == before) {
  2160. int timeout = done ? 100 : -1;
  2161. if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
  2162. if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0)
  2163. draining = true;
  2164. goto again;
  2165. }
  2166. } else {
  2167. goto again;
  2168. }
  2169. out_disable:
  2170. thread__zput(trace->current);
  2171. perf_evlist__disable(evlist);
  2172. if (!err) {
  2173. if (trace->summary)
  2174. trace__fprintf_thread_summary(trace, trace->output);
  2175. if (trace->show_tool_stats) {
  2176. fprintf(trace->output, "Stats:\n "
  2177. " vfs_getname : %" PRIu64 "\n"
  2178. " proc_getname: %" PRIu64 "\n",
  2179. trace->stats.vfs_getname,
  2180. trace->stats.proc_getname);
  2181. }
  2182. }
  2183. out_delete_evlist:
  2184. trace__symbols__exit(trace);
  2185. perf_evlist__delete(evlist);
  2186. cgroup__put(trace->cgroup);
  2187. trace->evlist = NULL;
  2188. trace->live = false;
  2189. return err;
  2190. {
  2191. char errbuf[BUFSIZ];
  2192. out_error_sched_stat_runtime:
  2193. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
  2194. goto out_error;
  2195. out_error_raw_syscalls:
  2196. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
  2197. goto out_error;
  2198. out_error_mmap:
  2199. perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
  2200. goto out_error;
  2201. out_error_open:
  2202. perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  2203. out_error:
  2204. fprintf(trace->output, "%s\n", errbuf);
  2205. goto out_delete_evlist;
  2206. out_error_apply_filters:
  2207. fprintf(trace->output,
  2208. "Failed to set filter \"%s\" on event %s with %d (%s)\n",
  2209. evsel->filter, perf_evsel__name(evsel), errno,
  2210. str_error_r(errno, errbuf, sizeof(errbuf)));
  2211. goto out_delete_evlist;
  2212. }
  2213. out_error_mem:
  2214. fprintf(trace->output, "Not enough memory to run!\n");
  2215. goto out_delete_evlist;
  2216. out_errno:
  2217. fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
  2218. goto out_delete_evlist;
  2219. }
  2220. static int trace__replay(struct trace *trace)
  2221. {
  2222. const struct perf_evsel_str_handler handlers[] = {
  2223. { "probe:vfs_getname", trace__vfs_getname, },
  2224. };
  2225. struct perf_data data = {
  2226. .file = {
  2227. .path = input_name,
  2228. },
  2229. .mode = PERF_DATA_MODE_READ,
  2230. .force = trace->force,
  2231. };
  2232. struct perf_session *session;
  2233. struct perf_evsel *evsel;
  2234. int err = -1;
  2235. trace->tool.sample = trace__process_sample;
  2236. trace->tool.mmap = perf_event__process_mmap;
  2237. trace->tool.mmap2 = perf_event__process_mmap2;
  2238. trace->tool.comm = perf_event__process_comm;
  2239. trace->tool.exit = perf_event__process_exit;
  2240. trace->tool.fork = perf_event__process_fork;
  2241. trace->tool.attr = perf_event__process_attr;
  2242. trace->tool.tracing_data = perf_event__process_tracing_data;
  2243. trace->tool.build_id = perf_event__process_build_id;
  2244. trace->tool.namespaces = perf_event__process_namespaces;
  2245. trace->tool.ordered_events = true;
  2246. trace->tool.ordering_requires_timestamps = true;
  2247. /* add tid to output */
  2248. trace->multiple_threads = true;
  2249. session = perf_session__new(&data, false, &trace->tool);
  2250. if (session == NULL)
  2251. return -1;
  2252. if (trace->opts.target.pid)
  2253. symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
  2254. if (trace->opts.target.tid)
  2255. symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
  2256. if (symbol__init(&session->header.env) < 0)
  2257. goto out;
  2258. trace->host = &session->machines.host;
  2259. err = perf_session__set_tracepoints_handlers(session, handlers);
  2260. if (err)
  2261. goto out;
  2262. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2263. "raw_syscalls:sys_enter");
  2264. /* older kernels have syscalls tp versus raw_syscalls */
  2265. if (evsel == NULL)
  2266. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2267. "syscalls:sys_enter");
  2268. if (evsel &&
  2269. (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
  2270. perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
  2271. pr_err("Error during initialize raw_syscalls:sys_enter event\n");
  2272. goto out;
  2273. }
  2274. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2275. "raw_syscalls:sys_exit");
  2276. if (evsel == NULL)
  2277. evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
  2278. "syscalls:sys_exit");
  2279. if (evsel &&
  2280. (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
  2281. perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
  2282. pr_err("Error during initialize raw_syscalls:sys_exit event\n");
  2283. goto out;
  2284. }
  2285. evlist__for_each_entry(session->evlist, evsel) {
  2286. if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
  2287. (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
  2288. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  2289. evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
  2290. evsel->handler = trace__pgfault;
  2291. }
  2292. setup_pager();
  2293. err = perf_session__process_events(session);
  2294. if (err)
  2295. pr_err("Failed to process events, error %d", err);
  2296. else if (trace->summary)
  2297. trace__fprintf_thread_summary(trace, trace->output);
  2298. out:
  2299. perf_session__delete(session);
  2300. return err;
  2301. }
  2302. static size_t trace__fprintf_threads_header(FILE *fp)
  2303. {
  2304. size_t printed;
  2305. printed = fprintf(fp, "\n Summary of events:\n\n");
  2306. return printed;
  2307. }
  2308. DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
  2309. struct stats *stats;
  2310. double msecs;
  2311. int syscall;
  2312. )
  2313. {
  2314. struct int_node *source = rb_entry(nd, struct int_node, rb_node);
  2315. struct stats *stats = source->priv;
  2316. entry->syscall = source->i;
  2317. entry->stats = stats;
  2318. entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
  2319. }
  2320. static size_t thread__dump_stats(struct thread_trace *ttrace,
  2321. struct trace *trace, FILE *fp)
  2322. {
  2323. size_t printed = 0;
  2324. struct syscall *sc;
  2325. struct rb_node *nd;
  2326. DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
  2327. if (syscall_stats == NULL)
  2328. return 0;
  2329. printed += fprintf(fp, "\n");
  2330. printed += fprintf(fp, " syscall calls total min avg max stddev\n");
  2331. printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
  2332. printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
  2333. resort_rb__for_each_entry(nd, syscall_stats) {
  2334. struct stats *stats = syscall_stats_entry->stats;
  2335. if (stats) {
  2336. double min = (double)(stats->min) / NSEC_PER_MSEC;
  2337. double max = (double)(stats->max) / NSEC_PER_MSEC;
  2338. double avg = avg_stats(stats);
  2339. double pct;
  2340. u64 n = (u64) stats->n;
  2341. pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
  2342. avg /= NSEC_PER_MSEC;
  2343. sc = &trace->syscalls.table[syscall_stats_entry->syscall];
  2344. printed += fprintf(fp, " %-15s", sc->name);
  2345. printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
  2346. n, syscall_stats_entry->msecs, min, avg);
  2347. printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
  2348. }
  2349. }
  2350. resort_rb__delete(syscall_stats);
  2351. printed += fprintf(fp, "\n\n");
  2352. return printed;
  2353. }
  2354. static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
  2355. {
  2356. size_t printed = 0;
  2357. struct thread_trace *ttrace = thread__priv(thread);
  2358. double ratio;
  2359. if (ttrace == NULL)
  2360. return 0;
  2361. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  2362. printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
  2363. printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
  2364. printed += fprintf(fp, "%.1f%%", ratio);
  2365. if (ttrace->pfmaj)
  2366. printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
  2367. if (ttrace->pfmin)
  2368. printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
  2369. if (trace->sched)
  2370. printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
  2371. else if (fputc('\n', fp) != EOF)
  2372. ++printed;
  2373. printed += thread__dump_stats(ttrace, trace, fp);
  2374. return printed;
  2375. }
  2376. static unsigned long thread__nr_events(struct thread_trace *ttrace)
  2377. {
  2378. return ttrace ? ttrace->nr_events : 0;
  2379. }
  2380. DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
  2381. struct thread *thread;
  2382. )
  2383. {
  2384. entry->thread = rb_entry(nd, struct thread, rb_node);
  2385. }
  2386. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  2387. {
  2388. size_t printed = trace__fprintf_threads_header(fp);
  2389. struct rb_node *nd;
  2390. int i;
  2391. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  2392. DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
  2393. if (threads == NULL) {
  2394. fprintf(fp, "%s", "Error sorting output by nr_events!\n");
  2395. return 0;
  2396. }
  2397. resort_rb__for_each_entry(nd, threads)
  2398. printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
  2399. resort_rb__delete(threads);
  2400. }
  2401. return printed;
  2402. }
  2403. static int trace__set_duration(const struct option *opt, const char *str,
  2404. int unset __maybe_unused)
  2405. {
  2406. struct trace *trace = opt->value;
  2407. trace->duration_filter = atof(str);
  2408. return 0;
  2409. }
  2410. static int trace__set_filter_pids(const struct option *opt, const char *str,
  2411. int unset __maybe_unused)
  2412. {
  2413. int ret = -1;
  2414. size_t i;
  2415. struct trace *trace = opt->value;
  2416. /*
  2417. * FIXME: introduce a intarray class, plain parse csv and create a
  2418. * { int nr, int entries[] } struct...
  2419. */
  2420. struct intlist *list = intlist__new(str);
  2421. if (list == NULL)
  2422. return -1;
  2423. i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
  2424. trace->filter_pids.entries = calloc(i, sizeof(pid_t));
  2425. if (trace->filter_pids.entries == NULL)
  2426. goto out;
  2427. trace->filter_pids.entries[0] = getpid();
  2428. for (i = 1; i < trace->filter_pids.nr; ++i)
  2429. trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
  2430. intlist__delete(list);
  2431. ret = 0;
  2432. out:
  2433. return ret;
  2434. }
  2435. static int trace__open_output(struct trace *trace, const char *filename)
  2436. {
  2437. struct stat st;
  2438. if (!stat(filename, &st) && st.st_size) {
  2439. char oldname[PATH_MAX];
  2440. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  2441. unlink(oldname);
  2442. rename(filename, oldname);
  2443. }
  2444. trace->output = fopen(filename, "w");
  2445. return trace->output == NULL ? -errno : 0;
  2446. }
  2447. static int parse_pagefaults(const struct option *opt, const char *str,
  2448. int unset __maybe_unused)
  2449. {
  2450. int *trace_pgfaults = opt->value;
  2451. if (strcmp(str, "all") == 0)
  2452. *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
  2453. else if (strcmp(str, "maj") == 0)
  2454. *trace_pgfaults |= TRACE_PFMAJ;
  2455. else if (strcmp(str, "min") == 0)
  2456. *trace_pgfaults |= TRACE_PFMIN;
  2457. else
  2458. return -1;
  2459. return 0;
  2460. }
  2461. static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
  2462. {
  2463. struct perf_evsel *evsel;
  2464. evlist__for_each_entry(evlist, evsel)
  2465. evsel->handler = handler;
  2466. }
  2467. /*
  2468. * XXX: Hackish, just splitting the combined -e+--event (syscalls
  2469. * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
  2470. * existing facilities unchanged (trace->ev_qualifier + parse_options()).
  2471. *
  2472. * It'd be better to introduce a parse_options() variant that would return a
  2473. * list with the terms it didn't match to an event...
  2474. */
  2475. static int trace__parse_events_option(const struct option *opt, const char *str,
  2476. int unset __maybe_unused)
  2477. {
  2478. struct trace *trace = (struct trace *)opt->value;
  2479. const char *s = str;
  2480. char *sep = NULL, *lists[2] = { NULL, NULL, };
  2481. int len = strlen(str) + 1, err = -1, list, idx;
  2482. char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
  2483. char group_name[PATH_MAX];
  2484. if (strace_groups_dir == NULL)
  2485. return -1;
  2486. if (*s == '!') {
  2487. ++s;
  2488. trace->not_ev_qualifier = true;
  2489. }
  2490. while (1) {
  2491. if ((sep = strchr(s, ',')) != NULL)
  2492. *sep = '\0';
  2493. list = 0;
  2494. if (syscalltbl__id(trace->sctbl, s) >= 0 ||
  2495. syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
  2496. list = 1;
  2497. } else {
  2498. path__join(group_name, sizeof(group_name), strace_groups_dir, s);
  2499. if (access(group_name, R_OK) == 0)
  2500. list = 1;
  2501. }
  2502. if (lists[list]) {
  2503. sprintf(lists[list] + strlen(lists[list]), ",%s", s);
  2504. } else {
  2505. lists[list] = malloc(len);
  2506. if (lists[list] == NULL)
  2507. goto out;
  2508. strcpy(lists[list], s);
  2509. }
  2510. if (!sep)
  2511. break;
  2512. *sep = ',';
  2513. s = sep + 1;
  2514. }
  2515. if (lists[1] != NULL) {
  2516. struct strlist_config slist_config = {
  2517. .dirname = strace_groups_dir,
  2518. };
  2519. trace->ev_qualifier = strlist__new(lists[1], &slist_config);
  2520. if (trace->ev_qualifier == NULL) {
  2521. fputs("Not enough memory to parse event qualifier", trace->output);
  2522. goto out;
  2523. }
  2524. if (trace__validate_ev_qualifier(trace))
  2525. goto out;
  2526. trace->trace_syscalls = true;
  2527. }
  2528. err = 0;
  2529. if (lists[0]) {
  2530. struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
  2531. "event selector. use 'perf list' to list available events",
  2532. parse_events_option);
  2533. err = parse_events_option(&o, lists[0], 0);
  2534. }
  2535. out:
  2536. if (sep)
  2537. *sep = ',';
  2538. return err;
  2539. }
  2540. static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
  2541. {
  2542. struct trace *trace = opt->value;
  2543. if (!list_empty(&trace->evlist->entries))
  2544. return parse_cgroups(opt, str, unset);
  2545. trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
  2546. return 0;
  2547. }
  2548. int cmd_trace(int argc, const char **argv)
  2549. {
  2550. const char *trace_usage[] = {
  2551. "perf trace [<options>] [<command>]",
  2552. "perf trace [<options>] -- <command> [<options>]",
  2553. "perf trace record [<options>] [<command>]",
  2554. "perf trace record [<options>] -- <command> [<options>]",
  2555. NULL
  2556. };
  2557. struct trace trace = {
  2558. .syscalls = {
  2559. . max = -1,
  2560. },
  2561. .opts = {
  2562. .target = {
  2563. .uid = UINT_MAX,
  2564. .uses_mmap = true,
  2565. },
  2566. .user_freq = UINT_MAX,
  2567. .user_interval = ULLONG_MAX,
  2568. .no_buffering = true,
  2569. .mmap_pages = UINT_MAX,
  2570. .proc_map_timeout = 500,
  2571. },
  2572. .output = stderr,
  2573. .show_comm = true,
  2574. .trace_syscalls = false,
  2575. .kernel_syscallchains = false,
  2576. .max_stack = UINT_MAX,
  2577. };
  2578. const char *output_name = NULL;
  2579. const struct option trace_options[] = {
  2580. OPT_CALLBACK('e', "event", &trace, "event",
  2581. "event/syscall selector. use 'perf list' to list available events",
  2582. trace__parse_events_option),
  2583. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  2584. "show the thread COMM next to its id"),
  2585. OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
  2586. OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
  2587. trace__parse_events_option),
  2588. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  2589. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  2590. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  2591. "trace events on existing process id"),
  2592. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  2593. "trace events on existing thread id"),
  2594. OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
  2595. "pids to filter (by the kernel)", trace__set_filter_pids),
  2596. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  2597. "system-wide collection from all CPUs"),
  2598. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  2599. "list of cpus to monitor"),
  2600. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  2601. "child tasks do not inherit counters"),
  2602. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  2603. "number of mmap data pages",
  2604. perf_evlist__parse_mmap_pages),
  2605. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  2606. "user to profile"),
  2607. OPT_CALLBACK(0, "duration", &trace, "float",
  2608. "show only events with duration > N.M ms",
  2609. trace__set_duration),
  2610. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  2611. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  2612. OPT_BOOLEAN('T', "time", &trace.full_time,
  2613. "Show full timestamp, not time relative to first start"),
  2614. OPT_BOOLEAN(0, "failure", &trace.failure_only,
  2615. "Show only syscalls that failed"),
  2616. OPT_BOOLEAN('s', "summary", &trace.summary_only,
  2617. "Show only syscall summary with statistics"),
  2618. OPT_BOOLEAN('S', "with-summary", &trace.summary,
  2619. "Show all syscalls and summary with statistics"),
  2620. OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
  2621. "Trace pagefaults", parse_pagefaults, "maj"),
  2622. OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
  2623. OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
  2624. OPT_CALLBACK(0, "call-graph", &trace.opts,
  2625. "record_mode[,record_size]", record_callchain_help,
  2626. &record_parse_callchain_opt),
  2627. OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
  2628. "Show the kernel callchains on the syscall exit path"),
  2629. OPT_UINTEGER(0, "min-stack", &trace.min_stack,
  2630. "Set the minimum stack depth when parsing the callchain, "
  2631. "anything below the specified depth will be ignored."),
  2632. OPT_UINTEGER(0, "max-stack", &trace.max_stack,
  2633. "Set the maximum stack depth when parsing the callchain, "
  2634. "anything beyond the specified depth will be ignored. "
  2635. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  2636. OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
  2637. "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
  2638. OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
  2639. "per thread proc mmap processing timeout in ms"),
  2640. OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
  2641. trace__parse_cgroups),
  2642. OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
  2643. "ms to wait before starting measurement after program "
  2644. "start"),
  2645. OPT_END()
  2646. };
  2647. bool __maybe_unused max_stack_user_set = true;
  2648. bool mmap_pages_user_set = true;
  2649. const char * const trace_subcommands[] = { "record", NULL };
  2650. int err;
  2651. char bf[BUFSIZ];
  2652. signal(SIGSEGV, sighandler_dump_stack);
  2653. signal(SIGFPE, sighandler_dump_stack);
  2654. trace.evlist = perf_evlist__new();
  2655. trace.sctbl = syscalltbl__new();
  2656. if (trace.evlist == NULL || trace.sctbl == NULL) {
  2657. pr_err("Not enough memory to run!\n");
  2658. err = -ENOMEM;
  2659. goto out;
  2660. }
  2661. argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
  2662. trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  2663. if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
  2664. usage_with_options_msg(trace_usage, trace_options,
  2665. "cgroup monitoring only available in system-wide mode");
  2666. }
  2667. err = bpf__setup_stdout(trace.evlist);
  2668. if (err) {
  2669. bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
  2670. pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
  2671. goto out;
  2672. }
  2673. err = -1;
  2674. if (trace.trace_pgfaults) {
  2675. trace.opts.sample_address = true;
  2676. trace.opts.sample_time = true;
  2677. }
  2678. if (trace.opts.mmap_pages == UINT_MAX)
  2679. mmap_pages_user_set = false;
  2680. if (trace.max_stack == UINT_MAX) {
  2681. trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
  2682. max_stack_user_set = false;
  2683. }
  2684. #ifdef HAVE_DWARF_UNWIND_SUPPORT
  2685. if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
  2686. record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
  2687. }
  2688. #endif
  2689. if (callchain_param.enabled) {
  2690. if (!mmap_pages_user_set && geteuid() == 0)
  2691. trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
  2692. symbol_conf.use_callchain = true;
  2693. }
  2694. if (trace.evlist->nr_entries > 0)
  2695. evlist__set_evsel_handler(trace.evlist, trace__event_handler);
  2696. if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
  2697. return trace__record(&trace, argc-1, &argv[1]);
  2698. /* summary_only implies summary option, but don't overwrite summary if set */
  2699. if (trace.summary_only)
  2700. trace.summary = trace.summary_only;
  2701. if (!trace.trace_syscalls && !trace.trace_pgfaults &&
  2702. trace.evlist->nr_entries == 0 /* Was --events used? */) {
  2703. trace.trace_syscalls = true;
  2704. }
  2705. if (output_name != NULL) {
  2706. err = trace__open_output(&trace, output_name);
  2707. if (err < 0) {
  2708. perror("failed to create output file");
  2709. goto out;
  2710. }
  2711. }
  2712. err = target__validate(&trace.opts.target);
  2713. if (err) {
  2714. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2715. fprintf(trace.output, "%s", bf);
  2716. goto out_close;
  2717. }
  2718. err = target__parse_uid(&trace.opts.target);
  2719. if (err) {
  2720. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  2721. fprintf(trace.output, "%s", bf);
  2722. goto out_close;
  2723. }
  2724. if (!argc && target__none(&trace.opts.target))
  2725. trace.opts.target.system_wide = true;
  2726. if (input_name)
  2727. err = trace__replay(&trace);
  2728. else
  2729. err = trace__run(&trace, argc, argv);
  2730. out_close:
  2731. if (output_name != NULL)
  2732. fclose(trace.output);
  2733. out:
  2734. return err;
  2735. }