gdb调试mysql之初体验

gdb简介

gdb是一款功能非常强大的调试软件,经常用于调试复杂case,该命令是由系统包自带的gdb-7.6.1-100.el7.x86_64.rpm ,包含了pstack和gdb两个调试命令,这里主要介绍gdb的调试案例

gdb常用命令

info threads:查看全部线程

thread n:指定某个线程

b:在某处打断点

c:继续往下走

s:执行一行代码,如果代码函数调用,则进入函数

n:执行一行代码,函数调用不进入

p:打印某个变量值

list:打印代码的文本信息

bt:查看某个线程的栈帧

info b:查看当前所有断点信息

gdb调试案例

[root@lineqi ~]# gdb -p `pidof mysqld`
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Attaching to process 1647
Reading symbols from /usr/sbin/mysqld...done.
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[New LWP 2684]
[New LWP 2683]
[New LWP 2682]
[New LWP 2657]
[New LWP 2164]
[New LWP 2163]
[New LWP 2162]
[New LWP 2161]
[New LWP 2002]
[New LWP 1679]
[New LWP 1678]
[New LWP 1677]
[New LWP 1676]
[New LWP 1675]
[New LWP 1674]
[New LWP 1673]
[New LWP 1672]
[New LWP 1671]
[New LWP 1670]
[New LWP 1669]
[New LWP 1668]
[New LWP 1667]
[New LWP 1666]
[New LWP 1665]
[New LWP 1659]
[New LWP 1658]
[New LWP 1657]
[New LWP 1656]
[New LWP 1655]
[New LWP 1654]
[New LWP 1653]
[New LWP 1652]
[New LWP 1651]
[New LWP 1650]
[New LWP 1649]
[New LWP 1648]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /lib64/libaio.so.1...Reading symbols from /lib64/libaio.so.1...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib64/libaio.so.1
Reading symbols from /lib64/libnuma.so.1...Reading symbols from /lib64/libnuma.so.1...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib64/libnuma.so.1
Reading symbols from /lib64/libcrypt.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libcrypt.so.1
Reading symbols from /lib64/librt.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/librt.so.1
Reading symbols from /lib64/libstdc++.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libstdc++.so.6
Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libgcc_s.so.1
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /lib64/libfreebl3.so...Reading symbols from /lib64/libfreebl3.so...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib64/libfreebl3.so
Reading symbols from /usr/lib64/mysql/plugin/validate_password.so...done.
Loaded symbols for /usr/lib64/mysql/plugin/validate_password.so
Reading symbols from /lib64/libnss_files.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libnss_files.so.2
0x00007fbcf58f5a3d in poll () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install mysql-community-server-5.7.29-1.el7.x86

这时连接数据库会被阻塞
[root@lineqi ~]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.

查看线程

gdb) info threads
  Id   Target Id         Frame 
  37   Thread 0x7fbcedbf7700 (LWP 1648) "mysqld" 0x00007fbcf583e3fa in sigwaitinfo () from /lib64/libc.so.6
  36   Thread 0x7fbce18e3700 (LWP 1649) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  35   Thread 0x7fbce10e2700 (LWP 1650) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  34   Thread 0x7fbce08e1700 (LWP 1651) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  33   Thread 0x7fbce00e0700 (LWP 1652) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  32   Thread 0x7fbcdf8df700 (LWP 1653) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  31   Thread 0x7fbcdf0de700 (LWP 1654) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  30   Thread 0x7fbcde8dd700 (LWP 1655) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  29   Thread 0x7fbcde0dc700 (LWP 1656) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  28   Thread 0x7fbcdd8db700 (LWP 1657) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  27   Thread 0x7fbcdd0da700 (LWP 1658) "mysqld" 0x00007fbcf6a36644 in __io_getevents_0_4 () from /lib64/libaio.so.1
  26   Thread 0x7fbcdc8d9700 (LWP 1659) "mysqld" 0x00007fbcf6e47cf2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  25   Thread 0x7fbcd7dfa700 (LWP 1665) "mysqld" 0x00007fbcf6e47cf2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  24   Thread 0x7fbcd75f9700 (LWP 1666) "mysqld" 0x00007fbcf6e47cf2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  23   Thread 0x7fbcd6df8700 (LWP 1667) "mysqld" 0x00007fbcf6e47cf2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  22   Thread 0x7fbcd65f7700 (LWP 1668) "mysqld" 0x00007fbcf6e4ae4d in nanosleep () from /lib64/libpthread.so.0
  21   Thread 0x7fbcd5df6700 (LWP 1669) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  20   Thread 0x7fbcd55f5700 (LWP 1670) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  19   Thread 0x7fbcd4df4700 (LWP 1671) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  18   Thread 0x7fbccffff700 (LWP 1672) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  17   Thread 0x7fbccf7fe700 (LWP 1673) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  16   Thread 0x7fbcceffd700 (LWP 1674) "mysqld" 0x00007fbcf6e47cf2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  15   Thread 0x7fbcce7fc700 (LWP 1675) "mysqld" 0x00007fbcf6e47cf2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  14   Thread 0x7fbccdffb700 (LWP 1676) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  13   Thread 0x7fbcdc0b7700 (LWP 1677) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  12   Thread 0x7fbcdc076700 (LWP 1678) "mysqld" 0x00007fbcf6e4b371 in sigwait () from /lib64/libpthread.so.0
  11   Thread 0x7fbccd5e7700 (LWP 1679) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  10   Thread 0x7fbcd412b700 (LWP 2002) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  9    Thread 0x7fbcd40ea700 (LWP 2161) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  8    Thread 0x7fbcd40a9700 (LWP 2162) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  7    Thread 0x7fbcd4068700 (LWP 2163) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
