linux gdb添加符号表,GDB如何调试没有符号表(未加-g选项的编译)的程序

/*********************************************************************

* Author  : Samson

* Date    : 01/30/2015

* Test platform:

*              3.13.0-24-generic

*              GNU bash, 4.3.11(1)-release

* *******************************************************************/

很多时候,发行版的程序在编译的时候都是没有加上-g这个选项的,那么若是想调试一个程序,应该怎么办呢?

在加了-g选项时,是可以通过行号、函数名等进行断点的设置的,但是没有符号表的情况下,那么怎么来进行程序的断点的设置并进行调试呢?

这就要用到反汇编然后再对地址进行断点的设置来进行调试,具体情况可参看以下例子的过程:

test.c代码如下:

#include

#include

int main()

{

int m = 0, n =9;

int k = m+n;

printf("k is %d\n", k);

m = k + n;

printf("m is %d\n", m);

n = m-n;

printf("n is %d\n", n);

return 0;

}

使用不带-g参数的编译命令行进行编译:

[email protected]:~$ gcc test.c

使用gdb进行程序的调试:

[email protected]:~$ gdb a.out

GNU gdb (GDB) 7.5.91.20130417-cvs-Ubuntu

Copyright (C) 2013 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 "i686-linux-gnu".

For bug reporting instructions, please see:

...

Reading symbols from a.out...(no debugging symbols found)...done.

(gdb) l

没有符号表被读取。请使用 "file" 命令。

(gdb) disassemble main

Dump of assembler code for function main:

0x0804841c :    push  %ebp

0x0804841d :    mov    %esp,%ebp

0x0804841f :    and    $0xfffffff0,%esp

0x08048422 :    sub    $0x20,%esp

0x08048425 :    movl  $0x0,0x14(%esp)

0x0804842d :    movl  $0x9,0x18(%esp)

0x08048435 :    mov    0x18(%esp),%eax

0x08048439 :    mov    0x14(%esp),%edx

0x0804843d :    add    %edx,%eax

0x0804843f :    mov    %eax,0x1c(%esp)

0x08048443 :    mov    0x1c(%esp),%eax

0x08048447 :    mov    %eax,0x4(%esp)

0x0804844b :    movl  $0x8048540,(%esp)

0x08048452 :    call  0x80482f0

0x08048457 :    mov    0x18(%esp),%eax

0x0804845b :    mov    0x1c(%esp),%edx

0x0804845f :    add    %edx,%eax

0x08048461 :    mov    %eax,0x14(%esp)

0x08048465 :    mov    0x14(%esp),%eax

0x08048469 :    mov    %eax,0x4(%esp)

0x0804846d :    movl  $0x8048549,(%esp)

0x08048474 :    call  0x80482f0

0x08048479 :    mov    0x18(%esp),%eax

0x0804847d :    mov    0x14(%esp),%edx

0x08048481 :    mov    %edx,%ecx

0x08048483 :    sub    %eax,%ecx

0x08048485 :    mov    %ecx,%eax

0x08048487 :    mov    %eax,0x18(%esp)

0x0804848b :    mov    0x18(%esp),%eax

0x0804848f :    mov    %eax,0x4(%esp)

0x08048493 :    movl  $0x8048552,(%esp)

0x0804849a :    call  0x80482f0

0x0804849f :    mov    $0x0,%eax

0x080484a4 :    leave

0x080484a5 :    ret

End of assembler dump.

(gdb) b *0x08048452

Breakpoint 1 at 0x8048452

(gdb) b *0x08048474

Breakpoint 2 at 0x8048474

(gdb) b *0x0804849a

Breakpoint 3 at 0x804849a

(gdb) info b

Num    Type          Disp Enb Address    What

1      breakpoint    keep y  0x08048452

2      breakpoint    keep y  0x08048474

3      breakpoint    keep y  0x0804849a

(gdb) r

Starting program: a.out

Breakpoint 1, 0x08048452 in main ()

(gdb) c

Continuing.

k is 9

Breakpoint 2, 0x08048474 in main ()

(gdb) c

Continuing.

m is 18

Breakpoint 3, 0x0804849a in main ()

(gdb) c

Continuing.

n is 9

[Inferior 1 (process 19933) exited normally]

由以上的步骤可以看出,使用了disassemble main 进行主函数的反汇编,然后使用了b *address进行三处printf的地址的断点的设置。

GDB 的详细介绍:请点这里

GDB的下载地址:请点这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值