blob: b1ec8ff52740421483ee7341d65e2895f32165c7 [file] [log] [blame]
Arnaldo Carvalho de Meloa598bb52015-08-28 12:02:37 -03001/*
2 * builtin-trace.c
3 *
4 * Builtin 'trace' command:
5 *
6 * Display a continuously updated trace of any workload, CPU, specific PID,
7 * system wide, etc. Default format is loosely strace like, but any other
8 * event may be specified using --event.
9 *
10 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
11 *
12 * Initially based on the 'trace' prototype by Thomas Gleixner:
13 *
14 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
Arnaldo Carvalho de Meloa598bb52015-08-28 12:02:37 -030015 */
16
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030017#include "util/record.h"
Robert Richter4e319022013-06-11 17:29:18 +020018#include <traceevent/event-parse.h>
Jiri Olsa988bdb32015-09-02 09:56:35 +020019#include <api/fs/tracing_path.h>
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -030020#include <bpf/bpf.h>
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -030021#include "util/bpf_map.h"
Arnaldo Carvalho de Meloc3e78a32019-07-09 16:36:45 -030022#include "util/rlimit.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030023#include "builtin.h"
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030024#include "util/cgroup.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030025#include "util/color.h"
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -030026#include "util/config.h"
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -030027#include "util/debug.h"
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +010028#include "util/env.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030029#include "util/event.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030030#include "util/evlist.h"
Arnaldo Carvalho de Melo22ac4312019-08-15 12:15:39 -030031#include "util/evswitch.h"
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030032#include <subcmd/pager.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060033#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030034#include "util/machine.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010035#include "util/map.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010036#include "util/symbol.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030037#include "util/path.h"
David Ahern6810fc92013-08-28 22:29:52 -060038#include "util/session.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030039#include "util/thread.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060040#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -030041#include "util/strlist.h"
David Ahernbdc89662013-08-28 22:29:53 -060042#include "util/intlist.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030043#include "util/thread_map.h"
David Ahernbf2575c2013-10-08 21:26:53 -060044#include "util/stat.h"
Arnaldo Carvalho de Melo2da39f12019-08-27 11:51:18 -030045#include "util/util.h"
Arnaldo Carvalho de Melofd5cead2017-03-14 16:19:30 -030046#include "trace/beauty/beauty.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010047#include "trace-event.h"
David Ahern9aca7f12013-12-04 19:41:39 -070048#include "util/parse-events.h"
Wang Nanba504232016-02-26 09:31:54 +000049#include "util/bpf-loader.h"
Milian Wolff566a0882016-04-08 13:34:15 +020050#include "callchain.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030051#include "print_binary.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030052#include "string2.h"
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030053#include "syscalltbl.h"
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -030054#include "rb_resort.h"
Arnaldo Carvalho de Meloc1a604d2019-08-29 15:20:59 -030055#include "../perf.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030056
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030057#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030058#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030059#include <poll.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030060#include <signal.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030061#include <stdlib.h>
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -030062#include <string.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020063#include <linux/err.h>
Arnaldo Carvalho de Melo997bba82016-03-30 19:43:32 -030064#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030065#include <linux/kernel.h>
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -030066#include <linux/random.h>
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -030067#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030068#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030069#include <linux/zalloc.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030070#include <fcntl.h>
Arnaldo Carvalho de Melo4bcc4cf2018-12-27 13:32:59 -030071#include <sys/sysmacros.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030072
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030073#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030074
Arnaldo Carvalho de Meloc188e7a2015-05-14 17:39:03 -030075#ifndef O_CLOEXEC
76# define O_CLOEXEC 02000000
77#endif
78
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -030079#ifndef F_LINUX_SPECIFIC_BASE
80# define F_LINUX_SPECIFIC_BASE 1024
81#endif
82
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030083struct trace {
84 struct perf_tool tool;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030085 struct syscalltbl *sctbl;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030086 struct {
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030087 struct syscall *table;
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -030088 struct bpf_map *map;
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -030089 struct { // per syscall BPF_MAP_TYPE_PROG_ARRAY
90 struct bpf_map *sys_enter,
91 *sys_exit;
92 } prog_array;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030093 struct {
Jiri Olsa32dcd022019-07-21 13:23:51 +020094 struct evsel *sys_enter,
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -030095 *sys_exit,
96 *augmented;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030097 } events;
Arnaldo Carvalho de Melo5834da72019-07-15 17:51:43 -030098 struct bpf_program *unaugmented_prog;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030099 } syscalls;
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -0300100 struct {
101 struct bpf_map *map;
102 } dump;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300103 struct record_opts opts;
Jiri Olsa63503db2019-07-21 13:23:52 +0200104 struct evlist *evlist;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300105 struct machine *host;
106 struct thread *current;
Arnaldo Carvalho de Meloc8c80572019-07-15 16:58:23 -0300107 struct bpf_object *bpf_obj;
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -0300108 struct cgroup *cgroup;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300109 u64 base_time;
110 FILE *output;
111 unsigned long nr_events;
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -0300112 unsigned long nr_events_printed;
113 unsigned long max_events;
Arnaldo Carvalho de Melo22ac4312019-08-15 12:15:39 -0300114 struct evswitch evswitch;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300115 struct strlist *ev_qualifier;
116 struct {
117 size_t nr;
118 int *entries;
119 } ev_qualifier_ids;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300120 struct {
121 size_t nr;
122 pid_t *entries;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -0300123 struct bpf_map *map;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300124 } filter_pids;
125 double duration_filter;
126 double runtime_ms;
127 struct {
128 u64 vfs_getname,
129 proc_getname;
130 } stats;
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -0300131 unsigned int max_stack;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -0300132 unsigned int min_stack;
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300133 int raw_augmented_syscalls_args_size;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -0300134 bool raw_augmented_syscalls;
Arnaldo Carvalho de Melo79d725c2019-07-16 16:56:49 -0300135 bool fd_path_disabled;
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300136 bool sort_events;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300137 bool not_ev_qualifier;
138 bool live;
139 bool full_time;
140 bool sched;
141 bool multiple_threads;
142 bool summary;
143 bool summary_only;
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -0300144 bool failure_only;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300145 bool show_comm;
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -0300146 bool print_sample;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300147 bool show_tool_stats;
148 bool trace_syscalls;
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -0300149 bool kernel_syscallchains;
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -0300150 s16 args_alignment;
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -0300151 bool show_tstamp;
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -0300152 bool show_duration;
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -0300153 bool show_zeros;
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -0300154 bool show_arg_names;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300155 bool show_string_prefix;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300156 bool force;
157 bool vfs_getname;
158 int trace_pgfaults;
Jiri Olsa028713a2018-12-05 17:05:09 +0100159 struct {
160 struct ordered_events data;
161 u64 last;
162 } oe;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300163};
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300164
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300165struct tp_field {
166 int offset;
167 union {
168 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
169 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
170 };
171};
172
173#define TP_UINT_FIELD(bits) \
174static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
175{ \
David Ahern55d43bca2015-02-19 15:00:22 -0500176 u##bits value; \
177 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
178 return value; \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300179}
180
181TP_UINT_FIELD(8);
182TP_UINT_FIELD(16);
183TP_UINT_FIELD(32);
184TP_UINT_FIELD(64);
185
186#define TP_UINT_FIELD__SWAPPED(bits) \
187static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
188{ \
David Ahern55d43bca2015-02-19 15:00:22 -0500189 u##bits value; \
190 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300191 return bswap_##bits(value);\
192}
193
194TP_UINT_FIELD__SWAPPED(16);
195TP_UINT_FIELD__SWAPPED(32);
196TP_UINT_FIELD__SWAPPED(64);
197
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300198static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300199{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300200 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300201
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300202 switch (size) {
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300203 case 1:
204 field->integer = tp_field__u8;
205 break;
206 case 2:
207 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
208 break;
209 case 4:
210 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
211 break;
212 case 8:
213 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
214 break;
215 default:
216 return -1;
217 }
218
219 return 0;
220}
221
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400222static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap)
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300223{
224 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
225}
226
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300227static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
228{
229 return sample->raw_data + field->offset;
230}
231
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300232static int __tp_field__init_ptr(struct tp_field *field, int offset)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300233{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300234 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300235 field->pointer = tp_field__ptr;
236 return 0;
237}
238
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400239static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300240{
241 return __tp_field__init_ptr(field, format_field->offset);
242}
243
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300244struct syscall_tp {
245 struct tp_field id;
246 union {
247 struct tp_field args, ret;
248 };
249};
250
Jiri Olsa32dcd022019-07-21 13:23:51 +0200251static int perf_evsel__init_tp_uint_field(struct evsel *evsel,
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300252 struct tp_field *field,
253 const char *name)
254{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400255 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300256
257 if (format_field == NULL)
258 return -1;
259
260 return tp_field__init_uint(field, format_field, evsel->needs_swap);
261}
262
263#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
264 ({ struct syscall_tp *sc = evsel->priv;\
265 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
266
Jiri Olsa32dcd022019-07-21 13:23:51 +0200267static int perf_evsel__init_tp_ptr_field(struct evsel *evsel,
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300268 struct tp_field *field,
269 const char *name)
270{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400271 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300272
273 if (format_field == NULL)
274 return -1;
275
276 return tp_field__init_ptr(field, format_field);
277}
278
279#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
280 ({ struct syscall_tp *sc = evsel->priv;\
281 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
282
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200283static void evsel__delete_priv(struct evsel *evsel)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300284{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300285 zfree(&evsel->priv);
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200286 evsel__delete(evsel);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300287}
288
Jiri Olsa32dcd022019-07-21 13:23:51 +0200289static int perf_evsel__init_syscall_tp(struct evsel *evsel)
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300290{
291 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
292
293 if (evsel->priv != NULL) {
Arnaldo Carvalho de Melo42da4382018-12-05 13:08:11 -0300294 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
295 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300296 goto out_delete;
297 return 0;
298 }
299
300 return -ENOMEM;
301out_delete:
302 zfree(&evsel->priv);
303 return -ENOENT;
304}
305
Jiri Olsa32dcd022019-07-21 13:23:51 +0200306static int perf_evsel__init_augmented_syscall_tp(struct evsel *evsel, struct evsel *tp)
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300307{
308 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
309
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300310 if (evsel->priv != NULL) {
311 struct tep_format_field *syscall_id = perf_evsel__field(tp, "id");
312 if (syscall_id == NULL)
313 syscall_id = perf_evsel__field(tp, "__syscall_nr");
314 if (syscall_id == NULL)
315 goto out_delete;
316 if (__tp_field__init_uint(&sc->id, syscall_id->size, syscall_id->offset, evsel->needs_swap))
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300317 goto out_delete;
318
319 return 0;
320 }
321
322 return -ENOMEM;
323out_delete:
324 zfree(&evsel->priv);
325 return -EINVAL;
326}
327
Jiri Olsa32dcd022019-07-21 13:23:51 +0200328static int perf_evsel__init_augmented_syscall_tp_args(struct evsel *evsel)
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300329{
330 struct syscall_tp *sc = evsel->priv;
331
332 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
333}
334
Jiri Olsa32dcd022019-07-21 13:23:51 +0200335static int perf_evsel__init_augmented_syscall_tp_ret(struct evsel *evsel)
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -0300336{
337 struct syscall_tp *sc = evsel->priv;
338
339 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
340}
341
Jiri Olsa32dcd022019-07-21 13:23:51 +0200342static int perf_evsel__init_raw_syscall_tp(struct evsel *evsel, void *handler)
Namhyung Kim96695d42013-11-12 08:51:45 -0300343{
344 evsel->priv = malloc(sizeof(struct syscall_tp));
345 if (evsel->priv != NULL) {
346 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
347 goto out_delete;
348
349 evsel->handler = handler;
350 return 0;
351 }
352
353 return -ENOMEM;
354
355out_delete:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300356 zfree(&evsel->priv);
Namhyung Kim96695d42013-11-12 08:51:45 -0300357 return -ENOENT;
358}
359
Jiri Olsa32dcd022019-07-21 13:23:51 +0200360static struct evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300361{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200362 struct evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300363
David Ahern9aca7f12013-12-04 19:41:39 -0700364 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200365 if (IS_ERR(evsel))
David Ahern9aca7f12013-12-04 19:41:39 -0700366 evsel = perf_evsel__newtp("syscalls", direction);
367
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200368 if (IS_ERR(evsel))
369 return NULL;
370
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300371 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200372 goto out_delete;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300373
374 return evsel;
375
376out_delete:
Jiri Olsa5eb2dd22019-07-21 13:23:57 +0200377 evsel__delete_priv(evsel);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300378 return NULL;
379}
380
381#define perf_evsel__sc_tp_uint(evsel, name, sample) \
382 ({ struct syscall_tp *fields = evsel->priv; \
383 fields->name.integer(&fields->name, sample); })
384
385#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
386 ({ struct syscall_tp *fields = evsel->priv; \
387 fields->name.pointer(&fields->name, sample); })
388
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300389size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300390{
391 int idx = val - sa->offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300392
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300393 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
394 size_t printed = scnprintf(bf, size, intfmt, val);
395 if (show_prefix)
396 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
397 return printed;
398 }
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300399
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300400 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300401}
402
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300403static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
404 const char *intfmt,
405 struct syscall_arg *arg)
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300406{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300407 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300408}
409
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300410static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
411 struct syscall_arg *arg)
412{
413 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
414}
415
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300416#define SCA_STRARRAY syscall_arg__scnprintf_strarray
417
Arnaldo Carvalho de Melof5b91db2019-05-23 18:05:03 -0300418size_t syscall_arg__scnprintf_strarray_flags(char *bf, size_t size, struct syscall_arg *arg)
419{
420 return strarray__scnprintf_flags(arg->parm, bf, size, arg->show_string_prefix, arg->val);
421}
422
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300423size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
424{
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300425 size_t printed;
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300426 int i;
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300427
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300428 for (i = 0; i < sas->nr_entries; ++i) {
429 struct strarray *sa = sas->entries[i];
430 int idx = val - sa->offset;
431
432 if (idx >= 0 && idx < sa->nr_entries) {
433 if (sa->entries[idx] == NULL)
434 break;
435 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
436 }
437 }
438
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300439 printed = scnprintf(bf, size, intfmt, val);
440 if (show_prefix)
441 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
442 return printed;
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300443}
444
Arnaldo Carvalho de Melo274e86f2017-07-14 09:38:38 -0300445size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
446 struct syscall_arg *arg)
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300447{
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300448 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300449}
450
Arnaldo Carvalho de Melo48e1f912016-07-05 14:43:27 -0300451#ifndef AT_FDCWD
452#define AT_FDCWD -100
453#endif
454
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300455static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
456 struct syscall_arg *arg)
457{
458 int fd = arg->val;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300459 const char *prefix = "AT_FD";
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300460
461 if (fd == AT_FDCWD)
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300462 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300463
464 return syscall_arg__scnprintf_fd(bf, size, arg);
465}
466
467#define SCA_FDAT syscall_arg__scnprintf_fd_at
468
469static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
470 struct syscall_arg *arg);
471
472#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
473
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300474size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300475{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300476 return scnprintf(bf, size, "%#lx", arg->val);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300477}
478
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300479size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
480{
481 if (arg->val == 0)
482 return scnprintf(bf, size, "NULL");
483 return syscall_arg__scnprintf_hex(bf, size, arg);
484}
485
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300486size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300487{
488 return scnprintf(bf, size, "%d", arg->val);
489}
490
Arnaldo Carvalho de Melo5dde91e2017-07-14 10:34:16 -0300491size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
492{
493 return scnprintf(bf, size, "%ld", arg->val);
494}
495
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300496static const char *bpf_cmd[] = {
497 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
498 "MAP_GET_NEXT_KEY", "PROG_LOAD",
499};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300500static DEFINE_STRARRAY(bpf_cmd, "BPF_");
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300501
Arnaldo Carvalho de Melof6af0952019-05-21 16:34:03 -0300502static const char *fsmount_flags[] = {
503 [1] = "CLOEXEC",
504};
505static DEFINE_STRARRAY(fsmount_flags, "FSMOUNT_");
506
Arnaldo Carvalho de Melodcc6fd62019-05-21 15:36:44 -0300507#include "trace/beauty/generated/fsconfig_arrays.c"
508
509static DEFINE_STRARRAY(fsconfig_cmds, "FSCONFIG_");
510
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300511static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300512static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
Arnaldo Carvalho de Meloeac032c2013-09-20 11:27:32 -0300513
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300514static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300515static DEFINE_STRARRAY(itimers, "ITIMER_");
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300516
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300517static const char *keyctl_options[] = {
518 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
519 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
520 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
521 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
522 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
523};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300524static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300525
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300526static const char *whences[] = { "SET", "CUR", "END",
527#ifdef SEEK_DATA
528"DATA",
529#endif
530#ifdef SEEK_HOLE
531"HOLE",
532#endif
533};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300534static DEFINE_STRARRAY(whences, "SEEK_");
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300535
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300536static const char *fcntl_cmds[] = {
537 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
Arnaldo Carvalho de Meloe000e5e2017-07-13 13:07:00 -0300538 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
539 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
540 "GETOWNER_UIDS",
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300541};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300542static DEFINE_STRARRAY(fcntl_cmds, "F_");
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300543
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300544static const char *fcntl_linux_specific_cmds[] = {
545 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
546 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
Arnaldo Carvalho de Melo64e45612017-07-13 17:34:46 -0300547 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300548};
549
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300550static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300551
552static struct strarray *fcntl_cmds_arrays[] = {
553 &strarray__fcntl_cmds,
554 &strarray__fcntl_linux_specific_cmds,
555};
556
557static DEFINE_STRARRAYS(fcntl_cmds_arrays);
558
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300559static const char *rlimit_resources[] = {
560 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
561 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
562 "RTTIME",
563};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300564static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300565
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300566static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300567static DEFINE_STRARRAY(sighow, "SIG_");
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300568
David Ahern4f8c1b72013-09-22 19:45:00 -0600569static const char *clockid[] = {
570 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
Arnaldo Carvalho de Melo28ebb872015-08-11 10:38:38 -0300571 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
572 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
David Ahern4f8c1b72013-09-22 19:45:00 -0600573};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300574static DEFINE_STRARRAY(clockid, "CLOCK_");
David Ahern4f8c1b72013-09-22 19:45:00 -0600575
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300576static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
577 struct syscall_arg *arg)
578{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300579 bool show_prefix = arg->show_string_prefix;
580 const char *suffix = "_OK";
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300581 size_t printed = 0;
582 int mode = arg->val;
583
584 if (mode == F_OK) /* 0 */
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300585 return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300586#define P_MODE(n) \
587 if (mode & n##_OK) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300588 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300589 mode &= ~n##_OK; \
590 }
591
592 P_MODE(R);
593 P_MODE(W);
594 P_MODE(X);
595#undef P_MODE
596
597 if (mode)
598 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
599
600 return printed;
601}
602
603#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
604
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300605static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
606 struct syscall_arg *arg);
607
608#define SCA_FILENAME syscall_arg__scnprintf_filename
609
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300610static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
611 struct syscall_arg *arg)
612{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300613 bool show_prefix = arg->show_string_prefix;
614 const char *prefix = "O_";
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300615 int printed = 0, flags = arg->val;
616
617#define P_FLAG(n) \
618 if (flags & O_##n) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300619 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300620 flags &= ~O_##n; \
621 }
622
623 P_FLAG(CLOEXEC);
624 P_FLAG(NONBLOCK);
625#undef P_FLAG
626
627 if (flags)
628 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
629
630 return printed;
631}
632
633#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
634
Arnaldo Carvalho de Meloa355a612016-04-13 11:55:18 -0300635#ifndef GRND_NONBLOCK
636#define GRND_NONBLOCK 0x0001
637#endif
638#ifndef GRND_RANDOM
639#define GRND_RANDOM 0x0002
640#endif
641
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300642static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
643 struct syscall_arg *arg)
644{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300645 bool show_prefix = arg->show_string_prefix;
646 const char *prefix = "GRND_";
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300647 int printed = 0, flags = arg->val;
648
649#define P_FLAG(n) \
650 if (flags & GRND_##n) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300651 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300652 flags &= ~GRND_##n; \
653 }
654
655 P_FLAG(RANDOM);
656 P_FLAG(NONBLOCK);
657#undef P_FLAG
658
659 if (flags)
660 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
661
662 return printed;
663}
664
665#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
666
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300667#define STRARRAY(name, array) \
668 { .scnprintf = SCA_STRARRAY, \
669 .parm = &strarray__##array, }
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -0300670
Arnaldo Carvalho de Melof6af0952019-05-21 16:34:03 -0300671#define STRARRAY_FLAGS(name, array) \
672 { .scnprintf = SCA_STRARRAY_FLAGS, \
673 .parm = &strarray__##array, }
674
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +0100675#include "trace/beauty/arch_errno_names.c"
Arnaldo Carvalho de Meloea8dc3c2016-04-13 12:10:19 -0300676#include "trace/beauty/eventfd.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300677#include "trace/beauty/futex_op.c"
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300678#include "trace/beauty/futex_val3.c"
Arnaldo Carvalho de Melodf4cb162016-04-13 12:05:44 -0300679#include "trace/beauty/mmap.c"
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -0300680#include "trace/beauty/mode_t.c"
Arnaldo Carvalho de Meloa30e6252016-04-27 19:01:52 -0300681#include "trace/beauty/msg_flags.c"
Arnaldo Carvalho de Melo8f48df62016-05-06 10:02:32 -0300682#include "trace/beauty/open_flags.c"
Arnaldo Carvalho de Melo62de3442016-04-26 11:03:03 -0300683#include "trace/beauty/perf_event_open.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300684#include "trace/beauty/pid.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300685#include "trace/beauty/sched_policy.c"
Arnaldo Carvalho de Melof5cd95e2016-05-11 10:32:20 -0300686#include "trace/beauty/seccomp.c"
Arnaldo Carvalho de Melo12199d82016-05-06 09:58:02 -0300687#include "trace/beauty/signum.c"
Arnaldo Carvalho de Melobbf86c42016-04-14 13:53:10 -0300688#include "trace/beauty/socket_type.c"
Arnaldo Carvalho de Melo7206b902016-04-06 14:11:36 -0300689#include "trace/beauty/waitid_options.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300690
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300691struct syscall_arg_fmt {
692 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -0300693 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300694 void *parm;
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -0300695 const char *name;
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -0300696 bool show_zero;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300697};
698
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300699static struct syscall_fmt {
700 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -0300701 const char *alias;
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -0300702 struct {
703 const char *sys_enter,
704 *sys_exit;
705 } bpf_prog_name;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300706 struct syscall_arg_fmt arg[6];
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -0300707 u8 nr_args;
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300708 bool errpid;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300709 bool timeout;
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -0300710 bool hexret;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300711} syscall_fmts[] = {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300712 { .name = "access",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300713 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
Arnaldo Carvalho de Melofb7068e2018-12-18 11:05:56 -0300714 { .name = "arch_prctl",
715 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
716 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
Arnaldo Carvalho de Melo02ef2882018-08-28 17:03:53 -0300717 { .name = "bind",
Arnaldo Carvalho de Meloef969ca642019-07-16 17:21:09 -0300718 .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ },
Arnaldo Carvalho de Melo247dd652019-07-16 17:38:26 -0300719 [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ },
720 [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300721 { .name = "bpf",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300722 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300723 { .name = "brk", .hexret = true,
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300724 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300725 { .name = "clock_gettime",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300726 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
Arnaldo Carvalho de Melo33396a32017-07-19 16:15:17 -0300727 { .name = "clone", .errpid = true, .nr_args = 5,
728 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
729 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
730 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
731 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
732 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300733 { .name = "close",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300734 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
Arnaldo Carvalho de Melod5a7e662018-08-28 16:24:44 -0300735 { .name = "connect",
Arnaldo Carvalho de Meloef969ca642019-07-16 17:21:09 -0300736 .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ },
737 [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ },
Arnaldo Carvalho de Melo1d862752019-07-16 16:34:27 -0300738 [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300739 { .name = "epoll_ctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300740 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300741 { .name = "eventfd2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300742 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300743 { .name = "fchmodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300744 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300745 { .name = "fchownat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300746 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300747 { .name = "fcntl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300748 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
Arnaldo Carvalho de Melo39cc3552017-07-17 16:02:52 -0300749 .parm = &strarrays__fcntl_cmds_arrays,
750 .show_zero = true, },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300751 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300752 { .name = "flock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300753 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
Arnaldo Carvalho de Melodcc6fd62019-05-21 15:36:44 -0300754 { .name = "fsconfig",
755 .arg = { [1] = STRARRAY(cmd, fsconfig_cmds), }, },
Arnaldo Carvalho de Melof6af0952019-05-21 16:34:03 -0300756 { .name = "fsmount",
757 .arg = { [1] = STRARRAY_FLAGS(flags, fsmount_flags),
758 [2] = { .scnprintf = SCA_FSMOUNT_ATTR_FLAGS, /* attr_flags */ }, }, },
Arnaldo Carvalho de Melo693bd392019-05-20 16:24:15 -0300759 { .name = "fspick",
760 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
761 [1] = { .scnprintf = SCA_FILENAME, /* path */ },
762 [2] = { .scnprintf = SCA_FSPICK_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300763 { .name = "fstat", .alias = "newfstat", },
764 { .name = "fstatat", .alias = "newfstatat", },
765 { .name = "futex",
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300766 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
767 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300768 { .name = "futimesat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300769 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300770 { .name = "getitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300771 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300772 { .name = "getpid", .errpid = true, },
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300773 { .name = "getpgid", .errpid = true, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300774 { .name = "getppid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300775 { .name = "getrandom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300776 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300777 { .name = "getrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300778 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo2d1073d2018-01-09 12:03:47 -0300779 { .name = "gettid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300780 { .name = "ioctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300781 .arg = {
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300782#if defined(__i386__) || defined(__x86_64__)
783/*
784 * FIXME: Make this available to all arches.
785 */
Arnaldo Carvalho de Melo1cc47f2d2017-07-31 13:20:14 -0300786 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300787 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300788#else
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300789 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300790#endif
Arnaldo Carvalho de Melo1de30382017-10-31 11:32:23 -0300791 { .name = "kcmp", .nr_args = 5,
792 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
793 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
794 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
795 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
796 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300797 { .name = "keyctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300798 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300799 { .name = "kill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300800 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300801 { .name = "linkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300802 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300803 { .name = "lseek",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300804 .arg = { [2] = STRARRAY(whence, whences), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300805 { .name = "lstat", .alias = "newlstat", },
806 { .name = "madvise",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300807 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
808 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300809 { .name = "mkdirat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300810 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300811 { .name = "mknodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300812 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300813 { .name = "mmap", .hexret = true,
Jiri Olsa54265662017-05-31 13:35:57 +0200814/* The standard mmap maps to old_mmap on s390x */
815#if defined(__s390x__)
816 .alias = "old_mmap",
817#endif
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300818 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
Arnaldo Carvalho de Meloa6631342018-12-18 11:25:08 -0300819 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ },
820 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300821 { .name = "mount",
Arnaldo Carvalho de Melo23c07a22018-10-26 13:51:45 -0300822 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
823 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300824 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo566e3022019-05-20 14:58:03 -0300825 { .name = "move_mount",
826 .arg = { [0] = { .scnprintf = SCA_FDAT, /* from_dfd */ },
827 [1] = { .scnprintf = SCA_FILENAME, /* from_pathname */ },
828 [2] = { .scnprintf = SCA_FDAT, /* to_dfd */ },
829 [3] = { .scnprintf = SCA_FILENAME, /* to_pathname */ },
830 [4] = { .scnprintf = SCA_MOVE_MOUNT_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300831 { .name = "mprotect",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300832 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
833 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300834 { .name = "mq_unlink",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300835 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300836 { .name = "mremap", .hexret = true,
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300837 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300838 { .name = "name_to_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300839 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300840 { .name = "newfstatat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300841 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300842 { .name = "open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300843 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300844 { .name = "open_by_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300845 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
846 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300847 { .name = "openat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300848 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
849 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300850 { .name = "perf_event_open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300851 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
852 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
853 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300854 { .name = "pipe2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300855 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo83bc9c372017-08-28 11:47:11 -0300856 { .name = "pkey_alloc",
857 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
858 { .name = "pkey_free",
859 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
860 { .name = "pkey_mprotect",
861 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
862 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
863 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300864 { .name = "poll", .timeout = true, },
865 { .name = "ppoll", .timeout = true, },
Arnaldo Carvalho de Melofb7068e2018-12-18 11:05:56 -0300866 { .name = "prctl",
Arnaldo Carvalho de Melod688d032017-10-26 15:19:35 -0300867 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
868 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
869 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300870 { .name = "pread", .alias = "pread64", },
871 { .name = "preadv", .alias = "pread", },
872 { .name = "prlimit64",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300873 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300874 { .name = "pwrite", .alias = "pwrite64", },
875 { .name = "readlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300876 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300877 { .name = "recvfrom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300878 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300879 { .name = "recvmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300880 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300881 { .name = "recvmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300882 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300883 { .name = "renameat",
Arnaldo Carvalho de Melo436651c2018-12-11 11:19:23 -0300884 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
885 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
Arnaldo Carvalho de Melo84a83542018-12-11 09:53:21 -0300886 { .name = "renameat2",
887 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
Arnaldo Carvalho de Meloca7ff2c2018-12-11 10:39:30 -0300888 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
889 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300890 { .name = "rt_sigaction",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300891 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300892 { .name = "rt_sigprocmask",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300893 .arg = { [0] = STRARRAY(how, sighow), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300894 { .name = "rt_sigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300895 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300896 { .name = "rt_tgsigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300897 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300898 { .name = "sched_setscheduler",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300899 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300900 { .name = "seccomp",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300901 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
902 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300903 { .name = "select", .timeout = true, },
Arnaldo Carvalho de Meloe4b00e92019-07-17 20:32:13 -0300904 { .name = "sendfile", .alias = "sendfile64", },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300905 { .name = "sendmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300906 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300907 { .name = "sendmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300908 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300909 { .name = "sendto",
Arnaldo Carvalho de Melo6ebb6862018-08-29 08:47:08 -0300910 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
911 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300912 { .name = "set_tid_address", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300913 { .name = "setitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300914 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300915 { .name = "setrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300916 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300917 { .name = "socket",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300918 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300919 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
920 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300921 { .name = "socketpair",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300922 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300923 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
924 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300925 { .name = "stat", .alias = "newstat", },
926 { .name = "statx",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300927 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
928 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
929 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300930 { .name = "swapoff",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300931 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300932 { .name = "swapon",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300933 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300934 { .name = "symlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300935 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Meloa9a187a2019-05-21 21:47:07 -0300936 { .name = "sync_file_range",
937 .arg = { [3] = { .scnprintf = SCA_SYNC_FILE_RANGE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300938 { .name = "tgkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300939 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300940 { .name = "tkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300941 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo476c92ca2018-10-26 13:23:25 -0300942 { .name = "umount2", .alias = "umount",
943 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300944 { .name = "uname", .alias = "newuname", },
945 { .name = "unlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300946 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300947 { .name = "utimensat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300948 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300949 { .name = "wait4", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300950 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300951 { .name = "waitid", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300952 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300953};
954
955static int syscall_fmt__cmp(const void *name, const void *fmtp)
956{
957 const struct syscall_fmt *fmt = fmtp;
958 return strcmp(name, fmt->name);
959}
960
961static struct syscall_fmt *syscall_fmt__find(const char *name)
962{
963 const int nmemb = ARRAY_SIZE(syscall_fmts);
964 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
965}
966
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -0300967static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
968{
969 int i, nmemb = ARRAY_SIZE(syscall_fmts);
970
971 for (i = 0; i < nmemb; ++i) {
972 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
973 return &syscall_fmts[i];
974 }
975
976 return NULL;
977}
978
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300979/*
980 * is_exit: is this "exit" or "exit_group"?
981 * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter.
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -0300982 * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc.
Arnaldo Carvalho de Melob8b10332019-07-17 20:21:37 -0300983 * nonexistent: Just a hole in the syscall table, syscall id not allocated
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300984 */
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300985struct syscall {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500986 struct tep_event *tp_format;
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -0300987 int nr_args;
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -0300988 int args_size;
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -0300989 struct {
990 struct bpf_program *sys_enter,
991 *sys_exit;
992 } bpf_prog;
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300993 bool is_exit;
994 bool is_open;
Arnaldo Carvalho de Melob8b10332019-07-17 20:21:37 -0300995 bool nonexistent;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400996 struct tep_format_field *args;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300997 const char *name;
998 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300999 struct syscall_arg_fmt *arg_fmt;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001000};
1001
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03001002/*
1003 * Must match what is in the BPF program:
1004 *
1005 * tools/perf/examples/bpf/augmented_raw_syscalls.c
1006 */
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03001007struct bpf_map_syscall_entry {
1008 bool enabled;
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03001009 u16 string_args_len[6];
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03001010};
1011
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001012/*
1013 * We need to have this 'calculated' boolean because in some cases we really
1014 * don't know what is the duration of a syscall, for instance, when we start
1015 * a session and some threads are waiting for a syscall to finish, say 'poll',
1016 * in which case all we can do is to print "( ? ) for duration and for the
1017 * start timestamp.
1018 */
1019static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001020{
1021 double duration = (double)t / NSEC_PER_MSEC;
1022 size_t printed = fprintf(fp, "(");
1023
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001024 if (!calculated)
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001025 printed += fprintf(fp, " ");
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001026 else if (duration >= 1.0)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001027 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
1028 else if (duration >= 0.01)
1029 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
1030 else
1031 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001032 return printed + fprintf(fp, "): ");
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001033}
1034
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001035/**
1036 * filename.ptr: The filename char pointer that will be vfs_getname'd
1037 * filename.entry_str_pos: Where to insert the string translated from
1038 * filename.ptr by the vfs_getname tracepoint/kprobe.
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001039 * ret_scnprintf: syscall args may set this to a different syscall return
1040 * formatter, for instance, fcntl may return fds, file flags, etc.
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001041 */
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001042struct thread_trace {
1043 u64 entry_time;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001044 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001045 unsigned long nr_events;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04001046 unsigned long pfmaj, pfmin;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001047 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03001048 double runtime_ms;
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001049 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001050 struct {
1051 unsigned long ptr;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001052 short int entry_str_pos;
1053 bool pending_open;
1054 unsigned int namelen;
1055 char *name;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001056 } filename;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001057 struct {
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001058 int max;
1059 struct file *table;
1060 } files;
David Ahernbf2575c2013-10-08 21:26:53 -06001061
1062 struct intlist *syscall_stats;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001063};
1064
1065static struct thread_trace *thread_trace__new(void)
1066{
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001067 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
1068
Leo Yan7a6d49d2019-07-02 18:34:14 +08001069 if (ttrace) {
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001070 ttrace->files.max = -1;
Leo Yan7a6d49d2019-07-02 18:34:14 +08001071 ttrace->syscall_stats = intlist__new(NULL);
1072 }
David Ahernbf2575c2013-10-08 21:26:53 -06001073
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001074 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001075}
1076
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001077static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001078{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001079 struct thread_trace *ttrace;
1080
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001081 if (thread == NULL)
1082 goto fail;
1083
Namhyung Kim89dceb22014-10-06 09:46:03 +09001084 if (thread__priv(thread) == NULL)
1085 thread__set_priv(thread, thread_trace__new());
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001086
Namhyung Kim89dceb22014-10-06 09:46:03 +09001087 if (thread__priv(thread) == NULL)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001088 goto fail;
1089
Namhyung Kim89dceb22014-10-06 09:46:03 +09001090 ttrace = thread__priv(thread);
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001091 ++ttrace->nr_events;
1092
1093 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001094fail:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001095 color_fprintf(fp, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001096 "WARNING: not enough memory, dropping samples!\n");
1097 return NULL;
1098}
1099
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001100
1101void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001102 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001103{
1104 struct thread_trace *ttrace = thread__priv(arg->thread);
1105
1106 ttrace->ret_scnprintf = ret_scnprintf;
1107}
1108
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04001109#define TRACE_PFMAJ (1 << 0)
1110#define TRACE_PFMIN (1 << 1)
1111
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001112static const size_t trace__entry_str_size = 2048;
1113
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001114static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001115{
Arnaldo Carvalho de Melo05107482019-02-12 10:18:36 -03001116 if (fd < 0)
1117 return NULL;
1118
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001119 if (fd > ttrace->files.max) {
1120 struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001121
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001122 if (nfiles == NULL)
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001123 return NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001124
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001125 if (ttrace->files.max != -1) {
1126 memset(nfiles + ttrace->files.max + 1, 0,
1127 (fd - ttrace->files.max) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001128 } else {
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001129 memset(nfiles, 0, (fd + 1) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001130 }
1131
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001132 ttrace->files.table = nfiles;
1133 ttrace->files.max = fd;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001134 }
1135
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001136 return ttrace->files.table + fd;
1137}
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001138
Arnaldo Carvalho de Melo2d473382018-12-27 16:19:33 -03001139struct file *thread__files_entry(struct thread *thread, int fd)
1140{
1141 return thread_trace__files_entry(thread__priv(thread), fd);
1142}
1143
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001144static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
1145{
1146 struct thread_trace *ttrace = thread__priv(thread);
1147 struct file *file = thread_trace__files_entry(ttrace, fd);
1148
1149 if (file != NULL) {
Arnaldo Carvalho de Melo4bcc4cf2018-12-27 13:32:59 -03001150 struct stat st;
1151 if (stat(pathname, &st) == 0)
1152 file->dev_maj = major(st.st_rdev);
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001153 file->pathname = strdup(pathname);
1154 if (file->pathname)
1155 return 0;
1156 }
1157
1158 return -1;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001159}
1160
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001161static int thread__read_fd_path(struct thread *thread, int fd)
1162{
1163 char linkname[PATH_MAX], pathname[PATH_MAX];
1164 struct stat st;
1165 int ret;
1166
1167 if (thread->pid_ == thread->tid) {
1168 scnprintf(linkname, sizeof(linkname),
1169 "/proc/%d/fd/%d", thread->pid_, fd);
1170 } else {
1171 scnprintf(linkname, sizeof(linkname),
1172 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1173 }
1174
1175 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1176 return -1;
1177
1178 ret = readlink(linkname, pathname, sizeof(pathname));
1179
1180 if (ret < 0 || ret > st.st_size)
1181 return -1;
1182
1183 pathname[ret] = '\0';
1184 return trace__set_fd_pathname(thread, fd, pathname);
1185}
1186
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001187static const char *thread__fd_path(struct thread *thread, int fd,
1188 struct trace *trace)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001189{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001190 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001191
Arnaldo Carvalho de Melo79d725c2019-07-16 16:56:49 -03001192 if (ttrace == NULL || trace->fd_path_disabled)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001193 return NULL;
1194
1195 if (fd < 0)
1196 return NULL;
1197
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001198 if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001199 if (!trace->live)
1200 return NULL;
1201 ++trace->stats.proc_getname;
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001202 if (thread__read_fd_path(thread, fd))
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001203 return NULL;
1204 }
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001205
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001206 return ttrace->files.table[fd].pathname;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001207}
1208
Arnaldo Carvalho de Melofc65eb82017-07-14 15:21:40 -03001209size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001210{
1211 int fd = arg->val;
1212 size_t printed = scnprintf(bf, size, "%d", fd);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001213 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001214
1215 if (path)
1216 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1217
1218 return printed;
1219}
1220
Arnaldo Carvalho de Melo0a2f75402017-10-31 11:30:09 -03001221size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1222{
1223 size_t printed = scnprintf(bf, size, "%d", fd);
1224 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1225
1226 if (thread) {
1227 const char *path = thread__fd_path(thread, fd, trace);
1228
1229 if (path)
1230 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1231
1232 thread__put(thread);
1233 }
1234
1235 return printed;
1236}
1237
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001238static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1239 struct syscall_arg *arg)
1240{
1241 int fd = arg->val;
1242 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
Namhyung Kim89dceb22014-10-06 09:46:03 +09001243 struct thread_trace *ttrace = thread__priv(arg->thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001244
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001245 if (ttrace && fd >= 0 && fd <= ttrace->files.max)
1246 zfree(&ttrace->files.table[fd].pathname);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001247
1248 return printed;
1249}
1250
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001251static void thread__set_filename_pos(struct thread *thread, const char *bf,
1252 unsigned long ptr)
1253{
1254 struct thread_trace *ttrace = thread__priv(thread);
1255
1256 ttrace->filename.ptr = ptr;
1257 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1258}
1259
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001260static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1261{
1262 struct augmented_arg *augmented_arg = arg->augmented.args;
Arnaldo Carvalho de Melo81951682019-06-05 10:34:47 -03001263 size_t printed = scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
1264 /*
1265 * So that the next arg with a payload can consume its augmented arg, i.e. for rename* syscalls
1266 * we would have two strings, each prefixed by its size.
1267 */
1268 int consumed = sizeof(*augmented_arg) + augmented_arg->size;
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001269
Arnaldo Carvalho de Melo016f3272019-06-14 16:50:19 -03001270 arg->augmented.args = ((void *)arg->augmented.args) + consumed;
Arnaldo Carvalho de Melo81951682019-06-05 10:34:47 -03001271 arg->augmented.size -= consumed;
1272
1273 return printed;
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001274}
1275
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001276static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1277 struct syscall_arg *arg)
1278{
1279 unsigned long ptr = arg->val;
1280
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001281 if (arg->augmented.args)
1282 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1283
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001284 if (!arg->trace->vfs_getname)
1285 return scnprintf(bf, size, "%#x", ptr);
1286
1287 thread__set_filename_pos(arg->thread, bf, ptr);
1288 return 0;
1289}
1290
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001291static bool trace__filter_duration(struct trace *trace, double t)
1292{
1293 return t < (trace->duration_filter * NSEC_PER_MSEC);
1294}
1295
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001296static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001297{
1298 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1299
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001300 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001301}
1302
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001303/*
1304 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1305 * using ttrace->entry_time for a thread that receives a sys_exit without
1306 * first having received a sys_enter ("poll" issued before tracing session
1307 * starts, lost sys_enter exit due to ring buffer overflow).
1308 */
1309static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1310{
1311 if (tstamp > 0)
1312 return __trace__fprintf_tstamp(trace, tstamp, fp);
1313
1314 return fprintf(fp, " ? ");
1315}
1316
Namhyung Kimf15eb532012-10-05 14:02:16 +09001317static bool done = false;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001318static bool interrupted = false;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001319
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001320static void sig_handler(int sig)
Namhyung Kimf15eb532012-10-05 14:02:16 +09001321{
1322 done = true;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001323 interrupted = sig == SIGINT;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001324}
1325
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001326static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001327{
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001328 size_t printed = 0;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001329
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001330 if (trace->multiple_threads) {
1331 if (trace->show_comm)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02001332 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
Adrian Hunter38051232013-07-04 16:20:31 +03001333 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001334 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001335
1336 return printed;
1337}
1338
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001339static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1340 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1341{
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03001342 size_t printed = 0;
1343
1344 if (trace->show_tstamp)
1345 printed = trace__fprintf_tstamp(trace, tstamp, fp);
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03001346 if (trace->show_duration)
1347 printed += fprintf_duration(duration, duration_calculated, fp);
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001348 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1349}
1350
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001351static int trace__process_event(struct trace *trace, struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001352 union perf_event *event, struct perf_sample *sample)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001353{
1354 int ret = 0;
1355
1356 switch (event->header.type) {
1357 case PERF_RECORD_LOST:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001358 color_fprintf(trace->output, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001359 "LOST %" PRIu64 " events!\n", event->lost.lost);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001360 ret = machine__process_lost_event(machine, event, sample);
Arnaldo Carvalho de Melo3ed5ca22016-03-30 16:51:17 -03001361 break;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001362 default:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001363 ret = machine__process_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001364 break;
1365 }
1366
1367 return ret;
1368}
1369
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001370static int trace__tool_process(struct perf_tool *tool,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001371 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001372 struct perf_sample *sample,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001373 struct machine *machine)
1374{
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001375 struct trace *trace = container_of(tool, struct trace, tool);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001376 return trace__process_event(trace, machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001377}
1378
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -03001379static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1380{
1381 struct machine *machine = vmachine;
1382
1383 if (machine->kptr_restrict_warned)
1384 return NULL;
1385
1386 if (symbol_conf.kptr_restrict) {
1387 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
Igor Lubashevd06e5fa2019-08-26 21:39:16 -04001388 "Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -03001389 "Kernel samples will not be resolved.\n");
1390 machine->kptr_restrict_warned = true;
1391 return NULL;
1392 }
1393
1394 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1395}
1396
Jiri Olsa63503db2019-07-21 13:23:52 +02001397static int trace__symbols_init(struct trace *trace, struct evlist *evlist)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001398{
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001399 int err = symbol__init(NULL);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001400
1401 if (err)
1402 return err;
1403
David Ahern8fb598e2013-09-28 13:13:00 -06001404 trace->host = machine__new_host();
1405 if (trace->host == NULL)
1406 return -ENOMEM;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001407
Andrei Vagincbd5c172017-11-07 16:22:46 -08001408 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1409 if (err < 0)
1410 goto out;
Arnaldo Carvalho de Melo706c3da2015-07-22 16:16:16 -03001411
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001412 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
Jiri Olsa03617c22019-07-21 13:24:42 +02001413 evlist->core.threads, trace__tool_process, false,
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001414 1);
Andrei Vagincbd5c172017-11-07 16:22:46 -08001415out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001416 if (err)
1417 symbol__exit();
1418
1419 return err;
1420}
1421
Andrei Vagin33974a42017-11-07 16:22:45 -08001422static void trace__symbols__exit(struct trace *trace)
1423{
1424 machine__exit(trace->host);
1425 trace->host = NULL;
1426
1427 symbol__exit();
1428}
1429
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001430static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1431{
1432 int idx;
1433
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001434 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1435 nr_args = sc->fmt->nr_args;
1436
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001437 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1438 if (sc->arg_fmt == NULL)
1439 return -1;
1440
1441 for (idx = 0; idx < nr_args; ++idx) {
1442 if (sc->fmt)
1443 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1444 }
1445
1446 sc->nr_args = nr_args;
1447 return 0;
1448}
1449
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001450static int syscall__set_arg_fmts(struct syscall *sc)
1451{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001452 struct tep_format_field *field, *last_field = NULL;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001453 int idx = 0, len;
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001454
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001455 for (field = sc->args; field; field = field->next, ++idx) {
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001456 last_field = field;
1457
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001458 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1459 continue;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001460
Arnaldo Carvalho de Melodea87bf2019-06-05 10:53:06 -03001461 len = strlen(field->name);
1462
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001463 if (strcmp(field->type, "const char *") == 0 &&
Arnaldo Carvalho de Melodea87bf2019-06-05 10:53:06 -03001464 ((len >= 4 && strcmp(field->name + len - 4, "name") == 0) ||
1465 strstr(field->name, "path") != NULL))
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001466 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -03001467 else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
1468 sc->arg_fmt[idx].scnprintf = SCA_PTR;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -03001469 else if (strcmp(field->type, "pid_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001470 sc->arg_fmt[idx].scnprintf = SCA_PID;
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -03001471 else if (strcmp(field->type, "umode_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001472 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001473 else if ((strcmp(field->type, "int") == 0 ||
1474 strcmp(field->type, "unsigned int") == 0 ||
1475 strcmp(field->type, "long") == 0) &&
Arnaldo Carvalho de Melodea87bf2019-06-05 10:53:06 -03001476 len >= 2 && strcmp(field->name + len - 2, "fd") == 0) {
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001477 /*
1478 * /sys/kernel/tracing/events/syscalls/sys_enter*
1479 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1480 * 65 int
1481 * 23 unsigned int
1482 * 7 unsigned long
1483 */
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001484 sc->arg_fmt[idx].scnprintf = SCA_FD;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001485 }
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001486 }
1487
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001488 if (last_field)
1489 sc->args_size = last_field->offset + last_field->size;
1490
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001491 return 0;
1492}
1493
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001494static int trace__read_syscall_info(struct trace *trace, int id)
1495{
1496 char tp_name[128];
1497 struct syscall *sc;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03001498 const char *name = syscalltbl__name(trace->sctbl, id);
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001499
Arnaldo Carvalho de Melo30a910d2019-07-18 20:19:30 -03001500 if (trace->syscalls.table == NULL) {
Leo Yan3e700082019-08-09 18:47:52 +08001501 trace->syscalls.table = calloc(trace->sctbl->syscalls.max_id + 1, sizeof(*sc));
Arnaldo Carvalho de Melo30a910d2019-07-18 20:19:30 -03001502 if (trace->syscalls.table == NULL)
Arnaldo Carvalho de Melo5d2bd8892019-07-17 19:53:51 -03001503 return -ENOMEM;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001504 }
1505
1506 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melob8b10332019-07-17 20:21:37 -03001507 if (sc->nonexistent)
1508 return 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001509
Arnaldo Carvalho de Melob8b10332019-07-17 20:21:37 -03001510 if (name == NULL) {
1511 sc->nonexistent = true;
1512 return 0;
1513 }
1514
1515 sc->name = name;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001516 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001517
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001518 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
Jiri Olsa97978b32013-12-03 14:09:24 +01001519 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001520
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001521 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001522 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
Jiri Olsa97978b32013-12-03 14:09:24 +01001523 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001524 }
1525
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001526 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
Arnaldo Carvalho de Melo5d2bd8892019-07-17 19:53:51 -03001527 return -ENOMEM;
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001528
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001529 if (IS_ERR(sc->tp_format))
Arnaldo Carvalho de Melo5d2bd8892019-07-17 19:53:51 -03001530 return PTR_ERR(sc->tp_format);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001531
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001532 sc->args = sc->tp_format->format.fields;
Taeung Songc42de702016-02-26 22:14:25 +09001533 /*
1534 * We need to check and discard the first variable '__syscall_nr'
1535 * or 'nr' that mean the syscall number. It is needless here.
1536 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1537 */
1538 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001539 sc->args = sc->args->next;
1540 --sc->nr_args;
1541 }
1542
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001543 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001544 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001545
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001546 return syscall__set_arg_fmts(sc);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001547}
1548
Arnaldo Carvalho de Melo83e69b92019-07-15 17:28:25 -03001549static int intcmp(const void *a, const void *b)
1550{
1551 const int *one = a, *another = b;
1552
1553 return *one - *another;
1554}
1555
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001556static int trace__validate_ev_qualifier(struct trace *trace)
1557{
Arnaldo Carvalho de Meloa4066d62019-06-13 17:35:09 -03001558 int err = 0;
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001559 bool printed_invalid_prefix = false;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001560 struct str_node *pos;
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001561 size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier);
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001562
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001563 trace->ev_qualifier_ids.entries = malloc(nr_allocated *
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001564 sizeof(trace->ev_qualifier_ids.entries[0]));
1565
1566 if (trace->ev_qualifier_ids.entries == NULL) {
1567 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1568 trace->output);
1569 err = -EINVAL;
1570 goto out;
1571 }
1572
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001573 strlist__for_each_entry(pos, trace->ev_qualifier) {
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001574 const char *sc = pos->s;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001575 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001576
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001577 if (id < 0) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001578 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1579 if (id >= 0)
1580 goto matches;
1581
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001582 if (!printed_invalid_prefix) {
1583 pr_debug("Skipping unknown syscalls: ");
1584 printed_invalid_prefix = true;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001585 } else {
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001586 pr_debug(", ");
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001587 }
1588
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001589 pr_debug("%s", sc);
1590 continue;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001591 }
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001592matches:
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001593 trace->ev_qualifier_ids.entries[nr_used++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001594 if (match_next == -1)
1595 continue;
1596
1597 while (1) {
1598 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1599 if (id < 0)
1600 break;
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001601 if (nr_allocated == nr_used) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001602 void *entries;
1603
1604 nr_allocated += 8;
1605 entries = realloc(trace->ev_qualifier_ids.entries,
1606 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1607 if (entries == NULL) {
1608 err = -ENOMEM;
1609 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1610 goto out_free;
1611 }
1612 trace->ev_qualifier_ids.entries = entries;
1613 }
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001614 trace->ev_qualifier_ids.entries[nr_used++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001615 }
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001616 }
1617
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001618 trace->ev_qualifier_ids.nr = nr_used;
Arnaldo Carvalho de Melo83e69b92019-07-15 17:28:25 -03001619 qsort(trace->ev_qualifier_ids.entries, nr_used, sizeof(int), intcmp);
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001620out:
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001621 if (printed_invalid_prefix)
1622 pr_debug("\n");
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001623 return err;
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001624out_free:
1625 zfree(&trace->ev_qualifier_ids.entries);
1626 trace->ev_qualifier_ids.nr = 0;
1627 goto out;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001628}
1629
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03001630static __maybe_unused bool trace__syscall_enabled(struct trace *trace, int id)
1631{
1632 bool in_ev_qualifier;
1633
1634 if (trace->ev_qualifier_ids.nr == 0)
1635 return true;
1636
1637 in_ev_qualifier = bsearch(&id, trace->ev_qualifier_ids.entries,
1638 trace->ev_qualifier_ids.nr, sizeof(int), intcmp) != NULL;
1639
1640 if (in_ev_qualifier)
1641 return !trace->not_ev_qualifier;
1642
1643 return trace->not_ev_qualifier;
1644}
1645
David Ahern55d43bca2015-02-19 15:00:22 -05001646/*
1647 * args is to be interpreted as a series of longs but we need to handle
1648 * 8-byte unaligned accesses. args points to raw_data within the event
1649 * and raw_data is guaranteed to be 8-byte unaligned because it is
1650 * preceded by raw_size which is a u32. So we need to copy args to a temp
1651 * variable to read it. Most notably this avoids extended load instructions
1652 * on unaligned addresses
1653 */
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001654unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001655{
1656 unsigned long val;
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001657 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001658
1659 memcpy(&val, p, sizeof(val));
1660 return val;
1661}
1662
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001663static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1664 struct syscall_arg *arg)
1665{
1666 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1667 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1668
1669 return scnprintf(bf, size, "arg%d: ", arg->idx);
1670}
1671
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001672/*
1673 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1674 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1675 * in tools/perf/trace/beauty/mount_flags.c
1676 */
1677static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1678{
1679 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1680 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1681
1682 return val;
1683}
1684
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001685static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1686 struct syscall_arg *arg, unsigned long val)
1687{
1688 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1689 arg->val = val;
1690 if (sc->arg_fmt[arg->idx].parm)
1691 arg->parm = sc->arg_fmt[arg->idx].parm;
1692 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1693 }
1694 return scnprintf(bf, size, "%ld", val);
1695}
1696
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001697static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001698 unsigned char *args, void *augmented_args, int augmented_args_size,
1699 struct trace *trace, struct thread *thread)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001700{
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001701 size_t printed = 0;
David Ahern55d43bca2015-02-19 15:00:22 -05001702 unsigned long val;
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001703 u8 bit = 1;
1704 struct syscall_arg arg = {
1705 .args = args,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001706 .augmented = {
1707 .size = augmented_args_size,
1708 .args = augmented_args,
1709 },
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001710 .idx = 0,
1711 .mask = 0,
1712 .trace = trace,
1713 .thread = thread,
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -03001714 .show_string_prefix = trace->show_string_prefix,
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001715 };
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001716 struct thread_trace *ttrace = thread__priv(thread);
1717
1718 /*
1719 * Things like fcntl will set this in its 'cmd' formatter to pick the
1720 * right formatter for the return value (an fd? file flags?), which is
1721 * not needed for syscalls that always return a given type, say an fd.
1722 */
1723 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001724
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001725 if (sc->args != NULL) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001726 struct tep_format_field *field;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001727
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001728 for (field = sc->args; field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001729 field = field->next, ++arg.idx, bit <<= 1) {
1730 if (arg.mask & bit)
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001731 continue;
David Ahern55d43bca2015-02-19 15:00:22 -05001732
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001733 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001734 /*
1735 * Some syscall args need some mask, most don't and
1736 * return val untouched.
1737 */
1738 val = syscall__mask_val(sc, &arg, val);
David Ahern55d43bca2015-02-19 15:00:22 -05001739
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001740 /*
1741 * Suppress this argument if its value is zero and
1742 * and we don't have a string associated in an
1743 * strarray for it.
1744 */
David Ahern55d43bca2015-02-19 15:00:22 -05001745 if (val == 0 &&
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03001746 !trace->show_zeros &&
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001747 !(sc->arg_fmt &&
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -03001748 (sc->arg_fmt[arg.idx].show_zero ||
1749 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001750 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1751 sc->arg_fmt[arg.idx].parm))
Arnaldo Carvalho de Melo22ae5cf12013-09-12 11:27:34 -03001752 continue;
1753
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03001754 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
1755
1756 if (trace->show_arg_names)
1757 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
1758
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001759 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001760 }
Arnaldo Carvalho de Melo4c4d6e52016-05-05 23:38:05 -03001761 } else if (IS_ERR(sc->tp_format)) {
1762 /*
1763 * If we managed to read the tracepoint /format file, then we
1764 * may end up not having any args, like with gettid(), so only
1765 * print the raw args when we didn't manage to read it.
1766 */
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001767 while (arg.idx < sc->nr_args) {
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001768 if (arg.mask & bit)
1769 goto next_arg;
1770 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001771 if (printed)
1772 printed += scnprintf(bf + printed, size - printed, ", ");
1773 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001774 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1775next_arg:
1776 ++arg.idx;
1777 bit <<= 1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001778 }
1779 }
1780
1781 return printed;
1782}
1783
Jiri Olsa32dcd022019-07-21 13:23:51 +02001784typedef int (*tracepoint_handler)(struct trace *trace, struct evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001785 union perf_event *event,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001786 struct perf_sample *sample);
1787
1788static struct syscall *trace__syscall_info(struct trace *trace,
Jiri Olsa32dcd022019-07-21 13:23:51 +02001789 struct evsel *evsel, int id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001790{
Arnaldo Carvalho de Melo5d2bd8892019-07-17 19:53:51 -03001791 int err = 0;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001792
1793 if (id < 0) {
Arnaldo Carvalho de Meloadaa18b2013-08-22 17:55:25 -03001794
1795 /*
1796 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1797 * before that, leaving at a higher verbosity level till that is
1798 * explained. Reproduced with plain ftrace with:
1799 *
1800 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1801 * grep "NR -1 " /t/trace_pipe
1802 *
1803 * After generating some load on the machine.
1804 */
1805 if (verbose > 1) {
1806 static u64 n;
1807 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1808 id, perf_evsel__name(evsel), ++n);
1809 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001810 return NULL;
1811 }
1812
Arnaldo Carvalho de Melob8b10332019-07-17 20:21:37 -03001813 err = -EINVAL;
1814
Arnaldo Carvalho de Melo30a910d2019-07-18 20:19:30 -03001815 if (id > trace->sctbl->syscalls.max_id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001816 goto out_cant_read;
1817
Arnaldo Carvalho de Melo30a910d2019-07-18 20:19:30 -03001818 if ((trace->syscalls.table == NULL || trace->syscalls.table[id].name == NULL) &&
1819 (err = trace__read_syscall_info(trace, id)) != 0)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001820 goto out_cant_read;
1821
Arnaldo Carvalho de Melob8b10332019-07-17 20:21:37 -03001822 if (trace->syscalls.table[id].name == NULL) {
1823 if (trace->syscalls.table[id].nonexistent)
1824 return NULL;
1825 goto out_cant_read;
1826 }
1827
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001828 return &trace->syscalls.table[id];
1829
1830out_cant_read:
Namhyung Kimbb963e12017-02-17 17:17:38 +09001831 if (verbose > 0) {
Arnaldo Carvalho de Melo5d2bd8892019-07-17 19:53:51 -03001832 char sbuf[STRERR_BUFSIZE];
1833 fprintf(trace->output, "Problems reading syscall %d: %d (%s)", id, -err, str_error_r(-err, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melo30a910d2019-07-18 20:19:30 -03001834 if (id <= trace->sctbl->syscalls.max_id && trace->syscalls.table[id].name != NULL)
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03001835 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1836 fputs(" information\n", trace->output);
1837 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001838 return NULL;
1839}
1840
David Ahernbf2575c2013-10-08 21:26:53 -06001841static void thread__update_stats(struct thread_trace *ttrace,
1842 int id, struct perf_sample *sample)
1843{
1844 struct int_node *inode;
1845 struct stats *stats;
1846 u64 duration = 0;
1847
1848 inode = intlist__findnew(ttrace->syscall_stats, id);
1849 if (inode == NULL)
1850 return;
1851
1852 stats = inode->priv;
1853 if (stats == NULL) {
1854 stats = malloc(sizeof(struct stats));
1855 if (stats == NULL)
1856 return;
1857 init_stats(stats);
1858 inode->priv = stats;
1859 }
1860
1861 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1862 duration = sample->time - ttrace->entry_time;
1863
1864 update_stats(stats, duration);
1865}
1866
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001867static int trace__printf_interrupted_entry(struct trace *trace)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001868{
1869 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001870 size_t printed;
Arnaldo Carvalho de Melo172bf022019-01-07 16:24:27 -03001871 int len;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001872
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001873 if (trace->failure_only || trace->current == NULL)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001874 return 0;
1875
1876 ttrace = thread__priv(trace->current);
1877
1878 if (!ttrace->entry_pending)
1879 return 0;
1880
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001881 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo172bf022019-01-07 16:24:27 -03001882 printed += len = fprintf(trace->output, "%s)", ttrace->entry_str);
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001883
Arnaldo Carvalho de Melo172bf022019-01-07 16:24:27 -03001884 if (len < trace->args_alignment - 4)
1885 printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " ");
1886
1887 printed += fprintf(trace->output, " ...\n");
1888
1889 ttrace->entry_pending = false;
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03001890 ++trace->nr_events_printed;
1891
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001892 return printed;
1893}
1894
Jiri Olsa32dcd022019-07-21 13:23:51 +02001895static int trace__fprintf_sample(struct trace *trace, struct evsel *evsel,
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001896 struct perf_sample *sample, struct thread *thread)
1897{
1898 int printed = 0;
1899
1900 if (trace->print_sample) {
1901 double ts = (double)sample->time / NSEC_PER_MSEC;
1902
1903 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1904 perf_evsel__name(evsel), ts,
1905 thread__comm_str(thread),
1906 sample->pid, sample->tid, sample->cpu);
1907 }
1908
1909 return printed;
1910}
1911
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001912static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, int raw_augmented_args_size)
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001913{
1914 void *augmented_args = NULL;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001915 /*
1916 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001917 * and there we get all 6 syscall args plus the tracepoint common fields
1918 * that gets calculated at the start and the syscall_nr (another long).
1919 * So we check if that is the case and if so don't look after the
1920 * sc->args_size but always after the full raw_syscalls:sys_enter payload,
1921 * which is fixed.
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001922 *
1923 * We'll revisit this later to pass s->args_size to the BPF augmenter
1924 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1925 * copies only what we need for each syscall, like what happens when we
1926 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1927 * traffic to just what is needed for each syscall.
1928 */
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001929 int args_size = raw_augmented_args_size ?: sc->args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001930
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001931 *augmented_args_size = sample->raw_size - args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001932 if (*augmented_args_size > 0)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001933 augmented_args = sample->raw_data + args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001934
1935 return augmented_args;
1936}
1937
Jiri Olsa32dcd022019-07-21 13:23:51 +02001938static int trace__sys_enter(struct trace *trace, struct evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001939 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001940 struct perf_sample *sample)
1941{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001942 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001943 void *args;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001944 int printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001945 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001946 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001947 int augmented_args_size = 0;
1948 void *augmented_args = NULL;
David Ahernbf2575c2013-10-08 21:26:53 -06001949 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001950 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001951
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001952 if (sc == NULL)
1953 return -1;
1954
David Ahern8fb598e2013-09-28 13:13:00 -06001955 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001956 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001957 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001958 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001959
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001960 trace__fprintf_sample(trace, evsel, sample, thread);
1961
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001962 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001963
1964 if (ttrace->entry_str == NULL) {
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001965 ttrace->entry_str = malloc(trace__entry_str_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001966 if (!ttrace->entry_str)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001967 goto out_put;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001968 }
1969
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001970 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001971 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001972 /*
1973 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1974 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1975 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1976 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1977 * so when handling, say the openat syscall, we end up getting 6 args for the
1978 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1979 * thinking that the extra 2 u64 args are the augmented filename, so just check
1980 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1981 */
1982 if (evsel != trace->syscalls.events.sys_enter)
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001983 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001984 ttrace->entry_time = sample->time;
1985 msg = ttrace->entry_str;
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001986 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001987
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001988 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001989 args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001990
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001991 if (sc->is_exit) {
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001992 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001993 int alignment = 0;
1994
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001995 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001996 printed = fprintf(trace->output, "%s)", ttrace->entry_str);
1997 if (trace->args_alignment > printed)
1998 alignment = trace->args_alignment - printed;
1999 fprintf(trace->output, "%*s= ?\n", alignment, " ");
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002000 }
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002001 } else {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002002 ttrace->entry_pending = true;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002003 /* See trace__vfs_getname & trace__sys_exit */
2004 ttrace->filename.pending_open = false;
2005 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002006
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03002007 if (trace->current != thread) {
2008 thread__put(trace->current);
2009 trace->current = thread__get(thread);
2010 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002011 err = 0;
2012out_put:
2013 thread__put(thread);
2014 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002015}
2016
Jiri Olsa32dcd022019-07-21 13:23:51 +02002017static int trace__fprintf_sys_enter(struct trace *trace, struct evsel *evsel,
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002018 struct perf_sample *sample)
2019{
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002020 struct thread_trace *ttrace;
2021 struct thread *thread;
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03002022 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
2023 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002024 char msg[1024];
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03002025 void *args, *augmented_args = NULL;
2026 int augmented_args_size;
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002027
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002028 if (sc == NULL)
2029 return -1;
2030
2031 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2032 ttrace = thread__trace(thread, trace->output);
2033 /*
2034 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
2035 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
2036 */
2037 if (ttrace == NULL)
2038 goto out_put;
2039
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03002040 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03002041 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03002042 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002043 fprintf(trace->output, "%s", msg);
2044 err = 0;
2045out_put:
2046 thread__put(thread);
2047 return err;
2048}
2049
Jiri Olsa32dcd022019-07-21 13:23:51 +02002050static int trace__resolve_callchain(struct trace *trace, struct evsel *evsel,
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002051 struct perf_sample *sample,
2052 struct callchain_cursor *cursor)
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002053{
2054 struct addr_location al;
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002055 int max_stack = evsel->core.attr.sample_max_stack ?
2056 evsel->core.attr.sample_max_stack :
Ravi Bangoria3a9e9a42018-01-30 11:00:53 +05302057 trace->max_stack;
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03002058 int err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002059
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03002060 if (machine__resolve(trace->host, &al, sample) < 0)
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002061 return -1;
2062
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03002063 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
2064 addr_location__put(&al);
2065 return err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002066}
2067
2068static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
2069{
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002070 /* TODO: user-configurable print_opts */
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03002071 const unsigned int print_opts = EVSEL__PRINT_SYM |
2072 EVSEL__PRINT_DSO |
2073 EVSEL__PRINT_UNKNOWN_AS_ADDR;
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002074
Arnaldo Carvalho de Melod327e602016-04-14 17:53:49 -03002075 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002076}
2077
Jiri Olsa32dcd022019-07-21 13:23:51 +02002078static const char *errno_to_name(struct evsel *evsel, int err)
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01002079{
2080 struct perf_env *env = perf_evsel__env(evsel);
2081 const char *arch_name = perf_env__arch(env);
2082
2083 return arch_syscalls__strerrno(arch_name, err);
2084}
2085
Jiri Olsa32dcd022019-07-21 13:23:51 +02002086static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002087 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002088 struct perf_sample *sample)
2089{
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09002090 long ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002091 u64 duration = 0;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002092 bool duration_calculated = false;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002093 struct thread *thread;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002094 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
2095 int alignment = trace->args_alignment;
David Ahernbf2575c2013-10-08 21:26:53 -06002096 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002097 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002098
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002099 if (sc == NULL)
2100 return -1;
2101
David Ahern8fb598e2013-09-28 13:13:00 -06002102 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002103 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002104 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002105 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002106
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03002107 trace__fprintf_sample(trace, evsel, sample, thread);
2108
David Ahernbf2575c2013-10-08 21:26:53 -06002109 if (trace->summary)
2110 thread__update_stats(ttrace, id, sample);
2111
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03002112 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002113
Arnaldo Carvalho de Melo79d725c2019-07-16 16:56:49 -03002114 if (!trace->fd_path_disabled && sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002115 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
2116 ttrace->filename.pending_open = false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002117 ++trace->stats.vfs_getname;
2118 }
2119
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002120 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002121 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002122 if (trace__filter_duration(trace, duration))
2123 goto out;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002124 duration_calculated = true;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002125 } else if (trace->duration_filter)
2126 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002127
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002128 if (sample->callchain) {
2129 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2130 if (callchain_ret == 0) {
2131 if (callchain_cursor.nr < trace->min_stack)
2132 goto out;
2133 callchain_ret = 1;
2134 }
2135 }
2136
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03002137 if (trace->summary_only || (ret >= 0 && trace->failure_only))
David Ahernfd2eaba2013-11-12 09:31:15 -07002138 goto out;
2139
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002140 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002141
2142 if (ttrace->entry_pending) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002143 printed = fprintf(trace->output, "%s", ttrace->entry_str);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002144 } else {
Arnaldo Carvalho de Meloac6e0222019-01-07 16:54:38 -03002145 printed += fprintf(trace->output, " ... [");
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002146 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
Arnaldo Carvalho de Meloac6e0222019-01-07 16:54:38 -03002147 printed += 9;
2148 printed += fprintf(trace->output, "]: %s()", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002149 }
2150
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002151 printed++; /* the closing ')' */
2152
2153 if (alignment > printed)
2154 alignment -= printed;
2155 else
2156 alignment = 0;
2157
2158 fprintf(trace->output, ")%*s= ", alignment, " ");
2159
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002160 if (sc->fmt == NULL) {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002161 if (ret < 0)
2162 goto errno_print;
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002163signed_print:
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002164 fprintf(trace->output, "%ld", ret);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002165 } else if (ret < 0) {
2166errno_print: {
Masami Hiramatsu942a91e2014-08-14 02:22:41 +00002167 char bf[STRERR_BUFSIZE];
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002168 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01002169 *e = errno_to_name(evsel, -ret);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002170
Arnaldo Carvalho de Melo2c83dfa2018-12-17 12:06:34 -03002171 fprintf(trace->output, "-1 %s (%s)", e, emsg);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002172 }
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002173 } else if (ret == 0 && sc->fmt->timeout)
Arnaldo Carvalho de Melo2c83dfa2018-12-17 12:06:34 -03002174 fprintf(trace->output, "0 (Timeout)");
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002175 else if (ttrace->ret_scnprintf) {
2176 char bf[1024];
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03002177 struct syscall_arg arg = {
2178 .val = ret,
2179 .thread = thread,
2180 .trace = trace,
2181 };
2182 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002183 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002184 fprintf(trace->output, "%s", bf);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002185 } else if (sc->fmt->hexret)
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002186 fprintf(trace->output, "%#lx", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002187 else if (sc->fmt->errpid) {
2188 struct thread *child = machine__find_thread(trace->host, ret, ret);
2189
2190 if (child != NULL) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002191 fprintf(trace->output, "%ld", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002192 if (child->comm_set)
2193 fprintf(trace->output, " (%s)", thread__comm_str(child));
2194 thread__put(child);
2195 }
2196 } else
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002197 goto signed_print;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002198
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002199 fputc('\n', trace->output);
Milian Wolff566a0882016-04-08 13:34:15 +02002200
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002201 /*
2202 * We only consider an 'event' for the sake of --max-events a non-filtered
2203 * sys_enter + sys_exit and other tracepoint events.
2204 */
2205 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2206 interrupted = true;
2207
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002208 if (callchain_ret > 0)
2209 trace__fprintf_callchain(trace, sample);
2210 else if (callchain_ret < 0)
2211 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002212out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002213 ttrace->entry_pending = false;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002214 err = 0;
2215out_put:
2216 thread__put(thread);
2217 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002218}
2219
Jiri Olsa32dcd022019-07-21 13:23:51 +02002220static int trace__vfs_getname(struct trace *trace, struct evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002221 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002222 struct perf_sample *sample)
2223{
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002224 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2225 struct thread_trace *ttrace;
2226 size_t filename_len, entry_str_len, to_move;
2227 ssize_t remaining_space;
2228 char *pos;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002229 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002230
2231 if (!thread)
2232 goto out;
2233
2234 ttrace = thread__priv(thread);
2235 if (!ttrace)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002236 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002237
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002238 filename_len = strlen(filename);
Arnaldo Carvalho de Melo39f0e7a2017-03-24 14:51:28 -03002239 if (filename_len == 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002240 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002241
2242 if (ttrace->filename.namelen < filename_len) {
2243 char *f = realloc(ttrace->filename.name, filename_len + 1);
2244
2245 if (f == NULL)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002246 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002247
2248 ttrace->filename.namelen = filename_len;
2249 ttrace->filename.name = f;
2250 }
2251
2252 strcpy(ttrace->filename.name, filename);
2253 ttrace->filename.pending_open = true;
2254
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002255 if (!ttrace->filename.ptr)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002256 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002257
2258 entry_str_len = strlen(ttrace->entry_str);
2259 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2260 if (remaining_space <= 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002261 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002262
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002263 if (filename_len > (size_t)remaining_space) {
2264 filename += filename_len - remaining_space;
2265 filename_len = remaining_space;
2266 }
2267
2268 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2269 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2270 memmove(pos + filename_len, pos, to_move);
2271 memcpy(pos, filename, filename_len);
2272
2273 ttrace->filename.ptr = 0;
2274 ttrace->filename.entry_str_pos = 0;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002275out_put:
2276 thread__put(thread);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002277out:
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002278 return 0;
2279}
2280
Jiri Olsa32dcd022019-07-21 13:23:51 +02002281static int trace__sched_stat_runtime(struct trace *trace, struct evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002282 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002283 struct perf_sample *sample)
2284{
2285 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2286 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
David Ahern8fb598e2013-09-28 13:13:00 -06002287 struct thread *thread = machine__findnew_thread(trace->host,
Adrian Hunter314add62013-08-27 11:23:03 +03002288 sample->pid,
2289 sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002290 struct thread_trace *ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002291
2292 if (ttrace == NULL)
2293 goto out_dump;
2294
2295 ttrace->runtime_ms += runtime_ms;
2296 trace->runtime_ms += runtime_ms;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002297out_put:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002298 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002299 return 0;
2300
2301out_dump:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002302 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002303 evsel->name,
2304 perf_evsel__strval(evsel, sample, "comm"),
2305 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2306 runtime,
2307 perf_evsel__intval(evsel, sample, "vruntime"));
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002308 goto out_put;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002309}
2310
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002311static int bpf_output__printer(enum binary_printer_ops op,
2312 unsigned int val, void *extra __maybe_unused, FILE *fp)
Wang Nan1d6c9402016-02-26 09:31:55 +00002313{
Wang Nan1d6c9402016-02-26 09:31:55 +00002314 unsigned char ch = (unsigned char)val;
2315
2316 switch (op) {
2317 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002318 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
Wang Nan1d6c9402016-02-26 09:31:55 +00002319 case BINARY_PRINT_DATA_BEGIN:
2320 case BINARY_PRINT_LINE_BEGIN:
2321 case BINARY_PRINT_ADDR:
2322 case BINARY_PRINT_NUM_DATA:
2323 case BINARY_PRINT_NUM_PAD:
2324 case BINARY_PRINT_SEP:
2325 case BINARY_PRINT_CHAR_PAD:
2326 case BINARY_PRINT_LINE_END:
2327 case BINARY_PRINT_DATA_END:
2328 default:
2329 break;
2330 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002331
2332 return 0;
Wang Nan1d6c9402016-02-26 09:31:55 +00002333}
2334
2335static void bpf_output__fprintf(struct trace *trace,
2336 struct perf_sample *sample)
2337{
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002338 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2339 bpf_output__printer, NULL, trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002340 ++trace->nr_events_printed;
Wang Nan1d6c9402016-02-26 09:31:55 +00002341}
2342
Jiri Olsa32dcd022019-07-21 13:23:51 +02002343static int trace__event_handler(struct trace *trace, struct evsel *evsel,
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002344 union perf_event *event __maybe_unused,
2345 struct perf_sample *sample)
2346{
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002347 struct thread *thread;
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002348 int callchain_ret = 0;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002349 /*
2350 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2351 * this event's max_events having been hit and this is an entry coming
2352 * from the ring buffer that we should discard, since the max events
2353 * have already been considered/printed.
2354 */
2355 if (evsel->disabled)
2356 return 0;
2357
2358 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002359
2360 if (sample->callchain) {
2361 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2362 if (callchain_ret == 0) {
2363 if (callchain_cursor.nr < trace->min_stack)
2364 goto out;
2365 callchain_ret = 1;
2366 }
2367 }
2368
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03002369 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002370 trace__fprintf_tstamp(trace, sample->time, trace->output);
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002371
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03002372 if (trace->trace_syscalls && trace->show_duration)
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002373 fprintf(trace->output, "( ): ");
2374
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002375 if (thread)
2376 trace__fprintf_comm_tid(trace, thread, trace->output);
2377
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002378 if (evsel == trace->syscalls.events.augmented) {
2379 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2380 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2381
2382 if (sc) {
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002383 fprintf(trace->output, "%s(", sc->name);
2384 trace__fprintf_sys_enter(trace, evsel, sample);
2385 fputc(')', trace->output);
2386 goto newline;
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002387 }
2388
2389 /*
2390 * XXX: Not having the associated syscall info or not finding/adding
2391 * the thread should never happen, but if it does...
2392 * fall thru and print it as a bpf_output event.
2393 */
2394 }
2395
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002396 fprintf(trace->output, "%s:", evsel->name);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002397
Wang Nan1d6c9402016-02-26 09:31:55 +00002398 if (perf_evsel__is_bpf_output(evsel)) {
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002399 bpf_output__fprintf(trace, sample);
Wang Nan1d6c9402016-02-26 09:31:55 +00002400 } else if (evsel->tp_format) {
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002401 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2402 trace__fprintf_sys_enter(trace, evsel, sample)) {
2403 event_format__fprintf(evsel->tp_format, sample->cpu,
2404 sample->raw_data, sample->raw_size,
2405 trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002406 ++trace->nr_events_printed;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002407
2408 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
Jiri Olsa9a10bb22019-07-21 13:24:03 +02002409 evsel__disable(evsel);
Jiri Olsa88761fa2019-07-21 13:24:50 +02002410 evsel__close(evsel);
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002411 }
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002412 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002413 }
2414
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002415newline:
Changbin Du51125a22018-03-13 18:40:01 +08002416 fprintf(trace->output, "\n");
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002417
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002418 if (callchain_ret > 0)
2419 trace__fprintf_callchain(trace, sample);
2420 else if (callchain_ret < 0)
2421 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2422out:
Arnaldo Carvalho de Meloa937c662018-10-20 09:27:52 -03002423 thread__put(thread);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002424 return 0;
2425}
2426
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002427static void print_location(FILE *f, struct perf_sample *sample,
2428 struct addr_location *al,
2429 bool print_dso, bool print_sym)
2430{
2431
Namhyung Kimbb963e12017-02-17 17:17:38 +09002432 if ((verbose > 0 || print_dso) && al->map)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002433 fprintf(f, "%s@", al->map->dso->long_name);
2434
Namhyung Kimbb963e12017-02-17 17:17:38 +09002435 if ((verbose > 0 || print_sym) && al->sym)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002436 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002437 al->addr - al->sym->start);
2438 else if (al->map)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002439 fprintf(f, "0x%" PRIx64, al->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002440 else
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002441 fprintf(f, "0x%" PRIx64, sample->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002442}
2443
2444static int trace__pgfault(struct trace *trace,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002445 struct evsel *evsel,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03002446 union perf_event *event __maybe_unused,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002447 struct perf_sample *sample)
2448{
2449 struct thread *thread;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002450 struct addr_location al;
2451 char map_type = 'd';
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002452 struct thread_trace *ttrace;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002453 int err = -1;
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002454 int callchain_ret = 0;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002455
2456 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002457
2458 if (sample->callchain) {
2459 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2460 if (callchain_ret == 0) {
2461 if (callchain_cursor.nr < trace->min_stack)
2462 goto out_put;
2463 callchain_ret = 1;
2464 }
2465 }
2466
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002467 ttrace = thread__trace(thread, trace->output);
2468 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002469 goto out_put;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002470
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002471 if (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002472 ttrace->pfmaj++;
2473 else
2474 ttrace->pfmin++;
2475
2476 if (trace->summary_only)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002477 goto out;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002478
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002479 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002480
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002481 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002482
2483 fprintf(trace->output, "%sfault [",
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002484 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002485 "maj" : "min");
2486
2487 print_location(trace->output, sample, &al, false, true);
2488
2489 fprintf(trace->output, "] => ");
2490
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03002491 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002492
2493 if (!al.map) {
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002494 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002495
2496 if (al.map)
2497 map_type = 'x';
2498 else
2499 map_type = '?';
2500 }
2501
2502 print_location(trace->output, sample, &al, true, false);
2503
2504 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
Arnaldo Carvalho de Melo0c3a6ef2016-04-19 16:31:12 -03002505
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002506 if (callchain_ret > 0)
2507 trace__fprintf_callchain(trace, sample);
2508 else if (callchain_ret < 0)
2509 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002510
2511 ++trace->nr_events_printed;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002512out:
2513 err = 0;
2514out_put:
2515 thread__put(thread);
2516 return err;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002517}
2518
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002519static void trace__set_base_time(struct trace *trace,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002520 struct evsel *evsel,
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002521 struct perf_sample *sample)
2522{
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002523 /*
2524 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2525 * and don't use sample->time unconditionally, we may end up having
2526 * some other event in the future without PERF_SAMPLE_TIME for good
2527 * reason, i.e. we may not be interested in its timestamps, just in
2528 * it taking place, picking some piece of information when it
2529 * appears in our event stream (vfs_getname comes to mind).
2530 */
2531 if (trace->base_time == 0 && !trace->full_time &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002532 (evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002533 trace->base_time = sample->time;
2534}
2535
David Ahern6810fc92013-08-28 22:29:52 -06002536static int trace__process_sample(struct perf_tool *tool,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002537 union perf_event *event,
David Ahern6810fc92013-08-28 22:29:52 -06002538 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002539 struct evsel *evsel,
David Ahern6810fc92013-08-28 22:29:52 -06002540 struct machine *machine __maybe_unused)
2541{
2542 struct trace *trace = container_of(tool, struct trace, tool);
David Ahernaa07df62016-11-25 09:29:52 -07002543 struct thread *thread;
David Ahern6810fc92013-08-28 22:29:52 -06002544 int err = 0;
2545
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002546 tracepoint_handler handler = evsel->handler;
David Ahern6810fc92013-08-28 22:29:52 -06002547
David Ahernaa07df62016-11-25 09:29:52 -07002548 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2549 if (thread && thread__is_filtered(thread))
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002550 goto out;
David Ahernbdc89662013-08-28 22:29:53 -06002551
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002552 trace__set_base_time(trace, evsel, sample);
David Ahern6810fc92013-08-28 22:29:52 -06002553
David Ahern31605652013-12-04 19:41:41 -07002554 if (handler) {
2555 ++trace->nr_events;
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002556 handler(trace, evsel, event, sample);
David Ahern31605652013-12-04 19:41:41 -07002557 }
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002558out:
2559 thread__put(thread);
David Ahern6810fc92013-08-28 22:29:52 -06002560 return err;
2561}
2562
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002563static int trace__record(struct trace *trace, int argc, const char **argv)
David Ahern5e2485b2013-09-28 13:13:01 -06002564{
2565 unsigned int rec_argc, i, j;
2566 const char **rec_argv;
2567 const char * const record_args[] = {
2568 "record",
2569 "-R",
2570 "-m", "1024",
2571 "-c", "1",
David Ahern5e2485b2013-09-28 13:13:01 -06002572 };
2573
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002574 const char * const sc_args[] = { "-e", };
2575 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2576 const char * const majpf_args[] = { "-e", "major-faults" };
2577 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2578 const char * const minpf_args[] = { "-e", "minor-faults" };
2579 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2580
David Ahern9aca7f12013-12-04 19:41:39 -07002581 /* +1 is for the event string below */
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002582 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2583 majpf_args_nr + minpf_args_nr + argc;
David Ahern5e2485b2013-09-28 13:13:01 -06002584 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2585
2586 if (rec_argv == NULL)
2587 return -ENOMEM;
2588
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002589 j = 0;
David Ahern5e2485b2013-09-28 13:13:01 -06002590 for (i = 0; i < ARRAY_SIZE(record_args); i++)
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002591 rec_argv[j++] = record_args[i];
2592
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002593 if (trace->trace_syscalls) {
2594 for (i = 0; i < sc_args_nr; i++)
2595 rec_argv[j++] = sc_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002596
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002597 /* event string may be different for older kernels - e.g., RHEL6 */
2598 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2599 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2600 else if (is_valid_tracepoint("syscalls:sys_enter"))
2601 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2602 else {
2603 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
Martin Kepplingerc896f852017-09-13 21:14:19 +02002604 free(rec_argv);
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002605 return -1;
2606 }
David Ahern9aca7f12013-12-04 19:41:39 -07002607 }
David Ahern9aca7f12013-12-04 19:41:39 -07002608
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002609 if (trace->trace_pgfaults & TRACE_PFMAJ)
2610 for (i = 0; i < majpf_args_nr; i++)
2611 rec_argv[j++] = majpf_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002612
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002613 if (trace->trace_pgfaults & TRACE_PFMIN)
2614 for (i = 0; i < minpf_args_nr; i++)
2615 rec_argv[j++] = minpf_args[i];
2616
2617 for (i = 0; i < (unsigned int)argc; i++)
2618 rec_argv[j++] = argv[i];
2619
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002620 return cmd_record(j, rec_argv);
David Ahern5e2485b2013-09-28 13:13:01 -06002621}
2622
David Ahernbf2575c2013-10-08 21:26:53 -06002623static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2624
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02002625static bool evlist__add_vfs_getname(struct evlist *evlist)
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002626{
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002627 bool found = false;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002628 struct evsel *evsel, *tmp;
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002629 struct parse_events_error err = { .idx = 0, };
2630 int ret = parse_events(evlist, "probe:vfs_getname*", &err);
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002631
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002632 if (ret)
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002633 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002634
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002635 evlist__for_each_entry_safe(evlist, evsel, tmp) {
2636 if (!strstarts(perf_evsel__name(evsel), "probe:vfs_getname"))
2637 continue;
2638
2639 if (perf_evsel__field(evsel, "pathname")) {
2640 evsel->handler = trace__vfs_getname;
2641 found = true;
2642 continue;
2643 }
2644
Jiri Olsab27c4ec2019-07-21 13:24:22 +02002645 list_del_init(&evsel->core.node);
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002646 evsel->evlist = NULL;
Jiri Olsa5eb2dd22019-07-21 13:23:57 +02002647 evsel__delete(evsel);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002648 }
2649
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002650 return found;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002651}
2652
Jiri Olsa32dcd022019-07-21 13:23:51 +02002653static struct evsel *perf_evsel__new_pgfault(u64 config)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002654{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002655 struct evsel *evsel;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002656 struct perf_event_attr attr = {
2657 .type = PERF_TYPE_SOFTWARE,
2658 .mmap_data = 1,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002659 };
2660
2661 attr.config = config;
Arnaldo Carvalho de Melo05247982014-07-23 18:15:09 -03002662 attr.sample_period = 1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002663
2664 event_attr_init(&attr);
2665
Jiri Olsa365c3ae2019-07-21 13:23:58 +02002666 evsel = evsel__new(&attr);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002667 if (evsel)
2668 evsel->handler = trace__pgfault;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002669
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002670 return evsel;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002671}
2672
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002673static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2674{
2675 const u32 type = event->header.type;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002676 struct evsel *evsel;
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002677
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002678 if (type != PERF_RECORD_SAMPLE) {
2679 trace__process_event(trace, trace->host, event, sample);
2680 return;
2681 }
2682
2683 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2684 if (evsel == NULL) {
2685 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2686 return;
2687 }
2688
Arnaldo Carvalho de Melo22ac4312019-08-15 12:15:39 -03002689 if (evswitch__discard(&trace->evswitch, evsel))
2690 return;
2691
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002692 trace__set_base_time(trace, evsel, sample);
2693
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002694 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002695 sample->raw_data == NULL) {
2696 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2697 perf_evsel__name(evsel), sample->tid,
2698 sample->cpu, sample->raw_size);
2699 } else {
2700 tracepoint_handler handler = evsel->handler;
2701 handler(trace, evsel, event, sample);
2702 }
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002703
2704 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2705 interrupted = true;
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002706}
2707
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002708static int trace__add_syscall_newtp(struct trace *trace)
2709{
2710 int ret = -1;
Jiri Olsa63503db2019-07-21 13:23:52 +02002711 struct evlist *evlist = trace->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002712 struct evsel *sys_enter, *sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002713
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002714 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002715 if (sys_enter == NULL)
2716 goto out;
2717
2718 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2719 goto out_delete_sys_enter;
2720
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002721 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002722 if (sys_exit == NULL)
2723 goto out_delete_sys_enter;
2724
2725 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2726 goto out_delete_sys_exit;
2727
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002728 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2729 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2730
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02002731 evlist__add(evlist, sys_enter);
2732 evlist__add(evlist, sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002733
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002734 if (callchain_param.enabled && !trace->kernel_syscallchains) {
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03002735 /*
2736 * We're interested only in the user space callchain
2737 * leading to the syscall, allow overriding that for
2738 * debugging reasons using --kernel_syscall_callchains
2739 */
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002740 sys_exit->core.attr.exclude_callchain_kernel = 1;
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03002741 }
2742
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03002743 trace->syscalls.events.sys_enter = sys_enter;
2744 trace->syscalls.events.sys_exit = sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002745
2746 ret = 0;
2747out:
2748 return ret;
2749
2750out_delete_sys_exit:
Jiri Olsa5eb2dd22019-07-21 13:23:57 +02002751 evsel__delete_priv(sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002752out_delete_sys_enter:
Jiri Olsa5eb2dd22019-07-21 13:23:57 +02002753 evsel__delete_priv(sys_enter);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002754 goto out;
2755}
2756
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002757static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002758{
2759 int err = -1;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002760 struct evsel *sys_exit;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002761 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2762 trace->ev_qualifier_ids.nr,
2763 trace->ev_qualifier_ids.entries);
2764
2765 if (filter == NULL)
2766 goto out_enomem;
2767
Mathieu Poirier3541c032016-09-16 08:44:04 -06002768 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2769 filter)) {
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002770 sys_exit = trace->syscalls.events.sys_exit;
Mathieu Poirier3541c032016-09-16 08:44:04 -06002771 err = perf_evsel__append_tp_filter(sys_exit, filter);
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002772 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002773
2774 free(filter);
2775out:
2776 return err;
2777out_enomem:
2778 errno = ENOMEM;
2779 goto out;
2780}
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002781
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002782#ifdef HAVE_LIBBPF_SUPPORT
Arnaldo Carvalho de Melo5834da72019-07-15 17:51:43 -03002783static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace, const char *name)
2784{
2785 if (trace->bpf_obj == NULL)
2786 return NULL;
2787
2788 return bpf_object__find_program_by_title(trace->bpf_obj, name);
2789}
2790
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -03002791static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace, struct syscall *sc,
2792 const char *prog_name, const char *type)
2793{
2794 struct bpf_program *prog;
2795
Arnaldo Carvalho de Melo8b8044e2019-07-16 15:24:58 -03002796 if (prog_name == NULL) {
2797 char default_prog_name[256];
2798 scnprintf(default_prog_name, sizeof(default_prog_name), "!syscalls:sys_%s_%s", type, sc->name);
2799 prog = trace__find_bpf_program_by_title(trace, default_prog_name);
2800 if (prog != NULL)
2801 goto out_found;
2802 if (sc->fmt && sc->fmt->alias) {
2803 scnprintf(default_prog_name, sizeof(default_prog_name), "!syscalls:sys_%s_%s", type, sc->fmt->alias);
2804 prog = trace__find_bpf_program_by_title(trace, default_prog_name);
2805 if (prog != NULL)
2806 goto out_found;
2807 }
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -03002808 goto out_unaugmented;
Arnaldo Carvalho de Melo8b8044e2019-07-16 15:24:58 -03002809 }
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -03002810
2811 prog = trace__find_bpf_program_by_title(trace, prog_name);
Arnaldo Carvalho de Melo8b8044e2019-07-16 15:24:58 -03002812
2813 if (prog != NULL) {
2814out_found:
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -03002815 return prog;
Arnaldo Carvalho de Melo8b8044e2019-07-16 15:24:58 -03002816 }
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -03002817
2818 pr_debug("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n",
2819 prog_name, type, sc->name);
2820out_unaugmented:
2821 return trace->syscalls.unaugmented_prog;
2822}
2823
2824static void trace__init_syscall_bpf_progs(struct trace *trace, int id)
2825{
2826 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2827
2828 if (sc == NULL)
2829 return;
2830
Arnaldo Carvalho de Melo8b8044e2019-07-16 15:24:58 -03002831 sc->bpf_prog.sys_enter = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_enter : NULL, "enter");
2832 sc->bpf_prog.sys_exit = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_exit : NULL, "exit");
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -03002833}
2834
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03002835static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int id)
2836{
2837 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2838 return sc ? bpf_program__fd(sc->bpf_prog.sys_enter) : bpf_program__fd(trace->syscalls.unaugmented_prog);
2839}
2840
2841static int trace__bpf_prog_sys_exit_fd(struct trace *trace, int id)
2842{
2843 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2844 return sc ? bpf_program__fd(sc->bpf_prog.sys_exit) : bpf_program__fd(trace->syscalls.unaugmented_prog);
2845}
2846
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03002847static void trace__init_bpf_map_syscall_args(struct trace *trace, int id, struct bpf_map_syscall_entry *entry)
2848{
2849 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2850 int arg = 0;
2851
2852 if (sc == NULL)
2853 goto out;
2854
2855 for (; arg < sc->nr_args; ++arg) {
2856 entry->string_args_len[arg] = 0;
2857 if (sc->arg_fmt[arg].scnprintf == SCA_FILENAME) {
2858 /* Should be set like strace -s strsize */
2859 entry->string_args_len[arg] = PATH_MAX;
2860 }
2861 }
2862out:
2863 for (; arg < 6; ++arg)
2864 entry->string_args_len[arg] = 0;
2865}
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002866static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2867{
2868 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002869 struct bpf_map_syscall_entry value = {
2870 .enabled = !trace->not_ev_qualifier,
2871 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002872 int err = 0;
2873 size_t i;
2874
2875 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2876 int key = trace->ev_qualifier_ids.entries[i];
2877
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -03002878 if (value.enabled) {
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03002879 trace__init_bpf_map_syscall_args(trace, key, &value);
Arnaldo Carvalho de Melo6ff8fff2019-07-16 10:59:19 -03002880 trace__init_syscall_bpf_progs(trace, key);
2881 }
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03002882
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002883 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2884 if (err)
2885 break;
2886 }
2887
2888 return err;
2889}
2890
2891static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2892{
2893 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002894 struct bpf_map_syscall_entry value = {
2895 .enabled = enabled,
2896 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002897 int err = 0, key;
2898
2899 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03002900 if (enabled)
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03002901 trace__init_bpf_map_syscall_args(trace, key, &value);
2902
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002903 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002904 if (err)
2905 break;
2906 }
2907
2908 return err;
2909}
2910
2911static int trace__init_syscalls_bpf_map(struct trace *trace)
2912{
2913 bool enabled = true;
2914
2915 if (trace->ev_qualifier_ids.nr)
2916 enabled = trace->not_ev_qualifier;
2917
2918 return __trace__init_syscalls_bpf_map(trace, enabled);
2919}
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03002920
Arnaldo Carvalho de Meload4153f2019-07-17 18:27:33 -03002921static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace, struct syscall *sc)
2922{
2923 struct tep_format_field *field, *candidate_field;
2924 int id;
2925
2926 /*
2927 * We're only interested in syscalls that have a pointer:
2928 */
2929 for (field = sc->args; field; field = field->next) {
2930 if (field->flags & TEP_FIELD_IS_POINTER)
2931 goto try_to_find_pair;
2932 }
2933
2934 return NULL;
2935
2936try_to_find_pair:
2937 for (id = 0; id < trace->sctbl->syscalls.nr_entries; ++id) {
2938 struct syscall *pair = trace__syscall_info(trace, NULL, id);
2939 struct bpf_program *pair_prog;
2940 bool is_candidate = false;
2941
2942 if (pair == NULL || pair == sc ||
2943 pair->bpf_prog.sys_enter == trace->syscalls.unaugmented_prog)
2944 continue;
2945
2946 for (field = sc->args, candidate_field = pair->args;
2947 field && candidate_field; field = field->next, candidate_field = candidate_field->next) {
2948 bool is_pointer = field->flags & TEP_FIELD_IS_POINTER,
2949 candidate_is_pointer = candidate_field->flags & TEP_FIELD_IS_POINTER;
2950
2951 if (is_pointer) {
2952 if (!candidate_is_pointer) {
2953 // The candidate just doesn't copies our pointer arg, might copy other pointers we want.
2954 continue;
2955 }
2956 } else {
2957 if (candidate_is_pointer) {
2958 // The candidate might copy a pointer we don't have, skip it.
2959 goto next_candidate;
2960 }
2961 continue;
2962 }
2963
2964 if (strcmp(field->type, candidate_field->type))
2965 goto next_candidate;
2966
2967 is_candidate = true;
2968 }
2969
2970 if (!is_candidate)
2971 goto next_candidate;
2972
2973 /*
2974 * Check if the tentative pair syscall augmenter has more pointers, if it has,
2975 * then it may be collecting that and we then can't use it, as it would collect
2976 * more than what is common to the two syscalls.
2977 */
2978 if (candidate_field) {
2979 for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next)
2980 if (candidate_field->flags & TEP_FIELD_IS_POINTER)
2981 goto next_candidate;
2982 }
2983
2984 pair_prog = pair->bpf_prog.sys_enter;
2985 /*
2986 * If the pair isn't enabled, then its bpf_prog.sys_enter will not
2987 * have been searched for, so search it here and if it returns the
2988 * unaugmented one, then ignore it, otherwise we'll reuse that BPF
2989 * program for a filtered syscall on a non-filtered one.
2990 *
2991 * For instance, we have "!syscalls:sys_enter_renameat" and that is
2992 * useful for "renameat2".
2993 */
2994 if (pair_prog == NULL) {
2995 pair_prog = trace__find_syscall_bpf_prog(trace, pair, pair->fmt ? pair->fmt->bpf_prog_name.sys_enter : NULL, "enter");
2996 if (pair_prog == trace->syscalls.unaugmented_prog)
2997 goto next_candidate;
2998 }
2999
3000 pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair->name, sc->name);
3001 return pair_prog;
3002 next_candidate:
3003 continue;
3004 }
3005
3006 return NULL;
3007}
3008
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03003009static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace)
3010{
3011 int map_enter_fd = bpf_map__fd(trace->syscalls.prog_array.sys_enter),
3012 map_exit_fd = bpf_map__fd(trace->syscalls.prog_array.sys_exit);
3013 int err = 0, key;
3014
3015 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
3016 int prog_fd;
3017
3018 if (!trace__syscall_enabled(trace, key))
3019 continue;
3020
3021 trace__init_syscall_bpf_progs(trace, key);
3022
3023 // It'll get at least the "!raw_syscalls:unaugmented"
3024 prog_fd = trace__bpf_prog_sys_enter_fd(trace, key);
3025 err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY);
3026 if (err)
3027 break;
3028 prog_fd = trace__bpf_prog_sys_exit_fd(trace, key);
3029 err = bpf_map_update_elem(map_exit_fd, &key, &prog_fd, BPF_ANY);
3030 if (err)
3031 break;
3032 }
3033
Arnaldo Carvalho de Meload4153f2019-07-17 18:27:33 -03003034 /*
3035 * Now lets do a second pass looking for enabled syscalls without
3036 * an augmenter that have a signature that is a superset of another
3037 * syscall with an augmenter so that we can auto-reuse it.
3038 *
3039 * I.e. if we have an augmenter for the "open" syscall that has
3040 * this signature:
3041 *
3042 * int open(const char *pathname, int flags, mode_t mode);
3043 *
3044 * I.e. that will collect just the first string argument, then we
3045 * can reuse it for the 'creat' syscall, that has this signature:
3046 *
3047 * int creat(const char *pathname, mode_t mode);
3048 *
3049 * and for:
3050 *
3051 * int stat(const char *pathname, struct stat *statbuf);
3052 * int lstat(const char *pathname, struct stat *statbuf);
3053 *
3054 * Because the 'open' augmenter will collect the first arg as a string,
3055 * and leave alone all the other args, which already helps with
3056 * beautifying 'stat' and 'lstat''s pathname arg.
3057 *
3058 * Then, in time, when 'stat' gets an augmenter that collects both
3059 * first and second arg (this one on the raw_syscalls:sys_exit prog
3060 * array tail call, then that one will be used.
3061 */
3062 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
3063 struct syscall *sc = trace__syscall_info(trace, NULL, key);
3064 struct bpf_program *pair_prog;
3065 int prog_fd;
3066
3067 if (sc == NULL || sc->bpf_prog.sys_enter == NULL)
3068 continue;
3069
3070 /*
3071 * For now we're just reusing the sys_enter prog, and if it
3072 * already has an augmenter, we don't need to find one.
3073 */
3074 if (sc->bpf_prog.sys_enter != trace->syscalls.unaugmented_prog)
3075 continue;
3076
3077 /*
3078 * Look at all the other syscalls for one that has a signature
3079 * that is close enough that we can share:
3080 */
3081 pair_prog = trace__find_usable_bpf_prog_entry(trace, sc);
3082 if (pair_prog == NULL)
3083 continue;
3084
3085 sc->bpf_prog.sys_enter = pair_prog;
3086
3087 /*
3088 * Update the BPF_MAP_TYPE_PROG_SHARED for raw_syscalls:sys_enter
3089 * with the fd for the program we're reusing:
3090 */
3091 prog_fd = bpf_program__fd(sc->bpf_prog.sys_enter);
3092 err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY);
3093 if (err)
3094 break;
3095 }
3096
3097
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03003098 return err;
3099}
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003100#else
3101static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
3102{
3103 return 0;
3104}
3105
3106static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
3107{
3108 return 0;
3109}
Arnaldo Carvalho de Melo5834da72019-07-15 17:51:43 -03003110
3111static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace __maybe_unused,
3112 const char *name __maybe_unused)
3113{
3114 return NULL;
3115}
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03003116
3117static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace __maybe_unused)
3118{
3119 return 0;
3120}
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003121#endif // HAVE_LIBBPF_SUPPORT
3122
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03003123static int trace__set_ev_qualifier_filter(struct trace *trace)
3124{
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003125 if (trace->syscalls.map)
3126 return trace__set_ev_qualifier_bpf_filter(trace);
Arnaldo Carvalho de Melof76214f2018-12-19 10:01:20 -03003127 if (trace->syscalls.events.sys_enter)
3128 return trace__set_ev_qualifier_tp_filter(trace);
3129 return 0;
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03003130}
3131
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03003132static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
3133 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
3134{
3135 int err = 0;
3136#ifdef HAVE_LIBBPF_SUPPORT
3137 bool value = true;
3138 int map_fd = bpf_map__fd(map);
3139 size_t i;
3140
3141 for (i = 0; i < npids; ++i) {
3142 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
3143 if (err)
3144 break;
3145 }
3146#endif
3147 return err;
3148}
3149
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03003150static int trace__set_filter_loop_pids(struct trace *trace)
3151{
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03003152 unsigned int nr = 1, err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03003153 pid_t pids[32] = {
3154 getpid(),
3155 };
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03003156 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
3157
3158 while (thread && nr < ARRAY_SIZE(pids)) {
3159 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
3160
3161 if (parent == NULL)
3162 break;
3163
Arnaldo Carvalho de Meloca2da702019-02-12 13:51:49 -03003164 if (!strcmp(thread__comm_str(parent), "sshd") ||
3165 strstarts(thread__comm_str(parent), "gnome-terminal")) {
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03003166 pids[nr++] = parent->tid;
3167 break;
3168 }
3169 thread = parent;
3170 }
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03003171
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03003172 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
3173 if (!err && trace->filter_pids.map)
3174 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
3175
3176 return err;
3177}
3178
3179static int trace__set_filter_pids(struct trace *trace)
3180{
3181 int err = 0;
3182 /*
3183 * Better not use !target__has_task() here because we need to cover the
3184 * case where no threads were specified in the command line, but a
3185 * workload was, and in that case we will fill in the thread_map when
3186 * we fork the workload in perf_evlist__prepare_workload.
3187 */
3188 if (trace->filter_pids.nr > 0) {
3189 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
3190 trace->filter_pids.entries);
3191 if (!err && trace->filter_pids.map) {
3192 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
3193 trace->filter_pids.entries);
3194 }
Jiri Olsaa2f354e2019-08-22 13:11:41 +02003195 } else if (perf_thread_map__pid(trace->evlist->core.threads, 0) == -1) {
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03003196 err = trace__set_filter_loop_pids(trace);
3197 }
3198
3199 return err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03003200}
3201
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003202static int __trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa1f44b3e2018-12-05 17:05:08 +01003203{
Jiri Olsa63503db2019-07-21 13:23:52 +02003204 struct evlist *evlist = trace->evlist;
Jiri Olsa1f44b3e2018-12-05 17:05:08 +01003205 struct perf_sample sample;
3206 int err;
3207
3208 err = perf_evlist__parse_sample(evlist, event, &sample);
3209 if (err)
3210 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
3211 else
3212 trace__handle_event(trace, event, &sample);
3213
3214 return 0;
3215}
3216
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003217static int __trace__flush_events(struct trace *trace)
Jiri Olsa028713a2018-12-05 17:05:09 +01003218{
3219 u64 first = ordered_events__first_time(&trace->oe.data);
3220 u64 flush = trace->oe.last - NSEC_PER_SEC;
3221
3222 /* Is there some thing to flush.. */
3223 if (first && first < flush)
3224 return ordered_events__flush_time(&trace->oe.data, flush);
3225
3226 return 0;
3227}
3228
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003229static int trace__flush_events(struct trace *trace)
3230{
3231 return !trace->sort_events ? 0 : __trace__flush_events(trace);
3232}
3233
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003234static int trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa028713a2018-12-05 17:05:09 +01003235{
Jiri Olsa028713a2018-12-05 17:05:09 +01003236 int err;
3237
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003238 if (!trace->sort_events)
3239 return __trace__deliver_event(trace, event);
3240
3241 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
Jiri Olsa028713a2018-12-05 17:05:09 +01003242 if (err && err != -1)
3243 return err;
3244
3245 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
3246 if (err)
3247 return err;
3248
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003249 return trace__flush_events(trace);
Jiri Olsa028713a2018-12-05 17:05:09 +01003250}
3251
3252static int ordered_events__deliver_event(struct ordered_events *oe,
3253 struct ordered_event *event)
3254{
3255 struct trace *trace = container_of(oe, struct trace, oe.data);
3256
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003257 return __trace__deliver_event(trace, event->event);
Jiri Olsa028713a2018-12-05 17:05:09 +01003258}
3259
Namhyung Kimf15eb532012-10-05 14:02:16 +09003260static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003261{
Jiri Olsa63503db2019-07-21 13:23:52 +02003262 struct evlist *evlist = trace->evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003263 struct evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003264 int err = -1, i;
3265 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003266 const bool forks = argc > 0;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003267 bool draining = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003268
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03003269 trace->live = true;
3270
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03003271 if (!trace->raw_augmented_syscalls) {
3272 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
3273 goto out_error_raw_syscalls;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003274
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03003275 if (trace->trace_syscalls)
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02003276 trace->vfs_getname = evlist__add_vfs_getname(evlist);
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03003277 }
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003278
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03003279 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
3280 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
3281 if (pgfault_maj == NULL)
3282 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03003283 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02003284 evlist__add(evlist, pgfault_maj);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03003285 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003286
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03003287 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
3288 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
3289 if (pgfault_min == NULL)
3290 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03003291 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02003292 evlist__add(evlist, pgfault_min);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03003293 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003294
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003295 if (trace->sched &&
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003296 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
3297 trace__sched_stat_runtime))
3298 goto out_error_sched_stat_runtime;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003299
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003300 /*
3301 * If a global cgroup was set, apply it to all the events without an
3302 * explicit cgroup. I.e.:
3303 *
3304 * trace -G A -e sched:*switch
3305 *
3306 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
3307 * _and_ sched:sched_switch to the 'A' cgroup, while:
3308 *
3309 * trace -e sched:*switch -G A
3310 *
3311 * will only set the sched:sched_switch event to the 'A' cgroup, all the
3312 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
3313 * a cgroup (on the root cgroup, sys wide, etc).
3314 *
3315 * Multiple cgroups:
3316 *
3317 * trace -G A -e sched:*switch -G B
3318 *
3319 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
3320 * to the 'B' cgroup.
3321 *
3322 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
3323 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
3324 */
3325 if (trace->cgroup)
3326 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
3327
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003328 err = perf_evlist__create_maps(evlist, &trace->opts.target);
3329 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003330 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003331 goto out_delete_evlist;
3332 }
3333
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03003334 err = trace__symbols_init(trace, evlist);
3335 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003336 fprintf(trace->output, "Problems initializing symbol libraries!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003337 goto out_delete_evlist;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03003338 }
3339
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003340 perf_evlist__config(evlist, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melofde54b72016-04-11 18:42:37 -03003341
Namhyung Kimf15eb532012-10-05 14:02:16 +09003342 signal(SIGCHLD, sig_handler);
3343 signal(SIGINT, sig_handler);
3344
3345 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09003346 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03003347 argv, false, NULL);
Namhyung Kimf15eb532012-10-05 14:02:16 +09003348 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003349 fprintf(trace->output, "Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003350 goto out_delete_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003351 }
3352 }
3353
Jiri Olsa474ddc42019-07-21 13:24:06 +02003354 err = evlist__open(evlist);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003355 if (err < 0)
3356 goto out_error_open;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003357
Wang Nanba504232016-02-26 09:31:54 +00003358 err = bpf__apply_obj_config();
3359 if (err) {
3360 char errbuf[BUFSIZ];
3361
3362 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
3363 pr_err("ERROR: Apply config to BPF failed: %s\n",
3364 errbuf);
3365 goto out_error_open;
3366 }
3367
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03003368 err = trace__set_filter_pids(trace);
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003369 if (err < 0)
3370 goto out_error_mem;
3371
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003372 if (trace->syscalls.map)
3373 trace__init_syscalls_bpf_map(trace);
3374
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03003375 if (trace->syscalls.prog_array.sys_enter)
3376 trace__init_syscalls_bpf_prog_array_maps(trace);
3377
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003378 if (trace->ev_qualifier_ids.nr > 0) {
3379 err = trace__set_ev_qualifier_filter(trace);
3380 if (err < 0)
3381 goto out_errno;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003382
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03003383 if (trace->syscalls.events.sys_exit) {
3384 pr_debug("event qualifier tracepoint filter: %s\n",
3385 trace->syscalls.events.sys_exit->filter);
3386 }
Arnaldo Carvalho de Melo2e5e5f82015-08-03 17:12:29 -03003387 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003388
Arnaldo Carvalho de Melo79d725c2019-07-16 16:56:49 -03003389 /*
3390 * If the "close" syscall is not traced, then we will not have the
3391 * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
3392 * fd->pathname table and were ending up showing the last value set by
3393 * syscalls opening a pathname and associating it with a descriptor or
3394 * reading it from /proc/pid/fd/ in cases where that doesn't make
3395 * sense.
3396 *
3397 * So just disable this beautifier (SCA_FD, SCA_FDAT) when 'close' is
3398 * not in use.
3399 */
3400 trace->fd_path_disabled = !trace__syscall_enabled(trace, syscalltbl__id(trace->sctbl, "close"));
3401
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003402 err = perf_evlist__apply_filters(evlist, &evsel);
3403 if (err < 0)
3404 goto out_error_apply_filters;
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08003405
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03003406 if (trace->dump.map)
3407 bpf_map__fprintf(trace->dump.map, trace->output);
3408
Wang Nanf74b9d3a2017-12-03 02:00:37 +00003409 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03003410 if (err < 0)
3411 goto out_error_mmap;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003412
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003413 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
Jiri Olsa1c87f162019-07-21 13:24:08 +02003414 evlist__enable(evlist);
Arnaldo Carvalho de Melocb24d012015-04-22 10:04:23 -03003415
Namhyung Kimf15eb532012-10-05 14:02:16 +09003416 if (forks)
3417 perf_evlist__start_workload(evlist);
3418
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003419 if (trace->opts.initial_delay) {
3420 usleep(trace->opts.initial_delay * 1000);
Jiri Olsa1c87f162019-07-21 13:24:08 +02003421 evlist__enable(evlist);
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003422 }
3423
Jiri Olsaa2f354e2019-08-22 13:11:41 +02003424 trace->multiple_threads = perf_thread_map__pid(evlist->core.threads, 0) == -1 ||
Jiri Olsa03617c22019-07-21 13:24:42 +02003425 evlist->core.threads->nr > 1 ||
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003426 perf_evlist__first(evlist)->core.attr.inherit;
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03003427
3428 /*
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003429 * Now that we already used evsel->core.attr to ask the kernel to setup the
3430 * events, lets reuse evsel->core.attr.sample_max_stack as the limit in
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03003431 * trace__resolve_callchain(), allowing per-event max-stack settings
Ingo Molnaradba1632018-12-03 11:22:00 +01003432 * to override an explicitly set --max-stack global setting.
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03003433 */
3434 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03003435 if (evsel__has_callchain(evsel) &&
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003436 evsel->core.attr.sample_max_stack == 0)
3437 evsel->core.attr.sample_max_stack = trace->max_stack;
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03003438 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003439again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003440 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003441
3442 for (i = 0; i < evlist->nr_mmaps; i++) {
3443 union perf_event *event;
Kan Liangd7f55c62018-03-01 18:08:59 -05003444 struct perf_mmap *md;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003445
Kan Liangd7f55c62018-03-01 18:08:59 -05003446 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -05003447 if (perf_mmap__read_init(md) < 0)
Kan Liangd7f55c62018-03-01 18:08:59 -05003448 continue;
3449
Kan Liang0019dc872018-03-06 10:36:06 -05003450 while ((event = perf_mmap__read_event(md)) != NULL) {
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003451 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003452
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003453 err = trace__deliver_event(trace, event);
Jiri Olsa028713a2018-12-05 17:05:09 +01003454 if (err)
3455 goto out_disable;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003456
Kan Liangd6ace3d2018-03-06 10:36:05 -05003457 perf_mmap__consume(md);
Arnaldo Carvalho de Melo20c5f102013-09-03 11:55:07 -03003458
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003459 if (interrupted)
3460 goto out_disable;
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03003461
3462 if (done && !draining) {
Jiri Olsae74676d2019-07-21 13:24:09 +02003463 evlist__disable(evlist);
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03003464 draining = true;
3465 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003466 }
Kan Liangd7f55c62018-03-01 18:08:59 -05003467 perf_mmap__read_done(md);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003468 }
3469
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003470 if (trace->nr_events == before) {
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003471 int timeout = done ? 100 : -1;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003472
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003473 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03003474 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003475 draining = true;
3476
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003477 goto again;
Jiri Olsa028713a2018-12-05 17:05:09 +01003478 } else {
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003479 if (trace__flush_events(trace))
Jiri Olsa028713a2018-12-05 17:05:09 +01003480 goto out_disable;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003481 }
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003482 } else {
3483 goto again;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003484 }
3485
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003486out_disable:
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03003487 thread__zput(trace->current);
3488
Jiri Olsae74676d2019-07-21 13:24:09 +02003489 evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003490
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003491 if (trace->sort_events)
3492 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
Jiri Olsa028713a2018-12-05 17:05:09 +01003493
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003494 if (!err) {
3495 if (trace->summary)
3496 trace__fprintf_thread_summary(trace, trace->output);
3497
3498 if (trace->show_tool_stats) {
3499 fprintf(trace->output, "Stats:\n "
3500 " vfs_getname : %" PRIu64 "\n"
3501 " proc_getname: %" PRIu64 "\n",
3502 trace->stats.vfs_getname,
3503 trace->stats.proc_getname);
3504 }
3505 }
David Ahernbf2575c2013-10-08 21:26:53 -06003506
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003507out_delete_evlist:
Andrei Vagin33974a42017-11-07 16:22:45 -08003508 trace__symbols__exit(trace);
3509
Jiri Olsac12995a2019-07-21 13:23:56 +02003510 evlist__delete(evlist);
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003511 cgroup__put(trace->cgroup);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003512 trace->evlist = NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03003513 trace->live = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003514 return err;
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003515{
3516 char errbuf[BUFSIZ];
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003517
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003518out_error_sched_stat_runtime:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003519 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003520 goto out_error;
3521
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03003522out_error_raw_syscalls:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003523 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003524 goto out_error;
3525
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03003526out_error_mmap:
3527 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3528 goto out_error;
3529
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003530out_error_open:
3531 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3532
3533out_error:
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003534 fprintf(trace->output, "%s\n", errbuf);
Ramkumar Ramachandra87f91862013-10-04 10:47:31 +05303535 goto out_delete_evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003536
3537out_error_apply_filters:
3538 fprintf(trace->output,
3539 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3540 evsel->filter, perf_evsel__name(evsel), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03003541 str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003542 goto out_delete_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003543}
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03003544out_error_mem:
3545 fprintf(trace->output, "Not enough memory to run!\n");
3546 goto out_delete_evlist;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003547
3548out_errno:
3549 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3550 goto out_delete_evlist;
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003551}
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003552
David Ahern6810fc92013-08-28 22:29:52 -06003553static int trace__replay(struct trace *trace)
3554{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003555 const struct evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003556 { "probe:vfs_getname", trace__vfs_getname, },
David Ahern6810fc92013-08-28 22:29:52 -06003557 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003558 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003559 .path = input_name,
3560 .mode = PERF_DATA_MODE_READ,
3561 .force = trace->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003562 };
David Ahern6810fc92013-08-28 22:29:52 -06003563 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003564 struct evsel *evsel;
David Ahern6810fc92013-08-28 22:29:52 -06003565 int err = -1;
3566
3567 trace->tool.sample = trace__process_sample;
3568 trace->tool.mmap = perf_event__process_mmap;
David Ahern384c6712013-09-22 19:44:58 -06003569 trace->tool.mmap2 = perf_event__process_mmap2;
David Ahern6810fc92013-08-28 22:29:52 -06003570 trace->tool.comm = perf_event__process_comm;
3571 trace->tool.exit = perf_event__process_exit;
3572 trace->tool.fork = perf_event__process_fork;
3573 trace->tool.attr = perf_event__process_attr;
Hari Bathinif3b36142017-03-08 02:11:43 +05303574 trace->tool.tracing_data = perf_event__process_tracing_data;
David Ahern6810fc92013-08-28 22:29:52 -06003575 trace->tool.build_id = perf_event__process_build_id;
Hari Bathinif3b36142017-03-08 02:11:43 +05303576 trace->tool.namespaces = perf_event__process_namespaces;
David Ahern6810fc92013-08-28 22:29:52 -06003577
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003578 trace->tool.ordered_events = true;
David Ahern6810fc92013-08-28 22:29:52 -06003579 trace->tool.ordering_requires_timestamps = true;
3580
3581 /* add tid to output */
3582 trace->multiple_threads = true;
3583
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003584 session = perf_session__new(&data, false, &trace->tool);
David Ahern6810fc92013-08-28 22:29:52 -06003585 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003586 return -1;
David Ahern6810fc92013-08-28 22:29:52 -06003587
David Ahernaa07df62016-11-25 09:29:52 -07003588 if (trace->opts.target.pid)
3589 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3590
3591 if (trace->opts.target.tid)
3592 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3593
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003594 if (symbol__init(&session->header.env) < 0)
Namhyung Kimcb2ffae2014-08-12 15:40:44 +09003595 goto out;
3596
David Ahern8fb598e2013-09-28 13:13:00 -06003597 trace->host = &session->machines.host;
3598
David Ahern6810fc92013-08-28 22:29:52 -06003599 err = perf_session__set_tracepoints_handlers(session, handlers);
3600 if (err)
3601 goto out;
3602
Namhyung Kim003824e2013-11-12 15:25:00 +09003603 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3604 "raw_syscalls:sys_enter");
David Ahern9aca7f12013-12-04 19:41:39 -07003605 /* older kernels have syscalls tp versus raw_syscalls */
3606 if (evsel == NULL)
3607 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3608 "syscalls:sys_enter");
David Ahern6810fc92013-08-28 22:29:52 -06003609
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003610 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003611 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003612 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003613 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3614 goto out;
3615 }
3616
3617 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3618 "raw_syscalls:sys_exit");
David Ahern9aca7f12013-12-04 19:41:39 -07003619 if (evsel == NULL)
3620 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3621 "syscalls:sys_exit");
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003622 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003623 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003624 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003625 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
David Ahern6810fc92013-08-28 22:29:52 -06003626 goto out;
3627 }
3628
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003629 evlist__for_each_entry(session->evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003630 if (evsel->core.attr.type == PERF_TYPE_SOFTWARE &&
3631 (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3632 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3633 evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS))
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003634 evsel->handler = trace__pgfault;
3635 }
3636
David Ahern6810fc92013-08-28 22:29:52 -06003637 setup_pager();
3638
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03003639 err = perf_session__process_events(session);
David Ahern6810fc92013-08-28 22:29:52 -06003640 if (err)
3641 pr_err("Failed to process events, error %d", err);
3642
David Ahernbf2575c2013-10-08 21:26:53 -06003643 else if (trace->summary)
3644 trace__fprintf_thread_summary(trace, trace->output);
3645
David Ahern6810fc92013-08-28 22:29:52 -06003646out:
3647 perf_session__delete(session);
3648
3649 return err;
3650}
3651
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003652static size_t trace__fprintf_threads_header(FILE *fp)
3653{
3654 size_t printed;
3655
Pekka Enberg99ff7152013-11-12 16:42:14 +02003656 printed = fprintf(fp, "\n Summary of events:\n\n");
David Ahernbf2575c2013-10-08 21:26:53 -06003657
3658 return printed;
3659}
3660
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003661DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3662 struct stats *stats;
3663 double msecs;
3664 int syscall;
3665)
3666{
3667 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3668 struct stats *stats = source->priv;
3669
3670 entry->syscall = source->i;
3671 entry->stats = stats;
3672 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3673}
3674
David Ahernbf2575c2013-10-08 21:26:53 -06003675static size_t thread__dump_stats(struct thread_trace *ttrace,
3676 struct trace *trace, FILE *fp)
3677{
David Ahernbf2575c2013-10-08 21:26:53 -06003678 size_t printed = 0;
3679 struct syscall *sc;
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003680 struct rb_node *nd;
3681 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003682
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003683 if (syscall_stats == NULL)
David Ahernbf2575c2013-10-08 21:26:53 -06003684 return 0;
3685
3686 printed += fprintf(fp, "\n");
3687
Milian Wolff834fd462015-08-06 11:24:29 +02003688 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3689 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3690 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
Pekka Enberg99ff7152013-11-12 16:42:14 +02003691
Arnaldo Carvalho de Melo98a91832016-06-23 11:34:10 -03003692 resort_rb__for_each_entry(nd, syscall_stats) {
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003693 struct stats *stats = syscall_stats_entry->stats;
David Ahernbf2575c2013-10-08 21:26:53 -06003694 if (stats) {
3695 double min = (double)(stats->min) / NSEC_PER_MSEC;
3696 double max = (double)(stats->max) / NSEC_PER_MSEC;
3697 double avg = avg_stats(stats);
3698 double pct;
3699 u64 n = (u64) stats->n;
3700
3701 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3702 avg /= NSEC_PER_MSEC;
3703
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003704 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
Pekka Enberg99ff7152013-11-12 16:42:14 +02003705 printed += fprintf(fp, " %-15s", sc->name);
Milian Wolff834fd462015-08-06 11:24:29 +02003706 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003707 n, syscall_stats_entry->msecs, min, avg);
Pekka Enberg27a778b2013-11-13 14:21:48 +02003708 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
David Ahernbf2575c2013-10-08 21:26:53 -06003709 }
David Ahernbf2575c2013-10-08 21:26:53 -06003710 }
3711
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003712 resort_rb__delete(syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003713 printed += fprintf(fp, "\n\n");
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003714
3715 return printed;
3716}
3717
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003718static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
David Ahern896cbb52013-09-28 13:12:59 -06003719{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003720 size_t printed = 0;
Namhyung Kim89dceb22014-10-06 09:46:03 +09003721 struct thread_trace *ttrace = thread__priv(thread);
David Ahern896cbb52013-09-28 13:12:59 -06003722 double ratio;
3723
3724 if (ttrace == NULL)
3725 return 0;
3726
3727 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3728
Pekka Enberg15e65c62013-11-14 18:43:30 +02003729 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
Pekka Enberg99ff7152013-11-12 16:42:14 +02003730 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
Pekka Enberg15e65c62013-11-14 18:43:30 +02003731 printed += fprintf(fp, "%.1f%%", ratio);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04003732 if (ttrace->pfmaj)
3733 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3734 if (ttrace->pfmin)
3735 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
Arnaldo Carvalho de Melo03548eb2016-05-05 15:46:50 -03003736 if (trace->sched)
3737 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3738 else if (fputc('\n', fp) != EOF)
3739 ++printed;
3740
David Ahernbf2575c2013-10-08 21:26:53 -06003741 printed += thread__dump_stats(ttrace, trace, fp);
David Ahern896cbb52013-09-28 13:12:59 -06003742
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003743 return printed;
3744}
David Ahern896cbb52013-09-28 13:12:59 -06003745
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003746static unsigned long thread__nr_events(struct thread_trace *ttrace)
3747{
3748 return ttrace ? ttrace->nr_events : 0;
3749}
3750
3751DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3752 struct thread *thread;
3753)
3754{
3755 entry->thread = rb_entry(nd, struct thread, rb_node);
David Ahern896cbb52013-09-28 13:12:59 -06003756}
3757
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003758static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3759{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003760 size_t printed = trace__fprintf_threads_header(fp);
3761 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -07003762 int i;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003763
Kan Liang91e467b2017-09-10 19:23:14 -07003764 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3765 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3766
3767 if (threads == NULL) {
3768 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3769 return 0;
3770 }
3771
3772 resort_rb__for_each_entry(nd, threads)
3773 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3774
3775 resort_rb__delete(threads);
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003776 }
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003777 return printed;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003778}
3779
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003780static int trace__set_duration(const struct option *opt, const char *str,
3781 int unset __maybe_unused)
3782{
3783 struct trace *trace = opt->value;
3784
3785 trace->duration_filter = atof(str);
3786 return 0;
3787}
3788
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003789static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3790 int unset __maybe_unused)
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08003791{
3792 int ret = -1;
3793 size_t i;
3794 struct trace *trace = opt->value;
3795 /*
3796 * FIXME: introduce a intarray class, plain parse csv and create a
3797 * { int nr, int entries[] } struct...
3798 */
3799 struct intlist *list = intlist__new(str);
3800
3801 if (list == NULL)
3802 return -1;
3803
3804 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3805 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3806
3807 if (trace->filter_pids.entries == NULL)
3808 goto out;
3809
3810 trace->filter_pids.entries[0] = getpid();
3811
3812 for (i = 1; i < trace->filter_pids.nr; ++i)
3813 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3814
3815 intlist__delete(list);
3816 ret = 0;
3817out:
3818 return ret;
3819}
3820
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003821static int trace__open_output(struct trace *trace, const char *filename)
3822{
3823 struct stat st;
3824
3825 if (!stat(filename, &st) && st.st_size) {
3826 char oldname[PATH_MAX];
3827
3828 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3829 unlink(oldname);
3830 rename(filename, oldname);
3831 }
3832
3833 trace->output = fopen(filename, "w");
3834
3835 return trace->output == NULL ? -errno : 0;
3836}
3837
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003838static int parse_pagefaults(const struct option *opt, const char *str,
3839 int unset __maybe_unused)
3840{
3841 int *trace_pgfaults = opt->value;
3842
3843 if (strcmp(str, "all") == 0)
3844 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3845 else if (strcmp(str, "maj") == 0)
3846 *trace_pgfaults |= TRACE_PFMAJ;
3847 else if (strcmp(str, "min") == 0)
3848 *trace_pgfaults |= TRACE_PFMIN;
3849 else
3850 return -1;
3851
3852 return 0;
3853}
3854
Jiri Olsa63503db2019-07-21 13:23:52 +02003855static void evlist__set_evsel_handler(struct evlist *evlist, void *handler)
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003856{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003857 struct evsel *evsel;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003858
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003859 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003860 evsel->handler = handler;
3861}
3862
Jiri Olsa63503db2019-07-21 13:23:52 +02003863static int evlist__set_syscall_tp_fields(struct evlist *evlist)
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003864{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003865 struct evsel *evsel;
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003866
3867 evlist__for_each_entry(evlist, evsel) {
3868 if (evsel->priv || !evsel->tp_format)
3869 continue;
3870
3871 if (strcmp(evsel->tp_format->system, "syscalls"))
3872 continue;
3873
3874 if (perf_evsel__init_syscall_tp(evsel))
3875 return -1;
3876
3877 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3878 struct syscall_tp *sc = evsel->priv;
3879
3880 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3881 return -1;
3882 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3883 struct syscall_tp *sc = evsel->priv;
3884
3885 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3886 return -1;
3887 }
3888 }
3889
3890 return 0;
3891}
3892
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003893/*
3894 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3895 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3896 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3897 *
3898 * It'd be better to introduce a parse_options() variant that would return a
3899 * list with the terms it didn't match to an event...
3900 */
3901static int trace__parse_events_option(const struct option *opt, const char *str,
3902 int unset __maybe_unused)
3903{
3904 struct trace *trace = (struct trace *)opt->value;
3905 const char *s = str;
3906 char *sep = NULL, *lists[2] = { NULL, NULL, };
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003907 int len = strlen(str) + 1, err = -1, list, idx;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003908 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3909 char group_name[PATH_MAX];
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003910 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003911
3912 if (strace_groups_dir == NULL)
3913 return -1;
3914
3915 if (*s == '!') {
3916 ++s;
3917 trace->not_ev_qualifier = true;
3918 }
3919
3920 while (1) {
3921 if ((sep = strchr(s, ',')) != NULL)
3922 *sep = '\0';
3923
3924 list = 0;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003925 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3926 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003927 list = 1;
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003928 goto do_concat;
3929 }
3930
3931 fmt = syscall_fmt__find_by_alias(s);
3932 if (fmt != NULL) {
3933 list = 1;
3934 s = fmt->name;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003935 } else {
3936 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3937 if (access(group_name, R_OK) == 0)
3938 list = 1;
3939 }
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003940do_concat:
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003941 if (lists[list]) {
3942 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3943 } else {
3944 lists[list] = malloc(len);
3945 if (lists[list] == NULL)
3946 goto out;
3947 strcpy(lists[list], s);
3948 }
3949
3950 if (!sep)
3951 break;
3952
3953 *sep = ',';
3954 s = sep + 1;
3955 }
3956
3957 if (lists[1] != NULL) {
3958 struct strlist_config slist_config = {
3959 .dirname = strace_groups_dir,
3960 };
3961
3962 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3963 if (trace->ev_qualifier == NULL) {
3964 fputs("Not enough memory to parse event qualifier", trace->output);
3965 goto out;
3966 }
3967
3968 if (trace__validate_ev_qualifier(trace))
3969 goto out;
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003970 trace->trace_syscalls = true;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003971 }
3972
3973 err = 0;
3974
3975 if (lists[0]) {
3976 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3977 "event selector. use 'perf list' to list available events",
3978 parse_events_option);
3979 err = parse_events_option(&o, lists[0], 0);
3980 }
3981out:
3982 if (sep)
3983 *sep = ',';
3984
3985 return err;
3986}
3987
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003988static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3989{
3990 struct trace *trace = opt->value;
3991
Jiri Olsace9036a2019-07-21 13:24:23 +02003992 if (!list_empty(&trace->evlist->core.entries))
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003993 return parse_cgroups(opt, str, unset);
3994
3995 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3996
3997 return 0;
3998}
3999
Arnaldo Carvalho de Melo5ca0b7f2019-07-15 17:03:10 -03004000static struct bpf_map *trace__find_bpf_map_by_name(struct trace *trace, const char *name)
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03004001{
Arnaldo Carvalho de Melo5ca0b7f2019-07-15 17:03:10 -03004002 if (trace->bpf_obj == NULL)
4003 return NULL;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03004004
Arnaldo Carvalho de Melo5ca0b7f2019-07-15 17:03:10 -03004005 return bpf_object__find_map_by_name(trace->bpf_obj, name);
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03004006}
4007
4008static void trace__set_bpf_map_filtered_pids(struct trace *trace)
4009{
Arnaldo Carvalho de Melo5ca0b7f2019-07-15 17:03:10 -03004010 trace->filter_pids.map = trace__find_bpf_map_by_name(trace, "pids_filtered");
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03004011}
4012
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03004013static void trace__set_bpf_map_syscalls(struct trace *trace)
4014{
Arnaldo Carvalho de Melo5ca0b7f2019-07-15 17:03:10 -03004015 trace->syscalls.map = trace__find_bpf_map_by_name(trace, "syscalls");
Arnaldo Carvalho de Melo3803a222019-07-16 11:27:03 -03004016 trace->syscalls.prog_array.sys_enter = trace__find_bpf_map_by_name(trace, "syscalls_sys_enter");
4017 trace->syscalls.prog_array.sys_exit = trace__find_bpf_map_by_name(trace, "syscalls_sys_exit");
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03004018}
4019
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03004020static int trace__config(const char *var, const char *value, void *arg)
4021{
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03004022 struct trace *trace = arg;
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03004023 int err = 0;
4024
4025 if (!strcmp(var, "trace.add_events")) {
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03004026 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
4027 "event selector. use 'perf list' to list available events",
4028 parse_events_option);
Leo Yan012749ca2019-06-06 10:38:59 -03004029 /*
4030 * We can't propagate parse_event_option() return, as it is 1
4031 * for failure while perf_config() expects -1.
4032 */
4033 if (parse_events_option(&o, value, 0))
4034 err = -1;
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03004035 } else if (!strcmp(var, "trace.show_timestamp")) {
4036 trace->show_tstamp = perf_config_bool(var, value);
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03004037 } else if (!strcmp(var, "trace.show_duration")) {
4038 trace->show_duration = perf_config_bool(var, value);
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03004039 } else if (!strcmp(var, "trace.show_arg_names")) {
4040 trace->show_arg_names = perf_config_bool(var, value);
4041 if (!trace->show_arg_names)
4042 trace->show_zeros = true;
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03004043 } else if (!strcmp(var, "trace.show_zeros")) {
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03004044 bool new_show_zeros = perf_config_bool(var, value);
4045 if (!trace->show_arg_names && !new_show_zeros) {
4046 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
4047 goto out;
4048 }
4049 trace->show_zeros = new_show_zeros;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -03004050 } else if (!strcmp(var, "trace.show_prefix")) {
4051 trace->show_string_prefix = perf_config_bool(var, value);
Arnaldo Carvalho de Melod32de872018-12-14 11:06:16 -03004052 } else if (!strcmp(var, "trace.no_inherit")) {
4053 trace->opts.no_inherit = perf_config_bool(var, value);
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -03004054 } else if (!strcmp(var, "trace.args_alignment")) {
4055 int args_alignment = 0;
4056 if (perf_config_int(&args_alignment, var, value) == 0)
4057 trace->args_alignment = args_alignment;
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03004058 }
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03004059out:
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03004060 return err;
4061}
4062
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03004063int cmd_trace(int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004064{
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08004065 const char *trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +09004066 "perf trace [<options>] [<command>]",
4067 "perf trace [<options>] -- <command> [<options>]",
David Ahern5e2485b2013-09-28 13:13:01 -06004068 "perf trace record [<options>] [<command>]",
4069 "perf trace record [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004070 NULL
4071 };
4072 struct trace trace = {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004073 .opts = {
4074 .target = {
4075 .uid = UINT_MAX,
4076 .uses_mmap = true,
4077 },
4078 .user_freq = UINT_MAX,
4079 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03004080 .no_buffering = true,
Arnaldo Carvalho de Melo38d54472014-12-12 17:28:32 -03004081 .mmap_pages = UINT_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004082 },
Milian Wolff007d66a2015-08-05 16:52:23 -03004083 .output = stderr,
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03004084 .show_comm = true,
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03004085 .show_tstamp = true,
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03004086 .show_duration = true,
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03004087 .show_arg_names = true,
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -03004088 .args_alignment = 70,
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03004089 .trace_syscalls = false,
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03004090 .kernel_syscallchains = false,
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03004091 .max_stack = UINT_MAX,
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03004092 .max_events = ULONG_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004093 };
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03004094 const char *map_dump_str = NULL;
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004095 const char *output_name = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004096 const struct option trace_options[] = {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03004097 OPT_CALLBACK('e', "event", &trace, "event",
4098 "event/syscall selector. use 'perf list' to list available events",
4099 trace__parse_events_option),
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03004100 OPT_BOOLEAN(0, "comm", &trace.show_comm,
4101 "show the thread COMM next to its id"),
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03004102 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03004103 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
4104 trace__parse_events_option),
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004105 OPT_STRING('o', "output", &output_name, "file", "output file name"),
David Ahern6810fc92013-08-28 22:29:52 -06004106 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004107 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
4108 "trace events on existing process id"),
David Ahernac9be8e2013-08-20 11:15:45 -06004109 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004110 "trace events on existing thread id"),
Arnaldo Carvalho de Melofa0e4ff2015-04-23 11:59:20 -03004111 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03004112 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
David Ahernac9be8e2013-08-20 11:15:45 -06004113 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004114 "system-wide collection from all CPUs"),
David Ahernac9be8e2013-08-20 11:15:45 -06004115 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004116 "list of cpus to monitor"),
David Ahern6810fc92013-08-28 22:29:52 -06004117 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004118 "child tasks do not inherit counters"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02004119 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
4120 "number of mmap data pages",
4121 perf_evlist__parse_mmap_pages),
David Ahernac9be8e2013-08-20 11:15:45 -06004122 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004123 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03004124 OPT_CALLBACK(0, "duration", &trace, "float",
4125 "show only events with duration > N.M ms",
4126 trace__set_duration),
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03004127#ifdef HAVE_LIBBPF_SUPPORT
4128 OPT_STRING(0, "map-dump", &map_dump_str, "BPF map", "BPF map to periodically dump"),
4129#endif
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03004130 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03004131 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
David Ahern4bb09192013-09-04 12:37:43 -06004132 OPT_BOOLEAN('T', "time", &trace.full_time,
4133 "Show full timestamp, not time relative to first start"),
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03004134 OPT_BOOLEAN(0, "failure", &trace.failure_only,
4135 "Show only syscalls that failed"),
David Ahernfd2eaba2013-11-12 09:31:15 -07004136 OPT_BOOLEAN('s', "summary", &trace.summary_only,
4137 "Show only syscall summary with statistics"),
4138 OPT_BOOLEAN('S', "with-summary", &trace.summary,
4139 "Show all syscalls and summary with statistics"),
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04004140 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
4141 "Trace pagefaults", parse_pagefaults, "maj"),
Stanislav Fomicheve281a962014-06-26 20:14:28 +04004142 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
Yunlong Songe366a6d2015-04-02 21:47:18 +08004143 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
Milian Wolff566a0882016-04-08 13:34:15 +02004144 OPT_CALLBACK(0, "call-graph", &trace.opts,
4145 "record_mode[,record_size]", record_callchain_help,
4146 &record_parse_callchain_opt),
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03004147 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
4148 "Show the kernel callchains on the syscall exit path"),
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03004149 OPT_ULONG(0, "max-events", &trace.max_events,
4150 "Set the maximum number of events to print, exit after that is reached. "),
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03004151 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
4152 "Set the minimum stack depth when parsing the callchain, "
4153 "anything below the specified depth will be ignored."),
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -03004154 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
4155 "Set the maximum stack depth when parsing the callchain, "
4156 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03004157 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03004158 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
4159 "Sort batch of events before processing, use if getting out of order events"),
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03004160 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
4161 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08004162 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04004163 "per thread proc mmap processing timeout in ms"),
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03004164 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
4165 trace__parse_cgroups),
Alexis Berlemonte36b7822016-10-10 07:43:28 +02004166 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
4167 "ms to wait before starting measurement after program "
4168 "start"),
Arnaldo Carvalho de Melo22ac4312019-08-15 12:15:39 -03004169 OPTS_EVSWITCH(&trace.evswitch),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004170 OPT_END()
4171 };
Arnaldo Carvalho de Meloccd62a82016-04-16 09:36:32 -03004172 bool __maybe_unused max_stack_user_set = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03004173 bool mmap_pages_user_set = true;
Jiri Olsa32dcd022019-07-21 13:23:51 +02004174 struct evsel *evsel;
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08004175 const char * const trace_subcommands[] = { "record", NULL };
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03004176 int err = -1;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09004177 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004178
Arnaldo Carvalho de Melo4d08cb82015-02-24 15:35:55 -03004179 signal(SIGSEGV, sighandler_dump_stack);
4180 signal(SIGFPE, sighandler_dump_stack);
4181
Jiri Olsa0f98b112019-07-21 13:23:55 +02004182 trace.evlist = evlist__new();
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03004183 trace.sctbl = syscalltbl__new();
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03004184
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03004185 if (trace.evlist == NULL || trace.sctbl == NULL) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03004186 pr_err("Not enough memory to run!\n");
He Kuangff8f6952015-05-11 12:28:36 +00004187 err = -ENOMEM;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03004188 goto out;
4189 }
4190
Arnaldo Carvalho de Meloc3e78a32019-07-09 16:36:45 -03004191 /*
4192 * Parsing .perfconfig may entail creating a BPF event, that may need
4193 * to create BPF maps, so bump RLIM_MEMLOCK as the default 64K setting
4194 * is too small. This affects just this process, not touching the
4195 * global setting. If it fails we'll get something in 'perf trace -v'
4196 * to help diagnose the problem.
4197 */
4198 rlimit__bump_memlock();
4199
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03004200 err = perf_config(trace__config, &trace);
4201 if (err)
4202 goto out;
4203
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08004204 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
4205 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
David Ahernfd2eaba2013-11-12 09:31:15 -07004206
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03004207 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
4208 usage_with_options_msg(trace_usage, trace_options,
4209 "cgroup monitoring only available in system-wide mode");
4210 }
4211
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03004212 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
4213 if (IS_ERR(evsel)) {
4214 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
Arnaldo Carvalho de Meloe0b6d2e2018-08-07 15:40:13 -03004215 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
4216 goto out;
4217 }
4218
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03004219 if (evsel) {
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03004220 trace.syscalls.events.augmented = evsel;
Arnaldo Carvalho de Meloc8c80572019-07-15 16:58:23 -03004221
4222 evsel = perf_evlist__find_tracepoint_by_name(trace.evlist, "raw_syscalls:sys_enter");
4223 if (evsel == NULL) {
4224 pr_err("ERROR: raw_syscalls:sys_enter not found in the augmented BPF object\n");
4225 goto out;
4226 }
4227
4228 if (evsel->bpf_obj == NULL) {
4229 pr_err("ERROR: raw_syscalls:sys_enter not associated to a BPF object\n");
4230 goto out;
4231 }
4232
4233 trace.bpf_obj = evsel->bpf_obj;
4234
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03004235 trace__set_bpf_map_filtered_pids(&trace);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03004236 trace__set_bpf_map_syscalls(&trace);
Arnaldo Carvalho de Melo5834da72019-07-15 17:51:43 -03004237 trace.syscalls.unaugmented_prog = trace__find_bpf_program_by_title(&trace, "!raw_syscalls:unaugmented");
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03004238 }
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03004239
Wang Nand7888572016-04-08 15:07:24 +00004240 err = bpf__setup_stdout(trace.evlist);
4241 if (err) {
4242 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
4243 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
4244 goto out;
4245 }
4246
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03004247 err = -1;
4248
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03004249 if (map_dump_str) {
Arnaldo Carvalho de Melo5ca0b7f2019-07-15 17:03:10 -03004250 trace.dump.map = trace__find_bpf_map_by_name(&trace, map_dump_str);
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03004251 if (trace.dump.map == NULL) {
4252 pr_err("ERROR: BPF map \"%s\" not found\n", map_dump_str);
4253 goto out;
4254 }
4255 }
4256
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04004257 if (trace.trace_pgfaults) {
4258 trace.opts.sample_address = true;
4259 trace.opts.sample_time = true;
4260 }
4261
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03004262 if (trace.opts.mmap_pages == UINT_MAX)
4263 mmap_pages_user_set = false;
4264
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03004265 if (trace.max_stack == UINT_MAX) {
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03004266 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03004267 max_stack_user_set = false;
4268 }
4269
4270#ifdef HAVE_DWARF_UNWIND_SUPPORT
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03004271 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03004272 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03004273 }
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03004274#endif
4275
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03004276 if (callchain_param.enabled) {
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03004277 if (!mmap_pages_user_set && geteuid() == 0)
4278 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
4279
Milian Wolff566a0882016-04-08 13:34:15 +02004280 symbol_conf.use_callchain = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03004281 }
Milian Wolff566a0882016-04-08 13:34:15 +02004282
Jiri Olsa6484d2f2019-07-21 13:24:28 +02004283 if (trace.evlist->core.nr_entries > 0) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03004284 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03004285 if (evlist__set_syscall_tp_fields(trace.evlist)) {
4286 perror("failed to set syscalls:* tracepoint fields");
4287 goto out;
4288 }
4289 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03004290
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03004291 if (trace.sort_events) {
4292 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
4293 ordered_events__set_copy_on_queue(&trace.oe.data, true);
4294 }
Jiri Olsa028713a2018-12-05 17:05:09 +01004295
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03004296 /*
4297 * If we are augmenting syscalls, then combine what we put in the
4298 * __augmented_syscalls__ BPF map with what is in the
4299 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
4300 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
4301 *
4302 * We'll switch to look at two BPF maps, one for sys_enter and the
4303 * other for sys_exit when we start augmenting the sys_exit paths with
4304 * buffers that are being copied from kernel to userspace, think 'read'
4305 * syscall.
4306 */
4307 if (trace.syscalls.events.augmented) {
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03004308 evlist__for_each_entry(trace.evlist, evsel) {
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03004309 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
4310
4311 if (raw_syscalls_sys_exit) {
4312 trace.raw_augmented_syscalls = true;
4313 goto init_augmented_syscall_tp;
4314 }
4315
Arnaldo Carvalho de Melo1d59cb12019-01-24 15:39:15 +01004316 if (trace.syscalls.events.augmented->priv == NULL &&
4317 strstr(perf_evsel__name(evsel), "syscalls:sys_enter")) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02004318 struct evsel *augmented = trace.syscalls.events.augmented;
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03004319 if (perf_evsel__init_augmented_syscall_tp(augmented, evsel) ||
4320 perf_evsel__init_augmented_syscall_tp_args(augmented))
4321 goto out;
Arnaldo Carvalho de Melob1199702019-07-16 11:53:03 -03004322 /*
4323 * Augmented is __augmented_syscalls__ BPF_OUTPUT event
4324 * Above we made sure we can get from the payload the tp fields
4325 * that we get from syscalls:sys_enter tracefs format file.
4326 */
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03004327 augmented->handler = trace__sys_enter;
Arnaldo Carvalho de Melob1199702019-07-16 11:53:03 -03004328 /*
4329 * Now we do the same for the *syscalls:sys_enter event so that
4330 * if we handle it directly, i.e. if the BPF prog returns 0 so
4331 * as not to filter it, then we'll handle it just like we would
4332 * for the BPF_OUTPUT one:
4333 */
4334 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel) ||
4335 perf_evsel__init_augmented_syscall_tp_args(evsel))
4336 goto out;
4337 evsel->handler = trace__sys_enter;
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03004338 }
4339
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03004340 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03004341 struct syscall_tp *sc;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03004342init_augmented_syscall_tp:
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03004343 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel))
4344 goto out;
4345 sc = evsel->priv;
4346 /*
4347 * For now with BPF raw_augmented we hook into
4348 * raw_syscalls:sys_enter and there we get all
4349 * 6 syscall args plus the tracepoint common
4350 * fields and the syscall_nr (another long).
4351 * So we check if that is the case and if so
4352 * don't look after the sc->args_size but
4353 * always after the full raw_syscalls:sys_enter
4354 * payload, which is fixed.
4355 *
4356 * We'll revisit this later to pass
4357 * s->args_size to the BPF augmenter (now
4358 * tools/perf/examples/bpf/augmented_raw_syscalls.c,
4359 * so that it copies only what we need for each
4360 * syscall, like what happens when we use
4361 * syscalls:sys_enter_NAME, so that we reduce
4362 * the kernel/userspace traffic to just what is
4363 * needed for each syscall.
4364 */
4365 if (trace.raw_augmented_syscalls)
4366 trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset;
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03004367 perf_evsel__init_augmented_syscall_tp_ret(evsel);
4368 evsel->handler = trace__sys_exit;
4369 }
4370 }
4371 }
4372
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04004373 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
4374 return trace__record(&trace, argc-1, &argv[1]);
4375
4376 /* summary_only implies summary option, but don't overwrite summary if set */
4377 if (trace.summary_only)
4378 trace.summary = trace.summary_only;
4379
Arnaldo Carvalho de Melo726f3232015-02-06 10:16:45 +01004380 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
Jiri Olsa6484d2f2019-07-21 13:24:28 +02004381 trace.evlist->core.nr_entries == 0 /* Was --events used? */) {
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03004382 trace.trace_syscalls = true;
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03004383 }
4384
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004385 if (output_name != NULL) {
4386 err = trace__open_output(&trace, output_name);
4387 if (err < 0) {
4388 perror("failed to create output file");
4389 goto out;
4390 }
4391 }
4392
Arnaldo Carvalho de Melo22ac4312019-08-15 12:15:39 -03004393 err = evswitch__init(&trace.evswitch, trace.evlist, stderr);
4394 if (err)
4395 goto out_close;
4396
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004397 err = target__validate(&trace.opts.target);
Namhyung Kim32caf0d2012-10-05 14:02:13 +09004398 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004399 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004400 fprintf(trace.output, "%s", bf);
4401 goto out_close;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09004402 }
4403
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004404 err = target__parse_uid(&trace.opts.target);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004405 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004406 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004407 fprintf(trace.output, "%s", bf);
4408 goto out_close;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004409 }
4410
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004411 if (!argc && target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +09004412 trace.opts.target.system_wide = true;
4413
David Ahern6810fc92013-08-28 22:29:52 -06004414 if (input_name)
4415 err = trace__replay(&trace);
4416 else
4417 err = trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03004418
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004419out_close:
4420 if (output_name != NULL)
4421 fclose(trace.output);
4422out:
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03004423 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004424}