blob: 02bab2c6394a2a28ed1bc89d4064abd516dd0c82 [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>
19#include <stdlib.h>
20#include <unistd.h>
21#include <errno.h>
22#include <signal.h>
23#include <pthread.h>
24#include <stdarg.h>
25#include <fcntl.h>
26#include <sys/types.h>
27#include <dirent.h>
28
29#include <sys/ptrace.h>
30#include <sys/wait.h>
31#include <sys/exec_elf.h>
32#include <sys/stat.h>
33
34#include <cutils/sockets.h>
35#include <cutils/logd.h>
36#include <cutils/sockets.h>
37#include <cutils/properties.h>
38
39#include <linux/input.h>
40
41#include <private/android_filesystem_config.h>
42
43#include "utility.h"
44
45/* Main entry point to get the backtrace from the crashing process */
46extern int unwind_backtrace_with_ptrace(int tfd, pid_t pid, mapinfo *map,
47 unsigned int sp_list[],
48 int *frame0_pc_sane,
49 bool at_fault);
50
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051static int logsocket = -1;
52
53#define ANDROID_LOG_INFO 4
54
55/* Log information onto the tombstone */
56void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...)
57{
Meng Huae7b91b2009-11-05 16:10:50 -060058 char buf[512];
Ben Cheng09e71372009-09-28 11:06:09 -070059
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060 va_list ap;
61 va_start(ap, fmt);
62
63 if (tfd >= 0) {
64 int len;
65 vsnprintf(buf, sizeof(buf), fmt, ap);
66 len = strlen(buf);
67 if(tfd >= 0) write(tfd, buf, len);
68 }
69
70 if (!in_tombstone_only)
71 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
72}
73
74#define LOG(fmt...) _LOG(-1, 0, fmt)
75#if 0
76#define XLOG(fmt...) _LOG(-1, 0, fmt)
77#else
78#define XLOG(fmt...) do {} while(0)
79#endif
80
81// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
82// 012345678901234567890123456789012345678901234567890123456789
83// 0 1 2 3 4 5
84
85mapinfo *parse_maps_line(char *line)
86{
87 mapinfo *mi;
88 int len = strlen(line);
89
90 if(len < 1) return 0;
91 line[--len] = 0;
Ben Cheng09e71372009-09-28 11:06:09 -070092
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 if(len < 50) return 0;
94 if(line[20] != 'x') return 0;
95
96 mi = malloc(sizeof(mapinfo) + (len - 47));
97 if(mi == 0) return 0;
Ben Cheng09e71372009-09-28 11:06:09 -070098
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 mi->start = strtoul(line, 0, 16);
100 mi->end = strtoul(line + 9, 0, 16);
Meng Huae7b91b2009-11-05 16:10:50 -0600101 /* To be filled in parse_elf_info if the mapped section starts with
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 * elf_header
103 */
104 mi->exidx_start = mi->exidx_end = 0;
Meng Huae7b91b2009-11-05 16:10:50 -0600105 mi->symbols = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 mi->next = 0;
107 strcpy(mi->name, line + 49);
108
109 return mi;
110}
111
112void dump_build_info(int tfd)
113{
114 char fingerprint[PROPERTY_VALUE_MAX];
115
116 property_get("ro.build.fingerprint", fingerprint, "unknown");
117
118 _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint);
119}
120
121
Ben Cheng09e71372009-09-28 11:06:09 -0700122void dump_stack_and_code(int tfd, int pid, mapinfo *map,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 int unwind_depth, unsigned int sp_list[],
124 int frame0_pc_sane, bool at_fault)
125{
126 unsigned int sp, pc, p, end, data;
127 struct pt_regs r;
128 int sp_depth;
129 bool only_in_tombstone = !at_fault;
Ben Cheng09e71372009-09-28 11:06:09 -0700130 char code_buffer[80];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131
132 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return;
133 sp = r.ARM_sp;
134 pc = r.ARM_pc;
135
136 /* Died because calling the weeds - dump
137 * the code around the PC in the next frame instead.
138 */
139 if (frame0_pc_sane == 0) {
140 pc = r.ARM_lr;
141 }
142
Ben Cheng09e71372009-09-28 11:06:09 -0700143 _LOG(tfd, only_in_tombstone,
144 "\ncode around %s:\n", frame0_pc_sane ? "pc" : "lr");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145
146 end = p = pc & ~3;
147 p -= 16;
Ben Cheng09e71372009-09-28 11:06:09 -0700148 end += 16;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
Ben Cheng09e71372009-09-28 11:06:09 -0700150 /* Dump the code around PC as:
151 * addr contents
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 * 00008d34 fffffcd0 4c0eb530 b0934a0e 1c05447c
153 * 00008d44 f7ff18a0 490ced94 68035860 d0012b00
154 */
155 while (p <= end) {
156 int i;
157
Ben Cheng09e71372009-09-28 11:06:09 -0700158 sprintf(code_buffer, "%08x ", p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 for (i = 0; i < 4; i++) {
160 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
Ben Cheng09e71372009-09-28 11:06:09 -0700161 sprintf(code_buffer + strlen(code_buffer), "%08x ", data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 p += 4;
163 }
Ben Cheng09e71372009-09-28 11:06:09 -0700164 _LOG(tfd, only_in_tombstone, "%s\n", code_buffer);
165 }
166
167 if (frame0_pc_sane) {
168 _LOG(tfd, only_in_tombstone, "\ncode around lr:\n");
169
170 end = p = r.ARM_lr & ~3;
171 p -= 16;
172 end += 16;
173
174 /* Dump the code around LR as:
175 * addr contents
176 * 00008d34 fffffcd0 4c0eb530 b0934a0e 1c05447c
177 * 00008d44 f7ff18a0 490ced94 68035860 d0012b00
178 */
179 while (p <= end) {
180 int i;
181
182 sprintf(code_buffer, "%08x ", p);
183 for (i = 0; i < 4; i++) {
184 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
185 sprintf(code_buffer + strlen(code_buffer), "%08x ", data);
186 p += 4;
187 }
188 _LOG(tfd, only_in_tombstone, "%s\n", code_buffer);
189 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 }
191
192 p = sp - 64;
193 p &= ~3;
194 if (unwind_depth != 0) {
195 if (unwind_depth < STACK_CONTENT_DEPTH) {
196 end = sp_list[unwind_depth-1];
197 }
198 else {
199 end = sp_list[STACK_CONTENT_DEPTH-1];
200 }
201 }
202 else {
203 end = sp | 0x000000ff;
204 end += 0xff;
205 }
206
Ben Cheng09e71372009-09-28 11:06:09 -0700207 _LOG(tfd, only_in_tombstone, "\nstack:\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208
209 /* If the crash is due to PC == 0, there will be two frames that
210 * have identical SP value.
211 */
212 if (sp_list[0] == sp_list[1]) {
213 sp_depth = 1;
214 }
215 else {
216 sp_depth = 0;
217 }
218
219 while (p <= end) {
Ben Cheng09e71372009-09-28 11:06:09 -0700220 char *prompt;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 char level[16];
222 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
223 if (p == sp_list[sp_depth]) {
224 sprintf(level, "#%02d", sp_depth++);
225 prompt = level;
226 }
227 else {
228 prompt = " ";
229 }
Ben Cheng09e71372009-09-28 11:06:09 -0700230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 /* Print the stack content in the log for the first 3 frames. For the
232 * rest only print them in the tombstone file.
233 */
Ben Cheng09e71372009-09-28 11:06:09 -0700234 _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
235 "%s %08x %08x %s\n", prompt, p, data,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 map_to_name(map, data, ""));
237 p += 4;
238 }
239 /* print another 64-byte of stack data after the last frame */
240
241 end = p+64;
242 while (p <= end) {
243 data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
Ben Cheng09e71372009-09-28 11:06:09 -0700244 _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
245 " %08x %08x %s\n", p, data,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 map_to_name(map, data, ""));
247 p += 4;
248 }
249}
250
Ben Cheng09e71372009-09-28 11:06:09 -0700251void dump_pc_and_lr(int tfd, int pid, mapinfo *map, int unwound_level,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 bool at_fault)
253{
254 struct pt_regs r;
255
256 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
257 _LOG(tfd, !at_fault, "tid %d not responding!\n", pid);
258 return;
259 }
260
261 if (unwound_level == 0) {
262 _LOG(tfd, !at_fault, " #%02d pc %08x %s\n", 0, r.ARM_pc,
263 map_to_name(map, r.ARM_pc, "<unknown>"));
264 }
265 _LOG(tfd, !at_fault, " #%02d lr %08x %s\n", 1, r.ARM_lr,
266 map_to_name(map, r.ARM_lr, "<unknown>"));
267}
268
Ben Cheng09e71372009-09-28 11:06:09 -0700269void dump_registers(int tfd, int pid, bool at_fault)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270{
271 struct pt_regs r;
272 bool only_in_tombstone = !at_fault;
273
274 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
Ben Cheng09e71372009-09-28 11:06:09 -0700275 _LOG(tfd, only_in_tombstone,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276 "cannot get registers: %s\n", strerror(errno));
277 return;
278 }
Ben Cheng09e71372009-09-28 11:06:09 -0700279
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 _LOG(tfd, only_in_tombstone, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
281 r.ARM_r0, r.ARM_r1, r.ARM_r2, r.ARM_r3);
282 _LOG(tfd, only_in_tombstone, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
283 r.ARM_r4, r.ARM_r5, r.ARM_r6, r.ARM_r7);
284 _LOG(tfd, only_in_tombstone, " r8 %08x r9 %08x 10 %08x fp %08x\n",
285 r.ARM_r8, r.ARM_r9, r.ARM_r10, r.ARM_fp);
Ben Cheng09e71372009-09-28 11:06:09 -0700286 _LOG(tfd, only_in_tombstone,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
Ben Cheng09e71372009-09-28 11:06:09 -0700288 r.ARM_ip, r.ARM_sp, r.ARM_lr, r.ARM_pc, r.ARM_cpsr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289}
290
291const char *get_signame(int sig)
292{
293 switch(sig) {
294 case SIGILL: return "SIGILL";
295 case SIGABRT: return "SIGABRT";
296 case SIGBUS: return "SIGBUS";
297 case SIGFPE: return "SIGFPE";
298 case SIGSEGV: return "SIGSEGV";
299 case SIGSTKFLT: return "SIGSTKFLT";
300 default: return "?";
301 }
302}
303
304void dump_fault_addr(int tfd, int pid, int sig)
305{
306 siginfo_t si;
Ben Cheng09e71372009-09-28 11:06:09 -0700307
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 memset(&si, 0, sizeof(si));
309 if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){
310 _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno));
311 } else {
312 _LOG(tfd, false, "signal %d (%s), fault addr %08x\n",
313 sig, get_signame(sig), si.si_addr);
314 }
315}
316
317void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig)
318{
319 char data[1024];
320 char *x = 0;
321 FILE *fp;
Ben Cheng09e71372009-09-28 11:06:09 -0700322
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 sprintf(data, "/proc/%d/cmdline", pid);
324 fp = fopen(data, "r");
325 if(fp) {
326 x = fgets(data, 1024, fp);
327 fclose(fp);
328 }
Ben Cheng09e71372009-09-28 11:06:09 -0700329
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 _LOG(tfd, false,
331 "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
332 dump_build_info(tfd);
333 _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n",
334 pid, tid, x ? x : "UNKNOWN");
Ben Cheng09e71372009-09-28 11:06:09 -0700335
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 if(sig) dump_fault_addr(tfd, tid, sig);
337}
338
Meng Huae7b91b2009-11-05 16:10:50 -0600339static void parse_elf_info(mapinfo *milist, pid_t pid)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340{
341 mapinfo *mi;
342 for (mi = milist; mi != NULL; mi = mi->next) {
343 Elf32_Ehdr ehdr;
344
345 memset(&ehdr, 0, sizeof(Elf32_Ehdr));
Ben Cheng09e71372009-09-28 11:06:09 -0700346 /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 * mapped section.
348 */
Ben Cheng09e71372009-09-28 11:06:09 -0700349 get_remote_struct(pid, (void *) (mi->start), &ehdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 sizeof(Elf32_Ehdr));
351 /* Check if it has the matching magic words */
352 if (IS_ELF(ehdr)) {
353 Elf32_Phdr phdr;
354 Elf32_Phdr *ptr;
355 int i;
356
357 ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff);
358 for (i = 0; i < ehdr.e_phnum; i++) {
359 /* Parse the program header */
Ben Cheng09e71372009-09-28 11:06:09 -0700360 get_remote_struct(pid, (char *) ptr+i, &phdr,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 sizeof(Elf32_Phdr));
362 /* Found a EXIDX segment? */
363 if (phdr.p_type == PT_ARM_EXIDX) {
364 mi->exidx_start = mi->start + phdr.p_offset;
365 mi->exidx_end = mi->exidx_start + phdr.p_filesz;
366 break;
367 }
368 }
Meng Huae7b91b2009-11-05 16:10:50 -0600369
370 /* Try to load symbols from this file */
371 mi->symbols = symbol_table_create(mi->name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 }
373 }
374}
375
376void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault)
377{
378 char data[1024];
379 FILE *fp;
380 mapinfo *milist = 0;
381 unsigned int sp_list[STACK_CONTENT_DEPTH];
382 int stack_depth;
383 int frame0_pc_sane = 1;
Ben Cheng09e71372009-09-28 11:06:09 -0700384
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 if (!at_fault) {
386 _LOG(tfd, true,
387 "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
388 _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid);
389 }
390
391 dump_registers(tfd, tid, at_fault);
Ben Cheng09e71372009-09-28 11:06:09 -0700392
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 /* Clear stack pointer records */
394 memset(sp_list, 0, sizeof(sp_list));
395
396 sprintf(data, "/proc/%d/maps", pid);
397 fp = fopen(data, "r");
398 if(fp) {
399 while(fgets(data, 1024, fp)) {
400 mapinfo *mi = parse_maps_line(data);
401 if(mi) {
402 mi->next = milist;
403 milist = mi;
404 }
405 }
406 fclose(fp);
407 }
408
Meng Huae7b91b2009-11-05 16:10:50 -0600409 parse_elf_info(milist, tid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410
411 /* If stack unwinder fails, use the default solution to dump the stack
412 * content.
413 */
414 stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list,
415 &frame0_pc_sane, at_fault);
416
417 /* The stack unwinder should at least unwind two levels of stack. If less
418 * level is seen we make sure at lease pc and lr are dumped.
419 */
420 if (stack_depth < 2) {
421 dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault);
422 }
423
424 dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, frame0_pc_sane,
425 at_fault);
426
427 while(milist) {
428 mapinfo *next = milist->next;
Meng Huae7b91b2009-11-05 16:10:50 -0600429 symbol_table_free(milist->symbols);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 free(milist);
431 milist = next;
432 }
433}
434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435#define MAX_TOMBSTONES 10
436
437#define typecheck(x,y) { \
438 typeof(x) __dummy1; \
439 typeof(y) __dummy2; \
440 (void)(&__dummy1 == &__dummy2); }
441
442#define TOMBSTONE_DIR "/data/tombstones"
443
444/*
445 * find_and_open_tombstone - find an available tombstone slot, if any, of the
446 * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
447 * file is available, we reuse the least-recently-modified file.
448 */
449static int find_and_open_tombstone(void)
450{
451 unsigned long mtime = ULONG_MAX;
452 struct stat sb;
453 char path[128];
454 int fd, i, oldest = 0;
455
456 /*
457 * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
458 * to, our logic breaks. This check will generate a warning if that happens.
459 */
460 typecheck(mtime, sb.st_mtime);
461
462 /*
463 * In a single wolf-like pass, find an available slot and, in case none
464 * exist, find and record the least-recently-modified file.
465 */
466 for (i = 0; i < MAX_TOMBSTONES; i++) {
467 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
468
469 if (!stat(path, &sb)) {
470 if (sb.st_mtime < mtime) {
471 oldest = i;
472 mtime = sb.st_mtime;
473 }
474 continue;
475 }
476 if (errno != ENOENT)
477 continue;
478
479 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
480 if (fd < 0)
481 continue; /* raced ? */
482
483 fchown(fd, AID_SYSTEM, AID_SYSTEM);
484 return fd;
485 }
486
487 /* we didn't find an available file, so we clobber the oldest one */
488 snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
489 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
490 fchown(fd, AID_SYSTEM, AID_SYSTEM);
491
492 return fd;
493}
494
495/* Return true if some thread is not detached cleanly */
496static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid)
497{
498 char task_path[1024];
499
500 sprintf(task_path, "/proc/%d/task", pid);
501 DIR *d;
502 struct dirent *de;
503 int need_cleanup = 0;
504
505 d = opendir(task_path);
506 /* Bail early if cannot open the task directory */
507 if (d == NULL) {
508 XLOG("Cannot open /proc/%d/task\n", pid);
509 return false;
510 }
511 while ((de = readdir(d)) != NULL) {
512 unsigned new_tid;
513 /* Ignore "." and ".." */
Ben Cheng09e71372009-09-28 11:06:09 -0700514 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 continue;
516 new_tid = atoi(de->d_name);
517 /* The main thread at fault has been handled individually */
518 if (new_tid == tid)
519 continue;
520
521 /* Skip this thread if cannot ptrace it */
522 if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0)
523 continue;
524
525 dump_crash_report(tfd, pid, new_tid, false);
526 need_cleanup |= ptrace(PTRACE_DETACH, new_tid, 0, 0);
527 }
528 closedir(d);
529 return need_cleanup != 0;
530}
531
532/* Return true if some thread is not detached cleanly */
Ben Cheng09e71372009-09-28 11:06:09 -0700533static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534 int signal)
535{
536 int fd;
537 bool need_cleanup = false;
538
539 mkdir(TOMBSTONE_DIR, 0755);
540 chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
541
542 fd = find_and_open_tombstone();
543 if (fd < 0)
544 return need_cleanup;
545
546 dump_crash_banner(fd, pid, tid, signal);
547 dump_crash_report(fd, pid, tid, true);
Ben Cheng09e71372009-09-28 11:06:09 -0700548 /*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 * If the user has requested to attach gdb, don't collect the per-thread
550 * information as it increases the chance to lose track of the process.
551 */
552 if ((signed)pid > debug_uid) {
553 need_cleanup = dump_sibling_thread_report(fd, pid, tid);
554 }
555
556 close(fd);
557 return need_cleanup;
558}
559
560static int
561write_string(const char* file, const char* string)
562{
563 int len;
564 int fd;
565 ssize_t amt;
566 fd = open(file, O_RDWR);
567 len = strlen(string);
568 if (fd < 0)
569 return -errno;
570 amt = write(fd, string, len);
571 close(fd);
572 return amt >= 0 ? 0 : -errno;
573}
574
575static
576void init_debug_led(void)
577{
578 // trout leds
579 write_string("/sys/class/leds/red/brightness", "0");
580 write_string("/sys/class/leds/green/brightness", "0");
581 write_string("/sys/class/leds/blue/brightness", "0");
582 write_string("/sys/class/leds/red/device/blink", "0");
583 // sardine leds
584 write_string("/sys/class/leds/left/cadence", "0,0");
585}
586
587static
588void enable_debug_led(void)
589{
590 // trout leds
591 write_string("/sys/class/leds/red/brightness", "255");
592 // sardine leds
593 write_string("/sys/class/leds/left/cadence", "1,0");
594}
595
596static
597void disable_debug_led(void)
598{
599 // trout leds
600 write_string("/sys/class/leds/red/brightness", "0");
601 // sardine leds
602 write_string("/sys/class/leds/left/cadence", "0,0");
603}
604
605extern int init_getevent();
606extern void uninit_getevent();
607extern int get_event(struct input_event* event, int timeout);
608
609static void wait_for_user_action(unsigned tid, struct ucred* cr)
610{
611 (void)tid;
612 /* First log a helpful message */
613 LOG( "********************************************************\n"
614 "* process %d crashed. debuggerd waiting for gdbserver \n"
615 "* \n"
616 "* adb shell gdbserver :port --attach %d & \n"
617 "* \n"
618 "* and press the HOME key. \n"
Ben Cheng09e71372009-09-28 11:06:09 -0700619 "********************************************************\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 cr->pid, cr->pid);
621
622 /* wait for HOME key */
623 if (init_getevent() == 0) {
624 int ms = 1200 / 10;
625 int dit = 1;
626 int dah = 3*dit;
627 int _ = -dit;
628 int ___ = 3*_;
629 int _______ = 7*_;
630 const signed char codes[] = {
631 dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______
632 };
633 size_t s = 0;
634 struct input_event e;
635 int home = 0;
636 init_debug_led();
637 enable_debug_led();
638 do {
639 int timeout = abs((int)(codes[s])) * ms;
640 int res = get_event(&e, timeout);
641 if (res == 0) {
642 if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0)
643 home = 1;
644 } else if (res == 1) {
645 if (++s >= sizeof(codes)/sizeof(*codes))
646 s = 0;
647 if (codes[s] > 0) {
648 enable_debug_led();
649 } else {
650 disable_debug_led();
651 }
652 }
Ben Cheng09e71372009-09-28 11:06:09 -0700653 } while (!home);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 uninit_getevent();
655 }
656
657 /* don't forget to turn debug led off */
658 disable_debug_led();
Ben Cheng09e71372009-09-28 11:06:09 -0700659
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 /* close filedescriptor */
661 LOG("debuggerd resuming process %d", cr->pid);
662 }
663
664static void handle_crashing_process(int fd)
665{
666 char buf[64];
667 struct stat s;
668 unsigned tid;
669 struct ucred cr;
Ben Cheng09e71372009-09-28 11:06:09 -0700670 int n, len, status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 int tid_attach_status = -1;
672 unsigned retry = 30;
673 bool need_cleanup = false;
674
675 char value[PROPERTY_VALUE_MAX];
676 property_get("debug.db.uid", value, "-1");
677 int debug_uid = atoi(value);
Ben Cheng09e71372009-09-28 11:06:09 -0700678
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 XLOG("handle_crashing_process(%d)\n", fd);
Ben Cheng09e71372009-09-28 11:06:09 -0700680
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 len = sizeof(cr);
682 n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
683 if(n != 0) {
684 LOG("cannot get credentials\n");
685 goto done;
686 }
687
Ben Cheng09e71372009-09-28 11:06:09 -0700688 XLOG("reading tid\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 fcntl(fd, F_SETFL, O_NONBLOCK);
690 while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) {
691 if(errno == EINTR) continue;
692 if(errno == EWOULDBLOCK) {
693 if(retry-- > 0) {
694 usleep(100 * 1000);
695 continue;
696 }
697 LOG("timed out reading tid\n");
698 goto done;
699 }
700 LOG("read failure? %s\n", strerror(errno));
701 goto done;
702 }
703
704 sprintf(buf,"/proc/%d/task/%d", cr.pid, tid);
705 if(stat(buf, &s)) {
706 LOG("tid %d does not exist in pid %d. ignorning debug request\n",
707 tid, cr.pid);
708 close(fd);
709 return;
710 }
Ben Cheng09e71372009-09-28 11:06:09 -0700711
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
713
714 tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
715 if(tid_attach_status < 0) {
716 LOG("ptrace attach failed: %s\n", strerror(errno));
717 goto done;
718 }
719
720 close(fd);
721 fd = -1;
722
723 for(;;) {
724 n = waitpid(tid, &status, __WALL);
Ben Cheng09e71372009-09-28 11:06:09 -0700725
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 if(n < 0) {
727 if(errno == EAGAIN) continue;
728 LOG("waitpid failed: %s\n", strerror(errno));
729 goto done;
730 }
731
732 XLOG("waitpid: n=%d status=%08x\n", n, status);
733
734 if(WIFSTOPPED(status)){
735 n = WSTOPSIG(status);
736 switch(n) {
737 case SIGSTOP:
738 XLOG("stopped -- continuing\n");
739 n = ptrace(PTRACE_CONT, tid, 0, 0);
740 if(n) {
741 LOG("ptrace failed: %s\n", strerror(errno));
742 goto done;
743 }
744 continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700745
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 case SIGILL:
747 case SIGABRT:
748 case SIGBUS:
749 case SIGFPE:
750 case SIGSEGV:
751 case SIGSTKFLT: {
752 XLOG("stopped -- fatal signal\n");
753 need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n);
754 kill(tid, SIGSTOP);
755 goto done;
756 }
757
758 default:
759 XLOG("stopped -- unexpected signal\n");
760 goto done;
761 }
762 } else {
763 XLOG("unexpected waitpid response\n");
764 goto done;
765 }
766 }
Ben Cheng09e71372009-09-28 11:06:09 -0700767
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800768done:
769 XLOG("detaching\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700770
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771 /* stop the process so we can debug */
772 kill(cr.pid, SIGSTOP);
773
Ben Cheng09e71372009-09-28 11:06:09 -0700774 /*
775 * If a thread has been attached by ptrace, make sure it is detached
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776 * successfully otherwise we will get a zombie.
Ben Cheng09e71372009-09-28 11:06:09 -0700777 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 if (tid_attach_status == 0) {
779 int detach_status;
780 /* detach so we can attach gdbserver */
781 detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
782 need_cleanup |= (detach_status != 0);
783 }
784
785 /*
786 * if debug.db.uid is set, its value indicates if we should wait
787 * for user action for the crashing process.
788 * in this case, we log a message and turn the debug LED on
789 * waiting for a gdb connection (for instance)
790 */
791
792 if ((signed)cr.uid <= debug_uid) {
793 wait_for_user_action(tid, &cr);
794 }
795
796 /* resume stopped process (so it can crash in peace) */
797 kill(cr.pid, SIGCONT);
798
799 if (need_cleanup) {
800 LOG("debuggerd committing suicide to free the zombie!\n");
801 kill(getpid(), SIGKILL);
802 }
803
804 if(fd != -1) close(fd);
805}
806
Ben Cheng09e71372009-09-28 11:06:09 -0700807int main()
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800808{
809 int s;
810 struct sigaction act;
Ben Cheng09e71372009-09-28 11:06:09 -0700811
812 logsocket = socket_local_client("logd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800813 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM);
814 if(logsocket < 0) {
815 logsocket = -1;
816 } else {
817 fcntl(logsocket, F_SETFD, FD_CLOEXEC);
818 }
819
820 act.sa_handler = SIG_DFL;
821 sigemptyset(&act.sa_mask);
822 sigaddset(&act.sa_mask,SIGCHLD);
823 act.sa_flags = SA_NOCLDWAIT;
824 sigaction(SIGCHLD, &act, 0);
Ben Cheng09e71372009-09-28 11:06:09 -0700825
826 s = socket_local_server("android:debuggerd",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
828 if(s < 0) return -1;
829 fcntl(s, F_SETFD, FD_CLOEXEC);
830
831 LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
Ben Cheng09e71372009-09-28 11:06:09 -0700832
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800833 for(;;) {
834 struct sockaddr addr;
835 socklen_t alen;
836 int fd;
Ben Cheng09e71372009-09-28 11:06:09 -0700837
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800838 alen = sizeof(addr);
839 fd = accept(s, &addr, &alen);
840 if(fd < 0) continue;
Ben Cheng09e71372009-09-28 11:06:09 -0700841
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800842 fcntl(fd, F_SETFD, FD_CLOEXEC);
843
844 handle_crashing_process(fd);
845 }
846 return 0;
847}