在 MacOS 系统下,经常使用 LLDB 调试程序,类似 《Gdb 使用》,记录一下常用命令和调试使用。
1. 命令
命令 | 描述 |
---|---|
help | 命令帮助。 |
lldb -p <pid> | 调试指定 pid 进程。 |
b | 设置断点。 |
breakpoint list | 列出所有断点。 |
breakpoint delete <number> | 删除断点,不带 number,默认删除所有断点。 |
si/ni | 类似(s/n命令),(si/ni)针对汇编指令。 |
register <read/write> | 读写寄存器。 |
fr v | 显示函数局部变量。 |
2. 调试
lldb 调试带参数的服务进程。
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 执行带参数调试命令。
[wenfh2020]$ lldb kimserver -- config.json
(lldb) target create "kimserver"
Current executable set to 'kimserver' (x86_64).
(lldb) settings set -- target.run-args "config.json"
# 设置环境变量。
(lldb) env DYLD_LIBRARY_PATH=&DYLD_LIBRARY_PATH:/usr/local/lib/mariadb/
# 设置断点。
(lldb) b zk_mgr.cpp : 59
Breakpoint 1: where = kimserver`kim::ZkMgr::load_zk_client() + 830 at zk_mgr.cpp:59:21, address = 0x00000001000322be
# 运行程序。
(lldb) r
Process 16993 launched: '/Users/wenfh2020/src/other/kimserver/bin/kimserver' (x86_64)
Process 16993 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x00000001000322be kimserver`kim::ZkMgr::load_zk_client(this=0x0000000101604600) at zk_mgr.cpp:59:21
56 return false;
57 }
58
-> 59 if (m_zk_client == nullptr) {
60 m_zk_client = new ZooKeeperClient(m_logger);
61 if (m_zk_client == nullptr) {
62 LOG_ERROR("alloc zk mgr failed!");
Target 0: (kimserver) stopped.
# 监控变量 m_zk_client 变化。
(lldb) watchpoint set variable m_zk_client
Watchpoint created: Watchpoint 1: addr = 0x101604618 size = 8 state = enabled type = w
watchpoint spec = 'm_zk_client'
new value: 0x0000000000000000
# 继续运行中断程序。
(lldb) c
Process 16993 resuming
# 变量 m_zk_client 有变化,调试程序中断在变量变化源码位置。
Watchpoint 1 hit:
old value: 0x0000000000000000
new value: 0x0000000101622460
Process 16993 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 1
frame #0: 0x000000010003231b kimserver`kim::ZkMgr::load_zk_client(this=0x0000000101604600) at zk_mgr.cpp:61:25
58
59 if (m_zk_client == nullptr) {
60 m_zk_client = new ZooKeeperClient(m_logger);
-> 61 if (m_zk_client == nullptr) {
62 LOG_ERROR("alloc zk mgr failed!");
63 return false;
64 }
Target 0: (kimserver) stopped.
# 继续运行中断程序。
(lldb) c
Process 16993 resuming
...
# 中断查看程序运行线程。
(lldb) thread list
Process 16993 stopped
* thread #1: tid = 0x3f8675, 0x00007fff6f7cc7e6 libsystem_kernel.dylib`__select + 10, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
thread #2: tid = 0x3f9288, 0x00007fff6f7cd5be libsystem_kernel.dylib`poll + 10
thread #3: tid = 0x3f9289, 0x00007fff6f7c8916 libsystem_kernel.dylib`__psynch_cvwait + 10
thread #4: tid = 0x3f928a, 0x00007fff6f7c8916 libsystem_kernel.dylib`__psynch_cvwait + 10
# 查看线程 1。
(lldb) thread select 1
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
frame #0: 0x00007fff6f7cc7e6 libsystem_kernel.dylib`__select + 10
libsystem_kernel.dylib`__select:
-> 0x7fff6f7cc7e6 <+10>: jae 0x7fff6f7cc7f0 ; <+20>
0x7fff6f7cc7e8 <+12>: movq %rax, %rdi
0x7fff6f7cc7eb <+15>: jmp 0x7fff6f7c5381 ; cerror
0x7fff6f7cc7f0 <+20>: retq
# 查看线程堆栈。
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
* frame #0: 0x00007fff6f7cc7e6 libsystem_kernel.dylib`__select + 10
frame #1: 0x000000010071e148 libev.4.dylib`select_poll + 165
frame #2: 0x000000010071b943 libev.4.dylib`ev_run + 1204
frame #3: 0x000000010000464e kimserver`kim::Events::run(this=0x0000000101608240) at events.cpp:40:9
frame #4: 0x0000000100013542 kimserver`kim::Network::run(this=0x0000000101666000) at network.cpp:45:19
frame #5: 0x0000000100005df2 kimserver`kim::Manager::run(this=0x00007ffeefbff268) at manager.cpp:29:16
frame #6: 0x0000000100023fb5 kimserver`main(argc=2, argv=0x00007ffeefbff550) at server.cpp:34:9
frame #7: 0x00007fff6f67c2e5 libdyld.dylib`start + 1
(lldb)