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常用命令总结:

QQ截图20130929161119

 

现在利用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)

转载于:https://www.cnblogs.com/mickole/p/3346015.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