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
|
/* Copyright (c) 2011, 2025, 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 <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};
/**
This function will try to dump relevant debugging information to stderr and
dump a core image.
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
*/
void print_fatal_signal(int sig) {
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);
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");
my_safe_printf_stderr("Query (%p): ", thd->query().str);
// 1024 * 1024 * 1024 is the limit for max_allowed_packet
my_safe_puts_stderr(thd->query().str, std::min(size_t{1024 * 1024 * 1024},
thd->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
*/
extern "C" void handle_fatal_signal(int sig) {
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);
}
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);
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));
}
}
|