lldb调试redis

lldb调试redis步骤

1、编译redis并启动redis服务
「目前位于redis源码目录」

1
make && ./src/redis-server &

2、lldb调试redis-cli

1
2
3
lldb src/redis-cli
(lldb) target create "src/redis-cli"
Current executable set to 'src/redis-cli' (x86_64).

4、设置断点
注:「redis-cli.c文件的第986行,redis版本为:3.0.2」,第986行代码如下

1
-> 986 if (line[0] != '\0') {

1
2
(lldb) b redis-cli.c:986
Breakpoint 1: where = redis-cli`main + 11728 [inlined] repl + 74 at redis-cli.c:2288, address = 0x00000001000081b0

5、运行redis-cli

1
2
3
4
(lldb) process launch
P
Process 7181 launched: '/Users/sunnysmilez/Desktop/code/redis-3.0.2/src/redis-cli' (x86_64)
P

6、执行redis命令
注「使用ctrl+c进去信号处理器」

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> SET KEY MS
Process 7181 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
frame #0: 0x00007fff52bf4142 libsystem_kernel.dylib`read + 10
libsystem_kernel.dylib`read:
-> 0x7fff52bf4142 <+10>: jae 0x7fff52bf414c ; <+20>
0x7fff52bf4144 <+12>: movq %rax, %rdi
0x7fff52bf4147 <+15>: jmp 0x7fff52be9b0e ; cerror
0x7fff52bf414c <+20>: retq
Target 1: (redis-cli) stopped.

7、使用c命令进入断点
注:「c也就是continue命令」

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(lldb) c
Process 7181 resuming
redis-cli was compiled with optimization - stepping may behave oddly; variables may not be available.
Process 7181 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x00000001000081b0 redis-cli`main [inlined] repl at redis-cli.c:986 [opt]
983
984 cliRefreshPrompt();
985 while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
-> 986 if (line[0] != '\0') {
987 argv = sdssplitargs(line,&argc);
988 if (history) linenoiseHistoryAdd(line);
989 if (historyfile) linenoiseHistorySave(historyfile);
Target 1: (redis-cli) stopped.

8、使用n向下执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(lldb) n
Process 7181 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
frame #0: 0x00000001000081bb redis-cli`main [inlined] repl at redis-cli.c:987 [opt]
984 cliRefreshPrompt();
985 while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
986 if (line[0] != '\0') {
-> 987 argv = sdssplitargs(line,&argc);
988 if (history) linenoiseHistoryAdd(line);
989 if (historyfile) linenoiseHistorySave(historyfile);
990
Target 1: (redis-cli) stopped.
(lldb) n
Process 7181 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
frame #0: 0x00000001000081d2 redis-cli`main [inlined] repl at redis-cli.c:988 [opt]
985 while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
986 if (line[0] != '\0') {
987 argv = sdssplitargs(line,&argc);
-> 988 if (history) linenoiseHistoryAdd(line);
989 if (historyfile) linenoiseHistorySave(historyfile);
990
991 if (argv == NULL) {
Target 1: (redis-cli) stopped.
(lldb) n
Process 7181 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
frame #0: 0x00000001000081df redis-cli`main [inlined] repl at redis-cli.c:989 [opt]
986 if (line[0] != '\0') {
987 argv = sdssplitargs(line,&argc);
988 if (history) linenoiseHistoryAdd(line);
-> 989 if (historyfile) linenoiseHistorySave(historyfile);
990
991 if (argv == NULL) {
992 printf("Invalid argument(s)\n");
Target 1: (redis-cli) stopped.
(lldb) n
Process 7181 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
frame #0: 0x00000001000081e7 redis-cli`main [inlined] repl at redis-cli.c:991 [opt]
988 if (history) linenoiseHistoryAdd(line);
989 if (historyfile) linenoiseHistorySave(historyfile);
990
-> 991 if (argv == NULL) {
992 printf("Invalid argument(s)\n");
993 free(line);
994 continue;
Target 1: (redis-cli) stopped.

9、使用p打印值

1
2
3
4
5
6
7
8
(lldb) p *argv
(sds) $0 = 0x0000000100203e88 "SET"
(lldb) p *(argv+1)
(sds) $1 = 0x0000000100203ed8 "KEY"
(lldb) p *(argv+2)
(sds) $2 = 0x0000000100203ee8 "MS"
(lldb) p line
(char *) $3 = 0x0000000100202ef0 "SET KEY MS\n"

10、结束

使用n和p去查看你想要看到的一切值吧

坚持原创技术分享,您的支持将鼓励我继续创作!