GDB 使用简单介绍


GDB概述

GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具

一般来说,GDB主要帮忙你完成下面四个方面的功能:

  • 启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
  • 可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
  • 当程序被停住时,可以检查此时你的程序中所发生的事。
  • 动态的改变你程序的执行环境。

一个简单的小程序

#include <stdio.h>

#define N 100

int fun(int x) {
    if (x < 0)
        return 0;
    return x + fun(x - 1);
}

int add(int a, int b) {
    return a + b;
}

void test_fun() {
    int x = 0;
    for (int i = 0; i < 10; ++i) {
        x = i;
    }

    for (int j = 0; j < 10; ++j) {
        x = j;
    }
}

int globalval = 10;

int main() {
    int n = 100;
    int a = 1;
    int b = 2;
    char str[100] = "hello, world";

    int c = add(a, b);

    for (int i = 0; i < n; ++i) {
        a = i;
    }

    c = -1;

    int d = add(a, b);

    test_fun();
    printf("c = %d d = %d\n", c, d);

    int ary[N];

    int x;
    scanf("%d", &x);
    int ans = fun(x);

    printf("%d\n", ans);
    printf("%s\n", str);

    return 0;
}

然后,通过以下命令编译文本

gcc -g a.cc

使用参数 -g 表示将源代码信息编译到可执行文件中,生成的可执行文件为a.out,当然,也可以用-o指定


GDB 常用命令

  在命令行中键入gdb进入调试模式

0. h

h file              # 查看file命令的帮助信息

help命令的缩写,查看命令帮助
  

1. file

file a.out

通过file命令载入被调试的程序
  

2. r

r a.out             # 运行被调试程序

run命令的缩写,运行程序
  

3. l

l                   # 列出程序的源代码,默认为10行
l 20                # 列出以第20行为中心的前后10行的代码
l fun               # 显示fun函数的源代码

list的缩写,查看源代码
  

4. b

b 22                # 在22行设置断点
b fun               # 在函数fun入口设置断点
b add if a > b      # 如果a > b, 在add函数设置断点

break的缩写,设置断点
  

5. i

i b                 # info breakpoints的缩写,显示断点情况
i locals            # info locals显示当前运行程序的变量值
i line              # info line 显示当前执行到第几行
i display           # info display 查看所有的display
i watch             # info watch 查看所有的watch
i thread            # info thread 查看当前进程的线程

info的缩写,显示相关信息
  

6. p

p a                 # 打印a的值
p str               # 打印字符串的值

print的缩写,打印相关的值
  

7. whatis

whatis ary          # 查询ary数组  结果为 type = int [100]
whatis str          # 查询字符串数组 结果为  type = char [100]

查询变量或函数
  

8. n

n                   # 继续执行下一行

next的缩写
  

9. s

s                   # 执行下一行

n不同的是,碰到函数,s会进入函数,而n会跳过函数,继续执行下一行
  

10. set

set $i = 0
set ary[$i++]       # 设置变量i为0,然后这样就可以一路回车,查看ary数组的情况
set variable x = 10 # 设置调试程序中的变量为10

set
  

11. bt

bt                  # 查看程序运行栈的相关信息

backtrace的缩写, 查看程序栈的相关信息
  

12. d

d                   # 删除所有的断点
d 2                 # 删除编号为2的断点
d watch 3           # 删除编号为2的watch
d display 2         # 删除编号为2的display

delete的缩写,删除断点
  

13. c

c

continue的缩写,跳到下一个断点处
  

14. u

u                   # 跳出当前的循环
u 20                # 跳到程序的第20行

until的缩写,用来跳出循环
  

15. finish

finish              # 跳出当前函数

跳出当前函数,并打印函数返回时的堆栈地址和返回值及参数值等信息
  

16. call

call fun(10)        # 调用程序中fun(10)函数

调用程序中可见的函数
  

17. disable

disable 2           # 暂停第二个断点

disable
  

18. enable

enable 2            # 开启第二个断点

enable
  

19. display

display a           # 每次单步调试都会显示a的值

在单步运行时将非常有用,使用display命令设置一个表达式后,它将在每次单步进行指令后,紧接着输出被设置的表达式及值
  

20. undisplay

undisplay 2         # 取消编号为2的display

undisplay
  

21. watch

watch a             # 假设a的值被改变,gdb将会给出提示
watch a + b < 1     # a + b  < 1 的时候,gdb会给提示

设置一个监视点,一旦被监视的“表达式”的值改变,gdb给出提示
  

21. kill

终止被调试程序
  

23. q

退出gdb

posted @ 2016-05-21 19:47  spxcds  阅读(265)  评论(0编辑  收藏  举报