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> |
Dmitry Petrochenko | 611c2c3 | 2014-02-10 14:48:12 +0700 | [diff] [blame] | 22 | #include <inttypes.h> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 24 | #include <iostream> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
| 28 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 29 | #include "base/dumpable.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 30 | #include "base/logging.h" |
Andreas Gampe | fcf81d8 | 2015-01-14 20:09:14 -0800 | [diff] [blame] | 31 | #include "base/macros.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 32 | #include "base/mutex.h" |
Andreas Gampe | 5dd44d0 | 2016-08-02 17:20:03 -0700 | [diff] [blame] | 33 | #include "native_stack_dump.h" |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 34 | #include "thread-inl.h" |
Andreas Gampe | 038bb22 | 2015-01-13 19:48:14 -0800 | [diff] [blame] | 35 | #include "thread_list.h" |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 36 | #include "utils.h" |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 40 | using android::base::StringPrintf; |
| 41 | |
Andreas Gampe | cbfded4 | 2015-01-14 18:35:01 -0800 | [diff] [blame] | 42 | static constexpr bool kUseSigRTTimeout = true; |
Andreas Gampe | 0d0ce27 | 2016-03-08 20:29:49 -0800 | [diff] [blame] | 43 | static constexpr bool kDumpNativeStackOnTimeout = true; |
Mathieu Chartier | c2f4d02 | 2014-03-03 16:11:42 -0800 | [diff] [blame] | 44 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 45 | struct Backtrace { |
Andreas Gampe | 628a61a | 2015-01-07 22:08:35 -0800 | [diff] [blame] | 46 | public: |
| 47 | explicit Backtrace(void* raw_context) : raw_context_(raw_context) {} |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 48 | void Dump(std::ostream& os) const { |
Christopher Ferris | 6cff48f | 2014-01-26 21:36:13 -0800 | [diff] [blame] | 49 | DumpNativeStack(os, GetTid(), nullptr, "\t", nullptr, raw_context_); |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 50 | } |
Andreas Gampe | 628a61a | 2015-01-07 22:08:35 -0800 | [diff] [blame] | 51 | private: |
| 52 | // Stores the context of the signal that was unexpected and will terminate the runtime. The |
| 53 | // DumpNativeStack code will take care of casting it to the expected type. This is required |
| 54 | // as our signal handler runs on an alternate stack. |
| 55 | void* raw_context_; |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 58 | struct OsInfo { |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 59 | void Dump(std::ostream& os) const { |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 60 | utsname info; |
| 61 | uname(&info); |
| 62 | // Linux 2.6.38.8-gg784 (x86_64) |
| 63 | // Darwin 11.4.0 (x86_64) |
| 64 | os << info.sysname << " " << info.release << " (" << info.machine << ")"; |
| 65 | } |
| 66 | }; |
| 67 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 68 | static const char* GetSignalName(int signal_number) { |
| 69 | switch (signal_number) { |
| 70 | case SIGABRT: return "SIGABRT"; |
| 71 | case SIGBUS: return "SIGBUS"; |
| 72 | case SIGFPE: return "SIGFPE"; |
| 73 | case SIGILL: return "SIGILL"; |
| 74 | case SIGPIPE: return "SIGPIPE"; |
| 75 | case SIGSEGV: return "SIGSEGV"; |
Elliott Hughes | 833770b | 2012-05-01 15:41:03 -0700 | [diff] [blame] | 76 | #if defined(SIGSTKFLT) |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 77 | case SIGSTKFLT: return "SIGSTKFLT"; |
| 78 | #endif |
| 79 | case SIGTRAP: return "SIGTRAP"; |
| 80 | } |
| 81 | return "??"; |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 84 | static const char* GetSignalCodeName(int signal_number, int signal_code) { |
| 85 | // Try the signal-specific codes... |
| 86 | switch (signal_number) { |
| 87 | case SIGILL: |
| 88 | switch (signal_code) { |
| 89 | case ILL_ILLOPC: return "ILL_ILLOPC"; |
| 90 | case ILL_ILLOPN: return "ILL_ILLOPN"; |
| 91 | case ILL_ILLADR: return "ILL_ILLADR"; |
| 92 | case ILL_ILLTRP: return "ILL_ILLTRP"; |
| 93 | case ILL_PRVOPC: return "ILL_PRVOPC"; |
| 94 | case ILL_PRVREG: return "ILL_PRVREG"; |
| 95 | case ILL_COPROC: return "ILL_COPROC"; |
| 96 | case ILL_BADSTK: return "ILL_BADSTK"; |
| 97 | } |
| 98 | break; |
| 99 | case SIGBUS: |
| 100 | switch (signal_code) { |
| 101 | case BUS_ADRALN: return "BUS_ADRALN"; |
| 102 | case BUS_ADRERR: return "BUS_ADRERR"; |
| 103 | case BUS_OBJERR: return "BUS_OBJERR"; |
| 104 | } |
| 105 | break; |
| 106 | case SIGFPE: |
| 107 | switch (signal_code) { |
| 108 | case FPE_INTDIV: return "FPE_INTDIV"; |
| 109 | case FPE_INTOVF: return "FPE_INTOVF"; |
| 110 | case FPE_FLTDIV: return "FPE_FLTDIV"; |
| 111 | case FPE_FLTOVF: return "FPE_FLTOVF"; |
| 112 | case FPE_FLTUND: return "FPE_FLTUND"; |
| 113 | case FPE_FLTRES: return "FPE_FLTRES"; |
| 114 | case FPE_FLTINV: return "FPE_FLTINV"; |
| 115 | case FPE_FLTSUB: return "FPE_FLTSUB"; |
| 116 | } |
| 117 | break; |
| 118 | case SIGSEGV: |
| 119 | switch (signal_code) { |
| 120 | case SEGV_MAPERR: return "SEGV_MAPERR"; |
| 121 | case SEGV_ACCERR: return "SEGV_ACCERR"; |
Christopher Ferris | 12b8c9d | 2016-02-04 14:06:34 -0800 | [diff] [blame] | 122 | #if defined(SEGV_BNDERR) |
| 123 | case SEGV_BNDERR: return "SEGV_BNDERR"; |
| 124 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 125 | } |
| 126 | break; |
| 127 | case SIGTRAP: |
| 128 | switch (signal_code) { |
| 129 | case TRAP_BRKPT: return "TRAP_BRKPT"; |
| 130 | case TRAP_TRACE: return "TRAP_TRACE"; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | // Then the other codes... |
| 135 | switch (signal_code) { |
| 136 | case SI_USER: return "SI_USER"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 137 | #if defined(SI_KERNEL) |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 138 | case SI_KERNEL: return "SI_KERNEL"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 139 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 140 | case SI_QUEUE: return "SI_QUEUE"; |
| 141 | case SI_TIMER: return "SI_TIMER"; |
| 142 | case SI_MESGQ: return "SI_MESGQ"; |
| 143 | case SI_ASYNCIO: return "SI_ASYNCIO"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 144 | #if defined(SI_SIGIO) |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 145 | case SI_SIGIO: return "SI_SIGIO"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 146 | #endif |
| 147 | #if defined(SI_TKILL) |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 148 | case SI_TKILL: return "SI_TKILL"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 149 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 150 | } |
| 151 | // Then give up... |
| 152 | return "?"; |
| 153 | } |
| 154 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 155 | struct UContext { |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 156 | explicit UContext(void* raw_context) : |
| 157 | context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) { |
| 158 | } |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 159 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 160 | void Dump(std::ostream& os) const { |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 161 | // TODO: support non-x86 hosts (not urgent because this code doesn't run on targets). |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 162 | #if defined(__APPLE__) && defined(__i386__) |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 163 | DumpRegister32(os, "eax", context->__ss.__eax); |
| 164 | DumpRegister32(os, "ebx", context->__ss.__ebx); |
| 165 | DumpRegister32(os, "ecx", context->__ss.__ecx); |
| 166 | DumpRegister32(os, "edx", context->__ss.__edx); |
| 167 | os << '\n'; |
| 168 | |
| 169 | DumpRegister32(os, "edi", context->__ss.__edi); |
| 170 | DumpRegister32(os, "esi", context->__ss.__esi); |
| 171 | DumpRegister32(os, "ebp", context->__ss.__ebp); |
| 172 | DumpRegister32(os, "esp", context->__ss.__esp); |
| 173 | os << '\n'; |
| 174 | |
| 175 | DumpRegister32(os, "eip", context->__ss.__eip); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 176 | os << " "; |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 177 | DumpRegister32(os, "eflags", context->__ss.__eflags); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 178 | DumpX86Flags(os, context->__ss.__eflags); |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 179 | os << '\n'; |
| 180 | |
| 181 | DumpRegister32(os, "cs", context->__ss.__cs); |
| 182 | DumpRegister32(os, "ds", context->__ss.__ds); |
| 183 | DumpRegister32(os, "es", context->__ss.__es); |
| 184 | DumpRegister32(os, "fs", context->__ss.__fs); |
| 185 | os << '\n'; |
| 186 | DumpRegister32(os, "gs", context->__ss.__gs); |
| 187 | DumpRegister32(os, "ss", context->__ss.__ss); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 188 | #elif defined(__linux__) && defined(__i386__) |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 189 | DumpRegister32(os, "eax", context.gregs[REG_EAX]); |
| 190 | DumpRegister32(os, "ebx", context.gregs[REG_EBX]); |
| 191 | DumpRegister32(os, "ecx", context.gregs[REG_ECX]); |
| 192 | DumpRegister32(os, "edx", context.gregs[REG_EDX]); |
| 193 | os << '\n'; |
| 194 | |
| 195 | DumpRegister32(os, "edi", context.gregs[REG_EDI]); |
| 196 | DumpRegister32(os, "esi", context.gregs[REG_ESI]); |
| 197 | DumpRegister32(os, "ebp", context.gregs[REG_EBP]); |
| 198 | DumpRegister32(os, "esp", context.gregs[REG_ESP]); |
| 199 | os << '\n'; |
| 200 | |
| 201 | DumpRegister32(os, "eip", context.gregs[REG_EIP]); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 202 | os << " "; |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 203 | DumpRegister32(os, "eflags", context.gregs[REG_EFL]); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 204 | DumpX86Flags(os, context.gregs[REG_EFL]); |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 205 | os << '\n'; |
| 206 | |
| 207 | DumpRegister32(os, "cs", context.gregs[REG_CS]); |
| 208 | DumpRegister32(os, "ds", context.gregs[REG_DS]); |
| 209 | DumpRegister32(os, "es", context.gregs[REG_ES]); |
| 210 | DumpRegister32(os, "fs", context.gregs[REG_FS]); |
| 211 | os << '\n'; |
| 212 | DumpRegister32(os, "gs", context.gregs[REG_GS]); |
| 213 | DumpRegister32(os, "ss", context.gregs[REG_SS]); |
Dmitry Petrochenko | 611c2c3 | 2014-02-10 14:48:12 +0700 | [diff] [blame] | 214 | #elif defined(__linux__) && defined(__x86_64__) |
| 215 | DumpRegister64(os, "rax", context.gregs[REG_RAX]); |
| 216 | DumpRegister64(os, "rbx", context.gregs[REG_RBX]); |
| 217 | DumpRegister64(os, "rcx", context.gregs[REG_RCX]); |
| 218 | DumpRegister64(os, "rdx", context.gregs[REG_RDX]); |
| 219 | os << '\n'; |
| 220 | |
| 221 | DumpRegister64(os, "rdi", context.gregs[REG_RDI]); |
| 222 | DumpRegister64(os, "rsi", context.gregs[REG_RSI]); |
| 223 | DumpRegister64(os, "rbp", context.gregs[REG_RBP]); |
| 224 | DumpRegister64(os, "rsp", context.gregs[REG_RSP]); |
| 225 | os << '\n'; |
| 226 | |
| 227 | DumpRegister64(os, "r8 ", context.gregs[REG_R8]); |
| 228 | DumpRegister64(os, "r9 ", context.gregs[REG_R9]); |
| 229 | DumpRegister64(os, "r10", context.gregs[REG_R10]); |
| 230 | DumpRegister64(os, "r11", context.gregs[REG_R11]); |
| 231 | os << '\n'; |
| 232 | |
| 233 | DumpRegister64(os, "r12", context.gregs[REG_R12]); |
| 234 | DumpRegister64(os, "r13", context.gregs[REG_R13]); |
| 235 | DumpRegister64(os, "r14", context.gregs[REG_R14]); |
| 236 | DumpRegister64(os, "r15", context.gregs[REG_R15]); |
| 237 | os << '\n'; |
| 238 | |
| 239 | DumpRegister64(os, "rip", context.gregs[REG_RIP]); |
| 240 | os << " "; |
| 241 | DumpRegister32(os, "eflags", context.gregs[REG_EFL]); |
| 242 | DumpX86Flags(os, context.gregs[REG_EFL]); |
| 243 | os << '\n'; |
| 244 | |
| 245 | DumpRegister32(os, "cs", (context.gregs[REG_CSGSFS]) & 0x0FFFF); |
| 246 | DumpRegister32(os, "gs", (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF); |
| 247 | DumpRegister32(os, "fs", (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF); |
| 248 | os << '\n'; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 249 | #else |
| 250 | os << "Unknown architecture/word size/OS in ucontext dump"; |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 251 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 254 | void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const { |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 255 | os << StringPrintf(" %6s: 0x%08x", name, value); |
| 256 | } |
| 257 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 258 | void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const { |
Dmitry Petrochenko | 611c2c3 | 2014-02-10 14:48:12 +0700 | [diff] [blame] | 259 | os << StringPrintf(" %6s: 0x%016" PRIx64, name, value); |
| 260 | } |
| 261 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 262 | void DumpX86Flags(std::ostream& os, uint32_t flags) const { |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 263 | os << " ["; |
| 264 | if ((flags & (1 << 0)) != 0) { |
| 265 | os << " CF"; |
| 266 | } |
| 267 | if ((flags & (1 << 2)) != 0) { |
| 268 | os << " PF"; |
| 269 | } |
| 270 | if ((flags & (1 << 4)) != 0) { |
| 271 | os << " AF"; |
| 272 | } |
| 273 | if ((flags & (1 << 6)) != 0) { |
| 274 | os << " ZF"; |
| 275 | } |
| 276 | if ((flags & (1 << 7)) != 0) { |
| 277 | os << " SF"; |
| 278 | } |
| 279 | if ((flags & (1 << 8)) != 0) { |
| 280 | os << " TF"; |
| 281 | } |
| 282 | if ((flags & (1 << 9)) != 0) { |
| 283 | os << " IF"; |
| 284 | } |
| 285 | if ((flags & (1 << 10)) != 0) { |
| 286 | os << " DF"; |
| 287 | } |
| 288 | if ((flags & (1 << 11)) != 0) { |
| 289 | os << " OF"; |
| 290 | } |
| 291 | os << " ]"; |
| 292 | } |
| 293 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 294 | mcontext_t& context; |
| 295 | }; |
| 296 | |
Andreas Gampe | cbfded4 | 2015-01-14 18:35:01 -0800 | [diff] [blame] | 297 | // Return the signal number we recognize as timeout. -1 means not active/supported. |
Andreas Gampe | 038bb22 | 2015-01-13 19:48:14 -0800 | [diff] [blame] | 298 | static int GetTimeoutSignal() { |
Andreas Gampe | cbfded4 | 2015-01-14 18:35:01 -0800 | [diff] [blame] | 299 | #if defined(__APPLE__) |
| 300 | // Mac does not support realtime signals. |
Andreas Gampe | fcf81d8 | 2015-01-14 20:09:14 -0800 | [diff] [blame] | 301 | UNUSED(kUseSigRTTimeout); |
Andreas Gampe | cbfded4 | 2015-01-14 18:35:01 -0800 | [diff] [blame] | 302 | return -1; |
| 303 | #else |
| 304 | return kUseSigRTTimeout ? (SIGRTMIN + 2) : -1; |
| 305 | #endif |
Andreas Gampe | 038bb22 | 2015-01-13 19:48:14 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static bool IsTimeoutSignal(int signal_number) { |
| 309 | return signal_number == GetTimeoutSignal(); |
| 310 | } |
| 311 | |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 312 | void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) { |
| 313 | static bool handlingUnexpectedSignal = false; |
| 314 | if (handlingUnexpectedSignal) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 315 | LogHelper::LogLineLowStack(__FILE__, |
| 316 | __LINE__, |
| 317 | ::android::base::FATAL_WITHOUT_ABORT, |
| 318 | "HandleUnexpectedSignal reentered\n"); |
Andreas Gampe | 038bb22 | 2015-01-13 19:48:14 -0800 | [diff] [blame] | 319 | if (IsTimeoutSignal(signal_number)) { |
| 320 | // Ignore a recursive timeout. |
| 321 | return; |
| 322 | } |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 323 | _exit(1); |
| 324 | } |
| 325 | handlingUnexpectedSignal = true; |
| 326 | |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 327 | gAborting++; // set before taking any locks |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 328 | MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_); |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 329 | |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 330 | bool has_address = (signal_number == SIGILL || signal_number == SIGBUS || |
| 331 | signal_number == SIGFPE || signal_number == SIGSEGV); |
| 332 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 333 | OsInfo os_info; |
Elliott Hughes | 98eedd8 | 2012-06-11 17:52:56 -0700 | [diff] [blame] | 334 | const char* cmd_line = GetCmdLine(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 335 | if (cmd_line == nullptr) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 336 | cmd_line = "<unset>"; // Because no-one called InitLogging. |
Elliott Hughes | 98eedd8 | 2012-06-11 17:52:56 -0700 | [diff] [blame] | 337 | } |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 338 | pid_t tid = GetTid(); |
| 339 | std::string thread_name(GetThreadName(tid)); |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 340 | UContext thread_context(raw_context); |
Andreas Gampe | 628a61a | 2015-01-07 22:08:35 -0800 | [diff] [blame] | 341 | Backtrace thread_backtrace(raw_context); |
Elliott Hughes | 8593fdb | 2012-04-21 20:53:44 -0700 | [diff] [blame] | 342 | |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 343 | // Note: We are using cerr directly instead of LOG macros to ensure even just partial output |
| 344 | // makes it out. That means we lose the "dalvikvm..." prefix, but that is acceptable |
| 345 | // considering this is an abort situation. |
| 346 | |
| 347 | std::cerr << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n" |
| 348 | << StringPrintf("Fatal signal %d (%s), code %d (%s)", |
| 349 | signal_number, GetSignalName(signal_number), |
| 350 | info->si_code, |
| 351 | GetSignalCodeName(signal_number, info->si_code)) |
| 352 | << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << std::endl |
| 353 | << "OS: " << Dumpable<OsInfo>(os_info) << std::endl |
| 354 | << "Cmdline: " << cmd_line << std::endl |
| 355 | << "Thread: " << tid << " \"" << thread_name << "\"" << std::endl |
| 356 | << "Registers:\n" << Dumpable<UContext>(thread_context) << std::endl |
| 357 | << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace) << std::endl; |
Hiroshi Yamauchi | ae0d7de | 2015-05-22 18:15:10 -0700 | [diff] [blame] | 358 | if (kIsDebugBuild && signal_number == SIGSEGV) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 359 | PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT); |
Hiroshi Yamauchi | ae0d7de | 2015-05-22 18:15:10 -0700 | [diff] [blame] | 360 | } |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 361 | Runtime* runtime = Runtime::Current(); |
| 362 | if (runtime != nullptr) { |
Andreas Gampe | 038bb22 | 2015-01-13 19:48:14 -0800 | [diff] [blame] | 363 | if (IsTimeoutSignal(signal_number)) { |
| 364 | // Special timeout signal. Try to dump all threads. |
Andreas Gampe | 0d0ce27 | 2016-03-08 20:29:49 -0800 | [diff] [blame] | 365 | // Note: Do not use DumpForSigQuit, as that might disable native unwind, but the native parts |
| 366 | // are of value here. |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 367 | runtime->GetThreadList()->Dump(std::cerr, kDumpNativeStackOnTimeout); |
| 368 | std::cerr << std::endl; |
Andreas Gampe | 038bb22 | 2015-01-13 19:48:14 -0800 | [diff] [blame] | 369 | } |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 370 | std::cerr << "Fault message: " << runtime->GetFaultMessage() << std::endl; |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 371 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 372 | if (getenv("debug_db_uid") != nullptr || getenv("art_wait_for_gdb_on_crash") != nullptr) { |
Andreas Gampe | ef29536 | 2016-10-11 20:04:11 -0700 | [diff] [blame] | 373 | std::cerr << "********************************************************\n" |
| 374 | << "* Process " << getpid() << " thread " << tid << " \"" << thread_name |
| 375 | << "\"" |
| 376 | << " has been suspended while crashing.\n" |
| 377 | << "* Attach gdb:\n" |
| 378 | << "* gdb -p " << tid << "\n" |
| 379 | << "********************************************************" |
| 380 | << std::endl; |
Elliott Hughes | 2554cb9 | 2012-04-18 17:19:26 -0700 | [diff] [blame] | 381 | // Wait for debugger to attach. |
| 382 | while (true) { |
| 383 | } |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 384 | } |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 385 | #ifdef __linux__ |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 386 | // Remove our signal handler for this signal... |
| 387 | struct sigaction action; |
| 388 | memset(&action, 0, sizeof(action)); |
| 389 | sigemptyset(&action.sa_mask); |
| 390 | action.sa_handler = SIG_DFL; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 391 | sigaction(signal_number, &action, nullptr); |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 392 | // ...and re-raise so we die with the appropriate status. |
| 393 | kill(getpid(), signal_number); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 394 | #else |
| 395 | exit(EXIT_FAILURE); |
| 396 | #endif |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 399 | void Runtime::InitPlatformSignalHandlers() { |
| 400 | // On the host, we don't have debuggerd to dump a stack for us when something unexpected happens. |
| 401 | struct sigaction action; |
| 402 | memset(&action, 0, sizeof(action)); |
| 403 | sigemptyset(&action.sa_mask); |
| 404 | action.sa_sigaction = HandleUnexpectedSignal; |
Elliott Hughes | 6c1c69e | 2012-04-23 16:12:51 -0700 | [diff] [blame] | 405 | // Use the three-argument sa_sigaction handler. |
| 406 | action.sa_flags |= SA_SIGINFO; |
Elliott Hughes | d06a6c7 | 2012-05-30 17:59:06 -0700 | [diff] [blame] | 407 | // Use the alternate signal stack so we can catch stack overflows. |
| 408 | action.sa_flags |= SA_ONSTACK; |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 409 | |
| 410 | int rc = 0; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 411 | rc += sigaction(SIGABRT, &action, nullptr); |
| 412 | rc += sigaction(SIGBUS, &action, nullptr); |
| 413 | rc += sigaction(SIGFPE, &action, nullptr); |
| 414 | rc += sigaction(SIGILL, &action, nullptr); |
| 415 | rc += sigaction(SIGPIPE, &action, nullptr); |
| 416 | rc += sigaction(SIGSEGV, &action, nullptr); |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 417 | #if defined(SIGSTKFLT) |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 418 | rc += sigaction(SIGSTKFLT, &action, nullptr); |
Elliott Hughes | ac8097f | 2012-04-16 14:59:44 -0700 | [diff] [blame] | 419 | #endif |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 420 | rc += sigaction(SIGTRAP, &action, nullptr); |
Andreas Gampe | 038bb22 | 2015-01-13 19:48:14 -0800 | [diff] [blame] | 421 | // Special dump-all timeout. |
Andreas Gampe | cbfded4 | 2015-01-14 18:35:01 -0800 | [diff] [blame] | 422 | if (GetTimeoutSignal() != -1) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 423 | rc += sigaction(GetTimeoutSignal(), &action, nullptr); |
Andreas Gampe | cbfded4 | 2015-01-14 18:35:01 -0800 | [diff] [blame] | 424 | } |
Elliott Hughes | 457005c | 2012-04-16 13:54:25 -0700 | [diff] [blame] | 425 | CHECK_EQ(rc, 0); |
| 426 | } |
| 427 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 428 | } // namespace art |