GDB使用入门

以实际调试一个简单的程序来说明GDB的基本用法。

mlinlcnan@mlinlcnan-ThinkPad:~/Desktop$cat hello.c

#include <stdio.h>
void main(void *args)
{
    int loop = 0;
    int count = 0;
    for(; loop < 100; loop++)
    {
	count += loop;
    }
    printf("count:%d\n", count);
}

mlinlcnan@mlinlcnan-ThinkPad:~/Desktop$ gcc -g hello.c -o hello

——说明:编译时需要加上‘-g’的编译选项,表示编译时增加gdb调试信息

mlinlcnan@mlinlcnan-ThinkPad:~/Desktop$ ls

hello  hello.c    hello.c~

mlinlcnan@mlinlcnan-ThinkPad:~/Desktop$ ./hello

count:4950

mlinlcnan@mlinlcnan-ThinkPad:~/Desktop$ gdb ./hello

——这里是gdb加载可执行程序

——还可以通过‘gdb 可执行程序 进程ID’来加载已经在运行的程序进行调试

GNU gdb (GDB) 7.5.91.20130417-cvs-ubuntu

Copyright (C) 2013 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 andredistribute 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://www.gnu.org/software/gdb/bugs/>...

Reading symbols from /home/mlinlcnan/Desktop/hello...done.

(gdb) ——进入了gdb命令行

(gdb) break hello.c:5  ——通过‘break 源文件:行号’在特定的行设置断点

Breakpoint 1 at0x8048425: file hello.c, line 5.——‘Breakpoint1’1就是断点号

(gdb) run  ——通过‘run’(命令简写‘r’)运行,如果程序运行时有命令行参数,可以通过‘set args 参数’来设置运行参数,‘show args’可以查看已经设置的参数

Starting program:/home/mlinlcnan/Desktop/hello

Breakpoint 1,main (args=0x1) at hello.c:5

5      intloop = 0;

 (gdb)
——上面已经运行到设置的断点处了,‘ctrl+x+a’可以调出代码,以便单步调试,如下图:

——调试方法:命令‘n’表示‘next’,‘s’表示‘step’,如果调试的这一行代码是一个函数调用,‘n’则不会进入到函数里,‘s’则会进入到函数里;‘until’表示退出当前循环体;‘continue’表示从这里开始一直运行到下一个断点;

——已经设置的断点可以:删除-‘delete 断点号’,禁用-‘disable断点号’,启用-‘enable断点号’

——调试过程中查看调用的堆栈信息命令:‘bt’

——调试过程中查看变量的值:‘p 变量名’,如下:

 (gdb) p count

$4 = 4950

——多线程调试过程,可以使用‘info thread’查看当前线程情况,如下,下面的Id就是线程ID,‘Id  *1’前面有个‘*’标示当前线程

(gdb) info thread

  Id   TargetId         Frame

* 1    process 2140 "hello" main(args=0x1) at hello.c:11

(gdb)

——如果要进入某个特定的线程对其进行调试,使用,命令‘thread 线程ID’

——设置断点‘break xxx’上面有对某一行设置断点,还可以对函数设置断点(公共函数被多线程调用,还可以设置断点针对特定的线程)‘break 函数名 thread 线程ID’



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值