Generating a trace

Generating a trace with the perl debugger.

Having the right tool for the job is essential. It’s all very well having a big, impressive hammer in your toolbox, but if you  ave a nut and a bolt to put together, you won’t get very far unless you have a spanner too.

A trace is a report of a program’s position in the code execution stack, how it got there and, in the case of subroutines and methods, what arguments were passed from the calling subroutine. Tracing is an invaluable tool for describing what your program is doing, which may not be what you thought it was doing.

Before we go any further, to give you an idea of the sort of information that is available, here is a short sample trace from the middle of executing the charcount from the previous chapter. This is generated by pressing T at the debugger prompt, and find that it’s already tracing code we never even wrote.

$ = Symbol::gensym() called from file \
`/opt/perl/lib/5.18.5/i686-linux/IO/Handle.pm’ line 306
$ = IO::Handle::new(‘FileHandle’) called from file \
`/opt/perl/lib/5.18.5/i686-linux/IO/File.pm’ line 145
$ = IO::File::new(‘FileHandle’, ‘< walrus’) called from file \
`charcount’ line 12

This is a three line stack trace, split for ease of viewing, from a single point in the code and shows how we came to this position, from which line in which file, the namespace and subroutine of the current context, and with what arguments it was called. It even notes the context is scalar, denoted by the $ on the left of each line.

All the information in a trace can look daunting at first, not least because the information provided is verbose.

For more information, see the Pro-Perl-Debugging book.

Find this content useful? Share it with your friends or comment below!

Leave a Comment