blob: 12df1e613be702b870e146ae7254a712fad98c9a [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>
Josh Gao36dd1442016-07-14 16:05:10 -070023#include <stdlib.h>
Pavel Chupinc6c194c2013-11-21 23:17:20 +040024#include <string.h>
25#include <unistd.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070026#include <sys/ptrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070027#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Pavel Chupinc6c194c2013-11-21 23:17:20 +040029#include <backtrace/Backtrace.h>
Mark Salyzyn99f47a92014-04-07 14:58:08 -070030#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Christopher Ferris84ddb342014-10-31 21:34:38 -070032const int SLEEP_TIME_USEC = 50000; // 0.05 seconds
33const int MAX_TOTAL_SLEEP_USEC = 10000000; // 10 seconds
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
Christopher Tateded2e5a2013-03-19 13:12:23 -070035static int write_to_am(int fd, const char* buf, int len) {
Christopher Ferris20303f82014-01-10 16:33:16 -080036 int to_write = len;
37 while (to_write > 0) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040038 int written = TEMP_FAILURE_RETRY(write(fd, buf + len - to_write, to_write));
Christopher Ferris20303f82014-01-10 16:33:16 -080039 if (written < 0) {
40 // hard failure
Brigid Smith50eb5462014-06-18 14:17:57 -070041 ALOGE("AM write failure (%d / %s)\n", errno, strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -080042 return -1;
Christopher Tateded2e5a2013-03-19 13:12:23 -070043 }
Christopher Ferris20303f82014-01-10 16:33:16 -080044 to_write -= written;
45 }
46 return len;
Christopher Tateded2e5a2013-03-19 13:12:23 -070047}
48
Brigid Smith62ba4892014-06-10 11:53:08 -070049// Whitelist output desired in the logcat output.
50bool is_allowed_in_logcat(enum logtype ltype) {
51 if ((ltype == ERROR)
52 || (ltype == HEADER)
53 || (ltype == REGISTERS)
54 || (ltype == BACKTRACE)) {
55 return true;
56 }
57 return false;
58}
59
60void _LOG(log_t* log, enum logtype ltype, const char* fmt, ...) {
Brigid Smith50eb5462014-06-18 14:17:57 -070061 bool write_to_tombstone = (log->tfd != -1);
62 bool write_to_logcat = is_allowed_in_logcat(ltype)
Brigid Smith166cfe62014-07-17 14:52:33 -070063 && log->crashed_tid != -1
64 && log->current_tid != -1
Brigid Smith50eb5462014-06-18 14:17:57 -070065 && (log->crashed_tid == log->current_tid);
66 bool write_to_activitymanager = (log->amfd != -1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Pavel Chupinc6c194c2013-11-21 23:17:20 +040068 char buf[512];
Christopher Ferris20303f82014-01-10 16:33:16 -080069 va_list ap;
70 va_start(ap, fmt);
Pavel Chupinc6c194c2013-11-21 23:17:20 +040071 vsnprintf(buf, sizeof(buf), fmt, ap);
72 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073
Pavel Chupinc6c194c2013-11-21 23:17:20 +040074 size_t len = strlen(buf);
75 if (len <= 0) {
76 return;
Christopher Ferris20303f82014-01-10 16:33:16 -080077 }
78
Brigid Smith62ba4892014-06-10 11:53:08 -070079 if (write_to_tombstone) {
Pavel Chupinc6c194c2013-11-21 23:17:20 +040080 TEMP_FAILURE_RETRY(write(log->tfd, buf, len));
Christopher Ferris20303f82014-01-10 16:33:16 -080081 }
82
Brigid Smith62ba4892014-06-10 11:53:08 -070083 if (write_to_logcat) {
84 __android_log_buf_write(LOG_ID_CRASH, ANDROID_LOG_INFO, LOG_TAG, buf);
85 if (write_to_activitymanager) {
Christopher Ferris20303f82014-01-10 16:33:16 -080086 int written = write_to_am(log->amfd, buf, len);
87 if (written <= 0) {
88 // timeout or other failure on write; stop informing the activity manager
89 log->amfd = -1;
90 }
Christopher Tateded2e5a2013-03-19 13:12:23 -070091 }
Christopher Ferris20303f82014-01-10 16:33:16 -080092 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093}
94
Christopher Ferris84ddb342014-10-31 21:34:38 -070095int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed) {
96 bool allow_dead_tid = false;
Christopher Ferris20303f82014-01-10 16:33:16 -080097 for (;;) {
98 int status;
Christopher Ferris84ddb342014-10-31 21:34:38 -070099 pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG));
100 if (n == -1) {
101 ALOGE("waitpid failed: tid %d, %s", tid, strerror(errno));
102 break;
103 } else if (n == tid) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800104 if (WIFSTOPPED(status)) {
105 return WSTOPSIG(status);
106 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700107 ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status);
Christopher Ferris84ddb342014-10-31 21:34:38 -0700108 // This is the only circumstance under which we can allow a detach
109 // to fail with ESRCH, which indicates the tid has exited.
110 allow_dead_tid = true;
111 break;
Christopher Ferris20303f82014-01-10 16:33:16 -0800112 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700113 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800114
Christopher Ferris84ddb342014-10-31 21:34:38 -0700115 if (*total_sleep_time_usec > MAX_TOTAL_SLEEP_USEC) {
116 ALOGE("timed out waiting for stop signal: tid=%d", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800117 break;
Jeff Brown13e715b2011-10-21 12:14:56 -0700118 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800119
Christopher Ferris84ddb342014-10-31 21:34:38 -0700120 usleep(SLEEP_TIME_USEC);
121 *total_sleep_time_usec += SLEEP_TIME_USEC;
Christopher Ferris20303f82014-01-10 16:33:16 -0800122 }
Christopher Ferris84ddb342014-10-31 21:34:38 -0700123
124 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
125 if (allow_dead_tid && errno == ESRCH) {
126 ALOGE("tid exited before attach completed: tid %d", tid);
127 } else {
128 *detach_failed = true;
129 ALOGE("detach failed: tid %d, %s", tid, strerror(errno));
130 }
131 }
132 return -1;
Jeff Brown13e715b2011-10-21 12:14:56 -0700133}
Kévin PETIT4bb47722013-12-18 16:44:24 +0000134
135#if defined (__mips__)
136#define DUMP_MEMORY_AS_ASCII 1
137#else
138#define DUMP_MEMORY_AS_ASCII 0
139#endif
140
Brigid Smith62ba4892014-06-10 11:53:08 -0700141void dump_memory(log_t* log, pid_t tid, uintptr_t addr) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000142 char code_buffer[64];
143 char ascii_buffer[32];
144 uintptr_t p, end;
145
146 p = addr & ~(sizeof(long) - 1);
147 /* Dump 32 bytes before addr */
148 p -= 32;
149 if (p > addr) {
150 /* catch underflow */
151 p = 0;
152 }
153 /* Dump 256 bytes */
154 end = p + 256;
155 /* catch overflow; 'end - p' has to be multiples of 16 */
156 while (end < p) {
157 end -= 16;
158 }
159
160 /* Dump the code around PC as:
161 * addr contents ascii
162 * 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
163 * 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
164 * On 32-bit machines, there are still 16 bytes per line but addresses and
165 * words are of course presented differently.
166 */
167 while (p < end) {
168 char* asc_out = ascii_buffer;
169
170 int len = snprintf(code_buffer, sizeof(code_buffer), "%" PRIPTR " ", p);
171
172 for (size_t i = 0; i < 16/sizeof(long); i++) {
173 long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL);
174 if (data == -1 && errno != 0) {
175 // ptrace failed, probably because we're dumping memory in an
176 // unmapped or inaccessible page.
177#ifdef __LP64__
178 len += sprintf(code_buffer + len, "---------------- ");
179#else
180 len += sprintf(code_buffer + len, "-------- ");
181#endif
182 } else {
183 len += sprintf(code_buffer + len, "%" PRIPTR " ",
184 static_cast<uintptr_t>(data));
185 }
186
187#if DUMP_MEMORY_AS_ASCII
188 for (size_t j = 0; j < sizeof(long); j++) {
189 /*
190 * Our isprint() allows high-ASCII characters that display
191 * differently (often badly) in different viewers, so we
192 * just use a simpler test.
193 */
194 char val = (data >> (j*8)) & 0xff;
195 if (val >= 0x20 && val < 0x7f) {
196 *asc_out++ = val;
197 } else {
198 *asc_out++ = '.';
199 }
200 }
201#endif
202 p += sizeof(long);
203 }
204 *asc_out = '\0';
Brigid Smith62ba4892014-06-10 11:53:08 -0700205 _LOG(log, logtype::MEMORY, " %s %s\n", code_buffer, ascii_buffer);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000206 }
207}
Josh Gao36dd1442016-07-14 16:05:10 -0700208
209bool pid_contains_tid(pid_t pid, pid_t tid) {
210 char task_path[PATH_MAX];
211 if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) {
212 ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid);
213 exit(1);
214 }
215
216 return access(task_path, F_OK) == 0;
217}
218
219// Attach to a thread, and verify that it's still a member of the given process
220bool ptrace_attach_thread(pid_t pid, pid_t tid) {
221 if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
222 return false;
223 }
224
225 // Make sure that the task we attached to is actually part of the pid we're dumping.
226 if (!pid_contains_tid(pid, tid)) {
227 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
228 ALOGE("debuggerd: failed to detach from thread '%d'", tid);
229 exit(1);
230 }
231 return false;
232 }
233
234 return true;
235}