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 "stacktrace.h"
23
#include <mysys/my_pthread.h>
24
#include <mystrings/m_string.h>
25
#ifdef HAVE_STACKTRACE
33
#define PTR_SANE(p) ((p) && (char*)(p) >= heap_start && (char*)(p) <= heap_end)
37
void safe_print_str(const char* name, const char* val, int max_len)
39
char *heap_end= (char*) sbrk(0);
40
fprintf(stderr, "%s at %p ", name, val);
44
fprintf(stderr, " is invalid pointer\n");
48
fprintf(stderr, "= ");
49
for (; max_len && PTR_SANE(val) && *val; --max_len)
50
fputc(*val++, stderr);
54
#ifdef TARGET_OS_LINUX
57
#define SIGRETURN_FRAME_OFFSET 17
61
#define SIGRETURN_FRAME_OFFSET 23
65
#if BACKTRACE_DEMANGLE
66
static void my_demangle_symbols(char **addrs, int n)
69
char *begin, *end, *demangled;
71
for (i= 0; i < n; i++)
74
begin= strchr(addrs[i], '(');
75
end= begin ? strchr(begin, '+') : NULL;
79
*begin++= *end++= '\0';
80
demangled= my_demangle(begin, &status);
81
if (!demangled || status)
90
fprintf(stderr, "%s(%s+%s\n", addrs[i], demangled, end);
92
fprintf(stderr, "%s\n", addrs[i]);
99
static void backtrace_current_thread(void)
102
char **strings= NULL;
103
int n = backtrace(addrs, array_elements(addrs));
104
#if BACKTRACE_DEMANGLE
105
if ((strings= backtrace_symbols(addrs, n)))
107
my_demangle_symbols(strings, n);
111
#if HAVE_BACKTRACE_SYMBOLS_FD
114
backtrace_symbols_fd(addrs, n, fileno(stderr));
121
void print_stacktrace(unsigned char* stack_bottom, ulong thread_stack)
124
backtrace_current_thread();
128
uint32_t frame_count = 0, sigreturn_frame_count;
131
__asm __volatile__ ("movl %%ebp,%0"
136
__asm __volatile__ ("movq %%rbp,%0"
142
fprintf(stderr, "frame pointer is NULL, did you compile with\n\
143
-fomit-frame-pointer? Aborting backtrace!\n");
147
if (!stack_bottom || (unsigned char*) stack_bottom > (unsigned char*) &fp)
149
ulong tmp= cmin(0x10000,thread_stack);
150
/* Assume that the stack starts at the previous even 65K */
151
stack_bottom= (unsigned char*) (((ulong) &fp + tmp) &
153
fprintf(stderr, "Cannot determine thread, fp=%p, backtrace may not be correct.\n", (void *)fp);
155
if (fp > (unsigned char**) stack_bottom ||
156
fp < (unsigned char**) stack_bottom - thread_stack)
158
fprintf(stderr, "Bogus stack limit or frame pointer,\
159
fp=%p, stack_bottom=%p, thread_stack=%ld, aborting backtrace.\n",
160
(void *)fp, (void *)stack_bottom, thread_stack);
164
fprintf(stderr, "Stack range sanity check OK, backtrace follows:\n");
166
/* We are 1 frame above signal frame with NPTL and 2 frames above with LT */
167
sigreturn_frame_count = thd_lib_detected == THD_LIB_LT ? 2 : 1;
169
while (fp < (unsigned char**) stack_bottom)
171
#if defined(__i386__) || defined(__x86_64__)
172
unsigned char** new_fp = (unsigned char**)*fp;
173
fprintf(stderr, "%p\n", frame_count == sigreturn_frame_count ?
174
*(fp + SIGRETURN_FRAME_OFFSET) : *(fp + 1));
175
#endif /* defined(__386__) || defined(__x86_64__) */
179
fprintf(stderr, "New value of fp=%p failed sanity check,\
180
terminating stack trace!\n", (void *)new_fp);
187
fprintf(stderr, "Stack trace seems successful - bottom reached\n");
191
"Please read http://dev.mysql.com/doc/refman/5.1/en/resolve-stack-dump.html\n"
192
"and follow instructions on how to resolve the stack trace.\n"
193
"Resolved stack trace is much more helpful in diagnosing the\n"
194
"problem, so please do resolve it\n");
196
#endif /* TARGET_OS_LINUX */
197
#endif /* HAVE_STACKTRACE */
199
/* Produce a core for the thread */
201
void write_core(int sig)
203
signal(sig, SIG_DFL);
206
For GCOV build, crashing will prevent the writing of code coverage
207
information from this process, causing gcov output to be incomplete.
208
So we force the writing of coverage information here before terminating.
210
extern void __gcov_flush(void);
213
pthread_kill(pthread_self(), sig);
214
#if defined(P_MYID) && !defined(SCO)
215
/* On Solaris, the above kill is not enough */
216
sigsend(P_PID,P_MYID,sig);