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> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 38 | #include <debuggerd/handler.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 39 | #include <log/log.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 40 | #include <unwindstack/Memory.h> |
Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame^] | 41 | #include <unwindstack/Unwinder.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 | |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 77 | std::string msg; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 78 | va_list ap; |
| 79 | va_start(ap, fmt); |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 80 | android::base::StringAppendV(&msg, fmt, ap); |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 81 | va_end(ap); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 83 | if (msg.empty()) return; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 84 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 85 | if (write_to_tombstone) { |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 86 | TEMP_FAILURE_RETRY(write(log->tfd, msg.c_str(), msg.size())); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 89 | if (write_to_logcat) { |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 90 | __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, msg.c_str()); |
Christopher Ferris | 99235e9 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 91 | if (log->amfd_data != nullptr) { |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 92 | *log->amfd_data += msg; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 93 | } |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 94 | |
| 95 | if (write_to_kmsg) { |
| 96 | unique_fd kmsg_fd(open("/dev/kmsg_debug", O_WRONLY | O_APPEND | O_CLOEXEC)); |
| 97 | if (kmsg_fd.get() >= 0) { |
| 98 | // Our output might contain newlines which would otherwise be handled by the android logger. |
| 99 | // Split the lines up ourselves before sending to the kernel logger. |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 100 | if (msg.back() == '\n') { |
| 101 | msg.back() = '\0'; |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 104 | std::vector<std::string> fragments = android::base::Split(msg, "\n"); |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 105 | for (const std::string& fragment : fragments) { |
| 106 | static constexpr char prefix[] = "<3>DEBUG: "; |
| 107 | struct iovec iov[3]; |
| 108 | iov[0].iov_base = const_cast<char*>(prefix); |
| 109 | iov[0].iov_len = strlen(prefix); |
| 110 | iov[1].iov_base = const_cast<char*>(fragment.c_str()); |
| 111 | iov[1].iov_len = fragment.length(); |
| 112 | iov[2].iov_base = const_cast<char*>("\n"); |
| 113 | iov[2].iov_len = 1; |
| 114 | TEMP_FAILURE_RETRY(writev(kmsg_fd.get(), iov, 3)); |
| 115 | } |
| 116 | } |
| 117 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 118 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 121 | #define MEMORY_BYTES_TO_DUMP 256 |
| 122 | #define MEMORY_BYTES_PER_LINE 16 |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 123 | |
Elliott Hughes | e1415a5 | 2018-02-15 09:18:21 -0800 | [diff] [blame] | 124 | void dump_memory(log_t* log, unwindstack::Memory* memory, uint64_t addr, const std::string& label) { |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 125 | // Align the address to sizeof(long) and start 32 bytes before the address. |
| 126 | addr &= ~(sizeof(long) - 1); |
| 127 | if (addr >= 4128) { |
| 128 | addr -= 32; |
| 129 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 130 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 131 | // Don't bother if the address looks too low, or looks too high. |
| 132 | if (addr < 4096 || |
| 133 | #if defined(__LP64__) |
| 134 | addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) { |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 135 | #else |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 136 | addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) { |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 137 | #endif |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 138 | return; |
| 139 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 140 | |
Elliott Hughes | e1415a5 | 2018-02-15 09:18:21 -0800 | [diff] [blame] | 141 | _LOG(log, logtype::MEMORY, "\n%s:\n", label.c_str()); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 142 | |
| 143 | // Dump 256 bytes |
| 144 | uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)]; |
| 145 | memset(data, 0, MEMORY_BYTES_TO_DUMP); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 146 | 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] | 147 | if (bytes % sizeof(uintptr_t) != 0) { |
| 148 | // This should never happen, but just in case. |
| 149 | ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); |
| 150 | bytes &= ~(sizeof(uintptr_t) - 1); |
| 151 | } |
| 152 | |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 153 | uint64_t start = 0; |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 154 | bool skip_2nd_read = false; |
| 155 | if (bytes == 0) { |
| 156 | // In this case, we might want to try another read at the beginning of |
| 157 | // the next page only if it's within the amount of memory we would have |
| 158 | // read. |
| 159 | size_t page_size = sysconf(_SC_PAGE_SIZE); |
| 160 | start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr; |
| 161 | if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) { |
| 162 | skip_2nd_read = true; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) { |
| 167 | // Try to do one more read. This could happen if a read crosses a map, |
| 168 | // but the maps do not have any break between them. Or it could happen |
| 169 | // if reading from an unreadable map, but the read would cross back |
| 170 | // into a readable map. Only requires one extra read because a map has |
| 171 | // to contain at least one page, and the total number of bytes to dump |
| 172 | // is smaller than a page. |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 173 | size_t bytes2 = memory->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes, |
| 174 | sizeof(data) - bytes - start); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 175 | bytes += bytes2; |
| 176 | if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) { |
| 177 | // This should never happen, but we'll try and continue any way. |
| 178 | ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); |
| 179 | bytes &= ~(sizeof(uintptr_t) - 1); |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 180 | } |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Dump the code around memory as: |
| 184 | // addr contents ascii |
| 185 | // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q |
| 186 | // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p...... |
| 187 | // On 32-bit machines, there are still 16 bytes per line but addresses and |
| 188 | // words are of course presented differently. |
| 189 | uintptr_t* data_ptr = data; |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 190 | size_t current = 0; |
| 191 | size_t total_bytes = start + bytes; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 192 | for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) { |
| 193 | std::string logline; |
| 194 | android::base::StringAppendF(&logline, " %" PRIPTR, addr); |
| 195 | |
| 196 | addr += MEMORY_BYTES_PER_LINE; |
| 197 | std::string ascii; |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 198 | for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) { |
| 199 | if (current >= start && current + sizeof(uintptr_t) <= total_bytes) { |
Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 200 | android::base::StringAppendF(&logline, " %" PRIPTR, static_cast<uint64_t>(*data_ptr)); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 201 | |
| 202 | // Fill out the ascii string from the data. |
| 203 | uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr); |
| 204 | for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) { |
| 205 | if (*ptr >= 0x20 && *ptr < 0x7f) { |
| 206 | ascii += *ptr; |
| 207 | } else { |
| 208 | ascii += '.'; |
| 209 | } |
| 210 | } |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 211 | data_ptr++; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 212 | } else { |
| 213 | logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-'); |
| 214 | ascii += std::string(sizeof(uintptr_t), '.'); |
| 215 | } |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 216 | current += sizeof(uintptr_t); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 217 | } |
| 218 | _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str()); |
| 219 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 220 | } |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 221 | |
| 222 | 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] | 223 | unique_fd fd(open(path, O_RDONLY | O_CLOEXEC)); |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 224 | if (fd != -1) { |
| 225 | int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1)); |
| 226 | if (rc != -1) { |
| 227 | buf[rc] = '\0'; |
| 228 | |
| 229 | // Trim trailing newlines. |
| 230 | if (rc > 0 && buf[rc - 1] == '\n') { |
| 231 | buf[rc - 1] = '\0'; |
| 232 | } |
| 233 | return; |
| 234 | } |
| 235 | } |
| 236 | strcpy(buf, default_value); |
| 237 | } |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 238 | |
| 239 | void drop_capabilities() { |
| 240 | __user_cap_header_struct capheader; |
| 241 | memset(&capheader, 0, sizeof(capheader)); |
| 242 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 243 | capheader.pid = 0; |
| 244 | |
| 245 | __user_cap_data_struct capdata[2]; |
| 246 | memset(&capdata, 0, sizeof(capdata)); |
| 247 | |
| 248 | if (capset(&capheader, &capdata[0]) == -1) { |
| 249 | PLOG(FATAL) << "failed to drop capabilities"; |
| 250 | } |
| 251 | |
| 252 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { |
| 253 | PLOG(FATAL) << "failed to set PR_SET_NO_NEW_PRIVS"; |
| 254 | } |
| 255 | } |
| 256 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 257 | bool signal_has_si_addr(const siginfo_t* si) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 258 | // Manually sent signals won't have si_addr. |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 259 | if (si->si_code == SI_USER || si->si_code == SI_QUEUE || si->si_code == SI_TKILL) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 263 | switch (si->si_signo) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 264 | case SIGBUS: |
| 265 | case SIGFPE: |
| 266 | case SIGILL: |
| 267 | case SIGSEGV: |
| 268 | case SIGTRAP: |
| 269 | return true; |
| 270 | default: |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 275 | bool signal_has_sender(const siginfo_t* si, pid_t caller_pid) { |
| 276 | return SI_FROMUSER(si) && (si->si_pid != 0) && (si->si_pid != caller_pid); |
| 277 | } |
| 278 | |
| 279 | void get_signal_sender(char* buf, size_t n, const siginfo_t* si) { |
| 280 | snprintf(buf, n, " from pid %d, uid %d", si->si_pid, si->si_uid); |
| 281 | } |
| 282 | |
| 283 | const char* get_signame(const siginfo_t* si) { |
| 284 | switch (si->si_signo) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 285 | case SIGABRT: return "SIGABRT"; |
| 286 | case SIGBUS: return "SIGBUS"; |
| 287 | case SIGFPE: return "SIGFPE"; |
| 288 | case SIGILL: return "SIGILL"; |
| 289 | case SIGSEGV: return "SIGSEGV"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 290 | case SIGSTKFLT: return "SIGSTKFLT"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 291 | case SIGSTOP: return "SIGSTOP"; |
| 292 | case SIGSYS: return "SIGSYS"; |
| 293 | case SIGTRAP: return "SIGTRAP"; |
| 294 | case DEBUGGER_SIGNAL: return "<debuggerd signal>"; |
| 295 | default: return "?"; |
| 296 | } |
| 297 | } |
| 298 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 299 | const char* get_sigcode(const siginfo_t* si) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 300 | // Try the signal-specific codes... |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 301 | switch (si->si_signo) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 302 | case SIGILL: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 303 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 304 | case ILL_ILLOPC: return "ILL_ILLOPC"; |
| 305 | case ILL_ILLOPN: return "ILL_ILLOPN"; |
| 306 | case ILL_ILLADR: return "ILL_ILLADR"; |
| 307 | case ILL_ILLTRP: return "ILL_ILLTRP"; |
| 308 | case ILL_PRVOPC: return "ILL_PRVOPC"; |
| 309 | case ILL_PRVREG: return "ILL_PRVREG"; |
| 310 | case ILL_COPROC: return "ILL_COPROC"; |
| 311 | case ILL_BADSTK: return "ILL_BADSTK"; |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 312 | case ILL_BADIADDR: |
| 313 | return "ILL_BADIADDR"; |
| 314 | case __ILL_BREAK: |
| 315 | return "ILL_BREAK"; |
| 316 | case __ILL_BNDMOD: |
| 317 | return "ILL_BNDMOD"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 318 | } |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 319 | static_assert(NSIGILL == __ILL_BNDMOD, "missing ILL_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 320 | break; |
| 321 | case SIGBUS: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 322 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 323 | case BUS_ADRALN: return "BUS_ADRALN"; |
| 324 | case BUS_ADRERR: return "BUS_ADRERR"; |
| 325 | case BUS_OBJERR: return "BUS_OBJERR"; |
| 326 | case BUS_MCEERR_AR: return "BUS_MCEERR_AR"; |
| 327 | case BUS_MCEERR_AO: return "BUS_MCEERR_AO"; |
| 328 | } |
| 329 | static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code"); |
| 330 | break; |
| 331 | case SIGFPE: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 332 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 333 | case FPE_INTDIV: return "FPE_INTDIV"; |
| 334 | case FPE_INTOVF: return "FPE_INTOVF"; |
| 335 | case FPE_FLTDIV: return "FPE_FLTDIV"; |
| 336 | case FPE_FLTOVF: return "FPE_FLTOVF"; |
| 337 | case FPE_FLTUND: return "FPE_FLTUND"; |
| 338 | case FPE_FLTRES: return "FPE_FLTRES"; |
| 339 | case FPE_FLTINV: return "FPE_FLTINV"; |
| 340 | case FPE_FLTSUB: return "FPE_FLTSUB"; |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 341 | case __FPE_DECOVF: |
| 342 | return "FPE_DECOVF"; |
| 343 | case __FPE_DECDIV: |
| 344 | return "FPE_DECDIV"; |
| 345 | case __FPE_DECERR: |
| 346 | return "FPE_DECERR"; |
| 347 | case __FPE_INVASC: |
| 348 | return "FPE_INVASC"; |
| 349 | case __FPE_INVDEC: |
| 350 | return "FPE_INVDEC"; |
| 351 | case FPE_FLTUNK: |
| 352 | return "FPE_FLTUNK"; |
| 353 | case FPE_CONDTRAP: |
| 354 | return "FPE_CONDTRAP"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 355 | } |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 356 | static_assert(NSIGFPE == FPE_CONDTRAP, "missing FPE_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 357 | break; |
| 358 | case SIGSEGV: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 359 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 360 | case SEGV_MAPERR: return "SEGV_MAPERR"; |
| 361 | case SEGV_ACCERR: return "SEGV_ACCERR"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 362 | case SEGV_BNDERR: return "SEGV_BNDERR"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 363 | case SEGV_PKUERR: return "SEGV_PKUERR"; |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 364 | case SEGV_ACCADI: |
| 365 | return "SEGV_ACCADI"; |
| 366 | case SEGV_ADIDERR: |
| 367 | return "SEGV_ADIDERR"; |
| 368 | case SEGV_ADIPERR: |
| 369 | return "SEGV_ADIPERR"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 370 | } |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 371 | static_assert(NSIGSEGV == SEGV_ADIPERR, "missing SEGV_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 372 | break; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 373 | case SIGSYS: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 374 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 375 | case SYS_SECCOMP: return "SYS_SECCOMP"; |
| 376 | } |
| 377 | static_assert(NSIGSYS == SYS_SECCOMP, "missing SYS_* si_code"); |
| 378 | break; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 379 | case SIGTRAP: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 380 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 381 | case TRAP_BRKPT: return "TRAP_BRKPT"; |
| 382 | case TRAP_TRACE: return "TRAP_TRACE"; |
| 383 | case TRAP_BRANCH: return "TRAP_BRANCH"; |
| 384 | case TRAP_HWBKPT: return "TRAP_HWBKPT"; |
Christopher Ferris | 461baeb | 2018-10-26 11:22:40 -0700 | [diff] [blame] | 385 | case TRAP_UNK: |
| 386 | return "TRAP_UNDIAGNOSED"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 387 | } |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 388 | if ((si->si_code & 0xff) == SIGTRAP) { |
| 389 | switch ((si->si_code >> 8) & 0xff) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 390 | case PTRACE_EVENT_FORK: |
| 391 | return "PTRACE_EVENT_FORK"; |
| 392 | case PTRACE_EVENT_VFORK: |
| 393 | return "PTRACE_EVENT_VFORK"; |
| 394 | case PTRACE_EVENT_CLONE: |
| 395 | return "PTRACE_EVENT_CLONE"; |
| 396 | case PTRACE_EVENT_EXEC: |
| 397 | return "PTRACE_EVENT_EXEC"; |
| 398 | case PTRACE_EVENT_VFORK_DONE: |
| 399 | return "PTRACE_EVENT_VFORK_DONE"; |
| 400 | case PTRACE_EVENT_EXIT: |
| 401 | return "PTRACE_EVENT_EXIT"; |
| 402 | case PTRACE_EVENT_SECCOMP: |
| 403 | return "PTRACE_EVENT_SECCOMP"; |
| 404 | case PTRACE_EVENT_STOP: |
| 405 | return "PTRACE_EVENT_STOP"; |
| 406 | } |
| 407 | } |
Christopher Ferris | 461baeb | 2018-10-26 11:22:40 -0700 | [diff] [blame] | 408 | static_assert(NSIGTRAP == TRAP_UNK, "missing TRAP_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 409 | break; |
| 410 | } |
| 411 | // Then the other codes... |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 412 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 413 | case SI_USER: return "SI_USER"; |
| 414 | case SI_KERNEL: return "SI_KERNEL"; |
| 415 | case SI_QUEUE: return "SI_QUEUE"; |
| 416 | case SI_TIMER: return "SI_TIMER"; |
| 417 | case SI_MESGQ: return "SI_MESGQ"; |
| 418 | case SI_ASYNCIO: return "SI_ASYNCIO"; |
| 419 | case SI_SIGIO: return "SI_SIGIO"; |
| 420 | case SI_TKILL: return "SI_TKILL"; |
| 421 | case SI_DETHREAD: return "SI_DETHREAD"; |
| 422 | } |
| 423 | // Then give up... |
| 424 | return "?"; |
| 425 | } |
Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame^] | 426 | |
| 427 | void log_backtrace(log_t* log, unwindstack::Unwinder* unwinder, const char* prefix) { |
| 428 | if (unwinder->elf_from_memory_not_file()) { |
| 429 | _LOG(log, logtype::BACKTRACE, |
| 430 | "%sNOTE: Function names and BuildId information is missing for some frames due\n", prefix); |
| 431 | _LOG(log, logtype::BACKTRACE, |
| 432 | "%sNOTE: to unreadable libraries. For unwinds of apps, only shared libraries\n", prefix); |
| 433 | _LOG(log, logtype::BACKTRACE, "%sNOTE: found under the lib/ directory are readable.\n", prefix); |
| 434 | #if defined(ROOT_POSSIBLE) |
| 435 | _LOG(log, logtype::BACKTRACE, |
| 436 | "%sNOTE: On this device, run setenforce 0 to make the libraries readable.\n", prefix); |
| 437 | #endif |
| 438 | } |
| 439 | |
| 440 | unwinder->SetDisplayBuildID(true); |
| 441 | for (size_t i = 0; i < unwinder->NumFrames(); i++) { |
| 442 | _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, unwinder->FormatFrame(i).c_str()); |
| 443 | } |
| 444 | } |