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