~ubuntu-branches/ubuntu/raring/mysql-5.5/raring-proposed

« back to all changes in this revision

Viewing changes to sql/signal_handler.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-02-14 23:59:22 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120214235922-cux5uek1e5l0hje9
Tags: 5.5.20-0ubuntu1
* New upstream release.
* d/mysql-server-5.5.mysql.upstart: Fix stop on to make sure mysql is
  fully stopped before shutdown commences. (LP: #688541) Also simplify
  start on as it is redundant.
* d/control: Depend on upstart version which has apparmor profile load
  script to prevent failure on upgrade from lucid to precise.
  (LP: #907465)
* d/apparmor-profile: need to allow /run since that is the true path
  of /var/run files. (LP: #917542)
* d/control: mysql-server-5.5 has files in it that used to be owned
  by libmysqlclient-dev, so it must break/replace it. (LP: #912487)
* d/rules, d/control: 5.5.20 Fixes segfault on tests with gcc 4.6,
  change compiler back to system default.
* d/rules: Turn off embedded libedit/readline.(Closes: #659566)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2011, 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  02111-1307  USA */
 
15
 
 
16
#include "my_global.h"
 
17
#include <signal.h>
 
18
 
 
19
#include "sys_vars.h"
 
20
#include "my_stacktrace.h"
 
21
 
 
22
#ifdef __WIN__
 
23
#include <crtdbg.h>
 
24
#define SIGNAL_FMT "exception 0x%x"
 
25
#else
 
26
#define SIGNAL_FMT "signal %d"
 
27
#endif
 
28
 
 
29
/*
 
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.
 
33
 */
 
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
 
40
 
 
41
/**
 
42
 * Handler for fatal signals
 
43
 *
 
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.
 
47
 *
 
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
 
54
 *
 
55
 * @param sig Signal number
 
56
*/
 
57
extern "C" sig_handler handle_fatal_signal(int sig)
 
58
{
 
59
  if (segfaulted)
 
60
  {
 
61
    my_safe_printf_stderr("Fatal " SIGNAL_FMT " while backtracing\n", sig);
 
62
    _exit(1); /* Quit without running destructors */
 
63
  }
 
64
 
 
65
  segfaulted = 1;
 
66
 
 
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
 
83
 
 
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]);
 
90
 
 
91
  my_safe_printf_stderr("%s:%s:%s UTC - mysqld got " SIGNAL_FMT " ;\n",
 
92
                        hrs_buf, mins_buf, secs_buf, sig);
 
93
 
 
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");
 
98
 
 
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");
 
103
 
 
104
  my_safe_printf_stderr("key_buffer_size=%lu\n",
 
105
                        (ulong) dflt_key_cache->key_cache_mem_size);
 
106
 
 
107
  my_safe_printf_stderr("read_buffer_size=%ld\n",
 
108
                        (long) global_system_variables.read_buff_size);
 
109
 
 
110
  my_safe_printf_stderr("max_used_connections=%lu\n",
 
111
                        (ulong) max_used_connections);
 
112
 
 
113
  my_safe_printf_stderr("max_threads=%u\n",
 
114
                        (uint) thread_scheduler->max_threads);
 
115
 
 
116
  my_safe_printf_stderr("thread_count=%u\n", (uint) thread_count);
 
117
 
 
118
  my_safe_printf_stderr("connection_count=%u\n", (uint) connection_count);
 
119
 
 
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);
 
129
 
 
130
  my_safe_printf_stderr("%s",
 
131
    "Hope that's ok; if not, decrease some variables in the equation.\n\n");
 
132
 
 
133
#if defined(HAVE_LINUXTHREADS)
 
134
  if (sizeof(char*) == 4 && thread_count > UNSAFE_DEFAULT_LINUX_THREADS)
 
135
  {
 
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"
 
145
      thread_count);
 
146
  }
 
147
#endif /* HAVE_LINUXTHREADS */
 
148
 
 
149
#ifdef HAVE_STACKTRACE
 
150
  THD *thd=current_thd;
 
151
 
 
152
  if (!(test_flags & TEST_NO_STACKTRACE))
 
153
  {
 
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);
 
162
  }
 
163
  if (thd)
 
164
  {
 
165
    const char *kreason= "UNKNOWN";
 
166
    switch (thd->killed) {
 
167
    case THD::NOT_KILLED:
 
168
      kreason= "NOT_KILLED";
 
169
      break;
 
170
    case THD::KILL_BAD_DATA:
 
171
      kreason= "KILL_BAD_DATA";
 
172
      break;
 
173
    case THD::KILL_CONNECTION:
 
174
      kreason= "KILL_CONNECTION";
 
175
      break;
 
176
    case THD::KILL_QUERY:
 
177
      kreason= "KILL_QUERY";
 
178
      break;
 
179
    case THD::KILLED_NO_VALUE:
 
180
      kreason= "KILLED_NO_VALUE";
 
181
      break;
 
182
    }
 
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");
 
186
 
 
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);
 
192
  }
 
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");
 
197
 
 
198
#endif /* HAVE_STACKTRACE */
 
199
 
 
200
#ifdef HAVE_INITGROUPS
 
201
  if (calling_initgroups)
 
202
  {
 
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");
 
211
  }
 
212
#endif
 
213
 
 
214
#ifdef HAVE_NPTL
 
215
  if (thd_lib_detected == THD_LIB_LT && !ld_assume_kernel_is_set)
 
216
  {
 
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");
 
226
  }
 
227
#endif
 
228
 
 
229
  if (locked_in_memory)
 
230
  {
 
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");
 
240
  }
 
241
 
 
242
#ifdef HAVE_WRITE_CORE
 
243
  if (test_flags & TEST_CORE_ON_SIGNAL)
 
244
  {
 
245
    my_safe_printf_stderr("%s", "Writing a core file\n");
 
246
    my_write_core(sig);
 
247
  }
 
248
#endif
 
249
 
 
250
#ifndef __WIN__
 
251
  /*
 
252
     Quit, without running destructors (etc.)
 
253
     On Windows, do not terminate, but pass control to exception filter.
 
254
  */
 
255
  _exit(1);  // Using _exit(), since exit() is not async signal safe
 
256
#endif
 
257
}