Author image Joe Hildebrand


Devel::TraceFuncs - trace function calls as they happen.



  require Devel::TraceFuncs;

  max_trace_depth 5;
  trace_file "foo.out";
  trace_file $file_handle;

  sub foo {
    IN(my $f, "a message");

    DEBUG "hellooo!";


Devel::TraceFuncs provides utilities to trace the execution of a program. It can print traces that look something like:

   +-> global: '0'
   |  +-> main::fo(4, 5) (in ./ 'now then'
   |  |  +-> main::fp(4, 5) (in ./
   |  |  |  +-> main::fq() (in ./
   |  |  |  |  que pee doll (in ./
   |  |  |  +-< main::fq() (in ./
   |  |  |  cee dee (in ./
   |  |  +-< main::fp(4, 5) (in ./
   |  |  ha
   |  |  hs (in ./
   |  +-< main::fo(4, 5) (in ./ 'now then'
   |  done (in ./
   +-< global: '0'


A trace begins when a function calls IN. A my'd variable is passed in, such that when that function exits, the destructor for the variable is called. If this trace is to be printed, the opening line of the trace in printed at this time. Any other parameters are concatenated together, and printed on both the opening and closing lines of the trace.

I wish the syntax could be a little nicer here, but I couldn't find anything in perl that resembles Tcl's uplevel or upvar commands. If I was one of the perl gods, I could have figured out a way to do something like

   sub sub {
     # create a new subroutine, with a my'd TraceFunc object


Print some text to the trace file, at the correct depth in the trace. If the last parameter ends in "!", the arguments are printed, regardless of current depth.


trace_file takes one argument, which is either a file name or an open file handle. All trace output will go to this file.


To avoid lots of nesting, particularly from recursive function calls, you can set the maximum depth to be traced. If this is -1 (the default), all levels of functions are traced. If it is 0, no trace output occurs, except for DEBUG statements that end in "!".


   #!/usr/local/bin/perl -w
   use Devel::TraceFuncs;
   use strict;
   sub fq {
     IN(my $f);
     DEBUG "que", "pee", "doll!";
   sub fp {
     IN(my $f);
     DEBUG "cee", "dee";
   sub fo {
     IN(my $f, "now", "then");
     DEBUG "ha\nhs";
   if (@ARGV) {
     max_trace_depth shift;
   if (@ARGV) {
     trace_file shift;
   IN(my $f, 0);
   DEBUG "done";


For some reason, the closing lines are reversed in this example:

   use Devel::TraceFuncs;

   max_trace_depth -1;

   sub g {
     IN(my $f);
   sub f {
     IN(my $f);


What it boils down to is not letting IN be the last line of a function. In the debugger, the objects are destructed in the correct order, so this must be caused by some sort of performance optimization in the perl runtime.


Joe Hildebrand

  Copyright (c) 1996 Joe Hildebrand. All rights reserved.
  This program is free software; you can redistribute it and/or
  modify it under the same terms as Perl itself.


Version 0.1, 1 Jun 1996