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. vue将指定区域的表格数据或element-ui中el-table的数据单笔或多笔批量导出excel

    公司在后台管理系统开发中用到了 vue+element-ui 组合的框架,但随着需求的越来越复杂,前端的工作难度也呈几何倍数递增,工作量随之增大.这不,在项目中增加一个将列表数据导出为excel的需求 ...

  2. [LeetCode] Flipping an Image 翻转图像

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  3. webpack学习最基本的使用方式(一)

    网页中引入的静态资源多了以后会有什么问题.? 1.网页加载速度慢,因为我们要发起很多的二次请求 2.要处理错综复杂的依赖关系 如何解决上面的问题 1.合并,压缩图片,使用精灵图 2.可以使用之前学过的 ...

  4. 对scanf和printf的研究!!

    在做项目的时候,突然很纠结要不要清理.所以赶紧写一篇博客记一下!! 1. scanf函数 在代码中,如果碰到了两个挨着输入的情况,就会出现问题!! 输入一个字符 r 就会出现一下情况!! 第2句sca ...

  5. android利用ContentResolver访问者获取手机联系人信息

    转载自:http://www.jb51.net/article/106379.htm 首先需要在AndroidManifest.xml文件中添加权限: <uses-permission andr ...

  6. C语言面试题分类->指针

    有关指针的经典面试题 C语言为何如此长寿并实用?C++为什么有那么多精彩?指针可以说是C/C++中的灵魂所在,虽然早期中pascal也有指针,但是和C/C++比起来不是一个级别的.今天为大家深入浅出的 ...

  7. 企业IT管理员IE11升级指南【11】—— 通过SCCM 2012和WSUS部署Internet Explorer 11

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  8. 史上最全java面试题

    基本概念 操作系统中 heap 和 stack 的区别 什么是基于注解的切面实现 什么是 对象/关系 映射集成模块 什么是 Java 的反射机制 什么是 ACID BS与CS的联系与区别 Cookie ...

  9. emWin智能家居主界面设计,含uCOS-III和FreeRTOS两个版本

    第6期:智能家居主界面设计配套例子:V6-910_STemWin提高篇实验_智能家居主界面设计(uCOS-III)V6-911_STemWin提高篇实验_智能家居主界面设计(FreeRTOS) 例程下 ...

  10. 深入理解Spring Redis的使用 (五)、常见问题汇总

    目前我所知道的Redistemplate里面,我没有使用到的就是管道.这个可以进行批量的读写.类似于jdbc的batch.还有就是Redis的集群部署.但是由于我业务里没有这种需求,所以没有使用无法给 ...