每天学点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.

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

posted @ 2013-03-25 20:43  徽沪一郎  阅读(819)  评论(0编辑  收藏  举报