开发者

Avoiding output of huge binary params when tracing with dbg

I have to debug some code that pa开发者_开发问答sses 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).


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