手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

GDB调试指南-启动调试

[日期:2019-01-24] 来源:简书  作者:AkuRinbu [字体: ]

GDB的快速入门指南:安装、启动、断点、单步调试、恢复执行、查看变量

目录

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

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

hello.c

#include <stdio.h>

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

gcc 编译

  • 在用gcc编译代码的时候,需要开启 -g 选项,提供调试用的信息;
  • gdb就是根据这些信息,来进行调试的;
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]
  • TUI模式,按组合键Ctrl+X+A切换
 
GDB 默认模式
 
GDB TUI模式

断点

设置断点

  • break 命令设置断点,简写b ;
  • break main ,在main()函数的入口处设置断点;
  • break 5,在源代码的第5行设置断点;
  • break hello.c:5 ,指定源码文件的代码第5行设置断点;
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) 

查看断点

  • info breakpoints,显示当前全部的断点,简写i b
(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

清除断点

  • delete + 断点的数值标识符delete 1,删除第1个断点;
(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.
  • clear + 函数名 、 +行号、+文件名:行号 ,清除断点main()函数处的断点:clear main 或者 clear 5 (本质是main函数的第一条语句所在);
(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) 

启用与禁用断点

  • disable + 断点的数值标识符disable 1禁用第1个断点;
  • enable + 断点的数值标识符enable 1启用第1个断点;
  • Enb字段,表明断点是禁用(n)还是启用(y)的;
(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

单步调试

  • nextn越过 函数调用(函数会在背地里自己悄悄运行完),单步执行;
  • steps进入 函数体内部,单步执行;
(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]

恢复执行

  • continuec,恢复执行,直到遇到下一个断点;
  • continue命令执行期间,按下CTRL-C瞬间停止;

查看变量

[GDB]检查变量:print、disp、call

  • disp,使得每次有暂停,都会输出指定的变量的值;
  • printp,只显示一次变量的值;
  • 要求变量名在当前的域是可见的,比如某个变量i函数foo()局部变量,那么只有是在进入到这个函数的里面时才可以使用print i 或者 disp i,不然gdb也不知道i是谁;

更多功能

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

  • 1、Ctrl + X + A 进入TUI模式;
  • 2、(gdb) list:显示10行C源码;
  • 3、(gdb) layout split :同时显示C源码以及汇编源码
  • 4、(gdb) info registers:显示使用到的寄存器信息;
 
TUI模式,双开 汇编代码 窗口
  • 5、(gdb) set disassembly-flavor intel :改变显示的汇编语法;
set disassembly-flavor intel
set disassembly-flavor att
  • 6、再次输入(gdb) layout split,使语法改变生效;
     
    显示Intel语法的汇编
linux
相关资讯       GDB  GDB调试 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款