~drizzle-trunk/drizzle/development

1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
#include "config.h"
21
22
#include <signal.h>
23
1300.5.22 by Monty Taylor
Moved and reworked a wrapper around sigset - which we shouldn't be using
24
#include "drizzled/signal_handler.h"
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
25
#include "drizzled/drizzled.h"
26
#include "drizzled/session.h"
27
#include "drizzled/internal/my_sys.h"
28
#include "drizzled/probes.h"
29
#include "drizzled/plugin.h"
30
#include "drizzled/plugin/scheduler.h"
1711.6.7 by Brian Aker
Improved message reporting during a crash.
31
32
#ifdef __GNUC__
1720 by Brian Aker
Merge of signals, plus build fix for FreeBSD. Also contains memset/constructor patch.
33
#ifdef HAVE_BACKTRACE
1711.6.7 by Brian Aker
Improved message reporting during a crash.
34
#include <execinfo.h>
35
#include <cxxabi.h>
1720 by Brian Aker
Merge of signals, plus build fix for FreeBSD. Also contains memset/constructor patch.
36
#endif // HAVE_BACKTRACE
1711.6.7 by Brian Aker
Improved message reporting during a crash.
37
#endif // __GNUC__
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
38
39
using namespace drizzled;
40
41
static uint32_t killed_threads;
42
static bool segfaulted= false;
43
44
/*
45
 * We declare these extern "C" because they are passed to system callback functions
46
 * and Sun Studio does not like it when those don't have C linkage. We prefix them
47
 * because extern "C"-ing something effectively removes the namespace from the
48
 * linker symbols, meaning they would be exporting symbols like "print_signal_warning
49
 */
