Erlang -- erl_tracer

User's Guide
Reference Manual
Release Notes
PDF
Top

Erlang Run-Time System Application (ERTS)
Reference Manual
Version 8.0

Expand All
Contract All

Table of Contents

- erl_prim_loader
  - Top of manual page
  - get_file/1
  - get_path/0
  - list_dir/1
  - read_file_info/1
  - read_link_info/1
  - set_path/1
- erlang
  - Top of manual page
  - abs/1
  - adler32/1
  - adler32/2
  - adler32_combine/3
  - append_element/2
  - apply/2
  - apply/3
  - atom_to_binary/2
  - atom_to_list/1
  - binary_part/2
  - binary_part/3
  - binary_to_atom/2
  - binary_to_existing_atom/2
  - binary_to_float/1
  - binary_to_integer/1
  - binary_to_integer/2
  - binary_to_list/1
  - binary_to_list/3
  - binary_to_term/1
  - binary_to_term/2
  - bit_size/1
  - bitstring_to_list/1
  - bump_reductions/1
  - byte_size/1
  - cancel_timer/2
  - cancel_timer/1
  - check_old_code/1
  - check_process_code/2
  - check_process_code/3
  - convert_time_unit/3
  - crc32/1
  - crc32/2
  - crc32_combine/3
  - date/0
  - decode_packet/3
  - delete_element/2
  - delete_module/1
  - demonitor/1
  - demonitor/2
  - disconnect_node/1
  - display/1
  - element/2
  - erase/0
  - erase/1
  - error/1
  - error/2
  - exit/1
  - exit/2
  - external_size/1
  - external_size/2
  - float/1
  - float_to_binary/1
  - float_to_binary/2
  - float_to_list/1
  - float_to_list/2
  - fun_info/1
  - fun_info/2
  - fun_to_list/1
  - function_exported/3
  - garbage_collect/0
  - garbage_collect/1
  - garbage_collect/2
  - get/0
  - get/1
  - get_cookie/0
  - get_keys/0
  - get_keys/1
  - get_stacktrace/0
  - group_leader/0
  - group_leader/2
  - halt/0
  - halt/1
  - halt/2
  - hash/2
  - hd/1
  - hibernate/3
  - insert_element/3
  - integer_to_binary/1
  - integer_to_binary/2
  - integer_to_list/1
  - integer_to_list/2
  - iolist_size/1
  - iolist_to_binary/1
  - is_alive/0
  - is_atom/1
  - is_binary/1
  - is_bitstring/1
  - is_boolean/1
  - is_builtin/3
  - is_float/1
  - is_function/1
  - is_function/2
  - is_integer/1
  - is_list/1
  - is_map/1
  - is_number/1
  - is_pid/1
  - is_port/1
  - is_process_alive/1
  - is_record/2
  - is_record/3
  - is_reference/1
  - is_tuple/1
  - length/1
  - link/1
  - list_to_atom/1
  - list_to_binary/1
  - list_to_bitstring/1
  - list_to_existing_atom/1
  - list_to_float/1
  - list_to_integer/1
  - list_to_integer/2
  - list_to_pid/1
  - list_to_tuple/1
  - load_module/2
  - load_nif/2
  - loaded/0
  - localtime/0
  - localtime_to_universaltime/1
  - localtime_to_universaltime/2
  - make_ref/0
  - make_tuple/2
  - make_tuple/3
  - map_size/1
  - match_spec_test/3
  - max/2
  - md5/1
  - md5_final/1
  - md5_init/0
  - md5_update/2
  - memory/0
  - memory/1
  - min/2
  - module_loaded/1
  - monitor/2
  - monitor_node/2
  - monitor_node/3
  - monotonic_time/0
  - monotonic_time/1
  - nif_error/1
  - nif_error/2
  - node/0
  - node/1
  - nodes/0
  - nodes/1
  - now/0
  - open_port/2
  - phash/2
  - phash2/1
  - phash2/2
  - pid_to_list/1
  - port_call/3
  - port_close/1
  - port_command/2
  - port_command/3
  - port_connect/2
  - port_control/3
  - port_info/1
  - port_info/2
  - port_to_list/1
  - ports/0
  - pre_loaded/0
  - process_display/2
  - process_flag/2
  - process_flag/3
  - process_info/1
  - process_info/2
  - processes/0
  - purge_module/1
  - put/2
  - raise/3
  - read_timer/2
  - read_timer/1
  - ref_to_list/1
  - register/2
  - registered/0
  - resume_process/1
  - round/1
  - self/0
  - send/2
  - send/3
  - send_after/4
  - send_after/3
  - send_nosuspend/2
  - send_nosuspend/3
  - set_cookie/2
  - setelement/3
  - size/1
  - spawn/1
  - spawn/2
  - spawn/3
  - spawn/4
  - spawn_link/1
  - spawn_link/2
  - spawn_link/3
  - spawn_link/4
  - spawn_monitor/1
  - spawn_monitor/3
  - spawn_opt/2
  - spawn_opt/3
  - spawn_opt/4
  - spawn_opt/5
  - split_binary/2
  - start_timer/4
  - start_timer/3
  - statistics/1
  - suspend_process/2
  - suspend_process/1
  - system_flag/2
  - system_info/1
  - system_monitor/0
  - system_monitor/1
  - system_monitor/2
  - system_profile/0
  - system_profile/2
  - system_time/0
  - system_time/1
  - term_to_binary/1
  - term_to_binary/2
  - throw/1
  - time/0
  - time_offset/0
  - time_offset/1
  - timestamp/0
  - tl/1
  - trace/3
  - trace_delivered/1
  - trace_info/2
  - trace_pattern/2
  - trace_pattern/3
  - trunc/1
  - tuple_size/1
  - tuple_to_list/1
  - unique_integer/0
  - unique_integer/1
  - universaltime/0
  - universaltime_to_localtime/1
  - unlink/1
  - unregister/1
  - whereis/1
  - yield/0
