|
25 | 25 | #include <boost/core/lightweight_test.hpp> |
26 | 26 | #include <boost/config.hpp> |
27 | 27 |
|
| 28 | +#if defined(__OpenBSD__) |
| 29 | + |
| 30 | +#include <signal.h> |
| 31 | +#include <unistd.h> |
| 32 | +#include <spawn.h> |
| 33 | + |
| 34 | +class signal_handler |
| 35 | +{ |
| 36 | +public: |
| 37 | + signal_handler() |
| 38 | + { |
| 39 | + struct sigaction act = {}; |
| 40 | + act.sa_flags = SA_SIGINFO | SA_RESTART; |
| 41 | + sigemptyset(&act.sa_mask); |
| 42 | + sigaddset(&act.sa_mask, SIGSEGV); |
| 43 | + sigaddset(&act.sa_mask, SIGFPE); |
| 44 | + sigaddset(&act.sa_mask, SIGILL); |
| 45 | + |
| 46 | + act.sa_sigaction = &signal_handler::on_signal; |
| 47 | + |
| 48 | + sigaction(SIGSEGV, &act, nullptr); |
| 49 | + sigaction(SIGFPE, &act, nullptr); |
| 50 | + sigaction(SIGILL, &act, nullptr); |
| 51 | + } |
| 52 | + |
| 53 | +private: |
| 54 | + static void on_signal(int sig, siginfo_t* sig_info, void* context) |
| 55 | + { |
| 56 | + char attach_command_arg[128] = "--eval-command=attach "; |
| 57 | + const char* cmdline[] = |
| 58 | + { |
| 59 | + "/usr/bin/gdb", |
| 60 | + "--batch", |
| 61 | + "--eval-command=set pagination off", |
| 62 | + attach_command_arg, |
| 63 | + "--eval-command=info thread", |
| 64 | + "--eval-command=thread apply all bt", |
| 65 | + "--eval-command=kill", |
| 66 | + nullptr |
| 67 | + }; |
| 68 | + |
| 69 | + pid_t self_pid = getpid(); |
| 70 | + print_pid(attach_command_arg + 22, self_pid); |
| 71 | + |
| 72 | + int res = posix_spawn(nullptr, cmdline[0], nullptr, nullptr, const_cast< char* const* >(cmdline), nullptr); |
| 73 | + if (res == 0) |
| 74 | + { |
| 75 | + kill(self_pid, SIGSTOP); |
| 76 | + } |
| 77 | + |
| 78 | + std::abort(); |
| 79 | + } |
| 80 | + |
| 81 | + static char* print_pid(char* p, unsigned int pid) |
| 82 | + { |
| 83 | + char* b = p; |
| 84 | + do |
| 85 | + { |
| 86 | + *p = '0' + pid % 10u; |
| 87 | + pid /= 10u; |
| 88 | + ++p; |
| 89 | + } |
| 90 | + while (pid > 0); |
| 91 | + |
| 92 | + char* const res = p; |
| 93 | + *p-- = '\0'; |
| 94 | + |
| 95 | + while (p > b) |
| 96 | + { |
| 97 | + char c = *b; |
| 98 | + *b = *p; |
| 99 | + *p = c; |
| 100 | + ++b; |
| 101 | + --p; |
| 102 | + } |
| 103 | + |
| 104 | + return res; |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +static signal_handler g_sig_handler; |
| 109 | + |
| 110 | +#endif // defined(__OpenBSD__) |
| 111 | + |
28 | 112 | thread_local unsigned throw_one = 0xFFFF; |
29 | 113 | thread_local unsigned operator_new_recursion_level = 0u; |
30 | 114 |
|
|
0 commit comments