From 460cb28eda044c8dd8ce28ac6bc36bbc49373c8a Mon Sep 17 00:00:00 2001 From: Steve Vinoski Date: Fri, 29 Mar 2013 13:30:09 -0400 Subject: update sys:get_status/2,3 documentation for gen_event Modify the documentation for the sys:get_status/2,3 functions to reflect that they also work on gen_event processes, and add a cross reference for gen_event:format_status/2 to go along with the existing cross references for gen_server and gen_fsm. --- lib/stdlib/doc/src/sys.xml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/stdlib/doc/src') diff --git a/lib/stdlib/doc/src/sys.xml b/lib/stdlib/doc/src/sys.xml index 073faf2df2..bb75efb5e7 100644 --- a/lib/stdlib/doc/src/sys.xml +++ b/lib/stdlib/doc/src/sys.xml @@ -211,14 +211,16 @@

Gets the status of the process.

The value of Misc varies for different types of processes. For example, a gen_server process returns - the callback module's state, and a gen_fsm process - returns information such as its current state name. Callback - modules for gen_server and gen_fsm can also - customise the value of Misc by exporting - a format_status/2 function that contributes - module-specific information; - see gen_server:format_status/2 - and gen_fsm:format_status/2 + the callback module's state, a gen_fsm process + returns information such as its current state name and state data, + and a gen_event process returns information about each of its + registered handlers. Callback modules for gen_server, + gen_fsm, and gen_event can also customise the value + of Misc by exporting a format_status/2 + function that contributes module-specific information; + see gen_server:format_status/2, + gen_fsm:format_status/2, and + gen_event:format_status/2 for more details.

-- cgit v1.2.3 From 876b3644ecfac12accf36fbf0d0625e3ac4f6498 Mon Sep 17 00:00:00 2001 From: Steve Vinoski Date: Thu, 28 Mar 2013 20:34:52 -0400 Subject: add sys:get_state/1,2 and sys:replace_state/2,3 At Erlang Factory 2013 there was discussion during one of the talks about the sys:get_status functions and how useful they were for debugging. Geoff Cant mentioned it would be very useful if the sys module also provided functions to use while debugging to get just the state of a process and also to be able to replace the state of a process, and many others in the audience appeared to agree. The sys:get_state/1,2 functions return the state of a gen_server, gen_fsm, or gen_event process. The return value varies depending on the process type: process state for a gen_server, state name and state data for a gen_fsm, and handler module, handler id, and handler state for each handler registered in a gen_event process. The sys:replace_state/2,3 functions allow the state of a gen_server, gen_fsm, or gen_event process to be replaced with a new state. These functions take a function argument that updates or replaces the process state; using a function to change the state eliminates the race condition of first reading the state via sys:get_state/1 or sys:get_state/2, using its return value to create a new state, and then replacing the old state with the new state, since during that time the process might have received other calls or messages that could have changed its state. * For a gen_server process, the state replacement function takes the process state as an argument and returns a new state. * For a gen_fsm process, the state replacement function gets a tuple of {StateName, StateData} and returns a similar tuple that specifies a new state name, new state data, or both. * For a gen_event process, the state replacement function is called for each registered event handler. It gets a tuple {Module, Id, HandlerState} and returns a similar tuple that specifies the same Module and Id values but may specify a different value for HandlerState. If the state replacement function crashes or results in an error, the original state of a gen_server or gen_fsm process is maintained; if such a crash occurs for a gen_event process, the original state of the event handler for which the state replacement function was called is maintained, but the states of other event handlers of the same gen_event process may still be updated if no errors or crashes occur while replacing their states. Add documentation for sys:get_state/1,2 and sys:replace_state/2,3. The documentation explicitly notes that the functions are intended for use during debugging. Add new tests for these functions to gen_server_SUITE, gen_fsm_SUITE, and gen_event_SUITE. --- lib/stdlib/doc/src/sys.xml | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'lib/stdlib/doc/src') diff --git a/lib/stdlib/doc/src/sys.xml b/lib/stdlib/doc/src/sys.xml index bb75efb5e7..a177b80739 100644 --- a/lib/stdlib/doc/src/sys.xml +++ b/lib/stdlib/doc/src/sys.xml @@ -224,6 +224,73 @@ for more details.

+ + + + Get the state of the process + +

Gets the state of the process.

+ +

These functions are intended only to help with debugging. They are provided for + convenience, allowing developers to avoid having to create their own state extraction + functions and also avoid having to interactively extract state from the return values of + get_status/1 or + get_status/2 while debugging.

+
+

The value of State varies for different types of + processes. For a gen_server process, the returned State + is simply the callback module's state. For a gen_fsm process, + State is the tuple {CurrentStateName, CurrentStateData}. + For a gen_event process, State a list of tuples, + where each tuple corresponds to an event handler registered in the process and contains + {Module, Id, HandlerState}, where Module is the event handler's module name, + Id is the handler's ID (which is the value false if it was registered without + an ID), and HandlerState is the handler's state.

+

To obtain more information about a process, including its state, see + get_status/1 and + get_status/2.

+
+
+ + + + Replace the state of the process + +

Replaces the state of the process, and returns the new state.

+ +

These functions are intended only to help with debugging, and they should not be + be called from normal code. They are provided for convenience, allowing developers + to avoid having to create their own custom state replacement functions.

+
+

The StateFun function provides a new state for the process. + The State argument and NewState return value + of StateFun vary for different types of processes. For a + gen_server process, State is simply the callback module's + state, and NewState is a new instance of that state. For a + gen_fsm process, State is the tuple + {CurrentStateName, CurrentStateData}, and NewState + is a similar tuple that may contain a new state name, new state data, or both. + For a gen_event process, State is the tuple + {Module, Id, HandlerState} where Module is the event handler's module name, + Id is the handler's ID (which is the value false if it was registered without + an ID), and HandlerState is the handler's state. NewState is a + similar tuple where Module and Id shall have the same values as in + State but the value of HandlerState may be different. Returning + a NewState whose Module or Id values differ from those of + State will result in the event handler's state remaining unchanged. For a + gen_event process, StateFun is called once for each event handler + registered in the gen_event process.

+

If a StateFun function decides not to effect any change in process + state, then regardless of process type, it may simply return its State + argument.

+

If a StateFun function crashes or throws an exception, then + for gen_server and gen_fsm processes, the original state of the process is + unchanged. For gen_event processes, a crashing or failing StateFun + function means that only the state of the particular event handler it was working on when it + failed or crashed is unchanged; it can still succeed in changing the states of other event + handlers registered in the same gen_event process.

+
+
-- cgit v1.2.3