gpt4 book ai didi

debugging - Eper redbug,打印消息中的行号

转载 作者:行者123 更新时间:2023-12-03 06:56:00 25 4
gpt4 key购买 nike

以下是在 shell 中使用 redbug 的一些消息:

% 02:49:02 <0.116.0>({cowboy_protocol,init,4})
% func1:start(<<"/second">>, [some])

% 02:49:02 <0.116.0>({cowboy_protocol,init,4})
% func1:looper(<<"/home/second">>, #{data => []}])

有没有办法也打印 redbug 消息中的行号?

redbug:help() 确实显示了这一点:

print_fun    ()            custom print handler, fun/1 or fun/2;
fun(TrcMsg) -> <ignored>
fun(TrcMsg,AccOld) -> AccNew

但是没有很好的解释如何使用它,所以我无法尝试查看是否可以向消息添加行号

最佳答案

您似乎无法以任何直接的方式做到这一点。

检查这一点的最简单方法是打印您在 print_fun 中收到的所有数据

1> PrintFun = fun (Msg) -> io:format( ">>> ~p~n" , [Msg]) end.
#Fun<erl_eval.6.90072148>
2> redbug:start("erlang" , [{print_fun, PrintFun}]).
{30,249}
>>> {call,{{erlang,demonitor,[#Ref<0.0.0.40>]},<<>>},
{<0.33.0>,{erlang,apply,2}},
{11,40,31,554200}}
>>> {call,{{erlang,atom_to_list,['PrintFun']},<<>>},
{<0.33.0>,{erlang,apply,2}},
{11,40,31,554210}}
>>> {call,{{erlang,group_leader,[]},<<>>},
{<0.33.0>,{erlang,apply,2}},
{11,40,31,554213}}
>>> {call,{{erlang,monitor,[process,<0.26.0>]},<<>>},
{<0.33.0>,{erlang,apply,2}},
{11,40,31,554215}}
>>> {call,{{erlang,port_control,[#Port<0.491>,101,[]]},<<>>},
{<0.24.0>,user_drv},
{11,40,31,554231}}
>>> {call,{{erlang,module_loaded,[calendar]},<<>>},
{<0.20.0>,code_server},
{11,40,31,554257}}
>>> {call,{{erlang,atom_to_list,[calendar]},<<>>},
{<0.20.0>,code_server},
{11,40,31,554263}}
>>> {call,{{erlang,'++',["calendar",".beam"]},<<>>},
{<0.20.0>,code_server},
{11,40,31,554265}}
>>> {call,{{erlang,'++',["ebin","/calendar.beam"]},<<>>},
{<0.20.0>,code_server},
{11,40,31,554268}}
>>> {call,{{erlang,whereis,[erl_prim_loader]},<<>>},
{<0.20.0>,code_server},
{11,40,31,554270}}
redbug done, msg_count - 10

如您所见,您得到的只是 MFA({Module, Function, Arguments})、调用过程和时间戳。

要获得实际的函数调用行,您必须使用 beam_lib 模块深入研究附加到 Beam 文件(如果有)的 debug_info 。我认为最好的方法是使用 beam_lib:chunks( Module, [abstract_code]).,就像这样

{ok,{redbug,[{abstract_code,{raw_abstract_v1,[{attribute,1,
file,
{"src/redbug.erl",1}},
{attribute,9,module,redbug},
{attribute,11,export,[{help,0}]},
{attribute,13,export,[{unix,1}]},
{attribute,15,export,
[{start,1},{start,2},{start,3},{start,4},{start,5}]},
{attribute,16,export,[{stop,0}]},
{attribute,1,file,{"src/log.hrl",1}},
{function,17,'?log',2,
[{clause,17,[{var,17,...},{var,...}],[[{...}]],[{...}]},
{clause,18,[{var,...},{...}],[],[...]}]},
{attribute,19,file,{"src/redbug.erl",19}},
{attribute,22,record,
{cnf,[{record_field,24,{...},...},
{record_field,25,...},
{record_field,...},
{...}|...]}},
{function,57,help,0,[{clause,57,[],...}]},
{function,123,unix,1,
[{clause,123,...},{clause,...},{...}|...]},
{function,146,to_term,1,[{clause,...},{...}]},
{function,154,maybe_halt,1,[{...}]},
{function,160,is_in_shell,0,[...]},
{function,167,stop,0,...},
{function,174,start,...},
{function,176,...},
{function,...},
{...}|...]}}]}}

在那里你可以找到列表,在其中你可以找到像 {function, LineNumber, FunctionName, Arity, FunctionCodeAsList } 这样的元组。因此,通过浏览此列表并找到您正在寻找的函数,您可以提取 LineNumber

您仍然需要考虑一些可能不起作用的事情。

  • 您正在分析光盘中的实际文件,因此它需要位于您的当前目录中。它与您的虚拟机是否加载以及加载什么内容(什么代码版本)无关。因此,您可能需要做一些工作才能真正找到这个 .beam
  • 在没有debug_info的情况下编译的文件可能无法生成此抽象语法树。您需要确定您的情况是否属于这种情况,以及您希望如何处理。
  • 某些光束可能已加密,有些可能存在其他问题。您应该阅读beam_lib module documentation只是为了感受一下您正在处理的事情。

如果您确实想出一些东西,请分享。黑客快乐!

关于debugging - Eper redbug,打印消息中的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com