如何在Erlang中调试?

时间:2012-02-14 08:43:14

标签: debugging erlang

当我运行广播服务器时,我收到了错误报告:

=ERROR REPORT==== 14-Feb-2012::16:22:29 ===
Error in process <0.757.0> with exit value: {badarg,[{mymodule1,func1,1}]}


=ERROR REPORT==== 14-Feb-2012::16:22:30 ===
Error in process <0.751.0> with exit value: {function_clause,[{mymodule2, func2,[{#Port<0.2

2 个答案:

答案 0 :(得分:3)

function_clause意味着没有与参数匹配的函数mymodule2:func2的定义。 E.g。

func2({X, Y}) -> ... %% only accepts a tuple of size 2 

func2([1, 2, 3])%% called with a list instead; will fail with function_clause
由于内置函数的错误参数,可能会抛出带有函数的

badarghttp://erlang.2086793.n4.nabble.com/function-badarg-td3645808.html

请在此处查看其他失败原因列表:http://learnyousomeerlang.com/errors-and-exceptions

用于调试:1)最新的Erlang版本(R15B)应包含异常消息中的行号; 2)您可以使用Erlang附带的debugger

答案 1 :(得分:3)

调试错误或崩溃时,查看某个函数的输入和输出通常很有用。 eper repo中的调试实用程序redbug使其变得非常简单

示例:

%%% Trace a function:
1>redbug:start("lists:sort")
2>lists:sort([3,1,2]).

21:41:00 <{erlang,apply,2}> {lists,sort,[[3,1,2]]}

%%% Trace a module and also get the return value
3>redbug:start("string->return")
4>string:to_upper("foo").

21:41:10 <{erlang,apply,2}> {string,to_upper,["foo"]}
21:41:10 <{erlang,apply,2}> {string,'-to_upper/1-lc$^0/1-0-',["foo"]}
...
21:41:10 <{erlang,apply,2}> {string,to_upper,1} -> "FOO"

因此,在您的代码中,我将查看mymodule1:func1获取的输入:

1>redbug:start("mymodule1:func1").
2> %% redo the call that caused the crash