| 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> | 
|  | 24 | #include <unistd.h> | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 25 | #include <sys/ptrace.h> | 
| Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 26 | #include <sys/wait.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 |  | 
| Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 28 | #include <string> | 
|  | 29 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 31 | #include <backtrace/Backtrace.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 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 34 | constexpr int SLEEP_TIME_USEC = 50000;          // 0.05 seconds | 
|  | 35 | constexpr 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) { | 
| Christopher Ferris | b36b592 | 2015-06-17 18:35:59 -0700 | [diff] [blame] | 39 | if ((ltype == HEADER) | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 40 | || (ltype == REGISTERS) | 
|  | 41 | || (ltype == BACKTRACE)) { | 
|  | 42 | return true; | 
|  | 43 | } | 
|  | 44 | return false; | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 48 | bool write_to_tombstone = (log->tfd != -1); | 
|  | 49 | bool write_to_logcat = is_allowed_in_logcat(ltype) | 
| Brigid Smith | c75a02f | 2014-07-17 14:52:33 -0700 | [diff] [blame] | 50 | && log->crashed_tid != -1 | 
|  | 51 | && log->current_tid != -1 | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 52 | && (log->crashed_tid == log->current_tid); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 54 | char buf[512]; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 55 | va_list ap; | 
|  | 56 | va_start(ap, fmt); | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 57 | vsnprintf(buf, sizeof(buf), fmt, ap); | 
|  | 58 | va_end(ap); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 60 | size_t len = strlen(buf); | 
|  | 61 | if (len <= 0) { | 
|  | 62 | return; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 65 | if (write_to_tombstone) { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 66 | TEMP_FAILURE_RETRY(write(log->tfd, buf, len)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 69 | if (write_to_logcat) { | 
| Christopher Ferris | b0412a5 | 2015-05-05 12:23:06 -0700 | [diff] [blame] | 70 | __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf); | 
| Christopher Ferris | 9818bd2 | 2016-05-03 16:32:13 -0700 | [diff] [blame] | 71 | if (log->amfd_data != nullptr) { | 
|  | 72 | *log->amfd_data += buf; | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 73 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 74 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 77 | int wait_for_signal(pid_t tid, int* total_sleep_time_usec) { | 
|  | 78 | while (true) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 79 | int status; | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 80 | pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG)); | 
|  | 81 | if (n == -1) { | 
|  | 82 | ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno)); | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 83 | return -1; | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 84 | } else if (n == tid) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 85 | if (WIFSTOPPED(status)) { | 
|  | 86 | return WSTOPSIG(status); | 
|  | 87 | } else { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 88 | ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status); | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 89 | // This is the only circumstance under which we can allow a detach | 
|  | 90 | // to fail with ESRCH, which indicates the tid has exited. | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 91 | return -1; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 92 | } | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 93 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 94 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 95 | if (*total_sleep_time_usec > MAX_TOTAL_SLEEP_USEC) { | 
|  | 96 | ALOGE("timed out waiting for stop signal: tid=%d", tid); | 
| Josh Gao | 7c89f9e | 2016-01-13 17:57:14 -0800 | [diff] [blame] | 97 | return -1; | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 98 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 99 |  | 
| Christopher Ferris | 1072f91 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 100 | usleep(SLEEP_TIME_USEC); | 
|  | 101 | *total_sleep_time_usec += SLEEP_TIME_USEC; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 102 | } | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 103 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 104 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 105 | #define MEMORY_BYTES_TO_DUMP 256 | 
|  | 106 | #define MEMORY_BYTES_PER_LINE 16 | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 107 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 108 | void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* fmt, ...) { | 
|  | 109 | std::string log_msg; | 
|  | 110 | va_list ap; | 
|  | 111 | va_start(ap, fmt); | 
|  | 112 | android::base::StringAppendV(&log_msg, fmt, ap); | 
|  | 113 | va_end(ap); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 114 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 115 | // Align the address to sizeof(long) and start 32 bytes before the address. | 
|  | 116 | addr &= ~(sizeof(long) - 1); | 
|  | 117 | if (addr >= 4128) { | 
|  | 118 | addr -= 32; | 
|  | 119 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 120 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 121 | // Don't bother if the address looks too low, or looks too high. | 
|  | 122 | if (addr < 4096 || | 
|  | 123 | #if defined(__LP64__) | 
|  | 124 | addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) { | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 125 | #else | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 126 | addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) { | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 127 | #endif | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 128 | return; | 
|  | 129 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 130 |  | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 131 | _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str()); | 
|  | 132 |  | 
|  | 133 | // Dump 256 bytes | 
|  | 134 | uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)]; | 
|  | 135 | memset(data, 0, MEMORY_BYTES_TO_DUMP); | 
|  | 136 | size_t bytes = backtrace->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data)); | 
|  | 137 | if (bytes % sizeof(uintptr_t) != 0) { | 
|  | 138 | // This should never happen, but just in case. | 
|  | 139 | ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); | 
|  | 140 | bytes &= ~(sizeof(uintptr_t) - 1); | 
|  | 141 | } | 
|  | 142 |  | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 143 | uintptr_t start = 0; | 
|  | 144 | bool skip_2nd_read = false; | 
|  | 145 | if (bytes == 0) { | 
|  | 146 | // In this case, we might want to try another read at the beginning of | 
|  | 147 | // the next page only if it's within the amount of memory we would have | 
|  | 148 | // read. | 
|  | 149 | size_t page_size = sysconf(_SC_PAGE_SIZE); | 
|  | 150 | start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr; | 
|  | 151 | if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) { | 
|  | 152 | skip_2nd_read = true; | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) { | 
|  | 157 | // Try to do one more read. This could happen if a read crosses a map, | 
|  | 158 | // but the maps do not have any break between them. Or it could happen | 
|  | 159 | // if reading from an unreadable map, but the read would cross back | 
|  | 160 | // into a readable map. Only requires one extra read because a map has | 
|  | 161 | // to contain at least one page, and the total number of bytes to dump | 
|  | 162 | // is smaller than a page. | 
|  | 163 | size_t bytes2 = backtrace->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes, | 
|  | 164 | sizeof(data) - bytes - start); | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 165 | bytes += bytes2; | 
|  | 166 | if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) { | 
|  | 167 | // This should never happen, but we'll try and continue any way. | 
|  | 168 | ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t)); | 
|  | 169 | bytes &= ~(sizeof(uintptr_t) - 1); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 170 | } | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
|  | 173 | // Dump the code around memory as: | 
|  | 174 | //  addr             contents                           ascii | 
|  | 175 | //  0000000000008d34 ef000000e8bd0090 e1b00000512fff1e  ............../Q | 
|  | 176 | //  0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000  ......-..p...... | 
|  | 177 | // On 32-bit machines, there are still 16 bytes per line but addresses and | 
|  | 178 | // words are of course presented differently. | 
|  | 179 | uintptr_t* data_ptr = data; | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 180 | size_t current = 0; | 
|  | 181 | size_t total_bytes = start + bytes; | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 182 | for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) { | 
|  | 183 | std::string logline; | 
|  | 184 | android::base::StringAppendF(&logline, "    %" PRIPTR, addr); | 
|  | 185 |  | 
|  | 186 | addr += MEMORY_BYTES_PER_LINE; | 
|  | 187 | std::string ascii; | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 188 | for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) { | 
|  | 189 | if (current >= start && current + sizeof(uintptr_t) <= total_bytes) { | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 190 | android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr); | 
|  | 191 |  | 
|  | 192 | // Fill out the ascii string from the data. | 
|  | 193 | uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr); | 
|  | 194 | for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) { | 
|  | 195 | if (*ptr >= 0x20 && *ptr < 0x7f) { | 
|  | 196 | ascii += *ptr; | 
|  | 197 | } else { | 
|  | 198 | ascii += '.'; | 
|  | 199 | } | 
|  | 200 | } | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 201 | data_ptr++; | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 202 | } else { | 
|  | 203 | logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-'); | 
|  | 204 | ascii += std::string(sizeof(uintptr_t), '.'); | 
|  | 205 | } | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 206 | current += sizeof(uintptr_t); | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 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 | } |