]> granicus.if.org Git - strace/blob - perf.c
Fix the length argument passed from print_iovec to decode_netlink
[strace] / perf.c
1 /*
2  * Copyright (c) 2013 Ben Noordhuis <info@bnoordhuis.nl>
3  * Copyright (c) 2013-2015 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "defs.h"
31
32 #include "perf_event_struct.h"
33
34 #include "xlat/hw_breakpoint_len.h"
35 #include "xlat/hw_breakpoint_type.h"
36 #include "xlat/perf_attr_size.h"
37 #include "xlat/perf_branch_sample_type.h"
38 #include "xlat/perf_event_open_flags.h"
39 #include "xlat/perf_event_read_format.h"
40 #include "xlat/perf_event_sample_format.h"
41 #include "xlat/perf_hw_cache_id.h"
42 #include "xlat/perf_hw_cache_op_id.h"
43 #include "xlat/perf_hw_cache_op_result_id.h"
44 #include "xlat/perf_hw_id.h"
45 #include "xlat/perf_sw_ids.h"
46 #include "xlat/perf_type_id.h"
47
48 struct pea_desc {
49         struct perf_event_attr *attr;
50         uint32_t size;
51 };
52
53 static void
54 free_pea_desc(void *pea_desc_ptr)
55 {
56         struct pea_desc *desc = pea_desc_ptr;
57
58         free(desc->attr);
59         free(desc);
60 }
61
62 static int
63 fetch_perf_event_attr(struct tcb *const tcp, const kernel_ulong_t addr)
64 {
65         struct pea_desc *desc;
66         struct perf_event_attr *attr;
67         uint32_t size;
68
69         if (umove(tcp, addr + offsetof(struct perf_event_attr, size), &size)) {
70                 printaddr(addr);
71                 return 1;
72         }
73
74         if (size > sizeof(*attr))
75                 size = sizeof(*attr);
76
77         if (!size)
78                 size = PERF_ATTR_SIZE_VER0;
79
80         /*
81          * Kernel (rightfully) deems invalid attribute structures with size less
82          * than first published format size, and we do the same.
83          */
84         if (size < PERF_ATTR_SIZE_VER0) {
85                 printaddr(addr);
86                 return 1;
87         }
88
89         if (abbrev(tcp))
90                 size = offsetofend(struct perf_event_attr, config);
91
92         /* Size should be multiple of 8, but kernel doesn't check for it */
93         /* size &= ~7; */
94
95         attr = xcalloc(1, sizeof(*attr));
96
97         if (umoven_or_printaddr(tcp, addr, size, attr)) {
98                 free(attr);
99
100                 return 1;
101         }
102
103         desc = xmalloc(sizeof(*desc));
104
105         desc->attr = attr;
106         desc->size = size;
107
108         set_tcb_priv_data(tcp, desc, free_pea_desc);
109
110         return 0;
111 }
112
113 #define PRINT_XLAT(prefix, xlat, x, dflt) \
114         do { \
115                 tprints(prefix); \
116                 printxval_search(xlat, x, dflt); \
117         } while (0)
118
119 static void
120 print_perf_event_attr(struct tcb *const tcp, const kernel_ulong_t addr)
121 {
122         static const char *precise_ip_desc[] = {
123                 "arbitrary skid",
124                 "constant skid",
125                 "requested to have 0 skid",
126                 "must have 0 skid",
127         };
128
129         struct pea_desc *desc;
130         struct perf_event_attr *attr;
131         uint32_t size;
132         uint32_t new_size;
133         int use_new_size = 0;
134
135         /*
136          * Amusingly, kernel accepts structures with only part of the field
137          * present, so we making check like this (instead of checking
138          * offsetofend against size) in order to print fields as kernel sees
139          * them. This also should work great on big endian architectures.
140          */
141         #define _PERF_CHECK_FIELD(_field) \
142                 do { \
143                         if (offsetof(struct perf_event_attr, _field) >= size) \
144                                 goto print_perf_event_attr_out; \
145                 } while (0)
146
147         desc = get_tcb_priv_data(tcp);
148
149         attr = desc->attr;
150         size = desc->size;
151
152         /* The only error which expected to change size field currently */
153         if (tcp->u_error == E2BIG) {
154                 if (umove(tcp, addr + offsetof(struct perf_event_attr, size),
155                     &new_size))
156                         use_new_size = -1;
157                 else
158                         use_new_size = 1;
159         }
160
161         PRINT_XLAT("{type=", perf_type_id, attr->type, "PERF_TYPE_???");
162         tprints(", size=");
163         printxval(perf_attr_size, attr->size, "PERF_ATTR_SIZE_???");
164
165         if (use_new_size) {
166                 tprints(" => ");
167
168                 if (use_new_size > 0)
169                         printxval(perf_attr_size, new_size,
170                                   "PERF_ATTR_SIZE_???");
171                 else
172                         tprints("???");
173         }
174
175         switch (attr->type) {
176         case PERF_TYPE_HARDWARE:
177                 PRINT_XLAT(", config=", perf_hw_id, attr->config,
178                            "PERF_COUNT_HW_???");
179                 break;
180         case PERF_TYPE_SOFTWARE:
181                 PRINT_XLAT(", config=", perf_sw_ids, attr->config,
182                            "PERF_COUNT_SW_???");
183                 break;
184         case PERF_TYPE_TRACEPOINT:
185                 /*
186                  * "The value to use in config can be obtained from under
187                  * debugfs tracing/events/../../id if ftrace is enabled in the
188                  * kernel."
189                  */
190                 tprintf(", config=%" PRIu64, attr->config);
191                 break;
192         case PERF_TYPE_HW_CACHE:
193                 /*
194                  * (perf_hw_cache_id) | (perf_hw_cache_op_id << 8) |
195                  * (perf_hw_cache_op_result_id << 16)
196                  */
197                 PRINT_XLAT(", config=", perf_hw_cache_id, attr->config & 0xFF,
198                            "PERF_COUNT_HW_CACHE_???");
199                 PRINT_XLAT("|", perf_hw_cache_op_id, (attr->config >> 8) & 0xFF,
200                            "PERF_COUNT_HW_CACHE_OP_???");
201                 /*
202                  * Current code (see set_ext_hw_attr in arch/x86/events/core.c,
203                  * tile_map_cache_event in arch/tile/kernel/perf_event.c,
204                  * arc_pmu_cache_event in arch/arc/kernel/perf_event.c,
205                  * hw_perf_cache_event in arch/blackfin/kernel/perf_event.c,
206                  * _hw_perf_cache_event in arch/metag/kernel/perf/perf_event.c,
207                  * mipspmu_map_cache_event in arch/mips/kernel/perf_event_mipsxx.c,
208                  * hw_perf_cache_event in arch/powerpc/perf/core-book3s.c,
209                  * hw_perf_cache_event in arch/powerpc/perf/core-fsl-emb.c,
210                  * hw_perf_cache_event in arch/sh/kernel/perf_event.c,
211                  * sparc_map_cache_event in arch/sparc/kernel/perf_event.c,
212                  * xtensa_pmu_cache_event in arch/xtensa/kernel/perf_event.c,
213                  * armpmu_map_cache_event in drivers/perf/arm_pmu.c) assumes
214                  * that cache result is 8 bits in size.
215                  */
216                 PRINT_XLAT("<<8|", perf_hw_cache_op_result_id,
217                            (attr->config >> 16) & 0xFF,
218                            "PERF_COUNT_HW_CACHE_RESULT_???");
219                 tprints("<<16");
220                 if (attr->config >> 24)
221                         tprintf("|%#" PRIx64 "<<24 "
222                                 "/* PERF_COUNT_HW_CACHE_??? */",
223                                 attr->config >> 24);
224                 break;
225         case PERF_TYPE_RAW:
226                 /*
227                  * "If type is PERF_TYPE_RAW, then a custom "raw" config
228                  * value is needed. Most CPUs support events that are not
229                  * covered by the "generalized" events. These are
230                  * implementation defined; see your CPU manual (for example the
231                  * Intel Volume 3B documentation or the AMD BIOS and Kernel
232                  * Developer Guide). The libpfm4 library can be used to
233                  * translate from the name in the architectural manuals
234                  * to the raw hex value perf_event_open() expects in this
235                  * field."
236                  */
237         case PERF_TYPE_BREAKPOINT:
238                 /*
239                  * "If type is PERF_TYPE_BREAKPOINT, then leave config set
240                  * to zero. Its parameters are set in other places."
241                  */
242         default:
243                 tprintf(", config=%#" PRIx64, attr->config);
244                 break;
245         }
246
247         if (abbrev(tcp))
248                 goto print_perf_event_attr_out;
249
250         if (attr->freq)
251                 tprintf(", sample_freq=%" PRIu64, attr->sample_freq);
252         else
253                 tprintf(", sample_period=%" PRIu64, attr->sample_period);
254
255         tprints(", sample_type=");
256         printflags64(perf_event_sample_format, attr->sample_type,
257                 "PERF_SAMPLE_???");
258
259         tprints(", read_format=");
260         printflags64(perf_event_read_format, attr->read_format,
261                 "PERF_FORMAT_???");
262
263         tprintf(", disabled=%u"
264                 ", inherit=%u"
265                 ", pinned=%u"
266                 ", exclusive=%u"
267                 ", exclusive_user=%u"
268                 ", exclude_kernel=%u"
269                 ", exclude_hv=%u"
270                 ", exclude_idle=%u"
271                 ", mmap=%u"
272                 ", comm=%u"
273                 ", freq=%u"
274                 ", inherit_stat=%u"
275                 ", enable_on_exec=%u"
276                 ", task=%u"
277                 ", watermark=%u"
278                 ", precise_ip=%u /* %s */"
279                 ", mmap_data=%u"
280                 ", sample_id_all=%u"
281                 ", exclude_host=%u"
282                 ", exclude_guest=%u"
283                 ", exclude_callchain_kernel=%u"
284                 ", exclude_callchain_user=%u"
285                 ", mmap2=%u"
286                 ", comm_exec=%u"
287                 ", use_clockid=%u"
288                 ", context_switch=%u"
289                 ", write_backward=%u",
290                 attr->disabled,
291                 attr->inherit,
292                 attr->pinned,
293                 attr->exclusive,
294                 attr->exclude_user,
295                 attr->exclude_kernel,
296                 attr->exclude_hv,
297                 attr->exclude_idle,
298                 attr->mmap,
299                 attr->comm,
300                 attr->freq,
301                 attr->inherit_stat,
302                 attr->enable_on_exec,
303                 attr->task,
304                 attr->watermark,
305                 attr->precise_ip, precise_ip_desc[attr->precise_ip],
306                 attr->mmap_data,
307                 attr->sample_id_all,
308                 attr->exclude_host,
309                 attr->exclude_guest,
310                 attr->exclude_callchain_kernel,
311                 attr->exclude_callchain_user,
312                 attr->mmap2,
313                 attr->comm_exec,
314                 attr->use_clockid,
315                 attr->context_switch,
316                 attr->write_backward);
317
318         /*
319          * Print it only in case it is non-zero, since it may contain flags we
320          * are not aware about.
321          */
322         if (attr->__reserved_1)
323                 tprintf(", __reserved_1=%#" PRIx64 " /* Bits 63..28 */",
324                         (uint64_t) attr->__reserved_1);
325
326         if (attr->watermark)
327                 tprintf(", wakeup_watermark=%u", attr->wakeup_watermark);
328         else
329                 tprintf(", wakeup_events=%u", attr->wakeup_events);
330
331         if (attr->type == PERF_TYPE_BREAKPOINT)
332                 /* Any combination of R/W with X is deemed invalid */
333                 PRINT_XLAT(", bp_type=", hw_breakpoint_type, attr->bp_type,
334                            (attr->bp_type <=
335                                    (HW_BREAKPOINT_X | HW_BREAKPOINT_RW)) ?
336                                            "HW_BREAKPOINT_INVALID" :
337                                            "HW_BREAKPOINT_???");
338
339         if (attr->type == PERF_TYPE_BREAKPOINT)
340                 tprintf(", bp_addr=%#" PRIx64, attr->bp_addr);
341         else
342                 tprintf(", config1=%#" PRIx64, attr->config1);
343
344         /*
345          * Fields after bp_addr/config1 are optional and may not present; check
346          * against size is needed.
347          */
348
349         _PERF_CHECK_FIELD(bp_len);
350         if (attr->type == PERF_TYPE_BREAKPOINT)
351                 tprintf(", bp_len=%" PRIu64, attr->bp_len);
352         else
353                 tprintf(", config2=%#" PRIx64, attr->config2);
354
355         _PERF_CHECK_FIELD(branch_sample_type);
356         if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
357                 tprints(", branch_sample_type=");
358                 printflags64(perf_branch_sample_type, attr->branch_sample_type,
359                              "PERF_SAMPLE_BRANCH_???");
360         }
361
362         _PERF_CHECK_FIELD(sample_regs_user);
363         /*
364          * "This bit mask defines the set of user CPU registers to dump on
365          * samples. The layout of the register mask is architecture-specific and
366          * described in the kernel header
367          * arch/ARCH/include/uapi/asm/perf_regs.h."
368          */
369         tprintf(", sample_regs_user=%#" PRIx64, attr->sample_regs_user);
370
371         _PERF_CHECK_FIELD(sample_stack_user);
372         /*
373          * "size of the user stack to dump if PERF_SAMPLE_STACK_USER is
374          * specified."
375          */
376         if (attr->sample_type & PERF_SAMPLE_STACK_USER)
377                 tprintf(", sample_stack_user=%#" PRIx32,
378                         attr->sample_stack_user);
379
380         if (attr->use_clockid) {
381                 _PERF_CHECK_FIELD(clockid);
382                 tprints(", clockid=");
383                 printxval(clocknames, attr->clockid, "CLOCK_???");
384         }
385
386         _PERF_CHECK_FIELD(sample_regs_intr);
387         tprintf(", sample_regs_intr=%#" PRIx64, attr->sample_regs_intr);
388
389         _PERF_CHECK_FIELD(aux_watermark);
390         tprintf(", aux_watermark=%" PRIu32, attr->aux_watermark);
391
392         _PERF_CHECK_FIELD(sample_max_stack);
393         tprintf(", sample_max_stack=%" PRIu16, attr->sample_max_stack);
394
395         /* _PERF_CHECK_FIELD(__reserved_2);
396         tprintf(", __reserved2=%" PRIu16, attr->__reserved_2); */
397
398 print_perf_event_attr_out:
399         if ((attr->size && (attr->size > size)) ||
400             (!attr->size && (size < PERF_ATTR_SIZE_VER0)))
401                 tprints(", ...");
402
403         tprints("}");
404 }
405
406 SYS_FUNC(perf_event_open)
407 {
408         /*
409          * We try to copy out the whole structure on entering in order to check
410          * size value on exiting. We do not check the rest of the fields because
411          * they shouldn't be changed, but copy the whole structure instead
412          * of just size field because they could.
413          */
414         if (entering(tcp)) {
415                 if (!fetch_perf_event_attr(tcp, tcp->u_arg[0]))
416                         return 0;
417         } else {
418                 print_perf_event_attr(tcp, tcp->u_arg[0]);
419         }
420
421         tprintf(", %d, %d, %d, ",
422                 (int) tcp->u_arg[1],
423                 (int) tcp->u_arg[2],
424                 (int) tcp->u_arg[3]);
425         printflags64(perf_event_open_flags, tcp->u_arg[4], "PERF_FLAG_???");
426
427         return RVAL_DECODED | RVAL_FD;
428 }