perl5db.pl is the perl debugger. It is loaded automatically by Perl when you invoke a script with perl -d. This documentation tries to outline the structure and services provided by perl5db.pl, and to describe how you can use them.

The debugger can look pretty forbidding to many Perl programmers. There are a number of reasons for this, many stemming out of the debugger's history.

When the debugger was first written, Perl didn't have a lot of its nicer features - no references, no lexical variables, no closures, no object-oriented programming. So a lot of the things one would normally have done using such features was done using global variables, globs and the local() operator in creative ways.

Some of these have survived into the current debugger; a few of the more interesting and still-useful idioms are noted in this section, along with notes on the comments themselves.

Experienced Perl programmers will note that the debugger code tends to use mostly package globals rather than lexically-scoped variables. This is done to allow a significant amount of control of the debugger from outside the debugger itself.

Unfortunately, though the variables are accessible, they're not well documented, so it's generally been a decision that hasn't made a lot of difference to most users. Where appropriate, comments have been added to make variables more accessible and usable, with the understanding that these are debugger internals, and are therefore subject to change. Future development should probably attempt to replace the globals with a well-defined API, but for now, the variables are what we've got.

As you may recall from reading perlfunc, the local() operator makes a temporary copy of a variable in the current scope. When the scope ends, the old copy is restored. This is often used in the debugger to handle the automatic stacking of variables during recursive calls:

sub foo {
local $some_global++;
# Do some stuff, then ...
return;
}

What happens is that on entry to the subroutine, $some_global is localized, then altered. When the subroutine returns, Perl automatically undoes the localization, restoring the previous value. Voila, automatic stack management.

The debugger uses this trick a lot. Of particular note is DB::eval, which lets the debugger get control inside of eval'ed code. The debugger localizes a saved copy of $@ inside the subroutine, which allows it to keep $@ safe until it DB::eval returns, at which point the previous value of $@ is restored. This makes it simple (well, simpler) to keep track of $@ inside evals which eval other eval's.

This is used to cleverly reverse the sense of a logical test depending on the value of an auxiliary variable. For instance, the debugger's S (search for subroutines by pattern) allows you to negate the pattern like this:

# Find all non-'foo' subs:
S !/foo/

Boolean algebra states that the truth table for XOR looks like this:

0 ^ 0 = 0

(! not present and no match) --> false, don't print

0 ^ 1 = 1

(! not present and matches) --> true, print

1 ^ 0 = 1

(! present and no match) --> true, print

1 ^ 1 = 0

(! present and matches) --> false, don't print

As you can see, the first pair applies when ! isn't supplied, and the second pair applies when it is. The XOR simply allows us to compact a more complicated if-then-elseif-else into a more elegant (but perhaps overly clever) single test. After all, it needed this explanation...

There is a certain C programming legacy in the debugger. Some variables, such as $single, $trace, and $frame, have magical values composed of 1, 2, 4, etc. (powers of 2) OR'ed together. This allows several pieces of state to be stored independently in a single scalar.

A test like

if ($scalar & 4) ...

is checking to see if the appropriate bit is on. Since each bit can be "addressed" independently in this way, $scalar is acting sort of like an array of bits. Obviously, since the contents of $scalar are just a bit-pattern, we can save and restore it easily (it will just look like a number).

The problem, is of course, that this tends to leave magic numbers scattered all over your program whenever a bit is set, cleared, or checked. So why do it?

First, doing an arithmetical or bitwise operation on a scalar is just about the fastest thing you can do in Perl: use constant actually creates a subroutine call, and array and hash lookups are much slower. Is this over-optimization at the expense of readability? Possibly, but the debugger accesses these variables a lot. Any rewrite of the code will probably have to benchmark alternate implementations and see which is the best balance of readability and speed, and then document how it actually works.

Second, it's very easy to serialize a scalar number. This is done in the restart code; the debugger state variables are saved in %ENV and then restored when the debugger is restarted. Having them be just numbers makes this trivial.

Third, some of these variables are being shared with the Perl core smack in the middle of the interpreter's execution loop. It's much faster for a C program (like the interpreter) to check a bit in a scalar than to access several different variables (or a Perl array).

Any comment containing XXX means that the comment is either somewhat speculative - it's not exactly clear what a given variable or chunk of code is doing, or that it is incomplete - the basics may be clear, but the subtleties are not completely documented.

There are a number of special data structures provided to the debugger by the Perl interpreter.

The array @{$main::{'_<'.$filename}} (aliased locally to @dbline via glob assignment) contains the text from $filename, with each element corresponding to a single line of $filename. Additionally, breakable lines will be dualvars with the numeric component being the memory address of a COP node. Non-breakable lines are dualvar to 0.

The hash %{'_<'.$filename} (aliased locally to %dbline via glob assignment) contains breakpoints and actions. The keys are line numbers; you can set individual values, but not the whole hash. The Perl interpreter uses this hash to determine where breakpoints have been set. Any true value is considered to be a breakpoint; perl5db.pl uses $break_condition\0$action. Values are magical in numeric context: 1 if the line is breakable, 0 if not.

