/*********************************************************************
* 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 <stdio.h>
#include <stdlib.h>
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參數的編譯命令行進行編譯:
linuxidc@linuxidc:~$ gcc test.c
使用gdb進行程序的調試:
linuxidc@linuxidc:~$ 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 <http://gnu.org/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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from a.out...(no debugging symbols found)...done.
(gdb) l
沒有符號表被讀取。請使用 "file" 命令。
(gdb) disassemble main
Dump of assembler code for function main:
0x0804841c <+0>: push %ebp
0x0804841d <+1>: mov %esp,%ebp
0x0804841f <+3>: and $0xfffffff0,%esp
0x08048422 <+6>: sub $0x20,%esp
0x08048425 <+9>: movl $0x0,0x14(%esp)
0x0804842d <+17>: movl $0x9,0x18(%esp)
0x08048435 <+25>: mov 0x18(%esp),%eax
0x08048439 <+29>: mov 0x14(%esp),%edx
0x0804843d <+33>: add %edx,%eax
0x0804843f <+35>: mov %eax,0x1c(%esp)
0x08048443 <+39>: mov 0x1c(%esp),%eax
0x08048447 <+43>: mov %eax,0x4(%esp)
0x0804844b <+47>: movl $0x8048540,(%esp)
0x08048452 <+54>: call 0x80482f0 <printf@plt>
0x08048457 <+59>: mov 0x18(%esp),%eax
0x0804845b <+63>: mov 0x1c(%esp),%edx
0x0804845f <+67>: add %edx,%eax
0x08048461 <+69>: mov %eax,0x14(%esp)
0x08048465 <+73>: mov 0x14(%esp),%eax
0x08048469 <+77>: mov %eax,0x4(%esp)
0x0804846d <+81>: movl $0x8048549,(%esp)
0x08048474 <+88>: call 0x80482f0 <printf@plt>
0x08048479 <+93>: mov 0x18(%esp),%eax
0x0804847d <+97>: mov 0x14(%esp),%edx
0x08048481 <+101>: mov %edx,%ecx
0x08048483 <+103>: sub %eax,%ecx
0x08048485 <+105>: mov %ecx,%eax
0x08048487 <+107>: mov %eax,0x18(%esp)
0x0804848b <+111>: mov 0x18(%esp),%eax
0x0804848f <+115>: mov %eax,0x4(%esp)
0x08048493 <+119>: movl $0x8048552,(%esp)
0x0804849a <+126>: call 0x80482f0 <printf@plt>
0x0804849f <+131>: mov $0x0,%eax
0x080484a4 <+136>: leave
0x080484a5 <+137>: 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 <main+54>
2 breakpoint keep y 0x08048474 <main+88>
3 breakpoint keep y 0x0804849a <main+126>
(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調試程序用法 http://www.linuxidc.com/Linux/2013-06/86044.htm
GDB+GDBserver無源碼調試Android 動態鏈接庫的技巧 http://www.linuxidc.com/Linux/2013-06/85936.htm
使用hello-gl2建立ndk-GDB環境(有源碼和無源碼調試環境) http://www.linuxidc.com/Linux/2013-06/85935.htm
在Ubuntu上用GDB調試printf源碼 http://www.linuxidc.com/Linux/2013-03/80346.htm
Linux下用GDB調試可加載模塊 http://www.linuxidc.com/Linux/2013-01/77969.htm
Ubuntu下使用GDB斷點Go程序 http://www.linuxidc.com/Linux/2012-06/62941.htm
使用GDB命令行調試器調試C/C++程序 http://www.linuxidc.com/Linux/2014-11/109845.htm
GDB 的詳細介紹:請點這裡
GDB 的下載地址:請點這裡