builtin-trace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. #include "builtin.h"
  2. #include "util/color.h"
  3. #include "util/evlist.h"
  4. #include "util/machine.h"
  5. #include "util/thread.h"
  6. #include "util/parse-options.h"
  7. #include "util/thread_map.h"
  8. #include "event-parse.h"
  9. #include <libaudit.h>
  10. #include <stdlib.h>
  11. static struct syscall_fmt {
  12. const char *name;
  13. const char *alias;
  14. bool errmsg;
  15. bool timeout;
  16. } syscall_fmts[] = {
  17. { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
  18. { .name = "fstat", .errmsg = true, .alias = "newfstat", },
  19. { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
  20. { .name = "futex", .errmsg = true, },
  21. { .name = "poll", .errmsg = true, .timeout = true, },
  22. { .name = "ppoll", .errmsg = true, .timeout = true, },
  23. { .name = "read", .errmsg = true, },
  24. { .name = "recvfrom", .errmsg = true, },
  25. { .name = "select", .errmsg = true, .timeout = true, },
  26. { .name = "stat", .errmsg = true, .alias = "newstat", },
  27. };
  28. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  29. {
  30. const struct syscall_fmt *fmt = fmtp;
  31. return strcmp(name, fmt->name);
  32. }
  33. static struct syscall_fmt *syscall_fmt__find(const char *name)
  34. {
  35. const int nmemb = ARRAY_SIZE(syscall_fmts);
  36. return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  37. }
  38. struct syscall {
  39. struct event_format *tp_format;
  40. const char *name;
  41. struct syscall_fmt *fmt;
  42. };
  43. static size_t fprintf_duration(unsigned long t, FILE *fp)
  44. {
  45. double duration = (double)t / NSEC_PER_MSEC;
  46. size_t printed = fprintf(fp, "(");
  47. if (duration >= 1.0)
  48. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  49. else if (duration >= 0.01)
  50. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  51. else
  52. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  53. return printed + fprintf(stdout, "): ");
  54. }
  55. struct thread_trace {
  56. u64 entry_time;
  57. u64 exit_time;
  58. bool entry_pending;
  59. char *entry_str;
  60. };
  61. static struct thread_trace *thread_trace__new(void)
  62. {
  63. return zalloc(sizeof(struct thread_trace));
  64. }
  65. static struct thread_trace *thread__trace(struct thread *thread)
  66. {
  67. if (thread == NULL)
  68. goto fail;
  69. if (thread->priv == NULL)
  70. thread->priv = thread_trace__new();
  71. if (thread->priv == NULL)
  72. goto fail;
  73. return thread->priv;
  74. fail:
  75. color_fprintf(stdout, PERF_COLOR_RED,
  76. "WARNING: not enough memory, dropping samples!\n");
  77. return NULL;
  78. }
  79. struct trace {
  80. int audit_machine;
  81. struct {
  82. int max;
  83. struct syscall *table;
  84. } syscalls;
  85. struct perf_record_opts opts;
  86. struct machine host;
  87. u64 base_time;
  88. bool multiple_threads;
  89. double duration_filter;
  90. };
  91. static bool trace__filter_duration(struct trace *trace, double t)
  92. {
  93. return t < (trace->duration_filter * NSEC_PER_MSEC);
  94. }
  95. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  96. {
  97. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  98. return fprintf(fp, "%10.3f ", ts);
  99. }
  100. static bool done = false;
  101. static void sig_handler(int sig __maybe_unused)
  102. {
  103. done = true;
  104. }
  105. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  106. u64 duration, u64 tstamp, FILE *fp)
  107. {
  108. size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
  109. printed += fprintf_duration(duration, fp);
  110. if (trace->multiple_threads)
  111. printed += fprintf(fp, "%d ", thread->pid);
  112. return printed;
  113. }
  114. static int trace__process_event(struct machine *machine, union perf_event *event)
  115. {
  116. int ret = 0;
  117. switch (event->header.type) {
  118. case PERF_RECORD_LOST:
  119. color_fprintf(stdout, PERF_COLOR_RED,
  120. "LOST %" PRIu64 " events!\n", event->lost.lost);
  121. ret = machine__process_lost_event(machine, event);
  122. default:
  123. ret = machine__process_event(machine, event);
  124. break;
  125. }
  126. return ret;
  127. }
  128. static int trace__tool_process(struct perf_tool *tool __maybe_unused,
  129. union perf_event *event,
  130. struct perf_sample *sample __maybe_unused,
  131. struct machine *machine)
  132. {
  133. return trace__process_event(machine, event);
  134. }
  135. static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
  136. {
  137. int err = symbol__init();
  138. if (err)
  139. return err;
  140. machine__init(&trace->host, "", HOST_KERNEL_ID);
  141. machine__create_kernel_maps(&trace->host);
  142. if (perf_target__has_task(&trace->opts.target)) {
  143. err = perf_event__synthesize_thread_map(NULL, evlist->threads,
  144. trace__tool_process,
  145. &trace->host);
  146. } else {
  147. err = perf_event__synthesize_threads(NULL, trace__tool_process,
  148. &trace->host);
  149. }
  150. if (err)
  151. symbol__exit();
  152. return err;
  153. }
  154. static int trace__read_syscall_info(struct trace *trace, int id)
  155. {
  156. char tp_name[128];
  157. struct syscall *sc;
  158. const char *name = audit_syscall_to_name(id, trace->audit_machine);
  159. if (name == NULL)
  160. return -1;
  161. if (id > trace->syscalls.max) {
  162. struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
  163. if (nsyscalls == NULL)
  164. return -1;
  165. if (trace->syscalls.max != -1) {
  166. memset(nsyscalls + trace->syscalls.max + 1, 0,
  167. (id - trace->syscalls.max) * sizeof(*sc));
  168. } else {
  169. memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
  170. }
  171. trace->syscalls.table = nsyscalls;
  172. trace->syscalls.max = id;
  173. }
  174. sc = trace->syscalls.table + id;
  175. sc->name = name;
  176. sc->fmt = syscall_fmt__find(sc->name);
  177. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  178. sc->tp_format = event_format__new("syscalls", tp_name);
  179. if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
  180. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  181. sc->tp_format = event_format__new("syscalls", tp_name);
  182. }
  183. return sc->tp_format != NULL ? 0 : -1;
  184. }
  185. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  186. unsigned long *args)
  187. {
  188. int i = 0;
  189. size_t printed = 0;
  190. if (sc->tp_format != NULL) {
  191. struct format_field *field;
  192. for (field = sc->tp_format->format.fields->next; field; field = field->next) {
  193. printed += scnprintf(bf + printed, size - printed,
  194. "%s%s: %ld", printed ? ", " : "",
  195. field->name, args[i++]);
  196. }
  197. } else {
  198. while (i < 6) {
  199. printed += scnprintf(bf + printed, size - printed,
  200. "%sarg%d: %ld",
  201. printed ? ", " : "", i, args[i]);
  202. ++i;
  203. }
  204. }
  205. return printed;
  206. }
  207. typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
  208. struct perf_sample *sample);
  209. static struct syscall *trace__syscall_info(struct trace *trace,
  210. struct perf_evsel *evsel,
  211. struct perf_sample *sample)
  212. {
  213. int id = perf_evsel__intval(evsel, sample, "id");
  214. if (id < 0) {
  215. printf("Invalid syscall %d id, skipping...\n", id);
  216. return NULL;
  217. }
  218. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
  219. trace__read_syscall_info(trace, id))
  220. goto out_cant_read;
  221. if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
  222. goto out_cant_read;
  223. return &trace->syscalls.table[id];
  224. out_cant_read:
  225. printf("Problems reading syscall %d information\n", id);
  226. return NULL;
  227. }
  228. static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
  229. struct perf_sample *sample)
  230. {
  231. char *msg;
  232. void *args;
  233. size_t printed = 0;
  234. struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
  235. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  236. struct thread_trace *ttrace = thread__trace(thread);
  237. if (ttrace == NULL || sc == NULL)
  238. return -1;
  239. args = perf_evsel__rawptr(evsel, sample, "args");
  240. if (args == NULL) {
  241. printf("Problems reading syscall arguments\n");
  242. return -1;
  243. }
  244. ttrace = thread->priv;
  245. if (ttrace->entry_str == NULL) {
  246. ttrace->entry_str = malloc(1024);
  247. if (!ttrace->entry_str)
  248. return -1;
  249. }
  250. ttrace->entry_time = sample->time;
  251. msg = ttrace->entry_str;
  252. printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
  253. printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
  254. if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
  255. if (!trace->duration_filter) {
  256. trace__fprintf_entry_head(trace, thread, 1, sample->time, stdout);
  257. printf("%-70s\n", ttrace->entry_str);
  258. }
  259. } else
  260. ttrace->entry_pending = true;
  261. return 0;
  262. }
  263. static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
  264. struct perf_sample *sample)
  265. {
  266. int ret;
  267. u64 duration = 0;
  268. struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
  269. struct thread_trace *ttrace = thread__trace(thread);
  270. struct syscall *sc = trace__syscall_info(trace, evsel, sample);
  271. if (ttrace == NULL || sc == NULL)
  272. return -1;
  273. ret = perf_evsel__intval(evsel, sample, "ret");
  274. ttrace = thread->priv;
  275. ttrace->exit_time = sample->time;
  276. if (ttrace->entry_time) {
  277. duration = sample->time - ttrace->entry_time;
  278. if (trace__filter_duration(trace, duration))
  279. goto out;
  280. } else if (trace->duration_filter)
  281. goto out;
  282. trace__fprintf_entry_head(trace, thread, duration, sample->time, stdout);
  283. if (ttrace->entry_pending) {
  284. printf("%-70s", ttrace->entry_str);
  285. } else {
  286. printf(" ... [");
  287. color_fprintf(stdout, PERF_COLOR_YELLOW, "continued");
  288. printf("]: %s()", sc->name);
  289. }
  290. if (ret < 0 && sc->fmt && sc->fmt->errmsg) {
  291. char bf[256];
  292. const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
  293. *e = audit_errno_to_name(-ret);
  294. printf(") = -1 %s %s", e, emsg);
  295. } else if (ret == 0 && sc->fmt && sc->fmt->timeout)
  296. printf(") = 0 Timeout");
  297. else
  298. printf(") = %d", ret);
  299. putchar('\n');
  300. out:
  301. ttrace->entry_pending = false;
  302. return 0;
  303. }
  304. static int trace__run(struct trace *trace, int argc, const char **argv)
  305. {
  306. struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
  307. struct perf_evsel *evsel;
  308. int err = -1, i, nr_events = 0, before;
  309. const bool forks = argc > 0;
  310. if (evlist == NULL) {
  311. printf("Not enough memory to run!\n");
  312. goto out;
  313. }
  314. if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
  315. perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
  316. printf("Couldn't read the raw_syscalls tracepoints information!\n");
  317. goto out_delete_evlist;
  318. }
  319. err = perf_evlist__create_maps(evlist, &trace->opts.target);
  320. if (err < 0) {
  321. printf("Problems parsing the target to trace, check your options!\n");
  322. goto out_delete_evlist;
  323. }
  324. err = trace__symbols_init(trace, evlist);
  325. if (err < 0) {
  326. printf("Problems initializing symbol libraries!\n");
  327. goto out_delete_evlist;
  328. }
  329. perf_evlist__config_attrs(evlist, &trace->opts);
  330. signal(SIGCHLD, sig_handler);
  331. signal(SIGINT, sig_handler);
  332. if (forks) {
  333. err = perf_evlist__prepare_workload(evlist, &trace->opts, argv);
  334. if (err < 0) {
  335. printf("Couldn't run the workload!\n");
  336. goto out_delete_evlist;
  337. }
  338. }
  339. err = perf_evlist__open(evlist);
  340. if (err < 0) {
  341. printf("Couldn't create the events: %s\n", strerror(errno));
  342. goto out_delete_evlist;
  343. }
  344. err = perf_evlist__mmap(evlist, UINT_MAX, false);
  345. if (err < 0) {
  346. printf("Couldn't mmap the events: %s\n", strerror(errno));
  347. goto out_delete_evlist;
  348. }
  349. perf_evlist__enable(evlist);
  350. if (forks)
  351. perf_evlist__start_workload(evlist);
  352. trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
  353. again:
  354. before = nr_events;
  355. for (i = 0; i < evlist->nr_mmaps; i++) {
  356. union perf_event *event;
  357. while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  358. const u32 type = event->header.type;
  359. tracepoint_handler handler;
  360. struct perf_sample sample;
  361. ++nr_events;
  362. err = perf_evlist__parse_sample(evlist, event, &sample);
  363. if (err) {
  364. printf("Can't parse sample, err = %d, skipping...\n", err);
  365. continue;
  366. }
  367. if (trace->base_time == 0)
  368. trace->base_time = sample.time;
  369. if (type != PERF_RECORD_SAMPLE) {
  370. trace__process_event(&trace->host, event);
  371. continue;
  372. }
  373. evsel = perf_evlist__id2evsel(evlist, sample.id);
  374. if (evsel == NULL) {
  375. printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
  376. continue;
  377. }
  378. if (sample.raw_data == NULL) {
  379. printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  380. perf_evsel__name(evsel), sample.tid,
  381. sample.cpu, sample.raw_size);
  382. continue;
  383. }
  384. if (sample.raw_data == NULL) {
  385. printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  386. perf_evsel__name(evsel), sample.tid,
  387. sample.cpu, sample.raw_size);
  388. continue;
  389. }
  390. handler = evsel->handler.func;
  391. handler(trace, evsel, &sample);
  392. }
  393. }
  394. if (nr_events == before) {
  395. if (done)
  396. goto out_delete_evlist;
  397. poll(evlist->pollfd, evlist->nr_fds, -1);
  398. }
  399. if (done)
  400. perf_evlist__disable(evlist);
  401. goto again;
  402. out_delete_evlist:
  403. perf_evlist__delete(evlist);
  404. out:
  405. return err;
  406. }
  407. static int trace__set_duration(const struct option *opt, const char *str,
  408. int unset __maybe_unused)
  409. {
  410. struct trace *trace = opt->value;
  411. trace->duration_filter = atof(str);
  412. return 0;
  413. }
  414. int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
  415. {
  416. const char * const trace_usage[] = {
  417. "perf trace [<options>] [<command>]",
  418. "perf trace [<options>] -- <command> [<options>]",
  419. NULL
  420. };
  421. struct trace trace = {
  422. .audit_machine = audit_detect_machine(),
  423. .syscalls = {
  424. . max = -1,
  425. },
  426. .opts = {
  427. .target = {
  428. .uid = UINT_MAX,
  429. .uses_mmap = true,
  430. },
  431. .user_freq = UINT_MAX,
  432. .user_interval = ULLONG_MAX,
  433. .no_delay = true,
  434. .mmap_pages = 1024,
  435. },
  436. };
  437. const struct option trace_options[] = {
  438. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  439. "trace events on existing process id"),
  440. OPT_STRING(0, "tid", &trace.opts.target.tid, "tid",
  441. "trace events on existing thread id"),
  442. OPT_BOOLEAN(0, "all-cpus", &trace.opts.target.system_wide,
  443. "system-wide collection from all CPUs"),
  444. OPT_STRING(0, "cpu", &trace.opts.target.cpu_list, "cpu",
  445. "list of cpus to monitor"),
  446. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  447. "child tasks do not inherit counters"),
  448. OPT_UINTEGER(0, "mmap-pages", &trace.opts.mmap_pages,
  449. "number of mmap data pages"),
  450. OPT_STRING(0, "uid", &trace.opts.target.uid_str, "user",
  451. "user to profile"),
  452. OPT_CALLBACK(0, "duration", &trace, "float",
  453. "show only events with duration > N.M ms",
  454. trace__set_duration),
  455. OPT_END()
  456. };
  457. int err;
  458. char bf[BUFSIZ];
  459. argc = parse_options(argc, argv, trace_options, trace_usage, 0);
  460. err = perf_target__validate(&trace.opts.target);
  461. if (err) {
  462. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  463. printf("%s", bf);
  464. return err;
  465. }
  466. err = perf_target__parse_uid(&trace.opts.target);
  467. if (err) {
  468. perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  469. printf("%s", bf);
  470. return err;
  471. }
  472. if (!argc && perf_target__none(&trace.opts.target))
  473. trace.opts.target.system_wide = true;
  474. return trace__run(&trace, argc, argv);
  475. }