一、概述 #
mysql本身的开源就让我们可以很方便的编译出debug版本进行调试
二、调试环境准备 #
1. 编译mysql的debug版本 #
- 预安装软件
1yum install ncurses-devel bison cmake libarchive gcc-toolset-10-gcc gcc-toolset-10-gcc-c++ gcc-toolset-10-binutils libtirpc-devel rpcgen
- 源码下载地址:
https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.33.tar.gz
- 需要下载boost库1.73:
https://boostorg.jfrog.io/artifactory/main/release/1.73.0/source/boost_1_73_0.tar.bz2
- 解压boost库到
mysql-8.0.33/boost_1_73_0
- 编译命令
1# -DWITH_BOOST=./boost_1_73_0/ 指定boost库路径
2# -DWITH_DEBUG=1 编译成debug版本
3cmake -B build -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DWITH_BOOST=./boost_1_73_0/ -DWITH_DEBUG=1
4cmake --build build -j 8
- 执行命令为下面的输出即为成功编译出debug版
1=> ./build/runtime_output_directory/mysqld --version
2/mysql-8.0.33/build/runtime_output_directory/mysqld Ver 8.0.33-debug for Linux on x86_64 (Source distribution)
2. 替换mysqld进行调试 #
- 编译好之后,需要上传
./build/runtime_output_directory/mysqld
和./build/runtime_output_directory/lib
目录到服务器
1=> cd build
2=> zip -r mysql-8.0.33.zip ./runtime_output_directory/mysqld ./lib
3=> scp mysql-8.0.33.zip admin@x.x.x.x:~/
- 上传之后解压后目录结构如下
1=> tree mysql-8.0.33/
2mysql-8.0.33/
3├── runtime_output_directory
4│ └── mysqld
5└── lib
6 ├── libduktape.so
7 ├── libhttp_client.so
8 ├── libjson_binlog.so
9 ├── libjson_binlog.so.8.0.27
10 ├── libmetadata_cache_tests.so
11 ├── libmysqlclient.so
12 ├── libmysqlclient.so.21
13 ├── libmysqlclient.so.21.2.27
14 ├── libmysqlharness.so
15 ├── libmysqlharness.so.1
16 ├── libmysqlharness_stdx.so
17 ├── libmysqlharness_stdx.so.1
18 ...
- 启新进程
1=> export LD_LIBRARY_PATH=/home/admin/mysql-8.0.33/lib
2=> /home/admin/mysql-8.0.33/runtime_output_directory/mysqld -defaults-file=/app/native-app/sdp-mysqld/conf/my.cnf --user=mysql
-
先在设备安装
yum install gdb-gdbserver
-
开启gdbserver远程调试
1# 先找到mysqld的进程号
2=> ps aux | grep mysqld
3mysql 3044 0.5 6.8 1061784 548580 ? Ssl 9月20 5:24 /home/admin/mysql-8.0.33/runtime_output_directory/mysqld --defaults-file=/etc/my.cnf --user=mysql
4admin 22163 0.0 0.0 12320 984 pts/4 S+ 14:49 0:00 grep --color=auto mysqld
5# 挂载到进程3044,起本地监听6666
6=> gdbserver 0.0.0.0:6666 --attach 3044
- 本地vscode打开mysql的源码目录,设置
launch.json
如下后,f5就可以开始调试了
1{
2 "name": "(gdb) Attach",
3 "type": "cppdbg",
4 "request": "launch",
5 "program": "${workspaceFolder}/build/runtime_output_directory/mysqld",
6 "MIMode": "gdb",
7 "miDebuggerServerAddress": "x.x.x.x:6666",
8 "cwd": ".",
9 "setupCommands": [
10 {
11 "description": "Enable pretty-printing for gdb",
12 "text": "-enable-pretty-printing",
13 "ignoreFailures": true
14 },
15 {
16 "description": "Set Disassembly Flavor to Intel",
17 "text": "-gdb-set disassembly-flavor intel",
18 "ignoreFailures": true
19 }
20 ]
21}