每天学点GDB 1

在Linux环境下进行C或是C++编程,调试工具首选GDB。

GDB的功能很多,一下子全弄明白似乎不太可能。那么就从最简单的使用说起吧。一谈起简单,Helloworld就成了最佳的选择了。

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main(int argc, char** argv) {
5     printf("hello,world\n");
6     return 0;
7 }

编译链接

gcc -o hello -g helloworld.c

 好了,现在可执行目标文件hello已经生成,接下来的事情就是用gdb将其载入并运行。

gdb ./hello

接下来的会看到如下的信息。

gdb hello
GNU gdb (GDB) 7.5.1
Copyright (C) 2012 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-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /tmp/hello...done.
(gdb)

显示源代码

(gdb)list 1,10

1	#include <stdlib.h>
2	#include <stdio.h>
3
4	int main(int argc, char** argv) {
5	  printf("hello,world\n");
6	  return 0;
7	}

设置断点有多种方法,如通过函数名,通过指定源码行数,下面的例子通过指定文件中的行数来设置断点

(gdb)break 7

运行程序

(gdb)run

当检查完相关的变量后,继续运行程序

(gdb)continue

最终,退出gdb

(gdb)quit

ok,重新回到bash shell.

至此一个简单的调试例子完整的过了一遍,是的,平淡的如白开水一般。

转载于:https://www.cnblogs.com/hseagle/archive/2013/03/25/GDB.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值