-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_signal_handler.cpp
76 lines (63 loc) · 1.63 KB
/
test_signal_handler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <chrono>
#include <csignal>
#include <cstring>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
class SignalHandler
{
private:
std::mutex m_sig_mtx;
SignalHandler() = default;
public:
SignalHandler(SignalHandler &other) = delete;
SignalHandler(SignalHandler &&other) = delete;
SignalHandler &operator=(SignalHandler &other) = delete;
SignalHandler &operator=(SignalHandler &&other) = delete;
static SignalHandler &
instance() {
static SignalHandler inst;
return inst;
}
static void
handle_signal([[maybe_unused]] int sig_num,
[[maybe_unused]] siginfo_t *sig_inf,
[[maybe_unused]] void *args) {
using namespace std::chrono_literals;
{
std::scoped_lock sl{instance().m_sig_mtx};
std::cout << "Thread " << std::this_thread::get_id() << " caught signal "
<< sig_num << " at address " << sig_inf->si_addr << std::endl;
}
volatile bool stop = false;
while (!stop) {
std::this_thread::sleep_for(1min);
}
}
static void register_handler() {
struct sigaction sa;
std::memset(&sa, 0, sizeof(struct sigaction));
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = SignalHandler::handle_signal;
sa.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &sa, nullptr);
}
};
void
cause_sigsegv() {
volatile int *ptr = nullptr;
*ptr = 1;
}
int
main() {
SignalHandler::register_handler();
std::vector<std::thread> threads;
for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) {
threads.emplace_back(cause_sigsegv);
}
for (std::thread &t : threads) {
t.join();
}
return 0;
}