命令:编译的时候添加-g的信息
gdb ./test #启动调试程序
run #程序执行
step #单步执行
next #单步执行
continue #继续执行
b 16 #设置断点
b func #在函数入口设置断点
p i #打印变量
l #列出函数 l 10 显示10行附件的代码
bt #显示堆栈信息
finish #退出函数
backtrace #打印堆栈信息
info thread #打印线程信息
quit #退出调试
# 需要安装 sudo apt install lldb
lldb ./test
# 1. 基本命令与gdb相同,LLDB的命令更加直观和简洁,更容易学习和使用
# 2. LLDB支持跨平台
# 3. LLDB具有更现代化和用户友好的命令行界面,支持自动补全和上下文感知等功能
# 其他命令
# 1. 查看当前堆栈:使用`thread backtrace`命令可以查看当前线程的函数调用堆栈;
# 2.修改变量的值:使用`expression`命令可以修改变量的值。例如,`expression variable = 10`可以将`variable`的值设置为10;
# 3. 观察变量得到值,`watchpoint set variable result`;
# 4. 查看线程:使用`thread list`命令可以查看当前程序中的线程列表及其对应的ID;
# 5. 切换线程:使用`thread select <线程ID>`命令可以切换到指定的线程进行调试;
调试示例:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <thread>
#include <chrono>
void straceTest() {
char buffer[256];
std::cout << "sizeof(buffer) = " << sizeof(buffer) << std::endl;
std::ifstream in("input.txt");
if (!in.is_open()) {
std::cout << "error open file" << std::endl;
exit(-1);
}
while (!in.eof()) {
in.getline(buffer, 100);
std::cout << buffer << std::endl;
}
in.close();
}
int fib(int n) {
int result = 0;
for (int i = 0; i <= n; ++i) {
result += i;
}
return result;
}
void testGBD() {
std::thread t(straceTest);
t.join();
int result = fib(9);
std::cout << result << std::endl;
}
int main() {
// straceTest();
testGBD();
return 0;
}
调试过程(部分输出):
(base) ubuntu@VM-8-14-ubuntu:~/cpp_learn/CPP-Basics$ gdb ./test
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 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-linux-gnu".
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 ./test...
(gdb) b fib
Breakpoint 1 at 0x2547: file tiaoshi.cpp, line 22.
(gdb) run
Starting program: /home/ubuntu/cpp_learn/CPP-Basics/test
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7a58700 (LWP 3576336)]
sizeof(buffer) = 256
hhh
xxx
ttttttt
pppppppppppppppppppppppppppp
[Thread 0x7ffff7a58700 (LWP 3576336) exited]
Thread 1 "test" hit Breakpoint 1, fib (n=0) at tiaoshi.cpp:22
22 int fib(int n) {
(gdb) backtrace
#0 fib (n=0) at tiaoshi.cpp:22
#1 0x00005555555565be in testGBD () at tiaoshi.cpp:32
#2 0x000055555555663c in main () at tiaoshi.cpp:39
(base) ubuntu@VM-8-14-ubuntu:~/cpp_learn/CPP-Basics$ lldb ./test
(lldb) target create "./test"
Current executable set to '/home/ubuntu/cpp_learn/CPP-Basics/test' (x86_64).
(lldb) b fib
Breakpoint 1: where = test`fib(int) + 11 at tiaoshi.cpp:23:9, address = 0x0000000000002552
(lldb) run
Process 3581640 launched: '/home/ubuntu/cpp_learn/CPP-Basics/test' (x86_64)
sizeof(buffer) = 256
hhh
xxx
ttttttt
pppppppppppppppppppppppppppp
Process 3581640 stopped
* thread #1, name = 'test', stop reason = breakpoint 1.1
frame #0: 0x0000555555556552 test`fib(n=9) at tiaoshi.cpp:23:9
20 }
21
22 int fib(int n) {
-> 23 int result = 0;
24 for (int i = 0; i <= n; ++i) {
25 result += i;
26 }
(lldb) n
Process 3581640 stopped
* thread #1, name = 'test', stop reason = step over
frame #0: 0x0000555555556559 test`fib(n=9) at tiaoshi.cpp:24:14
21
22 int fib(int n) {
23 int result = 0;
-> 24 for (int i = 0; i <= n; ++i) {
25 result += i;
26 }
27 return result;
(lldb) bt
* thread #1, name = 'test', stop reason = step over
* frame #0: 0x0000555555556559 test`fib(n=9) at tiaoshi.cpp:24:14
frame #1: 0x00005555555565be test`testGBD() at tiaoshi.cpp:32:21
frame #2: 0x000055555555663c test`main at tiaoshi.cpp:39:12
frame #3: 0x00007ffff7bd1083 libc.so.6`__libc_start_main(main=(test`main at tiaoshi.cpp:37:12), argc=1, argv=0x00007fffffffdf48, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fffffffdf38) at libc-start.c:308:16
frame #4: 0x000055555555630e test`_start + 46
(lldb) thread list
Process 3581640 stopped
* thread #1: tid = 3581640, 0x0000555555556559 test`fib(n=9) at tiaoshi.cpp:24:14, name = 'test', stop reason = step over
(lldb) thread backtrace
* thread #1, name = 'test', stop reason = step over
* frame #0: 0x0000555555556559 test`fib(n=9) at tiaoshi.cpp:24:14
frame #1: 0x00005555555565be test`testGBD() at tiaoshi.cpp:32:21
frame #2: 0x000055555555663c test`main at tiaoshi.cpp:39:12
frame #3: 0x00007ffff7bd1083 libc.so.6`__libc_start_main(main=(test`main at tiaoshi.cpp:37:12), argc=1, argv=0x00007fffffffdf48, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fffffffdf38) at libc-start.c:308:16
frame #4: 0x000055555555630e test`_start + 46
(lldb) watchpoint set variable result
Watchpoint created: Watchpoint 2: addr = 0x7fffffffddf8 size = 4 state = enabled type = w
declare @ '/home/ubuntu/cpp_learn/CPP-Basics/tiaoshi.cpp:23'
watchpoint spec = 'result'
new value: 0
(lldb) n
Process 3581640 stopped
* thread #1, name = 'test', stop reason = step over
frame #0: 0x0000555555556568 test`fib(n=9) at tiaoshi.cpp:25:16
22 int fib(int n) {
23 int result = 0;
24 for (int i = 0; i <= n; ++i) {
-> 25 result += i;
26 }
27 return result;
28 }
(lldb)
Watchpoint 2 hit:
old value: 0
new value: 1
Process 3581640 stopped
* thread #1, name = 'test', stop reason = watchpoint 2
frame #0: 0x000055555555656e test`fib(n=9) at tiaoshi.cpp:24:5
21
22 int fib(int n) {
23 int result = 0;
-> 24 for (int i = 0; i <= n; ++i) {
25 result += i;
26 }
27 return result;