本文介绍的redis源码阅读环境基于Linux,使用VSCode搭建编译调试环境。使用的redis版本6.2.6,其他的版本区别也不大。
1. 下载Redis源码,并命令行编译
在github redis release页面下载想看的版本,并在终端中进行编译,首先要保证命令编译通过。redis的编译非常简单,在源码根目录执行make即可编译。
2. 配置VSCode调试源码
VSCode需安装C/C++插件,安装完成后打开源码目录。在源码根目录新建.vscode目录,并新建launch.json与tasks.json文件。
- launch.json内容如下:
{
// 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",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/src/redis-server",
"args": [
"redis.conf"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
- tasks.json内容:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make",
"args": [
"CFLAGS=\"-g -O0\""
]
}
]
}
3.测试Debug
redis的main函数在src/server.c文件中,可以在main函数中打个断点。最后在调试面板中启动调试,测试是否正常。