The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* system/debuggerd/debuggerd.c |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 5 | ** 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 8 | ** |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | ** |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 11 | ** 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #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> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | #include <cutils/properties.h> |
| 35 | |
| 36 | #include <linux/input.h> |
| 37 | |
| 38 | #include <private/android_filesystem_config.h> |
| 39 | |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 40 | #include "debuggerd.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | #include "utility.h" |
| 42 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | #define ANDROID_LOG_INFO 4 |
| 44 | |
| 45 | /* Log information onto the tombstone */ |
| 46 | void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...) |
| 47 | { |
Meng Hu | ae7b91b | 2009-11-05 16:10:50 -0600 | [diff] [blame] | 48 | char buf[512]; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 49 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | va_list ap; |
| 51 | va_start(ap, fmt); |
| 52 | |
| 53 | if (tfd >= 0) { |
| 54 | int len; |
| 55 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 56 | len = strlen(buf); |
| 57 | if(tfd >= 0) write(tfd, buf, len); |
| 58 | } |
| 59 | |
| 60 | if (!in_tombstone_only) |
| 61 | __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap); |
| 62 | } |
| 63 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so |
| 65 | // 012345678901234567890123456789012345678901234567890123456789 |
| 66 | // 0 1 2 3 4 5 |
| 67 | |
| 68 | mapinfo *parse_maps_line(char *line) |
| 69 | { |
| 70 | mapinfo *mi; |
| 71 | int len = strlen(line); |
| 72 | |
| 73 | if(len < 1) return 0; |
| 74 | line[--len] = 0; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 75 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | if(len < 50) return 0; |
| 77 | if(line[20] != 'x') return 0; |
| 78 | |
| 79 | mi = malloc(sizeof(mapinfo) + (len - 47)); |
| 80 | if(mi == 0) return 0; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 81 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | mi->start = strtoul(line, 0, 16); |
| 83 | mi->end = strtoul(line + 9, 0, 16); |
Meng Hu | ae7b91b | 2009-11-05 16:10:50 -0600 | [diff] [blame] | 84 | /* To be filled in parse_elf_info if the mapped section starts with |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | * elf_header |
| 86 | */ |
| 87 | mi->exidx_start = mi->exidx_end = 0; |
Meng Hu | ae7b91b | 2009-11-05 16:10:50 -0600 | [diff] [blame] | 88 | mi->symbols = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | mi->next = 0; |
| 90 | strcpy(mi->name, line + 49); |
| 91 | |
| 92 | return mi; |
| 93 | } |
| 94 | |
| 95 | void dump_build_info(int tfd) |
| 96 | { |
| 97 | char fingerprint[PROPERTY_VALUE_MAX]; |
| 98 | |
| 99 | property_get("ro.build.fingerprint", fingerprint, "unknown"); |
| 100 | |
| 101 | _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint); |
| 102 | } |
| 103 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | const char *get_signame(int sig) |
| 105 | { |
| 106 | switch(sig) { |
| 107 | case SIGILL: return "SIGILL"; |
| 108 | case SIGABRT: return "SIGABRT"; |
| 109 | case SIGBUS: return "SIGBUS"; |
| 110 | case SIGFPE: return "SIGFPE"; |
| 111 | case SIGSEGV: return "SIGSEGV"; |
| 112 | case SIGSTKFLT: return "SIGSTKFLT"; |
| 113 | default: return "?"; |
| 114 | } |
| 115 | } |
| 116 | |
Carl Shapiro | 83c6b05 | 2010-10-08 18:10:24 -0700 | [diff] [blame] | 117 | const char *get_sigcode(int signo, int code) |
| 118 | { |
| 119 | switch (signo) { |
| 120 | case SIGILL: |
| 121 | switch (code) { |
| 122 | case ILL_ILLOPC: return "ILL_ILLOPC"; |
| 123 | case ILL_ILLOPN: return "ILL_ILLOPN"; |
| 124 | case ILL_ILLADR: return "ILL_ILLADR"; |
| 125 | case ILL_ILLTRP: return "ILL_ILLTRP"; |
| 126 | case ILL_PRVOPC: return "ILL_PRVOPC"; |
| 127 | case ILL_PRVREG: return "ILL_PRVREG"; |
| 128 | case ILL_COPROC: return "ILL_COPROC"; |
| 129 | case ILL_BADSTK: return "ILL_BADSTK"; |
| 130 | } |
| 131 | break; |
| 132 | case SIGBUS: |
| 133 | switch (code) { |
| 134 | case BUS_ADRALN: return "BUS_ADRALN"; |
| 135 | case BUS_ADRERR: return "BUS_ADRERR"; |
| 136 | case BUS_OBJERR: return "BUS_OBJERR"; |
| 137 | } |
| 138 | break; |
| 139 | case SIGFPE: |
| 140 | switch (code) { |
| 141 | case FPE_INTDIV: return "FPE_INTDIV"; |
| 142 | case FPE_INTOVF: return "FPE_INTOVF"; |
| 143 | case FPE_FLTDIV: return "FPE_FLTDIV"; |
| 144 | case FPE_FLTOVF: return "FPE_FLTOVF"; |
| 145 | case FPE_FLTUND: return "FPE_FLTUND"; |
| 146 | case FPE_FLTRES: return "FPE_FLTRES"; |
| 147 | case FPE_FLTINV: return "FPE_FLTINV"; |
| 148 | case FPE_FLTSUB: return "FPE_FLTSUB"; |
| 149 | } |
| 150 | break; |
| 151 | case SIGSEGV: |
| 152 | switch (code) { |
| 153 | case SEGV_MAPERR: return "SEGV_MAPERR"; |
| 154 | case SEGV_ACCERR: return "SEGV_ACCERR"; |
| 155 | } |
| 156 | break; |
| 157 | } |
| 158 | return "?"; |
| 159 | } |
| 160 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | void dump_fault_addr(int tfd, int pid, int sig) |
| 162 | { |
| 163 | siginfo_t si; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 164 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | memset(&si, 0, sizeof(si)); |
| 166 | if(ptrace(PTRACE_GETSIGINFO, pid, 0, &si)){ |
| 167 | _LOG(tfd, false, "cannot get siginfo: %s\n", strerror(errno)); |
| 168 | } else { |
Carl Shapiro | 83c6b05 | 2010-10-08 18:10:24 -0700 | [diff] [blame] | 169 | _LOG(tfd, false, "signal %d (%s), code %d (%s), fault addr %08x\n", |
| 170 | sig, get_signame(sig), |
| 171 | si.si_code, get_sigcode(sig, si.si_code), |
| 172 | si.si_addr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig) |
| 177 | { |
| 178 | char data[1024]; |
| 179 | char *x = 0; |
| 180 | FILE *fp; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 181 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 182 | sprintf(data, "/proc/%d/cmdline", pid); |
| 183 | fp = fopen(data, "r"); |
| 184 | if(fp) { |
| 185 | x = fgets(data, 1024, fp); |
| 186 | fclose(fp); |
| 187 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 188 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | _LOG(tfd, false, |
| 190 | "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); |
| 191 | dump_build_info(tfd); |
| 192 | _LOG(tfd, false, "pid: %d, tid: %d >>> %s <<<\n", |
| 193 | pid, tid, x ? x : "UNKNOWN"); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 194 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | if(sig) dump_fault_addr(tfd, tid, sig); |
| 196 | } |
| 197 | |
Meng Hu | ae7b91b | 2009-11-05 16:10:50 -0600 | [diff] [blame] | 198 | static void parse_elf_info(mapinfo *milist, pid_t pid) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | { |
| 200 | mapinfo *mi; |
| 201 | for (mi = milist; mi != NULL; mi = mi->next) { |
| 202 | Elf32_Ehdr ehdr; |
| 203 | |
| 204 | memset(&ehdr, 0, sizeof(Elf32_Ehdr)); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 205 | /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | * mapped section. |
| 207 | */ |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 208 | get_remote_struct(pid, (void *) (mi->start), &ehdr, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | sizeof(Elf32_Ehdr)); |
| 210 | /* Check if it has the matching magic words */ |
| 211 | if (IS_ELF(ehdr)) { |
| 212 | Elf32_Phdr phdr; |
| 213 | Elf32_Phdr *ptr; |
| 214 | int i; |
| 215 | |
| 216 | ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff); |
| 217 | for (i = 0; i < ehdr.e_phnum; i++) { |
| 218 | /* Parse the program header */ |
Mike Dodd | 6b65747 | 2010-07-14 11:28:29 -0700 | [diff] [blame] | 219 | get_remote_struct(pid, (char *) (ptr+i), &phdr, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | sizeof(Elf32_Phdr)); |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 221 | #ifdef __arm__ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 222 | /* Found a EXIDX segment? */ |
| 223 | if (phdr.p_type == PT_ARM_EXIDX) { |
| 224 | mi->exidx_start = mi->start + phdr.p_offset; |
| 225 | mi->exidx_end = mi->exidx_start + phdr.p_filesz; |
| 226 | break; |
| 227 | } |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 228 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 | } |
Meng Hu | ae7b91b | 2009-11-05 16:10:50 -0600 | [diff] [blame] | 230 | |
| 231 | /* Try to load symbols from this file */ |
| 232 | mi->symbols = symbol_table_create(mi->name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault) |
| 238 | { |
| 239 | char data[1024]; |
| 240 | FILE *fp; |
| 241 | mapinfo *milist = 0; |
| 242 | unsigned int sp_list[STACK_CONTENT_DEPTH]; |
| 243 | int stack_depth; |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 244 | #ifdef __arm__ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 245 | int frame0_pc_sane = 1; |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 246 | #endif |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 247 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 248 | if (!at_fault) { |
| 249 | _LOG(tfd, true, |
| 250 | "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n"); |
| 251 | _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid); |
| 252 | } |
| 253 | |
| 254 | dump_registers(tfd, tid, at_fault); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 255 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 256 | /* Clear stack pointer records */ |
| 257 | memset(sp_list, 0, sizeof(sp_list)); |
| 258 | |
| 259 | sprintf(data, "/proc/%d/maps", pid); |
| 260 | fp = fopen(data, "r"); |
| 261 | if(fp) { |
| 262 | while(fgets(data, 1024, fp)) { |
| 263 | mapinfo *mi = parse_maps_line(data); |
| 264 | if(mi) { |
| 265 | mi->next = milist; |
| 266 | milist = mi; |
| 267 | } |
| 268 | } |
| 269 | fclose(fp); |
| 270 | } |
| 271 | |
Meng Hu | ae7b91b | 2009-11-05 16:10:50 -0600 | [diff] [blame] | 272 | parse_elf_info(milist, tid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 274 | #if __arm__ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | /* If stack unwinder fails, use the default solution to dump the stack |
| 276 | * content. |
| 277 | */ |
| 278 | stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list, |
| 279 | &frame0_pc_sane, at_fault); |
| 280 | |
| 281 | /* The stack unwinder should at least unwind two levels of stack. If less |
| 282 | * level is seen we make sure at lease pc and lr are dumped. |
| 283 | */ |
| 284 | if (stack_depth < 2) { |
| 285 | dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault); |
| 286 | } |
| 287 | |
Ben Cheng | 2854db8 | 2010-01-28 10:00:03 -0800 | [diff] [blame] | 288 | dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, at_fault); |
Bruce Beare | 6cc4923 | 2010-10-13 16:11:15 -0700 | [diff] [blame] | 289 | #elif __i386__ |
| 290 | /* If stack unwinder fails, use the default solution to dump the stack |
| 291 | * content. |
| 292 | */ |
| 293 | stack_depth = unwind_backtrace_with_ptrace_x86(tfd, tid, milist,at_fault); |
| 294 | #else |
| 295 | #error "Unsupported architecture" |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 296 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 297 | |
| 298 | while(milist) { |
| 299 | mapinfo *next = milist->next; |
Meng Hu | ae7b91b | 2009-11-05 16:10:50 -0600 | [diff] [blame] | 300 | symbol_table_free(milist->symbols); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 301 | free(milist); |
| 302 | milist = next; |
| 303 | } |
| 304 | } |
| 305 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 306 | #define MAX_TOMBSTONES 10 |
| 307 | |
| 308 | #define typecheck(x,y) { \ |
| 309 | typeof(x) __dummy1; \ |
| 310 | typeof(y) __dummy2; \ |
| 311 | (void)(&__dummy1 == &__dummy2); } |
| 312 | |
| 313 | #define TOMBSTONE_DIR "/data/tombstones" |
| 314 | |
| 315 | /* |
| 316 | * find_and_open_tombstone - find an available tombstone slot, if any, of the |
| 317 | * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no |
| 318 | * file is available, we reuse the least-recently-modified file. |
| 319 | */ |
| 320 | static int find_and_open_tombstone(void) |
| 321 | { |
| 322 | unsigned long mtime = ULONG_MAX; |
| 323 | struct stat sb; |
| 324 | char path[128]; |
| 325 | int fd, i, oldest = 0; |
| 326 | |
| 327 | /* |
| 328 | * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought |
| 329 | * to, our logic breaks. This check will generate a warning if that happens. |
| 330 | */ |
| 331 | typecheck(mtime, sb.st_mtime); |
| 332 | |
| 333 | /* |
| 334 | * In a single wolf-like pass, find an available slot and, in case none |
| 335 | * exist, find and record the least-recently-modified file. |
| 336 | */ |
| 337 | for (i = 0; i < MAX_TOMBSTONES; i++) { |
| 338 | snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i); |
| 339 | |
| 340 | if (!stat(path, &sb)) { |
| 341 | if (sb.st_mtime < mtime) { |
| 342 | oldest = i; |
| 343 | mtime = sb.st_mtime; |
| 344 | } |
| 345 | continue; |
| 346 | } |
| 347 | if (errno != ENOENT) |
| 348 | continue; |
| 349 | |
| 350 | fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 351 | if (fd < 0) |
| 352 | continue; /* raced ? */ |
| 353 | |
| 354 | fchown(fd, AID_SYSTEM, AID_SYSTEM); |
| 355 | return fd; |
| 356 | } |
| 357 | |
| 358 | /* we didn't find an available file, so we clobber the oldest one */ |
| 359 | snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest); |
| 360 | fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 361 | fchown(fd, AID_SYSTEM, AID_SYSTEM); |
| 362 | |
| 363 | return fd; |
| 364 | } |
| 365 | |
| 366 | /* Return true if some thread is not detached cleanly */ |
| 367 | static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid) |
| 368 | { |
| 369 | char task_path[1024]; |
| 370 | |
| 371 | sprintf(task_path, "/proc/%d/task", pid); |
| 372 | DIR *d; |
| 373 | struct dirent *de; |
| 374 | int need_cleanup = 0; |
| 375 | |
| 376 | d = opendir(task_path); |
| 377 | /* Bail early if cannot open the task directory */ |
| 378 | if (d == NULL) { |
| 379 | XLOG("Cannot open /proc/%d/task\n", pid); |
| 380 | return false; |
| 381 | } |
| 382 | while ((de = readdir(d)) != NULL) { |
| 383 | unsigned new_tid; |
| 384 | /* Ignore "." and ".." */ |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 385 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | continue; |
| 387 | new_tid = atoi(de->d_name); |
| 388 | /* The main thread at fault has been handled individually */ |
| 389 | if (new_tid == tid) |
| 390 | continue; |
| 391 | |
| 392 | /* Skip this thread if cannot ptrace it */ |
| 393 | if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) |
| 394 | continue; |
| 395 | |
| 396 | dump_crash_report(tfd, pid, new_tid, false); |
| 397 | need_cleanup |= ptrace(PTRACE_DETACH, new_tid, 0, 0); |
| 398 | } |
| 399 | closedir(d); |
| 400 | return need_cleanup != 0; |
| 401 | } |
| 402 | |
| 403 | /* Return true if some thread is not detached cleanly */ |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 404 | static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 405 | int signal) |
| 406 | { |
| 407 | int fd; |
| 408 | bool need_cleanup = false; |
| 409 | |
| 410 | mkdir(TOMBSTONE_DIR, 0755); |
| 411 | chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM); |
| 412 | |
| 413 | fd = find_and_open_tombstone(); |
| 414 | if (fd < 0) |
| 415 | return need_cleanup; |
| 416 | |
| 417 | dump_crash_banner(fd, pid, tid, signal); |
| 418 | dump_crash_report(fd, pid, tid, true); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 419 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 420 | * If the user has requested to attach gdb, don't collect the per-thread |
| 421 | * information as it increases the chance to lose track of the process. |
| 422 | */ |
| 423 | if ((signed)pid > debug_uid) { |
| 424 | need_cleanup = dump_sibling_thread_report(fd, pid, tid); |
| 425 | } |
| 426 | |
| 427 | close(fd); |
| 428 | return need_cleanup; |
| 429 | } |
| 430 | |
| 431 | static int |
| 432 | write_string(const char* file, const char* string) |
| 433 | { |
| 434 | int len; |
| 435 | int fd; |
| 436 | ssize_t amt; |
| 437 | fd = open(file, O_RDWR); |
| 438 | len = strlen(string); |
| 439 | if (fd < 0) |
| 440 | return -errno; |
| 441 | amt = write(fd, string, len); |
| 442 | close(fd); |
| 443 | return amt >= 0 ? 0 : -errno; |
| 444 | } |
| 445 | |
| 446 | static |
| 447 | void init_debug_led(void) |
| 448 | { |
| 449 | // trout leds |
| 450 | write_string("/sys/class/leds/red/brightness", "0"); |
| 451 | write_string("/sys/class/leds/green/brightness", "0"); |
| 452 | write_string("/sys/class/leds/blue/brightness", "0"); |
| 453 | write_string("/sys/class/leds/red/device/blink", "0"); |
| 454 | // sardine leds |
| 455 | write_string("/sys/class/leds/left/cadence", "0,0"); |
| 456 | } |
| 457 | |
| 458 | static |
| 459 | void enable_debug_led(void) |
| 460 | { |
| 461 | // trout leds |
| 462 | write_string("/sys/class/leds/red/brightness", "255"); |
| 463 | // sardine leds |
| 464 | write_string("/sys/class/leds/left/cadence", "1,0"); |
| 465 | } |
| 466 | |
| 467 | static |
| 468 | void disable_debug_led(void) |
| 469 | { |
| 470 | // trout leds |
| 471 | write_string("/sys/class/leds/red/brightness", "0"); |
| 472 | // sardine leds |
| 473 | write_string("/sys/class/leds/left/cadence", "0,0"); |
| 474 | } |
| 475 | |
| 476 | extern int init_getevent(); |
| 477 | extern void uninit_getevent(); |
| 478 | extern int get_event(struct input_event* event, int timeout); |
| 479 | |
| 480 | static void wait_for_user_action(unsigned tid, struct ucred* cr) |
| 481 | { |
| 482 | (void)tid; |
| 483 | /* First log a helpful message */ |
| 484 | LOG( "********************************************************\n" |
Andy McFadden | 3bfdcc9 | 2009-12-01 12:37:26 -0800 | [diff] [blame] | 485 | "* Process %d has been suspended while crashing. To\n" |
| 486 | "* attach gdbserver for a gdb connection on port 5039:\n" |
| 487 | "*\n" |
| 488 | "* adb shell gdbserver :5039 --attach %d &\n" |
| 489 | "*\n" |
| 490 | "* Press HOME key to let the process continue crashing.\n" |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 491 | "********************************************************\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 | cr->pid, cr->pid); |
| 493 | |
Andy McFadden | 3bfdcc9 | 2009-12-01 12:37:26 -0800 | [diff] [blame] | 494 | /* wait for HOME key (TODO: something useful for devices w/o HOME key) */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 495 | if (init_getevent() == 0) { |
| 496 | int ms = 1200 / 10; |
| 497 | int dit = 1; |
| 498 | int dah = 3*dit; |
| 499 | int _ = -dit; |
| 500 | int ___ = 3*_; |
| 501 | int _______ = 7*_; |
| 502 | const signed char codes[] = { |
| 503 | dit,_,dit,_,dit,___,dah,_,dah,_,dah,___,dit,_,dit,_,dit,_______ |
| 504 | }; |
| 505 | size_t s = 0; |
| 506 | struct input_event e; |
| 507 | int home = 0; |
| 508 | init_debug_led(); |
| 509 | enable_debug_led(); |
| 510 | do { |
| 511 | int timeout = abs((int)(codes[s])) * ms; |
| 512 | int res = get_event(&e, timeout); |
| 513 | if (res == 0) { |
| 514 | if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0) |
| 515 | home = 1; |
| 516 | } else if (res == 1) { |
| 517 | if (++s >= sizeof(codes)/sizeof(*codes)) |
| 518 | s = 0; |
| 519 | if (codes[s] > 0) { |
| 520 | enable_debug_led(); |
| 521 | } else { |
| 522 | disable_debug_led(); |
| 523 | } |
| 524 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 525 | } while (!home); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 526 | uninit_getevent(); |
| 527 | } |
| 528 | |
| 529 | /* don't forget to turn debug led off */ |
| 530 | disable_debug_led(); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 531 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 532 | /* close filedescriptor */ |
| 533 | LOG("debuggerd resuming process %d", cr->pid); |
| 534 | } |
| 535 | |
| 536 | static void handle_crashing_process(int fd) |
| 537 | { |
| 538 | char buf[64]; |
| 539 | struct stat s; |
| 540 | unsigned tid; |
| 541 | struct ucred cr; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 542 | int n, len, status; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 543 | int tid_attach_status = -1; |
| 544 | unsigned retry = 30; |
| 545 | bool need_cleanup = false; |
| 546 | |
| 547 | char value[PROPERTY_VALUE_MAX]; |
| 548 | property_get("debug.db.uid", value, "-1"); |
| 549 | int debug_uid = atoi(value); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 550 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 551 | XLOG("handle_crashing_process(%d)\n", fd); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 552 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 553 | len = sizeof(cr); |
| 554 | n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); |
| 555 | if(n != 0) { |
| 556 | LOG("cannot get credentials\n"); |
| 557 | goto done; |
| 558 | } |
| 559 | |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 560 | XLOG("reading tid\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 561 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 562 | while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) { |
| 563 | if(errno == EINTR) continue; |
| 564 | if(errno == EWOULDBLOCK) { |
| 565 | if(retry-- > 0) { |
| 566 | usleep(100 * 1000); |
| 567 | continue; |
| 568 | } |
| 569 | LOG("timed out reading tid\n"); |
| 570 | goto done; |
| 571 | } |
| 572 | LOG("read failure? %s\n", strerror(errno)); |
| 573 | goto done; |
| 574 | } |
| 575 | |
David 'Digit' Turner | 02526d4 | 2011-01-21 04:27:12 +0100 | [diff] [blame] | 576 | snprintf(buf, sizeof buf, "/proc/%d/task/%d", cr.pid, tid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 577 | if(stat(buf, &s)) { |
Andy McFadden | 3bfdcc9 | 2009-12-01 12:37:26 -0800 | [diff] [blame] | 578 | LOG("tid %d does not exist in pid %d. ignoring debug request\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 579 | tid, cr.pid); |
| 580 | close(fd); |
| 581 | return; |
| 582 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 583 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 584 | XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid); |
| 585 | |
David 'Digit' Turner | 02526d4 | 2011-01-21 04:27:12 +0100 | [diff] [blame] | 586 | /* Note that at this point, the target thread's signal handler |
| 587 | * is blocked in a read() call. This gives us the time to PTRACE_ATTACH |
| 588 | * to it before it has a chance to really fault. |
| 589 | * |
| 590 | * After the attach, the thread is stopped, and we write to the file |
| 591 | * descriptor to ensure that it will run as soon as we call PTRACE_CONT |
| 592 | * below. See details in bionic/libc/linker/debugger.c, in function |
| 593 | * debugger_signal_handler(). |
| 594 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 595 | tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0); |
David 'Digit' Turner | 02526d4 | 2011-01-21 04:27:12 +0100 | [diff] [blame] | 596 | |
| 597 | TEMP_FAILURE_RETRY(write(fd, &tid, 1)); |
| 598 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 599 | if(tid_attach_status < 0) { |
| 600 | LOG("ptrace attach failed: %s\n", strerror(errno)); |
| 601 | goto done; |
| 602 | } |
| 603 | |
| 604 | close(fd); |
| 605 | fd = -1; |
| 606 | |
| 607 | for(;;) { |
| 608 | n = waitpid(tid, &status, __WALL); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 609 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 610 | if(n < 0) { |
| 611 | if(errno == EAGAIN) continue; |
| 612 | LOG("waitpid failed: %s\n", strerror(errno)); |
| 613 | goto done; |
| 614 | } |
| 615 | |
| 616 | XLOG("waitpid: n=%d status=%08x\n", n, status); |
| 617 | |
| 618 | if(WIFSTOPPED(status)){ |
| 619 | n = WSTOPSIG(status); |
| 620 | switch(n) { |
| 621 | case SIGSTOP: |
| 622 | XLOG("stopped -- continuing\n"); |
| 623 | n = ptrace(PTRACE_CONT, tid, 0, 0); |
| 624 | if(n) { |
| 625 | LOG("ptrace failed: %s\n", strerror(errno)); |
| 626 | goto done; |
| 627 | } |
| 628 | continue; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 629 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 630 | case SIGILL: |
| 631 | case SIGABRT: |
| 632 | case SIGBUS: |
| 633 | case SIGFPE: |
| 634 | case SIGSEGV: |
| 635 | case SIGSTKFLT: { |
| 636 | XLOG("stopped -- fatal signal\n"); |
| 637 | need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n); |
| 638 | kill(tid, SIGSTOP); |
| 639 | goto done; |
| 640 | } |
| 641 | |
| 642 | default: |
| 643 | XLOG("stopped -- unexpected signal\n"); |
| 644 | goto done; |
| 645 | } |
| 646 | } else { |
| 647 | XLOG("unexpected waitpid response\n"); |
| 648 | goto done; |
| 649 | } |
| 650 | } |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 651 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 652 | done: |
| 653 | XLOG("detaching\n"); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 654 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 655 | /* stop the process so we can debug */ |
| 656 | kill(cr.pid, SIGSTOP); |
| 657 | |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 658 | /* |
| 659 | * If a thread has been attached by ptrace, make sure it is detached |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 660 | * successfully otherwise we will get a zombie. |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 661 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 662 | if (tid_attach_status == 0) { |
| 663 | int detach_status; |
| 664 | /* detach so we can attach gdbserver */ |
| 665 | detach_status = ptrace(PTRACE_DETACH, tid, 0, 0); |
| 666 | need_cleanup |= (detach_status != 0); |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * if debug.db.uid is set, its value indicates if we should wait |
| 671 | * for user action for the crashing process. |
| 672 | * in this case, we log a message and turn the debug LED on |
| 673 | * waiting for a gdb connection (for instance) |
| 674 | */ |
| 675 | |
| 676 | if ((signed)cr.uid <= debug_uid) { |
| 677 | wait_for_user_action(tid, &cr); |
| 678 | } |
| 679 | |
| 680 | /* resume stopped process (so it can crash in peace) */ |
| 681 | kill(cr.pid, SIGCONT); |
| 682 | |
| 683 | if (need_cleanup) { |
| 684 | LOG("debuggerd committing suicide to free the zombie!\n"); |
| 685 | kill(getpid(), SIGKILL); |
| 686 | } |
| 687 | |
| 688 | if(fd != -1) close(fd); |
| 689 | } |
| 690 | |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 691 | |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 692 | int main() |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 693 | { |
| 694 | int s; |
| 695 | struct sigaction act; |
Bruce Beare | 8492490 | 2010-10-13 14:21:30 -0700 | [diff] [blame] | 696 | int logsocket = -1; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 697 | |
Andy McFadden | 44e12ec | 2011-07-29 12:36:47 -0700 | [diff] [blame] | 698 | /* |
| 699 | * debuggerd crashes can't be reported to debuggerd. Reset all of the |
| 700 | * crash handlers. |
| 701 | */ |
| 702 | signal(SIGILL, SIG_DFL); |
| 703 | signal(SIGABRT, SIG_DFL); |
| 704 | signal(SIGBUS, SIG_DFL); |
| 705 | signal(SIGFPE, SIG_DFL); |
| 706 | signal(SIGSEGV, SIG_DFL); |
| 707 | signal(SIGSTKFLT, SIG_DFL); |
| 708 | signal(SIGPIPE, SIG_DFL); |
| 709 | |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 710 | logsocket = socket_local_client("logd", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 711 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_DGRAM); |
| 712 | if(logsocket < 0) { |
| 713 | logsocket = -1; |
| 714 | } else { |
| 715 | fcntl(logsocket, F_SETFD, FD_CLOEXEC); |
| 716 | } |
| 717 | |
| 718 | act.sa_handler = SIG_DFL; |
| 719 | sigemptyset(&act.sa_mask); |
| 720 | sigaddset(&act.sa_mask,SIGCHLD); |
| 721 | act.sa_flags = SA_NOCLDWAIT; |
| 722 | sigaction(SIGCHLD, &act, 0); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 723 | |
| 724 | s = socket_local_server("android:debuggerd", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 725 | ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM); |
| 726 | if(s < 0) return -1; |
| 727 | fcntl(s, F_SETFD, FD_CLOEXEC); |
| 728 | |
| 729 | LOG("debuggerd: " __DATE__ " " __TIME__ "\n"); |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 730 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 731 | for(;;) { |
| 732 | struct sockaddr addr; |
| 733 | socklen_t alen; |
| 734 | int fd; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 735 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 736 | alen = sizeof(addr); |
| 737 | fd = accept(s, &addr, &alen); |
| 738 | if(fd < 0) continue; |
Ben Cheng | 09e7137 | 2009-09-28 11:06:09 -0700 | [diff] [blame] | 739 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 740 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 741 | |
| 742 | handle_crashing_process(fd); |
| 743 | } |
| 744 | return 0; |
| 745 | } |