The scalar ${"_<$filename"} simply contains the string $filename. This is also the case for evaluated strings that contain subroutines, or which are currently being executed. The $filename for evaled strings looks like (eval 34).

When perl5db.pl starts, it reads an rcfile (perl5db.ini for non-interactive sessions, .perldb for interactive ones) that can set a number of options. In addition, this file may define a subroutine &afterinit that will be executed (in the debugger's context) after the debugger has initialized itself.

Next, it checks the PERLDB_OPTS environment variable and treats its contents as the argument of a o command in the debugger.

The following options can only be specified at startup. To set them in your rcfile, add a call to &parse_options("optionName=new_value").

TTY

the TTY to use for debugging i/o.

noTTY

if set, goes in NonStop mode. On interrupt, if TTY is not set, uses the value of noTTY or $HOME/.perldbtty$$ to find TTY using Term::Rendezvous. Current variant is to have the name of TTY in this file.

ReadLine

if false, a dummy ReadLine is used, so you can debug ReadLine applications.

NonStop

if true, no i/o is performed until interrupt.

LineInfo

file or pipe to print line number info to. If it is a pipe, a short "emacs like" message is used.

RemotePort

host:port to connect to on remote host for remote debugging.

HistFile

file to store session history to. There is no default and so no history file is written unless this variable is explicitly set.

HistSize

number of commands to store to the file specified in HistFile. Default is 100.

Perl supplies the values for %sub. It effectively inserts a &DB::DB(); in front of each place that can have a breakpoint. At each subroutine call, it calls &DB::sub with $DB::sub set to the called subroutine. It also inserts a BEGIN {require 'perl5db.pl'} before the first line.

After each required file is compiled, but before it is executed, a call to &DB::postponed($main::{'_<'.$filename}) is done. $filename is the expanded name of the required file (as found via %INC).

Determines what messages (if any) will get printed when a subroutine (or eval) is entered or exited.

0 - No enter/exit messages

1 - Print entering messages on subroutine entry

2 - Adds exit messages on subroutine exit. If no other flag is on, acts like 1+2.

4 - Extended messages: <in|out> context=fully-qualified sub name from file:line. If no other flag is on, acts like 1+4.

8 - Adds parameter information to messages, and overloaded stringify and tied FETCH is enabled on the printed arguments. Ignored if 4 is not on.

16 - Adds context return from subname: value messages on subroutine/eval exit. Ignored if 4 is is not on.

To get everything, use $frame=30 (or o f=30 as a debugger command). The debugger internally juggles the value of $frame during execution to protect external modules that the debugger uses from getting traced.

Tracks current debugger nesting level. Used to figure out how many <> pairs to surround the line number with when the debugger outputs a prompt. Also used to help determine if the program has finished during command parsing.

This hash keeps track of breakpoints that need to be set for files that have not yet been compiled. Keys are filenames; values are references to hashes. Each of these hashes is keyed by line number, and its values are breakpoint definitions (condition\0action).

The debugger's initialization actually jumps all over the place inside this package. This is because there are several BEGIN blocks (which of course execute immediately) spread through the code. Why is that?

The debugger needs to be able to change some things and set some things up before the debugger code is compiled; most notably, the $deep variable that DB::sub uses to tell when a program has recursed deeply. In addition, the debugger has to turn off warnings while the debugger code is compiled, but then restore them to their original setting before the program being debugged begins executing.

The first BEGIN block simply turns off warnings by saving the current setting of $^W and then setting it to zero. The second one initializes the debugger variables that are needed before the debugger begins executing. The third one puts $^X back to its former value.

We'll detail the second BEGIN block later; just remember that if you need to initialize something before the debugger starts really executing, that's where it has to go.

This function replaces straight eval() inside the debugger; it simplifies the process of evaluating code in the user's context.

The code to be evaluated is passed via the package global variable $DB::evalarg; this is done to avoid fiddling with the contents of @_.

Before we do the eval(), we preserve the current settings of $trace, $single, $^D and $usercontext. The latter contains the preserved values of $@, $!, $^E, $,, $/, $\, $^W and the user's current package, grabbed when DB::DB got control. This causes the proper context to be used when the eval is actually done. Afterward, we restore $trace, $single, and $^D.

Next we need to handle $@ without getting confused. We save $@ in a local lexical, localize $saved[0] (which is where save() will put $@), and then call save() to capture $@, $!, $^E, $,, $/, $\, and $^W) and set $,, $/, $\, and $^W to values considered sane by the debugger. If there was an eval() error, we print it on the debugger's output. If $onetimedump is defined, we call dumpit if it's set to 'dump', or methods if it's set to 'methods'. Setting it to something else causes the debugger to do the eval but not print the result - handy if you want to do something else with it (the "watch expressions" code does this to get the value of the watch expression but not show it unless it matters).

In any case, we then return the list of output from eval to the caller, and unwinding restores the former version of $@ in @saved as well (the localization of $saved[0] goes away at the end of this scope).

