blob: e3fc9062f13682b0e726591f37ee1802135f37a3 [file] [log] [blame]
Thomas Gleixner91007042019-05-29 07:12:25 -07001// SPDX-License-Identifier: GPL-2.0-only
Arnaldo Carvalho de Meloa598bb52015-08-28 12:02:37 -03002/*
3 * builtin-trace.c
4 *
5 * Builtin 'trace' command:
6 *
7 * Display a continuously updated trace of any workload, CPU, specific PID,
8 * system wide, etc. Default format is loosely strace like, but any other
9 * event may be specified using --event.
10 *
11 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
12 *
13 * Initially based on the 'trace' prototype by Thomas Gleixner:
14 *
15 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
Arnaldo Carvalho de Meloa598bb52015-08-28 12:02:37 -030016 */
17
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 Melo514f1c62012-09-26 20:05:56 -030022#include "builtin.h"
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030023#include "util/cgroup.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030024#include "util/color.h"
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -030025#include "util/config.h"
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -030026#include "util/debug.h"
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +010027#include "util/env.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030028#include "util/event.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030029#include "util/evlist.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060030#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030031#include "util/machine.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010032#include "util/map.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010033#include "util/symbol.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030034#include "util/path.h"
David Ahern6810fc92013-08-28 22:29:52 -060035#include "util/session.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030036#include "util/thread.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060037#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -030038#include "util/strlist.h"
David Ahernbdc89662013-08-28 22:29:53 -060039#include "util/intlist.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030040#include "util/thread_map.h"
David Ahernbf2575c2013-10-08 21:26:53 -060041#include "util/stat.h"
Arnaldo Carvalho de Melofd5cead2017-03-14 16:19:30 -030042#include "trace/beauty/beauty.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010043#include "trace-event.h"
David Ahern9aca7f12013-12-04 19:41:39 -070044#include "util/parse-events.h"
Wang Nanba504232016-02-26 09:31:54 +000045#include "util/bpf-loader.h"
Milian Wolff566a0882016-04-08 13:34:15 +020046#include "callchain.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030047#include "print_binary.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030048#include "string2.h"
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030049#include "syscalltbl.h"
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -030050#include "rb_resort.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030051
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030052#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030053#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030054#include <poll.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030055#include <signal.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030056#include <stdlib.h>
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -030057#include <string.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020058#include <linux/err.h>
Arnaldo Carvalho de Melo997bba82016-03-30 19:43:32 -030059#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030060#include <linux/kernel.h>
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -030061#include <linux/random.h>
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -030062#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030063#include <linux/time64.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030064#include <fcntl.h>
Arnaldo Carvalho de Melo4bcc4cf2018-12-27 13:32:59 -030065#include <sys/sysmacros.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030066
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030067#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030068
Arnaldo Carvalho de Meloc188e7a2015-05-14 17:39:03 -030069#ifndef O_CLOEXEC
70# define O_CLOEXEC 02000000
71#endif
72
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -030073#ifndef F_LINUX_SPECIFIC_BASE
74# define F_LINUX_SPECIFIC_BASE 1024
75#endif
76
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030077struct trace {
78 struct perf_tool tool;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030079 struct syscalltbl *sctbl;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030080 struct {
81 int max;
82 struct syscall *table;
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -030083 struct bpf_map *map;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030084 struct {
85 struct perf_evsel *sys_enter,
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -030086 *sys_exit,
87 *augmented;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030088 } events;
89 } syscalls;
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -030090 struct {
91 struct bpf_map *map;
92 } dump;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030093 struct record_opts opts;
94 struct perf_evlist *evlist;
95 struct machine *host;
96 struct thread *current;
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030097 struct cgroup *cgroup;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030098 u64 base_time;
99 FILE *output;
100 unsigned long nr_events;
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -0300101 unsigned long nr_events_printed;
102 unsigned long max_events;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300103 struct strlist *ev_qualifier;
104 struct {
105 size_t nr;
106 int *entries;
107 } ev_qualifier_ids;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300108 struct {
109 size_t nr;
110 pid_t *entries;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -0300111 struct bpf_map *map;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300112 } filter_pids;
113 double duration_filter;
114 double runtime_ms;
115 struct {
116 u64 vfs_getname,
117 proc_getname;
118 } stats;
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -0300119 unsigned int max_stack;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -0300120 unsigned int min_stack;
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300121 int raw_augmented_syscalls_args_size;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -0300122 bool raw_augmented_syscalls;
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300123 bool sort_events;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300124 bool not_ev_qualifier;
125 bool live;
126 bool full_time;
127 bool sched;
128 bool multiple_threads;
129 bool summary;
130 bool summary_only;
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -0300131 bool failure_only;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300132 bool show_comm;
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -0300133 bool print_sample;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300134 bool show_tool_stats;
135 bool trace_syscalls;
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -0300136 bool kernel_syscallchains;
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -0300137 s16 args_alignment;
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -0300138 bool show_tstamp;
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -0300139 bool show_duration;
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -0300140 bool show_zeros;
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -0300141 bool show_arg_names;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300142 bool show_string_prefix;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300143 bool force;
144 bool vfs_getname;
145 int trace_pgfaults;
Jiri Olsa028713a2018-12-05 17:05:09 +0100146 struct {
147 struct ordered_events data;
148 u64 last;
149 } oe;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300150};
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300151
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300152struct tp_field {
153 int offset;
154 union {
155 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
156 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
157 };
158};
159
160#define TP_UINT_FIELD(bits) \
161static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
162{ \
David Ahern55d43bca2015-02-19 15:00:22 -0500163 u##bits value; \
164 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
165 return value; \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300166}
167
168TP_UINT_FIELD(8);
169TP_UINT_FIELD(16);
170TP_UINT_FIELD(32);
171TP_UINT_FIELD(64);
172
173#define TP_UINT_FIELD__SWAPPED(bits) \
174static u64 tp_field__swapped_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)); \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300178 return bswap_##bits(value);\
179}
180
181TP_UINT_FIELD__SWAPPED(16);
182TP_UINT_FIELD__SWAPPED(32);
183TP_UINT_FIELD__SWAPPED(64);
184
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300185static 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 -0300186{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300187 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300188
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300189 switch (size) {
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300190 case 1:
191 field->integer = tp_field__u8;
192 break;
193 case 2:
194 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
195 break;
196 case 4:
197 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
198 break;
199 case 8:
200 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
201 break;
202 default:
203 return -1;
204 }
205
206 return 0;
207}
208
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400209static 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 -0300210{
211 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
212}
213
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300214static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
215{
216 return sample->raw_data + field->offset;
217}
218
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300219static int __tp_field__init_ptr(struct tp_field *field, int offset)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300220{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300221 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300222 field->pointer = tp_field__ptr;
223 return 0;
224}
225
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400226static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300227{
228 return __tp_field__init_ptr(field, format_field->offset);
229}
230
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300231struct syscall_tp {
232 struct tp_field id;
233 union {
234 struct tp_field args, ret;
235 };
236};
237
238static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
239 struct tp_field *field,
240 const char *name)
241{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400242 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300243
244 if (format_field == NULL)
245 return -1;
246
247 return tp_field__init_uint(field, format_field, evsel->needs_swap);
248}
249
250#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
251 ({ struct syscall_tp *sc = evsel->priv;\
252 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
253
254static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
255 struct tp_field *field,
256 const char *name)
257{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400258 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300259
260 if (format_field == NULL)
261 return -1;
262
263 return tp_field__init_ptr(field, format_field);
264}
265
266#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
267 ({ struct syscall_tp *sc = evsel->priv;\
268 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
269
270static void perf_evsel__delete_priv(struct perf_evsel *evsel)
271{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300272 zfree(&evsel->priv);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300273 perf_evsel__delete(evsel);
274}
275
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300276static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
277{
278 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
279
280 if (evsel->priv != NULL) {
Arnaldo Carvalho de Melo42da4382018-12-05 13:08:11 -0300281 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
282 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300283 goto out_delete;
284 return 0;
285 }
286
287 return -ENOMEM;
288out_delete:
289 zfree(&evsel->priv);
290 return -ENOENT;
291}
292
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300293static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel, struct perf_evsel *tp)
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300294{
295 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
296
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300297 if (evsel->priv != NULL) {
298 struct tep_format_field *syscall_id = perf_evsel__field(tp, "id");
299 if (syscall_id == NULL)
300 syscall_id = perf_evsel__field(tp, "__syscall_nr");
301 if (syscall_id == NULL)
302 goto out_delete;
303 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 -0300304 goto out_delete;
305
306 return 0;
307 }
308
309 return -ENOMEM;
310out_delete:
311 zfree(&evsel->priv);
312 return -EINVAL;
313}
314
315static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
316{
317 struct syscall_tp *sc = evsel->priv;
318
319 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
320}
321
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -0300322static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
323{
324 struct syscall_tp *sc = evsel->priv;
325
326 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
327}
328
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300329static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
Namhyung Kim96695d42013-11-12 08:51:45 -0300330{
331 evsel->priv = malloc(sizeof(struct syscall_tp));
332 if (evsel->priv != NULL) {
333 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
334 goto out_delete;
335
336 evsel->handler = handler;
337 return 0;
338 }
339
340 return -ENOMEM;
341
342out_delete:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300343 zfree(&evsel->priv);
Namhyung Kim96695d42013-11-12 08:51:45 -0300344 return -ENOENT;
345}
346
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300347static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300348{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300349 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300350
David Ahern9aca7f12013-12-04 19:41:39 -0700351 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200352 if (IS_ERR(evsel))
David Ahern9aca7f12013-12-04 19:41:39 -0700353 evsel = perf_evsel__newtp("syscalls", direction);
354
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200355 if (IS_ERR(evsel))
356 return NULL;
357
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300358 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200359 goto out_delete;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300360
361 return evsel;
362
363out_delete:
364 perf_evsel__delete_priv(evsel);
365 return NULL;
366}
367
368#define perf_evsel__sc_tp_uint(evsel, name, sample) \
369 ({ struct syscall_tp *fields = evsel->priv; \
370 fields->name.integer(&fields->name, sample); })
371
372#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
373 ({ struct syscall_tp *fields = evsel->priv; \
374 fields->name.pointer(&fields->name, sample); })
375
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300376size_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 -0300377{
378 int idx = val - sa->offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300379
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300380 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
381 size_t printed = scnprintf(bf, size, intfmt, val);
382 if (show_prefix)
383 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
384 return printed;
385 }
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300386
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300387 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300388}
389
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300390static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
391 const char *intfmt,
392 struct syscall_arg *arg)
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300393{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300394 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300395}
396
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300397static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
398 struct syscall_arg *arg)
399{
400 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
401}
402
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300403#define SCA_STRARRAY syscall_arg__scnprintf_strarray
404
Arnaldo Carvalho de Melof5b91db2019-05-23 18:05:03 -0300405size_t syscall_arg__scnprintf_strarray_flags(char *bf, size_t size, struct syscall_arg *arg)
406{
407 return strarray__scnprintf_flags(arg->parm, bf, size, arg->show_string_prefix, arg->val);
408}
409
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300410size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
411{
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300412 size_t printed;
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300413 int i;
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300414
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300415 for (i = 0; i < sas->nr_entries; ++i) {
416 struct strarray *sa = sas->entries[i];
417 int idx = val - sa->offset;
418
419 if (idx >= 0 && idx < sa->nr_entries) {
420 if (sa->entries[idx] == NULL)
421 break;
422 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
423 }
424 }
425
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300426 printed = scnprintf(bf, size, intfmt, val);
427 if (show_prefix)
428 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
429 return printed;
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300430}
431
Arnaldo Carvalho de Melo274e86f2017-07-14 09:38:38 -0300432size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
433 struct syscall_arg *arg)
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300434{
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300435 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300436}
437
Arnaldo Carvalho de Melo48e1f912016-07-05 14:43:27 -0300438#ifndef AT_FDCWD
439#define AT_FDCWD -100
440#endif
441
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300442static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
443 struct syscall_arg *arg)
444{
445 int fd = arg->val;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300446 const char *prefix = "AT_FD";
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300447
448 if (fd == AT_FDCWD)
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300449 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300450
451 return syscall_arg__scnprintf_fd(bf, size, arg);
452}
453
454#define SCA_FDAT syscall_arg__scnprintf_fd_at
455
456static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
457 struct syscall_arg *arg);
458
459#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
460
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300461size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300462{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300463 return scnprintf(bf, size, "%#lx", arg->val);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300464}
465
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300466size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
467{
468 if (arg->val == 0)
469 return scnprintf(bf, size, "NULL");
470 return syscall_arg__scnprintf_hex(bf, size, arg);
471}
472
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300473size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300474{
475 return scnprintf(bf, size, "%d", arg->val);
476}
477
Arnaldo Carvalho de Melo5dde91e2017-07-14 10:34:16 -0300478size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
479{
480 return scnprintf(bf, size, "%ld", arg->val);
481}
482
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300483static const char *bpf_cmd[] = {
484 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
485 "MAP_GET_NEXT_KEY", "PROG_LOAD",
486};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300487static DEFINE_STRARRAY(bpf_cmd, "BPF_");
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300488
Arnaldo Carvalho de Melof6af0952019-05-21 16:34:03 -0300489static const char *fsmount_flags[] = {
490 [1] = "CLOEXEC",
491};
492static DEFINE_STRARRAY(fsmount_flags, "FSMOUNT_");
493
Arnaldo Carvalho de Melodcc6fd62019-05-21 15:36:44 -0300494#include "trace/beauty/generated/fsconfig_arrays.c"
495
496static DEFINE_STRARRAY(fsconfig_cmds, "FSCONFIG_");
497
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300498static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300499static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
Arnaldo Carvalho de Meloeac032c2013-09-20 11:27:32 -0300500
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300501static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300502static DEFINE_STRARRAY(itimers, "ITIMER_");
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300503
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300504static const char *keyctl_options[] = {
505 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
506 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
507 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
508 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
509 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
510};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300511static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300512
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300513static const char *whences[] = { "SET", "CUR", "END",
514#ifdef SEEK_DATA
515"DATA",
516#endif
517#ifdef SEEK_HOLE
518"HOLE",
519#endif
520};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300521static DEFINE_STRARRAY(whences, "SEEK_");
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300522
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300523static const char *fcntl_cmds[] = {
524 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
Arnaldo Carvalho de Meloe000e5e2017-07-13 13:07:00 -0300525 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
526 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
527 "GETOWNER_UIDS",
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300528};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300529static DEFINE_STRARRAY(fcntl_cmds, "F_");
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300530
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300531static const char *fcntl_linux_specific_cmds[] = {
532 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
533 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
Arnaldo Carvalho de Melo64e45612017-07-13 17:34:46 -0300534 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300535};
536
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300537static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300538
539static struct strarray *fcntl_cmds_arrays[] = {
540 &strarray__fcntl_cmds,
541 &strarray__fcntl_linux_specific_cmds,
542};
543
544static DEFINE_STRARRAYS(fcntl_cmds_arrays);
545
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300546static const char *rlimit_resources[] = {
547 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
548 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
549 "RTTIME",
550};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300551static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300552
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300553static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300554static DEFINE_STRARRAY(sighow, "SIG_");
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300555
David Ahern4f8c1b72013-09-22 19:45:00 -0600556static const char *clockid[] = {
557 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
Arnaldo Carvalho de Melo28ebb872015-08-11 10:38:38 -0300558 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
559 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
David Ahern4f8c1b72013-09-22 19:45:00 -0600560};
Arnaldo Carvalho de Melo2e3d7fac2018-12-14 15:08:40 -0300561static DEFINE_STRARRAY(clockid, "CLOCK_");
David Ahern4f8c1b72013-09-22 19:45:00 -0600562
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300563static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
564 struct syscall_arg *arg)
565{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300566 bool show_prefix = arg->show_string_prefix;
567 const char *suffix = "_OK";
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300568 size_t printed = 0;
569 int mode = arg->val;
570
571 if (mode == F_OK) /* 0 */
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300572 return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300573#define P_MODE(n) \
574 if (mode & n##_OK) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300575 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300576 mode &= ~n##_OK; \
577 }
578
579 P_MODE(R);
580 P_MODE(W);
581 P_MODE(X);
582#undef P_MODE
583
584 if (mode)
585 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
586
587 return printed;
588}
589
590#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
591
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300592static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
593 struct syscall_arg *arg);
594
595#define SCA_FILENAME syscall_arg__scnprintf_filename
596
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300597static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
598 struct syscall_arg *arg)
599{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300600 bool show_prefix = arg->show_string_prefix;
601 const char *prefix = "O_";
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300602 int printed = 0, flags = arg->val;
603
604#define P_FLAG(n) \
605 if (flags & O_##n) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300606 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300607 flags &= ~O_##n; \
608 }
609
610 P_FLAG(CLOEXEC);
611 P_FLAG(NONBLOCK);
612#undef P_FLAG
613
614 if (flags)
615 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
616
617 return printed;
618}
619
620#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
621
Arnaldo Carvalho de Meloa355a612016-04-13 11:55:18 -0300622#ifndef GRND_NONBLOCK
623#define GRND_NONBLOCK 0x0001
624#endif
625#ifndef GRND_RANDOM
626#define GRND_RANDOM 0x0002
627#endif
628
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300629static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
630 struct syscall_arg *arg)
631{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300632 bool show_prefix = arg->show_string_prefix;
633 const char *prefix = "GRND_";
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300634 int printed = 0, flags = arg->val;
635
636#define P_FLAG(n) \
637 if (flags & GRND_##n) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300638 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300639 flags &= ~GRND_##n; \
640 }
641
642 P_FLAG(RANDOM);
643 P_FLAG(NONBLOCK);
644#undef P_FLAG
645
646 if (flags)
647 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
648
649 return printed;
650}
651
652#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
653
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300654#define STRARRAY(name, array) \
655 { .scnprintf = SCA_STRARRAY, \
656 .parm = &strarray__##array, }
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -0300657
Arnaldo Carvalho de Melof6af0952019-05-21 16:34:03 -0300658#define STRARRAY_FLAGS(name, array) \
659 { .scnprintf = SCA_STRARRAY_FLAGS, \
660 .parm = &strarray__##array, }
661
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +0100662#include "trace/beauty/arch_errno_names.c"
Arnaldo Carvalho de Meloea8dc3c2016-04-13 12:10:19 -0300663#include "trace/beauty/eventfd.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300664#include "trace/beauty/futex_op.c"
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300665#include "trace/beauty/futex_val3.c"
Arnaldo Carvalho de Melodf4cb162016-04-13 12:05:44 -0300666#include "trace/beauty/mmap.c"
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -0300667#include "trace/beauty/mode_t.c"
Arnaldo Carvalho de Meloa30e6252016-04-27 19:01:52 -0300668#include "trace/beauty/msg_flags.c"
Arnaldo Carvalho de Melo8f48df62016-05-06 10:02:32 -0300669#include "trace/beauty/open_flags.c"
Arnaldo Carvalho de Melo62de3442016-04-26 11:03:03 -0300670#include "trace/beauty/perf_event_open.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300671#include "trace/beauty/pid.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300672#include "trace/beauty/sched_policy.c"
Arnaldo Carvalho de Melof5cd95e2016-05-11 10:32:20 -0300673#include "trace/beauty/seccomp.c"
Arnaldo Carvalho de Melo12199d82016-05-06 09:58:02 -0300674#include "trace/beauty/signum.c"
Arnaldo Carvalho de Melobbf86c42016-04-14 13:53:10 -0300675#include "trace/beauty/socket_type.c"
Arnaldo Carvalho de Melo7206b902016-04-06 14:11:36 -0300676#include "trace/beauty/waitid_options.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300677
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300678struct syscall_arg_fmt {
679 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -0300680 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300681 void *parm;
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -0300682 const char *name;
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -0300683 bool show_zero;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300684};
685
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300686static struct syscall_fmt {
687 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -0300688 const char *alias;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300689 struct syscall_arg_fmt arg[6];
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -0300690 u8 nr_args;
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300691 bool errpid;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300692 bool timeout;
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -0300693 bool hexret;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300694} syscall_fmts[] = {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300695 { .name = "access",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300696 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
Arnaldo Carvalho de Melofb7068e2018-12-18 11:05:56 -0300697 { .name = "arch_prctl",
698 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
699 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
Arnaldo Carvalho de Melo02ef2882018-08-28 17:03:53 -0300700 { .name = "bind",
701 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300702 { .name = "bpf",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300703 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300704 { .name = "brk", .hexret = true,
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300705 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300706 { .name = "clock_gettime",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300707 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
Arnaldo Carvalho de Melo33396a32017-07-19 16:15:17 -0300708 { .name = "clone", .errpid = true, .nr_args = 5,
709 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
710 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
711 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
712 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
713 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300714 { .name = "close",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300715 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
Arnaldo Carvalho de Melod5a7e662018-08-28 16:24:44 -0300716 { .name = "connect",
717 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300718 { .name = "epoll_ctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300719 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300720 { .name = "eventfd2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300721 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300722 { .name = "fchmodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300723 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300724 { .name = "fchownat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300725 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300726 { .name = "fcntl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300727 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
Arnaldo Carvalho de Melo39cc3552017-07-17 16:02:52 -0300728 .parm = &strarrays__fcntl_cmds_arrays,
729 .show_zero = true, },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300730 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300731 { .name = "flock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300732 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
Arnaldo Carvalho de Melodcc6fd62019-05-21 15:36:44 -0300733 { .name = "fsconfig",
734 .arg = { [1] = STRARRAY(cmd, fsconfig_cmds), }, },
Arnaldo Carvalho de Melof6af0952019-05-21 16:34:03 -0300735 { .name = "fsmount",
736 .arg = { [1] = STRARRAY_FLAGS(flags, fsmount_flags),
737 [2] = { .scnprintf = SCA_FSMOUNT_ATTR_FLAGS, /* attr_flags */ }, }, },
Arnaldo Carvalho de Melo693bd392019-05-20 16:24:15 -0300738 { .name = "fspick",
739 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
740 [1] = { .scnprintf = SCA_FILENAME, /* path */ },
741 [2] = { .scnprintf = SCA_FSPICK_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300742 { .name = "fstat", .alias = "newfstat", },
743 { .name = "fstatat", .alias = "newfstatat", },
744 { .name = "futex",
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300745 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
746 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300747 { .name = "futimesat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300748 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300749 { .name = "getitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300750 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300751 { .name = "getpid", .errpid = true, },
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300752 { .name = "getpgid", .errpid = true, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300753 { .name = "getppid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300754 { .name = "getrandom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300755 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300756 { .name = "getrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300757 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo2d1073d2018-01-09 12:03:47 -0300758 { .name = "gettid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300759 { .name = "ioctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300760 .arg = {
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300761#if defined(__i386__) || defined(__x86_64__)
762/*
763 * FIXME: Make this available to all arches.
764 */
Arnaldo Carvalho de Melo1cc47f2d2017-07-31 13:20:14 -0300765 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300766 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300767#else
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300768 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300769#endif
Arnaldo Carvalho de Melo1de30382017-10-31 11:32:23 -0300770 { .name = "kcmp", .nr_args = 5,
771 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
772 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
773 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
774 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
775 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300776 { .name = "keyctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300777 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300778 { .name = "kill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300779 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300780 { .name = "linkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300781 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300782 { .name = "lseek",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300783 .arg = { [2] = STRARRAY(whence, whences), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300784 { .name = "lstat", .alias = "newlstat", },
785 { .name = "madvise",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300786 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
787 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300788 { .name = "mkdirat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300789 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300790 { .name = "mknodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300791 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300792 { .name = "mmap", .hexret = true,
Jiri Olsa54265662017-05-31 13:35:57 +0200793/* The standard mmap maps to old_mmap on s390x */
794#if defined(__s390x__)
795 .alias = "old_mmap",
796#endif
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300797 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
Arnaldo Carvalho de Meloa6631342018-12-18 11:25:08 -0300798 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ },
799 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300800 { .name = "mount",
Arnaldo Carvalho de Melo23c07a22018-10-26 13:51:45 -0300801 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
802 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300803 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo566e3022019-05-20 14:58:03 -0300804 { .name = "move_mount",
805 .arg = { [0] = { .scnprintf = SCA_FDAT, /* from_dfd */ },
806 [1] = { .scnprintf = SCA_FILENAME, /* from_pathname */ },
807 [2] = { .scnprintf = SCA_FDAT, /* to_dfd */ },
808 [3] = { .scnprintf = SCA_FILENAME, /* to_pathname */ },
809 [4] = { .scnprintf = SCA_MOVE_MOUNT_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300810 { .name = "mprotect",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300811 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
812 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300813 { .name = "mq_unlink",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300814 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300815 { .name = "mremap", .hexret = true,
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300816 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300817 { .name = "name_to_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300818 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300819 { .name = "newfstatat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300820 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300821 { .name = "open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300822 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300823 { .name = "open_by_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300824 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
825 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300826 { .name = "openat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300827 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
828 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300829 { .name = "perf_event_open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300830 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
831 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
832 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300833 { .name = "pipe2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300834 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo83bc9c372017-08-28 11:47:11 -0300835 { .name = "pkey_alloc",
836 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
837 { .name = "pkey_free",
838 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
839 { .name = "pkey_mprotect",
840 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
841 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
842 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300843 { .name = "poll", .timeout = true, },
844 { .name = "ppoll", .timeout = true, },
Arnaldo Carvalho de Melofb7068e2018-12-18 11:05:56 -0300845 { .name = "prctl",
Arnaldo Carvalho de Melod688d032017-10-26 15:19:35 -0300846 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
847 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
848 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300849 { .name = "pread", .alias = "pread64", },
850 { .name = "preadv", .alias = "pread", },
851 { .name = "prlimit64",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300852 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300853 { .name = "pwrite", .alias = "pwrite64", },
854 { .name = "readlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300855 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300856 { .name = "recvfrom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300857 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300858 { .name = "recvmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300859 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300860 { .name = "recvmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300861 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300862 { .name = "renameat",
Arnaldo Carvalho de Melo436651c2018-12-11 11:19:23 -0300863 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
864 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
Arnaldo Carvalho de Melo84a83542018-12-11 09:53:21 -0300865 { .name = "renameat2",
866 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
Arnaldo Carvalho de Meloca7ff2c2018-12-11 10:39:30 -0300867 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
868 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300869 { .name = "rt_sigaction",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300870 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300871 { .name = "rt_sigprocmask",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300872 .arg = { [0] = STRARRAY(how, sighow), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300873 { .name = "rt_sigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300874 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300875 { .name = "rt_tgsigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300876 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300877 { .name = "sched_setscheduler",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300878 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300879 { .name = "seccomp",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300880 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
881 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300882 { .name = "select", .timeout = true, },
883 { .name = "sendmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300884 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300885 { .name = "sendmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300886 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300887 { .name = "sendto",
Arnaldo Carvalho de Melo6ebb6862018-08-29 08:47:08 -0300888 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
889 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300890 { .name = "set_tid_address", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300891 { .name = "setitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300892 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300893 { .name = "setrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300894 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300895 { .name = "socket",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300896 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300897 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
898 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300899 { .name = "socketpair",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300900 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300901 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
902 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300903 { .name = "stat", .alias = "newstat", },
904 { .name = "statx",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300905 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
906 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
907 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300908 { .name = "swapoff",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300909 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300910 { .name = "swapon",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300911 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300912 { .name = "symlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300913 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Meloa9a187a2019-05-21 21:47:07 -0300914 { .name = "sync_file_range",
915 .arg = { [3] = { .scnprintf = SCA_SYNC_FILE_RANGE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300916 { .name = "tgkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300917 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300918 { .name = "tkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300919 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo476c92ca2018-10-26 13:23:25 -0300920 { .name = "umount2", .alias = "umount",
921 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300922 { .name = "uname", .alias = "newuname", },
923 { .name = "unlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300924 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300925 { .name = "utimensat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300926 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300927 { .name = "wait4", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300928 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300929 { .name = "waitid", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300930 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300931};
932
933static int syscall_fmt__cmp(const void *name, const void *fmtp)
934{
935 const struct syscall_fmt *fmt = fmtp;
936 return strcmp(name, fmt->name);
937}
938
939static struct syscall_fmt *syscall_fmt__find(const char *name)
940{
941 const int nmemb = ARRAY_SIZE(syscall_fmts);
942 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
943}
944
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -0300945static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
946{
947 int i, nmemb = ARRAY_SIZE(syscall_fmts);
948
949 for (i = 0; i < nmemb; ++i) {
950 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
951 return &syscall_fmts[i];
952 }
953
954 return NULL;
955}
956
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300957/*
958 * is_exit: is this "exit" or "exit_group"?
959 * 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 -0300960 * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc.
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300961 */
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300962struct syscall {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500963 struct tep_event *tp_format;
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -0300964 int nr_args;
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -0300965 int args_size;
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300966 bool is_exit;
967 bool is_open;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400968 struct tep_format_field *args;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300969 const char *name;
970 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300971 struct syscall_arg_fmt *arg_fmt;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300972};
973
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -0300974/*
975 * Must match what is in the BPF program:
976 *
977 * tools/perf/examples/bpf/augmented_raw_syscalls.c
978 */
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -0300979struct bpf_map_syscall_entry {
980 bool enabled;
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -0300981 u16 string_args_len[6];
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -0300982};
983
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300984/*
985 * We need to have this 'calculated' boolean because in some cases we really
986 * don't know what is the duration of a syscall, for instance, when we start
987 * a session and some threads are waiting for a syscall to finish, say 'poll',
988 * in which case all we can do is to print "( ? ) for duration and for the
989 * start timestamp.
990 */
991static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200992{
993 double duration = (double)t / NSEC_PER_MSEC;
994 size_t printed = fprintf(fp, "(");
995
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300996 if (!calculated)
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -0300997 printed += fprintf(fp, " ");
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300998 else if (duration >= 1.0)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200999 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
1000 else if (duration >= 0.01)
1001 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
1002 else
1003 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001004 return printed + fprintf(fp, "): ");
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001005}
1006
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001007/**
1008 * filename.ptr: The filename char pointer that will be vfs_getname'd
1009 * filename.entry_str_pos: Where to insert the string translated from
1010 * filename.ptr by the vfs_getname tracepoint/kprobe.
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001011 * ret_scnprintf: syscall args may set this to a different syscall return
1012 * formatter, for instance, fcntl may return fds, file flags, etc.
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001013 */
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001014struct thread_trace {
1015 u64 entry_time;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001016 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001017 unsigned long nr_events;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04001018 unsigned long pfmaj, pfmin;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001019 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03001020 double runtime_ms;
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001021 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001022 struct {
1023 unsigned long ptr;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001024 short int entry_str_pos;
1025 bool pending_open;
1026 unsigned int namelen;
1027 char *name;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001028 } filename;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001029 struct {
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001030 int max;
1031 struct file *table;
1032 } files;
David Ahernbf2575c2013-10-08 21:26:53 -06001033
1034 struct intlist *syscall_stats;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001035};
1036
1037static struct thread_trace *thread_trace__new(void)
1038{
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001039 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
1040
Leo Yan7a6d49d2019-07-02 18:34:14 +08001041 if (ttrace) {
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001042 ttrace->files.max = -1;
Leo Yan7a6d49d2019-07-02 18:34:14 +08001043 ttrace->syscall_stats = intlist__new(NULL);
1044 }
David Ahernbf2575c2013-10-08 21:26:53 -06001045
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001046 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001047}
1048
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001049static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001050{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001051 struct thread_trace *ttrace;
1052
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001053 if (thread == NULL)
1054 goto fail;
1055
Namhyung Kim89dceb22014-10-06 09:46:03 +09001056 if (thread__priv(thread) == NULL)
1057 thread__set_priv(thread, thread_trace__new());
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001058
Namhyung Kim89dceb22014-10-06 09:46:03 +09001059 if (thread__priv(thread) == NULL)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001060 goto fail;
1061
Namhyung Kim89dceb22014-10-06 09:46:03 +09001062 ttrace = thread__priv(thread);
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001063 ++ttrace->nr_events;
1064
1065 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001066fail:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001067 color_fprintf(fp, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001068 "WARNING: not enough memory, dropping samples!\n");
1069 return NULL;
1070}
1071
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001072
1073void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001074 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001075{
1076 struct thread_trace *ttrace = thread__priv(arg->thread);
1077
1078 ttrace->ret_scnprintf = ret_scnprintf;
1079}
1080
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04001081#define TRACE_PFMAJ (1 << 0)
1082#define TRACE_PFMIN (1 << 1)
1083
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001084static const size_t trace__entry_str_size = 2048;
1085
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001086static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001087{
Arnaldo Carvalho de Melo05107482019-02-12 10:18:36 -03001088 if (fd < 0)
1089 return NULL;
1090
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001091 if (fd > ttrace->files.max) {
1092 struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001093
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001094 if (nfiles == NULL)
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001095 return NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001096
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001097 if (ttrace->files.max != -1) {
1098 memset(nfiles + ttrace->files.max + 1, 0,
1099 (fd - ttrace->files.max) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001100 } else {
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001101 memset(nfiles, 0, (fd + 1) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001102 }
1103
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001104 ttrace->files.table = nfiles;
1105 ttrace->files.max = fd;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001106 }
1107
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001108 return ttrace->files.table + fd;
1109}
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001110
Arnaldo Carvalho de Melo2d473382018-12-27 16:19:33 -03001111struct file *thread__files_entry(struct thread *thread, int fd)
1112{
1113 return thread_trace__files_entry(thread__priv(thread), fd);
1114}
1115
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001116static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
1117{
1118 struct thread_trace *ttrace = thread__priv(thread);
1119 struct file *file = thread_trace__files_entry(ttrace, fd);
1120
1121 if (file != NULL) {
Arnaldo Carvalho de Melo4bcc4cf2018-12-27 13:32:59 -03001122 struct stat st;
1123 if (stat(pathname, &st) == 0)
1124 file->dev_maj = major(st.st_rdev);
Arnaldo Carvalho de Melod7e13482018-12-27 11:05:18 -03001125 file->pathname = strdup(pathname);
1126 if (file->pathname)
1127 return 0;
1128 }
1129
1130 return -1;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001131}
1132
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001133static int thread__read_fd_path(struct thread *thread, int fd)
1134{
1135 char linkname[PATH_MAX], pathname[PATH_MAX];
1136 struct stat st;
1137 int ret;
1138
1139 if (thread->pid_ == thread->tid) {
1140 scnprintf(linkname, sizeof(linkname),
1141 "/proc/%d/fd/%d", thread->pid_, fd);
1142 } else {
1143 scnprintf(linkname, sizeof(linkname),
1144 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1145 }
1146
1147 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1148 return -1;
1149
1150 ret = readlink(linkname, pathname, sizeof(pathname));
1151
1152 if (ret < 0 || ret > st.st_size)
1153 return -1;
1154
1155 pathname[ret] = '\0';
1156 return trace__set_fd_pathname(thread, fd, pathname);
1157}
1158
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001159static const char *thread__fd_path(struct thread *thread, int fd,
1160 struct trace *trace)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001161{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001162 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001163
1164 if (ttrace == NULL)
1165 return NULL;
1166
1167 if (fd < 0)
1168 return NULL;
1169
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001170 if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001171 if (!trace->live)
1172 return NULL;
1173 ++trace->stats.proc_getname;
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001174 if (thread__read_fd_path(thread, fd))
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001175 return NULL;
1176 }
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001177
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001178 return ttrace->files.table[fd].pathname;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001179}
1180
Arnaldo Carvalho de Melofc65eb82017-07-14 15:21:40 -03001181size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001182{
1183 int fd = arg->val;
1184 size_t printed = scnprintf(bf, size, "%d", fd);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001185 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001186
1187 if (path)
1188 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1189
1190 return printed;
1191}
1192
Arnaldo Carvalho de Melo0a2f75402017-10-31 11:30:09 -03001193size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1194{
1195 size_t printed = scnprintf(bf, size, "%d", fd);
1196 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1197
1198 if (thread) {
1199 const char *path = thread__fd_path(thread, fd, trace);
1200
1201 if (path)
1202 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1203
1204 thread__put(thread);
1205 }
1206
1207 return printed;
1208}
1209
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001210static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1211 struct syscall_arg *arg)
1212{
1213 int fd = arg->val;
1214 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
Namhyung Kim89dceb22014-10-06 09:46:03 +09001215 struct thread_trace *ttrace = thread__priv(arg->thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001216
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001217 if (ttrace && fd >= 0 && fd <= ttrace->files.max)
1218 zfree(&ttrace->files.table[fd].pathname);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001219
1220 return printed;
1221}
1222
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001223static void thread__set_filename_pos(struct thread *thread, const char *bf,
1224 unsigned long ptr)
1225{
1226 struct thread_trace *ttrace = thread__priv(thread);
1227
1228 ttrace->filename.ptr = ptr;
1229 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1230}
1231
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001232static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1233{
1234 struct augmented_arg *augmented_arg = arg->augmented.args;
Arnaldo Carvalho de Melo81951682019-06-05 10:34:47 -03001235 size_t printed = scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
1236 /*
1237 * So that the next arg with a payload can consume its augmented arg, i.e. for rename* syscalls
1238 * we would have two strings, each prefixed by its size.
1239 */
1240 int consumed = sizeof(*augmented_arg) + augmented_arg->size;
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001241
Arnaldo Carvalho de Melo016f3272019-06-14 16:50:19 -03001242 arg->augmented.args = ((void *)arg->augmented.args) + consumed;
Arnaldo Carvalho de Melo81951682019-06-05 10:34:47 -03001243 arg->augmented.size -= consumed;
1244
1245 return printed;
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001246}
1247
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001248static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1249 struct syscall_arg *arg)
1250{
1251 unsigned long ptr = arg->val;
1252
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001253 if (arg->augmented.args)
1254 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1255
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001256 if (!arg->trace->vfs_getname)
1257 return scnprintf(bf, size, "%#x", ptr);
1258
1259 thread__set_filename_pos(arg->thread, bf, ptr);
1260 return 0;
1261}
1262
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001263static bool trace__filter_duration(struct trace *trace, double t)
1264{
1265 return t < (trace->duration_filter * NSEC_PER_MSEC);
1266}
1267
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001268static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001269{
1270 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1271
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001272 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001273}
1274
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001275/*
1276 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1277 * using ttrace->entry_time for a thread that receives a sys_exit without
1278 * first having received a sys_enter ("poll" issued before tracing session
1279 * starts, lost sys_enter exit due to ring buffer overflow).
1280 */
1281static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1282{
1283 if (tstamp > 0)
1284 return __trace__fprintf_tstamp(trace, tstamp, fp);
1285
1286 return fprintf(fp, " ? ");
1287}
1288
Namhyung Kimf15eb532012-10-05 14:02:16 +09001289static bool done = false;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001290static bool interrupted = false;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001291
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001292static void sig_handler(int sig)
Namhyung Kimf15eb532012-10-05 14:02:16 +09001293{
1294 done = true;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001295 interrupted = sig == SIGINT;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001296}
1297
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001298static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001299{
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001300 size_t printed = 0;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001301
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001302 if (trace->multiple_threads) {
1303 if (trace->show_comm)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02001304 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
Adrian Hunter38051232013-07-04 16:20:31 +03001305 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001306 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001307
1308 return printed;
1309}
1310
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001311static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1312 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1313{
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03001314 size_t printed = 0;
1315
1316 if (trace->show_tstamp)
1317 printed = trace__fprintf_tstamp(trace, tstamp, fp);
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03001318 if (trace->show_duration)
1319 printed += fprintf_duration(duration, duration_calculated, fp);
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001320 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1321}
1322
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001323static int trace__process_event(struct trace *trace, struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001324 union perf_event *event, struct perf_sample *sample)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001325{
1326 int ret = 0;
1327
1328 switch (event->header.type) {
1329 case PERF_RECORD_LOST:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001330 color_fprintf(trace->output, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001331 "LOST %" PRIu64 " events!\n", event->lost.lost);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001332 ret = machine__process_lost_event(machine, event, sample);
Arnaldo Carvalho de Melo3ed5ca22016-03-30 16:51:17 -03001333 break;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001334 default:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001335 ret = machine__process_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001336 break;
1337 }
1338
1339 return ret;
1340}
1341
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001342static int trace__tool_process(struct perf_tool *tool,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001343 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001344 struct perf_sample *sample,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001345 struct machine *machine)
1346{
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001347 struct trace *trace = container_of(tool, struct trace, tool);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001348 return trace__process_event(trace, machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001349}
1350
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -03001351static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1352{
1353 struct machine *machine = vmachine;
1354
1355 if (machine->kptr_restrict_warned)
1356 return NULL;
1357
1358 if (symbol_conf.kptr_restrict) {
1359 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1360 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1361 "Kernel samples will not be resolved.\n");
1362 machine->kptr_restrict_warned = true;
1363 return NULL;
1364 }
1365
1366 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1367}
1368
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001369static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1370{
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001371 int err = symbol__init(NULL);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001372
1373 if (err)
1374 return err;
1375
David Ahern8fb598e2013-09-28 13:13:00 -06001376 trace->host = machine__new_host();
1377 if (trace->host == NULL)
1378 return -ENOMEM;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001379
Andrei Vagincbd5c172017-11-07 16:22:46 -08001380 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1381 if (err < 0)
1382 goto out;
Arnaldo Carvalho de Melo706c3da2015-07-22 16:16:16 -03001383
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001384 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001385 evlist->threads, trace__tool_process, false,
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001386 1);
Andrei Vagincbd5c172017-11-07 16:22:46 -08001387out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001388 if (err)
1389 symbol__exit();
1390
1391 return err;
1392}
1393
Andrei Vagin33974a42017-11-07 16:22:45 -08001394static void trace__symbols__exit(struct trace *trace)
1395{
1396 machine__exit(trace->host);
1397 trace->host = NULL;
1398
1399 symbol__exit();
1400}
1401
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001402static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1403{
1404 int idx;
1405
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001406 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1407 nr_args = sc->fmt->nr_args;
1408
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001409 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1410 if (sc->arg_fmt == NULL)
1411 return -1;
1412
1413 for (idx = 0; idx < nr_args; ++idx) {
1414 if (sc->fmt)
1415 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1416 }
1417
1418 sc->nr_args = nr_args;
1419 return 0;
1420}
1421
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001422static int syscall__set_arg_fmts(struct syscall *sc)
1423{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001424 struct tep_format_field *field, *last_field = NULL;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001425 int idx = 0, len;
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001426
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001427 for (field = sc->args; field; field = field->next, ++idx) {
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001428 last_field = field;
1429
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001430 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1431 continue;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001432
Arnaldo Carvalho de Melodea87bf2019-06-05 10:53:06 -03001433 len = strlen(field->name);
1434
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001435 if (strcmp(field->type, "const char *") == 0 &&
Arnaldo Carvalho de Melodea87bf2019-06-05 10:53:06 -03001436 ((len >= 4 && strcmp(field->name + len - 4, "name") == 0) ||
1437 strstr(field->name, "path") != NULL))
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001438 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -03001439 else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
1440 sc->arg_fmt[idx].scnprintf = SCA_PTR;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -03001441 else if (strcmp(field->type, "pid_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001442 sc->arg_fmt[idx].scnprintf = SCA_PID;
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -03001443 else if (strcmp(field->type, "umode_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001444 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001445 else if ((strcmp(field->type, "int") == 0 ||
1446 strcmp(field->type, "unsigned int") == 0 ||
1447 strcmp(field->type, "long") == 0) &&
Arnaldo Carvalho de Melodea87bf2019-06-05 10:53:06 -03001448 len >= 2 && strcmp(field->name + len - 2, "fd") == 0) {
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001449 /*
1450 * /sys/kernel/tracing/events/syscalls/sys_enter*
1451 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1452 * 65 int
1453 * 23 unsigned int
1454 * 7 unsigned long
1455 */
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001456 sc->arg_fmt[idx].scnprintf = SCA_FD;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001457 }
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001458 }
1459
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001460 if (last_field)
1461 sc->args_size = last_field->offset + last_field->size;
1462
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001463 return 0;
1464}
1465
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001466static int trace__read_syscall_info(struct trace *trace, int id)
1467{
1468 char tp_name[128];
1469 struct syscall *sc;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03001470 const char *name = syscalltbl__name(trace->sctbl, id);
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001471
1472 if (name == NULL)
1473 return -1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001474
1475 if (id > trace->syscalls.max) {
1476 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1477
1478 if (nsyscalls == NULL)
1479 return -1;
1480
1481 if (trace->syscalls.max != -1) {
1482 memset(nsyscalls + trace->syscalls.max + 1, 0,
1483 (id - trace->syscalls.max) * sizeof(*sc));
1484 } else {
1485 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1486 }
1487
1488 trace->syscalls.table = nsyscalls;
1489 trace->syscalls.max = id;
1490 }
1491
1492 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001493 sc->name = name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001494
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001495 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001496
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001497 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
Jiri Olsa97978b32013-12-03 14:09:24 +01001498 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001499
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001500 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001501 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
Jiri Olsa97978b32013-12-03 14:09:24 +01001502 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001503 }
1504
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001505 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1506 return -1;
1507
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001508 if (IS_ERR(sc->tp_format))
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001509 return -1;
1510
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001511 sc->args = sc->tp_format->format.fields;
Taeung Songc42de702016-02-26 22:14:25 +09001512 /*
1513 * We need to check and discard the first variable '__syscall_nr'
1514 * or 'nr' that mean the syscall number. It is needless here.
1515 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1516 */
1517 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001518 sc->args = sc->args->next;
1519 --sc->nr_args;
1520 }
1521
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001522 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001523 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001524
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001525 return syscall__set_arg_fmts(sc);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001526}
1527
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001528static int trace__validate_ev_qualifier(struct trace *trace)
1529{
Arnaldo Carvalho de Meloa4066d62019-06-13 17:35:09 -03001530 int err = 0;
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001531 bool printed_invalid_prefix = false;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001532 struct str_node *pos;
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001533 size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier);
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001534
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001535 trace->ev_qualifier_ids.entries = malloc(nr_allocated *
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001536 sizeof(trace->ev_qualifier_ids.entries[0]));
1537
1538 if (trace->ev_qualifier_ids.entries == NULL) {
1539 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1540 trace->output);
1541 err = -EINVAL;
1542 goto out;
1543 }
1544
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001545 strlist__for_each_entry(pos, trace->ev_qualifier) {
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001546 const char *sc = pos->s;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001547 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001548
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001549 if (id < 0) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001550 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1551 if (id >= 0)
1552 goto matches;
1553
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001554 if (!printed_invalid_prefix) {
1555 pr_debug("Skipping unknown syscalls: ");
1556 printed_invalid_prefix = true;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001557 } else {
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001558 pr_debug(", ");
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001559 }
1560
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001561 pr_debug("%s", sc);
1562 continue;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001563 }
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001564matches:
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001565 trace->ev_qualifier_ids.entries[nr_used++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001566 if (match_next == -1)
1567 continue;
1568
1569 while (1) {
1570 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1571 if (id < 0)
1572 break;
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001573 if (nr_allocated == nr_used) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001574 void *entries;
1575
1576 nr_allocated += 8;
1577 entries = realloc(trace->ev_qualifier_ids.entries,
1578 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1579 if (entries == NULL) {
1580 err = -ENOMEM;
1581 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1582 goto out_free;
1583 }
1584 trace->ev_qualifier_ids.entries = entries;
1585 }
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001586 trace->ev_qualifier_ids.entries[nr_used++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001587 }
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001588 }
1589
Arnaldo Carvalho de Melo99f26f82019-06-13 18:17:41 -03001590 trace->ev_qualifier_ids.nr = nr_used;
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001591out:
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001592 if (printed_invalid_prefix)
1593 pr_debug("\n");
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001594 return err;
Arnaldo Carvalho de Melo04c41bc2019-06-10 15:37:45 -03001595out_free:
1596 zfree(&trace->ev_qualifier_ids.entries);
1597 trace->ev_qualifier_ids.nr = 0;
1598 goto out;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001599}
1600
David Ahern55d43bca2015-02-19 15:00:22 -05001601/*
1602 * args is to be interpreted as a series of longs but we need to handle
1603 * 8-byte unaligned accesses. args points to raw_data within the event
1604 * and raw_data is guaranteed to be 8-byte unaligned because it is
1605 * preceded by raw_size which is a u32. So we need to copy args to a temp
1606 * variable to read it. Most notably this avoids extended load instructions
1607 * on unaligned addresses
1608 */
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001609unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001610{
1611 unsigned long val;
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001612 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001613
1614 memcpy(&val, p, sizeof(val));
1615 return val;
1616}
1617
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001618static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1619 struct syscall_arg *arg)
1620{
1621 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1622 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1623
1624 return scnprintf(bf, size, "arg%d: ", arg->idx);
1625}
1626
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001627/*
1628 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1629 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1630 * in tools/perf/trace/beauty/mount_flags.c
1631 */
1632static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1633{
1634 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1635 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1636
1637 return val;
1638}
1639
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001640static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1641 struct syscall_arg *arg, unsigned long val)
1642{
1643 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1644 arg->val = val;
1645 if (sc->arg_fmt[arg->idx].parm)
1646 arg->parm = sc->arg_fmt[arg->idx].parm;
1647 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1648 }
1649 return scnprintf(bf, size, "%ld", val);
1650}
1651
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001652static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001653 unsigned char *args, void *augmented_args, int augmented_args_size,
1654 struct trace *trace, struct thread *thread)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001655{
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001656 size_t printed = 0;
David Ahern55d43bca2015-02-19 15:00:22 -05001657 unsigned long val;
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001658 u8 bit = 1;
1659 struct syscall_arg arg = {
1660 .args = args,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001661 .augmented = {
1662 .size = augmented_args_size,
1663 .args = augmented_args,
1664 },
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001665 .idx = 0,
1666 .mask = 0,
1667 .trace = trace,
1668 .thread = thread,
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -03001669 .show_string_prefix = trace->show_string_prefix,
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001670 };
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001671 struct thread_trace *ttrace = thread__priv(thread);
1672
1673 /*
1674 * Things like fcntl will set this in its 'cmd' formatter to pick the
1675 * right formatter for the return value (an fd? file flags?), which is
1676 * not needed for syscalls that always return a given type, say an fd.
1677 */
1678 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001679
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001680 if (sc->args != NULL) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001681 struct tep_format_field *field;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001682
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001683 for (field = sc->args; field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001684 field = field->next, ++arg.idx, bit <<= 1) {
1685 if (arg.mask & bit)
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001686 continue;
David Ahern55d43bca2015-02-19 15:00:22 -05001687
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001688 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001689 /*
1690 * Some syscall args need some mask, most don't and
1691 * return val untouched.
1692 */
1693 val = syscall__mask_val(sc, &arg, val);
David Ahern55d43bca2015-02-19 15:00:22 -05001694
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001695 /*
1696 * Suppress this argument if its value is zero and
1697 * and we don't have a string associated in an
1698 * strarray for it.
1699 */
David Ahern55d43bca2015-02-19 15:00:22 -05001700 if (val == 0 &&
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03001701 !trace->show_zeros &&
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001702 !(sc->arg_fmt &&
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -03001703 (sc->arg_fmt[arg.idx].show_zero ||
1704 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001705 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1706 sc->arg_fmt[arg.idx].parm))
Arnaldo Carvalho de Melo22ae5cf12013-09-12 11:27:34 -03001707 continue;
1708
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03001709 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
1710
1711 if (trace->show_arg_names)
1712 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
1713
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001714 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001715 }
Arnaldo Carvalho de Melo4c4d6e52016-05-05 23:38:05 -03001716 } else if (IS_ERR(sc->tp_format)) {
1717 /*
1718 * If we managed to read the tracepoint /format file, then we
1719 * may end up not having any args, like with gettid(), so only
1720 * print the raw args when we didn't manage to read it.
1721 */
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001722 while (arg.idx < sc->nr_args) {
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001723 if (arg.mask & bit)
1724 goto next_arg;
1725 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001726 if (printed)
1727 printed += scnprintf(bf + printed, size - printed, ", ");
1728 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001729 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1730next_arg:
1731 ++arg.idx;
1732 bit <<= 1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001733 }
1734 }
1735
1736 return printed;
1737}
1738
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001739typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001740 union perf_event *event,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001741 struct perf_sample *sample);
1742
1743static struct syscall *trace__syscall_info(struct trace *trace,
David Ahernbf2575c2013-10-08 21:26:53 -06001744 struct perf_evsel *evsel, int id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001745{
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001746
1747 if (id < 0) {
Arnaldo Carvalho de Meloadaa18b2013-08-22 17:55:25 -03001748
1749 /*
1750 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1751 * before that, leaving at a higher verbosity level till that is
1752 * explained. Reproduced with plain ftrace with:
1753 *
1754 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1755 * grep "NR -1 " /t/trace_pipe
1756 *
1757 * After generating some load on the machine.
1758 */
1759 if (verbose > 1) {
1760 static u64 n;
1761 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1762 id, perf_evsel__name(evsel), ++n);
1763 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001764 return NULL;
1765 }
1766
1767 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1768 trace__read_syscall_info(trace, id))
1769 goto out_cant_read;
1770
1771 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1772 goto out_cant_read;
1773
1774 return &trace->syscalls.table[id];
1775
1776out_cant_read:
Namhyung Kimbb963e12017-02-17 17:17:38 +09001777 if (verbose > 0) {
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03001778 fprintf(trace->output, "Problems reading syscall %d", id);
1779 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1780 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1781 fputs(" information\n", trace->output);
1782 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001783 return NULL;
1784}
1785
David Ahernbf2575c2013-10-08 21:26:53 -06001786static void thread__update_stats(struct thread_trace *ttrace,
1787 int id, struct perf_sample *sample)
1788{
1789 struct int_node *inode;
1790 struct stats *stats;
1791 u64 duration = 0;
1792
1793 inode = intlist__findnew(ttrace->syscall_stats, id);
1794 if (inode == NULL)
1795 return;
1796
1797 stats = inode->priv;
1798 if (stats == NULL) {
1799 stats = malloc(sizeof(struct stats));
1800 if (stats == NULL)
1801 return;
1802 init_stats(stats);
1803 inode->priv = stats;
1804 }
1805
1806 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1807 duration = sample->time - ttrace->entry_time;
1808
1809 update_stats(stats, duration);
1810}
1811
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001812static int trace__printf_interrupted_entry(struct trace *trace)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001813{
1814 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001815 size_t printed;
Arnaldo Carvalho de Melo172bf022019-01-07 16:24:27 -03001816 int len;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001817
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001818 if (trace->failure_only || trace->current == NULL)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001819 return 0;
1820
1821 ttrace = thread__priv(trace->current);
1822
1823 if (!ttrace->entry_pending)
1824 return 0;
1825
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001826 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo172bf022019-01-07 16:24:27 -03001827 printed += len = fprintf(trace->output, "%s)", ttrace->entry_str);
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001828
Arnaldo Carvalho de Melo172bf022019-01-07 16:24:27 -03001829 if (len < trace->args_alignment - 4)
1830 printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " ");
1831
1832 printed += fprintf(trace->output, " ...\n");
1833
1834 ttrace->entry_pending = false;
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03001835 ++trace->nr_events_printed;
1836
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001837 return printed;
1838}
1839
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001840static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1841 struct perf_sample *sample, struct thread *thread)
1842{
1843 int printed = 0;
1844
1845 if (trace->print_sample) {
1846 double ts = (double)sample->time / NSEC_PER_MSEC;
1847
1848 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1849 perf_evsel__name(evsel), ts,
1850 thread__comm_str(thread),
1851 sample->pid, sample->tid, sample->cpu);
1852 }
1853
1854 return printed;
1855}
1856
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001857static 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 -03001858{
1859 void *augmented_args = NULL;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001860 /*
1861 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001862 * and there we get all 6 syscall args plus the tracepoint common fields
1863 * that gets calculated at the start and the syscall_nr (another long).
1864 * So we check if that is the case and if so don't look after the
1865 * sc->args_size but always after the full raw_syscalls:sys_enter payload,
1866 * which is fixed.
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001867 *
1868 * We'll revisit this later to pass s->args_size to the BPF augmenter
1869 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1870 * copies only what we need for each syscall, like what happens when we
1871 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1872 * traffic to just what is needed for each syscall.
1873 */
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001874 int args_size = raw_augmented_args_size ?: sc->args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001875
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001876 *augmented_args_size = sample->raw_size - args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001877 if (*augmented_args_size > 0)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001878 augmented_args = sample->raw_data + args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001879
1880 return augmented_args;
1881}
1882
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001883static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001884 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001885 struct perf_sample *sample)
1886{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001887 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001888 void *args;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001889 int printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001890 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001891 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001892 int augmented_args_size = 0;
1893 void *augmented_args = NULL;
David Ahernbf2575c2013-10-08 21:26:53 -06001894 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001895 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001896
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001897 if (sc == NULL)
1898 return -1;
1899
David Ahern8fb598e2013-09-28 13:13:00 -06001900 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001901 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001902 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001903 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001904
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001905 trace__fprintf_sample(trace, evsel, sample, thread);
1906
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001907 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001908
1909 if (ttrace->entry_str == NULL) {
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001910 ttrace->entry_str = malloc(trace__entry_str_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001911 if (!ttrace->entry_str)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001912 goto out_put;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001913 }
1914
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001915 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001916 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001917 /*
1918 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1919 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1920 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1921 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1922 * so when handling, say the openat syscall, we end up getting 6 args for the
1923 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1924 * thinking that the extra 2 u64 args are the augmented filename, so just check
1925 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1926 */
1927 if (evsel != trace->syscalls.events.sys_enter)
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001928 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 -03001929 ttrace->entry_time = sample->time;
1930 msg = ttrace->entry_str;
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001931 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001932
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001933 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001934 args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001935
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001936 if (sc->is_exit) {
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001937 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001938 int alignment = 0;
1939
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001940 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001941 printed = fprintf(trace->output, "%s)", ttrace->entry_str);
1942 if (trace->args_alignment > printed)
1943 alignment = trace->args_alignment - printed;
1944 fprintf(trace->output, "%*s= ?\n", alignment, " ");
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001945 }
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001946 } else {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001947 ttrace->entry_pending = true;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001948 /* See trace__vfs_getname & trace__sys_exit */
1949 ttrace->filename.pending_open = false;
1950 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001951
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001952 if (trace->current != thread) {
1953 thread__put(trace->current);
1954 trace->current = thread__get(thread);
1955 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001956 err = 0;
1957out_put:
1958 thread__put(thread);
1959 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001960}
1961
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001962static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1963 struct perf_sample *sample)
1964{
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001965 struct thread_trace *ttrace;
1966 struct thread *thread;
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001967 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1968 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001969 char msg[1024];
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001970 void *args, *augmented_args = NULL;
1971 int augmented_args_size;
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001972
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001973 if (sc == NULL)
1974 return -1;
1975
1976 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1977 ttrace = thread__trace(thread, trace->output);
1978 /*
1979 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1980 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1981 */
1982 if (ttrace == NULL)
1983 goto out_put;
1984
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001985 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001986 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 -03001987 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001988 fprintf(trace->output, "%s", msg);
1989 err = 0;
1990out_put:
1991 thread__put(thread);
1992 return err;
1993}
1994
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001995static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1996 struct perf_sample *sample,
1997 struct callchain_cursor *cursor)
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001998{
1999 struct addr_location al;
Ravi Bangoria3a9e9a42018-01-30 11:00:53 +05302000 int max_stack = evsel->attr.sample_max_stack ?
2001 evsel->attr.sample_max_stack :
2002 trace->max_stack;
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03002003 int err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002004
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03002005 if (machine__resolve(trace->host, &al, sample) < 0)
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002006 return -1;
2007
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03002008 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
2009 addr_location__put(&al);
2010 return err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002011}
2012
2013static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
2014{
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002015 /* TODO: user-configurable print_opts */
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03002016 const unsigned int print_opts = EVSEL__PRINT_SYM |
2017 EVSEL__PRINT_DSO |
2018 EVSEL__PRINT_UNKNOWN_AS_ADDR;
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002019
Arnaldo Carvalho de Melod327e602016-04-14 17:53:49 -03002020 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002021}
2022
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01002023static const char *errno_to_name(struct perf_evsel *evsel, int err)
2024{
2025 struct perf_env *env = perf_evsel__env(evsel);
2026 const char *arch_name = perf_env__arch(env);
2027
2028 return arch_syscalls__strerrno(arch_name, err);
2029}
2030
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002031static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002032 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002033 struct perf_sample *sample)
2034{
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09002035 long ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002036 u64 duration = 0;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002037 bool duration_calculated = false;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002038 struct thread *thread;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002039 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
2040 int alignment = trace->args_alignment;
David Ahernbf2575c2013-10-08 21:26:53 -06002041 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002042 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002043
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002044 if (sc == NULL)
2045 return -1;
2046
David Ahern8fb598e2013-09-28 13:13:00 -06002047 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002048 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03002049 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002050 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002051
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03002052 trace__fprintf_sample(trace, evsel, sample, thread);
2053
David Ahernbf2575c2013-10-08 21:26:53 -06002054 if (trace->summary)
2055 thread__update_stats(ttrace, id, sample);
2056
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03002057 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002058
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03002059 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002060 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
2061 ttrace->filename.pending_open = false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002062 ++trace->stats.vfs_getname;
2063 }
2064
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002065 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002066 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002067 if (trace__filter_duration(trace, duration))
2068 goto out;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002069 duration_calculated = true;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002070 } else if (trace->duration_filter)
2071 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02002072
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002073 if (sample->callchain) {
2074 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2075 if (callchain_ret == 0) {
2076 if (callchain_cursor.nr < trace->min_stack)
2077 goto out;
2078 callchain_ret = 1;
2079 }
2080 }
2081
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03002082 if (trace->summary_only || (ret >= 0 && trace->failure_only))
David Ahernfd2eaba2013-11-12 09:31:15 -07002083 goto out;
2084
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002085 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002086
2087 if (ttrace->entry_pending) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002088 printed = fprintf(trace->output, "%s", ttrace->entry_str);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002089 } else {
Arnaldo Carvalho de Meloac6e0222019-01-07 16:54:38 -03002090 printed += fprintf(trace->output, " ... [");
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002091 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
Arnaldo Carvalho de Meloac6e0222019-01-07 16:54:38 -03002092 printed += 9;
2093 printed += fprintf(trace->output, "]: %s()", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002094 }
2095
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002096 printed++; /* the closing ')' */
2097
2098 if (alignment > printed)
2099 alignment -= printed;
2100 else
2101 alignment = 0;
2102
2103 fprintf(trace->output, ")%*s= ", alignment, " ");
2104
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002105 if (sc->fmt == NULL) {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002106 if (ret < 0)
2107 goto errno_print;
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002108signed_print:
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002109 fprintf(trace->output, "%ld", ret);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002110 } else if (ret < 0) {
2111errno_print: {
Masami Hiramatsu942a91e2014-08-14 02:22:41 +00002112 char bf[STRERR_BUFSIZE];
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002113 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01002114 *e = errno_to_name(evsel, -ret);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002115
Arnaldo Carvalho de Melo2c83dfa2018-12-17 12:06:34 -03002116 fprintf(trace->output, "-1 %s (%s)", e, emsg);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002117 }
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002118 } else if (ret == 0 && sc->fmt->timeout)
Arnaldo Carvalho de Melo2c83dfa2018-12-17 12:06:34 -03002119 fprintf(trace->output, "0 (Timeout)");
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002120 else if (ttrace->ret_scnprintf) {
2121 char bf[1024];
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03002122 struct syscall_arg arg = {
2123 .val = ret,
2124 .thread = thread,
2125 .trace = trace,
2126 };
2127 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002128 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002129 fprintf(trace->output, "%s", bf);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002130 } else if (sc->fmt->hexret)
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002131 fprintf(trace->output, "%#lx", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002132 else if (sc->fmt->errpid) {
2133 struct thread *child = machine__find_thread(trace->host, ret, ret);
2134
2135 if (child != NULL) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002136 fprintf(trace->output, "%ld", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002137 if (child->comm_set)
2138 fprintf(trace->output, " (%s)", thread__comm_str(child));
2139 thread__put(child);
2140 }
2141 } else
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002142 goto signed_print;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002143
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002144 fputc('\n', trace->output);
Milian Wolff566a0882016-04-08 13:34:15 +02002145
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002146 /*
2147 * We only consider an 'event' for the sake of --max-events a non-filtered
2148 * sys_enter + sys_exit and other tracepoint events.
2149 */
2150 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2151 interrupted = true;
2152
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002153 if (callchain_ret > 0)
2154 trace__fprintf_callchain(trace, sample);
2155 else if (callchain_ret < 0)
2156 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002157out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002158 ttrace->entry_pending = false;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002159 err = 0;
2160out_put:
2161 thread__put(thread);
2162 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002163}
2164
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002165static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002166 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002167 struct perf_sample *sample)
2168{
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002169 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2170 struct thread_trace *ttrace;
2171 size_t filename_len, entry_str_len, to_move;
2172 ssize_t remaining_space;
2173 char *pos;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002174 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002175
2176 if (!thread)
2177 goto out;
2178
2179 ttrace = thread__priv(thread);
2180 if (!ttrace)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002181 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002182
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002183 filename_len = strlen(filename);
Arnaldo Carvalho de Melo39f0e7a2017-03-24 14:51:28 -03002184 if (filename_len == 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002185 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002186
2187 if (ttrace->filename.namelen < filename_len) {
2188 char *f = realloc(ttrace->filename.name, filename_len + 1);
2189
2190 if (f == NULL)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002191 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002192
2193 ttrace->filename.namelen = filename_len;
2194 ttrace->filename.name = f;
2195 }
2196
2197 strcpy(ttrace->filename.name, filename);
2198 ttrace->filename.pending_open = true;
2199
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002200 if (!ttrace->filename.ptr)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002201 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002202
2203 entry_str_len = strlen(ttrace->entry_str);
2204 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2205 if (remaining_space <= 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002206 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002207
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002208 if (filename_len > (size_t)remaining_space) {
2209 filename += filename_len - remaining_space;
2210 filename_len = remaining_space;
2211 }
2212
2213 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2214 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2215 memmove(pos + filename_len, pos, to_move);
2216 memcpy(pos, filename, filename_len);
2217
2218 ttrace->filename.ptr = 0;
2219 ttrace->filename.entry_str_pos = 0;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002220out_put:
2221 thread__put(thread);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002222out:
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002223 return 0;
2224}
2225
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002226static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002227 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002228 struct perf_sample *sample)
2229{
2230 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2231 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
David Ahern8fb598e2013-09-28 13:13:00 -06002232 struct thread *thread = machine__findnew_thread(trace->host,
Adrian Hunter314add62013-08-27 11:23:03 +03002233 sample->pid,
2234 sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002235 struct thread_trace *ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002236
2237 if (ttrace == NULL)
2238 goto out_dump;
2239
2240 ttrace->runtime_ms += runtime_ms;
2241 trace->runtime_ms += runtime_ms;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002242out_put:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002243 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002244 return 0;
2245
2246out_dump:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002247 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002248 evsel->name,
2249 perf_evsel__strval(evsel, sample, "comm"),
2250 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2251 runtime,
2252 perf_evsel__intval(evsel, sample, "vruntime"));
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002253 goto out_put;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002254}
2255
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002256static int bpf_output__printer(enum binary_printer_ops op,
2257 unsigned int val, void *extra __maybe_unused, FILE *fp)
Wang Nan1d6c9402016-02-26 09:31:55 +00002258{
Wang Nan1d6c9402016-02-26 09:31:55 +00002259 unsigned char ch = (unsigned char)val;
2260
2261 switch (op) {
2262 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002263 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
Wang Nan1d6c9402016-02-26 09:31:55 +00002264 case BINARY_PRINT_DATA_BEGIN:
2265 case BINARY_PRINT_LINE_BEGIN:
2266 case BINARY_PRINT_ADDR:
2267 case BINARY_PRINT_NUM_DATA:
2268 case BINARY_PRINT_NUM_PAD:
2269 case BINARY_PRINT_SEP:
2270 case BINARY_PRINT_CHAR_PAD:
2271 case BINARY_PRINT_LINE_END:
2272 case BINARY_PRINT_DATA_END:
2273 default:
2274 break;
2275 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002276
2277 return 0;
Wang Nan1d6c9402016-02-26 09:31:55 +00002278}
2279
2280static void bpf_output__fprintf(struct trace *trace,
2281 struct perf_sample *sample)
2282{
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002283 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2284 bpf_output__printer, NULL, trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002285 ++trace->nr_events_printed;
Wang Nan1d6c9402016-02-26 09:31:55 +00002286}
2287
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002288static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2289 union perf_event *event __maybe_unused,
2290 struct perf_sample *sample)
2291{
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002292 struct thread *thread;
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002293 int callchain_ret = 0;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002294 /*
2295 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2296 * this event's max_events having been hit and this is an entry coming
2297 * from the ring buffer that we should discard, since the max events
2298 * have already been considered/printed.
2299 */
2300 if (evsel->disabled)
2301 return 0;
2302
2303 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002304
2305 if (sample->callchain) {
2306 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2307 if (callchain_ret == 0) {
2308 if (callchain_cursor.nr < trace->min_stack)
2309 goto out;
2310 callchain_ret = 1;
2311 }
2312 }
2313
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03002314 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002315 trace__fprintf_tstamp(trace, sample->time, trace->output);
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002316
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03002317 if (trace->trace_syscalls && trace->show_duration)
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002318 fprintf(trace->output, "( ): ");
2319
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002320 if (thread)
2321 trace__fprintf_comm_tid(trace, thread, trace->output);
2322
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002323 if (evsel == trace->syscalls.events.augmented) {
2324 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2325 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2326
2327 if (sc) {
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002328 fprintf(trace->output, "%s(", sc->name);
2329 trace__fprintf_sys_enter(trace, evsel, sample);
2330 fputc(')', trace->output);
2331 goto newline;
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002332 }
2333
2334 /*
2335 * XXX: Not having the associated syscall info or not finding/adding
2336 * the thread should never happen, but if it does...
2337 * fall thru and print it as a bpf_output event.
2338 */
2339 }
2340
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002341 fprintf(trace->output, "%s:", evsel->name);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002342
Wang Nan1d6c9402016-02-26 09:31:55 +00002343 if (perf_evsel__is_bpf_output(evsel)) {
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002344 bpf_output__fprintf(trace, sample);
Wang Nan1d6c9402016-02-26 09:31:55 +00002345 } else if (evsel->tp_format) {
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002346 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2347 trace__fprintf_sys_enter(trace, evsel, sample)) {
2348 event_format__fprintf(evsel->tp_format, sample->cpu,
2349 sample->raw_data, sample->raw_size,
2350 trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002351 ++trace->nr_events_printed;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002352
2353 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2354 perf_evsel__disable(evsel);
2355 perf_evsel__close(evsel);
2356 }
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002357 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002358 }
2359
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002360newline:
Changbin Du51125a22018-03-13 18:40:01 +08002361 fprintf(trace->output, "\n");
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002362
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002363 if (callchain_ret > 0)
2364 trace__fprintf_callchain(trace, sample);
2365 else if (callchain_ret < 0)
2366 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2367out:
Arnaldo Carvalho de Meloa937c662018-10-20 09:27:52 -03002368 thread__put(thread);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002369 return 0;
2370}
2371
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002372static void print_location(FILE *f, struct perf_sample *sample,
2373 struct addr_location *al,
2374 bool print_dso, bool print_sym)
2375{
2376
Namhyung Kimbb963e12017-02-17 17:17:38 +09002377 if ((verbose > 0 || print_dso) && al->map)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002378 fprintf(f, "%s@", al->map->dso->long_name);
2379
Namhyung Kimbb963e12017-02-17 17:17:38 +09002380 if ((verbose > 0 || print_sym) && al->sym)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002381 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002382 al->addr - al->sym->start);
2383 else if (al->map)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002384 fprintf(f, "0x%" PRIx64, al->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002385 else
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002386 fprintf(f, "0x%" PRIx64, sample->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002387}
2388
2389static int trace__pgfault(struct trace *trace,
2390 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03002391 union perf_event *event __maybe_unused,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002392 struct perf_sample *sample)
2393{
2394 struct thread *thread;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002395 struct addr_location al;
2396 char map_type = 'd';
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002397 struct thread_trace *ttrace;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002398 int err = -1;
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002399 int callchain_ret = 0;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002400
2401 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002402
2403 if (sample->callchain) {
2404 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2405 if (callchain_ret == 0) {
2406 if (callchain_cursor.nr < trace->min_stack)
2407 goto out_put;
2408 callchain_ret = 1;
2409 }
2410 }
2411
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002412 ttrace = thread__trace(thread, trace->output);
2413 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002414 goto out_put;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002415
2416 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2417 ttrace->pfmaj++;
2418 else
2419 ttrace->pfmin++;
2420
2421 if (trace->summary_only)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002422 goto out;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002423
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002424 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002425
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002426 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002427
2428 fprintf(trace->output, "%sfault [",
2429 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2430 "maj" : "min");
2431
2432 print_location(trace->output, sample, &al, false, true);
2433
2434 fprintf(trace->output, "] => ");
2435
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03002436 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002437
2438 if (!al.map) {
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002439 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002440
2441 if (al.map)
2442 map_type = 'x';
2443 else
2444 map_type = '?';
2445 }
2446
2447 print_location(trace->output, sample, &al, true, false);
2448
2449 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
Arnaldo Carvalho de Melo0c3a6ef2016-04-19 16:31:12 -03002450
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002451 if (callchain_ret > 0)
2452 trace__fprintf_callchain(trace, sample);
2453 else if (callchain_ret < 0)
2454 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002455
2456 ++trace->nr_events_printed;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002457out:
2458 err = 0;
2459out_put:
2460 thread__put(thread);
2461 return err;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002462}
2463
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002464static void trace__set_base_time(struct trace *trace,
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002465 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002466 struct perf_sample *sample)
2467{
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002468 /*
2469 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2470 * and don't use sample->time unconditionally, we may end up having
2471 * some other event in the future without PERF_SAMPLE_TIME for good
2472 * reason, i.e. we may not be interested in its timestamps, just in
2473 * it taking place, picking some piece of information when it
2474 * appears in our event stream (vfs_getname comes to mind).
2475 */
2476 if (trace->base_time == 0 && !trace->full_time &&
2477 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002478 trace->base_time = sample->time;
2479}
2480
David Ahern6810fc92013-08-28 22:29:52 -06002481static int trace__process_sample(struct perf_tool *tool,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002482 union perf_event *event,
David Ahern6810fc92013-08-28 22:29:52 -06002483 struct perf_sample *sample,
2484 struct perf_evsel *evsel,
2485 struct machine *machine __maybe_unused)
2486{
2487 struct trace *trace = container_of(tool, struct trace, tool);
David Ahernaa07df62016-11-25 09:29:52 -07002488 struct thread *thread;
David Ahern6810fc92013-08-28 22:29:52 -06002489 int err = 0;
2490
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002491 tracepoint_handler handler = evsel->handler;
David Ahern6810fc92013-08-28 22:29:52 -06002492
David Ahernaa07df62016-11-25 09:29:52 -07002493 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2494 if (thread && thread__is_filtered(thread))
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002495 goto out;
David Ahernbdc89662013-08-28 22:29:53 -06002496
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002497 trace__set_base_time(trace, evsel, sample);
David Ahern6810fc92013-08-28 22:29:52 -06002498
David Ahern31605652013-12-04 19:41:41 -07002499 if (handler) {
2500 ++trace->nr_events;
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002501 handler(trace, evsel, event, sample);
David Ahern31605652013-12-04 19:41:41 -07002502 }
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002503out:
2504 thread__put(thread);
David Ahern6810fc92013-08-28 22:29:52 -06002505 return err;
2506}
2507
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002508static int trace__record(struct trace *trace, int argc, const char **argv)
David Ahern5e2485b2013-09-28 13:13:01 -06002509{
2510 unsigned int rec_argc, i, j;
2511 const char **rec_argv;
2512 const char * const record_args[] = {
2513 "record",
2514 "-R",
2515 "-m", "1024",
2516 "-c", "1",
David Ahern5e2485b2013-09-28 13:13:01 -06002517 };
2518
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002519 const char * const sc_args[] = { "-e", };
2520 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2521 const char * const majpf_args[] = { "-e", "major-faults" };
2522 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2523 const char * const minpf_args[] = { "-e", "minor-faults" };
2524 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2525
David Ahern9aca7f12013-12-04 19:41:39 -07002526 /* +1 is for the event string below */
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002527 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2528 majpf_args_nr + minpf_args_nr + argc;
David Ahern5e2485b2013-09-28 13:13:01 -06002529 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2530
2531 if (rec_argv == NULL)
2532 return -ENOMEM;
2533
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002534 j = 0;
David Ahern5e2485b2013-09-28 13:13:01 -06002535 for (i = 0; i < ARRAY_SIZE(record_args); i++)
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002536 rec_argv[j++] = record_args[i];
2537
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002538 if (trace->trace_syscalls) {
2539 for (i = 0; i < sc_args_nr; i++)
2540 rec_argv[j++] = sc_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002541
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002542 /* event string may be different for older kernels - e.g., RHEL6 */
2543 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2544 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2545 else if (is_valid_tracepoint("syscalls:sys_enter"))
2546 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2547 else {
2548 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
Martin Kepplingerc896f852017-09-13 21:14:19 +02002549 free(rec_argv);
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002550 return -1;
2551 }
David Ahern9aca7f12013-12-04 19:41:39 -07002552 }
David Ahern9aca7f12013-12-04 19:41:39 -07002553
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002554 if (trace->trace_pgfaults & TRACE_PFMAJ)
2555 for (i = 0; i < majpf_args_nr; i++)
2556 rec_argv[j++] = majpf_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002557
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002558 if (trace->trace_pgfaults & TRACE_PFMIN)
2559 for (i = 0; i < minpf_args_nr; i++)
2560 rec_argv[j++] = minpf_args[i];
2561
2562 for (i = 0; i < (unsigned int)argc; i++)
2563 rec_argv[j++] = argv[i];
2564
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002565 return cmd_record(j, rec_argv);
David Ahern5e2485b2013-09-28 13:13:01 -06002566}
2567
David Ahernbf2575c2013-10-08 21:26:53 -06002568static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2569
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002570static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002571{
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002572 bool found = false;
2573 struct perf_evsel *evsel, *tmp;
2574 struct parse_events_error err = { .idx = 0, };
2575 int ret = parse_events(evlist, "probe:vfs_getname*", &err);
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002576
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002577 if (ret)
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002578 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002579
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002580 evlist__for_each_entry_safe(evlist, evsel, tmp) {
2581 if (!strstarts(perf_evsel__name(evsel), "probe:vfs_getname"))
2582 continue;
2583
2584 if (perf_evsel__field(evsel, "pathname")) {
2585 evsel->handler = trace__vfs_getname;
2586 found = true;
2587 continue;
2588 }
2589
2590 list_del_init(&evsel->node);
2591 evsel->evlist = NULL;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002592 perf_evsel__delete(evsel);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002593 }
2594
Arnaldo Carvalho de Melo6ab3bc22019-01-29 15:12:34 +01002595 return found;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002596}
2597
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002598static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002599{
2600 struct perf_evsel *evsel;
2601 struct perf_event_attr attr = {
2602 .type = PERF_TYPE_SOFTWARE,
2603 .mmap_data = 1,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002604 };
2605
2606 attr.config = config;
Arnaldo Carvalho de Melo05247982014-07-23 18:15:09 -03002607 attr.sample_period = 1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002608
2609 event_attr_init(&attr);
2610
2611 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002612 if (evsel)
2613 evsel->handler = trace__pgfault;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002614
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002615 return evsel;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002616}
2617
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002618static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2619{
2620 const u32 type = event->header.type;
2621 struct perf_evsel *evsel;
2622
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002623 if (type != PERF_RECORD_SAMPLE) {
2624 trace__process_event(trace, trace->host, event, sample);
2625 return;
2626 }
2627
2628 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2629 if (evsel == NULL) {
2630 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2631 return;
2632 }
2633
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002634 trace__set_base_time(trace, evsel, sample);
2635
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002636 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2637 sample->raw_data == NULL) {
2638 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2639 perf_evsel__name(evsel), sample->tid,
2640 sample->cpu, sample->raw_size);
2641 } else {
2642 tracepoint_handler handler = evsel->handler;
2643 handler(trace, evsel, event, sample);
2644 }
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002645
2646 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2647 interrupted = true;
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002648}
2649
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002650static int trace__add_syscall_newtp(struct trace *trace)
2651{
2652 int ret = -1;
2653 struct perf_evlist *evlist = trace->evlist;
2654 struct perf_evsel *sys_enter, *sys_exit;
2655
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002656 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002657 if (sys_enter == NULL)
2658 goto out;
2659
2660 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2661 goto out_delete_sys_enter;
2662
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002663 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002664 if (sys_exit == NULL)
2665 goto out_delete_sys_enter;
2666
2667 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2668 goto out_delete_sys_exit;
2669
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002670 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2671 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2672
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002673 perf_evlist__add(evlist, sys_enter);
2674 perf_evlist__add(evlist, sys_exit);
2675
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002676 if (callchain_param.enabled && !trace->kernel_syscallchains) {
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03002677 /*
2678 * We're interested only in the user space callchain
2679 * leading to the syscall, allow overriding that for
2680 * debugging reasons using --kernel_syscall_callchains
2681 */
2682 sys_exit->attr.exclude_callchain_kernel = 1;
2683 }
2684
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03002685 trace->syscalls.events.sys_enter = sys_enter;
2686 trace->syscalls.events.sys_exit = sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002687
2688 ret = 0;
2689out:
2690 return ret;
2691
2692out_delete_sys_exit:
2693 perf_evsel__delete_priv(sys_exit);
2694out_delete_sys_enter:
2695 perf_evsel__delete_priv(sys_enter);
2696 goto out;
2697}
2698
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002699static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002700{
2701 int err = -1;
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002702 struct perf_evsel *sys_exit;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002703 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2704 trace->ev_qualifier_ids.nr,
2705 trace->ev_qualifier_ids.entries);
2706
2707 if (filter == NULL)
2708 goto out_enomem;
2709
Mathieu Poirier3541c032016-09-16 08:44:04 -06002710 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2711 filter)) {
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002712 sys_exit = trace->syscalls.events.sys_exit;
Mathieu Poirier3541c032016-09-16 08:44:04 -06002713 err = perf_evsel__append_tp_filter(sys_exit, filter);
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002714 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002715
2716 free(filter);
2717out:
2718 return err;
2719out_enomem:
2720 errno = ENOMEM;
2721 goto out;
2722}
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002723
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002724#ifdef HAVE_LIBBPF_SUPPORT
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03002725static void trace__init_bpf_map_syscall_args(struct trace *trace, int id, struct bpf_map_syscall_entry *entry)
2726{
2727 struct syscall *sc = trace__syscall_info(trace, NULL, id);
2728 int arg = 0;
2729
2730 if (sc == NULL)
2731 goto out;
2732
2733 for (; arg < sc->nr_args; ++arg) {
2734 entry->string_args_len[arg] = 0;
2735 if (sc->arg_fmt[arg].scnprintf == SCA_FILENAME) {
2736 /* Should be set like strace -s strsize */
2737 entry->string_args_len[arg] = PATH_MAX;
2738 }
2739 }
2740out:
2741 for (; arg < 6; ++arg)
2742 entry->string_args_len[arg] = 0;
2743}
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002744static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2745{
2746 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002747 struct bpf_map_syscall_entry value = {
2748 .enabled = !trace->not_ev_qualifier,
2749 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002750 int err = 0;
2751 size_t i;
2752
2753 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2754 int key = trace->ev_qualifier_ids.entries[i];
2755
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03002756 if (value.enabled)
2757 trace__init_bpf_map_syscall_args(trace, key, &value);
2758
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002759 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2760 if (err)
2761 break;
2762 }
2763
2764 return err;
2765}
2766
2767static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2768{
2769 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002770 struct bpf_map_syscall_entry value = {
2771 .enabled = enabled,
2772 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002773 int err = 0, key;
2774
2775 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
Arnaldo Carvalho de Melo4cae86752019-06-03 16:52:46 -03002776 if (enabled)
2777 trace__init_bpf_map_syscall_args(trace, key, &value);
2778
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002779 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002780 if (err)
2781 break;
2782 }
2783
2784 return err;
2785}
2786
2787static int trace__init_syscalls_bpf_map(struct trace *trace)
2788{
2789 bool enabled = true;
2790
2791 if (trace->ev_qualifier_ids.nr)
2792 enabled = trace->not_ev_qualifier;
2793
2794 return __trace__init_syscalls_bpf_map(trace, enabled);
2795}
2796#else
2797static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
2798{
2799 return 0;
2800}
2801
2802static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
2803{
2804 return 0;
2805}
2806#endif // HAVE_LIBBPF_SUPPORT
2807
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002808static int trace__set_ev_qualifier_filter(struct trace *trace)
2809{
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002810 if (trace->syscalls.map)
2811 return trace__set_ev_qualifier_bpf_filter(trace);
Arnaldo Carvalho de Melof76214f2018-12-19 10:01:20 -03002812 if (trace->syscalls.events.sys_enter)
2813 return trace__set_ev_qualifier_tp_filter(trace);
2814 return 0;
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002815}
2816
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002817static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2818 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2819{
2820 int err = 0;
2821#ifdef HAVE_LIBBPF_SUPPORT
2822 bool value = true;
2823 int map_fd = bpf_map__fd(map);
2824 size_t i;
2825
2826 for (i = 0; i < npids; ++i) {
2827 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2828 if (err)
2829 break;
2830 }
2831#endif
2832 return err;
2833}
2834
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002835static int trace__set_filter_loop_pids(struct trace *trace)
2836{
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002837 unsigned int nr = 1, err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002838 pid_t pids[32] = {
2839 getpid(),
2840 };
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03002841 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2842
2843 while (thread && nr < ARRAY_SIZE(pids)) {
2844 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2845
2846 if (parent == NULL)
2847 break;
2848
Arnaldo Carvalho de Meloca2da702019-02-12 13:51:49 -03002849 if (!strcmp(thread__comm_str(parent), "sshd") ||
2850 strstarts(thread__comm_str(parent), "gnome-terminal")) {
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03002851 pids[nr++] = parent->tid;
2852 break;
2853 }
2854 thread = parent;
2855 }
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002856
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002857 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2858 if (!err && trace->filter_pids.map)
2859 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2860
2861 return err;
2862}
2863
2864static int trace__set_filter_pids(struct trace *trace)
2865{
2866 int err = 0;
2867 /*
2868 * Better not use !target__has_task() here because we need to cover the
2869 * case where no threads were specified in the command line, but a
2870 * workload was, and in that case we will fill in the thread_map when
2871 * we fork the workload in perf_evlist__prepare_workload.
2872 */
2873 if (trace->filter_pids.nr > 0) {
2874 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2875 trace->filter_pids.entries);
2876 if (!err && trace->filter_pids.map) {
2877 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2878 trace->filter_pids.entries);
2879 }
2880 } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2881 err = trace__set_filter_loop_pids(trace);
2882 }
2883
2884 return err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002885}
2886
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002887static int __trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa1f44b3e2018-12-05 17:05:08 +01002888{
2889 struct perf_evlist *evlist = trace->evlist;
2890 struct perf_sample sample;
2891 int err;
2892
2893 err = perf_evlist__parse_sample(evlist, event, &sample);
2894 if (err)
2895 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
2896 else
2897 trace__handle_event(trace, event, &sample);
2898
2899 return 0;
2900}
2901
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002902static int __trace__flush_events(struct trace *trace)
Jiri Olsa028713a2018-12-05 17:05:09 +01002903{
2904 u64 first = ordered_events__first_time(&trace->oe.data);
2905 u64 flush = trace->oe.last - NSEC_PER_SEC;
2906
2907 /* Is there some thing to flush.. */
2908 if (first && first < flush)
2909 return ordered_events__flush_time(&trace->oe.data, flush);
2910
2911 return 0;
2912}
2913
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002914static int trace__flush_events(struct trace *trace)
2915{
2916 return !trace->sort_events ? 0 : __trace__flush_events(trace);
2917}
2918
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002919static int trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa028713a2018-12-05 17:05:09 +01002920{
Jiri Olsa028713a2018-12-05 17:05:09 +01002921 int err;
2922
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002923 if (!trace->sort_events)
2924 return __trace__deliver_event(trace, event);
2925
2926 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
Jiri Olsa028713a2018-12-05 17:05:09 +01002927 if (err && err != -1)
2928 return err;
2929
2930 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
2931 if (err)
2932 return err;
2933
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002934 return trace__flush_events(trace);
Jiri Olsa028713a2018-12-05 17:05:09 +01002935}
2936
2937static int ordered_events__deliver_event(struct ordered_events *oe,
2938 struct ordered_event *event)
2939{
2940 struct trace *trace = container_of(oe, struct trace, oe.data);
2941
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002942 return __trace__deliver_event(trace, event->event);
Jiri Olsa028713a2018-12-05 17:05:09 +01002943}
2944
Namhyung Kimf15eb532012-10-05 14:02:16 +09002945static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002946{
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002947 struct perf_evlist *evlist = trace->evlist;
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002948 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002949 int err = -1, i;
2950 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002951 const bool forks = argc > 0;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002952 bool draining = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002953
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002954 trace->live = true;
2955
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002956 if (!trace->raw_augmented_syscalls) {
2957 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
2958 goto out_error_raw_syscalls;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002959
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002960 if (trace->trace_syscalls)
2961 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
2962 }
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002963
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002964 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2965 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2966 if (pgfault_maj == NULL)
2967 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002968 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002969 perf_evlist__add(evlist, pgfault_maj);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03002970 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002971
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002972 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2973 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2974 if (pgfault_min == NULL)
2975 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002976 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002977 perf_evlist__add(evlist, pgfault_min);
2978 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002979
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002980 if (trace->sched &&
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002981 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2982 trace__sched_stat_runtime))
2983 goto out_error_sched_stat_runtime;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002984
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03002985 /*
2986 * If a global cgroup was set, apply it to all the events without an
2987 * explicit cgroup. I.e.:
2988 *
2989 * trace -G A -e sched:*switch
2990 *
2991 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2992 * _and_ sched:sched_switch to the 'A' cgroup, while:
2993 *
2994 * trace -e sched:*switch -G A
2995 *
2996 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2997 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2998 * a cgroup (on the root cgroup, sys wide, etc).
2999 *
3000 * Multiple cgroups:
3001 *
3002 * trace -G A -e sched:*switch -G B
3003 *
3004 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
3005 * to the 'B' cgroup.
3006 *
3007 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
3008 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
3009 */
3010 if (trace->cgroup)
3011 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
3012
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003013 err = perf_evlist__create_maps(evlist, &trace->opts.target);
3014 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003015 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003016 goto out_delete_evlist;
3017 }
3018
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03003019 err = trace__symbols_init(trace, evlist);
3020 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003021 fprintf(trace->output, "Problems initializing symbol libraries!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003022 goto out_delete_evlist;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03003023 }
3024
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003025 perf_evlist__config(evlist, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melofde54b72016-04-11 18:42:37 -03003026
Namhyung Kimf15eb532012-10-05 14:02:16 +09003027 signal(SIGCHLD, sig_handler);
3028 signal(SIGINT, sig_handler);
3029
3030 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09003031 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03003032 argv, false, NULL);
Namhyung Kimf15eb532012-10-05 14:02:16 +09003033 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003034 fprintf(trace->output, "Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003035 goto out_delete_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003036 }
3037 }
3038
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003039 err = perf_evlist__open(evlist);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003040 if (err < 0)
3041 goto out_error_open;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003042
Wang Nanba504232016-02-26 09:31:54 +00003043 err = bpf__apply_obj_config();
3044 if (err) {
3045 char errbuf[BUFSIZ];
3046
3047 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
3048 pr_err("ERROR: Apply config to BPF failed: %s\n",
3049 errbuf);
3050 goto out_error_open;
3051 }
3052
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03003053 err = trace__set_filter_pids(trace);
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003054 if (err < 0)
3055 goto out_error_mem;
3056
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003057 if (trace->syscalls.map)
3058 trace__init_syscalls_bpf_map(trace);
3059
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003060 if (trace->ev_qualifier_ids.nr > 0) {
3061 err = trace__set_ev_qualifier_filter(trace);
3062 if (err < 0)
3063 goto out_errno;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003064
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03003065 if (trace->syscalls.events.sys_exit) {
3066 pr_debug("event qualifier tracepoint filter: %s\n",
3067 trace->syscalls.events.sys_exit->filter);
3068 }
Arnaldo Carvalho de Melo2e5e5f82015-08-03 17:12:29 -03003069 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003070
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003071 err = perf_evlist__apply_filters(evlist, &evsel);
3072 if (err < 0)
3073 goto out_error_apply_filters;
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08003074
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03003075 if (trace->dump.map)
3076 bpf_map__fprintf(trace->dump.map, trace->output);
3077
Wang Nanf74b9d3a2017-12-03 02:00:37 +00003078 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03003079 if (err < 0)
3080 goto out_error_mmap;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003081
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003082 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
Arnaldo Carvalho de Melocb24d012015-04-22 10:04:23 -03003083 perf_evlist__enable(evlist);
3084
Namhyung Kimf15eb532012-10-05 14:02:16 +09003085 if (forks)
3086 perf_evlist__start_workload(evlist);
3087
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003088 if (trace->opts.initial_delay) {
3089 usleep(trace->opts.initial_delay * 1000);
3090 perf_evlist__enable(evlist);
3091 }
3092
Jiri Olsae13798c2015-06-23 00:36:02 +02003093 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
Arnaldo Carvalho de Melo42052be2015-02-13 12:32:45 -03003094 evlist->threads->nr > 1 ||
3095 perf_evlist__first(evlist)->attr.inherit;
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03003096
3097 /*
3098 * Now that we already used evsel->attr to ask the kernel to setup the
3099 * events, lets reuse evsel->attr.sample_max_stack as the limit in
3100 * trace__resolve_callchain(), allowing per-event max-stack settings
Ingo Molnaradba1632018-12-03 11:22:00 +01003101 * to override an explicitly set --max-stack global setting.
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03003102 */
3103 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03003104 if (evsel__has_callchain(evsel) &&
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03003105 evsel->attr.sample_max_stack == 0)
3106 evsel->attr.sample_max_stack = trace->max_stack;
3107 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003108again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003109 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003110
3111 for (i = 0; i < evlist->nr_mmaps; i++) {
3112 union perf_event *event;
Kan Liangd7f55c62018-03-01 18:08:59 -05003113 struct perf_mmap *md;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003114
Kan Liangd7f55c62018-03-01 18:08:59 -05003115 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -05003116 if (perf_mmap__read_init(md) < 0)
Kan Liangd7f55c62018-03-01 18:08:59 -05003117 continue;
3118
Kan Liang0019dc872018-03-06 10:36:06 -05003119 while ((event = perf_mmap__read_event(md)) != NULL) {
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003120 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003121
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003122 err = trace__deliver_event(trace, event);
Jiri Olsa028713a2018-12-05 17:05:09 +01003123 if (err)
3124 goto out_disable;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003125
Kan Liangd6ace3d2018-03-06 10:36:05 -05003126 perf_mmap__consume(md);
Arnaldo Carvalho de Melo20c5f102013-09-03 11:55:07 -03003127
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003128 if (interrupted)
3129 goto out_disable;
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03003130
3131 if (done && !draining) {
3132 perf_evlist__disable(evlist);
3133 draining = true;
3134 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003135 }
Kan Liangd7f55c62018-03-01 18:08:59 -05003136 perf_mmap__read_done(md);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003137 }
3138
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003139 if (trace->nr_events == before) {
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003140 int timeout = done ? 100 : -1;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003141
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003142 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03003143 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003144 draining = true;
3145
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003146 goto again;
Jiri Olsa028713a2018-12-05 17:05:09 +01003147 } else {
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003148 if (trace__flush_events(trace))
Jiri Olsa028713a2018-12-05 17:05:09 +01003149 goto out_disable;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003150 }
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003151 } else {
3152 goto again;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003153 }
3154
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003155out_disable:
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03003156 thread__zput(trace->current);
3157
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003158 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003159
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003160 if (trace->sort_events)
3161 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
Jiri Olsa028713a2018-12-05 17:05:09 +01003162
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003163 if (!err) {
3164 if (trace->summary)
3165 trace__fprintf_thread_summary(trace, trace->output);
3166
3167 if (trace->show_tool_stats) {
3168 fprintf(trace->output, "Stats:\n "
3169 " vfs_getname : %" PRIu64 "\n"
3170 " proc_getname: %" PRIu64 "\n",
3171 trace->stats.vfs_getname,
3172 trace->stats.proc_getname);
3173 }
3174 }
David Ahernbf2575c2013-10-08 21:26:53 -06003175
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003176out_delete_evlist:
Andrei Vagin33974a42017-11-07 16:22:45 -08003177 trace__symbols__exit(trace);
3178
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003179 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003180 cgroup__put(trace->cgroup);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003181 trace->evlist = NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03003182 trace->live = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003183 return err;
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003184{
3185 char errbuf[BUFSIZ];
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003186
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003187out_error_sched_stat_runtime:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003188 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003189 goto out_error;
3190
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03003191out_error_raw_syscalls:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003192 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003193 goto out_error;
3194
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03003195out_error_mmap:
3196 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3197 goto out_error;
3198
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003199out_error_open:
3200 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3201
3202out_error:
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003203 fprintf(trace->output, "%s\n", errbuf);
Ramkumar Ramachandra87f91862013-10-04 10:47:31 +05303204 goto out_delete_evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003205
3206out_error_apply_filters:
3207 fprintf(trace->output,
3208 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3209 evsel->filter, perf_evsel__name(evsel), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03003210 str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003211 goto out_delete_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003212}
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03003213out_error_mem:
3214 fprintf(trace->output, "Not enough memory to run!\n");
3215 goto out_delete_evlist;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003216
3217out_errno:
3218 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3219 goto out_delete_evlist;
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003220}
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003221
David Ahern6810fc92013-08-28 22:29:52 -06003222static int trace__replay(struct trace *trace)
3223{
3224 const struct perf_evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003225 { "probe:vfs_getname", trace__vfs_getname, },
David Ahern6810fc92013-08-28 22:29:52 -06003226 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003227 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +01003228 .path = input_name,
3229 .mode = PERF_DATA_MODE_READ,
3230 .force = trace->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003231 };
David Ahern6810fc92013-08-28 22:29:52 -06003232 struct perf_session *session;
Namhyung Kim003824e2013-11-12 15:25:00 +09003233 struct perf_evsel *evsel;
David Ahern6810fc92013-08-28 22:29:52 -06003234 int err = -1;
3235
3236 trace->tool.sample = trace__process_sample;
3237 trace->tool.mmap = perf_event__process_mmap;
David Ahern384c6712013-09-22 19:44:58 -06003238 trace->tool.mmap2 = perf_event__process_mmap2;
David Ahern6810fc92013-08-28 22:29:52 -06003239 trace->tool.comm = perf_event__process_comm;
3240 trace->tool.exit = perf_event__process_exit;
3241 trace->tool.fork = perf_event__process_fork;
3242 trace->tool.attr = perf_event__process_attr;
Hari Bathinif3b36142017-03-08 02:11:43 +05303243 trace->tool.tracing_data = perf_event__process_tracing_data;
David Ahern6810fc92013-08-28 22:29:52 -06003244 trace->tool.build_id = perf_event__process_build_id;
Hari Bathinif3b36142017-03-08 02:11:43 +05303245 trace->tool.namespaces = perf_event__process_namespaces;
David Ahern6810fc92013-08-28 22:29:52 -06003246
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003247 trace->tool.ordered_events = true;
David Ahern6810fc92013-08-28 22:29:52 -06003248 trace->tool.ordering_requires_timestamps = true;
3249
3250 /* add tid to output */
3251 trace->multiple_threads = true;
3252
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003253 session = perf_session__new(&data, false, &trace->tool);
David Ahern6810fc92013-08-28 22:29:52 -06003254 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003255 return -1;
David Ahern6810fc92013-08-28 22:29:52 -06003256
David Ahernaa07df62016-11-25 09:29:52 -07003257 if (trace->opts.target.pid)
3258 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3259
3260 if (trace->opts.target.tid)
3261 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3262
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003263 if (symbol__init(&session->header.env) < 0)
Namhyung Kimcb2ffae2014-08-12 15:40:44 +09003264 goto out;
3265
David Ahern8fb598e2013-09-28 13:13:00 -06003266 trace->host = &session->machines.host;
3267
David Ahern6810fc92013-08-28 22:29:52 -06003268 err = perf_session__set_tracepoints_handlers(session, handlers);
3269 if (err)
3270 goto out;
3271
Namhyung Kim003824e2013-11-12 15:25:00 +09003272 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3273 "raw_syscalls:sys_enter");
David Ahern9aca7f12013-12-04 19:41:39 -07003274 /* older kernels have syscalls tp versus raw_syscalls */
3275 if (evsel == NULL)
3276 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3277 "syscalls:sys_enter");
David Ahern6810fc92013-08-28 22:29:52 -06003278
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003279 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003280 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003281 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003282 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3283 goto out;
3284 }
3285
3286 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3287 "raw_syscalls:sys_exit");
David Ahern9aca7f12013-12-04 19:41:39 -07003288 if (evsel == NULL)
3289 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3290 "syscalls:sys_exit");
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003291 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003292 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003293 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003294 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
David Ahern6810fc92013-08-28 22:29:52 -06003295 goto out;
3296 }
3297
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003298 evlist__for_each_entry(session->evlist, evsel) {
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003299 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
3300 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3301 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3302 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3303 evsel->handler = trace__pgfault;
3304 }
3305
David Ahern6810fc92013-08-28 22:29:52 -06003306 setup_pager();
3307
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03003308 err = perf_session__process_events(session);
David Ahern6810fc92013-08-28 22:29:52 -06003309 if (err)
3310 pr_err("Failed to process events, error %d", err);
3311
David Ahernbf2575c2013-10-08 21:26:53 -06003312 else if (trace->summary)
3313 trace__fprintf_thread_summary(trace, trace->output);
3314
David Ahern6810fc92013-08-28 22:29:52 -06003315out:
3316 perf_session__delete(session);
3317
3318 return err;
3319}
3320
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003321static size_t trace__fprintf_threads_header(FILE *fp)
3322{
3323 size_t printed;
3324
Pekka Enberg99ff7152013-11-12 16:42:14 +02003325 printed = fprintf(fp, "\n Summary of events:\n\n");
David Ahernbf2575c2013-10-08 21:26:53 -06003326
3327 return printed;
3328}
3329
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003330DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3331 struct stats *stats;
3332 double msecs;
3333 int syscall;
3334)
3335{
3336 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3337 struct stats *stats = source->priv;
3338
3339 entry->syscall = source->i;
3340 entry->stats = stats;
3341 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3342}
3343
David Ahernbf2575c2013-10-08 21:26:53 -06003344static size_t thread__dump_stats(struct thread_trace *ttrace,
3345 struct trace *trace, FILE *fp)
3346{
David Ahernbf2575c2013-10-08 21:26:53 -06003347 size_t printed = 0;
3348 struct syscall *sc;
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003349 struct rb_node *nd;
3350 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003351
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003352 if (syscall_stats == NULL)
David Ahernbf2575c2013-10-08 21:26:53 -06003353 return 0;
3354
3355 printed += fprintf(fp, "\n");
3356
Milian Wolff834fd462015-08-06 11:24:29 +02003357 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3358 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3359 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
Pekka Enberg99ff7152013-11-12 16:42:14 +02003360
Arnaldo Carvalho de Melo98a91832016-06-23 11:34:10 -03003361 resort_rb__for_each_entry(nd, syscall_stats) {
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003362 struct stats *stats = syscall_stats_entry->stats;
David Ahernbf2575c2013-10-08 21:26:53 -06003363 if (stats) {
3364 double min = (double)(stats->min) / NSEC_PER_MSEC;
3365 double max = (double)(stats->max) / NSEC_PER_MSEC;
3366 double avg = avg_stats(stats);
3367 double pct;
3368 u64 n = (u64) stats->n;
3369
3370 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3371 avg /= NSEC_PER_MSEC;
3372
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003373 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
Pekka Enberg99ff7152013-11-12 16:42:14 +02003374 printed += fprintf(fp, " %-15s", sc->name);
Milian Wolff834fd462015-08-06 11:24:29 +02003375 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003376 n, syscall_stats_entry->msecs, min, avg);
Pekka Enberg27a778b2013-11-13 14:21:48 +02003377 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
David Ahernbf2575c2013-10-08 21:26:53 -06003378 }
David Ahernbf2575c2013-10-08 21:26:53 -06003379 }
3380
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003381 resort_rb__delete(syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003382 printed += fprintf(fp, "\n\n");
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003383
3384 return printed;
3385}
3386
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003387static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
David Ahern896cbb52013-09-28 13:12:59 -06003388{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003389 size_t printed = 0;
Namhyung Kim89dceb22014-10-06 09:46:03 +09003390 struct thread_trace *ttrace = thread__priv(thread);
David Ahern896cbb52013-09-28 13:12:59 -06003391 double ratio;
3392
3393 if (ttrace == NULL)
3394 return 0;
3395
3396 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3397
Pekka Enberg15e65c62013-11-14 18:43:30 +02003398 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
Pekka Enberg99ff7152013-11-12 16:42:14 +02003399 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
Pekka Enberg15e65c62013-11-14 18:43:30 +02003400 printed += fprintf(fp, "%.1f%%", ratio);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04003401 if (ttrace->pfmaj)
3402 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3403 if (ttrace->pfmin)
3404 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
Arnaldo Carvalho de Melo03548eb2016-05-05 15:46:50 -03003405 if (trace->sched)
3406 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3407 else if (fputc('\n', fp) != EOF)
3408 ++printed;
3409
David Ahernbf2575c2013-10-08 21:26:53 -06003410 printed += thread__dump_stats(ttrace, trace, fp);
David Ahern896cbb52013-09-28 13:12:59 -06003411
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003412 return printed;
3413}
David Ahern896cbb52013-09-28 13:12:59 -06003414
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003415static unsigned long thread__nr_events(struct thread_trace *ttrace)
3416{
3417 return ttrace ? ttrace->nr_events : 0;
3418}
3419
3420DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3421 struct thread *thread;
3422)
3423{
3424 entry->thread = rb_entry(nd, struct thread, rb_node);
David Ahern896cbb52013-09-28 13:12:59 -06003425}
3426
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003427static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3428{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003429 size_t printed = trace__fprintf_threads_header(fp);
3430 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -07003431 int i;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003432
Kan Liang91e467b2017-09-10 19:23:14 -07003433 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3434 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3435
3436 if (threads == NULL) {
3437 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3438 return 0;
3439 }
3440
3441 resort_rb__for_each_entry(nd, threads)
3442 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3443
3444 resort_rb__delete(threads);
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003445 }
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003446 return printed;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003447}
3448
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003449static int trace__set_duration(const struct option *opt, const char *str,
3450 int unset __maybe_unused)
3451{
3452 struct trace *trace = opt->value;
3453
3454 trace->duration_filter = atof(str);
3455 return 0;
3456}
3457
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003458static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3459 int unset __maybe_unused)
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08003460{
3461 int ret = -1;
3462 size_t i;
3463 struct trace *trace = opt->value;
3464 /*
3465 * FIXME: introduce a intarray class, plain parse csv and create a
3466 * { int nr, int entries[] } struct...
3467 */
3468 struct intlist *list = intlist__new(str);
3469
3470 if (list == NULL)
3471 return -1;
3472
3473 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3474 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3475
3476 if (trace->filter_pids.entries == NULL)
3477 goto out;
3478
3479 trace->filter_pids.entries[0] = getpid();
3480
3481 for (i = 1; i < trace->filter_pids.nr; ++i)
3482 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3483
3484 intlist__delete(list);
3485 ret = 0;
3486out:
3487 return ret;
3488}
3489
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003490static int trace__open_output(struct trace *trace, const char *filename)
3491{
3492 struct stat st;
3493
3494 if (!stat(filename, &st) && st.st_size) {
3495 char oldname[PATH_MAX];
3496
3497 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3498 unlink(oldname);
3499 rename(filename, oldname);
3500 }
3501
3502 trace->output = fopen(filename, "w");
3503
3504 return trace->output == NULL ? -errno : 0;
3505}
3506
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003507static int parse_pagefaults(const struct option *opt, const char *str,
3508 int unset __maybe_unused)
3509{
3510 int *trace_pgfaults = opt->value;
3511
3512 if (strcmp(str, "all") == 0)
3513 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3514 else if (strcmp(str, "maj") == 0)
3515 *trace_pgfaults |= TRACE_PFMAJ;
3516 else if (strcmp(str, "min") == 0)
3517 *trace_pgfaults |= TRACE_PFMIN;
3518 else
3519 return -1;
3520
3521 return 0;
3522}
3523
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003524static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3525{
3526 struct perf_evsel *evsel;
3527
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003528 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003529 evsel->handler = handler;
3530}
3531
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003532static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3533{
3534 struct perf_evsel *evsel;
3535
3536 evlist__for_each_entry(evlist, evsel) {
3537 if (evsel->priv || !evsel->tp_format)
3538 continue;
3539
3540 if (strcmp(evsel->tp_format->system, "syscalls"))
3541 continue;
3542
3543 if (perf_evsel__init_syscall_tp(evsel))
3544 return -1;
3545
3546 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3547 struct syscall_tp *sc = evsel->priv;
3548
3549 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3550 return -1;
3551 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3552 struct syscall_tp *sc = evsel->priv;
3553
3554 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3555 return -1;
3556 }
3557 }
3558
3559 return 0;
3560}
3561
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003562/*
3563 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3564 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3565 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3566 *
3567 * It'd be better to introduce a parse_options() variant that would return a
3568 * list with the terms it didn't match to an event...
3569 */
3570static int trace__parse_events_option(const struct option *opt, const char *str,
3571 int unset __maybe_unused)
3572{
3573 struct trace *trace = (struct trace *)opt->value;
3574 const char *s = str;
3575 char *sep = NULL, *lists[2] = { NULL, NULL, };
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003576 int len = strlen(str) + 1, err = -1, list, idx;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003577 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3578 char group_name[PATH_MAX];
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003579 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003580
3581 if (strace_groups_dir == NULL)
3582 return -1;
3583
3584 if (*s == '!') {
3585 ++s;
3586 trace->not_ev_qualifier = true;
3587 }
3588
3589 while (1) {
3590 if ((sep = strchr(s, ',')) != NULL)
3591 *sep = '\0';
3592
3593 list = 0;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003594 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3595 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003596 list = 1;
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003597 goto do_concat;
3598 }
3599
3600 fmt = syscall_fmt__find_by_alias(s);
3601 if (fmt != NULL) {
3602 list = 1;
3603 s = fmt->name;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003604 } else {
3605 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3606 if (access(group_name, R_OK) == 0)
3607 list = 1;
3608 }
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003609do_concat:
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003610 if (lists[list]) {
3611 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3612 } else {
3613 lists[list] = malloc(len);
3614 if (lists[list] == NULL)
3615 goto out;
3616 strcpy(lists[list], s);
3617 }
3618
3619 if (!sep)
3620 break;
3621
3622 *sep = ',';
3623 s = sep + 1;
3624 }
3625
3626 if (lists[1] != NULL) {
3627 struct strlist_config slist_config = {
3628 .dirname = strace_groups_dir,
3629 };
3630
3631 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3632 if (trace->ev_qualifier == NULL) {
3633 fputs("Not enough memory to parse event qualifier", trace->output);
3634 goto out;
3635 }
3636
3637 if (trace__validate_ev_qualifier(trace))
3638 goto out;
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003639 trace->trace_syscalls = true;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003640 }
3641
3642 err = 0;
3643
3644 if (lists[0]) {
3645 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3646 "event selector. use 'perf list' to list available events",
3647 parse_events_option);
3648 err = parse_events_option(&o, lists[0], 0);
3649 }
3650out:
3651 if (sep)
3652 *sep = ',';
3653
3654 return err;
3655}
3656
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003657static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3658{
3659 struct trace *trace = opt->value;
3660
3661 if (!list_empty(&trace->evlist->entries))
3662 return parse_cgroups(opt, str, unset);
3663
3664 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3665
3666 return 0;
3667}
3668
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003669static struct bpf_map *bpf__find_map_by_name(const char *name)
3670{
3671 struct bpf_object *obj, *tmp;
3672
3673 bpf_object__for_each_safe(obj, tmp) {
3674 struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3675 if (map)
3676 return map;
3677
3678 }
3679
3680 return NULL;
3681}
3682
3683static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3684{
3685 trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3686}
3687
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003688static void trace__set_bpf_map_syscalls(struct trace *trace)
3689{
3690 trace->syscalls.map = bpf__find_map_by_name("syscalls");
3691}
3692
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003693static int trace__config(const char *var, const char *value, void *arg)
3694{
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03003695 struct trace *trace = arg;
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003696 int err = 0;
3697
3698 if (!strcmp(var, "trace.add_events")) {
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003699 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3700 "event selector. use 'perf list' to list available events",
3701 parse_events_option);
Leo Yan012749ca2019-06-06 10:38:59 -03003702 /*
3703 * We can't propagate parse_event_option() return, as it is 1
3704 * for failure while perf_config() expects -1.
3705 */
3706 if (parse_events_option(&o, value, 0))
3707 err = -1;
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03003708 } else if (!strcmp(var, "trace.show_timestamp")) {
3709 trace->show_tstamp = perf_config_bool(var, value);
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03003710 } else if (!strcmp(var, "trace.show_duration")) {
3711 trace->show_duration = perf_config_bool(var, value);
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003712 } else if (!strcmp(var, "trace.show_arg_names")) {
3713 trace->show_arg_names = perf_config_bool(var, value);
3714 if (!trace->show_arg_names)
3715 trace->show_zeros = true;
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03003716 } else if (!strcmp(var, "trace.show_zeros")) {
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003717 bool new_show_zeros = perf_config_bool(var, value);
3718 if (!trace->show_arg_names && !new_show_zeros) {
3719 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
3720 goto out;
3721 }
3722 trace->show_zeros = new_show_zeros;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -03003723 } else if (!strcmp(var, "trace.show_prefix")) {
3724 trace->show_string_prefix = perf_config_bool(var, value);
Arnaldo Carvalho de Melod32de872018-12-14 11:06:16 -03003725 } else if (!strcmp(var, "trace.no_inherit")) {
3726 trace->opts.no_inherit = perf_config_bool(var, value);
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -03003727 } else if (!strcmp(var, "trace.args_alignment")) {
3728 int args_alignment = 0;
3729 if (perf_config_int(&args_alignment, var, value) == 0)
3730 trace->args_alignment = args_alignment;
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003731 }
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003732out:
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003733 return err;
3734}
3735
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003736int cmd_trace(int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003737{
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003738 const char *trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +09003739 "perf trace [<options>] [<command>]",
3740 "perf trace [<options>] -- <command> [<options>]",
David Ahern5e2485b2013-09-28 13:13:01 -06003741 "perf trace record [<options>] [<command>]",
3742 "perf trace record [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003743 NULL
3744 };
3745 struct trace trace = {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003746 .syscalls = {
3747 . max = -1,
3748 },
3749 .opts = {
3750 .target = {
3751 .uid = UINT_MAX,
3752 .uses_mmap = true,
3753 },
3754 .user_freq = UINT_MAX,
3755 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03003756 .no_buffering = true,
Arnaldo Carvalho de Melo38d54472014-12-12 17:28:32 -03003757 .mmap_pages = UINT_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003758 },
Milian Wolff007d66a2015-08-05 16:52:23 -03003759 .output = stderr,
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003760 .show_comm = true,
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03003761 .show_tstamp = true,
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03003762 .show_duration = true,
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003763 .show_arg_names = true,
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -03003764 .args_alignment = 70,
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003765 .trace_syscalls = false,
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003766 .kernel_syscallchains = false,
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003767 .max_stack = UINT_MAX,
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003768 .max_events = ULONG_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003769 };
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03003770 const char *map_dump_str = NULL;
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003771 const char *output_name = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003772 const struct option trace_options[] = {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003773 OPT_CALLBACK('e', "event", &trace, "event",
3774 "event/syscall selector. use 'perf list' to list available events",
3775 trace__parse_events_option),
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003776 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3777 "show the thread COMM next to its id"),
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003778 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003779 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3780 trace__parse_events_option),
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003781 OPT_STRING('o', "output", &output_name, "file", "output file name"),
David Ahern6810fc92013-08-28 22:29:52 -06003782 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003783 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3784 "trace events on existing process id"),
David Ahernac9be8e2013-08-20 11:15:45 -06003785 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003786 "trace events on existing thread id"),
Arnaldo Carvalho de Melofa0e4ff2015-04-23 11:59:20 -03003787 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003788 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
David Ahernac9be8e2013-08-20 11:15:45 -06003789 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003790 "system-wide collection from all CPUs"),
David Ahernac9be8e2013-08-20 11:15:45 -06003791 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003792 "list of cpus to monitor"),
David Ahern6810fc92013-08-28 22:29:52 -06003793 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003794 "child tasks do not inherit counters"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02003795 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3796 "number of mmap data pages",
3797 perf_evlist__parse_mmap_pages),
David Ahernac9be8e2013-08-20 11:15:45 -06003798 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003799 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003800 OPT_CALLBACK(0, "duration", &trace, "float",
3801 "show only events with duration > N.M ms",
3802 trace__set_duration),
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03003803#ifdef HAVE_LIBBPF_SUPPORT
3804 OPT_STRING(0, "map-dump", &map_dump_str, "BPF map", "BPF map to periodically dump"),
3805#endif
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003806 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03003807 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
David Ahern4bb09192013-09-04 12:37:43 -06003808 OPT_BOOLEAN('T', "time", &trace.full_time,
3809 "Show full timestamp, not time relative to first start"),
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03003810 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3811 "Show only syscalls that failed"),
David Ahernfd2eaba2013-11-12 09:31:15 -07003812 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3813 "Show only syscall summary with statistics"),
3814 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3815 "Show all syscalls and summary with statistics"),
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003816 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3817 "Trace pagefaults", parse_pagefaults, "maj"),
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003818 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
Yunlong Songe366a6d2015-04-02 21:47:18 +08003819 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
Milian Wolff566a0882016-04-08 13:34:15 +02003820 OPT_CALLBACK(0, "call-graph", &trace.opts,
3821 "record_mode[,record_size]", record_callchain_help,
3822 &record_parse_callchain_opt),
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003823 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3824 "Show the kernel callchains on the syscall exit path"),
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003825 OPT_ULONG(0, "max-events", &trace.max_events,
3826 "Set the maximum number of events to print, exit after that is reached. "),
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03003827 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3828 "Set the minimum stack depth when parsing the callchain, "
3829 "anything below the specified depth will be ignored."),
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -03003830 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3831 "Set the maximum stack depth when parsing the callchain, "
3832 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003833 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003834 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
3835 "Sort batch of events before processing, use if getting out of order events"),
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03003836 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3837 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08003838 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04003839 "per thread proc mmap processing timeout in ms"),
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003840 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3841 trace__parse_cgroups),
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003842 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3843 "ms to wait before starting measurement after program "
3844 "start"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003845 OPT_END()
3846 };
Arnaldo Carvalho de Meloccd62a82016-04-16 09:36:32 -03003847 bool __maybe_unused max_stack_user_set = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003848 bool mmap_pages_user_set = true;
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003849 struct perf_evsel *evsel;
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003850 const char * const trace_subcommands[] = { "record", NULL };
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003851 int err = -1;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003852 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003853
Arnaldo Carvalho de Melo4d08cb82015-02-24 15:35:55 -03003854 signal(SIGSEGV, sighandler_dump_stack);
3855 signal(SIGFPE, sighandler_dump_stack);
3856
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003857 trace.evlist = perf_evlist__new();
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003858 trace.sctbl = syscalltbl__new();
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003859
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003860 if (trace.evlist == NULL || trace.sctbl == NULL) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003861 pr_err("Not enough memory to run!\n");
He Kuangff8f6952015-05-11 12:28:36 +00003862 err = -ENOMEM;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003863 goto out;
3864 }
3865
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003866 err = perf_config(trace__config, &trace);
3867 if (err)
3868 goto out;
3869
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003870 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3871 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
David Ahernfd2eaba2013-11-12 09:31:15 -07003872
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003873 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3874 usage_with_options_msg(trace_usage, trace_options,
3875 "cgroup monitoring only available in system-wide mode");
3876 }
3877
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003878 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3879 if (IS_ERR(evsel)) {
3880 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
Arnaldo Carvalho de Meloe0b6d2e2018-08-07 15:40:13 -03003881 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3882 goto out;
3883 }
3884
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003885 if (evsel) {
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003886 trace.syscalls.events.augmented = evsel;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003887 trace__set_bpf_map_filtered_pids(&trace);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003888 trace__set_bpf_map_syscalls(&trace);
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003889 }
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003890
Wang Nand7888572016-04-08 15:07:24 +00003891 err = bpf__setup_stdout(trace.evlist);
3892 if (err) {
3893 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3894 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3895 goto out;
3896 }
3897
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003898 err = -1;
3899
Arnaldo Carvalho de Meloff7a4f92019-02-19 16:17:33 -03003900 if (map_dump_str) {
3901 trace.dump.map = bpf__find_map_by_name(map_dump_str);
3902 if (trace.dump.map == NULL) {
3903 pr_err("ERROR: BPF map \"%s\" not found\n", map_dump_str);
3904 goto out;
3905 }
3906 }
3907
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003908 if (trace.trace_pgfaults) {
3909 trace.opts.sample_address = true;
3910 trace.opts.sample_time = true;
3911 }
3912
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003913 if (trace.opts.mmap_pages == UINT_MAX)
3914 mmap_pages_user_set = false;
3915
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003916 if (trace.max_stack == UINT_MAX) {
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03003917 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003918 max_stack_user_set = false;
3919 }
3920
3921#ifdef HAVE_DWARF_UNWIND_SUPPORT
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003922 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003923 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003924 }
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003925#endif
3926
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03003927 if (callchain_param.enabled) {
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003928 if (!mmap_pages_user_set && geteuid() == 0)
3929 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3930
Milian Wolff566a0882016-04-08 13:34:15 +02003931 symbol_conf.use_callchain = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003932 }
Milian Wolff566a0882016-04-08 13:34:15 +02003933
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003934 if (trace.evlist->nr_entries > 0) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003935 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003936 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3937 perror("failed to set syscalls:* tracepoint fields");
3938 goto out;
3939 }
3940 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003941
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003942 if (trace.sort_events) {
3943 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
3944 ordered_events__set_copy_on_queue(&trace.oe.data, true);
3945 }
Jiri Olsa028713a2018-12-05 17:05:09 +01003946
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003947 /*
3948 * If we are augmenting syscalls, then combine what we put in the
3949 * __augmented_syscalls__ BPF map with what is in the
3950 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3951 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3952 *
3953 * We'll switch to look at two BPF maps, one for sys_enter and the
3954 * other for sys_exit when we start augmenting the sys_exit paths with
3955 * buffers that are being copied from kernel to userspace, think 'read'
3956 * syscall.
3957 */
3958 if (trace.syscalls.events.augmented) {
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003959 evlist__for_each_entry(trace.evlist, evsel) {
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003960 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3961
3962 if (raw_syscalls_sys_exit) {
3963 trace.raw_augmented_syscalls = true;
3964 goto init_augmented_syscall_tp;
3965 }
3966
Arnaldo Carvalho de Melo1d59cb12019-01-24 15:39:15 +01003967 if (trace.syscalls.events.augmented->priv == NULL &&
3968 strstr(perf_evsel__name(evsel), "syscalls:sys_enter")) {
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03003969 struct perf_evsel *augmented = trace.syscalls.events.augmented;
3970 if (perf_evsel__init_augmented_syscall_tp(augmented, evsel) ||
3971 perf_evsel__init_augmented_syscall_tp_args(augmented))
3972 goto out;
3973 augmented->handler = trace__sys_enter;
3974 }
3975
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003976 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03003977 struct syscall_tp *sc;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003978init_augmented_syscall_tp:
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03003979 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel))
3980 goto out;
3981 sc = evsel->priv;
3982 /*
3983 * For now with BPF raw_augmented we hook into
3984 * raw_syscalls:sys_enter and there we get all
3985 * 6 syscall args plus the tracepoint common
3986 * fields and the syscall_nr (another long).
3987 * So we check if that is the case and if so
3988 * don't look after the sc->args_size but
3989 * always after the full raw_syscalls:sys_enter
3990 * payload, which is fixed.
3991 *
3992 * We'll revisit this later to pass
3993 * s->args_size to the BPF augmenter (now
3994 * tools/perf/examples/bpf/augmented_raw_syscalls.c,
3995 * so that it copies only what we need for each
3996 * syscall, like what happens when we use
3997 * syscalls:sys_enter_NAME, so that we reduce
3998 * the kernel/userspace traffic to just what is
3999 * needed for each syscall.
4000 */
4001 if (trace.raw_augmented_syscalls)
4002 trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset;
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03004003 perf_evsel__init_augmented_syscall_tp_ret(evsel);
4004 evsel->handler = trace__sys_exit;
4005 }
4006 }
4007 }
4008
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04004009 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
4010 return trace__record(&trace, argc-1, &argv[1]);
4011
4012 /* summary_only implies summary option, but don't overwrite summary if set */
4013 if (trace.summary_only)
4014 trace.summary = trace.summary_only;
4015
Arnaldo Carvalho de Melo726f3232015-02-06 10:16:45 +01004016 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
4017 trace.evlist->nr_entries == 0 /* Was --events used? */) {
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03004018 trace.trace_syscalls = true;
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03004019 }
4020
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004021 if (output_name != NULL) {
4022 err = trace__open_output(&trace, output_name);
4023 if (err < 0) {
4024 perror("failed to create output file");
4025 goto out;
4026 }
4027 }
4028
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004029 err = target__validate(&trace.opts.target);
Namhyung Kim32caf0d2012-10-05 14:02:13 +09004030 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004031 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004032 fprintf(trace.output, "%s", bf);
4033 goto out_close;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09004034 }
4035
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004036 err = target__parse_uid(&trace.opts.target);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004037 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004038 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004039 fprintf(trace.output, "%s", bf);
4040 goto out_close;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004041 }
4042
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03004043 if (!argc && target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +09004044 trace.opts.target.system_wide = true;
4045
David Ahern6810fc92013-08-28 22:29:52 -06004046 if (input_name)
4047 err = trace__replay(&trace);
4048 else
4049 err = trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03004050
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03004051out_close:
4052 if (output_name != NULL)
4053 fclose(trace.output);
4054out:
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03004055 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03004056}