我正在尝试开始使用 erlang:trace/3
和 dbg
模块可在不关闭服务器的情况下跟踪实时生产系统的行为。
documentation是 opaque (温和地说)而且网上似乎没有任何有用的教程。
我花了一整天的时间试图通过尝试将跟踪应用于 Module:Function
来捕获特定函数中发生的情况。使用dbg:c
和dbg:p
但根本没有成功。
有人对如何在实时 Erlang 系统中使用跟踪有简洁的解释吗?
最佳答案
在非事件节点上跟踪函数调用的基本步骤:
> dbg:start(). % start dbg
> dbg:tracer(). % start a simple tracer process
> dbg:tp(Module, Function, Arity, []). % specify MFA you are interested in
> dbg:p(all, c). % trace calls (c) of that MFA for all processes.
... trace here
> dbg:stop_clear(). % stop tracer and clear effect of tp and p calls.
您可以同时跟踪多个函数。通过为每个函数调用 tp
来添加函数。如果您想跟踪非导出函数,则需要调用tpl
。要删除函数,请以类似的方式调用 ctp
或 ctpl
。一些常见的 tp 调用是:
> dbg:tpl(Module, '_', []). % all calls in Module
> dbg:tpl(Module, Function, '_', []). % all calls to Module:Function with any arity.
> dbg:tpl(Module, Function, Arity, []). % all calls to Module:Function/Arity.
> dbg:tpl(M, F, A, [{'_', [], [{return_trace}]}]). % same as before, but also show return value.
最后一个参数是匹配规范。您可以使用 dbg:fun2ms
来解决这个问题。
您可以通过调用 p() 选择要跟踪的进程。这些项目在 erlang:trace 下描述。一些调用是:
> dbg:p(all, c). % trace calls to selected functions by all functions
> dbg:p(new, c). % trace calls by processes spawned from now on
> dbg:p(Pid, c). % trace calls by given process
> dbg:p(Pid, [c, m]). % trace calls and messages of a given process
我猜你永远不需要直接调用 erlang:trace
,因为 dbg
几乎为你做了所有事情。
事件节点的黄金法则是仅向 shell 生成一定量的跟踪输出,这样您就可以输入 dbg:stop_clear()。
。 :)
我经常使用跟踪器,它会在发生许多事件后自动停止。例如:
dbg:tracer(process, {fun (_,100) -> dbg:stop_clear();
(Msg, N) -> io:format("~p~n", [Msg]), N+1 end, 0
}).
如果您要在远程节点(或多个节点)上进行调试,请搜索 pan
、eper
、inviso
或 onviso
。
关于debugging - 如何在Erlang中使用trace和dbg来调试和跟踪我的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1954894/