GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7 Copyright (C) 2013 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-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/share/test/c/elf_test/test...done. (gdb)
gdb下调试函数栈帧,可用以下命令
backktrace:显示程序的调用栈信息,缩写’bt’
frame: 查看栈帧信息
info frame:查看栈帧详细信息
首先在main函数处设置断点,并查看函数栈信息
1 2 3 4 5 6 7 8 9 10
(gdb) b main Breakpoint 1 at 0x400510: file test.c, line 13. (gdb) run Starting program: /home/share/test/c/elf_test/test
Breakpoint 1, main (argc=1, argv=0x7fffffffe308) at test.c:13 13 a = 2; (gdb) bt #0 main (argc=1, argv=0x7fffffffe308) at test.c:13 (gdb)
可看到当前程序存在一个栈帧,栈帧号为0,是main函数
查看详细栈帧信息
1 2 3 4 5 6 7 8 9
(gdb) info frame Stack level 0, frame at 0x7fffffffe230: rip = 0x400510 in main (test.c:13); saved rip 0x7ffff7a3db35 source language c. Arglist at 0x7fffffffe220, args: argc=1, argv=0x7fffffffe308 Locals at 0x7fffffffe220, Previous frame's sp is 0x7fffffffe230 Saved registers: rbp at 0x7fffffffe220, rip at 0x7fffffffe228 (gdb)
(gdb) s 14 b = 3; (gdb) s 17 c = sum(a, b); (gdb) bt #0 main (argc=1, argv=0x7fffffffe308) at test.c:17 (gdb) s sum (a=2, b=3) at test.c:6 6 return (a+b); (gdb) bt #0 sum (a=2, b=3) at test.c:6 #1 0x000000000040052d in main (argc=1, argv=0x7fffffffe308) at test.c:17 (gdb)
(gdb) info frame 0 Stack frame at 0x7fffffffe200: rip = 0x4004f7 insum (test.c:6); saved rip 0x40052d called by frame at 0x7fffffffe230 source language c. Arglist at 0x7fffffffe1f0, args: a=2, b=3 Locals at 0x7fffffffe1f0, Previous frame's sp is 0x7fffffffe200 Saved registers: rbp at 0x7fffffffe1f0, rip at 0x7fffffffe1f8 (gdb) info frame 1 Stack frame at 0x7fffffffe230: rip = 0x40052d in main (test.c:17); saved rip 0x7ffff7a3db35 caller of frame at 0x7fffffffe200 source language c. Arglist at 0x7fffffffe220, args: argc=1, argv=0x7fffffffe308 Locals at 0x7fffffffe220, Previous frame's sp is 0x7fffffffe230 Saved registers: rbp at 0x7fffffffe220, rip at 0x7fffffffe228 (gdb)