gdb 调试mysql,这里调试 用户登陆报错
powershell
窗口1 用gdb 挂住 my_error 函数
[root@node1 ~]# ps -ef|grep mysql
root 4688 1 0 Mar22 ? 00:00:00 /bin/sh /usr/local/mysql//bin/mysqld_safe --datadir=/data/mysql/mysql3306/data --pid-file=/data/mysql/mysql3306/data/node1.pid
mysql 5814 4688 0 Mar22 ? 00:15:23 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/data/mysql/mysql3306/data --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/data/mysql/mysql3306/logs/error.log --open-files-limit=65536 --pid-file=/data/mysql/mysql3306/data/node1.pid --socket=/tmp/mysql3306.sock --port=3306
root 28275 27829 0 23:33 pts/4 00:00:00 grep --color=auto mysql
[root@node1 ~]# gdb -p 5814
--- 挂住my_error函数
(gdb) b my_error
Breakpoint 1 at 0x20729c4
(gdb) bt
#0 0x00007f1813fc1ddd in poll () from /lib64/libc.so.6
#1 0x0000000001059098 in Mysqld_socket_listener::listen_for_connection_event() ()
#2 0x0000000000e28151 in mysqld_main(int, char**) ()
#3 0x00007f1813ef0555 in __libc_start_main () from /lib64/libc.so.6
#4 0x0000000000e0c185 in _start ()
窗口2 模拟登陆报错 ,卡住
[root@node1 ~]# mysql -uroot -perror -S /tmp/mysql3306.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
窗口1 执行 c 继续往下走
(gdb) c
Continuing.
[Switching to Thread 0x7f1798288700 (LWP 1646)]
Breakpoint 1, 0x00000000020729c4 in my_error(int, int, ...) ()
(gdb) info threads
Id Target Id Frame
42 Thread 0x7f1805356700 (LWP 5822) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
41 Thread 0x7f1805b57700 (LWP 5823) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
40 Thread 0x7f1806358700 (LWP 5824) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
39 Thread 0x7f1806b59700 (LWP 5825) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
38 Thread 0x7f17ad3a2700 (LWP 5826) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
37 Thread 0x7f17acba1700 (LWP 5827) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
36 Thread 0x7f17ac3a0700 (LWP 5828) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
35 Thread 0x7f17abb9f700 (LWP 5829) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
34 Thread 0x7f17ab39e700 (LWP 5830) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
33 Thread 0x7f17aab9d700 (LWP 5831) "mysqld" 0x00007f1814cc8644 in __io_getevents_0_4 () from /lib64/libaio.so.1
32 Thread 0x7f17aa39c700 (LWP 5832) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
31 Thread 0x7f17a9b9b700 (LWP 5833) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
30 Thread 0x7f17a939a700 (LWP 5834) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
29 Thread 0x7f17a8b99700 (LWP 5835) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
28 Thread 0x7f179f396700 (LWP 5836) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
27 Thread 0x7f179eb95700 (LWP 5837) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
26 Thread 0x7f179e394700 (LWP 5838) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
25 Thread 0x7f179db93700 (LWP 5839) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
24 Thread 0x7f179d392700 (LWP 5840) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
23 Thread 0x7f179cb91700 (LWP 5841) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
22 Thread 0x7f179c390700 (LWP 5842) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
21 Thread 0x7f179bb8f700 (LWP 5843) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
20 Thread 0x7f179b38e700 (LWP 5845) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
19 Thread 0x7f179ab8d700 (LWP 5846) "mysqld" 0x00007f1815eeee9d in nanosleep () from /lib64/libpthread.so.0
18 Thread 0x7f179a38c700 (LWP 5847) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
17 Thread 0x7f1799b8b700 (LWP 5848) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
16 Thread 0x7f180405f700 (LWP 5849) "xpl_worker0" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
15 Thread 0x7f17ae20b700 (LWP 5850) "xpl_worker1" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
14 Thread 0x7f17ae1c4700 (LWP 5851) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
13 Thread 0x7f1807e27700 (LWP 5855) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
12 Thread 0x7f179938a700 (LWP 5856) "mysqld" 0x00007f1815eebde2 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
11 Thread 0x7f1798b89700 (LWP 5857) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
10 Thread 0x7f178bfff700 (LWP 5858) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
9 Thread 0x7f178b7fe700 (LWP 5859) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
8 Thread 0x7f178affd700 (LWP 5860) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
---Type <return> to continue, or q <return> to quit---
7 Thread 0x7f17ae17d700 (LWP 5861) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
6 Thread 0x7f17ae135700 (LWP 5862) "mysqld" 0x00007f1813f0558a in sigwaitinfo () from /lib64/libc.so.6
5 Thread 0x7f17ae0ed700 (LWP 5863) "mysqld" 0x00007f1813fcd0e3 in epoll_wait () from /lib64/libc.so.6
4 Thread 0x7f178a7fc700 (LWP 5865) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
3 Thread 0x7f17ae05f700 (LWP 5951) "mysqld" 0x00007f1815eeba35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
* 2 Thread 0x7f1798288700 (LWP 1646) "mysqld" 0x00000000020729c4 in my_error(int, int, ...) ()
1 Thread 0x7f1816309840 (LWP 5814) "mysqld" 0x00007f1813fc1ddd in poll () from /lib64/libc.so.6
(gdb)
(gdb)
这里 2 带星号的就是my_error 函数了
############ gdb 用法
1. 安装 gdb 和带调试符号的 MySQL
安装 gdb
如果你的系统没有 gdb,先安装它:
yum install -y gdb
或者,如果你使用 dnf:
dnf install -y gdb
确保 MySQL 具有调试符号
2. 启动 MySQL 并附加 gdb
2.1 启动 MySQL
如果 MySQL 还未运行,可以先启动它:
systemctl start mysqld
然后找到 mysqld 进程 ID:
ps aux | grep mysqld
例如:
mysql 1234 2.3 0.5 /usr/sbin/mysqld
记住 1234(进程 ID)。
2.2 附加 gdb
gdb -p 1234
如果 gdb 报错 ptrace: Operation not permitted,请执行:
echo 0 > /proc/sys/kernel/yama/ptrace_scope
3. 调试 MySQL
3.1 设置断点
(gdb) b sql_parse.cc:100 # 在 sql_parse.cc 第 100 行设置断点
(gdb) b my_function_name # 在特定函数上设置断点
3.2 继续运行
(gdb) c # 继续执行
3.3 打印变量值
(gdb) p some_variable
3.4 查看调用栈
(gdb) bt
3.5 单步执行
(gdb) n # 执行下一行
(gdb) s # 进入函数
3.6 解除断点
(gdb) delete 1
3.7 退出 gdb
(gdb) q
4. 在 MySQL 运行 SQL 语句进行调试
如果你想调试某个 SQL 语句的执行过程:
附加到 mysqld 进程。
在 dispatch_command 或 mysql_parse 处设置断点:
(gdb) b dispatch_command
在 MySQL 客户端执行 SQL 语句:
mysql -u root -p -e "SELECT * FROM test;"
gdb 会在 dispatch_command 处暂停,你可以查看调用栈:
(gdb) bt
5. 使用 gdb 运行 mysqld
你也可以直接用 gdb 启动 MySQL:
gdb --args /usr/sbin/mysqld --defaults-file=/etc/my.cnf --user=mysql
然后在 gdb 中输入:
(gdb) run
这样,你可以在 MySQL 进程启动时调试它的执行。
结论:
使用 gdb 调试 MySQL 主要包括:
确保 MySQL 具有调试符号。
使用 gdb -p 附加到 mysqld 进程。
设置断点、查看变量、执行 SQL 语句进行调试。
也可以直接用 gdb 启动 mysqld 以便深入分析