24.5.1.4 Debugging mysqld under gdb

On most systems you can also start mysqld
from gdb to get more information if
mysqld crashes.

With some older gdb versions on Linux you
must use run --one-thread if you want to be
able to debug mysqld threads. In this case,
you can only have one thread active at a time. It is best to
upgrade to gdb 5.1 because thread debugging
works much better with this version!

NPTL threads (the new thread library on Linux) may cause
problems while running mysqld under
gdb. Some symptoms are:

mysqld crashes during a
pthread_mutex_lock() or
pthread_mutex_unlock() call.

In this case, you should set the following environment
variable in the shell before starting gdb:

LD_ASSUME_KERNEL=2.4.1
export LD_ASSUME_KERNEL

When running mysqld under
gdb, you should disable the stack trace
with --skip-stack-trace to be
able to catch segfaults within gdb.

Use the --gdb option to
mysqld to install an interrupt handler for
SIGINT (needed to stop
mysqld with ^C to set
breakpoints) and disable stack tracing and core file handling.

It is very hard to debug MySQL under gdb if
you do a lot of new connections the whole time as
gdb does not free the memory for old
threads. You can avoid this problem by starting
mysqld with
thread_cache_size set to a
value equal to
max_connections + 1. In most
cases just using
--thread_cache_size=5' helps a
lot!

If you want to get a core dump on Linux if
mysqld dies with a SIGSEGV signal, you can
start mysqld with the
--core-file option. This core
file can be used to make a backtrace that may help you find
out why mysqld died:

To debug an already running mysqld process and seewhat happens to it if you issue a specific SQLcommand from the interactive mysql session, youhave to figure out first to which PID you have toconnect. Issue from you client commands line"SELECT 3+3" and figure out the PID. (Sinisa saysthat you can in principle connect to any mysqldprocess and switch context to access anotherthread, but NOT on Linux these days yet. Use `infothreads' command at the gdb prompt in such case)

chdir() to unpacked source tree. At the best,compile next time mysql using CFLAGS="-g3".

Where to set breakpoints as a general start place?In client in real_connect(). On the server in handle_connections(), but in version above 4.0.8 it got renamed, so use handle_one_connection() for example.