GDB使用示例(1)

##参考 gdb的使用方法
使用gdb调试程序
gdb调试

##编辑文件

$ vim test.c

#include <stdio.h>
int main()
{
    int i = 2;
    printf("%d\n", i);  
    return 0;
}

##编译

$ gcc -o test -g test.c

##示例1

注意,(gdb)是提示符。

使用gdb打开之:

letian $ gdb test 
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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 "x86_64-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 test...done.
(gdb)

列出代码:

(gdb) list
1   #include <stdio.h>
2   int main()
3   {
4       int i = 2;
5       printf("%d\n", i);  
6       return 0;
7   }

用run或者r运行程序:

(gdb) run
Starting program: /home/letian/Desktop/code/c/test 
2
[Inferior 1 (process 4372) exited normally]
(gdb) r
Starting program: /home/letian/Desktop/code/c/test 
2
[Inferior 1 (process 4376) exited normally]

##示例2

start程序,停在main()的开始处:

$ gdb test 
(gdb) start
Temporary breakpoint 1 at 0x400535: file test.c, line 4.
Starting program: /home/letian/Desktop/code/c/test 

Temporary breakpoint 1, main () at test.c:4
4       int i = 2;

此时int i = 2;还没有执行,输出i的值:

(gdb) print i
$1 = 0

执行int i = 2;,输出i的值:

(gdb) step
5       printf("%d\n", i);  
(gdb) print i
$2 = 2
(gdb) 

执行printf("%d\n", i);,直接把2输出了:

(gdb) step
2
6       return 0;

结束程序的执行:

(gdb) step
7   }
(gdb) step
0x00007ffff7a36ec5 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) step
Single stepping until exit from function __libc_start_main,
which has no line number information.
[Inferior 1 (process 4403) exited normally]

退出gdb:

(gdb) quit

##示例3

将代码修改为:

#include <stdio.h>
int main()
{
    int i = 2;
    printf("%d\n", i);  
    return -1;
}

并重新编译。

sstep的简写,qquit的简写。现在重新gdb下:

$ gdb test             
......
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) start
Temporary breakpoint 1 at 0x400535: file test.c, line 4.
Starting program: /home/letian/Desktop/code/c/test 

Temporary breakpoint 1, main () at test.c:4
4       int i = 2;
(gdb) s
5       printf("%d\n", i);  
(gdb) s
2
6       return -1;
(gdb) s
7   }
(gdb) s
0x00007ffff7a36ec5 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) s
Single stepping until exit from function __libc_start_main,
which has no line number information.
[Inferior 1 (process 4562) exited with code 0377]
(gdb) s
The program is not being run.
(gdb) q
$

##示例4

$ gdb test             
......
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) list      # 列出源码
1   #include <stdio.h>
2   int main()
3   {
4       int i = 2;
5       printf("%d\n", i);  
6       return -1;
7   }

(gdb) b 5       # 在第5行设置断点
Breakpoint 1 at 0x40053c: file test.c, line 5.

(gdb) info breakpoints   # 断点信息,只有一个断点,编号是1
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040053c in main at test.c:5

(gdb) start
Temporary breakpoint 2 at 0x400535: file test.c, line 4.
Starting program: /home/letian/Desktop/code/c/test 

Temporary breakpoint 2, main () at test.c:4
4       int i = 2;
(gdb) n         # 执行i=2,然后就到断点(第5行了)

Breakpoint 1, main () at test.c:5
5       printf("%d\n", i);  
(gdb) n         # 执行printf(此处是断点)
2

6       return -1;
(gdb) n

7   }
(gdb) n
0x00007ffff7a36ec5 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040053c in main at test.c:5
    breakpoint already hit 1 time

(gdb) delete 
Delete all breakpoints? (y or n) y

(gdb) info breakpoints
No breakpoints or watchpoints.

##重新编写源代码test.c

#include <stdio.h>
int main()
{
    int a = 2;
    int b = 9;
    int c = a+b;
    printf("%d\n", c);  
    return -1;
}

重新编译:

$ gcc -o test -g test.c

##示例5 使用break(简写为b)设置断点。

$ gdb test
....
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) list
1   #include <stdio.h>
2   int main()
3   {
4       int a = 2;
5       int b = 9;
6       int c = a+b;
7       printf("%d\n", c);  
8       return -1;
9   }
(gdb) b 6           # 在第6行设置断点
Breakpoint 1 at 0x400543: file test.c, line 6.
(gdb) run           # 运行到断点处
Starting program: /home/letian/Desktop/code/c/test 

Breakpoint 1, main () at test.c:6
6       int c = a+b;
(gdb) print a
$1 = 2
(gdb) print b
$2 = 9
(gdb) print c       # 值为0,还没进行a+b
$3 = 0
(gdb) print d       # 不存在变量d
No symbol "d" in current context.
(gdb) n             # 下一步
7       printf("%d\n", c);  
(gdb) print c       # c有值了
$4 = 11
(gdb) n
11
8       return -1;
(gdb) n
9   }
(gdb) n
0x00007ffff7a36ec5 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6

##示例6 使用continue(简写为c)运行到下一个断点处。

$ gdb test
......
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) list
1   #include <stdio.h>
2   int main()
3   {
4       int a = 2;
5       int b = 9;
6       int c = a+b;
7       printf("%d\n", c);  
8       return -1;
9   }

(gdb) b 4
Breakpoint 1 at 0x400535: file test.c, line 4.

(gdb) b 6
Breakpoint 2 at 0x400543: file test.c, line 6.

(gdb) run  # 运行到断点处
Starting program: /home/letian/Desktop/code/c/test 

Breakpoint 1, main () at test.c:4
4       int a = 2;
(gdb) c    # continue,运行到下一个断点处
Continuing.

Breakpoint 2, main () at test.c:6
6       int c = a+b;
(gdb) c
Continuing.
11
[Inferior 1 (process 5148) exited with code 0377]
(gdb) 

转载于:https://my.oschina.net/letiantian/blog/485897

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值