0%

centos7系统使用clion工具或者GDB或者vscode code远程debug调试redis7源码-图文详细

centos7系统使用clion工具或者GDB或者vscode code远程debug调试redis7源码-图文详细

拉取代码

1
git clone https://gitee.com/panguchuangshi/redis.git

image-20250117234247060

切换到7.2.0分支

1
git checkout 7.2.0

image-20250118222146565

查看gcc

1
gcc -v

image-20250117234356240

进入到redis目录下

1
cd redis

执行编译命令

1
make CFLAGS="-g -O0" MALLOC=jemalloc

make命令可以指定参数
-O0 参数表示告诉编译器不要优化代码,防止在 Debug 的时候, IDE Redis 源码 与 实际运行的 对应不上。
MALLOC=jemalloc ,Mac OS 系统Redis默认内存分配器是libc,通过 MALLOC=jemalloc 指定使用 jemalloc 内存分配器

image-20250118222247527

可以看到执行成功

image-20250118222426329

进入src目录下

1
cd src

可以看到编译成功

image-20250118222451718

Clion工具调试

clion工具很好用,就是需要付费。

安装clion

下载地址如下

https://www.jetbrains.com/clion/download/#section=windows

安装好后执行后面操作

配置调试环境

选择redis-server

image-20250118223415317

点击编辑

image-20250118223503184

配置执行器和配置文件

image-20250118223527692

server.c打断点,然后点击调试

image-20250118223903184

可以看到断点了

image-20250118223937406

运行完后,看到启动成功

image-20250118224025017

GDB调试

在没有任何别的工具情况下,它就是最强大的。

进入到src目录下

1
cd /home/hou/redis/src

执行gdb调试命令

1
gdb redis-server

image-20250122231842033

设置断点

1
b main

运行

1
r

单步执行

1
n

image-20250122231738001

退出

1
quit

image-20250122232012057

vscode gdb远程调试

借助开源免费工具调试,一个字爽,不在受license的干扰。

安装code

https://code.visualstudio.com/updates/v1_85

安装好后启动

输入code就可以启动

1
code

打开源码目录

image-20250122232509825

安装插件

image-20250122232620837

添加配置

image-20250122232733830

添加配置文件

image-20250122232824523

选择连接远程gdbserver

image-20250122232849848

修改执行路径

image-20250122232929476

拷贝路径

image-20250122233046817

进行替换

image-20250122233131815

配置launch.json如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "by-gdb",
"request": "launch",
"name": "Launch(remote)",
"program": "/home/hou/redis/src/redis-server",
"cwd": "${workspaceRoot}",
"remote": {
"enabled": true,
"address": ":2345",
"mode": "remote",
"execfile": "/home/hou/redis/src/redis-server"
}
}

]
}

安装gdbserver

1
sudo yum install gdb-gdbserver

进入redis src目录

1
cd /home/hou/redis/src

执行远程调试服务命令

1
gdbserver :2345 redis-server /home/hou/redis/redis.conf

看到服务器已经在监听了

image-20250122233509719

设置断点

image-20250122233612671

调试

点击调试

image-20250122233650777

看到断点已经在执行了

image-20250122233713036

执行单步调试

image-20250122233757024

可以看到执行成功

image-20250122233816567