View Source Matcha.Context.Trace (Matcha v0.1.10)

Additional functions that trace match specs can use in their bodies.

The return values of specs created in this context do not differentiate between specs that fail to find a matching clause for the given input, and specs with matching clauses that literally return the false value; they return {:traced, result, flags} tuples either way.

Tracing match specs offer a wide suite of instructions to drive Erlang's tracing engine in response to matching certain calls. Calls to these functions in match spec bodies will, when that clause is matched, effect the documented change during tracing.

These instructions are documented and type-specced here as a convenient reference. For more information, consult the Erlang tracing match spec docs.

In addition to general helpful informational functions, tracing supports:

trace-flags

Trace Flags

Match specs can change how tracing behaves by changing the trace flags on any process. See :erlang.trace/3 for more information.

Related functions:

sequential-tracing

Sequential Tracing

Match specs can be used to transfer information between processes via sequential tracing. See the Erlang sequential tracing docs for more information.

Related functions:

Link to this section Summary

Functions

Returns the module/function/arity of the calling function.

Turns off the provided trace_flag for the current process.

Turns off the provided trace_flag for the specified pid.

Displays the given value on stdout for debugging purposes.

Turns on the provided trace_flag for the current process.

Turns on the provided trace_flag for the specified pid.

Works as return_trace/0, generating an extra exception_from message on exceptions.

Retreives the (opaque) value of the trace token for the current process.

Returns the value of the current node's trace control word.

Returns true if a sequential trace token is set for the current process, otherwise false.

Sets an additional message appended to the trace message sent.

Returns some textual information about the current process as a binary.

Causes a return_from trace message to be sent upon return from the current function.

Sets a label, serial number, or flag token to value for sequential tracing.

Sets the value of the current node's trace control word to trace_control_word.

Changes the verbosity of the current process's messaging mode.

Atomically disables and enables a set of trace flags for the current process in one go.

Atomically disables and enables a set of trace flags for the given pid in one go.

Link to this section Types

@type seq_token() :: {integer(), boolean(), any(), any(), any()}
@type seq_token_component() :: :label | :serial | seq_token_flag()
Link to this type

seq_token_current_serial_number()

View Source
@type seq_token_current_serial_number() :: seq_token_serial_number()
@type seq_token_flag() ::
  :send
  | :receive
  | :print
  | :timestamp
  | :monotonic_timestamp
  | :strict_monotonic_timestamp
Link to this type

seq_token_label_value()

View Source
@type seq_token_label_value() :: any()
Link to this type

seq_token_previous_serial_number()

View Source
@type seq_token_previous_serial_number() :: seq_token_serial_number()
Link to this type

seq_token_serial_number()

View Source
@type seq_token_serial_number() :: non_neg_integer()
Link to this type

seq_token_serial_value()

View Source
@type seq_token_serial_value() ::
  {seq_token_previous_serial_number(), seq_token_current_serial_number()}
@type seq_token_value() ::
  seq_token_label_value() | seq_token_serial_value() | boolean()
@type trace_flag() ::
  :all
  | :send
  | :receive
  | :procs
  | :ports
  | :call
  | :arity
  | :return_to
  | :silent
  | :running
  | :exiting
  | :running_procs
  | :running_ports
  | :garbage_collection
  | :timestamp
  | :monotonic_timestamp
  | :strict_monotonic_timestamp
  | :set_on_spawn
  | :set_on_first_spawn
  | :set_on_link
  | :set_on_first_link
@type tracer_trace_flag() :: {:tracer, pid() | port()} | {:tracer, module(), any()}

Link to this section Functions

@spec caller() :: {module(), function(), arity :: non_neg_integer()} | :undefined

Returns the module/function/arity of the calling function.

If the calling function cannot be determined, returns :undefined. This can happen with BIFs in particular.

Link to this function

disable_trace(trace_flag)

View Source
@spec disable_trace(trace_flag()) :: true

Turns off the provided trace_flag for the current process.

See the third parameter of :erlang.trace/3/ for a list of flags and their effects. Note that the :cpu_timestamp and :tracer flags are not supported in this function.

Always returns true.

Link to this function

disable_trace(pid, trace_flag)

View Source
@spec disable_trace(pid(), trace_flag()) :: true

Turns off the provided trace_flag for the specified pid.

See the third parameter of :erlang.trace/3/ for a list of flags and their effects. Note that the :cpu_timestamp and :tracer flags are not supported in this function.

Always returns true.

@spec display(value :: any()) ::
  {module(), function(), arity :: non_neg_integer()} | :undefined

Displays the given value on stdout for debugging purposes.

Always returns true.

Link to this function

enable_trace(trace_flag)

View Source
@spec enable_trace(trace_flag()) :: true

Turns on the provided trace_flag for the current process.

See the third parameter of :erlang.trace/3/ for a list of flags and their effects. Note that the :cpu_timestamp and :tracer flags are not supported in this function.

Always returns true.

Link to this function

enable_trace(pid, trace_flag)

View Source
@spec enable_trace(pid(), trace_flag()) :: non_neg_integer()

Turns on the provided trace_flag for the specified pid.

See the third parameter of :erlang.trace/3/ for a list of flags and their effects. Note that the :cpu_timestamp and :tracer flags are not supported in this function.

Always returns true.

@spec exception_trace() :: true

Works as return_trace/0, generating an extra exception_from message on exceptions.

Causes a return_from trace message to be sent upon return from the current function. Plus, if the traced function exits because of an exception, an exception_from trace message is generated, whether or not the exception is caught.

