aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/evsel.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/evsel.c')
-rw-r--r--tools/perf/util/evsel.c597
1 files changed, 536 insertions, 61 deletions
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index c9c7494506a1..0ce9febf1ba0 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -9,27 +9,30 @@
9 9
10#include <byteswap.h> 10#include <byteswap.h>
11#include <linux/bitops.h> 11#include <linux/bitops.h>
12#include "asm/bug.h"
13#include <lk/debugfs.h> 12#include <lk/debugfs.h>
14#include "event-parse.h" 13#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
16#include <sys/resource.h>
17#include "asm/bug.h"
15#include "evsel.h" 18#include "evsel.h"
16#include "evlist.h" 19#include "evlist.h"
17#include "util.h" 20#include "util.h"
18#include "cpumap.h" 21#include "cpumap.h"
19#include "thread_map.h" 22#include "thread_map.h"
20#include "target.h" 23#include "target.h"
21#include <linux/hw_breakpoint.h>
22#include <linux/perf_event.h>
23#include "perf_regs.h" 24#include "perf_regs.h"
25#include "debug.h"
24 26
25static struct { 27static struct {
26 bool sample_id_all; 28 bool sample_id_all;
27 bool exclude_guest; 29 bool exclude_guest;
30 bool mmap2;
28} perf_missing_features; 31} perf_missing_features;
29 32
30#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 33#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
31 34
32static int __perf_evsel__sample_size(u64 sample_type) 35int __perf_evsel__sample_size(u64 sample_type)
33{ 36{
34 u64 mask = sample_type & PERF_SAMPLE_MASK; 37 u64 mask = sample_type & PERF_SAMPLE_MASK;
35 int size = 0; 38 int size = 0;
@@ -45,6 +48,72 @@ static int __perf_evsel__sample_size(u64 sample_type)
45 return size; 48 return size;
46} 49}
47 50
51/**
52 * __perf_evsel__calc_id_pos - calculate id_pos.
53 * @sample_type: sample type
54 *
55 * This function returns the position of the event id (PERF_SAMPLE_ID or
56 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
57 * sample_event.
58 */
59static int __perf_evsel__calc_id_pos(u64 sample_type)
60{
61 int idx = 0;
62
63 if (sample_type & PERF_SAMPLE_IDENTIFIER)
64 return 0;
65
66 if (!(sample_type & PERF_SAMPLE_ID))
67 return -1;
68
69 if (sample_type & PERF_SAMPLE_IP)
70 idx += 1;
71
72 if (sample_type & PERF_SAMPLE_TID)
73 idx += 1;
74
75 if (sample_type & PERF_SAMPLE_TIME)
76 idx += 1;
77
78 if (sample_type & PERF_SAMPLE_ADDR)
79 idx += 1;
80
81 return idx;
82}
83
84/**
85 * __perf_evsel__calc_is_pos - calculate is_pos.
86 * @sample_type: sample type
87 *
88 * This function returns the position (counting backwards) of the event id
89 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
90 * sample_id_all is used there is an id sample appended to non-sample events.
91 */
92static int __perf_evsel__calc_is_pos(u64 sample_type)
93{
94 int idx = 1;
95
96 if (sample_type & PERF_SAMPLE_IDENTIFIER)
97 return 1;
98
99 if (!(sample_type & PERF_SAMPLE_ID))
100 return -1;
101
102 if (sample_type & PERF_SAMPLE_CPU)
103 idx += 1;
104
105 if (sample_type & PERF_SAMPLE_STREAM_ID)
106 idx += 1;
107
108 return idx;
109}
110
111void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
112{
113 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
114 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
115}
116
48void hists__init(struct hists *hists) 117void hists__init(struct hists *hists)
49{ 118{
50 memset(hists, 0, sizeof(*hists)); 119 memset(hists, 0, sizeof(*hists));
@@ -61,6 +130,7 @@ void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
61 if (!(evsel->attr.sample_type & bit)) { 130 if (!(evsel->attr.sample_type & bit)) {
62 evsel->attr.sample_type |= bit; 131 evsel->attr.sample_type |= bit;
63 evsel->sample_size += sizeof(u64); 132 evsel->sample_size += sizeof(u64);
133 perf_evsel__calc_id_pos(evsel);
64 } 134 }
65} 135}
66 136
@@ -70,12 +140,19 @@ void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
70 if (evsel->attr.sample_type & bit) { 140 if (evsel->attr.sample_type & bit) {
71 evsel->attr.sample_type &= ~bit; 141 evsel->attr.sample_type &= ~bit;
72 evsel->sample_size -= sizeof(u64); 142 evsel->sample_size -= sizeof(u64);
143 perf_evsel__calc_id_pos(evsel);
73 } 144 }
74} 145}
75 146
76void perf_evsel__set_sample_id(struct perf_evsel *evsel) 147void perf_evsel__set_sample_id(struct perf_evsel *evsel,
148 bool can_sample_identifier)
77{ 149{
78 perf_evsel__set_sample_bit(evsel, ID); 150 if (can_sample_identifier) {
151 perf_evsel__reset_sample_bit(evsel, ID);
152 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
153 } else {
154 perf_evsel__set_sample_bit(evsel, ID);
155 }
79 evsel->attr.read_format |= PERF_FORMAT_ID; 156 evsel->attr.read_format |= PERF_FORMAT_ID;
80} 157}
81 158
@@ -88,6 +165,7 @@ void perf_evsel__init(struct perf_evsel *evsel,
88 INIT_LIST_HEAD(&evsel->node); 165 INIT_LIST_HEAD(&evsel->node);
89 hists__init(&evsel->hists); 166 hists__init(&evsel->hists);
90 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type); 167 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
168 perf_evsel__calc_id_pos(evsel);
91} 169}
92 170
93struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx) 171struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
@@ -246,6 +324,7 @@ const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
246 "major-faults", 324 "major-faults",
247 "alignment-faults", 325 "alignment-faults",
248 "emulation-faults", 326 "emulation-faults",
327 "dummy",
249}; 328};
250 329
251static const char *__perf_evsel__sw_name(u64 config) 330static const char *__perf_evsel__sw_name(u64 config)
@@ -490,6 +569,7 @@ int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
490void perf_evsel__config(struct perf_evsel *evsel, 569void perf_evsel__config(struct perf_evsel *evsel,
491 struct perf_record_opts *opts) 570 struct perf_record_opts *opts)
492{ 571{
572 struct perf_evsel *leader = evsel->leader;
493 struct perf_event_attr *attr = &evsel->attr; 573 struct perf_event_attr *attr = &evsel->attr;
494 int track = !evsel->idx; /* only the first counter needs these */ 574 int track = !evsel->idx; /* only the first counter needs these */
495 575
@@ -499,6 +579,25 @@ void perf_evsel__config(struct perf_evsel *evsel,
499 perf_evsel__set_sample_bit(evsel, IP); 579 perf_evsel__set_sample_bit(evsel, IP);
500 perf_evsel__set_sample_bit(evsel, TID); 580 perf_evsel__set_sample_bit(evsel, TID);
501 581
582 if (evsel->sample_read) {
583 perf_evsel__set_sample_bit(evsel, READ);
584
585 /*
586 * We need ID even in case of single event, because
587 * PERF_SAMPLE_READ process ID specific data.
588 */
589 perf_evsel__set_sample_id(evsel, false);
590
591 /*
592 * Apply group format only if we belong to group
593 * with more than one members.
594 */
595 if (leader->nr_members > 1) {
596 attr->read_format |= PERF_FORMAT_GROUP;
597 attr->inherit = 0;
598 }
599 }
600
502 /* 601 /*
503 * We default some events to a 1 default interval. But keep 602 * We default some events to a 1 default interval. But keep
504 * it a weak assumption overridable by the user. 603 * it a weak assumption overridable by the user.
@@ -514,6 +613,15 @@ void perf_evsel__config(struct perf_evsel *evsel,
514 } 613 }
515 } 614 }
516 615
616 /*
617 * Disable sampling for all group members other
618 * than leader in case leader 'leads' the sampling.
619 */
620 if ((leader != evsel) && leader->sample_read) {
621 attr->sample_freq = 0;
622 attr->sample_period = 0;
623 }
624
517 if (opts->no_samples) 625 if (opts->no_samples)
518 attr->sample_freq = 0; 626 attr->sample_freq = 0;
519 627
@@ -569,8 +677,9 @@ void perf_evsel__config(struct perf_evsel *evsel,
569 if (opts->sample_weight) 677 if (opts->sample_weight)
570 attr->sample_type |= PERF_SAMPLE_WEIGHT; 678 attr->sample_type |= PERF_SAMPLE_WEIGHT;
571 679
572 attr->mmap = track; 680 attr->mmap = track;
573 attr->comm = track; 681 attr->mmap2 = track && !perf_missing_features.mmap2;
682 attr->comm = track;
574 683
575 /* 684 /*
576 * XXX see the function comment above 685 * XXX see the function comment above
@@ -605,15 +714,15 @@ int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
605 return evsel->fd != NULL ? 0 : -ENOMEM; 714 return evsel->fd != NULL ? 0 : -ENOMEM;
606} 715}
607 716
608int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads, 717static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
609 const char *filter) 718 int ioc, void *arg)
610{ 719{
611 int cpu, thread; 720 int cpu, thread;
612 721
613 for (cpu = 0; cpu < ncpus; cpu++) { 722 for (cpu = 0; cpu < ncpus; cpu++) {
614 for (thread = 0; thread < nthreads; thread++) { 723 for (thread = 0; thread < nthreads; thread++) {
615 int fd = FD(evsel, cpu, thread), 724 int fd = FD(evsel, cpu, thread),
616 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); 725 err = ioctl(fd, ioc, arg);
617 726
618 if (err) 727 if (err)
619 return err; 728 return err;
@@ -623,6 +732,21 @@ int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
623 return 0; 732 return 0;
624} 733}
625 734
735int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
736 const char *filter)
737{
738 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
739 PERF_EVENT_IOC_SET_FILTER,
740 (void *)filter);
741}
742
743int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
744{
745 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
746 PERF_EVENT_IOC_ENABLE,
747 0);
748}
749
626int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) 750int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
627{ 751{
628 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); 752 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
@@ -817,12 +941,72 @@ static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
817 return fd; 941 return fd;
818} 942}
819 943
944#define __PRINT_ATTR(fmt, cast, field) \
945 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
946
947#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
948#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
949#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
950#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
951
952#define PRINT_ATTR2N(name1, field1, name2, field2) \
953 fprintf(fp, " %-19s %u %-19s %u\n", \
954 name1, attr->field1, name2, attr->field2)
955
956#define PRINT_ATTR2(field1, field2) \
957 PRINT_ATTR2N(#field1, field1, #field2, field2)
958
959static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
960{
961 size_t ret = 0;
962
963 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
964 ret += fprintf(fp, "perf_event_attr:\n");
965
966 ret += PRINT_ATTR_U32(type);
967 ret += PRINT_ATTR_U32(size);
968 ret += PRINT_ATTR_X64(config);
969 ret += PRINT_ATTR_U64(sample_period);
970 ret += PRINT_ATTR_U64(sample_freq);
971 ret += PRINT_ATTR_X64(sample_type);
972 ret += PRINT_ATTR_X64(read_format);
973
974 ret += PRINT_ATTR2(disabled, inherit);
975 ret += PRINT_ATTR2(pinned, exclusive);
976 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
977 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
978 ret += PRINT_ATTR2(mmap, comm);
979 ret += PRINT_ATTR2(freq, inherit_stat);
980 ret += PRINT_ATTR2(enable_on_exec, task);
981 ret += PRINT_ATTR2(watermark, precise_ip);
982 ret += PRINT_ATTR2(mmap_data, sample_id_all);
983 ret += PRINT_ATTR2(exclude_host, exclude_guest);
984 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
985 "excl.callchain_user", exclude_callchain_user);
986
987 ret += PRINT_ATTR_U32(wakeup_events);
988 ret += PRINT_ATTR_U32(wakeup_watermark);
989 ret += PRINT_ATTR_X32(bp_type);
990 ret += PRINT_ATTR_X64(bp_addr);
991 ret += PRINT_ATTR_X64(config1);
992 ret += PRINT_ATTR_U64(bp_len);
993 ret += PRINT_ATTR_X64(config2);
994 ret += PRINT_ATTR_X64(branch_sample_type);
995 ret += PRINT_ATTR_X64(sample_regs_user);
996 ret += PRINT_ATTR_U32(sample_stack_user);
997
998 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
999
1000 return ret;
1001}
1002
820static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, 1003static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
821 struct thread_map *threads) 1004 struct thread_map *threads)
822{ 1005{
823 int cpu, thread; 1006 int cpu, thread;
824 unsigned long flags = 0; 1007 unsigned long flags = 0;
825 int pid = -1, err; 1008 int pid = -1, err;
1009 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
826 1010
827 if (evsel->fd == NULL && 1011 if (evsel->fd == NULL &&
828 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0) 1012 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
@@ -834,12 +1018,17 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
834 } 1018 }
835 1019
836fallback_missing_features: 1020fallback_missing_features:
1021 if (perf_missing_features.mmap2)
1022 evsel->attr.mmap2 = 0;
837 if (perf_missing_features.exclude_guest) 1023 if (perf_missing_features.exclude_guest)
838 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0; 1024 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
839retry_sample_id: 1025retry_sample_id:
840 if (perf_missing_features.sample_id_all) 1026 if (perf_missing_features.sample_id_all)
841 evsel->attr.sample_id_all = 0; 1027 evsel->attr.sample_id_all = 0;
842 1028
1029 if (verbose >= 2)
1030 perf_event_attr__fprintf(&evsel->attr, stderr);
1031
843 for (cpu = 0; cpu < cpus->nr; cpu++) { 1032 for (cpu = 0; cpu < cpus->nr; cpu++) {
844 1033
845 for (thread = 0; thread < threads->nr; thread++) { 1034 for (thread = 0; thread < threads->nr; thread++) {
@@ -849,6 +1038,9 @@ retry_sample_id:
849 pid = threads->map[thread]; 1038 pid = threads->map[thread];
850 1039
851 group_fd = get_group_fd(evsel, cpu, thread); 1040 group_fd = get_group_fd(evsel, cpu, thread);
1041retry_open:
1042 pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1043 pid, cpus->map[cpu], group_fd, flags);
852 1044
853 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr, 1045 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
854 pid, 1046 pid,
@@ -858,17 +1050,45 @@ retry_sample_id:
858 err = -errno; 1050 err = -errno;
859 goto try_fallback; 1051 goto try_fallback;
860 } 1052 }
1053 set_rlimit = NO_CHANGE;
861 } 1054 }
862 } 1055 }
863 1056
864 return 0; 1057 return 0;
865 1058
866try_fallback: 1059try_fallback:
1060 /*
1061 * perf stat needs between 5 and 22 fds per CPU. When we run out
1062 * of them try to increase the limits.
1063 */
1064 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1065 struct rlimit l;
1066 int old_errno = errno;
1067
1068 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1069 if (set_rlimit == NO_CHANGE)
1070 l.rlim_cur = l.rlim_max;
1071 else {
1072 l.rlim_cur = l.rlim_max + 1000;
1073 l.rlim_max = l.rlim_cur;
1074 }
1075 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1076 set_rlimit++;
1077 errno = old_errno;
1078 goto retry_open;
1079 }
1080 }
1081 errno = old_errno;
1082 }
1083
867 if (err != -EINVAL || cpu > 0 || thread > 0) 1084 if (err != -EINVAL || cpu > 0 || thread > 0)
868 goto out_close; 1085 goto out_close;
869 1086
870 if (!perf_missing_features.exclude_guest && 1087 if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
871 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) { 1088 perf_missing_features.mmap2 = true;
1089 goto fallback_missing_features;
1090 } else if (!perf_missing_features.exclude_guest &&
1091 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
872 perf_missing_features.exclude_guest = true; 1092 perf_missing_features.exclude_guest = true;
873 goto fallback_missing_features; 1093 goto fallback_missing_features;
874 } else if (!perf_missing_features.sample_id_all) { 1094 } else if (!perf_missing_features.sample_id_all) {
@@ -951,6 +1171,11 @@ static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
951 array += ((event->header.size - 1171 array += ((event->header.size -
952 sizeof(event->header)) / sizeof(u64)) - 1; 1172 sizeof(event->header)) / sizeof(u64)) - 1;
953 1173
1174 if (type & PERF_SAMPLE_IDENTIFIER) {
1175 sample->id = *array;
1176 array--;
1177 }
1178
954 if (type & PERF_SAMPLE_CPU) { 1179 if (type & PERF_SAMPLE_CPU) {
955 u.val64 = *array; 1180 u.val64 = *array;
956 if (swapped) { 1181 if (swapped) {
@@ -994,24 +1219,30 @@ static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
994 return 0; 1219 return 0;
995} 1220}
996 1221
997static bool sample_overlap(const union perf_event *event, 1222static inline bool overflow(const void *endp, u16 max_size, const void *offset,
998 const void *offset, u64 size) 1223 u64 size)
999{ 1224{
1000 const void *base = event; 1225 return size > max_size || offset + size > endp;
1226}
1001 1227
1002 if (offset + size > base + event->header.size) 1228#define OVERFLOW_CHECK(offset, size, max_size) \
1003 return true; 1229 do { \
1230 if (overflow(endp, (max_size), (offset), (size))) \
1231 return -EFAULT; \
1232 } while (0)
1004 1233
1005 return false; 1234#define OVERFLOW_CHECK_u64(offset) \
1006} 1235 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1007 1236
1008int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event, 1237int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1009 struct perf_sample *data) 1238 struct perf_sample *data)
1010{ 1239{
1011 u64 type = evsel->attr.sample_type; 1240 u64 type = evsel->attr.sample_type;
1012 u64 regs_user = evsel->attr.sample_regs_user;
1013 bool swapped = evsel->needs_swap; 1241 bool swapped = evsel->needs_swap;
1014 const u64 *array; 1242 const u64 *array;
1243 u16 max_size = event->header.size;
1244 const void *endp = (void *)event + max_size;
1245 u64 sz;
1015 1246
1016 /* 1247 /*
1017 * used for cross-endian analysis. See git commit 65014ab3 1248 * used for cross-endian analysis. See git commit 65014ab3
@@ -1033,11 +1264,22 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1033 1264
1034 array = event->sample.array; 1265 array = event->sample.array;
1035 1266
1267 /*
1268 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1269 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1270 * check the format does not go past the end of the event.
1271 */
1036 if (evsel->sample_size + sizeof(event->header) > event->header.size) 1272 if (evsel->sample_size + sizeof(event->header) > event->header.size)
1037 return -EFAULT; 1273 return -EFAULT;
1038 1274
1275 data->id = -1ULL;
1276 if (type & PERF_SAMPLE_IDENTIFIER) {
1277 data->id = *array;
1278 array++;
1279 }
1280
1039 if (type & PERF_SAMPLE_IP) { 1281 if (type & PERF_SAMPLE_IP) {
1040 data->ip = event->ip.ip; 1282 data->ip = *array;
1041 array++; 1283 array++;
1042 } 1284 }
1043 1285
@@ -1066,7 +1308,6 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1066 array++; 1308 array++;
1067 } 1309 }
1068 1310
1069 data->id = -1ULL;
1070 if (type & PERF_SAMPLE_ID) { 1311 if (type & PERF_SAMPLE_ID) {
1071 data->id = *array; 1312 data->id = *array;
1072 array++; 1313 array++;
@@ -1096,25 +1337,62 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1096 } 1337 }
1097 1338
1098 if (type & PERF_SAMPLE_READ) { 1339 if (type & PERF_SAMPLE_READ) {
1099 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n"); 1340 u64 read_format = evsel->attr.read_format;
1100 return -1; 1341
1342 OVERFLOW_CHECK_u64(array);
1343 if (read_format & PERF_FORMAT_GROUP)
1344 data->read.group.nr = *array;
1345 else
1346 data->read.one.value = *array;
1347
1348 array++;
1349
1350 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1351 OVERFLOW_CHECK_u64(array);
1352 data->read.time_enabled = *array;
1353 array++;
1354 }
1355
1356 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1357 OVERFLOW_CHECK_u64(array);
1358 data->read.time_running = *array;
1359 array++;
1360 }
1361
1362 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1363 if (read_format & PERF_FORMAT_GROUP) {
1364 const u64 max_group_nr = UINT64_MAX /
1365 sizeof(struct sample_read_value);
1366
1367 if (data->read.group.nr > max_group_nr)
1368 return -EFAULT;
1369 sz = data->read.group.nr *
1370 sizeof(struct sample_read_value);
1371 OVERFLOW_CHECK(array, sz, max_size);
1372 data->read.group.values =
1373 (struct sample_read_value *)array;
1374 array = (void *)array + sz;
1375 } else {
1376 OVERFLOW_CHECK_u64(array);
1377 data->read.one.id = *array;
1378 array++;
1379 }
1101 } 1380 }
1102 1381
1103 if (type & PERF_SAMPLE_CALLCHAIN) { 1382 if (type & PERF_SAMPLE_CALLCHAIN) {
1104 if (sample_overlap(event, array, sizeof(data->callchain->nr))) 1383 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1105 return -EFAULT;
1106
1107 data->callchain = (struct ip_callchain *)array;
1108 1384
1109 if (sample_overlap(event, array, data->callchain->nr)) 1385 OVERFLOW_CHECK_u64(array);
1386 data->callchain = (struct ip_callchain *)array++;
1387 if (data->callchain->nr > max_callchain_nr)
1110 return -EFAULT; 1388 return -EFAULT;
1111 1389 sz = data->callchain->nr * sizeof(u64);
1112 array += 1 + data->callchain->nr; 1390 OVERFLOW_CHECK(array, sz, max_size);
1391 array = (void *)array + sz;
1113 } 1392 }
1114 1393
1115 if (type & PERF_SAMPLE_RAW) { 1394 if (type & PERF_SAMPLE_RAW) {
1116 const u64 *pdata; 1395 OVERFLOW_CHECK_u64(array);
1117
1118 u.val64 = *array; 1396 u.val64 = *array;
1119 if (WARN_ONCE(swapped, 1397 if (WARN_ONCE(swapped,
1120 "Endianness of raw data not corrected!\n")) { 1398 "Endianness of raw data not corrected!\n")) {
@@ -1123,65 +1401,71 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1123 u.val32[0] = bswap_32(u.val32[0]); 1401 u.val32[0] = bswap_32(u.val32[0]);
1124 u.val32[1] = bswap_32(u.val32[1]); 1402 u.val32[1] = bswap_32(u.val32[1]);
1125 } 1403 }
1126
1127 if (sample_overlap(event, array, sizeof(u32)))
1128 return -EFAULT;
1129
1130 data->raw_size = u.val32[0]; 1404 data->raw_size = u.val32[0];
1131 pdata = (void *) array + sizeof(u32); 1405 array = (void *)array + sizeof(u32);
1132 1406
1133 if (sample_overlap(event, pdata, data->raw_size)) 1407 OVERFLOW_CHECK(array, data->raw_size, max_size);
1134 return -EFAULT; 1408 data->raw_data = (void *)array;
1135 1409 array = (void *)array + data->raw_size;
1136 data->raw_data = (void *) pdata;
1137
1138 array = (void *)array + data->raw_size + sizeof(u32);
1139 } 1410 }
1140 1411
1141 if (type & PERF_SAMPLE_BRANCH_STACK) { 1412 if (type & PERF_SAMPLE_BRANCH_STACK) {
1142 u64 sz; 1413 const u64 max_branch_nr = UINT64_MAX /
1414 sizeof(struct branch_entry);
1143 1415
1144 data->branch_stack = (struct branch_stack *)array; 1416 OVERFLOW_CHECK_u64(array);
1145 array++; /* nr */ 1417 data->branch_stack = (struct branch_stack *)array++;
1146 1418
1419 if (data->branch_stack->nr > max_branch_nr)
1420 return -EFAULT;
1147 sz = data->branch_stack->nr * sizeof(struct branch_entry); 1421 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1148 sz /= sizeof(u64); 1422 OVERFLOW_CHECK(array, sz, max_size);
1149 array += sz; 1423 array = (void *)array + sz;
1150 } 1424 }
1151 1425
1152 if (type & PERF_SAMPLE_REGS_USER) { 1426 if (type & PERF_SAMPLE_REGS_USER) {
1153 /* First u64 tells us if we have any regs in sample. */ 1427 OVERFLOW_CHECK_u64(array);
1154 u64 avail = *array++; 1428 data->user_regs.abi = *array;
1429 array++;
1430
1431 if (data->user_regs.abi) {
1432 u64 regs_user = evsel->attr.sample_regs_user;
1155 1433
1156 if (avail) { 1434 sz = hweight_long(regs_user) * sizeof(u64);
1435 OVERFLOW_CHECK(array, sz, max_size);
1157 data->user_regs.regs = (u64 *)array; 1436 data->user_regs.regs = (u64 *)array;
1158 array += hweight_long(regs_user); 1437 array = (void *)array + sz;
1159 } 1438 }
1160 } 1439 }
1161 1440
1162 if (type & PERF_SAMPLE_STACK_USER) { 1441 if (type & PERF_SAMPLE_STACK_USER) {
1163 u64 size = *array++; 1442 OVERFLOW_CHECK_u64(array);
1443 sz = *array++;
1164 1444
1165 data->user_stack.offset = ((char *)(array - 1) 1445 data->user_stack.offset = ((char *)(array - 1)
1166 - (char *) event); 1446 - (char *) event);
1167 1447
1168 if (!size) { 1448 if (!sz) {
1169 data->user_stack.size = 0; 1449 data->user_stack.size = 0;
1170 } else { 1450 } else {
1451 OVERFLOW_CHECK(array, sz, max_size);
1171 data->user_stack.data = (char *)array; 1452 data->user_stack.data = (char *)array;
1172 array += size / sizeof(*array); 1453 array = (void *)array + sz;
1454 OVERFLOW_CHECK_u64(array);
1173 data->user_stack.size = *array++; 1455 data->user_stack.size = *array++;
1174 } 1456 }
1175 } 1457 }
1176 1458
1177 data->weight = 0; 1459 data->weight = 0;
1178 if (type & PERF_SAMPLE_WEIGHT) { 1460 if (type & PERF_SAMPLE_WEIGHT) {
1461 OVERFLOW_CHECK_u64(array);
1179 data->weight = *array; 1462 data->weight = *array;
1180 array++; 1463 array++;
1181 } 1464 }
1182 1465
1183 data->data_src = PERF_MEM_DATA_SRC_NONE; 1466 data->data_src = PERF_MEM_DATA_SRC_NONE;
1184 if (type & PERF_SAMPLE_DATA_SRC) { 1467 if (type & PERF_SAMPLE_DATA_SRC) {
1468 OVERFLOW_CHECK_u64(array);
1185 data->data_src = *array; 1469 data->data_src = *array;
1186 array++; 1470 array++;
1187 } 1471 }
@@ -1189,12 +1473,105 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1189 return 0; 1473 return 0;
1190} 1474}
1191 1475
1476size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1477 u64 sample_regs_user, u64 read_format)
1478{
1479 size_t sz, result = sizeof(struct sample_event);
1480
1481 if (type & PERF_SAMPLE_IDENTIFIER)
1482 result += sizeof(u64);
1483
1484 if (type & PERF_SAMPLE_IP)
1485 result += sizeof(u64);
1486
1487 if (type & PERF_SAMPLE_TID)
1488 result += sizeof(u64);
1489
1490 if (type & PERF_SAMPLE_TIME)
1491 result += sizeof(u64);
1492
1493 if (type & PERF_SAMPLE_ADDR)
1494 result += sizeof(u64);
1495
1496 if (type & PERF_SAMPLE_ID)
1497 result += sizeof(u64);
1498
1499 if (type & PERF_SAMPLE_STREAM_ID)
1500 result += sizeof(u64);
1501
1502 if (type & PERF_SAMPLE_CPU)
1503 result += sizeof(u64);
1504
1505 if (type & PERF_SAMPLE_PERIOD)
1506 result += sizeof(u64);
1507
1508 if (type & PERF_SAMPLE_READ) {
1509 result += sizeof(u64);
1510 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1511 result += sizeof(u64);
1512 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1513 result += sizeof(u64);
1514 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1515 if (read_format & PERF_FORMAT_GROUP) {
1516 sz = sample->read.group.nr *
1517 sizeof(struct sample_read_value);
1518 result += sz;
1519 } else {
1520 result += sizeof(u64);
1521 }
1522 }
1523
1524 if (type & PERF_SAMPLE_CALLCHAIN) {
1525 sz = (sample->callchain->nr + 1) * sizeof(u64);
1526 result += sz;
1527 }
1528
1529 if (type & PERF_SAMPLE_RAW) {
1530 result += sizeof(u32);
1531 result += sample->raw_size;
1532 }
1533
1534 if (type & PERF_SAMPLE_BRANCH_STACK) {
1535 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1536 sz += sizeof(u64);
1537 result += sz;
1538 }
1539
1540 if (type & PERF_SAMPLE_REGS_USER) {
1541 if (sample->user_regs.abi) {
1542 result += sizeof(u64);
1543 sz = hweight_long(sample_regs_user) * sizeof(u64);
1544 result += sz;
1545 } else {
1546 result += sizeof(u64);
1547 }
1548 }
1549
1550 if (type & PERF_SAMPLE_STACK_USER) {
1551 sz = sample->user_stack.size;
1552 result += sizeof(u64);
1553 if (sz) {
1554 result += sz;
1555 result += sizeof(u64);
1556 }
1557 }
1558
1559 if (type & PERF_SAMPLE_WEIGHT)
1560 result += sizeof(u64);
1561
1562 if (type & PERF_SAMPLE_DATA_SRC)
1563 result += sizeof(u64);
1564
1565 return result;
1566}
1567
1192int perf_event__synthesize_sample(union perf_event *event, u64 type, 1568int perf_event__synthesize_sample(union perf_event *event, u64 type,
1569 u64 sample_regs_user, u64 read_format,
1193 const struct perf_sample *sample, 1570 const struct perf_sample *sample,
1194 bool swapped) 1571 bool swapped)
1195{ 1572{
1196 u64 *array; 1573 u64 *array;
1197 1574 size_t sz;
1198 /* 1575 /*
1199 * used for cross-endian analysis. See git commit 65014ab3 1576 * used for cross-endian analysis. See git commit 65014ab3
1200 * for why this goofiness is needed. 1577 * for why this goofiness is needed.
@@ -1203,8 +1580,13 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type,
1203 1580
1204 array = event->sample.array; 1581 array = event->sample.array;
1205 1582
1583 if (type & PERF_SAMPLE_IDENTIFIER) {
1584 *array = sample->id;
1585 array++;
1586 }
1587
1206 if (type & PERF_SAMPLE_IP) { 1588 if (type & PERF_SAMPLE_IP) {
1207 event->ip.ip = sample->ip; 1589 *array = sample->ip;
1208 array++; 1590 array++;
1209 } 1591 }
1210 1592
@@ -1262,6 +1644,97 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type,
1262 array++; 1644 array++;
1263 } 1645 }
1264 1646
1647 if (type & PERF_SAMPLE_READ) {
1648 if (read_format & PERF_FORMAT_GROUP)
1649 *array = sample->read.group.nr;
1650 else
1651 *array = sample->read.one.value;
1652 array++;
1653
1654 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1655 *array = sample->read.time_enabled;
1656 array++;
1657 }
1658
1659 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1660 *array = sample->read.time_running;
1661 array++;
1662 }
1663
1664 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1665 if (read_format & PERF_FORMAT_GROUP) {
1666 sz = sample->read.group.nr *
1667 sizeof(struct sample_read_value);
1668 memcpy(array, sample->read.group.values, sz);
1669 array = (void *)array + sz;
1670 } else {
1671 *array = sample->read.one.id;
1672 array++;
1673 }
1674 }
1675
1676 if (type & PERF_SAMPLE_CALLCHAIN) {
1677 sz = (sample->callchain->nr + 1) * sizeof(u64);
1678 memcpy(array, sample->callchain, sz);
1679 array = (void *)array + sz;
1680 }
1681
1682 if (type & PERF_SAMPLE_RAW) {
1683 u.val32[0] = sample->raw_size;
1684 if (WARN_ONCE(swapped,
1685 "Endianness of raw data not corrected!\n")) {
1686 /*
1687 * Inverse of what is done in perf_evsel__parse_sample
1688 */
1689 u.val32[0] = bswap_32(u.val32[0]);
1690 u.val32[1] = bswap_32(u.val32[1]);
1691 u.val64 = bswap_64(u.val64);
1692 }
1693 *array = u.val64;
1694 array = (void *)array + sizeof(u32);
1695
1696 memcpy(array, sample->raw_data, sample->raw_size);
1697 array = (void *)array + sample->raw_size;
1698 }
1699
1700 if (type & PERF_SAMPLE_BRANCH_STACK) {
1701 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1702 sz += sizeof(u64);
1703 memcpy(array, sample->branch_stack, sz);
1704 array = (void *)array + sz;
1705 }
1706
1707 if (type & PERF_SAMPLE_REGS_USER) {
1708 if (sample->user_regs.abi) {
1709 *array++ = sample->user_regs.abi;
1710 sz = hweight_long(sample_regs_user) * sizeof(u64);
1711 memcpy(array, sample->user_regs.regs, sz);
1712 array = (void *)array + sz;
1713 } else {
1714 *array++ = 0;
1715 }
1716 }
1717
1718 if (type & PERF_SAMPLE_STACK_USER) {
1719 sz = sample->user_stack.size;
1720 *array++ = sz;
1721 if (sz) {
1722 memcpy(array, sample->user_stack.data, sz);
1723 array = (void *)array + sz;
1724 *array++ = sz;
1725 }
1726 }
1727
1728 if (type & PERF_SAMPLE_WEIGHT) {
1729 *array = sample->weight;
1730 array++;
1731 }
1732
1733 if (type & PERF_SAMPLE_DATA_SRC) {
1734 *array = sample->data_src;
1735 array++;
1736 }
1737
1265 return 0; 1738 return 0;
1266} 1739}
1267 1740
@@ -1391,6 +1864,7 @@ static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1391 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU), 1864 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1392 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW), 1865 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1393 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER), 1866 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1867 bit_name(IDENTIFIER),
1394 { .name = NULL, } 1868 { .name = NULL, }
1395 }; 1869 };
1396#undef bit_name 1870#undef bit_name
@@ -1458,6 +1932,7 @@ int perf_evsel__fprintf(struct perf_evsel *evsel,
1458 if_print(exclude_hv); 1932 if_print(exclude_hv);
1459 if_print(exclude_idle); 1933 if_print(exclude_idle);
1460 if_print(mmap); 1934 if_print(mmap);
1935 if_print(mmap2);
1461 if_print(comm); 1936 if_print(comm);
1462 if_print(freq); 1937 if_print(freq);
1463 if_print(inherit_stat); 1938 if_print(inherit_stat);
@@ -1482,7 +1957,7 @@ out:
1482bool perf_evsel__fallback(struct perf_evsel *evsel, int err, 1957bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1483 char *msg, size_t msgsize) 1958 char *msg, size_t msgsize)
1484{ 1959{
1485 if ((err == ENOENT || err == ENXIO) && 1960 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
1486 evsel->attr.type == PERF_TYPE_HARDWARE && 1961 evsel->attr.type == PERF_TYPE_HARDWARE &&
1487 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) { 1962 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1488 /* 1963 /*