blob: 589e0412652a5efe216706086e734d357c25552c [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 Melo514f1c62012-09-26 20:05:56 -030021#include "builtin.h"
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030022#include "util/cgroup.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030023#include "util/color.h"
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -030024#include "util/debug.h"
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +010025#include "util/env.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030026#include "util/event.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030027#include "util/evlist.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060028#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030029#include "util/machine.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030030#include "util/path.h"
David Ahern6810fc92013-08-28 22:29:52 -060031#include "util/session.h"
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -030032#include "util/thread.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060033#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -030034#include "util/strlist.h"
David Ahernbdc89662013-08-28 22:29:53 -060035#include "util/intlist.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030036#include "util/thread_map.h"
David Ahernbf2575c2013-10-08 21:26:53 -060037#include "util/stat.h"
Arnaldo Carvalho de Melofd5cead2017-03-14 16:19:30 -030038#include "trace/beauty/beauty.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010039#include "trace-event.h"
David Ahern9aca7f12013-12-04 19:41:39 -070040#include "util/parse-events.h"
Wang Nanba504232016-02-26 09:31:54 +000041#include "util/bpf-loader.h"
Milian Wolff566a0882016-04-08 13:34:15 +020042#include "callchain.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030043#include "print_binary.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030044#include "string2.h"
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030045#include "syscalltbl.h"
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -030046#include "rb_resort.h"
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030047
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030048#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030049#include <inttypes.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030050#include <poll.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030051#include <signal.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030052#include <stdlib.h>
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -030053#include <string.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020054#include <linux/err.h>
Arnaldo Carvalho de Melo997bba82016-03-30 19:43:32 -030055#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030056#include <linux/kernel.h>
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -030057#include <linux/random.h>
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -030058#include <linux/stringify.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030059#include <linux/time64.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -030060#include <fcntl.h>
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -030061
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030062#include "sane_ctype.h"
63
Arnaldo Carvalho de Meloc188e7a2015-05-14 17:39:03 -030064#ifndef O_CLOEXEC
65# define O_CLOEXEC 02000000
66#endif
67
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -030068#ifndef F_LINUX_SPECIFIC_BASE
69# define F_LINUX_SPECIFIC_BASE 1024
70#endif
71
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030072struct trace {
73 struct perf_tool tool;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030074 struct syscalltbl *sctbl;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030075 struct {
76 int max;
77 struct syscall *table;
78 struct {
79 struct perf_evsel *sys_enter,
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -030080 *sys_exit,
81 *augmented;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030082 } events;
83 } syscalls;
84 struct record_opts opts;
85 struct perf_evlist *evlist;
86 struct machine *host;
87 struct thread *current;
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -030088 struct cgroup *cgroup;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030089 u64 base_time;
90 FILE *output;
91 unsigned long nr_events;
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -030092 unsigned long nr_events_printed;
93 unsigned long max_events;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030094 struct strlist *ev_qualifier;
95 struct {
96 size_t nr;
97 int *entries;
98 } ev_qualifier_ids;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -030099 struct {
100 size_t nr;
101 pid_t *entries;
102 } filter_pids;
103 double duration_filter;
104 double runtime_ms;
105 struct {
106 u64 vfs_getname,
107 proc_getname;
108 } stats;
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -0300109 unsigned int max_stack;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -0300110 unsigned int min_stack;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300111 bool not_ev_qualifier;
112 bool live;
113 bool full_time;
114 bool sched;
115 bool multiple_threads;
116 bool summary;
117 bool summary_only;
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -0300118 bool failure_only;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300119 bool show_comm;
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -0300120 bool print_sample;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300121 bool show_tool_stats;
122 bool trace_syscalls;
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -0300123 bool kernel_syscallchains;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300124 bool force;
125 bool vfs_getname;
126 int trace_pgfaults;
127};
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300128
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300129struct tp_field {
130 int offset;
131 union {
132 u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
133 void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
134 };
135};
136
137#define TP_UINT_FIELD(bits) \
138static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
139{ \
David Ahern55d43bca2015-02-19 15:00:22 -0500140 u##bits value; \
141 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
142 return value; \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300143}
144
145TP_UINT_FIELD(8);
146TP_UINT_FIELD(16);
147TP_UINT_FIELD(32);
148TP_UINT_FIELD(64);
149
150#define TP_UINT_FIELD__SWAPPED(bits) \
151static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
152{ \
David Ahern55d43bca2015-02-19 15:00:22 -0500153 u##bits value; \
154 memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300155 return bswap_##bits(value);\
156}
157
158TP_UINT_FIELD__SWAPPED(16);
159TP_UINT_FIELD__SWAPPED(32);
160TP_UINT_FIELD__SWAPPED(64);
161
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300162static 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 -0300163{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300164 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300165
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300166 switch (size) {
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300167 case 1:
168 field->integer = tp_field__u8;
169 break;
170 case 2:
171 field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
172 break;
173 case 4:
174 field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
175 break;
176 case 8:
177 field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
178 break;
179 default:
180 return -1;
181 }
182
183 return 0;
184}
185
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400186static 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 -0300187{
188 return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
189}
190
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300191static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
192{
193 return sample->raw_data + field->offset;
194}
195
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300196static int __tp_field__init_ptr(struct tp_field *field, int offset)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300197{
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300198 field->offset = offset;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300199 field->pointer = tp_field__ptr;
200 return 0;
201}
202
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400203static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
Arnaldo Carvalho de Meloaa823f52018-08-02 15:03:02 -0300204{
205 return __tp_field__init_ptr(field, format_field->offset);
206}
207
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300208struct syscall_tp {
209 struct tp_field id;
210 union {
211 struct tp_field args, ret;
212 };
213};
214
215static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel,
216 struct tp_field *field,
217 const char *name)
218{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400219 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300220
221 if (format_field == NULL)
222 return -1;
223
224 return tp_field__init_uint(field, format_field, evsel->needs_swap);
225}
226
227#define perf_evsel__init_sc_tp_uint_field(evsel, name) \
228 ({ struct syscall_tp *sc = evsel->priv;\
229 perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); })
230
231static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
232 struct tp_field *field,
233 const char *name)
234{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400235 struct tep_format_field *format_field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300236
237 if (format_field == NULL)
238 return -1;
239
240 return tp_field__init_ptr(field, format_field);
241}
242
243#define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
244 ({ struct syscall_tp *sc = evsel->priv;\
245 perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
246
247static void perf_evsel__delete_priv(struct perf_evsel *evsel)
248{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300249 zfree(&evsel->priv);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300250 perf_evsel__delete(evsel);
251}
252
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -0300253static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel)
254{
255 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
256
257 if (evsel->priv != NULL) {
258 if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr"))
259 goto out_delete;
260 return 0;
261 }
262
263 return -ENOMEM;
264out_delete:
265 zfree(&evsel->priv);
266 return -ENOENT;
267}
268
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -0300269static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel)
270{
271 struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp));
272
273 if (evsel->priv != NULL) { /* field, sizeof_field, offsetof_field */
274 if (__tp_field__init_uint(&sc->id, sizeof(long), sizeof(long long), evsel->needs_swap))
275 goto out_delete;
276
277 return 0;
278 }
279
280 return -ENOMEM;
281out_delete:
282 zfree(&evsel->priv);
283 return -EINVAL;
284}
285
286static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel)
287{
288 struct syscall_tp *sc = evsel->priv;
289
290 return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
291}
292
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -0300293static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel)
294{
295 struct syscall_tp *sc = evsel->priv;
296
297 return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
298}
299
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300300static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler)
Namhyung Kim96695d42013-11-12 08:51:45 -0300301{
302 evsel->priv = malloc(sizeof(struct syscall_tp));
303 if (evsel->priv != NULL) {
304 if (perf_evsel__init_sc_tp_uint_field(evsel, id))
305 goto out_delete;
306
307 evsel->handler = handler;
308 return 0;
309 }
310
311 return -ENOMEM;
312
313out_delete:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300314 zfree(&evsel->priv);
Namhyung Kim96695d42013-11-12 08:51:45 -0300315 return -ENOENT;
316}
317
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300318static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300319{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300320 struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction);
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300321
David Ahern9aca7f12013-12-04 19:41:39 -0700322 /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200323 if (IS_ERR(evsel))
David Ahern9aca7f12013-12-04 19:41:39 -0700324 evsel = perf_evsel__newtp("syscalls", direction);
325
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200326 if (IS_ERR(evsel))
327 return NULL;
328
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -0300329 if (perf_evsel__init_raw_syscall_tp(evsel, handler))
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200330 goto out_delete;
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -0300331
332 return evsel;
333
334out_delete:
335 perf_evsel__delete_priv(evsel);
336 return NULL;
337}
338
339#define perf_evsel__sc_tp_uint(evsel, name, sample) \
340 ({ struct syscall_tp *fields = evsel->priv; \
341 fields->name.integer(&fields->name, sample); })
342
343#define perf_evsel__sc_tp_ptr(evsel, name, sample) \
344 ({ struct syscall_tp *fields = evsel->priv; \
345 fields->name.pointer(&fields->name, sample); })
346
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300347size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, int val)
348{
349 int idx = val - sa->offset;
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300350
Arnaldo Carvalho de Melobc972ad2018-07-26 15:30:33 -0300351 if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL)
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300352 return scnprintf(bf, size, intfmt, val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300353
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300354 return scnprintf(bf, size, "%s", sa->entries[idx]);
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300355}
356
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300357static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
358 const char *intfmt,
359 struct syscall_arg *arg)
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300360{
Arnaldo Carvalho de Melo0ae79632017-07-17 10:31:42 -0300361 return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->val);
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300362}
363
Arnaldo Carvalho de Melo975b7c22013-10-08 17:17:43 -0300364static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
365 struct syscall_arg *arg)
366{
367 return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
368}
369
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300370#define SCA_STRARRAY syscall_arg__scnprintf_strarray
371
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300372struct strarrays {
373 int nr_entries;
374 struct strarray **entries;
375};
376
377#define DEFINE_STRARRAYS(array) struct strarrays strarrays__##array = { \
378 .nr_entries = ARRAY_SIZE(array), \
379 .entries = array, \
380}
381
Arnaldo Carvalho de Melo274e86f2017-07-14 09:38:38 -0300382size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
383 struct syscall_arg *arg)
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300384{
385 struct strarrays *sas = arg->parm;
386 int i;
387
388 for (i = 0; i < sas->nr_entries; ++i) {
389 struct strarray *sa = sas->entries[i];
390 int idx = arg->val - sa->offset;
391
392 if (idx >= 0 && idx < sa->nr_entries) {
393 if (sa->entries[idx] == NULL)
394 break;
395 return scnprintf(bf, size, "%s", sa->entries[idx]);
396 }
397 }
398
399 return scnprintf(bf, size, "%d", arg->val);
400}
401
Arnaldo Carvalho de Melo48e1f912016-07-05 14:43:27 -0300402#ifndef AT_FDCWD
403#define AT_FDCWD -100
404#endif
405
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300406static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
407 struct syscall_arg *arg)
408{
409 int fd = arg->val;
410
411 if (fd == AT_FDCWD)
412 return scnprintf(bf, size, "CWD");
413
414 return syscall_arg__scnprintf_fd(bf, size, arg);
415}
416
417#define SCA_FDAT syscall_arg__scnprintf_fd_at
418
419static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
420 struct syscall_arg *arg);
421
422#define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
423
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300424size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300425{
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -0300426 return scnprintf(bf, size, "%#lx", arg->val);
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -0300427}
428
Arnaldo Carvalho de Melo2c2b1622017-07-14 10:19:18 -0300429size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Meloa1c25522015-06-11 22:47:54 -0300430{
431 return scnprintf(bf, size, "%d", arg->val);
432}
433
Arnaldo Carvalho de Melo5dde91e2017-07-14 10:34:16 -0300434size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
435{
436 return scnprintf(bf, size, "%ld", arg->val);
437}
438
Arnaldo Carvalho de Melo729a7842015-10-29 11:48:18 -0300439static const char *bpf_cmd[] = {
440 "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
441 "MAP_GET_NEXT_KEY", "PROG_LOAD",
442};
443static DEFINE_STRARRAY(bpf_cmd);
444
Arnaldo Carvalho de Melo03e3adc2013-10-08 16:00:21 -0300445static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
446static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1);
Arnaldo Carvalho de Meloeac032c2013-09-20 11:27:32 -0300447
Arnaldo Carvalho de Melo1f115cb2013-09-03 15:50:28 -0300448static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
449static DEFINE_STRARRAY(itimers);
450
Arnaldo Carvalho de Melob62bee12015-08-11 11:05:36 -0300451static const char *keyctl_options[] = {
452 "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
453 "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
454 "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
455 "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
456 "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
457};
458static DEFINE_STRARRAY(keyctl_options);
459
Arnaldo Carvalho de Meloefe6b882013-09-03 16:15:12 -0300460static const char *whences[] = { "SET", "CUR", "END",
461#ifdef SEEK_DATA
462"DATA",
463#endif
464#ifdef SEEK_HOLE
465"HOLE",
466#endif
467};
468static DEFINE_STRARRAY(whences);
Arnaldo Carvalho de Melof9da0b02013-09-02 13:46:44 -0300469
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300470static const char *fcntl_cmds[] = {
471 "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
Arnaldo Carvalho de Meloe000e5e2017-07-13 13:07:00 -0300472 "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
473 "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
474 "GETOWNER_UIDS",
Arnaldo Carvalho de Melo80f587d2013-09-03 16:28:58 -0300475};
476static DEFINE_STRARRAY(fcntl_cmds);
477
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300478static const char *fcntl_linux_specific_cmds[] = {
479 "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
480 "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
Arnaldo Carvalho de Melo64e45612017-07-13 17:34:46 -0300481 "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
Arnaldo Carvalho de Melo83a51692017-07-13 17:14:11 -0300482};
483
484static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, F_LINUX_SPECIFIC_BASE);
485
486static struct strarray *fcntl_cmds_arrays[] = {
487 &strarray__fcntl_cmds,
488 &strarray__fcntl_linux_specific_cmds,
489};
490
491static DEFINE_STRARRAYS(fcntl_cmds_arrays);
492
Arnaldo Carvalho de Meloc045bf02013-09-04 11:52:33 -0300493static const char *rlimit_resources[] = {
494 "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
495 "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
496 "RTTIME",
497};
498static DEFINE_STRARRAY(rlimit_resources);
499
Arnaldo Carvalho de Meloeb5b1b12013-09-03 16:37:46 -0300500static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
501static DEFINE_STRARRAY(sighow);
502
David Ahern4f8c1b72013-09-22 19:45:00 -0600503static const char *clockid[] = {
504 "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
Arnaldo Carvalho de Melo28ebb872015-08-11 10:38:38 -0300505 "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
506 "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
David Ahern4f8c1b72013-09-22 19:45:00 -0600507};
508static DEFINE_STRARRAY(clockid);
509
Arnaldo Carvalho de Melo51108992013-09-04 11:42:27 -0300510static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
511 struct syscall_arg *arg)
512{
513 size_t printed = 0;
514 int mode = arg->val;
515
516 if (mode == F_OK) /* 0 */
517 return scnprintf(bf, size, "F");
518#define P_MODE(n) \
519 if (mode & n##_OK) { \
520 printed += scnprintf(bf + printed, size - printed, "%s", #n); \
521 mode &= ~n##_OK; \
522 }
523
524 P_MODE(R);
525 P_MODE(W);
526 P_MODE(X);
527#undef P_MODE
528
529 if (mode)
530 printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
531
532 return printed;
533}
534
535#define SCA_ACCMODE syscall_arg__scnprintf_access_mode
536
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300537static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
538 struct syscall_arg *arg);
539
540#define SCA_FILENAME syscall_arg__scnprintf_filename
541
Arnaldo Carvalho de Melo46cce192013-09-23 12:52:04 -0300542static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
543 struct syscall_arg *arg)
544{
545 int printed = 0, flags = arg->val;
546
547#define P_FLAG(n) \
548 if (flags & O_##n) { \
549 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
550 flags &= ~O_##n; \
551 }
552
553 P_FLAG(CLOEXEC);
554 P_FLAG(NONBLOCK);
555#undef P_FLAG
556
557 if (flags)
558 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
559
560 return printed;
561}
562
563#define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
564
Arnaldo Carvalho de Meloa355a612016-04-13 11:55:18 -0300565#ifndef GRND_NONBLOCK
566#define GRND_NONBLOCK 0x0001
567#endif
568#ifndef GRND_RANDOM
569#define GRND_RANDOM 0x0002
570#endif
571
Arnaldo Carvalho de Melo39878d42016-03-30 20:02:15 -0300572static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
573 struct syscall_arg *arg)
574{
575 int printed = 0, flags = arg->val;
576
577#define P_FLAG(n) \
578 if (flags & GRND_##n) { \
579 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \
580 flags &= ~GRND_##n; \
581 }
582
583 P_FLAG(RANDOM);
584 P_FLAG(NONBLOCK);
585#undef P_FLAG
586
587 if (flags)
588 printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
589
590 return printed;
591}
592
593#define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
594
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300595#define STRARRAY(name, array) \
596 { .scnprintf = SCA_STRARRAY, \
597 .parm = &strarray__##array, }
Arnaldo Carvalho de Melo453350d2013-09-20 12:13:37 -0300598
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +0100599#include "trace/beauty/arch_errno_names.c"
Arnaldo Carvalho de Meloea8dc3c2016-04-13 12:10:19 -0300600#include "trace/beauty/eventfd.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300601#include "trace/beauty/futex_op.c"
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300602#include "trace/beauty/futex_val3.c"
Arnaldo Carvalho de Melodf4cb162016-04-13 12:05:44 -0300603#include "trace/beauty/mmap.c"
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -0300604#include "trace/beauty/mode_t.c"
Arnaldo Carvalho de Meloa30e6252016-04-27 19:01:52 -0300605#include "trace/beauty/msg_flags.c"
Arnaldo Carvalho de Melo8f48df62016-05-06 10:02:32 -0300606#include "trace/beauty/open_flags.c"
Arnaldo Carvalho de Melo62de3442016-04-26 11:03:03 -0300607#include "trace/beauty/perf_event_open.c"
Arnaldo Carvalho de Melod5d71e82016-05-06 12:45:25 -0300608#include "trace/beauty/pid.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300609#include "trace/beauty/sched_policy.c"
Arnaldo Carvalho de Melof5cd95e2016-05-11 10:32:20 -0300610#include "trace/beauty/seccomp.c"
Arnaldo Carvalho de Melo12199d82016-05-06 09:58:02 -0300611#include "trace/beauty/signum.c"
Arnaldo Carvalho de Melobbf86c42016-04-14 13:53:10 -0300612#include "trace/beauty/socket_type.c"
Arnaldo Carvalho de Melo7206b902016-04-06 14:11:36 -0300613#include "trace/beauty/waitid_options.c"
Arnaldo Carvalho de Meloa3bca912016-04-06 12:51:33 -0300614
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300615struct syscall_arg_fmt {
616 size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
617 void *parm;
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -0300618 const char *name;
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -0300619 bool show_zero;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300620};
621
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300622static struct syscall_fmt {
623 const char *name;
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -0300624 const char *alias;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300625 struct syscall_arg_fmt arg[6];
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -0300626 u8 nr_args;
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300627 bool errpid;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300628 bool timeout;
Arnaldo Carvalho de Melo04b34722013-08-26 11:36:30 -0300629 bool hexret;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300630} syscall_fmts[] = {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300631 { .name = "access",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300632 .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
Arnaldo Carvalho de Melo02ef2882018-08-28 17:03:53 -0300633 { .name = "bind",
634 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300635 { .name = "bpf",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300636 .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300637 { .name = "brk", .hexret = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300638 .arg = { [0] = { .scnprintf = SCA_HEX, /* brk */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300639 { .name = "clock_gettime",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300640 .arg = { [0] = STRARRAY(clk_id, clockid), }, },
Arnaldo Carvalho de Melo33396a32017-07-19 16:15:17 -0300641 { .name = "clone", .errpid = true, .nr_args = 5,
642 .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
643 [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
644 [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
645 [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
646 [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300647 { .name = "close",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300648 .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
Arnaldo Carvalho de Melod5a7e662018-08-28 16:24:44 -0300649 { .name = "connect",
650 .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300651 { .name = "epoll_ctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300652 .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300653 { .name = "eventfd2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300654 .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300655 { .name = "fchmodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300656 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300657 { .name = "fchownat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300658 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300659 { .name = "fcntl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300660 .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
Arnaldo Carvalho de Melo39cc3552017-07-17 16:02:52 -0300661 .parm = &strarrays__fcntl_cmds_arrays,
662 .show_zero = true, },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300663 [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300664 { .name = "flock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300665 .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300666 { .name = "fstat", .alias = "newfstat", },
667 { .name = "fstatat", .alias = "newfstatat", },
668 { .name = "futex",
Arnaldo Carvalho de Melo3258abe2018-01-22 12:56:59 -0300669 .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
670 [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300671 { .name = "futimesat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300672 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300673 { .name = "getitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300674 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300675 { .name = "getpid", .errpid = true, },
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -0300676 { .name = "getpgid", .errpid = true, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300677 { .name = "getppid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300678 { .name = "getrandom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300679 .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300680 { .name = "getrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300681 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo2d1073d2018-01-09 12:03:47 -0300682 { .name = "gettid", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300683 { .name = "ioctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300684 .arg = {
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300685#if defined(__i386__) || defined(__x86_64__)
686/*
687 * FIXME: Make this available to all arches.
688 */
Arnaldo Carvalho de Melo1cc47f2d2017-07-31 13:20:14 -0300689 [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300690 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300691#else
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300692 [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
Arnaldo Carvalho de Melo844ae5b2014-02-10 14:09:48 -0300693#endif
Arnaldo Carvalho de Melo1de30382017-10-31 11:32:23 -0300694 { .name = "kcmp", .nr_args = 5,
695 .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
696 [1] = { .name = "pid2", .scnprintf = SCA_PID, },
697 [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
698 [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
699 [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300700 { .name = "keyctl",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300701 .arg = { [0] = STRARRAY(option, keyctl_options), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300702 { .name = "kill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300703 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300704 { .name = "linkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300705 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300706 { .name = "lseek",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300707 .arg = { [2] = STRARRAY(whence, whences), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300708 { .name = "lstat", .alias = "newlstat", },
709 { .name = "madvise",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300710 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
711 [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300712 { .name = "mkdirat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300713 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300714 { .name = "mknodat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300715 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300716 { .name = "mlock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300717 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300718 { .name = "mlockall",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300719 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melobeccb2b2013-08-26 12:29:38 -0300720 { .name = "mmap", .hexret = true,
Jiri Olsa54265662017-05-31 13:35:57 +0200721/* The standard mmap maps to old_mmap on s390x */
722#if defined(__s390x__)
723 .alias = "old_mmap",
724#endif
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300725 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
726 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
727 [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300728 { .name = "mprotect",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300729 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
730 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300731 { .name = "mq_unlink",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300732 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, },
Arnaldo Carvalho de Meloae685382013-08-20 17:44:42 -0300733 { .name = "mremap", .hexret = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300734 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ },
735 [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ },
736 [4] = { .scnprintf = SCA_HEX, /* new_addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300737 { .name = "munlock",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300738 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300739 { .name = "munmap",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300740 .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300741 { .name = "name_to_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300742 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300743 { .name = "newfstatat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300744 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300745 { .name = "open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300746 .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300747 { .name = "open_by_handle_at",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300748 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
749 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300750 { .name = "openat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300751 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
752 [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300753 { .name = "perf_event_open",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300754 .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ },
755 [3] = { .scnprintf = SCA_FD, /* group_fd */ },
756 [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300757 { .name = "pipe2",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300758 .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo83bc9c372017-08-28 11:47:11 -0300759 { .name = "pkey_alloc",
760 .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
761 { .name = "pkey_free",
762 .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
763 { .name = "pkey_mprotect",
764 .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
765 [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ },
766 [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300767 { .name = "poll", .timeout = true, },
768 { .name = "ppoll", .timeout = true, },
Arnaldo Carvalho de Melod688d032017-10-26 15:19:35 -0300769 { .name = "prctl", .alias = "arch_prctl",
770 .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ },
771 [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
772 [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300773 { .name = "pread", .alias = "pread64", },
774 { .name = "preadv", .alias = "pread", },
775 { .name = "prlimit64",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300776 .arg = { [1] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300777 { .name = "pwrite", .alias = "pwrite64", },
778 { .name = "readlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300779 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300780 { .name = "recvfrom",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300781 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300782 { .name = "recvmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300783 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300784 { .name = "recvmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300785 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300786 { .name = "renameat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300787 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300788 { .name = "rt_sigaction",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300789 .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300790 { .name = "rt_sigprocmask",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300791 .arg = { [0] = STRARRAY(how, sighow), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300792 { .name = "rt_sigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300793 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300794 { .name = "rt_tgsigqueueinfo",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300795 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300796 { .name = "sched_setscheduler",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300797 .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300798 { .name = "seccomp",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300799 .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
800 [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300801 { .name = "select", .timeout = true, },
802 { .name = "sendmmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300803 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300804 { .name = "sendmsg",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300805 .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300806 { .name = "sendto",
Arnaldo Carvalho de Melo6ebb6862018-08-29 08:47:08 -0300807 .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
808 [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, },
Arnaldo Carvalho de Meloc65f1072016-04-06 14:55:18 -0300809 { .name = "set_tid_address", .errpid = true, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300810 { .name = "setitimer",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300811 .arg = { [0] = STRARRAY(which, itimers), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300812 { .name = "setrlimit",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300813 .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300814 { .name = "socket",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300815 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300816 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
817 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300818 { .name = "socketpair",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300819 .arg = { [0] = STRARRAY(family, socket_families),
Arnaldo Carvalho de Melo162d3ed2018-07-26 09:26:13 -0300820 [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
821 [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300822 { .name = "stat", .alias = "newstat", },
823 { .name = "statx",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300824 .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
825 [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } ,
826 [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300827 { .name = "swapoff",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300828 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300829 { .name = "swapon",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300830 .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300831 { .name = "symlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300832 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300833 { .name = "tgkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300834 .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300835 { .name = "tkill",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300836 .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
Benjamin Peterson3de3e8b2018-08-27 20:53:44 -0700837 { .name = "umount2", .alias = "umount", },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300838 { .name = "uname", .alias = "newuname", },
839 { .name = "unlinkat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300840 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -0300841 { .name = "utimensat",
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300842 .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300843 { .name = "wait4", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300844 .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -0300845 { .name = "waitid", .errpid = true,
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300846 .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300847};
848
849static int syscall_fmt__cmp(const void *name, const void *fmtp)
850{
851 const struct syscall_fmt *fmt = fmtp;
852 return strcmp(name, fmt->name);
853}
854
855static struct syscall_fmt *syscall_fmt__find(const char *name)
856{
857 const int nmemb = ARRAY_SIZE(syscall_fmts);
858 return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
859}
860
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300861/*
862 * is_exit: is this "exit" or "exit_group"?
863 * 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 -0300864 * 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 -0300865 */
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300866struct syscall {
Tzvetomir Stoyanov (VMware)4963b0f2018-09-19 14:56:44 -0400867 struct tep_event_format *tp_format;
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -0300868 int nr_args;
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -0300869 int args_size;
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -0300870 bool is_exit;
871 bool is_open;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400872 struct tep_format_field *args;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300873 const char *name;
874 struct syscall_fmt *fmt;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -0300875 struct syscall_arg_fmt *arg_fmt;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -0300876};
877
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300878/*
879 * We need to have this 'calculated' boolean because in some cases we really
880 * don't know what is the duration of a syscall, for instance, when we start
881 * a session and some threads are waiting for a syscall to finish, say 'poll',
882 * in which case all we can do is to print "( ? ) for duration and for the
883 * start timestamp.
884 */
885static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200886{
887 double duration = (double)t / NSEC_PER_MSEC;
888 size_t printed = fprintf(fp, "(");
889
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300890 if (!calculated)
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -0300891 printed += fprintf(fp, " ");
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -0300892 else if (duration >= 1.0)
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200893 printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
894 else if (duration >= 0.01)
895 printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
896 else
897 printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300898 return printed + fprintf(fp, "): ");
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -0200899}
900
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300901/**
902 * filename.ptr: The filename char pointer that will be vfs_getname'd
903 * filename.entry_str_pos: Where to insert the string translated from
904 * filename.ptr by the vfs_getname tracepoint/kprobe.
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300905 * ret_scnprintf: syscall args may set this to a different syscall return
906 * formatter, for instance, fcntl may return fds, file flags, etc.
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300907 */
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300908struct thread_trace {
909 u64 entry_time;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300910 bool entry_pending;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300911 unsigned long nr_events;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +0400912 unsigned long pfmaj, pfmin;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300913 char *entry_str;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -0300914 double runtime_ms;
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -0300915 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300916 struct {
917 unsigned long ptr;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -0300918 short int entry_str_pos;
919 bool pending_open;
920 unsigned int namelen;
921 char *name;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -0300922 } filename;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300923 struct {
924 int max;
925 char **table;
926 } paths;
David Ahernbf2575c2013-10-08 21:26:53 -0600927
928 struct intlist *syscall_stats;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300929};
930
931static struct thread_trace *thread_trace__new(void)
932{
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300933 struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
934
935 if (ttrace)
936 ttrace->paths.max = -1;
937
David Ahernbf2575c2013-10-08 21:26:53 -0600938 ttrace->syscall_stats = intlist__new(NULL);
939
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300940 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300941}
942
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300943static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300944{
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300945 struct thread_trace *ttrace;
946
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300947 if (thread == NULL)
948 goto fail;
949
Namhyung Kim89dceb22014-10-06 09:46:03 +0900950 if (thread__priv(thread) == NULL)
951 thread__set_priv(thread, thread_trace__new());
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300952
Namhyung Kim89dceb22014-10-06 09:46:03 +0900953 if (thread__priv(thread) == NULL)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300954 goto fail;
955
Namhyung Kim89dceb22014-10-06 09:46:03 +0900956 ttrace = thread__priv(thread);
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -0300957 ++ttrace->nr_events;
958
959 return ttrace;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300960fail:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -0300961 color_fprintf(fp, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -0300962 "WARNING: not enough memory, dropping samples!\n");
963 return NULL;
964}
965
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300966
967void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -0300968 size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -0300969{
970 struct thread_trace *ttrace = thread__priv(arg->thread);
971
972 ttrace->ret_scnprintf = ret_scnprintf;
973}
974
Stanislav Fomichev598d02c2014-06-26 20:14:25 +0400975#define TRACE_PFMAJ (1 << 0)
976#define TRACE_PFMIN (1 << 1)
977
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -0300978static const size_t trace__entry_str_size = 2048;
979
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -0300980static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300981{
Namhyung Kim89dceb22014-10-06 09:46:03 +0900982 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -0300983
984 if (fd > ttrace->paths.max) {
985 char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
986
987 if (npath == NULL)
988 return -1;
989
990 if (ttrace->paths.max != -1) {
991 memset(npath + ttrace->paths.max + 1, 0,
992 (fd - ttrace->paths.max) * sizeof(char *));
993 } else {
994 memset(npath, 0, (fd + 1) * sizeof(char *));
995 }
996
997 ttrace->paths.table = npath;
998 ttrace->paths.max = fd;
999 }
1000
1001 ttrace->paths.table[fd] = strdup(pathname);
1002
1003 return ttrace->paths.table[fd] != NULL ? 0 : -1;
1004}
1005
Arnaldo Carvalho de Melo97119f32013-09-27 17:34:10 -03001006static int thread__read_fd_path(struct thread *thread, int fd)
1007{
1008 char linkname[PATH_MAX], pathname[PATH_MAX];
1009 struct stat st;
1010 int ret;
1011
1012 if (thread->pid_ == thread->tid) {
1013 scnprintf(linkname, sizeof(linkname),
1014 "/proc/%d/fd/%d", thread->pid_, fd);
1015 } else {
1016 scnprintf(linkname, sizeof(linkname),
1017 "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd);
1018 }
1019
1020 if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
1021 return -1;
1022
1023 ret = readlink(linkname, pathname, sizeof(pathname));
1024
1025 if (ret < 0 || ret > st.st_size)
1026 return -1;
1027
1028 pathname[ret] = '\0';
1029 return trace__set_fd_pathname(thread, fd, pathname);
1030}
1031
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001032static const char *thread__fd_path(struct thread *thread, int fd,
1033 struct trace *trace)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001034{
Namhyung Kim89dceb22014-10-06 09:46:03 +09001035 struct thread_trace *ttrace = thread__priv(thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001036
1037 if (ttrace == NULL)
1038 return NULL;
1039
1040 if (fd < 0)
1041 return NULL;
1042
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001043 if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001044 if (!trace->live)
1045 return NULL;
1046 ++trace->stats.proc_getname;
Arnaldo Carvalho de Melocdcd1e62014-06-10 16:00:18 -03001047 if (thread__read_fd_path(thread, fd))
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001048 return NULL;
1049 }
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001050
1051 return ttrace->paths.table[fd];
1052}
1053
Arnaldo Carvalho de Melofc65eb82017-07-14 15:21:40 -03001054size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001055{
1056 int fd = arg->val;
1057 size_t printed = scnprintf(bf, size, "%d", fd);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001058 const char *path = thread__fd_path(arg->thread, fd, arg->trace);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001059
1060 if (path)
1061 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1062
1063 return printed;
1064}
1065
Arnaldo Carvalho de Melo0a2f75402017-10-31 11:30:09 -03001066size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
1067{
1068 size_t printed = scnprintf(bf, size, "%d", fd);
1069 struct thread *thread = machine__find_thread(trace->host, pid, pid);
1070
1071 if (thread) {
1072 const char *path = thread__fd_path(thread, fd, trace);
1073
1074 if (path)
1075 printed += scnprintf(bf + printed, size - printed, "<%s>", path);
1076
1077 thread__put(thread);
1078 }
1079
1080 return printed;
1081}
1082
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001083static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
1084 struct syscall_arg *arg)
1085{
1086 int fd = arg->val;
1087 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
Namhyung Kim89dceb22014-10-06 09:46:03 +09001088 struct thread_trace *ttrace = thread__priv(arg->thread);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001089
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001090 if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
1091 zfree(&ttrace->paths.table[fd]);
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03001092
1093 return printed;
1094}
1095
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001096static void thread__set_filename_pos(struct thread *thread, const char *bf,
1097 unsigned long ptr)
1098{
1099 struct thread_trace *ttrace = thread__priv(thread);
1100
1101 ttrace->filename.ptr = ptr;
1102 ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
1103}
1104
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001105static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
1106{
1107 struct augmented_arg *augmented_arg = arg->augmented.args;
1108
1109 return scnprintf(bf, size, "%.*s", augmented_arg->size, augmented_arg->value);
1110}
1111
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001112static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
1113 struct syscall_arg *arg)
1114{
1115 unsigned long ptr = arg->val;
1116
Arnaldo Carvalho de Melo75d1e302018-08-21 12:00:39 -03001117 if (arg->augmented.args)
1118 return syscall_arg__scnprintf_augmented_string(arg, bf, size);
1119
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001120 if (!arg->trace->vfs_getname)
1121 return scnprintf(bf, size, "%#x", ptr);
1122
1123 thread__set_filename_pos(arg->thread, bf, ptr);
1124 return 0;
1125}
1126
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001127static bool trace__filter_duration(struct trace *trace, double t)
1128{
1129 return t < (trace->duration_filter * NSEC_PER_MSEC);
1130}
1131
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001132static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001133{
1134 double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
1135
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001136 return fprintf(fp, "%10.3f ", ts);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001137}
1138
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001139/*
1140 * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
1141 * using ttrace->entry_time for a thread that receives a sys_exit without
1142 * first having received a sys_enter ("poll" issued before tracing session
1143 * starts, lost sys_enter exit due to ring buffer overflow).
1144 */
1145static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
1146{
1147 if (tstamp > 0)
1148 return __trace__fprintf_tstamp(trace, tstamp, fp);
1149
1150 return fprintf(fp, " ? ");
1151}
1152
Namhyung Kimf15eb532012-10-05 14:02:16 +09001153static bool done = false;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001154static bool interrupted = false;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001155
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001156static void sig_handler(int sig)
Namhyung Kimf15eb532012-10-05 14:02:16 +09001157{
1158 done = true;
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03001159 interrupted = sig == SIGINT;
Namhyung Kimf15eb532012-10-05 14:02:16 +09001160}
1161
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001162static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001163{
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001164 size_t printed = 0;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001165
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001166 if (trace->multiple_threads) {
1167 if (trace->show_comm)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02001168 printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
Adrian Hunter38051232013-07-04 16:20:31 +03001169 printed += fprintf(fp, "%d ", thread->tid);
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03001170 }
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001171
1172 return printed;
1173}
1174
Arnaldo Carvalho de Melo6dcbd212018-08-21 11:40:09 -03001175static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
1176 u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
1177{
1178 size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);
1179 printed += fprintf_duration(duration, duration_calculated, fp);
1180 return printed + trace__fprintf_comm_tid(trace, thread, fp);
1181}
1182
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001183static int trace__process_event(struct trace *trace, struct machine *machine,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001184 union perf_event *event, struct perf_sample *sample)
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001185{
1186 int ret = 0;
1187
1188 switch (event->header.type) {
1189 case PERF_RECORD_LOST:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001190 color_fprintf(trace->output, PERF_COLOR_RED,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001191 "LOST %" PRIu64 " events!\n", event->lost.lost);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001192 ret = machine__process_lost_event(machine, event, sample);
Arnaldo Carvalho de Melo3ed5ca22016-03-30 16:51:17 -03001193 break;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001194 default:
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001195 ret = machine__process_event(machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001196 break;
1197 }
1198
1199 return ret;
1200}
1201
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001202static int trace__tool_process(struct perf_tool *tool,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001203 union perf_event *event,
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001204 struct perf_sample *sample,
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001205 struct machine *machine)
1206{
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001207 struct trace *trace = container_of(tool, struct trace, tool);
Frederic Weisbecker162f0be2013-09-11 16:18:24 +02001208 return trace__process_event(trace, machine, event, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001209}
1210
Arnaldo Carvalho de Melocaf8a0d2016-05-17 11:56:24 -03001211static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
1212{
1213 struct machine *machine = vmachine;
1214
1215 if (machine->kptr_restrict_warned)
1216 return NULL;
1217
1218 if (symbol_conf.kptr_restrict) {
1219 pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
1220 "Check /proc/sys/kernel/kptr_restrict.\n\n"
1221 "Kernel samples will not be resolved.\n");
1222 machine->kptr_restrict_warned = true;
1223 return NULL;
1224 }
1225
1226 return machine__resolve_kernel_addr(vmachine, addrp, modp);
1227}
1228
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001229static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
1230{
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001231 int err = symbol__init(NULL);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001232
1233 if (err)
1234 return err;
1235
David Ahern8fb598e2013-09-28 13:13:00 -06001236 trace->host = machine__new_host();
1237 if (trace->host == NULL)
1238 return -ENOMEM;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001239
Andrei Vagincbd5c172017-11-07 16:22:46 -08001240 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
1241 if (err < 0)
1242 goto out;
Arnaldo Carvalho de Melo706c3da2015-07-22 16:16:16 -03001243
Arnaldo Carvalho de Meloa33fbd52013-11-11 11:36:12 -03001244 err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
Kan Liang9d9cad72015-06-17 09:51:11 -04001245 evlist->threads, trace__tool_process, false,
Kan Liang340b47f2017-09-29 07:47:54 -07001246 trace->opts.proc_map_timeout, 1);
Andrei Vagincbd5c172017-11-07 16:22:46 -08001247out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001248 if (err)
1249 symbol__exit();
1250
1251 return err;
1252}
1253
Andrei Vagin33974a42017-11-07 16:22:45 -08001254static void trace__symbols__exit(struct trace *trace)
1255{
1256 machine__exit(trace->host);
1257 trace->host = NULL;
1258
1259 symbol__exit();
1260}
1261
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001262static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
1263{
1264 int idx;
1265
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001266 if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0)
1267 nr_args = sc->fmt->nr_args;
1268
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001269 sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
1270 if (sc->arg_fmt == NULL)
1271 return -1;
1272
1273 for (idx = 0; idx < nr_args; ++idx) {
1274 if (sc->fmt)
1275 sc->arg_fmt[idx] = sc->fmt->arg[idx];
1276 }
1277
1278 sc->nr_args = nr_args;
1279 return 0;
1280}
1281
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001282static int syscall__set_arg_fmts(struct syscall *sc)
1283{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001284 struct tep_format_field *field, *last_field = NULL;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001285 int idx = 0, len;
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001286
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001287 for (field = sc->args; field; field = field->next, ++idx) {
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001288 last_field = field;
1289
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001290 if (sc->fmt && sc->fmt->arg[idx].scnprintf)
1291 continue;
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001292
1293 if (strcmp(field->type, "const char *") == 0 &&
Arnaldo Carvalho de Melo12f3ca42016-05-23 16:37:55 -03001294 (strcmp(field->name, "filename") == 0 ||
1295 strcmp(field->name, "path") == 0 ||
1296 strcmp(field->name, "pathname") == 0))
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001297 sc->arg_fmt[idx].scnprintf = SCA_FILENAME;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001298 else if (field->flags & TEP_FIELD_IS_POINTER)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001299 sc->arg_fmt[idx].scnprintf = syscall_arg__scnprintf_hex;
Arnaldo Carvalho de Melod1d438a2016-04-06 18:02:41 -03001300 else if (strcmp(field->type, "pid_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001301 sc->arg_fmt[idx].scnprintf = SCA_PID;
Arnaldo Carvalho de Meloba2f22c2016-04-07 12:05:51 -03001302 else if (strcmp(field->type, "umode_t") == 0)
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001303 sc->arg_fmt[idx].scnprintf = SCA_MODE_T;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001304 else if ((strcmp(field->type, "int") == 0 ||
1305 strcmp(field->type, "unsigned int") == 0 ||
1306 strcmp(field->type, "long") == 0) &&
1307 (len = strlen(field->name)) >= 2 &&
1308 strcmp(field->name + len - 2, "fd") == 0) {
1309 /*
1310 * /sys/kernel/tracing/events/syscalls/sys_enter*
1311 * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
1312 * 65 int
1313 * 23 unsigned int
1314 * 7 unsigned long
1315 */
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001316 sc->arg_fmt[idx].scnprintf = SCA_FD;
Arnaldo Carvalho de Melob6565c92016-05-23 12:59:53 -03001317 }
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001318 }
1319
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001320 if (last_field)
1321 sc->args_size = last_field->offset + last_field->size;
1322
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001323 return 0;
1324}
1325
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001326static int trace__read_syscall_info(struct trace *trace, int id)
1327{
1328 char tp_name[128];
1329 struct syscall *sc;
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03001330 const char *name = syscalltbl__name(trace->sctbl, id);
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001331
1332 if (name == NULL)
1333 return -1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001334
1335 if (id > trace->syscalls.max) {
1336 struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
1337
1338 if (nsyscalls == NULL)
1339 return -1;
1340
1341 if (trace->syscalls.max != -1) {
1342 memset(nsyscalls + trace->syscalls.max + 1, 0,
1343 (id - trace->syscalls.max) * sizeof(*sc));
1344 } else {
1345 memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
1346 }
1347
1348 trace->syscalls.table = nsyscalls;
1349 trace->syscalls.max = id;
1350 }
1351
1352 sc = trace->syscalls.table + id;
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001353 sc->name = name;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001354
Arnaldo Carvalho de Melo3a531262012-10-20 12:39:03 -03001355 sc->fmt = syscall_fmt__find(sc->name);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001356
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001357 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
Jiri Olsa97978b32013-12-03 14:09:24 +01001358 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001359
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001360 if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001361 snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
Jiri Olsa97978b32013-12-03 14:09:24 +01001362 sc->tp_format = trace_event__tp_format("syscalls", tp_name);
Arnaldo Carvalho de Meloaec19302012-09-27 13:16:00 -03001363 }
1364
Arnaldo Carvalho de Melo5e58fcf2017-07-19 14:32:11 -03001365 if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields))
1366 return -1;
1367
Jiri Olsa8dd2a132015-09-07 10:38:06 +02001368 if (IS_ERR(sc->tp_format))
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001369 return -1;
1370
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001371 sc->args = sc->tp_format->format.fields;
Taeung Songc42de702016-02-26 22:14:25 +09001372 /*
1373 * We need to check and discard the first variable '__syscall_nr'
1374 * or 'nr' that mean the syscall number. It is needless here.
1375 * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
1376 */
1377 if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001378 sc->args = sc->args->next;
1379 --sc->nr_args;
1380 }
1381
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001382 sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001383 sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001384
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001385 return syscall__set_arg_fmts(sc);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001386}
1387
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001388static int trace__validate_ev_qualifier(struct trace *trace)
1389{
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001390 int err = 0, i;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001391 size_t nr_allocated;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001392 struct str_node *pos;
1393
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001394 trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier);
1395 trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr *
1396 sizeof(trace->ev_qualifier_ids.entries[0]));
1397
1398 if (trace->ev_qualifier_ids.entries == NULL) {
1399 fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
1400 trace->output);
1401 err = -EINVAL;
1402 goto out;
1403 }
1404
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001405 nr_allocated = trace->ev_qualifier_ids.nr;
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001406 i = 0;
1407
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001408 strlist__for_each_entry(pos, trace->ev_qualifier) {
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001409 const char *sc = pos->s;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001410 int id = syscalltbl__id(trace->sctbl, sc), match_next = -1;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001411
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001412 if (id < 0) {
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001413 id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next);
1414 if (id >= 0)
1415 goto matches;
1416
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001417 if (err == 0) {
1418 fputs("Error:\tInvalid syscall ", trace->output);
1419 err = -EINVAL;
1420 } else {
1421 fputs(", ", trace->output);
1422 }
1423
1424 fputs(sc, trace->output);
1425 }
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001426matches:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001427 trace->ev_qualifier_ids.entries[i++] = id;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001428 if (match_next == -1)
1429 continue;
1430
1431 while (1) {
1432 id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next);
1433 if (id < 0)
1434 break;
1435 if (nr_allocated == trace->ev_qualifier_ids.nr) {
1436 void *entries;
1437
1438 nr_allocated += 8;
1439 entries = realloc(trace->ev_qualifier_ids.entries,
1440 nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
1441 if (entries == NULL) {
1442 err = -ENOMEM;
1443 fputs("\nError:\t Not enough memory for parsing\n", trace->output);
1444 goto out_free;
1445 }
1446 trace->ev_qualifier_ids.entries = entries;
1447 }
1448 trace->ev_qualifier_ids.nr++;
1449 trace->ev_qualifier_ids.entries[i++] = id;
1450 }
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001451 }
1452
1453 if (err < 0) {
1454 fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'"
1455 "\nHint:\tand: 'man syscalls'\n", trace->output);
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03001456out_free:
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001457 zfree(&trace->ev_qualifier_ids.entries);
1458 trace->ev_qualifier_ids.nr = 0;
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001459 }
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03001460out:
Arnaldo Carvalho de Melod0cc4392015-06-25 19:32:33 -03001461 return err;
1462}
1463
David Ahern55d43bca2015-02-19 15:00:22 -05001464/*
1465 * args is to be interpreted as a series of longs but we need to handle
1466 * 8-byte unaligned accesses. args points to raw_data within the event
1467 * and raw_data is guaranteed to be 8-byte unaligned because it is
1468 * preceded by raw_size which is a u32. So we need to copy args to a temp
1469 * variable to read it. Most notably this avoids extended load instructions
1470 * on unaligned addresses
1471 */
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001472unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001473{
1474 unsigned long val;
Arnaldo Carvalho de Melo325f5092017-07-19 15:02:43 -03001475 unsigned char *p = arg->args + sizeof(unsigned long) * idx;
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001476
1477 memcpy(&val, p, sizeof(val));
1478 return val;
1479}
1480
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001481static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
1482 struct syscall_arg *arg)
1483{
1484 if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
1485 return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
1486
1487 return scnprintf(bf, size, "arg%d: ", arg->idx);
1488}
1489
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001490static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size,
1491 struct syscall_arg *arg, unsigned long val)
1492{
1493 if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) {
1494 arg->val = val;
1495 if (sc->arg_fmt[arg->idx].parm)
1496 arg->parm = sc->arg_fmt[arg->idx].parm;
1497 return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg);
1498 }
1499 return scnprintf(bf, size, "%ld", val);
1500}
1501
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001502static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001503 unsigned char *args, void *augmented_args, int augmented_args_size,
1504 struct trace *trace, struct thread *thread)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001505{
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001506 size_t printed = 0;
David Ahern55d43bca2015-02-19 15:00:22 -05001507 unsigned long val;
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001508 u8 bit = 1;
1509 struct syscall_arg arg = {
1510 .args = args,
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001511 .augmented = {
1512 .size = augmented_args_size,
1513 .args = augmented_args,
1514 },
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001515 .idx = 0,
1516 .mask = 0,
1517 .trace = trace,
1518 .thread = thread,
1519 };
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001520 struct thread_trace *ttrace = thread__priv(thread);
1521
1522 /*
1523 * Things like fcntl will set this in its 'cmd' formatter to pick the
1524 * right formatter for the return value (an fd? file flags?), which is
1525 * not needed for syscalls that always return a given type, say an fd.
1526 */
1527 ttrace->ret_scnprintf = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001528
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001529 if (sc->args != NULL) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001530 struct tep_format_field *field;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001531
Arnaldo Carvalho de Melof208bd82015-03-20 17:46:53 -03001532 for (field = sc->args; field;
Arnaldo Carvalho de Melo01533e92013-09-03 12:20:12 -03001533 field = field->next, ++arg.idx, bit <<= 1) {
1534 if (arg.mask & bit)
Arnaldo Carvalho de Melo6e7eeb52013-09-02 10:39:21 -03001535 continue;
David Ahern55d43bca2015-02-19 15:00:22 -05001536
Arnaldo Carvalho de Melof9f83b32017-07-14 10:13:56 -03001537 val = syscall_arg__val(&arg, arg.idx);
David Ahern55d43bca2015-02-19 15:00:22 -05001538
Arnaldo Carvalho de Melo4aa58232013-09-20 12:19:41 -03001539 /*
1540 * Suppress this argument if its value is zero and
1541 * and we don't have a string associated in an
1542 * strarray for it.
1543 */
David Ahern55d43bca2015-02-19 15:00:22 -05001544 if (val == 0 &&
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001545 !(sc->arg_fmt &&
Arnaldo Carvalho de Melod47737d2017-07-17 15:59:03 -03001546 (sc->arg_fmt[arg.idx].show_zero ||
1547 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY ||
Arnaldo Carvalho de Melo82d4a112017-07-17 15:22:47 -03001548 sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) &&
1549 sc->arg_fmt[arg.idx].parm))
Arnaldo Carvalho de Melo22ae5cf12013-09-12 11:27:34 -03001550 continue;
1551
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001552 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo13d4ff32013-08-23 18:14:48 -03001553 "%s%s: ", printed ? ", " : "", field->name);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001554 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001555 }
Arnaldo Carvalho de Melo4c4d6e52016-05-05 23:38:05 -03001556 } else if (IS_ERR(sc->tp_format)) {
1557 /*
1558 * If we managed to read the tracepoint /format file, then we
1559 * may end up not having any args, like with gettid(), so only
1560 * print the raw args when we didn't manage to read it.
1561 */
Arnaldo Carvalho de Melo332337d2017-07-19 15:08:14 -03001562 while (arg.idx < sc->nr_args) {
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001563 if (arg.mask & bit)
1564 goto next_arg;
1565 val = syscall_arg__val(&arg, arg.idx);
Arnaldo Carvalho de Meloc51bdfe2017-07-19 15:47:30 -03001566 if (printed)
1567 printed += scnprintf(bf + printed, size - printed, ", ");
1568 printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
Arnaldo Carvalho de Melod032d792017-07-19 14:36:13 -03001569 printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val);
1570next_arg:
1571 ++arg.idx;
1572 bit <<= 1;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03001573 }
1574 }
1575
1576 return printed;
1577}
1578
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001579typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001580 union perf_event *event,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001581 struct perf_sample *sample);
1582
1583static struct syscall *trace__syscall_info(struct trace *trace,
David Ahernbf2575c2013-10-08 21:26:53 -06001584 struct perf_evsel *evsel, int id)
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001585{
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001586
1587 if (id < 0) {
Arnaldo Carvalho de Meloadaa18b2013-08-22 17:55:25 -03001588
1589 /*
1590 * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
1591 * before that, leaving at a higher verbosity level till that is
1592 * explained. Reproduced with plain ftrace with:
1593 *
1594 * echo 1 > /t/events/raw_syscalls/sys_exit/enable
1595 * grep "NR -1 " /t/trace_pipe
1596 *
1597 * After generating some load on the machine.
1598 */
1599 if (verbose > 1) {
1600 static u64 n;
1601 fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
1602 id, perf_evsel__name(evsel), ++n);
1603 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001604 return NULL;
1605 }
1606
1607 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
1608 trace__read_syscall_info(trace, id))
1609 goto out_cant_read;
1610
1611 if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL))
1612 goto out_cant_read;
1613
1614 return &trace->syscalls.table[id];
1615
1616out_cant_read:
Namhyung Kimbb963e12017-02-17 17:17:38 +09001617 if (verbose > 0) {
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03001618 fprintf(trace->output, "Problems reading syscall %d", id);
1619 if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
1620 fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
1621 fputs(" information\n", trace->output);
1622 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001623 return NULL;
1624}
1625
David Ahernbf2575c2013-10-08 21:26:53 -06001626static void thread__update_stats(struct thread_trace *ttrace,
1627 int id, struct perf_sample *sample)
1628{
1629 struct int_node *inode;
1630 struct stats *stats;
1631 u64 duration = 0;
1632
1633 inode = intlist__findnew(ttrace->syscall_stats, id);
1634 if (inode == NULL)
1635 return;
1636
1637 stats = inode->priv;
1638 if (stats == NULL) {
1639 stats = malloc(sizeof(struct stats));
1640 if (stats == NULL)
1641 return;
1642 init_stats(stats);
1643 inode->priv = stats;
1644 }
1645
1646 if (ttrace->entry_time && sample->time > ttrace->entry_time)
1647 duration = sample->time - ttrace->entry_time;
1648
1649 update_stats(stats, duration);
1650}
1651
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001652static int trace__printf_interrupted_entry(struct trace *trace)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001653{
1654 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001655 size_t printed;
1656
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001657 if (trace->failure_only || trace->current == NULL)
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001658 return 0;
1659
1660 ttrace = thread__priv(trace->current);
1661
1662 if (!ttrace->entry_pending)
1663 return 0;
1664
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001665 printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001666 printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str);
1667 ttrace->entry_pending = false;
1668
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03001669 ++trace->nr_events_printed;
1670
Arnaldo Carvalho de Meloe5966632015-02-13 13:22:21 -03001671 return printed;
1672}
1673
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001674static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel,
1675 struct perf_sample *sample, struct thread *thread)
1676{
1677 int printed = 0;
1678
1679 if (trace->print_sample) {
1680 double ts = (double)sample->time / NSEC_PER_MSEC;
1681
1682 printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
1683 perf_evsel__name(evsel), ts,
1684 thread__comm_str(thread),
1685 sample->pid, sample->tid, sample->cpu);
1686 }
1687
1688 return printed;
1689}
1690
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001691static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size)
1692{
1693 void *augmented_args = NULL;
1694
1695 *augmented_args_size = sample->raw_size - sc->args_size;
1696 if (*augmented_args_size > 0)
1697 augmented_args = sample->raw_data + sc->args_size;
1698
1699 return augmented_args;
1700}
1701
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001702static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001703 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001704 struct perf_sample *sample)
1705{
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001706 char *msg;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001707 void *args;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001708 size_t printed = 0;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001709 struct thread *thread;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001710 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001711 int augmented_args_size = 0;
1712 void *augmented_args = NULL;
David Ahernbf2575c2013-10-08 21:26:53 -06001713 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001714 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001715
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001716 if (sc == NULL)
1717 return -1;
1718
David Ahern8fb598e2013-09-28 13:13:00 -06001719 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001720 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001721 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001722 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001723
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001724 trace__fprintf_sample(trace, evsel, sample, thread);
1725
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001726 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001727
1728 if (ttrace->entry_str == NULL) {
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001729 ttrace->entry_str = malloc(trace__entry_str_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001730 if (!ttrace->entry_str)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001731 goto out_put;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001732 }
1733
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001734 if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03001735 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001736 /*
1737 * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
1738 * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
1739 * this breaks syscall__augmented_args() check for augmented args, as we calculate
1740 * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
1741 * so when handling, say the openat syscall, we end up getting 6 args for the
1742 * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
1743 * thinking that the extra 2 u64 args are the augmented filename, so just check
1744 * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
1745 */
1746 if (evsel != trace->syscalls.events.sys_enter)
1747 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001748 ttrace->entry_time = sample->time;
1749 msg = ttrace->entry_str;
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001750 printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001751
Arnaldo Carvalho de Meloe4d44e82015-08-04 22:17:29 -03001752 printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
Arnaldo Carvalho de Melodb2da3f2018-09-03 16:24:09 -03001753 args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001754
Arnaldo Carvalho de Melo5089f202014-06-17 14:29:24 -03001755 if (sc->is_exit) {
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001756 if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001757 trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Meloc008f782016-05-17 12:13:54 -03001758 fprintf(trace->output, "%-70s)\n", ttrace->entry_str);
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001759 }
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001760 } else {
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001761 ttrace->entry_pending = true;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001762 /* See trace__vfs_getname & trace__sys_exit */
1763 ttrace->filename.pending_open = false;
1764 }
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001765
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001766 if (trace->current != thread) {
1767 thread__put(trace->current);
1768 trace->current = thread__get(thread);
1769 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001770 err = 0;
1771out_put:
1772 thread__put(thread);
1773 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001774}
1775
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001776static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel,
1777 struct perf_sample *sample)
1778{
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001779 struct thread_trace *ttrace;
1780 struct thread *thread;
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001781 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
1782 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001783 char msg[1024];
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001784 void *args, *augmented_args = NULL;
1785 int augmented_args_size;
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001786
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001787 if (sc == NULL)
1788 return -1;
1789
1790 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1791 ttrace = thread__trace(thread, trace->output);
1792 /*
1793 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
1794 * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
1795 */
1796 if (ttrace == NULL)
1797 goto out_put;
1798
Arnaldo Carvalho de Melof3acd882018-08-02 15:11:58 -03001799 args = perf_evsel__sc_tp_ptr(evsel, args, sample);
Arnaldo Carvalho de Melo8a041f82018-09-03 16:07:53 -03001800 augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size);
Arnaldo Carvalho de Melo7a983a02018-08-20 17:58:17 -03001801 syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03001802 fprintf(trace->output, "%s", msg);
1803 err = 0;
1804out_put:
1805 thread__put(thread);
1806 return err;
1807}
1808
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001809static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel,
1810 struct perf_sample *sample,
1811 struct callchain_cursor *cursor)
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001812{
1813 struct addr_location al;
Ravi Bangoria3a9e9a42018-01-30 11:00:53 +05301814 int max_stack = evsel->attr.sample_max_stack ?
1815 evsel->attr.sample_max_stack :
1816 trace->max_stack;
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001817 int err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001818
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001819 if (machine__resolve(trace->host, &al, sample) < 0)
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001820 return -1;
1821
Arnaldo Carvalho de Melo4291bf52018-10-20 09:18:50 -03001822 err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
1823 addr_location__put(&al);
1824 return err;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001825}
1826
1827static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
1828{
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001829 /* TODO: user-configurable print_opts */
Arnaldo Carvalho de Meloe20ab862016-04-12 15:16:15 -03001830 const unsigned int print_opts = EVSEL__PRINT_SYM |
1831 EVSEL__PRINT_DSO |
1832 EVSEL__PRINT_UNKNOWN_AS_ADDR;
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001833
Arnaldo Carvalho de Melod327e602016-04-14 17:53:49 -03001834 return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output);
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03001835}
1836
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01001837static const char *errno_to_name(struct perf_evsel *evsel, int err)
1838{
1839 struct perf_env *env = perf_evsel__env(evsel);
1840 const char *arch_name = perf_env__arch(env);
1841
1842 return arch_syscalls__strerrno(arch_name, err);
1843}
1844
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001845static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001846 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001847 struct perf_sample *sample)
1848{
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09001849 long ret;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001850 u64 duration = 0;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001851 bool duration_calculated = false;
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001852 struct thread *thread;
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001853 int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0;
David Ahernbf2575c2013-10-08 21:26:53 -06001854 struct syscall *sc = trace__syscall_info(trace, evsel, id);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001855 struct thread_trace *ttrace;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001856
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001857 if (sc == NULL)
1858 return -1;
1859
David Ahern8fb598e2013-09-28 13:13:00 -06001860 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001861 ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo2ae3a312013-08-09 12:28:31 -03001862 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001863 goto out_put;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001864
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03001865 trace__fprintf_sample(trace, evsel, sample, thread);
1866
David Ahernbf2575c2013-10-08 21:26:53 -06001867 if (trace->summary)
1868 thread__update_stats(ttrace, id, sample);
1869
Arnaldo Carvalho de Melo77170982013-11-06 16:35:57 -03001870 ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001871
Arnaldo Carvalho de Melo6a648b52018-08-02 10:30:08 -03001872 if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001873 trace__set_fd_pathname(thread, ret, ttrace->filename.name);
1874 ttrace->filename.pending_open = false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001875 ++trace->stats.vfs_getname;
1876 }
1877
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001878 if (ttrace->entry_time) {
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001879 duration = sample->time - ttrace->entry_time;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001880 if (trace__filter_duration(trace, duration))
1881 goto out;
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001882 duration_calculated = true;
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001883 } else if (trace->duration_filter)
1884 goto out;
Arnaldo Carvalho de Melo60c907a2012-10-24 17:24:47 -02001885
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001886 if (sample->callchain) {
1887 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
1888 if (callchain_ret == 0) {
1889 if (callchain_cursor.nr < trace->min_stack)
1890 goto out;
1891 callchain_ret = 1;
1892 }
1893 }
1894
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03001895 if (trace->summary_only || (ret >= 0 && trace->failure_only))
David Ahernfd2eaba2013-11-12 09:31:15 -07001896 goto out;
1897
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03001898 trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001899
1900 if (ttrace->entry_pending) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001901 fprintf(trace->output, "%-70s", ttrace->entry_str);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001902 } else {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001903 fprintf(trace->output, " ... [");
1904 color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
1905 fprintf(trace->output, "]: %s()", sc->name);
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001906 }
1907
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001908 if (sc->fmt == NULL) {
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001909 if (ret < 0)
1910 goto errno_print;
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001911signed_print:
Arnaldo Carvalho de Melo6f8fe612017-07-19 11:39:47 -03001912 fprintf(trace->output, ") = %ld", ret);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001913 } else if (ret < 0) {
1914errno_print: {
Masami Hiramatsu942a91e2014-08-14 02:22:41 +00001915 char bf[STRERR_BUFSIZE];
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001916 const char *emsg = str_error_r(-ret, bf, sizeof(bf)),
Hendrik Brueckner092bd3c2018-01-19 09:56:16 +01001917 *e = errno_to_name(evsel, -ret);
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001918
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001919 fprintf(trace->output, ") = -1 %s %s", e, emsg);
Arnaldo Carvalho de Melo1f631392017-07-18 12:45:57 -03001920 }
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001921 } else if (ret == 0 && sc->fmt->timeout)
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001922 fprintf(trace->output, ") = 0 Timeout");
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001923 else if (ttrace->ret_scnprintf) {
1924 char bf[1024];
Arnaldo Carvalho de Melo7ee57432017-07-14 15:16:54 -03001925 struct syscall_arg arg = {
1926 .val = ret,
1927 .thread = thread,
1928 .trace = trace,
1929 };
1930 ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
Arnaldo Carvalho de Melo84486ca2017-07-14 11:35:03 -03001931 ttrace->ret_scnprintf = NULL;
1932 fprintf(trace->output, ") = %s", bf);
1933 } else if (sc->fmt->hexret)
Chang Hyun Park2c82c3a2014-09-26 21:54:01 +09001934 fprintf(trace->output, ") = %#lx", ret);
Arnaldo Carvalho de Melo11c8e392016-04-06 14:33:07 -03001935 else if (sc->fmt->errpid) {
1936 struct thread *child = machine__find_thread(trace->host, ret, ret);
1937
1938 if (child != NULL) {
1939 fprintf(trace->output, ") = %ld", ret);
1940 if (child->comm_set)
1941 fprintf(trace->output, " (%s)", thread__comm_str(child));
1942 thread__put(child);
1943 }
1944 } else
Arnaldo Carvalho de Meloda3c9a42013-08-26 11:28:34 -03001945 goto signed_print;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001946
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03001947 fputc('\n', trace->output);
Milian Wolff566a0882016-04-08 13:34:15 +02001948
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03001949 /*
1950 * We only consider an 'event' for the sake of --max-events a non-filtered
1951 * sys_enter + sys_exit and other tracepoint events.
1952 */
1953 if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
1954 interrupted = true;
1955
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03001956 if (callchain_ret > 0)
1957 trace__fprintf_callchain(trace, sample);
1958 else if (callchain_ret < 0)
1959 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03001960out:
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03001961 ttrace->entry_pending = false;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03001962 err = 0;
1963out_put:
1964 thread__put(thread);
1965 return err;
Arnaldo Carvalho de Meloba3d7de2012-09-28 17:58:36 -03001966}
1967
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001968static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04001969 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03001970 struct perf_sample *sample)
1971{
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001972 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
1973 struct thread_trace *ttrace;
1974 size_t filename_len, entry_str_len, to_move;
1975 ssize_t remaining_space;
1976 char *pos;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001977 const char *filename = perf_evsel__rawptr(evsel, sample, "pathname");
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001978
1979 if (!thread)
1980 goto out;
1981
1982 ttrace = thread__priv(thread);
1983 if (!ttrace)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03001984 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03001985
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001986 filename_len = strlen(filename);
Arnaldo Carvalho de Melo39f0e7a2017-03-24 14:51:28 -03001987 if (filename_len == 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03001988 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001989
1990 if (ttrace->filename.namelen < filename_len) {
1991 char *f = realloc(ttrace->filename.name, filename_len + 1);
1992
1993 if (f == NULL)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03001994 goto out_put;
Arnaldo Carvalho de Melo7f4f8002015-08-14 13:16:27 -03001995
1996 ttrace->filename.namelen = filename_len;
1997 ttrace->filename.name = f;
1998 }
1999
2000 strcpy(ttrace->filename.name, filename);
2001 ttrace->filename.pending_open = true;
2002
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002003 if (!ttrace->filename.ptr)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002004 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002005
2006 entry_str_len = strlen(ttrace->entry_str);
2007 remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
2008 if (remaining_space <= 0)
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002009 goto out_put;
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002010
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002011 if (filename_len > (size_t)remaining_space) {
2012 filename += filename_len - remaining_space;
2013 filename_len = remaining_space;
2014 }
2015
2016 to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
2017 pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
2018 memmove(pos + filename_len, pos, to_move);
2019 memcpy(pos, filename, filename_len);
2020
2021 ttrace->filename.ptr = 0;
2022 ttrace->filename.entry_str_pos = 0;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002023out_put:
2024 thread__put(thread);
Arnaldo Carvalho de Melof9945922015-08-04 22:30:09 -03002025out:
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002026 return 0;
2027}
2028
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002029static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002030 union perf_event *event __maybe_unused,
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002031 struct perf_sample *sample)
2032{
2033 u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
2034 double runtime_ms = (double)runtime / NSEC_PER_MSEC;
David Ahern8fb598e2013-09-28 13:13:00 -06002035 struct thread *thread = machine__findnew_thread(trace->host,
Adrian Hunter314add62013-08-27 11:23:03 +03002036 sample->pid,
2037 sample->tid);
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002038 struct thread_trace *ttrace = thread__trace(thread, trace->output);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002039
2040 if (ttrace == NULL)
2041 goto out_dump;
2042
2043 ttrace->runtime_ms += runtime_ms;
2044 trace->runtime_ms += runtime_ms;
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002045out_put:
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002046 thread__put(thread);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002047 return 0;
2048
2049out_dump:
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002050 fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002051 evsel->name,
2052 perf_evsel__strval(evsel, sample, "comm"),
2053 (pid_t)perf_evsel__intval(evsel, sample, "pid"),
2054 runtime,
2055 perf_evsel__intval(evsel, sample, "vruntime"));
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002056 goto out_put;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002057}
2058
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002059static int bpf_output__printer(enum binary_printer_ops op,
2060 unsigned int val, void *extra __maybe_unused, FILE *fp)
Wang Nan1d6c9402016-02-26 09:31:55 +00002061{
Wang Nan1d6c9402016-02-26 09:31:55 +00002062 unsigned char ch = (unsigned char)val;
2063
2064 switch (op) {
2065 case BINARY_PRINT_CHAR_DATA:
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002066 return fprintf(fp, "%c", isprint(ch) ? ch : '.');
Wang Nan1d6c9402016-02-26 09:31:55 +00002067 case BINARY_PRINT_DATA_BEGIN:
2068 case BINARY_PRINT_LINE_BEGIN:
2069 case BINARY_PRINT_ADDR:
2070 case BINARY_PRINT_NUM_DATA:
2071 case BINARY_PRINT_NUM_PAD:
2072 case BINARY_PRINT_SEP:
2073 case BINARY_PRINT_CHAR_PAD:
2074 case BINARY_PRINT_LINE_END:
2075 case BINARY_PRINT_DATA_END:
2076 default:
2077 break;
2078 }
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002079
2080 return 0;
Wang Nan1d6c9402016-02-26 09:31:55 +00002081}
2082
2083static void bpf_output__fprintf(struct trace *trace,
2084 struct perf_sample *sample)
2085{
Arnaldo Carvalho de Melo923d0c92017-10-17 10:35:00 -03002086 binary__fprintf(sample->raw_data, sample->raw_size, 8,
2087 bpf_output__printer, NULL, trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002088 ++trace->nr_events_printed;
Wang Nan1d6c9402016-02-26 09:31:55 +00002089}
2090
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002091static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel,
2092 union perf_event *event __maybe_unused,
2093 struct perf_sample *sample)
2094{
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002095 struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002096 int callchain_ret = 0;
2097
2098 if (sample->callchain) {
2099 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2100 if (callchain_ret == 0) {
2101 if (callchain_cursor.nr < trace->min_stack)
2102 goto out;
2103 callchain_ret = 1;
2104 }
2105 }
2106
Arnaldo Carvalho de Melo522283f2018-01-22 11:42:11 -03002107 trace__printf_interrupted_entry(trace);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002108 trace__fprintf_tstamp(trace, sample->time, trace->output);
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002109
2110 if (trace->trace_syscalls)
2111 fprintf(trace->output, "( ): ");
2112
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002113 if (thread)
2114 trace__fprintf_comm_tid(trace, thread, trace->output);
2115
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002116 if (evsel == trace->syscalls.events.augmented) {
2117 int id = perf_evsel__sc_tp_uint(evsel, id, sample);
2118 struct syscall *sc = trace__syscall_info(trace, evsel, id);
2119
2120 if (sc) {
Arnaldo Carvalho de Meloc4191e52018-08-30 13:37:28 -03002121 fprintf(trace->output, "%s(", sc->name);
2122 trace__fprintf_sys_enter(trace, evsel, sample);
2123 fputc(')', trace->output);
2124 goto newline;
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002125 }
2126
2127 /*
2128 * XXX: Not having the associated syscall info or not finding/adding
2129 * the thread should never happen, but if it does...
2130 * fall thru and print it as a bpf_output event.
2131 */
2132 }
2133
Arnaldo Carvalho de Melo08089212015-02-19 21:51:50 -08002134 fprintf(trace->output, "%s:", evsel->name);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002135
Wang Nan1d6c9402016-02-26 09:31:55 +00002136 if (perf_evsel__is_bpf_output(evsel)) {
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002137 bpf_output__fprintf(trace, sample);
Wang Nan1d6c9402016-02-26 09:31:55 +00002138 } else if (evsel->tp_format) {
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002139 if (strncmp(evsel->tp_format->name, "sys_enter_", 10) ||
2140 trace__fprintf_sys_enter(trace, evsel, sample)) {
2141 event_format__fprintf(evsel->tp_format, sample->cpu,
2142 sample->raw_data, sample->raw_size,
2143 trace->output);
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002144 ++trace->nr_events_printed;
Arnaldo Carvalho de Meloa98392b2018-08-02 14:05:09 -03002145 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002146 }
2147
Arnaldo Carvalho de Melo1cdf6182018-08-21 11:44:23 -03002148newline:
Changbin Du51125a22018-03-13 18:40:01 +08002149 fprintf(trace->output, "\n");
Arnaldo Carvalho de Melo202ff962016-04-12 10:11:07 -03002150
Arnaldo Carvalho de Melo7ad35612016-04-20 19:55:48 -03002151 if (callchain_ret > 0)
2152 trace__fprintf_callchain(trace, sample);
2153 else if (callchain_ret < 0)
2154 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
2155out:
Arnaldo Carvalho de Meloa937c662018-10-20 09:27:52 -03002156 thread__put(thread);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002157 return 0;
2158}
2159
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002160static void print_location(FILE *f, struct perf_sample *sample,
2161 struct addr_location *al,
2162 bool print_dso, bool print_sym)
2163{
2164
Namhyung Kimbb963e12017-02-17 17:17:38 +09002165 if ((verbose > 0 || print_dso) && al->map)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002166 fprintf(f, "%s@", al->map->dso->long_name);
2167
Namhyung Kimbb963e12017-02-17 17:17:38 +09002168 if ((verbose > 0 || print_sym) && al->sym)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002169 fprintf(f, "%s+0x%" PRIx64, al->sym->name,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002170 al->addr - al->sym->start);
2171 else if (al->map)
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002172 fprintf(f, "0x%" PRIx64, al->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002173 else
Arnaldo Carvalho de Melo4414a3c2014-07-08 15:39:21 -03002174 fprintf(f, "0x%" PRIx64, sample->addr);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002175}
2176
2177static int trace__pgfault(struct trace *trace,
2178 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03002179 union perf_event *event __maybe_unused,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002180 struct perf_sample *sample)
2181{
2182 struct thread *thread;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002183 struct addr_location al;
2184 char map_type = 'd';
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002185 struct thread_trace *ttrace;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002186 int err = -1;
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002187 int callchain_ret = 0;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002188
2189 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002190
2191 if (sample->callchain) {
2192 callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor);
2193 if (callchain_ret == 0) {
2194 if (callchain_cursor.nr < trace->min_stack)
2195 goto out_put;
2196 callchain_ret = 1;
2197 }
2198 }
2199
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002200 ttrace = thread__trace(thread, trace->output);
2201 if (ttrace == NULL)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002202 goto out_put;
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002203
2204 if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)
2205 ttrace->pfmaj++;
2206 else
2207 ttrace->pfmin++;
2208
2209 if (trace->summary_only)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002210 goto out;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002211
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002212 thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002213
Arnaldo Carvalho de Melofd2b2972017-03-29 16:37:51 -03002214 trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002215
2216 fprintf(trace->output, "%sfault [",
2217 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
2218 "maj" : "min");
2219
2220 print_location(trace->output, sample, &al, false, true);
2221
2222 fprintf(trace->output, "] => ");
2223
Arnaldo Carvalho de Melo117d3c22018-04-25 18:16:53 -03002224 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002225
2226 if (!al.map) {
Arnaldo Carvalho de Melo45462632018-04-24 11:24:49 -03002227 thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002228
2229 if (al.map)
2230 map_type = 'x';
2231 else
2232 map_type = '?';
2233 }
2234
2235 print_location(trace->output, sample, &al, true, false);
2236
2237 fprintf(trace->output, " (%c%c)\n", map_type, al.level);
Arnaldo Carvalho de Melo0c3a6ef2016-04-19 16:31:12 -03002238
Arnaldo Carvalho de Melo1df54292016-04-20 20:06:02 -03002239 if (callchain_ret > 0)
2240 trace__fprintf_callchain(trace, sample);
2241 else if (callchain_ret < 0)
2242 pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002243
2244 ++trace->nr_events_printed;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -03002245out:
2246 err = 0;
2247out_put:
2248 thread__put(thread);
2249 return err;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002250}
2251
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002252static void trace__set_base_time(struct trace *trace,
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002253 struct perf_evsel *evsel,
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002254 struct perf_sample *sample)
2255{
Arnaldo Carvalho de Melo8a07a802016-03-31 15:19:39 -03002256 /*
2257 * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
2258 * and don't use sample->time unconditionally, we may end up having
2259 * some other event in the future without PERF_SAMPLE_TIME for good
2260 * reason, i.e. we may not be interested in its timestamps, just in
2261 * it taking place, picking some piece of information when it
2262 * appears in our event stream (vfs_getname comes to mind).
2263 */
2264 if (trace->base_time == 0 && !trace->full_time &&
2265 (evsel->attr.sample_type & PERF_SAMPLE_TIME))
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002266 trace->base_time = sample->time;
2267}
2268
David Ahern6810fc92013-08-28 22:29:52 -06002269static int trace__process_sample(struct perf_tool *tool,
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002270 union perf_event *event,
David Ahern6810fc92013-08-28 22:29:52 -06002271 struct perf_sample *sample,
2272 struct perf_evsel *evsel,
2273 struct machine *machine __maybe_unused)
2274{
2275 struct trace *trace = container_of(tool, struct trace, tool);
David Ahernaa07df62016-11-25 09:29:52 -07002276 struct thread *thread;
David Ahern6810fc92013-08-28 22:29:52 -06002277 int err = 0;
2278
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002279 tracepoint_handler handler = evsel->handler;
David Ahern6810fc92013-08-28 22:29:52 -06002280
David Ahernaa07df62016-11-25 09:29:52 -07002281 thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
2282 if (thread && thread__is_filtered(thread))
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002283 goto out;
David Ahernbdc89662013-08-28 22:29:53 -06002284
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002285 trace__set_base_time(trace, evsel, sample);
David Ahern6810fc92013-08-28 22:29:52 -06002286
David Ahern31605652013-12-04 19:41:41 -07002287 if (handler) {
2288 ++trace->nr_events;
Stanislav Fomichev0c82adc2014-06-26 20:14:24 +04002289 handler(trace, evsel, event, sample);
David Ahern31605652013-12-04 19:41:41 -07002290 }
Arnaldo Carvalho de Meloef65e962017-03-24 15:03:19 -03002291out:
2292 thread__put(thread);
David Ahern6810fc92013-08-28 22:29:52 -06002293 return err;
2294}
2295
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002296static int trace__record(struct trace *trace, int argc, const char **argv)
David Ahern5e2485b2013-09-28 13:13:01 -06002297{
2298 unsigned int rec_argc, i, j;
2299 const char **rec_argv;
2300 const char * const record_args[] = {
2301 "record",
2302 "-R",
2303 "-m", "1024",
2304 "-c", "1",
David Ahern5e2485b2013-09-28 13:13:01 -06002305 };
2306
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002307 const char * const sc_args[] = { "-e", };
2308 unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
2309 const char * const majpf_args[] = { "-e", "major-faults" };
2310 unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
2311 const char * const minpf_args[] = { "-e", "minor-faults" };
2312 unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
2313
David Ahern9aca7f12013-12-04 19:41:39 -07002314 /* +1 is for the event string below */
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002315 rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 +
2316 majpf_args_nr + minpf_args_nr + argc;
David Ahern5e2485b2013-09-28 13:13:01 -06002317 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2318
2319 if (rec_argv == NULL)
2320 return -ENOMEM;
2321
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002322 j = 0;
David Ahern5e2485b2013-09-28 13:13:01 -06002323 for (i = 0; i < ARRAY_SIZE(record_args); i++)
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002324 rec_argv[j++] = record_args[i];
2325
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002326 if (trace->trace_syscalls) {
2327 for (i = 0; i < sc_args_nr; i++)
2328 rec_argv[j++] = sc_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002329
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002330 /* event string may be different for older kernels - e.g., RHEL6 */
2331 if (is_valid_tracepoint("raw_syscalls:sys_enter"))
2332 rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
2333 else if (is_valid_tracepoint("syscalls:sys_enter"))
2334 rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
2335 else {
2336 pr_err("Neither raw_syscalls nor syscalls events exist.\n");
Martin Kepplingerc896f852017-09-13 21:14:19 +02002337 free(rec_argv);
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002338 return -1;
2339 }
David Ahern9aca7f12013-12-04 19:41:39 -07002340 }
David Ahern9aca7f12013-12-04 19:41:39 -07002341
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002342 if (trace->trace_pgfaults & TRACE_PFMAJ)
2343 for (i = 0; i < majpf_args_nr; i++)
2344 rec_argv[j++] = majpf_args[i];
David Ahern5e2485b2013-09-28 13:13:01 -06002345
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002346 if (trace->trace_pgfaults & TRACE_PFMIN)
2347 for (i = 0; i < minpf_args_nr; i++)
2348 rec_argv[j++] = minpf_args[i];
2349
2350 for (i = 0; i < (unsigned int)argc; i++)
2351 rec_argv[j++] = argv[i];
2352
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002353 return cmd_record(j, rec_argv);
David Ahern5e2485b2013-09-28 13:13:01 -06002354}
2355
David Ahernbf2575c2013-10-08 21:26:53 -06002356static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
2357
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002358static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002359{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002360 struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname");
Jiri Olsa8dd2a132015-09-07 10:38:06 +02002361
2362 if (IS_ERR(evsel))
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002363 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002364
2365 if (perf_evsel__field(evsel, "pathname") == NULL) {
2366 perf_evsel__delete(evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002367 return false;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002368 }
2369
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -03002370 evsel->handler = trace__vfs_getname;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002371 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002372 return true;
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002373}
2374
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002375static struct perf_evsel *perf_evsel__new_pgfault(u64 config)
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002376{
2377 struct perf_evsel *evsel;
2378 struct perf_event_attr attr = {
2379 .type = PERF_TYPE_SOFTWARE,
2380 .mmap_data = 1,
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002381 };
2382
2383 attr.config = config;
Arnaldo Carvalho de Melo05247982014-07-23 18:15:09 -03002384 attr.sample_period = 1;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002385
2386 event_attr_init(&attr);
2387
2388 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002389 if (evsel)
2390 evsel->handler = trace__pgfault;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002391
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002392 return evsel;
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002393}
2394
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002395static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
2396{
2397 const u32 type = event->header.type;
2398 struct perf_evsel *evsel;
2399
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002400 if (type != PERF_RECORD_SAMPLE) {
2401 trace__process_event(trace, trace->host, event, sample);
2402 return;
2403 }
2404
2405 evsel = perf_evlist__id2evsel(trace->evlist, sample->id);
2406 if (evsel == NULL) {
2407 fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
2408 return;
2409 }
2410
Arnaldo Carvalho de Meloe6001982016-03-31 15:16:28 -03002411 trace__set_base_time(trace, evsel, sample);
2412
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002413 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
2414 sample->raw_data == NULL) {
2415 fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
2416 perf_evsel__name(evsel), sample->tid,
2417 sample->cpu, sample->raw_size);
2418 } else {
2419 tracepoint_handler handler = evsel->handler;
2420 handler(trace, evsel, event, sample);
2421 }
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03002422
2423 if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
2424 interrupted = true;
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002425}
2426
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002427static int trace__add_syscall_newtp(struct trace *trace)
2428{
2429 int ret = -1;
2430 struct perf_evlist *evlist = trace->evlist;
2431 struct perf_evsel *sys_enter, *sys_exit;
2432
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002433 sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002434 if (sys_enter == NULL)
2435 goto out;
2436
2437 if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
2438 goto out_delete_sys_enter;
2439
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002440 sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002441 if (sys_exit == NULL)
2442 goto out_delete_sys_enter;
2443
2444 if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
2445 goto out_delete_sys_exit;
2446
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002447 perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
2448 perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
2449
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002450 perf_evlist__add(evlist, sys_enter);
2451 perf_evlist__add(evlist, sys_exit);
2452
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03002453 if (callchain_param.enabled && !trace->kernel_syscallchains) {
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03002454 /*
2455 * We're interested only in the user space callchain
2456 * leading to the syscall, allow overriding that for
2457 * debugging reasons using --kernel_syscall_callchains
2458 */
2459 sys_exit->attr.exclude_callchain_kernel = 1;
2460 }
2461
Arnaldo Carvalho de Melo8b3ce752015-07-02 18:28:11 -03002462 trace->syscalls.events.sys_enter = sys_enter;
2463 trace->syscalls.events.sys_exit = sys_exit;
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002464
2465 ret = 0;
2466out:
2467 return ret;
2468
2469out_delete_sys_exit:
2470 perf_evsel__delete_priv(sys_exit);
2471out_delete_sys_enter:
2472 perf_evsel__delete_priv(sys_enter);
2473 goto out;
2474}
2475
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002476static int trace__set_ev_qualifier_filter(struct trace *trace)
2477{
2478 int err = -1;
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002479 struct perf_evsel *sys_exit;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002480 char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
2481 trace->ev_qualifier_ids.nr,
2482 trace->ev_qualifier_ids.entries);
2483
2484 if (filter == NULL)
2485 goto out_enomem;
2486
Mathieu Poirier3541c032016-09-16 08:44:04 -06002487 if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter,
2488 filter)) {
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002489 sys_exit = trace->syscalls.events.sys_exit;
Mathieu Poirier3541c032016-09-16 08:44:04 -06002490 err = perf_evsel__append_tp_filter(sys_exit, filter);
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06002491 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002492
2493 free(filter);
2494out:
2495 return err;
2496out_enomem:
2497 errno = ENOMEM;
2498 goto out;
2499}
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002500
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002501static int trace__set_filter_loop_pids(struct trace *trace)
2502{
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03002503 unsigned int nr = 1;
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002504 pid_t pids[32] = {
2505 getpid(),
2506 };
Arnaldo Carvalho de Melo082ab9a2017-07-20 11:32:05 -03002507 struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
2508
2509 while (thread && nr < ARRAY_SIZE(pids)) {
2510 struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid);
2511
2512 if (parent == NULL)
2513 break;
2514
2515 if (!strcmp(thread__comm_str(parent), "sshd")) {
2516 pids[nr++] = parent->tid;
2517 break;
2518 }
2519 thread = parent;
2520 }
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002521
2522 return perf_evlist__set_filter_pids(trace->evlist, nr, pids);
2523}
2524
Namhyung Kimf15eb532012-10-05 14:02:16 +09002525static int trace__run(struct trace *trace, int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002526{
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002527 struct perf_evlist *evlist = trace->evlist;
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002528 struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002529 int err = -1, i;
2530 unsigned long before;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002531 const bool forks = argc > 0;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002532 bool draining = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002533
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002534 trace->live = true;
2535
Arnaldo Carvalho de Meloc27366f2015-07-02 18:24:51 -03002536 if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03002537 goto out_error_raw_syscalls;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002538
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002539 if (trace->trace_syscalls)
Arnaldo Carvalho de Melo08c98772015-08-04 17:01:04 -03002540 trace->vfs_getname = perf_evlist__add_vfs_getname(evlist);
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002541
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002542 if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
2543 pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
2544 if (pgfault_maj == NULL)
2545 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002546 perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002547 perf_evlist__add(evlist, pgfault_maj);
Arnaldo Carvalho de Meloe2726d92015-01-22 10:34:22 -03002548 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002549
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002550 if ((trace->trace_pgfaults & TRACE_PFMIN)) {
2551 pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
2552 if (pgfault_min == NULL)
2553 goto out_error_mem;
Arnaldo Carvalho de Melo08e26392018-01-12 13:29:05 -03002554 perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melo0ae537c2016-04-19 16:00:01 -03002555 perf_evlist__add(evlist, pgfault_min);
2556 }
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04002557
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002558 if (trace->sched &&
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002559 perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
2560 trace__sched_stat_runtime))
2561 goto out_error_sched_stat_runtime;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002562
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03002563 /*
2564 * If a global cgroup was set, apply it to all the events without an
2565 * explicit cgroup. I.e.:
2566 *
2567 * trace -G A -e sched:*switch
2568 *
2569 * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
2570 * _and_ sched:sched_switch to the 'A' cgroup, while:
2571 *
2572 * trace -e sched:*switch -G A
2573 *
2574 * will only set the sched:sched_switch event to the 'A' cgroup, all the
2575 * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
2576 * a cgroup (on the root cgroup, sys wide, etc).
2577 *
2578 * Multiple cgroups:
2579 *
2580 * trace -G A -e sched:*switch -G B
2581 *
2582 * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
2583 * to the 'B' cgroup.
2584 *
2585 * evlist__set_default_cgroup() grabs a reference of the passed cgroup
2586 * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
2587 */
2588 if (trace->cgroup)
2589 evlist__set_default_cgroup(trace->evlist, trace->cgroup);
2590
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002591 err = perf_evlist__create_maps(evlist, &trace->opts.target);
2592 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002593 fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002594 goto out_delete_evlist;
2595 }
2596
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002597 err = trace__symbols_init(trace, evlist);
2598 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002599 fprintf(trace->output, "Problems initializing symbol libraries!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002600 goto out_delete_evlist;
Arnaldo Carvalho de Melo752fde42012-10-06 18:43:19 -03002601 }
2602
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03002603 perf_evlist__config(evlist, &trace->opts, &callchain_param);
Arnaldo Carvalho de Melofde54b72016-04-11 18:42:37 -03002604
Namhyung Kimf15eb532012-10-05 14:02:16 +09002605 signal(SIGCHLD, sig_handler);
2606 signal(SIGINT, sig_handler);
2607
2608 if (forks) {
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09002609 err = perf_evlist__prepare_workload(evlist, &trace->opts.target,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03002610 argv, false, NULL);
Namhyung Kimf15eb532012-10-05 14:02:16 +09002611 if (err < 0) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002612 fprintf(trace->output, "Couldn't run the workload!\n");
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002613 goto out_delete_evlist;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002614 }
2615 }
2616
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002617 err = perf_evlist__open(evlist);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002618 if (err < 0)
2619 goto out_error_open;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002620
Wang Nanba504232016-02-26 09:31:54 +00002621 err = bpf__apply_obj_config();
2622 if (err) {
2623 char errbuf[BUFSIZ];
2624
2625 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
2626 pr_err("ERROR: Apply config to BPF failed: %s\n",
2627 errbuf);
2628 goto out_error_open;
2629 }
2630
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08002631 /*
2632 * Better not use !target__has_task() here because we need to cover the
2633 * case where no threads were specified in the command line, but a
2634 * workload was, and in that case we will fill in the thread_map when
2635 * we fork the workload in perf_evlist__prepare_workload.
2636 */
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08002637 if (trace->filter_pids.nr > 0)
2638 err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
Jiri Olsae13798c2015-06-23 00:36:02 +02002639 else if (thread_map__pid(evlist->threads, 0) == -1)
Arnaldo Carvalho de Melodd1a5032017-07-20 11:17:51 -03002640 err = trace__set_filter_loop_pids(trace);
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08002641
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002642 if (err < 0)
2643 goto out_error_mem;
2644
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002645 if (trace->ev_qualifier_ids.nr > 0) {
2646 err = trace__set_ev_qualifier_filter(trace);
2647 if (err < 0)
2648 goto out_errno;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002649
Arnaldo Carvalho de Melo2e5e5f82015-08-03 17:12:29 -03002650 pr_debug("event qualifier tracepoint filter: %s\n",
2651 trace->syscalls.events.sys_exit->filter);
2652 }
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002653
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002654 err = perf_evlist__apply_filters(evlist, &evsel);
2655 if (err < 0)
2656 goto out_error_apply_filters;
Arnaldo Carvalho de Melo241b0572015-02-21 10:15:21 -08002657
Wang Nanf74b9d3a2017-12-03 02:00:37 +00002658 err = perf_evlist__mmap(evlist, trace->opts.mmap_pages);
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002659 if (err < 0)
2660 goto out_error_mmap;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002661
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002662 if (!target__none(&trace->opts.target) && !trace->opts.initial_delay)
Arnaldo Carvalho de Melocb24d012015-04-22 10:04:23 -03002663 perf_evlist__enable(evlist);
2664
Namhyung Kimf15eb532012-10-05 14:02:16 +09002665 if (forks)
2666 perf_evlist__start_workload(evlist);
2667
Alexis Berlemonte36b7822016-10-10 07:43:28 +02002668 if (trace->opts.initial_delay) {
2669 usleep(trace->opts.initial_delay * 1000);
2670 perf_evlist__enable(evlist);
2671 }
2672
Jiri Olsae13798c2015-06-23 00:36:02 +02002673 trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
Arnaldo Carvalho de Melo42052be2015-02-13 12:32:45 -03002674 evlist->threads->nr > 1 ||
2675 perf_evlist__first(evlist)->attr.inherit;
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002676
2677 /*
2678 * Now that we already used evsel->attr to ask the kernel to setup the
2679 * events, lets reuse evsel->attr.sample_max_stack as the limit in
2680 * trace__resolve_callchain(), allowing per-event max-stack settings
2681 * to override an explicitely set --max-stack global setting.
2682 */
2683 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03002684 if (evsel__has_callchain(evsel) &&
Arnaldo Carvalho de Melobd3dda92018-01-15 11:33:53 -03002685 evsel->attr.sample_max_stack == 0)
2686 evsel->attr.sample_max_stack = trace->max_stack;
2687 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002688again:
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002689 before = trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002690
2691 for (i = 0; i < evlist->nr_mmaps; i++) {
2692 union perf_event *event;
Kan Liangd7f55c62018-03-01 18:08:59 -05002693 struct perf_mmap *md;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002694
Kan Liangd7f55c62018-03-01 18:08:59 -05002695 md = &evlist->mmap[i];
Kan Liangb9bae2c2018-03-06 10:36:07 -05002696 if (perf_mmap__read_init(md) < 0)
Kan Liangd7f55c62018-03-01 18:08:59 -05002697 continue;
2698
Kan Liang0019dc872018-03-06 10:36:06 -05002699 while ((event = perf_mmap__read_event(md)) != NULL) {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002700 struct perf_sample sample;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002701
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002702 ++trace->nr_events;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002703
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002704 err = perf_evlist__parse_sample(evlist, event, &sample);
2705 if (err) {
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03002706 fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +08002707 goto next_event;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002708 }
2709
Arnaldo Carvalho de Meloddbb1b12015-02-21 12:10:29 -08002710 trace__handle_event(trace, event, &sample);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +08002711next_event:
Kan Liangd6ace3d2018-03-06 10:36:05 -05002712 perf_mmap__consume(md);
Arnaldo Carvalho de Melo20c5f102013-09-03 11:55:07 -03002713
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002714 if (interrupted)
2715 goto out_disable;
Arnaldo Carvalho de Melo02ac5422015-04-22 11:11:57 -03002716
2717 if (done && !draining) {
2718 perf_evlist__disable(evlist);
2719 draining = true;
2720 }
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002721 }
Kan Liangd7f55c62018-03-01 18:08:59 -05002722 perf_mmap__read_done(md);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002723 }
2724
Arnaldo Carvalho de Meloefd57452012-10-17 17:09:46 -03002725 if (trace->nr_events == before) {
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002726 int timeout = done ? 100 : -1;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002727
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002728 if (!draining && perf_evlist__poll(evlist, timeout) > 0) {
2729 if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP) == 0)
2730 draining = true;
2731
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002732 goto again;
Arnaldo Carvalho de Melo46fb3c22014-09-22 14:39:48 -03002733 }
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002734 } else {
2735 goto again;
Namhyung Kimf15eb532012-10-05 14:02:16 +09002736 }
2737
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002738out_disable:
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03002739 thread__zput(trace->current);
2740
Arnaldo Carvalho de Meloba209f82013-10-16 11:57:33 -03002741 perf_evlist__disable(evlist);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002742
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002743 if (!err) {
2744 if (trace->summary)
2745 trace__fprintf_thread_summary(trace, trace->output);
2746
2747 if (trace->show_tool_stats) {
2748 fprintf(trace->output, "Stats:\n "
2749 " vfs_getname : %" PRIu64 "\n"
2750 " proc_getname: %" PRIu64 "\n",
2751 trace->stats.vfs_getname,
2752 trace->stats.proc_getname);
2753 }
2754 }
David Ahernbf2575c2013-10-08 21:26:53 -06002755
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002756out_delete_evlist:
Andrei Vagin33974a42017-11-07 16:22:45 -08002757 trace__symbols__exit(trace);
2758
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002759 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03002760 cgroup__put(trace->cgroup);
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03002761 trace->evlist = NULL;
Arnaldo Carvalho de Melo75b757ca2013-09-24 11:04:32 -03002762 trace->live = false;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002763 return err;
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03002764{
2765 char errbuf[BUFSIZ];
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002766
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002767out_error_sched_stat_runtime:
Jiri Olsa988bdb32015-09-02 09:56:35 +02002768 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
Arnaldo Carvalho de Melo2cc990b2015-01-22 11:13:43 -03002769 goto out_error;
2770
Arnaldo Carvalho de Melo801c67b2015-01-22 10:52:55 -03002771out_error_raw_syscalls:
Jiri Olsa988bdb32015-09-02 09:56:35 +02002772 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002773 goto out_error;
2774
Arnaldo Carvalho de Meloe09b18d2014-12-11 18:04:10 -03002775out_error_mmap:
2776 perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
2777 goto out_error;
2778
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002779out_error_open:
2780 perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
2781
2782out_error:
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03002783 fprintf(trace->output, "%s\n", errbuf);
Ramkumar Ramachandra87f91862013-10-04 10:47:31 +05302784 goto out_delete_evlist;
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002785
2786out_error_apply_filters:
2787 fprintf(trace->output,
2788 "Failed to set filter \"%s\" on event %s with %d (%s)\n",
2789 evsel->filter, perf_evsel__name(evsel), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002790 str_error_r(errno, errbuf, sizeof(errbuf)));
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03002791 goto out_delete_evlist;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002792}
Arnaldo Carvalho de Melo5ed08da2015-01-22 11:08:04 -03002793out_error_mem:
2794 fprintf(trace->output, "Not enough memory to run!\n");
2795 goto out_delete_evlist;
Arnaldo Carvalho de Melo19867b62015-07-04 12:44:59 -03002796
2797out_errno:
2798 fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno));
2799 goto out_delete_evlist;
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03002800}
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03002801
David Ahern6810fc92013-08-28 22:29:52 -06002802static int trace__replay(struct trace *trace)
2803{
2804 const struct perf_evsel_str_handler handlers[] = {
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03002805 { "probe:vfs_getname", trace__vfs_getname, },
David Ahern6810fc92013-08-28 22:29:52 -06002806 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002807 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002808 .file = {
2809 .path = input_name,
2810 },
2811 .mode = PERF_DATA_MODE_READ,
2812 .force = trace->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +02002813 };
David Ahern6810fc92013-08-28 22:29:52 -06002814 struct perf_session *session;
Namhyung Kim003824e2013-11-12 15:25:00 +09002815 struct perf_evsel *evsel;
David Ahern6810fc92013-08-28 22:29:52 -06002816 int err = -1;
2817
2818 trace->tool.sample = trace__process_sample;
2819 trace->tool.mmap = perf_event__process_mmap;
David Ahern384c6712013-09-22 19:44:58 -06002820 trace->tool.mmap2 = perf_event__process_mmap2;
David Ahern6810fc92013-08-28 22:29:52 -06002821 trace->tool.comm = perf_event__process_comm;
2822 trace->tool.exit = perf_event__process_exit;
2823 trace->tool.fork = perf_event__process_fork;
2824 trace->tool.attr = perf_event__process_attr;
Hari Bathinif3b36142017-03-08 02:11:43 +05302825 trace->tool.tracing_data = perf_event__process_tracing_data;
David Ahern6810fc92013-08-28 22:29:52 -06002826 trace->tool.build_id = perf_event__process_build_id;
Hari Bathinif3b36142017-03-08 02:11:43 +05302827 trace->tool.namespaces = perf_event__process_namespaces;
David Ahern6810fc92013-08-28 22:29:52 -06002828
Jiri Olsa0a8cb852014-07-06 14:18:21 +02002829 trace->tool.ordered_events = true;
David Ahern6810fc92013-08-28 22:29:52 -06002830 trace->tool.ordering_requires_timestamps = true;
2831
2832 /* add tid to output */
2833 trace->multiple_threads = true;
2834
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002835 session = perf_session__new(&data, false, &trace->tool);
David Ahern6810fc92013-08-28 22:29:52 -06002836 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +09002837 return -1;
David Ahern6810fc92013-08-28 22:29:52 -06002838
David Ahernaa07df62016-11-25 09:29:52 -07002839 if (trace->opts.target.pid)
2840 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
2841
2842 if (trace->opts.target.tid)
2843 symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
2844
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002845 if (symbol__init(&session->header.env) < 0)
Namhyung Kimcb2ffae2014-08-12 15:40:44 +09002846 goto out;
2847
David Ahern8fb598e2013-09-28 13:13:00 -06002848 trace->host = &session->machines.host;
2849
David Ahern6810fc92013-08-28 22:29:52 -06002850 err = perf_session__set_tracepoints_handlers(session, handlers);
2851 if (err)
2852 goto out;
2853
Namhyung Kim003824e2013-11-12 15:25:00 +09002854 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2855 "raw_syscalls:sys_enter");
David Ahern9aca7f12013-12-04 19:41:39 -07002856 /* older kernels have syscalls tp versus raw_syscalls */
2857 if (evsel == NULL)
2858 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2859 "syscalls:sys_enter");
David Ahern6810fc92013-08-28 22:29:52 -06002860
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002861 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002862 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002863 perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09002864 pr_err("Error during initialize raw_syscalls:sys_enter event\n");
2865 goto out;
2866 }
2867
2868 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2869 "raw_syscalls:sys_exit");
David Ahern9aca7f12013-12-04 19:41:39 -07002870 if (evsel == NULL)
2871 evsel = perf_evlist__find_tracepoint_by_name(session->evlist,
2872 "syscalls:sys_exit");
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002873 if (evsel &&
Arnaldo Carvalho de Melo63f11c82018-08-02 14:27:09 -03002874 (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
Stanislav Fomicheve281a962014-06-26 20:14:28 +04002875 perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
Namhyung Kim003824e2013-11-12 15:25:00 +09002876 pr_err("Error during initialize raw_syscalls:sys_exit event\n");
David Ahern6810fc92013-08-28 22:29:52 -06002877 goto out;
2878 }
2879
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002880 evlist__for_each_entry(session->evlist, evsel) {
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04002881 if (evsel->attr.type == PERF_TYPE_SOFTWARE &&
2882 (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
2883 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
2884 evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS))
2885 evsel->handler = trace__pgfault;
2886 }
2887
David Ahern6810fc92013-08-28 22:29:52 -06002888 setup_pager();
2889
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -03002890 err = perf_session__process_events(session);
David Ahern6810fc92013-08-28 22:29:52 -06002891 if (err)
2892 pr_err("Failed to process events, error %d", err);
2893
David Ahernbf2575c2013-10-08 21:26:53 -06002894 else if (trace->summary)
2895 trace__fprintf_thread_summary(trace, trace->output);
2896
David Ahern6810fc92013-08-28 22:29:52 -06002897out:
2898 perf_session__delete(session);
2899
2900 return err;
2901}
2902
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002903static size_t trace__fprintf_threads_header(FILE *fp)
2904{
2905 size_t printed;
2906
Pekka Enberg99ff7152013-11-12 16:42:14 +02002907 printed = fprintf(fp, "\n Summary of events:\n\n");
David Ahernbf2575c2013-10-08 21:26:53 -06002908
2909 return printed;
2910}
2911
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03002912DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs,
2913 struct stats *stats;
2914 double msecs;
2915 int syscall;
2916)
2917{
2918 struct int_node *source = rb_entry(nd, struct int_node, rb_node);
2919 struct stats *stats = source->priv;
2920
2921 entry->syscall = source->i;
2922 entry->stats = stats;
2923 entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0;
2924}
2925
David Ahernbf2575c2013-10-08 21:26:53 -06002926static size_t thread__dump_stats(struct thread_trace *ttrace,
2927 struct trace *trace, FILE *fp)
2928{
David Ahernbf2575c2013-10-08 21:26:53 -06002929 size_t printed = 0;
2930 struct syscall *sc;
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03002931 struct rb_node *nd;
2932 DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06002933
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03002934 if (syscall_stats == NULL)
David Ahernbf2575c2013-10-08 21:26:53 -06002935 return 0;
2936
2937 printed += fprintf(fp, "\n");
2938
Milian Wolff834fd462015-08-06 11:24:29 +02002939 printed += fprintf(fp, " syscall calls total min avg max stddev\n");
2940 printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
2941 printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n");
Pekka Enberg99ff7152013-11-12 16:42:14 +02002942
Arnaldo Carvalho de Melo98a91832016-06-23 11:34:10 -03002943 resort_rb__for_each_entry(nd, syscall_stats) {
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03002944 struct stats *stats = syscall_stats_entry->stats;
David Ahernbf2575c2013-10-08 21:26:53 -06002945 if (stats) {
2946 double min = (double)(stats->min) / NSEC_PER_MSEC;
2947 double max = (double)(stats->max) / NSEC_PER_MSEC;
2948 double avg = avg_stats(stats);
2949 double pct;
2950 u64 n = (u64) stats->n;
2951
2952 pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0;
2953 avg /= NSEC_PER_MSEC;
2954
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03002955 sc = &trace->syscalls.table[syscall_stats_entry->syscall];
Pekka Enberg99ff7152013-11-12 16:42:14 +02002956 printed += fprintf(fp, " %-15s", sc->name);
Milian Wolff834fd462015-08-06 11:24:29 +02002957 printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f",
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03002958 n, syscall_stats_entry->msecs, min, avg);
Pekka Enberg27a778b2013-11-13 14:21:48 +02002959 printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
David Ahernbf2575c2013-10-08 21:26:53 -06002960 }
David Ahernbf2575c2013-10-08 21:26:53 -06002961 }
2962
Arnaldo Carvalho de Melob535d522016-05-04 16:06:26 -03002963 resort_rb__delete(syscall_stats);
David Ahernbf2575c2013-10-08 21:26:53 -06002964 printed += fprintf(fp, "\n\n");
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03002965
2966 return printed;
2967}
2968
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03002969static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
David Ahern896cbb52013-09-28 13:12:59 -06002970{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03002971 size_t printed = 0;
Namhyung Kim89dceb22014-10-06 09:46:03 +09002972 struct thread_trace *ttrace = thread__priv(thread);
David Ahern896cbb52013-09-28 13:12:59 -06002973 double ratio;
2974
2975 if (ttrace == NULL)
2976 return 0;
2977
2978 ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
2979
Pekka Enberg15e65c62013-11-14 18:43:30 +02002980 printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid);
Pekka Enberg99ff7152013-11-12 16:42:14 +02002981 printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
Pekka Enberg15e65c62013-11-14 18:43:30 +02002982 printed += fprintf(fp, "%.1f%%", ratio);
Stanislav Fomicheva2ea67d2014-07-08 22:05:16 +04002983 if (ttrace->pfmaj)
2984 printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
2985 if (ttrace->pfmin)
2986 printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
Arnaldo Carvalho de Melo03548eb2016-05-05 15:46:50 -03002987 if (trace->sched)
2988 printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
2989 else if (fputc('\n', fp) != EOF)
2990 ++printed;
2991
David Ahernbf2575c2013-10-08 21:26:53 -06002992 printed += thread__dump_stats(ttrace, trace, fp);
David Ahern896cbb52013-09-28 13:12:59 -06002993
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03002994 return printed;
2995}
David Ahern896cbb52013-09-28 13:12:59 -06002996
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03002997static unsigned long thread__nr_events(struct thread_trace *ttrace)
2998{
2999 return ttrace ? ttrace->nr_events : 0;
3000}
3001
3002DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)),
3003 struct thread *thread;
3004)
3005{
3006 entry->thread = rb_entry(nd, struct thread, rb_node);
David Ahern896cbb52013-09-28 13:12:59 -06003007}
3008
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003009static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
3010{
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003011 size_t printed = trace__fprintf_threads_header(fp);
3012 struct rb_node *nd;
Kan Liang91e467b2017-09-10 19:23:14 -07003013 int i;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003014
Kan Liang91e467b2017-09-10 19:23:14 -07003015 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3016 DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
3017
3018 if (threads == NULL) {
3019 fprintf(fp, "%s", "Error sorting output by nr_events!\n");
3020 return 0;
3021 }
3022
3023 resort_rb__for_each_entry(nd, threads)
3024 printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
3025
3026 resort_rb__delete(threads);
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003027 }
Arnaldo Carvalho de Melo96c14452016-05-04 12:47:16 -03003028 return printed;
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003029}
3030
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003031static int trace__set_duration(const struct option *opt, const char *str,
3032 int unset __maybe_unused)
3033{
3034 struct trace *trace = opt->value;
3035
3036 trace->duration_filter = atof(str);
3037 return 0;
3038}
3039
Arnaldo Carvalho de Melof078c382015-02-21 11:36:52 -08003040static int trace__set_filter_pids(const struct option *opt, const char *str,
3041 int unset __maybe_unused)
3042{
3043 int ret = -1;
3044 size_t i;
3045 struct trace *trace = opt->value;
3046 /*
3047 * FIXME: introduce a intarray class, plain parse csv and create a
3048 * { int nr, int entries[] } struct...
3049 */
3050 struct intlist *list = intlist__new(str);
3051
3052 if (list == NULL)
3053 return -1;
3054
3055 i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
3056 trace->filter_pids.entries = calloc(i, sizeof(pid_t));
3057
3058 if (trace->filter_pids.entries == NULL)
3059 goto out;
3060
3061 trace->filter_pids.entries[0] = getpid();
3062
3063 for (i = 1; i < trace->filter_pids.nr; ++i)
3064 trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
3065
3066 intlist__delete(list);
3067 ret = 0;
3068out:
3069 return ret;
3070}
3071
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003072static int trace__open_output(struct trace *trace, const char *filename)
3073{
3074 struct stat st;
3075
3076 if (!stat(filename, &st) && st.st_size) {
3077 char oldname[PATH_MAX];
3078
3079 scnprintf(oldname, sizeof(oldname), "%s.old", filename);
3080 unlink(oldname);
3081 rename(filename, oldname);
3082 }
3083
3084 trace->output = fopen(filename, "w");
3085
3086 return trace->output == NULL ? -errno : 0;
3087}
3088
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003089static int parse_pagefaults(const struct option *opt, const char *str,
3090 int unset __maybe_unused)
3091{
3092 int *trace_pgfaults = opt->value;
3093
3094 if (strcmp(str, "all") == 0)
3095 *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
3096 else if (strcmp(str, "maj") == 0)
3097 *trace_pgfaults |= TRACE_PFMAJ;
3098 else if (strcmp(str, "min") == 0)
3099 *trace_pgfaults |= TRACE_PFMIN;
3100 else
3101 return -1;
3102
3103 return 0;
3104}
3105
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003106static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
3107{
3108 struct perf_evsel *evsel;
3109
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003110 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003111 evsel->handler = handler;
3112}
3113
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003114static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist)
3115{
3116 struct perf_evsel *evsel;
3117
3118 evlist__for_each_entry(evlist, evsel) {
3119 if (evsel->priv || !evsel->tp_format)
3120 continue;
3121
3122 if (strcmp(evsel->tp_format->system, "syscalls"))
3123 continue;
3124
3125 if (perf_evsel__init_syscall_tp(evsel))
3126 return -1;
3127
3128 if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) {
3129 struct syscall_tp *sc = evsel->priv;
3130
3131 if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
3132 return -1;
3133 } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) {
3134 struct syscall_tp *sc = evsel->priv;
3135
3136 if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap))
3137 return -1;
3138 }
3139 }
3140
3141 return 0;
3142}
3143
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003144/*
3145 * XXX: Hackish, just splitting the combined -e+--event (syscalls
3146 * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
3147 * existing facilities unchanged (trace->ev_qualifier + parse_options()).
3148 *
3149 * It'd be better to introduce a parse_options() variant that would return a
3150 * list with the terms it didn't match to an event...
3151 */
3152static int trace__parse_events_option(const struct option *opt, const char *str,
3153 int unset __maybe_unused)
3154{
3155 struct trace *trace = (struct trace *)opt->value;
3156 const char *s = str;
3157 char *sep = NULL, *lists[2] = { NULL, NULL, };
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003158 int len = strlen(str) + 1, err = -1, list, idx;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003159 char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
3160 char group_name[PATH_MAX];
3161
3162 if (strace_groups_dir == NULL)
3163 return -1;
3164
3165 if (*s == '!') {
3166 ++s;
3167 trace->not_ev_qualifier = true;
3168 }
3169
3170 while (1) {
3171 if ((sep = strchr(s, ',')) != NULL)
3172 *sep = '\0';
3173
3174 list = 0;
Arnaldo Carvalho de Melo27702bc2017-08-31 11:50:04 -03003175 if (syscalltbl__id(trace->sctbl, s) >= 0 ||
3176 syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003177 list = 1;
3178 } else {
3179 path__join(group_name, sizeof(group_name), strace_groups_dir, s);
3180 if (access(group_name, R_OK) == 0)
3181 list = 1;
3182 }
3183
3184 if (lists[list]) {
3185 sprintf(lists[list] + strlen(lists[list]), ",%s", s);
3186 } else {
3187 lists[list] = malloc(len);
3188 if (lists[list] == NULL)
3189 goto out;
3190 strcpy(lists[list], s);
3191 }
3192
3193 if (!sep)
3194 break;
3195
3196 *sep = ',';
3197 s = sep + 1;
3198 }
3199
3200 if (lists[1] != NULL) {
3201 struct strlist_config slist_config = {
3202 .dirname = strace_groups_dir,
3203 };
3204
3205 trace->ev_qualifier = strlist__new(lists[1], &slist_config);
3206 if (trace->ev_qualifier == NULL) {
3207 fputs("Not enough memory to parse event qualifier", trace->output);
3208 goto out;
3209 }
3210
3211 if (trace__validate_ev_qualifier(trace))
3212 goto out;
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003213 trace->trace_syscalls = true;
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003214 }
3215
3216 err = 0;
3217
3218 if (lists[0]) {
3219 struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
3220 "event selector. use 'perf list' to list available events",
3221 parse_events_option);
3222 err = parse_events_option(&o, lists[0], 0);
3223 }
3224out:
3225 if (sep)
3226 *sep = ',';
3227
3228 return err;
3229}
3230
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003231static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
3232{
3233 struct trace *trace = opt->value;
3234
3235 if (!list_empty(&trace->evlist->entries))
3236 return parse_cgroups(opt, str, unset);
3237
3238 trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
3239
3240 return 0;
3241}
3242
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03003243int cmd_trace(int argc, const char **argv)
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003244{
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003245 const char *trace_usage[] = {
Namhyung Kimf15eb532012-10-05 14:02:16 +09003246 "perf trace [<options>] [<command>]",
3247 "perf trace [<options>] -- <command> [<options>]",
David Ahern5e2485b2013-09-28 13:13:01 -06003248 "perf trace record [<options>] [<command>]",
3249 "perf trace record [<options>] -- <command> [<options>]",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003250 NULL
3251 };
3252 struct trace trace = {
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003253 .syscalls = {
3254 . max = -1,
3255 },
3256 .opts = {
3257 .target = {
3258 .uid = UINT_MAX,
3259 .uses_mmap = true,
3260 },
3261 .user_freq = UINT_MAX,
3262 .user_interval = ULLONG_MAX,
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -03003263 .no_buffering = true,
Arnaldo Carvalho de Melo38d54472014-12-12 17:28:32 -03003264 .mmap_pages = UINT_MAX,
Kan Liang9d9cad72015-06-17 09:51:11 -04003265 .proc_map_timeout = 500,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003266 },
Milian Wolff007d66a2015-08-05 16:52:23 -03003267 .output = stderr,
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003268 .show_comm = true,
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003269 .trace_syscalls = false,
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003270 .kernel_syscallchains = false,
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003271 .max_stack = UINT_MAX,
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003272 .max_events = ULONG_MAX,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003273 };
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003274 const char *output_name = NULL;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003275 const struct option trace_options[] = {
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003276 OPT_CALLBACK('e', "event", &trace, "event",
3277 "event/syscall selector. use 'perf list' to list available events",
3278 trace__parse_events_option),
Arnaldo Carvalho de Melo50c95cb2013-09-12 12:35:21 -03003279 OPT_BOOLEAN(0, "comm", &trace.show_comm,
3280 "show the thread COMM next to its id"),
Arnaldo Carvalho de Meloc5227392013-09-27 18:06:19 -03003281 OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
Arnaldo Carvalho de Melo017037f2017-01-09 17:26:26 -03003282 OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
3283 trace__parse_events_option),
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003284 OPT_STRING('o', "output", &output_name, "file", "output file name"),
David Ahern6810fc92013-08-28 22:29:52 -06003285 OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003286 OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
3287 "trace events on existing process id"),
David Ahernac9be8e2013-08-20 11:15:45 -06003288 OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003289 "trace events on existing thread id"),
Arnaldo Carvalho de Melofa0e4ff2015-04-23 11:59:20 -03003290 OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
3291 "pids to filter (by the kernel)", trace__set_filter_pids),
David Ahernac9be8e2013-08-20 11:15:45 -06003292 OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003293 "system-wide collection from all CPUs"),
David Ahernac9be8e2013-08-20 11:15:45 -06003294 OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003295 "list of cpus to monitor"),
David Ahern6810fc92013-08-28 22:29:52 -06003296 OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003297 "child tasks do not inherit counters"),
Jiri Olsa994a1f72013-09-01 12:36:12 +02003298 OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
3299 "number of mmap data pages",
3300 perf_evlist__parse_mmap_pages),
David Ahernac9be8e2013-08-20 11:15:45 -06003301 OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user",
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003302 "user to profile"),
Arnaldo Carvalho de Meloae9ed032012-10-08 09:56:00 -03003303 OPT_CALLBACK(0, "duration", &trace, "float",
3304 "show only events with duration > N.M ms",
3305 trace__set_duration),
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003306 OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
Arnaldo Carvalho de Melo7c304ee02013-08-22 16:49:54 -03003307 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
David Ahern4bb09192013-09-04 12:37:43 -06003308 OPT_BOOLEAN('T', "time", &trace.full_time,
3309 "Show full timestamp, not time relative to first start"),
Arnaldo Carvalho de Melo0a6545b2018-03-29 12:22:59 -03003310 OPT_BOOLEAN(0, "failure", &trace.failure_only,
3311 "Show only syscalls that failed"),
David Ahernfd2eaba2013-11-12 09:31:15 -07003312 OPT_BOOLEAN('s', "summary", &trace.summary_only,
3313 "Show only syscall summary with statistics"),
3314 OPT_BOOLEAN('S', "with-summary", &trace.summary,
3315 "Show all syscalls and summary with statistics"),
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003316 OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
3317 "Trace pagefaults", parse_pagefaults, "maj"),
Stanislav Fomicheve281a962014-06-26 20:14:28 +04003318 OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
Yunlong Songe366a6d2015-04-02 21:47:18 +08003319 OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
Milian Wolff566a0882016-04-08 13:34:15 +02003320 OPT_CALLBACK(0, "call-graph", &trace.opts,
3321 "record_mode[,record_size]", record_callchain_help,
3322 &record_parse_callchain_opt),
Arnaldo Carvalho de Melo44621812016-04-11 15:49:11 -03003323 OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
3324 "Show the kernel callchains on the syscall exit path"),
Arnaldo Carvalho de Melo5067a8c2018-10-18 16:38:27 -03003325 OPT_ULONG(0, "max-events", &trace.max_events,
3326 "Set the maximum number of events to print, exit after that is reached. "),
Arnaldo Carvalho de Melo5cf9c842016-04-15 11:10:31 -03003327 OPT_UINTEGER(0, "min-stack", &trace.min_stack,
3328 "Set the minimum stack depth when parsing the callchain, "
3329 "anything below the specified depth will be ignored."),
Arnaldo Carvalho de Meloc6d4a492016-04-14 18:29:08 -03003330 OPT_UINTEGER(0, "max-stack", &trace.max_stack,
3331 "Set the maximum stack depth when parsing the callchain, "
3332 "anything beyond the specified depth will be ignored. "
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -03003333 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
Arnaldo Carvalho de Melo591421e2018-01-22 11:38:54 -03003334 OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
3335 "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
Kan Liang9d9cad72015-06-17 09:51:11 -04003336 OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
3337 "per thread proc mmap processing timeout in ms"),
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003338 OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
3339 trace__parse_cgroups),
Alexis Berlemonte36b7822016-10-10 07:43:28 +02003340 OPT_UINTEGER('D', "delay", &trace.opts.initial_delay,
3341 "ms to wait before starting measurement after program "
3342 "start"),
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003343 OPT_END()
3344 };
Arnaldo Carvalho de Meloccd62a82016-04-16 09:36:32 -03003345 bool __maybe_unused max_stack_user_set = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003346 bool mmap_pages_user_set = true;
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003347 struct perf_evsel *evsel;
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003348 const char * const trace_subcommands[] = { "record", NULL };
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003349 int err = -1;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003350 char bf[BUFSIZ];
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003351
Arnaldo Carvalho de Melo4d08cb82015-02-24 15:35:55 -03003352 signal(SIGSEGV, sighandler_dump_stack);
3353 signal(SIGFPE, sighandler_dump_stack);
3354
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003355 trace.evlist = perf_evlist__new();
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003356 trace.sctbl = syscalltbl__new();
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003357
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03003358 if (trace.evlist == NULL || trace.sctbl == NULL) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003359 pr_err("Not enough memory to run!\n");
He Kuangff8f6952015-05-11 12:28:36 +00003360 err = -ENOMEM;
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003361 goto out;
3362 }
3363
Yunlong Song6fdd9cb2015-03-18 21:35:57 +08003364 argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
3365 trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
David Ahernfd2eaba2013-11-12 09:31:15 -07003366
Arnaldo Carvalho de Melo9ea42ba2018-03-06 16:30:51 -03003367 if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
3368 usage_with_options_msg(trace_usage, trace_options,
3369 "cgroup monitoring only available in system-wide mode");
3370 }
3371
Arnaldo Carvalho de Melo78e890e2018-08-07 16:19:05 -03003372 evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__");
3373 if (IS_ERR(evsel)) {
3374 bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf));
Arnaldo Carvalho de Meloe0b6d2e2018-08-07 15:40:13 -03003375 pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf);
3376 goto out;
3377 }
3378
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003379 if (evsel)
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003380 trace.syscalls.events.augmented = evsel;
Arnaldo Carvalho de Melod3d1c4bdf52018-08-07 16:21:44 -03003381
Wang Nand7888572016-04-08 15:07:24 +00003382 err = bpf__setup_stdout(trace.evlist);
3383 if (err) {
3384 bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf));
3385 pr_err("ERROR: Setup BPF stdout failed: %s\n", bf);
3386 goto out;
3387 }
3388
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003389 err = -1;
3390
Stanislav Fomichev598d02c2014-06-26 20:14:25 +04003391 if (trace.trace_pgfaults) {
3392 trace.opts.sample_address = true;
3393 trace.opts.sample_time = true;
3394 }
3395
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003396 if (trace.opts.mmap_pages == UINT_MAX)
3397 mmap_pages_user_set = false;
3398
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003399 if (trace.max_stack == UINT_MAX) {
Arnaldo Carvalho de Melo029c75e2018-05-17 16:31:32 -03003400 trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003401 max_stack_user_set = false;
3402 }
3403
3404#ifdef HAVE_DWARF_UNWIND_SUPPORT
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003405 if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003406 record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
Arnaldo Carvalho de Melo75d50112018-01-15 10:39:55 -03003407 }
Arnaldo Carvalho de Melo05614992016-04-15 16:41:19 -03003408#endif
3409
Arnaldo Carvalho de Melo2ddd5c02016-04-18 12:09:08 -03003410 if (callchain_param.enabled) {
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003411 if (!mmap_pages_user_set && geteuid() == 0)
3412 trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
3413
Milian Wolff566a0882016-04-08 13:34:15 +02003414 symbol_conf.use_callchain = true;
Arnaldo Carvalho de Melof3e459d2016-04-15 17:52:34 -03003415 }
Milian Wolff566a0882016-04-08 13:34:15 +02003416
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003417 if (trace.evlist->nr_entries > 0) {
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003418 evlist__set_evsel_handler(trace.evlist, trace__event_handler);
Arnaldo Carvalho de Melod32855f2018-08-02 15:09:24 -03003419 if (evlist__set_syscall_tp_fields(trace.evlist)) {
3420 perror("failed to set syscalls:* tracepoint fields");
3421 goto out;
3422 }
3423 }
Arnaldo Carvalho de Melo14a052d2015-02-03 12:58:57 -03003424
Arnaldo Carvalho de Melob1a9e252018-09-03 16:29:39 -03003425 /*
3426 * If we are augmenting syscalls, then combine what we put in the
3427 * __augmented_syscalls__ BPF map with what is in the
3428 * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
3429 * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
3430 *
3431 * We'll switch to look at two BPF maps, one for sys_enter and the
3432 * other for sys_exit when we start augmenting the sys_exit paths with
3433 * buffers that are being copied from kernel to userspace, think 'read'
3434 * syscall.
3435 */
3436 if (trace.syscalls.events.augmented) {
3437 evsel = trace.syscalls.events.augmented;
3438
3439 if (perf_evsel__init_augmented_syscall_tp(evsel) ||
3440 perf_evsel__init_augmented_syscall_tp_args(evsel))
3441 goto out;
3442 evsel->handler = trace__sys_enter;
3443
3444 evlist__for_each_entry(trace.evlist, evsel) {
3445 if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) {
3446 perf_evsel__init_augmented_syscall_tp(evsel);
3447 perf_evsel__init_augmented_syscall_tp_ret(evsel);
3448 evsel->handler = trace__sys_exit;
3449 }
3450 }
3451 }
3452
Stanislav Fomichev1e28fe02014-06-26 20:14:26 +04003453 if ((argc >= 1) && (strcmp(argv[0], "record") == 0))
3454 return trace__record(&trace, argc-1, &argv[1]);
3455
3456 /* summary_only implies summary option, but don't overwrite summary if set */
3457 if (trace.summary_only)
3458 trace.summary = trace.summary_only;
3459
Arnaldo Carvalho de Melo726f3232015-02-06 10:16:45 +01003460 if (!trace.trace_syscalls && !trace.trace_pgfaults &&
3461 trace.evlist->nr_entries == 0 /* Was --events used? */) {
Arnaldo Carvalho de Melob9128852018-08-01 16:20:28 -03003462 trace.trace_syscalls = true;
Arnaldo Carvalho de Melo59247e32016-04-12 16:05:02 -03003463 }
3464
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003465 if (output_name != NULL) {
3466 err = trace__open_output(&trace, output_name);
3467 if (err < 0) {
3468 perror("failed to create output file");
3469 goto out;
3470 }
3471 }
3472
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003473 err = target__validate(&trace.opts.target);
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003474 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003475 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003476 fprintf(trace.output, "%s", bf);
3477 goto out_close;
Namhyung Kim32caf0d2012-10-05 14:02:13 +09003478 }
3479
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003480 err = target__parse_uid(&trace.opts.target);
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003481 if (err) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003482 target__strerror(&trace.opts.target, err, bf, sizeof(bf));
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003483 fprintf(trace.output, "%s", bf);
3484 goto out_close;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003485 }
3486
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003487 if (!argc && target__none(&trace.opts.target))
Namhyung Kimee761202012-10-05 14:02:14 +09003488 trace.opts.target.system_wide = true;
3489
David Ahern6810fc92013-08-28 22:29:52 -06003490 if (input_name)
3491 err = trace__replay(&trace);
3492 else
3493 err = trace__run(&trace, argc, argv);
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003494
Arnaldo Carvalho de Meloc24ff992013-08-19 12:01:10 -03003495out_close:
3496 if (output_name != NULL)
3497 fclose(trace.output);
3498out:
Arnaldo Carvalho de Melo1302d882012-10-17 17:13:12 -03003499 return err;
Arnaldo Carvalho de Melo514f1c62012-09-26 20:05:56 -03003500}