| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 17 | #define LOG_TAG "DEBUG" | 
|  | 18 |  | 
| Josh Gao | c370666 | 2017-08-29 13:08:32 -0700 | [diff] [blame] | 19 | #include "libdebuggerd/utility.h" | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 20 |  | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 21 | #include <errno.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 22 | #include <signal.h> | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 23 | #include <string.h> | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 24 | #include <sys/capability.h> | 
|  | 25 | #include <sys/prctl.h> | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 26 | #include <sys/ptrace.h> | 
| Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 27 | #include <sys/uio.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 28 | #include <sys/wait.h> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 29 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 |  | 
| Christopher Ferris | 99235e9 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 31 | #include <string> | 
|  | 32 |  | 
| Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> | 
|  | 34 | #include <android-base/properties.h> | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> | 
| Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 36 | #include <android-base/strings.h> | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 37 | #include <android-base/unique_fd.h> | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 38 | #include <backtrace/Backtrace.h> | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 39 | #include <debuggerd/handler.h> | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 40 | #include <log/log.h> | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 41 | #include <unwindstack/Memory.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 |  | 
| Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 43 | using android::base::unique_fd; | 
|  | 44 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 45 | // Whitelist output desired in the logcat output. | 
|  | 46 | bool is_allowed_in_logcat(enum logtype ltype) { | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 47 | if ((ltype == HEADER) | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 48 | || (ltype == REGISTERS) | 
|  | 49 | || (ltype == BACKTRACE)) { | 
|  | 50 | return true; | 
|  | 51 | } | 
|  | 52 | return false; | 
|  | 53 | } | 
|  | 54 |  | 
| Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 55 | static bool should_write_to_kmsg() { | 
|  | 56 | // Write to kmsg if tombstoned isn't up, and we're able to do so. | 
|  | 57 | if (!android::base::GetBoolProperty("ro.debuggable", false)) { | 
|  | 58 | return false; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | if (android::base::GetProperty("init.svc.tombstoned", "") == "running") { | 
|  | 62 | return false; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | return true; | 
|  | 66 | } | 
|  | 67 |  | 
| Chenjie Luo | 97258aa | 2017-03-06 12:12:07 -0800 | [diff] [blame] | 68 | __attribute__((__weak__, visibility("default"))) | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 69 | void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 70 | bool write_to_tombstone = (log->tfd != -1); | 
|  | 71 | bool write_to_logcat = is_allowed_in_logcat(ltype) | 
| Brigid Smith | c75a02f | 2014-07-17 14:52:33 -0700 | [diff] [blame] | 72 | && log->crashed_tid != -1 | 
|  | 73 | && log->current_tid != -1 | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 74 | && (log->crashed_tid == log->current_tid); | 
| Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 75 | static bool write_to_kmsg = should_write_to_kmsg(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 77 | char buf[512]; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 78 | va_list ap; | 
|  | 79 | va_start(ap, fmt); | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 80 | vsnprintf(buf, sizeof(buf), fmt, ap); | 
|  | 81 | va_end(ap); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 83 | size_t len = strlen(buf); | 
|  | 84 | if (len <= 0) { | 
|  | 85 | return; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 88 | if (write_to_tombstone) { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 89 | TEMP_FAILURE_RETRY(write(log->tfd, buf, len)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 92 | if (write_to_logcat) { | 
| Christopher Ferris | b0412a5 | 2015-05-05 12:23:06 -0700 | [diff] [blame] | 93 | __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf); | 
| Christopher Ferris | 99235e9 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 94 | if (log->amfd_data != nullptr) { | 
|  | 95 | *log->amfd_data += buf; | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 96 | } | 
| Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 97 |  | 
|  | 98 | if (write_to_kmsg) { | 
|  | 99 | unique_fd kmsg_fd(open("/dev/kmsg_debug", O_WRONLY | O_APPEND | O_CLOEXEC)); | 
|  | 100 | if (kmsg_fd.get() >= 0) { | 
|  | 101 | // Our output might contain newlines which would otherwise be handled by the android logger. | 
|  | 102 | // Split the lines up ourselves before sending to the kernel logger. | 
|  | 103 | if (buf[len - 1] == '\n') { | 
|  | 104 | buf[len - 1] = '\0'; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | std::vector<std::string> fragments = android::base::Split(buf, "\n"); | 
|  | 108 | for (const std::string& fragment : fragments) { | 
|  | 109 | static constexpr char prefix[] = "<3>DEBUG: "; | 
|  | 110 | struct iovec iov[3]; | 
|  | 111 | iov[0].iov_base = const_cast<char*>(prefix); | 
|  | 112 | iov[0].iov_len = strlen(prefix); | 
|  | 113 | iov[1].iov_base = const_cast<char*>(fragment.c_str()); | 
|  | 114 | iov[1].iov_len = fragment.length(); | 
|  | 115 | iov[2].iov_base = const_cast<char*>("\n"); | 
|  | 116 | iov[2].iov_len = 1; | 
|  | 117 | TEMP_FAILURE_RETRY(writev(kmsg_fd.get(), iov, 3)); | 
|  | 118 | } | 
|  | 119 | } | 
|  | 120 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 121 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 124 | #define MEMORY_BYTES_TO_DUMP 256 | 
|  | 125 | #define MEMORY_BYTES_PER_LINE 16 | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 126 |  | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 127 | void dump_memory(log_t* log, unwindstack::Memory* memory, uintptr_t addr, const char* fmt, ...) { | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 128 | std::string log_msg; | 
|  | 129 | va_list ap; | 
|  | 130 | va_start(ap, fmt); | 
|  | 131 | android::base::StringAppendV(&log_msg, fmt, ap); | 
|  | 132 | va_end(ap); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 133 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 134 | // Align the address to sizeof(long) and start 32 bytes before the address. | 
|  | 135 | addr &= ~(sizeof(long) - 1); | 
|  | 136 | if (addr >= 4128) { | 
|  | 137 | addr -= 32; | 
|  | 138 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 139 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 140 | // Don't bother if the address looks too low, or looks too high. | 
|  | 141 | if (addr < 4096 || | 
|  | 142 | #if defined(__LP64__) | 
|  | 143 | addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) { | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 144 | #else | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 145 | addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) { | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 146 | #endif | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 147 | return; | 
|  | 148 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 149 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 150 | _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str()); | 
|  | 151 |  | 
|  | 152 | // Dump 256 bytes | 
|  | 153 | uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)]; | 
|  | 154 | memset(data, 0, MEMORY_BYTES_TO_DUMP); | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 155 | size_t bytes = memory->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data)); | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 156 | if (bytes % sizeof(uintptr_t) != 0) { | 
|  | 157 | // This should never happen, but just in case. | 
|  | 158 | ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); | 
|  | 159 | bytes &= ~(sizeof(uintptr_t) - 1); | 
|  | 160 | } | 
|  | 161 |  | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 162 | uintptr_t start = 0; | 
|  | 163 | bool skip_2nd_read = false; | 
|  | 164 | if (bytes == 0) { | 
|  | 165 | // In this case, we might want to try another read at the beginning of | 
|  | 166 | // the next page only if it's within the amount of memory we would have | 
|  | 167 | // read. | 
|  | 168 | size_t page_size = sysconf(_SC_PAGE_SIZE); | 
|  | 169 | start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr; | 
|  | 170 | if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) { | 
|  | 171 | skip_2nd_read = true; | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) { | 
|  | 176 | // Try to do one more read. This could happen if a read crosses a map, | 
|  | 177 | // but the maps do not have any break between them. Or it could happen | 
|  | 178 | // if reading from an unreadable map, but the read would cross back | 
|  | 179 | // into a readable map. Only requires one extra read because a map has | 
|  | 180 | // to contain at least one page, and the total number of bytes to dump | 
|  | 181 | // is smaller than a page. | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 182 | size_t bytes2 = memory->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes, | 
|  | 183 | sizeof(data) - bytes - start); | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 184 | bytes += bytes2; | 
|  | 185 | if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) { | 
|  | 186 | // This should never happen, but we'll try and continue any way. | 
|  | 187 | ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); | 
|  | 188 | bytes &= ~(sizeof(uintptr_t) - 1); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 189 | } | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
|  | 192 | // Dump the code around memory as: | 
|  | 193 | //  addr             contents                           ascii | 
|  | 194 | //  0000000000008d34 ef000000e8bd0090 e1b00000512fff1e  ............../Q | 
|  | 195 | //  0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000  ......-..p...... | 
|  | 196 | // On 32-bit machines, there are still 16 bytes per line but addresses and | 
|  | 197 | // words are of course presented differently. | 
|  | 198 | uintptr_t* data_ptr = data; | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 199 | size_t current = 0; | 
|  | 200 | size_t total_bytes = start + bytes; | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 201 | for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) { | 
|  | 202 | std::string logline; | 
|  | 203 | android::base::StringAppendF(&logline, "    %" PRIPTR, addr); | 
|  | 204 |  | 
|  | 205 | addr += MEMORY_BYTES_PER_LINE; | 
|  | 206 | std::string ascii; | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 207 | for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) { | 
|  | 208 | if (current >= start && current + sizeof(uintptr_t) <= total_bytes) { | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 209 | android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr); | 
|  | 210 |  | 
|  | 211 | // Fill out the ascii string from the data. | 
|  | 212 | uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr); | 
|  | 213 | for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) { | 
|  | 214 | if (*ptr >= 0x20 && *ptr < 0x7f) { | 
|  | 215 | ascii += *ptr; | 
|  | 216 | } else { | 
|  | 217 | ascii += '.'; | 
|  | 218 | } | 
|  | 219 | } | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 220 | data_ptr++; | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 221 | } else { | 
|  | 222 | logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-'); | 
|  | 223 | ascii += std::string(sizeof(uintptr_t), '.'); | 
|  | 224 | } | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 225 | current += sizeof(uintptr_t); | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 226 | } | 
|  | 227 | _LOG(log, logtype::MEMORY, "%s  %s\n", logline.c_str(), ascii.c_str()); | 
|  | 228 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 229 | } | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | void read_with_default(const char* path, char* buf, size_t len, const char* default_value) { | 
| Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 232 | unique_fd fd(open(path, O_RDONLY | O_CLOEXEC)); | 
| Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 233 | if (fd != -1) { | 
|  | 234 | int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1)); | 
|  | 235 | if (rc != -1) { | 
|  | 236 | buf[rc] = '\0'; | 
|  | 237 |  | 
|  | 238 | // Trim trailing newlines. | 
|  | 239 | if (rc > 0 && buf[rc - 1] == '\n') { | 
|  | 240 | buf[rc - 1] = '\0'; | 
|  | 241 | } | 
|  | 242 | return; | 
|  | 243 | } | 
|  | 244 | } | 
|  | 245 | strcpy(buf, default_value); | 
|  | 246 | } | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 247 |  | 
|  | 248 | void drop_capabilities() { | 
|  | 249 | __user_cap_header_struct capheader; | 
|  | 250 | memset(&capheader, 0, sizeof(capheader)); | 
|  | 251 | capheader.version = _LINUX_CAPABILITY_VERSION_3; | 
|  | 252 | capheader.pid = 0; | 
|  | 253 |  | 
|  | 254 | __user_cap_data_struct capdata[2]; | 
|  | 255 | memset(&capdata, 0, sizeof(capdata)); | 
|  | 256 |  | 
|  | 257 | if (capset(&capheader, &capdata[0]) == -1) { | 
|  | 258 | PLOG(FATAL) << "failed to drop capabilities"; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { | 
|  | 262 | PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS"; | 
|  | 263 | } | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | bool signal_has_si_addr(int si_signo, int si_code) { | 
|  | 267 | // Manually sent signals won't have si_addr. | 
|  | 268 | if (si_code == SI_USER || si_code == SI_QUEUE || si_code == SI_TKILL) { | 
|  | 269 | return false; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | switch (si_signo) { | 
|  | 273 | case SIGBUS: | 
|  | 274 | case SIGFPE: | 
|  | 275 | case SIGILL: | 
|  | 276 | case SIGSEGV: | 
|  | 277 | case SIGTRAP: | 
|  | 278 | return true; | 
|  | 279 | default: | 
|  | 280 | return false; | 
|  | 281 | } | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | const char* get_signame(int sig) { | 
|  | 285 | switch (sig) { | 
|  | 286 | case SIGABRT: return "SIGABRT"; | 
|  | 287 | case SIGBUS: return "SIGBUS"; | 
|  | 288 | case SIGFPE: return "SIGFPE"; | 
|  | 289 | case SIGILL: return "SIGILL"; | 
|  | 290 | case SIGSEGV: return "SIGSEGV"; | 
|  | 291 | #if defined(SIGSTKFLT) | 
|  | 292 | case SIGSTKFLT: return "SIGSTKFLT"; | 
|  | 293 | #endif | 
|  | 294 | case SIGSTOP: return "SIGSTOP"; | 
|  | 295 | case SIGSYS: return "SIGSYS"; | 
|  | 296 | case SIGTRAP: return "SIGTRAP"; | 
|  | 297 | case DEBUGGER_SIGNAL: return "<debuggerd signal>"; | 
|  | 298 | default: return "?"; | 
|  | 299 | } | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | const char* get_sigcode(int signo, int code) { | 
|  | 303 | // Try the signal-specific codes... | 
|  | 304 | switch (signo) { | 
|  | 305 | case SIGILL: | 
|  | 306 | switch (code) { | 
|  | 307 | case ILL_ILLOPC: return "ILL_ILLOPC"; | 
|  | 308 | case ILL_ILLOPN: return "ILL_ILLOPN"; | 
|  | 309 | case ILL_ILLADR: return "ILL_ILLADR"; | 
|  | 310 | case ILL_ILLTRP: return "ILL_ILLTRP"; | 
|  | 311 | case ILL_PRVOPC: return "ILL_PRVOPC"; | 
|  | 312 | case ILL_PRVREG: return "ILL_PRVREG"; | 
|  | 313 | case ILL_COPROC: return "ILL_COPROC"; | 
|  | 314 | case ILL_BADSTK: return "ILL_BADSTK"; | 
|  | 315 | } | 
|  | 316 | static_assert(NSIGILL == ILL_BADSTK, "missing ILL_* si_code"); | 
|  | 317 | break; | 
|  | 318 | case SIGBUS: | 
|  | 319 | switch (code) { | 
|  | 320 | case BUS_ADRALN: return "BUS_ADRALN"; | 
|  | 321 | case BUS_ADRERR: return "BUS_ADRERR"; | 
|  | 322 | case BUS_OBJERR: return "BUS_OBJERR"; | 
|  | 323 | case BUS_MCEERR_AR: return "BUS_MCEERR_AR"; | 
|  | 324 | case BUS_MCEERR_AO: return "BUS_MCEERR_AO"; | 
|  | 325 | } | 
|  | 326 | static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code"); | 
|  | 327 | break; | 
|  | 328 | case SIGFPE: | 
|  | 329 | switch (code) { | 
|  | 330 | case FPE_INTDIV: return "FPE_INTDIV"; | 
|  | 331 | case FPE_INTOVF: return "FPE_INTOVF"; | 
|  | 332 | case FPE_FLTDIV: return "FPE_FLTDIV"; | 
|  | 333 | case FPE_FLTOVF: return "FPE_FLTOVF"; | 
|  | 334 | case FPE_FLTUND: return "FPE_FLTUND"; | 
|  | 335 | case FPE_FLTRES: return "FPE_FLTRES"; | 
|  | 336 | case FPE_FLTINV: return "FPE_FLTINV"; | 
|  | 337 | case FPE_FLTSUB: return "FPE_FLTSUB"; | 
|  | 338 | } | 
|  | 339 | static_assert(NSIGFPE == FPE_FLTSUB, "missing FPE_* si_code"); | 
|  | 340 | break; | 
|  | 341 | case SIGSEGV: | 
|  | 342 | switch (code) { | 
|  | 343 | case SEGV_MAPERR: return "SEGV_MAPERR"; | 
|  | 344 | case SEGV_ACCERR: return "SEGV_ACCERR"; | 
|  | 345 | #if defined(SEGV_BNDERR) | 
|  | 346 | case SEGV_BNDERR: return "SEGV_BNDERR"; | 
|  | 347 | #endif | 
|  | 348 | #if defined(SEGV_PKUERR) | 
|  | 349 | case SEGV_PKUERR: return "SEGV_PKUERR"; | 
|  | 350 | #endif | 
|  | 351 | } | 
|  | 352 | #if defined(SEGV_PKUERR) | 
|  | 353 | static_assert(NSIGSEGV == SEGV_PKUERR, "missing SEGV_* si_code"); | 
|  | 354 | #elif defined(SEGV_BNDERR) | 
|  | 355 | static_assert(NSIGSEGV == SEGV_BNDERR, "missing SEGV_* si_code"); | 
|  | 356 | #else | 
|  | 357 | static_assert(NSIGSEGV == SEGV_ACCERR, "missing SEGV_* si_code"); | 
|  | 358 | #endif | 
|  | 359 | break; | 
|  | 360 | #if defined(SYS_SECCOMP) // Our glibc is too old, and we build this for the host too. | 
|  | 361 | case SIGSYS: | 
|  | 362 | switch (code) { | 
|  | 363 | case SYS_SECCOMP: return "SYS_SECCOMP"; | 
|  | 364 | } | 
|  | 365 | static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code"); | 
|  | 366 | break; | 
|  | 367 | #endif | 
|  | 368 | case SIGTRAP: | 
|  | 369 | switch (code) { | 
|  | 370 | case TRAP_BRKPT: return "TRAP_BRKPT"; | 
|  | 371 | case TRAP_TRACE: return "TRAP_TRACE"; | 
|  | 372 | case TRAP_BRANCH: return "TRAP_BRANCH"; | 
|  | 373 | case TRAP_HWBKPT: return "TRAP_HWBKPT"; | 
|  | 374 | } | 
|  | 375 | if ((code & 0xff) == SIGTRAP) { | 
|  | 376 | switch ((code >> 8) & 0xff) { | 
|  | 377 | case PTRACE_EVENT_FORK: | 
|  | 378 | return "PTRACE_EVENT_FORK"; | 
|  | 379 | case PTRACE_EVENT_VFORK: | 
|  | 380 | return "PTRACE_EVENT_VFORK"; | 
|  | 381 | case PTRACE_EVENT_CLONE: | 
|  | 382 | return "PTRACE_EVENT_CLONE"; | 
|  | 383 | case PTRACE_EVENT_EXEC: | 
|  | 384 | return "PTRACE_EVENT_EXEC"; | 
|  | 385 | case PTRACE_EVENT_VFORK_DONE: | 
|  | 386 | return "PTRACE_EVENT_VFORK_DONE"; | 
|  | 387 | case PTRACE_EVENT_EXIT: | 
|  | 388 | return "PTRACE_EVENT_EXIT"; | 
|  | 389 | case PTRACE_EVENT_SECCOMP: | 
|  | 390 | return "PTRACE_EVENT_SECCOMP"; | 
|  | 391 | case PTRACE_EVENT_STOP: | 
|  | 392 | return "PTRACE_EVENT_STOP"; | 
|  | 393 | } | 
|  | 394 | } | 
|  | 395 | static_assert(NSIGTRAP == TRAP_HWBKPT, "missing TRAP_* si_code"); | 
|  | 396 | break; | 
|  | 397 | } | 
|  | 398 | // Then the other codes... | 
|  | 399 | switch (code) { | 
|  | 400 | case SI_USER: return "SI_USER"; | 
|  | 401 | case SI_KERNEL: return "SI_KERNEL"; | 
|  | 402 | case SI_QUEUE: return "SI_QUEUE"; | 
|  | 403 | case SI_TIMER: return "SI_TIMER"; | 
|  | 404 | case SI_MESGQ: return "SI_MESGQ"; | 
|  | 405 | case SI_ASYNCIO: return "SI_ASYNCIO"; | 
|  | 406 | case SI_SIGIO: return "SI_SIGIO"; | 
|  | 407 | case SI_TKILL: return "SI_TKILL"; | 
|  | 408 | case SI_DETHREAD: return "SI_DETHREAD"; | 
|  | 409 | } | 
|  | 410 | // Then give up... | 
|  | 411 | return "?"; | 
|  | 412 | } |