blob: 0f05bfd82dd9443a1d0674fff8ba3c5a5bb5c767 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* system/debuggerd/debuggerd.c
2**
3** Copyright 2006, The Android Open Source Project
4**
Ben Cheng09e71372009-09-28 11:06:09 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008**
Ben Cheng09e71372009-09-28 11:06:09 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010**
Ben Cheng09e71372009-09-28 11:06:09 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015** limitations under the License.
16*/
17
18#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <errno.h>
20#include <signal.h>
21#include <pthread.h>
22#include <stdarg.h>
23#include <fcntl.h>
24#include <sys/types.h>
25#include <dirent.h>
26
27#include <sys/ptrace.h>
28#include <sys/wait.h>
29#include <sys/exec_elf.h>
30#include <sys/stat.h>
Jeff Brown9524e412011-10-24 11:10:16 -070031#include <sys/poll.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
33#include <cutils/sockets.h>
34#include <cutils/logd.h>
Andy McFadden41e0cef2011-10-13 16:05:08 -070035#include <cutils/logger.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <cutils/properties.h>
37
Jeff Brown13e715b2011-10-21 12:14:56 -070038#include <corkscrew/backtrace.h>
39
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <linux/input.h>
41
42#include <private/android_filesystem_config.h>
43
Jeff Brown13e715b2011-10-21 12:14:56 -070044#include "getevent.h"
45#include "machine.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#include "utility.h"
47
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048#define ANDROID_LOG_INFO 4
49
Jeff Brown13e715b2011-10-21 12:14:56 -070050static void dump_build_info(int tfd)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051{
52 char fingerprint[PROPERTY_VALUE_MAX];
53
54 property_get("ro.build.fingerprint", fingerprint, "unknown");
55
56 _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint);
57}
58
Jeff Brown13e715b2011-10-21 12:14:56 -070059static const char *get_signame(int sig)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060{
61 switch(sig) {
62 case SIGILL: return "SIGILL";
63 case SIGABRT: return "SIGABRT";
64 case SIGBUS: return "SIGBUS";
65 case SIGFPE: return "SIGFPE";
66 case SIGSEGV: return "SIGSEGV";
67 case SIGSTKFLT: return "SIGSTKFLT";
Jeff Brown9524e412011-10-24 11:10:16 -070068 case SIGSTOP: return "SIGSTOP";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069 default: return "?";
70 }
71}
72
Jeff Brown13e715b2011-10-21 12:14:56 -070073static const char *get_sigcode(int signo, int code)
Carl Shapiro83c6b052010-10-08 18:10:24 -070074{
75 switch (signo) {
76 case SIGILL:
77 switch (code) {
78 case ILL_ILLOPC: return "ILL_ILLOPC";
79 case ILL_ILLOPN: return "ILL_ILLOPN";
80 case ILL_ILLADR: return "ILL_ILLADR";
81 case ILL_ILLTRP: return "ILL_ILLTRP";
82 case ILL_PRVOPC: return "ILL_PRVOPC";
83 case ILL_PRVREG: return "ILL_PRVREG";
84 case ILL_COPROC: return "ILL_COPROC";
85 case ILL_BADSTK: return "ILL_BADSTK";
86 }
87 break;
88 case SIGBUS:
89 switch (code) {
90 case BUS_ADRALN: return "BUS_ADRALN";
91 case BUS_ADRERR: return "BUS_ADRERR";
92 case BUS_OBJERR: return "BUS_OBJERR";
93 }
94 break;
95 case SIGFPE:
96 switch (code) {
97 case FPE_INTDIV: return "FPE_INTDIV";
98 case FPE_INTOVF: return "FPE_INTOVF";
99 case FPE_FLTDIV: return "FPE_FLTDIV";
100 case FPE_FLTOVF: return "FPE_FLTOVF";
101 case FPE_FLTUND: return "FPE_FLTUND";
102 case FPE_FLTRES: return "FPE_FLTRES";
103 case FPE_FLTINV: return "FPE_FLTINV";
104 case FPE_FLTSUB: return "FPE_FLTSUB";
105 }
106 break;
107 case SIGSEGV:
108 switch (code) {
109 case SEGV_MAPERR: return "SEGV_MAPERR";
110 case SEGV_ACCERR: return "SEGV_ACCERR";
111 }
112 break;
113 }
114 return "?";
115}
116
Jeff Brownf0c58722011-11-03 17:58:44 -0700117static void dump_fault_addr(int tfd, pid_t tid, int sig)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118{
119 siginfo_t si;
Ben Cheng09e71372009-09-28 11:06:09 -0700120
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 memset(&si, 0, sizeof(si));
Jeff Brownf0c58722011-11-03 17:58:44 -0700122 if(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
Andy McFadden136dcc52011-09-22 16:37:06 -0700124 } else if (signal_has_address(sig)) {
Carl Shapiro83c6b052010-10-08 18:10:24 -0700125 _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr %08x\n",
126 sig, get_signame(sig),
127 si.si_code, get_sigcode(sig, si.si_code),
Andy McFaddene5cc5392011-10-18 20:03:07 -0700128 (uintptr_t) si.si_addr);
Andy McFadden136dcc52011-09-22 16:37:06 -0700129 } else {
130 _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr --------\n",
131 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 }
133}
134
Jeff Brown13e715b2011-10-21 12:14:56 -0700135static void dump_crash_banner(int tfd, pid_t pid, pid_t tid, int sig)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136{
137 char data[1024];
138 char *x = 0;
139 FILE *fp;
Ben Cheng09e71372009-09-28 11:06:09 -0700140
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 sprintf(data, "/proc/%d/cmdline", pid);
142 fp = fopen(data, "r");
143 if(fp) {
144 x = fgets(data, 1024, fp);
145 fclose(fp);
146 }
Ben Cheng09e71372009-09-28 11:06:09 -0700147
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 _LOG(tfd, false,
Jeff Brown13e715b2011-10-21 12:14:56 -0700149 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 dump_build_info(tfd);
151 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n",
152 pid, tid, x ? x : "UNKNOWN");
Ben Cheng09e71372009-09-28 11:06:09 -0700153
Jeff Brown13e715b2011-10-21 12:14:56 -0700154 if(sig) {
155 dump_fault_addr(tfd, tid, sig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 }
157}
158
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159/* Return true if some thread is not detached cleanly */
Jeff Brownf0c58722011-11-03 17:58:44 -0700160static bool dump_sibling_thread_report(const ptrace_context_t* context,
Jeff Brown9524e412011-10-24 11:10:16 -0700161 int tfd, pid_t pid, pid_t tid) {
162 char task_path[64];
163 snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Jeff Brown9524e412011-10-24 11:10:16 -0700165 DIR* d = opendir(task_path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 /* Bail early if cannot open the task directory */
167 if (d == NULL) {
168 XLOG("Cannot open /proc/%d/task\n", pid);
169 return false;
170 }
Jeff Brown9524e412011-10-24 11:10:16 -0700171
172 bool detach_failed = false;
173 struct dirent *de;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 while ((de = readdir(d)) != NULL) {
Jeff Brown13e715b2011-10-21 12:14:56 -0700175 pid_t new_tid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 /* Ignore "." and ".." */
Jeff Brown9524e412011-10-24 11:10:16 -0700177 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 continue;
Jeff Brown9524e412011-10-24 11:10:16 -0700179 }
180
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 new_tid = atoi(de->d_name);
182 /* The main thread at fault has been handled individually */
Jeff Brown9524e412011-10-24 11:10:16 -0700183 if (new_tid == tid) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 continue;
Jeff Brown9524e412011-10-24 11:10:16 -0700185 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
187 /* Skip this thread if cannot ptrace it */
Jeff Brown9524e412011-10-24 11:10:16 -0700188 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 continue;
Jeff Brown9524e412011-10-24 11:10:16 -0700190 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Jeff Brown9524e412011-10-24 11:10:16 -0700192 _LOG(tfd, true, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
Jeff Brown13e715b2011-10-21 12:14:56 -0700193 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, new_tid);
194
195 dump_thread(context, tfd, new_tid, false);
Andy McFaddene5cc5392011-10-18 20:03:07 -0700196
197 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
Jeff Brown9524e412011-10-24 11:10:16 -0700198 LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
199 detach_failed = true;
Andy McFaddene5cc5392011-10-18 20:03:07 -0700200 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 }
Andy McFaddene5cc5392011-10-18 20:03:07 -0700202
Jeff Brown9524e412011-10-24 11:10:16 -0700203 closedir(d);
204 return detach_failed;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205}
206
Andy McFadden41e0cef2011-10-13 16:05:08 -0700207/*
208 * Reads the contents of the specified log device, filters out the entries
209 * that don't match the specified pid, and writes them to the tombstone file.
Andy McFaddene5cc5392011-10-18 20:03:07 -0700210 *
211 * If "tailOnly" is set, we only print the last few lines.
Andy McFadden41e0cef2011-10-13 16:05:08 -0700212 */
Jeff Brown13e715b2011-10-21 12:14:56 -0700213static void dump_log_file(int tfd, pid_t pid, const char* filename,
Andy McFaddene5cc5392011-10-18 20:03:07 -0700214 bool tailOnly)
Andy McFadden41e0cef2011-10-13 16:05:08 -0700215{
Andy McFaddene5cc5392011-10-18 20:03:07 -0700216 bool first = true;
217
218 /* circular buffer, for "tailOnly" mode */
219 const int kShortLogMaxLines = 5;
220 const int kShortLogLineLen = 256;
221 char shortLog[kShortLogMaxLines][kShortLogLineLen];
222 int shortLogCount = 0;
223 int shortLogNext = 0;
224
Andy McFadden41e0cef2011-10-13 16:05:08 -0700225 int logfd = open(filename, O_RDONLY | O_NONBLOCK);
226 if (logfd < 0) {
227 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
228 return;
229 }
Andy McFadden41e0cef2011-10-13 16:05:08 -0700230
231 union {
232 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
233 struct logger_entry entry;
234 } log_entry;
235
236 while (true) {
237 ssize_t actual = read(logfd, log_entry.buf, LOGGER_ENTRY_MAX_LEN);
238 if (actual < 0) {
239 if (errno == EINTR) {
240 /* interrupted by signal, retry */
241 continue;
242 } else if (errno == EAGAIN) {
243 /* non-blocking EOF; we're done */
244 break;
245 } else {
246 _LOG(tfd, true, "Error while reading log: %s\n",
247 strerror(errno));
248 break;
249 }
250 } else if (actual == 0) {
251 _LOG(tfd, true, "Got zero bytes while reading log: %s\n",
252 strerror(errno));
253 break;
254 }
255
256 /*
257 * NOTE: if you XLOG something here, this will spin forever,
258 * because you will be writing as fast as you're reading. Any
259 * high-frequency debug diagnostics should just be written to
260 * the tombstone file.
261 */
262
263 struct logger_entry* entry = &log_entry.entry;
264
265 if (entry->pid != (int32_t) pid) {
266 /* wrong pid, ignore */
267 continue;
268 }
269
Andy McFaddene5cc5392011-10-18 20:03:07 -0700270 if (first) {
271 _LOG(tfd, true, "--------- %slog %s\n",
272 tailOnly ? "tail end of " : "", filename);
273 first = false;
274 }
275
Andy McFadden41e0cef2011-10-13 16:05:08 -0700276 /*
277 * Msg format is: <priority:1><tag:N>\0<message:N>\0
278 *
279 * We want to display it in the same format as "logcat -v threadtime"
280 * (although in this case the pid is redundant).
281 *
282 * TODO: scan for line breaks ('\n') and display each text line
283 * on a separate line, prefixed with the header, like logcat does.
284 */
285 static const char* kPrioChars = "!.VDIWEFS";
286 unsigned char prio = entry->msg[0];
Andy McFaddene5cc5392011-10-18 20:03:07 -0700287 char* tag = entry->msg + 1;
288 char* msg = tag + strlen(tag) + 1;
Andy McFadden41e0cef2011-10-13 16:05:08 -0700289
Andy McFaddene5cc5392011-10-18 20:03:07 -0700290 /* consume any trailing newlines */
291 char* eatnl = msg + strlen(msg) - 1;
292 while (eatnl >= msg && *eatnl == '\n') {
293 *eatnl-- = '\0';
294 }
295
296 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
Andy McFadden41e0cef2011-10-13 16:05:08 -0700297
298 char timeBuf[32];
299 time_t sec = (time_t) entry->sec;
300 struct tm tmBuf;
301 struct tm* ptm;
302 ptm = localtime_r(&sec, &tmBuf);
303 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
304
Andy McFaddene5cc5392011-10-18 20:03:07 -0700305 if (tailOnly) {
306 snprintf(shortLog[shortLogNext], kShortLogLineLen,
307 "%s.%03d %5d %5d %c %-8s: %s",
308 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
309 prioChar, tag, msg);
310 shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
311 shortLogCount++;
312 } else {
313 _LOG(tfd, true, "%s.%03d %5d %5d %c %-8s: %s\n",
314 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
315 prioChar, tag, msg);
316 }
317 }
318
319 if (tailOnly) {
320 int i;
321
322 /*
323 * If we filled the buffer, we want to start at "next", which has
324 * the oldest entry. If we didn't, we want to start at zero.
325 */
326 if (shortLogCount < kShortLogMaxLines) {
327 shortLogNext = 0;
328 } else {
329 shortLogCount = kShortLogMaxLines; /* cap at window size */
330 }
331
332 for (i = 0; i < shortLogCount; i++) {
333 _LOG(tfd, true, "%s\n", shortLog[shortLogNext]);
334 shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
335 }
Andy McFadden41e0cef2011-10-13 16:05:08 -0700336 }
337
338 close(logfd);
339}
340
341/*
342 * Dumps the logs generated by the specified pid to the tombstone, from both
343 * "system" and "main" log devices. Ideally we'd interleave the output.
344 */
Jeff Brown13e715b2011-10-21 12:14:56 -0700345static void dump_logs(int tfd, pid_t pid, bool tailOnly)
Andy McFadden41e0cef2011-10-13 16:05:08 -0700346{
Andy McFaddene5cc5392011-10-18 20:03:07 -0700347 dump_log_file(tfd, pid, "/dev/log/system", tailOnly);
348 dump_log_file(tfd, pid, "/dev/log/main", tailOnly);
Andy McFadden41e0cef2011-10-13 16:05:08 -0700349}
350
Jeff Brown13e715b2011-10-21 12:14:56 -0700351/*
352 * Dumps all information about the specified pid to the tombstone.
353 */
354static bool dump_crash(int tfd, pid_t pid, pid_t tid, int signal,
355 bool dump_sibling_threads)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356{
Andy McFaddene5cc5392011-10-18 20:03:07 -0700357 /* don't copy log messages to tombstone unless this is a dev device */
358 char value[PROPERTY_VALUE_MAX];
359 property_get("ro.debuggable", value, "0");
360 bool wantLogs = (value[0] == '1');
Jeff Brown13e715b2011-10-21 12:14:56 -0700361
362 dump_crash_banner(tfd, pid, tid, signal);
363
Jeff Brownf0c58722011-11-03 17:58:44 -0700364 ptrace_context_t* context = load_ptrace_context(tid);
Jeff Brown13e715b2011-10-21 12:14:56 -0700365
366 dump_thread(context, tfd, tid, true);
367
368 if (wantLogs) {
369 dump_logs(tfd, pid, true);
370 }
371
Jeff Brown9524e412011-10-24 11:10:16 -0700372 bool detach_failed = false;
Jeff Brown13e715b2011-10-21 12:14:56 -0700373 if (dump_sibling_threads) {
Jeff Brown9524e412011-10-24 11:10:16 -0700374 detach_failed = dump_sibling_thread_report(context, tfd, pid, tid);
Jeff Brown13e715b2011-10-21 12:14:56 -0700375 }
376
377 free_ptrace_context(context);
378
379 if (wantLogs) {
380 dump_logs(tfd, pid, false);
381 }
Jeff Brown9524e412011-10-24 11:10:16 -0700382 return detach_failed;
Jeff Brown13e715b2011-10-21 12:14:56 -0700383}
384
385#define MAX_TOMBSTONES 10
386
387#define typecheck(x,y) { \
388 typeof(x) __dummy1; \
389 typeof(y) __dummy2; \
390 (void)(&__dummy1 == &__dummy2); }
391
392#define TOMBSTONE_DIR "/data/tombstones"
393
394/*
395 * find_and_open_tombstone - find an available tombstone slot, if any, of the
396 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
397 * file is available, we reuse the least-recently-modified file.
Jeff Brownfb9804b2011-11-08 20:17:05 -0800398 *
399 * Returns the path of the tombstone file, allocated using malloc(). Caller must free() it.
Jeff Brown13e715b2011-10-21 12:14:56 -0700400 */
Jeff Brownfb9804b2011-11-08 20:17:05 -0800401static char* find_and_open_tombstone(int* fd)
Jeff Brown13e715b2011-10-21 12:14:56 -0700402{
403 unsigned long mtime = ULONG_MAX;
404 struct stat sb;
Jeff Brown13e715b2011-10-21 12:14:56 -0700405
406 /*
407 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
408 * to, our logic breaks. This check will generate a warning if that happens.
409 */
410 typecheck(mtime, sb.st_mtime);
411
412 /*
413 * In a single wolf-like pass, find an available slot and, in case none
414 * exist, find and record the least-recently-modified file.
415 */
Jeff Brownfb9804b2011-11-08 20:17:05 -0800416 char path[128];
417 int oldest = 0;
418 for (int i = 0; i < MAX_TOMBSTONES; i++) {
Jeff Brown13e715b2011-10-21 12:14:56 -0700419 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
420
421 if (!stat(path, &sb)) {
422 if (sb.st_mtime < mtime) {
423 oldest = i;
424 mtime = sb.st_mtime;
425 }
426 continue;
427 }
428 if (errno != ENOENT)
429 continue;
430
Jeff Brownfb9804b2011-11-08 20:17:05 -0800431 *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
432 if (*fd < 0)
Jeff Brown13e715b2011-10-21 12:14:56 -0700433 continue; /* raced ? */
434
Jeff Brownfb9804b2011-11-08 20:17:05 -0800435 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
436 return strdup(path);
Jeff Brown13e715b2011-10-21 12:14:56 -0700437 }
438
439 /* we didn't find an available file, so we clobber the oldest one */
440 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
Jeff Brownfb9804b2011-11-08 20:17:05 -0800441 *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
442 if (*fd < 0) {
443 LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno));
444 return NULL;
445 }
446 fchown(*fd, AID_SYSTEM, AID_SYSTEM);
447 return strdup(path);
Jeff Brown13e715b2011-10-21 12:14:56 -0700448}
449
450/* Return true if some thread is not detached cleanly */
Jeff Brownfb9804b2011-11-08 20:17:05 -0800451static char* engrave_tombstone(pid_t pid, pid_t tid, int signal, bool dump_sibling_threads,
452 bool* detach_failed)
Jeff Brown13e715b2011-10-21 12:14:56 -0700453{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 mkdir(TOMBSTONE_DIR, 0755);
455 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
456
Jeff Brownfb9804b2011-11-08 20:17:05 -0800457 int fd;
458 char* path = find_and_open_tombstone(&fd);
459 if (!path) {
460 *detach_failed = false;
461 return NULL;
Jeff Brown9524e412011-10-24 11:10:16 -0700462 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463
Jeff Brownfb9804b2011-11-08 20:17:05 -0800464 *detach_failed = dump_crash(fd, pid, tid, signal, dump_sibling_threads);
Andy McFadden41e0cef2011-10-13 16:05:08 -0700465
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 close(fd);
Jeff Brownfb9804b2011-11-08 20:17:05 -0800467 return path;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468}
469
470static int
471write_string(const char* file, const char* string)
472{
473 int len;
474 int fd;
475 ssize_t amt;
476 fd = open(file, O_RDWR);
477 len = strlen(string);
478 if (fd < 0)
479 return -errno;
480 amt = write(fd, string, len);
481 close(fd);
482 return amt >= 0 ? 0 : -errno;
483}
484
485static
486void init_debug_led(void)
487{
488 // trout leds
489 write_string("/sys/class/leds/red/brightness", "0");
490 write_string("/sys/class/leds/green/brightness", "0");
491 write_string("/sys/class/leds/blue/brightness", "0");
492 write_string("/sys/class/leds/red/device/blink", "0");
493 // sardine leds
494 write_string("/sys/class/leds/left/cadence", "0,0");
495}
496
497static
498void enable_debug_led(void)
499{
500 // trout leds
501 write_string("/sys/class/leds/red/brightness", "255");
502 // sardine leds
503 write_string("/sys/class/leds/left/cadence", "1,0");
504}
505
506static
507void disable_debug_led(void)
508{
509 // trout leds
510 write_string("/sys/class/leds/red/brightness", "0");
511 // sardine leds
512 write_string("/sys/class/leds/left/cadence", "0,0");
513}
514
Jeff Brown9524e412011-10-24 11:10:16 -0700515static void wait_for_user_action(pid_t pid) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 /* First log a helpful message */
517 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800518 "* Process %d has been suspended while crashing. To\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700519 "* attach gdbserver for a gdb connection on port 5039\n"
520 "* and start gdbclient:\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800521 "*\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700522 "* gdbclient app_process :5039 %d\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800523 "*\n"
Jeff Brown9524e412011-10-24 11:10:16 -0700524 "* Wait for gdb to start, then press HOME or VOLUME DOWN key\n"
525 "* to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700526 "********************************************************\n",
Jeff Brown9524e412011-10-24 11:10:16 -0700527 pid, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528
Jeff Brown9524e412011-10-24 11:10:16 -0700529 /* wait for HOME or VOLUME DOWN key */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 if (init_getevent() == 0) {
531 int ms = 1200 / 10;
532 int dit = 1;
533 int dah = 3*dit;
534 int _ = -dit;
535 int ___ = 3*_;
536 int _______ = 7*_;
537 const signed char codes[] = {
538 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
539 };
540 size_t s = 0;
541 struct input_event e;
Jeff Brown9524e412011-10-24 11:10:16 -0700542 bool done = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543 init_debug_led();
544 enable_debug_led();
545 do {
546 int timeout = abs((int)(codes[s])) * ms;
547 int res = get_event(&e, timeout);
548 if (res == 0) {
Jeff Brown9524e412011-10-24 11:10:16 -0700549 if (e.type == EV_KEY
550 && (e.code == KEY_HOME || e.code == KEY_VOLUMEDOWN)
551 && e.value == 0) {
552 done = true;
553 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 } else if (res == 1) {
555 if (++s >= sizeof(codes)/sizeof(*codes))
556 s = 0;
557 if (codes[s] > 0) {
558 enable_debug_led();
559 } else {
560 disable_debug_led();
561 }
562 }
Jeff Brown9524e412011-10-24 11:10:16 -0700563 } while (!done);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 uninit_getevent();
565 }
566
567 /* don't forget to turn debug led off */
568 disable_debug_led();
Jeff Brown9524e412011-10-24 11:10:16 -0700569 LOG("debuggerd resuming process %d", pid);
570}
Ben Cheng09e71372009-09-28 11:06:09 -0700571
Jeff Brown9524e412011-10-24 11:10:16 -0700572static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
573 char path[64];
574 snprintf(path, sizeof(path), "/proc/%d/status", tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575
Jeff Brown9524e412011-10-24 11:10:16 -0700576 FILE* fp = fopen(path, "r");
577 if (!fp) {
578 return -1;
579 }
580
581 int fields = 0;
582 char line[1024];
583 while (fgets(line, sizeof(line), fp)) {
584 size_t len = strlen(line);
585 if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
586 *out_pid = atoi(line + 6);
587 fields |= 1;
588 } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
589 *out_uid = atoi(line + 5);
590 fields |= 2;
591 } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
592 *out_gid = atoi(line + 5);
593 fields |= 4;
594 }
595 }
596 fclose(fp);
597 return fields == 7 ? 0 : -1;
598}
599
600static int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
601 const int sleep_time_usec = 200000; /* 0.2 seconds */
602 const int max_total_sleep_usec = 3000000; /* 3 seconds */
603 for (;;) {
604 int status;
605 pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
606 if (n < 0) {
607 if(errno == EAGAIN) continue;
608 LOG("waitpid failed: %s\n", strerror(errno));
609 return -1;
610 } else if (n > 0) {
611 XLOG("waitpid: n=%d status=%08x\n", n, status);
612 if (WIFSTOPPED(status)) {
613 return WSTOPSIG(status);
614 } else {
615 LOG("unexpected waitpid response: n=%d, status=%08x\n", n, status);
616 return -1;
617 }
618 }
619
620 if (*total_sleep_time_usec > max_total_sleep_usec) {
621 LOG("timed out waiting for tid=%d to die\n", tid);
622 return -1;
623 }
624
625 /* not ready yet */
626 XLOG("not ready yet\n");
627 usleep(sleep_time_usec);
628 *total_sleep_time_usec += sleep_time_usec;
629 }
630}
631
632enum {
633 REQUEST_TYPE_CRASH,
634 REQUEST_TYPE_DUMP,
635};
636
637typedef struct {
638 int type;
639 pid_t pid, tid;
640 uid_t uid, gid;
641} request_t;
642
643static int read_request(int fd, request_t* out_request) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644 struct ucred cr;
Jeff Brown9524e412011-10-24 11:10:16 -0700645 int len = sizeof(cr);
646 int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
647 if (status != 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648 LOG("cannot get credentials\n");
Jeff Brown9524e412011-10-24 11:10:16 -0700649 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 }
651
Ben Cheng09e71372009-09-28 11:06:09 -0700652 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 fcntl(fd, F_SETFL, O_NONBLOCK);
Jeff Brown9524e412011-10-24 11:10:16 -0700654
655 struct pollfd pollfds[1];
656 pollfds[0].fd = fd;
657 pollfds[0].events = POLLIN;
658 pollfds[0].revents = 0;
659 status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
660 if (status != 1) {
661 LOG("timed out reading tid\n");
662 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663 }
664
Jeff Brown9524e412011-10-24 11:10:16 -0700665 status = TEMP_FAILURE_RETRY(read(fd, &out_request->tid, sizeof(pid_t)));
666 if (status < 0) {
667 LOG("read failure? %s\n", strerror(errno));
668 return -1;
669 }
670 if (status != sizeof(pid_t)) {
671 LOG("invalid crash request of size %d\n", status);
672 return -1;
673 }
674
675 if (out_request->tid < 0 && cr.uid == 0) {
676 /* Root can ask us to attach to any process and dump it explicitly. */
677 out_request->type = REQUEST_TYPE_DUMP;
678 out_request->tid = -out_request->tid;
679 status = get_process_info(out_request->tid, &out_request->pid,
680 &out_request->uid, &out_request->gid);
681 if (status < 0) {
682 LOG("tid %d does not exist. ignoring explicit dump request\n",
683 out_request->tid);
684 return -1;
685 }
686 return 0;
687 }
688
689 /* Ensure that the tid reported by the crashing process is valid. */
690 out_request->type = REQUEST_TYPE_CRASH;
691 out_request->pid = cr.pid;
692 out_request->uid = cr.uid;
693 out_request->gid = cr.gid;
694
695 char buf[64];
696 struct stat s;
697 snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 if(stat(buf, &s)) {
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800699 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
Jeff Brown9524e412011-10-24 11:10:16 -0700700 out_request->tid, out_request->pid);
701 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 }
Jeff Brown9524e412011-10-24 11:10:16 -0700703 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800704}
705
Jeff Brown9524e412011-10-24 11:10:16 -0700706static bool should_attach_gdb(request_t* request) {
707 if (request->type == REQUEST_TYPE_CRASH) {
708 char value[PROPERTY_VALUE_MAX];
709 property_get("debug.db.uid", value, "-1");
710 int debug_uid = atoi(value);
711 return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
712 }
713 return false;
714}
Bruce Beare84924902010-10-13 14:21:30 -0700715
Jeff Brown9524e412011-10-24 11:10:16 -0700716static void handle_request(int fd) {
717 XLOG("handle_request(%d)\n", fd);
718
719 request_t request;
720 int status = read_request(fd, &request);
721 if (!status) {
722 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", pid, uid, gid, tid);
723
724 /* At this point, the thread that made the request is blocked in
725 * a read() call. If the thread has crashed, then this gives us
726 * time to PTRACE_ATTACH to it before it has a chance to really fault.
727 *
728 * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
729 * won't necessarily have stopped by the time ptrace() returns. (We
730 * currently assume it does.) We write to the file descriptor to
731 * ensure that it can run as soon as we call PTRACE_CONT below.
732 * See details in bionic/libc/linker/debugger.c, in function
733 * debugger_signal_handler().
734 */
735 if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
736 LOG("ptrace attach failed: %s\n", strerror(errno));
737 } else {
738 bool detach_failed = false;
739 bool attach_gdb = should_attach_gdb(&request);
740 char response = 0;
741 if (TEMP_FAILURE_RETRY(write(fd, &response, 1)) != 1) {
742 LOG("failed responding to client: %s\n", strerror(errno));
743 } else {
Jeff Brownfb9804b2011-11-08 20:17:05 -0800744 char* tombstone_path = NULL;
745
746 if (request.type != REQUEST_TYPE_DUMP) {
747 close(fd);
748 fd = -1;
749 }
Jeff Brown9524e412011-10-24 11:10:16 -0700750
751 int total_sleep_time_usec = 0;
752 for (;;) {
753 int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
754 if (signal < 0) {
755 break;
756 }
757
758 switch (signal) {
759 case SIGSTOP:
760 if (request.type == REQUEST_TYPE_DUMP) {
761 XLOG("stopped -- dumping\n");
Jeff Brownfb9804b2011-11-08 20:17:05 -0800762 tombstone_path = engrave_tombstone(request.pid, request.tid,
763 signal, true, &detach_failed);
Jeff Brown9524e412011-10-24 11:10:16 -0700764 } else {
765 XLOG("stopped -- continuing\n");
766 status = ptrace(PTRACE_CONT, request.tid, 0, 0);
767 if (status) {
768 LOG("ptrace continue failed: %s\n", strerror(errno));
769 }
770 continue; /* loop again */
771 }
772 break;
773
774 case SIGILL:
775 case SIGABRT:
776 case SIGBUS:
777 case SIGFPE:
778 case SIGSEGV:
779 case SIGSTKFLT: {
780 XLOG("stopped -- fatal signal\n");
781 /* don't dump sibling threads when attaching to GDB because it
782 * makes the process less reliable, apparently... */
Jeff Brownfb9804b2011-11-08 20:17:05 -0800783 tombstone_path = engrave_tombstone(request.pid, request.tid,
784 signal, !attach_gdb, &detach_failed);
Jeff Brown9524e412011-10-24 11:10:16 -0700785 break;
786 }
787
788 default:
789 XLOG("stopped -- unexpected signal\n");
790 LOG("process stopped due to unexpected signal %d\n", signal);
791 break;
792 }
793 break;
794 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800795
796 if (request.type == REQUEST_TYPE_DUMP) {
797 if (tombstone_path) {
798 write(fd, tombstone_path, strlen(tombstone_path));
799 }
800 close(fd);
801 fd = -1;
802 }
803 free(tombstone_path);
Jeff Brown9524e412011-10-24 11:10:16 -0700804 }
805
806 XLOG("detaching\n");
807 if (attach_gdb) {
808 /* stop the process so we can debug */
809 kill(request.pid, SIGSTOP);
810
811 /* detach so we can attach gdbserver */
812 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
813 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
814 detach_failed = true;
815 }
816
817 /*
818 * if debug.db.uid is set, its value indicates if we should wait
819 * for user action for the crashing process.
820 * in this case, we log a message and turn the debug LED on
821 * waiting for a gdb connection (for instance)
822 */
823 wait_for_user_action(request.pid);
824 } else {
825 /* just detach */
826 if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
827 LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
828 detach_failed = true;
829 }
830 }
831
832 /* resume stopped process (so it can crash in peace). */
833 kill(request.pid, SIGCONT);
834
835 /* If we didn't successfully detach, we're still the parent, and the
836 * actual parent won't receive a death notification via wait(2). At this point
837 * there's not much we can do about that. */
838 if (detach_failed) {
839 LOG("debuggerd committing suicide to free the zombie!\n");
840 kill(getpid(), SIGKILL);
841 }
842 }
843
844 }
845 if (fd >= 0) {
846 close(fd);
847 }
848}
849
850static int do_server() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800851 int s;
852 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700853 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700854
Andy McFadden44e12ec2011-07-29 12:36:47 -0700855 /*
856 * debuggerd crashes can't be reported to debuggerd. Reset all of the
857 * crash handlers.
858 */
859 signal(SIGILL, SIG_DFL);
860 signal(SIGABRT, SIG_DFL);
861 signal(SIGBUS, SIG_DFL);
862 signal(SIGFPE, SIG_DFL);
863 signal(SIGSEGV, SIG_DFL);
864 signal(SIGSTKFLT, SIG_DFL);
865 signal(SIGPIPE, SIG_DFL);
866
Ben Cheng09e71372009-09-28 11:06:09 -0700867 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800868 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
869 if(logsocket < 0) {
870 logsocket = -1;
871 } else {
872 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
873 }
874
875 act.sa_handler = SIG_DFL;
876 sigemptyset(&act.sa_mask);
877 sigaddset(&act.sa_mask,SIGCHLD);
878 act.sa_flags = SA_NOCLDWAIT;
879 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700880
881 s = socket_local_server("android:debuggerd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
Jeff Brown9524e412011-10-24 11:10:16 -0700883 if(s < 0) return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800884 fcntl(s, F_SETFD, FD_CLOEXEC);
885
886 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700887
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800888 for(;;) {
889 struct sockaddr addr;
890 socklen_t alen;
891 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700892
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800893 alen = sizeof(addr);
Andy McFadden655835b2011-07-26 07:50:37 -0700894 XLOG("waiting for connection\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895 fd = accept(s, &addr, &alen);
Andy McFadden655835b2011-07-26 07:50:37 -0700896 if(fd < 0) {
897 XLOG("accept failed: %s\n", strerror(errno));
898 continue;
899 }
Ben Cheng09e71372009-09-28 11:06:09 -0700900
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800901 fcntl(fd, F_SETFD, FD_CLOEXEC);
902
Jeff Brown9524e412011-10-24 11:10:16 -0700903 handle_request(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904 }
905 return 0;
906}
Jeff Brown9524e412011-10-24 11:10:16 -0700907
908static int do_explicit_dump(pid_t tid) {
909 fprintf(stdout, "Sending request to dump task %d.\n", tid);
910
911 int fd = socket_local_client("android:debuggerd",
912 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
913 if (fd < 0) {
914 fputs("Error opening local socket to debuggerd.\n", stderr);
915 return 1;
916 }
917
918 pid_t request = -tid;
919 write(fd, &request, sizeof(pid_t));
920 if (read(fd, &request, 1) != 1) {
921 /* did not get expected reply, debuggerd must have closed the socket */
922 fputs("Error sending request. Did not receive reply from debuggerd.\n", stderr);
Jeff Brownfb9804b2011-11-08 20:17:05 -0800923 } else {
924 char tombstone_path[PATH_MAX];
925 ssize_t n = read(fd, &tombstone_path, sizeof(tombstone_path) - 1);
926 if (n <= 0) {
927 fputs("Error dumping process. Check log for details.\n", stderr);
928 } else {
929 tombstone_path[n] = '\0';
930 fprintf(stderr, "Tombstone written to: %s\n", tombstone_path);
931 }
Jeff Brown9524e412011-10-24 11:10:16 -0700932 }
Jeff Brownfb9804b2011-11-08 20:17:05 -0800933
Jeff Brown9524e412011-10-24 11:10:16 -0700934 close(fd);
935 return 0;
936}
937
938int main(int argc, char** argv) {
939 if (argc == 2) {
940 pid_t tid = atoi(argv[1]);
941 if (!tid) {
942 fputs("Usage: [<tid>]\n"
943 "\n"
944 "If tid specified, sends a request to debuggerd to dump that task.\n"
945 "Otherwise, starts the debuggerd server.\n", stderr);
946 return 1;
947 }
948 return do_explicit_dump(tid);
949 }
950 return do_server();
951}