NAME
Devel::RemoteTrace - Attachable call trace of perl scripts (a.k.a) perldebguts by example
SYNOPSIS
$ perl -d:RemoteTrace your-script
or
use Devel::RemoteTrace;
DESCRIPTION
The purpose of this module is twofold. First of all it solves a real problem taht seems hard with the standard debugger: Trace the execution of a long running process when it stops serving requests.
The secondary purpose is to be an easy understandable example for writing your own debuggers. A kind of perldebgutstut (but what a horrible name that would have been).
USAGE
Devel::RemoteTrace uses UDP to send trace messages to the address defined by the environent variables DEBUG_ADDR
and DEBUG_HOST
. If neither of these are defined the default is to send to localhost:9999.
Tracing is enabled and disabled by sending the traced process an SIGUSR2. If Devel::RemoteTrace is loaded with the single argument ':trace' tracing is enabled from the beginning otherwise it is disabled from the beginning.
perldebguts by examples
Variables used or maintained by the interpreter
- $DB::single, $DB::trace, $DB::signal
-
If either of these are true the interpreter enters single step-mode where DB::DB() is called right before execution of each statement.
(Only $DB::trace are really used by Devel::RemoteTrace)
- $main::{"_<$filename"}
-
For each compiled file (
$filename
) the interpreter maintains a scalar containing the filename. This doesn't seem usefull at first, but DB::postponed is called with a glob pointing at this. - @main::{"_<$filename"}
-
For each compiled file (
$filename
) the interpreter maintains an array that holds the lines of the file.Values in this array are magical in numeric context: they compare equal to zero only if the line is not breakable.
- %DB::sub
-
The keys of this hash is the fully quallyfied name of each subroutine known to the debugger. The values is of the form 'filename:startline-endline'.
- $DB::sub
-
In DB::sub() this scalar holds the fully quallyfied name of the subroutine to be called.
- @DB::args
-
When called from the DB namespace,
caller()
places the argument for the invoked subroutine here.
Subroutine hooks
- DB::DB
-
This subroutine is called before exeution of each statement if either of $DB::single, $DB::trace, or $DB::signal is true. All debuggers are required to have this subroutine, but it might be empty.
Use
caller(0)
to get where in you code you are. - DB::sub
-
This subroutine is called instead of each normal subroutine call. The subroutine name is placed in $DB::sub and the debugger is responsibel for make the actual subroutine call in the right context. This is done either directly at the return statement:
return &{ $sub };
or by examinating wantarray():
my ($ret, @ret); # Call the function in the correct context: if (wantarray) { @ret = &{ $sub }; } elsif (defined wantarray) { $ret = &{ $sub }; } else { &{ $sub }; } # inspect the return value or something # and return the correct context return (wantarray) ? @ret : defined(wantarray) ? $ret : undef;
Use
caller()
to inspct where in the code you are called from. As a special casecaller(-1)
returns the stack frame you are about to call. - DB::postponed
-
postponed is called when perl is finished compiling either a subroutine or a complete file. For subroutine this call is only made if the subroutine name exists as a key in %DB::postponed though.
For subroutines the argument to postponed() is simply the subroutine name.
For complete files the argument to postponed is the glob
*{"_<$filename"}
referring to both the scalar containing the filename and the array containing the complete file.
BUGS, FEATURES, AND OTHER ISSUES
Using this on untrusted networks might leak security related information
Due to the nature of UDP there is no guarantee that the receiver gets all function calls. This could be "fixed" by adding a sequence number.
AUTHOR
Peter Makholm, <peter at makholm.net>
COPYRIGHT & LICENSE
Copyright 2009 Peter Makholm, all rights reserved.
This software is released under the MIT license cited below.
The "MIT" License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.