MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
signal_handler.cc
1 /* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2 
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.
6 
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.
11 
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 */
15 
16 #include "my_global.h"
17 #include <signal.h>
18 
19 #include "sys_vars.h"
20 #include "my_stacktrace.h"
21 #include "global_threads.h"
22 
23 #ifdef __WIN__
24 #include <crtdbg.h>
25 #define SIGNAL_FMT "exception 0x%x"
26 #else
27 #define SIGNAL_FMT "signal %d"
28 #endif
29 
30 /*
31  We are handling signals in this file.
32  Any global variables we read should be 'volatile sig_atomic_t'
33  to guarantee that we read some consistent value.
34  */
35 static volatile sig_atomic_t segfaulted= 0;
36 extern ulong max_used_connections;
37 extern volatile sig_atomic_t calling_initgroups;
38 #ifdef HAVE_NPTL
39 extern volatile sig_atomic_t ld_assume_kernel_is_set;
40 #endif
41 
58 extern "C" sig_handler handle_fatal_signal(int sig)
59 {
60  if (segfaulted)
61  {
62  my_safe_printf_stderr("Fatal " SIGNAL_FMT " while backtracing\n", sig);
63  _exit(1); /* Quit without running destructors */
64  }
65 
66  segfaulted = 1;
67 
68 #ifdef __WIN__
69  SYSTEMTIME utc_time;
70  GetSystemTime(&utc_time);
71  const long hrs= utc_time.wHour;
72  const long mins= utc_time.wMinute;
73  const long secs= utc_time.wSecond;
74 #else
75  /* Using time() instead of my_time() to avoid looping */
76  const time_t curr_time= time(NULL);
77  /* Calculate time of day */
78  const long tmins = curr_time / 60;
79  const long thrs = tmins / 60;
80  const long hrs = thrs % 24;
81  const long mins = tmins % 60;
82  const long secs = curr_time % 60;
83 #endif
84 
85  char hrs_buf[3]= "00";
86  char mins_buf[3]= "00";
87  char secs_buf[3]= "00";
88  my_safe_itoa(10, hrs, &hrs_buf[2]);
89  my_safe_itoa(10, mins, &mins_buf[2]);
90  my_safe_itoa(10, secs, &secs_buf[2]);
91 
92  my_safe_printf_stderr("%s:%s:%s UTC - mysqld got " SIGNAL_FMT " ;\n",
93  hrs_buf, mins_buf, secs_buf, sig);
94 
95  my_safe_printf_stderr("%s",
96  "This could be because you hit a bug. It is also possible that this binary\n"
97  "or one of the libraries it was linked against is corrupt, improperly built,\n"
98  "or misconfigured. This error can also be caused by malfunctioning hardware.\n");
99 
100  my_safe_printf_stderr("%s",
101  "We will try our best to scrape up some info that will hopefully help\n"
102  "diagnose the problem, but since we have already crashed, \n"
103  "something is definitely wrong and this may fail.\n\n");
104 
105  my_safe_printf_stderr("key_buffer_size=%lu\n",
106  (ulong) dflt_key_cache->key_cache_mem_size);
107 
108  my_safe_printf_stderr("read_buffer_size=%ld\n",
109  (long) global_system_variables.read_buff_size);
110 
111  my_safe_printf_stderr("max_used_connections=%lu\n",
112  (ulong) max_used_connections);
113 
114  my_safe_printf_stderr("max_threads=%u\n",
115  (uint) thread_scheduler->max_threads);
116 
117  my_safe_printf_stderr("thread_count=%u\n", get_thread_count());
118 
119  my_safe_printf_stderr("connection_count=%u\n", (uint) connection_count);
120 
121  my_safe_printf_stderr("It is possible that mysqld could use up to \n"
122  "key_buffer_size + "
123  "(read_buffer_size + sort_buffer_size)*max_threads = "
124  "%lu K bytes of memory\n",
125  ((ulong) dflt_key_cache->key_cache_mem_size +
126  (global_system_variables.read_buff_size +
127  global_system_variables.sortbuff_size) *
128  thread_scheduler->max_threads +
129  max_connections * sizeof(THD)) / 1024);
130 
131  my_safe_printf_stderr("%s",
132  "Hope that's ok; if not, decrease some variables in the equation.\n\n");
133 
134 #if defined(HAVE_LINUXTHREADS)
135 #define UNSAFE_DEFAULT_LINUX_THREADS 200
136  if (sizeof(char*) == 4 && thread_count > UNSAFE_DEFAULT_LINUX_THREADS)
137  {
138  my_safe_printf_stderr(
139  "You seem to be running 32-bit Linux and have "
140  "%d concurrent connections.\n"
141  "If you have not changed STACK_SIZE in LinuxThreads "
142  "and built the binary \n"
143  "yourself, LinuxThreads is quite likely to steal "
144  "a part of the global heap for\n"
145  "the thread stack. Please read "
146  "http://dev.mysql.com/doc/mysql/en/linux-installation.html\n\n"
147  thread_count);
148  }
149 #endif /* HAVE_LINUXTHREADS */
150 
151 #ifdef HAVE_STACKTRACE
152  THD *thd=current_thd;
153 
154  if (!(test_flags & TEST_NO_STACKTRACE))
155  {
156  my_safe_printf_stderr("Thread pointer: 0x%p\n", thd);
157  my_safe_printf_stderr("%s",
158  "Attempting backtrace. You can use the following "
159  "information to find out\n"
160  "where mysqld died. If you see no messages after this, something went\n"
161  "terribly wrong...\n");
162  my_print_stacktrace(thd ? (uchar*) thd->thread_stack : NULL,
163  my_thread_stack_size);
164  }
165  if (thd)
166  {
167  const char *kreason= "UNKNOWN";
168  switch (thd->killed) {
169  case THD::NOT_KILLED:
170  kreason= "NOT_KILLED";
171  break;
172  case THD::KILL_BAD_DATA:
173  kreason= "KILL_BAD_DATA";
174  break;
175  case THD::KILL_CONNECTION:
176  kreason= "KILL_CONNECTION";
177  break;
178  case THD::KILL_QUERY:
179  kreason= "KILL_QUERY";
180  break;
181  case THD::KILLED_NO_VALUE:
182  kreason= "KILLED_NO_VALUE";
183  break;
184  }
185  my_safe_printf_stderr("%s", "\n"
186  "Trying to get some variables.\n"
187  "Some pointers may be invalid and cause the dump to abort.\n");
188 
189  my_safe_printf_stderr("Query (%p): ", thd->query());
190  my_safe_print_str(thd->query(), MY_MIN(1024U, thd->query_length()));
191  my_safe_printf_stderr("Connection ID (thread ID): %lu\n",
192  (ulong) thd->thread_id);
193  my_safe_printf_stderr("Status: %s\n\n", kreason);
194  }
195  my_safe_printf_stderr("%s",
196  "The manual page at "
197  "http://dev.mysql.com/doc/mysql/en/crashing.html contains\n"
198  "information that should help you find out what is causing the crash.\n");
199 
200 #endif /* HAVE_STACKTRACE */
201 
202 #ifdef HAVE_INITGROUPS
203  if (calling_initgroups)
204  {
205  my_safe_printf_stderr("%s", "\n"
206  "This crash occured while the server was calling initgroups(). This is\n"
207  "often due to the use of a mysqld that is statically linked against \n"
208  "glibc and configured to use LDAP in /etc/nsswitch.conf.\n"
209  "You will need to either upgrade to a version of glibc that does not\n"
210  "have this problem (2.3.4 or later when used with nscd),\n"
211  "disable LDAP in your nsswitch.conf, or use a "
212  "mysqld that is not statically linked.\n");
213  }
214 #endif
215 
216 #ifdef HAVE_NPTL
217  if (thd_lib_detected == THD_LIB_LT && !ld_assume_kernel_is_set)
218  {
219  my_safe_printf_stderr("%s",
220  "You are running a statically-linked LinuxThreads binary on an NPTL\n"
221  "system. This can result in crashes on some distributions due to "
222  "LT/NPTL conflicts.\n"
223  "You should either build a dynamically-linked binary, "
224  "or force LinuxThreads\n"
225  "to be used with the LD_ASSUME_KERNEL environment variable.\n"
226  "Please consult the documentation for your distribution "
227  "on how to do that.\n");
228  }
229 #endif
230 
231  if (locked_in_memory)
232  {
233  my_safe_printf_stderr("%s", "\n"
234  "The \"--memlock\" argument, which was enabled, "
235  "uses system calls that are\n"
236  "unreliable and unstable on some operating systems and "
237  "operating-system versions (notably, some versions of Linux).\n"
238  "This crash could be due to use of those buggy OS calls.\n"
239  "You should consider whether you really need the "
240  "\"--memlock\" parameter and/or consult the OS distributer about "
241  "\"mlockall\" bugs.\n");
242  }
243 
244 #ifdef HAVE_WRITE_CORE
245  if (test_flags & TEST_CORE_ON_SIGNAL)
246  {
247  my_safe_printf_stderr("%s", "Writing a core file\n");
248  my_write_core(sig);
249  }
250 #endif
251 
252 #ifndef __WIN__
253  /*
254  Quit, without running destructors (etc.)
255  On Windows, do not terminate, but pass control to exception filter.
256  */
257  _exit(1); // Using _exit(), since exit() is not async signal safe
258 #endif
259 }