linux.c 5.45 KB
Newer Older
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
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2022 Dmitry Chagin <dchagin@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/uio.h>
#include <sys/ktrace.h>
34
35
#include <err.h>
#include <errno.h>
36
#include <stddef.h>
37
38
#include <stdlib.h>
#include <string.h>
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <sysdecode.h>

#include "kdump.h"

#ifdef __amd64__
#include <amd64/linux/linux_syscall.h>
#include <amd64/linux32/linux32_syscall.h>
#elif __aarch64__
#include <arm64/linux/linux_syscall.h>
#elif __i386__
#include <i386/linux/linux_syscall.h>
#endif

52
53
#include <compat/linux/linux.h>

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
static void
print_linux_signal(int signo)
{
	const char *signame;

	signame = sysdecode_linux_signal(signo);
	if (signame != NULL)
		printf("%s", signame);
	else
		printf("SIG %d", signo);
}

void
ktrsyscall_linux(struct ktr_syscall *ktr, register_t **resip,
    int *resnarg, char *resc)
{
	int narg = ktr->ktr_narg;
	register_t *ip, *first;
	int quad_align, quad_slots;
	char c;

	ip = first = &ktr->ktr_args[0];
	c = *resc;
	quad_align = 0;
	quad_slots = 1;
	switch (ktr->ktr_code) {
	case LINUX_SYS_linux_clock_gettime:
	case LINUX_SYS_linux_clock_settime:
	case LINUX_SYS_linux_clock_getres:
	case LINUX_SYS_linux_timer_create:
		putchar('(');
		sysdecode_linux_clockid(stdout, *ip);
		c = ',';
		ip++;
		narg--;
		break;
	case LINUX_SYS_linux_kill:
	case LINUX_SYS_linux_tkill:
	case LINUX_SYS_linux_rt_sigqueueinfo:
		print_number(ip, narg, c);
		putchar(',');
		print_linux_signal(*ip);
		ip++;
		narg--;
		break;
	case LINUX_SYS_linux_tgkill:
	case LINUX_SYS_linux_rt_tgsigqueueinfo:
		print_number(ip, narg, c);
		print_number(ip, narg, c);
		putchar(',');
		print_linux_signal(*ip);
		ip++;
		narg--;
		break;
	case LINUX_SYS_linux_rt_sigaction:
		putchar('(');
		print_linux_signal(*ip);
		ip++;
		narg--;
		c = ',';
		break;
	case LINUX_SYS_linux_ftruncate:
	case LINUX_SYS_linux_truncate:
		print_number(ip, narg, c);
		print_number64(first, ip, narg, c);
		break;
	}
	*resc = c;
	*resip = ip;
	*resnarg = narg;
}

#if defined(__amd64__)
void
ktrsyscall_linux32(struct ktr_syscall *ktr, register_t **resip,
    int *resnarg, char *resc)
{
	int narg = ktr->ktr_narg;
	register_t *ip, *first;
	int quad_align, quad_slots;
	char c;

	ip = first = &ktr->ktr_args[0];
	c = *resc;
	quad_align = 0;
	quad_slots = 2;
	switch (ktr->ktr_code) {
	case LINUX32_SYS_linux_clock_gettime:
	case LINUX32_SYS_linux_clock_settime:
	case LINUX32_SYS_linux_clock_getres:
	case LINUX32_SYS_linux_timer_create:
	case LINUX32_SYS_linux_clock_gettime64:
	case LINUX32_SYS_linux_clock_settime64:
	case LINUX32_SYS_linux_clock_getres_time64:
		putchar('(');
		sysdecode_linux_clockid(stdout, *ip);
		c = ',';
		ip++;
		narg--;
		break;
	case LINUX32_SYS_linux_kill:
	case LINUX32_SYS_linux_tkill:
	case LINUX32_SYS_linux_rt_sigqueueinfo:
		print_number(ip, narg, c);
		putchar(',');
		print_linux_signal(*ip);
		ip++;
		narg--;
		break;
	case LINUX32_SYS_linux_tgkill:
	case LINUX32_SYS_linux_rt_tgsigqueueinfo:
		print_number(ip, narg, c);
		print_number(ip, narg, c);
		putchar(',');
		print_linux_signal(*ip);
		ip++;
		narg--;
		break;
	case LINUX32_SYS_linux_signal:
	case LINUX32_SYS_linux_sigaction:
	case LINUX32_SYS_linux_rt_sigaction:
		putchar('(');
		print_linux_signal(*ip);
		ip++;
		narg--;
		c = ',';
		break;
	case LINUX32_SYS_linux_ftruncate:
	case LINUX32_SYS_linux_truncate:
		print_number(ip, narg, c);
		print_number64(first, ip, narg, c);
		break;
	}
	*resc = c;
	*resip = ip;
	*resnarg = narg;
}
#endif /* __amd64__ */
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

static void
ktrsigset(const char *name, const l_sigset_t *mask, size_t sz)
{
	unsigned long i, c;

	printf("%s [ ", name);
	c = 0;
	for (i = 1; i <= sz * CHAR_BIT; i++) {
		if (!LINUX_SIGISMEMBER(*mask, i))
			continue;
		if (c != 0)
			printf(", ");
		printf("%s", sysdecode_linux_signal(i));
		c++;
	}
	if (c == 0)
		printf("empty ]\n");
	else
		printf(" ]\n");
}

bool
ktrstruct_linux(const char *name, const char *data, size_t datalen)
{
	l_sigset_t mask;

	if (strcmp(name, "l_sigset_t") == 0) {
		/* Old Linux sigset_t is one word size. */
		if (datalen < sizeof(int) || datalen > sizeof(l_sigset_t))
			return (false);
		memcpy(&mask, data, datalen);
		ktrsigset(name, &mask, datalen);
	} else
		return (false);

	return (true);
}