blob: 93b80f12f35e6797f76cfe201e89fbae82016b36 [file] [log] [blame]
Arnaldo Carvalho de Meloa598bb52015-08-28 12:02:37 -03001/*
2 * builtin-trace.c
3 *
4 * Builtin 'trace' command:
5 *
6 * Display a continuously updated trace of any workload, CPU, specific PID,
7 * system wide, etc. Default format is loosely strace like, but any other
8 * event may be specified using --event.
9 *
10 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
11 *
12 * Initially based on the 'trace' prototype by Thomas Gleixner:
13 *
14 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
15 *
16 * Released under the GPL v2. (and only v2, not any later version)
17 */
18
Robert Richter4e319022013-06-11 17:29:18 +020019#include <traceevent/event-parse.h>
Jiri Olsa988bdb32015-09-02 09:56:35 +020020#include <api/fs/tracing_path.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030021#include "builtin.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030022#include "util/color.h"
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -030023#include "util/debug.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030024#include "util/evlist.h"
Arnaldo Carvalho de Melo005438a82015-07-20 12:02:09 -030025#include "util/exec_cmd.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030026#include "util/machine.h"
David Ahern6810fc92013-08-28 22:29:52 -060027#include "util/session.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030028#include "util/thread.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030029#include "util/parse-options.h"
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -030030#include "util/strlist.h"
David Ahernbdc89662013-08-28 22:29:53 -060031#include "util/intlist.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030032#include "util/thread_map.h"
David Ahernbf2575c2013-10-08 21:26:53 -060033#include "util/stat.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010034#include "trace-event.h"
David Ahern9aca7f12013-12-04 19:41:39 -070035#include "util/parse-events.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030036
37#include <libaudit.h>
38#include <stdlib.h>
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -030039#include <sys/mman.h>
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -030040#include <linux/futex.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020041#include <linux/err.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030042
Ingo Molnar456857b2013-09-12 15:29:00 +020043/* For older distros: */
44#ifndef MAP_STACK
45# define MAP_STACK 0x20000
46#endif
47
48#ifndef MADV_HWPOISON
49# define MADV_HWPOISON 100
Arnaldo Carvalho de Meloa598bb52015-08-28 12:02:37 -030050
Ingo Molnar456857b2013-09-12 15:29:00 +020051#endif
52
53#ifndef MADV_MERGEABLE
54# define MADV_MERGEABLE 12
55#endif
56
57#ifndef MADV_UNMERGEABLE
58# define MADV_UNMERGEABLE 13
59#endif
60
Ben Hutchings79d26a62014-02-06 01:00:35 +000061#ifndef EFD_SEMAPHORE
62# define EFD_SEMAPHORE 1
63#endif
64
Arnaldo Carvalho de Meloc188e7a2015-05-14 17:39:03 -030065#ifndef EFD_NONBLOCK
66# define EFD_NONBLOCK 00004000
67#endif
68
69#ifndef EFD_CLOEXEC
70# define EFD_CLOEXEC 02000000
71#endif
72
73#ifndef O_CLOEXEC
74# define O_CLOEXEC 02000000
75#endif
76
77#ifndef SOCK_DCCP
78# define SOCK_DCCP 6
79#endif
80
81#ifndef SOCK_CLOEXEC
82# define SOCK_CLOEXEC 02000000
83#endif
84
85#ifndef SOCK_NONBLOCK
86# define SOCK_NONBLOCK 00004000
87#endif
88
89#ifndef MSG_CMSG_CLOEXEC
90# define MSG_CMSG_CLOEXEC 0x40000000
91#endif
92
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -030093#ifndef PERF_FLAG_FD_NO_GROUP
94# define PERF_FLAG_FD_NO_GROUP (1UL << 0)
95#endif
96
97#ifndef PERF_FLAG_FD_OUTPUT
98# define PERF_FLAG_FD_OUTPUT (1UL << 1)
99#endif
100
101#ifndef PERF_FLAG_PID_CGROUP
102# define PERF_FLAG_PID_CGROUP (1UL << 2) /* pid=cgroup id, per-cpu mode only */
103#endif
104
105#ifndef PERF_FLAG_FD_CLOEXEC
106# define PERF_FLAG_FD_CLOEXEC (1UL << 3) /* O_CLOEXEC */
107#endif
108
109
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300110struct tp_field {
111 int offset;
112 union {
113 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
114 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
115 };
116};
117
118#define TP_UINT_FIELD(bits) \
119static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
120{ \
David Ahern55d43bc2015-02-19 15:00:22 -0500121 u##bits value; \
122 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
123 return value; \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300124}
125
126TP_UINT_FIELD(8);
127TP_UINT_FIELD(16);
128TP_UINT_FIELD(32);
129TP_UINT_FIELD(64);
130
131#define TP_UINT_FIELD__SWAPPED(bits) \
132static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
133{ \
David Ahern55d43bc2015-02-19 15:00:22 -0500134 u##bits value; \
135 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300136 return bswap_##bits(value);\
137}
138
139TP_UINT_FIELD__SWAPPED(16);
140TP_UINT_FIELD__SWAPPED(32);
141TP_UINT_FIELD__SWAPPED(64);
142
143static int tp_field__init_uint(struct tp_field *field,
144 struct format_field *format_field,
145 bool needs_swap)
146{
147 field->offset = format_field->offset;
148
149 switch (format_field->size) {
150 case 1:
151 field->integer = tp_field__u8;
152 break;
153 case 2:
154 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
155 break;
156 case 4:
157 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
158 break;
159 case 8:
160 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
161 break;
162 default:
163 return -1;
164 }
165
166 return 0;
167}
168
169static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
170{
171 return sample->raw_data + field->offset;
172}
173
174static int tp_field__init_ptr(struct tp_field *field, struct format_field *format_field)
175{
176 field->offset = format_field->offset;
177 field->pointer = tp_field__ptr;
178 return 0;
179}
180
181struct syscall_tp {
182 struct tp_field id;
183 union {
184 struct tp_field args, ret;
185 };
186};
187
188static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
189 struct tp_field *field,
190 const char *name)
191{
192 struct format_field *format_field = perf_evsel__field(evsel, name);
193
194 if (format_field == NULL)
195 return -1;
196
197 return tp_field__init_uint(field, format_field, evsel->needs_swap);
198}
199
200#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
201 ({ struct syscall_tp *sc = evsel->priv;\
202 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
203
204static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
205 struct tp_field *field,
206 const char *name)
207{
208 struct format_field *format_field = perf_evsel__field(evsel, name);
209
210 if (format_field == NULL)
211 return -1;
212
213 return tp_field__init_ptr(field, format_field);
214}
215
216#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
217 ({ struct syscall_tp *sc = evsel->priv;\
218 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
219
220static void perf_evsel__delete_priv(struct perf_evsel *evsel)
221{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300222 zfree(&evsel->priv);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300223 perf_evsel__delete(evsel);
224}
225
Namhyung Kim96695d42013-11-12 08:51:45 -0300226static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel, void *handler)
227{
228 evsel->priv = malloc(sizeof(struct syscall_tp));
229 if (evsel->priv != NULL) {
230 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
231 goto out_delete;
232
233 evsel->handler = handler;
234 return 0;
235 }
236
237 return -ENOMEM;
238
239out_delete:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300240 zfree(&evsel->priv);
Namhyung Kim96695d42013-11-12 08:51:45 -0300241 return -ENOENT;
242}
243
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300244static struct perf_evsel *perf_evsel__syscall_newtp(const char *direction, void *handler)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300245{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300246 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300247
David Ahern9aca7f12013-12-04 19:41:39 -0700248 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200249 if (IS_ERR(evsel))
David Ahern9aca7f12013-12-04 19:41:39 -0700250 evsel = perf_evsel__newtp("syscalls", direction);
251
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200252 if (IS_ERR(evsel))
253 return NULL;
254
255 if (perf_evsel__init_syscall_tp(evsel, handler))
256 goto out_delete;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300257
258 return evsel;
259
260out_delete:
261 perf_evsel__delete_priv(evsel);
262 return NULL;
263}
264
265#define perf_evsel__sc_tp_uint(evsel, name, sample) \
266 ({ struct syscall_tp *fields = evsel->priv; \
267 fields->name.integer(&fields->name, sample); })
268
269#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
270 ({ struct syscall_tp *fields = evsel->priv; \
271 fields->name.pointer(&fields->name, sample); })
272
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300273struct syscall_arg {
274 unsigned long val;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300275 struct thread *thread;
276 struct trace *trace;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300277 void *parm;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300278 u8 idx;
279 u8 mask;
280};
281
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300282struct strarray {
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300283 int offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300284 int nr_entries;
285 const char **entries;
286};
287
288#define DEFINE_STRARRAY(array) struct strarray strarray__##array = { \
289 .nr_entries = ARRAY_SIZE(array), \
290 .entries = array, \
291}
292
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300293#define DEFINE_STRARRAY_OFFSET(array, off) struct strarray strarray__##array = { \
294 .offset = off, \
295 .nr_entries = ARRAY_SIZE(array), \
296 .entries = array, \
297}
298
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300299static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
300 const char *intfmt,
301 struct syscall_arg *arg)
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300302{
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300303 struct strarray *sa = arg->parm;
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300304 int idx = arg->val - sa->offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300305
306 if (idx < 0 || idx >= sa->nr_entries)
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300307 return scnprintf(bf, size, intfmt, arg->val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300308
309 return scnprintf(bf, size, "%s", sa->entries[idx]);
310}
311
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300312static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
313 struct syscall_arg *arg)
314{
315 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
316}
317
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300318#define SCA_STRARRAY syscall_arg__scnprintf_strarray
319
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300320#if defined(__i386__) || defined(__x86_64__)
321/*
322 * FIXME: Make this available to all arches as soon as the ioctl beautifier
323 * gets rewritten to support all arches.
324 */
Arnaldo Carvalho de Melo78645cf2013-10-08 17:43:20 -0300325static size_t syscall_arg__scnprintf_strhexarray(char *bf, size_t size,
326 struct syscall_arg *arg)
327{
328 return __syscall_arg__scnprintf_strarray(bf, size, "%#x", arg);
329}
330
331#define SCA_STRHEXARRAY syscall_arg__scnprintf_strhexarray
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300332#endif /* defined(__i386__) || defined(__x86_64__) */
Arnaldo Carvalho de Melo78645cf2013-10-08 17:43:20 -0300333
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300334static size_t syscall_arg__scnprintf_fd(char *bf, size_t size,
335 struct syscall_arg *arg);
336
337#define SCA_FD syscall_arg__scnprintf_fd
338
339static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
340 struct syscall_arg *arg)
341{
342 int fd = arg->val;
343
344 if (fd == AT_FDCWD)
345 return scnprintf(bf, size, "CWD");
346
347 return syscall_arg__scnprintf_fd(bf, size, arg);
348}
349
350#define SCA_FDAT syscall_arg__scnprintf_fd_at
351
352static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
353 struct syscall_arg *arg);
354
355#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
356
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -0300357static size_t syscall_arg__scnprintf_hex(char *bf, size_t size,
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300358 struct syscall_arg *arg)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300359{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300360 return scnprintf(bf, size, "%#lx", arg->val);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300361}
362
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300363#define SCA_HEX syscall_arg__scnprintf_hex
364
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300365static size_t syscall_arg__scnprintf_int(char *bf, size_t size,
366 struct syscall_arg *arg)
367{
368 return scnprintf(bf, size, "%d", arg->val);
369}
370
371#define SCA_INT syscall_arg__scnprintf_int
372
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -0300373static size_t syscall_arg__scnprintf_mmap_prot(char *bf, size_t size,
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300374 struct syscall_arg *arg)
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300375{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300376 int printed = 0, prot = arg->val;
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300377
378 if (prot == PROT_NONE)
379 return scnprintf(bf, size, "NONE");
380#define P_MMAP_PROT(n) \
381 if (prot & PROT_##n) { \
382 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
383 prot &= ~PROT_##n; \
384 }
385
386 P_MMAP_PROT(EXEC);
387 P_MMAP_PROT(READ);
388 P_MMAP_PROT(WRITE);
389#ifdef PROT_SEM
390 P_MMAP_PROT(SEM);
391#endif
392 P_MMAP_PROT(GROWSDOWN);
393 P_MMAP_PROT(GROWSUP);
394#undef P_MMAP_PROT
395
396 if (prot)
397 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", prot);
398
399 return printed;
400}
401
402#define SCA_MMAP_PROT syscall_arg__scnprintf_mmap_prot
403
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -0300404static size_t syscall_arg__scnprintf_mmap_flags(char *bf, size_t size,
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300405 struct syscall_arg *arg)
Arnaldo Carvalho de Melo941557e2013-08-23 10:48:33 -0300406{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300407 int printed = 0, flags = arg->val;
Arnaldo Carvalho de Melo941557e2013-08-23 10:48:33 -0300408
409#define P_MMAP_FLAG(n) \
410 if (flags & MAP_##n) { \
411 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
412 flags &= ~MAP_##n; \
413 }
414
415 P_MMAP_FLAG(SHARED);
416 P_MMAP_FLAG(PRIVATE);
Kyle McMartin41817812013-09-05 10:29:47 -0400417#ifdef MAP_32BIT
Arnaldo Carvalho de Melo941557e2013-08-23 10:48:33 -0300418 P_MMAP_FLAG(32BIT);
Kyle McMartin41817812013-09-05 10:29:47 -0400419#endif
Arnaldo Carvalho de Melo941557e2013-08-23 10:48:33 -0300420 P_MMAP_FLAG(ANONYMOUS);
421 P_MMAP_FLAG(DENYWRITE);
422 P_MMAP_FLAG(EXECUTABLE);
423 P_MMAP_FLAG(FILE);
424 P_MMAP_FLAG(FIXED);
425 P_MMAP_FLAG(GROWSDOWN);
David Ahernf2935f32013-08-27 10:50:40 -0600426#ifdef MAP_HUGETLB
Arnaldo Carvalho de Melo941557e2013-08-23 10:48:33 -0300427 P_MMAP_FLAG(HUGETLB);
David Ahernf2935f32013-08-27 10:50:40 -0600428#endif
Arnaldo Carvalho de Melo941557e2013-08-23 10:48:33 -0300429 P_MMAP_FLAG(LOCKED);
430 P_MMAP_FLAG(NONBLOCK);
431 P_MMAP_FLAG(NORESERVE);
432 P_MMAP_FLAG(POPULATE);
433 P_MMAP_FLAG(STACK);
434#ifdef MAP_UNINITIALIZED
435 P_MMAP_FLAG(UNINITIALIZED);
436#endif
437#undef P_MMAP_FLAG
438
439 if (flags)
440 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
441
442 return printed;
443}
444
445#define SCA_MMAP_FLAGS syscall_arg__scnprintf_mmap_flags
446
Alex Snast86998dd2014-08-13 18:42:40 +0300447static size_t syscall_arg__scnprintf_mremap_flags(char *bf, size_t size,
448 struct syscall_arg *arg)
449{
450 int printed = 0, flags = arg->val;
451
452#define P_MREMAP_FLAG(n) \
453 if (flags & MREMAP_##n) { \
454 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
455 flags &= ~MREMAP_##n; \
456 }
457
458 P_MREMAP_FLAG(MAYMOVE);
459#ifdef MREMAP_FIXED
460 P_MREMAP_FLAG(FIXED);
461#endif
462#undef P_MREMAP_FLAG
463
464 if (flags)
465 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
466
467 return printed;
468}
469
470#define SCA_MREMAP_FLAGS syscall_arg__scnprintf_mremap_flags
471
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -0300472static size_t syscall_arg__scnprintf_madvise_behavior(char *bf, size_t size,
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300473 struct syscall_arg *arg)
Arnaldo Carvalho de Melo9e9716d2013-08-23 10:06:41 -0300474{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300475 int behavior = arg->val;
Arnaldo Carvalho de Melo9e9716d2013-08-23 10:06:41 -0300476
477 switch (behavior) {
478#define P_MADV_BHV(n) case MADV_##n: return scnprintf(bf, size, #n)
479 P_MADV_BHV(NORMAL);
480 P_MADV_BHV(RANDOM);
481 P_MADV_BHV(SEQUENTIAL);
482 P_MADV_BHV(WILLNEED);
483 P_MADV_BHV(DONTNEED);
484 P_MADV_BHV(REMOVE);
485 P_MADV_BHV(DONTFORK);
486 P_MADV_BHV(DOFORK);
487 P_MADV_BHV(HWPOISON);
488#ifdef MADV_SOFT_OFFLINE
489 P_MADV_BHV(SOFT_OFFLINE);
490#endif
491 P_MADV_BHV(MERGEABLE);
492 P_MADV_BHV(UNMERGEABLE);
David Ahernf2935f32013-08-27 10:50:40 -0600493#ifdef MADV_HUGEPAGE
Arnaldo Carvalho de Melo9e9716d2013-08-23 10:06:41 -0300494 P_MADV_BHV(HUGEPAGE);
David Ahernf2935f32013-08-27 10:50:40 -0600495#endif
496#ifdef MADV_NOHUGEPAGE
Arnaldo Carvalho de Melo9e9716d2013-08-23 10:06:41 -0300497 P_MADV_BHV(NOHUGEPAGE);
David Ahernf2935f32013-08-27 10:50:40 -0600498#endif
Arnaldo Carvalho de Melo9e9716d2013-08-23 10:06:41 -0300499#ifdef MADV_DONTDUMP
500 P_MADV_BHV(DONTDUMP);
501#endif
502#ifdef MADV_DODUMP
503 P_MADV_BHV(DODUMP);
504#endif
505#undef P_MADV_PHV
506 default: break;
507 }
508
509 return scnprintf(bf, size, "%#x", behavior);
510}
511
512#define SCA_MADV_BHV syscall_arg__scnprintf_madvise_behavior
513
Arnaldo Carvalho de Melo5cea6ff2013-09-20 11:49:50 -0300514static size_t syscall_arg__scnprintf_flock(char *bf, size_t size,
515 struct syscall_arg *arg)
516{
517 int printed = 0, op = arg->val;
518
519 if (op == 0)
520 return scnprintf(bf, size, "NONE");
521#define P_CMD(cmd) \
522 if ((op & LOCK_##cmd) == LOCK_##cmd) { \
523 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #cmd); \
524 op &= ~LOCK_##cmd; \
525 }
526
527 P_CMD(SH);
528 P_CMD(EX);
529 P_CMD(NB);
530 P_CMD(UN);
531 P_CMD(MAND);
532 P_CMD(RW);
533 P_CMD(READ);
534 P_CMD(WRITE);
535#undef P_OP
536
537 if (op)
538 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", op);
539
540 return printed;
541}
542
543#define SCA_FLOCK syscall_arg__scnprintf_flock
544
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300545static size_t syscall_arg__scnprintf_futex_op(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300546{
547 enum syscall_futex_args {
548 SCF_UADDR = (1 << 0),
549 SCF_OP = (1 << 1),
550 SCF_VAL = (1 << 2),
551 SCF_TIMEOUT = (1 << 3),
552 SCF_UADDR2 = (1 << 4),
553 SCF_VAL3 = (1 << 5),
554 };
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300555 int op = arg->val;
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300556 int cmd = op & FUTEX_CMD_MASK;
557 size_t printed = 0;
558
559 switch (cmd) {
560#define P_FUTEX_OP(n) case FUTEX_##n: printed = scnprintf(bf, size, #n);
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300561 P_FUTEX_OP(WAIT); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
562 P_FUTEX_OP(WAKE); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
563 P_FUTEX_OP(FD); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
564 P_FUTEX_OP(REQUEUE); arg->mask |= SCF_VAL3|SCF_TIMEOUT; break;
565 P_FUTEX_OP(CMP_REQUEUE); arg->mask |= SCF_TIMEOUT; break;
566 P_FUTEX_OP(CMP_REQUEUE_PI); arg->mask |= SCF_TIMEOUT; break;
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300567 P_FUTEX_OP(WAKE_OP); break;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300568 P_FUTEX_OP(LOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
569 P_FUTEX_OP(UNLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2|SCF_TIMEOUT; break;
570 P_FUTEX_OP(TRYLOCK_PI); arg->mask |= SCF_VAL3|SCF_UADDR2; break;
571 P_FUTEX_OP(WAIT_BITSET); arg->mask |= SCF_UADDR2; break;
572 P_FUTEX_OP(WAKE_BITSET); arg->mask |= SCF_UADDR2; break;
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300573 P_FUTEX_OP(WAIT_REQUEUE_PI); break;
574 default: printed = scnprintf(bf, size, "%#x", cmd); break;
575 }
576
577 if (op & FUTEX_PRIVATE_FLAG)
578 printed += scnprintf(bf + printed, size - printed, "|PRIV");
579
580 if (op & FUTEX_CLOCK_REALTIME)
581 printed += scnprintf(bf + printed, size - printed, "|CLKRT");
582
583 return printed;
584}
585
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300586#define SCA_FUTEX_OP syscall_arg__scnprintf_futex_op
587
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300588static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
589static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1);
Arnaldo Carvalho de Meloeac032c2013-09-20 11:27:32 -0300590
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300591static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
592static DEFINE_STRARRAY(itimers);
593
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300594static const char *keyctl_options[] = {
595 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
596 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
597 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
598 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
599 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
600};
601static DEFINE_STRARRAY(keyctl_options);
602
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300603static const char *whences[] = { "SET", "CUR", "END",
604#ifdef SEEK_DATA
605"DATA",
606#endif
607#ifdef SEEK_HOLE
608"HOLE",
609#endif
610};
611static DEFINE_STRARRAY(whences);
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300612
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300613static const char *fcntl_cmds[] = {
614 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
615 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "F_GETLK64",
616 "F_SETLK64", "F_SETLKW64", "F_SETOWN_EX", "F_GETOWN_EX",
617 "F_GETOWNER_UIDS",
618};
619static DEFINE_STRARRAY(fcntl_cmds);
620
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300621static const char *rlimit_resources[] = {
622 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
623 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
624 "RTTIME",
625};
626static DEFINE_STRARRAY(rlimit_resources);
627
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300628static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
629static DEFINE_STRARRAY(sighow);
630
David Ahern4f8c1b72013-09-22 19:45:00 -0600631static const char *clockid[] = {
632 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
Arnaldo Carvalho de Melo28ebb872015-08-11 10:38:38 -0300633 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
634 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
David Ahern4f8c1b72013-09-22 19:45:00 -0600635};
636static DEFINE_STRARRAY(clockid);
637
Arnaldo Carvalho de Meloe10bce82013-09-04 10:27:41 -0300638static const char *socket_families[] = {
639 "UNSPEC", "LOCAL", "INET", "AX25", "IPX", "APPLETALK", "NETROM",
640 "BRIDGE", "ATMPVC", "X25", "INET6", "ROSE", "DECnet", "NETBEUI",
641 "SECURITY", "KEY", "NETLINK", "PACKET", "ASH", "ECONET", "ATMSVC",
642 "RDS", "SNA", "IRDA", "PPPOX", "WANPIPE", "LLC", "IB", "CAN", "TIPC",
643 "BLUETOOTH", "IUCV", "RXRPC", "ISDN", "PHONET", "IEEE802154", "CAIF",
644 "ALG", "NFC", "VSOCK",
645};
646static DEFINE_STRARRAY(socket_families);
647
Arnaldo Carvalho de Meloa28b24b2013-09-04 11:00:44 -0300648#ifndef SOCK_TYPE_MASK
649#define SOCK_TYPE_MASK 0xf
650#endif
651
652static size_t syscall_arg__scnprintf_socket_type(char *bf, size_t size,
653 struct syscall_arg *arg)
654{
655 size_t printed;
656 int type = arg->val,
657 flags = type & ~SOCK_TYPE_MASK;
658
659 type &= SOCK_TYPE_MASK;
660 /*
661 * Can't use a strarray, MIPS may override for ABI reasons.
662 */
663 switch (type) {
664#define P_SK_TYPE(n) case SOCK_##n: printed = scnprintf(bf, size, #n); break;
665 P_SK_TYPE(STREAM);
666 P_SK_TYPE(DGRAM);
667 P_SK_TYPE(RAW);
668 P_SK_TYPE(RDM);
669 P_SK_TYPE(SEQPACKET);
670 P_SK_TYPE(DCCP);
671 P_SK_TYPE(PACKET);
672#undef P_SK_TYPE
673 default:
674 printed = scnprintf(bf, size, "%#x", type);
675 }
676
677#define P_SK_FLAG(n) \
678 if (flags & SOCK_##n) { \
679 printed += scnprintf(bf + printed, size - printed, "|%s", #n); \
680 flags &= ~SOCK_##n; \
681 }
682
683 P_SK_FLAG(CLOEXEC);
684 P_SK_FLAG(NONBLOCK);
685#undef P_SK_FLAG
686
687 if (flags)
688 printed += scnprintf(bf + printed, size - printed, "|%#x", flags);
689
690 return printed;
691}
692
693#define SCA_SK_TYPE syscall_arg__scnprintf_socket_type
694
Arnaldo Carvalho de Melob2cc99fd2013-09-12 11:54:48 -0300695#ifndef MSG_PROBE
696#define MSG_PROBE 0x10
697#endif
David Ahernb6e8f8f2013-09-22 19:44:56 -0600698#ifndef MSG_WAITFORONE
699#define MSG_WAITFORONE 0x10000
700#endif
Arnaldo Carvalho de Melob2cc99fd2013-09-12 11:54:48 -0300701#ifndef MSG_SENDPAGE_NOTLAST
702#define MSG_SENDPAGE_NOTLAST 0x20000
703#endif
704#ifndef MSG_FASTOPEN
705#define MSG_FASTOPEN 0x20000000
706#endif
707
708static size_t syscall_arg__scnprintf_msg_flags(char *bf, size_t size,
709 struct syscall_arg *arg)
710{
711 int printed = 0, flags = arg->val;
712
713 if (flags == 0)
714 return scnprintf(bf, size, "NONE");
715#define P_MSG_FLAG(n) \
716 if (flags & MSG_##n) { \
717 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
718 flags &= ~MSG_##n; \
719 }
720
721 P_MSG_FLAG(OOB);
722 P_MSG_FLAG(PEEK);
723 P_MSG_FLAG(DONTROUTE);
724 P_MSG_FLAG(TRYHARD);
725 P_MSG_FLAG(CTRUNC);
726 P_MSG_FLAG(PROBE);
727 P_MSG_FLAG(TRUNC);
728 P_MSG_FLAG(DONTWAIT);
729 P_MSG_FLAG(EOR);
730 P_MSG_FLAG(WAITALL);
731 P_MSG_FLAG(FIN);
732 P_MSG_FLAG(SYN);
733 P_MSG_FLAG(CONFIRM);
734 P_MSG_FLAG(RST);
735 P_MSG_FLAG(ERRQUEUE);
736 P_MSG_FLAG(NOSIGNAL);
737 P_MSG_FLAG(MORE);
738 P_MSG_FLAG(WAITFORONE);
739 P_MSG_FLAG(SENDPAGE_NOTLAST);
740 P_MSG_FLAG(FASTOPEN);
741 P_MSG_FLAG(CMSG_CLOEXEC);
742#undef P_MSG_FLAG
743
744 if (flags)
745 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
746
747 return printed;
748}
749
750#define SCA_MSG_FLAGS syscall_arg__scnprintf_msg_flags
751
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300752static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
753 struct syscall_arg *arg)
754{
755 size_t printed = 0;
756 int mode = arg->val;
757
758 if (mode == F_OK) /* 0 */
759 return scnprintf(bf, size, "F");
760#define P_MODE(n) \
761 if (mode & n##_OK) { \
762 printed += scnprintf(bf + printed, size - printed, "%s", #n); \
763 mode &= ~n##_OK; \
764 }
765
766 P_MODE(R);
767 P_MODE(W);
768 P_MODE(X);
769#undef P_MODE
770
771 if (mode)
772 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
773
774 return printed;
775}
776
777#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
778
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300779static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
780 struct syscall_arg *arg);
781
782#define SCA_FILENAME syscall_arg__scnprintf_filename
783
Arnaldo Carvalho de Melobe65a892013-09-02 16:22:31 -0300784static size_t syscall_arg__scnprintf_open_flags(char *bf, size_t size,
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300785 struct syscall_arg *arg)
Arnaldo Carvalho de Melobe65a892013-09-02 16:22:31 -0300786{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300787 int printed = 0, flags = arg->val;
Arnaldo Carvalho de Melobe65a892013-09-02 16:22:31 -0300788
789 if (!(flags & O_CREAT))
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300790 arg->mask |= 1 << (arg->idx + 1); /* Mask the mode parm */
Arnaldo Carvalho de Melobe65a892013-09-02 16:22:31 -0300791
792 if (flags == 0)
793 return scnprintf(bf, size, "RDONLY");
794#define P_FLAG(n) \
795 if (flags & O_##n) { \
796 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
797 flags &= ~O_##n; \
798 }
799
800 P_FLAG(APPEND);
801 P_FLAG(ASYNC);
802 P_FLAG(CLOEXEC);
803 P_FLAG(CREAT);
804 P_FLAG(DIRECT);
805 P_FLAG(DIRECTORY);
806 P_FLAG(EXCL);
807 P_FLAG(LARGEFILE);
808 P_FLAG(NOATIME);
809 P_FLAG(NOCTTY);
810#ifdef O_NONBLOCK
811 P_FLAG(NONBLOCK);
812#elif O_NDELAY
813 P_FLAG(NDELAY);
814#endif
815#ifdef O_PATH
816 P_FLAG(PATH);
817#endif
818 P_FLAG(RDWR);
819#ifdef O_DSYNC
820 if ((flags & O_SYNC) == O_SYNC)
821 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", "SYNC");
822 else {
823 P_FLAG(DSYNC);
824 }
825#else
826 P_FLAG(SYNC);
827#endif
828 P_FLAG(TRUNC);
829 P_FLAG(WRONLY);
830#undef P_FLAG
831
832 if (flags)
833 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
834
835 return printed;
836}
837
838#define SCA_OPEN_FLAGS syscall_arg__scnprintf_open_flags
839
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300840static size_t syscall_arg__scnprintf_perf_flags(char *bf, size_t size,
841 struct syscall_arg *arg)
842{
843 int printed = 0, flags = arg->val;
844
845 if (flags == 0)
846 return 0;
847
848#define P_FLAG(n) \
849 if (flags & PERF_FLAG_##n) { \
850 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
851 flags &= ~PERF_FLAG_##n; \
852 }
853
854 P_FLAG(FD_NO_GROUP);
855 P_FLAG(FD_OUTPUT);
856 P_FLAG(PID_CGROUP);
857 P_FLAG(FD_CLOEXEC);
858#undef P_FLAG
859
860 if (flags)
861 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
862
863 return printed;
864}
865
866#define SCA_PERF_FLAGS syscall_arg__scnprintf_perf_flags
867
Arnaldo Carvalho de Melo49af9e92013-09-12 12:18:56 -0300868static size_t syscall_arg__scnprintf_eventfd_flags(char *bf, size_t size,
869 struct syscall_arg *arg)
870{
871 int printed = 0, flags = arg->val;
872
873 if (flags == 0)
874 return scnprintf(bf, size, "NONE");
875#define P_FLAG(n) \
876 if (flags & EFD_##n) { \
877 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
878 flags &= ~EFD_##n; \
879 }
880
881 P_FLAG(SEMAPHORE);
882 P_FLAG(CLOEXEC);
883 P_FLAG(NONBLOCK);
884#undef P_FLAG
885
886 if (flags)
887 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
888
889 return printed;
890}
891
892#define SCA_EFD_FLAGS syscall_arg__scnprintf_eventfd_flags
893
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300894static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
895 struct syscall_arg *arg)
896{
897 int printed = 0, flags = arg->val;
898
899#define P_FLAG(n) \
900 if (flags & O_##n) { \
901 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
902 flags &= ~O_##n; \
903 }
904
905 P_FLAG(CLOEXEC);
906 P_FLAG(NONBLOCK);
907#undef P_FLAG
908
909 if (flags)
910 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
911
912 return printed;
913}
914
915#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
916
Arnaldo Carvalho de Melo8bad5b02013-09-03 17:17:15 -0300917static size_t syscall_arg__scnprintf_signum(char *bf, size_t size, struct syscall_arg *arg)
918{
919 int sig = arg->val;
920
921 switch (sig) {
922#define P_SIGNUM(n) case SIG##n: return scnprintf(bf, size, #n)
923 P_SIGNUM(HUP);
924 P_SIGNUM(INT);
925 P_SIGNUM(QUIT);
926 P_SIGNUM(ILL);
927 P_SIGNUM(TRAP);
928 P_SIGNUM(ABRT);
929 P_SIGNUM(BUS);
930 P_SIGNUM(FPE);
931 P_SIGNUM(KILL);
932 P_SIGNUM(USR1);
933 P_SIGNUM(SEGV);
934 P_SIGNUM(USR2);
935 P_SIGNUM(PIPE);
936 P_SIGNUM(ALRM);
937 P_SIGNUM(TERM);
Arnaldo Carvalho de Melo8bad5b02013-09-03 17:17:15 -0300938 P_SIGNUM(CHLD);
939 P_SIGNUM(CONT);
940 P_SIGNUM(STOP);
941 P_SIGNUM(TSTP);
942 P_SIGNUM(TTIN);
943 P_SIGNUM(TTOU);
944 P_SIGNUM(URG);
945 P_SIGNUM(XCPU);
946 P_SIGNUM(XFSZ);
947 P_SIGNUM(VTALRM);
948 P_SIGNUM(PROF);
949 P_SIGNUM(WINCH);
950 P_SIGNUM(IO);
951 P_SIGNUM(PWR);
952 P_SIGNUM(SYS);
Ben Hutchings02c5bb42014-02-06 01:00:41 +0000953#ifdef SIGEMT
954 P_SIGNUM(EMT);
955#endif
956#ifdef SIGSTKFLT
957 P_SIGNUM(STKFLT);
958#endif
959#ifdef SIGSWI
960 P_SIGNUM(SWI);
961#endif
Arnaldo Carvalho de Melo8bad5b02013-09-03 17:17:15 -0300962 default: break;
963 }
964
965 return scnprintf(bf, size, "%#x", sig);
966}
967
968#define SCA_SIGNUM syscall_arg__scnprintf_signum
969
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300970#if defined(__i386__) || defined(__x86_64__)
971/*
972 * FIXME: Make this available to all arches.
973 */
Arnaldo Carvalho de Melo78645cf2013-10-08 17:43:20 -0300974#define TCGETS 0x5401
975
976static const char *tioctls[] = {
977 "TCGETS", "TCSETS", "TCSETSW", "TCSETSF", "TCGETA", "TCSETA", "TCSETAW",
978 "TCSETAF", "TCSBRK", "TCXONC", "TCFLSH", "TIOCEXCL", "TIOCNXCL",
979 "TIOCSCTTY", "TIOCGPGRP", "TIOCSPGRP", "TIOCOUTQ", "TIOCSTI",
980 "TIOCGWINSZ", "TIOCSWINSZ", "TIOCMGET", "TIOCMBIS", "TIOCMBIC",
981 "TIOCMSET", "TIOCGSOFTCAR", "TIOCSSOFTCAR", "FIONREAD", "TIOCLINUX",
982 "TIOCCONS", "TIOCGSERIAL", "TIOCSSERIAL", "TIOCPKT", "FIONBIO",
983 "TIOCNOTTY", "TIOCSETD", "TIOCGETD", "TCSBRKP", [0x27] = "TIOCSBRK",
984 "TIOCCBRK", "TIOCGSID", "TCGETS2", "TCSETS2", "TCSETSW2", "TCSETSF2",
985 "TIOCGRS485", "TIOCSRS485", "TIOCGPTN", "TIOCSPTLCK",
986 "TIOCGDEV||TCGETX", "TCSETX", "TCSETXF", "TCSETXW", "TIOCSIG",
987 "TIOCVHANGUP", "TIOCGPKT", "TIOCGPTLCK", "TIOCGEXCL",
988 [0x50] = "FIONCLEX", "FIOCLEX", "FIOASYNC", "TIOCSERCONFIG",
989 "TIOCSERGWILD", "TIOCSERSWILD", "TIOCGLCKTRMIOS", "TIOCSLCKTRMIOS",
990 "TIOCSERGSTRUCT", "TIOCSERGETLSR", "TIOCSERGETMULTI", "TIOCSERSETMULTI",
991 "TIOCMIWAIT", "TIOCGICOUNT", [0x60] = "FIOQSIZE",
992};
993
994static DEFINE_STRARRAY_OFFSET(tioctls, 0x5401);
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300995#endif /* defined(__i386__) || defined(__x86_64__) */
Arnaldo Carvalho de Melo78645cf2013-10-08 17:43:20 -0300996
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -0300997#define STRARRAY(arg, name, array) \
998 .arg_scnprintf = { [arg] = SCA_STRARRAY, }, \
999 .arg_parm = { [arg] = &strarray__##array, }
1000
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001001static struct syscall_fmt {
1002 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001003 const char *alias;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001004 size_t (*arg_scnprintf[6])(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -03001005 void *arg_parm[6];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001006 bool errmsg;
1007 bool timeout;
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -03001008 bool hexret;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001009} syscall_fmts[] = {
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -03001010 { .name = "access", .errmsg = true,
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001011 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */
1012 [1] = SCA_ACCMODE, /* mode */ }, },
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001013 { .name = "arch_prctl", .errmsg = true, .alias = "prctl", },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -03001014 { .name = "brk", .hexret = true,
1015 .arg_scnprintf = { [0] = SCA_HEX, /* brk */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001016 { .name = "chdir", .errmsg = true,
1017 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, },
1018 { .name = "chmod", .errmsg = true,
1019 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, },
1020 { .name = "chroot", .errmsg = true,
1021 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, },
David Ahern4f8c1b72013-09-22 19:45:00 -06001022 { .name = "clock_gettime", .errmsg = true, STRARRAY(0, clk_id, clockid), },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001023 { .name = "close", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001024 .arg_scnprintf = { [0] = SCA_CLOSE_FD, /* fd */ }, },
Arnaldo Carvalho de Meloa14bb862013-07-30 16:38:23 -03001025 { .name = "connect", .errmsg = true, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001026 { .name = "creat", .errmsg = true,
1027 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001028 { .name = "dup", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001029 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001030 { .name = "dup2", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001031 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001032 { .name = "dup3", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001033 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -03001034 { .name = "epoll_ctl", .errmsg = true, STRARRAY(1, op, epoll_ctl_ops), },
Arnaldo Carvalho de Melo49af9e92013-09-12 12:18:56 -03001035 { .name = "eventfd2", .errmsg = true,
1036 .arg_scnprintf = { [1] = SCA_EFD_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001037 { .name = "faccessat", .errmsg = true,
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001038 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
1039 [1] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001040 { .name = "fadvise64", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001041 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001042 { .name = "fallocate", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001043 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001044 { .name = "fchdir", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001045 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001046 { .name = "fchmod", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001047 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001048 { .name = "fchmodat", .errmsg = true,
Arnaldo Carvalho de Melo090389b62015-08-10 19:20:52 -03001049 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */
1050 [1] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001051 { .name = "fchown", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001052 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001053 { .name = "fchownat", .errmsg = true,
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001054 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */
1055 [1] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001056 { .name = "fcntl", .errmsg = true,
1057 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1058 [1] = SCA_STRARRAY, /* cmd */ },
1059 .arg_parm = { [1] = &strarray__fcntl_cmds, /* cmd */ }, },
1060 { .name = "fdatasync", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001061 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo5cea6ff2013-09-20 11:49:50 -03001062 { .name = "flock", .errmsg = true,
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001063 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1064 [1] = SCA_FLOCK, /* cmd */ }, },
1065 { .name = "fsetxattr", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001066 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001067 { .name = "fstat", .errmsg = true, .alias = "newfstat",
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001068 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001069 { .name = "fstatat", .errmsg = true, .alias = "newfstatat",
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001070 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
1071 [1] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001072 { .name = "fstatfs", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001073 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001074 { .name = "fsync", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001075 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001076 { .name = "ftruncate", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001077 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -03001078 { .name = "futex", .errmsg = true,
1079 .arg_scnprintf = { [1] = SCA_FUTEX_OP, /* op */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001080 { .name = "futimesat", .errmsg = true,
Arnaldo Carvalho de Melo090389b62015-08-10 19:20:52 -03001081 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */
1082 [1] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001083 { .name = "getdents", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001084 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001085 { .name = "getdents64", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001086 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -03001087 { .name = "getitimer", .errmsg = true, STRARRAY(0, which, itimers), },
1088 { .name = "getrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001089 { .name = "getxattr", .errmsg = true,
1090 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
1091 { .name = "inotify_add_watch", .errmsg = true,
1092 .arg_scnprintf = { [1] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -03001093 { .name = "ioctl", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001094 .arg_scnprintf = { [0] = SCA_FD, /* fd */
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -03001095#if defined(__i386__) || defined(__x86_64__)
1096/*
1097 * FIXME: Make this available to all arches.
1098 */
Arnaldo Carvalho de Melo78645cf2013-10-08 17:43:20 -03001099 [1] = SCA_STRHEXARRAY, /* cmd */
1100 [2] = SCA_HEX, /* arg */ },
1101 .arg_parm = { [1] = &strarray__tioctls, /* cmd */ }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -03001102#else
1103 [2] = SCA_HEX, /* arg */ }, },
1104#endif
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -03001105 { .name = "keyctl", .errmsg = true, STRARRAY(0, option, keyctl_options), },
Arnaldo Carvalho de Melo8bad5b02013-09-03 17:17:15 -03001106 { .name = "kill", .errmsg = true,
1107 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001108 { .name = "lchown", .errmsg = true,
1109 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, },
1110 { .name = "lgetxattr", .errmsg = true,
1111 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001112 { .name = "linkat", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001113 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001114 { .name = "listxattr", .errmsg = true,
1115 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo090389b62015-08-10 19:20:52 -03001116 { .name = "llistxattr", .errmsg = true,
1117 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
1118 { .name = "lremovexattr", .errmsg = true,
1119 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001120 { .name = "lseek", .errmsg = true,
1121 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1122 [2] = SCA_STRARRAY, /* whence */ },
1123 .arg_parm = { [2] = &strarray__whences, /* whence */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001124 { .name = "lsetxattr", .errmsg = true,
1125 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo090389b62015-08-10 19:20:52 -03001126 { .name = "lstat", .errmsg = true, .alias = "newlstat",
1127 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001128 { .name = "lsxattr", .errmsg = true,
1129 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo9e9716d2013-08-23 10:06:41 -03001130 { .name = "madvise", .errmsg = true,
1131 .arg_scnprintf = { [0] = SCA_HEX, /* start */
1132 [2] = SCA_MADV_BHV, /* behavior */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001133 { .name = "mkdir", .errmsg = true,
1134 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001135 { .name = "mkdirat", .errmsg = true,
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001136 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */
1137 [1] = SCA_FILENAME, /* pathname */ }, },
1138 { .name = "mknod", .errmsg = true,
1139 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001140 { .name = "mknodat", .errmsg = true,
Arnaldo Carvalho de Melo090389b62015-08-10 19:20:52 -03001141 .arg_scnprintf = { [0] = SCA_FDAT, /* fd */
1142 [1] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo3d903aa2013-09-24 00:09:38 -03001143 { .name = "mlock", .errmsg = true,
1144 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
1145 { .name = "mlockall", .errmsg = true,
1146 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -03001147 { .name = "mmap", .hexret = true,
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -03001148 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
Arnaldo Carvalho de Melo941557e2013-08-23 10:48:33 -03001149 [2] = SCA_MMAP_PROT, /* prot */
Namhyung Kim73faab32013-11-12 15:24:59 +09001150 [3] = SCA_MMAP_FLAGS, /* flags */
1151 [4] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -03001152 { .name = "mprotect", .errmsg = true,
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -03001153 .arg_scnprintf = { [0] = SCA_HEX, /* start */
1154 [2] = SCA_MMAP_PROT, /* prot */ }, },
Arnaldo Carvalho de Melo090389b62015-08-10 19:20:52 -03001155 { .name = "mq_unlink", .errmsg = true,
1156 .arg_scnprintf = { [0] = SCA_FILENAME, /* u_name */ }, },
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -03001157 { .name = "mremap", .hexret = true,
1158 .arg_scnprintf = { [0] = SCA_HEX, /* addr */
Alex Snast86998dd2014-08-13 18:42:40 +03001159 [3] = SCA_MREMAP_FLAGS, /* flags */
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -03001160 [4] = SCA_HEX, /* new_addr */ }, },
Arnaldo Carvalho de Melo3d903aa2013-09-24 00:09:38 -03001161 { .name = "munlock", .errmsg = true,
1162 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -03001163 { .name = "munmap", .errmsg = true,
1164 .arg_scnprintf = { [0] = SCA_HEX, /* addr */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001165 { .name = "name_to_handle_at", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001166 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001167 { .name = "newfstatat", .errmsg = true,
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001168 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
1169 [1] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melobe65a892013-09-02 16:22:31 -03001170 { .name = "open", .errmsg = true,
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001171 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */
1172 [1] = SCA_OPEN_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melo31cd3852013-09-02 16:40:40 -03001173 { .name = "open_by_handle_at", .errmsg = true,
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001174 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
1175 [2] = SCA_OPEN_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melo31cd3852013-09-02 16:40:40 -03001176 { .name = "openat", .errmsg = true,
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001177 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001178 [1] = SCA_FILENAME, /* filename */
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001179 [2] = SCA_OPEN_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -03001180 { .name = "perf_event_open", .errmsg = true,
1181 .arg_scnprintf = { [1] = SCA_INT, /* pid */
1182 [2] = SCA_INT, /* cpu */
1183 [3] = SCA_FD, /* group_fd */
1184 [4] = SCA_PERF_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -03001185 { .name = "pipe2", .errmsg = true,
1186 .arg_scnprintf = { [1] = SCA_PIPE_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001187 { .name = "poll", .errmsg = true, .timeout = true, },
1188 { .name = "ppoll", .errmsg = true, .timeout = true, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001189 { .name = "pread", .errmsg = true, .alias = "pread64",
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001190 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001191 { .name = "preadv", .errmsg = true, .alias = "pread",
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001192 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -03001193 { .name = "prlimit64", .errmsg = true, STRARRAY(1, resource, rlimit_resources), },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001194 { .name = "pwrite", .errmsg = true, .alias = "pwrite64",
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001195 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001196 { .name = "pwritev", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001197 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001198 { .name = "read", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001199 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001200 { .name = "readlink", .errmsg = true,
1201 .arg_scnprintf = { [0] = SCA_FILENAME, /* path */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001202 { .name = "readlinkat", .errmsg = true,
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001203 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
1204 [1] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001205 { .name = "readv", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001206 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melob2cc99fd2013-09-12 11:54:48 -03001207 { .name = "recvfrom", .errmsg = true,
Arnaldo Carvalho de Melo8d8c66a2015-08-11 10:57:02 -03001208 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1209 [3] = SCA_MSG_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melob2cc99fd2013-09-12 11:54:48 -03001210 { .name = "recvmmsg", .errmsg = true,
Arnaldo Carvalho de Melo8d8c66a2015-08-11 10:57:02 -03001211 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1212 [3] = SCA_MSG_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melob2cc99fd2013-09-12 11:54:48 -03001213 { .name = "recvmsg", .errmsg = true,
Arnaldo Carvalho de Melo8d8c66a2015-08-11 10:57:02 -03001214 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1215 [2] = SCA_MSG_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001216 { .name = "removexattr", .errmsg = true,
1217 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001218 { .name = "renameat", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001219 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001220 { .name = "rmdir", .errmsg = true,
1221 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo8bad5b02013-09-03 17:17:15 -03001222 { .name = "rt_sigaction", .errmsg = true,
1223 .arg_scnprintf = { [0] = SCA_SIGNUM, /* sig */ }, },
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -03001224 { .name = "rt_sigprocmask", .errmsg = true, STRARRAY(0, how, sighow), },
Arnaldo Carvalho de Melo8bad5b02013-09-03 17:17:15 -03001225 { .name = "rt_sigqueueinfo", .errmsg = true,
1226 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
1227 { .name = "rt_tgsigqueueinfo", .errmsg = true,
1228 .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001229 { .name = "select", .errmsg = true, .timeout = true, },
Arnaldo Carvalho de Melob2cc99fd2013-09-12 11:54:48 -03001230 { .name = "sendmmsg", .errmsg = true,
Arnaldo Carvalho de Melo8d8c66a2015-08-11 10:57:02 -03001231 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1232 [3] = SCA_MSG_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melob2cc99fd2013-09-12 11:54:48 -03001233 { .name = "sendmsg", .errmsg = true,
Arnaldo Carvalho de Melo8d8c66a2015-08-11 10:57:02 -03001234 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1235 [2] = SCA_MSG_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melob2cc99fd2013-09-12 11:54:48 -03001236 { .name = "sendto", .errmsg = true,
Arnaldo Carvalho de Melo8d8c66a2015-08-11 10:57:02 -03001237 .arg_scnprintf = { [0] = SCA_FD, /* fd */
1238 [3] = SCA_MSG_FLAGS, /* flags */ }, },
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -03001239 { .name = "setitimer", .errmsg = true, STRARRAY(0, which, itimers), },
1240 { .name = "setrlimit", .errmsg = true, STRARRAY(0, resource, rlimit_resources), },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001241 { .name = "setxattr", .errmsg = true,
1242 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001243 { .name = "shutdown", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001244 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Meloe10bce82013-09-04 10:27:41 -03001245 { .name = "socket", .errmsg = true,
Arnaldo Carvalho de Meloa28b24b2013-09-04 11:00:44 -03001246 .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
1247 [1] = SCA_SK_TYPE, /* type */ },
Arnaldo Carvalho de Meloe10bce82013-09-04 10:27:41 -03001248 .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
Arnaldo Carvalho de Melo07120aa2013-09-20 12:24:20 -03001249 { .name = "socketpair", .errmsg = true,
1250 .arg_scnprintf = { [0] = SCA_STRARRAY, /* family */
1251 [1] = SCA_SK_TYPE, /* type */ },
1252 .arg_parm = { [0] = &strarray__socket_families, /* family */ }, },
Arnaldo Carvalho de Melo090389b62015-08-10 19:20:52 -03001253 { .name = "stat", .errmsg = true, .alias = "newstat",
1254 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001255 { .name = "statfs", .errmsg = true,
1256 .arg_scnprintf = { [0] = SCA_FILENAME, /* pathname */ }, },
1257 { .name = "swapoff", .errmsg = true,
1258 .arg_scnprintf = { [0] = SCA_FILENAME, /* specialfile */ }, },
1259 { .name = "swapon", .errmsg = true,
1260 .arg_scnprintf = { [0] = SCA_FILENAME, /* specialfile */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001261 { .name = "symlinkat", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001262 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */ }, },
Arnaldo Carvalho de Melo8bad5b02013-09-03 17:17:15 -03001263 { .name = "tgkill", .errmsg = true,
1264 .arg_scnprintf = { [2] = SCA_SIGNUM, /* sig */ }, },
1265 { .name = "tkill", .errmsg = true,
1266 .arg_scnprintf = { [1] = SCA_SIGNUM, /* sig */ }, },
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001267 { .name = "truncate", .errmsg = true,
1268 .arg_scnprintf = { [0] = SCA_FILENAME, /* path */ }, },
Arnaldo Carvalho de Meloe5959682013-08-26 12:21:41 -03001269 { .name = "uname", .errmsg = true, .alias = "newuname", },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001270 { .name = "unlinkat", .errmsg = true,
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001271 .arg_scnprintf = { [0] = SCA_FDAT, /* dfd */
1272 [1] = SCA_FILENAME, /* pathname */ }, },
1273 { .name = "utime", .errmsg = true,
1274 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001275 { .name = "utimensat", .errmsg = true,
Arnaldo Carvalho de Melo34221112015-08-04 23:31:25 -03001276 .arg_scnprintf = { [0] = SCA_FDAT, /* dirfd */
1277 [1] = SCA_FILENAME, /* filename */ }, },
1278 { .name = "utimes", .errmsg = true,
1279 .arg_scnprintf = { [0] = SCA_FILENAME, /* filename */ }, },
Arnaldo Carvalho de Melo090389b62015-08-10 19:20:52 -03001280 { .name = "vmsplice", .errmsg = true,
1281 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001282 { .name = "write", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001283 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001284 { .name = "writev", .errmsg = true,
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001285 .arg_scnprintf = { [0] = SCA_FD, /* fd */ }, },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001286};
1287
1288static int syscall_fmt__cmp(const void *name, const void *fmtp)
1289{
1290 const struct syscall_fmt *fmt = fmtp;
1291 return strcmp(name, fmt->name);
1292}
1293
1294static struct syscall_fmt *syscall_fmt__find(const char *name)
1295{
1296 const int nmemb = ARRAY_SIZE(syscall_fmts);
1297 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
1298}
1299
1300struct syscall {
1301 struct event_format *tp_format;
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001302 int nr_args;
1303 struct format_field *args;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001304 const char *name;
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001305 bool is_exit;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001306 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001307 size_t (**arg_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -03001308 void **arg_parm;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001309};
1310
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001311static size_t fprintf_duration(unsigned long t, FILE *fp)
1312{
1313 double duration = (double)t / NSEC_PER_MSEC;
1314 size_t printed = fprintf(fp, "(");
1315
1316 if (duration >= 1.0)
1317 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
1318 else if (duration >= 0.01)
1319 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
1320 else
1321 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001322 return printed + fprintf(fp, "): ");
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001323}
1324
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001325/**
1326 * filename.ptr: The filename char pointer that will be vfs_getname'd
1327 * filename.entry_str_pos: Where to insert the string translated from
1328 * filename.ptr by the vfs_getname tracepoint/kprobe.
1329 */
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001330struct thread_trace {
1331 u64 entry_time;
1332 u64 exit_time;
1333 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001334 unsigned long nr_events;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04001335 unsigned long pfmaj, pfmin;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001336 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03001337 double runtime_ms;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001338 struct {
1339 unsigned long ptr;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001340 short int entry_str_pos;
1341 bool pending_open;
1342 unsigned int namelen;
1343 char *name;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001344 } filename;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001345 struct {
1346 int max;
1347 char **table;
1348 } paths;
David Ahernbf2575c2013-10-08 21:26:53 -06001349
1350 struct intlist *syscall_stats;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001351};
1352
1353static struct thread_trace *thread_trace__new(void)
1354{
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001355 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
1356
1357 if (ttrace)
1358 ttrace->paths.max = -1;
1359
David Ahernbf2575c2013-10-08 21:26:53 -06001360 ttrace->syscall_stats = intlist__new(NULL);
1361
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001362 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001363}
1364
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001365static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001366{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001367 struct thread_trace *ttrace;
1368
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001369 if (thread == NULL)
1370 goto fail;
1371
Namhyung Kim89dceb22014-10-06 09:46:03 +09001372 if (thread__priv(thread) == NULL)
1373 thread__set_priv(thread, thread_trace__new());
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001374
Namhyung Kim89dceb22014-10-06 09:46:03 +09001375 if (thread__priv(thread) == NULL)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001376 goto fail;
1377
Namhyung Kim89dceb22014-10-06 09:46:03 +09001378 ttrace = thread__priv(thread);
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001379 ++ttrace->nr_events;
1380
1381 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001382fail:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001383 color_fprintf(fp, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001384 "WARNING: not enough memory, dropping samples!\n");
1385 return NULL;
1386}
1387
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04001388#define TRACE_PFMAJ (1 << 0)
1389#define TRACE_PFMIN (1 << 1)
1390
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001391static const size_t trace__entry_str_size = 2048;
1392
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001393struct trace {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001394 struct perf_tool tool;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001395 struct {
1396 int machine;
1397 int open_id;
1398 } audit;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001399 struct {
1400 int max;
1401 struct syscall *table;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03001402 struct {
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001403 struct perf_evsel *sys_enter,
1404 *sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03001405 } events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001406 } syscalls;
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -03001407 struct record_opts opts;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03001408 struct perf_evlist *evlist;
David Ahern8fb598e2013-09-28 13:13:00 -06001409 struct machine *host;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001410 struct thread *current;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001411 u64 base_time;
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001412 FILE *output;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001413 unsigned long nr_events;
Arnaldo Carvalho de Melob059efd2013-08-21 12:56:21 -03001414 struct strlist *ev_qualifier;
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001415 struct {
1416 size_t nr;
1417 int *entries;
1418 } ev_qualifier_ids;
David Ahernbdc89662013-08-28 22:29:53 -06001419 struct intlist *tid_list;
1420 struct intlist *pid_list;
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08001421 struct {
1422 size_t nr;
1423 pid_t *entries;
1424 } filter_pids;
Arnaldo Carvalho de Melo98eafce2014-01-06 15:43:02 -03001425 double duration_filter;
1426 double runtime_ms;
1427 struct {
1428 u64 vfs_getname,
1429 proc_getname;
1430 } stats;
1431 bool not_ev_qualifier;
1432 bool live;
1433 bool full_time;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03001434 bool sched;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001435 bool multiple_threads;
David Ahernbf2575c2013-10-08 21:26:53 -06001436 bool summary;
David Ahernfd2eaba2013-11-12 09:31:15 -07001437 bool summary_only;
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001438 bool show_comm;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001439 bool show_tool_stats;
Stanislav Fomicheve281a962014-06-26 20:14:28 +04001440 bool trace_syscalls;
Yunlong Songe366a6d2015-04-02 21:47:18 +08001441 bool force;
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03001442 bool vfs_getname;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04001443 int trace_pgfaults;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001444};
1445
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001446static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001447{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001448 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001449
1450 if (fd > ttrace->paths.max) {
1451 char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
1452
1453 if (npath == NULL)
1454 return -1;
1455
1456 if (ttrace->paths.max != -1) {
1457 memset(npath + ttrace->paths.max + 1, 0,
1458 (fd - ttrace->paths.max) * sizeof(char *));
1459 } else {
1460 memset(npath, 0, (fd + 1) * sizeof(char *));
1461 }
1462
1463 ttrace->paths.table = npath;
1464 ttrace->paths.max = fd;
1465 }
1466
1467 ttrace->paths.table[fd] = strdup(pathname);
1468
1469 return ttrace->paths.table[fd] != NULL ? 0 : -1;
1470}
1471
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001472static int thread__read_fd_path(struct thread *thread, int fd)
1473{
1474 char linkname[PATH_MAX], pathname[PATH_MAX];
1475 struct stat st;
1476 int ret;
1477
1478 if (thread->pid_ == thread->tid) {
1479 scnprintf(linkname, sizeof(linkname),
1480 "/proc/%d/fd/%d", thread->pid_, fd);
1481 } else {
1482 scnprintf(linkname, sizeof(linkname),
1483 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1484 }
1485
1486 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1487 return -1;
1488
1489 ret = readlink(linkname, pathname, sizeof(pathname));
1490
1491 if (ret < 0 || ret > st.st_size)
1492 return -1;
1493
1494 pathname[ret] = '\0';
1495 return trace__set_fd_pathname(thread, fd, pathname);
1496}
1497
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001498static const char *thread__fd_path(struct thread *thread, int fd,
1499 struct trace *trace)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001500{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001501 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001502
1503 if (ttrace == NULL)
1504 return NULL;
1505
1506 if (fd < 0)
1507 return NULL;
1508
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001509 if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001510 if (!trace->live)
1511 return NULL;
1512 ++trace->stats.proc_getname;
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001513 if (thread__read_fd_path(thread, fd))
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001514 return NULL;
1515 }
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001516
1517 return ttrace->paths.table[fd];
1518}
1519
1520static size_t syscall_arg__scnprintf_fd(char *bf, size_t size,
1521 struct syscall_arg *arg)
1522{
1523 int fd = arg->val;
1524 size_t printed = scnprintf(bf, size, "%d", fd);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001525 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001526
1527 if (path)
1528 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1529
1530 return printed;
1531}
1532
1533static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1534 struct syscall_arg *arg)
1535{
1536 int fd = arg->val;
1537 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
Namhyung Kim89dceb22014-10-06 09:46:03 +09001538 struct thread_trace *ttrace = thread__priv(arg->thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001539
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001540 if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
1541 zfree(&ttrace->paths.table[fd]);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001542
1543 return printed;
1544}
1545
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001546static void thread__set_filename_pos(struct thread *thread, const char *bf,
1547 unsigned long ptr)
1548{
1549 struct thread_trace *ttrace = thread__priv(thread);
1550
1551 ttrace->filename.ptr = ptr;
1552 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1553}
1554
1555static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1556 struct syscall_arg *arg)
1557{
1558 unsigned long ptr = arg->val;
1559
1560 if (!arg->trace->vfs_getname)
1561 return scnprintf(bf, size, "%#x", ptr);
1562
1563 thread__set_filename_pos(arg->thread, bf, ptr);
1564 return 0;
1565}
1566
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001567static bool trace__filter_duration(struct trace *trace, double t)
1568{
1569 return t < (trace->duration_filter * NSEC_PER_MSEC);
1570}
1571
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001572static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1573{
1574 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1575
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001576 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001577}
1578
Namhyung Kimf15eb532012-10-05 14:02:16 +09001579static bool done = false;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001580static bool interrupted = false;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001581
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001582static void sig_handler(int sig)
Namhyung Kimf15eb532012-10-05 14:02:16 +09001583{
1584 done = true;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001585 interrupted = sig == SIGINT;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001586}
1587
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001588static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001589 u64 duration, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001590{
1591 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001592 printed += fprintf_duration(duration, fp);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001593
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001594 if (trace->multiple_threads) {
1595 if (trace->show_comm)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02001596 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
Adrian Hunter38051232013-07-04 16:20:31 +03001597 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001598 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001599
1600 return printed;
1601}
1602
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001603static int trace__process_event(struct trace *trace, struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001604 union perf_event *event, struct perf_sample *sample)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001605{
1606 int ret = 0;
1607
1608 switch (event->header.type) {
1609 case PERF_RECORD_LOST:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001610 color_fprintf(trace->output, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001611 "LOST %" PRIu64 " events!\n", event->lost.lost);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001612 ret = machine__process_lost_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001613 default:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001614 ret = machine__process_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001615 break;
1616 }
1617
1618 return ret;
1619}
1620
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001621static int trace__tool_process(struct perf_tool *tool,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001622 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001623 struct perf_sample *sample,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001624 struct machine *machine)
1625{
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001626 struct trace *trace = container_of(tool, struct trace, tool);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001627 return trace__process_event(trace, machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001628}
1629
1630static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1631{
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001632 int err = symbol__init(NULL);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001633
1634 if (err)
1635 return err;
1636
David Ahern8fb598e2013-09-28 13:13:00 -06001637 trace->host = machine__new_host();
1638 if (trace->host == NULL)
1639 return -ENOMEM;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001640
Arnaldo Carvalho de Melo959c2192015-07-24 12:13:05 -03001641 if (trace_event__register_resolver(trace->host, machine__resolve_kernel_addr) < 0)
Arnaldo Carvalho de Melo706c3da2015-07-22 16:16:16 -03001642 return -errno;
1643
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001644 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001645 evlist->threads, trace__tool_process, false,
1646 trace->opts.proc_map_timeout);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001647 if (err)
1648 symbol__exit();
1649
1650 return err;
1651}
1652
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001653static int syscall__set_arg_fmts(struct syscall *sc)
1654{
1655 struct format_field *field;
1656 int idx = 0;
1657
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001658 sc->arg_scnprintf = calloc(sc->nr_args, sizeof(void *));
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001659 if (sc->arg_scnprintf == NULL)
1660 return -1;
1661
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -03001662 if (sc->fmt)
1663 sc->arg_parm = sc->fmt->arg_parm;
1664
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001665 for (field = sc->args; field; field = field->next) {
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -03001666 if (sc->fmt && sc->fmt->arg_scnprintf[idx])
1667 sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
1668 else if (field->flags & FIELD_IS_POINTER)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001669 sc->arg_scnprintf[idx] = syscall_arg__scnprintf_hex;
1670 ++idx;
1671 }
1672
1673 return 0;
1674}
1675
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001676static int trace__read_syscall_info(struct trace *trace, int id)
1677{
1678 char tp_name[128];
1679 struct syscall *sc;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001680 const char *name = audit_syscall_to_name(id, trace->audit.machine);
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001681
1682 if (name == NULL)
1683 return -1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001684
1685 if (id > trace->syscalls.max) {
1686 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1687
1688 if (nsyscalls == NULL)
1689 return -1;
1690
1691 if (trace->syscalls.max != -1) {
1692 memset(nsyscalls + trace->syscalls.max + 1, 0,
1693 (id - trace->syscalls.max) * sizeof(*sc));
1694 } else {
1695 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1696 }
1697
1698 trace->syscalls.table = nsyscalls;
1699 trace->syscalls.max = id;
1700 }
1701
1702 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001703 sc->name = name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001704
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001705 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001706
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001707 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
Jiri Olsa97978b32013-12-03 14:09:24 +01001708 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001709
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001710 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001711 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
Jiri Olsa97978b32013-12-03 14:09:24 +01001712 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001713 }
1714
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001715 if (IS_ERR(sc->tp_format))
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001716 return -1;
1717
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001718 sc->args = sc->tp_format->format.fields;
1719 sc->nr_args = sc->tp_format->format.nr_fields;
1720 /* drop nr field - not relevant here; does not exist on older kernels */
1721 if (sc->args && strcmp(sc->args->name, "nr") == 0) {
1722 sc->args = sc->args->next;
1723 --sc->nr_args;
1724 }
1725
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001726 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
1727
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001728 return syscall__set_arg_fmts(sc);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001729}
1730
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001731static int trace__validate_ev_qualifier(struct trace *trace)
1732{
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001733 int err = 0, i;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001734 struct str_node *pos;
1735
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001736 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1737 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1738 sizeof(trace->ev_qualifier_ids.entries[0]));
1739
1740 if (trace->ev_qualifier_ids.entries == NULL) {
1741 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1742 trace->output);
1743 err = -EINVAL;
1744 goto out;
1745 }
1746
1747 i = 0;
1748
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001749 strlist__for_each(pos, trace->ev_qualifier) {
1750 const char *sc = pos->s;
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001751 int id = audit_name_to_syscall(sc, trace->audit.machine);
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001752
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001753 if (id < 0) {
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001754 if (err == 0) {
1755 fputs("Error:\tInvalid syscall ", trace->output);
1756 err = -EINVAL;
1757 } else {
1758 fputs(", ", trace->output);
1759 }
1760
1761 fputs(sc, trace->output);
1762 }
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001763
1764 trace->ev_qualifier_ids.entries[i++] = id;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001765 }
1766
1767 if (err < 0) {
1768 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1769 "\nHint:\tand: 'man syscalls'\n", trace->output);
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001770 zfree(&trace->ev_qualifier_ids.entries);
1771 trace->ev_qualifier_ids.nr = 0;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001772 }
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001773out:
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001774 return err;
1775}
1776
David Ahern55d43bc2015-02-19 15:00:22 -05001777/*
1778 * args is to be interpreted as a series of longs but we need to handle
1779 * 8-byte unaligned accesses. args points to raw_data within the event
1780 * and raw_data is guaranteed to be 8-byte unaligned because it is
1781 * preceded by raw_size which is a u32. So we need to copy args to a temp
1782 * variable to read it. Most notably this avoids extended load instructions
1783 * on unaligned addresses
1784 */
1785
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001786static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
David Ahern55d43bc2015-02-19 15:00:22 -05001787 unsigned char *args, struct trace *trace,
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001788 struct thread *thread)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001789{
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001790 size_t printed = 0;
David Ahern55d43bc2015-02-19 15:00:22 -05001791 unsigned char *p;
1792 unsigned long val;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001793
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001794 if (sc->args != NULL) {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001795 struct format_field *field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001796 u8 bit = 1;
1797 struct syscall_arg arg = {
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001798 .idx = 0,
1799 .mask = 0,
1800 .trace = trace,
1801 .thread = thread,
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001802 };
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001803
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001804 for (field = sc->args; field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001805 field = field->next, ++arg.idx, bit <<= 1) {
1806 if (arg.mask & bit)
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001807 continue;
David Ahern55d43bc2015-02-19 15:00:22 -05001808
1809 /* special care for unaligned accesses */
1810 p = args + sizeof(unsigned long) * arg.idx;
1811 memcpy(&val, p, sizeof(val));
1812
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001813 /*
1814 * Suppress this argument if its value is zero and
1815 * and we don't have a string associated in an
1816 * strarray for it.
1817 */
David Ahern55d43bc2015-02-19 15:00:22 -05001818 if (val == 0 &&
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001819 !(sc->arg_scnprintf &&
1820 sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
1821 sc->arg_parm[arg.idx]))
Arnaldo Carvalho de Melo22ae5cf12013-09-12 11:27:34 -03001822 continue;
1823
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001824 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001825 "%s%s: ", printed ? ", " : "", field->name);
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001826 if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
David Ahern55d43bc2015-02-19 15:00:22 -05001827 arg.val = val;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -03001828 if (sc->arg_parm)
1829 arg.parm = sc->arg_parm[arg.idx];
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001830 printed += sc->arg_scnprintf[arg.idx](bf + printed,
1831 size - printed, &arg);
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001832 } else {
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001833 printed += scnprintf(bf + printed, size - printed,
David Ahern55d43bc2015-02-19 15:00:22 -05001834 "%ld", val);
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001835 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001836 }
1837 } else {
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001838 int i = 0;
1839
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001840 while (i < 6) {
David Ahern55d43bc2015-02-19 15:00:22 -05001841 /* special care for unaligned accesses */
1842 p = args + sizeof(unsigned long) * i;
1843 memcpy(&val, p, sizeof(val));
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001844 printed += scnprintf(bf + printed, size - printed,
1845 "%sarg%d: %ld",
David Ahern55d43bc2015-02-19 15:00:22 -05001846 printed ? ", " : "", i, val);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001847 ++i;
1848 }
1849 }
1850
1851 return printed;
1852}
1853
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001854typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001855 union perf_event *event,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001856 struct perf_sample *sample);
1857
1858static struct syscall *trace__syscall_info(struct trace *trace,
David Ahernbf2575c2013-10-08 21:26:53 -06001859 struct perf_evsel *evsel, int id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001860{
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001861
1862 if (id < 0) {
Arnaldo Carvalho de Meloadaa18b2013-08-22 17:55:25 -03001863
1864 /*
1865 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1866 * before that, leaving at a higher verbosity level till that is
1867 * explained. Reproduced with plain ftrace with:
1868 *
1869 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1870 * grep "NR -1 " /t/trace_pipe
1871 *
1872 * After generating some load on the machine.
1873 */
1874 if (verbose > 1) {
1875 static u64 n;
1876 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1877 id, perf_evsel__name(evsel), ++n);
1878 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001879 return NULL;
1880 }
1881
1882 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1883 trace__read_syscall_info(trace, id))
1884 goto out_cant_read;
1885
1886 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1887 goto out_cant_read;
1888
1889 return &trace->syscalls.table[id];
1890
1891out_cant_read:
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03001892 if (verbose) {
1893 fprintf(trace->output, "Problems reading syscall %d", id);
1894 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1895 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1896 fputs(" information\n", trace->output);
1897 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001898 return NULL;
1899}
1900
David Ahernbf2575c2013-10-08 21:26:53 -06001901static void thread__update_stats(struct thread_trace *ttrace,
1902 int id, struct perf_sample *sample)
1903{
1904 struct int_node *inode;
1905 struct stats *stats;
1906 u64 duration = 0;
1907
1908 inode = intlist__findnew(ttrace->syscall_stats, id);
1909 if (inode == NULL)
1910 return;
1911
1912 stats = inode->priv;
1913 if (stats == NULL) {
1914 stats = malloc(sizeof(struct stats));
1915 if (stats == NULL)
1916 return;
1917 init_stats(stats);
1918 inode->priv = stats;
1919 }
1920
1921 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1922 duration = sample->time - ttrace->entry_time;
1923
1924 update_stats(stats, duration);
1925}
1926
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001927static int trace__printf_interrupted_entry(struct trace *trace, struct perf_sample *sample)
1928{
1929 struct thread_trace *ttrace;
1930 u64 duration;
1931 size_t printed;
1932
1933 if (trace->current == NULL)
1934 return 0;
1935
1936 ttrace = thread__priv(trace->current);
1937
1938 if (!ttrace->entry_pending)
1939 return 0;
1940
1941 duration = sample->time - ttrace->entry_time;
1942
1943 printed = trace__fprintf_entry_head(trace, trace->current, duration, sample->time, trace->output);
1944 printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
1945 ttrace->entry_pending = false;
1946
1947 return printed;
1948}
1949
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001950static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001951 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001952 struct perf_sample *sample)
1953{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001954 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001955 void *args;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001956 size_t printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001957 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001958 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
David Ahernbf2575c2013-10-08 21:26:53 -06001959 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001960 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001961
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001962 if (sc == NULL)
1963 return -1;
1964
David Ahern8fb598e2013-09-28 13:13:00 -06001965 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001966 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001967 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001968 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001969
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001970 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001971
1972 if (ttrace->entry_str == NULL) {
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001973 ttrace->entry_str = malloc(trace__entry_str_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001974 if (!ttrace->entry_str)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001975 goto out_put;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001976 }
1977
David Ahern13f22a22015-03-19 12:23:03 -06001978 if (!trace->summary_only)
Arnaldo Carvalho de Melo6ebad5c2015-03-25 18:01:15 -03001979 trace__printf_interrupted_entry(trace, sample);
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001980
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001981 ttrace->entry_time = sample->time;
1982 msg = ttrace->entry_str;
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001983 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001984
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001985 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001986 args, trace, thread);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001987
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001988 if (sc->is_exit) {
David Ahernfd2eaba2013-11-12 09:31:15 -07001989 if (!trace->duration_filter && !trace->summary_only) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001990 trace__fprintf_entry_head(trace, thread, 1, sample->time, trace->output);
1991 fprintf(trace->output, "%-70s\n", ttrace->entry_str);
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001992 }
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001993 } else {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001994 ttrace->entry_pending = true;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001995 /* See trace__vfs_getname & trace__sys_exit */
1996 ttrace->filename.pending_open = false;
1997 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001998
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001999 if (trace->current != thread) {
2000 thread__put(trace->current);
2001 trace->current = thread__get(thread);
2002 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002003 err = 0;
2004out_put:
2005 thread__put(thread);
2006 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002007}
2008
2009static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002010 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002011 struct perf_sample *sample)
2012{
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09002013 long ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002014 u64 duration = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002015 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002016 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
David Ahernbf2575c2013-10-08 21:26:53 -06002017 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002018 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002019
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002020 if (sc == NULL)
2021 return -1;
2022
David Ahern8fb598e2013-09-28 13:13:00 -06002023 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002024 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002025 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002026 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002027
David Ahernbf2575c2013-10-08 21:26:53 -06002028 if (trace->summary)
2029 thread__update_stats(ttrace, id, sample);
2030
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03002031 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002032
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002033 if (id == trace->audit.open_id && ret >= 0 && ttrace->filename.pending_open) {
2034 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
2035 ttrace->filename.pending_open = false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002036 ++trace->stats.vfs_getname;
2037 }
2038
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002039 ttrace->exit_time = sample->time;
2040
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002041 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002042 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002043 if (trace__filter_duration(trace, duration))
2044 goto out;
2045 } else if (trace->duration_filter)
2046 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002047
David Ahernfd2eaba2013-11-12 09:31:15 -07002048 if (trace->summary_only)
2049 goto out;
2050
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002051 trace__fprintf_entry_head(trace, thread, duration, sample->time, trace->output);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002052
2053 if (ttrace->entry_pending) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002054 fprintf(trace->output, "%-70s", ttrace->entry_str);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002055 } else {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002056 fprintf(trace->output, " ... [");
2057 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
2058 fprintf(trace->output, "]: %s()", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002059 }
2060
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002061 if (sc->fmt == NULL) {
2062signed_print:
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09002063 fprintf(trace->output, ") = %ld", ret);
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002064 } else if (ret < 0 && sc->fmt->errmsg) {
Masami Hiramatsu942a91e2014-08-14 02:22:41 +00002065 char bf[STRERR_BUFSIZE];
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002066 const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
2067 *e = audit_errno_to_name(-ret);
2068
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002069 fprintf(trace->output, ") = -1 %s %s", e, emsg);
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002070 } else if (ret == 0 && sc->fmt->timeout)
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002071 fprintf(trace->output, ") = 0 Timeout");
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -03002072 else if (sc->fmt->hexret)
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09002073 fprintf(trace->output, ") = %#lx", ret);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002074 else
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002075 goto signed_print;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002076
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002077 fputc('\n', trace->output);
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002078out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002079 ttrace->entry_pending = false;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002080 err = 0;
2081out_put:
2082 thread__put(thread);
2083 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002084}
2085
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002086static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002087 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002088 struct perf_sample *sample)
2089{
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002090 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2091 struct thread_trace *ttrace;
2092 size_t filename_len, entry_str_len, to_move;
2093 ssize_t remaining_space;
2094 char *pos;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002095 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002096
2097 if (!thread)
2098 goto out;
2099
2100 ttrace = thread__priv(thread);
2101 if (!ttrace)
2102 goto out;
2103
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002104 filename_len = strlen(filename);
2105
2106 if (ttrace->filename.namelen < filename_len) {
2107 char *f = realloc(ttrace->filename.name, filename_len + 1);
2108
2109 if (f == NULL)
2110 goto out;
2111
2112 ttrace->filename.namelen = filename_len;
2113 ttrace->filename.name = f;
2114 }
2115
2116 strcpy(ttrace->filename.name, filename);
2117 ttrace->filename.pending_open = true;
2118
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002119 if (!ttrace->filename.ptr)
2120 goto out;
2121
2122 entry_str_len = strlen(ttrace->entry_str);
2123 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2124 if (remaining_space <= 0)
2125 goto out;
2126
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002127 if (filename_len > (size_t)remaining_space) {
2128 filename += filename_len - remaining_space;
2129 filename_len = remaining_space;
2130 }
2131
2132 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2133 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2134 memmove(pos + filename_len, pos, to_move);
2135 memcpy(pos, filename, filename_len);
2136
2137 ttrace->filename.ptr = 0;
2138 ttrace->filename.entry_str_pos = 0;
2139out:
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002140 return 0;
2141}
2142
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002143static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002144 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002145 struct perf_sample *sample)
2146{
2147 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2148 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
David Ahern8fb598e2013-09-28 13:13:00 -06002149 struct thread *thread = machine__findnew_thread(trace->host,
Adrian Hunter314add62013-08-27 11:23:03 +03002150 sample->pid,
2151 sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002152 struct thread_trace *ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002153
2154 if (ttrace == NULL)
2155 goto out_dump;
2156
2157 ttrace->runtime_ms += runtime_ms;
2158 trace->runtime_ms += runtime_ms;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002159 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002160 return 0;
2161
2162out_dump:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002163 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002164 evsel->name,
2165 perf_evsel__strval(evsel, sample, "comm"),
2166 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2167 runtime,
2168 perf_evsel__intval(evsel, sample, "vruntime"));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002169 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002170 return 0;
2171}
2172
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002173static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2174 union perf_event *event __maybe_unused,
2175 struct perf_sample *sample)
2176{
2177 trace__printf_interrupted_entry(trace, sample);
2178 trace__fprintf_tstamp(trace, sample->time, trace->output);
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002179
2180 if (trace->trace_syscalls)
2181 fprintf(trace->output, "( ): ");
2182
2183 fprintf(trace->output, "%s:", evsel->name);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002184
2185 if (evsel->tp_format) {
2186 event_format__fprintf(evsel->tp_format, sample->cpu,
2187 sample->raw_data, sample->raw_size,
2188 trace->output);
2189 }
2190
2191 fprintf(trace->output, ")\n");
2192 return 0;
2193}
2194
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002195static void print_location(FILE *f, struct perf_sample *sample,
2196 struct addr_location *al,
2197 bool print_dso, bool print_sym)
2198{
2199
2200 if ((verbose || print_dso) && al->map)
2201 fprintf(f, "%s@", al->map->dso->long_name);
2202
2203 if ((verbose || print_sym) && al->sym)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002204 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002205 al->addr - al->sym->start);
2206 else if (al->map)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002207 fprintf(f, "0x%" PRIx64, al->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002208 else
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002209 fprintf(f, "0x%" PRIx64, sample->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002210}
2211
2212static int trace__pgfault(struct trace *trace,
2213 struct perf_evsel *evsel,
2214 union perf_event *event,
2215 struct perf_sample *sample)
2216{
2217 struct thread *thread;
2218 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2219 struct addr_location al;
2220 char map_type = 'd';
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002221 struct thread_trace *ttrace;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002222 int err = -1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002223
2224 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002225 ttrace = thread__trace(thread, trace->output);
2226 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002227 goto out_put;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002228
2229 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2230 ttrace->pfmaj++;
2231 else
2232 ttrace->pfmin++;
2233
2234 if (trace->summary_only)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002235 goto out;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002236
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002237 thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002238 sample->ip, &al);
2239
2240 trace__fprintf_entry_head(trace, thread, 0, sample->time, trace->output);
2241
2242 fprintf(trace->output, "%sfault [",
2243 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2244 "maj" : "min");
2245
2246 print_location(trace->output, sample, &al, false, true);
2247
2248 fprintf(trace->output, "] => ");
2249
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002250 thread__find_addr_location(thread, cpumode, MAP__VARIABLE,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002251 sample->addr, &al);
2252
2253 if (!al.map) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -03002254 thread__find_addr_location(thread, cpumode,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002255 MAP__FUNCTION, sample->addr, &al);
2256
2257 if (al.map)
2258 map_type = 'x';
2259 else
2260 map_type = '?';
2261 }
2262
2263 print_location(trace->output, sample, &al, true, false);
2264
2265 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002266out:
2267 err = 0;
2268out_put:
2269 thread__put(thread);
2270 return err;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002271}
2272
David Ahernbdc89662013-08-28 22:29:53 -06002273static bool skip_sample(struct trace *trace, struct perf_sample *sample)
2274{
2275 if ((trace->pid_list && intlist__find(trace->pid_list, sample->pid)) ||
2276 (trace->tid_list && intlist__find(trace->tid_list, sample->tid)))
2277 return false;
2278
2279 if (trace->pid_list || trace->tid_list)
2280 return true;
2281
2282 return false;
2283}
2284
David Ahern6810fc92013-08-28 22:29:52 -06002285static int trace__process_sample(struct perf_tool *tool,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002286 union perf_event *event,
David Ahern6810fc92013-08-28 22:29:52 -06002287 struct perf_sample *sample,
2288 struct perf_evsel *evsel,
2289 struct machine *machine __maybe_unused)
2290{
2291 struct trace *trace = container_of(tool, struct trace, tool);
2292 int err = 0;
2293
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002294 tracepoint_handler handler = evsel->handler;
David Ahern6810fc92013-08-28 22:29:52 -06002295
David Ahernbdc89662013-08-28 22:29:53 -06002296 if (skip_sample(trace, sample))
2297 return 0;
2298
David Ahern4bb09192013-09-04 12:37:43 -06002299 if (!trace->full_time && trace->base_time == 0)
David Ahern6810fc92013-08-28 22:29:52 -06002300 trace->base_time = sample->time;
2301
David Ahern31605652013-12-04 19:41:41 -07002302 if (handler) {
2303 ++trace->nr_events;
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002304 handler(trace, evsel, event, sample);
David Ahern31605652013-12-04 19:41:41 -07002305 }
David Ahern6810fc92013-08-28 22:29:52 -06002306
2307 return err;
2308}
2309
David Ahernbdc89662013-08-28 22:29:53 -06002310static int parse_target_str(struct trace *trace)
2311{
2312 if (trace->opts.target.pid) {
2313 trace->pid_list = intlist__new(trace->opts.target.pid);
2314 if (trace->pid_list == NULL) {
2315 pr_err("Error parsing process id string\n");
2316 return -EINVAL;
2317 }
2318 }
2319
2320 if (trace->opts.target.tid) {
2321 trace->tid_list = intlist__new(trace->opts.target.tid);
2322 if (trace->tid_list == NULL) {
2323 pr_err("Error parsing thread id string\n");
2324 return -EINVAL;
2325 }
2326 }
2327
2328 return 0;
2329}
2330
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002331static int trace__record(struct trace *trace, int argc, const char **argv)
David Ahern5e2485b2013-09-28 13:13:01 -06002332{
2333 unsigned int rec_argc, i, j;
2334 const char **rec_argv;
2335 const char * const record_args[] = {
2336 "record",
2337 "-R",
2338 "-m", "1024",
2339 "-c", "1",
David Ahern5e2485b2013-09-28 13:13:01 -06002340 };
2341
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002342 const char * const sc_args[] = { "-e", };
2343 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2344 const char * const majpf_args[] = { "-e", "major-faults" };
2345 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2346 const char * const minpf_args[] = { "-e", "minor-faults" };
2347 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2348
David Ahern9aca7f12013-12-04 19:41:39 -07002349 /* +1 is for the event string below */
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002350 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2351 majpf_args_nr + minpf_args_nr + argc;
David Ahern5e2485b2013-09-28 13:13:01 -06002352 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2353
2354 if (rec_argv == NULL)
2355 return -ENOMEM;
2356
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002357 j = 0;
David Ahern5e2485b2013-09-28 13:13:01 -06002358 for (i = 0; i < ARRAY_SIZE(record_args); i++)
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002359 rec_argv[j++] = record_args[i];
2360
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002361 if (trace->trace_syscalls) {
2362 for (i = 0; i < sc_args_nr; i++)
2363 rec_argv[j++] = sc_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002364
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002365 /* event string may be different for older kernels - e.g., RHEL6 */
2366 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2367 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2368 else if (is_valid_tracepoint("syscalls:sys_enter"))
2369 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2370 else {
2371 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
2372 return -1;
2373 }
David Ahern9aca7f12013-12-04 19:41:39 -07002374 }
David Ahern9aca7f12013-12-04 19:41:39 -07002375
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002376 if (trace->trace_pgfaults & TRACE_PFMAJ)
2377 for (i = 0; i < majpf_args_nr; i++)
2378 rec_argv[j++] = majpf_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002379
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002380 if (trace->trace_pgfaults & TRACE_PFMIN)
2381 for (i = 0; i < minpf_args_nr; i++)
2382 rec_argv[j++] = minpf_args[i];
2383
2384 for (i = 0; i < (unsigned int)argc; i++)
2385 rec_argv[j++] = argv[i];
2386
2387 return cmd_record(j, rec_argv, NULL);
David Ahern5e2485b2013-09-28 13:13:01 -06002388}
2389
David Ahernbf2575c2013-10-08 21:26:53 -06002390static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2391
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002392static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002393{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002394 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002395
2396 if (IS_ERR(evsel))
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002397 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002398
2399 if (perf_evsel__field(evsel, "pathname") == NULL) {
2400 perf_evsel__delete(evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002401 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002402 }
2403
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002404 evsel->handler = trace__vfs_getname;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002405 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002406 return true;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002407}
2408
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002409static int perf_evlist__add_pgfault(struct perf_evlist *evlist,
2410 u64 config)
2411{
2412 struct perf_evsel *evsel;
2413 struct perf_event_attr attr = {
2414 .type = PERF_TYPE_SOFTWARE,
2415 .mmap_data = 1,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002416 };
2417
2418 attr.config = config;
Arnaldo Carvalho de Melo05247982014-07-23 18:15:09 -03002419 attr.sample_period = 1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002420
2421 event_attr_init(&attr);
2422
2423 evsel = perf_evsel__new(&attr);
2424 if (!evsel)
2425 return -ENOMEM;
2426
2427 evsel->handler = trace__pgfault;
2428 perf_evlist__add(evlist, evsel);
2429
2430 return 0;
2431}
2432
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002433static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2434{
2435 const u32 type = event->header.type;
2436 struct perf_evsel *evsel;
2437
2438 if (!trace->full_time && trace->base_time == 0)
2439 trace->base_time = sample->time;
2440
2441 if (type != PERF_RECORD_SAMPLE) {
2442 trace__process_event(trace, trace->host, event, sample);
2443 return;
2444 }
2445
2446 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2447 if (evsel == NULL) {
2448 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2449 return;
2450 }
2451
2452 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2453 sample->raw_data == NULL) {
2454 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2455 perf_evsel__name(evsel), sample->tid,
2456 sample->cpu, sample->raw_size);
2457 } else {
2458 tracepoint_handler handler = evsel->handler;
2459 handler(trace, evsel, event, sample);
2460 }
2461}
2462
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002463static int trace__add_syscall_newtp(struct trace *trace)
2464{
2465 int ret = -1;
2466 struct perf_evlist *evlist = trace->evlist;
2467 struct perf_evsel *sys_enter, *sys_exit;
2468
2469 sys_enter = perf_evsel__syscall_newtp("sys_enter", trace__sys_enter);
2470 if (sys_enter == NULL)
2471 goto out;
2472
2473 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2474 goto out_delete_sys_enter;
2475
2476 sys_exit = perf_evsel__syscall_newtp("sys_exit", trace__sys_exit);
2477 if (sys_exit == NULL)
2478 goto out_delete_sys_enter;
2479
2480 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2481 goto out_delete_sys_exit;
2482
2483 perf_evlist__add(evlist, sys_enter);
2484 perf_evlist__add(evlist, sys_exit);
2485
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03002486 trace->syscalls.events.sys_enter = sys_enter;
2487 trace->syscalls.events.sys_exit = sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002488
2489 ret = 0;
2490out:
2491 return ret;
2492
2493out_delete_sys_exit:
2494 perf_evsel__delete_priv(sys_exit);
2495out_delete_sys_enter:
2496 perf_evsel__delete_priv(sys_enter);
2497 goto out;
2498}
2499
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002500static int trace__set_ev_qualifier_filter(struct trace *trace)
2501{
2502 int err = -1;
2503 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2504 trace->ev_qualifier_ids.nr,
2505 trace->ev_qualifier_ids.entries);
2506
2507 if (filter == NULL)
2508 goto out_enomem;
2509
2510 if (!perf_evsel__append_filter(trace->syscalls.events.sys_enter, "&&", filter))
2511 err = perf_evsel__append_filter(trace->syscalls.events.sys_exit, "&&", filter);
2512
2513 free(filter);
2514out:
2515 return err;
2516out_enomem:
2517 errno = ENOMEM;
2518 goto out;
2519}
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002520
Namhyung Kimf15eb532012-10-05 14:02:16 +09002521static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002522{
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002523 struct perf_evlist *evlist = trace->evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002524 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002525 int err = -1, i;
2526 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002527 const bool forks = argc > 0;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002528 bool draining = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002529
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002530 trace->live = true;
2531
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002532 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03002533 goto out_error_raw_syscalls;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002534
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002535 if (trace->trace_syscalls)
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002536 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002537
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002538 if ((trace->trace_pgfaults & TRACE_PFMAJ) &&
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03002539 perf_evlist__add_pgfault(evlist, PERF_COUNT_SW_PAGE_FAULTS_MAJ)) {
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03002540 goto out_error_mem;
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03002541 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002542
2543 if ((trace->trace_pgfaults & TRACE_PFMIN) &&
2544 perf_evlist__add_pgfault(evlist, PERF_COUNT_SW_PAGE_FAULTS_MIN))
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03002545 goto out_error_mem;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002546
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002547 if (trace->sched &&
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002548 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2549 trace__sched_stat_runtime))
2550 goto out_error_sched_stat_runtime;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002551
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002552 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2553 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002554 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002555 goto out_delete_evlist;
2556 }
2557
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002558 err = trace__symbols_init(trace, evlist);
2559 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002560 fprintf(trace->output, "Problems initializing symbol libraries!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002561 goto out_delete_evlist;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002562 }
2563
Arnaldo Carvalho de Melof77a9512012-12-10 16:41:31 -03002564 perf_evlist__config(evlist, &trace->opts);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002565
Namhyung Kimf15eb532012-10-05 14:02:16 +09002566 signal(SIGCHLD, sig_handler);
2567 signal(SIGINT, sig_handler);
2568
2569 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09002570 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03002571 argv, false, NULL);
Namhyung Kimf15eb532012-10-05 14:02:16 +09002572 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002573 fprintf(trace->output, "Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002574 goto out_delete_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002575 }
2576 }
2577
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002578 err = perf_evlist__open(evlist);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002579 if (err < 0)
2580 goto out_error_open;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002581
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08002582 /*
2583 * Better not use !target__has_task() here because we need to cover the
2584 * case where no threads were specified in the command line, but a
2585 * workload was, and in that case we will fill in the thread_map when
2586 * we fork the workload in perf_evlist__prepare_workload.
2587 */
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08002588 if (trace->filter_pids.nr > 0)
2589 err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
Jiri Olsae13798c2015-06-23 00:36:02 +02002590 else if (thread_map__pid(evlist->threads, 0) == -1)
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08002591 err = perf_evlist__set_filter_pid(evlist, getpid());
2592
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002593 if (err < 0)
2594 goto out_error_mem;
2595
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002596 if (trace->ev_qualifier_ids.nr > 0) {
2597 err = trace__set_ev_qualifier_filter(trace);
2598 if (err < 0)
2599 goto out_errno;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002600
Arnaldo Carvalho de Melo2e5e5f82015-08-03 17:12:29 -03002601 pr_debug("event qualifier tracepoint filter: %s\n",
2602 trace->syscalls.events.sys_exit->filter);
2603 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002604
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002605 err = perf_evlist__apply_filters(evlist, &evsel);
2606 if (err < 0)
2607 goto out_error_apply_filters;
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08002608
Jiri Olsaf8850372013-11-28 17:57:22 +01002609 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages, false);
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002610 if (err < 0)
2611 goto out_error_mmap;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002612
Arnaldo Carvalho de Melocb24d012015-04-22 10:04:23 -03002613 if (!target__none(&trace->opts.target))
2614 perf_evlist__enable(evlist);
2615
Namhyung Kimf15eb532012-10-05 14:02:16 +09002616 if (forks)
2617 perf_evlist__start_workload(evlist);
2618
Jiri Olsae13798c2015-06-23 00:36:02 +02002619 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
Arnaldo Carvalho de Melo42052be2015-02-13 12:32:45 -03002620 evlist->threads->nr > 1 ||
2621 perf_evlist__first(evlist)->attr.inherit;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002622again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002623 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002624
2625 for (i = 0; i < evlist->nr_mmaps; i++) {
2626 union perf_event *event;
2627
2628 while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002629 struct perf_sample sample;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002630
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002631 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002632
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002633 err = perf_evlist__parse_sample(evlist, event, &sample);
2634 if (err) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002635 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +08002636 goto next_event;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002637 }
2638
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002639 trace__handle_event(trace, event, &sample);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +08002640next_event:
2641 perf_evlist__mmap_consume(evlist, i);
Arnaldo Carvalho de Melo20c5f102013-09-03 11:55:07 -03002642
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002643 if (interrupted)
2644 goto out_disable;
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03002645
2646 if (done && !draining) {
2647 perf_evlist__disable(evlist);
2648 draining = true;
2649 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002650 }
2651 }
2652
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002653 if (trace->nr_events == before) {
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002654 int timeout = done ? 100 : -1;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002655
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002656 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
2657 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0)
2658 draining = true;
2659
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002660 goto again;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002661 }
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002662 } else {
2663 goto again;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002664 }
2665
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002666out_disable:
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03002667 thread__zput(trace->current);
2668
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002669 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002670
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002671 if (!err) {
2672 if (trace->summary)
2673 trace__fprintf_thread_summary(trace, trace->output);
2674
2675 if (trace->show_tool_stats) {
2676 fprintf(trace->output, "Stats:\n "
2677 " vfs_getname : %" PRIu64 "\n"
2678 " proc_getname: %" PRIu64 "\n",
2679 trace->stats.vfs_getname,
2680 trace->stats.proc_getname);
2681 }
2682 }
David Ahernbf2575c2013-10-08 21:26:53 -06002683
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002684out_delete_evlist:
2685 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002686 trace->evlist = NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002687 trace->live = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002688 return err;
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03002689{
2690 char errbuf[BUFSIZ];
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002691
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002692out_error_sched_stat_runtime:
Jiri Olsa988bdb32015-09-02 09:56:35 +02002693 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002694 goto out_error;
2695
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03002696out_error_raw_syscalls:
Jiri Olsa988bdb32015-09-02 09:56:35 +02002697 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002698 goto out_error;
2699
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002700out_error_mmap:
2701 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
2702 goto out_error;
2703
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002704out_error_open:
2705 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
2706
2707out_error:
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03002708 fprintf(trace->output, "%s\n", errbuf);
Ramkumar Ramachandra87f91862013-10-04 10:47:31 +05302709 goto out_delete_evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002710
2711out_error_apply_filters:
2712 fprintf(trace->output,
2713 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
2714 evsel->filter, perf_evsel__name(evsel), errno,
2715 strerror_r(errno, errbuf, sizeof(errbuf)));
2716 goto out_delete_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002717}
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03002718out_error_mem:
2719 fprintf(trace->output, "Not enough memory to run!\n");
2720 goto out_delete_evlist;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002721
2722out_errno:
2723 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
2724 goto out_delete_evlist;
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002725}
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002726
David Ahern6810fc92013-08-28 22:29:52 -06002727static int trace__replay(struct trace *trace)
2728{
2729 const struct perf_evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002730 { "probe:vfs_getname", trace__vfs_getname, },
David Ahern6810fc92013-08-28 22:29:52 -06002731 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002732 struct perf_data_file file = {
2733 .path = input_name,
2734 .mode = PERF_DATA_MODE_READ,
Yunlong Songe366a6d2015-04-02 21:47:18 +08002735 .force = trace->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002736 };
David Ahern6810fc92013-08-28 22:29:52 -06002737 struct perf_session *session;
Namhyung Kim003824e2013-11-12 15:25:00 +09002738 struct perf_evsel *evsel;
David Ahern6810fc92013-08-28 22:29:52 -06002739 int err = -1;
2740
2741 trace->tool.sample = trace__process_sample;
2742 trace->tool.mmap = perf_event__process_mmap;
David Ahern384c6712013-09-22 19:44:58 -06002743 trace->tool.mmap2 = perf_event__process_mmap2;
David Ahern6810fc92013-08-28 22:29:52 -06002744 trace->tool.comm = perf_event__process_comm;
2745 trace->tool.exit = perf_event__process_exit;
2746 trace->tool.fork = perf_event__process_fork;
2747 trace->tool.attr = perf_event__process_attr;
2748 trace->tool.tracing_data = perf_event__process_tracing_data;
2749 trace->tool.build_id = perf_event__process_build_id;
2750
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002751 trace->tool.ordered_events = true;
David Ahern6810fc92013-08-28 22:29:52 -06002752 trace->tool.ordering_requires_timestamps = true;
2753
2754 /* add tid to output */
2755 trace->multiple_threads = true;
2756
Jiri Olsaf5fc1412013-10-15 16:27:32 +02002757 session = perf_session__new(&file, false, &trace->tool);
David Ahern6810fc92013-08-28 22:29:52 -06002758 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002759 return -1;
David Ahern6810fc92013-08-28 22:29:52 -06002760
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002761 if (symbol__init(&session->header.env) < 0)
Namhyung Kimcb2ffae2014-08-12 15:40:44 +09002762 goto out;
2763
David Ahern8fb598e2013-09-28 13:13:00 -06002764 trace->host = &session->machines.host;
2765
David Ahern6810fc92013-08-28 22:29:52 -06002766 err = perf_session__set_tracepoints_handlers(session, handlers);
2767 if (err)
2768 goto out;
2769
Namhyung Kim003824e2013-11-12 15:25:00 +09002770 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2771 "raw_syscalls:sys_enter");
David Ahern9aca7f12013-12-04 19:41:39 -07002772 /* older kernels have syscalls tp versus raw_syscalls */
2773 if (evsel == NULL)
2774 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2775 "syscalls:sys_enter");
David Ahern6810fc92013-08-28 22:29:52 -06002776
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002777 if (evsel &&
2778 (perf_evsel__init_syscall_tp(evsel, trace__sys_enter) < 0 ||
2779 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09002780 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
2781 goto out;
2782 }
2783
2784 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2785 "raw_syscalls:sys_exit");
David Ahern9aca7f12013-12-04 19:41:39 -07002786 if (evsel == NULL)
2787 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2788 "syscalls:sys_exit");
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002789 if (evsel &&
2790 (perf_evsel__init_syscall_tp(evsel, trace__sys_exit) < 0 ||
2791 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09002792 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
David Ahern6810fc92013-08-28 22:29:52 -06002793 goto out;
2794 }
2795
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002796 evlist__for_each(session->evlist, evsel) {
2797 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
2798 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
2799 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
2800 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
2801 evsel->handler = trace__pgfault;
2802 }
2803
David Ahernbdc89662013-08-28 22:29:53 -06002804 err = parse_target_str(trace);
2805 if (err != 0)
2806 goto out;
2807
David Ahern6810fc92013-08-28 22:29:52 -06002808 setup_pager();
2809
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002810 err = perf_session__process_events(session);
David Ahern6810fc92013-08-28 22:29:52 -06002811 if (err)
2812 pr_err("Failed to process events, error %d", err);
2813
David Ahernbf2575c2013-10-08 21:26:53 -06002814 else if (trace->summary)
2815 trace__fprintf_thread_summary(trace, trace->output);
2816
David Ahern6810fc92013-08-28 22:29:52 -06002817out:
2818 perf_session__delete(session);
2819
2820 return err;
2821}
2822
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002823static size_t trace__fprintf_threads_header(FILE *fp)
2824{
2825 size_t printed;
2826
Pekka Enberg99ff7152013-11-12 16:42:14 +02002827 printed = fprintf(fp, "\n Summary of events:\n\n");
David Ahernbf2575c2013-10-08 21:26:53 -06002828
2829 return printed;
2830}
2831
2832static size_t thread__dump_stats(struct thread_trace *ttrace,
2833 struct trace *trace, FILE *fp)
2834{
2835 struct stats *stats;
2836 size_t printed = 0;
2837 struct syscall *sc;
2838 struct int_node *inode = intlist__first(ttrace->syscall_stats);
2839
2840 if (inode == NULL)
2841 return 0;
2842
2843 printed += fprintf(fp, "\n");
2844
Milian Wolff834fd462015-08-06 11:24:29 +02002845 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
2846 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
2847 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
Pekka Enberg99ff7152013-11-12 16:42:14 +02002848
David Ahernbf2575c2013-10-08 21:26:53 -06002849 /* each int_node is a syscall */
2850 while (inode) {
2851 stats = inode->priv;
2852 if (stats) {
2853 double min = (double)(stats->min) / NSEC_PER_MSEC;
2854 double max = (double)(stats->max) / NSEC_PER_MSEC;
2855 double avg = avg_stats(stats);
2856 double pct;
2857 u64 n = (u64) stats->n;
2858
2859 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
2860 avg /= NSEC_PER_MSEC;
2861
2862 sc = &trace->syscalls.table[inode->i];
Pekka Enberg99ff7152013-11-12 16:42:14 +02002863 printed += fprintf(fp, " %-15s", sc->name);
Milian Wolff834fd462015-08-06 11:24:29 +02002864 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
2865 n, avg * n, min, avg);
Pekka Enberg27a778b2013-11-13 14:21:48 +02002866 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
David Ahernbf2575c2013-10-08 21:26:53 -06002867 }
2868
2869 inode = intlist__next(inode);
2870 }
2871
2872 printed += fprintf(fp, "\n\n");
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002873
2874 return printed;
2875}
2876
David Ahern896cbb52013-09-28 13:12:59 -06002877/* struct used to pass data to per-thread function */
2878struct summary_data {
2879 FILE *fp;
2880 struct trace *trace;
2881 size_t printed;
2882};
2883
2884static int trace__fprintf_one_thread(struct thread *thread, void *priv)
2885{
2886 struct summary_data *data = priv;
2887 FILE *fp = data->fp;
2888 size_t printed = data->printed;
2889 struct trace *trace = data->trace;
Namhyung Kim89dceb22014-10-06 09:46:03 +09002890 struct thread_trace *ttrace = thread__priv(thread);
David Ahern896cbb52013-09-28 13:12:59 -06002891 double ratio;
2892
2893 if (ttrace == NULL)
2894 return 0;
2895
2896 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
2897
Pekka Enberg15e65c62013-11-14 18:43:30 +02002898 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
Pekka Enberg99ff7152013-11-12 16:42:14 +02002899 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
Pekka Enberg15e65c62013-11-14 18:43:30 +02002900 printed += fprintf(fp, "%.1f%%", ratio);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002901 if (ttrace->pfmaj)
2902 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
2903 if (ttrace->pfmin)
2904 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
Pekka Enberg99ff7152013-11-12 16:42:14 +02002905 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
David Ahernbf2575c2013-10-08 21:26:53 -06002906 printed += thread__dump_stats(ttrace, trace, fp);
David Ahern896cbb52013-09-28 13:12:59 -06002907
2908 data->printed += printed;
2909
2910 return 0;
2911}
2912
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002913static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
2914{
David Ahern896cbb52013-09-28 13:12:59 -06002915 struct summary_data data = {
2916 .fp = fp,
2917 .trace = trace
2918 };
2919 data.printed = trace__fprintf_threads_header(fp);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002920
David Ahern896cbb52013-09-28 13:12:59 -06002921 machine__for_each_thread(trace->host, trace__fprintf_one_thread, &data);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002922
David Ahern896cbb52013-09-28 13:12:59 -06002923 return data.printed;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002924}
2925
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002926static int trace__set_duration(const struct option *opt, const char *str,
2927 int unset __maybe_unused)
2928{
2929 struct trace *trace = opt->value;
2930
2931 trace->duration_filter = atof(str);
2932 return 0;
2933}
2934
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08002935static int trace__set_filter_pids(const struct option *opt, const char *str,
2936 int unset __maybe_unused)
2937{
2938 int ret = -1;
2939 size_t i;
2940 struct trace *trace = opt->value;
2941 /*
2942 * FIXME: introduce a intarray class, plain parse csv and create a
2943 * { int nr, int entries[] } struct...
2944 */
2945 struct intlist *list = intlist__new(str);
2946
2947 if (list == NULL)
2948 return -1;
2949
2950 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
2951 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
2952
2953 if (trace->filter_pids.entries == NULL)
2954 goto out;
2955
2956 trace->filter_pids.entries[0] = getpid();
2957
2958 for (i = 1; i < trace->filter_pids.nr; ++i)
2959 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
2960
2961 intlist__delete(list);
2962 ret = 0;
2963out:
2964 return ret;
2965}
2966
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002967static int trace__open_output(struct trace *trace, const char *filename)
2968{
2969 struct stat st;
2970
2971 if (!stat(filename, &st) && st.st_size) {
2972 char oldname[PATH_MAX];
2973
2974 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
2975 unlink(oldname);
2976 rename(filename, oldname);
2977 }
2978
2979 trace->output = fopen(filename, "w");
2980
2981 return trace->output == NULL ? -errno : 0;
2982}
2983
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002984static int parse_pagefaults(const struct option *opt, const char *str,
2985 int unset __maybe_unused)
2986{
2987 int *trace_pgfaults = opt->value;
2988
2989 if (strcmp(str, "all") == 0)
2990 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
2991 else if (strcmp(str, "maj") == 0)
2992 *trace_pgfaults |= TRACE_PFMAJ;
2993 else if (strcmp(str, "min") == 0)
2994 *trace_pgfaults |= TRACE_PFMIN;
2995 else
2996 return -1;
2997
2998 return 0;
2999}
3000
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003001static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3002{
3003 struct perf_evsel *evsel;
3004
3005 evlist__for_each(evlist, evsel)
3006 evsel->handler = handler;
3007}
3008
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003009int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
3010{
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003011 const char *trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +09003012 "perf trace [<options>] [<command>]",
3013 "perf trace [<options>] -- <command> [<options>]",
David Ahern5e2485b2013-09-28 13:13:01 -06003014 "perf trace record [<options>] [<command>]",
3015 "perf trace record [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003016 NULL
3017 };
3018 struct trace trace = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003019 .audit = {
3020 .machine = audit_detect_machine(),
3021 .open_id = audit_name_to_syscall("open", trace.audit.machine),
3022 },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003023 .syscalls = {
3024 . max = -1,
3025 },
3026 .opts = {
3027 .target = {
3028 .uid = UINT_MAX,
3029 .uses_mmap = true,
3030 },
3031 .user_freq = UINT_MAX,
3032 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03003033 .no_buffering = true,
Arnaldo Carvalho de Melo38d54472014-12-12 17:28:32 -03003034 .mmap_pages = UINT_MAX,
Kan Liang9d9cad72015-06-17 09:51:11 -04003035 .proc_map_timeout = 500,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003036 },
Milian Wolff007d66a2015-08-05 16:52:23 -03003037 .output = stderr,
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003038 .show_comm = true,
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003039 .trace_syscalls = true,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003040 };
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003041 const char *output_name = NULL;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03003042 const char *ev_qualifier_str = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003043 const struct option trace_options[] = {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003044 OPT_CALLBACK(0, "event", &trace.evlist, "event",
3045 "event selector. use 'perf list' to list available events",
3046 parse_events_option),
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003047 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3048 "show the thread COMM next to its id"),
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003049 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
Arnaldo Carvalho de Melod303e852015-04-23 12:02:07 -03003050 OPT_STRING('e', "expr", &ev_qualifier_str, "expr", "list of syscalls to trace"),
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003051 OPT_STRING('o', "output", &output_name, "file", "output file name"),
David Ahern6810fc92013-08-28 22:29:52 -06003052 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003053 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3054 "trace events on existing process id"),
David Ahernac9be8e2013-08-20 11:15:45 -06003055 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003056 "trace events on existing thread id"),
Arnaldo Carvalho de Melofa0e4ff2015-04-23 11:59:20 -03003057 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
3058 "pids to filter (by the kernel)", trace__set_filter_pids),
David Ahernac9be8e2013-08-20 11:15:45 -06003059 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003060 "system-wide collection from all CPUs"),
David Ahernac9be8e2013-08-20 11:15:45 -06003061 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003062 "list of cpus to monitor"),
David Ahern6810fc92013-08-28 22:29:52 -06003063 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003064 "child tasks do not inherit counters"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02003065 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3066 "number of mmap data pages",
3067 perf_evlist__parse_mmap_pages),
David Ahernac9be8e2013-08-20 11:15:45 -06003068 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003069 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003070 OPT_CALLBACK(0, "duration", &trace, "float",
3071 "show only events with duration > N.M ms",
3072 trace__set_duration),
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003073 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03003074 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
David Ahern4bb09192013-09-04 12:37:43 -06003075 OPT_BOOLEAN('T', "time", &trace.full_time,
3076 "Show full timestamp, not time relative to first start"),
David Ahernfd2eaba2013-11-12 09:31:15 -07003077 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3078 "Show only syscall summary with statistics"),
3079 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3080 "Show all syscalls and summary with statistics"),
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003081 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3082 "Trace pagefaults", parse_pagefaults, "maj"),
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003083 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
Yunlong Songe366a6d2015-04-02 21:47:18 +08003084 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
Kan Liang9d9cad72015-06-17 09:51:11 -04003085 OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
3086 "per thread proc mmap processing timeout in ms"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003087 OPT_END()
3088 };
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003089 const char * const trace_subcommands[] = { "record", NULL };
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003090 int err;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003091 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003092
Arnaldo Carvalho de Melo4d08cb82015-02-24 15:35:55 -03003093 signal(SIGSEGV, sighandler_dump_stack);
3094 signal(SIGFPE, sighandler_dump_stack);
3095
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003096 trace.evlist = perf_evlist__new();
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003097
3098 if (trace.evlist == NULL) {
3099 pr_err("Not enough memory to run!\n");
He Kuangff8f6952015-05-11 12:28:36 +00003100 err = -ENOMEM;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003101 goto out;
3102 }
3103
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003104 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3105 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
David Ahernfd2eaba2013-11-12 09:31:15 -07003106
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003107 if (trace.trace_pgfaults) {
3108 trace.opts.sample_address = true;
3109 trace.opts.sample_time = true;
3110 }
3111
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003112 if (trace.evlist->nr_entries > 0)
3113 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
3114
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003115 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3116 return trace__record(&trace, argc-1, &argv[1]);
3117
3118 /* summary_only implies summary option, but don't overwrite summary if set */
3119 if (trace.summary_only)
3120 trace.summary = trace.summary_only;
3121
Arnaldo Carvalho de Melo726f3232015-02-06 10:16:45 +01003122 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3123 trace.evlist->nr_entries == 0 /* Was --events used? */) {
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003124 pr_err("Please specify something to trace.\n");
3125 return -1;
3126 }
3127
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003128 if (output_name != NULL) {
3129 err = trace__open_output(&trace, output_name);
3130 if (err < 0) {
3131 perror("failed to create output file");
3132 goto out;
3133 }
3134 }
3135
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03003136 if (ev_qualifier_str != NULL) {
Arnaldo Carvalho de Melob059efd2013-08-21 12:56:21 -03003137 const char *s = ev_qualifier_str;
Arnaldo Carvalho de Melo005438a82015-07-20 12:02:09 -03003138 struct strlist_config slist_config = {
3139 .dirname = system_path(STRACE_GROUPS_DIR),
3140 };
Arnaldo Carvalho de Melob059efd2013-08-21 12:56:21 -03003141
3142 trace.not_ev_qualifier = *s == '!';
3143 if (trace.not_ev_qualifier)
3144 ++s;
Arnaldo Carvalho de Melo005438a82015-07-20 12:02:09 -03003145 trace.ev_qualifier = strlist__new(s, &slist_config);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03003146 if (trace.ev_qualifier == NULL) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003147 fputs("Not enough memory to parse event qualifier",
3148 trace.output);
3149 err = -ENOMEM;
3150 goto out_close;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03003151 }
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03003152
3153 err = trace__validate_ev_qualifier(&trace);
3154 if (err)
3155 goto out_close;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03003156 }
3157
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003158 err = target__validate(&trace.opts.target);
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003159 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003160 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003161 fprintf(trace.output, "%s", bf);
3162 goto out_close;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003163 }
3164
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003165 err = target__parse_uid(&trace.opts.target);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003166 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003167 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003168 fprintf(trace.output, "%s", bf);
3169 goto out_close;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003170 }
3171
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003172 if (!argc && target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +09003173 trace.opts.target.system_wide = true;
3174
David Ahern6810fc92013-08-28 22:29:52 -06003175 if (input_name)
3176 err = trace__replay(&trace);
3177 else
3178 err = trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003179
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003180out_close:
3181 if (output_name != NULL)
3182 fclose(trace.output);
3183out:
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003184 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003185}