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
21
#include "drizzled/stacktrace.h"
25
#include "drizzled/internal/my_pthread.h"
26
#include "drizzled/internal/m_string.h"
27
#ifdef HAVE_STACKTRACE
39
#include "drizzled/definitions.h"
46
#define PTR_SANE(p) ((p) && (char*)(p) >= heap_start && (char*)(p) <= heap_end)
50
void safe_print_str(const char* name, const char* val, int max_len)
52
char *heap_end= (char*) sbrk(0);
53
fprintf(stderr, "%s at %p ", name, val);
57
fprintf(stderr, " is invalid pointer\n");
61
fprintf(stderr, "= ");
62
for (; max_len && PTR_SANE(val) && *val; --max_len)
63
fputc(*val++, stderr);
67
#ifdef TARGET_OS_LINUX
70
#define SIGRETURN_FRAME_OFFSET 17
74
#define SIGRETURN_FRAME_OFFSET 23
78
#if BACKTRACE_DEMANGLE
79
static void my_demangle_symbols(char **addrs, int n)
82
char *begin, *end, *demangled;
84
for (i= 0; i < n; i++)
87
begin= strchr(addrs[i], '(');
88
end= begin ? strchr(begin, '+') : NULL;
92
*begin++= *end++= '\0';
93
demangled= my_demangle(begin, &status);
94
if (!demangled || status)
103
fprintf(stderr, "%s(%s+%s\n", addrs[i], demangled, end);
105
fprintf(stderr, "%s\n", addrs[i]);
112
static void backtrace_current_thread(void)
115
char **strings= NULL;
116
int n = backtrace(addrs, array_elements(addrs));
117
#if BACKTRACE_DEMANGLE
118
if ((strings= backtrace_symbols(addrs, n)))
120
my_demangle_symbols(strings, n);
124
#if HAVE_BACKTRACE_SYMBOLS_FD
127
backtrace_symbols_fd(addrs, n, fileno(stderr));
134
void print_stacktrace(unsigned char* stack_bottom, size_t thread_stack)
137
backtrace_current_thread();
141
uint32_t frame_count = 0, sigreturn_frame_count;
144
__asm __volatile__ ("movl %%ebp,%0"
149
__asm __volatile__ ("movq %%rbp,%0"
155
fprintf(stderr, "frame pointer is NULL, did you compile with\n\
156
-fomit-frame-pointer? Aborting backtrace!\n");
160
if (!stack_bottom || (unsigned char*) stack_bottom > (unsigned char*) &fp)
162
ulong tmp= min((size_t)0x10000,thread_stack);
163
/* Assume that the stack starts at the previous even 65K */
164
stack_bottom= (unsigned char*) (((ulong) &fp + tmp) &
166
fprintf(stderr, "Cannot determine thread, fp=%p, backtrace may not be correct.\n", (void *)fp);
168
if (fp > (unsigned char**) stack_bottom ||
169
fp < (unsigned char**) stack_bottom - thread_stack)
171
fprintf(stderr, "Bogus stack limit or frame pointer,\
172
fp=%p, stack_bottom=%p, thread_stack=%"PRIu64", aborting backtrace.\n",
173
(void *)fp, (void *)stack_bottom, (uint64_t)thread_stack);
177
fprintf(stderr, "Stack range sanity check OK, backtrace follows:\n");
179
/* We are 1 frame above signal frame with NPTL and 2 frames above with LT */
180
sigreturn_frame_count = internal::thd_lib_detected == THD_LIB_LT ? 2 : 1;
182
while (fp < (unsigned char**) stack_bottom)
184
#if defined(__i386__) || defined(__x86_64__)
185
unsigned char** new_fp = (unsigned char**)*fp;
186
fprintf(stderr, "%p\n", frame_count == sigreturn_frame_count ?
187
*(fp + SIGRETURN_FRAME_OFFSET) : *(fp + 1));
188
#endif /* defined(__386__) || defined(__x86_64__) */
192
fprintf(stderr, "New value of fp=%p failed sanity check,\
193
terminating stack trace!\n", (void *)new_fp);
200
fprintf(stderr, "Stack trace seems successful - bottom reached\n");
204
"Please read http://dev.mysql.com/doc/refman/5.1/en/resolve-stack-dump.html\n"
205
"and follow instructions on how to resolve the stack trace.\n"
206
"Resolved stack trace is much more helpful in diagnosing the\n"
207
"problem, so please do resolve it\n");
209
#endif /* TARGET_OS_LINUX */
211
} /* namespace drizzled */
213
#endif /* HAVE_STACKTRACE */
215
/* Produce a core for the thread */
220
void write_core(int sig)
222
signal(sig, SIG_DFL);
225
For GCOV build, crashing will prevent the writing of code coverage
226
information from this process, causing gcov output to be incomplete.
227
So we force the writing of coverage information here before terminating.
229
extern void __gcov_flush(void);
232
pthread_kill(pthread_self(), sig);
233
#if defined(P_MYID) && !defined(SCO)
234
/* On Solaris, the above kill is not enough */
235
sigsend(P_PID,P_MYID,sig);
239
} /* namespace drizzled */