【发布时间】:2013-12-23 06:16:13
【问题描述】:
如何让 GDB 在打印功能中进行额外的取消引用,例如
x/s?
当我在 x/ 中尝试显式取消引用时,我收到错误“尝试
取消引用通用指针”。多次使用 x/ 有效,因为
每次使用都包含一个隐式取消引用,但这很烦人,因为
我必须复制并粘贴每个中间结果。
示例
考虑一下非常有用的 C 程序,example.c:
#include <stdio.h>
int main(int argc, char **argv) {
printf("argv[0] = %s\n", argv[0]);
}
如果我构建它并将其加载到 GDB 中,我会看到 argv 存储在
0xc(%ebp),因为它的双重引用作为第二个传递
第 26 行 printf 的参数(即在 0x4(%esp) 中):
$ gcc -o example example.c
$ gdb example
(gdb) disass main
Dump of assembler code for function main:
0x080483e4 <+0>: push %ebp
0x080483e5 <+1>: mov %esp,%ebp
0x080483e7 <+3>: and $0xfffffff0,%esp
0x080483ea <+6>: sub $0x10,%esp
0x080483ed <+9>: mov 0xc(%ebp),%eax
0x080483f0 <+12>: mov (%eax),%edx
0x080483f2 <+14>: mov $0x80484e0,%eax
0x080483f7 <+19>: mov %edx,0x4(%esp)
0x080483fb <+23>: mov %eax,(%esp)
0x080483fe <+26>: call 0x8048300 <printf@plt>
0x08048403 <+31>: leave
0x08048404 <+32>: ret
End of assembler dump.
我在printf 中断并使用参数first 和运行程序
second:
(gdb) break *main + 26
Breakpoint 1 at 0x80483fe
(gdb) run first second
Starting program: /var/tmp/SO-attempt-to-dereference-generic-pointer/example first second
我尝试在 GDB 中打印 argv[0],但我得到了“通用指针”
错误:
Breakpoint 1, 0x080483e5 in main ()
(gdb) x/s **(0xc + $ebp)
Attempt to dereference a generic pointer.
但是,通过使用 'x/xw' 手动取消引用几次,我
最终能够打印argv[0](和argv[1]):
(gdb) x/xw 0xc + $ebp
0xbfffeba4: 0xbfffec34
(gdb) x/xw 0xbfffec34
0xbfffec34: 0xbfffedc8
(gdb) x/s 0xbfffedc8
0xbfffedc8: "/var/tmp/SO-attempt-to-dereference-generic-pointer/example"
(gdb) x/xw 0xbfffec34 + 4
0xbfffec38: 0xbfffee03
(gdb) x/s 0xbfffee03
0xbfffee03: "first"
(gdb)
但这很烦人而且是间接的(因为指针编程不应该是这样的?)
【问题讨论】: