我正在通过gdb软件研究C程序的体系结构。我编写了以下程序来检查堆栈的操作。
我使用下面的代码。
#include <stdio.h>
void foo(double x, double y)
{
double a=255;
double b=255;
double c=255;
double d=255;
double e=255;
double f=255;
double g=255;
double h=255;
double i=255;
double j=255;
double k=255;
double l=255;
double m=255;
double o=255;
double p=255;
double a1=255;
double b1=255;
double c1=255;
double d1=255;
double e1=255;
double f1=255;
double g1=255;
double h1=255;
double i1=255;
double j1=255;
double a2=255;
double b2=255;
double c2=255;
double d2=255;
double e2=255;
double f2=255;
double g2=255;
double h2=255;
double i2=255;
double j2=255;
c=a+b;
}
double main()
{
foo(255,255);
return 0;
}
编译程序
gcc -g -o exemplo01 exemplo01.c
我在主函数中创建了一个断点,并在gdb中运行该程序,并且正在执行“ step”命令并查看SP寄存器的内容,但不会更改该值。当我在foo函数中创建变量时,它不应该堆叠吗?
ricardobarbosa@isadora:~/dev/c/pilha$ gdb exemplo01
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from exemplo01...done.
(gdb) b main
Breakpoint 1 at 0x40074d: file exemplo01.c, line 50.
(gdb) run
Starting program: /home/ricardobarbosa/dev/c/pilha/exemplo01
Breakpoint 1, main () at exemplo01.c:50
50 foo(255,255);
(gdb) list
45 c=a+b;
46 }
47
48 double main()
49 {
50 foo(255,255);
51 return 0;
52 }
53
(gdb) s
foo (x=255, y=255) at exemplo01.c:5
5 double a=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
6 double b=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
7 double c=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
8 double d=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
9 double e=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
10 double f=255;
(gdb)
11 double g=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
12 double h=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
13 double i=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
14 double j=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
15 double k=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
16 double l=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
17 double m=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
18 double o=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
19 double p=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
21 double a1=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
22 double b1=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
23 double c1=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
24 double d1=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
25 double e1=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
26 double f1=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
27 double g1=255;
(gdb) i r $rsp
rsp 0x7fffffffdaf0 0x7fffffffdaf0
(gdb) n
28 double h1=255;
(gdb)
知道我在做什么错吗?还是我的理解不正确? 当我在函数中使用变量时,学习堆栈的数量会增加,并且堆将成为分配区域,对吗?
转储内存
(gdb) x/128db $rsp
0x7fffffffdaf0: 0 0 0 0 0 -32 111 64
0x7fffffffdaf8: 0 0 0 0 0 -32 111 64
0x7fffffffdb00: 0 0 0 0 0 -32 111 64
0x7fffffffdb08: 0 0 0 0 0 -32 111 64
0x7fffffffdb10: 0 0 0 0 0 -32 111 64
0x7fffffffdb18: 0 0 0 0 0 -32 111 64
0x7fffffffdb20: 0 0 0 0 0 -32 111 64
0x7fffffffdb28: 0 0 0 0 0 -32 111 64
0x7fffffffdb30: 0 0 0 0 0 -32 111 64
0x7fffffffdb38: 0 0 0 0 0 -32 111 64
0x7fffffffdb40: 46 78 61 -10 0 0 0 0
0x7fffffffdb48: 11 3 64 0 0 0 0 0
0x7fffffffdb50: -1 -1 -1 -1 0 0 0 0
0x7fffffffdb58: -72 -36 -1 -1 -1 127 0 0
0x7fffffffdb60: -8 81 -94 -9 -1 127 0 0
0x7fffffffdb68: -64 116 -1 -9 -1 127 0 0
(gdb)
(gdb) x/128db $rsp-128
0x7fffffffda70: -56 93 -94 -9 -1 127 0 0
0x7fffffffda78: 0 0 0 0 0 -32 111 64
0x7fffffffda80: 0 0 0 0 0 -32 111 64
0x7fffffffda88: 51 8 0 0 1 0 0 0
0x7fffffffda90: 0 0 0 0 0 -32 111 64
0x7fffffffda98: 0 0 0 0 0 -32 111 64
0x7fffffffdaa0: 0 0 0 0 0 -32 111 64
0x7fffffffdaa8: 0 0 0 0 0 -32 111 64
0x7fffffffdab0: 0 0 0 0 0 -32 111 64
0x7fffffffdab8: 0 0 0 0 0 -32 111 64
0x7fffffffdac0: 0 0 0 0 0 -32 111 64
0x7fffffffdac8: 0 0 0 0 0 -32 111 64
0x7fffffffdad0: 0 0 0 0 0 -32 111 64
0x7fffffffdad8: 0 0 0 0 0 -32 111 64
0x7fffffffdae0: 0 0 0 0 0 -32 111 64
0x7fffffffdae8: 0 0 0 0 0 -32 111 64
(gdb) x/128db $rsp+128
0x7fffffffdb70: -56 -31 -1 -9 -1 127 0 0
0x7fffffffdb78: 0 0 0 0 0 0 0 0
0x7fffffffdb80: 1 0 0 0 0 0 0 0
0x7fffffffdb88: -35 7 64 0 0 0 0 0
0x7fffffffdb90: -64 -37 -1 -1 -1 127 0 0
0x7fffffffdb98: 0 0 0 0 0 0 0 0
0x7fffffffdba0: -112 7 64 0 0 0 0 0
0x7fffffffdba8: -64 -37 -1 -1 -1 127 0 0
0x7fffffffdbb0: 120 7 64 0 0 0 0 0
0x7fffffffdbb8: 0 0 0 0 0 -32 111 64
0x7fffffffdbc0: 0 0 0 0 0 0 0 0
0x7fffffffdbc8: 69 111 -93 -9 -1 127 0 0
0x7fffffffdbd0: 0 0 0 0 0 0 0 0
0x7fffffffdbd8: -88 -36 -1 -1 -1 127 0 0
0x7fffffffdbe0: 0 0 0 0 1 0 0 0
0x7fffffffdbe8: 69 7 64 0 0 0 0 0
(gdb)
答案 0 :(得分:0)
您没有做错什么,只是gcc
在函数开始时计算堆栈大小:
void foo(void)
{
int a = 0
int b = 0
int c = 0;
}
void bar(void)
{
int a = 0
int b = 0
int c = 0;
int d = 0
int e = 0
int f = 0;
}
将给出类似(gcc -s
,对于i386 CPU)
_foo:
pushl %esp
movl %esp, %ebp
subl $12, %esp ; reserve 12 bytes for variables on stack
movl $0, -4(%ebp) ; set a
movl $0, -8(%ebp) ; set b
movl $0, -12(%ebp) ; set c
leave
ret
_bar:
pushl %esp
movl %esp, %ebp
subl $24, %esp ; reserve 24 bytes for variables on stack
movl $0, -4(%ebp) ; set a
movl $0, -8(%ebp) ; set b
movl $0, -12(%ebp) ; set c
movl $0, -16(%ebp) ; set d
movl $0, -20(%ebp) ; set e
movl $0, -24(%ebp) ; set f
leave
ret