通过源代码编译来安装 Redis ,在自己的机器上搭建一套可以 Debug 的 Redis 源码环境。
推荐在 Linux 或者 MacOS 上Debug。
(1) 调试环境准备
- 下载源码
- 安装C语言GCC编译器
- 通过源码编码Redis源码
- 启动Redis开始Debug
(1.1) 下载源码
git clone https://github.com/redis/redis.git
(1.2) GCC编译器
编译之前,需要安装一些依赖,Redis 是 C 语言编写的,需要 gcc 编译器。
[weikeqin@MacBookPro ~ ]$ 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@MacBookPro ~ ]$
(1.3) 源码安装
make
命令会执行Makefile
文件里准备的命令,主要是编译、链接等。
make
命令可以指定参数
-O0
参数表示告诉编译器不要优化代码,防止在 Debug 的时候, IDE Redis 源码 与 实际运行的 对应不上。
MALLOC=jemalloc
,Mac OS 系统Redis默认内存分配器是libc,通过 MALLOC=jemalloc
指定使用 jemalloc 内存分配器
[weikeqin@MacBookPro redis (6.0 <) ]$ pwd
/Users/weikeqin/Workspaces/middleware/redis
[weikeqin@MacBookPro redis (6.0 <) ]$
[weikeqin@MacBookPro 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@MacBookPro redis (6.0 <) ]$
[weikeqin@MacBookPro redis (6.0 <) ]$
make完可以在src目录看到编译链接后的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
来调试的
(2.1) 调试配置


(2.2) 开始调试

(2.2.1) 调试main方法
(2.2.2) 加载Server配置
(2.2.3) 读取配置文件