Redis源码调试环境

背景

深入学习redis绕不过去的肯定是源码,源码学习的最好方式就是带着问题去debug,所以这里使用vscode进行调试。

编译

我比较顺利,一遍跑通了,如果缺少什么可以根据错误去自行解决。

1
make CFLAGS="-g -O0"  #-O0不进行优化处理

如果编译成功,在src目录下可以看到redis二进制文件:

1
2
3
4
5
6
7
-rwxr-xr-x 1 root root  8990408 Mar 14 11:19 redis-benchmark
-rwxr-xr-x 1 root root 11723016 Mar 14 11:19 redis-check-aof
-rwxr-xr-x 1 root root 11723016 Mar 14 11:19 redis-check-rdb
-rwxr-xr-x 1 root root 8694912 Mar 14 11:19 redis-cli
-rwxr-xr-x 1 root root 11723016 Mar 14 11:19 redis-sentinel
-rwxr-xr-x 1 root root 11723016 Mar 14 11:19 redis-server
-rwxr-xr-x 1 root root 3729 Mar 12 14:50 redis-trib.rb

我在编译之前修改了server.c中一些提示,这样方便做对比:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
serverLog(LL_WARNING, "oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo");
serverLog(LL_WARNING,
"Redis version=%s, bits=%d, commit=%s, modified=%d, pid=%d, just started Test Done",
REDIS_VERSION,
(sizeof(long) == 8) ? 64 : 32,
redisGitSHA1(),
strtol(redisGitDirty(),NULL,10) > 0,
(int)getpid());

if (argc == 1) {
serverLog(LL_WARNING, "Warning: no config file specified, using the default config. In order to specify a config file use %s /path/to/%s.conf", argv[0], server.sentinel_mode ? "sentinel" : "redis");
} else {
serverLog(LL_WARNING, "Configuration loaded");
}

接着配置vscode中launch.json,方便进行调试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
// 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": [
{
"name": "redis-server",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/redis-server",
"args": [
"./redis.conf"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

这里需要注意MIMode,根据具体编译器来选择,我是gdb,所以填的是gdb,有一些是lldb。

例子

最近在学习持久化和数据结构这些,这里就用RDB做一个测试,RDB有save和bgsave两种方法将redis数据持久化到磁盘上,所以在server.c中打上断点:

image-20240314142031853

之后在redis-cli中执行save就会触发断点,也就以上展示的内容。


Redis源码调试环境
http://example.com/2024/03/14/redis源码调试环境/
Author
John Doe
Posted on
March 14, 2024
Licensed under