作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下是在 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/
以下是在 shell 中使用 redbug 的一些消息: % 02:49:02 ({cowboy_protocol,init,4}) % func1:start(>, [some]) % 02:49:
我是一名优秀的程序员,十分优秀!