Home > Erlang探索 > 如何用gdb调试erlang运行期(高级)

如何用gdb调试erlang运行期(高级)

October 11th, 2009

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 如何用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.

Categories: Erlang探索 Tags: , , , ,
  1. wyt
    June 22nd, 2013 at 13:16 | #1

    霸爷你好,关注你很久了
    由于你的推荐,我试了下cerl,但无论在emacs 22、23还是24的版本中都会报错(加-gdb参数),我看了下代码,cerl是用调用erlexec -emu_args_exit的方式获取参数的,但在erlexec.c中参数的返回用换行符分隔了,我把这些换行符去掉,结果正常运行,不知你平时用时有没有遇到这个问题

    Yu Feng Reply:

    最新的版本支持 cel -rgdb 可以脱离emacs

    wyt Reply:

    脱离emacs是可以正常的,这个没问题,我上面的问题其实就想问问在emacs环境下的情况,因为有更直观的源码对照,还有-rgdb这个参数只是设置进入gdb环境,这个不是直接用-break就可以了吗

    wyt Reply:

    还有就是,博客中提到的“cerl -debug -gdb -break main”是不是应该把-gdb放到最后,因为如果放到之前的话-break又会把cerl中的GDB设置成非emacs环境,参照:https://github.com/erlang/otp/blob/maint/erts/etc/unix/cerl.src#L191

Comments are closed.