如何将 erlang dbg 附加到 运行 进程?

How to attach erlang dbg to a running process?

如何将调试器附加到 运行 erlang 进程 (rabbitmq)?我有 运行 相同兔子版本的源代码。我想在源代码行上设置断点,并将调试器附加到 运行 rabbit 实例。我不确定 erlang 是否需要调试符号 async_dirty.

在一个完美的世界里,我希望能够在本地和远程都做到这一点。

您可以使用图形 debugger or in shell using int 模块。 需要使用 debug_info 选项编译模块。

可以使用 debugger:start(全局)或连接到远程 shell (^G) 从连接的节点远程调试进程。

根据您的说法,您并不真的需要 运行 调试器。 Erlang VM 的并发模型不太适合停止一切并检查式调试的概念。

另一方面,VM 具有强大的内置跟踪功能。 dbg 模块是它们全部公开的地方,但是该模块的接口很难使用,尤其是如果您是初学者。 我建议使用 recon_trace 来了解您的流程的进展情况:http://ferd.github.io/recon/recon_trace.html.

如果您不想安装 recon,请从 Erlang shell 启动您的程序,然后在 shell 中键入:

%enable tracing capabilities
1> dbg:tracer().  

% Trace Pattern Local-scope 
% (tell the tracer to trace every call in YourModule, even unexported functions).
2> dbg:tpl(YourModule, x). 

% Tell dbg to print calls from all processes calling your module.
3> dbg:p(all,call). 

% Run your traced module
4> YourModule:SomeFun().  

% You should see nice(?) traces of inputs, outputs, and
% exceptions in your shell

查看以下会话,我在其中摸索着不知道如何使用 queue 模块:

Eshell V6.3  (abort with ^G)
1> dbg:tracer(), dbg:tpl(queue, x), dbg:p(all, call).
{ok,[{matched,nonode@nohost,26}]}
2> X = queue:new().
(<0.33.0>) call queue:new()
(<0.33.0>) returned from queue:new/0 -> {[],[]}
{[],[]}
3> X = queue:cons(1).
** exception error: undefined function queue:cons/1
4> X = queue:cons(X,1).
(<0.39.0>) call queue:cons({[],[]},1)
(<0.39.0>) call queue:in_r({[],[]},1)
(<0.39.0>) exception_from {queue,in_r,2} {error,badarg}
(<0.39.0>) exception_from {queue,cons,2} {error,badarg}
** exception error: bad argument
     in function  queue:in_r/2
        called as queue:in_r({[],[]},1)
5> X = queue:cons(1,X).
(<0.41.0>) call queue:cons(1,{[],[]})
(<0.41.0>) call queue:in_r(1,{[],[]})
(<0.41.0>) returned from queue:in_r/2 -> {[],[1]}
(<0.41.0>) returned from queue:cons/2 -> {[],[1]}
** exception error: no match of right hand side value {[],[1]}
6> X1 = queue:cons(1,X).
(<0.43.0>) call queue:cons(1,{[],[]})
(<0.43.0>) call queue:in_r(1,{[],[]})
(<0.43.0>) returned from queue:in_r/2 -> {[],[1]}
(<0.43.0>) returned from queue:cons/2 -> {[],[1]}
{[],[1]}

希望这有助于您入门。

如果您想了解 Erlang 奇才是如何做到的,请观看 Mats Cronqvist's talk "Taking the printf out of printf debugging"