首页 > 解决方案 > 如何在 Erlang 中调试进程

问题描述

你好,我正在运行一个问题,我从一个链接的进程中得到一个错误。我不知道如何调试进程中出了什么问题。我已启用process_flag(trap_exit,true),但仍然无法弄清楚出了什么问题。

错误

30> conc2:initFam().                 
<0.170.0>
=ERROR REPORT==== 24-Aug-2019::07:55:03.403000 ===
Error in process <0.170.0> with exit value:
{undef,[{conc2,brother,[],[]}]}

如何读取错误?那么undef返回元组中的内容是什么,元[]组的第二个元素中的两个是什么?我可以理解方法中发生的某些事情,brother但我无法理解更多。

如何在 Erlang 中调试进程?

-module(conc2).
-compile([debug_info]).
-export([initFam/0]).


initFam()->
    Bid=spawn(?MODULE,brother,[]),
    Bid. 



brother()->

    Sid=spawn(?MODULE,sister,[self()]),
    link(Sid),
    brotherLoop(Sid).

brotherLoop(Sid)->
    receive   
        kill->Sid ! issue_kill;
        Msg->[Msg|brotherLoop(Sid)]
    end.

sister()->
    receive
        MSG ->
             exit(killed_by_bro)
    end.

基本上,我生成了一个进程,该进程又生成了另一个进程并链接到它,并且第一个进程被递归调用以侦听终止消息。

稍后编辑:
我也尝试将 Shell 传递给brother进程,PID以查看它在哪一行崩溃,但我仍然无法收到消息:

initFam()->
    Bid=spawn(?MODULE,brother,[self()]),
    Bid. 



brother(Shell)->
    Shell! i_m_here,
    Sid=spawn(?MODULE,sister,self()),
    link(Sid),
    brotherLoop(Sid).`

如您所见,我仍然无法收到来自 的任何消息brother,我不应该在它崩溃之前收到消息吗?

41> conc2:initFam().                 
<0.203.0>
=ERROR REPORT==== 24-Aug-2019::08:13:31.662000 ===
Error in process <0.203.0> with exit value:
{undef,[{conc2,brother,[<0.196.0>],[]}]}
42> flush().
ok

标签: debuggingprocesserlang

解决方案


当您发布问题时,您需要显示您在 shell 中所做的一切,以获得您发布的结果。例如,您没有显示 shell 命令来编译您的模块。如果你有,它会显示这个输出:

1> c(conc2).
conc2.erl:12: Warning: function brother/0 is unused
conc2.erl:18: Warning: function brotherLoop/1 is unused
conc2.erl:24: Warning: function sister/0 is unused
conc2.erl:26: Warning: variable 'MSG' is unused

是什么让你认为那不重要?未使用的警告brother/0意味着模块内定义的任何函数都不会调用该函数,并且由于brother/0未导出,因此模块外定义的任何函数都无法调用brother/0,因此brother/0永远无法执行。

当您spawn()使用函数时,必须导出该函数,因此会undef出现错误消息。


推荐阅读