GDB调试笔记
参考资料: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
- 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调试笔记的更多相关文章
- nginx模块_使用gdb调试nginx源码
工欲善其事必先利其器,如何使用调试工具gdb一步步调试nginx是了解nginx的重要手段. ps:本文的目标人群是像我这样初接触Unix编程的同学,如果有什么地方错误请指正. 熟悉gdb的使用 这里 ...
- 用gdb调试程序笔记: 以段错误(Segmental fault)为例
用gdb调试程序笔记: 以段错误(Segmental fault)为例[转] 1.背景介绍2.程序中常见的bug分类3.程序调试器(如gdb)有什么用4.段错误(Segmental fault)介绍5 ...
- GDB调试基础
GDB调试基础 https://lesca.me/archives/gdb-basic-knowledge.html GDB笔记(二):条件断点.命令列表.监视点 https://lesca.me/a ...
- GDB调试命令小结
1.启动调试 前置条件:编译生成执行码时带上 -g,如果使用Makefile,通过给CFLAGS指定-g选项,否则调试时没有符号信息.gdb program //最常用的用gdb启动程序,开始调试的方 ...
- GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 分析过程 这是我的C源文件:click here 使用gcc - g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb ...
- gdb调试器的使用
想要使用gdb调试程序的话,首先需要gcc -g main.c -o test 然后运行gdb test对程序进行调试 l (小写的l,是list的首字母),用以列出程序 回车 是运行上一个命令 ...
- 20145212——GDB调试汇编堆栈过程分析
GDB调试汇编堆栈过程分析 测试代码 #include <stdio.h> short val = 1; int vv = 2; int g(int xxx) { return xxx + ...
- gdb调试PHP扩展错误
有时候,使用PHP的第三方扩展之后,可能会发生一些错误,这个时候,可能就需要更底层的方式追踪调试程序发生错误的地方和原因,熟悉linux下C编程的肯定不陌生gdb 首先,使用ulimit -c命令,查 ...
- gdb调试汇编堆栈过程的学习
gdb调试汇编堆栈过程的学习 以下为C源文件 使用gcc - g code.c -o code -m32指令在64位的机器上产生32位汇编,然后使用gdb example指令进入gdb调试器: 进入之 ...
随机推荐
- 大数据分析与机器学习领域Python兵器谱
http://www.thebigdata.cn/JieJueFangAn/13317.html 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开发语言是C/ ...
- hdu 2049 不容易系列之(4)——考新郎
在本博AC代码中,求CNM用的是Anm/amm没用阶乘的形式,两者皆可 #include <stdio.h> int main(void) { long long a,b,larr[21] ...
- 利用dedecms autoindex让文章列表加上序列号
有些时候我们在制作模板的需要在文章标题前面加上序列号,可以通过织梦自带的autoindex属性来实现,实现方法很简单,只需要在序号递增的地方加上 这段代码就行,[field:global runphp ...
- 调用gluNurbsCurve绘制圆弧
<OpenGL编程指南>第12章第3小结专门介绍调用GLU绘制NURBS曲线或曲面,很可惜的是并未给出绘制圆弧的例子.网上可以找到很多绘制整个园的例子,却没圆弧例子,自己瞎折腾了2个礼拜, ...
- 用LoadRunner实现接口测试
接口测试的两种方法 其实无论用那种测试方法,接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过 ...
- java笔记--超级类Object多线程的应用+哲学家进餐算法内部类与多线程结合
关于Object类中的线程方法: Object类是所有Java类的 父类,在该类中定义了三个与线程操作有关的方法,使得所有的Java类在创建之后就支持多线程 这三个方法是:notify(),notif ...
- Ubuntu 16.04 下安装Firefox的Flash插件
在ubuntu系统环境下面打开优酷视频,发现无法播放视频.Adobe Flash Player 是一款轻量级浏览器插件,具有丰富的 Internet 应用运行时间,提供持续的迷人用户体验.绝妙的音频/ ...
- B,B+,B-,B*树
B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...
- 黑客讲述渗透Hacking Team全过程(详细解说)
近期,黑客Phineas Fisher在pastebin.com上讲述了入侵Hacking Team的过程,以下为其讲述的原文情况,文中附带有相关文档.工具及网站的链接,请在安全环境下进行打开,并合理 ...
- Delphi10 安装Graphics32
一.下载Graphics安装包 官网:www.graphics32.org 下载地址:http://sourceforge.net/projects/graphics32/files/graphics ...