The dbg module is a bit complex to use, but it gives good control on what to trace, With using match specification it is possible to create more advanced filtering.
Standard debug tool in erlang enviroment - The dbg module. it is a part of the OTP. How to use? we need to specify three things:
- which process(es) to trace
- which function(s) to watch
- under which conditions to receive a trace output(match specification)
Some type of faults require continuous logging functionality implemented in the product.(Log must be limited in memory and CPU usage)
Tracing with the dbg module
initialization dbg:tracer()
- starts a trace server on the node
- possible alternatives: send to another process for processing, or send to a file.
specify process(es) dbg:p(process, Flags).
process
- a Pid
- a tuple{X, Y, Z} -> turns to Pid
- "all" -> all existing and future processes
- "new" -> all future processes
- "existing" -> all existing processes
- "processname" -> the process with registered name "processname" is traced
Specify function dbg:tp and dbg:tpl
- dbg:tp(Module, MatchSpec)
- dbg:tpl(Module, MatchSpec) Module:
{Module, '_', '_'}{Module, Function, '_'}{Module, Function, Arity}
- dbg:tpl is the same as the equivalent dbg:tp, but enables tracing for local calls (and local functions) as well as for global calls(and functions)
Format of MatchSpec
[{MatchHead1, MatchConditions1, MatchBody1 }, {MatchHead1, MatchConditions1, MatchBody1 }, ...]
- Examples of MatchSpec
[{'_', [], [{return_trace}]}]or:[{[gcpLinkTable, '_'], [], {return_trace}}]
- Examples with dbg:tpl
Tracing for ets:insert/2 dbg:tpl(ets, insert, [{'_', [], [return_trace]}]). Tracing for ets:insert/2, but only for one table dbg:tpl(ets, insert, [{[Table, '_'], [], [{return_trace}]}]). same as above, and include also the process stack in trace output dbg:tpl(ets, insert, [{[Table, "_"], [], [{message, {process_dump}}, {return_trace}]}]).
- Tracing into file
dbg:tracer(port, dbg:trace_port(file, '/tmp/trace001.txt')).dbg:p(all, [c]).dbg:tpl(ets, insert, [{'_', [], [return_trace]}]).stop tracedbg:stop_clear().
- close the file
At the end don't forget to stop tracing, it will close the file. dbg:stop(). dbg:stop_clear(). same as stop/0, but also clears all trace patterns on local and global functions calls.
- Trace on ets:insert/2. Receive trace output for a specific table (exercise) and a specific key value (99):
ets:new(exercise, [named_table, ordered_set, public, {keypos, 2}]). dbg:tracer(). dbg:p(all, [c]). dbg:tpl(ets, insert, [{['$1', '$2'], [{'==', exercise, '$1'}, {'==', 99, {element, 2, '$2'}}], [{message, {process_dump}}, {return_trace}]}]). ets:insert(exercise, {exercise, 1, data1}). ets:insert(exercise, {exercise, 99, data99}).
- Try also the same exercise with trace output to a file instead of the shell log, and compare the results.
-
dbg:tpl(ets, insert, ..
Tracing on module 'ets', function 'insert', arity unspecified -> all -
[{['$1', '$2'], (Match Head)
Matching ets:insert('$1', '$2'). implicitly means; Tracing on arity= 2 -
[{'==', exercise, '$1'}, (Match Condition)
{'==', 99, {element, 2, '$2'}}], (Match Condition) Tracing output when these conditions evaluates to ‘true’ I.e. when ‘$1’ == exercise and element(2, ‘$2’) == 99 -
[{message, {process_dump}}, {return_trace}]}]) (Match Body)