使用gdb,可以在静态对象销毁后添加断点
With gdb, can a breakpoint be added after static object destruction?
我在一家小型软件公司工作,该公司正在用该公司开发的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()
上设置一个断点,并在执行此操作时检查您的链表。如果我理解正确,您的链表必须是全局变量。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- 无法删除指针,已触发断点
- Qt和C++:将QLineEdit添加到QTabWidget中
- 使用gdb,可以在静态对象销毁后添加断点
- 在添加断点并运行程序时,无法访问地址处的内存
- 添加断点会改变程序的行为
- 如何在gdb中的内存位置上添加断点