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