Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find where a process started in elixir

Tags:

erlang

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.

like image 899
Kshitij Mittal Avatar asked Dec 30 '25 09:12

Kshitij Mittal


1 Answers

[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()).
like image 136
Roger Lipscombe Avatar answered Jan 02 '26 11:01

Roger Lipscombe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!