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