How to debug errors in Erlang? -
i got 1 error when starting gen server, want know how debug it, thanks!
i run "example:add_listener(self(), "127.0.0.1", 10999)." after start_link.
the error :
=error report==== 11-may-2011::13:41:57 === ** generic server <0.37.0> terminating ** last message in {'exit',<0.35.0>, {{timeout, {gen_server,call, [<0.35.0>, {add_listener,"127.0.0.1",10999}]}}, [{gen_server,call,2}, {erl_eval,do_apply,5}, {shell,exprs,6}, {shell,eval_exprs,6}, {shell,eval_loop,3}]}} ** when server state == {state,example, {dict,0,16,16,8,80,48, {[],[],[],[],[],[],[],[],[],[],[],[],[], [],[],[]}, {{[],[],[],[],[],[],[],[],[],[],[],[],[], [],[],[]}}}, {dict,0,16,16,8,80,48, {[],[],[],[],[],[],[],[],[],[],[],[],[], [],[],[]}, {{[],[],[],[],[],[],[],[],[],[],[],[],[], [],[],[]}}}, []} ** reason termination == ** {{timeout,{gen_server,call,[<0.35.0>,{add_listener,"127.0.0.1",10999}]}}, [{gen_server,call,2}, {erl_eval,do_apply,5}, {shell,exprs,6}, {shell,eval_exprs,6}, {shell,eval_loop,3}]} ** exception exit: {timeout,{gen_server,call, [<0.35.0>,{add_listener,"127.0.0.1",10999}]}} in function gen_server:call/2
my code :
-module(test_ess_tcp). -export([start_link/0, add_listener/3, remove_listener/3]). -export([init/2, handle_call/3, handle_cast/2, handle_info/2]). -export([terminate/2, sock_opts/0, new_connection/4]). -behavior(ess_tcp). start_link() -> ess_tcp:start_link(?module, []). add_listener(pid, ipaddr, port) -> gen_server:call(pid, {add_listener, ipaddr, port}). remove_listener(pid, ipaddr, port) -> gen_server:call(pid, {remove_listener, ipaddr, port}). init([], state) -> %% example storing callback module specific state %% modifies server state {ok, ess_tcp:store_cb_state([], state)}. handle_call({add_listener, ipaddr, port}, _from, state) -> %% example of getting callback module state io:format("not used here, example"), [] = ess_tcp:get_cb_state(state), case ess_tcp:add_listen_socket({ipaddr, port}, state) of {ok, state1} -> {reply, ok, state1}; error -> {reply, error, state} end; handle_call({remove_listener, ipaddr, port}, _from, state) -> case ess_tcp:remove_listen_socket({ipaddr, port}, state) of {ok, state1} -> {reply, ok, state1}; error -> {reply, error, state} end; handle_call(_msg, _from, state) -> {reply, ignored, state}. handle_cast(_msg, state) -> {noreply, state}. handle_info({tcp, sock, data}, state) -> me = self(), p = spawn(fun() -> worker(me, sock, data) end), gen_tcp:controlling_process(sock, p), {noreply, state}; handle_info(_msg, state) -> {noreply, state}. terminate(_reason, _state) -> ok. sock_opts() -> [binary, {active, once}, {packet, 0}]. new_connection(_ipaddr, _port, sock, state) -> me = self(), p = spawn(fun() -> worker(me, sock) end), gen_tcp:controlling_process(sock, p), {ok, state}. worker(owner, sock) -> gen_tcp:send(sock, "hello\n"), inet:setopts(sock, [{active, once}]), gen_tcp:controlling_process(sock, owner). worker(owner, sock, data) -> gen_tcp:send(sock, data), inet:setopts(sock, [{active, once}]), gen_tcp:controlling_process(sock, owner).
well, gen_server:call getting timeout when called. means gen_server either taking longer default 3 second timeout call or blocked somewhere.
using tracing debug kind of behaviour ideal. instance if type in shell before running test:
dbg:tracer(),dbg:p(all,c),dbg:tpl(ess_tcp, x).
you trace on functions within ess_tcp see going on in there. more info dbg see http://www.erlang.org/doc/man/dbg.html
Comments
Post a Comment