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