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