日期:2014-05-16  浏览次数:20516 次

用gdb获得寄存器的值
//gdb_debug.c
#include <stdio.h>
f1()
{
	register int i = 12345;
	printf("%d\n", i);
	double x = -5.5625;
	printf("%d\n",x);
}
main()
{
	f1();
}


gcc -g -o gdb_debug gdb_debug.c

用gdb调试:
ubuntu@ubuntu-VirtualBox:~$ gdb gdb_debug
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 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 /home/ubuntu/gdb_debug...done.
(gdb) b main
Breakpoint 1 at 0x804840a: file gdb_debug.c, line 11.
(gdb) r
Starting program: /home/ubuntu/gdb_debug

Breakpoint 1, main () at gdb_debug.c:11
11 f1();
(gdb) list
6 double x = -5.5625;
7 printf("%d\n",x);
8 }
9 main()
10 {
11 f1();
12 }
(gdb) x/i $pc
=> 0x804840a <main+6>: call   0x80483c4 <f1>
(gdb) si
f1 () at gdb_debug.c:3
3 {
(gdb) si
0x080483c5 3 {
(gdb) si
0x080483c7 3 {
(gdb) disass main
Dump of assembler code for function main:
   0x08048404 <+0>: push   %ebp
   0x08048405 <+1>: mov    %esp,%ebp
   0x08048407 <+3>: and    $0xfffffff0,%esp
   0x0804840a <+6>: call   0x80483c4 <f1>
   0x0804840f <+11>: mov    %ebp,%esp
   0x08048411 <+13>: pop    %ebp
   0x08048412 <+14>: ret   
End of assembler dump.
(gdb) disass f1
Dump of assembler code for function f1:
   0x080483c4 <+0>: push   %ebp
   0x080483c5 <+1>: mov    %esp,%ebp
=> 0x080483c7 <+3>: push   %ebx
   0x080483c8 <+4>: sub    $0x24,%esp
   0x080483cb <+7>: mov    $0x3039,%ebx
   0x080483d0 <+12>: mov    $0x80484e0,%eax
   0x080483d5 <+17>: mov    %ebx,0x4(%esp)
   0x080483d9 <+21>: mov    %eax,(%esp)
   0x080483dc <+24>: call   0x80482f4 <printf@plt>
   0x080483e1 <+29>: fldl   0x80484e8
   0x080483e7 <+35>: fstpl  -0x10(%ebp)
   0x080483ea <+38>: mov    $0x80484e0,%eax
   0x080483ef <+43>: fldl   -0x10(%ebp)
   0x080483f2 <+46>: fstpl  0x4(%esp)
   0x080483f6 <+50>: mov    %eax,(%esp)
   0x080483f9 <+53>: call   0x80482f4 <printf@plt>
   0x080483fe <+58>: add    $0x24,%esp
   0x08048401 <+61>: pop    %ebx
   0x08048402 <+62>: pop    %ebp
   0x08048403 <+63>: ret   
---Type <return> to continue, or q <return> to quit---
End of assembler dump.
(gdb)

IP是指令寄存器,存放当前指令的下一条指令的地址。CPU该执行哪条指令就是通过IP来指示的。
EIP是32位机的指令寄存器。

IP:instruction pointer
PC: progtam counter 
两者都是一个寄存器,指向当前执行指令的下一条指令。


(gdb) info all-registers  //显示所有寄存器的内容
eip