]> granicus.if.org Git - strace/blob - tests/perf_event_open_nonverbose.c
tests: use TAIL_ALLOC_OBJECT_CONST_PTR
[strace] / tests / perf_event_open_nonverbose.c
1 /*
2  * Check decoding of perf_event_open syscall.
3  *
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 "tests.h"
31 #include <asm/unistd.h>
32
33 #if defined(__NR_perf_event_open) && defined(HAVE_LINUX_PERF_EVENT_H)
34
35 # include <limits.h>
36 # include <stdio.h>
37 # include <unistd.h>
38
39 # include <linux/perf_event.h>
40
41 # include "xlat.h"
42 # include "xlat/perf_event_open_flags.h"
43
44 #if ULONG_MAX > UINT_MAX
45 #define LONG_STR_PREFIX "ffffffff"
46 #else
47 #define LONG_STR_PREFIX ""
48 #endif
49
50 static const char *printaddr(void *ptr)
51 {
52         static char buf[sizeof("0x") + sizeof(void *) * 2];
53
54         if (ptr == NULL)
55                 return "NULL";
56
57         snprintf(buf, sizeof(buf), "%#lx", (unsigned long)ptr);
58
59         return buf;
60 }
61
62 int
63 main(void)
64 {
65         TAIL_ALLOC_OBJECT_CONST_PTR(struct perf_event_attr, attr);
66
67         attr->type = PERF_TYPE_HARDWARE;
68         attr->size = sizeof(*attr);
69
70         struct {
71                 struct perf_event_attr *attr;
72                 pid_t pid;
73                 int cpu;
74                 int group_fd;
75                 unsigned long flags;
76                 const char *flags_str;
77         } args[] = {
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"
83                         },
84                 { attr + 1, 0,          0,          0,
85                         0, "0" },
86                 { attr,     -1,         -1,         1,
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" },
93         };
94         size_t i;
95         int rc;
96
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));
103         }
104
105         puts("+++ exited with 0 +++");
106         return 0;
107 }
108
109 #else
110
111 SKIP_MAIN_UNDEFINED("__NR_perf_event_open && HAVE_LINUX_PERF_EVENT_H");
112
113 #endif