- 它使你能监视你程序中变量的值.
- 它使你能设置断点以使程序在指定的代码行上停止执行.
- 它使你能一行行的执行你的代码.
GDB is free software and you are welcome to distribute copies of it
under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.14 (i486-slakware-linux), Copyright 1995 Free Software Foundation, Inc.
(gdb)
gdb <fname>
为调试编译代码(Compiling Code for Debugging)
gdb 基本命令
命 | 描 |
file | 装入想要调试的可执行文件. |
kill | 终止正在调试的程序. |
list | 列出产生执行文件的源代码的一部分. |
next | 执行一行源代码但不进入函数内部. |
step | 执行一行源代码而且进入函数内部. |
run | 执行当前被调试的程序 |
quit | 终止 gdb |
watch | 使你能监视一个变量的值而不管它何时被改变. |
break | 在代码里设置断点,这将使程序执行到这里时被挂起. |
make | 使你能不退出 gdb 就可以重新产生可执行文件. |
shell | 使你能不离开 gdb 就执行 UNIX shell命令. |
gdb 应用举例
#include <stdio.h>
main ()
{
char my_string[] = "hello there";
my_print (my_string);
my_print2 (my_string);
}
void my_print (char *string)
{
printf ("The string is %s\n", string);
}
void my_print2 (char *string)
{
char *string2;
int size, i;
size = strlen (string);
string2 = (char *) malloc (size + 1);
for (i = 0; i < size; i++)
string2[size - i] = string[i];
string2[size+1] = `\0';
printf ("The string printed backward is %s\n", string2);
}
gcc -o test test.c
The string is hello there
The string printed backward is
The string printed backward is ereht olleh
gdb greeting
-
注意: 记得在编译 greeting程序时把调试选项打开.
(gdb) file greeting
(gdb) run
Starting program: /root/greeting
The string is hello there
The string printed backward is
Program exited with code 041
(gdb) list
(gdb) list
(gdb) list
-
技巧:在 gdb提示符下按回车健将重复上一个命令.
1 #include <stdio.h>
2
3 main ()
4 {
5 char my_string[] = "hello there";
6
7 my_print (my_string);
8 my_print2 (my_string);
9 }
10
11 my_print (char *string)
12 {
13 printf ("The string is %s\n", string);
14 }
15
16 my_print2 (char *string)
17 {
18 char *string2;
19 int size, i;
20
21 size = strlen (string);
22 string2 = (char *) malloc (size + 1);
23 for (i = 0; i < size; i++)
24 string2[size - i] = string[i];
25 string2[size+1] = `\0';
26 printf ("The string printed backward is %s\n", string2);
27 }
(gdb) break 24
Breakpoint 1 at 0x139: file greeting.c, line 24
(gdb)
Starting program: /root/greeting
The string is hello there
Breakpoint 1, my_print2 (string = 0xbfffdc4 "hello there") at greeting.c :24
24 string2[size-i]=string[i]
(gdb) watch string2[size - i]
Watchpoint 2: string2[size - i]
(gdb) next
Watchpoint 2, string2[size - i]
Old value = 0 `\000'
New value = 104 `h'
my_print2(string = 0xbfffdc4 "hello there") at greeting.c:23
23 for (i=0; i<size; i++)
#include <stdio.h>
main ()
{
char my_string[] = "hello there";
my_print (my_string);
my_print2 (my_string);
}
my_print (char *string)
{
printf ("The string is %s\n", string);
}
my_print2 (char *string)
{
char *string2;
int size, size2, i;
size = strlen (string);
size2 = size -1;
string2 = (char *) malloc (size + 1);
for (i = 0; i < size; i++)
string2[size2 - i] = string[i];
string2[size] = `\0';
printf ("The string printed backward is %s\n", string2);
}