Arnaldo Carvalho de Melo | a598bb5 | 2015-08-28 12:02:37 -0300 | [diff] [blame] | 1 | /* |
| 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 Richter | 4e31902 | 2013-06-11 17:29:18 +0200 | [diff] [blame] | 19 | #include <traceevent/event-parse.h> |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 20 | #include <api/fs/tracing_path.h> |
Arnaldo Carvalho de Melo | 744fafc | 2018-11-07 10:04:51 -0300 | [diff] [blame] | 21 | #include <bpf/bpf.h> |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 22 | #include "builtin.h" |
Arnaldo Carvalho de Melo | 9ea42ba | 2018-03-06 16:30:51 -0300 | [diff] [blame] | 23 | #include "util/cgroup.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 24 | #include "util/color.h" |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 25 | #include "util/debug.h" |
Hendrik Brueckner | 092bd3c | 2018-01-19 09:56:16 +0100 | [diff] [blame] | 26 | #include "util/env.h" |
Arnaldo Carvalho de Melo | 5ab8c68 | 2017-04-25 15:30:47 -0300 | [diff] [blame] | 27 | #include "util/event.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 28 | #include "util/evlist.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 29 | #include <subcmd/exec-cmd.h> |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 30 | #include "util/machine.h" |
Arnaldo Carvalho de Melo | 9a3993d | 2017-04-18 11:33:48 -0300 | [diff] [blame] | 31 | #include "util/path.h" |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 32 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 33 | #include "util/thread.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 34 | #include <subcmd/parse-options.h> |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 35 | #include "util/strlist.h" |
David Ahern | bdc8966 | 2013-08-28 22:29:53 -0600 | [diff] [blame] | 36 | #include "util/intlist.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 37 | #include "util/thread_map.h" |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 38 | #include "util/stat.h" |
Arnaldo Carvalho de Melo | fd5cead | 2017-03-14 16:19:30 -0300 | [diff] [blame] | 39 | #include "trace/beauty/beauty.h" |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 40 | #include "trace-event.h" |
David Ahern | 9aca7f1 | 2013-12-04 19:41:39 -0700 | [diff] [blame] | 41 | #include "util/parse-events.h" |
Wang Nan | ba50423 | 2016-02-26 09:31:54 +0000 | [diff] [blame] | 42 | #include "util/bpf-loader.h" |
Milian Wolff | 566a088 | 2016-04-08 13:34:15 +0200 | [diff] [blame] | 43 | #include "callchain.h" |
Arnaldo Carvalho de Melo | fea0139 | 2017-04-17 16:23:22 -0300 | [diff] [blame] | 44 | #include "print_binary.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 45 | #include "string2.h" |
Arnaldo Carvalho de Melo | fd0db10 | 2016-04-04 13:32:20 -0300 | [diff] [blame] | 46 | #include "syscalltbl.h" |
Arnaldo Carvalho de Melo | 96c1445 | 2016-05-04 12:47:16 -0300 | [diff] [blame] | 47 | #include "rb_resort.h" |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 48 | |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 49 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 50 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 4208735 | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 51 | #include <poll.h> |
Arnaldo Carvalho de Melo | 9607ad3 | 2017-04-19 15:49:18 -0300 | [diff] [blame] | 52 | #include <signal.h> |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 53 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 54 | #include <string.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 55 | #include <linux/err.h> |
Arnaldo Carvalho de Melo | 997bba8 | 2016-03-30 19:43:32 -0300 | [diff] [blame] | 56 | #include <linux/filter.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 57 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 39878d4 | 2016-03-30 20:02:15 -0300 | [diff] [blame] | 58 | #include <linux/random.h> |
Arnaldo Carvalho de Melo | c6d4a49 | 2016-04-14 18:29:08 -0300 | [diff] [blame] | 59 | #include <linux/stringify.h> |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 60 | #include <linux/time64.h> |
Arnaldo Carvalho de Melo | bafae98 | 2018-01-22 16:42:16 -0300 | [diff] [blame] | 61 | #include <fcntl.h> |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 62 | |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 63 | #include "sane_ctype.h" |
| 64 | |
Arnaldo Carvalho de Melo | c188e7a | 2015-05-14 17:39:03 -0300 | [diff] [blame] | 65 | #ifndef O_CLOEXEC |
| 66 | # define O_CLOEXEC 02000000 |
| 67 | #endif |
| 68 | |
Arnaldo Carvalho de Melo | 83a5169 | 2017-07-13 17:14:11 -0300 | [diff] [blame] | 69 | #ifndef F_LINUX_SPECIFIC_BASE |
| 70 | # define F_LINUX_SPECIFIC_BASE 1024 |
| 71 | #endif |
| 72 | |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 73 | struct trace { |
| 74 | struct perf_tool tool; |
Arnaldo Carvalho de Melo | fd0db10 | 2016-04-04 13:32:20 -0300 | [diff] [blame] | 75 | struct syscalltbl *sctbl; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 76 | struct { |
| 77 | int max; |
| 78 | struct syscall *table; |
| 79 | struct { |
| 80 | struct perf_evsel *sys_enter, |
Arnaldo Carvalho de Melo | d3d1c4bdf5 | 2018-08-07 16:21:44 -0300 | [diff] [blame] | 81 | *sys_exit, |
| 82 | *augmented; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 83 | } events; |
| 84 | } syscalls; |
| 85 | struct record_opts opts; |
| 86 | struct perf_evlist *evlist; |
| 87 | struct machine *host; |
| 88 | struct thread *current; |
Arnaldo Carvalho de Melo | 9ea42ba | 2018-03-06 16:30:51 -0300 | [diff] [blame] | 89 | struct cgroup *cgroup; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 90 | u64 base_time; |
| 91 | FILE *output; |
| 92 | unsigned long nr_events; |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 93 | unsigned long nr_events_printed; |
| 94 | unsigned long max_events; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 95 | struct strlist *ev_qualifier; |
| 96 | struct { |
| 97 | size_t nr; |
| 98 | int *entries; |
| 99 | } ev_qualifier_ids; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 100 | struct { |
| 101 | size_t nr; |
| 102 | pid_t *entries; |
Arnaldo Carvalho de Melo | 744fafc | 2018-11-07 10:04:51 -0300 | [diff] [blame] | 103 | struct bpf_map *map; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 104 | } filter_pids; |
| 105 | double duration_filter; |
| 106 | double runtime_ms; |
| 107 | struct { |
| 108 | u64 vfs_getname, |
| 109 | proc_getname; |
| 110 | } stats; |
Arnaldo Carvalho de Melo | c6d4a49 | 2016-04-14 18:29:08 -0300 | [diff] [blame] | 111 | unsigned int max_stack; |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 112 | unsigned int min_stack; |
Arnaldo Carvalho de Melo | cd26ea6 | 2018-11-03 08:19:56 -0300 | [diff] [blame] | 113 | bool raw_augmented_syscalls; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 114 | bool not_ev_qualifier; |
| 115 | bool live; |
| 116 | bool full_time; |
| 117 | bool sched; |
| 118 | bool multiple_threads; |
| 119 | bool summary; |
| 120 | bool summary_only; |
Arnaldo Carvalho de Melo | 0a6545b | 2018-03-29 12:22:59 -0300 | [diff] [blame] | 121 | bool failure_only; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 122 | bool show_comm; |
Arnaldo Carvalho de Melo | 591421e | 2018-01-22 11:38:54 -0300 | [diff] [blame] | 123 | bool print_sample; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 124 | bool show_tool_stats; |
| 125 | bool trace_syscalls; |
Arnaldo Carvalho de Melo | 4462181 | 2016-04-11 15:49:11 -0300 | [diff] [blame] | 126 | bool kernel_syscallchains; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 127 | bool force; |
| 128 | bool vfs_getname; |
| 129 | int trace_pgfaults; |
| 130 | }; |
Arnaldo Carvalho de Melo | a1c2552 | 2015-06-11 22:47:54 -0300 | [diff] [blame] | 131 | |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 132 | struct tp_field { |
| 133 | int offset; |
| 134 | union { |
| 135 | u64 (*integer)(struct tp_field *field, struct perf_sample *sample); |
| 136 | void *(*pointer)(struct tp_field *field, struct perf_sample *sample); |
| 137 | }; |
| 138 | }; |
| 139 | |
| 140 | #define TP_UINT_FIELD(bits) \ |
| 141 | static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \ |
| 142 | { \ |
David Ahern | 55d43bca | 2015-02-19 15:00:22 -0500 | [diff] [blame] | 143 | u##bits value; \ |
| 144 | memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ |
| 145 | return value; \ |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TP_UINT_FIELD(8); |
| 149 | TP_UINT_FIELD(16); |
| 150 | TP_UINT_FIELD(32); |
| 151 | TP_UINT_FIELD(64); |
| 152 | |
| 153 | #define TP_UINT_FIELD__SWAPPED(bits) \ |
| 154 | static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \ |
| 155 | { \ |
David Ahern | 55d43bca | 2015-02-19 15:00:22 -0500 | [diff] [blame] | 156 | u##bits value; \ |
| 157 | memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \ |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 158 | return bswap_##bits(value);\ |
| 159 | } |
| 160 | |
| 161 | TP_UINT_FIELD__SWAPPED(16); |
| 162 | TP_UINT_FIELD__SWAPPED(32); |
| 163 | TP_UINT_FIELD__SWAPPED(64); |
| 164 | |
Arnaldo Carvalho de Melo | aa823f5 | 2018-08-02 15:03:02 -0300 | [diff] [blame] | 165 | static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap) |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 166 | { |
Arnaldo Carvalho de Melo | aa823f5 | 2018-08-02 15:03:02 -0300 | [diff] [blame] | 167 | field->offset = offset; |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 168 | |
Arnaldo Carvalho de Melo | aa823f5 | 2018-08-02 15:03:02 -0300 | [diff] [blame] | 169 | switch (size) { |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 170 | case 1: |
| 171 | field->integer = tp_field__u8; |
| 172 | break; |
| 173 | case 2: |
| 174 | field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16; |
| 175 | break; |
| 176 | case 4: |
| 177 | field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32; |
| 178 | break; |
| 179 | case 8: |
| 180 | field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64; |
| 181 | break; |
| 182 | default: |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 189 | static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap) |
Arnaldo Carvalho de Melo | aa823f5 | 2018-08-02 15:03:02 -0300 | [diff] [blame] | 190 | { |
| 191 | return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap); |
| 192 | } |
| 193 | |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 194 | static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample) |
| 195 | { |
| 196 | return sample->raw_data + field->offset; |
| 197 | } |
| 198 | |
Arnaldo Carvalho de Melo | aa823f5 | 2018-08-02 15:03:02 -0300 | [diff] [blame] | 199 | static int __tp_field__init_ptr(struct tp_field *field, int offset) |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 200 | { |
Arnaldo Carvalho de Melo | aa823f5 | 2018-08-02 15:03:02 -0300 | [diff] [blame] | 201 | field->offset = offset; |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 202 | field->pointer = tp_field__ptr; |
| 203 | return 0; |
| 204 | } |
| 205 | |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 206 | static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field) |
Arnaldo Carvalho de Melo | aa823f5 | 2018-08-02 15:03:02 -0300 | [diff] [blame] | 207 | { |
| 208 | return __tp_field__init_ptr(field, format_field->offset); |
| 209 | } |
| 210 | |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 211 | struct syscall_tp { |
| 212 | struct tp_field id; |
| 213 | union { |
| 214 | struct tp_field args, ret; |
| 215 | }; |
| 216 | }; |
| 217 | |
| 218 | static int perf_evsel__init_tp_uint_field(struct perf_evsel *evsel, |
| 219 | struct tp_field *field, |
| 220 | const char *name) |
| 221 | { |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 222 | struct tep_format_field *format_field = perf_evsel__field(evsel, name); |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 223 | |
| 224 | if (format_field == NULL) |
| 225 | return -1; |
| 226 | |
| 227 | return tp_field__init_uint(field, format_field, evsel->needs_swap); |
| 228 | } |
| 229 | |
| 230 | #define perf_evsel__init_sc_tp_uint_field(evsel, name) \ |
| 231 | ({ struct syscall_tp *sc = evsel->priv;\ |
| 232 | perf_evsel__init_tp_uint_field(evsel, &sc->name, #name); }) |
| 233 | |
| 234 | static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel, |
| 235 | struct tp_field *field, |
| 236 | const char *name) |
| 237 | { |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 238 | struct tep_format_field *format_field = perf_evsel__field(evsel, name); |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 239 | |
| 240 | if (format_field == NULL) |
| 241 | return -1; |
| 242 | |
| 243 | return tp_field__init_ptr(field, format_field); |
| 244 | } |
| 245 | |
| 246 | #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \ |
| 247 | ({ struct syscall_tp *sc = evsel->priv;\ |
| 248 | perf_evsel__init_tp_ptr_field(evsel, &sc->name, #name); }) |
| 249 | |
| 250 | static void perf_evsel__delete_priv(struct perf_evsel *evsel) |
| 251 | { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 252 | zfree(&evsel->priv); |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 253 | perf_evsel__delete(evsel); |
| 254 | } |
| 255 | |
Arnaldo Carvalho de Melo | d32855f | 2018-08-02 15:09:24 -0300 | [diff] [blame] | 256 | static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel) |
| 257 | { |
| 258 | struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp)); |
| 259 | |
| 260 | if (evsel->priv != NULL) { |
| 261 | if (perf_evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr")) |
| 262 | goto out_delete; |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | return -ENOMEM; |
| 267 | out_delete: |
| 268 | zfree(&evsel->priv); |
| 269 | return -ENOENT; |
| 270 | } |
| 271 | |
Arnaldo Carvalho de Melo | d3d1c4bdf5 | 2018-08-07 16:21:44 -0300 | [diff] [blame] | 272 | static int perf_evsel__init_augmented_syscall_tp(struct perf_evsel *evsel) |
| 273 | { |
| 274 | struct syscall_tp *sc = evsel->priv = malloc(sizeof(struct syscall_tp)); |
| 275 | |
| 276 | if (evsel->priv != NULL) { /* field, sizeof_field, offsetof_field */ |
| 277 | if (__tp_field__init_uint(&sc->id, sizeof(long), sizeof(long long), evsel->needs_swap)) |
| 278 | goto out_delete; |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | return -ENOMEM; |
| 284 | out_delete: |
| 285 | zfree(&evsel->priv); |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
| 289 | static int perf_evsel__init_augmented_syscall_tp_args(struct perf_evsel *evsel) |
| 290 | { |
| 291 | struct syscall_tp *sc = evsel->priv; |
| 292 | |
| 293 | return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)); |
| 294 | } |
| 295 | |
Arnaldo Carvalho de Melo | b1a9e25 | 2018-09-03 16:29:39 -0300 | [diff] [blame] | 296 | static int perf_evsel__init_augmented_syscall_tp_ret(struct perf_evsel *evsel) |
| 297 | { |
| 298 | struct syscall_tp *sc = evsel->priv; |
| 299 | |
| 300 | return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap); |
| 301 | } |
| 302 | |
Arnaldo Carvalho de Melo | 63f11c8 | 2018-08-02 14:27:09 -0300 | [diff] [blame] | 303 | static int perf_evsel__init_raw_syscall_tp(struct perf_evsel *evsel, void *handler) |
Namhyung Kim | 96695d4 | 2013-11-12 08:51:45 -0300 | [diff] [blame] | 304 | { |
| 305 | evsel->priv = malloc(sizeof(struct syscall_tp)); |
| 306 | if (evsel->priv != NULL) { |
| 307 | if (perf_evsel__init_sc_tp_uint_field(evsel, id)) |
| 308 | goto out_delete; |
| 309 | |
| 310 | evsel->handler = handler; |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | return -ENOMEM; |
| 315 | |
| 316 | out_delete: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 317 | zfree(&evsel->priv); |
Namhyung Kim | 96695d4 | 2013-11-12 08:51:45 -0300 | [diff] [blame] | 318 | return -ENOENT; |
| 319 | } |
| 320 | |
Arnaldo Carvalho de Melo | 63f11c8 | 2018-08-02 14:27:09 -0300 | [diff] [blame] | 321 | static struct perf_evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler) |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 322 | { |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 323 | struct perf_evsel *evsel = perf_evsel__newtp("raw_syscalls", direction); |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 324 | |
David Ahern | 9aca7f1 | 2013-12-04 19:41:39 -0700 | [diff] [blame] | 325 | /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */ |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 326 | if (IS_ERR(evsel)) |
David Ahern | 9aca7f1 | 2013-12-04 19:41:39 -0700 | [diff] [blame] | 327 | evsel = perf_evsel__newtp("syscalls", direction); |
| 328 | |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 329 | if (IS_ERR(evsel)) |
| 330 | return NULL; |
| 331 | |
Arnaldo Carvalho de Melo | 63f11c8 | 2018-08-02 14:27:09 -0300 | [diff] [blame] | 332 | if (perf_evsel__init_raw_syscall_tp(evsel, handler)) |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 333 | goto out_delete; |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 334 | |
| 335 | return evsel; |
| 336 | |
| 337 | out_delete: |
| 338 | perf_evsel__delete_priv(evsel); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | #define perf_evsel__sc_tp_uint(evsel, name, sample) \ |
| 343 | ({ struct syscall_tp *fields = evsel->priv; \ |
| 344 | fields->name.integer(&fields->name, sample); }) |
| 345 | |
| 346 | #define perf_evsel__sc_tp_ptr(evsel, name, sample) \ |
| 347 | ({ struct syscall_tp *fields = evsel->priv; \ |
| 348 | fields->name.pointer(&fields->name, sample); }) |
| 349 | |
Arnaldo Carvalho de Melo | 0ae7963 | 2017-07-17 10:31:42 -0300 | [diff] [blame] | 350 | size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, int val) |
| 351 | { |
| 352 | int idx = val - sa->offset; |
Arnaldo Carvalho de Melo | 1f115cb | 2013-09-03 15:50:28 -0300 | [diff] [blame] | 353 | |
Arnaldo Carvalho de Melo | bc972ad | 2018-07-26 15:30:33 -0300 | [diff] [blame] | 354 | if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) |
Arnaldo Carvalho de Melo | 0ae7963 | 2017-07-17 10:31:42 -0300 | [diff] [blame] | 355 | return scnprintf(bf, size, intfmt, val); |
Arnaldo Carvalho de Melo | 1f115cb | 2013-09-03 15:50:28 -0300 | [diff] [blame] | 356 | |
Arnaldo Carvalho de Melo | 0ae7963 | 2017-07-17 10:31:42 -0300 | [diff] [blame] | 357 | return scnprintf(bf, size, "%s", sa->entries[idx]); |
Arnaldo Carvalho de Melo | 03e3adc | 2013-10-08 16:00:21 -0300 | [diff] [blame] | 358 | } |
| 359 | |
Arnaldo Carvalho de Melo | 975b7c2 | 2013-10-08 17:17:43 -0300 | [diff] [blame] | 360 | static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size, |
| 361 | const char *intfmt, |
| 362 | struct syscall_arg *arg) |
Arnaldo Carvalho de Melo | 1f115cb | 2013-09-03 15:50:28 -0300 | [diff] [blame] | 363 | { |
Arnaldo Carvalho de Melo | 0ae7963 | 2017-07-17 10:31:42 -0300 | [diff] [blame] | 364 | return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->val); |
Arnaldo Carvalho de Melo | 1f115cb | 2013-09-03 15:50:28 -0300 | [diff] [blame] | 365 | } |
| 366 | |
Arnaldo Carvalho de Melo | 975b7c2 | 2013-10-08 17:17:43 -0300 | [diff] [blame] | 367 | static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size, |
| 368 | struct syscall_arg *arg) |
| 369 | { |
| 370 | return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg); |
| 371 | } |
| 372 | |
Arnaldo Carvalho de Melo | 1f115cb | 2013-09-03 15:50:28 -0300 | [diff] [blame] | 373 | #define SCA_STRARRAY syscall_arg__scnprintf_strarray |
| 374 | |
Arnaldo Carvalho de Melo | 83a5169 | 2017-07-13 17:14:11 -0300 | [diff] [blame] | 375 | struct strarrays { |
| 376 | int nr_entries; |
| 377 | struct strarray **entries; |
| 378 | }; |
| 379 | |
| 380 | #define DEFINE_STRARRAYS(array) struct strarrays strarrays__##array = { \ |
| 381 | .nr_entries = ARRAY_SIZE(array), \ |
| 382 | .entries = array, \ |
| 383 | } |
| 384 | |
Arnaldo Carvalho de Melo | 274e86f | 2017-07-14 09:38:38 -0300 | [diff] [blame] | 385 | size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size, |
| 386 | struct syscall_arg *arg) |
Arnaldo Carvalho de Melo | 83a5169 | 2017-07-13 17:14:11 -0300 | [diff] [blame] | 387 | { |
| 388 | struct strarrays *sas = arg->parm; |
| 389 | int i; |
| 390 | |
| 391 | for (i = 0; i < sas->nr_entries; ++i) { |
| 392 | struct strarray *sa = sas->entries[i]; |
| 393 | int idx = arg->val - sa->offset; |
| 394 | |
| 395 | if (idx >= 0 && idx < sa->nr_entries) { |
| 396 | if (sa->entries[idx] == NULL) |
| 397 | break; |
| 398 | return scnprintf(bf, size, "%s", sa->entries[idx]); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return scnprintf(bf, size, "%d", arg->val); |
| 403 | } |
| 404 | |
Arnaldo Carvalho de Melo | 48e1f91 | 2016-07-05 14:43:27 -0300 | [diff] [blame] | 405 | #ifndef AT_FDCWD |
| 406 | #define AT_FDCWD -100 |
| 407 | #endif |
| 408 | |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 409 | static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size, |
| 410 | struct syscall_arg *arg) |
| 411 | { |
| 412 | int fd = arg->val; |
| 413 | |
| 414 | if (fd == AT_FDCWD) |
| 415 | return scnprintf(bf, size, "CWD"); |
| 416 | |
| 417 | return syscall_arg__scnprintf_fd(bf, size, arg); |
| 418 | } |
| 419 | |
| 420 | #define SCA_FDAT syscall_arg__scnprintf_fd_at |
| 421 | |
| 422 | static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, |
| 423 | struct syscall_arg *arg); |
| 424 | |
| 425 | #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd |
| 426 | |
Arnaldo Carvalho de Melo | 2c2b162 | 2017-07-14 10:19:18 -0300 | [diff] [blame] | 427 | size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg) |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 428 | { |
Arnaldo Carvalho de Melo | 01533e9 | 2013-09-03 12:20:12 -0300 | [diff] [blame] | 429 | return scnprintf(bf, size, "%#lx", arg->val); |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 430 | } |
| 431 | |
Arnaldo Carvalho de Melo | 2c2b162 | 2017-07-14 10:19:18 -0300 | [diff] [blame] | 432 | size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg) |
Arnaldo Carvalho de Melo | a1c2552 | 2015-06-11 22:47:54 -0300 | [diff] [blame] | 433 | { |
| 434 | return scnprintf(bf, size, "%d", arg->val); |
| 435 | } |
| 436 | |
Arnaldo Carvalho de Melo | 5dde91e | 2017-07-14 10:34:16 -0300 | [diff] [blame] | 437 | size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg) |
| 438 | { |
| 439 | return scnprintf(bf, size, "%ld", arg->val); |
| 440 | } |
| 441 | |
Arnaldo Carvalho de Melo | 729a784 | 2015-10-29 11:48:18 -0300 | [diff] [blame] | 442 | static const char *bpf_cmd[] = { |
| 443 | "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM", |
| 444 | "MAP_GET_NEXT_KEY", "PROG_LOAD", |
| 445 | }; |
| 446 | static DEFINE_STRARRAY(bpf_cmd); |
| 447 | |
Arnaldo Carvalho de Melo | 03e3adc | 2013-10-08 16:00:21 -0300 | [diff] [blame] | 448 | static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", }; |
| 449 | static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, 1); |
Arnaldo Carvalho de Melo | eac032c | 2013-09-20 11:27:32 -0300 | [diff] [blame] | 450 | |
Arnaldo Carvalho de Melo | 1f115cb | 2013-09-03 15:50:28 -0300 | [diff] [blame] | 451 | static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", }; |
| 452 | static DEFINE_STRARRAY(itimers); |
| 453 | |
Arnaldo Carvalho de Melo | b62bee1 | 2015-08-11 11:05:36 -0300 | [diff] [blame] | 454 | static const char *keyctl_options[] = { |
| 455 | "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN", |
| 456 | "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ", |
| 457 | "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT", |
| 458 | "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT", |
| 459 | "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT", |
| 460 | }; |
| 461 | static DEFINE_STRARRAY(keyctl_options); |
| 462 | |
Arnaldo Carvalho de Melo | efe6b88 | 2013-09-03 16:15:12 -0300 | [diff] [blame] | 463 | static const char *whences[] = { "SET", "CUR", "END", |
| 464 | #ifdef SEEK_DATA |
| 465 | "DATA", |
| 466 | #endif |
| 467 | #ifdef SEEK_HOLE |
| 468 | "HOLE", |
| 469 | #endif |
| 470 | }; |
| 471 | static DEFINE_STRARRAY(whences); |
Arnaldo Carvalho de Melo | f9da0b0 | 2013-09-02 13:46:44 -0300 | [diff] [blame] | 472 | |
Arnaldo Carvalho de Melo | 80f587d | 2013-09-03 16:28:58 -0300 | [diff] [blame] | 473 | static const char *fcntl_cmds[] = { |
| 474 | "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK", |
Arnaldo Carvalho de Melo | e000e5e | 2017-07-13 13:07:00 -0300 | [diff] [blame] | 475 | "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64", |
| 476 | "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX", |
| 477 | "GETOWNER_UIDS", |
Arnaldo Carvalho de Melo | 80f587d | 2013-09-03 16:28:58 -0300 | [diff] [blame] | 478 | }; |
| 479 | static DEFINE_STRARRAY(fcntl_cmds); |
| 480 | |
Arnaldo Carvalho de Melo | 83a5169 | 2017-07-13 17:14:11 -0300 | [diff] [blame] | 481 | static const char *fcntl_linux_specific_cmds[] = { |
| 482 | "SETLEASE", "GETLEASE", "NOTIFY", [5] = "CANCELLK", "DUPFD_CLOEXEC", |
| 483 | "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS", |
Arnaldo Carvalho de Melo | 64e4561 | 2017-07-13 17:34:46 -0300 | [diff] [blame] | 484 | "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT", |
Arnaldo Carvalho de Melo | 83a5169 | 2017-07-13 17:14:11 -0300 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, F_LINUX_SPECIFIC_BASE); |
| 488 | |
| 489 | static struct strarray *fcntl_cmds_arrays[] = { |
| 490 | &strarray__fcntl_cmds, |
| 491 | &strarray__fcntl_linux_specific_cmds, |
| 492 | }; |
| 493 | |
| 494 | static DEFINE_STRARRAYS(fcntl_cmds_arrays); |
| 495 | |
Arnaldo Carvalho de Melo | c045bf0 | 2013-09-04 11:52:33 -0300 | [diff] [blame] | 496 | static const char *rlimit_resources[] = { |
| 497 | "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE", |
| 498 | "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO", |
| 499 | "RTTIME", |
| 500 | }; |
| 501 | static DEFINE_STRARRAY(rlimit_resources); |
| 502 | |
Arnaldo Carvalho de Melo | eb5b1b1 | 2013-09-03 16:37:46 -0300 | [diff] [blame] | 503 | static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", }; |
| 504 | static DEFINE_STRARRAY(sighow); |
| 505 | |
David Ahern | 4f8c1b7 | 2013-09-22 19:45:00 -0600 | [diff] [blame] | 506 | static const char *clockid[] = { |
| 507 | "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID", |
Arnaldo Carvalho de Melo | 28ebb87 | 2015-08-11 10:38:38 -0300 | [diff] [blame] | 508 | "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME", |
| 509 | "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI" |
David Ahern | 4f8c1b7 | 2013-09-22 19:45:00 -0600 | [diff] [blame] | 510 | }; |
| 511 | static DEFINE_STRARRAY(clockid); |
| 512 | |
Arnaldo Carvalho de Melo | 5110899 | 2013-09-04 11:42:27 -0300 | [diff] [blame] | 513 | static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size, |
| 514 | struct syscall_arg *arg) |
| 515 | { |
| 516 | size_t printed = 0; |
| 517 | int mode = arg->val; |
| 518 | |
| 519 | if (mode == F_OK) /* 0 */ |
| 520 | return scnprintf(bf, size, "F"); |
| 521 | #define P_MODE(n) \ |
| 522 | if (mode & n##_OK) { \ |
| 523 | printed += scnprintf(bf + printed, size - printed, "%s", #n); \ |
| 524 | mode &= ~n##_OK; \ |
| 525 | } |
| 526 | |
| 527 | P_MODE(R); |
| 528 | P_MODE(W); |
| 529 | P_MODE(X); |
| 530 | #undef P_MODE |
| 531 | |
| 532 | if (mode) |
| 533 | printed += scnprintf(bf + printed, size - printed, "|%#x", mode); |
| 534 | |
| 535 | return printed; |
| 536 | } |
| 537 | |
| 538 | #define SCA_ACCMODE syscall_arg__scnprintf_access_mode |
| 539 | |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 540 | static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, |
| 541 | struct syscall_arg *arg); |
| 542 | |
| 543 | #define SCA_FILENAME syscall_arg__scnprintf_filename |
| 544 | |
Arnaldo Carvalho de Melo | 46cce19 | 2013-09-23 12:52:04 -0300 | [diff] [blame] | 545 | static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size, |
| 546 | struct syscall_arg *arg) |
| 547 | { |
| 548 | int printed = 0, flags = arg->val; |
| 549 | |
| 550 | #define P_FLAG(n) \ |
| 551 | if (flags & O_##n) { \ |
| 552 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ |
| 553 | flags &= ~O_##n; \ |
| 554 | } |
| 555 | |
| 556 | P_FLAG(CLOEXEC); |
| 557 | P_FLAG(NONBLOCK); |
| 558 | #undef P_FLAG |
| 559 | |
| 560 | if (flags) |
| 561 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); |
| 562 | |
| 563 | return printed; |
| 564 | } |
| 565 | |
| 566 | #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags |
| 567 | |
Arnaldo Carvalho de Melo | a355a61 | 2016-04-13 11:55:18 -0300 | [diff] [blame] | 568 | #ifndef GRND_NONBLOCK |
| 569 | #define GRND_NONBLOCK 0x0001 |
| 570 | #endif |
| 571 | #ifndef GRND_RANDOM |
| 572 | #define GRND_RANDOM 0x0002 |
| 573 | #endif |
| 574 | |
Arnaldo Carvalho de Melo | 39878d4 | 2016-03-30 20:02:15 -0300 | [diff] [blame] | 575 | static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size, |
| 576 | struct syscall_arg *arg) |
| 577 | { |
| 578 | int printed = 0, flags = arg->val; |
| 579 | |
| 580 | #define P_FLAG(n) \ |
| 581 | if (flags & GRND_##n) { \ |
| 582 | printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "|" : "", #n); \ |
| 583 | flags &= ~GRND_##n; \ |
| 584 | } |
| 585 | |
| 586 | P_FLAG(RANDOM); |
| 587 | P_FLAG(NONBLOCK); |
| 588 | #undef P_FLAG |
| 589 | |
| 590 | if (flags) |
| 591 | printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags); |
| 592 | |
| 593 | return printed; |
| 594 | } |
| 595 | |
| 596 | #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags |
| 597 | |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 598 | #define STRARRAY(name, array) \ |
| 599 | { .scnprintf = SCA_STRARRAY, \ |
| 600 | .parm = &strarray__##array, } |
Arnaldo Carvalho de Melo | 453350d | 2013-09-20 12:13:37 -0300 | [diff] [blame] | 601 | |
Hendrik Brueckner | 092bd3c | 2018-01-19 09:56:16 +0100 | [diff] [blame] | 602 | #include "trace/beauty/arch_errno_names.c" |
Arnaldo Carvalho de Melo | ea8dc3c | 2016-04-13 12:10:19 -0300 | [diff] [blame] | 603 | #include "trace/beauty/eventfd.c" |
Arnaldo Carvalho de Melo | d5d71e8 | 2016-05-06 12:45:25 -0300 | [diff] [blame] | 604 | #include "trace/beauty/futex_op.c" |
Arnaldo Carvalho de Melo | 3258abe | 2018-01-22 12:56:59 -0300 | [diff] [blame] | 605 | #include "trace/beauty/futex_val3.c" |
Arnaldo Carvalho de Melo | df4cb16 | 2016-04-13 12:05:44 -0300 | [diff] [blame] | 606 | #include "trace/beauty/mmap.c" |
Arnaldo Carvalho de Melo | ba2f22c | 2016-04-07 12:05:51 -0300 | [diff] [blame] | 607 | #include "trace/beauty/mode_t.c" |
Arnaldo Carvalho de Melo | a30e625 | 2016-04-27 19:01:52 -0300 | [diff] [blame] | 608 | #include "trace/beauty/msg_flags.c" |
Arnaldo Carvalho de Melo | 8f48df6 | 2016-05-06 10:02:32 -0300 | [diff] [blame] | 609 | #include "trace/beauty/open_flags.c" |
Arnaldo Carvalho de Melo | 62de344 | 2016-04-26 11:03:03 -0300 | [diff] [blame] | 610 | #include "trace/beauty/perf_event_open.c" |
Arnaldo Carvalho de Melo | d5d71e8 | 2016-05-06 12:45:25 -0300 | [diff] [blame] | 611 | #include "trace/beauty/pid.c" |
Arnaldo Carvalho de Melo | a3bca91 | 2016-04-06 12:51:33 -0300 | [diff] [blame] | 612 | #include "trace/beauty/sched_policy.c" |
Arnaldo Carvalho de Melo | f5cd95e | 2016-05-11 10:32:20 -0300 | [diff] [blame] | 613 | #include "trace/beauty/seccomp.c" |
Arnaldo Carvalho de Melo | 12199d8 | 2016-05-06 09:58:02 -0300 | [diff] [blame] | 614 | #include "trace/beauty/signum.c" |
Arnaldo Carvalho de Melo | bbf86c4 | 2016-04-14 13:53:10 -0300 | [diff] [blame] | 615 | #include "trace/beauty/socket_type.c" |
Arnaldo Carvalho de Melo | 7206b90 | 2016-04-06 14:11:36 -0300 | [diff] [blame] | 616 | #include "trace/beauty/waitid_options.c" |
Arnaldo Carvalho de Melo | a3bca91 | 2016-04-06 12:51:33 -0300 | [diff] [blame] | 617 | |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 618 | struct syscall_arg_fmt { |
| 619 | size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg); |
Arnaldo Carvalho de Melo | 496fd34 | 2018-10-25 16:09:47 -0300 | [diff] [blame] | 620 | unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val); |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 621 | void *parm; |
Arnaldo Carvalho de Melo | c51bdfe | 2017-07-19 15:47:30 -0300 | [diff] [blame] | 622 | const char *name; |
Arnaldo Carvalho de Melo | d47737d | 2017-07-17 15:59:03 -0300 | [diff] [blame] | 623 | bool show_zero; |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 624 | }; |
| 625 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 626 | static struct syscall_fmt { |
| 627 | const char *name; |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 628 | const char *alias; |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 629 | struct syscall_arg_fmt arg[6]; |
Arnaldo Carvalho de Melo | 332337d | 2017-07-19 15:08:14 -0300 | [diff] [blame] | 630 | u8 nr_args; |
Arnaldo Carvalho de Melo | 11c8e39 | 2016-04-06 14:33:07 -0300 | [diff] [blame] | 631 | bool errpid; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 632 | bool timeout; |
Arnaldo Carvalho de Melo | 04b3472 | 2013-08-26 11:36:30 -0300 | [diff] [blame] | 633 | bool hexret; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 634 | } syscall_fmts[] = { |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 635 | { .name = "access", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 636 | .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, }, |
Arnaldo Carvalho de Melo | 02ef288 | 2018-08-28 17:03:53 -0300 | [diff] [blame] | 637 | { .name = "bind", |
| 638 | .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* umyaddr */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 639 | { .name = "bpf", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 640 | .arg = { [0] = STRARRAY(cmd, bpf_cmd), }, }, |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 641 | { .name = "brk", .hexret = true, |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 642 | .arg = { [0] = { .scnprintf = SCA_HEX, /* brk */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 643 | { .name = "clock_gettime", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 644 | .arg = { [0] = STRARRAY(clk_id, clockid), }, }, |
Arnaldo Carvalho de Melo | 33396a3 | 2017-07-19 16:15:17 -0300 | [diff] [blame] | 645 | { .name = "clone", .errpid = true, .nr_args = 5, |
| 646 | .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, }, |
| 647 | [1] = { .name = "child_stack", .scnprintf = SCA_HEX, }, |
| 648 | [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, }, |
| 649 | [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, }, |
| 650 | [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 651 | { .name = "close", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 652 | .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, }, |
Arnaldo Carvalho de Melo | d5a7e66 | 2018-08-28 16:24:44 -0300 | [diff] [blame] | 653 | { .name = "connect", |
| 654 | .arg = { [1] = { .scnprintf = SCA_SOCKADDR, /* servaddr */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 655 | { .name = "epoll_ctl", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 656 | .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 657 | { .name = "eventfd2", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 658 | .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 659 | { .name = "fchmodat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 660 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 661 | { .name = "fchownat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 662 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 663 | { .name = "fcntl", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 664 | .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */ |
Arnaldo Carvalho de Melo | 39cc355 | 2017-07-17 16:02:52 -0300 | [diff] [blame] | 665 | .parm = &strarrays__fcntl_cmds_arrays, |
| 666 | .show_zero = true, }, |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 667 | [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 668 | { .name = "flock", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 669 | .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 670 | { .name = "fstat", .alias = "newfstat", }, |
| 671 | { .name = "fstatat", .alias = "newfstatat", }, |
| 672 | { .name = "futex", |
Arnaldo Carvalho de Melo | 3258abe | 2018-01-22 12:56:59 -0300 | [diff] [blame] | 673 | .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ }, |
| 674 | [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 675 | { .name = "futimesat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 676 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 677 | { .name = "getitimer", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 678 | .arg = { [0] = STRARRAY(which, itimers), }, }, |
Arnaldo Carvalho de Melo | c65f107 | 2016-04-06 14:55:18 -0300 | [diff] [blame] | 679 | { .name = "getpid", .errpid = true, }, |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 680 | { .name = "getpgid", .errpid = true, }, |
Arnaldo Carvalho de Melo | c65f107 | 2016-04-06 14:55:18 -0300 | [diff] [blame] | 681 | { .name = "getppid", .errpid = true, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 682 | { .name = "getrandom", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 683 | .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 684 | { .name = "getrlimit", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 685 | .arg = { [0] = STRARRAY(resource, rlimit_resources), }, }, |
Arnaldo Carvalho de Melo | 2d1073d | 2018-01-09 12:03:47 -0300 | [diff] [blame] | 686 | { .name = "gettid", .errpid = true, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 687 | { .name = "ioctl", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 688 | .arg = { |
Arnaldo Carvalho de Melo | 844ae5b | 2014-02-10 14:09:48 -0300 | [diff] [blame] | 689 | #if defined(__i386__) || defined(__x86_64__) |
| 690 | /* |
| 691 | * FIXME: Make this available to all arches. |
| 692 | */ |
Arnaldo Carvalho de Melo | 1cc47f2d | 2017-07-31 13:20:14 -0300 | [diff] [blame] | 693 | [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ }, |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 694 | [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, }, |
Arnaldo Carvalho de Melo | 844ae5b | 2014-02-10 14:09:48 -0300 | [diff] [blame] | 695 | #else |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 696 | [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, }, |
Arnaldo Carvalho de Melo | 844ae5b | 2014-02-10 14:09:48 -0300 | [diff] [blame] | 697 | #endif |
Arnaldo Carvalho de Melo | 1de3038 | 2017-10-31 11:32:23 -0300 | [diff] [blame] | 698 | { .name = "kcmp", .nr_args = 5, |
| 699 | .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, }, |
| 700 | [1] = { .name = "pid2", .scnprintf = SCA_PID, }, |
| 701 | [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, }, |
| 702 | [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, }, |
| 703 | [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 704 | { .name = "keyctl", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 705 | .arg = { [0] = STRARRAY(option, keyctl_options), }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 706 | { .name = "kill", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 707 | .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 708 | { .name = "linkat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 709 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 710 | { .name = "lseek", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 711 | .arg = { [2] = STRARRAY(whence, whences), }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 712 | { .name = "lstat", .alias = "newlstat", }, |
| 713 | { .name = "madvise", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 714 | .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, |
| 715 | [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 716 | { .name = "mkdirat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 717 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 718 | { .name = "mknodat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 719 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 720 | { .name = "mlock", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 721 | .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 722 | { .name = "mlockall", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 723 | .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, }, |
Arnaldo Carvalho de Melo | beccb2b | 2013-08-26 12:29:38 -0300 | [diff] [blame] | 724 | { .name = "mmap", .hexret = true, |
Jiri Olsa | 5426566 | 2017-05-31 13:35:57 +0200 | [diff] [blame] | 725 | /* The standard mmap maps to old_mmap on s390x */ |
| 726 | #if defined(__s390x__) |
| 727 | .alias = "old_mmap", |
| 728 | #endif |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 729 | .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, |
| 730 | [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, |
| 731 | [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 73d141a | 2018-10-25 15:18:06 -0300 | [diff] [blame] | 732 | { .name = "mount", |
Arnaldo Carvalho de Melo | 23c07a2 | 2018-10-26 13:51:45 -0300 | [diff] [blame] | 733 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* dev_name */ }, |
| 734 | [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */ |
Arnaldo Carvalho de Melo | 73d141a | 2018-10-25 15:18:06 -0300 | [diff] [blame] | 735 | .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 736 | { .name = "mprotect", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 737 | .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, |
| 738 | [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 739 | { .name = "mq_unlink", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 740 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* u_name */ }, }, }, |
Arnaldo Carvalho de Melo | ae68538 | 2013-08-20 17:44:42 -0300 | [diff] [blame] | 741 | { .name = "mremap", .hexret = true, |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 742 | .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, |
| 743 | [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, |
| 744 | [4] = { .scnprintf = SCA_HEX, /* new_addr */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 745 | { .name = "munlock", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 746 | .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 747 | { .name = "munmap", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 748 | .arg = { [0] = { .scnprintf = SCA_HEX, /* addr */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 749 | { .name = "name_to_handle_at", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 750 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 751 | { .name = "newfstatat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 752 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 753 | { .name = "open", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 754 | .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 755 | { .name = "open_by_handle_at", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 756 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, |
| 757 | [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 758 | { .name = "openat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 759 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, |
| 760 | [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 761 | { .name = "perf_event_open", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 762 | .arg = { [2] = { .scnprintf = SCA_INT, /* cpu */ }, |
| 763 | [3] = { .scnprintf = SCA_FD, /* group_fd */ }, |
| 764 | [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 765 | { .name = "pipe2", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 766 | .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 83bc9c37 | 2017-08-28 11:47:11 -0300 | [diff] [blame] | 767 | { .name = "pkey_alloc", |
| 768 | .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, }, |
| 769 | { .name = "pkey_free", |
| 770 | .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, }, |
| 771 | { .name = "pkey_mprotect", |
| 772 | .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ }, |
| 773 | [2] = { .scnprintf = SCA_MMAP_PROT, /* prot */ }, |
| 774 | [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 775 | { .name = "poll", .timeout = true, }, |
| 776 | { .name = "ppoll", .timeout = true, }, |
Arnaldo Carvalho de Melo | d688d03 | 2017-10-26 15:19:35 -0300 | [diff] [blame] | 777 | { .name = "prctl", .alias = "arch_prctl", |
| 778 | .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */ }, |
| 779 | [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ }, |
| 780 | [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 781 | { .name = "pread", .alias = "pread64", }, |
| 782 | { .name = "preadv", .alias = "pread", }, |
| 783 | { .name = "prlimit64", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 784 | .arg = { [1] = STRARRAY(resource, rlimit_resources), }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 785 | { .name = "pwrite", .alias = "pwrite64", }, |
| 786 | { .name = "readlinkat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 787 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 788 | { .name = "recvfrom", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 789 | .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 790 | { .name = "recvmmsg", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 791 | .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 792 | { .name = "recvmsg", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 793 | .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 794 | { .name = "renameat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 795 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 796 | { .name = "rt_sigaction", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 797 | .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 798 | { .name = "rt_sigprocmask", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 799 | .arg = { [0] = STRARRAY(how, sighow), }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 800 | { .name = "rt_sigqueueinfo", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 801 | .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 802 | { .name = "rt_tgsigqueueinfo", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 803 | .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 804 | { .name = "sched_setscheduler", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 805 | .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 806 | { .name = "seccomp", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 807 | .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ }, |
| 808 | [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 809 | { .name = "select", .timeout = true, }, |
| 810 | { .name = "sendmmsg", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 811 | .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 812 | { .name = "sendmsg", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 813 | .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 814 | { .name = "sendto", |
Arnaldo Carvalho de Melo | 6ebb686 | 2018-08-29 08:47:08 -0300 | [diff] [blame] | 815 | .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, |
| 816 | [4] = { .scnprintf = SCA_SOCKADDR, /* addr */ }, }, }, |
Arnaldo Carvalho de Melo | c65f107 | 2016-04-06 14:55:18 -0300 | [diff] [blame] | 817 | { .name = "set_tid_address", .errpid = true, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 818 | { .name = "setitimer", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 819 | .arg = { [0] = STRARRAY(which, itimers), }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 820 | { .name = "setrlimit", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 821 | .arg = { [0] = STRARRAY(resource, rlimit_resources), }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 822 | { .name = "socket", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 823 | .arg = { [0] = STRARRAY(family, socket_families), |
Arnaldo Carvalho de Melo | 162d3ed | 2018-07-26 09:26:13 -0300 | [diff] [blame] | 824 | [1] = { .scnprintf = SCA_SK_TYPE, /* type */ }, |
| 825 | [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 826 | { .name = "socketpair", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 827 | .arg = { [0] = STRARRAY(family, socket_families), |
Arnaldo Carvalho de Melo | 162d3ed | 2018-07-26 09:26:13 -0300 | [diff] [blame] | 828 | [1] = { .scnprintf = SCA_SK_TYPE, /* type */ }, |
| 829 | [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 830 | { .name = "stat", .alias = "newstat", }, |
| 831 | { .name = "statx", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 832 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ }, |
| 833 | [2] = { .scnprintf = SCA_STATX_FLAGS, /* flags */ } , |
| 834 | [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 835 | { .name = "swapoff", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 836 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 837 | { .name = "swapon", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 838 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* specialfile */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 839 | { .name = "symlinkat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 840 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 841 | { .name = "tgkill", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 842 | .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 843 | { .name = "tkill", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 844 | .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, }, |
Arnaldo Carvalho de Melo | 476c92ca | 2018-10-26 13:23:25 -0300 | [diff] [blame] | 845 | { .name = "umount2", .alias = "umount", |
| 846 | .arg = { [0] = { .scnprintf = SCA_FILENAME, /* name */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 847 | { .name = "uname", .alias = "newuname", }, |
| 848 | { .name = "unlinkat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 849 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, }, |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 850 | { .name = "utimensat", |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 851 | .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, }, |
Arnaldo Carvalho de Melo | 11c8e39 | 2016-04-06 14:33:07 -0300 | [diff] [blame] | 852 | { .name = "wait4", .errpid = true, |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 853 | .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, }, |
Arnaldo Carvalho de Melo | 11c8e39 | 2016-04-06 14:33:07 -0300 | [diff] [blame] | 854 | { .name = "waitid", .errpid = true, |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 855 | .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, }, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 856 | }; |
| 857 | |
| 858 | static int syscall_fmt__cmp(const void *name, const void *fmtp) |
| 859 | { |
| 860 | const struct syscall_fmt *fmt = fmtp; |
| 861 | return strcmp(name, fmt->name); |
| 862 | } |
| 863 | |
| 864 | static struct syscall_fmt *syscall_fmt__find(const char *name) |
| 865 | { |
| 866 | const int nmemb = ARRAY_SIZE(syscall_fmts); |
| 867 | return bsearch(name, syscall_fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp); |
| 868 | } |
| 869 | |
Arnaldo Carvalho de Melo | f932184 | 2018-10-25 17:24:45 -0300 | [diff] [blame] | 870 | static struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias) |
| 871 | { |
| 872 | int i, nmemb = ARRAY_SIZE(syscall_fmts); |
| 873 | |
| 874 | for (i = 0; i < nmemb; ++i) { |
| 875 | if (syscall_fmts[i].alias && strcmp(syscall_fmts[i].alias, alias) == 0) |
| 876 | return &syscall_fmts[i]; |
| 877 | } |
| 878 | |
| 879 | return NULL; |
| 880 | } |
| 881 | |
Arnaldo Carvalho de Melo | 6a648b5 | 2018-08-02 10:30:08 -0300 | [diff] [blame] | 882 | /* |
| 883 | * is_exit: is this "exit" or "exit_group"? |
| 884 | * is_open: is this "open" or "openat"? To associate the fd returned in sys_exit with the pathname in sys_enter. |
Arnaldo Carvalho de Melo | 7a983a0 | 2018-08-20 17:58:17 -0300 | [diff] [blame] | 885 | * args_size: sum of the sizes of the syscall arguments, anything after that is augmented stuff: pathname for openat, etc. |
Arnaldo Carvalho de Melo | 6a648b5 | 2018-08-02 10:30:08 -0300 | [diff] [blame] | 886 | */ |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 887 | struct syscall { |
Tzvetomir Stoyanov | 97fbf3f | 2018-11-30 10:44:07 -0500 | [diff] [blame] | 888 | struct tep_event *tp_format; |
Arnaldo Carvalho de Melo | f208bd8 | 2015-03-20 17:46:53 -0300 | [diff] [blame] | 889 | int nr_args; |
Arnaldo Carvalho de Melo | 7a983a0 | 2018-08-20 17:58:17 -0300 | [diff] [blame] | 890 | int args_size; |
Arnaldo Carvalho de Melo | 6a648b5 | 2018-08-02 10:30:08 -0300 | [diff] [blame] | 891 | bool is_exit; |
| 892 | bool is_open; |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 893 | struct tep_format_field *args; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 894 | const char *name; |
| 895 | struct syscall_fmt *fmt; |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 896 | struct syscall_arg_fmt *arg_fmt; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 897 | }; |
| 898 | |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 899 | /* |
| 900 | * We need to have this 'calculated' boolean because in some cases we really |
| 901 | * don't know what is the duration of a syscall, for instance, when we start |
| 902 | * a session and some threads are waiting for a syscall to finish, say 'poll', |
| 903 | * in which case all we can do is to print "( ? ) for duration and for the |
| 904 | * start timestamp. |
| 905 | */ |
| 906 | static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp) |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 907 | { |
| 908 | double duration = (double)t / NSEC_PER_MSEC; |
| 909 | size_t printed = fprintf(fp, "("); |
| 910 | |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 911 | if (!calculated) |
Arnaldo Carvalho de Melo | 522283f | 2018-01-22 11:42:11 -0300 | [diff] [blame] | 912 | printed += fprintf(fp, " "); |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 913 | else if (duration >= 1.0) |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 914 | printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration); |
| 915 | else if (duration >= 0.01) |
| 916 | printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration); |
| 917 | else |
| 918 | printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 919 | return printed + fprintf(fp, "): "); |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 920 | } |
| 921 | |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 922 | /** |
| 923 | * filename.ptr: The filename char pointer that will be vfs_getname'd |
| 924 | * filename.entry_str_pos: Where to insert the string translated from |
| 925 | * filename.ptr by the vfs_getname tracepoint/kprobe. |
Arnaldo Carvalho de Melo | 84486ca | 2017-07-14 11:35:03 -0300 | [diff] [blame] | 926 | * ret_scnprintf: syscall args may set this to a different syscall return |
| 927 | * formatter, for instance, fcntl may return fds, file flags, etc. |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 928 | */ |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 929 | struct thread_trace { |
| 930 | u64 entry_time; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 931 | bool entry_pending; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 932 | unsigned long nr_events; |
Stanislav Fomichev | a2ea67d | 2014-07-08 22:05:16 +0400 | [diff] [blame] | 933 | unsigned long pfmaj, pfmin; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 934 | char *entry_str; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 935 | double runtime_ms; |
Arnaldo Carvalho de Melo | 7ee5743 | 2017-07-14 15:16:54 -0300 | [diff] [blame] | 936 | size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg); |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 937 | struct { |
| 938 | unsigned long ptr; |
Arnaldo Carvalho de Melo | 7f4f800 | 2015-08-14 13:16:27 -0300 | [diff] [blame] | 939 | short int entry_str_pos; |
| 940 | bool pending_open; |
| 941 | unsigned int namelen; |
| 942 | char *name; |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 943 | } filename; |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 944 | struct { |
| 945 | int max; |
| 946 | char **table; |
| 947 | } paths; |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 948 | |
| 949 | struct intlist *syscall_stats; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 950 | }; |
| 951 | |
| 952 | static struct thread_trace *thread_trace__new(void) |
| 953 | { |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 954 | struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace)); |
| 955 | |
| 956 | if (ttrace) |
| 957 | ttrace->paths.max = -1; |
| 958 | |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 959 | ttrace->syscall_stats = intlist__new(NULL); |
| 960 | |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 961 | return ttrace; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 962 | } |
| 963 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 964 | static struct thread_trace *thread__trace(struct thread *thread, FILE *fp) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 965 | { |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 966 | struct thread_trace *ttrace; |
| 967 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 968 | if (thread == NULL) |
| 969 | goto fail; |
| 970 | |
Namhyung Kim | 89dceb2 | 2014-10-06 09:46:03 +0900 | [diff] [blame] | 971 | if (thread__priv(thread) == NULL) |
| 972 | thread__set_priv(thread, thread_trace__new()); |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 973 | |
Namhyung Kim | 89dceb2 | 2014-10-06 09:46:03 +0900 | [diff] [blame] | 974 | if (thread__priv(thread) == NULL) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 975 | goto fail; |
| 976 | |
Namhyung Kim | 89dceb2 | 2014-10-06 09:46:03 +0900 | [diff] [blame] | 977 | ttrace = thread__priv(thread); |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 978 | ++ttrace->nr_events; |
| 979 | |
| 980 | return ttrace; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 981 | fail: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 982 | color_fprintf(fp, PERF_COLOR_RED, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 983 | "WARNING: not enough memory, dropping samples!\n"); |
| 984 | return NULL; |
| 985 | } |
| 986 | |
Arnaldo Carvalho de Melo | 84486ca | 2017-07-14 11:35:03 -0300 | [diff] [blame] | 987 | |
| 988 | void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg, |
Arnaldo Carvalho de Melo | 7ee5743 | 2017-07-14 15:16:54 -0300 | [diff] [blame] | 989 | size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg)) |
Arnaldo Carvalho de Melo | 84486ca | 2017-07-14 11:35:03 -0300 | [diff] [blame] | 990 | { |
| 991 | struct thread_trace *ttrace = thread__priv(arg->thread); |
| 992 | |
| 993 | ttrace->ret_scnprintf = ret_scnprintf; |
| 994 | } |
| 995 | |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 996 | #define TRACE_PFMAJ (1 << 0) |
| 997 | #define TRACE_PFMIN (1 << 1) |
| 998 | |
Arnaldo Carvalho de Melo | e4d44e8 | 2015-08-04 22:17:29 -0300 | [diff] [blame] | 999 | static const size_t trace__entry_str_size = 2048; |
| 1000 | |
Arnaldo Carvalho de Melo | 97119f3 | 2013-09-27 17:34:10 -0300 | [diff] [blame] | 1001 | static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname) |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1002 | { |
Namhyung Kim | 89dceb2 | 2014-10-06 09:46:03 +0900 | [diff] [blame] | 1003 | struct thread_trace *ttrace = thread__priv(thread); |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1004 | |
| 1005 | if (fd > ttrace->paths.max) { |
| 1006 | char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *)); |
| 1007 | |
| 1008 | if (npath == NULL) |
| 1009 | return -1; |
| 1010 | |
| 1011 | if (ttrace->paths.max != -1) { |
| 1012 | memset(npath + ttrace->paths.max + 1, 0, |
| 1013 | (fd - ttrace->paths.max) * sizeof(char *)); |
| 1014 | } else { |
| 1015 | memset(npath, 0, (fd + 1) * sizeof(char *)); |
| 1016 | } |
| 1017 | |
| 1018 | ttrace->paths.table = npath; |
| 1019 | ttrace->paths.max = fd; |
| 1020 | } |
| 1021 | |
| 1022 | ttrace->paths.table[fd] = strdup(pathname); |
| 1023 | |
| 1024 | return ttrace->paths.table[fd] != NULL ? 0 : -1; |
| 1025 | } |
| 1026 | |
Arnaldo Carvalho de Melo | 97119f3 | 2013-09-27 17:34:10 -0300 | [diff] [blame] | 1027 | static int thread__read_fd_path(struct thread *thread, int fd) |
| 1028 | { |
| 1029 | char linkname[PATH_MAX], pathname[PATH_MAX]; |
| 1030 | struct stat st; |
| 1031 | int ret; |
| 1032 | |
| 1033 | if (thread->pid_ == thread->tid) { |
| 1034 | scnprintf(linkname, sizeof(linkname), |
| 1035 | "/proc/%d/fd/%d", thread->pid_, fd); |
| 1036 | } else { |
| 1037 | scnprintf(linkname, sizeof(linkname), |
| 1038 | "/proc/%d/task/%d/fd/%d", thread->pid_, thread->tid, fd); |
| 1039 | } |
| 1040 | |
| 1041 | if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname)) |
| 1042 | return -1; |
| 1043 | |
| 1044 | ret = readlink(linkname, pathname, sizeof(pathname)); |
| 1045 | |
| 1046 | if (ret < 0 || ret > st.st_size) |
| 1047 | return -1; |
| 1048 | |
| 1049 | pathname[ret] = '\0'; |
| 1050 | return trace__set_fd_pathname(thread, fd, pathname); |
| 1051 | } |
| 1052 | |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 1053 | static const char *thread__fd_path(struct thread *thread, int fd, |
| 1054 | struct trace *trace) |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1055 | { |
Namhyung Kim | 89dceb2 | 2014-10-06 09:46:03 +0900 | [diff] [blame] | 1056 | struct thread_trace *ttrace = thread__priv(thread); |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1057 | |
| 1058 | if (ttrace == NULL) |
| 1059 | return NULL; |
| 1060 | |
| 1061 | if (fd < 0) |
| 1062 | return NULL; |
| 1063 | |
Arnaldo Carvalho de Melo | cdcd1e6 | 2014-06-10 16:00:18 -0300 | [diff] [blame] | 1064 | if ((fd > ttrace->paths.max || ttrace->paths.table[fd] == NULL)) { |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 1065 | if (!trace->live) |
| 1066 | return NULL; |
| 1067 | ++trace->stats.proc_getname; |
Arnaldo Carvalho de Melo | cdcd1e6 | 2014-06-10 16:00:18 -0300 | [diff] [blame] | 1068 | if (thread__read_fd_path(thread, fd)) |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 1069 | return NULL; |
| 1070 | } |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1071 | |
| 1072 | return ttrace->paths.table[fd]; |
| 1073 | } |
| 1074 | |
Arnaldo Carvalho de Melo | fc65eb8 | 2017-07-14 15:21:40 -0300 | [diff] [blame] | 1075 | size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg) |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1076 | { |
| 1077 | int fd = arg->val; |
| 1078 | size_t printed = scnprintf(bf, size, "%d", fd); |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 1079 | const char *path = thread__fd_path(arg->thread, fd, arg->trace); |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1080 | |
| 1081 | if (path) |
| 1082 | printed += scnprintf(bf + printed, size - printed, "<%s>", path); |
| 1083 | |
| 1084 | return printed; |
| 1085 | } |
| 1086 | |
Arnaldo Carvalho de Melo | 0a2f7540 | 2017-10-31 11:30:09 -0300 | [diff] [blame] | 1087 | size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size) |
| 1088 | { |
| 1089 | size_t printed = scnprintf(bf, size, "%d", fd); |
| 1090 | struct thread *thread = machine__find_thread(trace->host, pid, pid); |
| 1091 | |
| 1092 | if (thread) { |
| 1093 | const char *path = thread__fd_path(thread, fd, trace); |
| 1094 | |
| 1095 | if (path) |
| 1096 | printed += scnprintf(bf + printed, size - printed, "<%s>", path); |
| 1097 | |
| 1098 | thread__put(thread); |
| 1099 | } |
| 1100 | |
| 1101 | return printed; |
| 1102 | } |
| 1103 | |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1104 | static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, |
| 1105 | struct syscall_arg *arg) |
| 1106 | { |
| 1107 | int fd = arg->val; |
| 1108 | size_t printed = syscall_arg__scnprintf_fd(bf, size, arg); |
Namhyung Kim | 89dceb2 | 2014-10-06 09:46:03 +0900 | [diff] [blame] | 1109 | struct thread_trace *ttrace = thread__priv(arg->thread); |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1110 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1111 | if (ttrace && fd >= 0 && fd <= ttrace->paths.max) |
| 1112 | zfree(&ttrace->paths.table[fd]); |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 1113 | |
| 1114 | return printed; |
| 1115 | } |
| 1116 | |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 1117 | static void thread__set_filename_pos(struct thread *thread, const char *bf, |
| 1118 | unsigned long ptr) |
| 1119 | { |
| 1120 | struct thread_trace *ttrace = thread__priv(thread); |
| 1121 | |
| 1122 | ttrace->filename.ptr = ptr; |
| 1123 | ttrace->filename.entry_str_pos = bf - ttrace->entry_str; |
| 1124 | } |
| 1125 | |
Arnaldo Carvalho de Melo | 75d1e30 | 2018-08-21 12:00:39 -0300 | [diff] [blame] | 1126 | static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size) |
| 1127 | { |
| 1128 | struct augmented_arg *augmented_arg = arg->augmented.args; |
| 1129 | |
| 1130 | return scnprintf(bf, size, "%.*s", augmented_arg->size, augmented_arg->value); |
| 1131 | } |
| 1132 | |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 1133 | static size_t syscall_arg__scnprintf_filename(char *bf, size_t size, |
| 1134 | struct syscall_arg *arg) |
| 1135 | { |
| 1136 | unsigned long ptr = arg->val; |
| 1137 | |
Arnaldo Carvalho de Melo | 75d1e30 | 2018-08-21 12:00:39 -0300 | [diff] [blame] | 1138 | if (arg->augmented.args) |
| 1139 | return syscall_arg__scnprintf_augmented_string(arg, bf, size); |
| 1140 | |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 1141 | if (!arg->trace->vfs_getname) |
| 1142 | return scnprintf(bf, size, "%#x", ptr); |
| 1143 | |
| 1144 | thread__set_filename_pos(arg->thread, bf, ptr); |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 1148 | static bool trace__filter_duration(struct trace *trace, double t) |
| 1149 | { |
| 1150 | return t < (trace->duration_filter * NSEC_PER_MSEC); |
| 1151 | } |
| 1152 | |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 1153 | static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1154 | { |
| 1155 | double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC; |
| 1156 | |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 1157 | return fprintf(fp, "%10.3f ", ts); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1158 | } |
| 1159 | |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 1160 | /* |
| 1161 | * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are |
| 1162 | * using ttrace->entry_time for a thread that receives a sys_exit without |
| 1163 | * first having received a sys_enter ("poll" issued before tracing session |
| 1164 | * starts, lost sys_enter exit due to ring buffer overflow). |
| 1165 | */ |
| 1166 | static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) |
| 1167 | { |
| 1168 | if (tstamp > 0) |
| 1169 | return __trace__fprintf_tstamp(trace, tstamp, fp); |
| 1170 | |
| 1171 | return fprintf(fp, " ? "); |
| 1172 | } |
| 1173 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 1174 | static bool done = false; |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 1175 | static bool interrupted = false; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 1176 | |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 1177 | static void sig_handler(int sig) |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 1178 | { |
| 1179 | done = true; |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 1180 | interrupted = sig == SIGINT; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 1181 | } |
| 1182 | |
Arnaldo Carvalho de Melo | 6dcbd21 | 2018-08-21 11:40:09 -0300 | [diff] [blame] | 1183 | static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1184 | { |
Arnaldo Carvalho de Melo | 6dcbd21 | 2018-08-21 11:40:09 -0300 | [diff] [blame] | 1185 | size_t printed = 0; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1186 | |
Arnaldo Carvalho de Melo | 50c95cb | 2013-09-12 12:35:21 -0300 | [diff] [blame] | 1187 | if (trace->multiple_threads) { |
| 1188 | if (trace->show_comm) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 1189 | printed += fprintf(fp, "%.14s/", thread__comm_str(thread)); |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 1190 | printed += fprintf(fp, "%d ", thread->tid); |
Arnaldo Carvalho de Melo | 50c95cb | 2013-09-12 12:35:21 -0300 | [diff] [blame] | 1191 | } |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1192 | |
| 1193 | return printed; |
| 1194 | } |
| 1195 | |
Arnaldo Carvalho de Melo | 6dcbd21 | 2018-08-21 11:40:09 -0300 | [diff] [blame] | 1196 | static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread, |
| 1197 | u64 duration, bool duration_calculated, u64 tstamp, FILE *fp) |
| 1198 | { |
| 1199 | size_t printed = trace__fprintf_tstamp(trace, tstamp, fp); |
| 1200 | printed += fprintf_duration(duration, duration_calculated, fp); |
| 1201 | return printed + trace__fprintf_comm_tid(trace, thread, fp); |
| 1202 | } |
| 1203 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1204 | static int trace__process_event(struct trace *trace, struct machine *machine, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1205 | union perf_event *event, struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1206 | { |
| 1207 | int ret = 0; |
| 1208 | |
| 1209 | switch (event->header.type) { |
| 1210 | case PERF_RECORD_LOST: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1211 | color_fprintf(trace->output, PERF_COLOR_RED, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1212 | "LOST %" PRIu64 " events!\n", event->lost.lost); |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1213 | ret = machine__process_lost_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 3ed5ca2 | 2016-03-30 16:51:17 -0300 | [diff] [blame] | 1214 | break; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1215 | default: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1216 | ret = machine__process_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1217 | break; |
| 1218 | } |
| 1219 | |
| 1220 | return ret; |
| 1221 | } |
| 1222 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1223 | static int trace__tool_process(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1224 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1225 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1226 | struct machine *machine) |
| 1227 | { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1228 | struct trace *trace = container_of(tool, struct trace, tool); |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1229 | return trace__process_event(trace, machine, event, sample); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1230 | } |
| 1231 | |
Arnaldo Carvalho de Melo | caf8a0d | 2016-05-17 11:56:24 -0300 | [diff] [blame] | 1232 | static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) |
| 1233 | { |
| 1234 | struct machine *machine = vmachine; |
| 1235 | |
| 1236 | if (machine->kptr_restrict_warned) |
| 1237 | return NULL; |
| 1238 | |
| 1239 | if (symbol_conf.kptr_restrict) { |
| 1240 | pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n" |
| 1241 | "Check /proc/sys/kernel/kptr_restrict.\n\n" |
| 1242 | "Kernel samples will not be resolved.\n"); |
| 1243 | machine->kptr_restrict_warned = true; |
| 1244 | return NULL; |
| 1245 | } |
| 1246 | |
| 1247 | return machine__resolve_kernel_addr(vmachine, addrp, modp); |
| 1248 | } |
| 1249 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1250 | static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist) |
| 1251 | { |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 1252 | int err = symbol__init(NULL); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1253 | |
| 1254 | if (err) |
| 1255 | return err; |
| 1256 | |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 1257 | trace->host = machine__new_host(); |
| 1258 | if (trace->host == NULL) |
| 1259 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1260 | |
Andrei Vagin | cbd5c17 | 2017-11-07 16:22:46 -0800 | [diff] [blame] | 1261 | err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr); |
| 1262 | if (err < 0) |
| 1263 | goto out; |
Arnaldo Carvalho de Melo | 706c3da | 2015-07-22 16:16:16 -0300 | [diff] [blame] | 1264 | |
Arnaldo Carvalho de Melo | a33fbd5 | 2013-11-11 11:36:12 -0300 | [diff] [blame] | 1265 | err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 1266 | evlist->threads, trace__tool_process, false, |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 1267 | trace->opts.proc_map_timeout, 1); |
Andrei Vagin | cbd5c17 | 2017-11-07 16:22:46 -0800 | [diff] [blame] | 1268 | out: |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1269 | if (err) |
| 1270 | symbol__exit(); |
| 1271 | |
| 1272 | return err; |
| 1273 | } |
| 1274 | |
Andrei Vagin | 33974a4 | 2017-11-07 16:22:45 -0800 | [diff] [blame] | 1275 | static void trace__symbols__exit(struct trace *trace) |
| 1276 | { |
| 1277 | machine__exit(trace->host); |
| 1278 | trace->host = NULL; |
| 1279 | |
| 1280 | symbol__exit(); |
| 1281 | } |
| 1282 | |
Arnaldo Carvalho de Melo | 5e58fcf | 2017-07-19 14:32:11 -0300 | [diff] [blame] | 1283 | static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args) |
| 1284 | { |
| 1285 | int idx; |
| 1286 | |
Arnaldo Carvalho de Melo | 332337d | 2017-07-19 15:08:14 -0300 | [diff] [blame] | 1287 | if (nr_args == 6 && sc->fmt && sc->fmt->nr_args != 0) |
| 1288 | nr_args = sc->fmt->nr_args; |
| 1289 | |
Arnaldo Carvalho de Melo | 5e58fcf | 2017-07-19 14:32:11 -0300 | [diff] [blame] | 1290 | sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt)); |
| 1291 | if (sc->arg_fmt == NULL) |
| 1292 | return -1; |
| 1293 | |
| 1294 | for (idx = 0; idx < nr_args; ++idx) { |
| 1295 | if (sc->fmt) |
| 1296 | sc->arg_fmt[idx] = sc->fmt->arg[idx]; |
| 1297 | } |
| 1298 | |
| 1299 | sc->nr_args = nr_args; |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 1303 | static int syscall__set_arg_fmts(struct syscall *sc) |
| 1304 | { |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 1305 | struct tep_format_field *field, *last_field = NULL; |
Arnaldo Carvalho de Melo | b6565c9 | 2016-05-23 12:59:53 -0300 | [diff] [blame] | 1306 | int idx = 0, len; |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 1307 | |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1308 | for (field = sc->args; field; field = field->next, ++idx) { |
Arnaldo Carvalho de Melo | 7a983a0 | 2018-08-20 17:58:17 -0300 | [diff] [blame] | 1309 | last_field = field; |
| 1310 | |
Arnaldo Carvalho de Melo | 5e58fcf | 2017-07-19 14:32:11 -0300 | [diff] [blame] | 1311 | if (sc->fmt && sc->fmt->arg[idx].scnprintf) |
| 1312 | continue; |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1313 | |
| 1314 | if (strcmp(field->type, "const char *") == 0 && |
Arnaldo Carvalho de Melo | 12f3ca4 | 2016-05-23 16:37:55 -0300 | [diff] [blame] | 1315 | (strcmp(field->name, "filename") == 0 || |
| 1316 | strcmp(field->name, "path") == 0 || |
| 1317 | strcmp(field->name, "pathname") == 0)) |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1318 | sc->arg_fmt[idx].scnprintf = SCA_FILENAME; |
Tzvetomir Stoyanov (VMware) | bb39ccb | 2018-09-19 14:56:46 -0400 | [diff] [blame] | 1319 | else if (field->flags & TEP_FIELD_IS_POINTER) |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1320 | sc->arg_fmt[idx].scnprintf = syscall_arg__scnprintf_hex; |
Arnaldo Carvalho de Melo | d1d438a | 2016-04-06 18:02:41 -0300 | [diff] [blame] | 1321 | else if (strcmp(field->type, "pid_t") == 0) |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1322 | sc->arg_fmt[idx].scnprintf = SCA_PID; |
Arnaldo Carvalho de Melo | ba2f22c | 2016-04-07 12:05:51 -0300 | [diff] [blame] | 1323 | else if (strcmp(field->type, "umode_t") == 0) |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1324 | sc->arg_fmt[idx].scnprintf = SCA_MODE_T; |
Arnaldo Carvalho de Melo | b6565c9 | 2016-05-23 12:59:53 -0300 | [diff] [blame] | 1325 | else if ((strcmp(field->type, "int") == 0 || |
| 1326 | strcmp(field->type, "unsigned int") == 0 || |
| 1327 | strcmp(field->type, "long") == 0) && |
| 1328 | (len = strlen(field->name)) >= 2 && |
| 1329 | strcmp(field->name + len - 2, "fd") == 0) { |
| 1330 | /* |
| 1331 | * /sys/kernel/tracing/events/syscalls/sys_enter* |
| 1332 | * egrep 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c |
| 1333 | * 65 int |
| 1334 | * 23 unsigned int |
| 1335 | * 7 unsigned long |
| 1336 | */ |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1337 | sc->arg_fmt[idx].scnprintf = SCA_FD; |
Arnaldo Carvalho de Melo | b6565c9 | 2016-05-23 12:59:53 -0300 | [diff] [blame] | 1338 | } |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 1339 | } |
| 1340 | |
Arnaldo Carvalho de Melo | 7a983a0 | 2018-08-20 17:58:17 -0300 | [diff] [blame] | 1341 | if (last_field) |
| 1342 | sc->args_size = last_field->offset + last_field->size; |
| 1343 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 1344 | return 0; |
| 1345 | } |
| 1346 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1347 | static int trace__read_syscall_info(struct trace *trace, int id) |
| 1348 | { |
| 1349 | char tp_name[128]; |
| 1350 | struct syscall *sc; |
Arnaldo Carvalho de Melo | fd0db10 | 2016-04-04 13:32:20 -0300 | [diff] [blame] | 1351 | const char *name = syscalltbl__name(trace->sctbl, id); |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 1352 | |
| 1353 | if (name == NULL) |
| 1354 | return -1; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1355 | |
| 1356 | if (id > trace->syscalls.max) { |
| 1357 | struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc)); |
| 1358 | |
| 1359 | if (nsyscalls == NULL) |
| 1360 | return -1; |
| 1361 | |
| 1362 | if (trace->syscalls.max != -1) { |
| 1363 | memset(nsyscalls + trace->syscalls.max + 1, 0, |
| 1364 | (id - trace->syscalls.max) * sizeof(*sc)); |
| 1365 | } else { |
| 1366 | memset(nsyscalls, 0, (id + 1) * sizeof(*sc)); |
| 1367 | } |
| 1368 | |
| 1369 | trace->syscalls.table = nsyscalls; |
| 1370 | trace->syscalls.max = id; |
| 1371 | } |
| 1372 | |
| 1373 | sc = trace->syscalls.table + id; |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 1374 | sc->name = name; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1375 | |
Arnaldo Carvalho de Melo | 3a53126 | 2012-10-20 12:39:03 -0300 | [diff] [blame] | 1376 | sc->fmt = syscall_fmt__find(sc->name); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1377 | |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 1378 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name); |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 1379 | sc->tp_format = trace_event__tp_format("syscalls", tp_name); |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 1380 | |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 1381 | if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) { |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 1382 | snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias); |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 1383 | sc->tp_format = trace_event__tp_format("syscalls", tp_name); |
Arnaldo Carvalho de Melo | aec1930 | 2012-09-27 13:16:00 -0300 | [diff] [blame] | 1384 | } |
| 1385 | |
Arnaldo Carvalho de Melo | 5e58fcf | 2017-07-19 14:32:11 -0300 | [diff] [blame] | 1386 | if (syscall__alloc_arg_fmts(sc, IS_ERR(sc->tp_format) ? 6 : sc->tp_format->format.nr_fields)) |
| 1387 | return -1; |
| 1388 | |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 1389 | if (IS_ERR(sc->tp_format)) |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 1390 | return -1; |
| 1391 | |
Arnaldo Carvalho de Melo | f208bd8 | 2015-03-20 17:46:53 -0300 | [diff] [blame] | 1392 | sc->args = sc->tp_format->format.fields; |
Taeung Song | c42de70 | 2016-02-26 22:14:25 +0900 | [diff] [blame] | 1393 | /* |
| 1394 | * We need to check and discard the first variable '__syscall_nr' |
| 1395 | * or 'nr' that mean the syscall number. It is needless here. |
| 1396 | * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels. |
| 1397 | */ |
| 1398 | if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) { |
Arnaldo Carvalho de Melo | f208bd8 | 2015-03-20 17:46:53 -0300 | [diff] [blame] | 1399 | sc->args = sc->args->next; |
| 1400 | --sc->nr_args; |
| 1401 | } |
| 1402 | |
Arnaldo Carvalho de Melo | 5089f20 | 2014-06-17 14:29:24 -0300 | [diff] [blame] | 1403 | sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit"); |
Arnaldo Carvalho de Melo | 6a648b5 | 2018-08-02 10:30:08 -0300 | [diff] [blame] | 1404 | sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat"); |
Arnaldo Carvalho de Melo | 5089f20 | 2014-06-17 14:29:24 -0300 | [diff] [blame] | 1405 | |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 1406 | return syscall__set_arg_fmts(sc); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1407 | } |
| 1408 | |
Arnaldo Carvalho de Melo | d0cc439 | 2015-06-25 19:32:33 -0300 | [diff] [blame] | 1409 | static int trace__validate_ev_qualifier(struct trace *trace) |
| 1410 | { |
Arnaldo Carvalho de Melo | 8b3ce75 | 2015-07-02 18:28:11 -0300 | [diff] [blame] | 1411 | int err = 0, i; |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 1412 | size_t nr_allocated; |
Arnaldo Carvalho de Melo | d0cc439 | 2015-06-25 19:32:33 -0300 | [diff] [blame] | 1413 | struct str_node *pos; |
| 1414 | |
Arnaldo Carvalho de Melo | 8b3ce75 | 2015-07-02 18:28:11 -0300 | [diff] [blame] | 1415 | trace->ev_qualifier_ids.nr = strlist__nr_entries(trace->ev_qualifier); |
| 1416 | trace->ev_qualifier_ids.entries = malloc(trace->ev_qualifier_ids.nr * |
| 1417 | sizeof(trace->ev_qualifier_ids.entries[0])); |
| 1418 | |
| 1419 | if (trace->ev_qualifier_ids.entries == NULL) { |
| 1420 | fputs("Error:\tNot enough memory for allocating events qualifier ids\n", |
| 1421 | trace->output); |
| 1422 | err = -EINVAL; |
| 1423 | goto out; |
| 1424 | } |
| 1425 | |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 1426 | nr_allocated = trace->ev_qualifier_ids.nr; |
Arnaldo Carvalho de Melo | 8b3ce75 | 2015-07-02 18:28:11 -0300 | [diff] [blame] | 1427 | i = 0; |
| 1428 | |
Arnaldo Carvalho de Melo | 602a1f4 | 2016-06-23 11:31:20 -0300 | [diff] [blame] | 1429 | strlist__for_each_entry(pos, trace->ev_qualifier) { |
Arnaldo Carvalho de Melo | d0cc439 | 2015-06-25 19:32:33 -0300 | [diff] [blame] | 1430 | const char *sc = pos->s; |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 1431 | int id = syscalltbl__id(trace->sctbl, sc), match_next = -1; |
Arnaldo Carvalho de Melo | d0cc439 | 2015-06-25 19:32:33 -0300 | [diff] [blame] | 1432 | |
Arnaldo Carvalho de Melo | 8b3ce75 | 2015-07-02 18:28:11 -0300 | [diff] [blame] | 1433 | if (id < 0) { |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 1434 | id = syscalltbl__strglobmatch_first(trace->sctbl, sc, &match_next); |
| 1435 | if (id >= 0) |
| 1436 | goto matches; |
| 1437 | |
Arnaldo Carvalho de Melo | d0cc439 | 2015-06-25 19:32:33 -0300 | [diff] [blame] | 1438 | if (err == 0) { |
| 1439 | fputs("Error:\tInvalid syscall ", trace->output); |
| 1440 | err = -EINVAL; |
| 1441 | } else { |
| 1442 | fputs(", ", trace->output); |
| 1443 | } |
| 1444 | |
| 1445 | fputs(sc, trace->output); |
| 1446 | } |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 1447 | matches: |
Arnaldo Carvalho de Melo | 8b3ce75 | 2015-07-02 18:28:11 -0300 | [diff] [blame] | 1448 | trace->ev_qualifier_ids.entries[i++] = id; |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 1449 | if (match_next == -1) |
| 1450 | continue; |
| 1451 | |
| 1452 | while (1) { |
| 1453 | id = syscalltbl__strglobmatch_next(trace->sctbl, sc, &match_next); |
| 1454 | if (id < 0) |
| 1455 | break; |
| 1456 | if (nr_allocated == trace->ev_qualifier_ids.nr) { |
| 1457 | void *entries; |
| 1458 | |
| 1459 | nr_allocated += 8; |
| 1460 | entries = realloc(trace->ev_qualifier_ids.entries, |
| 1461 | nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0])); |
| 1462 | if (entries == NULL) { |
| 1463 | err = -ENOMEM; |
| 1464 | fputs("\nError:\t Not enough memory for parsing\n", trace->output); |
| 1465 | goto out_free; |
| 1466 | } |
| 1467 | trace->ev_qualifier_ids.entries = entries; |
| 1468 | } |
| 1469 | trace->ev_qualifier_ids.nr++; |
| 1470 | trace->ev_qualifier_ids.entries[i++] = id; |
| 1471 | } |
Arnaldo Carvalho de Melo | d0cc439 | 2015-06-25 19:32:33 -0300 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | if (err < 0) { |
| 1475 | fputs("\nHint:\ttry 'perf list syscalls:sys_enter_*'" |
| 1476 | "\nHint:\tand: 'man syscalls'\n", trace->output); |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 1477 | out_free: |
Arnaldo Carvalho de Melo | 8b3ce75 | 2015-07-02 18:28:11 -0300 | [diff] [blame] | 1478 | zfree(&trace->ev_qualifier_ids.entries); |
| 1479 | trace->ev_qualifier_ids.nr = 0; |
Arnaldo Carvalho de Melo | d0cc439 | 2015-06-25 19:32:33 -0300 | [diff] [blame] | 1480 | } |
Arnaldo Carvalho de Melo | 8b3ce75 | 2015-07-02 18:28:11 -0300 | [diff] [blame] | 1481 | out: |
Arnaldo Carvalho de Melo | d0cc439 | 2015-06-25 19:32:33 -0300 | [diff] [blame] | 1482 | return err; |
| 1483 | } |
| 1484 | |
David Ahern | 55d43bca | 2015-02-19 15:00:22 -0500 | [diff] [blame] | 1485 | /* |
| 1486 | * args is to be interpreted as a series of longs but we need to handle |
| 1487 | * 8-byte unaligned accesses. args points to raw_data within the event |
| 1488 | * and raw_data is guaranteed to be 8-byte unaligned because it is |
| 1489 | * preceded by raw_size which is a u32. So we need to copy args to a temp |
| 1490 | * variable to read it. Most notably this avoids extended load instructions |
| 1491 | * on unaligned addresses |
| 1492 | */ |
Arnaldo Carvalho de Melo | 325f509 | 2017-07-19 15:02:43 -0300 | [diff] [blame] | 1493 | unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx) |
Arnaldo Carvalho de Melo | f9f83b3 | 2017-07-14 10:13:56 -0300 | [diff] [blame] | 1494 | { |
| 1495 | unsigned long val; |
Arnaldo Carvalho de Melo | 325f509 | 2017-07-19 15:02:43 -0300 | [diff] [blame] | 1496 | unsigned char *p = arg->args + sizeof(unsigned long) * idx; |
Arnaldo Carvalho de Melo | f9f83b3 | 2017-07-14 10:13:56 -0300 | [diff] [blame] | 1497 | |
| 1498 | memcpy(&val, p, sizeof(val)); |
| 1499 | return val; |
| 1500 | } |
| 1501 | |
Arnaldo Carvalho de Melo | c51bdfe | 2017-07-19 15:47:30 -0300 | [diff] [blame] | 1502 | static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size, |
| 1503 | struct syscall_arg *arg) |
| 1504 | { |
| 1505 | if (sc->arg_fmt && sc->arg_fmt[arg->idx].name) |
| 1506 | return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name); |
| 1507 | |
| 1508 | return scnprintf(bf, size, "arg%d: ", arg->idx); |
| 1509 | } |
| 1510 | |
Arnaldo Carvalho de Melo | 496fd34 | 2018-10-25 16:09:47 -0300 | [diff] [blame] | 1511 | /* |
| 1512 | * Check if the value is in fact zero, i.e. mask whatever needs masking, such |
| 1513 | * as mount 'flags' argument that needs ignoring some magic flag, see comment |
| 1514 | * in tools/perf/trace/beauty/mount_flags.c |
| 1515 | */ |
| 1516 | static unsigned long syscall__mask_val(struct syscall *sc, struct syscall_arg *arg, unsigned long val) |
| 1517 | { |
| 1518 | if (sc->arg_fmt && sc->arg_fmt[arg->idx].mask_val) |
| 1519 | return sc->arg_fmt[arg->idx].mask_val(arg, val); |
| 1520 | |
| 1521 | return val; |
| 1522 | } |
| 1523 | |
Arnaldo Carvalho de Melo | d032d79 | 2017-07-19 14:36:13 -0300 | [diff] [blame] | 1524 | static size_t syscall__scnprintf_val(struct syscall *sc, char *bf, size_t size, |
| 1525 | struct syscall_arg *arg, unsigned long val) |
| 1526 | { |
| 1527 | if (sc->arg_fmt && sc->arg_fmt[arg->idx].scnprintf) { |
| 1528 | arg->val = val; |
| 1529 | if (sc->arg_fmt[arg->idx].parm) |
| 1530 | arg->parm = sc->arg_fmt[arg->idx].parm; |
| 1531 | return sc->arg_fmt[arg->idx].scnprintf(bf, size, arg); |
| 1532 | } |
| 1533 | return scnprintf(bf, size, "%ld", val); |
| 1534 | } |
| 1535 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1536 | static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size, |
Arnaldo Carvalho de Melo | 7a983a0 | 2018-08-20 17:58:17 -0300 | [diff] [blame] | 1537 | unsigned char *args, void *augmented_args, int augmented_args_size, |
| 1538 | struct trace *trace, struct thread *thread) |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1539 | { |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1540 | size_t printed = 0; |
David Ahern | 55d43bca | 2015-02-19 15:00:22 -0500 | [diff] [blame] | 1541 | unsigned long val; |
Arnaldo Carvalho de Melo | d032d79 | 2017-07-19 14:36:13 -0300 | [diff] [blame] | 1542 | u8 bit = 1; |
| 1543 | struct syscall_arg arg = { |
| 1544 | .args = args, |
Arnaldo Carvalho de Melo | 7a983a0 | 2018-08-20 17:58:17 -0300 | [diff] [blame] | 1545 | .augmented = { |
| 1546 | .size = augmented_args_size, |
| 1547 | .args = augmented_args, |
| 1548 | }, |
Arnaldo Carvalho de Melo | d032d79 | 2017-07-19 14:36:13 -0300 | [diff] [blame] | 1549 | .idx = 0, |
| 1550 | .mask = 0, |
| 1551 | .trace = trace, |
| 1552 | .thread = thread, |
| 1553 | }; |
Arnaldo Carvalho de Melo | 84486ca | 2017-07-14 11:35:03 -0300 | [diff] [blame] | 1554 | struct thread_trace *ttrace = thread__priv(thread); |
| 1555 | |
| 1556 | /* |
| 1557 | * Things like fcntl will set this in its 'cmd' formatter to pick the |
| 1558 | * right formatter for the return value (an fd? file flags?), which is |
| 1559 | * not needed for syscalls that always return a given type, say an fd. |
| 1560 | */ |
| 1561 | ttrace->ret_scnprintf = NULL; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1562 | |
Arnaldo Carvalho de Melo | f208bd8 | 2015-03-20 17:46:53 -0300 | [diff] [blame] | 1563 | if (sc->args != NULL) { |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 1564 | struct tep_format_field *field; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1565 | |
Arnaldo Carvalho de Melo | f208bd8 | 2015-03-20 17:46:53 -0300 | [diff] [blame] | 1566 | for (field = sc->args; field; |
Arnaldo Carvalho de Melo | 01533e9 | 2013-09-03 12:20:12 -0300 | [diff] [blame] | 1567 | field = field->next, ++arg.idx, bit <<= 1) { |
| 1568 | if (arg.mask & bit) |
Arnaldo Carvalho de Melo | 6e7eeb5 | 2013-09-02 10:39:21 -0300 | [diff] [blame] | 1569 | continue; |
David Ahern | 55d43bca | 2015-02-19 15:00:22 -0500 | [diff] [blame] | 1570 | |
Arnaldo Carvalho de Melo | f9f83b3 | 2017-07-14 10:13:56 -0300 | [diff] [blame] | 1571 | val = syscall_arg__val(&arg, arg.idx); |
Arnaldo Carvalho de Melo | 496fd34 | 2018-10-25 16:09:47 -0300 | [diff] [blame] | 1572 | /* |
| 1573 | * Some syscall args need some mask, most don't and |
| 1574 | * return val untouched. |
| 1575 | */ |
| 1576 | val = syscall__mask_val(sc, &arg, val); |
David Ahern | 55d43bca | 2015-02-19 15:00:22 -0500 | [diff] [blame] | 1577 | |
Arnaldo Carvalho de Melo | 4aa5823 | 2013-09-20 12:19:41 -0300 | [diff] [blame] | 1578 | /* |
| 1579 | * Suppress this argument if its value is zero and |
| 1580 | * and we don't have a string associated in an |
| 1581 | * strarray for it. |
| 1582 | */ |
David Ahern | 55d43bca | 2015-02-19 15:00:22 -0500 | [diff] [blame] | 1583 | if (val == 0 && |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1584 | !(sc->arg_fmt && |
Arnaldo Carvalho de Melo | d47737d | 2017-07-17 15:59:03 -0300 | [diff] [blame] | 1585 | (sc->arg_fmt[arg.idx].show_zero || |
| 1586 | sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAY || |
Arnaldo Carvalho de Melo | 82d4a11 | 2017-07-17 15:22:47 -0300 | [diff] [blame] | 1587 | sc->arg_fmt[arg.idx].scnprintf == SCA_STRARRAYS) && |
| 1588 | sc->arg_fmt[arg.idx].parm)) |
Arnaldo Carvalho de Melo | 22ae5cf1 | 2013-09-12 11:27:34 -0300 | [diff] [blame] | 1589 | continue; |
| 1590 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1591 | printed += scnprintf(bf + printed, size - printed, |
Arnaldo Carvalho de Melo | 13d4ff3 | 2013-08-23 18:14:48 -0300 | [diff] [blame] | 1592 | "%s%s: ", printed ? ", " : "", field->name); |
Arnaldo Carvalho de Melo | d032d79 | 2017-07-19 14:36:13 -0300 | [diff] [blame] | 1593 | printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1594 | } |
Arnaldo Carvalho de Melo | 4c4d6e5 | 2016-05-05 23:38:05 -0300 | [diff] [blame] | 1595 | } else if (IS_ERR(sc->tp_format)) { |
| 1596 | /* |
| 1597 | * If we managed to read the tracepoint /format file, then we |
| 1598 | * may end up not having any args, like with gettid(), so only |
| 1599 | * print the raw args when we didn't manage to read it. |
| 1600 | */ |
Arnaldo Carvalho de Melo | 332337d | 2017-07-19 15:08:14 -0300 | [diff] [blame] | 1601 | while (arg.idx < sc->nr_args) { |
Arnaldo Carvalho de Melo | d032d79 | 2017-07-19 14:36:13 -0300 | [diff] [blame] | 1602 | if (arg.mask & bit) |
| 1603 | goto next_arg; |
| 1604 | val = syscall_arg__val(&arg, arg.idx); |
Arnaldo Carvalho de Melo | c51bdfe | 2017-07-19 15:47:30 -0300 | [diff] [blame] | 1605 | if (printed) |
| 1606 | printed += scnprintf(bf + printed, size - printed, ", "); |
| 1607 | printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg); |
Arnaldo Carvalho de Melo | d032d79 | 2017-07-19 14:36:13 -0300 | [diff] [blame] | 1608 | printed += syscall__scnprintf_val(sc, bf + printed, size - printed, &arg, val); |
| 1609 | next_arg: |
| 1610 | ++arg.idx; |
| 1611 | bit <<= 1; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | return printed; |
| 1616 | } |
| 1617 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1618 | typedef int (*tracepoint_handler)(struct trace *trace, struct perf_evsel *evsel, |
Stanislav Fomichev | 0c82adc | 2014-06-26 20:14:24 +0400 | [diff] [blame] | 1619 | union perf_event *event, |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1620 | struct perf_sample *sample); |
| 1621 | |
| 1622 | static struct syscall *trace__syscall_info(struct trace *trace, |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 1623 | struct perf_evsel *evsel, int id) |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1624 | { |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1625 | |
| 1626 | if (id < 0) { |
Arnaldo Carvalho de Melo | adaa18b | 2013-08-22 17:55:25 -0300 | [diff] [blame] | 1627 | |
| 1628 | /* |
| 1629 | * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried |
| 1630 | * before that, leaving at a higher verbosity level till that is |
| 1631 | * explained. Reproduced with plain ftrace with: |
| 1632 | * |
| 1633 | * echo 1 > /t/events/raw_syscalls/sys_exit/enable |
| 1634 | * grep "NR -1 " /t/trace_pipe |
| 1635 | * |
| 1636 | * After generating some load on the machine. |
| 1637 | */ |
| 1638 | if (verbose > 1) { |
| 1639 | static u64 n; |
| 1640 | fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n", |
| 1641 | id, perf_evsel__name(evsel), ++n); |
| 1642 | } |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1643 | return NULL; |
| 1644 | } |
| 1645 | |
| 1646 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) && |
| 1647 | trace__read_syscall_info(trace, id)) |
| 1648 | goto out_cant_read; |
| 1649 | |
| 1650 | if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL)) |
| 1651 | goto out_cant_read; |
| 1652 | |
| 1653 | return &trace->syscalls.table[id]; |
| 1654 | |
| 1655 | out_cant_read: |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 1656 | if (verbose > 0) { |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 1657 | fprintf(trace->output, "Problems reading syscall %d", id); |
| 1658 | if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL) |
| 1659 | fprintf(trace->output, "(%s)", trace->syscalls.table[id].name); |
| 1660 | fputs(" information\n", trace->output); |
| 1661 | } |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1662 | return NULL; |
| 1663 | } |
| 1664 | |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 1665 | static void thread__update_stats(struct thread_trace *ttrace, |
| 1666 | int id, struct perf_sample *sample) |
| 1667 | { |
| 1668 | struct int_node *inode; |
| 1669 | struct stats *stats; |
| 1670 | u64 duration = 0; |
| 1671 | |
| 1672 | inode = intlist__findnew(ttrace->syscall_stats, id); |
| 1673 | if (inode == NULL) |
| 1674 | return; |
| 1675 | |
| 1676 | stats = inode->priv; |
| 1677 | if (stats == NULL) { |
| 1678 | stats = malloc(sizeof(struct stats)); |
| 1679 | if (stats == NULL) |
| 1680 | return; |
| 1681 | init_stats(stats); |
| 1682 | inode->priv = stats; |
| 1683 | } |
| 1684 | |
| 1685 | if (ttrace->entry_time && sample->time > ttrace->entry_time) |
| 1686 | duration = sample->time - ttrace->entry_time; |
| 1687 | |
| 1688 | update_stats(stats, duration); |
| 1689 | } |
| 1690 | |
Arnaldo Carvalho de Melo | 522283f | 2018-01-22 11:42:11 -0300 | [diff] [blame] | 1691 | static int trace__printf_interrupted_entry(struct trace *trace) |
Arnaldo Carvalho de Melo | e596663 | 2015-02-13 13:22:21 -0300 | [diff] [blame] | 1692 | { |
| 1693 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | e596663 | 2015-02-13 13:22:21 -0300 | [diff] [blame] | 1694 | size_t printed; |
| 1695 | |
Arnaldo Carvalho de Melo | 0a6545b | 2018-03-29 12:22:59 -0300 | [diff] [blame] | 1696 | if (trace->failure_only || trace->current == NULL) |
Arnaldo Carvalho de Melo | e596663 | 2015-02-13 13:22:21 -0300 | [diff] [blame] | 1697 | return 0; |
| 1698 | |
| 1699 | ttrace = thread__priv(trace->current); |
| 1700 | |
| 1701 | if (!ttrace->entry_pending) |
| 1702 | return 0; |
| 1703 | |
Arnaldo Carvalho de Melo | 522283f | 2018-01-22 11:42:11 -0300 | [diff] [blame] | 1704 | printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output); |
Arnaldo Carvalho de Melo | e596663 | 2015-02-13 13:22:21 -0300 | [diff] [blame] | 1705 | printed += fprintf(trace->output, "%-70s) ...\n", ttrace->entry_str); |
| 1706 | ttrace->entry_pending = false; |
| 1707 | |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 1708 | ++trace->nr_events_printed; |
| 1709 | |
Arnaldo Carvalho de Melo | e596663 | 2015-02-13 13:22:21 -0300 | [diff] [blame] | 1710 | return printed; |
| 1711 | } |
| 1712 | |
Arnaldo Carvalho de Melo | 591421e | 2018-01-22 11:38:54 -0300 | [diff] [blame] | 1713 | static int trace__fprintf_sample(struct trace *trace, struct perf_evsel *evsel, |
| 1714 | struct perf_sample *sample, struct thread *thread) |
| 1715 | { |
| 1716 | int printed = 0; |
| 1717 | |
| 1718 | if (trace->print_sample) { |
| 1719 | double ts = (double)sample->time / NSEC_PER_MSEC; |
| 1720 | |
| 1721 | printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n", |
| 1722 | perf_evsel__name(evsel), ts, |
| 1723 | thread__comm_str(thread), |
| 1724 | sample->pid, sample->tid, sample->cpu); |
| 1725 | } |
| 1726 | |
| 1727 | return printed; |
| 1728 | } |
| 1729 | |
Arnaldo Carvalho de Melo | cd26ea6 | 2018-11-03 08:19:56 -0300 | [diff] [blame] | 1730 | static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, bool raw_augmented) |
Arnaldo Carvalho de Melo | 8a041f8 | 2018-09-03 16:07:53 -0300 | [diff] [blame] | 1731 | { |
| 1732 | void *augmented_args = NULL; |
Arnaldo Carvalho de Melo | cd26ea6 | 2018-11-03 08:19:56 -0300 | [diff] [blame] | 1733 | /* |
| 1734 | * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter |
| 1735 | * and there we get all 6 syscall args plus the tracepoint common |
| 1736 | * fields (sizeof(long)) and the syscall_nr (another long). So we check |
| 1737 | * if that is the case and if so don't look after the sc->args_size, |
| 1738 | * but always after the full raw_syscalls:sys_enter payload, which is |
| 1739 | * fixed. |
| 1740 | * |
| 1741 | * We'll revisit this later to pass s->args_size to the BPF augmenter |
| 1742 | * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it |
| 1743 | * copies only what we need for each syscall, like what happens when we |
| 1744 | * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace |
| 1745 | * traffic to just what is needed for each syscall. |
| 1746 | */ |
| 1747 | int args_size = raw_augmented ? (8 * (int)sizeof(long)) : sc->args_size; |
Arnaldo Carvalho de Melo | 8a041f8 | 2018-09-03 16:07:53 -0300 | [diff] [blame] | 1748 | |
Arnaldo Carvalho de Melo | cd26ea6 | 2018-11-03 08:19:56 -0300 | [diff] [blame] | 1749 | *augmented_args_size = sample->raw_size - args_size; |
Arnaldo Carvalho de Melo | 8a041f8 | 2018-09-03 16:07:53 -0300 | [diff] [blame] | 1750 | if (*augmented_args_size > 0) |
Arnaldo Carvalho de Melo | cd26ea6 | 2018-11-03 08:19:56 -0300 | [diff] [blame] | 1751 | augmented_args = sample->raw_data + args_size; |
Arnaldo Carvalho de Melo | 8a041f8 | 2018-09-03 16:07:53 -0300 | [diff] [blame] | 1752 | |
| 1753 | return augmented_args; |
| 1754 | } |
| 1755 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1756 | static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel, |
Stanislav Fomichev | 0c82adc | 2014-06-26 20:14:24 +0400 | [diff] [blame] | 1757 | union perf_event *event __maybe_unused, |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1758 | struct perf_sample *sample) |
| 1759 | { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1760 | char *msg; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1761 | void *args; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1762 | size_t printed = 0; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1763 | struct thread *thread; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1764 | int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; |
Arnaldo Carvalho de Melo | db2da3f | 2018-09-03 16:24:09 -0300 | [diff] [blame] | 1765 | int augmented_args_size = 0; |
| 1766 | void *augmented_args = NULL; |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 1767 | struct syscall *sc = trace__syscall_info(trace, evsel, id); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1768 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1769 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1770 | if (sc == NULL) |
| 1771 | return -1; |
| 1772 | |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 1773 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1774 | ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1775 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1776 | goto out_put; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1777 | |
Arnaldo Carvalho de Melo | 591421e | 2018-01-22 11:38:54 -0300 | [diff] [blame] | 1778 | trace__fprintf_sample(trace, evsel, sample, thread); |
| 1779 | |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 1780 | args = perf_evsel__sc_tp_ptr(evsel, args, sample); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1781 | |
| 1782 | if (ttrace->entry_str == NULL) { |
Arnaldo Carvalho de Melo | e4d44e8 | 2015-08-04 22:17:29 -0300 | [diff] [blame] | 1783 | ttrace->entry_str = malloc(trace__entry_str_size); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1784 | if (!ttrace->entry_str) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1785 | goto out_put; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1786 | } |
| 1787 | |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 1788 | if (!(trace->duration_filter || trace->summary_only || trace->min_stack)) |
Arnaldo Carvalho de Melo | 522283f | 2018-01-22 11:42:11 -0300 | [diff] [blame] | 1789 | trace__printf_interrupted_entry(trace); |
Arnaldo Carvalho de Melo | db2da3f | 2018-09-03 16:24:09 -0300 | [diff] [blame] | 1790 | /* |
| 1791 | * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible |
| 1792 | * arguments, even if the syscall being handled, say "openat", uses only 4 arguments |
| 1793 | * this breaks syscall__augmented_args() check for augmented args, as we calculate |
| 1794 | * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file, |
| 1795 | * so when handling, say the openat syscall, we end up getting 6 args for the |
| 1796 | * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly |
| 1797 | * thinking that the extra 2 u64 args are the augmented filename, so just check |
| 1798 | * here and avoid using augmented syscalls when the evsel is the raw_syscalls one. |
| 1799 | */ |
| 1800 | if (evsel != trace->syscalls.events.sys_enter) |
Arnaldo Carvalho de Melo | cd26ea6 | 2018-11-03 08:19:56 -0300 | [diff] [blame] | 1801 | augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1802 | ttrace->entry_time = sample->time; |
| 1803 | msg = ttrace->entry_str; |
Arnaldo Carvalho de Melo | e4d44e8 | 2015-08-04 22:17:29 -0300 | [diff] [blame] | 1804 | printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1805 | |
Arnaldo Carvalho de Melo | e4d44e8 | 2015-08-04 22:17:29 -0300 | [diff] [blame] | 1806 | printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed, |
Arnaldo Carvalho de Melo | db2da3f | 2018-09-03 16:24:09 -0300 | [diff] [blame] | 1807 | args, augmented_args, augmented_args_size, trace, thread); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1808 | |
Arnaldo Carvalho de Melo | 5089f20 | 2014-06-17 14:29:24 -0300 | [diff] [blame] | 1809 | if (sc->is_exit) { |
Arnaldo Carvalho de Melo | 0a6545b | 2018-03-29 12:22:59 -0300 | [diff] [blame] | 1810 | if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) { |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 1811 | trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output); |
Arnaldo Carvalho de Melo | c008f78 | 2016-05-17 12:13:54 -0300 | [diff] [blame] | 1812 | fprintf(trace->output, "%-70s)\n", ttrace->entry_str); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 1813 | } |
Arnaldo Carvalho de Melo | 7f4f800 | 2015-08-14 13:16:27 -0300 | [diff] [blame] | 1814 | } else { |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1815 | ttrace->entry_pending = true; |
Arnaldo Carvalho de Melo | 7f4f800 | 2015-08-14 13:16:27 -0300 | [diff] [blame] | 1816 | /* See trace__vfs_getname & trace__sys_exit */ |
| 1817 | ttrace->filename.pending_open = false; |
| 1818 | } |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1819 | |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1820 | if (trace->current != thread) { |
| 1821 | thread__put(trace->current); |
| 1822 | trace->current = thread__get(thread); |
| 1823 | } |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1824 | err = 0; |
| 1825 | out_put: |
| 1826 | thread__put(thread); |
| 1827 | return err; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1828 | } |
| 1829 | |
Arnaldo Carvalho de Melo | a98392b | 2018-08-02 14:05:09 -0300 | [diff] [blame] | 1830 | static int trace__fprintf_sys_enter(struct trace *trace, struct perf_evsel *evsel, |
| 1831 | struct perf_sample *sample) |
| 1832 | { |
Arnaldo Carvalho de Melo | a98392b | 2018-08-02 14:05:09 -0300 | [diff] [blame] | 1833 | struct thread_trace *ttrace; |
| 1834 | struct thread *thread; |
Arnaldo Carvalho de Melo | f3acd88 | 2018-08-02 15:11:58 -0300 | [diff] [blame] | 1835 | int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1; |
| 1836 | struct syscall *sc = trace__syscall_info(trace, evsel, id); |
Arnaldo Carvalho de Melo | a98392b | 2018-08-02 14:05:09 -0300 | [diff] [blame] | 1837 | char msg[1024]; |
Arnaldo Carvalho de Melo | 7a983a0 | 2018-08-20 17:58:17 -0300 | [diff] [blame] | 1838 | void *args, *augmented_args = NULL; |
| 1839 | int augmented_args_size; |
Arnaldo Carvalho de Melo | a98392b | 2018-08-02 14:05:09 -0300 | [diff] [blame] | 1840 | |
Arnaldo Carvalho de Melo | a98392b | 2018-08-02 14:05:09 -0300 | [diff] [blame] | 1841 | if (sc == NULL) |
| 1842 | return -1; |
| 1843 | |
| 1844 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
| 1845 | ttrace = thread__trace(thread, trace->output); |
| 1846 | /* |
| 1847 | * We need to get ttrace just to make sure it is there when syscall__scnprintf_args() |
| 1848 | * and the rest of the beautifiers accessing it via struct syscall_arg touches it. |
| 1849 | */ |
| 1850 | if (ttrace == NULL) |
| 1851 | goto out_put; |
| 1852 | |
Arnaldo Carvalho de Melo | f3acd88 | 2018-08-02 15:11:58 -0300 | [diff] [blame] | 1853 | args = perf_evsel__sc_tp_ptr(evsel, args, sample); |
Arnaldo Carvalho de Melo | cd26ea6 | 2018-11-03 08:19:56 -0300 | [diff] [blame] | 1854 | augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls); |
Arnaldo Carvalho de Melo | 7a983a0 | 2018-08-20 17:58:17 -0300 | [diff] [blame] | 1855 | syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread); |
Arnaldo Carvalho de Melo | a98392b | 2018-08-02 14:05:09 -0300 | [diff] [blame] | 1856 | fprintf(trace->output, "%s", msg); |
| 1857 | err = 0; |
| 1858 | out_put: |
| 1859 | thread__put(thread); |
| 1860 | return err; |
| 1861 | } |
| 1862 | |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 1863 | static int trace__resolve_callchain(struct trace *trace, struct perf_evsel *evsel, |
| 1864 | struct perf_sample *sample, |
| 1865 | struct callchain_cursor *cursor) |
Arnaldo Carvalho de Melo | 202ff96 | 2016-04-12 10:11:07 -0300 | [diff] [blame] | 1866 | { |
| 1867 | struct addr_location al; |
Ravi Bangoria | 3a9e9a4 | 2018-01-30 11:00:53 +0530 | [diff] [blame] | 1868 | int max_stack = evsel->attr.sample_max_stack ? |
| 1869 | evsel->attr.sample_max_stack : |
| 1870 | trace->max_stack; |
Arnaldo Carvalho de Melo | 4291bf5 | 2018-10-20 09:18:50 -0300 | [diff] [blame] | 1871 | int err; |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 1872 | |
Arnaldo Carvalho de Melo | 4291bf5 | 2018-10-20 09:18:50 -0300 | [diff] [blame] | 1873 | if (machine__resolve(trace->host, &al, sample) < 0) |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 1874 | return -1; |
| 1875 | |
Arnaldo Carvalho de Melo | 4291bf5 | 2018-10-20 09:18:50 -0300 | [diff] [blame] | 1876 | err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack); |
| 1877 | addr_location__put(&al); |
| 1878 | return err; |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample) |
| 1882 | { |
Arnaldo Carvalho de Melo | 202ff96 | 2016-04-12 10:11:07 -0300 | [diff] [blame] | 1883 | /* TODO: user-configurable print_opts */ |
Arnaldo Carvalho de Melo | e20ab86 | 2016-04-12 15:16:15 -0300 | [diff] [blame] | 1884 | const unsigned int print_opts = EVSEL__PRINT_SYM | |
| 1885 | EVSEL__PRINT_DSO | |
| 1886 | EVSEL__PRINT_UNKNOWN_AS_ADDR; |
Arnaldo Carvalho de Melo | 202ff96 | 2016-04-12 10:11:07 -0300 | [diff] [blame] | 1887 | |
Arnaldo Carvalho de Melo | d327e60 | 2016-04-14 17:53:49 -0300 | [diff] [blame] | 1888 | return sample__fprintf_callchain(sample, 38, print_opts, &callchain_cursor, trace->output); |
Arnaldo Carvalho de Melo | 202ff96 | 2016-04-12 10:11:07 -0300 | [diff] [blame] | 1889 | } |
| 1890 | |
Hendrik Brueckner | 092bd3c | 2018-01-19 09:56:16 +0100 | [diff] [blame] | 1891 | static const char *errno_to_name(struct perf_evsel *evsel, int err) |
| 1892 | { |
| 1893 | struct perf_env *env = perf_evsel__env(evsel); |
| 1894 | const char *arch_name = perf_env__arch(env); |
| 1895 | |
| 1896 | return arch_syscalls__strerrno(arch_name, err); |
| 1897 | } |
| 1898 | |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1899 | static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel, |
Stanislav Fomichev | 0c82adc | 2014-06-26 20:14:24 +0400 | [diff] [blame] | 1900 | union perf_event *event __maybe_unused, |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1901 | struct perf_sample *sample) |
| 1902 | { |
Chang Hyun Park | 2c82c3a | 2014-09-26 21:54:01 +0900 | [diff] [blame] | 1903 | long ret; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 1904 | u64 duration = 0; |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 1905 | bool duration_calculated = false; |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1906 | struct thread *thread; |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 1907 | int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0; |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 1908 | struct syscall *sc = trace__syscall_info(trace, evsel, id); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1909 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1910 | |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1911 | if (sc == NULL) |
| 1912 | return -1; |
| 1913 | |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 1914 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1915 | ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 2ae3a31 | 2013-08-09 12:28:31 -0300 | [diff] [blame] | 1916 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1917 | goto out_put; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1918 | |
Arnaldo Carvalho de Melo | 591421e | 2018-01-22 11:38:54 -0300 | [diff] [blame] | 1919 | trace__fprintf_sample(trace, evsel, sample, thread); |
| 1920 | |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 1921 | if (trace->summary) |
| 1922 | thread__update_stats(ttrace, id, sample); |
| 1923 | |
Arnaldo Carvalho de Melo | 7717098 | 2013-11-06 16:35:57 -0300 | [diff] [blame] | 1924 | ret = perf_evsel__sc_tp_uint(evsel, ret, sample); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1925 | |
Arnaldo Carvalho de Melo | 6a648b5 | 2018-08-02 10:30:08 -0300 | [diff] [blame] | 1926 | if (sc->is_open && ret >= 0 && ttrace->filename.pending_open) { |
Arnaldo Carvalho de Melo | 7f4f800 | 2015-08-14 13:16:27 -0300 | [diff] [blame] | 1927 | trace__set_fd_pathname(thread, ret, ttrace->filename.name); |
| 1928 | ttrace->filename.pending_open = false; |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 1929 | ++trace->stats.vfs_getname; |
| 1930 | } |
| 1931 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 1932 | if (ttrace->entry_time) { |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 1933 | duration = sample->time - ttrace->entry_time; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 1934 | if (trace__filter_duration(trace, duration)) |
| 1935 | goto out; |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 1936 | duration_calculated = true; |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 1937 | } else if (trace->duration_filter) |
| 1938 | goto out; |
Arnaldo Carvalho de Melo | 60c907a | 2012-10-24 17:24:47 -0200 | [diff] [blame] | 1939 | |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 1940 | if (sample->callchain) { |
| 1941 | callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); |
| 1942 | if (callchain_ret == 0) { |
| 1943 | if (callchain_cursor.nr < trace->min_stack) |
| 1944 | goto out; |
| 1945 | callchain_ret = 1; |
| 1946 | } |
| 1947 | } |
| 1948 | |
Arnaldo Carvalho de Melo | 0a6545b | 2018-03-29 12:22:59 -0300 | [diff] [blame] | 1949 | if (trace->summary_only || (ret >= 0 && trace->failure_only)) |
David Ahern | fd2eaba | 2013-11-12 09:31:15 -0700 | [diff] [blame] | 1950 | goto out; |
| 1951 | |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 1952 | trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1953 | |
| 1954 | if (ttrace->entry_pending) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1955 | fprintf(trace->output, "%-70s", ttrace->entry_str); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1956 | } else { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1957 | fprintf(trace->output, " ... ["); |
| 1958 | color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued"); |
| 1959 | fprintf(trace->output, "]: %s()", sc->name); |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 1960 | } |
| 1961 | |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 1962 | if (sc->fmt == NULL) { |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 1963 | if (ret < 0) |
| 1964 | goto errno_print; |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 1965 | signed_print: |
Arnaldo Carvalho de Melo | 6f8fe61 | 2017-07-19 11:39:47 -0300 | [diff] [blame] | 1966 | fprintf(trace->output, ") = %ld", ret); |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 1967 | } else if (ret < 0) { |
| 1968 | errno_print: { |
Masami Hiramatsu | 942a91e | 2014-08-14 02:22:41 +0000 | [diff] [blame] | 1969 | char bf[STRERR_BUFSIZE]; |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 1970 | const char *emsg = str_error_r(-ret, bf, sizeof(bf)), |
Hendrik Brueckner | 092bd3c | 2018-01-19 09:56:16 +0100 | [diff] [blame] | 1971 | *e = errno_to_name(evsel, -ret); |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 1972 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1973 | fprintf(trace->output, ") = -1 %s %s", e, emsg); |
Arnaldo Carvalho de Melo | 1f63139 | 2017-07-18 12:45:57 -0300 | [diff] [blame] | 1974 | } |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 1975 | } else if (ret == 0 && sc->fmt->timeout) |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 1976 | fprintf(trace->output, ") = 0 Timeout"); |
Arnaldo Carvalho de Melo | 84486ca | 2017-07-14 11:35:03 -0300 | [diff] [blame] | 1977 | else if (ttrace->ret_scnprintf) { |
| 1978 | char bf[1024]; |
Arnaldo Carvalho de Melo | 7ee5743 | 2017-07-14 15:16:54 -0300 | [diff] [blame] | 1979 | struct syscall_arg arg = { |
| 1980 | .val = ret, |
| 1981 | .thread = thread, |
| 1982 | .trace = trace, |
| 1983 | }; |
| 1984 | ttrace->ret_scnprintf(bf, sizeof(bf), &arg); |
Arnaldo Carvalho de Melo | 84486ca | 2017-07-14 11:35:03 -0300 | [diff] [blame] | 1985 | ttrace->ret_scnprintf = NULL; |
| 1986 | fprintf(trace->output, ") = %s", bf); |
| 1987 | } else if (sc->fmt->hexret) |
Chang Hyun Park | 2c82c3a | 2014-09-26 21:54:01 +0900 | [diff] [blame] | 1988 | fprintf(trace->output, ") = %#lx", ret); |
Arnaldo Carvalho de Melo | 11c8e39 | 2016-04-06 14:33:07 -0300 | [diff] [blame] | 1989 | else if (sc->fmt->errpid) { |
| 1990 | struct thread *child = machine__find_thread(trace->host, ret, ret); |
| 1991 | |
| 1992 | if (child != NULL) { |
| 1993 | fprintf(trace->output, ") = %ld", ret); |
| 1994 | if (child->comm_set) |
| 1995 | fprintf(trace->output, " (%s)", thread__comm_str(child)); |
| 1996 | thread__put(child); |
| 1997 | } |
| 1998 | } else |
Arnaldo Carvalho de Melo | da3c9a4 | 2013-08-26 11:28:34 -0300 | [diff] [blame] | 1999 | goto signed_print; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 2000 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 2001 | fputc('\n', trace->output); |
Milian Wolff | 566a088 | 2016-04-08 13:34:15 +0200 | [diff] [blame] | 2002 | |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 2003 | /* |
| 2004 | * We only consider an 'event' for the sake of --max-events a non-filtered |
| 2005 | * sys_enter + sys_exit and other tracepoint events. |
| 2006 | */ |
| 2007 | if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX) |
| 2008 | interrupted = true; |
| 2009 | |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 2010 | if (callchain_ret > 0) |
| 2011 | trace__fprintf_callchain(trace, sample); |
| 2012 | else if (callchain_ret < 0) |
| 2013 | pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 2014 | out: |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 2015 | ttrace->entry_pending = false; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 2016 | err = 0; |
| 2017 | out_put: |
| 2018 | thread__put(thread); |
| 2019 | return err; |
Arnaldo Carvalho de Melo | ba3d7de | 2012-09-28 17:58:36 -0300 | [diff] [blame] | 2020 | } |
| 2021 | |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2022 | static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel, |
Stanislav Fomichev | 0c82adc | 2014-06-26 20:14:24 +0400 | [diff] [blame] | 2023 | union perf_event *event __maybe_unused, |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2024 | struct perf_sample *sample) |
| 2025 | { |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 2026 | struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
| 2027 | struct thread_trace *ttrace; |
| 2028 | size_t filename_len, entry_str_len, to_move; |
| 2029 | ssize_t remaining_space; |
| 2030 | char *pos; |
Arnaldo Carvalho de Melo | 7f4f800 | 2015-08-14 13:16:27 -0300 | [diff] [blame] | 2031 | const char *filename = perf_evsel__rawptr(evsel, sample, "pathname"); |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 2032 | |
| 2033 | if (!thread) |
| 2034 | goto out; |
| 2035 | |
| 2036 | ttrace = thread__priv(thread); |
| 2037 | if (!ttrace) |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2038 | goto out_put; |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 2039 | |
Arnaldo Carvalho de Melo | 7f4f800 | 2015-08-14 13:16:27 -0300 | [diff] [blame] | 2040 | filename_len = strlen(filename); |
Arnaldo Carvalho de Melo | 39f0e7a | 2017-03-24 14:51:28 -0300 | [diff] [blame] | 2041 | if (filename_len == 0) |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2042 | goto out_put; |
Arnaldo Carvalho de Melo | 7f4f800 | 2015-08-14 13:16:27 -0300 | [diff] [blame] | 2043 | |
| 2044 | if (ttrace->filename.namelen < filename_len) { |
| 2045 | char *f = realloc(ttrace->filename.name, filename_len + 1); |
| 2046 | |
| 2047 | if (f == NULL) |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2048 | goto out_put; |
Arnaldo Carvalho de Melo | 7f4f800 | 2015-08-14 13:16:27 -0300 | [diff] [blame] | 2049 | |
| 2050 | ttrace->filename.namelen = filename_len; |
| 2051 | ttrace->filename.name = f; |
| 2052 | } |
| 2053 | |
| 2054 | strcpy(ttrace->filename.name, filename); |
| 2055 | ttrace->filename.pending_open = true; |
| 2056 | |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 2057 | if (!ttrace->filename.ptr) |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2058 | goto out_put; |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 2059 | |
| 2060 | entry_str_len = strlen(ttrace->entry_str); |
| 2061 | remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */ |
| 2062 | if (remaining_space <= 0) |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2063 | goto out_put; |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 2064 | |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 2065 | if (filename_len > (size_t)remaining_space) { |
| 2066 | filename += filename_len - remaining_space; |
| 2067 | filename_len = remaining_space; |
| 2068 | } |
| 2069 | |
| 2070 | to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */ |
| 2071 | pos = ttrace->entry_str + ttrace->filename.entry_str_pos; |
| 2072 | memmove(pos + filename_len, pos, to_move); |
| 2073 | memcpy(pos, filename, filename_len); |
| 2074 | |
| 2075 | ttrace->filename.ptr = 0; |
| 2076 | ttrace->filename.entry_str_pos = 0; |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2077 | out_put: |
| 2078 | thread__put(thread); |
Arnaldo Carvalho de Melo | f994592 | 2015-08-04 22:30:09 -0300 | [diff] [blame] | 2079 | out: |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2080 | return 0; |
| 2081 | } |
| 2082 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 2083 | static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel, |
Stanislav Fomichev | 0c82adc | 2014-06-26 20:14:24 +0400 | [diff] [blame] | 2084 | union perf_event *event __maybe_unused, |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 2085 | struct perf_sample *sample) |
| 2086 | { |
| 2087 | u64 runtime = perf_evsel__intval(evsel, sample, "runtime"); |
| 2088 | double runtime_ms = (double)runtime / NSEC_PER_MSEC; |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 2089 | struct thread *thread = machine__findnew_thread(trace->host, |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 2090 | sample->pid, |
| 2091 | sample->tid); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 2092 | struct thread_trace *ttrace = thread__trace(thread, trace->output); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 2093 | |
| 2094 | if (ttrace == NULL) |
| 2095 | goto out_dump; |
| 2096 | |
| 2097 | ttrace->runtime_ms += runtime_ms; |
| 2098 | trace->runtime_ms += runtime_ms; |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2099 | out_put: |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 2100 | thread__put(thread); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 2101 | return 0; |
| 2102 | |
| 2103 | out_dump: |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 2104 | fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n", |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 2105 | evsel->name, |
| 2106 | perf_evsel__strval(evsel, sample, "comm"), |
| 2107 | (pid_t)perf_evsel__intval(evsel, sample, "pid"), |
| 2108 | runtime, |
| 2109 | perf_evsel__intval(evsel, sample, "vruntime")); |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2110 | goto out_put; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 2111 | } |
| 2112 | |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 2113 | static int bpf_output__printer(enum binary_printer_ops op, |
| 2114 | unsigned int val, void *extra __maybe_unused, FILE *fp) |
Wang Nan | 1d6c940 | 2016-02-26 09:31:55 +0000 | [diff] [blame] | 2115 | { |
Wang Nan | 1d6c940 | 2016-02-26 09:31:55 +0000 | [diff] [blame] | 2116 | unsigned char ch = (unsigned char)val; |
| 2117 | |
| 2118 | switch (op) { |
| 2119 | case BINARY_PRINT_CHAR_DATA: |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 2120 | return fprintf(fp, "%c", isprint(ch) ? ch : '.'); |
Wang Nan | 1d6c940 | 2016-02-26 09:31:55 +0000 | [diff] [blame] | 2121 | case BINARY_PRINT_DATA_BEGIN: |
| 2122 | case BINARY_PRINT_LINE_BEGIN: |
| 2123 | case BINARY_PRINT_ADDR: |
| 2124 | case BINARY_PRINT_NUM_DATA: |
| 2125 | case BINARY_PRINT_NUM_PAD: |
| 2126 | case BINARY_PRINT_SEP: |
| 2127 | case BINARY_PRINT_CHAR_PAD: |
| 2128 | case BINARY_PRINT_LINE_END: |
| 2129 | case BINARY_PRINT_DATA_END: |
| 2130 | default: |
| 2131 | break; |
| 2132 | } |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 2133 | |
| 2134 | return 0; |
Wang Nan | 1d6c940 | 2016-02-26 09:31:55 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | static void bpf_output__fprintf(struct trace *trace, |
| 2138 | struct perf_sample *sample) |
| 2139 | { |
Arnaldo Carvalho de Melo | 923d0c9 | 2017-10-17 10:35:00 -0300 | [diff] [blame] | 2140 | binary__fprintf(sample->raw_data, sample->raw_size, 8, |
| 2141 | bpf_output__printer, NULL, trace->output); |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 2142 | ++trace->nr_events_printed; |
Wang Nan | 1d6c940 | 2016-02-26 09:31:55 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 2145 | static int trace__event_handler(struct trace *trace, struct perf_evsel *evsel, |
| 2146 | union perf_event *event __maybe_unused, |
| 2147 | struct perf_sample *sample) |
| 2148 | { |
Arnaldo Carvalho de Melo | a9c5e6c | 2018-10-22 14:14:16 -0300 | [diff] [blame] | 2149 | struct thread *thread; |
Arnaldo Carvalho de Melo | 7ad3561 | 2016-04-20 19:55:48 -0300 | [diff] [blame] | 2150 | int callchain_ret = 0; |
Arnaldo Carvalho de Melo | a9c5e6c | 2018-10-22 14:14:16 -0300 | [diff] [blame] | 2151 | /* |
| 2152 | * Check if we called perf_evsel__disable(evsel) due to, for instance, |
| 2153 | * this event's max_events having been hit and this is an entry coming |
| 2154 | * from the ring buffer that we should discard, since the max events |
| 2155 | * have already been considered/printed. |
| 2156 | */ |
| 2157 | if (evsel->disabled) |
| 2158 | return 0; |
| 2159 | |
| 2160 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
Arnaldo Carvalho de Melo | 7ad3561 | 2016-04-20 19:55:48 -0300 | [diff] [blame] | 2161 | |
| 2162 | if (sample->callchain) { |
| 2163 | callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); |
| 2164 | if (callchain_ret == 0) { |
| 2165 | if (callchain_cursor.nr < trace->min_stack) |
| 2166 | goto out; |
| 2167 | callchain_ret = 1; |
| 2168 | } |
| 2169 | } |
| 2170 | |
Arnaldo Carvalho de Melo | 522283f | 2018-01-22 11:42:11 -0300 | [diff] [blame] | 2171 | trace__printf_interrupted_entry(trace); |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 2172 | trace__fprintf_tstamp(trace, sample->time, trace->output); |
Arnaldo Carvalho de Melo | 0808921 | 2015-02-19 21:51:50 -0800 | [diff] [blame] | 2173 | |
| 2174 | if (trace->trace_syscalls) |
| 2175 | fprintf(trace->output, "( ): "); |
| 2176 | |
Arnaldo Carvalho de Melo | c4191e5 | 2018-08-30 13:37:28 -0300 | [diff] [blame] | 2177 | if (thread) |
| 2178 | trace__fprintf_comm_tid(trace, thread, trace->output); |
| 2179 | |
Arnaldo Carvalho de Melo | 1cdf618 | 2018-08-21 11:44:23 -0300 | [diff] [blame] | 2180 | if (evsel == trace->syscalls.events.augmented) { |
| 2181 | int id = perf_evsel__sc_tp_uint(evsel, id, sample); |
| 2182 | struct syscall *sc = trace__syscall_info(trace, evsel, id); |
| 2183 | |
| 2184 | if (sc) { |
Arnaldo Carvalho de Melo | c4191e5 | 2018-08-30 13:37:28 -0300 | [diff] [blame] | 2185 | fprintf(trace->output, "%s(", sc->name); |
| 2186 | trace__fprintf_sys_enter(trace, evsel, sample); |
| 2187 | fputc(')', trace->output); |
| 2188 | goto newline; |
Arnaldo Carvalho de Melo | 1cdf618 | 2018-08-21 11:44:23 -0300 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * XXX: Not having the associated syscall info or not finding/adding |
| 2193 | * the thread should never happen, but if it does... |
| 2194 | * fall thru and print it as a bpf_output event. |
| 2195 | */ |
| 2196 | } |
| 2197 | |
Arnaldo Carvalho de Melo | 0808921 | 2015-02-19 21:51:50 -0800 | [diff] [blame] | 2198 | fprintf(trace->output, "%s:", evsel->name); |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 2199 | |
Wang Nan | 1d6c940 | 2016-02-26 09:31:55 +0000 | [diff] [blame] | 2200 | if (perf_evsel__is_bpf_output(evsel)) { |
Arnaldo Carvalho de Melo | 1cdf618 | 2018-08-21 11:44:23 -0300 | [diff] [blame] | 2201 | bpf_output__fprintf(trace, sample); |
Wang Nan | 1d6c940 | 2016-02-26 09:31:55 +0000 | [diff] [blame] | 2202 | } else if (evsel->tp_format) { |
Arnaldo Carvalho de Melo | a98392b | 2018-08-02 14:05:09 -0300 | [diff] [blame] | 2203 | if (strncmp(evsel->tp_format->name, "sys_enter_", 10) || |
| 2204 | trace__fprintf_sys_enter(trace, evsel, sample)) { |
| 2205 | event_format__fprintf(evsel->tp_format, sample->cpu, |
| 2206 | sample->raw_data, sample->raw_size, |
| 2207 | trace->output); |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 2208 | ++trace->nr_events_printed; |
Arnaldo Carvalho de Melo | a9c5e6c | 2018-10-22 14:14:16 -0300 | [diff] [blame] | 2209 | |
| 2210 | if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) { |
| 2211 | perf_evsel__disable(evsel); |
| 2212 | perf_evsel__close(evsel); |
| 2213 | } |
Arnaldo Carvalho de Melo | a98392b | 2018-08-02 14:05:09 -0300 | [diff] [blame] | 2214 | } |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 2215 | } |
| 2216 | |
Arnaldo Carvalho de Melo | 1cdf618 | 2018-08-21 11:44:23 -0300 | [diff] [blame] | 2217 | newline: |
Changbin Du | 51125a2 | 2018-03-13 18:40:01 +0800 | [diff] [blame] | 2218 | fprintf(trace->output, "\n"); |
Arnaldo Carvalho de Melo | 202ff96 | 2016-04-12 10:11:07 -0300 | [diff] [blame] | 2219 | |
Arnaldo Carvalho de Melo | 7ad3561 | 2016-04-20 19:55:48 -0300 | [diff] [blame] | 2220 | if (callchain_ret > 0) |
| 2221 | trace__fprintf_callchain(trace, sample); |
| 2222 | else if (callchain_ret < 0) |
| 2223 | pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); |
| 2224 | out: |
Arnaldo Carvalho de Melo | a937c66 | 2018-10-20 09:27:52 -0300 | [diff] [blame] | 2225 | thread__put(thread); |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 2226 | return 0; |
| 2227 | } |
| 2228 | |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2229 | static void print_location(FILE *f, struct perf_sample *sample, |
| 2230 | struct addr_location *al, |
| 2231 | bool print_dso, bool print_sym) |
| 2232 | { |
| 2233 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 2234 | if ((verbose > 0 || print_dso) && al->map) |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2235 | fprintf(f, "%s@", al->map->dso->long_name); |
| 2236 | |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 2237 | if ((verbose > 0 || print_sym) && al->sym) |
Arnaldo Carvalho de Melo | 4414a3c | 2014-07-08 15:39:21 -0300 | [diff] [blame] | 2238 | fprintf(f, "%s+0x%" PRIx64, al->sym->name, |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2239 | al->addr - al->sym->start); |
| 2240 | else if (al->map) |
Arnaldo Carvalho de Melo | 4414a3c | 2014-07-08 15:39:21 -0300 | [diff] [blame] | 2241 | fprintf(f, "0x%" PRIx64, al->addr); |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2242 | else |
Arnaldo Carvalho de Melo | 4414a3c | 2014-07-08 15:39:21 -0300 | [diff] [blame] | 2243 | fprintf(f, "0x%" PRIx64, sample->addr); |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | static int trace__pgfault(struct trace *trace, |
| 2247 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 2248 | union perf_event *event __maybe_unused, |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2249 | struct perf_sample *sample) |
| 2250 | { |
| 2251 | struct thread *thread; |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2252 | struct addr_location al; |
| 2253 | char map_type = 'd'; |
Stanislav Fomichev | a2ea67d | 2014-07-08 22:05:16 +0400 | [diff] [blame] | 2254 | struct thread_trace *ttrace; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 2255 | int err = -1; |
Arnaldo Carvalho de Melo | 1df5429 | 2016-04-20 20:06:02 -0300 | [diff] [blame] | 2256 | int callchain_ret = 0; |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2257 | |
| 2258 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
Arnaldo Carvalho de Melo | 1df5429 | 2016-04-20 20:06:02 -0300 | [diff] [blame] | 2259 | |
| 2260 | if (sample->callchain) { |
| 2261 | callchain_ret = trace__resolve_callchain(trace, evsel, sample, &callchain_cursor); |
| 2262 | if (callchain_ret == 0) { |
| 2263 | if (callchain_cursor.nr < trace->min_stack) |
| 2264 | goto out_put; |
| 2265 | callchain_ret = 1; |
| 2266 | } |
| 2267 | } |
| 2268 | |
Stanislav Fomichev | a2ea67d | 2014-07-08 22:05:16 +0400 | [diff] [blame] | 2269 | ttrace = thread__trace(thread, trace->output); |
| 2270 | if (ttrace == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 2271 | goto out_put; |
Stanislav Fomichev | a2ea67d | 2014-07-08 22:05:16 +0400 | [diff] [blame] | 2272 | |
| 2273 | if (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ) |
| 2274 | ttrace->pfmaj++; |
| 2275 | else |
| 2276 | ttrace->pfmin++; |
| 2277 | |
| 2278 | if (trace->summary_only) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 2279 | goto out; |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2280 | |
Arnaldo Carvalho de Melo | 4546263 | 2018-04-24 11:24:49 -0300 | [diff] [blame] | 2281 | thread__find_symbol(thread, sample->cpumode, sample->ip, &al); |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2282 | |
Arnaldo Carvalho de Melo | fd2b297 | 2017-03-29 16:37:51 -0300 | [diff] [blame] | 2283 | trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output); |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2284 | |
| 2285 | fprintf(trace->output, "%sfault [", |
| 2286 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ? |
| 2287 | "maj" : "min"); |
| 2288 | |
| 2289 | print_location(trace->output, sample, &al, false, true); |
| 2290 | |
| 2291 | fprintf(trace->output, "] => "); |
| 2292 | |
Arnaldo Carvalho de Melo | 117d3c2 | 2018-04-25 18:16:53 -0300 | [diff] [blame] | 2293 | thread__find_symbol(thread, sample->cpumode, sample->addr, &al); |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2294 | |
| 2295 | if (!al.map) { |
Arnaldo Carvalho de Melo | 4546263 | 2018-04-24 11:24:49 -0300 | [diff] [blame] | 2296 | thread__find_symbol(thread, sample->cpumode, sample->addr, &al); |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2297 | |
| 2298 | if (al.map) |
| 2299 | map_type = 'x'; |
| 2300 | else |
| 2301 | map_type = '?'; |
| 2302 | } |
| 2303 | |
| 2304 | print_location(trace->output, sample, &al, true, false); |
| 2305 | |
| 2306 | fprintf(trace->output, " (%c%c)\n", map_type, al.level); |
Arnaldo Carvalho de Melo | 0c3a6ef | 2016-04-19 16:31:12 -0300 | [diff] [blame] | 2307 | |
Arnaldo Carvalho de Melo | 1df5429 | 2016-04-20 20:06:02 -0300 | [diff] [blame] | 2308 | if (callchain_ret > 0) |
| 2309 | trace__fprintf_callchain(trace, sample); |
| 2310 | else if (callchain_ret < 0) |
| 2311 | pr_err("Problem processing %s callchain, skipping...\n", perf_evsel__name(evsel)); |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 2312 | |
| 2313 | ++trace->nr_events_printed; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 2314 | out: |
| 2315 | err = 0; |
| 2316 | out_put: |
| 2317 | thread__put(thread); |
| 2318 | return err; |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2319 | } |
| 2320 | |
Arnaldo Carvalho de Melo | e600198 | 2016-03-31 15:16:28 -0300 | [diff] [blame] | 2321 | static void trace__set_base_time(struct trace *trace, |
Arnaldo Carvalho de Melo | 8a07a80 | 2016-03-31 15:19:39 -0300 | [diff] [blame] | 2322 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | e600198 | 2016-03-31 15:16:28 -0300 | [diff] [blame] | 2323 | struct perf_sample *sample) |
| 2324 | { |
Arnaldo Carvalho de Melo | 8a07a80 | 2016-03-31 15:19:39 -0300 | [diff] [blame] | 2325 | /* |
| 2326 | * BPF events were not setting PERF_SAMPLE_TIME, so be more robust |
| 2327 | * and don't use sample->time unconditionally, we may end up having |
| 2328 | * some other event in the future without PERF_SAMPLE_TIME for good |
| 2329 | * reason, i.e. we may not be interested in its timestamps, just in |
| 2330 | * it taking place, picking some piece of information when it |
| 2331 | * appears in our event stream (vfs_getname comes to mind). |
| 2332 | */ |
| 2333 | if (trace->base_time == 0 && !trace->full_time && |
| 2334 | (evsel->attr.sample_type & PERF_SAMPLE_TIME)) |
Arnaldo Carvalho de Melo | e600198 | 2016-03-31 15:16:28 -0300 | [diff] [blame] | 2335 | trace->base_time = sample->time; |
| 2336 | } |
| 2337 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2338 | static int trace__process_sample(struct perf_tool *tool, |
Stanislav Fomichev | 0c82adc | 2014-06-26 20:14:24 +0400 | [diff] [blame] | 2339 | union perf_event *event, |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2340 | struct perf_sample *sample, |
| 2341 | struct perf_evsel *evsel, |
| 2342 | struct machine *machine __maybe_unused) |
| 2343 | { |
| 2344 | struct trace *trace = container_of(tool, struct trace, tool); |
David Ahern | aa07df6 | 2016-11-25 09:29:52 -0700 | [diff] [blame] | 2345 | struct thread *thread; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2346 | int err = 0; |
| 2347 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 2348 | tracepoint_handler handler = evsel->handler; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2349 | |
David Ahern | aa07df6 | 2016-11-25 09:29:52 -0700 | [diff] [blame] | 2350 | thread = machine__findnew_thread(trace->host, sample->pid, sample->tid); |
| 2351 | if (thread && thread__is_filtered(thread)) |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2352 | goto out; |
David Ahern | bdc8966 | 2013-08-28 22:29:53 -0600 | [diff] [blame] | 2353 | |
Arnaldo Carvalho de Melo | e600198 | 2016-03-31 15:16:28 -0300 | [diff] [blame] | 2354 | trace__set_base_time(trace, evsel, sample); |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2355 | |
David Ahern | 3160565 | 2013-12-04 19:41:41 -0700 | [diff] [blame] | 2356 | if (handler) { |
| 2357 | ++trace->nr_events; |
Stanislav Fomichev | 0c82adc | 2014-06-26 20:14:24 +0400 | [diff] [blame] | 2358 | handler(trace, evsel, event, sample); |
David Ahern | 3160565 | 2013-12-04 19:41:41 -0700 | [diff] [blame] | 2359 | } |
Arnaldo Carvalho de Melo | ef65e96 | 2017-03-24 15:03:19 -0300 | [diff] [blame] | 2360 | out: |
| 2361 | thread__put(thread); |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2362 | return err; |
| 2363 | } |
| 2364 | |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 2365 | static int trace__record(struct trace *trace, int argc, const char **argv) |
David Ahern | 5e2485b | 2013-09-28 13:13:01 -0600 | [diff] [blame] | 2366 | { |
| 2367 | unsigned int rec_argc, i, j; |
| 2368 | const char **rec_argv; |
| 2369 | const char * const record_args[] = { |
| 2370 | "record", |
| 2371 | "-R", |
| 2372 | "-m", "1024", |
| 2373 | "-c", "1", |
David Ahern | 5e2485b | 2013-09-28 13:13:01 -0600 | [diff] [blame] | 2374 | }; |
| 2375 | |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 2376 | const char * const sc_args[] = { "-e", }; |
| 2377 | unsigned int sc_args_nr = ARRAY_SIZE(sc_args); |
| 2378 | const char * const majpf_args[] = { "-e", "major-faults" }; |
| 2379 | unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args); |
| 2380 | const char * const minpf_args[] = { "-e", "minor-faults" }; |
| 2381 | unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args); |
| 2382 | |
David Ahern | 9aca7f1 | 2013-12-04 19:41:39 -0700 | [diff] [blame] | 2383 | /* +1 is for the event string below */ |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 2384 | rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 1 + |
| 2385 | majpf_args_nr + minpf_args_nr + argc; |
David Ahern | 5e2485b | 2013-09-28 13:13:01 -0600 | [diff] [blame] | 2386 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 2387 | |
| 2388 | if (rec_argv == NULL) |
| 2389 | return -ENOMEM; |
| 2390 | |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 2391 | j = 0; |
David Ahern | 5e2485b | 2013-09-28 13:13:01 -0600 | [diff] [blame] | 2392 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 2393 | rec_argv[j++] = record_args[i]; |
| 2394 | |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 2395 | if (trace->trace_syscalls) { |
| 2396 | for (i = 0; i < sc_args_nr; i++) |
| 2397 | rec_argv[j++] = sc_args[i]; |
David Ahern | 5e2485b | 2013-09-28 13:13:01 -0600 | [diff] [blame] | 2398 | |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 2399 | /* event string may be different for older kernels - e.g., RHEL6 */ |
| 2400 | if (is_valid_tracepoint("raw_syscalls:sys_enter")) |
| 2401 | rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit"; |
| 2402 | else if (is_valid_tracepoint("syscalls:sys_enter")) |
| 2403 | rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit"; |
| 2404 | else { |
| 2405 | pr_err("Neither raw_syscalls nor syscalls events exist.\n"); |
Martin Kepplinger | c896f85 | 2017-09-13 21:14:19 +0200 | [diff] [blame] | 2406 | free(rec_argv); |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 2407 | return -1; |
| 2408 | } |
David Ahern | 9aca7f1 | 2013-12-04 19:41:39 -0700 | [diff] [blame] | 2409 | } |
David Ahern | 9aca7f1 | 2013-12-04 19:41:39 -0700 | [diff] [blame] | 2410 | |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 2411 | if (trace->trace_pgfaults & TRACE_PFMAJ) |
| 2412 | for (i = 0; i < majpf_args_nr; i++) |
| 2413 | rec_argv[j++] = majpf_args[i]; |
David Ahern | 5e2485b | 2013-09-28 13:13:01 -0600 | [diff] [blame] | 2414 | |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 2415 | if (trace->trace_pgfaults & TRACE_PFMIN) |
| 2416 | for (i = 0; i < minpf_args_nr; i++) |
| 2417 | rec_argv[j++] = minpf_args[i]; |
| 2418 | |
| 2419 | for (i = 0; i < (unsigned int)argc; i++) |
| 2420 | rec_argv[j++] = argv[i]; |
| 2421 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 2422 | return cmd_record(j, rec_argv); |
David Ahern | 5e2485b | 2013-09-28 13:13:01 -0600 | [diff] [blame] | 2423 | } |
| 2424 | |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 2425 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp); |
| 2426 | |
Arnaldo Carvalho de Melo | 08c9877 | 2015-08-04 17:01:04 -0300 | [diff] [blame] | 2427 | static bool perf_evlist__add_vfs_getname(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2428 | { |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 2429 | struct perf_evsel *evsel = perf_evsel__newtp("probe", "vfs_getname"); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 2430 | |
| 2431 | if (IS_ERR(evsel)) |
Arnaldo Carvalho de Melo | 08c9877 | 2015-08-04 17:01:04 -0300 | [diff] [blame] | 2432 | return false; |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2433 | |
| 2434 | if (perf_evsel__field(evsel, "pathname") == NULL) { |
| 2435 | perf_evsel__delete(evsel); |
Arnaldo Carvalho de Melo | 08c9877 | 2015-08-04 17:01:04 -0300 | [diff] [blame] | 2436 | return false; |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2437 | } |
| 2438 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 2439 | evsel->handler = trace__vfs_getname; |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2440 | perf_evlist__add(evlist, evsel); |
Arnaldo Carvalho de Melo | 08c9877 | 2015-08-04 17:01:04 -0300 | [diff] [blame] | 2441 | return true; |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2442 | } |
| 2443 | |
Arnaldo Carvalho de Melo | 0ae537c | 2016-04-19 16:00:01 -0300 | [diff] [blame] | 2444 | static struct perf_evsel *perf_evsel__new_pgfault(u64 config) |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2445 | { |
| 2446 | struct perf_evsel *evsel; |
| 2447 | struct perf_event_attr attr = { |
| 2448 | .type = PERF_TYPE_SOFTWARE, |
| 2449 | .mmap_data = 1, |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2450 | }; |
| 2451 | |
| 2452 | attr.config = config; |
Arnaldo Carvalho de Melo | 0524798 | 2014-07-23 18:15:09 -0300 | [diff] [blame] | 2453 | attr.sample_period = 1; |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2454 | |
| 2455 | event_attr_init(&attr); |
| 2456 | |
| 2457 | evsel = perf_evsel__new(&attr); |
Arnaldo Carvalho de Melo | 0ae537c | 2016-04-19 16:00:01 -0300 | [diff] [blame] | 2458 | if (evsel) |
| 2459 | evsel->handler = trace__pgfault; |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2460 | |
Arnaldo Carvalho de Melo | 0ae537c | 2016-04-19 16:00:01 -0300 | [diff] [blame] | 2461 | return evsel; |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2462 | } |
| 2463 | |
Arnaldo Carvalho de Melo | ddbb1b1 | 2015-02-21 12:10:29 -0800 | [diff] [blame] | 2464 | static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample) |
| 2465 | { |
| 2466 | const u32 type = event->header.type; |
| 2467 | struct perf_evsel *evsel; |
| 2468 | |
Arnaldo Carvalho de Melo | ddbb1b1 | 2015-02-21 12:10:29 -0800 | [diff] [blame] | 2469 | if (type != PERF_RECORD_SAMPLE) { |
| 2470 | trace__process_event(trace, trace->host, event, sample); |
| 2471 | return; |
| 2472 | } |
| 2473 | |
| 2474 | evsel = perf_evlist__id2evsel(trace->evlist, sample->id); |
| 2475 | if (evsel == NULL) { |
| 2476 | fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id); |
| 2477 | return; |
| 2478 | } |
| 2479 | |
Arnaldo Carvalho de Melo | e600198 | 2016-03-31 15:16:28 -0300 | [diff] [blame] | 2480 | trace__set_base_time(trace, evsel, sample); |
| 2481 | |
Arnaldo Carvalho de Melo | ddbb1b1 | 2015-02-21 12:10:29 -0800 | [diff] [blame] | 2482 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && |
| 2483 | sample->raw_data == NULL) { |
| 2484 | fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n", |
| 2485 | perf_evsel__name(evsel), sample->tid, |
| 2486 | sample->cpu, sample->raw_size); |
| 2487 | } else { |
| 2488 | tracepoint_handler handler = evsel->handler; |
| 2489 | handler(trace, evsel, event, sample); |
| 2490 | } |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 2491 | |
| 2492 | if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX) |
| 2493 | interrupted = true; |
Arnaldo Carvalho de Melo | ddbb1b1 | 2015-02-21 12:10:29 -0800 | [diff] [blame] | 2494 | } |
| 2495 | |
Arnaldo Carvalho de Melo | c27366f | 2015-07-02 18:24:51 -0300 | [diff] [blame] | 2496 | static int trace__add_syscall_newtp(struct trace *trace) |
| 2497 | { |
| 2498 | int ret = -1; |
| 2499 | struct perf_evlist *evlist = trace->evlist; |
| 2500 | struct perf_evsel *sys_enter, *sys_exit; |
| 2501 | |
Arnaldo Carvalho de Melo | 63f11c8 | 2018-08-02 14:27:09 -0300 | [diff] [blame] | 2502 | sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter); |
Arnaldo Carvalho de Melo | c27366f | 2015-07-02 18:24:51 -0300 | [diff] [blame] | 2503 | if (sys_enter == NULL) |
| 2504 | goto out; |
| 2505 | |
| 2506 | if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args)) |
| 2507 | goto out_delete_sys_enter; |
| 2508 | |
Arnaldo Carvalho de Melo | 63f11c8 | 2018-08-02 14:27:09 -0300 | [diff] [blame] | 2509 | sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit); |
Arnaldo Carvalho de Melo | c27366f | 2015-07-02 18:24:51 -0300 | [diff] [blame] | 2510 | if (sys_exit == NULL) |
| 2511 | goto out_delete_sys_enter; |
| 2512 | |
| 2513 | if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret)) |
| 2514 | goto out_delete_sys_exit; |
| 2515 | |
Arnaldo Carvalho de Melo | 08e2639 | 2018-01-12 13:29:05 -0300 | [diff] [blame] | 2516 | perf_evsel__config_callchain(sys_enter, &trace->opts, &callchain_param); |
| 2517 | perf_evsel__config_callchain(sys_exit, &trace->opts, &callchain_param); |
| 2518 | |
Arnaldo Carvalho de Melo | c27366f | 2015-07-02 18:24:51 -0300 | [diff] [blame] | 2519 | perf_evlist__add(evlist, sys_enter); |
| 2520 | perf_evlist__add(evlist, sys_exit); |
| 2521 | |
Arnaldo Carvalho de Melo | 2ddd5c0 | 2016-04-18 12:09:08 -0300 | [diff] [blame] | 2522 | if (callchain_param.enabled && !trace->kernel_syscallchains) { |
Arnaldo Carvalho de Melo | 4462181 | 2016-04-11 15:49:11 -0300 | [diff] [blame] | 2523 | /* |
| 2524 | * We're interested only in the user space callchain |
| 2525 | * leading to the syscall, allow overriding that for |
| 2526 | * debugging reasons using --kernel_syscall_callchains |
| 2527 | */ |
| 2528 | sys_exit->attr.exclude_callchain_kernel = 1; |
| 2529 | } |
| 2530 | |
Arnaldo Carvalho de Melo | 8b3ce75 | 2015-07-02 18:28:11 -0300 | [diff] [blame] | 2531 | trace->syscalls.events.sys_enter = sys_enter; |
| 2532 | trace->syscalls.events.sys_exit = sys_exit; |
Arnaldo Carvalho de Melo | c27366f | 2015-07-02 18:24:51 -0300 | [diff] [blame] | 2533 | |
| 2534 | ret = 0; |
| 2535 | out: |
| 2536 | return ret; |
| 2537 | |
| 2538 | out_delete_sys_exit: |
| 2539 | perf_evsel__delete_priv(sys_exit); |
| 2540 | out_delete_sys_enter: |
| 2541 | perf_evsel__delete_priv(sys_enter); |
| 2542 | goto out; |
| 2543 | } |
| 2544 | |
Arnaldo Carvalho de Melo | 19867b6 | 2015-07-04 12:44:59 -0300 | [diff] [blame] | 2545 | static int trace__set_ev_qualifier_filter(struct trace *trace) |
| 2546 | { |
| 2547 | int err = -1; |
Mathieu Poirier | b15d0a4 | 2016-09-16 08:44:03 -0600 | [diff] [blame] | 2548 | struct perf_evsel *sys_exit; |
Arnaldo Carvalho de Melo | 19867b6 | 2015-07-04 12:44:59 -0300 | [diff] [blame] | 2549 | char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier, |
| 2550 | trace->ev_qualifier_ids.nr, |
| 2551 | trace->ev_qualifier_ids.entries); |
| 2552 | |
| 2553 | if (filter == NULL) |
| 2554 | goto out_enomem; |
| 2555 | |
Mathieu Poirier | 3541c03 | 2016-09-16 08:44:04 -0600 | [diff] [blame] | 2556 | if (!perf_evsel__append_tp_filter(trace->syscalls.events.sys_enter, |
| 2557 | filter)) { |
Mathieu Poirier | b15d0a4 | 2016-09-16 08:44:03 -0600 | [diff] [blame] | 2558 | sys_exit = trace->syscalls.events.sys_exit; |
Mathieu Poirier | 3541c03 | 2016-09-16 08:44:04 -0600 | [diff] [blame] | 2559 | err = perf_evsel__append_tp_filter(sys_exit, filter); |
Mathieu Poirier | b15d0a4 | 2016-09-16 08:44:03 -0600 | [diff] [blame] | 2560 | } |
Arnaldo Carvalho de Melo | 19867b6 | 2015-07-04 12:44:59 -0300 | [diff] [blame] | 2561 | |
| 2562 | free(filter); |
| 2563 | out: |
| 2564 | return err; |
| 2565 | out_enomem: |
| 2566 | errno = ENOMEM; |
| 2567 | goto out; |
| 2568 | } |
Arnaldo Carvalho de Melo | c27366f | 2015-07-02 18:24:51 -0300 | [diff] [blame] | 2569 | |
Arnaldo Carvalho de Melo | a9964c4 | 2018-11-07 10:08:00 -0300 | [diff] [blame] | 2570 | static int bpf_map__set_filter_pids(struct bpf_map *map __maybe_unused, |
| 2571 | size_t npids __maybe_unused, pid_t *pids __maybe_unused) |
| 2572 | { |
| 2573 | int err = 0; |
| 2574 | #ifdef HAVE_LIBBPF_SUPPORT |
| 2575 | bool value = true; |
| 2576 | int map_fd = bpf_map__fd(map); |
| 2577 | size_t i; |
| 2578 | |
| 2579 | for (i = 0; i < npids; ++i) { |
| 2580 | err = bpf_map_update_elem(map_fd, &pids[i], &value, BPF_ANY); |
| 2581 | if (err) |
| 2582 | break; |
| 2583 | } |
| 2584 | #endif |
| 2585 | return err; |
| 2586 | } |
| 2587 | |
Arnaldo Carvalho de Melo | dd1a503 | 2017-07-20 11:17:51 -0300 | [diff] [blame] | 2588 | static int trace__set_filter_loop_pids(struct trace *trace) |
| 2589 | { |
Arnaldo Carvalho de Melo | a9964c4 | 2018-11-07 10:08:00 -0300 | [diff] [blame] | 2590 | unsigned int nr = 1, err; |
Arnaldo Carvalho de Melo | dd1a503 | 2017-07-20 11:17:51 -0300 | [diff] [blame] | 2591 | pid_t pids[32] = { |
| 2592 | getpid(), |
| 2593 | }; |
Arnaldo Carvalho de Melo | 082ab9a | 2017-07-20 11:32:05 -0300 | [diff] [blame] | 2594 | struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]); |
| 2595 | |
| 2596 | while (thread && nr < ARRAY_SIZE(pids)) { |
| 2597 | struct thread *parent = machine__find_thread(trace->host, thread->ppid, thread->ppid); |
| 2598 | |
| 2599 | if (parent == NULL) |
| 2600 | break; |
| 2601 | |
| 2602 | if (!strcmp(thread__comm_str(parent), "sshd")) { |
| 2603 | pids[nr++] = parent->tid; |
| 2604 | break; |
| 2605 | } |
| 2606 | thread = parent; |
| 2607 | } |
Arnaldo Carvalho de Melo | dd1a503 | 2017-07-20 11:17:51 -0300 | [diff] [blame] | 2608 | |
Arnaldo Carvalho de Melo | a9964c4 | 2018-11-07 10:08:00 -0300 | [diff] [blame] | 2609 | err = perf_evlist__set_tp_filter_pids(trace->evlist, nr, pids); |
| 2610 | if (!err && trace->filter_pids.map) |
| 2611 | err = bpf_map__set_filter_pids(trace->filter_pids.map, nr, pids); |
| 2612 | |
| 2613 | return err; |
| 2614 | } |
| 2615 | |
| 2616 | static int trace__set_filter_pids(struct trace *trace) |
| 2617 | { |
| 2618 | int err = 0; |
| 2619 | /* |
| 2620 | * Better not use !target__has_task() here because we need to cover the |
| 2621 | * case where no threads were specified in the command line, but a |
| 2622 | * workload was, and in that case we will fill in the thread_map when |
| 2623 | * we fork the workload in perf_evlist__prepare_workload. |
| 2624 | */ |
| 2625 | if (trace->filter_pids.nr > 0) { |
| 2626 | err = perf_evlist__set_tp_filter_pids(trace->evlist, trace->filter_pids.nr, |
| 2627 | trace->filter_pids.entries); |
| 2628 | if (!err && trace->filter_pids.map) { |
| 2629 | err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr, |
| 2630 | trace->filter_pids.entries); |
| 2631 | } |
| 2632 | } else if (thread_map__pid(trace->evlist->threads, 0) == -1) { |
| 2633 | err = trace__set_filter_loop_pids(trace); |
| 2634 | } |
| 2635 | |
| 2636 | return err; |
Arnaldo Carvalho de Melo | dd1a503 | 2017-07-20 11:17:51 -0300 | [diff] [blame] | 2637 | } |
| 2638 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 2639 | static int trace__run(struct trace *trace, int argc, const char **argv) |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2640 | { |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 2641 | struct perf_evlist *evlist = trace->evlist; |
Arnaldo Carvalho de Melo | 0ae537c | 2016-04-19 16:00:01 -0300 | [diff] [blame] | 2642 | struct perf_evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL; |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 2643 | int err = -1, i; |
| 2644 | unsigned long before; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 2645 | const bool forks = argc > 0; |
Arnaldo Carvalho de Melo | 46fb3c2 | 2014-09-22 14:39:48 -0300 | [diff] [blame] | 2646 | bool draining = false; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2647 | |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 2648 | trace->live = true; |
| 2649 | |
Arnaldo Carvalho de Melo | c27366f | 2015-07-02 18:24:51 -0300 | [diff] [blame] | 2650 | if (trace->trace_syscalls && trace__add_syscall_newtp(trace)) |
Arnaldo Carvalho de Melo | 801c67b | 2015-01-22 10:52:55 -0300 | [diff] [blame] | 2651 | goto out_error_raw_syscalls; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2652 | |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 2653 | if (trace->trace_syscalls) |
Arnaldo Carvalho de Melo | 08c9877 | 2015-08-04 17:01:04 -0300 | [diff] [blame] | 2654 | trace->vfs_getname = perf_evlist__add_vfs_getname(evlist); |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2655 | |
Arnaldo Carvalho de Melo | 0ae537c | 2016-04-19 16:00:01 -0300 | [diff] [blame] | 2656 | if ((trace->trace_pgfaults & TRACE_PFMAJ)) { |
| 2657 | pgfault_maj = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ); |
| 2658 | if (pgfault_maj == NULL) |
| 2659 | goto out_error_mem; |
Arnaldo Carvalho de Melo | 08e2639 | 2018-01-12 13:29:05 -0300 | [diff] [blame] | 2660 | perf_evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param); |
Arnaldo Carvalho de Melo | 0ae537c | 2016-04-19 16:00:01 -0300 | [diff] [blame] | 2661 | perf_evlist__add(evlist, pgfault_maj); |
Arnaldo Carvalho de Melo | e2726d9 | 2015-01-22 10:34:22 -0300 | [diff] [blame] | 2662 | } |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2663 | |
Arnaldo Carvalho de Melo | 0ae537c | 2016-04-19 16:00:01 -0300 | [diff] [blame] | 2664 | if ((trace->trace_pgfaults & TRACE_PFMIN)) { |
| 2665 | pgfault_min = perf_evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN); |
| 2666 | if (pgfault_min == NULL) |
| 2667 | goto out_error_mem; |
Arnaldo Carvalho de Melo | 08e2639 | 2018-01-12 13:29:05 -0300 | [diff] [blame] | 2668 | perf_evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param); |
Arnaldo Carvalho de Melo | 0ae537c | 2016-04-19 16:00:01 -0300 | [diff] [blame] | 2669 | perf_evlist__add(evlist, pgfault_min); |
| 2670 | } |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 2671 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 2672 | if (trace->sched && |
Arnaldo Carvalho de Melo | 2cc990b | 2015-01-22 11:13:43 -0300 | [diff] [blame] | 2673 | perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime", |
| 2674 | trace__sched_stat_runtime)) |
| 2675 | goto out_error_sched_stat_runtime; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 2676 | |
Arnaldo Carvalho de Melo | 9ea42ba | 2018-03-06 16:30:51 -0300 | [diff] [blame] | 2677 | /* |
| 2678 | * If a global cgroup was set, apply it to all the events without an |
| 2679 | * explicit cgroup. I.e.: |
| 2680 | * |
| 2681 | * trace -G A -e sched:*switch |
| 2682 | * |
| 2683 | * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc |
| 2684 | * _and_ sched:sched_switch to the 'A' cgroup, while: |
| 2685 | * |
| 2686 | * trace -e sched:*switch -G A |
| 2687 | * |
| 2688 | * will only set the sched:sched_switch event to the 'A' cgroup, all the |
| 2689 | * other events (raw_syscalls:sys_{enter,exit}, etc are left "without" |
| 2690 | * a cgroup (on the root cgroup, sys wide, etc). |
| 2691 | * |
| 2692 | * Multiple cgroups: |
| 2693 | * |
| 2694 | * trace -G A -e sched:*switch -G B |
| 2695 | * |
| 2696 | * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes |
| 2697 | * to the 'B' cgroup. |
| 2698 | * |
| 2699 | * evlist__set_default_cgroup() grabs a reference of the passed cgroup |
| 2700 | * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL. |
| 2701 | */ |
| 2702 | if (trace->cgroup) |
| 2703 | evlist__set_default_cgroup(trace->evlist, trace->cgroup); |
| 2704 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2705 | err = perf_evlist__create_maps(evlist, &trace->opts.target); |
| 2706 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 2707 | fprintf(trace->output, "Problems parsing the target to trace, check your options!\n"); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2708 | goto out_delete_evlist; |
| 2709 | } |
| 2710 | |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 2711 | err = trace__symbols_init(trace, evlist); |
| 2712 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 2713 | fprintf(trace->output, "Problems initializing symbol libraries!\n"); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 2714 | goto out_delete_evlist; |
Arnaldo Carvalho de Melo | 752fde4 | 2012-10-06 18:43:19 -0300 | [diff] [blame] | 2715 | } |
| 2716 | |
Arnaldo Carvalho de Melo | 75d5011 | 2018-01-15 10:39:55 -0300 | [diff] [blame] | 2717 | perf_evlist__config(evlist, &trace->opts, &callchain_param); |
Arnaldo Carvalho de Melo | fde54b7 | 2016-04-11 18:42:37 -0300 | [diff] [blame] | 2718 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 2719 | signal(SIGCHLD, sig_handler); |
| 2720 | signal(SIGINT, sig_handler); |
| 2721 | |
| 2722 | if (forks) { |
Namhyung Kim | 6ef73ec | 2013-03-11 16:43:15 +0900 | [diff] [blame] | 2723 | err = perf_evlist__prepare_workload(evlist, &trace->opts.target, |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 2724 | argv, false, NULL); |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 2725 | if (err < 0) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 2726 | fprintf(trace->output, "Couldn't run the workload!\n"); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 2727 | goto out_delete_evlist; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 2728 | } |
| 2729 | } |
| 2730 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2731 | err = perf_evlist__open(evlist); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 2732 | if (err < 0) |
| 2733 | goto out_error_open; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2734 | |
Wang Nan | ba50423 | 2016-02-26 09:31:54 +0000 | [diff] [blame] | 2735 | err = bpf__apply_obj_config(); |
| 2736 | if (err) { |
| 2737 | char errbuf[BUFSIZ]; |
| 2738 | |
| 2739 | bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); |
| 2740 | pr_err("ERROR: Apply config to BPF failed: %s\n", |
| 2741 | errbuf); |
| 2742 | goto out_error_open; |
| 2743 | } |
| 2744 | |
Arnaldo Carvalho de Melo | a9964c4 | 2018-11-07 10:08:00 -0300 | [diff] [blame] | 2745 | err = trace__set_filter_pids(trace); |
Arnaldo Carvalho de Melo | 94ad89b | 2015-07-03 17:42:03 -0300 | [diff] [blame] | 2746 | if (err < 0) |
| 2747 | goto out_error_mem; |
| 2748 | |
Arnaldo Carvalho de Melo | 19867b6 | 2015-07-04 12:44:59 -0300 | [diff] [blame] | 2749 | if (trace->ev_qualifier_ids.nr > 0) { |
| 2750 | err = trace__set_ev_qualifier_filter(trace); |
| 2751 | if (err < 0) |
| 2752 | goto out_errno; |
Arnaldo Carvalho de Melo | 19867b6 | 2015-07-04 12:44:59 -0300 | [diff] [blame] | 2753 | |
Arnaldo Carvalho de Melo | 2e5e5f8 | 2015-08-03 17:12:29 -0300 | [diff] [blame] | 2754 | pr_debug("event qualifier tracepoint filter: %s\n", |
| 2755 | trace->syscalls.events.sys_exit->filter); |
| 2756 | } |
Arnaldo Carvalho de Melo | 19867b6 | 2015-07-04 12:44:59 -0300 | [diff] [blame] | 2757 | |
Arnaldo Carvalho de Melo | 94ad89b | 2015-07-03 17:42:03 -0300 | [diff] [blame] | 2758 | err = perf_evlist__apply_filters(evlist, &evsel); |
| 2759 | if (err < 0) |
| 2760 | goto out_error_apply_filters; |
Arnaldo Carvalho de Melo | 241b057 | 2015-02-21 10:15:21 -0800 | [diff] [blame] | 2761 | |
Wang Nan | f74b9d3a | 2017-12-03 02:00:37 +0000 | [diff] [blame] | 2762 | err = perf_evlist__mmap(evlist, trace->opts.mmap_pages); |
Arnaldo Carvalho de Melo | e09b18d | 2014-12-11 18:04:10 -0300 | [diff] [blame] | 2763 | if (err < 0) |
| 2764 | goto out_error_mmap; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2765 | |
Alexis Berlemont | e36b782 | 2016-10-10 07:43:28 +0200 | [diff] [blame] | 2766 | if (!target__none(&trace->opts.target) && !trace->opts.initial_delay) |
Arnaldo Carvalho de Melo | cb24d01 | 2015-04-22 10:04:23 -0300 | [diff] [blame] | 2767 | perf_evlist__enable(evlist); |
| 2768 | |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 2769 | if (forks) |
| 2770 | perf_evlist__start_workload(evlist); |
| 2771 | |
Alexis Berlemont | e36b782 | 2016-10-10 07:43:28 +0200 | [diff] [blame] | 2772 | if (trace->opts.initial_delay) { |
| 2773 | usleep(trace->opts.initial_delay * 1000); |
| 2774 | perf_evlist__enable(evlist); |
| 2775 | } |
| 2776 | |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 2777 | trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 || |
Arnaldo Carvalho de Melo | 42052be | 2015-02-13 12:32:45 -0300 | [diff] [blame] | 2778 | evlist->threads->nr > 1 || |
| 2779 | perf_evlist__first(evlist)->attr.inherit; |
Arnaldo Carvalho de Melo | bd3dda9 | 2018-01-15 11:33:53 -0300 | [diff] [blame] | 2780 | |
| 2781 | /* |
| 2782 | * Now that we already used evsel->attr to ask the kernel to setup the |
| 2783 | * events, lets reuse evsel->attr.sample_max_stack as the limit in |
| 2784 | * trace__resolve_callchain(), allowing per-event max-stack settings |
Ingo Molnar | adba163 | 2018-12-03 11:22:00 +0100 | [diff] [blame^] | 2785 | * to override an explicitly set --max-stack global setting. |
Arnaldo Carvalho de Melo | bd3dda9 | 2018-01-15 11:33:53 -0300 | [diff] [blame] | 2786 | */ |
| 2787 | evlist__for_each_entry(evlist, evsel) { |
Arnaldo Carvalho de Melo | 27de9b2 | 2018-05-28 16:00:29 -0300 | [diff] [blame] | 2788 | if (evsel__has_callchain(evsel) && |
Arnaldo Carvalho de Melo | bd3dda9 | 2018-01-15 11:33:53 -0300 | [diff] [blame] | 2789 | evsel->attr.sample_max_stack == 0) |
| 2790 | evsel->attr.sample_max_stack = trace->max_stack; |
| 2791 | } |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2792 | again: |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 2793 | before = trace->nr_events; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2794 | |
| 2795 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 2796 | union perf_event *event; |
Kan Liang | d7f55c6 | 2018-03-01 18:08:59 -0500 | [diff] [blame] | 2797 | struct perf_mmap *md; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2798 | |
Kan Liang | d7f55c6 | 2018-03-01 18:08:59 -0500 | [diff] [blame] | 2799 | md = &evlist->mmap[i]; |
Kan Liang | b9bae2c | 2018-03-06 10:36:07 -0500 | [diff] [blame] | 2800 | if (perf_mmap__read_init(md) < 0) |
Kan Liang | d7f55c6 | 2018-03-01 18:08:59 -0500 | [diff] [blame] | 2801 | continue; |
| 2802 | |
Kan Liang | 0019dc87 | 2018-03-06 10:36:06 -0500 | [diff] [blame] | 2803 | while ((event = perf_mmap__read_event(md)) != NULL) { |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2804 | struct perf_sample sample; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2805 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 2806 | ++trace->nr_events; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2807 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2808 | err = perf_evlist__parse_sample(evlist, event, &sample); |
| 2809 | if (err) { |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 2810 | fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err); |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 2811 | goto next_event; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2812 | } |
| 2813 | |
Arnaldo Carvalho de Melo | ddbb1b1 | 2015-02-21 12:10:29 -0800 | [diff] [blame] | 2814 | trace__handle_event(trace, event, &sample); |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 2815 | next_event: |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 2816 | perf_mmap__consume(md); |
Arnaldo Carvalho de Melo | 20c5f10 | 2013-09-03 11:55:07 -0300 | [diff] [blame] | 2817 | |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 2818 | if (interrupted) |
| 2819 | goto out_disable; |
Arnaldo Carvalho de Melo | 02ac542 | 2015-04-22 11:11:57 -0300 | [diff] [blame] | 2820 | |
| 2821 | if (done && !draining) { |
| 2822 | perf_evlist__disable(evlist); |
| 2823 | draining = true; |
| 2824 | } |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2825 | } |
Kan Liang | d7f55c6 | 2018-03-01 18:08:59 -0500 | [diff] [blame] | 2826 | perf_mmap__read_done(md); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2827 | } |
| 2828 | |
Arnaldo Carvalho de Melo | efd5745 | 2012-10-17 17:09:46 -0300 | [diff] [blame] | 2829 | if (trace->nr_events == before) { |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 2830 | int timeout = done ? 100 : -1; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 2831 | |
Arnaldo Carvalho de Melo | 46fb3c2 | 2014-09-22 14:39:48 -0300 | [diff] [blame] | 2832 | if (!draining && perf_evlist__poll(evlist, timeout) > 0) { |
Arnaldo Carvalho de Melo | a9c5e6c | 2018-10-22 14:14:16 -0300 | [diff] [blame] | 2833 | if (perf_evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0) |
Arnaldo Carvalho de Melo | 46fb3c2 | 2014-09-22 14:39:48 -0300 | [diff] [blame] | 2834 | draining = true; |
| 2835 | |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 2836 | goto again; |
Arnaldo Carvalho de Melo | 46fb3c2 | 2014-09-22 14:39:48 -0300 | [diff] [blame] | 2837 | } |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 2838 | } else { |
| 2839 | goto again; |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 2840 | } |
| 2841 | |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 2842 | out_disable: |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 2843 | thread__zput(trace->current); |
| 2844 | |
Arnaldo Carvalho de Melo | ba209f8 | 2013-10-16 11:57:33 -0300 | [diff] [blame] | 2845 | perf_evlist__disable(evlist); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2846 | |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2847 | if (!err) { |
| 2848 | if (trace->summary) |
| 2849 | trace__fprintf_thread_summary(trace, trace->output); |
| 2850 | |
| 2851 | if (trace->show_tool_stats) { |
| 2852 | fprintf(trace->output, "Stats:\n " |
| 2853 | " vfs_getname : %" PRIu64 "\n" |
| 2854 | " proc_getname: %" PRIu64 "\n", |
| 2855 | trace->stats.vfs_getname, |
| 2856 | trace->stats.proc_getname); |
| 2857 | } |
| 2858 | } |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 2859 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2860 | out_delete_evlist: |
Andrei Vagin | 33974a4 | 2017-11-07 16:22:45 -0800 | [diff] [blame] | 2861 | trace__symbols__exit(trace); |
| 2862 | |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2863 | perf_evlist__delete(evlist); |
Arnaldo Carvalho de Melo | 9ea42ba | 2018-03-06 16:30:51 -0300 | [diff] [blame] | 2864 | cgroup__put(trace->cgroup); |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 2865 | trace->evlist = NULL; |
Arnaldo Carvalho de Melo | 75b757ca | 2013-09-24 11:04:32 -0300 | [diff] [blame] | 2866 | trace->live = false; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2867 | return err; |
Arnaldo Carvalho de Melo | 6ef068c | 2013-10-17 12:07:58 -0300 | [diff] [blame] | 2868 | { |
| 2869 | char errbuf[BUFSIZ]; |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 2870 | |
Arnaldo Carvalho de Melo | 2cc990b | 2015-01-22 11:13:43 -0300 | [diff] [blame] | 2871 | out_error_sched_stat_runtime: |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 2872 | tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime"); |
Arnaldo Carvalho de Melo | 2cc990b | 2015-01-22 11:13:43 -0300 | [diff] [blame] | 2873 | goto out_error; |
| 2874 | |
Arnaldo Carvalho de Melo | 801c67b | 2015-01-22 10:52:55 -0300 | [diff] [blame] | 2875 | out_error_raw_syscalls: |
Jiri Olsa | 988bdb3 | 2015-09-02 09:56:35 +0200 | [diff] [blame] | 2876 | tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)"); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 2877 | goto out_error; |
| 2878 | |
Arnaldo Carvalho de Melo | e09b18d | 2014-12-11 18:04:10 -0300 | [diff] [blame] | 2879 | out_error_mmap: |
| 2880 | perf_evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf)); |
| 2881 | goto out_error; |
| 2882 | |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 2883 | out_error_open: |
| 2884 | perf_evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf)); |
| 2885 | |
| 2886 | out_error: |
Arnaldo Carvalho de Melo | 6ef068c | 2013-10-17 12:07:58 -0300 | [diff] [blame] | 2887 | fprintf(trace->output, "%s\n", errbuf); |
Ramkumar Ramachandra | 87f9186 | 2013-10-04 10:47:31 +0530 | [diff] [blame] | 2888 | goto out_delete_evlist; |
Arnaldo Carvalho de Melo | 94ad89b | 2015-07-03 17:42:03 -0300 | [diff] [blame] | 2889 | |
| 2890 | out_error_apply_filters: |
| 2891 | fprintf(trace->output, |
| 2892 | "Failed to set filter \"%s\" on event %s with %d (%s)\n", |
| 2893 | evsel->filter, perf_evsel__name(evsel), errno, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 2894 | str_error_r(errno, errbuf, sizeof(errbuf))); |
Arnaldo Carvalho de Melo | 94ad89b | 2015-07-03 17:42:03 -0300 | [diff] [blame] | 2895 | goto out_delete_evlist; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2896 | } |
Arnaldo Carvalho de Melo | 5ed08da | 2015-01-22 11:08:04 -0300 | [diff] [blame] | 2897 | out_error_mem: |
| 2898 | fprintf(trace->output, "Not enough memory to run!\n"); |
| 2899 | goto out_delete_evlist; |
Arnaldo Carvalho de Melo | 19867b6 | 2015-07-04 12:44:59 -0300 | [diff] [blame] | 2900 | |
| 2901 | out_errno: |
| 2902 | fprintf(trace->output, "errno=%d,%s\n", errno, strerror(errno)); |
| 2903 | goto out_delete_evlist; |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 2904 | } |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 2905 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2906 | static int trace__replay(struct trace *trace) |
| 2907 | { |
| 2908 | const struct perf_evsel_str_handler handlers[] = { |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 2909 | { "probe:vfs_getname", trace__vfs_getname, }, |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2910 | }; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2911 | struct perf_data data = { |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 2912 | .file = { |
| 2913 | .path = input_name, |
| 2914 | }, |
| 2915 | .mode = PERF_DATA_MODE_READ, |
| 2916 | .force = trace->force, |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 2917 | }; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2918 | struct perf_session *session; |
Namhyung Kim | 003824e | 2013-11-12 15:25:00 +0900 | [diff] [blame] | 2919 | struct perf_evsel *evsel; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2920 | int err = -1; |
| 2921 | |
| 2922 | trace->tool.sample = trace__process_sample; |
| 2923 | trace->tool.mmap = perf_event__process_mmap; |
David Ahern | 384c671 | 2013-09-22 19:44:58 -0600 | [diff] [blame] | 2924 | trace->tool.mmap2 = perf_event__process_mmap2; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2925 | trace->tool.comm = perf_event__process_comm; |
| 2926 | trace->tool.exit = perf_event__process_exit; |
| 2927 | trace->tool.fork = perf_event__process_fork; |
| 2928 | trace->tool.attr = perf_event__process_attr; |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 2929 | trace->tool.tracing_data = perf_event__process_tracing_data; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2930 | trace->tool.build_id = perf_event__process_build_id; |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 2931 | trace->tool.namespaces = perf_event__process_namespaces; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2932 | |
Jiri Olsa | 0a8cb85 | 2014-07-06 14:18:21 +0200 | [diff] [blame] | 2933 | trace->tool.ordered_events = true; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2934 | trace->tool.ordering_requires_timestamps = true; |
| 2935 | |
| 2936 | /* add tid to output */ |
| 2937 | trace->multiple_threads = true; |
| 2938 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2939 | session = perf_session__new(&data, false, &trace->tool); |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2940 | if (session == NULL) |
Taeung Song | 52e02834 | 2014-09-24 10:33:37 +0900 | [diff] [blame] | 2941 | return -1; |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2942 | |
David Ahern | aa07df6 | 2016-11-25 09:29:52 -0700 | [diff] [blame] | 2943 | if (trace->opts.target.pid) |
| 2944 | symbol_conf.pid_list_str = strdup(trace->opts.target.pid); |
| 2945 | |
| 2946 | if (trace->opts.target.tid) |
| 2947 | symbol_conf.tid_list_str = strdup(trace->opts.target.tid); |
| 2948 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 2949 | if (symbol__init(&session->header.env) < 0) |
Namhyung Kim | cb2ffae | 2014-08-12 15:40:44 +0900 | [diff] [blame] | 2950 | goto out; |
| 2951 | |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 2952 | trace->host = &session->machines.host; |
| 2953 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2954 | err = perf_session__set_tracepoints_handlers(session, handlers); |
| 2955 | if (err) |
| 2956 | goto out; |
| 2957 | |
Namhyung Kim | 003824e | 2013-11-12 15:25:00 +0900 | [diff] [blame] | 2958 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, |
| 2959 | "raw_syscalls:sys_enter"); |
David Ahern | 9aca7f1 | 2013-12-04 19:41:39 -0700 | [diff] [blame] | 2960 | /* older kernels have syscalls tp versus raw_syscalls */ |
| 2961 | if (evsel == NULL) |
| 2962 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, |
| 2963 | "syscalls:sys_enter"); |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2964 | |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 2965 | if (evsel && |
Arnaldo Carvalho de Melo | 63f11c8 | 2018-08-02 14:27:09 -0300 | [diff] [blame] | 2966 | (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 || |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 2967 | perf_evsel__init_sc_tp_ptr_field(evsel, args))) { |
Namhyung Kim | 003824e | 2013-11-12 15:25:00 +0900 | [diff] [blame] | 2968 | pr_err("Error during initialize raw_syscalls:sys_enter event\n"); |
| 2969 | goto out; |
| 2970 | } |
| 2971 | |
| 2972 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, |
| 2973 | "raw_syscalls:sys_exit"); |
David Ahern | 9aca7f1 | 2013-12-04 19:41:39 -0700 | [diff] [blame] | 2974 | if (evsel == NULL) |
| 2975 | evsel = perf_evlist__find_tracepoint_by_name(session->evlist, |
| 2976 | "syscalls:sys_exit"); |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 2977 | if (evsel && |
Arnaldo Carvalho de Melo | 63f11c8 | 2018-08-02 14:27:09 -0300 | [diff] [blame] | 2978 | (perf_evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 || |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 2979 | perf_evsel__init_sc_tp_uint_field(evsel, ret))) { |
Namhyung Kim | 003824e | 2013-11-12 15:25:00 +0900 | [diff] [blame] | 2980 | pr_err("Error during initialize raw_syscalls:sys_exit event\n"); |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2981 | goto out; |
| 2982 | } |
| 2983 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2984 | evlist__for_each_entry(session->evlist, evsel) { |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 2985 | if (evsel->attr.type == PERF_TYPE_SOFTWARE && |
| 2986 | (evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ || |
| 2987 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN || |
| 2988 | evsel->attr.config == PERF_COUNT_SW_PAGE_FAULTS)) |
| 2989 | evsel->handler = trace__pgfault; |
| 2990 | } |
| 2991 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2992 | setup_pager(); |
| 2993 | |
Arnaldo Carvalho de Melo | b7b61cb | 2015-03-03 11:58:45 -0300 | [diff] [blame] | 2994 | err = perf_session__process_events(session); |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 2995 | if (err) |
| 2996 | pr_err("Failed to process events, error %d", err); |
| 2997 | |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 2998 | else if (trace->summary) |
| 2999 | trace__fprintf_thread_summary(trace, trace->output); |
| 3000 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 3001 | out: |
| 3002 | perf_session__delete(session); |
| 3003 | |
| 3004 | return err; |
| 3005 | } |
| 3006 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 3007 | static size_t trace__fprintf_threads_header(FILE *fp) |
| 3008 | { |
| 3009 | size_t printed; |
| 3010 | |
Pekka Enberg | 99ff715 | 2013-11-12 16:42:14 +0200 | [diff] [blame] | 3011 | printed = fprintf(fp, "\n Summary of events:\n\n"); |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3012 | |
| 3013 | return printed; |
| 3014 | } |
| 3015 | |
Arnaldo Carvalho de Melo | b535d52 | 2016-05-04 16:06:26 -0300 | [diff] [blame] | 3016 | DEFINE_RESORT_RB(syscall_stats, a->msecs > b->msecs, |
| 3017 | struct stats *stats; |
| 3018 | double msecs; |
| 3019 | int syscall; |
| 3020 | ) |
| 3021 | { |
| 3022 | struct int_node *source = rb_entry(nd, struct int_node, rb_node); |
| 3023 | struct stats *stats = source->priv; |
| 3024 | |
| 3025 | entry->syscall = source->i; |
| 3026 | entry->stats = stats; |
| 3027 | entry->msecs = stats ? (u64)stats->n * (avg_stats(stats) / NSEC_PER_MSEC) : 0; |
| 3028 | } |
| 3029 | |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3030 | static size_t thread__dump_stats(struct thread_trace *ttrace, |
| 3031 | struct trace *trace, FILE *fp) |
| 3032 | { |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3033 | size_t printed = 0; |
| 3034 | struct syscall *sc; |
Arnaldo Carvalho de Melo | b535d52 | 2016-05-04 16:06:26 -0300 | [diff] [blame] | 3035 | struct rb_node *nd; |
| 3036 | DECLARE_RESORT_RB_INTLIST(syscall_stats, ttrace->syscall_stats); |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3037 | |
Arnaldo Carvalho de Melo | b535d52 | 2016-05-04 16:06:26 -0300 | [diff] [blame] | 3038 | if (syscall_stats == NULL) |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3039 | return 0; |
| 3040 | |
| 3041 | printed += fprintf(fp, "\n"); |
| 3042 | |
Milian Wolff | 834fd46 | 2015-08-06 11:24:29 +0200 | [diff] [blame] | 3043 | printed += fprintf(fp, " syscall calls total min avg max stddev\n"); |
| 3044 | printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n"); |
| 3045 | printed += fprintf(fp, " --------------- -------- --------- --------- --------- --------- ------\n"); |
Pekka Enberg | 99ff715 | 2013-11-12 16:42:14 +0200 | [diff] [blame] | 3046 | |
Arnaldo Carvalho de Melo | 98a9183 | 2016-06-23 11:34:10 -0300 | [diff] [blame] | 3047 | resort_rb__for_each_entry(nd, syscall_stats) { |
Arnaldo Carvalho de Melo | b535d52 | 2016-05-04 16:06:26 -0300 | [diff] [blame] | 3048 | struct stats *stats = syscall_stats_entry->stats; |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3049 | if (stats) { |
| 3050 | double min = (double)(stats->min) / NSEC_PER_MSEC; |
| 3051 | double max = (double)(stats->max) / NSEC_PER_MSEC; |
| 3052 | double avg = avg_stats(stats); |
| 3053 | double pct; |
| 3054 | u64 n = (u64) stats->n; |
| 3055 | |
| 3056 | pct = avg ? 100.0 * stddev_stats(stats)/avg : 0.0; |
| 3057 | avg /= NSEC_PER_MSEC; |
| 3058 | |
Arnaldo Carvalho de Melo | b535d52 | 2016-05-04 16:06:26 -0300 | [diff] [blame] | 3059 | sc = &trace->syscalls.table[syscall_stats_entry->syscall]; |
Pekka Enberg | 99ff715 | 2013-11-12 16:42:14 +0200 | [diff] [blame] | 3060 | printed += fprintf(fp, " %-15s", sc->name); |
Milian Wolff | 834fd46 | 2015-08-06 11:24:29 +0200 | [diff] [blame] | 3061 | printed += fprintf(fp, " %8" PRIu64 " %9.3f %9.3f %9.3f", |
Arnaldo Carvalho de Melo | b535d52 | 2016-05-04 16:06:26 -0300 | [diff] [blame] | 3062 | n, syscall_stats_entry->msecs, min, avg); |
Pekka Enberg | 27a778b | 2013-11-13 14:21:48 +0200 | [diff] [blame] | 3063 | printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct); |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3064 | } |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3065 | } |
| 3066 | |
Arnaldo Carvalho de Melo | b535d52 | 2016-05-04 16:06:26 -0300 | [diff] [blame] | 3067 | resort_rb__delete(syscall_stats); |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3068 | printed += fprintf(fp, "\n\n"); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 3069 | |
| 3070 | return printed; |
| 3071 | } |
| 3072 | |
Arnaldo Carvalho de Melo | 96c1445 | 2016-05-04 12:47:16 -0300 | [diff] [blame] | 3073 | static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace) |
David Ahern | 896cbb5 | 2013-09-28 13:12:59 -0600 | [diff] [blame] | 3074 | { |
Arnaldo Carvalho de Melo | 96c1445 | 2016-05-04 12:47:16 -0300 | [diff] [blame] | 3075 | size_t printed = 0; |
Namhyung Kim | 89dceb2 | 2014-10-06 09:46:03 +0900 | [diff] [blame] | 3076 | struct thread_trace *ttrace = thread__priv(thread); |
David Ahern | 896cbb5 | 2013-09-28 13:12:59 -0600 | [diff] [blame] | 3077 | double ratio; |
| 3078 | |
| 3079 | if (ttrace == NULL) |
| 3080 | return 0; |
| 3081 | |
| 3082 | ratio = (double)ttrace->nr_events / trace->nr_events * 100.0; |
| 3083 | |
Pekka Enberg | 15e65c6 | 2013-11-14 18:43:30 +0200 | [diff] [blame] | 3084 | printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread->tid); |
Pekka Enberg | 99ff715 | 2013-11-12 16:42:14 +0200 | [diff] [blame] | 3085 | printed += fprintf(fp, "%lu events, ", ttrace->nr_events); |
Pekka Enberg | 15e65c6 | 2013-11-14 18:43:30 +0200 | [diff] [blame] | 3086 | printed += fprintf(fp, "%.1f%%", ratio); |
Stanislav Fomichev | a2ea67d | 2014-07-08 22:05:16 +0400 | [diff] [blame] | 3087 | if (ttrace->pfmaj) |
| 3088 | printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj); |
| 3089 | if (ttrace->pfmin) |
| 3090 | printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin); |
Arnaldo Carvalho de Melo | 03548eb | 2016-05-05 15:46:50 -0300 | [diff] [blame] | 3091 | if (trace->sched) |
| 3092 | printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms); |
| 3093 | else if (fputc('\n', fp) != EOF) |
| 3094 | ++printed; |
| 3095 | |
David Ahern | bf2575c | 2013-10-08 21:26:53 -0600 | [diff] [blame] | 3096 | printed += thread__dump_stats(ttrace, trace, fp); |
David Ahern | 896cbb5 | 2013-09-28 13:12:59 -0600 | [diff] [blame] | 3097 | |
Arnaldo Carvalho de Melo | 96c1445 | 2016-05-04 12:47:16 -0300 | [diff] [blame] | 3098 | return printed; |
| 3099 | } |
David Ahern | 896cbb5 | 2013-09-28 13:12:59 -0600 | [diff] [blame] | 3100 | |
Arnaldo Carvalho de Melo | 96c1445 | 2016-05-04 12:47:16 -0300 | [diff] [blame] | 3101 | static unsigned long thread__nr_events(struct thread_trace *ttrace) |
| 3102 | { |
| 3103 | return ttrace ? ttrace->nr_events : 0; |
| 3104 | } |
| 3105 | |
| 3106 | DEFINE_RESORT_RB(threads, (thread__nr_events(a->thread->priv) < thread__nr_events(b->thread->priv)), |
| 3107 | struct thread *thread; |
| 3108 | ) |
| 3109 | { |
| 3110 | entry->thread = rb_entry(nd, struct thread, rb_node); |
David Ahern | 896cbb5 | 2013-09-28 13:12:59 -0600 | [diff] [blame] | 3111 | } |
| 3112 | |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 3113 | static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp) |
| 3114 | { |
Arnaldo Carvalho de Melo | 96c1445 | 2016-05-04 12:47:16 -0300 | [diff] [blame] | 3115 | size_t printed = trace__fprintf_threads_header(fp); |
| 3116 | struct rb_node *nd; |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 3117 | int i; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 3118 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 3119 | for (i = 0; i < THREADS__TABLE_SIZE; i++) { |
| 3120 | DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i); |
| 3121 | |
| 3122 | if (threads == NULL) { |
| 3123 | fprintf(fp, "%s", "Error sorting output by nr_events!\n"); |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
| 3127 | resort_rb__for_each_entry(nd, threads) |
| 3128 | printed += trace__fprintf_thread(fp, threads_entry->thread, trace); |
| 3129 | |
| 3130 | resort_rb__delete(threads); |
Arnaldo Carvalho de Melo | 96c1445 | 2016-05-04 12:47:16 -0300 | [diff] [blame] | 3131 | } |
Arnaldo Carvalho de Melo | 96c1445 | 2016-05-04 12:47:16 -0300 | [diff] [blame] | 3132 | return printed; |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 3133 | } |
| 3134 | |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 3135 | static int trace__set_duration(const struct option *opt, const char *str, |
| 3136 | int unset __maybe_unused) |
| 3137 | { |
| 3138 | struct trace *trace = opt->value; |
| 3139 | |
| 3140 | trace->duration_filter = atof(str); |
| 3141 | return 0; |
| 3142 | } |
| 3143 | |
Arnaldo Carvalho de Melo | 6a0b3ab | 2018-11-07 09:38:49 -0300 | [diff] [blame] | 3144 | static int trace__set_filter_pids_from_option(const struct option *opt, const char *str, |
| 3145 | int unset __maybe_unused) |
Arnaldo Carvalho de Melo | f078c38 | 2015-02-21 11:36:52 -0800 | [diff] [blame] | 3146 | { |
| 3147 | int ret = -1; |
| 3148 | size_t i; |
| 3149 | struct trace *trace = opt->value; |
| 3150 | /* |
| 3151 | * FIXME: introduce a intarray class, plain parse csv and create a |
| 3152 | * { int nr, int entries[] } struct... |
| 3153 | */ |
| 3154 | struct intlist *list = intlist__new(str); |
| 3155 | |
| 3156 | if (list == NULL) |
| 3157 | return -1; |
| 3158 | |
| 3159 | i = trace->filter_pids.nr = intlist__nr_entries(list) + 1; |
| 3160 | trace->filter_pids.entries = calloc(i, sizeof(pid_t)); |
| 3161 | |
| 3162 | if (trace->filter_pids.entries == NULL) |
| 3163 | goto out; |
| 3164 | |
| 3165 | trace->filter_pids.entries[0] = getpid(); |
| 3166 | |
| 3167 | for (i = 1; i < trace->filter_pids.nr; ++i) |
| 3168 | trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i; |
| 3169 | |
| 3170 | intlist__delete(list); |
| 3171 | ret = 0; |
| 3172 | out: |
| 3173 | return ret; |
| 3174 | } |
| 3175 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 3176 | static int trace__open_output(struct trace *trace, const char *filename) |
| 3177 | { |
| 3178 | struct stat st; |
| 3179 | |
| 3180 | if (!stat(filename, &st) && st.st_size) { |
| 3181 | char oldname[PATH_MAX]; |
| 3182 | |
| 3183 | scnprintf(oldname, sizeof(oldname), "%s.old", filename); |
| 3184 | unlink(oldname); |
| 3185 | rename(filename, oldname); |
| 3186 | } |
| 3187 | |
| 3188 | trace->output = fopen(filename, "w"); |
| 3189 | |
| 3190 | return trace->output == NULL ? -errno : 0; |
| 3191 | } |
| 3192 | |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 3193 | static int parse_pagefaults(const struct option *opt, const char *str, |
| 3194 | int unset __maybe_unused) |
| 3195 | { |
| 3196 | int *trace_pgfaults = opt->value; |
| 3197 | |
| 3198 | if (strcmp(str, "all") == 0) |
| 3199 | *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN; |
| 3200 | else if (strcmp(str, "maj") == 0) |
| 3201 | *trace_pgfaults |= TRACE_PFMAJ; |
| 3202 | else if (strcmp(str, "min") == 0) |
| 3203 | *trace_pgfaults |= TRACE_PFMIN; |
| 3204 | else |
| 3205 | return -1; |
| 3206 | |
| 3207 | return 0; |
| 3208 | } |
| 3209 | |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 3210 | static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler) |
| 3211 | { |
| 3212 | struct perf_evsel *evsel; |
| 3213 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 3214 | evlist__for_each_entry(evlist, evsel) |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 3215 | evsel->handler = handler; |
| 3216 | } |
| 3217 | |
Arnaldo Carvalho de Melo | d32855f | 2018-08-02 15:09:24 -0300 | [diff] [blame] | 3218 | static int evlist__set_syscall_tp_fields(struct perf_evlist *evlist) |
| 3219 | { |
| 3220 | struct perf_evsel *evsel; |
| 3221 | |
| 3222 | evlist__for_each_entry(evlist, evsel) { |
| 3223 | if (evsel->priv || !evsel->tp_format) |
| 3224 | continue; |
| 3225 | |
| 3226 | if (strcmp(evsel->tp_format->system, "syscalls")) |
| 3227 | continue; |
| 3228 | |
| 3229 | if (perf_evsel__init_syscall_tp(evsel)) |
| 3230 | return -1; |
| 3231 | |
| 3232 | if (!strncmp(evsel->tp_format->name, "sys_enter_", 10)) { |
| 3233 | struct syscall_tp *sc = evsel->priv; |
| 3234 | |
| 3235 | if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64))) |
| 3236 | return -1; |
| 3237 | } else if (!strncmp(evsel->tp_format->name, "sys_exit_", 9)) { |
| 3238 | struct syscall_tp *sc = evsel->priv; |
| 3239 | |
| 3240 | if (__tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap)) |
| 3241 | return -1; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | return 0; |
| 3246 | } |
| 3247 | |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3248 | /* |
| 3249 | * XXX: Hackish, just splitting the combined -e+--event (syscalls |
| 3250 | * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use |
| 3251 | * existing facilities unchanged (trace->ev_qualifier + parse_options()). |
| 3252 | * |
| 3253 | * It'd be better to introduce a parse_options() variant that would return a |
| 3254 | * list with the terms it didn't match to an event... |
| 3255 | */ |
| 3256 | static int trace__parse_events_option(const struct option *opt, const char *str, |
| 3257 | int unset __maybe_unused) |
| 3258 | { |
| 3259 | struct trace *trace = (struct trace *)opt->value; |
| 3260 | const char *s = str; |
| 3261 | char *sep = NULL, *lists[2] = { NULL, NULL, }; |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 3262 | int len = strlen(str) + 1, err = -1, list, idx; |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3263 | char *strace_groups_dir = system_path(STRACE_GROUPS_DIR); |
| 3264 | char group_name[PATH_MAX]; |
Arnaldo Carvalho de Melo | f932184 | 2018-10-25 17:24:45 -0300 | [diff] [blame] | 3265 | struct syscall_fmt *fmt; |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3266 | |
| 3267 | if (strace_groups_dir == NULL) |
| 3268 | return -1; |
| 3269 | |
| 3270 | if (*s == '!') { |
| 3271 | ++s; |
| 3272 | trace->not_ev_qualifier = true; |
| 3273 | } |
| 3274 | |
| 3275 | while (1) { |
| 3276 | if ((sep = strchr(s, ',')) != NULL) |
| 3277 | *sep = '\0'; |
| 3278 | |
| 3279 | list = 0; |
Arnaldo Carvalho de Melo | 27702bc | 2017-08-31 11:50:04 -0300 | [diff] [blame] | 3280 | if (syscalltbl__id(trace->sctbl, s) >= 0 || |
| 3281 | syscalltbl__strglobmatch_first(trace->sctbl, s, &idx) >= 0) { |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3282 | list = 1; |
Arnaldo Carvalho de Melo | f932184 | 2018-10-25 17:24:45 -0300 | [diff] [blame] | 3283 | goto do_concat; |
| 3284 | } |
| 3285 | |
| 3286 | fmt = syscall_fmt__find_by_alias(s); |
| 3287 | if (fmt != NULL) { |
| 3288 | list = 1; |
| 3289 | s = fmt->name; |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3290 | } else { |
| 3291 | path__join(group_name, sizeof(group_name), strace_groups_dir, s); |
| 3292 | if (access(group_name, R_OK) == 0) |
| 3293 | list = 1; |
| 3294 | } |
Arnaldo Carvalho de Melo | f932184 | 2018-10-25 17:24:45 -0300 | [diff] [blame] | 3295 | do_concat: |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3296 | if (lists[list]) { |
| 3297 | sprintf(lists[list] + strlen(lists[list]), ",%s", s); |
| 3298 | } else { |
| 3299 | lists[list] = malloc(len); |
| 3300 | if (lists[list] == NULL) |
| 3301 | goto out; |
| 3302 | strcpy(lists[list], s); |
| 3303 | } |
| 3304 | |
| 3305 | if (!sep) |
| 3306 | break; |
| 3307 | |
| 3308 | *sep = ','; |
| 3309 | s = sep + 1; |
| 3310 | } |
| 3311 | |
| 3312 | if (lists[1] != NULL) { |
| 3313 | struct strlist_config slist_config = { |
| 3314 | .dirname = strace_groups_dir, |
| 3315 | }; |
| 3316 | |
| 3317 | trace->ev_qualifier = strlist__new(lists[1], &slist_config); |
| 3318 | if (trace->ev_qualifier == NULL) { |
| 3319 | fputs("Not enough memory to parse event qualifier", trace->output); |
| 3320 | goto out; |
| 3321 | } |
| 3322 | |
| 3323 | if (trace__validate_ev_qualifier(trace)) |
| 3324 | goto out; |
Arnaldo Carvalho de Melo | b912885 | 2018-08-01 16:20:28 -0300 | [diff] [blame] | 3325 | trace->trace_syscalls = true; |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | err = 0; |
| 3329 | |
| 3330 | if (lists[0]) { |
| 3331 | struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event", |
| 3332 | "event selector. use 'perf list' to list available events", |
| 3333 | parse_events_option); |
| 3334 | err = parse_events_option(&o, lists[0], 0); |
| 3335 | } |
| 3336 | out: |
| 3337 | if (sep) |
| 3338 | *sep = ','; |
| 3339 | |
| 3340 | return err; |
| 3341 | } |
| 3342 | |
Arnaldo Carvalho de Melo | 9ea42ba | 2018-03-06 16:30:51 -0300 | [diff] [blame] | 3343 | static int trace__parse_cgroups(const struct option *opt, const char *str, int unset) |
| 3344 | { |
| 3345 | struct trace *trace = opt->value; |
| 3346 | |
| 3347 | if (!list_empty(&trace->evlist->entries)) |
| 3348 | return parse_cgroups(opt, str, unset); |
| 3349 | |
| 3350 | trace->cgroup = evlist__findnew_cgroup(trace->evlist, str); |
| 3351 | |
| 3352 | return 0; |
| 3353 | } |
| 3354 | |
Arnaldo Carvalho de Melo | 744fafc | 2018-11-07 10:04:51 -0300 | [diff] [blame] | 3355 | static struct bpf_map *bpf__find_map_by_name(const char *name) |
| 3356 | { |
| 3357 | struct bpf_object *obj, *tmp; |
| 3358 | |
| 3359 | bpf_object__for_each_safe(obj, tmp) { |
| 3360 | struct bpf_map *map = bpf_object__find_map_by_name(obj, name); |
| 3361 | if (map) |
| 3362 | return map; |
| 3363 | |
| 3364 | } |
| 3365 | |
| 3366 | return NULL; |
| 3367 | } |
| 3368 | |
| 3369 | static void trace__set_bpf_map_filtered_pids(struct trace *trace) |
| 3370 | { |
| 3371 | trace->filter_pids.map = bpf__find_map_by_name("pids_filtered"); |
| 3372 | } |
| 3373 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 3374 | int cmd_trace(int argc, const char **argv) |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3375 | { |
Yunlong Song | 6fdd9cb | 2015-03-18 21:35:57 +0800 | [diff] [blame] | 3376 | const char *trace_usage[] = { |
Namhyung Kim | f15eb53 | 2012-10-05 14:02:16 +0900 | [diff] [blame] | 3377 | "perf trace [<options>] [<command>]", |
| 3378 | "perf trace [<options>] -- <command> [<options>]", |
David Ahern | 5e2485b | 2013-09-28 13:13:01 -0600 | [diff] [blame] | 3379 | "perf trace record [<options>] [<command>]", |
| 3380 | "perf trace record [<options>] -- <command> [<options>]", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3381 | NULL |
| 3382 | }; |
| 3383 | struct trace trace = { |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3384 | .syscalls = { |
| 3385 | . max = -1, |
| 3386 | }, |
| 3387 | .opts = { |
| 3388 | .target = { |
| 3389 | .uid = UINT_MAX, |
| 3390 | .uses_mmap = true, |
| 3391 | }, |
| 3392 | .user_freq = UINT_MAX, |
| 3393 | .user_interval = ULLONG_MAX, |
Arnaldo Carvalho de Melo | 509051e | 2014-01-14 17:52:14 -0300 | [diff] [blame] | 3394 | .no_buffering = true, |
Arnaldo Carvalho de Melo | 38d5447 | 2014-12-12 17:28:32 -0300 | [diff] [blame] | 3395 | .mmap_pages = UINT_MAX, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 3396 | .proc_map_timeout = 500, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3397 | }, |
Milian Wolff | 007d66a | 2015-08-05 16:52:23 -0300 | [diff] [blame] | 3398 | .output = stderr, |
Arnaldo Carvalho de Melo | 50c95cb | 2013-09-12 12:35:21 -0300 | [diff] [blame] | 3399 | .show_comm = true, |
Arnaldo Carvalho de Melo | b912885 | 2018-08-01 16:20:28 -0300 | [diff] [blame] | 3400 | .trace_syscalls = false, |
Arnaldo Carvalho de Melo | 4462181 | 2016-04-11 15:49:11 -0300 | [diff] [blame] | 3401 | .kernel_syscallchains = false, |
Arnaldo Carvalho de Melo | 0561499 | 2016-04-15 16:41:19 -0300 | [diff] [blame] | 3402 | .max_stack = UINT_MAX, |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 3403 | .max_events = ULONG_MAX, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3404 | }; |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 3405 | const char *output_name = NULL; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3406 | const struct option trace_options[] = { |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3407 | OPT_CALLBACK('e', "event", &trace, "event", |
| 3408 | "event/syscall selector. use 'perf list' to list available events", |
| 3409 | trace__parse_events_option), |
Arnaldo Carvalho de Melo | 50c95cb | 2013-09-12 12:35:21 -0300 | [diff] [blame] | 3410 | OPT_BOOLEAN(0, "comm", &trace.show_comm, |
| 3411 | "show the thread COMM next to its id"), |
Arnaldo Carvalho de Melo | c522739 | 2013-09-27 18:06:19 -0300 | [diff] [blame] | 3412 | OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"), |
Arnaldo Carvalho de Melo | 017037f | 2017-01-09 17:26:26 -0300 | [diff] [blame] | 3413 | OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace", |
| 3414 | trace__parse_events_option), |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 3415 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 3416 | OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3417 | OPT_STRING('p', "pid", &trace.opts.target.pid, "pid", |
| 3418 | "trace events on existing process id"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 3419 | OPT_STRING('t', "tid", &trace.opts.target.tid, "tid", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3420 | "trace events on existing thread id"), |
Arnaldo Carvalho de Melo | fa0e4ff | 2015-04-23 11:59:20 -0300 | [diff] [blame] | 3421 | OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids", |
Arnaldo Carvalho de Melo | 6a0b3ab | 2018-11-07 09:38:49 -0300 | [diff] [blame] | 3422 | "pids to filter (by the kernel)", trace__set_filter_pids_from_option), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 3423 | OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3424 | "system-wide collection from all CPUs"), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 3425 | OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3426 | "list of cpus to monitor"), |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 3427 | OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit, |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3428 | "child tasks do not inherit counters"), |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 3429 | OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages", |
| 3430 | "number of mmap data pages", |
| 3431 | perf_evlist__parse_mmap_pages), |
David Ahern | ac9be8e | 2013-08-20 11:15:45 -0600 | [diff] [blame] | 3432 | OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3433 | "user to profile"), |
Arnaldo Carvalho de Melo | ae9ed03 | 2012-10-08 09:56:00 -0300 | [diff] [blame] | 3434 | OPT_CALLBACK(0, "duration", &trace, "float", |
| 3435 | "show only events with duration > N.M ms", |
| 3436 | trace__set_duration), |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 3437 | OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"), |
Arnaldo Carvalho de Melo | 7c304ee0 | 2013-08-22 16:49:54 -0300 | [diff] [blame] | 3438 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
David Ahern | 4bb0919 | 2013-09-04 12:37:43 -0600 | [diff] [blame] | 3439 | OPT_BOOLEAN('T', "time", &trace.full_time, |
| 3440 | "Show full timestamp, not time relative to first start"), |
Arnaldo Carvalho de Melo | 0a6545b | 2018-03-29 12:22:59 -0300 | [diff] [blame] | 3441 | OPT_BOOLEAN(0, "failure", &trace.failure_only, |
| 3442 | "Show only syscalls that failed"), |
David Ahern | fd2eaba | 2013-11-12 09:31:15 -0700 | [diff] [blame] | 3443 | OPT_BOOLEAN('s', "summary", &trace.summary_only, |
| 3444 | "Show only syscall summary with statistics"), |
| 3445 | OPT_BOOLEAN('S', "with-summary", &trace.summary, |
| 3446 | "Show all syscalls and summary with statistics"), |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 3447 | OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min", |
| 3448 | "Trace pagefaults", parse_pagefaults, "maj"), |
Stanislav Fomichev | e281a96 | 2014-06-26 20:14:28 +0400 | [diff] [blame] | 3449 | OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"), |
Yunlong Song | e366a6d | 2015-04-02 21:47:18 +0800 | [diff] [blame] | 3450 | OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"), |
Milian Wolff | 566a088 | 2016-04-08 13:34:15 +0200 | [diff] [blame] | 3451 | OPT_CALLBACK(0, "call-graph", &trace.opts, |
| 3452 | "record_mode[,record_size]", record_callchain_help, |
| 3453 | &record_parse_callchain_opt), |
Arnaldo Carvalho de Melo | 4462181 | 2016-04-11 15:49:11 -0300 | [diff] [blame] | 3454 | OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains, |
| 3455 | "Show the kernel callchains on the syscall exit path"), |
Arnaldo Carvalho de Melo | 5067a8c | 2018-10-18 16:38:27 -0300 | [diff] [blame] | 3456 | OPT_ULONG(0, "max-events", &trace.max_events, |
| 3457 | "Set the maximum number of events to print, exit after that is reached. "), |
Arnaldo Carvalho de Melo | 5cf9c84 | 2016-04-15 11:10:31 -0300 | [diff] [blame] | 3458 | OPT_UINTEGER(0, "min-stack", &trace.min_stack, |
| 3459 | "Set the minimum stack depth when parsing the callchain, " |
| 3460 | "anything below the specified depth will be ignored."), |
Arnaldo Carvalho de Melo | c6d4a49 | 2016-04-14 18:29:08 -0300 | [diff] [blame] | 3461 | OPT_UINTEGER(0, "max-stack", &trace.max_stack, |
| 3462 | "Set the maximum stack depth when parsing the callchain, " |
| 3463 | "anything beyond the specified depth will be ignored. " |
Arnaldo Carvalho de Melo | 4cb9344 | 2016-04-27 10:16:24 -0300 | [diff] [blame] | 3464 | "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), |
Arnaldo Carvalho de Melo | 591421e | 2018-01-22 11:38:54 -0300 | [diff] [blame] | 3465 | OPT_BOOLEAN(0, "print-sample", &trace.print_sample, |
| 3466 | "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"), |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 3467 | OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout, |
| 3468 | "per thread proc mmap processing timeout in ms"), |
Arnaldo Carvalho de Melo | 9ea42ba | 2018-03-06 16:30:51 -0300 | [diff] [blame] | 3469 | OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only", |
| 3470 | trace__parse_cgroups), |
Alexis Berlemont | e36b782 | 2016-10-10 07:43:28 +0200 | [diff] [blame] | 3471 | OPT_UINTEGER('D', "delay", &trace.opts.initial_delay, |
| 3472 | "ms to wait before starting measurement after program " |
| 3473 | "start"), |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3474 | OPT_END() |
| 3475 | }; |
Arnaldo Carvalho de Melo | ccd62a8 | 2016-04-16 09:36:32 -0300 | [diff] [blame] | 3476 | bool __maybe_unused max_stack_user_set = true; |
Arnaldo Carvalho de Melo | f3e459d | 2016-04-15 17:52:34 -0300 | [diff] [blame] | 3477 | bool mmap_pages_user_set = true; |
Arnaldo Carvalho de Melo | 78e890e | 2018-08-07 16:19:05 -0300 | [diff] [blame] | 3478 | struct perf_evsel *evsel; |
Yunlong Song | 6fdd9cb | 2015-03-18 21:35:57 +0800 | [diff] [blame] | 3479 | const char * const trace_subcommands[] = { "record", NULL }; |
Arnaldo Carvalho de Melo | 78e890e | 2018-08-07 16:19:05 -0300 | [diff] [blame] | 3480 | int err = -1; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 3481 | char bf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3482 | |
Arnaldo Carvalho de Melo | 4d08cb8 | 2015-02-24 15:35:55 -0300 | [diff] [blame] | 3483 | signal(SIGSEGV, sighandler_dump_stack); |
| 3484 | signal(SIGFPE, sighandler_dump_stack); |
| 3485 | |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 3486 | trace.evlist = perf_evlist__new(); |
Arnaldo Carvalho de Melo | fd0db10 | 2016-04-04 13:32:20 -0300 | [diff] [blame] | 3487 | trace.sctbl = syscalltbl__new(); |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 3488 | |
Arnaldo Carvalho de Melo | fd0db10 | 2016-04-04 13:32:20 -0300 | [diff] [blame] | 3489 | if (trace.evlist == NULL || trace.sctbl == NULL) { |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 3490 | pr_err("Not enough memory to run!\n"); |
He Kuang | ff8f695 | 2015-05-11 12:28:36 +0000 | [diff] [blame] | 3491 | err = -ENOMEM; |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 3492 | goto out; |
| 3493 | } |
| 3494 | |
Yunlong Song | 6fdd9cb | 2015-03-18 21:35:57 +0800 | [diff] [blame] | 3495 | argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands, |
| 3496 | trace_usage, PARSE_OPT_STOP_AT_NON_OPTION); |
David Ahern | fd2eaba | 2013-11-12 09:31:15 -0700 | [diff] [blame] | 3497 | |
Arnaldo Carvalho de Melo | 9ea42ba | 2018-03-06 16:30:51 -0300 | [diff] [blame] | 3498 | if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) { |
| 3499 | usage_with_options_msg(trace_usage, trace_options, |
| 3500 | "cgroup monitoring only available in system-wide mode"); |
| 3501 | } |
| 3502 | |
Arnaldo Carvalho de Melo | 78e890e | 2018-08-07 16:19:05 -0300 | [diff] [blame] | 3503 | evsel = bpf__setup_output_event(trace.evlist, "__augmented_syscalls__"); |
| 3504 | if (IS_ERR(evsel)) { |
| 3505 | bpf__strerror_setup_output_event(trace.evlist, PTR_ERR(evsel), bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | e0b6d2e | 2018-08-07 15:40:13 -0300 | [diff] [blame] | 3506 | pr_err("ERROR: Setup trace syscalls enter failed: %s\n", bf); |
| 3507 | goto out; |
| 3508 | } |
| 3509 | |
Arnaldo Carvalho de Melo | 744fafc | 2018-11-07 10:04:51 -0300 | [diff] [blame] | 3510 | if (evsel) { |
Arnaldo Carvalho de Melo | d3d1c4bdf5 | 2018-08-07 16:21:44 -0300 | [diff] [blame] | 3511 | trace.syscalls.events.augmented = evsel; |
Arnaldo Carvalho de Melo | 744fafc | 2018-11-07 10:04:51 -0300 | [diff] [blame] | 3512 | trace__set_bpf_map_filtered_pids(&trace); |
| 3513 | } |
Arnaldo Carvalho de Melo | d3d1c4bdf5 | 2018-08-07 16:21:44 -0300 | [diff] [blame] | 3514 | |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 3515 | err = bpf__setup_stdout(trace.evlist); |
| 3516 | if (err) { |
| 3517 | bpf__strerror_setup_stdout(trace.evlist, err, bf, sizeof(bf)); |
| 3518 | pr_err("ERROR: Setup BPF stdout failed: %s\n", bf); |
| 3519 | goto out; |
| 3520 | } |
| 3521 | |
Arnaldo Carvalho de Melo | 59247e3 | 2016-04-12 16:05:02 -0300 | [diff] [blame] | 3522 | err = -1; |
| 3523 | |
Stanislav Fomichev | 598d02c | 2014-06-26 20:14:25 +0400 | [diff] [blame] | 3524 | if (trace.trace_pgfaults) { |
| 3525 | trace.opts.sample_address = true; |
| 3526 | trace.opts.sample_time = true; |
| 3527 | } |
| 3528 | |
Arnaldo Carvalho de Melo | f3e459d | 2016-04-15 17:52:34 -0300 | [diff] [blame] | 3529 | if (trace.opts.mmap_pages == UINT_MAX) |
| 3530 | mmap_pages_user_set = false; |
| 3531 | |
Arnaldo Carvalho de Melo | 0561499 | 2016-04-15 16:41:19 -0300 | [diff] [blame] | 3532 | if (trace.max_stack == UINT_MAX) { |
Arnaldo Carvalho de Melo | 029c75e | 2018-05-17 16:31:32 -0300 | [diff] [blame] | 3533 | trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack(); |
Arnaldo Carvalho de Melo | 0561499 | 2016-04-15 16:41:19 -0300 | [diff] [blame] | 3534 | max_stack_user_set = false; |
| 3535 | } |
| 3536 | |
| 3537 | #ifdef HAVE_DWARF_UNWIND_SUPPORT |
Arnaldo Carvalho de Melo | 75d5011 | 2018-01-15 10:39:55 -0300 | [diff] [blame] | 3538 | if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) { |
Arnaldo Carvalho de Melo | 0561499 | 2016-04-15 16:41:19 -0300 | [diff] [blame] | 3539 | record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false); |
Arnaldo Carvalho de Melo | 75d5011 | 2018-01-15 10:39:55 -0300 | [diff] [blame] | 3540 | } |
Arnaldo Carvalho de Melo | 0561499 | 2016-04-15 16:41:19 -0300 | [diff] [blame] | 3541 | #endif |
| 3542 | |
Arnaldo Carvalho de Melo | 2ddd5c0 | 2016-04-18 12:09:08 -0300 | [diff] [blame] | 3543 | if (callchain_param.enabled) { |
Arnaldo Carvalho de Melo | f3e459d | 2016-04-15 17:52:34 -0300 | [diff] [blame] | 3544 | if (!mmap_pages_user_set && geteuid() == 0) |
| 3545 | trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4; |
| 3546 | |
Milian Wolff | 566a088 | 2016-04-08 13:34:15 +0200 | [diff] [blame] | 3547 | symbol_conf.use_callchain = true; |
Arnaldo Carvalho de Melo | f3e459d | 2016-04-15 17:52:34 -0300 | [diff] [blame] | 3548 | } |
Milian Wolff | 566a088 | 2016-04-08 13:34:15 +0200 | [diff] [blame] | 3549 | |
Arnaldo Carvalho de Melo | d32855f | 2018-08-02 15:09:24 -0300 | [diff] [blame] | 3550 | if (trace.evlist->nr_entries > 0) { |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 3551 | evlist__set_evsel_handler(trace.evlist, trace__event_handler); |
Arnaldo Carvalho de Melo | d32855f | 2018-08-02 15:09:24 -0300 | [diff] [blame] | 3552 | if (evlist__set_syscall_tp_fields(trace.evlist)) { |
| 3553 | perror("failed to set syscalls:* tracepoint fields"); |
| 3554 | goto out; |
| 3555 | } |
| 3556 | } |
Arnaldo Carvalho de Melo | 14a052d | 2015-02-03 12:58:57 -0300 | [diff] [blame] | 3557 | |
Arnaldo Carvalho de Melo | b1a9e25 | 2018-09-03 16:29:39 -0300 | [diff] [blame] | 3558 | /* |
| 3559 | * If we are augmenting syscalls, then combine what we put in the |
| 3560 | * __augmented_syscalls__ BPF map with what is in the |
| 3561 | * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF, |
| 3562 | * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit. |
| 3563 | * |
| 3564 | * We'll switch to look at two BPF maps, one for sys_enter and the |
| 3565 | * other for sys_exit when we start augmenting the sys_exit paths with |
| 3566 | * buffers that are being copied from kernel to userspace, think 'read' |
| 3567 | * syscall. |
| 3568 | */ |
| 3569 | if (trace.syscalls.events.augmented) { |
| 3570 | evsel = trace.syscalls.events.augmented; |
| 3571 | |
| 3572 | if (perf_evsel__init_augmented_syscall_tp(evsel) || |
| 3573 | perf_evsel__init_augmented_syscall_tp_args(evsel)) |
| 3574 | goto out; |
| 3575 | evsel->handler = trace__sys_enter; |
| 3576 | |
| 3577 | evlist__for_each_entry(trace.evlist, evsel) { |
Arnaldo Carvalho de Melo | cd26ea6 | 2018-11-03 08:19:56 -0300 | [diff] [blame] | 3578 | bool raw_syscalls_sys_exit = strcmp(perf_evsel__name(evsel), "raw_syscalls:sys_exit") == 0; |
| 3579 | |
| 3580 | if (raw_syscalls_sys_exit) { |
| 3581 | trace.raw_augmented_syscalls = true; |
| 3582 | goto init_augmented_syscall_tp; |
| 3583 | } |
| 3584 | |
| 3585 | if (strstarts(perf_evsel__name(evsel), "syscalls:sys_exit_")) { |
| 3586 | init_augmented_syscall_tp: |
Arnaldo Carvalho de Melo | b1a9e25 | 2018-09-03 16:29:39 -0300 | [diff] [blame] | 3587 | perf_evsel__init_augmented_syscall_tp(evsel); |
| 3588 | perf_evsel__init_augmented_syscall_tp_ret(evsel); |
| 3589 | evsel->handler = trace__sys_exit; |
| 3590 | } |
| 3591 | } |
| 3592 | } |
| 3593 | |
Stanislav Fomichev | 1e28fe0 | 2014-06-26 20:14:26 +0400 | [diff] [blame] | 3594 | if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) |
| 3595 | return trace__record(&trace, argc-1, &argv[1]); |
| 3596 | |
| 3597 | /* summary_only implies summary option, but don't overwrite summary if set */ |
| 3598 | if (trace.summary_only) |
| 3599 | trace.summary = trace.summary_only; |
| 3600 | |
Arnaldo Carvalho de Melo | 726f323 | 2015-02-06 10:16:45 +0100 | [diff] [blame] | 3601 | if (!trace.trace_syscalls && !trace.trace_pgfaults && |
| 3602 | trace.evlist->nr_entries == 0 /* Was --events used? */) { |
Arnaldo Carvalho de Melo | b912885 | 2018-08-01 16:20:28 -0300 | [diff] [blame] | 3603 | trace.trace_syscalls = true; |
Arnaldo Carvalho de Melo | 59247e3 | 2016-04-12 16:05:02 -0300 | [diff] [blame] | 3604 | } |
| 3605 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 3606 | if (output_name != NULL) { |
| 3607 | err = trace__open_output(&trace, output_name); |
| 3608 | if (err < 0) { |
| 3609 | perror("failed to create output file"); |
| 3610 | goto out; |
| 3611 | } |
| 3612 | } |
| 3613 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 3614 | err = target__validate(&trace.opts.target); |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 3615 | if (err) { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 3616 | target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 3617 | fprintf(trace.output, "%s", bf); |
| 3618 | goto out_close; |
Namhyung Kim | 32caf0d | 2012-10-05 14:02:13 +0900 | [diff] [blame] | 3619 | } |
| 3620 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 3621 | err = target__parse_uid(&trace.opts.target); |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3622 | if (err) { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 3623 | target__strerror(&trace.opts.target, err, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 3624 | fprintf(trace.output, "%s", bf); |
| 3625 | goto out_close; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3626 | } |
| 3627 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 3628 | if (!argc && target__none(&trace.opts.target)) |
Namhyung Kim | ee76120 | 2012-10-05 14:02:14 +0900 | [diff] [blame] | 3629 | trace.opts.target.system_wide = true; |
| 3630 | |
David Ahern | 6810fc9 | 2013-08-28 22:29:52 -0600 | [diff] [blame] | 3631 | if (input_name) |
| 3632 | err = trace__replay(&trace); |
| 3633 | else |
| 3634 | err = trace__run(&trace, argc, argv); |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 3635 | |
Arnaldo Carvalho de Melo | c24ff99 | 2013-08-19 12:01:10 -0300 | [diff] [blame] | 3636 | out_close: |
| 3637 | if (output_name != NULL) |
| 3638 | fclose(trace.output); |
| 3639 | out: |
Arnaldo Carvalho de Melo | 1302d88 | 2012-10-17 17:13:12 -0300 | [diff] [blame] | 3640 | return err; |
Arnaldo Carvalho de Melo | 514f1c6 | 2012-09-26 20:05:56 -0300 | [diff] [blame] | 3641 | } |