1 #include <traceevent/event-parse.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"
17 #include <linux/futex.h>
19 /* For older distros: */
21 # define MAP_STACK 0x20000
25 # define MADV_HWPOISON 100
28 #ifndef MADV_MERGEABLE
29 # define MADV_MERGEABLE 12
32 #ifndef MADV_UNMERGEABLE
33 # define MADV_UNMERGEABLE 13
48 #define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \
49 .nr_entries = ARRAY_SIZE(array), \
53 static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
54 struct syscall_arg *arg)
57 struct strarray *sa = arg->parm;
59 if (idx < 0 || idx >= sa->nr_entries)
60 return scnprintf(bf, size, "%d", idx);
62 return scnprintf(bf, size, "%s", sa->entries[idx]);
65 #define SCA_STRARRAY syscall_arg__scnprintf_strarray
67 static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
68 struct syscall_arg *arg)
70 return scnprintf(bf, size, "%#lx", arg->val);
73 #define SCA_HEX syscall_arg__scnprintf_hex
75 static size_t syscall_arg__scnprintf_whence(char *bf, size_t size,
76 struct syscall_arg *arg)
78 int whence = arg->val;
81 #define P_WHENCE(n) case SEEK_##n: return scnprintf(bf, size, #n)
95 return scnprintf(bf, size, "%#x", whence);
98 #define SCA_WHENCE syscall_arg__scnprintf_whence
100 static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
101 struct syscall_arg *arg)
103 int printed = 0, prot = arg->val;
105 if (prot == PROT_NONE)
106 return scnprintf(bf, size, "NONE");
107 #define P_MMAP_PROT(n) \
108 if (prot & PROT_##n) { \
109 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
119 P_MMAP_PROT(GROWSDOWN);
120 P_MMAP_PROT(GROWSUP);
124 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
129 #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
131 static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
132 struct syscall_arg *arg)
134 int printed = 0, flags = arg->val;
136 #define P_MMAP_FLAG(n) \
137 if (flags & MAP_##n) { \
138 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
143 P_MMAP_FLAG(PRIVATE);
147 P_MMAP_FLAG(ANONYMOUS);
148 P_MMAP_FLAG(DENYWRITE);
149 P_MMAP_FLAG(EXECUTABLE);
152 P_MMAP_FLAG(GROWSDOWN);
154 P_MMAP_FLAG(HUGETLB);
157 P_MMAP_FLAG(NONBLOCK);
158 P_MMAP_FLAG(NORESERVE);
159 P_MMAP_FLAG(POPULATE);
161 #ifdef MAP_UNINITIALIZED
162 P_MMAP_FLAG(UNINITIALIZED);
167 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
172 #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
174 static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
175 struct syscall_arg *arg)
177 int behavior = arg->val;
180 #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
183 P_MADV_BHV(SEQUENTIAL);
184 P_MADV_BHV(WILLNEED);
185 P_MADV_BHV(DONTNEED);
187 P_MADV_BHV(DONTFORK);
189 P_MADV_BHV(HWPOISON);
190 #ifdef MADV_SOFT_OFFLINE
191 P_MADV_BHV(SOFT_OFFLINE);
193 P_MADV_BHV(MERGEABLE);
194 P_MADV_BHV(UNMERGEABLE);
196 P_MADV_BHV(HUGEPAGE);
198 #ifdef MADV_NOHUGEPAGE
199 P_MADV_BHV(NOHUGEPAGE);
202 P_MADV_BHV(DONTDUMP);
211 return scnprintf(bf, size, "%#x", behavior);
214 #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
216 static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, struct syscall_arg *arg)
218 enum syscall_futex_args {
219 SCF_UADDR = (1 << 0),
222 SCF_TIMEOUT = (1 << 3),
223 SCF_UADDR2 = (1 << 4),
227 int cmd = op & FUTEX_CMD_MASK;
231 #define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
232 P_FUTEX_OP(WAIT); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
233 P_FUTEX_OP(WAKE); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
234 P_FUTEX_OP(FD); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
235 P_FUTEX_OP(REQUEUE); arg->mask |= SCF_VAL3|SCF_TIMEOUT; break;
236 P_FUTEX_OP(CMP_REQUEUE); arg->mask |= SCF_TIMEOUT; break;
237 P_FUTEX_OP(CMP_REQUEUE_PI); arg->mask |= SCF_TIMEOUT; break;
238 P_FUTEX_OP(WAKE_OP); break;
239 P_FUTEX_OP(LOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
240 P_FUTEX_OP(UNLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
241 P_FUTEX_OP(TRYLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
242 P_FUTEX_OP(WAIT_BITSET); arg->mask |= SCF_UADDR2; break;
243 P_FUTEX_OP(WAKE_BITSET); arg->mask |= SCF_UADDR2; break;
244 P_FUTEX_OP(WAIT_REQUEUE_PI); break;
245 default: printed = scnprintf(bf, size, "%#x", cmd); break;
248 if (op & FUTEX_PRIVATE_FLAG)
249 printed += scnprintf(bf + printed, size - printed, "|PRIV");
251 if (op & FUTEX_CLOCK_REALTIME)
252 printed += scnprintf(bf + printed, size - printed, "|CLKRT");
257 static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
258 static DEFINE_STRARRAY(itimers);
260 #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
262 static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
263 struct syscall_arg *arg)
265 int printed = 0, flags = arg->val;
267 if (!(flags & O_CREAT))
268 arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
271 return scnprintf(bf, size, "RDONLY");
273 if (flags & O_##n) { \
274 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
298 if ((flags & O_SYNC) == O_SYNC)
299 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
311 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
316 #define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
318 static struct syscall_fmt {
321 size_t (*arg_scnprintf[6])(char *bf, size_t size, struct syscall_arg *arg);
327 { .name = "access", .errmsg = true, },
328 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
329 { .name = "brk", .hexret = true,
330 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
331 { .name = "mmap", .hexret = true, },
332 { .name = "connect", .errmsg = true, },
333 { .name = "fstat", .errmsg = true, .alias = "newfstat", },
334 { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
335 { .name = "futex", .errmsg = true,
336 .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
337 { .name = "getitimer", .errmsg = true,
338 .arg_scnprintf = { [0] = SCA_STRARRAY, /* which */ },
339 .arg_parm = { [0] = &strarray__itimers, /* which */ }, },
340 { .name = "ioctl", .errmsg = true,
341 .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
342 { .name = "lseek", .errmsg = true,
343 .arg_scnprintf = { [2] = SCA_WHENCE, /* whence */ }, },
344 { .name = "lstat", .errmsg = true, .alias = "newlstat", },
345 { .name = "madvise", .errmsg = true,
346 .arg_scnprintf = { [0] = SCA_HEX, /* start */
347 [2] = SCA_MADV_BHV, /* behavior */ }, },
348 { .name = "mmap", .hexret = true,
349 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
350 [2] = SCA_MMAP_PROT, /* prot */
351 [3] = SCA_MMAP_FLAGS, /* flags */ }, },
352 { .name = "mprotect", .errmsg = true,
353 .arg_scnprintf = { [0] = SCA_HEX, /* start */
354 [2] = SCA_MMAP_PROT, /* prot */ }, },
355 { .name = "mremap", .hexret = true,
356 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
357 [4] = SCA_HEX, /* new_addr */ }, },
358 { .name = "munmap", .errmsg = true,
359 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
360 { .name = "open", .errmsg = true,
361 .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
362 { .name = "open_by_handle_at", .errmsg = true,
363 .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
364 { .name = "openat", .errmsg = true,
365 .arg_scnprintf = { [2] = SCA_OPEN_FLAGS, /* flags */ }, },
366 { .name = "poll", .errmsg = true, .timeout = true, },
367 { .name = "ppoll", .errmsg = true, .timeout = true, },
368 { .name = "pread", .errmsg = true, .alias = "pread64", },
369 { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
370 { .name = "read", .errmsg = true, },
371 { .name = "recvfrom", .errmsg = true, },
372 { .name = "select", .errmsg = true, .timeout = true, },
373 { .name = "setitimer", .errmsg = true,
374 .arg_scnprintf = { [0] = SCA_STRARRAY, /* which */ },
375 .arg_parm = { [0] = &strarray__itimers, /* which */ }, },
376 { .name = "socket", .errmsg = true, },
377 { .name = "stat", .errmsg = true, .alias = "newstat", },
378 { .name = "uname", .errmsg = true, .alias = "newuname", },
381 static int syscall_fmt__cmp(const void *name, const void *fmtp)
383 const struct syscall_fmt *fmt = fmtp;
384 return strcmp(name, fmt->name);
387 static struct syscall_fmt *syscall_fmt__find(const char *name)
389 const int nmemb = ARRAY_SIZE(syscall_fmts);
390 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
394 struct event_format *tp_format;
397 struct syscall_fmt *fmt;
398 size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
402 static size_t fprintf_duration(unsigned long t, FILE *fp)
404 double duration = (double)t / NSEC_PER_MSEC;
405 size_t printed = fprintf(fp, "(");
408 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
409 else if (duration >= 0.01)
410 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
412 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
413 return printed + fprintf(fp, "): ");
416 struct thread_trace {
420 unsigned long nr_events;
425 static struct thread_trace *thread_trace__new(void)
427 return zalloc(sizeof(struct thread_trace));
430 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
432 struct thread_trace *ttrace;
437 if (thread->priv == NULL)
438 thread->priv = thread_trace__new();
440 if (thread->priv == NULL)
443 ttrace = thread->priv;
448 color_fprintf(fp, PERF_COLOR_RED,
449 "WARNING: not enough memory, dropping samples!\n");
454 struct perf_tool tool;
458 struct syscall *table;
460 struct perf_record_opts opts;
464 unsigned long nr_events;
465 struct strlist *ev_qualifier;
466 bool not_ev_qualifier;
467 struct intlist *tid_list;
468 struct intlist *pid_list;
470 bool multiple_threads;
471 double duration_filter;
475 static bool trace__filter_duration(struct trace *trace, double t)
477 return t < (trace->duration_filter * NSEC_PER_MSEC);
480 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
482 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
484 return fprintf(fp, "%10.3f ", ts);
487 static bool done = false;
489 static void sig_handler(int sig __maybe_unused)
494 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
495 u64 duration, u64 tstamp, FILE *fp)
497 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
498 printed += fprintf_duration(duration, fp);
500 if (trace->multiple_threads)
501 printed += fprintf(fp, "%d ", thread->tid);
506 static int trace__process_event(struct trace *trace, struct machine *machine,
507 union perf_event *event)
511 switch (event->header.type) {
512 case PERF_RECORD_LOST:
513 color_fprintf(trace->output, PERF_COLOR_RED,
514 "LOST %" PRIu64 " events!\n", event->lost.lost);
515 ret = machine__process_lost_event(machine, event);
517 ret = machine__process_event(machine, event);
524 static int trace__tool_process(struct perf_tool *tool,
525 union perf_event *event,
526 struct perf_sample *sample __maybe_unused,
527 struct machine *machine)
529 struct trace *trace = container_of(tool, struct trace, tool);
530 return trace__process_event(trace, machine, event);
533 static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
535 int err = symbol__init();
540 machine__init(&trace->host, "", HOST_KERNEL_ID);
541 machine__create_kernel_maps(&trace->host);
543 if (perf_target__has_task(&trace->opts.target)) {
544 err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
548 err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
558 static int syscall__set_arg_fmts(struct syscall *sc)
560 struct format_field *field;
563 sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
564 if (sc->arg_scnprintf == NULL)
568 sc->arg_parm = sc->fmt->arg_parm;
570 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
571 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
572 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
573 else if (field->flags & FIELD_IS_POINTER)
574 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
581 static int trace__read_syscall_info(struct trace *trace, int id)
585 const char *name = audit_syscall_to_name(id, trace->audit_machine);
590 if (id > trace->syscalls.max) {
591 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
593 if (nsyscalls == NULL)
596 if (trace->syscalls.max != -1) {
597 memset(nsyscalls + trace->syscalls.max + 1, 0,
598 (id - trace->syscalls.max) * sizeof(*sc));
600 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
603 trace->syscalls.table = nsyscalls;
604 trace->syscalls.max = id;
607 sc = trace->syscalls.table + id;
610 if (trace->ev_qualifier) {
611 bool in = strlist__find(trace->ev_qualifier, name) != NULL;
613 if (!(in ^ trace->not_ev_qualifier)) {
616 * No need to do read tracepoint information since this will be
623 sc->fmt = syscall_fmt__find(sc->name);
625 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
626 sc->tp_format = event_format__new("syscalls", tp_name);
628 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
629 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
630 sc->tp_format = event_format__new("syscalls", tp_name);
633 if (sc->tp_format == NULL)
636 return syscall__set_arg_fmts(sc);
639 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
644 if (sc->tp_format != NULL) {
645 struct format_field *field;
647 struct syscall_arg arg = {
652 for (field = sc->tp_format->format.fields->next; field;
653 field = field->next, ++arg.idx, bit <<= 1) {
657 printed += scnprintf(bf + printed, size - printed,
658 "%s%s: ", printed ? ", " : "", field->name);
659 if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
660 arg.val = args[arg.idx];
662 arg.parm = sc->arg_parm[arg.idx];
663 printed += sc->arg_scnprintf[arg.idx](bf + printed,
664 size - printed, &arg);
666 printed += scnprintf(bf + printed, size - printed,
667 "%ld", args[arg.idx]);
674 printed += scnprintf(bf + printed, size - printed,
676 printed ? ", " : "", i, args[i]);
684 typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
685 struct perf_sample *sample);
687 static struct syscall *trace__syscall_info(struct trace *trace,
688 struct perf_evsel *evsel,
689 struct perf_sample *sample)
691 int id = perf_evsel__intval(evsel, sample, "id");
696 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
697 * before that, leaving at a higher verbosity level till that is
698 * explained. Reproduced with plain ftrace with:
700 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
701 * grep "NR -1 " /t/trace_pipe
703 * After generating some load on the machine.
707 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
708 id, perf_evsel__name(evsel), ++n);
713 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
714 trace__read_syscall_info(trace, id))
717 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
720 return &trace->syscalls.table[id];
724 fprintf(trace->output, "Problems reading syscall %d", id);
725 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
726 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
727 fputs(" information\n", trace->output);
732 static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
733 struct perf_sample *sample)
738 struct thread *thread;
739 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
740 struct thread_trace *ttrace;
748 thread = machine__findnew_thread(&trace->host, sample->pid,
750 ttrace = thread__trace(thread, trace->output);
754 args = perf_evsel__rawptr(evsel, sample, "args");
756 fprintf(trace->output, "Problems reading syscall arguments\n");
760 ttrace = thread->priv;
762 if (ttrace->entry_str == NULL) {
763 ttrace->entry_str = malloc(1024);
764 if (!ttrace->entry_str)
768 ttrace->entry_time = sample->time;
769 msg = ttrace->entry_str;
770 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
772 printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
774 if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
775 if (!trace->duration_filter) {
776 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
777 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
780 ttrace->entry_pending = true;
785 static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
786 struct perf_sample *sample)
790 struct thread *thread;
791 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
792 struct thread_trace *ttrace;
800 thread = machine__findnew_thread(&trace->host, sample->pid,
802 ttrace = thread__trace(thread, trace->output);
806 ret = perf_evsel__intval(evsel, sample, "ret");
808 ttrace = thread->priv;
810 ttrace->exit_time = sample->time;
812 if (ttrace->entry_time) {
813 duration = sample->time - ttrace->entry_time;
814 if (trace__filter_duration(trace, duration))
816 } else if (trace->duration_filter)
819 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
821 if (ttrace->entry_pending) {
822 fprintf(trace->output, "%-70s", ttrace->entry_str);
824 fprintf(trace->output, " ... [");
825 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
826 fprintf(trace->output, "]: %s()", sc->name);
829 if (sc->fmt == NULL) {
831 fprintf(trace->output, ") = %d", ret);
832 } else if (ret < 0 && sc->fmt->errmsg) {
834 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
835 *e = audit_errno_to_name(-ret);
837 fprintf(trace->output, ") = -1 %s %s", e, emsg);
838 } else if (ret == 0 && sc->fmt->timeout)
839 fprintf(trace->output, ") = 0 Timeout");
840 else if (sc->fmt->hexret)
841 fprintf(trace->output, ") = %#x", ret);
845 fputc('\n', trace->output);
847 ttrace->entry_pending = false;
852 static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
853 struct perf_sample *sample)
855 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
856 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
857 struct thread *thread = machine__findnew_thread(&trace->host,
860 struct thread_trace *ttrace = thread__trace(thread, trace->output);
865 ttrace->runtime_ms += runtime_ms;
866 trace->runtime_ms += runtime_ms;
870 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
872 perf_evsel__strval(evsel, sample, "comm"),
873 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
875 perf_evsel__intval(evsel, sample, "vruntime"));
879 static bool skip_sample(struct trace *trace, struct perf_sample *sample)
881 if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
882 (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
885 if (trace->pid_list || trace->tid_list)
891 static int trace__process_sample(struct perf_tool *tool,
892 union perf_event *event __maybe_unused,
893 struct perf_sample *sample,
894 struct perf_evsel *evsel,
895 struct machine *machine __maybe_unused)
897 struct trace *trace = container_of(tool, struct trace, tool);
900 tracepoint_handler handler = evsel->handler.func;
902 if (skip_sample(trace, sample))
905 if (trace->base_time == 0)
906 trace->base_time = sample->time;
909 handler(trace, evsel, sample);
915 perf_session__has_tp(struct perf_session *session, const char *name)
917 struct perf_evsel *evsel;
919 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
921 return evsel != NULL;
924 static int parse_target_str(struct trace *trace)
926 if (trace->opts.target.pid) {
927 trace->pid_list = intlist__new(trace->opts.target.pid);
928 if (trace->pid_list == NULL) {
929 pr_err("Error parsing process id string\n");
934 if (trace->opts.target.tid) {
935 trace->tid_list = intlist__new(trace->opts.target.tid);
936 if (trace->tid_list == NULL) {
937 pr_err("Error parsing thread id string\n");
945 static int trace__run(struct trace *trace, int argc, const char **argv)
947 struct perf_evlist *evlist = perf_evlist__new();
948 struct perf_evsel *evsel;
950 unsigned long before;
951 const bool forks = argc > 0;
953 if (evlist == NULL) {
954 fprintf(trace->output, "Not enough memory to run!\n");
958 if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
959 perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
960 fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
961 goto out_delete_evlist;
965 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
966 trace__sched_stat_runtime)) {
967 fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
968 goto out_delete_evlist;
971 err = perf_evlist__create_maps(evlist, &trace->opts.target);
973 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
974 goto out_delete_evlist;
977 err = trace__symbols_init(trace, evlist);
979 fprintf(trace->output, "Problems initializing symbol libraries!\n");
980 goto out_delete_maps;
983 perf_evlist__config(evlist, &trace->opts);
985 signal(SIGCHLD, sig_handler);
986 signal(SIGINT, sig_handler);
989 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
992 fprintf(trace->output, "Couldn't run the workload!\n");
993 goto out_delete_maps;
997 err = perf_evlist__open(evlist);
999 fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
1000 goto out_delete_maps;
1003 err = perf_evlist__mmap(evlist, UINT_MAX, false);
1005 fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
1006 goto out_close_evlist;
1009 perf_evlist__enable(evlist);
1012 perf_evlist__start_workload(evlist);
1014 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
1016 before = trace->nr_events;
1018 for (i = 0; i < evlist->nr_mmaps; i++) {
1019 union perf_event *event;
1021 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
1022 const u32 type = event->header.type;
1023 tracepoint_handler handler;
1024 struct perf_sample sample;
1028 err = perf_evlist__parse_sample(evlist, event, &sample);
1030 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
1034 if (trace->base_time == 0)
1035 trace->base_time = sample.time;
1037 if (type != PERF_RECORD_SAMPLE) {
1038 trace__process_event(trace, &trace->host, event);
1042 evsel = perf_evlist__id2evsel(evlist, sample.id);
1043 if (evsel == NULL) {
1044 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
1048 if (sample.raw_data == NULL) {
1049 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
1050 perf_evsel__name(evsel), sample.tid,
1051 sample.cpu, sample.raw_size);
1055 handler = evsel->handler.func;
1056 handler(trace, evsel, &sample);
1059 goto out_unmap_evlist;
1063 if (trace->nr_events == before) {
1065 goto out_unmap_evlist;
1067 poll(evlist->pollfd, evlist->nr_fds, -1);
1071 perf_evlist__disable(evlist);
1076 perf_evlist__munmap(evlist);
1078 perf_evlist__close(evlist);
1080 perf_evlist__delete_maps(evlist);
1082 perf_evlist__delete(evlist);
1087 static int trace__replay(struct trace *trace)
1089 const struct perf_evsel_str_handler handlers[] = {
1090 { "raw_syscalls:sys_enter", trace__sys_enter, },
1091 { "raw_syscalls:sys_exit", trace__sys_exit, },
1094 struct perf_session *session;
1097 trace->tool.sample = trace__process_sample;
1098 trace->tool.mmap = perf_event__process_mmap;
1099 trace->tool.mmap2 = perf_event__process_mmap2;
1100 trace->tool.comm = perf_event__process_comm;
1101 trace->tool.exit = perf_event__process_exit;
1102 trace->tool.fork = perf_event__process_fork;
1103 trace->tool.attr = perf_event__process_attr;
1104 trace->tool.tracing_data = perf_event__process_tracing_data;
1105 trace->tool.build_id = perf_event__process_build_id;
1107 trace->tool.ordered_samples = true;
1108 trace->tool.ordering_requires_timestamps = true;
1110 /* add tid to output */
1111 trace->multiple_threads = true;
1113 if (symbol__init() < 0)
1116 session = perf_session__new(input_name, O_RDONLY, 0, false,
1118 if (session == NULL)
1121 err = perf_session__set_tracepoints_handlers(session, handlers);
1125 if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
1126 pr_err("Data file does not have raw_syscalls:sys_enter events\n");
1130 if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
1131 pr_err("Data file does not have raw_syscalls:sys_exit events\n");
1135 err = parse_target_str(trace);
1141 err = perf_session__process_events(session, &trace->tool);
1143 pr_err("Failed to process events, error %d", err);
1146 perf_session__delete(session);
1151 static size_t trace__fprintf_threads_header(FILE *fp)
1155 printed = fprintf(fp, "\n _____________________________________________________________________\n");
1156 printed += fprintf(fp," __) Summary of events (__\n\n");
1157 printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
1158 printed += fprintf(fp," _____________________________________________________________________\n\n");
1163 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
1165 size_t printed = trace__fprintf_threads_header(fp);
1168 for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
1169 struct thread *thread = rb_entry(nd, struct thread, rb_node);
1170 struct thread_trace *ttrace = thread->priv;
1177 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
1179 color = PERF_COLOR_NORMAL;
1181 color = PERF_COLOR_RED;
1182 else if (ratio > 25.0)
1183 color = PERF_COLOR_GREEN;
1184 else if (ratio > 5.0)
1185 color = PERF_COLOR_YELLOW;
1187 printed += color_fprintf(fp, color, "%20s", thread->comm);
1188 printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
1189 printed += color_fprintf(fp, color, "%5.1f%%", ratio);
1190 printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
1196 static int trace__set_duration(const struct option *opt, const char *str,
1197 int unset __maybe_unused)
1199 struct trace *trace = opt->value;
1201 trace->duration_filter = atof(str);
1205 static int trace__open_output(struct trace *trace, const char *filename)
1209 if (!stat(filename, &st) && st.st_size) {
1210 char oldname[PATH_MAX];
1212 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
1214 rename(filename, oldname);
1217 trace->output = fopen(filename, "w");
1219 return trace->output == NULL ? -errno : 0;
1222 int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
1224 const char * const trace_usage[] = {
1225 "perf trace [<options>] [<command>]",
1226 "perf trace [<options>] -- <command> [<options>]",
1229 struct trace trace = {
1230 .audit_machine = audit_detect_machine(),
1239 .user_freq = UINT_MAX,
1240 .user_interval = ULLONG_MAX,
1246 const char *output_name = NULL;
1247 const char *ev_qualifier_str = NULL;
1248 const struct option trace_options[] = {
1249 OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
1250 "list of events to trace"),
1251 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1252 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
1253 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
1254 "trace events on existing process id"),
1255 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
1256 "trace events on existing thread id"),
1257 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
1258 "system-wide collection from all CPUs"),
1259 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
1260 "list of cpus to monitor"),
1261 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
1262 "child tasks do not inherit counters"),
1263 OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
1264 "number of mmap data pages"),
1265 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
1267 OPT_CALLBACK(0, "duration", &trace, "float",
1268 "show only events with duration > N.M ms",
1269 trace__set_duration),
1270 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
1271 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1277 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
1279 if (output_name != NULL) {
1280 err = trace__open_output(&trace, output_name);
1282 perror("failed to create output file");
1287 if (ev_qualifier_str != NULL) {
1288 const char *s = ev_qualifier_str;
1290 trace.not_ev_qualifier = *s == '!';
1291 if (trace.not_ev_qualifier)
1293 trace.ev_qualifier = strlist__new(true, s);
1294 if (trace.ev_qualifier == NULL) {
1295 fputs("Not enough memory to parse event qualifier",
1302 err = perf_target__validate(&trace.opts.target);
1304 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1305 fprintf(trace.output, "%s", bf);
1309 err = perf_target__parse_uid(&trace.opts.target);
1311 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1312 fprintf(trace.output, "%s", bf);
1316 if (!argc && perf_target__none(&trace.opts.target))
1317 trace.opts.target.system_wide = true;
1320 err = trace__replay(&trace);
1322 err = trace__run(&trace, argc, argv);
1324 if (trace.sched && !err)
1325 trace__fprintf_thread_summary(&trace, trace.output);
1328 if (output_name != NULL)
1329 fclose(trace.output);