📄 perldebug.1
字号:
See \*(L"Debugger Internals\*(R" in perldebguts for details..Sh "Debugger input/output".IX Subsection "Debugger input/output".IP "Prompt" 8.IX Item "Prompt"The debugger prompt is something like.Sp.Vb 1\& DB<8>.Ve.Spor even.Sp.Vb 1\& DB<<17>>.Ve.Spwhere that number is the command number, and which you'd use toaccess with the built-in \fBcsh\fR\-like history mechanism. For example,\&\f(CW\*(C`!17\*(C'\fR would repeat command number 17. The depth of the anglebrackets indicates the nesting depth of the debugger. You couldget more than one set of brackets, for example, if you'd alreadyat a breakpoint and then printed the result of a function call thatitself has a breakpoint, or you step into an expression via \f(CW\*(C`s/n/texpression\*(C'\fR command..IP "Multiline commands" 8.IX Item "Multiline commands"If you want to enter a multi-line command, such as a subroutinedefinition with several statements or a format, escape the newlinethat would normally end the debugger command with a backslash.Here's an example:.Sp.Vb 7\& DB<1> for (1..4) { \e\& cont: print "ok\en"; \e\& cont: }\& ok\& ok\& ok\& ok.Ve.SpNote that this business of escaping a newline is specific to interactivecommands typed into the debugger..IP "Stack backtrace" 8.IX Xref "backtrace stack, backtrace".IX Item "Stack backtrace"Here's an example of what a stack backtrace via \f(CW\*(C`T\*(C'\fR command mightlook like:.Sp.Vb 3\& $ = main::infested called from file \`Ambulation.pm\*(Aq line 10\& @ = Ambulation::legs(1, 2, 3, 4) called from file \`camel_flea\*(Aq line 7\& $ = main::pests(\*(Aqbactrian\*(Aq, 4) called from file \`camel_flea\*(Aq line 4.Ve.SpThe left-hand character up there indicates the context in which thefunction was called, with \f(CW\*(C`$\*(C'\fR and \f(CW\*(C`@\*(C'\fR meaning scalar or listcontexts respectively, and \f(CW\*(C`.\*(C'\fR meaning void context (which isactually a sort of scalar context). The display above saysthat you were in the function \f(CW\*(C`main::infested\*(C'\fR when you ran thestack dump, and that it was called in scalar context from line10 of the file \fIAmbulation.pm\fR, but without any arguments at all,meaning it was called as \f(CW&infested\fR. The next stack frame showsthat the function \f(CW\*(C`Ambulation::legs\*(C'\fR was called in list contextfrom the \fIcamel_flea\fR file with four arguments. The last stackframe shows that \f(CW\*(C`main::pests\*(C'\fR was called in scalar context,also from \fIcamel_flea\fR, but from line 4..SpIf you execute the \f(CW\*(C`T\*(C'\fR command from inside an active \f(CW\*(C`use\*(C'\fRstatement, the backtrace will contain both a \f(CW\*(C`require\*(C'\fR frame andan \f(CW\*(C`eval\*(C'\fR) frame..IP "Line Listing Format" 8.IX Item "Line Listing Format"This shows the sorts of output the \f(CW\*(C`l\*(C'\fR command can produce:.Sp.Vb 11\& DB<<13>> l\& 101: @i{@i} = ();\& 102:b @isa{@i,$pack} = ()\& 103 if(exists $i{$prevpack} || exists $isa{$pack});\& 104 }\& 105\& 106 next\& 107==> if(exists $isa{$pack});\& 108\& 109:a if ($extra\-\- > 0) {\& 110: %isa = ($pack,1);.Ve.SpBreakable lines are marked with \f(CW\*(C`:\*(C'\fR. Lines with breakpoints aremarked by \f(CW\*(C`b\*(C'\fR and those with actions by \f(CW\*(C`a\*(C'\fR. The line that'sabout to be executed is marked by \f(CW\*(C`==>\*(C'\fR..SpPlease be aware that code in debugger listings may not look the sameas your original source code. Line directives and external sourcefilters can alter the code before Perl sees it, causing code to movefrom its original positions or take on entirely different forms..IP "Frame listing" 8.IX Item "Frame listing"When the \f(CW\*(C`frame\*(C'\fR option is set, the debugger would print entered (andoptionally exited) subroutines in different styles. See perldebgutsfor incredibly long examples of these..Sh "Debugging compile-time statements".IX Subsection "Debugging compile-time statements"If you have compile-time executable statements (such as code within\&\s-1BEGIN\s0, \s-1UNITCHECK\s0 and \s-1CHECK\s0 blocks or \f(CW\*(C`use\*(C'\fR statements), these will\&\fInot\fR be stopped by debugger, although \f(CW\*(C`require\*(C'\fRs and \s-1INIT\s0 blockswill, and compile-time statements can be traced with \f(CW\*(C`AutoTrace\*(C'\fRoption set in \f(CW\*(C`PERLDB_OPTS\*(C'\fR). From your own Perl code, however, youcan transfer control back to the debugger using the followingstatement, which is harmless if the debugger is not running:.PP.Vb 1\& $DB::single = 1;.Ve.PPIf you set \f(CW$DB::single\fR to 2, it's equivalent to havingjust typed the \f(CW\*(C`n\*(C'\fR command, whereas a value of 1 means the \f(CW\*(C`s\*(C'\fRcommand. The \f(CW$DB::trace\fR variable should be set to 1 to simulatehaving typed the \f(CW\*(C`t\*(C'\fR command..PPAnother way to debug compile-time code is to start the debugger, set abreakpoint on the \fIload\fR of some module:.PP.Vb 2\& DB<7> b load f:/perllib/lib/Carp.pm\& Will stop on load of \`f:/perllib/lib/Carp.pm\*(Aq..Ve.PPand then restart the debugger using the \f(CW\*(C`R\*(C'\fR command (if possible). One can use \f(CW\*(C`bcompile subname\*(C'\fR for the same purpose..Sh "Debugger Customization".IX Subsection "Debugger Customization"The debugger probably contains enough configuration hooks that youwon't ever have to modify it yourself. You may change the behaviourof debugger from within the debugger using its \f(CW\*(C`o\*(C'\fR command, fromthe command line via the \f(CW\*(C`PERLDB_OPTS\*(C'\fR environment variable, andfrom customization files..PPYou can do some customization by setting up a \fI.perldb\fR file, whichcontains initialization code. For instance, you could make aliaseslike these (the last one is one people expect to be there):.PP.Vb 4\& $DB::alias{\*(Aqlen\*(Aq} = \*(Aqs/^len(.*)/p length($1)/\*(Aq;\& $DB::alias{\*(Aqstop\*(Aq} = \*(Aqs/^stop (at|in)/b/\*(Aq;\& $DB::alias{\*(Aqps\*(Aq} = \*(Aqs/^ps\eb/p scalar /\*(Aq;\& $DB::alias{\*(Aqquit\*(Aq} = \*(Aqs/^quit(\es*)/exit/\*(Aq;.Ve.PPYou can change options from \fI.perldb\fR by using calls like this one;.PP.Vb 1\& parse_options("NonStop=1 LineInfo=db.out AutoTrace=1 frame=2");.Ve.PPThe code is executed in the package \f(CW\*(C`DB\*(C'\fR. Note that \fI.perldb\fR isprocessed before processing \f(CW\*(C`PERLDB_OPTS\*(C'\fR. If \fI.perldb\fR defines thesubroutine \f(CW\*(C`afterinit\*(C'\fR, that function is called after debuggerinitialization ends. \fI.perldb\fR may be contained in the currentdirectory, or in the home directory. Because this file is sourcedin by Perl and may contain arbitrary commands, for security reasons,it must be owned by the superuser or the current user, and writableby no one but its owner..PPYou can mock \s-1TTY\s0 input to debugger by adding arbitrary commands to\&\f(CW@DB::typeahead\fR. For example, your \fI.perldb\fR file might contain:.PP.Vb 1\& sub afterinit { push @DB::typeahead, "b 4", "b 6"; }.Ve.PPWhich would attempt to set breakpoints on lines 4 and 6 immediatelyafter debugger initialization. Note that \f(CW@DB::typeahead\fR is not a supportedinterface and is subject to change in future releases..PPIf you want to modify the debugger, copy \fIperl5db.pl\fR from thePerl library to another name and hack it to your heart's content.You'll then want to set your \f(CW\*(C`PERL5DB\*(C'\fR environment variable to saysomething like this:.PP.Vb 1\& BEGIN { require "myperl5db.pl" }.Ve.PPAs a last resort, you could also use \f(CW\*(C`PERL5DB\*(C'\fR to customize the debuggerby directly setting internal variables or calling debugger functions..PPNote that any variables and functions that are not documented inthis document (or in perldebguts) are considered for internaluse only, and as such are subject to change without notice..Sh "Readline Support".IX Subsection "Readline Support"As shipped, the only command-line history supplied is a simplistic onethat checks for leading exclamation points. However, if you installthe Term::ReadKey and Term::ReadLine modules from \s-1CPAN\s0, you willhave full editing capabilities much like \s-1GNU\s0 \fIreadline\fR(3) provides.Look for these in the \fImodules/by\-module/Term\fR directory on \s-1CPAN\s0.These do not support normal \fBvi\fR command-line editing, however..PPA rudimentary command-line completion is also available.Unfortunately, the names of lexical variables are not available forcompletion..Sh "Editor Support for Debugging".IX Subsection "Editor Support for Debugging"If you have the \s-1FSF\s0's version of \fBemacs\fR installed on your system,it can interact with the Perl debugger to provide an integratedsoftware development environment reminiscent of its interactionswith C debuggers..PPPerl comes with a start file for making \fBemacs\fR act like asyntax-directed editor that understands (some of) Perl's syntax.Look in the \fIemacs\fR directory of the Perl source distribution..PPA similar setup by Tom Christiansen for interacting with anyvendor-shipped \fBvi\fR and the X11 window system is also available.This works similarly to the integrated multiwindow support that\&\fBemacs\fR provides, where the debugger drives the editor. At thetime of this writing, however, that tool's eventual location in thePerl distribution was uncertain..PPUsers of \fBvi\fR should also look into \fBvim\fR and \fBgvim\fR, the mouseyand windy version, for coloring of Perl keywords..PPNote that only perl can truly parse Perl, so all such \s-1CASE\s0 toolsfall somewhat short of the mark, especially if you don't programyour Perl as a C programmer might..Sh "The Perl Profiler".IX Xref "profile profiling profiler".IX Subsection "The Perl Profiler"If you wish to supply an alternative debugger for Perl to run, justinvoke your script with a colon and a package argument given to the\&\fB\-d\fR flag. The most popular alternative debuggers for Perl is thePerl profiler. Devel::DProf is now included with the standard Perldistribution. To profile your Perl program in the file \fImycode.pl\fR,just type:.PP.Vb 1\& $ perl \-d:DProf mycode.pl.Ve.PPWhen the script terminates the profiler will dump the profileinformation to a file called \fItmon.out\fR. A tool like \fBdprofpp\fR,also supplied with the standard Perl distribution, can be used tointerpret the information in that profile..SH "Debugging regular expressions".IX Xref "regular expression, debugging regex, debugging regexp, debugging".IX Header "Debugging regular expressions"\&\f(CW\*(C`use re \*(Aqdebug\*(Aq\*(C'\fR enables you to see the gory details of how the Perlregular expression engine works. In order to understand this typicallyvoluminous output, one must not only have some idea about how regularexpression matching works in general, but also know how Perl's regularexpressions are internally compiled into an automaton. These mattersare explored in some detail in\&\*(L"Debugging regular expressions\*(R" in perldebguts..SH "Debugging memory usage".IX Xref "memory usage".IX Header "Debugging memory usage"Perl contains internal support for reporting its own memory usage,but this is a fairly advanced concept that requires some understandingof how memory allocation works.See \*(L"Debugging Perl memory usage\*(R" in perldebguts for the details..SH "SEE ALSO".IX Header "SEE ALSO"You did try the \fB\-w\fR switch, didn't you?.PPperldebtut,perldebguts,re,\&\s-1DB\s0,Devel::DProf,dprofpp,Dumpvalue,andperlrun..PPWhen debugging a script that uses #! and is thus normally found in\&\f(CW$PATH\fR, the \-S option causes perl to search \f(CW$PATH\fR for it, so you don'thave to type the path or \f(CW\*(C`which $scriptname\*(C'\fR..PP.Vb 1\& $ perl \-Sd foo.pl.Ve.SH "BUGS".IX Header "BUGS"You cannot get stack frame information or in any fashion debug functionsthat were not compiled by Perl, such as those from C or \*(C+ extensions..PPIf you alter your \f(CW@_\fR arguments in a subroutine (such as with \f(CW\*(C`shift\*(C'\fRor \f(CW\*(C`pop\*(C'\fR), the stack backtrace will not show the original values..PPThe debugger does not currently work in conjunction with the \fB\-W\fRcommand-line switch, because it itself is not free of warnings..PPIf you're in a slow syscall (like \f(CW\*(C`wait\*(C'\fRing, \f(CW\*(C`accept\*(C'\fRing, or \f(CW\*(C`read\*(C'\fRingfrom your keyboard or a socket) and haven't set up your own \f(CW$SIG{INT}\fRhandler, then you won't be able to CTRL-C your way back to the debugger,because the debugger's own \f(CW$SIG{INT}\fR handler doesn't understand thatit needs to raise an exception to \fIlongjmp\fR\|(3) out of slow syscalls.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -