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