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