blob: 554d2f28a3bb217c7271a766aa0a0d98d35e76e5 [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 Gao05288292016-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 Ferris20303f82014-01-10 16:33:16 -080032const 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
Jeff Brown053b8652012-06-06 16:25:03 -070095int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -080096 for (;;) {
97 int status;
98 pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
99 if (n < 0) {
100 if (errno == EAGAIN)
101 continue;
Brigid Smith50eb5462014-06-18 14:17:57 -0700102 ALOGE("waitpid failed: %s\n", strerror(errno));
Christopher Ferris20303f82014-01-10 16:33:16 -0800103 return -1;
104 } else if (n > 0) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700105 ALOGV("waitpid: n=%d status=%08x\n", n, status);
Christopher Ferris20303f82014-01-10 16:33:16 -0800106 if (WIFSTOPPED(status)) {
107 return WSTOPSIG(status);
108 } else {
Brigid Smith50eb5462014-06-18 14:17:57 -0700109 ALOGE("unexpected waitpid response: n=%d, status=%08x\n", n, status);
Christopher Ferris20303f82014-01-10 16:33:16 -0800110 return -1;
111 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700112 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800113
114 if (*total_sleep_time_usec > max_total_sleep_usec) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700115 ALOGE("timed out waiting for tid=%d to die\n", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800116 return -1;
117 }
118
119 // not ready yet
Brigid Smith50eb5462014-06-18 14:17:57 -0700120 ALOGV("not ready yet\n");
Christopher Ferris20303f82014-01-10 16:33:16 -0800121 usleep(sleep_time_usec);
122 *total_sleep_time_usec += sleep_time_usec;
123 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700124}
125
Jeff Brown053b8652012-06-06 16:25:03 -0700126void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800127 siginfo_t si;
128 while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) {
129 if (*total_sleep_time_usec > max_total_sleep_usec) {
Brigid Smith50eb5462014-06-18 14:17:57 -0700130 ALOGE("timed out waiting for tid=%d to stop\n", tid);
Christopher Ferris20303f82014-01-10 16:33:16 -0800131 break;
Jeff Brown13e715b2011-10-21 12:14:56 -0700132 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800133
134 usleep(sleep_time_usec);
135 *total_sleep_time_usec += sleep_time_usec;
136 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700137}
Kévin PETIT4bb47722013-12-18 16:44:24 +0000138
139#if defined (__mips__)
140#define DUMP_MEMORY_AS_ASCII 1
141#else
142#define DUMP_MEMORY_AS_ASCII 0
143#endif
144
Brigid Smith62ba4892014-06-10 11:53:08 -0700145void dump_memory(log_t* log, pid_t tid, uintptr_t addr) {
Kévin PETIT4bb47722013-12-18 16:44:24 +0000146 char code_buffer[64];
147 char ascii_buffer[32];
148 uintptr_t p, end;
149
150 p = addr & ~(sizeof(long) - 1);
151 /* Dump 32 bytes before addr */
152 p -= 32;
153 if (p > addr) {
154 /* catch underflow */
155 p = 0;
156 }
157 /* Dump 256 bytes */
158 end = p + 256;
159 /* catch overflow; 'end - p' has to be multiples of 16 */
160 while (end < p) {
161 end -= 16;
162 }
163
164 /* Dump the code around PC as:
165 * addr contents ascii
166 * 0000000000008d34 ef000000e8bd0090 e1b00000512fff1e ............../Q
167 * 0000000000008d44 ea00b1f9e92d0090 e3a070fcef000000 ......-..p......
168 * On 32-bit machines, there are still 16 bytes per line but addresses and
169 * words are of course presented differently.
170 */
171 while (p < end) {
172 char* asc_out = ascii_buffer;
173
174 int len = snprintf(code_buffer, sizeof(code_buffer), "%" PRIPTR " ", p);
175
176 for (size_t i = 0; i < 16/sizeof(long); i++) {
177 long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL);
178 if (data == -1 && errno != 0) {
179 // ptrace failed, probably because we're dumping memory in an
180 // unmapped or inaccessible page.
181#ifdef __LP64__
182 len += sprintf(code_buffer + len, "---------------- ");
183#else
184 len += sprintf(code_buffer + len, "-------- ");
185#endif
186 } else {
187 len += sprintf(code_buffer + len, "%" PRIPTR " ",
188 static_cast<uintptr_t>(data));
189 }
190
191#if DUMP_MEMORY_AS_ASCII
192 for (size_t j = 0; j < sizeof(long); j++) {
193 /*
194 * Our isprint() allows high-ASCII characters that display
195 * differently (often badly) in different viewers, so we
196 * just use a simpler test.
197 */
198 char val = (data >> (j*8)) & 0xff;
199 if (val >= 0x20 && val < 0x7f) {
200 *asc_out++ = val;
201 } else {
202 *asc_out++ = '.';
203 }
204 }
205#endif
206 p += sizeof(long);
207 }
208 *asc_out = '\0';
Brigid Smith62ba4892014-06-10 11:53:08 -0700209 _LOG(log, logtype::MEMORY, " %s %s\n", code_buffer, ascii_buffer);
Kévin PETIT4bb47722013-12-18 16:44:24 +0000210 }
211}
Josh Gao05288292016-07-14 16:05:10 -0700212
213bool pid_contains_tid(pid_t pid, pid_t tid) {
214 char task_path[PATH_MAX];
215 if (snprintf(task_path, PATH_MAX, "/proc/%d/task/%d", pid, tid) >= PATH_MAX) {
216 ALOGE("debuggerd: task path overflow (pid = %d, tid = %d)\n", pid, tid);
217 exit(1);
218 }
219
220 return access(task_path, F_OK) == 0;
221}
222
223// Attach to a thread, and verify that it's still a member of the given process
224bool ptrace_attach_thread(pid_t pid, pid_t tid) {
225 if (ptrace(PTRACE_ATTACH, tid, 0, 0) != 0) {
226 return false;
227 }
228
229 // Make sure that the task we attached to is actually part of the pid we're dumping.
230 if (!pid_contains_tid(pid, tid)) {
231 if (ptrace(PTRACE_DETACH, tid, 0, 0) != 0) {
232 ALOGE("debuggerd: failed to detach from thread '%d'", tid);
233 exit(1);
234 }
235 return false;
236 }
237
238 return true;
239}