erlang调试方法

  1. 第一种方式,加打印记录
%%-define(debug, ok).

-ifdef(debug).
-define(LOG(X), io:format("pid:~p , {~p,~p}: ~p~n", [self(), ?MODULE, ?LINE, X])).
-else.
-define(LOG(X), true).
-endif.

设置LOG的宏定义,调试只要在代码中加入?LOG(Val),就可以

特点是,简单,调试清楚,对代码性能的影响小,也是最常用的方法,就不举例说明了

第二种方法,加trace 日志

%trace Mod 所有方法的调用
trace(Mod) ->
    dbg:tracer(),
    dbg:p(all, [call]),
    dbg:tp(Mod,  [{'_', [], [{return_trace}]}]);


%trace Mod 的Fun方法的调用
trace(Mod, Fun) ->
    dbg:tracer(),
    dbg:p(all, [call]),
    dbg:tp(Mod, Fun, [{'_', [], [{return_trace}]}]);

%停止trace
trace_stop() ->
    dbg:stop_clear().

特点:对整个模块,或者某些函数进行详细的调试,调试信息比较多,对性能有一段影响。

举例

......
......
init([Opt]) ->
    Tab = ets:new(xx, [set]),
    case opt_find(debug, Opt, false) of              %%如果设置了debug,开启debug跟踪
    true ->
        dbg:tracer(),
        dbg:p(all, [call]),
        dbg:tp(tt5, [{'_', [], [{return_trace}]}]);
    _ ->
        %% Keep silent
        ok
    end,
    {ok, #state{tab = Tab}}.

.....
.....
.....
1> c(tt5).
{ok,tt5}
2> tt5:start([{debug, true}]). 
{ok,<0.39.0>}
3> tt5:add(name, "wowchina"). 
(<0.32.0>) call tt5:add(name,"wowchina")
(<0.39.0>) call tt5:handle_call({add,name,"wowchina"},{<0.32.0>,#Ref<0.0.0.104>},{state,16400})
(<0.39.0>) returned from tt5:handle_call/3 -> {reply,true,{state,16400}}
(<0.32.0>) returned from tt5:add/2 -> true
true
4> tt5:find(name).           
(<0.32.0>) call tt5:find(name)
(<0.39.0>) call tt5:handle_call({find,name},{<0.32.0>,#Ref<0.0.0.113>},{state,16400})
(<0.39.0>) returned from tt5:handle_call/3 -> {reply,
                                               [{name,"wowchina"}],
                                               {state,16400}}
(<0.32.0>) returned from tt5:find/1 -> [{name,"wowchina"}]
[{name,"wowchina"}]
5> 
原文地址:https://www.cnblogs.com/tudou008/p/5210109.html