Tracing LFE Functions

Eric Bailey

Written on 12 April, 2016
Updated on 18 December, 2023
Tags: , , , ,

What follows is an LFE translation of Roberto Aloi's Tracing Erlang Functions.

Tracing LFE functions for debugging purposes is quite simple.

Let's say you have the following module and want to trace one of its functions.

(defmodule maths
  (export (sum 2) (diff 2)))

(defun sum (a b) (+ a b))

(defun diff (a b) (- a b))

Before we get started, make sure you compile the maths module:

(c "/path/to/maths.lfe")

Just start the tracer:

(dbg:tracer)
#(ok <0.46.0>)

Tell the tracer you are interested in all calls for all processes:

(dbg:p 'all 'c)
#(ok (#(matched nonode@nohost 26)))

Finally, tell it you want to trace the function, sum, from the maths module:

(dbg:tpl 'maths 'sum [])
#(ok (#(matched nonode@nohost 1)))

Now, try to call the function, as usual. The tracer is active!

(maths:sum 2 3)
5
(<0.29.0>) call maths:sum(2,3)

To trace all functions from the maths module:

> (dbg:tpl 'maths [])

To trace the return value for a given function:

(dbg:tpl 'maths 'sum (match-spec ([_] (return_trace))))
#(ok (#(matched nonode@nohost 1) #(saved 1)))
(maths:sum 19 23)
42
(<0.56.0>) call maths:sum(19,23)
(<0.56.0>) returned from maths:sum/2 -> 42

To stop the trace:

(dbg:stop)
ok