blob: a6aa4589ad5063265f0e7fbd813de29e3771fe97 [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 Melo7c304ee02013-08-22 16:49:54 -030025#include "util/debug.h"
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +010026#include "util/env.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030027#include "util/event.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030028#include "util/evlist.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060029#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030030#include "util/machine.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030031#include "util/path.h"
David Ahern6810fc92013-08-28 22:29:52 -060032#include "util/session.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030033#include "util/thread.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060034#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -030035#include "util/strlist.h"
David Ahernbdc89662013-08-28 22:29:53 -060036#include "util/intlist.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030037#include "util/thread_map.h"
David Ahernbf2575c2013-10-08 21:26:53 -060038#include "util/stat.h"
Arnaldo Carvalho de Melofd5cead2017-03-14 16:19:30 -030039#include "trace/beauty/beauty.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010040#include "trace-event.h"
David Ahern9aca7f12013-12-04 19:41:39 -070041#include "util/parse-events.h"
Wang Nanba504232016-02-26 09:31:54 +000042#include "util/bpf-loader.h"
Milian Wolff566a0882016-04-08 13:34:15 +020043#include "callchain.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030044#include "print_binary.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030045#include "string2.h"
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030046#include "syscalltbl.h"
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -030047#include "rb_resort.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030048
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030049#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030050#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030051#include <poll.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030052#include <signal.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030053#include <stdlib.h>
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -030054#include <string.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020055#include <linux/err.h>
Arnaldo Carvalho de Melo997bba82016-03-30 19:43:32 -030056#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030057#include <linux/kernel.h>
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -030058#include <linux/random.h>
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -030059#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030060#include <linux/time64.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030061#include <fcntl.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030062
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030063#include "sane_ctype.h"
64
Arnaldo Carvalho de Meloc188e7a2015-05-14 17:39:03 -030065#ifndef O_CLOEXEC
66# define O_CLOEXEC 02000000
67#endif
68
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -030069#ifndef F_LINUX_SPECIFIC_BASE
70# define F_LINUX_SPECIFIC_BASE 1024
71#endif
72
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030073struct trace {
74 struct perf_tool tool;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030075 struct syscalltbl *sctbl;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030076 struct {
77 int max;
78 struct syscall *table;
79 struct {
80 struct perf_evsel *sys_enter,
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -030081 *sys_exit,
82 *augmented;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030083 } events;
84 } syscalls;
85 struct record_opts opts;
86 struct perf_evlist *evlist;
87 struct machine *host;
88 struct thread *current;
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030089 struct cgroup *cgroup;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030090 u64 base_time;
91 FILE *output;
92 unsigned long nr_events;
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -030093 unsigned long nr_events_printed;
94 unsigned long max_events;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030095 struct strlist *ev_qualifier;
96 struct {
97 size_t nr;
98 int *entries;
99 } ev_qualifier_ids;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300100 struct {
101 size_t nr;
102 pid_t *entries;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -0300103 struct bpf_map *map;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300104 } filter_pids;
105 double duration_filter;
106 double runtime_ms;
107 struct {
108 u64 vfs_getname,
109 proc_getname;
110 } stats;
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -0300111 unsigned int max_stack;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -0300112 unsigned int min_stack;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -0300113 bool raw_augmented_syscalls;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300114 bool not_ev_qualifier;
115 bool live;
116 bool full_time;
117 bool sched;
118 bool multiple_threads;
119 bool summary;
120 bool summary_only;
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -0300121 bool failure_only;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300122 bool show_comm;
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -0300123 bool print_sample;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300124 bool show_tool_stats;
125 bool trace_syscalls;
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -0300126 bool kernel_syscallchains;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300127 bool force;
128 bool vfs_getname;
129 int trace_pgfaults;
130};
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300131
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300132struct tp_field {
133 int offset;
134 union {
135 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
136 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
137 };
138};
139
140#define TP_UINT_FIELD(bits) \
141static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
142{ \
David Ahern55d43bca2015-02-19 15:00:22 -0500143 u##bits value; \
144 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
145 return value; \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300146}
147
148TP_UINT_FIELD(8);
149TP_UINT_FIELD(16);
150TP_UINT_FIELD(32);
151TP_UINT_FIELD(64);
152
153#define TP_UINT_FIELD__SWAPPED(bits) \
154static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
155{ \
David Ahern55d43bca2015-02-19 15:00:22 -0500156 u##bits value; \
157 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300158 return bswap_##bits(value);\
159}
160
161TP_UINT_FIELD__SWAPPED(16);
162TP_UINT_FIELD__SWAPPED(32);
163TP_UINT_FIELD__SWAPPED(64);
164
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300165static 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 -0300166{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300167 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300168
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300169 switch (size) {
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300170 case 1:
171 field->integer = tp_field__u8;
172 break;
173 case 2:
174 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
175 break;
176 case 4:
177 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
178 break;
179 case 8:
180 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
181 break;
182 default:
183 return -1;
184 }
185
186 return 0;
187}
188
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400189static 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 -0300190{
191 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
192}
193
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300194static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
195{
196 return sample->raw_data + field->offset;
197}
198
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300199static int __tp_field__init_ptr(struct tp_field *field, int offset)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300200{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300201 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300202 field->pointer = tp_field__ptr;
203 return 0;
204}
205
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400206static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300207{
208 return __tp_field__init_ptr(field, format_field->offset);
209}
210
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300211struct syscall_tp {
212 struct tp_field id;
213 union {
214 struct tp_field args, ret;
215 };
216};
217
218static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
219 struct tp_field *field,
220 const char *name)
221{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400222 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300223
224 if (format_field == NULL)
225 return -1;
226
227 return tp_field__init_uint(field, format_field, evsel->needs_swap);
228}
229
230#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
231 ({ struct syscall_tp *sc = evsel->priv;\
232 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
233
234static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
235 struct tp_field *field,
236 const char *name)
237{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400238 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300239
240 if (format_field == NULL)
241 return -1;
242
243 return tp_field__init_ptr(field, format_field);
244}
245
246#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
247 ({ struct syscall_tp *sc = evsel->priv;\
248 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
249
250static void perf_evsel__delete_priv(struct perf_evsel *evsel)
251{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300252 zfree(&evsel->priv);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300253 perf_evsel__delete(evsel);
254}
255
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300256static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
257{
258 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
259
260 if (evsel->priv != NULL) {
261 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr"))
262 goto out_delete;
263 return 0;
264 }
265
266 return -ENOMEM;
267out_delete:
268 zfree(&evsel->priv);
269 return -ENOENT;
270}
271
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300272static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel)
273{
274 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
275
276 if (evsel->priv != NULL) { /* field, sizeof_field, offsetof_field */
277 if (__tp_field__init_uint(&sc->id, sizeof(long), sizeof(long long), evsel->needs_swap))
278 goto out_delete;
279
280 return 0;
281 }
282
283 return -ENOMEM;
284out_delete:
285 zfree(&evsel->priv);
286 return -EINVAL;
287}
288
289static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
290{
291 struct syscall_tp *sc = evsel->priv;
292
293 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
294}
295
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -0300296static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
297{
298 struct syscall_tp *sc = evsel->priv;
299
300 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
301}
302
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300303static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
Namhyung Kim96695d42013-11-12 08:51:45 -0300304{
305 evsel->priv = malloc(sizeof(struct syscall_tp));
306 if (evsel->priv != NULL) {
307 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
308 goto out_delete;
309
310 evsel->handler = handler;
311 return 0;
312 }
313
314 return -ENOMEM;
315
316out_delete:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300317 zfree(&evsel->priv);
Namhyung Kim96695d42013-11-12 08:51:45 -0300318 return -ENOENT;
319}
320
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300321static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300322{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300323 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300324
David Ahern9aca7f12013-12-04 19:41:39 -0700325 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200326 if (IS_ERR(evsel))
David Ahern9aca7f12013-12-04 19:41:39 -0700327 evsel = perf_evsel__newtp("syscalls", direction);
328
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200329 if (IS_ERR(evsel))
330 return NULL;
331
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300332 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200333 goto out_delete;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300334
335 return evsel;
336
337out_delete:
338 perf_evsel__delete_priv(evsel);
339 return NULL;
340}
341
342#define perf_evsel__sc_tp_uint(evsel, name, sample) \
343 ({ struct syscall_tp *fields = evsel->priv; \
344 fields->name.integer(&fields->name, sample); })
345
346#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
347 ({ struct syscall_tp *fields = evsel->priv; \
348 fields->name.pointer(&fields->name, sample); })
349
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300350size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, int val)
351{
352 int idx = val - sa->offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300353
Arnaldo Carvalho de Melobc972ad2018-07-26 15:30:33 -0300354 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL)
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300355 return scnprintf(bf, size, intfmt, val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300356
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300357 return scnprintf(bf, size, "%s", sa->entries[idx]);
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300358}
359
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300360static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
361 const char *intfmt,
362 struct syscall_arg *arg)
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300363{
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300364 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300365}
366
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300367static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
368 struct syscall_arg *arg)
369{
370 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
371}
372
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300373#define SCA_STRARRAY syscall_arg__scnprintf_strarray
374
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300375struct strarrays {
376 int nr_entries;
377 struct strarray **entries;
378};
379
380#define DEFINE_STRARRAYS(array) struct strarrays strarrays__##array = { \
381 .nr_entries = ARRAY_SIZE(array), \
382 .entries = array, \
383}
384
Arnaldo Carvalho de Melo274e86f2017-07-14 09:38:38 -0300385size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
386 struct syscall_arg *arg)
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300387{
388 struct strarrays *sas = arg->parm;
389 int i;
390
391 for (i = 0; i < sas->nr_entries; ++i) {
392 struct strarray *sa = sas->entries[i];
393 int idx = arg->val - sa->offset;
394
395 if (idx >= 0 && idx < sa->nr_entries) {
396 if (sa->entries[idx] == NULL)
397 break;
398 return scnprintf(bf, size, "%s", sa->entries[idx]);
399 }
400 }
401
402 return scnprintf(bf, size, "%d", arg->val);
403}
404
Arnaldo Carvalho de Melo48e1f912016-07-05 14:43:27 -0300405#ifndef AT_FDCWD
406#define AT_FDCWD -100
407#endif
408
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300409static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
410 struct syscall_arg *arg)
411{
412 int fd = arg->val;
413
414 if (fd == AT_FDCWD)
415 return scnprintf(bf, size, "CWD");
416
417 return syscall_arg__scnprintf_fd(bf, size, arg);
418}
419
420#define SCA_FDAT syscall_arg__scnprintf_fd_at
421
422static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
423 struct syscall_arg *arg);
424
425#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
426
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300427size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300428{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300429 return scnprintf(bf, size, "%#lx", arg->val);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300430}
431
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300432size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300433{
434 return scnprintf(bf, size, "%d", arg->val);
435}
436
Arnaldo Carvalho de Melo5dde91e2017-07-14 10:34:16 -0300437size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
438{
439 return scnprintf(bf, size, "%ld", arg->val);
440}
441
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300442static const char *bpf_cmd[] = {
443 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
444 "MAP_GET_NEXT_KEY", "PROG_LOAD",
445};
446static DEFINE_STRARRAY(bpf_cmd);
447
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300448static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
449static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1);
Arnaldo Carvalho de Meloeac032c2013-09-20 11:27:32 -0300450
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300451static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
452static DEFINE_STRARRAY(itimers);
453
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300454static const char *keyctl_options[] = {
455 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
456 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
457 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
458 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
459 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
460};
461static DEFINE_STRARRAY(keyctl_options);
462
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300463static const char *whences[] = { "SET", "CUR", "END",
464#ifdef SEEK_DATA
465"DATA",
466#endif
467#ifdef SEEK_HOLE
468"HOLE",
469#endif
470};
471static DEFINE_STRARRAY(whences);
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300472
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300473static const char *fcntl_cmds[] = {
474 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
Arnaldo Carvalho de Meloe000e5e2017-07-13 13:07:00 -0300475 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
476 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
477 "GETOWNER_UIDS",
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300478};
479static DEFINE_STRARRAY(fcntl_cmds);
480
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300481static const char *fcntl_linux_specific_cmds[] = {
482 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
483 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
Arnaldo Carvalho de Melo64e45612017-07-13 17:34:46 -0300484 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300485};
486
487static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, F_LINUX_SPECIFIC_BASE);
488
489static struct strarray *fcntl_cmds_arrays[] = {
490 &strarray__fcntl_cmds,
491 &strarray__fcntl_linux_specific_cmds,
492};
493
494static DEFINE_STRARRAYS(fcntl_cmds_arrays);
495
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300496static const char *rlimit_resources[] = {
497 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
498 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
499 "RTTIME",
500};
501static DEFINE_STRARRAY(rlimit_resources);
502
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300503static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
504static DEFINE_STRARRAY(sighow);
505
David Ahern4f8c1b72013-09-22 19:45:00 -0600506static const char *clockid[] = {
507 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
Arnaldo Carvalho de Melo28ebb872015-08-11 10:38:38 -0300508 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
509 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
David Ahern4f8c1b72013-09-22 19:45:00 -0600510};
511static DEFINE_STRARRAY(clockid);
512
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300513static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
514 struct syscall_arg *arg)
515{
516 size_t printed = 0;
517 int mode = arg->val;
518
519 if (mode == F_OK) /* 0 */
520 return scnprintf(bf, size, "F");
521#define P_MODE(n) \
522 if (mode & n##_OK) { \
523 printed += scnprintf(bf + printed, size - printed, "%s", #n); \
524 mode &= ~n##_OK; \
525 }
526
527 P_MODE(R);
528 P_MODE(W);
529 P_MODE(X);
530#undef P_MODE
531
532 if (mode)
533 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
534
535 return printed;
536}
537
538#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
539
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300540static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
541 struct syscall_arg *arg);
542
543#define SCA_FILENAME syscall_arg__scnprintf_filename
544
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300545static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
546 struct syscall_arg *arg)
547{
548 int printed = 0, flags = arg->val;
549
550#define P_FLAG(n) \
551 if (flags & O_##n) { \
552 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
553 flags &= ~O_##n; \
554 }
555
556 P_FLAG(CLOEXEC);
557 P_FLAG(NONBLOCK);
558#undef P_FLAG
559
560 if (flags)
561 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
562
563 return printed;
564}
565
566#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
567
Arnaldo Carvalho de Meloa355a612016-04-13 11:55:18 -0300568#ifndef GRND_NONBLOCK
569#define GRND_NONBLOCK 0x0001
570#endif
571#ifndef GRND_RANDOM
572#define GRND_RANDOM 0x0002
573#endif
574
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300575static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
576 struct syscall_arg *arg)
577{
578 int printed = 0, flags = arg->val;
579
580#define P_FLAG(n) \
581 if (flags & GRND_##n) { \
582 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
583 flags &= ~GRND_##n; \
584 }
585
586 P_FLAG(RANDOM);
587 P_FLAG(NONBLOCK);
588#undef P_FLAG
589
590 if (flags)
591 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
592
593 return printed;
594}
595
596#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
597
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300598#define STRARRAY(name, array) \
599 { .scnprintf = SCA_STRARRAY, \
600 .parm = &strarray__##array, }
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -0300601
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +0100602#include "trace/beauty/arch_errno_names.c"
Arnaldo Carvalho de Meloea8dc3c2016-04-13 12:10:19 -0300603#include "trace/beauty/eventfd.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300604#include "trace/beauty/futex_op.c"
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300605#include "trace/beauty/futex_val3.c"
Arnaldo Carvalho de Melodf4cb162016-04-13 12:05:44 -0300606#include "trace/beauty/mmap.c"
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -0300607#include "trace/beauty/mode_t.c"
Arnaldo Carvalho de Meloa30e6252016-04-27 19:01:52 -0300608#include "trace/beauty/msg_flags.c"
Arnaldo Carvalho de Melo8f48df62016-05-06 10:02:32 -0300609#include "trace/beauty/open_flags.c"
Arnaldo Carvalho de Melo62de3442016-04-26 11:03:03 -0300610#include "trace/beauty/perf_event_open.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300611#include "trace/beauty/pid.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300612#include "trace/beauty/sched_policy.c"
Arnaldo Carvalho de Melof5cd95e2016-05-11 10:32:20 -0300613#include "trace/beauty/seccomp.c"
Arnaldo Carvalho de Melo12199d82016-05-06 09:58:02 -0300614#include "trace/beauty/signum.c"
Arnaldo Carvalho de Melobbf86c42016-04-14 13:53:10 -0300615#include "trace/beauty/socket_type.c"
Arnaldo Carvalho de Melo7206b902016-04-06 14:11:36 -0300616#include "trace/beauty/waitid_options.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300617
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300618struct syscall_arg_fmt {
619 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -0300620 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300621 void *parm;
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -0300622 const char *name;
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -0300623 bool show_zero;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300624};
625
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300626static struct syscall_fmt {
627 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -0300628 const char *alias;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300629 struct syscall_arg_fmt arg[6];
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -0300630 u8 nr_args;
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300631 bool errpid;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300632 bool timeout;
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -0300633 bool hexret;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300634} syscall_fmts[] = {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300635 { .name = "access",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300636 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
Arnaldo Carvalho de Melo02ef2882018-08-28 17:03:53 -0300637 { .name = "bind",
638 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300639 { .name = "bpf",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300640 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300641 { .name = "brk", .hexret = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300642 .arg = { [0] = { .scnprintf = SCA_HEX, /* brk */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300643 { .name = "clock_gettime",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300644 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
Arnaldo Carvalho de Melo33396a32017-07-19 16:15:17 -0300645 { .name = "clone", .errpid = true, .nr_args = 5,
646 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
647 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
648 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
649 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
650 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300651 { .name = "close",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300652 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
Arnaldo Carvalho de Melod5a7e662018-08-28 16:24:44 -0300653 { .name = "connect",
654 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300655 { .name = "epoll_ctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300656 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300657 { .name = "eventfd2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300658 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300659 { .name = "fchmodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300660 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300661 { .name = "fchownat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300662 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300663 { .name = "fcntl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300664 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
Arnaldo Carvalho de Melo39cc3552017-07-17 16:02:52 -0300665 .parm = &strarrays__fcntl_cmds_arrays,
666 .show_zero = true, },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300667 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300668 { .name = "flock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300669 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300670 { .name = "fstat", .alias = "newfstat", },
671 { .name = "fstatat", .alias = "newfstatat", },
672 { .name = "futex",
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300673 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
674 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300675 { .name = "futimesat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300676 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300677 { .name = "getitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300678 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300679 { .name = "getpid", .errpid = true, },
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300680 { .name = "getpgid", .errpid = true, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300681 { .name = "getppid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300682 { .name = "getrandom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300683 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300684 { .name = "getrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300685 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo2d1073d2018-01-09 12:03:47 -0300686 { .name = "gettid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300687 { .name = "ioctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300688 .arg = {
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300689#if defined(__i386__) || defined(__x86_64__)
690/*
691 * FIXME: Make this available to all arches.
692 */
Arnaldo Carvalho de Melo1cc47f2d2017-07-31 13:20:14 -0300693 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300694 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300695#else
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300696 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300697#endif
Arnaldo Carvalho de Melo1de30382017-10-31 11:32:23 -0300698 { .name = "kcmp", .nr_args = 5,
699 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
700 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
701 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
702 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
703 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300704 { .name = "keyctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300705 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300706 { .name = "kill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300707 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300708 { .name = "linkat",
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 = "lseek",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300711 .arg = { [2] = STRARRAY(whence, whences), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300712 { .name = "lstat", .alias = "newlstat", },
713 { .name = "madvise",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300714 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
715 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300716 { .name = "mkdirat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300717 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300718 { .name = "mknodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300719 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300720 { .name = "mlock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300721 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300722 { .name = "mlockall",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300723 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300724 { .name = "mmap", .hexret = true,
Jiri Olsa54265662017-05-31 13:35:57 +0200725/* The standard mmap maps to old_mmap on s390x */
726#if defined(__s390x__)
727 .alias = "old_mmap",
728#endif
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300729 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
730 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
731 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300732 { .name = "mount",
Arnaldo Carvalho de Melo23c07a22018-10-26 13:51:45 -0300733 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
734 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300735 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300736 { .name = "mprotect",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300737 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
738 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300739 { .name = "mq_unlink",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300740 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300741 { .name = "mremap", .hexret = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300742 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
743 [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ },
744 [4] = { .scnprintf = SCA_HEX, /* new_addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300745 { .name = "munlock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300746 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300747 { .name = "munmap",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300748 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300749 { .name = "name_to_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300750 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300751 { .name = "newfstatat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300752 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300753 { .name = "open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300754 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300755 { .name = "open_by_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300756 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
757 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300758 { .name = "openat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300759 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
760 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300761 { .name = "perf_event_open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300762 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
763 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
764 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300765 { .name = "pipe2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300766 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo83bc9c372017-08-28 11:47:11 -0300767 { .name = "pkey_alloc",
768 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
769 { .name = "pkey_free",
770 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
771 { .name = "pkey_mprotect",
772 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
773 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
774 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300775 { .name = "poll", .timeout = true, },
776 { .name = "ppoll", .timeout = true, },
Arnaldo Carvalho de Melod688d032017-10-26 15:19:35 -0300777 { .name = "prctl", .alias = "arch_prctl",
778 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
779 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
780 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300781 { .name = "pread", .alias = "pread64", },
782 { .name = "preadv", .alias = "pread", },
783 { .name = "prlimit64",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300784 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300785 { .name = "pwrite", .alias = "pwrite64", },
786 { .name = "readlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300787 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300788 { .name = "recvfrom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300789 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300790 { .name = "recvmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300791 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300792 { .name = "recvmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300793 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300794 { .name = "renameat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300795 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300796 { .name = "rt_sigaction",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300797 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300798 { .name = "rt_sigprocmask",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300799 .arg = { [0] = STRARRAY(how, sighow), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300800 { .name = "rt_sigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300801 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300802 { .name = "rt_tgsigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300803 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300804 { .name = "sched_setscheduler",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300805 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300806 { .name = "seccomp",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300807 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
808 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300809 { .name = "select", .timeout = true, },
810 { .name = "sendmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300811 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300812 { .name = "sendmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300813 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300814 { .name = "sendto",
Arnaldo Carvalho de Melo6ebb6862018-08-29 08:47:08 -0300815 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
816 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300817 { .name = "set_tid_address", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300818 { .name = "setitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300819 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300820 { .name = "setrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300821 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300822 { .name = "socket",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300823 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300824 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
825 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300826 { .name = "socketpair",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300827 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300828 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
829 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300830 { .name = "stat", .alias = "newstat", },
831 { .name = "statx",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300832 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
833 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
834 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300835 { .name = "swapoff",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300836 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300837 { .name = "swapon",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300838 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300839 { .name = "symlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300840 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300841 { .name = "tgkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300842 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300843 { .name = "tkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300844 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo476c92ca2018-10-26 13:23:25 -0300845 { .name = "umount2", .alias = "umount",
846 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300847 { .name = "uname", .alias = "newuname", },
848 { .name = "unlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300849 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300850 { .name = "utimensat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300851 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300852 { .name = "wait4", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300853 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300854 { .name = "waitid", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300855 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300856};
857
858static int syscall_fmt__cmp(const void *name, const void *fmtp)
859{
860 const struct syscall_fmt *fmt = fmtp;
861 return strcmp(name, fmt->name);
862}
863
864static struct syscall_fmt *syscall_fmt__find(const char *name)
865{
866 const int nmemb = ARRAY_SIZE(syscall_fmts);
867 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
868}
869
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -0300870static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
871{
872 int i, nmemb = ARRAY_SIZE(syscall_fmts);
873
874 for (i = 0; i < nmemb; ++i) {
875 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
876 return &syscall_fmts[i];
877 }
878
879 return NULL;
880}
881
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300882/*
883 * is_exit: is this "exit" or "exit_group"?
884 * 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 -0300885 * 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 -0300886 */
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300887struct syscall {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500888 struct tep_event *tp_format;
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -0300889 int nr_args;
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -0300890 int args_size;
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300891 bool is_exit;
892 bool is_open;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400893 struct tep_format_field *args;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300894 const char *name;
895 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300896 struct syscall_arg_fmt *arg_fmt;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300897};
898
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300899/*
900 * We need to have this 'calculated' boolean because in some cases we really
901 * don't know what is the duration of a syscall, for instance, when we start
902 * a session and some threads are waiting for a syscall to finish, say 'poll',
903 * in which case all we can do is to print "( ? ) for duration and for the
904 * start timestamp.
905 */
906static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200907{
908 double duration = (double)t / NSEC_PER_MSEC;
909 size_t printed = fprintf(fp, "(");
910
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300911 if (!calculated)
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -0300912 printed += fprintf(fp, " ");
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300913 else if (duration >= 1.0)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200914 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
915 else if (duration >= 0.01)
916 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
917 else
918 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300919 return printed + fprintf(fp, "): ");
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200920}
921
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300922/**
923 * filename.ptr: The filename char pointer that will be vfs_getname'd
924 * filename.entry_str_pos: Where to insert the string translated from
925 * filename.ptr by the vfs_getname tracepoint/kprobe.
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300926 * ret_scnprintf: syscall args may set this to a different syscall return
927 * formatter, for instance, fcntl may return fds, file flags, etc.
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300928 */
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300929struct thread_trace {
930 u64 entry_time;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300931 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300932 unsigned long nr_events;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +0400933 unsigned long pfmaj, pfmin;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300934 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300935 double runtime_ms;
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -0300936 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300937 struct {
938 unsigned long ptr;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -0300939 short int entry_str_pos;
940 bool pending_open;
941 unsigned int namelen;
942 char *name;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300943 } filename;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300944 struct {
945 int max;
946 char **table;
947 } paths;
David Ahernbf2575c2013-10-08 21:26:53 -0600948
949 struct intlist *syscall_stats;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300950};
951
952static struct thread_trace *thread_trace__new(void)
953{
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300954 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
955
956 if (ttrace)
957 ttrace->paths.max = -1;
958
David Ahernbf2575c2013-10-08 21:26:53 -0600959 ttrace->syscall_stats = intlist__new(NULL);
960
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300961 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300962}
963
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300964static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300965{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300966 struct thread_trace *ttrace;
967
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300968 if (thread == NULL)
969 goto fail;
970
Namhyung Kim89dceb22014-10-06 09:46:03 +0900971 if (thread__priv(thread) == NULL)
972 thread__set_priv(thread, thread_trace__new());
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300973
Namhyung Kim89dceb22014-10-06 09:46:03 +0900974 if (thread__priv(thread) == NULL)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300975 goto fail;
976
Namhyung Kim89dceb22014-10-06 09:46:03 +0900977 ttrace = thread__priv(thread);
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300978 ++ttrace->nr_events;
979
980 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300981fail:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300982 color_fprintf(fp, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300983 "WARNING: not enough memory, dropping samples!\n");
984 return NULL;
985}
986
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300987
988void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -0300989 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300990{
991 struct thread_trace *ttrace = thread__priv(arg->thread);
992
993 ttrace->ret_scnprintf = ret_scnprintf;
994}
995
Stanislav Fomichev598d02c2014-06-26 20:14:25 +0400996#define TRACE_PFMAJ (1 << 0)
997#define TRACE_PFMIN (1 << 1)
998
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -0300999static const size_t trace__entry_str_size = 2048;
1000
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001001static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001002{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001003 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001004
1005 if (fd > ttrace->paths.max) {
1006 char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
1007
1008 if (npath == NULL)
1009 return -1;
1010
1011 if (ttrace->paths.max != -1) {
1012 memset(npath + ttrace->paths.max + 1, 0,
1013 (fd - ttrace->paths.max) * sizeof(char *));
1014 } else {
1015 memset(npath, 0, (fd + 1) * sizeof(char *));
1016 }
1017
1018 ttrace->paths.table = npath;
1019 ttrace->paths.max = fd;
1020 }
1021
1022 ttrace->paths.table[fd] = strdup(pathname);
1023
1024 return ttrace->paths.table[fd] != NULL ? 0 : -1;
1025}
1026
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001027static int thread__read_fd_path(struct thread *thread, int fd)
1028{
1029 char linkname[PATH_MAX], pathname[PATH_MAX];
1030 struct stat st;
1031 int ret;
1032
1033 if (thread->pid_ == thread->tid) {
1034 scnprintf(linkname, sizeof(linkname),
1035 "/proc/%d/fd/%d", thread->pid_, fd);
1036 } else {
1037 scnprintf(linkname, sizeof(linkname),
1038 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1039 }
1040
1041 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1042 return -1;
1043
1044 ret = readlink(linkname, pathname, sizeof(pathname));
1045
1046 if (ret < 0 || ret > st.st_size)
1047 return -1;
1048
1049 pathname[ret] = '\0';
1050 return trace__set_fd_pathname(thread, fd, pathname);
1051}
1052
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001053static const char *thread__fd_path(struct thread *thread, int fd,
1054 struct trace *trace)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001055{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001056 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001057
1058 if (ttrace == NULL)
1059 return NULL;
1060
1061 if (fd < 0)
1062 return NULL;
1063
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001064 if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001065 if (!trace->live)
1066 return NULL;
1067 ++trace->stats.proc_getname;
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001068 if (thread__read_fd_path(thread, fd))
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001069 return NULL;
1070 }
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001071
1072 return ttrace->paths.table[fd];
1073}
1074
Arnaldo Carvalho de Melofc65eb82017-07-14 15:21:40 -03001075size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001076{
1077 int fd = arg->val;
1078 size_t printed = scnprintf(bf, size, "%d", fd);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001079 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001080
1081 if (path)
1082 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1083
1084 return printed;
1085}
1086
Arnaldo Carvalho de Melo0a2f75402017-10-31 11:30:09 -03001087size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1088{
1089 size_t printed = scnprintf(bf, size, "%d", fd);
1090 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1091
1092 if (thread) {
1093 const char *path = thread__fd_path(thread, fd, trace);
1094
1095 if (path)
1096 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1097
1098 thread__put(thread);
1099 }
1100
1101 return printed;
1102}
1103
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001104static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1105 struct syscall_arg *arg)
1106{
1107 int fd = arg->val;
1108 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
Namhyung Kim89dceb22014-10-06 09:46:03 +09001109 struct thread_trace *ttrace = thread__priv(arg->thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001110
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001111 if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
1112 zfree(&ttrace->paths.table[fd]);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001113
1114 return printed;
1115}
1116
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001117static void thread__set_filename_pos(struct thread *thread, const char *bf,
1118 unsigned long ptr)
1119{
1120 struct thread_trace *ttrace = thread__priv(thread);
1121
1122 ttrace->filename.ptr = ptr;
1123 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1124}
1125
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001126static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1127{
1128 struct augmented_arg *augmented_arg = arg->augmented.args;
1129
1130 return scnprintf(bf, size, "%.*s", augmented_arg->size, augmented_arg->value);
1131}
1132
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001133static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1134 struct syscall_arg *arg)
1135{
1136 unsigned long ptr = arg->val;
1137
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001138 if (arg->augmented.args)
1139 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1140
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001141 if (!arg->trace->vfs_getname)
1142 return scnprintf(bf, size, "%#x", ptr);
1143
1144 thread__set_filename_pos(arg->thread, bf, ptr);
1145 return 0;
1146}
1147
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001148static bool trace__filter_duration(struct trace *trace, double t)
1149{
1150 return t < (trace->duration_filter * NSEC_PER_MSEC);
1151}
1152
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001153static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001154{
1155 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1156
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001157 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001158}
1159
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001160/*
1161 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1162 * using ttrace->entry_time for a thread that receives a sys_exit without
1163 * first having received a sys_enter ("poll" issued before tracing session
1164 * starts, lost sys_enter exit due to ring buffer overflow).
1165 */
1166static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1167{
1168 if (tstamp > 0)
1169 return __trace__fprintf_tstamp(trace, tstamp, fp);
1170
1171 return fprintf(fp, " ? ");
1172}
1173
Namhyung Kimf15eb532012-10-05 14:02:16 +09001174static bool done = false;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001175static bool interrupted = false;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001176
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001177static void sig_handler(int sig)
Namhyung Kimf15eb532012-10-05 14:02:16 +09001178{
1179 done = true;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001180 interrupted = sig == SIGINT;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001181}
1182
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001183static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001184{
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001185 size_t printed = 0;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001186
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001187 if (trace->multiple_threads) {
1188 if (trace->show_comm)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02001189 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
Adrian Hunter38051232013-07-04 16:20:31 +03001190 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001191 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001192
1193 return printed;
1194}
1195
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001196static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1197 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1198{
1199 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
1200 printed += fprintf_duration(duration, duration_calculated, fp);
1201 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1202}
1203
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001204static int trace__process_event(struct trace *trace, struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001205 union perf_event *event, struct perf_sample *sample)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001206{
1207 int ret = 0;
1208
1209 switch (event->header.type) {
1210 case PERF_RECORD_LOST:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001211 color_fprintf(trace->output, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001212 "LOST %" PRIu64 " events!\n", event->lost.lost);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001213 ret = machine__process_lost_event(machine, event, sample);
Arnaldo Carvalho de Melo3ed5ca22016-03-30 16:51:17 -03001214 break;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001215 default:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001216 ret = machine__process_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001217 break;
1218 }
1219
1220 return ret;
1221}
1222
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001223static int trace__tool_process(struct perf_tool *tool,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001224 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001225 struct perf_sample *sample,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001226 struct machine *machine)
1227{
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001228 struct trace *trace = container_of(tool, struct trace, tool);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001229 return trace__process_event(trace, machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001230}
1231
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -03001232static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1233{
1234 struct machine *machine = vmachine;
1235
1236 if (machine->kptr_restrict_warned)
1237 return NULL;
1238
1239 if (symbol_conf.kptr_restrict) {
1240 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1241 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1242 "Kernel samples will not be resolved.\n");
1243 machine->kptr_restrict_warned = true;
1244 return NULL;
1245 }
1246
1247 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1248}
1249
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001250static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1251{
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001252 int err = symbol__init(NULL);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001253
1254 if (err)
1255 return err;
1256
David Ahern8fb598e2013-09-28 13:13:00 -06001257 trace->host = machine__new_host();
1258 if (trace->host == NULL)
1259 return -ENOMEM;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001260
Andrei Vagincbd5c172017-11-07 16:22:46 -08001261 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1262 if (err < 0)
1263 goto out;
Arnaldo Carvalho de Melo706c3da2015-07-22 16:16:16 -03001264
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001265 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001266 evlist->threads, trace__tool_process, false,
Kan Liang340b47f2017-09-29 07:47:54 -07001267 trace->opts.proc_map_timeout, 1);
Andrei Vagincbd5c172017-11-07 16:22:46 -08001268out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001269 if (err)
1270 symbol__exit();
1271
1272 return err;
1273}
1274
Andrei Vagin33974a42017-11-07 16:22:45 -08001275static void trace__symbols__exit(struct trace *trace)
1276{
1277 machine__exit(trace->host);
1278 trace->host = NULL;
1279
1280 symbol__exit();
1281}
1282
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001283static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1284{
1285 int idx;
1286
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001287 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1288 nr_args = sc->fmt->nr_args;
1289
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001290 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1291 if (sc->arg_fmt == NULL)
1292 return -1;
1293
1294 for (idx = 0; idx < nr_args; ++idx) {
1295 if (sc->fmt)
1296 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1297 }
1298
1299 sc->nr_args = nr_args;
1300 return 0;
1301}
1302
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001303static int syscall__set_arg_fmts(struct syscall *sc)
1304{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001305 struct tep_format_field *field, *last_field = NULL;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001306 int idx = 0, len;
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001307
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001308 for (field = sc->args; field; field = field->next, ++idx) {
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001309 last_field = field;
1310
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001311 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1312 continue;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001313
1314 if (strcmp(field->type, "const char *") == 0 &&
Arnaldo Carvalho de Melo12f3ca42016-05-23 16:37:55 -03001315 (strcmp(field->name, "filename") == 0 ||
1316 strcmp(field->name, "path") == 0 ||
1317 strcmp(field->name, "pathname") == 0))
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001318 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001319 else if (field->flags & TEP_FIELD_IS_POINTER)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001320 sc->arg_fmt[idx].scnprintf = syscall_arg__scnprintf_hex;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -03001321 else if (strcmp(field->type, "pid_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001322 sc->arg_fmt[idx].scnprintf = SCA_PID;
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -03001323 else if (strcmp(field->type, "umode_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001324 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001325 else if ((strcmp(field->type, "int") == 0 ||
1326 strcmp(field->type, "unsigned int") == 0 ||
1327 strcmp(field->type, "long") == 0) &&
1328 (len = strlen(field->name)) >= 2 &&
1329 strcmp(field->name + len - 2, "fd") == 0) {
1330 /*
1331 * /sys/kernel/tracing/events/syscalls/sys_enter*
1332 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1333 * 65 int
1334 * 23 unsigned int
1335 * 7 unsigned long
1336 */
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001337 sc->arg_fmt[idx].scnprintf = SCA_FD;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001338 }
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001339 }
1340
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001341 if (last_field)
1342 sc->args_size = last_field->offset + last_field->size;
1343
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001344 return 0;
1345}
1346
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001347static int trace__read_syscall_info(struct trace *trace, int id)
1348{
1349 char tp_name[128];
1350 struct syscall *sc;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03001351 const char *name = syscalltbl__name(trace->sctbl, id);
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001352
1353 if (name == NULL)
1354 return -1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001355
1356 if (id > trace->syscalls.max) {
1357 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1358
1359 if (nsyscalls == NULL)
1360 return -1;
1361
1362 if (trace->syscalls.max != -1) {
1363 memset(nsyscalls + trace->syscalls.max + 1, 0,
1364 (id - trace->syscalls.max) * sizeof(*sc));
1365 } else {
1366 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1367 }
1368
1369 trace->syscalls.table = nsyscalls;
1370 trace->syscalls.max = id;
1371 }
1372
1373 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001374 sc->name = name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001375
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001376 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001377
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001378 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
Jiri Olsa97978b32013-12-03 14:09:24 +01001379 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001380
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001381 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001382 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
Jiri Olsa97978b32013-12-03 14:09:24 +01001383 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001384 }
1385
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001386 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1387 return -1;
1388
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001389 if (IS_ERR(sc->tp_format))
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001390 return -1;
1391
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001392 sc->args = sc->tp_format->format.fields;
Taeung Songc42de702016-02-26 22:14:25 +09001393 /*
1394 * We need to check and discard the first variable '__syscall_nr'
1395 * or 'nr' that mean the syscall number. It is needless here.
1396 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1397 */
1398 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001399 sc->args = sc->args->next;
1400 --sc->nr_args;
1401 }
1402
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001403 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001404 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001405
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001406 return syscall__set_arg_fmts(sc);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001407}
1408
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001409static int trace__validate_ev_qualifier(struct trace *trace)
1410{
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001411 int err = 0, i;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001412 size_t nr_allocated;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001413 struct str_node *pos;
1414
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001415 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1416 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1417 sizeof(trace->ev_qualifier_ids.entries[0]));
1418
1419 if (trace->ev_qualifier_ids.entries == NULL) {
1420 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1421 trace->output);
1422 err = -EINVAL;
1423 goto out;
1424 }
1425
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001426 nr_allocated = trace->ev_qualifier_ids.nr;
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001427 i = 0;
1428
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001429 strlist__for_each_entry(pos, trace->ev_qualifier) {
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001430 const char *sc = pos->s;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001431 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001432
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001433 if (id < 0) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001434 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1435 if (id >= 0)
1436 goto matches;
1437
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001438 if (err == 0) {
1439 fputs("Error:\tInvalid syscall ", trace->output);
1440 err = -EINVAL;
1441 } else {
1442 fputs(", ", trace->output);
1443 }
1444
1445 fputs(sc, trace->output);
1446 }
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001447matches:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001448 trace->ev_qualifier_ids.entries[i++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001449 if (match_next == -1)
1450 continue;
1451
1452 while (1) {
1453 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1454 if (id < 0)
1455 break;
1456 if (nr_allocated == trace->ev_qualifier_ids.nr) {
1457 void *entries;
1458
1459 nr_allocated += 8;
1460 entries = realloc(trace->ev_qualifier_ids.entries,
1461 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1462 if (entries == NULL) {
1463 err = -ENOMEM;
1464 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1465 goto out_free;
1466 }
1467 trace->ev_qualifier_ids.entries = entries;
1468 }
1469 trace->ev_qualifier_ids.nr++;
1470 trace->ev_qualifier_ids.entries[i++] = id;
1471 }
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001472 }
1473
1474 if (err < 0) {
1475 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1476 "\nHint:\tand: 'man syscalls'\n", trace->output);
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001477out_free:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001478 zfree(&trace->ev_qualifier_ids.entries);
1479 trace->ev_qualifier_ids.nr = 0;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001480 }
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001481out:
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001482 return err;
1483}
1484
David Ahern55d43bca2015-02-19 15:00:22 -05001485/*
1486 * args is to be interpreted as a series of longs but we need to handle
1487 * 8-byte unaligned accesses. args points to raw_data within the event
1488 * and raw_data is guaranteed to be 8-byte unaligned because it is
1489 * preceded by raw_size which is a u32. So we need to copy args to a temp
1490 * variable to read it. Most notably this avoids extended load instructions
1491 * on unaligned addresses
1492 */
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001493unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001494{
1495 unsigned long val;
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001496 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001497
1498 memcpy(&val, p, sizeof(val));
1499 return val;
1500}
1501
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001502static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1503 struct syscall_arg *arg)
1504{
1505 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1506 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1507
1508 return scnprintf(bf, size, "arg%d: ", arg->idx);
1509}
1510
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001511/*
1512 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1513 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1514 * in tools/perf/trace/beauty/mount_flags.c
1515 */
1516static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1517{
1518 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1519 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1520
1521 return val;
1522}
1523
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001524static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1525 struct syscall_arg *arg, unsigned long val)
1526{
1527 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1528 arg->val = val;
1529 if (sc->arg_fmt[arg->idx].parm)
1530 arg->parm = sc->arg_fmt[arg->idx].parm;
1531 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1532 }
1533 return scnprintf(bf, size, "%ld", val);
1534}
1535
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001536static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001537 unsigned char *args, void *augmented_args, int augmented_args_size,
1538 struct trace *trace, struct thread *thread)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001539{
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001540 size_t printed = 0;
David Ahern55d43bca2015-02-19 15:00:22 -05001541 unsigned long val;
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001542 u8 bit = 1;
1543 struct syscall_arg arg = {
1544 .args = args,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001545 .augmented = {
1546 .size = augmented_args_size,
1547 .args = augmented_args,
1548 },
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001549 .idx = 0,
1550 .mask = 0,
1551 .trace = trace,
1552 .thread = thread,
1553 };
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001554 struct thread_trace *ttrace = thread__priv(thread);
1555
1556 /*
1557 * Things like fcntl will set this in its 'cmd' formatter to pick the
1558 * right formatter for the return value (an fd? file flags?), which is
1559 * not needed for syscalls that always return a given type, say an fd.
1560 */
1561 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001562
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001563 if (sc->args != NULL) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001564 struct tep_format_field *field;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001565
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001566 for (field = sc->args; field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001567 field = field->next, ++arg.idx, bit <<= 1) {
1568 if (arg.mask & bit)
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001569 continue;
David Ahern55d43bca2015-02-19 15:00:22 -05001570
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001571 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001572 /*
1573 * Some syscall args need some mask, most don't and
1574 * return val untouched.
1575 */
1576 val = syscall__mask_val(sc, &arg, val);
David Ahern55d43bca2015-02-19 15:00:22 -05001577
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001578 /*
1579 * Suppress this argument if its value is zero and
1580 * and we don't have a string associated in an
1581 * strarray for it.
1582 */
David Ahern55d43bca2015-02-19 15:00:22 -05001583 if (val == 0 &&
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001584 !(sc->arg_fmt &&
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -03001585 (sc->arg_fmt[arg.idx].show_zero ||
1586 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001587 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1588 sc->arg_fmt[arg.idx].parm))
Arnaldo Carvalho de Melo22ae5cf12013-09-12 11:27:34 -03001589 continue;
1590
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001591 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001592 "%s%s: ", printed ? ", " : "", field->name);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001593 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001594 }
Arnaldo Carvalho de Melo4c4d6e52016-05-05 23:38:05 -03001595 } else if (IS_ERR(sc->tp_format)) {
1596 /*
1597 * If we managed to read the tracepoint /format file, then we
1598 * may end up not having any args, like with gettid(), so only
1599 * print the raw args when we didn't manage to read it.
1600 */
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001601 while (arg.idx < sc->nr_args) {
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001602 if (arg.mask & bit)
1603 goto next_arg;
1604 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001605 if (printed)
1606 printed += scnprintf(bf + printed, size - printed, ", ");
1607 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001608 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1609next_arg:
1610 ++arg.idx;
1611 bit <<= 1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001612 }
1613 }
1614
1615 return printed;
1616}
1617
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001618typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001619 union perf_event *event,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001620 struct perf_sample *sample);
1621
1622static struct syscall *trace__syscall_info(struct trace *trace,
David Ahernbf2575c2013-10-08 21:26:53 -06001623 struct perf_evsel *evsel, int id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001624{
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001625
1626 if (id < 0) {
Arnaldo Carvalho de Meloadaa18b2013-08-22 17:55:25 -03001627
1628 /*
1629 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1630 * before that, leaving at a higher verbosity level till that is
1631 * explained. Reproduced with plain ftrace with:
1632 *
1633 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1634 * grep "NR -1 " /t/trace_pipe
1635 *
1636 * After generating some load on the machine.
1637 */
1638 if (verbose > 1) {
1639 static u64 n;
1640 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1641 id, perf_evsel__name(evsel), ++n);
1642 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001643 return NULL;
1644 }
1645
1646 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1647 trace__read_syscall_info(trace, id))
1648 goto out_cant_read;
1649
1650 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1651 goto out_cant_read;
1652
1653 return &trace->syscalls.table[id];
1654
1655out_cant_read:
Namhyung Kimbb963e12017-02-17 17:17:38 +09001656 if (verbose > 0) {
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03001657 fprintf(trace->output, "Problems reading syscall %d", id);
1658 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1659 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1660 fputs(" information\n", trace->output);
1661 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001662 return NULL;
1663}
1664
David Ahernbf2575c2013-10-08 21:26:53 -06001665static void thread__update_stats(struct thread_trace *ttrace,
1666 int id, struct perf_sample *sample)
1667{
1668 struct int_node *inode;
1669 struct stats *stats;
1670 u64 duration = 0;
1671
1672 inode = intlist__findnew(ttrace->syscall_stats, id);
1673 if (inode == NULL)
1674 return;
1675
1676 stats = inode->priv;
1677 if (stats == NULL) {
1678 stats = malloc(sizeof(struct stats));
1679 if (stats == NULL)
1680 return;
1681 init_stats(stats);
1682 inode->priv = stats;
1683 }
1684
1685 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1686 duration = sample->time - ttrace->entry_time;
1687
1688 update_stats(stats, duration);
1689}
1690
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001691static int trace__printf_interrupted_entry(struct trace *trace)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001692{
1693 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001694 size_t printed;
1695
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001696 if (trace->failure_only || trace->current == NULL)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001697 return 0;
1698
1699 ttrace = thread__priv(trace->current);
1700
1701 if (!ttrace->entry_pending)
1702 return 0;
1703
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001704 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001705 printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
1706 ttrace->entry_pending = false;
1707
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03001708 ++trace->nr_events_printed;
1709
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001710 return printed;
1711}
1712
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001713static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1714 struct perf_sample *sample, struct thread *thread)
1715{
1716 int printed = 0;
1717
1718 if (trace->print_sample) {
1719 double ts = (double)sample->time / NSEC_PER_MSEC;
1720
1721 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1722 perf_evsel__name(evsel), ts,
1723 thread__comm_str(thread),
1724 sample->pid, sample->tid, sample->cpu);
1725 }
1726
1727 return printed;
1728}
1729
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001730static 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 -03001731{
1732 void *augmented_args = NULL;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001733 /*
1734 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
1735 * and there we get all 6 syscall args plus the tracepoint common
1736 * fields (sizeof(long)) and the syscall_nr (another long). So we check
1737 * if that is the case and if so don't look after the sc->args_size,
1738 * but always after the full raw_syscalls:sys_enter payload, which is
1739 * fixed.
1740 *
1741 * We'll revisit this later to pass s->args_size to the BPF augmenter
1742 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1743 * copies only what we need for each syscall, like what happens when we
1744 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1745 * traffic to just what is needed for each syscall.
1746 */
1747 int args_size = raw_augmented ? (8 * (int)sizeof(long)) : sc->args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001748
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001749 *augmented_args_size = sample->raw_size - args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001750 if (*augmented_args_size > 0)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001751 augmented_args = sample->raw_data + args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001752
1753 return augmented_args;
1754}
1755
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001756static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001757 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001758 struct perf_sample *sample)
1759{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001760 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001761 void *args;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001762 size_t printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001763 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001764 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001765 int augmented_args_size = 0;
1766 void *augmented_args = NULL;
David Ahernbf2575c2013-10-08 21:26:53 -06001767 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001768 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001769
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001770 if (sc == NULL)
1771 return -1;
1772
David Ahern8fb598e2013-09-28 13:13:00 -06001773 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001774 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001775 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001776 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001777
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001778 trace__fprintf_sample(trace, evsel, sample, thread);
1779
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001780 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001781
1782 if (ttrace->entry_str == NULL) {
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001783 ttrace->entry_str = malloc(trace__entry_str_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001784 if (!ttrace->entry_str)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001785 goto out_put;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001786 }
1787
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001788 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001789 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001790 /*
1791 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1792 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1793 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1794 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1795 * so when handling, say the openat syscall, we end up getting 6 args for the
1796 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1797 * thinking that the extra 2 u64 args are the augmented filename, so just check
1798 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1799 */
1800 if (evsel != trace->syscalls.events.sys_enter)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001801 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001802 ttrace->entry_time = sample->time;
1803 msg = ttrace->entry_str;
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001804 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001805
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001806 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001807 args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001808
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001809 if (sc->is_exit) {
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001810 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001811 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Meloc008f782016-05-17 12:13:54 -03001812 fprintf(trace->output, "%-70s)\n", ttrace->entry_str);
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001813 }
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001814 } else {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001815 ttrace->entry_pending = true;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001816 /* See trace__vfs_getname & trace__sys_exit */
1817 ttrace->filename.pending_open = false;
1818 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001819
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001820 if (trace->current != thread) {
1821 thread__put(trace->current);
1822 trace->current = thread__get(thread);
1823 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001824 err = 0;
1825out_put:
1826 thread__put(thread);
1827 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001828}
1829
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001830static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1831 struct perf_sample *sample)
1832{
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001833 struct thread_trace *ttrace;
1834 struct thread *thread;
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001835 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1836 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001837 char msg[1024];
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001838 void *args, *augmented_args = NULL;
1839 int augmented_args_size;
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001840
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001841 if (sc == NULL)
1842 return -1;
1843
1844 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1845 ttrace = thread__trace(thread, trace->output);
1846 /*
1847 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1848 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1849 */
1850 if (ttrace == NULL)
1851 goto out_put;
1852
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001853 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001854 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001855 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001856 fprintf(trace->output, "%s", msg);
1857 err = 0;
1858out_put:
1859 thread__put(thread);
1860 return err;
1861}
1862
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001863static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1864 struct perf_sample *sample,
1865 struct callchain_cursor *cursor)
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001866{
1867 struct addr_location al;
Ravi Bangoria3a9e9a42018-01-30 11:00:53 +05301868 int max_stack = evsel->attr.sample_max_stack ?
1869 evsel->attr.sample_max_stack :
1870 trace->max_stack;
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001871 int err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001872
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001873 if (machine__resolve(trace->host, &al, sample) < 0)
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001874 return -1;
1875
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001876 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1877 addr_location__put(&al);
1878 return err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001879}
1880
1881static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1882{
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001883 /* TODO: user-configurable print_opts */
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001884 const unsigned int print_opts = EVSEL__PRINT_SYM |
1885 EVSEL__PRINT_DSO |
1886 EVSEL__PRINT_UNKNOWN_AS_ADDR;
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001887
Arnaldo Carvalho de Melod327e602016-04-14 17:53:49 -03001888 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001889}
1890
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01001891static const char *errno_to_name(struct perf_evsel *evsel, int err)
1892{
1893 struct perf_env *env = perf_evsel__env(evsel);
1894 const char *arch_name = perf_env__arch(env);
1895
1896 return arch_syscalls__strerrno(arch_name, err);
1897}
1898
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001899static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001900 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001901 struct perf_sample *sample)
1902{
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09001903 long ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001904 u64 duration = 0;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001905 bool duration_calculated = false;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001906 struct thread *thread;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001907 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0;
David Ahernbf2575c2013-10-08 21:26:53 -06001908 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001909 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001910
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001911 if (sc == NULL)
1912 return -1;
1913
David Ahern8fb598e2013-09-28 13:13:00 -06001914 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001915 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001916 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001917 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001918
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001919 trace__fprintf_sample(trace, evsel, sample, thread);
1920
David Ahernbf2575c2013-10-08 21:26:53 -06001921 if (trace->summary)
1922 thread__update_stats(ttrace, id, sample);
1923
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001924 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001925
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001926 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001927 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
1928 ttrace->filename.pending_open = false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001929 ++trace->stats.vfs_getname;
1930 }
1931
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001932 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001933 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001934 if (trace__filter_duration(trace, duration))
1935 goto out;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001936 duration_calculated = true;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001937 } else if (trace->duration_filter)
1938 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001939
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001940 if (sample->callchain) {
1941 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
1942 if (callchain_ret == 0) {
1943 if (callchain_cursor.nr < trace->min_stack)
1944 goto out;
1945 callchain_ret = 1;
1946 }
1947 }
1948
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001949 if (trace->summary_only || (ret >= 0 && trace->failure_only))
David Ahernfd2eaba2013-11-12 09:31:15 -07001950 goto out;
1951
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001952 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001953
1954 if (ttrace->entry_pending) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001955 fprintf(trace->output, "%-70s", ttrace->entry_str);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001956 } else {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001957 fprintf(trace->output, " ... [");
1958 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
1959 fprintf(trace->output, "]: %s()", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001960 }
1961
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001962 if (sc->fmt == NULL) {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001963 if (ret < 0)
1964 goto errno_print;
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001965signed_print:
Arnaldo Carvalho de Melo6f8fe612017-07-19 11:39:47 -03001966 fprintf(trace->output, ") = %ld", ret);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001967 } else if (ret < 0) {
1968errno_print: {
Masami Hiramatsu942a91e2014-08-14 02:22:41 +00001969 char bf[STRERR_BUFSIZE];
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001970 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01001971 *e = errno_to_name(evsel, -ret);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001972
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001973 fprintf(trace->output, ") = -1 %s %s", e, emsg);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001974 }
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001975 } else if (ret == 0 && sc->fmt->timeout)
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001976 fprintf(trace->output, ") = 0 Timeout");
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001977 else if (ttrace->ret_scnprintf) {
1978 char bf[1024];
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001979 struct syscall_arg arg = {
1980 .val = ret,
1981 .thread = thread,
1982 .trace = trace,
1983 };
1984 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001985 ttrace->ret_scnprintf = NULL;
1986 fprintf(trace->output, ") = %s", bf);
1987 } else if (sc->fmt->hexret)
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09001988 fprintf(trace->output, ") = %#lx", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03001989 else if (sc->fmt->errpid) {
1990 struct thread *child = machine__find_thread(trace->host, ret, ret);
1991
1992 if (child != NULL) {
1993 fprintf(trace->output, ") = %ld", ret);
1994 if (child->comm_set)
1995 fprintf(trace->output, " (%s)", thread__comm_str(child));
1996 thread__put(child);
1997 }
1998 } else
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001999 goto signed_print;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002000
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002001 fputc('\n', trace->output);
Milian Wolff566a0882016-04-08 13:34:15 +02002002
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002003 /*
2004 * We only consider an 'event' for the sake of --max-events a non-filtered
2005 * sys_enter + sys_exit and other tracepoint events.
2006 */
2007 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2008 interrupted = true;
2009
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002010 if (callchain_ret > 0)
2011 trace__fprintf_callchain(trace, sample);
2012 else if (callchain_ret < 0)
2013 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002014out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002015 ttrace->entry_pending = false;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002016 err = 0;
2017out_put:
2018 thread__put(thread);
2019 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002020}
2021
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002022static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002023 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002024 struct perf_sample *sample)
2025{
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002026 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2027 struct thread_trace *ttrace;
2028 size_t filename_len, entry_str_len, to_move;
2029 ssize_t remaining_space;
2030 char *pos;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002031 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002032
2033 if (!thread)
2034 goto out;
2035
2036 ttrace = thread__priv(thread);
2037 if (!ttrace)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002038 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002039
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002040 filename_len = strlen(filename);
Arnaldo Carvalho de Melo39f0e7a2017-03-24 14:51:28 -03002041 if (filename_len == 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002042 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002043
2044 if (ttrace->filename.namelen < filename_len) {
2045 char *f = realloc(ttrace->filename.name, filename_len + 1);
2046
2047 if (f == NULL)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002048 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002049
2050 ttrace->filename.namelen = filename_len;
2051 ttrace->filename.name = f;
2052 }
2053
2054 strcpy(ttrace->filename.name, filename);
2055 ttrace->filename.pending_open = true;
2056
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002057 if (!ttrace->filename.ptr)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002058 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002059
2060 entry_str_len = strlen(ttrace->entry_str);
2061 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2062 if (remaining_space <= 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002063 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002064
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002065 if (filename_len > (size_t)remaining_space) {
2066 filename += filename_len - remaining_space;
2067 filename_len = remaining_space;
2068 }
2069
2070 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2071 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2072 memmove(pos + filename_len, pos, to_move);
2073 memcpy(pos, filename, filename_len);
2074
2075 ttrace->filename.ptr = 0;
2076 ttrace->filename.entry_str_pos = 0;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002077out_put:
2078 thread__put(thread);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002079out:
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002080 return 0;
2081}
2082
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002083static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002084 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002085 struct perf_sample *sample)
2086{
2087 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2088 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
David Ahern8fb598e2013-09-28 13:13:00 -06002089 struct thread *thread = machine__findnew_thread(trace->host,
Adrian Hunter314add62013-08-27 11:23:03 +03002090 sample->pid,
2091 sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002092 struct thread_trace *ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002093
2094 if (ttrace == NULL)
2095 goto out_dump;
2096
2097 ttrace->runtime_ms += runtime_ms;
2098 trace->runtime_ms += runtime_ms;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002099out_put:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002100 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002101 return 0;
2102
2103out_dump:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002104 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002105 evsel->name,
2106 perf_evsel__strval(evsel, sample, "comm"),
2107 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2108 runtime,
2109 perf_evsel__intval(evsel, sample, "vruntime"));
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002110 goto out_put;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002111}
2112
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002113static int bpf_output__printer(enum binary_printer_ops op,
2114 unsigned int val, void *extra __maybe_unused, FILE *fp)
Wang Nan1d6c9402016-02-26 09:31:55 +00002115{
Wang Nan1d6c9402016-02-26 09:31:55 +00002116 unsigned char ch = (unsigned char)val;
2117
2118 switch (op) {
2119 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002120 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
Wang Nan1d6c9402016-02-26 09:31:55 +00002121 case BINARY_PRINT_DATA_BEGIN:
2122 case BINARY_PRINT_LINE_BEGIN:
2123 case BINARY_PRINT_ADDR:
2124 case BINARY_PRINT_NUM_DATA:
2125 case BINARY_PRINT_NUM_PAD:
2126 case BINARY_PRINT_SEP:
2127 case BINARY_PRINT_CHAR_PAD:
2128 case BINARY_PRINT_LINE_END:
2129 case BINARY_PRINT_DATA_END:
2130 default:
2131 break;
2132 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002133
2134 return 0;
Wang Nan1d6c9402016-02-26 09:31:55 +00002135}
2136
2137static void bpf_output__fprintf(struct trace *trace,
2138 struct perf_sample *sample)
2139{
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002140 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2141 bpf_output__printer, NULL, trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002142 ++trace->nr_events_printed;
Wang Nan1d6c9402016-02-26 09:31:55 +00002143}
2144
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002145static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2146 union perf_event *event __maybe_unused,
2147 struct perf_sample *sample)
2148{
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002149 struct thread *thread;
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002150 int callchain_ret = 0;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002151 /*
2152 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2153 * this event's max_events having been hit and this is an entry coming
2154 * from the ring buffer that we should discard, since the max events
2155 * have already been considered/printed.
2156 */
2157 if (evsel->disabled)
2158 return 0;
2159
2160 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002161
2162 if (sample->callchain) {
2163 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2164 if (callchain_ret == 0) {
2165 if (callchain_cursor.nr < trace->min_stack)
2166 goto out;
2167 callchain_ret = 1;
2168 }
2169 }
2170
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03002171 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002172 trace__fprintf_tstamp(trace, sample->time, trace->output);
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002173
2174 if (trace->trace_syscalls)
2175 fprintf(trace->output, "( ): ");
2176
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002177 if (thread)
2178 trace__fprintf_comm_tid(trace, thread, trace->output);
2179
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002180 if (evsel == trace->syscalls.events.augmented) {
2181 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2182 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2183
2184 if (sc) {
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002185 fprintf(trace->output, "%s(", sc->name);
2186 trace__fprintf_sys_enter(trace, evsel, sample);
2187 fputc(')', trace->output);
2188 goto newline;
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002189 }
2190
2191 /*
2192 * XXX: Not having the associated syscall info or not finding/adding
2193 * the thread should never happen, but if it does...
2194 * fall thru and print it as a bpf_output event.
2195 */
2196 }
2197
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002198 fprintf(trace->output, "%s:", evsel->name);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002199
Wang Nan1d6c9402016-02-26 09:31:55 +00002200 if (perf_evsel__is_bpf_output(evsel)) {
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002201 bpf_output__fprintf(trace, sample);
Wang Nan1d6c9402016-02-26 09:31:55 +00002202 } else if (evsel->tp_format) {
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002203 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2204 trace__fprintf_sys_enter(trace, evsel, sample)) {
2205 event_format__fprintf(evsel->tp_format, sample->cpu,
2206 sample->raw_data, sample->raw_size,
2207 trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002208 ++trace->nr_events_printed;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002209
2210 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2211 perf_evsel__disable(evsel);
2212 perf_evsel__close(evsel);
2213 }
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002214 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002215 }
2216
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002217newline:
Changbin Du51125a22018-03-13 18:40:01 +08002218 fprintf(trace->output, "\n");
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002219
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002220 if (callchain_ret > 0)
2221 trace__fprintf_callchain(trace, sample);
2222 else if (callchain_ret < 0)
2223 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2224out:
Arnaldo Carvalho de Meloa937c662018-10-20 09:27:52 -03002225 thread__put(thread);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002226 return 0;
2227}
2228
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002229static void print_location(FILE *f, struct perf_sample *sample,
2230 struct addr_location *al,
2231 bool print_dso, bool print_sym)
2232{
2233
Namhyung Kimbb963e12017-02-17 17:17:38 +09002234 if ((verbose > 0 || print_dso) && al->map)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002235 fprintf(f, "%s@", al->map->dso->long_name);
2236
Namhyung Kimbb963e12017-02-17 17:17:38 +09002237 if ((verbose > 0 || print_sym) && al->sym)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002238 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002239 al->addr - al->sym->start);
2240 else if (al->map)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002241 fprintf(f, "0x%" PRIx64, al->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002242 else
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002243 fprintf(f, "0x%" PRIx64, sample->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002244}
2245
2246static int trace__pgfault(struct trace *trace,
2247 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03002248 union perf_event *event __maybe_unused,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002249 struct perf_sample *sample)
2250{
2251 struct thread *thread;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002252 struct addr_location al;
2253 char map_type = 'd';
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002254 struct thread_trace *ttrace;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002255 int err = -1;
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002256 int callchain_ret = 0;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002257
2258 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002259
2260 if (sample->callchain) {
2261 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2262 if (callchain_ret == 0) {
2263 if (callchain_cursor.nr < trace->min_stack)
2264 goto out_put;
2265 callchain_ret = 1;
2266 }
2267 }
2268
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002269 ttrace = thread__trace(thread, trace->output);
2270 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002271 goto out_put;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002272
2273 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2274 ttrace->pfmaj++;
2275 else
2276 ttrace->pfmin++;
2277
2278 if (trace->summary_only)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002279 goto out;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002280
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002281 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002282
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002283 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002284
2285 fprintf(trace->output, "%sfault [",
2286 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2287 "maj" : "min");
2288
2289 print_location(trace->output, sample, &al, false, true);
2290
2291 fprintf(trace->output, "] => ");
2292
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03002293 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002294
2295 if (!al.map) {
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002296 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002297
2298 if (al.map)
2299 map_type = 'x';
2300 else
2301 map_type = '?';
2302 }
2303
2304 print_location(trace->output, sample, &al, true, false);
2305
2306 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
Arnaldo Carvalho de Melo0c3a6ef2016-04-19 16:31:12 -03002307
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002308 if (callchain_ret > 0)
2309 trace__fprintf_callchain(trace, sample);
2310 else if (callchain_ret < 0)
2311 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002312
2313 ++trace->nr_events_printed;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002314out:
2315 err = 0;
2316out_put:
2317 thread__put(thread);
2318 return err;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002319}
2320
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002321static void trace__set_base_time(struct trace *trace,
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002322 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002323 struct perf_sample *sample)
2324{
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002325 /*
2326 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2327 * and don't use sample->time unconditionally, we may end up having
2328 * some other event in the future without PERF_SAMPLE_TIME for good
2329 * reason, i.e. we may not be interested in its timestamps, just in
2330 * it taking place, picking some piece of information when it
2331 * appears in our event stream (vfs_getname comes to mind).
2332 */
2333 if (trace->base_time == 0 && !trace->full_time &&
2334 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002335 trace->base_time = sample->time;
2336}
2337
David Ahern6810fc92013-08-28 22:29:52 -06002338static int trace__process_sample(struct perf_tool *tool,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002339 union perf_event *event,
David Ahern6810fc92013-08-28 22:29:52 -06002340 struct perf_sample *sample,
2341 struct perf_evsel *evsel,
2342 struct machine *machine __maybe_unused)
2343{
2344 struct trace *trace = container_of(tool, struct trace, tool);
David Ahernaa07df62016-11-25 09:29:52 -07002345 struct thread *thread;
David Ahern6810fc92013-08-28 22:29:52 -06002346 int err = 0;
2347
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002348 tracepoint_handler handler = evsel->handler;
David Ahern6810fc92013-08-28 22:29:52 -06002349
David Ahernaa07df62016-11-25 09:29:52 -07002350 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2351 if (thread && thread__is_filtered(thread))
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002352 goto out;
David Ahernbdc89662013-08-28 22:29:53 -06002353
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002354 trace__set_base_time(trace, evsel, sample);
David Ahern6810fc92013-08-28 22:29:52 -06002355
David Ahern31605652013-12-04 19:41:41 -07002356 if (handler) {
2357 ++trace->nr_events;
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002358 handler(trace, evsel, event, sample);
David Ahern31605652013-12-04 19:41:41 -07002359 }
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002360out:
2361 thread__put(thread);
David Ahern6810fc92013-08-28 22:29:52 -06002362 return err;
2363}
2364
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002365static int trace__record(struct trace *trace, int argc, const char **argv)
David Ahern5e2485b2013-09-28 13:13:01 -06002366{
2367 unsigned int rec_argc, i, j;
2368 const char **rec_argv;
2369 const char * const record_args[] = {
2370 "record",
2371 "-R",
2372 "-m", "1024",
2373 "-c", "1",
David Ahern5e2485b2013-09-28 13:13:01 -06002374 };
2375
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002376 const char * const sc_args[] = { "-e", };
2377 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2378 const char * const majpf_args[] = { "-e", "major-faults" };
2379 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2380 const char * const minpf_args[] = { "-e", "minor-faults" };
2381 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2382
David Ahern9aca7f12013-12-04 19:41:39 -07002383 /* +1 is for the event string below */
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002384 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2385 majpf_args_nr + minpf_args_nr + argc;
David Ahern5e2485b2013-09-28 13:13:01 -06002386 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2387
2388 if (rec_argv == NULL)
2389 return -ENOMEM;
2390
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002391 j = 0;
David Ahern5e2485b2013-09-28 13:13:01 -06002392 for (i = 0; i < ARRAY_SIZE(record_args); i++)
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002393 rec_argv[j++] = record_args[i];
2394
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002395 if (trace->trace_syscalls) {
2396 for (i = 0; i < sc_args_nr; i++)
2397 rec_argv[j++] = sc_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002398
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002399 /* event string may be different for older kernels - e.g., RHEL6 */
2400 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2401 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2402 else if (is_valid_tracepoint("syscalls:sys_enter"))
2403 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2404 else {
2405 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
Martin Kepplingerc896f852017-09-13 21:14:19 +02002406 free(rec_argv);
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002407 return -1;
2408 }
David Ahern9aca7f12013-12-04 19:41:39 -07002409 }
David Ahern9aca7f12013-12-04 19:41:39 -07002410
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002411 if (trace->trace_pgfaults & TRACE_PFMAJ)
2412 for (i = 0; i < majpf_args_nr; i++)
2413 rec_argv[j++] = majpf_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002414
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002415 if (trace->trace_pgfaults & TRACE_PFMIN)
2416 for (i = 0; i < minpf_args_nr; i++)
2417 rec_argv[j++] = minpf_args[i];
2418
2419 for (i = 0; i < (unsigned int)argc; i++)
2420 rec_argv[j++] = argv[i];
2421
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002422 return cmd_record(j, rec_argv);
David Ahern5e2485b2013-09-28 13:13:01 -06002423}
2424
David Ahernbf2575c2013-10-08 21:26:53 -06002425static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2426
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002427static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002428{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002429 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002430
2431 if (IS_ERR(evsel))
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002432 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002433
2434 if (perf_evsel__field(evsel, "pathname") == NULL) {
2435 perf_evsel__delete(evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002436 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002437 }
2438
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002439 evsel->handler = trace__vfs_getname;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002440 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002441 return true;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002442}
2443
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002444static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002445{
2446 struct perf_evsel *evsel;
2447 struct perf_event_attr attr = {
2448 .type = PERF_TYPE_SOFTWARE,
2449 .mmap_data = 1,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002450 };
2451
2452 attr.config = config;
Arnaldo Carvalho de Melo05247982014-07-23 18:15:09 -03002453 attr.sample_period = 1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002454
2455 event_attr_init(&attr);
2456
2457 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002458 if (evsel)
2459 evsel->handler = trace__pgfault;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002460
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002461 return evsel;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002462}
2463
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002464static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2465{
2466 const u32 type = event->header.type;
2467 struct perf_evsel *evsel;
2468
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002469 if (type != PERF_RECORD_SAMPLE) {
2470 trace__process_event(trace, trace->host, event, sample);
2471 return;
2472 }
2473
2474 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2475 if (evsel == NULL) {
2476 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2477 return;
2478 }
2479
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002480 trace__set_base_time(trace, evsel, sample);
2481
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002482 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2483 sample->raw_data == NULL) {
2484 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2485 perf_evsel__name(evsel), sample->tid,
2486 sample->cpu, sample->raw_size);
2487 } else {
2488 tracepoint_handler handler = evsel->handler;
2489 handler(trace, evsel, event, sample);
2490 }
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002491
2492 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2493 interrupted = true;
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002494}
2495
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002496static int trace__add_syscall_newtp(struct trace *trace)
2497{
2498 int ret = -1;
2499 struct perf_evlist *evlist = trace->evlist;
2500 struct perf_evsel *sys_enter, *sys_exit;
2501
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002502 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002503 if (sys_enter == NULL)
2504 goto out;
2505
2506 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2507 goto out_delete_sys_enter;
2508
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002509 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002510 if (sys_exit == NULL)
2511 goto out_delete_sys_enter;
2512
2513 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2514 goto out_delete_sys_exit;
2515
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002516 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2517 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2518
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002519 perf_evlist__add(evlist, sys_enter);
2520 perf_evlist__add(evlist, sys_exit);
2521
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002522 if (callchain_param.enabled && !trace->kernel_syscallchains) {
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03002523 /*
2524 * We're interested only in the user space callchain
2525 * leading to the syscall, allow overriding that for
2526 * debugging reasons using --kernel_syscall_callchains
2527 */
2528 sys_exit->attr.exclude_callchain_kernel = 1;
2529 }
2530
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03002531 trace->syscalls.events.sys_enter = sys_enter;
2532 trace->syscalls.events.sys_exit = sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002533
2534 ret = 0;
2535out:
2536 return ret;
2537
2538out_delete_sys_exit:
2539 perf_evsel__delete_priv(sys_exit);
2540out_delete_sys_enter:
2541 perf_evsel__delete_priv(sys_enter);
2542 goto out;
2543}
2544
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002545static int trace__set_ev_qualifier_filter(struct trace *trace)
2546{
2547 int err = -1;
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002548 struct perf_evsel *sys_exit;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002549 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2550 trace->ev_qualifier_ids.nr,
2551 trace->ev_qualifier_ids.entries);
2552
2553 if (filter == NULL)
2554 goto out_enomem;
2555
Mathieu Poirier3541c032016-09-16 08:44:04 -06002556 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2557 filter)) {
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002558 sys_exit = trace->syscalls.events.sys_exit;
Mathieu Poirier3541c032016-09-16 08:44:04 -06002559 err = perf_evsel__append_tp_filter(sys_exit, filter);
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002560 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002561
2562 free(filter);
2563out:
2564 return err;
2565out_enomem:
2566 errno = ENOMEM;
2567 goto out;
2568}
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002569
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002570static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2571 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2572{
2573 int err = 0;
2574#ifdef HAVE_LIBBPF_SUPPORT
2575 bool value = true;
2576 int map_fd = bpf_map__fd(map);
2577 size_t i;
2578
2579 for (i = 0; i < npids; ++i) {
2580 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2581 if (err)
2582 break;
2583 }
2584#endif
2585 return err;
2586}
2587
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002588static int trace__set_filter_loop_pids(struct trace *trace)
2589{
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002590 unsigned int nr = 1, err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002591 pid_t pids[32] = {
2592 getpid(),
2593 };
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03002594 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2595
2596 while (thread && nr < ARRAY_SIZE(pids)) {
2597 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2598
2599 if (parent == NULL)
2600 break;
2601
2602 if (!strcmp(thread__comm_str(parent), "sshd")) {
2603 pids[nr++] = parent->tid;
2604 break;
2605 }
2606 thread = parent;
2607 }
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002608
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002609 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2610 if (!err && trace->filter_pids.map)
2611 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2612
2613 return err;
2614}
2615
2616static int trace__set_filter_pids(struct trace *trace)
2617{
2618 int err = 0;
2619 /*
2620 * Better not use !target__has_task() here because we need to cover the
2621 * case where no threads were specified in the command line, but a
2622 * workload was, and in that case we will fill in the thread_map when
2623 * we fork the workload in perf_evlist__prepare_workload.
2624 */
2625 if (trace->filter_pids.nr > 0) {
2626 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2627 trace->filter_pids.entries);
2628 if (!err && trace->filter_pids.map) {
2629 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2630 trace->filter_pids.entries);
2631 }
2632 } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2633 err = trace__set_filter_loop_pids(trace);
2634 }
2635
2636 return err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002637}
2638
Namhyung Kimf15eb532012-10-05 14:02:16 +09002639static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002640{
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002641 struct perf_evlist *evlist = trace->evlist;
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002642 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002643 int err = -1, i;
2644 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002645 const bool forks = argc > 0;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002646 bool draining = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002647
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002648 trace->live = true;
2649
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002650 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03002651 goto out_error_raw_syscalls;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002652
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002653 if (trace->trace_syscalls)
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002654 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002655
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002656 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2657 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2658 if (pgfault_maj == NULL)
2659 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002660 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002661 perf_evlist__add(evlist, pgfault_maj);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03002662 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002663
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002664 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2665 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2666 if (pgfault_min == NULL)
2667 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002668 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002669 perf_evlist__add(evlist, pgfault_min);
2670 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002671
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002672 if (trace->sched &&
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002673 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2674 trace__sched_stat_runtime))
2675 goto out_error_sched_stat_runtime;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002676
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03002677 /*
2678 * If a global cgroup was set, apply it to all the events without an
2679 * explicit cgroup. I.e.:
2680 *
2681 * trace -G A -e sched:*switch
2682 *
2683 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2684 * _and_ sched:sched_switch to the 'A' cgroup, while:
2685 *
2686 * trace -e sched:*switch -G A
2687 *
2688 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2689 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2690 * a cgroup (on the root cgroup, sys wide, etc).
2691 *
2692 * Multiple cgroups:
2693 *
2694 * trace -G A -e sched:*switch -G B
2695 *
2696 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2697 * to the 'B' cgroup.
2698 *
2699 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2700 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2701 */
2702 if (trace->cgroup)
2703 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2704
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002705 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2706 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002707 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002708 goto out_delete_evlist;
2709 }
2710
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002711 err = trace__symbols_init(trace, evlist);
2712 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002713 fprintf(trace->output, "Problems initializing symbol libraries!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002714 goto out_delete_evlist;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002715 }
2716
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03002717 perf_evlist__config(evlist, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melofde54b72016-04-11 18:42:37 -03002718
Namhyung Kimf15eb532012-10-05 14:02:16 +09002719 signal(SIGCHLD, sig_handler);
2720 signal(SIGINT, sig_handler);
2721
2722 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09002723 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03002724 argv, false, NULL);
Namhyung Kimf15eb532012-10-05 14:02:16 +09002725 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002726 fprintf(trace->output, "Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002727 goto out_delete_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002728 }
2729 }
2730
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002731 err = perf_evlist__open(evlist);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002732 if (err < 0)
2733 goto out_error_open;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002734
Wang Nanba504232016-02-26 09:31:54 +00002735 err = bpf__apply_obj_config();
2736 if (err) {
2737 char errbuf[BUFSIZ];
2738
2739 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2740 pr_err("ERROR: Apply config to BPF failed: %s\n",
2741 errbuf);
2742 goto out_error_open;
2743 }
2744
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002745 err = trace__set_filter_pids(trace);
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002746 if (err < 0)
2747 goto out_error_mem;
2748
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002749 if (trace->ev_qualifier_ids.nr > 0) {
2750 err = trace__set_ev_qualifier_filter(trace);
2751 if (err < 0)
2752 goto out_errno;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002753
Arnaldo Carvalho de Melo2e5e5f82015-08-03 17:12:29 -03002754 pr_debug("event qualifier tracepoint filter: %s\n",
2755 trace->syscalls.events.sys_exit->filter);
2756 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002757
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002758 err = perf_evlist__apply_filters(evlist, &evsel);
2759 if (err < 0)
2760 goto out_error_apply_filters;
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08002761
Wang Nanf74b9d3a2017-12-03 02:00:37 +00002762 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002763 if (err < 0)
2764 goto out_error_mmap;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002765
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002766 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
Arnaldo Carvalho de Melocb24d012015-04-22 10:04:23 -03002767 perf_evlist__enable(evlist);
2768
Namhyung Kimf15eb532012-10-05 14:02:16 +09002769 if (forks)
2770 perf_evlist__start_workload(evlist);
2771
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002772 if (trace->opts.initial_delay) {
2773 usleep(trace->opts.initial_delay * 1000);
2774 perf_evlist__enable(evlist);
2775 }
2776
Jiri Olsae13798c2015-06-23 00:36:02 +02002777 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
Arnaldo Carvalho de Melo42052be2015-02-13 12:32:45 -03002778 evlist->threads->nr > 1 ||
2779 perf_evlist__first(evlist)->attr.inherit;
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002780
2781 /*
2782 * Now that we already used evsel->attr to ask the kernel to setup the
2783 * events, lets reuse evsel->attr.sample_max_stack as the limit in
2784 * trace__resolve_callchain(), allowing per-event max-stack settings
Ingo Molnaradba1632018-12-03 11:22:00 +01002785 * to override an explicitly set --max-stack global setting.
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002786 */
2787 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03002788 if (evsel__has_callchain(evsel) &&
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002789 evsel->attr.sample_max_stack == 0)
2790 evsel->attr.sample_max_stack = trace->max_stack;
2791 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002792again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002793 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002794
2795 for (i = 0; i < evlist->nr_mmaps; i++) {
2796 union perf_event *event;
Kan Liangd7f55c62018-03-01 18:08:59 -05002797 struct perf_mmap *md;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002798
Kan Liangd7f55c62018-03-01 18:08:59 -05002799 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -05002800 if (perf_mmap__read_init(md) < 0)
Kan Liangd7f55c62018-03-01 18:08:59 -05002801 continue;
2802
Kan Liang0019dc872018-03-06 10:36:06 -05002803 while ((event = perf_mmap__read_event(md)) != NULL) {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002804 struct perf_sample sample;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002805
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002806 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002807
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002808 err = perf_evlist__parse_sample(evlist, event, &sample);
2809 if (err) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002810 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +08002811 goto next_event;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002812 }
2813
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002814 trace__handle_event(trace, event, &sample);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +08002815next_event:
Kan Liangd6ace3d2018-03-06 10:36:05 -05002816 perf_mmap__consume(md);
Arnaldo Carvalho de Melo20c5f102013-09-03 11:55:07 -03002817
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002818 if (interrupted)
2819 goto out_disable;
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03002820
2821 if (done && !draining) {
2822 perf_evlist__disable(evlist);
2823 draining = true;
2824 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002825 }
Kan Liangd7f55c62018-03-01 18:08:59 -05002826 perf_mmap__read_done(md);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002827 }
2828
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002829 if (trace->nr_events == before) {
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002830 int timeout = done ? 100 : -1;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002831
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002832 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002833 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002834 draining = true;
2835
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002836 goto again;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002837 }
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002838 } else {
2839 goto again;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002840 }
2841
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002842out_disable:
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03002843 thread__zput(trace->current);
2844
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002845 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002846
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002847 if (!err) {
2848 if (trace->summary)
2849 trace__fprintf_thread_summary(trace, trace->output);
2850
2851 if (trace->show_tool_stats) {
2852 fprintf(trace->output, "Stats:\n "
2853 " vfs_getname : %" PRIu64 "\n"
2854 " proc_getname: %" PRIu64 "\n",
2855 trace->stats.vfs_getname,
2856 trace->stats.proc_getname);
2857 }
2858 }
David Ahernbf2575c2013-10-08 21:26:53 -06002859
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002860out_delete_evlist:
Andrei Vagin33974a42017-11-07 16:22:45 -08002861 trace__symbols__exit(trace);
2862
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002863 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03002864 cgroup__put(trace->cgroup);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002865 trace->evlist = NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002866 trace->live = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002867 return err;
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03002868{
2869 char errbuf[BUFSIZ];
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002870
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002871out_error_sched_stat_runtime:
Jiri Olsa988bdb32015-09-02 09:56:35 +02002872 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002873 goto out_error;
2874
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03002875out_error_raw_syscalls:
Jiri Olsa988bdb32015-09-02 09:56:35 +02002876 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002877 goto out_error;
2878
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002879out_error_mmap:
2880 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
2881 goto out_error;
2882
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002883out_error_open:
2884 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
2885
2886out_error:
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03002887 fprintf(trace->output, "%s\n", errbuf);
Ramkumar Ramachandra87f91862013-10-04 10:47:31 +05302888 goto out_delete_evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002889
2890out_error_apply_filters:
2891 fprintf(trace->output,
2892 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
2893 evsel->filter, perf_evsel__name(evsel), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002894 str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002895 goto out_delete_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002896}
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03002897out_error_mem:
2898 fprintf(trace->output, "Not enough memory to run!\n");
2899 goto out_delete_evlist;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002900
2901out_errno:
2902 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
2903 goto out_delete_evlist;
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002904}
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002905
David Ahern6810fc92013-08-28 22:29:52 -06002906static int trace__replay(struct trace *trace)
2907{
2908 const struct perf_evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002909 { "probe:vfs_getname", trace__vfs_getname, },
David Ahern6810fc92013-08-28 22:29:52 -06002910 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002911 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002912 .file = {
2913 .path = input_name,
2914 },
2915 .mode = PERF_DATA_MODE_READ,
2916 .force = trace->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002917 };
David Ahern6810fc92013-08-28 22:29:52 -06002918 struct perf_session *session;
Namhyung Kim003824e2013-11-12 15:25:00 +09002919 struct perf_evsel *evsel;
David Ahern6810fc92013-08-28 22:29:52 -06002920 int err = -1;
2921
2922 trace->tool.sample = trace__process_sample;
2923 trace->tool.mmap = perf_event__process_mmap;
David Ahern384c6712013-09-22 19:44:58 -06002924 trace->tool.mmap2 = perf_event__process_mmap2;
David Ahern6810fc92013-08-28 22:29:52 -06002925 trace->tool.comm = perf_event__process_comm;
2926 trace->tool.exit = perf_event__process_exit;
2927 trace->tool.fork = perf_event__process_fork;
2928 trace->tool.attr = perf_event__process_attr;
Hari Bathinif3b36142017-03-08 02:11:43 +05302929 trace->tool.tracing_data = perf_event__process_tracing_data;
David Ahern6810fc92013-08-28 22:29:52 -06002930 trace->tool.build_id = perf_event__process_build_id;
Hari Bathinif3b36142017-03-08 02:11:43 +05302931 trace->tool.namespaces = perf_event__process_namespaces;
David Ahern6810fc92013-08-28 22:29:52 -06002932
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002933 trace->tool.ordered_events = true;
David Ahern6810fc92013-08-28 22:29:52 -06002934 trace->tool.ordering_requires_timestamps = true;
2935
2936 /* add tid to output */
2937 trace->multiple_threads = true;
2938
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002939 session = perf_session__new(&data, false, &trace->tool);
David Ahern6810fc92013-08-28 22:29:52 -06002940 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002941 return -1;
David Ahern6810fc92013-08-28 22:29:52 -06002942
David Ahernaa07df62016-11-25 09:29:52 -07002943 if (trace->opts.target.pid)
2944 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
2945
2946 if (trace->opts.target.tid)
2947 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
2948
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002949 if (symbol__init(&session->header.env) < 0)
Namhyung Kimcb2ffae2014-08-12 15:40:44 +09002950 goto out;
2951
David Ahern8fb598e2013-09-28 13:13:00 -06002952 trace->host = &session->machines.host;
2953
David Ahern6810fc92013-08-28 22:29:52 -06002954 err = perf_session__set_tracepoints_handlers(session, handlers);
2955 if (err)
2956 goto out;
2957
Namhyung Kim003824e2013-11-12 15:25:00 +09002958 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2959 "raw_syscalls:sys_enter");
David Ahern9aca7f12013-12-04 19:41:39 -07002960 /* older kernels have syscalls tp versus raw_syscalls */
2961 if (evsel == NULL)
2962 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2963 "syscalls:sys_enter");
David Ahern6810fc92013-08-28 22:29:52 -06002964
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002965 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002966 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002967 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09002968 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
2969 goto out;
2970 }
2971
2972 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2973 "raw_syscalls:sys_exit");
David Ahern9aca7f12013-12-04 19:41:39 -07002974 if (evsel == NULL)
2975 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2976 "syscalls:sys_exit");
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002977 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002978 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002979 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09002980 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
David Ahern6810fc92013-08-28 22:29:52 -06002981 goto out;
2982 }
2983
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002984 evlist__for_each_entry(session->evlist, evsel) {
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002985 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
2986 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
2987 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
2988 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
2989 evsel->handler = trace__pgfault;
2990 }
2991
David Ahern6810fc92013-08-28 22:29:52 -06002992 setup_pager();
2993
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002994 err = perf_session__process_events(session);
David Ahern6810fc92013-08-28 22:29:52 -06002995 if (err)
2996 pr_err("Failed to process events, error %d", err);
2997
David Ahernbf2575c2013-10-08 21:26:53 -06002998 else if (trace->summary)
2999 trace__fprintf_thread_summary(trace, trace->output);
3000
David Ahern6810fc92013-08-28 22:29:52 -06003001out:
3002 perf_session__delete(session);
3003
3004 return err;
3005}
3006
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003007static size_t trace__fprintf_threads_header(FILE *fp)
3008{
3009 size_t printed;
3010
Pekka Enberg99ff7152013-11-12 16:42:14 +02003011 printed = fprintf(fp, "\n Summary of events:\n\n");
David Ahernbf2575c2013-10-08 21:26:53 -06003012
3013 return printed;
3014}
3015
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003016DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3017 struct stats *stats;
3018 double msecs;
3019 int syscall;
3020)
3021{
3022 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3023 struct stats *stats = source->priv;
3024
3025 entry->syscall = source->i;
3026 entry->stats = stats;
3027 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3028}
3029
David Ahernbf2575c2013-10-08 21:26:53 -06003030static size_t thread__dump_stats(struct thread_trace *ttrace,
3031 struct trace *trace, FILE *fp)
3032{
David Ahernbf2575c2013-10-08 21:26:53 -06003033 size_t printed = 0;
3034 struct syscall *sc;
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003035 struct rb_node *nd;
3036 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003037
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003038 if (syscall_stats == NULL)
David Ahernbf2575c2013-10-08 21:26:53 -06003039 return 0;
3040
3041 printed += fprintf(fp, "\n");
3042
Milian Wolff834fd462015-08-06 11:24:29 +02003043 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3044 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3045 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
Pekka Enberg99ff7152013-11-12 16:42:14 +02003046
Arnaldo Carvalho de Melo98a91832016-06-23 11:34:10 -03003047 resort_rb__for_each_entry(nd, syscall_stats) {
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003048 struct stats *stats = syscall_stats_entry->stats;
David Ahernbf2575c2013-10-08 21:26:53 -06003049 if (stats) {
3050 double min = (double)(stats->min) / NSEC_PER_MSEC;
3051 double max = (double)(stats->max) / NSEC_PER_MSEC;
3052 double avg = avg_stats(stats);
3053 double pct;
3054 u64 n = (u64) stats->n;
3055
3056 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3057 avg /= NSEC_PER_MSEC;
3058
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003059 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
Pekka Enberg99ff7152013-11-12 16:42:14 +02003060 printed += fprintf(fp, " %-15s", sc->name);
Milian Wolff834fd462015-08-06 11:24:29 +02003061 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003062 n, syscall_stats_entry->msecs, min, avg);
Pekka Enberg27a778b2013-11-13 14:21:48 +02003063 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
David Ahernbf2575c2013-10-08 21:26:53 -06003064 }
David Ahernbf2575c2013-10-08 21:26:53 -06003065 }
3066
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003067 resort_rb__delete(syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003068 printed += fprintf(fp, "\n\n");
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003069
3070 return printed;
3071}
3072
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003073static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
David Ahern896cbb52013-09-28 13:12:59 -06003074{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003075 size_t printed = 0;
Namhyung Kim89dceb22014-10-06 09:46:03 +09003076 struct thread_trace *ttrace = thread__priv(thread);
David Ahern896cbb52013-09-28 13:12:59 -06003077 double ratio;
3078
3079 if (ttrace == NULL)
3080 return 0;
3081
3082 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3083
Pekka Enberg15e65c62013-11-14 18:43:30 +02003084 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
Pekka Enberg99ff7152013-11-12 16:42:14 +02003085 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
Pekka Enberg15e65c62013-11-14 18:43:30 +02003086 printed += fprintf(fp, "%.1f%%", ratio);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04003087 if (ttrace->pfmaj)
3088 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3089 if (ttrace->pfmin)
3090 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
Arnaldo Carvalho de Melo03548eb2016-05-05 15:46:50 -03003091 if (trace->sched)
3092 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3093 else if (fputc('\n', fp) != EOF)
3094 ++printed;
3095
David Ahernbf2575c2013-10-08 21:26:53 -06003096 printed += thread__dump_stats(ttrace, trace, fp);
David Ahern896cbb52013-09-28 13:12:59 -06003097
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003098 return printed;
3099}
David Ahern896cbb52013-09-28 13:12:59 -06003100
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003101static unsigned long thread__nr_events(struct thread_trace *ttrace)
3102{
3103 return ttrace ? ttrace->nr_events : 0;
3104}
3105
3106DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3107 struct thread *thread;
3108)
3109{
3110 entry->thread = rb_entry(nd, struct thread, rb_node);
David Ahern896cbb52013-09-28 13:12:59 -06003111}
3112
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003113static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3114{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003115 size_t printed = trace__fprintf_threads_header(fp);
3116 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -07003117 int i;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003118
Kan Liang91e467b2017-09-10 19:23:14 -07003119 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3120 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3121
3122 if (threads == NULL) {
3123 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3124 return 0;
3125 }
3126
3127 resort_rb__for_each_entry(nd, threads)
3128 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3129
3130 resort_rb__delete(threads);
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003131 }
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003132 return printed;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003133}
3134
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003135static int trace__set_duration(const struct option *opt, const char *str,
3136 int unset __maybe_unused)
3137{
3138 struct trace *trace = opt->value;
3139
3140 trace->duration_filter = atof(str);
3141 return 0;
3142}
3143
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003144static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3145 int unset __maybe_unused)
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08003146{
3147 int ret = -1;
3148 size_t i;
3149 struct trace *trace = opt->value;
3150 /*
3151 * FIXME: introduce a intarray class, plain parse csv and create a
3152 * { int nr, int entries[] } struct...
3153 */
3154 struct intlist *list = intlist__new(str);
3155
3156 if (list == NULL)
3157 return -1;
3158
3159 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3160 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3161
3162 if (trace->filter_pids.entries == NULL)
3163 goto out;
3164
3165 trace->filter_pids.entries[0] = getpid();
3166
3167 for (i = 1; i < trace->filter_pids.nr; ++i)
3168 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3169
3170 intlist__delete(list);
3171 ret = 0;
3172out:
3173 return ret;
3174}
3175
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003176static int trace__open_output(struct trace *trace, const char *filename)
3177{
3178 struct stat st;
3179
3180 if (!stat(filename, &st) && st.st_size) {
3181 char oldname[PATH_MAX];
3182
3183 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3184 unlink(oldname);
3185 rename(filename, oldname);
3186 }
3187
3188 trace->output = fopen(filename, "w");
3189
3190 return trace->output == NULL ? -errno : 0;
3191}
3192
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003193static int parse_pagefaults(const struct option *opt, const char *str,
3194 int unset __maybe_unused)
3195{
3196 int *trace_pgfaults = opt->value;
3197
3198 if (strcmp(str, "all") == 0)
3199 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3200 else if (strcmp(str, "maj") == 0)
3201 *trace_pgfaults |= TRACE_PFMAJ;
3202 else if (strcmp(str, "min") == 0)
3203 *trace_pgfaults |= TRACE_PFMIN;
3204 else
3205 return -1;
3206
3207 return 0;
3208}
3209
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003210static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3211{
3212 struct perf_evsel *evsel;
3213
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003214 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003215 evsel->handler = handler;
3216}
3217
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003218static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3219{
3220 struct perf_evsel *evsel;
3221
3222 evlist__for_each_entry(evlist, evsel) {
3223 if (evsel->priv || !evsel->tp_format)
3224 continue;
3225
3226 if (strcmp(evsel->tp_format->system, "syscalls"))
3227 continue;
3228
3229 if (perf_evsel__init_syscall_tp(evsel))
3230 return -1;
3231
3232 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3233 struct syscall_tp *sc = evsel->priv;
3234
3235 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3236 return -1;
3237 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3238 struct syscall_tp *sc = evsel->priv;
3239
3240 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3241 return -1;
3242 }
3243 }
3244
3245 return 0;
3246}
3247
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003248/*
3249 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3250 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3251 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3252 *
3253 * It'd be better to introduce a parse_options() variant that would return a
3254 * list with the terms it didn't match to an event...
3255 */
3256static int trace__parse_events_option(const struct option *opt, const char *str,
3257 int unset __maybe_unused)
3258{
3259 struct trace *trace = (struct trace *)opt->value;
3260 const char *s = str;
3261 char *sep = NULL, *lists[2] = { NULL, NULL, };
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003262 int len = strlen(str) + 1, err = -1, list, idx;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003263 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3264 char group_name[PATH_MAX];
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003265 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003266
3267 if (strace_groups_dir == NULL)
3268 return -1;
3269
3270 if (*s == '!') {
3271 ++s;
3272 trace->not_ev_qualifier = true;
3273 }
3274
3275 while (1) {
3276 if ((sep = strchr(s, ',')) != NULL)
3277 *sep = '\0';
3278
3279 list = 0;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003280 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3281 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003282 list = 1;
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003283 goto do_concat;
3284 }
3285
3286 fmt = syscall_fmt__find_by_alias(s);
3287 if (fmt != NULL) {
3288 list = 1;
3289 s = fmt->name;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003290 } else {
3291 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3292 if (access(group_name, R_OK) == 0)
3293 list = 1;
3294 }
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003295do_concat:
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003296 if (lists[list]) {
3297 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3298 } else {
3299 lists[list] = malloc(len);
3300 if (lists[list] == NULL)
3301 goto out;
3302 strcpy(lists[list], s);
3303 }
3304
3305 if (!sep)
3306 break;
3307
3308 *sep = ',';
3309 s = sep + 1;
3310 }
3311
3312 if (lists[1] != NULL) {
3313 struct strlist_config slist_config = {
3314 .dirname = strace_groups_dir,
3315 };
3316
3317 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3318 if (trace->ev_qualifier == NULL) {
3319 fputs("Not enough memory to parse event qualifier", trace->output);
3320 goto out;
3321 }
3322
3323 if (trace__validate_ev_qualifier(trace))
3324 goto out;
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003325 trace->trace_syscalls = true;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003326 }
3327
3328 err = 0;
3329
3330 if (lists[0]) {
3331 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3332 "event selector. use 'perf list' to list available events",
3333 parse_events_option);
3334 err = parse_events_option(&o, lists[0], 0);
3335 }
3336out:
3337 if (sep)
3338 *sep = ',';
3339
3340 return err;
3341}
3342
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003343static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3344{
3345 struct trace *trace = opt->value;
3346
3347 if (!list_empty(&trace->evlist->entries))
3348 return parse_cgroups(opt, str, unset);
3349
3350 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3351
3352 return 0;
3353}
3354
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003355static struct bpf_map *bpf__find_map_by_name(const char *name)
3356{
3357 struct bpf_object *obj, *tmp;
3358
3359 bpf_object__for_each_safe(obj, tmp) {
3360 struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3361 if (map)
3362 return map;
3363
3364 }
3365
3366 return NULL;
3367}
3368
3369static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3370{
3371 trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3372}
3373
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003374int cmd_trace(int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003375{
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003376 const char *trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +09003377 "perf trace [<options>] [<command>]",
3378 "perf trace [<options>] -- <command> [<options>]",
David Ahern5e2485b2013-09-28 13:13:01 -06003379 "perf trace record [<options>] [<command>]",
3380 "perf trace record [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003381 NULL
3382 };
3383 struct trace trace = {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003384 .syscalls = {
3385 . max = -1,
3386 },
3387 .opts = {
3388 .target = {
3389 .uid = UINT_MAX,
3390 .uses_mmap = true,
3391 },
3392 .user_freq = UINT_MAX,
3393 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03003394 .no_buffering = true,
Arnaldo Carvalho de Melo38d54472014-12-12 17:28:32 -03003395 .mmap_pages = UINT_MAX,
Kan Liang9d9cad72015-06-17 09:51:11 -04003396 .proc_map_timeout = 500,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003397 },
Milian Wolff007d66a2015-08-05 16:52:23 -03003398 .output = stderr,
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003399 .show_comm = true,
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003400 .trace_syscalls = false,
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003401 .kernel_syscallchains = false,
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003402 .max_stack = UINT_MAX,
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003403 .max_events = ULONG_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003404 };
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003405 const char *output_name = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003406 const struct option trace_options[] = {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003407 OPT_CALLBACK('e', "event", &trace, "event",
3408 "event/syscall selector. use 'perf list' to list available events",
3409 trace__parse_events_option),
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003410 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3411 "show the thread COMM next to its id"),
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003412 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003413 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3414 trace__parse_events_option),
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003415 OPT_STRING('o', "output", &output_name, "file", "output file name"),
David Ahern6810fc92013-08-28 22:29:52 -06003416 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003417 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3418 "trace events on existing process id"),
David Ahernac9be8e2013-08-20 11:15:45 -06003419 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003420 "trace events on existing thread id"),
Arnaldo Carvalho de Melofa0e4ff2015-04-23 11:59:20 -03003421 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003422 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
David Ahernac9be8e2013-08-20 11:15:45 -06003423 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003424 "system-wide collection from all CPUs"),
David Ahernac9be8e2013-08-20 11:15:45 -06003425 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003426 "list of cpus to monitor"),
David Ahern6810fc92013-08-28 22:29:52 -06003427 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003428 "child tasks do not inherit counters"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02003429 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3430 "number of mmap data pages",
3431 perf_evlist__parse_mmap_pages),
David Ahernac9be8e2013-08-20 11:15:45 -06003432 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003433 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003434 OPT_CALLBACK(0, "duration", &trace, "float",
3435 "show only events with duration > N.M ms",
3436 trace__set_duration),
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003437 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03003438 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
David Ahern4bb09192013-09-04 12:37:43 -06003439 OPT_BOOLEAN('T', "time", &trace.full_time,
3440 "Show full timestamp, not time relative to first start"),
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03003441 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3442 "Show only syscalls that failed"),
David Ahernfd2eaba2013-11-12 09:31:15 -07003443 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3444 "Show only syscall summary with statistics"),
3445 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3446 "Show all syscalls and summary with statistics"),
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003447 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3448 "Trace pagefaults", parse_pagefaults, "maj"),
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003449 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
Yunlong Songe366a6d2015-04-02 21:47:18 +08003450 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
Milian Wolff566a0882016-04-08 13:34:15 +02003451 OPT_CALLBACK(0, "call-graph", &trace.opts,
3452 "record_mode[,record_size]", record_callchain_help,
3453 &record_parse_callchain_opt),
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003454 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3455 "Show the kernel callchains on the syscall exit path"),
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003456 OPT_ULONG(0, "max-events", &trace.max_events,
3457 "Set the maximum number of events to print, exit after that is reached. "),
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03003458 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3459 "Set the minimum stack depth when parsing the callchain, "
3460 "anything below the specified depth will be ignored."),
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -03003461 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3462 "Set the maximum stack depth when parsing the callchain, "
3463 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003464 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03003465 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3466 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
Kan Liang9d9cad72015-06-17 09:51:11 -04003467 OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
3468 "per thread proc mmap processing timeout in ms"),
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003469 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3470 trace__parse_cgroups),
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003471 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3472 "ms to wait before starting measurement after program "
3473 "start"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003474 OPT_END()
3475 };
Arnaldo Carvalho de Meloccd62a82016-04-16 09:36:32 -03003476 bool __maybe_unused max_stack_user_set = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003477 bool mmap_pages_user_set = true;
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003478 struct perf_evsel *evsel;
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003479 const char * const trace_subcommands[] = { "record", NULL };
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003480 int err = -1;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003481 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003482
Arnaldo Carvalho de Melo4d08cb82015-02-24 15:35:55 -03003483 signal(SIGSEGV, sighandler_dump_stack);
3484 signal(SIGFPE, sighandler_dump_stack);
3485
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003486 trace.evlist = perf_evlist__new();
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003487 trace.sctbl = syscalltbl__new();
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003488
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003489 if (trace.evlist == NULL || trace.sctbl == NULL) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003490 pr_err("Not enough memory to run!\n");
He Kuangff8f6952015-05-11 12:28:36 +00003491 err = -ENOMEM;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003492 goto out;
3493 }
3494
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003495 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3496 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
David Ahernfd2eaba2013-11-12 09:31:15 -07003497
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003498 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3499 usage_with_options_msg(trace_usage, trace_options,
3500 "cgroup monitoring only available in system-wide mode");
3501 }
3502
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003503 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3504 if (IS_ERR(evsel)) {
3505 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
Arnaldo Carvalho de Meloe0b6d2e2018-08-07 15:40:13 -03003506 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3507 goto out;
3508 }
3509
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003510 if (evsel) {
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003511 trace.syscalls.events.augmented = evsel;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003512 trace__set_bpf_map_filtered_pids(&trace);
3513 }
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003514
Wang Nand7888572016-04-08 15:07:24 +00003515 err = bpf__setup_stdout(trace.evlist);
3516 if (err) {
3517 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3518 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3519 goto out;
3520 }
3521
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003522 err = -1;
3523
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003524 if (trace.trace_pgfaults) {
3525 trace.opts.sample_address = true;
3526 trace.opts.sample_time = true;
3527 }
3528
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003529 if (trace.opts.mmap_pages == UINT_MAX)
3530 mmap_pages_user_set = false;
3531
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003532 if (trace.max_stack == UINT_MAX) {
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03003533 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003534 max_stack_user_set = false;
3535 }
3536
3537#ifdef HAVE_DWARF_UNWIND_SUPPORT
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003538 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003539 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003540 }
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003541#endif
3542
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03003543 if (callchain_param.enabled) {
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003544 if (!mmap_pages_user_set && geteuid() == 0)
3545 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3546
Milian Wolff566a0882016-04-08 13:34:15 +02003547 symbol_conf.use_callchain = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003548 }
Milian Wolff566a0882016-04-08 13:34:15 +02003549
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003550 if (trace.evlist->nr_entries > 0) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003551 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003552 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3553 perror("failed to set syscalls:* tracepoint fields");
3554 goto out;
3555 }
3556 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003557
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003558 /*
3559 * If we are augmenting syscalls, then combine what we put in the
3560 * __augmented_syscalls__ BPF map with what is in the
3561 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3562 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3563 *
3564 * We'll switch to look at two BPF maps, one for sys_enter and the
3565 * other for sys_exit when we start augmenting the sys_exit paths with
3566 * buffers that are being copied from kernel to userspace, think 'read'
3567 * syscall.
3568 */
3569 if (trace.syscalls.events.augmented) {
3570 evsel = trace.syscalls.events.augmented;
3571
3572 if (perf_evsel__init_augmented_syscall_tp(evsel) ||
3573 perf_evsel__init_augmented_syscall_tp_args(evsel))
3574 goto out;
3575 evsel->handler = trace__sys_enter;
3576
3577 evlist__for_each_entry(trace.evlist, evsel) {
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003578 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3579
3580 if (raw_syscalls_sys_exit) {
3581 trace.raw_augmented_syscalls = true;
3582 goto init_augmented_syscall_tp;
3583 }
3584
3585 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
3586init_augmented_syscall_tp:
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003587 perf_evsel__init_augmented_syscall_tp(evsel);
3588 perf_evsel__init_augmented_syscall_tp_ret(evsel);
3589 evsel->handler = trace__sys_exit;
3590 }
3591 }
3592 }
3593
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003594 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3595 return trace__record(&trace, argc-1, &argv[1]);
3596
3597 /* summary_only implies summary option, but don't overwrite summary if set */
3598 if (trace.summary_only)
3599 trace.summary = trace.summary_only;
3600
Arnaldo Carvalho de Melo726f3232015-02-06 10:16:45 +01003601 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3602 trace.evlist->nr_entries == 0 /* Was --events used? */) {
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003603 trace.trace_syscalls = true;
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003604 }
3605
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003606 if (output_name != NULL) {
3607 err = trace__open_output(&trace, output_name);
3608 if (err < 0) {
3609 perror("failed to create output file");
3610 goto out;
3611 }
3612 }
3613
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003614 err = target__validate(&trace.opts.target);
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003615 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003616 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003617 fprintf(trace.output, "%s", bf);
3618 goto out_close;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003619 }
3620
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003621 err = target__parse_uid(&trace.opts.target);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003622 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003623 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003624 fprintf(trace.output, "%s", bf);
3625 goto out_close;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003626 }
3627
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003628 if (!argc && target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +09003629 trace.opts.target.system_wide = true;
3630
David Ahern6810fc92013-08-28 22:29:52 -06003631 if (input_name)
3632 err = trace__replay(&trace);
3633 else
3634 err = trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003635
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003636out_close:
3637 if (output_name != NULL)
3638 fclose(trace.output);
3639out:
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003640 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003641}