/*********************************************************************
* 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的下载地址:请点这里