]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - tools/perf/util/auxtrace.c
perf auxtrace: Fix 'instructions' period of zero
[karo-tx-linux.git] / tools / perf / util / auxtrace.c
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
16 #include <sys/types.h>
17 #include <sys/mman.h>
18 #include <stdbool.h>
19
20 #include <linux/kernel.h>
21 #include <linux/perf_event.h>
22 #include <linux/types.h>
23 #include <linux/bitops.h>
24 #include <linux/log2.h>
25 #include <linux/string.h>
26
27 #include <sys/param.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include <linux/list.h>
34
35 #include "../perf.h"
36 #include "util.h"
37 #include "evlist.h"
38 #include "cpumap.h"
39 #include "thread_map.h"
40 #include "asm/bug.h"
41 #include "auxtrace.h"
42
43 #include <linux/hash.h>
44
45 #include "event.h"
46 #include "session.h"
47 #include "debug.h"
48 #include "parse-options.h"
49
50 #include "intel-pt.h"
51 #include "intel-bts.h"
52
53 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
54                         struct auxtrace_mmap_params *mp,
55                         void *userpg, int fd)
56 {
57         struct perf_event_mmap_page *pc = userpg;
58
59         WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
60
61         mm->userpg = userpg;
62         mm->mask = mp->mask;
63         mm->len = mp->len;
64         mm->prev = 0;
65         mm->idx = mp->idx;
66         mm->tid = mp->tid;
67         mm->cpu = mp->cpu;
68
69         if (!mp->len) {
70                 mm->base = NULL;
71                 return 0;
72         }
73
74 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
75         pr_err("Cannot use AUX area tracing mmaps\n");
76         return -1;
77 #endif
78
79         pc->aux_offset = mp->offset;
80         pc->aux_size = mp->len;
81
82         mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
83         if (mm->base == MAP_FAILED) {
84                 pr_debug2("failed to mmap AUX area\n");
85                 mm->base = NULL;
86                 return -1;
87         }
88
89         return 0;
90 }
91
92 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
93 {
94         if (mm->base) {
95                 munmap(mm->base, mm->len);
96                 mm->base = NULL;
97         }
98 }
99
100 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
101                                 off_t auxtrace_offset,
102                                 unsigned int auxtrace_pages,
103                                 bool auxtrace_overwrite)
104 {
105         if (auxtrace_pages) {
106                 mp->offset = auxtrace_offset;
107                 mp->len = auxtrace_pages * (size_t)page_size;
108                 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
109                 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
110                 pr_debug2("AUX area mmap length %zu\n", mp->len);
111         } else {
112                 mp->len = 0;
113         }
114 }
115
116 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
117                                    struct perf_evlist *evlist, int idx,
118                                    bool per_cpu)
119 {
120         mp->idx = idx;
121
122         if (per_cpu) {
123                 mp->cpu = evlist->cpus->map[idx];
124                 if (evlist->threads)
125                         mp->tid = thread_map__pid(evlist->threads, 0);
126                 else
127                         mp->tid = -1;
128         } else {
129                 mp->cpu = -1;
130                 mp->tid = thread_map__pid(evlist->threads, idx);
131         }
132 }
133
134 #define AUXTRACE_INIT_NR_QUEUES 32
135
136 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
137 {
138         struct auxtrace_queue *queue_array;
139         unsigned int max_nr_queues, i;
140
141         max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
142         if (nr_queues > max_nr_queues)
143                 return NULL;
144
145         queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
146         if (!queue_array)
147                 return NULL;
148
149         for (i = 0; i < nr_queues; i++) {
150                 INIT_LIST_HEAD(&queue_array[i].head);
151                 queue_array[i].priv = NULL;
152         }
153
154         return queue_array;
155 }
156
157 int auxtrace_queues__init(struct auxtrace_queues *queues)
158 {
159         queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
160         queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
161         if (!queues->queue_array)
162                 return -ENOMEM;
163         return 0;
164 }
165
166 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
167                                  unsigned int new_nr_queues)
168 {
169         unsigned int nr_queues = queues->nr_queues;
170         struct auxtrace_queue *queue_array;
171         unsigned int i;
172
173         if (!nr_queues)
174                 nr_queues = AUXTRACE_INIT_NR_QUEUES;
175
176         while (nr_queues && nr_queues < new_nr_queues)
177                 nr_queues <<= 1;
178
179         if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
180                 return -EINVAL;
181
182         queue_array = auxtrace_alloc_queue_array(nr_queues);
183         if (!queue_array)
184                 return -ENOMEM;
185
186         for (i = 0; i < queues->nr_queues; i++) {
187                 list_splice_tail(&queues->queue_array[i].head,
188                                  &queue_array[i].head);
189                 queue_array[i].priv = queues->queue_array[i].priv;
190         }
191
192         queues->nr_queues = nr_queues;
193         queues->queue_array = queue_array;
194
195         return 0;
196 }
197
198 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
199 {
200         int fd = perf_data_file__fd(session->file);
201         void *p;
202         ssize_t ret;
203
204         if (size > SSIZE_MAX)
205                 return NULL;
206
207         p = malloc(size);
208         if (!p)
209                 return NULL;
210
211         ret = readn(fd, p, size);
212         if (ret != (ssize_t)size) {
213                 free(p);
214                 return NULL;
215         }
216
217         return p;
218 }
219
220 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
221                                        unsigned int idx,
222                                        struct auxtrace_buffer *buffer)
223 {
224         struct auxtrace_queue *queue;
225         int err;
226
227         if (idx >= queues->nr_queues) {
228                 err = auxtrace_queues__grow(queues, idx + 1);
229                 if (err)
230                         return err;
231         }
232
233         queue = &queues->queue_array[idx];
234
235         if (!queue->set) {
236                 queue->set = true;
237                 queue->tid = buffer->tid;
238                 queue->cpu = buffer->cpu;
239         } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
240                 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
241                        queue->cpu, queue->tid, buffer->cpu, buffer->tid);
242                 return -EINVAL;
243         }
244
245         buffer->buffer_nr = queues->next_buffer_nr++;
246
247         list_add_tail(&buffer->list, &queue->head);
248
249         queues->new_data = true;
250         queues->populated = true;
251
252         return 0;
253 }
254
255 /* Limit buffers to 32MiB on 32-bit */
256 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
257
258 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
259                                          unsigned int idx,
260                                          struct auxtrace_buffer *buffer)
261 {
262         u64 sz = buffer->size;
263         bool consecutive = false;
264         struct auxtrace_buffer *b;
265         int err;
266
267         while (sz > BUFFER_LIMIT_FOR_32_BIT) {
268                 b = memdup(buffer, sizeof(struct auxtrace_buffer));
269                 if (!b)
270                         return -ENOMEM;
271                 b->size = BUFFER_LIMIT_FOR_32_BIT;
272                 b->consecutive = consecutive;
273                 err = auxtrace_queues__add_buffer(queues, idx, b);
274                 if (err) {
275                         auxtrace_buffer__free(b);
276                         return err;
277                 }
278                 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
279                 sz -= BUFFER_LIMIT_FOR_32_BIT;
280                 consecutive = true;
281         }
282
283         buffer->size = sz;
284         buffer->consecutive = consecutive;
285
286         return 0;
287 }
288
289 static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
290                                              struct perf_session *session,
291                                              unsigned int idx,
292                                              struct auxtrace_buffer *buffer)
293 {
294         if (session->one_mmap) {
295                 buffer->data = buffer->data_offset - session->one_mmap_offset +
296                                session->one_mmap_addr;
297         } else if (perf_data_file__is_pipe(session->file)) {
298                 buffer->data = auxtrace_copy_data(buffer->size, session);
299                 if (!buffer->data)
300                         return -ENOMEM;
301                 buffer->data_needs_freeing = true;
302         } else if (BITS_PER_LONG == 32 &&
303                    buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
304                 int err;
305
306                 err = auxtrace_queues__split_buffer(queues, idx, buffer);
307                 if (err)
308                         return err;
309         }
310
311         return auxtrace_queues__add_buffer(queues, idx, buffer);
312 }
313
314 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
315                                struct perf_session *session,
316                                union perf_event *event, off_t data_offset,
317                                struct auxtrace_buffer **buffer_ptr)
318 {
319         struct auxtrace_buffer *buffer;
320         unsigned int idx;
321         int err;
322
323         buffer = zalloc(sizeof(struct auxtrace_buffer));
324         if (!buffer)
325                 return -ENOMEM;
326
327         buffer->pid = -1;
328         buffer->tid = event->auxtrace.tid;
329         buffer->cpu = event->auxtrace.cpu;
330         buffer->data_offset = data_offset;
331         buffer->offset = event->auxtrace.offset;
332         buffer->reference = event->auxtrace.reference;
333         buffer->size = event->auxtrace.size;
334         idx = event->auxtrace.idx;
335
336         err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
337         if (err)
338                 goto out_err;
339
340         if (buffer_ptr)
341                 *buffer_ptr = buffer;
342
343         return 0;
344
345 out_err:
346         auxtrace_buffer__free(buffer);
347         return err;
348 }
349
350 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
351                                               struct perf_session *session,
352                                               off_t file_offset, size_t sz)
353 {
354         union perf_event *event;
355         int err;
356         char buf[PERF_SAMPLE_MAX_SIZE];
357
358         err = perf_session__peek_event(session, file_offset, buf,
359                                        PERF_SAMPLE_MAX_SIZE, &event, NULL);
360         if (err)
361                 return err;
362
363         if (event->header.type == PERF_RECORD_AUXTRACE) {
364                 if (event->header.size < sizeof(struct auxtrace_event) ||
365                     event->header.size != sz) {
366                         err = -EINVAL;
367                         goto out;
368                 }
369                 file_offset += event->header.size;
370                 err = auxtrace_queues__add_event(queues, session, event,
371                                                  file_offset, NULL);
372         }
373 out:
374         return err;
375 }
376
377 void auxtrace_queues__free(struct auxtrace_queues *queues)
378 {
379         unsigned int i;
380
381         for (i = 0; i < queues->nr_queues; i++) {
382                 while (!list_empty(&queues->queue_array[i].head)) {
383                         struct auxtrace_buffer *buffer;
384
385                         buffer = list_entry(queues->queue_array[i].head.next,
386                                             struct auxtrace_buffer, list);
387                         list_del(&buffer->list);
388                         auxtrace_buffer__free(buffer);
389                 }
390         }
391
392         zfree(&queues->queue_array);
393         queues->nr_queues = 0;
394 }
395
396 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
397                              unsigned int pos, unsigned int queue_nr,
398                              u64 ordinal)
399 {
400         unsigned int parent;
401
402         while (pos) {
403                 parent = (pos - 1) >> 1;
404                 if (heap_array[parent].ordinal <= ordinal)
405                         break;
406                 heap_array[pos] = heap_array[parent];
407                 pos = parent;
408         }
409         heap_array[pos].queue_nr = queue_nr;
410         heap_array[pos].ordinal = ordinal;
411 }
412
413 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
414                        u64 ordinal)
415 {
416         struct auxtrace_heap_item *heap_array;
417
418         if (queue_nr >= heap->heap_sz) {
419                 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
420
421                 while (heap_sz <= queue_nr)
422                         heap_sz <<= 1;
423                 heap_array = realloc(heap->heap_array,
424                                      heap_sz * sizeof(struct auxtrace_heap_item));
425                 if (!heap_array)
426                         return -ENOMEM;
427                 heap->heap_array = heap_array;
428                 heap->heap_sz = heap_sz;
429         }
430
431         auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
432
433         return 0;
434 }
435
436 void auxtrace_heap__free(struct auxtrace_heap *heap)
437 {
438         zfree(&heap->heap_array);
439         heap->heap_cnt = 0;
440         heap->heap_sz = 0;
441 }
442
443 void auxtrace_heap__pop(struct auxtrace_heap *heap)
444 {
445         unsigned int pos, last, heap_cnt = heap->heap_cnt;
446         struct auxtrace_heap_item *heap_array;
447
448         if (!heap_cnt)
449                 return;
450
451         heap->heap_cnt -= 1;
452
453         heap_array = heap->heap_array;
454
455         pos = 0;
456         while (1) {
457                 unsigned int left, right;
458
459                 left = (pos << 1) + 1;
460                 if (left >= heap_cnt)
461                         break;
462                 right = left + 1;
463                 if (right >= heap_cnt) {
464                         heap_array[pos] = heap_array[left];
465                         return;
466                 }
467                 if (heap_array[left].ordinal < heap_array[right].ordinal) {
468                         heap_array[pos] = heap_array[left];
469                         pos = left;
470                 } else {
471                         heap_array[pos] = heap_array[right];
472                         pos = right;
473                 }
474         }
475
476         last = heap_cnt - 1;
477         auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
478                          heap_array[last].ordinal);
479 }
480
481 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr)
482 {
483         if (itr)
484                 return itr->info_priv_size(itr);
485         return 0;
486 }
487
488 static int auxtrace_not_supported(void)
489 {
490         pr_err("AUX area tracing is not supported on this architecture\n");
491         return -EINVAL;
492 }
493
494 int auxtrace_record__info_fill(struct auxtrace_record *itr,
495                                struct perf_session *session,
496                                struct auxtrace_info_event *auxtrace_info,
497                                size_t priv_size)
498 {
499         if (itr)
500                 return itr->info_fill(itr, session, auxtrace_info, priv_size);
501         return auxtrace_not_supported();
502 }
503
504 void auxtrace_record__free(struct auxtrace_record *itr)
505 {
506         if (itr)
507                 itr->free(itr);
508 }
509
510 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
511 {
512         if (itr && itr->snapshot_start)
513                 return itr->snapshot_start(itr);
514         return 0;
515 }
516
517 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
518 {
519         if (itr && itr->snapshot_finish)
520                 return itr->snapshot_finish(itr);
521         return 0;
522 }
523
524 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
525                                    struct auxtrace_mmap *mm,
526                                    unsigned char *data, u64 *head, u64 *old)
527 {
528         if (itr && itr->find_snapshot)
529                 return itr->find_snapshot(itr, idx, mm, data, head, old);
530         return 0;
531 }
532
533 int auxtrace_record__options(struct auxtrace_record *itr,
534                              struct perf_evlist *evlist,
535                              struct record_opts *opts)
536 {
537         if (itr)
538                 return itr->recording_options(itr, evlist, opts);
539         return 0;
540 }
541
542 u64 auxtrace_record__reference(struct auxtrace_record *itr)
543 {
544         if (itr)
545                 return itr->reference(itr);
546         return 0;
547 }
548
549 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
550                                     struct record_opts *opts, const char *str)
551 {
552         if (!str)
553                 return 0;
554
555         if (itr)
556                 return itr->parse_snapshot_options(itr, opts, str);
557
558         pr_err("No AUX area tracing to snapshot\n");
559         return -EINVAL;
560 }
561
562 struct auxtrace_record *__weak
563 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
564 {
565         *err = 0;
566         return NULL;
567 }
568
569 static int auxtrace_index__alloc(struct list_head *head)
570 {
571         struct auxtrace_index *auxtrace_index;
572
573         auxtrace_index = malloc(sizeof(struct auxtrace_index));
574         if (!auxtrace_index)
575                 return -ENOMEM;
576
577         auxtrace_index->nr = 0;
578         INIT_LIST_HEAD(&auxtrace_index->list);
579
580         list_add_tail(&auxtrace_index->list, head);
581
582         return 0;
583 }
584
585 void auxtrace_index__free(struct list_head *head)
586 {
587         struct auxtrace_index *auxtrace_index, *n;
588
589         list_for_each_entry_safe(auxtrace_index, n, head, list) {
590                 list_del(&auxtrace_index->list);
591                 free(auxtrace_index);
592         }
593 }
594
595 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
596 {
597         struct auxtrace_index *auxtrace_index;
598         int err;
599
600         if (list_empty(head)) {
601                 err = auxtrace_index__alloc(head);
602                 if (err)
603                         return NULL;
604         }
605
606         auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
607
608         if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
609                 err = auxtrace_index__alloc(head);
610                 if (err)
611                         return NULL;
612                 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
613                                             list);
614         }
615
616         return auxtrace_index;
617 }
618
619 int auxtrace_index__auxtrace_event(struct list_head *head,
620                                    union perf_event *event, off_t file_offset)
621 {
622         struct auxtrace_index *auxtrace_index;
623         size_t nr;
624
625         auxtrace_index = auxtrace_index__last(head);
626         if (!auxtrace_index)
627                 return -ENOMEM;
628
629         nr = auxtrace_index->nr;
630         auxtrace_index->entries[nr].file_offset = file_offset;
631         auxtrace_index->entries[nr].sz = event->header.size;
632         auxtrace_index->nr += 1;
633
634         return 0;
635 }
636
637 static int auxtrace_index__do_write(int fd,
638                                     struct auxtrace_index *auxtrace_index)
639 {
640         struct auxtrace_index_entry ent;
641         size_t i;
642
643         for (i = 0; i < auxtrace_index->nr; i++) {
644                 ent.file_offset = auxtrace_index->entries[i].file_offset;
645                 ent.sz = auxtrace_index->entries[i].sz;
646                 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
647                         return -errno;
648         }
649         return 0;
650 }
651
652 int auxtrace_index__write(int fd, struct list_head *head)
653 {
654         struct auxtrace_index *auxtrace_index;
655         u64 total = 0;
656         int err;
657
658         list_for_each_entry(auxtrace_index, head, list)
659                 total += auxtrace_index->nr;
660
661         if (writen(fd, &total, sizeof(total)) != sizeof(total))
662                 return -errno;
663
664         list_for_each_entry(auxtrace_index, head, list) {
665                 err = auxtrace_index__do_write(fd, auxtrace_index);
666                 if (err)
667                         return err;
668         }
669
670         return 0;
671 }
672
673 static int auxtrace_index__process_entry(int fd, struct list_head *head,
674                                          bool needs_swap)
675 {
676         struct auxtrace_index *auxtrace_index;
677         struct auxtrace_index_entry ent;
678         size_t nr;
679
680         if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
681                 return -1;
682
683         auxtrace_index = auxtrace_index__last(head);
684         if (!auxtrace_index)
685                 return -1;
686
687         nr = auxtrace_index->nr;
688         if (needs_swap) {
689                 auxtrace_index->entries[nr].file_offset =
690                                                 bswap_64(ent.file_offset);
691                 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
692         } else {
693                 auxtrace_index->entries[nr].file_offset = ent.file_offset;
694                 auxtrace_index->entries[nr].sz = ent.sz;
695         }
696
697         auxtrace_index->nr = nr + 1;
698
699         return 0;
700 }
701
702 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
703                             bool needs_swap)
704 {
705         struct list_head *head = &session->auxtrace_index;
706         u64 nr;
707
708         if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
709                 return -1;
710
711         if (needs_swap)
712                 nr = bswap_64(nr);
713
714         if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
715                 return -1;
716
717         while (nr--) {
718                 int err;
719
720                 err = auxtrace_index__process_entry(fd, head, needs_swap);
721                 if (err)
722                         return -1;
723         }
724
725         return 0;
726 }
727
728 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
729                                                 struct perf_session *session,
730                                                 struct auxtrace_index_entry *ent)
731 {
732         return auxtrace_queues__add_indexed_event(queues, session,
733                                                   ent->file_offset, ent->sz);
734 }
735
736 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
737                                    struct perf_session *session)
738 {
739         struct auxtrace_index *auxtrace_index;
740         struct auxtrace_index_entry *ent;
741         size_t i;
742         int err;
743
744         list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
745                 for (i = 0; i < auxtrace_index->nr; i++) {
746                         ent = &auxtrace_index->entries[i];
747                         err = auxtrace_queues__process_index_entry(queues,
748                                                                    session,
749                                                                    ent);
750                         if (err)
751                                 return err;
752                 }
753         }
754         return 0;
755 }
756
757 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
758                                               struct auxtrace_buffer *buffer)
759 {
760         if (buffer) {
761                 if (list_is_last(&buffer->list, &queue->head))
762                         return NULL;
763                 return list_entry(buffer->list.next, struct auxtrace_buffer,
764                                   list);
765         } else {
766                 if (list_empty(&queue->head))
767                         return NULL;
768                 return list_entry(queue->head.next, struct auxtrace_buffer,
769                                   list);
770         }
771 }
772
773 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
774 {
775         size_t adj = buffer->data_offset & (page_size - 1);
776         size_t size = buffer->size + adj;
777         off_t file_offset = buffer->data_offset - adj;
778         void *addr;
779
780         if (buffer->data)
781                 return buffer->data;
782
783         addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
784         if (addr == MAP_FAILED)
785                 return NULL;
786
787         buffer->mmap_addr = addr;
788         buffer->mmap_size = size;
789
790         buffer->data = addr + adj;
791
792         return buffer->data;
793 }
794
795 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
796 {
797         if (!buffer->data || !buffer->mmap_addr)
798                 return;
799         munmap(buffer->mmap_addr, buffer->mmap_size);
800         buffer->mmap_addr = NULL;
801         buffer->mmap_size = 0;
802         buffer->data = NULL;
803         buffer->use_data = NULL;
804 }
805
806 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
807 {
808         auxtrace_buffer__put_data(buffer);
809         if (buffer->data_needs_freeing) {
810                 buffer->data_needs_freeing = false;
811                 zfree(&buffer->data);
812                 buffer->use_data = NULL;
813                 buffer->size = 0;
814         }
815 }
816
817 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
818 {
819         auxtrace_buffer__drop_data(buffer);
820         free(buffer);
821 }
822
823 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
824                           int code, int cpu, pid_t pid, pid_t tid, u64 ip,
825                           const char *msg)
826 {
827         size_t size;
828
829         memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
830
831         auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
832         auxtrace_error->type = type;
833         auxtrace_error->code = code;
834         auxtrace_error->cpu = cpu;
835         auxtrace_error->pid = pid;
836         auxtrace_error->tid = tid;
837         auxtrace_error->ip = ip;
838         strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
839
840         size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
841                strlen(auxtrace_error->msg) + 1;
842         auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
843 }
844
845 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
846                                          struct perf_tool *tool,
847                                          struct perf_session *session,
848                                          perf_event__handler_t process)
849 {
850         union perf_event *ev;
851         size_t priv_size;
852         int err;
853
854         pr_debug2("Synthesizing auxtrace information\n");
855         priv_size = auxtrace_record__info_priv_size(itr);
856         ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
857         if (!ev)
858                 return -ENOMEM;
859
860         ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
861         ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
862                                         priv_size;
863         err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
864                                          priv_size);
865         if (err)
866                 goto out_free;
867
868         err = process(tool, ev, NULL, NULL);
869 out_free:
870         free(ev);
871         return err;
872 }
873
874 static bool auxtrace__dont_decode(struct perf_session *session)
875 {
876         return !session->itrace_synth_opts ||
877                session->itrace_synth_opts->dont_decode;
878 }
879
880 int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
881                                       union perf_event *event,
882                                       struct perf_session *session)
883 {
884         enum auxtrace_type type = event->auxtrace_info.type;
885
886         if (dump_trace)
887                 fprintf(stdout, " type: %u\n", type);
888
889         switch (type) {
890         case PERF_AUXTRACE_INTEL_PT:
891                 return intel_pt_process_auxtrace_info(event, session);
892         case PERF_AUXTRACE_INTEL_BTS:
893                 return intel_bts_process_auxtrace_info(event, session);
894         case PERF_AUXTRACE_UNKNOWN:
895         default:
896                 return -EINVAL;
897         }
898 }
899
900 s64 perf_event__process_auxtrace(struct perf_tool *tool,
901                                  union perf_event *event,
902                                  struct perf_session *session)
903 {
904         s64 err;
905
906         if (dump_trace)
907                 fprintf(stdout, " size: %#"PRIx64"  offset: %#"PRIx64"  ref: %#"PRIx64"  idx: %u  tid: %d  cpu: %d\n",
908                         event->auxtrace.size, event->auxtrace.offset,
909                         event->auxtrace.reference, event->auxtrace.idx,
910                         event->auxtrace.tid, event->auxtrace.cpu);
911
912         if (auxtrace__dont_decode(session))
913                 return event->auxtrace.size;
914
915         if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
916                 return -EINVAL;
917
918         err = session->auxtrace->process_auxtrace_event(session, event, tool);
919         if (err < 0)
920                 return err;
921
922         return event->auxtrace.size;
923 }
924
925 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE         PERF_ITRACE_PERIOD_NANOSECS
926 #define PERF_ITRACE_DEFAULT_PERIOD              100000
927 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ        16
928 #define PERF_ITRACE_MAX_CALLCHAIN_SZ            1024
929
930 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
931 {
932         synth_opts->instructions = true;
933         synth_opts->branches = true;
934         synth_opts->transactions = true;
935         synth_opts->errors = true;
936         synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
937         synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
938         synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
939 }
940
941 /*
942  * Please check tools/perf/Documentation/perf-script.txt for information
943  * about the options parsed here, which is introduced after this cset,
944  * when support in 'perf script' for these options is introduced.
945  */
946 int itrace_parse_synth_opts(const struct option *opt, const char *str,
947                             int unset)
948 {
949         struct itrace_synth_opts *synth_opts = opt->value;
950         const char *p;
951         char *endptr;
952         bool period_type_set = false;
953         bool period_set = false;
954
955         synth_opts->set = true;
956
957         if (unset) {
958                 synth_opts->dont_decode = true;
959                 return 0;
960         }
961
962         if (!str) {
963                 itrace_synth_opts__set_default(synth_opts);
964                 return 0;
965         }
966
967         for (p = str; *p;) {
968                 switch (*p++) {
969                 case 'i':
970                         synth_opts->instructions = true;
971                         while (*p == ' ' || *p == ',')
972                                 p += 1;
973                         if (isdigit(*p)) {
974                                 synth_opts->period = strtoull(p, &endptr, 10);
975                                 period_set = true;
976                                 p = endptr;
977                                 while (*p == ' ' || *p == ',')
978                                         p += 1;
979                                 switch (*p++) {
980                                 case 'i':
981                                         synth_opts->period_type =
982                                                 PERF_ITRACE_PERIOD_INSTRUCTIONS;
983                                         period_type_set = true;
984                                         break;
985                                 case 't':
986                                         synth_opts->period_type =
987                                                 PERF_ITRACE_PERIOD_TICKS;
988                                         period_type_set = true;
989                                         break;
990                                 case 'm':
991                                         synth_opts->period *= 1000;
992                                         /* Fall through */
993                                 case 'u':
994                                         synth_opts->period *= 1000;
995                                         /* Fall through */
996                                 case 'n':
997                                         if (*p++ != 's')
998                                                 goto out_err;
999                                         synth_opts->period_type =
1000                                                 PERF_ITRACE_PERIOD_NANOSECS;
1001                                         period_type_set = true;
1002                                         break;
1003                                 case '\0':
1004                                         goto out;
1005                                 default:
1006                                         goto out_err;
1007                                 }
1008                         }
1009                         break;
1010                 case 'b':
1011                         synth_opts->branches = true;
1012                         break;
1013                 case 'x':
1014                         synth_opts->transactions = true;
1015                         break;
1016                 case 'e':
1017                         synth_opts->errors = true;
1018                         break;
1019                 case 'd':
1020                         synth_opts->log = true;
1021                         break;
1022                 case 'c':
1023                         synth_opts->branches = true;
1024                         synth_opts->calls = true;
1025                         break;
1026                 case 'r':
1027                         synth_opts->branches = true;
1028                         synth_opts->returns = true;
1029                         break;
1030                 case 'g':
1031                         synth_opts->callchain = true;
1032                         synth_opts->callchain_sz =
1033                                         PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1034                         while (*p == ' ' || *p == ',')
1035                                 p += 1;
1036                         if (isdigit(*p)) {
1037                                 unsigned int val;
1038
1039                                 val = strtoul(p, &endptr, 10);
1040                                 p = endptr;
1041                                 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1042                                         goto out_err;
1043                                 synth_opts->callchain_sz = val;
1044                         }
1045                         break;
1046                 case ' ':
1047                 case ',':
1048                         break;
1049                 default:
1050                         goto out_err;
1051                 }
1052         }
1053 out:
1054         if (synth_opts->instructions) {
1055                 if (!period_type_set)
1056                         synth_opts->period_type =
1057                                         PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1058                 if (!period_set)
1059                         synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1060         }
1061
1062         return 0;
1063
1064 out_err:
1065         pr_err("Bad Instruction Tracing options '%s'\n", str);
1066         return -EINVAL;
1067 }
1068
1069 static const char * const auxtrace_error_type_name[] = {
1070         [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1071 };
1072
1073 static const char *auxtrace_error_name(int type)
1074 {
1075         const char *error_type_name = NULL;
1076
1077         if (type < PERF_AUXTRACE_ERROR_MAX)
1078                 error_type_name = auxtrace_error_type_name[type];
1079         if (!error_type_name)
1080                 error_type_name = "unknown AUX";
1081         return error_type_name;
1082 }
1083
1084 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1085 {
1086         struct auxtrace_error_event *e = &event->auxtrace_error;
1087         int ret;
1088
1089         ret = fprintf(fp, " %s error type %u",
1090                       auxtrace_error_name(e->type), e->type);
1091         ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1092                        e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1093         return ret;
1094 }
1095
1096 void perf_session__auxtrace_error_inc(struct perf_session *session,
1097                                       union perf_event *event)
1098 {
1099         struct auxtrace_error_event *e = &event->auxtrace_error;
1100
1101         if (e->type < PERF_AUXTRACE_ERROR_MAX)
1102                 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1103 }
1104
1105 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1106 {
1107         int i;
1108
1109         for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1110                 if (!stats->nr_auxtrace_errors[i])
1111                         continue;
1112                 ui__warning("%u %s errors\n",
1113                             stats->nr_auxtrace_errors[i],
1114                             auxtrace_error_name(i));
1115         }
1116 }
1117
1118 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1119                                        union perf_event *event,
1120                                        struct perf_session *session)
1121 {
1122         if (auxtrace__dont_decode(session))
1123                 return 0;
1124
1125         perf_event__fprintf_auxtrace_error(event, stdout);
1126         return 0;
1127 }
1128
1129 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1130                                  struct auxtrace_record *itr,
1131                                  struct perf_tool *tool, process_auxtrace_t fn,
1132                                  bool snapshot, size_t snapshot_size)
1133 {
1134         u64 head, old = mm->prev, offset, ref;
1135         unsigned char *data = mm->base;
1136         size_t size, head_off, old_off, len1, len2, padding;
1137         union perf_event ev;
1138         void *data1, *data2;
1139
1140         if (snapshot) {
1141                 head = auxtrace_mmap__read_snapshot_head(mm);
1142                 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1143                                                    &head, &old))
1144                         return -1;
1145         } else {
1146                 head = auxtrace_mmap__read_head(mm);
1147         }
1148
1149         if (old == head)
1150                 return 0;
1151
1152         pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1153                   mm->idx, old, head, head - old);
1154
1155         if (mm->mask) {
1156                 head_off = head & mm->mask;
1157                 old_off = old & mm->mask;
1158         } else {
1159                 head_off = head % mm->len;
1160                 old_off = old % mm->len;
1161         }
1162
1163         if (head_off > old_off)
1164                 size = head_off - old_off;
1165         else
1166                 size = mm->len - (old_off - head_off);
1167
1168         if (snapshot && size > snapshot_size)
1169                 size = snapshot_size;
1170
1171         ref = auxtrace_record__reference(itr);
1172
1173         if (head > old || size <= head || mm->mask) {
1174                 offset = head - size;
1175         } else {
1176                 /*
1177                  * When the buffer size is not a power of 2, 'head' wraps at the
1178                  * highest multiple of the buffer size, so we have to subtract
1179                  * the remainder here.
1180                  */
1181                 u64 rem = (0ULL - mm->len) % mm->len;
1182
1183                 offset = head - size - rem;
1184         }
1185
1186         if (size > head_off) {
1187                 len1 = size - head_off;
1188                 data1 = &data[mm->len - len1];
1189                 len2 = head_off;
1190                 data2 = &data[0];
1191         } else {
1192                 len1 = size;
1193                 data1 = &data[head_off - len1];
1194                 len2 = 0;
1195                 data2 = NULL;
1196         }
1197
1198         if (itr->alignment) {
1199                 unsigned int unwanted = len1 % itr->alignment;
1200
1201                 len1 -= unwanted;
1202                 size -= unwanted;
1203         }
1204
1205         /* padding must be written by fn() e.g. record__process_auxtrace() */
1206         padding = size & 7;
1207         if (padding)
1208                 padding = 8 - padding;
1209
1210         memset(&ev, 0, sizeof(ev));
1211         ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1212         ev.auxtrace.header.size = sizeof(ev.auxtrace);
1213         ev.auxtrace.size = size + padding;
1214         ev.auxtrace.offset = offset;
1215         ev.auxtrace.reference = ref;
1216         ev.auxtrace.idx = mm->idx;
1217         ev.auxtrace.tid = mm->tid;
1218         ev.auxtrace.cpu = mm->cpu;
1219
1220         if (fn(tool, &ev, data1, len1, data2, len2))
1221                 return -1;
1222
1223         mm->prev = head;
1224
1225         if (!snapshot) {
1226                 auxtrace_mmap__write_tail(mm, head);
1227                 if (itr->read_finish) {
1228                         int err;
1229
1230                         err = itr->read_finish(itr, mm->idx);
1231                         if (err < 0)
1232                                 return err;
1233                 }
1234         }
1235
1236         return 1;
1237 }
1238
1239 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1240                         struct perf_tool *tool, process_auxtrace_t fn)
1241 {
1242         return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1243 }
1244
1245 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1246                                  struct auxtrace_record *itr,
1247                                  struct perf_tool *tool, process_auxtrace_t fn,
1248                                  size_t snapshot_size)
1249 {
1250         return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1251 }
1252
1253 /**
1254  * struct auxtrace_cache - hash table to implement a cache
1255  * @hashtable: the hashtable
1256  * @sz: hashtable size (number of hlists)
1257  * @entry_size: size of an entry
1258  * @limit: limit the number of entries to this maximum, when reached the cache
1259  *         is dropped and caching begins again with an empty cache
1260  * @cnt: current number of entries
1261  * @bits: hashtable size (@sz = 2^@bits)
1262  */
1263 struct auxtrace_cache {
1264         struct hlist_head *hashtable;
1265         size_t sz;
1266         size_t entry_size;
1267         size_t limit;
1268         size_t cnt;
1269         unsigned int bits;
1270 };
1271
1272 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1273                                            unsigned int limit_percent)
1274 {
1275         struct auxtrace_cache *c;
1276         struct hlist_head *ht;
1277         size_t sz, i;
1278
1279         c = zalloc(sizeof(struct auxtrace_cache));
1280         if (!c)
1281                 return NULL;
1282
1283         sz = 1UL << bits;
1284
1285         ht = calloc(sz, sizeof(struct hlist_head));
1286         if (!ht)
1287                 goto out_free;
1288
1289         for (i = 0; i < sz; i++)
1290                 INIT_HLIST_HEAD(&ht[i]);
1291
1292         c->hashtable = ht;
1293         c->sz = sz;
1294         c->entry_size = entry_size;
1295         c->limit = (c->sz * limit_percent) / 100;
1296         c->bits = bits;
1297
1298         return c;
1299
1300 out_free:
1301         free(c);
1302         return NULL;
1303 }
1304
1305 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1306 {
1307         struct auxtrace_cache_entry *entry;
1308         struct hlist_node *tmp;
1309         size_t i;
1310
1311         if (!c)
1312                 return;
1313
1314         for (i = 0; i < c->sz; i++) {
1315                 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1316                         hlist_del(&entry->hash);
1317                         auxtrace_cache__free_entry(c, entry);
1318                 }
1319         }
1320
1321         c->cnt = 0;
1322 }
1323
1324 void auxtrace_cache__free(struct auxtrace_cache *c)
1325 {
1326         if (!c)
1327                 return;
1328
1329         auxtrace_cache__drop(c);
1330         free(c->hashtable);
1331         free(c);
1332 }
1333
1334 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1335 {
1336         return malloc(c->entry_size);
1337 }
1338
1339 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1340                                 void *entry)
1341 {
1342         free(entry);
1343 }
1344
1345 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1346                         struct auxtrace_cache_entry *entry)
1347 {
1348         if (c->limit && ++c->cnt > c->limit)
1349                 auxtrace_cache__drop(c);
1350
1351         entry->key = key;
1352         hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1353
1354         return 0;
1355 }
1356
1357 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1358 {
1359         struct auxtrace_cache_entry *entry;
1360         struct hlist_head *hlist;
1361
1362         if (!c)
1363                 return NULL;
1364
1365         hlist = &c->hashtable[hash_32(key, c->bits)];
1366         hlist_for_each_entry(entry, hlist, hash) {
1367                 if (entry->key == key)
1368                         return entry;
1369         }
1370
1371         return NULL;
1372 }