2.4 实例—使用gdb调试器1.编写实例程序gcctest.c,见2.2小节的开头部分2.编译3.启动GDB,执行程序启动gdb,进入gdb调试环境,可以使用gdb的命令对程序进行调试。
[root@localhost gdbtest txt]# gdb //启动gdbGNU gdb Fedora (6.8-27.el5)Copyright (C) 2008 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later </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 "i386-redhat-linux-gnu".(gdb) run gcctest //在gdb中,运行程序使用r或是run命令,注意,gcctest没有调试信息Starting program: gcctestNo executable file specified.Use the "file" or "exec-file" command. //要使用file或exec-file命令指出要运行的程序(gdb) file gcctest //使用file命令指出要运行的程序gcctest,注意,对gdb命令也可以使用Tab gcctest gcctest.c gcctestg(gdb) file gcctest //使用file命令指出要运行的程序gcctestReading symbols from /root/Desktop/gdbtest txt/gcctest...(no debugging symbols found)...done.(gdb) r //在gdb中,运行程序使用r或是run命令Starting program: /root/Desktop/gdbtest txt/gcctest gcctest(no debugging symbols found)(no debugging symbols found)(no debugging symbols found)hello in mainhello 1hello 2sum=54125560035401396161080590579269632.000000Program exited with code 057.(gdb) file gcctestg //使用file命令指出要运行的程序gcctestgReading symbols from /root/Desktop/gdbtest txt/gcctestg...done.(gdb) r //在gdb中,运行程序使用r或是run命令Starting program: /root/Desktop/gdbtest txt/gcctestg gcctesthello in mainhello 1hello 2sum=54125560035401396161080590579269632.000000Program exited with code 057.(gdb) q //使用q或是quit命令退出gdb[root@localhost gdbtest txt]#4.GDB命令简介support -- Support facilitiestracepoints -- Tracing of program execution without stopping the programuser-defined -- User-defined commandsType "help" followed by a class name for a list of commands in that class.Type "help all" for the list of all commands.Type "help" followed by command name for full documentation.Type "apropos word" to search for commands related to "word".Command name abbreviations are allowed if unambiguous.(gdb) help files //使用help <class>命令查看files类中的命令Specifying and examining files.List of commands:add-shared-symbol-files -- Load the symbols from shared objects in the dynamic linker's link mapadd-symbol-file -- Load symbols from FILEadd-symbol-file-from-memory -- Load the symbols out of memory from a dynamically loaded object filecd -- Set working directory to DIR for debugger and program being debuggedcore-file -- Use FILE as core dump for examining memory and registersdirectory -- Add directory DIR to beginning of search path for source filesedit -- Edit specified file or functionexec-file -- Use FILE as program for getting contents of pure memoryfile -- Use FILE as program to be debuggedforward-search -- Search for regular expression (see regex(3)) from last line listedgenerate-core-file -- Save a core file with the current state of the debugged processlist -- List specified function or lineload -- Dynamically load FILE into the running programnosharedlibrary -- Unload all shared object library symbolspath -- Add directory DIR(s) to beginning of search path for object filespwd -- Print working directoryremote -- Manipulate files on the remote systemremote delete -- Delete a remote fileremote get -- Copy a remote file to the local systemremote put -- Copy a local file to the remote system---Type <return> to continue, or q <return> to quit--- //一屏显示不完,敲回车键显示后面的内容reverse-search -- Search backward for regular expression (see regex(3)) from last line listedsearch -- Search for regular expression (see regex(3)) from last line listedsection -- Change the base address of section SECTION of the exec file to ADDRsharedlibrary -- Load shared object library symbols for files matching REGEXPsymbol-file -- Load symbol table from executable file FILEType "help" followed by command name for full documentation.Type "apropos word" to search for commands related to "word".Command name abbreviations are allowed if unambiguous.(gdb)5.显示源代码(gdb) file gcctestg //使用file命令指出要运行的程序gcctestgLoad new symbol table from "/root/Desktop/gdbtest txt/gcctestg"? (y or n) yReading symbols from /root/Desktop/gdbtest txt/gcctestg...done.(gdb) list //显示当前行后面的源程序1 #include <stdio.h>23 void print_hello1(char *p_str);4 void print_hello2(char *p_str);56 int main(int argc,char **argv)7 {8 double i,sum=0;9 char *pstr="hello in main";10 int arr[]={1,2,3,4,5};(gdb) list //显示当前行后面的源程序11 printf("%s\n",pstr);12 print_hello1("hello 1");13 print_hello2("hello 2");1415 for(i=1; i<=1020000020.01*1020000020.01*10100020.01*10100020.00202; i=i*1.0000016)16 sum=sum/1.0201809902203*1.000102101203*1.00006605+i*10.01016/1.0005;17 printf("sum=%f\n",sum);18 }1920 void print_hello1(char *p_str)(gdb) //敲回车键,继续执行list命令,显示当前行后面的源程序21 {22 printf("%s\n",p_str);23 }2425 void print_hello2(char *p_str)26 {27 printf("%s\n",p_str);28 }(gdb) list 8 //显示程序第8行的周围的源程序3 void print_hello1(char *p_str);4 void print_hello2(char *p_str);56 int main(int argc,char **argv)7 {8 double i,sum=0;9 char *pstr="hello in main";10 int arr[]={1,2,3,4,5};11 printf("%s\n",pstr);12 print_hello1("hello 1");(gdb) list 6,10 //显示程序第6-10行的源程序6 int main(int argc,char **argv)7 {8 double i,sum=0;9 char *pstr="hello in main";10 int arr[]={1,2,3,4,5};(gdb)gdb可以显示调试程序(编译程序时一定要加上-g参数,把源程序信息编译到执行文件中)的源代码。