- init
  - Top of manual page
  - boot/1
  - get_argument/1
  - get_arguments/0
  - get_plain_arguments/0
  - get_status/0
  - reboot/0
  - restart/0
  - script_id/0
  - stop/0
  - stop/1
- zlib
  - Top of manual page
  - adler32/2
  - adler32/3
  - adler32_combine/4
  - close/1
  - compress/1
  - crc32/1
  - crc32/2
  - crc32/3
  - crc32_combine/4
  - deflate/2
  - deflate/3
  - deflateEnd/1
  - deflateInit/1
  - deflateInit/2
  - deflateInit/6
  - deflateParams/3
  - deflateReset/1
  - deflateSetDictionary/2
  - getBufSize/1
  - gunzip/1
  - gzip/1
  - inflate/2
  - inflateChunk/2
  - inflateChunk/1
  - inflateEnd/1
  - inflateInit/1
  - inflateInit/2
  - inflateReset/1
  - inflateSetDictionary/2
  - open/0
  - setBufSize/2
  - uncompress/1
  - unzip/1
  - zip/1
- epmd
- erl
- erlc
- werl
- escript
- erlsrv
- start_erl
- run_erl
- start
- erl_driver
  - Top of manual page
  - erl_drv_cond_name()
  - erl_drv_mutex_name()
  - erl_drv_rwlock_name()
  - erl_drv_thread_name()
  - erl_errno_id()
  - driver_cancel_timer()
  - driver_compare_monitors()
  - driver_demonitor_process()
  - driver_enq()
  - driver_enq_bin()
  - driver_enqv()
  - driver_failure()
  - driver_failure_atom()
  - driver_failure_eof()
  - driver_failure_posix()
  - driver_get_now()
  - driver_lock_driver()
  - driver_monitor_process()
  - driver_output()
  - driver_output2()
  - driver_output_binary()
  - driver_output_term()
  - driver_outputv()
  - driver_pushq()
  - driver_pushq_bin()
  - driver_pushqv()
  - driver_read_timer()
  - driver_select()
  - driver_send_term()
  - driver_set_timer()
  - erl_drv_consume_timeslice()
  - erl_drv_equal_tids()
  - erl_drv_getenv()
  - erl_drv_mutex_trylock()
  - erl_drv_output_term()
  - erl_drv_putenv()
  - erl_drv_rwlock_tryrlock()
  - erl_drv_rwlock_tryrwlock()
  - erl_drv_send_term()
  - erl_drv_thread_create()
  - erl_drv_thread_join()
  - erl_drv_tsd_key_create()
  - remove_driver_entry()
  - driver_async ()
  - driver_binary_dec_refc()
  - driver_binary_get_refc()
  - driver_binary_inc_refc()
  - driver_pdl_dec_refc()
  - driver_pdl_get_refc()
  - driver_pdl_inc_refc()
  - driver_async_port_key ()
  - driver_alloc()
  - driver_realloc()
  - erl_drv_tsd_get()
  - add_driver_entry()
  - driver_free()
  - driver_free_binary()
  - driver_pdl_lock()
  - driver_pdl_unlock()
  - driver_system_info()
  - erl_drv_busy_msgq_limits()
  - erl_drv_cond_broadcast()
  - erl_drv_cond_destroy()
  - erl_drv_cond_signal()
  - erl_drv_cond_wait()
  - erl_drv_init_ack()
  - erl_drv_mutex_destroy()
  - erl_drv_mutex_lock()
  - erl_drv_mutex_unlock()
  - erl_drv_rwlock_destroy()
  - erl_drv_rwlock_rlock()
  - erl_drv_rwlock_runlock()
  - erl_drv_rwlock_rwlock()
  - erl_drv_rwlock_rwunlock()
  - erl_drv_set_os_pid()
  - erl_drv_thread_exit()
  - erl_drv_thread_opts_destroy()
  - erl_drv_tsd_key_destroy()
  - erl_drv_tsd_set()
  - set_busy_port()
  - set_port_control_flags()
  - driver_alloc_binary()
  - driver_realloc_binary()
  - erl_drv_cond_create()
  - erl_drv_mutex_create()
  - driver_pdl_create()
  - driver_create_port()
  - erl_drv_rwlock_create()
  - driver_deq()
  - driver_peekqv()
  - driver_sizeq()
  - driver_vec_to_buf()
  - driver_caller()
  - driver_connected()
  - driver_get_monitored_process()
  - driver_mk_atom()
  - driver_mk_port()
  - erl_drv_thread_opts_create()
  - erl_drv_thread_self()
  - erl_drv_convert_time_unit()
  - erl_drv_monotonic_time()
  - erl_drv_time_offset()
  - driver_peekq()
