Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stddef.h> |
| 18 | #include <stdbool.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <signal.h> |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <errno.h> |
| 25 | #include <dirent.h> |
| 26 | #include <time.h> |
| 27 | #include <sys/ptrace.h> |
| 28 | #include <sys/stat.h> |
| 29 | |
| 30 | #include <private/android_filesystem_config.h> |
| 31 | |
| 32 | #include <cutils/logger.h> |
| 33 | #include <cutils/properties.h> |
| 34 | |
| 35 | #include <corkscrew/demangle.h> |
| 36 | #include <corkscrew/backtrace.h> |
| 37 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 38 | #include <sys/socket.h> |
| 39 | #include <linux/un.h> |
| 40 | |
rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 41 | #include <selinux/android.h> |
rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 42 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 43 | #include "machine.h" |
| 44 | #include "tombstone.h" |
| 45 | #include "utility.h" |
| 46 | |
| 47 | #define STACK_DEPTH 32 |
| 48 | #define STACK_WORDS 16 |
| 49 | |
| 50 | #define MAX_TOMBSTONES 10 |
| 51 | #define TOMBSTONE_DIR "/data/tombstones" |
| 52 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 53 | /* Must match the path defined in NativeCrashListener.java */ |
| 54 | #define NCRASH_SOCKET_PATH "/data/system/ndebugsocket" |
| 55 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 56 | #define typecheck(x,y) { \ |
| 57 | typeof(x) __dummy1; \ |
| 58 | typeof(y) __dummy2; \ |
| 59 | (void)(&__dummy1 == &__dummy2); } |
| 60 | |
| 61 | |
| 62 | static bool signal_has_address(int sig) { |
| 63 | switch (sig) { |
| 64 | case SIGILL: |
| 65 | case SIGFPE: |
| 66 | case SIGSEGV: |
| 67 | case SIGBUS: |
| 68 | return true; |
| 69 | default: |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static const char *get_signame(int sig) |
| 75 | { |
| 76 | switch(sig) { |
| 77 | case SIGILL: return "SIGILL"; |
| 78 | case SIGABRT: return "SIGABRT"; |
| 79 | case SIGBUS: return "SIGBUS"; |
| 80 | case SIGFPE: return "SIGFPE"; |
| 81 | case SIGSEGV: return "SIGSEGV"; |
| 82 | case SIGPIPE: return "SIGPIPE"; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 83 | #ifdef SIGSTKFLT |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 84 | case SIGSTKFLT: return "SIGSTKFLT"; |
Chris Dearman | 231e3c8 | 2012-08-10 17:06:20 -0700 | [diff] [blame] | 85 | #endif |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 86 | case SIGSTOP: return "SIGSTOP"; |
| 87 | default: return "?"; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static const char *get_sigcode(int signo, int code) |
| 92 | { |
Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 93 | // Try the signal-specific codes... |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 94 | switch (signo) { |
| 95 | case SIGILL: |
| 96 | switch (code) { |
| 97 | case ILL_ILLOPC: return "ILL_ILLOPC"; |
| 98 | case ILL_ILLOPN: return "ILL_ILLOPN"; |
| 99 | case ILL_ILLADR: return "ILL_ILLADR"; |
| 100 | case ILL_ILLTRP: return "ILL_ILLTRP"; |
| 101 | case ILL_PRVOPC: return "ILL_PRVOPC"; |
| 102 | case ILL_PRVREG: return "ILL_PRVREG"; |
| 103 | case ILL_COPROC: return "ILL_COPROC"; |
| 104 | case ILL_BADSTK: return "ILL_BADSTK"; |
| 105 | } |
| 106 | break; |
| 107 | case SIGBUS: |
| 108 | switch (code) { |
| 109 | case BUS_ADRALN: return "BUS_ADRALN"; |
| 110 | case BUS_ADRERR: return "BUS_ADRERR"; |
| 111 | case BUS_OBJERR: return "BUS_OBJERR"; |
| 112 | } |
| 113 | break; |
| 114 | case SIGFPE: |
| 115 | switch (code) { |
| 116 | case FPE_INTDIV: return "FPE_INTDIV"; |
| 117 | case FPE_INTOVF: return "FPE_INTOVF"; |
| 118 | case FPE_FLTDIV: return "FPE_FLTDIV"; |
| 119 | case FPE_FLTOVF: return "FPE_FLTOVF"; |
| 120 | case FPE_FLTUND: return "FPE_FLTUND"; |
| 121 | case FPE_FLTRES: return "FPE_FLTRES"; |
| 122 | case FPE_FLTINV: return "FPE_FLTINV"; |
| 123 | case FPE_FLTSUB: return "FPE_FLTSUB"; |
| 124 | } |
| 125 | break; |
| 126 | case SIGSEGV: |
| 127 | switch (code) { |
| 128 | case SEGV_MAPERR: return "SEGV_MAPERR"; |
| 129 | case SEGV_ACCERR: return "SEGV_ACCERR"; |
| 130 | } |
| 131 | break; |
Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 132 | case SIGTRAP: |
| 133 | switch (code) { |
| 134 | case TRAP_BRKPT: return "TRAP_BRKPT"; |
| 135 | case TRAP_TRACE: return "TRAP_TRACE"; |
| 136 | } |
| 137 | break; |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 138 | } |
Elliott Hughes | 8f7d443 | 2012-12-10 10:29:05 -0800 | [diff] [blame] | 139 | // Then the other codes... |
| 140 | switch (code) { |
| 141 | case SI_USER: return "SI_USER"; |
| 142 | #if defined(SI_KERNEL) |
| 143 | case SI_KERNEL: return "SI_KERNEL"; |
| 144 | #endif |
| 145 | case SI_QUEUE: return "SI_QUEUE"; |
| 146 | case SI_TIMER: return "SI_TIMER"; |
| 147 | case SI_MESGQ: return "SI_MESGQ"; |
| 148 | case SI_ASYNCIO: return "SI_ASYNCIO"; |
| 149 | #if defined(SI_SIGIO) |
| 150 | case SI_SIGIO: return "SI_SIGIO"; |
| 151 | #endif |
| 152 | #if defined(SI_TKILL) |
| 153 | case SI_TKILL: return "SI_TKILL"; |
| 154 | #endif |
| 155 | } |
| 156 | // Then give up... |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 157 | return "?"; |
| 158 | } |
| 159 | |
Ben Cheng | d7760c1 | 2012-09-19 16:04:01 -0700 | [diff] [blame] | 160 | static void dump_revision_info(log_t* log) |
| 161 | { |
| 162 | char revision[PROPERTY_VALUE_MAX]; |
| 163 | |
| 164 | property_get("ro.revision", revision, "unknown"); |
| 165 | |
| 166 | _LOG(log, false, "Revision: '%s'\n", revision); |
| 167 | } |
| 168 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 169 | static void dump_build_info(log_t* log) |
| 170 | { |
| 171 | char fingerprint[PROPERTY_VALUE_MAX]; |
| 172 | |
| 173 | property_get("ro.build.fingerprint", fingerprint, "unknown"); |
| 174 | |
| 175 | _LOG(log, false, "Build fingerprint: '%s'\n", fingerprint); |
| 176 | } |
| 177 | |
| 178 | static void dump_fault_addr(log_t* log, pid_t tid, int sig) |
| 179 | { |
| 180 | siginfo_t si; |
| 181 | |
| 182 | memset(&si, 0, sizeof(si)); |
| 183 | if(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)){ |
| 184 | _LOG(log, false, "cannot get siginfo: %s\n", strerror(errno)); |
| 185 | } else if (signal_has_address(sig)) { |
| 186 | _LOG(log, false, "signal %d (%s), code %d (%s), fault addr %08x\n", |
| 187 | sig, get_signame(sig), |
| 188 | si.si_code, get_sigcode(sig, si.si_code), |
| 189 | (uintptr_t) si.si_addr); |
| 190 | } else { |
| 191 | _LOG(log, false, "signal %d (%s), code %d (%s), fault addr --------\n", |
| 192 | sig, get_signame(sig), si.si_code, get_sigcode(sig, si.si_code)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void dump_thread_info(log_t* log, pid_t pid, pid_t tid, bool at_fault) { |
| 197 | char path[64]; |
| 198 | char threadnamebuf[1024]; |
| 199 | char* threadname = NULL; |
| 200 | FILE *fp; |
| 201 | |
| 202 | snprintf(path, sizeof(path), "/proc/%d/comm", tid); |
| 203 | if ((fp = fopen(path, "r"))) { |
| 204 | threadname = fgets(threadnamebuf, sizeof(threadnamebuf), fp); |
| 205 | fclose(fp); |
| 206 | if (threadname) { |
| 207 | size_t len = strlen(threadname); |
| 208 | if (len && threadname[len - 1] == '\n') { |
| 209 | threadname[len - 1] = '\0'; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (at_fault) { |
| 215 | char procnamebuf[1024]; |
| 216 | char* procname = NULL; |
| 217 | |
| 218 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 219 | if ((fp = fopen(path, "r"))) { |
| 220 | procname = fgets(procnamebuf, sizeof(procnamebuf), fp); |
| 221 | fclose(fp); |
| 222 | } |
| 223 | |
| 224 | _LOG(log, false, "pid: %d, tid: %d, name: %s >>> %s <<<\n", pid, tid, |
| 225 | threadname ? threadname : "UNKNOWN", |
| 226 | procname ? procname : "UNKNOWN"); |
| 227 | } else { |
| 228 | _LOG(log, true, "pid: %d, tid: %d, name: %s\n", pid, tid, |
| 229 | threadname ? threadname : "UNKNOWN"); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static void dump_backtrace(const ptrace_context_t* context __attribute((unused)), |
| 234 | log_t* log, pid_t tid __attribute((unused)), bool at_fault, |
| 235 | const backtrace_frame_t* backtrace, size_t frames) { |
| 236 | _LOG(log, !at_fault, "\nbacktrace:\n"); |
| 237 | |
| 238 | backtrace_symbol_t backtrace_symbols[STACK_DEPTH]; |
| 239 | get_backtrace_symbols_ptrace(context, backtrace, frames, backtrace_symbols); |
| 240 | for (size_t i = 0; i < frames; i++) { |
| 241 | char line[MAX_BACKTRACE_LINE_LENGTH]; |
| 242 | format_backtrace_line(i, &backtrace[i], &backtrace_symbols[i], |
| 243 | line, MAX_BACKTRACE_LINE_LENGTH); |
| 244 | _LOG(log, !at_fault, " %s\n", line); |
| 245 | } |
| 246 | free_backtrace_symbols(backtrace_symbols, frames); |
| 247 | } |
| 248 | |
| 249 | static void dump_stack_segment(const ptrace_context_t* context, log_t* log, pid_t tid, |
| 250 | bool only_in_tombstone, uintptr_t* sp, size_t words, int label) { |
| 251 | for (size_t i = 0; i < words; i++) { |
| 252 | uint32_t stack_content; |
| 253 | if (!try_get_word_ptrace(tid, *sp, &stack_content)) { |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | const map_info_t* mi; |
| 258 | const symbol_t* symbol; |
| 259 | find_symbol_ptrace(context, stack_content, &mi, &symbol); |
| 260 | |
| 261 | if (symbol) { |
| 262 | char* demangled_name = demangle_symbol_name(symbol->name); |
| 263 | const char* symbol_name = demangled_name ? demangled_name : symbol->name; |
| 264 | uint32_t offset = stack_content - (mi->start + symbol->start); |
| 265 | if (!i && label >= 0) { |
| 266 | if (offset) { |
| 267 | _LOG(log, only_in_tombstone, " #%02d %08x %08x %s (%s+%u)\n", |
| 268 | label, *sp, stack_content, mi ? mi->name : "", symbol_name, offset); |
| 269 | } else { |
| 270 | _LOG(log, only_in_tombstone, " #%02d %08x %08x %s (%s)\n", |
| 271 | label, *sp, stack_content, mi ? mi->name : "", symbol_name); |
| 272 | } |
| 273 | } else { |
| 274 | if (offset) { |
| 275 | _LOG(log, only_in_tombstone, " %08x %08x %s (%s+%u)\n", |
| 276 | *sp, stack_content, mi ? mi->name : "", symbol_name, offset); |
| 277 | } else { |
| 278 | _LOG(log, only_in_tombstone, " %08x %08x %s (%s)\n", |
| 279 | *sp, stack_content, mi ? mi->name : "", symbol_name); |
| 280 | } |
| 281 | } |
| 282 | free(demangled_name); |
| 283 | } else { |
| 284 | if (!i && label >= 0) { |
| 285 | _LOG(log, only_in_tombstone, " #%02d %08x %08x %s\n", |
| 286 | label, *sp, stack_content, mi ? mi->name : ""); |
| 287 | } else { |
| 288 | _LOG(log, only_in_tombstone, " %08x %08x %s\n", |
| 289 | *sp, stack_content, mi ? mi->name : ""); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | *sp += sizeof(uint32_t); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static void dump_stack(const ptrace_context_t* context, log_t* log, pid_t tid, bool at_fault, |
| 298 | const backtrace_frame_t* backtrace, size_t frames) { |
| 299 | bool have_first = false; |
| 300 | size_t first, last; |
| 301 | for (size_t i = 0; i < frames; i++) { |
| 302 | if (backtrace[i].stack_top) { |
| 303 | if (!have_first) { |
| 304 | have_first = true; |
| 305 | first = i; |
| 306 | } |
| 307 | last = i; |
| 308 | } |
| 309 | } |
| 310 | if (!have_first) { |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | _LOG(log, !at_fault, "\nstack:\n"); |
| 315 | |
| 316 | // Dump a few words before the first frame. |
| 317 | bool only_in_tombstone = !at_fault; |
| 318 | uintptr_t sp = backtrace[first].stack_top - STACK_WORDS * sizeof(uint32_t); |
| 319 | dump_stack_segment(context, log, tid, only_in_tombstone, &sp, STACK_WORDS, -1); |
| 320 | |
| 321 | // Dump a few words from all successive frames. |
| 322 | // Only log the first 3 frames, put the rest in the tombstone. |
| 323 | for (size_t i = first; i <= last; i++) { |
| 324 | const backtrace_frame_t* frame = &backtrace[i]; |
| 325 | if (sp != frame->stack_top) { |
| 326 | _LOG(log, only_in_tombstone, " ........ ........\n"); |
| 327 | sp = frame->stack_top; |
| 328 | } |
| 329 | if (i - first == 3) { |
| 330 | only_in_tombstone = true; |
| 331 | } |
| 332 | if (i == last) { |
| 333 | dump_stack_segment(context, log, tid, only_in_tombstone, &sp, STACK_WORDS, i); |
| 334 | if (sp < frame->stack_top + frame->stack_size) { |
| 335 | _LOG(log, only_in_tombstone, " ........ ........\n"); |
| 336 | } |
| 337 | } else { |
| 338 | size_t words = frame->stack_size / sizeof(uint32_t); |
| 339 | if (words == 0) { |
| 340 | words = 1; |
| 341 | } else if (words > STACK_WORDS) { |
| 342 | words = STACK_WORDS; |
| 343 | } |
| 344 | dump_stack_segment(context, log, tid, only_in_tombstone, &sp, words, i); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static void dump_backtrace_and_stack(const ptrace_context_t* context, log_t* log, pid_t tid, |
| 350 | bool at_fault) { |
| 351 | backtrace_frame_t backtrace[STACK_DEPTH]; |
| 352 | ssize_t frames = unwind_backtrace_ptrace(tid, context, backtrace, 0, STACK_DEPTH); |
| 353 | if (frames > 0) { |
| 354 | dump_backtrace(context, log, tid, at_fault, backtrace, frames); |
| 355 | dump_stack(context, log, tid, at_fault, backtrace, frames); |
| 356 | } |
| 357 | } |
| 358 | |
Elliott Hughes | d1420be | 2013-01-03 13:39:57 -0800 | [diff] [blame] | 359 | static void dump_map(log_t* log, map_info_t* m, const char* what) { |
| 360 | if (m != NULL) { |
| 361 | _LOG(log, false, " %08x-%08x %c%c%c %s\n", m->start, m->end, |
| 362 | m->is_readable ? 'r' : '-', |
| 363 | m->is_writable ? 'w' : '-', |
| 364 | m->is_executable ? 'x' : '-', |
| 365 | m->name); |
| 366 | } else { |
| 367 | _LOG(log, false, " (no %s)\n", what); |
| 368 | } |
| 369 | } |
| 370 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 371 | static void dump_nearby_maps(const ptrace_context_t* context, log_t* log, pid_t tid) { |
| 372 | siginfo_t si; |
| 373 | memset(&si, 0, sizeof(si)); |
| 374 | if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) { |
| 375 | _LOG(log, false, "cannot get siginfo for %d: %s\n", |
| 376 | tid, strerror(errno)); |
| 377 | return; |
| 378 | } |
| 379 | if (!signal_has_address(si.si_signo)) { |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | uintptr_t addr = (uintptr_t) si.si_addr; |
| 384 | addr &= ~0xfff; /* round to 4K page boundary */ |
| 385 | if (addr == 0) { /* null-pointer deref */ |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | _LOG(log, false, "\nmemory map around fault addr %08x:\n", (int)si.si_addr); |
| 390 | |
| 391 | /* |
| 392 | * Search for a match, or for a hole where the match would be. The list |
| 393 | * is backward from the file content, so it starts at high addresses. |
| 394 | */ |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 395 | map_info_t* map = context->map_info_list; |
| 396 | map_info_t *next = NULL; |
| 397 | map_info_t *prev = NULL; |
| 398 | while (map != NULL) { |
| 399 | if (addr >= map->start && addr < map->end) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 400 | next = map->next; |
| 401 | break; |
| 402 | } else if (addr >= map->end) { |
| 403 | /* map would be between "prev" and this entry */ |
| 404 | next = map; |
| 405 | map = NULL; |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | prev = map; |
| 410 | map = map->next; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Show "next" then "match" then "prev" so that the addresses appear in |
| 415 | * ascending order (like /proc/pid/maps). |
| 416 | */ |
Elliott Hughes | d1420be | 2013-01-03 13:39:57 -0800 | [diff] [blame] | 417 | dump_map(log, next, "map below"); |
| 418 | dump_map(log, map, "map for address"); |
| 419 | dump_map(log, prev, "map above"); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void dump_thread(const ptrace_context_t* context, log_t* log, pid_t tid, bool at_fault, |
| 423 | int* total_sleep_time_usec) { |
| 424 | wait_for_stop(tid, total_sleep_time_usec); |
| 425 | |
| 426 | dump_registers(context, log, tid, at_fault); |
| 427 | dump_backtrace_and_stack(context, log, tid, at_fault); |
| 428 | if (at_fault) { |
| 429 | dump_memory_and_code(context, log, tid, at_fault); |
| 430 | dump_nearby_maps(context, log, tid); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* Return true if some thread is not detached cleanly */ |
| 435 | static bool dump_sibling_thread_report(const ptrace_context_t* context, |
| 436 | log_t* log, pid_t pid, pid_t tid, int* total_sleep_time_usec) { |
| 437 | char task_path[64]; |
| 438 | snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid); |
| 439 | |
| 440 | DIR* d = opendir(task_path); |
| 441 | /* Bail early if cannot open the task directory */ |
| 442 | if (d == NULL) { |
| 443 | XLOG("Cannot open /proc/%d/task\n", pid); |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | bool detach_failed = false; |
Elliott Hughes | c463d2c | 2012-10-26 16:47:09 -0700 | [diff] [blame] | 448 | struct dirent* de; |
| 449 | while ((de = readdir(d)) != NULL) { |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 450 | /* Ignore "." and ".." */ |
| 451 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) { |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | /* The main thread at fault has been handled individually */ |
| 456 | char* end; |
| 457 | pid_t new_tid = strtoul(de->d_name, &end, 10); |
| 458 | if (*end || new_tid == tid) { |
| 459 | continue; |
| 460 | } |
| 461 | |
| 462 | /* Skip this thread if cannot ptrace it */ |
| 463 | if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) { |
| 464 | continue; |
| 465 | } |
| 466 | |
| 467 | _LOG(log, true, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n"); |
| 468 | dump_thread_info(log, pid, new_tid, false); |
| 469 | dump_thread(context, log, new_tid, false, total_sleep_time_usec); |
| 470 | |
| 471 | if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) { |
| 472 | LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno)); |
| 473 | detach_failed = true; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | closedir(d); |
| 478 | return detach_failed; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Reads the contents of the specified log device, filters out the entries |
| 483 | * that don't match the specified pid, and writes them to the tombstone file. |
| 484 | * |
| 485 | * If "tailOnly" is set, we only print the last few lines. |
| 486 | */ |
| 487 | static void dump_log_file(log_t* log, pid_t pid, const char* filename, |
| 488 | bool tailOnly) |
| 489 | { |
| 490 | bool first = true; |
| 491 | |
| 492 | /* circular buffer, for "tailOnly" mode */ |
| 493 | const int kShortLogMaxLines = 5; |
| 494 | const int kShortLogLineLen = 256; |
| 495 | char shortLog[kShortLogMaxLines][kShortLogLineLen]; |
| 496 | int shortLogCount = 0; |
| 497 | int shortLogNext = 0; |
| 498 | |
| 499 | int logfd = open(filename, O_RDONLY | O_NONBLOCK); |
| 500 | if (logfd < 0) { |
| 501 | XLOG("Unable to open %s: %s\n", filename, strerror(errno)); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | union { |
| 506 | unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1]; |
| 507 | struct logger_entry entry; |
| 508 | } log_entry; |
| 509 | |
| 510 | while (true) { |
| 511 | ssize_t actual = read(logfd, log_entry.buf, LOGGER_ENTRY_MAX_LEN); |
| 512 | if (actual < 0) { |
| 513 | if (errno == EINTR) { |
| 514 | /* interrupted by signal, retry */ |
| 515 | continue; |
| 516 | } else if (errno == EAGAIN) { |
| 517 | /* non-blocking EOF; we're done */ |
| 518 | break; |
| 519 | } else { |
| 520 | _LOG(log, true, "Error while reading log: %s\n", |
| 521 | strerror(errno)); |
| 522 | break; |
| 523 | } |
| 524 | } else if (actual == 0) { |
| 525 | _LOG(log, true, "Got zero bytes while reading log: %s\n", |
| 526 | strerror(errno)); |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * NOTE: if you XLOG something here, this will spin forever, |
| 532 | * because you will be writing as fast as you're reading. Any |
| 533 | * high-frequency debug diagnostics should just be written to |
| 534 | * the tombstone file. |
| 535 | */ |
| 536 | |
| 537 | struct logger_entry* entry = &log_entry.entry; |
| 538 | |
| 539 | if (entry->pid != (int32_t) pid) { |
| 540 | /* wrong pid, ignore */ |
| 541 | continue; |
| 542 | } |
| 543 | |
| 544 | if (first) { |
| 545 | _LOG(log, true, "--------- %slog %s\n", |
| 546 | tailOnly ? "tail end of " : "", filename); |
| 547 | first = false; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Msg format is: <priority:1><tag:N>\0<message:N>\0 |
| 552 | * |
| 553 | * We want to display it in the same format as "logcat -v threadtime" |
| 554 | * (although in this case the pid is redundant). |
| 555 | * |
| 556 | * TODO: scan for line breaks ('\n') and display each text line |
| 557 | * on a separate line, prefixed with the header, like logcat does. |
| 558 | */ |
| 559 | static const char* kPrioChars = "!.VDIWEFS"; |
| 560 | unsigned char prio = entry->msg[0]; |
| 561 | char* tag = entry->msg + 1; |
| 562 | char* msg = tag + strlen(tag) + 1; |
| 563 | |
| 564 | /* consume any trailing newlines */ |
| 565 | char* eatnl = msg + strlen(msg) - 1; |
| 566 | while (eatnl >= msg && *eatnl == '\n') { |
| 567 | *eatnl-- = '\0'; |
| 568 | } |
| 569 | |
| 570 | char prioChar = (prio < strlen(kPrioChars) ? kPrioChars[prio] : '?'); |
| 571 | |
| 572 | char timeBuf[32]; |
| 573 | time_t sec = (time_t) entry->sec; |
| 574 | struct tm tmBuf; |
| 575 | struct tm* ptm; |
| 576 | ptm = localtime_r(&sec, &tmBuf); |
| 577 | strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm); |
| 578 | |
| 579 | if (tailOnly) { |
| 580 | snprintf(shortLog[shortLogNext], kShortLogLineLen, |
| 581 | "%s.%03d %5d %5d %c %-8s: %s", |
| 582 | timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, |
| 583 | prioChar, tag, msg); |
| 584 | shortLogNext = (shortLogNext + 1) % kShortLogMaxLines; |
| 585 | shortLogCount++; |
| 586 | } else { |
| 587 | _LOG(log, true, "%s.%03d %5d %5d %c %-8s: %s\n", |
| 588 | timeBuf, entry->nsec / 1000000, entry->pid, entry->tid, |
| 589 | prioChar, tag, msg); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if (tailOnly) { |
| 594 | int i; |
| 595 | |
| 596 | /* |
| 597 | * If we filled the buffer, we want to start at "next", which has |
| 598 | * the oldest entry. If we didn't, we want to start at zero. |
| 599 | */ |
| 600 | if (shortLogCount < kShortLogMaxLines) { |
| 601 | shortLogNext = 0; |
| 602 | } else { |
| 603 | shortLogCount = kShortLogMaxLines; /* cap at window size */ |
| 604 | } |
| 605 | |
| 606 | for (i = 0; i < shortLogCount; i++) { |
| 607 | _LOG(log, true, "%s\n", shortLog[shortLogNext]); |
| 608 | shortLogNext = (shortLogNext + 1) % kShortLogMaxLines; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | close(logfd); |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Dumps the logs generated by the specified pid to the tombstone, from both |
| 617 | * "system" and "main" log devices. Ideally we'd interleave the output. |
| 618 | */ |
| 619 | static void dump_logs(log_t* log, pid_t pid, bool tailOnly) |
| 620 | { |
| 621 | dump_log_file(log, pid, "/dev/log/system", tailOnly); |
| 622 | dump_log_file(log, pid, "/dev/log/main", tailOnly); |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * Dumps all information about the specified pid to the tombstone. |
| 627 | */ |
| 628 | static bool dump_crash(log_t* log, pid_t pid, pid_t tid, int signal, |
| 629 | bool dump_sibling_threads, int* total_sleep_time_usec) |
| 630 | { |
| 631 | /* don't copy log messages to tombstone unless this is a dev device */ |
| 632 | char value[PROPERTY_VALUE_MAX]; |
| 633 | property_get("ro.debuggable", value, "0"); |
| 634 | bool want_logs = (value[0] == '1'); |
| 635 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 636 | if (log->amfd >= 0) { |
| 637 | /* |
| 638 | * Activity Manager protocol: binary 32-bit network-byte-order ints for the |
| 639 | * pid and signal number, followed by the raw text of the dump, culminating |
| 640 | * in a zero byte that marks end-of-data. |
| 641 | */ |
| 642 | uint32_t datum = htonl(pid); |
| 643 | TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) ); |
| 644 | datum = htonl(signal); |
| 645 | TEMP_FAILURE_RETRY( write(log->amfd, &datum, 4) ); |
| 646 | } |
| 647 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 648 | _LOG(log, false, |
| 649 | "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); |
| 650 | dump_build_info(log); |
Ben Cheng | d7760c1 | 2012-09-19 16:04:01 -0700 | [diff] [blame] | 651 | dump_revision_info(log); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 652 | dump_thread_info(log, pid, tid, true); |
| 653 | if(signal) { |
| 654 | dump_fault_addr(log, tid, signal); |
| 655 | } |
| 656 | |
| 657 | ptrace_context_t* context = load_ptrace_context(tid); |
| 658 | dump_thread(context, log, tid, true, total_sleep_time_usec); |
| 659 | |
| 660 | if (want_logs) { |
| 661 | dump_logs(log, pid, true); |
| 662 | } |
| 663 | |
| 664 | bool detach_failed = false; |
| 665 | if (dump_sibling_threads) { |
| 666 | detach_failed = dump_sibling_thread_report(context, log, pid, tid, total_sleep_time_usec); |
| 667 | } |
| 668 | |
| 669 | free_ptrace_context(context); |
| 670 | |
| 671 | if (want_logs) { |
| 672 | dump_logs(log, pid, false); |
| 673 | } |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 674 | |
| 675 | /* send EOD to the Activity Manager, then wait for its ack to avoid racing ahead |
| 676 | * and killing the target out from under it */ |
| 677 | if (log->amfd >= 0) { |
| 678 | uint8_t eodMarker = 0; |
| 679 | TEMP_FAILURE_RETRY( write(log->amfd, &eodMarker, 1) ); |
| 680 | /* 3 sec timeout reading the ack; we're fine if that happens */ |
| 681 | TEMP_FAILURE_RETRY( read(log->amfd, &eodMarker, 1) ); |
| 682 | } |
| 683 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 684 | return detach_failed; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * find_and_open_tombstone - find an available tombstone slot, if any, of the |
| 689 | * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no |
| 690 | * file is available, we reuse the least-recently-modified file. |
| 691 | * |
| 692 | * Returns the path of the tombstone file, allocated using malloc(). Caller must free() it. |
| 693 | */ |
| 694 | static char* find_and_open_tombstone(int* fd) |
| 695 | { |
| 696 | unsigned long mtime = ULONG_MAX; |
| 697 | struct stat sb; |
| 698 | |
| 699 | /* |
| 700 | * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought |
| 701 | * to, our logic breaks. This check will generate a warning if that happens. |
| 702 | */ |
| 703 | typecheck(mtime, sb.st_mtime); |
| 704 | |
| 705 | /* |
| 706 | * In a single wolf-like pass, find an available slot and, in case none |
| 707 | * exist, find and record the least-recently-modified file. |
| 708 | */ |
| 709 | char path[128]; |
| 710 | int oldest = 0; |
| 711 | for (int i = 0; i < MAX_TOMBSTONES; i++) { |
| 712 | snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i); |
| 713 | |
| 714 | if (!stat(path, &sb)) { |
| 715 | if (sb.st_mtime < mtime) { |
| 716 | oldest = i; |
| 717 | mtime = sb.st_mtime; |
| 718 | } |
| 719 | continue; |
| 720 | } |
| 721 | if (errno != ENOENT) |
| 722 | continue; |
| 723 | |
| 724 | *fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 725 | if (*fd < 0) |
| 726 | continue; /* raced ? */ |
| 727 | |
| 728 | fchown(*fd, AID_SYSTEM, AID_SYSTEM); |
| 729 | return strdup(path); |
| 730 | } |
| 731 | |
| 732 | /* we didn't find an available file, so we clobber the oldest one */ |
| 733 | snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest); |
| 734 | *fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); |
| 735 | if (*fd < 0) { |
| 736 | LOG("failed to open tombstone file '%s': %s\n", path, strerror(errno)); |
| 737 | return NULL; |
| 738 | } |
| 739 | fchown(*fd, AID_SYSTEM, AID_SYSTEM); |
| 740 | return strdup(path); |
| 741 | } |
| 742 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 743 | static int activity_manager_connect() { |
| 744 | int amfd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 745 | if (amfd >= 0) { |
| 746 | struct sockaddr_un address; |
| 747 | int err; |
| 748 | |
| 749 | memset(&address, 0, sizeof(address)); |
| 750 | address.sun_family = AF_UNIX; |
| 751 | strncpy(address.sun_path, NCRASH_SOCKET_PATH, sizeof(address.sun_path)); |
| 752 | err = TEMP_FAILURE_RETRY( connect(amfd, (struct sockaddr*) &address, sizeof(address)) ); |
| 753 | if (!err) { |
| 754 | struct timeval tv; |
| 755 | memset(&tv, 0, sizeof(tv)); |
| 756 | tv.tv_sec = 1; // tight leash |
| 757 | err = setsockopt(amfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); |
| 758 | if (!err) { |
| 759 | tv.tv_sec = 3; // 3 seconds on handshake read |
| 760 | err = setsockopt(amfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 761 | } |
| 762 | } |
| 763 | if (err) { |
| 764 | close(amfd); |
| 765 | amfd = -1; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return amfd; |
| 770 | } |
| 771 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 772 | char* engrave_tombstone(pid_t pid, pid_t tid, int signal, |
| 773 | bool dump_sibling_threads, bool quiet, bool* detach_failed, |
| 774 | int* total_sleep_time_usec) { |
| 775 | mkdir(TOMBSTONE_DIR, 0755); |
| 776 | chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM); |
| 777 | |
rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 778 | if (selinux_android_restorecon(TOMBSTONE_DIR) == -1) { |
| 779 | *detach_failed = false; |
| 780 | return NULL; |
| 781 | } |
rpcraig | f1186f3 | 2012-07-19 09:38:06 -0400 | [diff] [blame] | 782 | |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 783 | int fd; |
| 784 | char* path = find_and_open_tombstone(&fd); |
| 785 | if (!path) { |
| 786 | *detach_failed = false; |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | log_t log; |
| 791 | log.tfd = fd; |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 792 | log.amfd = activity_manager_connect(); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 793 | log.quiet = quiet; |
| 794 | *detach_failed = dump_crash(&log, pid, tid, signal, dump_sibling_threads, |
| 795 | total_sleep_time_usec); |
| 796 | |
Christopher Tate | ded2e5a | 2013-03-19 13:12:23 -0700 | [diff] [blame] | 797 | close(log.amfd); |
Jeff Brown | 053b865 | 2012-06-06 16:25:03 -0700 | [diff] [blame] | 798 | close(fd); |
| 799 | return path; |
| 800 | } |