https://gist.github.com/0acc83b3db29dc78dde4
{_,Pid,_,_} = sys:get_status(riak_repl2_rtq).
rp(process_info(Pid, [backtrace])).
- Get full stack trace, prevent truncating of the map
rp(erlang:registered()).
- Write a single output to the file, from a list comprehension
file:write_file("/tmp/state.txt", io_lib:format("~p~n",[Res]), [append] ).
The full list comprehension
[
file:write_file("/tmp/state.txt",
io_lib:format("== ~s ~n~p~n",[X,Y])
, [append] )
|| {X,Y} <-
[ {"foo", sys:get_status(riak_core_cluster_manager)} ]
].
[ io:format("== ~s ~n~p~n",[X,Y]) || {X,Y} <- [ {"foo", sys:get_status(riak_core_cluster_manager)} ] ] .
[ io:format("~p~n",[X]) || X <- [ sys:get_status(riak_core_cluster_manager) ] ] .
[ X <-
file:write_file("/tmp/foo", io_lib:format("~p~n",
[
{ "\n riak_core_cluster_manager: \n" , sys:get_status(riak_core_cluster_manager) } ,
{ "\n riak_core_connection_manager: \n" , sys:get_status(riak_core_connection_manager)} ,
{ "\n riak_core_service_manager: \n" , sys:get_status(riak_core_service_manager) } ,
{ "\n riak_repl2_leader_gs: \n" , sys:get_status(riak_repl2_leader_gs) } ,
{ "\n riak_repl_leader_gs): \n" , sys:get_status(riak_repl_leader_gs) }
]
)).
{"riak_repl2_fscoordinator ", sys:get_status(riak_repl2_fscoordinator) },
{"riak_repl2_fscoordinator_serv",sys:get_status(riak_repl2_fscoordinator_serv) },
{"riak_repl2_rtq ", sys:get_status(riak_repl2_rtq) }
Cmd = fun(X) -> try {success,sys:get_status(X)} catch Err:Type -> {error, io_lib:format("~p", [Type]) } end end.
[
file:write_file("/tmp/state.txt",
io_lib:format("== ~s ~n~p~n",[X, try
sys:get_status(X) catch Err:Type -> io_lib:format("~p", [Type]) end])
, [append] )
|| X <-
[
riak_core_cluster_manager ,
riak_core_connection_manager ,
riak_core_service_manager ,
riak_repl2_leader_gs ,
riak_repl_leader_gs ,
riak_repl2_fscoordinator ,
riak_repl2_fscoordinator_serv ,
riak_repl2_rtq
]
].
- Nested function definitions
Outer = fun(Name) ->
Cmd = fun(X) -> try
{success,sys:get_status(X)}
catch Err:Type -> {error, io_lib:format("~p", [Type]) }
end
end,
Cmd(Name)
end.
StatusF = fun(Name) -> Cmd = fun(X) -> try {success,sys:get_status(X)} catch Err:Type -> {error, io_lib:format("~p", [Type]) } end end, Cmd(Name) end.
{success,{status,Pid,_,_}} = StatusF(riak_repl2_rtq).
Into a case statement
case StatusF(riak_repl2_rtq) of {success,{status,Pid,_,_}} -> true end.
With handling
case StatusF(riak_repl2_rtq) of {success,{status,Pid,Module,Status}} -> true; _ -> false end.
Return to the backtrace function
erlang:process_info(Pid, [backtrace])
Create a function to safely grab the backtrace
PInfoF = fun(Name) -> Cmd = fun(Pid) -> try {success,erlang:process_info(Pid, [backtrace]) } catch Err:Type -> {error, io_lib:format("~p", [Type]) } end end, Cmd(Name) end.
Put it all together, the final working version
StatusF = fun(Name) -> Cmd = fun(X) -> try {success,sys:get_status(X)} catch Err:Type -> {error, io_lib:format("~p", [Type]) } end end, Cmd(Name) end.
PInfoF = fun(Name) -> Cmd = fun(Pid) -> try {success,erlang:process_info(Pid, [backtrace]) } catch Err:Type -> {error, io_lib:format("~p", [Type]) } end end, Cmd(Name) end.
[
file:write_file("/tmp/state.txt",
io_lib:format("== ~s ~n~p~n",[X,
case StatusF(X) of {success,{status,Pid,Module,Status}} ->
{success, {X, PInfoF(Pid),Module,Status}}
; _ ->
{fail, cannot_access_process,X }
end ]) , [append] )
|| X <-
[
riak_core_cluster_manager ,
riak_core_connection_manager ,
riak_core_service_manager ,
riak_repl2_leader_gs ,
riak_repl_leader_gs ,
riak_repl2_fscoordinator ,
riak_repl2_fscoordinator_serv ,
riak_repl2_rtq
]
].