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