blob: d4bca74f282c275185c45c1d374bc86b62dde77c [file] [log] [blame]
Arnaldo Carvalho de Meloa598bb52015-08-28 12:02:37 -03001/*
2 * builtin-trace.c
3 *
4 * Builtin 'trace' command:
5 *
6 * Display a continuously updated trace of any workload, CPU, specific PID,
7 * system wide, etc. Default format is loosely strace like, but any other
8 * event may be specified using --event.
9 *
10 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
11 *
12 * Initially based on the 'trace' prototype by Thomas Gleixner:
13 *
14 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
15 *
16 * Released under the GPL v2. (and only v2, not any later version)
17 */
18
Robert Richter4e319022013-06-11 17:29:18 +020019#include <traceevent/event-parse.h>
Jiri Olsa988bdb32015-09-02 09:56:35 +020020#include <api/fs/tracing_path.h>
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -030021#include <bpf/bpf.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 Melo9a3993d2017-04-18 11:33:48 -030032#include "util/path.h"
David Ahern6810fc92013-08-28 22:29:52 -060033#include "util/session.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030034#include "util/thread.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060035#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -030036#include "util/strlist.h"
David Ahernbdc89662013-08-28 22:29:53 -060037#include "util/intlist.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030038#include "util/thread_map.h"
David Ahernbf2575c2013-10-08 21:26:53 -060039#include "util/stat.h"
Arnaldo Carvalho de Melofd5cead2017-03-14 16:19:30 -030040#include "trace/beauty/beauty.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010041#include "trace-event.h"
David Ahern9aca7f12013-12-04 19:41:39 -070042#include "util/parse-events.h"
Wang Nanba504232016-02-26 09:31:54 +000043#include "util/bpf-loader.h"
Milian Wolff566a0882016-04-08 13:34:15 +020044#include "callchain.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030045#include "print_binary.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030046#include "string2.h"
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030047#include "syscalltbl.h"
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -030048#include "rb_resort.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030049
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030050#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030051#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030052#include <poll.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030053#include <signal.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030054#include <stdlib.h>
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -030055#include <string.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020056#include <linux/err.h>
Arnaldo Carvalho de Melo997bba82016-03-30 19:43:32 -030057#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030058#include <linux/kernel.h>
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -030059#include <linux/random.h>
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -030060#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030061#include <linux/time64.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030062#include <fcntl.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030063
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030064#include "sane_ctype.h"
65
Arnaldo Carvalho de Meloc188e7a2015-05-14 17:39:03 -030066#ifndef O_CLOEXEC
67# define O_CLOEXEC 02000000
68#endif
69
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -030070#ifndef F_LINUX_SPECIFIC_BASE
71# define F_LINUX_SPECIFIC_BASE 1024
72#endif
73
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030074struct trace {
75 struct perf_tool tool;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030076 struct syscalltbl *sctbl;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030077 struct {
78 int max;
79 struct syscall *table;
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -030080 struct bpf_map *map;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030081 struct {
82 struct perf_evsel *sys_enter,
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -030083 *sys_exit,
84 *augmented;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030085 } events;
86 } syscalls;
87 struct record_opts opts;
88 struct perf_evlist *evlist;
89 struct machine *host;
90 struct thread *current;
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030091 struct cgroup *cgroup;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030092 u64 base_time;
93 FILE *output;
94 unsigned long nr_events;
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -030095 unsigned long nr_events_printed;
96 unsigned long max_events;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030097 struct strlist *ev_qualifier;
98 struct {
99 size_t nr;
100 int *entries;
101 } ev_qualifier_ids;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300102 struct {
103 size_t nr;
104 pid_t *entries;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -0300105 struct bpf_map *map;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300106 } filter_pids;
107 double duration_filter;
108 double runtime_ms;
109 struct {
110 u64 vfs_getname,
111 proc_getname;
112 } stats;
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -0300113 unsigned int max_stack;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -0300114 unsigned int min_stack;
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300115 int raw_augmented_syscalls_args_size;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -0300116 bool raw_augmented_syscalls;
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300117 bool sort_events;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300118 bool not_ev_qualifier;
119 bool live;
120 bool full_time;
121 bool sched;
122 bool multiple_threads;
123 bool summary;
124 bool summary_only;
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -0300125 bool failure_only;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300126 bool show_comm;
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -0300127 bool print_sample;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300128 bool show_tool_stats;
129 bool trace_syscalls;
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -0300130 bool kernel_syscallchains;
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -0300131 s16 args_alignment;
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -0300132 bool show_tstamp;
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -0300133 bool show_duration;
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -0300134 bool show_zeros;
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -0300135 bool show_arg_names;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300136 bool show_string_prefix;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300137 bool force;
138 bool vfs_getname;
139 int trace_pgfaults;
Jiri Olsa028713a2018-12-05 17:05:09 +0100140 struct {
141 struct ordered_events data;
142 u64 last;
143 } oe;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300144};
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300145
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300146struct tp_field {
147 int offset;
148 union {
149 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
150 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
151 };
152};
153
154#define TP_UINT_FIELD(bits) \
155static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
156{ \
David Ahern55d43bc2015-02-19 15:00:22 -0500157 u##bits value; \
158 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
159 return value; \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300160}
161
162TP_UINT_FIELD(8);
163TP_UINT_FIELD(16);
164TP_UINT_FIELD(32);
165TP_UINT_FIELD(64);
166
167#define TP_UINT_FIELD__SWAPPED(bits) \
168static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
169{ \
David Ahern55d43bc2015-02-19 15:00:22 -0500170 u##bits value; \
171 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300172 return bswap_##bits(value);\
173}
174
175TP_UINT_FIELD__SWAPPED(16);
176TP_UINT_FIELD__SWAPPED(32);
177TP_UINT_FIELD__SWAPPED(64);
178
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300179static 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 -0300180{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300181 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300182
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300183 switch (size) {
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300184 case 1:
185 field->integer = tp_field__u8;
186 break;
187 case 2:
188 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
189 break;
190 case 4:
191 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
192 break;
193 case 8:
194 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
195 break;
196 default:
197 return -1;
198 }
199
200 return 0;
201}
202
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400203static 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 -0300204{
205 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
206}
207
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300208static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
209{
210 return sample->raw_data + field->offset;
211}
212
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300213static int __tp_field__init_ptr(struct tp_field *field, int offset)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300214{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300215 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300216 field->pointer = tp_field__ptr;
217 return 0;
218}
219
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400220static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300221{
222 return __tp_field__init_ptr(field, format_field->offset);
223}
224
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300225struct syscall_tp {
226 struct tp_field id;
227 union {
228 struct tp_field args, ret;
229 };
230};
231
232static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
233 struct tp_field *field,
234 const char *name)
235{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400236 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300237
238 if (format_field == NULL)
239 return -1;
240
241 return tp_field__init_uint(field, format_field, evsel->needs_swap);
242}
243
244#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
245 ({ struct syscall_tp *sc = evsel->priv;\
246 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
247
248static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
249 struct tp_field *field,
250 const char *name)
251{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400252 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300253
254 if (format_field == NULL)
255 return -1;
256
257 return tp_field__init_ptr(field, format_field);
258}
259
260#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
261 ({ struct syscall_tp *sc = evsel->priv;\
262 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
263
264static void perf_evsel__delete_priv(struct perf_evsel *evsel)
265{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300266 zfree(&evsel->priv);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300267 perf_evsel__delete(evsel);
268}
269
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300270static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
271{
272 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
273
274 if (evsel->priv != NULL) {
Arnaldo Carvalho de Melo42da4382018-12-05 13:08:11 -0300275 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
276 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300277 goto out_delete;
278 return 0;
279 }
280
281 return -ENOMEM;
282out_delete:
283 zfree(&evsel->priv);
284 return -ENOENT;
285}
286
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300287static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel, struct perf_evsel *tp)
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300288{
289 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
290
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -0300291 if (evsel->priv != NULL) {
292 struct tep_format_field *syscall_id = perf_evsel__field(tp, "id");
293 if (syscall_id == NULL)
294 syscall_id = perf_evsel__field(tp, "__syscall_nr");
295 if (syscall_id == NULL)
296 goto out_delete;
297 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 -0300298 goto out_delete;
299
300 return 0;
301 }
302
303 return -ENOMEM;
304out_delete:
305 zfree(&evsel->priv);
306 return -EINVAL;
307}
308
309static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
310{
311 struct syscall_tp *sc = evsel->priv;
312
313 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
314}
315
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -0300316static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
317{
318 struct syscall_tp *sc = evsel->priv;
319
320 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
321}
322
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300323static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
Namhyung Kim96695d42013-11-12 08:51:45 -0300324{
325 evsel->priv = malloc(sizeof(struct syscall_tp));
326 if (evsel->priv != NULL) {
327 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
328 goto out_delete;
329
330 evsel->handler = handler;
331 return 0;
332 }
333
334 return -ENOMEM;
335
336out_delete:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300337 zfree(&evsel->priv);
Namhyung Kim96695d42013-11-12 08:51:45 -0300338 return -ENOENT;
339}
340
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300341static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300342{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300343 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300344
David Ahern9aca7f12013-12-04 19:41:39 -0700345 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200346 if (IS_ERR(evsel))
David Ahern9aca7f12013-12-04 19:41:39 -0700347 evsel = perf_evsel__newtp("syscalls", direction);
348
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200349 if (IS_ERR(evsel))
350 return NULL;
351
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300352 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200353 goto out_delete;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300354
355 return evsel;
356
357out_delete:
358 perf_evsel__delete_priv(evsel);
359 return NULL;
360}
361
362#define perf_evsel__sc_tp_uint(evsel, name, sample) \
363 ({ struct syscall_tp *fields = evsel->priv; \
364 fields->name.integer(&fields->name, sample); })
365
366#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
367 ({ struct syscall_tp *fields = evsel->priv; \
368 fields->name.pointer(&fields->name, sample); })
369
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300370size_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 -0300371{
372 int idx = val - sa->offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300373
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300374 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
375 size_t printed = scnprintf(bf, size, intfmt, val);
376 if (show_prefix)
377 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
378 return printed;
379 }
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300380
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300381 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300382}
383
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300384static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
385 const char *intfmt,
386 struct syscall_arg *arg)
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300387{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300388 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300389}
390
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300391static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
392 struct syscall_arg *arg)
393{
394 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
395}
396
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300397#define SCA_STRARRAY syscall_arg__scnprintf_strarray
398
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300399size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
400{
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300401 size_t printed;
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300402 int i;
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300403
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300404 for (i = 0; i < sas->nr_entries; ++i) {
405 struct strarray *sa = sas->entries[i];
406 int idx = val - sa->offset;
407
408 if (idx >= 0 && idx < sa->nr_entries) {
409 if (sa->entries[idx] == NULL)
410 break;
411 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
412 }
413 }
414
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300415 printed = scnprintf(bf, size, intfmt, val);
416 if (show_prefix)
417 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
418 return printed;
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300419}
420
Arnaldo Carvalho de Melo274e86f2017-07-14 09:38:38 -0300421size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
422 struct syscall_arg *arg)
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300423{
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300424 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300425}
426
Arnaldo Carvalho de Melo48e1f912016-07-05 14:43:27 -0300427#ifndef AT_FDCWD
428#define AT_FDCWD -100
429#endif
430
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300431static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
432 struct syscall_arg *arg)
433{
434 int fd = arg->val;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300435 const char *prefix = "AT_FD";
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300436
437 if (fd == AT_FDCWD)
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300438 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300439
440 return syscall_arg__scnprintf_fd(bf, size, arg);
441}
442
443#define SCA_FDAT syscall_arg__scnprintf_fd_at
444
445static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
446 struct syscall_arg *arg);
447
448#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
449
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300450size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300451{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300452 return scnprintf(bf, size, "%#lx", arg->val);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300453}
454
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300455size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
456{
457 if (arg->val == 0)
458 return scnprintf(bf, size, "NULL");
459 return syscall_arg__scnprintf_hex(bf, size, arg);
460}
461
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300462size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300463{
464 return scnprintf(bf, size, "%d", arg->val);
465}
466
Arnaldo Carvalho de Melo5dde91e2017-07-14 10:34:16 -0300467size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
468{
469 return scnprintf(bf, size, "%ld", arg->val);
470}
471
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300472static const char *bpf_cmd[] = {
473 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
474 "MAP_GET_NEXT_KEY", "PROG_LOAD",
475};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300476static DEFINE_STRARRAY(bpf_cmd, "BPF_");
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300477
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300478static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300479static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
Arnaldo Carvalho de Meloeac032c2013-09-20 11:27:32 -0300480
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300481static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300482static DEFINE_STRARRAY(itimers, "ITIMER_");
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300483
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300484static const char *keyctl_options[] = {
485 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
486 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
487 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
488 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
489 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
490};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300491static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300492
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300493static const char *whences[] = { "SET", "CUR", "END",
494#ifdef SEEK_DATA
495"DATA",
496#endif
497#ifdef SEEK_HOLE
498"HOLE",
499#endif
500};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300501static DEFINE_STRARRAY(whences, "SEEK_");
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300502
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300503static const char *fcntl_cmds[] = {
504 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
Arnaldo Carvalho de Meloe000e5e2017-07-13 13:07:00 -0300505 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
506 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
507 "GETOWNER_UIDS",
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300508};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300509static DEFINE_STRARRAY(fcntl_cmds, "F_");
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300510
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300511static const char *fcntl_linux_specific_cmds[] = {
512 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
513 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
Arnaldo Carvalho de Melo64e45612017-07-13 17:34:46 -0300514 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300515};
516
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300517static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300518
519static struct strarray *fcntl_cmds_arrays[] = {
520 &strarray__fcntl_cmds,
521 &strarray__fcntl_linux_specific_cmds,
522};
523
524static DEFINE_STRARRAYS(fcntl_cmds_arrays);
525
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300526static const char *rlimit_resources[] = {
527 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
528 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
529 "RTTIME",
530};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300531static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300532
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300533static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300534static DEFINE_STRARRAY(sighow, "SIG_");
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300535
David Ahern4f8c1b72013-09-22 19:45:00 -0600536static const char *clockid[] = {
537 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
Arnaldo Carvalho de Melo28ebb872015-08-11 10:38:38 -0300538 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
539 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
David Ahern4f8c1b72013-09-22 19:45:00 -0600540};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300541static DEFINE_STRARRAY(clockid, "CLOCK_");
David Ahern4f8c1b72013-09-22 19:45:00 -0600542
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300543static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
544 struct syscall_arg *arg)
545{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300546 bool show_prefix = arg->show_string_prefix;
547 const char *suffix = "_OK";
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300548 size_t printed = 0;
549 int mode = arg->val;
550
551 if (mode == F_OK) /* 0 */
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300552 return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300553#define P_MODE(n) \
554 if (mode & n##_OK) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300555 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300556 mode &= ~n##_OK; \
557 }
558
559 P_MODE(R);
560 P_MODE(W);
561 P_MODE(X);
562#undef P_MODE
563
564 if (mode)
565 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
566
567 return printed;
568}
569
570#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
571
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300572static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
573 struct syscall_arg *arg);
574
575#define SCA_FILENAME syscall_arg__scnprintf_filename
576
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300577static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
578 struct syscall_arg *arg)
579{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300580 bool show_prefix = arg->show_string_prefix;
581 const char *prefix = "O_";
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300582 int printed = 0, flags = arg->val;
583
584#define P_FLAG(n) \
585 if (flags & O_##n) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300586 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300587 flags &= ~O_##n; \
588 }
589
590 P_FLAG(CLOEXEC);
591 P_FLAG(NONBLOCK);
592#undef P_FLAG
593
594 if (flags)
595 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
596
597 return printed;
598}
599
600#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
601
Arnaldo Carvalho de Meloa355a612016-04-13 11:55:18 -0300602#ifndef GRND_NONBLOCK
603#define GRND_NONBLOCK 0x0001
604#endif
605#ifndef GRND_RANDOM
606#define GRND_RANDOM 0x0002
607#endif
608
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300609static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
610 struct syscall_arg *arg)
611{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300612 bool show_prefix = arg->show_string_prefix;
613 const char *prefix = "GRND_";
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300614 int printed = 0, flags = arg->val;
615
616#define P_FLAG(n) \
617 if (flags & GRND_##n) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300618 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300619 flags &= ~GRND_##n; \
620 }
621
622 P_FLAG(RANDOM);
623 P_FLAG(NONBLOCK);
624#undef P_FLAG
625
626 if (flags)
627 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
628
629 return printed;
630}
631
632#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
633
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300634#define STRARRAY(name, array) \
635 { .scnprintf = SCA_STRARRAY, \
636 .parm = &strarray__##array, }
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -0300637
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +0100638#include "trace/beauty/arch_errno_names.c"
Arnaldo Carvalho de Meloea8dc3c2016-04-13 12:10:19 -0300639#include "trace/beauty/eventfd.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300640#include "trace/beauty/futex_op.c"
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300641#include "trace/beauty/futex_val3.c"
Arnaldo Carvalho de Melodf4cb162016-04-13 12:05:44 -0300642#include "trace/beauty/mmap.c"
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -0300643#include "trace/beauty/mode_t.c"
Arnaldo Carvalho de Meloa30e6252016-04-27 19:01:52 -0300644#include "trace/beauty/msg_flags.c"
Arnaldo Carvalho de Melo8f48df62016-05-06 10:02:32 -0300645#include "trace/beauty/open_flags.c"
Arnaldo Carvalho de Melo62de3442016-04-26 11:03:03 -0300646#include "trace/beauty/perf_event_open.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300647#include "trace/beauty/pid.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300648#include "trace/beauty/sched_policy.c"
Arnaldo Carvalho de Melof5cd95e2016-05-11 10:32:20 -0300649#include "trace/beauty/seccomp.c"
Arnaldo Carvalho de Melo12199d82016-05-06 09:58:02 -0300650#include "trace/beauty/signum.c"
Arnaldo Carvalho de Melobbf86c42016-04-14 13:53:10 -0300651#include "trace/beauty/socket_type.c"
Arnaldo Carvalho de Melo7206b902016-04-06 14:11:36 -0300652#include "trace/beauty/waitid_options.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300653
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300654struct syscall_arg_fmt {
655 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -0300656 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300657 void *parm;
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -0300658 const char *name;
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -0300659 bool show_zero;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300660};
661
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300662static struct syscall_fmt {
663 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -0300664 const char *alias;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300665 struct syscall_arg_fmt arg[6];
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -0300666 u8 nr_args;
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300667 bool errpid;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300668 bool timeout;
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -0300669 bool hexret;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300670} syscall_fmts[] = {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300671 { .name = "access",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300672 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
Arnaldo Carvalho de Melofb7068e2018-12-18 11:05:56 -0300673 { .name = "arch_prctl",
674 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
675 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
Arnaldo Carvalho de Melo02ef2882018-08-28 17:03:53 -0300676 { .name = "bind",
677 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300678 { .name = "bpf",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300679 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300680 { .name = "brk", .hexret = true,
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300681 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300682 { .name = "clock_gettime",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300683 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
Arnaldo Carvalho de Melo33396a32017-07-19 16:15:17 -0300684 { .name = "clone", .errpid = true, .nr_args = 5,
685 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
686 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
687 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
688 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
689 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300690 { .name = "close",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300691 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
Arnaldo Carvalho de Melod5a7e662018-08-28 16:24:44 -0300692 { .name = "connect",
693 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300694 { .name = "epoll_ctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300695 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300696 { .name = "eventfd2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300697 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300698 { .name = "fchmodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300699 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300700 { .name = "fchownat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300701 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300702 { .name = "fcntl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300703 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
Arnaldo Carvalho de Melo39cc3552017-07-17 16:02:52 -0300704 .parm = &strarrays__fcntl_cmds_arrays,
705 .show_zero = true, },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300706 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300707 { .name = "flock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300708 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300709 { .name = "fstat", .alias = "newfstat", },
710 { .name = "fstatat", .alias = "newfstatat", },
711 { .name = "futex",
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300712 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
713 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300714 { .name = "futimesat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300715 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300716 { .name = "getitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300717 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300718 { .name = "getpid", .errpid = true, },
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300719 { .name = "getpgid", .errpid = true, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300720 { .name = "getppid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300721 { .name = "getrandom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300722 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300723 { .name = "getrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300724 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo2d1073d2018-01-09 12:03:47 -0300725 { .name = "gettid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300726 { .name = "ioctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300727 .arg = {
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300728#if defined(__i386__) || defined(__x86_64__)
729/*
730 * FIXME: Make this available to all arches.
731 */
Arnaldo Carvalho de Melo1cc47f22017-07-31 13:20:14 -0300732 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300733 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300734#else
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300735 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300736#endif
Arnaldo Carvalho de Melo1de30382017-10-31 11:32:23 -0300737 { .name = "kcmp", .nr_args = 5,
738 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
739 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
740 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
741 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
742 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300743 { .name = "keyctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300744 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300745 { .name = "kill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300746 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300747 { .name = "linkat",
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 = "lseek",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300750 .arg = { [2] = STRARRAY(whence, whences), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300751 { .name = "lstat", .alias = "newlstat", },
752 { .name = "madvise",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300753 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
754 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300755 { .name = "mkdirat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300756 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300757 { .name = "mknodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300758 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300759 { .name = "mmap", .hexret = true,
Jiri Olsa54265662017-05-31 13:35:57 +0200760/* The standard mmap maps to old_mmap on s390x */
761#if defined(__s390x__)
762 .alias = "old_mmap",
763#endif
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300764 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
Arnaldo Carvalho de Meloa6631342018-12-18 11:25:08 -0300765 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ },
766 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300767 { .name = "mount",
Arnaldo Carvalho de Melo23c07a22018-10-26 13:51:45 -0300768 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
769 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300770 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300771 { .name = "mprotect",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300772 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
773 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300774 { .name = "mq_unlink",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300775 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300776 { .name = "mremap", .hexret = true,
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300777 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300778 { .name = "name_to_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300779 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300780 { .name = "newfstatat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300781 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300782 { .name = "open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300783 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300784 { .name = "open_by_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300785 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
786 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300787 { .name = "openat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300788 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
789 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300790 { .name = "perf_event_open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300791 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
792 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
793 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300794 { .name = "pipe2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300795 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo83bc9c372017-08-28 11:47:11 -0300796 { .name = "pkey_alloc",
797 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
798 { .name = "pkey_free",
799 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
800 { .name = "pkey_mprotect",
801 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
802 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
803 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300804 { .name = "poll", .timeout = true, },
805 { .name = "ppoll", .timeout = true, },
Arnaldo Carvalho de Melofb7068e2018-12-18 11:05:56 -0300806 { .name = "prctl",
Arnaldo Carvalho de Melod688d032017-10-26 15:19:35 -0300807 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
808 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
809 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300810 { .name = "pread", .alias = "pread64", },
811 { .name = "preadv", .alias = "pread", },
812 { .name = "prlimit64",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300813 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300814 { .name = "pwrite", .alias = "pwrite64", },
815 { .name = "readlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300816 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300817 { .name = "recvfrom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300818 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300819 { .name = "recvmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300820 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300821 { .name = "recvmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300822 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300823 { .name = "renameat",
Arnaldo Carvalho de Melo436651c2018-12-11 11:19:23 -0300824 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
825 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
Arnaldo Carvalho de Melo84a83542018-12-11 09:53:21 -0300826 { .name = "renameat2",
827 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
Arnaldo Carvalho de Meloca7ff2c2018-12-11 10:39:30 -0300828 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
829 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300830 { .name = "rt_sigaction",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300831 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300832 { .name = "rt_sigprocmask",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300833 .arg = { [0] = STRARRAY(how, sighow), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300834 { .name = "rt_sigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300835 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300836 { .name = "rt_tgsigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300837 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300838 { .name = "sched_setscheduler",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300839 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300840 { .name = "seccomp",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300841 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
842 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300843 { .name = "select", .timeout = true, },
844 { .name = "sendmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300845 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300846 { .name = "sendmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300847 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300848 { .name = "sendto",
Arnaldo Carvalho de Melo6ebb6862018-08-29 08:47:08 -0300849 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
850 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300851 { .name = "set_tid_address", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300852 { .name = "setitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300853 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300854 { .name = "setrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300855 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300856 { .name = "socket",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300857 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300858 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
859 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300860 { .name = "socketpair",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300861 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300862 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
863 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300864 { .name = "stat", .alias = "newstat", },
865 { .name = "statx",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300866 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
867 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
868 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300869 { .name = "swapoff",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300870 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300871 { .name = "swapon",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300872 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300873 { .name = "symlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300874 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300875 { .name = "tgkill",
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 = "tkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300878 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo476c92ca2018-10-26 13:23:25 -0300879 { .name = "umount2", .alias = "umount",
880 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300881 { .name = "uname", .alias = "newuname", },
882 { .name = "unlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300883 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300884 { .name = "utimensat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300885 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300886 { .name = "wait4", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300887 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300888 { .name = "waitid", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300889 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300890};
891
892static int syscall_fmt__cmp(const void *name, const void *fmtp)
893{
894 const struct syscall_fmt *fmt = fmtp;
895 return strcmp(name, fmt->name);
896}
897
898static struct syscall_fmt *syscall_fmt__find(const char *name)
899{
900 const int nmemb = ARRAY_SIZE(syscall_fmts);
901 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
902}
903
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -0300904static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
905{
906 int i, nmemb = ARRAY_SIZE(syscall_fmts);
907
908 for (i = 0; i < nmemb; ++i) {
909 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
910 return &syscall_fmts[i];
911 }
912
913 return NULL;
914}
915
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300916/*
917 * is_exit: is this "exit" or "exit_group"?
918 * 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 -0300919 * 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 -0300920 */
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300921struct syscall {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500922 struct tep_event *tp_format;
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -0300923 int nr_args;
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -0300924 int args_size;
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300925 bool is_exit;
926 bool is_open;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400927 struct tep_format_field *args;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300928 const char *name;
929 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300930 struct syscall_arg_fmt *arg_fmt;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300931};
932
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -0300933struct bpf_map_syscall_entry {
934 bool enabled;
935};
936
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300937/*
938 * We need to have this 'calculated' boolean because in some cases we really
939 * don't know what is the duration of a syscall, for instance, when we start
940 * a session and some threads are waiting for a syscall to finish, say 'poll',
941 * in which case all we can do is to print "( ? ) for duration and for the
942 * start timestamp.
943 */
944static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200945{
946 double duration = (double)t / NSEC_PER_MSEC;
947 size_t printed = fprintf(fp, "(");
948
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300949 if (!calculated)
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -0300950 printed += fprintf(fp, " ");
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300951 else if (duration >= 1.0)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200952 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
953 else if (duration >= 0.01)
954 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
955 else
956 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300957 return printed + fprintf(fp, "): ");
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200958}
959
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -0300960struct file {
961 char *pathname;
962};
963
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300964/**
965 * filename.ptr: The filename char pointer that will be vfs_getname'd
966 * filename.entry_str_pos: Where to insert the string translated from
967 * filename.ptr by the vfs_getname tracepoint/kprobe.
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300968 * ret_scnprintf: syscall args may set this to a different syscall return
969 * formatter, for instance, fcntl may return fds, file flags, etc.
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300970 */
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300971struct thread_trace {
972 u64 entry_time;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300973 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300974 unsigned long nr_events;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +0400975 unsigned long pfmaj, pfmin;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300976 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300977 double runtime_ms;
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -0300978 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300979 struct {
980 unsigned long ptr;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -0300981 short int entry_str_pos;
982 bool pending_open;
983 unsigned int namelen;
984 char *name;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300985 } filename;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300986 struct {
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -0300987 int max;
988 struct file *table;
989 } files;
David Ahernbf2575c2013-10-08 21:26:53 -0600990
991 struct intlist *syscall_stats;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300992};
993
994static struct thread_trace *thread_trace__new(void)
995{
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300996 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
997
998 if (ttrace)
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -0300999 ttrace->files.max = -1;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001000
David Ahernbf2575c2013-10-08 21:26:53 -06001001 ttrace->syscall_stats = intlist__new(NULL);
1002
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001003 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001004}
1005
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001006static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001007{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001008 struct thread_trace *ttrace;
1009
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001010 if (thread == NULL)
1011 goto fail;
1012
Namhyung Kim89dceb22014-10-06 09:46:03 +09001013 if (thread__priv(thread) == NULL)
1014 thread__set_priv(thread, thread_trace__new());
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001015
Namhyung Kim89dceb22014-10-06 09:46:03 +09001016 if (thread__priv(thread) == NULL)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001017 goto fail;
1018
Namhyung Kim89dceb22014-10-06 09:46:03 +09001019 ttrace = thread__priv(thread);
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001020 ++ttrace->nr_events;
1021
1022 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001023fail:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001024 color_fprintf(fp, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001025 "WARNING: not enough memory, dropping samples!\n");
1026 return NULL;
1027}
1028
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001029
1030void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001031 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001032{
1033 struct thread_trace *ttrace = thread__priv(arg->thread);
1034
1035 ttrace->ret_scnprintf = ret_scnprintf;
1036}
1037
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04001038#define TRACE_PFMAJ (1 << 0)
1039#define TRACE_PFMIN (1 << 1)
1040
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001041static const size_t trace__entry_str_size = 2048;
1042
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001043static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001044{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001045 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001046
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001047 if (fd > ttrace->files.max) {
1048 struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001049
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001050 if (nfiles == NULL)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001051 return -1;
1052
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001053 if (ttrace->files.max != -1) {
1054 memset(nfiles + ttrace->files.max + 1, 0,
1055 (fd - ttrace->files.max) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001056 } else {
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001057 memset(nfiles, 0, (fd + 1) * sizeof(struct file));
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001058 }
1059
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001060 ttrace->files.table = nfiles;
1061 ttrace->files.max = fd;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001062 }
1063
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001064 ttrace->files.table[fd].pathname = strdup(pathname);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001065
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001066 return ttrace->files.table[fd].pathname != NULL ? 0 : -1;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001067}
1068
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001069static int thread__read_fd_path(struct thread *thread, int fd)
1070{
1071 char linkname[PATH_MAX], pathname[PATH_MAX];
1072 struct stat st;
1073 int ret;
1074
1075 if (thread->pid_ == thread->tid) {
1076 scnprintf(linkname, sizeof(linkname),
1077 "/proc/%d/fd/%d", thread->pid_, fd);
1078 } else {
1079 scnprintf(linkname, sizeof(linkname),
1080 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1081 }
1082
1083 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1084 return -1;
1085
1086 ret = readlink(linkname, pathname, sizeof(pathname));
1087
1088 if (ret < 0 || ret > st.st_size)
1089 return -1;
1090
1091 pathname[ret] = '\0';
1092 return trace__set_fd_pathname(thread, fd, pathname);
1093}
1094
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001095static const char *thread__fd_path(struct thread *thread, int fd,
1096 struct trace *trace)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001097{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001098 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001099
1100 if (ttrace == NULL)
1101 return NULL;
1102
1103 if (fd < 0)
1104 return NULL;
1105
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001106 if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001107 if (!trace->live)
1108 return NULL;
1109 ++trace->stats.proc_getname;
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001110 if (thread__read_fd_path(thread, fd))
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001111 return NULL;
1112 }
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001113
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001114 return ttrace->files.table[fd].pathname;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001115}
1116
Arnaldo Carvalho de Melofc65eb82017-07-14 15:21:40 -03001117size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001118{
1119 int fd = arg->val;
1120 size_t printed = scnprintf(bf, size, "%d", fd);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001121 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001122
1123 if (path)
1124 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1125
1126 return printed;
1127}
1128
Arnaldo Carvalho de Melo0a2f75402017-10-31 11:30:09 -03001129size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1130{
1131 size_t printed = scnprintf(bf, size, "%d", fd);
1132 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1133
1134 if (thread) {
1135 const char *path = thread__fd_path(thread, fd, trace);
1136
1137 if (path)
1138 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1139
1140 thread__put(thread);
1141 }
1142
1143 return printed;
1144}
1145
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001146static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1147 struct syscall_arg *arg)
1148{
1149 int fd = arg->val;
1150 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
Namhyung Kim89dceb22014-10-06 09:46:03 +09001151 struct thread_trace *ttrace = thread__priv(arg->thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001152
Arnaldo Carvalho de Melof4a74fc2018-12-27 10:56:12 -03001153 if (ttrace && fd >= 0 && fd <= ttrace->files.max)
1154 zfree(&ttrace->files.table[fd].pathname);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001155
1156 return printed;
1157}
1158
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001159static void thread__set_filename_pos(struct thread *thread, const char *bf,
1160 unsigned long ptr)
1161{
1162 struct thread_trace *ttrace = thread__priv(thread);
1163
1164 ttrace->filename.ptr = ptr;
1165 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1166}
1167
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001168static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1169{
1170 struct augmented_arg *augmented_arg = arg->augmented.args;
1171
Arnaldo Carvalho de Melo721f5322018-12-14 14:02:03 -03001172 return scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001173}
1174
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001175static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1176 struct syscall_arg *arg)
1177{
1178 unsigned long ptr = arg->val;
1179
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001180 if (arg->augmented.args)
1181 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1182
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001183 if (!arg->trace->vfs_getname)
1184 return scnprintf(bf, size, "%#x", ptr);
1185
1186 thread__set_filename_pos(arg->thread, bf, ptr);
1187 return 0;
1188}
1189
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001190static bool trace__filter_duration(struct trace *trace, double t)
1191{
1192 return t < (trace->duration_filter * NSEC_PER_MSEC);
1193}
1194
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001195static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001196{
1197 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1198
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001199 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001200}
1201
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001202/*
1203 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1204 * using ttrace->entry_time for a thread that receives a sys_exit without
1205 * first having received a sys_enter ("poll" issued before tracing session
1206 * starts, lost sys_enter exit due to ring buffer overflow).
1207 */
1208static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1209{
1210 if (tstamp > 0)
1211 return __trace__fprintf_tstamp(trace, tstamp, fp);
1212
1213 return fprintf(fp, " ? ");
1214}
1215
Namhyung Kimf15eb532012-10-05 14:02:16 +09001216static bool done = false;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001217static bool interrupted = false;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001218
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001219static void sig_handler(int sig)
Namhyung Kimf15eb532012-10-05 14:02:16 +09001220{
1221 done = true;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001222 interrupted = sig == SIGINT;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001223}
1224
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001225static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001226{
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001227 size_t printed = 0;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001228
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001229 if (trace->multiple_threads) {
1230 if (trace->show_comm)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02001231 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
Adrian Hunter38051232013-07-04 16:20:31 +03001232 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001233 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001234
1235 return printed;
1236}
1237
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001238static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1239 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1240{
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03001241 size_t printed = 0;
1242
1243 if (trace->show_tstamp)
1244 printed = trace__fprintf_tstamp(trace, tstamp, fp);
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03001245 if (trace->show_duration)
1246 printed += fprintf_duration(duration, duration_calculated, fp);
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001247 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1248}
1249
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001250static int trace__process_event(struct trace *trace, struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001251 union perf_event *event, struct perf_sample *sample)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001252{
1253 int ret = 0;
1254
1255 switch (event->header.type) {
1256 case PERF_RECORD_LOST:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001257 color_fprintf(trace->output, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001258 "LOST %" PRIu64 " events!\n", event->lost.lost);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001259 ret = machine__process_lost_event(machine, event, sample);
Arnaldo Carvalho de Melo3ed5ca22016-03-30 16:51:17 -03001260 break;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001261 default:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001262 ret = machine__process_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001263 break;
1264 }
1265
1266 return ret;
1267}
1268
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001269static int trace__tool_process(struct perf_tool *tool,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001270 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001271 struct perf_sample *sample,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001272 struct machine *machine)
1273{
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001274 struct trace *trace = container_of(tool, struct trace, tool);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001275 return trace__process_event(trace, machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001276}
1277
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -03001278static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1279{
1280 struct machine *machine = vmachine;
1281
1282 if (machine->kptr_restrict_warned)
1283 return NULL;
1284
1285 if (symbol_conf.kptr_restrict) {
1286 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1287 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1288 "Kernel samples will not be resolved.\n");
1289 machine->kptr_restrict_warned = true;
1290 return NULL;
1291 }
1292
1293 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1294}
1295
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001296static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1297{
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001298 int err = symbol__init(NULL);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001299
1300 if (err)
1301 return err;
1302
David Ahern8fb598e2013-09-28 13:13:00 -06001303 trace->host = machine__new_host();
1304 if (trace->host == NULL)
1305 return -ENOMEM;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001306
Andrei Vagincbd5c172017-11-07 16:22:46 -08001307 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1308 if (err < 0)
1309 goto out;
Arnaldo Carvalho de Melo706c3da2015-07-22 16:16:16 -03001310
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001311 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001312 evlist->threads, trace__tool_process, false,
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001313 1);
Andrei Vagincbd5c172017-11-07 16:22:46 -08001314out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001315 if (err)
1316 symbol__exit();
1317
1318 return err;
1319}
1320
Andrei Vagin33974a42017-11-07 16:22:45 -08001321static void trace__symbols__exit(struct trace *trace)
1322{
1323 machine__exit(trace->host);
1324 trace->host = NULL;
1325
1326 symbol__exit();
1327}
1328
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001329static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1330{
1331 int idx;
1332
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001333 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1334 nr_args = sc->fmt->nr_args;
1335
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001336 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1337 if (sc->arg_fmt == NULL)
1338 return -1;
1339
1340 for (idx = 0; idx < nr_args; ++idx) {
1341 if (sc->fmt)
1342 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1343 }
1344
1345 sc->nr_args = nr_args;
1346 return 0;
1347}
1348
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001349static int syscall__set_arg_fmts(struct syscall *sc)
1350{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001351 struct tep_format_field *field, *last_field = NULL;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001352 int idx = 0, len;
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001353
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001354 for (field = sc->args; field; field = field->next, ++idx) {
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001355 last_field = field;
1356
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001357 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1358 continue;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001359
1360 if (strcmp(field->type, "const char *") == 0 &&
Arnaldo Carvalho de Melo12f3ca42016-05-23 16:37:55 -03001361 (strcmp(field->name, "filename") == 0 ||
1362 strcmp(field->name, "path") == 0 ||
1363 strcmp(field->name, "pathname") == 0))
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001364 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -03001365 else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
1366 sc->arg_fmt[idx].scnprintf = SCA_PTR;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -03001367 else if (strcmp(field->type, "pid_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001368 sc->arg_fmt[idx].scnprintf = SCA_PID;
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -03001369 else if (strcmp(field->type, "umode_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001370 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001371 else if ((strcmp(field->type, "int") == 0 ||
1372 strcmp(field->type, "unsigned int") == 0 ||
1373 strcmp(field->type, "long") == 0) &&
1374 (len = strlen(field->name)) >= 2 &&
1375 strcmp(field->name + len - 2, "fd") == 0) {
1376 /*
1377 * /sys/kernel/tracing/events/syscalls/sys_enter*
1378 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1379 * 65 int
1380 * 23 unsigned int
1381 * 7 unsigned long
1382 */
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001383 sc->arg_fmt[idx].scnprintf = SCA_FD;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001384 }
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001385 }
1386
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001387 if (last_field)
1388 sc->args_size = last_field->offset + last_field->size;
1389
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001390 return 0;
1391}
1392
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001393static int trace__read_syscall_info(struct trace *trace, int id)
1394{
1395 char tp_name[128];
1396 struct syscall *sc;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03001397 const char *name = syscalltbl__name(trace->sctbl, id);
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001398
1399 if (name == NULL)
1400 return -1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001401
1402 if (id > trace->syscalls.max) {
1403 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1404
1405 if (nsyscalls == NULL)
1406 return -1;
1407
1408 if (trace->syscalls.max != -1) {
1409 memset(nsyscalls + trace->syscalls.max + 1, 0,
1410 (id - trace->syscalls.max) * sizeof(*sc));
1411 } else {
1412 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1413 }
1414
1415 trace->syscalls.table = nsyscalls;
1416 trace->syscalls.max = id;
1417 }
1418
1419 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001420 sc->name = name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001421
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001422 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001423
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001424 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
Jiri Olsa97978b32013-12-03 14:09:24 +01001425 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001426
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001427 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001428 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
Jiri Olsa97978b32013-12-03 14:09:24 +01001429 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001430 }
1431
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001432 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1433 return -1;
1434
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001435 if (IS_ERR(sc->tp_format))
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001436 return -1;
1437
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001438 sc->args = sc->tp_format->format.fields;
Taeung Songc42de702016-02-26 22:14:25 +09001439 /*
1440 * We need to check and discard the first variable '__syscall_nr'
1441 * or 'nr' that mean the syscall number. It is needless here.
1442 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1443 */
1444 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001445 sc->args = sc->args->next;
1446 --sc->nr_args;
1447 }
1448
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001449 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001450 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001451
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001452 return syscall__set_arg_fmts(sc);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001453}
1454
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001455static int trace__validate_ev_qualifier(struct trace *trace)
1456{
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001457 int err = 0, i;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001458 size_t nr_allocated;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001459 struct str_node *pos;
1460
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001461 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1462 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1463 sizeof(trace->ev_qualifier_ids.entries[0]));
1464
1465 if (trace->ev_qualifier_ids.entries == NULL) {
1466 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1467 trace->output);
1468 err = -EINVAL;
1469 goto out;
1470 }
1471
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001472 nr_allocated = trace->ev_qualifier_ids.nr;
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001473 i = 0;
1474
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001475 strlist__for_each_entry(pos, trace->ev_qualifier) {
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001476 const char *sc = pos->s;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001477 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001478
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001479 if (id < 0) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001480 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1481 if (id >= 0)
1482 goto matches;
1483
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001484 if (err == 0) {
1485 fputs("Error:\tInvalid syscall ", trace->output);
1486 err = -EINVAL;
1487 } else {
1488 fputs(", ", trace->output);
1489 }
1490
1491 fputs(sc, trace->output);
1492 }
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001493matches:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001494 trace->ev_qualifier_ids.entries[i++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001495 if (match_next == -1)
1496 continue;
1497
1498 while (1) {
1499 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1500 if (id < 0)
1501 break;
1502 if (nr_allocated == trace->ev_qualifier_ids.nr) {
1503 void *entries;
1504
1505 nr_allocated += 8;
1506 entries = realloc(trace->ev_qualifier_ids.entries,
1507 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1508 if (entries == NULL) {
1509 err = -ENOMEM;
1510 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1511 goto out_free;
1512 }
1513 trace->ev_qualifier_ids.entries = entries;
1514 }
1515 trace->ev_qualifier_ids.nr++;
1516 trace->ev_qualifier_ids.entries[i++] = id;
1517 }
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001518 }
1519
1520 if (err < 0) {
1521 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1522 "\nHint:\tand: 'man syscalls'\n", trace->output);
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001523out_free:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001524 zfree(&trace->ev_qualifier_ids.entries);
1525 trace->ev_qualifier_ids.nr = 0;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001526 }
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001527out:
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001528 return err;
1529}
1530
David Ahern55d43bc2015-02-19 15:00:22 -05001531/*
1532 * args is to be interpreted as a series of longs but we need to handle
1533 * 8-byte unaligned accesses. args points to raw_data within the event
1534 * and raw_data is guaranteed to be 8-byte unaligned because it is
1535 * preceded by raw_size which is a u32. So we need to copy args to a temp
1536 * variable to read it. Most notably this avoids extended load instructions
1537 * on unaligned addresses
1538 */
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001539unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001540{
1541 unsigned long val;
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001542 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001543
1544 memcpy(&val, p, sizeof(val));
1545 return val;
1546}
1547
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001548static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1549 struct syscall_arg *arg)
1550{
1551 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1552 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1553
1554 return scnprintf(bf, size, "arg%d: ", arg->idx);
1555}
1556
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001557/*
1558 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1559 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1560 * in tools/perf/trace/beauty/mount_flags.c
1561 */
1562static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1563{
1564 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1565 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1566
1567 return val;
1568}
1569
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001570static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1571 struct syscall_arg *arg, unsigned long val)
1572{
1573 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1574 arg->val = val;
1575 if (sc->arg_fmt[arg->idx].parm)
1576 arg->parm = sc->arg_fmt[arg->idx].parm;
1577 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1578 }
1579 return scnprintf(bf, size, "%ld", val);
1580}
1581
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001582static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001583 unsigned char *args, void *augmented_args, int augmented_args_size,
1584 struct trace *trace, struct thread *thread)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001585{
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001586 size_t printed = 0;
David Ahern55d43bc2015-02-19 15:00:22 -05001587 unsigned long val;
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001588 u8 bit = 1;
1589 struct syscall_arg arg = {
1590 .args = args,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001591 .augmented = {
1592 .size = augmented_args_size,
1593 .args = augmented_args,
1594 },
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001595 .idx = 0,
1596 .mask = 0,
1597 .trace = trace,
1598 .thread = thread,
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -03001599 .show_string_prefix = trace->show_string_prefix,
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001600 };
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001601 struct thread_trace *ttrace = thread__priv(thread);
1602
1603 /*
1604 * Things like fcntl will set this in its 'cmd' formatter to pick the
1605 * right formatter for the return value (an fd? file flags?), which is
1606 * not needed for syscalls that always return a given type, say an fd.
1607 */
1608 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001609
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001610 if (sc->args != NULL) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001611 struct tep_format_field *field;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001612
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001613 for (field = sc->args; field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001614 field = field->next, ++arg.idx, bit <<= 1) {
1615 if (arg.mask & bit)
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001616 continue;
David Ahern55d43bc2015-02-19 15:00:22 -05001617
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001618 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001619 /*
1620 * Some syscall args need some mask, most don't and
1621 * return val untouched.
1622 */
1623 val = syscall__mask_val(sc, &arg, val);
David Ahern55d43bc2015-02-19 15:00:22 -05001624
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001625 /*
1626 * Suppress this argument if its value is zero and
1627 * and we don't have a string associated in an
1628 * strarray for it.
1629 */
David Ahern55d43bc2015-02-19 15:00:22 -05001630 if (val == 0 &&
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03001631 !trace->show_zeros &&
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001632 !(sc->arg_fmt &&
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -03001633 (sc->arg_fmt[arg.idx].show_zero ||
1634 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001635 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1636 sc->arg_fmt[arg.idx].parm))
Arnaldo Carvalho de Melo22ae5cf12013-09-12 11:27:34 -03001637 continue;
1638
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03001639 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
1640
1641 if (trace->show_arg_names)
1642 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
1643
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001644 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001645 }
Arnaldo Carvalho de Melo4c4d6e52016-05-05 23:38:05 -03001646 } else if (IS_ERR(sc->tp_format)) {
1647 /*
1648 * If we managed to read the tracepoint /format file, then we
1649 * may end up not having any args, like with gettid(), so only
1650 * print the raw args when we didn't manage to read it.
1651 */
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001652 while (arg.idx < sc->nr_args) {
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001653 if (arg.mask & bit)
1654 goto next_arg;
1655 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001656 if (printed)
1657 printed += scnprintf(bf + printed, size - printed, ", ");
1658 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001659 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1660next_arg:
1661 ++arg.idx;
1662 bit <<= 1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001663 }
1664 }
1665
1666 return printed;
1667}
1668
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001669typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001670 union perf_event *event,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001671 struct perf_sample *sample);
1672
1673static struct syscall *trace__syscall_info(struct trace *trace,
David Ahernbf2575c2013-10-08 21:26:53 -06001674 struct perf_evsel *evsel, int id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001675{
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001676
1677 if (id < 0) {
Arnaldo Carvalho de Meloadaa18b2013-08-22 17:55:25 -03001678
1679 /*
1680 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1681 * before that, leaving at a higher verbosity level till that is
1682 * explained. Reproduced with plain ftrace with:
1683 *
1684 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1685 * grep "NR -1 " /t/trace_pipe
1686 *
1687 * After generating some load on the machine.
1688 */
1689 if (verbose > 1) {
1690 static u64 n;
1691 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1692 id, perf_evsel__name(evsel), ++n);
1693 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001694 return NULL;
1695 }
1696
1697 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1698 trace__read_syscall_info(trace, id))
1699 goto out_cant_read;
1700
1701 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1702 goto out_cant_read;
1703
1704 return &trace->syscalls.table[id];
1705
1706out_cant_read:
Namhyung Kimbb963e12017-02-17 17:17:38 +09001707 if (verbose > 0) {
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03001708 fprintf(trace->output, "Problems reading syscall %d", id);
1709 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1710 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1711 fputs(" information\n", trace->output);
1712 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001713 return NULL;
1714}
1715
David Ahernbf2575c2013-10-08 21:26:53 -06001716static void thread__update_stats(struct thread_trace *ttrace,
1717 int id, struct perf_sample *sample)
1718{
1719 struct int_node *inode;
1720 struct stats *stats;
1721 u64 duration = 0;
1722
1723 inode = intlist__findnew(ttrace->syscall_stats, id);
1724 if (inode == NULL)
1725 return;
1726
1727 stats = inode->priv;
1728 if (stats == NULL) {
1729 stats = malloc(sizeof(struct stats));
1730 if (stats == NULL)
1731 return;
1732 init_stats(stats);
1733 inode->priv = stats;
1734 }
1735
1736 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1737 duration = sample->time - ttrace->entry_time;
1738
1739 update_stats(stats, duration);
1740}
1741
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001742static int trace__printf_interrupted_entry(struct trace *trace)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001743{
1744 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001745 size_t printed;
1746
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001747 if (trace->failure_only || trace->current == NULL)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001748 return 0;
1749
1750 ttrace = thread__priv(trace->current);
1751
1752 if (!ttrace->entry_pending)
1753 return 0;
1754
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001755 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001756 printed += fprintf(trace->output, ")%-*s ...\n", trace->args_alignment, ttrace->entry_str);
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001757 ttrace->entry_pending = false;
1758
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03001759 ++trace->nr_events_printed;
1760
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001761 return printed;
1762}
1763
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001764static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1765 struct perf_sample *sample, struct thread *thread)
1766{
1767 int printed = 0;
1768
1769 if (trace->print_sample) {
1770 double ts = (double)sample->time / NSEC_PER_MSEC;
1771
1772 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1773 perf_evsel__name(evsel), ts,
1774 thread__comm_str(thread),
1775 sample->pid, sample->tid, sample->cpu);
1776 }
1777
1778 return printed;
1779}
1780
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001781static 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 -03001782{
1783 void *augmented_args = NULL;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001784 /*
1785 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001786 * and there we get all 6 syscall args plus the tracepoint common fields
1787 * that gets calculated at the start and the syscall_nr (another long).
1788 * So we check if that is the case and if so don't look after the
1789 * sc->args_size but always after the full raw_syscalls:sys_enter payload,
1790 * which is fixed.
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001791 *
1792 * We'll revisit this later to pass s->args_size to the BPF augmenter
1793 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1794 * copies only what we need for each syscall, like what happens when we
1795 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1796 * traffic to just what is needed for each syscall.
1797 */
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001798 int args_size = raw_augmented_args_size ?: sc->args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001799
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001800 *augmented_args_size = sample->raw_size - args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001801 if (*augmented_args_size > 0)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001802 augmented_args = sample->raw_data + args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001803
1804 return augmented_args;
1805}
1806
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001807static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001808 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001809 struct perf_sample *sample)
1810{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001811 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001812 void *args;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001813 int printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001814 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001815 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001816 int augmented_args_size = 0;
1817 void *augmented_args = NULL;
David Ahernbf2575c2013-10-08 21:26:53 -06001818 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001819 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001820
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001821 if (sc == NULL)
1822 return -1;
1823
David Ahern8fb598e2013-09-28 13:13:00 -06001824 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001825 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001826 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001827 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001828
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001829 trace__fprintf_sample(trace, evsel, sample, thread);
1830
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001831 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001832
1833 if (ttrace->entry_str == NULL) {
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001834 ttrace->entry_str = malloc(trace__entry_str_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001835 if (!ttrace->entry_str)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001836 goto out_put;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001837 }
1838
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001839 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001840 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001841 /*
1842 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1843 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1844 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1845 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1846 * so when handling, say the openat syscall, we end up getting 6 args for the
1847 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1848 * thinking that the extra 2 u64 args are the augmented filename, so just check
1849 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1850 */
1851 if (evsel != trace->syscalls.events.sys_enter)
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001852 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 -03001853 ttrace->entry_time = sample->time;
1854 msg = ttrace->entry_str;
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001855 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001856
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001857 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001858 args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001859
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001860 if (sc->is_exit) {
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001861 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001862 int alignment = 0;
1863
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001864 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001865 printed = fprintf(trace->output, "%s)", ttrace->entry_str);
1866 if (trace->args_alignment > printed)
1867 alignment = trace->args_alignment - printed;
1868 fprintf(trace->output, "%*s= ?\n", alignment, " ");
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001869 }
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001870 } else {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001871 ttrace->entry_pending = true;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001872 /* See trace__vfs_getname & trace__sys_exit */
1873 ttrace->filename.pending_open = false;
1874 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001875
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001876 if (trace->current != thread) {
1877 thread__put(trace->current);
1878 trace->current = thread__get(thread);
1879 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001880 err = 0;
1881out_put:
1882 thread__put(thread);
1883 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001884}
1885
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001886static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1887 struct perf_sample *sample)
1888{
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001889 struct thread_trace *ttrace;
1890 struct thread *thread;
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001891 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1892 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001893 char msg[1024];
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001894 void *args, *augmented_args = NULL;
1895 int augmented_args_size;
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001896
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001897 if (sc == NULL)
1898 return -1;
1899
1900 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1901 ttrace = thread__trace(thread, trace->output);
1902 /*
1903 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1904 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1905 */
1906 if (ttrace == NULL)
1907 goto out_put;
1908
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001909 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03001910 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 -03001911 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001912 fprintf(trace->output, "%s", msg);
1913 err = 0;
1914out_put:
1915 thread__put(thread);
1916 return err;
1917}
1918
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001919static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1920 struct perf_sample *sample,
1921 struct callchain_cursor *cursor)
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001922{
1923 struct addr_location al;
Ravi Bangoria3a9e9a42018-01-30 11:00:53 +05301924 int max_stack = evsel->attr.sample_max_stack ?
1925 evsel->attr.sample_max_stack :
1926 trace->max_stack;
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001927 int err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001928
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001929 if (machine__resolve(trace->host, &al, sample) < 0)
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001930 return -1;
1931
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001932 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1933 addr_location__put(&al);
1934 return err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001935}
1936
1937static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1938{
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001939 /* TODO: user-configurable print_opts */
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001940 const unsigned int print_opts = EVSEL__PRINT_SYM |
1941 EVSEL__PRINT_DSO |
1942 EVSEL__PRINT_UNKNOWN_AS_ADDR;
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001943
Arnaldo Carvalho de Melod327e602016-04-14 17:53:49 -03001944 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001945}
1946
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01001947static const char *errno_to_name(struct perf_evsel *evsel, int err)
1948{
1949 struct perf_env *env = perf_evsel__env(evsel);
1950 const char *arch_name = perf_env__arch(env);
1951
1952 return arch_syscalls__strerrno(arch_name, err);
1953}
1954
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001955static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001956 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001957 struct perf_sample *sample)
1958{
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09001959 long ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001960 u64 duration = 0;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001961 bool duration_calculated = false;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001962 struct thread *thread;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001963 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
1964 int alignment = trace->args_alignment;
David Ahernbf2575c2013-10-08 21:26:53 -06001965 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001966 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001967
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001968 if (sc == NULL)
1969 return -1;
1970
David Ahern8fb598e2013-09-28 13:13:00 -06001971 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001972 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001973 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001974 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001975
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001976 trace__fprintf_sample(trace, evsel, sample, thread);
1977
David Ahernbf2575c2013-10-08 21:26:53 -06001978 if (trace->summary)
1979 thread__update_stats(ttrace, id, sample);
1980
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001981 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001982
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001983 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001984 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
1985 ttrace->filename.pending_open = false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001986 ++trace->stats.vfs_getname;
1987 }
1988
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001989 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001990 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001991 if (trace__filter_duration(trace, duration))
1992 goto out;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001993 duration_calculated = true;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001994 } else if (trace->duration_filter)
1995 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001996
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001997 if (sample->callchain) {
1998 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
1999 if (callchain_ret == 0) {
2000 if (callchain_cursor.nr < trace->min_stack)
2001 goto out;
2002 callchain_ret = 1;
2003 }
2004 }
2005
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03002006 if (trace->summary_only || (ret >= 0 && trace->failure_only))
David Ahernfd2eaba2013-11-12 09:31:15 -07002007 goto out;
2008
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002009 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002010
2011 if (ttrace->entry_pending) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002012 printed = fprintf(trace->output, "%s", ttrace->entry_str);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002013 } else {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002014 fprintf(trace->output, " ... [");
2015 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
2016 fprintf(trace->output, "]: %s()", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002017 }
2018
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002019 printed++; /* the closing ')' */
2020
2021 if (alignment > printed)
2022 alignment -= printed;
2023 else
2024 alignment = 0;
2025
2026 fprintf(trace->output, ")%*s= ", alignment, " ");
2027
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002028 if (sc->fmt == NULL) {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002029 if (ret < 0)
2030 goto errno_print;
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002031signed_print:
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002032 fprintf(trace->output, "%ld", ret);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002033 } else if (ret < 0) {
2034errno_print: {
Masami Hiramatsu942a91e2014-08-14 02:22:41 +00002035 char bf[STRERR_BUFSIZE];
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002036 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01002037 *e = errno_to_name(evsel, -ret);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002038
Arnaldo Carvalho de Melo2c83dfa2018-12-17 12:06:34 -03002039 fprintf(trace->output, "-1 %s (%s)", e, emsg);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002040 }
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002041 } else if (ret == 0 && sc->fmt->timeout)
Arnaldo Carvalho de Melo2c83dfa2018-12-17 12:06:34 -03002042 fprintf(trace->output, "0 (Timeout)");
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002043 else if (ttrace->ret_scnprintf) {
2044 char bf[1024];
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03002045 struct syscall_arg arg = {
2046 .val = ret,
2047 .thread = thread,
2048 .trace = trace,
2049 };
2050 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002051 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002052 fprintf(trace->output, "%s", bf);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002053 } else if (sc->fmt->hexret)
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002054 fprintf(trace->output, "%#lx", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002055 else if (sc->fmt->errpid) {
2056 struct thread *child = machine__find_thread(trace->host, ret, ret);
2057
2058 if (child != NULL) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002059 fprintf(trace->output, "%ld", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002060 if (child->comm_set)
2061 fprintf(trace->output, " (%s)", thread__comm_str(child));
2062 thread__put(child);
2063 }
2064 } else
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002065 goto signed_print;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002066
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002067 fputc('\n', trace->output);
Milian Wolff566a0882016-04-08 13:34:15 +02002068
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002069 /*
2070 * We only consider an 'event' for the sake of --max-events a non-filtered
2071 * sys_enter + sys_exit and other tracepoint events.
2072 */
2073 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2074 interrupted = true;
2075
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002076 if (callchain_ret > 0)
2077 trace__fprintf_callchain(trace, sample);
2078 else if (callchain_ret < 0)
2079 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002080out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002081 ttrace->entry_pending = false;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002082 err = 0;
2083out_put:
2084 thread__put(thread);
2085 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002086}
2087
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002088static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002089 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002090 struct perf_sample *sample)
2091{
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002092 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2093 struct thread_trace *ttrace;
2094 size_t filename_len, entry_str_len, to_move;
2095 ssize_t remaining_space;
2096 char *pos;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002097 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002098
2099 if (!thread)
2100 goto out;
2101
2102 ttrace = thread__priv(thread);
2103 if (!ttrace)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002104 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002105
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002106 filename_len = strlen(filename);
Arnaldo Carvalho de Melo39f0e7a2017-03-24 14:51:28 -03002107 if (filename_len == 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002108 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002109
2110 if (ttrace->filename.namelen < filename_len) {
2111 char *f = realloc(ttrace->filename.name, filename_len + 1);
2112
2113 if (f == NULL)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002114 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002115
2116 ttrace->filename.namelen = filename_len;
2117 ttrace->filename.name = f;
2118 }
2119
2120 strcpy(ttrace->filename.name, filename);
2121 ttrace->filename.pending_open = true;
2122
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002123 if (!ttrace->filename.ptr)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002124 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002125
2126 entry_str_len = strlen(ttrace->entry_str);
2127 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2128 if (remaining_space <= 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002129 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002130
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002131 if (filename_len > (size_t)remaining_space) {
2132 filename += filename_len - remaining_space;
2133 filename_len = remaining_space;
2134 }
2135
2136 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2137 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2138 memmove(pos + filename_len, pos, to_move);
2139 memcpy(pos, filename, filename_len);
2140
2141 ttrace->filename.ptr = 0;
2142 ttrace->filename.entry_str_pos = 0;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002143out_put:
2144 thread__put(thread);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002145out:
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002146 return 0;
2147}
2148
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002149static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002150 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002151 struct perf_sample *sample)
2152{
2153 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2154 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
David Ahern8fb598e2013-09-28 13:13:00 -06002155 struct thread *thread = machine__findnew_thread(trace->host,
Adrian Hunter314add62013-08-27 11:23:03 +03002156 sample->pid,
2157 sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002158 struct thread_trace *ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002159
2160 if (ttrace == NULL)
2161 goto out_dump;
2162
2163 ttrace->runtime_ms += runtime_ms;
2164 trace->runtime_ms += runtime_ms;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002165out_put:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002166 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002167 return 0;
2168
2169out_dump:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002170 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002171 evsel->name,
2172 perf_evsel__strval(evsel, sample, "comm"),
2173 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2174 runtime,
2175 perf_evsel__intval(evsel, sample, "vruntime"));
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002176 goto out_put;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002177}
2178
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002179static int bpf_output__printer(enum binary_printer_ops op,
2180 unsigned int val, void *extra __maybe_unused, FILE *fp)
Wang Nan1d6c9402016-02-26 09:31:55 +00002181{
Wang Nan1d6c9402016-02-26 09:31:55 +00002182 unsigned char ch = (unsigned char)val;
2183
2184 switch (op) {
2185 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002186 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
Wang Nan1d6c9402016-02-26 09:31:55 +00002187 case BINARY_PRINT_DATA_BEGIN:
2188 case BINARY_PRINT_LINE_BEGIN:
2189 case BINARY_PRINT_ADDR:
2190 case BINARY_PRINT_NUM_DATA:
2191 case BINARY_PRINT_NUM_PAD:
2192 case BINARY_PRINT_SEP:
2193 case BINARY_PRINT_CHAR_PAD:
2194 case BINARY_PRINT_LINE_END:
2195 case BINARY_PRINT_DATA_END:
2196 default:
2197 break;
2198 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002199
2200 return 0;
Wang Nan1d6c9402016-02-26 09:31:55 +00002201}
2202
2203static void bpf_output__fprintf(struct trace *trace,
2204 struct perf_sample *sample)
2205{
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002206 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2207 bpf_output__printer, NULL, trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002208 ++trace->nr_events_printed;
Wang Nan1d6c9402016-02-26 09:31:55 +00002209}
2210
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002211static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2212 union perf_event *event __maybe_unused,
2213 struct perf_sample *sample)
2214{
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002215 struct thread *thread;
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002216 int callchain_ret = 0;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002217 /*
2218 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2219 * this event's max_events having been hit and this is an entry coming
2220 * from the ring buffer that we should discard, since the max events
2221 * have already been considered/printed.
2222 */
2223 if (evsel->disabled)
2224 return 0;
2225
2226 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002227
2228 if (sample->callchain) {
2229 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2230 if (callchain_ret == 0) {
2231 if (callchain_cursor.nr < trace->min_stack)
2232 goto out;
2233 callchain_ret = 1;
2234 }
2235 }
2236
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03002237 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002238 trace__fprintf_tstamp(trace, sample->time, trace->output);
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002239
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03002240 if (trace->trace_syscalls && trace->show_duration)
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002241 fprintf(trace->output, "( ): ");
2242
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002243 if (thread)
2244 trace__fprintf_comm_tid(trace, thread, trace->output);
2245
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002246 if (evsel == trace->syscalls.events.augmented) {
2247 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2248 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2249
2250 if (sc) {
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002251 fprintf(trace->output, "%s(", sc->name);
2252 trace__fprintf_sys_enter(trace, evsel, sample);
2253 fputc(')', trace->output);
2254 goto newline;
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002255 }
2256
2257 /*
2258 * XXX: Not having the associated syscall info or not finding/adding
2259 * the thread should never happen, but if it does...
2260 * fall thru and print it as a bpf_output event.
2261 */
2262 }
2263
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002264 fprintf(trace->output, "%s:", evsel->name);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002265
Wang Nan1d6c9402016-02-26 09:31:55 +00002266 if (perf_evsel__is_bpf_output(evsel)) {
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002267 bpf_output__fprintf(trace, sample);
Wang Nan1d6c9402016-02-26 09:31:55 +00002268 } else if (evsel->tp_format) {
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002269 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2270 trace__fprintf_sys_enter(trace, evsel, sample)) {
2271 event_format__fprintf(evsel->tp_format, sample->cpu,
2272 sample->raw_data, sample->raw_size,
2273 trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002274 ++trace->nr_events_printed;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002275
2276 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2277 perf_evsel__disable(evsel);
2278 perf_evsel__close(evsel);
2279 }
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002280 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002281 }
2282
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002283newline:
Changbin Du51125a22018-03-13 18:40:01 +08002284 fprintf(trace->output, "\n");
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002285
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002286 if (callchain_ret > 0)
2287 trace__fprintf_callchain(trace, sample);
2288 else if (callchain_ret < 0)
2289 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2290out:
Arnaldo Carvalho de Meloa937c662018-10-20 09:27:52 -03002291 thread__put(thread);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002292 return 0;
2293}
2294
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002295static void print_location(FILE *f, struct perf_sample *sample,
2296 struct addr_location *al,
2297 bool print_dso, bool print_sym)
2298{
2299
Namhyung Kimbb963e12017-02-17 17:17:38 +09002300 if ((verbose > 0 || print_dso) && al->map)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002301 fprintf(f, "%s@", al->map->dso->long_name);
2302
Namhyung Kimbb963e12017-02-17 17:17:38 +09002303 if ((verbose > 0 || print_sym) && al->sym)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002304 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002305 al->addr - al->sym->start);
2306 else if (al->map)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002307 fprintf(f, "0x%" PRIx64, al->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002308 else
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002309 fprintf(f, "0x%" PRIx64, sample->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002310}
2311
2312static int trace__pgfault(struct trace *trace,
2313 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03002314 union perf_event *event __maybe_unused,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002315 struct perf_sample *sample)
2316{
2317 struct thread *thread;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002318 struct addr_location al;
2319 char map_type = 'd';
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002320 struct thread_trace *ttrace;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002321 int err = -1;
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002322 int callchain_ret = 0;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002323
2324 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002325
2326 if (sample->callchain) {
2327 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2328 if (callchain_ret == 0) {
2329 if (callchain_cursor.nr < trace->min_stack)
2330 goto out_put;
2331 callchain_ret = 1;
2332 }
2333 }
2334
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002335 ttrace = thread__trace(thread, trace->output);
2336 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002337 goto out_put;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002338
2339 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2340 ttrace->pfmaj++;
2341 else
2342 ttrace->pfmin++;
2343
2344 if (trace->summary_only)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002345 goto out;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002346
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002347 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002348
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002349 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002350
2351 fprintf(trace->output, "%sfault [",
2352 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2353 "maj" : "min");
2354
2355 print_location(trace->output, sample, &al, false, true);
2356
2357 fprintf(trace->output, "] => ");
2358
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03002359 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002360
2361 if (!al.map) {
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002362 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002363
2364 if (al.map)
2365 map_type = 'x';
2366 else
2367 map_type = '?';
2368 }
2369
2370 print_location(trace->output, sample, &al, true, false);
2371
2372 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
Arnaldo Carvalho de Melo0c3a6ef2016-04-19 16:31:12 -03002373
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002374 if (callchain_ret > 0)
2375 trace__fprintf_callchain(trace, sample);
2376 else if (callchain_ret < 0)
2377 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002378
2379 ++trace->nr_events_printed;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002380out:
2381 err = 0;
2382out_put:
2383 thread__put(thread);
2384 return err;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002385}
2386
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002387static void trace__set_base_time(struct trace *trace,
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002388 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002389 struct perf_sample *sample)
2390{
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002391 /*
2392 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2393 * and don't use sample->time unconditionally, we may end up having
2394 * some other event in the future without PERF_SAMPLE_TIME for good
2395 * reason, i.e. we may not be interested in its timestamps, just in
2396 * it taking place, picking some piece of information when it
2397 * appears in our event stream (vfs_getname comes to mind).
2398 */
2399 if (trace->base_time == 0 && !trace->full_time &&
2400 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002401 trace->base_time = sample->time;
2402}
2403
David Ahern6810fc92013-08-28 22:29:52 -06002404static int trace__process_sample(struct perf_tool *tool,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002405 union perf_event *event,
David Ahern6810fc92013-08-28 22:29:52 -06002406 struct perf_sample *sample,
2407 struct perf_evsel *evsel,
2408 struct machine *machine __maybe_unused)
2409{
2410 struct trace *trace = container_of(tool, struct trace, tool);
David Ahernaa07df62016-11-25 09:29:52 -07002411 struct thread *thread;
David Ahern6810fc92013-08-28 22:29:52 -06002412 int err = 0;
2413
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002414 tracepoint_handler handler = evsel->handler;
David Ahern6810fc92013-08-28 22:29:52 -06002415
David Ahernaa07df62016-11-25 09:29:52 -07002416 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2417 if (thread && thread__is_filtered(thread))
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002418 goto out;
David Ahernbdc89662013-08-28 22:29:53 -06002419
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002420 trace__set_base_time(trace, evsel, sample);
David Ahern6810fc92013-08-28 22:29:52 -06002421
David Ahern31605652013-12-04 19:41:41 -07002422 if (handler) {
2423 ++trace->nr_events;
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002424 handler(trace, evsel, event, sample);
David Ahern31605652013-12-04 19:41:41 -07002425 }
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002426out:
2427 thread__put(thread);
David Ahern6810fc92013-08-28 22:29:52 -06002428 return err;
2429}
2430
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002431static int trace__record(struct trace *trace, int argc, const char **argv)
David Ahern5e2485b2013-09-28 13:13:01 -06002432{
2433 unsigned int rec_argc, i, j;
2434 const char **rec_argv;
2435 const char * const record_args[] = {
2436 "record",
2437 "-R",
2438 "-m", "1024",
2439 "-c", "1",
David Ahern5e2485b2013-09-28 13:13:01 -06002440 };
2441
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002442 const char * const sc_args[] = { "-e", };
2443 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2444 const char * const majpf_args[] = { "-e", "major-faults" };
2445 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2446 const char * const minpf_args[] = { "-e", "minor-faults" };
2447 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2448
David Ahern9aca7f12013-12-04 19:41:39 -07002449 /* +1 is for the event string below */
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002450 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2451 majpf_args_nr + minpf_args_nr + argc;
David Ahern5e2485b2013-09-28 13:13:01 -06002452 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2453
2454 if (rec_argv == NULL)
2455 return -ENOMEM;
2456
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002457 j = 0;
David Ahern5e2485b2013-09-28 13:13:01 -06002458 for (i = 0; i < ARRAY_SIZE(record_args); i++)
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002459 rec_argv[j++] = record_args[i];
2460
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002461 if (trace->trace_syscalls) {
2462 for (i = 0; i < sc_args_nr; i++)
2463 rec_argv[j++] = sc_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002464
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002465 /* event string may be different for older kernels - e.g., RHEL6 */
2466 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2467 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2468 else if (is_valid_tracepoint("syscalls:sys_enter"))
2469 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2470 else {
2471 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
Martin Kepplingerc896f852017-09-13 21:14:19 +02002472 free(rec_argv);
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002473 return -1;
2474 }
David Ahern9aca7f12013-12-04 19:41:39 -07002475 }
David Ahern9aca7f12013-12-04 19:41:39 -07002476
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002477 if (trace->trace_pgfaults & TRACE_PFMAJ)
2478 for (i = 0; i < majpf_args_nr; i++)
2479 rec_argv[j++] = majpf_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002480
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002481 if (trace->trace_pgfaults & TRACE_PFMIN)
2482 for (i = 0; i < minpf_args_nr; i++)
2483 rec_argv[j++] = minpf_args[i];
2484
2485 for (i = 0; i < (unsigned int)argc; i++)
2486 rec_argv[j++] = argv[i];
2487
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002488 return cmd_record(j, rec_argv);
David Ahern5e2485b2013-09-28 13:13:01 -06002489}
2490
David Ahernbf2575c2013-10-08 21:26:53 -06002491static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2492
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002493static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002494{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002495 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002496
2497 if (IS_ERR(evsel))
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002498 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002499
2500 if (perf_evsel__field(evsel, "pathname") == NULL) {
2501 perf_evsel__delete(evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002502 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002503 }
2504
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002505 evsel->handler = trace__vfs_getname;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002506 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002507 return true;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002508}
2509
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002510static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002511{
2512 struct perf_evsel *evsel;
2513 struct perf_event_attr attr = {
2514 .type = PERF_TYPE_SOFTWARE,
2515 .mmap_data = 1,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002516 };
2517
2518 attr.config = config;
Arnaldo Carvalho de Melo05247982014-07-23 18:15:09 -03002519 attr.sample_period = 1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002520
2521 event_attr_init(&attr);
2522
2523 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002524 if (evsel)
2525 evsel->handler = trace__pgfault;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002526
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002527 return evsel;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002528}
2529
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002530static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2531{
2532 const u32 type = event->header.type;
2533 struct perf_evsel *evsel;
2534
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002535 if (type != PERF_RECORD_SAMPLE) {
2536 trace__process_event(trace, trace->host, event, sample);
2537 return;
2538 }
2539
2540 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2541 if (evsel == NULL) {
2542 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2543 return;
2544 }
2545
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002546 trace__set_base_time(trace, evsel, sample);
2547
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002548 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2549 sample->raw_data == NULL) {
2550 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2551 perf_evsel__name(evsel), sample->tid,
2552 sample->cpu, sample->raw_size);
2553 } else {
2554 tracepoint_handler handler = evsel->handler;
2555 handler(trace, evsel, event, sample);
2556 }
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002557
2558 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2559 interrupted = true;
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002560}
2561
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002562static int trace__add_syscall_newtp(struct trace *trace)
2563{
2564 int ret = -1;
2565 struct perf_evlist *evlist = trace->evlist;
2566 struct perf_evsel *sys_enter, *sys_exit;
2567
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002568 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002569 if (sys_enter == NULL)
2570 goto out;
2571
2572 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2573 goto out_delete_sys_enter;
2574
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002575 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002576 if (sys_exit == NULL)
2577 goto out_delete_sys_enter;
2578
2579 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2580 goto out_delete_sys_exit;
2581
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002582 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2583 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2584
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002585 perf_evlist__add(evlist, sys_enter);
2586 perf_evlist__add(evlist, sys_exit);
2587
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002588 if (callchain_param.enabled && !trace->kernel_syscallchains) {
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03002589 /*
2590 * We're interested only in the user space callchain
2591 * leading to the syscall, allow overriding that for
2592 * debugging reasons using --kernel_syscall_callchains
2593 */
2594 sys_exit->attr.exclude_callchain_kernel = 1;
2595 }
2596
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03002597 trace->syscalls.events.sys_enter = sys_enter;
2598 trace->syscalls.events.sys_exit = sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002599
2600 ret = 0;
2601out:
2602 return ret;
2603
2604out_delete_sys_exit:
2605 perf_evsel__delete_priv(sys_exit);
2606out_delete_sys_enter:
2607 perf_evsel__delete_priv(sys_enter);
2608 goto out;
2609}
2610
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002611static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002612{
2613 int err = -1;
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002614 struct perf_evsel *sys_exit;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002615 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2616 trace->ev_qualifier_ids.nr,
2617 trace->ev_qualifier_ids.entries);
2618
2619 if (filter == NULL)
2620 goto out_enomem;
2621
Mathieu Poirier3541c032016-09-16 08:44:04 -06002622 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2623 filter)) {
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002624 sys_exit = trace->syscalls.events.sys_exit;
Mathieu Poirier3541c032016-09-16 08:44:04 -06002625 err = perf_evsel__append_tp_filter(sys_exit, filter);
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002626 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002627
2628 free(filter);
2629out:
2630 return err;
2631out_enomem:
2632 errno = ENOMEM;
2633 goto out;
2634}
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002635
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002636#ifdef HAVE_LIBBPF_SUPPORT
2637static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2638{
2639 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002640 struct bpf_map_syscall_entry value = {
2641 .enabled = !trace->not_ev_qualifier,
2642 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002643 int err = 0;
2644 size_t i;
2645
2646 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2647 int key = trace->ev_qualifier_ids.entries[i];
2648
2649 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2650 if (err)
2651 break;
2652 }
2653
2654 return err;
2655}
2656
2657static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2658{
2659 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002660 struct bpf_map_syscall_entry value = {
2661 .enabled = enabled,
2662 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002663 int err = 0, key;
2664
2665 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002666 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002667 if (err)
2668 break;
2669 }
2670
2671 return err;
2672}
2673
2674static int trace__init_syscalls_bpf_map(struct trace *trace)
2675{
2676 bool enabled = true;
2677
2678 if (trace->ev_qualifier_ids.nr)
2679 enabled = trace->not_ev_qualifier;
2680
2681 return __trace__init_syscalls_bpf_map(trace, enabled);
2682}
2683#else
2684static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
2685{
2686 return 0;
2687}
2688
2689static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
2690{
2691 return 0;
2692}
2693#endif // HAVE_LIBBPF_SUPPORT
2694
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002695static int trace__set_ev_qualifier_filter(struct trace *trace)
2696{
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002697 if (trace->syscalls.map)
2698 return trace__set_ev_qualifier_bpf_filter(trace);
Arnaldo Carvalho de Melof76214f2018-12-19 10:01:20 -03002699 if (trace->syscalls.events.sys_enter)
2700 return trace__set_ev_qualifier_tp_filter(trace);
2701 return 0;
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002702}
2703
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002704static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2705 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2706{
2707 int err = 0;
2708#ifdef HAVE_LIBBPF_SUPPORT
2709 bool value = true;
2710 int map_fd = bpf_map__fd(map);
2711 size_t i;
2712
2713 for (i = 0; i < npids; ++i) {
2714 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2715 if (err)
2716 break;
2717 }
2718#endif
2719 return err;
2720}
2721
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002722static int trace__set_filter_loop_pids(struct trace *trace)
2723{
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002724 unsigned int nr = 1, err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002725 pid_t pids[32] = {
2726 getpid(),
2727 };
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03002728 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2729
2730 while (thread && nr < ARRAY_SIZE(pids)) {
2731 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2732
2733 if (parent == NULL)
2734 break;
2735
2736 if (!strcmp(thread__comm_str(parent), "sshd")) {
2737 pids[nr++] = parent->tid;
2738 break;
2739 }
2740 thread = parent;
2741 }
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002742
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002743 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2744 if (!err && trace->filter_pids.map)
2745 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2746
2747 return err;
2748}
2749
2750static int trace__set_filter_pids(struct trace *trace)
2751{
2752 int err = 0;
2753 /*
2754 * Better not use !target__has_task() here because we need to cover the
2755 * case where no threads were specified in the command line, but a
2756 * workload was, and in that case we will fill in the thread_map when
2757 * we fork the workload in perf_evlist__prepare_workload.
2758 */
2759 if (trace->filter_pids.nr > 0) {
2760 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2761 trace->filter_pids.entries);
2762 if (!err && trace->filter_pids.map) {
2763 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2764 trace->filter_pids.entries);
2765 }
2766 } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2767 err = trace__set_filter_loop_pids(trace);
2768 }
2769
2770 return err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002771}
2772
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002773static int __trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa1f44b3e2018-12-05 17:05:08 +01002774{
2775 struct perf_evlist *evlist = trace->evlist;
2776 struct perf_sample sample;
2777 int err;
2778
2779 err = perf_evlist__parse_sample(evlist, event, &sample);
2780 if (err)
2781 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
2782 else
2783 trace__handle_event(trace, event, &sample);
2784
2785 return 0;
2786}
2787
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002788static int __trace__flush_events(struct trace *trace)
Jiri Olsa028713a2018-12-05 17:05:09 +01002789{
2790 u64 first = ordered_events__first_time(&trace->oe.data);
2791 u64 flush = trace->oe.last - NSEC_PER_SEC;
2792
2793 /* Is there some thing to flush.. */
2794 if (first && first < flush)
2795 return ordered_events__flush_time(&trace->oe.data, flush);
2796
2797 return 0;
2798}
2799
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002800static int trace__flush_events(struct trace *trace)
2801{
2802 return !trace->sort_events ? 0 : __trace__flush_events(trace);
2803}
2804
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002805static int trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa028713a2018-12-05 17:05:09 +01002806{
Jiri Olsa028713a2018-12-05 17:05:09 +01002807 int err;
2808
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002809 if (!trace->sort_events)
2810 return __trace__deliver_event(trace, event);
2811
2812 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
Jiri Olsa028713a2018-12-05 17:05:09 +01002813 if (err && err != -1)
2814 return err;
2815
2816 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
2817 if (err)
2818 return err;
2819
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002820 return trace__flush_events(trace);
Jiri Olsa028713a2018-12-05 17:05:09 +01002821}
2822
2823static int ordered_events__deliver_event(struct ordered_events *oe,
2824 struct ordered_event *event)
2825{
2826 struct trace *trace = container_of(oe, struct trace, oe.data);
2827
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002828 return __trace__deliver_event(trace, event->event);
Jiri Olsa028713a2018-12-05 17:05:09 +01002829}
2830
Namhyung Kimf15eb532012-10-05 14:02:16 +09002831static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002832{
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002833 struct perf_evlist *evlist = trace->evlist;
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002834 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002835 int err = -1, i;
2836 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002837 const bool forks = argc > 0;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002838 bool draining = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002839
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002840 trace->live = true;
2841
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002842 if (!trace->raw_augmented_syscalls) {
2843 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
2844 goto out_error_raw_syscalls;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002845
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002846 if (trace->trace_syscalls)
2847 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
2848 }
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002849
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002850 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2851 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2852 if (pgfault_maj == NULL)
2853 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002854 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002855 perf_evlist__add(evlist, pgfault_maj);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03002856 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002857
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002858 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2859 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2860 if (pgfault_min == NULL)
2861 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002862 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002863 perf_evlist__add(evlist, pgfault_min);
2864 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002865
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002866 if (trace->sched &&
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002867 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2868 trace__sched_stat_runtime))
2869 goto out_error_sched_stat_runtime;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002870
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03002871 /*
2872 * If a global cgroup was set, apply it to all the events without an
2873 * explicit cgroup. I.e.:
2874 *
2875 * trace -G A -e sched:*switch
2876 *
2877 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2878 * _and_ sched:sched_switch to the 'A' cgroup, while:
2879 *
2880 * trace -e sched:*switch -G A
2881 *
2882 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2883 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2884 * a cgroup (on the root cgroup, sys wide, etc).
2885 *
2886 * Multiple cgroups:
2887 *
2888 * trace -G A -e sched:*switch -G B
2889 *
2890 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2891 * to the 'B' cgroup.
2892 *
2893 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2894 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2895 */
2896 if (trace->cgroup)
2897 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2898
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002899 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2900 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002901 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002902 goto out_delete_evlist;
2903 }
2904
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002905 err = trace__symbols_init(trace, evlist);
2906 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002907 fprintf(trace->output, "Problems initializing symbol libraries!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002908 goto out_delete_evlist;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002909 }
2910
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03002911 perf_evlist__config(evlist, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melofde54b72016-04-11 18:42:37 -03002912
Namhyung Kimf15eb532012-10-05 14:02:16 +09002913 signal(SIGCHLD, sig_handler);
2914 signal(SIGINT, sig_handler);
2915
2916 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09002917 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03002918 argv, false, NULL);
Namhyung Kimf15eb532012-10-05 14:02:16 +09002919 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002920 fprintf(trace->output, "Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002921 goto out_delete_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002922 }
2923 }
2924
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002925 err = perf_evlist__open(evlist);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002926 if (err < 0)
2927 goto out_error_open;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002928
Wang Nanba504232016-02-26 09:31:54 +00002929 err = bpf__apply_obj_config();
2930 if (err) {
2931 char errbuf[BUFSIZ];
2932
2933 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2934 pr_err("ERROR: Apply config to BPF failed: %s\n",
2935 errbuf);
2936 goto out_error_open;
2937 }
2938
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002939 err = trace__set_filter_pids(trace);
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002940 if (err < 0)
2941 goto out_error_mem;
2942
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002943 if (trace->syscalls.map)
2944 trace__init_syscalls_bpf_map(trace);
2945
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002946 if (trace->ev_qualifier_ids.nr > 0) {
2947 err = trace__set_ev_qualifier_filter(trace);
2948 if (err < 0)
2949 goto out_errno;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002950
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002951 if (trace->syscalls.events.sys_exit) {
2952 pr_debug("event qualifier tracepoint filter: %s\n",
2953 trace->syscalls.events.sys_exit->filter);
2954 }
Arnaldo Carvalho de Melo2e5e5f82015-08-03 17:12:29 -03002955 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002956
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002957 err = perf_evlist__apply_filters(evlist, &evsel);
2958 if (err < 0)
2959 goto out_error_apply_filters;
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08002960
Wang Nanf74b9d3a2017-12-03 02:00:37 +00002961 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002962 if (err < 0)
2963 goto out_error_mmap;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002964
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002965 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
Arnaldo Carvalho de Melocb24d012015-04-22 10:04:23 -03002966 perf_evlist__enable(evlist);
2967
Namhyung Kimf15eb532012-10-05 14:02:16 +09002968 if (forks)
2969 perf_evlist__start_workload(evlist);
2970
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002971 if (trace->opts.initial_delay) {
2972 usleep(trace->opts.initial_delay * 1000);
2973 perf_evlist__enable(evlist);
2974 }
2975
Jiri Olsae13798c2015-06-23 00:36:02 +02002976 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
Arnaldo Carvalho de Melo42052be2015-02-13 12:32:45 -03002977 evlist->threads->nr > 1 ||
2978 perf_evlist__first(evlist)->attr.inherit;
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002979
2980 /*
2981 * Now that we already used evsel->attr to ask the kernel to setup the
2982 * events, lets reuse evsel->attr.sample_max_stack as the limit in
2983 * trace__resolve_callchain(), allowing per-event max-stack settings
Ingo Molnaradba1632018-12-03 11:22:00 +01002984 * to override an explicitly set --max-stack global setting.
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002985 */
2986 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03002987 if (evsel__has_callchain(evsel) &&
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002988 evsel->attr.sample_max_stack == 0)
2989 evsel->attr.sample_max_stack = trace->max_stack;
2990 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002991again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002992 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002993
2994 for (i = 0; i < evlist->nr_mmaps; i++) {
2995 union perf_event *event;
Kan Liangd7f55c62018-03-01 18:08:59 -05002996 struct perf_mmap *md;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002997
Kan Liangd7f55c62018-03-01 18:08:59 -05002998 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -05002999 if (perf_mmap__read_init(md) < 0)
Kan Liangd7f55c62018-03-01 18:08:59 -05003000 continue;
3001
Kan Liang0019dc872018-03-06 10:36:06 -05003002 while ((event = perf_mmap__read_event(md)) != NULL) {
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003003 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003004
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003005 err = trace__deliver_event(trace, event);
Jiri Olsa028713a2018-12-05 17:05:09 +01003006 if (err)
3007 goto out_disable;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003008
Kan Liangd6ace3d2018-03-06 10:36:05 -05003009 perf_mmap__consume(md);
Arnaldo Carvalho de Melo20c5f102013-09-03 11:55:07 -03003010
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003011 if (interrupted)
3012 goto out_disable;
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03003013
3014 if (done && !draining) {
3015 perf_evlist__disable(evlist);
3016 draining = true;
3017 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003018 }
Kan Liangd7f55c62018-03-01 18:08:59 -05003019 perf_mmap__read_done(md);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003020 }
3021
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003022 if (trace->nr_events == before) {
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003023 int timeout = done ? 100 : -1;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003024
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003025 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03003026 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003027 draining = true;
3028
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003029 goto again;
Jiri Olsa028713a2018-12-05 17:05:09 +01003030 } else {
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003031 if (trace__flush_events(trace))
Jiri Olsa028713a2018-12-05 17:05:09 +01003032 goto out_disable;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003033 }
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003034 } else {
3035 goto again;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003036 }
3037
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003038out_disable:
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03003039 thread__zput(trace->current);
3040
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003041 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003042
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003043 if (trace->sort_events)
3044 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
Jiri Olsa028713a2018-12-05 17:05:09 +01003045
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003046 if (!err) {
3047 if (trace->summary)
3048 trace__fprintf_thread_summary(trace, trace->output);
3049
3050 if (trace->show_tool_stats) {
3051 fprintf(trace->output, "Stats:\n "
3052 " vfs_getname : %" PRIu64 "\n"
3053 " proc_getname: %" PRIu64 "\n",
3054 trace->stats.vfs_getname,
3055 trace->stats.proc_getname);
3056 }
3057 }
David Ahernbf2575c2013-10-08 21:26:53 -06003058
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003059out_delete_evlist:
Andrei Vagin33974a42017-11-07 16:22:45 -08003060 trace__symbols__exit(trace);
3061
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003062 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003063 cgroup__put(trace->cgroup);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003064 trace->evlist = NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03003065 trace->live = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003066 return err;
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003067{
3068 char errbuf[BUFSIZ];
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003069
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003070out_error_sched_stat_runtime:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003071 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003072 goto out_error;
3073
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03003074out_error_raw_syscalls:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003075 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003076 goto out_error;
3077
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03003078out_error_mmap:
3079 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3080 goto out_error;
3081
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003082out_error_open:
3083 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3084
3085out_error:
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003086 fprintf(trace->output, "%s\n", errbuf);
Ramkumar Ramachandra87f91862013-10-04 10:47:31 +05303087 goto out_delete_evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003088
3089out_error_apply_filters:
3090 fprintf(trace->output,
3091 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3092 evsel->filter, perf_evsel__name(evsel), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03003093 str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003094 goto out_delete_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003095}
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03003096out_error_mem:
3097 fprintf(trace->output, "Not enough memory to run!\n");
3098 goto out_delete_evlist;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003099
3100out_errno:
3101 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3102 goto out_delete_evlist;
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003103}
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003104
David Ahern6810fc92013-08-28 22:29:52 -06003105static int trace__replay(struct trace *trace)
3106{
3107 const struct perf_evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003108 { "probe:vfs_getname", trace__vfs_getname, },
David Ahern6810fc92013-08-28 22:29:52 -06003109 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003110 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003111 .file = {
3112 .path = input_name,
3113 },
3114 .mode = PERF_DATA_MODE_READ,
3115 .force = trace->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003116 };
David Ahern6810fc92013-08-28 22:29:52 -06003117 struct perf_session *session;
Namhyung Kim003824e2013-11-12 15:25:00 +09003118 struct perf_evsel *evsel;
David Ahern6810fc92013-08-28 22:29:52 -06003119 int err = -1;
3120
3121 trace->tool.sample = trace__process_sample;
3122 trace->tool.mmap = perf_event__process_mmap;
David Ahern384c6712013-09-22 19:44:58 -06003123 trace->tool.mmap2 = perf_event__process_mmap2;
David Ahern6810fc92013-08-28 22:29:52 -06003124 trace->tool.comm = perf_event__process_comm;
3125 trace->tool.exit = perf_event__process_exit;
3126 trace->tool.fork = perf_event__process_fork;
3127 trace->tool.attr = perf_event__process_attr;
Hari Bathinif3b36142017-03-08 02:11:43 +05303128 trace->tool.tracing_data = perf_event__process_tracing_data;
David Ahern6810fc92013-08-28 22:29:52 -06003129 trace->tool.build_id = perf_event__process_build_id;
Hari Bathinif3b36142017-03-08 02:11:43 +05303130 trace->tool.namespaces = perf_event__process_namespaces;
David Ahern6810fc92013-08-28 22:29:52 -06003131
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003132 trace->tool.ordered_events = true;
David Ahern6810fc92013-08-28 22:29:52 -06003133 trace->tool.ordering_requires_timestamps = true;
3134
3135 /* add tid to output */
3136 trace->multiple_threads = true;
3137
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003138 session = perf_session__new(&data, false, &trace->tool);
David Ahern6810fc92013-08-28 22:29:52 -06003139 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003140 return -1;
David Ahern6810fc92013-08-28 22:29:52 -06003141
David Ahernaa07df62016-11-25 09:29:52 -07003142 if (trace->opts.target.pid)
3143 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3144
3145 if (trace->opts.target.tid)
3146 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3147
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003148 if (symbol__init(&session->header.env) < 0)
Namhyung Kimcb2ffae2014-08-12 15:40:44 +09003149 goto out;
3150
David Ahern8fb598e2013-09-28 13:13:00 -06003151 trace->host = &session->machines.host;
3152
David Ahern6810fc92013-08-28 22:29:52 -06003153 err = perf_session__set_tracepoints_handlers(session, handlers);
3154 if (err)
3155 goto out;
3156
Namhyung Kim003824e2013-11-12 15:25:00 +09003157 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3158 "raw_syscalls:sys_enter");
David Ahern9aca7f12013-12-04 19:41:39 -07003159 /* older kernels have syscalls tp versus raw_syscalls */
3160 if (evsel == NULL)
3161 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3162 "syscalls:sys_enter");
David Ahern6810fc92013-08-28 22:29:52 -06003163
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003164 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003165 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003166 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003167 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3168 goto out;
3169 }
3170
3171 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3172 "raw_syscalls:sys_exit");
David Ahern9aca7f12013-12-04 19:41:39 -07003173 if (evsel == NULL)
3174 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3175 "syscalls:sys_exit");
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003176 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003177 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003178 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003179 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
David Ahern6810fc92013-08-28 22:29:52 -06003180 goto out;
3181 }
3182
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003183 evlist__for_each_entry(session->evlist, evsel) {
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003184 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
3185 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3186 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3187 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3188 evsel->handler = trace__pgfault;
3189 }
3190
David Ahern6810fc92013-08-28 22:29:52 -06003191 setup_pager();
3192
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03003193 err = perf_session__process_events(session);
David Ahern6810fc92013-08-28 22:29:52 -06003194 if (err)
3195 pr_err("Failed to process events, error %d", err);
3196
David Ahernbf2575c2013-10-08 21:26:53 -06003197 else if (trace->summary)
3198 trace__fprintf_thread_summary(trace, trace->output);
3199
David Ahern6810fc92013-08-28 22:29:52 -06003200out:
3201 perf_session__delete(session);
3202
3203 return err;
3204}
3205
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003206static size_t trace__fprintf_threads_header(FILE *fp)
3207{
3208 size_t printed;
3209
Pekka Enberg99ff7152013-11-12 16:42:14 +02003210 printed = fprintf(fp, "\n Summary of events:\n\n");
David Ahernbf2575c2013-10-08 21:26:53 -06003211
3212 return printed;
3213}
3214
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003215DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3216 struct stats *stats;
3217 double msecs;
3218 int syscall;
3219)
3220{
3221 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3222 struct stats *stats = source->priv;
3223
3224 entry->syscall = source->i;
3225 entry->stats = stats;
3226 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3227}
3228
David Ahernbf2575c2013-10-08 21:26:53 -06003229static size_t thread__dump_stats(struct thread_trace *ttrace,
3230 struct trace *trace, FILE *fp)
3231{
David Ahernbf2575c2013-10-08 21:26:53 -06003232 size_t printed = 0;
3233 struct syscall *sc;
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003234 struct rb_node *nd;
3235 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003236
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003237 if (syscall_stats == NULL)
David Ahernbf2575c2013-10-08 21:26:53 -06003238 return 0;
3239
3240 printed += fprintf(fp, "\n");
3241
Milian Wolff834fd462015-08-06 11:24:29 +02003242 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3243 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3244 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
Pekka Enberg99ff7152013-11-12 16:42:14 +02003245
Arnaldo Carvalho de Melo98a91832016-06-23 11:34:10 -03003246 resort_rb__for_each_entry(nd, syscall_stats) {
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003247 struct stats *stats = syscall_stats_entry->stats;
David Ahernbf2575c2013-10-08 21:26:53 -06003248 if (stats) {
3249 double min = (double)(stats->min) / NSEC_PER_MSEC;
3250 double max = (double)(stats->max) / NSEC_PER_MSEC;
3251 double avg = avg_stats(stats);
3252 double pct;
3253 u64 n = (u64) stats->n;
3254
3255 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3256 avg /= NSEC_PER_MSEC;
3257
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003258 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
Pekka Enberg99ff7152013-11-12 16:42:14 +02003259 printed += fprintf(fp, " %-15s", sc->name);
Milian Wolff834fd462015-08-06 11:24:29 +02003260 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003261 n, syscall_stats_entry->msecs, min, avg);
Pekka Enberg27a778b2013-11-13 14:21:48 +02003262 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
David Ahernbf2575c2013-10-08 21:26:53 -06003263 }
David Ahernbf2575c2013-10-08 21:26:53 -06003264 }
3265
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003266 resort_rb__delete(syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003267 printed += fprintf(fp, "\n\n");
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003268
3269 return printed;
3270}
3271
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003272static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
David Ahern896cbb52013-09-28 13:12:59 -06003273{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003274 size_t printed = 0;
Namhyung Kim89dceb22014-10-06 09:46:03 +09003275 struct thread_trace *ttrace = thread__priv(thread);
David Ahern896cbb52013-09-28 13:12:59 -06003276 double ratio;
3277
3278 if (ttrace == NULL)
3279 return 0;
3280
3281 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3282
Pekka Enberg15e65c62013-11-14 18:43:30 +02003283 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
Pekka Enberg99ff7152013-11-12 16:42:14 +02003284 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
Pekka Enberg15e65c62013-11-14 18:43:30 +02003285 printed += fprintf(fp, "%.1f%%", ratio);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04003286 if (ttrace->pfmaj)
3287 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3288 if (ttrace->pfmin)
3289 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
Arnaldo Carvalho de Melo03548eb2016-05-05 15:46:50 -03003290 if (trace->sched)
3291 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3292 else if (fputc('\n', fp) != EOF)
3293 ++printed;
3294
David Ahernbf2575c2013-10-08 21:26:53 -06003295 printed += thread__dump_stats(ttrace, trace, fp);
David Ahern896cbb52013-09-28 13:12:59 -06003296
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003297 return printed;
3298}
David Ahern896cbb52013-09-28 13:12:59 -06003299
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003300static unsigned long thread__nr_events(struct thread_trace *ttrace)
3301{
3302 return ttrace ? ttrace->nr_events : 0;
3303}
3304
3305DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3306 struct thread *thread;
3307)
3308{
3309 entry->thread = rb_entry(nd, struct thread, rb_node);
David Ahern896cbb52013-09-28 13:12:59 -06003310}
3311
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003312static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3313{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003314 size_t printed = trace__fprintf_threads_header(fp);
3315 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -07003316 int i;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003317
Kan Liang91e467b2017-09-10 19:23:14 -07003318 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3319 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3320
3321 if (threads == NULL) {
3322 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3323 return 0;
3324 }
3325
3326 resort_rb__for_each_entry(nd, threads)
3327 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3328
3329 resort_rb__delete(threads);
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003330 }
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003331 return printed;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003332}
3333
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003334static int trace__set_duration(const struct option *opt, const char *str,
3335 int unset __maybe_unused)
3336{
3337 struct trace *trace = opt->value;
3338
3339 trace->duration_filter = atof(str);
3340 return 0;
3341}
3342
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003343static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3344 int unset __maybe_unused)
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08003345{
3346 int ret = -1;
3347 size_t i;
3348 struct trace *trace = opt->value;
3349 /*
3350 * FIXME: introduce a intarray class, plain parse csv and create a
3351 * { int nr, int entries[] } struct...
3352 */
3353 struct intlist *list = intlist__new(str);
3354
3355 if (list == NULL)
3356 return -1;
3357
3358 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3359 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3360
3361 if (trace->filter_pids.entries == NULL)
3362 goto out;
3363
3364 trace->filter_pids.entries[0] = getpid();
3365
3366 for (i = 1; i < trace->filter_pids.nr; ++i)
3367 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3368
3369 intlist__delete(list);
3370 ret = 0;
3371out:
3372 return ret;
3373}
3374
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003375static int trace__open_output(struct trace *trace, const char *filename)
3376{
3377 struct stat st;
3378
3379 if (!stat(filename, &st) && st.st_size) {
3380 char oldname[PATH_MAX];
3381
3382 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3383 unlink(oldname);
3384 rename(filename, oldname);
3385 }
3386
3387 trace->output = fopen(filename, "w");
3388
3389 return trace->output == NULL ? -errno : 0;
3390}
3391
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003392static int parse_pagefaults(const struct option *opt, const char *str,
3393 int unset __maybe_unused)
3394{
3395 int *trace_pgfaults = opt->value;
3396
3397 if (strcmp(str, "all") == 0)
3398 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3399 else if (strcmp(str, "maj") == 0)
3400 *trace_pgfaults |= TRACE_PFMAJ;
3401 else if (strcmp(str, "min") == 0)
3402 *trace_pgfaults |= TRACE_PFMIN;
3403 else
3404 return -1;
3405
3406 return 0;
3407}
3408
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003409static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3410{
3411 struct perf_evsel *evsel;
3412
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003413 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003414 evsel->handler = handler;
3415}
3416
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003417static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3418{
3419 struct perf_evsel *evsel;
3420
3421 evlist__for_each_entry(evlist, evsel) {
3422 if (evsel->priv || !evsel->tp_format)
3423 continue;
3424
3425 if (strcmp(evsel->tp_format->system, "syscalls"))
3426 continue;
3427
3428 if (perf_evsel__init_syscall_tp(evsel))
3429 return -1;
3430
3431 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3432 struct syscall_tp *sc = evsel->priv;
3433
3434 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3435 return -1;
3436 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3437 struct syscall_tp *sc = evsel->priv;
3438
3439 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3440 return -1;
3441 }
3442 }
3443
3444 return 0;
3445}
3446
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003447/*
3448 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3449 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3450 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3451 *
3452 * It'd be better to introduce a parse_options() variant that would return a
3453 * list with the terms it didn't match to an event...
3454 */
3455static int trace__parse_events_option(const struct option *opt, const char *str,
3456 int unset __maybe_unused)
3457{
3458 struct trace *trace = (struct trace *)opt->value;
3459 const char *s = str;
3460 char *sep = NULL, *lists[2] = { NULL, NULL, };
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003461 int len = strlen(str) + 1, err = -1, list, idx;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003462 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3463 char group_name[PATH_MAX];
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003464 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003465
3466 if (strace_groups_dir == NULL)
3467 return -1;
3468
3469 if (*s == '!') {
3470 ++s;
3471 trace->not_ev_qualifier = true;
3472 }
3473
3474 while (1) {
3475 if ((sep = strchr(s, ',')) != NULL)
3476 *sep = '\0';
3477
3478 list = 0;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003479 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3480 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003481 list = 1;
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003482 goto do_concat;
3483 }
3484
3485 fmt = syscall_fmt__find_by_alias(s);
3486 if (fmt != NULL) {
3487 list = 1;
3488 s = fmt->name;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003489 } else {
3490 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3491 if (access(group_name, R_OK) == 0)
3492 list = 1;
3493 }
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003494do_concat:
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003495 if (lists[list]) {
3496 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3497 } else {
3498 lists[list] = malloc(len);
3499 if (lists[list] == NULL)
3500 goto out;
3501 strcpy(lists[list], s);
3502 }
3503
3504 if (!sep)
3505 break;
3506
3507 *sep = ',';
3508 s = sep + 1;
3509 }
3510
3511 if (lists[1] != NULL) {
3512 struct strlist_config slist_config = {
3513 .dirname = strace_groups_dir,
3514 };
3515
3516 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3517 if (trace->ev_qualifier == NULL) {
3518 fputs("Not enough memory to parse event qualifier", trace->output);
3519 goto out;
3520 }
3521
3522 if (trace__validate_ev_qualifier(trace))
3523 goto out;
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003524 trace->trace_syscalls = true;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003525 }
3526
3527 err = 0;
3528
3529 if (lists[0]) {
3530 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3531 "event selector. use 'perf list' to list available events",
3532 parse_events_option);
3533 err = parse_events_option(&o, lists[0], 0);
3534 }
3535out:
3536 if (sep)
3537 *sep = ',';
3538
3539 return err;
3540}
3541
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003542static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3543{
3544 struct trace *trace = opt->value;
3545
3546 if (!list_empty(&trace->evlist->entries))
3547 return parse_cgroups(opt, str, unset);
3548
3549 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3550
3551 return 0;
3552}
3553
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003554static struct bpf_map *bpf__find_map_by_name(const char *name)
3555{
3556 struct bpf_object *obj, *tmp;
3557
3558 bpf_object__for_each_safe(obj, tmp) {
3559 struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3560 if (map)
3561 return map;
3562
3563 }
3564
3565 return NULL;
3566}
3567
3568static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3569{
3570 trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3571}
3572
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003573static void trace__set_bpf_map_syscalls(struct trace *trace)
3574{
3575 trace->syscalls.map = bpf__find_map_by_name("syscalls");
3576}
3577
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003578static int trace__config(const char *var, const char *value, void *arg)
3579{
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03003580 struct trace *trace = arg;
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003581 int err = 0;
3582
3583 if (!strcmp(var, "trace.add_events")) {
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003584 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3585 "event selector. use 'perf list' to list available events",
3586 parse_events_option);
3587 err = parse_events_option(&o, value, 0);
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03003588 } else if (!strcmp(var, "trace.show_timestamp")) {
3589 trace->show_tstamp = perf_config_bool(var, value);
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03003590 } else if (!strcmp(var, "trace.show_duration")) {
3591 trace->show_duration = perf_config_bool(var, value);
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003592 } else if (!strcmp(var, "trace.show_arg_names")) {
3593 trace->show_arg_names = perf_config_bool(var, value);
3594 if (!trace->show_arg_names)
3595 trace->show_zeros = true;
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03003596 } else if (!strcmp(var, "trace.show_zeros")) {
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003597 bool new_show_zeros = perf_config_bool(var, value);
3598 if (!trace->show_arg_names && !new_show_zeros) {
3599 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
3600 goto out;
3601 }
3602 trace->show_zeros = new_show_zeros;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -03003603 } else if (!strcmp(var, "trace.show_prefix")) {
3604 trace->show_string_prefix = perf_config_bool(var, value);
Arnaldo Carvalho de Melod32de872018-12-14 11:06:16 -03003605 } else if (!strcmp(var, "trace.no_inherit")) {
3606 trace->opts.no_inherit = perf_config_bool(var, value);
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -03003607 } else if (!strcmp(var, "trace.args_alignment")) {
3608 int args_alignment = 0;
3609 if (perf_config_int(&args_alignment, var, value) == 0)
3610 trace->args_alignment = args_alignment;
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003611 }
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003612out:
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003613 return err;
3614}
3615
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003616int cmd_trace(int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003617{
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003618 const char *trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +09003619 "perf trace [<options>] [<command>]",
3620 "perf trace [<options>] -- <command> [<options>]",
David Ahern5e2485b2013-09-28 13:13:01 -06003621 "perf trace record [<options>] [<command>]",
3622 "perf trace record [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003623 NULL
3624 };
3625 struct trace trace = {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003626 .syscalls = {
3627 . max = -1,
3628 },
3629 .opts = {
3630 .target = {
3631 .uid = UINT_MAX,
3632 .uses_mmap = true,
3633 },
3634 .user_freq = UINT_MAX,
3635 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03003636 .no_buffering = true,
Arnaldo Carvalho de Melo38d54472014-12-12 17:28:32 -03003637 .mmap_pages = UINT_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003638 },
Milian Wolff007d66a2015-08-05 16:52:23 -03003639 .output = stderr,
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003640 .show_comm = true,
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03003641 .show_tstamp = true,
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03003642 .show_duration = true,
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003643 .show_arg_names = true,
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -03003644 .args_alignment = 70,
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003645 .trace_syscalls = false,
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003646 .kernel_syscallchains = false,
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003647 .max_stack = UINT_MAX,
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003648 .max_events = ULONG_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003649 };
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003650 const char *output_name = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003651 const struct option trace_options[] = {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003652 OPT_CALLBACK('e', "event", &trace, "event",
3653 "event/syscall selector. use 'perf list' to list available events",
3654 trace__parse_events_option),
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003655 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3656 "show the thread COMM next to its id"),
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003657 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003658 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3659 trace__parse_events_option),
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003660 OPT_STRING('o', "output", &output_name, "file", "output file name"),
David Ahern6810fc92013-08-28 22:29:52 -06003661 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003662 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3663 "trace events on existing process id"),
David Ahernac9be8e2013-08-20 11:15:45 -06003664 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003665 "trace events on existing thread id"),
Arnaldo Carvalho de Melofa0e4ff2015-04-23 11:59:20 -03003666 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003667 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
David Ahernac9be8e2013-08-20 11:15:45 -06003668 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003669 "system-wide collection from all CPUs"),
David Ahernac9be8e2013-08-20 11:15:45 -06003670 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003671 "list of cpus to monitor"),
David Ahern6810fc92013-08-28 22:29:52 -06003672 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003673 "child tasks do not inherit counters"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02003674 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3675 "number of mmap data pages",
3676 perf_evlist__parse_mmap_pages),
David Ahernac9be8e2013-08-20 11:15:45 -06003677 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003678 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003679 OPT_CALLBACK(0, "duration", &trace, "float",
3680 "show only events with duration > N.M ms",
3681 trace__set_duration),
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003682 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03003683 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
David Ahern4bb09192013-09-04 12:37:43 -06003684 OPT_BOOLEAN('T', "time", &trace.full_time,
3685 "Show full timestamp, not time relative to first start"),
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03003686 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3687 "Show only syscalls that failed"),
David Ahernfd2eaba2013-11-12 09:31:15 -07003688 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3689 "Show only syscall summary with statistics"),
3690 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3691 "Show all syscalls and summary with statistics"),
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003692 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3693 "Trace pagefaults", parse_pagefaults, "maj"),
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003694 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
Yunlong Songe366a6d2015-04-02 21:47:18 +08003695 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
Milian Wolff566a0882016-04-08 13:34:15 +02003696 OPT_CALLBACK(0, "call-graph", &trace.opts,
3697 "record_mode[,record_size]", record_callchain_help,
3698 &record_parse_callchain_opt),
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003699 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3700 "Show the kernel callchains on the syscall exit path"),
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003701 OPT_ULONG(0, "max-events", &trace.max_events,
3702 "Set the maximum number of events to print, exit after that is reached. "),
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03003703 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3704 "Set the minimum stack depth when parsing the callchain, "
3705 "anything below the specified depth will be ignored."),
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -03003706 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3707 "Set the maximum stack depth when parsing the callchain, "
3708 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003709 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003710 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
3711 "Sort batch of events before processing, use if getting out of order events"),
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03003712 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3713 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08003714 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04003715 "per thread proc mmap processing timeout in ms"),
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003716 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3717 trace__parse_cgroups),
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003718 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3719 "ms to wait before starting measurement after program "
3720 "start"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003721 OPT_END()
3722 };
Arnaldo Carvalho de Meloccd62a82016-04-16 09:36:32 -03003723 bool __maybe_unused max_stack_user_set = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003724 bool mmap_pages_user_set = true;
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003725 struct perf_evsel *evsel;
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003726 const char * const trace_subcommands[] = { "record", NULL };
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003727 int err = -1;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003728 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003729
Arnaldo Carvalho de Melo4d08cb82015-02-24 15:35:55 -03003730 signal(SIGSEGV, sighandler_dump_stack);
3731 signal(SIGFPE, sighandler_dump_stack);
3732
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003733 trace.evlist = perf_evlist__new();
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003734 trace.sctbl = syscalltbl__new();
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003735
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003736 if (trace.evlist == NULL || trace.sctbl == NULL) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003737 pr_err("Not enough memory to run!\n");
He Kuangff8f6952015-05-11 12:28:36 +00003738 err = -ENOMEM;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003739 goto out;
3740 }
3741
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003742 err = perf_config(trace__config, &trace);
3743 if (err)
3744 goto out;
3745
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003746 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3747 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
David Ahernfd2eaba2013-11-12 09:31:15 -07003748
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003749 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3750 usage_with_options_msg(trace_usage, trace_options,
3751 "cgroup monitoring only available in system-wide mode");
3752 }
3753
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003754 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3755 if (IS_ERR(evsel)) {
3756 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
Arnaldo Carvalho de Meloe0b6d2e2018-08-07 15:40:13 -03003757 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3758 goto out;
3759 }
3760
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003761 if (evsel) {
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003762 trace.syscalls.events.augmented = evsel;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003763 trace__set_bpf_map_filtered_pids(&trace);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003764 trace__set_bpf_map_syscalls(&trace);
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003765 }
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003766
Wang Nand7888572016-04-08 15:07:24 +00003767 err = bpf__setup_stdout(trace.evlist);
3768 if (err) {
3769 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3770 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3771 goto out;
3772 }
3773
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003774 err = -1;
3775
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003776 if (trace.trace_pgfaults) {
3777 trace.opts.sample_address = true;
3778 trace.opts.sample_time = true;
3779 }
3780
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003781 if (trace.opts.mmap_pages == UINT_MAX)
3782 mmap_pages_user_set = false;
3783
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003784 if (trace.max_stack == UINT_MAX) {
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03003785 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003786 max_stack_user_set = false;
3787 }
3788
3789#ifdef HAVE_DWARF_UNWIND_SUPPORT
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003790 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003791 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003792 }
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003793#endif
3794
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03003795 if (callchain_param.enabled) {
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003796 if (!mmap_pages_user_set && geteuid() == 0)
3797 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3798
Milian Wolff566a0882016-04-08 13:34:15 +02003799 symbol_conf.use_callchain = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003800 }
Milian Wolff566a0882016-04-08 13:34:15 +02003801
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003802 if (trace.evlist->nr_entries > 0) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003803 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003804 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3805 perror("failed to set syscalls:* tracepoint fields");
3806 goto out;
3807 }
3808 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003809
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003810 if (trace.sort_events) {
3811 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
3812 ordered_events__set_copy_on_queue(&trace.oe.data, true);
3813 }
Jiri Olsa028713a2018-12-05 17:05:09 +01003814
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003815 /*
3816 * If we are augmenting syscalls, then combine what we put in the
3817 * __augmented_syscalls__ BPF map with what is in the
3818 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3819 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3820 *
3821 * We'll switch to look at two BPF maps, one for sys_enter and the
3822 * other for sys_exit when we start augmenting the sys_exit paths with
3823 * buffers that are being copied from kernel to userspace, think 'read'
3824 * syscall.
3825 */
3826 if (trace.syscalls.events.augmented) {
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003827 evlist__for_each_entry(trace.evlist, evsel) {
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003828 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3829
3830 if (raw_syscalls_sys_exit) {
3831 trace.raw_augmented_syscalls = true;
3832 goto init_augmented_syscall_tp;
3833 }
3834
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03003835 if (strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_enter") == 0) {
3836 struct perf_evsel *augmented = trace.syscalls.events.augmented;
3837 if (perf_evsel__init_augmented_syscall_tp(augmented, evsel) ||
3838 perf_evsel__init_augmented_syscall_tp_args(augmented))
3839 goto out;
3840 augmented->handler = trace__sys_enter;
3841 }
3842
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003843 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03003844 struct syscall_tp *sc;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003845init_augmented_syscall_tp:
Arnaldo Carvalho de Melob9b6a2e2018-12-19 18:54:36 -03003846 if (perf_evsel__init_augmented_syscall_tp(evsel, evsel))
3847 goto out;
3848 sc = evsel->priv;
3849 /*
3850 * For now with BPF raw_augmented we hook into
3851 * raw_syscalls:sys_enter and there we get all
3852 * 6 syscall args plus the tracepoint common
3853 * fields and the syscall_nr (another long).
3854 * So we check if that is the case and if so
3855 * don't look after the sc->args_size but
3856 * always after the full raw_syscalls:sys_enter
3857 * payload, which is fixed.
3858 *
3859 * We'll revisit this later to pass
3860 * s->args_size to the BPF augmenter (now
3861 * tools/perf/examples/bpf/augmented_raw_syscalls.c,
3862 * so that it copies only what we need for each
3863 * syscall, like what happens when we use
3864 * syscalls:sys_enter_NAME, so that we reduce
3865 * the kernel/userspace traffic to just what is
3866 * needed for each syscall.
3867 */
3868 if (trace.raw_augmented_syscalls)
3869 trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset;
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003870 perf_evsel__init_augmented_syscall_tp_ret(evsel);
3871 evsel->handler = trace__sys_exit;
3872 }
3873 }
3874 }
3875
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003876 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3877 return trace__record(&trace, argc-1, &argv[1]);
3878
3879 /* summary_only implies summary option, but don't overwrite summary if set */
3880 if (trace.summary_only)
3881 trace.summary = trace.summary_only;
3882
Arnaldo Carvalho de Melo726f3232015-02-06 10:16:45 +01003883 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3884 trace.evlist->nr_entries == 0 /* Was --events used? */) {
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003885 trace.trace_syscalls = true;
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003886 }
3887
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003888 if (output_name != NULL) {
3889 err = trace__open_output(&trace, output_name);
3890 if (err < 0) {
3891 perror("failed to create output file");
3892 goto out;
3893 }
3894 }
3895
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003896 err = target__validate(&trace.opts.target);
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003897 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003898 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003899 fprintf(trace.output, "%s", bf);
3900 goto out_close;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003901 }
3902
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003903 err = target__parse_uid(&trace.opts.target);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003904 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003905 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003906 fprintf(trace.output, "%s", bf);
3907 goto out_close;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003908 }
3909
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003910 if (!argc && target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +09003911 trace.opts.target.system_wide = true;
3912
David Ahern6810fc92013-08-28 22:29:52 -06003913 if (input_name)
3914 err = trace__replay(&trace);
3915 else
3916 err = trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003917
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003918out_close:
3919 if (output_name != NULL)
3920 fclose(trace.output);
3921out:
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003922 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003923}