builtin-trace.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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/thread.h"
  8. #include "util/parse-options.h"
  9. #include "util/strlist.h"
  10. #include "util/thread_map.h"
  11. #include <libaudit.h>
  12. #include <stdlib.h>
  13. #include <sys/mman.h>
  14. static size_t syscall_arg__scnprintf_hex(char *bf, size_t size, unsigned long arg)
  15. {
  16. return scnprintf(bf, size, "%#lx", arg);
  17. }
  18. #define SCA_HEX syscall_arg__scnprintf_hex
  19. static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size, unsigned long arg)
  20. {
  21. int printed = 0, prot = arg;
  22. if (prot == PROT_NONE)
  23. return scnprintf(bf, size, "NONE");
  24. #define P_MMAP_PROT(n) \
  25. if (prot & PROT_##n) { \
  26. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  27. prot &= ~PROT_##n; \
  28. }
  29. P_MMAP_PROT(EXEC);
  30. P_MMAP_PROT(READ);
  31. P_MMAP_PROT(WRITE);
  32. #ifdef PROT_SEM
  33. P_MMAP_PROT(SEM);
  34. #endif
  35. P_MMAP_PROT(GROWSDOWN);
  36. P_MMAP_PROT(GROWSUP);
  37. #undef P_MMAP_PROT
  38. if (prot)
  39. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
  40. return printed;
  41. }
  42. #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
  43. static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size, unsigned long arg)
  44. {
  45. int printed = 0, flags = arg;
  46. #define P_MMAP_FLAG(n) \
  47. if (flags & MAP_##n) { \
  48. printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
  49. flags &= ~MAP_##n; \
  50. }
  51. P_MMAP_FLAG(SHARED);
  52. P_MMAP_FLAG(PRIVATE);
  53. P_MMAP_FLAG(32BIT);
  54. P_MMAP_FLAG(ANONYMOUS);
  55. P_MMAP_FLAG(DENYWRITE);
  56. P_MMAP_FLAG(EXECUTABLE);
  57. P_MMAP_FLAG(FILE);
  58. P_MMAP_FLAG(FIXED);
  59. P_MMAP_FLAG(GROWSDOWN);
  60. P_MMAP_FLAG(HUGETLB);
  61. P_MMAP_FLAG(LOCKED);
  62. P_MMAP_FLAG(NONBLOCK);
  63. P_MMAP_FLAG(NORESERVE);
  64. P_MMAP_FLAG(POPULATE);
  65. P_MMAP_FLAG(STACK);
  66. #ifdef MAP_UNINITIALIZED
  67. P_MMAP_FLAG(UNINITIALIZED);
  68. #endif
  69. #undef P_MMAP_FLAG
  70. if (flags)
  71. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  72. return printed;
  73. }
  74. #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
  75. static struct syscall_fmt {
  76. const char *name;
  77. const char *alias;
  78. size_t (*arg_scnprintf[6])(char *bf, size_t size, unsigned long arg);
  79. bool errmsg;
  80. bool timeout;
  81. bool hexret;
  82. } syscall_fmts[] = {
  83. { .name = "access", .errmsg = true, },
  84. { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
  85. { .name = "brk", .hexret = true,
  86. .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
  87. { .name = "mmap", .hexret = true, },
  88. { .name = "connect", .errmsg = true, },
  89. { .name = "fstat", .errmsg = true, .alias = "newfstat", },
  90. { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
  91. { .name = "futex", .errmsg = true, },
  92. { .name = "ioctl", .errmsg = true,
  93. .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
  94. { .name = "lstat", .errmsg = true, .alias = "newlstat", },
  95. { .name = "mmap", .hexret = true,
  96. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  97. [2] = SCA_MMAP_PROT, /* prot */
  98. [3] = SCA_MMAP_FLAGS, /* flags */ }, },
  99. { .name = "mprotect", .errmsg = true,
  100. .arg_scnprintf = { [0] = SCA_HEX, /* start */
  101. [2] = SCA_MMAP_PROT, /* prot */ }, },
  102. { .name = "mremap", .hexret = true,
  103. .arg_scnprintf = { [0] = SCA_HEX, /* addr */
  104. [4] = SCA_HEX, /* new_addr */ }, },
  105. { .name = "munmap", .errmsg = true,
  106. .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
  107. { .name = "open", .errmsg = true, },
  108. { .name = "poll", .errmsg = true, .timeout = true, },
  109. { .name = "ppoll", .errmsg = true, .timeout = true, },
  110. { .name = "pread", .errmsg = true, .alias = "pread64", },
  111. { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
  112. { .name = "read", .errmsg = true, },
  113. { .name = "recvfrom", .errmsg = true, },
  114. { .name = "select", .errmsg = true, .timeout = true, },
  115. { .name = "socket", .errmsg = true, },
  116. { .name = "stat", .errmsg = true, .alias = "newstat", },
  117. { .name = "uname", .errmsg = true, .alias = "newuname", },
  118. };
  119. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  120. {
  121. const struct syscall_fmt *fmt = fmtp;
  122. return strcmp(name, fmt->name);
  123. }
  124. static struct syscall_fmt *syscall_fmt__find(const char *name)
  125. {
  126. const int nmemb = ARRAY_SIZE(syscall_fmts);
  127. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  128. }
  129. struct syscall {
  130. struct event_format *tp_format;
  131. const char *name;
  132. bool filtered;
  133. struct syscall_fmt *fmt;
  134. size_t (**arg_scnprintf)(char *bf, size_t size, unsigned long arg);
  135. };
  136. static size_t fprintf_duration(unsigned long t, FILE *fp)
  137. {
  138. double duration = (double)t / NSEC_PER_MSEC;
  139. size_t printed = fprintf(fp, "(");
  140. if (duration >= 1.0)
  141. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  142. else if (duration >= 0.01)
  143. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  144. else
  145. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  146. return printed + fprintf(fp, "): ");
  147. }
  148. struct thread_trace {
  149. u64 entry_time;
  150. u64 exit_time;
  151. bool entry_pending;
  152. unsigned long nr_events;
  153. char *entry_str;
  154. double runtime_ms;
  155. };
  156. static struct thread_trace *thread_trace__new(void)
  157. {
  158. return zalloc(sizeof(struct thread_trace));
  159. }
  160. static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
  161. {
  162. struct thread_trace *ttrace;
  163. if (thread == NULL)
  164. goto fail;
  165. if (thread->priv == NULL)
  166. thread->priv = thread_trace__new();
  167. if (thread->priv == NULL)
  168. goto fail;
  169. ttrace = thread->priv;
  170. ++ttrace->nr_events;
  171. return ttrace;
  172. fail:
  173. color_fprintf(fp, PERF_COLOR_RED,
  174. "WARNING: not enough memory, dropping samples!\n");
  175. return NULL;
  176. }
  177. struct trace {
  178. struct perf_tool tool;
  179. int audit_machine;
  180. struct {
  181. int max;
  182. struct syscall *table;
  183. } syscalls;
  184. struct perf_record_opts opts;
  185. struct machine host;
  186. u64 base_time;
  187. FILE *output;
  188. unsigned long nr_events;
  189. struct strlist *ev_qualifier;
  190. bool not_ev_qualifier;
  191. bool sched;
  192. bool multiple_threads;
  193. double duration_filter;
  194. double runtime_ms;
  195. };
  196. static bool trace__filter_duration(struct trace *trace, double t)
  197. {
  198. return t < (trace->duration_filter * NSEC_PER_MSEC);
  199. }
  200. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  201. {
  202. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  203. return fprintf(fp, "%10.3f ", ts);
  204. }
  205. static bool done = false;
  206. static void sig_handler(int sig __maybe_unused)
  207. {
  208. done = true;
  209. }
  210. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  211. u64 duration, u64 tstamp, FILE *fp)
  212. {
  213. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  214. printed += fprintf_duration(duration, fp);
  215. if (trace->multiple_threads)
  216. printed += fprintf(fp, "%d ", thread->tid);
  217. return printed;
  218. }
  219. static int trace__process_event(struct trace *trace, struct machine *machine,
  220. union perf_event *event)
  221. {
  222. int ret = 0;
  223. switch (event->header.type) {
  224. case PERF_RECORD_LOST:
  225. color_fprintf(trace->output, PERF_COLOR_RED,
  226. "LOST %" PRIu64 " events!\n", event->lost.lost);
  227. ret = machine__process_lost_event(machine, event);
  228. default:
  229. ret = machine__process_event(machine, event);
  230. break;
  231. }
  232. return ret;
  233. }
  234. static int trace__tool_process(struct perf_tool *tool,
  235. union perf_event *event,
  236. struct perf_sample *sample __maybe_unused,
  237. struct machine *machine)
  238. {
  239. struct trace *trace = container_of(tool, struct trace, tool);
  240. return trace__process_event(trace, machine, event);
  241. }
  242. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  243. {
  244. int err = symbol__init();
  245. if (err)
  246. return err;
  247. machine__init(&trace->host, "", HOST_KERNEL_ID);
  248. machine__create_kernel_maps(&trace->host);
  249. if (perf_target__has_task(&trace->opts.target)) {
  250. err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
  251. trace__tool_process,
  252. &trace->host);
  253. } else {
  254. err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
  255. &trace->host);
  256. }
  257. if (err)
  258. symbol__exit();
  259. return err;
  260. }
  261. static int syscall__set_arg_fmts(struct syscall *sc)
  262. {
  263. struct format_field *field;
  264. int idx = 0;
  265. sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
  266. if (sc->arg_scnprintf == NULL)
  267. return -1;
  268. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  269. if (sc->fmt && sc->fmt->arg_scnprintf[idx])
  270. sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
  271. else if (field->flags & FIELD_IS_POINTER)
  272. sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
  273. ++idx;
  274. }
  275. return 0;
  276. }
  277. static int trace__read_syscall_info(struct trace *trace, int id)
  278. {
  279. char tp_name[128];
  280. struct syscall *sc;
  281. const char *name = audit_syscall_to_name(id, trace->audit_machine);
  282. if (name == NULL)
  283. return -1;
  284. if (id > trace->syscalls.max) {
  285. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  286. if (nsyscalls == NULL)
  287. return -1;
  288. if (trace->syscalls.max != -1) {
  289. memset(nsyscalls + trace->syscalls.max + 1, 0,
  290. (id - trace->syscalls.max) * sizeof(*sc));
  291. } else {
  292. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  293. }
  294. trace->syscalls.table = nsyscalls;
  295. trace->syscalls.max = id;
  296. }
  297. sc = trace->syscalls.table + id;
  298. sc->name = name;
  299. if (trace->ev_qualifier) {
  300. bool in = strlist__find(trace->ev_qualifier, name) != NULL;
  301. if (!(in ^ trace->not_ev_qualifier)) {
  302. sc->filtered = true;
  303. /*
  304. * No need to do read tracepoint information since this will be
  305. * filtered out.
  306. */
  307. return 0;
  308. }
  309. }
  310. sc->fmt = syscall_fmt__find(sc->name);
  311. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  312. sc->tp_format = event_format__new("syscalls", tp_name);
  313. if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
  314. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  315. sc->tp_format = event_format__new("syscalls", tp_name);
  316. }
  317. if (sc->tp_format == NULL)
  318. return -1;
  319. return syscall__set_arg_fmts(sc);
  320. }
  321. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  322. unsigned long *args)
  323. {
  324. int i = 0;
  325. size_t printed = 0;
  326. if (sc->tp_format != NULL) {
  327. struct format_field *field;
  328. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  329. printed += scnprintf(bf + printed, size - printed,
  330. "%s%s: ", printed ? ", " : "", field->name);
  331. if (sc->arg_scnprintf && sc->arg_scnprintf[i])
  332. printed += sc->arg_scnprintf[i](bf + printed, size - printed, args[i]);
  333. else
  334. printed += scnprintf(bf + printed, size - printed,
  335. "%ld", args[i]);
  336. ++i;
  337. }
  338. } else {
  339. while (i < 6) {
  340. printed += scnprintf(bf + printed, size - printed,
  341. "%sarg%d: %ld",
  342. printed ? ", " : "", i, args[i]);
  343. ++i;
  344. }
  345. }
  346. return printed;
  347. }
  348. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  349. struct perf_sample *sample);
  350. static struct syscall *trace__syscall_info(struct trace *trace,
  351. struct perf_evsel *evsel,
  352. struct perf_sample *sample)
  353. {
  354. int id = perf_evsel__intval(evsel, sample, "id");
  355. if (id < 0) {
  356. /*
  357. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  358. * before that, leaving at a higher verbosity level till that is
  359. * explained. Reproduced with plain ftrace with:
  360. *
  361. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  362. * grep "NR -1 " /t/trace_pipe
  363. *
  364. * After generating some load on the machine.
  365. */
  366. if (verbose > 1) {
  367. static u64 n;
  368. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  369. id, perf_evsel__name(evsel), ++n);
  370. }
  371. return NULL;
  372. }
  373. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  374. trace__read_syscall_info(trace, id))
  375. goto out_cant_read;
  376. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  377. goto out_cant_read;
  378. return &trace->syscalls.table[id];
  379. out_cant_read:
  380. if (verbose) {
  381. fprintf(trace->output, "Problems reading syscall %d", id);
  382. if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
  383. fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
  384. fputs(" information\n", trace->output);
  385. }
  386. return NULL;
  387. }
  388. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  389. struct perf_sample *sample)
  390. {
  391. char *msg;
  392. void *args;
  393. size_t printed = 0;
  394. struct thread *thread;
  395. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  396. struct thread_trace *ttrace;
  397. if (sc == NULL)
  398. return -1;
  399. if (sc->filtered)
  400. return 0;
  401. thread = machine__findnew_thread(&trace->host, sample->tid);
  402. ttrace = thread__trace(thread, trace->output);
  403. if (ttrace == NULL)
  404. return -1;
  405. args = perf_evsel__rawptr(evsel, sample, "args");
  406. if (args == NULL) {
  407. fprintf(trace->output, "Problems reading syscall arguments\n");
  408. return -1;
  409. }
  410. ttrace = thread->priv;
  411. if (ttrace->entry_str == NULL) {
  412. ttrace->entry_str = malloc(1024);
  413. if (!ttrace->entry_str)
  414. return -1;
  415. }
  416. ttrace->entry_time = sample->time;
  417. msg = ttrace->entry_str;
  418. printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
  419. printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
  420. if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
  421. if (!trace->duration_filter) {
  422. trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
  423. fprintf(trace->output, "%-70s\n", ttrace->entry_str);
  424. }
  425. } else
  426. ttrace->entry_pending = true;
  427. return 0;
  428. }
  429. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  430. struct perf_sample *sample)
  431. {
  432. int ret;
  433. u64 duration = 0;
  434. struct thread *thread;
  435. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  436. struct thread_trace *ttrace;
  437. if (sc == NULL)
  438. return -1;
  439. if (sc->filtered)
  440. return 0;
  441. thread = machine__findnew_thread(&trace->host, sample->tid);
  442. ttrace = thread__trace(thread, trace->output);
  443. if (ttrace == NULL)
  444. return -1;
  445. ret = perf_evsel__intval(evsel, sample, "ret");
  446. ttrace = thread->priv;
  447. ttrace->exit_time = sample->time;
  448. if (ttrace->entry_time) {
  449. duration = sample->time - ttrace->entry_time;
  450. if (trace__filter_duration(trace, duration))
  451. goto out;
  452. } else if (trace->duration_filter)
  453. goto out;
  454. trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
  455. if (ttrace->entry_pending) {
  456. fprintf(trace->output, "%-70s", ttrace->entry_str);
  457. } else {
  458. fprintf(trace->output, " ... [");
  459. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  460. fprintf(trace->output, "]: %s()", sc->name);
  461. }
  462. if (sc->fmt == NULL) {
  463. signed_print:
  464. fprintf(trace->output, ") = %d", ret);
  465. } else if (ret < 0 && sc->fmt->errmsg) {
  466. char bf[256];
  467. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  468. *e = audit_errno_to_name(-ret);
  469. fprintf(trace->output, ") = -1 %s %s", e, emsg);
  470. } else if (ret == 0 && sc->fmt->timeout)
  471. fprintf(trace->output, ") = 0 Timeout");
  472. else if (sc->fmt->hexret)
  473. fprintf(trace->output, ") = %#x", ret);
  474. else
  475. goto signed_print;
  476. fputc('\n', trace->output);
  477. out:
  478. ttrace->entry_pending = false;
  479. return 0;
  480. }
  481. static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
  482. struct perf_sample *sample)
  483. {
  484. u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
  485. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  486. struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
  487. struct thread_trace *ttrace = thread__trace(thread, trace->output);
  488. if (ttrace == NULL)
  489. goto out_dump;
  490. ttrace->runtime_ms += runtime_ms;
  491. trace->runtime_ms += runtime_ms;
  492. return 0;
  493. out_dump:
  494. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  495. evsel->name,
  496. perf_evsel__strval(evsel, sample, "comm"),
  497. (pid_t)perf_evsel__intval(evsel, sample, "pid"),
  498. runtime,
  499. perf_evsel__intval(evsel, sample, "vruntime"));
  500. return 0;
  501. }
  502. static int trace__run(struct trace *trace, int argc, const char **argv)
  503. {
  504. struct perf_evlist *evlist = perf_evlist__new();
  505. struct perf_evsel *evsel;
  506. int err = -1, i;
  507. unsigned long before;
  508. const bool forks = argc > 0;
  509. if (evlist == NULL) {
  510. fprintf(trace->output, "Not enough memory to run!\n");
  511. goto out;
  512. }
  513. if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
  514. perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
  515. fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
  516. goto out_delete_evlist;
  517. }
  518. if (trace->sched &&
  519. perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
  520. trace__sched_stat_runtime)) {
  521. fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
  522. goto out_delete_evlist;
  523. }
  524. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  525. if (err < 0) {
  526. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  527. goto out_delete_evlist;
  528. }
  529. err = trace__symbols_init(trace, evlist);
  530. if (err < 0) {
  531. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  532. goto out_delete_maps;
  533. }
  534. perf_evlist__config(evlist, &trace->opts);
  535. signal(SIGCHLD, sig_handler);
  536. signal(SIGINT, sig_handler);
  537. if (forks) {
  538. err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
  539. argv, false, false);
  540. if (err < 0) {
  541. fprintf(trace->output, "Couldn't run the workload!\n");
  542. goto out_delete_maps;
  543. }
  544. }
  545. err = perf_evlist__open(evlist);
  546. if (err < 0) {
  547. fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
  548. goto out_delete_maps;
  549. }
  550. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  551. if (err < 0) {
  552. fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
  553. goto out_close_evlist;
  554. }
  555. perf_evlist__enable(evlist);
  556. if (forks)
  557. perf_evlist__start_workload(evlist);
  558. trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
  559. again:
  560. before = trace->nr_events;
  561. for (i = 0; i < evlist->nr_mmaps; i++) {
  562. union perf_event *event;
  563. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  564. const u32 type = event->header.type;
  565. tracepoint_handler handler;
  566. struct perf_sample sample;
  567. ++trace->nr_events;
  568. err = perf_evlist__parse_sample(evlist, event, &sample);
  569. if (err) {
  570. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  571. continue;
  572. }
  573. if (trace->base_time == 0)
  574. trace->base_time = sample.time;
  575. if (type != PERF_RECORD_SAMPLE) {
  576. trace__process_event(trace, &trace->host, event);
  577. continue;
  578. }
  579. evsel = perf_evlist__id2evsel(evlist, sample.id);
  580. if (evsel == NULL) {
  581. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
  582. continue;
  583. }
  584. if (sample.raw_data == NULL) {
  585. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  586. perf_evsel__name(evsel), sample.tid,
  587. sample.cpu, sample.raw_size);
  588. continue;
  589. }
  590. handler = evsel->handler.func;
  591. handler(trace, evsel, &sample);
  592. }
  593. }
  594. if (trace->nr_events == before) {
  595. if (done)
  596. goto out_unmap_evlist;
  597. poll(evlist->pollfd, evlist->nr_fds, -1);
  598. }
  599. if (done)
  600. perf_evlist__disable(evlist);
  601. goto again;
  602. out_unmap_evlist:
  603. perf_evlist__munmap(evlist);
  604. out_close_evlist:
  605. perf_evlist__close(evlist);
  606. out_delete_maps:
  607. perf_evlist__delete_maps(evlist);
  608. out_delete_evlist:
  609. perf_evlist__delete(evlist);
  610. out:
  611. return err;
  612. }
  613. static size_t trace__fprintf_threads_header(FILE *fp)
  614. {
  615. size_t printed;
  616. printed = fprintf(fp, "\n _____________________________________________________________________\n");
  617. printed += fprintf(fp," __) Summary of events (__\n\n");
  618. printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
  619. printed += fprintf(fp," _____________________________________________________________________\n\n");
  620. return printed;
  621. }
  622. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  623. {
  624. size_t printed = trace__fprintf_threads_header(fp);
  625. struct rb_node *nd;
  626. for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
  627. struct thread *thread = rb_entry(nd, struct thread, rb_node);
  628. struct thread_trace *ttrace = thread->priv;
  629. const char *color;
  630. double ratio;
  631. if (ttrace == NULL)
  632. continue;
  633. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  634. color = PERF_COLOR_NORMAL;
  635. if (ratio > 50.0)
  636. color = PERF_COLOR_RED;
  637. else if (ratio > 25.0)
  638. color = PERF_COLOR_GREEN;
  639. else if (ratio > 5.0)
  640. color = PERF_COLOR_YELLOW;
  641. printed += color_fprintf(fp, color, "%20s", thread->comm);
  642. printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
  643. printed += color_fprintf(fp, color, "%5.1f%%", ratio);
  644. printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
  645. }
  646. return printed;
  647. }
  648. static int trace__set_duration(const struct option *opt, const char *str,
  649. int unset __maybe_unused)
  650. {
  651. struct trace *trace = opt->value;
  652. trace->duration_filter = atof(str);
  653. return 0;
  654. }
  655. static int trace__open_output(struct trace *trace, const char *filename)
  656. {
  657. struct stat st;
  658. if (!stat(filename, &st) && st.st_size) {
  659. char oldname[PATH_MAX];
  660. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  661. unlink(oldname);
  662. rename(filename, oldname);
  663. }
  664. trace->output = fopen(filename, "w");
  665. return trace->output == NULL ? -errno : 0;
  666. }
  667. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  668. {
  669. const char * const trace_usage[] = {
  670. "perf trace [<options>] [<command>]",
  671. "perf trace [<options>] -- <command> [<options>]",
  672. NULL
  673. };
  674. struct trace trace = {
  675. .audit_machine = audit_detect_machine(),
  676. .syscalls = {
  677. . max = -1,
  678. },
  679. .opts = {
  680. .target = {
  681. .uid = UINT_MAX,
  682. .uses_mmap = true,
  683. },
  684. .user_freq = UINT_MAX,
  685. .user_interval = ULLONG_MAX,
  686. .no_delay = true,
  687. .mmap_pages = 1024,
  688. },
  689. .output = stdout,
  690. };
  691. const char *output_name = NULL;
  692. const char *ev_qualifier_str = NULL;
  693. const struct option trace_options[] = {
  694. OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
  695. "list of events to trace"),
  696. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  697. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  698. "trace events on existing process id"),
  699. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  700. "trace events on existing thread id"),
  701. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  702. "system-wide collection from all CPUs"),
  703. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  704. "list of cpus to monitor"),
  705. OPT_BOOLEAN('i', "no-inherit", &trace.opts.no_inherit,
  706. "child tasks do not inherit counters"),
  707. OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
  708. "number of mmap data pages"),
  709. OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
  710. "user to profile"),
  711. OPT_CALLBACK(0, "duration", &trace, "float",
  712. "show only events with duration > N.M ms",
  713. trace__set_duration),
  714. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  715. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  716. OPT_END()
  717. };
  718. int err;
  719. char bf[BUFSIZ];
  720. argc = parse_options(argc, argv, trace_options, trace_usage, 0);
  721. if (output_name != NULL) {
  722. err = trace__open_output(&trace, output_name);
  723. if (err < 0) {
  724. perror("failed to create output file");
  725. goto out;
  726. }
  727. }
  728. if (ev_qualifier_str != NULL) {
  729. const char *s = ev_qualifier_str;
  730. trace.not_ev_qualifier = *s == '!';
  731. if (trace.not_ev_qualifier)
  732. ++s;
  733. trace.ev_qualifier = strlist__new(true, s);
  734. if (trace.ev_qualifier == NULL) {
  735. fputs("Not enough memory to parse event qualifier",
  736. trace.output);
  737. err = -ENOMEM;
  738. goto out_close;
  739. }
  740. }
  741. err = perf_target__validate(&trace.opts.target);
  742. if (err) {
  743. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  744. fprintf(trace.output, "%s", bf);
  745. goto out_close;
  746. }
  747. err = perf_target__parse_uid(&trace.opts.target);
  748. if (err) {
  749. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  750. fprintf(trace.output, "%s", bf);
  751. goto out_close;
  752. }
  753. if (!argc && perf_target__none(&trace.opts.target))
  754. trace.opts.target.system_wide = true;
  755. err = trace__run(&trace, argc, argv);
  756. if (trace.sched && !err)
  757. trace__fprintf_thread_summary(&trace, trace.output);
  758. out_close:
  759. if (output_name != NULL)
  760. fclose(trace.output);
  761. out:
  762. return err;
  763. }