perl5db.html
来自「perl教程」· HTML 代码 · 共 1,157 行 · 第 1/5 页
HTML
1,157 行
are "stacked" via <a href="../lib/Pod/perlfunc.html#item_local"><code>local()</code></a>, enabling recursive calls to <a href="#item_eval"><code>DB::eval()</code></a>.</p>
<dl>
<dt><strong><a name="item__res"><code>@res</code> - used to capture output from actual <a href="#item_eval"><code>eval</code></a>.</a></strong>
<dt><strong><a name="item__otrace"><code>$otrace</code> - saved value of <a href="#item__trace"><code>$trace</code></a>.</a></strong>
<dt><strong><a name="item__osingle"><code>$osingle</code> - saved value of <a href="#item__single"><code>$single</code></a>.</a></strong>
<dt><strong><a name="item__od"><code>$od</code> - saved value of <a href="../lib/Pod/perlvar.html#item___d"><code>$^D</code></a>.</a></strong>
<dt><strong><a name="item__saved_0_"><code>$saved[0]</code> - saved value of <a href="../lib/Pod/perlvar.html#item___"><code>$@</code></a>.</a></strong>
<dt><strong><a name="item___">$\ - for output of <a href="../lib/Pod/perlvar.html#item___"><code>$@</code></a> if there is an evaluation error.</a></strong>
</dl>
<p>
</p>
<h3><a name="the_problem_of_lexicals">The problem of lexicals</a></h3>
<p>The context of <a href="#item_eval"><code>DB::eval()</code></a> 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.</p>
<p>We can't use local, because the code inside <code>DB::eval</code> can see localized
variables; and we can't use <a href="../lib/Pod/perlfunc.html#item_my"><code>my</code></a> either for the same reason. The code
in this routine compromises and uses <a href="../lib/Pod/perlfunc.html#item_my"><code>my</code></a>.</p>
<p>After this routine is over, we don't have user code executing in the debugger's
context, so we can use <a href="../lib/Pod/perlfunc.html#item_my"><code>my</code></a> freely.</p>
<p>
</p>
<hr />
<h1><a name="debugger_initialization">DEBUGGER INITIALIZATION</a></h1>
<p>The debugger starts up in phases.</p>
<p>
</p>
<h2><a name="basic_setup">BASIC SETUP</a></h2>
<p>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 <a href="../lib/Pod/perlguts.html#item_r"><code>r</code></a> command.</p>
<p>
</p>
<h2><a name="threads_support">THREADS SUPPORT</a></h2>
<p>If we are running under a threaded Perl, we require threads and threads::shared
if the environment variable <a href="../lib/Pod/perlrun.html#item_perl5db_threaded"><code>PERL5DB_THREADED</code></a> is set, to enable proper
threaded debugger control. <a href="../lib/Pod/perlrun.html#item__2ddt"><code>-dt</code></a> can also be used to set this.</p>
<p>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:</p>
<pre>
[tid] DB<$i></pre>
<p>Where <code>[tid]</code> is an integer thread id and <code>$i</code> is the familiar debugger
command prompt. The prompt will show: <code>[0]</code> when running under threads, but
not actually in a thread. <code>[tid]</code> is consistent with <code>gdb</code> usage.</p>
<p>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.</p>
<p>The <code>e</code> and <a href="../lib/Pod/perlguts.html#item_e"><code>E</code></a> commands are currently fairly minimal - see <code>h e</code> and <code>h E</code>.</p>
<p>Note that threading support was built into the debugger as of Perl version
<code>5.8.6</code> and debugger version <code>1.2.8</code>.</p>
<p>
</p>
<hr />
<h1><a name="option_processing">OPTION PROCESSING</a></h1>
<p>The debugger's options are actually spread out over the debugger itself and
<code>dumpvar.pl</code>; 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.</p>
<p>First, the <code>@options</code> array defines the <em>names</em> of all the options that
are to be accepted.</p>
<p>Second, <code>optionVars</code> lists the variables that each option uses to save its
state.</p>
<p>Third, <code>%optionAction</code> defines the subroutine to be called to process each
option.</p>
<p>Last, the <code>%optionRequire</code> notes modules that must be <a href="../lib/Pod/perlfunc.html#item_require"><code>require</code></a>d if an
option is used.</p>
<p>There are a number of initialization-related variables which can be set
by putting code to set them in a BEGIN block in the <a href="../lib/Pod/perlrun.html#item_perl5db"><code>PERL5DB</code></a> environment
variable. These are:</p>
<dl>
<dt><strong><a name="item__rl"><code>$rl</code> - readline control XXX needs more explanation</a></strong>
<dt><strong><a name="item__warnlevel"><code>$warnLevel</code> - whether or not debugger takes over warning handling</a></strong>
<dt><strong><a name="item__dielevel"><code>$dieLevel</code> - whether or not debugger takes over die handling</a></strong>
<dt><strong><a name="item__signallevel"><code>$signalLevel</code> - whether or not debugger takes over signal handling</a></strong>
<dt><strong><a name="item_actions"><code>$pre</code> - preprompt actions (array reference)</a></strong>
<dt><strong><code>$post</code> - postprompt actions (array reference)</strong>
<dt><strong><a name="item__pretype"><code>$pretype</code></a></strong>
<dt><strong><a name="item__createtty"><code>$CreateTTY</code> - whether or not to create a new TTY for this debugger</a></strong>
<dt><strong><a name="item_use"><code>$CommandSet</code> - which command set to use (defaults to new, documented set)</a></strong>
</dl>
<p>The default <a href="../lib/Pod/perlfunc.html#item_die"><code>die</code></a>, <a href="../lib/Pod/perlfunc.html#item_warn"><code>warn</code></a>, and <code>signal</code> handlers are set up.</p>
<p>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 <code>Config.pm</code>. If it's not there, we default to <code>more</code>. We
then call the <code>pager()</code> function to save the pager name.</p>
<p>We set up the command to be used to access the man pages, the command
recall character (<code>!</code> unless otherwise defined) and the shell escape
character (<code>!</code> unless otherwise defined). Yes, these do conflict, and
neither works in the debugger at the moment.</p>
<p>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.</p>
<p>
</p>
<h2><a name="setting_up_the_debugger_greeting">SETTING UP THE DEBUGGER GREETING</a></h2>
<p>The debugger <em>greeting</em> helps to inform the user how many debuggers are
running, and whether the current debugger is the primary or a child.</p>
<p>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.</p>
<p>We save the current contents of the <code>PERLDB_PIDS</code> environment variable
because we mess around with it. We'll also need to hang onto it because
we'll need it if we restart.</p>
<p>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 <code>resetterm()</code>.</p>
<p>
</p>
<h2><a name="reading_the_rc_file">READING THE RC FILE</a></h2>
<p>The debugger will read a file of initialization options if supplied. If
running interactively, this is <code>.perldb</code>; if not, it's <code>perldb.ini</code>.</p>
<p>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.</p>
<p>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 X Windows and OS/2.</p>
<p>
</p>
<h2><a name="restart_processing">RESTART PROCESSING</a></h2>
<p>This section handles the restart command. When the <code>R</code> command is invoked, it
tries to capture all of the state it can into environment variables, and
then sets <code>PERLDB_RESTART</code>. When we start executing again, we check to see
if <code>PERLDB_RESTART</code> is there; if so, we reload all the information that
the R command stuffed into the environment variables.</p>
<pre>
PERLDB_RESTART - flag only, contains no restart data itself.
PERLDB_HIST - command history, if it's available
PERLDB_ON_LOAD - breakpoints set by the rc file
PERLDB_POSTPONE - subs that have been loaded/not executed, and have actions
PERLDB_VISITED - files that had breakpoints
PERLDB_FILE_... - breakpoints for a file
PERLDB_OPT - active options
PERLDB_INC - the original @INC
PERLDB_PRETYPE - preprompt debugger actions
PERLDB_PRE - preprompt Perl code
PERLDB_POST - post-prompt Perl code
PERLDB_TYPEAHEAD - typeahead captured by readline()</pre>
<p>We chug through all these variables and plug the values saved in them
back into the appropriate spots in the debugger.</p>
<p>
</p>
<h2><a name="setting_up_the_terminal">SETTING UP THE TERMINAL</a></h2>
<p>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.</p>
<p>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 <a href="#item__rl"><code>$rl</code></a> to 0 (XXX ostensibly to do straight reads).</p>
<p>We then determine what the console should be on various systems:</p>
<ul>
<li><strong><a name="item_cygwin__2d_we_use_stdin_instead_of_a_separate_devi">Cygwin - We use <code>stdin</code> instead of a separate device.</a></strong>
<li><strong><a name="item_unix__2d_use__2fdev_2ftty_2e">Unix - use <code>/dev/tty</code>.</a></strong>
<li><strong><a name="item_windows_or_msdos__2d_use_con_2e">Windows or MSDOS - use <code>con</code>.</a></strong>
<li><strong><a name="item_macos__2d_use_dev_3aconsole_3aperl_debug_if_this_i">MacOS - use <code>Dev:Console:Perl Debug</code> if this is the MPW version; <code>Dev:
Console</code> if not.</a></strong>
<p>Note that Mac OS X returns <code>darwin</code>, not <code>MacOS</code>. Also note that the debugger doesn't do anything special for <code>darwin</code>. Maybe it should.</p>
</li>
<li><strong><a name="item_vms__2d_use_sys_24command_2e">VMS - use <code>sys$command</code>.</a></strong>
</ul>
<p>Several other systems don't use a specific console. We <code>undef $console</code>
for those (Windows using a slave editor/graphical debugger, NetWare, OS/2
with a slave editor, Epoc).</p>
<p>If there is a TTY hanging around from a parent, we use that as the console.</p>
<p>
</p>
<h2><a name="socket_handling">SOCKET HANDLING</a></h2>
<p>The debugger is capable of opening a socket and carrying out a debugging
session over the socket.</p>
<p>If <a href="#item_remoteport"><code>RemotePort</code></a> 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.</p>
<p>If no <a href="#item_remoteport"><code>RemotePort</code></a> 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.</p>
<p>To finish initialization, we show the debugger greeting,
and then call the <code>afterinit()</code> subroutine if there is one.</p>
<p>
</p>
<hr />
<h1><a name="subroutines">SUBROUTINES</a></h1>
<p>
</p>
<h2><a name="db">DB</a></h2>
<p>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 hen send execution off to the next statement.</p>
<p>Note that the order in which the commands are processed is very important;
some commands earlier in the loop will actually alter the <code>$cmd</code> variable
to create other commands to be executed later. This is all highly <em>optimized</em>
but can be confusing. Check the comments for each <code>$cmd ... && do {}</code> to
see what's happening in any given command.</p>
<p>
</p>
<h2><a name="watchfunction__"><a href="#item_watchfunction"><code>watchfunction()</code></a></a></h2>
<p><a href="#item_watchfunction"><code>watchfunction()</code></a> is a function that can be defined by the user; it is a
function which will be run on each entry to <code>DB::DB</code>; it gets the
current package, filename, and line as its parameters.</p>
<p>The watc
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?