test_config.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  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. #include "test/core/util/test_config.h"
  34. #include <grpc/support/port_platform.h>
  35. #include <grpc/support/log.h>
  36. #include <stdlib.h>
  37. #include <signal.h>
  38. double g_fixture_slowdown_factor = 1.0;
  39. #if GPR_GETPID_IN_UNISTD_H
  40. #include <unistd.h>
  41. static unsigned seed(void) { return (unsigned)getpid(); }
  42. #endif
  43. #if GPR_GETPID_IN_PROCESS_H
  44. #include <process.h>
  45. static unsigned seed(void) { return _getpid(); }
  46. #endif
  47. #if GPR_WINDOWS_CRASH_HANDLER
  48. LONG crash_handler(struct _EXCEPTION_POINTERS *ex_info) {
  49. gpr_log(GPR_DEBUG, "Exception handler called, dumping information");
  50. while (ex_info->ExceptionRecord) {
  51. DWORD code = ex_info->ExceptionRecord->ExceptionCode;
  52. DWORD flgs = ex_info->ExceptionRecord->ExceptionFlags;
  53. PVOID addr = ex_info->ExceptionRecord->ExceptionAddress;
  54. gpr_log("code: %x - flags: %d - address: %p", code, flgs, addr);
  55. ex_info->ExceptionRecord = ex_info->ExceptionRecord->ExceptionRecord;
  56. }
  57. if (IsDebuggerPresent()) {
  58. __debugbreak();
  59. } else {
  60. _exit(1);
  61. }
  62. return EXCEPTION_EXECUTE_HANDLER;
  63. }
  64. void abort_handler(int sig) {
  65. gpr_log(GPR_DEBUG, "Abort handler called.");
  66. if (IsDebuggerPresent()) {
  67. __debugbreak();
  68. } else {
  69. _exit(1);
  70. }
  71. }
  72. static void install_crash_handler() {
  73. SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)crash_handler);
  74. _set_abort_behavior(0, _WRITE_ABORT_MSG);
  75. _set_abort_behavior(0, _CALL_REPORTFAULT);
  76. signal(SIGABRT, abort_handler);
  77. }
  78. #elif GPR_POSIX_CRASH_HANDLER
  79. #include <execinfo.h>
  80. #include <stdio.h>
  81. #include <string.h>
  82. #include <grpc/support/useful.h>
  83. static char g_alt_stack[MINSIGSTKSZ];
  84. #define MAX_FRAMES 32
  85. static void crash_handler(int signum, siginfo_t *info, void *data) {
  86. void *addrlist[MAX_FRAMES + 1];
  87. int addrlen;
  88. int i;
  89. char **symlist;
  90. fprintf(stderr, "Caught signal %d\n", signum);
  91. addrlen = backtrace(addrlist, GPR_ARRAY_SIZE(addrlist));
  92. symlist = backtrace_symbols(addrlist, addrlen);
  93. for (i = 0; i < addrlen; i++) {
  94. fprintf(stderr, " %s\n", symlist[i]);
  95. }
  96. free(symlist);
  97. raise(signum);
  98. }
  99. static void install_crash_handler() {
  100. stack_t ss;
  101. struct sigaction sa;
  102. memset(&ss, 0, sizeof(ss));
  103. memset(&sa, 0, sizeof(sa));
  104. ss.ss_size = sizeof(g_alt_stack);
  105. ss.ss_sp = g_alt_stack;
  106. GPR_ASSERT(sigaltstack(&ss, NULL) == 0);
  107. sa.sa_flags = (int)(SA_SIGINFO | SA_ONSTACK | SA_RESETHAND);
  108. sa.sa_sigaction = crash_handler;
  109. GPR_ASSERT(sigaction(SIGILL, &sa, NULL) == 0);
  110. GPR_ASSERT(sigaction(SIGABRT, &sa, NULL) == 0);
  111. GPR_ASSERT(sigaction(SIGBUS, &sa, NULL) == 0);
  112. GPR_ASSERT(sigaction(SIGSEGV, &sa, NULL) == 0);
  113. }
  114. #else
  115. static void install_crash_handler() {}
  116. #endif
  117. void grpc_test_init(int argc, char **argv) {
  118. install_crash_handler();
  119. gpr_log(GPR_DEBUG, "test slowdown: machine=%f build=%f total=%f",
  120. (double)GRPC_TEST_SLOWDOWN_MACHINE_FACTOR,
  121. (double)GRPC_TEST_SLOWDOWN_BUILD_FACTOR,
  122. (double)GRPC_TEST_SLOWDOWN_FACTOR);
  123. /* seed rng with pid, so we don't end up with the same random numbers as a
  124. concurrently running test binary */
  125. srand(seed());
  126. }