如何细化调试?

发布于 2024-10-16 14:01:33 字数 61 浏览 0 评论 0原文

崩溃报告 (SASL) 或多或少地给出了错误发生的位置和原因。 但是否有可能对其进行改进(函数、代码行等)?

Crash report (SASL) gives more or less where and why a bug happens.
But is it possible to refine this (the function, the line de code, etc) ?

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

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

发布评论

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

评论(2

红颜悴 2024-10-23 14:01:33

如果您可以重现该错误,获取更多信息的最佳方法是在有问题的部分上进行 dbg 跟踪并查看该输出。

dbg:tracer(),dbg:p(all,c),dbg:tpl(Mod,Func,x).

这通常对我有用。将 Mod 和 Func 替换为您想要调试的任何模块和函数。

如果您正在寻找更详细的事后日志记录,那么 sasl 和 error_logger 是您的朋友。当然,有时 SASL 无法为您提供足够的信息,如果这种情况在您的系统中经常发生,您可能应该学习更好地理解 SASL 输出或编写自己的日志处理程序。将您自己的错误处理程序插入 SASL 并根据需要输出内容非常容易。

然而,您永远不会获得行号,因为该信息在编译时被破坏,并且虚拟机无法知道哪一行崩溃了。然而,它确实知道哪个函数以及可能使用哪个参数,鉴于此,通常可以找出问题所在。除非你编写很长的函数,否则在我看来这是不好的代码味道,并且表明你应该将代码重构为更小的函数。

If you can reproduce the fault, the best way to get more information is to put a dbg trace on sections in question and review that output.

dbg:tracer(),dbg:p(all,c),dbg:tpl(Mod,Func,x).

This usually does the trick for me. Replace Mod and Func with whatever module and function you want to debug.

If you are looking for more detailed post-mortem logging then sasl and the error_logger are your friends. There are of course times when SASL does not give you enough info, if this happens a lot in your system you probably should either learn to understand the SASL output better or write your own log handler. It is quite easy to plug-in your own error handler into SASL and output things as you want.

You will however never get line number as that information is destroyed at compilation time and there is no way for the VM to know which line crashed. It does however know which function and possibly with which arguments, given this it is usually possible to find out where things went wrong. Unless you write very long functions, which IMO is bad code smell and a sign that you should refactor your code to smaller functions.

谁的年少不轻狂 2024-10-23 14:01:33

一般来说,没有。 erlang .beam 文件不包含原始代码的行号,因此很难知道问题发生在哪一行。我在项目中确实使用了许多宏,包括“log.hrl”:

-define(INFO(T), error_logger:info_report(T)).
-define(WARN(T), error_logger:warning_report(
    [process_info(self(), current_function), {line, ?LINE} | T])).
-define(ERR(T), error_logger:error_report(
    [process_info(self(), current_function), {line, ?LINE} | T])).

-define(DEBUG(Format, Args), io:format("D(~p:~p:~p) : "++Format++"~n",
                                   [self(),?MODULE,?LINE]++Args)).
-define(DEBUGP(Args), io:format("D(~p:~p:~p) : ~p~n",
                                   [self(),?MODULE,?LINE, Args])).

这确实为您提供了程序中的一些日志行以供查找。为了进行调试,我还经常使用 eper 套件中的 redbug 工具:

https://github.com/massemanet/eper

它允许您在发生呼叫时实时跟踪:

 Eshell V5.8.3  (abort with ^G)
 1> redbug:start("erlang:now() -> stack;return", [{time, 60*1000}]).
 ok
 2> erlang:now().
 {1297,183814,756227}

 17:50:14 <{erlang,apply,2}> {erlang,now,[]}
   shell:eval_loop/3 
   shell:eval_exprs/7 
   shell:exprs/7 

 17:50:14 <{erlang,apply,2}> {erlang,now,0} -> {1297,183814,756227}
 3> 

我希望这会有所帮助。

In general, no. The erlang .beam files does not contain the line numbers from the original code, so it is hard to know at what line the problem occurred. I do have a number of macros I use in my project, included as "log.hrl":

-define(INFO(T), error_logger:info_report(T)).
-define(WARN(T), error_logger:warning_report(
    [process_info(self(), current_function), {line, ?LINE} | T])).
-define(ERR(T), error_logger:error_report(
    [process_info(self(), current_function), {line, ?LINE} | T])).

-define(DEBUG(Format, Args), io:format("D(~p:~p:~p) : "++Format++"~n",
                                   [self(),?MODULE,?LINE]++Args)).
-define(DEBUGP(Args), io:format("D(~p:~p:~p) : ~p~n",
                                   [self(),?MODULE,?LINE, Args])).

and this does give you some log lines in the program to hunt for. For debugging I often also use the redbug tool from the eper suite:

https://github.com/massemanet/eper

It allows you to trace in realtime whenever a call happens:

 Eshell V5.8.3  (abort with ^G)
 1> redbug:start("erlang:now() -> stack;return", [{time, 60*1000}]).
 ok
 2> erlang:now().
 {1297,183814,756227}

 17:50:14 <{erlang,apply,2}> {erlang,now,[]}
   shell:eval_loop/3 
   shell:eval_exprs/7 
   shell:exprs/7 

 17:50:14 <{erlang,apply,2}> {erlang,now,0} -> {1297,183814,756227}
 3> 

I hope this helps.

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