首先用一个简单的打印字符的程序来做下示例

#include<stdio.h>
#include<string.h>
void main()
{
    int i=0;
    char a[4]="abc";
    for(i=0;i<strlen(a);i++)
    {
        printf("%c",a[i]);
    }
}

要想用gdb调试文件,有两种方法 1 gcc生成可执行的文件, gdb 直接调试文件2 直接进入gdb 然后输入file 可执行文件

方法一:

root@zhf-linux:/home/zhf/zhf/c_prj# gcc -g -o test1 test1.c

root@zhf-linux:/home/zhf/zhf/c_prj# gdb test1

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1

Copyright (C) 2016 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 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-linux-gnu".

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 test1...(no debugging symbols found)...done.

(gdb) run

Starting program: /home/zhf/zhf/c_prj/test1

abc[Inferior 1 (process 3802) exited normally]

方法二:

root@zhf-linux:/home/zhf/zhf/c_prj#
gdb

GNU
gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1

Copyright
(C) 2016 Free Software Foundation, Inc.

License
GPLv3+: GNU GPL version 3 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-linux-gnu".

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".

(gdb)
file test1

Reading
symbols from test1...(no debugging symbols found)...done.

(gdb)
run

Starting
program: /home/zhf/zhf/c_prj/test1

abc[Inferior
1 (process 3818) exited normally]

进入gdb后,我们来看下有哪些功能可供调试。在程序调试中,断点是必不可少的。我们先来看下如何设置断点。break命令就是用来设置断点用的。

Break
行号 运行到某行停止

break
函数名称 运行到某个函数停止运行

break
行号/函数名称
if
条件

上面的程序中我们在第7行也就是for循环的地方做一个断点。disable
断点号可以停止断点。比如disable
1就是停止掉第一个断点

(gdb)
break 7

Breakpoint
1 at 0x80484b7: file test1.c, line 7.

然后执行run,程序执行到第7行的时候停止

(gdb)
run

Starting
program: /home/zhf/zhf/c_prj/test1

Breakpoint
1, main () at test1.c:7

7 char
a[4]="abc";

进入断点后我们可以通过print命令来打印具体的变量,来查看变量的值。其中$1代表的是第几次使用print命令。

(gdb)
print i

$1
= 0

(gdb)
print a[0]

$2
= 97 'a'

要在断点处继续执行需要执行next命令,与此同时我们还可以设置display
变量来查看每次运行后变量的

(gdb)
display i

1:
i = 0

(gdb)
next

9 printf("%c",a[i]);

1:
i = 0

(gdb)
next

7 for(i=0;i<strlen(a);i++)

1:
i = 0

(gdb)
next

9 printf("%c",a[i]);

1:
i = 1

(gdb)
next

7 for(i=0;i<strlen(a);i++)

1:
i = 1

(gdb)
next

9 printf("%c",a[i]);

1:
i = 2

如果不想单步的执行,可以运行continue命令运行到第二个断点处停止,如果没有第二个断点,则直接运行到程序结尾

通过whatis
变量名可以查看变量或者函数的类型:

(gdb)
whatis i

type
= int

在运行的时候还可以实时的改变变量的值来控制程序或者循环

(gdb)
set var i=0

(gdb)
print i

$7
= 0

另外还可以设置观察窗口,当变量发生变化的时候,程序立即停止,并显示变化的值。命令是watch

这里设置当i>0的时候停止,并显示出old
value和new
value

(gdb)
watch i>0

Hardware
watchpoint 5: i>0

(gdb)
continue

Continuing.

Hardware
watchpoint 3: i

Old
value = 0

New
value = 1

Hardware
watchpoint 5: i>0

Old
value = 0

New
value = 1

0x080484ec
in main () at test1.c:7

7 for(i=0;i<strlen(a);i++)

另外还有rwatch:当表达式值被读取的时候,就停止程序。awatch:用于当表达式的值被读或被写的时候,停止程序

在运行过程中还可以实时的查看源代码。采用list命令。

(gdb)
list

4 {

5 int
i=0;

6 char
a[4]="abc";

7 for(i=0;i<strlen(a);i++)

8 {

9 printf("%c",a[i]);

10 }

11 }

(gdb)
list 4,6

