2001-05-14 01:12:40 +03:00
|
|
|
/* Copyright (C) 2000 MySQL AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2001-05-14 01:12:40 +03:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
2001-09-14 02:54:33 +03:00
|
|
|
#include <my_global.h>
|
2008-06-18 13:17:15 -03:00
|
|
|
#include <my_stacktrace.h>
|
2008-02-19 12:37:39 +01:00
|
|
|
|
|
|
|
#ifndef __WIN__
|
2001-05-14 01:12:40 +03:00
|
|
|
#include <signal.h>
|
2001-09-02 16:03:37 +03:00
|
|
|
#include <my_pthread.h>
|
2008-02-07 19:58:06 -02:00
|
|
|
#include <m_string.h>
|
2001-05-14 01:12:40 +03:00
|
|
|
#ifdef HAVE_STACKTRACE
|
|
|
|
#include <unistd.h>
|
2006-08-25 17:59:47 +04:00
|
|
|
#include <strings.h>
|
2001-05-14 01:12:40 +03:00
|
|
|
|
2008-02-07 19:58:06 -02:00
|
|
|
#if HAVE_EXECINFO_H
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2001-05-14 01:12:40 +03:00
|
|
|
#define PTR_SANE(p) ((p) && (char*)(p) >= heap_start && (char*)(p) <= heap_end)
|
|
|
|
|
2008-06-18 13:17:15 -03:00
|
|
|
static char *heap_start;
|
2008-06-19 11:02:32 -03:00
|
|
|
|
|
|
|
#ifdef HAVE_BSS_START
|
2008-06-18 13:17:15 -03:00
|
|
|
extern char *__bss_start;
|
2008-06-19 11:02:32 -03:00
|
|
|
#endif
|
2001-05-14 01:12:40 +03:00
|
|
|
|
2008-06-18 13:17:15 -03:00
|
|
|
void my_init_stacktrace()
|
|
|
|
{
|
2008-06-19 11:02:32 -03:00
|
|
|
#ifdef HAVE_BSS_START
|
2008-06-18 13:17:15 -03:00
|
|
|
heap_start = (char*) &__bss_start;
|
2008-06-19 11:02:32 -03:00
|
|
|
#endif
|
2008-06-18 13:17:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void my_safe_print_str(const char* name, const char* val, int max_len)
|
2001-05-14 01:12:40 +03:00
|
|
|
{
|
|
|
|
char *heap_end= (char*) sbrk(0);
|
|
|
|
fprintf(stderr, "%s at %p ", name, val);
|
|
|
|
|
|
|
|
if (!PTR_SANE(val))
|
|
|
|
{
|
2008-06-18 13:17:15 -03:00
|
|
|
fprintf(stderr, "is an invalid pointer\n");
|
2001-05-14 01:12:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "= ");
|
2002-12-28 01:01:05 +02:00
|
|
|
for (; max_len && PTR_SANE(val) && *val; --max_len)
|
2001-05-14 01:12:40 +03:00
|
|
|
fputc(*val++, stderr);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
2009-12-06 19:01:11 +01:00
|
|
|
#if defined(HAVE_PRINTSTACK)
|
2006-08-25 17:59:47 +04:00
|
|
|
|
2009-12-06 19:01:11 +01:00
|
|
|
/* Use Solaris' symbolic stack trace routine. */
|
|
|
|
#include <ucontext.h>
|
2006-08-25 17:59:47 +04:00
|
|
|
|
2009-12-06 19:01:11 +01:00
|
|
|
void my_print_stacktrace(uchar* stack_bottom __attribute__((unused)),
|
|
|
|
ulong thread_stack __attribute__((unused)))
|
|
|
|
{
|
|
|
|
if (printstack(fileno(stderr)) == -1)
|
|
|
|
fprintf(stderr, "Error when traversing the stack, stack appears corrupt.\n");
|
|
|
|
else
|
|
|
|
fprintf(stderr,
|
|
|
|
"Please read "
|
|
|
|
"http://dev.mysql.com/doc/refman/5.1/en/resolve-stack-dump.html\n"
|
|
|
|
"and follow instructions on how to resolve the stack trace.\n"
|
|
|
|
"Resolved stack trace is much more helpful in diagnosing the\n"
|
|
|
|
"problem, so please do resolve it\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif HAVE_BACKTRACE && (HAVE_BACKTRACE_SYMBOLS || HAVE_BACKTRACE_SYMBOLS_FD)
|
2001-05-14 01:12:40 +03:00
|
|
|
|
2008-02-07 19:58:06 -02:00
|
|
|
#if BACKTRACE_DEMANGLE
|
2008-06-18 13:17:15 -03:00
|
|
|
|
2010-07-30 17:33:10 -03:00
|
|
|
char __attribute__ ((weak)) *
|
|
|
|
my_demangle(const char *mangled_name __attribute__((unused)),
|
|
|
|
int *status __attribute__((unused)))
|
2008-06-18 13:17:15 -03:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-07 19:58:06 -02:00
|
|
|
static void my_demangle_symbols(char **addrs, int n)
|
|
|
|
{
|
|
|
|
int status, i;
|
|
|
|
char *begin, *end, *demangled;
|
|
|
|
|
|
|
|
for (i= 0; i < n; i++)
|
|
|
|
{
|
|
|
|
demangled= NULL;
|
|
|
|
begin= strchr(addrs[i], '(');
|
|
|
|
end= begin ? strchr(begin, '+') : NULL;
|
|
|
|
|
|
|
|
if (begin && end)
|
|
|
|
{
|
|
|
|
*begin++= *end++= '\0';
|
|
|
|
demangled= my_demangle(begin, &status);
|
|
|
|
if (!demangled || status)
|
|
|
|
{
|
|
|
|
demangled= NULL;
|
|
|
|
begin[-1]= '(';
|
|
|
|
end[-1]= '+';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (demangled)
|
|
|
|
fprintf(stderr, "%s(%s+%s\n", addrs[i], demangled, end);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s\n", addrs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-19 11:02:32 -03:00
|
|
|
#endif /* BACKTRACE_DEMANGLE */
|
2008-02-07 19:58:06 -02:00
|
|
|
|
2008-06-19 11:02:32 -03:00
|
|
|
void my_print_stacktrace(uchar* stack_bottom, ulong thread_stack)
|
2008-02-07 19:58:06 -02:00
|
|
|
{
|
|
|
|
void *addrs[128];
|
|
|
|
char **strings= NULL;
|
|
|
|
int n = backtrace(addrs, array_elements(addrs));
|
2008-06-19 13:00:53 -03:00
|
|
|
fprintf(stderr, "stack_bottom = %p thread_stack 0x%lx\n",
|
|
|
|
stack_bottom, thread_stack);
|
2008-02-07 19:58:06 -02:00
|
|
|
#if BACKTRACE_DEMANGLE
|
|
|
|
if ((strings= backtrace_symbols(addrs, n)))
|
|
|
|
{
|
|
|
|
my_demangle_symbols(strings, n);
|
|
|
|
free(strings);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if HAVE_BACKTRACE_SYMBOLS_FD
|
|
|
|
if (!strings)
|
|
|
|
{
|
|
|
|
backtrace_symbols_fd(addrs, n, fileno(stderr));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2008-06-19 11:02:32 -03:00
|
|
|
|
|
|
|
#elif defined(TARGET_OS_LINUX)
|
|
|
|
|
|
|
|
#ifdef __i386__
|
|
|
|
#define SIGRETURN_FRAME_OFFSET 17
|
2008-02-07 19:58:06 -02:00
|
|
|
#endif
|
|
|
|
|
2008-06-19 11:02:32 -03:00
|
|
|
#ifdef __x86_64__
|
|
|
|
#define SIGRETURN_FRAME_OFFSET 23
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__alpha__) && defined(__GNUC__)
|
|
|
|
/*
|
|
|
|
The only way to backtrace without a symbol table on alpha
|
|
|
|
is to find stq fp,N(sp), and the first byte
|
|
|
|
of the instruction opcode will give us the value of N. From this
|
|
|
|
we can find where the old value of fp is stored
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_INSTR_IN_FUNC 10000
|
|
|
|
|
|
|
|
inline uchar** find_prev_fp(uint32* pc, uchar** fp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_INSTR_IN_FUNC; ++i,--pc)
|
|
|
|
{
|
|
|
|
uchar* p = (uchar*)pc;
|
|
|
|
if (p[2] == 222 && p[3] == 35)
|
|
|
|
{
|
|
|
|
return (uchar**)((uchar*)fp - *(short int*)p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32* find_prev_pc(uint32* pc, uchar** fp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_INSTR_IN_FUNC; ++i,--pc)
|
|
|
|
{
|
|
|
|
char* p = (char*)pc;
|
|
|
|
if (p[1] == 0 && p[2] == 94 && p[3] == -73)
|
|
|
|
{
|
|
|
|
uint32* prev_pc = (uint32*)*((fp+p[0]/sizeof(fp)));
|
|
|
|
return prev_pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* defined(__alpha__) && defined(__GNUC__) */
|
2001-05-14 01:12:40 +03:00
|
|
|
|
2008-06-18 13:17:15 -03:00
|
|
|
void my_print_stacktrace(uchar* stack_bottom, ulong thread_stack)
|
2001-05-14 01:12:40 +03:00
|
|
|
{
|
|
|
|
uchar** fp;
|
2006-08-25 17:59:47 +04:00
|
|
|
uint frame_count = 0, sigreturn_frame_count;
|
2001-05-14 01:12:40 +03:00
|
|
|
#if defined(__alpha__) && defined(__GNUC__)
|
|
|
|
uint32* pc;
|
|
|
|
#endif
|
|
|
|
LINT_INIT(fp);
|
|
|
|
|
2008-02-19 12:37:39 +01:00
|
|
|
|
2006-08-25 17:59:47 +04:00
|
|
|
#ifdef __i386__
|
2001-05-14 01:12:40 +03:00
|
|
|
__asm __volatile__ ("movl %%ebp,%0"
|
|
|
|
:"=r"(fp)
|
|
|
|
:"r"(fp));
|
2006-08-25 17:59:47 +04:00
|
|
|
#endif
|
|
|
|
#ifdef __x86_64__
|
|
|
|
__asm __volatile__ ("movq %%rbp,%0"
|
|
|
|
:"=r"(fp)
|
|
|
|
:"r"(fp));
|
2001-05-14 01:12:40 +03:00
|
|
|
#endif
|
|
|
|
#if defined(__alpha__) && defined(__GNUC__)
|
2001-05-14 14:11:26 -06:00
|
|
|
__asm __volatile__ ("mov $30,%0"
|
2001-05-14 01:12:40 +03:00
|
|
|
:"=r"(fp)
|
|
|
|
:"r"(fp));
|
2006-08-25 17:59:47 +04:00
|
|
|
#endif
|
2001-05-14 01:12:40 +03:00
|
|
|
if (!fp)
|
|
|
|
{
|
2006-08-25 17:59:47 +04:00
|
|
|
fprintf(stderr, "frame pointer is NULL, did you compile with\n\
|
2001-05-14 01:12:40 +03:00
|
|
|
-fomit-frame-pointer? Aborting backtrace!\n");
|
|
|
|
return;
|
|
|
|
}
|
2001-12-06 14:10:51 +02:00
|
|
|
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 12:59:39 +03:00
|
|
|
if (!stack_bottom || (uchar*) stack_bottom > (uchar*) &fp)
|
2001-05-14 01:12:40 +03:00
|
|
|
{
|
|
|
|
ulong tmp= min(0x10000,thread_stack);
|
|
|
|
/* Assume that the stack starts at the previous even 65K */
|
WL#3817: Simplify string / memory area types and make things more consistent (first part)
The following type conversions was done:
- Changed byte to uchar
- Changed gptr to uchar*
- Change my_string to char *
- Change my_size_t to size_t
- Change size_s to size_t
Removed declaration of byte, gptr, my_string, my_size_t and size_s.
Following function parameter changes was done:
- All string functions in mysys/strings was changed to use size_t
instead of uint for string lengths.
- All read()/write() functions changed to use size_t (including vio).
- All protocoll functions changed to use size_t instead of uint
- Functions that used a pointer to a string length was changed to use size_t*
- Changed malloc(), free() and related functions from using gptr to use void *
as this requires fewer casts in the code and is more in line with how the
standard functions work.
- Added extra length argument to dirname_part() to return the length of the
created string.
- Changed (at least) following functions to take uchar* as argument:
- db_dump()
- my_net_write()
- net_write_command()
- net_store_data()
- DBUG_DUMP()
- decimal2bin() & bin2decimal()
- Changed my_compress() and my_uncompress() to use size_t. Changed one
argument to my_uncompress() from a pointer to a value as we only return
one value (makes function easier to use).
- Changed type of 'pack_data' argument to packfrm() to avoid casts.
- Changed in readfrm() and writefrom(), ha_discover and handler::discover()
the type for argument 'frmdata' to uchar** to avoid casts.
- Changed most Field functions to use uchar* instead of char* (reduced a lot of
casts).
- Changed field->val_xxx(xxx, new_ptr) to take const pointers.
Other changes:
- Removed a lot of not needed casts
- Added a few new cast required by other changes
- Added some cast to my_multi_malloc() arguments for safety (as string lengths
needs to be uint, not size_t).
- Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done
explicitely as this conflict was often hided by casting the function to
hash_get_key).
- Changed some buffers to memory regions to uchar* to avoid casts.
- Changed some string lengths from uint to size_t.
- Changed field->ptr to be uchar* instead of char*. This allowed us to
get rid of a lot of casts.
- Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar
- Include zlib.h in some files as we needed declaration of crc32()
- Changed MY_FILE_ERROR to be (size_t) -1.
- Changed many variables to hold the result of my_read() / my_write() to be
size_t. This was needed to properly detect errors (which are
returned as (size_t) -1).
- Removed some very old VMS code
- Changed packfrm()/unpackfrm() to not be depending on uint size
(portability fix)
- Removed windows specific code to restore cursor position as this
causes slowdown on windows and we should not mix read() and pread()
calls anyway as this is not thread safe. Updated function comment to
reflect this. Changed function that depended on original behavior of
my_pwrite() to itself restore the cursor position (one such case).
- Added some missing checking of return value of malloc().
- Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow.
- Changed type of table_def::m_size from my_size_t to ulong to reflect that
m_size is the number of elements in the array, not a string/memory
length.
- Moved THD::max_row_length() to table.cc (as it's not depending on THD).
Inlined max_row_length_blob() into this function.
- More function comments
- Fixed some compiler warnings when compiled without partitions.
- Removed setting of LEX_STRING() arguments in declaration (portability fix).
- Some trivial indentation/variable name changes.
- Some trivial code simplifications:
- Replaced some calls to alloc_root + memcpy to use
strmake_root()/strdup_root().
- Changed some calls from memdup() to strmake() (Safety fix)
- Simpler loops in client-simple.c
2007-05-10 12:59:39 +03:00
|
|
|
stack_bottom= (uchar*) (((ulong) &fp + tmp) &
|
2001-05-14 01:12:40 +03:00
|
|
|
~(ulong) 0xFFFF);
|
|
|
|
fprintf(stderr, "Cannot determine thread, fp=%p, backtrace may not be correct.\n", fp);
|
|
|
|
}
|
|
|
|
if (fp > (uchar**) stack_bottom ||
|
|
|
|
fp < (uchar**) stack_bottom - thread_stack)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Bogus stack limit or frame pointer,\
|
|
|
|
fp=%p, stack_bottom=%p, thread_stack=%ld, aborting backtrace.\n",
|
|
|
|
fp, stack_bottom, thread_stack);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Stack range sanity check OK, backtrace follows:\n");
|
|
|
|
#if defined(__alpha__) && defined(__GNUC__)
|
|
|
|
fprintf(stderr, "Warning: Alpha stacks are difficult -\
|
|
|
|
will be taking some wild guesses, stack trace may be incorrect or \
|
|
|
|
terminate abruptly\n");
|
2002-07-17 15:17:20 +03:00
|
|
|
/* On Alpha, we need to get pc */
|
2001-05-14 01:12:40 +03:00
|
|
|
__asm __volatile__ ("bsr %0, do_next; do_next: "
|
|
|
|
:"=r"(pc)
|
|
|
|
:"r"(pc));
|
|
|
|
#endif /* __alpha__ */
|
2001-12-06 14:10:51 +02:00
|
|
|
|
2006-08-25 17:59:47 +04:00
|
|
|
/* We are 1 frame above signal frame with NPTL and 2 frames above with LT */
|
2007-03-28 15:33:29 +02:00
|
|
|
sigreturn_frame_count = thd_lib_detected == THD_LIB_LT ? 2 : 1;
|
|
|
|
|
2001-05-14 01:12:40 +03:00
|
|
|
while (fp < (uchar**) stack_bottom)
|
|
|
|
{
|
2006-08-25 17:59:47 +04:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
2001-05-14 01:12:40 +03:00
|
|
|
uchar** new_fp = (uchar**)*fp;
|
2006-08-25 17:59:47 +04:00
|
|
|
fprintf(stderr, "%p\n", frame_count == sigreturn_frame_count ?
|
|
|
|
*(fp + SIGRETURN_FRAME_OFFSET) : *(fp + 1));
|
|
|
|
#endif /* defined(__386__) || defined(__x86_64__) */
|
2001-05-14 01:12:40 +03:00
|
|
|
|
|
|
|
#if defined(__alpha__) && defined(__GNUC__)
|
|
|
|
uchar** new_fp = find_prev_fp(pc, fp);
|
2006-10-20 14:31:47 +04:00
|
|
|
if (frame_count == sigreturn_frame_count - 1)
|
2001-05-14 01:12:40 +03:00
|
|
|
{
|
|
|
|
new_fp += 90;
|
|
|
|
}
|
2001-12-06 14:10:51 +02:00
|
|
|
|
2001-05-14 01:12:40 +03:00
|
|
|
if (fp && pc)
|
|
|
|
{
|
|
|
|
pc = find_prev_pc(pc, fp);
|
|
|
|
if (pc)
|
|
|
|
fprintf(stderr, "%p\n", pc);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Not smart enough to deal with the rest\
|
|
|
|
of this stack\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Not smart enough to deal with the rest of this stack\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
#endif /* defined(__alpha__) && defined(__GNUC__) */
|
|
|
|
if (new_fp <= fp )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "New value of fp=%p failed sanity check,\
|
|
|
|
terminating stack trace!\n", new_fp);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
fp = new_fp;
|
|
|
|
++frame_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Stack trace seems successful - bottom reached\n");
|
2001-12-06 14:10:51 +02:00
|
|
|
|
2001-05-14 01:12:40 +03:00
|
|
|
end:
|
2007-05-31 17:45:22 +03:00
|
|
|
fprintf(stderr,
|
|
|
|
"Please read http://dev.mysql.com/doc/refman/5.1/en/resolve-stack-dump.html\n"
|
|
|
|
"and follow instructions on how to resolve the stack trace.\n"
|
|
|
|
"Resolved stack trace is much more helpful in diagnosing the\n"
|
|
|
|
"problem, so please do resolve it\n");
|
2001-05-14 01:12:40 +03:00
|
|
|
}
|
2005-04-20 20:38:57 +02:00
|
|
|
#endif /* TARGET_OS_LINUX */
|
2001-05-14 01:12:40 +03:00
|
|
|
#endif /* HAVE_STACKTRACE */
|
|
|
|
|
|
|
|
/* Produce a core for the thread */
|
2008-06-18 13:17:15 -03:00
|
|
|
void my_write_core(int sig)
|
2001-08-22 12:22:46 +03:00
|
|
|
{
|
|
|
|
signal(sig, SIG_DFL);
|
2007-07-07 07:46:17 +02:00
|
|
|
#ifdef HAVE_gcov
|
|
|
|
/*
|
|
|
|
For GCOV build, crashing will prevent the writing of code coverage
|
|
|
|
information from this process, causing gcov output to be incomplete.
|
|
|
|
So we force the writing of coverage information here before terminating.
|
|
|
|
*/
|
|
|
|
extern void __gcov_flush(void);
|
|
|
|
__gcov_flush();
|
|
|
|
#endif
|
2001-08-22 12:22:46 +03:00
|
|
|
pthread_kill(pthread_self(), sig);
|
2001-11-28 03:47:15 +02:00
|
|
|
#if defined(P_MYID) && !defined(SCO)
|
2001-11-21 15:08:01 +02:00
|
|
|
/* On Solaris, the above kill is not enough */
|
|
|
|
sigsend(P_PID,P_MYID,sig);
|
2001-11-22 13:07:55 +02:00
|
|
|
#endif
|
2001-08-22 12:22:46 +03:00
|
|
|
}
|
2008-06-18 13:17:15 -03:00
|
|
|
|
2008-02-19 12:37:39 +01:00
|
|
|
#else /* __WIN__*/
|
|
|
|
|
|
|
|
#include <dbghelp.h>
|
2008-09-15 14:58:32 +02:00
|
|
|
#include <tlhelp32.h>
|
2010-06-30 14:10:29 +02:00
|
|
|
#if _MSC_VER
|
|
|
|
#pragma comment(lib, "dbghelp")
|
|
|
|
#endif
|
2008-02-19 12:37:39 +01:00
|
|
|
|
|
|
|
static EXCEPTION_POINTERS *exception_ptrs;
|
|
|
|
|
|
|
|
#define MODULE64_SIZE_WINXP 576
|
|
|
|
#define STACKWALK_MAX_FRAMES 64
|
|
|
|
|
2008-06-18 13:17:15 -03:00
|
|
|
void my_init_stacktrace()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-19 12:37:39 +01:00
|
|
|
|
2008-06-18 13:17:15 -03:00
|
|
|
void my_set_exception_pointers(EXCEPTION_POINTERS *ep)
|
2008-02-19 12:37:39 +01:00
|
|
|
{
|
|
|
|
exception_ptrs = ep;
|
|
|
|
}
|
|
|
|
|
2010-06-30 14:10:29 +02:00
|
|
|
/*
|
|
|
|
Appends directory to symbol path.
|
|
|
|
*/
|
|
|
|
static void add_to_symbol_path(char *path, size_t path_buffer_size,
|
|
|
|
char *dir, size_t dir_buffer_size)
|
|
|
|
{
|
|
|
|
strcat_s(dir, dir_buffer_size, ";");
|
|
|
|
if (!strstr(path, dir))
|
|
|
|
{
|
|
|
|
strcat_s(path, path_buffer_size, dir);
|
|
|
|
}
|
|
|
|
}
|
2008-09-15 14:58:32 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Get symbol path - semicolon-separated list of directories to search for debug
|
|
|
|
symbols. We expect PDB in the same directory as corresponding exe or dll,
|
|
|
|
so the path is build from directories of the loaded modules. If environment
|
|
|
|
variable _NT_SYMBOL_PATH is set, it's value appended to the symbol search path
|
|
|
|
*/
|
|
|
|
static void get_symbol_path(char *path, size_t size)
|
|
|
|
{
|
|
|
|
HANDLE hSnap;
|
|
|
|
char *envvar;
|
2010-06-30 14:10:29 +02:00
|
|
|
char *p;
|
|
|
|
#ifndef DBUG_OFF
|
|
|
|
static char pdb_debug_dir[MAX_PATH + 7];
|
|
|
|
#endif
|
2008-09-15 14:58:32 +02:00
|
|
|
|
|
|
|
path[0]= '\0';
|
2010-06-30 14:10:29 +02:00
|
|
|
|
|
|
|
#ifndef DBUG_OFF
|
|
|
|
/*
|
|
|
|
Add "debug" subdirectory of the application directory, sometimes PDB will
|
|
|
|
placed here by installation.
|
|
|
|
*/
|
|
|
|
GetModuleFileName(NULL, pdb_debug_dir, MAX_PATH);
|
|
|
|
p= strrchr(pdb_debug_dir, '\\');
|
|
|
|
if(p)
|
|
|
|
{
|
|
|
|
*p= 0;
|
|
|
|
strcat_s(pdb_debug_dir, sizeof(pdb_debug_dir), "\\debug;");
|
|
|
|
add_to_symbol_path(path, size, pdb_debug_dir, sizeof(pdb_debug_dir));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-15 14:58:32 +02:00
|
|
|
/*
|
|
|
|
Enumerate all modules, and add their directories to the path.
|
|
|
|
Avoid duplicate entries.
|
|
|
|
*/
|
|
|
|
hSnap= CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetCurrentProcessId());
|
|
|
|
if (hSnap != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
MODULEENTRY32 mod;
|
|
|
|
mod.dwSize= sizeof(MODULEENTRY32);
|
|
|
|
for (ret= Module32First(hSnap, &mod); ret; ret= Module32Next(hSnap, &mod))
|
|
|
|
{
|
|
|
|
char *module_dir= mod.szExePath;
|
2010-06-30 14:10:29 +02:00
|
|
|
p= strrchr(module_dir,'\\');
|
2008-09-15 14:58:32 +02:00
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Path separator was not found. Not known to happen, if ever happens,
|
|
|
|
will indicate current directory.
|
|
|
|
*/
|
|
|
|
module_dir[0]= '.';
|
2010-06-30 14:10:29 +02:00
|
|
|
module_dir[1]= '\0';
|
2008-09-15 14:58:32 +02:00
|
|
|
}
|
2010-06-30 14:10:29 +02:00
|
|
|
else
|
2008-09-15 14:58:32 +02:00
|
|
|
{
|
2010-06-30 14:10:29 +02:00
|
|
|
*p= '\0';
|
2008-09-15 14:58:32 +02:00
|
|
|
}
|
2010-06-30 14:10:29 +02:00
|
|
|
add_to_symbol_path(path, size, module_dir,sizeof(mod.szExePath));
|
2008-09-15 14:58:32 +02:00
|
|
|
}
|
|
|
|
CloseHandle(hSnap);
|
|
|
|
}
|
|
|
|
|
2010-06-30 14:10:29 +02:00
|
|
|
|
2008-09-15 14:58:32 +02:00
|
|
|
/* Add _NT_SYMBOL_PATH, if present. */
|
|
|
|
envvar= getenv("_NT_SYMBOL_PATH");
|
2010-06-30 14:10:29 +02:00
|
|
|
if(envvar)
|
2008-09-15 14:58:32 +02:00
|
|
|
{
|
2010-06-30 14:10:29 +02:00
|
|
|
strcat_s(path, size, envvar);
|
2008-09-15 14:58:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_SYMBOL_PATH 32768
|
|
|
|
|
2008-02-19 12:37:39 +01:00
|
|
|
/* Platform SDK in VS2003 does not have definition for SYMOPT_NO_PROMPTS*/
|
|
|
|
#ifndef SYMOPT_NO_PROMPTS
|
|
|
|
#define SYMOPT_NO_PROMPTS 0
|
|
|
|
#endif
|
|
|
|
|
2008-06-18 13:17:15 -03:00
|
|
|
void my_print_stacktrace(uchar* unused1, ulong unused2)
|
2008-02-19 12:37:39 +01:00
|
|
|
{
|
|
|
|
HANDLE hProcess= GetCurrentProcess();
|
|
|
|
HANDLE hThread= GetCurrentThread();
|
|
|
|
static IMAGEHLP_MODULE64 module= {sizeof(module)};
|
|
|
|
static IMAGEHLP_SYMBOL64_PACKAGE package;
|
|
|
|
DWORD64 addr;
|
|
|
|
DWORD machine;
|
|
|
|
int i;
|
|
|
|
CONTEXT context;
|
|
|
|
STACKFRAME64 frame={0};
|
2008-09-16 13:16:41 +02:00
|
|
|
static char symbol_path[MAX_SYMBOL_PATH];
|
2008-02-19 12:37:39 +01:00
|
|
|
|
2010-06-30 14:10:29 +02:00
|
|
|
if(!exception_ptrs)
|
2008-02-19 12:37:39 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Copy context, as stackwalking on original will unwind the stack */
|
|
|
|
context = *(exception_ptrs->ContextRecord);
|
|
|
|
/*Initialize symbols.*/
|
2010-06-30 14:10:29 +02:00
|
|
|
SymSetOptions(SYMOPT_LOAD_LINES|SYMOPT_NO_PROMPTS|SYMOPT_DEFERRED_LOADS|SYMOPT_DEBUG);
|
2008-09-16 13:16:41 +02:00
|
|
|
get_symbol_path(symbol_path, sizeof(symbol_path));
|
2010-06-30 14:10:29 +02:00
|
|
|
SymInitialize(hProcess, symbol_path, TRUE);
|
2008-02-19 12:37:39 +01:00
|
|
|
|
|
|
|
/*Prepare stackframe for the first StackWalk64 call*/
|
|
|
|
frame.AddrFrame.Mode= frame.AddrPC.Mode= frame.AddrStack.Mode= AddrModeFlat;
|
|
|
|
#if (defined _M_IX86)
|
|
|
|
machine= IMAGE_FILE_MACHINE_I386;
|
|
|
|
frame.AddrFrame.Offset= context.Ebp;
|
|
|
|
frame.AddrPC.Offset= context.Eip;
|
|
|
|
frame.AddrStack.Offset= context.Esp;
|
|
|
|
#elif (defined _M_X64)
|
|
|
|
machine = IMAGE_FILE_MACHINE_AMD64;
|
|
|
|
frame.AddrFrame.Offset= context.Rbp;
|
|
|
|
frame.AddrPC.Offset= context.Rip;
|
|
|
|
frame.AddrStack.Offset= context.Rsp;
|
|
|
|
#else
|
|
|
|
/*There is currently no need to support IA64*/
|
|
|
|
#pragma error ("unsupported architecture")
|
|
|
|
#endif
|
|
|
|
|
|
|
|
package.sym.SizeOfStruct= sizeof(package.sym);
|
|
|
|
package.sym.MaxNameLength= sizeof(package.name);
|
|
|
|
|
|
|
|
/*Walk the stack, output useful information*/
|
|
|
|
for(i= 0; i< STACKWALK_MAX_FRAMES;i++)
|
|
|
|
{
|
|
|
|
DWORD64 function_offset= 0;
|
|
|
|
DWORD line_offset= 0;
|
|
|
|
IMAGEHLP_LINE64 line= {sizeof(line)};
|
|
|
|
BOOL have_module= FALSE;
|
|
|
|
BOOL have_symbol= FALSE;
|
|
|
|
BOOL have_source= FALSE;
|
|
|
|
|
2010-06-30 14:10:29 +02:00
|
|
|
if(!StackWalk64(machine, hProcess, hThread, &frame, &context, 0, 0, 0 ,0))
|
2008-02-19 12:37:39 +01:00
|
|
|
break;
|
|
|
|
addr= frame.AddrPC.Offset;
|
|
|
|
|
2010-06-30 14:10:29 +02:00
|
|
|
have_module= SymGetModuleInfo64(hProcess,addr,&module);
|
2008-02-19 12:37:39 +01:00
|
|
|
#ifdef _M_IX86
|
|
|
|
if(!have_module)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
ModuleInfo structure has been "compatibly" extended in releases after XP,
|
|
|
|
and its size was increased. To make XP dbghelp.dll function
|
|
|
|
happy, pretend passing the old structure.
|
|
|
|
*/
|
|
|
|
module.SizeOfStruct= MODULE64_SIZE_WINXP;
|
2010-06-30 14:10:29 +02:00
|
|
|
have_module= SymGetModuleInfo64(hProcess, addr, &module);
|
2008-02-19 12:37:39 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-06-30 14:10:29 +02:00
|
|
|
have_symbol= SymGetSymFromAddr64(hProcess, addr, &function_offset,
|
2008-02-19 12:37:39 +01:00
|
|
|
&(package.sym));
|
2010-06-30 14:10:29 +02:00
|
|
|
have_source= SymGetLineFromAddr64(hProcess, addr, &line_offset, &line);
|
2008-02-19 12:37:39 +01:00
|
|
|
|
|
|
|
fprintf(stderr, "%p ", addr);
|
|
|
|
if(have_module)
|
|
|
|
{
|
|
|
|
char *base_image_name= strrchr(module.ImageName, '\\');
|
|
|
|
if(base_image_name)
|
|
|
|
base_image_name++;
|
|
|
|
else
|
|
|
|
base_image_name= module.ImageName;
|
|
|
|
fprintf(stderr, "%s!", base_image_name);
|
|
|
|
}
|
|
|
|
if(have_symbol)
|
|
|
|
fprintf(stderr, "%s()", package.sym.Name);
|
|
|
|
else if(have_module)
|
|
|
|
fprintf(stderr, "???");
|
|
|
|
|
|
|
|
if(have_source)
|
|
|
|
{
|
|
|
|
char *base_file_name= strrchr(line.FileName, '\\');
|
|
|
|
if(base_file_name)
|
|
|
|
base_file_name++;
|
|
|
|
else
|
|
|
|
base_file_name= line.FileName;
|
|
|
|
fprintf(stderr,"[%s:%u]", base_file_name, line.LineNumber);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Write dump. The dump is created in current directory,
|
|
|
|
file name is constructed from executable name plus
|
|
|
|
".dmp" extension
|
|
|
|
*/
|
2008-06-18 13:17:15 -03:00
|
|
|
void my_write_core(int unused)
|
2008-02-19 12:37:39 +01:00
|
|
|
{
|
|
|
|
char path[MAX_PATH];
|
|
|
|
char dump_fname[MAX_PATH]= "core.dmp";
|
|
|
|
MINIDUMP_EXCEPTION_INFORMATION info;
|
|
|
|
HANDLE hFile;
|
|
|
|
|
2010-06-30 14:10:29 +02:00
|
|
|
if(!exception_ptrs)
|
2008-02-19 12:37:39 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
info.ExceptionPointers= exception_ptrs;
|
|
|
|
info.ClientPointers= FALSE;
|
|
|
|
info.ThreadId= GetCurrentThreadId();
|
|
|
|
|
|
|
|
if(GetModuleFileName(NULL, path, sizeof(path)))
|
|
|
|
{
|
|
|
|
_splitpath(path, NULL, NULL,dump_fname,NULL);
|
|
|
|
strncat(dump_fname, ".dmp", sizeof(dump_fname));
|
|
|
|
}
|
|
|
|
|
|
|
|
hFile= CreateFile(dump_fname, GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if(hFile)
|
|
|
|
{
|
|
|
|
/* Create minidump */
|
2010-06-30 14:10:29 +02:00
|
|
|
if(MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
|
2008-02-19 12:37:39 +01:00
|
|
|
hFile, MiniDumpNormal, &info, 0, 0))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Minidump written to %s\n",
|
|
|
|
_fullpath(path, dump_fname, sizeof(path)) ? path : dump_fname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr,"MiniDumpWriteDump() failed, last error %u\n",
|
|
|
|
GetLastError());
|
|
|
|
}
|
|
|
|
CloseHandle(hFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "CreateFile(%s) failed, last error %u\n", dump_fname,
|
|
|
|
GetLastError());
|
|
|
|
}
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 13:17:15 -03:00
|
|
|
void my_safe_print_str(const char *name, const char *val, int len)
|
2008-02-19 12:37:39 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,"%s at %p", name, val);
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
fprintf(stderr,"=%.*s\n", len, val);
|
|
|
|
}
|
|
|
|
__except(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"is an invalid string pointer\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /*__WIN__*/
|