]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - tools/perf/util/auxtrace.c
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[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
954         synth_opts->set = true;
955
956         if (unset) {
957                 synth_opts->dont_decode = true;
958                 return 0;
959         }
960
961         if (!str) {
962                 itrace_synth_opts__set_default(synth_opts);
963                 return 0;
964         }
965
966         for (p = str; *p;) {
967                 switch (*p++) {
968                 case 'i':
969                         synth_opts->instructions = true;
970                         while (*p == ' ' || *p == ',')
971                                 p += 1;
972                         if (isdigit(*p)) {
973                                 synth_opts->period = strtoull(p, &endptr, 10);
974                                 p = endptr;
975                                 while (*p == ' ' || *p == ',')
976                                         p += 1;
977                                 switch (*p++) {
978                                 case 'i':
979                                         synth_opts->period_type =
980                                                 PERF_ITRACE_PERIOD_INSTRUCTIONS;
981                                         period_type_set = true;
982                                         break;
983                                 case 't':
984                                         synth_opts->period_type =
985                                                 PERF_ITRACE_PERIOD_TICKS;
986                                         period_type_set = true;
987                                         break;
988                                 case 'm':
989                                         synth_opts->period *= 1000;
990                                         /* Fall through */
991                                 case 'u':
992                                         synth_opts->period *= 1000;
993                                         /* Fall through */
994                                 case 'n':
995                                         if (*p++ != 's')
996                                                 goto out_err;
997                                         synth_opts->period_type =
998                                                 PERF_ITRACE_PERIOD_NANOSECS;
999                                         period_type_set = true;
1000                                         break;
1001                                 case '\0':
1002                                         goto out;
1003                                 default:
1004                                         goto out_err;
1005                                 }
1006                         }
1007                         break;
1008                 case 'b':
1009                         synth_opts->branches = true;
1010                         break;
1011                 case 'x':
1012                         synth_opts->transactions = true;
1013                         break;
1014                 case 'e':
1015                         synth_opts->errors = true;
1016                         break;
1017                 case 'd':
1018                         synth_opts->log = true;
1019                         break;
1020                 case 'c':
1021                         synth_opts->branches = true;
1022                         synth_opts->calls = true;
1023                         break;
1024                 case 'r':
1025                         synth_opts->branches = true;
1026                         synth_opts->returns = true;
1027                         break;
1028                 case 'g':
1029                         synth_opts->callchain = true;
1030                         synth_opts->callchain_sz =
1031                                         PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1032                         while (*p == ' ' || *p == ',')
1033                                 p += 1;
1034                         if (isdigit(*p)) {
1035                                 unsigned int val;
1036
1037                                 val = strtoul(p, &endptr, 10);
1038                                 p = endptr;
1039                                 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1040                                         goto out_err;
1041                                 synth_opts->callchain_sz = val;
1042                         }
1043                         break;
1044                 case ' ':
1045                 case ',':
1046                         break;
1047                 default:
1048                         goto out_err;
1049                 }
1050         }
1051 out:
1052         if (synth_opts->instructions) {
1053                 if (!period_type_set)
1054                         synth_opts->period_type =
1055                                         PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1056                 if (!synth_opts->period)
1057                         synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1058         }
1059
1060         return 0;
1061
1062 out_err:
1063         pr_err("Bad Instruction Tracing options '%s'\n", str);
1064         return -EINVAL;
1065 }
1066
1067 static const char * const auxtrace_error_type_name[] = {
1068         [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1069 };
1070
1071 static const char *auxtrace_error_name(int type)
1072 {
1073         const char *error_type_name = NULL;
1074
1075         if (type < PERF_AUXTRACE_ERROR_MAX)
1076                 error_type_name = auxtrace_error_type_name[type];
1077         if (!error_type_name)
1078                 error_type_name = "unknown AUX";
1079         return error_type_name;
1080 }
1081
1082 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1083 {
1084         struct auxtrace_error_event *e = &event->auxtrace_error;
1085         int ret;
1086
1087         ret = fprintf(fp, " %s error type %u",
1088                       auxtrace_error_name(e->type), e->type);
1089         ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1090                        e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1091         return ret;
1092 }
1093
1094 void perf_session__auxtrace_error_inc(struct perf_session *session,
1095                                       union perf_event *event)
1096 {
1097         struct auxtrace_error_event *e = &event->auxtrace_error;
1098
1099         if (e->type < PERF_AUXTRACE_ERROR_MAX)
1100                 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1101 }
1102
1103 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1104 {
1105         int i;
1106
1107         for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1108                 if (!stats->nr_auxtrace_errors[i])
1109                         continue;
1110                 ui__warning("%u %s errors\n",
1111                             stats->nr_auxtrace_errors[i],
1112                             auxtrace_error_name(i));
1113         }
1114 }
1115
1116 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1117                                        union perf_event *event,
1118                                        struct perf_session *session)
1119 {
1120         if (auxtrace__dont_decode(session))
1121                 return 0;
1122
1123         perf_event__fprintf_auxtrace_error(event, stdout);
1124         return 0;
1125 }
1126
1127 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1128                                  struct auxtrace_record *itr,
1129                                  struct perf_tool *tool, process_auxtrace_t fn,
1130                                  bool snapshot, size_t snapshot_size)
1131 {
1132         u64 head, old = mm->prev, offset, ref;
1133         unsigned char *data = mm->base;
1134         size_t size, head_off, old_off, len1, len2, padding;
1135         union perf_event ev;
1136         void *data1, *data2;
1137
1138         if (snapshot) {
1139                 head = auxtrace_mmap__read_snapshot_head(mm);
1140                 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1141                                                    &head, &old))
1142                         return -1;
1143         } else {
1144                 head = auxtrace_mmap__read_head(mm);
1145         }
1146
1147         if (old == head)
1148                 return 0;
1149
1150         pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1151                   mm->idx, old, head, head - old);
1152
1153         if (mm->mask) {
1154                 head_off = head & mm->mask;
1155                 old_off = old & mm->mask;
1156         } else {
1157                 head_off = head % mm->len;
1158                 old_off = old % mm->len;
1159         }
1160
1161         if (head_off > old_off)
1162                 size = head_off - old_off;
1163         else
1164                 size = mm->len - (old_off - head_off);
1165
1166         if (snapshot && size > snapshot_size)
1167                 size = snapshot_size;
1168
1169         ref = auxtrace_record__reference(itr);
1170
1171         if (head > old || size <= head || mm->mask) {
1172                 offset = head - size;
1173         } else {
1174                 /*
1175                  * When the buffer size is not a power of 2, 'head' wraps at the
1176                  * highest multiple of the buffer size, so we have to subtract
1177                  * the remainder here.
1178                  */
1179                 u64 rem = (0ULL - mm->len) % mm->len;
1180
1181                 offset = head - size - rem;
1182         }
1183
1184         if (size > head_off) {
1185                 len1 = size - head_off;
1186                 data1 = &data[mm->len - len1];
1187                 len2 = head_off;
1188                 data2 = &data[0];
1189         } else {
1190                 len1 = size;
1191                 data1 = &data[head_off - len1];
1192                 len2 = 0;
1193                 data2 = NULL;
1194         }
1195
1196         if (itr->alignment) {
1197                 unsigned int unwanted = len1 % itr->alignment;
1198
1199                 len1 -= unwanted;
1200                 size -= unwanted;
1201         }
1202
1203         /* padding must be written by fn() e.g. record__process_auxtrace() */
1204         padding = size & 7;
1205         if (padding)
1206                 padding = 8 - padding;
1207
1208         memset(&ev, 0, sizeof(ev));
1209         ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1210         ev.auxtrace.header.size = sizeof(ev.auxtrace);
1211         ev.auxtrace.size = size + padding;
1212         ev.auxtrace.offset = offset;
1213         ev.auxtrace.reference = ref;
1214         ev.auxtrace.idx = mm->idx;
1215         ev.auxtrace.tid = mm->tid;
1216         ev.auxtrace.cpu = mm->cpu;
1217
1218         if (fn(tool, &ev, data1, len1, data2, len2))
1219                 return -1;
1220
1221         mm->prev = head;
1222
1223         if (!snapshot) {
1224                 auxtrace_mmap__write_tail(mm, head);
1225                 if (itr->read_finish) {
1226                         int err;
1227
1228                         err = itr->read_finish(itr, mm->idx);
1229                         if (err < 0)
1230                                 return err;
1231                 }
1232         }
1233
1234         return 1;
1235 }
1236
1237 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1238                         struct perf_tool *tool, process_auxtrace_t fn)
1239 {
1240         return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1241 }
1242
1243 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1244                                  struct auxtrace_record *itr,
1245                                  struct perf_tool *tool, process_auxtrace_t fn,
1246                                  size_t snapshot_size)
1247 {
1248         return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1249 }
1250
1251 /**
1252  * struct auxtrace_cache - hash table to implement a cache
1253  * @hashtable: the hashtable
1254  * @sz: hashtable size (number of hlists)
1255  * @entry_size: size of an entry
1256  * @limit: limit the number of entries to this maximum, when reached the cache
1257  *         is dropped and caching begins again with an empty cache
1258  * @cnt: current number of entries
1259  * @bits: hashtable size (@sz = 2^@bits)
1260  */
1261 struct auxtrace_cache {
1262         struct hlist_head *hashtable;
1263         size_t sz;
1264         size_t entry_size;
1265         size_t limit;
1266         size_t cnt;
1267         unsigned int bits;
1268 };
1269
1270 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1271                                            unsigned int limit_percent)
1272 {
1273         struct auxtrace_cache *c;
1274         struct hlist_head *ht;
1275         size_t sz, i;
1276
1277         c = zalloc(sizeof(struct auxtrace_cache));
1278         if (!c)
1279                 return NULL;
1280
1281         sz = 1UL << bits;
1282
1283         ht = calloc(sz, sizeof(struct hlist_head));
1284         if (!ht)
1285                 goto out_free;
1286
1287         for (i = 0; i < sz; i++)
1288                 INIT_HLIST_HEAD(&ht[i]);
1289
1290         c->hashtable = ht;
1291         c->sz = sz;
1292         c->entry_size = entry_size;
1293         c->limit = (c->sz * limit_percent) / 100;
1294         c->bits = bits;
1295
1296         return c;
1297
1298 out_free:
1299         free(c);
1300         return NULL;
1301 }
1302
1303 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1304 {
1305         struct auxtrace_cache_entry *entry;
1306         struct hlist_node *tmp;
1307         size_t i;
1308
1309         if (!c)
1310                 return;
1311
1312         for (i = 0; i < c->sz; i++) {
1313                 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1314                         hlist_del(&entry->hash);
1315                         auxtrace_cache__free_entry(c, entry);
1316                 }
1317         }
1318
1319         c->cnt = 0;
1320 }
1321
1322 void auxtrace_cache__free(struct auxtrace_cache *c)
1323 {
1324         if (!c)
1325                 return;
1326
1327         auxtrace_cache__drop(c);
1328         free(c->hashtable);
1329         free(c);
1330 }
1331
1332 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1333 {
1334         return malloc(c->entry_size);
1335 }
1336
1337 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1338                                 void *entry)
1339 {
1340         free(entry);
1341 }
1342
1343 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1344                         struct auxtrace_cache_entry *entry)
1345 {
1346         if (c->limit && ++c->cnt > c->limit)
1347                 auxtrace_cache__drop(c);
1348
1349         entry->key = key;
1350         hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1351
1352         return 0;
1353 }
1354
1355 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1356 {
1357         struct auxtrace_cache_entry *entry;
1358         struct hlist_head *hlist;
1359
1360         if (!c)
1361                 return NULL;
1362
1363         hlist = &c->hashtable[hash_32(key, c->bits)];
1364         hlist_for_each_entry(entry, hlist, hash) {
1365                 if (entry->key == key)
1366                         return entry;
1367         }
1368
1369         return NULL;
1370 }