1. 程式人生 > >系統技術非業餘研究 » 如何用gdb除錯erlang執行期(高階)

系統技術非業餘研究 » 如何用gdb除錯erlang執行期(高階)

前些天在erlang的原始碼裡面閒逛的時候發現了 bin目錄下的cerl,一看原來是個除錯的高階貨。

我之前寫過一篇文章http://mryufeng.javaeye.com/blog/113852 如何除錯erlang 但是這是土八路的方法, cerl才是現代化工業。

# This is a script to start Erlang/OTP for debugging. PATH is set to
# include this script so if slave nodes are started they will use this
# script as well.
#
# usage: cerl [ OPTIONS ] [ ARGS ]
#
# The OPTIONS are
#
# -rootdir $MYROOTDIR
# Run an installed emulator built from this source
# -debug Run debug compiled emulator
# -gdb Run the debug compiled emulator in emacs and gdb.
# You have to start beam in gdb using “run”.
# -break F Run the debug compiled emulator in emacs and gdb and set break.
# The session is started, i.e. “run” is already don for you.
# -xxgdb FIXME currently disabled
# -purify Run emulator compiled for purify
# -quantify Run emulator compiled for quantify
# -purecov Run emulator compiled for purecov
# -gcov Run emulator compiled for gcov
# -valgrind Run emulator compiled for valgrind
# -lcnt Run emulator compiled for lock counting
# -nox Unset the DISPLAY variable to disable us of X Windows
#

要使用cerl 我們最好準備個除錯版本的erlang。R13B 修正了些編譯錯誤,可以編譯出debug版本的系統:./configure && make TYPE=debug && make

這樣就生成了個beam.debug的執行期。

我們要除錯的時候 就可以在otp的binx目錄下執行 cerl -debug -gdb -break main

這時候cerl自動會載入 emacs 啟動 gud, 整個過程都是自動的。但是這個指令碼有點小問題, gud模型下沒有把原始碼和當前對應的除錯對應起來。可以通過以下方式修正:

yu-fengdemacbook-2:bin yufeng$ diff cerl cerl2
284c284
<     exec $EMACS --eval "(progn (gdb \"gdb $EMU\") $gdbcmd)"
---
>     exec $EMACS --eval "(progn (gdb \"gdb --annotate=3  $EMU\") $gdbcmd)"

具體的操作和介面可以參照這篇文章:
http://www.nabble.com/printing-of-Eterm%27s-from-gdb-td19240493.html

在除錯的時候 我們會希望檢視 eterm的值,但是由於eterm的格式非常複雜, gdb的print什麼的無法滿足我們的需求。 otp開發團隊於是開發出了一套方法來減輕我們的負擔:

1. erts/etc/unix/etp-commands 這是gdb的指令碼 包含了幾十個etp方法,而且文件非常詳細。

2. 原始碼裡面的 pp, ps等函式, 這些函式是專門為gdb除錯開發的。 可以用gdb的 call pp(xxx)來呼叫。

有了這些工具 除錯和研究erts變成了一件很快樂的事情!

Post Footer automatically generated by wp-posturl plugin for wordpress.