1 /*
2 * Check decoding of perf_event_open syscall.
3 *
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * Copyright (c) 2016-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include "scno.h"
13
14 #include <limits.h>
15 #include <stdio.h>
16 #include <unistd.h>
17
18 #include <linux/perf_event.h>
19
20 #include "xlat.h"
21 #include "xlat/perf_event_open_flags.h"
22
23 #if ULONG_MAX > UINT_MAX
24 # define LONG_STR_PREFIX "ffffffff"
25 #else
26 # define LONG_STR_PREFIX ""
27 #endif
28
29 static const char *printaddr(void *ptr)
30 {
31 static char buf[sizeof("0x") + sizeof(void *) * 2];
32
33 if (ptr == NULL)
34 return "NULL";
35
36 snprintf(buf, sizeof(buf), "%#lx", (unsigned long)ptr);
37
38 return buf;
39 }
40
41 int
42 main(void)
43 {
44 TAIL_ALLOC_OBJECT_CONST_PTR(struct perf_event_attr, attr);
45
46 attr->type = PERF_TYPE_HARDWARE;
47 attr->size = sizeof(*attr);
48
49 struct {
50 struct perf_event_attr *attr;
51 pid_t pid;
52 int cpu;
53 int group_fd;
54 unsigned long flags;
55 const char *flags_str;
56 } args[] = {
57 { NULL, 0xfacef00d, 0xbadabba7, -1,
58 (unsigned long) 0xFFFFFFFFFFFFFFFFLLU,
59 "PERF_FLAG_FD_NO_GROUP|PERF_FLAG_FD_OUTPUT|"
60 "PERF_FLAG_PID_CGROUP|PERF_FLAG_FD_CLOEXEC|"
61 "0x" LONG_STR_PREFIX "fffffff0"
62 },
63 { attr + 1, 0, 0, 0,
64 0, "0" },
65 { attr, -1, -1, 1,
66 PERF_FLAG_FD_CLOEXEC, "PERF_FLAG_FD_CLOEXEC" },
67 { attr - 1, -100, 100, 0xface1e55,
68 PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT |
69 PERF_FLAG_PID_CGROUP | PERF_FLAG_FD_CLOEXEC,
70 "PERF_FLAG_FD_NO_GROUP|PERF_FLAG_FD_OUTPUT|"
71 "PERF_FLAG_PID_CGROUP|PERF_FLAG_FD_CLOEXEC" },
72 };
73 int rc;
74
75 for (size_t i = 0; i < ARRAY_SIZE(args); ++i) {
76 rc = syscall(__NR_perf_event_open, args[i].attr, args[i].pid,
77 args[i].cpu, args[i].group_fd, args[i].flags);
78 printf("perf_event_open(%s, %d, %d, %d, %s) = %s\n",
79 printaddr(args[i].attr), args[i].pid, args[i].cpu,
80 args[i].group_fd, args[i].flags_str, sprintrc(rc));
81 }
82
83 puts("+++ exited with 0 +++");
84 return 0;
85 }