xref: /freebsd/lib/libsysdecode/linux.c (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Dmitry Chagin <dchagin@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
30 #include <sys/proc.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <sysdecode.h>
34 
35 #include "support.h"
36 
37 #ifdef __aarch64__
38 #include <arm64/linux/linux.h>
39 #elif __i386__
40 #include <i386/linux/linux.h>
41 #elif __amd64__
42 #include <amd64/linux/linux.h>
43 #else
44 #error "Unsupported Linux arch"
45 #endif
46 
47 #include <compat/linux/linux.h>
48 #include <compat/linux/linux_file.h>
49 #include <compat/linux/linux_fork.h>
50 #include <compat/linux/linux_time.h>
51 
52 #define	X(a,b)	{ a, #b },
53 #define	XEND	{ 0, NULL }
54 
55 #define	TABLE_START(n)	static struct name_table n[] = {
56 #define	TABLE_ENTRY	X
57 #define	TABLE_END	XEND };
58 
59 #include "tables_linux.h"
60 
61 #undef TABLE_START
62 #undef TABLE_ENTRY
63 #undef TABLE_END
64 
65 static const char *linux_signames[] = {
66 	[LINUX_SIGHUP] = "SIGHUP",
67 	[LINUX_SIGINT] = "SIGINT",
68 	[LINUX_SIGQUIT] = "SIGQUIT",
69 	[LINUX_SIGILL] = "SIGILL",
70 	[LINUX_SIGTRAP] = "SIGTRAP",
71 	[LINUX_SIGABRT] = "SIGABRT",
72 	[LINUX_SIGBUS] = "SIGBUS",
73 	[LINUX_SIGFPE] = "SIGFPE",
74 	[LINUX_SIGKILL] = "SIGKILL",
75 	[LINUX_SIGUSR1] = "SIGUSR1",
76 	[LINUX_SIGSEGV] = "SIGSEGV",
77 	[LINUX_SIGUSR2] = "SIGUSR2",
78 	[LINUX_SIGPIPE] = "SIGPIPE",
79 	[LINUX_SIGALRM] = "SIGALRM",
80 	[LINUX_SIGTERM] = "SIGTERM",
81 	[LINUX_SIGSTKFLT] = "SIGSTKFLT",
82 	[LINUX_SIGCHLD] = "SIGCHLD",
83 	[LINUX_SIGCONT] = "SIGCONT",
84 	[LINUX_SIGSTOP] = "SIGSTOP",
85 	[LINUX_SIGTSTP] = "SIGTSTP",
86 	[LINUX_SIGTTIN] = "SIGTTIN",
87 	[LINUX_SIGTTOU] = "SIGTTOU",
88 	[LINUX_SIGURG] = "SIGURG",
89 	[LINUX_SIGXCPU] = "SIGXCPU",
90 	[LINUX_SIGXFSZ] = "SIGXFSZ",
91 	[LINUX_SIGVTALRM] = "SIGVTALRM",
92 	[LINUX_SIGPROF] = "SIGPROF",
93 	[LINUX_SIGWINCH] = "SIGWINCH",
94 	[LINUX_SIGIO] = "SIGIO",
95 	[LINUX_SIGPWR] = "SIGPWR",
96 	[LINUX_SIGSYS] = "SIGSYS",
97 
98 	[LINUX_SIGRTMIN] = "SIGCANCEL",
99 	[LINUX_SIGRTMIN + 1] = "SIGSETXID",
100 	[LINUX_SIGRTMIN + 2] = "SIGRT2",
101 	[LINUX_SIGRTMIN + 3] = "SIGRT3",
102 	[LINUX_SIGRTMIN + 4] = "SIGRT4",
103 	[LINUX_SIGRTMIN + 5] = "SIGRT5",
104 	[LINUX_SIGRTMIN + 6] = "SIGRT6",
105 	[LINUX_SIGRTMIN + 7] = "SIGRT7",
106 	[LINUX_SIGRTMIN + 8] = "SIGRT8",
107 	[LINUX_SIGRTMIN + 9] = "SIGRT9",
108 	[LINUX_SIGRTMIN + 10] = "SIGRT10",
109 	[LINUX_SIGRTMIN + 11] = "SIGRT11",
110 	[LINUX_SIGRTMIN + 12] = "SIGRT12",
111 	[LINUX_SIGRTMIN + 13] = "SIGRT13",
112 	[LINUX_SIGRTMIN + 14] = "SIGRT14",
113 	[LINUX_SIGRTMIN + 15] = "SIGRT15",
114 	[LINUX_SIGRTMIN + 16] = "SIGRT16",
115 	[LINUX_SIGRTMIN + 17] = "SIGRT17",
116 	[LINUX_SIGRTMIN + 18] = "SIGRT18",
117 	[LINUX_SIGRTMIN + 19] = "SIGRT19",
118 	[LINUX_SIGRTMIN + 20] = "SIGRT20",
119 	[LINUX_SIGRTMIN + 21] = "SIGRT21",
120 	[LINUX_SIGRTMIN + 22] = "SIGRT22",
121 	[LINUX_SIGRTMIN + 23] = "SIGRT23",
122 	[LINUX_SIGRTMIN + 24] = "SIGRT24",
123 	[LINUX_SIGRTMIN + 25] = "SIGRT25",
124 	[LINUX_SIGRTMIN + 26] = "SIGRT26",
125 	[LINUX_SIGRTMIN + 27] = "SIGRT27",
126 	[LINUX_SIGRTMIN + 28] = "SIGRT28",
127 	[LINUX_SIGRTMIN + 29] = "SIGRT29",
128 	[LINUX_SIGRTMIN + 30] = "SIGRT30",
129 	[LINUX_SIGRTMIN + 31] = "SIGRT31",
130 	[LINUX_SIGRTMIN + 32] = "SIGRTMAX",
131 };
132 _Static_assert(nitems(linux_signames) == LINUX_SIGRTMAX + 1,
133     "invalid entries count in linux_signames");
134 
135 void
136 sysdecode_linux_clockid(FILE *fp, clockid_t which)
137 {
138 	const char *str;
139 	clockid_t ci;
140 	pid_t pid;
141 
142 	if (which >= 0) {
143 		str = lookup_value(clockids, which);
144 		if (str == NULL)
145 			fprintf(fp, "UNKNOWN(%d)", which);
146 		else
147 			fputs(str, fp);
148 		return;
149 	}
150 	if ((which & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD_MASK) {
151 		fputs("INVALID PERTHREAD|CLOCKFD", fp);
152 		goto pidp;
153 	}
154 	ci = LINUX_CPUCLOCK_WHICH(which);
155 	if (LINUX_CPUCLOCK_PERTHREAD(which) == true)
156 		fputs("THREAD|", fp);
157 	else
158 		fputs("PROCESS|", fp);
159 	str = lookup_value(clockcpuids, ci);
160 	if (str != NULL)
161 		fputs(str, fp);
162 	else {
163 		if (ci == LINUX_CLOCKFD)
164 			fputs("CLOCKFD", fp);
165 		else
166 			fprintf(fp, "UNKNOWN(%d)", which);
167 	}
168 
169 pidp:
170 	pid = LINUX_CPUCLOCK_ID(which);
171 	fprintf(fp, "(%d)", pid);
172 }
173 
174 const char *
175 sysdecode_linux_signal(int sig)
176 {
177 
178 	if ((unsigned)sig < nitems(linux_signames))
179 		return (linux_signames[sig]);
180 	return (NULL);
181 }
182 
183 const char *
184 sysdecode_linux_sigprocmask_how(int how)
185 {
186 
187 	return (lookup_value(sigprocmaskhow, how));
188 }
189 
190 bool
191 sysdecode_linux_clock_flags(FILE *fp, int flags, int *rem)
192 {
193 
194 	return (print_mask_int(fp, clockflags, flags, rem));
195 }
196 
197 bool
198 sysdecode_linux_atflags(FILE *fp, int flag, int *rem)
199 {
200 
201 	return (print_mask_int(fp, atflags, flag, rem));
202 }
203 
204 bool
205 sysdecode_linux_open_flags(FILE *fp, int flags, int *rem)
206 {
207 	bool printed;
208 	int mode;
209 	uintmax_t val;
210 
211 	mode = flags & LINUX_O_ACCMODE;
212 	flags &= ~LINUX_O_ACCMODE;
213 	switch (mode) {
214 	case LINUX_O_RDONLY:
215 		fputs("O_RDONLY", fp);
216 		printed = true;
217 		mode = 0;
218 		break;
219 	case LINUX_O_WRONLY:
220 		fputs("O_WRONLY", fp);
221 		printed = true;
222 		mode = 0;
223 		break;
224 	case LINUX_O_RDWR:
225 		fputs("O_RDWR", fp);
226 		printed = true;
227 		mode = 0;
228 		break;
229 	default:
230 		printed = false;
231 	}
232 	val = (unsigned)flags;
233 	print_mask_part(fp, openflags, &val, &printed);
234 	if (rem != NULL)
235 		*rem = val | mode;
236 	return (printed);
237 }
238 
239 bool
240 sysdecode_linux_clone_flags(FILE *fp, int flags, int *rem)
241 {
242 	uintmax_t val;
243 	bool printed;
244 	int sig;
245 
246 	sig = flags & LINUX_CSIGNAL;
247 	if (sig != 0)
248 		fprintf(fp, "(%s)", sysdecode_linux_signal(sig));
249 	val = (unsigned)flags & ~LINUX_CSIGNAL;
250 	print_mask_part(fp, cloneflags, &val, &printed);
251 	if (rem != NULL)
252 		*rem = val;
253 	return (printed);
254 }
255