Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug in Erlang?

When I run my broadcast server, I got the error report:

=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
like image 801
why Avatar asked Jan 29 '26 13:01

why


2 Answers

When debugging an error or crash, it is often useful to see what input and output a certain function gets. The debug utility redbug in the eper repo makes it rather easy

Examples:

%%% 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"

So in your code I would for example see what input mymodule1:func1 gets:

1>redbug:start("mymodule1:func1").
2> %% redo the call that caused the crash
like image 96
selle Avatar answered Feb 01 '26 10:02

selle


function_clause means simply that there is no definition for function mymodule2:func2 which matches the arguments. 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

badarg with your function can be thrown because of wrong arguments to a built-in function: http://erlang.2086793.n4.nabble.com/function-badarg-td3645808.html

See a list of other failure reasons here: http://learnyousomeerlang.com/errors-and-exceptions

For debugging: 1) the latest Erlang release (R15B) should include line numbers in exception messages; 2) you can use the debugger which comes with Erlang.

like image 34
Alexey Romanov Avatar answered Feb 01 '26 11:02

Alexey Romanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!