- driver_entry
- erts_alloc
- erl_nif
  - Top of manual page
  - enif_alloc_binary()
  - enif_compare()
  - enif_consume_timeslice()
  - enif_equal_tids()
  - enif_get_atom()
  - enif_get_atom_length()
  - enif_get_double()
  - enif_get_int()
  - enif_get_int64()
  - enif_get_list_cell()
  - enif_get_list_length()
  - enif_get_local_pid()
  - enif_get_local_port()
  - enif_get_long()
  - enif_get_map_size()
  - enif_get_map_value()
  - enif_get_resource()
  - enif_get_string()
  - enif_get_tuple()
  - enif_get_uint()
  - enif_get_uint64()
  - enif_get_ulong()
  - enif_getenv()
  - enif_has_pending_exception()
  - enif_inspect_binary()
  - enif_inspect_iolist_as_binary()
  - enif_is_atom()
  - enif_is_binary()
  - enif_is_current_process_alive()
  - enif_is_empty_list()
  - enif_is_exception()
  - enif_is_fun()
  - enif_is_identical()
  - enif_is_list()
  - enif_is_map()
  - enif_is_number()
  - enif_is_pid()
  - enif_is_port()
  - enif_is_port_alive()
  - enif_is_process_alive()
  - enif_is_ref()
  - enif_is_tuple()
  - enif_keep_resource()
  - enif_make_existing_atom()
  - enif_make_existing_atom_len()
  - enif_make_map_put()
  - enif_make_map_remove()
  - enif_make_map_update()
  - enif_make_reverse_list()
  - enif_map_iterator_create()
  - enif_map_iterator_get_pair()
  - enif_map_iterator_is_head()
  - enif_map_iterator_is_tail()
  - enif_map_iterator_next()
  - enif_map_iterator_prev()
  - enif_mutex_trylock()
  - enif_port_command()
  - enif_realloc_binary()
  - enif_rwlock_tryrlock()
  - enif_rwlock_tryrwlock()
  - enif_send()
  - enif_snprintf()
  - enif_term_to_binary()
  - enif_thread_create()
  - enif_thread_join()
  - enif_thread_type()
  - enif_tsd_key_create()
  - enif_binary_to_term()
  - enif_make_new_binary()
  - enif_sizeof_resource()
  - enif_alloc()
  - enif_alloc_resource()
  - enif_priv_data()
  - enif_tsd_get()
  - enif_clear_env()
  - enif_cond_broadcast()
  - enif_cond_destroy()
  - enif_cond_signal()
  - enif_cond_wait()
  - enif_free()
  - enif_free_env()
  - enif_map_iterator_destroy()
  - enif_mutex_destroy()
  - enif_mutex_lock()
  - enif_mutex_unlock()
  - enif_release_binary()
  - enif_release_resource()
  - enif_rwlock_destroy()
  - enif_rwlock_rlock()
  - enif_rwlock_runlock()
  - enif_rwlock_rwlock()
  - enif_rwlock_rwunlock()
  - enif_system_info()
  - enif_thread_exit()
  - enif_thread_opts_destroy()
  - enif_tsd_key_destroy()
  - enif_tsd_set()
  - enif_cpu_time()
  - enif_make_atom()
  - enif_make_atom_len()
  - enif_make_badarg()
  - enif_make_binary()
  - enif_make_copy()
  - enif_make_double()
  - enif_make_int()
  - enif_make_int64()
  - enif_make_list()
  - enif_make_list1()
  - enif_make_list2()
  - enif_make_list3()
  - enif_make_list4()
  - enif_make_list5()
  - enif_make_list6()
  - enif_make_list7()
  - enif_make_list8()
  - enif_make_list9()
  - enif_make_list_cell()
  - enif_make_list_from_array()
  - enif_make_long()
  - enif_make_new_map()
  - enif_make_pid()
  - enif_make_ref()
  - enif_make_resource()
  - enif_make_resource_binary()
  - enif_make_string()
  - enif_make_string_len()
  - enif_make_sub_binary()
  - enif_make_tuple()
  - enif_make_tuple1()
  - enif_make_tuple2()
  - enif_make_tuple3()
  - enif_make_tuple4()
  - enif_make_tuple5()
  - enif_make_tuple6()
  - enif_make_tuple7()
  - enif_make_tuple8()
  - enif_make_tuple9()
  - enif_make_tuple_from_array()
  - enif_make_uint()
  - enif_make_uint64()
  - enif_make_ulong()
  - enif_make_unique_integer()
  - enif_now_time()
  - enif_raise_exception()
  - enif_schedule_nif()
  - enif_cond_create()
  - enif_alloc_env()
  - enif_mutex_create()
  - enif_self()
  - enif_rwlock_create()
  - enif_open_resource_type()
  - enif_thread_opts_create()
  - enif_thread_self()
  - enif_convert_time_unit()
  - enif_monotonic_time()
  - enif_time_offset()
