]> git.ipfire.org Git - people/arne_f/kernel.git/blame - tools/perf/builtin-trace.c
perf trace: Allow configuring if the syscall start timestamp should be printed
[people/arne_f/kernel.git] / tools / perf / builtin-trace.c
CommitLineData
a598bb5e
ACM
1/*
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
4e319027 19#include <traceevent/event-parse.h>
988bdb31 20#include <api/fs/tracing_path.h>
744fafc7 21#include <bpf/bpf.h>
514f1c67 22#include "builtin.h"
9ea42ba4 23#include "util/cgroup.h"
752fde44 24#include "util/color.h"
ac96287c 25#include "util/config.h"
7c304ee0 26#include "util/debug.h"
092bd3cd 27#include "util/env.h"
5ab8c689 28#include "util/event.h"
514f1c67 29#include "util/evlist.h"
4b6ab94e 30#include <subcmd/exec-cmd.h>
752fde44 31#include "util/machine.h"
9a3993d4 32#include "util/path.h"
6810fc91 33#include "util/session.h"
752fde44 34#include "util/thread.h"
4b6ab94e 35#include <subcmd/parse-options.h>
2ae3a312 36#include "util/strlist.h"
bdc89661 37#include "util/intlist.h"
514f1c67 38#include "util/thread_map.h"
bf2575c1 39#include "util/stat.h"
fd5cead2 40#include "trace/beauty/beauty.h"
97978b3e 41#include "trace-event.h"
9aca7f17 42#include "util/parse-events.h"
ba504235 43#include "util/bpf-loader.h"
566a0885 44#include "callchain.h"
fea01392 45#include "print_binary.h"
a067558e 46#include "string2.h"
fd0db102 47#include "syscalltbl.h"
96c14451 48#include "rb_resort.h"
514f1c67 49
a43783ae 50#include <errno.h>
fd20e811 51#include <inttypes.h>
4208735d 52#include <poll.h>
9607ad3a 53#include <signal.h>
514f1c67 54#include <stdlib.h>
017037ff 55#include <string.h>
8dd2a131 56#include <linux/err.h>
997bba8c 57#include <linux/filter.h>
877a7a11 58#include <linux/kernel.h>
39878d49 59#include <linux/random.h>
c6d4a494 60#include <linux/stringify.h>
bd48c63e 61#include <linux/time64.h>
bafae98e 62#include <fcntl.h>
514f1c67 63
3d689ed6
ACM
64#include "sane_ctype.h"
65
c188e7ac
ACM
66#ifndef O_CLOEXEC
67# define O_CLOEXEC 02000000
68#endif
69
83a51694
ACM
70#ifndef F_LINUX_SPECIFIC_BASE
71# define F_LINUX_SPECIFIC_BASE 1024
72#endif
73
d1d438a3
ACM
74struct trace {
75 struct perf_tool tool;
fd0db102 76 struct syscalltbl *sctbl;
d1d438a3
ACM
77 struct {
78 int max;
79 struct syscall *table;
b27b38ed 80 struct bpf_map *map;
d1d438a3
ACM
81 struct {
82 struct perf_evsel *sys_enter,
d3d1c4bd
ACM
83 *sys_exit,
84 *augmented;
d1d438a3
ACM
85 } events;
86 } syscalls;
87 struct record_opts opts;
88 struct perf_evlist *evlist;
89 struct machine *host;
90 struct thread *current;
9ea42ba4 91 struct cgroup *cgroup;
d1d438a3
ACM
92 u64 base_time;
93 FILE *output;
94 unsigned long nr_events;
5067a8cd
ACM
95 unsigned long nr_events_printed;
96 unsigned long max_events;
d1d438a3
ACM
97 struct strlist *ev_qualifier;
98 struct {
99 size_t nr;
100 int *entries;
101 } ev_qualifier_ids;
d1d438a3
ACM
102 struct {
103 size_t nr;
104 pid_t *entries;
744fafc7 105 struct bpf_map *map;
d1d438a3
ACM
106 } filter_pids;
107 double duration_filter;
108 double runtime_ms;
109 struct {
110 u64 vfs_getname,
111 proc_getname;
112 } stats;
c6d4a494 113 unsigned int max_stack;
5cf9c84e 114 unsigned int min_stack;
a761a8d1 115 bool sort_events;
cd26ea6d 116 bool raw_augmented_syscalls;
d1d438a3
ACM
117 bool not_ev_qualifier;
118 bool live;
119 bool full_time;
120 bool sched;
121 bool multiple_threads;
122 bool summary;
123 bool summary_only;
0a6545bd 124 bool failure_only;
d1d438a3 125 bool show_comm;
591421e1 126 bool print_sample;
d1d438a3
ACM
127 bool show_tool_stats;
128 bool trace_syscalls;
44621819 129 bool kernel_syscallchains;
b036146f 130 bool show_tstamp;
42e4a52d 131 bool show_duration;
e7c634fc 132 bool show_zeros;
d1d438a3
ACM
133 bool force;
134 bool vfs_getname;
135 int trace_pgfaults;
028713aa
JO
136 struct {
137 struct ordered_events data;
138 u64 last;
139 } oe;
d1d438a3 140};
a1c2552d 141
77170988
ACM
142struct tp_field {
143 int offset;
144 union {
145 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
146 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
147 };
148};
149
150#define TP_UINT_FIELD(bits) \
151static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
152{ \
55d43bca
DA
153 u##bits value; \
154 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
155 return value; \
77170988
ACM
156}
157
158TP_UINT_FIELD(8);
159TP_UINT_FIELD(16);
160TP_UINT_FIELD(32);
161TP_UINT_FIELD(64);
162
163#define TP_UINT_FIELD__SWAPPED(bits) \
164static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
165{ \
55d43bca
DA
166 u##bits value; \
167 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
77170988
ACM
168 return bswap_##bits(value);\
169}
170
171TP_UINT_FIELD__SWAPPED(16);
172TP_UINT_FIELD__SWAPPED(32);
173TP_UINT_FIELD__SWAPPED(64);
174
aa823f58 175static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
77170988 176{
aa823f58 177 field->offset = offset;
77170988 178
aa823f58 179 switch (size) {
77170988
ACM
180 case 1:
181 field->integer = tp_field__u8;
182 break;
183 case 2:
184 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
185 break;
186 case 4:
187 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
188 break;
189 case 8:
190 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
191 break;
192 default:
193 return -1;
194 }
195
196 return 0;
197}
198
2c92f982 199static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap)
aa823f58
ACM
200{
201 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
202}
203
77170988
ACM
204static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
205{
206 return sample->raw_data + field->offset;
207}
208
aa823f58 209static int __tp_field__init_ptr(struct tp_field *field, int offset)
77170988 210{
aa823f58 211 field->offset = offset;
77170988
ACM
212 field->pointer = tp_field__ptr;
213 return 0;
214}
215
2c92f982 216static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
aa823f58
ACM
217{
218 return __tp_field__init_ptr(field, format_field->offset);
219}
220
77170988
ACM
221struct syscall_tp {
222 struct tp_field id;
223 union {
224 struct tp_field args, ret;
225 };
226};
227
228static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
229 struct tp_field *field,
230 const char *name)
231{
2c92f982 232 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
77170988
ACM
233
234 if (format_field == NULL)
235 return -1;
236
237 return tp_field__init_uint(field, format_field, evsel->needs_swap);
238}
239
240#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
241 ({ struct syscall_tp *sc = evsel->priv;\
242 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
243
244static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
245 struct tp_field *field,
246 const char *name)
247{
2c92f982 248 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
77170988
ACM
249
250 if (format_field == NULL)
251 return -1;
252
253 return tp_field__init_ptr(field, format_field);
254}
255
256#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
257 ({ struct syscall_tp *sc = evsel->priv;\
258 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
259
260static void perf_evsel__delete_priv(struct perf_evsel *evsel)
261{
04662523 262 zfree(&evsel->priv);
77170988
ACM
263 perf_evsel__delete(evsel);
264}
265
d32855fa
ACM
266static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
267{
268 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
269
270 if (evsel->priv != NULL) {
42da438c
ACM
271 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
272 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
d32855fa
ACM
273 goto out_delete;
274 return 0;
275 }
276
277 return -ENOMEM;
278out_delete:
279 zfree(&evsel->priv);
280 return -ENOENT;
281}
282
d3d1c4bd
ACM
283static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel)
284{
285 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
286
287 if (evsel->priv != NULL) { /* field, sizeof_field, offsetof_field */
288 if (__tp_field__init_uint(&sc->id, sizeof(long), sizeof(long long), evsel->needs_swap))
289 goto out_delete;
290
291 return 0;
292 }
293
294 return -ENOMEM;
295out_delete:
296 zfree(&evsel->priv);
297 return -EINVAL;
298}
299
300static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
301{
302 struct syscall_tp *sc = evsel->priv;
303
304 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
305}
306
b1a9e253
ACM
307static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
308{
309 struct syscall_tp *sc = evsel->priv;
310
311 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
312}
313
63f11c80 314static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
96695d44
NK
315{
316 evsel->priv = malloc(sizeof(struct syscall_tp));
317 if (evsel->priv != NULL) {
318 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
319 goto out_delete;
320
321 evsel->handler = handler;
322 return 0;
323 }
324
325 return -ENOMEM;
326
327out_delete:
04662523 328 zfree(&evsel->priv);
96695d44
NK
329 return -ENOENT;
330}
331
63f11c80 332static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
77170988 333{
ef503831 334 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
77170988 335
9aca7f17 336 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
8dd2a131 337 if (IS_ERR(evsel))
9aca7f17
DA
338 evsel = perf_evsel__newtp("syscalls", direction);
339
8dd2a131
JO
340 if (IS_ERR(evsel))
341 return NULL;
342
63f11c80 343 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
8dd2a131 344 goto out_delete;
77170988
ACM
345
346 return evsel;
347
348out_delete:
349 perf_evsel__delete_priv(evsel);
350 return NULL;
351}
352
353#define perf_evsel__sc_tp_uint(evsel, name, sample) \
354 ({ struct syscall_tp *fields = evsel->priv; \
355 fields->name.integer(&fields->name, sample); })
356
357#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
358 ({ struct syscall_tp *fields = evsel->priv; \
359 fields->name.pointer(&fields->name, sample); })
360
0ae79636
ACM
361size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, int val)
362{
363 int idx = val - sa->offset;
1f115cb7 364
bc972ada 365 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL)
0ae79636 366 return scnprintf(bf, size, intfmt, val);
1f115cb7 367
0ae79636 368 return scnprintf(bf, size, "%s", sa->entries[idx]);
03e3adc9
ACM
369}
370
975b7c2f
ACM
371static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
372 const char *intfmt,
373 struct syscall_arg *arg)
1f115cb7 374{
0ae79636 375 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->val);
1f115cb7
ACM
376}
377
975b7c2f
ACM
378static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
379 struct syscall_arg *arg)
380{
381 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
382}
383
1f115cb7
ACM
384#define SCA_STRARRAY syscall_arg__scnprintf_strarray
385
83a51694
ACM
386struct strarrays {
387 int nr_entries;
388 struct strarray **entries;
389};
390
391#define DEFINE_STRARRAYS(array) struct strarrays strarrays__##array = { \
392 .nr_entries = ARRAY_SIZE(array), \
393 .entries = array, \
394}
395
274e86fd
ACM
396size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
397 struct syscall_arg *arg)
83a51694
ACM
398{
399 struct strarrays *sas = arg->parm;
400 int i;
401
402 for (i = 0; i < sas->nr_entries; ++i) {
403 struct strarray *sa = sas->entries[i];
404 int idx = arg->val - sa->offset;
405
406 if (idx >= 0 && idx < sa->nr_entries) {
407 if (sa->entries[idx] == NULL)
408 break;
409 return scnprintf(bf, size, "%s", sa->entries[idx]);
410 }
411 }
412
413 return scnprintf(bf, size, "%d", arg->val);
414}
415
48e1f91a
ACM
416#ifndef AT_FDCWD
417#define AT_FDCWD -100
418#endif
419
75b757ca
ACM
420static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
421 struct syscall_arg *arg)
422{
423 int fd = arg->val;
424
425 if (fd == AT_FDCWD)
426 return scnprintf(bf, size, "CWD");
427
428 return syscall_arg__scnprintf_fd(bf, size, arg);
429}
430
431#define SCA_FDAT syscall_arg__scnprintf_fd_at
432
433static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
434 struct syscall_arg *arg);
435
436#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
437
2c2b1623 438size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
13d4ff3e 439{
01533e97 440 return scnprintf(bf, size, "%#lx", arg->val);
13d4ff3e
ACM
441}
442
2c2b1623 443size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
a1c2552d
ACM
444{
445 return scnprintf(bf, size, "%d", arg->val);
446}
447
5dde91ed
ACM
448size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
449{
450 return scnprintf(bf, size, "%ld", arg->val);
451}
452
729a7841
ACM
453static const char *bpf_cmd[] = {
454 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
455 "MAP_GET_NEXT_KEY", "PROG_LOAD",
456};
457static DEFINE_STRARRAY(bpf_cmd);
458
03e3adc9
ACM
459static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
460static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1);
eac032c5 461
1f115cb7
ACM
462static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
463static DEFINE_STRARRAY(itimers);
464
b62bee1b
ACM
465static const char *keyctl_options[] = {
466 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
467 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
468 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
469 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
470 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
471};
472static DEFINE_STRARRAY(keyctl_options);
473
efe6b882
ACM
474static const char *whences[] = { "SET", "CUR", "END",
475#ifdef SEEK_DATA
476"DATA",
477#endif
478#ifdef SEEK_HOLE
479"HOLE",
480#endif
481};
482static DEFINE_STRARRAY(whences);
f9da0b0c 483
80f587d5
ACM
484static const char *fcntl_cmds[] = {
485 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
e000e5e3
ACM
486 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
487 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
488 "GETOWNER_UIDS",
80f587d5
ACM
489};
490static DEFINE_STRARRAY(fcntl_cmds);
491
83a51694
ACM
492static const char *fcntl_linux_specific_cmds[] = {
493 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
494 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
64e4561d 495 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
83a51694
ACM
496};
497
498static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, F_LINUX_SPECIFIC_BASE);
499
500static struct strarray *fcntl_cmds_arrays[] = {
501 &strarray__fcntl_cmds,
502 &strarray__fcntl_linux_specific_cmds,
503};
504
505static DEFINE_STRARRAYS(fcntl_cmds_arrays);
506
c045bf02
ACM
507static const char *rlimit_resources[] = {
508 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
509 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
510 "RTTIME",
511};
512static DEFINE_STRARRAY(rlimit_resources);
513
eb5b1b14
ACM
514static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
515static DEFINE_STRARRAY(sighow);
516
4f8c1b74
DA
517static const char *clockid[] = {
518 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
28ebb87c
ACM
519 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
520 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
4f8c1b74
DA
521};
522static DEFINE_STRARRAY(clockid);
523
51108999
ACM
524static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
525 struct syscall_arg *arg)
526{
527 size_t printed = 0;
528 int mode = arg->val;
529
530 if (mode == F_OK) /* 0 */
531 return scnprintf(bf, size, "F");
532#define P_MODE(n) \
533 if (mode & n##_OK) { \
534 printed += scnprintf(bf + printed, size - printed, "%s", #n); \
535 mode &= ~n##_OK; \
536 }
537
538 P_MODE(R);
539 P_MODE(W);
540 P_MODE(X);
541#undef P_MODE
542
543 if (mode)
544 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
545
546 return printed;
547}
548
549#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
550
f994592d
ACM
551static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
552 struct syscall_arg *arg);
553
554#define SCA_FILENAME syscall_arg__scnprintf_filename
555
46cce19b
ACM
556static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
557 struct syscall_arg *arg)
558{
559 int printed = 0, flags = arg->val;
560
561#define P_FLAG(n) \
562 if (flags & O_##n) { \
563 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
564 flags &= ~O_##n; \
565 }
566
567 P_FLAG(CLOEXEC);
568 P_FLAG(NONBLOCK);
569#undef P_FLAG
570
571 if (flags)
572 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
573
574 return printed;
575}
576
577#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
578
a355a61e
ACM
579#ifndef GRND_NONBLOCK
580#define GRND_NONBLOCK 0x0001
581#endif
582#ifndef GRND_RANDOM
583#define GRND_RANDOM 0x0002
584#endif
585
39878d49
ACM
586static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
587 struct syscall_arg *arg)
588{
589 int printed = 0, flags = arg->val;
590
591#define P_FLAG(n) \
592 if (flags & GRND_##n) { \
593 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
594 flags &= ~GRND_##n; \
595 }
596
597 P_FLAG(RANDOM);
598 P_FLAG(NONBLOCK);
599#undef P_FLAG
600
601 if (flags)
602 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
603
604 return printed;
605}
606
607#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
608
82d4a110
ACM
609#define STRARRAY(name, array) \
610 { .scnprintf = SCA_STRARRAY, \
611 .parm = &strarray__##array, }
453350dd 612
092bd3cd 613#include "trace/beauty/arch_errno_names.c"
ea8dc3ce 614#include "trace/beauty/eventfd.c"
d5d71e86 615#include "trace/beauty/futex_op.c"
3258abe0 616#include "trace/beauty/futex_val3.c"
df4cb167 617#include "trace/beauty/mmap.c"
ba2f22cf 618#include "trace/beauty/mode_t.c"
a30e6259 619#include "trace/beauty/msg_flags.c"
8f48df69 620#include "trace/beauty/open_flags.c"
62de344e 621#include "trace/beauty/perf_event_open.c"
d5d71e86 622#include "trace/beauty/pid.c"
a3bca91f 623#include "trace/beauty/sched_policy.c"
f5cd95ea 624#include "trace/beauty/seccomp.c"
12199d8e 625#include "trace/beauty/signum.c"
bbf86c43 626#include "trace/beauty/socket_type.c"
7206b900 627#include "trace/beauty/waitid_options.c"
a3bca91f 628
82d4a110
ACM
629struct syscall_arg_fmt {
630 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
496fd346 631 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
82d4a110 632 void *parm;
c51bdfec 633 const char *name;
d47737d5 634 bool show_zero;
82d4a110
ACM
635};
636
514f1c67
ACM
637static struct syscall_fmt {
638 const char *name;
aec1930b 639 const char *alias;
82d4a110 640 struct syscall_arg_fmt arg[6];
332337da 641 u8 nr_args;
11c8e39f 642 bool errpid;
514f1c67 643 bool timeout;
04b34729 644 bool hexret;
514f1c67 645} syscall_fmts[] = {
1f63139c 646 { .name = "access",
82d4a110 647 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
02ef2884
ACM
648 { .name = "bind",
649 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
1f63139c 650 { .name = "bpf",
82d4a110 651 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
beccb2b5 652 { .name = "brk", .hexret = true,
82d4a110 653 .arg = { [0] = { .scnprintf = SCA_HEX, /* brk */ }, }, },
1f63139c 654 { .name = "clock_gettime",
82d4a110 655 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
33396a3a
ACM
656 { .name = "clone", .errpid = true, .nr_args = 5,
657 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
658 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
659 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
660 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
661 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
1f63139c 662 { .name = "close",
82d4a110 663 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
d5a7e661
ACM
664 { .name = "connect",
665 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
1f63139c 666 { .name = "epoll_ctl",
82d4a110 667 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
1f63139c 668 { .name = "eventfd2",
82d4a110 669 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
1f63139c 670 { .name = "fchmodat",
82d4a110 671 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
1f63139c 672 { .name = "fchownat",
82d4a110 673 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
1f63139c 674 { .name = "fcntl",
82d4a110 675 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
39cc355b
ACM
676 .parm = &strarrays__fcntl_cmds_arrays,
677 .show_zero = true, },
82d4a110 678 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
1f63139c 679 { .name = "flock",
82d4a110 680 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
1f63139c
ACM
681 { .name = "fstat", .alias = "newfstat", },
682 { .name = "fstatat", .alias = "newfstatat", },
683 { .name = "futex",
3258abe0
ACM
684 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
685 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
1f63139c 686 { .name = "futimesat",
82d4a110 687 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
1f63139c 688 { .name = "getitimer",
82d4a110 689 .arg = { [0] = STRARRAY(which, itimers), }, },
c65f1070 690 { .name = "getpid", .errpid = true, },
d1d438a3 691 { .name = "getpgid", .errpid = true, },
c65f1070 692 { .name = "getppid", .errpid = true, },
1f63139c 693 { .name = "getrandom",
82d4a110 694 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
1f63139c 695 { .name = "getrlimit",
82d4a110 696 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
2d1073de 697 { .name = "gettid", .errpid = true, },
1f63139c 698 { .name = "ioctl",
82d4a110 699 .arg = {
844ae5b4
ACM
700#if defined(__i386__) || defined(__x86_64__)
701/*
702 * FIXME: Make this available to all arches.
703 */
1cc47f2d 704 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
82d4a110 705 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
844ae5b4 706#else
82d4a110 707 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
844ae5b4 708#endif
1de3038d
ACM
709 { .name = "kcmp", .nr_args = 5,
710 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
711 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
712 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
713 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
714 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
1f63139c 715 { .name = "keyctl",
82d4a110 716 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
1f63139c 717 { .name = "kill",
82d4a110 718 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
1f63139c 719 { .name = "linkat",
82d4a110 720 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
1f63139c 721 { .name = "lseek",
82d4a110 722 .arg = { [2] = STRARRAY(whence, whences), }, },
1f63139c
ACM
723 { .name = "lstat", .alias = "newlstat", },
724 { .name = "madvise",
82d4a110
ACM
725 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
726 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
1f63139c 727 { .name = "mkdirat",
82d4a110 728 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
1f63139c 729 { .name = "mknodat",
82d4a110 730 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
1f63139c 731 { .name = "mlock",
82d4a110 732 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
1f63139c 733 { .name = "mlockall",
82d4a110 734 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
beccb2b5 735 { .name = "mmap", .hexret = true,
54265664
JO
736/* The standard mmap maps to old_mmap on s390x */
737#if defined(__s390x__)
738 .alias = "old_mmap",
739#endif
82d4a110
ACM
740 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
741 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
742 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ }, }, },
73d141ad 743 { .name = "mount",
23c07a23
ACM
744 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
745 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
73d141ad 746 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
1f63139c 747 { .name = "mprotect",
82d4a110
ACM
748 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
749 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
1f63139c 750 { .name = "mq_unlink",
82d4a110 751 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
ae685380 752 { .name = "mremap", .hexret = true,
82d4a110
ACM
753 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
754 [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ },
755 [4] = { .scnprintf = SCA_HEX, /* new_addr */ }, }, },
1f63139c 756 { .name = "munlock",
82d4a110 757 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
1f63139c 758 { .name = "munmap",
82d4a110 759 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
1f63139c 760 { .name = "name_to_handle_at",
82d4a110 761 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
1f63139c 762 { .name = "newfstatat",
82d4a110 763 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
1f63139c 764 { .name = "open",
82d4a110 765 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
1f63139c 766 { .name = "open_by_handle_at",
82d4a110
ACM
767 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
768 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
1f63139c 769 { .name = "openat",
82d4a110
ACM
770 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
771 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
1f63139c 772 { .name = "perf_event_open",
82d4a110
ACM
773 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
774 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
775 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
1f63139c 776 { .name = "pipe2",
82d4a110 777 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
83bc9c37
ACM
778 { .name = "pkey_alloc",
779 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
780 { .name = "pkey_free",
781 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
782 { .name = "pkey_mprotect",
783 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
784 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
785 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
1f63139c
ACM
786 { .name = "poll", .timeout = true, },
787 { .name = "ppoll", .timeout = true, },
d688d037
ACM
788 { .name = "prctl", .alias = "arch_prctl",
789 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
790 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
791 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
1f63139c
ACM
792 { .name = "pread", .alias = "pread64", },
793 { .name = "preadv", .alias = "pread", },
794 { .name = "prlimit64",
82d4a110 795 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
1f63139c
ACM
796 { .name = "pwrite", .alias = "pwrite64", },
797 { .name = "readlinkat",
82d4a110 798 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
1f63139c 799 { .name = "recvfrom",
82d4a110 800 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
1f63139c 801 { .name = "recvmmsg",
82d4a110 802 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
1f63139c 803 { .name = "recvmsg",
82d4a110 804 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
1f63139c 805 { .name = "renameat",
436651ca
ACM
806 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
807 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
84a83541
ACM
808 { .name = "renameat2",
809 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
ca7ff2c8
ACM
810 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
811 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
1f63139c 812 { .name = "rt_sigaction",
82d4a110 813 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
1f63139c 814 { .name = "rt_sigprocmask",
82d4a110 815 .arg = { [0] = STRARRAY(how, sighow), }, },
1f63139c 816 { .name = "rt_sigqueueinfo",
82d4a110 817 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
1f63139c 818 { .name = "rt_tgsigqueueinfo",
82d4a110 819 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
1f63139c 820 { .name = "sched_setscheduler",
82d4a110 821 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
1f63139c 822 { .name = "seccomp",
82d4a110
ACM
823 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
824 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
1f63139c
ACM
825 { .name = "select", .timeout = true, },
826 { .name = "sendmmsg",
82d4a110 827 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
1f63139c 828 { .name = "sendmsg",
82d4a110 829 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
1f63139c 830 { .name = "sendto",
6ebb6862
ACM
831 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
832 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
c65f1070 833 { .name = "set_tid_address", .errpid = true, },
1f63139c 834 { .name = "setitimer",
82d4a110 835 .arg = { [0] = STRARRAY(which, itimers), }, },
1f63139c 836 { .name = "setrlimit",
82d4a110 837 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
1f63139c 838 { .name = "socket",
82d4a110 839 .arg = { [0] = STRARRAY(family, socket_families),
162d3edb
ACM
840 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
841 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
1f63139c 842 { .name = "socketpair",
82d4a110 843 .arg = { [0] = STRARRAY(family, socket_families),
162d3edb
ACM
844 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
845 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
1f63139c
ACM
846 { .name = "stat", .alias = "newstat", },
847 { .name = "statx",
82d4a110
ACM
848 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
849 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
850 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
1f63139c 851 { .name = "swapoff",
82d4a110 852 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
1f63139c 853 { .name = "swapon",
82d4a110 854 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
1f63139c 855 { .name = "symlinkat",
82d4a110 856 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
1f63139c 857 { .name = "tgkill",
82d4a110 858 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
1f63139c 859 { .name = "tkill",
82d4a110 860 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
476c92ca
ACM
861 { .name = "umount2", .alias = "umount",
862 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
1f63139c
ACM
863 { .name = "uname", .alias = "newuname", },
864 { .name = "unlinkat",
82d4a110 865 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
1f63139c 866 { .name = "utimensat",
82d4a110 867 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
11c8e39f 868 { .name = "wait4", .errpid = true,
82d4a110 869 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
11c8e39f 870 { .name = "waitid", .errpid = true,
82d4a110 871 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
514f1c67
ACM
872};
873
874static int syscall_fmt__cmp(const void *name, const void *fmtp)
875{
876 const struct syscall_fmt *fmt = fmtp;
877 return strcmp(name, fmt->name);
878}
879
880static struct syscall_fmt *syscall_fmt__find(const char *name)
881{
882 const int nmemb = ARRAY_SIZE(syscall_fmts);
883 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
884}
885
f932184e
ACM
886static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
887{
888 int i, nmemb = ARRAY_SIZE(syscall_fmts);
889
890 for (i = 0; i < nmemb; ++i) {
891 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
892 return &syscall_fmts[i];
893 }
894
895 return NULL;
896}
897
6a648b53
ACM
898/*
899 * is_exit: is this "exit" or "exit_group"?
900 * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter.
7a983a0f 901 * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc.
6a648b53 902 */
514f1c67 903struct syscall {
97fbf3f0 904 struct tep_event *tp_format;
f208bd8d 905 int nr_args;
7a983a0f 906 int args_size;
6a648b53
ACM
907 bool is_exit;
908 bool is_open;
2c92f982 909 struct tep_format_field *args;
514f1c67
ACM
910 const char *name;
911 struct syscall_fmt *fmt;
82d4a110 912 struct syscall_arg_fmt *arg_fmt;
514f1c67
ACM
913};
914
bbab50dd
ACM
915struct bpf_map_syscall_entry {
916 bool enabled;
917};
918
fd2b2975
ACM
919/*
920 * We need to have this 'calculated' boolean because in some cases we really
921 * don't know what is the duration of a syscall, for instance, when we start
922 * a session and some threads are waiting for a syscall to finish, say 'poll',
923 * in which case all we can do is to print "( ? ) for duration and for the
924 * start timestamp.
925 */
926static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
60c907ab
ACM
927{
928 double duration = (double)t / NSEC_PER_MSEC;
929 size_t printed = fprintf(fp, "(");
930
fd2b2975 931 if (!calculated)
522283fe 932 printed += fprintf(fp, " ");
fd2b2975 933 else if (duration >= 1.0)
60c907ab
ACM
934 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
935 else if (duration >= 0.01)
936 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
937 else
938 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
c24ff998 939 return printed + fprintf(fp, "): ");
60c907ab
ACM
940}
941
f994592d
ACM
942/**
943 * filename.ptr: The filename char pointer that will be vfs_getname'd
944 * filename.entry_str_pos: Where to insert the string translated from
945 * filename.ptr by the vfs_getname tracepoint/kprobe.
84486caa
ACM
946 * ret_scnprintf: syscall args may set this to a different syscall return
947 * formatter, for instance, fcntl may return fds, file flags, etc.
f994592d 948 */
752fde44
ACM
949struct thread_trace {
950 u64 entry_time;
752fde44 951 bool entry_pending;
efd5745e 952 unsigned long nr_events;
a2ea67d7 953 unsigned long pfmaj, pfmin;
752fde44 954 char *entry_str;
1302d88e 955 double runtime_ms;
7ee57434 956 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
f994592d
ACM
957 struct {
958 unsigned long ptr;
7f4f8001
ACM
959 short int entry_str_pos;
960 bool pending_open;
961 unsigned int namelen;
962 char *name;
f994592d 963 } filename;
75b757ca
ACM
964 struct {
965 int max;
966 char **table;
967 } paths;
bf2575c1
DA
968
969 struct intlist *syscall_stats;
752fde44
ACM
970};
971
972static struct thread_trace *thread_trace__new(void)
973{
75b757ca
ACM
974 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
975
976 if (ttrace)
977 ttrace->paths.max = -1;
978
bf2575c1
DA
979 ttrace->syscall_stats = intlist__new(NULL);
980
75b757ca 981 return ttrace;
752fde44
ACM
982}
983
c24ff998 984static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
752fde44 985{
efd5745e
ACM
986 struct thread_trace *ttrace;
987
752fde44
ACM
988 if (thread == NULL)
989 goto fail;
990
89dceb22
NK
991 if (thread__priv(thread) == NULL)
992 thread__set_priv(thread, thread_trace__new());
48000a1a 993
89dceb22 994 if (thread__priv(thread) == NULL)
752fde44
ACM
995 goto fail;
996
89dceb22 997 ttrace = thread__priv(thread);
efd5745e
ACM
998 ++ttrace->nr_events;
999
1000 return ttrace;
752fde44 1001fail:
c24ff998 1002 color_fprintf(fp, PERF_COLOR_RED,
752fde44
ACM
1003 "WARNING: not enough memory, dropping samples!\n");
1004 return NULL;
1005}
1006
84486caa
ACM
1007
1008void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
7ee57434 1009 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
84486caa
ACM
1010{
1011 struct thread_trace *ttrace = thread__priv(arg->thread);
1012
1013 ttrace->ret_scnprintf = ret_scnprintf;
1014}
1015
598d02c5
SF
1016#define TRACE_PFMAJ (1 << 0)
1017#define TRACE_PFMIN (1 << 1)
1018
e4d44e83
ACM
1019static const size_t trace__entry_str_size = 2048;
1020
97119f37 1021static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
75b757ca 1022{
89dceb22 1023 struct thread_trace *ttrace = thread__priv(thread);
75b757ca
ACM
1024
1025 if (fd > ttrace->paths.max) {
1026 char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
1027
1028 if (npath == NULL)
1029 return -1;
1030
1031 if (ttrace->paths.max != -1) {
1032 memset(npath + ttrace->paths.max + 1, 0,
1033 (fd - ttrace->paths.max) * sizeof(char *));
1034 } else {
1035 memset(npath, 0, (fd + 1) * sizeof(char *));
1036 }
1037
1038 ttrace->paths.table = npath;
1039 ttrace->paths.max = fd;
1040 }
1041
1042 ttrace->paths.table[fd] = strdup(pathname);
1043
1044 return ttrace->paths.table[fd] != NULL ? 0 : -1;
1045}
1046
97119f37
ACM
1047static int thread__read_fd_path(struct thread *thread, int fd)
1048{
1049 char linkname[PATH_MAX], pathname[PATH_MAX];
1050 struct stat st;
1051 int ret;
1052
1053 if (thread->pid_ == thread->tid) {
1054 scnprintf(linkname, sizeof(linkname),
1055 "/proc/%d/fd/%d", thread->pid_, fd);
1056 } else {
1057 scnprintf(linkname, sizeof(linkname),
1058 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1059 }
1060
1061 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1062 return -1;
1063
1064 ret = readlink(linkname, pathname, sizeof(pathname));
1065
1066 if (ret < 0 || ret > st.st_size)
1067 return -1;
1068
1069 pathname[ret] = '\0';
1070 return trace__set_fd_pathname(thread, fd, pathname);
1071}
1072
c522739d
ACM
1073static const char *thread__fd_path(struct thread *thread, int fd,
1074 struct trace *trace)
75b757ca 1075{
89dceb22 1076 struct thread_trace *ttrace = thread__priv(thread);
75b757ca
ACM
1077
1078 if (ttrace == NULL)
1079 return NULL;
1080
1081 if (fd < 0)
1082 return NULL;
1083
cdcd1e6b 1084 if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
c522739d
ACM
1085 if (!trace->live)
1086 return NULL;
1087 ++trace->stats.proc_getname;
cdcd1e6b 1088 if (thread__read_fd_path(thread, fd))
c522739d
ACM
1089 return NULL;
1090 }
75b757ca
ACM
1091
1092 return ttrace->paths.table[fd];
1093}
1094
fc65eb82 1095size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
75b757ca
ACM
1096{
1097 int fd = arg->val;
1098 size_t printed = scnprintf(bf, size, "%d", fd);
c522739d 1099 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
75b757ca
ACM
1100
1101 if (path)
1102 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1103
1104 return printed;
1105}
1106
0a2f7540
ACM
1107size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1108{
1109 size_t printed = scnprintf(bf, size, "%d", fd);
1110 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1111
1112 if (thread) {
1113 const char *path = thread__fd_path(thread, fd, trace);
1114
1115 if (path)
1116 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1117
1118 thread__put(thread);
1119 }
1120
1121 return printed;
1122}
1123
75b757ca
ACM
1124static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1125 struct syscall_arg *arg)
1126{
1127 int fd = arg->val;
1128 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
89dceb22 1129 struct thread_trace *ttrace = thread__priv(arg->thread);
75b757ca 1130
04662523
ACM
1131 if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
1132 zfree(&ttrace->paths.table[fd]);
75b757ca
ACM
1133
1134 return printed;
1135}
1136
f994592d
ACM
1137static void thread__set_filename_pos(struct thread *thread, const char *bf,
1138 unsigned long ptr)
1139{
1140 struct thread_trace *ttrace = thread__priv(thread);
1141
1142 ttrace->filename.ptr = ptr;
1143 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1144}
1145
75d1e306
ACM
1146static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1147{
1148 struct augmented_arg *augmented_arg = arg->augmented.args;
1149
1150 return scnprintf(bf, size, "%.*s", augmented_arg->size, augmented_arg->value);
1151}
1152
f994592d
ACM
1153static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1154 struct syscall_arg *arg)
1155{
1156 unsigned long ptr = arg->val;
1157
75d1e306
ACM
1158 if (arg->augmented.args)
1159 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1160
f994592d
ACM
1161 if (!arg->trace->vfs_getname)
1162 return scnprintf(bf, size, "%#x", ptr);
1163
1164 thread__set_filename_pos(arg->thread, bf, ptr);
1165 return 0;
1166}
1167
ae9ed035
ACM
1168static bool trace__filter_duration(struct trace *trace, double t)
1169{
1170 return t < (trace->duration_filter * NSEC_PER_MSEC);
1171}
1172
fd2b2975 1173static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
752fde44
ACM
1174{
1175 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1176
60c907ab 1177 return fprintf(fp, "%10.3f ", ts);
752fde44
ACM
1178}
1179
fd2b2975
ACM
1180/*
1181 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1182 * using ttrace->entry_time for a thread that receives a sys_exit without
1183 * first having received a sys_enter ("poll" issued before tracing session
1184 * starts, lost sys_enter exit due to ring buffer overflow).
1185 */
1186static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1187{
1188 if (tstamp > 0)
1189 return __trace__fprintf_tstamp(trace, tstamp, fp);
1190
1191 return fprintf(fp, " ? ");
1192}
1193
f15eb531 1194static bool done = false;
ba209f85 1195static bool interrupted = false;
f15eb531 1196
ba209f85 1197static void sig_handler(int sig)
f15eb531
NK
1198{
1199 done = true;
ba209f85 1200 interrupted = sig == SIGINT;
f15eb531
NK
1201}
1202
6dcbd212 1203static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
752fde44 1204{
6dcbd212 1205 size_t printed = 0;
752fde44 1206
50c95cbd
ACM
1207 if (trace->multiple_threads) {
1208 if (trace->show_comm)
1902efe7 1209 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
38051234 1210 printed += fprintf(fp, "%d ", thread->tid);
50c95cbd 1211 }
752fde44
ACM
1212
1213 return printed;
1214}
1215
6dcbd212
ACM
1216static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1217 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1218{
b036146f
ACM
1219 size_t printed = 0;
1220
1221 if (trace->show_tstamp)
1222 printed = trace__fprintf_tstamp(trace, tstamp, fp);
42e4a52d
ACM
1223 if (trace->show_duration)
1224 printed += fprintf_duration(duration, duration_calculated, fp);
6dcbd212
ACM
1225 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1226}
1227
c24ff998 1228static int trace__process_event(struct trace *trace, struct machine *machine,
162f0bef 1229 union perf_event *event, struct perf_sample *sample)
752fde44
ACM
1230{
1231 int ret = 0;
1232
1233 switch (event->header.type) {
1234 case PERF_RECORD_LOST:
c24ff998 1235 color_fprintf(trace->output, PERF_COLOR_RED,
752fde44 1236 "LOST %" PRIu64 " events!\n", event->lost.lost);
162f0bef 1237 ret = machine__process_lost_event(machine, event, sample);
3ed5ca2e 1238 break;
752fde44 1239 default:
162f0bef 1240 ret = machine__process_event(machine, event, sample);
752fde44
ACM
1241 break;
1242 }
1243
1244 return ret;
1245}
1246
c24ff998 1247static int trace__tool_process(struct perf_tool *tool,
752fde44 1248 union perf_event *event,
162f0bef 1249 struct perf_sample *sample,
752fde44
ACM
1250 struct machine *machine)
1251{
c24ff998 1252 struct trace *trace = container_of(tool, struct trace, tool);
162f0bef 1253 return trace__process_event(trace, machine, event, sample);
752fde44
ACM
1254}
1255
caf8a0d0
ACM
1256static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1257{
1258 struct machine *machine = vmachine;
1259
1260 if (machine->kptr_restrict_warned)
1261 return NULL;
1262
1263 if (symbol_conf.kptr_restrict) {
1264 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1265 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1266 "Kernel samples will not be resolved.\n");
1267 machine->kptr_restrict_warned = true;
1268 return NULL;
1269 }
1270
1271 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1272}
1273
752fde44
ACM
1274static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1275{
0a7e6d1b 1276 int err = symbol__init(NULL);
752fde44
ACM
1277
1278 if (err)
1279 return err;
1280
8fb598e5
DA
1281 trace->host = machine__new_host();
1282 if (trace->host == NULL)
1283 return -ENOMEM;
752fde44 1284
cbd5c178
AV
1285 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1286 if (err < 0)
1287 goto out;
706c3da4 1288
a33fbd56 1289 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
9d9cad76 1290 evlist->threads, trace__tool_process, false,
3fcb10e4 1291 1);
cbd5c178 1292out:
752fde44
ACM
1293 if (err)
1294 symbol__exit();
1295
1296 return err;
1297}
1298
33974a41
AV
1299static void trace__symbols__exit(struct trace *trace)
1300{
1301 machine__exit(trace->host);
1302 trace->host = NULL;
1303
1304 symbol__exit();
1305}
1306
5e58fcfa 1307static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
13d4ff3e 1308{
5e58fcfa 1309 int idx;
13d4ff3e 1310
332337da
ACM
1311 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1312 nr_args = sc->fmt->nr_args;
1313
5e58fcfa 1314 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
82d4a110 1315 if (sc->arg_fmt == NULL)
13d4ff3e
ACM
1316 return -1;
1317
5e58fcfa
ACM
1318 for (idx = 0; idx < nr_args; ++idx) {
1319 if (sc->fmt)
82d4a110 1320 sc->arg_fmt[idx] = sc->fmt->arg[idx];
5e58fcfa 1321 }
82d4a110 1322
5e58fcfa
ACM
1323 sc->nr_args = nr_args;
1324 return 0;
1325}
1326
1327static int syscall__set_arg_fmts(struct syscall *sc)
1328{
2c92f982 1329 struct tep_format_field *field, *last_field = NULL;
5e58fcfa
ACM
1330 int idx = 0, len;
1331
1332 for (field = sc->args; field; field = field->next, ++idx) {
7a983a0f
ACM
1333 last_field = field;
1334
5e58fcfa
ACM
1335 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1336 continue;
1f115cb7 1337
82d4a110 1338 if (strcmp(field->type, "const char *") == 0 &&
12f3ca4f
ACM
1339 (strcmp(field->name, "filename") == 0 ||
1340 strcmp(field->name, "path") == 0 ||
1341 strcmp(field->name, "pathname") == 0))
82d4a110 1342 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
bb39ccb2 1343 else if (field->flags & TEP_FIELD_IS_POINTER)
82d4a110 1344 sc->arg_fmt[idx].scnprintf = syscall_arg__scnprintf_hex;
d1d438a3 1345 else if (strcmp(field->type, "pid_t") == 0)
82d4a110 1346 sc->arg_fmt[idx].scnprintf = SCA_PID;
ba2f22cf 1347 else if (strcmp(field->type, "umode_t") == 0)
82d4a110 1348 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
b6565c90
ACM
1349 else if ((strcmp(field->type, "int") == 0 ||
1350 strcmp(field->type, "unsigned int") == 0 ||
1351 strcmp(field->type, "long") == 0) &&
1352 (len = strlen(field->name)) >= 2 &&
1353 strcmp(field->name + len - 2, "fd") == 0) {
1354 /*
1355 * /sys/kernel/tracing/events/syscalls/sys_enter*
1356 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1357 * 65 int
1358 * 23 unsigned int
1359 * 7 unsigned long
1360 */
82d4a110 1361 sc->arg_fmt[idx].scnprintf = SCA_FD;
b6565c90 1362 }
13d4ff3e
ACM
1363 }
1364
7a983a0f
ACM
1365 if (last_field)
1366 sc->args_size = last_field->offset + last_field->size;
1367
13d4ff3e
ACM
1368 return 0;
1369}
1370
514f1c67
ACM
1371static int trace__read_syscall_info(struct trace *trace, int id)
1372{
1373 char tp_name[128];
1374 struct syscall *sc;
fd0db102 1375 const char *name = syscalltbl__name(trace->sctbl, id);
3a531260
ACM
1376
1377 if (name == NULL)
1378 return -1;
514f1c67
ACM
1379
1380 if (id > trace->syscalls.max) {
1381 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1382
1383 if (nsyscalls == NULL)
1384 return -1;
1385
1386 if (trace->syscalls.max != -1) {
1387 memset(nsyscalls + trace->syscalls.max + 1, 0,
1388 (id - trace->syscalls.max) * sizeof(*sc));
1389 } else {
1390 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1391 }
1392
1393 trace->syscalls.table = nsyscalls;
1394 trace->syscalls.max = id;
1395 }
1396
1397 sc = trace->syscalls.table + id;
3a531260 1398 sc->name = name;
2ae3a312 1399
3a531260 1400 sc->fmt = syscall_fmt__find(sc->name);
514f1c67 1401
aec1930b 1402 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
97978b3e 1403 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
aec1930b 1404
8dd2a131 1405 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
aec1930b 1406 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
97978b3e 1407 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
aec1930b 1408 }
514f1c67 1409
5e58fcfa
ACM
1410 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1411 return -1;
1412
8dd2a131 1413 if (IS_ERR(sc->tp_format))
13d4ff3e
ACM
1414 return -1;
1415
f208bd8d 1416 sc->args = sc->tp_format->format.fields;
c42de706
TS
1417 /*
1418 * We need to check and discard the first variable '__syscall_nr'
1419 * or 'nr' that mean the syscall number. It is needless here.
1420 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1421 */
1422 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
f208bd8d
ACM
1423 sc->args = sc->args->next;
1424 --sc->nr_args;
1425 }
1426
5089f20e 1427 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
6a648b53 1428 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
5089f20e 1429
13d4ff3e 1430 return syscall__set_arg_fmts(sc);
514f1c67
ACM
1431}
1432
d0cc439b
ACM
1433static int trace__validate_ev_qualifier(struct trace *trace)
1434{
8b3ce757 1435 int err = 0, i;
27702bcf 1436 size_t nr_allocated;
d0cc439b
ACM
1437 struct str_node *pos;
1438
8b3ce757
ACM
1439 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1440 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1441 sizeof(trace->ev_qualifier_ids.entries[0]));
1442
1443 if (trace->ev_qualifier_ids.entries == NULL) {
1444 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1445 trace->output);
1446 err = -EINVAL;
1447 goto out;
1448 }
1449
27702bcf 1450 nr_allocated = trace->ev_qualifier_ids.nr;
8b3ce757
ACM
1451 i = 0;
1452
602a1f4d 1453 strlist__for_each_entry(pos, trace->ev_qualifier) {
d0cc439b 1454 const char *sc = pos->s;
27702bcf 1455 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
d0cc439b 1456
8b3ce757 1457 if (id < 0) {
27702bcf
ACM
1458 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1459 if (id >= 0)
1460 goto matches;
1461
d0cc439b
ACM
1462 if (err == 0) {
1463 fputs("Error:\tInvalid syscall ", trace->output);
1464 err = -EINVAL;
1465 } else {
1466 fputs(", ", trace->output);
1467 }
1468
1469 fputs(sc, trace->output);
1470 }
27702bcf 1471matches:
8b3ce757 1472 trace->ev_qualifier_ids.entries[i++] = id;
27702bcf
ACM
1473 if (match_next == -1)
1474 continue;
1475
1476 while (1) {
1477 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1478 if (id < 0)
1479 break;
1480 if (nr_allocated == trace->ev_qualifier_ids.nr) {
1481 void *entries;
1482
1483 nr_allocated += 8;
1484 entries = realloc(trace->ev_qualifier_ids.entries,
1485 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1486 if (entries == NULL) {
1487 err = -ENOMEM;
1488 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1489 goto out_free;
1490 }
1491 trace->ev_qualifier_ids.entries = entries;
1492 }
1493 trace->ev_qualifier_ids.nr++;
1494 trace->ev_qualifier_ids.entries[i++] = id;
1495 }
d0cc439b
ACM
1496 }
1497
1498 if (err < 0) {
1499 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1500 "\nHint:\tand: 'man syscalls'\n", trace->output);
27702bcf 1501out_free:
8b3ce757
ACM
1502 zfree(&trace->ev_qualifier_ids.entries);
1503 trace->ev_qualifier_ids.nr = 0;
d0cc439b 1504 }
8b3ce757 1505out:
d0cc439b
ACM
1506 return err;
1507}
1508
55d43bca
DA
1509/*
1510 * args is to be interpreted as a series of longs but we need to handle
1511 * 8-byte unaligned accesses. args points to raw_data within the event
1512 * and raw_data is guaranteed to be 8-byte unaligned because it is
1513 * preceded by raw_size which is a u32. So we need to copy args to a temp
1514 * variable to read it. Most notably this avoids extended load instructions
1515 * on unaligned addresses
1516 */
325f5091 1517unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
f9f83b33
ACM
1518{
1519 unsigned long val;
325f5091 1520 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
f9f83b33
ACM
1521
1522 memcpy(&val, p, sizeof(val));
1523 return val;
1524}
1525
c51bdfec
ACM
1526static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1527 struct syscall_arg *arg)
1528{
1529 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1530 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1531
1532 return scnprintf(bf, size, "arg%d: ", arg->idx);
1533}
1534
496fd346
ACM
1535/*
1536 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1537 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1538 * in tools/perf/trace/beauty/mount_flags.c
1539 */
1540static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1541{
1542 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1543 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1544
1545 return val;
1546}
1547
d032d79e
ACM
1548static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1549 struct syscall_arg *arg, unsigned long val)
1550{
1551 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1552 arg->val = val;
1553 if (sc->arg_fmt[arg->idx].parm)
1554 arg->parm = sc->arg_fmt[arg->idx].parm;
1555 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1556 }
1557 return scnprintf(bf, size, "%ld", val);
1558}
1559
752fde44 1560static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
7a983a0f
ACM
1561 unsigned char *args, void *augmented_args, int augmented_args_size,
1562 struct trace *trace, struct thread *thread)
514f1c67 1563{
514f1c67 1564 size_t printed = 0;
55d43bca 1565 unsigned long val;
d032d79e
ACM
1566 u8 bit = 1;
1567 struct syscall_arg arg = {
1568 .args = args,
7a983a0f
ACM
1569 .augmented = {
1570 .size = augmented_args_size,
1571 .args = augmented_args,
1572 },
d032d79e
ACM
1573 .idx = 0,
1574 .mask = 0,
1575 .trace = trace,
1576 .thread = thread,
1577 };
84486caa
ACM
1578 struct thread_trace *ttrace = thread__priv(thread);
1579
1580 /*
1581 * Things like fcntl will set this in its 'cmd' formatter to pick the
1582 * right formatter for the return value (an fd? file flags?), which is
1583 * not needed for syscalls that always return a given type, say an fd.
1584 */
1585 ttrace->ret_scnprintf = NULL;
514f1c67 1586
f208bd8d 1587 if (sc->args != NULL) {
2c92f982 1588 struct tep_format_field *field;
6e7eeb51 1589
f208bd8d 1590 for (field = sc->args; field;
01533e97
ACM
1591 field = field->next, ++arg.idx, bit <<= 1) {
1592 if (arg.mask & bit)
6e7eeb51 1593 continue;
55d43bca 1594
f9f83b33 1595 val = syscall_arg__val(&arg, arg.idx);
496fd346
ACM
1596 /*
1597 * Some syscall args need some mask, most don't and
1598 * return val untouched.
1599 */
1600 val = syscall__mask_val(sc, &arg, val);
55d43bca 1601
4aa58232
ACM
1602 /*
1603 * Suppress this argument if its value is zero and
1604 * and we don't have a string associated in an
1605 * strarray for it.
1606 */
55d43bca 1607 if (val == 0 &&
e7c634fc 1608 !trace->show_zeros &&
82d4a110 1609 !(sc->arg_fmt &&
d47737d5
ACM
1610 (sc->arg_fmt[arg.idx].show_zero ||
1611 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
82d4a110
ACM
1612 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1613 sc->arg_fmt[arg.idx].parm))
22ae5cf1
ACM
1614 continue;
1615
752fde44 1616 printed += scnprintf(bf + printed, size - printed,
13d4ff3e 1617 "%s%s: ", printed ? ", " : "", field->name);
d032d79e 1618 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
514f1c67 1619 }
4c4d6e51
ACM
1620 } else if (IS_ERR(sc->tp_format)) {
1621 /*
1622 * If we managed to read the tracepoint /format file, then we
1623 * may end up not having any args, like with gettid(), so only
1624 * print the raw args when we didn't manage to read it.
1625 */
332337da 1626 while (arg.idx < sc->nr_args) {
d032d79e
ACM
1627 if (arg.mask & bit)
1628 goto next_arg;
1629 val = syscall_arg__val(&arg, arg.idx);
c51bdfec
ACM
1630 if (printed)
1631 printed += scnprintf(bf + printed, size - printed, ", ");
1632 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
d032d79e
ACM
1633 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1634next_arg:
1635 ++arg.idx;
1636 bit <<= 1;
514f1c67
ACM
1637 }
1638 }
1639
1640 return printed;
1641}
1642
ba3d7dee 1643typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 1644 union perf_event *event,
ba3d7dee
ACM
1645 struct perf_sample *sample);
1646
1647static struct syscall *trace__syscall_info(struct trace *trace,
bf2575c1 1648 struct perf_evsel *evsel, int id)
ba3d7dee 1649{
ba3d7dee
ACM
1650
1651 if (id < 0) {
adaa18bf
ACM
1652
1653 /*
1654 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1655 * before that, leaving at a higher verbosity level till that is
1656 * explained. Reproduced with plain ftrace with:
1657 *
1658 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1659 * grep "NR -1 " /t/trace_pipe
1660 *
1661 * After generating some load on the machine.
1662 */
1663 if (verbose > 1) {
1664 static u64 n;
1665 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1666 id, perf_evsel__name(evsel), ++n);
1667 }
ba3d7dee
ACM
1668 return NULL;
1669 }
1670
1671 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1672 trace__read_syscall_info(trace, id))
1673 goto out_cant_read;
1674
1675 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1676 goto out_cant_read;
1677
1678 return &trace->syscalls.table[id];
1679
1680out_cant_read:
bb963e16 1681 if (verbose > 0) {
7c304ee0
ACM
1682 fprintf(trace->output, "Problems reading syscall %d", id);
1683 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1684 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1685 fputs(" information\n", trace->output);
1686 }
ba3d7dee
ACM
1687 return NULL;
1688}
1689
bf2575c1
DA
1690static void thread__update_stats(struct thread_trace *ttrace,
1691 int id, struct perf_sample *sample)
1692{
1693 struct int_node *inode;
1694 struct stats *stats;
1695 u64 duration = 0;
1696
1697 inode = intlist__findnew(ttrace->syscall_stats, id);
1698 if (inode == NULL)
1699 return;
1700
1701 stats = inode->priv;
1702 if (stats == NULL) {
1703 stats = malloc(sizeof(struct stats));
1704 if (stats == NULL)
1705 return;
1706 init_stats(stats);
1707 inode->priv = stats;
1708 }
1709
1710 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1711 duration = sample->time - ttrace->entry_time;
1712
1713 update_stats(stats, duration);
1714}
1715
522283fe 1716static int trace__printf_interrupted_entry(struct trace *trace)
e596663e
ACM
1717{
1718 struct thread_trace *ttrace;
e596663e
ACM
1719 size_t printed;
1720
0a6545bd 1721 if (trace->failure_only || trace->current == NULL)
e596663e
ACM
1722 return 0;
1723
1724 ttrace = thread__priv(trace->current);
1725
1726 if (!ttrace->entry_pending)
1727 return 0;
1728
522283fe 1729 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
e596663e
ACM
1730 printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
1731 ttrace->entry_pending = false;
1732
5067a8cd
ACM
1733 ++trace->nr_events_printed;
1734
e596663e
ACM
1735 return printed;
1736}
1737
591421e1
ACM
1738static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1739 struct perf_sample *sample, struct thread *thread)
1740{
1741 int printed = 0;
1742
1743 if (trace->print_sample) {
1744 double ts = (double)sample->time / NSEC_PER_MSEC;
1745
1746 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1747 perf_evsel__name(evsel), ts,
1748 thread__comm_str(thread),
1749 sample->pid, sample->tid, sample->cpu);
1750 }
1751
1752 return printed;
1753}
1754
cd26ea6d 1755static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, bool raw_augmented)
8a041f86
ACM
1756{
1757 void *augmented_args = NULL;
cd26ea6d
ACM
1758 /*
1759 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
1760 * and there we get all 6 syscall args plus the tracepoint common
1761 * fields (sizeof(long)) and the syscall_nr (another long). So we check
1762 * if that is the case and if so don't look after the sc->args_size,
1763 * but always after the full raw_syscalls:sys_enter payload, which is
1764 * fixed.
1765 *
1766 * We'll revisit this later to pass s->args_size to the BPF augmenter
1767 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1768 * copies only what we need for each syscall, like what happens when we
1769 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1770 * traffic to just what is needed for each syscall.
1771 */
1772 int args_size = raw_augmented ? (8 * (int)sizeof(long)) : sc->args_size;
8a041f86 1773
cd26ea6d 1774 *augmented_args_size = sample->raw_size - args_size;
8a041f86 1775 if (*augmented_args_size > 0)
cd26ea6d 1776 augmented_args = sample->raw_data + args_size;
8a041f86
ACM
1777
1778 return augmented_args;
1779}
1780
ba3d7dee 1781static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 1782 union perf_event *event __maybe_unused,
ba3d7dee
ACM
1783 struct perf_sample *sample)
1784{
752fde44 1785 char *msg;
ba3d7dee 1786 void *args;
752fde44 1787 size_t printed = 0;
2ae3a312 1788 struct thread *thread;
b91fc39f 1789 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
db2da3f8
ACM
1790 int augmented_args_size = 0;
1791 void *augmented_args = NULL;
bf2575c1 1792 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2ae3a312
ACM
1793 struct thread_trace *ttrace;
1794
1795 if (sc == NULL)
1796 return -1;
ba3d7dee 1797
8fb598e5 1798 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
c24ff998 1799 ttrace = thread__trace(thread, trace->output);
2ae3a312 1800 if (ttrace == NULL)
b91fc39f 1801 goto out_put;
ba3d7dee 1802
591421e1
ACM
1803 trace__fprintf_sample(trace, evsel, sample, thread);
1804
77170988 1805 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
752fde44
ACM
1806
1807 if (ttrace->entry_str == NULL) {
e4d44e83 1808 ttrace->entry_str = malloc(trace__entry_str_size);
752fde44 1809 if (!ttrace->entry_str)
b91fc39f 1810 goto out_put;
752fde44
ACM
1811 }
1812
5cf9c84e 1813 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
522283fe 1814 trace__printf_interrupted_entry(trace);
db2da3f8
ACM
1815 /*
1816 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1817 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1818 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1819 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1820 * so when handling, say the openat syscall, we end up getting 6 args for the
1821 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1822 * thinking that the extra 2 u64 args are the augmented filename, so just check
1823 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1824 */
1825 if (evsel != trace->syscalls.events.sys_enter)
cd26ea6d 1826 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
752fde44
ACM
1827 ttrace->entry_time = sample->time;
1828 msg = ttrace->entry_str;
e4d44e83 1829 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
752fde44 1830
e4d44e83 1831 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
db2da3f8 1832 args, augmented_args, augmented_args_size, trace, thread);
752fde44 1833
5089f20e 1834 if (sc->is_exit) {
0a6545bd 1835 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
fd2b2975 1836 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
c008f78f 1837 fprintf(trace->output, "%-70s)\n", ttrace->entry_str);
ae9ed035 1838 }
7f4f8001 1839 } else {
752fde44 1840 ttrace->entry_pending = true;
7f4f8001
ACM
1841 /* See trace__vfs_getname & trace__sys_exit */
1842 ttrace->filename.pending_open = false;
1843 }
ba3d7dee 1844
f3b623b8
ACM
1845 if (trace->current != thread) {
1846 thread__put(trace->current);
1847 trace->current = thread__get(thread);
1848 }
b91fc39f
ACM
1849 err = 0;
1850out_put:
1851 thread__put(thread);
1852 return err;
ba3d7dee
ACM
1853}
1854
a98392bb
ACM
1855static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1856 struct perf_sample *sample)
1857{
a98392bb
ACM
1858 struct thread_trace *ttrace;
1859 struct thread *thread;
f3acd886
ACM
1860 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1861 struct syscall *sc = trace__syscall_info(trace, evsel, id);
a98392bb 1862 char msg[1024];
7a983a0f
ACM
1863 void *args, *augmented_args = NULL;
1864 int augmented_args_size;
a98392bb 1865
a98392bb
ACM
1866 if (sc == NULL)
1867 return -1;
1868
1869 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1870 ttrace = thread__trace(thread, trace->output);
1871 /*
1872 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1873 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1874 */
1875 if (ttrace == NULL)
1876 goto out_put;
1877
f3acd886 1878 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
cd26ea6d 1879 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
7a983a0f 1880 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
a98392bb
ACM
1881 fprintf(trace->output, "%s", msg);
1882 err = 0;
1883out_put:
1884 thread__put(thread);
1885 return err;
1886}
1887
5cf9c84e
ACM
1888static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1889 struct perf_sample *sample,
1890 struct callchain_cursor *cursor)
202ff968
ACM
1891{
1892 struct addr_location al;
3a9e9a47
RB
1893 int max_stack = evsel->attr.sample_max_stack ?
1894 evsel->attr.sample_max_stack :
1895 trace->max_stack;
4291bf5c 1896 int err;
5cf9c84e 1897
4291bf5c 1898 if (machine__resolve(trace->host, &al, sample) < 0)
5cf9c84e
ACM
1899 return -1;
1900
4291bf5c
ACM
1901 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1902 addr_location__put(&al);
1903 return err;
5cf9c84e
ACM
1904}
1905
1906static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1907{
202ff968 1908 /* TODO: user-configurable print_opts */
e20ab86e
ACM
1909 const unsigned int print_opts = EVSEL__PRINT_SYM |
1910 EVSEL__PRINT_DSO |
1911 EVSEL__PRINT_UNKNOWN_AS_ADDR;
202ff968 1912
d327e60c 1913 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
202ff968
ACM
1914}
1915
092bd3cd
HB
1916static const char *errno_to_name(struct perf_evsel *evsel, int err)
1917{
1918 struct perf_env *env = perf_evsel__env(evsel);
1919 const char *arch_name = perf_env__arch(env);
1920
1921 return arch_syscalls__strerrno(arch_name, err);
1922}
1923
ba3d7dee 1924static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 1925 union perf_event *event __maybe_unused,
ba3d7dee
ACM
1926 struct perf_sample *sample)
1927{
2c82c3ad 1928 long ret;
60c907ab 1929 u64 duration = 0;
fd2b2975 1930 bool duration_calculated = false;
2ae3a312 1931 struct thread *thread;
5cf9c84e 1932 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0;
bf2575c1 1933 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2ae3a312
ACM
1934 struct thread_trace *ttrace;
1935
1936 if (sc == NULL)
1937 return -1;
ba3d7dee 1938
8fb598e5 1939 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
c24ff998 1940 ttrace = thread__trace(thread, trace->output);
2ae3a312 1941 if (ttrace == NULL)
b91fc39f 1942 goto out_put;
ba3d7dee 1943
591421e1
ACM
1944 trace__fprintf_sample(trace, evsel, sample, thread);
1945
bf2575c1
DA
1946 if (trace->summary)
1947 thread__update_stats(ttrace, id, sample);
1948
77170988 1949 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
ba3d7dee 1950
6a648b53 1951 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
7f4f8001
ACM
1952 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
1953 ttrace->filename.pending_open = false;
c522739d
ACM
1954 ++trace->stats.vfs_getname;
1955 }
1956
ae9ed035 1957 if (ttrace->entry_time) {
60c907ab 1958 duration = sample->time - ttrace->entry_time;
ae9ed035
ACM
1959 if (trace__filter_duration(trace, duration))
1960 goto out;
fd2b2975 1961 duration_calculated = true;
ae9ed035
ACM
1962 } else if (trace->duration_filter)
1963 goto out;
60c907ab 1964
5cf9c84e
ACM
1965 if (sample->callchain) {
1966 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
1967 if (callchain_ret == 0) {
1968 if (callchain_cursor.nr < trace->min_stack)
1969 goto out;
1970 callchain_ret = 1;
1971 }
1972 }
1973
0a6545bd 1974 if (trace->summary_only || (ret >= 0 && trace->failure_only))
fd2eabaf
DA
1975 goto out;
1976
fd2b2975 1977 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
752fde44
ACM
1978
1979 if (ttrace->entry_pending) {
c24ff998 1980 fprintf(trace->output, "%-70s", ttrace->entry_str);
752fde44 1981 } else {
c24ff998
ACM
1982 fprintf(trace->output, " ... [");
1983 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
1984 fprintf(trace->output, "]: %s()", sc->name);
752fde44
ACM
1985 }
1986
da3c9a44 1987 if (sc->fmt == NULL) {
1f63139c
ACM
1988 if (ret < 0)
1989 goto errno_print;
da3c9a44 1990signed_print:
6f8fe61e 1991 fprintf(trace->output, ") = %ld", ret);
1f63139c
ACM
1992 } else if (ret < 0) {
1993errno_print: {
942a91ed 1994 char bf[STRERR_BUFSIZE];
c8b5f2c9 1995 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
092bd3cd 1996 *e = errno_to_name(evsel, -ret);
ba3d7dee 1997
c24ff998 1998 fprintf(trace->output, ") = -1 %s %s", e, emsg);
1f63139c 1999 }
da3c9a44 2000 } else if (ret == 0 && sc->fmt->timeout)
c24ff998 2001 fprintf(trace->output, ") = 0 Timeout");
84486caa
ACM
2002 else if (ttrace->ret_scnprintf) {
2003 char bf[1024];
7ee57434
ACM
2004 struct syscall_arg arg = {
2005 .val = ret,
2006 .thread = thread,
2007 .trace = trace,
2008 };
2009 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
84486caa
ACM
2010 ttrace->ret_scnprintf = NULL;
2011 fprintf(trace->output, ") = %s", bf);
2012 } else if (sc->fmt->hexret)
2c82c3ad 2013 fprintf(trace->output, ") = %#lx", ret);
11c8e39f
ACM
2014 else if (sc->fmt->errpid) {
2015 struct thread *child = machine__find_thread(trace->host, ret, ret);
2016
2017 if (child != NULL) {
2018 fprintf(trace->output, ") = %ld", ret);
2019 if (child->comm_set)
2020 fprintf(trace->output, " (%s)", thread__comm_str(child));
2021 thread__put(child);
2022 }
2023 } else
da3c9a44 2024 goto signed_print;
ba3d7dee 2025
c24ff998 2026 fputc('\n', trace->output);
566a0885 2027
5067a8cd
ACM
2028 /*
2029 * We only consider an 'event' for the sake of --max-events a non-filtered
2030 * sys_enter + sys_exit and other tracepoint events.
2031 */
2032 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2033 interrupted = true;
2034
5cf9c84e
ACM
2035 if (callchain_ret > 0)
2036 trace__fprintf_callchain(trace, sample);
2037 else if (callchain_ret < 0)
2038 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
ae9ed035 2039out:
752fde44 2040 ttrace->entry_pending = false;
b91fc39f
ACM
2041 err = 0;
2042out_put:
2043 thread__put(thread);
2044 return err;
ba3d7dee
ACM
2045}
2046
c522739d 2047static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 2048 union perf_event *event __maybe_unused,
c522739d
ACM
2049 struct perf_sample *sample)
2050{
f994592d
ACM
2051 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2052 struct thread_trace *ttrace;
2053 size_t filename_len, entry_str_len, to_move;
2054 ssize_t remaining_space;
2055 char *pos;
7f4f8001 2056 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
f994592d
ACM
2057
2058 if (!thread)
2059 goto out;
2060
2061 ttrace = thread__priv(thread);
2062 if (!ttrace)
ef65e96e 2063 goto out_put;
f994592d 2064
7f4f8001 2065 filename_len = strlen(filename);
39f0e7a8 2066 if (filename_len == 0)
ef65e96e 2067 goto out_put;
7f4f8001
ACM
2068
2069 if (ttrace->filename.namelen < filename_len) {
2070 char *f = realloc(ttrace->filename.name, filename_len + 1);
2071
2072 if (f == NULL)
ef65e96e 2073 goto out_put;
7f4f8001
ACM
2074
2075 ttrace->filename.namelen = filename_len;
2076 ttrace->filename.name = f;
2077 }
2078
2079 strcpy(ttrace->filename.name, filename);
2080 ttrace->filename.pending_open = true;
2081
f994592d 2082 if (!ttrace->filename.ptr)
ef65e96e 2083 goto out_put;
f994592d
ACM
2084
2085 entry_str_len = strlen(ttrace->entry_str);
2086 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2087 if (remaining_space <= 0)
ef65e96e 2088 goto out_put;
f994592d 2089
f994592d
ACM
2090 if (filename_len > (size_t)remaining_space) {
2091 filename += filename_len - remaining_space;
2092 filename_len = remaining_space;
2093 }
2094
2095 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2096 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2097 memmove(pos + filename_len, pos, to_move);
2098 memcpy(pos, filename, filename_len);
2099
2100 ttrace->filename.ptr = 0;
2101 ttrace->filename.entry_str_pos = 0;
ef65e96e
ACM
2102out_put:
2103 thread__put(thread);
f994592d 2104out:
c522739d
ACM
2105 return 0;
2106}
2107
1302d88e 2108static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
0c82adcf 2109 union perf_event *event __maybe_unused,
1302d88e
ACM
2110 struct perf_sample *sample)
2111{
2112 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2113 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
8fb598e5 2114 struct thread *thread = machine__findnew_thread(trace->host,
314add6b
AH
2115 sample->pid,
2116 sample->tid);
c24ff998 2117 struct thread_trace *ttrace = thread__trace(thread, trace->output);
1302d88e
ACM
2118
2119 if (ttrace == NULL)
2120 goto out_dump;
2121
2122 ttrace->runtime_ms += runtime_ms;
2123 trace->runtime_ms += runtime_ms;
ef65e96e 2124out_put:
b91fc39f 2125 thread__put(thread);
1302d88e
ACM
2126 return 0;
2127
2128out_dump:
c24ff998 2129 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
1302d88e
ACM
2130 evsel->name,
2131 perf_evsel__strval(evsel, sample, "comm"),
2132 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2133 runtime,
2134 perf_evsel__intval(evsel, sample, "vruntime"));
ef65e96e 2135 goto out_put;
1302d88e
ACM
2136}
2137
923d0c9a
ACM
2138static int bpf_output__printer(enum binary_printer_ops op,
2139 unsigned int val, void *extra __maybe_unused, FILE *fp)
1d6c9407 2140{
1d6c9407
WN
2141 unsigned char ch = (unsigned char)val;
2142
2143 switch (op) {
2144 case BINARY_PRINT_CHAR_DATA:
923d0c9a 2145 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
1d6c9407
WN
2146 case BINARY_PRINT_DATA_BEGIN:
2147 case BINARY_PRINT_LINE_BEGIN:
2148 case BINARY_PRINT_ADDR:
2149 case BINARY_PRINT_NUM_DATA:
2150 case BINARY_PRINT_NUM_PAD:
2151 case BINARY_PRINT_SEP:
2152 case BINARY_PRINT_CHAR_PAD:
2153 case BINARY_PRINT_LINE_END:
2154 case BINARY_PRINT_DATA_END:
2155 default:
2156 break;
2157 }
923d0c9a
ACM
2158
2159 return 0;
1d6c9407
WN
2160}
2161
2162static void bpf_output__fprintf(struct trace *trace,
2163 struct perf_sample *sample)
2164{
923d0c9a
ACM
2165 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2166 bpf_output__printer, NULL, trace->output);
5067a8cd 2167 ++trace->nr_events_printed;
1d6c9407
WN
2168}
2169
14a052df
ACM
2170static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2171 union perf_event *event __maybe_unused,
2172 struct perf_sample *sample)
2173{
a9c5e6c1 2174 struct thread *thread;
7ad35615 2175 int callchain_ret = 0;
a9c5e6c1
ACM
2176 /*
2177 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2178 * this event's max_events having been hit and this is an entry coming
2179 * from the ring buffer that we should discard, since the max events
2180 * have already been considered/printed.
2181 */
2182 if (evsel->disabled)
2183 return 0;
2184
2185 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
7ad35615
ACM
2186
2187 if (sample->callchain) {
2188 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2189 if (callchain_ret == 0) {
2190 if (callchain_cursor.nr < trace->min_stack)
2191 goto out;
2192 callchain_ret = 1;
2193 }
2194 }
2195
522283fe 2196 trace__printf_interrupted_entry(trace);
14a052df 2197 trace__fprintf_tstamp(trace, sample->time, trace->output);
0808921a 2198
42e4a52d 2199 if (trace->trace_syscalls && trace->show_duration)
0808921a
ACM
2200 fprintf(trace->output, "( ): ");
2201
c4191e55
ACM
2202 if (thread)
2203 trace__fprintf_comm_tid(trace, thread, trace->output);
2204
1cdf618f
ACM
2205 if (evsel == trace->syscalls.events.augmented) {
2206 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2207 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2208
2209 if (sc) {
c4191e55
ACM
2210 fprintf(trace->output, "%s(", sc->name);
2211 trace__fprintf_sys_enter(trace, evsel, sample);
2212 fputc(')', trace->output);
2213 goto newline;
1cdf618f
ACM
2214 }
2215
2216 /*
2217 * XXX: Not having the associated syscall info or not finding/adding
2218 * the thread should never happen, but if it does...
2219 * fall thru and print it as a bpf_output event.
2220 */
2221 }
2222
0808921a 2223 fprintf(trace->output, "%s:", evsel->name);
14a052df 2224
1d6c9407 2225 if (perf_evsel__is_bpf_output(evsel)) {
1cdf618f 2226 bpf_output__fprintf(trace, sample);
1d6c9407 2227 } else if (evsel->tp_format) {
a98392bb
ACM
2228 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2229 trace__fprintf_sys_enter(trace, evsel, sample)) {
2230 event_format__fprintf(evsel->tp_format, sample->cpu,
2231 sample->raw_data, sample->raw_size,
2232 trace->output);
5067a8cd 2233 ++trace->nr_events_printed;
a9c5e6c1
ACM
2234
2235 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2236 perf_evsel__disable(evsel);
2237 perf_evsel__close(evsel);
2238 }
a98392bb 2239 }
14a052df
ACM
2240 }
2241
1cdf618f 2242newline:
51125a29 2243 fprintf(trace->output, "\n");
202ff968 2244
7ad35615
ACM
2245 if (callchain_ret > 0)
2246 trace__fprintf_callchain(trace, sample);
2247 else if (callchain_ret < 0)
2248 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2249out:
a937c665 2250 thread__put(thread);
14a052df
ACM
2251 return 0;
2252}
2253
598d02c5
SF
2254static void print_location(FILE *f, struct perf_sample *sample,
2255 struct addr_location *al,
2256 bool print_dso, bool print_sym)
2257{
2258
bb963e16 2259 if ((verbose > 0 || print_dso) && al->map)
598d02c5
SF
2260 fprintf(f, "%s@", al->map->dso->long_name);
2261
bb963e16 2262 if ((verbose > 0 || print_sym) && al->sym)
4414a3c5 2263 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
598d02c5
SF
2264 al->addr - al->sym->start);
2265 else if (al->map)
4414a3c5 2266 fprintf(f, "0x%" PRIx64, al->addr);
598d02c5 2267 else
4414a3c5 2268 fprintf(f, "0x%" PRIx64, sample->addr);
598d02c5
SF
2269}
2270
2271static int trace__pgfault(struct trace *trace,
2272 struct perf_evsel *evsel,
473398a2 2273 union perf_event *event __maybe_unused,
598d02c5
SF
2274 struct perf_sample *sample)
2275{
2276 struct thread *thread;
598d02c5
SF
2277 struct addr_location al;
2278 char map_type = 'd';
a2ea67d7 2279 struct thread_trace *ttrace;
b91fc39f 2280 int err = -1;
1df54290 2281 int callchain_ret = 0;
598d02c5
SF
2282
2283 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1df54290
ACM
2284
2285 if (sample->callchain) {
2286 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2287 if (callchain_ret == 0) {
2288 if (callchain_cursor.nr < trace->min_stack)
2289 goto out_put;
2290 callchain_ret = 1;
2291 }
2292 }
2293
a2ea67d7
SF
2294 ttrace = thread__trace(thread, trace->output);
2295 if (ttrace == NULL)
b91fc39f 2296 goto out_put;
a2ea67d7
SF
2297
2298 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2299 ttrace->pfmaj++;
2300 else
2301 ttrace->pfmin++;
2302
2303 if (trace->summary_only)
b91fc39f 2304 goto out;
598d02c5 2305
4546263d 2306 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
598d02c5 2307
fd2b2975 2308 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
598d02c5
SF
2309
2310 fprintf(trace->output, "%sfault [",
2311 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2312 "maj" : "min");
2313
2314 print_location(trace->output, sample, &al, false, true);
2315
2316 fprintf(trace->output, "] => ");
2317
117d3c24 2318 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
598d02c5
SF
2319
2320 if (!al.map) {
4546263d 2321 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
598d02c5
SF
2322
2323 if (al.map)
2324 map_type = 'x';
2325 else
2326 map_type = '?';
2327 }
2328
2329 print_location(trace->output, sample, &al, true, false);
2330
2331 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
0c3a6ef4 2332
1df54290
ACM
2333 if (callchain_ret > 0)
2334 trace__fprintf_callchain(trace, sample);
2335 else if (callchain_ret < 0)
2336 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
5067a8cd
ACM
2337
2338 ++trace->nr_events_printed;
b91fc39f
ACM
2339out:
2340 err = 0;
2341out_put:
2342 thread__put(thread);
2343 return err;
598d02c5
SF
2344}
2345
e6001980 2346static void trace__set_base_time(struct trace *trace,
8a07a809 2347 struct perf_evsel *evsel,
e6001980
ACM
2348 struct perf_sample *sample)
2349{
8a07a809
ACM
2350 /*
2351 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2352 * and don't use sample->time unconditionally, we may end up having
2353 * some other event in the future without PERF_SAMPLE_TIME for good
2354 * reason, i.e. we may not be interested in its timestamps, just in
2355 * it taking place, picking some piece of information when it
2356 * appears in our event stream (vfs_getname comes to mind).
2357 */
2358 if (trace->base_time == 0 && !trace->full_time &&
2359 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
e6001980
ACM
2360 trace->base_time = sample->time;
2361}
2362
6810fc91 2363static int trace__process_sample(struct perf_tool *tool,
0c82adcf 2364 union perf_event *event,
6810fc91
DA
2365 struct perf_sample *sample,
2366 struct perf_evsel *evsel,
2367 struct machine *machine __maybe_unused)
2368{
2369 struct trace *trace = container_of(tool, struct trace, tool);
aa07df6e 2370 struct thread *thread;
6810fc91
DA
2371 int err = 0;
2372
744a9719 2373 tracepoint_handler handler = evsel->handler;
6810fc91 2374
aa07df6e
DA
2375 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2376 if (thread && thread__is_filtered(thread))
ef65e96e 2377 goto out;
bdc89661 2378
e6001980 2379 trace__set_base_time(trace, evsel, sample);
6810fc91 2380
3160565f
DA
2381 if (handler) {
2382 ++trace->nr_events;
0c82adcf 2383 handler(trace, evsel, event, sample);
3160565f 2384 }
ef65e96e
ACM
2385out:
2386 thread__put(thread);
6810fc91
DA
2387 return err;
2388}
2389
1e28fe0a 2390static int trace__record(struct trace *trace, int argc, const char **argv)
5e2485b1
DA
2391{
2392 unsigned int rec_argc, i, j;
2393 const char **rec_argv;
2394 const char * const record_args[] = {
2395 "record",
2396 "-R",
2397 "-m", "1024",
2398 "-c", "1",
5e2485b1
DA
2399 };
2400
1e28fe0a
SF
2401 const char * const sc_args[] = { "-e", };
2402 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2403 const char * const majpf_args[] = { "-e", "major-faults" };
2404 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2405 const char * const minpf_args[] = { "-e", "minor-faults" };
2406 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2407
9aca7f17 2408 /* +1 is for the event string below */
1e28fe0a
SF
2409 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2410 majpf_args_nr + minpf_args_nr + argc;
5e2485b1
DA
2411 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2412
2413 if (rec_argv == NULL)
2414 return -ENOMEM;
2415
1e28fe0a 2416 j = 0;
5e2485b1 2417 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1e28fe0a
SF
2418 rec_argv[j++] = record_args[i];
2419
e281a960
SF
2420 if (trace->trace_syscalls) {
2421 for (i = 0; i < sc_args_nr; i++)
2422 rec_argv[j++] = sc_args[i];
2423
2424 /* event string may be different for older kernels - e.g., RHEL6 */
2425 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2426 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2427 else if (is_valid_tracepoint("syscalls:sys_enter"))
2428 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2429 else {
2430 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
c896f85a 2431 free(rec_argv);
e281a960
SF
2432 return -1;
2433 }
9aca7f17 2434 }
9aca7f17 2435
1e28fe0a
SF
2436 if (trace->trace_pgfaults & TRACE_PFMAJ)
2437 for (i = 0; i < majpf_args_nr; i++)
2438 rec_argv[j++] = majpf_args[i];
2439
2440 if (trace->trace_pgfaults & TRACE_PFMIN)
2441 for (i = 0; i < minpf_args_nr; i++)
2442 rec_argv[j++] = minpf_args[i];
2443
2444 for (i = 0; i < (unsigned int)argc; i++)
2445 rec_argv[j++] = argv[i];
5e2485b1 2446
b0ad8ea6 2447 return cmd_record(j, rec_argv);
5e2485b1
DA
2448}
2449
bf2575c1
DA
2450static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2451
08c98776 2452static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
c522739d 2453{
ef503831 2454 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
8dd2a131
JO
2455
2456 if (IS_ERR(evsel))
08c98776 2457 return false;
c522739d
ACM
2458
2459 if (perf_evsel__field(evsel, "pathname") == NULL) {
2460 perf_evsel__delete(evsel);
08c98776 2461 return false;
c522739d
ACM
2462 }
2463
744a9719 2464 evsel->handler = trace__vfs_getname;
c522739d 2465 perf_evlist__add(evlist, evsel);
08c98776 2466 return true;
c522739d
ACM
2467}
2468
0ae537cb 2469static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
598d02c5
SF
2470{
2471 struct perf_evsel *evsel;
2472 struct perf_event_attr attr = {
2473 .type = PERF_TYPE_SOFTWARE,
2474 .mmap_data = 1,
598d02c5
SF
2475 };
2476
2477 attr.config = config;
0524798c 2478 attr.sample_period = 1;
598d02c5
SF
2479
2480 event_attr_init(&attr);
2481
2482 evsel = perf_evsel__new(&attr);
0ae537cb
ACM
2483 if (evsel)
2484 evsel->handler = trace__pgfault;
598d02c5 2485
0ae537cb 2486 return evsel;
598d02c5
SF
2487}
2488
ddbb1b13
ACM
2489static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2490{
2491 const u32 type = event->header.type;
2492 struct perf_evsel *evsel;
2493
ddbb1b13
ACM
2494 if (type != PERF_RECORD_SAMPLE) {
2495 trace__process_event(trace, trace->host, event, sample);
2496 return;
2497 }
2498
2499 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2500 if (evsel == NULL) {
2501 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2502 return;
2503 }
2504
e6001980
ACM
2505 trace__set_base_time(trace, evsel, sample);
2506
ddbb1b13
ACM
2507 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2508 sample->raw_data == NULL) {
2509 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2510 perf_evsel__name(evsel), sample->tid,
2511 sample->cpu, sample->raw_size);
2512 } else {
2513 tracepoint_handler handler = evsel->handler;
2514 handler(trace, evsel, event, sample);
2515 }
5067a8cd
ACM
2516
2517 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2518 interrupted = true;
ddbb1b13
ACM
2519}
2520
c27366f0
ACM
2521static int trace__add_syscall_newtp(struct trace *trace)
2522{
2523 int ret = -1;
2524 struct perf_evlist *evlist = trace->evlist;
2525 struct perf_evsel *sys_enter, *sys_exit;
2526
63f11c80 2527 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
c27366f0
ACM
2528 if (sys_enter == NULL)
2529 goto out;
2530
2531 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2532 goto out_delete_sys_enter;
2533
63f11c80 2534 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
c27366f0
ACM
2535 if (sys_exit == NULL)
2536 goto out_delete_sys_enter;
2537
2538 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2539 goto out_delete_sys_exit;
2540
08e26396
ACM
2541 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2542 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2543
c27366f0
ACM
2544 perf_evlist__add(evlist, sys_enter);
2545 perf_evlist__add(evlist, sys_exit);
2546
2ddd5c04 2547 if (callchain_param.enabled && !trace->kernel_syscallchains) {
44621819
ACM
2548 /*
2549 * We're interested only in the user space callchain
2550 * leading to the syscall, allow overriding that for
2551 * debugging reasons using --kernel_syscall_callchains
2552 */
2553 sys_exit->attr.exclude_callchain_kernel = 1;
2554 }
2555
8b3ce757
ACM
2556 trace->syscalls.events.sys_enter = sys_enter;
2557 trace->syscalls.events.sys_exit = sys_exit;
c27366f0
ACM
2558
2559 ret = 0;
2560out:
2561 return ret;
2562
2563out_delete_sys_exit:
2564 perf_evsel__delete_priv(sys_exit);
2565out_delete_sys_enter:
2566 perf_evsel__delete_priv(sys_enter);
2567 goto out;
2568}
2569
246fbe03 2570static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
19867b61
ACM
2571{
2572 int err = -1;
b15d0a4c 2573 struct perf_evsel *sys_exit;
19867b61
ACM
2574 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2575 trace->ev_qualifier_ids.nr,
2576 trace->ev_qualifier_ids.entries);
2577
2578 if (filter == NULL)
2579 goto out_enomem;
2580
3541c034
MP
2581 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2582 filter)) {
b15d0a4c 2583 sys_exit = trace->syscalls.events.sys_exit;
3541c034 2584 err = perf_evsel__append_tp_filter(sys_exit, filter);
b15d0a4c 2585 }
19867b61
ACM
2586
2587 free(filter);
2588out:
2589 return err;
2590out_enomem:
2591 errno = ENOMEM;
2592 goto out;
2593}
c27366f0 2594
b27b38ed
ACM
2595#ifdef HAVE_LIBBPF_SUPPORT
2596static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2597{
2598 int fd = bpf_map__fd(trace->syscalls.map);
bbab50dd
ACM
2599 struct bpf_map_syscall_entry value = {
2600 .enabled = !trace->not_ev_qualifier,
2601 };
b27b38ed
ACM
2602 int err = 0;
2603 size_t i;
2604
2605 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2606 int key = trace->ev_qualifier_ids.entries[i];
2607
2608 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2609 if (err)
2610 break;
2611 }
2612
2613 return err;
2614}
2615
2616static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2617{
2618 int fd = bpf_map__fd(trace->syscalls.map);
bbab50dd
ACM
2619 struct bpf_map_syscall_entry value = {
2620 .enabled = enabled,
2621 };
b27b38ed
ACM
2622 int err = 0, key;
2623
2624 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
bbab50dd 2625 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
b27b38ed
ACM
2626 if (err)
2627 break;
2628 }
2629
2630 return err;
2631}
2632
2633static int trace__init_syscalls_bpf_map(struct trace *trace)
2634{
2635 bool enabled = true;
2636
2637 if (trace->ev_qualifier_ids.nr)
2638 enabled = trace->not_ev_qualifier;
2639
2640 return __trace__init_syscalls_bpf_map(trace, enabled);
2641}
2642#else
2643static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
2644{
2645 return 0;
2646}
2647
2648static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
2649{
2650 return 0;
2651}
2652#endif // HAVE_LIBBPF_SUPPORT
2653
246fbe03
ACM
2654static int trace__set_ev_qualifier_filter(struct trace *trace)
2655{
b27b38ed
ACM
2656 if (trace->syscalls.map)
2657 return trace__set_ev_qualifier_bpf_filter(trace);
246fbe03
ACM
2658 return trace__set_ev_qualifier_tp_filter(trace);
2659}
2660
a9964c43
ACM
2661static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2662 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2663{
2664 int err = 0;
2665#ifdef HAVE_LIBBPF_SUPPORT
2666 bool value = true;
2667 int map_fd = bpf_map__fd(map);
2668 size_t i;
2669
2670 for (i = 0; i < npids; ++i) {
2671 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2672 if (err)
2673 break;
2674 }
2675#endif
2676 return err;
2677}
2678
dd1a5037
ACM
2679static int trace__set_filter_loop_pids(struct trace *trace)
2680{
a9964c43 2681 unsigned int nr = 1, err;
dd1a5037
ACM
2682 pid_t pids[32] = {
2683 getpid(),
2684 };
082ab9a1
ACM
2685 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2686
2687 while (thread && nr < ARRAY_SIZE(pids)) {
2688 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2689
2690 if (parent == NULL)
2691 break;
2692
2693 if (!strcmp(thread__comm_str(parent), "sshd")) {
2694 pids[nr++] = parent->tid;
2695 break;
2696 }
2697 thread = parent;
2698 }
dd1a5037 2699
a9964c43
ACM
2700 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2701 if (!err && trace->filter_pids.map)
2702 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2703
2704 return err;
2705}
2706
2707static int trace__set_filter_pids(struct trace *trace)
2708{
2709 int err = 0;
2710 /*
2711 * Better not use !target__has_task() here because we need to cover the
2712 * case where no threads were specified in the command line, but a
2713 * workload was, and in that case we will fill in the thread_map when
2714 * we fork the workload in perf_evlist__prepare_workload.
2715 */
2716 if (trace->filter_pids.nr > 0) {
2717 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2718 trace->filter_pids.entries);
2719 if (!err && trace->filter_pids.map) {
2720 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2721 trace->filter_pids.entries);
2722 }
2723 } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2724 err = trace__set_filter_loop_pids(trace);
2725 }
2726
2727 return err;
dd1a5037
ACM
2728}
2729
7ba61524 2730static int __trace__deliver_event(struct trace *trace, union perf_event *event)
1f44b3e2
JO
2731{
2732 struct perf_evlist *evlist = trace->evlist;
2733 struct perf_sample sample;
2734 int err;
2735
2736 err = perf_evlist__parse_sample(evlist, event, &sample);
2737 if (err)
2738 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
2739 else
2740 trace__handle_event(trace, event, &sample);
2741
2742 return 0;
2743}
2744
a761a8d1 2745static int __trace__flush_events(struct trace *trace)
028713aa
JO
2746{
2747 u64 first = ordered_events__first_time(&trace->oe.data);
2748 u64 flush = trace->oe.last - NSEC_PER_SEC;
2749
2750 /* Is there some thing to flush.. */
2751 if (first && first < flush)
2752 return ordered_events__flush_time(&trace->oe.data, flush);
2753
2754 return 0;
2755}
2756
a761a8d1
ACM
2757static int trace__flush_events(struct trace *trace)
2758{
2759 return !trace->sort_events ? 0 : __trace__flush_events(trace);
2760}
2761
7ba61524 2762static int trace__deliver_event(struct trace *trace, union perf_event *event)
028713aa 2763{
028713aa
JO
2764 int err;
2765
a761a8d1
ACM
2766 if (!trace->sort_events)
2767 return __trace__deliver_event(trace, event);
2768
2769 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
028713aa
JO
2770 if (err && err != -1)
2771 return err;
2772
2773 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
2774 if (err)
2775 return err;
2776
7ba61524 2777 return trace__flush_events(trace);
028713aa
JO
2778}
2779
2780static int ordered_events__deliver_event(struct ordered_events *oe,
2781 struct ordered_event *event)
2782{
2783 struct trace *trace = container_of(oe, struct trace, oe.data);
2784
7ba61524 2785 return __trace__deliver_event(trace, event->event);
028713aa
JO
2786}
2787
f15eb531 2788static int trace__run(struct trace *trace, int argc, const char **argv)
514f1c67 2789{
14a052df 2790 struct perf_evlist *evlist = trace->evlist;
0ae537cb 2791 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
efd5745e
ACM
2792 int err = -1, i;
2793 unsigned long before;
f15eb531 2794 const bool forks = argc > 0;
46fb3c21 2795 bool draining = false;
514f1c67 2796
75b757ca
ACM
2797 trace->live = true;
2798
0df50e0b
ACM
2799 if (!trace->raw_augmented_syscalls) {
2800 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
2801 goto out_error_raw_syscalls;
514f1c67 2802
0df50e0b
ACM
2803 if (trace->trace_syscalls)
2804 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
2805 }
c522739d 2806
0ae537cb
ACM
2807 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2808 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2809 if (pgfault_maj == NULL)
2810 goto out_error_mem;
08e26396 2811 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
0ae537cb 2812 perf_evlist__add(evlist, pgfault_maj);
e2726d99 2813 }
598d02c5 2814
0ae537cb
ACM
2815 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2816 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2817 if (pgfault_min == NULL)
2818 goto out_error_mem;
08e26396 2819 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
0ae537cb
ACM
2820 perf_evlist__add(evlist, pgfault_min);
2821 }
598d02c5 2822
1302d88e 2823 if (trace->sched &&
2cc990ba
ACM
2824 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2825 trace__sched_stat_runtime))
2826 goto out_error_sched_stat_runtime;
1302d88e 2827
9ea42ba4
ACM
2828 /*
2829 * If a global cgroup was set, apply it to all the events without an
2830 * explicit cgroup. I.e.:
2831 *
2832 * trace -G A -e sched:*switch
2833 *
2834 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2835 * _and_ sched:sched_switch to the 'A' cgroup, while:
2836 *
2837 * trace -e sched:*switch -G A
2838 *
2839 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2840 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2841 * a cgroup (on the root cgroup, sys wide, etc).
2842 *
2843 * Multiple cgroups:
2844 *
2845 * trace -G A -e sched:*switch -G B
2846 *
2847 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2848 * to the 'B' cgroup.
2849 *
2850 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2851 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2852 */
2853 if (trace->cgroup)
2854 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2855
514f1c67
ACM
2856 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2857 if (err < 0) {
c24ff998 2858 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
514f1c67
ACM
2859 goto out_delete_evlist;
2860 }
2861
752fde44
ACM
2862 err = trace__symbols_init(trace, evlist);
2863 if (err < 0) {
c24ff998 2864 fprintf(trace->output, "Problems initializing symbol libraries!\n");
03ad9747 2865 goto out_delete_evlist;
752fde44
ACM
2866 }
2867
75d50117 2868 perf_evlist__config(evlist, &trace->opts, &callchain_param);
fde54b78 2869
f15eb531
NK
2870 signal(SIGCHLD, sig_handler);
2871 signal(SIGINT, sig_handler);
2872
2873 if (forks) {
6ef73ec4 2874 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
735f7e0b 2875 argv, false, NULL);
f15eb531 2876 if (err < 0) {
c24ff998 2877 fprintf(trace->output, "Couldn't run the workload!\n");
03ad9747 2878 goto out_delete_evlist;
f15eb531
NK
2879 }
2880 }
2881
514f1c67 2882 err = perf_evlist__open(evlist);
a8f23d8f
ACM
2883 if (err < 0)
2884 goto out_error_open;
514f1c67 2885
ba504235
WN
2886 err = bpf__apply_obj_config();
2887 if (err) {
2888 char errbuf[BUFSIZ];
2889
2890 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2891 pr_err("ERROR: Apply config to BPF failed: %s\n",
2892 errbuf);
2893 goto out_error_open;
2894 }
2895
a9964c43 2896 err = trace__set_filter_pids(trace);
94ad89bc
ACM
2897 if (err < 0)
2898 goto out_error_mem;
2899
b27b38ed
ACM
2900 if (trace->syscalls.map)
2901 trace__init_syscalls_bpf_map(trace);
2902
19867b61
ACM
2903 if (trace->ev_qualifier_ids.nr > 0) {
2904 err = trace__set_ev_qualifier_filter(trace);
2905 if (err < 0)
2906 goto out_errno;
19867b61 2907
0df50e0b
ACM
2908 if (trace->syscalls.events.sys_exit) {
2909 pr_debug("event qualifier tracepoint filter: %s\n",
2910 trace->syscalls.events.sys_exit->filter);
2911 }
2e5e5f87 2912 }
19867b61 2913
94ad89bc
ACM
2914 err = perf_evlist__apply_filters(evlist, &evsel);
2915 if (err < 0)
2916 goto out_error_apply_filters;
241b057c 2917
f74b9d3a 2918 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
e09b18d4
ACM
2919 if (err < 0)
2920 goto out_error_mmap;
514f1c67 2921
e36b7821 2922 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
cb24d01d
ACM
2923 perf_evlist__enable(evlist);
2924
f15eb531
NK
2925 if (forks)
2926 perf_evlist__start_workload(evlist);
2927
e36b7821
AB
2928 if (trace->opts.initial_delay) {
2929 usleep(trace->opts.initial_delay * 1000);
2930 perf_evlist__enable(evlist);
2931 }
2932
e13798c7 2933 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
42052bea
ACM
2934 evlist->threads->nr > 1 ||
2935 perf_evlist__first(evlist)->attr.inherit;
bd3dda9a
ACM
2936
2937 /*
2938 * Now that we already used evsel->attr to ask the kernel to setup the
2939 * events, lets reuse evsel->attr.sample_max_stack as the limit in
2940 * trace__resolve_callchain(), allowing per-event max-stack settings
adba1634 2941 * to override an explicitly set --max-stack global setting.
bd3dda9a
ACM
2942 */
2943 evlist__for_each_entry(evlist, evsel) {
27de9b2b 2944 if (evsel__has_callchain(evsel) &&
bd3dda9a
ACM
2945 evsel->attr.sample_max_stack == 0)
2946 evsel->attr.sample_max_stack = trace->max_stack;
2947 }
514f1c67 2948again:
efd5745e 2949 before = trace->nr_events;
514f1c67
ACM
2950
2951 for (i = 0; i < evlist->nr_mmaps; i++) {
2952 union perf_event *event;
d7f55c62 2953 struct perf_mmap *md;
514f1c67 2954
d7f55c62 2955 md = &evlist->mmap[i];
b9bae2c8 2956 if (perf_mmap__read_init(md) < 0)
d7f55c62
KL
2957 continue;
2958
0019dc87 2959 while ((event = perf_mmap__read_event(md)) != NULL) {
efd5745e 2960 ++trace->nr_events;
514f1c67 2961
7ba61524 2962 err = trace__deliver_event(trace, event);
028713aa
JO
2963 if (err)
2964 goto out_disable;
514f1c67 2965
d6ace3df 2966 perf_mmap__consume(md);
20c5f10e 2967
ba209f85
ACM
2968 if (interrupted)
2969 goto out_disable;
02ac5421
ACM
2970
2971 if (done && !draining) {
2972 perf_evlist__disable(evlist);
2973 draining = true;
2974 }
514f1c67 2975 }
d7f55c62 2976 perf_mmap__read_done(md);
514f1c67
ACM
2977 }
2978
efd5745e 2979 if (trace->nr_events == before) {
ba209f85 2980 int timeout = done ? 100 : -1;
f15eb531 2981
46fb3c21 2982 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
a9c5e6c1 2983 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
46fb3c21
ACM
2984 draining = true;
2985
ba209f85 2986 goto again;
028713aa 2987 } else {
7ba61524 2988 if (trace__flush_events(trace))
028713aa 2989 goto out_disable;
46fb3c21 2990 }
ba209f85
ACM
2991 } else {
2992 goto again;
f15eb531
NK
2993 }
2994
ba209f85 2995out_disable:
f3b623b8
ACM
2996 thread__zput(trace->current);
2997
ba209f85 2998 perf_evlist__disable(evlist);
514f1c67 2999
a761a8d1
ACM
3000 if (trace->sort_events)
3001 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
028713aa 3002
c522739d
ACM
3003 if (!err) {
3004 if (trace->summary)
3005 trace__fprintf_thread_summary(trace, trace->output);
3006
3007 if (trace->show_tool_stats) {
3008 fprintf(trace->output, "Stats:\n "
3009 " vfs_getname : %" PRIu64 "\n"
3010 " proc_getname: %" PRIu64 "\n",
3011 trace->stats.vfs_getname,
3012 trace->stats.proc_getname);
3013 }
3014 }
bf2575c1 3015
514f1c67 3016out_delete_evlist:
33974a41
AV
3017 trace__symbols__exit(trace);
3018
514f1c67 3019 perf_evlist__delete(evlist);
9ea42ba4 3020 cgroup__put(trace->cgroup);
14a052df 3021 trace->evlist = NULL;
75b757ca 3022 trace->live = false;
514f1c67 3023 return err;
6ef068cb
ACM
3024{
3025 char errbuf[BUFSIZ];
a8f23d8f 3026
2cc990ba 3027out_error_sched_stat_runtime:
988bdb31 3028 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
2cc990ba
ACM
3029 goto out_error;
3030
801c67b0 3031out_error_raw_syscalls:
988bdb31 3032 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
a8f23d8f
ACM
3033 goto out_error;
3034
e09b18d4
ACM
3035out_error_mmap:
3036 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3037 goto out_error;
3038
a8f23d8f
ACM
3039out_error_open:
3040 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3041
3042out_error:
6ef068cb 3043 fprintf(trace->output, "%s\n", errbuf);
87f91868 3044 goto out_delete_evlist;
94ad89bc
ACM
3045
3046out_error_apply_filters:
3047 fprintf(trace->output,
3048 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3049 evsel->filter, perf_evsel__name(evsel), errno,
c8b5f2c9 3050 str_error_r(errno, errbuf, sizeof(errbuf)));
94ad89bc 3051 goto out_delete_evlist;
514f1c67 3052}
5ed08dae
ACM
3053out_error_mem:
3054 fprintf(trace->output, "Not enough memory to run!\n");
3055 goto out_delete_evlist;
19867b61
ACM
3056
3057out_errno:
3058 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3059 goto out_delete_evlist;
a8f23d8f 3060}
514f1c67 3061
6810fc91
DA
3062static int trace__replay(struct trace *trace)
3063{
3064 const struct perf_evsel_str_handler handlers[] = {
c522739d 3065 { "probe:vfs_getname", trace__vfs_getname, },
6810fc91 3066 };
8ceb41d7 3067 struct perf_data data = {
eae8ad80
JO
3068 .file = {
3069 .path = input_name,
3070 },
3071 .mode = PERF_DATA_MODE_READ,
3072 .force = trace->force,
f5fc1412 3073 };
6810fc91 3074 struct perf_session *session;
003824e8 3075 struct perf_evsel *evsel;
6810fc91
DA
3076 int err = -1;
3077
3078 trace->tool.sample = trace__process_sample;
3079 trace->tool.mmap = perf_event__process_mmap;
384c671e 3080 trace->tool.mmap2 = perf_event__process_mmap2;
6810fc91
DA
3081 trace->tool.comm = perf_event__process_comm;
3082 trace->tool.exit = perf_event__process_exit;
3083 trace->tool.fork = perf_event__process_fork;
3084 trace->tool.attr = perf_event__process_attr;
f3b3614a 3085 trace->tool.tracing_data = perf_event__process_tracing_data;
6810fc91 3086 trace->tool.build_id = perf_event__process_build_id;
f3b3614a 3087 trace->tool.namespaces = perf_event__process_namespaces;
6810fc91 3088
0a8cb85c 3089 trace->tool.ordered_events = true;
6810fc91
DA
3090 trace->tool.ordering_requires_timestamps = true;
3091
3092 /* add tid to output */
3093 trace->multiple_threads = true;
3094
8ceb41d7 3095 session = perf_session__new(&data, false, &trace->tool);
6810fc91 3096 if (session == NULL)
52e02834 3097 return -1;
6810fc91 3098
aa07df6e
DA
3099 if (trace->opts.target.pid)
3100 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3101
3102 if (trace->opts.target.tid)
3103 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3104
0a7e6d1b 3105 if (symbol__init(&session->header.env) < 0)
cb2ffae2
NK
3106 goto out;
3107
8fb598e5
DA
3108 trace->host = &session->machines.host;
3109
6810fc91
DA
3110 err = perf_session__set_tracepoints_handlers(session, handlers);
3111 if (err)
3112 goto out;
3113
003824e8
NK
3114 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3115 "raw_syscalls:sys_enter");
9aca7f17
DA
3116 /* older kernels have syscalls tp versus raw_syscalls */
3117 if (evsel == NULL)
3118 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3119 "syscalls:sys_enter");
003824e8 3120
e281a960 3121 if (evsel &&
63f11c80 3122 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
e281a960 3123 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
003824e8
NK
3124 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3125 goto out;
3126 }
3127
3128 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3129 "raw_syscalls:sys_exit");
9aca7f17
DA
3130 if (evsel == NULL)
3131 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3132 "syscalls:sys_exit");
e281a960 3133 if (evsel &&
63f11c80 3134 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
e281a960 3135 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
003824e8 3136 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
6810fc91
DA
3137 goto out;
3138 }
3139
e5cadb93 3140 evlist__for_each_entry(session->evlist, evsel) {
1e28fe0a
SF
3141 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
3142 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3143 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3144 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3145 evsel->handler = trace__pgfault;
3146 }
3147
6810fc91
DA
3148 setup_pager();
3149
b7b61cbe 3150 err = perf_session__process_events(session);
6810fc91
DA
3151 if (err)
3152 pr_err("Failed to process events, error %d", err);
3153
bf2575c1
DA
3154 else if (trace->summary)
3155 trace__fprintf_thread_summary(trace, trace->output);
3156
6810fc91
DA
3157out:
3158 perf_session__delete(session);
3159
3160 return err;
3161}
3162
1302d88e
ACM
3163static size_t trace__fprintf_threads_header(FILE *fp)
3164{
3165 size_t printed;
3166
99ff7150 3167 printed = fprintf(fp, "\n Summary of events:\n\n");
bf2575c1
DA
3168
3169 return printed;
3170}
3171
b535d523
ACM
3172DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3173 struct stats *stats;
3174 double msecs;
3175 int syscall;
3176)
3177{
3178 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3179 struct stats *stats = source->priv;
3180
3181 entry->syscall = source->i;
3182 entry->stats = stats;
3183 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3184}
3185
bf2575c1
DA
3186static size_t thread__dump_stats(struct thread_trace *ttrace,
3187 struct trace *trace, FILE *fp)
3188{
bf2575c1
DA
3189 size_t printed = 0;
3190 struct syscall *sc;
b535d523
ACM
3191 struct rb_node *nd;
3192 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
bf2575c1 3193
b535d523 3194 if (syscall_stats == NULL)
bf2575c1
DA
3195 return 0;
3196
3197 printed += fprintf(fp, "\n");
3198
834fd46d
MW
3199 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3200 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3201 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
99ff7150 3202
98a91837 3203 resort_rb__for_each_entry(nd, syscall_stats) {
b535d523 3204 struct stats *stats = syscall_stats_entry->stats;
bf2575c1
DA
3205 if (stats) {
3206 double min = (double)(stats->min) / NSEC_PER_MSEC;
3207 double max = (double)(stats->max) / NSEC_PER_MSEC;
3208 double avg = avg_stats(stats);
3209 double pct;
3210 u64 n = (u64) stats->n;
3211
3212 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3213 avg /= NSEC_PER_MSEC;
3214
b535d523 3215 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
99ff7150 3216 printed += fprintf(fp, " %-15s", sc->name);
834fd46d 3217 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
b535d523 3218 n, syscall_stats_entry->msecs, min, avg);
27a778b5 3219 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
bf2575c1 3220 }
bf2575c1
DA
3221 }
3222
b535d523 3223 resort_rb__delete(syscall_stats);
bf2575c1 3224 printed += fprintf(fp, "\n\n");
1302d88e
ACM
3225
3226 return printed;
3227}
3228
96c14451 3229static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
896cbb56 3230{
96c14451 3231 size_t printed = 0;
89dceb22 3232 struct thread_trace *ttrace = thread__priv(thread);
896cbb56
DA
3233 double ratio;
3234
3235 if (ttrace == NULL)
3236 return 0;
3237
3238 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3239
15e65c69 3240 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
99ff7150 3241 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
15e65c69 3242 printed += fprintf(fp, "%.1f%%", ratio);
a2ea67d7
SF
3243 if (ttrace->pfmaj)
3244 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3245 if (ttrace->pfmin)
3246 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
03548ebf
ACM
3247 if (trace->sched)
3248 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3249 else if (fputc('\n', fp) != EOF)
3250 ++printed;
3251
bf2575c1 3252 printed += thread__dump_stats(ttrace, trace, fp);
896cbb56 3253
96c14451
ACM
3254 return printed;
3255}
896cbb56 3256
96c14451
ACM
3257static unsigned long thread__nr_events(struct thread_trace *ttrace)
3258{
3259 return ttrace ? ttrace->nr_events : 0;
3260}
3261
3262DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3263 struct thread *thread;
3264)
3265{
3266 entry->thread = rb_entry(nd, struct thread, rb_node);
896cbb56
DA
3267}
3268
1302d88e
ACM
3269static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3270{
96c14451
ACM
3271 size_t printed = trace__fprintf_threads_header(fp);
3272 struct rb_node *nd;
91e467bc 3273 int i;
1302d88e 3274
91e467bc
KL
3275 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3276 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
96c14451 3277
91e467bc
KL
3278 if (threads == NULL) {
3279 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3280 return 0;
3281 }
896cbb56 3282
91e467bc
KL
3283 resort_rb__for_each_entry(nd, threads)
3284 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
96c14451 3285
91e467bc
KL
3286 resort_rb__delete(threads);
3287 }
96c14451 3288 return printed;
1302d88e
ACM
3289}
3290
ae9ed035
ACM
3291static int trace__set_duration(const struct option *opt, const char *str,
3292 int unset __maybe_unused)
3293{
3294 struct trace *trace = opt->value;
3295
3296 trace->duration_filter = atof(str);
3297 return 0;
3298}
3299
6a0b3aba
ACM
3300static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3301 int unset __maybe_unused)
f078c385
ACM
3302{
3303 int ret = -1;
3304 size_t i;
3305 struct trace *trace = opt->value;
3306 /*
3307 * FIXME: introduce a intarray class, plain parse csv and create a
3308 * { int nr, int entries[] } struct...
3309 */
3310 struct intlist *list = intlist__new(str);
3311
3312 if (list == NULL)
3313 return -1;
3314
3315 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3316 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3317
3318 if (trace->filter_pids.entries == NULL)
3319 goto out;
3320
3321 trace->filter_pids.entries[0] = getpid();
3322
3323 for (i = 1; i < trace->filter_pids.nr; ++i)
3324 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3325
3326 intlist__delete(list);
3327 ret = 0;
3328out:
3329 return ret;
3330}
3331
c24ff998
ACM
3332static int trace__open_output(struct trace *trace, const char *filename)
3333{
3334 struct stat st;
3335
3336 if (!stat(filename, &st) && st.st_size) {
3337 char oldname[PATH_MAX];
3338
3339 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3340 unlink(oldname);
3341 rename(filename, oldname);
3342 }
3343
3344 trace->output = fopen(filename, "w");
3345
3346 return trace->output == NULL ? -errno : 0;
3347}
3348
598d02c5
SF
3349static int parse_pagefaults(const struct option *opt, const char *str,
3350 int unset __maybe_unused)
3351{
3352 int *trace_pgfaults = opt->value;
3353
3354 if (strcmp(str, "all") == 0)
3355 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3356 else if (strcmp(str, "maj") == 0)
3357 *trace_pgfaults |= TRACE_PFMAJ;
3358 else if (strcmp(str, "min") == 0)
3359 *trace_pgfaults |= TRACE_PFMIN;
3360 else
3361 return -1;
3362
3363 return 0;
3364}
3365
14a052df
ACM
3366static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3367{
3368 struct perf_evsel *evsel;
3369
e5cadb93 3370 evlist__for_each_entry(evlist, evsel)
14a052df
ACM
3371 evsel->handler = handler;
3372}
3373
d32855fa
ACM
3374static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3375{
3376 struct perf_evsel *evsel;
3377
3378 evlist__for_each_entry(evlist, evsel) {
3379 if (evsel->priv || !evsel->tp_format)
3380 continue;
3381
3382 if (strcmp(evsel->tp_format->system, "syscalls"))
3383 continue;
3384
3385 if (perf_evsel__init_syscall_tp(evsel))
3386 return -1;
3387
3388 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3389 struct syscall_tp *sc = evsel->priv;
3390
3391 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3392 return -1;
3393 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3394 struct syscall_tp *sc = evsel->priv;
3395
3396 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3397 return -1;
3398 }
3399 }
3400
3401 return 0;
3402}
3403
017037ff
ACM
3404/*
3405 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3406 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3407 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3408 *
3409 * It'd be better to introduce a parse_options() variant that would return a
3410 * list with the terms it didn't match to an event...
3411 */
3412static int trace__parse_events_option(const struct option *opt, const char *str,
3413 int unset __maybe_unused)
3414{
3415 struct trace *trace = (struct trace *)opt->value;
3416 const char *s = str;
3417 char *sep = NULL, *lists[2] = { NULL, NULL, };
27702bcf 3418 int len = strlen(str) + 1, err = -1, list, idx;
017037ff
ACM
3419 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3420 char group_name[PATH_MAX];
f932184e 3421 struct syscall_fmt *fmt;
017037ff
ACM
3422
3423 if (strace_groups_dir == NULL)
3424 return -1;
3425
3426 if (*s == '!') {
3427 ++s;
3428 trace->not_ev_qualifier = true;
3429 }
3430
3431 while (1) {
3432 if ((sep = strchr(s, ',')) != NULL)
3433 *sep = '\0';
3434
3435 list = 0;
27702bcf
ACM
3436 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3437 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
017037ff 3438 list = 1;
f932184e
ACM
3439 goto do_concat;
3440 }
3441
3442 fmt = syscall_fmt__find_by_alias(s);
3443 if (fmt != NULL) {
3444 list = 1;
3445 s = fmt->name;
017037ff
ACM
3446 } else {
3447 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3448 if (access(group_name, R_OK) == 0)
3449 list = 1;
3450 }
f932184e 3451do_concat:
017037ff
ACM
3452 if (lists[list]) {
3453 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3454 } else {
3455 lists[list] = malloc(len);
3456 if (lists[list] == NULL)
3457 goto out;
3458 strcpy(lists[list], s);
3459 }
3460
3461 if (!sep)
3462 break;
3463
3464 *sep = ',';
3465 s = sep + 1;
3466 }
3467
3468 if (lists[1] != NULL) {
3469 struct strlist_config slist_config = {
3470 .dirname = strace_groups_dir,
3471 };
3472
3473 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3474 if (trace->ev_qualifier == NULL) {
3475 fputs("Not enough memory to parse event qualifier", trace->output);
3476 goto out;
3477 }
3478
3479 if (trace__validate_ev_qualifier(trace))
3480 goto out;
b912885a 3481 trace->trace_syscalls = true;
017037ff
ACM
3482 }
3483
3484 err = 0;
3485
3486 if (lists[0]) {
3487 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3488 "event selector. use 'perf list' to list available events",
3489 parse_events_option);
3490 err = parse_events_option(&o, lists[0], 0);
3491 }
3492out:
3493 if (sep)
3494 *sep = ',';
3495
3496 return err;
3497}
3498
9ea42ba4
ACM
3499static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3500{
3501 struct trace *trace = opt->value;
3502
3503 if (!list_empty(&trace->evlist->entries))
3504 return parse_cgroups(opt, str, unset);
3505
3506 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3507
3508 return 0;
3509}
3510
744fafc7
ACM
3511static struct bpf_map *bpf__find_map_by_name(const char *name)
3512{
3513 struct bpf_object *obj, *tmp;
3514
3515 bpf_object__for_each_safe(obj, tmp) {
3516 struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3517 if (map)
3518 return map;
3519
3520 }
3521
3522 return NULL;
3523}
3524
3525static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3526{
3527 trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3528}
3529
b27b38ed
ACM
3530static void trace__set_bpf_map_syscalls(struct trace *trace)
3531{
3532 trace->syscalls.map = bpf__find_map_by_name("syscalls");
3533}
3534
ac96287c
ACM
3535static int trace__config(const char *var, const char *value, void *arg)
3536{
e7c634fc 3537 struct trace *trace = arg;
ac96287c
ACM
3538 int err = 0;
3539
3540 if (!strcmp(var, "trace.add_events")) {
ac96287c
ACM
3541 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3542 "event selector. use 'perf list' to list available events",
3543 parse_events_option);
3544 err = parse_events_option(&o, value, 0);
b036146f
ACM
3545 } else if (!strcmp(var, "trace.show_timestamp")) {
3546 trace->show_tstamp = perf_config_bool(var, value);
42e4a52d
ACM
3547 } else if (!strcmp(var, "trace.show_duration")) {
3548 trace->show_duration = perf_config_bool(var, value);
e7c634fc
ACM
3549 } else if (!strcmp(var, "trace.show_zeros")) {
3550 trace->show_zeros = perf_config_bool(var, value);
d32de87e
ACM
3551 } else if (!strcmp(var, "trace.no_inherit")) {
3552 trace->opts.no_inherit = perf_config_bool(var, value);
ac96287c
ACM
3553 }
3554
3555 return err;
3556}
3557
b0ad8ea6 3558int cmd_trace(int argc, const char **argv)
514f1c67 3559{
6fdd9cb7 3560 const char *trace_usage[] = {
f15eb531
NK
3561 "perf trace [<options>] [<command>]",
3562 "perf trace [<options>] -- <command> [<options>]",
5e2485b1
DA
3563 "perf trace record [<options>] [<command>]",
3564 "perf trace record [<options>] -- <command> [<options>]",
514f1c67
ACM
3565 NULL
3566 };
3567 struct trace trace = {
514f1c67
ACM
3568 .syscalls = {
3569 . max = -1,
3570 },
3571 .opts = {
3572 .target = {
3573 .uid = UINT_MAX,
3574 .uses_mmap = true,
3575 },
3576 .user_freq = UINT_MAX,
3577 .user_interval = ULLONG_MAX,
509051ea 3578 .no_buffering = true,
38d5447d 3579 .mmap_pages = UINT_MAX,
514f1c67 3580 },
007d66a0 3581 .output = stderr,
50c95cbd 3582 .show_comm = true,
b036146f 3583 .show_tstamp = true,
42e4a52d 3584 .show_duration = true,
b912885a 3585 .trace_syscalls = false,
44621819 3586 .kernel_syscallchains = false,
05614993 3587 .max_stack = UINT_MAX,
5067a8cd 3588 .max_events = ULONG_MAX,
514f1c67 3589 };
c24ff998 3590 const char *output_name = NULL;
514f1c67 3591 const struct option trace_options[] = {
017037ff
ACM
3592 OPT_CALLBACK('e', "event", &trace, "event",
3593 "event/syscall selector. use 'perf list' to list available events",
3594 trace__parse_events_option),
50c95cbd
ACM
3595 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3596 "show the thread COMM next to its id"),
c522739d 3597 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
017037ff
ACM
3598 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3599 trace__parse_events_option),
c24ff998 3600 OPT_STRING('o', "output", &output_name, "file", "output file name"),
6810fc91 3601 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
514f1c67
ACM
3602 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3603 "trace events on existing process id"),
ac9be8ee 3604 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
514f1c67 3605 "trace events on existing thread id"),
fa0e4ffe 3606 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
6a0b3aba 3607 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
ac9be8ee 3608 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
514f1c67 3609 "system-wide collection from all CPUs"),
ac9be8ee 3610 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
514f1c67 3611 "list of cpus to monitor"),
6810fc91 3612 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
514f1c67 3613 "child tasks do not inherit counters"),
994a1f78
JO
3614 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3615 "number of mmap data pages",
3616 perf_evlist__parse_mmap_pages),
ac9be8ee 3617 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
514f1c67 3618 "user to profile"),
ae9ed035
ACM
3619 OPT_CALLBACK(0, "duration", &trace, "float",
3620 "show only events with duration > N.M ms",
3621 trace__set_duration),
1302d88e 3622 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
7c304ee0 3623 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
4bb09192
DA
3624 OPT_BOOLEAN('T', "time", &trace.full_time,
3625 "Show full timestamp, not time relative to first start"),
0a6545bd
ACM
3626 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3627 "Show only syscalls that failed"),
fd2eabaf
DA
3628 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3629 "Show only syscall summary with statistics"),
3630 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3631 "Show all syscalls and summary with statistics"),
598d02c5
SF
3632 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3633 "Trace pagefaults", parse_pagefaults, "maj"),
e281a960 3634 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
e366a6d8 3635 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
566a0885
MW
3636 OPT_CALLBACK(0, "call-graph", &trace.opts,
3637 "record_mode[,record_size]", record_callchain_help,
3638 &record_parse_callchain_opt),
44621819
ACM
3639 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3640 "Show the kernel callchains on the syscall exit path"),
5067a8cd
ACM
3641 OPT_ULONG(0, "max-events", &trace.max_events,
3642 "Set the maximum number of events to print, exit after that is reached. "),
5cf9c84e
ACM
3643 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3644 "Set the minimum stack depth when parsing the callchain, "
3645 "anything below the specified depth will be ignored."),
c6d4a494
ACM
3646 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3647 "Set the maximum stack depth when parsing the callchain, "
3648 "anything beyond the specified depth will be ignored. "
4cb93446 3649 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
a761a8d1
ACM
3650 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
3651 "Sort batch of events before processing, use if getting out of order events"),
591421e1
ACM
3652 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3653 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
3fcb10e4 3654 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
9d9cad76 3655 "per thread proc mmap processing timeout in ms"),
9ea42ba4
ACM
3656 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3657 trace__parse_cgroups),
e36b7821
AB
3658 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3659 "ms to wait before starting measurement after program "
3660 "start"),
514f1c67
ACM
3661 OPT_END()
3662 };
ccd62a89 3663 bool __maybe_unused max_stack_user_set = true;
f3e459d1 3664 bool mmap_pages_user_set = true;
78e890ea 3665 struct perf_evsel *evsel;
6fdd9cb7 3666 const char * const trace_subcommands[] = { "record", NULL };
78e890ea 3667 int err = -1;
32caf0d1 3668 char bf[BUFSIZ];
514f1c67 3669
4d08cb80
ACM
3670 signal(SIGSEGV, sighandler_dump_stack);
3671 signal(SIGFPE, sighandler_dump_stack);
3672
14a052df 3673 trace.evlist = perf_evlist__new();
fd0db102 3674 trace.sctbl = syscalltbl__new();
14a052df 3675
fd0db102 3676 if (trace.evlist == NULL || trace.sctbl == NULL) {
14a052df 3677 pr_err("Not enough memory to run!\n");
ff8f695c 3678 err = -ENOMEM;
14a052df
ACM
3679 goto out;
3680 }
3681
ac96287c
ACM
3682 err = perf_config(trace__config, &trace);
3683 if (err)
3684 goto out;
3685
6fdd9cb7
YS
3686 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3687 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
fd2eabaf 3688
9ea42ba4
ACM
3689 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3690 usage_with_options_msg(trace_usage, trace_options,
3691 "cgroup monitoring only available in system-wide mode");
3692 }
3693
78e890ea
ACM
3694 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3695 if (IS_ERR(evsel)) {
3696 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
e0b6d2ef
ACM
3697 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3698 goto out;
3699 }
3700
744fafc7 3701 if (evsel) {
d3d1c4bd 3702 trace.syscalls.events.augmented = evsel;
744fafc7 3703 trace__set_bpf_map_filtered_pids(&trace);
b27b38ed 3704 trace__set_bpf_map_syscalls(&trace);
744fafc7 3705 }
d3d1c4bd 3706
d7888573
WN
3707 err = bpf__setup_stdout(trace.evlist);
3708 if (err) {
3709 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3710 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3711 goto out;
3712 }
3713
59247e33
ACM
3714 err = -1;
3715
598d02c5
SF
3716 if (trace.trace_pgfaults) {
3717 trace.opts.sample_address = true;
3718 trace.opts.sample_time = true;
3719 }
3720
f3e459d1
ACM
3721 if (trace.opts.mmap_pages == UINT_MAX)
3722 mmap_pages_user_set = false;
3723
05614993 3724 if (trace.max_stack == UINT_MAX) {
029c75e5 3725 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
05614993
ACM
3726 max_stack_user_set = false;
3727 }
3728
3729#ifdef HAVE_DWARF_UNWIND_SUPPORT
75d50117 3730 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
05614993 3731 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
75d50117 3732 }
05614993
ACM
3733#endif
3734
2ddd5c04 3735 if (callchain_param.enabled) {
f3e459d1
ACM
3736 if (!mmap_pages_user_set && geteuid() == 0)
3737 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3738
566a0885 3739 symbol_conf.use_callchain = true;
f3e459d1 3740 }
566a0885 3741
d32855fa 3742 if (trace.evlist->nr_entries > 0) {
14a052df 3743 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
d32855fa
ACM
3744 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3745 perror("failed to set syscalls:* tracepoint fields");
3746 goto out;
3747 }
3748 }
14a052df 3749
a761a8d1
ACM
3750 if (trace.sort_events) {
3751 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
3752 ordered_events__set_copy_on_queue(&trace.oe.data, true);
3753 }
028713aa 3754
b1a9e253
ACM
3755 /*
3756 * If we are augmenting syscalls, then combine what we put in the
3757 * __augmented_syscalls__ BPF map with what is in the
3758 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3759 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3760 *
3761 * We'll switch to look at two BPF maps, one for sys_enter and the
3762 * other for sys_exit when we start augmenting the sys_exit paths with
3763 * buffers that are being copied from kernel to userspace, think 'read'
3764 * syscall.
3765 */
3766 if (trace.syscalls.events.augmented) {
3767 evsel = trace.syscalls.events.augmented;
3768
3769 if (perf_evsel__init_augmented_syscall_tp(evsel) ||
3770 perf_evsel__init_augmented_syscall_tp_args(evsel))
3771 goto out;
3772 evsel->handler = trace__sys_enter;
3773
3774 evlist__for_each_entry(trace.evlist, evsel) {
cd26ea6d
ACM
3775 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3776
3777 if (raw_syscalls_sys_exit) {
3778 trace.raw_augmented_syscalls = true;
3779 goto init_augmented_syscall_tp;
3780 }
3781
3782 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
3783init_augmented_syscall_tp:
b1a9e253
ACM
3784 perf_evsel__init_augmented_syscall_tp(evsel);
3785 perf_evsel__init_augmented_syscall_tp_ret(evsel);
3786 evsel->handler = trace__sys_exit;
3787 }
3788 }
3789 }
3790
1e28fe0a
SF
3791 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3792 return trace__record(&trace, argc-1, &argv[1]);
3793
3794 /* summary_only implies summary option, but don't overwrite summary if set */
3795 if (trace.summary_only)
3796 trace.summary = trace.summary_only;
3797
726f3234
ACM
3798 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3799 trace.evlist->nr_entries == 0 /* Was --events used? */) {
b912885a 3800 trace.trace_syscalls = true;
59247e33
ACM
3801 }
3802
c24ff998
ACM
3803 if (output_name != NULL) {
3804 err = trace__open_output(&trace, output_name);
3805 if (err < 0) {
3806 perror("failed to create output file");
3807 goto out;
3808 }
3809 }
3810
602ad878 3811 err = target__validate(&trace.opts.target);
32caf0d1 3812 if (err) {
602ad878 3813 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
c24ff998
ACM
3814 fprintf(trace.output, "%s", bf);
3815 goto out_close;
32caf0d1
NK
3816 }
3817
602ad878 3818 err = target__parse_uid(&trace.opts.target);
514f1c67 3819 if (err) {
602ad878 3820 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
c24ff998
ACM
3821 fprintf(trace.output, "%s", bf);
3822 goto out_close;
514f1c67
ACM
3823 }
3824
602ad878 3825 if (!argc && target__none(&trace.opts.target))
ee76120e
NK
3826 trace.opts.target.system_wide = true;
3827
6810fc91
DA
3828 if (input_name)
3829 err = trace__replay(&trace);
3830 else
3831 err = trace__run(&trace, argc, argv);
1302d88e 3832
c24ff998
ACM
3833out_close:
3834 if (output_name != NULL)
3835 fclose(trace.output);
3836out:
1302d88e 3837 return err;
514f1c67 3838}