Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 2 | /* |
| 3 | * auxtrace.c: AUX area trace support |
| 4 | * Copyright (c) 2013-2015, Intel Corporation. |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 5 | */ |
| 6 | |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 7 | #include <inttypes.h> |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/mman.h> |
| 10 | #include <stdbool.h> |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <limits.h> |
| 13 | #include <errno.h> |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/perf_event.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/bitops.h> |
| 19 | #include <linux/log2.h> |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 20 | #include <linux/string.h> |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 21 | #include <linux/time64.h> |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 22 | |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 23 | #include <sys/param.h> |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 24 | #include <stdlib.h> |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 25 | #include <stdio.h> |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 26 | #include <linux/list.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 27 | #include <linux/zalloc.h> |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 28 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 29 | #include "evlist.h" |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 30 | #include "dso.h" |
| 31 | #include "map.h" |
| 32 | #include "pmu.h" |
| 33 | #include "evsel.h" |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 34 | #include "evsel_config.h" |
Arnaldo Carvalho de Melo | 7cadca8 | 2019-01-27 14:10:31 +0100 | [diff] [blame] | 35 | #include "symbol.h" |
Arnaldo Carvalho de Melo | 40c7d24 | 2020-05-05 11:49:08 -0300 | [diff] [blame] | 36 | #include "util/perf_api_probe.h" |
Arnaldo Carvalho de Melo | ea49e01 | 2019-09-18 11:36:13 -0300 | [diff] [blame] | 37 | #include "util/synthetic-events.h" |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 38 | #include "thread_map.h" |
| 39 | #include "asm/bug.h" |
| 40 | #include "auxtrace.h" |
| 41 | |
Adrian Hunter | c3278f0 | 2015-04-09 18:53:54 +0300 | [diff] [blame] | 42 | #include <linux/hash.h> |
| 43 | |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 44 | #include "event.h" |
Arnaldo Carvalho de Melo | aeb00b1 | 2019-08-22 15:40:29 -0300 | [diff] [blame] | 45 | #include "record.h" |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 46 | #include "session.h" |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 47 | #include "debug.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 48 | #include <subcmd/parse-options.h> |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 49 | |
Mathieu Poirier | 440a23b | 2018-01-17 10:52:11 -0700 | [diff] [blame] | 50 | #include "cs-etm.h" |
Adrian Hunter | 5efb1d5 | 2015-07-17 19:33:42 +0300 | [diff] [blame] | 51 | #include "intel-pt.h" |
Adrian Hunter | d0170af | 2015-07-17 19:33:43 +0300 | [diff] [blame] | 52 | #include "intel-bts.h" |
Kim Phillips | ffd3d18 | 2018-01-14 13:28:50 -0600 | [diff] [blame] | 53 | #include "arm-spe.h" |
Thomas Richter | b96e661 | 2018-08-02 09:46:20 +0200 | [diff] [blame] | 54 | #include "s390-cpumsf.h" |
Arnaldo Carvalho de Melo | e0fcfb0 | 2019-09-23 12:20:38 -0300 | [diff] [blame] | 55 | #include "util/mmap.h" |
Adrian Hunter | 5efb1d5 | 2015-07-17 19:33:42 +0300 | [diff] [blame] | 56 | |
Arnaldo Carvalho de Melo | 3052ba5 | 2019-06-25 17:27:31 -0300 | [diff] [blame] | 57 | #include <linux/ctype.h> |
Arnaldo Carvalho de Melo | fb71c86c | 2019-09-03 10:56:06 -0300 | [diff] [blame] | 58 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 59 | #include "symbol/kallsyms.h" |
Arnaldo Carvalho de Melo | fb71c86c | 2019-09-03 10:56:06 -0300 | [diff] [blame] | 60 | #include <internal/lib.h> |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 61 | |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 62 | /* |
| 63 | * Make a group from 'leader' to 'last', requiring that the events were not |
| 64 | * already grouped to a different leader. |
| 65 | */ |
| 66 | static int perf_evlist__regroup(struct evlist *evlist, |
| 67 | struct evsel *leader, |
| 68 | struct evsel *last) |
| 69 | { |
| 70 | struct evsel *evsel; |
| 71 | bool grp; |
| 72 | |
| 73 | if (!perf_evsel__is_group_leader(leader)) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | grp = false; |
| 77 | evlist__for_each_entry(evlist, evsel) { |
| 78 | if (grp) { |
| 79 | if (!(evsel->leader == leader || |
| 80 | (evsel->leader == evsel && |
| 81 | evsel->core.nr_members <= 1))) |
| 82 | return -EINVAL; |
| 83 | } else if (evsel == leader) { |
| 84 | grp = true; |
| 85 | } |
| 86 | if (evsel == last) |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | grp = false; |
| 91 | evlist__for_each_entry(evlist, evsel) { |
| 92 | if (grp) { |
| 93 | if (evsel->leader != leader) { |
| 94 | evsel->leader = leader; |
| 95 | if (leader->core.nr_members < 1) |
| 96 | leader->core.nr_members = 1; |
| 97 | leader->core.nr_members += 1; |
| 98 | } |
| 99 | } else if (evsel == leader) { |
| 100 | grp = true; |
| 101 | } |
| 102 | if (evsel == last) |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Adrian Hunter | 2e2967f | 2018-03-06 11:13:13 +0200 | [diff] [blame] | 109 | static bool auxtrace__dont_decode(struct perf_session *session) |
| 110 | { |
| 111 | return !session->itrace_synth_opts || |
| 112 | session->itrace_synth_opts->dont_decode; |
| 113 | } |
| 114 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 115 | int auxtrace_mmap__mmap(struct auxtrace_mmap *mm, |
| 116 | struct auxtrace_mmap_params *mp, |
| 117 | void *userpg, int fd) |
| 118 | { |
| 119 | struct perf_event_mmap_page *pc = userpg; |
| 120 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 121 | WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n"); |
| 122 | |
| 123 | mm->userpg = userpg; |
| 124 | mm->mask = mp->mask; |
| 125 | mm->len = mp->len; |
| 126 | mm->prev = 0; |
| 127 | mm->idx = mp->idx; |
| 128 | mm->tid = mp->tid; |
| 129 | mm->cpu = mp->cpu; |
| 130 | |
| 131 | if (!mp->len) { |
| 132 | mm->base = NULL; |
| 133 | return 0; |
| 134 | } |
| 135 | |
Adrian Hunter | a7fde09 | 2015-07-14 15:32:41 +0300 | [diff] [blame] | 136 | #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT) |
| 137 | pr_err("Cannot use AUX area tracing mmaps\n"); |
| 138 | return -1; |
| 139 | #endif |
| 140 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 141 | pc->aux_offset = mp->offset; |
| 142 | pc->aux_size = mp->len; |
| 143 | |
| 144 | mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset); |
| 145 | if (mm->base == MAP_FAILED) { |
| 146 | pr_debug2("failed to mmap AUX area\n"); |
| 147 | mm->base = NULL; |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | void auxtrace_mmap__munmap(struct auxtrace_mmap *mm) |
| 155 | { |
| 156 | if (mm->base) { |
| 157 | munmap(mm->base, mm->len); |
| 158 | mm->base = NULL; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp, |
| 163 | off_t auxtrace_offset, |
| 164 | unsigned int auxtrace_pages, |
| 165 | bool auxtrace_overwrite) |
| 166 | { |
| 167 | if (auxtrace_pages) { |
| 168 | mp->offset = auxtrace_offset; |
| 169 | mp->len = auxtrace_pages * (size_t)page_size; |
| 170 | mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0; |
| 171 | mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE); |
| 172 | pr_debug2("AUX area mmap length %zu\n", mp->len); |
| 173 | } else { |
| 174 | mp->len = 0; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 179 | struct evlist *evlist, int idx, |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 180 | bool per_cpu) |
| 181 | { |
| 182 | mp->idx = idx; |
| 183 | |
| 184 | if (per_cpu) { |
Jiri Olsa | f72f901 | 2019-07-21 13:24:41 +0200 | [diff] [blame] | 185 | mp->cpu = evlist->core.cpus->map[idx]; |
Jiri Olsa | 03617c2 | 2019-07-21 13:24:42 +0200 | [diff] [blame] | 186 | if (evlist->core.threads) |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 187 | mp->tid = perf_thread_map__pid(evlist->core.threads, 0); |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 188 | else |
| 189 | mp->tid = -1; |
| 190 | } else { |
| 191 | mp->cpu = -1; |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 192 | mp->tid = perf_thread_map__pid(evlist->core.threads, idx); |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 193 | } |
| 194 | } |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 195 | |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 196 | #define AUXTRACE_INIT_NR_QUEUES 32 |
| 197 | |
| 198 | static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues) |
| 199 | { |
| 200 | struct auxtrace_queue *queue_array; |
| 201 | unsigned int max_nr_queues, i; |
| 202 | |
| 203 | max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue); |
| 204 | if (nr_queues > max_nr_queues) |
| 205 | return NULL; |
| 206 | |
| 207 | queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue)); |
| 208 | if (!queue_array) |
| 209 | return NULL; |
| 210 | |
| 211 | for (i = 0; i < nr_queues; i++) { |
| 212 | INIT_LIST_HEAD(&queue_array[i].head); |
| 213 | queue_array[i].priv = NULL; |
| 214 | } |
| 215 | |
| 216 | return queue_array; |
| 217 | } |
| 218 | |
| 219 | int auxtrace_queues__init(struct auxtrace_queues *queues) |
| 220 | { |
| 221 | queues->nr_queues = AUXTRACE_INIT_NR_QUEUES; |
| 222 | queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues); |
| 223 | if (!queues->queue_array) |
| 224 | return -ENOMEM; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int auxtrace_queues__grow(struct auxtrace_queues *queues, |
| 229 | unsigned int new_nr_queues) |
| 230 | { |
| 231 | unsigned int nr_queues = queues->nr_queues; |
| 232 | struct auxtrace_queue *queue_array; |
| 233 | unsigned int i; |
| 234 | |
| 235 | if (!nr_queues) |
| 236 | nr_queues = AUXTRACE_INIT_NR_QUEUES; |
| 237 | |
| 238 | while (nr_queues && nr_queues < new_nr_queues) |
| 239 | nr_queues <<= 1; |
| 240 | |
| 241 | if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues) |
| 242 | return -EINVAL; |
| 243 | |
| 244 | queue_array = auxtrace_alloc_queue_array(nr_queues); |
| 245 | if (!queue_array) |
| 246 | return -ENOMEM; |
| 247 | |
| 248 | for (i = 0; i < queues->nr_queues; i++) { |
| 249 | list_splice_tail(&queues->queue_array[i].head, |
| 250 | &queue_array[i].head); |
Adrian Hunter | 99cbbe5 | 2018-08-14 11:46:08 +0300 | [diff] [blame] | 251 | queue_array[i].tid = queues->queue_array[i].tid; |
| 252 | queue_array[i].cpu = queues->queue_array[i].cpu; |
| 253 | queue_array[i].set = queues->queue_array[i].set; |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 254 | queue_array[i].priv = queues->queue_array[i].priv; |
| 255 | } |
| 256 | |
| 257 | queues->nr_queues = nr_queues; |
| 258 | queues->queue_array = queue_array; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static void *auxtrace_copy_data(u64 size, struct perf_session *session) |
| 264 | { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 265 | int fd = perf_data__fd(session->data); |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 266 | void *p; |
| 267 | ssize_t ret; |
| 268 | |
| 269 | if (size > SSIZE_MAX) |
| 270 | return NULL; |
| 271 | |
| 272 | p = malloc(size); |
| 273 | if (!p) |
| 274 | return NULL; |
| 275 | |
| 276 | ret = readn(fd, p, size); |
| 277 | if (ret != (ssize_t)size) { |
| 278 | free(p); |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | return p; |
| 283 | } |
| 284 | |
Adrian Hunter | a356a59 | 2018-03-06 11:13:15 +0200 | [diff] [blame] | 285 | static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues, |
| 286 | unsigned int idx, |
| 287 | struct auxtrace_buffer *buffer) |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 288 | { |
| 289 | struct auxtrace_queue *queue; |
| 290 | int err; |
| 291 | |
| 292 | if (idx >= queues->nr_queues) { |
| 293 | err = auxtrace_queues__grow(queues, idx + 1); |
| 294 | if (err) |
| 295 | return err; |
| 296 | } |
| 297 | |
| 298 | queue = &queues->queue_array[idx]; |
| 299 | |
| 300 | if (!queue->set) { |
| 301 | queue->set = true; |
| 302 | queue->tid = buffer->tid; |
| 303 | queue->cpu = buffer->cpu; |
| 304 | } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) { |
| 305 | pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n", |
| 306 | queue->cpu, queue->tid, buffer->cpu, buffer->tid); |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | |
| 310 | buffer->buffer_nr = queues->next_buffer_nr++; |
| 311 | |
| 312 | list_add_tail(&buffer->list, &queue->head); |
| 313 | |
| 314 | queues->new_data = true; |
| 315 | queues->populated = true; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* Limit buffers to 32MiB on 32-bit */ |
| 321 | #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024) |
| 322 | |
| 323 | static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues, |
| 324 | unsigned int idx, |
| 325 | struct auxtrace_buffer *buffer) |
| 326 | { |
| 327 | u64 sz = buffer->size; |
| 328 | bool consecutive = false; |
| 329 | struct auxtrace_buffer *b; |
| 330 | int err; |
| 331 | |
| 332 | while (sz > BUFFER_LIMIT_FOR_32_BIT) { |
| 333 | b = memdup(buffer, sizeof(struct auxtrace_buffer)); |
| 334 | if (!b) |
| 335 | return -ENOMEM; |
| 336 | b->size = BUFFER_LIMIT_FOR_32_BIT; |
| 337 | b->consecutive = consecutive; |
Adrian Hunter | a356a59 | 2018-03-06 11:13:15 +0200 | [diff] [blame] | 338 | err = auxtrace_queues__queue_buffer(queues, idx, b); |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 339 | if (err) { |
| 340 | auxtrace_buffer__free(b); |
| 341 | return err; |
| 342 | } |
| 343 | buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT; |
| 344 | sz -= BUFFER_LIMIT_FOR_32_BIT; |
| 345 | consecutive = true; |
| 346 | } |
| 347 | |
| 348 | buffer->size = sz; |
| 349 | buffer->consecutive = consecutive; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Adrian Hunter | b238db6 | 2018-03-06 11:13:18 +0200 | [diff] [blame] | 354 | static bool filter_cpu(struct perf_session *session, int cpu) |
| 355 | { |
| 356 | unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap; |
| 357 | |
| 358 | return cpu_bitmap && cpu != -1 && !test_bit(cpu, cpu_bitmap); |
| 359 | } |
| 360 | |
Adrian Hunter | a356a59 | 2018-03-06 11:13:15 +0200 | [diff] [blame] | 361 | static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues, |
| 362 | struct perf_session *session, |
| 363 | unsigned int idx, |
Adrian Hunter | 4c45484 | 2018-03-06 11:13:16 +0200 | [diff] [blame] | 364 | struct auxtrace_buffer *buffer, |
| 365 | struct auxtrace_buffer **buffer_ptr) |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 366 | { |
Adrian Hunter | 0d75f12 | 2018-03-06 11:13:17 +0200 | [diff] [blame] | 367 | int err = -ENOMEM; |
| 368 | |
Adrian Hunter | b238db6 | 2018-03-06 11:13:18 +0200 | [diff] [blame] | 369 | if (filter_cpu(session, buffer->cpu)) |
| 370 | return 0; |
| 371 | |
Adrian Hunter | 0d75f12 | 2018-03-06 11:13:17 +0200 | [diff] [blame] | 372 | buffer = memdup(buffer, sizeof(*buffer)); |
| 373 | if (!buffer) |
| 374 | return -ENOMEM; |
Adrian Hunter | 4c45484 | 2018-03-06 11:13:16 +0200 | [diff] [blame] | 375 | |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 376 | if (session->one_mmap) { |
| 377 | buffer->data = buffer->data_offset - session->one_mmap_offset + |
| 378 | session->one_mmap_addr; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 379 | } else if (perf_data__is_pipe(session->data)) { |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 380 | buffer->data = auxtrace_copy_data(buffer->size, session); |
| 381 | if (!buffer->data) |
Adrian Hunter | 0d75f12 | 2018-03-06 11:13:17 +0200 | [diff] [blame] | 382 | goto out_free; |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 383 | buffer->data_needs_freeing = true; |
| 384 | } else if (BITS_PER_LONG == 32 && |
| 385 | buffer->size > BUFFER_LIMIT_FOR_32_BIT) { |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 386 | err = auxtrace_queues__split_buffer(queues, idx, buffer); |
| 387 | if (err) |
Adrian Hunter | 0d75f12 | 2018-03-06 11:13:17 +0200 | [diff] [blame] | 388 | goto out_free; |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 389 | } |
| 390 | |
Adrian Hunter | 4c45484 | 2018-03-06 11:13:16 +0200 | [diff] [blame] | 391 | err = auxtrace_queues__queue_buffer(queues, idx, buffer); |
| 392 | if (err) |
Adrian Hunter | 0d75f12 | 2018-03-06 11:13:17 +0200 | [diff] [blame] | 393 | goto out_free; |
Adrian Hunter | 4c45484 | 2018-03-06 11:13:16 +0200 | [diff] [blame] | 394 | |
| 395 | /* FIXME: Doesn't work for split buffer */ |
| 396 | if (buffer_ptr) |
| 397 | *buffer_ptr = buffer; |
| 398 | |
| 399 | return 0; |
Adrian Hunter | 0d75f12 | 2018-03-06 11:13:17 +0200 | [diff] [blame] | 400 | |
| 401 | out_free: |
| 402 | auxtrace_buffer__free(buffer); |
| 403 | return err; |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | int auxtrace_queues__add_event(struct auxtrace_queues *queues, |
| 407 | struct perf_session *session, |
| 408 | union perf_event *event, off_t data_offset, |
| 409 | struct auxtrace_buffer **buffer_ptr) |
| 410 | { |
Adrian Hunter | 0d75f12 | 2018-03-06 11:13:17 +0200 | [diff] [blame] | 411 | struct auxtrace_buffer buffer = { |
| 412 | .pid = -1, |
| 413 | .tid = event->auxtrace.tid, |
| 414 | .cpu = event->auxtrace.cpu, |
| 415 | .data_offset = data_offset, |
| 416 | .offset = event->auxtrace.offset, |
| 417 | .reference = event->auxtrace.reference, |
| 418 | .size = event->auxtrace.size, |
| 419 | }; |
| 420 | unsigned int idx = event->auxtrace.idx; |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 421 | |
Adrian Hunter | 0d75f12 | 2018-03-06 11:13:17 +0200 | [diff] [blame] | 422 | return auxtrace_queues__add_buffer(queues, session, idx, &buffer, |
| 423 | buffer_ptr); |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 424 | } |
| 425 | |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 426 | static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues, |
| 427 | struct perf_session *session, |
| 428 | off_t file_offset, size_t sz) |
| 429 | { |
| 430 | union perf_event *event; |
| 431 | int err; |
| 432 | char buf[PERF_SAMPLE_MAX_SIZE]; |
| 433 | |
| 434 | err = perf_session__peek_event(session, file_offset, buf, |
| 435 | PERF_SAMPLE_MAX_SIZE, &event, NULL); |
| 436 | if (err) |
| 437 | return err; |
| 438 | |
| 439 | if (event->header.type == PERF_RECORD_AUXTRACE) { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 440 | if (event->header.size < sizeof(struct perf_record_auxtrace) || |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 441 | event->header.size != sz) { |
| 442 | err = -EINVAL; |
| 443 | goto out; |
| 444 | } |
| 445 | file_offset += event->header.size; |
| 446 | err = auxtrace_queues__add_event(queues, session, event, |
| 447 | file_offset, NULL); |
| 448 | } |
| 449 | out: |
| 450 | return err; |
| 451 | } |
| 452 | |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 453 | void auxtrace_queues__free(struct auxtrace_queues *queues) |
| 454 | { |
| 455 | unsigned int i; |
| 456 | |
| 457 | for (i = 0; i < queues->nr_queues; i++) { |
| 458 | while (!list_empty(&queues->queue_array[i].head)) { |
| 459 | struct auxtrace_buffer *buffer; |
| 460 | |
| 461 | buffer = list_entry(queues->queue_array[i].head.next, |
| 462 | struct auxtrace_buffer, list); |
Arnaldo Carvalho de Melo | e56fbc9 | 2019-07-04 12:13:46 -0300 | [diff] [blame] | 463 | list_del_init(&buffer->list); |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 464 | auxtrace_buffer__free(buffer); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | zfree(&queues->queue_array); |
| 469 | queues->nr_queues = 0; |
| 470 | } |
| 471 | |
Adrian Hunter | f939715 | 2015-04-09 18:53:52 +0300 | [diff] [blame] | 472 | static void auxtrace_heapify(struct auxtrace_heap_item *heap_array, |
| 473 | unsigned int pos, unsigned int queue_nr, |
| 474 | u64 ordinal) |
| 475 | { |
| 476 | unsigned int parent; |
| 477 | |
| 478 | while (pos) { |
| 479 | parent = (pos - 1) >> 1; |
| 480 | if (heap_array[parent].ordinal <= ordinal) |
| 481 | break; |
| 482 | heap_array[pos] = heap_array[parent]; |
| 483 | pos = parent; |
| 484 | } |
| 485 | heap_array[pos].queue_nr = queue_nr; |
| 486 | heap_array[pos].ordinal = ordinal; |
| 487 | } |
| 488 | |
| 489 | int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr, |
| 490 | u64 ordinal) |
| 491 | { |
| 492 | struct auxtrace_heap_item *heap_array; |
| 493 | |
| 494 | if (queue_nr >= heap->heap_sz) { |
| 495 | unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES; |
| 496 | |
| 497 | while (heap_sz <= queue_nr) |
| 498 | heap_sz <<= 1; |
| 499 | heap_array = realloc(heap->heap_array, |
| 500 | heap_sz * sizeof(struct auxtrace_heap_item)); |
| 501 | if (!heap_array) |
| 502 | return -ENOMEM; |
| 503 | heap->heap_array = heap_array; |
| 504 | heap->heap_sz = heap_sz; |
| 505 | } |
| 506 | |
| 507 | auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal); |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | void auxtrace_heap__free(struct auxtrace_heap *heap) |
| 513 | { |
| 514 | zfree(&heap->heap_array); |
| 515 | heap->heap_cnt = 0; |
| 516 | heap->heap_sz = 0; |
| 517 | } |
| 518 | |
| 519 | void auxtrace_heap__pop(struct auxtrace_heap *heap) |
| 520 | { |
| 521 | unsigned int pos, last, heap_cnt = heap->heap_cnt; |
| 522 | struct auxtrace_heap_item *heap_array; |
| 523 | |
| 524 | if (!heap_cnt) |
| 525 | return; |
| 526 | |
| 527 | heap->heap_cnt -= 1; |
| 528 | |
| 529 | heap_array = heap->heap_array; |
| 530 | |
| 531 | pos = 0; |
| 532 | while (1) { |
| 533 | unsigned int left, right; |
| 534 | |
| 535 | left = (pos << 1) + 1; |
| 536 | if (left >= heap_cnt) |
| 537 | break; |
| 538 | right = left + 1; |
| 539 | if (right >= heap_cnt) { |
| 540 | heap_array[pos] = heap_array[left]; |
| 541 | return; |
| 542 | } |
| 543 | if (heap_array[left].ordinal < heap_array[right].ordinal) { |
| 544 | heap_array[pos] = heap_array[left]; |
| 545 | pos = left; |
| 546 | } else { |
| 547 | heap_array[pos] = heap_array[right]; |
| 548 | pos = right; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | last = heap_cnt - 1; |
| 553 | auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr, |
| 554 | heap_array[last].ordinal); |
| 555 | } |
| 556 | |
Mathieu Poirier | 14a05e1 | 2016-01-14 14:46:15 -0700 | [diff] [blame] | 557 | size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 558 | struct evlist *evlist) |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 559 | { |
| 560 | if (itr) |
Mathieu Poirier | 14a05e1 | 2016-01-14 14:46:15 -0700 | [diff] [blame] | 561 | return itr->info_priv_size(itr, evlist); |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static int auxtrace_not_supported(void) |
| 566 | { |
| 567 | pr_err("AUX area tracing is not supported on this architecture\n"); |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | |
| 571 | int auxtrace_record__info_fill(struct auxtrace_record *itr, |
| 572 | struct perf_session *session, |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 573 | struct perf_record_auxtrace_info *auxtrace_info, |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 574 | size_t priv_size) |
| 575 | { |
| 576 | if (itr) |
| 577 | return itr->info_fill(itr, session, auxtrace_info, priv_size); |
| 578 | return auxtrace_not_supported(); |
| 579 | } |
| 580 | |
| 581 | void auxtrace_record__free(struct auxtrace_record *itr) |
| 582 | { |
| 583 | if (itr) |
| 584 | itr->free(itr); |
| 585 | } |
| 586 | |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 587 | int auxtrace_record__snapshot_start(struct auxtrace_record *itr) |
| 588 | { |
| 589 | if (itr && itr->snapshot_start) |
| 590 | return itr->snapshot_start(itr); |
| 591 | return 0; |
| 592 | } |
| 593 | |
Alexander Shishkin | ce7b0e4 | 2019-08-06 17:41:01 +0300 | [diff] [blame] | 594 | int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit) |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 595 | { |
Alexander Shishkin | ce7b0e4 | 2019-08-06 17:41:01 +0300 | [diff] [blame] | 596 | if (!on_exit && itr && itr->snapshot_finish) |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 597 | return itr->snapshot_finish(itr); |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx, |
| 602 | struct auxtrace_mmap *mm, |
| 603 | unsigned char *data, u64 *head, u64 *old) |
| 604 | { |
| 605 | if (itr && itr->find_snapshot) |
| 606 | return itr->find_snapshot(itr, idx, mm, data, head, old); |
| 607 | return 0; |
| 608 | } |
| 609 | |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 610 | int auxtrace_record__options(struct auxtrace_record *itr, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 611 | struct evlist *evlist, |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 612 | struct record_opts *opts) |
| 613 | { |
Adrian Hunter | ad60ba0 | 2020-02-17 10:23:00 +0200 | [diff] [blame] | 614 | if (itr) { |
| 615 | itr->evlist = evlist; |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 616 | return itr->recording_options(itr, evlist, opts); |
Adrian Hunter | ad60ba0 | 2020-02-17 10:23:00 +0200 | [diff] [blame] | 617 | } |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | u64 auxtrace_record__reference(struct auxtrace_record *itr) |
| 622 | { |
| 623 | if (itr) |
| 624 | return itr->reference(itr); |
| 625 | return 0; |
| 626 | } |
| 627 | |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 628 | int auxtrace_parse_snapshot_options(struct auxtrace_record *itr, |
| 629 | struct record_opts *opts, const char *str) |
| 630 | { |
| 631 | if (!str) |
| 632 | return 0; |
| 633 | |
Alexander Shishkin | ce7b0e4 | 2019-08-06 17:41:01 +0300 | [diff] [blame] | 634 | /* PMU-agnostic options */ |
| 635 | switch (*str) { |
| 636 | case 'e': |
| 637 | opts->auxtrace_snapshot_on_exit = true; |
| 638 | str++; |
| 639 | break; |
| 640 | default: |
| 641 | break; |
| 642 | } |
| 643 | |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 644 | if (itr) |
| 645 | return itr->parse_snapshot_options(itr, opts, str); |
| 646 | |
| 647 | pr_err("No AUX area tracing to snapshot\n"); |
| 648 | return -EINVAL; |
| 649 | } |
| 650 | |
Adrian Hunter | ad60ba0 | 2020-02-17 10:23:00 +0200 | [diff] [blame] | 651 | int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx) |
| 652 | { |
| 653 | struct evsel *evsel; |
| 654 | |
| 655 | if (!itr->evlist || !itr->pmu) |
| 656 | return -EINVAL; |
| 657 | |
| 658 | evlist__for_each_entry(itr->evlist, evsel) { |
| 659 | if (evsel->core.attr.type == itr->pmu->type) { |
| 660 | if (evsel->disabled) |
| 661 | return 0; |
| 662 | return perf_evlist__enable_event_idx(itr->evlist, evsel, |
| 663 | idx); |
| 664 | } |
| 665 | } |
| 666 | return -EINVAL; |
| 667 | } |
| 668 | |
Adrian Hunter | f0bb7ee | 2019-11-15 14:42:15 +0200 | [diff] [blame] | 669 | /* |
| 670 | * Event record size is 16-bit which results in a maximum size of about 64KiB. |
| 671 | * Allow about 4KiB for the rest of the sample record, to give a maximum |
| 672 | * AUX area sample size of 60KiB. |
| 673 | */ |
| 674 | #define MAX_AUX_SAMPLE_SIZE (60 * 1024) |
| 675 | |
| 676 | /* Arbitrary default size if no other default provided */ |
| 677 | #define DEFAULT_AUX_SAMPLE_SIZE (4 * 1024) |
| 678 | |
| 679 | static int auxtrace_validate_aux_sample_size(struct evlist *evlist, |
| 680 | struct record_opts *opts) |
| 681 | { |
| 682 | struct evsel *evsel; |
| 683 | bool has_aux_leader = false; |
| 684 | u32 sz; |
| 685 | |
| 686 | evlist__for_each_entry(evlist, evsel) { |
| 687 | sz = evsel->core.attr.aux_sample_size; |
| 688 | if (perf_evsel__is_group_leader(evsel)) { |
Arnaldo Carvalho de Melo | 39453ed | 2020-04-29 15:51:38 -0300 | [diff] [blame] | 689 | has_aux_leader = evsel__is_aux_event(evsel); |
Adrian Hunter | f0bb7ee | 2019-11-15 14:42:15 +0200 | [diff] [blame] | 690 | if (sz) { |
| 691 | if (has_aux_leader) |
| 692 | pr_err("Cannot add AUX area sampling to an AUX area event\n"); |
| 693 | else |
| 694 | pr_err("Cannot add AUX area sampling to a group leader\n"); |
| 695 | return -EINVAL; |
| 696 | } |
| 697 | } |
| 698 | if (sz > MAX_AUX_SAMPLE_SIZE) { |
| 699 | pr_err("AUX area sample size %u too big, max. %d\n", |
| 700 | sz, MAX_AUX_SAMPLE_SIZE); |
| 701 | return -EINVAL; |
| 702 | } |
| 703 | if (sz) { |
| 704 | if (!has_aux_leader) { |
| 705 | pr_err("Cannot add AUX area sampling because group leader is not an AUX area event\n"); |
| 706 | return -EINVAL; |
| 707 | } |
Arnaldo Carvalho de Melo | 862b2f8 | 2020-04-29 16:12:15 -0300 | [diff] [blame^] | 708 | evsel__set_sample_bit(evsel, AUX); |
Adrian Hunter | f0bb7ee | 2019-11-15 14:42:15 +0200 | [diff] [blame] | 709 | opts->auxtrace_sample_mode = true; |
| 710 | } else { |
Arnaldo Carvalho de Melo | 862b2f8 | 2020-04-29 16:12:15 -0300 | [diff] [blame^] | 711 | evsel__reset_sample_bit(evsel, AUX); |
Adrian Hunter | f0bb7ee | 2019-11-15 14:42:15 +0200 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | if (!opts->auxtrace_sample_mode) { |
| 716 | pr_err("AUX area sampling requires an AUX area event group leader plus other events to which to add samples\n"); |
| 717 | return -EINVAL; |
| 718 | } |
| 719 | |
| 720 | if (!perf_can_aux_sample()) { |
| 721 | pr_err("AUX area sampling is not supported by kernel\n"); |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | int auxtrace_parse_sample_options(struct auxtrace_record *itr, |
| 729 | struct evlist *evlist, |
| 730 | struct record_opts *opts, const char *str) |
| 731 | { |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 732 | struct perf_evsel_config_term *term; |
| 733 | struct evsel *aux_evsel; |
| 734 | bool has_aux_sample_size = false; |
Adrian Hunter | f0bb7ee | 2019-11-15 14:42:15 +0200 | [diff] [blame] | 735 | bool has_aux_leader = false; |
| 736 | struct evsel *evsel; |
| 737 | char *endptr; |
| 738 | unsigned long sz; |
| 739 | |
| 740 | if (!str) |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 741 | goto no_opt; |
Adrian Hunter | f0bb7ee | 2019-11-15 14:42:15 +0200 | [diff] [blame] | 742 | |
| 743 | if (!itr) { |
| 744 | pr_err("No AUX area event to sample\n"); |
| 745 | return -EINVAL; |
| 746 | } |
| 747 | |
| 748 | sz = strtoul(str, &endptr, 0); |
| 749 | if (*endptr || sz > UINT_MAX) { |
| 750 | pr_err("Bad AUX area sampling option: '%s'\n", str); |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | |
| 754 | if (!sz) |
| 755 | sz = itr->default_aux_sample_size; |
| 756 | |
| 757 | if (!sz) |
| 758 | sz = DEFAULT_AUX_SAMPLE_SIZE; |
| 759 | |
| 760 | /* Set aux_sample_size based on --aux-sample option */ |
| 761 | evlist__for_each_entry(evlist, evsel) { |
| 762 | if (perf_evsel__is_group_leader(evsel)) { |
Arnaldo Carvalho de Melo | 39453ed | 2020-04-29 15:51:38 -0300 | [diff] [blame] | 763 | has_aux_leader = evsel__is_aux_event(evsel); |
Adrian Hunter | f0bb7ee | 2019-11-15 14:42:15 +0200 | [diff] [blame] | 764 | } else if (has_aux_leader) { |
| 765 | evsel->core.attr.aux_sample_size = sz; |
| 766 | } |
| 767 | } |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 768 | no_opt: |
| 769 | aux_evsel = NULL; |
| 770 | /* Override with aux_sample_size from config term */ |
| 771 | evlist__for_each_entry(evlist, evsel) { |
Arnaldo Carvalho de Melo | 39453ed | 2020-04-29 15:51:38 -0300 | [diff] [blame] | 772 | if (evsel__is_aux_event(evsel)) |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 773 | aux_evsel = evsel; |
| 774 | term = perf_evsel__get_config_term(evsel, AUX_SAMPLE_SIZE); |
| 775 | if (term) { |
| 776 | has_aux_sample_size = true; |
| 777 | evsel->core.attr.aux_sample_size = term->val.aux_sample_size; |
| 778 | /* If possible, group with the AUX event */ |
| 779 | if (aux_evsel && evsel->core.attr.aux_sample_size) |
| 780 | perf_evlist__regroup(evlist, aux_evsel, evsel); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | if (!str && !has_aux_sample_size) |
| 785 | return 0; |
| 786 | |
| 787 | if (!itr) { |
| 788 | pr_err("No AUX area event to sample\n"); |
| 789 | return -EINVAL; |
| 790 | } |
Adrian Hunter | f0bb7ee | 2019-11-15 14:42:15 +0200 | [diff] [blame] | 791 | |
| 792 | return auxtrace_validate_aux_sample_size(evlist, opts); |
| 793 | } |
| 794 | |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 795 | struct auxtrace_record *__weak |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 796 | auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err) |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 797 | { |
| 798 | *err = 0; |
| 799 | return NULL; |
| 800 | } |
| 801 | |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 802 | static int auxtrace_index__alloc(struct list_head *head) |
| 803 | { |
| 804 | struct auxtrace_index *auxtrace_index; |
| 805 | |
| 806 | auxtrace_index = malloc(sizeof(struct auxtrace_index)); |
| 807 | if (!auxtrace_index) |
| 808 | return -ENOMEM; |
| 809 | |
| 810 | auxtrace_index->nr = 0; |
| 811 | INIT_LIST_HEAD(&auxtrace_index->list); |
| 812 | |
| 813 | list_add_tail(&auxtrace_index->list, head); |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | void auxtrace_index__free(struct list_head *head) |
| 819 | { |
| 820 | struct auxtrace_index *auxtrace_index, *n; |
| 821 | |
| 822 | list_for_each_entry_safe(auxtrace_index, n, head, list) { |
Arnaldo Carvalho de Melo | e56fbc9 | 2019-07-04 12:13:46 -0300 | [diff] [blame] | 823 | list_del_init(&auxtrace_index->list); |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 824 | free(auxtrace_index); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | static struct auxtrace_index *auxtrace_index__last(struct list_head *head) |
| 829 | { |
| 830 | struct auxtrace_index *auxtrace_index; |
| 831 | int err; |
| 832 | |
| 833 | if (list_empty(head)) { |
| 834 | err = auxtrace_index__alloc(head); |
| 835 | if (err) |
| 836 | return NULL; |
| 837 | } |
| 838 | |
| 839 | auxtrace_index = list_entry(head->prev, struct auxtrace_index, list); |
| 840 | |
| 841 | if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) { |
| 842 | err = auxtrace_index__alloc(head); |
| 843 | if (err) |
| 844 | return NULL; |
| 845 | auxtrace_index = list_entry(head->prev, struct auxtrace_index, |
| 846 | list); |
| 847 | } |
| 848 | |
| 849 | return auxtrace_index; |
| 850 | } |
| 851 | |
| 852 | int auxtrace_index__auxtrace_event(struct list_head *head, |
| 853 | union perf_event *event, off_t file_offset) |
| 854 | { |
| 855 | struct auxtrace_index *auxtrace_index; |
| 856 | size_t nr; |
| 857 | |
| 858 | auxtrace_index = auxtrace_index__last(head); |
| 859 | if (!auxtrace_index) |
| 860 | return -ENOMEM; |
| 861 | |
| 862 | nr = auxtrace_index->nr; |
| 863 | auxtrace_index->entries[nr].file_offset = file_offset; |
| 864 | auxtrace_index->entries[nr].sz = event->header.size; |
| 865 | auxtrace_index->nr += 1; |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | static int auxtrace_index__do_write(int fd, |
| 871 | struct auxtrace_index *auxtrace_index) |
| 872 | { |
| 873 | struct auxtrace_index_entry ent; |
| 874 | size_t i; |
| 875 | |
| 876 | for (i = 0; i < auxtrace_index->nr; i++) { |
| 877 | ent.file_offset = auxtrace_index->entries[i].file_offset; |
| 878 | ent.sz = auxtrace_index->entries[i].sz; |
| 879 | if (writen(fd, &ent, sizeof(ent)) != sizeof(ent)) |
| 880 | return -errno; |
| 881 | } |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | int auxtrace_index__write(int fd, struct list_head *head) |
| 886 | { |
| 887 | struct auxtrace_index *auxtrace_index; |
| 888 | u64 total = 0; |
| 889 | int err; |
| 890 | |
| 891 | list_for_each_entry(auxtrace_index, head, list) |
| 892 | total += auxtrace_index->nr; |
| 893 | |
| 894 | if (writen(fd, &total, sizeof(total)) != sizeof(total)) |
| 895 | return -errno; |
| 896 | |
| 897 | list_for_each_entry(auxtrace_index, head, list) { |
| 898 | err = auxtrace_index__do_write(fd, auxtrace_index); |
| 899 | if (err) |
| 900 | return err; |
| 901 | } |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static int auxtrace_index__process_entry(int fd, struct list_head *head, |
| 907 | bool needs_swap) |
| 908 | { |
| 909 | struct auxtrace_index *auxtrace_index; |
| 910 | struct auxtrace_index_entry ent; |
| 911 | size_t nr; |
| 912 | |
| 913 | if (readn(fd, &ent, sizeof(ent)) != sizeof(ent)) |
| 914 | return -1; |
| 915 | |
| 916 | auxtrace_index = auxtrace_index__last(head); |
| 917 | if (!auxtrace_index) |
| 918 | return -1; |
| 919 | |
| 920 | nr = auxtrace_index->nr; |
| 921 | if (needs_swap) { |
| 922 | auxtrace_index->entries[nr].file_offset = |
| 923 | bswap_64(ent.file_offset); |
| 924 | auxtrace_index->entries[nr].sz = bswap_64(ent.sz); |
| 925 | } else { |
| 926 | auxtrace_index->entries[nr].file_offset = ent.file_offset; |
| 927 | auxtrace_index->entries[nr].sz = ent.sz; |
| 928 | } |
| 929 | |
| 930 | auxtrace_index->nr = nr + 1; |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | int auxtrace_index__process(int fd, u64 size, struct perf_session *session, |
| 936 | bool needs_swap) |
| 937 | { |
| 938 | struct list_head *head = &session->auxtrace_index; |
| 939 | u64 nr; |
| 940 | |
| 941 | if (readn(fd, &nr, sizeof(u64)) != sizeof(u64)) |
| 942 | return -1; |
| 943 | |
| 944 | if (needs_swap) |
| 945 | nr = bswap_64(nr); |
| 946 | |
| 947 | if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size) |
| 948 | return -1; |
| 949 | |
| 950 | while (nr--) { |
| 951 | int err; |
| 952 | |
| 953 | err = auxtrace_index__process_entry(fd, head, needs_swap); |
| 954 | if (err) |
| 955 | return -1; |
| 956 | } |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues, |
| 962 | struct perf_session *session, |
| 963 | struct auxtrace_index_entry *ent) |
| 964 | { |
| 965 | return auxtrace_queues__add_indexed_event(queues, session, |
| 966 | ent->file_offset, ent->sz); |
| 967 | } |
| 968 | |
| 969 | int auxtrace_queues__process_index(struct auxtrace_queues *queues, |
| 970 | struct perf_session *session) |
| 971 | { |
| 972 | struct auxtrace_index *auxtrace_index; |
| 973 | struct auxtrace_index_entry *ent; |
| 974 | size_t i; |
| 975 | int err; |
| 976 | |
Adrian Hunter | 2e2967f | 2018-03-06 11:13:13 +0200 | [diff] [blame] | 977 | if (auxtrace__dont_decode(session)) |
| 978 | return 0; |
| 979 | |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 980 | list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) { |
| 981 | for (i = 0; i < auxtrace_index->nr; i++) { |
| 982 | ent = &auxtrace_index->entries[i]; |
| 983 | err = auxtrace_queues__process_index_entry(queues, |
| 984 | session, |
| 985 | ent); |
| 986 | if (err) |
| 987 | return err; |
| 988 | } |
| 989 | } |
| 990 | return 0; |
| 991 | } |
| 992 | |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 993 | struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue, |
| 994 | struct auxtrace_buffer *buffer) |
| 995 | { |
| 996 | if (buffer) { |
| 997 | if (list_is_last(&buffer->list, &queue->head)) |
| 998 | return NULL; |
| 999 | return list_entry(buffer->list.next, struct auxtrace_buffer, |
| 1000 | list); |
| 1001 | } else { |
| 1002 | if (list_empty(&queue->head)) |
| 1003 | return NULL; |
| 1004 | return list_entry(queue->head.next, struct auxtrace_buffer, |
| 1005 | list); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Adrian Hunter | ac2f445 | 2019-11-15 14:42:21 +0200 | [diff] [blame] | 1009 | struct auxtrace_queue *auxtrace_queues__sample_queue(struct auxtrace_queues *queues, |
| 1010 | struct perf_sample *sample, |
| 1011 | struct perf_session *session) |
| 1012 | { |
| 1013 | struct perf_sample_id *sid; |
| 1014 | unsigned int idx; |
| 1015 | u64 id; |
| 1016 | |
| 1017 | id = sample->id; |
| 1018 | if (!id) |
| 1019 | return NULL; |
| 1020 | |
| 1021 | sid = perf_evlist__id2sid(session->evlist, id); |
| 1022 | if (!sid) |
| 1023 | return NULL; |
| 1024 | |
| 1025 | idx = sid->idx; |
| 1026 | |
| 1027 | if (idx >= queues->nr_queues) |
| 1028 | return NULL; |
| 1029 | |
| 1030 | return &queues->queue_array[idx]; |
| 1031 | } |
| 1032 | |
| 1033 | int auxtrace_queues__add_sample(struct auxtrace_queues *queues, |
| 1034 | struct perf_session *session, |
| 1035 | struct perf_sample *sample, u64 data_offset, |
| 1036 | u64 reference) |
| 1037 | { |
| 1038 | struct auxtrace_buffer buffer = { |
| 1039 | .pid = -1, |
| 1040 | .data_offset = data_offset, |
| 1041 | .reference = reference, |
| 1042 | .size = sample->aux_sample.size, |
| 1043 | }; |
| 1044 | struct perf_sample_id *sid; |
| 1045 | u64 id = sample->id; |
| 1046 | unsigned int idx; |
| 1047 | |
| 1048 | if (!id) |
| 1049 | return -EINVAL; |
| 1050 | |
| 1051 | sid = perf_evlist__id2sid(session->evlist, id); |
| 1052 | if (!sid) |
| 1053 | return -ENOENT; |
| 1054 | |
| 1055 | idx = sid->idx; |
| 1056 | buffer.tid = sid->tid; |
| 1057 | buffer.cpu = sid->cpu; |
| 1058 | |
| 1059 | return auxtrace_queues__add_buffer(queues, session, idx, &buffer, NULL); |
| 1060 | } |
| 1061 | |
| 1062 | struct queue_data { |
| 1063 | bool samples; |
| 1064 | bool events; |
| 1065 | }; |
| 1066 | |
| 1067 | static int auxtrace_queue_data_cb(struct perf_session *session, |
| 1068 | union perf_event *event, u64 offset, |
| 1069 | void *data) |
| 1070 | { |
| 1071 | struct queue_data *qd = data; |
| 1072 | struct perf_sample sample; |
| 1073 | int err; |
| 1074 | |
| 1075 | if (qd->events && event->header.type == PERF_RECORD_AUXTRACE) { |
| 1076 | if (event->header.size < sizeof(struct perf_record_auxtrace)) |
| 1077 | return -EINVAL; |
| 1078 | offset += event->header.size; |
| 1079 | return session->auxtrace->queue_data(session, NULL, event, |
| 1080 | offset); |
| 1081 | } |
| 1082 | |
| 1083 | if (!qd->samples || event->header.type != PERF_RECORD_SAMPLE) |
| 1084 | return 0; |
| 1085 | |
| 1086 | err = perf_evlist__parse_sample(session->evlist, event, &sample); |
| 1087 | if (err) |
| 1088 | return err; |
| 1089 | |
| 1090 | if (!sample.aux_sample.size) |
| 1091 | return 0; |
| 1092 | |
| 1093 | offset += sample.aux_sample.data - (void *)event; |
| 1094 | |
| 1095 | return session->auxtrace->queue_data(session, &sample, NULL, offset); |
| 1096 | } |
| 1097 | |
| 1098 | int auxtrace_queue_data(struct perf_session *session, bool samples, bool events) |
| 1099 | { |
| 1100 | struct queue_data qd = { |
| 1101 | .samples = samples, |
| 1102 | .events = events, |
| 1103 | }; |
| 1104 | |
| 1105 | if (auxtrace__dont_decode(session)) |
| 1106 | return 0; |
| 1107 | |
| 1108 | if (!session->auxtrace || !session->auxtrace->queue_data) |
| 1109 | return -EINVAL; |
| 1110 | |
| 1111 | return perf_session__peek_events(session, session->header.data_offset, |
| 1112 | session->header.data_size, |
| 1113 | auxtrace_queue_data_cb, &qd); |
| 1114 | } |
| 1115 | |
Adrian Hunter | e502789 | 2015-04-21 12:21:51 +0300 | [diff] [blame] | 1116 | void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd) |
| 1117 | { |
| 1118 | size_t adj = buffer->data_offset & (page_size - 1); |
| 1119 | size_t size = buffer->size + adj; |
| 1120 | off_t file_offset = buffer->data_offset - adj; |
| 1121 | void *addr; |
| 1122 | |
| 1123 | if (buffer->data) |
| 1124 | return buffer->data; |
| 1125 | |
| 1126 | addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset); |
| 1127 | if (addr == MAP_FAILED) |
| 1128 | return NULL; |
| 1129 | |
| 1130 | buffer->mmap_addr = addr; |
| 1131 | buffer->mmap_size = size; |
| 1132 | |
| 1133 | buffer->data = addr + adj; |
| 1134 | |
| 1135 | return buffer->data; |
| 1136 | } |
| 1137 | |
| 1138 | void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer) |
| 1139 | { |
| 1140 | if (!buffer->data || !buffer->mmap_addr) |
| 1141 | return; |
| 1142 | munmap(buffer->mmap_addr, buffer->mmap_size); |
| 1143 | buffer->mmap_addr = NULL; |
| 1144 | buffer->mmap_size = 0; |
| 1145 | buffer->data = NULL; |
| 1146 | buffer->use_data = NULL; |
| 1147 | } |
| 1148 | |
| 1149 | void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer) |
| 1150 | { |
| 1151 | auxtrace_buffer__put_data(buffer); |
| 1152 | if (buffer->data_needs_freeing) { |
| 1153 | buffer->data_needs_freeing = false; |
| 1154 | zfree(&buffer->data); |
| 1155 | buffer->use_data = NULL; |
| 1156 | buffer->size = 0; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | void auxtrace_buffer__free(struct auxtrace_buffer *buffer) |
| 1161 | { |
| 1162 | auxtrace_buffer__drop_data(buffer); |
| 1163 | free(buffer); |
| 1164 | } |
| 1165 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1166 | void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type, |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1167 | int code, int cpu, pid_t pid, pid_t tid, u64 ip, |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1168 | const char *msg, u64 timestamp) |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1169 | { |
| 1170 | size_t size; |
| 1171 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1172 | memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error)); |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1173 | |
| 1174 | auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR; |
| 1175 | auxtrace_error->type = type; |
| 1176 | auxtrace_error->code = code; |
| 1177 | auxtrace_error->cpu = cpu; |
| 1178 | auxtrace_error->pid = pid; |
| 1179 | auxtrace_error->tid = tid; |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1180 | auxtrace_error->fmt = 1; |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1181 | auxtrace_error->ip = ip; |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1182 | auxtrace_error->time = timestamp; |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1183 | strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG); |
| 1184 | |
| 1185 | size = (void *)auxtrace_error->msg - (void *)auxtrace_error + |
| 1186 | strlen(auxtrace_error->msg) + 1; |
| 1187 | auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64)); |
| 1188 | } |
| 1189 | |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1190 | int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr, |
| 1191 | struct perf_tool *tool, |
| 1192 | struct perf_session *session, |
| 1193 | perf_event__handler_t process) |
| 1194 | { |
| 1195 | union perf_event *ev; |
| 1196 | size_t priv_size; |
| 1197 | int err; |
| 1198 | |
| 1199 | pr_debug2("Synthesizing auxtrace information\n"); |
Mathieu Poirier | 14a05e1 | 2016-01-14 14:46:15 -0700 | [diff] [blame] | 1200 | priv_size = auxtrace_record__info_priv_size(itr, session->evlist); |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1201 | ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size); |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1202 | if (!ev) |
| 1203 | return -ENOMEM; |
| 1204 | |
| 1205 | ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO; |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1206 | ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) + |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1207 | priv_size; |
| 1208 | err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info, |
| 1209 | priv_size); |
| 1210 | if (err) |
| 1211 | goto out_free; |
| 1212 | |
| 1213 | err = process(tool, ev, NULL, NULL); |
| 1214 | out_free: |
| 1215 | free(ev); |
| 1216 | return err; |
| 1217 | } |
| 1218 | |
Adrian Hunter | 5c7bec0 | 2020-04-01 13:16:04 +0300 | [diff] [blame] | 1219 | static void unleader_evsel(struct evlist *evlist, struct evsel *leader) |
| 1220 | { |
| 1221 | struct evsel *new_leader = NULL; |
| 1222 | struct evsel *evsel; |
| 1223 | |
| 1224 | /* Find new leader for the group */ |
| 1225 | evlist__for_each_entry(evlist, evsel) { |
| 1226 | if (evsel->leader != leader || evsel == leader) |
| 1227 | continue; |
| 1228 | if (!new_leader) |
| 1229 | new_leader = evsel; |
| 1230 | evsel->leader = new_leader; |
| 1231 | } |
| 1232 | |
| 1233 | /* Update group information */ |
| 1234 | if (new_leader) { |
| 1235 | zfree(&new_leader->group_name); |
| 1236 | new_leader->group_name = leader->group_name; |
| 1237 | leader->group_name = NULL; |
| 1238 | |
| 1239 | new_leader->core.nr_members = leader->core.nr_members - 1; |
| 1240 | leader->core.nr_members = 1; |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | static void unleader_auxtrace(struct perf_session *session) |
| 1245 | { |
| 1246 | struct evsel *evsel; |
| 1247 | |
| 1248 | evlist__for_each_entry(session->evlist, evsel) { |
| 1249 | if (auxtrace__evsel_is_auxtrace(session, evsel) && |
| 1250 | perf_evsel__is_group_leader(evsel)) { |
| 1251 | unleader_evsel(session->evlist, evsel); |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1256 | int perf_event__process_auxtrace_info(struct perf_session *session, |
| 1257 | union perf_event *event) |
Adrian Hunter | 73f75fb | 2015-04-09 18:53:53 +0300 | [diff] [blame] | 1258 | { |
| 1259 | enum auxtrace_type type = event->auxtrace_info.type; |
Adrian Hunter | 5c7bec0 | 2020-04-01 13:16:04 +0300 | [diff] [blame] | 1260 | int err; |
Adrian Hunter | 73f75fb | 2015-04-09 18:53:53 +0300 | [diff] [blame] | 1261 | |
| 1262 | if (dump_trace) |
| 1263 | fprintf(stdout, " type: %u\n", type); |
| 1264 | |
| 1265 | switch (type) { |
Adrian Hunter | 55ea4ab | 2015-07-17 19:33:36 +0300 | [diff] [blame] | 1266 | case PERF_AUXTRACE_INTEL_PT: |
Adrian Hunter | 5c7bec0 | 2020-04-01 13:16:04 +0300 | [diff] [blame] | 1267 | err = intel_pt_process_auxtrace_info(event, session); |
| 1268 | break; |
Adrian Hunter | d0170af | 2015-07-17 19:33:43 +0300 | [diff] [blame] | 1269 | case PERF_AUXTRACE_INTEL_BTS: |
Adrian Hunter | 5c7bec0 | 2020-04-01 13:16:04 +0300 | [diff] [blame] | 1270 | err = intel_bts_process_auxtrace_info(event, session); |
| 1271 | break; |
Kim Phillips | ffd3d18 | 2018-01-14 13:28:50 -0600 | [diff] [blame] | 1272 | case PERF_AUXTRACE_ARM_SPE: |
Adrian Hunter | 5c7bec0 | 2020-04-01 13:16:04 +0300 | [diff] [blame] | 1273 | err = arm_spe_process_auxtrace_info(event, session); |
| 1274 | break; |
Mathieu Poirier | a818c56 | 2016-09-16 09:50:00 -0600 | [diff] [blame] | 1275 | case PERF_AUXTRACE_CS_ETM: |
Adrian Hunter | 5c7bec0 | 2020-04-01 13:16:04 +0300 | [diff] [blame] | 1276 | err = cs_etm__process_auxtrace_info(event, session); |
| 1277 | break; |
Thomas Richter | b96e661 | 2018-08-02 09:46:20 +0200 | [diff] [blame] | 1278 | case PERF_AUXTRACE_S390_CPUMSF: |
Adrian Hunter | 5c7bec0 | 2020-04-01 13:16:04 +0300 | [diff] [blame] | 1279 | err = s390_cpumsf_process_auxtrace_info(event, session); |
| 1280 | break; |
Adrian Hunter | 73f75fb | 2015-04-09 18:53:53 +0300 | [diff] [blame] | 1281 | case PERF_AUXTRACE_UNKNOWN: |
| 1282 | default: |
| 1283 | return -EINVAL; |
| 1284 | } |
Adrian Hunter | 5c7bec0 | 2020-04-01 13:16:04 +0300 | [diff] [blame] | 1285 | |
| 1286 | if (err) |
| 1287 | return err; |
| 1288 | |
| 1289 | unleader_auxtrace(session); |
| 1290 | |
| 1291 | return 0; |
Adrian Hunter | 73f75fb | 2015-04-09 18:53:53 +0300 | [diff] [blame] | 1292 | } |
| 1293 | |
Jiri Olsa | 7336555 | 2018-09-13 14:54:04 +0200 | [diff] [blame] | 1294 | s64 perf_event__process_auxtrace(struct perf_session *session, |
| 1295 | union perf_event *event) |
Adrian Hunter | 73f75fb | 2015-04-09 18:53:53 +0300 | [diff] [blame] | 1296 | { |
| 1297 | s64 err; |
| 1298 | |
| 1299 | if (dump_trace) |
Jiri Olsa | 306c9d2 | 2019-08-28 15:57:03 +0200 | [diff] [blame] | 1300 | fprintf(stdout, " size: %#"PRI_lx64" offset: %#"PRI_lx64" ref: %#"PRI_lx64" idx: %u tid: %d cpu: %d\n", |
Adrian Hunter | 73f75fb | 2015-04-09 18:53:53 +0300 | [diff] [blame] | 1301 | event->auxtrace.size, event->auxtrace.offset, |
| 1302 | event->auxtrace.reference, event->auxtrace.idx, |
| 1303 | event->auxtrace.tid, event->auxtrace.cpu); |
| 1304 | |
| 1305 | if (auxtrace__dont_decode(session)) |
| 1306 | return event->auxtrace.size; |
| 1307 | |
| 1308 | if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE) |
| 1309 | return -EINVAL; |
| 1310 | |
Jiri Olsa | 7336555 | 2018-09-13 14:54:04 +0200 | [diff] [blame] | 1311 | err = session->auxtrace->process_auxtrace_event(session, event, session->tool); |
Adrian Hunter | 73f75fb | 2015-04-09 18:53:53 +0300 | [diff] [blame] | 1312 | if (err < 0) |
| 1313 | return err; |
| 1314 | |
| 1315 | return event->auxtrace.size; |
| 1316 | } |
| 1317 | |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1318 | #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS |
| 1319 | #define PERF_ITRACE_DEFAULT_PERIOD 100000 |
| 1320 | #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16 |
| 1321 | #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024 |
Adrian Hunter | 601897b | 2015-09-25 16:15:39 +0300 | [diff] [blame] | 1322 | #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64 |
| 1323 | #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024 |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1324 | |
Andi Kleen | 4eb0681 | 2018-09-20 11:05:37 -0700 | [diff] [blame] | 1325 | void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts, |
| 1326 | bool no_sample) |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1327 | { |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1328 | synth_opts->branches = true; |
Adrian Hunter | 53c76b0 | 2015-04-30 17:37:28 +0300 | [diff] [blame] | 1329 | synth_opts->transactions = true; |
Adrian Hunter | 3bdafdf | 2017-05-26 11:17:24 +0300 | [diff] [blame] | 1330 | synth_opts->ptwrites = true; |
Adrian Hunter | 70d110d | 2017-05-26 11:17:25 +0300 | [diff] [blame] | 1331 | synth_opts->pwr_events = true; |
Adrian Hunter | 181ebb5 | 2019-08-06 11:46:03 +0300 | [diff] [blame] | 1332 | synth_opts->other_events = true; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1333 | synth_opts->errors = true; |
Andi Kleen | 4eb0681 | 2018-09-20 11:05:37 -0700 | [diff] [blame] | 1334 | if (no_sample) { |
| 1335 | synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS; |
| 1336 | synth_opts->period = 1; |
| 1337 | synth_opts->calls = true; |
| 1338 | } else { |
| 1339 | synth_opts->instructions = true; |
| 1340 | synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE; |
| 1341 | synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD; |
| 1342 | } |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1343 | synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ; |
Adrian Hunter | 601897b | 2015-09-25 16:15:39 +0300 | [diff] [blame] | 1344 | synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ; |
Andi Kleen | d1706b3 | 2016-03-28 10:45:38 -0700 | [diff] [blame] | 1345 | synth_opts->initial_skip = 0; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * Please check tools/perf/Documentation/perf-script.txt for information |
| 1350 | * about the options parsed here, which is introduced after this cset, |
| 1351 | * when support in 'perf script' for these options is introduced. |
| 1352 | */ |
| 1353 | int itrace_parse_synth_opts(const struct option *opt, const char *str, |
| 1354 | int unset) |
| 1355 | { |
| 1356 | struct itrace_synth_opts *synth_opts = opt->value; |
| 1357 | const char *p; |
| 1358 | char *endptr; |
Adrian Hunter | f70cfa0 | 2015-07-17 19:33:46 +0300 | [diff] [blame] | 1359 | bool period_type_set = false; |
Adrian Hunter | e179134 | 2015-09-25 16:15:32 +0300 | [diff] [blame] | 1360 | bool period_set = false; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1361 | |
| 1362 | synth_opts->set = true; |
| 1363 | |
| 1364 | if (unset) { |
| 1365 | synth_opts->dont_decode = true; |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
| 1369 | if (!str) { |
Adrian Hunter | 355200e | 2019-05-20 14:37:08 +0300 | [diff] [blame] | 1370 | itrace_synth_opts__set_default(synth_opts, |
| 1371 | synth_opts->default_no_sample); |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | for (p = str; *p;) { |
| 1376 | switch (*p++) { |
| 1377 | case 'i': |
| 1378 | synth_opts->instructions = true; |
| 1379 | while (*p == ' ' || *p == ',') |
| 1380 | p += 1; |
| 1381 | if (isdigit(*p)) { |
| 1382 | synth_opts->period = strtoull(p, &endptr, 10); |
Adrian Hunter | e179134 | 2015-09-25 16:15:32 +0300 | [diff] [blame] | 1383 | period_set = true; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1384 | p = endptr; |
| 1385 | while (*p == ' ' || *p == ',') |
| 1386 | p += 1; |
| 1387 | switch (*p++) { |
| 1388 | case 'i': |
| 1389 | synth_opts->period_type = |
| 1390 | PERF_ITRACE_PERIOD_INSTRUCTIONS; |
Adrian Hunter | f70cfa0 | 2015-07-17 19:33:46 +0300 | [diff] [blame] | 1391 | period_type_set = true; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1392 | break; |
| 1393 | case 't': |
| 1394 | synth_opts->period_type = |
| 1395 | PERF_ITRACE_PERIOD_TICKS; |
Adrian Hunter | f70cfa0 | 2015-07-17 19:33:46 +0300 | [diff] [blame] | 1396 | period_type_set = true; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1397 | break; |
| 1398 | case 'm': |
| 1399 | synth_opts->period *= 1000; |
| 1400 | /* Fall through */ |
| 1401 | case 'u': |
| 1402 | synth_opts->period *= 1000; |
| 1403 | /* Fall through */ |
| 1404 | case 'n': |
| 1405 | if (*p++ != 's') |
| 1406 | goto out_err; |
| 1407 | synth_opts->period_type = |
| 1408 | PERF_ITRACE_PERIOD_NANOSECS; |
Adrian Hunter | f70cfa0 | 2015-07-17 19:33:46 +0300 | [diff] [blame] | 1409 | period_type_set = true; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1410 | break; |
| 1411 | case '\0': |
| 1412 | goto out; |
| 1413 | default: |
| 1414 | goto out_err; |
| 1415 | } |
| 1416 | } |
| 1417 | break; |
| 1418 | case 'b': |
| 1419 | synth_opts->branches = true; |
| 1420 | break; |
Adrian Hunter | 53c76b0 | 2015-04-30 17:37:28 +0300 | [diff] [blame] | 1421 | case 'x': |
| 1422 | synth_opts->transactions = true; |
| 1423 | break; |
Adrian Hunter | 3bdafdf | 2017-05-26 11:17:24 +0300 | [diff] [blame] | 1424 | case 'w': |
| 1425 | synth_opts->ptwrites = true; |
| 1426 | break; |
Adrian Hunter | 70d110d | 2017-05-26 11:17:25 +0300 | [diff] [blame] | 1427 | case 'p': |
| 1428 | synth_opts->pwr_events = true; |
| 1429 | break; |
Adrian Hunter | 181ebb5 | 2019-08-06 11:46:03 +0300 | [diff] [blame] | 1430 | case 'o': |
| 1431 | synth_opts->other_events = true; |
| 1432 | break; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1433 | case 'e': |
| 1434 | synth_opts->errors = true; |
| 1435 | break; |
| 1436 | case 'd': |
| 1437 | synth_opts->log = true; |
| 1438 | break; |
| 1439 | case 'c': |
| 1440 | synth_opts->branches = true; |
| 1441 | synth_opts->calls = true; |
| 1442 | break; |
| 1443 | case 'r': |
| 1444 | synth_opts->branches = true; |
| 1445 | synth_opts->returns = true; |
| 1446 | break; |
Adrian Hunter | 1c5c25b | 2020-04-01 13:16:05 +0300 | [diff] [blame] | 1447 | case 'G': |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1448 | case 'g': |
Adrian Hunter | 1c5c25b | 2020-04-01 13:16:05 +0300 | [diff] [blame] | 1449 | if (p[-1] == 'G') |
| 1450 | synth_opts->add_callchain = true; |
| 1451 | else |
| 1452 | synth_opts->callchain = true; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1453 | synth_opts->callchain_sz = |
| 1454 | PERF_ITRACE_DEFAULT_CALLCHAIN_SZ; |
| 1455 | while (*p == ' ' || *p == ',') |
| 1456 | p += 1; |
| 1457 | if (isdigit(*p)) { |
| 1458 | unsigned int val; |
| 1459 | |
| 1460 | val = strtoul(p, &endptr, 10); |
| 1461 | p = endptr; |
| 1462 | if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ) |
| 1463 | goto out_err; |
| 1464 | synth_opts->callchain_sz = val; |
| 1465 | } |
| 1466 | break; |
Adrian Hunter | ec90e42 | 2020-04-29 18:07:46 +0300 | [diff] [blame] | 1467 | case 'L': |
Adrian Hunter | 601897b | 2015-09-25 16:15:39 +0300 | [diff] [blame] | 1468 | case 'l': |
Adrian Hunter | ec90e42 | 2020-04-29 18:07:46 +0300 | [diff] [blame] | 1469 | if (p[-1] == 'L') |
| 1470 | synth_opts->add_last_branch = true; |
| 1471 | else |
| 1472 | synth_opts->last_branch = true; |
Adrian Hunter | 601897b | 2015-09-25 16:15:39 +0300 | [diff] [blame] | 1473 | synth_opts->last_branch_sz = |
| 1474 | PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ; |
| 1475 | while (*p == ' ' || *p == ',') |
| 1476 | p += 1; |
| 1477 | if (isdigit(*p)) { |
| 1478 | unsigned int val; |
| 1479 | |
| 1480 | val = strtoul(p, &endptr, 10); |
| 1481 | p = endptr; |
| 1482 | if (!val || |
| 1483 | val > PERF_ITRACE_MAX_LAST_BRANCH_SZ) |
| 1484 | goto out_err; |
| 1485 | synth_opts->last_branch_sz = val; |
| 1486 | } |
| 1487 | break; |
Andi Kleen | d1706b3 | 2016-03-28 10:45:38 -0700 | [diff] [blame] | 1488 | case 's': |
| 1489 | synth_opts->initial_skip = strtoul(p, &endptr, 10); |
| 1490 | if (p == endptr) |
| 1491 | goto out_err; |
| 1492 | p = endptr; |
| 1493 | break; |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1494 | case ' ': |
| 1495 | case ',': |
| 1496 | break; |
| 1497 | default: |
| 1498 | goto out_err; |
| 1499 | } |
| 1500 | } |
| 1501 | out: |
| 1502 | if (synth_opts->instructions) { |
Adrian Hunter | f70cfa0 | 2015-07-17 19:33:46 +0300 | [diff] [blame] | 1503 | if (!period_type_set) |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1504 | synth_opts->period_type = |
| 1505 | PERF_ITRACE_DEFAULT_PERIOD_TYPE; |
Adrian Hunter | e179134 | 2015-09-25 16:15:32 +0300 | [diff] [blame] | 1506 | if (!period_set) |
Adrian Hunter | f6986c95 | 2015-04-09 18:53:49 +0300 | [diff] [blame] | 1507 | synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD; |
| 1508 | } |
| 1509 | |
| 1510 | return 0; |
| 1511 | |
| 1512 | out_err: |
| 1513 | pr_err("Bad Instruction Tracing options '%s'\n", str); |
| 1514 | return -EINVAL; |
| 1515 | } |
| 1516 | |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1517 | static const char * const auxtrace_error_type_name[] = { |
| 1518 | [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace", |
| 1519 | }; |
| 1520 | |
| 1521 | static const char *auxtrace_error_name(int type) |
| 1522 | { |
| 1523 | const char *error_type_name = NULL; |
| 1524 | |
| 1525 | if (type < PERF_AUXTRACE_ERROR_MAX) |
| 1526 | error_type_name = auxtrace_error_type_name[type]; |
| 1527 | if (!error_type_name) |
| 1528 | error_type_name = "unknown AUX"; |
| 1529 | return error_type_name; |
| 1530 | } |
| 1531 | |
| 1532 | size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp) |
| 1533 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1534 | struct perf_record_auxtrace_error *e = &event->auxtrace_error; |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1535 | unsigned long long nsecs = e->time; |
| 1536 | const char *msg = e->msg; |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1537 | int ret; |
| 1538 | |
| 1539 | ret = fprintf(fp, " %s error type %u", |
| 1540 | auxtrace_error_name(e->type), e->type); |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1541 | |
| 1542 | if (e->fmt && nsecs) { |
| 1543 | unsigned long secs = nsecs / NSEC_PER_SEC; |
| 1544 | |
| 1545 | nsecs -= secs * NSEC_PER_SEC; |
| 1546 | ret += fprintf(fp, " time %lu.%09llu", secs, nsecs); |
| 1547 | } else { |
| 1548 | ret += fprintf(fp, " time 0"); |
| 1549 | } |
| 1550 | |
| 1551 | if (!e->fmt) |
| 1552 | msg = (const char *)&e->time; |
| 1553 | |
Jiri Olsa | 3460efb | 2019-08-28 15:57:04 +0200 | [diff] [blame] | 1554 | ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n", |
Adrian Hunter | 16bd432 | 2019-02-06 12:39:47 +0200 | [diff] [blame] | 1555 | e->cpu, e->pid, e->tid, e->ip, e->code, msg); |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1556 | return ret; |
| 1557 | } |
| 1558 | |
| 1559 | void perf_session__auxtrace_error_inc(struct perf_session *session, |
| 1560 | union perf_event *event) |
| 1561 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1562 | struct perf_record_auxtrace_error *e = &event->auxtrace_error; |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1563 | |
| 1564 | if (e->type < PERF_AUXTRACE_ERROR_MAX) |
| 1565 | session->evlist->stats.nr_auxtrace_errors[e->type] += 1; |
| 1566 | } |
| 1567 | |
| 1568 | void events_stats__auxtrace_error_warn(const struct events_stats *stats) |
| 1569 | { |
| 1570 | int i; |
| 1571 | |
| 1572 | for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) { |
| 1573 | if (!stats->nr_auxtrace_errors[i]) |
| 1574 | continue; |
| 1575 | ui__warning("%u %s errors\n", |
| 1576 | stats->nr_auxtrace_errors[i], |
| 1577 | auxtrace_error_name(i)); |
| 1578 | } |
| 1579 | } |
| 1580 | |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 1581 | int perf_event__process_auxtrace_error(struct perf_session *session, |
| 1582 | union perf_event *event) |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1583 | { |
Adrian Hunter | 73f75fb | 2015-04-09 18:53:53 +0300 | [diff] [blame] | 1584 | if (auxtrace__dont_decode(session)) |
| 1585 | return 0; |
| 1586 | |
Adrian Hunter | 85ed472 | 2015-04-09 18:53:50 +0300 | [diff] [blame] | 1587 | perf_event__fprintf_auxtrace_error(event, stdout); |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 1591 | static int __auxtrace_mmap__read(struct mmap *map, |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1592 | struct auxtrace_record *itr, |
| 1593 | struct perf_tool *tool, process_auxtrace_t fn, |
| 1594 | bool snapshot, size_t snapshot_size) |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1595 | { |
Jiri Olsa | e035f4c | 2018-09-13 14:54:05 +0200 | [diff] [blame] | 1596 | struct auxtrace_mmap *mm = &map->auxtrace_mmap; |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1597 | u64 head, old = mm->prev, offset, ref; |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1598 | unsigned char *data = mm->base; |
| 1599 | size_t size, head_off, old_off, len1, len2, padding; |
| 1600 | union perf_event ev; |
| 1601 | void *data1, *data2; |
| 1602 | |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1603 | if (snapshot) { |
| 1604 | head = auxtrace_mmap__read_snapshot_head(mm); |
| 1605 | if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data, |
| 1606 | &head, &old)) |
| 1607 | return -1; |
| 1608 | } else { |
| 1609 | head = auxtrace_mmap__read_head(mm); |
| 1610 | } |
| 1611 | |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1612 | if (old == head) |
| 1613 | return 0; |
| 1614 | |
| 1615 | pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n", |
| 1616 | mm->idx, old, head, head - old); |
| 1617 | |
| 1618 | if (mm->mask) { |
| 1619 | head_off = head & mm->mask; |
| 1620 | old_off = old & mm->mask; |
| 1621 | } else { |
| 1622 | head_off = head % mm->len; |
| 1623 | old_off = old % mm->len; |
| 1624 | } |
| 1625 | |
| 1626 | if (head_off > old_off) |
| 1627 | size = head_off - old_off; |
| 1628 | else |
| 1629 | size = mm->len - (old_off - head_off); |
| 1630 | |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1631 | if (snapshot && size > snapshot_size) |
| 1632 | size = snapshot_size; |
| 1633 | |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1634 | ref = auxtrace_record__reference(itr); |
| 1635 | |
| 1636 | if (head > old || size <= head || mm->mask) { |
| 1637 | offset = head - size; |
| 1638 | } else { |
| 1639 | /* |
| 1640 | * When the buffer size is not a power of 2, 'head' wraps at the |
| 1641 | * highest multiple of the buffer size, so we have to subtract |
| 1642 | * the remainder here. |
| 1643 | */ |
| 1644 | u64 rem = (0ULL - mm->len) % mm->len; |
| 1645 | |
| 1646 | offset = head - size - rem; |
| 1647 | } |
| 1648 | |
| 1649 | if (size > head_off) { |
| 1650 | len1 = size - head_off; |
| 1651 | data1 = &data[mm->len - len1]; |
| 1652 | len2 = head_off; |
| 1653 | data2 = &data[0]; |
| 1654 | } else { |
| 1655 | len1 = size; |
| 1656 | data1 = &data[head_off - len1]; |
| 1657 | len2 = 0; |
| 1658 | data2 = NULL; |
| 1659 | } |
| 1660 | |
Adrian Hunter | 83b2ea2 | 2015-05-29 16:33:38 +0300 | [diff] [blame] | 1661 | if (itr->alignment) { |
| 1662 | unsigned int unwanted = len1 % itr->alignment; |
| 1663 | |
| 1664 | len1 -= unwanted; |
| 1665 | size -= unwanted; |
| 1666 | } |
| 1667 | |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1668 | /* padding must be written by fn() e.g. record__process_auxtrace() */ |
Adrian Hunter | c3fcadf0 | 2019-02-06 12:39:43 +0200 | [diff] [blame] | 1669 | padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1); |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1670 | if (padding) |
Adrian Hunter | c3fcadf0 | 2019-02-06 12:39:43 +0200 | [diff] [blame] | 1671 | padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding; |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1672 | |
| 1673 | memset(&ev, 0, sizeof(ev)); |
| 1674 | ev.auxtrace.header.type = PERF_RECORD_AUXTRACE; |
| 1675 | ev.auxtrace.header.size = sizeof(ev.auxtrace); |
| 1676 | ev.auxtrace.size = size + padding; |
| 1677 | ev.auxtrace.offset = offset; |
| 1678 | ev.auxtrace.reference = ref; |
| 1679 | ev.auxtrace.idx = mm->idx; |
| 1680 | ev.auxtrace.tid = mm->tid; |
| 1681 | ev.auxtrace.cpu = mm->cpu; |
| 1682 | |
Jiri Olsa | ded2b8f | 2018-09-13 14:54:06 +0200 | [diff] [blame] | 1683 | if (fn(tool, map, &ev, data1, len1, data2, len2)) |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1684 | return -1; |
| 1685 | |
| 1686 | mm->prev = head; |
| 1687 | |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1688 | if (!snapshot) { |
| 1689 | auxtrace_mmap__write_tail(mm, head); |
| 1690 | if (itr->read_finish) { |
| 1691 | int err; |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1692 | |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1693 | err = itr->read_finish(itr, mm->idx); |
| 1694 | if (err < 0) |
| 1695 | return err; |
| 1696 | } |
Adrian Hunter | 9e0cc4f | 2015-04-09 18:53:44 +0300 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | return 1; |
| 1700 | } |
Adrian Hunter | c3278f0 | 2015-04-09 18:53:54 +0300 | [diff] [blame] | 1701 | |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 1702 | int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr, |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1703 | struct perf_tool *tool, process_auxtrace_t fn) |
| 1704 | { |
Jiri Olsa | e035f4c | 2018-09-13 14:54:05 +0200 | [diff] [blame] | 1705 | return __auxtrace_mmap__read(map, itr, tool, fn, false, 0); |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1706 | } |
| 1707 | |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 1708 | int auxtrace_mmap__read_snapshot(struct mmap *map, |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1709 | struct auxtrace_record *itr, |
| 1710 | struct perf_tool *tool, process_auxtrace_t fn, |
| 1711 | size_t snapshot_size) |
| 1712 | { |
Jiri Olsa | e035f4c | 2018-09-13 14:54:05 +0200 | [diff] [blame] | 1713 | return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size); |
Adrian Hunter | d20031b | 2015-04-30 17:37:31 +0300 | [diff] [blame] | 1714 | } |
| 1715 | |
Adrian Hunter | c3278f0 | 2015-04-09 18:53:54 +0300 | [diff] [blame] | 1716 | /** |
| 1717 | * struct auxtrace_cache - hash table to implement a cache |
| 1718 | * @hashtable: the hashtable |
| 1719 | * @sz: hashtable size (number of hlists) |
| 1720 | * @entry_size: size of an entry |
| 1721 | * @limit: limit the number of entries to this maximum, when reached the cache |
| 1722 | * is dropped and caching begins again with an empty cache |
| 1723 | * @cnt: current number of entries |
| 1724 | * @bits: hashtable size (@sz = 2^@bits) |
| 1725 | */ |
| 1726 | struct auxtrace_cache { |
| 1727 | struct hlist_head *hashtable; |
| 1728 | size_t sz; |
| 1729 | size_t entry_size; |
| 1730 | size_t limit; |
| 1731 | size_t cnt; |
| 1732 | unsigned int bits; |
| 1733 | }; |
| 1734 | |
| 1735 | struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size, |
| 1736 | unsigned int limit_percent) |
| 1737 | { |
| 1738 | struct auxtrace_cache *c; |
| 1739 | struct hlist_head *ht; |
| 1740 | size_t sz, i; |
| 1741 | |
| 1742 | c = zalloc(sizeof(struct auxtrace_cache)); |
| 1743 | if (!c) |
| 1744 | return NULL; |
| 1745 | |
| 1746 | sz = 1UL << bits; |
| 1747 | |
| 1748 | ht = calloc(sz, sizeof(struct hlist_head)); |
| 1749 | if (!ht) |
| 1750 | goto out_free; |
| 1751 | |
| 1752 | for (i = 0; i < sz; i++) |
| 1753 | INIT_HLIST_HEAD(&ht[i]); |
| 1754 | |
| 1755 | c->hashtable = ht; |
| 1756 | c->sz = sz; |
| 1757 | c->entry_size = entry_size; |
| 1758 | c->limit = (c->sz * limit_percent) / 100; |
| 1759 | c->bits = bits; |
| 1760 | |
| 1761 | return c; |
| 1762 | |
| 1763 | out_free: |
| 1764 | free(c); |
| 1765 | return NULL; |
| 1766 | } |
| 1767 | |
| 1768 | static void auxtrace_cache__drop(struct auxtrace_cache *c) |
| 1769 | { |
| 1770 | struct auxtrace_cache_entry *entry; |
| 1771 | struct hlist_node *tmp; |
| 1772 | size_t i; |
| 1773 | |
| 1774 | if (!c) |
| 1775 | return; |
| 1776 | |
| 1777 | for (i = 0; i < c->sz; i++) { |
| 1778 | hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) { |
| 1779 | hlist_del(&entry->hash); |
| 1780 | auxtrace_cache__free_entry(c, entry); |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | c->cnt = 0; |
| 1785 | } |
| 1786 | |
| 1787 | void auxtrace_cache__free(struct auxtrace_cache *c) |
| 1788 | { |
| 1789 | if (!c) |
| 1790 | return; |
| 1791 | |
| 1792 | auxtrace_cache__drop(c); |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 1793 | zfree(&c->hashtable); |
Adrian Hunter | c3278f0 | 2015-04-09 18:53:54 +0300 | [diff] [blame] | 1794 | free(c); |
| 1795 | } |
| 1796 | |
| 1797 | void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c) |
| 1798 | { |
| 1799 | return malloc(c->entry_size); |
| 1800 | } |
| 1801 | |
| 1802 | void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused, |
| 1803 | void *entry) |
| 1804 | { |
| 1805 | free(entry); |
| 1806 | } |
| 1807 | |
| 1808 | int auxtrace_cache__add(struct auxtrace_cache *c, u32 key, |
| 1809 | struct auxtrace_cache_entry *entry) |
| 1810 | { |
| 1811 | if (c->limit && ++c->cnt > c->limit) |
| 1812 | auxtrace_cache__drop(c); |
| 1813 | |
| 1814 | entry->key = key; |
| 1815 | hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]); |
| 1816 | |
| 1817 | return 0; |
| 1818 | } |
| 1819 | |
Adrian Hunter | fd62c10 | 2019-10-25 15:59:59 +0300 | [diff] [blame] | 1820 | static struct auxtrace_cache_entry *auxtrace_cache__rm(struct auxtrace_cache *c, |
| 1821 | u32 key) |
| 1822 | { |
| 1823 | struct auxtrace_cache_entry *entry; |
| 1824 | struct hlist_head *hlist; |
| 1825 | struct hlist_node *n; |
| 1826 | |
| 1827 | if (!c) |
| 1828 | return NULL; |
| 1829 | |
| 1830 | hlist = &c->hashtable[hash_32(key, c->bits)]; |
| 1831 | hlist_for_each_entry_safe(entry, n, hlist, hash) { |
| 1832 | if (entry->key == key) { |
| 1833 | hlist_del(&entry->hash); |
| 1834 | return entry; |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | return NULL; |
| 1839 | } |
| 1840 | |
| 1841 | void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key) |
| 1842 | { |
| 1843 | struct auxtrace_cache_entry *entry = auxtrace_cache__rm(c, key); |
| 1844 | |
| 1845 | auxtrace_cache__free_entry(c, entry); |
| 1846 | } |
| 1847 | |
Adrian Hunter | c3278f0 | 2015-04-09 18:53:54 +0300 | [diff] [blame] | 1848 | void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key) |
| 1849 | { |
| 1850 | struct auxtrace_cache_entry *entry; |
| 1851 | struct hlist_head *hlist; |
| 1852 | |
| 1853 | if (!c) |
| 1854 | return NULL; |
| 1855 | |
| 1856 | hlist = &c->hashtable[hash_32(key, c->bits)]; |
| 1857 | hlist_for_each_entry(entry, hlist, hash) { |
| 1858 | if (entry->key == key) |
| 1859 | return entry; |
| 1860 | } |
| 1861 | |
| 1862 | return NULL; |
| 1863 | } |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 1864 | |
| 1865 | static void addr_filter__free_str(struct addr_filter *filt) |
| 1866 | { |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 1867 | zfree(&filt->str); |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 1868 | filt->action = NULL; |
| 1869 | filt->sym_from = NULL; |
| 1870 | filt->sym_to = NULL; |
| 1871 | filt->filename = NULL; |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | static struct addr_filter *addr_filter__new(void) |
| 1875 | { |
| 1876 | struct addr_filter *filt = zalloc(sizeof(*filt)); |
| 1877 | |
| 1878 | if (filt) |
| 1879 | INIT_LIST_HEAD(&filt->list); |
| 1880 | |
| 1881 | return filt; |
| 1882 | } |
| 1883 | |
| 1884 | static void addr_filter__free(struct addr_filter *filt) |
| 1885 | { |
| 1886 | if (filt) |
| 1887 | addr_filter__free_str(filt); |
| 1888 | free(filt); |
| 1889 | } |
| 1890 | |
| 1891 | static void addr_filters__add(struct addr_filters *filts, |
| 1892 | struct addr_filter *filt) |
| 1893 | { |
| 1894 | list_add_tail(&filt->list, &filts->head); |
| 1895 | filts->cnt += 1; |
| 1896 | } |
| 1897 | |
| 1898 | static void addr_filters__del(struct addr_filters *filts, |
| 1899 | struct addr_filter *filt) |
| 1900 | { |
| 1901 | list_del_init(&filt->list); |
| 1902 | filts->cnt -= 1; |
| 1903 | } |
| 1904 | |
| 1905 | void addr_filters__init(struct addr_filters *filts) |
| 1906 | { |
| 1907 | INIT_LIST_HEAD(&filts->head); |
| 1908 | filts->cnt = 0; |
| 1909 | } |
| 1910 | |
| 1911 | void addr_filters__exit(struct addr_filters *filts) |
| 1912 | { |
| 1913 | struct addr_filter *filt, *n; |
| 1914 | |
| 1915 | list_for_each_entry_safe(filt, n, &filts->head, list) { |
| 1916 | addr_filters__del(filts, filt); |
| 1917 | addr_filter__free(filt); |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | static int parse_num_or_str(char **inp, u64 *num, const char **str, |
| 1922 | const char *str_delim) |
| 1923 | { |
| 1924 | *inp += strspn(*inp, " "); |
| 1925 | |
| 1926 | if (isdigit(**inp)) { |
| 1927 | char *endptr; |
| 1928 | |
| 1929 | if (!num) |
| 1930 | return -EINVAL; |
| 1931 | errno = 0; |
| 1932 | *num = strtoull(*inp, &endptr, 0); |
| 1933 | if (errno) |
| 1934 | return -errno; |
| 1935 | if (endptr == *inp) |
| 1936 | return -EINVAL; |
| 1937 | *inp = endptr; |
| 1938 | } else { |
| 1939 | size_t n; |
| 1940 | |
| 1941 | if (!str) |
| 1942 | return -EINVAL; |
| 1943 | *inp += strspn(*inp, " "); |
| 1944 | *str = *inp; |
| 1945 | n = strcspn(*inp, str_delim); |
| 1946 | if (!n) |
| 1947 | return -EINVAL; |
| 1948 | *inp += n; |
| 1949 | if (**inp) { |
| 1950 | **inp = '\0'; |
| 1951 | *inp += 1; |
| 1952 | } |
| 1953 | } |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | static int parse_action(struct addr_filter *filt) |
| 1958 | { |
| 1959 | if (!strcmp(filt->action, "filter")) { |
| 1960 | filt->start = true; |
| 1961 | filt->range = true; |
| 1962 | } else if (!strcmp(filt->action, "start")) { |
| 1963 | filt->start = true; |
| 1964 | } else if (!strcmp(filt->action, "stop")) { |
| 1965 | filt->start = false; |
| 1966 | } else if (!strcmp(filt->action, "tracestop")) { |
| 1967 | filt->start = false; |
| 1968 | filt->range = true; |
| 1969 | filt->action += 5; /* Change 'tracestop' to 'stop' */ |
| 1970 | } else { |
| 1971 | return -EINVAL; |
| 1972 | } |
| 1973 | return 0; |
| 1974 | } |
| 1975 | |
| 1976 | static int parse_sym_idx(char **inp, int *idx) |
| 1977 | { |
| 1978 | *idx = -1; |
| 1979 | |
| 1980 | *inp += strspn(*inp, " "); |
| 1981 | |
| 1982 | if (**inp != '#') |
| 1983 | return 0; |
| 1984 | |
| 1985 | *inp += 1; |
| 1986 | |
| 1987 | if (**inp == 'g' || **inp == 'G') { |
| 1988 | *inp += 1; |
| 1989 | *idx = 0; |
| 1990 | } else { |
| 1991 | unsigned long num; |
| 1992 | char *endptr; |
| 1993 | |
| 1994 | errno = 0; |
| 1995 | num = strtoul(*inp, &endptr, 0); |
| 1996 | if (errno) |
| 1997 | return -errno; |
| 1998 | if (endptr == *inp || num > INT_MAX) |
| 1999 | return -EINVAL; |
| 2000 | *inp = endptr; |
| 2001 | *idx = num; |
| 2002 | } |
| 2003 | |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
| 2007 | static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx) |
| 2008 | { |
| 2009 | int err = parse_num_or_str(inp, num, str, " "); |
| 2010 | |
| 2011 | if (!err && *str) |
| 2012 | err = parse_sym_idx(inp, idx); |
| 2013 | |
| 2014 | return err; |
| 2015 | } |
| 2016 | |
| 2017 | static int parse_one_filter(struct addr_filter *filt, const char **filter_inp) |
| 2018 | { |
| 2019 | char *fstr; |
| 2020 | int err; |
| 2021 | |
| 2022 | filt->str = fstr = strdup(*filter_inp); |
| 2023 | if (!fstr) |
| 2024 | return -ENOMEM; |
| 2025 | |
| 2026 | err = parse_num_or_str(&fstr, NULL, &filt->action, " "); |
| 2027 | if (err) |
| 2028 | goto out_err; |
| 2029 | |
| 2030 | err = parse_action(filt); |
| 2031 | if (err) |
| 2032 | goto out_err; |
| 2033 | |
| 2034 | err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from, |
| 2035 | &filt->sym_from_idx); |
| 2036 | if (err) |
| 2037 | goto out_err; |
| 2038 | |
| 2039 | fstr += strspn(fstr, " "); |
| 2040 | |
| 2041 | if (*fstr == '/') { |
| 2042 | fstr += 1; |
| 2043 | err = parse_addr_size(&fstr, &filt->size, &filt->sym_to, |
| 2044 | &filt->sym_to_idx); |
| 2045 | if (err) |
| 2046 | goto out_err; |
| 2047 | filt->range = true; |
| 2048 | } |
| 2049 | |
| 2050 | fstr += strspn(fstr, " "); |
| 2051 | |
| 2052 | if (*fstr == '@') { |
| 2053 | fstr += 1; |
| 2054 | err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,"); |
| 2055 | if (err) |
| 2056 | goto out_err; |
| 2057 | } |
| 2058 | |
| 2059 | fstr += strspn(fstr, " ,"); |
| 2060 | |
| 2061 | *filter_inp += fstr - filt->str; |
| 2062 | |
| 2063 | return 0; |
| 2064 | |
| 2065 | out_err: |
| 2066 | addr_filter__free_str(filt); |
| 2067 | |
| 2068 | return err; |
| 2069 | } |
| 2070 | |
| 2071 | int addr_filters__parse_bare_filter(struct addr_filters *filts, |
| 2072 | const char *filter) |
| 2073 | { |
| 2074 | struct addr_filter *filt; |
| 2075 | const char *fstr = filter; |
| 2076 | int err; |
| 2077 | |
| 2078 | while (*fstr) { |
| 2079 | filt = addr_filter__new(); |
| 2080 | err = parse_one_filter(filt, &fstr); |
| 2081 | if (err) { |
| 2082 | addr_filter__free(filt); |
| 2083 | addr_filters__exit(filts); |
| 2084 | return err; |
| 2085 | } |
| 2086 | addr_filters__add(filts, filt); |
| 2087 | } |
| 2088 | |
| 2089 | return 0; |
| 2090 | } |
| 2091 | |
| 2092 | struct sym_args { |
| 2093 | const char *name; |
| 2094 | u64 start; |
| 2095 | u64 size; |
| 2096 | int idx; |
| 2097 | int cnt; |
| 2098 | bool started; |
| 2099 | bool global; |
| 2100 | bool selected; |
| 2101 | bool duplicate; |
| 2102 | bool near; |
| 2103 | }; |
| 2104 | |
| 2105 | static bool kern_sym_match(struct sym_args *args, const char *name, char type) |
| 2106 | { |
| 2107 | /* A function with the same name, and global or the n'th found or any */ |
Arnaldo Carvalho de Melo | e85e0e0 | 2018-04-25 17:16:31 -0300 | [diff] [blame] | 2108 | return kallsyms__is_function(type) && |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2109 | !strcmp(name, args->name) && |
| 2110 | ((args->global && isupper(type)) || |
| 2111 | (args->selected && ++(args->cnt) == args->idx) || |
| 2112 | (!args->global && !args->selected)); |
| 2113 | } |
| 2114 | |
| 2115 | static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start) |
| 2116 | { |
| 2117 | struct sym_args *args = arg; |
| 2118 | |
| 2119 | if (args->started) { |
| 2120 | if (!args->size) |
| 2121 | args->size = start - args->start; |
| 2122 | if (args->selected) { |
| 2123 | if (args->size) |
| 2124 | return 1; |
| 2125 | } else if (kern_sym_match(args, name, type)) { |
| 2126 | args->duplicate = true; |
| 2127 | return 1; |
| 2128 | } |
| 2129 | } else if (kern_sym_match(args, name, type)) { |
| 2130 | args->started = true; |
| 2131 | args->start = start; |
| 2132 | } |
| 2133 | |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
| 2137 | static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start) |
| 2138 | { |
| 2139 | struct sym_args *args = arg; |
| 2140 | |
| 2141 | if (kern_sym_match(args, name, type)) { |
| 2142 | pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n", |
| 2143 | ++args->cnt, start, type, name); |
| 2144 | args->near = true; |
| 2145 | } else if (args->near) { |
| 2146 | args->near = false; |
| 2147 | pr_err("\t\twhich is near\t\t%s\n", name); |
| 2148 | } |
| 2149 | |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | static int sym_not_found_error(const char *sym_name, int idx) |
| 2154 | { |
| 2155 | if (idx > 0) { |
| 2156 | pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n", |
| 2157 | idx, sym_name); |
| 2158 | } else if (!idx) { |
| 2159 | pr_err("Global symbol '%s' not found.\n", sym_name); |
| 2160 | } else { |
| 2161 | pr_err("Symbol '%s' not found.\n", sym_name); |
| 2162 | } |
| 2163 | pr_err("Note that symbols must be functions.\n"); |
| 2164 | |
| 2165 | return -EINVAL; |
| 2166 | } |
| 2167 | |
| 2168 | static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx) |
| 2169 | { |
| 2170 | struct sym_args args = { |
| 2171 | .name = sym_name, |
| 2172 | .idx = idx, |
| 2173 | .global = !idx, |
| 2174 | .selected = idx > 0, |
| 2175 | }; |
| 2176 | int err; |
| 2177 | |
| 2178 | *start = 0; |
| 2179 | *size = 0; |
| 2180 | |
| 2181 | err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb); |
| 2182 | if (err < 0) { |
| 2183 | pr_err("Failed to parse /proc/kallsyms\n"); |
| 2184 | return err; |
| 2185 | } |
| 2186 | |
| 2187 | if (args.duplicate) { |
| 2188 | pr_err("Multiple kernel symbols with name '%s'\n", sym_name); |
| 2189 | args.cnt = 0; |
| 2190 | kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb); |
| 2191 | pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n", |
| 2192 | sym_name); |
| 2193 | pr_err("Or select a global symbol by inserting #0 or #g or #G\n"); |
| 2194 | return -EINVAL; |
| 2195 | } |
| 2196 | |
| 2197 | if (!args.started) { |
| 2198 | pr_err("Kernel symbol lookup: "); |
| 2199 | return sym_not_found_error(sym_name, idx); |
| 2200 | } |
| 2201 | |
| 2202 | *start = args.start; |
| 2203 | *size = args.size; |
| 2204 | |
| 2205 | return 0; |
| 2206 | } |
| 2207 | |
| 2208 | static int find_entire_kern_cb(void *arg, const char *name __maybe_unused, |
| 2209 | char type, u64 start) |
| 2210 | { |
| 2211 | struct sym_args *args = arg; |
| 2212 | |
Arnaldo Carvalho de Melo | e85e0e0 | 2018-04-25 17:16:31 -0300 | [diff] [blame] | 2213 | if (!kallsyms__is_function(type)) |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2214 | return 0; |
| 2215 | |
| 2216 | if (!args->started) { |
| 2217 | args->started = true; |
| 2218 | args->start = start; |
| 2219 | } |
| 2220 | /* Don't know exactly where the kernel ends, so we add a page */ |
| 2221 | args->size = round_up(start, page_size) + page_size - args->start; |
| 2222 | |
| 2223 | return 0; |
| 2224 | } |
| 2225 | |
| 2226 | static int addr_filter__entire_kernel(struct addr_filter *filt) |
| 2227 | { |
| 2228 | struct sym_args args = { .started = false }; |
| 2229 | int err; |
| 2230 | |
| 2231 | err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb); |
| 2232 | if (err < 0 || !args.started) { |
| 2233 | pr_err("Failed to parse /proc/kallsyms\n"); |
| 2234 | return err; |
| 2235 | } |
| 2236 | |
| 2237 | filt->addr = args.start; |
| 2238 | filt->size = args.size; |
| 2239 | |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size) |
| 2244 | { |
| 2245 | if (start + size >= filt->addr) |
| 2246 | return 0; |
| 2247 | |
| 2248 | if (filt->sym_from) { |
| 2249 | pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n", |
| 2250 | filt->sym_to, start, filt->sym_from, filt->addr); |
| 2251 | } else { |
| 2252 | pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n", |
| 2253 | filt->sym_to, start, filt->addr); |
| 2254 | } |
| 2255 | |
| 2256 | return -EINVAL; |
| 2257 | } |
| 2258 | |
| 2259 | static int addr_filter__resolve_kernel_syms(struct addr_filter *filt) |
| 2260 | { |
| 2261 | bool no_size = false; |
| 2262 | u64 start, size; |
| 2263 | int err; |
| 2264 | |
| 2265 | if (symbol_conf.kptr_restrict) { |
| 2266 | pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n"); |
| 2267 | return -EINVAL; |
| 2268 | } |
| 2269 | |
| 2270 | if (filt->sym_from && !strcmp(filt->sym_from, "*")) |
| 2271 | return addr_filter__entire_kernel(filt); |
| 2272 | |
| 2273 | if (filt->sym_from) { |
| 2274 | err = find_kern_sym(filt->sym_from, &start, &size, |
| 2275 | filt->sym_from_idx); |
| 2276 | if (err) |
| 2277 | return err; |
| 2278 | filt->addr = start; |
| 2279 | if (filt->range && !filt->size && !filt->sym_to) { |
| 2280 | filt->size = size; |
Adrian Hunter | c3a0bbc | 2017-03-24 14:15:52 +0200 | [diff] [blame] | 2281 | no_size = !size; |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2282 | } |
| 2283 | } |
| 2284 | |
| 2285 | if (filt->sym_to) { |
| 2286 | err = find_kern_sym(filt->sym_to, &start, &size, |
| 2287 | filt->sym_to_idx); |
| 2288 | if (err) |
| 2289 | return err; |
| 2290 | |
| 2291 | err = check_end_after_start(filt, start, size); |
| 2292 | if (err) |
| 2293 | return err; |
| 2294 | filt->size = start + size - filt->addr; |
Adrian Hunter | c3a0bbc | 2017-03-24 14:15:52 +0200 | [diff] [blame] | 2295 | no_size = !size; |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | /* The very last symbol in kallsyms does not imply a particular size */ |
| 2299 | if (no_size) { |
| 2300 | pr_err("Cannot determine size of symbol '%s'\n", |
| 2301 | filt->sym_to ? filt->sym_to : filt->sym_from); |
| 2302 | return -EINVAL; |
| 2303 | } |
| 2304 | |
| 2305 | return 0; |
| 2306 | } |
| 2307 | |
| 2308 | static struct dso *load_dso(const char *name) |
| 2309 | { |
| 2310 | struct map *map; |
| 2311 | struct dso *dso; |
| 2312 | |
| 2313 | map = dso__new_map(name); |
| 2314 | if (!map) |
| 2315 | return NULL; |
| 2316 | |
Adrian Hunter | c1c4920 | 2019-03-01 14:29:02 +0200 | [diff] [blame] | 2317 | if (map__load(map) < 0) |
| 2318 | pr_err("File '%s' not found or has no symbols.\n", name); |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2319 | |
| 2320 | dso = dso__get(map->dso); |
| 2321 | |
| 2322 | map__put(map); |
| 2323 | |
| 2324 | return dso; |
| 2325 | } |
| 2326 | |
| 2327 | static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt, |
| 2328 | int idx) |
| 2329 | { |
| 2330 | /* Same name, and global or the n'th found or any */ |
| 2331 | return !arch__compare_symbol_names(name, sym->name) && |
| 2332 | ((!idx && sym->binding == STB_GLOBAL) || |
| 2333 | (idx > 0 && ++*cnt == idx) || |
| 2334 | idx < 0); |
| 2335 | } |
| 2336 | |
| 2337 | static void print_duplicate_syms(struct dso *dso, const char *sym_name) |
| 2338 | { |
| 2339 | struct symbol *sym; |
| 2340 | bool near = false; |
| 2341 | int cnt = 0; |
| 2342 | |
| 2343 | pr_err("Multiple symbols with name '%s'\n", sym_name); |
| 2344 | |
Arnaldo Carvalho de Melo | 5cf88a6 | 2018-04-25 17:01:46 -0300 | [diff] [blame] | 2345 | sym = dso__first_symbol(dso); |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2346 | while (sym) { |
| 2347 | if (dso_sym_match(sym, sym_name, &cnt, -1)) { |
| 2348 | pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n", |
| 2349 | ++cnt, sym->start, |
| 2350 | sym->binding == STB_GLOBAL ? 'g' : |
| 2351 | sym->binding == STB_LOCAL ? 'l' : 'w', |
| 2352 | sym->name); |
| 2353 | near = true; |
| 2354 | } else if (near) { |
| 2355 | near = false; |
| 2356 | pr_err("\t\twhich is near\t\t%s\n", sym->name); |
| 2357 | } |
| 2358 | sym = dso__next_symbol(sym); |
| 2359 | } |
| 2360 | |
| 2361 | pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n", |
| 2362 | sym_name); |
| 2363 | pr_err("Or select a global symbol by inserting #0 or #g or #G\n"); |
| 2364 | } |
| 2365 | |
| 2366 | static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start, |
| 2367 | u64 *size, int idx) |
| 2368 | { |
| 2369 | struct symbol *sym; |
| 2370 | int cnt = 0; |
| 2371 | |
| 2372 | *start = 0; |
| 2373 | *size = 0; |
| 2374 | |
Arnaldo Carvalho de Melo | 5cf88a6 | 2018-04-25 17:01:46 -0300 | [diff] [blame] | 2375 | sym = dso__first_symbol(dso); |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2376 | while (sym) { |
| 2377 | if (*start) { |
| 2378 | if (!*size) |
| 2379 | *size = sym->start - *start; |
| 2380 | if (idx > 0) { |
| 2381 | if (*size) |
| 2382 | return 1; |
| 2383 | } else if (dso_sym_match(sym, sym_name, &cnt, idx)) { |
| 2384 | print_duplicate_syms(dso, sym_name); |
| 2385 | return -EINVAL; |
| 2386 | } |
| 2387 | } else if (dso_sym_match(sym, sym_name, &cnt, idx)) { |
| 2388 | *start = sym->start; |
| 2389 | *size = sym->end - sym->start; |
| 2390 | } |
| 2391 | sym = dso__next_symbol(sym); |
| 2392 | } |
| 2393 | |
| 2394 | if (!*start) |
| 2395 | return sym_not_found_error(sym_name, idx); |
| 2396 | |
| 2397 | return 0; |
| 2398 | } |
| 2399 | |
| 2400 | static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso) |
| 2401 | { |
Adrian Hunter | 5717660 | 2018-11-27 10:46:34 +0200 | [diff] [blame] | 2402 | if (dso__data_file_size(dso, NULL)) { |
| 2403 | pr_err("Failed to determine filter for %s\nCannot determine file size.\n", |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2404 | filt->filename); |
| 2405 | return -EINVAL; |
| 2406 | } |
| 2407 | |
Adrian Hunter | 5717660 | 2018-11-27 10:46:34 +0200 | [diff] [blame] | 2408 | filt->addr = 0; |
| 2409 | filt->size = dso->data.file_size; |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2410 | |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
| 2414 | static int addr_filter__resolve_syms(struct addr_filter *filt) |
| 2415 | { |
| 2416 | u64 start, size; |
| 2417 | struct dso *dso; |
| 2418 | int err = 0; |
| 2419 | |
| 2420 | if (!filt->sym_from && !filt->sym_to) |
| 2421 | return 0; |
| 2422 | |
| 2423 | if (!filt->filename) |
| 2424 | return addr_filter__resolve_kernel_syms(filt); |
| 2425 | |
| 2426 | dso = load_dso(filt->filename); |
| 2427 | if (!dso) { |
| 2428 | pr_err("Failed to load symbols from: %s\n", filt->filename); |
| 2429 | return -EINVAL; |
| 2430 | } |
| 2431 | |
| 2432 | if (filt->sym_from && !strcmp(filt->sym_from, "*")) { |
| 2433 | err = addr_filter__entire_dso(filt, dso); |
| 2434 | goto put_dso; |
| 2435 | } |
| 2436 | |
| 2437 | if (filt->sym_from) { |
| 2438 | err = find_dso_sym(dso, filt->sym_from, &start, &size, |
| 2439 | filt->sym_from_idx); |
| 2440 | if (err) |
| 2441 | goto put_dso; |
| 2442 | filt->addr = start; |
| 2443 | if (filt->range && !filt->size && !filt->sym_to) |
| 2444 | filt->size = size; |
| 2445 | } |
| 2446 | |
| 2447 | if (filt->sym_to) { |
| 2448 | err = find_dso_sym(dso, filt->sym_to, &start, &size, |
| 2449 | filt->sym_to_idx); |
| 2450 | if (err) |
| 2451 | goto put_dso; |
| 2452 | |
| 2453 | err = check_end_after_start(filt, start, size); |
| 2454 | if (err) |
| 2455 | return err; |
| 2456 | |
| 2457 | filt->size = start + size - filt->addr; |
| 2458 | } |
| 2459 | |
| 2460 | put_dso: |
| 2461 | dso__put(dso); |
| 2462 | |
| 2463 | return err; |
| 2464 | } |
| 2465 | |
| 2466 | static char *addr_filter__to_str(struct addr_filter *filt) |
| 2467 | { |
| 2468 | char filename_buf[PATH_MAX]; |
| 2469 | const char *at = ""; |
| 2470 | const char *fn = ""; |
| 2471 | char *filter; |
| 2472 | int err; |
| 2473 | |
| 2474 | if (filt->filename) { |
| 2475 | at = "@"; |
| 2476 | fn = realpath(filt->filename, filename_buf); |
| 2477 | if (!fn) |
| 2478 | return NULL; |
| 2479 | } |
| 2480 | |
| 2481 | if (filt->range) { |
| 2482 | err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s", |
| 2483 | filt->action, filt->addr, filt->size, at, fn); |
| 2484 | } else { |
| 2485 | err = asprintf(&filter, "%s 0x%"PRIx64"%s%s", |
| 2486 | filt->action, filt->addr, at, fn); |
| 2487 | } |
| 2488 | |
| 2489 | return err < 0 ? NULL : filter; |
| 2490 | } |
| 2491 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2492 | static int parse_addr_filter(struct evsel *evsel, const char *filter, |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2493 | int max_nr) |
| 2494 | { |
| 2495 | struct addr_filters filts; |
| 2496 | struct addr_filter *filt; |
| 2497 | int err; |
| 2498 | |
| 2499 | addr_filters__init(&filts); |
| 2500 | |
| 2501 | err = addr_filters__parse_bare_filter(&filts, filter); |
| 2502 | if (err) |
| 2503 | goto out_exit; |
| 2504 | |
| 2505 | if (filts.cnt > max_nr) { |
| 2506 | pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n", |
| 2507 | filts.cnt, max_nr); |
| 2508 | err = -EINVAL; |
| 2509 | goto out_exit; |
| 2510 | } |
| 2511 | |
| 2512 | list_for_each_entry(filt, &filts.head, list) { |
| 2513 | char *new_filter; |
| 2514 | |
| 2515 | err = addr_filter__resolve_syms(filt); |
| 2516 | if (err) |
| 2517 | goto out_exit; |
| 2518 | |
| 2519 | new_filter = addr_filter__to_str(filt); |
| 2520 | if (!new_filter) { |
| 2521 | err = -ENOMEM; |
| 2522 | goto out_exit; |
| 2523 | } |
| 2524 | |
| 2525 | if (perf_evsel__append_addr_filter(evsel, new_filter)) { |
| 2526 | err = -ENOMEM; |
| 2527 | goto out_exit; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | out_exit: |
| 2532 | addr_filters__exit(&filts); |
| 2533 | |
| 2534 | if (err) { |
| 2535 | pr_err("Failed to parse address filter: '%s'\n", filter); |
| 2536 | pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n"); |
| 2537 | pr_err("Where multiple filters are separated by space or comma.\n"); |
| 2538 | } |
| 2539 | |
| 2540 | return err; |
| 2541 | } |
| 2542 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2543 | static int perf_evsel__nr_addr_filter(struct evsel *evsel) |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2544 | { |
Arnaldo Carvalho de Melo | e76026b | 2020-04-29 15:50:10 -0300 | [diff] [blame] | 2545 | struct perf_pmu *pmu = evsel__find_pmu(evsel); |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2546 | int nr_addr_filters = 0; |
| 2547 | |
| 2548 | if (!pmu) |
| 2549 | return 0; |
| 2550 | |
| 2551 | perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters); |
| 2552 | |
| 2553 | return nr_addr_filters; |
| 2554 | } |
| 2555 | |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2556 | int auxtrace_parse_filters(struct evlist *evlist) |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2557 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2558 | struct evsel *evsel; |
Adrian Hunter | 1b36c03 | 2016-09-23 17:38:39 +0300 | [diff] [blame] | 2559 | char *filter; |
| 2560 | int err, max_nr; |
| 2561 | |
| 2562 | evlist__for_each_entry(evlist, evsel) { |
| 2563 | filter = evsel->filter; |
| 2564 | max_nr = perf_evsel__nr_addr_filter(evsel); |
| 2565 | if (!filter || !max_nr) |
| 2566 | continue; |
| 2567 | evsel->filter = NULL; |
| 2568 | err = parse_addr_filter(evsel, filter, max_nr); |
| 2569 | free(filter); |
| 2570 | if (err) |
| 2571 | return err; |
| 2572 | pr_debug("Address filter: %s\n", evsel->filter); |
| 2573 | } |
| 2574 | |
| 2575 | return 0; |
| 2576 | } |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 2577 | |
| 2578 | int auxtrace__process_event(struct perf_session *session, union perf_event *event, |
| 2579 | struct perf_sample *sample, struct perf_tool *tool) |
| 2580 | { |
| 2581 | if (!session->auxtrace) |
| 2582 | return 0; |
| 2583 | |
| 2584 | return session->auxtrace->process_event(session, event, sample, tool); |
| 2585 | } |
| 2586 | |
Adrian Hunter | b04b8dd | 2019-11-15 14:42:19 +0200 | [diff] [blame] | 2587 | void auxtrace__dump_auxtrace_sample(struct perf_session *session, |
| 2588 | struct perf_sample *sample) |
| 2589 | { |
| 2590 | if (!session->auxtrace || !session->auxtrace->dump_auxtrace_sample || |
| 2591 | auxtrace__dont_decode(session)) |
| 2592 | return; |
| 2593 | |
| 2594 | session->auxtrace->dump_auxtrace_sample(session, sample); |
| 2595 | } |
| 2596 | |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 2597 | int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool) |
| 2598 | { |
| 2599 | if (!session->auxtrace) |
| 2600 | return 0; |
| 2601 | |
| 2602 | return session->auxtrace->flush_events(session, tool); |
| 2603 | } |
| 2604 | |
| 2605 | void auxtrace__free_events(struct perf_session *session) |
| 2606 | { |
| 2607 | if (!session->auxtrace) |
| 2608 | return; |
| 2609 | |
| 2610 | return session->auxtrace->free_events(session); |
| 2611 | } |
| 2612 | |
| 2613 | void auxtrace__free(struct perf_session *session) |
| 2614 | { |
| 2615 | if (!session->auxtrace) |
| 2616 | return; |
| 2617 | |
| 2618 | return session->auxtrace->free(session); |
| 2619 | } |
Adrian Hunter | 853f37d | 2020-04-01 13:15:58 +0300 | [diff] [blame] | 2620 | |
| 2621 | bool auxtrace__evsel_is_auxtrace(struct perf_session *session, |
| 2622 | struct evsel *evsel) |
| 2623 | { |
| 2624 | if (!session->auxtrace || !session->auxtrace->evsel_is_auxtrace) |
| 2625 | return false; |
| 2626 | |
| 2627 | return session->auxtrace->evsel_is_auxtrace(session, evsel); |
| 2628 | } |