Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 3c78805 | 2016-03-16 10:45:32 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "test/core/util/test_config.h" |
| 35 | |
Nicolas "Pixel" Noble | 80d68c0 | 2015-01-13 20:14:43 -0800 | [diff] [blame] | 36 | #include <grpc/support/port_platform.h> |
Craig Tiller | 8451e87 | 2015-02-27 09:25:51 -0800 | [diff] [blame] | 37 | #include <grpc/support/log.h> |
Craig Tiller | 4e710f5 | 2015-09-30 07:38:49 -0700 | [diff] [blame] | 38 | #include "src/core/support/string.h" |
ctiller | b3e3317 | 2015-01-05 14:40:53 -0800 | [diff] [blame] | 39 | #include <stdlib.h> |
Craig Tiller | e1e2475 | 2015-01-13 17:26:27 -0800 | [diff] [blame] | 40 | #include <signal.h> |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 41 | #include <stdio.h> |
| 42 | #include <stdbool.h> |
Nicolas "Pixel" Noble | 80d68c0 | 2015-01-13 20:14:43 -0800 | [diff] [blame] | 43 | |
Craig Tiller | 7b9ed35 | 2015-08-04 08:55:12 -0700 | [diff] [blame] | 44 | double g_fixture_slowdown_factor = 1.0; |
| 45 | |
Nicolas "Pixel" Noble | 80d68c0 | 2015-01-13 20:14:43 -0800 | [diff] [blame] | 46 | #if GPR_GETPID_IN_UNISTD_H |
ctiller | b3e3317 | 2015-01-05 14:40:53 -0800 | [diff] [blame] | 47 | #include <unistd.h> |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 48 | static unsigned seed(void) { return (unsigned)getpid(); } |
Nicolas "Pixel" Noble | 80d68c0 | 2015-01-13 20:14:43 -0800 | [diff] [blame] | 49 | #endif |
| 50 | |
| 51 | #if GPR_GETPID_IN_PROCESS_H |
| 52 | #include <process.h> |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | static unsigned seed(void) { return _getpid(); } |
Nicolas "Pixel" Noble | 80d68c0 | 2015-01-13 20:14:43 -0800 | [diff] [blame] | 54 | #endif |
ctiller | b3e3317 | 2015-01-05 14:40:53 -0800 | [diff] [blame] | 55 | |
Nicolas "Pixel" Noble | 717ea0e | 2015-07-11 22:50:09 +0200 | [diff] [blame] | 56 | #if GPR_WINDOWS_CRASH_HANDLER |
Craig Tiller | c9c0b8b | 2016-03-16 14:57:29 -0700 | [diff] [blame^] | 57 | #include <windows.h> |
| 58 | #include <tchar.h> |
| 59 | #define DBGHELP_TRANSLATE_TCHAR |
| 60 | #include <dbghelp.h> |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 61 | |
Craig Tiller | c9c0b8b | 2016-03-16 14:57:29 -0700 | [diff] [blame^] | 62 | #ifdef _MSC_VER |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 63 | #pragma comment(lib, "dbghelp.lib") |
Craig Tiller | c9c0b8b | 2016-03-16 14:57:29 -0700 | [diff] [blame^] | 64 | #endif |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 65 | |
| 66 | static void print_current_stack() { |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 67 | typedef USHORT(WINAPI * CaptureStackBackTraceType)( |
| 68 | __in ULONG, __in ULONG, __out PVOID *, __out_opt PULONG); |
| 69 | CaptureStackBackTraceType func = (CaptureStackBackTraceType)( |
Craig Tiller | c9c0b8b | 2016-03-16 14:57:29 -0700 | [diff] [blame^] | 70 | GetProcAddress(LoadLibrary(_T("kernel32.dll")), "RtlCaptureStackBackTrace")); |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 71 | |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 72 | if (func == NULL) return; // WOE 29.SEP.2010 |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 73 | |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 74 | // Quote from Microsoft Documentation: |
| 75 | // ## Windows Server 2003 and Windows XP: |
| 76 | // ## The sum of the FramesToSkip and FramesToCapture parameters must be less |
| 77 | // than 63. |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 78 | #define MAX_CALLERS 62 |
| 79 | |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 80 | void *callers_stack[MAX_CALLERS]; |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 81 | unsigned short frames; |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 82 | SYMBOL_INFOW *symbol; |
| 83 | HANDLE process; |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 84 | process = GetCurrentProcess(); |
| 85 | SymInitialize(process, NULL, TRUE); |
| 86 | frames = (func)(0, MAX_CALLERS, callers_stack, NULL); |
Craig Tiller | c9c0b8b | 2016-03-16 14:57:29 -0700 | [diff] [blame^] | 87 | symbol = (SYMBOL_INFOW *)calloc(sizeof(SYMBOL_INFOW) + 256 * sizeof(wchar_t), 1); |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 88 | symbol->MaxNameLen = 255; |
| 89 | symbol->SizeOfStruct = sizeof(SYMBOL_INFOW); |
| 90 | |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 91 | const unsigned short MAX_CALLERS_SHOWN = 32; |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 92 | frames = frames < MAX_CALLERS_SHOWN ? frames : MAX_CALLERS_SHOWN; |
| 93 | for (unsigned int i = 0; i < frames; i++) { |
| 94 | SymFromAddrW(process, (DWORD64)(callers_stack[i]), 0, symbol); |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 95 | fwprintf(stderr, L"*** %d: %016I64LX %ls - 0x%0X\n", i, |
| 96 | (DWORD64)callers_stack[i], symbol->Name, symbol->Address); |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | free(symbol); |
| 100 | } |
| 101 | |
| 102 | static void print_stack_from_context(CONTEXT c) { |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 103 | STACKFRAME s; // in/out stackframe |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 104 | memset(&s, 0, sizeof(s)); |
| 105 | DWORD imageType; |
| 106 | #ifdef _M_IX86 |
| 107 | // normally, call ImageNtHeader() and use machine info from PE header |
| 108 | imageType = IMAGE_FILE_MACHINE_I386; |
| 109 | s.AddrPC.Offset = c.Eip; |
| 110 | s.AddrPC.Mode = AddrModeFlat; |
| 111 | s.AddrFrame.Offset = c.Ebp; |
| 112 | s.AddrFrame.Mode = AddrModeFlat; |
| 113 | s.AddrStack.Offset = c.Esp; |
| 114 | s.AddrStack.Mode = AddrModeFlat; |
| 115 | #elif _M_X64 |
| 116 | imageType = IMAGE_FILE_MACHINE_AMD64; |
| 117 | s.AddrPC.Offset = c.Rip; |
| 118 | s.AddrPC.Mode = AddrModeFlat; |
| 119 | s.AddrFrame.Offset = c.Rsp; |
| 120 | s.AddrFrame.Mode = AddrModeFlat; |
| 121 | s.AddrStack.Offset = c.Rsp; |
| 122 | s.AddrStack.Mode = AddrModeFlat; |
| 123 | #elif _M_IA64 |
| 124 | imageType = IMAGE_FILE_MACHINE_IA64; |
| 125 | s.AddrPC.Offset = c.StIIP; |
| 126 | s.AddrPC.Mode = AddrModeFlat; |
| 127 | s.AddrFrame.Offset = c.IntSp; |
| 128 | s.AddrFrame.Mode = AddrModeFlat; |
| 129 | s.AddrBStore.Offset = c.RsBSP; |
| 130 | s.AddrBStore.Mode = AddrModeFlat; |
| 131 | s.AddrStack.Offset = c.IntSp; |
| 132 | s.AddrStack.Mode = AddrModeFlat; |
| 133 | #else |
| 134 | #error "Platform not supported!" |
| 135 | #endif |
| 136 | |
| 137 | HANDLE process = GetCurrentProcess(); |
| 138 | HANDLE thread = GetCurrentThread(); |
| 139 | |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 140 | SYMBOL_INFOW *symbol = |
Craig Tiller | c9c0b8b | 2016-03-16 14:57:29 -0700 | [diff] [blame^] | 141 | (SYMBOL_INFOW *)calloc(sizeof(SYMBOL_INFOW) + 256 * sizeof(wchar_t), 1); |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 142 | symbol->MaxNameLen = 255; |
| 143 | symbol->SizeOfStruct = sizeof(SYMBOL_INFOW); |
| 144 | |
Craig Tiller | 2b45cb8 | 2016-03-16 10:44:57 -0700 | [diff] [blame] | 145 | while (StackWalk(imageType, process, thread, &s, &c, 0, |
| 146 | SymFunctionTableAccess, SymGetModuleBase, 0)) { |
| 147 | BOOL has_symbol = |
| 148 | SymFromAddrW(process, (DWORD64)(s.AddrPC.Offset), 0, symbol); |
| 149 | fwprintf(stderr, L"*** %016I64LX %ls - 0x%0X\n", (DWORD64)(s.AddrPC.Offset), |
| 150 | has_symbol ? symbol->Name : L"<<no symbol>>", symbol->Address); |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | free(symbol); |
| 154 | } |
| 155 | |
| 156 | static LONG crash_handler(struct _EXCEPTION_POINTERS *ex_info) { |
| 157 | fprintf(stderr, "Exception handler called, dumping information\n"); |
| 158 | bool try_to_print_stack = true; |
| 159 | PEXCEPTION_RECORD exrec = ex_info->ExceptionRecord; |
| 160 | while (exrec) { |
| 161 | DWORD code = exrec->ExceptionCode; |
| 162 | DWORD flgs = exrec->ExceptionFlags; |
| 163 | PVOID addr = exrec->ExceptionAddress; |
| 164 | if (code == EXCEPTION_STACK_OVERFLOW) try_to_print_stack = false; |
| 165 | fprintf(stderr, "code: %x - flags: %d - address: %p\n", code, flgs, addr); |
| 166 | exrec = exrec->ExceptionRecord; |
| 167 | } |
| 168 | if (try_to_print_stack) { |
| 169 | print_stack_from_context(*ex_info->ContextRecord); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | } |
| 171 | if (IsDebuggerPresent()) { |
| 172 | __debugbreak(); |
| 173 | } else { |
| 174 | _exit(1); |
| 175 | } |
Nicolas "Pixel" Noble | 717ea0e | 2015-07-11 22:50:09 +0200 | [diff] [blame] | 176 | return EXCEPTION_EXECUTE_HANDLER; |
| 177 | } |
| 178 | |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 179 | static void abort_handler(int sig) { |
| 180 | fprintf(stderr, "Abort handler called."); |
| 181 | print_current_stack(NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 182 | if (IsDebuggerPresent()) { |
| 183 | __debugbreak(); |
| 184 | } else { |
| 185 | _exit(1); |
| 186 | } |
Nicolas "Pixel" Noble | e1d95d6 | 2015-07-14 02:08:50 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | static void install_crash_handler() { |
Craig Tiller | ce2b308 | 2016-03-16 10:43:49 -0700 | [diff] [blame] | 190 | if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) { |
| 191 | fprintf(stderr, "SymInitialize failed: %d", GetLastError()); |
| 192 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)crash_handler); |
| 194 | _set_abort_behavior(0, _WRITE_ABORT_MSG); |
| 195 | _set_abort_behavior(0, _CALL_REPORTFAULT); |
| 196 | signal(SIGABRT, abort_handler); |
Nicolas "Pixel" Noble | 717ea0e | 2015-07-11 22:50:09 +0200 | [diff] [blame] | 197 | } |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 198 | #elif GPR_POSIX_CRASH_HANDLER |
| 199 | #include <execinfo.h> |
| 200 | #include <stdio.h> |
| 201 | #include <string.h> |
| 202 | #include <grpc/support/useful.h> |
Craig Tiller | 4e710f5 | 2015-09-30 07:38:49 -0700 | [diff] [blame] | 203 | #include <errno.h> |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 204 | |
Craig Tiller | 73c4e87 | 2015-09-28 13:21:54 -0700 | [diff] [blame] | 205 | static char g_alt_stack[MINSIGSTKSZ]; |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 206 | |
| 207 | #define MAX_FRAMES 32 |
| 208 | |
Craig Tiller | 4e710f5 | 2015-09-30 07:38:49 -0700 | [diff] [blame] | 209 | /* signal safe output */ |
| 210 | static void output_string(const char *string) { |
| 211 | size_t len = strlen(string); |
| 212 | ssize_t r; |
| 213 | |
| 214 | do { |
| 215 | r = write(STDERR_FILENO, string, len); |
| 216 | } while (r == -1 && errno == EINTR); |
| 217 | } |
| 218 | |
| 219 | static void output_num(long num) { |
| 220 | char buf[GPR_LTOA_MIN_BUFSIZE]; |
| 221 | gpr_ltoa(num, buf); |
| 222 | output_string(buf); |
| 223 | } |
| 224 | |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 225 | static void crash_handler(int signum, siginfo_t *info, void *data) { |
| 226 | void *addrlist[MAX_FRAMES + 1]; |
| 227 | int addrlen; |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 228 | |
Craig Tiller | 4e710f5 | 2015-09-30 07:38:49 -0700 | [diff] [blame] | 229 | output_string("\n\n\n*******************************\nCaught signal "); |
| 230 | output_num(signum); |
| 231 | output_string("\n"); |
| 232 | |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 233 | addrlen = backtrace(addrlist, GPR_ARRAY_SIZE(addrlist)); |
| 234 | |
Craig Tiller | 3a12dcf | 2015-09-30 07:47:08 -0700 | [diff] [blame] | 235 | if (addrlen == 0) { |
| 236 | output_string(" no backtrace\n"); |
| 237 | } else { |
Craig Tiller | 2c0b91d | 2015-09-30 07:49:59 -0700 | [diff] [blame] | 238 | backtrace_symbols_fd(addrlist, addrlen, STDERR_FILENO); |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 239 | } |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 240 | |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 241 | /* try to get a core dump for SIGTERM */ |
| 242 | if (signum == SIGTERM) signum = SIGQUIT; |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 243 | raise(signum); |
| 244 | } |
| 245 | |
| 246 | static void install_crash_handler() { |
| 247 | stack_t ss; |
| 248 | struct sigaction sa; |
Craig Tiller | 4e710f5 | 2015-09-30 07:38:49 -0700 | [diff] [blame] | 249 | |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 250 | memset(&ss, 0, sizeof(ss)); |
| 251 | memset(&sa, 0, sizeof(sa)); |
| 252 | ss.ss_size = sizeof(g_alt_stack); |
| 253 | ss.ss_sp = g_alt_stack; |
| 254 | GPR_ASSERT(sigaltstack(&ss, NULL) == 0); |
| 255 | sa.sa_flags = (int)(SA_SIGINFO | SA_ONSTACK | SA_RESETHAND); |
| 256 | sa.sa_sigaction = crash_handler; |
| 257 | GPR_ASSERT(sigaction(SIGILL, &sa, NULL) == 0); |
| 258 | GPR_ASSERT(sigaction(SIGABRT, &sa, NULL) == 0); |
| 259 | GPR_ASSERT(sigaction(SIGBUS, &sa, NULL) == 0); |
| 260 | GPR_ASSERT(sigaction(SIGSEGV, &sa, NULL) == 0); |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 261 | GPR_ASSERT(sigaction(SIGTERM, &sa, NULL) == 0); |
| 262 | GPR_ASSERT(sigaction(SIGQUIT, &sa, NULL) == 0); |
Craig Tiller | 2bc3729 | 2015-09-28 10:08:21 -0700 | [diff] [blame] | 263 | } |
Nicolas "Pixel" Noble | 717ea0e | 2015-07-11 22:50:09 +0200 | [diff] [blame] | 264 | #else |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 265 | static void install_crash_handler() {} |
Nicolas "Pixel" Noble | 717ea0e | 2015-07-11 22:50:09 +0200 | [diff] [blame] | 266 | #endif |
| 267 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 268 | void grpc_test_init(int argc, char **argv) { |
| 269 | install_crash_handler(); |
| 270 | gpr_log(GPR_DEBUG, "test slowdown: machine=%f build=%f total=%f", |
| 271 | (double)GRPC_TEST_SLOWDOWN_MACHINE_FACTOR, |
| 272 | (double)GRPC_TEST_SLOWDOWN_BUILD_FACTOR, |
| 273 | (double)GRPC_TEST_SLOWDOWN_FACTOR); |
ctiller | b3e3317 | 2015-01-05 14:40:53 -0800 | [diff] [blame] | 274 | /* seed rng with pid, so we don't end up with the same random numbers as a |
| 275 | concurrently running test binary */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 276 | srand(seed()); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 277 | } |