首先安装gdb工具
yum install gdb
情景一 正常
#include <iostream>
using namespace std;
void show(const char*name1,const char*name2,const char*messages)
{
cout<<name1<<"开始表白"<<endl;
cout<<name1<<":"<<messages<<endl;
}
int main(int argc, char *argv[], char *envp[])
{
if(argc!=4)
{
cout<<"表白神奇程序得使用方法:。/test 追求者名字 被追求者姓名 表白内容"<<endl;
return 0;
}
cout<<"表白前得准备工作一"<<endl;
cout<<"表白前得准备工作二"<<endl;
cout<<"表白前得准备工作三"<<endl;
show(argv[1],argv[2],argv[3]);
cout<<"表白成功"<<endl;
// cout << "一共有" << argc << "个参数" << endl;
// for (int i = 0; i < argc; i++)
// {
// cout << "第" << i << "个参数" << argv[i] << endl;
// }
// for (int i = 0; envp[i]!=0; i++)
// {
// cout << envp[i]<<endl;
// }
return 0;
}
makefile 文件
test3:test3.cpp
g++ -o $@ $^ -std=c++11 -g
.PHONY:clean
clean:
rm -rf test3
使用make 编译 ,注意这里-g就是代表加了gdb
[zk@VM-24-17-centos test1]$ make
g++ -o test1 test1.cpp -std=c++11 -g
[zk@VM-24-17-centos test1]$ ./test1 1 2 love
表白前得准备工作一
表白前得准备工作二
表白前得准备工作三
1开始表白
1:love
表白成功
我们发现运行这个需要我们传入三个参数
set args :gdb传入参数
r(run) ,运行
b(break) 打断点 b20 就是在20行打断点
n(next) 执行下一行,就是不进入函数体
s(step) 执行下一行,但是会进入函数体
c(continue) 执行到下一个断点出
p(print) 给一个变量赋值,打印值
set var 也是给一个变量赋值,不打印值
第一步,执行gdb
[zk@VM-24-17-centos test1]$ gdb test1
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/zk/project/test1/test1...done.
(gdb)
第二步:传入我们的参数
set args 1 2 love
第三步:打断点
(gdb) b 17
Breakpoint 1 at 0x400897: file test1.cpp, line 17.
第4步:运行
(gdb) r
Starting program: /home/zk/project/test1/test1 1 2 love
Breakpoint 1, main (argc=4, argv=0x7fffffffe098, envp=0x7fffffffe0c0) at test1.cpp:17
17 cout<<"表白前得准备工作一"<<endl;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64 libstdc++-4.8.5-44.el7.x86_64
第5步:执行下一行
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64 libstdc++-4.8.5-44.el7.x86_64
(gdb) n
表白前得准备工作一
18 cout<<"表白前得准备工作二"<<endl;
第6步:打下一个断点
(gdb) b 21
Breakpoint 2 at 0x400917: file test1.cpp, line 21.
第7步:执行下一到下一个断点
(gdb) c
Continuing.
表白前得准备工作二
表白前得准备工作三
1开始表白
1:love
Breakpoint 2, main (argc=4, argv=0x7fffffffe098, envp=0x7fffffffe0c0) at test1.cpp:21
21 cout<<"表白成功"<<endl;
接着就是往下执行。
情景二
test2.cpp代码
#include<iostream>
using namespace std;
int main()
{
for(int i =0;i<10;i++)
{
cout<<i<<"我爱你"<<endl;
}
return 0;
}
makefile 文件
test2:test2.cpp
g++ -o $@ $^ -std=c++11 -g
.PHONY:clean
clean:
rm -rf test2
同样的执行代码
[zk@VM-24-17-centos test1]$ ./test2
0我爱你
1我爱你
2我爱你
3我爱你
4我爱你
5我爱你
6我爱你
7我爱你
8我爱你
9我爱你
gdb调试
在6行设置断点
(gdb) b 6
Breakpoint 1 at 0x4007e5: file test2.cpp, line 6.
运行
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64 libstdc++-4.8.5-44.el7.x86_64
(gdb) r
Starting program: /home/zk/project/test1/test2
Breakpoint 1, main () at test2.cpp:6
6 for(int i =0;i<10;i++)
(gdb) n
8 cout<<i<<"我爱你"<<endl;
(gdb) n
0我爱你
正常情况下要执行10次
我们可以用set val 把值变了
(gdb) set var i=5
(gdb) n
8 cout<<i<<"我爱你"<<endl;
(gdb) n
6我爱你
6 for(int i =0;i<10;i++)
接着就是print 改变值
(gdb) print i =8
$1 = 8
(gdb) n
8 cout<<i<<"我爱你"<<endl;
(gdb) n
9我爱你
情景三
有时候我们遇到段错误了,很多时候是内存泄漏
我们怎么知道在哪呢?
我们先要改一个东西
输入命令
[zk@VM-24-17-centos test1]$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7908
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 100002
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7908
virtual memory (kbytes, -v) unlimited
file locks
发现我们的core文件的不能申城 ,修改一下。
ulimit -c unlimited
修改之后
[zk@VM-24-17-centos test1]$ ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7908
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 100002
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7908
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
代码:test3.cpp
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void b (const int bh, const string name)
{
char* ptr = nullptr;
*ptr =3;
strcpy(ptr,name.c_str());
}
void a (const int no, const string name)
{
b(3,"冰冰");
}
int main()
{
a(8,"西施");
return 0;
}
编译运行
[zk@VM-24-17-centos test1]$ ./test3
Segmentation fault (core dumped)
[zk@VM-24-17-centos test1]$ ls
core.10645 makefile test1 test1.cpp test2 test2.cpp test3 test3.cpp
会遇到一个段错误,并且生成了一个core文件
我们用gdb调试
[zk@VM-24-17-centos test1]$ gdb test3 core.10645
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/zk/project/test1/test3...done.
[New LWP 10645]
Core was generated by `./test3'.
Program terminated with signal 11, Segmentation fault.
#0 0x0000000000400928 in b (bh=3, name=...) at test3.cpp:9
9 *ptr =3;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64 libstdc++-4.8.5-44.el7.x86_64
能帮我们找到错误的位置。
情景四 调试正在运行得代码
#include<iostream>
#include<string>
#include<cstring>
#include<unistd.h>
using namespace std;
void b (const int bh, const string name)
{
for(int i =0;i<1000000;i++)
{
sleep(1);
cout<<"i="<<i<<endl;
}
}
void a (const int no, const string name)
{
b(3,"冰冰");
}
int main()
{
a(8,"西施");
return 0;
}
调试方法
ps -ef|grep test4
zk 19859 18123 0 09:17 pts/4 00:00:00 ./test4
zk 20399 19952 0 09:18 pts/5 00:00:00 grep --color=auto test4
找到pid,这样就可以i调试了
gdb test4 -p 19859