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