/
ProfilerSignalManager.cpp
192 lines (156 loc) · 5.11 KB
/
ProfilerSignalManager.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc.
#include "ProfilerSignalManager.h"
#include <signal.h>
#include <sys/syscall.h>
#include "Log.h"
#include "OpSysTools.h"
ProfilerSignalManager::ProfilerSignalManager() noexcept :
_canReplaceSignalHandler{true},
_signalToSend{SIGUSR1},
_handler{nullptr},
_processId{OpSysTools::GetProcId()},
_isHandlerInPlace{false},
_previousAction{},
_handlerRegisterMutex{}
{
}
ProfilerSignalManager::~ProfilerSignalManager() noexcept
{
if (_isHandlerInPlace)
{
_isHandlerInPlace = false;
sigaction(_signalToSend, &_previousAction, nullptr);
}
_handler = nullptr;
}
ProfilerSignalManager* ProfilerSignalManager::Get()
{
static ProfilerSignalManager signalManager{};
return &signalManager;
}
bool ProfilerSignalManager::RegisterHandler(HandlerFn_t handler)
{
HandlerFn_t current = _handler;
if (current != nullptr)
{
assert(current == handler);
return current == handler;
}
std::unique_lock<std::mutex> lock(_handlerRegisterMutex);
if (current != nullptr)
{
assert(current == handler);
return current == handler;
}
_isHandlerInPlace = SetupSignalHandler();
if (_isHandlerInPlace)
{
_handler = handler;
}
return _isHandlerInPlace;
}
std::int32_t ProfilerSignalManager::SendSignal(pid_t threadId)
{
#ifndef NDEBUG
Log::Debug("ProfilerSignalManager::CollectStackSampleImplementation: Sending signal ",
_signalToSend, " to thread with threadId=", threadId, ".");
#endif
return syscall(SYS_tgkill, _processId, threadId, _signalToSend);
}
bool ProfilerSignalManager::CheckSignalHandler()
{
if (!_canReplaceSignalHandler)
{
static bool alreadyLogged = false;
if (alreadyLogged)
return false;
alreadyLogged = true;
_isHandlerInPlace = false;
Log::Warn("Profiler signal handler was replaced again. As of now, it will not be restored: the profiler is disabled.");
return false;
}
if (IsProfilerSignalHandlerInstalled())
{
return true;
}
Log::Info("Profiler signal handler has been replaced. Restoring it.");
// restore profiler handler
_isHandlerInPlace = SetupSignalHandler();
if (!_isHandlerInPlace)
{
Log::Warn("Fail to restore profiler signal handler.");
}
_canReplaceSignalHandler = false;
return _isHandlerInPlace;
}
bool ProfilerSignalManager::IsProfilerSignalHandlerInstalled()
{
struct sigaction currentAction;
sigaction(_signalToSend, nullptr, ¤tAction);
return (currentAction.sa_flags & SA_SIGINFO) == SA_SIGINFO &&
currentAction.sa_sigaction == ProfilerSignalManager::SignalHandler;
}
bool ProfilerSignalManager::IsHandlerInPlace() const
{
return _isHandlerInPlace;
}
bool ProfilerSignalManager::SetupSignalHandler()
{
struct sigaction sampleAction;
sampleAction.sa_flags = SA_RESTART | SA_SIGINFO;
sampleAction.sa_sigaction = ProfilerSignalManager::SignalHandler;
sigemptyset(&sampleAction.sa_mask);
sigaddset(&sampleAction.sa_mask, _signalToSend);
int32_t result = sigaction(_signalToSend, &sampleAction, &_previousAction);
if (result != 0)
{
Log::Error("ProfilerSignalManager::SetupSignalHandler: Failed to setup signal handler for SIGUSR1 signals. Reason: ",
strerror(errno), ".");
return false;
}
Log::Info("ProfilerSignalManager::SetupSignalHandler: Successfully setup signal handler for SIGUSR1 signal.");
return true;
}
void ProfilerSignalManager::SignalHandler(int signal, siginfo_t* info, void* context)
{
auto* signalManager = Get();
if (!signalManager->CallCustomHandler(signal, info, context))
{
signalManager->CallOrignalHandler(signal, info, context);
}
}
bool ProfilerSignalManager::CallCustomHandler(int32_t signal, siginfo_t* info, void* context)
{
HandlerFn_t handler = _handler;
return handler != nullptr && handler(signal, info, context);
}
void ProfilerSignalManager::CallOrignalHandler(int32_t signal, siginfo_t* info, void* context)
{
// This thread local variable helps in detecting the case where the profiler handler and
// the previous handler are referencing/calling each other.
// As it's synchchronous, we can check if we already executed the previous handler and
// stop if it's the case.
static thread_local bool isExecuting = false;
if (isExecuting)
return;
isExecuting = true;
try
{
if ((_previousAction.sa_flags & SA_SIGINFO) == SA_SIGINFO && _previousAction.sa_sigaction != nullptr)
{
_previousAction.sa_sigaction(signal, info, context);
}
else
{
if (_previousAction.sa_handler != SIG_DFL && _previousAction.sa_handler != SIG_IGN)
{
_previousAction.sa_handler(signal);
}
}
}
catch (...)
{
}
isExecuting = false;
}