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. Anaconda虚拟环境

    创建虚拟环境:conda create -n env_name packages 例:创建名为env1的虚拟环境,并在其中安装numpy,conda create -n env1 numpy. 指定特 ...

  2. vue的风格指南(必要的)

    1.v-if与v-for不要放在同一个元素上 当 v-if 与 v-for 一起使用时,v-for 具有比 v-if 更高的优先级.永远不要把 v-if 和 v-for 同时用在同一个元素上. 一般我 ...

  3. composer 实现自动加载原理

    简介 一般在框架中都会用到composer工具,用它来管理依赖.其中composer有类的自动加载机制,可以加载composer下载的库中的所有的类文件.那么composer的自动加载机制是怎么实现的 ...

  4. NodeJS NPM 镜像使用方法

    每次npm的时候,走国外的镜像,非常的慢,可以配置一下 通过改变默认npm镜像代理服务,以下三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候不用重新配置. 通过config命令 ...

  5. sql server xp_cmdshell 过程中报错原因

    1.net use 连接目标服务器是报错: 发生系统错误 53.找不到网络路径 可能原因是:主机装防护软件 比如 360 金山毒霸等阻止了cmd.exe程序. 将cmd.exe权限改成管理员(属性&g ...

  6. PuppeteerSharp+AngleSharp的爬虫实战之汽车之家数据抓取

    参考了DotNetSpider示例, 感觉DotNetSpider太重了,它是一个比较完整的爬虫框架. 对比了以下各种无头浏览器,最终采用PuppeteerSharp+AngleSharp写一个爬虫示 ...

  7. 基于Java的ArrayList和LinkedList的实现与总结

    一.定义MyList接口,包含列表常见方法: import java.util.Iterator; /** * 线性表(列表)的接口定义 */ public interface MyList<T ...

  8. Kubernetes(k8s)1.12.2集群搭建

    本博客搭建k8s集群1.12.2版本 1. 准备2台以上最低2核4G的服务器 2. 关闭机器的防火墙 12 systemctl disable firewalldsystemctl stop fire ...

  9. [Swift]LeetCode435. 无重叠区间 | Non-overlapping Intervals

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  10. [Swift]LeetCode732. 我的日程安排表 III | My Calendar III

    Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...