关于数据库:工具-常用-MySQL-内核-Debug-技巧

3次阅读

共计 4271 个字符,预计需要花费 11 分钟才能阅读完成。

作者:柯煜昌 参谋软件工程师

目前从事 RadonDB MySQL 容器化研发,华中科技大学研究生毕业,有多年的数据库内核开发教训。

把握 MySQL 内核源码的浏览和调试能力,不仅是数据库研发人员的日常,也是 DBA 进阶的必经之路。

浏览本文你将理解:

  • 如何筹备 MySQL 调试环境
  • GDB 调试入门及操作示例
  • Trace 文件调试及操作示例

| 一、筹备 Debug 环境

首先用源码编译装置一个用来调试的 MySQL 环境。

开启 -DWITH_DEBUG,在源码门路创立 build 目录,进入目录并执行:

cmake .. -DWITH_BOOST=../../boost -DWITH_DEBUG=1

而后通过如下形式,确认是否编译胜利。

形式一:

$ ./bin/mysqld --verbose --version

回显 debug 版本信息,则编译的是 debug 版本。

ver 8.0.18-debug for Linux on x86_64 (Source distribution)

形式二:

连贯数据库,执行查看版本命令。回显蕴含了 debug 字样,则编译的是 debug 版本。

$ mysql> select version();
+--------------+
| version()    |
+--------------+
| 8.0.18-debug |
+--------------+
1 row in set (0.00 sec)

| 二、应用 GDB 调试

GDB 全称“GNU symbolic debugger”,是 Linux 下罕用的程序调试器,通常以 gdb 命令的模式在终端(Shell)中应用。

启动 GDB 编译器

执行如下命令启动 GDB 编译器(假如 my.cnf 在用户根目录中)。进入 GDB 后,敲入 run 即可运行。

gdb --args ./bin/mysqld --defaults-file=~/my.cnf --gdb

其中 –gdb 参数容许你随时 Ctrl+C 的形式中断 mysqld 过程,进行调试命令。

GDB 常用命令

应用多窗口查看源码与调试的读者,能够应用 layout 命令,在 gdb 中执行 help layout 能够查看更多 gdb 命令用法。

(gdb) help layout
(gdb) help layoutChange the layout of windows.
Usage: layout prev | next | <layout_name>
Layout names are:
   src   : Displays source and command windows.
   asm   : Displays disassembly and command windows.
   split : Displays source, disassembly and command windows.
   regs  : Displays register window. If existing layout
           is source/command or assembly/command, the
           register window is displayed. If the
           source/assembly/command (split) is displayed,
           the register window is displayed with
           the window that has current logical focus.

(gdb)

能够通过 GDB cheat sheet[1],理解更多 GDB 应用形式。

Debug 示例

装置好 Debug 环境后,咱们用以下两个例子,来简略演示应用思路及技巧。

1、取变量值

在某种状况下发现 mysqld 曾经 crash,零碎只有一个 core 文件,而咱们要晓得某个零碎变量的值。然而零碎变量的值,不见得与 my.cnf 文件统一。

此时,就能够用 gdb 命令将变量打印进去,获取变量值。

如下所示,需获取变量 version 的值,只须要在后面加 mysql_sysvar_ 前缀打印即可。

Thread 1 "mysqld" received signal SIGINT, Interrupt.
0x00007ffff5f74cb9 in __GI___poll (fds=0x55555e8a3de0, nfds=2, timeout=-1) at ../sysdeps/unix/sysv/linux/poll.c:29
29    ../sysdeps/unix/sysv/linux/poll.c: No such file or directory.
(gdb) p mysql_sysvar_version
$1 = {flags = 68101, name = 0x55555e7ff738 "innodb_version", comment = 0x55555ca953e2 "InnoDB version", check = 0x555558e222f1 <check_func_str(THD*, SYS_VAR*, void*, st_mysql_value*)>, update = 0x555558e22881 <update_func_str(THD*, SYS_VAR*, void*, void const*)>,
  value = 0x55555def1c20 <innodb_version_str>, def_val = 0x55555ca89598 "8.0.18"}
(gdb)

2、调试脚本

假如需获取某一个连贯进入 dispatch_command 有哪些 command,能够执行 gdb 脚本 [2] 获取。

gdb 脚本内容如下:

b dispatch_command
commands
    print command
    continue
end

执行 gdb 脚本,而后应用 mysql 客户端连贯数据库,并执行 SQL 语句操作,即可查看到 gdb 调试信息。

(gdb) b dispatch_command
Breakpoint 3 at 0x555558ddb37c: file /home/kyc/mysql8/sql/sql_parse.cc, line 1581.
(gdb) commands
Type commands for breakpoint(s) 3, one per line.
End with a line saying just "end".
>print command
>continue
>end
(gdb) c
Continuing.
[Switching to Thread 0x7fffe01fc700 (LWP 5941)]

Thread 49 "mysqld" hit Breakpoint 3, dispatch_command (thd=0x7fff4c000f70, com_data=0x7fffe01fbba0, command=COM_QUERY) at /home/kyc/galaxyengine/sql/sql_parse.cc:1581
1581                          enum enum_server_command command) {$4 = COM_QUERY

| 三、应用 Trace 文件调试

MySQL 的 debug 版提供了一个专门的 DBUG 包 [3]。通过这个 DBUG 包,可获取正在执行操作程序的 Trace 文件。

通过管制 DBUG 开关,能够将 MySQL 的任何操作,以及所波及的调用模块、函数、状态信息记录在 Trace 文件中。

设置 debug 参数

通过设置 debug 参数选项,指定跟踪形式。

--debug [= debug_options]

[= debug _ options] 可辨认字符 dtio 等。

Debug 示例

若需获取代码中 DBUG_PRINT("info:" 打印的日志,能够应用 MySQL 客户端连上服务器,并执行如下命令,开启 debug 参数。

set debug = 'd,info';
use test;

查看 mysqld.trace 文件,可获取 use test 在 MySQL 中的执行流程。

do_command: info: Command on socket (46) = 3 (Query)
do_command: info: packet: ' '; command: 3
dispatch_command: info: command: 3
gtid_pre_statement_checks: info: gtid_next->type=0 owned_gtid.{sidno,gno}={0,0}
THD::is_ddl_gtid_compatible: info: SQLCOM_CREATE:0 CREATE-TMP:0 SELECT:1 SQLCOM_DROP:0 DROP-TMP:0 trx:0
SELECT_LEX::prepare: info: setup_ref_array this 0x7fff1400d298    3 :    0    0    1    2    0    0
setup_fields: info: thd->mark_used_columns: 1
setup_fields: info: thd->mark_used_columns: 1
SELECT_LEX::setup_conds: info: thd->mark_used_columns: 1
THD::decide_logging_format: info: query: SELECT DATABASE()
THD::decide_logging_format: info: variables.binlog_format: 2
................
MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff14019ae0
MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff1412dd20
MDL_context::release_locks_stored_before: info: found lock to release ticket=0x7fff1412dcc0
net_send_ok: info: affected_rows: 0  id: 0  status: 2  warning_count: 0
net_send_ok: info: OK sent, so no more error sending allowed

本文应用几个简略的示例,演示了 MySQL 内核的 Debug 的几种常见办法。当然,仅仅起到抛砖引玉的作用,更多好玩的技巧,还需读者自行深度开掘。

参考

[1]:GDB cheat sheet:https://gist.github.com/rkubi…
[2]:GDB 脚本调试:https://sourceware.org/gdb/cu…
[3]:DBUG Package[:https://dev.mysql.com/doc/ref…

正文完
 0