blob: e028187c51fe8b51f2cb684771a279154818e87b [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Adrian Hunter718c6022015-04-09 18:53:42 +03002/*
3 * auxtrace.c: AUX area trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
Adrian Hunter718c6022015-04-09 18:53:42 +03005 */
6
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03007#include <inttypes.h>
Adrian Hunter718c6022015-04-09 18:53:42 +03008#include <sys/types.h>
9#include <sys/mman.h>
10#include <stdbool.h>
Adrian Hunter1b36c032016-09-23 17:38:39 +030011#include <string.h>
12#include <limits.h>
13#include <errno.h>
Adrian Hunter718c6022015-04-09 18:53:42 +030014
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 Huntere5027892015-04-21 12:21:51 +030020#include <linux/string.h>
Adrian Hunter16bd4322019-02-06 12:39:47 +020021#include <linux/time64.h>
Adrian Hunter718c6022015-04-09 18:53:42 +030022
Adrian Huntere5027892015-04-21 12:21:51 +030023#include <sys/param.h>
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030024#include <stdlib.h>
Adrian Hunter85ed4722015-04-09 18:53:50 +030025#include <stdio.h>
Adrian Huntere5027892015-04-21 12:21:51 +030026#include <linux/list.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030027#include <linux/zalloc.h>
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030028
Adrian Hunter718c6022015-04-09 18:53:42 +030029#include "evlist.h"
Adrian Hunter1b36c032016-09-23 17:38:39 +030030#include "dso.h"
31#include "map.h"
32#include "pmu.h"
33#include "evsel.h"
Adrian Huntereb7a52d2019-11-15 14:42:17 +020034#include "evsel_config.h"
Arnaldo Carvalho de Melo7cadca82019-01-27 14:10:31 +010035#include "symbol.h"
Arnaldo Carvalho de Melo40c7d242020-05-05 11:49:08 -030036#include "util/perf_api_probe.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030037#include "util/synthetic-events.h"
Adrian Hunter718c6022015-04-09 18:53:42 +030038#include "thread_map.h"
39#include "asm/bug.h"
40#include "auxtrace.h"
41
Adrian Hunterc3278f02015-04-09 18:53:54 +030042#include <linux/hash.h>
43
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030044#include "event.h"
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -030045#include "record.h"
Adrian Hunter85ed4722015-04-09 18:53:50 +030046#include "session.h"
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030047#include "debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +030049
Mathieu Poirier440a23b2018-01-17 10:52:11 -070050#include "cs-etm.h"
Adrian Hunter5efb1d52015-07-17 19:33:42 +030051#include "intel-pt.h"
Adrian Hunterd0170af2015-07-17 19:33:43 +030052#include "intel-bts.h"
Kim Phillipsffd3d182018-01-14 13:28:50 -060053#include "arm-spe.h"
Thomas Richterb96e6612018-08-02 09:46:20 +020054#include "s390-cpumsf.h"
Arnaldo Carvalho de Meloe0fcfb02019-09-23 12:20:38 -030055#include "util/mmap.h"
Adrian Hunter5efb1d52015-07-17 19:33:42 +030056
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030057#include <linux/ctype.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030058#include "symbol/kallsyms.h"
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030059#include <internal/lib.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030060
Adrian Huntereb7a52d2019-11-15 14:42:17 +020061/*
62 * Make a group from 'leader' to 'last', requiring that the events were not
63 * already grouped to a different leader.
64 */
65static int perf_evlist__regroup(struct evlist *evlist,
66 struct evsel *leader,
67 struct evsel *last)
68{
69 struct evsel *evsel;
70 bool grp;
71
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -030072 if (!evsel__is_group_leader(leader))
Adrian Huntereb7a52d2019-11-15 14:42:17 +020073 return -EINVAL;
74
75 grp = false;
76 evlist__for_each_entry(evlist, evsel) {
77 if (grp) {
78 if (!(evsel->leader == leader ||
79 (evsel->leader == evsel &&
80 evsel->core.nr_members <= 1)))
81 return -EINVAL;
82 } else if (evsel == leader) {
83 grp = true;
84 }
85 if (evsel == last)
86 break;
87 }
88
89 grp = false;
90 evlist__for_each_entry(evlist, evsel) {
91 if (grp) {
92 if (evsel->leader != leader) {
93 evsel->leader = leader;
94 if (leader->core.nr_members < 1)
95 leader->core.nr_members = 1;
96 leader->core.nr_members += 1;
97 }
98 } else if (evsel == leader) {
99 grp = true;
100 }
101 if (evsel == last)
102 break;
103 }
104
105 return 0;
106}
107
Adrian Hunter2e2967f2018-03-06 11:13:13 +0200108static bool auxtrace__dont_decode(struct perf_session *session)
109{
110 return !session->itrace_synth_opts ||
111 session->itrace_synth_opts->dont_decode;
112}
113
Adrian Hunter718c6022015-04-09 18:53:42 +0300114int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
115 struct auxtrace_mmap_params *mp,
116 void *userpg, int fd)
117{
118 struct perf_event_mmap_page *pc = userpg;
119
Adrian Hunter718c6022015-04-09 18:53:42 +0300120 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
121
122 mm->userpg = userpg;
123 mm->mask = mp->mask;
124 mm->len = mp->len;
125 mm->prev = 0;
126 mm->idx = mp->idx;
127 mm->tid = mp->tid;
128 mm->cpu = mp->cpu;
129
130 if (!mp->len) {
131 mm->base = NULL;
132 return 0;
133 }
134
Adrian Huntera7fde092015-07-14 15:32:41 +0300135#if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
136 pr_err("Cannot use AUX area tracing mmaps\n");
137 return -1;
138#endif
139
Adrian Hunter718c6022015-04-09 18:53:42 +0300140 pc->aux_offset = mp->offset;
141 pc->aux_size = mp->len;
142
143 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
144 if (mm->base == MAP_FAILED) {
145 pr_debug2("failed to mmap AUX area\n");
146 mm->base = NULL;
147 return -1;
148 }
149
150 return 0;
151}
152
153void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
154{
155 if (mm->base) {
156 munmap(mm->base, mm->len);
157 mm->base = NULL;
158 }
159}
160
161void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
162 off_t auxtrace_offset,
163 unsigned int auxtrace_pages,
164 bool auxtrace_overwrite)
165{
166 if (auxtrace_pages) {
167 mp->offset = auxtrace_offset;
168 mp->len = auxtrace_pages * (size_t)page_size;
169 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
170 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
171 pr_debug2("AUX area mmap length %zu\n", mp->len);
172 } else {
173 mp->len = 0;
174 }
175}
176
177void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
Jiri Olsa63503db2019-07-21 13:23:52 +0200178 struct evlist *evlist, int idx,
Adrian Hunter718c6022015-04-09 18:53:42 +0300179 bool per_cpu)
180{
181 mp->idx = idx;
182
183 if (per_cpu) {
Jiri Olsaf72f9012019-07-21 13:24:41 +0200184 mp->cpu = evlist->core.cpus->map[idx];
Jiri Olsa03617c22019-07-21 13:24:42 +0200185 if (evlist->core.threads)
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200186 mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
Adrian Hunter718c6022015-04-09 18:53:42 +0300187 else
188 mp->tid = -1;
189 } else {
190 mp->cpu = -1;
Jiri Olsaa2f354e2019-08-22 13:11:41 +0200191 mp->tid = perf_thread_map__pid(evlist->core.threads, idx);
Adrian Hunter718c6022015-04-09 18:53:42 +0300192 }
193}
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300194
Adrian Huntere5027892015-04-21 12:21:51 +0300195#define AUXTRACE_INIT_NR_QUEUES 32
196
197static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
198{
199 struct auxtrace_queue *queue_array;
200 unsigned int max_nr_queues, i;
201
202 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
203 if (nr_queues > max_nr_queues)
204 return NULL;
205
206 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
207 if (!queue_array)
208 return NULL;
209
210 for (i = 0; i < nr_queues; i++) {
211 INIT_LIST_HEAD(&queue_array[i].head);
212 queue_array[i].priv = NULL;
213 }
214
215 return queue_array;
216}
217
218int auxtrace_queues__init(struct auxtrace_queues *queues)
219{
220 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
221 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
222 if (!queues->queue_array)
223 return -ENOMEM;
224 return 0;
225}
226
227static int auxtrace_queues__grow(struct auxtrace_queues *queues,
228 unsigned int new_nr_queues)
229{
230 unsigned int nr_queues = queues->nr_queues;
231 struct auxtrace_queue *queue_array;
232 unsigned int i;
233
234 if (!nr_queues)
235 nr_queues = AUXTRACE_INIT_NR_QUEUES;
236
237 while (nr_queues && nr_queues < new_nr_queues)
238 nr_queues <<= 1;
239
240 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
241 return -EINVAL;
242
243 queue_array = auxtrace_alloc_queue_array(nr_queues);
244 if (!queue_array)
245 return -ENOMEM;
246
247 for (i = 0; i < queues->nr_queues; i++) {
248 list_splice_tail(&queues->queue_array[i].head,
249 &queue_array[i].head);
Adrian Hunter99cbbe52018-08-14 11:46:08 +0300250 queue_array[i].tid = queues->queue_array[i].tid;
251 queue_array[i].cpu = queues->queue_array[i].cpu;
252 queue_array[i].set = queues->queue_array[i].set;
Adrian Huntere5027892015-04-21 12:21:51 +0300253 queue_array[i].priv = queues->queue_array[i].priv;
254 }
255
256 queues->nr_queues = nr_queues;
257 queues->queue_array = queue_array;
258
259 return 0;
260}
261
262static void *auxtrace_copy_data(u64 size, struct perf_session *session)
263{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100264 int fd = perf_data__fd(session->data);
Adrian Huntere5027892015-04-21 12:21:51 +0300265 void *p;
266 ssize_t ret;
267
268 if (size > SSIZE_MAX)
269 return NULL;
270
271 p = malloc(size);
272 if (!p)
273 return NULL;
274
275 ret = readn(fd, p, size);
276 if (ret != (ssize_t)size) {
277 free(p);
278 return NULL;
279 }
280
281 return p;
282}
283
Adrian Huntera356a592018-03-06 11:13:15 +0200284static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues,
285 unsigned int idx,
286 struct auxtrace_buffer *buffer)
Adrian Huntere5027892015-04-21 12:21:51 +0300287{
288 struct auxtrace_queue *queue;
289 int err;
290
291 if (idx >= queues->nr_queues) {
292 err = auxtrace_queues__grow(queues, idx + 1);
293 if (err)
294 return err;
295 }
296
297 queue = &queues->queue_array[idx];
298
299 if (!queue->set) {
300 queue->set = true;
301 queue->tid = buffer->tid;
302 queue->cpu = buffer->cpu;
303 } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
304 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
305 queue->cpu, queue->tid, buffer->cpu, buffer->tid);
306 return -EINVAL;
307 }
308
309 buffer->buffer_nr = queues->next_buffer_nr++;
310
311 list_add_tail(&buffer->list, &queue->head);
312
313 queues->new_data = true;
314 queues->populated = true;
315
316 return 0;
317}
318
319/* Limit buffers to 32MiB on 32-bit */
320#define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
321
322static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
323 unsigned int idx,
324 struct auxtrace_buffer *buffer)
325{
326 u64 sz = buffer->size;
327 bool consecutive = false;
328 struct auxtrace_buffer *b;
329 int err;
330
331 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
332 b = memdup(buffer, sizeof(struct auxtrace_buffer));
333 if (!b)
334 return -ENOMEM;
335 b->size = BUFFER_LIMIT_FOR_32_BIT;
336 b->consecutive = consecutive;
Adrian Huntera356a592018-03-06 11:13:15 +0200337 err = auxtrace_queues__queue_buffer(queues, idx, b);
Adrian Huntere5027892015-04-21 12:21:51 +0300338 if (err) {
339 auxtrace_buffer__free(b);
340 return err;
341 }
342 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
343 sz -= BUFFER_LIMIT_FOR_32_BIT;
344 consecutive = true;
345 }
346
347 buffer->size = sz;
348 buffer->consecutive = consecutive;
349
350 return 0;
351}
352
Adrian Hunterb238db62018-03-06 11:13:18 +0200353static bool filter_cpu(struct perf_session *session, int cpu)
354{
355 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;
356
357 return cpu_bitmap && cpu != -1 && !test_bit(cpu, cpu_bitmap);
358}
359
Adrian Huntera356a592018-03-06 11:13:15 +0200360static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
361 struct perf_session *session,
362 unsigned int idx,
Adrian Hunter4c454842018-03-06 11:13:16 +0200363 struct auxtrace_buffer *buffer,
364 struct auxtrace_buffer **buffer_ptr)
Adrian Huntere5027892015-04-21 12:21:51 +0300365{
Adrian Hunter0d75f122018-03-06 11:13:17 +0200366 int err = -ENOMEM;
367
Adrian Hunterb238db62018-03-06 11:13:18 +0200368 if (filter_cpu(session, buffer->cpu))
369 return 0;
370
Adrian Hunter0d75f122018-03-06 11:13:17 +0200371 buffer = memdup(buffer, sizeof(*buffer));
372 if (!buffer)
373 return -ENOMEM;
Adrian Hunter4c454842018-03-06 11:13:16 +0200374
Adrian Huntere5027892015-04-21 12:21:51 +0300375 if (session->one_mmap) {
376 buffer->data = buffer->data_offset - session->one_mmap_offset +
377 session->one_mmap_addr;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100378 } else if (perf_data__is_pipe(session->data)) {
Adrian Huntere5027892015-04-21 12:21:51 +0300379 buffer->data = auxtrace_copy_data(buffer->size, session);
380 if (!buffer->data)
Adrian Hunter0d75f122018-03-06 11:13:17 +0200381 goto out_free;
Adrian Huntere5027892015-04-21 12:21:51 +0300382 buffer->data_needs_freeing = true;
383 } else if (BITS_PER_LONG == 32 &&
384 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
Adrian Huntere5027892015-04-21 12:21:51 +0300385 err = auxtrace_queues__split_buffer(queues, idx, buffer);
386 if (err)
Adrian Hunter0d75f122018-03-06 11:13:17 +0200387 goto out_free;
Adrian Huntere5027892015-04-21 12:21:51 +0300388 }
389
Adrian Hunter4c454842018-03-06 11:13:16 +0200390 err = auxtrace_queues__queue_buffer(queues, idx, buffer);
391 if (err)
Adrian Hunter0d75f122018-03-06 11:13:17 +0200392 goto out_free;
Adrian Hunter4c454842018-03-06 11:13:16 +0200393
394 /* FIXME: Doesn't work for split buffer */
395 if (buffer_ptr)
396 *buffer_ptr = buffer;
397
398 return 0;
Adrian Hunter0d75f122018-03-06 11:13:17 +0200399
400out_free:
401 auxtrace_buffer__free(buffer);
402 return err;
Adrian Huntere5027892015-04-21 12:21:51 +0300403}
404
405int auxtrace_queues__add_event(struct auxtrace_queues *queues,
406 struct perf_session *session,
407 union perf_event *event, off_t data_offset,
408 struct auxtrace_buffer **buffer_ptr)
409{
Adrian Hunter0d75f122018-03-06 11:13:17 +0200410 struct auxtrace_buffer buffer = {
411 .pid = -1,
412 .tid = event->auxtrace.tid,
413 .cpu = event->auxtrace.cpu,
414 .data_offset = data_offset,
415 .offset = event->auxtrace.offset,
416 .reference = event->auxtrace.reference,
417 .size = event->auxtrace.size,
418 };
419 unsigned int idx = event->auxtrace.idx;
Adrian Huntere5027892015-04-21 12:21:51 +0300420
Adrian Hunter0d75f122018-03-06 11:13:17 +0200421 return auxtrace_queues__add_buffer(queues, session, idx, &buffer,
422 buffer_ptr);
Adrian Huntere5027892015-04-21 12:21:51 +0300423}
424
Adrian Hunter99fa2982015-04-30 17:37:25 +0300425static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
426 struct perf_session *session,
427 off_t file_offset, size_t sz)
428{
429 union perf_event *event;
430 int err;
431 char buf[PERF_SAMPLE_MAX_SIZE];
432
433 err = perf_session__peek_event(session, file_offset, buf,
434 PERF_SAMPLE_MAX_SIZE, &event, NULL);
435 if (err)
436 return err;
437
438 if (event->header.type == PERF_RECORD_AUXTRACE) {
Jiri Olsa72932372019-08-28 15:57:16 +0200439 if (event->header.size < sizeof(struct perf_record_auxtrace) ||
Adrian Hunter99fa2982015-04-30 17:37:25 +0300440 event->header.size != sz) {
441 err = -EINVAL;
442 goto out;
443 }
444 file_offset += event->header.size;
445 err = auxtrace_queues__add_event(queues, session, event,
446 file_offset, NULL);
447 }
448out:
449 return err;
450}
451
Adrian Huntere5027892015-04-21 12:21:51 +0300452void auxtrace_queues__free(struct auxtrace_queues *queues)
453{
454 unsigned int i;
455
456 for (i = 0; i < queues->nr_queues; i++) {
457 while (!list_empty(&queues->queue_array[i].head)) {
458 struct auxtrace_buffer *buffer;
459
460 buffer = list_entry(queues->queue_array[i].head.next,
461 struct auxtrace_buffer, list);
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300462 list_del_init(&buffer->list);
Adrian Huntere5027892015-04-21 12:21:51 +0300463 auxtrace_buffer__free(buffer);
464 }
465 }
466
467 zfree(&queues->queue_array);
468 queues->nr_queues = 0;
469}
470
Adrian Hunterf9397152015-04-09 18:53:52 +0300471static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
472 unsigned int pos, unsigned int queue_nr,
473 u64 ordinal)
474{
475 unsigned int parent;
476
477 while (pos) {
478 parent = (pos - 1) >> 1;
479 if (heap_array[parent].ordinal <= ordinal)
480 break;
481 heap_array[pos] = heap_array[parent];
482 pos = parent;
483 }
484 heap_array[pos].queue_nr = queue_nr;
485 heap_array[pos].ordinal = ordinal;
486}
487
488int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
489 u64 ordinal)
490{
491 struct auxtrace_heap_item *heap_array;
492
493 if (queue_nr >= heap->heap_sz) {
494 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
495
496 while (heap_sz <= queue_nr)
497 heap_sz <<= 1;
498 heap_array = realloc(heap->heap_array,
499 heap_sz * sizeof(struct auxtrace_heap_item));
500 if (!heap_array)
501 return -ENOMEM;
502 heap->heap_array = heap_array;
503 heap->heap_sz = heap_sz;
504 }
505
506 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
507
508 return 0;
509}
510
511void auxtrace_heap__free(struct auxtrace_heap *heap)
512{
513 zfree(&heap->heap_array);
514 heap->heap_cnt = 0;
515 heap->heap_sz = 0;
516}
517
518void auxtrace_heap__pop(struct auxtrace_heap *heap)
519{
520 unsigned int pos, last, heap_cnt = heap->heap_cnt;
521 struct auxtrace_heap_item *heap_array;
522
523 if (!heap_cnt)
524 return;
525
526 heap->heap_cnt -= 1;
527
528 heap_array = heap->heap_array;
529
530 pos = 0;
531 while (1) {
532 unsigned int left, right;
533
534 left = (pos << 1) + 1;
535 if (left >= heap_cnt)
536 break;
537 right = left + 1;
538 if (right >= heap_cnt) {
539 heap_array[pos] = heap_array[left];
540 return;
541 }
542 if (heap_array[left].ordinal < heap_array[right].ordinal) {
543 heap_array[pos] = heap_array[left];
544 pos = left;
545 } else {
546 heap_array[pos] = heap_array[right];
547 pos = right;
548 }
549 }
550
551 last = heap_cnt - 1;
552 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
553 heap_array[last].ordinal);
554}
555
Mathieu Poirier14a05e12016-01-14 14:46:15 -0700556size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
Jiri Olsa63503db2019-07-21 13:23:52 +0200557 struct evlist *evlist)
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300558{
559 if (itr)
Mathieu Poirier14a05e12016-01-14 14:46:15 -0700560 return itr->info_priv_size(itr, evlist);
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300561 return 0;
562}
563
564static int auxtrace_not_supported(void)
565{
566 pr_err("AUX area tracing is not supported on this architecture\n");
567 return -EINVAL;
568}
569
570int auxtrace_record__info_fill(struct auxtrace_record *itr,
571 struct perf_session *session,
Jiri Olsa72932372019-08-28 15:57:16 +0200572 struct perf_record_auxtrace_info *auxtrace_info,
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300573 size_t priv_size)
574{
575 if (itr)
576 return itr->info_fill(itr, session, auxtrace_info, priv_size);
577 return auxtrace_not_supported();
578}
579
580void auxtrace_record__free(struct auxtrace_record *itr)
581{
582 if (itr)
583 itr->free(itr);
584}
585
Adrian Hunterd20031b2015-04-30 17:37:31 +0300586int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
587{
588 if (itr && itr->snapshot_start)
589 return itr->snapshot_start(itr);
590 return 0;
591}
592
Alexander Shishkince7b0e42019-08-06 17:41:01 +0300593int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit)
Adrian Hunterd20031b2015-04-30 17:37:31 +0300594{
Alexander Shishkince7b0e42019-08-06 17:41:01 +0300595 if (!on_exit && itr && itr->snapshot_finish)
Adrian Hunterd20031b2015-04-30 17:37:31 +0300596 return itr->snapshot_finish(itr);
597 return 0;
598}
599
600int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
601 struct auxtrace_mmap *mm,
602 unsigned char *data, u64 *head, u64 *old)
603{
604 if (itr && itr->find_snapshot)
605 return itr->find_snapshot(itr, idx, mm, data, head, old);
606 return 0;
607}
608
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300609int auxtrace_record__options(struct auxtrace_record *itr,
Jiri Olsa63503db2019-07-21 13:23:52 +0200610 struct evlist *evlist,
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300611 struct record_opts *opts)
612{
Adrian Hunterad60ba02020-02-17 10:23:00 +0200613 if (itr) {
614 itr->evlist = evlist;
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300615 return itr->recording_options(itr, evlist, opts);
Adrian Hunterad60ba02020-02-17 10:23:00 +0200616 }
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300617 return 0;
618}
619
620u64 auxtrace_record__reference(struct auxtrace_record *itr)
621{
622 if (itr)
623 return itr->reference(itr);
624 return 0;
625}
626
Adrian Hunterd20031b2015-04-30 17:37:31 +0300627int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
628 struct record_opts *opts, const char *str)
629{
630 if (!str)
631 return 0;
632
Alexander Shishkince7b0e42019-08-06 17:41:01 +0300633 /* PMU-agnostic options */
634 switch (*str) {
635 case 'e':
636 opts->auxtrace_snapshot_on_exit = true;
637 str++;
638 break;
639 default:
640 break;
641 }
642
Adrian Hunterd20031b2015-04-30 17:37:31 +0300643 if (itr)
644 return itr->parse_snapshot_options(itr, opts, str);
645
646 pr_err("No AUX area tracing to snapshot\n");
647 return -EINVAL;
648}
649
Adrian Hunterad60ba02020-02-17 10:23:00 +0200650int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
651{
652 struct evsel *evsel;
653
654 if (!itr->evlist || !itr->pmu)
655 return -EINVAL;
656
657 evlist__for_each_entry(itr->evlist, evsel) {
658 if (evsel->core.attr.type == itr->pmu->type) {
659 if (evsel->disabled)
660 return 0;
661 return perf_evlist__enable_event_idx(itr->evlist, evsel,
662 idx);
663 }
664 }
665 return -EINVAL;
666}
667
Adrian Hunterf0bb7ee2019-11-15 14:42:15 +0200668/*
669 * Event record size is 16-bit which results in a maximum size of about 64KiB.
670 * Allow about 4KiB for the rest of the sample record, to give a maximum
671 * AUX area sample size of 60KiB.
672 */
673#define MAX_AUX_SAMPLE_SIZE (60 * 1024)
674
675/* Arbitrary default size if no other default provided */
676#define DEFAULT_AUX_SAMPLE_SIZE (4 * 1024)
677
678static int auxtrace_validate_aux_sample_size(struct evlist *evlist,
679 struct record_opts *opts)
680{
681 struct evsel *evsel;
682 bool has_aux_leader = false;
683 u32 sz;
684
685 evlist__for_each_entry(evlist, evsel) {
686 sz = evsel->core.attr.aux_sample_size;
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300687 if (evsel__is_group_leader(evsel)) {
Arnaldo Carvalho de Melo39453ed2020-04-29 15:51:38 -0300688 has_aux_leader = evsel__is_aux_event(evsel);
Adrian Hunterf0bb7ee2019-11-15 14:42:15 +0200689 if (sz) {
690 if (has_aux_leader)
691 pr_err("Cannot add AUX area sampling to an AUX area event\n");
692 else
693 pr_err("Cannot add AUX area sampling to a group leader\n");
694 return -EINVAL;
695 }
696 }
697 if (sz > MAX_AUX_SAMPLE_SIZE) {
698 pr_err("AUX area sample size %u too big, max. %d\n",
699 sz, MAX_AUX_SAMPLE_SIZE);
700 return -EINVAL;
701 }
702 if (sz) {
703 if (!has_aux_leader) {
704 pr_err("Cannot add AUX area sampling because group leader is not an AUX area event\n");
705 return -EINVAL;
706 }
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300707 evsel__set_sample_bit(evsel, AUX);
Adrian Hunterf0bb7ee2019-11-15 14:42:15 +0200708 opts->auxtrace_sample_mode = true;
709 } else {
Arnaldo Carvalho de Melo862b2f82020-04-29 16:12:15 -0300710 evsel__reset_sample_bit(evsel, AUX);
Adrian Hunterf0bb7ee2019-11-15 14:42:15 +0200711 }
712 }
713
714 if (!opts->auxtrace_sample_mode) {
715 pr_err("AUX area sampling requires an AUX area event group leader plus other events to which to add samples\n");
716 return -EINVAL;
717 }
718
719 if (!perf_can_aux_sample()) {
720 pr_err("AUX area sampling is not supported by kernel\n");
721 return -EINVAL;
722 }
723
724 return 0;
725}
726
727int auxtrace_parse_sample_options(struct auxtrace_record *itr,
728 struct evlist *evlist,
729 struct record_opts *opts, const char *str)
730{
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300731 struct evsel_config_term *term;
Adrian Huntereb7a52d2019-11-15 14:42:17 +0200732 struct evsel *aux_evsel;
733 bool has_aux_sample_size = false;
Adrian Hunterf0bb7ee2019-11-15 14:42:15 +0200734 bool has_aux_leader = false;
735 struct evsel *evsel;
736 char *endptr;
737 unsigned long sz;
738
739 if (!str)
Adrian Huntereb7a52d2019-11-15 14:42:17 +0200740 goto no_opt;
Adrian Hunterf0bb7ee2019-11-15 14:42:15 +0200741
742 if (!itr) {
743 pr_err("No AUX area event to sample\n");
744 return -EINVAL;
745 }
746
747 sz = strtoul(str, &endptr, 0);
748 if (*endptr || sz > UINT_MAX) {
749 pr_err("Bad AUX area sampling option: '%s'\n", str);
750 return -EINVAL;
751 }
752
753 if (!sz)
754 sz = itr->default_aux_sample_size;
755
756 if (!sz)
757 sz = DEFAULT_AUX_SAMPLE_SIZE;
758
759 /* Set aux_sample_size based on --aux-sample option */
760 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300761 if (evsel__is_group_leader(evsel)) {
Arnaldo Carvalho de Melo39453ed2020-04-29 15:51:38 -0300762 has_aux_leader = evsel__is_aux_event(evsel);
Adrian Hunterf0bb7ee2019-11-15 14:42:15 +0200763 } else if (has_aux_leader) {
764 evsel->core.attr.aux_sample_size = sz;
765 }
766 }
Adrian Huntereb7a52d2019-11-15 14:42:17 +0200767no_opt:
768 aux_evsel = NULL;
769 /* Override with aux_sample_size from config term */
770 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo39453ed2020-04-29 15:51:38 -0300771 if (evsel__is_aux_event(evsel))
Adrian Huntereb7a52d2019-11-15 14:42:17 +0200772 aux_evsel = evsel;
Arnaldo Carvalho de Melo35ac0ca2020-05-06 13:05:08 -0300773 term = evsel__get_config_term(evsel, AUX_SAMPLE_SIZE);
Adrian Huntereb7a52d2019-11-15 14:42:17 +0200774 if (term) {
775 has_aux_sample_size = true;
776 evsel->core.attr.aux_sample_size = term->val.aux_sample_size;
777 /* If possible, group with the AUX event */
778 if (aux_evsel && evsel->core.attr.aux_sample_size)
779 perf_evlist__regroup(evlist, aux_evsel, evsel);
780 }
781 }
782
783 if (!str && !has_aux_sample_size)
784 return 0;
785
786 if (!itr) {
787 pr_err("No AUX area event to sample\n");
788 return -EINVAL;
789 }
Adrian Hunterf0bb7ee2019-11-15 14:42:15 +0200790
791 return auxtrace_validate_aux_sample_size(evlist, opts);
792}
793
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300794struct auxtrace_record *__weak
Jiri Olsa63503db2019-07-21 13:23:52 +0200795auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err)
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +0300796{
797 *err = 0;
798 return NULL;
799}
800
Adrian Hunter99fa2982015-04-30 17:37:25 +0300801static int auxtrace_index__alloc(struct list_head *head)
802{
803 struct auxtrace_index *auxtrace_index;
804
805 auxtrace_index = malloc(sizeof(struct auxtrace_index));
806 if (!auxtrace_index)
807 return -ENOMEM;
808
809 auxtrace_index->nr = 0;
810 INIT_LIST_HEAD(&auxtrace_index->list);
811
812 list_add_tail(&auxtrace_index->list, head);
813
814 return 0;
815}
816
817void auxtrace_index__free(struct list_head *head)
818{
819 struct auxtrace_index *auxtrace_index, *n;
820
821 list_for_each_entry_safe(auxtrace_index, n, head, list) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300822 list_del_init(&auxtrace_index->list);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300823 free(auxtrace_index);
824 }
825}
826
827static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
828{
829 struct auxtrace_index *auxtrace_index;
830 int err;
831
832 if (list_empty(head)) {
833 err = auxtrace_index__alloc(head);
834 if (err)
835 return NULL;
836 }
837
838 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
839
840 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
841 err = auxtrace_index__alloc(head);
842 if (err)
843 return NULL;
844 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
845 list);
846 }
847
848 return auxtrace_index;
849}
850
851int auxtrace_index__auxtrace_event(struct list_head *head,
852 union perf_event *event, off_t file_offset)
853{
854 struct auxtrace_index *auxtrace_index;
855 size_t nr;
856
857 auxtrace_index = auxtrace_index__last(head);
858 if (!auxtrace_index)
859 return -ENOMEM;
860
861 nr = auxtrace_index->nr;
862 auxtrace_index->entries[nr].file_offset = file_offset;
863 auxtrace_index->entries[nr].sz = event->header.size;
864 auxtrace_index->nr += 1;
865
866 return 0;
867}
868
869static int auxtrace_index__do_write(int fd,
870 struct auxtrace_index *auxtrace_index)
871{
872 struct auxtrace_index_entry ent;
873 size_t i;
874
875 for (i = 0; i < auxtrace_index->nr; i++) {
876 ent.file_offset = auxtrace_index->entries[i].file_offset;
877 ent.sz = auxtrace_index->entries[i].sz;
878 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
879 return -errno;
880 }
881 return 0;
882}
883
884int auxtrace_index__write(int fd, struct list_head *head)
885{
886 struct auxtrace_index *auxtrace_index;
887 u64 total = 0;
888 int err;
889
890 list_for_each_entry(auxtrace_index, head, list)
891 total += auxtrace_index->nr;
892
893 if (writen(fd, &total, sizeof(total)) != sizeof(total))
894 return -errno;
895
896 list_for_each_entry(auxtrace_index, head, list) {
897 err = auxtrace_index__do_write(fd, auxtrace_index);
898 if (err)
899 return err;
900 }
901
902 return 0;
903}
904
905static int auxtrace_index__process_entry(int fd, struct list_head *head,
906 bool needs_swap)
907{
908 struct auxtrace_index *auxtrace_index;
909 struct auxtrace_index_entry ent;
910 size_t nr;
911
912 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
913 return -1;
914
915 auxtrace_index = auxtrace_index__last(head);
916 if (!auxtrace_index)
917 return -1;
918
919 nr = auxtrace_index->nr;
920 if (needs_swap) {
921 auxtrace_index->entries[nr].file_offset =
922 bswap_64(ent.file_offset);
923 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
924 } else {
925 auxtrace_index->entries[nr].file_offset = ent.file_offset;
926 auxtrace_index->entries[nr].sz = ent.sz;
927 }
928
929 auxtrace_index->nr = nr + 1;
930
931 return 0;
932}
933
934int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
935 bool needs_swap)
936{
937 struct list_head *head = &session->auxtrace_index;
938 u64 nr;
939
940 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
941 return -1;
942
943 if (needs_swap)
944 nr = bswap_64(nr);
945
946 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
947 return -1;
948
949 while (nr--) {
950 int err;
951
952 err = auxtrace_index__process_entry(fd, head, needs_swap);
953 if (err)
954 return -1;
955 }
956
957 return 0;
958}
959
960static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
961 struct perf_session *session,
962 struct auxtrace_index_entry *ent)
963{
964 return auxtrace_queues__add_indexed_event(queues, session,
965 ent->file_offset, ent->sz);
966}
967
968int auxtrace_queues__process_index(struct auxtrace_queues *queues,
969 struct perf_session *session)
970{
971 struct auxtrace_index *auxtrace_index;
972 struct auxtrace_index_entry *ent;
973 size_t i;
974 int err;
975
Adrian Hunter2e2967f2018-03-06 11:13:13 +0200976 if (auxtrace__dont_decode(session))
977 return 0;
978
Adrian Hunter99fa2982015-04-30 17:37:25 +0300979 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
980 for (i = 0; i < auxtrace_index->nr; i++) {
981 ent = &auxtrace_index->entries[i];
982 err = auxtrace_queues__process_index_entry(queues,
983 session,
984 ent);
985 if (err)
986 return err;
987 }
988 }
989 return 0;
990}
991
Adrian Huntere5027892015-04-21 12:21:51 +0300992struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
993 struct auxtrace_buffer *buffer)
994{
995 if (buffer) {
996 if (list_is_last(&buffer->list, &queue->head))
997 return NULL;
998 return list_entry(buffer->list.next, struct auxtrace_buffer,
999 list);
1000 } else {
1001 if (list_empty(&queue->head))
1002 return NULL;
1003 return list_entry(queue->head.next, struct auxtrace_buffer,
1004 list);
1005 }
1006}
1007
Adrian Hunterac2f4452019-11-15 14:42:21 +02001008struct auxtrace_queue *auxtrace_queues__sample_queue(struct auxtrace_queues *queues,
1009 struct perf_sample *sample,
1010 struct perf_session *session)
1011{
1012 struct perf_sample_id *sid;
1013 unsigned int idx;
1014 u64 id;
1015
1016 id = sample->id;
1017 if (!id)
1018 return NULL;
1019
1020 sid = perf_evlist__id2sid(session->evlist, id);
1021 if (!sid)
1022 return NULL;
1023
1024 idx = sid->idx;
1025
1026 if (idx >= queues->nr_queues)
1027 return NULL;
1028
1029 return &queues->queue_array[idx];
1030}
1031
1032int auxtrace_queues__add_sample(struct auxtrace_queues *queues,
1033 struct perf_session *session,
1034 struct perf_sample *sample, u64 data_offset,
1035 u64 reference)
1036{
1037 struct auxtrace_buffer buffer = {
1038 .pid = -1,
1039 .data_offset = data_offset,
1040 .reference = reference,
1041 .size = sample->aux_sample.size,
1042 };
1043 struct perf_sample_id *sid;
1044 u64 id = sample->id;
1045 unsigned int idx;
1046
1047 if (!id)
1048 return -EINVAL;
1049
1050 sid = perf_evlist__id2sid(session->evlist, id);
1051 if (!sid)
1052 return -ENOENT;
1053
1054 idx = sid->idx;
1055 buffer.tid = sid->tid;
1056 buffer.cpu = sid->cpu;
1057
1058 return auxtrace_queues__add_buffer(queues, session, idx, &buffer, NULL);
1059}
1060
1061struct queue_data {
1062 bool samples;
1063 bool events;
1064};
1065
1066static int auxtrace_queue_data_cb(struct perf_session *session,
1067 union perf_event *event, u64 offset,
1068 void *data)
1069{
1070 struct queue_data *qd = data;
1071 struct perf_sample sample;
1072 int err;
1073
1074 if (qd->events && event->header.type == PERF_RECORD_AUXTRACE) {
1075 if (event->header.size < sizeof(struct perf_record_auxtrace))
1076 return -EINVAL;
1077 offset += event->header.size;
1078 return session->auxtrace->queue_data(session, NULL, event,
1079 offset);
1080 }
1081
1082 if (!qd->samples || event->header.type != PERF_RECORD_SAMPLE)
1083 return 0;
1084
1085 err = perf_evlist__parse_sample(session->evlist, event, &sample);
1086 if (err)
1087 return err;
1088
1089 if (!sample.aux_sample.size)
1090 return 0;
1091
1092 offset += sample.aux_sample.data - (void *)event;
1093
1094 return session->auxtrace->queue_data(session, &sample, NULL, offset);
1095}
1096
1097int auxtrace_queue_data(struct perf_session *session, bool samples, bool events)
1098{
1099 struct queue_data qd = {
1100 .samples = samples,
1101 .events = events,
1102 };
1103
1104 if (auxtrace__dont_decode(session))
1105 return 0;
1106
1107 if (!session->auxtrace || !session->auxtrace->queue_data)
1108 return -EINVAL;
1109
1110 return perf_session__peek_events(session, session->header.data_offset,
1111 session->header.data_size,
1112 auxtrace_queue_data_cb, &qd);
1113}
1114
Adrian Huntere5027892015-04-21 12:21:51 +03001115void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
1116{
1117 size_t adj = buffer->data_offset & (page_size - 1);
1118 size_t size = buffer->size + adj;
1119 off_t file_offset = buffer->data_offset - adj;
1120 void *addr;
1121
1122 if (buffer->data)
1123 return buffer->data;
1124
1125 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
1126 if (addr == MAP_FAILED)
1127 return NULL;
1128
1129 buffer->mmap_addr = addr;
1130 buffer->mmap_size = size;
1131
1132 buffer->data = addr + adj;
1133
1134 return buffer->data;
1135}
1136
1137void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
1138{
1139 if (!buffer->data || !buffer->mmap_addr)
1140 return;
1141 munmap(buffer->mmap_addr, buffer->mmap_size);
1142 buffer->mmap_addr = NULL;
1143 buffer->mmap_size = 0;
1144 buffer->data = NULL;
1145 buffer->use_data = NULL;
1146}
1147
1148void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
1149{
1150 auxtrace_buffer__put_data(buffer);
1151 if (buffer->data_needs_freeing) {
1152 buffer->data_needs_freeing = false;
1153 zfree(&buffer->data);
1154 buffer->use_data = NULL;
1155 buffer->size = 0;
1156 }
1157}
1158
1159void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
1160{
1161 auxtrace_buffer__drop_data(buffer);
1162 free(buffer);
1163}
1164
Jiri Olsa72932372019-08-28 15:57:16 +02001165void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
Adrian Hunter85ed4722015-04-09 18:53:50 +03001166 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
Adrian Hunter16bd4322019-02-06 12:39:47 +02001167 const char *msg, u64 timestamp)
Adrian Hunter85ed4722015-04-09 18:53:50 +03001168{
1169 size_t size;
1170
Jiri Olsa72932372019-08-28 15:57:16 +02001171 memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error));
Adrian Hunter85ed4722015-04-09 18:53:50 +03001172
1173 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
1174 auxtrace_error->type = type;
1175 auxtrace_error->code = code;
1176 auxtrace_error->cpu = cpu;
1177 auxtrace_error->pid = pid;
1178 auxtrace_error->tid = tid;
Adrian Hunter16bd4322019-02-06 12:39:47 +02001179 auxtrace_error->fmt = 1;
Adrian Hunter85ed4722015-04-09 18:53:50 +03001180 auxtrace_error->ip = ip;
Adrian Hunter16bd4322019-02-06 12:39:47 +02001181 auxtrace_error->time = timestamp;
Adrian Hunter85ed4722015-04-09 18:53:50 +03001182 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
1183
1184 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
1185 strlen(auxtrace_error->msg) + 1;
1186 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
1187}
1188
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001189int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
1190 struct perf_tool *tool,
1191 struct perf_session *session,
1192 perf_event__handler_t process)
1193{
1194 union perf_event *ev;
1195 size_t priv_size;
1196 int err;
1197
1198 pr_debug2("Synthesizing auxtrace information\n");
Mathieu Poirier14a05e12016-01-14 14:46:15 -07001199 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
Jiri Olsa72932372019-08-28 15:57:16 +02001200 ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size);
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001201 if (!ev)
1202 return -ENOMEM;
1203
1204 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
Jiri Olsa72932372019-08-28 15:57:16 +02001205 ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) +
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001206 priv_size;
1207 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
1208 priv_size);
1209 if (err)
1210 goto out_free;
1211
1212 err = process(tool, ev, NULL, NULL);
1213out_free:
1214 free(ev);
1215 return err;
1216}
1217
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001218static void unleader_evsel(struct evlist *evlist, struct evsel *leader)
1219{
1220 struct evsel *new_leader = NULL;
1221 struct evsel *evsel;
1222
1223 /* Find new leader for the group */
1224 evlist__for_each_entry(evlist, evsel) {
1225 if (evsel->leader != leader || evsel == leader)
1226 continue;
1227 if (!new_leader)
1228 new_leader = evsel;
1229 evsel->leader = new_leader;
1230 }
1231
1232 /* Update group information */
1233 if (new_leader) {
1234 zfree(&new_leader->group_name);
1235 new_leader->group_name = leader->group_name;
1236 leader->group_name = NULL;
1237
1238 new_leader->core.nr_members = leader->core.nr_members - 1;
1239 leader->core.nr_members = 1;
1240 }
1241}
1242
1243static void unleader_auxtrace(struct perf_session *session)
1244{
1245 struct evsel *evsel;
1246
1247 evlist__for_each_entry(session->evlist, evsel) {
1248 if (auxtrace__evsel_is_auxtrace(session, evsel) &&
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001249 evsel__is_group_leader(evsel)) {
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001250 unleader_evsel(session->evlist, evsel);
1251 }
1252 }
1253}
1254
Jiri Olsa89f16882018-09-13 14:54:03 +02001255int perf_event__process_auxtrace_info(struct perf_session *session,
1256 union perf_event *event)
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001257{
1258 enum auxtrace_type type = event->auxtrace_info.type;
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001259 int err;
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001260
1261 if (dump_trace)
1262 fprintf(stdout, " type: %u\n", type);
1263
1264 switch (type) {
Adrian Hunter55ea4ab2015-07-17 19:33:36 +03001265 case PERF_AUXTRACE_INTEL_PT:
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001266 err = intel_pt_process_auxtrace_info(event, session);
1267 break;
Adrian Hunterd0170af2015-07-17 19:33:43 +03001268 case PERF_AUXTRACE_INTEL_BTS:
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001269 err = intel_bts_process_auxtrace_info(event, session);
1270 break;
Kim Phillipsffd3d182018-01-14 13:28:50 -06001271 case PERF_AUXTRACE_ARM_SPE:
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001272 err = arm_spe_process_auxtrace_info(event, session);
1273 break;
Mathieu Poiriera818c562016-09-16 09:50:00 -06001274 case PERF_AUXTRACE_CS_ETM:
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001275 err = cs_etm__process_auxtrace_info(event, session);
1276 break;
Thomas Richterb96e6612018-08-02 09:46:20 +02001277 case PERF_AUXTRACE_S390_CPUMSF:
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001278 err = s390_cpumsf_process_auxtrace_info(event, session);
1279 break;
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001280 case PERF_AUXTRACE_UNKNOWN:
1281 default:
1282 return -EINVAL;
1283 }
Adrian Hunter5c7bec02020-04-01 13:16:04 +03001284
1285 if (err)
1286 return err;
1287
1288 unleader_auxtrace(session);
1289
1290 return 0;
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001291}
1292
Jiri Olsa73365552018-09-13 14:54:04 +02001293s64 perf_event__process_auxtrace(struct perf_session *session,
1294 union perf_event *event)
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001295{
1296 s64 err;
1297
1298 if (dump_trace)
Jiri Olsa306c9d22019-08-28 15:57:03 +02001299 fprintf(stdout, " size: %#"PRI_lx64" offset: %#"PRI_lx64" ref: %#"PRI_lx64" idx: %u tid: %d cpu: %d\n",
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001300 event->auxtrace.size, event->auxtrace.offset,
1301 event->auxtrace.reference, event->auxtrace.idx,
1302 event->auxtrace.tid, event->auxtrace.cpu);
1303
1304 if (auxtrace__dont_decode(session))
1305 return event->auxtrace.size;
1306
1307 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
1308 return -EINVAL;
1309
Jiri Olsa73365552018-09-13 14:54:04 +02001310 err = session->auxtrace->process_auxtrace_event(session, event, session->tool);
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001311 if (err < 0)
1312 return err;
1313
1314 return event->auxtrace.size;
1315}
1316
Adrian Hunterf6986c952015-04-09 18:53:49 +03001317#define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
1318#define PERF_ITRACE_DEFAULT_PERIOD 100000
1319#define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
1320#define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
Adrian Hunter601897b2015-09-25 16:15:39 +03001321#define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
1322#define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
Adrian Hunterf6986c952015-04-09 18:53:49 +03001323
Andi Kleen4eb06812018-09-20 11:05:37 -07001324void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
1325 bool no_sample)
Adrian Hunterf6986c952015-04-09 18:53:49 +03001326{
Adrian Hunterf6986c952015-04-09 18:53:49 +03001327 synth_opts->branches = true;
Adrian Hunter53c76b02015-04-30 17:37:28 +03001328 synth_opts->transactions = true;
Adrian Hunter3bdafdf2017-05-26 11:17:24 +03001329 synth_opts->ptwrites = true;
Adrian Hunter70d110d2017-05-26 11:17:25 +03001330 synth_opts->pwr_events = true;
Adrian Hunter181ebb52019-08-06 11:46:03 +03001331 synth_opts->other_events = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001332 synth_opts->errors = true;
Tan Xiaojun9f74d772020-05-30 20:24:41 +08001333 synth_opts->flc = true;
1334 synth_opts->llc = true;
1335 synth_opts->tlb = true;
1336 synth_opts->remote_access = true;
1337
Andi Kleen4eb06812018-09-20 11:05:37 -07001338 if (no_sample) {
1339 synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS;
1340 synth_opts->period = 1;
1341 synth_opts->calls = true;
1342 } else {
1343 synth_opts->instructions = true;
1344 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1345 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1346 }
Adrian Hunterf6986c952015-04-09 18:53:49 +03001347 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
Adrian Hunter601897b2015-09-25 16:15:39 +03001348 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
Andi Kleend1706b32016-03-28 10:45:38 -07001349 synth_opts->initial_skip = 0;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001350}
1351
Adrian Huntercb971432020-07-10 18:10:57 +03001352static int get_flag(const char **ptr, unsigned int *flags)
1353{
1354 while (1) {
1355 char c = **ptr;
1356
1357 if (c >= 'a' && c <= 'z') {
1358 *flags |= 1 << (c - 'a');
1359 ++*ptr;
1360 return 0;
1361 } else if (c == ' ') {
1362 ++*ptr;
1363 continue;
1364 } else {
1365 return -1;
1366 }
1367 }
1368}
1369
1370static int get_flags(const char **ptr, unsigned int *plus_flags, unsigned int *minus_flags)
1371{
1372 while (1) {
1373 switch (**ptr) {
1374 case '+':
1375 ++*ptr;
1376 if (get_flag(ptr, plus_flags))
1377 return -1;
1378 break;
1379 case '-':
1380 ++*ptr;
1381 if (get_flag(ptr, minus_flags))
1382 return -1;
1383 break;
1384 case ' ':
1385 ++*ptr;
1386 break;
1387 default:
1388 return 0;
1389 }
1390 }
1391}
1392
Adrian Hunterf6986c952015-04-09 18:53:49 +03001393/*
1394 * Please check tools/perf/Documentation/perf-script.txt for information
1395 * about the options parsed here, which is introduced after this cset,
1396 * when support in 'perf script' for these options is introduced.
1397 */
1398int itrace_parse_synth_opts(const struct option *opt, const char *str,
1399 int unset)
1400{
1401 struct itrace_synth_opts *synth_opts = opt->value;
1402 const char *p;
1403 char *endptr;
Adrian Hunterf70cfa02015-07-17 19:33:46 +03001404 bool period_type_set = false;
Adrian Huntere1791342015-09-25 16:15:32 +03001405 bool period_set = false;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001406
1407 synth_opts->set = true;
1408
1409 if (unset) {
1410 synth_opts->dont_decode = true;
1411 return 0;
1412 }
1413
1414 if (!str) {
Adrian Hunter355200e2019-05-20 14:37:08 +03001415 itrace_synth_opts__set_default(synth_opts,
1416 synth_opts->default_no_sample);
Adrian Hunterf6986c952015-04-09 18:53:49 +03001417 return 0;
1418 }
1419
1420 for (p = str; *p;) {
1421 switch (*p++) {
1422 case 'i':
1423 synth_opts->instructions = true;
1424 while (*p == ' ' || *p == ',')
1425 p += 1;
1426 if (isdigit(*p)) {
1427 synth_opts->period = strtoull(p, &endptr, 10);
Adrian Huntere1791342015-09-25 16:15:32 +03001428 period_set = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001429 p = endptr;
1430 while (*p == ' ' || *p == ',')
1431 p += 1;
1432 switch (*p++) {
1433 case 'i':
1434 synth_opts->period_type =
1435 PERF_ITRACE_PERIOD_INSTRUCTIONS;
Adrian Hunterf70cfa02015-07-17 19:33:46 +03001436 period_type_set = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001437 break;
1438 case 't':
1439 synth_opts->period_type =
1440 PERF_ITRACE_PERIOD_TICKS;
Adrian Hunterf70cfa02015-07-17 19:33:46 +03001441 period_type_set = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001442 break;
1443 case 'm':
1444 synth_opts->period *= 1000;
1445 /* Fall through */
1446 case 'u':
1447 synth_opts->period *= 1000;
1448 /* Fall through */
1449 case 'n':
1450 if (*p++ != 's')
1451 goto out_err;
1452 synth_opts->period_type =
1453 PERF_ITRACE_PERIOD_NANOSECS;
Adrian Hunterf70cfa02015-07-17 19:33:46 +03001454 period_type_set = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001455 break;
1456 case '\0':
1457 goto out;
1458 default:
1459 goto out_err;
1460 }
1461 }
1462 break;
1463 case 'b':
1464 synth_opts->branches = true;
1465 break;
Adrian Hunter53c76b02015-04-30 17:37:28 +03001466 case 'x':
1467 synth_opts->transactions = true;
1468 break;
Adrian Hunter3bdafdf2017-05-26 11:17:24 +03001469 case 'w':
1470 synth_opts->ptwrites = true;
1471 break;
Adrian Hunter70d110d2017-05-26 11:17:25 +03001472 case 'p':
1473 synth_opts->pwr_events = true;
1474 break;
Adrian Hunter181ebb52019-08-06 11:46:03 +03001475 case 'o':
1476 synth_opts->other_events = true;
1477 break;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001478 case 'e':
1479 synth_opts->errors = true;
Adrian Huntercb971432020-07-10 18:10:57 +03001480 if (get_flags(&p, &synth_opts->error_plus_flags,
1481 &synth_opts->error_minus_flags))
1482 goto out_err;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001483 break;
1484 case 'd':
1485 synth_opts->log = true;
Adrian Hunter935aac22020-07-10 18:10:59 +03001486 if (get_flags(&p, &synth_opts->log_plus_flags,
1487 &synth_opts->log_minus_flags))
1488 goto out_err;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001489 break;
1490 case 'c':
1491 synth_opts->branches = true;
1492 synth_opts->calls = true;
1493 break;
1494 case 'r':
1495 synth_opts->branches = true;
1496 synth_opts->returns = true;
1497 break;
Adrian Hunter1c5c25b2020-04-01 13:16:05 +03001498 case 'G':
Adrian Hunterf6986c952015-04-09 18:53:49 +03001499 case 'g':
Adrian Hunter1c5c25b2020-04-01 13:16:05 +03001500 if (p[-1] == 'G')
1501 synth_opts->add_callchain = true;
1502 else
1503 synth_opts->callchain = true;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001504 synth_opts->callchain_sz =
1505 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1506 while (*p == ' ' || *p == ',')
1507 p += 1;
1508 if (isdigit(*p)) {
1509 unsigned int val;
1510
1511 val = strtoul(p, &endptr, 10);
1512 p = endptr;
1513 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1514 goto out_err;
1515 synth_opts->callchain_sz = val;
1516 }
1517 break;
Adrian Hunterec90e422020-04-29 18:07:46 +03001518 case 'L':
Adrian Hunter601897b2015-09-25 16:15:39 +03001519 case 'l':
Adrian Hunterec90e422020-04-29 18:07:46 +03001520 if (p[-1] == 'L')
1521 synth_opts->add_last_branch = true;
1522 else
1523 synth_opts->last_branch = true;
Adrian Hunter601897b2015-09-25 16:15:39 +03001524 synth_opts->last_branch_sz =
1525 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1526 while (*p == ' ' || *p == ',')
1527 p += 1;
1528 if (isdigit(*p)) {
1529 unsigned int val;
1530
1531 val = strtoul(p, &endptr, 10);
1532 p = endptr;
1533 if (!val ||
1534 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1535 goto out_err;
1536 synth_opts->last_branch_sz = val;
1537 }
1538 break;
Andi Kleend1706b32016-03-28 10:45:38 -07001539 case 's':
1540 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1541 if (p == endptr)
1542 goto out_err;
1543 p = endptr;
1544 break;
Tan Xiaojun9f74d772020-05-30 20:24:41 +08001545 case 'f':
1546 synth_opts->flc = true;
1547 break;
1548 case 'm':
1549 synth_opts->llc = true;
1550 break;
1551 case 't':
1552 synth_opts->tlb = true;
1553 break;
1554 case 'a':
1555 synth_opts->remote_access = true;
1556 break;
Adrian Hunterf6986c952015-04-09 18:53:49 +03001557 case ' ':
1558 case ',':
1559 break;
1560 default:
1561 goto out_err;
1562 }
1563 }
1564out:
1565 if (synth_opts->instructions) {
Adrian Hunterf70cfa02015-07-17 19:33:46 +03001566 if (!period_type_set)
Adrian Hunterf6986c952015-04-09 18:53:49 +03001567 synth_opts->period_type =
1568 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
Adrian Huntere1791342015-09-25 16:15:32 +03001569 if (!period_set)
Adrian Hunterf6986c952015-04-09 18:53:49 +03001570 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1571 }
1572
1573 return 0;
1574
1575out_err:
1576 pr_err("Bad Instruction Tracing options '%s'\n", str);
1577 return -EINVAL;
1578}
1579
Adrian Hunter85ed4722015-04-09 18:53:50 +03001580static const char * const auxtrace_error_type_name[] = {
1581 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1582};
1583
1584static const char *auxtrace_error_name(int type)
1585{
1586 const char *error_type_name = NULL;
1587
1588 if (type < PERF_AUXTRACE_ERROR_MAX)
1589 error_type_name = auxtrace_error_type_name[type];
1590 if (!error_type_name)
1591 error_type_name = "unknown AUX";
1592 return error_type_name;
1593}
1594
1595size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1596{
Jiri Olsa72932372019-08-28 15:57:16 +02001597 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
Adrian Hunter16bd4322019-02-06 12:39:47 +02001598 unsigned long long nsecs = e->time;
1599 const char *msg = e->msg;
Adrian Hunter85ed4722015-04-09 18:53:50 +03001600 int ret;
1601
1602 ret = fprintf(fp, " %s error type %u",
1603 auxtrace_error_name(e->type), e->type);
Adrian Hunter16bd4322019-02-06 12:39:47 +02001604
1605 if (e->fmt && nsecs) {
1606 unsigned long secs = nsecs / NSEC_PER_SEC;
1607
1608 nsecs -= secs * NSEC_PER_SEC;
1609 ret += fprintf(fp, " time %lu.%09llu", secs, nsecs);
1610 } else {
1611 ret += fprintf(fp, " time 0");
1612 }
1613
1614 if (!e->fmt)
1615 msg = (const char *)&e->time;
1616
Jiri Olsa3460efb2019-08-28 15:57:04 +02001617 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n",
Adrian Hunter16bd4322019-02-06 12:39:47 +02001618 e->cpu, e->pid, e->tid, e->ip, e->code, msg);
Adrian Hunter85ed4722015-04-09 18:53:50 +03001619 return ret;
1620}
1621
1622void perf_session__auxtrace_error_inc(struct perf_session *session,
1623 union perf_event *event)
1624{
Jiri Olsa72932372019-08-28 15:57:16 +02001625 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
Adrian Hunter85ed4722015-04-09 18:53:50 +03001626
1627 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1628 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1629}
1630
1631void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1632{
1633 int i;
1634
1635 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1636 if (!stats->nr_auxtrace_errors[i])
1637 continue;
1638 ui__warning("%u %s errors\n",
1639 stats->nr_auxtrace_errors[i],
1640 auxtrace_error_name(i));
1641 }
1642}
1643
Jiri Olsa89f16882018-09-13 14:54:03 +02001644int perf_event__process_auxtrace_error(struct perf_session *session,
1645 union perf_event *event)
Adrian Hunter85ed4722015-04-09 18:53:50 +03001646{
Adrian Hunter73f75fb2015-04-09 18:53:53 +03001647 if (auxtrace__dont_decode(session))
1648 return 0;
1649
Adrian Hunter85ed4722015-04-09 18:53:50 +03001650 perf_event__fprintf_auxtrace_error(event, stdout);
1651 return 0;
1652}
1653
Jiri Olsaa5830532019-07-27 20:30:53 +02001654static int __auxtrace_mmap__read(struct mmap *map,
Adrian Hunterd20031b2015-04-30 17:37:31 +03001655 struct auxtrace_record *itr,
1656 struct perf_tool *tool, process_auxtrace_t fn,
1657 bool snapshot, size_t snapshot_size)
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001658{
Jiri Olsae035f4c2018-09-13 14:54:05 +02001659 struct auxtrace_mmap *mm = &map->auxtrace_mmap;
Adrian Hunterd20031b2015-04-30 17:37:31 +03001660 u64 head, old = mm->prev, offset, ref;
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001661 unsigned char *data = mm->base;
1662 size_t size, head_off, old_off, len1, len2, padding;
1663 union perf_event ev;
1664 void *data1, *data2;
1665
Adrian Hunterd20031b2015-04-30 17:37:31 +03001666 if (snapshot) {
1667 head = auxtrace_mmap__read_snapshot_head(mm);
1668 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1669 &head, &old))
1670 return -1;
1671 } else {
1672 head = auxtrace_mmap__read_head(mm);
1673 }
1674
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001675 if (old == head)
1676 return 0;
1677
1678 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1679 mm->idx, old, head, head - old);
1680
1681 if (mm->mask) {
1682 head_off = head & mm->mask;
1683 old_off = old & mm->mask;
1684 } else {
1685 head_off = head % mm->len;
1686 old_off = old % mm->len;
1687 }
1688
1689 if (head_off > old_off)
1690 size = head_off - old_off;
1691 else
1692 size = mm->len - (old_off - head_off);
1693
Adrian Hunterd20031b2015-04-30 17:37:31 +03001694 if (snapshot && size > snapshot_size)
1695 size = snapshot_size;
1696
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001697 ref = auxtrace_record__reference(itr);
1698
1699 if (head > old || size <= head || mm->mask) {
1700 offset = head - size;
1701 } else {
1702 /*
1703 * When the buffer size is not a power of 2, 'head' wraps at the
1704 * highest multiple of the buffer size, so we have to subtract
1705 * the remainder here.
1706 */
1707 u64 rem = (0ULL - mm->len) % mm->len;
1708
1709 offset = head - size - rem;
1710 }
1711
1712 if (size > head_off) {
1713 len1 = size - head_off;
1714 data1 = &data[mm->len - len1];
1715 len2 = head_off;
1716 data2 = &data[0];
1717 } else {
1718 len1 = size;
1719 data1 = &data[head_off - len1];
1720 len2 = 0;
1721 data2 = NULL;
1722 }
1723
Adrian Hunter83b2ea22015-05-29 16:33:38 +03001724 if (itr->alignment) {
1725 unsigned int unwanted = len1 % itr->alignment;
1726
1727 len1 -= unwanted;
1728 size -= unwanted;
1729 }
1730
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001731 /* padding must be written by fn() e.g. record__process_auxtrace() */
Adrian Hunterc3fcadf02019-02-06 12:39:43 +02001732 padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001733 if (padding)
Adrian Hunterc3fcadf02019-02-06 12:39:43 +02001734 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001735
1736 memset(&ev, 0, sizeof(ev));
1737 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1738 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1739 ev.auxtrace.size = size + padding;
1740 ev.auxtrace.offset = offset;
1741 ev.auxtrace.reference = ref;
1742 ev.auxtrace.idx = mm->idx;
1743 ev.auxtrace.tid = mm->tid;
1744 ev.auxtrace.cpu = mm->cpu;
1745
Jiri Olsaded2b8f2018-09-13 14:54:06 +02001746 if (fn(tool, map, &ev, data1, len1, data2, len2))
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001747 return -1;
1748
1749 mm->prev = head;
1750
Adrian Hunterd20031b2015-04-30 17:37:31 +03001751 if (!snapshot) {
1752 auxtrace_mmap__write_tail(mm, head);
1753 if (itr->read_finish) {
1754 int err;
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001755
Adrian Hunterd20031b2015-04-30 17:37:31 +03001756 err = itr->read_finish(itr, mm->idx);
1757 if (err < 0)
1758 return err;
1759 }
Adrian Hunter9e0cc4f2015-04-09 18:53:44 +03001760 }
1761
1762 return 1;
1763}
Adrian Hunterc3278f02015-04-09 18:53:54 +03001764
Jiri Olsaa5830532019-07-27 20:30:53 +02001765int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr,
Adrian Hunterd20031b2015-04-30 17:37:31 +03001766 struct perf_tool *tool, process_auxtrace_t fn)
1767{
Jiri Olsae035f4c2018-09-13 14:54:05 +02001768 return __auxtrace_mmap__read(map, itr, tool, fn, false, 0);
Adrian Hunterd20031b2015-04-30 17:37:31 +03001769}
1770
Jiri Olsaa5830532019-07-27 20:30:53 +02001771int auxtrace_mmap__read_snapshot(struct mmap *map,
Adrian Hunterd20031b2015-04-30 17:37:31 +03001772 struct auxtrace_record *itr,
1773 struct perf_tool *tool, process_auxtrace_t fn,
1774 size_t snapshot_size)
1775{
Jiri Olsae035f4c2018-09-13 14:54:05 +02001776 return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size);
Adrian Hunterd20031b2015-04-30 17:37:31 +03001777}
1778
Adrian Hunterc3278f02015-04-09 18:53:54 +03001779/**
1780 * struct auxtrace_cache - hash table to implement a cache
1781 * @hashtable: the hashtable
1782 * @sz: hashtable size (number of hlists)
1783 * @entry_size: size of an entry
1784 * @limit: limit the number of entries to this maximum, when reached the cache
1785 * is dropped and caching begins again with an empty cache
1786 * @cnt: current number of entries
1787 * @bits: hashtable size (@sz = 2^@bits)
1788 */
1789struct auxtrace_cache {
1790 struct hlist_head *hashtable;
1791 size_t sz;
1792 size_t entry_size;
1793 size_t limit;
1794 size_t cnt;
1795 unsigned int bits;
1796};
1797
1798struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1799 unsigned int limit_percent)
1800{
1801 struct auxtrace_cache *c;
1802 struct hlist_head *ht;
1803 size_t sz, i;
1804
1805 c = zalloc(sizeof(struct auxtrace_cache));
1806 if (!c)
1807 return NULL;
1808
1809 sz = 1UL << bits;
1810
1811 ht = calloc(sz, sizeof(struct hlist_head));
1812 if (!ht)
1813 goto out_free;
1814
1815 for (i = 0; i < sz; i++)
1816 INIT_HLIST_HEAD(&ht[i]);
1817
1818 c->hashtable = ht;
1819 c->sz = sz;
1820 c->entry_size = entry_size;
1821 c->limit = (c->sz * limit_percent) / 100;
1822 c->bits = bits;
1823
1824 return c;
1825
1826out_free:
1827 free(c);
1828 return NULL;
1829}
1830
1831static void auxtrace_cache__drop(struct auxtrace_cache *c)
1832{
1833 struct auxtrace_cache_entry *entry;
1834 struct hlist_node *tmp;
1835 size_t i;
1836
1837 if (!c)
1838 return;
1839
1840 for (i = 0; i < c->sz; i++) {
1841 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1842 hlist_del(&entry->hash);
1843 auxtrace_cache__free_entry(c, entry);
1844 }
1845 }
1846
1847 c->cnt = 0;
1848}
1849
1850void auxtrace_cache__free(struct auxtrace_cache *c)
1851{
1852 if (!c)
1853 return;
1854
1855 auxtrace_cache__drop(c);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001856 zfree(&c->hashtable);
Adrian Hunterc3278f02015-04-09 18:53:54 +03001857 free(c);
1858}
1859
1860void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1861{
1862 return malloc(c->entry_size);
1863}
1864
1865void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1866 void *entry)
1867{
1868 free(entry);
1869}
1870
1871int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1872 struct auxtrace_cache_entry *entry)
1873{
1874 if (c->limit && ++c->cnt > c->limit)
1875 auxtrace_cache__drop(c);
1876
1877 entry->key = key;
1878 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1879
1880 return 0;
1881}
1882
Adrian Hunterfd62c102019-10-25 15:59:59 +03001883static struct auxtrace_cache_entry *auxtrace_cache__rm(struct auxtrace_cache *c,
1884 u32 key)
1885{
1886 struct auxtrace_cache_entry *entry;
1887 struct hlist_head *hlist;
1888 struct hlist_node *n;
1889
1890 if (!c)
1891 return NULL;
1892
1893 hlist = &c->hashtable[hash_32(key, c->bits)];
1894 hlist_for_each_entry_safe(entry, n, hlist, hash) {
1895 if (entry->key == key) {
1896 hlist_del(&entry->hash);
1897 return entry;
1898 }
1899 }
1900
1901 return NULL;
1902}
1903
1904void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key)
1905{
1906 struct auxtrace_cache_entry *entry = auxtrace_cache__rm(c, key);
1907
1908 auxtrace_cache__free_entry(c, entry);
1909}
1910
Adrian Hunterc3278f02015-04-09 18:53:54 +03001911void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1912{
1913 struct auxtrace_cache_entry *entry;
1914 struct hlist_head *hlist;
1915
1916 if (!c)
1917 return NULL;
1918
1919 hlist = &c->hashtable[hash_32(key, c->bits)];
1920 hlist_for_each_entry(entry, hlist, hash) {
1921 if (entry->key == key)
1922 return entry;
1923 }
1924
1925 return NULL;
1926}
Adrian Hunter1b36c032016-09-23 17:38:39 +03001927
1928static void addr_filter__free_str(struct addr_filter *filt)
1929{
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001930 zfree(&filt->str);
Adrian Hunter1b36c032016-09-23 17:38:39 +03001931 filt->action = NULL;
1932 filt->sym_from = NULL;
1933 filt->sym_to = NULL;
1934 filt->filename = NULL;
Adrian Hunter1b36c032016-09-23 17:38:39 +03001935}
1936
1937static struct addr_filter *addr_filter__new(void)
1938{
1939 struct addr_filter *filt = zalloc(sizeof(*filt));
1940
1941 if (filt)
1942 INIT_LIST_HEAD(&filt->list);
1943
1944 return filt;
1945}
1946
1947static void addr_filter__free(struct addr_filter *filt)
1948{
1949 if (filt)
1950 addr_filter__free_str(filt);
1951 free(filt);
1952}
1953
1954static void addr_filters__add(struct addr_filters *filts,
1955 struct addr_filter *filt)
1956{
1957 list_add_tail(&filt->list, &filts->head);
1958 filts->cnt += 1;
1959}
1960
1961static void addr_filters__del(struct addr_filters *filts,
1962 struct addr_filter *filt)
1963{
1964 list_del_init(&filt->list);
1965 filts->cnt -= 1;
1966}
1967
1968void addr_filters__init(struct addr_filters *filts)
1969{
1970 INIT_LIST_HEAD(&filts->head);
1971 filts->cnt = 0;
1972}
1973
1974void addr_filters__exit(struct addr_filters *filts)
1975{
1976 struct addr_filter *filt, *n;
1977
1978 list_for_each_entry_safe(filt, n, &filts->head, list) {
1979 addr_filters__del(filts, filt);
1980 addr_filter__free(filt);
1981 }
1982}
1983
1984static int parse_num_or_str(char **inp, u64 *num, const char **str,
1985 const char *str_delim)
1986{
1987 *inp += strspn(*inp, " ");
1988
1989 if (isdigit(**inp)) {
1990 char *endptr;
1991
1992 if (!num)
1993 return -EINVAL;
1994 errno = 0;
1995 *num = strtoull(*inp, &endptr, 0);
1996 if (errno)
1997 return -errno;
1998 if (endptr == *inp)
1999 return -EINVAL;
2000 *inp = endptr;
2001 } else {
2002 size_t n;
2003
2004 if (!str)
2005 return -EINVAL;
2006 *inp += strspn(*inp, " ");
2007 *str = *inp;
2008 n = strcspn(*inp, str_delim);
2009 if (!n)
2010 return -EINVAL;
2011 *inp += n;
2012 if (**inp) {
2013 **inp = '\0';
2014 *inp += 1;
2015 }
2016 }
2017 return 0;
2018}
2019
2020static int parse_action(struct addr_filter *filt)
2021{
2022 if (!strcmp(filt->action, "filter")) {
2023 filt->start = true;
2024 filt->range = true;
2025 } else if (!strcmp(filt->action, "start")) {
2026 filt->start = true;
2027 } else if (!strcmp(filt->action, "stop")) {
2028 filt->start = false;
2029 } else if (!strcmp(filt->action, "tracestop")) {
2030 filt->start = false;
2031 filt->range = true;
2032 filt->action += 5; /* Change 'tracestop' to 'stop' */
2033 } else {
2034 return -EINVAL;
2035 }
2036 return 0;
2037}
2038
2039static int parse_sym_idx(char **inp, int *idx)
2040{
2041 *idx = -1;
2042
2043 *inp += strspn(*inp, " ");
2044
2045 if (**inp != '#')
2046 return 0;
2047
2048 *inp += 1;
2049
2050 if (**inp == 'g' || **inp == 'G') {
2051 *inp += 1;
2052 *idx = 0;
2053 } else {
2054 unsigned long num;
2055 char *endptr;
2056
2057 errno = 0;
2058 num = strtoul(*inp, &endptr, 0);
2059 if (errno)
2060 return -errno;
2061 if (endptr == *inp || num > INT_MAX)
2062 return -EINVAL;
2063 *inp = endptr;
2064 *idx = num;
2065 }
2066
2067 return 0;
2068}
2069
2070static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
2071{
2072 int err = parse_num_or_str(inp, num, str, " ");
2073
2074 if (!err && *str)
2075 err = parse_sym_idx(inp, idx);
2076
2077 return err;
2078}
2079
2080static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
2081{
2082 char *fstr;
2083 int err;
2084
2085 filt->str = fstr = strdup(*filter_inp);
2086 if (!fstr)
2087 return -ENOMEM;
2088
2089 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
2090 if (err)
2091 goto out_err;
2092
2093 err = parse_action(filt);
2094 if (err)
2095 goto out_err;
2096
2097 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
2098 &filt->sym_from_idx);
2099 if (err)
2100 goto out_err;
2101
2102 fstr += strspn(fstr, " ");
2103
2104 if (*fstr == '/') {
2105 fstr += 1;
2106 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
2107 &filt->sym_to_idx);
2108 if (err)
2109 goto out_err;
2110 filt->range = true;
2111 }
2112
2113 fstr += strspn(fstr, " ");
2114
2115 if (*fstr == '@') {
2116 fstr += 1;
2117 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
2118 if (err)
2119 goto out_err;
2120 }
2121
2122 fstr += strspn(fstr, " ,");
2123
2124 *filter_inp += fstr - filt->str;
2125
2126 return 0;
2127
2128out_err:
2129 addr_filter__free_str(filt);
2130
2131 return err;
2132}
2133
2134int addr_filters__parse_bare_filter(struct addr_filters *filts,
2135 const char *filter)
2136{
2137 struct addr_filter *filt;
2138 const char *fstr = filter;
2139 int err;
2140
2141 while (*fstr) {
2142 filt = addr_filter__new();
2143 err = parse_one_filter(filt, &fstr);
2144 if (err) {
2145 addr_filter__free(filt);
2146 addr_filters__exit(filts);
2147 return err;
2148 }
2149 addr_filters__add(filts, filt);
2150 }
2151
2152 return 0;
2153}
2154
2155struct sym_args {
2156 const char *name;
2157 u64 start;
2158 u64 size;
2159 int idx;
2160 int cnt;
2161 bool started;
2162 bool global;
2163 bool selected;
2164 bool duplicate;
2165 bool near;
2166};
2167
2168static bool kern_sym_match(struct sym_args *args, const char *name, char type)
2169{
2170 /* A function with the same name, and global or the n'th found or any */
Arnaldo Carvalho de Meloe85e0e02018-04-25 17:16:31 -03002171 return kallsyms__is_function(type) &&
Adrian Hunter1b36c032016-09-23 17:38:39 +03002172 !strcmp(name, args->name) &&
2173 ((args->global && isupper(type)) ||
2174 (args->selected && ++(args->cnt) == args->idx) ||
2175 (!args->global && !args->selected));
2176}
2177
2178static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2179{
2180 struct sym_args *args = arg;
2181
2182 if (args->started) {
2183 if (!args->size)
2184 args->size = start - args->start;
2185 if (args->selected) {
2186 if (args->size)
2187 return 1;
2188 } else if (kern_sym_match(args, name, type)) {
2189 args->duplicate = true;
2190 return 1;
2191 }
2192 } else if (kern_sym_match(args, name, type)) {
2193 args->started = true;
2194 args->start = start;
2195 }
2196
2197 return 0;
2198}
2199
2200static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2201{
2202 struct sym_args *args = arg;
2203
2204 if (kern_sym_match(args, name, type)) {
2205 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2206 ++args->cnt, start, type, name);
2207 args->near = true;
2208 } else if (args->near) {
2209 args->near = false;
2210 pr_err("\t\twhich is near\t\t%s\n", name);
2211 }
2212
2213 return 0;
2214}
2215
2216static int sym_not_found_error(const char *sym_name, int idx)
2217{
2218 if (idx > 0) {
2219 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
2220 idx, sym_name);
2221 } else if (!idx) {
2222 pr_err("Global symbol '%s' not found.\n", sym_name);
2223 } else {
2224 pr_err("Symbol '%s' not found.\n", sym_name);
2225 }
2226 pr_err("Note that symbols must be functions.\n");
2227
2228 return -EINVAL;
2229}
2230
2231static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
2232{
2233 struct sym_args args = {
2234 .name = sym_name,
2235 .idx = idx,
2236 .global = !idx,
2237 .selected = idx > 0,
2238 };
2239 int err;
2240
2241 *start = 0;
2242 *size = 0;
2243
2244 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
2245 if (err < 0) {
2246 pr_err("Failed to parse /proc/kallsyms\n");
2247 return err;
2248 }
2249
2250 if (args.duplicate) {
2251 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
2252 args.cnt = 0;
2253 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
2254 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2255 sym_name);
2256 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2257 return -EINVAL;
2258 }
2259
2260 if (!args.started) {
2261 pr_err("Kernel symbol lookup: ");
2262 return sym_not_found_error(sym_name, idx);
2263 }
2264
2265 *start = args.start;
2266 *size = args.size;
2267
2268 return 0;
2269}
2270
2271static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
2272 char type, u64 start)
2273{
2274 struct sym_args *args = arg;
2275
Arnaldo Carvalho de Meloe85e0e02018-04-25 17:16:31 -03002276 if (!kallsyms__is_function(type))
Adrian Hunter1b36c032016-09-23 17:38:39 +03002277 return 0;
2278
2279 if (!args->started) {
2280 args->started = true;
2281 args->start = start;
2282 }
2283 /* Don't know exactly where the kernel ends, so we add a page */
2284 args->size = round_up(start, page_size) + page_size - args->start;
2285
2286 return 0;
2287}
2288
2289static int addr_filter__entire_kernel(struct addr_filter *filt)
2290{
2291 struct sym_args args = { .started = false };
2292 int err;
2293
2294 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
2295 if (err < 0 || !args.started) {
2296 pr_err("Failed to parse /proc/kallsyms\n");
2297 return err;
2298 }
2299
2300 filt->addr = args.start;
2301 filt->size = args.size;
2302
2303 return 0;
2304}
2305
2306static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
2307{
2308 if (start + size >= filt->addr)
2309 return 0;
2310
2311 if (filt->sym_from) {
2312 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
2313 filt->sym_to, start, filt->sym_from, filt->addr);
2314 } else {
2315 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
2316 filt->sym_to, start, filt->addr);
2317 }
2318
2319 return -EINVAL;
2320}
2321
2322static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
2323{
2324 bool no_size = false;
2325 u64 start, size;
2326 int err;
2327
2328 if (symbol_conf.kptr_restrict) {
2329 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
2330 return -EINVAL;
2331 }
2332
2333 if (filt->sym_from && !strcmp(filt->sym_from, "*"))
2334 return addr_filter__entire_kernel(filt);
2335
2336 if (filt->sym_from) {
2337 err = find_kern_sym(filt->sym_from, &start, &size,
2338 filt->sym_from_idx);
2339 if (err)
2340 return err;
2341 filt->addr = start;
2342 if (filt->range && !filt->size && !filt->sym_to) {
2343 filt->size = size;
Adrian Hunterc3a0bbc2017-03-24 14:15:52 +02002344 no_size = !size;
Adrian Hunter1b36c032016-09-23 17:38:39 +03002345 }
2346 }
2347
2348 if (filt->sym_to) {
2349 err = find_kern_sym(filt->sym_to, &start, &size,
2350 filt->sym_to_idx);
2351 if (err)
2352 return err;
2353
2354 err = check_end_after_start(filt, start, size);
2355 if (err)
2356 return err;
2357 filt->size = start + size - filt->addr;
Adrian Hunterc3a0bbc2017-03-24 14:15:52 +02002358 no_size = !size;
Adrian Hunter1b36c032016-09-23 17:38:39 +03002359 }
2360
2361 /* The very last symbol in kallsyms does not imply a particular size */
2362 if (no_size) {
2363 pr_err("Cannot determine size of symbol '%s'\n",
2364 filt->sym_to ? filt->sym_to : filt->sym_from);
2365 return -EINVAL;
2366 }
2367
2368 return 0;
2369}
2370
2371static struct dso *load_dso(const char *name)
2372{
2373 struct map *map;
2374 struct dso *dso;
2375
2376 map = dso__new_map(name);
2377 if (!map)
2378 return NULL;
2379
Adrian Hunterc1c49202019-03-01 14:29:02 +02002380 if (map__load(map) < 0)
2381 pr_err("File '%s' not found or has no symbols.\n", name);
Adrian Hunter1b36c032016-09-23 17:38:39 +03002382
2383 dso = dso__get(map->dso);
2384
2385 map__put(map);
2386
2387 return dso;
2388}
2389
2390static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
2391 int idx)
2392{
2393 /* Same name, and global or the n'th found or any */
2394 return !arch__compare_symbol_names(name, sym->name) &&
2395 ((!idx && sym->binding == STB_GLOBAL) ||
2396 (idx > 0 && ++*cnt == idx) ||
2397 idx < 0);
2398}
2399
2400static void print_duplicate_syms(struct dso *dso, const char *sym_name)
2401{
2402 struct symbol *sym;
2403 bool near = false;
2404 int cnt = 0;
2405
2406 pr_err("Multiple symbols with name '%s'\n", sym_name);
2407
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -03002408 sym = dso__first_symbol(dso);
Adrian Hunter1b36c032016-09-23 17:38:39 +03002409 while (sym) {
2410 if (dso_sym_match(sym, sym_name, &cnt, -1)) {
2411 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2412 ++cnt, sym->start,
2413 sym->binding == STB_GLOBAL ? 'g' :
2414 sym->binding == STB_LOCAL ? 'l' : 'w',
2415 sym->name);
2416 near = true;
2417 } else if (near) {
2418 near = false;
2419 pr_err("\t\twhich is near\t\t%s\n", sym->name);
2420 }
2421 sym = dso__next_symbol(sym);
2422 }
2423
2424 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2425 sym_name);
2426 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2427}
2428
2429static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
2430 u64 *size, int idx)
2431{
2432 struct symbol *sym;
2433 int cnt = 0;
2434
2435 *start = 0;
2436 *size = 0;
2437
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -03002438 sym = dso__first_symbol(dso);
Adrian Hunter1b36c032016-09-23 17:38:39 +03002439 while (sym) {
2440 if (*start) {
2441 if (!*size)
2442 *size = sym->start - *start;
2443 if (idx > 0) {
2444 if (*size)
2445 return 1;
2446 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2447 print_duplicate_syms(dso, sym_name);
2448 return -EINVAL;
2449 }
2450 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2451 *start = sym->start;
2452 *size = sym->end - sym->start;
2453 }
2454 sym = dso__next_symbol(sym);
2455 }
2456
2457 if (!*start)
2458 return sym_not_found_error(sym_name, idx);
2459
2460 return 0;
2461}
2462
2463static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
2464{
Adrian Hunter57176602018-11-27 10:46:34 +02002465 if (dso__data_file_size(dso, NULL)) {
2466 pr_err("Failed to determine filter for %s\nCannot determine file size.\n",
Adrian Hunter1b36c032016-09-23 17:38:39 +03002467 filt->filename);
2468 return -EINVAL;
2469 }
2470
Adrian Hunter57176602018-11-27 10:46:34 +02002471 filt->addr = 0;
2472 filt->size = dso->data.file_size;
Adrian Hunter1b36c032016-09-23 17:38:39 +03002473
2474 return 0;
2475}
2476
2477static int addr_filter__resolve_syms(struct addr_filter *filt)
2478{
2479 u64 start, size;
2480 struct dso *dso;
2481 int err = 0;
2482
2483 if (!filt->sym_from && !filt->sym_to)
2484 return 0;
2485
2486 if (!filt->filename)
2487 return addr_filter__resolve_kernel_syms(filt);
2488
2489 dso = load_dso(filt->filename);
2490 if (!dso) {
2491 pr_err("Failed to load symbols from: %s\n", filt->filename);
2492 return -EINVAL;
2493 }
2494
2495 if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
2496 err = addr_filter__entire_dso(filt, dso);
2497 goto put_dso;
2498 }
2499
2500 if (filt->sym_from) {
2501 err = find_dso_sym(dso, filt->sym_from, &start, &size,
2502 filt->sym_from_idx);
2503 if (err)
2504 goto put_dso;
2505 filt->addr = start;
2506 if (filt->range && !filt->size && !filt->sym_to)
2507 filt->size = size;
2508 }
2509
2510 if (filt->sym_to) {
2511 err = find_dso_sym(dso, filt->sym_to, &start, &size,
2512 filt->sym_to_idx);
2513 if (err)
2514 goto put_dso;
2515
2516 err = check_end_after_start(filt, start, size);
2517 if (err)
2518 return err;
2519
2520 filt->size = start + size - filt->addr;
2521 }
2522
2523put_dso:
2524 dso__put(dso);
2525
2526 return err;
2527}
2528
2529static char *addr_filter__to_str(struct addr_filter *filt)
2530{
2531 char filename_buf[PATH_MAX];
2532 const char *at = "";
2533 const char *fn = "";
2534 char *filter;
2535 int err;
2536
2537 if (filt->filename) {
2538 at = "@";
2539 fn = realpath(filt->filename, filename_buf);
2540 if (!fn)
2541 return NULL;
2542 }
2543
2544 if (filt->range) {
2545 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2546 filt->action, filt->addr, filt->size, at, fn);
2547 } else {
2548 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2549 filt->action, filt->addr, at, fn);
2550 }
2551
2552 return err < 0 ? NULL : filter;
2553}
2554
Jiri Olsa32dcd022019-07-21 13:23:51 +02002555static int parse_addr_filter(struct evsel *evsel, const char *filter,
Adrian Hunter1b36c032016-09-23 17:38:39 +03002556 int max_nr)
2557{
2558 struct addr_filters filts;
2559 struct addr_filter *filt;
2560 int err;
2561
2562 addr_filters__init(&filts);
2563
2564 err = addr_filters__parse_bare_filter(&filts, filter);
2565 if (err)
2566 goto out_exit;
2567
2568 if (filts.cnt > max_nr) {
2569 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2570 filts.cnt, max_nr);
2571 err = -EINVAL;
2572 goto out_exit;
2573 }
2574
2575 list_for_each_entry(filt, &filts.head, list) {
2576 char *new_filter;
2577
2578 err = addr_filter__resolve_syms(filt);
2579 if (err)
2580 goto out_exit;
2581
2582 new_filter = addr_filter__to_str(filt);
2583 if (!new_filter) {
2584 err = -ENOMEM;
2585 goto out_exit;
2586 }
2587
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03002588 if (evsel__append_addr_filter(evsel, new_filter)) {
Adrian Hunter1b36c032016-09-23 17:38:39 +03002589 err = -ENOMEM;
2590 goto out_exit;
2591 }
2592 }
2593
2594out_exit:
2595 addr_filters__exit(&filts);
2596
2597 if (err) {
2598 pr_err("Failed to parse address filter: '%s'\n", filter);
2599 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2600 pr_err("Where multiple filters are separated by space or comma.\n");
2601 }
2602
2603 return err;
2604}
2605
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03002606static int evsel__nr_addr_filter(struct evsel *evsel)
Adrian Hunter1b36c032016-09-23 17:38:39 +03002607{
Arnaldo Carvalho de Meloe76026b2020-04-29 15:50:10 -03002608 struct perf_pmu *pmu = evsel__find_pmu(evsel);
Adrian Hunter1b36c032016-09-23 17:38:39 +03002609 int nr_addr_filters = 0;
2610
2611 if (!pmu)
2612 return 0;
2613
2614 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2615
2616 return nr_addr_filters;
2617}
2618
Jiri Olsa63503db2019-07-21 13:23:52 +02002619int auxtrace_parse_filters(struct evlist *evlist)
Adrian Hunter1b36c032016-09-23 17:38:39 +03002620{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002621 struct evsel *evsel;
Adrian Hunter1b36c032016-09-23 17:38:39 +03002622 char *filter;
2623 int err, max_nr;
2624
2625 evlist__for_each_entry(evlist, evsel) {
2626 filter = evsel->filter;
Arnaldo Carvalho de Meload681ad2020-04-29 16:19:05 -03002627 max_nr = evsel__nr_addr_filter(evsel);
Adrian Hunter1b36c032016-09-23 17:38:39 +03002628 if (!filter || !max_nr)
2629 continue;
2630 evsel->filter = NULL;
2631 err = parse_addr_filter(evsel, filter, max_nr);
2632 free(filter);
2633 if (err)
2634 return err;
2635 pr_debug("Address filter: %s\n", evsel->filter);
2636 }
2637
2638 return 0;
2639}
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03002640
2641int auxtrace__process_event(struct perf_session *session, union perf_event *event,
2642 struct perf_sample *sample, struct perf_tool *tool)
2643{
2644 if (!session->auxtrace)
2645 return 0;
2646
2647 return session->auxtrace->process_event(session, event, sample, tool);
2648}
2649
Adrian Hunterb04b8dd2019-11-15 14:42:19 +02002650void auxtrace__dump_auxtrace_sample(struct perf_session *session,
2651 struct perf_sample *sample)
2652{
2653 if (!session->auxtrace || !session->auxtrace->dump_auxtrace_sample ||
2654 auxtrace__dont_decode(session))
2655 return;
2656
2657 session->auxtrace->dump_auxtrace_sample(session, sample);
2658}
2659
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03002660int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool)
2661{
2662 if (!session->auxtrace)
2663 return 0;
2664
2665 return session->auxtrace->flush_events(session, tool);
2666}
2667
2668void auxtrace__free_events(struct perf_session *session)
2669{
2670 if (!session->auxtrace)
2671 return;
2672
2673 return session->auxtrace->free_events(session);
2674}
2675
2676void auxtrace__free(struct perf_session *session)
2677{
2678 if (!session->auxtrace)
2679 return;
2680
2681 return session->auxtrace->free(session);
2682}
Adrian Hunter853f37d2020-04-01 13:15:58 +03002683
2684bool auxtrace__evsel_is_auxtrace(struct perf_session *session,
2685 struct evsel *evsel)
2686{
2687 if (!session->auxtrace || !session->auxtrace->evsel_is_auxtrace)
2688 return false;
2689
2690 return session->auxtrace->evsel_is_auxtrace(session, evsel);
2691}