Kshitij Mittal
Kshitij Mittal

Reputation: 2776

How to find where a process started in elixir

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.

Upvotes: 3

Views: 1881

Answers (3)

Roger Lipscombe
Roger Lipscombe

Reputation: 91925

[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()).

Upvotes: 1

Hamidreza Soleimani
Hamidreza Soleimani

Reputation: 2554

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.

Upvotes: 0

Sasha Fonseca
Sasha Fonseca

Reputation: 2311

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

Upvotes: 0

Related Questions