blob: d754a74aef4625285686600610d07603b3448e67 [file] [log] [blame]
Arnaldo Carvalho de Meloa598bb52015-08-28 12:02:37 -03001/*
2 * builtin-trace.c
3 *
4 * Builtin 'trace' command:
5 *
6 * Display a continuously updated trace of any workload, CPU, specific PID,
7 * system wide, etc. Default format is loosely strace like, but any other
8 * event may be specified using --event.
9 *
10 * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
11 *
12 * Initially based on the 'trace' prototype by Thomas Gleixner:
13 *
14 * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
15 *
16 * Released under the GPL v2. (and only v2, not any later version)
17 */
18
Robert Richter4e319022013-06-11 17:29:18 +020019#include <traceevent/event-parse.h>
Jiri Olsa988bdb32015-09-02 09:56:35 +020020#include <api/fs/tracing_path.h>
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -030021#include <bpf/bpf.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030022#include "builtin.h"
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030023#include "util/cgroup.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030024#include "util/color.h"
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -030025#include "util/config.h"
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -030026#include "util/debug.h"
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +010027#include "util/env.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030028#include "util/event.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030029#include "util/evlist.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060030#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030031#include "util/machine.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030032#include "util/path.h"
David Ahern6810fc92013-08-28 22:29:52 -060033#include "util/session.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030034#include "util/thread.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060035#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -030036#include "util/strlist.h"
David Ahernbdc89662013-08-28 22:29:53 -060037#include "util/intlist.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030038#include "util/thread_map.h"
David Ahernbf2575c2013-10-08 21:26:53 -060039#include "util/stat.h"
Arnaldo Carvalho de Melofd5cead2017-03-14 16:19:30 -030040#include "trace/beauty/beauty.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010041#include "trace-event.h"
David Ahern9aca7f12013-12-04 19:41:39 -070042#include "util/parse-events.h"
Wang Nanba504232016-02-26 09:31:54 +000043#include "util/bpf-loader.h"
Milian Wolff566a0882016-04-08 13:34:15 +020044#include "callchain.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030045#include "print_binary.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030046#include "string2.h"
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030047#include "syscalltbl.h"
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -030048#include "rb_resort.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030049
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030050#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030051#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030052#include <poll.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030053#include <signal.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030054#include <stdlib.h>
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -030055#include <string.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020056#include <linux/err.h>
Arnaldo Carvalho de Melo997bba82016-03-30 19:43:32 -030057#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030058#include <linux/kernel.h>
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -030059#include <linux/random.h>
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -030060#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030061#include <linux/time64.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030062#include <fcntl.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030063
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030064#include "sane_ctype.h"
65
Arnaldo Carvalho de Meloc188e7a2015-05-14 17:39:03 -030066#ifndef O_CLOEXEC
67# define O_CLOEXEC 02000000
68#endif
69
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -030070#ifndef F_LINUX_SPECIFIC_BASE
71# define F_LINUX_SPECIFIC_BASE 1024
72#endif
73
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030074struct trace {
75 struct perf_tool tool;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030076 struct syscalltbl *sctbl;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030077 struct {
78 int max;
79 struct syscall *table;
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -030080 struct bpf_map *map;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030081 struct {
82 struct perf_evsel *sys_enter,
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -030083 *sys_exit,
84 *augmented;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030085 } events;
86 } syscalls;
87 struct record_opts opts;
88 struct perf_evlist *evlist;
89 struct machine *host;
90 struct thread *current;
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030091 struct cgroup *cgroup;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030092 u64 base_time;
93 FILE *output;
94 unsigned long nr_events;
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -030095 unsigned long nr_events_printed;
96 unsigned long max_events;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030097 struct strlist *ev_qualifier;
98 struct {
99 size_t nr;
100 int *entries;
101 } ev_qualifier_ids;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300102 struct {
103 size_t nr;
104 pid_t *entries;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -0300105 struct bpf_map *map;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300106 } filter_pids;
107 double duration_filter;
108 double runtime_ms;
109 struct {
110 u64 vfs_getname,
111 proc_getname;
112 } stats;
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -0300113 unsigned int max_stack;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -0300114 unsigned int min_stack;
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -0300115 bool sort_events;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -0300116 bool raw_augmented_syscalls;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300117 bool not_ev_qualifier;
118 bool live;
119 bool full_time;
120 bool sched;
121 bool multiple_threads;
122 bool summary;
123 bool summary_only;
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -0300124 bool failure_only;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300125 bool show_comm;
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -0300126 bool print_sample;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300127 bool show_tool_stats;
128 bool trace_syscalls;
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -0300129 bool kernel_syscallchains;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300130 bool force;
131 bool vfs_getname;
132 int trace_pgfaults;
Jiri Olsa028713a2018-12-05 17:05:09 +0100133 struct {
134 struct ordered_events data;
135 u64 last;
136 } oe;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300137};
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300138
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300139struct tp_field {
140 int offset;
141 union {
142 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
143 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
144 };
145};
146
147#define TP_UINT_FIELD(bits) \
148static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
149{ \
David Ahern55d43bc2015-02-19 15:00:22 -0500150 u##bits value; \
151 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
152 return value; \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300153}
154
155TP_UINT_FIELD(8);
156TP_UINT_FIELD(16);
157TP_UINT_FIELD(32);
158TP_UINT_FIELD(64);
159
160#define TP_UINT_FIELD__SWAPPED(bits) \
161static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
162{ \
David Ahern55d43bc2015-02-19 15:00:22 -0500163 u##bits value; \
164 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300165 return bswap_##bits(value);\
166}
167
168TP_UINT_FIELD__SWAPPED(16);
169TP_UINT_FIELD__SWAPPED(32);
170TP_UINT_FIELD__SWAPPED(64);
171
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300172static 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 -0300173{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300174 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300175
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300176 switch (size) {
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300177 case 1:
178 field->integer = tp_field__u8;
179 break;
180 case 2:
181 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
182 break;
183 case 4:
184 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
185 break;
186 case 8:
187 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
188 break;
189 default:
190 return -1;
191 }
192
193 return 0;
194}
195
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400196static 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 -0300197{
198 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
199}
200
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300201static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
202{
203 return sample->raw_data + field->offset;
204}
205
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300206static int __tp_field__init_ptr(struct tp_field *field, int offset)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300207{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300208 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300209 field->pointer = tp_field__ptr;
210 return 0;
211}
212
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400213static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300214{
215 return __tp_field__init_ptr(field, format_field->offset);
216}
217
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300218struct syscall_tp {
219 struct tp_field id;
220 union {
221 struct tp_field args, ret;
222 };
223};
224
225static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
226 struct tp_field *field,
227 const char *name)
228{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400229 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300230
231 if (format_field == NULL)
232 return -1;
233
234 return tp_field__init_uint(field, format_field, evsel->needs_swap);
235}
236
237#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
238 ({ struct syscall_tp *sc = evsel->priv;\
239 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
240
241static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
242 struct tp_field *field,
243 const char *name)
244{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400245 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300246
247 if (format_field == NULL)
248 return -1;
249
250 return tp_field__init_ptr(field, format_field);
251}
252
253#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
254 ({ struct syscall_tp *sc = evsel->priv;\
255 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
256
257static void perf_evsel__delete_priv(struct perf_evsel *evsel)
258{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300259 zfree(&evsel->priv);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300260 perf_evsel__delete(evsel);
261}
262
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300263static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
264{
265 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
266
267 if (evsel->priv != NULL) {
Arnaldo Carvalho de Melo42da4382018-12-05 13:08:11 -0300268 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
269 perf_evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300270 goto out_delete;
271 return 0;
272 }
273
274 return -ENOMEM;
275out_delete:
276 zfree(&evsel->priv);
277 return -ENOENT;
278}
279
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300280static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel)
281{
282 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
283
284 if (evsel->priv != NULL) { /* field, sizeof_field, offsetof_field */
285 if (__tp_field__init_uint(&sc->id, sizeof(long), sizeof(long long), evsel->needs_swap))
286 goto out_delete;
287
288 return 0;
289 }
290
291 return -ENOMEM;
292out_delete:
293 zfree(&evsel->priv);
294 return -EINVAL;
295}
296
297static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
298{
299 struct syscall_tp *sc = evsel->priv;
300
301 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
302}
303
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -0300304static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
305{
306 struct syscall_tp *sc = evsel->priv;
307
308 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
309}
310
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300311static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
Namhyung Kim96695d42013-11-12 08:51:45 -0300312{
313 evsel->priv = malloc(sizeof(struct syscall_tp));
314 if (evsel->priv != NULL) {
315 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
316 goto out_delete;
317
318 evsel->handler = handler;
319 return 0;
320 }
321
322 return -ENOMEM;
323
324out_delete:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300325 zfree(&evsel->priv);
Namhyung Kim96695d42013-11-12 08:51:45 -0300326 return -ENOENT;
327}
328
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300329static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300330{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300331 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300332
David Ahern9aca7f12013-12-04 19:41:39 -0700333 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200334 if (IS_ERR(evsel))
David Ahern9aca7f12013-12-04 19:41:39 -0700335 evsel = perf_evsel__newtp("syscalls", direction);
336
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200337 if (IS_ERR(evsel))
338 return NULL;
339
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300340 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200341 goto out_delete;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300342
343 return evsel;
344
345out_delete:
346 perf_evsel__delete_priv(evsel);
347 return NULL;
348}
349
350#define perf_evsel__sc_tp_uint(evsel, name, sample) \
351 ({ struct syscall_tp *fields = evsel->priv; \
352 fields->name.integer(&fields->name, sample); })
353
354#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
355 ({ struct syscall_tp *fields = evsel->priv; \
356 fields->name.pointer(&fields->name, sample); })
357
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300358size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, int val)
359{
360 int idx = val - sa->offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300361
Arnaldo Carvalho de Melobc972ad2018-07-26 15:30:33 -0300362 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL)
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300363 return scnprintf(bf, size, intfmt, val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300364
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300365 return scnprintf(bf, size, "%s", sa->entries[idx]);
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300366}
367
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300368static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
369 const char *intfmt,
370 struct syscall_arg *arg)
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300371{
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300372 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300373}
374
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300375static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
376 struct syscall_arg *arg)
377{
378 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
379}
380
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300381#define SCA_STRARRAY syscall_arg__scnprintf_strarray
382
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300383struct strarrays {
384 int nr_entries;
385 struct strarray **entries;
386};
387
388#define DEFINE_STRARRAYS(array) struct strarrays strarrays__##array = { \
389 .nr_entries = ARRAY_SIZE(array), \
390 .entries = array, \
391}
392
Arnaldo Carvalho de Melo274e86f2017-07-14 09:38:38 -0300393size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
394 struct syscall_arg *arg)
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300395{
396 struct strarrays *sas = arg->parm;
397 int i;
398
399 for (i = 0; i < sas->nr_entries; ++i) {
400 struct strarray *sa = sas->entries[i];
401 int idx = arg->val - sa->offset;
402
403 if (idx >= 0 && idx < sa->nr_entries) {
404 if (sa->entries[idx] == NULL)
405 break;
406 return scnprintf(bf, size, "%s", sa->entries[idx]);
407 }
408 }
409
410 return scnprintf(bf, size, "%d", arg->val);
411}
412
Arnaldo Carvalho de Melo48e1f912016-07-05 14:43:27 -0300413#ifndef AT_FDCWD
414#define AT_FDCWD -100
415#endif
416
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300417static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
418 struct syscall_arg *arg)
419{
420 int fd = arg->val;
421
422 if (fd == AT_FDCWD)
423 return scnprintf(bf, size, "CWD");
424
425 return syscall_arg__scnprintf_fd(bf, size, arg);
426}
427
428#define SCA_FDAT syscall_arg__scnprintf_fd_at
429
430static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
431 struct syscall_arg *arg);
432
433#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
434
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300435size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300436{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300437 return scnprintf(bf, size, "%#lx", arg->val);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300438}
439
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300440size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300441{
442 return scnprintf(bf, size, "%d", arg->val);
443}
444
Arnaldo Carvalho de Melo5dde91e2017-07-14 10:34:16 -0300445size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
446{
447 return scnprintf(bf, size, "%ld", arg->val);
448}
449
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300450static const char *bpf_cmd[] = {
451 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
452 "MAP_GET_NEXT_KEY", "PROG_LOAD",
453};
454static DEFINE_STRARRAY(bpf_cmd);
455
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300456static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
457static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1);
Arnaldo Carvalho de Meloeac032c2013-09-20 11:27:32 -0300458
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300459static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
460static DEFINE_STRARRAY(itimers);
461
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300462static const char *keyctl_options[] = {
463 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
464 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
465 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
466 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
467 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
468};
469static DEFINE_STRARRAY(keyctl_options);
470
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300471static const char *whences[] = { "SET", "CUR", "END",
472#ifdef SEEK_DATA
473"DATA",
474#endif
475#ifdef SEEK_HOLE
476"HOLE",
477#endif
478};
479static DEFINE_STRARRAY(whences);
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300480
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300481static const char *fcntl_cmds[] = {
482 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
Arnaldo Carvalho de Meloe000e5e2017-07-13 13:07:00 -0300483 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
484 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
485 "GETOWNER_UIDS",
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300486};
487static DEFINE_STRARRAY(fcntl_cmds);
488
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300489static const char *fcntl_linux_specific_cmds[] = {
490 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
491 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
Arnaldo Carvalho de Melo64e45612017-07-13 17:34:46 -0300492 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300493};
494
495static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, F_LINUX_SPECIFIC_BASE);
496
497static struct strarray *fcntl_cmds_arrays[] = {
498 &strarray__fcntl_cmds,
499 &strarray__fcntl_linux_specific_cmds,
500};
501
502static DEFINE_STRARRAYS(fcntl_cmds_arrays);
503
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300504static const char *rlimit_resources[] = {
505 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
506 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
507 "RTTIME",
508};
509static DEFINE_STRARRAY(rlimit_resources);
510
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300511static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
512static DEFINE_STRARRAY(sighow);
513
David Ahern4f8c1b72013-09-22 19:45:00 -0600514static const char *clockid[] = {
515 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
Arnaldo Carvalho de Melo28ebb872015-08-11 10:38:38 -0300516 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
517 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
David Ahern4f8c1b72013-09-22 19:45:00 -0600518};
519static DEFINE_STRARRAY(clockid);
520
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300521static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
522 struct syscall_arg *arg)
523{
524 size_t printed = 0;
525 int mode = arg->val;
526
527 if (mode == F_OK) /* 0 */
528 return scnprintf(bf, size, "F");
529#define P_MODE(n) \
530 if (mode & n##_OK) { \
531 printed += scnprintf(bf + printed, size - printed, "%s", #n); \
532 mode &= ~n##_OK; \
533 }
534
535 P_MODE(R);
536 P_MODE(W);
537 P_MODE(X);
538#undef P_MODE
539
540 if (mode)
541 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
542
543 return printed;
544}
545
546#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
547
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300548static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
549 struct syscall_arg *arg);
550
551#define SCA_FILENAME syscall_arg__scnprintf_filename
552
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300553static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
554 struct syscall_arg *arg)
555{
556 int printed = 0, flags = arg->val;
557
558#define P_FLAG(n) \
559 if (flags & O_##n) { \
560 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
561 flags &= ~O_##n; \
562 }
563
564 P_FLAG(CLOEXEC);
565 P_FLAG(NONBLOCK);
566#undef P_FLAG
567
568 if (flags)
569 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
570
571 return printed;
572}
573
574#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
575
Arnaldo Carvalho de Meloa355a612016-04-13 11:55:18 -0300576#ifndef GRND_NONBLOCK
577#define GRND_NONBLOCK 0x0001
578#endif
579#ifndef GRND_RANDOM
580#define GRND_RANDOM 0x0002
581#endif
582
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300583static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
584 struct syscall_arg *arg)
585{
586 int printed = 0, flags = arg->val;
587
588#define P_FLAG(n) \
589 if (flags & GRND_##n) { \
590 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
591 flags &= ~GRND_##n; \
592 }
593
594 P_FLAG(RANDOM);
595 P_FLAG(NONBLOCK);
596#undef P_FLAG
597
598 if (flags)
599 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
600
601 return printed;
602}
603
604#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
605
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300606#define STRARRAY(name, array) \
607 { .scnprintf = SCA_STRARRAY, \
608 .parm = &strarray__##array, }
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -0300609
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +0100610#include "trace/beauty/arch_errno_names.c"
Arnaldo Carvalho de Meloea8dc3c2016-04-13 12:10:19 -0300611#include "trace/beauty/eventfd.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300612#include "trace/beauty/futex_op.c"
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300613#include "trace/beauty/futex_val3.c"
Arnaldo Carvalho de Melodf4cb162016-04-13 12:05:44 -0300614#include "trace/beauty/mmap.c"
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -0300615#include "trace/beauty/mode_t.c"
Arnaldo Carvalho de Meloa30e6252016-04-27 19:01:52 -0300616#include "trace/beauty/msg_flags.c"
Arnaldo Carvalho de Melo8f48df62016-05-06 10:02:32 -0300617#include "trace/beauty/open_flags.c"
Arnaldo Carvalho de Melo62de3442016-04-26 11:03:03 -0300618#include "trace/beauty/perf_event_open.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300619#include "trace/beauty/pid.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300620#include "trace/beauty/sched_policy.c"
Arnaldo Carvalho de Melof5cd95e2016-05-11 10:32:20 -0300621#include "trace/beauty/seccomp.c"
Arnaldo Carvalho de Melo12199d82016-05-06 09:58:02 -0300622#include "trace/beauty/signum.c"
Arnaldo Carvalho de Melobbf86c42016-04-14 13:53:10 -0300623#include "trace/beauty/socket_type.c"
Arnaldo Carvalho de Melo7206b902016-04-06 14:11:36 -0300624#include "trace/beauty/waitid_options.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300625
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300626struct syscall_arg_fmt {
627 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -0300628 unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300629 void *parm;
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -0300630 const char *name;
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -0300631 bool show_zero;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300632};
633
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300634static struct syscall_fmt {
635 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -0300636 const char *alias;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300637 struct syscall_arg_fmt arg[6];
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -0300638 u8 nr_args;
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300639 bool errpid;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300640 bool timeout;
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -0300641 bool hexret;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300642} syscall_fmts[] = {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300643 { .name = "access",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300644 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
Arnaldo Carvalho de Melo02ef2882018-08-28 17:03:53 -0300645 { .name = "bind",
646 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300647 { .name = "bpf",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300648 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300649 { .name = "brk", .hexret = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300650 .arg = { [0] = { .scnprintf = SCA_HEX, /* brk */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300651 { .name = "clock_gettime",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300652 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
Arnaldo Carvalho de Melo33396a32017-07-19 16:15:17 -0300653 { .name = "clone", .errpid = true, .nr_args = 5,
654 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
655 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
656 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
657 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
658 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300659 { .name = "close",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300660 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
Arnaldo Carvalho de Melod5a7e662018-08-28 16:24:44 -0300661 { .name = "connect",
662 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300663 { .name = "epoll_ctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300664 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300665 { .name = "eventfd2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300666 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300667 { .name = "fchmodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300668 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300669 { .name = "fchownat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300670 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300671 { .name = "fcntl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300672 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
Arnaldo Carvalho de Melo39cc3552017-07-17 16:02:52 -0300673 .parm = &strarrays__fcntl_cmds_arrays,
674 .show_zero = true, },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300675 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300676 { .name = "flock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300677 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300678 { .name = "fstat", .alias = "newfstat", },
679 { .name = "fstatat", .alias = "newfstatat", },
680 { .name = "futex",
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300681 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
682 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300683 { .name = "futimesat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300684 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300685 { .name = "getitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300686 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300687 { .name = "getpid", .errpid = true, },
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300688 { .name = "getpgid", .errpid = true, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300689 { .name = "getppid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300690 { .name = "getrandom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300691 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300692 { .name = "getrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300693 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo2d1073d2018-01-09 12:03:47 -0300694 { .name = "gettid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300695 { .name = "ioctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300696 .arg = {
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300697#if defined(__i386__) || defined(__x86_64__)
698/*
699 * FIXME: Make this available to all arches.
700 */
Arnaldo Carvalho de Melo1cc47f22017-07-31 13:20:14 -0300701 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300702 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300703#else
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300704 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300705#endif
Arnaldo Carvalho de Melo1de30382017-10-31 11:32:23 -0300706 { .name = "kcmp", .nr_args = 5,
707 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
708 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
709 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
710 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
711 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300712 { .name = "keyctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300713 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300714 { .name = "kill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300715 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300716 { .name = "linkat",
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 = "lseek",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300719 .arg = { [2] = STRARRAY(whence, whences), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300720 { .name = "lstat", .alias = "newlstat", },
721 { .name = "madvise",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300722 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
723 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300724 { .name = "mkdirat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300725 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300726 { .name = "mknodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300727 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300728 { .name = "mlock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300729 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300730 { .name = "mlockall",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300731 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300732 { .name = "mmap", .hexret = true,
Jiri Olsa54265662017-05-31 13:35:57 +0200733/* The standard mmap maps to old_mmap on s390x */
734#if defined(__s390x__)
735 .alias = "old_mmap",
736#endif
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300737 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
738 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
739 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300740 { .name = "mount",
Arnaldo Carvalho de Melo23c07a22018-10-26 13:51:45 -0300741 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ },
742 [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
Arnaldo Carvalho de Melo73d141a2018-10-25 15:18:06 -0300743 .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300744 { .name = "mprotect",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300745 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
746 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300747 { .name = "mq_unlink",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300748 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300749 { .name = "mremap", .hexret = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300750 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
751 [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ },
752 [4] = { .scnprintf = SCA_HEX, /* new_addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300753 { .name = "munlock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300754 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300755 { .name = "munmap",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300756 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300757 { .name = "name_to_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300758 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300759 { .name = "newfstatat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300760 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300761 { .name = "open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300762 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300763 { .name = "open_by_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300764 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
765 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300766 { .name = "openat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300767 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
768 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300769 { .name = "perf_event_open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300770 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
771 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
772 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300773 { .name = "pipe2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300774 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo83bc9c372017-08-28 11:47:11 -0300775 { .name = "pkey_alloc",
776 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
777 { .name = "pkey_free",
778 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
779 { .name = "pkey_mprotect",
780 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
781 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
782 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300783 { .name = "poll", .timeout = true, },
784 { .name = "ppoll", .timeout = true, },
Arnaldo Carvalho de Melod688d032017-10-26 15:19:35 -0300785 { .name = "prctl", .alias = "arch_prctl",
786 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
787 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
788 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300789 { .name = "pread", .alias = "pread64", },
790 { .name = "preadv", .alias = "pread", },
791 { .name = "prlimit64",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300792 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300793 { .name = "pwrite", .alias = "pwrite64", },
794 { .name = "readlinkat",
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 = "recvfrom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300797 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300798 { .name = "recvmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300799 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300800 { .name = "recvmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300801 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300802 { .name = "renameat",
Arnaldo Carvalho de Melo436651c2018-12-11 11:19:23 -0300803 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
804 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
Arnaldo Carvalho de Melo84a83542018-12-11 09:53:21 -0300805 { .name = "renameat2",
806 .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
Arnaldo Carvalho de Meloca7ff2c2018-12-11 10:39:30 -0300807 [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
808 [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300809 { .name = "rt_sigaction",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300810 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300811 { .name = "rt_sigprocmask",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300812 .arg = { [0] = STRARRAY(how, sighow), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300813 { .name = "rt_sigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300814 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300815 { .name = "rt_tgsigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300816 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300817 { .name = "sched_setscheduler",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300818 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300819 { .name = "seccomp",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300820 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
821 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300822 { .name = "select", .timeout = true, },
823 { .name = "sendmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300824 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300825 { .name = "sendmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300826 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300827 { .name = "sendto",
Arnaldo Carvalho de Melo6ebb6862018-08-29 08:47:08 -0300828 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
829 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300830 { .name = "set_tid_address", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300831 { .name = "setitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300832 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300833 { .name = "setrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300834 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300835 { .name = "socket",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300836 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300837 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
838 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300839 { .name = "socketpair",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300840 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300841 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
842 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300843 { .name = "stat", .alias = "newstat", },
844 { .name = "statx",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300845 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
846 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
847 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300848 { .name = "swapoff",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300849 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300850 { .name = "swapon",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300851 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300852 { .name = "symlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300853 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300854 { .name = "tgkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300855 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300856 { .name = "tkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300857 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo476c92ca2018-10-26 13:23:25 -0300858 { .name = "umount2", .alias = "umount",
859 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300860 { .name = "uname", .alias = "newuname", },
861 { .name = "unlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300862 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300863 { .name = "utimensat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300864 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300865 { .name = "wait4", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300866 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300867 { .name = "waitid", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300868 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300869};
870
871static int syscall_fmt__cmp(const void *name, const void *fmtp)
872{
873 const struct syscall_fmt *fmt = fmtp;
874 return strcmp(name, fmt->name);
875}
876
877static struct syscall_fmt *syscall_fmt__find(const char *name)
878{
879 const int nmemb = ARRAY_SIZE(syscall_fmts);
880 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
881}
882
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -0300883static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
884{
885 int i, nmemb = ARRAY_SIZE(syscall_fmts);
886
887 for (i = 0; i < nmemb; ++i) {
888 if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0)
889 return &syscall_fmts[i];
890 }
891
892 return NULL;
893}
894
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300895/*
896 * is_exit: is this "exit" or "exit_group"?
897 * 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 -0300898 * 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 -0300899 */
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300900struct syscall {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500901 struct tep_event *tp_format;
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -0300902 int nr_args;
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -0300903 int args_size;
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300904 bool is_exit;
905 bool is_open;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400906 struct tep_format_field *args;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300907 const char *name;
908 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300909 struct syscall_arg_fmt *arg_fmt;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300910};
911
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -0300912struct bpf_map_syscall_entry {
913 bool enabled;
914};
915
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300916/*
917 * We need to have this 'calculated' boolean because in some cases we really
918 * don't know what is the duration of a syscall, for instance, when we start
919 * a session and some threads are waiting for a syscall to finish, say 'poll',
920 * in which case all we can do is to print "( ? ) for duration and for the
921 * start timestamp.
922 */
923static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200924{
925 double duration = (double)t / NSEC_PER_MSEC;
926 size_t printed = fprintf(fp, "(");
927
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300928 if (!calculated)
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -0300929 printed += fprintf(fp, " ");
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300930 else if (duration >= 1.0)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200931 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
932 else if (duration >= 0.01)
933 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
934 else
935 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300936 return printed + fprintf(fp, "): ");
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200937}
938
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300939/**
940 * filename.ptr: The filename char pointer that will be vfs_getname'd
941 * filename.entry_str_pos: Where to insert the string translated from
942 * filename.ptr by the vfs_getname tracepoint/kprobe.
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300943 * ret_scnprintf: syscall args may set this to a different syscall return
944 * formatter, for instance, fcntl may return fds, file flags, etc.
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300945 */
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300946struct thread_trace {
947 u64 entry_time;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300948 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300949 unsigned long nr_events;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +0400950 unsigned long pfmaj, pfmin;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300951 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300952 double runtime_ms;
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -0300953 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300954 struct {
955 unsigned long ptr;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -0300956 short int entry_str_pos;
957 bool pending_open;
958 unsigned int namelen;
959 char *name;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300960 } filename;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300961 struct {
962 int max;
963 char **table;
964 } paths;
David Ahernbf2575c2013-10-08 21:26:53 -0600965
966 struct intlist *syscall_stats;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300967};
968
969static struct thread_trace *thread_trace__new(void)
970{
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300971 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
972
973 if (ttrace)
974 ttrace->paths.max = -1;
975
David Ahernbf2575c2013-10-08 21:26:53 -0600976 ttrace->syscall_stats = intlist__new(NULL);
977
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300978 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300979}
980
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300981static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300982{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300983 struct thread_trace *ttrace;
984
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300985 if (thread == NULL)
986 goto fail;
987
Namhyung Kim89dceb22014-10-06 09:46:03 +0900988 if (thread__priv(thread) == NULL)
989 thread__set_priv(thread, thread_trace__new());
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300990
Namhyung Kim89dceb22014-10-06 09:46:03 +0900991 if (thread__priv(thread) == NULL)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300992 goto fail;
993
Namhyung Kim89dceb22014-10-06 09:46:03 +0900994 ttrace = thread__priv(thread);
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300995 ++ttrace->nr_events;
996
997 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300998fail:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300999 color_fprintf(fp, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001000 "WARNING: not enough memory, dropping samples!\n");
1001 return NULL;
1002}
1003
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001004
1005void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001006 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001007{
1008 struct thread_trace *ttrace = thread__priv(arg->thread);
1009
1010 ttrace->ret_scnprintf = ret_scnprintf;
1011}
1012
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04001013#define TRACE_PFMAJ (1 << 0)
1014#define TRACE_PFMIN (1 << 1)
1015
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001016static const size_t trace__entry_str_size = 2048;
1017
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001018static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001019{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001020 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001021
1022 if (fd > ttrace->paths.max) {
1023 char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
1024
1025 if (npath == NULL)
1026 return -1;
1027
1028 if (ttrace->paths.max != -1) {
1029 memset(npath + ttrace->paths.max + 1, 0,
1030 (fd - ttrace->paths.max) * sizeof(char *));
1031 } else {
1032 memset(npath, 0, (fd + 1) * sizeof(char *));
1033 }
1034
1035 ttrace->paths.table = npath;
1036 ttrace->paths.max = fd;
1037 }
1038
1039 ttrace->paths.table[fd] = strdup(pathname);
1040
1041 return ttrace->paths.table[fd] != NULL ? 0 : -1;
1042}
1043
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001044static int thread__read_fd_path(struct thread *thread, int fd)
1045{
1046 char linkname[PATH_MAX], pathname[PATH_MAX];
1047 struct stat st;
1048 int ret;
1049
1050 if (thread->pid_ == thread->tid) {
1051 scnprintf(linkname, sizeof(linkname),
1052 "/proc/%d/fd/%d", thread->pid_, fd);
1053 } else {
1054 scnprintf(linkname, sizeof(linkname),
1055 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1056 }
1057
1058 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1059 return -1;
1060
1061 ret = readlink(linkname, pathname, sizeof(pathname));
1062
1063 if (ret < 0 || ret > st.st_size)
1064 return -1;
1065
1066 pathname[ret] = '\0';
1067 return trace__set_fd_pathname(thread, fd, pathname);
1068}
1069
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001070static const char *thread__fd_path(struct thread *thread, int fd,
1071 struct trace *trace)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001072{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001073 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001074
1075 if (ttrace == NULL)
1076 return NULL;
1077
1078 if (fd < 0)
1079 return NULL;
1080
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001081 if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001082 if (!trace->live)
1083 return NULL;
1084 ++trace->stats.proc_getname;
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001085 if (thread__read_fd_path(thread, fd))
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001086 return NULL;
1087 }
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001088
1089 return ttrace->paths.table[fd];
1090}
1091
Arnaldo Carvalho de Melofc65eb82017-07-14 15:21:40 -03001092size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001093{
1094 int fd = arg->val;
1095 size_t printed = scnprintf(bf, size, "%d", fd);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001096 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001097
1098 if (path)
1099 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1100
1101 return printed;
1102}
1103
Arnaldo Carvalho de Melo0a2f75402017-10-31 11:30:09 -03001104size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1105{
1106 size_t printed = scnprintf(bf, size, "%d", fd);
1107 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1108
1109 if (thread) {
1110 const char *path = thread__fd_path(thread, fd, trace);
1111
1112 if (path)
1113 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1114
1115 thread__put(thread);
1116 }
1117
1118 return printed;
1119}
1120
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001121static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1122 struct syscall_arg *arg)
1123{
1124 int fd = arg->val;
1125 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
Namhyung Kim89dceb22014-10-06 09:46:03 +09001126 struct thread_trace *ttrace = thread__priv(arg->thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001127
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001128 if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
1129 zfree(&ttrace->paths.table[fd]);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001130
1131 return printed;
1132}
1133
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001134static void thread__set_filename_pos(struct thread *thread, const char *bf,
1135 unsigned long ptr)
1136{
1137 struct thread_trace *ttrace = thread__priv(thread);
1138
1139 ttrace->filename.ptr = ptr;
1140 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1141}
1142
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001143static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1144{
1145 struct augmented_arg *augmented_arg = arg->augmented.args;
1146
1147 return scnprintf(bf, size, "%.*s", augmented_arg->size, augmented_arg->value);
1148}
1149
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001150static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1151 struct syscall_arg *arg)
1152{
1153 unsigned long ptr = arg->val;
1154
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001155 if (arg->augmented.args)
1156 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1157
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001158 if (!arg->trace->vfs_getname)
1159 return scnprintf(bf, size, "%#x", ptr);
1160
1161 thread__set_filename_pos(arg->thread, bf, ptr);
1162 return 0;
1163}
1164
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001165static bool trace__filter_duration(struct trace *trace, double t)
1166{
1167 return t < (trace->duration_filter * NSEC_PER_MSEC);
1168}
1169
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001170static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001171{
1172 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1173
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001174 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001175}
1176
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001177/*
1178 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1179 * using ttrace->entry_time for a thread that receives a sys_exit without
1180 * first having received a sys_enter ("poll" issued before tracing session
1181 * starts, lost sys_enter exit due to ring buffer overflow).
1182 */
1183static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1184{
1185 if (tstamp > 0)
1186 return __trace__fprintf_tstamp(trace, tstamp, fp);
1187
1188 return fprintf(fp, " ? ");
1189}
1190
Namhyung Kimf15eb532012-10-05 14:02:16 +09001191static bool done = false;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001192static bool interrupted = false;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001193
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001194static void sig_handler(int sig)
Namhyung Kimf15eb532012-10-05 14:02:16 +09001195{
1196 done = true;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001197 interrupted = sig == SIGINT;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001198}
1199
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001200static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001201{
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001202 size_t printed = 0;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001203
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001204 if (trace->multiple_threads) {
1205 if (trace->show_comm)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02001206 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
Adrian Hunter38051232013-07-04 16:20:31 +03001207 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001208 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001209
1210 return printed;
1211}
1212
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001213static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1214 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1215{
1216 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
1217 printed += fprintf_duration(duration, duration_calculated, fp);
1218 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1219}
1220
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001221static int trace__process_event(struct trace *trace, struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001222 union perf_event *event, struct perf_sample *sample)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001223{
1224 int ret = 0;
1225
1226 switch (event->header.type) {
1227 case PERF_RECORD_LOST:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001228 color_fprintf(trace->output, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001229 "LOST %" PRIu64 " events!\n", event->lost.lost);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001230 ret = machine__process_lost_event(machine, event, sample);
Arnaldo Carvalho de Melo3ed5ca22016-03-30 16:51:17 -03001231 break;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001232 default:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001233 ret = machine__process_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001234 break;
1235 }
1236
1237 return ret;
1238}
1239
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001240static int trace__tool_process(struct perf_tool *tool,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001241 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001242 struct perf_sample *sample,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001243 struct machine *machine)
1244{
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001245 struct trace *trace = container_of(tool, struct trace, tool);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001246 return trace__process_event(trace, machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001247}
1248
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -03001249static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1250{
1251 struct machine *machine = vmachine;
1252
1253 if (machine->kptr_restrict_warned)
1254 return NULL;
1255
1256 if (symbol_conf.kptr_restrict) {
1257 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1258 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1259 "Kernel samples will not be resolved.\n");
1260 machine->kptr_restrict_warned = true;
1261 return NULL;
1262 }
1263
1264 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1265}
1266
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001267static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1268{
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001269 int err = symbol__init(NULL);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001270
1271 if (err)
1272 return err;
1273
David Ahern8fb598e2013-09-28 13:13:00 -06001274 trace->host = machine__new_host();
1275 if (trace->host == NULL)
1276 return -ENOMEM;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001277
Andrei Vagincbd5c172017-11-07 16:22:46 -08001278 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1279 if (err < 0)
1280 goto out;
Arnaldo Carvalho de Melo706c3da2015-07-22 16:16:16 -03001281
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001282 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001283 evlist->threads, trace__tool_process, false,
Mark Drayton3fcb10e2018-12-04 12:34:20 -08001284 1);
Andrei Vagincbd5c172017-11-07 16:22:46 -08001285out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001286 if (err)
1287 symbol__exit();
1288
1289 return err;
1290}
1291
Andrei Vagin33974a42017-11-07 16:22:45 -08001292static void trace__symbols__exit(struct trace *trace)
1293{
1294 machine__exit(trace->host);
1295 trace->host = NULL;
1296
1297 symbol__exit();
1298}
1299
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001300static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1301{
1302 int idx;
1303
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001304 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1305 nr_args = sc->fmt->nr_args;
1306
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001307 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1308 if (sc->arg_fmt == NULL)
1309 return -1;
1310
1311 for (idx = 0; idx < nr_args; ++idx) {
1312 if (sc->fmt)
1313 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1314 }
1315
1316 sc->nr_args = nr_args;
1317 return 0;
1318}
1319
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001320static int syscall__set_arg_fmts(struct syscall *sc)
1321{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001322 struct tep_format_field *field, *last_field = NULL;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001323 int idx = 0, len;
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001324
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001325 for (field = sc->args; field; field = field->next, ++idx) {
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001326 last_field = field;
1327
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001328 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1329 continue;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001330
1331 if (strcmp(field->type, "const char *") == 0 &&
Arnaldo Carvalho de Melo12f3ca42016-05-23 16:37:55 -03001332 (strcmp(field->name, "filename") == 0 ||
1333 strcmp(field->name, "path") == 0 ||
1334 strcmp(field->name, "pathname") == 0))
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001335 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001336 else if (field->flags & TEP_FIELD_IS_POINTER)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001337 sc->arg_fmt[idx].scnprintf = syscall_arg__scnprintf_hex;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -03001338 else if (strcmp(field->type, "pid_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001339 sc->arg_fmt[idx].scnprintf = SCA_PID;
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -03001340 else if (strcmp(field->type, "umode_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001341 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001342 else if ((strcmp(field->type, "int") == 0 ||
1343 strcmp(field->type, "unsigned int") == 0 ||
1344 strcmp(field->type, "long") == 0) &&
1345 (len = strlen(field->name)) >= 2 &&
1346 strcmp(field->name + len - 2, "fd") == 0) {
1347 /*
1348 * /sys/kernel/tracing/events/syscalls/sys_enter*
1349 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1350 * 65 int
1351 * 23 unsigned int
1352 * 7 unsigned long
1353 */
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001354 sc->arg_fmt[idx].scnprintf = SCA_FD;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001355 }
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001356 }
1357
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001358 if (last_field)
1359 sc->args_size = last_field->offset + last_field->size;
1360
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001361 return 0;
1362}
1363
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001364static int trace__read_syscall_info(struct trace *trace, int id)
1365{
1366 char tp_name[128];
1367 struct syscall *sc;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03001368 const char *name = syscalltbl__name(trace->sctbl, id);
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001369
1370 if (name == NULL)
1371 return -1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001372
1373 if (id > trace->syscalls.max) {
1374 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1375
1376 if (nsyscalls == NULL)
1377 return -1;
1378
1379 if (trace->syscalls.max != -1) {
1380 memset(nsyscalls + trace->syscalls.max + 1, 0,
1381 (id - trace->syscalls.max) * sizeof(*sc));
1382 } else {
1383 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1384 }
1385
1386 trace->syscalls.table = nsyscalls;
1387 trace->syscalls.max = id;
1388 }
1389
1390 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001391 sc->name = name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001392
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001393 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001394
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001395 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
Jiri Olsa97978b32013-12-03 14:09:24 +01001396 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001397
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001398 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001399 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
Jiri Olsa97978b32013-12-03 14:09:24 +01001400 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001401 }
1402
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001403 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1404 return -1;
1405
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001406 if (IS_ERR(sc->tp_format))
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001407 return -1;
1408
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001409 sc->args = sc->tp_format->format.fields;
Taeung Songc42de702016-02-26 22:14:25 +09001410 /*
1411 * We need to check and discard the first variable '__syscall_nr'
1412 * or 'nr' that mean the syscall number. It is needless here.
1413 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1414 */
1415 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001416 sc->args = sc->args->next;
1417 --sc->nr_args;
1418 }
1419
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001420 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001421 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001422
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001423 return syscall__set_arg_fmts(sc);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001424}
1425
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001426static int trace__validate_ev_qualifier(struct trace *trace)
1427{
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001428 int err = 0, i;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001429 size_t nr_allocated;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001430 struct str_node *pos;
1431
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001432 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1433 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1434 sizeof(trace->ev_qualifier_ids.entries[0]));
1435
1436 if (trace->ev_qualifier_ids.entries == NULL) {
1437 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1438 trace->output);
1439 err = -EINVAL;
1440 goto out;
1441 }
1442
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001443 nr_allocated = trace->ev_qualifier_ids.nr;
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001444 i = 0;
1445
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001446 strlist__for_each_entry(pos, trace->ev_qualifier) {
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001447 const char *sc = pos->s;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001448 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001449
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001450 if (id < 0) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001451 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1452 if (id >= 0)
1453 goto matches;
1454
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001455 if (err == 0) {
1456 fputs("Error:\tInvalid syscall ", trace->output);
1457 err = -EINVAL;
1458 } else {
1459 fputs(", ", trace->output);
1460 }
1461
1462 fputs(sc, trace->output);
1463 }
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001464matches:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001465 trace->ev_qualifier_ids.entries[i++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001466 if (match_next == -1)
1467 continue;
1468
1469 while (1) {
1470 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1471 if (id < 0)
1472 break;
1473 if (nr_allocated == trace->ev_qualifier_ids.nr) {
1474 void *entries;
1475
1476 nr_allocated += 8;
1477 entries = realloc(trace->ev_qualifier_ids.entries,
1478 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1479 if (entries == NULL) {
1480 err = -ENOMEM;
1481 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1482 goto out_free;
1483 }
1484 trace->ev_qualifier_ids.entries = entries;
1485 }
1486 trace->ev_qualifier_ids.nr++;
1487 trace->ev_qualifier_ids.entries[i++] = id;
1488 }
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001489 }
1490
1491 if (err < 0) {
1492 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1493 "\nHint:\tand: 'man syscalls'\n", trace->output);
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001494out_free:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001495 zfree(&trace->ev_qualifier_ids.entries);
1496 trace->ev_qualifier_ids.nr = 0;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001497 }
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001498out:
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001499 return err;
1500}
1501
David Ahern55d43bc2015-02-19 15:00:22 -05001502/*
1503 * args is to be interpreted as a series of longs but we need to handle
1504 * 8-byte unaligned accesses. args points to raw_data within the event
1505 * and raw_data is guaranteed to be 8-byte unaligned because it is
1506 * preceded by raw_size which is a u32. So we need to copy args to a temp
1507 * variable to read it. Most notably this avoids extended load instructions
1508 * on unaligned addresses
1509 */
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001510unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001511{
1512 unsigned long val;
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001513 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001514
1515 memcpy(&val, p, sizeof(val));
1516 return val;
1517}
1518
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001519static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1520 struct syscall_arg *arg)
1521{
1522 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1523 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1524
1525 return scnprintf(bf, size, "arg%d: ", arg->idx);
1526}
1527
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001528/*
1529 * Check if the value is in fact zero, i.e. mask whatever needs masking, such
1530 * as mount 'flags' argument that needs ignoring some magic flag, see comment
1531 * in tools/perf/trace/beauty/mount_flags.c
1532 */
1533static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val)
1534{
1535 if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val)
1536 return sc->arg_fmt[arg->idx].mask_val(arg, val);
1537
1538 return val;
1539}
1540
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001541static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1542 struct syscall_arg *arg, unsigned long val)
1543{
1544 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1545 arg->val = val;
1546 if (sc->arg_fmt[arg->idx].parm)
1547 arg->parm = sc->arg_fmt[arg->idx].parm;
1548 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1549 }
1550 return scnprintf(bf, size, "%ld", val);
1551}
1552
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001553static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001554 unsigned char *args, void *augmented_args, int augmented_args_size,
1555 struct trace *trace, struct thread *thread)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001556{
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001557 size_t printed = 0;
David Ahern55d43bc2015-02-19 15:00:22 -05001558 unsigned long val;
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001559 u8 bit = 1;
1560 struct syscall_arg arg = {
1561 .args = args,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001562 .augmented = {
1563 .size = augmented_args_size,
1564 .args = augmented_args,
1565 },
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001566 .idx = 0,
1567 .mask = 0,
1568 .trace = trace,
1569 .thread = thread,
1570 };
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001571 struct thread_trace *ttrace = thread__priv(thread);
1572
1573 /*
1574 * Things like fcntl will set this in its 'cmd' formatter to pick the
1575 * right formatter for the return value (an fd? file flags?), which is
1576 * not needed for syscalls that always return a given type, say an fd.
1577 */
1578 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001579
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001580 if (sc->args != NULL) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001581 struct tep_format_field *field;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001582
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001583 for (field = sc->args; field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001584 field = field->next, ++arg.idx, bit <<= 1) {
1585 if (arg.mask & bit)
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001586 continue;
David Ahern55d43bc2015-02-19 15:00:22 -05001587
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001588 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Melo496fd342018-10-25 16:09:47 -03001589 /*
1590 * Some syscall args need some mask, most don't and
1591 * return val untouched.
1592 */
1593 val = syscall__mask_val(sc, &arg, val);
David Ahern55d43bc2015-02-19 15:00:22 -05001594
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001595 /*
1596 * Suppress this argument if its value is zero and
1597 * and we don't have a string associated in an
1598 * strarray for it.
1599 */
David Ahern55d43bc2015-02-19 15:00:22 -05001600 if (val == 0 &&
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001601 !(sc->arg_fmt &&
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -03001602 (sc->arg_fmt[arg.idx].show_zero ||
1603 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001604 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1605 sc->arg_fmt[arg.idx].parm))
Arnaldo Carvalho de Melo22ae5cf12013-09-12 11:27:34 -03001606 continue;
1607
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001608 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001609 "%s%s: ", printed ? ", " : "", field->name);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001610 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001611 }
Arnaldo Carvalho de Melo4c4d6e52016-05-05 23:38:05 -03001612 } else if (IS_ERR(sc->tp_format)) {
1613 /*
1614 * If we managed to read the tracepoint /format file, then we
1615 * may end up not having any args, like with gettid(), so only
1616 * print the raw args when we didn't manage to read it.
1617 */
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001618 while (arg.idx < sc->nr_args) {
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001619 if (arg.mask & bit)
1620 goto next_arg;
1621 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001622 if (printed)
1623 printed += scnprintf(bf + printed, size - printed, ", ");
1624 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001625 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1626next_arg:
1627 ++arg.idx;
1628 bit <<= 1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001629 }
1630 }
1631
1632 return printed;
1633}
1634
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001635typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001636 union perf_event *event,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001637 struct perf_sample *sample);
1638
1639static struct syscall *trace__syscall_info(struct trace *trace,
David Ahernbf2575c2013-10-08 21:26:53 -06001640 struct perf_evsel *evsel, int id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001641{
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001642
1643 if (id < 0) {
Arnaldo Carvalho de Meloadaa18b2013-08-22 17:55:25 -03001644
1645 /*
1646 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1647 * before that, leaving at a higher verbosity level till that is
1648 * explained. Reproduced with plain ftrace with:
1649 *
1650 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1651 * grep "NR -1 " /t/trace_pipe
1652 *
1653 * After generating some load on the machine.
1654 */
1655 if (verbose > 1) {
1656 static u64 n;
1657 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1658 id, perf_evsel__name(evsel), ++n);
1659 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001660 return NULL;
1661 }
1662
1663 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1664 trace__read_syscall_info(trace, id))
1665 goto out_cant_read;
1666
1667 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1668 goto out_cant_read;
1669
1670 return &trace->syscalls.table[id];
1671
1672out_cant_read:
Namhyung Kimbb963e12017-02-17 17:17:38 +09001673 if (verbose > 0) {
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03001674 fprintf(trace->output, "Problems reading syscall %d", id);
1675 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1676 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1677 fputs(" information\n", trace->output);
1678 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001679 return NULL;
1680}
1681
David Ahernbf2575c2013-10-08 21:26:53 -06001682static void thread__update_stats(struct thread_trace *ttrace,
1683 int id, struct perf_sample *sample)
1684{
1685 struct int_node *inode;
1686 struct stats *stats;
1687 u64 duration = 0;
1688
1689 inode = intlist__findnew(ttrace->syscall_stats, id);
1690 if (inode == NULL)
1691 return;
1692
1693 stats = inode->priv;
1694 if (stats == NULL) {
1695 stats = malloc(sizeof(struct stats));
1696 if (stats == NULL)
1697 return;
1698 init_stats(stats);
1699 inode->priv = stats;
1700 }
1701
1702 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1703 duration = sample->time - ttrace->entry_time;
1704
1705 update_stats(stats, duration);
1706}
1707
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001708static int trace__printf_interrupted_entry(struct trace *trace)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001709{
1710 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001711 size_t printed;
1712
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001713 if (trace->failure_only || trace->current == NULL)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001714 return 0;
1715
1716 ttrace = thread__priv(trace->current);
1717
1718 if (!ttrace->entry_pending)
1719 return 0;
1720
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001721 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001722 printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
1723 ttrace->entry_pending = false;
1724
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03001725 ++trace->nr_events_printed;
1726
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001727 return printed;
1728}
1729
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001730static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1731 struct perf_sample *sample, struct thread *thread)
1732{
1733 int printed = 0;
1734
1735 if (trace->print_sample) {
1736 double ts = (double)sample->time / NSEC_PER_MSEC;
1737
1738 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1739 perf_evsel__name(evsel), ts,
1740 thread__comm_str(thread),
1741 sample->pid, sample->tid, sample->cpu);
1742 }
1743
1744 return printed;
1745}
1746
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001747static 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 -03001748{
1749 void *augmented_args = NULL;
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001750 /*
1751 * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
1752 * and there we get all 6 syscall args plus the tracepoint common
1753 * fields (sizeof(long)) and the syscall_nr (another long). So we check
1754 * if that is the case and if so don't look after the sc->args_size,
1755 * but always after the full raw_syscalls:sys_enter payload, which is
1756 * fixed.
1757 *
1758 * We'll revisit this later to pass s->args_size to the BPF augmenter
1759 * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
1760 * copies only what we need for each syscall, like what happens when we
1761 * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
1762 * traffic to just what is needed for each syscall.
1763 */
1764 int args_size = raw_augmented ? (8 * (int)sizeof(long)) : sc->args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001765
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001766 *augmented_args_size = sample->raw_size - args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001767 if (*augmented_args_size > 0)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001768 augmented_args = sample->raw_data + args_size;
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001769
1770 return augmented_args;
1771}
1772
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001773static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001774 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001775 struct perf_sample *sample)
1776{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001777 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001778 void *args;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001779 size_t printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001780 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001781 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001782 int augmented_args_size = 0;
1783 void *augmented_args = NULL;
David Ahernbf2575c2013-10-08 21:26:53 -06001784 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001785 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001786
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001787 if (sc == NULL)
1788 return -1;
1789
David Ahern8fb598e2013-09-28 13:13:00 -06001790 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001791 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001792 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001793 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001794
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001795 trace__fprintf_sample(trace, evsel, sample, thread);
1796
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001797 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001798
1799 if (ttrace->entry_str == NULL) {
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001800 ttrace->entry_str = malloc(trace__entry_str_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001801 if (!ttrace->entry_str)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001802 goto out_put;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001803 }
1804
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001805 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001806 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001807 /*
1808 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1809 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1810 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1811 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1812 * so when handling, say the openat syscall, we end up getting 6 args for the
1813 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1814 * thinking that the extra 2 u64 args are the augmented filename, so just check
1815 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1816 */
1817 if (evsel != trace->syscalls.events.sys_enter)
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001818 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001819 ttrace->entry_time = sample->time;
1820 msg = ttrace->entry_str;
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001821 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001822
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001823 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001824 args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001825
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001826 if (sc->is_exit) {
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001827 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001828 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Meloc008f782016-05-17 12:13:54 -03001829 fprintf(trace->output, "%-70s)\n", ttrace->entry_str);
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001830 }
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001831 } else {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001832 ttrace->entry_pending = true;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001833 /* See trace__vfs_getname & trace__sys_exit */
1834 ttrace->filename.pending_open = false;
1835 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001836
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001837 if (trace->current != thread) {
1838 thread__put(trace->current);
1839 trace->current = thread__get(thread);
1840 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001841 err = 0;
1842out_put:
1843 thread__put(thread);
1844 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001845}
1846
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001847static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1848 struct perf_sample *sample)
1849{
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001850 struct thread_trace *ttrace;
1851 struct thread *thread;
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001852 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1853 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001854 char msg[1024];
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001855 void *args, *augmented_args = NULL;
1856 int augmented_args_size;
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001857
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001858 if (sc == NULL)
1859 return -1;
1860
1861 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1862 ttrace = thread__trace(thread, trace->output);
1863 /*
1864 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1865 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1866 */
1867 if (ttrace == NULL)
1868 goto out_put;
1869
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001870 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03001871 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls);
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001872 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001873 fprintf(trace->output, "%s", msg);
1874 err = 0;
1875out_put:
1876 thread__put(thread);
1877 return err;
1878}
1879
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001880static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1881 struct perf_sample *sample,
1882 struct callchain_cursor *cursor)
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001883{
1884 struct addr_location al;
Ravi Bangoria3a9e9a42018-01-30 11:00:53 +05301885 int max_stack = evsel->attr.sample_max_stack ?
1886 evsel->attr.sample_max_stack :
1887 trace->max_stack;
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001888 int err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001889
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001890 if (machine__resolve(trace->host, &al, sample) < 0)
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001891 return -1;
1892
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001893 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1894 addr_location__put(&al);
1895 return err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001896}
1897
1898static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1899{
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001900 /* TODO: user-configurable print_opts */
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001901 const unsigned int print_opts = EVSEL__PRINT_SYM |
1902 EVSEL__PRINT_DSO |
1903 EVSEL__PRINT_UNKNOWN_AS_ADDR;
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001904
Arnaldo Carvalho de Melod327e602016-04-14 17:53:49 -03001905 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001906}
1907
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01001908static const char *errno_to_name(struct perf_evsel *evsel, int err)
1909{
1910 struct perf_env *env = perf_evsel__env(evsel);
1911 const char *arch_name = perf_env__arch(env);
1912
1913 return arch_syscalls__strerrno(arch_name, err);
1914}
1915
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001916static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001917 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001918 struct perf_sample *sample)
1919{
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09001920 long ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001921 u64 duration = 0;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001922 bool duration_calculated = false;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001923 struct thread *thread;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001924 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0;
David Ahernbf2575c2013-10-08 21:26:53 -06001925 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001926 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001927
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001928 if (sc == NULL)
1929 return -1;
1930
David Ahern8fb598e2013-09-28 13:13:00 -06001931 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001932 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001933 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001934 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001935
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001936 trace__fprintf_sample(trace, evsel, sample, thread);
1937
David Ahernbf2575c2013-10-08 21:26:53 -06001938 if (trace->summary)
1939 thread__update_stats(ttrace, id, sample);
1940
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001941 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001942
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001943 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001944 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
1945 ttrace->filename.pending_open = false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001946 ++trace->stats.vfs_getname;
1947 }
1948
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001949 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001950 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001951 if (trace__filter_duration(trace, duration))
1952 goto out;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001953 duration_calculated = true;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001954 } else if (trace->duration_filter)
1955 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001956
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001957 if (sample->callchain) {
1958 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
1959 if (callchain_ret == 0) {
1960 if (callchain_cursor.nr < trace->min_stack)
1961 goto out;
1962 callchain_ret = 1;
1963 }
1964 }
1965
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001966 if (trace->summary_only || (ret >= 0 && trace->failure_only))
David Ahernfd2eaba2013-11-12 09:31:15 -07001967 goto out;
1968
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001969 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001970
1971 if (ttrace->entry_pending) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001972 fprintf(trace->output, "%-70s", ttrace->entry_str);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001973 } else {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001974 fprintf(trace->output, " ... [");
1975 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
1976 fprintf(trace->output, "]: %s()", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001977 }
1978
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001979 if (sc->fmt == NULL) {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001980 if (ret < 0)
1981 goto errno_print;
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001982signed_print:
Arnaldo Carvalho de Melo6f8fe612017-07-19 11:39:47 -03001983 fprintf(trace->output, ") = %ld", ret);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001984 } else if (ret < 0) {
1985errno_print: {
Masami Hiramatsu942a91e2014-08-14 02:22:41 +00001986 char bf[STRERR_BUFSIZE];
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001987 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01001988 *e = errno_to_name(evsel, -ret);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001989
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001990 fprintf(trace->output, ") = -1 %s %s", e, emsg);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001991 }
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001992 } else if (ret == 0 && sc->fmt->timeout)
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001993 fprintf(trace->output, ") = 0 Timeout");
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001994 else if (ttrace->ret_scnprintf) {
1995 char bf[1024];
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001996 struct syscall_arg arg = {
1997 .val = ret,
1998 .thread = thread,
1999 .trace = trace,
2000 };
2001 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03002002 ttrace->ret_scnprintf = NULL;
2003 fprintf(trace->output, ") = %s", bf);
2004 } else if (sc->fmt->hexret)
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09002005 fprintf(trace->output, ") = %#lx", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03002006 else if (sc->fmt->errpid) {
2007 struct thread *child = machine__find_thread(trace->host, ret, ret);
2008
2009 if (child != NULL) {
2010 fprintf(trace->output, ") = %ld", ret);
2011 if (child->comm_set)
2012 fprintf(trace->output, " (%s)", thread__comm_str(child));
2013 thread__put(child);
2014 }
2015 } else
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03002016 goto signed_print;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002017
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002018 fputc('\n', trace->output);
Milian Wolff566a0882016-04-08 13:34:15 +02002019
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002020 /*
2021 * We only consider an 'event' for the sake of --max-events a non-filtered
2022 * sys_enter + sys_exit and other tracepoint events.
2023 */
2024 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
2025 interrupted = true;
2026
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03002027 if (callchain_ret > 0)
2028 trace__fprintf_callchain(trace, sample);
2029 else if (callchain_ret < 0)
2030 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03002031out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002032 ttrace->entry_pending = false;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002033 err = 0;
2034out_put:
2035 thread__put(thread);
2036 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03002037}
2038
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002039static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002040 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002041 struct perf_sample *sample)
2042{
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002043 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2044 struct thread_trace *ttrace;
2045 size_t filename_len, entry_str_len, to_move;
2046 ssize_t remaining_space;
2047 char *pos;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002048 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002049
2050 if (!thread)
2051 goto out;
2052
2053 ttrace = thread__priv(thread);
2054 if (!ttrace)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002055 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002056
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002057 filename_len = strlen(filename);
Arnaldo Carvalho de Melo39f0e7a2017-03-24 14:51:28 -03002058 if (filename_len == 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002059 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002060
2061 if (ttrace->filename.namelen < filename_len) {
2062 char *f = realloc(ttrace->filename.name, filename_len + 1);
2063
2064 if (f == NULL)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002065 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03002066
2067 ttrace->filename.namelen = filename_len;
2068 ttrace->filename.name = f;
2069 }
2070
2071 strcpy(ttrace->filename.name, filename);
2072 ttrace->filename.pending_open = true;
2073
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002074 if (!ttrace->filename.ptr)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002075 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002076
2077 entry_str_len = strlen(ttrace->entry_str);
2078 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2079 if (remaining_space <= 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002080 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002081
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002082 if (filename_len > (size_t)remaining_space) {
2083 filename += filename_len - remaining_space;
2084 filename_len = remaining_space;
2085 }
2086
2087 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2088 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2089 memmove(pos + filename_len, pos, to_move);
2090 memcpy(pos, filename, filename_len);
2091
2092 ttrace->filename.ptr = 0;
2093 ttrace->filename.entry_str_pos = 0;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002094out_put:
2095 thread__put(thread);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002096out:
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002097 return 0;
2098}
2099
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002100static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002101 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002102 struct perf_sample *sample)
2103{
2104 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2105 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
David Ahern8fb598e2013-09-28 13:13:00 -06002106 struct thread *thread = machine__findnew_thread(trace->host,
Adrian Hunter314add62013-08-27 11:23:03 +03002107 sample->pid,
2108 sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002109 struct thread_trace *ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002110
2111 if (ttrace == NULL)
2112 goto out_dump;
2113
2114 ttrace->runtime_ms += runtime_ms;
2115 trace->runtime_ms += runtime_ms;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002116out_put:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002117 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002118 return 0;
2119
2120out_dump:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002121 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002122 evsel->name,
2123 perf_evsel__strval(evsel, sample, "comm"),
2124 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2125 runtime,
2126 perf_evsel__intval(evsel, sample, "vruntime"));
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002127 goto out_put;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002128}
2129
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002130static int bpf_output__printer(enum binary_printer_ops op,
2131 unsigned int val, void *extra __maybe_unused, FILE *fp)
Wang Nan1d6c9402016-02-26 09:31:55 +00002132{
Wang Nan1d6c9402016-02-26 09:31:55 +00002133 unsigned char ch = (unsigned char)val;
2134
2135 switch (op) {
2136 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002137 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
Wang Nan1d6c9402016-02-26 09:31:55 +00002138 case BINARY_PRINT_DATA_BEGIN:
2139 case BINARY_PRINT_LINE_BEGIN:
2140 case BINARY_PRINT_ADDR:
2141 case BINARY_PRINT_NUM_DATA:
2142 case BINARY_PRINT_NUM_PAD:
2143 case BINARY_PRINT_SEP:
2144 case BINARY_PRINT_CHAR_PAD:
2145 case BINARY_PRINT_LINE_END:
2146 case BINARY_PRINT_DATA_END:
2147 default:
2148 break;
2149 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002150
2151 return 0;
Wang Nan1d6c9402016-02-26 09:31:55 +00002152}
2153
2154static void bpf_output__fprintf(struct trace *trace,
2155 struct perf_sample *sample)
2156{
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002157 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2158 bpf_output__printer, NULL, trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002159 ++trace->nr_events_printed;
Wang Nan1d6c9402016-02-26 09:31:55 +00002160}
2161
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002162static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2163 union perf_event *event __maybe_unused,
2164 struct perf_sample *sample)
2165{
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002166 struct thread *thread;
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002167 int callchain_ret = 0;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002168 /*
2169 * Check if we called perf_evsel__disable(evsel) due to, for instance,
2170 * this event's max_events having been hit and this is an entry coming
2171 * from the ring buffer that we should discard, since the max events
2172 * have already been considered/printed.
2173 */
2174 if (evsel->disabled)
2175 return 0;
2176
2177 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002178
2179 if (sample->callchain) {
2180 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2181 if (callchain_ret == 0) {
2182 if (callchain_cursor.nr < trace->min_stack)
2183 goto out;
2184 callchain_ret = 1;
2185 }
2186 }
2187
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03002188 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002189 trace__fprintf_tstamp(trace, sample->time, trace->output);
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002190
2191 if (trace->trace_syscalls)
2192 fprintf(trace->output, "( ): ");
2193
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002194 if (thread)
2195 trace__fprintf_comm_tid(trace, thread, trace->output);
2196
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002197 if (evsel == trace->syscalls.events.augmented) {
2198 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2199 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2200
2201 if (sc) {
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002202 fprintf(trace->output, "%s(", sc->name);
2203 trace__fprintf_sys_enter(trace, evsel, sample);
2204 fputc(')', trace->output);
2205 goto newline;
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002206 }
2207
2208 /*
2209 * XXX: Not having the associated syscall info or not finding/adding
2210 * the thread should never happen, but if it does...
2211 * fall thru and print it as a bpf_output event.
2212 */
2213 }
2214
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002215 fprintf(trace->output, "%s:", evsel->name);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002216
Wang Nan1d6c9402016-02-26 09:31:55 +00002217 if (perf_evsel__is_bpf_output(evsel)) {
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002218 bpf_output__fprintf(trace, sample);
Wang Nan1d6c9402016-02-26 09:31:55 +00002219 } else if (evsel->tp_format) {
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002220 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2221 trace__fprintf_sys_enter(trace, evsel, sample)) {
2222 event_format__fprintf(evsel->tp_format, sample->cpu,
2223 sample->raw_data, sample->raw_size,
2224 trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002225 ++trace->nr_events_printed;
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002226
2227 if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
2228 perf_evsel__disable(evsel);
2229 perf_evsel__close(evsel);
2230 }
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002231 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002232 }
2233
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002234newline:
Changbin Du51125a22018-03-13 18:40:01 +08002235 fprintf(trace->output, "\n");
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002236
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002237 if (callchain_ret > 0)
2238 trace__fprintf_callchain(trace, sample);
2239 else if (callchain_ret < 0)
2240 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2241out:
Arnaldo Carvalho de Meloa937c662018-10-20 09:27:52 -03002242 thread__put(thread);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002243 return 0;
2244}
2245
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002246static void print_location(FILE *f, struct perf_sample *sample,
2247 struct addr_location *al,
2248 bool print_dso, bool print_sym)
2249{
2250
Namhyung Kimbb963e12017-02-17 17:17:38 +09002251 if ((verbose > 0 || print_dso) && al->map)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002252 fprintf(f, "%s@", al->map->dso->long_name);
2253
Namhyung Kimbb963e12017-02-17 17:17:38 +09002254 if ((verbose > 0 || print_sym) && al->sym)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002255 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002256 al->addr - al->sym->start);
2257 else if (al->map)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002258 fprintf(f, "0x%" PRIx64, al->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002259 else
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002260 fprintf(f, "0x%" PRIx64, sample->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002261}
2262
2263static int trace__pgfault(struct trace *trace,
2264 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03002265 union perf_event *event __maybe_unused,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002266 struct perf_sample *sample)
2267{
2268 struct thread *thread;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002269 struct addr_location al;
2270 char map_type = 'd';
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002271 struct thread_trace *ttrace;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002272 int err = -1;
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002273 int callchain_ret = 0;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002274
2275 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002276
2277 if (sample->callchain) {
2278 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2279 if (callchain_ret == 0) {
2280 if (callchain_cursor.nr < trace->min_stack)
2281 goto out_put;
2282 callchain_ret = 1;
2283 }
2284 }
2285
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002286 ttrace = thread__trace(thread, trace->output);
2287 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002288 goto out_put;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002289
2290 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2291 ttrace->pfmaj++;
2292 else
2293 ttrace->pfmin++;
2294
2295 if (trace->summary_only)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002296 goto out;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002297
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002298 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002299
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002300 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002301
2302 fprintf(trace->output, "%sfault [",
2303 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2304 "maj" : "min");
2305
2306 print_location(trace->output, sample, &al, false, true);
2307
2308 fprintf(trace->output, "] => ");
2309
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03002310 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002311
2312 if (!al.map) {
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002313 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002314
2315 if (al.map)
2316 map_type = 'x';
2317 else
2318 map_type = '?';
2319 }
2320
2321 print_location(trace->output, sample, &al, true, false);
2322
2323 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
Arnaldo Carvalho de Melo0c3a6ef2016-04-19 16:31:12 -03002324
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002325 if (callchain_ret > 0)
2326 trace__fprintf_callchain(trace, sample);
2327 else if (callchain_ret < 0)
2328 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002329
2330 ++trace->nr_events_printed;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002331out:
2332 err = 0;
2333out_put:
2334 thread__put(thread);
2335 return err;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002336}
2337
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002338static void trace__set_base_time(struct trace *trace,
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002339 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002340 struct perf_sample *sample)
2341{
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002342 /*
2343 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2344 * and don't use sample->time unconditionally, we may end up having
2345 * some other event in the future without PERF_SAMPLE_TIME for good
2346 * reason, i.e. we may not be interested in its timestamps, just in
2347 * it taking place, picking some piece of information when it
2348 * appears in our event stream (vfs_getname comes to mind).
2349 */
2350 if (trace->base_time == 0 && !trace->full_time &&
2351 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002352 trace->base_time = sample->time;
2353}
2354
David Ahern6810fc92013-08-28 22:29:52 -06002355static int trace__process_sample(struct perf_tool *tool,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002356 union perf_event *event,
David Ahern6810fc92013-08-28 22:29:52 -06002357 struct perf_sample *sample,
2358 struct perf_evsel *evsel,
2359 struct machine *machine __maybe_unused)
2360{
2361 struct trace *trace = container_of(tool, struct trace, tool);
David Ahernaa07df62016-11-25 09:29:52 -07002362 struct thread *thread;
David Ahern6810fc92013-08-28 22:29:52 -06002363 int err = 0;
2364
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002365 tracepoint_handler handler = evsel->handler;
David Ahern6810fc92013-08-28 22:29:52 -06002366
David Ahernaa07df62016-11-25 09:29:52 -07002367 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2368 if (thread && thread__is_filtered(thread))
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002369 goto out;
David Ahernbdc89662013-08-28 22:29:53 -06002370
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002371 trace__set_base_time(trace, evsel, sample);
David Ahern6810fc92013-08-28 22:29:52 -06002372
David Ahern31605652013-12-04 19:41:41 -07002373 if (handler) {
2374 ++trace->nr_events;
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002375 handler(trace, evsel, event, sample);
David Ahern31605652013-12-04 19:41:41 -07002376 }
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002377out:
2378 thread__put(thread);
David Ahern6810fc92013-08-28 22:29:52 -06002379 return err;
2380}
2381
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002382static int trace__record(struct trace *trace, int argc, const char **argv)
David Ahern5e2485b2013-09-28 13:13:01 -06002383{
2384 unsigned int rec_argc, i, j;
2385 const char **rec_argv;
2386 const char * const record_args[] = {
2387 "record",
2388 "-R",
2389 "-m", "1024",
2390 "-c", "1",
David Ahern5e2485b2013-09-28 13:13:01 -06002391 };
2392
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002393 const char * const sc_args[] = { "-e", };
2394 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2395 const char * const majpf_args[] = { "-e", "major-faults" };
2396 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2397 const char * const minpf_args[] = { "-e", "minor-faults" };
2398 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2399
David Ahern9aca7f12013-12-04 19:41:39 -07002400 /* +1 is for the event string below */
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002401 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2402 majpf_args_nr + minpf_args_nr + argc;
David Ahern5e2485b2013-09-28 13:13:01 -06002403 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2404
2405 if (rec_argv == NULL)
2406 return -ENOMEM;
2407
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002408 j = 0;
David Ahern5e2485b2013-09-28 13:13:01 -06002409 for (i = 0; i < ARRAY_SIZE(record_args); i++)
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002410 rec_argv[j++] = record_args[i];
2411
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002412 if (trace->trace_syscalls) {
2413 for (i = 0; i < sc_args_nr; i++)
2414 rec_argv[j++] = sc_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002415
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002416 /* event string may be different for older kernels - e.g., RHEL6 */
2417 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2418 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2419 else if (is_valid_tracepoint("syscalls:sys_enter"))
2420 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2421 else {
2422 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
Martin Kepplingerc896f852017-09-13 21:14:19 +02002423 free(rec_argv);
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002424 return -1;
2425 }
David Ahern9aca7f12013-12-04 19:41:39 -07002426 }
David Ahern9aca7f12013-12-04 19:41:39 -07002427
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002428 if (trace->trace_pgfaults & TRACE_PFMAJ)
2429 for (i = 0; i < majpf_args_nr; i++)
2430 rec_argv[j++] = majpf_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002431
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002432 if (trace->trace_pgfaults & TRACE_PFMIN)
2433 for (i = 0; i < minpf_args_nr; i++)
2434 rec_argv[j++] = minpf_args[i];
2435
2436 for (i = 0; i < (unsigned int)argc; i++)
2437 rec_argv[j++] = argv[i];
2438
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002439 return cmd_record(j, rec_argv);
David Ahern5e2485b2013-09-28 13:13:01 -06002440}
2441
David Ahernbf2575c2013-10-08 21:26:53 -06002442static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2443
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002444static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002445{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002446 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002447
2448 if (IS_ERR(evsel))
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002449 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002450
2451 if (perf_evsel__field(evsel, "pathname") == NULL) {
2452 perf_evsel__delete(evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002453 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002454 }
2455
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002456 evsel->handler = trace__vfs_getname;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002457 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002458 return true;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002459}
2460
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002461static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002462{
2463 struct perf_evsel *evsel;
2464 struct perf_event_attr attr = {
2465 .type = PERF_TYPE_SOFTWARE,
2466 .mmap_data = 1,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002467 };
2468
2469 attr.config = config;
Arnaldo Carvalho de Melo05247982014-07-23 18:15:09 -03002470 attr.sample_period = 1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002471
2472 event_attr_init(&attr);
2473
2474 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002475 if (evsel)
2476 evsel->handler = trace__pgfault;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002477
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002478 return evsel;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002479}
2480
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002481static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2482{
2483 const u32 type = event->header.type;
2484 struct perf_evsel *evsel;
2485
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002486 if (type != PERF_RECORD_SAMPLE) {
2487 trace__process_event(trace, trace->host, event, sample);
2488 return;
2489 }
2490
2491 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2492 if (evsel == NULL) {
2493 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2494 return;
2495 }
2496
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002497 trace__set_base_time(trace, evsel, sample);
2498
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002499 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2500 sample->raw_data == NULL) {
2501 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2502 perf_evsel__name(evsel), sample->tid,
2503 sample->cpu, sample->raw_size);
2504 } else {
2505 tracepoint_handler handler = evsel->handler;
2506 handler(trace, evsel, event, sample);
2507 }
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002508
2509 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2510 interrupted = true;
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002511}
2512
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002513static int trace__add_syscall_newtp(struct trace *trace)
2514{
2515 int ret = -1;
2516 struct perf_evlist *evlist = trace->evlist;
2517 struct perf_evsel *sys_enter, *sys_exit;
2518
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002519 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002520 if (sys_enter == NULL)
2521 goto out;
2522
2523 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2524 goto out_delete_sys_enter;
2525
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002526 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002527 if (sys_exit == NULL)
2528 goto out_delete_sys_enter;
2529
2530 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2531 goto out_delete_sys_exit;
2532
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002533 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2534 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2535
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002536 perf_evlist__add(evlist, sys_enter);
2537 perf_evlist__add(evlist, sys_exit);
2538
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002539 if (callchain_param.enabled && !trace->kernel_syscallchains) {
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03002540 /*
2541 * We're interested only in the user space callchain
2542 * leading to the syscall, allow overriding that for
2543 * debugging reasons using --kernel_syscall_callchains
2544 */
2545 sys_exit->attr.exclude_callchain_kernel = 1;
2546 }
2547
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03002548 trace->syscalls.events.sys_enter = sys_enter;
2549 trace->syscalls.events.sys_exit = sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002550
2551 ret = 0;
2552out:
2553 return ret;
2554
2555out_delete_sys_exit:
2556 perf_evsel__delete_priv(sys_exit);
2557out_delete_sys_enter:
2558 perf_evsel__delete_priv(sys_enter);
2559 goto out;
2560}
2561
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002562static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002563{
2564 int err = -1;
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002565 struct perf_evsel *sys_exit;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002566 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2567 trace->ev_qualifier_ids.nr,
2568 trace->ev_qualifier_ids.entries);
2569
2570 if (filter == NULL)
2571 goto out_enomem;
2572
Mathieu Poirier3541c032016-09-16 08:44:04 -06002573 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2574 filter)) {
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002575 sys_exit = trace->syscalls.events.sys_exit;
Mathieu Poirier3541c032016-09-16 08:44:04 -06002576 err = perf_evsel__append_tp_filter(sys_exit, filter);
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002577 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002578
2579 free(filter);
2580out:
2581 return err;
2582out_enomem:
2583 errno = ENOMEM;
2584 goto out;
2585}
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002586
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002587#ifdef HAVE_LIBBPF_SUPPORT
2588static int trace__set_ev_qualifier_bpf_filter(struct trace *trace)
2589{
2590 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002591 struct bpf_map_syscall_entry value = {
2592 .enabled = !trace->not_ev_qualifier,
2593 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002594 int err = 0;
2595 size_t i;
2596
2597 for (i = 0; i < trace->ev_qualifier_ids.nr; ++i) {
2598 int key = trace->ev_qualifier_ids.entries[i];
2599
2600 err = bpf_map_update_elem(fd, &key, &value, BPF_EXIST);
2601 if (err)
2602 break;
2603 }
2604
2605 return err;
2606}
2607
2608static int __trace__init_syscalls_bpf_map(struct trace *trace, bool enabled)
2609{
2610 int fd = bpf_map__fd(trace->syscalls.map);
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002611 struct bpf_map_syscall_entry value = {
2612 .enabled = enabled,
2613 };
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002614 int err = 0, key;
2615
2616 for (key = 0; key < trace->sctbl->syscalls.nr_entries; ++key) {
Arnaldo Carvalho de Melobbab50d2018-12-12 16:54:09 -03002617 err = bpf_map_update_elem(fd, &key, &value, BPF_ANY);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002618 if (err)
2619 break;
2620 }
2621
2622 return err;
2623}
2624
2625static int trace__init_syscalls_bpf_map(struct trace *trace)
2626{
2627 bool enabled = true;
2628
2629 if (trace->ev_qualifier_ids.nr)
2630 enabled = trace->not_ev_qualifier;
2631
2632 return __trace__init_syscalls_bpf_map(trace, enabled);
2633}
2634#else
2635static int trace__set_ev_qualifier_bpf_filter(struct trace *trace __maybe_unused)
2636{
2637 return 0;
2638}
2639
2640static int trace__init_syscalls_bpf_map(struct trace *trace __maybe_unused)
2641{
2642 return 0;
2643}
2644#endif // HAVE_LIBBPF_SUPPORT
2645
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002646static int trace__set_ev_qualifier_filter(struct trace *trace)
2647{
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002648 if (trace->syscalls.map)
2649 return trace__set_ev_qualifier_bpf_filter(trace);
Arnaldo Carvalho de Melo246fbe02018-12-12 11:06:53 -03002650 return trace__set_ev_qualifier_tp_filter(trace);
2651}
2652
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002653static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused,
2654 size_t npids __maybe_unused, pid_t *pids __maybe_unused)
2655{
2656 int err = 0;
2657#ifdef HAVE_LIBBPF_SUPPORT
2658 bool value = true;
2659 int map_fd = bpf_map__fd(map);
2660 size_t i;
2661
2662 for (i = 0; i < npids; ++i) {
2663 err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY);
2664 if (err)
2665 break;
2666 }
2667#endif
2668 return err;
2669}
2670
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002671static int trace__set_filter_loop_pids(struct trace *trace)
2672{
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002673 unsigned int nr = 1, err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002674 pid_t pids[32] = {
2675 getpid(),
2676 };
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03002677 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2678
2679 while (thread && nr < ARRAY_SIZE(pids)) {
2680 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2681
2682 if (parent == NULL)
2683 break;
2684
2685 if (!strcmp(thread__comm_str(parent), "sshd")) {
2686 pids[nr++] = parent->tid;
2687 break;
2688 }
2689 thread = parent;
2690 }
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002691
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002692 err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids);
2693 if (!err && trace->filter_pids.map)
2694 err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids);
2695
2696 return err;
2697}
2698
2699static int trace__set_filter_pids(struct trace *trace)
2700{
2701 int err = 0;
2702 /*
2703 * Better not use !target__has_task() here because we need to cover the
2704 * case where no threads were specified in the command line, but a
2705 * workload was, and in that case we will fill in the thread_map when
2706 * we fork the workload in perf_evlist__prepare_workload.
2707 */
2708 if (trace->filter_pids.nr > 0) {
2709 err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
2710 trace->filter_pids.entries);
2711 if (!err && trace->filter_pids.map) {
2712 err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
2713 trace->filter_pids.entries);
2714 }
2715 } else if (thread_map__pid(trace->evlist->threads, 0) == -1) {
2716 err = trace__set_filter_loop_pids(trace);
2717 }
2718
2719 return err;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002720}
2721
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002722static int __trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa1f44b3e2018-12-05 17:05:08 +01002723{
2724 struct perf_evlist *evlist = trace->evlist;
2725 struct perf_sample sample;
2726 int err;
2727
2728 err = perf_evlist__parse_sample(evlist, event, &sample);
2729 if (err)
2730 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
2731 else
2732 trace__handle_event(trace, event, &sample);
2733
2734 return 0;
2735}
2736
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002737static int __trace__flush_events(struct trace *trace)
Jiri Olsa028713a2018-12-05 17:05:09 +01002738{
2739 u64 first = ordered_events__first_time(&trace->oe.data);
2740 u64 flush = trace->oe.last - NSEC_PER_SEC;
2741
2742 /* Is there some thing to flush.. */
2743 if (first && first < flush)
2744 return ordered_events__flush_time(&trace->oe.data, flush);
2745
2746 return 0;
2747}
2748
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002749static int trace__flush_events(struct trace *trace)
2750{
2751 return !trace->sort_events ? 0 : __trace__flush_events(trace);
2752}
2753
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002754static int trace__deliver_event(struct trace *trace, union perf_event *event)
Jiri Olsa028713a2018-12-05 17:05:09 +01002755{
Jiri Olsa028713a2018-12-05 17:05:09 +01002756 int err;
2757
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002758 if (!trace->sort_events)
2759 return __trace__deliver_event(trace, event);
2760
2761 err = perf_evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
Jiri Olsa028713a2018-12-05 17:05:09 +01002762 if (err && err != -1)
2763 return err;
2764
2765 err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0);
2766 if (err)
2767 return err;
2768
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002769 return trace__flush_events(trace);
Jiri Olsa028713a2018-12-05 17:05:09 +01002770}
2771
2772static int ordered_events__deliver_event(struct ordered_events *oe,
2773 struct ordered_event *event)
2774{
2775 struct trace *trace = container_of(oe, struct trace, oe.data);
2776
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002777 return __trace__deliver_event(trace, event->event);
Jiri Olsa028713a2018-12-05 17:05:09 +01002778}
2779
Namhyung Kimf15eb532012-10-05 14:02:16 +09002780static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002781{
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002782 struct perf_evlist *evlist = trace->evlist;
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002783 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002784 int err = -1, i;
2785 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002786 const bool forks = argc > 0;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002787 bool draining = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002788
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002789 trace->live = true;
2790
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002791 if (!trace->raw_augmented_syscalls) {
2792 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
2793 goto out_error_raw_syscalls;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002794
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002795 if (trace->trace_syscalls)
2796 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
2797 }
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002798
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002799 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2800 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2801 if (pgfault_maj == NULL)
2802 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002803 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002804 perf_evlist__add(evlist, pgfault_maj);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03002805 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002806
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002807 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2808 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2809 if (pgfault_min == NULL)
2810 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002811 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002812 perf_evlist__add(evlist, pgfault_min);
2813 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002814
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002815 if (trace->sched &&
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002816 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2817 trace__sched_stat_runtime))
2818 goto out_error_sched_stat_runtime;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002819
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03002820 /*
2821 * If a global cgroup was set, apply it to all the events without an
2822 * explicit cgroup. I.e.:
2823 *
2824 * trace -G A -e sched:*switch
2825 *
2826 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2827 * _and_ sched:sched_switch to the 'A' cgroup, while:
2828 *
2829 * trace -e sched:*switch -G A
2830 *
2831 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2832 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2833 * a cgroup (on the root cgroup, sys wide, etc).
2834 *
2835 * Multiple cgroups:
2836 *
2837 * trace -G A -e sched:*switch -G B
2838 *
2839 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2840 * to the 'B' cgroup.
2841 *
2842 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2843 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2844 */
2845 if (trace->cgroup)
2846 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2847
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002848 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2849 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002850 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002851 goto out_delete_evlist;
2852 }
2853
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002854 err = trace__symbols_init(trace, evlist);
2855 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002856 fprintf(trace->output, "Problems initializing symbol libraries!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002857 goto out_delete_evlist;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002858 }
2859
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03002860 perf_evlist__config(evlist, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melofde54b72016-04-11 18:42:37 -03002861
Namhyung Kimf15eb532012-10-05 14:02:16 +09002862 signal(SIGCHLD, sig_handler);
2863 signal(SIGINT, sig_handler);
2864
2865 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09002866 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03002867 argv, false, NULL);
Namhyung Kimf15eb532012-10-05 14:02:16 +09002868 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002869 fprintf(trace->output, "Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002870 goto out_delete_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002871 }
2872 }
2873
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002874 err = perf_evlist__open(evlist);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002875 if (err < 0)
2876 goto out_error_open;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002877
Wang Nanba504232016-02-26 09:31:54 +00002878 err = bpf__apply_obj_config();
2879 if (err) {
2880 char errbuf[BUFSIZ];
2881
2882 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2883 pr_err("ERROR: Apply config to BPF failed: %s\n",
2884 errbuf);
2885 goto out_error_open;
2886 }
2887
Arnaldo Carvalho de Meloa9964c42018-11-07 10:08:00 -03002888 err = trace__set_filter_pids(trace);
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002889 if (err < 0)
2890 goto out_error_mem;
2891
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03002892 if (trace->syscalls.map)
2893 trace__init_syscalls_bpf_map(trace);
2894
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002895 if (trace->ev_qualifier_ids.nr > 0) {
2896 err = trace__set_ev_qualifier_filter(trace);
2897 if (err < 0)
2898 goto out_errno;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002899
Arnaldo Carvalho de Melo0df50e02018-12-12 13:47:40 -03002900 if (trace->syscalls.events.sys_exit) {
2901 pr_debug("event qualifier tracepoint filter: %s\n",
2902 trace->syscalls.events.sys_exit->filter);
2903 }
Arnaldo Carvalho de Melo2e5e5f82015-08-03 17:12:29 -03002904 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002905
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002906 err = perf_evlist__apply_filters(evlist, &evsel);
2907 if (err < 0)
2908 goto out_error_apply_filters;
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08002909
Wang Nanf74b9d3a2017-12-03 02:00:37 +00002910 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002911 if (err < 0)
2912 goto out_error_mmap;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002913
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002914 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
Arnaldo Carvalho de Melocb24d012015-04-22 10:04:23 -03002915 perf_evlist__enable(evlist);
2916
Namhyung Kimf15eb532012-10-05 14:02:16 +09002917 if (forks)
2918 perf_evlist__start_workload(evlist);
2919
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002920 if (trace->opts.initial_delay) {
2921 usleep(trace->opts.initial_delay * 1000);
2922 perf_evlist__enable(evlist);
2923 }
2924
Jiri Olsae13798c2015-06-23 00:36:02 +02002925 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
Arnaldo Carvalho de Melo42052be2015-02-13 12:32:45 -03002926 evlist->threads->nr > 1 ||
2927 perf_evlist__first(evlist)->attr.inherit;
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002928
2929 /*
2930 * Now that we already used evsel->attr to ask the kernel to setup the
2931 * events, lets reuse evsel->attr.sample_max_stack as the limit in
2932 * trace__resolve_callchain(), allowing per-event max-stack settings
Ingo Molnaradba1632018-12-03 11:22:00 +01002933 * to override an explicitly set --max-stack global setting.
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002934 */
2935 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03002936 if (evsel__has_callchain(evsel) &&
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002937 evsel->attr.sample_max_stack == 0)
2938 evsel->attr.sample_max_stack = trace->max_stack;
2939 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002940again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002941 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002942
2943 for (i = 0; i < evlist->nr_mmaps; i++) {
2944 union perf_event *event;
Kan Liangd7f55c62018-03-01 18:08:59 -05002945 struct perf_mmap *md;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002946
Kan Liangd7f55c62018-03-01 18:08:59 -05002947 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -05002948 if (perf_mmap__read_init(md) < 0)
Kan Liangd7f55c62018-03-01 18:08:59 -05002949 continue;
2950
Kan Liang0019dc872018-03-06 10:36:06 -05002951 while ((event = perf_mmap__read_event(md)) != NULL) {
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002952 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002953
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002954 err = trace__deliver_event(trace, event);
Jiri Olsa028713a2018-12-05 17:05:09 +01002955 if (err)
2956 goto out_disable;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002957
Kan Liangd6ace3d2018-03-06 10:36:05 -05002958 perf_mmap__consume(md);
Arnaldo Carvalho de Melo20c5f102013-09-03 11:55:07 -03002959
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002960 if (interrupted)
2961 goto out_disable;
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03002962
2963 if (done && !draining) {
2964 perf_evlist__disable(evlist);
2965 draining = true;
2966 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002967 }
Kan Liangd7f55c62018-03-01 18:08:59 -05002968 perf_mmap__read_done(md);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002969 }
2970
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002971 if (trace->nr_events == before) {
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002972 int timeout = done ? 100 : -1;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002973
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002974 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
Arnaldo Carvalho de Meloa9c5e6c2018-10-22 14:14:16 -03002975 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002976 draining = true;
2977
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002978 goto again;
Jiri Olsa028713a2018-12-05 17:05:09 +01002979 } else {
Arnaldo Carvalho de Melo7ba61522018-12-10 16:09:51 -03002980 if (trace__flush_events(trace))
Jiri Olsa028713a2018-12-05 17:05:09 +01002981 goto out_disable;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002982 }
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002983 } else {
2984 goto again;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002985 }
2986
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002987out_disable:
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03002988 thread__zput(trace->current);
2989
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002990 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002991
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03002992 if (trace->sort_events)
2993 ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
Jiri Olsa028713a2018-12-05 17:05:09 +01002994
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002995 if (!err) {
2996 if (trace->summary)
2997 trace__fprintf_thread_summary(trace, trace->output);
2998
2999 if (trace->show_tool_stats) {
3000 fprintf(trace->output, "Stats:\n "
3001 " vfs_getname : %" PRIu64 "\n"
3002 " proc_getname: %" PRIu64 "\n",
3003 trace->stats.vfs_getname,
3004 trace->stats.proc_getname);
3005 }
3006 }
David Ahernbf2575c2013-10-08 21:26:53 -06003007
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003008out_delete_evlist:
Andrei Vagin33974a42017-11-07 16:22:45 -08003009 trace__symbols__exit(trace);
3010
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003011 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003012 cgroup__put(trace->cgroup);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003013 trace->evlist = NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03003014 trace->live = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003015 return err;
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003016{
3017 char errbuf[BUFSIZ];
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003018
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003019out_error_sched_stat_runtime:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003020 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03003021 goto out_error;
3022
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03003023out_error_raw_syscalls:
Jiri Olsa988bdb32015-09-02 09:56:35 +02003024 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003025 goto out_error;
3026
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03003027out_error_mmap:
3028 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
3029 goto out_error;
3030
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003031out_error_open:
3032 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
3033
3034out_error:
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03003035 fprintf(trace->output, "%s\n", errbuf);
Ramkumar Ramachandra87f91862013-10-04 10:47:31 +05303036 goto out_delete_evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003037
3038out_error_apply_filters:
3039 fprintf(trace->output,
3040 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
3041 evsel->filter, perf_evsel__name(evsel), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03003042 str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03003043 goto out_delete_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003044}
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03003045out_error_mem:
3046 fprintf(trace->output, "Not enough memory to run!\n");
3047 goto out_delete_evlist;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03003048
3049out_errno:
3050 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
3051 goto out_delete_evlist;
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03003052}
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003053
David Ahern6810fc92013-08-28 22:29:52 -06003054static int trace__replay(struct trace *trace)
3055{
3056 const struct perf_evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003057 { "probe:vfs_getname", trace__vfs_getname, },
David Ahern6810fc92013-08-28 22:29:52 -06003058 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003059 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003060 .file = {
3061 .path = input_name,
3062 },
3063 .mode = PERF_DATA_MODE_READ,
3064 .force = trace->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02003065 };
David Ahern6810fc92013-08-28 22:29:52 -06003066 struct perf_session *session;
Namhyung Kim003824e2013-11-12 15:25:00 +09003067 struct perf_evsel *evsel;
David Ahern6810fc92013-08-28 22:29:52 -06003068 int err = -1;
3069
3070 trace->tool.sample = trace__process_sample;
3071 trace->tool.mmap = perf_event__process_mmap;
David Ahern384c6712013-09-22 19:44:58 -06003072 trace->tool.mmap2 = perf_event__process_mmap2;
David Ahern6810fc92013-08-28 22:29:52 -06003073 trace->tool.comm = perf_event__process_comm;
3074 trace->tool.exit = perf_event__process_exit;
3075 trace->tool.fork = perf_event__process_fork;
3076 trace->tool.attr = perf_event__process_attr;
Hari Bathinif3b36142017-03-08 02:11:43 +05303077 trace->tool.tracing_data = perf_event__process_tracing_data;
David Ahern6810fc92013-08-28 22:29:52 -06003078 trace->tool.build_id = perf_event__process_build_id;
Hari Bathinif3b36142017-03-08 02:11:43 +05303079 trace->tool.namespaces = perf_event__process_namespaces;
David Ahern6810fc92013-08-28 22:29:52 -06003080
Jiri Olsa0a8cb852014-07-06 14:18:21 +02003081 trace->tool.ordered_events = true;
David Ahern6810fc92013-08-28 22:29:52 -06003082 trace->tool.ordering_requires_timestamps = true;
3083
3084 /* add tid to output */
3085 trace->multiple_threads = true;
3086
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003087 session = perf_session__new(&data, false, &trace->tool);
David Ahern6810fc92013-08-28 22:29:52 -06003088 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09003089 return -1;
David Ahern6810fc92013-08-28 22:29:52 -06003090
David Ahernaa07df62016-11-25 09:29:52 -07003091 if (trace->opts.target.pid)
3092 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
3093
3094 if (trace->opts.target.tid)
3095 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
3096
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09003097 if (symbol__init(&session->header.env) < 0)
Namhyung Kimcb2ffae2014-08-12 15:40:44 +09003098 goto out;
3099
David Ahern8fb598e2013-09-28 13:13:00 -06003100 trace->host = &session->machines.host;
3101
David Ahern6810fc92013-08-28 22:29:52 -06003102 err = perf_session__set_tracepoints_handlers(session, handlers);
3103 if (err)
3104 goto out;
3105
Namhyung Kim003824e2013-11-12 15:25:00 +09003106 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3107 "raw_syscalls:sys_enter");
David Ahern9aca7f12013-12-04 19:41:39 -07003108 /* older kernels have syscalls tp versus raw_syscalls */
3109 if (evsel == NULL)
3110 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3111 "syscalls:sys_enter");
David Ahern6810fc92013-08-28 22:29:52 -06003112
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003113 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003114 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003115 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003116 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
3117 goto out;
3118 }
3119
3120 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3121 "raw_syscalls:sys_exit");
David Ahern9aca7f12013-12-04 19:41:39 -07003122 if (evsel == NULL)
3123 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
3124 "syscalls:sys_exit");
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003125 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03003126 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003127 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09003128 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
David Ahern6810fc92013-08-28 22:29:52 -06003129 goto out;
3130 }
3131
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003132 evlist__for_each_entry(session->evlist, evsel) {
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003133 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
3134 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
3135 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
3136 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
3137 evsel->handler = trace__pgfault;
3138 }
3139
David Ahern6810fc92013-08-28 22:29:52 -06003140 setup_pager();
3141
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03003142 err = perf_session__process_events(session);
David Ahern6810fc92013-08-28 22:29:52 -06003143 if (err)
3144 pr_err("Failed to process events, error %d", err);
3145
David Ahernbf2575c2013-10-08 21:26:53 -06003146 else if (trace->summary)
3147 trace__fprintf_thread_summary(trace, trace->output);
3148
David Ahern6810fc92013-08-28 22:29:52 -06003149out:
3150 perf_session__delete(session);
3151
3152 return err;
3153}
3154
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003155static size_t trace__fprintf_threads_header(FILE *fp)
3156{
3157 size_t printed;
3158
Pekka Enberg99ff7152013-11-12 16:42:14 +02003159 printed = fprintf(fp, "\n Summary of events:\n\n");
David Ahernbf2575c2013-10-08 21:26:53 -06003160
3161 return printed;
3162}
3163
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003164DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
3165 struct stats *stats;
3166 double msecs;
3167 int syscall;
3168)
3169{
3170 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
3171 struct stats *stats = source->priv;
3172
3173 entry->syscall = source->i;
3174 entry->stats = stats;
3175 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
3176}
3177
David Ahernbf2575c2013-10-08 21:26:53 -06003178static size_t thread__dump_stats(struct thread_trace *ttrace,
3179 struct trace *trace, FILE *fp)
3180{
David Ahernbf2575c2013-10-08 21:26:53 -06003181 size_t printed = 0;
3182 struct syscall *sc;
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003183 struct rb_node *nd;
3184 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003185
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003186 if (syscall_stats == NULL)
David Ahernbf2575c2013-10-08 21:26:53 -06003187 return 0;
3188
3189 printed += fprintf(fp, "\n");
3190
Milian Wolff834fd462015-08-06 11:24:29 +02003191 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
3192 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
3193 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
Pekka Enberg99ff7152013-11-12 16:42:14 +02003194
Arnaldo Carvalho de Melo98a91832016-06-23 11:34:10 -03003195 resort_rb__for_each_entry(nd, syscall_stats) {
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003196 struct stats *stats = syscall_stats_entry->stats;
David Ahernbf2575c2013-10-08 21:26:53 -06003197 if (stats) {
3198 double min = (double)(stats->min) / NSEC_PER_MSEC;
3199 double max = (double)(stats->max) / NSEC_PER_MSEC;
3200 double avg = avg_stats(stats);
3201 double pct;
3202 u64 n = (u64) stats->n;
3203
3204 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
3205 avg /= NSEC_PER_MSEC;
3206
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003207 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
Pekka Enberg99ff7152013-11-12 16:42:14 +02003208 printed += fprintf(fp, " %-15s", sc->name);
Milian Wolff834fd462015-08-06 11:24:29 +02003209 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003210 n, syscall_stats_entry->msecs, min, avg);
Pekka Enberg27a778b2013-11-13 14:21:48 +02003211 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
David Ahernbf2575c2013-10-08 21:26:53 -06003212 }
David Ahernbf2575c2013-10-08 21:26:53 -06003213 }
3214
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03003215 resort_rb__delete(syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06003216 printed += fprintf(fp, "\n\n");
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003217
3218 return printed;
3219}
3220
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003221static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
David Ahern896cbb52013-09-28 13:12:59 -06003222{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003223 size_t printed = 0;
Namhyung Kim89dceb22014-10-06 09:46:03 +09003224 struct thread_trace *ttrace = thread__priv(thread);
David Ahern896cbb52013-09-28 13:12:59 -06003225 double ratio;
3226
3227 if (ttrace == NULL)
3228 return 0;
3229
3230 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
3231
Pekka Enberg15e65c62013-11-14 18:43:30 +02003232 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
Pekka Enberg99ff7152013-11-12 16:42:14 +02003233 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
Pekka Enberg15e65c62013-11-14 18:43:30 +02003234 printed += fprintf(fp, "%.1f%%", ratio);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04003235 if (ttrace->pfmaj)
3236 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
3237 if (ttrace->pfmin)
3238 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
Arnaldo Carvalho de Melo03548eb2016-05-05 15:46:50 -03003239 if (trace->sched)
3240 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
3241 else if (fputc('\n', fp) != EOF)
3242 ++printed;
3243
David Ahernbf2575c2013-10-08 21:26:53 -06003244 printed += thread__dump_stats(ttrace, trace, fp);
David Ahern896cbb52013-09-28 13:12:59 -06003245
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003246 return printed;
3247}
David Ahern896cbb52013-09-28 13:12:59 -06003248
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003249static unsigned long thread__nr_events(struct thread_trace *ttrace)
3250{
3251 return ttrace ? ttrace->nr_events : 0;
3252}
3253
3254DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3255 struct thread *thread;
3256)
3257{
3258 entry->thread = rb_entry(nd, struct thread, rb_node);
David Ahern896cbb52013-09-28 13:12:59 -06003259}
3260
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003261static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3262{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003263 size_t printed = trace__fprintf_threads_header(fp);
3264 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -07003265 int i;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003266
Kan Liang91e467b2017-09-10 19:23:14 -07003267 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3268 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3269
3270 if (threads == NULL) {
3271 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3272 return 0;
3273 }
3274
3275 resort_rb__for_each_entry(nd, threads)
3276 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3277
3278 resort_rb__delete(threads);
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003279 }
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003280 return printed;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003281}
3282
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003283static int trace__set_duration(const struct option *opt, const char *str,
3284 int unset __maybe_unused)
3285{
3286 struct trace *trace = opt->value;
3287
3288 trace->duration_filter = atof(str);
3289 return 0;
3290}
3291
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003292static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
3293 int unset __maybe_unused)
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08003294{
3295 int ret = -1;
3296 size_t i;
3297 struct trace *trace = opt->value;
3298 /*
3299 * FIXME: introduce a intarray class, plain parse csv and create a
3300 * { int nr, int entries[] } struct...
3301 */
3302 struct intlist *list = intlist__new(str);
3303
3304 if (list == NULL)
3305 return -1;
3306
3307 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3308 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3309
3310 if (trace->filter_pids.entries == NULL)
3311 goto out;
3312
3313 trace->filter_pids.entries[0] = getpid();
3314
3315 for (i = 1; i < trace->filter_pids.nr; ++i)
3316 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3317
3318 intlist__delete(list);
3319 ret = 0;
3320out:
3321 return ret;
3322}
3323
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003324static int trace__open_output(struct trace *trace, const char *filename)
3325{
3326 struct stat st;
3327
3328 if (!stat(filename, &st) && st.st_size) {
3329 char oldname[PATH_MAX];
3330
3331 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3332 unlink(oldname);
3333 rename(filename, oldname);
3334 }
3335
3336 trace->output = fopen(filename, "w");
3337
3338 return trace->output == NULL ? -errno : 0;
3339}
3340
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003341static int parse_pagefaults(const struct option *opt, const char *str,
3342 int unset __maybe_unused)
3343{
3344 int *trace_pgfaults = opt->value;
3345
3346 if (strcmp(str, "all") == 0)
3347 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3348 else if (strcmp(str, "maj") == 0)
3349 *trace_pgfaults |= TRACE_PFMAJ;
3350 else if (strcmp(str, "min") == 0)
3351 *trace_pgfaults |= TRACE_PFMIN;
3352 else
3353 return -1;
3354
3355 return 0;
3356}
3357
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003358static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3359{
3360 struct perf_evsel *evsel;
3361
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003362 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003363 evsel->handler = handler;
3364}
3365
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003366static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3367{
3368 struct perf_evsel *evsel;
3369
3370 evlist__for_each_entry(evlist, evsel) {
3371 if (evsel->priv || !evsel->tp_format)
3372 continue;
3373
3374 if (strcmp(evsel->tp_format->system, "syscalls"))
3375 continue;
3376
3377 if (perf_evsel__init_syscall_tp(evsel))
3378 return -1;
3379
3380 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3381 struct syscall_tp *sc = evsel->priv;
3382
3383 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3384 return -1;
3385 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3386 struct syscall_tp *sc = evsel->priv;
3387
3388 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3389 return -1;
3390 }
3391 }
3392
3393 return 0;
3394}
3395
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003396/*
3397 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3398 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3399 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3400 *
3401 * It'd be better to introduce a parse_options() variant that would return a
3402 * list with the terms it didn't match to an event...
3403 */
3404static int trace__parse_events_option(const struct option *opt, const char *str,
3405 int unset __maybe_unused)
3406{
3407 struct trace *trace = (struct trace *)opt->value;
3408 const char *s = str;
3409 char *sep = NULL, *lists[2] = { NULL, NULL, };
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003410 int len = strlen(str) + 1, err = -1, list, idx;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003411 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3412 char group_name[PATH_MAX];
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003413 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003414
3415 if (strace_groups_dir == NULL)
3416 return -1;
3417
3418 if (*s == '!') {
3419 ++s;
3420 trace->not_ev_qualifier = true;
3421 }
3422
3423 while (1) {
3424 if ((sep = strchr(s, ',')) != NULL)
3425 *sep = '\0';
3426
3427 list = 0;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003428 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3429 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003430 list = 1;
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003431 goto do_concat;
3432 }
3433
3434 fmt = syscall_fmt__find_by_alias(s);
3435 if (fmt != NULL) {
3436 list = 1;
3437 s = fmt->name;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003438 } else {
3439 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3440 if (access(group_name, R_OK) == 0)
3441 list = 1;
3442 }
Arnaldo Carvalho de Melof9321842018-10-25 17:24:45 -03003443do_concat:
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003444 if (lists[list]) {
3445 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3446 } else {
3447 lists[list] = malloc(len);
3448 if (lists[list] == NULL)
3449 goto out;
3450 strcpy(lists[list], s);
3451 }
3452
3453 if (!sep)
3454 break;
3455
3456 *sep = ',';
3457 s = sep + 1;
3458 }
3459
3460 if (lists[1] != NULL) {
3461 struct strlist_config slist_config = {
3462 .dirname = strace_groups_dir,
3463 };
3464
3465 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3466 if (trace->ev_qualifier == NULL) {
3467 fputs("Not enough memory to parse event qualifier", trace->output);
3468 goto out;
3469 }
3470
3471 if (trace__validate_ev_qualifier(trace))
3472 goto out;
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003473 trace->trace_syscalls = true;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003474 }
3475
3476 err = 0;
3477
3478 if (lists[0]) {
3479 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3480 "event selector. use 'perf list' to list available events",
3481 parse_events_option);
3482 err = parse_events_option(&o, lists[0], 0);
3483 }
3484out:
3485 if (sep)
3486 *sep = ',';
3487
3488 return err;
3489}
3490
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003491static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3492{
3493 struct trace *trace = opt->value;
3494
3495 if (!list_empty(&trace->evlist->entries))
3496 return parse_cgroups(opt, str, unset);
3497
3498 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3499
3500 return 0;
3501}
3502
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003503static struct bpf_map *bpf__find_map_by_name(const char *name)
3504{
3505 struct bpf_object *obj, *tmp;
3506
3507 bpf_object__for_each_safe(obj, tmp) {
3508 struct bpf_map *map = bpf_object__find_map_by_name(obj, name);
3509 if (map)
3510 return map;
3511
3512 }
3513
3514 return NULL;
3515}
3516
3517static void trace__set_bpf_map_filtered_pids(struct trace *trace)
3518{
3519 trace->filter_pids.map = bpf__find_map_by_name("pids_filtered");
3520}
3521
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003522static void trace__set_bpf_map_syscalls(struct trace *trace)
3523{
3524 trace->syscalls.map = bpf__find_map_by_name("syscalls");
3525}
3526
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003527static int trace__config(const char *var, const char *value, void *arg)
3528{
3529 int err = 0;
3530
3531 if (!strcmp(var, "trace.add_events")) {
3532 struct trace *trace = arg;
3533 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3534 "event selector. use 'perf list' to list available events",
3535 parse_events_option);
3536 err = parse_events_option(&o, value, 0);
3537 }
3538
3539 return err;
3540}
3541
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003542int cmd_trace(int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003543{
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003544 const char *trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +09003545 "perf trace [<options>] [<command>]",
3546 "perf trace [<options>] -- <command> [<options>]",
David Ahern5e2485b2013-09-28 13:13:01 -06003547 "perf trace record [<options>] [<command>]",
3548 "perf trace record [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003549 NULL
3550 };
3551 struct trace trace = {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003552 .syscalls = {
3553 . max = -1,
3554 },
3555 .opts = {
3556 .target = {
3557 .uid = UINT_MAX,
3558 .uses_mmap = true,
3559 },
3560 .user_freq = UINT_MAX,
3561 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03003562 .no_buffering = true,
Arnaldo Carvalho de Melo38d54472014-12-12 17:28:32 -03003563 .mmap_pages = UINT_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003564 },
Milian Wolff007d66a2015-08-05 16:52:23 -03003565 .output = stderr,
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003566 .show_comm = true,
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003567 .trace_syscalls = false,
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003568 .kernel_syscallchains = false,
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003569 .max_stack = UINT_MAX,
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003570 .max_events = ULONG_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003571 };
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003572 const char *output_name = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003573 const struct option trace_options[] = {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003574 OPT_CALLBACK('e', "event", &trace, "event",
3575 "event/syscall selector. use 'perf list' to list available events",
3576 trace__parse_events_option),
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003577 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3578 "show the thread COMM next to its id"),
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003579 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003580 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3581 trace__parse_events_option),
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003582 OPT_STRING('o', "output", &output_name, "file", "output file name"),
David Ahern6810fc92013-08-28 22:29:52 -06003583 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003584 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3585 "trace events on existing process id"),
David Ahernac9be8e2013-08-20 11:15:45 -06003586 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003587 "trace events on existing thread id"),
Arnaldo Carvalho de Melofa0e4ff2015-04-23 11:59:20 -03003588 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
Arnaldo Carvalho de Melo6a0b3ab2018-11-07 09:38:49 -03003589 "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
David Ahernac9be8e2013-08-20 11:15:45 -06003590 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003591 "system-wide collection from all CPUs"),
David Ahernac9be8e2013-08-20 11:15:45 -06003592 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003593 "list of cpus to monitor"),
David Ahern6810fc92013-08-28 22:29:52 -06003594 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003595 "child tasks do not inherit counters"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02003596 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3597 "number of mmap data pages",
3598 perf_evlist__parse_mmap_pages),
David Ahernac9be8e2013-08-20 11:15:45 -06003599 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003600 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003601 OPT_CALLBACK(0, "duration", &trace, "float",
3602 "show only events with duration > N.M ms",
3603 trace__set_duration),
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003604 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03003605 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
David Ahern4bb09192013-09-04 12:37:43 -06003606 OPT_BOOLEAN('T', "time", &trace.full_time,
3607 "Show full timestamp, not time relative to first start"),
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03003608 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3609 "Show only syscalls that failed"),
David Ahernfd2eaba2013-11-12 09:31:15 -07003610 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3611 "Show only syscall summary with statistics"),
3612 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3613 "Show all syscalls and summary with statistics"),
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003614 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3615 "Trace pagefaults", parse_pagefaults, "maj"),
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003616 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
Yunlong Songe366a6d2015-04-02 21:47:18 +08003617 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
Milian Wolff566a0882016-04-08 13:34:15 +02003618 OPT_CALLBACK(0, "call-graph", &trace.opts,
3619 "record_mode[,record_size]", record_callchain_help,
3620 &record_parse_callchain_opt),
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003621 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3622 "Show the kernel callchains on the syscall exit path"),
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003623 OPT_ULONG(0, "max-events", &trace.max_events,
3624 "Set the maximum number of events to print, exit after that is reached. "),
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03003625 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3626 "Set the minimum stack depth when parsing the callchain, "
3627 "anything below the specified depth will be ignored."),
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -03003628 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3629 "Set the maximum stack depth when parsing the callchain, "
3630 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003631 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003632 OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
3633 "Sort batch of events before processing, use if getting out of order events"),
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03003634 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3635 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
Mark Drayton3fcb10e2018-12-04 12:34:20 -08003636 OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
Kan Liang9d9cad72015-06-17 09:51:11 -04003637 "per thread proc mmap processing timeout in ms"),
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003638 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3639 trace__parse_cgroups),
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003640 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3641 "ms to wait before starting measurement after program "
3642 "start"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003643 OPT_END()
3644 };
Arnaldo Carvalho de Meloccd62a82016-04-16 09:36:32 -03003645 bool __maybe_unused max_stack_user_set = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003646 bool mmap_pages_user_set = true;
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003647 struct perf_evsel *evsel;
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003648 const char * const trace_subcommands[] = { "record", NULL };
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003649 int err = -1;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003650 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003651
Arnaldo Carvalho de Melo4d08cb82015-02-24 15:35:55 -03003652 signal(SIGSEGV, sighandler_dump_stack);
3653 signal(SIGFPE, sighandler_dump_stack);
3654
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003655 trace.evlist = perf_evlist__new();
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003656 trace.sctbl = syscalltbl__new();
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003657
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003658 if (trace.evlist == NULL || trace.sctbl == NULL) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003659 pr_err("Not enough memory to run!\n");
He Kuangff8f6952015-05-11 12:28:36 +00003660 err = -ENOMEM;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003661 goto out;
3662 }
3663
Arnaldo Carvalho de Meloac962872018-12-13 17:30:20 -03003664 err = perf_config(trace__config, &trace);
3665 if (err)
3666 goto out;
3667
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003668 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3669 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
David Ahernfd2eaba2013-11-12 09:31:15 -07003670
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003671 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3672 usage_with_options_msg(trace_usage, trace_options,
3673 "cgroup monitoring only available in system-wide mode");
3674 }
3675
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003676 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3677 if (IS_ERR(evsel)) {
3678 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
Arnaldo Carvalho de Meloe0b6d2e2018-08-07 15:40:13 -03003679 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3680 goto out;
3681 }
3682
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003683 if (evsel) {
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003684 trace.syscalls.events.augmented = evsel;
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003685 trace__set_bpf_map_filtered_pids(&trace);
Arnaldo Carvalho de Melob27b38e2018-12-12 13:39:24 -03003686 trace__set_bpf_map_syscalls(&trace);
Arnaldo Carvalho de Melo744fafc2018-11-07 10:04:51 -03003687 }
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003688
Wang Nand7888572016-04-08 15:07:24 +00003689 err = bpf__setup_stdout(trace.evlist);
3690 if (err) {
3691 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3692 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3693 goto out;
3694 }
3695
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003696 err = -1;
3697
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003698 if (trace.trace_pgfaults) {
3699 trace.opts.sample_address = true;
3700 trace.opts.sample_time = true;
3701 }
3702
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003703 if (trace.opts.mmap_pages == UINT_MAX)
3704 mmap_pages_user_set = false;
3705
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003706 if (trace.max_stack == UINT_MAX) {
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03003707 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003708 max_stack_user_set = false;
3709 }
3710
3711#ifdef HAVE_DWARF_UNWIND_SUPPORT
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003712 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003713 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003714 }
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003715#endif
3716
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03003717 if (callchain_param.enabled) {
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003718 if (!mmap_pages_user_set && geteuid() == 0)
3719 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3720
Milian Wolff566a0882016-04-08 13:34:15 +02003721 symbol_conf.use_callchain = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003722 }
Milian Wolff566a0882016-04-08 13:34:15 +02003723
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003724 if (trace.evlist->nr_entries > 0) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003725 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003726 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3727 perror("failed to set syscalls:* tracepoint fields");
3728 goto out;
3729 }
3730 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003731
Arnaldo Carvalho de Meloa761a8d2018-12-10 16:27:33 -03003732 if (trace.sort_events) {
3733 ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
3734 ordered_events__set_copy_on_queue(&trace.oe.data, true);
3735 }
Jiri Olsa028713a2018-12-05 17:05:09 +01003736
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003737 /*
3738 * If we are augmenting syscalls, then combine what we put in the
3739 * __augmented_syscalls__ BPF map with what is in the
3740 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3741 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3742 *
3743 * We'll switch to look at two BPF maps, one for sys_enter and the
3744 * other for sys_exit when we start augmenting the sys_exit paths with
3745 * buffers that are being copied from kernel to userspace, think 'read'
3746 * syscall.
3747 */
3748 if (trace.syscalls.events.augmented) {
3749 evsel = trace.syscalls.events.augmented;
3750
3751 if (perf_evsel__init_augmented_syscall_tp(evsel) ||
3752 perf_evsel__init_augmented_syscall_tp_args(evsel))
3753 goto out;
3754 evsel->handler = trace__sys_enter;
3755
3756 evlist__for_each_entry(trace.evlist, evsel) {
Arnaldo Carvalho de Melocd26ea62018-11-03 08:19:56 -03003757 bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0;
3758
3759 if (raw_syscalls_sys_exit) {
3760 trace.raw_augmented_syscalls = true;
3761 goto init_augmented_syscall_tp;
3762 }
3763
3764 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
3765init_augmented_syscall_tp:
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003766 perf_evsel__init_augmented_syscall_tp(evsel);
3767 perf_evsel__init_augmented_syscall_tp_ret(evsel);
3768 evsel->handler = trace__sys_exit;
3769 }
3770 }
3771 }
3772
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003773 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3774 return trace__record(&trace, argc-1, &argv[1]);
3775
3776 /* summary_only implies summary option, but don't overwrite summary if set */
3777 if (trace.summary_only)
3778 trace.summary = trace.summary_only;
3779
Arnaldo Carvalho de Melo726f3232015-02-06 10:16:45 +01003780 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3781 trace.evlist->nr_entries == 0 /* Was --events used? */) {
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003782 trace.trace_syscalls = true;
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003783 }
3784
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003785 if (output_name != NULL) {
3786 err = trace__open_output(&trace, output_name);
3787 if (err < 0) {
3788 perror("failed to create output file");
3789 goto out;
3790 }
3791 }
3792
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003793 err = target__validate(&trace.opts.target);
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003794 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003795 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003796 fprintf(trace.output, "%s", bf);
3797 goto out_close;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003798 }
3799
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003800 err = target__parse_uid(&trace.opts.target);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003801 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003802 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003803 fprintf(trace.output, "%s", bf);
3804 goto out_close;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003805 }
3806
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003807 if (!argc && target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +09003808 trace.opts.target.system_wide = true;
3809
David Ahern6810fc92013-08-28 22:29:52 -06003810 if (input_name)
3811 err = trace__replay(&trace);
3812 else
3813 err = trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003814
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003815out_close:
3816 if (output_name != NULL)
3817 fclose(trace.output);
3818out:
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003819 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003820}