Redis本地调试

通过源代码编译来安装 Redis ,在自己的机器上搭建一套可以 Debug 的 Redis 源码环境。
推荐在 Linux 或者 MacOS 上Debug。

redis-server调试截图

(1) 调试环境准备

  1. 下载源码
  2. 安装C语言GCC编译器
  3. 通过源码编码Redis源码
  4. 启动Redis开始Debug

(1.1) 下载源码

git clone https://github.com/redis/redis.git

(1.2) GCC编译器

编译之前,需要安装一些依赖,Redis 是 C 语言编写的,需要 gcc 编译器。

[weikeqin@localhost ~ ]$ gcc -v
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
[weikeqin@localhost ~ ]$

(1.3) 源码安装

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

[weikeqin@localhost redis (6.0 <) ]$ pwd
/Users/weikeqin/Workspaces/middleware/redis
[weikeqin@localhost redis (6.0 <) ]$ 
[weikeqin@localhost redis (6.0 <) ]$ make CFLAGS="-g -O0" MALLOC=jemalloc 
cd src && /Library/Developer/CommandLineTools/usr/bin/make all
/bin/sh: pkg-config: command not found
    CC Makefile.dep
/bin/sh: pkg-config: command not found
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
rm -f adlist.d quicklist.d ae.d anet.d dict.d server.d sds.d zmalloc.d lzf_c.d lzf_d.d pqsort.d zipmap.d sha1.d ziplist.d release.d networking.d util.d object.d db.d replication.d rdb.d t_string.d t_list.d t_set.d t_zset.d t_hash.d config.d aof.d pubsub.d multi.d debug.d sort.d intset.d syncio.d cluster.d crc16.d endianconv.d slowlog.d scripting.d bio.d rio.d rand.d memtest.d crcspeed.d crc64.d bitops.d sentinel.d notify.d setproctitle.d blocked.d hyperloglog.d latency.d sparkline.d redis-check-rdb.d redis-check-aof.d geo.d lazyfree.d module.d evict.d expire.d geohash.d geohash_helper.d childinfo.d defrag.d siphash.d rax.d t_stream.d listpack.d localtime.d lolwut.d lolwut5.d lolwut6.d acl.d gopher.d tracking.d connection.d tls.d sha256.d timeout.d setcpuaffinity.d mt19937-64.d anet.d adlist.d dict.d redis-cli.d zmalloc.d release.d ae.d crcspeed.d crc64.d siphash.d crc16.d mt19937-64.d ae.d anet.d redis-benchmark.d adlist.d dict.d zmalloc.d siphash.d mt19937-64.d
(cd ../deps && /Library/Developer/CommandLineTools/usr/bin/make distclean)
(cd hiredis && /Library/Developer/CommandLineTools/usr/bin/make clean) > /dev/null || true
(cd linenoise && /Library/Developer/CommandLineTools/usr/bin/make clean) > /dev/null || true
(cd lua && /Library/Developer/CommandLineTools/usr/bin/make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && /Library/Developer/CommandLineTools/usr/bin/make distclean) > /dev/null || true


9 warnings generated.
    LINK redis-benchmark
    INSTALL redis-check-rdb
    INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

[weikeqin@localhost redis (6.0 <) ]$  
[weikeqin@localhost redis (6.0 <) ]$ 

make完可以在src目录看到编译链接后的redis-server文件

编译后的redis-server文件


(1.4) 启动redis-server

/Users/weikeqin/Workspaces/middleware/redis/src/redis-server /Users/weikeqin/Workspaces/middleware/redis/redis.conf
43277:C 22 Jul 2023 15:25:22.277 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
43277:C 22 Jul 2023 15:25:22.277 # Redis version=6.0.20, bits=64, commit=de0d9632, modified=0, pid=43277, just started
43277:C 22 Jul 2023 15:25:22.277 # Configuration loaded
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.20 (de0d9632/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 43277
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

43277:M 22 Jul 2023 15:25:22.282 # Server initialized
43277:M 22 Jul 2023 15:25:22.283 * Ready to accept connections

(2) 用开发工具Clion调试

这里我用的clion来调试的

(2.1) 调试配置

redis-server调试配置
redis-server调试配置

(2.2) 开始调试

redis-server调试

(2.2.1) 调试main方法

redis-server调试-启动

(2.2.2) 加载Server配置

redis-server调试截图

(2.2.3) 读取配置文件

redis-server调试-读取配置文件


(3) 用开发工具VS-Code调试

make install 后的截图,可以看到 生成了 redis-server 文件

编译安装完的截图


(3.1) 调试配置

修改VS-Code 配置 launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "redis-server",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/src/redis-server",
            "args": [
                "./redis.conf"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

Redis-VSCode-launch_json配置截图

(3.2) 开始调试

Redis-VSCode-Debug

(3.2.1) 调试main方法

Redis-VSCode-Debug-main



(4) 遇到的问题

(4.1) ‘lua.h’ file not found

/usr/bin/gcc -fdiagnostics-color=always -g /Users/weikeqin/WorkSpaces/middleware/redis/src/server.c -o /Users/weikeqin/WorkSpaces/middleware/redis/src/server
In file included from /Users/weikeqin/WorkSpaces/middleware/redis/src/server.c:30:
/Users/weikeqin/WorkSpaces/middleware/redis/src/server.h:50:10: fatal error: 'lua.h' file not found
#include <lua.h>
         ^~~~~~~
1 error generated.

生成已完成,但出现错误。
终端进程启动失败(退出代码: -1)

少了lua的库

在mac下可以直接通过 brew install lua 安装lua

也可以在 https://www.lua.org/download.html 下载安装
可以选择源码安装,也可以选择二进制包安装

安装完需要重新 编译安装Redis


(4.2) ‘lua.h’ file not found gcc

'lua.h' file not found gcc
[weikeqin@computer ~ ]$ fd lua /usr/local/include
/usr/local/include/lua
/usr/local/include/lua5.4
[weikeqin@computer ~ ]$

homebrew 安装在 /usr/local/Cellar/

[weikeqin@computer ~ ]$  fd lua.h /usr/local/Cellar/lua/5.4.4_1/
/usr/local/Cellar/lua/5.4.4_1/include/lua/lua.h
/usr/local/Cellar/lua/5.4.4_1/include/lua/lua.hpp
/usr/local/Cellar/lua/5.4.4_1/include/lua5.4/lua.h
/usr/local/Cellar/lua/5.4.4_1/include/lua5.4/lua.hpp
[weikeqin@computer ~ ]$

CFLAGS配置为包含 /usr/local/Cellar/lua/5.4.4_1/include/lua