blob: 9f340a8ac0c273dfb7146a222557468a40810e17 [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) {
38 if ((ltype == ERROR)
39 || (ltype == HEADER)
40 || (ltype == REGISTERS)
41 || (ltype == BACKTRACE)) {
42 return true;
43 }
44 return false;
45}
46
47void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Brigid Smith50eb5462014-06-18 14:17:57 -070048 bool write_to_tombstone = (log->tfd != -1);
49 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smithc75a02f2014-07-17 14:52:33 -070050 && log->crashed_tid != -1
51 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070052 && (log->crashed_tid == log->current_tid);
53 bool write_to_activitymanager = (log->amfd != -1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Pavel Chupinc6c194c2013-11-21 23:17:20 +040055 char buf[512];
Christopher Ferris20303f82014-01-10 16:33:16 -080056 va_list ap;
57 va_start(ap, fmt);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040058 vsnprintf(buf, sizeof(buf), fmt, ap);
59 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Pavel Chupinc6c194c2013-11-21 23:17:20 +040061 size_t len = strlen(buf);
62 if (len <= 0) {
63 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080064 }
65
Brigid Smith62ba4892014-06-10 11:53:08 -070066 if (write_to_tombstone) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040067 TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
Christopher Ferris20303f82014-01-10 16:33:16 -080068 }
69
Brigid Smith62ba4892014-06-10 11:53:08 -070070 if (write_to_logcat) {
Christopher Ferrisb0412a52015-05-05 12:23:06 -070071 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_FATAL, LOG_TAG, buf);
Brigid Smith62ba4892014-06-10 11:53:08 -070072 if (write_to_activitymanager) {
Elliott Hughesb7cd09b2015-04-24 22:25:12 -070073 if (!android::base::WriteFully(log->amfd, buf, len)) {
Christopher Ferris20303f82014-01-10 16:33:16 -080074 // timeout or other failure on write; stop informing the activity manager
Elliott Hughesb7cd09b2015-04-24 22:25:12 -070075 ALOGE("AM write failed: %s", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080076 log->amfd = -1;
77 }
Christopher Tateded2e5a2013-03-19 13:12:23 -070078 }
Christopher Ferris20303f82014-01-10 16:33:16 -080079 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080}
81
Christopher Ferris1072f912014-10-31 21:34:38 -070082int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed) {
83 bool allow_dead_tid = false;
Christopher Ferris20303f82014-01-10 16:33:16 -080084 for (;;) {
85 int status;
Christopher Ferris1072f912014-10-31 21:34:38 -070086 pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG));
87 if (n == -1) {
88 ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno));
89 break;
90 } else if (n == tid) {
Christopher Ferris20303f82014-01-10 16:33:16 -080091 if (WIFSTOPPED(status)) {
92 return WSTOPSIG(status);
93 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -070094 ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status);
Christopher Ferris1072f912014-10-31 21:34:38 -070095 // This is the only circumstance under which we can allow a detach
96 // to fail with ESRCH, which indicates the tid has exited.
97 allow_dead_tid = true;
98 break;
Christopher Ferris20303f82014-01-10 16:33:16 -080099 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700100 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800101
Christopher Ferris1072f912014-10-31 21:34:38 -0700102 if (*total_sleep_time_usec > MAX_TOTAL_SLEEP_USEC) {
103 ALOGE("timed out waiting for stop signal: tid=%d", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 break;
Jeff Brown13e715b2011-10-21 12:14:56 -0700105 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800106
Christopher Ferris1072f912014-10-31 21:34:38 -0700107 usleep(SLEEP_TIME_USEC);
108 *total_sleep_time_usec += SLEEP_TIME_USEC;
Christopher Ferris20303f82014-01-10 16:33:16 -0800109 }
Christopher Ferris1072f912014-10-31 21:34:38 -0700110
111 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
112 if (allow_dead_tid && errno == ESRCH) {
113 ALOGE("tid exited before attach completed: tid %d", tid);
114 } else {
115 *detach_failed = true;
116 ALOGE("detach failed: tid %d, %s", tid, strerror(errno));
117 }
118 }
119 return -1;
Jeff Brown13e715b2011-10-21 12:14:56 -0700120}
Kévin PETIT4bb47722013-12-18 16:44:24 +0000121
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700122#define MEMORY_BYTES_TO_DUMP 256
123#define MEMORY_BYTES_PER_LINE 16
Kévin PETIT4bb47722013-12-18 16:44:24 +0000124
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700125void dump_memory(log_t* log, Backtrace* backtrace, uintptr_t addr, const char* fmt, ...) {
126 std::string log_msg;
127 va_list ap;
128 va_start(ap, fmt);
129 android::base::StringAppendV(&log_msg, fmt, ap);
130 va_end(ap);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000131
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700132 // Align the address to sizeof(long) and start 32 bytes before the address.
133 addr &= ~(sizeof(long) - 1);
134 if (addr >= 4128) {
135 addr -= 32;
136 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000137
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700138 // Don't bother if the address looks too low, or looks too high.
139 if (addr < 4096 ||
140#if defined(__LP64__)
141 addr > 0x4000000000000000UL - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000142#else
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700143 addr > 0xffff0000 - MEMORY_BYTES_TO_DUMP) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000144#endif
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700145 return;
146 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000147
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700148 _LOG(log, logtype::MEMORY, "\n%s\n", log_msg.c_str());
149
150 // Dump 256 bytes
151 uintptr_t data[MEMORY_BYTES_TO_DUMP/sizeof(uintptr_t)];
152 memset(data, 0, MEMORY_BYTES_TO_DUMP);
153 size_t bytes = backtrace->Read(addr, reinterpret_cast<uint8_t*>(data), sizeof(data));
154 if (bytes % sizeof(uintptr_t) != 0) {
155 // This should never happen, but just in case.
156 ALOGE("Bytes read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
157 bytes &= ~(sizeof(uintptr_t) - 1);
158 }
159
160 if (bytes < MEMORY_BYTES_TO_DUMP && bytes > 0) {
161 // Try to do one more read. This could happen if a read crosses a map, but
162 // the maps do not have any break between them. Only requires one extra
163 // read because a map has to contain at least one page, and the total
164 // number of bytes to dump is smaller than a page.
165 size_t bytes2 = backtrace->Read(addr + bytes, reinterpret_cast<uint8_t*>(data) + bytes,
166 sizeof(data) - bytes);
167 bytes += bytes2;
168 if (bytes2 > 0 && bytes % sizeof(uintptr_t) != 0) {
169 // This should never happen, but we'll try and continue any way.
170 ALOGE("Bytes after second read %zu, is not a multiple of %zu", bytes, sizeof(uintptr_t));
171 bytes &= ~(sizeof(uintptr_t) - 1);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000172 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700173 }
174
175 // Dump the code around memory as:
176 // addr contents ascii
177 // 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
178 // 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
179 // On 32-bit machines, there are still 16 bytes per line but addresses and
180 // words are of course presented differently.
181 uintptr_t* data_ptr = data;
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;
188 for (size_t i = 0; i < MEMORY_BYTES_PER_LINE / sizeof(uintptr_t); i++, data_ptr++) {
189 if (bytes >= sizeof(uintptr_t)) {
190 bytes -= sizeof(uintptr_t);
191 android::base::StringAppendF(&logline, " %" PRIPTR, *data_ptr);
192
193 // Fill out the ascii string from the data.
194 uint8_t* ptr = reinterpret_cast<uint8_t*>(data_ptr);
195 for (size_t val = 0; val < sizeof(uintptr_t); val++, ptr++) {
196 if (*ptr >= 0x20 && *ptr < 0x7f) {
197 ascii += *ptr;
198 } else {
199 ascii += '.';
200 }
201 }
202 } else {
203 logline += ' ' + std::string(sizeof(uintptr_t) * 2, '-');
204 ascii += std::string(sizeof(uintptr_t), '.');
205 }
206 }
207 _LOG(log, logtype::MEMORY, "%s %s\n", logline.c_str(), ascii.c_str());
208 }
Kévin PETIT4bb47722013-12-18 16:44:24 +0000209}