簡體   English   中英

使用gdb,可以在銷毀靜態對象之后添加斷點嗎?

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

我正在一家小型軟件公司工作,該公司正在使用該公司開發Windows應用程序並由該公司開發IDE,但該公司缺乏對Valgrind或C運行時調試庫的支持。 我們已經為C編寫了一個原始的內存泄漏檢測程序,該程序的工作方式是在main()退出之前設置一個斷點,然后檢查未釋放內存的鏈表(跟蹤內存分配和釋放)。 我們想增加C ++支持,但是由於內存釋放可以在main()返回全局變量的析構函數之后發生,因此在main的末尾添加斷點不再可行。

所以我想知道在靜態對象銷毀之后是否有添加斷點的方法? 順便說一下,我們使用的編譯器是Clang。

C ++靜態對象在exit()被破壞。 您可以在_exit()上放置一個斷點,此時必須銷毀所有靜態對象。

請在使用сlang++編譯的Linux測試程序中查看回溯記錄:

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()上放置一個斷點,並在執行操作時檢查鏈接列表。 如果我理解正確,則您的鏈接列表必須是全局變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM