[erlang-questions] dbg:p(all, [c]) hangs the shell

Chris Newcombe <>
Thu Jul 2 18:45:08 CEST 2009


> I would like to trace all calls in all processes, however the erlang
> shell hangs after dbg:p(all, [c]). It even does not accept Ctrl-G.


The documentation describes one issue that can cause deadlock, and
suggests some work-arounds.

Chris

>From the end of http://erlang.org/doc/man/dbg.html

"Note of caution

When tracing function calls on a group leader process (an IO process),
there is risk of causing a deadlock. This will happen if a group
leader process generates a trace message and the tracer process, by
calling the trace handler function, sends an IO request to the same
group leader. The problem can only occur if the trace handler prints
to tty using an io function such as format/2. Note that when
dbg:p(all,call) is called, IO processes are also traced. Here's an
example:

%% Using a default line editing shell
1> dbg:tracer(process, {fun(Msg,_) -> io:format("~p~n", [Msg]), 0 end, 0}).
{ok,<0.37.0>}
2> dbg:p(all, [call]).
{ok,[{,25}]}
3> dbg:tp(mymod,[{'_',[],[]}]).
{ok,[{,0},{saved,1}]}
4> mymod: % TAB pressed here
%% -- Deadlock --

Here's another example:

%% Using a shell without line editing (oldshell)
1> dbg:tracer(process).
{ok,<0.31.0>}
2> dbg:p(all, [call]).
{ok,[{,25}]}
3> dbg:tp(lists,[{'_',[],[]}]).
{ok,[{,0},{saved,1}]}
% -- Deadlock --

The reason we get a deadlock in the first example is because when TAB
is pressed to expand the function name, the group leader (which
handles character input) calls mymod:module_info(). This generates a
trace message which, in turn, causes the tracer process to send an IO
request to the group leader (by calling io:format/2). We end up in a
deadlock.

In the second example we use the default trace handler function. This
handler prints to tty by sending IO requests to the user process. When
Erlang is started in oldshell mode, the shell process will have user
as its group leader and so will the tracer process in this example.
Since user calls functions in lists we end up in a deadlock as soon as
the first IO request is sent.

Here are a few suggestions for how to avoid deadlock:

    * Don't trace the group leader of the tracer process. If tracing
has been switched on for all processes, call dbg:p(TracerGLPid,clear)
to stop tracing the group leader (TracerGLPid).
process_info(TracerPid,group_leader) tells you which process this is
(TracerPid is returned from dbg:get_tracer/0).
    * Don't trace the user process if using the default trace handler function.
    * In your own trace handler function, call erlang:display/1
instead of an io function or, if user is not used as group leader,
print to user instead of the default group leader. Example:
io:format(user,Str,Args).




On Wed, Jul 1, 2009 at 11:28 AM, Kaiduan Xie<> wrote:
> Hi, all,
>
> I would like to trace all calls in all processes, however the erlang
> shell hangs after dbg:p(all, [c]). It even does not accept Ctrl-G.
>
> :~$ erl
> Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [hipe]
> [kernel-poll:false]
>
> Eshell V5.6.5  (abort with ^G)
> 1> dbg:tracer().
> {ok,<0.33.0>}
> 2> dbg:tpl('_', '_', '_', dbg:fun2ms(fun(_) -> return_trace() end)).
> {ok,[{,5905},{saved,1}]}
> 3> dbg:get_tracer().
> {ok,<0.34.0>}
> 4> process_info(list_to_pid("<0.34.0>")).
> [{current_function,{dbg,tracer_loop,2}},
>  {initial_call,{erlang,apply,2}},
>  {status,waiting},
>  {message_queue_len,0},
>  {messages,[]},
>  {links,[<0.33.0>]},
>  {dictionary,[]},
>  {trap_exit,true},
>  {error_handler,error_handler},
>  {priority,max},
>  {group_leader,<0.24.0>},
>  {total_heap_size,233},
>  {heap_size,233},
>  {stack_size,3},
>  {reductions,5},
>  {garbage_collection,[{fullsweep_after,65535},{minor_gcs,0}]},
>  {suspending,[]}]
> 5> dbg:p(list_to_pid("<0.24.0>"), clear).
> {ok,[{,1}]}
> 6> dbg:p(all, [c]).
> (<0.33.0>) call lists:map(#Fun<dbg.25.124365820>,[])
> (<0.33.0>) returned from lists:map/2 -> []
> (<0.33.0>) call dbg:reply(<0.31.0>,{ok,[{,25}]})
> (<0.33.0>) returned from dbg:reply/2 -> {dbg,
>                                         {ok,[{,25}]}}
> {ok,[{,25}]}
>
> Any idea on what was wrong?
>
> Thanks,
>
> kaiduan
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>


More information about the erlang-questions mailing list