DB::eval isn't parameterized in the standard way; this is to keep the debugger's calls to DB::eval() from mucking with @_, among other things. The variables listed below influence DB::eval()'s execution directly.

The context of DB::eval() presents us with some problems. Obviously, we want to be 'sandboxed' away from the debugger's internals when we do the eval, but we need some way to control how punctuation variables and debugger globals are used.

We can't use local, because the code inside DB::eval can see localized variables; and we can't use my either for the same reason. The code in this routine compromises and uses my.

After this routine is over, we don't have user code executing in the debugger's context, so we can use my freely.

First, it initializes the environment it wants to run in: turning off warnings during its own compilation, defining variables which it will need to avoid warnings later, setting itself up to not exit when the program terminates, and defaulting to printing return values for the r command.

If we are running under a threaded Perl, we require threads and threads::shared if the environment variable PERL5DB_THREADED is set, to enable proper threaded debugger control. -dt can also be used to set this.

Each new thread will be announced and the debugger prompt will always inform you of each new thread created. It will also indicate the thread id in which we are currently running within the prompt like this:

[tid] DB<$i>

Where [tid] is an integer thread id and $i is the familiar debugger command prompt. The prompt will show: [0] when running under threads, but not actually in a thread. [tid] is consistent with gdb usage.

While running under threads, when you set or delete a breakpoint (etc.), this will apply to all threads, not just the currently running one. When you are in a currently executing thread, you will stay there until it completes. With the current implementation it is not currently possible to hop from one thread to another.

The e and E commands are currently fairly minimal - see h e and h E.

Note that threading support was built into the debugger as of Perl version 5.8.6 and debugger version 1.2.8.

The debugger's options are actually spread out over the debugger itself and dumpvar.pl; some of these are variables to be set, while others are subs to be called with a value. To try to make this a little easier to manage, the debugger uses a few data structures to define what options are legal and how they are to be processed.

First, the @options array defines the names of all the options that are to be accepted.

Second, optionVars lists the variables that each option uses to save its state.

Third, %optionAction defines the subroutine to be called to process each option.

Last, the %optionRequire notes modules that must be required if an option is used.

There are a number of initialization-related variables which can be set by putting code to set them in a BEGIN block in the PERL5DB environment variable. These are:

The pager to be used is needed next. We try to get it from the environment first. If it's not defined there, we try to find it in the Perl Config.pm. If it's not there, we default to more. We then call the pager() function to save the pager name.

We set up the command to be used to access the man pages, the command recall character (! unless otherwise defined) and the shell escape character (! unless otherwise defined). Yes, these do conflict, and neither works in the debugger at the moment.

We then set up the gigantic string containing the debugger help. We also set the limit on the number of arguments we'll display during a trace.

The debugger greeting helps to inform the user how many debuggers are running, and whether the current debugger is the primary or a child.

If we are the primary, we just hang onto our pid so we'll have it when or if we start a child debugger. If we are a child, we'll set things up so we'll have a unique greeting and so the parent will give us our own TTY later.

We save the current contents of the PERLDB_PIDS environment variable because we mess around with it. We'll also need to hang onto it because we'll need it if we restart.

Child debuggers make a label out of the current PID structure recorded in PERLDB_PIDS plus the new PID. They also mark themselves as not having a TTY yet so the parent will give them one later via resetterm().

The debugger will read a file of initialization options if supplied. If running interactively, this is .perldb; if not, it's perldb.ini.

The debugger does a safety test of the file to be read. It must be owned either by the current user or root, and must only be writable by the owner.

The last thing we do during initialization is determine which subroutine is to be used to obtain a new terminal when a new debugger is started. Right now, the debugger only handles TCP sockets, X11, OS/2, amd Mac OS X (darwin).

This section handles the restart command. When the R command is invoked, it tries to capture all of the state it can into environment variables, and then sets PERLDB_RESTART. When we start executing again, we check to see if PERLDB_RESTART is there; if so, we reload all the information that the R command stuffed into the environment variables.

Now, we'll decide how the debugger is going to interact with the user. If there's no TTY, we set the debugger to run non-stop; there's not going to be anyone there to enter commands.

If there is a TTY, we have to determine who it belongs to before we can proceed. If this is a slave editor or graphical debugger (denoted by the first command-line switch being '-emacs'), we shift this off and set $rl to 0 (XXX ostensibly to do straight reads).

We then determine what the console should be on various systems:

Cygwin - We use stdin instead of a separate device.

Unix - use /dev/tty.

Windows or MSDOS - use con.

VMS - use sys$command.

Several other systems don't use a specific console. We undef $console for those (Windows using a slave editor/graphical debugger, NetWare, OS/2 with a slave editor).

If there is a TTY hanging around from a parent, we use that as the console.

The debugger is capable of opening a socket and carrying out a debugging session over the socket.

If RemotePort was defined in the options, the debugger assumes that it should try to start a debugging session on that port. It builds the socket and then tries to connect the input and output filehandles to it.

