blob: 6689c1a114fec3d99c7a6507c272a063d435e1f3 [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 Meloa761a8d2018-12-10 16:27:33 -0300115 bool sort_events;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -0300116 bool raw_augmented_syscalls;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300117 bool not_ev_qualifier;
118 bool live;
119 bool full_time;
120 bool sched;
121 bool multiple_threads;
122 bool summary;
123 bool summary_only;
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -0300124 bool failure_only;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300125 bool show_comm;
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -0300126 bool print_sample;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300127 bool show_tool_stats;
128 bool trace_syscalls;
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -0300129 bool kernel_syscallchains;
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -0300130 s16 args_alignment;
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -0300131 bool show_tstamp;
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -0300132 bool show_duration;
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -0300133 bool show_zeros;
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -0300134 bool show_arg_names;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300135 bool show_string_prefix;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300136 bool force;
137 bool vfs_getname;
138 int trace_pgfaults;
Jiri Olsa028713a2018-12-05 17:05:09 +0100139 struct {
140 struct ordered_events data;
141 u64 last;
142 } oe;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300143};
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300144
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300145struct tp_field {
146 int offset;
147 union {
148 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
149 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
150 };
151};
152
153#define TP_UINT_FIELD(bits) \
154static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
155{ \
David Ahern55d43bc2015-02-19 15:00:22 -0500156 u##bits value; \
157 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
158 return value; \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300159}
160
161TP_UINT_FIELD(8);
162TP_UINT_FIELD(16);
163TP_UINT_FIELD(32);
164TP_UINT_FIELD(64);
165
166#define TP_UINT_FIELD__SWAPPED(bits) \
167static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
168{ \
David Ahern55d43bc2015-02-19 15:00:22 -0500169 u##bits value; \
170 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300171 return bswap_##bits(value);\
172}
173
174TP_UINT_FIELD__SWAPPED(16);
175TP_UINT_FIELD__SWAPPED(32);
176TP_UINT_FIELD__SWAPPED(64);
177
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300178static 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 -0300179{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300180 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300181
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300182 switch (size) {
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300183 case 1:
184 field->integer = tp_field__u8;
185 break;
186 case 2:
187 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
188 break;
189 case 4:
190 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
191 break;
192 case 8:
193 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
194 break;
195 default:
196 return -1;
197 }
198
199 return 0;
200}
201
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400202static 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 -0300203{
204 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
205}
206
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300207static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
208{
209 return sample->raw_data + field->offset;
210}
211
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300212static int __tp_field__init_ptr(struct tp_field *field, int offset)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300213{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300214 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300215 field->pointer = tp_field__ptr;
216 return 0;
217}
218
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400219static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300220{
221 return __tp_field__init_ptr(field, format_field->offset);
222}
223
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300224struct syscall_tp {
225 struct tp_field id;
226 union {
227 struct tp_field args, ret;
228 };
229};
230
231static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
232 struct tp_field *field,
233 const char *name)
234{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400235 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300236
237 if (format_field == NULL)
238 return -1;
239
240 return tp_field__init_uint(field, format_field, evsel->needs_swap);
241}
242
243#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
244 ({ struct syscall_tp *sc = evsel->priv;\
245 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
246
247static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
248 struct tp_field *field,
249 const char *name)
250{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400251 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300252
253 if (format_field == NULL)
254 return -1;
255
256 return tp_field__init_ptr(field, format_field);
257}
258
259#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
260 ({ struct syscall_tp *sc = evsel->priv;\
261 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
262
263static void perf_evsel__delete_priv(struct perf_evsel *evsel)
264{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300265 zfree(&evsel->priv);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300266 perf_evsel__delete(evsel);
267}
268
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300269static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
270{
271 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
272
273 if (evsel->priv != NULL) {
Arnaldo Carvalho de Melo42da4382018-12-05 13:08:11 -0300274 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
275 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300276 goto out_delete;
277 return 0;
278 }
279
280 return -ENOMEM;
281out_delete:
282 zfree(&evsel->priv);
283 return -ENOENT;
284}
285
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300286static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel)
287{
288 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
289
290 if (evsel->priv != NULL) { /* field, sizeof_field, offsetof_field */
291 if (__tp_field__init_uint(&sc->id, sizeof(long), sizeof(long long), evsel->needs_swap))
292 goto out_delete;
293
294 return 0;
295 }
296
297 return -ENOMEM;
298out_delete:
299 zfree(&evsel->priv);
300 return -EINVAL;
301}
302
303static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
304{
305 struct syscall_tp *sc = evsel->priv;
306
307 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
308}
309
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -0300310static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
311{
312 struct syscall_tp *sc = evsel->priv;
313
314 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
315}
316
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300317static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
Namhyung Kim96695d42013-11-12 08:51:45 -0300318{
319 evsel->priv = malloc(sizeof(struct syscall_tp));
320 if (evsel->priv != NULL) {
321 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
322 goto out_delete;
323
324 evsel->handler = handler;
325 return 0;
326 }
327
328 return -ENOMEM;
329
330out_delete:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300331 zfree(&evsel->priv);
Namhyung Kim96695d42013-11-12 08:51:45 -0300332 return -ENOENT;
333}
334
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300335static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300336{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300337 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300338
David Ahern9aca7f12013-12-04 19:41:39 -0700339 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200340 if (IS_ERR(evsel))
David Ahern9aca7f12013-12-04 19:41:39 -0700341 evsel = perf_evsel__newtp("syscalls", direction);
342
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200343 if (IS_ERR(evsel))
344 return NULL;
345
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300346 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200347 goto out_delete;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300348
349 return evsel;
350
351out_delete:
352 perf_evsel__delete_priv(evsel);
353 return NULL;
354}
355
356#define perf_evsel__sc_tp_uint(evsel, name, sample) \
357 ({ struct syscall_tp *fields = evsel->priv; \
358 fields->name.integer(&fields->name, sample); })
359
360#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
361 ({ struct syscall_tp *fields = evsel->priv; \
362 fields->name.pointer(&fields->name, sample); })
363
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300364size_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 -0300365{
366 int idx = val - sa->offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300367
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300368 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
369 size_t printed = scnprintf(bf, size, intfmt, val);
370 if (show_prefix)
371 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
372 return printed;
373 }
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300374
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300375 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300376}
377
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300378static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
379 const char *intfmt,
380 struct syscall_arg *arg)
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300381{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300382 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300383}
384
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300385static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
386 struct syscall_arg *arg)
387{
388 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
389}
390
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300391#define SCA_STRARRAY syscall_arg__scnprintf_strarray
392
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300393size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
394{
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300395 size_t printed;
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300396 int i;
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300397
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300398 for (i = 0; i < sas->nr_entries; ++i) {
399 struct strarray *sa = sas->entries[i];
400 int idx = val - sa->offset;
401
402 if (idx >= 0 && idx < sa->nr_entries) {
403 if (sa->entries[idx] == NULL)
404 break;
405 return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
406 }
407 }
408
Arnaldo Carvalho de Melo9614b8d2018-12-18 10:54:19 -0300409 printed = scnprintf(bf, size, intfmt, val);
410 if (show_prefix)
411 printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
412 return printed;
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300413}
414
Arnaldo Carvalho de Melo274e86f2017-07-14 09:38:38 -0300415size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
416 struct syscall_arg *arg)
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300417{
Arnaldo Carvalho de Melo1f2d0852018-12-18 10:25:28 -0300418 return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300419}
420
Arnaldo Carvalho de Melo48e1f912016-07-05 14:43:27 -0300421#ifndef AT_FDCWD
422#define AT_FDCWD -100
423#endif
424
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300425static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
426 struct syscall_arg *arg)
427{
428 int fd = arg->val;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300429 const char *prefix = "AT_FD";
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300430
431 if (fd == AT_FDCWD)
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300432 return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300433
434 return syscall_arg__scnprintf_fd(bf, size, arg);
435}
436
437#define SCA_FDAT syscall_arg__scnprintf_fd_at
438
439static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
440 struct syscall_arg *arg);
441
442#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
443
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300444size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300445{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300446 return scnprintf(bf, size, "%#lx", arg->val);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300447}
448
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300449size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
450{
451 if (arg->val == 0)
452 return scnprintf(bf, size, "NULL");
453 return syscall_arg__scnprintf_hex(bf, size, arg);
454}
455
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300456size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300457{
458 return scnprintf(bf, size, "%d", arg->val);
459}
460
Arnaldo Carvalho de Melo5dde91e2017-07-14 10:34:16 -0300461size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
462{
463 return scnprintf(bf, size, "%ld", arg->val);
464}
465
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300466static const char *bpf_cmd[] = {
467 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
468 "MAP_GET_NEXT_KEY", "PROG_LOAD",
469};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300470static DEFINE_STRARRAY(bpf_cmd, "BPF_");
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300471
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300472static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300473static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
Arnaldo Carvalho de Meloeac032c2013-09-20 11:27:32 -0300474
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300475static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300476static DEFINE_STRARRAY(itimers, "ITIMER_");
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300477
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300478static const char *keyctl_options[] = {
479 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
480 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
481 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
482 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
483 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
484};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300485static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300486
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300487static const char *whences[] = { "SET", "CUR", "END",
488#ifdef SEEK_DATA
489"DATA",
490#endif
491#ifdef SEEK_HOLE
492"HOLE",
493#endif
494};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300495static DEFINE_STRARRAY(whences, "SEEK_");
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300496
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300497static const char *fcntl_cmds[] = {
498 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
Arnaldo Carvalho de Meloe000e5e2017-07-13 13:07:00 -0300499 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
500 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
501 "GETOWNER_UIDS",
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300502};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300503static DEFINE_STRARRAY(fcntl_cmds, "F_");
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300504
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300505static const char *fcntl_linux_specific_cmds[] = {
506 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
507 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
Arnaldo Carvalho de Melo64e45612017-07-13 17:34:46 -0300508 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300509};
510
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300511static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300512
513static struct strarray *fcntl_cmds_arrays[] = {
514 &strarray__fcntl_cmds,
515 &strarray__fcntl_linux_specific_cmds,
516};
517
518static DEFINE_STRARRAYS(fcntl_cmds_arrays);
519
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300520static const char *rlimit_resources[] = {
521 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
522 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
523 "RTTIME",
524};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300525static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300526
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300527static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300528static DEFINE_STRARRAY(sighow, "SIG_");
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300529
David Ahern4f8c1b72013-09-22 19:45:00 -0600530static const char *clockid[] = {
531 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
Arnaldo Carvalho de Melo28ebb872015-08-11 10:38:38 -0300532 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
533 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
David Ahern4f8c1b72013-09-22 19:45:00 -0600534};
Arnaldo Carvalho de Melo2e3d7fa2018-12-14 15:08:40 -0300535static DEFINE_STRARRAY(clockid, "CLOCK_");
David Ahern4f8c1b72013-09-22 19:45:00 -0600536
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300537static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
538 struct syscall_arg *arg)
539{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300540 bool show_prefix = arg->show_string_prefix;
541 const char *suffix = "_OK";
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300542 size_t printed = 0;
543 int mode = arg->val;
544
545 if (mode == F_OK) /* 0 */
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300546 return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300547#define P_MODE(n) \
548 if (mode & n##_OK) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300549 printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300550 mode &= ~n##_OK; \
551 }
552
553 P_MODE(R);
554 P_MODE(W);
555 P_MODE(X);
556#undef P_MODE
557
558 if (mode)
559 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
560
561 return printed;
562}
563
564#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
565
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300566static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
567 struct syscall_arg *arg);
568
569#define SCA_FILENAME syscall_arg__scnprintf_filename
570
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300571static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
572 struct syscall_arg *arg)
573{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300574 bool show_prefix = arg->show_string_prefix;
575 const char *prefix = "O_";
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300576 int printed = 0, flags = arg->val;
577
578#define P_FLAG(n) \
579 if (flags & O_##n) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300580 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300581 flags &= ~O_##n; \
582 }
583
584 P_FLAG(CLOEXEC);
585 P_FLAG(NONBLOCK);
586#undef P_FLAG
587
588 if (flags)
589 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
590
591 return printed;
592}
593
594#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
595
Arnaldo Carvalho de Meloa355a612016-04-13 11:55:18 -0300596#ifndef GRND_NONBLOCK
597#define GRND_NONBLOCK 0x0001
598#endif
599#ifndef GRND_RANDOM
600#define GRND_RANDOM 0x0002
601#endif
602
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300603static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
604 struct syscall_arg *arg)
605{
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300606 bool show_prefix = arg->show_string_prefix;
607 const char *prefix = "GRND_";
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300608 int printed = 0, flags = arg->val;
609
610#define P_FLAG(n) \
611 if (flags & GRND_##n) { \
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -0300612 printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300613 flags &= ~GRND_##n; \
614 }
615
616 P_FLAG(RANDOM);
617 P_FLAG(NONBLOCK);
618#undef P_FLAG
619
620 if (flags)
621 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
622
623 return printed;
624}
625
626#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
627
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300628#define STRARRAY(name, array) \
629 { .scnprintf = SCA_STRARRAY, \
630 .parm = &strarray__##array, }
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -0300631
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +0100632#include "trace/beauty/arch_errno_names.c"
Arnaldo Carvalho de Meloea8dc3c2016-04-13 12:10:19 -0300633#include "trace/beauty/eventfd.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300634#include "trace/beauty/futex_op.c"
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300635#include "trace/beauty/futex_val3.c"
Arnaldo Carvalho de Melodf4cb162016-04-13 12:05:44 -0300636#include "trace/beauty/mmap.c"
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -0300637#include "trace/beauty/mode_t.c"
Arnaldo Carvalho de Meloa30e6252016-04-27 19:01:52 -0300638#include "trace/beauty/msg_flags.c"
Arnaldo Carvalho de Melo8f48df62016-05-06 10:02:32 -0300639#include "trace/beauty/open_flags.c"
Arnaldo Carvalho de Melo62de3442016-04-26 11:03:03 -0300640#include "trace/beauty/perf_event_open.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300641#include "trace/beauty/pid.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300642#include "trace/beauty/sched_policy.c"
Arnaldo Carvalho de Melof5cd95e2016-05-11 10:32:20 -0300643#include "trace/beauty/seccomp.c"
Arnaldo Carvalho de Melo12199d82016-05-06 09:58:02 -0300644#include "trace/beauty/signum.c"
Arnaldo Carvalho de Melobbf86c42016-04-14 13:53:10 -0300645#include "trace/beauty/socket_type.c"
Arnaldo Carvalho de Melo7206b902016-04-06 14:11:36 -0300646#include "trace/beauty/waitid_options.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300647
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300648struct syscall_arg_fmt {
649 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -0300650 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300651 void *parm;
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -0300652 const char *name;
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -0300653 bool show_zero;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300654};
655
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300656static struct syscall_fmt {
657 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -0300658 const char *alias;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300659 struct syscall_arg_fmt arg[6];
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -0300660 u8 nr_args;
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300661 bool errpid;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300662 bool timeout;
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -0300663 bool hexret;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300664} syscall_fmts[] = {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300665 { .name = "access",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300666 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
Arnaldo Carvalho de Melofb7068e2018-12-18 11:05:56 -0300667 { .name = "arch_prctl",
668 .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
669 [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
Arnaldo Carvalho de Melo02ef2882018-08-28 17:03:53 -0300670 { .name = "bind",
671 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300672 { .name = "bpf",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300673 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300674 { .name = "brk", .hexret = true,
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300675 .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300676 { .name = "clock_gettime",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300677 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
Arnaldo Carvalho de Melo33396a32017-07-19 16:15:17 -0300678 { .name = "clone", .errpid = true, .nr_args = 5,
679 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
680 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
681 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
682 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
683 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300684 { .name = "close",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300685 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
Arnaldo Carvalho de Melod5a7e662018-08-28 16:24:44 -0300686 { .name = "connect",
687 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300688 { .name = "epoll_ctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300689 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300690 { .name = "eventfd2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300691 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300692 { .name = "fchmodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300693 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300694 { .name = "fchownat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300695 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300696 { .name = "fcntl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300697 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
Arnaldo Carvalho de Melo39cc3552017-07-17 16:02:52 -0300698 .parm = &strarrays__fcntl_cmds_arrays,
699 .show_zero = true, },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300700 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300701 { .name = "flock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300702 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300703 { .name = "fstat", .alias = "newfstat", },
704 { .name = "fstatat", .alias = "newfstatat", },
705 { .name = "futex",
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300706 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
707 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300708 { .name = "futimesat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300709 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300710 { .name = "getitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300711 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300712 { .name = "getpid", .errpid = true, },
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300713 { .name = "getpgid", .errpid = true, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300714 { .name = "getppid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300715 { .name = "getrandom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300716 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300717 { .name = "getrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300718 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo2d1073d2018-01-09 12:03:47 -0300719 { .name = "gettid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300720 { .name = "ioctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300721 .arg = {
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300722#if defined(__i386__) || defined(__x86_64__)
723/*
724 * FIXME: Make this available to all arches.
725 */
Arnaldo Carvalho de Melo1cc47f22017-07-31 13:20:14 -0300726 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300727 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300728#else
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300729 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300730#endif
Arnaldo Carvalho de Melo1de30382017-10-31 11:32:23 -0300731 { .name = "kcmp", .nr_args = 5,
732 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
733 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
734 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
735 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
736 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300737 { .name = "keyctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300738 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300739 { .name = "kill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300740 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300741 { .name = "linkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300742 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300743 { .name = "lseek",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300744 .arg = { [2] = STRARRAY(whence, whences), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300745 { .name = "lstat", .alias = "newlstat", },
746 { .name = "madvise",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300747 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
748 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300749 { .name = "mkdirat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300750 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300751 { .name = "mknodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300752 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300753 { .name = "mmap", .hexret = true,
Jiri Olsa54265662017-05-31 13:35:57 +0200754/* The standard mmap maps to old_mmap on s390x */
755#if defined(__s390x__)
756 .alias = "old_mmap",
757#endif
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300758 .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
Arnaldo Carvalho de Meloa6631342018-12-18 11:25:08 -0300759 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ },
760 [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300761 { .name = "mount",
Arnaldo Carvalho de Melo23c07a22018-10-26 13:51:45 -0300762 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
763 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300764 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300765 { .name = "mprotect",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300766 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
767 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300768 { .name = "mq_unlink",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300769 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300770 { .name = "mremap", .hexret = true,
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -0300771 .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300772 { .name = "name_to_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300773 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300774 { .name = "newfstatat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300775 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300776 { .name = "open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300777 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300778 { .name = "open_by_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300779 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
780 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300781 { .name = "openat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300782 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
783 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300784 { .name = "perf_event_open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300785 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
786 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
787 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300788 { .name = "pipe2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300789 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo83bc9c372017-08-28 11:47:11 -0300790 { .name = "pkey_alloc",
791 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
792 { .name = "pkey_free",
793 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
794 { .name = "pkey_mprotect",
795 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
796 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
797 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300798 { .name = "poll", .timeout = true, },
799 { .name = "ppoll", .timeout = true, },
Arnaldo Carvalho de Melofb7068e2018-12-18 11:05:56 -0300800 { .name = "prctl",
Arnaldo Carvalho de Melod688d032017-10-26 15:19:35 -0300801 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
802 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
803 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300804 { .name = "pread", .alias = "pread64", },
805 { .name = "preadv", .alias = "pread", },
806 { .name = "prlimit64",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300807 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300808 { .name = "pwrite", .alias = "pwrite64", },
809 { .name = "readlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300810 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300811 { .name = "recvfrom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300812 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300813 { .name = "recvmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300814 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300815 { .name = "recvmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300816 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300817 { .name = "renameat",
Arnaldo Carvalho de Melo436651c2018-12-11 11:19:23 -0300818 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
819 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
Arnaldo Carvalho de Melo84a83542018-12-11 09:53:21 -0300820 { .name = "renameat2",
821 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
Arnaldo Carvalho de Meloca7ff2c2018-12-11 10:39:30 -0300822 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
823 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300824 { .name = "rt_sigaction",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300825 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300826 { .name = "rt_sigprocmask",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300827 .arg = { [0] = STRARRAY(how, sighow), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300828 { .name = "rt_sigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300829 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300830 { .name = "rt_tgsigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300831 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300832 { .name = "sched_setscheduler",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300833 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300834 { .name = "seccomp",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300835 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
836 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300837 { .name = "select", .timeout = true, },
838 { .name = "sendmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300839 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300840 { .name = "sendmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300841 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300842 { .name = "sendto",
Arnaldo Carvalho de Melo6ebb6862018-08-29 08:47:08 -0300843 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
844 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300845 { .name = "set_tid_address", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300846 { .name = "setitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300847 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300848 { .name = "setrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300849 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300850 { .name = "socket",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300851 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300852 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
853 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300854 { .name = "socketpair",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300855 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300856 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
857 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300858 { .name = "stat", .alias = "newstat", },
859 { .name = "statx",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300860 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
861 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
862 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300863 { .name = "swapoff",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300864 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300865 { .name = "swapon",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300866 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300867 { .name = "symlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300868 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300869 { .name = "tgkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300870 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300871 { .name = "tkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300872 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo476c92ca2018-10-26 13:23:25 -0300873 { .name = "umount2", .alias = "umount",
874 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300875 { .name = "uname", .alias = "newuname", },
876 { .name = "unlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300877 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300878 { .name = "utimensat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300879 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300880 { .name = "wait4", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300881 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300882 { .name = "waitid", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300883 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300884};
885
886static int syscall_fmt__cmp(const void *name, const void *fmtp)
887{
888 const struct syscall_fmt *fmt = fmtp;
889 return strcmp(name, fmt->name);
890}
891
892static struct syscall_fmt *syscall_fmt__find(const char *name)
893{
894 const int nmemb = ARRAY_SIZE(syscall_fmts);
895 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
896}
897
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -0300898static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
899{
900 int i, nmemb = ARRAY_SIZE(syscall_fmts);
901
902 for (i = 0; i < nmemb; ++i) {
903 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
904 return &syscall_fmts[i];
905 }
906
907 return NULL;
908}
909
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300910/*
911 * is_exit: is this "exit" or "exit_group"?
912 * 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 -0300913 * 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 -0300914 */
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300915struct syscall {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500916 struct tep_event *tp_format;
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -0300917 int nr_args;
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -0300918 int args_size;
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300919 bool is_exit;
920 bool is_open;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400921 struct tep_format_field *args;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300922 const char *name;
923 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300924 struct syscall_arg_fmt *arg_fmt;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300925};
926
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -0300927struct bpf_map_syscall_entry {
928 bool enabled;
929};
930
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300931/*
932 * We need to have this 'calculated' boolean because in some cases we really
933 * don't know what is the duration of a syscall, for instance, when we start
934 * a session and some threads are waiting for a syscall to finish, say 'poll',
935 * in which case all we can do is to print "( ? ) for duration and for the
936 * start timestamp.
937 */
938static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200939{
940 double duration = (double)t / NSEC_PER_MSEC;
941 size_t printed = fprintf(fp, "(");
942
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300943 if (!calculated)
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -0300944 printed += fprintf(fp, " ");
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300945 else if (duration >= 1.0)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200946 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
947 else if (duration >= 0.01)
948 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
949 else
950 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300951 return printed + fprintf(fp, "): ");
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200952}
953
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300954/**
955 * filename.ptr: The filename char pointer that will be vfs_getname'd
956 * filename.entry_str_pos: Where to insert the string translated from
957 * filename.ptr by the vfs_getname tracepoint/kprobe.
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300958 * ret_scnprintf: syscall args may set this to a different syscall return
959 * formatter, for instance, fcntl may return fds, file flags, etc.
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300960 */
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300961struct thread_trace {
962 u64 entry_time;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300963 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300964 unsigned long nr_events;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +0400965 unsigned long pfmaj, pfmin;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300966 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300967 double runtime_ms;
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -0300968 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300969 struct {
970 unsigned long ptr;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -0300971 short int entry_str_pos;
972 bool pending_open;
973 unsigned int namelen;
974 char *name;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300975 } filename;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300976 struct {
977 int max;
978 char **table;
979 } paths;
David Ahernbf2575c2013-10-08 21:26:53 -0600980
981 struct intlist *syscall_stats;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300982};
983
984static struct thread_trace *thread_trace__new(void)
985{
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300986 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
987
988 if (ttrace)
989 ttrace->paths.max = -1;
990
David Ahernbf2575c2013-10-08 21:26:53 -0600991 ttrace->syscall_stats = intlist__new(NULL);
992
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300993 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300994}
995
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300996static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300997{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300998 struct thread_trace *ttrace;
999
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001000 if (thread == NULL)
1001 goto fail;
1002
Namhyung Kim89dceb22014-10-06 09:46:03 +09001003 if (thread__priv(thread) == NULL)
1004 thread__set_priv(thread, thread_trace__new());
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001005
Namhyung Kim89dceb22014-10-06 09:46:03 +09001006 if (thread__priv(thread) == NULL)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001007 goto fail;
1008
Namhyung Kim89dceb22014-10-06 09:46:03 +09001009 ttrace = thread__priv(thread);
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03001010 ++ttrace->nr_events;
1011
1012 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001013fail:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001014 color_fprintf(fp, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001015 "WARNING: not enough memory, dropping samples!\n");
1016 return NULL;
1017}
1018
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001019
1020void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001021 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001022{
1023 struct thread_trace *ttrace = thread__priv(arg->thread);
1024
1025 ttrace->ret_scnprintf = ret_scnprintf;
1026}
1027
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04001028#define TRACE_PFMAJ (1 << 0)
1029#define TRACE_PFMIN (1 << 1)
1030
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001031static const size_t trace__entry_str_size = 2048;
1032
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001033static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001034{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001035 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001036
1037 if (fd > ttrace->paths.max) {
1038 char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
1039
1040 if (npath == NULL)
1041 return -1;
1042
1043 if (ttrace->paths.max != -1) {
1044 memset(npath + ttrace->paths.max + 1, 0,
1045 (fd - ttrace->paths.max) * sizeof(char *));
1046 } else {
1047 memset(npath, 0, (fd + 1) * sizeof(char *));
1048 }
1049
1050 ttrace->paths.table = npath;
1051 ttrace->paths.max = fd;
1052 }
1053
1054 ttrace->paths.table[fd] = strdup(pathname);
1055
1056 return ttrace->paths.table[fd] != NULL ? 0 : -1;
1057}
1058
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001059static int thread__read_fd_path(struct thread *thread, int fd)
1060{
1061 char linkname[PATH_MAX], pathname[PATH_MAX];
1062 struct stat st;
1063 int ret;
1064
1065 if (thread->pid_ == thread->tid) {
1066 scnprintf(linkname, sizeof(linkname),
1067 "/proc/%d/fd/%d", thread->pid_, fd);
1068 } else {
1069 scnprintf(linkname, sizeof(linkname),
1070 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1071 }
1072
1073 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1074 return -1;
1075
1076 ret = readlink(linkname, pathname, sizeof(pathname));
1077
1078 if (ret < 0 || ret > st.st_size)
1079 return -1;
1080
1081 pathname[ret] = '\0';
1082 return trace__set_fd_pathname(thread, fd, pathname);
1083}
1084
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001085static const char *thread__fd_path(struct thread *thread, int fd,
1086 struct trace *trace)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001087{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001088 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001089
1090 if (ttrace == NULL)
1091 return NULL;
1092
1093 if (fd < 0)
1094 return NULL;
1095
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001096 if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001097 if (!trace->live)
1098 return NULL;
1099 ++trace->stats.proc_getname;
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001100 if (thread__read_fd_path(thread, fd))
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001101 return NULL;
1102 }
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001103
1104 return ttrace->paths.table[fd];
1105}
1106
Arnaldo Carvalho de Melofc65eb82017-07-14 15:21:40 -03001107size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001108{
1109 int fd = arg->val;
1110 size_t printed = scnprintf(bf, size, "%d", fd);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001111 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001112
1113 if (path)
1114 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1115
1116 return printed;
1117}
1118
Arnaldo Carvalho de Melo0a2f75402017-10-31 11:30:09 -03001119size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1120{
1121 size_t printed = scnprintf(bf, size, "%d", fd);
1122 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1123
1124 if (thread) {
1125 const char *path = thread__fd_path(thread, fd, trace);
1126
1127 if (path)
1128 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1129
1130 thread__put(thread);
1131 }
1132
1133 return printed;
1134}
1135
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001136static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1137 struct syscall_arg *arg)
1138{
1139 int fd = arg->val;
1140 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
Namhyung Kim89dceb22014-10-06 09:46:03 +09001141 struct thread_trace *ttrace = thread__priv(arg->thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001142
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001143 if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
1144 zfree(&ttrace->paths.table[fd]);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001145
1146 return printed;
1147}
1148
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001149static void thread__set_filename_pos(struct thread *thread, const char *bf,
1150 unsigned long ptr)
1151{
1152 struct thread_trace *ttrace = thread__priv(thread);
1153
1154 ttrace->filename.ptr = ptr;
1155 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1156}
1157
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001158static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1159{
1160 struct augmented_arg *augmented_arg = arg->augmented.args;
1161
Arnaldo Carvalho de Melo721f5322018-12-14 14:02:03 -03001162 return scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001163}
1164
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001165static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1166 struct syscall_arg *arg)
1167{
1168 unsigned long ptr = arg->val;
1169
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001170 if (arg->augmented.args)
1171 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1172
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001173 if (!arg->trace->vfs_getname)
1174 return scnprintf(bf, size, "%#x", ptr);
1175
1176 thread__set_filename_pos(arg->thread, bf, ptr);
1177 return 0;
1178}
1179
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001180static bool trace__filter_duration(struct trace *trace, double t)
1181{
1182 return t < (trace->duration_filter * NSEC_PER_MSEC);
1183}
1184
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001185static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001186{
1187 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1188
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001189 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001190}
1191
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001192/*
1193 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1194 * using ttrace->entry_time for a thread that receives a sys_exit without
1195 * first having received a sys_enter ("poll" issued before tracing session
1196 * starts, lost sys_enter exit due to ring buffer overflow).
1197 */
1198static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1199{
1200 if (tstamp > 0)
1201 return __trace__fprintf_tstamp(trace, tstamp, fp);
1202
1203 return fprintf(fp, " ? ");
1204}
1205
Namhyung Kimf15eb532012-10-05 14:02:16 +09001206static bool done = false;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001207static bool interrupted = false;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001208
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001209static void sig_handler(int sig)
Namhyung Kimf15eb532012-10-05 14:02:16 +09001210{
1211 done = true;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001212 interrupted = sig == SIGINT;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001213}
1214
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001215static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001216{
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001217 size_t printed = 0;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001218
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001219 if (trace->multiple_threads) {
1220 if (trace->show_comm)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02001221 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
Adrian Hunter38051232013-07-04 16:20:31 +03001222 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001223 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001224
1225 return printed;
1226}
1227
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001228static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1229 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1230{
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03001231 size_t printed = 0;
1232
1233 if (trace->show_tstamp)
1234 printed = trace__fprintf_tstamp(trace, tstamp, fp);
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03001235 if (trace->show_duration)
1236 printed += fprintf_duration(duration, duration_calculated, fp);
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001237 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1238}
1239
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001240static int trace__process_event(struct trace *trace, struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001241 union perf_event *event, struct perf_sample *sample)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001242{
1243 int ret = 0;
1244
1245 switch (event->header.type) {
1246 case PERF_RECORD_LOST:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001247 color_fprintf(trace->output, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001248 "LOST %" PRIu64 " events!\n", event->lost.lost);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001249 ret = machine__process_lost_event(machine, event, sample);
Arnaldo Carvalho de Melo3ed5ca22016-03-30 16:51:17 -03001250 break;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001251 default:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001252 ret = machine__process_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001253 break;
1254 }
1255
1256 return ret;
1257}
1258
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001259static int trace__tool_process(struct perf_tool *tool,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001260 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001261 struct perf_sample *sample,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001262 struct machine *machine)
1263{
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001264 struct trace *trace = container_of(tool, struct trace, tool);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001265 return trace__process_event(trace, machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001266}
1267
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -03001268static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1269{
1270 struct machine *machine = vmachine;
1271
1272 if (machine->kptr_restrict_warned)
1273 return NULL;
1274
1275 if (symbol_conf.kptr_restrict) {
1276 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1277 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1278 "Kernel samples will not be resolved.\n");
1279 machine->kptr_restrict_warned = true;
1280 return NULL;
1281 }
1282
1283 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1284}
1285
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001286static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1287{
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001288 int err = symbol__init(NULL);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001289
1290 if (err)
1291 return err;
1292
David Ahern8fb598e2013-09-28 13:13:00 -06001293 trace->host = machine__new_host();
1294 if (trace->host == NULL)
1295 return -ENOMEM;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001296
Andrei Vagincbd5c172017-11-07 16:22:46 -08001297 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1298 if (err < 0)
1299 goto out;
Arnaldo Carvalho de Melo706c3da2015-07-22 16:16:16 -03001300
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001301 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001302 evlist->threads, trace__tool_process, false,
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001303 1);
Andrei Vagincbd5c172017-11-07 16:22:46 -08001304out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001305 if (err)
1306 symbol__exit();
1307
1308 return err;
1309}
1310
Andrei Vagin33974a42017-11-07 16:22:45 -08001311static void trace__symbols__exit(struct trace *trace)
1312{
1313 machine__exit(trace->host);
1314 trace->host = NULL;
1315
1316 symbol__exit();
1317}
1318
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001319static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1320{
1321 int idx;
1322
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001323 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1324 nr_args = sc->fmt->nr_args;
1325
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001326 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1327 if (sc->arg_fmt == NULL)
1328 return -1;
1329
1330 for (idx = 0; idx < nr_args; ++idx) {
1331 if (sc->fmt)
1332 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1333 }
1334
1335 sc->nr_args = nr_args;
1336 return 0;
1337}
1338
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001339static int syscall__set_arg_fmts(struct syscall *sc)
1340{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001341 struct tep_format_field *field, *last_field = NULL;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001342 int idx = 0, len;
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001343
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001344 for (field = sc->args; field; field = field->next, ++idx) {
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001345 last_field = field;
1346
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001347 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1348 continue;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001349
1350 if (strcmp(field->type, "const char *") == 0 &&
Arnaldo Carvalho de Melo12f3ca42016-05-23 16:37:55 -03001351 (strcmp(field->name, "filename") == 0 ||
1352 strcmp(field->name, "path") == 0 ||
1353 strcmp(field->name, "pathname") == 0))
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001354 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
Arnaldo Carvalho de Meloce055392018-12-17 12:21:09 -03001355 else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
1356 sc->arg_fmt[idx].scnprintf = SCA_PTR;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -03001357 else if (strcmp(field->type, "pid_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001358 sc->arg_fmt[idx].scnprintf = SCA_PID;
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -03001359 else if (strcmp(field->type, "umode_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001360 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001361 else if ((strcmp(field->type, "int") == 0 ||
1362 strcmp(field->type, "unsigned int") == 0 ||
1363 strcmp(field->type, "long") == 0) &&
1364 (len = strlen(field->name)) >= 2 &&
1365 strcmp(field->name + len - 2, "fd") == 0) {
1366 /*
1367 * /sys/kernel/tracing/events/syscalls/sys_enter*
1368 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1369 * 65 int
1370 * 23 unsigned int
1371 * 7 unsigned long
1372 */
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001373 sc->arg_fmt[idx].scnprintf = SCA_FD;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001374 }
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001375 }
1376
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001377 if (last_field)
1378 sc->args_size = last_field->offset + last_field->size;
1379
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001380 return 0;
1381}
1382
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001383static int trace__read_syscall_info(struct trace *trace, int id)
1384{
1385 char tp_name[128];
1386 struct syscall *sc;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03001387 const char *name = syscalltbl__name(trace->sctbl, id);
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001388
1389 if (name == NULL)
1390 return -1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001391
1392 if (id > trace->syscalls.max) {
1393 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1394
1395 if (nsyscalls == NULL)
1396 return -1;
1397
1398 if (trace->syscalls.max != -1) {
1399 memset(nsyscalls + trace->syscalls.max + 1, 0,
1400 (id - trace->syscalls.max) * sizeof(*sc));
1401 } else {
1402 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1403 }
1404
1405 trace->syscalls.table = nsyscalls;
1406 trace->syscalls.max = id;
1407 }
1408
1409 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001410 sc->name = name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001411
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001412 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001413
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001414 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
Jiri Olsa97978b32013-12-03 14:09:24 +01001415 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001416
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001417 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001418 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
Jiri Olsa97978b32013-12-03 14:09:24 +01001419 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001420 }
1421
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001422 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1423 return -1;
1424
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001425 if (IS_ERR(sc->tp_format))
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001426 return -1;
1427
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001428 sc->args = sc->tp_format->format.fields;
Taeung Songc42de702016-02-26 22:14:25 +09001429 /*
1430 * We need to check and discard the first variable '__syscall_nr'
1431 * or 'nr' that mean the syscall number. It is needless here.
1432 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1433 */
1434 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001435 sc->args = sc->args->next;
1436 --sc->nr_args;
1437 }
1438
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001439 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001440 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001441
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001442 return syscall__set_arg_fmts(sc);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001443}
1444
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001445static int trace__validate_ev_qualifier(struct trace *trace)
1446{
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001447 int err = 0, i;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001448 size_t nr_allocated;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001449 struct str_node *pos;
1450
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001451 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1452 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1453 sizeof(trace->ev_qualifier_ids.entries[0]));
1454
1455 if (trace->ev_qualifier_ids.entries == NULL) {
1456 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1457 trace->output);
1458 err = -EINVAL;
1459 goto out;
1460 }
1461
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001462 nr_allocated = trace->ev_qualifier_ids.nr;
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001463 i = 0;
1464
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001465 strlist__for_each_entry(pos, trace->ev_qualifier) {
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001466 const char *sc = pos->s;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001467 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001468
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001469 if (id < 0) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001470 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1471 if (id >= 0)
1472 goto matches;
1473
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001474 if (err == 0) {
1475 fputs("Error:\tInvalid syscall ", trace->output);
1476 err = -EINVAL;
1477 } else {
1478 fputs(", ", trace->output);
1479 }
1480
1481 fputs(sc, trace->output);
1482 }
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001483matches:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001484 trace->ev_qualifier_ids.entries[i++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001485 if (match_next == -1)
1486 continue;
1487
1488 while (1) {
1489 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1490 if (id < 0)
1491 break;
1492 if (nr_allocated == trace->ev_qualifier_ids.nr) {
1493 void *entries;
1494
1495 nr_allocated += 8;
1496 entries = realloc(trace->ev_qualifier_ids.entries,
1497 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1498 if (entries == NULL) {
1499 err = -ENOMEM;
1500 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1501 goto out_free;
1502 }
1503 trace->ev_qualifier_ids.entries = entries;
1504 }
1505 trace->ev_qualifier_ids.nr++;
1506 trace->ev_qualifier_ids.entries[i++] = id;
1507 }
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001508 }
1509
1510 if (err < 0) {
1511 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1512 "\nHint:\tand: 'man syscalls'\n", trace->output);
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001513out_free:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001514 zfree(&trace->ev_qualifier_ids.entries);
1515 trace->ev_qualifier_ids.nr = 0;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001516 }
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001517out:
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001518 return err;
1519}
1520
David Ahern55d43bc2015-02-19 15:00:22 -05001521/*
1522 * args is to be interpreted as a series of longs but we need to handle
1523 * 8-byte unaligned accesses. args points to raw_data within the event
1524 * and raw_data is guaranteed to be 8-byte unaligned because it is
1525 * preceded by raw_size which is a u32. So we need to copy args to a temp
1526 * variable to read it. Most notably this avoids extended load instructions
1527 * on unaligned addresses
1528 */
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001529unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001530{
1531 unsigned long val;
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001532 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001533
1534 memcpy(&val, p, sizeof(val));
1535 return val;
1536}
1537
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001538static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1539 struct syscall_arg *arg)
1540{
1541 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1542 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1543
1544 return scnprintf(bf, size, "arg%d: ", arg->idx);
1545}
1546
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001547/*
1548 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1549 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1550 * in tools/perf/trace/beauty/mount_flags.c
1551 */
1552static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1553{
1554 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1555 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1556
1557 return val;
1558}
1559
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001560static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1561 struct syscall_arg *arg, unsigned long val)
1562{
1563 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1564 arg->val = val;
1565 if (sc->arg_fmt[arg->idx].parm)
1566 arg->parm = sc->arg_fmt[arg->idx].parm;
1567 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1568 }
1569 return scnprintf(bf, size, "%ld", val);
1570}
1571
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001572static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001573 unsigned char *args, void *augmented_args, int augmented_args_size,
1574 struct trace *trace, struct thread *thread)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001575{
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001576 size_t printed = 0;
David Ahern55d43bc2015-02-19 15:00:22 -05001577 unsigned long val;
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001578 u8 bit = 1;
1579 struct syscall_arg arg = {
1580 .args = args,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001581 .augmented = {
1582 .size = augmented_args_size,
1583 .args = augmented_args,
1584 },
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001585 .idx = 0,
1586 .mask = 0,
1587 .trace = trace,
1588 .thread = thread,
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -03001589 .show_string_prefix = trace->show_string_prefix,
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001590 };
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001591 struct thread_trace *ttrace = thread__priv(thread);
1592
1593 /*
1594 * Things like fcntl will set this in its 'cmd' formatter to pick the
1595 * right formatter for the return value (an fd? file flags?), which is
1596 * not needed for syscalls that always return a given type, say an fd.
1597 */
1598 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001599
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001600 if (sc->args != NULL) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001601 struct tep_format_field *field;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001602
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001603 for (field = sc->args; field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001604 field = field->next, ++arg.idx, bit <<= 1) {
1605 if (arg.mask & bit)
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001606 continue;
David Ahern55d43bc2015-02-19 15:00:22 -05001607
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001608 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001609 /*
1610 * Some syscall args need some mask, most don't and
1611 * return val untouched.
1612 */
1613 val = syscall__mask_val(sc, &arg, val);
David Ahern55d43bc2015-02-19 15:00:22 -05001614
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001615 /*
1616 * Suppress this argument if its value is zero and
1617 * and we don't have a string associated in an
1618 * strarray for it.
1619 */
David Ahern55d43bc2015-02-19 15:00:22 -05001620 if (val == 0 &&
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03001621 !trace->show_zeros &&
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001622 !(sc->arg_fmt &&
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -03001623 (sc->arg_fmt[arg.idx].show_zero ||
1624 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001625 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1626 sc->arg_fmt[arg.idx].parm))
Arnaldo Carvalho de Melo22ae5cf12013-09-12 11:27:34 -03001627 continue;
1628
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03001629 printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
1630
1631 if (trace->show_arg_names)
1632 printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
1633
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001634 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001635 }
Arnaldo Carvalho de Melo4c4d6e52016-05-05 23:38:05 -03001636 } else if (IS_ERR(sc->tp_format)) {
1637 /*
1638 * If we managed to read the tracepoint /format file, then we
1639 * may end up not having any args, like with gettid(), so only
1640 * print the raw args when we didn't manage to read it.
1641 */
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001642 while (arg.idx < sc->nr_args) {
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001643 if (arg.mask & bit)
1644 goto next_arg;
1645 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001646 if (printed)
1647 printed += scnprintf(bf + printed, size - printed, ", ");
1648 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001649 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1650next_arg:
1651 ++arg.idx;
1652 bit <<= 1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001653 }
1654 }
1655
1656 return printed;
1657}
1658
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001659typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001660 union perf_event *event,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001661 struct perf_sample *sample);
1662
1663static struct syscall *trace__syscall_info(struct trace *trace,
David Ahernbf2575c2013-10-08 21:26:53 -06001664 struct perf_evsel *evsel, int id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001665{
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001666
1667 if (id < 0) {
Arnaldo Carvalho de Meloadaa18b2013-08-22 17:55:25 -03001668
1669 /*
1670 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1671 * before that, leaving at a higher verbosity level till that is
1672 * explained. Reproduced with plain ftrace with:
1673 *
1674 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1675 * grep "NR -1 " /t/trace_pipe
1676 *
1677 * After generating some load on the machine.
1678 */
1679 if (verbose > 1) {
1680 static u64 n;
1681 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1682 id, perf_evsel__name(evsel), ++n);
1683 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001684 return NULL;
1685 }
1686
1687 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1688 trace__read_syscall_info(trace, id))
1689 goto out_cant_read;
1690
1691 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1692 goto out_cant_read;
1693
1694 return &trace->syscalls.table[id];
1695
1696out_cant_read:
Namhyung Kimbb963e12017-02-17 17:17:38 +09001697 if (verbose > 0) {
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03001698 fprintf(trace->output, "Problems reading syscall %d", id);
1699 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1700 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1701 fputs(" information\n", trace->output);
1702 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001703 return NULL;
1704}
1705
David Ahernbf2575c2013-10-08 21:26:53 -06001706static void thread__update_stats(struct thread_trace *ttrace,
1707 int id, struct perf_sample *sample)
1708{
1709 struct int_node *inode;
1710 struct stats *stats;
1711 u64 duration = 0;
1712
1713 inode = intlist__findnew(ttrace->syscall_stats, id);
1714 if (inode == NULL)
1715 return;
1716
1717 stats = inode->priv;
1718 if (stats == NULL) {
1719 stats = malloc(sizeof(struct stats));
1720 if (stats == NULL)
1721 return;
1722 init_stats(stats);
1723 inode->priv = stats;
1724 }
1725
1726 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1727 duration = sample->time - ttrace->entry_time;
1728
1729 update_stats(stats, duration);
1730}
1731
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001732static int trace__printf_interrupted_entry(struct trace *trace)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001733{
1734 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001735 size_t printed;
1736
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001737 if (trace->failure_only || trace->current == NULL)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001738 return 0;
1739
1740 ttrace = thread__priv(trace->current);
1741
1742 if (!ttrace->entry_pending)
1743 return 0;
1744
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001745 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001746 printed += fprintf(trace->output, ")%-*s ...\n", trace->args_alignment, ttrace->entry_str);
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001747 ttrace->entry_pending = false;
1748
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03001749 ++trace->nr_events_printed;
1750
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001751 return printed;
1752}
1753
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001754static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1755 struct perf_sample *sample, struct thread *thread)
1756{
1757 int printed = 0;
1758
1759 if (trace->print_sample) {
1760 double ts = (double)sample->time / NSEC_PER_MSEC;
1761
1762 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1763 perf_evsel__name(evsel), ts,
1764 thread__comm_str(thread),
1765 sample->pid, sample->tid, sample->cpu);
1766 }
1767
1768 return printed;
1769}
1770
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001771static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, bool raw_augmented)
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001772{
1773 void *augmented_args = NULL;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001774 /*
1775 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
1776 * and there we get all 6 syscall args plus the tracepoint common
1777 * fields (sizeof(long)) and the syscall_nr (another long). So we check
1778 * if that is the case and if so don't look after the sc->args_size,
1779 * but always after the full raw_syscalls:sys_enter payload, which is
1780 * fixed.
1781 *
1782 * We'll revisit this later to pass s->args_size to the BPF augmenter
1783 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1784 * copies only what we need for each syscall, like what happens when we
1785 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1786 * traffic to just what is needed for each syscall.
1787 */
1788 int args_size = raw_augmented ? (8 * (int)sizeof(long)) : sc->args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001789
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001790 *augmented_args_size = sample->raw_size - args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001791 if (*augmented_args_size > 0)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001792 augmented_args = sample->raw_data + args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001793
1794 return augmented_args;
1795}
1796
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001797static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001798 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001799 struct perf_sample *sample)
1800{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001801 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001802 void *args;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001803 int printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001804 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001805 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001806 int augmented_args_size = 0;
1807 void *augmented_args = NULL;
David Ahernbf2575c2013-10-08 21:26:53 -06001808 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001809 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001810
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001811 if (sc == NULL)
1812 return -1;
1813
David Ahern8fb598e2013-09-28 13:13:00 -06001814 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001815 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001816 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001817 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001818
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001819 trace__fprintf_sample(trace, evsel, sample, thread);
1820
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001821 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001822
1823 if (ttrace->entry_str == NULL) {
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001824 ttrace->entry_str = malloc(trace__entry_str_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001825 if (!ttrace->entry_str)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001826 goto out_put;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001827 }
1828
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001829 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001830 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001831 /*
1832 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1833 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1834 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1835 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1836 * so when handling, say the openat syscall, we end up getting 6 args for the
1837 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1838 * thinking that the extra 2 u64 args are the augmented filename, so just check
1839 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1840 */
1841 if (evsel != trace->syscalls.events.sys_enter)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001842 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001843 ttrace->entry_time = sample->time;
1844 msg = ttrace->entry_str;
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001845 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001846
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001847 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001848 args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001849
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001850 if (sc->is_exit) {
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001851 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001852 int alignment = 0;
1853
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001854 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001855 printed = fprintf(trace->output, "%s)", ttrace->entry_str);
1856 if (trace->args_alignment > printed)
1857 alignment = trace->args_alignment - printed;
1858 fprintf(trace->output, "%*s= ?\n", alignment, " ");
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001859 }
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001860 } else {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001861 ttrace->entry_pending = true;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001862 /* See trace__vfs_getname & trace__sys_exit */
1863 ttrace->filename.pending_open = false;
1864 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001865
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001866 if (trace->current != thread) {
1867 thread__put(trace->current);
1868 trace->current = thread__get(thread);
1869 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001870 err = 0;
1871out_put:
1872 thread__put(thread);
1873 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001874}
1875
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001876static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1877 struct perf_sample *sample)
1878{
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001879 struct thread_trace *ttrace;
1880 struct thread *thread;
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001881 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1882 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001883 char msg[1024];
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001884 void *args, *augmented_args = NULL;
1885 int augmented_args_size;
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001886
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001887 if (sc == NULL)
1888 return -1;
1889
1890 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1891 ttrace = thread__trace(thread, trace->output);
1892 /*
1893 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1894 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1895 */
1896 if (ttrace == NULL)
1897 goto out_put;
1898
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001899 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001900 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001901 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001902 fprintf(trace->output, "%s", msg);
1903 err = 0;
1904out_put:
1905 thread__put(thread);
1906 return err;
1907}
1908
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001909static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1910 struct perf_sample *sample,
1911 struct callchain_cursor *cursor)
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001912{
1913 struct addr_location al;
Ravi Bangoria3a9e9a42018-01-30 11:00:53 +05301914 int max_stack = evsel->attr.sample_max_stack ?
1915 evsel->attr.sample_max_stack :
1916 trace->max_stack;
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001917 int err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001918
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001919 if (machine__resolve(trace->host, &al, sample) < 0)
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001920 return -1;
1921
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001922 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1923 addr_location__put(&al);
1924 return err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001925}
1926
1927static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1928{
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001929 /* TODO: user-configurable print_opts */
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001930 const unsigned int print_opts = EVSEL__PRINT_SYM |
1931 EVSEL__PRINT_DSO |
1932 EVSEL__PRINT_UNKNOWN_AS_ADDR;
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001933
Arnaldo Carvalho de Melod327e602016-04-14 17:53:49 -03001934 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001935}
1936
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01001937static const char *errno_to_name(struct perf_evsel *evsel, int err)
1938{
1939 struct perf_env *env = perf_evsel__env(evsel);
1940 const char *arch_name = perf_env__arch(env);
1941
1942 return arch_syscalls__strerrno(arch_name, err);
1943}
1944
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001945static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001946 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001947 struct perf_sample *sample)
1948{
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09001949 long ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001950 u64 duration = 0;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001951 bool duration_calculated = false;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001952 struct thread *thread;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03001953 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
1954 int alignment = trace->args_alignment;
David Ahernbf2575c2013-10-08 21:26:53 -06001955 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001956 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001957
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001958 if (sc == NULL)
1959 return -1;
1960
David Ahern8fb598e2013-09-28 13:13:00 -06001961 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001962 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001963 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001964 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001965
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001966 trace__fprintf_sample(trace, evsel, sample, thread);
1967
David Ahernbf2575c2013-10-08 21:26:53 -06001968 if (trace->summary)
1969 thread__update_stats(ttrace, id, sample);
1970
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001971 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001972
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001973 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001974 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
1975 ttrace->filename.pending_open = false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001976 ++trace->stats.vfs_getname;
1977 }
1978
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001979 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001980 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001981 if (trace__filter_duration(trace, duration))
1982 goto out;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001983 duration_calculated = true;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001984 } else if (trace->duration_filter)
1985 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001986
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001987 if (sample->callchain) {
1988 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
1989 if (callchain_ret == 0) {
1990 if (callchain_cursor.nr < trace->min_stack)
1991 goto out;
1992 callchain_ret = 1;
1993 }
1994 }
1995
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001996 if (trace->summary_only || (ret >= 0 && trace->failure_only))
David Ahernfd2eaba2013-11-12 09:31:15 -07001997 goto out;
1998
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001999 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002000
2001 if (ttrace->entry_pending) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002002 printed = fprintf(trace->output, "%s", ttrace->entry_str);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002003 } else {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002004 fprintf(trace->output, " ... [");
2005 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
2006 fprintf(trace->output, "]: %s()", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002007 }
2008
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002009 printed++; /* the closing ')' */
2010
2011 if (alignment > printed)
2012 alignment -= printed;
2013 else
2014 alignment = 0;
2015
2016 fprintf(trace->output, ")%*s= ", alignment, " ");
2017
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002018 if (sc->fmt == NULL) {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002019 if (ret < 0)
2020 goto errno_print;
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002021signed_print:
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002022 fprintf(trace->output, "%ld", ret);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002023 } else if (ret < 0) {
2024errno_print: {
Masami Hiramatsu942a91e2014-08-14 02:22:41 +00002025 char bf[STRERR_BUFSIZE];
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002026 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01002027 *e = errno_to_name(evsel, -ret);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002028
Arnaldo Carvalho de Melo2c83dfa2018-12-17 12:06:34 -03002029 fprintf(trace->output, "-1 %s (%s)", e, emsg);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03002030 }
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002031 } else if (ret == 0 && sc->fmt->timeout)
Arnaldo Carvalho de Melo2c83dfa2018-12-17 12:06:34 -03002032 fprintf(trace->output, "0 (Timeout)");
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002033 else if (ttrace->ret_scnprintf) {
2034 char bf[1024];
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03002035 struct syscall_arg arg = {
2036 .val = ret,
2037 .thread = thread,
2038 .trace = trace,
2039 };
2040 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002041 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002042 fprintf(trace->output, "%s", bf);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002043 } else if (sc->fmt->hexret)
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002044 fprintf(trace->output, "%#lx", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002045 else if (sc->fmt->errpid) {
2046 struct thread *child = machine__find_thread(trace->host, ret, ret);
2047
2048 if (child != NULL) {
Arnaldo Carvalho de Melo4b8a2402018-12-17 11:48:46 -03002049 fprintf(trace->output, "%ld", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002050 if (child->comm_set)
2051 fprintf(trace->output, " (%s)", thread__comm_str(child));
2052 thread__put(child);
2053 }
2054 } else
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002055 goto signed_print;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002056
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002057 fputc('\n', trace->output);
Milian Wolff566a0882016-04-08 13:34:15 +02002058
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002059 /*
2060 * We only consider an 'event' for the sake of --max-events a non-filtered
2061 * sys_enter + sys_exit and other tracepoint events.
2062 */
2063 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2064 interrupted = true;
2065
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002066 if (callchain_ret > 0)
2067 trace__fprintf_callchain(trace, sample);
2068 else if (callchain_ret < 0)
2069 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002070out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002071 ttrace->entry_pending = false;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002072 err = 0;
2073out_put:
2074 thread__put(thread);
2075 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002076}
2077
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002078static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002079 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002080 struct perf_sample *sample)
2081{
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002082 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2083 struct thread_trace *ttrace;
2084 size_t filename_len, entry_str_len, to_move;
2085 ssize_t remaining_space;
2086 char *pos;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002087 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002088
2089 if (!thread)
2090 goto out;
2091
2092 ttrace = thread__priv(thread);
2093 if (!ttrace)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002094 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002095
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002096 filename_len = strlen(filename);
Arnaldo Carvalho de Melo39f0e7a2017-03-24 14:51:28 -03002097 if (filename_len == 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002098 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002099
2100 if (ttrace->filename.namelen < filename_len) {
2101 char *f = realloc(ttrace->filename.name, filename_len + 1);
2102
2103 if (f == NULL)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002104 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002105
2106 ttrace->filename.namelen = filename_len;
2107 ttrace->filename.name = f;
2108 }
2109
2110 strcpy(ttrace->filename.name, filename);
2111 ttrace->filename.pending_open = true;
2112
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002113 if (!ttrace->filename.ptr)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002114 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002115
2116 entry_str_len = strlen(ttrace->entry_str);
2117 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2118 if (remaining_space <= 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002119 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002120
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002121 if (filename_len > (size_t)remaining_space) {
2122 filename += filename_len - remaining_space;
2123 filename_len = remaining_space;
2124 }
2125
2126 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2127 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2128 memmove(pos + filename_len, pos, to_move);
2129 memcpy(pos, filename, filename_len);
2130
2131 ttrace->filename.ptr = 0;
2132 ttrace->filename.entry_str_pos = 0;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002133out_put:
2134 thread__put(thread);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002135out:
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002136 return 0;
2137}
2138
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002139static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002140 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002141 struct perf_sample *sample)
2142{
2143 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2144 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
David Ahern8fb598e2013-09-28 13:13:00 -06002145 struct thread *thread = machine__findnew_thread(trace->host,
Adrian Hunter314add62013-08-27 11:23:03 +03002146 sample->pid,
2147 sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002148 struct thread_trace *ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002149
2150 if (ttrace == NULL)
2151 goto out_dump;
2152
2153 ttrace->runtime_ms += runtime_ms;
2154 trace->runtime_ms += runtime_ms;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002155out_put:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002156 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002157 return 0;
2158
2159out_dump:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002160 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002161 evsel->name,
2162 perf_evsel__strval(evsel, sample, "comm"),
2163 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2164 runtime,
2165 perf_evsel__intval(evsel, sample, "vruntime"));
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002166 goto out_put;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002167}
2168
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002169static int bpf_output__printer(enum binary_printer_ops op,
2170 unsigned int val, void *extra __maybe_unused, FILE *fp)
Wang Nan1d6c9402016-02-26 09:31:55 +00002171{
Wang Nan1d6c9402016-02-26 09:31:55 +00002172 unsigned char ch = (unsigned char)val;
2173
2174 switch (op) {
2175 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002176 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
Wang Nan1d6c9402016-02-26 09:31:55 +00002177 case BINARY_PRINT_DATA_BEGIN:
2178 case BINARY_PRINT_LINE_BEGIN:
2179 case BINARY_PRINT_ADDR:
2180 case BINARY_PRINT_NUM_DATA:
2181 case BINARY_PRINT_NUM_PAD:
2182 case BINARY_PRINT_SEP:
2183 case BINARY_PRINT_CHAR_PAD:
2184 case BINARY_PRINT_LINE_END:
2185 case BINARY_PRINT_DATA_END:
2186 default:
2187 break;
2188 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002189
2190 return 0;
Wang Nan1d6c9402016-02-26 09:31:55 +00002191}
2192
2193static void bpf_output__fprintf(struct trace *trace,
2194 struct perf_sample *sample)
2195{
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002196 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2197 bpf_output__printer, NULL, trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002198 ++trace->nr_events_printed;
Wang Nan1d6c9402016-02-26 09:31:55 +00002199}
2200
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002201static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2202 union perf_event *event __maybe_unused,
2203 struct perf_sample *sample)
2204{
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002205 struct thread *thread;
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002206 int callchain_ret = 0;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002207 /*
2208 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2209 * this event's max_events having been hit and this is an entry coming
2210 * from the ring buffer that we should discard, since the max events
2211 * have already been considered/printed.
2212 */
2213 if (evsel->disabled)
2214 return 0;
2215
2216 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002217
2218 if (sample->callchain) {
2219 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2220 if (callchain_ret == 0) {
2221 if (callchain_cursor.nr < trace->min_stack)
2222 goto out;
2223 callchain_ret = 1;
2224 }
2225 }
2226
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03002227 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002228 trace__fprintf_tstamp(trace, sample->time, trace->output);
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002229
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03002230 if (trace->trace_syscalls && trace->show_duration)
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002231 fprintf(trace->output, "( ): ");
2232
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002233 if (thread)
2234 trace__fprintf_comm_tid(trace, thread, trace->output);
2235
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002236 if (evsel == trace->syscalls.events.augmented) {
2237 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2238 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2239
2240 if (sc) {
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002241 fprintf(trace->output, "%s(", sc->name);
2242 trace__fprintf_sys_enter(trace, evsel, sample);
2243 fputc(')', trace->output);
2244 goto newline;
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002245 }
2246
2247 /*
2248 * XXX: Not having the associated syscall info or not finding/adding
2249 * the thread should never happen, but if it does...
2250 * fall thru and print it as a bpf_output event.
2251 */
2252 }
2253
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002254 fprintf(trace->output, "%s:", evsel->name);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002255
Wang Nan1d6c9402016-02-26 09:31:55 +00002256 if (perf_evsel__is_bpf_output(evsel)) {
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002257 bpf_output__fprintf(trace, sample);
Wang Nan1d6c9402016-02-26 09:31:55 +00002258 } else if (evsel->tp_format) {
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002259 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2260 trace__fprintf_sys_enter(trace, evsel, sample)) {
2261 event_format__fprintf(evsel->tp_format, sample->cpu,
2262 sample->raw_data, sample->raw_size,
2263 trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002264 ++trace->nr_events_printed;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002265
2266 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2267 perf_evsel__disable(evsel);
2268 perf_evsel__close(evsel);
2269 }
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002270 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002271 }
2272
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002273newline:
Changbin Du51125a22018-03-13 18:40:01 +08002274 fprintf(trace->output, "\n");
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002275
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002276 if (callchain_ret > 0)
2277 trace__fprintf_callchain(trace, sample);
2278 else if (callchain_ret < 0)
2279 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2280out:
Arnaldo Carvalho de Meloa937c662018-10-20 09:27:52 -03002281 thread__put(thread);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002282 return 0;
2283}
2284
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002285static void print_location(FILE *f, struct perf_sample *sample,
2286 struct addr_location *al,
2287 bool print_dso, bool print_sym)
2288{
2289
Namhyung Kimbb963e12017-02-17 17:17:38 +09002290 if ((verbose > 0 || print_dso) && al->map)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002291 fprintf(f, "%s@", al->map->dso->long_name);
2292
Namhyung Kimbb963e12017-02-17 17:17:38 +09002293 if ((verbose > 0 || print_sym) && al->sym)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002294 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002295 al->addr - al->sym->start);
2296 else if (al->map)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002297 fprintf(f, "0x%" PRIx64, al->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002298 else
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002299 fprintf(f, "0x%" PRIx64, sample->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002300}
2301
2302static int trace__pgfault(struct trace *trace,
2303 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03002304 union perf_event *event __maybe_unused,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002305 struct perf_sample *sample)
2306{
2307 struct thread *thread;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002308 struct addr_location al;
2309 char map_type = 'd';
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002310 struct thread_trace *ttrace;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002311 int err = -1;
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002312 int callchain_ret = 0;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002313
2314 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002315
2316 if (sample->callchain) {
2317 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2318 if (callchain_ret == 0) {
2319 if (callchain_cursor.nr < trace->min_stack)
2320 goto out_put;
2321 callchain_ret = 1;
2322 }
2323 }
2324
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002325 ttrace = thread__trace(thread, trace->output);
2326 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002327 goto out_put;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002328
2329 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2330 ttrace->pfmaj++;
2331 else
2332 ttrace->pfmin++;
2333
2334 if (trace->summary_only)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002335 goto out;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002336
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002337 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002338
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002339 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002340
2341 fprintf(trace->output, "%sfault [",
2342 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2343 "maj" : "min");
2344
2345 print_location(trace->output, sample, &al, false, true);
2346
2347 fprintf(trace->output, "] => ");
2348
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03002349 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002350
2351 if (!al.map) {
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002352 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002353
2354 if (al.map)
2355 map_type = 'x';
2356 else
2357 map_type = '?';
2358 }
2359
2360 print_location(trace->output, sample, &al, true, false);
2361
2362 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
Arnaldo Carvalho de Melo0c3a6ef2016-04-19 16:31:12 -03002363
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002364 if (callchain_ret > 0)
2365 trace__fprintf_callchain(trace, sample);
2366 else if (callchain_ret < 0)
2367 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002368
2369 ++trace->nr_events_printed;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002370out:
2371 err = 0;
2372out_put:
2373 thread__put(thread);
2374 return err;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002375}
2376
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002377static void trace__set_base_time(struct trace *trace,
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002378 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002379 struct perf_sample *sample)
2380{
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002381 /*
2382 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2383 * and don't use sample->time unconditionally, we may end up having
2384 * some other event in the future without PERF_SAMPLE_TIME for good
2385 * reason, i.e. we may not be interested in its timestamps, just in
2386 * it taking place, picking some piece of information when it
2387 * appears in our event stream (vfs_getname comes to mind).
2388 */
2389 if (trace->base_time == 0 && !trace->full_time &&
2390 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002391 trace->base_time = sample->time;
2392}
2393
David Ahern6810fc92013-08-28 22:29:52 -06002394static int trace__process_sample(struct perf_tool *tool,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002395 union perf_event *event,
David Ahern6810fc92013-08-28 22:29:52 -06002396 struct perf_sample *sample,
2397 struct perf_evsel *evsel,
2398 struct machine *machine __maybe_unused)
2399{
2400 struct trace *trace = container_of(tool, struct trace, tool);
David Ahernaa07df62016-11-25 09:29:52 -07002401 struct thread *thread;
David Ahern6810fc92013-08-28 22:29:52 -06002402 int err = 0;
2403
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002404 tracepoint_handler handler = evsel->handler;
David Ahern6810fc92013-08-28 22:29:52 -06002405
David Ahernaa07df62016-11-25 09:29:52 -07002406 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2407 if (thread && thread__is_filtered(thread))
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002408 goto out;
David Ahernbdc89662013-08-28 22:29:53 -06002409
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002410 trace__set_base_time(trace, evsel, sample);
David Ahern6810fc92013-08-28 22:29:52 -06002411
David Ahern31605652013-12-04 19:41:41 -07002412 if (handler) {
2413 ++trace->nr_events;
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002414 handler(trace, evsel, event, sample);
David Ahern31605652013-12-04 19:41:41 -07002415 }
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002416out:
2417 thread__put(thread);
David Ahern6810fc92013-08-28 22:29:52 -06002418 return err;
2419}
2420
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002421static int trace__record(struct trace *trace, int argc, const char **argv)
David Ahern5e2485b2013-09-28 13:13:01 -06002422{
2423 unsigned int rec_argc, i, j;
2424 const char **rec_argv;
2425 const char * const record_args[] = {
2426 "record",
2427 "-R",
2428 "-m", "1024",
2429 "-c", "1",
David Ahern5e2485b2013-09-28 13:13:01 -06002430 };
2431
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002432 const char * const sc_args[] = { "-e", };
2433 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2434 const char * const majpf_args[] = { "-e", "major-faults" };
2435 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2436 const char * const minpf_args[] = { "-e", "minor-faults" };
2437 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2438
David Ahern9aca7f12013-12-04 19:41:39 -07002439 /* +1 is for the event string below */
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002440 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2441 majpf_args_nr + minpf_args_nr + argc;
David Ahern5e2485b2013-09-28 13:13:01 -06002442 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2443
2444 if (rec_argv == NULL)
2445 return -ENOMEM;
2446
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002447 j = 0;
David Ahern5e2485b2013-09-28 13:13:01 -06002448 for (i = 0; i < ARRAY_SIZE(record_args); i++)
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002449 rec_argv[j++] = record_args[i];
2450
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002451 if (trace->trace_syscalls) {
2452 for (i = 0; i < sc_args_nr; i++)
2453 rec_argv[j++] = sc_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002454
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002455 /* event string may be different for older kernels - e.g., RHEL6 */
2456 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2457 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2458 else if (is_valid_tracepoint("syscalls:sys_enter"))
2459 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2460 else {
2461 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
Martin Kepplingerc896f852017-09-13 21:14:19 +02002462 free(rec_argv);
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002463 return -1;
2464 }
David Ahern9aca7f12013-12-04 19:41:39 -07002465 }
David Ahern9aca7f12013-12-04 19:41:39 -07002466
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002467 if (trace->trace_pgfaults & TRACE_PFMAJ)
2468 for (i = 0; i < majpf_args_nr; i++)
2469 rec_argv[j++] = majpf_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002470
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002471 if (trace->trace_pgfaults & TRACE_PFMIN)
2472 for (i = 0; i < minpf_args_nr; i++)
2473 rec_argv[j++] = minpf_args[i];
2474
2475 for (i = 0; i < (unsigned int)argc; i++)
2476 rec_argv[j++] = argv[i];
2477
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002478 return cmd_record(j, rec_argv);
David Ahern5e2485b2013-09-28 13:13:01 -06002479}
2480
David Ahernbf2575c2013-10-08 21:26:53 -06002481static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2482
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002483static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002484{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002485 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002486
2487 if (IS_ERR(evsel))
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002488 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002489
2490 if (perf_evsel__field(evsel, "pathname") == NULL) {
2491 perf_evsel__delete(evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002492 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002493 }
2494
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002495 evsel->handler = trace__vfs_getname;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002496 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002497 return true;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002498}
2499
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002500static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002501{
2502 struct perf_evsel *evsel;
2503 struct perf_event_attr attr = {
2504 .type = PERF_TYPE_SOFTWARE,
2505 .mmap_data = 1,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002506 };
2507
2508 attr.config = config;
Arnaldo Carvalho de Melo05247982014-07-23 18:15:09 -03002509 attr.sample_period = 1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002510
2511 event_attr_init(&attr);
2512
2513 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002514 if (evsel)
2515 evsel->handler = trace__pgfault;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002516
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002517 return evsel;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002518}
2519
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002520static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2521{
2522 const u32 type = event->header.type;
2523 struct perf_evsel *evsel;
2524
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002525 if (type != PERF_RECORD_SAMPLE) {
2526 trace__process_event(trace, trace->host, event, sample);
2527 return;
2528 }
2529
2530 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2531 if (evsel == NULL) {
2532 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2533 return;
2534 }
2535
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002536 trace__set_base_time(trace, evsel, sample);
2537
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002538 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2539 sample->raw_data == NULL) {
2540 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2541 perf_evsel__name(evsel), sample->tid,
2542 sample->cpu, sample->raw_size);
2543 } else {
2544 tracepoint_handler handler = evsel->handler;
2545 handler(trace, evsel, event, sample);
2546 }
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002547
2548 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2549 interrupted = true;
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002550}
2551
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002552static int trace__add_syscall_newtp(struct trace *trace)
2553{
2554 int ret = -1;
2555 struct perf_evlist *evlist = trace->evlist;
2556 struct perf_evsel *sys_enter, *sys_exit;
2557
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002558 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002559 if (sys_enter == NULL)
2560 goto out;
2561
2562 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2563 goto out_delete_sys_enter;
2564
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002565 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002566 if (sys_exit == NULL)
2567 goto out_delete_sys_enter;
2568
2569 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2570 goto out_delete_sys_exit;
2571
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002572 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2573 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2574
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002575 perf_evlist__add(evlist, sys_enter);
2576 perf_evlist__add(evlist, sys_exit);
2577
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002578 if (callchain_param.enabled && !trace->kernel_syscallchains) {
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03002579 /*
2580 * We're interested only in the user space callchain
2581 * leading to the syscall, allow overriding that for
2582 * debugging reasons using --kernel_syscall_callchains
2583 */
2584 sys_exit->attr.exclude_callchain_kernel = 1;
2585 }
2586
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03002587 trace->syscalls.events.sys_enter = sys_enter;
2588 trace->syscalls.events.sys_exit = sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002589
2590 ret = 0;
2591out:
2592 return ret;
2593
2594out_delete_sys_exit:
2595 perf_evsel__delete_priv(sys_exit);
2596out_delete_sys_enter:
2597 perf_evsel__delete_priv(sys_enter);
2598 goto out;
2599}
2600
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002601static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002602{
2603 int err = -1;
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002604 struct perf_evsel *sys_exit;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002605 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2606 trace->ev_qualifier_ids.nr,
2607 trace->ev_qualifier_ids.entries);
2608
2609 if (filter == NULL)
2610 goto out_enomem;
2611
Mathieu Poirier3541c032016-09-16 08:44:04 -06002612 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2613 filter)) {
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002614 sys_exit = trace->syscalls.events.sys_exit;
Mathieu Poirier3541c032016-09-16 08:44:04 -06002615 err = perf_evsel__append_tp_filter(sys_exit, filter);
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002616 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002617
2618 free(filter);
2619out:
2620 return err;
2621out_enomem:
2622 errno = ENOMEM;
2623 goto out;
2624}
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002625
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002626#ifdef HAVE_LIBBPF_SUPPORT
2627static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2628{
2629 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002630 struct bpf_map_syscall_entry value = {
2631 .enabled = !trace->not_ev_qualifier,
2632 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002633 int err = 0;
2634 size_t i;
2635
2636 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2637 int key = trace->ev_qualifier_ids.entries[i];
2638
2639 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2640 if (err)
2641 break;
2642 }
2643
2644 return err;
2645}
2646
2647static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2648{
2649 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002650 struct bpf_map_syscall_entry value = {
2651 .enabled = enabled,
2652 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002653 int err = 0, key;
2654
2655 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002656 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002657 if (err)
2658 break;
2659 }
2660
2661 return err;
2662}
2663
2664static int trace__init_syscalls_bpf_map(struct trace *trace)
2665{
2666 bool enabled = true;
2667
2668 if (trace->ev_qualifier_ids.nr)
2669 enabled = trace->not_ev_qualifier;
2670
2671 return __trace__init_syscalls_bpf_map(trace, enabled);
2672}
2673#else
2674static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
2675{
2676 return 0;
2677}
2678
2679static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
2680{
2681 return 0;
2682}
2683#endif // HAVE_LIBBPF_SUPPORT
2684
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002685static int trace__set_ev_qualifier_filter(struct trace *trace)
2686{
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002687 if (trace->syscalls.map)
2688 return trace__set_ev_qualifier_bpf_filter(trace);
Arnaldo Carvalho de Melof76214f2018-12-19 10:01:20 -03002689 if (trace->syscalls.events.sys_enter)
2690 return trace__set_ev_qualifier_tp_filter(trace);
2691 return 0;
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002692}
2693
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002694static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2695 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2696{
2697 int err = 0;
2698#ifdef HAVE_LIBBPF_SUPPORT
2699 bool value = true;
2700 int map_fd = bpf_map__fd(map);
2701 size_t i;
2702
2703 for (i = 0; i < npids; ++i) {
2704 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2705 if (err)
2706 break;
2707 }
2708#endif
2709 return err;
2710}
2711
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002712static int trace__set_filter_loop_pids(struct trace *trace)
2713{
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002714 unsigned int nr = 1, err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002715 pid_t pids[32] = {
2716 getpid(),
2717 };
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03002718 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2719
2720 while (thread && nr < ARRAY_SIZE(pids)) {
2721 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2722
2723 if (parent == NULL)
2724 break;
2725
2726 if (!strcmp(thread__comm_str(parent), "sshd")) {
2727 pids[nr++] = parent->tid;
2728 break;
2729 }
2730 thread = parent;
2731 }
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002732
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002733 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2734 if (!err && trace->filter_pids.map)
2735 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2736
2737 return err;
2738}
2739
2740static int trace__set_filter_pids(struct trace *trace)
2741{
2742 int err = 0;
2743 /*
2744 * Better not use !target__has_task() here because we need to cover the
2745 * case where no threads were specified in the command line, but a
2746 * workload was, and in that case we will fill in the thread_map when
2747 * we fork the workload in perf_evlist__prepare_workload.
2748 */
2749 if (trace->filter_pids.nr > 0) {
2750 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2751 trace->filter_pids.entries);
2752 if (!err && trace->filter_pids.map) {
2753 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2754 trace->filter_pids.entries);
2755 }
2756 } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2757 err = trace__set_filter_loop_pids(trace);
2758 }
2759
2760 return err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002761}
2762
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002763static int __trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa1f44b3e2018-12-05 17:05:08 +01002764{
2765 struct perf_evlist *evlist = trace->evlist;
2766 struct perf_sample sample;
2767 int err;
2768
2769 err = perf_evlist__parse_sample(evlist, event, &sample);
2770 if (err)
2771 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
2772 else
2773 trace__handle_event(trace, event, &sample);
2774
2775 return 0;
2776}
2777
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002778static int __trace__flush_events(struct trace *trace)
Jiri Olsa028713a2018-12-05 17:05:09 +01002779{
2780 u64 first = ordered_events__first_time(&trace->oe.data);
2781 u64 flush = trace->oe.last - NSEC_PER_SEC;
2782
2783 /* Is there some thing to flush.. */
2784 if (first && first < flush)
2785 return ordered_events__flush_time(&trace->oe.data, flush);
2786
2787 return 0;
2788}
2789
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002790static int trace__flush_events(struct trace *trace)
2791{
2792 return !trace->sort_events ? 0 : __trace__flush_events(trace);
2793}
2794
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002795static int trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa028713a2018-12-05 17:05:09 +01002796{
Jiri Olsa028713a2018-12-05 17:05:09 +01002797 int err;
2798
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002799 if (!trace->sort_events)
2800 return __trace__deliver_event(trace, event);
2801
2802 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
Jiri Olsa028713a2018-12-05 17:05:09 +01002803 if (err && err != -1)
2804 return err;
2805
2806 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
2807 if (err)
2808 return err;
2809
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002810 return trace__flush_events(trace);
Jiri Olsa028713a2018-12-05 17:05:09 +01002811}
2812
2813static int ordered_events__deliver_event(struct ordered_events *oe,
2814 struct ordered_event *event)
2815{
2816 struct trace *trace = container_of(oe, struct trace, oe.data);
2817
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002818 return __trace__deliver_event(trace, event->event);
Jiri Olsa028713a2018-12-05 17:05:09 +01002819}
2820
Namhyung Kimf15eb532012-10-05 14:02:16 +09002821static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002822{
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002823 struct perf_evlist *evlist = trace->evlist;
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002824 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002825 int err = -1, i;
2826 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002827 const bool forks = argc > 0;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002828 bool draining = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002829
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002830 trace->live = true;
2831
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002832 if (!trace->raw_augmented_syscalls) {
2833 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
2834 goto out_error_raw_syscalls;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002835
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002836 if (trace->trace_syscalls)
2837 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
2838 }
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002839
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002840 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2841 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2842 if (pgfault_maj == NULL)
2843 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002844 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002845 perf_evlist__add(evlist, pgfault_maj);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03002846 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002847
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002848 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2849 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2850 if (pgfault_min == NULL)
2851 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002852 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002853 perf_evlist__add(evlist, pgfault_min);
2854 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002855
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002856 if (trace->sched &&
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002857 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2858 trace__sched_stat_runtime))
2859 goto out_error_sched_stat_runtime;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002860
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03002861 /*
2862 * If a global cgroup was set, apply it to all the events without an
2863 * explicit cgroup. I.e.:
2864 *
2865 * trace -G A -e sched:*switch
2866 *
2867 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2868 * _and_ sched:sched_switch to the 'A' cgroup, while:
2869 *
2870 * trace -e sched:*switch -G A
2871 *
2872 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2873 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2874 * a cgroup (on the root cgroup, sys wide, etc).
2875 *
2876 * Multiple cgroups:
2877 *
2878 * trace -G A -e sched:*switch -G B
2879 *
2880 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2881 * to the 'B' cgroup.
2882 *
2883 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2884 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2885 */
2886 if (trace->cgroup)
2887 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2888
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002889 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2890 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002891 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002892 goto out_delete_evlist;
2893 }
2894
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002895 err = trace__symbols_init(trace, evlist);
2896 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002897 fprintf(trace->output, "Problems initializing symbol libraries!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002898 goto out_delete_evlist;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002899 }
2900
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03002901 perf_evlist__config(evlist, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melofde54b72016-04-11 18:42:37 -03002902
Namhyung Kimf15eb532012-10-05 14:02:16 +09002903 signal(SIGCHLD, sig_handler);
2904 signal(SIGINT, sig_handler);
2905
2906 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09002907 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03002908 argv, false, NULL);
Namhyung Kimf15eb532012-10-05 14:02:16 +09002909 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002910 fprintf(trace->output, "Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002911 goto out_delete_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002912 }
2913 }
2914
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002915 err = perf_evlist__open(evlist);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002916 if (err < 0)
2917 goto out_error_open;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002918
Wang Nanba504232016-02-26 09:31:54 +00002919 err = bpf__apply_obj_config();
2920 if (err) {
2921 char errbuf[BUFSIZ];
2922
2923 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2924 pr_err("ERROR: Apply config to BPF failed: %s\n",
2925 errbuf);
2926 goto out_error_open;
2927 }
2928
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002929 err = trace__set_filter_pids(trace);
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002930 if (err < 0)
2931 goto out_error_mem;
2932
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002933 if (trace->syscalls.map)
2934 trace__init_syscalls_bpf_map(trace);
2935
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002936 if (trace->ev_qualifier_ids.nr > 0) {
2937 err = trace__set_ev_qualifier_filter(trace);
2938 if (err < 0)
2939 goto out_errno;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002940
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002941 if (trace->syscalls.events.sys_exit) {
2942 pr_debug("event qualifier tracepoint filter: %s\n",
2943 trace->syscalls.events.sys_exit->filter);
2944 }
Arnaldo Carvalho de Melo2e5e5f82015-08-03 17:12:29 -03002945 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002946
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002947 err = perf_evlist__apply_filters(evlist, &evsel);
2948 if (err < 0)
2949 goto out_error_apply_filters;
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08002950
Wang Nanf74b9d3a2017-12-03 02:00:37 +00002951 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002952 if (err < 0)
2953 goto out_error_mmap;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002954
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002955 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
Arnaldo Carvalho de Melocb24d012015-04-22 10:04:23 -03002956 perf_evlist__enable(evlist);
2957
Namhyung Kimf15eb532012-10-05 14:02:16 +09002958 if (forks)
2959 perf_evlist__start_workload(evlist);
2960
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002961 if (trace->opts.initial_delay) {
2962 usleep(trace->opts.initial_delay * 1000);
2963 perf_evlist__enable(evlist);
2964 }
2965
Jiri Olsae13798c2015-06-23 00:36:02 +02002966 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
Arnaldo Carvalho de Melo42052be2015-02-13 12:32:45 -03002967 evlist->threads->nr > 1 ||
2968 perf_evlist__first(evlist)->attr.inherit;
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002969
2970 /*
2971 * Now that we already used evsel->attr to ask the kernel to setup the
2972 * events, lets reuse evsel->attr.sample_max_stack as the limit in
2973 * trace__resolve_callchain(), allowing per-event max-stack settings
Ingo Molnaradba1632018-12-03 11:22:00 +01002974 * to override an explicitly set --max-stack global setting.
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002975 */
2976 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03002977 if (evsel__has_callchain(evsel) &&
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002978 evsel->attr.sample_max_stack == 0)
2979 evsel->attr.sample_max_stack = trace->max_stack;
2980 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002981again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002982 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002983
2984 for (i = 0; i < evlist->nr_mmaps; i++) {
2985 union perf_event *event;
Kan Liangd7f55c62018-03-01 18:08:59 -05002986 struct perf_mmap *md;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002987
Kan Liangd7f55c62018-03-01 18:08:59 -05002988 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -05002989 if (perf_mmap__read_init(md) < 0)
Kan Liangd7f55c62018-03-01 18:08:59 -05002990 continue;
2991
Kan Liang0019dc872018-03-06 10:36:06 -05002992 while ((event = perf_mmap__read_event(md)) != NULL) {
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002993 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002994
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002995 err = trace__deliver_event(trace, event);
Jiri Olsa028713a2018-12-05 17:05:09 +01002996 if (err)
2997 goto out_disable;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002998
Kan Liangd6ace3d2018-03-06 10:36:05 -05002999 perf_mmap__consume(md);
Arnaldo Carvalho de Melo20c5f102013-09-03 11:55:07 -03003000
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003001 if (interrupted)
3002 goto out_disable;
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03003003
3004 if (done && !draining) {
3005 perf_evlist__disable(evlist);
3006 draining = true;
3007 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003008 }
Kan Liangd7f55c62018-03-01 18:08:59 -05003009 perf_mmap__read_done(md);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003010 }
3011
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03003012 if (trace->nr_events == before) {
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003013 int timeout = done ? 100 : -1;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003014
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003015 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03003016 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003017 draining = true;
3018
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003019 goto again;
Jiri Olsa028713a2018-12-05 17:05:09 +01003020 } else {
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03003021 if (trace__flush_events(trace))
Jiri Olsa028713a2018-12-05 17:05:09 +01003022 goto out_disable;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03003023 }
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003024 } else {
3025 goto again;
Namhyung Kimf15eb532012-10-05 14:02:16 +09003026 }
3027
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003028out_disable:
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03003029 thread__zput(trace->current);
3030
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03003031 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003032
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003033 if (trace->sort_events)
3034 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
Jiri Olsa028713a2018-12-05 17:05:09 +01003035
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003036 if (!err) {
3037 if (trace->summary)
3038 trace__fprintf_thread_summary(trace, trace->output);
3039
3040 if (trace->show_tool_stats) {
3041 fprintf(trace->output, "Stats:\n "
3042 " vfs_getname : %" PRIu64 "\n"
3043 " proc_getname: %" PRIu64 "\n",
3044 trace->stats.vfs_getname,
3045 trace->stats.proc_getname);
3046 }
3047 }
David Ahernbf2575c2013-10-08 21:26:53 -06003048
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003049out_delete_evlist:
Andrei Vagin33974a42017-11-07 16:22:45 -08003050 trace__symbols__exit(trace);
3051
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003052 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003053 cgroup__put(trace->cgroup);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003054 trace->evlist = NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03003055 trace->live = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003056 return err;
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003057{
3058 char errbuf[BUFSIZ];
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003059
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003060out_error_sched_stat_runtime:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003061 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003062 goto out_error;
3063
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03003064out_error_raw_syscalls:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003065 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003066 goto out_error;
3067
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03003068out_error_mmap:
3069 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3070 goto out_error;
3071
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003072out_error_open:
3073 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3074
3075out_error:
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003076 fprintf(trace->output, "%s\n", errbuf);
Ramkumar Ramachandra87f91862013-10-04 10:47:31 +05303077 goto out_delete_evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003078
3079out_error_apply_filters:
3080 fprintf(trace->output,
3081 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3082 evsel->filter, perf_evsel__name(evsel), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03003083 str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003084 goto out_delete_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003085}
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03003086out_error_mem:
3087 fprintf(trace->output, "Not enough memory to run!\n");
3088 goto out_delete_evlist;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003089
3090out_errno:
3091 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3092 goto out_delete_evlist;
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003093}
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003094
David Ahern6810fc92013-08-28 22:29:52 -06003095static int trace__replay(struct trace *trace)
3096{
3097 const struct perf_evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003098 { "probe:vfs_getname", trace__vfs_getname, },
David Ahern6810fc92013-08-28 22:29:52 -06003099 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003100 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003101 .file = {
3102 .path = input_name,
3103 },
3104 .mode = PERF_DATA_MODE_READ,
3105 .force = trace->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003106 };
David Ahern6810fc92013-08-28 22:29:52 -06003107 struct perf_session *session;
Namhyung Kim003824e2013-11-12 15:25:00 +09003108 struct perf_evsel *evsel;
David Ahern6810fc92013-08-28 22:29:52 -06003109 int err = -1;
3110
3111 trace->tool.sample = trace__process_sample;
3112 trace->tool.mmap = perf_event__process_mmap;
David Ahern384c6712013-09-22 19:44:58 -06003113 trace->tool.mmap2 = perf_event__process_mmap2;
David Ahern6810fc92013-08-28 22:29:52 -06003114 trace->tool.comm = perf_event__process_comm;
3115 trace->tool.exit = perf_event__process_exit;
3116 trace->tool.fork = perf_event__process_fork;
3117 trace->tool.attr = perf_event__process_attr;
Hari Bathinif3b36142017-03-08 02:11:43 +05303118 trace->tool.tracing_data = perf_event__process_tracing_data;
David Ahern6810fc92013-08-28 22:29:52 -06003119 trace->tool.build_id = perf_event__process_build_id;
Hari Bathinif3b36142017-03-08 02:11:43 +05303120 trace->tool.namespaces = perf_event__process_namespaces;
David Ahern6810fc92013-08-28 22:29:52 -06003121
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003122 trace->tool.ordered_events = true;
David Ahern6810fc92013-08-28 22:29:52 -06003123 trace->tool.ordering_requires_timestamps = true;
3124
3125 /* add tid to output */
3126 trace->multiple_threads = true;
3127
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003128 session = perf_session__new(&data, false, &trace->tool);
David Ahern6810fc92013-08-28 22:29:52 -06003129 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003130 return -1;
David Ahern6810fc92013-08-28 22:29:52 -06003131
David Ahernaa07df62016-11-25 09:29:52 -07003132 if (trace->opts.target.pid)
3133 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3134
3135 if (trace->opts.target.tid)
3136 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3137
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003138 if (symbol__init(&session->header.env) < 0)
Namhyung Kimcb2ffae2014-08-12 15:40:44 +09003139 goto out;
3140
David Ahern8fb598e2013-09-28 13:13:00 -06003141 trace->host = &session->machines.host;
3142
David Ahern6810fc92013-08-28 22:29:52 -06003143 err = perf_session__set_tracepoints_handlers(session, handlers);
3144 if (err)
3145 goto out;
3146
Namhyung Kim003824e2013-11-12 15:25:00 +09003147 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3148 "raw_syscalls:sys_enter");
David Ahern9aca7f12013-12-04 19:41:39 -07003149 /* older kernels have syscalls tp versus raw_syscalls */
3150 if (evsel == NULL)
3151 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3152 "syscalls:sys_enter");
David Ahern6810fc92013-08-28 22:29:52 -06003153
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003154 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003155 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003156 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003157 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3158 goto out;
3159 }
3160
3161 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3162 "raw_syscalls:sys_exit");
David Ahern9aca7f12013-12-04 19:41:39 -07003163 if (evsel == NULL)
3164 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3165 "syscalls:sys_exit");
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003166 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003167 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003168 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003169 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
David Ahern6810fc92013-08-28 22:29:52 -06003170 goto out;
3171 }
3172
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003173 evlist__for_each_entry(session->evlist, evsel) {
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003174 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
3175 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3176 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3177 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3178 evsel->handler = trace__pgfault;
3179 }
3180
David Ahern6810fc92013-08-28 22:29:52 -06003181 setup_pager();
3182
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03003183 err = perf_session__process_events(session);
David Ahern6810fc92013-08-28 22:29:52 -06003184 if (err)
3185 pr_err("Failed to process events, error %d", err);
3186
David Ahernbf2575c2013-10-08 21:26:53 -06003187 else if (trace->summary)
3188 trace__fprintf_thread_summary(trace, trace->output);
3189
David Ahern6810fc92013-08-28 22:29:52 -06003190out:
3191 perf_session__delete(session);
3192
3193 return err;
3194}
3195
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003196static size_t trace__fprintf_threads_header(FILE *fp)
3197{
3198 size_t printed;
3199
Pekka Enberg99ff7152013-11-12 16:42:14 +02003200 printed = fprintf(fp, "\n Summary of events:\n\n");
David Ahernbf2575c2013-10-08 21:26:53 -06003201
3202 return printed;
3203}
3204
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003205DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3206 struct stats *stats;
3207 double msecs;
3208 int syscall;
3209)
3210{
3211 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3212 struct stats *stats = source->priv;
3213
3214 entry->syscall = source->i;
3215 entry->stats = stats;
3216 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3217}
3218
David Ahernbf2575c2013-10-08 21:26:53 -06003219static size_t thread__dump_stats(struct thread_trace *ttrace,
3220 struct trace *trace, FILE *fp)
3221{
David Ahernbf2575c2013-10-08 21:26:53 -06003222 size_t printed = 0;
3223 struct syscall *sc;
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003224 struct rb_node *nd;
3225 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003226
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003227 if (syscall_stats == NULL)
David Ahernbf2575c2013-10-08 21:26:53 -06003228 return 0;
3229
3230 printed += fprintf(fp, "\n");
3231
Milian Wolff834fd462015-08-06 11:24:29 +02003232 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3233 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3234 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
Pekka Enberg99ff7152013-11-12 16:42:14 +02003235
Arnaldo Carvalho de Melo98a91832016-06-23 11:34:10 -03003236 resort_rb__for_each_entry(nd, syscall_stats) {
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003237 struct stats *stats = syscall_stats_entry->stats;
David Ahernbf2575c2013-10-08 21:26:53 -06003238 if (stats) {
3239 double min = (double)(stats->min) / NSEC_PER_MSEC;
3240 double max = (double)(stats->max) / NSEC_PER_MSEC;
3241 double avg = avg_stats(stats);
3242 double pct;
3243 u64 n = (u64) stats->n;
3244
3245 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3246 avg /= NSEC_PER_MSEC;
3247
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003248 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
Pekka Enberg99ff7152013-11-12 16:42:14 +02003249 printed += fprintf(fp, " %-15s", sc->name);
Milian Wolff834fd462015-08-06 11:24:29 +02003250 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003251 n, syscall_stats_entry->msecs, min, avg);
Pekka Enberg27a778b2013-11-13 14:21:48 +02003252 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
David Ahernbf2575c2013-10-08 21:26:53 -06003253 }
David Ahernbf2575c2013-10-08 21:26:53 -06003254 }
3255
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003256 resort_rb__delete(syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003257 printed += fprintf(fp, "\n\n");
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003258
3259 return printed;
3260}
3261
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003262static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
David Ahern896cbb52013-09-28 13:12:59 -06003263{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003264 size_t printed = 0;
Namhyung Kim89dceb22014-10-06 09:46:03 +09003265 struct thread_trace *ttrace = thread__priv(thread);
David Ahern896cbb52013-09-28 13:12:59 -06003266 double ratio;
3267
3268 if (ttrace == NULL)
3269 return 0;
3270
3271 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3272
Pekka Enberg15e65c62013-11-14 18:43:30 +02003273 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
Pekka Enberg99ff7152013-11-12 16:42:14 +02003274 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
Pekka Enberg15e65c62013-11-14 18:43:30 +02003275 printed += fprintf(fp, "%.1f%%", ratio);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04003276 if (ttrace->pfmaj)
3277 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3278 if (ttrace->pfmin)
3279 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
Arnaldo Carvalho de Melo03548eb2016-05-05 15:46:50 -03003280 if (trace->sched)
3281 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3282 else if (fputc('\n', fp) != EOF)
3283 ++printed;
3284
David Ahernbf2575c2013-10-08 21:26:53 -06003285 printed += thread__dump_stats(ttrace, trace, fp);
David Ahern896cbb52013-09-28 13:12:59 -06003286
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003287 return printed;
3288}
David Ahern896cbb52013-09-28 13:12:59 -06003289
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003290static unsigned long thread__nr_events(struct thread_trace *ttrace)
3291{
3292 return ttrace ? ttrace->nr_events : 0;
3293}
3294
3295DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3296 struct thread *thread;
3297)
3298{
3299 entry->thread = rb_entry(nd, struct thread, rb_node);
David Ahern896cbb52013-09-28 13:12:59 -06003300}
3301
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003302static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3303{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003304 size_t printed = trace__fprintf_threads_header(fp);
3305 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -07003306 int i;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003307
Kan Liang91e467b2017-09-10 19:23:14 -07003308 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3309 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3310
3311 if (threads == NULL) {
3312 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3313 return 0;
3314 }
3315
3316 resort_rb__for_each_entry(nd, threads)
3317 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3318
3319 resort_rb__delete(threads);
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003320 }
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003321 return printed;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003322}
3323
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003324static int trace__set_duration(const struct option *opt, const char *str,
3325 int unset __maybe_unused)
3326{
3327 struct trace *trace = opt->value;
3328
3329 trace->duration_filter = atof(str);
3330 return 0;
3331}
3332
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003333static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3334 int unset __maybe_unused)
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08003335{
3336 int ret = -1;
3337 size_t i;
3338 struct trace *trace = opt->value;
3339 /*
3340 * FIXME: introduce a intarray class, plain parse csv and create a
3341 * { int nr, int entries[] } struct...
3342 */
3343 struct intlist *list = intlist__new(str);
3344
3345 if (list == NULL)
3346 return -1;
3347
3348 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3349 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3350
3351 if (trace->filter_pids.entries == NULL)
3352 goto out;
3353
3354 trace->filter_pids.entries[0] = getpid();
3355
3356 for (i = 1; i < trace->filter_pids.nr; ++i)
3357 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3358
3359 intlist__delete(list);
3360 ret = 0;
3361out:
3362 return ret;
3363}
3364
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003365static int trace__open_output(struct trace *trace, const char *filename)
3366{
3367 struct stat st;
3368
3369 if (!stat(filename, &st) && st.st_size) {
3370 char oldname[PATH_MAX];
3371
3372 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3373 unlink(oldname);
3374 rename(filename, oldname);
3375 }
3376
3377 trace->output = fopen(filename, "w");
3378
3379 return trace->output == NULL ? -errno : 0;
3380}
3381
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003382static int parse_pagefaults(const struct option *opt, const char *str,
3383 int unset __maybe_unused)
3384{
3385 int *trace_pgfaults = opt->value;
3386
3387 if (strcmp(str, "all") == 0)
3388 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3389 else if (strcmp(str, "maj") == 0)
3390 *trace_pgfaults |= TRACE_PFMAJ;
3391 else if (strcmp(str, "min") == 0)
3392 *trace_pgfaults |= TRACE_PFMIN;
3393 else
3394 return -1;
3395
3396 return 0;
3397}
3398
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003399static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3400{
3401 struct perf_evsel *evsel;
3402
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003403 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003404 evsel->handler = handler;
3405}
3406
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003407static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3408{
3409 struct perf_evsel *evsel;
3410
3411 evlist__for_each_entry(evlist, evsel) {
3412 if (evsel->priv || !evsel->tp_format)
3413 continue;
3414
3415 if (strcmp(evsel->tp_format->system, "syscalls"))
3416 continue;
3417
3418 if (perf_evsel__init_syscall_tp(evsel))
3419 return -1;
3420
3421 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3422 struct syscall_tp *sc = evsel->priv;
3423
3424 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3425 return -1;
3426 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3427 struct syscall_tp *sc = evsel->priv;
3428
3429 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3430 return -1;
3431 }
3432 }
3433
3434 return 0;
3435}
3436
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003437/*
3438 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3439 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3440 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3441 *
3442 * It'd be better to introduce a parse_options() variant that would return a
3443 * list with the terms it didn't match to an event...
3444 */
3445static int trace__parse_events_option(const struct option *opt, const char *str,
3446 int unset __maybe_unused)
3447{
3448 struct trace *trace = (struct trace *)opt->value;
3449 const char *s = str;
3450 char *sep = NULL, *lists[2] = { NULL, NULL, };
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003451 int len = strlen(str) + 1, err = -1, list, idx;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003452 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3453 char group_name[PATH_MAX];
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003454 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003455
3456 if (strace_groups_dir == NULL)
3457 return -1;
3458
3459 if (*s == '!') {
3460 ++s;
3461 trace->not_ev_qualifier = true;
3462 }
3463
3464 while (1) {
3465 if ((sep = strchr(s, ',')) != NULL)
3466 *sep = '\0';
3467
3468 list = 0;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003469 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3470 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003471 list = 1;
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003472 goto do_concat;
3473 }
3474
3475 fmt = syscall_fmt__find_by_alias(s);
3476 if (fmt != NULL) {
3477 list = 1;
3478 s = fmt->name;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003479 } else {
3480 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3481 if (access(group_name, R_OK) == 0)
3482 list = 1;
3483 }
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003484do_concat:
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003485 if (lists[list]) {
3486 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3487 } else {
3488 lists[list] = malloc(len);
3489 if (lists[list] == NULL)
3490 goto out;
3491 strcpy(lists[list], s);
3492 }
3493
3494 if (!sep)
3495 break;
3496
3497 *sep = ',';
3498 s = sep + 1;
3499 }
3500
3501 if (lists[1] != NULL) {
3502 struct strlist_config slist_config = {
3503 .dirname = strace_groups_dir,
3504 };
3505
3506 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3507 if (trace->ev_qualifier == NULL) {
3508 fputs("Not enough memory to parse event qualifier", trace->output);
3509 goto out;
3510 }
3511
3512 if (trace__validate_ev_qualifier(trace))
3513 goto out;
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003514 trace->trace_syscalls = true;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003515 }
3516
3517 err = 0;
3518
3519 if (lists[0]) {
3520 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3521 "event selector. use 'perf list' to list available events",
3522 parse_events_option);
3523 err = parse_events_option(&o, lists[0], 0);
3524 }
3525out:
3526 if (sep)
3527 *sep = ',';
3528
3529 return err;
3530}
3531
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003532static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3533{
3534 struct trace *trace = opt->value;
3535
3536 if (!list_empty(&trace->evlist->entries))
3537 return parse_cgroups(opt, str, unset);
3538
3539 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3540
3541 return 0;
3542}
3543
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003544static struct bpf_map *bpf__find_map_by_name(const char *name)
3545{
3546 struct bpf_object *obj, *tmp;
3547
3548 bpf_object__for_each_safe(obj, tmp) {
3549 struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3550 if (map)
3551 return map;
3552
3553 }
3554
3555 return NULL;
3556}
3557
3558static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3559{
3560 trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3561}
3562
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003563static void trace__set_bpf_map_syscalls(struct trace *trace)
3564{
3565 trace->syscalls.map = bpf__find_map_by_name("syscalls");
3566}
3567
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003568static int trace__config(const char *var, const char *value, void *arg)
3569{
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03003570 struct trace *trace = arg;
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003571 int err = 0;
3572
3573 if (!strcmp(var, "trace.add_events")) {
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003574 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3575 "event selector. use 'perf list' to list available events",
3576 parse_events_option);
3577 err = parse_events_option(&o, value, 0);
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03003578 } else if (!strcmp(var, "trace.show_timestamp")) {
3579 trace->show_tstamp = perf_config_bool(var, value);
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03003580 } else if (!strcmp(var, "trace.show_duration")) {
3581 trace->show_duration = perf_config_bool(var, value);
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003582 } else if (!strcmp(var, "trace.show_arg_names")) {
3583 trace->show_arg_names = perf_config_bool(var, value);
3584 if (!trace->show_arg_names)
3585 trace->show_zeros = true;
Arnaldo Carvalho de Meloe7c634f2018-12-14 10:12:09 -03003586 } else if (!strcmp(var, "trace.show_zeros")) {
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003587 bool new_show_zeros = perf_config_bool(var, value);
3588 if (!trace->show_arg_names && !new_show_zeros) {
3589 pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
3590 goto out;
3591 }
3592 trace->show_zeros = new_show_zeros;
Arnaldo Carvalho de Meloc65c83f2018-12-14 17:06:47 -03003593 } else if (!strcmp(var, "trace.show_prefix")) {
3594 trace->show_string_prefix = perf_config_bool(var, value);
Arnaldo Carvalho de Melod32de872018-12-14 11:06:16 -03003595 } else if (!strcmp(var, "trace.no_inherit")) {
3596 trace->opts.no_inherit = perf_config_bool(var, value);
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -03003597 } else if (!strcmp(var, "trace.args_alignment")) {
3598 int args_alignment = 0;
3599 if (perf_config_int(&args_alignment, var, value) == 0)
3600 trace->args_alignment = args_alignment;
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003601 }
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003602out:
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003603 return err;
3604}
3605
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003606int cmd_trace(int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003607{
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003608 const char *trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +09003609 "perf trace [<options>] [<command>]",
3610 "perf trace [<options>] -- <command> [<options>]",
David Ahern5e2485b2013-09-28 13:13:01 -06003611 "perf trace record [<options>] [<command>]",
3612 "perf trace record [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003613 NULL
3614 };
3615 struct trace trace = {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003616 .syscalls = {
3617 . max = -1,
3618 },
3619 .opts = {
3620 .target = {
3621 .uid = UINT_MAX,
3622 .uses_mmap = true,
3623 },
3624 .user_freq = UINT_MAX,
3625 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03003626 .no_buffering = true,
Arnaldo Carvalho de Melo38d54472014-12-12 17:28:32 -03003627 .mmap_pages = UINT_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003628 },
Milian Wolff007d66a2015-08-05 16:52:23 -03003629 .output = stderr,
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003630 .show_comm = true,
Arnaldo Carvalho de Melob0361462018-12-14 11:17:19 -03003631 .show_tstamp = true,
Arnaldo Carvalho de Melo42e4a522018-12-14 10:22:18 -03003632 .show_duration = true,
Arnaldo Carvalho de Melo9d6dc172018-12-14 11:32:02 -03003633 .show_arg_names = true,
Arnaldo Carvalho de Melo9ed45d52018-12-14 13:40:49 -03003634 .args_alignment = 70,
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003635 .trace_syscalls = false,
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003636 .kernel_syscallchains = false,
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003637 .max_stack = UINT_MAX,
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003638 .max_events = ULONG_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003639 };
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003640 const char *output_name = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003641 const struct option trace_options[] = {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003642 OPT_CALLBACK('e', "event", &trace, "event",
3643 "event/syscall selector. use 'perf list' to list available events",
3644 trace__parse_events_option),
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003645 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3646 "show the thread COMM next to its id"),
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003647 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003648 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3649 trace__parse_events_option),
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003650 OPT_STRING('o', "output", &output_name, "file", "output file name"),
David Ahern6810fc92013-08-28 22:29:52 -06003651 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003652 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3653 "trace events on existing process id"),
David Ahernac9be8e2013-08-20 11:15:45 -06003654 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003655 "trace events on existing thread id"),
Arnaldo Carvalho de Melofa0e4ff2015-04-23 11:59:20 -03003656 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003657 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
David Ahernac9be8e2013-08-20 11:15:45 -06003658 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003659 "system-wide collection from all CPUs"),
David Ahernac9be8e2013-08-20 11:15:45 -06003660 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003661 "list of cpus to monitor"),
David Ahern6810fc92013-08-28 22:29:52 -06003662 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003663 "child tasks do not inherit counters"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02003664 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3665 "number of mmap data pages",
3666 perf_evlist__parse_mmap_pages),
David Ahernac9be8e2013-08-20 11:15:45 -06003667 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003668 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003669 OPT_CALLBACK(0, "duration", &trace, "float",
3670 "show only events with duration > N.M ms",
3671 trace__set_duration),
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003672 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03003673 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
David Ahern4bb09192013-09-04 12:37:43 -06003674 OPT_BOOLEAN('T', "time", &trace.full_time,
3675 "Show full timestamp, not time relative to first start"),
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03003676 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3677 "Show only syscalls that failed"),
David Ahernfd2eaba2013-11-12 09:31:15 -07003678 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3679 "Show only syscall summary with statistics"),
3680 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3681 "Show all syscalls and summary with statistics"),
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003682 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3683 "Trace pagefaults", parse_pagefaults, "maj"),
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003684 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
Yunlong Songe366a6d2015-04-02 21:47:18 +08003685 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
Milian Wolff566a0882016-04-08 13:34:15 +02003686 OPT_CALLBACK(0, "call-graph", &trace.opts,
3687 "record_mode[,record_size]", record_callchain_help,
3688 &record_parse_callchain_opt),
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003689 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3690 "Show the kernel callchains on the syscall exit path"),
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003691 OPT_ULONG(0, "max-events", &trace.max_events,
3692 "Set the maximum number of events to print, exit after that is reached. "),
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03003693 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3694 "Set the minimum stack depth when parsing the callchain, "
3695 "anything below the specified depth will be ignored."),
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -03003696 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3697 "Set the maximum stack depth when parsing the callchain, "
3698 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003699 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003700 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
3701 "Sort batch of events before processing, use if getting out of order events"),
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03003702 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3703 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08003704 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04003705 "per thread proc mmap processing timeout in ms"),
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003706 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3707 trace__parse_cgroups),
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003708 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3709 "ms to wait before starting measurement after program "
3710 "start"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003711 OPT_END()
3712 };
Arnaldo Carvalho de Meloccd62a82016-04-16 09:36:32 -03003713 bool __maybe_unused max_stack_user_set = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003714 bool mmap_pages_user_set = true;
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003715 struct perf_evsel *evsel;
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003716 const char * const trace_subcommands[] = { "record", NULL };
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003717 int err = -1;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003718 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003719
Arnaldo Carvalho de Melo4d08cb82015-02-24 15:35:55 -03003720 signal(SIGSEGV, sighandler_dump_stack);
3721 signal(SIGFPE, sighandler_dump_stack);
3722
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003723 trace.evlist = perf_evlist__new();
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003724 trace.sctbl = syscalltbl__new();
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003725
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003726 if (trace.evlist == NULL || trace.sctbl == NULL) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003727 pr_err("Not enough memory to run!\n");
He Kuangff8f6952015-05-11 12:28:36 +00003728 err = -ENOMEM;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003729 goto out;
3730 }
3731
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003732 err = perf_config(trace__config, &trace);
3733 if (err)
3734 goto out;
3735
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003736 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3737 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
David Ahernfd2eaba2013-11-12 09:31:15 -07003738
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003739 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3740 usage_with_options_msg(trace_usage, trace_options,
3741 "cgroup monitoring only available in system-wide mode");
3742 }
3743
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003744 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3745 if (IS_ERR(evsel)) {
3746 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
Arnaldo Carvalho de Meloe0b6d2e2018-08-07 15:40:13 -03003747 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3748 goto out;
3749 }
3750
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003751 if (evsel) {
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003752 trace.syscalls.events.augmented = evsel;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003753 trace__set_bpf_map_filtered_pids(&trace);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003754 trace__set_bpf_map_syscalls(&trace);
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003755 }
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003756
Wang Nand7888572016-04-08 15:07:24 +00003757 err = bpf__setup_stdout(trace.evlist);
3758 if (err) {
3759 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3760 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3761 goto out;
3762 }
3763
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003764 err = -1;
3765
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003766 if (trace.trace_pgfaults) {
3767 trace.opts.sample_address = true;
3768 trace.opts.sample_time = true;
3769 }
3770
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003771 if (trace.opts.mmap_pages == UINT_MAX)
3772 mmap_pages_user_set = false;
3773
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003774 if (trace.max_stack == UINT_MAX) {
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03003775 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003776 max_stack_user_set = false;
3777 }
3778
3779#ifdef HAVE_DWARF_UNWIND_SUPPORT
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003780 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003781 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003782 }
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003783#endif
3784
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03003785 if (callchain_param.enabled) {
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003786 if (!mmap_pages_user_set && geteuid() == 0)
3787 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3788
Milian Wolff566a0882016-04-08 13:34:15 +02003789 symbol_conf.use_callchain = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003790 }
Milian Wolff566a0882016-04-08 13:34:15 +02003791
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003792 if (trace.evlist->nr_entries > 0) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003793 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003794 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3795 perror("failed to set syscalls:* tracepoint fields");
3796 goto out;
3797 }
3798 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003799
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003800 if (trace.sort_events) {
3801 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
3802 ordered_events__set_copy_on_queue(&trace.oe.data, true);
3803 }
Jiri Olsa028713a2018-12-05 17:05:09 +01003804
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003805 /*
3806 * If we are augmenting syscalls, then combine what we put in the
3807 * __augmented_syscalls__ BPF map with what is in the
3808 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3809 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3810 *
3811 * We'll switch to look at two BPF maps, one for sys_enter and the
3812 * other for sys_exit when we start augmenting the sys_exit paths with
3813 * buffers that are being copied from kernel to userspace, think 'read'
3814 * syscall.
3815 */
3816 if (trace.syscalls.events.augmented) {
3817 evsel = trace.syscalls.events.augmented;
3818
3819 if (perf_evsel__init_augmented_syscall_tp(evsel) ||
3820 perf_evsel__init_augmented_syscall_tp_args(evsel))
3821 goto out;
3822 evsel->handler = trace__sys_enter;
3823
3824 evlist__for_each_entry(trace.evlist, evsel) {
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003825 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3826
3827 if (raw_syscalls_sys_exit) {
3828 trace.raw_augmented_syscalls = true;
3829 goto init_augmented_syscall_tp;
3830 }
3831
3832 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
3833init_augmented_syscall_tp:
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003834 perf_evsel__init_augmented_syscall_tp(evsel);
3835 perf_evsel__init_augmented_syscall_tp_ret(evsel);
3836 evsel->handler = trace__sys_exit;
3837 }
3838 }
3839 }
3840
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003841 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3842 return trace__record(&trace, argc-1, &argv[1]);
3843
3844 /* summary_only implies summary option, but don't overwrite summary if set */
3845 if (trace.summary_only)
3846 trace.summary = trace.summary_only;
3847
Arnaldo Carvalho de Melo726f3232015-02-06 10:16:45 +01003848 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3849 trace.evlist->nr_entries == 0 /* Was --events used? */) {
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003850 trace.trace_syscalls = true;
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003851 }
3852
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003853 if (output_name != NULL) {
3854 err = trace__open_output(&trace, output_name);
3855 if (err < 0) {
3856 perror("failed to create output file");
3857 goto out;
3858 }
3859 }
3860
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003861 err = target__validate(&trace.opts.target);
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003862 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003863 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003864 fprintf(trace.output, "%s", bf);
3865 goto out_close;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003866 }
3867
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003868 err = target__parse_uid(&trace.opts.target);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003869 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003870 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003871 fprintf(trace.output, "%s", bf);
3872 goto out_close;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003873 }
3874
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003875 if (!argc && target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +09003876 trace.opts.target.system_wide = true;
3877
David Ahern6810fc92013-08-28 22:29:52 -06003878 if (input_name)
3879 err = trace__replay(&trace);
3880 else
3881 err = trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003882
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003883out_close:
3884 if (output_name != NULL)
3885 fclose(trace.output);
3886out:
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003887 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003888}