3

I am using observer in elixir and the following is the snapshot of an Application [under applications tab]:

snapshot of observer

I need to exit these processes once their work is done. Somehow, I am not able to figure out where some of the processes are originating. Is there a way in elixir/erlang to figure out the module/function where a particular process was created?

Suggestions will be highly appreciated. Thanks.

3 Answers 3

1

First you must always have the process's PID or its reference name.

Process.info/2

will give you information about that Process. You may get more documentation and information on how this function works in the Erlang's function it is calling:

process_info-2

There are also arity 1 variants: Process Docs

1
[erlang:process_info(Pid, initial_call) || Pid <- erlang:processes()].

But note that gen_server, etc., all have the same initial call, so you need to dig a little deeper.

The following is adapted from https://gist.github.com/rlipscombe/a8e87583d47799170f8b:

lists:map(
    fun(Pid) ->
        InitialCall = case erlang:process_info(Pid, initial_call) of
            {initial_call,{proc_lib,init_p,A}} ->
                case erlang:process_info(Pid, dictionary) of
                    {dictionary, D} ->
                        proplists:get_value('$initial_call', D, undefined);    
                    _ ->
                        {proc_lib,init_p,A}
                end;
            {initial_call,{erlang,apply,A}} ->
                case erlang:process_info(Pid, current_function) of
                    {current_function,MFA} -> MFA;
                    _ -> {erlang,apply,A}
                end;
            {initial_call,IC} ->
                IC;
            Other ->
                Other
        end,
        {Pid, InitialCall}
    end, erlang:processes()).
0

Using process_info/1 you can get a list of process information from which initial_call and current_function could help you to find the initial function call with which the process was spawned and the current function call of the process respectively.

Also process_info(Pid, initial_call) and process_info(Pid, current_function) functions are using as shortcut.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.