- erl_tracer
  - Top of manual page
  - Module:enabled/3
  - Module:enabled_call/3
  - Module:enabled_garbage_collection/3
  - Module:enabled_ports/3
  - Module:enabled_procs/3
  - Module:enabled_receive/3
  - Module:enabled_running_ports/3
  - Module:enabled_running_procs/3
  - Module:enabled_send/3
  - Module:trace/5
  - Module:trace_call/5
  - Module:trace_garbage_collection/5
  - Module:trace_ports/5
  - Module:trace_procs/5
  - Module:trace_receive/5
  - Module:trace_running_ports/5
  - Module:trace_running_procs/5
  - Module:trace_send/5

# erl_tracer

### MODULE

erl_tracer

### MODULE SUMMARY

Erlang Tracer Behaviour

### DESCRIPTION

A behaviour module for implementing the back end of the erlang tracing system. The functions in this module will be called whenever a trace probe is triggered. Both the enabled and trace functions are called in the context of the entity that triggered the trace probe. This means that the overhead by having the tracing enabled will be greatly effected by how much time is spent in these functions. So do as little work as possible in these functions.

Note

All functions in this behaviour have to be implemented as NIF's. This is a limitation that may the lifted in the future. There is an example tracer module nif implementation at the end of this page.

Warning

