builtin-trace.c 33 KB

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