If no RemotePort was defined, and we want to create a TTY on startup, this is probably a situation where multiple debuggers are running (for example, a backticked command that starts up another debugger). We create a new IN and OUT filehandle, and do the necessary mojo to create a new TTY if we know how and if we can.

To finish initialization, we show the debugger greeting, and then call the afterinit() subroutine if there is one.

This gigantic subroutine is the heart of the debugger. Called before every statement, its job is to determine if a breakpoint has been reached, and stop if so; read commands from the user, parse them, and execute them, and then send execution off to the next statement.

Note that the order in which the commands are processed is very important; some commands earlier in the loop will actually alter the $cmd variable to create other commands to be executed later. This is all highly optimized but can be confusing. Check the comments for each $cmd ... && do {} to see what's happening in any given command.

The debugger decides to take control if single-step mode is on, the t command was entered, or the user generated a signal. If the program has fallen off the end, we set things up so that entering further commands won't cause trouble, and we say that the program is over.

If there's an action to be executed for the line we stopped at, execute it. If there are any preprompt actions, execute those as well.

The debugger normally shows the line corresponding to the current line of execution. Sometimes, though, we want to see the next line, or to move elsewhere in the file. This is done via the $incr, $start, and $max variables.

$incr controls by how many lines the current line should move forward after a command is executed. If set to -1, this indicates that the current line shouldn't change.

$start is the current line. It is used for things like knowing where to move forwards or backwards from when doing an L or - command.

$max tells the debugger where the last line of the current file is. It's used to terminate loops most often.

Most of DB::DB is actually a command parsing and dispatch loop. It comes in two parts:

The outer part of the loop, starting at the CMD label. This loop reads a command and then executes it.

The inner part of the loop, starting at the PIPE label. This part is wholly contained inside the CMD block and only executes a command. Used to handle commands running inside a pager.

So why have two labels to restart the loop? Because sometimes, it's easier to have a command generate another command and then re-execute the loop to do the new command. This is faster, but perhaps a bit more convoluted.

A newline entered by itself means re-execute the last command. We grab the command out of $laststep (where it was recorded previously), and copy it back into $cmd to be executed below. If there wasn't any previous command, we'll do nothing below (no command will match). If there was, we also save it in the command history and fall through to allow the command parsing to pick it up.

The debugger can create aliases for commands (these are stored in the %alias hash). Before a command is executed, the command loop looks it up in the alias hash and substitutes the contents of the alias for the command, completely replacing it.

We change $start to be one window back; if we go back past the first line, we set it to be the first line. We ser $incr to put us back at the currently-executing line, and then put a l $start + (list one window from $start) in $cmd to be executed later.

In Perl 5.8.0, a realignment of the commands was done to fix up a number of problems, most notably that the default case of several commands destroying the user's work in setting watchpoints, actions, etc. We wanted, however, to retain the old commands for those who were used to using them or who preferred them. At this point, we check for the new commands and call cmd_wrapper to deal with them instead of processing them in-line.

All of the commands below this point don't work after the program being debugged has ended. All of them check to see if the program has ended; this allows the commands to be relocated without worrying about a 'line of demarcation' above which commands can be entered anytime, and below which they can't.

Most of the code for this command is taken up with locating the optional breakpoint, which is either a subroutine name or a line number. We set the appropriate one-time-break in @dbline and then turn off single-stepping in this and all call levels above this one.

For r to work properly, the debugger has to stop execution again immediately after the return is executed. This is done by forcing single-stepping to be on in the call level above the current one. If we are printing return values when a r is executed, set $doret appropriately, and force us out of the command loop.

We take the argument and treat it as a pattern. If it turns out to be a bad one, we return the error we got from trying to eval it and exit. If not, we create some code to do the search and eval it so it can't mess us up.

For |, we save OUT (the debugger's output filehandle) and STDOUT (the program's standard output). For ||, we only save OUT. We open a pipe to the pager (restoring the output filehandles if this fails). If this is the | command, we also set up a SIGPIPE handler which will simply set $signal, sending us back into the debugger.

We then trim off the pipe symbols and redo the command loop at the PIPE label, causing us to evaluate the command in $cmd without reading another.

Anything left in $cmd at this point is a Perl expression that we want to evaluate. We'll always evaluate in the user's context, and fully qualify any variables we might want to address in the DB package.

When commands have finished executing, we come here. If the user closed the input filehandle, we turn on $fall_off_end to emulate a q command. We evaluate any post-prompt items. We restore $@, $!, $^E, $,, $/, $\, and $^W, and return a null list as expected by the Perl interpreter. The interpreter will then execute the next line and then return control to us again.

Special check: if we're in package DB::fake, we've gone through the END block at least once. We set up everything so that we can continue to enter commands and have a valid context to be in.

If the program hasn't finished executing, we scan forward to the next executable line, print that out, build the prompt from the file and line number information, and print that.

sub is called whenever a subroutine call happens in the program being debugged. The variable $DB::sub contains the name of the subroutine being called.

