【发布时间】:2019-04-13 20:11:58
【问题描述】:
我正在调试以下程序(这是正确的)。
[OP@localhost 04]$ cat factorial.s
.section .text
.globl _start
.globl _factorial
_start:
push $4
call factorial
add $8, %rsp
mov %rax, %rdi
mov $60, %rax
syscall
.type factorial, @function
factorial:
# Parameters
# n : int
# Number to take factorial of
push %rbp
mov %rsp, %rbp
mov 0x10(%rbp), %rax
if:
cmp $1, %rax
jne else
jmp end_if
else:
dec %rax
push %rax
call factorial
add $8, %rsp
imul 0x10(%rbp), %rax
end_if:
pop %rbp
ret
我在阶乘函数处设置了一个断点,并继续了两次。检查%rsp的值,发现是
(gdb) print/$rsp
$1 = 0x7fffffffd698
检查周围的区域,我发现
(gdb) x /10xg 0x7fffffffd690
0x7fffffffd690: 0x0000000000000000 0x00007fffffffd6b0
0x7fffffffd6a0: 0x0000000000401030 0x0000000000000002
0x7fffffffd6b0: 0x00007fffffffd6c8 0x0000000000401030
0x7fffffffd6c0: 0x0000000000000003 0x0000000000000000
0x7fffffffd6d0: 0x0000000000401007 0x0000000000000004
大致上,这是人们所期望的。但是,where 的输出如下:
(gdb) where
#0 0x000000000040101b in factorial ()
#1 0x0000000000401030 in else ()
#2 0x0000000000000002 in ?? ()
#3 0x00007fffffffd6c8 in ?? ()
#4 0x0000000000401030 in else ()
#5 0x0000000000000003 in ?? ()
#6 0x0000000000000000 in ?? ()
我似乎无法理解。它似乎是按顺序读取堆栈,但我不知道数字 0x40101b 来自哪里(该数字不在堆栈上),我不确定它为什么停在那里,因为它没有打印出对factorial 的初始函数调用的堆栈帧。
【问题讨论】: