使用gdb,可以在静态对象销毁后添加断点

With gdb, can a breakpoint be added after static object destruction?

本文关键字:添加 断点 对象 静态 gdb 使用      更新时间:2023-10-16

我在一家小型软件公司工作,该公司正在用该公司开发的IDE制作windows应用程序,该公司缺乏对Valgrind或C运行时调试库的支持。我们为C编写了一个基本的内存泄漏检测程序,它的工作方式是在main()退出前设置一个断点,并检查未解冻内存的链表(跟踪内存分配和释放)。我们想添加C++支持,但由于main()在全局变量的析构函数中返回后可能会发生内存释放,因此在main末尾添加断点不再有效。

所以我想知道是否有一种方法可以在静态对象销毁后添加断点?顺便说一下,我们使用的编译器是Clang。

C++静态对象在exit()中被销毁。您可以在_exit()上放置一个断点,此时所有静态对象都必须被销毁。

请参阅Linux上使用сlang++编译的测试程序中的回溯:

struct C {
  C() :
    ptr (new int)
  {
  }
  ~C()
  {
    delete ptr;
  }
  int *ptr;
};
C c;
int main()
{
  return 0;
}

这是一个gdb脚本:

set breakpoint pending on
b _exit
command
bt
c
end
b C::~C
command
bt
c
end
b free
command
bt
c
end
r

这本身就是一个测试:

gdb -q -x test.gdb  ./a.out
Reading symbols from /import/home/sergey.kurenkov/src/linux.x64.6.0/tests/test.break_after_static/a.out...done.
Function "_exit" not defined.
Breakpoint 1 (_exit) pending.
Breakpoint 2 at 0x400680: C::~C. (2 locations)
Function "free" not defined.
Breakpoint 3 (free) pending.
Breakpoint 2, C::~C (this=0x600be8 <c>) at main.cpp:8
8         {
#0  C::~C (this=0x600be8 <c>) at main.cpp:8
#1  0x0000003c41235db2 in exit () from /lib64/libc.so.6
#2  0x0000003c4121ece4 in __libc_start_main () from /lib64/libc.so.6
#3  0x0000000000400519 in _start ()
Breakpoint 2, C::~C (this=0x600be8 <c>) at main.cpp:9
9           delete ptr;
#0  C::~C (this=0x600be8 <c>) at main.cpp:9
#1  0x0000000000400685 in C::~C (this=0x600be8 <c>) at main.cpp:8
#2  0x0000003c41235db2 in exit () from /lib64/libc.so.6
#3  0x0000003c4121ece4 in __libc_start_main () from /lib64/libc.so.6
#4  0x0000000000400519 in _start ()
Breakpoint 3, 0x0000003c4127a950 in free () from /lib64/libc.so.6
#0  0x0000003c4127a950 in free () from /lib64/libc.so.6
#1  0x00000000004006c0 in C::~C (this=0x600be8 <c>) at main.cpp:9
#2  0x0000000000400685 in C::~C (this=0x600be8 <c>) at main.cpp:8
#3  0x0000003c41235db2 in exit () from /lib64/libc.so.6
#4  0x0000003c4121ece4 in __libc_start_main () from /lib64/libc.so.6
#5  0x0000000000400519 in _start ()
Breakpoint 1, 0x0000003c412abc30 in _exit () from /lib64/libc.so.6
#0  0x0000003c412abc30 in _exit () from /lib64/libc.so.6
#1  0x0000003c41235d62 in exit () from /lib64/libc.so.6
#2  0x0000003c4121ece4 in __libc_start_main () from /lib64/libc.so.6
#3  0x0000000000400519 in _start ()
[Inferior 1 (process 13558) exited normally]
(gdb)

正如您所看到的,C::~C()在exit()中被调用,C::~ C()本身调用free(),然后调用_exit()。因此,在_exit()上设置一个断点,并在执行此操作时检查您的链表。如果我理解正确,您的链表必须是全局变量。