The core function of this subroutine is to actually call the sub in the proper context, capturing its output. This of course causes DB::DB to get called again, repeating until the subroutine ends and returns control to DB::sub again. Once control returns, DB::sub figures out whether or not to dump the return value, and returns its captured copy of the return value as its own return value. The value then feeds back into the program being debugged as if DB::sub hadn't been there at all.

sub does all the work of printing the subroutine entry and exit messages enabled by setting $frame. It notes what sub the autoloader got called for, and also prints the return value if needed (for the r command and if the 16 bit is set in $frame).

It also tracks the subroutine call depth by saving the current setting of $single in the @stack package global; if this exceeds the value in $deep, sub automatically turns on printing of the current depth by setting the 4 bit in $single. In any case, it keeps the current setting of stop/don't stop on entry to subs set as it currently is set.

In Perl 5.8.0, there was a major realignment of the commands and what they did, Most of the changes were to systematize the command structure and to eliminate commands that threw away user input without checking.

The following sections describe the code added to make it easy to support multiple command sets with conflicting command names. This section is a start at unifying all command processing to make it simpler to develop commands.

Note that all the cmd_[a-zA-Z] subroutines require the command name, a line number, and $dbline (the current line) as arguments.

Support functions in this section which have multiple modes of failure die on error; the rest simply return a false value.

The user-interface functions (all of the cmd_* functions) just output error messages.

The %set hash defines the mapping from command letter to subroutine name suffix.

%set is a two-level hash, indexed by set name and then by command name. Note that trying to set the CommandSet to foobar simply results in the 5.8.0 command set being used, since there's no top-level entry for foobar.

cmd_wrapper() allows the debugger to switch command sets depending on the value of the CommandSet option.

It tries to look up the command in the %set package-level lexical (which means external entities can't fiddle with it) and create the name of the sub to call based on the value found in the hash (if it's there). All of the commands to be handled in a set have to be added to %set; if they aren't found, the 5.8.0 equivalent is called (if there is one).

delete_action accepts either a line number or undef. If a line number is specified, we check for the line being executable (if it's not, it couldn't have had an action). If it is, we just take the action off (this will get any kind of an action, including breakpoints).

Set breakpoints. Since breakpoints can be set in so many places, in so many ways, conditionally or not, the breakpoint code is kind of complex. Mostly, we try to parse the command type, and then shuttle it off to an appropriate subroutine to actually do the work of setting the breakpoint in the right place.

