使用 dbg 跟踪时避免输出巨大的二进制参数

发布于 2024-11-15 11:47:18 字数 249 浏览 6 评论 0原文

我必须调试一些在参数中传递巨大二进制文件的代码。

为此,我想使用 dbg:tracer/0 、 dbg:p/2 、 dbg:tpl/3 的组合。

但是,如果我这样做,每次都会输出所有二进制文件,这会弄乱输出,因此很难找到重要信息。

更糟糕的是,这些二进制文件的输出扰乱了代码的时间。这使得它的行为完全不同,以至于我无法重现我想要的 dbg 行为。

我仍然想查看其他参数,但不需要查看二进制文件(缩短的二进制文件也可以)。

I have to debug some code that passes around huge binaries in the params.

For this I want to use a combination of dbg:tracer/0, dbg:p/2, dbg:tpl/3.

But if I do this all the binaries are output every time which for one messes up the output so the important info is hard to find.

What is even worse, the output of these binaries messes up the timing of the code. Which makes it behave sufficiently different that I can't reproduce the behaviour I want to dbg.

I still want to see the other params but don't need to see the binaries (shortened binaries would also be ok).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2024-11-22 11:47:18

您可能想使用如下内容:

-module(test).

-compile(export_all).

foo(_LongBinary, _OtherParam) ->
    ok.

test() ->
    dbg:tracer(process, {
         fun({trace, Pid, call, {M,F,A}}, _) ->
             NewA = lists:map(fun(<<Reduced:5/binary, _/binary>>) ->
                          Reduced;
                         (Else) ->
                          Else
                      end, A),
             erlang:display({Pid, "->", M, F, NewA});       
            ({trace, Pid, return_from, {M,F,A}, R}, _) ->
             erlang:display({Pid, "<-", M, F, A, R})        
         end, unused}),
    dbg:p(all,c),
    dbg:tpl(test, foo, x).

主要是,我使用的是 dbg:tracer 的替代版本,它带有两个参数。第一个是类型,可以是进程端口(有关更多详细信息,请参阅文档)。第二个参数是消息处理函数(实际上是包含处理函数和初始处理数据的元组),将为每个跟踪消息调用该函数。

在那里,您可以实现逻辑来截断超过一定长度的二进制文件或您需要执行的任何其他操作。

因此,您会得到类似的结果:

1> test:test().                               
{ok,[{matched,nonode@nohost,1},{saved,x}]}
2> test:foo(<<"aa">>,b).                      
ok
3> {<0.45.0>,"->",test,foo,[<<2 bytes>>,b]}
{<0.45.0>,"<-",test,foo,2,ok}
4> test:foo(<<"aaaaaaa">>,b).
ok
5> {<0.45.0>,"->",test,foo,[<<5 bytes>>,b]}
{<0.45.0>,"<-",test,foo,2,ok}
6> test:foo(<<"aaaaaaasdaaaaaaaaa">>,b).
ok
7> {<0.45.0>,"->",test,foo,[<<5 bytes>>,b]}
{<0.45.0>,"<-",test,foo,2,ok}

您可能还想截断返回值。您还可以查看 dbg 模块来模拟它们漂亮的打印(不幸的是,格式化函数不在那里导出)。

You might want to use something like this:

-module(test).

-compile(export_all).

foo(_LongBinary, _OtherParam) ->
    ok.

test() ->
    dbg:tracer(process, {
         fun({trace, Pid, call, {M,F,A}}, _) ->
             NewA = lists:map(fun(<<Reduced:5/binary, _/binary>>) ->
                          Reduced;
                         (Else) ->
                          Else
                      end, A),
             erlang:display({Pid, "->", M, F, NewA});       
            ({trace, Pid, return_from, {M,F,A}, R}, _) ->
             erlang:display({Pid, "<-", M, F, A, R})        
         end, unused}),
    dbg:p(all,c),
    dbg:tpl(test, foo, x).

Mainly, I'm using an alternative version of dbg:tracer, which takes two arguments. The first is the type and could be process or port (see doc for more details). The second parameter is a message handler function (actually a tuple containing the handler function and the initial handler data), which will be called for each trace message.

There, you can implement your logic to truncate binaries longer than a certain amount or whatever else you need to do.

So, you will get something like:

1> test:test().                               
{ok,[{matched,nonode@nohost,1},{saved,x}]}
2> test:foo(<<"aa">>,b).                      
ok
3> {<0.45.0>,"->",test,foo,[<<2 bytes>>,b]}
{<0.45.0>,"<-",test,foo,2,ok}
4> test:foo(<<"aaaaaaa">>,b).
ok
5> {<0.45.0>,"->",test,foo,[<<5 bytes>>,b]}
{<0.45.0>,"<-",test,foo,2,ok}
6> test:foo(<<"aaaaaaasdaaaaaaaaa">>,b).
ok
7> {<0.45.0>,"->",test,foo,[<<5 bytes>>,b]}
{<0.45.0>,"<-",test,foo,2,ok}

You might want to truncate return values as well. You could also look at the dbg module to emulate their pretty printing (unfortunately, formatting functions are not exported there).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文