1
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
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.
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.
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
20
#include "drizzled/global.h"
21
#include "stacktrace.h"
24
#include <mysys/my_pthread.h>
25
#include <mystrings/m_string.h>
26
#ifdef HAVE_STACKTRACE
34
#define PTR_SANE(p) ((p) && (char*)(p) >= heap_start && (char*)(p) <= heap_end)
38
void safe_print_str(const char* name, const char* val, int max_len)
40
char *heap_end= (char*) sbrk(0);
41
fprintf(stderr, "%s at %p ", name, val);
45
fprintf(stderr, " is invalid pointer\n");
49
fprintf(stderr, "= ");
50
for (; max_len && PTR_SANE(val) && *val; --max_len)
51
fputc(*val++, stderr);
55
#ifdef TARGET_OS_LINUX
58
#define SIGRETURN_FRAME_OFFSET 17
62
#define SIGRETURN_FRAME_OFFSET 23
66
#if BACKTRACE_DEMANGLE
67
static void my_demangle_symbols(char **addrs, int n)
70
char *begin, *end, *demangled;
72
for (i= 0; i < n; i++)
75
begin= strchr(addrs[i], '(');
76
end= begin ? strchr(begin, '+') : NULL;
80
*begin++= *end++= '\0';
81
demangled= my_demangle(begin, &status);
82
if (!demangled || status)
91
fprintf(stderr, "%s(%s+%s\n", addrs[i], demangled, end);
93
fprintf(stderr, "%s\n", addrs[i]);
100
static void backtrace_current_thread(void)
103
char **strings= NULL;
104
int n = backtrace(addrs, array_elements(addrs));
105
#if BACKTRACE_DEMANGLE
106
if ((strings= backtrace_symbols(addrs, n)))
108
my_demangle_symbols(strings, n);
112
#if HAVE_BACKTRACE_SYMBOLS_FD
115
backtrace_symbols_fd(addrs, n, fileno(stderr));
122
void print_stacktrace(unsigned char* stack_bottom, size_t thread_stack)
125
backtrace_current_thread();
129
uint32_t frame_count = 0, sigreturn_frame_count;
132
__asm __volatile__ ("movl %%ebp,%0"
137
__asm __volatile__ ("movq %%rbp,%0"
143
fprintf(stderr, "frame pointer is NULL, did you compile with\n\
144
-fomit-frame-pointer? Aborting backtrace!\n");
148
if (!stack_bottom || (unsigned char*) stack_bottom > (unsigned char*) &fp)
150
ulong tmp= cmin(0x10000,thread_stack);
151
/* Assume that the stack starts at the previous even 65K */
152
stack_bottom= (unsigned char*) (((ulong) &fp + tmp) &
154
fprintf(stderr, "Cannot determine thread, fp=%p, backtrace may not be correct.\n", (void *)fp);
156
if (fp > (unsigned char**) stack_bottom ||
157
fp < (unsigned char**) stack_bottom - thread_stack)
159
fprintf(stderr, "Bogus stack limit or frame pointer,\
160
fp=%p, stack_bottom=%p, thread_stack=%"PRIu64", aborting backtrace.\n",
161
(void *)fp, (void *)stack_bottom, (uint64_t)thread_stack);
165
fprintf(stderr, "Stack range sanity check OK, backtrace follows:\n");
167
/* We are 1 frame above signal frame with NPTL and 2 frames above with LT */
168
sigreturn_frame_count = thd_lib_detected == THD_LIB_LT ? 2 : 1;
170
while (fp < (unsigned char**) stack_bottom)
172
#if defined(__i386__) || defined(__x86_64__)
173
unsigned char** new_fp = (unsigned char**)*fp;
174
fprintf(stderr, "%p\n", frame_count == sigreturn_frame_count ?
175
*(fp + SIGRETURN_FRAME_OFFSET) : *(fp + 1));
176
#endif /* defined(__386__) || defined(__x86_64__) */
180
fprintf(stderr, "New value of fp=%p failed sanity check,\
181
terminating stack trace!\n", (void *)new_fp);
188
fprintf(stderr, "Stack trace seems successful - bottom reached\n");
192
"Please read http://dev.mysql.com/doc/refman/5.1/en/resolve-stack-dump.html\n"
193
"and follow instructions on how to resolve the stack trace.\n"
194
"Resolved stack trace is much more helpful in diagnosing the\n"
195
"problem, so please do resolve it\n");
197
#endif /* TARGET_OS_LINUX */
198
#endif /* HAVE_STACKTRACE */
200
/* Produce a core for the thread */
202
void write_core(int sig)
204
signal(sig, SIG_DFL);
207
For GCOV build, crashing will prevent the writing of code coverage
208
information from this process, causing gcov output to be incomplete.
209
So we force the writing of coverage information here before terminating.
211
extern void __gcov_flush(void);
214
pthread_kill(pthread_self(), sig);
215
#if defined(P_MYID) && !defined(SCO)
216
/* On Solaris, the above kill is not enough */
217
sigsend(P_PID,P_MYID,sig);