50
extern "C"
51
{
52
53
void drizzled_print_signal_warning(int sig)
54
{
55
  if (global_system_variables.log_warnings)
56
    errmsg_printf(ERRMSG_LVL_WARN, _("Got signal %d from thread %"PRIu64),
57
                  sig, global_thread_id);
58
#ifndef HAVE_BSD_SIGNALS
1300.5.23 by Monty Taylor
Merged in revs removing depend on the plugin tree.
59
  sigset_t set;
60
  sigemptyset(&set);
61
62
  struct sigaction sa;
63
  sa.sa_handler= drizzled_print_signal_warning;
64
  sa.sa_mask= set;
65
  sa.sa_flags= 0;
66
  sigaction(sig, &sa, NULL);  /* int. thread system calls */
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
67
#endif
68
  if (sig == SIGALRM)
69
    alarm(2);					/* reschedule alarm */
70
}
71
72
/** Called when a thread is aborted. */
73
void drizzled_end_thread_signal(int )
74
{
75
  Session *session=current_session;
76
  if (session)
77
  {
1689.5.1 by Joseph Daly
remove increment calls
78
    killed_threads++;
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
79
    session->scheduler->killSessionNow(session);
80
    DRIZZLE_CONNECTION_DONE(session->thread_id);
81
  }
82
  return;
83
}
84
1711.6.6 by Brian Aker
Remove the code surrounding stack trace.
85
static void write_core(int sig)
86
{
87
  signal(sig, SIG_DFL);
88
#ifdef HAVE_gcov
89
  /*
90
    For GCOV build, crashing will prevent the writing of code coverage
91
    information from this process, causing gcov output to be incomplete.
92
    So we force the writing of coverage information here before terminating.
93
  */
94
  extern void __gcov_flush(void);
95
  __gcov_flush();
96
#endif
97
  pthread_kill(pthread_self(), sig);
98
#if defined(P_MYID) && !defined(SCO)
99
  /* On Solaris, the above kill is not enough */
100
  sigsend(P_PID,P_MYID,sig);
101
#endif
102
}
103
1711.6.7 by Brian Aker
Improved message reporting during a crash.
104
#define BACKTRACE_STACK_SIZE 50
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
105
void drizzled_handle_segfault(int sig)
106
{
107
  time_t curr_time;
108
  struct tm tm;
109
110
  /*
111
    Strictly speaking, one needs a mutex here
112
    but since we have got SIGSEGV already, things are a mess
113
    so not having the mutex is not as bad as possibly using a buggy
114
    mutex - so we keep things simple
115
  */
116
  if (segfaulted)
117
  {
118
    fprintf(stderr, _("Fatal signal %d while backtracing\n"), sig);
119
    exit(1);
120
  }
121
122
  segfaulted= true;
123
124
  curr_time= time(NULL);
125
  if(curr_time == (time_t)-1)
126
  {
1300.5.11 by Monty Taylor
Merged in trunk.
127
    fprintf(stderr, _("Fatal: time() call failed\n"));
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
128
    exit(1);
129
  }
130
131
  localtime_r(&curr_time, &tm);
132
  
1300.5.11 by Monty Taylor
Merged in trunk.
133
  fprintf(stderr,_("%02d%02d%02d %2d:%02d:%02d - drizzled got signal %d;\n"
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
134
          "This could be because you hit a bug. It is also possible that "
135
          "this binary\n or one of the libraries it was linked against is "
136
          "corrupt, improperly built,\n or misconfigured. This error can "
1300.5.11 by Monty Taylor
Merged in trunk.
137
          "also be caused by malfunctioning hardware.\n"),
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
138
          tm.tm_year % 100, tm.tm_mon+1, tm.tm_mday,
139
          tm.tm_hour, tm.tm_min, tm.tm_sec,
140
          sig);
141
  fprintf(stderr, _("We will try our best to scrape up some info that "
142
                    "will hopefully help diagnose\n"
143
                    "the problem, but since we have already crashed, "
144
                    "something is definitely wrong\nand this may fail.\n\n"));
145
  fprintf(stderr, "read_buffer_size=%ld\n", (long) global_system_variables.read_buff_size);
1561.3.14 by Joe Daly
fix compiler warning (hopefully) change lu to PRIu64
146
  fprintf(stderr, "max_used_connections=%"PRIu64"\n", current_global_counters.max_used_connections);
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
147
  fprintf(stderr, "connection_count=%u\n", uint32_t(connection_count));
148
  fprintf(stderr, _("It is possible that drizzled could use up to \n"
1711.6.7 by Brian Aker
Improved message reporting during a crash.
149
                    "(read_buffer_size + sort_buffer_size)*thread_count\n"
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
150
                    "bytes of memory\n"
151
                    "Hope that's ok; if not, decrease some variables in the "
152
                    "equation.\n\n"));
1711.6.7 by Brian Aker
Improved message reporting during a crash.
153
154
#ifdef __GNUC__
1720 by Brian Aker
Merge of signals, plus build fix for FreeBSD. Also contains memset/constructor patch.
155
#ifdef HAVE_BACKTRACE
1711.6.7 by Brian Aker
Improved message reporting during a crash.
156
  {
157
    void *array[BACKTRACE_STACK_SIZE];
158
    size_t size;
159
    char **strings;
160
1720 by Brian Aker
Merge of signals, plus build fix for FreeBSD. Also contains memset/constructor patch.
161
    size= backtrace(array, BACKTRACE_STACK_SIZE);
162
    strings= backtrace_symbols(array, size);
1711.6.7 by Brian Aker
Improved message reporting during a crash.
163
164
    std::cerr << "Number of stack frames obtained: " << size <<  std::endl;
165
166
    for (size_t x= 1; x < size; x++) 
167
    {
168
      size_t sz= 200;
169
      char *function= (char *)malloc(sz);
170
      char *begin= 0;
171
      char *end= 0;
172
173
      for (char *j = strings[x]; *j; ++j)
174
      {
175
        if (*j == '(') {
176
          begin = j;
177
        }
178
        else if (*j == '+') {
179
          end = j;
180
        }
181
      }
182
      if (begin && end)
183
      {
184
        begin++;
185
        *end= NULL;
186
187
        int status;
188
        char *ret = abi::__cxa_demangle(begin, function, &sz, &status);
189
        if (ret) 
190
        {
191
          function= ret;
192
        }
193
        else
194
        {
195
          std::strncpy(function, begin, sz);
196
          std::strncat(function, "()", sz);
197
          function[sz-1] = NULL;
198
        }
199
        std::cerr << function << std::endl;
200
      }
201
      else
202
      {
203
        std::cerr << strings[x] << std::endl;
204
      }
205
      free(function);
206
    }
207
208
209
    free (strings);
210
  }
1720 by Brian Aker
Merge of signals, plus build fix for FreeBSD. Also contains memset/constructor patch.
211
#endif // HAVE_BACKTRACE
1711.6.7 by Brian Aker
Improved message reporting during a crash.
212
#endif // __GNUC__
213
1711.6.6 by Brian Aker
Remove the code surrounding stack trace.
214
  write_core(sig);
1300.5.2 by Monty Taylor
Changed build to build the almost all of drizzle into libdrizzled and then
215
216
  exit(1);
217
}
218
219
} /* extern "C" */