GDB:尝试取消引用通用指针 [英] GDB: Attempt to dereference generic pointer

查看:2244
本文介绍了GDB:尝试取消引用通用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能让GDB在打印功能像做额外的指针引用
X / S ?

How can I make GDB do extra dereferences in a printing function like x/s?

当我尝试在 X / 我得到的错误尝试明确的指针引用
解引用一个普通指针。使用 X / 多次工作,因为
每次使用包括一个隐含解引用,但是这是恼人的,因为
我有复制和每个中间结果粘贴。

When I try explicit dereferences in x/ I get the error "Attempt to dereference a generic pointer". Using x/ multiple times works, since each use includes an implicit dereference, but this is annoying since I have to copy and paste each intermediate result.

考虑非常有用的C程序, example.c

Consider the very useful C program, example.c:

#include <stdio.h>
int main(int argc, char **argv) {
  printf("argv[0] = %s\n", argv[0]);
}

如果我构建它并将其加载到GDB,我看到的argv 存储在
位于0xC(%EBP),因为一个双dererence作为第二传递
参数的printf (即为0x4(%ESP))的行26:

If I build it and load it into GDB, I see that argv is stored at 0xc(%ebp), since a double dererence of that is passed as the second argument to printf (i.e. in 0x4(%esp)) on line 26:

$ 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 键,带参数运行程序第一
第二

I break at printf and run the program with arguments first and 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

我试图打印的argv [0] 广发行,但我得到了通用指针
错误:

I attempt to print argv[0] in GDB, but I get the "generic pointer" error:

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) 

但是,这是恼人的和间接的(如指针编程是不会要?)

But this is annoying and indirect (as pointer programming is wont to be?)

推荐答案

解决方案是取消引用之前投指针。

The solution is to cast the pointers before dereferencing them.

例如,拿起我们不放过​​上面:

For example, picking up where we left off above:

(gdb) x/s **((char ***) (0xc + $ebp))
0xbfffedc8:  "/var/tmp/SO-attempt-to-dereference-generic-pointer/example"
(gdb) x/s *(*((char ***) (0xc + $ebp)) + 1)
0xbfffee03:  "first"
(gdb) x/s *(*((char ***) (0xc + $ebp)) + 2)
0xbfffee09:  "second"

请注意堆栈地址位于0xC + $ EBP 本身就是一个指针
该堆栈单元的内容,所以我们需要字符*** 而不是
的char **

Note that the stack address 0xc + $ebp is itself a pointer to the contents of that stack location, and so we need char *** and not char **.

这篇关于GDB:尝试取消引用通用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