Do not send messages or issue port commands to the Tracee in any of the callbacks. Doing so is not allowed and can cause all sorts of strange behaviour, including but not limited to infinite recursions.

### DATA TYPES

trace_tag_send() = send | send_to_non_existing_process

trace_tag_receive() = 'receive'

trace_tag_call() =
call | return_to | return_from | exception_from

trace_tag_procs() =
spawn |
spawned |
exit |
link |
unlink |
getting_linked |
getting_unlinked |
register |
unregister

trace_tag_ports() =
open |
closed |
link |
unlink |
getting_linked |
getting_unlinked

trace_tag_running_procs() =
in | out | in_exiting | out_exiting | out_exited

trace_tag_running_ports() =
in | out | in_exiting | out_exiting | out_exited

trace_tag_gc() =
gc_minor_start | gc_minor_end | gc_major_start | gc_major_end

trace_tag() =
trace_tag_send() |
trace_tag_receive() |
trace_tag_call() |
trace_tag_procs() |
trace_tag_ports() |
trace_tag_running_procs() |
trace_tag_running_ports() |
trace_tag_gc()

The different trace tags that the tracer will be called with. Each trace tag is described in greater detail in Module:trace/5

tracee() = port() | pid() | undefined

The process or port that the trace belongs to.

trace_opts() =
#{extra => term(),
match_spec_result => term(),
scheduler_id => integer() >= 0,
timestamp =>
timestamp | cpu_timestamp | monotonic | strict_monotonic}

The options for the tracee.

timestamp
  If set the tracer has been requested to include a timestamp.
extra
  If set the tracepoint has included additonal data about the trace event. What the additional data is depends on which TraceTag has been triggered. The extra trace data corresponds to the fifth elemnt in the trace tuples described in erlang:trace/3.
match_spec_result
  If set the tracer has been requested to include the output of a match specification that was run.
scheduler_id
  Set the scheduler id is to be included by the tracer.

tracer_state() = term()

The state which is given when calling erlang:trace(PidPortSpec,true,[{tracer,Module,TracerState}]). The tracer state is an immutable value that is passed to erl_tracer callbacks and should contain all the data that is needed to generate the trace event.

### CALLBACK FUNCTIONS

The following functions should be exported from a erl_tracer callback module.

Module:enabled/3
  Mandatory
Module:trace/5
  Mandatory
Module:enabled_procs/3
  Optional
Module:trace_procs/5
  Optional
Module:enabled_ports/3
  Optional
Module:trace_ports/5
  Optional
Module:enabled_running_ports/3
  Optional
Module:trace_running_ports/5
  Optional
Module:enabled_running_procs/3
  Optional
Module:trace_running_procs/5
  Optional

### EXPORTS

