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