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