mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / sql / signal_handler.cc
blobae1fe5f5eb7278b86d7ba5e442910af83210abb2
1 /* Copyright (c) 2011, 2012, 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 02110-1301 USA */
16 #include "my_global.h"
17 #include <signal.h>
19 #include "mysql_priv.h"
20 #include "my_stacktrace.h"
22 #ifdef __WIN__
23 #include <crtdbg.h>
24 #define SIGNAL_FMT "exception 0x%x"
25 #else
26 #define SIGNAL_FMT "signal %d"
27 #endif
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;
37 #ifdef HAVE_NPTL
38 extern volatile sig_atomic_t ld_assume_kernel_is_set;
39 #endif
41 /**
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
50 * are available at:
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)
59 if (segfaulted)
61 my_safe_printf_stderr("Fatal " SIGNAL_FMT " while backtracing\n", sig);
62 _exit(1); /* Quit without running destructors */
65 segfaulted = 1;
67 #ifdef __WIN__
68 SYSTEMTIME utc_time;
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;
73 #else
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;
82 #endif
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"
121 "key_buffer_size + "
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 #define UNSAFE_DEFAULT_LINUX_THREADS 200
135 if (sizeof(char*) == 4 && thread_count > UNSAFE_DEFAULT_LINUX_THREADS)
137 my_safe_printf_stderr(
138 "You seem to be running 32-bit Linux and have "
139 "%d concurrent connections.\n"
140 "If you have not changed STACK_SIZE in LinuxThreads "
141 "and built the binary \n"
142 "yourself, LinuxThreads is quite likely to steal "
143 "a part of the global heap for\n"
144 "the thread stack. Please read "
145 "http://dev.mysql.com/doc/mysql/en/linux-installation.html\n\n"
146 thread_count);
148 #endif /* HAVE_LINUXTHREADS */
150 #ifdef HAVE_STACKTRACE
151 THD *thd=current_thd;
153 if (!(test_flags & TEST_NO_STACKTRACE))
155 my_safe_printf_stderr("Thread pointer: 0x%p\n", thd);
156 my_safe_printf_stderr("%s",
157 "Attempting backtrace. You can use the following "
158 "information to find out\n"
159 "where mysqld died. If you see no messages after this, something went\n"
160 "terribly wrong...\n");
161 my_print_stacktrace(thd ? (uchar*) thd->thread_stack : NULL,
162 my_thread_stack_size);
164 if (thd)
166 const char *kreason= "UNKNOWN";
167 switch (thd->killed) {
168 case THD::NOT_KILLED:
169 kreason= "NOT_KILLED";
170 break;
171 case THD::KILL_BAD_DATA:
172 kreason= "KILL_BAD_DATA";
173 break;
174 case THD::KILL_CONNECTION:
175 kreason= "KILL_CONNECTION";
176 break;
177 case THD::KILL_QUERY:
178 kreason= "KILL_QUERY";
179 break;
180 case THD::KILLED_NO_VALUE:
181 kreason= "KILLED_NO_VALUE";
182 break;
184 my_safe_printf_stderr("%s", "\n"
185 "Trying to get some variables.\n"
186 "Some pointers may be invalid and cause the dump to abort.\n");
188 my_safe_printf_stderr("Query (%p): ", thd->query());
189 my_safe_print_str(thd->query(), min(1024U, thd->query_length()));
190 my_safe_printf_stderr("Connection ID (thread ID): %lu\n",
191 (ulong) thd->thread_id);
192 my_safe_printf_stderr("Status: %s\n\n", kreason);
194 my_safe_printf_stderr("%s",
195 "The manual page at "
196 "http://dev.mysql.com/doc/mysql/en/crashing.html contains\n"
197 "information that should help you find out what is causing the crash.\n");
199 #endif /* HAVE_STACKTRACE */
201 #ifdef HAVE_INITGROUPS
202 if (calling_initgroups)
204 my_safe_printf_stderr("%s", "\n"
205 "This crash occured while the server was calling initgroups(). This is\n"
206 "often due to the use of a mysqld that is statically linked against \n"
207 "glibc and configured to use LDAP in /etc/nsswitch.conf.\n"
208 "You will need to either upgrade to a version of glibc that does not\n"
209 "have this problem (2.3.4 or later when used with nscd),\n"
210 "disable LDAP in your nsswitch.conf, or use a "
211 "mysqld that is not statically linked.\n");
213 #endif
215 #ifdef HAVE_NPTL
216 if (thd_lib_detected == THD_LIB_LT && !ld_assume_kernel_is_set)
218 my_safe_printf_stderr("%s",
219 "You are running a statically-linked LinuxThreads binary on an NPTL\n"
220 "system. This can result in crashes on some distributions due to "
221 "LT/NPTL conflicts.\n"
222 "You should either build a dynamically-linked binary, "
223 "or force LinuxThreads\n"
224 "to be used with the LD_ASSUME_KERNEL environment variable.\n"
225 "Please consult the documentation for your distribution "
226 "on how to do that.\n");
228 #endif
230 if (locked_in_memory)
232 my_safe_printf_stderr("%s", "\n"
233 "The \"--memlock\" argument, which was enabled, "
234 "uses system calls that are\n"
235 "unreliable and unstable on some operating systems and "
236 "operating-system versions (notably, some versions of Linux).\n"
237 "This crash could be due to use of those buggy OS calls.\n"
238 "You should consider whether you really need the "
239 "\"--memlock\" parameter and/or consult the OS distributer about "
240 "\"mlockall\" bugs.\n");
243 #ifdef HAVE_WRITE_CORE
244 if (test_flags & TEST_CORE_ON_SIGNAL)
246 my_safe_printf_stderr("%s", "Writing a core file\n");
247 my_write_core(sig);
249 #endif
251 #ifndef __WIN__
253 Quit, without running destructors (etc.)
254 On Windows, do not terminate, but pass control to exception filter.
256 _exit(1); // Using _exit(), since exit() is not async signal safe
257 #endif