[英]Getting GDB to save a list of breakpoints
好的, info break列出了斷點,但格式不適合使用 --command 重用它們,如本問題所示。 GDB 是否有將它們轉儲到可接受再次輸入的文件中的方法? 有時在調試會話中,需要在建立一組斷點后重新啟動 GDB 進行測試。
.gdbinit 文件與 --command 存在相同的問題。 info break命令不列出命令,而是一個供人類使用的表。
詳細說明,這里是info break的示例:
(gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x08048517 <foo::bar(void)+7>
從 GDB 7.2 (2011-08-23) 開始,您現在可以使用save breakpoints命令。
save breakpoints <filename>
Save all current breakpoint definitions to a file suitable for use
in a later debugging session. To read the saved breakpoint
definitions, use the `source' command.
使用source <filename>
從文件中恢復保存的斷點。
這個答案已經過時了。 GDB 現在支持直接保存。 看到這個答案。
您可以使用日志記錄:
(gdb) b main
Breakpoint 1 at 0x8049329
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
(gdb) set logging file breaks.txt
(gdb) set logging on
Copying output to breaks.txt.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
(gdb) q
文件breaks.txt 現在包含:
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
編寫一個 AWK 腳本將其轉換為對.gdbinit
或--command
文件有用的格式很容易。 或者您甚至可以讓腳本向 GDB 命令行發出單獨的--eval-command
......
將這個小宏添加到.gdbinit將幫助您做到這一點:
# Call with dump_breaks file.txt
define dump_breaks
set logging file $arg0
set logging redirect on
set logging on
info breakpoints
set logging off
set logging redirect off
end
將您的 GDB 命令和斷點放在.gdbinit 文件中,就像您可能在gdb>
提示符下鍵入它們一樣,GDB 將在啟動時自動加載並運行它們。 這是每個目錄的文件,因此您可以為不同的項目使用不同的文件。
.gdbinit:
define bsave
shell rm -f brestore.txt
set logging file brestore.txt
set logging on
info break
set logging off
# Reformat on-the-fly to a valid GDB command file
shell perl -n -e 'print "break $1\n" if /^\d+.+?(\S+)$/g' brestore.txt > brestore.gdb
end
document bsave
store actual breakpoints
end
define brestore
source brestore.gdb
end
document brestore
restore breakpoints saved by bsave
end
使用brestore
然后您可以恢復使用bsave
保存的斷點。
Johannes 回答的擴展:您可以自動將info break
的輸出重新格式化為有效的 GDB 命令文件:
.gdbinit:
define bsave
shell rm -f brestore.txt
set logging file brestore.txt
set logging on
info break
set logging off
# Reformat on-the-fly to a valid gdb command file
shell perl -n -e 'print "break $1\n" if /^\d+.+?(\S+)$/g' brestore.txt > brestore.gdb
end
document bsave
store actual breakpoints
end
之后,您在brestore.gdb
有一個有效的命令brestore.gdb
。
當使用-g
編譯應用程序時,這對我-g
。
我還在Ubuntu 9.10 (Karmic Koala) 上使用 GDB v6.8 成功測試了它。
將以下內容放入~/.gdbinit以將bsave和brestore定義為 GDB 命令以保存和恢復斷點。
define bsave
save breakpoints ~/.breakpoints
end
define brestore
source ~/.breakpoints
end
警告:當前輸出協議不支持重定向
嘗試在TUI模式下啟用日志記錄時,我也在 GDB 中收到此錯誤/警告。 但是,日志記錄似乎在“非 TUI”模式下工作。 所以每當我想記錄一些東西時,我都會離開 TUI 模式。 (使用Ctrl + X 、 Ctrl + A來回切換到 TUI 模式)。
這是我的工作方式:
set logging on
- 現在它不應該抱怨。我發現對先前答案的以下補充有助於將斷點保存/加載到特定文件。
與上一個答案一樣,將以下代碼添加到文件 ~/.gdbinit
# Save breakpoints to a file
define bsave
if $argc != 1
help bsave
else
save breakpoints $arg0
end
end
document bsave
Saves all current defined breakpoints to the defined file in the PWD
Usage: bsave <filename>
end
# Loads breakpoints from a file
define bload
if $argc != 1
help bload
else
source $arg0
end
end
document bload
Loads all breakpoints from the defined file in the PWD
Usage: bload <filename>
end
問題是設置斷點是上下文敏感的。 如果您有兩個名為foo 的靜態函數怎么辦?
如果您已經在調試定義 foo 的模塊之一,那么 GDB 將假定您指的是那個模塊。 但是,如果您只是將“break foo”轉儲到一個文件中,然后在啟動時讀取該文件,則不清楚您指的是哪個函數foo 。
問題在於設置斷點是上下文相關的。 如果您有兩個名為foo的靜態函數,該怎么辦? 如果您已經在調試定義foo的模塊之一,則gdb將假定您的意思是那個。 但是,如果只是將“ break foo”轉儲到文件中,然后在啟動時讀取該文件,則您不清楚foo是指哪個函數。
我沒有要回復的修改點,但是您要做的是通過指定源文件和行號來明確斷點。 如果在foo.c:42和bar.c:1337中都指定了foo()
break foo.c:42
break bar.c:1337
或者,指定僅在程序在gdb下運行時才觸發的源內斷點。 請參閱如何檢測GDB是否正在運行當前進程?
還有其他想法嗎? 我有
warning: Current output protocol does not support redirection
后
set logging on
編輯:
我知道這個問題是“如何保存斷點列表”,但是我剛剛發現,使用 GDB 我們可以簡單地設置“保存在文件中”斷點
gdb> source breakpoints.txt
其中breakpoints.txt
是這樣的文件:
break main.cpp:25
break engine.cpp:465
break wheel.cpp:57
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.