builtin-trace.c 20 KB

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