GDB 1、超详细的GDB入门笔记,包含演示代码,快速入门
01、程序
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void printVector(vector<string>& v){
cout<<"size = "<<v.size()<<endl;
cout<<"print begin..."<<endl;
for (int i = 0; i < v.size(); i++){
cout<<v[i]<<" ";
}
cout<<endl<<"print end..."<<endl;
cout<<endl;
}
int main(){
vector<string> v(10, "mhzzj");
//修改指定下标的数据
v[6] = "MHZZJ";
//删除尾部数据(v[n-1])
v.pop_back();
//添加数据到尾部(v[n-1])
v.push_back("zzjmhz");
printVector(v);
//删除下标为v[0]的数据,头部
v.erase(v.begin());
//边迭代边删除
vector<string>::iterator it = v.begin();
while (it != v.end()) {
if (*it == "zzjmhz") it = v.erase(it);
else it++;
}
printVector(v);
cout<<"++++++++++++++++++++endl+++++++++++++++++++++++"<<endl;
}
02、GDB调试
生成调试程序
mhzzj@mhzzj-virtual-machine:~/codes/C++$ g++ gdbdemo.cpp -o demo -g
如果是c语言可以使用gcc gdbdemo.cpp -o demo -g
进入gdb调试
gdb demo
mhzzj@mhzzj-virtual-machine:~/codes/C++$ gdb demo
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 demo...done.
显示源码
list:现实源码,可以简写为:l
(gdb) l
2 #include<vector>
3 #include<string>
4 using namespace std;
5
6 void printVector(vector<string>& v){
7 cout<<"size = "<<v.size()<<endl;
8 cout<<"print begin..."<<endl;
9 for (int i = 0; i < v.size(); i++){
10 cout<<v[i]<<" ";
11 }
(gdb)
如果想查看第n行前后的代码,可以使用
l n
命令
设置断点
break设置断点,可以简写为:b
(gdb) b 29 #在第29行设置断点
Breakpoint 1 at 0x4011e5: file gdbdemo.cpp, line 29.
(gdb) b printVector
Breakpoint 2 at 0x401003: file gdbdemo.cpp, line 7.
查看断点
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004011e5 in main() at gdbdemo.cpp:29
2 breakpoint keep y 0x0000000000401003 in printVector(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&) at gdbdemo.cpp:7
删除断点
(gdb) d 2 #删除第2个断点
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004011e5 in main() at gdbdemo.cpp:29
启动程序
启动程序run,可以简写为:r
(gdb) r
Starting program: /home/mhzzj/codes/C++/demo
Breakpoint 1, main () at gdbdemo.cpp:29
29 printVector(v);
使用run命令可以直接运行到断点的位置,如果想从mian函数开始执行,可以使用start指令
查看变量
查看变量指令为print,可以简写为p
Breakpoint 1, main () at gdbdemo.cpp:29
29 printVector(v);
(gdb) p v
$1 = std::vector of length 10, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj", "zzjmhz"}
监控某个变量
(gdb) display v
1: v = std::vector of length 8, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj"}
(gdb) n
size = 8
8 cout<<"print begin..."<<endl;
1: v = std::vector of length 8, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj"}
(gdb)
可以使用undispla命令取消监控
执行下一行
执行下一行next,可简写为:n
Breakpoint 1, main () at gdbdemo.cpp:29
29 printVector(v);
(gdb) display v
1: v = std::vector of length 10, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj", "zzjmhz"}
(gdb) n
size = 10
print begin...
mhzzj mhzzj mhzzj mhzzj mhzzj mhzzj MHZZJ mhzzj mhzzj zzjmhz
print end...
32 v.erase(v.begin());
1: v = std::vector of length 10, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj", "zzjmhz"}
(gdb) n
34 vector<string>::iterator it = v.begin();
1: v = std::vector of length 9, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj", "zzjmhz"}
(gdb)
执行到下一个断点
continue 继续运行程序,停在下一个断点的位置,可简写为:c
(gdb) b 39 #在第39行设置断点
Breakpoint 3 at 0x40128f: file gdbdemo.cpp, line 39.
(gdb) c #执行到下一个断点
Continuing.
Breakpoint 3, main () at gdbdemo.cpp:39
39 printVector(v);
进入函数内部
如果执行到某个函数,想进入函数内部,可以使用step指令,简写为s
如果在改行为函数调用,则进入函数内部,否则为执行下一行,效果如图n指令
Breakpoint 3, main () at gdbdemo.cpp:39
39 printVector(v);
(gdb) s
printVector (v=std::vector of length 8, capacity 10 = {...}) at gdbdemo.cpp:7
7 cout<<"size = "<<v.size()<<endl;
查看栈帧
(gdb) bt
#0 printVector (v=std::vector of length 8, capacity 10 = {...}) at gdbdemo.cpp:7
#1 0x000000000040129b in main () at gdbdemo.cpp:39
切换栈帧
(gdb) frame 1
#1 0x000000000040129b in main () at gdbdemo.cpp:39
39 printVector(v);
(gdb)
退出进入的函数
退出进入的函数:finish
(gdb) frame 1
#1 0x000000000040129b in main () at gdbdemo.cpp:39
39 printVector(v);
(gdb) frame 0
#0 printVector (v=std::vector of length 8, capacity 10 = {...}) at gdbdemo.cpp:7
7 cout<<"size = "<<v.size()<<endl;
(gdb) finish
Run till exit from #0 printVector (v=std::vector of length 8, capacity 10 = {...}) at gdbdemo.cpp:7
size = 8
print begin...
mhzzj mhzzj mhzzj mhzzj mhzzj MHZZJ mhzzj mhzzj
print end...
main () at gdbdemo.cpp:40
40 cout<<"++++++++++++++++++++endl+++++++++++++++++++++++"<<endl;
1: v = std::vector of length 8, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj"}