0
votes

Hello i am running a problem and i get an error from a linked process.I do not know how to debug what went wrong in a process. I have enabled process_flag(trap_exit,true) but still can't figure out what is wrong.

Error

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,[],[]}]}

How can the error be read?So what is undef in the return tuple,and what are the two [] in the second element of the tuple ? I can understand something happened in brother method but more i can't understand.

How do you debug a process in 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.

Basically i spawn a process that in turn spawns another and links to it , and this first process gets called recursively in order to listen for kill messages.

Later Edit:
I have also tried to pass to the brother process the PID of the Shell in order to see at what line it crashes , but i still can not receive the message:

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



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

As you can see i still can not receive any message from the brother , shouldn't i get the message before it crashes?

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
3

3 Answers

1
votes

When you post a question, you need to show EVERYTHING you did in the shell to get the result you posted. For instance, you did not show the shell command to compile your module. If you had, it would have showed this output:

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

What made you think that was unimportant?? The warning that brother/0 is unused means that no function defined within the module calls that function, and because brother/0 isn't exported, no function defined outside the module can call brother/0, so brother/0 can never execute.

When you spawn() a function, the function has to be exported, hence the undef error message.

1
votes

You get undefined function error, most likely because you can't spawn a process if the function is not exported.

1
votes

For use spawn you need to export function what you try call by spawn. But if you need debug a module, you need do compile of module with flag debug_info, eg, of test.erl:

1> c(test, [debug_info]).

Then you need run debug:

2> debug:start().

Then you can choose your module for debugging. More info in manual: http://erlang.org/doc/apps/debugger/debugger_chapter.html