| 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> | 
| Josh Gao | a50e8ff | 2016-07-14 16:43:12 -0700 | [diff] [blame] | 23 | #include <stdlib.h> | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 24 | #include <string.h> | 
|  | 25 | #include <unistd.h> | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 26 | #include <sys/ptrace.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 27 | #include <sys/wait.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 29 | #include <backtrace/Backtrace.h> | 
| Elliott Hughes | f5290ee | 2015-04-24 22:25:12 -0700 | [diff] [blame] | 30 | #include <base/file.h> | 
| Christopher Ferris | 0c3f1ae | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 31 | #include <base/stringprintf.h> | 
| Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 32 | #include <log/log.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 34 | const int SLEEP_TIME_USEC = 50000;         // 0.05 seconds | 
|  | 35 | const int MAX_TOTAL_SLEEP_USEC = 10000000; // 10 seconds | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 37 | // Whitelist output desired in the logcat output. | 
|  | 38 | bool is_allowed_in_logcat(enum logtype ltype) { | 
|  | 39 | if ((ltype == ERROR) | 
|  | 40 | || (ltype == HEADER) | 
|  | 41 | || (ltype == REGISTERS) | 
|  | 42 | || (ltype == BACKTRACE)) { | 
|  | 43 | return true; | 
|  | 44 | } | 
|  | 45 | return false; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 49 | bool write_to_tombstone = (log->tfd != -1); | 
|  | 50 | bool write_to_logcat = is_allowed_in_logcat(ltype) | 
| Brigid Smith | c75a02f | 2014-07-17 14:52:33 -0700 | [diff] [blame] | 51 | && log->crashed_tid != -1 | 
|  | 52 | && log->current_tid != -1 | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 53 | && (log->crashed_tid == log->current_tid); | 
|  | 54 | bool write_to_activitymanager = (log->amfd != -1); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 56 | char buf[512]; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 57 | va_list ap; | 
|  | 58 | va_start(ap, fmt); | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 59 | vsnprintf(buf, sizeof(buf), fmt, ap); | 
|  | 60 | va_end(ap); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 62 | size_t len = strlen(buf); | 
|  | 63 | if (len <= 0) { | 
|  | 64 | return; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 67 | if (write_to_tombstone) { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 68 | TEMP_FAILURE_RETRY(write(log->tfd, buf, len)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 71 | if (write_to_logcat) { | 
| Christopher Ferris | b0481f4 | 2015-05-05 12:23:06 -0700 | [diff] [blame] | 72 | __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf); | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 73 | if (write_to_activitymanager) { | 
| Elliott Hughes | f5290ee | 2015-04-24 22:25:12 -0700 | [diff] [blame] | 74 | if (!android::base::WriteFully(log->amfd, buf, len)) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 75 | // timeout or other failure on write; stop informing the activity manager | 
| Elliott Hughes | f5290ee | 2015-04-24 22:25:12 -0700 | [diff] [blame] | 76 | ALOGE("AM write failed: %s", strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 77 | log->amfd = -1; | 
|  | 78 | } | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 79 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 80 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 83 | int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed) { | 
|  | 84 | bool allow_dead_tid = false; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 85 | for (;;) { | 
|  | 86 | int status; | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 87 | pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG)); | 
|  | 88 | if (n == -1) { | 
|  | 89 | ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno)); | 
|  | 90 | break; | 
|  | 91 | } else if (n == tid) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 92 | if (WIFSTOPPED(status)) { | 
|  | 93 | return WSTOPSIG(status); | 
|  | 94 | } else { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 95 | ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status); | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 96 | // This is the only circumstance under which we can allow a detach | 
|  | 97 | // to fail with ESRCH, which indicates the tid has exited. | 
|  | 98 | allow_dead_tid = true; | 
|  | 99 | break; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 100 | } | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 101 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 102 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 103 | if (*total_sleep_time_usec > MAX_TOTAL_SLEEP_USEC) { | 
|  | 104 | ALOGE("timed out waiting for stop signal: tid=%d", tid); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 105 | break; | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 106 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 107 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 108 | usleep(SLEEP_TIME_USEC); | 
|  | 109 | *total_sleep_time_usec += SLEEP_TIME_USEC; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 110 | } | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 111 |  | 
|  | 112 | if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { | 
|  | 113 | if (allow_dead_tid && errno == ESRCH) { | 
|  | 114 | ALOGE("tid exited before attach completed: tid %d", tid); | 
|  | 115 | } else { | 
|  | 116 | *detach_failed = true; | 
|  | 117 | ALOGE("detach failed: tid %d, %s", tid, strerror(errno)); | 
|  | 118 | } | 
|  | 119 | } | 
|  | 120 | return -1; | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 121 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 122 |  | 
| Christopher Ferris | 0c3f1ae | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 123 | #define MEMORY_BYTES_TO_DUMP 256 | 
|  | 124 | #define MEMORY_BYTES_PER_LINE 16 | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 125 |  | 
| Christopher Ferris | 0c3f1ae | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 126 | void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* fmt, ...) { | 
|  | 127 | std::string log_msg; | 
|  | 128 | va_list ap; | 
|  | 129 | va_start(ap, fmt); | 
|  | 130 | android::base::StringAppendV(&log_msg, fmt, ap); | 
|  | 131 | va_end(ap); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 132 |  | 
| Christopher Ferris | 0c3f1ae | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 133 | // Align the address to sizeof(long) and start 32 bytes before the address. | 
|  | 134 | addr &= ~(sizeof(long) - 1); | 
|  | 135 | if (addr >= 4128) { | 
|  | 136 | addr -= 32; | 
|  | 137 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 138 |  | 
| Christopher Ferris | 0c3f1ae | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 139 | // Don't bother if the address looks too low, or looks too high. | 
|  | 140 | if (addr < 4096 || | 
|  | 141 | #if defined(__LP64__) | 
|  | 142 | addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) { | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 143 | #else | 
| Christopher Ferris | 0c3f1ae | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 144 | addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) { | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 145 | #endif | 
| Christopher Ferris | 0c3f1ae | 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 | 0c3f1ae | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 149 | _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str()); | 
|  | 150 |  | 
|  | 151 | // Dump 256 bytes | 
|  | 152 | uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)]; | 
|  | 153 | memset(data, 0, MEMORY_BYTES_TO_DUMP); | 
|  | 154 | size_t bytes = backtrace->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data)); | 
|  | 155 | if (bytes % sizeof(uintptr_t) != 0) { | 
|  | 156 | // This should never happen, but just in case. | 
|  | 157 | ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); | 
|  | 158 | bytes &= ~(sizeof(uintptr_t) - 1); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | if (bytes < MEMORY_BYTES_TO_DUMP && bytes > 0) { | 
|  | 162 | // Try to do one more read. This could happen if a read crosses a map, but | 
|  | 163 | // the maps do not have any break between them. Only requires one extra | 
|  | 164 | // read because a map has to contain at least one page, and the total | 
|  | 165 | // number of bytes to dump is smaller than a page. | 
|  | 166 | size_t bytes2 = backtrace->Read(addr + bytes, reinterpret_cast<uint8_t*>(data) + bytes, | 
|  | 167 | sizeof(data) - bytes); | 
|  | 168 | bytes += bytes2; | 
|  | 169 | if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) { | 
|  | 170 | // This should never happen, but we'll try and continue any way. | 
|  | 171 | ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); | 
|  | 172 | bytes &= ~(sizeof(uintptr_t) - 1); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 173 | } | 
| Christopher Ferris | 0c3f1ae | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 174 | } | 
|  | 175 |  | 
|  | 176 | // Dump the code around memory as: | 
|  | 177 | //  addr             contents                           ascii | 
|  | 178 | //  0000000000008d34 ef000000e8bd0090 e1b00000512fff1e  ............../Q | 
|  | 179 | //  0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000  ......-..p...... | 
|  | 180 | // On 32-bit machines, there are still 16 bytes per line but addresses and | 
|  | 181 | // words are of course presented differently. | 
|  | 182 | uintptr_t* data_ptr = data; | 
|  | 183 | for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) { | 
|  | 184 | std::string logline; | 
|  | 185 | android::base::StringAppendF(&logline, "    %" PRIPTR, addr); | 
|  | 186 |  | 
|  | 187 | addr += MEMORY_BYTES_PER_LINE; | 
|  | 188 | std::string ascii; | 
|  | 189 | for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++, data_ptr++) { | 
|  | 190 | if (bytes >= sizeof(uintptr_t)) { | 
|  | 191 | bytes -= sizeof(uintptr_t); | 
|  | 192 | android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr); | 
|  | 193 |  | 
|  | 194 | // Fill out the ascii string from the data. | 
|  | 195 | uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr); | 
|  | 196 | for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) { | 
|  | 197 | if (*ptr >= 0x20 && *ptr < 0x7f) { | 
|  | 198 | ascii += *ptr; | 
|  | 199 | } else { | 
|  | 200 | ascii += '.'; | 
|  | 201 | } | 
|  | 202 | } | 
|  | 203 | } else { | 
|  | 204 | logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-'); | 
|  | 205 | ascii += std::string(sizeof(uintptr_t), '.'); | 
|  | 206 | } | 
|  | 207 | } | 
|  | 208 | _LOG(log, logtype::MEMORY, "%s  %s\n", logline.c_str(), ascii.c_str()); | 
|  | 209 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 210 | } | 
| Josh Gao | a50e8ff | 2016-07-14 16:43:12 -0700 | [diff] [blame] | 211 |  | 
|  | 212 | bool pid_contains_tid(pid_t pid, pid_t tid) { | 
|  | 213 | char task_path[PATH_MAX]; | 
|  | 214 | if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) { | 
|  | 215 | ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid); | 
|  | 216 | exit(1); | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | return access(task_path, F_OK) == 0; | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | // Attach to a thread, and verify that it's still a member of the given process | 
|  | 223 | bool ptrace_attach_thread(pid_t pid, pid_t tid) { | 
|  | 224 | if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) { | 
|  | 225 | return false; | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | // Make sure that the task we attached to is actually part of the pid we're dumping. | 
|  | 229 | if (!pid_contains_tid(pid, tid)) { | 
|  | 230 | if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { | 
|  | 231 | ALOGE("debuggerd: failed to detach from thread '%d'", tid); | 
|  | 232 | exit(1); | 
|  | 233 | } | 
|  | 234 | return false; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | return true; | 
|  | 238 | } |