blob: f5d6ec158c38c6bca9de94a164e2fd4aedf33d5e [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
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 Projectdd7bc332009-03-03 19:32:55 -080016
Brigid Smith62ba4892014-06-10 11:53:08 -070017#define LOG_TAG "DEBUG"
18
Pavel Chupinc6c194c2013-11-21 23:17:20 +040019#include "utility.h"
20
Jeff Brown053b8652012-06-06 16:25:03 -070021#include <errno.h>
Jeff Brown053b8652012-06-06 16:25:03 -070022#include <signal.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040023#include <string.h>
24#include <unistd.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070025#include <sys/ptrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070026#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Pavel Chupinc6c194c2013-11-21 23:17:20 +040028#include <backtrace/Backtrace.h>
Elliott Hughesb7cd09b2015-04-24 22:25:12 -070029#include <base/file.h>
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070030#include <base/stringprintf.h>
Mark Salyzyn99f47a92014-04-07 14:58:08 -070031#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Christopher Ferris1072f912014-10-31 21:34:38 -070033const int SLEEP_TIME_USEC = 50000; // 0.05 seconds
34const int MAX_TOTAL_SLEEP_USEC = 10000000; // 10 seconds
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Brigid Smith62ba4892014-06-10 11:53:08 -070036// Whitelist output desired in the logcat output.
37bool is_allowed_in_logcat(enum logtype ltype) {
Christopher Ferrisb36b5922015-06-17 18:35:59 -070038 if ((ltype == HEADER)
Brigid Smith62ba4892014-06-10 11:53:08 -070039 || (ltype == REGISTERS)
40 || (ltype == BACKTRACE)) {
41 return true;
42 }
43 return false;
44}
45
46void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Brigid Smith50eb5462014-06-18 14:17:57 -070047 bool write_to_tombstone = (log->tfd != -1);
48 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smithc75a02f2014-07-17 14:52:33 -070049 && log->crashed_tid != -1
50 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070051 && (log->crashed_tid == log->current_tid);
52 bool write_to_activitymanager = (log->amfd != -1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Pavel Chupinc6c194c2013-11-21 23:17:20 +040054 char buf[512];
Christopher Ferris20303f82014-01-10 16:33:16 -080055 va_list ap;
56 va_start(ap, fmt);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040057 vsnprintf(buf, sizeof(buf), fmt, ap);
58 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Pavel Chupinc6c194c2013-11-21 23:17:20 +040060 size_t len = strlen(buf);
61 if (len <= 0) {
62 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080063 }
64
Brigid Smith62ba4892014-06-10 11:53:08 -070065 if (write_to_tombstone) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040066 TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
Christopher Ferris20303f82014-01-10 16:33:16 -080067 }
68
Brigid Smith62ba4892014-06-10 11:53:08 -070069 if (write_to_logcat) {
Christopher Ferrisb0412a52015-05-05 12:23:06 -070070 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf);
Brigid Smith62ba4892014-06-10 11:53:08 -070071 if (write_to_activitymanager) {
Elliott Hughesb7cd09b2015-04-24 22:25:12 -070072 if (!android::base::WriteFully(log->amfd, buf, len)) {
Christopher Ferris20303f82014-01-10 16:33:16 -080073 // timeout or other failure on write; stop informing the activity manager
Elliott Hughesb7cd09b2015-04-24 22:25:12 -070074 ALOGE("AM write failed: %s", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080075 log->amfd = -1;
76 }
Christopher Tateded2e5a2013-03-19 13:12:23 -070077 }
Christopher Ferris20303f82014-01-10 16:33:16 -080078 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079}
80
Christopher Ferris1072f912014-10-31 21:34:38 -070081int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed) {
82 bool allow_dead_tid = false;
Christopher Ferris20303f82014-01-10 16:33:16 -080083 for (;;) {
84 int status;
Christopher Ferris1072f912014-10-31 21:34:38 -070085 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 Ferris20303f82014-01-10 16:33:16 -080090 if (WIFSTOPPED(status)) {
91 return WSTOPSIG(status);
92 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -070093 ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status);
Christopher Ferris1072f912014-10-31 21:34:38 -070094 // 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 Ferris20303f82014-01-10 16:33:16 -080098 }
Jeff Brown13e715b2011-10-21 12:14:56 -070099 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800100
Christopher Ferris1072f912014-10-31 21:34:38 -0700101 if (*total_sleep_time_usec > MAX_TOTAL_SLEEP_USEC) {
102 ALOGE("timed out waiting for stop signal: tid=%d", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 break;
Jeff Brown13e715b2011-10-21 12:14:56 -0700104 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800105
Christopher Ferris1072f912014-10-31 21:34:38 -0700106 usleep(SLEEP_TIME_USEC);
107 *total_sleep_time_usec += SLEEP_TIME_USEC;
Christopher Ferris20303f82014-01-10 16:33:16 -0800108 }
Christopher Ferris1072f912014-10-31 21:34:38 -0700109
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 Brown13e715b2011-10-21 12:14:56 -0700119}
Kévin PETIT4bb47722013-12-18 16:44:24 +0000120
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700121#define MEMORY_BYTES_TO_DUMP 256
122#define MEMORY_BYTES_PER_LINE 16
Kévin PETIT4bb47722013-12-18 16:44:24 +0000123
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700124void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* fmt, ...) {
125 std::string log_msg;
126 va_list ap;
127 va_start(ap, fmt);
128 android::base::StringAppendV(&log_msg, fmt, ap);
129 va_end(ap);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000130
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700131 // Align the address to sizeof(long) and start 32 bytes before the address.
132 addr &= ~(sizeof(long) - 1);
133 if (addr >= 4128) {
134 addr -= 32;
135 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000136
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700137 // Don't bother if the address looks too low, or looks too high.
138 if (addr < 4096 ||
139#if defined(__LP64__)
140 addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000141#else
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700142 addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000143#endif
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700144 return;
145 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000146
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700147 _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str());
148
149 // Dump 256 bytes
150 uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)];
151 memset(data, 0, MEMORY_BYTES_TO_DUMP);
152 size_t bytes = backtrace->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data));
153 if (bytes % sizeof(uintptr_t) != 0) {
154 // This should never happen, but just in case.
155 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
156 bytes &= ~(sizeof(uintptr_t) - 1);
157 }
158
Christopher Ferris456abba2015-07-09 15:35:47 -0700159 uintptr_t start = 0;
160 bool skip_2nd_read = false;
161 if (bytes == 0) {
162 // In this case, we might want to try another read at the beginning of
163 // the next page only if it's within the amount of memory we would have
164 // read.
165 size_t page_size = sysconf(_SC_PAGE_SIZE);
166 start = ((addr + (page_size - 1)) & ~(page_size - 1)) - addr;
167 if (start == 0 || start >= MEMORY_BYTES_TO_DUMP) {
168 skip_2nd_read = true;
169 }
170 }
171
172 if (bytes < MEMORY_BYTES_TO_DUMP && !skip_2nd_read) {
173 // Try to do one more read. This could happen if a read crosses a map,
174 // but the maps do not have any break between them. Or it could happen
175 // if reading from an unreadable map, but the read would cross back
176 // into a readable map. Only requires one extra read because a map has
177 // to contain at least one page, and the total number of bytes to dump
178 // is smaller than a page.
179 size_t bytes2 = backtrace->Read(addr + start + bytes, reinterpret_cast<uint8_t*>(data) + bytes,
180 sizeof(data) - bytes - start);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700181 bytes += bytes2;
182 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
183 // This should never happen, but we'll try and continue any way.
184 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
185 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000186 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700187 }
188
189 // Dump the code around memory as:
190 // addr contents ascii
191 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
192 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
193 // On 32-bit machines, there are still 16 bytes per line but addresses and
194 // words are of course presented differently.
195 uintptr_t* data_ptr = data;
Christopher Ferris456abba2015-07-09 15:35:47 -0700196 size_t current = 0;
197 size_t total_bytes = start + bytes;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700198 for (size_t line = 0; line < MEMORY_BYTES_TO_DUMP / MEMORY_BYTES_PER_LINE; line++) {
199 std::string logline;
200 android::base::StringAppendF(&logline, " %" PRIPTR, addr);
201
202 addr += MEMORY_BYTES_PER_LINE;
203 std::string ascii;
Christopher Ferris456abba2015-07-09 15:35:47 -0700204 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++) {
205 if (current >= start && current + sizeof(uintptr_t) <= total_bytes) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700206 android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr);
207
208 // Fill out the ascii string from the data.
209 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
210 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
211 if (*ptr >= 0x20 && *ptr < 0x7f) {
212 ascii += *ptr;
213 } else {
214 ascii += '.';
215 }
216 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700217 data_ptr++;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700218 } else {
219 logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-');
220 ascii += std::string(sizeof(uintptr_t), '.');
221 }
Christopher Ferris456abba2015-07-09 15:35:47 -0700222 current += sizeof(uintptr_t);
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700223 }
224 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
225 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000226}