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