本文整理自网络,侵删。
目录
- 一、准备Debug环境
- 二、使用GDB调试
- 启动GDB编译器
- GDB常用命令
- Debug示例
- 1、取变量值
- 2、调试脚本
- 三、使用Trace文件调试
- 设置debug参数
- Debug示例
阅读本文你将了解:
- 如何准备 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)
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] 获取。
相关阅读 >>
更多相关阅读请进入《mysql》频道 >>

数据库系统概念 第6版
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » 分享MySQL常用内核Debug几种常见方法
相关推荐
评论
管理员已关闭评论功能...