简体   繁体   English

GDB:调用特定对象的析构函数时的断点

[英]GDB: breakpoint when calling destructor of specific object

In my app, I'm getting a SIGSEGV fault after trying to access a field inside a widget pointer. 在我的应用中,尝试访问小部件指针中的字段后出现SIGSEGV错误。 That widget comes from a 3rd-party library. 该小部件来自第3方库。 I know exactly the point where the signal is being thrown. 我确切地知道发出信号的点。 What I want to know, is if in that specific moment, the "this" pointer of the faulty widget has been deleted or not, and when that happened. 我想知道的是,在那个特定时刻,故障小部件的“ this”指针是否被删除,以及何时删除。

So, the idea is to set a breakpoint at a place where I know my object does exist, and, and here is where my question borns, say to gdb: "break when the destructor of this specific "this" pointer is called". 因此,想法是在我知道我的对象确实存在的地方设置一个断点,并且在我的问题诞生的地方对gdb说:“当调用此特定“ this”指针的析构函数时中断”。 How can I tell gdb to do that? 我怎样才能告诉gdb做到这一点?

In such a case, a can know if the object is deleted before the signal is thrown, and where and why that object has been deleted (to fix the situation). 在这种情况下,可以知道是否在抛出信号之前删除了该对象,以及删除该对象的位置和原因(以解决这种情况)。

How can I tell gdb to do that? 我怎样才能告诉gdb做到这一点?

Use conditional breakpoint. 使用条件断点。 Example: 例:

cat -n t.cc
     1  struct Foo {
     2    ~Foo() {}
     3  };
     4  
     5  Foo *af1, *af2;
     6  int main()
     7  {
     8    Foo f1;
     9    af1 = &f1;
    10    {
    11      Foo f2;
    12      af2 = &f2;
    13    }
    14  }

g++ -g t.cc && gdb -q ./a.out

(gdb) b 12
Breakpoint 1 at 0x400500: file t.cc, line 12.
(gdb) r
Starting program: /tmp/a.out 

Breakpoint 1, main () at t.cc:12
12      af2 = &f2;
(gdb) p &f2
$1 = (Foo *) 0x7fffffffdc9f
(gdb) p &f1
$2 = (Foo *) 0x7fffffffdc9e
(gdb) b 'Foo::~Foo()' if this == 0x7fffffffdc9f
Breakpoint 2 at 0x400532: file t.cc, line 2.
(gdb) c
Continuing.

Breakpoint 2, Foo::~Foo (this=0x7fffffffdc9f, __in_chrg=<optimized out>) at t.cc:2
2     ~Foo() {}
(gdb) bt
#0  Foo::~Foo (this=0x7fffffffdc9f, __in_chrg=<optimized out>) at t.cc:2
#1  0x0000000000400517 in main () at t.cc:12
(gdb) c
Continuing.
[Inferior 1 (process 121877) exited normally]

Voila: breakpoint was hit when f2 was destructed, but not when f1 was. 瞧:在破坏f2时击中了断点,但没有击中f1时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM