C-Gdb不允许我查看堆栈内存中的值

时间:2018-08-10 17:33:32

标签: c assembly gdb stack

我正在读一本书,并使用C和汇编语言进行练习,我正在使用以下简单脚本来分析内存的功能:

void test_function(int a, int b, int c, int d) {
   int flag;
   char buffer[10];

   flag = 31337;
   buffer[0] = 'A';
}

int main() {
   test_function(1, 2, 3, 4);
}

这是gdb的输出。我有intel拆卸风格和64位系统

(gdb) break 10
Breakpoint 1 at 0x67e: file stack_example.c, line 10.
(gdb) break test_function
Breakpoint 2 at 0x649: file stack_example.c, line 1.
(gdb) run
Starting program: /home/ut/Desktop/a.out 

Breakpoint 1, main () at stack_example.c:10
10     test_function(1, 2, 3, 4);
(gdb) cont
Continuing.

Breakpoint 2, test_function (a=1, b=2, c=3, d=4) at stack_example.c:1
1   void test_function(int a, int b, int c, int d) {
(gdb) disass test_function
Dump of assembler code for function test_function:
   0x0000555555554635 <+0>: push   rbp
   0x0000555555554636 <+1>: mov    rbp,rsp
   0x0000555555554639 <+4>: sub    rsp,0x30
   0x000055555555463d <+8>: mov    DWORD PTR [rbp-0x24],edi
   0x0000555555554640 <+11>:    mov    DWORD PTR [rbp-0x28],esi
   0x0000555555554643 <+14>:    mov    DWORD PTR [rbp-0x2c],edx
   0x0000555555554646 <+17>:    mov    DWORD PTR [rbp-0x30],ecx
=> 0x0000555555554649 <+20>:    mov    rax,QWORD PTR fs:0x28
   0x0000555555554652 <+29>:    mov    QWORD PTR [rbp-0x8],rax
   0x0000555555554656 <+33>:    xor    eax,eax
   0x0000555555554658 <+35>:    mov    DWORD PTR [rbp-0x18],0x7a69
   0x000055555555465f <+42>:    mov    BYTE PTR [rbp-0x12],0x41
   0x0000555555554663 <+46>:    nop
   0x0000555555554664 <+47>:    mov    rax,QWORD PTR [rbp-0x8]
   0x0000555555554668 <+51>:    xor    rax,QWORD PTR fs:0x28
   0x0000555555554671 <+60>:    je     0x555555554678 <test_function+67>
   0x0000555555554673 <+62>:    call   0x555555554540 <__stack_chk_fail@plt>
   0x0000555555554678 <+67>:    leave  
   0x0000555555554679 <+68>:    ret    
End of assembler dump.
(gdb) x/16xg $rsp
0x7fffffffe850: 0x0000000300000004  0x0000000100000002
0x7fffffffe860: 0x00007ffff7de72c0  0x0000000000000000
0x7fffffffe870: 0x00005555555546a0  0x0000555555554550
0x7fffffffe880: 0x00007fffffffe890  0x0000555555554697
0x7fffffffe890: 0x00005555555546a0  0x00007ffff7a3f06b
0x7fffffffe8a0: 0x0000000000000000  0x00007fffffffe978
0x7fffffffe8b0: 0x0000000100100000  0x000055555555467a
0x7fffffffe8c0: 0x0000000000000000  0x3a50765db3702ef3

在本书之后,多亏了gdb中的最后一个命令,我应该能够在最后一个内存地址中看到4个初始int。我还应该在这些int之前看到“ flag”和“ buffer”的其他值,但是我看不到。我怎么了?如何查看这些值?

而且,为什么我要使用“ 16”?以及为什么$ rsp?这些值不应该在$ rbp中吗?

1 个答案:

答案 0 :(得分:0)

这是您的值所在的行:0x7fffffffe850:0x0000000300000004 0x0000000100000002,并且您的程序以这种方式放入堆栈(就像任何C使用堆栈一样)。

尝试:

(gdb) x/10 &buffer

(gdb) x/s buffer