blob: 15994392ffc6194b22d6823884ab8b2a5b85bfa5 [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>
31
32#include <cutils/sockets.h>
33#include <cutils/logd.h>
Andy McFadden41e0cef2011-10-13 16:05:08 -070034#include <cutils/logger.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <cutils/properties.h>
36
Jeff Brown13e715b2011-10-21 12:14:56 -070037#include <corkscrew/backtrace.h>
38
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#include <linux/input.h>
40
41#include <private/android_filesystem_config.h>
42
Jeff Brown13e715b2011-10-21 12:14:56 -070043#include "getevent.h"
44#include "machine.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045#include "utility.h"
46
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#define ANDROID_LOG_INFO 4
48
Jeff Brown13e715b2011-10-21 12:14:56 -070049static void dump_build_info(int tfd)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050{
51 char fingerprint[PROPERTY_VALUE_MAX];
52
53 property_get("ro.build.fingerprint", fingerprint, "unknown");
54
55 _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint);
56}
57
Jeff Brown13e715b2011-10-21 12:14:56 -070058static const char *get_signame(int sig)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059{
60 switch(sig) {
61 case SIGILL: return "SIGILL";
62 case SIGABRT: return "SIGABRT";
63 case SIGBUS: return "SIGBUS";
64 case SIGFPE: return "SIGFPE";
65 case SIGSEGV: return "SIGSEGV";
66 case SIGSTKFLT: return "SIGSTKFLT";
67 default: return "?";
68 }
69}
70
Jeff Brown13e715b2011-10-21 12:14:56 -070071static const char *get_sigcode(int signo, int code)
Carl Shapiro83c6b052010-10-08 18:10:24 -070072{
73 switch (signo) {
74 case SIGILL:
75 switch (code) {
76 case ILL_ILLOPC: return "ILL_ILLOPC";
77 case ILL_ILLOPN: return "ILL_ILLOPN";
78 case ILL_ILLADR: return "ILL_ILLADR";
79 case ILL_ILLTRP: return "ILL_ILLTRP";
80 case ILL_PRVOPC: return "ILL_PRVOPC";
81 case ILL_PRVREG: return "ILL_PRVREG";
82 case ILL_COPROC: return "ILL_COPROC";
83 case ILL_BADSTK: return "ILL_BADSTK";
84 }
85 break;
86 case SIGBUS:
87 switch (code) {
88 case BUS_ADRALN: return "BUS_ADRALN";
89 case BUS_ADRERR: return "BUS_ADRERR";
90 case BUS_OBJERR: return "BUS_OBJERR";
91 }
92 break;
93 case SIGFPE:
94 switch (code) {
95 case FPE_INTDIV: return "FPE_INTDIV";
96 case FPE_INTOVF: return "FPE_INTOVF";
97 case FPE_FLTDIV: return "FPE_FLTDIV";
98 case FPE_FLTOVF: return "FPE_FLTOVF";
99 case FPE_FLTUND: return "FPE_FLTUND";
100 case FPE_FLTRES: return "FPE_FLTRES";
101 case FPE_FLTINV: return "FPE_FLTINV";
102 case FPE_FLTSUB: return "FPE_FLTSUB";
103 }
104 break;
105 case SIGSEGV:
106 switch (code) {
107 case SEGV_MAPERR: return "SEGV_MAPERR";
108 case SEGV_ACCERR: return "SEGV_ACCERR";
109 }
110 break;
111 }
112 return "?";
113}
114
Jeff Brown13e715b2011-10-21 12:14:56 -0700115static void dump_fault_addr(int tfd, pid_t pid, int sig)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116{
117 siginfo_t si;
Ben Cheng09e71372009-09-28 11:06:09 -0700118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 memset(&si, 0, sizeof(si));
120 if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){
121 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
Andy McFadden136dcc52011-09-22 16:37:06 -0700122 } else if (signal_has_address(sig)) {
Carl Shapiro83c6b052010-10-08 18:10:24 -0700123 _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr %08x\n",
124 sig, get_signame(sig),
125 si.si_code, get_sigcode(sig, si.si_code),
Andy McFaddene5cc5392011-10-18 20:03:07 -0700126 (uintptr_t) si.si_addr);
Andy McFadden136dcc52011-09-22 16:37:06 -0700127 } else {
128 _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr --------\n",
129 sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 }
131}
132
Jeff Brown13e715b2011-10-21 12:14:56 -0700133static void dump_crash_banner(int tfd, pid_t pid, pid_t tid, int sig)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134{
135 char data[1024];
136 char *x = 0;
137 FILE *fp;
Ben Cheng09e71372009-09-28 11:06:09 -0700138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 sprintf(data, "/proc/%d/cmdline", pid);
140 fp = fopen(data, "r");
141 if(fp) {
142 x = fgets(data, 1024, fp);
143 fclose(fp);
144 }
Ben Cheng09e71372009-09-28 11:06:09 -0700145
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 _LOG(tfd, false,
Jeff Brown13e715b2011-10-21 12:14:56 -0700147 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 dump_build_info(tfd);
149 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n",
150 pid, tid, x ? x : "UNKNOWN");
Ben Cheng09e71372009-09-28 11:06:09 -0700151
Jeff Brown13e715b2011-10-21 12:14:56 -0700152 if(sig) {
153 dump_fault_addr(tfd, tid, sig);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 }
155}
156
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157/* Return true if some thread is not detached cleanly */
Jeff Brown13e715b2011-10-21 12:14:56 -0700158static bool dump_sibling_thread_report(ptrace_context_t* context,
159 int tfd, pid_t pid, pid_t tid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160{
161 char task_path[1024];
162
163 sprintf(task_path, "/proc/%d/task", pid);
164 DIR *d;
165 struct dirent *de;
166 int need_cleanup = 0;
167
168 d = opendir(task_path);
169 /* Bail early if cannot open the task directory */
170 if (d == NULL) {
171 XLOG("Cannot open /proc/%d/task\n", pid);
172 return false;
173 }
174 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 ".." */
Ben Cheng09e71372009-09-28 11:06:09 -0700177 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 continue;
179 new_tid = atoi(de->d_name);
180 /* The main thread at fault has been handled individually */
181 if (new_tid == tid)
182 continue;
183
184 /* Skip this thread if cannot ptrace it */
185 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0)
186 continue;
187
Jeff Brown13e715b2011-10-21 12:14:56 -0700188 _LOG(tfd, true,
189 "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
190 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, new_tid);
191
192 dump_thread(context, tfd, new_tid, false);
Andy McFaddene5cc5392011-10-18 20:03:07 -0700193
194 if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
195 XLOG("detach of tid %d failed: %s\n", new_tid, strerror(errno));
196 need_cleanup = 1;
197 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 }
199 closedir(d);
Andy McFaddene5cc5392011-10-18 20:03:07 -0700200
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 return need_cleanup != 0;
202}
203
Andy McFadden41e0cef2011-10-13 16:05:08 -0700204/*
205 * Reads the contents of the specified log device, filters out the entries
206 * that don't match the specified pid, and writes them to the tombstone file.
Andy McFaddene5cc5392011-10-18 20:03:07 -0700207 *
208 * If "tailOnly" is set, we only print the last few lines.
Andy McFadden41e0cef2011-10-13 16:05:08 -0700209 */
Jeff Brown13e715b2011-10-21 12:14:56 -0700210static void dump_log_file(int tfd, pid_t pid, const char* filename,
Andy McFaddene5cc5392011-10-18 20:03:07 -0700211 bool tailOnly)
Andy McFadden41e0cef2011-10-13 16:05:08 -0700212{
Andy McFaddene5cc5392011-10-18 20:03:07 -0700213 bool first = true;
214
215 /* circular buffer, for "tailOnly" mode */
216 const int kShortLogMaxLines = 5;
217 const int kShortLogLineLen = 256;
218 char shortLog[kShortLogMaxLines][kShortLogLineLen];
219 int shortLogCount = 0;
220 int shortLogNext = 0;
221
Andy McFadden41e0cef2011-10-13 16:05:08 -0700222 int logfd = open(filename, O_RDONLY | O_NONBLOCK);
223 if (logfd < 0) {
224 XLOG("Unable to open %s: %s\n", filename, strerror(errno));
225 return;
226 }
Andy McFadden41e0cef2011-10-13 16:05:08 -0700227
228 union {
229 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
230 struct logger_entry entry;
231 } log_entry;
232
233 while (true) {
234 ssize_t actual = read(logfd, log_entry.buf, LOGGER_ENTRY_MAX_LEN);
235 if (actual < 0) {
236 if (errno == EINTR) {
237 /* interrupted by signal, retry */
238 continue;
239 } else if (errno == EAGAIN) {
240 /* non-blocking EOF; we're done */
241 break;
242 } else {
243 _LOG(tfd, true, "Error while reading log: %s\n",
244 strerror(errno));
245 break;
246 }
247 } else if (actual == 0) {
248 _LOG(tfd, true, "Got zero bytes while reading log: %s\n",
249 strerror(errno));
250 break;
251 }
252
253 /*
254 * NOTE: if you XLOG something here, this will spin forever,
255 * because you will be writing as fast as you're reading. Any
256 * high-frequency debug diagnostics should just be written to
257 * the tombstone file.
258 */
259
260 struct logger_entry* entry = &log_entry.entry;
261
262 if (entry->pid != (int32_t) pid) {
263 /* wrong pid, ignore */
264 continue;
265 }
266
Andy McFaddene5cc5392011-10-18 20:03:07 -0700267 if (first) {
268 _LOG(tfd, true, "--------- %slog %s\n",
269 tailOnly ? "tail end of " : "", filename);
270 first = false;
271 }
272
Andy McFadden41e0cef2011-10-13 16:05:08 -0700273 /*
274 * Msg format is: <priority:1><tag:N>\0<message:N>\0
275 *
276 * We want to display it in the same format as "logcat -v threadtime"
277 * (although in this case the pid is redundant).
278 *
279 * TODO: scan for line breaks ('\n') and display each text line
280 * on a separate line, prefixed with the header, like logcat does.
281 */
282 static const char* kPrioChars = "!.VDIWEFS";
283 unsigned char prio = entry->msg[0];
Andy McFaddene5cc5392011-10-18 20:03:07 -0700284 char* tag = entry->msg + 1;
285 char* msg = tag + strlen(tag) + 1;
Andy McFadden41e0cef2011-10-13 16:05:08 -0700286
Andy McFaddene5cc5392011-10-18 20:03:07 -0700287 /* consume any trailing newlines */
288 char* eatnl = msg + strlen(msg) - 1;
289 while (eatnl >= msg && *eatnl == '\n') {
290 *eatnl-- = '\0';
291 }
292
293 char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?');
Andy McFadden41e0cef2011-10-13 16:05:08 -0700294
295 char timeBuf[32];
296 time_t sec = (time_t) entry->sec;
297 struct tm tmBuf;
298 struct tm* ptm;
299 ptm = localtime_r(&sec, &tmBuf);
300 strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);
301
Andy McFaddene5cc5392011-10-18 20:03:07 -0700302 if (tailOnly) {
303 snprintf(shortLog[shortLogNext], kShortLogLineLen,
304 "%s.%03d %5d %5d %c %-8s: %s",
305 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
306 prioChar, tag, msg);
307 shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
308 shortLogCount++;
309 } else {
310 _LOG(tfd, true, "%s.%03d %5d %5d %c %-8s: %s\n",
311 timeBuf, entry->nsec / 1000000, entry->pid, entry->tid,
312 prioChar, tag, msg);
313 }
314 }
315
316 if (tailOnly) {
317 int i;
318
319 /*
320 * If we filled the buffer, we want to start at "next", which has
321 * the oldest entry. If we didn't, we want to start at zero.
322 */
323 if (shortLogCount < kShortLogMaxLines) {
324 shortLogNext = 0;
325 } else {
326 shortLogCount = kShortLogMaxLines; /* cap at window size */
327 }
328
329 for (i = 0; i < shortLogCount; i++) {
330 _LOG(tfd, true, "%s\n", shortLog[shortLogNext]);
331 shortLogNext = (shortLogNext + 1) % kShortLogMaxLines;
332 }
Andy McFadden41e0cef2011-10-13 16:05:08 -0700333 }
334
335 close(logfd);
336}
337
338/*
339 * Dumps the logs generated by the specified pid to the tombstone, from both
340 * "system" and "main" log devices. Ideally we'd interleave the output.
341 */
Jeff Brown13e715b2011-10-21 12:14:56 -0700342static void dump_logs(int tfd, pid_t pid, bool tailOnly)
Andy McFadden41e0cef2011-10-13 16:05:08 -0700343{
Andy McFaddene5cc5392011-10-18 20:03:07 -0700344 dump_log_file(tfd, pid, "/dev/log/system", tailOnly);
345 dump_log_file(tfd, pid, "/dev/log/main", tailOnly);
Andy McFadden41e0cef2011-10-13 16:05:08 -0700346}
347
Jeff Brown13e715b2011-10-21 12:14:56 -0700348/*
349 * Dumps all information about the specified pid to the tombstone.
350 */
351static bool dump_crash(int tfd, pid_t pid, pid_t tid, int signal,
352 bool dump_sibling_threads)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353{
Andy McFaddene5cc5392011-10-18 20:03:07 -0700354 /* don't copy log messages to tombstone unless this is a dev device */
355 char value[PROPERTY_VALUE_MAX];
356 property_get("ro.debuggable", value, "0");
357 bool wantLogs = (value[0] == '1');
Jeff Brown13e715b2011-10-21 12:14:56 -0700358 bool need_cleanup = false;
359
360 dump_crash_banner(tfd, pid, tid, signal);
361
362 ptrace_context_t* context = load_ptrace_context(pid);
363
364 dump_thread(context, tfd, tid, true);
365
366 if (wantLogs) {
367 dump_logs(tfd, pid, true);
368 }
369
370 if (dump_sibling_threads) {
371 need_cleanup = dump_sibling_thread_report(context, tfd, pid, tid);
372 }
373
374 free_ptrace_context(context);
375
376 if (wantLogs) {
377 dump_logs(tfd, pid, false);
378 }
379 return need_cleanup;
380}
381
382#define MAX_TOMBSTONES 10
383
384#define typecheck(x,y) { \
385 typeof(x) __dummy1; \
386 typeof(y) __dummy2; \
387 (void)(&__dummy1 == &__dummy2); }
388
389#define TOMBSTONE_DIR "/data/tombstones"
390
391/*
392 * find_and_open_tombstone - find an available tombstone slot, if any, of the
393 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
394 * file is available, we reuse the least-recently-modified file.
395 */
396static int find_and_open_tombstone(void)
397{
398 unsigned long mtime = ULONG_MAX;
399 struct stat sb;
400 char path[128];
401 int fd, i, oldest = 0;
402
403 /*
404 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
405 * to, our logic breaks. This check will generate a warning if that happens.
406 */
407 typecheck(mtime, sb.st_mtime);
408
409 /*
410 * In a single wolf-like pass, find an available slot and, in case none
411 * exist, find and record the least-recently-modified file.
412 */
413 for (i = 0; i < MAX_TOMBSTONES; i++) {
414 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
415
416 if (!stat(path, &sb)) {
417 if (sb.st_mtime < mtime) {
418 oldest = i;
419 mtime = sb.st_mtime;
420 }
421 continue;
422 }
423 if (errno != ENOENT)
424 continue;
425
426 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
427 if (fd < 0)
428 continue; /* raced ? */
429
430 fchown(fd, AID_SYSTEM, AID_SYSTEM);
431 return fd;
432 }
433
434 /* we didn't find an available file, so we clobber the oldest one */
435 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
436 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
437 fchown(fd, AID_SYSTEM, AID_SYSTEM);
438
439 return fd;
440}
441
442/* Return true if some thread is not detached cleanly */
443static bool engrave_tombstone(pid_t pid, pid_t tid, int signal,
444 bool dump_sibling_threads)
445{
446 int fd;
447 bool need_cleanup = false;
Andy McFaddene5cc5392011-10-18 20:03:07 -0700448
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 mkdir(TOMBSTONE_DIR, 0755);
450 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
451
452 fd = find_and_open_tombstone();
453 if (fd < 0)
454 return need_cleanup;
455
Jeff Brown13e715b2011-10-21 12:14:56 -0700456 need_cleanup = dump_crash(fd, pid, tid, signal, dump_sibling_threads);
Andy McFadden41e0cef2011-10-13 16:05:08 -0700457
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 close(fd);
459 return need_cleanup;
460}
461
462static int
463write_string(const char* file, const char* string)
464{
465 int len;
466 int fd;
467 ssize_t amt;
468 fd = open(file, O_RDWR);
469 len = strlen(string);
470 if (fd < 0)
471 return -errno;
472 amt = write(fd, string, len);
473 close(fd);
474 return amt >= 0 ? 0 : -errno;
475}
476
477static
478void init_debug_led(void)
479{
480 // trout leds
481 write_string("/sys/class/leds/red/brightness", "0");
482 write_string("/sys/class/leds/green/brightness", "0");
483 write_string("/sys/class/leds/blue/brightness", "0");
484 write_string("/sys/class/leds/red/device/blink", "0");
485 // sardine leds
486 write_string("/sys/class/leds/left/cadence", "0,0");
487}
488
489static
490void enable_debug_led(void)
491{
492 // trout leds
493 write_string("/sys/class/leds/red/brightness", "255");
494 // sardine leds
495 write_string("/sys/class/leds/left/cadence", "1,0");
496}
497
498static
499void disable_debug_led(void)
500{
501 // trout leds
502 write_string("/sys/class/leds/red/brightness", "0");
503 // sardine leds
504 write_string("/sys/class/leds/left/cadence", "0,0");
505}
506
Jeff Brown13e715b2011-10-21 12:14:56 -0700507static void wait_for_user_action(pid_t tid, struct ucred* cr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508{
509 (void)tid;
510 /* First log a helpful message */
511 LOG( "********************************************************\n"
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800512 "* Process %d has been suspended while crashing. To\n"
513 "* attach gdbserver for a gdb connection on port 5039:\n"
514 "*\n"
515 "* adb shell gdbserver :5039 --attach %d &\n"
516 "*\n"
517 "* Press HOME key to let the process continue crashing.\n"
Ben Cheng09e71372009-09-28 11:06:09 -0700518 "********************************************************\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 cr->pid, cr->pid);
520
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800521 /* wait for HOME key (TODO: something useful for devices w/o HOME key) */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 if (init_getevent() == 0) {
523 int ms = 1200 / 10;
524 int dit = 1;
525 int dah = 3*dit;
526 int _ = -dit;
527 int ___ = 3*_;
528 int _______ = 7*_;
529 const signed char codes[] = {
530 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
531 };
532 size_t s = 0;
533 struct input_event e;
534 int home = 0;
535 init_debug_led();
536 enable_debug_led();
537 do {
538 int timeout = abs((int)(codes[s])) * ms;
539 int res = get_event(&e, timeout);
540 if (res == 0) {
541 if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0)
542 home = 1;
543 } else if (res == 1) {
544 if (++s >= sizeof(codes)/sizeof(*codes))
545 s = 0;
546 if (codes[s] > 0) {
547 enable_debug_led();
548 } else {
549 disable_debug_led();
550 }
551 }
Ben Cheng09e71372009-09-28 11:06:09 -0700552 } while (!home);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 uninit_getevent();
554 }
555
556 /* don't forget to turn debug led off */
557 disable_debug_led();
Ben Cheng09e71372009-09-28 11:06:09 -0700558
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559 /* close filedescriptor */
560 LOG("debuggerd resuming process %d", cr->pid);
561 }
562
563static void handle_crashing_process(int fd)
564{
565 char buf[64];
566 struct stat s;
Jeff Brown13e715b2011-10-21 12:14:56 -0700567 pid_t tid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 struct ucred cr;
Ben Cheng09e71372009-09-28 11:06:09 -0700569 int n, len, status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800570 int tid_attach_status = -1;
571 unsigned retry = 30;
572 bool need_cleanup = false;
573
Jeff Brown13e715b2011-10-21 12:14:56 -0700574 XLOG("handle_crashing_process(%d)\n", fd);
575
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576 char value[PROPERTY_VALUE_MAX];
577 property_get("debug.db.uid", value, "-1");
578 int debug_uid = atoi(value);
Ben Cheng09e71372009-09-28 11:06:09 -0700579
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800580 len = sizeof(cr);
581 n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
582 if(n != 0) {
583 LOG("cannot get credentials\n");
584 goto done;
585 }
586
Ben Cheng09e71372009-09-28 11:06:09 -0700587 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588 fcntl(fd, F_SETFL, O_NONBLOCK);
Jeff Brown13e715b2011-10-21 12:14:56 -0700589 while((n = read(fd, &tid, sizeof(pid_t))) != sizeof(pid_t)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 if(errno == EINTR) continue;
591 if(errno == EWOULDBLOCK) {
592 if(retry-- > 0) {
593 usleep(100 * 1000);
594 continue;
595 }
596 LOG("timed out reading tid\n");
597 goto done;
598 }
599 LOG("read failure? %s\n", strerror(errno));
600 goto done;
601 }
602
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100603 snprintf(buf, sizeof buf, "/proc/%d/task/%d", cr.pid, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 if(stat(buf, &s)) {
Andy McFadden3bfdcc92009-12-01 12:37:26 -0800605 LOG("tid %d does not exist in pid %d. ignoring debug request\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 tid, cr.pid);
607 close(fd);
608 return;
609 }
Ben Cheng09e71372009-09-28 11:06:09 -0700610
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
612
Jeff Brown13e715b2011-10-21 12:14:56 -0700613 /*
614 * If the user has requested to attach gdb, don't collect the per-thread
615 * information as it increases the chance to lose track of the process.
616 */
617 bool dump_sibling_threads = (signed)cr.pid > debug_uid;
618
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100619 /* Note that at this point, the target thread's signal handler
620 * is blocked in a read() call. This gives us the time to PTRACE_ATTACH
621 * to it before it has a chance to really fault.
622 *
Andy McFaddene5cc5392011-10-18 20:03:07 -0700623 * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
624 * won't necessarily have stopped by the time ptrace() returns. (We
625 * currently assume it does.) We write to the file descriptor to
626 * ensure that it can run as soon as we call PTRACE_CONT below.
627 * See details in bionic/libc/linker/debugger.c, in function
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100628 * debugger_signal_handler().
629 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
Andy McFadden655835b2011-07-26 07:50:37 -0700631 int ptrace_error = errno;
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100632
Andy McFadden655835b2011-07-26 07:50:37 -0700633 if (TEMP_FAILURE_RETRY(write(fd, &tid, 1)) != 1) {
634 XLOG("failed responding to client: %s\n",
635 strerror(errno));
636 goto done;
637 }
David 'Digit' Turner02526d42011-01-21 04:27:12 +0100638
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 if(tid_attach_status < 0) {
Andy McFadden655835b2011-07-26 07:50:37 -0700640 LOG("ptrace attach failed: %s\n", strerror(ptrace_error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641 goto done;
642 }
643
644 close(fd);
645 fd = -1;
646
Andy McFadden655835b2011-07-26 07:50:37 -0700647 const int sleep_time_usec = 200000; /* 0.2 seconds */
648 const int max_total_sleep_usec = 3000000; /* 3 seconds */
649 int loop_limit = max_total_sleep_usec / sleep_time_usec;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 for(;;) {
Andy McFadden655835b2011-07-26 07:50:37 -0700651 if (loop_limit-- == 0) {
652 LOG("timed out waiting for pid=%d tid=%d uid=%d to die\n",
653 cr.pid, tid, cr.uid);
654 goto done;
655 }
656 n = waitpid(tid, &status, __WALL | WNOHANG);
657
658 if (n == 0) {
659 /* not ready yet */
660 XLOG("not ready yet\n");
661 usleep(sleep_time_usec);
662 continue;
663 }
Ben Cheng09e71372009-09-28 11:06:09 -0700664
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 if(n < 0) {
666 if(errno == EAGAIN) continue;
667 LOG("waitpid failed: %s\n", strerror(errno));
668 goto done;
669 }
670
671 XLOG("waitpid: n=%d status=%08x\n", n, status);
672
673 if(WIFSTOPPED(status)){
674 n = WSTOPSIG(status);
675 switch(n) {
676 case SIGSTOP:
677 XLOG("stopped -- continuing\n");
678 n = ptrace(PTRACE_CONT, tid, 0, 0);
679 if(n) {
680 LOG("ptrace failed: %s\n", strerror(errno));
681 goto done;
682 }
683 continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700684
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 case SIGILL:
686 case SIGABRT:
687 case SIGBUS:
688 case SIGFPE:
689 case SIGSEGV:
690 case SIGSTKFLT: {
691 XLOG("stopped -- fatal signal\n");
Jeff Brown13e715b2011-10-21 12:14:56 -0700692 need_cleanup = engrave_tombstone(cr.pid, tid, n,
693 dump_sibling_threads);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 kill(tid, SIGSTOP);
695 goto done;
696 }
697
698 default:
699 XLOG("stopped -- unexpected signal\n");
700 goto done;
701 }
702 } else {
703 XLOG("unexpected waitpid response\n");
704 goto done;
705 }
706 }
Ben Cheng09e71372009-09-28 11:06:09 -0700707
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708done:
709 XLOG("detaching\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700710
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 /* stop the process so we can debug */
712 kill(cr.pid, SIGSTOP);
713
Ben Cheng09e71372009-09-28 11:06:09 -0700714 /*
715 * If a thread has been attached by ptrace, make sure it is detached
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716 * successfully otherwise we will get a zombie.
Ben Cheng09e71372009-09-28 11:06:09 -0700717 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 if (tid_attach_status == 0) {
719 int detach_status;
720 /* detach so we can attach gdbserver */
721 detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
722 need_cleanup |= (detach_status != 0);
723 }
724
725 /*
726 * if debug.db.uid is set, its value indicates if we should wait
727 * for user action for the crashing process.
728 * in this case, we log a message and turn the debug LED on
729 * waiting for a gdb connection (for instance)
730 */
731
732 if ((signed)cr.uid <= debug_uid) {
733 wait_for_user_action(tid, &cr);
734 }
735
Andy McFaddene5cc5392011-10-18 20:03:07 -0700736 /*
737 * Resume stopped process (so it can crash in peace). If we didn't
738 * successfully detach, we're still the parent, and the actual parent
739 * won't receive a death notification via wait(2). At this point
740 * there's not much we can do about that.
741 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742 kill(cr.pid, SIGCONT);
743
744 if (need_cleanup) {
745 LOG("debuggerd committing suicide to free the zombie!\n");
746 kill(getpid(), SIGKILL);
747 }
748
749 if(fd != -1) close(fd);
750}
751
Bruce Beare84924902010-10-13 14:21:30 -0700752
Ben Cheng09e71372009-09-28 11:06:09 -0700753int main()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754{
755 int s;
756 struct sigaction act;
Bruce Beare84924902010-10-13 14:21:30 -0700757 int logsocket = -1;
Ben Cheng09e71372009-09-28 11:06:09 -0700758
Andy McFadden44e12ec2011-07-29 12:36:47 -0700759 /*
760 * debuggerd crashes can't be reported to debuggerd. Reset all of the
761 * crash handlers.
762 */
763 signal(SIGILL, SIG_DFL);
764 signal(SIGABRT, SIG_DFL);
765 signal(SIGBUS, SIG_DFL);
766 signal(SIGFPE, SIG_DFL);
767 signal(SIGSEGV, SIG_DFL);
768 signal(SIGSTKFLT, SIG_DFL);
769 signal(SIGPIPE, SIG_DFL);
770
Ben Cheng09e71372009-09-28 11:06:09 -0700771 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
773 if(logsocket < 0) {
774 logsocket = -1;
775 } else {
776 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
777 }
778
779 act.sa_handler = SIG_DFL;
780 sigemptyset(&act.sa_mask);
781 sigaddset(&act.sa_mask,SIGCHLD);
782 act.sa_flags = SA_NOCLDWAIT;
783 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700784
785 s = socket_local_server("android:debuggerd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
787 if(s < 0) return -1;
788 fcntl(s, F_SETFD, FD_CLOEXEC);
789
790 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700791
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 for(;;) {
793 struct sockaddr addr;
794 socklen_t alen;
795 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700796
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797 alen = sizeof(addr);
Andy McFadden655835b2011-07-26 07:50:37 -0700798 XLOG("waiting for connection\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800799 fd = accept(s, &addr, &alen);
Andy McFadden655835b2011-07-26 07:50:37 -0700800 if(fd < 0) {
801 XLOG("accept failed: %s\n", strerror(errno));
802 continue;
803 }
Ben Cheng09e71372009-09-28 11:06:09 -0700804
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 fcntl(fd, F_SETFD, FD_CLOEXEC);
806
807 handle_crashing_process(fd);
808 }
809 return 0;
810}