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 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 19 | #include "utility.h" |
| 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> |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 24 | #include <sys/ptrace.h> |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame^] | 25 | #include <sys/uio.h> |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 26 | #include <sys/wait.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Christopher Ferris | 99235e9 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 29 | #include <string> |
| 30 | |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame^] | 31 | #include <android-base/logging.h> |
| 32 | #include <android-base/properties.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame^] | 34 | #include <android-base/strings.h> |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 35 | #include <android-base/unique_fd.h> |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 36 | #include <backtrace/Backtrace.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 37 | #include <log/log.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame^] | 39 | using android::base::unique_fd; |
| 40 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 41 | // Whitelist output desired in the logcat output. |
| 42 | bool is_allowed_in_logcat(enum logtype ltype) { |
Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 43 | if ((ltype == HEADER) |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 44 | || (ltype == REGISTERS) |
| 45 | || (ltype == BACKTRACE)) { |
| 46 | return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame^] | 51 | static bool should_write_to_kmsg() { |
| 52 | // Write to kmsg if tombstoned isn't up, and we're able to do so. |
| 53 | if (!android::base::GetBoolProperty("ro.debuggable", false)) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | if (android::base::GetProperty("init.svc.tombstoned", "") == "running") { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
Chenjie Luo | 97258aa | 2017-03-06 12:12:07 -0800 | [diff] [blame] | 64 | __attribute__((__weak__, visibility("default"))) |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 65 | void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 66 | bool write_to_tombstone = (log->tfd != -1); |
| 67 | bool write_to_logcat = is_allowed_in_logcat(ltype) |
Brigid Smith | c75a02f | 2014-07-17 14:52:33 -0700 | [diff] [blame] | 68 | && log->crashed_tid != -1 |
| 69 | && log->current_tid != -1 |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 70 | && (log->crashed_tid == log->current_tid); |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame^] | 71 | static bool write_to_kmsg = should_write_to_kmsg(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 73 | char buf[512]; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 74 | va_list ap; |
| 75 | va_start(ap, fmt); |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 76 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 77 | va_end(ap); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 79 | size_t len = strlen(buf); |
| 80 | if (len <= 0) { |
| 81 | return; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 84 | if (write_to_tombstone) { |
Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 85 | TEMP_FAILURE_RETRY(write(log->tfd, buf, len)); |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 88 | if (write_to_logcat) { |
Christopher Ferris | b0412a5 | 2015-05-05 12:23:06 -0700 | [diff] [blame] | 89 | __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf); |
Christopher Ferris | 99235e9 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 90 | if (log->amfd_data != nullptr) { |
| 91 | *log->amfd_data += buf; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 92 | } |
Josh Gao | bf2dd48 | 2017-03-28 13:07:15 -0700 | [diff] [blame^] | 93 | |
| 94 | if (write_to_kmsg) { |
| 95 | unique_fd kmsg_fd(open("/dev/kmsg_debug", O_WRONLY | O_APPEND | O_CLOEXEC)); |
| 96 | if (kmsg_fd.get() >= 0) { |
| 97 | // Our output might contain newlines which would otherwise be handled by the android logger. |
| 98 | // Split the lines up ourselves before sending to the kernel logger. |
| 99 | if (buf[len - 1] == '\n') { |
| 100 | buf[len - 1] = '\0'; |
| 101 | } |
| 102 | |
| 103 | std::vector<std::string> fragments = android::base::Split(buf, "\n"); |
| 104 | for (const std::string& fragment : fragments) { |
| 105 | static constexpr char prefix[] = "<3>DEBUG: "; |
| 106 | struct iovec iov[3]; |
| 107 | iov[0].iov_base = const_cast<char*>(prefix); |
| 108 | iov[0].iov_len = strlen(prefix); |
| 109 | iov[1].iov_base = const_cast<char*>(fragment.c_str()); |
| 110 | iov[1].iov_len = fragment.length(); |
| 111 | iov[2].iov_base = const_cast<char*>("\n"); |
| 112 | iov[2].iov_len = 1; |
| 113 | TEMP_FAILURE_RETRY(writev(kmsg_fd.get(), iov, 3)); |
| 114 | } |
| 115 | } |
| 116 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 117 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 120 | bool wait_for_signal(pid_t tid, siginfo_t* siginfo) { |
Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 121 | while (true) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 122 | int status; |
Josh Gao | f5a960a | 2016-08-10 17:57:01 -0700 | [diff] [blame] | 123 | pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL)); |
Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 124 | if (n == -1) { |
| 125 | ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno)); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 126 | return false; |
Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 127 | } else if (n == tid) { |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 128 | if (WIFSTOPPED(status)) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 129 | if (ptrace(PTRACE_GETSIGINFO, tid, nullptr, siginfo) != 0) { |
| 130 | ALOGE("PTRACE_GETSIGINFO failed: %s", strerror(errno)); |
| 131 | return false; |
| 132 | } |
| 133 | return true; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 134 | } else { |
Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 135 | ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status); |
Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 136 | // This is the only circumstance under which we can allow a detach |
| 137 | // to fail with ESRCH, which indicates the tid has exited. |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 138 | return false; |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 139 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 140 | } |
Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 141 | } |
Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 142 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 143 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 144 | #define MEMORY_BYTES_TO_DUMP 256 |
| 145 | #define MEMORY_BYTES_PER_LINE 16 |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 146 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 147 | void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* fmt, ...) { |
| 148 | std::string log_msg; |
| 149 | va_list ap; |
| 150 | va_start(ap, fmt); |
| 151 | android::base::StringAppendV(&log_msg, fmt, ap); |
| 152 | va_end(ap); |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 153 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 154 | // Align the address to sizeof(long) and start 32 bytes before the address. |
| 155 | addr &= ~(sizeof(long) - 1); |
| 156 | if (addr >= 4128) { |
| 157 | addr -= 32; |
| 158 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 159 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 160 | // Don't bother if the address looks too low, or looks too high. |
| 161 | if (addr < 4096 || |
| 162 | #if defined(__LP64__) |
| 163 | addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) { |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 164 | #else |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 165 | addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) { |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 166 | #endif |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 167 | return; |
| 168 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 169 | |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 170 | _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str()); |
| 171 | |
| 172 | // Dump 256 bytes |
| 173 | uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)]; |
| 174 | memset(data, 0, MEMORY_BYTES_TO_DUMP); |
| 175 | size_t bytes = backtrace->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data)); |
| 176 | if (bytes % sizeof(uintptr_t) != 0) { |
| 177 | // This should never happen, but just in case. |
| 178 | ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); |
| 179 | bytes &= ~(sizeof(uintptr_t) - 1); |
| 180 | } |
| 181 | |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 182 | uintptr_t start = 0; |
| 183 | bool skip_2nd_read = false; |
| 184 | if (bytes == 0) { |
| 185 | // In this case, we might want to try another read at the beginning of |
| 186 | // the next page only if it's within the amount of memory we would have |
| 187 | // read. |
| 188 | size_t page_size = sysconf(_SC_PAGE_SIZE); |
| 189 | start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr; |
| 190 | if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) { |
| 191 | skip_2nd_read = true; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) { |
| 196 | // Try to do one more read. This could happen if a read crosses a map, |
| 197 | // but the maps do not have any break between them. Or it could happen |
| 198 | // if reading from an unreadable map, but the read would cross back |
| 199 | // into a readable map. Only requires one extra read because a map has |
| 200 | // to contain at least one page, and the total number of bytes to dump |
| 201 | // is smaller than a page. |
| 202 | size_t bytes2 = backtrace->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes, |
| 203 | sizeof(data) - bytes - start); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 204 | bytes += bytes2; |
| 205 | if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) { |
| 206 | // This should never happen, but we'll try and continue any way. |
| 207 | ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); |
| 208 | bytes &= ~(sizeof(uintptr_t) - 1); |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 209 | } |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Dump the code around memory as: |
| 213 | // addr contents ascii |
| 214 | // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q |
| 215 | // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p...... |
| 216 | // On 32-bit machines, there are still 16 bytes per line but addresses and |
| 217 | // words are of course presented differently. |
| 218 | uintptr_t* data_ptr = data; |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 219 | size_t current = 0; |
| 220 | size_t total_bytes = start + bytes; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 221 | for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) { |
| 222 | std::string logline; |
| 223 | android::base::StringAppendF(&logline, " %" PRIPTR, addr); |
| 224 | |
| 225 | addr += MEMORY_BYTES_PER_LINE; |
| 226 | std::string ascii; |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 227 | for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) { |
| 228 | if (current >= start && current + sizeof(uintptr_t) <= total_bytes) { |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 229 | android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr); |
| 230 | |
| 231 | // Fill out the ascii string from the data. |
| 232 | uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr); |
| 233 | for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) { |
| 234 | if (*ptr >= 0x20 && *ptr < 0x7f) { |
| 235 | ascii += *ptr; |
| 236 | } else { |
| 237 | ascii += '.'; |
| 238 | } |
| 239 | } |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 240 | data_ptr++; |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 241 | } else { |
| 242 | logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-'); |
| 243 | ascii += std::string(sizeof(uintptr_t), '.'); |
| 244 | } |
Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 245 | current += sizeof(uintptr_t); |
Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 246 | } |
| 247 | _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str()); |
| 248 | } |
Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 249 | } |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 250 | |
| 251 | 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^] | 252 | unique_fd fd(open(path, O_RDONLY | O_CLOEXEC)); |
Josh Gao | 57f58f8 | 2017-03-15 23:23:22 -0700 | [diff] [blame] | 253 | if (fd != -1) { |
| 254 | int rc = TEMP_FAILURE_RETRY(read(fd.get(), buf, len - 1)); |
| 255 | if (rc != -1) { |
| 256 | buf[rc] = '\0'; |
| 257 | |
| 258 | // Trim trailing newlines. |
| 259 | if (rc > 0 && buf[rc - 1] == '\n') { |
| 260 | buf[rc - 1] = '\0'; |
| 261 | } |
| 262 | return; |
| 263 | } |
| 264 | } |
| 265 | strcpy(buf, default_value); |
| 266 | } |