Linux下C程序的编辑,编译和运行以及调试linux
要使用的工具:
编辑:vim(vi)
编译和运行:gcc
调试:gdbredis
安装很简单(如下是以在CentOS中安装为例):
shell
yum vim gcc gdb
1.使用vim编辑源文件
首先,打开终端练下手:
express
vim hello.c
(进入通常模式)
按下"i",进入编辑模式,在编辑模式下输入:
vim
#include <stdio.h> int main(){ printf("Hello, World!\n"); return 0; }
输入完成,按"ESC"键,回到通常模式,而后按下":wq",便可保存并退出vim。数组
附注:
在通常模式下,按下":%!xxd"查看hello.c的16进制形式,回到文本格式按下":%!xxd -r"。
查看hello.c的二进制形式,按下":%!xxd -b",这是hello.c保存在磁盘上的存储状态。
至此,在vim已完成C源文件的编辑。
关于vim的使用,直接上网搜索vim,相关的文章是至关多的;或者参考vim的联机帮助,在命令行上键入"man vim"便可。less
2.编译和运行函数
gcc命令的基本用法:
工具
gcc[options] [filenames]
其中,filenames为文件名;options为编译选项
当不使用任何编译选项编译hello.c时,gcc将会自动编译产生一个a.out的可执行文件:
优化
[root@localhost c]# ls hello.c [root@localhost c]# gcc hello.c [root@localhost c]# ls a.out hello.c
执行:
[root@localhost c]# ./a.out Hello, World!
使用-o编译选择,能够为编译后的文件指定一个名字:
[root@localhost c]# ls a.out hello.c [root@localhost c]# gcc hello.c -o hello [root@localhost c]# ls a.out hello hello.c
执行:
[root@localhost c]# ./hello Hello, World!
注意:使用-o选项时,-o后面必须跟一个文件名,即:-o outfile。
为了便于描述后面的选项,删除hello和a.out可执行文件。
结合介绍gcc的编译选项,分析hello.c的编译和执行过程:
(1)预处理阶段:使用-E选项,对输入文件只作预处理不编译。当使用这个选项时,预处理器的输出被送到标准输出而不是存储到文件。若是想将预处理的输出存储到文件,可结合-o选项使用,使用以下:
[root@localhost c]# ls hello.c [root@localhost c]# gcc -E hello.c -o hello.i [root@localhost c]# ls hello.c hello.i
使用less查看下hello.i:
[root@localhost c]# less hello.i
(2)编译阶段:使用-S选项,将C程序编译为汇编语言文件后中止编译,gcc编译产生汇编文件的默认后缀为.s。
[root@localhost c]# ls hello.c hello.i [root@localhost c]# gcc -S hello.c [root@localhost c]# ls hello.c hello.i hello.s
在gcc -S hello.c处,使用C源文件编译,也能够用gcc -S hello.i的预处理文件编译,结果同样。
使用-S编译时,也能够和-o结合使用指定编译产生的汇编语言文件的名字:
[root@localhost c]# ls hello.c hello.i hello.s [root@localhost c]# gcc -S hello.i -o hello_s.s [root@localhost c]# ls hello.c hello.i hello.s hello_s.s
可以使用less命令查看汇编代码。
(3)汇编阶段:使用-c选项,将C源文件或者汇编语言文件编译成可重定向的目标文件(二进制形式),其默认后缀为.o。
[root@localhost c]# ls hello.c hello.i hello.s hello_s.s [root@localhost c]# gcc -c hello.s [root@localhost c]# ls hello.c hello.i hello.o hello.s hello_s.s
也能够和-o结合使用指定编译产生的目标文件的名字:
[root@localhost c]# gcc -c hello.s -o hello.o
因为hello.o是二进制文件,使用less查看显示为乱码;
而后使用vim hello.o打开也显示为乱码,按下":%!xxd"查看其16进制形式,按下":%!xxd -r"退出 16进制查看模式,回到乱码状态。在退出vim时,若提示已经修改了文件,则使用":q!"强制退出。
(4)连接阶段:连接器将可重定向的目标文件hello.o以及库文件(如printf.o)执行并入操做,造成最终可执行的可执行目标文件。
[root@localhost c]# ls hello.c hello.i hello.o hello.s hello_s.s [root@localhost c]# gcc hello.o [root@localhost c]# ls a.out hello.c hello.i hello.o hello.s hello_s.s
可以使用-o选项,指定输出文件(便可执行目标文件)的名字:
[root@localhost c]# gcc hello.o -o hello [root@localhost c]# ls a.out hello hello.c hello.i hello.o hello.s hello_s.s
(5)执行阶段:
[root@localhost c]# ./a.out Hello, World! [root@localhost c]# ./hello Hello, World!
由此,看出前面使用的gcc hello.c -o hello命令,将hello.c直接编译为可执行的目标文件,中间通过于处理器的预处理阶段(源文件到预处理文件),编译器的编译阶段(预处理文件到汇编文件),汇编器的汇编阶段(汇编文件到可重定向的目标文件),连接器的连接阶段(可重定向的目标文件到可执行的目标文件)。
还有其余的选项以下:
-Idir:dir是头文件所在的目录
-Ldir:dir是库文件所在的目录
-Wall:打印全部的警告信息
-Wl,options:options是传递给连接器的选项
编译优化选项:-O和-O2
-O选项告诉GCC 对源代码进行基本优化。这些优化在大多数状况下都会使程序执行的更快。-O2选项告诉GCC产生尽量小和尽量快的代码。
-O2选项将使编译的速度比使用-O时慢。但一般产生的代码执行速度会更快。
除了-O和-O2优化选项外,还有一些低级选项用于产生更快的代码。这些选项很是的特殊,并且最好只有当你彻底理解这些选项将会对编译后的代码产生什么样的效果时再去使用。这些选项的详细描述,请参考GCC的联机帮助,在命令行上键入"man gcc"便可。
调试选项:-g(使用详情见第3部分)
-g选项告诉GCC产生能被GNU调试器使用的调试信息以便调试你的程序。
即:在生成的目标文件中添加调试信息,所谓调试信息就是源代码和指令之间的对应关系,在gdb调试和objdump反汇编时要用到这些信息。
3.调试
虽然GCC提供了调试选项,可是自己不能用于调试。Linux 提供了一个名为gdb的GNU调试程序。gdb是一个用来调试C和C++程序的调试器。它使你能在程序运行时观察程序的内部结构和内存的使用状况。如下是gdb所提供的一些功能:
a.它使你能监视你程序中变量的值;
b.它使你能设置断点以使程序在指定的代码行上中止执行;
c.它使你能一行行的执行你的代码。
(1)启动gdb
在命令行上键入"gdb"并按回车键就能够运行gdb了,以下:
[root@localhost c]# gdb GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1) Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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 "x86_64-redhat-linux-gnu". For bug reporting instructions, please see:<>. (gdb)
当启动gdb以后,便可在命令行上输入命令进行相关的调试操做。
也能够如下面的方式来启动gdb:
[root@localhost c]# gdb hello
这种方式启动gdb,直接将指定调试的程序文件装载到调试环境中。也就是让gdb装入名称为filename的可执行文件,从而准备调试。
为了可以进行调试,当前调试的程序文件中必须包含调试信息。其中调试信息包含程序中的每一个变量的类型和其在可执行文件里的地址映射以及源代码的行号,gdb利用这些信息使源代码和机器码相关联。所以在使用gcc编译源程序的时候必须使用-g选项,以便将调试信息包含在可执行文件中。
例如:
[root@localhost c]# gcc -g hello.c -o hello
gdb还提供了其余的启动选项,请参考gdb的联机帮助。在命令行上键入"man gdb"并回车便可。
(2)gdb基本命令
<1>单步执行和跟踪函数调用
程序编辑以下:
#include <stdio.h> int add_range(int low, int high){ int i; int sum; for(i = low; i <= high; i++){ sum = sum + i; } return sum; } int main(){ int result[100]; result[0] = add_range(1, 10); result[1] = add_range(1, 100); printf("result[0] = %d\nresult[1] = %d\n", result[0], result[1]); return 0; }
编译和运行以下:
[root@localhost gdb_demo]# vim test1.c [root@localhost gdb_demo]# gcc test1.c -o test1 [root@localhost gdb_demo]# ls test1 test1.c [root@localhost gdb_demo]# ./test1 result[0] = 55 result[1] = 5105
以上程序的结果中,显然第二个结果是不正确的,有基础的人会一眼看出问题处在哪里,呵呵,这里只是为了演示使用gdb调试而故意为之。固然在开发人员最好不要太过于依赖gdb才能找到错误。
在编译时加上-g选项,生成的目标文件才能用gdb进行调试:
[root@localhost gdb_demo]# gcc test1.c -g -o test1 [root@localhost gdb_demo]# gdb test1 GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1) Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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 "x86_64-redhat-linux-gnu". For bug reporting instructions, please see:<>... Reading symbols from /root/code/c/gdb_demo/test1...done. (gdb)
-g选项的做用是在目标文件中加入源代码的信息,好比目标文件中的第几条机器指令对应源代码的第几行,但并非把整个源文件嵌入到目标文件中,因此在调试时目标文件必须保证gdb也能找到源文件。
gdb提供一个类是shell的命令行环境,上面的(gdb)就是提示符,在这个提示符下输入help能够查看命令的类别:
(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 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.
能够进一步查看某一个类别中有哪些命令,例如查看files类别下有哪些命令能够用:
(gdb) help files Specifying and examining files. List of commands: add-symbol-file -- Load symbols from FILE add-symbol-file-from-memory -- Load the symbols out of memory from a dynamically loaded object file cd -- Set working directory to DIR for debugger and program being debugged core-file -- Use FILE as core dump for examining memory and registers directory -- Add directory DIR to beginning of search path for source files edit -- Edit specified file or function exec-file -- Use FILE as program for getting contents of pure memory file -- Use FILE as program to be debugged forward-search -- Search for regular expression (see regex(3)) from last line listed generate-core-file -- Save a core file with the current state of the debugged process list -- List specified function or line load -- Dynamically load FILE into the running program
使用list命令从第一行开始列出源代码:
(gdb) list 1 1 #include <stdio.h> 2 3 int add_range(int low, int high){ 4 int i; 5 int sum; 6 for(i = low; i <= high; i++){ 7 sum = sum + i; 8 } 9 return sum; 10 } (gdb)
一次只列出10行,若是要从11行开始继续列出源代码能够输入:
(gdb) list
也能够什么都不输入直接敲回车,gdb提供类一个方便的功能,在提示符下直接敲回车表示用适当的参数重复上一条命令。
(gdb) (直接回车) 11 12 int main(){ 13 int result[100]; 14 result[0] = add_range(1, 10); 15 result[1] = add_range(1, 100); 16 printf("result[0] = %d\nresult[1] = %d\n", result[0], result[1]); 17 return 0; 18 }
gdb的不少经常使用命令有简写形式,例如list命令能够写成l,要列出一个函数的源码也能够用函数名作list的参数:
(gdb) l add_range 1 #include <stdio.h> 2 3 int add_range(int low, int high){ 4 int i; 5 int sum; 6 for(i = low; i <= high; i++){ 7 sum = sum + i; 8 } 9 return sum; 10 }
如今退出gdb的环境(quit或简写形式q):
(gdb) quit
如今把源代码更名或移动到别处,再用gdb调试目标文件,就列不出源代码了:
[root@localhost gdb_demo]# ls test1 test1.c [root@localhost gdb_demo]# mv test1.c test.c [root@localhost gdb_demo]# ls test1 test.c [root@localhost gdb_demo]# gdb test1 ...... (gdb) l 5 test1.c: 没有那个文件或目录. in test1.c (gdb)
可见gcc的-g选项并非把源代码嵌入到目标文件中的,在调试目标文件时也须要源文件。
如今把源代码恢复原样,继续调试。首先使用start命令开始执行程序:
[root@localhost gdb_demo]# mv test.c test1.c [root@localhost gdb_demo]# gdb test1 ...... (gdb) start Temporary breakpoint 1 at 0x4004f8: file test1.c, line 14. Starting program: /root/code/c/gdb_demo/test1 Temporary breakpoint 1, main () at test1.c:14 14 result[0] = add_range(1, 10); Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 (gdb)
这表示停在main函数中变量定义以后的第一条语句处等待咱们发命令,gdb列出这条语句表示它还没执行,而且立刻要执行。咱们能够用next命令(简写为n)控制这些语句一条一条地执行:
(gdb) n 15 result[1] = add_range(1, 100); (gdb) (直接回车) 16 printf("result[0] = %d\nresult[1] = %d\n", result[0], result[1]); (gdb) (直接回车) result[0] = 55 result[1] = 5105 17 return 0;
用n命令依次执行两行赋值语句和一行打印语句,在执行打印语句时结果马上打印出来类,而后停在return语句以前等待咱们发命令。
虽然咱们彻底控制了程序的执行,但仍然看不出哪里错了,由于错误再也不main函数中而是在add_range函数中,如今用start命令从新执行,此次用step命令(简写为s)进入函数中去执行:
(gdb) start The program being debugged has been started already. Start it from the beginning? (y or n) y Temporary breakpoint 3 at 0x4004f8: file test1.c, line 14. Starting program: /root/code/c/gdb_demo/test1 Temporary breakpoint 3, main () at test1.c:14 14 result[0] = add_range(1, 10); (gdb) s add_range (low=1, high=10) at test1.c:6 6 for(i = low; i <= high; i++){
此次进入add_range函数中,停在函数中定义变量以后的第一条语句处。
在函数中有几种查看状态的办法,backtrace(简写为bt)能够查看函数调用的栈帧:
(gdb) bt #0 add_range (low=1, high=10) at test1.c:6 #1 0x0000000000400507 in main () at test1.c:14
可见当前的add_range函数是被main函数调用的,main函数中传给add_range的参数是low=1, high=10。main函数的栈帧编号是1,add_range函数的栈帧编号是0。
如今可使用info命令(简写为i)查看add_range局部变量的值:
(gdb) i locals i = 0 sum = 0
若是想查看main函数中的当前局部变量的值也能够作到的,先用frame命令(简写为f)选择1号栈帧,而后再查看main中的局部变量:
(gdb) f 1 #1 0x0000000000400507 in main () at test1.c:14 14 result[0] = add_range(1, 10); (gdb) i locals result = {4195073, 0, -1646196904, 50, 4195016, 0, 0, 1, 2041, 1, -1654612390, 50, 0, 0, -1652419360, 50, -7728, 32767, -7688, 32767, -1652420216, 50, -134227048, 32767, -163754450, 0, -1654612390, 50, 0, 0, -134227048, 32767, 1, 0, 0, 0, 1, 50, -1652420216, 50, -7848, 32767, 750006344, 0, 6, 0, -7826, 32767, 0, 0, -1652419360, 50, -7808, 32767, -1645672825, 50, -7784, 32767, 0, 1, 0, 0, 4195073, 0, 191, 0, -7826, 32767, -7825, 32767, 1, 0, 0, 0, -1645672168, 50, 0, 0, 4195680, 0, 0, 0, 4195235, 0, -7512, 32767, 4195749, 0, -1646199904, 50, 4195680, 0, 0, 0, 4195296, 0, -7536, 32767, 0, 0}
注意到result数组中的不少元素具备杂乱无章的值,由于未经初始化的局部变量具备不肯定的值。
到目前为止(即已经进入第一次的函数调用的函数体内),是正常的。
继续用s或n往下走,而后用print(简写为p)打印变量sum的值。
(gdb) s 7 sum = sum + i; (gdb) 6 for(i = low; i <= high; i++){ (gdb) 7 sum = sum + i; (gdb) 6 for(i = low; i <= high; i++){ (gdb) p sum $1 = 3
注意:这里的$1表示gdb保存着这些中间结果,$后面的编号会自动增加,在命令中能够用$一、$二、$3等编号代替相应的值。
-----------------------------------------------------
以上的执行过程使用下面的方法可能看得更清楚(这里的步骤不是继续跟着上面步骤,是在另外一个终端中调试的):
(gdb) f 0 #0 add_range (low=1, high=10) at test1.c:7 7 sum = sum + i; (gdb) i locals i = 1 sum = 0 (gdb) s 6 for(i = low; i <= high; i++){ (gdb) i locals i = 1 sum = 1 (gdb) s 7 sum = sum + i; (gdb) i locals i = 2 sum = 1 (gdb) s 6 for(i = low; i <= high; i++){ (gdb) i locals i = 2 sum = 3
-----------------------------------------------------
由此看出,第一次循环的结果是正确的,再往下单步调试已经没有意义了,可使用finish命令让程序一直运行到从当前函数返回为止:
(gdb) finish Run till exit from #0 add_range (low=1, high=10) at test1.c:6 0x0000000000400507 in main () at test1.c:14 14 result[0] = add_range(1, 10); Value returned is $1 = 55
返回值是55,当前正准备执行赋值操做,用s命令执行赋值操做,而后查看result数组:
(gdb) s 15 result[1] = add_range(1, 100); (gdb) print result $2 = {55, 0, -1646196904, 50, 4195016, 0, 0, 1, 2041, 1, -1654612390, 50, 0, 0, -1652419360, 50, -7728, 32767, -7688, 32767, -1652420216, 50, -134227048, 32767, -163754450, 0, -1654612390, 50, 0, 0, -134227048, 32767, 1, 0, 0, 0, 1, 50, -1652420216, 50, -7848, 32767, 750006344, 0, 6, 0, -7826, 32767, 0, 0, -1652419360, 50, -7808, 32767, -1645672825, 50, -7784, 32767, 0, 1, 0, 0, 4195073, 0, 191, 0, -7826, 32767, -7825, 32767, 1, 0, 0, 0, -1645672168, 50, 0, 0, 4195680, 0, 0, 0, 4195235, 0, -7512, 32767, 4195749, 0, -1646199904, 50, 4195680, 0, 0, 0, 4195296, 0, -7536, 32767, 0, 0}
第一个值是55确实赋值给类result数组的第0个元素。
使用s命令进入第二次add_range调用,进入以后首先查看参数和局部变量:
(gdb) s add_range (low=1, high=100) at test1.c:6 6 for(i = low; i <= high; i++){ (gdb) bt #0 add_range (low=1, high=100) at test1.c:6 #1 0x000000000040051c in main () at test1.c:15 (gdb) i locals i = 11 sum = 55
到这里,看出了问题:因为局部变量i和sum没有初始化,因此具备不肯定的值,又因为连次调用是连续的,i和sum正好取类上次调用时的值。i的初始值不是0没关系,由于在for循环开始从新赋值了,但若是sum的处置不是0,累加获得的结果就错了。
问题找到了,能够退出gdb修改源代码了。然而咱们不想浪费一次调试机会,能够在gdb中立刻把sum的初始值改成0,继续运行,看看改了以后有没有其余的bug:
(gdb) set var sum=0 (gdb) finish Run till exit from #0 add_range (low=1, high=100) at test1.c:6 0x000000000040051c in main () at test1.c:15 15 result[1] = add_range(1, 100); Value returned is $3 = 5050 (gdb) s 16 printf("result[0] = %d\nresult[1] = %d\n", result[0], result[1]); (gdb) s result[0] = 55 result[1] = 5050 17 return 0;
这样结果就对了。
修改变量的值除了用set命令以外也可使用print命令,由于print命令后跟的是表达式,而咱们知道赋值和函数调用都是表达式,因此还能够用print来修改变量的值,或者调用函数:
(gdb) print result[2]=88 $4 = 88 (gdb) p printf("result[2]=%d\n", result[2]) result[2]=88 $5 = 13
咱们知道:printf函数的返回值表示实际打印的字符数,因此$5的结果是13。
总结一下本节使用过的gdb命令:
list(l):列出源代码,接着上次的位置往下列,每次列10行 list 行号:列出产品从第几行开始的源代码 list 函数名:列出某个函数的源代码 start:开始执行程序,停在main函数第一行语句前面等待命令 next(n):执行下一列语句 step(s):执行下一行语句,若是有函数调用则进入到函数中 breaktrace(或bt):查看各级函数调用及参数 frame(f) 帧编号:选择栈帧 info(i) locals:查看当前栈帧局部变量的值 finish:执行到当前函数返回,而后挺下来等待命令 print(p):打印表达式的值,经过表达式能够修改变量的值或者调用函数 set var:修改变量的值 quit:退出gdb
<2>断点
程序编辑以下:
# include <stdio.h> int main(){ int sum =0; int i = 0; char input[5]; while(1){ scanf("%s", input);//在输入字符后自动加'\0'造成字符串 for(i = 0; input[i] != '\0'; i++){ sum = sum * 10 + input[i] - '0';//'1'-'0'=1,'\0'=0 } printf("input = %d\n", sum); } return 0; }
编译和运行:
[root@localhost gdb_demo]# vim test2.c [root@localhost gdb_demo]# gcc test2.c -g -o test2 [root@localhost gdb_demo]# ls test1 test1.c test2 test2.c [root@localhost gdb_demo]# ./test2 123 input = 123 12345 input = 12345 12345678 input = -268647318 (Ctrl-C退出程序)
从结果看出程序明显是有问题的。
下面来调试:
[root@localhost gdb_demo]# gdb test2 ...... (gdb) start Temporary breakpoint 1 at 0x40053c: file test2.c, line 4. Starting program: /root/code/c/gdb_demo/test2 Temporary breakpoint 1, main () at test2.c:4 4 int sum =0; Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 (gdb)
可见,start不会跳过赋值语句,经过第一次单步调试的例子,sum可列为重点观察对象,可使用display命令使得每次停下来的时候都显示当前的sum值,继续往下走:
(gdb) display sum 1: sum = 0 (gdb) n 5 int i = 0; 1: sum = 0 (gdb) 9 scanf("%s", input); 1: sum = 0 (gdb) 123 10 for(i = 0; input[i] != '\0'; i++){ 1: sum = 0 (gdb)
这个循环应该是没有问题的,由于循环开始sum的值是正确的。可使用undisplay取消先前设置的那些变量的跟踪。
若是不想一步一步跟踪这个循环,可使用break(简写为b)在第8行设置一个断点(Breakpoint):
(gdb) l 5 int i = 0; 6 char input[5]; 7 8 while(1){ 9 scanf("%s", input); 10 for(i = 0; input[i] != '\0'; i++){ 11 sum = sum * 10 + input[i] - '0'; 12 } 13 printf("input = %d\n", sum); 14 } (gdb) b 8 Breakpoint 2 at 0x40054a: file test2.c, line 8.
break命令的参数也能够是函数名,表示在某一个函数开头设置断点。如今用continue命令(简写为c)连续运行而非单步运行,程序到达断点会自动停下来,这样就能够停在下一次循环的开头:
(gdb) c Continuing. input = 123 Breakpoint 2, main () at test2.c:9 9 scanf("%s", input); 1: sum = 123
而后输入新的字符串准备转换:
(gdb) n 12345 10 for(i = 0; input[i] != '\0'; i++){ 1: sum = 123
此时问题已经暴露出来了,新的转换应该是从0开始累加的,而如今sum倒是123,缘由在于新的循环没有把sum归零。
可见断点有助于快速跳过与问题无关的代码,而后在有问题的代码上慢慢走慢慢分析,“断点加单步”是使用调试器的基本方法。至于应该在哪里设置断点,怎么知道哪些代码能够跳过而哪些代码要慢慢走,也要经过对错误现象的分析和假设来肯定。
一次调试能够设置多个断点,用info命令(简写为i)能够查看已经设置的断点:
(gdb) b 11 Breakpoint 3 at 0x40056c: file test2.c, line 11. (gdb) i breakpoints Num Type Disp Enb Address What 2 breakpoint keep y 0x000000000040054a in main at test2.c:8 breakpoint already hit 2 times 3 breakpoint keep y 0x000000000040056c in main at test2.c:11
每个断点都有一个编号,能够用编号指定删除某个断点:
(gdb) delete breakpoints 2 (gdb) i breakpoints Num Type Disp Enb Address What 3 breakpoint keep y 0x000000000040056c in main at test2.c:11
有时候一个断点不想用能够禁用而没必要删除,这样之后想用的时候能够直接启用,而没必要从新从代码里找应该在哪一行设置断点:
(gdb) disable breakpoints 3 (gdb) i breakpoints Num Type Disp Enb Address What 3 breakpoint keep n 0x000000000040056c in main at test2.c:11 (gdb) enable breakpoints 3 (gdb) i breakpoints Num Type Disp Enb Address What 3 breakpoint keep y 0x000000000040056c in main at test2.c:11 (gdb) delete breakpoints Delete all breakpoints? (y or n) y (gdb) i breakpoints No breakpoints or watchpoints.
gdb设置断点功能很是灵活,还能够设置断点在知足某个条件时才激活,例如咱们仍然在循环开头设置断点,可是仅当sum不等于0时才中断,而后用run(简写为r)从新从程序开头连续执行:
(gdb) break 10 if sum != 0 Breakpoint 5 at 0x400563: file test2.c, line 10. (gdb) i breakpoints Num Type Disp Enb Address What 5 breakpoint keep y 0x0000000000400563 in main at test2.c:10 stop only if sum != 0 (gdb) r The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /root/code/c/gdb_demo/test2 123 input = 123 123 Breakpoint 5, main () at test2.c:10 10 for(i = 0; input[i] != '\0'; i++){ 1: sum = 123
结果:第一次执行输入以后没有中断,第二次却中断了,由于第二次循环开始sum的值为123。
总结一下本节使用到的gdb命令:
break(b) 行号:在某一行设置断点 break 函数名:在某个函数开头设置断点 break...if...:设置条件断点 continue(或c):从当前位置开始连续而非单步执行程序 delete breakpoints:删除全部断点 delete breakpoints n:删除序号为n的断点 disable breakpoints:禁用断点 enable breakpoints:启用断点 info(或i) breakpoints:参看当前设置了哪些断点 run(或r):从开始连续而非单步执行程序 display 变量名:跟踪查看一个变量,每次停下来都显示它的值 undisplay:取消对先前设置的那些变量的跟踪
下面再看一个例子:
程序以下:
#include <stdio.h> int main(){ int i; char str[6] = "hello"; char reverse_str[6] = ""; printf("%s\n", str); for(i = 0; i < 5; i ++){ reverse_str[5-i] = str[i]; } printf("%s\n", reverse_str); return 0; }
运行结果:
[root@localhost gdb_demo]# gcc test3.c -g -o test3 [root@localhost gdb_demo]# ./test3 hello
其中,第二次打印空白,结果显然是不正确的。
调试过程以下:
[root@localhost gdb_demo]# gdb test3 ...... (gdb) l 1 #include <stdio.h> 2 3 int main(){ 4 int i; 5 char str[6] = "hello"; 6 char reverse_str[6] = ""; 7 8 printf("%s\n", str); 9 for(i = 0; i < 5; i ++){ 10 reverse_str[5-i] = str[i]; (gdb) 11 } 12 printf("%s\n", reverse_str); 13 return 0; 14 } (gdb) i breakpoints No breakpoints or watchpoints. (gdb) b 10 Breakpoint 1 at 0x4004fb: file test3.c, line 10. (gdb) i breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x00000000004004fb in main at test3.c:10 (gdb) r Starting program: /root/code/c/gdb_demo/test3 hello Breakpoint 1, main () at test3.c:10 10 reverse_str[5-i] = str[i]; Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 (gdb) p reverse_str $1 = "\000\000\000\000\000" (gdb) c Continuing. Breakpoint 1, main () at test3.c:10 10 reverse_str[5-i] = str[i]; (gdb) p reverse_str $2 = "\000\000\000\000\000h" (gdb) c Continuing. Breakpoint 1, main () at test3.c:10 10 reverse_str[5-i] = str[i]; (gdb) p reverse_str $3 = "\000\000\000\000eh" (gdb) c Continuing. Breakpoint 1, main () at test3.c:10 10 reverse_str[5-i] = str[i]; (gdb) p reverse_str $4 = "\000\000\000leh" (gdb) c Continuing. Breakpoint 1, main () at test3.c:10 10 reverse_str[5-i] = str[i]; (gdb) p reverse_str $5 = "\000\000lleh" (gdb) c Continuing. Program exited normally. (gdb)
由上面的观察可知:
在于将str数组中的值赋值到reverse_str的时候,将str的第1个元素赋值给类reverse_str的第6个元素,而该循环只循环了5次(即str数组元素个数),从而致使reverse_str的第一个元素为'\000',因此输出为空白。
修改以下:
#include <stdio.h> #include <string.h> int main(){ int i; char str[6] = "hello"; char reverse_str[6] = ""; printf("%s\n", str); int len = strlen(str); for(i = 0; i <= len-1; i ++){ reverse_str[len-1-i] = str[i]; } printf("%s\n", reverse_str); return 0; }
再次运行就行了:
[root@localhost gdb_demo]# gcc test3.c -o test3 [root@localhost gdb_demo]# ./test3 hello olleh
<3>观察点(Watchpoint)
断点是当程序执行到某一代码行时中断,而观察点通常来观察某个表达式(变量也是一种表达式)的值是否有变化了,若是有变化,立刻停住程序。
vim test4.c
# include <stdio.h> int main(){ int sum =0; int i; for(i = 1; i <= 10; i++){ sum = sum +i; } printf("sum = %d\n", sum); return 0; }
编译运行:
[root@localhost gdb_demo]# gcc test4.c -g -o test4 [root@localhost gdb_demo]# ./test4 sum = 55
设置观察点进行调试:
[root@localhost gdb_demo]# gdb test4 ...... (gdb) start Temporary breakpoint 1 at 0x4004cc: file test4.c, line 4. Starting program: /root/code/c/gdb_demo/test4 Temporary breakpoint 1, main () at test4.c:4 4 int sum =0; Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 (gdb) l 1 # include <stdio.h> 2 3 int main(){ 4 int sum =0; 5 int i; 6 for(i = 1; i <= 10; i++){ 7 sum = sum +i; 8 } 9 printf("sum = %d\n", sum); 10 return 0; (gdb) 11 } (gdb) watch sum Hardware watchpoint 2: sum (gdb) c Continuing. Hardware watchpoint 2: sum Old value = 0 New value = 1 main () at test4.c:6 6 for(i = 1; i <= 10; i++){ (gdb) c Continuing. Hardware watchpoint 2: sum Old value = 1 New value = 3 main () at test4.c:6 6 for(i = 1; i <= 10; i++){ (gdb) c Continuing. Hardware watchpoint 2: sum Old value = 3 New value = 6 main () at test4.c:6 6 for(i = 1; i <= 10; i++){ (gdb) c Continuing. Hardware watchpoint 2: sum Old value = 6 New value = 10 main () at test4.c:6 6 for(i = 1; i <= 10; i++){ (gdb)
总结一下本节使用到的gdb命令:
watch:设置观察点 info(或i) watchpoints:查看当前设置了哪些观察点
GDB的补充:
输出格式:
通常来讲,GDB会根据变量的类型输出变量的值。但你也能够自定义GDB的输出的格式。
例如,你想输出一个整数的十六进制,或是二进制来查看这个整型变量的中的位的状况。要
作到这样,你可使用GDB的数据显示格式:
x 按十六进制格式显示变量。
d 按十进制格式显示变量。
u 按十六进制格式显示无符号整型。
o 按八进制格式显示变量。
t 按二进制格式显示变量。
a 按十六进制格式显示变量。
c 按字符格式显示变量。
f 按浮点数格式显示变量。
(gdb) p sum $1 = 10 (gdb) p/a sum $2 = 0xa (gdb) p/x sum $3 = 0xa (gdb) p/o sum $4 = 012 (gdb) p/t sum $5 = 1010 (gdb) p/f sum $6 = 1.40129846e-44 (gdb) p/c sum $7 = 10 '\n'
查看内存:你可使用examine命令(简写是x)来查看内存地址中的值。x命令的语法以下所示: x/ n、f、u是可选的参数。 n 是一个正整数,表示显示内存的长度,也就是说从当前地址向后显示几个地址的内容。 f 表示显示的格式,参见上面。若是地址所指的是字符串,那么格式能够是s,若是地十是指令地址,那么格式能够是i。u 表示从当前地址日后请求的字节数,若是不指定的话,GDB默认是4个bytes。u参数能够用下面的字符来代替,b表示单字节,h表示双字节,w表示四字节,g表示八字节。当咱们指定了字节长度后,GDB会从指内存定的内存地址开始,读写指定字节,并把其看成一个值取出来。 表示一个内存地址。 n/f/u三个参数能够一块儿使用。例如: 命令:x/3uh 0x54320 表示,从内存地址0x54320读取内容,h表示以双字节为一个单位,3表示三个单位,u表示按十六进制显示。