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