一、gdb简介
GDB
是一个由GNU
开源组织发布的、UNIX/LINUX
操作系统下的、基于命令行的、功能强大的程序调试工具。 对于一名Linux
下工作的c/c++
程序员,gdb
是必不可少的工具;
二、gdb使用流程
1、启动gdb
编译一个测试程序,-g
表示可以调试,命令如下:
gcc -g test.c -o test
启动gdb,命令如下:
gdb test
gdb -q test //表示不打印gdb版本信息,界面较为干净;
测试如下:
root@ubuntu:/home/eit/c_test# gdb test
GNU gdb (Ubuntu 7.7-0ubuntu3) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <GNU General Public License>
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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<Bugs in GDB>.
Find the GDB manual and other documentation resources online at:
<GDB Documentation>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) q
root@ubuntu:/home/eit/c_test# gdb -q test
Reading symbols from test...done.
(gdb)
到此gdb
启动完成!
2、查看源码
list(简写 l)
: 查看源程序代码,默认显示10行,按回车键继续看余下的。
测试如下:
(gdb) list
9 #define MAX_SIZE
10
11 int main()
12 {
13 int i,fd,size1 ,size2 ,len;
14 char *buf = "helo!I'm li hanrui ";
15 char buf_r[15];
16 len = strlen(buf);
17 fd = open("/home/hello.txt",O_CREAT | O_TRUNC | O_RDWR,