GDB调式

程序遇到bug时候除了可以查看报错信息,跟踪Log,也可以通过调试的手段去解决。使用IDE时我们可以轻而易举的打断点调试,但是一旦脱离IDE,GDB就是一款强大的调式工具。

GDB 调试 What is GDB?

https://www.gnu.org/software/gdb/

GDB, the GNU Project debugger, allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed.

使用GDB

调试编译时,关掉编译器的优化选项(-O), 并打开调试选项(-g)。另外,-Wall选项打开所有警告信息。

arvin@VM-0-2-ubuntu:~/gdb$ g++ helloGDB.cpp print.cpp -g -Wall -o helloGDB arvin@VM-0-2-ubuntu:~/gdb$ ls helloGDB helloGDB.cpp print.cpp print.h gdb常用调试命令 启动和退出

启动 gdb helloGDB
关闭 quit

查看代码

当前文件

list 等于 l (gdb) list 1 #include"print.h" 2 #include<string> 3 using namespace std; 4 5 6 int main() 7 { 8 cout << "Hello GDB\n"; 9 cout << "What is GDB?\n"; 10 cout << "The GNU Project Debugger\n"; 从指定行显示 l 行号 l 14 (gdb) l 14 9 cout << "What is GDB?\n"; 10 cout << "The GNU Project Debugger\n"; 11 cout << "Hello GDB\n"; 12 cout << "What is GDB?\n"; 13 cout << "The GNU Project Debugger\n"; 14 cout << "Hello GDB\n"; 15 cout << "What is GDB?\n"; 16 cout << "The GNU Project Debugger\n"; 17 printNumber(10); 18 cout << "The GNU Project Debugger\n"; 从指定函数显示 l 函数名 l printNumber (gdb) l printNumber 1 #include "print.h" 2 3 void printNumber(int size) 4 { 5 for (int i = 0; i < size; ++i) { 6 cout << "The number is " << i << endl; 7 } 8 } 9

非当前文件

l 文件名:行号 l print.cpp:2 (gdb) l print.cpp:2 1 #include "print.h" 2 3 void printNumber(int size) 4 { 5 for (int i = 0; i < size; ++i) { 6 cout << "The number is " << i << endl; 7 } 8 } 9 l 文件名:函数名 l print.cpp:printNumber (gdb) l print.cpp:printNumber 1 #include "print.h" 2 3 void printNumber(int size) 4 { 5 for (int i = 0; i < size; ++i) { 6 cout << "The number is " << i << endl; 7 } 8 } 9

设置显示的行数

查看默认显示的行数 list 等于 listsize show list / show listsize (gdb) show list Number of source lines gdb will list by default is 10. (gdb) show listsize Number of source lines gdb will list by default is 10. 更改显示的函数 set list 13 / set listsize 14 (gdb) set list 13 (gdb) show list Number of source lines gdb will list by default is 13. (gdb) set listsize 14 (gdb) show listsize Number of source lines gdb will list by default is 14.

断点操作

设置断点 b等于break 在当前文件设置断点 b 行号 (gdb) b 8 Breakpoint 1 at 0x92e: file helloGDB.cpp, line 8. (gdb) break 10 Breakpoint 2 at 0x954: file helloGDB.cpp, line 10. b 函数名 (gdb) break main Note: breakpoint 1 also set at pc 0x92e. Breakpoint 3 at 0x92e: file helloGDB.cpp, line 8. 在非当前文件设置断点 b 文件名:行号 (gdb) b print.cpp:4 Breakpoint 4 at 0xaeb: file print.cpp, line 4. b 文件名:函数名 (gdb) b print.cpp:printNumber Note: breakpoint 4 also set at pc 0xaeb. Breakpoint 5 at 0xaeb: file print.cpp, line 5.

查看断点

i 等于 info i b (gdb) i b Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000000092e in main() at helloGDB.cpp:8 2 breakpoint keep y 0x0000000000000954 in main() at helloGDB.cpp:10 3 breakpoint keep y 0x000000000000092e in main() at helloGDB.cpp:8 4 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:4 5 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:5

删除断点

d 等于 del 、delete d 断点编号 (gdb) d 1 (gdb) d 2 (gdb) d 3 (gdb) i b Num Type Disp Enb Address What 4 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:4 5 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:5

设置断点无效

dis 等于 disable dis 断点编号 (gdb) dis 4 (gdb) i b Num Type Disp Enb Address What 4 breakpoint keep n 0x0000000000000aeb in printNumber(int) at print.cpp:4 5 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:5

设置无效断点生效

ena 等于enable (gdb) ena 4 (gdb) i b Num Type Disp Enb Address What 4 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:4 5 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:5

设置条件断点

b 行号 if 变量==某个值 (gdb) b print.cpp:5 if i == 6 Note: breakpoints 4 and 5 also set at pc 0xaeb. Breakpoint 6 at 0xaeb: file print.cpp, line 5. (gdb) i b Num Type Disp Enb Address What 4 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:4 5 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:5 6 breakpoint keep y 0x0000000000000aeb in printNumber(int) at print.cpp:5 stop only if i == 6

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpffxj.html