在GDB中完成类似vs的set next statement的行为
在GDB中完成类似vs的set next statement的行为
在visual studio中,可以同鼠标右键在源代码的指定位置,选择set next statement来快速控制程序到指定位置开始执行。这可以让那些粗心大意的程序员无需重复前面的步骤,查看之前程序是如何运行的。
在GDB中,这一行为由两个指令完成。
1、break point
2、jump point
注意
1、如果将指针设置为当前执行函数以外的位置,会造成各种未知的后果(有兴趣可以自行尝试)
2、如果在代码运行逻辑上,执行了未初始化的代码,或者执行了不能重复执行代码,也会造成未知的后果
例子
源码
#include <iostream>
using namespace std;
int main()
{
cout << "line1" << endl;
cout << "line2" << endl;
cout << "line3" << endl;
cout << "line4" << endl;
cout << "line5" << endl;
cout << "line6" << endl;
cout << "line7" << endl;
return 0;
}
编译
g++ -g gdbjump.cpp -o gdbjump
调试
gdb gdbjump
调试操作
(gdb) b main
Breakpoint 1 at 0x400848: file gdbjump.cpp, line 7.
(gdb) r
Starting program: ~/temp/gdbjump
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000
Breakpoint 1, main () at gdbjump.cpp:7
7 cout << "line1" << endl;
(gdb) b 9
Breakpoint 2 at 0x400880: file gdbjump.cpp, line 9.
(gdb) jump 9
Continuing at 0x400880.
Breakpoint 2, main () at gdbjump.cpp:9
9 cout << "line3" << endl;
(gdb) n
line3
10 cout << "line4" << endl;
(gdb) n
line4
11 cout << "line5" << endl;
(gdb) n
line5
12 cout << "line6" << endl;
(gdb) n
line6
13 cout << "line7" << endl;
(gdb) n
line7
14 return 0;
(gdb) n
15 }
(gdb) c
Continuing.
Program exited normally.
说明:line1和line2将不被打印出来