If the process trace flag silent is active, the return_from and exception_from trace messages are inhibited.

Always returns true.

Warning: If the traced function is tail-recursive, this match specification function destroys that property. Hence, if a match specification executing this function is used on a perpetual server process, t can only be active for a limited period of time, or the emulator will eventually use all memory in the host machine and crash. If this match specification function is inhibited using process trace flag silent, tail-recursiveness still remains.

@spec get_seq_token() :: seq_token() | []

Retreives the (opaque) value of the trace token for the current process.

If the current process is not being traced, returns [].

Acts identically to :seq_trace.get_token/0. The docs say that the return value can be passed back into :seq_trace.set_token/1. However, in a tracing match spec context, there is no equivalent (set_seq_token/2 works, but there's no set_seq_token/1). So I am unsure what this can be used for.

For more information, consult :seq_trace.get_token/0 docs.

@spec get_tcw() :: trace_control_word when trace_control_word: non_neg_integer()

Returns the value of the current node's trace control word.

Identical to calling :erlang.system_info/1 with the argument :trace_control_word.

The trace control word is a 32-bit unsigned integer intended for generic trace control. The trace control word can be tested and set both from within trace match specifications and with BIFs.

@spec is_seq_trace() :: boolean()

Returns true if a sequential trace token is set for the current process, otherwise false.

@spec message(message | {message, false | message, true}) :: true when message: any()

Sets an additional message appended to the trace message sent.

One can only set one additional message in the body. Later calls replace the appended message.

Always returns true.

As a special case, {message, false} disables sending of trace messages ('call' and 'return_to') for this function call, just like if the match specification had not matched. This can be useful if only the side effects of the match spec clause's body part are desired.

Another special case is {message, true}, which sets the default behavior, as if the function had no match specification; trace message is sent with no extra information (if no other calls to message are placed before {message, true}, it is in fact a "noop").

@spec process_dump() :: true

Returns some textual information about the current process as a binary.

@spec return_trace() :: true

Causes a return_from trace message to be sent upon return from the current function.

If the process trace flag silent is active, the return_from trace message is inhibited.

Always returns true.

Warning:

If the traced function is tail-recursive, this match specification function destroys that property. Hence, if a match specification executing this function is used on a perpetual server process, it can only be active for a limited period of time, or the emulator will eventually use all memory in the host machine and crash. If this match specification function is inhibited using process trace flag silent, tail-recursiveness still remains.

Link to this function

set_seq_token(token, value)

View Source
@spec set_seq_token(seq_token_component(), seq_token_value()) :: true | charlist()

Sets a label, serial number, or flag token to value for sequential tracing.

Acts like :seq_trace.set_token/2, except returns true on success, and 'EXIT' on error or bad argument.

Note that this function cannot be used to exclude message passing from the trace, since that is normally accomplished by passing [] into :seq_trace.set_token/1 (however there is no set_seq_token/1 allowed in match specs).

Note that the values set here cannot be introspected in a match spec tracing context (get_seq_token/0 returns an opaque representation of the current trace token, but there's no get_seq_token/1 to inspect individual values).

For more information, consult :seq_trace.set_token/2 docs.

Link to this function

set_tcw(trace_control_word)

View Source
@spec set_tcw(trace_control_word) :: trace_control_word
when trace_control_word: non_neg_integer()

Sets the value of the current node's trace control word to trace_control_word.

Identical to calling :erlang.system_flag/2 with the arguments :trace_control_word and trace_control_word.

Returns the previous value of the node's trace control word.

@spec silent(mode :: boolean() | any()) :: any()

Changes the verbosity of the current process's messaging mode.

  • If mode is true, supresses all trace messages.
  • If mode is false, re-enables trace messages in future calls.
  • If mode is anything else, the current mode remains active.
Link to this function

trace(disable_flags, enable_flags)

View Source
@spec trace(
  disable_flags :: [trace_flag() | tracer_trace_flag()],
  enable_flags :: [trace_flag() | tracer_trace_flag()]
) :: boolean()
@spec trace(
  disable_flags :: [trace_flag() | tracer_trace_flag()],
  enable_flags :: [trace_flag() | tracer_trace_flag()]
) :: boolean()

Atomically disables and enables a set of trace flags for the current process in one go.

Flags enabled in the enable_flags list will override duplicate flags in the disable_flags list.

See the third parameter of :erlang.trace/3/ for a list of flags and their effects. Note that the :cpu_timestamp flag is not supported in this function, however unlike the enable_trace/1 and disable_trace/1 functions, the :tracer flags are supported..

If no :tracer is specified, the same tracer as the process executing the match specification is used (not the meta tracer). If that process doesn't have tracer either, then trace flags are ignored. When using a tracer module, the module must be loaded before the match specification is executed. If it is not loaded, the match fails.

Returns true if any trace property was changed for the current process, otherwise false.

Link to this function

trace(pid, disable_flags, enable_flags)

View Source

Atomically disables and enables a set of trace flags for the given pid in one go.

Flags enabled in the enable_flags list will override duplicate flags in the disable_flags list.

See the third parameter of :erlang.trace/3/ for a list of flags and their effects. Note that the :cpu_timestamp flag is not supported in this function, however unlike the enable_trace/1 and disable_trace/1 functions, the :tracer flags are supported..

If no :tracer is specified, the same tracer as the process executing the match specification is used (not the meta tracer). If that process doesn't have tracer either, then trace flags are ignored. When using a tracer module, the module must be loaded before the match specification is executed. If it is not loaded, the match fails.

Returns true if any trace property was changed for the given pid, otherwise false.