源程序:tst.c 1 #include 2 3 int func(int n) 4 { 5 int sum=0,i; 6 for(i=0; i cc -g tst.c -o tst 使用GDB调试: hchen/test> gdb tst <---------- 启动GDB GNU gdb 5.1.1 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 "i386-suse-linux"... (gdb) l <-------------------- l命令相当于list,从第一行开始例出原码。 1 #include 2 3 int func(int n) 4 { 5 int sum=0,i; 6 for(i=0; i 好了,有了以上的感性认识,还是让我们来系统地认识一下gdb吧。
启动gdb后,就你被带入gdb的调试环境中,就可以使用 gdb的命令开始调试程序了,gdb的命令可以使用help命令来查看,如 下所示: /home/hchen> gdb GNU gdb 5.1.1 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 "i386-suse-linux". (gdb) help List of classes of commands: aliases -- Aliases of other commands breakpoints -- Making program stop at certain points data -- Examining data files -- Specifying and examining files internals -- Maintenance commands obscure -- Obscure features running -- Running the program stack -- Examining the stack status -- Status inquiries support -- Support facilities tracepoints -- Tracing of program execution without stopping the program user-defined -- User-defined commands Type "help" followed by a class name for a list of commands in that class. Type "help" followed by command name for full documentation. Command name abbreviations are allowed if unambiguous. (gdb) gdb 的命令很多,gdb把之分成许多个种类。help命令只是例出gdb的 命令种类,如果要看种类中的命令,可以使用help 命令, 如:help breakpoints,查看设置断点的所有命令。也可以直接help 来查看命令的帮助。 gdb中,输入命令时,可以不用打全命令,只用打命令的前几个字符 就可以了,当然,命令的前几个字符应该要标志着一个唯一的命令, 在Linux下,你可以敲击两次TAB键来补齐命令的全称,如果有重复的 ,那么gdb会把其例出来。 示例一:在进入函数func时,设置一个断点。可以敲入break func,或是