1. 为什么要调试MySQL源码?
在实际工作中,你是否遇到过这些问题?
这些答案及解题灵感都藏于源码中! 本系列就准备通过案例结合源码调试来进行学习、解惑。其中源码安装可以参考历史文章MySQL8.0.40编译安装,如有问题可关注我进行交流。
命令 | 功能描述 | 示例(调试 MySQL 时) |
---|---|---|
break [file:line] | 设置断点(支持文件名 + 行号或函数名) | break sql/handler.cc:1234 或 break ha_innobase::update_row |
run [args] | 启动程序(可附加参数) | run --defaults-file=/etc/mysql/my.cnf |
next 或 n | 单步执行(不进入函数) | n |
step 或 s | 单步执行(进入函数) | s |
backtrace 或 bt | 查看当前调用栈 | bt |
print [expr] | 打印变量值 | print row->data 或 print query_string |
continue 或 c | 继续执行到下一个断点或结束 | c |
watch [expr] | 设置观察点(当表达式变化时触发中断) | watch table->rows_modified |
list 或 l | 查看代码上下文 | l 10 或 l ha_innobase::index_read |
finish | 执行到当前函数返回 | finish |
delete [breakpoint_num] | 删除指定编号的断点,如果不指定编号则删除所有断点 | delete 3 删除编号为 3 的断点;delete 删除所有断点 |
disable [breakpoint_num] | 禁用指定编号的断点,如果不指定编号则禁用所有断点 | disable 2 禁用编号为 2 的断点;disable 禁用所有断点 |
enable [breakpoint_num] | 启用指定编号的断点,如果不指定编号则启用所有断点 | enable 4 启用编号为 4 的断点;enable 启用所有断点 |
frame 或 f | 选择并打印指定栈帧的信息,不指定参数则打印当前栈帧信息 | f 2 选择并打印编号为 2 的栈帧信息;f 打印当前栈帧信息 |
3. 启动调试
3.1 启动一个数据库实例
建议最开始学习时使用mtr启动实例,例如
/usr/local/mysql-8.0.40/runtime_output_directory/mysqld --defaults-group-suffix=.1 --defaults-file=/usr/local/mysql-8.0.40/mysql-test/var/my.cnf --user=root --log-output=file --explain-format=TRADITIONAL_STRICT --loose-debug-sync-timeout=600 --core-file
启动后默认的MySQL端口是13000
3.2 连接数据库
按照以上步骤开启的MySQL可以用如下命令登录数据库
root@Ubuntu22045:~# /usr/local/mysql-8.0.40/runtime_output_directory/mysql -uroot -P 13000 -h 127.0.0.1
3.3 启动gdb
查看mysqld进程的id,使用 gdb -p 进程号即可进行对应实例的调试,例如
root@Ubuntu22045:~# ps -ef|grep mysqld
root 3656 2986 0 3月25 pts/0 00:01:56 /usr/local/mysql-8.0.40/runtime_output_directory/mysqld --defaults-group-suffix=.1 --defaults-file=/usr/local/mysql-8.0.40/mysql-test/var/my.cnf --user=root --log-output=file --explain-format=TRADITIONAL_STRICT --loose-debug-sync-timeout=600 --core-file
root 29180 29138 0 17:11 pts/4 00:00:00 grep --color=auto mysqld
root@Ubuntu22045:~# gdb -p 3656
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1
Copyright (C) 2022 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 3656
[New LWP 3669]
[New LWP 3670]
[New LWP 3671]
可以使用info threads查看mysql相关进程
至此基础环境准备完毕。
4. 断点示例
在 GDB 调试 MySQL 源码时,最常设置的断点通常集中在核心函数和关键流程节点上。以下列举几个常用断点以供参考设置:
(gdb) b mysql_execute_command
Breakpoint 1 at 0x5c2990212d77: file /usr/local/mysql-8.0.40/sql/sql_parse.cc, line 2946.
(gdb) c
Continuing.
[Switching to Thread 0x74e7619ff640 (LWP 3867)]
Thread 38 "connection" hit Breakpoint 1, mysql_execute_command (thd=0x74e6c4003fd0, first_level=true) at /usr/local/mysql-8.0.40/sql/sql_parse.cc:2946
2946 int mysql_execute_command(THD *thd, bool first_level) {
(gdb) p thd.m_query_string
$1 = {str = 0x74e6c40139d0 "select now()", length = 12}
(gdb)
(gdb) b dispatch_command
Breakpoint 2 at 0x5c59b7e7c8fd: file /usr/local/mysql-8.0.40/sql/sql_parse.cc, line 1689.
(gdb) c
Continuing.
[Thread 0x7ba3a9000640 (LWP 29321) exited]
[Switching to Thread 0x7ba3a55ff640 (LWP 29337)]
Thread 39 "connection" hit Breakpoint 2, dispatch_command (thd=0x7ba3080012e0, com_data=0x7ba3a55fe300, command=COM_QUERY) at /usr/local/mysql-8.0.40/sql/sql_parse.cc:1689
1689 enum enum_server_command command) {
(gdb)
(gdb) b Query_expression::execute
Breakpoint 3 at 0x5c59b7fd59fd: file /usr/local/mysql-8.0.40/sql/sql_union.cc, line 1809.
(gdb) c
Continuing.
Thread 39 "connection" hit Breakpoint 1, mysql_execute_command (thd=0x7ba3080012e0, first_level=true) at /usr/local/mysql-8.0.40/sql/sql_parse.cc:2946
2946 int mysql_execute_command(THD *thd, bool first_level) {
(gdb) c
Continuing.
Thread 39 "connection" hit Breakpoint 3, Query_expression::execute (this=0x7ba30811f2b0, thd=0x7ba3080012e0) at /usr/local/mysql-8.0.40/sql/sql_union.cc:1809
1809 bool Query_expression::execute(THD *thd) {
(gdb) l
1804 @param thd thread handle
1805
1806 @returns false if success, true if error
1807 */
1808
1809 bool Query_expression::execute(THD *thd) {
1810 DBUG_TRACE;
1811 assert(is_optimized());
1812
1813 if (is_executed() && !uncacheable) return false;
(gdb)
其他比较常用的断点如下:
5. 总结
掌握GDB调试,等于拥有了MySQL的“显微镜”——无论是性能优化、死锁排查,还是学习源码设计,都能游刃有余!
但是也有如下避坑点(血泪经验!):