blob: 7aebea8fed3d407da6d363b14efdeee18187cbdc [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
2 * Copyright 2008, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Brigid Smith62ba4892014-06-10 11:53:08 -070017#define LOG_TAG "DEBUG"
18
Josh Gaoc3706662017-08-29 13:08:32 -070019#include "libdebuggerd/utility.h"
Pavel Chupinc6c194c2013-11-21 23:17:20 +040020
Jeff Brown053b8652012-06-06 16:25:03 -070021#include <errno.h>
Jeff Brown053b8652012-06-06 16:25:03 -070022#include <signal.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040023#include <string.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070024#include <sys/capability.h>
25#include <sys/prctl.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070026#include <sys/ptrace.h>
Josh Gaobf2dd482017-03-28 13:07:15 -070027#include <sys/uio.h>
Jeff Brown053b8652012-06-06 16:25:03 -070028#include <sys/wait.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070029#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Christopher Ferris99235e92016-05-03 16:32:13 -070031#include <string>
32
Josh Gaobf2dd482017-03-28 13:07:15 -070033#include <android-base/logging.h>
34#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/stringprintf.h>
Josh Gaobf2dd482017-03-28 13:07:15 -070036#include <android-base/strings.h>
Josh Gao57f58f82017-03-15 23:23:22 -070037#include <android-base/unique_fd.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070038#include <debuggerd/handler.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070039#include <log/log.h>
Josh Gao2b2ae0c2017-08-21 14:31:17 -070040#include <unwindstack/Memory.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Josh Gaobf2dd482017-03-28 13:07:15 -070042using android::base::unique_fd;
43
Brigid Smith62ba4892014-06-10 11:53:08 -070044// Whitelist output desired in the logcat output.
45bool is_allowed_in_logcat(enum logtype ltype) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070046 if ((ltype == HEADER)
Brigid Smith62ba4892014-06-10 11:53:08 -070047 || (ltype == REGISTERS)
48 || (ltype == BACKTRACE)) {
49 return true;
50 }
51 return false;
52}
53
Josh Gaobf2dd482017-03-28 13:07:15 -070054static bool should_write_to_kmsg() {
55 // Write to kmsg if tombstoned isn't up, and we're able to do so.
56 if (!android::base::GetBoolProperty("ro.debuggable", false)) {
57 return false;
58 }
59
60 if (android::base::GetProperty("init.svc.tombstoned", "") == "running") {
61 return false;
62 }
63
64 return true;
65}
66
Chenjie Luo97258aa2017-03-06 12:12:07 -080067__attribute__((__weak__, visibility("default")))
Brigid Smith62ba4892014-06-10 11:53:08 -070068void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Brigid Smith50eb5462014-06-18 14:17:57 -070069 bool write_to_tombstone = (log->tfd != -1);
70 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smithc75a02f2014-07-17 14:52:33 -070071 && log->crashed_tid != -1
72 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070073 && (log->crashed_tid == log->current_tid);
Josh Gaobf2dd482017-03-28 13:07:15 -070074 static bool write_to_kmsg = should_write_to_kmsg();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Josh Gao1cc7bd82018-02-13 13:16:17 -080076 std::string msg;
Christopher Ferris20303f82014-01-10 16:33:16 -080077 va_list ap;
78 va_start(ap, fmt);
Josh Gao1cc7bd82018-02-13 13:16:17 -080079 android::base::StringAppendV(&msg, fmt, ap);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040080 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081
Josh Gao1cc7bd82018-02-13 13:16:17 -080082 if (msg.empty()) return;
Christopher Ferris20303f82014-01-10 16:33:16 -080083
Brigid Smith62ba4892014-06-10 11:53:08 -070084 if (write_to_tombstone) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080085 TEMP_FAILURE_RETRY(write(log->tfd, msg.c_str(), msg.size()));
Christopher Ferris20303f82014-01-10 16:33:16 -080086 }
87
Brigid Smith62ba4892014-06-10 11:53:08 -070088 if (write_to_logcat) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080089 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, msg.c_str());
Christopher Ferris99235e92016-05-03 16:32:13 -070090 if (log->amfd_data != nullptr) {
Josh Gao1cc7bd82018-02-13 13:16:17 -080091 *log->amfd_data += msg;
Christopher Tateded2e5a2013-03-19 13:12:23 -070092 }
Josh Gaobf2dd482017-03-28 13:07:15 -070093
94 if (write_to_kmsg) {
95 unique_fd kmsg_fd(open("/dev/kmsg_debug", O_WRONLY | O_APPEND | O_CLOEXEC));
96 if (kmsg_fd.get() >= 0) {
97 // Our output might contain newlines which would otherwise be handled by the android logger.
98 // Split the lines up ourselves before sending to the kernel logger.
Josh Gao1cc7bd82018-02-13 13:16:17 -080099 if (msg.back() == '\n') {
100 msg.back() = '\0';
Josh Gaobf2dd482017-03-28 13:07:15 -0700101 }
102
Josh Gao1cc7bd82018-02-13 13:16:17 -0800103 std::vector<std::string> fragments = android::base::Split(msg, "\n");
Josh Gaobf2dd482017-03-28 13:07:15 -0700104 for (const std::string& fragment : fragments) {
105 static constexpr char prefix[] = "<3>DEBUG: ";
106 struct iovec iov[3];
107 iov[0].iov_base = const_cast<char*>(prefix);
108 iov[0].iov_len = strlen(prefix);
109 iov[1].iov_base = const_cast<char*>(fragment.c_str());
110 iov[1].iov_len = fragment.length();
111 iov[2].iov_base = const_cast<char*>("\n");
112 iov[2].iov_len = 1;
113 TEMP_FAILURE_RETRY(writev(kmsg_fd.get(), iov, 3));
114 }
115 }
116 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800117 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118}
119
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700120#define MEMORY_BYTES_TO_DUMP 256
121#define MEMORY_BYTES_PER_LINE 16
Kévin PETIT4bb47722013-12-18 16:44:24 +0000122
Elliott Hughese1415a52018-02-15 09:18:21 -0800123void dump_memory(log_t* log, unwindstack::Memory* memory, uint64_t addr, const std::string& label) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700124 // Align the address to sizeof(long) and start 32 bytes before the address.
125 addr &= ~(sizeof(long) - 1);
126 if (addr >= 4128) {
127 addr -= 32;
128 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000129
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700130 // Don't bother if the address looks too low, or looks too high.
131 if (addr < 4096 ||
132#if defined(__LP64__)
133 addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000134#else
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700135 addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000136#endif
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700137 return;
138 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000139
Elliott Hughese1415a52018-02-15 09:18:21 -0800140 _LOG(log, logtype::MEMORY, "\n%s:\n", label.c_str());
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700141
142 // Dump 256 bytes
143 uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)];
144 memset(data, 0, MEMORY_BYTES_TO_DUMP);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700145 size_t bytes = memory->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700146 if (bytes % sizeof(uintptr_t) != 0) {
147 // This should never happen, but just in case.
148 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
149 bytes &= ~(sizeof(uintptr_t) - 1);
150 }
151
Christopher Ferris7937a362018-01-18 11:15:49 -0800152 uint64_t start = 0;
Christopher Ferris456abba2015-07-09 15:35:47 -0700153 bool skip_2nd_read = false;
154 if (bytes == 0) {
155 // In this case, we might want to try another read at the beginning of
156 // the next page only if it's within the amount of memory we would have
157 // read.
158 size_t page_size = sysconf(_SC_PAGE_SIZE);
159 start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr;
160 if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) {
161 skip_2nd_read = true;
162 }
163 }
164
165 if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) {
166 // Try to do one more read. This could happen if a read crosses a map,
167 // but the maps do not have any break between them. Or it could happen
168 // if reading from an unreadable map, but the read would cross back
169 // into a readable map. Only requires one extra read because a map has
170 // to contain at least one page, and the total number of bytes to dump
171 // is smaller than a page.
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700172 size_t bytes2 = memory->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes,
173 sizeof(data) - bytes - start);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700174 bytes += bytes2;
175 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
176 // This should never happen, but we'll try and continue any way.
177 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
178 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000179 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700180 }
181
182 // Dump the code around memory as:
183 // addr contents ascii
184 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
185 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
186 // On 32-bit machines, there are still 16 bytes per line but addresses and
187 // words are of course presented differently.
188 uintptr_t* data_ptr = data;
Christopher Ferris456abba2015-07-09 15:35:47 -0700189 size_t current = 0;
190 size_t total_bytes = start + bytes;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700191 for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) {
192 std::string logline;
193 android::base::StringAppendF(&logline, " %" PRIPTR, addr);
194
195 addr += MEMORY_BYTES_PER_LINE;
196 std::string ascii;
Christopher Ferris456abba2015-07-09 15:35:47 -0700197 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) {
198 if (current >= start && current + sizeof(uintptr_t) <= total_bytes) {
Christopher Ferris7937a362018-01-18 11:15:49 -0800199 android::base::StringAppendF(&logline, " %" PRIPTR, static_cast<uint64_t>(*data_ptr));
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700200
201 // Fill out the ascii string from the data.
202 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
203 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
204 if (*ptr >= 0x20 && *ptr < 0x7f) {
205 ascii += *ptr;
206 } else {
207 ascii += '.';
208 }
209 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700210 data_ptr++;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700211 } else {
212 logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-');
213 ascii += std::string(sizeof(uintptr_t), '.');
214 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700215 current += sizeof(uintptr_t);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700216 }
217 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
218 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000219}
Josh Gao57f58f82017-03-15 23:23:22 -0700220
221void read_with_default(const char* path, char* buf, size_t len, const char* default_value) {
Josh Gaobf2dd482017-03-28 13:07:15 -0700222 unique_fd fd(open(path, O_RDONLY | O_CLOEXEC));
Josh Gao57f58f82017-03-15 23:23:22 -0700223 if (fd != -1) {
224 int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1));
225 if (rc != -1) {
226 buf[rc] = '\0';
227
228 // Trim trailing newlines.
229 if (rc > 0 && buf[rc - 1] == '\n') {
230 buf[rc - 1] = '\0';
231 }
232 return;
233 }
234 }
235 strcpy(buf, default_value);
236}
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700237
238void drop_capabilities() {
239 __user_cap_header_struct capheader;
240 memset(&capheader, 0, sizeof(capheader));
241 capheader.version = _LINUX_CAPABILITY_VERSION_3;
242 capheader.pid = 0;
243
244 __user_cap_data_struct capdata[2];
245 memset(&capdata, 0, sizeof(capdata));
246
247 if (capset(&capheader, &capdata[0]) == -1) {
248 PLOG(FATAL) << "failed to drop capabilities";
249 }
250
251 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
252 PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS";
253 }
254}
255
Elliott Hughes70d8f282018-04-25 17:00:14 -0700256bool signal_has_si_addr(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700257 // Manually sent signals won't have si_addr.
Elliott Hughes70d8f282018-04-25 17:00:14 -0700258 if (si->si_code == SI_USER || si->si_code == SI_QUEUE || si->si_code == SI_TKILL) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700259 return false;
260 }
261
Elliott Hughes70d8f282018-04-25 17:00:14 -0700262 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700263 case SIGBUS:
264 case SIGFPE:
265 case SIGILL:
266 case SIGSEGV:
267 case SIGTRAP:
268 return true;
269 default:
270 return false;
271 }
272}
273
Elliott Hughes70d8f282018-04-25 17:00:14 -0700274bool signal_has_sender(const siginfo_t* si, pid_t caller_pid) {
275 return SI_FROMUSER(si) && (si->si_pid != 0) && (si->si_pid != caller_pid);
276}
277
278void get_signal_sender(char* buf, size_t n, const siginfo_t* si) {
279 snprintf(buf, n, " from pid %d, uid %d", si->si_pid, si->si_uid);
280}
281
282const char* get_signame(const siginfo_t* si) {
283 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700284 case SIGABRT: return "SIGABRT";
285 case SIGBUS: return "SIGBUS";
286 case SIGFPE: return "SIGFPE";
287 case SIGILL: return "SIGILL";
288 case SIGSEGV: return "SIGSEGV";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700289 case SIGSTKFLT: return "SIGSTKFLT";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700290 case SIGSTOP: return "SIGSTOP";
291 case SIGSYS: return "SIGSYS";
292 case SIGTRAP: return "SIGTRAP";
293 case DEBUGGER_SIGNAL: return "<debuggerd signal>";
294 default: return "?";
295 }
296}
297
Elliott Hughes70d8f282018-04-25 17:00:14 -0700298const char* get_sigcode(const siginfo_t* si) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700299 // Try the signal-specific codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700300 switch (si->si_signo) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700301 case SIGILL:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700302 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700303 case ILL_ILLOPC: return "ILL_ILLOPC";
304 case ILL_ILLOPN: return "ILL_ILLOPN";
305 case ILL_ILLADR: return "ILL_ILLADR";
306 case ILL_ILLTRP: return "ILL_ILLTRP";
307 case ILL_PRVOPC: return "ILL_PRVOPC";
308 case ILL_PRVREG: return "ILL_PRVREG";
309 case ILL_COPROC: return "ILL_COPROC";
310 case ILL_BADSTK: return "ILL_BADSTK";
Christopher Ferris432791e2018-06-27 15:06:01 -0700311 case ILL_BADIADDR:
312 return "ILL_BADIADDR";
313 case __ILL_BREAK:
314 return "ILL_BREAK";
315 case __ILL_BNDMOD:
316 return "ILL_BNDMOD";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700317 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700318 static_assert(NSIGILL == __ILL_BNDMOD, "missing ILL_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700319 break;
320 case SIGBUS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700321 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700322 case BUS_ADRALN: return "BUS_ADRALN";
323 case BUS_ADRERR: return "BUS_ADRERR";
324 case BUS_OBJERR: return "BUS_OBJERR";
325 case BUS_MCEERR_AR: return "BUS_MCEERR_AR";
326 case BUS_MCEERR_AO: return "BUS_MCEERR_AO";
327 }
328 static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code");
329 break;
330 case SIGFPE:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700331 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700332 case FPE_INTDIV: return "FPE_INTDIV";
333 case FPE_INTOVF: return "FPE_INTOVF";
334 case FPE_FLTDIV: return "FPE_FLTDIV";
335 case FPE_FLTOVF: return "FPE_FLTOVF";
336 case FPE_FLTUND: return "FPE_FLTUND";
337 case FPE_FLTRES: return "FPE_FLTRES";
338 case FPE_FLTINV: return "FPE_FLTINV";
339 case FPE_FLTSUB: return "FPE_FLTSUB";
Christopher Ferris432791e2018-06-27 15:06:01 -0700340 case __FPE_DECOVF:
341 return "FPE_DECOVF";
342 case __FPE_DECDIV:
343 return "FPE_DECDIV";
344 case __FPE_DECERR:
345 return "FPE_DECERR";
346 case __FPE_INVASC:
347 return "FPE_INVASC";
348 case __FPE_INVDEC:
349 return "FPE_INVDEC";
350 case FPE_FLTUNK:
351 return "FPE_FLTUNK";
352 case FPE_CONDTRAP:
353 return "FPE_CONDTRAP";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700354 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700355 static_assert(NSIGFPE == FPE_CONDTRAP, "missing FPE_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700356 break;
357 case SIGSEGV:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700358 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700359 case SEGV_MAPERR: return "SEGV_MAPERR";
360 case SEGV_ACCERR: return "SEGV_ACCERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700361 case SEGV_BNDERR: return "SEGV_BNDERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700362 case SEGV_PKUERR: return "SEGV_PKUERR";
Christopher Ferris432791e2018-06-27 15:06:01 -0700363 case SEGV_ACCADI:
364 return "SEGV_ACCADI";
365 case SEGV_ADIDERR:
366 return "SEGV_ADIDERR";
367 case SEGV_ADIPERR:
368 return "SEGV_ADIPERR";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700369 }
Christopher Ferris432791e2018-06-27 15:06:01 -0700370 static_assert(NSIGSEGV == SEGV_ADIPERR, "missing SEGV_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700371 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700372 case SIGSYS:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700373 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700374 case SYS_SECCOMP: return "SYS_SECCOMP";
375 }
376 static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code");
377 break;
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700378 case SIGTRAP:
Elliott Hughes70d8f282018-04-25 17:00:14 -0700379 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700380 case TRAP_BRKPT: return "TRAP_BRKPT";
381 case TRAP_TRACE: return "TRAP_TRACE";
382 case TRAP_BRANCH: return "TRAP_BRANCH";
383 case TRAP_HWBKPT: return "TRAP_HWBKPT";
Christopher Ferris461baeb2018-10-26 11:22:40 -0700384 case TRAP_UNK:
385 return "TRAP_UNDIAGNOSED";
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700386 }
Elliott Hughes70d8f282018-04-25 17:00:14 -0700387 if ((si->si_code & 0xff) == SIGTRAP) {
388 switch ((si->si_code >> 8) & 0xff) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700389 case PTRACE_EVENT_FORK:
390 return "PTRACE_EVENT_FORK";
391 case PTRACE_EVENT_VFORK:
392 return "PTRACE_EVENT_VFORK";
393 case PTRACE_EVENT_CLONE:
394 return "PTRACE_EVENT_CLONE";
395 case PTRACE_EVENT_EXEC:
396 return "PTRACE_EVENT_EXEC";
397 case PTRACE_EVENT_VFORK_DONE:
398 return "PTRACE_EVENT_VFORK_DONE";
399 case PTRACE_EVENT_EXIT:
400 return "PTRACE_EVENT_EXIT";
401 case PTRACE_EVENT_SECCOMP:
402 return "PTRACE_EVENT_SECCOMP";
403 case PTRACE_EVENT_STOP:
404 return "PTRACE_EVENT_STOP";
405 }
406 }
Christopher Ferris461baeb2018-10-26 11:22:40 -0700407 static_assert(NSIGTRAP == TRAP_UNK, "missing TRAP_* si_code");
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700408 break;
409 }
410 // Then the other codes...
Elliott Hughes70d8f282018-04-25 17:00:14 -0700411 switch (si->si_code) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700412 case SI_USER: return "SI_USER";
413 case SI_KERNEL: return "SI_KERNEL";
414 case SI_QUEUE: return "SI_QUEUE";
415 case SI_TIMER: return "SI_TIMER";
416 case SI_MESGQ: return "SI_MESGQ";
417 case SI_ASYNCIO: return "SI_ASYNCIO";
418 case SI_SIGIO: return "SI_SIGIO";
419 case SI_TKILL: return "SI_TKILL";
420 case SI_DETHREAD: return "SI_DETHREAD";
421 }
422 // Then give up...
423 return "?";
424}