File: ioctl_counter.c

package info (click to toggle)
strace 6.1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 64,424 kB
  • sloc: ansic: 160,349; sh: 9,223; makefile: 3,817; cpp: 944; awk: 353; perl: 267; exp: 62; sed: 9
file content (133 lines) | stat: -rw-r--r-- 3,797 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Check decoding of COUNTER_* commands of ioctl syscall.
 *
 * Copyright (c) 2022 Eugene Syromyatnikov <evgsyr@gmail.com>.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"

#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <linux/ioctl.h>
#include <linux/counter.h>


/* A hack for handling different types of _IOC() on various platforms */
#if XLAT_RAW
# define XLAT_ARGS_U(a_) (unsigned int) (a_)
#elif XLAT_VERBOSE
# define XLAT_ARGS_U(a_) (unsigned int) (a_), #a_
#else
# define XLAT_ARGS_U(a_) #a_
#endif

static const char *errstr;

static long
sys_ioctl(kernel_long_t fd, kernel_ulong_t cmd, kernel_ulong_t arg)
{
	const long rc = syscall(__NR_ioctl, fd, cmd, arg);
	errstr = sprintrc(rc);
	return rc;
}

int
main(void)
{
	static const struct {
		uint32_t val;
		const char *str;
	} dirs[] = {
		{ ARG_STR(_IOC_NONE) },
		{ ARG_STR(_IOC_READ) },
		{ ARG_STR(_IOC_WRITE) },
		{ ARG_STR(_IOC_READ|_IOC_WRITE) },
	};
	static const kernel_ulong_t magic =
		(kernel_ulong_t) 0xdeadbeefbadc0dedULL;

	/* Unknown counter ioctl */
	for (size_t i = 0; i < ARRAY_SIZE(dirs); i++) {
		for (unsigned int j = 0; j < 32; j += 1) {
			sys_ioctl(-1, _IOC(dirs[i].val, '>', 3, j), magic);
			printf("ioctl(-1, "
			       XLAT_KNOWN(%#x, "_IOC(%s, 0x3e, 0x3, %#x)")
			       ", %#lx) = %s\n",
#if XLAT_RAW || XLAT_VERBOSE
			       (unsigned int) _IOC(dirs[i].val, '>', 3, j),
#endif
#if !XLAT_RAW
			       dirs[i].str, j,
#endif
			       (unsigned long) magic, errstr);
		}
	}

	/* COUNTER_ADD_WATCH_IOCTL */
	static const struct {
		struct counter_watch val;;
		const char *str;
	} watches[] = {
		{ { { 0 } },
		  "{component={type=" XLAT_KNOWN(0, "COUNTER_COMPONENT_NONE")
		  ", scope=" XLAT_KNOWN(0, "COUNTER_SCOPE_DEVICE")
		  ", parent=0, id=0}"
		  ", event=" XLAT_KNOWN(0, "COUNTER_EVENT_OVERFLOW")
		  ", channel=0}" },
		{ { { COUNTER_COMPONENT_EXTENSION, COUNTER_SCOPE_COUNT,
		      23, 42 }, COUNTER_EVENT_CAPTURE, 69 },
		  "{component="
		  "{type=" XLAT_KNOWN(0x5, "COUNTER_COMPONENT_EXTENSION")
		  ", scope=" XLAT_KNOWN(0x2, "COUNTER_SCOPE_COUNT")
		  ", parent=23, id=42}"
		  ", event=" XLAT_KNOWN(0x6, "COUNTER_EVENT_CAPTURE")
		  ", channel=69}" },
		{ { { COUNTER_COMPONENT_EXTENSION + 1, COUNTER_SCOPE_COUNT + 1,
		      142, 160 }, COUNTER_EVENT_CAPTURE + 1, 173 },
		  "{component={type=" XLAT_UNKNOWN(0x6, "COUNTER_COMPONENT_???")
		  ", scope=" XLAT_UNKNOWN(0x3, "COUNTER_SCOPE_???")
		  ", parent=142, id=160}"
		  ", event=" XLAT_UNKNOWN(0x7, "COUNTER_EVENT_???")
		  ", channel=173}" },
	};
	TAIL_ALLOC_OBJECT_CONST_PTR(struct counter_watch, watch);

	sys_ioctl(-1, COUNTER_ADD_WATCH_IOCTL, 0);
	printf("ioctl(-1, " XLAT_FMT ", NULL) = %s\n",
	       XLAT_ARGS_U(COUNTER_ADD_WATCH_IOCTL), errstr);

	sys_ioctl(-1, COUNTER_ADD_WATCH_IOCTL, (uintptr_t) watch + 1);
	printf("ioctl(-1, " XLAT_FMT ", %p) = %s\n",
	       XLAT_ARGS_U(COUNTER_ADD_WATCH_IOCTL),
	       (char *) watch + 1, errstr);

	for (size_t i = 0; i < ARRAY_SIZE(watches); i++) {
		memcpy(watch, &watches[i].val, sizeof(watches[i].val));
		sys_ioctl(-1, COUNTER_ADD_WATCH_IOCTL, (uintptr_t) watch);
		printf("ioctl(-1, " XLAT_FMT ", %s) = %s\n",
		       XLAT_ARGS_U(COUNTER_ADD_WATCH_IOCTL),
		       watches[i].str, errstr);
	}

	/* COUNTER_ENABLE_EVENTS_IOCTL */
	sys_ioctl(-1, COUNTER_ENABLE_EVENTS_IOCTL, magic);
	printf("ioctl(-1, " XLAT_FMT ") = %s\n",
	       XLAT_ARGS_U(COUNTER_ENABLE_EVENTS_IOCTL), errstr);

	/* COUNTER_DISABLE_EVENTS_IOCTL */
	sys_ioctl(-1, COUNTER_DISABLE_EVENTS_IOCTL, magic);
	printf("ioctl(-1, " XLAT_FMT ") = %s\n",
	       XLAT_ARGS_U(COUNTER_DISABLE_EVENTS_IOCTL), errstr);

	puts("+++ exited with 0 +++");
	return 0;
}