-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsignal_handler.cc
473 lines (427 loc) · 14.6 KB
/
signal_handler.cc
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* Copyright (c) 2011, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "my_config.h"
#include "sql/signal_handler.h"
#include <signal.h>
#include <sys/types.h>
#include <time.h>
#include <algorithm>
#include <atomic>
#include "lex_string.h"
#include "my_inttypes.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "my_macros.h"
#include "my_stacktrace.h"
#include "my_sys.h"
#include "my_time.h"
#include "sql/mysqld.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#ifdef _WIN32
#include <crtdbg.h>
#define SIGNAL_FMT "exception 0x%x"
#else
#define SIGNAL_FMT "signal %d"
#endif
/*
We are handling signals in this file.
Any global variables we read should be 'volatile sig_atomic_t' or lock-free
std::atomic.
*/
/**
This is used to check if the signal handler is not called again while already
handling the previous signal, as may happen when either another thread
triggers it, or a bug in handling causes abort again.
*/
static std::atomic<bool> s_handler_being_processed{false};
/**
Used to remember if the fatal info was already printed. The info can be
printed from user threads, but in the fatal signal handler we want to print it
if and only if the info was not yet printed. User threads after printing the
info will call abort which will call the handler.
*/
static std::atomic<bool> s_fatal_info_printed{false};
#ifndef _WIN32
static void print_extra_signal_information(int sig, siginfo_t *info) {
if (info == nullptr) {
return;
}
// SIGILL, SIGFPE, SIGSEGV, SIGBUS, and SIGTRAP fill in si_addr
// All the si_code constants below are enum values (and macro defines).
// si_code itself is an int, so we should never see something like:
// error: enumeration value ... handled in switch [-Werror=switch]
const char *signal_name = nullptr;
const char *extra_info = "unknown siginfo_t::si_code";
switch (sig) {
case SIGILL:
signal_name = "SIGILL";
switch (info->si_code) {
case ILL_ILLOPC:
extra_info = "Illegal opcode";
break;
case ILL_ILLOPN:
extra_info = "Illegal operand";
break;
case ILL_ILLADR:
extra_info = "Illegal addressing mode";
break;
case ILL_ILLTRP:
extra_info = "Illegal trap";
break;
case ILL_PRVOPC:
extra_info = "Privileged opcode";
break;
case ILL_PRVREG:
extra_info = "Privileged register";
break;
case ILL_COPROC:
extra_info = "Coprocessor error";
break;
case ILL_BADSTK:
extra_info = "Internal stack error";
break;
#ifdef ILL_BADIADDR
case ILL_BADIADDR:
extra_info = "Unimplemented instruction address";
break;
#endif
}
break;
case SIGFPE:
signal_name = "SIGFPE";
switch (info->si_code) {
case FPE_INTDIV:
extra_info = "Integer divide by zero";
break;
case FPE_INTOVF:
extra_info = "Integer overflow";
break;
case FPE_FLTDIV:
extra_info = "Floating point divide by zero";
break;
case FPE_FLTOVF:
extra_info = "Floating point overflow";
break;
case FPE_FLTUND:
extra_info = "Floating point underflow";
break;
case FPE_FLTRES:
extra_info = "Floating point inexact result";
break;
case FPE_FLTINV:
extra_info = "Floating point invalid operation";
break;
case FPE_FLTSUB:
extra_info = "Subscript out of range";
break;
#ifdef FPE_FLTUNK
case FPE_FLTUNK:
extra_info = "Undiagnosed floating-point exception";
break;
#endif
#ifdef FPE_CONDTRAP
case FPE_CONDTRAP:
extra_info = "Trap on condition";
break;
#endif
}
break;
case SIGSEGV:
signal_name = "SIGSEGV";
switch (info->si_code) {
case SEGV_MAPERR:
extra_info = "Address not mapped to object";
break;
case SEGV_ACCERR:
extra_info = "Invalid permissions for mapped object";
break;
}
break;
case SIGBUS:
signal_name = "SIGBUS";
switch (info->si_code) {
case BUS_ADRALN:
extra_info = "Invalid address alignment";
break;
case BUS_ADRERR:
extra_info = "Non-existant physical address";
break;
case BUS_OBJERR:
extra_info = "Object specific hardware error";
break;
#ifdef BUS_MCEERR_AR
case BUS_MCEERR_AR:
extra_info = "Hardware memory error: action required";
break;
#endif
#ifdef BUS_MCEERR_AO
case BUS_MCEERR_AO:
extra_info = "Hardware memory error: action optional";
break;
#endif
}
break;
case SIGTRAP:
signal_name = "SIGTRAP";
switch (info->si_code) {
case TRAP_BRKPT:
extra_info = "Process breakpoint";
break;
case TRAP_TRACE:
extra_info = "Process trace trap";
break;
#ifdef TRAP_BRANCH
case TRAP_BRANCH:
extra_info = "Process taken branch trap";
break;
#endif
#ifdef TRAP_HWBKPT
case TRAP_HWBKPT:
extra_info = "Hardware breakpoint/watchpoint";
break;
#endif
#ifdef TRAP_UNK
case TRAP_UNK:
extra_info = "Undiagnosed trap";
break;
#endif
}
break;
}
if (signal_name != nullptr) {
my_safe_printf_stderr("Signal %s (%s) at address 0x%p\n", signal_name,
extra_info, info->si_addr);
if (sig == SIGFPE) {
}
}
}
#endif // not _WIN32
std::atomic<my_signal_handler_callback_t> g_fatal_callback;
/**
This function will try to dump relevant debugging information to stderr.
It may be called as part of the signal handler. This fact limits library calls
that we can perform and much more, @see handle_fatal_signal
@param sig Signal number
@param info Misc. information about the signal, see man sigaction(2).
*/
static void print_fatal_signal(int sig, siginfo_t *info [[maybe_unused]]) {
s_fatal_info_printed = true;
#ifdef _WIN32
SYSTEMTIME utc_time;
GetSystemTime(&utc_time);
const long year = utc_time.wYear;
const long month = utc_time.wMonth;
const long day = utc_time.wDay;
const long hrs = utc_time.wHour;
const long mins = utc_time.wMinute;
const long secs = utc_time.wSecond;
#else
/* Using time() instead of my_time() to avoid looping */
const time_t curr_time = time(nullptr);
// Offset for the UNIX epoch.
const ulong days_at_timestart = 719528;
/* Calculate time of day */
const long total_mins = curr_time / 60;
const long total_hrs = total_mins / 60;
const long total_days = (total_hrs / 24) + days_at_timestart;
const long hrs = total_hrs % 24;
const long mins = total_mins % 60;
const long secs = curr_time % 60;
uint year, month, day;
get_date_from_daynr(total_days, &year, &month, &day);
#endif
char hrs_buf[3] = "00";
char mins_buf[3] = "00";
char secs_buf[3] = "00";
my_safe_itoa(10, hrs, &hrs_buf[2]);
my_safe_itoa(10, mins, &mins_buf[2]);
my_safe_itoa(10, secs, &secs_buf[2]);
char year_buf[5] = "0000";
char month_buf[3] = "00";
char day_buf[3] = "00";
my_safe_itoa(10, year, &year_buf[4]);
my_safe_itoa(10, month, &month_buf[2]);
my_safe_itoa(10, day, &day_buf[2]);
my_safe_printf_stderr(
"%s-%s-%sT%s:%s:%sZ UTC - mysqld got " SIGNAL_FMT " ;\n", year_buf,
month_buf, day_buf, hrs_buf, mins_buf, secs_buf, sig);
#ifndef _WIN32
print_extra_signal_information(sig, info);
#endif
my_safe_printf_stderr(
"%s",
"Most likely, you have hit a bug, but this error can also "
"be caused by malfunctioning hardware.\n");
#if defined(HAVE_BUILD_ID_SUPPORT)
my_safe_printf_stderr("BuildID[sha1]=%s\n", server_build_id);
#endif
#ifdef HAVE_STACKTRACE
THD *thd = current_thd;
if (!(test_flags & TEST_NO_STACKTRACE)) {
my_safe_printf_stderr("Thread pointer: 0x%p\n", thd);
my_safe_printf_stderr(
"%s",
"Attempting backtrace. You can use the following "
"information to find out\n"
"where mysqld died. If you see no messages after this, something went\n"
"terribly wrong...\n");
my_print_stacktrace(
thd ? pointer_cast<const uchar *>(thd->thread_stack) : nullptr,
my_thread_stack_size);
}
if (thd) {
const char *kreason = "UNKNOWN";
switch (thd->killed.load()) {
case THD::NOT_KILLED:
kreason = "NOT_KILLED";
break;
case THD::KILL_CONNECTION:
kreason = "KILL_CONNECTION";
break;
case THD::KILL_QUERY:
kreason = "KILL_QUERY";
break;
case THD::KILL_TIMEOUT:
kreason = "KILL_TIMEOUT";
break;
case THD::KILLED_NO_VALUE:
kreason = "KILLED_NO_VALUE";
break;
}
my_safe_printf_stderr(
"%s",
"\n"
"Trying to get some variables.\n"
"Some pointers may be invalid and cause the dump to abort.\n");
const char *query = thd->rewritten_query().ptr();
size_t query_length = thd->rewritten_query().length();
if (query_length == 0) {
query = thd->query().str;
query_length = thd->query().length;
}
my_safe_printf_stderr("Query (%p): ", query);
my_safe_puts_stderr(query, std::min(size_t{1024}, query_length));
my_safe_printf_stderr("Connection ID (thread ID): %u\n", thd->thread_id());
my_safe_printf_stderr("Status: %s\n\n", kreason);
}
my_safe_printf_stderr(
"%s",
"The manual page at "
"http://dev.mysql.com/doc/mysql/en/crashing.html contains\n"
"information that should help you find out what is causing the crash.\n");
#endif /* HAVE_STACKTRACE */
}
/**
Handler for fatal signals
Fatal events (seg.fault, bus error etc.) will trigger this signal handler. The
handler will try to dump relevant debugging information to stderr and dump a
core image.
Signal handlers can only use a set of 'safe' system calls and library
functions.
- A list of safe calls in POSIX systems are available at:
http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html
- For MS Windows, guidelines are available in documentation of the `signal()`
function:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/signal?view=msvc-160
@param sig Signal number
@param info Misc. information about the signal, see man sigaction(2).
@param ucontext Pointer to a ucontext_t structure.
"Commonly, the handler function doesn't make any use of the third argument."
*/
void handle_fatal_signal(int sig, siginfo_t *info [[maybe_unused]],
void *ucontext [[maybe_unused]]) {
if (s_handler_being_processed) {
my_safe_printf_stderr("Fatal " SIGNAL_FMT " while backtracing\n", sig);
_exit(MYSQLD_FAILURE_EXIT); /* Quit without running destructors */
}
s_handler_being_processed = true;
if (!s_fatal_info_printed) {
print_fatal_signal(sig, info);
}
if (g_fatal_callback.load() != nullptr)
(*g_fatal_callback)(sig, info, ucontext);
if ((test_flags & TEST_CORE_ON_SIGNAL) != 0) {
my_safe_printf_stderr("%s", "Writing a core file\n");
my_write_core(sig);
}
#ifndef _WIN32
/*
Quit, without running destructors (etc.)
On Windows, do not terminate, but pass control to exception filter.
*/
_exit(MYSQLD_FAILURE_EXIT); // Using _exit(), since exit() is not async
// signal safe
#endif
}
/**
This is a wrapper around abort() which ensures that abort() will be called
exactly once, as calling it more than once might cause following problems:
When original abort() is called there is a signal processing triggered, but
only the first abort() causes the signal handler to be called, all other
abort()s called by the other threads will cause immediate exit() call, which
will also terminate the first abort() processing within the signal handler,
aborting stacktrace printing, core writeout or any other processing.
*/
void my_server_abort() {
static std::atomic_int aborts_pending{0};
static std::atomic_bool abort_processing{false};
/* Broadcast that this thread wants to print the signal info. */
aborts_pending++;
/*
Wait for the exclusive right to print the signal info. This assures the
output is not interleaved.
*/
while (abort_processing.exchange(true)) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
/*
This actually takes some time, some or many other threads may call
my_server_abort in meantime.
*/
print_fatal_signal(SIGABRT, nullptr);
abort_processing = false;
/*
If there are no other threads pending abort then we call real abort as the
last aborting thread. If that succeeds, we are left with a positive
`aborts_pending`, and it will never go down to zero again. This effectively
prevents any other thread from calling real `abort`.
*/
auto left = --aborts_pending;
if (!left && aborts_pending.compare_exchange_strong(left, 1)) {
/*
Wait again for the exclusive right to print the signal info by calling
the real `abort`. This assures the output is not interleaved with any
printing from a few lines above, that could start in the meantime.
*/
while (abort_processing.exchange(true)) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
abort();
}
/*
Abort can't return, we will sleep here forever - the algorithm above
assures exactly one thread, eventually, will call `abort()` and terminate
the whole program.
*/
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}