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 static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
20 unsigned long arg, u8 *arg_mask __maybe_unused)
22 return scnprintf(bf, size, "%#lx", arg);
25 #define SCA_HEX syscall_arg__scnprintf_hex
27 static size_t syscall_arg__scnprintf_whence(char *bf, size_t size,
28 unsigned long arg, u8 *arg_mask __maybe_unused)
33 #define P_WHENCE(n) case SEEK_##n: return scnprintf(bf, size, #n)
47 return scnprintf(bf, size, "%#x", whence);
50 #define SCA_WHENCE syscall_arg__scnprintf_whence
52 static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
53 unsigned long arg, u8 *arg_mask __maybe_unused)
55 int printed = 0, prot = arg;
57 if (prot == PROT_NONE)
58 return scnprintf(bf, size, "NONE");
59 #define P_MMAP_PROT(n) \
60 if (prot & PROT_##n) { \
61 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
71 P_MMAP_PROT(GROWSDOWN);
76 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
81 #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
83 static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
84 unsigned long arg, u8 *arg_mask __maybe_unused)
86 int printed = 0, flags = arg;
88 #define P_MMAP_FLAG(n) \
89 if (flags & MAP_##n) { \
90 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
97 P_MMAP_FLAG(ANONYMOUS);
98 P_MMAP_FLAG(DENYWRITE);
99 P_MMAP_FLAG(EXECUTABLE);
102 P_MMAP_FLAG(GROWSDOWN);
104 P_MMAP_FLAG(HUGETLB);
107 P_MMAP_FLAG(NONBLOCK);
108 P_MMAP_FLAG(NORESERVE);
109 P_MMAP_FLAG(POPULATE);
111 #ifdef MAP_UNINITIALIZED
112 P_MMAP_FLAG(UNINITIALIZED);
117 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
122 #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
124 static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
125 unsigned long arg, u8 *arg_mask __maybe_unused)
130 #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
133 P_MADV_BHV(SEQUENTIAL);
134 P_MADV_BHV(WILLNEED);
135 P_MADV_BHV(DONTNEED);
137 P_MADV_BHV(DONTFORK);
139 P_MADV_BHV(HWPOISON);
140 #ifdef MADV_SOFT_OFFLINE
141 P_MADV_BHV(SOFT_OFFLINE);
143 P_MADV_BHV(MERGEABLE);
144 P_MADV_BHV(UNMERGEABLE);
146 P_MADV_BHV(HUGEPAGE);
148 #ifdef MADV_NOHUGEPAGE
149 P_MADV_BHV(NOHUGEPAGE);
152 P_MADV_BHV(DONTDUMP);
161 return scnprintf(bf, size, "%#x", behavior);
164 #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
166 static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, unsigned long arg, u8 *arg_mask)
168 enum syscall_futex_args {
169 SCF_UADDR = (1 << 0),
172 SCF_TIMEOUT = (1 << 3),
173 SCF_UADDR2 = (1 << 4),
177 int cmd = op & FUTEX_CMD_MASK;
181 #define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
182 P_FUTEX_OP(WAIT); *arg_mask |= SCF_VAL3|SCF_UADDR2; break;
183 P_FUTEX_OP(WAKE); *arg_mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
184 P_FUTEX_OP(FD); *arg_mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
185 P_FUTEX_OP(REQUEUE); *arg_mask |= SCF_VAL3|SCF_TIMEOUT; break;
186 P_FUTEX_OP(CMP_REQUEUE); *arg_mask |= SCF_TIMEOUT; break;
187 P_FUTEX_OP(CMP_REQUEUE_PI); *arg_mask |= SCF_TIMEOUT; break;
188 P_FUTEX_OP(WAKE_OP); break;
189 P_FUTEX_OP(LOCK_PI); *arg_mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
190 P_FUTEX_OP(UNLOCK_PI); *arg_mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
191 P_FUTEX_OP(TRYLOCK_PI); *arg_mask |= SCF_VAL3|SCF_UADDR2; break;
192 P_FUTEX_OP(WAIT_BITSET); *arg_mask |= SCF_UADDR2; break;
193 P_FUTEX_OP(WAKE_BITSET); *arg_mask |= SCF_UADDR2; break;
194 P_FUTEX_OP(WAIT_REQUEUE_PI); break;
195 default: printed = scnprintf(bf, size, "%#x", cmd); break;
198 if (op & FUTEX_PRIVATE_FLAG)
199 printed += scnprintf(bf + printed, size - printed, "|PRIV");
201 if (op & FUTEX_CLOCK_REALTIME)
202 printed += scnprintf(bf + printed, size - printed, "|CLKRT");
207 #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
209 static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
210 unsigned long arg, u8 *arg_mask)
212 int printed = 0, flags = arg;
214 if (!(flags & O_CREAT))
215 *arg_mask |= 1 << 2; /* Mask the mode parm */
218 return scnprintf(bf, size, "RDONLY");
220 if (flags & O_##n) { \
221 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
245 if ((flags & O_SYNC) == O_SYNC)
246 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
258 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
263 #define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
265 static struct syscall_fmt {
268 size_t (*arg_scnprintf[6])(char *bf, size_t size, unsigned long arg, u8 *arg_mask);
273 { .name = "access", .errmsg = true, },
274 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
275 { .name = "brk", .hexret = true,
276 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
277 { .name = "mmap", .hexret = true, },
278 { .name = "connect", .errmsg = true, },
279 { .name = "fstat", .errmsg = true, .alias = "newfstat", },
280 { .name = "fstatat", .errmsg = true, .alias = "newfstatat", },
281 { .name = "futex", .errmsg = true,
282 .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
283 { .name = "ioctl", .errmsg = true,
284 .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, },
285 { .name = "lseek", .errmsg = true,
286 .arg_scnprintf = { [2] = SCA_WHENCE, /* whence */ }, },
287 { .name = "lstat", .errmsg = true, .alias = "newlstat", },
288 { .name = "madvise", .errmsg = true,
289 .arg_scnprintf = { [0] = SCA_HEX, /* start */
290 [2] = SCA_MADV_BHV, /* behavior */ }, },
291 { .name = "mmap", .hexret = true,
292 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
293 [2] = SCA_MMAP_PROT, /* prot */
294 [3] = SCA_MMAP_FLAGS, /* flags */ }, },
295 { .name = "mprotect", .errmsg = true,
296 .arg_scnprintf = { [0] = SCA_HEX, /* start */
297 [2] = SCA_MMAP_PROT, /* prot */ }, },
298 { .name = "mremap", .hexret = true,
299 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
300 [4] = SCA_HEX, /* new_addr */ }, },
301 { .name = "munmap", .errmsg = true,
302 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
303 { .name = "open", .errmsg = true,
304 .arg_scnprintf = { [1] = SCA_OPEN_FLAGS, /* flags */ }, },
305 { .name = "poll", .errmsg = true, .timeout = true, },
306 { .name = "ppoll", .errmsg = true, .timeout = true, },
307 { .name = "pread", .errmsg = true, .alias = "pread64", },
308 { .name = "pwrite", .errmsg = true, .alias = "pwrite64", },
309 { .name = "read", .errmsg = true, },
310 { .name = "recvfrom", .errmsg = true, },
311 { .name = "select", .errmsg = true, .timeout = true, },
312 { .name = "socket", .errmsg = true, },
313 { .name = "stat", .errmsg = true, .alias = "newstat", },
314 { .name = "uname", .errmsg = true, .alias = "newuname", },
317 static int syscall_fmt__cmp(const void *name, const void *fmtp)
319 const struct syscall_fmt *fmt = fmtp;
320 return strcmp(name, fmt->name);
323 static struct syscall_fmt *syscall_fmt__find(const char *name)
325 const int nmemb = ARRAY_SIZE(syscall_fmts);
326 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
330 struct event_format *tp_format;
333 struct syscall_fmt *fmt;
334 size_t (**arg_scnprintf)(char *bf, size_t size,
335 unsigned long arg, u8 *args_mask);
338 static size_t fprintf_duration(unsigned long t, FILE *fp)
340 double duration = (double)t / NSEC_PER_MSEC;
341 size_t printed = fprintf(fp, "(");
344 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
345 else if (duration >= 0.01)
346 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
348 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
349 return printed + fprintf(fp, "): ");
352 struct thread_trace {
356 unsigned long nr_events;
361 static struct thread_trace *thread_trace__new(void)
363 return zalloc(sizeof(struct thread_trace));
366 static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
368 struct thread_trace *ttrace;
373 if (thread->priv == NULL)
374 thread->priv = thread_trace__new();
376 if (thread->priv == NULL)
379 ttrace = thread->priv;
384 color_fprintf(fp, PERF_COLOR_RED,
385 "WARNING: not enough memory, dropping samples!\n");
390 struct perf_tool tool;
394 struct syscall *table;
396 struct perf_record_opts opts;
400 unsigned long nr_events;
401 struct strlist *ev_qualifier;
402 bool not_ev_qualifier;
403 struct intlist *tid_list;
404 struct intlist *pid_list;
406 bool multiple_threads;
407 double duration_filter;
411 static bool trace__filter_duration(struct trace *trace, double t)
413 return t < (trace->duration_filter * NSEC_PER_MSEC);
416 static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
418 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
420 return fprintf(fp, "%10.3f ", ts);
423 static bool done = false;
425 static void sig_handler(int sig __maybe_unused)
430 static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
431 u64 duration, u64 tstamp, FILE *fp)
433 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
434 printed += fprintf_duration(duration, fp);
436 if (trace->multiple_threads)
437 printed += fprintf(fp, "%d ", thread->tid);
442 static int trace__process_event(struct trace *trace, struct machine *machine,
443 union perf_event *event)
447 switch (event->header.type) {
448 case PERF_RECORD_LOST:
449 color_fprintf(trace->output, PERF_COLOR_RED,
450 "LOST %" PRIu64 " events!\n", event->lost.lost);
451 ret = machine__process_lost_event(machine, event);
453 ret = machine__process_event(machine, event);
460 static int trace__tool_process(struct perf_tool *tool,
461 union perf_event *event,
462 struct perf_sample *sample __maybe_unused,
463 struct machine *machine)
465 struct trace *trace = container_of(tool, struct trace, tool);
466 return trace__process_event(trace, machine, event);
469 static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
471 int err = symbol__init();
476 machine__init(&trace->host, "", HOST_KERNEL_ID);
477 machine__create_kernel_maps(&trace->host);
479 if (perf_target__has_task(&trace->opts.target)) {
480 err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads,
484 err = perf_event__synthesize_threads(&trace->tool, trace__tool_process,
494 static int syscall__set_arg_fmts(struct syscall *sc)
496 struct format_field *field;
499 sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
500 if (sc->arg_scnprintf == NULL)
503 for (field = sc->tp_format->format.fields->next; field; field = field->next) {
504 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
505 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
506 else if (field->flags & FIELD_IS_POINTER)
507 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
514 static int trace__read_syscall_info(struct trace *trace, int id)
518 const char *name = audit_syscall_to_name(id, trace->audit_machine);
523 if (id > trace->syscalls.max) {
524 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
526 if (nsyscalls == NULL)
529 if (trace->syscalls.max != -1) {
530 memset(nsyscalls + trace->syscalls.max + 1, 0,
531 (id - trace->syscalls.max) * sizeof(*sc));
533 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
536 trace->syscalls.table = nsyscalls;
537 trace->syscalls.max = id;
540 sc = trace->syscalls.table + id;
543 if (trace->ev_qualifier) {
544 bool in = strlist__find(trace->ev_qualifier, name) != NULL;
546 if (!(in ^ trace->not_ev_qualifier)) {
549 * No need to do read tracepoint information since this will be
556 sc->fmt = syscall_fmt__find(sc->name);
558 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
559 sc->tp_format = event_format__new("syscalls", tp_name);
561 if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) {
562 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
563 sc->tp_format = event_format__new("syscalls", tp_name);
566 if (sc->tp_format == NULL)
569 return syscall__set_arg_fmts(sc);
572 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
578 if (sc->tp_format != NULL) {
579 struct format_field *field;
580 u8 mask = 0, bit = 1;
582 for (field = sc->tp_format->format.fields->next; field;
583 field = field->next, ++i, bit <<= 1) {
587 printed += scnprintf(bf + printed, size - printed,
588 "%s%s: ", printed ? ", " : "", field->name);
590 if (sc->arg_scnprintf && sc->arg_scnprintf[i]) {
591 printed += sc->arg_scnprintf[i](bf + printed, size - printed,
594 printed += scnprintf(bf + printed, size - printed,
600 printed += scnprintf(bf + printed, size - printed,
602 printed ? ", " : "", i, args[i]);
610 typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
611 struct perf_sample *sample);
613 static struct syscall *trace__syscall_info(struct trace *trace,
614 struct perf_evsel *evsel,
615 struct perf_sample *sample)
617 int id = perf_evsel__intval(evsel, sample, "id");
622 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
623 * before that, leaving at a higher verbosity level till that is
624 * explained. Reproduced with plain ftrace with:
626 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
627 * grep "NR -1 " /t/trace_pipe
629 * After generating some load on the machine.
633 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
634 id, perf_evsel__name(evsel), ++n);
639 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
640 trace__read_syscall_info(trace, id))
643 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
646 return &trace->syscalls.table[id];
650 fprintf(trace->output, "Problems reading syscall %d", id);
651 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
652 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
653 fputs(" information\n", trace->output);
658 static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
659 struct perf_sample *sample)
664 struct thread *thread;
665 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
666 struct thread_trace *ttrace;
674 thread = machine__findnew_thread(&trace->host, sample->pid,
676 ttrace = thread__trace(thread, trace->output);
680 args = perf_evsel__rawptr(evsel, sample, "args");
682 fprintf(trace->output, "Problems reading syscall arguments\n");
686 ttrace = thread->priv;
688 if (ttrace->entry_str == NULL) {
689 ttrace->entry_str = malloc(1024);
690 if (!ttrace->entry_str)
694 ttrace->entry_time = sample->time;
695 msg = ttrace->entry_str;
696 printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);
698 printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);
700 if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
701 if (!trace->duration_filter) {
702 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
703 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
706 ttrace->entry_pending = true;
711 static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
712 struct perf_sample *sample)
716 struct thread *thread;
717 struct syscall *sc = trace__syscall_info(trace, evsel, sample);
718 struct thread_trace *ttrace;
726 thread = machine__findnew_thread(&trace->host, sample->pid,
728 ttrace = thread__trace(thread, trace->output);
732 ret = perf_evsel__intval(evsel, sample, "ret");
734 ttrace = thread->priv;
736 ttrace->exit_time = sample->time;
738 if (ttrace->entry_time) {
739 duration = sample->time - ttrace->entry_time;
740 if (trace__filter_duration(trace, duration))
742 } else if (trace->duration_filter)
745 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
747 if (ttrace->entry_pending) {
748 fprintf(trace->output, "%-70s", ttrace->entry_str);
750 fprintf(trace->output, " ... [");
751 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
752 fprintf(trace->output, "]: %s()", sc->name);
755 if (sc->fmt == NULL) {
757 fprintf(trace->output, ") = %d", ret);
758 } else if (ret < 0 && sc->fmt->errmsg) {
760 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
761 *e = audit_errno_to_name(-ret);
763 fprintf(trace->output, ") = -1 %s %s", e, emsg);
764 } else if (ret == 0 && sc->fmt->timeout)
765 fprintf(trace->output, ") = 0 Timeout");
766 else if (sc->fmt->hexret)
767 fprintf(trace->output, ") = %#x", ret);
771 fputc('\n', trace->output);
773 ttrace->entry_pending = false;
778 static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
779 struct perf_sample *sample)
781 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
782 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
783 struct thread *thread = machine__findnew_thread(&trace->host,
786 struct thread_trace *ttrace = thread__trace(thread, trace->output);
791 ttrace->runtime_ms += runtime_ms;
792 trace->runtime_ms += runtime_ms;
796 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
798 perf_evsel__strval(evsel, sample, "comm"),
799 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
801 perf_evsel__intval(evsel, sample, "vruntime"));
805 static bool skip_sample(struct trace *trace, struct perf_sample *sample)
807 if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
808 (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
811 if (trace->pid_list || trace->tid_list)
817 static int trace__process_sample(struct perf_tool *tool,
818 union perf_event *event __maybe_unused,
819 struct perf_sample *sample,
820 struct perf_evsel *evsel,
821 struct machine *machine __maybe_unused)
823 struct trace *trace = container_of(tool, struct trace, tool);
826 tracepoint_handler handler = evsel->handler.func;
828 if (skip_sample(trace, sample))
831 if (trace->base_time == 0)
832 trace->base_time = sample->time;
835 handler(trace, evsel, sample);
841 perf_session__has_tp(struct perf_session *session, const char *name)
843 struct perf_evsel *evsel;
845 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name);
847 return evsel != NULL;
850 static int parse_target_str(struct trace *trace)
852 if (trace->opts.target.pid) {
853 trace->pid_list = intlist__new(trace->opts.target.pid);
854 if (trace->pid_list == NULL) {
855 pr_err("Error parsing process id string\n");
860 if (trace->opts.target.tid) {
861 trace->tid_list = intlist__new(trace->opts.target.tid);
862 if (trace->tid_list == NULL) {
863 pr_err("Error parsing thread id string\n");
871 static int trace__run(struct trace *trace, int argc, const char **argv)
873 struct perf_evlist *evlist = perf_evlist__new();
874 struct perf_evsel *evsel;
876 unsigned long before;
877 const bool forks = argc > 0;
879 if (evlist == NULL) {
880 fprintf(trace->output, "Not enough memory to run!\n");
884 if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) ||
885 perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) {
886 fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n");
887 goto out_delete_evlist;
891 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
892 trace__sched_stat_runtime)) {
893 fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n");
894 goto out_delete_evlist;
897 err = perf_evlist__create_maps(evlist, &trace->opts.target);
899 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
900 goto out_delete_evlist;
903 err = trace__symbols_init(trace, evlist);
905 fprintf(trace->output, "Problems initializing symbol libraries!\n");
906 goto out_delete_maps;
909 perf_evlist__config(evlist, &trace->opts);
911 signal(SIGCHLD, sig_handler);
912 signal(SIGINT, sig_handler);
915 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
918 fprintf(trace->output, "Couldn't run the workload!\n");
919 goto out_delete_maps;
923 err = perf_evlist__open(evlist);
925 fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno));
926 goto out_delete_maps;
929 err = perf_evlist__mmap(evlist, UINT_MAX, false);
931 fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno));
932 goto out_close_evlist;
935 perf_evlist__enable(evlist);
938 perf_evlist__start_workload(evlist);
940 trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
942 before = trace->nr_events;
944 for (i = 0; i < evlist->nr_mmaps; i++) {
945 union perf_event *event;
947 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
948 const u32 type = event->header.type;
949 tracepoint_handler handler;
950 struct perf_sample sample;
954 err = perf_evlist__parse_sample(evlist, event, &sample);
956 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
960 if (trace->base_time == 0)
961 trace->base_time = sample.time;
963 if (type != PERF_RECORD_SAMPLE) {
964 trace__process_event(trace, &trace->host, event);
968 evsel = perf_evlist__id2evsel(evlist, sample.id);
970 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
974 if (sample.raw_data == NULL) {
975 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
976 perf_evsel__name(evsel), sample.tid,
977 sample.cpu, sample.raw_size);
981 handler = evsel->handler.func;
982 handler(trace, evsel, &sample);
986 if (trace->nr_events == before) {
988 goto out_unmap_evlist;
990 poll(evlist->pollfd, evlist->nr_fds, -1);
994 perf_evlist__disable(evlist);
999 perf_evlist__munmap(evlist);
1001 perf_evlist__close(evlist);
1003 perf_evlist__delete_maps(evlist);
1005 perf_evlist__delete(evlist);
1010 static int trace__replay(struct trace *trace)
1012 const struct perf_evsel_str_handler handlers[] = {
1013 { "raw_syscalls:sys_enter", trace__sys_enter, },
1014 { "raw_syscalls:sys_exit", trace__sys_exit, },
1017 struct perf_session *session;
1020 trace->tool.sample = trace__process_sample;
1021 trace->tool.mmap = perf_event__process_mmap;
1022 trace->tool.comm = perf_event__process_comm;
1023 trace->tool.exit = perf_event__process_exit;
1024 trace->tool.fork = perf_event__process_fork;
1025 trace->tool.attr = perf_event__process_attr;
1026 trace->tool.tracing_data = perf_event__process_tracing_data;
1027 trace->tool.build_id = perf_event__process_build_id;
1029 trace->tool.ordered_samples = true;
1030 trace->tool.ordering_requires_timestamps = true;
1032 /* add tid to output */
1033 trace->multiple_threads = true;
1035 if (symbol__init() < 0)
1038 session = perf_session__new(input_name, O_RDONLY, 0, false,
1040 if (session == NULL)
1043 err = perf_session__set_tracepoints_handlers(session, handlers);
1047 if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) {
1048 pr_err("Data file does not have raw_syscalls:sys_enter events\n");
1052 if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) {
1053 pr_err("Data file does not have raw_syscalls:sys_exit events\n");
1057 err = parse_target_str(trace);
1063 err = perf_session__process_events(session, &trace->tool);
1065 pr_err("Failed to process events, error %d", err);
1068 perf_session__delete(session);
1073 static size_t trace__fprintf_threads_header(FILE *fp)
1077 printed = fprintf(fp, "\n _____________________________________________________________________\n");
1078 printed += fprintf(fp," __) Summary of events (__\n\n");
1079 printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
1080 printed += fprintf(fp," _____________________________________________________________________\n\n");
1085 static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
1087 size_t printed = trace__fprintf_threads_header(fp);
1090 for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
1091 struct thread *thread = rb_entry(nd, struct thread, rb_node);
1092 struct thread_trace *ttrace = thread->priv;
1099 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
1101 color = PERF_COLOR_NORMAL;
1103 color = PERF_COLOR_RED;
1104 else if (ratio > 25.0)
1105 color = PERF_COLOR_GREEN;
1106 else if (ratio > 5.0)
1107 color = PERF_COLOR_YELLOW;
1109 printed += color_fprintf(fp, color, "%20s", thread->comm);
1110 printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events);
1111 printed += color_fprintf(fp, color, "%5.1f%%", ratio);
1112 printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
1118 static int trace__set_duration(const struct option *opt, const char *str,
1119 int unset __maybe_unused)
1121 struct trace *trace = opt->value;
1123 trace->duration_filter = atof(str);
1127 static int trace__open_output(struct trace *trace, const char *filename)
1131 if (!stat(filename, &st) && st.st_size) {
1132 char oldname[PATH_MAX];
1134 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
1136 rename(filename, oldname);
1139 trace->output = fopen(filename, "w");
1141 return trace->output == NULL ? -errno : 0;
1144 int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
1146 const char * const trace_usage[] = {
1147 "perf trace [<options>] [<command>]",
1148 "perf trace [<options>] -- <command> [<options>]",
1151 struct trace trace = {
1152 .audit_machine = audit_detect_machine(),
1161 .user_freq = UINT_MAX,
1162 .user_interval = ULLONG_MAX,
1168 const char *output_name = NULL;
1169 const char *ev_qualifier_str = NULL;
1170 const struct option trace_options[] = {
1171 OPT_STRING('e', "expr", &ev_qualifier_str, "expr",
1172 "list of events to trace"),
1173 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1174 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
1175 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
1176 "trace events on existing process id"),
1177 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
1178 "trace events on existing thread id"),
1179 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
1180 "system-wide collection from all CPUs"),
1181 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
1182 "list of cpus to monitor"),
1183 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
1184 "child tasks do not inherit counters"),
1185 OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages,
1186 "number of mmap data pages"),
1187 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
1189 OPT_CALLBACK(0, "duration", &trace, "float",
1190 "show only events with duration > N.M ms",
1191 trace__set_duration),
1192 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
1193 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1199 argc = parse_options(argc, argv, trace_options, trace_usage, 0);
1201 if (output_name != NULL) {
1202 err = trace__open_output(&trace, output_name);
1204 perror("failed to create output file");
1209 if (ev_qualifier_str != NULL) {
1210 const char *s = ev_qualifier_str;
1212 trace.not_ev_qualifier = *s == '!';
1213 if (trace.not_ev_qualifier)
1215 trace.ev_qualifier = strlist__new(true, s);
1216 if (trace.ev_qualifier == NULL) {
1217 fputs("Not enough memory to parse event qualifier",
1224 err = perf_target__validate(&trace.opts.target);
1226 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1227 fprintf(trace.output, "%s", bf);
1231 err = perf_target__parse_uid(&trace.opts.target);
1233 perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf));
1234 fprintf(trace.output, "%s", bf);
1238 if (!argc && perf_target__none(&trace.opts.target))
1239 trace.opts.target.system_wide = true;
1242 err = trace__replay(&trace);
1244 err = trace__run(&trace, argc, argv);
1246 if (trace.sched && !err)
1247 trace__fprintf_thread_summary(&trace, trace.output);
1250 if (output_name != NULL)
1251 fclose(trace.output);