Getting gen_server/gen_fsm state for debugging

痞子三分冷 提交于 2020-01-01 02:16:07

问题


Is it possible to obtain the current state of a gen_server process (presumably by sending some system message)? It could be useful when debugging.

Of course, I can add a message which returns the current state to handle_call:

get_state(Server) -> gen_server:call(Server, '$get_state').

%% in every gen_server I want to debug
...
handle_call('$get_state', _From, State) ->
  {reply, State, State};
...

but is there something built-in (even if it is a bit hacky)?


回答1:


Use sys:get_status/1,2 function. It's definition is:

get_status(Name,Timeout) -> 
    {status, Pid, {module, Mod}, [PDict, SysState, Parent, Dbg, Misc]}

SysState will contain state of the process. It works for all processes using OTP behaviors and other processes implementing proc_lib and sys requirements.



来源:https://stackoverflow.com/questions/4254070/getting-gen-server-gen-fsm-state-for-debugging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!