`
fanrey
  • 浏览: 252603 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

用gdb获得寄存器的值

    博客分类:
  • 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            0x80483c5 0x80483c5 <f1+1>

(gdb) disass f1   //反汇编函数f1
Dump of assembler code for function f1:
   0x080483c4 <+0>: push   %ebp
=> 0x080483c5 <+1>: mov    %esp,%ebp
   0x080483c7 <+3>: push   %ebx

EBP: 栈底指针
ESP: 栈顶指针

通用寄存器:
(gdb) info all-registers
eax            0xbffff1d4 -1073745452
ecx            0xf877f12f -126357201
edx            0x1 1
ebx            0x3039 12345  //寄存器变量存在此
st0~7是80位的浮点数寄存器,用于存放浮点数:
st0            0 (raw 0x00000000000000000000)
st1            0 (raw 0x00000000000000000000)
st2            0 (raw 0x00000000000000000000)
st3            0 (raw 0x00000000000000000000)
st4            0 (raw 0x00000000000000000000)
---Type <return> to continue, or q <return> to quit---
st5            0 (raw 0x00000000000000000000)
st6            0 (raw 0x00000000000000000000)
st7            -5.5625 (raw 0xc001b200000000000000)


用GDB查看core file;
gdb <binary file> <core file>
然后输入where即可看到core里的堆栈信息。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics