xref: /freebsd/lib/libsysdecode/linux.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
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/types.h>
29 #include <sys/proc.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <sysdecode.h>
33 
34 #include "support.h"
35 
36 #ifdef __aarch64__
37 #include <arm64/linux/linux.h>
38 #elif __i386__
39 #include <i386/linux/linux.h>
40 #elif __amd64__
41 #include <amd64/linux/linux.h>
42 #else
43 #error "Unsupported Linux arch"
44 #endif
45 
46 #include <compat/linux/linux.h>
47 #include <compat/linux/linux_file.h>
48 #include <compat/linux/linux_fork.h>
49 #include <compat/linux/linux_time.h>
50 
51 #define	X(a,b)	{ a, #b },
52 #define	XEND	{ 0, NULL }
53 
54 #define	TABLE_START(n)	static struct name_table n[] = {
55 #define	TABLE_ENTRY	X
56 #define	TABLE_END	XEND };
57 
58 #include "tables_linux.h"
59 
60 #undef TABLE_START
61 #undef TABLE_ENTRY
62 #undef TABLE_END
63 
64 static const char *linux_signames[] = {
65 	[LINUX_SIGHUP] = "SIGHUP",
66 	[LINUX_SIGINT] = "SIGINT",
67 	[LINUX_SIGQUIT] = "SIGQUIT",
68 	[LINUX_SIGILL] = "SIGILL",
69 	[LINUX_SIGTRAP] = "SIGTRAP",
70 	[LINUX_SIGABRT] = "SIGABRT",
71 	[LINUX_SIGBUS] = "SIGBUS",
72 	[LINUX_SIGFPE] = "SIGFPE",
73 	[LINUX_SIGKILL] = "SIGKILL",
74 	[LINUX_SIGUSR1] = "SIGUSR1",
75 	[LINUX_SIGSEGV] = "SIGSEGV",
76 	[LINUX_SIGUSR2] = "SIGUSR2",
77 	[LINUX_SIGPIPE] = "SIGPIPE",
78 	[LINUX_SIGALRM] = "SIGALRM",
79 	[LINUX_SIGTERM] = "SIGTERM",
80 	[LINUX_SIGSTKFLT] = "SIGSTKFLT",
81 	[LINUX_SIGCHLD] = "SIGCHLD",
82 	[LINUX_SIGCONT] = "SIGCONT",
83 	[LINUX_SIGSTOP] = "SIGSTOP",
84 	[LINUX_SIGTSTP] = "SIGTSTP",
85 	[LINUX_SIGTTIN] = "SIGTTIN",
86 	[LINUX_SIGTTOU] = "SIGTTOU",
87 	[LINUX_SIGURG] = "SIGURG",
88 	[LINUX_SIGXCPU] = "SIGXCPU",
89 	[LINUX_SIGXFSZ] = "SIGXFSZ",
90 	[LINUX_SIGVTALRM] = "SIGVTALRM",
91 	[LINUX_SIGPROF] = "SIGPROF",
92 	[LINUX_SIGWINCH] = "SIGWINCH",
93 	[LINUX_SIGIO] = "SIGIO",
94 	[LINUX_SIGPWR] = "SIGPWR",
95 	[LINUX_SIGSYS] = "SIGSYS",
96 
97 	[LINUX_SIGRTMIN] = "SIGCANCEL",
98 	[LINUX_SIGRTMIN + 1] = "SIGSETXID",
99 	[LINUX_SIGRTMIN + 2] = "SIGRT2",
100 	[LINUX_SIGRTMIN + 3] = "SIGRT3",
101 	[LINUX_SIGRTMIN + 4] = "SIGRT4",
102 	[LINUX_SIGRTMIN + 5] = "SIGRT5",
103 	[LINUX_SIGRTMIN + 6] = "SIGRT6",
104 	[LINUX_SIGRTMIN + 7] = "SIGRT7",
105 	[LINUX_SIGRTMIN + 8] = "SIGRT8",
106 	[LINUX_SIGRTMIN + 9] = "SIGRT9",
107 	[LINUX_SIGRTMIN + 10] = "SIGRT10",
108 	[LINUX_SIGRTMIN + 11] = "SIGRT11",
109 	[LINUX_SIGRTMIN + 12] = "SIGRT12",
110 	[LINUX_SIGRTMIN + 13] = "SIGRT13",
111 	[LINUX_SIGRTMIN + 14] = "SIGRT14",
112 	[LINUX_SIGRTMIN + 15] = "SIGRT15",
113 	[LINUX_SIGRTMIN + 16] = "SIGRT16",
114 	[LINUX_SIGRTMIN + 17] = "SIGRT17",
115 	[LINUX_SIGRTMIN + 18] = "SIGRT18",
116 	[LINUX_SIGRTMIN + 19] = "SIGRT19",
117 	[LINUX_SIGRTMIN + 20] = "SIGRT20",
118 	[LINUX_SIGRTMIN + 21] = "SIGRT21",
119 	[LINUX_SIGRTMIN + 22] = "SIGRT22",
120 	[LINUX_SIGRTMIN + 23] = "SIGRT23",
121 	[LINUX_SIGRTMIN + 24] = "SIGRT24",
122 	[LINUX_SIGRTMIN + 25] = "SIGRT25",
123 	[LINUX_SIGRTMIN + 26] = "SIGRT26",
124 	[LINUX_SIGRTMIN + 27] = "SIGRT27",
125 	[LINUX_SIGRTMIN + 28] = "SIGRT28",
126 	[LINUX_SIGRTMIN + 29] = "SIGRT29",
127 	[LINUX_SIGRTMIN + 30] = "SIGRT30",
128 	[LINUX_SIGRTMIN + 31] = "SIGRT31",
129 	[LINUX_SIGRTMIN + 32] = "SIGRTMAX",
130 };
131 _Static_assert(nitems(linux_signames) == LINUX_SIGRTMAX + 1,
132     "invalid entries count in linux_signames");
133 
134 void
135 sysdecode_linux_clockid(FILE *fp, clockid_t which)
136 {
137 	const char *str;
138 	clockid_t ci;
139 	pid_t pid;
140 
141 	if (which >= 0) {
142 		str = lookup_value(clockids, which);
143 		if (str == NULL)
144 			fprintf(fp, "UNKNOWN(%d)", which);
145 		else
146 			fputs(str, fp);
147 		return;
148 	}
149 	if ((which & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD_MASK) {
150 		fputs("INVALID PERTHREAD|CLOCKFD", fp);
151 		goto pidp;
152 	}
153 	ci = LINUX_CPUCLOCK_WHICH(which);
154 	if (LINUX_CPUCLOCK_PERTHREAD(which) == true)
155 		fputs("THREAD|", fp);
156 	else
157 		fputs("PROCESS|", fp);
158 	str = lookup_value(clockcpuids, ci);
159 	if (str != NULL)
160 		fputs(str, fp);
161 	else {
162 		if (ci == LINUX_CLOCKFD)
163 			fputs("CLOCKFD", fp);
164 		else
165 			fprintf(fp, "UNKNOWN(%d)", which);
166 	}
167 
168 pidp:
169 	pid = LINUX_CPUCLOCK_ID(which);
170 	fprintf(fp, "(%d)", pid);
171 }
172 
173 const char *
174 sysdecode_linux_signal(int sig)
175 {
176 
177 	if ((unsigned)sig < nitems(linux_signames))
178 		return (linux_signames[sig]);
179 	return (NULL);
180 }
181 
182 const char *
183 sysdecode_linux_sigprocmask_how(int how)
184 {
185 
186 	return (lookup_value(sigprocmaskhow, how));
187 }
188 
189 bool
190 sysdecode_linux_clock_flags(FILE *fp, int flags, int *rem)
191 {
192 
193 	return (print_mask_int(fp, clockflags, flags, rem));
194 }
195 
196 bool
197 sysdecode_linux_atflags(FILE *fp, int flag, int *rem)
198 {
199 
200 	return (print_mask_int(fp, atflags, flag, rem));
201 }
202 
203 bool
204 sysdecode_linux_open_flags(FILE *fp, int flags, int *rem)
205 {
206 	bool printed;
207 	int mode;
208 	uintmax_t val;
209 
210 	mode = flags & LINUX_O_ACCMODE;
211 	flags &= ~LINUX_O_ACCMODE;
212 	switch (mode) {
213 	case LINUX_O_RDONLY:
214 		fputs("O_RDONLY", fp);
215 		printed = true;
216 		mode = 0;
217 		break;
218 	case LINUX_O_WRONLY:
219 		fputs("O_WRONLY", fp);
220 		printed = true;
221 		mode = 0;
222 		break;
223 	case LINUX_O_RDWR:
224 		fputs("O_RDWR", fp);
225 		printed = true;
226 		mode = 0;
227 		break;
228 	default:
229 		printed = false;
230 	}
231 	val = (unsigned)flags;
232 	print_mask_part(fp, openflags, &val, &printed);
233 	if (rem != NULL)
234 		*rem = val | mode;
235 	return (printed);
236 }
237 
238 bool
239 sysdecode_linux_clone_flags(FILE *fp, int flags, int *rem)
240 {
241 	uintmax_t val;
242 	bool printed;
243 	int sig;
244 
245 	sig = flags & LINUX_CSIGNAL;
246 	if (sig != 0)
247 		fprintf(fp, "(%s)", sysdecode_linux_signal(sig));
248 	val = (unsigned)flags & ~LINUX_CSIGNAL;
249 	print_mask_part(fp, cloneflags, &val, &printed);
250 	if (rem != NULL)
251 		*rem = val;
252 	return (printed);
253 }
254