linux gdb 简单使用

来源:互联网 发布:外文期刊数据库检索 编辑:程序博客网 时间:2024/05/18 21:11

网上找一个别人的例子 源码如下:

//此程序仅作为“GDB十分钟教程”的示例代码, by liigo//Email: liigo@sina.com//blog: http://blog.csdn.net/liigo//WebSite: www.liigo.com #include <stdio.h>int nGlobalVar = 0;int tempFunction(int a, int b){    printf("tempFunction is called, a = %d, b = %d \n", a, b);    return (a + b);}int main(int argc,char **argv){    int n;        n = 1;        n++;        n--;       printf("argv[1] is =--- %s \n", argv[1]);       printf("argv[2] is ==== %s \n", argv[2]);        nGlobalVar += 100;        nGlobalVar -= 12;    printf("n = %d, nGlobalVar = %d \n", n, nGlobalVar);        n = tempFunction(1, 2);    printf("n = %d \n", n);    return 0;}

请将此代码复制出来并保存到文件 gdb-sample.c 中,然后切换到此文件所在目录,用GCC编译之:

gcc gdb-sample.c -o gdb-sample -g

使用参数 -g 表示将源代码信息编译到可执行文件中。如果不使用参数 -g,会给后面的GDB调试造成不便。当然,如果我们没有程序的源代码,自然也无从使用 -g 参数,调试/跟踪时也只能是汇编代码级别的调试/跟踪。

下面就是编译时候不添加 -g参数后的调试信息。我们发现没有函数名 没有变量值全是内存地址

<http://bugs.launchpad.net/gdb-linaro/>...Reading symbols from /home/biaolv/dsp/gdb-sample...(no debugging symbols found)...done.(gdb) listNo symbol table is loaded.  Use the "file" command.(gdb) b main Breakpoint 1 at 0x40052b(gdb) rStarting program: /home/biaolv/dsp/gdb-sample Breakpoint 1, 0x000000000040052b in main ()(gdb) sSingle stepping until exit from function main,which has no line number information.n = 1, nGlobalVar = 88 tempFunction is called, a = 1, b = 2 n = 3 0x00007ffff7a3b76d in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6(gdb) 

单步执行:step(s)、next(n),仅执行到代码的下一行后再次暂停。
注意二者区别:在函数调用时step会进入函数,next导致下一次暂停出现在调用函数之后。next被称为单步越过(stepping over)函数,而step被称为单步进入(stepping into)函数。
next和step都可以采用一个可选的数值参数,来表示要使用next或step执行的额外行数。

利用 set args 命令就可以修改发送给程序的参数, 而使用 show args 命令就可以查看其缺省参
数的列表。

(gdb) set args 10 11(gdb) rStarting program: /home/biaolv/dsp/gdb-sample 10 11argv[1] is =--- 10 argv[2] is ==== 11 n = 1, nGlobalVar = 88 tempFunction is called, a = 1, b = 2 tempFunction i111======================== n = 6 [Inferior 1 (process 26063) exited normally](gdb) q

发现我们set args 的参数在程序中被完整打印出来

使用 print 命令可以检查各个变量的值

(gdb) print n $6 = 1

也可以打印 函数的局部变量值。注意此时局部变量的栈应该存在。若不存在。就没有意义,意思就是在函数内部查看局部变量值。不能函数运行了再查看 函数变量值

新建文件 test.h

#include <stdio.h>int tempFunction(int a, int b);int pp(int a, int b);

test.c

#include <stdio.h>int g = 4;int tempFunction(int a, int b){    a= a + g;    b= b+2;    return (a + b);}int pp(int a, int b){    a= a + g;    b= b+2;    return (a + b);}

gdb-sample.c内容

#include <stdio.h>#include "test.h"int nGlobalVar = 0;int main( ){    int n;        n = 1;        n++;        n--;//       printf("argv[1] is =--- %s \n", argv[1]);  //     printf("argv[2] is ==== %s \n", argv[2]);        nGlobalVar += 100;        nGlobalVar -= 12;        int c = pp(7,8);        n = tempFunction(1, 2);    printf("n = %d pp is %d \n", n,c);    return 0;}

编译test静态库
gcc -o test.o -c test.c -g 记得加 -g 不然gdb调试的时候会发现函数内部的代码看不到。test.c文件变量也找不到
将 test.o打包成静态库
ar rcs libtest.a test.o
编译 gdb-sample程序
gcc -o gdb-sample gdb-sample.c libtest.a -g

下面是打印 文件 test.c文件中 全局变量值

eading symbols from /home/biaolv/dsp/gdb-sample...done.(gdb) b ppBreakpoint 1 at 0x40059c: file test.c, line 19.(gdb) rStarting program: /home/biaolv/dsp/gdb-sample Breakpoint 1, pp (a=7, b=8) at test.c:1919          a= a + g;(gdb) s20          b= b+2;(gdb) p 'test.c'::g$1 = 4(gdb) p 'gdb-sample.c'::nGlobalVar$2 = 88

打印 函数的局部变量值

Breakpoint 1, pp (a=7, b=8) at test.c:1919          a= a + g;(gdb) n20          b= b+2;(gdb) p pp:aA syntax error in expression, near `:a'.(gdb) p pp::a11
 p tempFunction::a$5 = 1

还可以 调用函数 ,打印返回结果

注意 : 比如设置断点在 b pp函数的时候,此时 p pp(5,6)将不能被调用(断点函数不能被pringt)。但是可以调用其他的。

Breakpoint 1, tempFunction (a=1, b=2) at test.c:1212          a= a + g;(gdb) p pp(3,4)$1 = 13(gdb) p tempFunction(3,4)Breakpoint 1, tempFunction (a=3, b=4) at test.c:1212          a= a + g;

breakpoint 在程序中设置一个断点 ,可以按照 linenumber 或者 函数名设置
比如:

b pp 就是在 pp函数入口处停止, 也就是这行函数 a= a + g;b  27 就是在程序的27地址停止

设置好之后。可以 run 命令运行到 pp 函数 。之后continue 运行到下一个断点

(gdb) b pp Breakpoint 3 at 0x4005a0: file test.c, line 19.(gdb) b 27Breakpoint 4 at 0x400567: file gdb-sample.c, line 27.(gdb) rThe program being debugged has been started already.Start it from the beginning? (y or n) yStarting program: /home/biaolv/dsp/gdb-sample Breakpoint 3, pp (a=7, b=8) at test.c:1919          a= a + g;(gdb) cContinuing.n = 9 pp is 21 Breakpoint 4, main () at gdb-sample.c:2727          n=n + 4;(gdb) delete Delete all breakpoints? (y or n) y(gdb) qA debugging session is active.

delete num 删除 指定 num 的断点 delete 不加删除所有的断点 。 clear 删除当前的断点
当然如果程序有多个源文件。可以设置任意一个文件处断点。可以使用 b file:num 或者 b file:fun

0 0