- 论坛徽章:
- 0
|
gdb使用说明
gdb是一个用来调试 C 和 C++ 程序的强力调试器. 它使你能在程序运行时观察程序的内部结构和内存的使用情况. 以下是 gdb 所提供的一些功能:
它使你能监视你程序中变量的值.
它使你能设置断点以使程序在指定的代码行上停止执行.
它使你能一行行的执行你的代码.
gdb支持很多的命令使你能实现不同的功能. 这些命令从简单的文件装入到允许你检查所调用的堆栈内容的复杂命令, 表27.1列出了你在用 gdb 调试时会用到的一些命令. 想了解 gdb 的详细使用请参考 gdb 的指南页.
基本 gdb 命令.
命 令
描 述
file
装入想要调试的可执行文件.
kill
终止正在调试的程序.
list
列出产生执行文件的源代码的一部分.
next
执行一行源代码但不进入函数内部.
step
执行一行源代码而且进入函数内部.
run
执行当前被调试的程序
quit
终止 gdb
watch
使你能监视一个变量的值而不管它何时被改变.
break
在代码里设置断点, 这将使程序执行到这里时被挂起.
make
使你能不退出 gdb就可以重新产生可执行文件.
shell
使你能不离开 gdb就执行 UNIX shell 命令.
实例:
mytest.cpp
1: #include
2: #include
3:
4: void my_print2(char *string)
5: {
6: char* ptmp;
7: int size, i;
8: size=strlen(string);
9: ptmp=(char *)malloc(size+1);
10:
11: for(i=0; i
12: ptmp[size-i] = string;
13:
14: ptmp[size+1]='';
15: printf ("The string printed backward is %sn", ptmp);
16: }
17:
18: void my_print (char *string)
19: {
20: printf ("The string is %sn", string);
21: }
22:
23: void main(){
24: char my_string[]="Hello";
25: my_print(my_string);
26: my_print2(my_string);
27: }
预期结果:
$mytest
Hello
olleH
事实结果:
$mytest
Hello
单步调试过程:
etl> g++ -g -o mytest mytest.cpp
etl> gdb
GNU gdb 5.3
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or 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.
This GDB was configured as "sparc-sun-solaris2.8".
(gdb) file mytest
Reading symbols from mytest...done.
(gdb) list
15 printf ("The string printed backward is %sn", ptmp);
16 }
17
18 void my_print (char *string)
19 {
20 printf ("The string is %sn", string);
21 }
22
23 void main(){
24 char my_string[]="Hello";
(gdb) break 12
Breakpoint 1 at 0x106d0: file mytest.cpp, line 12.
(gdb) run
Starting program: /opt/etl/shenxj/cpp/mytest
The string is Hello
Breakpoint 1, my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) watch size-i
Hardware watchpoint 2: size - i
(gdb) watch ptmp[size-i]
Hardware watchpoint 3: ptmp[size - i]
(gdb) n
Hardware watchpoint 3: ptmp[size - i]
Old value = 0 ''
New value = 72 'H'
0x000106f8 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 2: size - i
Old value = 5
New value = 4
Hardware watchpoint 3: ptmp[size - i]
Old value = 72 'H'
New value = 0 ''
0x00010704 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Breakpoint 1, my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 3: ptmp[size - i]
Old value = 0 ''
New value = 101 'e'
0x000106f8 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 2: size - i
Old value = 4
New value = 3
Hardware watchpoint 3: ptmp[size - i]
Old value = 101 'e'
New value = 8 'b'
0x00010704 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Breakpoint 1, my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 3: ptmp[size - i]
Old value = 8 'b'
New value = 108 'l'
0x000106f8 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 2: size - i
Old value = 3
New value = 2
Hardware watchpoint 3: ptmp[size - i]
Old value = 108 'l'
New value = 11 'v'
0x00010704 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Breakpoint 1, my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 3: ptmp[size - i]
Old value = 11 'v'
New value = 108 'l'
0x000106f8 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 2: size - i
Old value = 2
New value = 1
Hardware watchpoint 3: ptmp[size - i]
Old value = 108 'l'
New value = 2 '02'
0x00010704 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Breakpoint 1, my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 3: ptmp[size - i]
Old value = 2 '02'
New value = 111 'o'
0x000106f8 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
Hardware watchpoint 2: size - i
Old value = 1
New value = 0
Hardware watchpoint 3: ptmp[size - i]
Old value = 111 'o'
New value = 0 ''
0x00010704 in my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i] = string;
(gdb) n
14 ptmp[size+1]='';
(gdb) n
15 printf ("The string printed backward is %sn", ptmp);
(gdb) n
The string printed backward is
16 }
(gdb) n
Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
Watchpoint 3 deleted because the program has left the block in
which its expression is valid.
0x000107d8 in main () at mytest.cpp:26
26 my_print2(my_string);
(gdb) n
27 }
(gdb) n
0x00010550 in _start ()
(gdb) n
Single stepping until exit from function _start,
which has no line number information.
Program exited normally.
(gdb) start
etl>
诊断结果:
没有值分配给ptmp[0] 了,而它是新串的第一个字符, 因为malloc函数在分配内存时把它们初始化为空(null)字符. 所以 ptmp的第一个字符是空字符. 这解释了为什么在打印 ptmp时没有任何输出了.
现在找出了问题出在哪里, 修正这个错误是很容易的. 你得把代码里写入 ptmp的第一个字符的的偏移量改为size - 1而不是size.
修改后调试结果如下:
etl> g++ -g -o mytest mytest.cpp
etl> gdb
GNU gdb 5.3
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or 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.
This GDB was configured as "sparc-sun-solaris2.8".
(gdb) file mytest
Reading symbols from mytest...done.
(gdb) break 12
Breakpoint 1 at 0x106d0: file mytest.cpp, line 12.
(gdb) run
Starting program: /opt/etl/shenxj/cpp/mytest
The string is Hello
Breakpoint 1, my_print2 (string=0xffbef7e8 "Hello") at mytest.cpp:12
12 ptmp[size-i-1] = string;
(gdb) watch size-i-1
Hardware watchpoint 2: size - i - 1
(gdb) watch ptmp[size-i-1]
Hardware watchpoint 3: ptmp[size - i - 1]
(gdb) n
Hardware watchpoint 3: ptmp[size - i - 1]
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/29024/showart_488688.html |
|