赞
踩
码字不易,转载请附原链,搬砖繁忙回复不及时见谅,技术交流请加QQ群:909211071
参考博客:https://success.blog.csdn.net/article/details/83659776
注意需要在makefile的开头定义CFLAGS 变量:CFLAGS = -g ,否则调试过程中无法跟踪代码
[why@whydeMacBook-Pro] ~$sudo gdb /usr/local/bin/redis-server /usr/local/etc/redis/redis_6379.conf
会遇到如下问题:
- (gdb) r
- Starting program: /usr/local/bin/redis-server
- Unable to find Mach task port for process-id 49363: (os/kern) failure (0x5).
- (please check gdb is codesigned - see taskgated(8))
mac的签名问题,参考这篇文章解决:https://blog.csdn.net/qq_33154343/article/details/104784641
启动成功如下:
- [why@whydeMacBook-Pro] ~$sudo gdb /usr/local/bin/redis-server /usr/local/etc/redis/redis_6379.conf
- GNU gdb (GDB) 8.3
- Copyright (C) 2019 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-apple-darwin18.5.0".
- Type "show configuration" for configuration details.
- For bug reporting instructions, please see:
- <http://www.gnu.org/software/gdb/bugs/>.
- Find the GDB manual and other documentation resources online at:
- <http://www.gnu.org/software/gdb/documentation/>.
-
- For help, type "help".
- Type "apropos word" to search for commands related to "word"...
- Reading symbols from /usr/local/bin/redis-server...
- "/usr/local/etc/redis/redis_6379.conf" is not a core dump: file format not recognized
- (gdb)

执行r启动
help all 查看支持的命令
追一下set命令的执行过程:
- (gdb) b setCommand
- Breakpoint 1 at 0x100035a7e: file t_string.c, line 102.
- (gdb) c
- (gdb)
cli模式连接redis并执行set命令,此时程序会阻塞在这:
- [why@whydeMacBook-Pro] /usr/local/bin$redis-cli
- 127.0.0.1:6379> set why why
gdb进入断点调试状态:
-
- Thread 3 hit Breakpoint 1, setCommand (c=0x10180c400) at t_string.c:102
- 102 for (j = 3; j < c->argc; j++) {
- (gdb)
- (gdb)
gdb交互输入n单步执行,c跳到下一个断点处
gdb focus开启可视化查看代码(Ctrl + X + A退出):
- ┌──t_string.c─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
- │90 if (expire) notifyKeyspaceEvent(NOTIFY_GENERIC, │
- │91 "expire",key,c->db->id); │
- │92 addReply(c, ok_reply ? ok_reply : shared.ok); │
- │93 } │
- │94 │
- │95 /* SET key value [NX] [XX] [EX <seconds>] [PX <milliseconds>] */ │
- │96 void setCommand(client *c) { │
- │97 int j; │
- │98 robj *expire = NULL; │
- │99 int unit = UNIT_SECONDS; │
- │100 int flags = OBJ_SET_NO_FLAGS; │
- │101 │
- B+>│102 for (j = 3; j < c->argc; j++) { │
- │103 char *a = c->argv[j]->ptr; │
- │104 robj *next = (j == c->argc-1) ? NULL : c->argv[j+1]; │
- │105 │
- │106 if ((a[0] == 'n' || a[0] == 'N') && │
- │107 (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' && │
- │108 !(flags & OBJ_SET_XX)) │
- │109 { │
- │110 flags |= OBJ_SET_NX; │
- │111 } else if ((a[0] == 'x' || a[0] == 'X') && │
- │112 (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' && │
- │113 !(flags & OBJ_SET_NX)) │
- │114 { │
- │115 flags |= OBJ_SET_XX; │
- │116 } else if ((a[0] == 'e' || a[0] == 'E') && │
- └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
- native Thread 0xf03 of pro In: setCommand L102 PC: 0x109ce7a7e
- warning: Invalid window specified.
- The window name specified must be valid and visible.
-
- Focus set to src window.
- (gdb)

接下来便可以一步步输入n向下追踪代码执行了。
help查看命令,命令+help查看支持的参数
先启动服务:
/usr/local/bin/redis-server /usr/local/etc/redis/redis_6379.conf
登陆客户端:
redis-cli
启动lldb:
lldb
在set命令入口加断点:
(lldb) b setCommand
执行set命令:
- [why@whydeMacBook-Pro] ~/Desktop/source/redis-5.0.0$redis-cli
- 127.0.0.1:6379> set why why
自动执行到断点处:
- Process 15971 stopped
- * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
- frame #0: 0x0000000101a97a7e redis-server`setCommand(c=0x00007fe3ca005e00) at t_string.c:102:24
- 99 int unit = UNIT_SECONDS;
- 100 int flags = OBJ_SET_NO_FLAGS;
- 101
- -> 102 for (j = 3; j < c->argc; j++) {
- 103 char *a = c->argv[j]->ptr;
- 104 robj *next = (j == c->argc-1) ? NULL : c->argv[j+1];
- 105
- Target 0: (redis-server) stopped.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。