Linux 中 gdb 调试 Redis

这月开启实战 gdb 调试 Redis 源码系列,以目前官网上最新的稳定版本 6.2.4 来调式。

首先,下载源码。

wget https://download.redis.io/releases/redis-6.2.4.tar.gz
tar xf redis-6.2.4.tar.gz
cd redis-6.2.4

其次,解压并编译 make CFLAGS="-g -O0",不用 make install。

最后,使用 gdb src/redis-server 进行调试。

# gdb src/redis-server 
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /usr/local/redis-6.2.4/src/redis-server...done.
(gdb) r
Starting program: /usr/local/redis-6.2.4/src/redis-server 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
10988:C 05 Jun 2021 10:34:39.825 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
10988:C 05 Jun 2021 10:34:39.825 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=10988, just started
10988:C 05 Jun 2021 10:34:39.825 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/local/redis-6.2.4/src/redis-server /path/to/redis.conf
10988:M 05 Jun 2021 10:34:39.829 * monotonic clock: POSIX clock_gettime
10988:M 05 Jun 2021 10:34:39.830 # Warning: Could not create server TCP listening socket ::*:6379: unable to bind socket, errno: 97
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 10988
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

10988:M 05 Jun 2021 10:34:39.830 # Server initialized
10988:M 05 Jun 2021 10:34:39.830 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
10988:M 05 Jun 2021 10:34:39.830 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
[New Thread 0x7ffff096e700 (LWP 10994)]
[New Thread 0x7fffeff6d700 (LWP 10995)]
[New Thread 0x7fffef56c700 (LWP 10996)]
[New Thread 0x7fffeeb6b700 (LWP 10997)]
10988:M 05 Jun 2021 10:34:39.833 * Ready to accept connections
### 配置 VSCode 调试 Redis 源码 为了在 VSCode 中成功调试 Redis 源码,建议使用 Linux 环境下的 WSL(Windows Subsystem for Linux),这能提供更稳定和高效的开发体验[^3]。 #### 安装并配置 WSL 和 Redis 源码 确保已安装 WSL 并设置了合适的 Linux 发行版。接着,在 WSL 终端中克隆 Redis 的官方仓库到本地机器上: ```bash git clone https://github.com/redis/redis.git ~/projects/ cd ~/projects/redis make distclean && make -j$(nproc) ``` 上述命令会清理旧编译文件并尽可能多地利用 CPU 核心数加速构建过程[^1]。 #### 打开项目于 VSCode 内 通过快捷键 `Ctrl+Shift+P` 召唤命令面板,输入 `WSL: Open Folder in WSL...` 来选取先前下载好的 Redis 源码所在位置[^5]。 #### 创建 launch.json 文件用于启动调试会话 创建 `.vscode` 文件夹并在其中放置名为 `launch.json` 的 JSON 文件,其内容如下所示[^4]: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch redis-server with config", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/src/redis-server", "args": ["${workspaceFolder}/redis.conf"], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build_redis" } ] } ``` 此配置指定了 GDB 作为调试器,并设定了要运行的目标程序及其参数;还定义了一个预启动任务 `"preLaunchTask"` ,该任务将在每次开始调试前自动重新编译 Redis。 #### 编写 tasks.json 自动化编译流程 同样位于 `.vscode` 下新建或编辑现有的 `tasks.json` 文件,加入下面的内容以便实现一键编译功能: ```json { "version": "2.0.0", "tasks": [ { "label": "build_redis", "type": "shell", "command": "make -C ${workspaceFolder} -j$(nproc)", "group": { "kind": "build", "isDefault": true }, "problemMatcher": [] } ] } ``` 这段脚本会在调用时进入工作区根目录执行 Makefile 构建指令,从而保证最新版本的二进制文件始终可用。 完成以上步骤之后就可以直接点击左侧边栏中的“Run and Debug”图标选择刚才建立过的 `(gdb) Launch redis-server with config` 开始调试了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值