参考资料:GDB调试精粹及使用实例

# 调试实例

 #include <iostream>
#include <cstring>
using namespace std;
int f[][];
int ax,ay;
int bx,by;
int dx[]={,,-,};
int dy[]={,,,-};
int _count=;
int _min=;
int n,m,k; void dfs(int d,int t,int curx,int cury)
{
if(t>_min)
return ;
if(curx< || cury< || cury>n- || curx>m-)
return ;
if(f[cury][curx])
return ;
if(curx==by && cury==bx){
if(t<_min){
_count=;
_min=t;
}
else{
_count++;
}
return ;
}
f[cury][curx]=true;
if(d==-){
for(int i=;i<;i++){
bool bt = f[cury+dy[i]][curx+dx[i]];
if(!bt){
dfs(i,t+,curx+dx[i],cury+dy[i]);
if( !(bx==cury+dy[i] && by==curx+dx[i] ))
f[cury+dy[i]][curx+dx[i]]=false;
}
}
}
else{
for(int i=;i<;i++){
bool bt = f[cury+dy[i]][curx+dx[i]];
int tt=d^;
if(tt==i) continue;
if(!bt){
dfs(i,t+,curx+dx[i],cury+dy[i]);
if( !(bx==cury+dy[i] && by==curx+dx[i] ))
f[cury+dy[i]][curx+dx[i]]=false;
}
}
}
return ;
} int main()
{
while(cin>>n>>m>>k){
_count=;
_min=;
memset(f,,sizeof(f));
for(int i=;i<=k;i++){
int a,b;
cin>>a>>b;
f[a][b]=true;
}
cin>>ax>>ay;
cin>>bx>>by;
dfs(-,,ay,ax);
cout<<_min<<endl;
cout<<_count<<endl;
}
return ;
}

  这是一道“小鼠迷宫问题”的代码,是道搜索题,要求输出矩阵内a到b的最短路的长度以及最短路的条数。这道题的第一遍代码写完之后,运行后发现堆栈溢出了程序没有输出直接结束。用codeblocks自带的调试debug了一会,发现不是很好用,遂转入cygwin下编译原程序启动gdb调试。顺便也复习了一下gdb的命令。

  下面是我以此程序为例,学习gdb的笔记:

Administrator@PC- /cygdrive/d/bin/小鼠迷宫问题
$ g++ -o xs -ggdb main.cpp
-- 编译原程序,并产生调试信息,便于之后进行gdb调试。
•$ g++ -o ex1 -g  ex1.cpp
利用操作系统的native format生成调试信息。GDB 可以直接利用这个信息,其它调试器也可以使用这个调试信息。
•$ g++ -o ex1 -ggdb  ex1.cpp
为 GDB 生成专用的更为丰富的调试信息,但是,此时就不能用其他的调试器来进行调试了 (如 ddx)
(引自L老师课件)
Administrator@PC- /cygdrive/d/bin/小鼠迷宫问题
$ gdb xs

-- 启动gdb调试原程序。

-- 启动之后会刷出以下内容:

Administrator@PC- /cygdrive/d/bin/小鼠迷宫问题
$ gdb xs
GNU gdb (GDB) 7.6.50.20130728-cvs (cygwin-special)
Copyright (C) Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 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 "i686-pc-cygwin".
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 /cygdrive/d/bin/小鼠迷宫问题/xs...done.
(gdb)

-- 最后一行(gdb)后面会有一个光标“_”在闪,表示可以输入命令。

