| 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 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 28 | #include <backtrace/Backtrace.h> | 
| Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 29 | #include <log/log.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 |  | 
| Christopher Ferris | 84ddb34 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 31 | const int SLEEP_TIME_USEC = 50000;         // 0.05 seconds | 
|  | 32 | const int MAX_TOTAL_SLEEP_USEC = 10000000; // 10 seconds | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 |  | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 34 | static int write_to_am(int fd, const char* buf, int len) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 35 | int to_write = len; | 
|  | 36 | while (to_write > 0) { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 37 | int written = TEMP_FAILURE_RETRY(write(fd, buf + len - to_write, to_write)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 38 | if (written < 0) { | 
|  | 39 | // hard failure | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 40 | ALOGE("AM write failure (%d / %s)\n", errno, strerror(errno)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 41 | return -1; | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 42 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 43 | to_write -= written; | 
|  | 44 | } | 
|  | 45 | return len; | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 48 | // Whitelist output desired in the logcat output. | 
|  | 49 | bool is_allowed_in_logcat(enum logtype ltype) { | 
|  | 50 | if ((ltype == ERROR) | 
|  | 51 | || (ltype == HEADER) | 
|  | 52 | || (ltype == REGISTERS) | 
|  | 53 | || (ltype == BACKTRACE)) { | 
|  | 54 | return true; | 
|  | 55 | } | 
|  | 56 | return false; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 60 | bool write_to_tombstone = (log->tfd != -1); | 
|  | 61 | bool write_to_logcat = is_allowed_in_logcat(ltype) | 
| Brigid Smith | 166cfe6 | 2014-07-17 14:52:33 -0700 | [diff] [blame] | 62 | && log->crashed_tid != -1 | 
|  | 63 | && log->current_tid != -1 | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 64 | && (log->crashed_tid == log->current_tid); | 
|  | 65 | bool write_to_activitymanager = (log->amfd != -1); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 |  | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 67 | char buf[512]; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 68 | va_list ap; | 
|  | 69 | va_start(ap, fmt); | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 70 | vsnprintf(buf, sizeof(buf), fmt, ap); | 
|  | 71 | va_end(ap); | 
| 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 | size_t len = strlen(buf); | 
|  | 74 | if (len <= 0) { | 
|  | 75 | return; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 78 | if (write_to_tombstone) { | 
| Pavel Chupin | c6c194c | 2013-11-21 23:17:20 +0400 | [diff] [blame] | 79 | TEMP_FAILURE_RETRY(write(log->tfd, buf, len)); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 82 | if (write_to_logcat) { | 
|  | 83 | __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_INFO, LOG_TAG, buf); | 
|  | 84 | if (write_to_activitymanager) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 85 | int written = write_to_am(log->amfd, buf, len); | 
|  | 86 | if (written <= 0) { | 
|  | 87 | // timeout or other failure on write; stop informing the activity manager | 
|  | 88 | log->amfd = -1; | 
|  | 89 | } | 
| Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 90 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 91 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
| Christopher Ferris | 84ddb34 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 94 | int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed) { | 
|  | 95 | bool allow_dead_tid = false; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 96 | for (;;) { | 
|  | 97 | int status; | 
| Christopher Ferris | 84ddb34 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 98 | pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG)); | 
|  | 99 | if (n == -1) { | 
|  | 100 | ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno)); | 
|  | 101 | break; | 
|  | 102 | } else if (n == tid) { | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 103 | if (WIFSTOPPED(status)) { | 
|  | 104 | return WSTOPSIG(status); | 
|  | 105 | } else { | 
| Brigid Smith | 50eb546 | 2014-06-18 14:17:57 -0700 | [diff] [blame] | 106 | ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status); | 
| Christopher Ferris | 84ddb34 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 107 | // This is the only circumstance under which we can allow a detach | 
|  | 108 | // to fail with ESRCH, which indicates the tid has exited. | 
|  | 109 | allow_dead_tid = true; | 
|  | 110 | break; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 111 | } | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 112 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 113 |  | 
| Christopher Ferris | 84ddb34 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 114 | if (*total_sleep_time_usec > MAX_TOTAL_SLEEP_USEC) { | 
|  | 115 | ALOGE("timed out waiting for stop signal: tid=%d", tid); | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 116 | break; | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 117 | } | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 118 |  | 
| Christopher Ferris | 84ddb34 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 119 | usleep(SLEEP_TIME_USEC); | 
|  | 120 | *total_sleep_time_usec += SLEEP_TIME_USEC; | 
| Christopher Ferris | 20303f8 | 2014-01-10 16:33:16 -0800 | [diff] [blame] | 121 | } | 
| Christopher Ferris | 84ddb34 | 2014-10-31 21:34:38 -0700 | [diff] [blame] | 122 |  | 
|  | 123 | if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) { | 
|  | 124 | if (allow_dead_tid && errno == ESRCH) { | 
|  | 125 | ALOGE("tid exited before attach completed: tid %d", tid); | 
|  | 126 | } else { | 
|  | 127 | *detach_failed = true; | 
|  | 128 | ALOGE("detach failed: tid %d, %s", tid, strerror(errno)); | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 | return -1; | 
| Jeff Brown | 13e715b | 2011-10-21 12:14:56 -0700 | [diff] [blame] | 132 | } | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 133 |  | 
|  | 134 | #if defined (__mips__) | 
|  | 135 | #define DUMP_MEMORY_AS_ASCII 1 | 
|  | 136 | #else | 
|  | 137 | #define DUMP_MEMORY_AS_ASCII 0 | 
|  | 138 | #endif | 
|  | 139 |  | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 140 | void dump_memory(log_t* log, pid_t tid, uintptr_t addr) { | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 141 | char code_buffer[64]; | 
|  | 142 | char ascii_buffer[32]; | 
|  | 143 | uintptr_t p, end; | 
|  | 144 |  | 
|  | 145 | p = addr & ~(sizeof(long) - 1); | 
|  | 146 | /* Dump 32 bytes before addr */ | 
|  | 147 | p -= 32; | 
|  | 148 | if (p > addr) { | 
|  | 149 | /* catch underflow */ | 
|  | 150 | p = 0; | 
|  | 151 | } | 
|  | 152 | /* Dump 256 bytes */ | 
|  | 153 | end = p + 256; | 
|  | 154 | /* catch overflow; 'end - p' has to be multiples of 16 */ | 
|  | 155 | while (end < p) { | 
|  | 156 | end -= 16; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | /* Dump the code around PC as: | 
|  | 160 | *  addr             contents                           ascii | 
|  | 161 | *  0000000000008d34 ef000000e8bd0090 e1b00000512fff1e  ............../Q | 
|  | 162 | *  0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000  ......-..p...... | 
|  | 163 | * On 32-bit machines, there are still 16 bytes per line but addresses and | 
|  | 164 | * words are of course presented differently. | 
|  | 165 | */ | 
|  | 166 | while (p < end) { | 
|  | 167 | char* asc_out = ascii_buffer; | 
|  | 168 |  | 
|  | 169 | int len = snprintf(code_buffer, sizeof(code_buffer), "%" PRIPTR " ", p); | 
|  | 170 |  | 
|  | 171 | for (size_t i = 0; i < 16/sizeof(long); i++) { | 
|  | 172 | long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL); | 
|  | 173 | if (data == -1 && errno != 0) { | 
|  | 174 | // ptrace failed, probably because we're dumping memory in an | 
|  | 175 | // unmapped or inaccessible page. | 
|  | 176 | #ifdef __LP64__ | 
|  | 177 | len += sprintf(code_buffer + len, "---------------- "); | 
|  | 178 | #else | 
|  | 179 | len += sprintf(code_buffer + len, "-------- "); | 
|  | 180 | #endif | 
|  | 181 | } else { | 
|  | 182 | len += sprintf(code_buffer + len, "%" PRIPTR " ", | 
|  | 183 | static_cast<uintptr_t>(data)); | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | #if DUMP_MEMORY_AS_ASCII | 
|  | 187 | for (size_t j = 0; j < sizeof(long); j++) { | 
|  | 188 | /* | 
|  | 189 | * Our isprint() allows high-ASCII characters that display | 
|  | 190 | * differently (often badly) in different viewers, so we | 
|  | 191 | * just use a simpler test. | 
|  | 192 | */ | 
|  | 193 | char val = (data >> (j*8)) & 0xff; | 
|  | 194 | if (val >= 0x20 && val < 0x7f) { | 
|  | 195 | *asc_out++ = val; | 
|  | 196 | } else { | 
|  | 197 | *asc_out++ = '.'; | 
|  | 198 | } | 
|  | 199 | } | 
|  | 200 | #endif | 
|  | 201 | p += sizeof(long); | 
|  | 202 | } | 
|  | 203 | *asc_out = '\0'; | 
| Brigid Smith | 62ba489 | 2014-06-10 11:53:08 -0700 | [diff] [blame] | 204 | _LOG(log, logtype::MEMORY, "    %s %s\n", code_buffer, ascii_buffer); | 
| Kévin PETIT | 4bb4772 | 2013-12-18 16:44:24 +0000 | [diff] [blame] | 205 | } | 
|  | 206 | } |