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 | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 31 | #include <set> |
Christopher Ferris | 99235e9 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 32 | #include <string> |
| 33 | |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 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 | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 38 | #include <async_safe/log.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> |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 42 | #include <unwindstack/AndroidUnwinder.h> |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 43 | #include <unwindstack/Memory.h> |
Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 44 | #include <unwindstack/Unwinder.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
Peter Collingbourne | 47d784e | 2021-11-05 18:40:52 -0700 | [diff] [blame] | 46 | using android::base::StringPrintf; |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 47 | using android::base::unique_fd; |
| 48 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 49 | bool is_allowed_in_logcat(enum logtype ltype) { |
Elliott Hughes | f9cd73f | 2023-03-17 00:38:26 +0000 | [diff] [blame] | 50 | return (ltype == HEADER) || (ltype == REGISTERS) || (ltype == BACKTRACE); |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 53 | static bool should_write_to_kmsg() { |
| 54 | // Write to kmsg if tombstoned isn't up, and we're able to do so. |
| 55 | if (!android::base::GetBoolProperty("ro.debuggable", false)) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | if (android::base::GetProperty("init.svc.tombstoned", "") == "running") { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
Chenjie Luo | 97258aa | 2017-03-06 12:12:07 -0800 | [diff] [blame] | 66 | __attribute__((__weak__, visibility("default"))) |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 67 | void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) { |
Mitch Phillips | aadebd8 | 2019-11-18 15:17:18 -0800 | [diff] [blame] | 68 | va_list ap; |
| 69 | va_start(ap, fmt); |
| 70 | _VLOG(log, ltype, fmt, ap); |
| 71 | va_end(ap); |
| 72 | } |
| 73 | |
| 74 | __attribute__((__weak__, visibility("default"))) |
| 75 | 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] | 76 | bool write_to_tombstone = (log->tfd != -1); |
| 77 | bool write_to_logcat = is_allowed_in_logcat(ltype) |
Brigid Smith | c75a02f | 2014-07-17 14:52:33 -0700 | [diff] [blame] | 78 | && log->crashed_tid != -1 |
| 79 | && log->current_tid != -1 |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 80 | && (log->crashed_tid == log->current_tid); |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 81 | static bool write_to_kmsg = should_write_to_kmsg(); |
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 | std::string msg; |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 84 | android::base::StringAppendV(&msg, fmt, ap); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 86 | if (msg.empty()) return; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 87 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 88 | if (write_to_tombstone) { |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 89 | TEMP_FAILURE_RETRY(write(log->tfd, msg.c_str(), msg.size())); |
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) { |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 93 | __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] | 94 | if (log->amfd_data != nullptr) { |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 95 | *log->amfd_data += msg; |
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. |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 103 | if (msg.back() == '\n') { |
| 104 | msg.back() = '\0'; |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 107 | std::vector<std::string> fragments = android::base::Split(msg, "\n"); |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame] | 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 |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 126 | static_assert(MEMORY_BYTES_PER_LINE == kTagGranuleSize); |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 127 | |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 128 | ssize_t dump_memory(void* out, size_t len, uint8_t* tags, size_t tags_len, uint64_t* addr, |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 129 | unwindstack::Memory* memory) { |
Peter Collingbourne | fe8997a | 2020-07-20 15:08:52 -0700 | [diff] [blame] | 130 | // Align the address to the number of bytes per line to avoid confusing memory tag output if |
| 131 | // memory is tagged and we start from a misaligned address. Start 32 bytes before the address. |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 132 | *addr &= ~(MEMORY_BYTES_PER_LINE - 1); |
| 133 | if (*addr >= 4128) { |
| 134 | *addr -= 32; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 135 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 136 | |
Peter Collingbourne | b1fcedb | 2020-07-21 16:29:02 -0700 | [diff] [blame] | 137 | // We don't want the address tag to appear in the addresses in the memory dump. |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 138 | *addr = untag_address(*addr); |
Peter Collingbourne | 10e428d | 2020-07-17 14:49:31 -0700 | [diff] [blame] | 139 | |
Peter Collingbourne | b1fcedb | 2020-07-21 16:29:02 -0700 | [diff] [blame] | 140 | // Don't bother if the address would overflow, taking tag bits into account. Note that |
| 141 | // untag_address truncates to 32 bits on 32-bit platforms as a side effect of returning a |
| 142 | // uintptr_t, so this also checks for 32-bit overflow. |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 143 | if (untag_address(*addr + MEMORY_BYTES_TO_DUMP - 1) < *addr) { |
| 144 | return -1; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 145 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 146 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 147 | memset(out, 0, len); |
| 148 | |
| 149 | size_t bytes = memory->Read(*addr, reinterpret_cast<uint8_t*>(out), len); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 150 | if (bytes % sizeof(uintptr_t) != 0) { |
| 151 | // This should never happen, but just in case. |
| 152 | ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); |
| 153 | bytes &= ~(sizeof(uintptr_t) - 1); |
| 154 | } |
| 155 | |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 156 | bool skip_2nd_read = false; |
| 157 | if (bytes == 0) { |
| 158 | // In this case, we might want to try another read at the beginning of |
| 159 | // the next page only if it's within the amount of memory we would have |
| 160 | // read. |
| 161 | size_t page_size = sysconf(_SC_PAGE_SIZE); |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 162 | uint64_t next_page = (*addr + (page_size - 1)) & ~(page_size - 1); |
| 163 | if (next_page == *addr || next_page >= *addr + len) { |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 164 | skip_2nd_read = true; |
| 165 | } |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 166 | *addr = next_page; |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 169 | if (bytes < len && !skip_2nd_read) { |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 170 | // Try to do one more read. This could happen if a read crosses a map, |
| 171 | // but the maps do not have any break between them. Or it could happen |
| 172 | // if reading from an unreadable map, but the read would cross back |
| 173 | // into a readable map. Only requires one extra read because a map has |
| 174 | // to contain at least one page, and the total number of bytes to dump |
| 175 | // is smaller than a page. |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 176 | size_t bytes2 = memory->Read(*addr + bytes, static_cast<uint8_t*>(out) + bytes, len - bytes); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 177 | bytes += bytes2; |
| 178 | if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) { |
| 179 | // This should never happen, but we'll try and continue any way. |
| 180 | ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); |
| 181 | bytes &= ~(sizeof(uintptr_t) - 1); |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 182 | } |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Peter Collingbourne | b1fcedb | 2020-07-21 16:29:02 -0700 | [diff] [blame] | 185 | // If we were unable to read anything, it probably means that the register doesn't contain a |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 186 | // valid pointer. |
Peter Collingbourne | b1fcedb | 2020-07-21 16:29:02 -0700 | [diff] [blame] | 187 | if (bytes == 0) { |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 188 | return -1; |
| 189 | } |
| 190 | |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 191 | for (uint64_t tag_granule = 0; tag_granule < bytes / kTagGranuleSize; ++tag_granule) { |
| 192 | long tag = memory->ReadTag(*addr + kTagGranuleSize * tag_granule); |
| 193 | if (tag_granule < tags_len) { |
| 194 | tags[tag_granule] = tag >= 0 ? tag : 0; |
| 195 | } else { |
| 196 | ALOGE("Insufficient space for tags"); |
| 197 | } |
| 198 | } |
| 199 | |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 200 | return bytes; |
| 201 | } |
| 202 | |
| 203 | void dump_memory(log_t* log, unwindstack::Memory* memory, uint64_t addr, const std::string& label) { |
| 204 | // Dump 256 bytes |
| 205 | uintptr_t data[MEMORY_BYTES_TO_DUMP / sizeof(uintptr_t)]; |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 206 | uint8_t tags[MEMORY_BYTES_TO_DUMP / kTagGranuleSize]; |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 207 | |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 208 | ssize_t bytes = dump_memory(data, sizeof(data), tags, sizeof(tags), &addr, memory); |
Josh Gao | 76e1e30 | 2021-01-26 15:53:11 -0800 | [diff] [blame] | 209 | if (bytes == -1) { |
Peter Collingbourne | b1fcedb | 2020-07-21 16:29:02 -0700 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | |
| 213 | _LOG(log, logtype::MEMORY, "\n%s:\n", label.c_str()); |
| 214 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 215 | // Dump the code around memory as: |
| 216 | // addr contents ascii |
| 217 | // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q |
| 218 | // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p...... |
| 219 | // On 32-bit machines, there are still 16 bytes per line but addresses and |
| 220 | // words are of course presented differently. |
| 221 | uintptr_t* data_ptr = data; |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 222 | uint8_t* tags_ptr = tags; |
| 223 | for (size_t line = 0; line < static_cast<size_t>(bytes) / MEMORY_BYTES_PER_LINE; line++) { |
| 224 | uint64_t tagged_addr = addr | static_cast<uint64_t>(*tags_ptr++) << 56; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 225 | std::string logline; |
Peter Collingbourne | fe8997a | 2020-07-20 15:08:52 -0700 | [diff] [blame] | 226 | android::base::StringAppendF(&logline, " %" PRIPTR, tagged_addr); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 227 | |
| 228 | addr += MEMORY_BYTES_PER_LINE; |
| 229 | std::string ascii; |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 230 | for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) { |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 231 | android::base::StringAppendF(&logline, " %" PRIPTR, static_cast<uint64_t>(*data_ptr)); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 232 | |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 233 | // Fill out the ascii string from the data. |
| 234 | uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr); |
| 235 | for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) { |
| 236 | if (*ptr >= 0x20 && *ptr < 0x7f) { |
| 237 | ascii += *ptr; |
| 238 | } else { |
| 239 | ascii += '.'; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 240 | } |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 241 | } |
Peter Collingbourne | 1a1f7d7 | 2021-03-08 16:53:54 -0800 | [diff] [blame] | 242 | data_ptr++; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 243 | } |
| 244 | _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str()); |
| 245 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 246 | } |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 247 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 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) { |
Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 258 | async_safe_fatal("failed to drop capabilities: %s", strerror(errno)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { |
Josh Gao | 618cea3 | 2021-01-26 17:45:43 -0800 | [diff] [blame] | 262 | async_safe_fatal("failed to set PR_SET_NO_NEW_PRIVS: %s", strerror(errno)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 266 | bool signal_has_si_addr(const siginfo_t* si) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 267 | // Manually sent signals won't have si_addr. |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 268 | 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] | 269 | return false; |
| 270 | } |
| 271 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 272 | switch (si->si_signo) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 273 | case SIGBUS: |
| 274 | case SIGFPE: |
| 275 | case SIGILL: |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 276 | case SIGTRAP: |
| 277 | return true; |
Peter Collingbourne | 773acaa | 2021-11-10 16:29:23 -0800 | [diff] [blame] | 278 | case SIGSEGV: |
| 279 | return si->si_code != SEGV_MTEAERR; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 280 | default: |
| 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 285 | bool signal_has_sender(const siginfo_t* si, pid_t caller_pid) { |
| 286 | return SI_FROMUSER(si) && (si->si_pid != 0) && (si->si_pid != caller_pid); |
| 287 | } |
| 288 | |
| 289 | void get_signal_sender(char* buf, size_t n, const siginfo_t* si) { |
| 290 | snprintf(buf, n, " from pid %d, uid %d", si->si_pid, si->si_uid); |
| 291 | } |
| 292 | |
| 293 | const char* get_signame(const siginfo_t* si) { |
| 294 | switch (si->si_signo) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 295 | case SIGABRT: return "SIGABRT"; |
| 296 | case SIGBUS: return "SIGBUS"; |
| 297 | case SIGFPE: return "SIGFPE"; |
| 298 | case SIGILL: return "SIGILL"; |
| 299 | case SIGSEGV: return "SIGSEGV"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 300 | case SIGSTKFLT: return "SIGSTKFLT"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 301 | case SIGSTOP: return "SIGSTOP"; |
| 302 | case SIGSYS: return "SIGSYS"; |
| 303 | case SIGTRAP: return "SIGTRAP"; |
Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 304 | case BIONIC_SIGNAL_DEBUGGER: |
| 305 | return "<debuggerd signal>"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 306 | default: return "?"; |
| 307 | } |
| 308 | } |
| 309 | |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 310 | const char* get_sigcode(const siginfo_t* si) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 311 | // Try the signal-specific codes... |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 312 | switch (si->si_signo) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 313 | case SIGILL: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 314 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 315 | case ILL_ILLOPC: return "ILL_ILLOPC"; |
| 316 | case ILL_ILLOPN: return "ILL_ILLOPN"; |
| 317 | case ILL_ILLADR: return "ILL_ILLADR"; |
| 318 | case ILL_ILLTRP: return "ILL_ILLTRP"; |
| 319 | case ILL_PRVOPC: return "ILL_PRVOPC"; |
| 320 | case ILL_PRVREG: return "ILL_PRVREG"; |
| 321 | case ILL_COPROC: return "ILL_COPROC"; |
| 322 | case ILL_BADSTK: return "ILL_BADSTK"; |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 323 | case ILL_BADIADDR: |
| 324 | return "ILL_BADIADDR"; |
| 325 | case __ILL_BREAK: |
| 326 | return "ILL_BREAK"; |
| 327 | case __ILL_BNDMOD: |
| 328 | return "ILL_BNDMOD"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 329 | } |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 330 | static_assert(NSIGILL == __ILL_BNDMOD, "missing ILL_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 331 | break; |
| 332 | case SIGBUS: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 333 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 334 | case BUS_ADRALN: return "BUS_ADRALN"; |
| 335 | case BUS_ADRERR: return "BUS_ADRERR"; |
| 336 | case BUS_OBJERR: return "BUS_OBJERR"; |
| 337 | case BUS_MCEERR_AR: return "BUS_MCEERR_AR"; |
| 338 | case BUS_MCEERR_AO: return "BUS_MCEERR_AO"; |
| 339 | } |
| 340 | static_assert(NSIGBUS == BUS_MCEERR_AO, "missing BUS_* si_code"); |
| 341 | break; |
| 342 | case SIGFPE: |
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 FPE_INTDIV: return "FPE_INTDIV"; |
| 345 | case FPE_INTOVF: return "FPE_INTOVF"; |
| 346 | case FPE_FLTDIV: return "FPE_FLTDIV"; |
| 347 | case FPE_FLTOVF: return "FPE_FLTOVF"; |
| 348 | case FPE_FLTUND: return "FPE_FLTUND"; |
| 349 | case FPE_FLTRES: return "FPE_FLTRES"; |
| 350 | case FPE_FLTINV: return "FPE_FLTINV"; |
| 351 | case FPE_FLTSUB: return "FPE_FLTSUB"; |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 352 | case __FPE_DECOVF: |
| 353 | return "FPE_DECOVF"; |
| 354 | case __FPE_DECDIV: |
| 355 | return "FPE_DECDIV"; |
| 356 | case __FPE_DECERR: |
| 357 | return "FPE_DECERR"; |
| 358 | case __FPE_INVASC: |
| 359 | return "FPE_INVASC"; |
| 360 | case __FPE_INVDEC: |
| 361 | return "FPE_INVDEC"; |
| 362 | case FPE_FLTUNK: |
| 363 | return "FPE_FLTUNK"; |
| 364 | case FPE_CONDTRAP: |
| 365 | return "FPE_CONDTRAP"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 366 | } |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 367 | static_assert(NSIGFPE == FPE_CONDTRAP, "missing FPE_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 368 | break; |
| 369 | case SIGSEGV: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 370 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 371 | case SEGV_MAPERR: return "SEGV_MAPERR"; |
| 372 | case SEGV_ACCERR: return "SEGV_ACCERR"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 373 | case SEGV_BNDERR: return "SEGV_BNDERR"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 374 | case SEGV_PKUERR: return "SEGV_PKUERR"; |
Christopher Ferris | 432791e | 2018-06-27 15:06:01 -0700 | [diff] [blame] | 375 | case SEGV_ACCADI: |
| 376 | return "SEGV_ACCADI"; |
| 377 | case SEGV_ADIDERR: |
| 378 | return "SEGV_ADIDERR"; |
| 379 | case SEGV_ADIPERR: |
| 380 | return "SEGV_ADIPERR"; |
Peter Collingbourne | f862252 | 2020-04-07 14:07:32 -0700 | [diff] [blame] | 381 | case SEGV_MTEAERR: |
| 382 | return "SEGV_MTEAERR"; |
| 383 | case SEGV_MTESERR: |
| 384 | return "SEGV_MTESERR"; |
Christopher Ferris | 6aa7249 | 2023-10-31 14:01:09 -0700 | [diff] [blame] | 385 | case SEGV_CPERR: |
| 386 | return "SEGV_CPERR"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 387 | } |
Christopher Ferris | 6aa7249 | 2023-10-31 14:01:09 -0700 | [diff] [blame] | 388 | static_assert(NSIGSEGV == SEGV_CPERR, "missing SEGV_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 389 | break; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 390 | case SIGSYS: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 391 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 392 | case SYS_SECCOMP: return "SYS_SECCOMP"; |
Christopher Ferris | 95b026f | 2021-02-18 11:44:10 -0800 | [diff] [blame] | 393 | case SYS_USER_DISPATCH: |
| 394 | return "SYS_USER_DISPATCH"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 395 | } |
Christopher Ferris | 95b026f | 2021-02-18 11:44:10 -0800 | [diff] [blame] | 396 | static_assert(NSIGSYS == SYS_USER_DISPATCH, "missing SYS_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 397 | break; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 398 | case SIGTRAP: |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 399 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 400 | case TRAP_BRKPT: return "TRAP_BRKPT"; |
| 401 | case TRAP_TRACE: return "TRAP_TRACE"; |
| 402 | case TRAP_BRANCH: return "TRAP_BRANCH"; |
| 403 | case TRAP_HWBKPT: return "TRAP_HWBKPT"; |
Christopher Ferris | 461baeb | 2018-10-26 11:22:40 -0700 | [diff] [blame] | 404 | case TRAP_UNK: |
| 405 | return "TRAP_UNDIAGNOSED"; |
Christopher Ferris | c31ccb4 | 2021-07-01 01:43:21 +0000 | [diff] [blame] | 406 | case TRAP_PERF: |
| 407 | return "TRAP_PERF"; |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 408 | } |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 409 | if ((si->si_code & 0xff) == SIGTRAP) { |
| 410 | switch ((si->si_code >> 8) & 0xff) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 411 | case PTRACE_EVENT_FORK: |
| 412 | return "PTRACE_EVENT_FORK"; |
| 413 | case PTRACE_EVENT_VFORK: |
| 414 | return "PTRACE_EVENT_VFORK"; |
| 415 | case PTRACE_EVENT_CLONE: |
| 416 | return "PTRACE_EVENT_CLONE"; |
| 417 | case PTRACE_EVENT_EXEC: |
| 418 | return "PTRACE_EVENT_EXEC"; |
| 419 | case PTRACE_EVENT_VFORK_DONE: |
| 420 | return "PTRACE_EVENT_VFORK_DONE"; |
| 421 | case PTRACE_EVENT_EXIT: |
| 422 | return "PTRACE_EVENT_EXIT"; |
| 423 | case PTRACE_EVENT_SECCOMP: |
| 424 | return "PTRACE_EVENT_SECCOMP"; |
| 425 | case PTRACE_EVENT_STOP: |
| 426 | return "PTRACE_EVENT_STOP"; |
| 427 | } |
| 428 | } |
Christopher Ferris | c31ccb4 | 2021-07-01 01:43:21 +0000 | [diff] [blame] | 429 | static_assert(NSIGTRAP == TRAP_PERF, "missing TRAP_* si_code"); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | // Then the other codes... |
Elliott Hughes | 70d8f28 | 2018-04-25 17:00:14 -0700 | [diff] [blame] | 433 | switch (si->si_code) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 434 | case SI_USER: return "SI_USER"; |
| 435 | case SI_KERNEL: return "SI_KERNEL"; |
| 436 | case SI_QUEUE: return "SI_QUEUE"; |
| 437 | case SI_TIMER: return "SI_TIMER"; |
| 438 | case SI_MESGQ: return "SI_MESGQ"; |
| 439 | case SI_ASYNCIO: return "SI_ASYNCIO"; |
| 440 | case SI_SIGIO: return "SI_SIGIO"; |
| 441 | case SI_TKILL: return "SI_TKILL"; |
| 442 | case SI_DETHREAD: return "SI_DETHREAD"; |
| 443 | } |
| 444 | // Then give up... |
| 445 | return "?"; |
| 446 | } |
Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 447 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 448 | void log_backtrace(log_t* log, unwindstack::AndroidUnwinder* unwinder, |
| 449 | unwindstack::AndroidUnwinderData& data, const char* prefix) { |
Christopher Ferris | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 450 | std::set<std::string> unreadable_elf_files; |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 451 | for (const auto& frame : data.frames) { |
Christopher Ferris | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 452 | if (frame.map_info != nullptr && frame.map_info->ElfFileNotReadable()) { |
| 453 | unreadable_elf_files.emplace(frame.map_info->name()); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Put the preamble ahead of the backtrace. |
| 458 | if (!unreadable_elf_files.empty()) { |
Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 459 | _LOG(log, logtype::BACKTRACE, |
| 460 | "%sNOTE: Function names and BuildId information is missing for some frames due\n", prefix); |
| 461 | _LOG(log, logtype::BACKTRACE, |
| 462 | "%sNOTE: to unreadable libraries. For unwinds of apps, only shared libraries\n", prefix); |
| 463 | _LOG(log, logtype::BACKTRACE, "%sNOTE: found under the lib/ directory are readable.\n", prefix); |
| 464 | #if defined(ROOT_POSSIBLE) |
| 465 | _LOG(log, logtype::BACKTRACE, |
| 466 | "%sNOTE: On this device, run setenforce 0 to make the libraries readable.\n", prefix); |
| 467 | #endif |
Christopher Ferris | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 468 | _LOG(log, logtype::BACKTRACE, "%sNOTE: Unreadable libraries:\n", prefix); |
| 469 | for (auto& name : unreadable_elf_files) { |
| 470 | _LOG(log, logtype::BACKTRACE, "%sNOTE: %s\n", prefix, name.c_str()); |
| 471 | } |
Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Christopher Ferris | 3b7b7ba | 2022-03-15 16:56:09 -0700 | [diff] [blame] | 474 | for (const auto& frame : data.frames) { |
Christopher Ferris | c95047d | 2022-03-14 15:02:11 -0700 | [diff] [blame] | 475 | _LOG(log, logtype::BACKTRACE, "%s%s\n", prefix, unwinder->FormatFrame(frame).c_str()); |
Christopher Ferris | 4ae266c | 2019-04-03 09:27:12 -0700 | [diff] [blame] | 476 | } |
| 477 | } |