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