4 {

5 int
i=0;

6 char
a[4]="abc";

list
<function> 列出某个函数的代码

list
<first,last>列出first到last行的代码

list
<,last>列出当前行到last行的代码

list
<filename,n> 列出文件的第n行代码

list
<filename,function>列出文件名函数名为function的代码

linux c编程:gdb的使用的更多相关文章

  1. linux C编程 gdb的使用

    linux C编程 gdb的使用 通常来说,gdb是linux在安装时自带的,在命令行键入"gdb"字符并按回车键会启动gdb调试环境. 1.gdb的基本命令 命令 说明 file ...

  2. gdb笔记 ---《Linux.C编程一站式学习》

    gdb笔记 ---<Linux.C编程一站式学习> 单步执行和跟踪函数调用 函数调试实例 #include <stdio.h> int add_range(int low, i ...

  3. Linux环境编程相关的文章

    Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...

  4. Linux C编程之一:Linux下c语言的开发环境

    ---恢复内容开始--- 今天开始根据Linux C编程相关视频的学习所做的笔记,希望能一直坚持下去... 1.开发环境的构成 编辑器:VI: 编译器:选择GNU  C/C++编译器gcc: 调试器: ...

  5. Linux多任务编程之七:Linux守护进程及其基础实验(转)

    来源:CSDN  作者:王文松  转自Linux公社 ------------------------------------------------------------------------- ...

  6. Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号

    Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号 背景 上一讲我们介绍了Unix IPC中的2种管道. 回顾一下上一讲的介绍,IPC的方式通常有: Unix IPC包括:管道 ...

  7. [Linux] Linux C编程一站式学习 Part.3

    Linux系统编程 文件与I/O C标准I/O库函数与Unbuffered I/O函数 C标准I/O库函数printf().putchar().fputs(),会在用户空间开辟I/O缓冲区 系统函数o ...

  8. 【深入浅出Linux网络编程】 "开篇 -- 知其然,知其所以然"

    [深入浅出Linux网络编程]是一个连载博客,内容源于本人的工作经验,旨在给读者提供靠谱高效的学习途径,不必在零散的互联网资源中浪费精力,快速的掌握Linux网络编程. 连载包含4篇,会陆续编写发出, ...

  9. 【linux草鞋应用编程系列】_5_ Linux网络编程

    一.网络通信简介   第一部分内容,暂时没法描述,内容实在太多,待后续专门的系列文章.   二.linux网络通信     在linux中继承了Unix下“一切皆文件”的思想, 在linux中要实现网 ...

  10. 学习linux/unix编程方法的建议(转)

    假设你是计算机科班出身,计算机系的基本课程如数据结构.操作系统.体系结构.编译原理.计算机网络你全修过 我想大概可以分为4个阶段,水平从低到高从安装使用=>linux常用命令=>linux ...

随机推荐

  1. IIS 7 Access to the path ‘c:\windows\system32\inetsrv\’ is denied

    https://randypaulo.wordpress.com/2011/09/13/iis-7-access-to-the-path-cwindowssystem32inetsrv-isdenie ...

  2. C语言实现的水仙花数

    #include <stdio.h>void main(){ int ge,shi,bai;      for (int i =100; i < 1000; i++)     {   ...

  3. 在网页中插入flash播放器,播放flv视频

    效果图如下: 所用代码如下: <object id="FLVPlayer" height="480" width="640" code ...

  4. 删除UTF-8 BOM头的GUI小工具

    经常看到PHP群里有人因为UTF-8的BOM头出现这样那样的问题,给出的一个PHP删除BOM头的程序,新手也不会用,所以用wxpython写了一 个GUI,直接选择文件夹路径,就可以将该文件夹下所有指 ...

  5. osgcuda 【转】

    原文 : http://blog.sina.com.cn/s/blog_df1b276a0101inbi.html osgCompute是对代码的并行流处理器执行的抽象基库.库连接到OSG的(OSG) ...

  6. Python爬虫爬取美剧网站

    一直有爱看美剧的习惯,一方面锻炼一下英语听力,一方面打发一下时间.之前是能在视频网站上面在线看的,可是自从广电总局的限制令之后,进口的美剧英剧等貌似就不在像以前一样同步更新了.但是,作为一个宅diao ...

  7. AWK 思维导图

      完整的AWK思维导图 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing

  8. apue学习笔记(第五章 标准I/O)

    本章讲述标准I/O库 流和FILE对象 对于标准I/O库,它们的操作是围绕流进行的.流的定向决定了所读.写的字符是单字节还是多字节的. #include <stdio.h> #includ ...

  9. PCA的数学原理Matlab演示

    关于 PCA(Principal component analysis)主成分分析.是SVD(Singular value decomposition)神秘值分析的一种特殊情况.主要用于数据降维.特征 ...

  10. git stash 保存当前工作状态

    1. git stash   暂存当前工作状态 2. git stash list 查看暂存列表 3. git stash save 'title' 暂存工作状态并添加说明 4. git stash ...