A functional interface to system messages.
This module contains functions for sending system messages used by programs, and messages used for debugging purposes.
Functions used for implementation of processes are also expected to understand
system messages, such as debug messages and code change. These functions must be
used to implement the use of system messages for a process; either directly, or
through standard behaviors, such as gen_server.
The default time-out is 5000 ms, unless otherwise specified. timeout defines
the time to wait for the process to respond to a request. If the process does
not respond, the function evaluates exit({timeout, {M, F, A}}).
The functions make references to a debug structure. The debug structure is a
list of dbg_opt/0, which is an internal data type used by function
handle_system_msg/6. No debugging is performed if it is an empty list.
System Messages
Processes that are not implemented as one of the standard behaviors must still understand system messages. The following three messages must be understood:
Plain system messages. These are received as
{system, From, Msg}. The content and meaning of this message are not interpreted by the receiving process module. When a system message is received, functionhandle_system_msg/6is called to handle the request.Shutdown messages. If the process traps exits, it must be able to handle a shutdown request from its parent, the supervisor. The message
{'EXIT', Parent, Reason}from the parent is an order to terminate. The process must terminate when this message is received, normally with the sameReasonasParent.If the modules used to implement the process change dynamically during runtime, the process must understand one more message. An example is the
gen_eventprocesses. The message is{_Label, {From, Ref}, get_modules}. The reply to this message isFrom ! {Ref, Modules}, whereModulesis a list of the currently active modules in the process.This message is used by the release handler to find which processes that execute a certain module. The process can later be suspended and ordered to perform a code change for one of its modules.
System Events
When debugging a process with the functions of this module, the process
generates system_events, which are then treated in the debug function. For
example, trace formats the system events to the terminal.
Four predefined system events are used when a process receives or sends a message. The process can also define its own system events. It is always up to the process itself to format these events.
Summary
Types
See the introduction of this manual page.
Debug events produced by gen_server, gen_statem and gen_event
Callbacks: Process Implementation Functions
Called from handle_system_msg/6 when the process is to perform a code change.
The code change is used when the internal data structure has changed. This
function converts argument Misc to the new data structure. OldVsn is
attribute vsn of the old version of the Module. If no such attribute is
defined, the atom undefined is sent.
Called from handle_system_msg/6 when the process is to continue its execution
(for example, after it has been suspended). This function never returns.
Called from handle_system_msg/6 when the process is to return a term that
reflects its current state. State is the value returned by get_state/2.
Called from handle_system_msg/6 when the process is to replace its current
state. NState is the value returned by replace_state/3.
Called from handle_system_msg/6 when the process is to terminate. For example,
this function is called when the process is suspended and its parent orders
shutdown. It gives the process a chance to do a cleanup. This function never
returns.
Functions
Equivalent to change_code(Name, Module, OldVsn, Extra, 5000).
Tells the process to change code.
Equivalent to get_state(Name, 5000).
Gets the state of the process.
Equivalent to get_status(Name, 5000).
Gets the status of the process.
Equivalent to install(Name, FuncSpec, 5000).
Enables installation of alternative debug functions. An example of such a function is a trigger, a function that waits for some special event and performs some action when the event is generated. For example, turning on low-level tracing.
Equivalent to log(Name, Flag, 5000).
Turns the logging of system events on or off. If on, a maximum of N events are
kept in the debug structure (default is 10).
Equivalent to log_to_file(Name, FileName, 5000).
Enables or disables the logging of all system events in text format to the file.
The events are formatted with a function that is defined by the process that
generated the event (with a call to handle_debug/4). The file is opened with
encoding UTF-8.
Equivalent to no_debug(Name, 5000).
Turns off all debugging for the process. This includes functions that are
installed explicitly with function install/2,3, for example,
triggers.
Equivalent to remove(Name, FuncOrFuncId, 5000).
Removes an installed debug function from the process. Func or FuncId must be
the same as previously installed.
Equivalent to replace_state(Name, StateFun, 5000).
Replaces the state of the process, and returns the new state.
Equivalent to resume(Name, 5000).
Resumes a suspended process.
Equivalent to statistics(Name, Flag, 5000).
Enables or disables the collection of statistics. If Flag is get, the
statistical collection is returned.
Equivalent to suspend(Name, 5000).
Suspends the process. When the process is suspended, it only responds to other system messages, but not other messages.
Equivalent to terminate(Name, Reason, 5000).
Orders the process to terminate with the specified Reason. The termination is
done asynchronously, so it is not guaranteed that the process is terminated when
the function returns.
Equivalent to trace(Name, Flag, 5000).
Prints all system events on standard_io. The events
are formatted with a function that is defined by the process that generated the
event (with a call to handle_debug/4).
Process Implementation Functions
Can be used by a process that initiates a debug structure from a list of
options. The values of argument Opt are the same as for the corresponding
functions.
Gets the data associated with a debug option. Default is returned if Item is
not found. Can be used by the process to retrieve debug data for printing before
it terminates.
Returns the logged system events in the debug structure, that is the last
argument to handle_debug/4.
This function is called by a process when it generates a system event.
FormFunc is a formatting function, called as FormFunc(Device, Event, Extra)
to print the events, which is necessary if tracing is activated. Extra is any
extra information that the process needs in the format function, for example,
the process name.
This function is used by a process module to take care of system messages. The
process receives a {system, From, Msg} message and passes Msg and From to
this function.
Prints the logged system events in the debug structure, using FormFunc as
defined when the event was generated by a call to handle_debug/4.
Types
-type dbg_fun() :: fun((FuncState :: _, Event :: system_event(), ProcState :: _) -> done | (NewFuncState :: _)).
-opaque dbg_opt()
See the introduction of this manual page.
-type format_fun() :: fun((Device :: io:device() | file:io_device(), Event :: system_event(), Extra :: term()) -> any()).
-type system_event() :: {in, Msg :: _} | {in, Msg :: _, State :: _} | {out, Msg :: _, To :: _} | {out, Msg :: _, To :: _, State :: _} | {noreply, State :: _} | {continue, Continuation :: _} | {postpone, Event :: _, State :: _, NextState :: _} | {consume, Event :: _, State :: _, NextState :: _} | {start_timer, Action :: _, State :: _} | {insert_timeout, Event :: _, State :: _} | {enter, Module :: module(), State :: _} | {module, Module :: module(), State :: _} | {terminate, Reason :: _, State :: _} | term().
Debug events produced by gen_server, gen_statem and gen_event
{in,Msg}- Is produced bygen_serverandgen_eventwhen the messageMsgarrives.{in,Msg,State}- Is produced bygen_statemwhen the messageMsgarrives in stateState.For
gen_statemtheMsgterm is an{EventType,EventContent}tuple.{out,Msg,To}- Is produced bygen_statemwhen the replyMsgis sent back toToby returning a{reply,To,Msg}action from the callback module.Tois of the same type as the first argument togen_statem:reply/2.{out,Msg,To,State}- Is produced bygen_serverwhen the replyMsgis sent back toToby returning a{reply,...}tuple from the callback module.Tois of the same type as the first argument togen_server:reply/2.Stateis the new server state.{noreply,State}- Is produced bygen_serverwhen a{noreply,...}tuple is returned from the callback module.Stateis the new server state.{continue,Continuation}- Is produced bygen_serverwhen a{continue,Continuation}tuple is returned from the callback module.{postpone,Event,State,NextState}- Is produced bygen_statemwhen the message