(gdb) l
f[cury+dy[i]][curx+dx[i]]=false;
}
}
}
return ;
} int main()
{
while(cin>>n>>m>>k){

-- list(可简写为 l):显示代码

(gdb) l
#include <iostream>
#include <cstring>
using namespace std;
int f[][];
int ax,ay;
int bx,by;
int dx[]={,,-,};
int dy[]={,,,-};
int _count=;
int _min=;

-- list 1:从第一行显示代码

(gdb) l
int n,m,k; void dfs(int d,int t,int curx,int cury)
{
if(curx< || cury< || cury>n- || curx>m-)
return ;
if(f[cury][curx])
return ;
if(curx==by && cury==bx){
if(t<_min){

-- 继续显示代码

=============== 插入一下 ================

这个时候我看命令行里代码有堆起来了,想清理一下,结果尝试性的输入命令“cls”和“clear”,发现这两个命令是行不通的。

(gdb) cls
Undefined command: "cls". Try "help".
(gdb) clear
No source file specified.

好吧,既然不行,我们继续。

======================================

(gdb) b dfs
Breakpoint at 0x401186: file main.cpp, line .

-- breakpoint dfs:在函数dfs入口处设置断点

-- breakpoint 1 表示这是第一个断点

(gdb) r
Starting program: /cygdrive/d/bin/小鼠迷宫问题/xs
[New Thread .0xdc0]
[New Thread .0x155c] Breakpoint , dfs (d=-, t=, curx=, cury=) at main.cpp:
if(curx< || cury< || cury>n- || curx>m-)

-- run:运行程序。

-- run 命令运行之后,光标会停下来闪烁,表示在等待输入数据。输入数据之后会直接运行到第一个断点处停止,并显示相关信息。

(gdb) c
Continuing. Breakpoint , dfs (d=, t=, curx=, cury=) at main.cpp:
if(curx< || cury< || cury>n- || curx>m-)

-- continue:继续运行。

========== 小问题 ============

(gdb) b dfs if curx== && cury==
Note: breakpoint also set at pc 0x401186.
Breakpoint at 0x401186: file main.cpp, line .
(gdb) c
Continuing. Breakpoint , dfs (d=, t=, curx=, cury=) at main.cpp:
if(curx< || cury< || cury>n- || curx>m-)

第一条gdb命令设置一个条件断点为第二个断点,第二条gdb命令为继续运行,为何继续运行后不是跳到刚设置的第二个断点处呢?

===========================

(gdb) p curx
$ =

-- print:显示一个变量的值

========== 小例子 ==========

(gdb) p curx
$ =
(gdb) p
$ =
(gdb) p
$ =
(gdb) p cury
$ =
(gdb) p
$ =

连续输入p会持续显示上一次观察的变量的值。

==========================

====== 小问题:这时候连续输入“continue”会发生什么呢? =======

(gdb) c
Continuing. Breakpoint , dfs (d=, t=, curx=, cury=) at main.cpp:
if(curx< || cury< || cury>n- || curx>m-)
(gdb) c
Continuing. Breakpoint , dfs (d=, t=, curx=, cury=) at main.cpp:
if(curx< || cury< || cury>n- || curx>m-)
(gdb) c
Continuing. Breakpoint , dfs (d=, t=, curx=, cury=) at main.cpp:
if(curx< || cury< || cury>n- || curx>m-)
(gdb) c
Continuing. Breakpoint , dfs (d=, t=, curx=, cury=) at main.cpp:
if(curx< || cury< || cury>n- || curx>m-)

会连续显示第一个断点处的变化。

===========================================

还有其它常用命令:

(gdb) s
if(f[cury][curx])
(gdb) s
if(curx==by && cury==bx){
(gdb) s
s20 if(t<_min){
(gdb) s
_count=;
(gdb) s
_min=t;
(gdb) s
return ;

-- step:单步执行,等效于step into(可进入函数)。

与之相对的

-- next(n):单步执行,等效于step over(只在当前函数中执行)。

-- finish(f):跳出当前函数,等效于step out。

(gdb) b dfs if curx== && cury==
Breakpoint at 0x401186: file main.cpp, line .

-- b dfs if curx==2 && cury==2:设置条件断点,程序执行到符合条件处停止。

(gdb) watch i
Hardware watchpoint : i
(gdb) s
if(!bt){
(gdb) s
for(int i=;i<;i++){
(gdb) s
Hardware watchpoint : i Old value =
New value =
0x00401443 in dfs (d=, t=, curx=, cury=) at main.cpp:

-- watch:设置一个监测点(数据断点),被检测变量在程序中出现时,显示其变化。

(gdb) i b
Num Type Disp Enb Address What
breakpoint keep y 0x00401186 in dfs(int, int, int, int)
at main.cpp:
stop only if curx== && cury==
breakpoint already hit time
breakpoint keep y 0x00401186 in dfs(int, int, int, int)
at main.cpp:
breakpoint keep y 0x004011bc in dfs(int, int, int, int)
at main.cpp:

-- info breakpoint:显示当前所有断点信息。

(gdb) d b
Ambiguous delete command "b 1": bookmark, breakpoints.
(gdb) d breakpoint
(gdb) i b
Num Type Disp Enb Address What
breakpoint keep y 0x00401186 in dfs(int, int, int, int)
at main.cpp:
breakpoint keep y 0x004011bc in dfs(int, int, int, int)
at main.cpp:

-- delete breakpoint 断点号:删除指定断点

(gdb) d
Delete all breakpoints? (y or n) y
(gdb) i b
No breakpoints or watchpoints.

-- delete:删除当前所有断点

Freecode : www.cnblogs.com/yym2013

GDB调试笔记的更多相关文章

  1. nginx模块_使用gdb调试nginx源码

    工欲善其事必先利其器,如何使用调试工具gdb一步步调试nginx是了解nginx的重要手段. ps:本文的目标人群是像我这样初接触Unix编程的同学,如果有什么地方错误请指正. 熟悉gdb的使用 这里 ...

  2. 用gdb调试程序笔记: 以段错误(Segmental fault)为例

    用gdb调试程序笔记: 以段错误(Segmental fault)为例[转] 1.背景介绍2.程序中常见的bug分类3.程序调试器(如gdb)有什么用4.段错误(Segmental fault)介绍5 ...

  3. GDB调试基础

    GDB调试基础 https://lesca.me/archives/gdb-basic-knowledge.html GDB笔记(二):条件断点.命令列表.监视点 https://lesca.me/a ...

  4. GDB调试命令小结

    1.启动调试 前置条件:编译生成执行码时带上 -g,如果使用Makefile,通过给CFLAGS指定-g选项,否则调试时没有符号信息.gdb program //最常用的用gdb启动程序,开始调试的方 ...

  5. GDB调试汇编堆栈过程分析

    GDB调试汇编堆栈过程分析 分析过程 这是我的C源文件:click here 使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb ...

  6. gdb调试器的使用

    想要使用gdb调试程序的话,首先需要gcc -g main.c -o test 然后运行gdb test对程序进行调试 l (小写的l,是list的首字母),用以列出程序 回车    是运行上一个命令 ...

  7. 20145212——GDB调试汇编堆栈过程分析

    GDB调试汇编堆栈过程分析 测试代码 #include <stdio.h> short val = 1; int vv = 2; int g(int xxx) { return xxx + ...

  8. gdb调试PHP扩展错误

    有时候,使用PHP的第三方扩展之后,可能会发生一些错误,这个时候,可能就需要更底层的方式追踪调试程序发生错误的地方和原因,熟悉linux下C编程的肯定不陌生gdb 首先,使用ulimit -c命令,查 ...

  9. gdb调试汇编堆栈过程的学习

    gdb调试汇编堆栈过程的学习 以下为C源文件 使用gcc - g code.c -o code -m32指令在64位的机器上产生32位汇编,然后使用gdb example指令进入gdb调试器: 进入之 ...

随机推荐

  1. RSA算法小记

    学习来源:http://www.cnblogs.com/vamei/p/3480994.html 小记: 一.数学基础: 欧拉Phi函数:Φ(n)=总数(从1到n-1中与n互质的整数) (1)欧拉定理 ...

  2. DataFrame转矩阵Np-Array

    DataFrame.as_matrix(columns=None)¶ Convert the frame to its Numpy-array representation.

  3. memcached安装和php-memcached扩展安装.update.2014-08-15

    服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.3.0 .下载官网:http://www.danga.com另外,Memcache用到了libevent这个库用于 ...

  4. java中不带package和带package的编译运行方式

    Java中不带package的程序和带package的程序编译的方式是不同的. 一.不带package的程序建立个HelloWorld.java的文件,放入C:\,内容如下:public class ...

  5. linux基本命令(3)-文件目录操作指令

    1.拷贝文件 目录:cp - r /usr/xu/ /usr/liu/ 文件:cp /home/itcast/Desktop/jdk-6u24-linux-i586.bin  /usr/java/ 2 ...

  6. 10 steps to becoming the developer everyone wants

    You thought it was all about programming skills. But you were wrong! Great code is fine, yet command ...

  7. [LA3026]Period

    [LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...

  8. [BZOJ1064][Noi2008]假面舞会

    [BZOJ1064][Noi2008]假面舞会 试题描述 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢 ...

  9. arch linux 新版安装(转)

    原文地址:http://blog.sina.com.cn/s/blog_69e5d8400101bqlj.html 正式安装: 以下,是安装的具体步骤,我将做尽可能详细的解说: 一.基本系统的安装 进 ...

  10. 在CentOS 6.4中编译安装gcc 4.8.1

    在CentOS 6.4中编译安装gcc 4.8.1 分类: C/C++ Linux/Unix2013-11-28 21:02 1877人阅读 评论(0) 收藏 举报 原文链接:http://www.c ...