参考资料: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. js获取页面url的方法

    我们可以用javascript获得其中的各个部分 1, window.location.href 整个URl字符串(在浏览器中就是完整的地址栏) 本例返回值: http://ifisker.com/b ...

  2. win32控制台消息机制

    源码: #include<windows.h>HANDLE hInput; /* 获取标准输入设备句柄 */INPUT_RECORD inRec;/* 返回数据记录 */DWORD num ...

  3. Java操作Excel(读、写、搜索关键字、插入图片)

    import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workb ...

  4. Visual Studio Online Integrations

                                                                                                         ...

  5. WPF控件模拟双击事件

    Action a = () => { i += ; ) { Interval = }; timer.Elapsed += (sender, e) => { timer.Enabled = ...

  6. ExtJS学习之路第三步:理解引擎之下,ExtJS4中的类

    写写就发现,有些代码不查查源头,不明白是怎么回事?搜到这篇文章觉得还是收益匪浅,更容易读懂代码. Classes in Ext JS 4: Under the hood Countdown to Ex ...

  7. Help Me with the Game(imitate)

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3630   Accepted:  ...

  8. php面试题之二——Javascript(基础部分)

    二.JavaScript部分 1. JS 表单弹出对话框函数是?获得输入焦点函数是? 弹出对话框函数:alert(), prompt(), confirm() 获得输入焦点函数:focus() 2. ...

  9. Unity3D使用小技巧

    原地址:http://unity3d.9tech.cn/news/2014/0411/40178.html 1.Crtl+f摄像机自动适配场景. 2.可以用一个立方体作为底盘. 3.人物角色可以直接引 ...

  10. Android中多个调用Activity的问题

    <application android:allowBackup="true" android:icon="@drawable/ic_launcher" ...