mickole@test:~/ctest/05gdb$ gdb simple //开始gdb调试
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/mickole/ctest/05gdb/simple...done.
(gdb) list //显示源文件也可用l简写
1    #include <stdio.h>
2    #include <stdlib.h>
3   
4    long func(int n);
5   
6    int main(int argc, char *argv[])
7    {
8        printf("Entering main ...\n");
9        int i;
10        for (i=0; i<argc; ++i)
(gdb) //回车键,继续上一条命令
11        {
12            printf("%s ", argv[i]);
13        }
14        printf("\n");
15        long result = 0;
16        for (i=1; i<=100; ++i)
17        {
18            result += i;
19        }
20        printf("result[1-100] = %ld\n", result);
(gdb)
21        printf("result[1-10] = %ld\n", func(10));
22   
23        printf("Exiting main ...\n");
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
(gdb)
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;
34        }
35   
36        return sum;
37    }
(gdb)
Line number 38 out of range; simple.c has 37 lines.

(gdb) break 10 //设置断点也可用b
Breakpoint 1 at 0x8048459: file simple.c, line 10.
(gdb) info b //查看所有断点信息也可用i b代替
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10

(gdb) b func //也可在某个函数的入口位置设置断点
Breakpoint 2 at 0x804850b: file simple.c, line 29.
(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10
2       breakpoint     keep y   0x0804850b in func at simple.c:29

(gdb) l 10 //查看指定行源代码
5   
6    int main(int argc, char *argv[])
7    {
8        printf("Entering main ...\n");
9        int i;
10        for (i=0; i<argc; ++i)
11        {
12            printf("%s ", argv[i]);
13        }
14        printf("\n");
(gdb) l 29
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;

(gdb) r //开始运行调试run ,停在第一个断点处
Starting program: /home/mickole/ctest/05gdb/simple
Entering main ...

Breakpoint 1, main (argc=1, argv=0xbffff3f4) at simple.c:10
10        for (i=0; i<argc; ++i)
(gdb) step //单步调试
12            printf("%s ", argv[i]);
(gdb) s
10        for (i=0; i<argc; ++i)
(gdb)
14        printf("\n");
(gdb)
/home/mickole/ctest/05gdb/simple
15        long result = 0;
(gdb)
16        for (i=1; i<=100; ++i)
(gdb)
18            result += i;
(gdb)
16        for (i=1; i<=100; ++i)
(gdb) p i //打印i变量值print i
$1 = 1
(gdb) s
18            result += i;
(gdb)
16        for (i=1; i<=100; ++i)
(gdb)
18            result += i;
(gdb) p i
$2 = 3

(gdb) until //如果不想再继续循环可用until跳出循环,此处跳出for循环
20        printf("result[1-100] = %ld\n", result);
(gdb) l 20
15        long result = 0;
16        for (i=1; i<=100; ++i)
17        {
18            result += i;
19        }
20        printf("result[1-100] = %ld\n", result);
21        printf("result[1-10] = %ld\n", func(10));
22   
23        printf("Exiting main ...\n");
24        return 0;

(gdb) p result //跳出循环后打印result值
$4 = 5050
(gdb) s //继续单步跟踪
result[1-100] = 5050
21        printf("result[1-10] = %ld\n", func(10));
(gdb) c//continue继续跟踪,直接跳到下一个断点处
Continuing.

Breakpoint 2, func (n=10) at simple.c:29
29        long sum = 0;
(gdb) l 29
24        return 0;
25    }
26   
27    long func(int n)
28    {
29        long sum = 0;
30        int i;
31        for (i=1; i<=n; ++i)
32        {
33            sum += i;
(gdb) s //单步跟踪
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb)
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb) p sum
$5 = 6
(gdb) s
33            sum += i;
(gdb)
31        for (i=1; i<=n; ++i)
(gdb) finish //如果想跳出函数,可用finish
Run till exit from #0  func (n=10) at simple.c:31
0x080484e1 in main (argc=1, argv=0xbffff3f4) at simple.c:21
21        printf("result[1-10] = %ld\n", func(10));
Value returned is $6 = 55
(gdb) s
result[1-10] = 55
23        printf("Exiting main ...\n");
(gdb) s
Exiting main ...
24        return 0;
(gdb) c //继续跟踪,直至正常结束
Continuing.
[Inferior 1 (process 4450) exited normally]

(gdb) quit //退出调试
mickole@test:~/ctest/05gdb$

以下是单个命令解析:

run: 开始调试,后面可接程序运行所需参数,如:

mickole@test:~/ctest/05gdb$ gdb simple
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/mickole/ctest/05gdb/simple...done.
(gdb) r a b c
Starting program: /home/mickole/ctest/05gdb/simple a b c
Entering main ...
/home/mickole/ctest/05gdb/simple a b c
result[1-100] = 5050
result[1-10] = 55
Exiting main ...
[Inferior 1 (process 4476) exited normally]
(gdb)

 

list: 列出源代码:

list 默认列出前十行

list 10 列出指定行

list 2,10 列出2到10行

list ,15 列出第15行包括第15行前面十行,如:

(gdb) l ,15
6    int main(int argc, char *argv[])
7    {
8        printf("Entering main ...\n");
9        int i;
10        for (i=0; i<argc; ++i)
11        {
12            printf("%s ", argv[i]);
13        }
14        printf("\n");
15        long result = 0;

 

list main :列出指定函数的前十行,如

(gdb) l main
2    #include <stdlib.h>
3   
4    long func(int n);
5   
6    int main(int argc, char *argv[])
7    {
8        printf("Entering main ...\n");
9        int i;
10        for (i=0; i<argc; ++i)
11        {

list hello.c:func  :查看指定文件中的指定函数源码

 

设置断点和查看断点:

break 行号

break fun

break file:行号

break file:fun

break if <condition> - 条件成立时程序停住。

info break(i b) - 查看断点

watch expr - 一量expr值发生改变,程序停住。

delete n - 删除断点,如:

(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048459 in main at simple.c:10
2       breakpoint     keep y   0x0804850b in func at simple.c:29
(gdb) d 1
(gdb) d 2

 

单步调试:

continue(c) - 运行至下一个断点

step(s) - 单步跟踪,进入函数,类似于VC中的step in

next(n) - 单步跟踪,不进入函数,类似于VC中的step out

finish - 运行程序,直到当前函数完成返回。并打印函数返回时的堆栈地址和返回值及参数值等信息。

until - 当厌倦了在一个循环体内单步跟踪时,这个命令可以运行程序直到退出循环体。

 

gdb常用命令总结:

 

现在利用gdb查看运行时数据:

print - 查看变量值

ptype - 查看类型

print array - 查看数组

print arr1  -查看整个数组元素

print arr[2] -查看指定数组元素

print &arr1[2] -查看指定下标元素地址

print &arr1 - 查看数组首地址

print *array@len - 查看动态内存

print x=5 - 改变运行时数据

 

gdb调试段错误:

段错误是由于访问非法地址而产生的错误。

  • 访问系统数据区,尤其是往系统保护的内存地址写数据。最常见就是给一个指针以0地址
  • 内存越界(数组越界,变量类型不一致等) 访问到不属于你的内存区域

示例:

mickole@test:~/ctest/06gdb$ gdb bugging
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/mickole/ctest/06gdb/bugging...done.
(gdb) r //如果程序出现段错误,最好的定位方式就是直接运行,它会自动停在段错误处
Starting program: /home/mickole/ctest/06gdb/bugging

Program received signal SIGSEGV, Segmentation fault.
0x080483c4 in segfault () at bugging.c:7
7        *p = 100;
(gdb) bt  //利用栈回溯
#0  0x080483c4 in segfault () at bugging.c:7
#1  0x080483d4 in main () at bugging.c:12
(gdb) l 7 //定位到第7行
2    #include <stdlib.h>
3   
4    void segfault()
5    {
6        int *p = NULL;
7        *p = 100;
8    }
9   
10    int main(void)
11    {
(gdb)

 

利用core文件进行调试:

1、core文件

在程序崩溃时,一般会生成一个文件叫core文件。core文件记录的是程序崩溃时的内存映像,并加入调试信息。

core文件生成的过程叫做core dump

2、设置生成core文件

ulimit -c 查看core-dump状态即是否会产生core文件,如:

mickole@test:~/ctest/06gdb$ ulimit -a
core file size          (blocks, -c) 0 //块大小为0,表示不会产生core文件
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7895
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7895
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
mickole@test:~/ctest/06gdb$ ulimit -c
0
mickole@test:~/ctest/06gdb$ ulimit -c 1024 //设置core文件块大小
mickole@test:~/ctest/06gdb$ ulimit -c
1024

ulimit -c 数字(如:ulimit -c 1024)

ulimit -c unlimited

3、gdb利用core文件调试

gdb 文件名 core文件

bt

 

示例:

mickole@test:~/ctest/06gdb$ ./bugging
Segmentation fault (core dumped)
mickole@test:~/ctest/06gdb$ ls
bugging  bugging.c  core  Makefile  reverse  reverse.c  simple  simple.c

以上出现段错误,产生core文件,现在利用core文件进行调试:

mickole@test:~/ctest/06gdb$ gdb bugging core
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/mickole/ctest/06gdb/bugging...done.
[New LWP 4582]

warning: Can't read pathname for load map: Input/output error.
Core was generated by `./bugging'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483c4 in segfault () at bugging.c:7
7        *p = 100;
(gdb) bt
#0  0x080483c4 in segfault () at bugging.c:7
#1  0x080483d4 in main () at bugging.c:12
(gdb) l 7
2    #include <stdlib.h>
3   
4    void segfault()
5    {
6        int *p = NULL;
7        *p = 100;
8    }
9   
10    int main(void)
11    {
(gdb)

gdb用法的更多相关文章

  1. GDB用法简要整理

    [时间:2017-05] [状态:Open] [关键词:gdb,调试,debug,用户手册] 使用gdb是需要在编译是指定-g命令,在可执行文件中添加符号信息. 1. 启动和退出 可以使用gdb gd ...

  2. 【Linux】GDB用法详解(5小时快速教程)

    GDB是一个强大的命令行调试工具.虽然X Window提供了GDB的图形版DDD,但是我仍然更钟爱在命令行模式下使用GDB.大家知道命令行的强大就是在于,其可以形成执行序列,形成脚本. UNIX下的软 ...

  3. GDB 调试多线程多进程

    GDB是linux下的调试利器,在c/c++程序开发过程中必不可少的.这里总结一下多进程和多线程的调试方法和技巧. 多进程的调试: 如下示例 #include <sys/mman.h> # ...

  4. gcc-4.8.3安装,gdb-7.6安装

    gdb用法: http://blog.chinaunix.net/uid-26548237-id-3435525.html gdb-7.6.tar.gz:  (官网下载:http://ftp.gnu. ...

  5. [找工作] 2019秋招|从春招到秋招,Java岗经验总结(收获AT)

    转自(有更多) https://blog.csdn.net/zj15527620802/article/month/2018/10 前言 找工作是一件辛酸而又难忘的历程.经历过焦虑.等待.希望,我们最 ...

  6. TECH books

    文章目录 TECH books linux vxworks bat c gdb c++ vbscript make java bash perl web uml software-misc cpu e ...

  7. C++程序员面试题目总结(涉及C++基础、多线程多进程、网络编程、数据结构与算法)

     说明:C++程序员面试题目总结(涉及C++基础知识.多线程多进程.TCP/IP网络编程.Linux操作.数据结构与算法) 内容来自作者看过的帖子或者看过的文章,个人整理自互联网,如有侵权,请联系作者 ...

  8. linux下的程序调试方法汇总

    搞电子都知道,电路不是焊接出来的,是调试出来的.程序员也一定认同,程序不是写出来的,是调试出来的.那么调试工具就显得尤为重要,linux作为笔者重要的开发平台,在linux中讨论调试工具主要是为那些入 ...

  9. [转]五个Linux下用户空间的调试工具

    有几个Linux下的用户空间调试工具和技术,它们用来分析用户空间的问题相当有用.它们是: 'print' 语句 查询 (/proc, /sys 等) 跟踪 (strace/ltrace) Valgri ...

随机推荐

  1. 数字组合 · Combination Sum

    不能重复: [抄题]: 给出一个候选数字的set(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T.C中的数字可以无限制重复被选取. 例如,给出候选数组[2,3,6,7]和目标数字7,所求 ...

  2. [leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  3. Photoshop和Halcon中的极坐标变换

    极坐标想必学过高中数学的人都听过,一般的坐标系中用(x, y)值来描述一个点的位置,而在极坐标系中,则使用到原点的距离ρ和夹角θ来描述该点的位置. 我很早就接触了Photoshop,知道Photosh ...

  4. sphinx文档

    Navigation index modules | Sphinx主页 | 文档 » 下载 目前版本: 1.2 获得 Sphinx 从 Python Package Index, 或者使用如下命令安装 ...

  5. 现代编译原理--第二章(语法分析之LR(1))

    (转载请表明出处  http://www.cnblogs.com/BlackWalnut/p/4472772.html) 前面已经介绍过LL(1),以及如何使用LL(1)文法.但是LL(K)文法要求在 ...

  6. Storm 系列(三)Storm 集群部署和配置

    Storm 系列(二)Storm 集群部署和配置 本章中主要介绍了 Storm 的部署过程以及相关的配置信息.通过本章内容,帮助读者从零开始搭建一个 Storm 集群. 一.Storm 的依赖组件 1 ...

  7. centos7 nginx 加入开机启动

    设置nginx开机启动 vi /etc/rc.d/init.d/nginx  #编辑启动文件添加下面内容 ############################################### ...

  8. centos6.5 redis 安装配置及java调用

    1.安装gcc 执行命令  yum install gcc 2. 安装ruby环境 yum install ruby yum install rubygems gem install redis 下载 ...

  9. java如何实现以数据流的形式下载压缩包到本地?

    先不多说,直接贴代码吧,在服务器的E盘下放一个E:/manual.rar的压缩包 package com.cellstrain.icell.controller; import org.springf ...

  10. phalApi数据库操作

    在很多时候,我们会遇到数据库表里面的某个值需要+1操作,我们不能简单地在update的时候写入array('key' => 'key+1'),因为在解析sql的时候,key+1 会带上引号作为一 ...