Linux 如果对象的类类型类似于 A::B,如何使用 GDB 从地址打印 C++ 对象成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7380027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:09:07  来源:igfitidea点击:

How to print a c++ object members using GDB from an address if the object's class type is like A::B

c++linuxgdb

提问by Gary Hu

From this link gdb interpret memory address as an objectwe know that, if an object of class type A is at a specific address such as 0x6cf010, then we can use:

从这个链接 gdb 将内存地址解释为一个对象,我们知道,如果类类型 A 的对象位于特定地址,例如 0x6cf010,那么我们可以使用:

(gdb) p *(A *) 0x6cf010 

to print the member elements of this object.

打印此对象的成员元素。

However, this seems doesn't work when c++ namespace is involved. That is, if the object of class type A::B, then all the following trying doesn't work:

但是,当涉及 c++ 命名空间时,这似乎不起作用。也就是说,如果类类型为 A::B 的对象,则以下所有尝试都不起作用:

(gdb) p *(A::B *) 0x6cf010
(gdb) p *((A::B *) 0x6cf010)

So, who knows how to print the object elements under this conditions?

那么,谁知道在这种情况下如何打印对象元素呢?



We can use the following deliberate core code to try to print the members of p from the address (we can use "info locals" to show the address).

我们可以使用下面刻意的核心代码来尝试从地址中打印出 p 的成员(我们可以使用“info locals”来显示地址)。

#include <stdio.h>

namespace A
{
    class B
    {
    public:
        B(int a) : m_a(a) {}

        void print()
        {
            printf("m_a is %d\n", m_a);
        }

    private:
        int  m_a;
    };
}

int main()
{
    A::B *p = new A::B(100);

    p->print();

    int *q = 0;

    // Generating a core here
    *q = 0;
    return 0;

}

}

采纳答案by sehe

Works for me:

对我有用:

g++ -g test.cpp -o test
gdb test
(gdb) break main
(gdb) r


Breakpoint 1, main () at test.cpp:22
22      A::B *p = new A::B(100);
(gdb) n
24      p->print();
(gdb) n
m_a is 100
26      int *q = 0;
(gdb) p p
 = (A::B *) 0x602010
(gdb) p (A::B *) 0x602010
 = (A::B *) 0x602010
(gdb) p *((A::B *) 0x602010)
 = {m_a = 100}

It works for me. What are you using (gcc version, OS, compilation flags?)

这个对我有用。您在使用什么(gcc 版本、操作系统、编译标志?)

回答by Matt

I know that this is labeled as answered, but I was able to reproduce this problem using gdb on OS X (GNU gdb 6.3.50-20050815 (Apple version gdb-1820) (Sat Jun 16 02:40:11 UTC 2012))and the works-for-me solution didn't answer it for me.

我知道这被标记为已回答,但我能够在 OS X 上使用 gdb 重现这个问题,(GNU gdb 6.3.50-20050815 (Apple version gdb-1820) (Sat Jun 16 02:40:11 UTC 2012))并且为我工作的解决方案没有为我回答。

Turns out there was another question on SO that did have an answer which worked, so I think it's worth pulling into this quesiton:

原来还有另一个关于 SO 的问题确实有一个有效的答案,所以我认为值得考虑这个问题:

Why gdb casting is not working?

为什么 gdb 铸造不起作用?

The short answer is that you may have to single-quote your namespaced variables:

简短的回答是您可能必须单引号您的命名空间变量:

(gdb) p ('MyScope::MyClass'*) ptr;

(gdb) p ('MyScope::MyClass'*) ptr;