2 * Check decoding of perf_event_open syscall.
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include <asm/unistd.h>
33 #if defined(__NR_perf_event_open) && defined(HAVE_LINUX_PERF_EVENT_H)
39 # include <linux/perf_event.h>
42 # include "xlat/perf_event_open_flags.h"
44 #if ULONG_MAX > UINT_MAX
45 #define LONG_STR_PREFIX "ffffffff"
47 #define LONG_STR_PREFIX ""
50 static const char *printaddr(void *ptr)
52 static char buf[sizeof("0x") + sizeof(void *) * 2];
57 snprintf(buf, sizeof(buf), "%#lx", (unsigned long)ptr);
65 struct perf_event_attr *attr = tail_alloc(sizeof(*attr));
67 attr->type = PERF_TYPE_HARDWARE;
68 attr->size = sizeof(*attr);
71 struct perf_event_attr *attr;
76 const char *flags_str;
78 { NULL, 0xfacef00d, 0xbadabba7, -1,
79 (unsigned long) 0xFFFFFFFFFFFFFFFFLLU,
80 "PERF_FLAG_FD_NO_GROUP|PERF_FLAG_FD_OUTPUT|"
81 "PERF_FLAG_PID_CGROUP|PERF_FLAG_FD_CLOEXEC|"
82 "0x" LONG_STR_PREFIX "fffffff0"
87 PERF_FLAG_FD_CLOEXEC, "PERF_FLAG_FD_CLOEXEC" },
88 { attr - 1, -100, 100, 0xface1e55,
89 PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT |
90 PERF_FLAG_PID_CGROUP | PERF_FLAG_FD_CLOEXEC,
91 "PERF_FLAG_FD_NO_GROUP|PERF_FLAG_FD_OUTPUT|"
92 "PERF_FLAG_PID_CGROUP|PERF_FLAG_FD_CLOEXEC" },
97 for (i = 0; i < ARRAY_SIZE(args); i++) {
98 rc = syscall(__NR_perf_event_open, args[i].attr, args[i].pid,
99 args[i].cpu, args[i].group_fd, args[i].flags);
100 printf("perf_event_open(%s, %d, %d, %d, %s) = %s\n",
101 printaddr(args[i].attr), args[i].pid, args[i].cpu,
102 args[i].group_fd, args[i].flags_str, sprintrc(rc));
105 puts("+++ exited with 0 +++");
111 SKIP_MAIN_UNDEFINED("__NR_perf_event_open && HAVE_LINUX_PERF_EVENT_H");