1
/* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA */
16
#include "my_global.h"
20
#include "my_stacktrace.h"
24
#define SIGNAL_FMT "exception 0x%x"
26
#define SIGNAL_FMT "signal %d"
30
We are handling signals in this file.
31
Any global variables we read should be 'volatile sig_atomic_t'
32
to guarantee that we read some consistent value.
34
static volatile sig_atomic_t segfaulted= 0;
35
extern ulong max_used_connections;
36
extern volatile sig_atomic_t calling_initgroups;
38
extern volatile sig_atomic_t ld_assume_kernel_is_set;
42
* Handler for fatal signals
44
* Fatal events (seg.fault, bus error etc.) will trigger
45
* this signal handler. The handler will try to dump relevant
46
* debugging information to stderr and dump a core image.
48
* Signal handlers can only use a set of 'safe' system calls
49
* and library functions. A list of safe calls in POSIX systems
51
* http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html
52
* For MS Windows, guidelines are available at:
53
* http://msdn.microsoft.com/en-us/library/xdkz3x12(v=vs.71).aspx
55
* @param sig Signal number
57
extern "C" sig_handler handle_fatal_signal(int sig)
61
my_safe_printf_stderr("Fatal " SIGNAL_FMT " while backtracing\n", sig);
62
_exit(1); /* Quit without running destructors */
69
GetSystemTime(&utc_time);
70
const long hrs= utc_time.wHour;
71
const long mins= utc_time.wMinute;
72
const long secs= utc_time.wSecond;
74
/* Using time() instead of my_time() to avoid looping */
75
const time_t curr_time= time(NULL);
76
/* Calculate time of day */
77
const long tmins = curr_time / 60;
78
const long thrs = tmins / 60;
79
const long hrs = thrs % 24;
80
const long mins = tmins % 60;
81
const long secs = curr_time % 60;
84
char hrs_buf[3]= "00";
85
char mins_buf[3]= "00";
86
char secs_buf[3]= "00";
87
my_safe_itoa(10, hrs, &hrs_buf[2]);
88
my_safe_itoa(10, mins, &mins_buf[2]);
89
my_safe_itoa(10, secs, &secs_buf[2]);
91
my_safe_printf_stderr("%s:%s:%s UTC - mysqld got " SIGNAL_FMT " ;\n",
92
hrs_buf, mins_buf, secs_buf, sig);
94
my_safe_printf_stderr("%s",
95
"This could be because you hit a bug. It is also possible that this binary\n"
96
"or one of the libraries it was linked against is corrupt, improperly built,\n"
97
"or misconfigured. This error can also be caused by malfunctioning hardware.\n");
99
my_safe_printf_stderr("%s",
100
"We will try our best to scrape up some info that will hopefully help\n"
101
"diagnose the problem, but since we have already crashed, \n"
102
"something is definitely wrong and this may fail.\n\n");
104
my_safe_printf_stderr("key_buffer_size=%lu\n",
105
(ulong) dflt_key_cache->key_cache_mem_size);
107
my_safe_printf_stderr("read_buffer_size=%ld\n",
108
(long) global_system_variables.read_buff_size);
110
my_safe_printf_stderr("max_used_connections=%lu\n",
111
(ulong) max_used_connections);
113
my_safe_printf_stderr("max_threads=%u\n",
114
(uint) thread_scheduler->max_threads);
116
my_safe_printf_stderr("thread_count=%u\n", (uint) thread_count);
118
my_safe_printf_stderr("connection_count=%u\n", (uint) connection_count);
120
my_safe_printf_stderr("It is possible that mysqld could use up to \n"
122
"(read_buffer_size + sort_buffer_size)*max_threads = "
123
"%lu K bytes of memory\n",
124
((ulong) dflt_key_cache->key_cache_mem_size +
125
(global_system_variables.read_buff_size +
126
global_system_variables.sortbuff_size) *
127
thread_scheduler->max_threads +
128
max_connections * sizeof(THD)) / 1024);
130
my_safe_printf_stderr("%s",
131
"Hope that's ok; if not, decrease some variables in the equation.\n\n");
133
#if defined(HAVE_LINUXTHREADS)
134
if (sizeof(char*) == 4 && thread_count > UNSAFE_DEFAULT_LINUX_THREADS)
136
my_safe_printf_stderr(
137
"You seem to be running 32-bit Linux and have "
138
"%d concurrent connections.\n"
139
"If you have not changed STACK_SIZE in LinuxThreads "
140
"and built the binary \n"
141
"yourself, LinuxThreads is quite likely to steal "
142
"a part of the global heap for\n"
143
"the thread stack. Please read "
144
"http://dev.mysql.com/doc/mysql/en/linux-installation.html\n\n"
147
#endif /* HAVE_LINUXTHREADS */
149
#ifdef HAVE_STACKTRACE
150
THD *thd=current_thd;
152
if (!(test_flags & TEST_NO_STACKTRACE))
154
my_safe_printf_stderr("Thread pointer: 0x%p\n", thd);
155
my_safe_printf_stderr("%s",
156
"Attempting backtrace. You can use the following "
157
"information to find out\n"
158
"where mysqld died. If you see no messages after this, something went\n"
159
"terribly wrong...\n");
160
my_print_stacktrace(thd ? (uchar*) thd->thread_stack : NULL,
161
my_thread_stack_size);
165
const char *kreason= "UNKNOWN";
166
switch (thd->killed) {
167
case THD::NOT_KILLED:
168
kreason= "NOT_KILLED";
170
case THD::KILL_BAD_DATA:
171
kreason= "KILL_BAD_DATA";
173
case THD::KILL_CONNECTION:
174
kreason= "KILL_CONNECTION";
176
case THD::KILL_QUERY:
177
kreason= "KILL_QUERY";
179
case THD::KILLED_NO_VALUE:
180
kreason= "KILLED_NO_VALUE";
183
my_safe_printf_stderr("%s", "\n"
184
"Trying to get some variables.\n"
185
"Some pointers may be invalid and cause the dump to abort.\n");
187
my_safe_printf_stderr("Query (%p): ", thd->query());
188
my_safe_print_str(thd->query(), min(1024U, thd->query_length()));
189
my_safe_printf_stderr("Connection ID (thread ID): %lu\n",
190
(ulong) thd->thread_id);
191
my_safe_printf_stderr("Status: %s\n\n", kreason);
193
my_safe_printf_stderr("%s",
194
"The manual page at "
195
"http://dev.mysql.com/doc/mysql/en/crashing.html contains\n"
196
"information that should help you find out what is causing the crash.\n");
198
#endif /* HAVE_STACKTRACE */
200
#ifdef HAVE_INITGROUPS
201
if (calling_initgroups)
203
my_safe_printf_stderr("%s", "\n"
204
"This crash occured while the server was calling initgroups(). This is\n"
205
"often due to the use of a mysqld that is statically linked against \n"
206
"glibc and configured to use LDAP in /etc/nsswitch.conf.\n"
207
"You will need to either upgrade to a version of glibc that does not\n"
208
"have this problem (2.3.4 or later when used with nscd),\n"
209
"disable LDAP in your nsswitch.conf, or use a "
210
"mysqld that is not statically linked.\n");
215
if (thd_lib_detected == THD_LIB_LT && !ld_assume_kernel_is_set)
217
my_safe_printf_stderr("%s",
218
"You are running a statically-linked LinuxThreads binary on an NPTL\n"
219
"system. This can result in crashes on some distributions due to "
220
"LT/NPTL conflicts.\n"
221
"You should either build a dynamically-linked binary, "
222
"or force LinuxThreads\n"
223
"to be used with the LD_ASSUME_KERNEL environment variable.\n"
224
"Please consult the documentation for your distribution "
225
"on how to do that.\n");
229
if (locked_in_memory)
231
my_safe_printf_stderr("%s", "\n"
232
"The \"--memlock\" argument, which was enabled, "
233
"uses system calls that are\n"
234
"unreliable and unstable on some operating systems and "
235
"operating-system versions (notably, some versions of Linux).\n"
236
"This crash could be due to use of those buggy OS calls.\n"
237
"You should consider whether you really need the "
238
"\"--memlock\" parameter and/or consult the OS distributer about "
239
"\"mlockall\" bugs.\n");
242
#ifdef HAVE_WRITE_CORE
243
if (test_flags & TEST_CORE_ON_SIGNAL)
245
my_safe_printf_stderr("%s", "Writing a core file\n");
252
Quit, without running destructors (etc.)
253
On Windows, do not terminate, but pass control to exception filter.
255
_exit(1); // Using _exit(), since exit() is not async signal safe