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