gdb调试
来源:互联网 发布:怎么使用淘宝客 编辑:程序博客网 时间:2024/06/06 04:51
gdb是一个用来调试C和C++程序的功能强大的调试器,它可以在程序运行时,观查程序内部结构和内存的使用情况。
主要功能
(1) 监视程序中的变量。
(2) 设置断点,让程序在指定的代码行暂停。
(3) 单步执行。
(4) 分析崩溃程序产生的核文件。
gdb调试小例
使用gdb调试一个非常简单的c++程序,使用gdb的几个常用的主要功能监视变量、设置断点、单步执行。
(1)源文件:helloworld.cpp
#include<iostream>using namespace std;int main(){ int counter=0; int i=0; for(;counter<10;++counter) { cin>>i; cout<<"i is: "<<i<<endl; } return 0;}
(2)编译源文件
使用 “-ggdb3”选项编译,编译后“gdb”可以判断编译后的代码和源程序代码的关系。否则,gdb无法判断源程序中的哪一行代码被执行。
g++ -ggdb3 -o helloworld helloworld.cpp
(3)调试程序
gdb+程序名
adver@adver-VirtualBox:~/cproject$ gdb helloworldGNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1Copyright (C) 2014 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 helloworld...done.(gdb)
设置断点:在main函数处,设置断点
(gdb) break mainBreakpoint 1 at 0x400936: file helloworld.cpp, line 6.
执行程序:run。程序运行到断点处(第6行)停止。
(gdb) runStarting program: /home/adver/cproject/helloworld Breakpoint 1, main () at helloworld.cpp:66 int counter=0;
单步执行:step或s
s7 int i=0;(gdb) s8 for(;counter<10;++counter)
监视变量:display。使用该命令观测变量后,程序每次暂停时(断点或单步执行),会显示变量的值。
(gdb)display counter1: counter = 0(gdb) display i2: i = 0(gdb) sBreakpoint 2, main () at helloworld.cpp:1010 cin>>i;2: i = 01: counter = 0(gdb) s1011 cout<<"i is: "<<i<<endl;2: i = 101: counter = 0
注意:上面最后一个“s”输入后,会执行”cin>>i”,因此输入10给变量i。
(gdb) si is: 108 for(;counter<10;++counter)2: i = 101: counter = 0(gdb) s10 cin>>i;2: i = 101: counter = 1(gdb) s211 cout<<"i is: "<<i<<endl;2: i = 21: counter = 1(gdb) si is: 28 for(;counter<10;++counter)2: i = 21: counter = 1
next命令:它的功能和step类似,但它不会进入到程序里面(如:调试执行自己写的某函数时,step会进入该函数里,next则不会),本例没有这种情况,所以next和step效果看起来一样。
(gdb) n10 cin>>i;2: i = 21: counter = 2(gdb) n2011 cout<<"i is: "<<i<<endl;2: i = 201: counter = 2(gdb) nexti is: 208 for(;counter<10;++counter)2: i = 201: counter = 2
退出调试
(gdb) qA debugging session is active. Inferior 1 [process 3451] will be killed.Quit anyway? (y or n) yadver@adver-VirtualBox:~/cproject$
0 0
- GDB调试
- gdb 调试
- gdb调试
- gdb 调试
- GDB调试
- GDB调试
- GDB调试
- GDB调试
- GDB调试
- GDB调试
- gdb 调试
- GDB 调试
- gdb 调试
- GDB调试
- gdb调试
- gdb调试
- GDB 调试
- GDB调试
- CodeForces
- TI-DM8127:相关英文缩写总结
- opencv关于直方图的归一化,均衡化
- JDK6 新版本特性
- 蓝桥杯--3--凑算式
- gdb调试
- [我可能是个SB啊] UOJ #290 【ZJOI2017】仙人掌
- java中scan.next()与scan.nextline()函数的使用与区别
- Android 7.0新特性
- Activex控件不能例示 ActiveX 控件“Microsoft Chart Control 6.0(sp4)(OLEDB)"不能例示的解决办法
- collection中的方法和接口
- html5(1)Hello World
- 哒哒哒哒
- 关于Java中HashMap的常见应用