当前位置:   article > 正文

一篇文章玩转GDB/LLDB调试Redis源码_lldb 调试 redis

lldb 调试 redis

码字不易,转载请附原链,搬砖繁忙回复不及时见谅,技术交流请加QQ群:909211071

一、安装调试版redis

参考博客:https://success.blog.csdn.net/article/details/83659776

注意需要在makefile的开头定义CFLAGS 变量:CFLAGS = -g ,否则调试过程中无法跟踪代码

二、使用gdb启动redis-server

[why@whydeMacBook-Pro] ~$sudo gdb /usr/local/bin/redis-server /usr/local/etc/redis/redis_6379.conf 

会遇到如下问题:

  1. (gdb) r
  2. Starting program: /usr/local/bin/redis-server
  3. Unable to find Mach task port for process-id 49363: (os/kern) failure (0x5).
  4. (please check gdb is codesigned - see taskgated(8))

mac的签名问题,参考这篇文章解决:https://blog.csdn.net/qq_33154343/article/details/104784641

启动成功如下:

  1. [why@whydeMacBook-Pro] ~$sudo gdb /usr/local/bin/redis-server /usr/local/etc/redis/redis_6379.conf
  2. GNU gdb (GDB) 8.3
  3. Copyright (C) 2019 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.
  7. Type "show copying" and "show warranty" for details.
  8. This GDB was configured as "x86_64-apple-darwin18.5.0".
  9. Type "show configuration" for configuration details.
  10. For bug reporting instructions, please see:
  11. <http://www.gnu.org/software/gdb/bugs/>.
  12. Find the GDB manual and other documentation resources online at:
  13. <http://www.gnu.org/software/gdb/documentation/>.
  14. For help, type "help".
  15. Type "apropos word" to search for commands related to "word"...
  16. Reading symbols from /usr/local/bin/redis-server...
  17. "/usr/local/etc/redis/redis_6379.conf" is not a core dump: file format not recognized
  18. (gdb)

执行r启动

三、GDB调试过程

help all 查看支持的命令

追一下set命令的执行过程:

  1. (gdb) b setCommand
  2. Breakpoint 1 at 0x100035a7e: file t_string.c, line 102.
  3. (gdb) c
  4. (gdb)

cli模式连接redis并执行set命令,此时程序会阻塞在这:

  1. [why@whydeMacBook-Pro] /usr/local/bin$redis-cli
  2. 127.0.0.1:6379> set why why

gdb进入断点调试状态:

  1. Thread 3 hit Breakpoint 1, setCommand (c=0x10180c400) at t_string.c:102
  2. 102 for (j = 3; j < c->argc; j++) {
  3. (gdb)
  4. (gdb)

gdb交互输入n单步执行,c跳到下一个断点处

gdb focus开启可视化查看代码(Ctrl + X + A退出):

  1. ┌──t_string.c─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  2. 90 if (expire) notifyKeyspaceEvent(NOTIFY_GENERIC, │
  3. 91 "expire",key,c->db->id); │
  4. 92 addReply(c, ok_reply ? ok_reply : shared.ok); │
  5. 93 } │
  6. 94
  7. 95 /* SET key value [NX] [XX] [EX <seconds>] [PX <milliseconds>] */
  8. 96 void setCommand(client *c) { │
  9. 97 int j; │
  10. 98 robj *expire = NULL; │
  11. 99 int unit = UNIT_SECONDS; │
  12. 100 int flags = OBJ_SET_NO_FLAGS; │
  13. 101
  14. B+>102 for (j = 3; j < c->argc; j++) { │
  15. 103 char *a = c->argv[j]->ptr; │
  16. 104 robj *next = (j == c->argc-1) ? NULL : c->argv[j+1]; │
  17. 105
  18. 106 if ((a[0] == 'n' || a[0] == 'N') &&
  19. 107 (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
  20. 108 !(flags & OBJ_SET_XX)) │
  21. 109 { │
  22. 110 flags |= OBJ_SET_NX; │
  23. 111 } else if ((a[0] == 'x' || a[0] == 'X') &&
  24. 112 (a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
  25. 113 !(flags & OBJ_SET_NX)) │
  26. 114 { │
  27. 115 flags |= OBJ_SET_XX; │
  28. 116 } else if ((a[0] == 'e' || a[0] == 'E') &&
  29. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  30. native Thread 0xf03 of pro In: setCommand L102 PC: 0x109ce7a7e
  31. warning: Invalid window specified.
  32. The window name specified must be valid and visible.
  33. Focus set to src window.
  34. (gdb)

接下来便可以一步步输入n向下追踪代码执行了。

三、LLDB调试

help查看命令,命令+help查看支持的参数

先启动服务:

/usr/local/bin/redis-server /usr/local/etc/redis/redis_6379.conf 

登陆客户端:

redis-cli

启动lldb:

lldb

在set命令入口加断点:

(lldb) b setCommand

执行set命令:

  1. [why@whydeMacBook-Pro] ~/Desktop/source/redis-5.0.0$redis-cli
  2. 127.0.0.1:6379> set why why

自动执行到断点处:

  1. Process 15971 stopped
  2. * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  3. frame #0: 0x0000000101a97a7e redis-server`setCommand(c=0x00007fe3ca005e00) at t_string.c:102:24
  4. 99 int unit = UNIT_SECONDS;
  5. 100 int flags = OBJ_SET_NO_FLAGS;
  6. 101
  7. -> 102 for (j = 3; j < c->argc; j++) {
  8. 103 char *a = c->argv[j]->ptr;
  9. 104 robj *next = (j == c->argc-1) ? NULL : c->argv[j+1];
  10. 105
  11. Target 0: (redis-server) stopped.

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/916316
推荐阅读
相关标签
  

闽ICP备14008679号