【问题标题】:Only log specific arguments in a call trace when tracing a function using `dbg`?使用`dbg`跟踪函数时仅在调用跟踪中记录特定参数?
【发布时间】:2016-11-30 11:54:03
【问题描述】:

我正在尝试使用dbg 记录对函数的所有调用以进行调试(感谢this 的回答)。代码如下:

-module(a).
-export([main/0]).

trace_me(_, _, _) ->
  ok.

main() ->
  dbg:start(),
  dbg:tracer(),
  dbg:tpl(a, trace_me, 3, []),
  dbg:p(all, c),
  LargeBinary = binary:copy(<<"foo">>, 10000),
  trace_me(foo, bar, LargeBinary).

问题是其中一个参数是一个非常大的二进制文件,上面的代码在每次调用时都会打印完整的二进制文件:

1> c(a).
{ok,a}
2> a:main().
(<0.57.0>) call a:trace_me(foo,bar,<<"foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo...lots of foos omitted...">>)
ok

是否可以(不修改trace_me/3):

  1. 每次调用只打印前 2 个参数?

  2. 打印前 2 个参数 + 第三个参数的前几个字节,还是在打印前通过自定义函数传递第三个参数?

【问题讨论】:

  • 你用 redbug 试过我的建议了吗,还是你真的需要 dbg?
  • @marco.m 抱歉,我没有机会更新您的信息。我真的想要一个不添加任何新依赖项的解决方案。感谢您发布的视频,我深入研究了 redbug 和 dbg 的实现,并编写了一个小函数,它使用跟踪器 API 来做我想做的事。我会尽快在此处发布解决方案。

标签: erlang


【解决方案1】:

我不知道用dbg,但是你可以用非常好的和小的redbug

trace_me(_, _, _) ->
    ok.

do_redbug() ->
    % 1. print arguments as dbg:
    % redbug:start("a:trace_me"),

    % 2. print arity only:
    % redbug:start("a:trace_me", [arity]),

    % 3. Specify the formatting depth:
    redbug:start("a:trace_me", [{print_depth, 10}]),

    LargeBinary = binary:copy(<<"foo">>, 100000),
    trace_me(foo, bar, LargeBinary).

查看上述链接中的(相当简洁的)redbug 文档,了解更多选项以及如何传递您自己的打印机/格式化程序函数。

还请注意,与 dbg 不同,redbug 在生产系统上使用是安全的。查看 redbug 作者的此演示文稿:Taking the printf out of printf Debugging

【讨论】:

  • 我最终使用了我现在编写的一个小模块:gist.github.com/3cc6b76e9a58823c763e3f000ccf4bed。它不处理元组等其他事物中的大型二进制文件之类的情况,但我不想向这个玩具项目添加依赖项,这已经足够了。 Redbug 看起来像是一个更强大的调试生产应用程序的解决方案。感谢您的推荐以及演示视频链接。
猜你喜欢
  • 2019-06-05
  • 2015-11-07
  • 2014-05-23
  • 2018-04-20
  • 2018-10-26
  • 1970-01-01
  • 2016-05-12
  • 2014-10-11
  • 1970-01-01
相关资源
最近更新 更多