p { margin-bottom: 0.25cm; line-height: 120% }
首先用一个简单的打印字符的程序来做下示例
#include<stdio.h>
#include<string.h>
void main()
{
int i=0;
char a[4]="abc";
for(i=0;i<strlen(a);i++)
{
printf("%c",a[i]);
}
}
要想用gdb调试文件,有两种方法 1 gcc生成可执行的文件, gdb 直接调试文件2 直接进入gdb 然后输入file 可执行文件
p { margin-bottom: 0.25cm; line-height: 120% }
方法一:
[email protected]:/home/zhf/zhf/c_prj# gcc -g -o test1 test1.c
[email protected]:/home/zhf/zhf/c_prj# gdb test1
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test1...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/zhf/zhf/c_prj/test1
abc[Inferior 1 (process 3802) exited normally]
方法二:
[email protected]:/home/zhf/zhf/c_prj#
gdb
GNU
gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright
(C) 2016 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 "i686-linux-gnu".
Type
"show configuration" for configuration details.
For
bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find
the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For
help, type "help".
Type
"apropos word" to search for commands related to "word".
(gdb)
file test1
Reading
symbols from test1...(no debugging symbols found)...done.
(gdb)
run
Starting
program: /home/zhf/zhf/c_prj/test1
abc[Inferior
1 (process 3818) exited normally]
进入gdb后,我们来看下有哪些功能可供调试。在程序调试中,断点是必不可少的。我们先来看下如何设置断点。break命令就是用来设置断点用的。
Break
行号 运行到某行停止
break
函数名称 运行到某个函数停止运行
break
行号/函数名称
if
条件
上面的程序中我们在第7行也就是for循环的地方做一个断点。disable
断点号可以停止断点。比如disable
1就是停止掉第一个断点
(gdb)
break 7
Breakpoint
1 at 0x80484b7: file test1.c, line 7.
然后执行run,程序执行到第7行的时候停止
(gdb)
run
Starting
program: /home/zhf/zhf/c_prj/test1
Breakpoint
1, main () at test1.c:7
7 char
a[4]="abc";
进入断点后我们可以通过print命令来打印具体的变量,来查看变量的值。其中$1代表的是第几次使用print命令。
(gdb)
print i
$1
= 0
(gdb)
print a[0]
$2
= 97 ‘a‘
要在断点处继续执行需要执行next命令,与此同时我们还可以设置display
变量来查看每次运行后变量的
(gdb)
display i
1:
i = 0
(gdb)
next
9 printf("%c",a[i]);
1:
i = 0
(gdb)
next
7 for(i=0;i<strlen(a);i++)
1:
i = 0
(gdb)
next
9 printf("%c",a[i]);
1:
i = 1
(gdb)
next
7 for(i=0;i<strlen(a);i++)
1:
i = 1
(gdb)
next
9 printf("%c",a[i]);
1:
i = 2
如果不想单步的执行,可以运行continue命令运行到第二个断点处停止,如果没有第二个断点,则直接运行到程序结尾
通过whatis
变量名可以查看变量或者函数的类型:
(gdb)
whatis i
type
= int
在运行的时候还可以实时的改变变量的值来控制程序或者循环
(gdb)
set var i=0
(gdb)
print i
$7
= 0
另外还可以设置观察窗口,当变量发生变化的时候,程序立即停止,并显示变化的值。命令是watch
这里设置当i>0的时候停止,并显示出old
value和new
value
(gdb)
watch i>0
Hardware
watchpoint 5: i>0
(gdb)
continue
Continuing.
Hardware
watchpoint 3: i
Old
value = 0
New
value = 1
Hardware
watchpoint 5: i>0
Old
value = 0
New
value = 1
0x080484ec
in main () at test1.c:7
7 for(i=0;i<strlen(a);i++)
另外还有rwatch:当表达式值被读取的时候,就停止程序。awatch:用于当表达式的值被读或被写的时候,停止程序
在运行过程中还可以实时的查看源代码。采用list命令。
(gdb)
list
4 {
5 int
i=0;
6 char
a[4]="abc";
7 for(i=0;i<strlen(a);i++)
8 {
9 printf("%c",a[i]);
10 }
11 }
(gdb)
list 4,6
4 {
5 int
i=0;
6 char
a[4]="abc";
list
<function> 列出某个函数的代码
list
<first,last>列出first到last行的代码
list
<,last>列出当前行到last行的代码
list
<filename,n> 列出文件的第n行代码
list
<filename,function>列出文件名函数名为function的代码