[GDB] GDB的快速入门指南:安装、启动、断点、单步调试、恢

2018-09-10  本文已影响165人  AkuRinbu

目录

hello.c
gcc 编译
安装GDB
启动GDB

断点
    设置断点
    查看断点
    清除断点
    启用与禁用断点
单步调试
恢复执行
查看变量

hello.c

#include <stdio.h>

int main(void)
{
    printf("Hello, world!\n");
    return 0;
}

gcc 编译

anno@anno-m:~/Desktop$ ls
hello.c

anno@anno-m:~/Desktop$ gcc -g hello.c -o hello

anno@anno-m:~/Desktop$ ls
hello  hello.c

安装GDB

http://www.gdbtutorial.com/tutorial/how-install-gdb

$ sudo apt-get update
$ sudo apt-get install gdb
$ gdb -version
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.

启动GDB

anno@anno-m:~/Desktop$ gdb hello

(gdb) run
Starting program: /home/anno/Desktop/hello 
Hello, world!
[Inferior 1 (process 2978) exited normally]
GDB 默认模式 GDB TUI模式

断点

设置断点

anno@anno-m:~/Desktop$ gdb hello

(gdb) b main
Breakpoint 1 at 0x400531: file hello.c, line 5.
(gdb) r
Starting program: /home/anno/Desktop/hello 

Breakpoint 1, main () at hello.c:5
5       printf("Hello, world!\n");
(gdb) 

查看断点

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400531 in main at hello.c:5
    breakpoint already hit 1 time

清除断点

(gdb) b main
Breakpoint 1 at 0x400531: file hello.c, line 5.
(gdb) i b 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400531 in main at hello.c:5
(gdb) delete 1
(gdb) i b
No breakpoints or watchpoints.
(gdb) b main
Breakpoint 1 at 0x400531: file hello.c, line 5.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400531 in main at hello.c:5
(gdb) clear main
Deleted breakpoint 1 
(gdb) i b
No breakpoints or watchpoints.
(gdb) 

启用与禁用断点

(gdb) b main
Breakpoint 1 at 0x400531: file hello.c, line 5.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400531 in main at hello.c:5

(gdb) disable 1
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x0000000000400531 in main at hello.c:5

(gdb) enable 1
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400531 in main at hello.c:5

单步调试

(gdb) r
Starting program: /home/anno/Desktop/hello 

Breakpoint 1, main () at hello.c:5
5       printf("Hello, world!\n");
(gdb) n
Hello, world!
7       return 0;
(gdb) n
8   }
(gdb) n
__libc_start_main (main=0x40052d <main>, argc=1, argv=0x7fffffffdf38, 
    init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, 
    stack_end=0x7fffffffdf28) at libc-start.c:321
321 libc-start.c: No such file or directory.
(gdb) n
[Inferior 1 (process 3984) exited normally]

恢复执行

查看变量

[GDB]检查变量:print、disp、call
https://www.jianshu.com/p/79671588f6d8

更多功能

一、TUI模式,双开 汇编代码 窗口

TUI模式,双开 汇编代码 窗口
set disassembly-flavor intel
set disassembly-flavor att

参考资料

Beej's Quick Guide to GDB

http://beej.us/guide/bggdb/

上一篇 下一篇

猜你喜欢

热点阅读