builtin-trace.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. #include <traceevent/event-parse.h>
  2. #include "builtin.h"
  3. #include "util/color.h"
  4. #include "util/debug.h"
  5. #include "util/evlist.h"
  6. #include "util/machine.h"
  7. #include "util/session.h"
  8. #include "util/thread.h"
  9. #include "util/parse-options.h"
  10. #include "util/strlist.h"
  11. #include "util/intlist.h"
  12. #include "util/thread_map.h"
  13. #include <libaudit.h>
  14. #include <stdlib.h>
  15. #include <sys/mman.h>
  16. #include <linux/futex.h>
  17. /* For older distros: */
  18. #ifndef MAP_STACK
  19. # define MAP_STACK 0x20000
  20. #endif
  21. #ifndef MADV_HWPOISON
  22. # define MADV_HWPOISON 100
  23. #endif
  24. #ifndef MADV_MERGEABLE
  25. # define MADV_MERGEABLE 12
  26. #endif
  27. #ifndef MADV_UNMERGEABLE
  28. # define MADV_UNMERGEABLE 13
  29. #endif
  30. struct syscall_arg {
  31. unsigned long val;
  32. void *parm;
  33. u8 idx;
  34. u8 mask;
  35. };
  36. struct strarray {
  37. int nr_entries;
  38. const char **entries;
  39. };
  40. #define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \
  41. .nr_entries = ARRAY_SIZE(array), \
  42. .entries = array, \
  43. }
  44. static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
  45. struct syscall_arg *arg)
  46. {
  47. int idx = arg->val;
  48. struct strarray *sa = arg->parm;
  49. if (idx < 0 || idx >= sa->nr_entries)
  50. return scnprintf(bf, size, "%d", idx);
  51. return scnprintf(bf, size, "%s", sa->entries[idx]);
  52. }
  53. #define SCA_STRARRAY syscall_arg__scnprintf_strarray
  54. static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
  55. struct syscall_arg *arg)
  56. {
  57. return scnprintf(bf, size, "%#lx", arg->val);
  58. }
  59. #define SCA_HEX syscall_arg__scnprintf_hex
  60. static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
  61. struct syscall_arg *arg)
  62. {
  63. int printed = 0, prot = arg->val;
  64. if (prot == PROT_NONE)
  65. return scnprintf(bf, size, "NONE");
  66. #define P_MMAP_PROT(n) \
  67. if (prot & PROT_##n) { \
  68. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  69. prot &= ~PROT_##n; \
  70. }
  71. P_MMAP_PROT(EXEC);
  72. P_MMAP_PROT(READ);
  73. P_MMAP_PROT(WRITE);
  74. #ifdef PROT_SEM
  75. P_MMAP_PROT(SEM);
  76. #endif
  77. P_MMAP_PROT(GROWSDOWN);
  78. P_MMAP_PROT(GROWSUP);
  79. #undef P_MMAP_PROT
  80. if (prot)
  81. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
  82. return printed;
  83. }
  84. #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
  85. static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
  86. struct syscall_arg *arg)
  87. {
  88. int printed = 0, flags = arg->val;
  89. #define P_MMAP_FLAG(n) \
  90. if (flags & MAP_##n) { \
  91. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  92. flags &= ~MAP_##n; \
  93. }
  94. P_MMAP_FLAG(SHARED);
  95. P_MMAP_FLAG(PRIVATE);
  96. #ifdef MAP_32BIT
  97. P_MMAP_FLAG(32BIT);
  98. #endif
  99. P_MMAP_FLAG(ANONYMOUS);
  100. P_MMAP_FLAG(DENYWRITE);
  101. P_MMAP_FLAG(EXECUTABLE);
  102. P_MMAP_FLAG(FILE);
  103. P_MMAP_FLAG(FIXED);
  104. P_MMAP_FLAG(GROWSDOWN);
  105. #ifdef MAP_HUGETLB
  106. P_MMAP_FLAG(HUGETLB);
  107. #endif
  108. P_MMAP_FLAG(LOCKED);
  109. P_MMAP_FLAG(NONBLOCK);
  110. P_MMAP_FLAG(NORESERVE);
  111. P_MMAP_FLAG(POPULATE);
  112. P_MMAP_FLAG(STACK);
  113. #ifdef MAP_UNINITIALIZED
  114. P_MMAP_FLAG(UNINITIALIZED);
  115. #endif
  116. #undef P_MMAP_FLAG
  117. if (flags)
  118. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  119. return printed;
  120. }
  121. #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
  122. static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
  123. struct syscall_arg *arg)
  124. {
  125. int behavior = arg->val;
  126. switch (behavior) {
  127. #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
  128. P_MADV_BHV(NORMAL);
  129. P_MADV_BHV(RANDOM);
  130. P_MADV_BHV(SEQUENTIAL);
  131. P_MADV_BHV(WILLNEED);
  132. P_MADV_BHV(DONTNEED);
  133. P_MADV_BHV(REMOVE);
  134. P_MADV_BHV(DONTFORK);
  135. P_MADV_BHV(DOFORK);
  136. P_MADV_BHV(HWPOISON);
  137. #ifdef MADV_SOFT_OFFLINE
  138. P_MADV_BHV(SOFT_OFFLINE);
  139. #endif
  140. P_MADV_BHV(MERGEABLE);
  141. P_MADV_BHV(UNMERGEABLE);
  142. #ifdef MADV_HUGEPAGE
  143. P_MADV_BHV(HUGEPAGE);
  144. #endif
  145. #ifdef MADV_NOHUGEPAGE
  146. P_MADV_BHV(NOHUGEPAGE);
  147. #endif
  148. #ifdef MADV_DONTDUMP
  149. P_MADV_BHV(DONTDUMP);
  150. #endif
  151. #ifdef MADV_DODUMP
  152. P_MADV_BHV(DODUMP);
  153. #endif
  154. #undef P_MADV_PHV
  155. default: break;
  156. }
  157. return scnprintf(bf, size, "%#x", behavior);
  158. }
  159. #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
  160. static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, struct syscall_arg *arg)
  161. {
  162. enum syscall_futex_args {
  163. SCF_UADDR = (1 << 0),
  164. SCF_OP = (1 << 1),
  165. SCF_VAL = (1 << 2),
  166. SCF_TIMEOUT = (1 << 3),
  167. SCF_UADDR2 = (1 << 4),
  168. SCF_VAL3 = (1 << 5),
  169. };
  170. int op = arg->val;
  171. int cmd = op & FUTEX_CMD_MASK;
  172. size_t printed = 0;
  173. switch (cmd) {
  174. #define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
  175. P_FUTEX_OP(WAIT); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
  176. P_FUTEX_OP(WAKE); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
  177. P_FUTEX_OP(FD); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
  178. P_FUTEX_OP(REQUEUE); arg->mask |= SCF_VAL3|SCF_TIMEOUT; break;
  179. P_FUTEX_OP(CMP_REQUEUE); arg->mask |= SCF_TIMEOUT; break;
  180. P_FUTEX_OP(CMP_REQUEUE_PI); arg->mask |= SCF_TIMEOUT; break;
  181. P_FUTEX_OP(WAKE_OP); break;
  182. P_FUTEX_OP(LOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
  183. P_FUTEX_OP(UNLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
  184. P_FUTEX_OP(TRYLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
  185. P_FUTEX_OP(WAIT_BITSET); arg->mask |= SCF_UADDR2; break;
  186. P_FUTEX_OP(WAKE_BITSET); arg->mask |= SCF_UADDR2; break;
  187. P_FUTEX_OP(WAIT_REQUEUE_PI); break;
  188. default: printed = scnprintf(bf, size, "%#x", cmd); break;
  189. }
  190. if (op & FUTEX_PRIVATE_FLAG)
  191. printed += scnprintf(bf + printed, size - printed, "|PRIV");
  192. if (op & FUTEX_CLOCK_REALTIME)
  193. printed += scnprintf(bf + printed, size - printed, "|CLKRT");
  194. return printed;
  195. }
  196. #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
  197. static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
  198. static DEFINE_STRARRAY(itimers);
  199. static const char *whences[] = { "SET", "CUR", "END",
  200. #ifdef SEEK_DATA
  201. "DATA",
  202. #endif
  203. #ifdef SEEK_HOLE
  204. "HOLE",
  205. #endif
  206. };
  207. static DEFINE_STRARRAY(whences);
  208. static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
  209. struct syscall_arg *arg)
  210. {
  211. int printed = 0, flags = arg->val;
  212. if (!(flags & O_CREAT))
  213. arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
  214. if (flags == 0)
  215. return scnprintf(bf, size, "RDONLY");
  216. #define P_FLAG(n) \
  217. if (flags & O_##n) { \
  218. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  219. flags &= ~O_##n; \
  220. }
  221. P_FLAG(APPEND);
  222. P_FLAG(ASYNC);
  223. P_FLAG(CLOEXEC);
  224. P_FLAG(CREAT);
  225. P_FLAG(DIRECT);
  226. P_FLAG(DIRECTORY);
  227. P_FLAG(EXCL);
  228. P_FLAG(LARGEFILE);
  229. P_FLAG(NOATIME);
  230. P_FLAG(NOCTTY);
  231. #ifdef O_NONBLOCK
  232. P_FLAG(NONBLOCK);
  233. #elif O_NDELAY
  234. P_FLAG(NDELAY);
  235. #endif
  236. #ifdef O_PATH
  237. P_FLAG(PATH);
  238. #endif
  239. P_FLAG(RDWR);
  240. #ifdef O_DSYNC
  241. if ((flags & O_SYNC) == O_SYNC)
  242. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
  243. else {
  244. P_FLAG(DSYNC);
  245. }
  246. #else
  247. P_FLAG(SYNC);
  248. #endif
  249. P_FLAG(TRUNC);
  250. P_FLAG(WRONLY);
  251. #undef P_FLAG
  252. if (flags)
  253. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  254. return printed;
  255. }
  256. #define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
  257. static struct syscall_fmt {
  258. const char *name;
  259. const char *alias;
  260. size_t (*arg_scnprintf[6])(char *bf, size_t size, struct syscall_arg *arg);
  261. void *arg_parm[6];
  262. bool errmsg;
  263. bool timeout;
  264. bool hexret;
  265. } syscall_fmts[] = {
  266. { .name = "access", .errmsg = true, },
  267. { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
  268. { .name = "brk", .hexret = true,
  269. .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
  270. { .name = "mmap", .hexret = true, },
  271. { .name = "connect", .errmsg = true, },
  272. { .name = "fstat", .errmsg = true, .alias = "newfstat", },
  273. { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
  274. { .name = "futex", .errmsg = true,
  275. .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
  276. { .name = "getitimer", .errmsg = true,
  277. .arg_scnprintf = { [0] = SCA_STRARRAY, /* which */ },
  278. .arg_parm = { [0] = &strarray__itimers, /* which */ }, },
  279. { .name = "ioctl", .errmsg = true,
  280. .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
  281. { .name = "lseek", .errmsg = true,
  282. .arg_scnprintf = { [2] = SCA_STRARRAY, /* whence */ },
  283. .arg_parm = { [2] = &strarray__whences, /* whence */ }, },
  284. { .name = "lstat", .errmsg = true, .alias = "newlstat", },
  285. { .name = "madvise", .errmsg = true,
  286. .arg_scnprintf = { [0] = SCA_HEX, /* start */
  287. [2] = SCA_MADV_BHV, /* behavior */ }, },
  288. { .name = "mmap", .hexret = true,
  289. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  290. [2] = SCA_MMAP_PROT, /* prot */
  291. [3] = SCA_MMAP_FLAGS, /* flags */ }, },
  292. { .name = "mprotect", .errmsg = true,
  293. .arg_scnprintf = { [0] = SCA_HEX, /* start */
  294. [2] = SCA_MMAP_PROT, /* prot */ }, },
  295. { .name = "mremap", .hexret = true,
  296. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  297. [4] = SCA_HEX, /* new_addr */ }, },
  298. { .name = "munmap", .errmsg = true,
  299. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  300. { .name = "open", .errmsg = true,
  301. .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
  302. { .name = "open_by_handle_at", .errmsg = true,
  303. .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
  304. { .name = "openat", .errmsg = true,
  305. .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
  306. { .name = "poll", .errmsg = true, .timeout = true, },
  307. { .name = "ppoll", .errmsg = true, .timeout = true, },
  308. { .name = "pread", .errmsg = true, .alias = "pread64", },
  309. { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
  310. { .name = "read", .errmsg = true, },
  311. { .name = "recvfrom", .errmsg = true, },
  312. { .name = "select", .errmsg = true, .timeout = true, },
  313. { .name = "setitimer", .errmsg = true,
  314. .arg_scnprintf = { [0] = SCA_STRARRAY, /* which */ },
  315. .arg_parm = { [0] = &strarray__itimers, /* which */ }, },
  316. { .name = "socket", .errmsg = true, },
  317. { .name = "stat", .errmsg = true, .alias = "newstat", },
  318. { .name = "uname", .errmsg = true, .alias = "newuname", },
  319. };
  320. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  321. {
  322. const struct syscall_fmt *fmt = fmtp;
  323. return strcmp(name, fmt->name);
  324. }
  325. static struct syscall_fmt *syscall_fmt__find(const char *name)
  326. {
  327. const int nmemb = ARRAY_SIZE(syscall_fmts);
  328. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  329. }
  330. struct syscall {
  331. struct event_format *tp_format;
  332. const char *name;
  333. bool filtered;
  334. struct syscall_fmt *fmt;
  335. size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  336. void **arg_parm;
  337. };
  338. static size_t fprintf_duration(unsigned long t, FILE *fp)
  339. {
  340. double duration = (double)t / NSEC_PER_MSEC;
  341. size_t printed = fprintf(fp, "(");
  342. if (duration >= 1.0)
  343. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  344. else if (duration >= 0.01)
  345. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  346. else
  347. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  348. return printed + fprintf(fp, "): ");
  349. }
  350. struct thread_trace {
  351. u64 entry_time;
  352. u64 exit_time;
  353. bool entry_pending;
  354. unsigned long nr_events;
  355. char *entry_str;
  356. double runtime_ms;
  357. };
  358. static struct thread_trace *thread_trace__new(void)
  359. {
  360. return zalloc(sizeof(struct thread_trace));
  361. }
  362. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  363. {
  364. struct thread_trace *ttrace;
  365. if (thread == NULL)
  366. goto fail;
  367. if (thread->priv == NULL)
  368. thread->priv = thread_trace__new();
  369. if (thread->priv == NULL)
  370. goto fail;
  371. ttrace = thread->priv;
  372. ++ttrace->nr_events;
  373. return ttrace;
  374. fail:
  375. color_fprintf(fp, PERF_COLOR_RED,
  376. "WARNING: not enough memory, dropping samples!\n");
  377. return NULL;
  378. }
  379. struct trace {
  380. struct perf_tool tool;
  381. int audit_machine;
  382. struct {
  383. int max;
  384. struct syscall *table;
  385. } syscalls;
  386. struct perf_record_opts opts;
  387. struct machine host;
  388. u64 base_time;
  389. FILE *output;
  390. unsigned long nr_events;
  391. struct strlist *ev_qualifier;
  392. bool not_ev_qualifier;
  393. struct intlist *tid_list;
  394. struct intlist *pid_list;
  395. bool sched;
  396. bool multiple_threads;
  397. double duration_filter;
  398. double runtime_ms;
  399. };
  400. static bool trace__filter_duration(struct trace *trace, double t)
  401. {
  402. return t < (trace->duration_filter * NSEC_PER_MSEC);
  403. }
  404. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  405. {
  406. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  407. return fprintf(fp, "%10.3f ", ts);
  408. }
  409. static bool done = false;
  410. static void sig_handler(int sig __maybe_unused)
  411. {
  412. done = true;
  413. }
  414. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  415. u64 duration, u64 tstamp, FILE *fp)
  416. {
  417. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  418. printed += fprintf_duration(duration, fp);
  419. if (trace->multiple_threads)
  420. printed += fprintf(fp, "%d ", thread->tid);
  421. return printed;
  422. }
  423. static int trace__process_event(struct trace *trace, struct machine *machine,
  424. union perf_event *event)
  425. {
  426. int ret = 0;
  427. switch (event->header.type) {
  428. case PERF_RECORD_LOST:
  429. color_fprintf(trace->output, PERF_COLOR_RED,
  430. "LOST %" PRIu64 " events!\n", event->lost.lost);
  431. ret = machine__process_lost_event(machine, event);
  432. default:
  433. ret = machine__process_event(machine, event);
  434. break;
  435. }
  436. return ret;
  437. }
  438. static int trace__tool_process(struct perf_tool *tool,
  439. union perf_event *event,
  440. struct perf_sample *sample __maybe_unused,
  441. struct machine *machine)
  442. {
  443. struct trace *trace = container_of(tool, struct trace, tool);
  444. return trace__process_event(trace, machine, event);
  445. }
  446. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  447. {
  448. int err = symbol__init();
  449. if (err)
  450. return err;
  451. machine__init(&trace->host, "", HOST_KERNEL_ID);
  452. machine__create_kernel_maps(&trace->host);
  453. if (perf_target__has_task(&trace->opts.target)) {
  454. err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
  455. trace__tool_process,
  456. &trace->host);
  457. } else {
  458. err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
  459. &trace->host);
  460. }
  461. if (err)
  462. symbol__exit();
  463. return err;
  464. }
  465. static int syscall__set_arg_fmts(struct syscall *sc)
  466. {
  467. struct format_field *field;
  468. int idx = 0;
  469. sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
  470. if (sc->arg_scnprintf == NULL)
  471. return -1;
  472. if (sc->fmt)
  473. sc->arg_parm = sc->fmt->arg_parm;
  474. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  475. if (sc->fmt && sc->fmt->arg_scnprintf[idx])
  476. sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
  477. else if (field->flags & FIELD_IS_POINTER)
  478. sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
  479. ++idx;
  480. }
  481. return 0;
  482. }
  483. static int trace__read_syscall_info(struct trace *trace, int id)
  484. {
  485. char tp_name[128];
  486. struct syscall *sc;
  487. const char *name = audit_syscall_to_name(id, trace->audit_machine);
  488. if (name == NULL)
  489. return -1;
  490. if (id > trace->syscalls.max) {
  491. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  492. if (nsyscalls == NULL)
  493. return -1;
  494. if (trace->syscalls.max != -1) {
  495. memset(nsyscalls + trace->syscalls.max + 1, 0,
  496. (id - trace->syscalls.max) * sizeof(*sc));
  497. } else {
  498. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  499. }
  500. trace->syscalls.table = nsyscalls;
  501. trace->syscalls.max = id;
  502. }
  503. sc = trace->syscalls.table + id;
  504. sc->name = name;
  505. if (trace->ev_qualifier) {
  506. bool in = strlist__find(trace->ev_qualifier, name) != NULL;
  507. if (!(in ^ trace->not_ev_qualifier)) {
  508. sc->filtered = true;
  509. /*
  510. * No need to do read tracepoint information since this will be
  511. * filtered out.
  512. */
  513. return 0;
  514. }
  515. }
  516. sc->fmt = syscall_fmt__find(sc->name);
  517. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  518. sc->tp_format = event_format__new("syscalls", tp_name);
  519. if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
  520. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  521. sc->tp_format = event_format__new("syscalls", tp_name);
  522. }
  523. if (sc->tp_format == NULL)
  524. return -1;
  525. return syscall__set_arg_fmts(sc);
  526. }
  527. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  528. unsigned long *args)
  529. {
  530. size_t printed = 0;
  531. if (sc->tp_format != NULL) {
  532. struct format_field *field;
  533. u8 bit = 1;
  534. struct syscall_arg arg = {
  535. .idx = 0,
  536. .mask = 0,
  537. };
  538. for (field = sc->tp_format->format.fields->next; field;
  539. field = field->next, ++arg.idx, bit <<= 1) {
  540. if (arg.mask & bit)
  541. continue;
  542. printed += scnprintf(bf + printed, size - printed,
  543. "%s%s: ", printed ? ", " : "", field->name);
  544. if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
  545. arg.val = args[arg.idx];
  546. if (sc->arg_parm)
  547. arg.parm = sc->arg_parm[arg.idx];
  548. printed += sc->arg_scnprintf[arg.idx](bf + printed,
  549. size - printed, &arg);
  550. } else {
  551. printed += scnprintf(bf + printed, size - printed,
  552. "%ld", args[arg.idx]);
  553. }
  554. }
  555. } else {
  556. int i = 0;
  557. while (i < 6) {
  558. printed += scnprintf(bf + printed, size - printed,
  559. "%sarg%d: %ld",
  560. printed ? ", " : "", i, args[i]);
  561. ++i;
  562. }
  563. }
  564. return printed;
  565. }
  566. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  567. struct perf_sample *sample);
  568. static struct syscall *trace__syscall_info(struct trace *trace,
  569. struct perf_evsel *evsel,
  570. struct perf_sample *sample)
  571. {
  572. int id = perf_evsel__intval(evsel, sample, "id");
  573. if (id < 0) {
  574. /*
  575. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  576. * before that, leaving at a higher verbosity level till that is
  577. * explained. Reproduced with plain ftrace with:
  578. *
  579. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  580. * grep "NR -1 " /t/trace_pipe
  581. *
  582. * After generating some load on the machine.
  583. */
  584. if (verbose > 1) {
  585. static u64 n;
  586. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  587. id, perf_evsel__name(evsel), ++n);
  588. }
  589. return NULL;
  590. }
  591. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  592. trace__read_syscall_info(trace, id))
  593. goto out_cant_read;
  594. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  595. goto out_cant_read;
  596. return &trace->syscalls.table[id];
  597. out_cant_read:
  598. if (verbose) {
  599. fprintf(trace->output, "Problems reading syscall %d", id);
  600. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  601. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  602. fputs(" information\n", trace->output);
  603. }
  604. return NULL;
  605. }
  606. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  607. struct perf_sample *sample)
  608. {
  609. char *msg;
  610. void *args;
  611. size_t printed = 0;
  612. struct thread *thread;
  613. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  614. struct thread_trace *ttrace;
  615. if (sc == NULL)
  616. return -1;
  617. if (sc->filtered)
  618. return 0;
  619. thread = machine__findnew_thread(&trace->host, sample->pid,
  620. sample->tid);
  621. ttrace = thread__trace(thread, trace->output);
  622. if (ttrace == NULL)
  623. return -1;
  624. args = perf_evsel__rawptr(evsel, sample, "args");
  625. if (args == NULL) {
  626. fprintf(trace->output, "Problems reading syscall arguments\n");
  627. return -1;
  628. }
  629. ttrace = thread->priv;
  630. if (ttrace->entry_str == NULL) {
  631. ttrace->entry_str = malloc(1024);
  632. if (!ttrace->entry_str)
  633. return -1;
  634. }
  635. ttrace->entry_time = sample->time;
  636. msg = ttrace->entry_str;
  637. printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
  638. printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
  639. if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
  640. if (!trace->duration_filter) {
  641. trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
  642. fprintf(trace->output, "%-70s\n", ttrace->entry_str);
  643. }
  644. } else
  645. ttrace->entry_pending = true;
  646. return 0;
  647. }
  648. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  649. struct perf_sample *sample)
  650. {
  651. int ret;
  652. u64 duration = 0;
  653. struct thread *thread;
  654. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  655. struct thread_trace *ttrace;
  656. if (sc == NULL)
  657. return -1;
  658. if (sc->filtered)
  659. return 0;
  660. thread = machine__findnew_thread(&trace->host, sample->pid,
  661. sample->tid);
  662. ttrace = thread__trace(thread, trace->output);
  663. if (ttrace == NULL)
  664. return -1;
  665. ret = perf_evsel__intval(evsel, sample, "ret");
  666. ttrace = thread->priv;
  667. ttrace->exit_time = sample->time;
  668. if (ttrace->entry_time) {
  669. duration = sample->time - ttrace->entry_time;
  670. if (trace__filter_duration(trace, duration))
  671. goto out;
  672. } else if (trace->duration_filter)
  673. goto out;
  674. trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
  675. if (ttrace->entry_pending) {
  676. fprintf(trace->output, "%-70s", ttrace->entry_str);
  677. } else {
  678. fprintf(trace->output, " ... [");
  679. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  680. fprintf(trace->output, "]: %s()", sc->name);
  681. }
  682. if (sc->fmt == NULL) {
  683. signed_print:
  684. fprintf(trace->output, ") = %d", ret);
  685. } else if (ret < 0 && sc->fmt->errmsg) {
  686. char bf[256];
  687. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  688. *e = audit_errno_to_name(-ret);
  689. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  690. } else if (ret == 0 && sc->fmt->timeout)
  691. fprintf(trace->output, ") = 0 Timeout");
  692. else if (sc->fmt->hexret)
  693. fprintf(trace->output, ") = %#x", ret);
  694. else
  695. goto signed_print;
  696. fputc('\n', trace->output);
  697. out:
  698. ttrace->entry_pending = false;
  699. return 0;
  700. }
  701. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  702. struct perf_sample *sample)
  703. {
  704. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  705. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  706. struct thread *thread = machine__findnew_thread(&trace->host,
  707. sample->pid,
  708. sample->tid);
  709. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  710. if (ttrace == NULL)
  711. goto out_dump;
  712. ttrace->runtime_ms += runtime_ms;
  713. trace->runtime_ms += runtime_ms;
  714. return 0;
  715. out_dump:
  716. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  717. evsel->name,
  718. perf_evsel__strval(evsel, sample, "comm"),
  719. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  720. runtime,
  721. perf_evsel__intval(evsel, sample, "vruntime"));
  722. return 0;
  723. }
  724. static bool skip_sample(struct trace *trace, struct perf_sample *sample)
  725. {
  726. if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
  727. (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
  728. return false;
  729. if (trace->pid_list || trace->tid_list)
  730. return true;
  731. return false;
  732. }
  733. static int trace__process_sample(struct perf_tool *tool,
  734. union perf_event *event __maybe_unused,
  735. struct perf_sample *sample,
  736. struct perf_evsel *evsel,
  737. struct machine *machine __maybe_unused)
  738. {
  739. struct trace *trace = container_of(tool, struct trace, tool);
  740. int err = 0;
  741. tracepoint_handler handler = evsel->handler.func;
  742. if (skip_sample(trace, sample))
  743. return 0;
  744. if (trace->base_time == 0)
  745. trace->base_time = sample->time;
  746. if (handler)
  747. handler(trace, evsel, sample);
  748. return err;
  749. }
  750. static bool
  751. perf_session__has_tp(struct perf_session *session, const char *name)
  752. {
  753. struct perf_evsel *evsel;
  754. evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
  755. return evsel != NULL;
  756. }
  757. static int parse_target_str(struct trace *trace)
  758. {
  759. if (trace->opts.target.pid) {
  760. trace->pid_list = intlist__new(trace->opts.target.pid);
  761. if (trace->pid_list == NULL) {
  762. pr_err("Error parsing process id string\n");
  763. return -EINVAL;
  764. }
  765. }
  766. if (trace->opts.target.tid) {
  767. trace->tid_list = intlist__new(trace->opts.target.tid);
  768. if (trace->tid_list == NULL) {
  769. pr_err("Error parsing thread id string\n");
  770. return -EINVAL;
  771. }
  772. }
  773. return 0;
  774. }
  775. static int trace__run(struct trace *trace, int argc, const char **argv)
  776. {
  777. struct perf_evlist *evlist = perf_evlist__new();
  778. struct perf_evsel *evsel;
  779. int err = -1, i;
  780. unsigned long before;
  781. const bool forks = argc > 0;
  782. if (evlist == NULL) {
  783. fprintf(trace->output, "Not enough memory to run!\n");
  784. goto out;
  785. }
  786. if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
  787. perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
  788. fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
  789. goto out_delete_evlist;
  790. }
  791. if (trace->sched &&
  792. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  793. trace__sched_stat_runtime)) {
  794. fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
  795. goto out_delete_evlist;
  796. }
  797. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  798. if (err < 0) {
  799. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  800. goto out_delete_evlist;
  801. }
  802. err = trace__symbols_init(trace, evlist);
  803. if (err < 0) {
  804. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  805. goto out_delete_maps;
  806. }
  807. perf_evlist__config(evlist, &trace->opts);
  808. signal(SIGCHLD, sig_handler);
  809. signal(SIGINT, sig_handler);
  810. if (forks) {
  811. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  812. argv, false, false);
  813. if (err < 0) {
  814. fprintf(trace->output, "Couldn't run the workload!\n");
  815. goto out_delete_maps;
  816. }
  817. }
  818. err = perf_evlist__open(evlist);
  819. if (err < 0) {
  820. fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
  821. goto out_delete_maps;
  822. }
  823. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  824. if (err < 0) {
  825. fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
  826. goto out_close_evlist;
  827. }
  828. perf_evlist__enable(evlist);
  829. if (forks)
  830. perf_evlist__start_workload(evlist);
  831. trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
  832. again:
  833. before = trace->nr_events;
  834. for (i = 0; i < evlist->nr_mmaps; i++) {
  835. union perf_event *event;
  836. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  837. const u32 type = event->header.type;
  838. tracepoint_handler handler;
  839. struct perf_sample sample;
  840. ++trace->nr_events;
  841. err = perf_evlist__parse_sample(evlist, event, &sample);
  842. if (err) {
  843. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  844. continue;
  845. }
  846. if (trace->base_time == 0)
  847. trace->base_time = sample.time;
  848. if (type != PERF_RECORD_SAMPLE) {
  849. trace__process_event(trace, &trace->host, event);
  850. continue;
  851. }
  852. evsel = perf_evlist__id2evsel(evlist, sample.id);
  853. if (evsel == NULL) {
  854. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
  855. continue;
  856. }
  857. if (sample.raw_data == NULL) {
  858. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  859. perf_evsel__name(evsel), sample.tid,
  860. sample.cpu, sample.raw_size);
  861. continue;
  862. }
  863. handler = evsel->handler.func;
  864. handler(trace, evsel, &sample);
  865. if (done)
  866. goto out_unmap_evlist;
  867. }
  868. }
  869. if (trace->nr_events == before) {
  870. if (done)
  871. goto out_unmap_evlist;
  872. poll(evlist->pollfd, evlist->nr_fds, -1);
  873. }
  874. if (done)
  875. perf_evlist__disable(evlist);
  876. goto again;
  877. out_unmap_evlist:
  878. perf_evlist__munmap(evlist);
  879. out_close_evlist:
  880. perf_evlist__close(evlist);
  881. out_delete_maps:
  882. perf_evlist__delete_maps(evlist);
  883. out_delete_evlist:
  884. perf_evlist__delete(evlist);
  885. out:
  886. return err;
  887. }
  888. static int trace__replay(struct trace *trace)
  889. {
  890. const struct perf_evsel_str_handler handlers[] = {
  891. { "raw_syscalls:sys_enter", trace__sys_enter, },
  892. { "raw_syscalls:sys_exit", trace__sys_exit, },
  893. };
  894. struct perf_session *session;
  895. int err = -1;
  896. trace->tool.sample = trace__process_sample;
  897. trace->tool.mmap = perf_event__process_mmap;
  898. trace->tool.mmap2 = perf_event__process_mmap2;
  899. trace->tool.comm = perf_event__process_comm;
  900. trace->tool.exit = perf_event__process_exit;
  901. trace->tool.fork = perf_event__process_fork;
  902. trace->tool.attr = perf_event__process_attr;
  903. trace->tool.tracing_data = perf_event__process_tracing_data;
  904. trace->tool.build_id = perf_event__process_build_id;
  905. trace->tool.ordered_samples = true;
  906. trace->tool.ordering_requires_timestamps = true;
  907. /* add tid to output */
  908. trace->multiple_threads = true;
  909. if (symbol__init() < 0)
  910. return -1;
  911. session = perf_session__new(input_name, O_RDONLY, 0, false,
  912. &trace->tool);
  913. if (session == NULL)
  914. return -ENOMEM;
  915. err = perf_session__set_tracepoints_handlers(session, handlers);
  916. if (err)
  917. goto out;
  918. if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
  919. pr_err("Data file does not have raw_syscalls:sys_enter events\n");
  920. goto out;
  921. }
  922. if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
  923. pr_err("Data file does not have raw_syscalls:sys_exit events\n");
  924. goto out;
  925. }
  926. err = parse_target_str(trace);
  927. if (err != 0)
  928. goto out;
  929. setup_pager();
  930. err = perf_session__process_events(session, &trace->tool);
  931. if (err)
  932. pr_err("Failed to process events, error %d", err);
  933. out:
  934. perf_session__delete(session);
  935. return err;
  936. }
  937. static size_t trace__fprintf_threads_header(FILE *fp)
  938. {
  939. size_t printed;
  940. printed = fprintf(fp, "\n _____________________________________________________________________\n");
  941. printed += fprintf(fp," __) Summary of events (__\n\n");
  942. printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
  943. printed += fprintf(fp," _____________________________________________________________________\n\n");
  944. return printed;
  945. }
  946. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  947. {
  948. size_t printed = trace__fprintf_threads_header(fp);
  949. struct rb_node *nd;
  950. for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
  951. struct thread *thread = rb_entry(nd, struct thread, rb_node);
  952. struct thread_trace *ttrace = thread->priv;
  953. const char *color;
  954. double ratio;
  955. if (ttrace == NULL)
  956. continue;
  957. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  958. color = PERF_COLOR_NORMAL;
  959. if (ratio > 50.0)
  960. color = PERF_COLOR_RED;
  961. else if (ratio > 25.0)
  962. color = PERF_COLOR_GREEN;
  963. else if (ratio > 5.0)
  964. color = PERF_COLOR_YELLOW;
  965. printed += color_fprintf(fp, color, "%20s", thread->comm);
  966. printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
  967. printed += color_fprintf(fp, color, "%5.1f%%", ratio);
  968. printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
  969. }
  970. return printed;
  971. }
  972. static int trace__set_duration(const struct option *opt, const char *str,
  973. int unset __maybe_unused)
  974. {
  975. struct trace *trace = opt->value;
  976. trace->duration_filter = atof(str);
  977. return 0;
  978. }
  979. static int trace__open_output(struct trace *trace, const char *filename)
  980. {
  981. struct stat st;
  982. if (!stat(filename, &st) && st.st_size) {
  983. char oldname[PATH_MAX];
  984. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  985. unlink(oldname);
  986. rename(filename, oldname);
  987. }
  988. trace->output = fopen(filename, "w");
  989. return trace->output == NULL ? -errno : 0;
  990. }
  991. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  992. {
  993. const char * const trace_usage[] = {
  994. "perf trace [<options>] [<command>]",
  995. "perf trace [<options>] -- <command> [<options>]",
  996. NULL
  997. };
  998. struct trace trace = {
  999. .audit_machine = audit_detect_machine(),
  1000. .syscalls = {
  1001. . max = -1,
  1002. },
  1003. .opts = {
  1004. .target = {
  1005. .uid = UINT_MAX,
  1006. .uses_mmap = true,
  1007. },
  1008. .user_freq = UINT_MAX,
  1009. .user_interval = ULLONG_MAX,
  1010. .no_delay = true,
  1011. .mmap_pages = 1024,
  1012. },
  1013. .output = stdout,
  1014. };
  1015. const char *output_name = NULL;
  1016. const char *ev_qualifier_str = NULL;
  1017. const struct option trace_options[] = {
  1018. OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
  1019. "list of events to trace"),
  1020. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1021. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  1022. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  1023. "trace events on existing process id"),
  1024. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  1025. "trace events on existing thread id"),
  1026. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  1027. "system-wide collection from all CPUs"),
  1028. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  1029. "list of cpus to monitor"),
  1030. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  1031. "child tasks do not inherit counters"),
  1032. OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
  1033. "number of mmap data pages"),
  1034. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  1035. "user to profile"),
  1036. OPT_CALLBACK(0, "duration", &trace, "float",
  1037. "show only events with duration > N.M ms",
  1038. trace__set_duration),
  1039. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  1040. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  1041. OPT_END()
  1042. };
  1043. int err;
  1044. char bf[BUFSIZ];
  1045. argc = parse_options(argc, argv, trace_options, trace_usage, 0);
  1046. if (output_name != NULL) {
  1047. err = trace__open_output(&trace, output_name);
  1048. if (err < 0) {
  1049. perror("failed to create output file");
  1050. goto out;
  1051. }
  1052. }
  1053. if (ev_qualifier_str != NULL) {
  1054. const char *s = ev_qualifier_str;
  1055. trace.not_ev_qualifier = *s == '!';
  1056. if (trace.not_ev_qualifier)
  1057. ++s;
  1058. trace.ev_qualifier = strlist__new(true, s);
  1059. if (trace.ev_qualifier == NULL) {
  1060. fputs("Not enough memory to parse event qualifier",
  1061. trace.output);
  1062. err = -ENOMEM;
  1063. goto out_close;
  1064. }
  1065. }
  1066. err = perf_target__validate(&trace.opts.target);
  1067. if (err) {
  1068. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  1069. fprintf(trace.output, "%s", bf);
  1070. goto out_close;
  1071. }
  1072. err = perf_target__parse_uid(&trace.opts.target);
  1073. if (err) {
  1074. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  1075. fprintf(trace.output, "%s", bf);
  1076. goto out_close;
  1077. }
  1078. if (!argc && perf_target__none(&trace.opts.target))
  1079. trace.opts.target.system_wide = true;
  1080. if (input_name)
  1081. err = trace__replay(&trace);
  1082. else
  1083. err = trace__run(&trace, argc, argv);
  1084. if (trace.sched && !err)
  1085. trace__fprintf_thread_summary(&trace, trace.output);
  1086. out_close:
  1087. if (output_name != NULL)
  1088. fclose(trace.output);
  1089. out:
  1090. return err;
  1091. }