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