---Type <return> to continue, or q <return> to quit---
  6    Thread 0x7fbcccde6700 (LWP 2164) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  5    Thread 0x7fbcccda5700 (LWP 2657) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  4    Thread 0x7fbcccd64700 (LWP 2682) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  3    Thread 0x7fbcccd23700 (LWP 2683) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  2    Thread 0x7fbcccce2700 (LWP 2684) "mysqld" 0x00007fbcf6e47945 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
* 1    Thread 0x7fbcf7256740 (LWP 1647) "mysqld" 0x00007fbcf58f5a3d in poll () from /lib64/libc.so.6

切换线程

(gdb) thread 1
[Switching to thread 1 (Thread 0x7fbcf7256740 (LWP 1647))]
#0  0x00007fbcf58f5a3d in poll () from /lib64/libc.so.6

设置断点

(gdb) b
Breakpoint 1 at 0x7fbcf58f5a3d

查看线程trace

(gdb) bt
#0  0x00007fbcf58f5a3d in poll () from /lib64/libc.so.6
#1  0x0000000000dce5ee in poll (__timeout=-1, __nfds=<optimized out>, __fds=0x2f371a0) at /usr/include/bits/poll2.h:41
#2  Mysqld_socket_listener::listen_for_connection_event (this=0x2f37140)
    at /export/home/pb2/build/sb_0-37309218-1576675139.51/rpm/BUILD/mysql-5.7.29/mysql-5.7.29/sql/conn_handler/socket_connection.cc:859
#3  0x00000000007e3948 in connection_event_loop (this=0x2f371e0)
    at /export/home/pb2/build/sb_0-37309218-1576675139.51/rpm/BUILD/mysql-5.7.29/mysql-5.7.29/sql/conn_handler/connection_acceptor.h:73
#4  mysqld_main (argc=37, argv=0x267bb08) at /export/home/pb2/build/sb_0-37309218-1576675139.51/rpm/BUILD/mysql-5.7.29/mysql-5.7.29/sql/mysqld.cc:5128
#5  0x00007fbcf5829c05 in __libc_start_main () from /lib64/libc.so.6
#6  0x00000000007d7b34 in _start ()

继续

(gdb) c
Continuing.

Breakpoint 1, 0x00007fbcf58f5a3d in poll () from /lib64/libc.so.6

退出

(gdb) q
A debugging session is active.

        Inferior 1 [process 1647] will be detached.

Quit anyway? (y or n) y
Detaching from program: /usr/sbin/mysqld, process 1647

gdb使用小技巧

当数据库连接满了时,可以通过下面命令调整连接数

gdb --pid=`pidof mysqld` -ex "set max_connections=1500" --batch

总结与思考

1、gdb的小技巧能够使用在postgresql和oracle中吗
2、gdb调试mysqld时会阻塞整个数据库
3、gdb也可以调试某个数据库连接进程,并不会影响其他新连接

参考资料

https://blog.csdn.net/weixin_34049032/article/details/88589069
https://blog.csdn.net/n88Lpo/article/details/106484780

猜你喜欢

转载自blog.csdn.net/weixin_41561946/article/details/107584782