Module:enabled(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag() | trace_status
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint is triggered. It allows the tracer to decide whether a trace should be generated or not. This check is made as early as possible in order to limit the amount of overhead associated with tracing. If trace is returned the necessary trace data will be created and the trace call-back of the tracer will be called. If discard is returned, this trace call will be discarded and no call to trace will be done.

trace_status is a special type of TraceTag which is used to check if the tracer should still be active. It is called in multiple scenarios, but most significantly it is used when tracing is started using this tracer. If remove is returned when the trace_status is checked, the tracer will be removed from the tracee.

This function may be called multiple times per tracepoint, so it is important that it is both fast and side effect free.

Module:trace(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled/3 callback returned trace. In it any side effects needed by the tracer should be done. The tracepoint payload is located in the TraceTerm. The content of the TraceTerm depends on which TraceTag has been triggered. The TraceTerm corresponds to the fourth element in the trace tuples described in erlang:trace/3. If the trace tuple has five elements, the fifth element will be sent as the extra value in the Opts maps.

Module:trace(seq_trace, TracerState, Label, SeqTraceInfo, Opts) -> Result

Types:

TracerState = term()
Label = term()
SeqTraceInfo = term()
Opts = trace_opts()
Result = ok

The TraceTag seq_trace is handled a little bit differently. There is not Tracee for seq_trace, instead the Label associated with the seq_trace event is given. For more info on what Label and SeqTraceInfo can be see the seq_trace manual.

Module:enabled_procs(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag_procs()
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint with trace flag procs is triggered.

If enabled_procs/3 is not defined enabled/3 will be called instead.

Module:trace_procs(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled_procs/3 callback returned trace.

If trace_procs/5 is not defined trace/5 will be called instead.

Module:enabled_ports(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag_ports()
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint with trace flag ports is triggered.

If enabled_ports/3 is not defined enabled/3 will be called instead.

Module:trace_ports(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled_ports/3 callback returned trace.

If trace_ports/5 is not defined trace/5 will be called instead.

Module:enabled_running_procs(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag_running_procs()
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint with trace flag running_procs | running is triggered.

If enabled_running_procs/3 is not defined enabled/3 will be called instead.

Module:trace_running_procs(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag_running_procs()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled_running_procs/3 callback returned trace.

If trace_running_procs/5 is not defined trace/5 will be called instead.

Module:enabled_running_ports(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag_running_ports()
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint with trace flag running_ports is triggered.

If enabled_running_ports/3 is not defined enabled/3 will be called instead.

Module:trace_running_ports(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag_running_ports()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled_running_ports/3 callback returned trace.

If trace_running_ports/5 is not defined trace/5 will be called instead.

Module:enabled_call(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag_call()
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint with trace flag call | return_to is triggered.

If enabled_call/3 is not defined enabled/3 will be called instead.

Module:trace_call(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag_call()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled_call/3 callback returned trace.

If trace_call/5 is not defined trace/5 will be called instead.

Module:enabled_send(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag_send()
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint with trace flag send is triggered.

If enabled_send/3 is not defined enabled/3 will be called instead.

Module:trace_send(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag_send()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled_send/3 callback returned trace.

If trace_send/5 is not defined trace/5 will be called instead.

Module:enabled_receive(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag_receive()
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint with trace flag 'receive' is triggered.

If enabled_receive/3 is not defined enabled/3 will be called instead.

Module:trace_receive(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag_receive()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled_receive/3 callback returned trace.

If trace_receive/5 is not defined trace/5 will be called instead.

Module:enabled_garbage_collection(TraceTag, TracerState, Tracee) -> Result

Types:

TraceTag = trace_tag_gc()
TracerState = term()
Tracee = tracee()
Result = trace | discard | remove

This callback will be called whenever a tracepoint with trace flag garbage_collection is triggered.

If enabled_garbage_collection/3 is not defined enabled/3 will be called instead.

Module:trace_garbage_collection(TraceTag, TracerState, Tracee, TraceTerm, Opts) -> Result

Types:

TraceTag = trace_tag_gc()
TracerState = term()
Tracee = tracee()
FirstTraceTerm = term()
Opts = trace_opts()
Result = ok

This callback will be called when a tracepoint is triggered and the Module:enabled_garbage_collection/3 callback returned trace.

If trace_garbage_collection/5 is not defined trace/5 will be called instead.

### Erl Tracer Module example

In the example below a tracer module with a nif backend sends a message for each send trace tag containing only the sender and receiver. Using this tracer module, a much more lightweight message tracer is used that only records who sent messages to who.

Here is an example session using it on Linux.

    $ gcc -I erts-8.0/include/ -fPIC -shared -o erl_msg_tracer.so erl_msg_tracer.c
    $ erl
    Erlang/OTP 19 [DEVELOPMENT] [erts-8.0] [source-ed2b56b] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

    Eshell V8.0  (abort with ^G)
    1> c(erl_msg_tracer), erl_msg_tracer:load().
    ok
    2> Tracer = spawn(fun F() -> receive M -> io:format("~p~n",[M]), F() end end).
    <0.37.0>
    3> erlang:trace(new, true, [send,{tracer, erl_msg_tracer, Tracer}]).
    0
    {<0.39.0>,<0.27.0>}
    4> {ok, D} = file:open("/tmp/tmp.data",[write]).
    {trace,#Port<0.486>,<0.40.0>}
    {trace,<0.40.0>,<0.21.0>}
    {trace,#Port<0.487>,<0.4.0>}
    {trace,#Port<0.488>,<0.4.0>}
    {trace,#Port<0.489>,<0.4.0>}
    {trace,#Port<0.490>,<0.4.0>}
    {ok,<0.40.0>}
    {trace,<0.41.0>,<0.27.0>}
    5>

erl_msg_tracer.erl

    -module(erl_msg_tracer).

    -export([enabled/3, trace/5, load/0]).

    load() ->
        erlang:load_nif("erl_msg_tracer", []).

    enabled(_, _, _) ->
        error.

    trace(_, _, _,_, _) ->
        error.

erl_msg_tracer.c

    #include "erl_nif.h"

    /* NIF interface declarations */
    static int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info);
    static int upgrade(ErlNifEnv* env, void** priv_data, void** old_priv_data, ERL_NIF_TERM load_info);
    static void unload(ErlNifEnv* env, void* priv_data);

    /* The NIFs: */
    static ERL_NIF_TERM enabled(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
    static ERL_NIF_TERM trace(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);

    static ErlNifFunc nif_funcs[] = {
        {"enabled", 3, enabled},
        {"trace", 5, trace}
    };

    ERL_NIF_INIT(erl_msg_tracer, nif_funcs, load, NULL, upgrade, unload)

    static int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
    {
        *priv_data = NULL;
        return 0;
    }

    static void unload(ErlNifEnv* env, void* priv_data)
    {

    }

    static int upgrade(ErlNifEnv* env, void** priv_data, void** old_priv_data,
    		   ERL_NIF_TERM load_info)
    {
        if (*old_priv_data != NULL || *priv_data != NULL) {
    	return -1; /* Don't know how to do that */
        }
        if (load(env, priv_data, load_info)) {
    	return -1;
        }
        return 0;
    }

    /*
     * argv[0]: TraceTag
     * argv[1]: TracerState
     * argv[2]: Tracee
     */
    static ERL_NIF_TERM enabled(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
    {
        ErlNifPid to_pid;
        if (enif_get_local_pid(env, argv[1], &to_pid))
            if (!enif_is_process_alive(env, &to_pid))
                if (enif_is_identical(enif_make_atom(env, "trace_status"), argv[0]))
                    /* tracer is dead so we should remove this tracepoint */
                    return enif_make_atom(env, "remove");
                else
                    return enif_make_atom(env, "discard");

        /* Only generate trace for when tracer != tracee */
        if (enif_is_identical(argv[1], argv[2]))
            return enif_make_atom(env, "discard");

        /* Only trigger trace messages on 'send' */
        if (enif_is_identical(enif_make_atom(env, "send"), argv[0]))
            return enif_make_atom(env, "trace");

        /* Have to answer trace_status */
        if (enif_is_identical(enif_make_atom(env, "trace_status"), argv[0]))
            return enif_make_atom(env, "trace");

        return enif_make_atom(env, "discard");
    }

    /*
     * argv[0]: TraceTag, should only be 'send'
     * argv[1]: TracerState, process to send {argv[2], argv[4]} to
     * argv[2]: Tracee
     * argv[3]: Recipient
     * argv[4]: Options, ignored
     */
    static ERL_NIF_TERM trace(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
    {
        ErlNifPid to_pid;

        if (enif_get_local_pid(env, argv[1], &to_pid)) {
            ERL_NIF_TERM msg = enif_make_tuple3(env, enif_make_atom(env, "trace"), argv[2], argv[4]);
            enif_send(env, &to_pid, NULL, msg);
        }

        return enif_make_atom(env, "ok");
    }