Commit | Line | Data |
---|---|---|
4e319027 | 1 | #include <traceevent/event-parse.h> |
514f1c67 | 2 | #include "builtin.h" |
752fde44 | 3 | #include "util/color.h" |
7c304ee0 | 4 | #include "util/debug.h" |
514f1c67 | 5 | #include "util/evlist.h" |
752fde44 | 6 | #include "util/machine.h" |
6810fc91 | 7 | #include "util/session.h" |
752fde44 | 8 | #include "util/thread.h" |
514f1c67 | 9 | #include "util/parse-options.h" |
2ae3a312 | 10 | #include "util/strlist.h" |
bdc89661 | 11 | #include "util/intlist.h" |
514f1c67 | 12 | #include "util/thread_map.h" |
514f1c67 ACM |
13 | |
14 | #include <libaudit.h> | |
15 | #include <stdlib.h> | |
ae685380 | 16 | #include <sys/mman.h> |
f9da0b0c | 17 | #include <linux/futex.h> |
514f1c67 | 18 | |
6e7eeb51 ACM |
19 | static size_t syscall_arg__scnprintf_hex(char *bf, size_t size, |
20 | unsigned long arg, u8 *arg_mask __maybe_unused) | |
13d4ff3e ACM |
21 | { |
22 | return scnprintf(bf, size, "%#lx", arg); | |
23 | } | |
24 | ||
beccb2b5 ACM |
25 | #define SCA_HEX syscall_arg__scnprintf_hex |
26 | ||
579e7865 ACM |
27 | static size_t syscall_arg__scnprintf_whence(char *bf, size_t size, |
28 | unsigned long arg, u8 *arg_mask __maybe_unused) | |
29 | { | |
30 | int whence = arg; | |
31 | ||
32 | switch (whence) { | |
33 | #define P_WHENCE(n) case SEEK_##n: return scnprintf(bf, size, #n) | |
34 | P_WHENCE(SET); | |
35 | P_WHENCE(CUR); | |
36 | P_WHENCE(END); | |
37 | #ifdef SEEK_DATA | |
38 | P_WHENCE(DATA); | |
39 | #endif | |
40 | #ifdef SEEK_HOLE | |
41 | P_WHENCE(HOLE); | |
42 | #endif | |
43 | #undef P_WHENCE | |
44 | default: break; | |
45 | } | |
46 | ||
47 | return scnprintf(bf, size, "%#x", whence); | |
48 | } | |
49 | ||
50 | #define SCA_WHENCE syscall_arg__scnprintf_whence | |
51 | ||
6e7eeb51 ACM |
52 | static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size, |
53 | unsigned long arg, u8 *arg_mask __maybe_unused) | |
ae685380 ACM |
54 | { |
55 | int printed = 0, prot = arg; | |
56 | ||
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); \ | |
62 | prot &= ~PROT_##n; \ | |
63 | } | |
64 | ||
65 | P_MMAP_PROT(EXEC); | |
66 | P_MMAP_PROT(READ); | |
67 | P_MMAP_PROT(WRITE); | |
68 | #ifdef PROT_SEM | |
69 | P_MMAP_PROT(SEM); | |
70 | #endif | |
71 | P_MMAP_PROT(GROWSDOWN); | |
72 | P_MMAP_PROT(GROWSUP); | |
73 | #undef P_MMAP_PROT | |
74 | ||
75 | if (prot) | |
76 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot); | |
77 | ||
78 | return printed; | |
79 | } | |
80 | ||
81 | #define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot | |
82 | ||
6e7eeb51 ACM |
83 | static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size, |
84 | unsigned long arg, u8 *arg_mask __maybe_unused) | |
941557e0 ACM |
85 | { |
86 | int printed = 0, flags = arg; | |
87 | ||
88 | #define P_MMAP_FLAG(n) \ | |
89 | if (flags & MAP_##n) { \ | |
90 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ | |
91 | flags &= ~MAP_##n; \ | |
92 | } | |
93 | ||
94 | P_MMAP_FLAG(SHARED); | |
95 | P_MMAP_FLAG(PRIVATE); | |
96 | P_MMAP_FLAG(32BIT); | |
97 | P_MMAP_FLAG(ANONYMOUS); | |
98 | P_MMAP_FLAG(DENYWRITE); | |
99 | P_MMAP_FLAG(EXECUTABLE); | |
100 | P_MMAP_FLAG(FILE); | |
101 | P_MMAP_FLAG(FIXED); | |
102 | P_MMAP_FLAG(GROWSDOWN); | |
f2935f3e | 103 | #ifdef MAP_HUGETLB |
941557e0 | 104 | P_MMAP_FLAG(HUGETLB); |
f2935f3e | 105 | #endif |
941557e0 ACM |
106 | P_MMAP_FLAG(LOCKED); |
107 | P_MMAP_FLAG(NONBLOCK); | |
108 | P_MMAP_FLAG(NORESERVE); | |
109 | P_MMAP_FLAG(POPULATE); | |
110 | P_MMAP_FLAG(STACK); | |
111 | #ifdef MAP_UNINITIALIZED | |
112 | P_MMAP_FLAG(UNINITIALIZED); | |
113 | #endif | |
114 | #undef P_MMAP_FLAG | |
115 | ||
116 | if (flags) | |
117 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); | |
118 | ||
119 | return printed; | |
120 | } | |
121 | ||
122 | #define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags | |
123 | ||
6e7eeb51 ACM |
124 | static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size, |
125 | unsigned long arg, u8 *arg_mask __maybe_unused) | |
9e9716d1 ACM |
126 | { |
127 | int behavior = arg; | |
128 | ||
129 | switch (behavior) { | |
130 | #define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n) | |
131 | P_MADV_BHV(NORMAL); | |
132 | P_MADV_BHV(RANDOM); | |
133 | P_MADV_BHV(SEQUENTIAL); | |
134 | P_MADV_BHV(WILLNEED); | |
135 | P_MADV_BHV(DONTNEED); | |
136 | P_MADV_BHV(REMOVE); | |
137 | P_MADV_BHV(DONTFORK); | |
138 | P_MADV_BHV(DOFORK); | |
139 | P_MADV_BHV(HWPOISON); | |
140 | #ifdef MADV_SOFT_OFFLINE | |
141 | P_MADV_BHV(SOFT_OFFLINE); | |
142 | #endif | |
143 | P_MADV_BHV(MERGEABLE); | |
144 | P_MADV_BHV(UNMERGEABLE); | |
f2935f3e | 145 | #ifdef MADV_HUGEPAGE |
9e9716d1 | 146 | P_MADV_BHV(HUGEPAGE); |
f2935f3e DA |
147 | #endif |
148 | #ifdef MADV_NOHUGEPAGE | |
9e9716d1 | 149 | P_MADV_BHV(NOHUGEPAGE); |
f2935f3e | 150 | #endif |
9e9716d1 ACM |
151 | #ifdef MADV_DONTDUMP |
152 | P_MADV_BHV(DONTDUMP); | |
153 | #endif | |
154 | #ifdef MADV_DODUMP | |
155 | P_MADV_BHV(DODUMP); | |
156 | #endif | |
157 | #undef P_MADV_PHV | |
158 | default: break; | |
159 | } | |
160 | ||
161 | return scnprintf(bf, size, "%#x", behavior); | |
162 | } | |
163 | ||
164 | #define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior | |
165 | ||
f9da0b0c ACM |
166 | static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, unsigned long arg, u8 *arg_mask) |
167 | { | |
168 | enum syscall_futex_args { | |
169 | SCF_UADDR = (1 << 0), | |
170 | SCF_OP = (1 << 1), | |
171 | SCF_VAL = (1 << 2), | |
172 | SCF_TIMEOUT = (1 << 3), | |
173 | SCF_UADDR2 = (1 << 4), | |
174 | SCF_VAL3 = (1 << 5), | |
175 | }; | |
176 | int op = arg; | |
177 | int cmd = op & FUTEX_CMD_MASK; | |
178 | size_t printed = 0; | |
179 | ||
180 | switch (cmd) { | |
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; | |
196 | } | |
197 | ||
198 | if (op & FUTEX_PRIVATE_FLAG) | |
199 | printed += scnprintf(bf + printed, size - printed, "|PRIV"); | |
200 | ||
201 | if (op & FUTEX_CLOCK_REALTIME) | |
202 | printed += scnprintf(bf + printed, size - printed, "|CLKRT"); | |
203 | ||
204 | return printed; | |
205 | } | |
206 | ||
207 | #define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op | |
208 | ||
514f1c67 ACM |
209 | static struct syscall_fmt { |
210 | const char *name; | |
aec1930b | 211 | const char *alias; |
6e7eeb51 | 212 | size_t (*arg_scnprintf[6])(char *bf, size_t size, unsigned long arg, u8 *arg_mask); |
514f1c67 ACM |
213 | bool errmsg; |
214 | bool timeout; | |
04b34729 | 215 | bool hexret; |
514f1c67 | 216 | } syscall_fmts[] = { |
8b745263 | 217 | { .name = "access", .errmsg = true, }, |
aec1930b | 218 | { .name = "arch_prctl", .errmsg = true, .alias = "prctl", }, |
beccb2b5 ACM |
219 | { .name = "brk", .hexret = true, |
220 | .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, }, | |
04b34729 | 221 | { .name = "mmap", .hexret = true, }, |
a14bb860 | 222 | { .name = "connect", .errmsg = true, }, |
aec1930b ACM |
223 | { .name = "fstat", .errmsg = true, .alias = "newfstat", }, |
224 | { .name = "fstatat", .errmsg = true, .alias = "newfstatat", }, | |
f9da0b0c ACM |
225 | { .name = "futex", .errmsg = true, |
226 | .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, }, | |
beccb2b5 ACM |
227 | { .name = "ioctl", .errmsg = true, |
228 | .arg_scnprintf = { [2] = SCA_HEX, /* arg */ }, }, | |
579e7865 ACM |
229 | { .name = "lseek", .errmsg = true, |
230 | .arg_scnprintf = { [2] = SCA_WHENCE, /* whence */ }, }, | |
e5959683 | 231 | { .name = "lstat", .errmsg = true, .alias = "newlstat", }, |
9e9716d1 ACM |
232 | { .name = "madvise", .errmsg = true, |
233 | .arg_scnprintf = { [0] = SCA_HEX, /* start */ | |
234 | [2] = SCA_MADV_BHV, /* behavior */ }, }, | |
beccb2b5 | 235 | { .name = "mmap", .hexret = true, |
ae685380 | 236 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ |
941557e0 ACM |
237 | [2] = SCA_MMAP_PROT, /* prot */ |
238 | [3] = SCA_MMAP_FLAGS, /* flags */ }, }, | |
beccb2b5 | 239 | { .name = "mprotect", .errmsg = true, |
ae685380 ACM |
240 | .arg_scnprintf = { [0] = SCA_HEX, /* start */ |
241 | [2] = SCA_MMAP_PROT, /* prot */ }, }, | |
242 | { .name = "mremap", .hexret = true, | |
243 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ | |
244 | [4] = SCA_HEX, /* new_addr */ }, }, | |
beccb2b5 ACM |
245 | { .name = "munmap", .errmsg = true, |
246 | .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, }, | |
8b745263 | 247 | { .name = "open", .errmsg = true, }, |
aec1930b ACM |
248 | { .name = "poll", .errmsg = true, .timeout = true, }, |
249 | { .name = "ppoll", .errmsg = true, .timeout = true, }, | |
e5959683 ACM |
250 | { .name = "pread", .errmsg = true, .alias = "pread64", }, |
251 | { .name = "pwrite", .errmsg = true, .alias = "pwrite64", }, | |
aec1930b ACM |
252 | { .name = "read", .errmsg = true, }, |
253 | { .name = "recvfrom", .errmsg = true, }, | |
254 | { .name = "select", .errmsg = true, .timeout = true, }, | |
8b745263 | 255 | { .name = "socket", .errmsg = true, }, |
aec1930b | 256 | { .name = "stat", .errmsg = true, .alias = "newstat", }, |
e5959683 | 257 | { .name = "uname", .errmsg = true, .alias = "newuname", }, |
514f1c67 ACM |
258 | }; |
259 | ||
260 | static int syscall_fmt__cmp(const void *name, const void *fmtp) | |
261 | { | |
262 | const struct syscall_fmt *fmt = fmtp; | |
263 | return strcmp(name, fmt->name); | |
264 | } | |
265 | ||
266 | static struct syscall_fmt *syscall_fmt__find(const char *name) | |
267 | { | |
268 | const int nmemb = ARRAY_SIZE(syscall_fmts); | |
269 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); | |
270 | } | |
271 | ||
272 | struct syscall { | |
273 | struct event_format *tp_format; | |
274 | const char *name; | |
2ae3a312 | 275 | bool filtered; |
514f1c67 | 276 | struct syscall_fmt *fmt; |
6e7eeb51 ACM |
277 | size_t (**arg_scnprintf)(char *bf, size_t size, |
278 | unsigned long arg, u8 *args_mask); | |
514f1c67 ACM |
279 | }; |
280 | ||
60c907ab ACM |
281 | static size_t fprintf_duration(unsigned long t, FILE *fp) |
282 | { | |
283 | double duration = (double)t / NSEC_PER_MSEC; | |
284 | size_t printed = fprintf(fp, "("); | |
285 | ||
286 | if (duration >= 1.0) | |
287 | printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); | |
288 | else if (duration >= 0.01) | |
289 | printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); | |
290 | else | |
291 | printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); | |
c24ff998 | 292 | return printed + fprintf(fp, "): "); |
60c907ab ACM |
293 | } |
294 | ||
752fde44 ACM |
295 | struct thread_trace { |
296 | u64 entry_time; | |
297 | u64 exit_time; | |
298 | bool entry_pending; | |
efd5745e | 299 | unsigned long nr_events; |
752fde44 | 300 | char *entry_str; |
1302d88e | 301 | double runtime_ms; |
752fde44 ACM |
302 | }; |
303 | ||
304 | static struct thread_trace *thread_trace__new(void) | |
305 | { | |
306 | return zalloc(sizeof(struct thread_trace)); | |
307 | } | |
308 | ||
c24ff998 | 309 | static struct thread_trace *thread__trace(struct thread *thread, FILE *fp) |
752fde44 | 310 | { |
efd5745e ACM |
311 | struct thread_trace *ttrace; |
312 | ||
752fde44 ACM |
313 | if (thread == NULL) |
314 | goto fail; | |
315 | ||
316 | if (thread->priv == NULL) | |
317 | thread->priv = thread_trace__new(); | |
efd5745e | 318 | |
752fde44 ACM |
319 | if (thread->priv == NULL) |
320 | goto fail; | |
321 | ||
efd5745e ACM |
322 | ttrace = thread->priv; |
323 | ++ttrace->nr_events; | |
324 | ||
325 | return ttrace; | |
752fde44 | 326 | fail: |
c24ff998 | 327 | color_fprintf(fp, PERF_COLOR_RED, |
752fde44 ACM |
328 | "WARNING: not enough memory, dropping samples!\n"); |
329 | return NULL; | |
330 | } | |
331 | ||
514f1c67 | 332 | struct trace { |
c24ff998 | 333 | struct perf_tool tool; |
514f1c67 ACM |
334 | int audit_machine; |
335 | struct { | |
336 | int max; | |
337 | struct syscall *table; | |
338 | } syscalls; | |
339 | struct perf_record_opts opts; | |
752fde44 ACM |
340 | struct machine host; |
341 | u64 base_time; | |
c24ff998 | 342 | FILE *output; |
efd5745e | 343 | unsigned long nr_events; |
b059efdf ACM |
344 | struct strlist *ev_qualifier; |
345 | bool not_ev_qualifier; | |
bdc89661 DA |
346 | struct intlist *tid_list; |
347 | struct intlist *pid_list; | |
1302d88e | 348 | bool sched; |
752fde44 | 349 | bool multiple_threads; |
ae9ed035 | 350 | double duration_filter; |
1302d88e | 351 | double runtime_ms; |
514f1c67 ACM |
352 | }; |
353 | ||
ae9ed035 ACM |
354 | static bool trace__filter_duration(struct trace *trace, double t) |
355 | { | |
356 | return t < (trace->duration_filter * NSEC_PER_MSEC); | |
357 | } | |
358 | ||
752fde44 ACM |
359 | static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
360 | { | |
361 | double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; | |
362 | ||
60c907ab | 363 | return fprintf(fp, "%10.3f ", ts); |
752fde44 ACM |
364 | } |
365 | ||
f15eb531 NK |
366 | static bool done = false; |
367 | ||
368 | static void sig_handler(int sig __maybe_unused) | |
369 | { | |
370 | done = true; | |
371 | } | |
372 | ||
752fde44 | 373 | static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, |
60c907ab | 374 | u64 duration, u64 tstamp, FILE *fp) |
752fde44 ACM |
375 | { |
376 | size_t printed = trace__fprintf_tstamp(trace, tstamp, fp); | |
60c907ab | 377 | printed += fprintf_duration(duration, fp); |
752fde44 ACM |
378 | |
379 | if (trace->multiple_threads) | |
38051234 | 380 | printed += fprintf(fp, "%d ", thread->tid); |
752fde44 ACM |
381 | |
382 | return printed; | |
383 | } | |
384 | ||
c24ff998 ACM |
385 | static int trace__process_event(struct trace *trace, struct machine *machine, |
386 | union perf_event *event) | |
752fde44 ACM |
387 | { |
388 | int ret = 0; | |
389 | ||
390 | switch (event->header.type) { | |
391 | case PERF_RECORD_LOST: | |
c24ff998 | 392 | color_fprintf(trace->output, PERF_COLOR_RED, |
752fde44 ACM |
393 | "LOST %" PRIu64 " events!\n", event->lost.lost); |
394 | ret = machine__process_lost_event(machine, event); | |
395 | default: | |
396 | ret = machine__process_event(machine, event); | |
397 | break; | |
398 | } | |
399 | ||
400 | return ret; | |
401 | } | |
402 | ||
c24ff998 | 403 | static int trace__tool_process(struct perf_tool *tool, |
752fde44 ACM |
404 | union perf_event *event, |
405 | struct perf_sample *sample __maybe_unused, | |
406 | struct machine *machine) | |
407 | { | |
c24ff998 ACM |
408 | struct trace *trace = container_of(tool, struct trace, tool); |
409 | return trace__process_event(trace, machine, event); | |
752fde44 ACM |
410 | } |
411 | ||
412 | static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) | |
413 | { | |
414 | int err = symbol__init(); | |
415 | ||
416 | if (err) | |
417 | return err; | |
418 | ||
419 | machine__init(&trace->host, "", HOST_KERNEL_ID); | |
420 | machine__create_kernel_maps(&trace->host); | |
421 | ||
422 | if (perf_target__has_task(&trace->opts.target)) { | |
c24ff998 | 423 | err = perf_event__synthesize_thread_map(&trace->tool, evlist->threads, |
752fde44 ACM |
424 | trace__tool_process, |
425 | &trace->host); | |
426 | } else { | |
c24ff998 | 427 | err = perf_event__synthesize_threads(&trace->tool, trace__tool_process, |
752fde44 ACM |
428 | &trace->host); |
429 | } | |
430 | ||
431 | if (err) | |
432 | symbol__exit(); | |
433 | ||
434 | return err; | |
435 | } | |
436 | ||
13d4ff3e ACM |
437 | static int syscall__set_arg_fmts(struct syscall *sc) |
438 | { | |
439 | struct format_field *field; | |
440 | int idx = 0; | |
441 | ||
442 | sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *)); | |
443 | if (sc->arg_scnprintf == NULL) | |
444 | return -1; | |
445 | ||
446 | for (field = sc->tp_format->format.fields->next; field; field = field->next) { | |
beccb2b5 ACM |
447 | if (sc->fmt && sc->fmt->arg_scnprintf[idx]) |
448 | sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx]; | |
449 | else if (field->flags & FIELD_IS_POINTER) | |
13d4ff3e ACM |
450 | sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex; |
451 | ++idx; | |
452 | } | |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
514f1c67 ACM |
457 | static int trace__read_syscall_info(struct trace *trace, int id) |
458 | { | |
459 | char tp_name[128]; | |
460 | struct syscall *sc; | |
3a531260 ACM |
461 | const char *name = audit_syscall_to_name(id, trace->audit_machine); |
462 | ||
463 | if (name == NULL) | |
464 | return -1; | |
514f1c67 ACM |
465 | |
466 | if (id > trace->syscalls.max) { | |
467 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); | |
468 | ||
469 | if (nsyscalls == NULL) | |
470 | return -1; | |
471 | ||
472 | if (trace->syscalls.max != -1) { | |
473 | memset(nsyscalls + trace->syscalls.max + 1, 0, | |
474 | (id - trace->syscalls.max) * sizeof(*sc)); | |
475 | } else { | |
476 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); | |
477 | } | |
478 | ||
479 | trace->syscalls.table = nsyscalls; | |
480 | trace->syscalls.max = id; | |
481 | } | |
482 | ||
483 | sc = trace->syscalls.table + id; | |
3a531260 | 484 | sc->name = name; |
2ae3a312 | 485 | |
b059efdf ACM |
486 | if (trace->ev_qualifier) { |
487 | bool in = strlist__find(trace->ev_qualifier, name) != NULL; | |
488 | ||
489 | if (!(in ^ trace->not_ev_qualifier)) { | |
490 | sc->filtered = true; | |
491 | /* | |
492 | * No need to do read tracepoint information since this will be | |
493 | * filtered out. | |
494 | */ | |
495 | return 0; | |
496 | } | |
2ae3a312 ACM |
497 | } |
498 | ||
3a531260 | 499 | sc->fmt = syscall_fmt__find(sc->name); |
514f1c67 | 500 | |
aec1930b | 501 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
514f1c67 | 502 | sc->tp_format = event_format__new("syscalls", tp_name); |
aec1930b ACM |
503 | |
504 | if (sc->tp_format == NULL && sc->fmt && sc->fmt->alias) { | |
505 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); | |
506 | sc->tp_format = event_format__new("syscalls", tp_name); | |
507 | } | |
514f1c67 | 508 | |
13d4ff3e ACM |
509 | if (sc->tp_format == NULL) |
510 | return -1; | |
511 | ||
512 | return syscall__set_arg_fmts(sc); | |
514f1c67 ACM |
513 | } |
514 | ||
752fde44 ACM |
515 | static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, |
516 | unsigned long *args) | |
514f1c67 ACM |
517 | { |
518 | int i = 0; | |
519 | size_t printed = 0; | |
520 | ||
521 | if (sc->tp_format != NULL) { | |
522 | struct format_field *field; | |
6e7eeb51 ACM |
523 | u8 mask = 0, bit = 1; |
524 | ||
525 | for (field = sc->tp_format->format.fields->next; field; | |
526 | field = field->next, ++i, bit <<= 1) { | |
527 | if (mask & bit) | |
528 | continue; | |
514f1c67 | 529 | |
752fde44 | 530 | printed += scnprintf(bf + printed, size - printed, |
13d4ff3e ACM |
531 | "%s%s: ", printed ? ", " : "", field->name); |
532 | ||
6e7eeb51 ACM |
533 | if (sc->arg_scnprintf && sc->arg_scnprintf[i]) { |
534 | printed += sc->arg_scnprintf[i](bf + printed, size - printed, | |
535 | args[i], &mask); | |
536 | } else { | |
13d4ff3e ACM |
537 | printed += scnprintf(bf + printed, size - printed, |
538 | "%ld", args[i]); | |
6e7eeb51 | 539 | } |
514f1c67 ACM |
540 | } |
541 | } else { | |
542 | while (i < 6) { | |
752fde44 ACM |
543 | printed += scnprintf(bf + printed, size - printed, |
544 | "%sarg%d: %ld", | |
545 | printed ? ", " : "", i, args[i]); | |
514f1c67 ACM |
546 | ++i; |
547 | } | |
548 | } | |
549 | ||
550 | return printed; | |
551 | } | |
552 | ||
ba3d7dee ACM |
553 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
554 | struct perf_sample *sample); | |
555 | ||
556 | static struct syscall *trace__syscall_info(struct trace *trace, | |
557 | struct perf_evsel *evsel, | |
558 | struct perf_sample *sample) | |
559 | { | |
560 | int id = perf_evsel__intval(evsel, sample, "id"); | |
561 | ||
562 | if (id < 0) { | |
adaa18bf ACM |
563 | |
564 | /* | |
565 | * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried | |
566 | * before that, leaving at a higher verbosity level till that is | |
567 | * explained. Reproduced with plain ftrace with: | |
568 | * | |
569 | * echo 1 > /t/events/raw_syscalls/sys_exit/enable | |
570 | * grep "NR -1 " /t/trace_pipe | |
571 | * | |
572 | * After generating some load on the machine. | |
573 | */ | |
574 | if (verbose > 1) { | |
575 | static u64 n; | |
576 | fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n", | |
577 | id, perf_evsel__name(evsel), ++n); | |
578 | } | |
ba3d7dee ACM |
579 | return NULL; |
580 | } | |
581 | ||
582 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && | |
583 | trace__read_syscall_info(trace, id)) | |
584 | goto out_cant_read; | |
585 | ||
586 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) | |
587 | goto out_cant_read; | |
588 | ||
589 | return &trace->syscalls.table[id]; | |
590 | ||
591 | out_cant_read: | |
7c304ee0 ACM |
592 | if (verbose) { |
593 | fprintf(trace->output, "Problems reading syscall %d", id); | |
594 | if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL) | |
595 | fprintf(trace->output, "(%s)", trace->syscalls.table[id].name); | |
596 | fputs(" information\n", trace->output); | |
597 | } | |
ba3d7dee ACM |
598 | return NULL; |
599 | } | |
600 | ||
601 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, | |
602 | struct perf_sample *sample) | |
603 | { | |
752fde44 | 604 | char *msg; |
ba3d7dee | 605 | void *args; |
752fde44 | 606 | size_t printed = 0; |
2ae3a312 | 607 | struct thread *thread; |
ba3d7dee | 608 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
2ae3a312 ACM |
609 | struct thread_trace *ttrace; |
610 | ||
611 | if (sc == NULL) | |
612 | return -1; | |
ba3d7dee | 613 | |
2ae3a312 ACM |
614 | if (sc->filtered) |
615 | return 0; | |
616 | ||
314add6b AH |
617 | thread = machine__findnew_thread(&trace->host, sample->pid, |
618 | sample->tid); | |
c24ff998 | 619 | ttrace = thread__trace(thread, trace->output); |
2ae3a312 | 620 | if (ttrace == NULL) |
ba3d7dee ACM |
621 | return -1; |
622 | ||
623 | args = perf_evsel__rawptr(evsel, sample, "args"); | |
624 | if (args == NULL) { | |
c24ff998 | 625 | fprintf(trace->output, "Problems reading syscall arguments\n"); |
ba3d7dee ACM |
626 | return -1; |
627 | } | |
628 | ||
752fde44 ACM |
629 | ttrace = thread->priv; |
630 | ||
631 | if (ttrace->entry_str == NULL) { | |
632 | ttrace->entry_str = malloc(1024); | |
633 | if (!ttrace->entry_str) | |
634 | return -1; | |
635 | } | |
636 | ||
637 | ttrace->entry_time = sample->time; | |
638 | msg = ttrace->entry_str; | |
639 | printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name); | |
640 | ||
641 | printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args); | |
642 | ||
643 | if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) { | |
ae9ed035 | 644 | if (!trace->duration_filter) { |
c24ff998 ACM |
645 | trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output); |
646 | fprintf(trace->output, "%-70s\n", ttrace->entry_str); | |
ae9ed035 | 647 | } |
752fde44 ACM |
648 | } else |
649 | ttrace->entry_pending = true; | |
ba3d7dee ACM |
650 | |
651 | return 0; | |
652 | } | |
653 | ||
654 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, | |
655 | struct perf_sample *sample) | |
656 | { | |
657 | int ret; | |
60c907ab | 658 | u64 duration = 0; |
2ae3a312 | 659 | struct thread *thread; |
ba3d7dee | 660 | struct syscall *sc = trace__syscall_info(trace, evsel, sample); |
2ae3a312 ACM |
661 | struct thread_trace *ttrace; |
662 | ||
663 | if (sc == NULL) | |
664 | return -1; | |
ba3d7dee | 665 | |
2ae3a312 ACM |
666 | if (sc->filtered) |
667 | return 0; | |
668 | ||
314add6b AH |
669 | thread = machine__findnew_thread(&trace->host, sample->pid, |
670 | sample->tid); | |
c24ff998 | 671 | ttrace = thread__trace(thread, trace->output); |
2ae3a312 | 672 | if (ttrace == NULL) |
ba3d7dee ACM |
673 | return -1; |
674 | ||
675 | ret = perf_evsel__intval(evsel, sample, "ret"); | |
676 | ||
752fde44 ACM |
677 | ttrace = thread->priv; |
678 | ||
679 | ttrace->exit_time = sample->time; | |
680 | ||
ae9ed035 | 681 | if (ttrace->entry_time) { |
60c907ab | 682 | duration = sample->time - ttrace->entry_time; |
ae9ed035 ACM |
683 | if (trace__filter_duration(trace, duration)) |
684 | goto out; | |
685 | } else if (trace->duration_filter) | |
686 | goto out; | |
60c907ab | 687 | |
c24ff998 | 688 | trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output); |
752fde44 ACM |
689 | |
690 | if (ttrace->entry_pending) { | |
c24ff998 | 691 | fprintf(trace->output, "%-70s", ttrace->entry_str); |
752fde44 | 692 | } else { |
c24ff998 ACM |
693 | fprintf(trace->output, " ... ["); |
694 | color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); | |
695 | fprintf(trace->output, "]: %s()", sc->name); | |
752fde44 ACM |
696 | } |
697 | ||
da3c9a44 ACM |
698 | if (sc->fmt == NULL) { |
699 | signed_print: | |
700 | fprintf(trace->output, ") = %d", ret); | |
701 | } else if (ret < 0 && sc->fmt->errmsg) { | |
ba3d7dee ACM |
702 | char bf[256]; |
703 | const char *emsg = strerror_r(-ret, bf, sizeof(bf)), | |
704 | *e = audit_errno_to_name(-ret); | |
705 | ||
c24ff998 | 706 | fprintf(trace->output, ") = -1 %s %s", e, emsg); |
da3c9a44 | 707 | } else if (ret == 0 && sc->fmt->timeout) |
c24ff998 | 708 | fprintf(trace->output, ") = 0 Timeout"); |
04b34729 ACM |
709 | else if (sc->fmt->hexret) |
710 | fprintf(trace->output, ") = %#x", ret); | |
ba3d7dee | 711 | else |
da3c9a44 | 712 | goto signed_print; |
ba3d7dee | 713 | |
c24ff998 | 714 | fputc('\n', trace->output); |
ae9ed035 | 715 | out: |
752fde44 ACM |
716 | ttrace->entry_pending = false; |
717 | ||
ba3d7dee ACM |
718 | return 0; |
719 | } | |
720 | ||
1302d88e ACM |
721 | static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel, |
722 | struct perf_sample *sample) | |
723 | { | |
724 | u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); | |
725 | double runtime_ms = (double)runtime / NSEC_PER_MSEC; | |
314add6b AH |
726 | struct thread *thread = machine__findnew_thread(&trace->host, |
727 | sample->pid, | |
728 | sample->tid); | |
c24ff998 | 729 | struct thread_trace *ttrace = thread__trace(thread, trace->output); |
1302d88e ACM |
730 | |
731 | if (ttrace == NULL) | |
732 | goto out_dump; | |
733 | ||
734 | ttrace->runtime_ms += runtime_ms; | |
735 | trace->runtime_ms += runtime_ms; | |
736 | return 0; | |
737 | ||
738 | out_dump: | |
c24ff998 | 739 | fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", |
1302d88e ACM |
740 | evsel->name, |
741 | perf_evsel__strval(evsel, sample, "comm"), | |
742 | (pid_t)perf_evsel__intval(evsel, sample, "pid"), | |
743 | runtime, | |
744 | perf_evsel__intval(evsel, sample, "vruntime")); | |
745 | return 0; | |
746 | } | |
747 | ||
bdc89661 DA |
748 | static bool skip_sample(struct trace *trace, struct perf_sample *sample) |
749 | { | |
750 | if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) || | |
751 | (trace->tid_list && intlist__find(trace->tid_list, sample->tid))) | |
752 | return false; | |
753 | ||
754 | if (trace->pid_list || trace->tid_list) | |
755 | return true; | |
756 | ||
757 | return false; | |
758 | } | |
759 | ||
6810fc91 DA |
760 | static int trace__process_sample(struct perf_tool *tool, |
761 | union perf_event *event __maybe_unused, | |
762 | struct perf_sample *sample, | |
763 | struct perf_evsel *evsel, | |
764 | struct machine *machine __maybe_unused) | |
765 | { | |
766 | struct trace *trace = container_of(tool, struct trace, tool); | |
767 | int err = 0; | |
768 | ||
769 | tracepoint_handler handler = evsel->handler.func; | |
770 | ||
bdc89661 DA |
771 | if (skip_sample(trace, sample)) |
772 | return 0; | |
773 | ||
6810fc91 DA |
774 | if (trace->base_time == 0) |
775 | trace->base_time = sample->time; | |
776 | ||
777 | if (handler) | |
778 | handler(trace, evsel, sample); | |
779 | ||
780 | return err; | |
781 | } | |
782 | ||
783 | static bool | |
784 | perf_session__has_tp(struct perf_session *session, const char *name) | |
785 | { | |
786 | struct perf_evsel *evsel; | |
787 | ||
788 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, name); | |
789 | ||
790 | return evsel != NULL; | |
791 | } | |
792 | ||
bdc89661 DA |
793 | static int parse_target_str(struct trace *trace) |
794 | { | |
795 | if (trace->opts.target.pid) { | |
796 | trace->pid_list = intlist__new(trace->opts.target.pid); | |
797 | if (trace->pid_list == NULL) { | |
798 | pr_err("Error parsing process id string\n"); | |
799 | return -EINVAL; | |
800 | } | |
801 | } | |
802 | ||
803 | if (trace->opts.target.tid) { | |
804 | trace->tid_list = intlist__new(trace->opts.target.tid); | |
805 | if (trace->tid_list == NULL) { | |
806 | pr_err("Error parsing thread id string\n"); | |
807 | return -EINVAL; | |
808 | } | |
809 | } | |
810 | ||
811 | return 0; | |
812 | } | |
813 | ||
f15eb531 | 814 | static int trace__run(struct trace *trace, int argc, const char **argv) |
514f1c67 | 815 | { |
334fe7a3 | 816 | struct perf_evlist *evlist = perf_evlist__new(); |
ba3d7dee | 817 | struct perf_evsel *evsel; |
efd5745e ACM |
818 | int err = -1, i; |
819 | unsigned long before; | |
f15eb531 | 820 | const bool forks = argc > 0; |
514f1c67 ACM |
821 | |
822 | if (evlist == NULL) { | |
c24ff998 | 823 | fprintf(trace->output, "Not enough memory to run!\n"); |
514f1c67 ACM |
824 | goto out; |
825 | } | |
826 | ||
39876e7d ACM |
827 | if (perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_enter", trace__sys_enter) || |
828 | perf_evlist__add_newtp(evlist, "raw_syscalls", "sys_exit", trace__sys_exit)) { | |
c24ff998 | 829 | fprintf(trace->output, "Couldn't read the raw_syscalls tracepoints information!\n"); |
514f1c67 ACM |
830 | goto out_delete_evlist; |
831 | } | |
832 | ||
1302d88e ACM |
833 | if (trace->sched && |
834 | perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", | |
835 | trace__sched_stat_runtime)) { | |
c24ff998 | 836 | fprintf(trace->output, "Couldn't read the sched_stat_runtime tracepoint information!\n"); |
1302d88e ACM |
837 | goto out_delete_evlist; |
838 | } | |
839 | ||
514f1c67 ACM |
840 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
841 | if (err < 0) { | |
c24ff998 | 842 | fprintf(trace->output, "Problems parsing the target to trace, check your options!\n"); |
514f1c67 ACM |
843 | goto out_delete_evlist; |
844 | } | |
845 | ||
752fde44 ACM |
846 | err = trace__symbols_init(trace, evlist); |
847 | if (err < 0) { | |
c24ff998 | 848 | fprintf(trace->output, "Problems initializing symbol libraries!\n"); |
3beb0861 | 849 | goto out_delete_maps; |
752fde44 ACM |
850 | } |
851 | ||
f77a9518 | 852 | perf_evlist__config(evlist, &trace->opts); |
514f1c67 | 853 | |
f15eb531 NK |
854 | signal(SIGCHLD, sig_handler); |
855 | signal(SIGINT, sig_handler); | |
856 | ||
857 | if (forks) { | |
6ef73ec4 | 858 | err = perf_evlist__prepare_workload(evlist, &trace->opts.target, |
55e162ea | 859 | argv, false, false); |
f15eb531 | 860 | if (err < 0) { |
c24ff998 | 861 | fprintf(trace->output, "Couldn't run the workload!\n"); |
3beb0861 | 862 | goto out_delete_maps; |
f15eb531 NK |
863 | } |
864 | } | |
865 | ||
514f1c67 ACM |
866 | err = perf_evlist__open(evlist); |
867 | if (err < 0) { | |
c24ff998 | 868 | fprintf(trace->output, "Couldn't create the events: %s\n", strerror(errno)); |
3beb0861 | 869 | goto out_delete_maps; |
514f1c67 ACM |
870 | } |
871 | ||
872 | err = perf_evlist__mmap(evlist, UINT_MAX, false); | |
873 | if (err < 0) { | |
c24ff998 | 874 | fprintf(trace->output, "Couldn't mmap the events: %s\n", strerror(errno)); |
3beb0861 | 875 | goto out_close_evlist; |
514f1c67 ACM |
876 | } |
877 | ||
878 | perf_evlist__enable(evlist); | |
f15eb531 NK |
879 | |
880 | if (forks) | |
881 | perf_evlist__start_workload(evlist); | |
882 | ||
752fde44 | 883 | trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1; |
514f1c67 | 884 | again: |
efd5745e | 885 | before = trace->nr_events; |
514f1c67 ACM |
886 | |
887 | for (i = 0; i < evlist->nr_mmaps; i++) { | |
888 | union perf_event *event; | |
889 | ||
890 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { | |
891 | const u32 type = event->header.type; | |
ba3d7dee | 892 | tracepoint_handler handler; |
514f1c67 | 893 | struct perf_sample sample; |
514f1c67 | 894 | |
efd5745e | 895 | ++trace->nr_events; |
514f1c67 | 896 | |
514f1c67 ACM |
897 | err = perf_evlist__parse_sample(evlist, event, &sample); |
898 | if (err) { | |
c24ff998 | 899 | fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err); |
514f1c67 ACM |
900 | continue; |
901 | } | |
902 | ||
752fde44 ACM |
903 | if (trace->base_time == 0) |
904 | trace->base_time = sample.time; | |
905 | ||
906 | if (type != PERF_RECORD_SAMPLE) { | |
c24ff998 | 907 | trace__process_event(trace, &trace->host, event); |
752fde44 ACM |
908 | continue; |
909 | } | |
910 | ||
514f1c67 ACM |
911 | evsel = perf_evlist__id2evsel(evlist, sample.id); |
912 | if (evsel == NULL) { | |
c24ff998 | 913 | fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample.id); |
514f1c67 ACM |
914 | continue; |
915 | } | |
916 | ||
fc551f8d | 917 | if (sample.raw_data == NULL) { |
c24ff998 | 918 | fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", |
fc551f8d ACM |
919 | perf_evsel__name(evsel), sample.tid, |
920 | sample.cpu, sample.raw_size); | |
921 | continue; | |
922 | } | |
923 | ||
ba3d7dee ACM |
924 | handler = evsel->handler.func; |
925 | handler(trace, evsel, &sample); | |
514f1c67 ACM |
926 | } |
927 | } | |
928 | ||
efd5745e | 929 | if (trace->nr_events == before) { |
f15eb531 | 930 | if (done) |
3beb0861 | 931 | goto out_unmap_evlist; |
f15eb531 | 932 | |
514f1c67 | 933 | poll(evlist->pollfd, evlist->nr_fds, -1); |
f15eb531 NK |
934 | } |
935 | ||
936 | if (done) | |
937 | perf_evlist__disable(evlist); | |
514f1c67 ACM |
938 | |
939 | goto again; | |
940 | ||
3beb0861 NK |
941 | out_unmap_evlist: |
942 | perf_evlist__munmap(evlist); | |
943 | out_close_evlist: | |
944 | perf_evlist__close(evlist); | |
945 | out_delete_maps: | |
946 | perf_evlist__delete_maps(evlist); | |
514f1c67 ACM |
947 | out_delete_evlist: |
948 | perf_evlist__delete(evlist); | |
949 | out: | |
950 | return err; | |
951 | } | |
952 | ||
6810fc91 DA |
953 | static int trace__replay(struct trace *trace) |
954 | { | |
955 | const struct perf_evsel_str_handler handlers[] = { | |
956 | { "raw_syscalls:sys_enter", trace__sys_enter, }, | |
957 | { "raw_syscalls:sys_exit", trace__sys_exit, }, | |
958 | }; | |
959 | ||
960 | struct perf_session *session; | |
961 | int err = -1; | |
962 | ||
963 | trace->tool.sample = trace__process_sample; | |
964 | trace->tool.mmap = perf_event__process_mmap; | |
965 | trace->tool.comm = perf_event__process_comm; | |
966 | trace->tool.exit = perf_event__process_exit; | |
967 | trace->tool.fork = perf_event__process_fork; | |
968 | trace->tool.attr = perf_event__process_attr; | |
969 | trace->tool.tracing_data = perf_event__process_tracing_data; | |
970 | trace->tool.build_id = perf_event__process_build_id; | |
971 | ||
972 | trace->tool.ordered_samples = true; | |
973 | trace->tool.ordering_requires_timestamps = true; | |
974 | ||
975 | /* add tid to output */ | |
976 | trace->multiple_threads = true; | |
977 | ||
978 | if (symbol__init() < 0) | |
979 | return -1; | |
980 | ||
981 | session = perf_session__new(input_name, O_RDONLY, 0, false, | |
982 | &trace->tool); | |
983 | if (session == NULL) | |
984 | return -ENOMEM; | |
985 | ||
986 | err = perf_session__set_tracepoints_handlers(session, handlers); | |
987 | if (err) | |
988 | goto out; | |
989 | ||
990 | if (!perf_session__has_tp(session, "raw_syscalls:sys_enter")) { | |
991 | pr_err("Data file does not have raw_syscalls:sys_enter events\n"); | |
992 | goto out; | |
993 | } | |
994 | ||
995 | if (!perf_session__has_tp(session, "raw_syscalls:sys_exit")) { | |
996 | pr_err("Data file does not have raw_syscalls:sys_exit events\n"); | |
997 | goto out; | |
998 | } | |
999 | ||
bdc89661 DA |
1000 | err = parse_target_str(trace); |
1001 | if (err != 0) | |
1002 | goto out; | |
1003 | ||
6810fc91 DA |
1004 | setup_pager(); |
1005 | ||
1006 | err = perf_session__process_events(session, &trace->tool); | |
1007 | if (err) | |
1008 | pr_err("Failed to process events, error %d", err); | |
1009 | ||
1010 | out: | |
1011 | perf_session__delete(session); | |
1012 | ||
1013 | return err; | |
1014 | } | |
1015 | ||
1302d88e ACM |
1016 | static size_t trace__fprintf_threads_header(FILE *fp) |
1017 | { | |
1018 | size_t printed; | |
1019 | ||
1020 | printed = fprintf(fp, "\n _____________________________________________________________________\n"); | |
1021 | printed += fprintf(fp," __) Summary of events (__\n\n"); | |
1022 | printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n"); | |
1023 | printed += fprintf(fp," _____________________________________________________________________\n\n"); | |
1024 | ||
1025 | return printed; | |
1026 | } | |
1027 | ||
1028 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) | |
1029 | { | |
1030 | size_t printed = trace__fprintf_threads_header(fp); | |
1031 | struct rb_node *nd; | |
1032 | ||
1033 | for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) { | |
1034 | struct thread *thread = rb_entry(nd, struct thread, rb_node); | |
1035 | struct thread_trace *ttrace = thread->priv; | |
1036 | const char *color; | |
1037 | double ratio; | |
1038 | ||
1039 | if (ttrace == NULL) | |
1040 | continue; | |
1041 | ||
1042 | ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; | |
1043 | ||
1044 | color = PERF_COLOR_NORMAL; | |
1045 | if (ratio > 50.0) | |
1046 | color = PERF_COLOR_RED; | |
1047 | else if (ratio > 25.0) | |
1048 | color = PERF_COLOR_GREEN; | |
1049 | else if (ratio > 5.0) | |
1050 | color = PERF_COLOR_YELLOW; | |
1051 | ||
1052 | printed += color_fprintf(fp, color, "%20s", thread->comm); | |
38051234 | 1053 | printed += fprintf(fp, " - %-5d :%11lu [", thread->tid, ttrace->nr_events); |
1302d88e ACM |
1054 | printed += color_fprintf(fp, color, "%5.1f%%", ratio); |
1055 | printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms); | |
1056 | } | |
1057 | ||
1058 | return printed; | |
1059 | } | |
1060 | ||
ae9ed035 ACM |
1061 | static int trace__set_duration(const struct option *opt, const char *str, |
1062 | int unset __maybe_unused) | |
1063 | { | |
1064 | struct trace *trace = opt->value; | |
1065 | ||
1066 | trace->duration_filter = atof(str); | |
1067 | return 0; | |
1068 | } | |
1069 | ||
c24ff998 ACM |
1070 | static int trace__open_output(struct trace *trace, const char *filename) |
1071 | { | |
1072 | struct stat st; | |
1073 | ||
1074 | if (!stat(filename, &st) && st.st_size) { | |
1075 | char oldname[PATH_MAX]; | |
1076 | ||
1077 | scnprintf(oldname, sizeof(oldname), "%s.old", filename); | |
1078 | unlink(oldname); | |
1079 | rename(filename, oldname); | |
1080 | } | |
1081 | ||
1082 | trace->output = fopen(filename, "w"); | |
1083 | ||
1084 | return trace->output == NULL ? -errno : 0; | |
1085 | } | |
1086 | ||
514f1c67 ACM |
1087 | int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused) |
1088 | { | |
1089 | const char * const trace_usage[] = { | |
f15eb531 NK |
1090 | "perf trace [<options>] [<command>]", |
1091 | "perf trace [<options>] -- <command> [<options>]", | |
514f1c67 ACM |
1092 | NULL |
1093 | }; | |
1094 | struct trace trace = { | |
1095 | .audit_machine = audit_detect_machine(), | |
1096 | .syscalls = { | |
1097 | . max = -1, | |
1098 | }, | |
1099 | .opts = { | |
1100 | .target = { | |
1101 | .uid = UINT_MAX, | |
1102 | .uses_mmap = true, | |
1103 | }, | |
1104 | .user_freq = UINT_MAX, | |
1105 | .user_interval = ULLONG_MAX, | |
1106 | .no_delay = true, | |
1107 | .mmap_pages = 1024, | |
1108 | }, | |
c24ff998 | 1109 | .output = stdout, |
514f1c67 | 1110 | }; |
c24ff998 | 1111 | const char *output_name = NULL; |
2ae3a312 | 1112 | const char *ev_qualifier_str = NULL; |
514f1c67 | 1113 | const struct option trace_options[] = { |
2ae3a312 ACM |
1114 | OPT_STRING('e', "expr", &ev_qualifier_str, "expr", |
1115 | "list of events to trace"), | |
c24ff998 | 1116 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
6810fc91 | 1117 | OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"), |
514f1c67 ACM |
1118 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", |
1119 | "trace events on existing process id"), | |
ac9be8ee | 1120 | OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", |
514f1c67 | 1121 | "trace events on existing thread id"), |
ac9be8ee | 1122 | OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, |
514f1c67 | 1123 | "system-wide collection from all CPUs"), |
ac9be8ee | 1124 | OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", |
514f1c67 | 1125 | "list of cpus to monitor"), |
6810fc91 | 1126 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, |
514f1c67 | 1127 | "child tasks do not inherit counters"), |
ac9be8ee | 1128 | OPT_UINTEGER('m', "mmap-pages", &trace.opts.mmap_pages, |
514f1c67 | 1129 | "number of mmap data pages"), |
ac9be8ee | 1130 | OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", |
514f1c67 | 1131 | "user to profile"), |
ae9ed035 ACM |
1132 | OPT_CALLBACK(0, "duration", &trace, "float", |
1133 | "show only events with duration > N.M ms", | |
1134 | trace__set_duration), | |
1302d88e | 1135 | OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), |
7c304ee0 | 1136 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
514f1c67 ACM |
1137 | OPT_END() |
1138 | }; | |
1139 | int err; | |
32caf0d1 | 1140 | char bf[BUFSIZ]; |
514f1c67 ACM |
1141 | |
1142 | argc = parse_options(argc, argv, trace_options, trace_usage, 0); | |
514f1c67 | 1143 | |
c24ff998 ACM |
1144 | if (output_name != NULL) { |
1145 | err = trace__open_output(&trace, output_name); | |
1146 | if (err < 0) { | |
1147 | perror("failed to create output file"); | |
1148 | goto out; | |
1149 | } | |
1150 | } | |
1151 | ||
2ae3a312 | 1152 | if (ev_qualifier_str != NULL) { |
b059efdf ACM |
1153 | const char *s = ev_qualifier_str; |
1154 | ||
1155 | trace.not_ev_qualifier = *s == '!'; | |
1156 | if (trace.not_ev_qualifier) | |
1157 | ++s; | |
1158 | trace.ev_qualifier = strlist__new(true, s); | |
2ae3a312 | 1159 | if (trace.ev_qualifier == NULL) { |
c24ff998 ACM |
1160 | fputs("Not enough memory to parse event qualifier", |
1161 | trace.output); | |
1162 | err = -ENOMEM; | |
1163 | goto out_close; | |
2ae3a312 ACM |
1164 | } |
1165 | } | |
1166 | ||
32caf0d1 NK |
1167 | err = perf_target__validate(&trace.opts.target); |
1168 | if (err) { | |
1169 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); | |
c24ff998 ACM |
1170 | fprintf(trace.output, "%s", bf); |
1171 | goto out_close; | |
32caf0d1 NK |
1172 | } |
1173 | ||
514f1c67 ACM |
1174 | err = perf_target__parse_uid(&trace.opts.target); |
1175 | if (err) { | |
514f1c67 | 1176 | perf_target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
c24ff998 ACM |
1177 | fprintf(trace.output, "%s", bf); |
1178 | goto out_close; | |
514f1c67 ACM |
1179 | } |
1180 | ||
f15eb531 | 1181 | if (!argc && perf_target__none(&trace.opts.target)) |
ee76120e NK |
1182 | trace.opts.target.system_wide = true; |
1183 | ||
6810fc91 DA |
1184 | if (input_name) |
1185 | err = trace__replay(&trace); | |
1186 | else | |
1187 | err = trace__run(&trace, argc, argv); | |
1302d88e ACM |
1188 | |
1189 | if (trace.sched && !err) | |
c24ff998 | 1190 | trace__fprintf_thread_summary(&trace, trace.output); |
1302d88e | 1191 | |
c24ff998 ACM |
1192 | out_close: |
1193 | if (output_name != NULL) | |
1194 | fclose(trace.output); | |
1195 | out: | |
1302d88e | 1196 | return err; |
514f1c67 | 1197 | } |