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