1.此文档演示如何使用gdb调试c语言代码。

代码如下:

#include <stdio.h>
/*函数声明*/
void digui(int n); int main()
{
int n=;
digui(n);
return ;
} void digui(int n)
{
printf("level1-value of %d\n",n);
if(n>){
digui(n-);
}
printf("level2-value of %d\n",n);
}

2.编译debug模式下的程序,编译方式如下:

[zsd@TOMCAT ~]$ gcc -g test03.c -o test03debug

3.进入gdb的debug模式,如下:

[zsd@TOMCAT ~]$ gdb test03debug
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-.el6_4.)
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 "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/zsd/test03debug...done.
(gdb)

4.gdb模式下,list命令,查看源代码:

(gdb) list
#include <stdio.h>
/*函数声明*/
void digui(int n); int main()
{
int n=;
digui(n);
return ;
}
(gdb) void digui(int n)
{
printf("level1-value of %d\n",n);
if(n>){
digui(n-);
}
printf("level2-value of %d\n",n);
}

5.list的相关命令,如下:

(gdb) set listsize    //设置显示行数
(gdb) show listsize; //查看显示行数
Number of source lines gdb will list by default is .
(gdb) list , //显示1~20行的源代码
#include <stdio.h>
/*函数声明*/
void digui(int n); int main()
{
int n=;
digui(n);
return ;
} void digui(int n)
{
printf("level1-value of %d\n",n);
if(n>){
digui(n-);
}
printf("level2-value of %d\n",n);
}

6.设置断点。

个人思路:由于希望研究递归函数的过程,所以对目前程序的16行和18行,设置断点。操作如下:

(gdb) break     //设置16行的断点
Breakpoint at 0x40050c: file test03.c, line .
(gdb) break //设置18行的断点
Breakpoint at 0x400519: file test03.c, line .
(gdb) info breakpoints //查看断点信息
Num Type Disp Enb Address What
breakpoint keep y 0x000000000040050c in digui at test03.c:
breakpoint keep y 0x0000000000400519 in digui at test03.c:18 删除断点的命令:(这里不执行,只是告知断点的方式)
(gdb) clear 16
(gdb) clear

7.开始调试程序

(gdb) run        //开始执行调试程序
Starting program: /home/zsd/test03debug
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-); //停止到设置在第一个断点,程序在第16行暂停。
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64
(gdb) contiune //continue命令,是在碰到断点的情况下,停止
Undefined command: "contiune". Try "help".
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-); //第二次碰到断点,程序停止,依次递推
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
digui(n-);
(gdb) continue
Continuing.
level1-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Breakpoint , digui (n=) at test03.c:
printf("level2-value of %d\n",n);
(gdb) continue
Continuing.
level2-value of Program exited normally.

run,开始运行程序;

continue,程序暂停时继续运行程序的命令;

print 变量名或表达式,打印该变量或者该表达式的值。whatis 变量名或者表达式,可以显示该变量或表达式的数据类型。

print  变量=值,这种形式还可以给对应的变量赋值;类似的还有set variable 变量=值。作用和用print赋值相同。

next,继续执行下一条语句;还有一条命令step,与之类似,不同的是,当下一条语句遇到函数调用的时候,next不会跟踪进入函数,而是继续执行下面的语句,而step命令则会跟踪进入函数内部。

c语言之gdb调试。的更多相关文章

  1. 使用GDB调试Go语言

    用Go语言已经有一段时间了,总结一下如何用GDB来调试它! ps:网上有很多文章都有描述,但是都不是很全面,这里将那些方法汇总一下 GDB简介  GDB是GNU开源组织发布的⼀一个强⼤大的UNIX下的 ...

  2. 【嵌入式开发】C语言 命令行参数 函数指针 gdb调试

    . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21551397 | http://www.hanshul ...

  3. C语言 命令行参数 函数指针 gdb调试

    . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21551397 | http://www.hanshul ...

  4. linux下的C语言开发(gdb调试)

    原文: http://blog.csdn.net/feixiaoxing/article/details/7199643 用gdb调试多进程的程序会遇到困难,gdb只能跟踪一个进程(默认是跟踪父进程) ...

  5. 20145223《信息安全系统设计基础》 GDB调试汇编堆栈过程分析

    20145223<信息安全系统设计基础> GDB调试汇编堆栈过程分析 分析的c语言源码 生成汇编代码--命令:gcc -g example.c -o example -m32 进入gdb调 ...

  6. GDB调试汇编堆栈

    GDB调试汇编堆栈 分析过程 C语言源代码 int g(int x) { return x+6; } int f(int x) { return g(x+1); } int main(void) { ...

  7. GDB调试汇编分析

    GDB调试汇编分析 代码 本次实践我参照了许多先做了的同学的博客,有卢肖明,高其,张梓靖同学.代码借用的是卢肖明同学的代码进行调试运行. GCC编译 使用gcc -g gdbtest.c -o gdb ...

  8. Linux下GDB调试

    GDB 是一个强大的命令行调试工具.大家知道命令行的强大就是在于,其可以形成执行 序列,形成脚本.UNIX 下的软件全是命令行的,这给程序开发提供了极大的便利,命令行 软件的优势在于, 他们可以非常容 ...

  9. 20145311利用gdb调试汇编代码

    利用GDB调试汇编代码 首先编写c语言原代码,我使用的是同学分析过的代码 #include<stdio.h>short addend1 = 1;static int addend2 = 2 ...

随机推荐

  1. docker之使用System.Drawing生成图片缺少Gdiplus.dll错误

    1.在docker 上找到并运行需要System.Drawing的镜像 然后退出执行另一语句 docker run -it container01 进入镜像以方便安装gdiplus docker ex ...

  2. vue图片上传到七牛云

    代码: <template> <div class="upload-info"> <div> <el-upload class=" ...

  3. VIM常用快捷键(转载)

    移动光标 h,j,k,l 上,下,左,右 ctrl-e 移动页面 ctrl-f 上翻一页 ctrl-b 下翻一页 ctrl-u 上翻半页 ctrl-d 下翻半页 w 跳到下一个字首,按标点或单词分割 ...

  4. swust oj 1014

    交换排序算法的设计与实现——冒泡排序 1000(ms) 10000(kb) 2640 / 7503 编程实现冒泡排序,按照非递减排序,测试数据为整数. 输入 第一行是待排序数据元素的个数: 第二行是待 ...

  5. CentOS7设置SVN自启动,提交报错,无权限.手动kill掉后重启,成功.

    参考文档:http://tieba.baidu.com/p/5174054662 最近想尝试在CentOS7上搭建SVN服务.遇到的问题大致如题,我这边再详细描述一下. 虚拟机:VMware® Wor ...

  6. Portainer介绍

    Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台操作.Swarm集群和服 ...

  7. 实战深度学习OpenCV(三):视频实时canny边缘检测

    #include <stdio.h> #include"opencv2/opencv.hpp" using namespace cv; int main() { Vid ...

  8. MyEclipse设置Console输出到文件

    Java程序默认输出为Console,如果要想将Console输出结果保存到文件中,则需要做如下配置: 在JAVA程序上右键--> Run As --> Run Configuration ...

  9. Hadoop伪分布式配置

    一步一步来: 安装VMWARE简单,安装CentOS也简单 但是,碰到了一个问题:安装的虚拟机没有图形化界面 最后,我选择了CentOS-7-x86_64-DVD-1503-01.iso镜像 配置用户 ...

  10. [Swift]LeetCode169. 求众数 | Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...