Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 16 | |
| 17 | #include "runtime.h" |
| 18 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 19 | #include <signal.h> |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 20 | #include <string.h> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 21 | |
| 22 | #include "logging.h" |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 23 | #include "stringprintf.h" |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame^] | 24 | #include "utils.h" |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 28 | struct Backtrace { |
| 29 | void Dump(std::ostream& os) { |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame^] | 30 | DumpNativeStack(os, GetTid(), "\t", true); |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 31 | } |
| 32 | }; |
| 33 | |
| 34 | static const char* GetSignalName(int signal_number) { |
| 35 | switch (signal_number) { |
| 36 | case SIGABRT: return "SIGABRT"; |
| 37 | case SIGBUS: return "SIGBUS"; |
| 38 | case SIGFPE: return "SIGFPE"; |
| 39 | case SIGILL: return "SIGILL"; |
| 40 | case SIGPIPE: return "SIGPIPE"; |
| 41 | case SIGSEGV: return "SIGSEGV"; |
Elliott Hughes | 833770b | 2012-05-01 15:41:03 -0700 | [diff] [blame] | 42 | #if defined(SIGSTKFLT) |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 43 | case SIGSTKFLT: return "SIGSTKFLT"; |
| 44 | #endif |
| 45 | case SIGTRAP: return "SIGTRAP"; |
| 46 | } |
| 47 | return "??"; |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 50 | static const char* GetSignalCodeName(int signal_number, int signal_code) { |
| 51 | // Try the signal-specific codes... |
| 52 | switch (signal_number) { |
| 53 | case SIGILL: |
| 54 | switch (signal_code) { |
| 55 | case ILL_ILLOPC: return "ILL_ILLOPC"; |
| 56 | case ILL_ILLOPN: return "ILL_ILLOPN"; |
| 57 | case ILL_ILLADR: return "ILL_ILLADR"; |
| 58 | case ILL_ILLTRP: return "ILL_ILLTRP"; |
| 59 | case ILL_PRVOPC: return "ILL_PRVOPC"; |
| 60 | case ILL_PRVREG: return "ILL_PRVREG"; |
| 61 | case ILL_COPROC: return "ILL_COPROC"; |
| 62 | case ILL_BADSTK: return "ILL_BADSTK"; |
| 63 | } |
| 64 | break; |
| 65 | case SIGBUS: |
| 66 | switch (signal_code) { |
| 67 | case BUS_ADRALN: return "BUS_ADRALN"; |
| 68 | case BUS_ADRERR: return "BUS_ADRERR"; |
| 69 | case BUS_OBJERR: return "BUS_OBJERR"; |
| 70 | } |
| 71 | break; |
| 72 | case SIGFPE: |
| 73 | switch (signal_code) { |
| 74 | case FPE_INTDIV: return "FPE_INTDIV"; |
| 75 | case FPE_INTOVF: return "FPE_INTOVF"; |
| 76 | case FPE_FLTDIV: return "FPE_FLTDIV"; |
| 77 | case FPE_FLTOVF: return "FPE_FLTOVF"; |
| 78 | case FPE_FLTUND: return "FPE_FLTUND"; |
| 79 | case FPE_FLTRES: return "FPE_FLTRES"; |
| 80 | case FPE_FLTINV: return "FPE_FLTINV"; |
| 81 | case FPE_FLTSUB: return "FPE_FLTSUB"; |
| 82 | } |
| 83 | break; |
| 84 | case SIGSEGV: |
| 85 | switch (signal_code) { |
| 86 | case SEGV_MAPERR: return "SEGV_MAPERR"; |
| 87 | case SEGV_ACCERR: return "SEGV_ACCERR"; |
| 88 | } |
| 89 | break; |
| 90 | case SIGTRAP: |
| 91 | switch (signal_code) { |
| 92 | case TRAP_BRKPT: return "TRAP_BRKPT"; |
| 93 | case TRAP_TRACE: return "TRAP_TRACE"; |
| 94 | } |
| 95 | break; |
| 96 | } |
| 97 | // Then the other codes... |
| 98 | switch (signal_code) { |
| 99 | case SI_USER: return "SI_USER"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 100 | #if defined(SI_KERNEL) |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 101 | case SI_KERNEL: return "SI_KERNEL"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 102 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 103 | case SI_QUEUE: return "SI_QUEUE"; |
| 104 | case SI_TIMER: return "SI_TIMER"; |
| 105 | case SI_MESGQ: return "SI_MESGQ"; |
| 106 | case SI_ASYNCIO: return "SI_ASYNCIO"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 107 | #if defined(SI_SIGIO) |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 108 | case SI_SIGIO: return "SI_SIGIO"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 109 | #endif |
| 110 | #if defined(SI_TKILL) |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 111 | case SI_TKILL: return "SI_TKILL"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 112 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 113 | } |
| 114 | // Then give up... |
| 115 | return "?"; |
| 116 | } |
| 117 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 118 | struct UContext { |
| 119 | UContext(void* raw_context) : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {} |
| 120 | |
| 121 | void Dump(std::ostream& os) { |
| 122 | // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets). |
| 123 | #if defined(__APPLE__) |
| 124 | DumpRegister32(os, "eax", context->__ss.__eax); |
| 125 | DumpRegister32(os, "ebx", context->__ss.__ebx); |
| 126 | DumpRegister32(os, "ecx", context->__ss.__ecx); |
| 127 | DumpRegister32(os, "edx", context->__ss.__edx); |
| 128 | os << '\n'; |
| 129 | |
| 130 | DumpRegister32(os, "edi", context->__ss.__edi); |
| 131 | DumpRegister32(os, "esi", context->__ss.__esi); |
| 132 | DumpRegister32(os, "ebp", context->__ss.__ebp); |
| 133 | DumpRegister32(os, "esp", context->__ss.__esp); |
| 134 | os << '\n'; |
| 135 | |
| 136 | DumpRegister32(os, "eip", context->__ss.__eip); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame^] | 137 | os << " "; |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 138 | DumpRegister32(os, "eflags", context->__ss.__eflags); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame^] | 139 | DumpX86Flags(os, context->__ss.__eflags); |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 140 | os << '\n'; |
| 141 | |
| 142 | DumpRegister32(os, "cs", context->__ss.__cs); |
| 143 | DumpRegister32(os, "ds", context->__ss.__ds); |
| 144 | DumpRegister32(os, "es", context->__ss.__es); |
| 145 | DumpRegister32(os, "fs", context->__ss.__fs); |
| 146 | os << '\n'; |
| 147 | DumpRegister32(os, "gs", context->__ss.__gs); |
| 148 | DumpRegister32(os, "ss", context->__ss.__ss); |
| 149 | #else |
| 150 | DumpRegister32(os, "eax", context.gregs[REG_EAX]); |
| 151 | DumpRegister32(os, "ebx", context.gregs[REG_EBX]); |
| 152 | DumpRegister32(os, "ecx", context.gregs[REG_ECX]); |
| 153 | DumpRegister32(os, "edx", context.gregs[REG_EDX]); |
| 154 | os << '\n'; |
| 155 | |
| 156 | DumpRegister32(os, "edi", context.gregs[REG_EDI]); |
| 157 | DumpRegister32(os, "esi", context.gregs[REG_ESI]); |
| 158 | DumpRegister32(os, "ebp", context.gregs[REG_EBP]); |
| 159 | DumpRegister32(os, "esp", context.gregs[REG_ESP]); |
| 160 | os << '\n'; |
| 161 | |
| 162 | DumpRegister32(os, "eip", context.gregs[REG_EIP]); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame^] | 163 | os << " "; |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 164 | DumpRegister32(os, "eflags", context.gregs[REG_EFL]); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame^] | 165 | DumpX86Flags(os, context.gregs[REG_EFL]); |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 166 | os << '\n'; |
| 167 | |
| 168 | DumpRegister32(os, "cs", context.gregs[REG_CS]); |
| 169 | DumpRegister32(os, "ds", context.gregs[REG_DS]); |
| 170 | DumpRegister32(os, "es", context.gregs[REG_ES]); |
| 171 | DumpRegister32(os, "fs", context.gregs[REG_FS]); |
| 172 | os << '\n'; |
| 173 | DumpRegister32(os, "gs", context.gregs[REG_GS]); |
| 174 | DumpRegister32(os, "ss", context.gregs[REG_SS]); |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 175 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 178 | void DumpRegister32(std::ostream& os, const char* name, uint32_t value) { |
| 179 | os << StringPrintf(" %6s: 0x%08x", name, value); |
| 180 | } |
| 181 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame^] | 182 | void DumpX86Flags(std::ostream& os, uint32_t flags) { |
| 183 | os << " ["; |
| 184 | if ((flags & (1 << 0)) != 0) { |
| 185 | os << " CF"; |
| 186 | } |
| 187 | if ((flags & (1 << 2)) != 0) { |
| 188 | os << " PF"; |
| 189 | } |
| 190 | if ((flags & (1 << 4)) != 0) { |
| 191 | os << " AF"; |
| 192 | } |
| 193 | if ((flags & (1 << 6)) != 0) { |
| 194 | os << " ZF"; |
| 195 | } |
| 196 | if ((flags & (1 << 7)) != 0) { |
| 197 | os << " SF"; |
| 198 | } |
| 199 | if ((flags & (1 << 8)) != 0) { |
| 200 | os << " TF"; |
| 201 | } |
| 202 | if ((flags & (1 << 9)) != 0) { |
| 203 | os << " IF"; |
| 204 | } |
| 205 | if ((flags & (1 << 10)) != 0) { |
| 206 | os << " DF"; |
| 207 | } |
| 208 | if ((flags & (1 << 11)) != 0) { |
| 209 | os << " OF"; |
| 210 | } |
| 211 | os << " ]"; |
| 212 | } |
| 213 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 214 | mcontext_t& context; |
| 215 | }; |
| 216 | |
| 217 | static void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) { |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 218 | static Mutex unexpected_signal_lock("unexpected signal lock"); |
| 219 | MutexLock mu(unexpected_signal_lock); |
| 220 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 221 | bool has_address = (signal_number == SIGILL || signal_number == SIGBUS || |
| 222 | signal_number == SIGFPE || signal_number == SIGSEGV); |
| 223 | |
| 224 | UContext thread_context(raw_context); |
| 225 | Backtrace thread_backtrace; |
Elliott Hughes | 8593fdb | 2012-04-21 20:53:44 -0700 | [diff] [blame] | 226 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 227 | LOG(INTERNAL_FATAL) << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n" |
| 228 | << StringPrintf("Fatal signal %d (%s), code %d (%s)", |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 229 | signal_number, GetSignalName(signal_number), |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 230 | info->si_code, |
| 231 | GetSignalCodeName(signal_number, info->si_code)) |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 232 | << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << "\n" |
| 233 | << "Registers:\n" << Dumpable<UContext>(thread_context) << "\n" |
| 234 | << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace); |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 235 | |
Elliott Hughes | 2554cb9 | 2012-04-18 17:19:26 -0700 | [diff] [blame] | 236 | // TODO: instead, get debuggerd running on the host, try to connect, and hang around on success. |
| 237 | if (getenv("debug_db_uid") != NULL) { |
| 238 | LOG(INTERNAL_FATAL) << "********************************************************\n" |
| 239 | << "* Process " << getpid() << " has been suspended while crashing. Attach gdb:\n" |
| 240 | << "* gdb -p " << getpid() << "\n" |
| 241 | << "********************************************************\n"; |
| 242 | // Wait for debugger to attach. |
| 243 | while (true) { |
| 244 | } |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 245 | } |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 246 | |
| 247 | // Remove our signal handler for this signal... |
| 248 | struct sigaction action; |
| 249 | memset(&action, 0, sizeof(action)); |
| 250 | sigemptyset(&action.sa_mask); |
| 251 | action.sa_handler = SIG_DFL; |
| 252 | sigaction(signal_number, &action, NULL); |
| 253 | // ...and re-raise so we die with the appropriate status. |
| 254 | kill(getpid(), signal_number); |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 257 | void Runtime::InitPlatformSignalHandlers() { |
| 258 | // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens. |
| 259 | struct sigaction action; |
| 260 | memset(&action, 0, sizeof(action)); |
| 261 | sigemptyset(&action.sa_mask); |
| 262 | action.sa_sigaction = HandleUnexpectedSignal; |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 263 | // Use the three-argument sa_sigaction handler. |
| 264 | action.sa_flags |= SA_SIGINFO; |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 265 | #if !defined(__APPLE__) |
| 266 | // Use the alternate signal stack so we can catch stack overflows. |
| 267 | action.sa_flags |= SA_ONSTACK; |
| 268 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 269 | |
| 270 | int rc = 0; |
| 271 | rc += sigaction(SIGILL, &action, NULL); |
| 272 | rc += sigaction(SIGTRAP, &action, NULL); |
| 273 | rc += sigaction(SIGABRT, &action, NULL); |
| 274 | rc += sigaction(SIGBUS, &action, NULL); |
| 275 | rc += sigaction(SIGFPE, &action, NULL); |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 276 | #if defined(SIGSTKFLT) |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 277 | rc += sigaction(SIGSTKFLT, &action, NULL); |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 278 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 279 | rc += sigaction(SIGPIPE, &action, NULL); |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 280 | |
| 281 | // Use the alternate signal stack so we can catch stack overflows. |
| 282 | // On Mac OS 10.7, backtrace(3) is broken and will return no frames when called from the alternate stack, |
| 283 | // so we only use the alternate stack for SIGSEGV so that we at least get backtraces for other signals. |
| 284 | // (glibc does the right thing, so we could use the alternate stack for all signals there.) |
| 285 | action.sa_flags |= SA_ONSTACK; |
| 286 | rc += sigaction(SIGSEGV, &action, NULL); |
| 287 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 288 | CHECK_EQ(rc, 0); |
| 289 | } |
| 290 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 291 | } // namespace art |