Several of the functions we need to implement in the API need to work both on the current file and on other files. We don't want to duplicate code, so $filename_error is used to contain the name of the file that's being worked on (if it's not the current one).

We can now build functions in pairs: the basic function works on the current file, and uses $filename_error as part of its error message. Since this is initialized to "", no filename will appear when we are working on the current file.

The second function is a wrapper which does the following:

Localizes $filename_error and sets it to the name of the file to be processed.

Localizes the *dbline glob and reassigns it to point to the file we want to process.

Calls the first function.

The first function works on the current file (i.e., the one we changed to), and prints $filename_error in the error message (the name of the other file) if it needs to. When the functions return, *dbline is restored to point to the actual current file (the one we're executing in) and $filename_error is restored to "". This restores everything to the way it was before the second function was called at all.

See the comments in breakable_line and breakable_line_in_file for more details.

This actually does the work of deleting either a single breakpoint, or all of them.

For a single line, we look for it in @dbline. If it's nonbreakable, we just drop out with a message saying so. If it is, we remove the condition part of the 'condition\0action' that says there's a breakpoint here. If, after we've done that, there's nothing left, we delete the corresponding line in %dbline to signal that no action needs to be taken for this line.

For all breakpoints, we iterate through the keys of %had_breakpoints, which lists all currently-loaded files which have breakpoints. We then look at each line in each of these files, temporarily switching the %dbline and @dbline structures to point to the files in question, and do what we did in the single line case: delete the condition in @dbline, and delete the key in %dbline if nothing's left.

We then wholesale delete %postponed, %postponed_file, and %break_on_load, because these structures contain breakpoints for files and code that haven't been loaded yet. We can just kill these off because there are no magical debugger structures associated with them.

Most of the command is taken up with transforming all the different line specification syntaxes into 'start-stop'. After that is done, the command runs a loop over @dbline for the specified range of lines. It handles the printing of each line and any markers (==> for current line, b for break on this line, a for action on this line, : for this line breakable).

We save the last line listed in the $start global for further listing later.

To list breakpoints, the command has to look determine where all of them are first. It starts a %had_breakpoints, which tells us what all files have breakpoints and/or actions. For each file, we switch the *dbline glob (the magic source and breakpoint data structures) to the file, and then look through %dbline for lines with breakpoints and/or actions, listing them out. We look through %postponed not-yet-compiled subroutines that have breakpoints, and through %postponed_file for not-yet-require'd files that have breakpoints.

Uses the $preview variable set in the second BEGIN block (q.v.) to move back a few lines to list the selected line in context. Uses cmd_l to do the actual listing after figuring out the range of line to request.

The 5.8 version of this command adds a watch expression if one is specified; it does nothing if entered with no operands.

We extract the expression, save it, evaluate it in the user's context, and save the value. We'll re-evaluate it each time the debugger passes a line, and will stop (see the code at the top of the command loop) if the value of any of the expressions changes.

This command accepts either a watch expression to be removed from the list of watch expressions, or * to delete them all.

If * is specified, we simply empty the watch expression list and the watch expression value list. We also turn off the bit that says we've got watch expressions.

If an expression (or partial expression) is specified, we pattern-match through the expressions and remove the ones that match. We also discard the corresponding values. If no watch expressions are left, we turn off the watching expressions bit.

print_lineinfo prints whatever it is that it is handed; it prints it to the $LINEINFO filehandle instead of just printing it to STDOUT. This allows us to feed line information to a slave editor without messing up the debugger output.

Handles setting postponed breakpoints in subroutines once they're compiled. For breakpoints, we use DB::find_sub to locate the source file and line range for the subroutine, then mark the file as having a breakpoint, temporarily switch the *dbline glob over to the source file, and then search the given range of lines to find a breakable line. If we find one, we set the breakpoint on it, deleting the breakpoint from %postponed.

Called after each required file is compiled, but before it is executed; also called if the name of a just-compiled subroutine is a key of %postponed. Propagates saved breakpoints (from b compile, b load, etc.) into the just-compiled code.

If this is a require'd file, the incoming parameter is the glob *{"_<$filename"}, with $filename the name of the require'd file.

It gets a filehandle (to which dumpvar.pl's output will be directed) and a reference to a variable (the thing to be dumped) as its input.

The incoming filehandle is selected for output (dumpvar.pl is printing to the currently-selected filehandle, thank you very much). The current values of the package globals $single and $trace are backed up in lexicals, and they are turned off (this keeps the debugger from trying to single-step through dumpvar.pl (I think.)). $frame is localized to preserve its current value and it is set to zero to prevent entry/exit messages from printing, and $doret is localized as well and set to -2 to prevent return values from being shown.

dumpit() then checks to see if it needs to load dumpvar.pl and tries to load it (note: if you have a dumpvar.pl ahead of the installed version in @INC, yours will be used instead. Possible security problem?).

It then checks to see if the subroutine main::dumpValue is now defined it should have been defined by dumpvar.pl). If it has, dumpit() localizes the globals necessary for things to be sane when main::dumpValue() is called, and picks up the variable to be dumped from the parameter list.

It checks the package global %options to see if there's a dumpDepth specified. If not, -1 is assumed; if so, the supplied value gets passed on to dumpvar.pl. This tells dumpvar.pl where to leave off when dumping a structure: -1 means dump everything.

dumpValue() is then called if possible; if not, dumpit()just prints a warning.

In either case, $single, $trace, $frame, and $doret are restored and we then return to the caller.

print_trace's job is to print a stack trace. It does this via the dump_trace routine, which actually does all the ferreting-out of the stack trace data. print_trace takes care of formatting it nicely and printing it to the proper filehandle.

Parameters:

The filehandle to print to.

How many frames to skip before starting trace.

How many frames to print.

A flag: if true, print a short trace without filenames, line numbers, or arguments

The original comment below seems to be noting that the traceback may not be correct if this routine is called in a tied method.

Actually collect the traceback information available via caller(). It does some filtering and cleanup of the data, but mostly it just collects it to make print_trace()'s job easier.

skip defines the number of stack frames to be skipped, working backwards from the most current. count determines the total number of frames to be returned; all of them (well, the first 10^9) are returned if count is omitted.

This routine returns a list of hashes, from most-recent to least-recent stack frame. Each has the following keys and values:

action() takes input provided as the argument to an add-action command, either pre- or post-, and makes sure it's a complete command. It doesn't do any fancy parsing; it just keeps reading input until it gets a string without a trailing backslash.

This routine mostly just packages up a regular expression to be used to check that the thing it's being matched against has properly-matched curly braces.

Of note is the definition of the $balanced_brace_re global via ||=, which speeds things up by only creating the qr//'ed expression once; if it's already defined, we don't try to define it again. A speed hack.

Top-level function called when we want to set up a new terminal for use by the debugger.

If the noTTY debugger option was set, we'll either use the terminal supplied (the value of the noTTY option), or we'll use Term::Rendezvous to find one. If we're a forked debugger, we call resetterm to try to get a whole new terminal if we can.

In either case, we set up the terminal next. If the ReadLine option was true, we'll get a Term::ReadLine object for the current terminal and save the appropriate attributes. We then

When the process being debugged forks, or the process invokes a command via system() which starts a new debugger, we need to be able to get a new IN and OUT filehandle for the new debugger. Otherwise, the two processes fight over the terminal, and you can never quite be sure who's going to get the input you're typing.

get_fork_TTY is a glob-aliased function which calls the real function that is tasked with doing all the necessary operating system mojo to get a new TTY (and probably another window) and to direct the new debugger to read and write there.

The debugger provides get_fork_TTY functions which work for TCP socket servers, X11, OS/2, and Mac OS X. Other systems are not supported. You are encouraged to write get_fork_TTY functions which work for your platform and contribute them.

This function provides the get_fork_TTY function for X11. If a program running under the debugger forks, a new <xterm> window is opened and the subsidiary debugger is directed there.

The open() call is of particular note here. We have the new xterm we're spawning route file number 3 to STDOUT, and then execute the tty command (which prints the device name of the TTY we'll want to use for input and output to STDOUT, then sleep for a very long time, routing this output to file number 3. This way we can simply read from the <XT> filehandle (which is STDOUT from the commands we ran) to get the TTY we want to use.

Only works if xterm is in your path and $ENV{DISPLAY}, etc. are properly set up.

If the new debugger happened because of a system() that invoked a program under the debugger, the arrow between the old pid and the new in the prompt has two dashes instead of one.

We take the current list of pids and add this one to the end. If there isn't any list yet, we make one up out of the initial pid associated with the terminal and our new pid, sticking an arrow (either one-dashed or two dashed) in between them.

If CreateTTY is off, or resetterm was called with no arguments, we don't try to create a new IN and OUT filehandle. Otherwise, we go ahead and try to do that.

First, we handle stuff in the typeahead buffer. If there is any, we shift off the next line, print a message saying we got it, add it to the terminal history (if possible), and return it.

If there's nothing in the typeahead buffer, check the command filehandle stack. If there are any filehandles there, read from the last one, and return the line if we got one. If not, we pop the filehandle off and close it, and try the next one up the stack.

If we've emptied the filehandle stack, we check to see if we've got a socket open, and we read that and return it if we do. If we don't, we just call the core readline() and return its value.

Handles the parsing and execution of option setting/displaying commands.

An option entered by itself is assumed to be set me to 1 (the default value) if the option is a boolean one. If not, the user is prompted to enter a valid value or to query the current value (via option? ).

If option=value is entered, we try to extract a quoted string from the value (if it is quoted). If it's not, we just use the whole value as-is.

We load any modules required to service this option, and then we set it: if it just gets stuck in a variable, we do that; if there's a subroutine to handle setting the option, we call that.

Finally, if we're running in interactive mode, we display the effect of the user's command back to the terminal, skipping this if we're setting things during initialization.

Set_list packages up items to be stored in a set of environment variables (VAR_n, containing the number of items, and VAR_0, VAR_1, etc., containing the values). Values outside the standard ASCII charset are stored by encoding then as hexadecimal values.

The catch() subroutine is the essence of fast and low-impact. We simply set an already-existing global scalar variable to a constant value. This avoids allocating any memory possibly in the middle of something that will get all confused if we do, particularly under unsafe signals.

warn emits a warning, by joining together its arguments and printing them, with couple of fillips.

If the composited message doesn't end with a newline, we automatically add $! and a newline to the end of the message. The subroutine expects $OUT to be set to the filehandle to be used to output warnings; it makes no assumptions about what filehandles are available.

This routine handles restoring the debugger's input and output filehandles after we've tried and failed to move them elsewhere. In addition, it assigns the debugger's output filehandle to $LINEINFO if it was already open there.

Sets the input and output filehandles to the specified files or pipes. If the terminal supports switching, we go ahead and do it. If not, and there's already a terminal in place, we save the information to take effect on restart.

If there's no terminal yet (for instance, during debugger initialization), we go ahead and set $console and $tty to the file indicated.

Sets the $rl option variable. If 0, we use Term::ReadLine::Stub (essentially, no readline processing on this terminal). Otherwise, we use Term::ReadLine. Can't be changed after a terminal's in place; we save the value in case a restart is done so we can change it then.

For the M command: list modules loaded and their versions. Essentially just runs through the keys in %INC, picks each package's $VERSION variable, gets the file name, and formats the information for output.

The help message is a peculiar format unto itself; it mixes podornaments () with tabs to come up with a format that's fairly easy to parse and portable, but which still allows the help to be a little nicer than just plain text.

Essentially, you define the command name (usually marked up with and ), followed by a tab, and then the descriptive text, ending in a newline. The descriptive text can also be marked up in the same way. If you need to continue the descriptive text to another line, start that line with just tabs and then enter the marked-up text.

If you are modifying the help text, be careful. The help-string parser is not very sophisticated, and if you don't follow these rules it will mangle the help beyond hope until you fix the string.

Most of what print_help does is just text formatting. It finds the B and I ornaments, cleans them off, and substitutes the proper terminal control characters to simulate them (courtesy of Term::ReadLine::TermCap).

This routine does a lot of gyrations to be sure that the pager is less. It checks for less masquerading as more and records the result in $fixed_less so we don't have to go through doing the stats again.

The debugger's own $SIG{__DIE__} handler. Handles providing a stack trace by loading Carp and calling Carp::longmess() to get it. We turn off single stepping and tracing during the call to Carp::longmess to avoid debugging it - we just want to use it.

If dieLevel is zero, we let the program being debugged handle the exceptions. If it's 1, you get backtraces for any exception. If it's 2, the debugger takes over all exception handling, printing a backtrace and displaying the exception via its dbwarn() routine.

Set the $DB::warnLevel variable that stores the value of the warnLevel option. Calling warnLevel() with a positive value results in the debugger taking over all warning handlers. Setting warnLevel to zero leaves any warning handlers set up by the program being debugged in place.

These subroutines are used during the x and X commands to try to produce as much information as possible about a code reference. They use Devel::Peek to try to find the glob in which this code reference lives (if it does) - this allows us to actually code references which correspond to named subroutines (including those aliased via glob assignment).

A utility routine used in various places; finds the file where a subroutine was defined, and returns that filename and a line-number range.

Tries to use @sub first; if it can't find it there, it tries building a reference to the subroutine and uses CvGV_name_or_bust to locate it, loading it into @sub as a side effect (XXX I think). If it can't find it this way, it brute-force searches %sub, checking for identical references.

methods_via does the work of crawling up the @ISA tree and reporting all the parent class methods. $class is the name of the next class to try; $prefix is the message prefix, which gets built up as we go up the @ISA tree to show parentage; $crawl_upward is 1 if we should try to go higher in the @ISA tree, 0 if we should stop.

Because of the way the debugger interface to the Perl core is designed, any debugger package globals that DB::sub() requires have to be defined before any subroutines can be called. These are defined in the second BEGIN block.

This block sets things up so that (basically) the world is sane before the debugger starts executing. We set up various variables that the debugger has to have set up before the Perl core starts running:

The debugger's own filehandles (copies of STD and STDOUT for now).

Characters for shell escapes, the recall command, and the history command.

The maximum recursion depth.

The size of a w command's window.

The before-this-line context to be printed in a v (view a window around this line) command.

The fact that we're not in a sub at all right now.

The default SIGINT handler for the debugger.

The appropriate value of the flag in $^D that says the debugger is running

Take a partially-qualified package and find all subpackages for it by getting all the subpackages for the package so far, matching all the subpackages against the text, and discarding all of them which start with 'main::'. Return this list.

Under the debugger, source files are represented as _</fullpath/to/file (evals are _<(eval NNN)) keys in %main::. We pull all of these out of %main::, add the initial source file, and extract the ones that match the completion text so far.

We set the prefix to the item's sigil, and trim off the sigil to get the text to be completed.

We look for the lexical scope above DB::DB and auto-complete lexical variables if PadWalker could be loaded.

If the package is :: (main), create an empty list; if it's something else, create a list of all the packages known. Append whichever list to a list of all the possible symbols in the current package. grep out the matches to the text entered so far, then map the prefix back onto the symbols.

If there's only one hit, it's a package qualifier, and it's not equal to the initial text, recomplete using this symbol.

We use option_val() to look up the current value of the option. If there's only a single value, we complete the command in such a way that it is a complete command for setting the option in question. If there are multiple possible values, we generate a command consisting of the option plus a trailing question mark, which, if executed, will list the current value of the option.

Restarting the debugger is a complex operation that occurs in several phases. First, we try to reconstruct the command line that was used to invoke Perl and the debugger.

After the command line has been reconstructed, the next step is to save the debugger's status in environment variables. The DB::set_list routine is used to save aggregate variables (both hashes and arrays); scalars are just popped into environment variables directly.

The most complex part of this is the saving of all of the breakpoints. They can live in an awful lot of places, and we have to go through all of them, find the breakpoints, and then save them in the appropriate environment variable via DB::set_list.

After all the debugger status has been saved, we take the command we built up and then return it, so we can exec() it. The debugger will spot the PERLDB_RESTART environment variable and realize it needs to reload its state from the environment.

Come here at the very end of processing. We want to go into a loop where we allow the user to enter commands and interact with the debugger, but we don't want anything else to execute.

First we set the $finished variable, so that some commands that shouldn't be run after the end of program quit working.

We then figure out whether we're truly done (as in the user entered a q command, or we finished execution while running nonstop). If we aren't, we set $single to 1 (causing the debugger to get control again).

We then call DB::fake::at_exit(), which returns the Use 'q' to quit ... message and returns control to the debugger. Repeat.

When the user finally enters a q command, $fall_off_end is set to 1 and the END block simply exits with $single set to 0 (don't break, run to completion.).

Some of the commands changed function quite a bit in the 5.8 command realignment, so much so that the old code had to be replaced completely. Because we wanted to retain the option of being able to go back to the former command set, we moved the old code off to this section.

The debugger used to have a bunch of nearly-identical code to handle the pre-and-post-prompt action commands. cmd_pre590_prepost and cmd_prepost unify all this into one set of code to handle the appropriate actions.

Actually does all the handling for <, >, {{, {, etc. Since the lists of actions are all held in arrays that are pointed to by references anyway, all we have to do is pick the right array reference and then use generic code to all, delete, or list actions.