Commit | Line | Data |
---|---|---|
e22ea7cc | 1 | |
69893cff RGS |
2 | =head1 NAME |
3 | ||
4 | C<perl5db.pl> - the perl debugger | |
5 | ||
6 | =head1 SYNOPSIS | |
7 | ||
8 | perl -d your_Perl_script | |
9 | ||
10 | =head1 DESCRIPTION | |
11 | ||
12 | C<perl5db.pl> is the perl debugger. It is loaded automatically by Perl when | |
13 | you invoke a script with C<perl -d>. This documentation tries to outline the | |
14 | structure and services provided by C<perl5db.pl>, and to describe how you | |
15 | can use them. | |
16 | ||
17 | =head1 GENERAL NOTES | |
18 | ||
19 | The debugger can look pretty forbidding to many Perl programmers. There are | |
20 | a number of reasons for this, many stemming out of the debugger's history. | |
21 | ||
22 | When the debugger was first written, Perl didn't have a lot of its nicer | |
23 | features - no references, no lexical variables, no closures, no object-oriented | |
24 | programming. So a lot of the things one would normally have done using such | |
25 | features was done using global variables, globs and the C<local()> operator | |
26 | in creative ways. | |
27 | ||
28 | Some of these have survived into the current debugger; a few of the more | |
29 | interesting and still-useful idioms are noted in this section, along with notes | |
30 | on the comments themselves. | |
31 | ||
32 | =head2 Why not use more lexicals? | |
33 | ||
34 | Experienced Perl programmers will note that the debugger code tends to use | |
35 | mostly package globals rather than lexically-scoped variables. This is done | |
36 | to allow a significant amount of control of the debugger from outside the | |
37 | debugger itself. | |
38 | ||
39 | Unfortunately, though the variables are accessible, they're not well | |
40 | documented, so it's generally been a decision that hasn't made a lot of | |
41 | difference to most users. Where appropriate, comments have been added to | |
42 | make variables more accessible and usable, with the understanding that these | |
43 | i<are> debugger internals, and are therefore subject to change. Future | |
44 | development should probably attempt to replace the globals with a well-defined | |
45 | API, but for now, the variables are what we've got. | |
46 | ||
47 | =head2 Automated variable stacking via C<local()> | |
48 | ||
49 | As you may recall from reading C<perlfunc>, the C<local()> operator makes a | |
50 | temporary copy of a variable in the current scope. When the scope ends, the | |
51 | old copy is restored. This is often used in the debugger to handle the | |
52 | automatic stacking of variables during recursive calls: | |
53 | ||
54 | sub foo { | |
55 | local $some_global++; | |
56 | ||
57 | # Do some stuff, then ... | |
58 | return; | |
59 | } | |
60 | ||
61 | What happens is that on entry to the subroutine, C<$some_global> is localized, | |
62 | then altered. When the subroutine returns, Perl automatically undoes the | |
63 | localization, restoring the previous value. Voila, automatic stack management. | |
64 | ||
65 | The debugger uses this trick a I<lot>. Of particular note is C<DB::eval>, | |
66 | which lets the debugger get control inside of C<eval>'ed code. The debugger | |
67 | localizes a saved copy of C<$@> inside the subroutine, which allows it to | |
68 | keep C<$@> safe until it C<DB::eval> returns, at which point the previous | |
69 | value of C<$@> is restored. This makes it simple (well, I<simpler>) to keep | |
70 | track of C<$@> inside C<eval>s which C<eval> other C<eval's>. | |
71 | ||
72 | In any case, watch for this pattern. It occurs fairly often. | |
73 | ||
74 | =head2 The C<^> trick | |
75 | ||
76 | This is used to cleverly reverse the sense of a logical test depending on | |
77 | the value of an auxiliary variable. For instance, the debugger's C<S> | |
78 | (search for subroutines by pattern) allows you to negate the pattern | |
79 | like this: | |
80 | ||
81 | # Find all non-'foo' subs: | |
82 | S !/foo/ | |
83 | ||
84 | Boolean algebra states that the truth table for XOR looks like this: | |
85 | ||
86 | =over 4 | |
87 | ||
88 | =item * 0 ^ 0 = 0 | |
89 | ||
90 | (! not present and no match) --> false, don't print | |
91 | ||
92 | =item * 0 ^ 1 = 1 | |
93 | ||
94 | (! not present and matches) --> true, print | |
95 | ||
96 | =item * 1 ^ 0 = 1 | |
97 | ||
98 | (! present and no match) --> true, print | |
99 | ||
100 | =item * 1 ^ 1 = 0 | |
101 | ||
102 | (! present and matches) --> false, don't print | |
103 | ||
104 | =back | |
105 | ||
106 | As you can see, the first pair applies when C<!> isn't supplied, and | |
107 | the second pair applies when it isn't. The XOR simply allows us to | |
108 | compact a more complicated if-then-elseif-else into a more elegant | |
109 | (but perhaps overly clever) single test. After all, it needed this | |
110 | explanation... | |
111 | ||
112 | =head2 FLAGS, FLAGS, FLAGS | |
113 | ||
114 | There is a certain C programming legacy in the debugger. Some variables, | |
115 | such as C<$single>, C<$trace>, and C<$frame>, have "magical" values composed | |
116 | of 1, 2, 4, etc. (powers of 2) OR'ed together. This allows several pieces | |
117 | of state to be stored independently in a single scalar. | |
118 | ||
119 | A test like | |
120 | ||
121 | if ($scalar & 4) ... | |
122 | ||
123 | is checking to see if the appropriate bit is on. Since each bit can be | |
124 | "addressed" independently in this way, C<$scalar> is acting sort of like | |
125 | an array of bits. Obviously, since the contents of C<$scalar> are just a | |
126 | bit-pattern, we can save and restore it easily (it will just look like | |
127 | a number). | |
128 | ||
129 | The problem, is of course, that this tends to leave magic numbers scattered | |
130 | all over your program whenever a bit is set, cleared, or checked. So why do | |
131 | it? | |
132 | ||
133 | =over 4 | |
134 | ||
135 | ||
136 | =item * First, doing an arithmetical or bitwise operation on a scalar is | |
137 | just about the fastest thing you can do in Perl: C<use constant> actually | |
138 | creates a subroutine call, and array hand hash lookups are much slower. Is | |
139 | this over-optimization at the expense of readability? Possibly, but the | |
140 | debugger accesses these variables a I<lot>. Any rewrite of the code will | |
141 | probably have to benchmark alternate implementations and see which is the | |
142 | best balance of readability and speed, and then document how it actually | |
143 | works. | |
144 | ||
145 | =item * Second, it's very easy to serialize a scalar number. This is done in | |
146 | the restart code; the debugger state variables are saved in C<%ENV> and then | |
147 | restored when the debugger is restarted. Having them be just numbers makes | |
148 | this trivial. | |
149 | ||
150 | =item * Third, some of these variables are being shared with the Perl core | |
151 | smack in the middle of the interpreter's execution loop. It's much faster for | |
152 | a C program (like the interpreter) to check a bit in a scalar than to access | |
153 | several different variables (or a Perl array). | |
154 | ||
155 | =back | |
156 | ||
157 | =head2 What are those C<XXX> comments for? | |
158 | ||
159 | Any comment containing C<XXX> means that the comment is either somewhat | |
160 | speculative - it's not exactly clear what a given variable or chunk of | |
161 | code is doing, or that it is incomplete - the basics may be clear, but the | |
162 | subtleties are not completely documented. | |
163 | ||
164 | Send in a patch if you can clear up, fill out, or clarify an C<XXX>. | |
165 | ||
166 | =head1 DATA STRUCTURES MAINTAINED BY CORE | |
167 | ||
168 | There are a number of special data structures provided to the debugger by | |
169 | the Perl interpreter. | |
170 | ||
171 | The array C<@{$main::{'_<'.$filename}}> (aliased locally to C<@dbline> via glob | |
172 | assignment) contains the text from C<$filename>, with each element | |
173 | corresponding to a single line of C<$filename>. | |
174 | ||
175 | The hash C<%{'_<'.$filename}> (aliased locally to C<%dbline> via glob | |
176 | assignment) contains breakpoints and actions. The keys are line numbers; | |
177 | you can set individual values, but not the whole hash. The Perl interpreter | |
178 | uses this hash to determine where breakpoints have been set. Any true value is | |
179 | considered to be a breakpoint; C<perl5db.pl> uses "$break_condition\0$action". | |
180 | Values are magical in numeric context: 1 if the line is breakable, 0 if not. | |
181 | ||
182 | The scalar ${'_<'.$filename} contains $filename XXX What? | |
183 | ||
184 | =head1 DEBUGGER STARTUP | |
185 | ||
186 | When C<perl5db.pl> starts, it reads an rcfile (C<perl5db.ini> for | |
187 | non-interactive sessions, C<.perldb> for interactive ones) that can set a number | |
188 | of options. In addition, this file may define a subroutine C<&afterinit> | |
189 | that will be executed (in the debugger's context) after the debugger has | |
190 | initialized itself. | |
191 | ||
192 | Next, it checks the C<PERLDB_OPTS> environment variable and treats its | |
193 | contents as the argument of a debugger <C<o> command. | |
194 | ||
195 | =head2 STARTUP-ONLY OPTIONS | |
196 | ||
197 | The following options can only be specified at startup. | |
198 | To set them in your rcfile, add a call to | |
199 | C<&parse_options("optionName=new_value")>. | |
200 | ||
201 | =over 4 | |
202 | ||
203 | =item * TTY | |
204 | ||
205 | the TTY to use for debugging i/o. | |
206 | ||
207 | =item * noTTY | |
208 | ||
209 | if set, goes in NonStop mode. On interrupt, if TTY is not set, | |
210 | uses the value of noTTY or "/tmp/perldbtty$$" to find TTY using | |
211 | Term::Rendezvous. Current variant is to have the name of TTY in this | |
212 | file. | |
213 | ||
214 | =item * ReadLine | |
215 | ||
216 | If false, a dummy ReadLine is used, so you can debug | |
217 | ReadLine applications. | |
218 | ||
219 | =item * NonStop | |
220 | ||
221 | if true, no i/o is performed until interrupt. | |
222 | ||
223 | =item * LineInfo | |
224 | ||
225 | file or pipe to print line number info to. If it is a | |
226 | pipe, a short "emacs like" message is used. | |
227 | ||
228 | =item * RemotePort | |
229 | ||
230 | host:port to connect to on remote host for remote debugging. | |
231 | ||
232 | =back | |
233 | ||
234 | =head3 SAMPLE RCFILE | |
235 | ||
236 | &parse_options("NonStop=1 LineInfo=db.out"); | |
237 | sub afterinit { $trace = 1; } | |
238 | ||
239 | The script will run without human intervention, putting trace | |
240 | information into C<db.out>. (If you interrupt it, you had better | |
241 | reset C<LineInfo> to something "interactive"!) | |
242 | ||
243 | =head1 INTERNALS DESCRIPTION | |
244 | ||
245 | =head2 DEBUGGER INTERFACE VARIABLES | |
246 | ||
247 | Perl supplies the values for C<%sub>. It effectively inserts | |
248 | a C<&DB'DB();> in front of each place that can have a | |
249 | breakpoint. At each subroutine call, it calls C<&DB::sub> with | |
250 | C<$DB::sub> set to the called subroutine. It also inserts a C<BEGIN | |
251 | {require 'perl5db.pl'}> before the first line. | |
252 | ||
253 | After each C<require>d file is compiled, but before it is executed, a | |
254 | call to C<&DB::postponed($main::{'_<'.$filename})> is done. C<$filename> | |
255 | is the expanded name of the C<require>d file (as found via C<%INC>). | |
256 | ||
257 | =head3 IMPORTANT INTERNAL VARIABLES | |
258 | ||
259 | =head4 C<$CreateTTY> | |
260 | ||
261 | Used to control when the debugger will attempt to acquire another TTY to be | |
262 | used for input. | |
263 | ||
264 | =over | |
265 | ||
266 | =item * 1 - on C<fork()> | |
267 | ||
268 | =item * 2 - debugger is started inside debugger | |
269 | ||
270 | =item * 4 - on startup | |
271 | ||
272 | =back | |
273 | ||
274 | =head4 C<$doret> | |
275 | ||
276 | The value -2 indicates that no return value should be printed. | |
277 | Any other positive value causes C<DB::sub> to print return values. | |
278 | ||
279 | =head4 C<$evalarg> | |
280 | ||
281 | The item to be eval'ed by C<DB::eval>. Used to prevent messing with the current | |
282 | contents of C<@_> when C<DB::eval> is called. | |
283 | ||
284 | =head4 C<$frame> | |
285 | ||
286 | Determines what messages (if any) will get printed when a subroutine (or eval) | |
287 | is entered or exited. | |
288 | ||
289 | =over 4 | |
290 | ||
291 | =item * 0 - No enter/exit messages | |
292 | ||
293 | =item * 1 - Print "entering" messages on subroutine entry | |
294 | ||
295 | =item * 2 - Adds exit messages on subroutine exit. If no other flag is on, acts like 1+2. | |
296 | ||
297 | =item * 4 - Extended messages: C<in|out> I<context>=I<fully-qualified sub name> from I<file>:I<line>>. If no other flag is on, acts like 1+4. | |
298 | ||
299 | =item * 8 - Adds parameter information to messages, and overloaded stringify and tied FETCH is enabled on the printed arguments. Ignored if C<4> is not on. | |
300 | ||
301 | =item * 16 - Adds C<I<context> return from I<subname>: I<value>> messages on subroutine/eval exit. Ignored if C<4> is is not on. | |
302 | ||
303 | =back | |
304 | ||
305 | To get everything, use C<$frame=30> (or C<o f-30> as a debugger command). | |
306 | The debugger internally juggles the value of C<$frame> during execution to | |
307 | protect external modules that the debugger uses from getting traced. | |
308 | ||
309 | =head4 C<$level> | |
310 | ||
311 | Tracks current debugger nesting level. Used to figure out how many | |
312 | C<E<lt>E<gt>> pairs to surround the line number with when the debugger | |
313 | outputs a prompt. Also used to help determine if the program has finished | |
314 | during command parsing. | |
315 | ||
316 | =head4 C<$onetimeDump> | |
317 | ||
318 | Controls what (if anything) C<DB::eval()> will print after evaluating an | |
319 | expression. | |
320 | ||
321 | =over 4 | |
322 | ||
323 | =item * C<undef> - don't print anything | |
324 | ||
325 | =item * C<dump> - use C<dumpvar.pl> to display the value returned | |
326 | ||
327 | =item * C<methods> - print the methods callable on the first item returned | |
328 | ||
329 | =back | |
330 | ||
331 | =head4 C<$onetimeDumpDepth> | |
332 | ||
333 | Controls how far down C<dumpvar.pl> will go before printing '...' while | |
334 | dumping a structure. Numeric. If C<undef>, print all levels. | |
335 | ||
336 | =head4 C<$signal> | |
337 | ||
338 | Used to track whether or not an C<INT> signal has been detected. C<DB::DB()>, | |
339 | which is called before every statement, checks this and puts the user into | |
340 | command mode if it finds C<$signal> set to a true value. | |
341 | ||
342 | =head4 C<$single> | |
343 | ||
344 | Controls behavior during single-stepping. Stacked in C<@stack> on entry to | |
345 | each subroutine; popped again at the end of each subroutine. | |
346 | ||
347 | =over 4 | |
348 | ||
349 | =item * 0 - run continuously. | |
350 | ||
351 | =item * 1 - single-step, go into subs. The 's' command. | |
352 | ||
353 | =item * 2 - single-step, don't go into subs. The 'n' command. | |
354 | ||
355 | =item * 4 - print current sub depth (turned on to force this when "too much | |
356 | recursion" occurs. | |
357 | ||
358 | =back | |
359 | ||
360 | =head4 C<$trace> | |
361 | ||
362 | Controls the output of trace information. | |
363 | ||
364 | =over 4 | |
365 | ||
366 | =item * 1 - The C<t> command was entered to turn on tracing (every line executed is printed) | |
367 | ||
368 | =item * 2 - watch expressions are active | |
369 | ||
370 | =item * 4 - user defined a C<watchfunction()> in C<afterinit()> | |
371 | ||
372 | =back | |
373 | ||
374 | =head4 C<$slave_editor> | |
375 | ||
376 | 1 if C<LINEINFO> was directed to a pipe; 0 otherwise. | |
377 | ||
378 | =head4 C<@cmdfhs> | |
379 | ||
380 | Stack of filehandles that C<DB::readline()> will read commands from. | |
381 | Manipulated by the debugger's C<source> command and C<DB::readline()> itself. | |
382 | ||
383 | =head4 C<@dbline> | |
384 | ||
385 | Local alias to the magical line array, C<@{$main::{'_<'.$filename}}> , | |
386 | supplied by the Perl interpreter to the debugger. Contains the source. | |
387 | ||
388 | =head4 C<@old_watch> | |
389 | ||
390 | Previous values of watch expressions. First set when the expression is | |
391 | entered; reset whenever the watch expression changes. | |
392 | ||
393 | =head4 C<@saved> | |
394 | ||
395 | Saves important globals (C<$@>, C<$!>, C<$^E>, C<$,>, C<$/>, C<$\>, C<$^W>) | |
396 | so that the debugger can substitute safe values while it's running, and | |
397 | restore them when it returns control. | |
398 | ||
399 | =head4 C<@stack> | |
400 | ||
401 | Saves the current value of C<$single> on entry to a subroutine. | |
402 | Manipulated by the C<c> command to turn off tracing in all subs above the | |
403 | current one. | |
404 | ||
405 | =head4 C<@to_watch> | |
406 | ||
407 | The 'watch' expressions: to be evaluated before each line is executed. | |
408 | ||
409 | =head4 C<@typeahead> | |
410 | ||
411 | The typeahead buffer, used by C<DB::readline>. | |
412 | ||
413 | =head4 C<%alias> | |
414 | ||
415 | Command aliases. Stored as character strings to be substituted for a command | |
416 | entered. | |
417 | ||
418 | =head4 C<%break_on_load> | |
419 | ||
420 | Keys are file names, values are 1 (break when this file is loaded) or undef | |
421 | (don't break when it is loaded). | |
422 | ||
423 | =head4 C<%dbline> | |
424 | ||
425 | Keys are line numbers, values are "condition\0action". If used in numeric | |
426 | context, values are 0 if not breakable, 1 if breakable, no matter what is | |
427 | in the actual hash entry. | |
428 | ||
429 | =head4 C<%had_breakpoints> | |
430 | ||
431 | Keys are file names; values are bitfields: | |
432 | ||
433 | =over 4 | |
434 | ||
435 | =item * 1 - file has a breakpoint in it. | |
436 | ||
437 | =item * 2 - file has an action in it. | |
438 | ||
439 | =back | |
440 | ||
441 | A zero or undefined value means this file has neither. | |
442 | ||
443 | =head4 C<%option> | |
444 | ||
445 | Stores the debugger options. These are character string values. | |
446 | ||
447 | =head4 C<%postponed> | |
448 | ||
449 | Saves breakpoints for code that hasn't been compiled yet. | |
450 | Keys are subroutine names, values are: | |
451 | ||
452 | =over 4 | |
453 | ||
454 | =item * 'compile' - break when this sub is compiled | |
455 | ||
456 | =item * 'break +0 if <condition>' - break (conditionally) at the start of this routine. The condition will be '1' if no condition was specified. | |
457 | ||
458 | =back | |
459 | ||
460 | =head4 C<%postponed_file> | |
461 | ||
462 | This hash keeps track of breakpoints that need to be set for files that have | |
463 | not yet been compiled. Keys are filenames; values are references to hashes. | |
464 | Each of these hashes is keyed by line number, and its values are breakpoint | |
465 | definitions ("condition\0action"). | |
466 | ||
467 | =head1 DEBUGGER INITIALIZATION | |
468 | ||
469 | The debugger's initialization actually jumps all over the place inside this | |
470 | package. This is because there are several BEGIN blocks (which of course | |
471 | execute immediately) spread through the code. Why is that? | |
472 | ||
473 | The debugger needs to be able to change some things and set some things up | |
474 | before the debugger code is compiled; most notably, the C<$deep> variable that | |
475 | C<DB::sub> uses to tell when a program has recursed deeply. In addition, the | |
476 | debugger has to turn off warnings while the debugger code is compiled, but then | |
477 | restore them to their original setting before the program being debugged begins | |
478 | executing. | |
479 | ||
480 | The first C<BEGIN> block simply turns off warnings by saving the current | |
481 | setting of C<$^W> and then setting it to zero. The second one initializes | |
482 | the debugger variables that are needed before the debugger begins executing. | |
483 | The third one puts C<$^X> back to its former value. | |
484 | ||
485 | We'll detail the second C<BEGIN> block later; just remember that if you need | |
486 | to initialize something before the debugger starts really executing, that's | |
487 | where it has to go. | |
488 | ||
489 | =cut | |
490 | ||
a687059c LW |
491 | package DB; |
492 | ||
9eba6a4e AV |
493 | use IO::Handle; |
494 | ||
54d04a52 | 495 | # Debugger for Perl 5.00x; perl5db.pl patch level: |
7fddc82f | 496 | $VERSION = 1.27; |
69893cff | 497 | |
e22ea7cc | 498 | $header = "perl5db.pl version $VERSION"; |
d338d6fe | 499 | |
69893cff RGS |
500 | =head1 DEBUGGER ROUTINES |
501 | ||
502 | =head2 C<DB::eval()> | |
503 | ||
504 | This function replaces straight C<eval()> inside the debugger; it simplifies | |
505 | the process of evaluating code in the user's context. | |
506 | ||
507 | The code to be evaluated is passed via the package global variable | |
508 | C<$DB::evalarg>; this is done to avoid fiddling with the contents of C<@_>. | |
509 | ||
510 | We preserve the current settings of X<C<$trace>>, X<C<$single>>, and X<C<$^D>>; | |
511 | add the X<C<$usercontext>> (that's the preserved values of C<$@>, C<$!>, | |
512 | C<$^E>, C<$,>, C<$/>, C<$\>, and C<$^W>, grabbed when C<DB::DB> got control, | |
513 | and the user's current package) and a add a newline before we do the C<eval()>. | |
514 | This causes the proper context to be used when the eval is actually done. | |
515 | Afterward, we restore C<$trace>, C<$single>, and C<$^D>. | |
516 | ||
517 | Next we need to handle C<$@> without getting confused. We save C<$@> in a | |
518 | local lexical, localize C<$saved[0]> (which is where C<save()> will put | |
519 | C<$@>), and then call C<save()> to capture C<$@>, C<$!>, C<$^E>, C<$,>, | |
520 | C<$/>, C<$\>, and C<$^W>) and set C<$,>, C<$/>, C<$\>, and C<$^W> to values | |
521 | considered sane by the debugger. If there was an C<eval()> error, we print | |
522 | it on the debugger's output. If X<C<$onetimedump>> is defined, we call | |
523 | X<C<dumpit>> if it's set to 'dump', or X<C<methods>> if it's set to | |
524 | 'methods'. Setting it to something else causes the debugger to do the eval | |
525 | but not print the result - handy if you want to do something else with it | |
526 | (the "watch expressions" code does this to get the value of the watch | |
527 | expression but not show it unless it matters). | |
528 | ||
529 | In any case, we then return the list of output from C<eval> to the caller, | |
530 | and unwinding restores the former version of C<$@> in C<@saved> as well | |
531 | (the localization of C<$saved[0]> goes away at the end of this scope). | |
532 | ||
533 | =head3 Parameters and variables influencing execution of DB::eval() | |
534 | ||
535 | C<DB::eval> isn't parameterized in the standard way; this is to keep the | |
536 | debugger's calls to C<DB::eval()> from mucking with C<@_>, among other things. | |
537 | The variables listed below influence C<DB::eval()>'s execution directly. | |
538 | ||
539 | =over 4 | |
540 | ||
541 | =item C<$evalarg> - the thing to actually be eval'ed | |
542 | ||
543 | =item C<$trace> - Current state of execution tracing (see X<$trace>) | |
544 | ||
545 | =item C<$single> - Current state of single-stepping (see X<$single>) | |
546 | ||
547 | =item C<$onetimeDump> - what is to be displayed after the evaluation | |
548 | ||
549 | =item C<$onetimeDumpDepth> - how deep C<dumpit()> should go when dumping results | |
550 | ||
551 | =back | |
552 | ||
553 | The following variables are altered by C<DB::eval()> during its execution. They | |
554 | are "stacked" via C<local()>, enabling recursive calls to C<DB::eval()>. | |
555 | ||
556 | =over 4 | |
557 | ||
558 | =item C<@res> - used to capture output from actual C<eval>. | |
559 | ||
560 | =item C<$otrace> - saved value of C<$trace>. | |
561 | ||
562 | =item C<$osingle> - saved value of C<$single>. | |
563 | ||
564 | =item C<$od> - saved value of C<$^D>. | |
565 | ||
566 | =item C<$saved[0]> - saved value of C<$@>. | |
567 | ||
568 | =item $\ - for output of C<$@> if there is an evaluation error. | |
569 | ||
570 | =back | |
571 | ||
572 | =head3 The problem of lexicals | |
573 | ||
574 | The context of C<DB::eval()> presents us with some problems. Obviously, | |
575 | we want to be 'sandboxed' away from the debugger's internals when we do | |
576 | the eval, but we need some way to control how punctuation variables and | |
577 | debugger globals are used. | |
578 | ||
579 | We can't use local, because the code inside C<DB::eval> can see localized | |
580 | variables; and we can't use C<my> either for the same reason. The code | |
581 | in this routine compromises and uses C<my>. | |
582 | ||
583 | After this routine is over, we don't have user code executing in the debugger's | |
584 | context, so we can use C<my> freely. | |
585 | ||
586 | =cut | |
587 | ||
588 | ############################################## Begin lexical danger zone | |
589 | ||
590 | # 'my' variables used here could leak into (that is, be visible in) | |
591 | # the context that the code being evaluated is executing in. This means that | |
592 | # the code could modify the debugger's variables. | |
593 | # | |
594 | # Fiddling with the debugger's context could be Bad. We insulate things as | |
595 | # much as we can. | |
596 | ||
c1051fcf | 597 | sub eval { |
69893cff | 598 | |
c1051fcf | 599 | # 'my' would make it visible from user code |
e22ea7cc | 600 | # but so does local! --tchrist |
69893cff | 601 | # Remember: this localizes @DB::res, not @main::res. |
c1051fcf IZ |
602 | local @res; |
603 | { | |
e22ea7cc RF |
604 | |
605 | # Try to keep the user code from messing with us. Save these so that | |
606 | # even if the eval'ed code changes them, we can put them back again. | |
607 | # Needed because the user could refer directly to the debugger's | |
69893cff RGS |
608 | # package globals (and any 'my' variables in this containing scope) |
609 | # inside the eval(), and we want to try to stay safe. | |
e22ea7cc | 610 | local $otrace = $trace; |
69893cff RGS |
611 | local $osingle = $single; |
612 | local $od = $^D; | |
613 | ||
614 | # Untaint the incoming eval() argument. | |
615 | { ($evalarg) = $evalarg =~ /(.*)/s; } | |
616 | ||
e22ea7cc | 617 | # $usercontext built in DB::DB near the comment |
69893cff RGS |
618 | # "set up the context for DB::eval ..." |
619 | # Evaluate and save any results. | |
e22ea7cc | 620 | @res = eval "$usercontext $evalarg;\n"; # '\n' for nice recursive debug |
69893cff RGS |
621 | |
622 | # Restore those old values. | |
623 | $trace = $otrace; | |
624 | $single = $osingle; | |
625 | $^D = $od; | |
c1051fcf | 626 | } |
69893cff RGS |
627 | |
628 | # Save the current value of $@, and preserve it in the debugger's copy | |
629 | # of the saved precious globals. | |
c1051fcf | 630 | my $at = $@; |
69893cff RGS |
631 | |
632 | # Since we're only saving $@, we only have to localize the array element | |
633 | # that it will be stored in. | |
e22ea7cc | 634 | local $saved[0]; # Preserve the old value of $@ |
c1051fcf | 635 | eval { &DB::save }; |
69893cff RGS |
636 | |
637 | # Now see whether we need to report an error back to the user. | |
c1051fcf | 638 | if ($at) { |
69893cff RGS |
639 | local $\ = ''; |
640 | print $OUT $at; | |
641 | } | |
642 | ||
643 | # Display as required by the caller. $onetimeDump and $onetimedumpDepth | |
644 | # are package globals. | |
645 | elsif ($onetimeDump) { | |
e22ea7cc RF |
646 | if ( $onetimeDump eq 'dump' ) { |
647 | local $option{dumpDepth} = $onetimedumpDepth | |
648 | if defined $onetimedumpDepth; | |
649 | dumpit( $OUT, \@res ); | |
650 | } | |
651 | elsif ( $onetimeDump eq 'methods' ) { | |
652 | methods( $res[0] ); | |
653 | } | |
69893cff | 654 | } ## end elsif ($onetimeDump) |
c1051fcf | 655 | @res; |
69893cff RGS |
656 | } ## end sub eval |
657 | ||
658 | ############################################## End lexical danger zone | |
c1051fcf | 659 | |
e22ea7cc RF |
660 | # After this point it is safe to introduce lexicals. |
661 | # The code being debugged will be executing in its own context, and | |
69893cff | 662 | # can't see the inside of the debugger. |
d338d6fe | 663 | # |
e22ea7cc | 664 | # However, one should not overdo it: leave as much control from outside as |
69893cff RGS |
665 | # possible. If you make something a lexical, it's not going to be addressable |
666 | # from outside the debugger even if you know its name. | |
667 | ||
d338d6fe | 668 | # This file is automatically included if you do perl -d. |
669 | # It's probably not useful to include this yourself. | |
670 | # | |
e22ea7cc | 671 | # Before venturing further into these twisty passages, it is |
2f7e9187 MS |
672 | # wise to read the perldebguts man page or risk the ire of dragons. |
673 | # | |
69893cff RGS |
674 | # (It should be noted that perldebguts will tell you a lot about |
675 | # the underlying mechanics of how the debugger interfaces into the | |
676 | # Perl interpreter, but not a lot about the debugger itself. The new | |
677 | # comments in this code try to address this problem.) | |
678 | ||
d338d6fe | 679 | # Note that no subroutine call is possible until &DB::sub is defined |
36477c24 | 680 | # (for subroutines defined outside of the package DB). In fact the same is |
d338d6fe | 681 | # true if $deep is not defined. |
682 | # | |
69893cff | 683 | # $Log: perldb.pl,v $ |
055fd3a9 GS |
684 | |
685 | # Enhanced by ilya@math.ohio-state.edu (Ilya Zakharevich) | |
055fd3a9 GS |
686 | |
687 | # modified Perl debugger, to be run from Emacs in perldb-mode | |
688 | # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990 | |
689 | # Johan Vromans -- upgrade to 4.0 pl 10 | |
690 | # Ilya Zakharevich -- patches after 5.001 (and some before ;-) | |
691 | ||
69893cff RGS |
692 | # (We have made efforts to clarify the comments in the change log |
693 | # in other places; some of them may seem somewhat obscure as they | |
694 | # were originally written, and explaining them away from the code | |
695 | # in question seems conterproductive.. -JM) | |
696 | ||
697 | ######################################################################## | |
698 | # Changes: 0.94 | |
699 | # + A lot of things changed after 0.94. First of all, core now informs | |
700 | # debugger about entry into XSUBs, overloaded operators, tied operations, | |
701 | # BEGIN and END. Handy with `O f=2'. | |
702 | # + This can make debugger a little bit too verbose, please be patient | |
703 | # and report your problems promptly. | |
704 | # + Now the option frame has 3 values: 0,1,2. XXX Document! | |
705 | # + Note that if DESTROY returns a reference to the object (or object), | |
706 | # the deletion of data may be postponed until the next function call, | |
707 | # due to the need to examine the return value. | |
708 | # | |
709 | # Changes: 0.95 | |
710 | # + `v' command shows versions. | |
711 | # | |
e22ea7cc | 712 | # Changes: 0.96 |
69893cff RGS |
713 | # + `v' command shows version of readline. |
714 | # primitive completion works (dynamic variables, subs for `b' and `l', | |
715 | # options). Can `p %var' | |
716 | # + Better help (`h <' now works). New commands <<, >>, {, {{. | |
717 | # {dump|print}_trace() coded (to be able to do it from <<cmd). | |
718 | # + `c sub' documented. | |
719 | # + At last enough magic combined to stop after the end of debuggee. | |
720 | # + !! should work now (thanks to Emacs bracket matching an extra | |
721 | # `]' in a regexp is caught). | |
722 | # + `L', `D' and `A' span files now (as documented). | |
723 | # + Breakpoints in `require'd code are possible (used in `R'). | |
724 | # + Some additional words on internal work of debugger. | |
725 | # + `b load filename' implemented. | |
726 | # + `b postpone subr' implemented. | |
727 | # + now only `q' exits debugger (overwritable on $inhibit_exit). | |
728 | # + When restarting debugger breakpoints/actions persist. | |
e22ea7cc | 729 | # + Buglet: When restarting debugger only one breakpoint/action per |
69893cff RGS |
730 | # autoloaded function persists. |
731 | # | |
36477c24 | 732 | # Changes: 0.97: NonStop will not stop in at_exit(). |
69893cff RGS |
733 | # + Option AutoTrace implemented. |
734 | # + Trace printed differently if frames are printed too. | |
735 | # + new `inhibitExit' option. | |
736 | # + printing of a very long statement interruptible. | |
1d06cb2d | 737 | # Changes: 0.98: New command `m' for printing possible methods |
69893cff RGS |
738 | # + 'l -' is a synonym for `-'. |
739 | # + Cosmetic bugs in printing stack trace. | |
740 | # + `frame' & 8 to print "expanded args" in stack trace. | |
741 | # + Can list/break in imported subs. | |
742 | # + new `maxTraceLen' option. | |
743 | # + frame & 4 and frame & 8 granted. | |
744 | # + new command `m' | |
745 | # + nonstoppable lines do not have `:' near the line number. | |
746 | # + `b compile subname' implemented. | |
747 | # + Will not use $` any more. | |
748 | # + `-' behaves sane now. | |
477ea2b1 | 749 | # Changes: 0.99: Completion for `f', `m'. |
69893cff RGS |
750 | # + `m' will remove duplicate names instead of duplicate functions. |
751 | # + `b load' strips trailing whitespace. | |
752 | # completion ignores leading `|'; takes into account current package | |
753 | # when completing a subroutine name (same for `l'). | |
055fd3a9 GS |
754 | # Changes: 1.07: Many fixed by tchrist 13-March-2000 |
755 | # BUG FIXES: | |
04e43a21 | 756 | # + Added bare minimal security checks on perldb rc files, plus |
055fd3a9 GS |
757 | # comments on what else is needed. |
758 | # + Fixed the ornaments that made "|h" completely unusable. | |
759 | # They are not used in print_help if they will hurt. Strip pod | |
760 | # if we're paging to less. | |
761 | # + Fixed mis-formatting of help messages caused by ornaments | |
e22ea7cc RF |
762 | # to restore Larry's original formatting. |
763 | # + Fixed many other formatting errors. The code is still suboptimal, | |
04e43a21 | 764 | # and needs a lot of work at restructuring. It's also misindented |
055fd3a9 GS |
765 | # in many places. |
766 | # + Fixed bug where trying to look at an option like your pager | |
e22ea7cc | 767 | # shows "1". |
055fd3a9 GS |
768 | # + Fixed some $? processing. Note: if you use csh or tcsh, you will |
769 | # lose. You should consider shell escapes not using their shell, | |
770 | # or else not caring about detailed status. This should really be | |
771 | # unified into one place, too. | |
772 | # + Fixed bug where invisible trailing whitespace on commands hoses you, | |
04e43a21 | 773 | # tricking Perl into thinking you weren't calling a debugger command! |
055fd3a9 GS |
774 | # + Fixed bug where leading whitespace on commands hoses you. (One |
775 | # suggests a leading semicolon or any other irrelevant non-whitespace | |
776 | # to indicate literal Perl code.) | |
777 | # + Fixed bugs that ate warnings due to wrong selected handle. | |
778 | # + Fixed a precedence bug on signal stuff. | |
779 | # + Fixed some unseemly wording. | |
780 | # + Fixed bug in help command trying to call perl method code. | |
781 | # + Fixed to call dumpvar from exception handler. SIGPIPE killed us. | |
782 | # ENHANCEMENTS: | |
783 | # + Added some comments. This code is still nasty spaghetti. | |
784 | # + Added message if you clear your pre/post command stacks which was | |
785 | # very easy to do if you just typed a bare >, <, or {. (A command | |
786 | # without an argument should *never* be a destructive action; this | |
787 | # API is fundamentally screwed up; likewise option setting, which | |
788 | # is equally buggered.) | |
789 | # + Added command stack dump on argument of "?" for >, <, or {. | |
790 | # + Added a semi-built-in doc viewer command that calls man with the | |
791 | # proper %Config::Config path (and thus gets caching, man -k, etc), | |
792 | # or else perldoc on obstreperous platforms. | |
793 | # + Added to and rearranged the help information. | |
794 | # + Detected apparent misuse of { ... } to declare a block; this used | |
795 | # to work but now is a command, and mysteriously gave no complaint. | |
04e43a21 DL |
796 | # |
797 | # Changes: 1.08: Apr 25, 2001 Jon Eveland <jweveland@yahoo.com> | |
798 | # BUG FIX: | |
799 | # + This patch to perl5db.pl cleans up formatting issues on the help | |
800 | # summary (h h) screen in the debugger. Mostly columnar alignment | |
801 | # issues, plus converted the printed text to use all spaces, since | |
802 | # tabs don't seem to help much here. | |
803 | # | |
804 | # Changes: 1.09: May 19, 2001 Ilya Zakharevich <ilya@math.ohio-state.edu> | |
69893cff RGS |
805 | # Minor bugs corrected; |
806 | # + Support for auto-creation of new TTY window on startup, either | |
807 | # unconditionally, or if started as a kid of another debugger session; | |
808 | # + New `O'ption CreateTTY | |
809 | # I<CreateTTY> bits control attempts to create a new TTY on events: | |
e22ea7cc | 810 | # 1: on fork() |
69893cff RGS |
811 | # 2: debugger is started inside debugger |
812 | # 4: on startup | |
813 | # + Code to auto-create a new TTY window on OS/2 (currently one | |
814 | # extra window per session - need named pipes to have more...); | |
815 | # + Simplified interface for custom createTTY functions (with a backward | |
816 | # compatibility hack); now returns the TTY name to use; return of '' | |
817 | # means that the function reset the I/O handles itself; | |
818 | # + Better message on the semantic of custom createTTY function; | |
819 | # + Convert the existing code to create a TTY into a custom createTTY | |
820 | # function; | |
821 | # + Consistent support for TTY names of the form "TTYin,TTYout"; | |
822 | # + Switch line-tracing output too to the created TTY window; | |
823 | # + make `b fork' DWIM with CORE::GLOBAL::fork; | |
824 | # + High-level debugger API cmd_*(): | |
04e43a21 DL |
825 | # cmd_b_load($filenamepart) # b load filenamepart |
826 | # cmd_b_line($lineno [, $cond]) # b lineno [cond] | |
827 | # cmd_b_sub($sub [, $cond]) # b sub [cond] | |
828 | # cmd_stop() # Control-C | |
492652be | 829 | # cmd_d($lineno) # d lineno (B) |
04e43a21 DL |
830 | # The cmd_*() API returns FALSE on failure; in this case it outputs |
831 | # the error message to the debugging output. | |
69893cff | 832 | # + Low-level debugger API |
04e43a21 DL |
833 | # break_on_load($filename) # b load filename |
834 | # @files = report_break_on_load() # List files with load-breakpoints | |
835 | # breakable_line_in_filename($name, $from [, $to]) | |
836 | # # First breakable line in the | |
837 | # # range $from .. $to. $to defaults | |
e22ea7cc | 838 | # # to $from, and may be less than |
69893cff | 839 | # # $to |
04e43a21 DL |
840 | # breakable_line($from [, $to]) # Same for the current file |
841 | # break_on_filename_line($name, $lineno [, $cond]) | |
e22ea7cc | 842 | # # Set breakpoint,$cond defaults to |
69893cff | 843 | # # 1 |
04e43a21 DL |
844 | # break_on_filename_line_range($name, $from, $to [, $cond]) |
845 | # # As above, on the first | |
846 | # # breakable line in range | |
847 | # break_on_line($lineno [, $cond]) # As above, in the current file | |
848 | # break_subroutine($sub [, $cond]) # break on the first breakable line | |
849 | # ($name, $from, $to) = subroutine_filename_lines($sub) | |
850 | # # The range of lines of the text | |
851 | # The low-level API returns TRUE on success, and die()s on failure. | |
852 | # | |
853 | # Changes: 1.10: May 23, 2001 Daniel Lewart <d-lewart@uiuc.edu> | |
854 | # BUG FIXES: | |
855 | # + Fixed warnings generated by "perl -dWe 42" | |
856 | # + Corrected spelling errors | |
857 | # + Squeezed Help (h) output into 80 columns | |
600d99fa DL |
858 | # |
859 | # Changes: 1.11: May 24, 2001 David Dyck <dcd@tc.fluke.com> | |
860 | # + Made "x @INC" work like it used to | |
861 | # | |
862 | # Changes: 1.12: May 24, 2001 Daniel Lewart <d-lewart@uiuc.edu> | |
863 | # + Fixed warnings generated by "O" (Show debugger options) | |
864 | # + Fixed warnings generated by "p 42" (Print expression) | |
6f891d7d | 865 | # Changes: 1.13: Jun 19, 2001 Scott.L.Miller@compaq.com |
e22ea7cc | 866 | # + Added windowSize option |
2f7e9187 MS |
867 | # Changes: 1.14: Oct 9, 2001 multiple |
868 | # + Clean up after itself on VMS (Charles Lane in 12385) | |
869 | # + Adding "@ file" syntax (Peter Scott in 12014) | |
870 | # + Debug reloading selfloaded stuff (Ilya Zakharevich in 11457) | |
871 | # + $^S and other debugger fixes (Ilya Zakharevich in 11120) | |
872 | # + Forgot a my() declaration (Ilya Zakharevich in 11085) | |
873 | # Changes: 1.15: Nov 6, 2001 Michael G Schwern <schwern@pobox.com> | |
874 | # + Updated 1.14 change log | |
875 | # + Added *dbline explainatory comments | |
876 | # + Mentioning perldebguts man page | |
492652be | 877 | # Changes: 1.16: Feb 15, 2002 Mark-Jason Dominus <mjd@plover.com> |
69893cff | 878 | # + $onetimeDump improvements |
492652be RF |
879 | # Changes: 1.17: Feb 20, 2002 Richard Foley <richard.foley@rfi.net> |
880 | # Moved some code to cmd_[.]()'s for clarity and ease of handling, | |
e22ea7cc RF |
881 | # rationalised the following commands and added cmd_wrapper() to |
882 | # enable switching between old and frighteningly consistent new | |
492652be RF |
883 | # behaviours for diehards: 'o CommandSet=pre580' (sigh...) |
884 | # a(add), A(del) # action expr (added del by line) | |
885 | # + b(add), B(del) # break [line] (was b,D) | |
e22ea7cc | 886 | # + w(add), W(del) # watch expr (was W,W) |
69893cff | 887 | # # added del by expr |
492652be RF |
888 | # + h(summary), h h(long) # help (hh) (was h h,h) |
889 | # + m(methods), M(modules) # ... (was m,v) | |
890 | # + o(option) # lc (was O) | |
891 | # + v(view code), V(view Variables) # ... (was w,V) | |
aef14ef9 RF |
892 | # Changes: 1.18: Mar 17, 2002 Richard Foley <richard.foley@rfi.net> |
893 | # + fixed missing cmd_O bug | |
471505cc SB |
894 | # Changes: 1.19: Mar 29, 2002 Spider Boardman |
895 | # + Added missing local()s -- DB::DB is called recursively. | |
35408c4e RF |
896 | # Changes: 1.20: Feb 17, 2003 Richard Foley <richard.foley@rfi.net> |
897 | # + pre'n'post commands no longer trashed with no args | |
898 | # + watch val joined out of eval() | |
69893cff RGS |
899 | # Changes: 1.21: Jun 04, 2003 Joe McMahon <mcmahon@ibiblio.org> |
900 | # + Added comments and reformatted source. No bug fixes/enhancements. | |
901 | # + Includes cleanup by Robin Barker and Jarkko Hietaniemi. | |
902 | # Changes: 1.22 Jun 09, 2003 Alex Vandiver <alexmv@MIT.EDU> | |
903 | # + Flush stdout/stderr before the debugger prompt is printed. | |
904 | # Changes: 1.23: Dec 21, 2003 Dominique Quatravaux | |
de5e1a3d | 905 | # + Fix a side-effect of bug #24674 in the perl debugger ("odd taint bug") |
e219e2fb RF |
906 | # Changes: 1.24: Mar 03, 2004 Richard Foley <richard.foley@rfi.net> |
907 | # + Added command to save all debugger commands for sourcing later. | |
908 | # + Added command to display parent inheritence tree of given class. | |
909 | # + Fixed minor newline in history bug. | |
e22ea7cc RF |
910 | # Changes: 1.25: Apr 17, 2004 Richard Foley <richard.foley@rfi.net> |
911 | # + Fixed option bug (setting invalid options + not recognising valid short forms) | |
912 | # Changes: 1.26: Apr 22, 2004 Richard Foley <richard.foley@rfi.net> | |
913 | # + unfork the 5.8.x and 5.9.x debuggers. | |
914 | # + whitespace and assertions call cleanup across versions | |
915 | # + H * deletes (resets) history | |
916 | # + i now handles Class + blessed objects | |
7fddc82f RF |
917 | # Changes: 1.27: May 09, 2004 Richard Foley <richard.foley@rfi.net> |
918 | # + updated pod page references - clunky. | |
919 | # + removed windowid restriction for forking into an xterm. | |
920 | # + more whitespace again. | |
921 | # + wrapped restart and enabled rerun [-n] (go back n steps) command. | |
ee971a18 | 922 | #################################################################### |
d338d6fe | 923 | |
69893cff RGS |
924 | =head1 DEBUGGER INITIALIZATION |
925 | ||
926 | The debugger starts up in phases. | |
927 | ||
928 | =head2 BASIC SETUP | |
929 | ||
930 | First, it initializes the environment it wants to run in: turning off | |
931 | warnings during its own compilation, defining variables which it will need | |
932 | to avoid warnings later, setting itself up to not exit when the program | |
933 | terminates, and defaulting to printing return values for the C<r> command. | |
934 | ||
935 | =cut | |
936 | ||
eda6e075 | 937 | # Needed for the statement after exec(): |
69893cff RGS |
938 | # |
939 | # This BEGIN block is simply used to switch off warnings during debugger | |
940 | # compiliation. Probably it would be better practice to fix the warnings, | |
941 | # but this is how it's done at the moment. | |
eda6e075 | 942 | |
e22ea7cc RF |
943 | BEGIN { |
944 | $ini_warn = $^W; | |
945 | $^W = 0; | |
946 | } # Switch compilation warnings off until another BEGIN. | |
d12a4851 JH |
947 | |
948 | # test if assertions are supported and actived: | |
eda6e075 | 949 | BEGIN { |
e22ea7cc RF |
950 | $ini_assertion = eval "sub asserting_test : assertion {1}; 1"; |
951 | ||
d12a4851 | 952 | # $ini_assertion = undef => assertions unsupported, |
e22ea7cc | 953 | # " = 1 => assertions supported |
d12a4851 JH |
954 | # print "\$ini_assertion=$ini_assertion\n"; |
955 | } | |
956 | ||
69893cff RGS |
957 | local ($^W) = 0; # Switch run-time warnings off during init. |
958 | ||
959 | # This would probably be better done with "use vars", but that wasn't around | |
960 | # when this code was originally written. (Neither was "use strict".) And on | |
961 | # the principle of not fiddling with something that was working, this was | |
962 | # left alone. | |
963 | warn( # Do not ;-) | |
e22ea7cc | 964 | # These variables control the execution of 'dumpvar.pl'. |
69893cff RGS |
965 | $dumpvar::hashDepth, |
966 | $dumpvar::arrayDepth, | |
967 | $dumpvar::dumpDBFiles, | |
968 | $dumpvar::dumpPackages, | |
969 | $dumpvar::quoteHighBit, | |
970 | $dumpvar::printUndef, | |
971 | $dumpvar::globPrint, | |
972 | $dumpvar::usageOnly, | |
973 | ||
974 | # used to save @ARGV and extract any debugger-related flags. | |
975 | @ARGS, | |
976 | ||
977 | # used to control die() reporting in diesignal() | |
978 | $Carp::CarpLevel, | |
979 | ||
980 | # used to prevent multiple entries to diesignal() | |
981 | # (if for instance diesignal() itself dies) | |
982 | $panic, | |
983 | ||
984 | # used to prevent the debugger from running nonstop | |
985 | # after a restart | |
986 | $second_time, | |
987 | ) | |
988 | if 0; | |
d338d6fe | 989 | |
54d04a52 | 990 | # Command-line + PERLLIB: |
69893cff | 991 | # Save the contents of @INC before they are modified elsewhere. |
54d04a52 IZ |
992 | @ini_INC = @INC; |
993 | ||
69893cff RGS |
994 | # This was an attempt to clear out the previous values of various |
995 | # trapped errors. Apparently it didn't help. XXX More info needed! | |
d338d6fe | 996 | # $prevwarn = $prevdie = $prevbus = $prevsegv = ''; # Does not help?! |
997 | ||
69893cff RGS |
998 | # We set these variables to safe values. We don't want to blindly turn |
999 | # off warnings, because other packages may still want them. | |
e22ea7cc RF |
1000 | $trace = $signal = $single = 0; # Uninitialized warning suppression |
1001 | # (local $^W cannot help - other packages!). | |
69893cff RGS |
1002 | |
1003 | # Default to not exiting when program finishes; print the return | |
1004 | # value when the 'r' command is used to return from a subroutine. | |
55497cff | 1005 | $inhibit_exit = $option{PrintRet} = 1; |
d338d6fe | 1006 | |
69893cff RGS |
1007 | =head1 OPTION PROCESSING |
1008 | ||
1009 | The debugger's options are actually spread out over the debugger itself and | |
1010 | C<dumpvar.pl>; some of these are variables to be set, while others are | |
1011 | subs to be called with a value. To try to make this a little easier to | |
1012 | manage, the debugger uses a few data structures to define what options | |
1013 | are legal and how they are to be processed. | |
1014 | ||
1015 | First, the C<@options> array defines the I<names> of all the options that | |
1016 | are to be accepted. | |
1017 | ||
1018 | =cut | |
1019 | ||
1020 | @options = qw( | |
e22ea7cc RF |
1021 | CommandSet |
1022 | hashDepth arrayDepth dumpDepth | |
1023 | DumpDBFiles DumpPackages DumpReused | |
1024 | compactDump veryCompact quote | |
1025 | HighBit undefPrint globPrint | |
1026 | PrintRet UsageOnly frame | |
1027 | AutoTrace TTY noTTY | |
1028 | ReadLine NonStop LineInfo | |
1029 | maxTraceLen recallCommand ShellBang | |
1030 | pager tkRunning ornaments | |
1031 | signalLevel warnLevel dieLevel | |
1032 | inhibit_exit ImmediateStop bareStringify | |
1033 | CreateTTY RemotePort windowSize | |
1034 | DollarCaretP OnlyAssertions WarnAssertions | |
1035 | ); | |
d12a4851 JH |
1036 | |
1037 | @RememberOnROptions = qw(DollarCaretP OnlyAssertions); | |
1038 | ||
69893cff RGS |
1039 | =pod |
1040 | ||
1041 | Second, C<optionVars> lists the variables that each option uses to save its | |
1042 | state. | |
1043 | ||
1044 | =cut | |
1045 | ||
1046 | %optionVars = ( | |
e22ea7cc RF |
1047 | hashDepth => \$dumpvar::hashDepth, |
1048 | arrayDepth => \$dumpvar::arrayDepth, | |
1049 | CommandSet => \$CommandSet, | |
1050 | DumpDBFiles => \$dumpvar::dumpDBFiles, | |
1051 | DumpPackages => \$dumpvar::dumpPackages, | |
1052 | DumpReused => \$dumpvar::dumpReused, | |
1053 | HighBit => \$dumpvar::quoteHighBit, | |
1054 | undefPrint => \$dumpvar::printUndef, | |
1055 | globPrint => \$dumpvar::globPrint, | |
1056 | UsageOnly => \$dumpvar::usageOnly, | |
1057 | CreateTTY => \$CreateTTY, | |
1058 | bareStringify => \$dumpvar::bareStringify, | |
1059 | frame => \$frame, | |
1060 | AutoTrace => \$trace, | |
1061 | inhibit_exit => \$inhibit_exit, | |
1062 | maxTraceLen => \$maxtrace, | |
1063 | ImmediateStop => \$ImmediateStop, | |
1064 | RemotePort => \$remoteport, | |
1065 | windowSize => \$window, | |
1066 | WarnAssertions => \$warnassertions, | |
69893cff RGS |
1067 | ); |
1068 | ||
1069 | =pod | |
1070 | ||
1071 | Third, C<%optionAction> defines the subroutine to be called to process each | |
1072 | option. | |
1073 | ||
1074 | =cut | |
1075 | ||
1076 | %optionAction = ( | |
1077 | compactDump => \&dumpvar::compactDump, | |
1078 | veryCompact => \&dumpvar::veryCompact, | |
1079 | quote => \&dumpvar::quote, | |
1080 | TTY => \&TTY, | |
1081 | noTTY => \&noTTY, | |
1082 | ReadLine => \&ReadLine, | |
1083 | NonStop => \&NonStop, | |
1084 | LineInfo => \&LineInfo, | |
1085 | recallCommand => \&recallCommand, | |
1086 | ShellBang => \&shellBang, | |
1087 | pager => \&pager, | |
1088 | signalLevel => \&signalLevel, | |
1089 | warnLevel => \&warnLevel, | |
1090 | dieLevel => \&dieLevel, | |
1091 | tkRunning => \&tkRunning, | |
1092 | ornaments => \&ornaments, | |
1093 | RemotePort => \&RemotePort, | |
1094 | DollarCaretP => \&DollarCaretP, | |
1095 | OnlyAssertions=> \&OnlyAssertions, | |
d12a4851 JH |
1096 | ); |
1097 | ||
69893cff RGS |
1098 | =pod |
1099 | ||
1100 | Last, the C<%optionRequire> notes modules that must be C<require>d if an | |
1101 | option is used. | |
1102 | ||
1103 | =cut | |
d338d6fe | 1104 | |
69893cff RGS |
1105 | # Note that this list is not complete: several options not listed here |
1106 | # actually require that dumpvar.pl be loaded for them to work, but are | |
1107 | # not in the table. A subsequent patch will correct this problem; for | |
1108 | # the moment, we're just recommenting, and we are NOT going to change | |
1109 | # function. | |
eda6e075 | 1110 | %optionRequire = ( |
69893cff RGS |
1111 | compactDump => 'dumpvar.pl', |
1112 | veryCompact => 'dumpvar.pl', | |
1113 | quote => 'dumpvar.pl', | |
e22ea7cc | 1114 | ); |
69893cff RGS |
1115 | |
1116 | =pod | |
1117 | ||
1118 | There are a number of initialization-related variables which can be set | |
1119 | by putting code to set them in a BEGIN block in the C<PERL5DB> environment | |
1120 | variable. These are: | |
1121 | ||
1122 | =over 4 | |
1123 | ||
1124 | =item C<$rl> - readline control XXX needs more explanation | |
1125 | ||
1126 | =item C<$warnLevel> - whether or not debugger takes over warning handling | |
1127 | ||
1128 | =item C<$dieLevel> - whether or not debugger takes over die handling | |
1129 | ||
1130 | =item C<$signalLevel> - whether or not debugger takes over signal handling | |
1131 | ||
1132 | =item C<$pre> - preprompt actions (array reference) | |
1133 | ||
1134 | =item C<$post> - postprompt actions (array reference) | |
1135 | ||
1136 | =item C<$pretype> | |
1137 | ||
1138 | =item C<$CreateTTY> - whether or not to create a new TTY for this debugger | |
1139 | ||
1140 | =item C<$CommandSet> - which command set to use (defaults to new, documented set) | |
1141 | ||
1142 | =back | |
1143 | ||
1144 | =cut | |
d338d6fe | 1145 | |
1146 | # These guys may be defined in $ENV{PERL5DB} : | |
69893cff RGS |
1147 | $rl = 1 unless defined $rl; |
1148 | $warnLevel = 1 unless defined $warnLevel; | |
1149 | $dieLevel = 1 unless defined $dieLevel; | |
1150 | $signalLevel = 1 unless defined $signalLevel; | |
1151 | $pre = [] unless defined $pre; | |
1152 | $post = [] unless defined $post; | |
1153 | $pretype = [] unless defined $pretype; | |
1154 | $CreateTTY = 3 unless defined $CreateTTY; | |
1155 | $CommandSet = '580' unless defined $CommandSet; | |
1156 | ||
1157 | =pod | |
1158 | ||
1159 | The default C<die>, C<warn>, and C<signal> handlers are set up. | |
1160 | ||
1161 | =cut | |
055fd3a9 | 1162 | |
d338d6fe | 1163 | warnLevel($warnLevel); |
1164 | dieLevel($dieLevel); | |
1165 | signalLevel($signalLevel); | |
055fd3a9 | 1166 | |
69893cff RGS |
1167 | =pod |
1168 | ||
1169 | The pager to be used is needed next. We try to get it from the | |
1170 | environment first. if it's not defined there, we try to find it in | |
1171 | the Perl C<Config.pm>. If it's not there, we default to C<more>. We | |
1172 | then call the C<pager()> function to save the pager name. | |
1173 | ||
1174 | =cut | |
1175 | ||
1176 | # This routine makes sure $pager is set up so that '|' can use it. | |
4865a36d | 1177 | pager( |
e22ea7cc | 1178 | |
69893cff | 1179 | # If PAGER is defined in the environment, use it. |
e22ea7cc RF |
1180 | defined $ENV{PAGER} |
1181 | ? $ENV{PAGER} | |
69893cff RGS |
1182 | |
1183 | # If not, see if Config.pm defines it. | |
e22ea7cc RF |
1184 | : eval { require Config } |
1185 | && defined $Config::Config{pager} | |
1186 | ? $Config::Config{pager} | |
69893cff RGS |
1187 | |
1188 | # If not, fall back to 'more'. | |
e22ea7cc RF |
1189 | : 'more' |
1190 | ) | |
1191 | unless defined $pager; | |
69893cff RGS |
1192 | |
1193 | =pod | |
1194 | ||
1195 | We set up the command to be used to access the man pages, the command | |
1196 | recall character ("!" unless otherwise defined) and the shell escape | |
1197 | character ("!" unless otherwise defined). Yes, these do conflict, and | |
1198 | neither works in the debugger at the moment. | |
1199 | ||
1200 | =cut | |
1201 | ||
055fd3a9 | 1202 | setman(); |
69893cff RGS |
1203 | |
1204 | # Set up defaults for command recall and shell escape (note: | |
1205 | # these currently don't work in linemode debugging). | |
d338d6fe | 1206 | &recallCommand("!") unless defined $prc; |
69893cff RGS |
1207 | &shellBang("!") unless defined $psh; |
1208 | ||
1209 | =pod | |
1210 | ||
1211 | We then set up the gigantic string containing the debugger help. | |
1212 | We also set the limit on the number of arguments we'll display during a | |
1213 | trace. | |
1214 | ||
1215 | =cut | |
1216 | ||
04e43a21 | 1217 | sethelp(); |
69893cff RGS |
1218 | |
1219 | # If we didn't get a default for the length of eval/stack trace args, | |
1220 | # set it here. | |
1d06cb2d | 1221 | $maxtrace = 400 unless defined $maxtrace; |
69893cff RGS |
1222 | |
1223 | =head2 SETTING UP THE DEBUGGER GREETING | |
1224 | ||
1225 | The debugger 'greeting' helps to inform the user how many debuggers are | |
1226 | running, and whether the current debugger is the primary or a child. | |
1227 | ||
1228 | If we are the primary, we just hang onto our pid so we'll have it when | |
1229 | or if we start a child debugger. If we are a child, we'll set things up | |
1230 | so we'll have a unique greeting and so the parent will give us our own | |
1231 | TTY later. | |
1232 | ||
1233 | We save the current contents of the C<PERLDB_PIDS> environment variable | |
1234 | because we mess around with it. We'll also need to hang onto it because | |
1235 | we'll need it if we restart. | |
1236 | ||
1237 | Child debuggers make a label out of the current PID structure recorded in | |
1238 | PERLDB_PIDS plus the new PID. They also mark themselves as not having a TTY | |
1239 | yet so the parent will give them one later via C<resetterm()>. | |
1240 | ||
1241 | =cut | |
1242 | ||
e22ea7cc | 1243 | # Save the current contents of the environment; we're about to |
69893cff | 1244 | # much with it. We'll need this if we have to restart. |
f1583d8f | 1245 | $ini_pids = $ENV{PERLDB_PIDS}; |
69893cff | 1246 | |
e22ea7cc RF |
1247 | if ( defined $ENV{PERLDB_PIDS} ) { |
1248 | ||
69893cff | 1249 | # We're a child. Make us a label out of the current PID structure |
e22ea7cc | 1250 | # recorded in PERLDB_PIDS plus our (new) PID. Mark us as not having |
69893cff | 1251 | # a term yet so the parent will give us one later via resetterm(). |
e22ea7cc RF |
1252 | $pids = "[$ENV{PERLDB_PIDS}]"; |
1253 | $ENV{PERLDB_PIDS} .= "->$$"; | |
1254 | $term_pid = -1; | |
69893cff RGS |
1255 | } ## end if (defined $ENV{PERLDB_PIDS... |
1256 | else { | |
e22ea7cc RF |
1257 | |
1258 | # We're the parent PID. Initialize PERLDB_PID in case we end up with a | |
69893cff RGS |
1259 | # child debugger, and mark us as the parent, so we'll know to set up |
1260 | # more TTY's is we have to. | |
1261 | $ENV{PERLDB_PIDS} = "$$"; | |
e22ea7cc RF |
1262 | $pids = "{pid=$$}"; |
1263 | $term_pid = $$; | |
f1583d8f | 1264 | } |
69893cff | 1265 | |
f1583d8f | 1266 | $pidprompt = ''; |
69893cff RGS |
1267 | |
1268 | # Sets up $emacs as a synonym for $slave_editor. | |
1269 | *emacs = $slave_editor if $slave_editor; # May be used in afterinit()... | |
1270 | ||
1271 | =head2 READING THE RC FILE | |
1272 | ||
1273 | The debugger will read a file of initialization options if supplied. If | |
1274 | running interactively, this is C<.perldb>; if not, it's C<perldb.ini>. | |
1275 | ||
1276 | =cut | |
1277 | ||
1278 | # As noted, this test really doesn't check accurately that the debugger | |
1279 | # is running at a terminal or not. | |
d338d6fe | 1280 | |
e22ea7cc RF |
1281 | if ( -e "/dev/tty" ) { # this is the wrong metric! |
1282 | $rcfile = ".perldb"; | |
1283 | } | |
69893cff RGS |
1284 | else { |
1285 | $rcfile = "perldb.ini"; | |
d338d6fe | 1286 | } |
1287 | ||
69893cff RGS |
1288 | =pod |
1289 | ||
1290 | The debugger does a safety test of the file to be read. It must be owned | |
1291 | either by the current user or root, and must only be writable by the owner. | |
1292 | ||
1293 | =cut | |
1294 | ||
1295 | # This wraps a safety test around "do" to read and evaluate the init file. | |
1296 | # | |
055fd3a9 GS |
1297 | # This isn't really safe, because there's a race |
1298 | # between checking and opening. The solution is to | |
1299 | # open and fstat the handle, but then you have to read and | |
1300 | # eval the contents. But then the silly thing gets | |
69893cff RGS |
1301 | # your lexical scope, which is unfortunate at best. |
1302 | sub safe_do { | |
055fd3a9 GS |
1303 | my $file = shift; |
1304 | ||
1305 | # Just exactly what part of the word "CORE::" don't you understand? | |
69893cff RGS |
1306 | local $SIG{__WARN__}; |
1307 | local $SIG{__DIE__}; | |
055fd3a9 | 1308 | |
e22ea7cc | 1309 | unless ( is_safe_file($file) ) { |
69893cff | 1310 | CORE::warn <<EO_GRIPE; |
055fd3a9 GS |
1311 | perldb: Must not source insecure rcfile $file. |
1312 | You or the superuser must be the owner, and it must not | |
69893cff | 1313 | be writable by anyone but its owner. |
055fd3a9 | 1314 | EO_GRIPE |
69893cff RGS |
1315 | return; |
1316 | } ## end unless (is_safe_file($file... | |
055fd3a9 GS |
1317 | |
1318 | do $file; | |
1319 | CORE::warn("perldb: couldn't parse $file: $@") if $@; | |
69893cff | 1320 | } ## end sub safe_do |
055fd3a9 | 1321 | |
69893cff RGS |
1322 | # This is the safety test itself. |
1323 | # | |
055fd3a9 GS |
1324 | # Verifies that owner is either real user or superuser and that no |
1325 | # one but owner may write to it. This function is of limited use | |
1326 | # when called on a path instead of upon a handle, because there are | |
1327 | # no guarantees that filename (by dirent) whose file (by ino) is | |
e22ea7cc | 1328 | # eventually accessed is the same as the one tested. |
055fd3a9 GS |
1329 | # Assumes that the file's existence is not in doubt. |
1330 | sub is_safe_file { | |
1331 | my $path = shift; | |
69893cff | 1332 | stat($path) || return; # mysteriously vaporized |
e22ea7cc | 1333 | my ( $dev, $ino, $mode, $nlink, $uid, $gid ) = stat(_); |
055fd3a9 GS |
1334 | |
1335 | return 0 if $uid != 0 && $uid != $<; | |
1336 | return 0 if $mode & 022; | |
1337 | return 1; | |
69893cff | 1338 | } ## end sub is_safe_file |
055fd3a9 | 1339 | |
69893cff | 1340 | # If the rcfile (whichever one we decided was the right one to read) |
e22ea7cc RF |
1341 | # exists, we safely do it. |
1342 | if ( -f $rcfile ) { | |
055fd3a9 | 1343 | safe_do("./$rcfile"); |
69893cff | 1344 | } |
e22ea7cc | 1345 | |
69893cff | 1346 | # If there isn't one here, try the user's home directory. |
e22ea7cc | 1347 | elsif ( defined $ENV{HOME} && -f "$ENV{HOME}/$rcfile" ) { |
055fd3a9 GS |
1348 | safe_do("$ENV{HOME}/$rcfile"); |
1349 | } | |
e22ea7cc | 1350 | |
69893cff | 1351 | # Else try the login directory. |
e22ea7cc | 1352 | elsif ( defined $ENV{LOGDIR} && -f "$ENV{LOGDIR}/$rcfile" ) { |
055fd3a9 | 1353 | safe_do("$ENV{LOGDIR}/$rcfile"); |
d338d6fe | 1354 | } |
1355 | ||
69893cff | 1356 | # If the PERLDB_OPTS variable has options in it, parse those out next. |
e22ea7cc RF |
1357 | if ( defined $ENV{PERLDB_OPTS} ) { |
1358 | parse_options( $ENV{PERLDB_OPTS} ); | |
d338d6fe | 1359 | } |
1360 | ||
69893cff RGS |
1361 | =pod |
1362 | ||
1363 | The last thing we do during initialization is determine which subroutine is | |
1364 | to be used to obtain a new terminal when a new debugger is started. Right now, | |
1365 | the debugger only handles X Windows and OS/2. | |
1366 | ||
1367 | =cut | |
1368 | ||
1369 | # Set up the get_fork_TTY subroutine to be aliased to the proper routine. | |
1370 | # Works if you're running an xterm or xterm-like window, or you're on | |
1371 | # OS/2. This may need some expansion: for instance, this doesn't handle | |
e22ea7cc RF |
1372 | # OS X Terminal windows. |
1373 | ||
1374 | if ( | |
1375 | not defined &get_fork_TTY # no routine exists, | |
1376 | and defined $ENV{TERM} # and we know what kind | |
1377 | # of terminal this is, | |
1378 | and $ENV{TERM} eq 'xterm' # and it's an xterm, | |
7fddc82f RF |
1379 | # and defined $ENV{WINDOWID} # and we know what window this is, <- wrong metric |
1380 | and defined $ENV{DISPLAY} # and what display it's on, | |
1381 | ) | |
69893cff | 1382 | { |
e22ea7cc | 1383 | *get_fork_TTY = \&xterm_get_fork_TTY; # use the xterm version |
69893cff | 1384 | } ## end if (not defined &get_fork_TTY... |
e22ea7cc RF |
1385 | elsif ( $^O eq 'os2' ) { # If this is OS/2, |
1386 | *get_fork_TTY = \&os2_get_fork_TTY; # use the OS/2 version | |
f1583d8f | 1387 | } |
e22ea7cc | 1388 | |
dbb46cec DQ |
1389 | # untaint $^O, which may have been tainted by the last statement. |
1390 | # see bug [perl #24674] | |
e22ea7cc RF |
1391 | $^O =~ m/^(.*)\z/; |
1392 | $^O = $1; | |
f1583d8f | 1393 | |
d12a4851 | 1394 | # Here begin the unreadable code. It needs fixing. |
055fd3a9 | 1395 | |
69893cff RGS |
1396 | =head2 RESTART PROCESSING |
1397 | ||
1398 | This section handles the restart command. When the C<R> command is invoked, it | |
1399 | tries to capture all of the state it can into environment variables, and | |
1400 | then sets C<PERLDB_RESTART>. When we start executing again, we check to see | |
1401 | if C<PERLDB_RESTART> is there; if so, we reload all the information that | |
1402 | the R command stuffed into the environment variables. | |
1403 | ||
1404 | PERLDB_RESTART - flag only, contains no restart data itself. | |
1405 | PERLDB_HIST - command history, if it's available | |
1406 | PERLDB_ON_LOAD - breakpoints set by the rc file | |
1407 | PERLDB_POSTPONE - subs that have been loaded/not executed, and have actions | |
1408 | PERLDB_VISITED - files that had breakpoints | |
1409 | PERLDB_FILE_... - breakpoints for a file | |
1410 | PERLDB_OPT - active options | |
1411 | PERLDB_INC - the original @INC | |
1412 | PERLDB_PRETYPE - preprompt debugger actions | |
1413 | PERLDB_PRE - preprompt Perl code | |
1414 | PERLDB_POST - post-prompt Perl code | |
1415 | PERLDB_TYPEAHEAD - typeahead captured by readline() | |
1416 | ||
1417 | We chug through all these variables and plug the values saved in them | |
1418 | back into the appropriate spots in the debugger. | |
1419 | ||
1420 | =cut | |
1421 | ||
e22ea7cc RF |
1422 | if ( exists $ENV{PERLDB_RESTART} ) { |
1423 | ||
69893cff | 1424 | # We're restarting, so we don't need the flag that says to restart anymore. |
e22ea7cc RF |
1425 | delete $ENV{PERLDB_RESTART}; |
1426 | ||
1427 | # $restart = 1; | |
1428 | @hist = get_list('PERLDB_HIST'); | |
1429 | %break_on_load = get_list("PERLDB_ON_LOAD"); | |
1430 | %postponed = get_list("PERLDB_POSTPONE"); | |
69893cff RGS |
1431 | |
1432 | # restore breakpoints/actions | |
e22ea7cc RF |
1433 | my @had_breakpoints = get_list("PERLDB_VISITED"); |
1434 | for ( 0 .. $#had_breakpoints ) { | |
1435 | my %pf = get_list("PERLDB_FILE_$_"); | |
1436 | $postponed_file{ $had_breakpoints[$_] } = \%pf if %pf; | |
1437 | } | |
69893cff RGS |
1438 | |
1439 | # restore options | |
e22ea7cc RF |
1440 | my %opt = get_list("PERLDB_OPT"); |
1441 | my ( $opt, $val ); | |
1442 | while ( ( $opt, $val ) = each %opt ) { | |
1443 | $val =~ s/[\\\']/\\$1/g; | |
1444 | parse_options("$opt'$val'"); | |
1445 | } | |
69893cff RGS |
1446 | |
1447 | # restore original @INC | |
e22ea7cc RF |
1448 | @INC = get_list("PERLDB_INC"); |
1449 | @ini_INC = @INC; | |
1450 | ||
1451 | # return pre/postprompt actions and typeahead buffer | |
1452 | $pretype = [ get_list("PERLDB_PRETYPE") ]; | |
1453 | $pre = [ get_list("PERLDB_PRE") ]; | |
1454 | $post = [ get_list("PERLDB_POST") ]; | |
1455 | @typeahead = get_list( "PERLDB_TYPEAHEAD", @typeahead ); | |
69893cff RGS |
1456 | } ## end if (exists $ENV{PERLDB_RESTART... |
1457 | ||
1458 | =head2 SETTING UP THE TERMINAL | |
1459 | ||
1460 | Now, we'll decide how the debugger is going to interact with the user. | |
1461 | If there's no TTY, we set the debugger to run non-stop; there's not going | |
1462 | to be anyone there to enter commands. | |
1463 | ||
1464 | =cut | |
54d04a52 | 1465 | |
d338d6fe | 1466 | if ($notty) { |
69893cff RGS |
1467 | $runnonstop = 1; |
1468 | } | |
d12a4851 | 1469 | |
69893cff RGS |
1470 | =pod |
1471 | ||
1472 | If there is a TTY, we have to determine who it belongs to before we can | |
1473 | proceed. If this is a slave editor or graphical debugger (denoted by | |
1474 | the first command-line switch being '-emacs'), we shift this off and | |
1475 | set C<$rl> to 0 (XXX ostensibly to do straight reads). | |
1476 | ||
1477 | =cut | |
1478 | ||
1479 | else { | |
e22ea7cc | 1480 | |
69893cff RGS |
1481 | # Is Perl being run from a slave editor or graphical debugger? |
1482 | # If so, don't use readline, and set $slave_editor = 1. | |
e22ea7cc RF |
1483 | $slave_editor = |
1484 | ( ( defined $main::ARGV[0] ) and ( $main::ARGV[0] eq '-emacs' ) ); | |
1485 | $rl = 0, shift(@main::ARGV) if $slave_editor; | |
1486 | ||
1487 | #require Term::ReadLine; | |
d12a4851 | 1488 | |
69893cff RGS |
1489 | =pod |
1490 | ||
1491 | We then determine what the console should be on various systems: | |
1492 | ||
1493 | =over 4 | |
1494 | ||
1495 | =item * Cygwin - We use C<stdin> instead of a separate device. | |
1496 | ||
1497 | =cut | |
1498 | ||
e22ea7cc RF |
1499 | if ( $^O eq 'cygwin' ) { |
1500 | ||
69893cff RGS |
1501 | # /dev/tty is binary. use stdin for textmode |
1502 | undef $console; | |
1503 | } | |
1504 | ||
1505 | =item * Unix - use C</dev/tty>. | |
1506 | ||
1507 | =cut | |
1508 | ||
e22ea7cc | 1509 | elsif ( -e "/dev/tty" ) { |
69893cff RGS |
1510 | $console = "/dev/tty"; |
1511 | } | |
1512 | ||
1513 | =item * Windows or MSDOS - use C<con>. | |
1514 | ||
1515 | =cut | |
1516 | ||
e22ea7cc | 1517 | elsif ( $^O eq 'dos' or -e "con" or $^O eq 'MSWin32' ) { |
69893cff RGS |
1518 | $console = "con"; |
1519 | } | |
1520 | ||
1521 | =item * MacOS - use C<Dev:Console:Perl Debug> if this is the MPW version; C<Dev: | |
1522 | Console> if not. (Note that Mac OS X returns 'darwin', not 'MacOS'. Also note that the debugger doesn't do anything special for 'darwin'. Maybe it should.) | |
1523 | ||
1524 | =cut | |
1525 | ||
e22ea7cc RF |
1526 | elsif ( $^O eq 'MacOS' ) { |
1527 | if ( $MacPerl::Version !~ /MPW/ ) { | |
1528 | $console = | |
1529 | "Dev:Console:Perl Debug"; # Separate window for application | |
69893cff RGS |
1530 | } |
1531 | else { | |
1532 | $console = "Dev:Console"; | |
1533 | } | |
1534 | } ## end elsif ($^O eq 'MacOS') | |
1535 | ||
1536 | =item * VMS - use C<sys$command>. | |
1537 | ||
1538 | =cut | |
1539 | ||
1540 | else { | |
e22ea7cc | 1541 | |
69893cff RGS |
1542 | # everything else is ... |
1543 | $console = "sys\$command"; | |
d12a4851 | 1544 | } |
69893cff RGS |
1545 | |
1546 | =pod | |
1547 | ||
1548 | =back | |
1549 | ||
1550 | Several other systems don't use a specific console. We C<undef $console> | |
1551 | for those (Windows using a slave editor/graphical debugger, NetWare, OS/2 | |
1552 | with a slave editor, Epoc). | |
1553 | ||
1554 | =cut | |
d12a4851 | 1555 | |
e22ea7cc RF |
1556 | if ( ( $^O eq 'MSWin32' ) and ( $slave_editor or defined $ENV{EMACS} ) ) { |
1557 | ||
69893cff | 1558 | # /dev/tty is binary. use stdin for textmode |
e22ea7cc RF |
1559 | $console = undef; |
1560 | } | |
1561 | ||
1562 | if ( $^O eq 'NetWare' ) { | |
d12a4851 | 1563 | |
69893cff RGS |
1564 | # /dev/tty is binary. use stdin for textmode |
1565 | $console = undef; | |
1566 | } | |
d12a4851 | 1567 | |
69893cff RGS |
1568 | # In OS/2, we need to use STDIN to get textmode too, even though |
1569 | # it pretty much looks like Unix otherwise. | |
e22ea7cc RF |
1570 | if ( defined $ENV{OS2_SHELL} and ( $slave_editor or $ENV{WINDOWID} ) ) |
1571 | { # In OS/2 | |
1572 | $console = undef; | |
1573 | } | |
1574 | ||
1575 | # EPOC also falls into the 'got to use STDIN' camp. | |
1576 | if ( $^O eq 'epoc' ) { | |
1577 | $console = undef; | |
1578 | } | |
d12a4851 | 1579 | |
69893cff RGS |
1580 | =pod |
1581 | ||
1582 | If there is a TTY hanging around from a parent, we use that as the console. | |
1583 | ||
1584 | =cut | |
1585 | ||
e22ea7cc | 1586 | $console = $tty if defined $tty; |
d12a4851 | 1587 | |
69893cff RGS |
1588 | =head2 SOCKET HANDLING |
1589 | ||
1590 | The debugger is capable of opening a socket and carrying out a debugging | |
1591 | session over the socket. | |
1592 | ||
1593 | If C<RemotePort> was defined in the options, the debugger assumes that it | |
1594 | should try to start a debugging session on that port. It builds the socket | |
1595 | and then tries to connect the input and output filehandles to it. | |
1596 | ||
1597 | =cut | |
1598 | ||
1599 | # Handle socket stuff. | |
e22ea7cc RF |
1600 | |
1601 | if ( defined $remoteport ) { | |
1602 | ||
69893cff RGS |
1603 | # If RemotePort was defined in the options, connect input and output |
1604 | # to the socket. | |
e22ea7cc RF |
1605 | require IO::Socket; |
1606 | $OUT = new IO::Socket::INET( | |
1607 | Timeout => '10', | |
1608 | PeerAddr => $remoteport, | |
1609 | Proto => 'tcp', | |
69893cff | 1610 | ); |
e22ea7cc RF |
1611 | if ( !$OUT ) { die "Unable to connect to remote host: $remoteport\n"; } |
1612 | $IN = $OUT; | |
69893cff RGS |
1613 | } ## end if (defined $remoteport) |
1614 | ||
1615 | =pod | |
1616 | ||
1617 | If no C<RemotePort> was defined, and we want to create a TTY on startup, | |
1618 | this is probably a situation where multiple debuggers are running (for example, | |
1619 | a backticked command that starts up another debugger). We create a new IN and | |
1620 | OUT filehandle, and do the necessary mojo to create a new TTY if we know how | |
1621 | and if we can. | |
1622 | ||
1623 | =cut | |
1624 | ||
1625 | # Non-socket. | |
1626 | else { | |
e22ea7cc | 1627 | |
69893cff RGS |
1628 | # Two debuggers running (probably a system or a backtick that invokes |
1629 | # the debugger itself under the running one). create a new IN and OUT | |
e22ea7cc | 1630 | # filehandle, and do the necessary mojo to create a new tty if we |
69893cff | 1631 | # know how, and we can. |
e22ea7cc RF |
1632 | create_IN_OUT(4) if $CreateTTY & 4; |
1633 | if ($console) { | |
1634 | ||
69893cff RGS |
1635 | # If we have a console, check to see if there are separate ins and |
1636 | # outs to open. (They are assumed identiical if not.) | |
1637 | ||
e22ea7cc RF |
1638 | my ( $i, $o ) = split /,/, $console; |
1639 | $o = $i unless defined $o; | |
69893cff | 1640 | |
69893cff | 1641 | # read/write on in, or just read, or read on STDIN. |
e22ea7cc RF |
1642 | open( IN, "+<$i" ) |
1643 | || open( IN, "<$i" ) | |
1644 | || open( IN, "<&STDIN" ); | |
1645 | ||
69893cff RGS |
1646 | # read/write/create/clobber out, or write/create/clobber out, |
1647 | # or merge with STDERR, or merge with STDOUT. | |
e22ea7cc RF |
1648 | open( OUT, "+>$o" ) |
1649 | || open( OUT, ">$o" ) | |
1650 | || open( OUT, ">&STDERR" ) | |
1651 | || open( OUT, ">&STDOUT" ); # so we don't dongle stdout | |
1652 | ||
1653 | } ## end if ($console) | |
1654 | elsif ( not defined $console ) { | |
1655 | ||
1656 | # No console. Open STDIN. | |
1657 | open( IN, "<&STDIN" ); | |
1658 | ||
1659 | # merge with STDERR, or with STDOUT. | |
1660 | open( OUT, ">&STDERR" ) | |
1661 | || open( OUT, ">&STDOUT" ); # so we don't dongle stdout | |
1662 | $console = 'STDIN/OUT'; | |
69893cff RGS |
1663 | } ## end elsif (not defined $console) |
1664 | ||
1665 | # Keep copies of the filehandles so that when the pager runs, it | |
1666 | # can close standard input without clobbering ours. | |
e22ea7cc RF |
1667 | $IN = \*IN, $OUT = \*OUT if $console or not defined $console; |
1668 | } ## end elsif (from if(defined $remoteport)) | |
1669 | ||
1670 | # Unbuffer DB::OUT. We need to see responses right away. | |
1671 | my $previous = select($OUT); | |
1672 | $| = 1; # for DB::OUT | |
1673 | select($previous); | |
1674 | ||
1675 | # Line info goes to debugger output unless pointed elsewhere. | |
1676 | # Pointing elsewhere makes it possible for slave editors to | |
1677 | # keep track of file and position. We have both a filehandle | |
1678 | # and a I/O description to keep track of. | |
1679 | $LINEINFO = $OUT unless defined $LINEINFO; | |
1680 | $lineinfo = $console unless defined $lineinfo; | |
1681 | ||
69893cff RGS |
1682 | =pod |
1683 | ||
1684 | To finish initialization, we show the debugger greeting, | |
1685 | and then call the C<afterinit()> subroutine if there is one. | |
1686 | ||
1687 | =cut | |
d12a4851 | 1688 | |
e22ea7cc RF |
1689 | # Show the debugger greeting. |
1690 | $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/; | |
1691 | unless ($runnonstop) { | |
1692 | local $\ = ''; | |
1693 | local $, = ''; | |
1694 | if ( $term_pid eq '-1' ) { | |
1695 | print $OUT "\nDaughter DB session started...\n"; | |
1696 | } | |
1697 | else { | |
1698 | print $OUT "\nLoading DB routines from $header\n"; | |
1699 | print $OUT ( | |
1700 | "Editor support ", | |
1701 | $slave_editor ? "enabled" : "available", ".\n" | |
1702 | ); | |
1703 | print $OUT | |
69893cff RGS |
1704 | "\nEnter h or `h h' for help, or `$doccmd perldebug' for more help.\n\n"; |
1705 | } ## end else [ if ($term_pid eq '-1') | |
1706 | } ## end unless ($runnonstop) | |
1707 | } ## end else [ if ($notty) | |
1708 | ||
1709 | # XXX This looks like a bug to me. | |
1710 | # Why copy to @ARGS and then futz with @args? | |
d338d6fe | 1711 | @ARGS = @ARGV; |
1712 | for (@args) { | |
69893cff RGS |
1713 | # Make sure backslashes before single quotes are stripped out, and |
1714 | # keep args unless they are numeric (XXX why?) | |
e22ea7cc RF |
1715 | # s/\'/\\\'/g; # removed while not justified understandably |
1716 | # s/(.*)/'$1'/ unless /^-?[\d.]+$/; # ditto | |
d338d6fe | 1717 | } |
1718 | ||
e22ea7cc | 1719 | # If there was an afterinit() sub defined, call it. It will get |
69893cff | 1720 | # executed in our scope, so it can fiddle with debugger globals. |
e22ea7cc | 1721 | if ( defined &afterinit ) { # May be defined in $rcfile |
69893cff | 1722 | &afterinit(); |
d338d6fe | 1723 | } |
e22ea7cc | 1724 | |
69893cff | 1725 | # Inform us about "Stack dump during die enabled ..." in dieLevel(). |
43aed9ee IZ |
1726 | $I_m_init = 1; |
1727 | ||
d338d6fe | 1728 | ############################################################ Subroutines |
1729 | ||
69893cff RGS |
1730 | =head1 SUBROUTINES |
1731 | ||
1732 | =head2 DB | |
1733 | ||
1734 | This gigantic subroutine is the heart of the debugger. Called before every | |
1735 | statement, its job is to determine if a breakpoint has been reached, and | |
1736 | stop if so; read commands from the user, parse them, and execute | |
1737 | them, and hen send execution off to the next statement. | |
1738 | ||
1739 | Note that the order in which the commands are processed is very important; | |
1740 | some commands earlier in the loop will actually alter the C<$cmd> variable | |
1741 | to create other commands to be executed later. This is all highly "optimized" | |
1742 | but can be confusing. Check the comments for each C<$cmd ... && do {}> to | |
1743 | see what's happening in any given command. | |
1744 | ||
1745 | =cut | |
1746 | ||
d338d6fe | 1747 | sub DB { |
69893cff RGS |
1748 | |
1749 | # Check for whether we should be running continuously or not. | |
36477c24 | 1750 | # _After_ the perl program is compiled, $single is set to 1: |
e22ea7cc RF |
1751 | if ( $single and not $second_time++ ) { |
1752 | ||
69893cff | 1753 | # Options say run non-stop. Run until we get an interrupt. |
e22ea7cc RF |
1754 | if ($runnonstop) { # Disable until signal |
1755 | # If there's any call stack in place, turn off single | |
1756 | # stepping into subs throughout the stack. | |
1757 | for ( $i = 0 ; $i <= $stack_depth ; ) { | |
1758 | $stack[ $i++ ] &= ~1; | |
1759 | } | |
1760 | ||
69893cff | 1761 | # And we are now no longer in single-step mode. |
e22ea7cc | 1762 | $single = 0; |
69893cff RGS |
1763 | |
1764 | # If we simply returned at this point, we wouldn't get | |
1765 | # the trace info. Fall on through. | |
e22ea7cc | 1766 | # return; |
69893cff RGS |
1767 | } ## end if ($runnonstop) |
1768 | ||
e22ea7cc RF |
1769 | elsif ($ImmediateStop) { |
1770 | ||
1771 | # We are supposed to stop here; XXX probably a break. | |
1772 | $ImmediateStop = 0; # We've processed it; turn it off | |
1773 | $signal = 1; # Simulate an interrupt to force | |
1774 | # us into the command loop | |
69893cff RGS |
1775 | } |
1776 | } ## end if ($single and not $second_time... | |
1777 | ||
1778 | # If we're in single-step mode, or an interrupt (real or fake) | |
1779 | # has occurred, turn off non-stop mode. | |
1780 | $runnonstop = 0 if $single or $signal; | |
1781 | ||
1782 | # Preserve current values of $@, $!, $^E, $,, $/, $\, $^W. | |
1783 | # The code being debugged may have altered them. | |
d338d6fe | 1784 | &save; |
69893cff RGS |
1785 | |
1786 | # Since DB::DB gets called after every line, we can use caller() to | |
1787 | # figure out where we last were executing. Sneaky, eh? This works because | |
e22ea7cc | 1788 | # caller is returning all the extra information when called from the |
69893cff | 1789 | # debugger. |
e22ea7cc | 1790 | local ( $package, $filename, $line ) = caller; |
471505cc | 1791 | local $filename_ini = $filename; |
69893cff RGS |
1792 | |
1793 | # set up the context for DB::eval, so it can properly execute | |
1794 | # code on behalf of the user. We add the package in so that the | |
1795 | # code is eval'ed in the proper package (not in the debugger!). | |
1796 | local $usercontext = | |
e22ea7cc | 1797 | '($@, $!, $^E, $,, $/, $\, $^W) = @saved;' . "package $package;"; |
69893cff RGS |
1798 | |
1799 | # Create an alias to the active file magical array to simplify | |
1800 | # the code here. | |
e22ea7cc | 1801 | local (*dbline) = $main::{ '_<' . $filename }; |
aa057b67 CN |
1802 | |
1803 | # we need to check for pseudofiles on Mac OS (these are files | |
1804 | # not attached to a filename, but instead stored in Dev:Pseudo) | |
e22ea7cc RF |
1805 | if ( $^O eq 'MacOS' && $#dbline < 0 ) { |
1806 | $filename_ini = $filename = 'Dev:Pseudo'; | |
1807 | *dbline = $main::{ '_<' . $filename }; | |
aa057b67 CN |
1808 | } |
1809 | ||
69893cff | 1810 | # Last line in the program. |
471505cc | 1811 | local $max = $#dbline; |
69893cff RGS |
1812 | |
1813 | # if we have something here, see if we should break. | |
e22ea7cc RF |
1814 | if ( $dbline{$line} |
1815 | && ( ( $stop, $action ) = split( /\0/, $dbline{$line} ) ) ) | |
1816 | { | |
1817 | ||
69893cff | 1818 | # Stop if the stop criterion says to just stop. |
e22ea7cc | 1819 | if ( $stop eq '1' ) { |
69893cff RGS |
1820 | $signal |= 1; |
1821 | } | |
e22ea7cc | 1822 | |
69893cff RGS |
1823 | # It's a conditional stop; eval it in the user's context and |
1824 | # see if we should stop. If so, remove the one-time sigil. | |
1825 | elsif ($stop) { | |
e22ea7cc | 1826 | $evalarg = "\$DB::signal |= 1 if do {$stop}"; |
69893cff RGS |
1827 | &eval; |
1828 | $dbline{$line} =~ s/;9($|\0)/$1/; | |
1829 | } | |
1830 | } ## end if ($dbline{$line} && ... | |
1831 | ||
1832 | # Preserve the current stop-or-not, and see if any of the W | |
1833 | # (watch expressions) has changed. | |
36477c24 | 1834 | my $was_signal = $signal; |
69893cff RGS |
1835 | |
1836 | # If we have any watch expressions ... | |
e22ea7cc RF |
1837 | if ( $trace & 2 ) { |
1838 | for ( my $n = 0 ; $n <= $#to_watch ; $n++ ) { | |
1839 | $evalarg = $to_watch[$n]; | |
1840 | local $onetimeDump; # Tell DB::eval() to not output results | |
69893cff RGS |
1841 | |
1842 | # Fix context DB::eval() wants to return an array, but | |
1843 | # we need a scalar here. | |
e22ea7cc RF |
1844 | my ($val) = join( "', '", &eval ); |
1845 | $val = ( ( defined $val ) ? "'$val'" : 'undef' ); | |
69893cff RGS |
1846 | |
1847 | # Did it change? | |
e22ea7cc RF |
1848 | if ( $val ne $old_watch[$n] ) { |
1849 | ||
69893cff | 1850 | # Yep! Show the difference, and fake an interrupt. |
e22ea7cc RF |
1851 | $signal = 1; |
1852 | print $OUT <<EOP; | |
405ff068 | 1853 | Watchpoint $n:\t$to_watch[$n] changed: |
69893cff RGS |
1854 | old value:\t$old_watch[$n] |
1855 | new value:\t$val | |
6027b9a3 | 1856 | EOP |
e22ea7cc | 1857 | $old_watch[$n] = $val; |
69893cff RGS |
1858 | } ## end if ($val ne $old_watch... |
1859 | } ## end for (my $n = 0 ; $n <= ... | |
1860 | } ## end if ($trace & 2) | |
1861 | ||
1862 | =head2 C<watchfunction()> | |
1863 | ||
1864 | C<watchfunction()> is a function that can be defined by the user; it is a | |
1865 | function which will be run on each entry to C<DB::DB>; it gets the | |
1866 | current package, filename, and line as its parameters. | |
1867 | ||
1868 | The watchfunction can do anything it likes; it is executing in the | |
1869 | debugger's context, so it has access to all of the debugger's internal | |
1870 | data structures and functions. | |
1871 | ||
1872 | C<watchfunction()> can control the debugger's actions. Any of the following | |
1873 | will cause the debugger to return control to the user's program after | |
1874 | C<watchfunction()> executes: | |
1875 | ||
1876 | =over 4 | |
1877 | ||
1878 | =item * Returning a false value from the C<watchfunction()> itself. | |
1879 | ||
1880 | =item * Altering C<$single> to a false value. | |
1881 | ||
1882 | =item * Altering C<$signal> to a false value. | |
1883 | ||
1884 | =item * Turning off the '4' bit in C<$trace> (this also disables the | |
1885 | check for C<watchfunction()>. This can be done with | |
1886 | ||
1887 | $trace &= ~4; | |
1888 | ||
1889 | =back | |
1890 | ||
1891 | =cut | |
1892 | ||
e22ea7cc | 1893 | # If there's a user-defined DB::watchfunction, call it with the |
69893cff RGS |
1894 | # current package, filename, and line. The function executes in |
1895 | # the DB:: package. | |
e22ea7cc RF |
1896 | if ( $trace & 4 ) { # User-installed watch |
1897 | return | |
1898 | if watchfunction( $package, $filename, $line ) | |
1899 | and not $single | |
1900 | and not $was_signal | |
1901 | and not( $trace & ~4 ); | |
69893cff RGS |
1902 | } ## end if ($trace & 4) |
1903 | ||
e22ea7cc | 1904 | # Pick up any alteration to $signal in the watchfunction, and |
69893cff | 1905 | # turn off the signal now. |
6027b9a3 | 1906 | $was_signal = $signal; |
69893cff RGS |
1907 | $signal = 0; |
1908 | ||
1909 | =head2 GETTING READY TO EXECUTE COMMANDS | |
1910 | ||
1911 | The debugger decides to take control if single-step mode is on, the | |
1912 | C<t> command was entered, or the user generated a signal. If the program | |
1913 | has fallen off the end, we set things up so that entering further commands | |
1914 | won't cause trouble, and we say that the program is over. | |
1915 | ||
1916 | =cut | |
1917 | ||
1918 | # Check to see if we should grab control ($single true, | |
1919 | # trace set appropriately, or we got a signal). | |
e22ea7cc RF |
1920 | if ( $single || ( $trace & 1 ) || $was_signal ) { |
1921 | ||
69893cff | 1922 | # Yes, grab control. |
e22ea7cc RF |
1923 | if ($slave_editor) { |
1924 | ||
69893cff | 1925 | # Tell the editor to update its position. |
e22ea7cc RF |
1926 | $position = "\032\032$filename:$line:0\n"; |
1927 | print_lineinfo($position); | |
1928 | } | |
69893cff RGS |
1929 | |
1930 | =pod | |
1931 | ||
1932 | Special check: if we're in package C<DB::fake>, we've gone through the | |
1933 | C<END> block at least once. We set up everything so that we can continue | |
1934 | to enter commands and have a valid context to be in. | |
1935 | ||
1936 | =cut | |
1937 | ||
e22ea7cc | 1938 | elsif ( $package eq 'DB::fake' ) { |
69893cff | 1939 | |
69893cff | 1940 | # Fallen off the end already. |
e22ea7cc RF |
1941 | $term || &setterm; |
1942 | print_help(<<EOP); | |
405ff068 IZ |
1943 | Debugged program terminated. Use B<q> to quit or B<R> to restart, |
1944 | use B<O> I<inhibit_exit> to avoid stopping after program termination, | |
1945 | B<h q>, B<h R> or B<h O> to get additional info. | |
1946 | EOP | |
e22ea7cc | 1947 | |
69893cff | 1948 | # Set the DB::eval context appropriately. |
e22ea7cc RF |
1949 | $package = 'main'; |
1950 | $usercontext = | |
1951 | '($@, $!, $^E, $,, $/, $\, $^W) = @saved;' | |
1952 | . "package $package;"; # this won't let them modify, alas | |
69893cff | 1953 | } ## end elsif ($package eq 'DB::fake') |
e219e2fb | 1954 | |
69893cff | 1955 | =pod |
e219e2fb | 1956 | |
69893cff RGS |
1957 | If the program hasn't finished executing, we scan forward to the |
1958 | next executable line, print that out, build the prompt from the file and line | |
1959 | number information, and print that. | |
e219e2fb | 1960 | |
69893cff RGS |
1961 | =cut |
1962 | ||
e22ea7cc RF |
1963 | else { |
1964 | ||
69893cff RGS |
1965 | # Still somewhere in the midst of execution. Set up the |
1966 | # debugger prompt. | |
1967 | $sub =~ s/\'/::/; # Swap Perl 4 package separators (') to | |
e22ea7cc | 1968 | # Perl 5 ones (sorry, we don't print Klingon |
69893cff RGS |
1969 | #module names) |
1970 | ||
e22ea7cc RF |
1971 | $prefix = $sub =~ /::/ ? "" : "${'package'}::"; |
1972 | $prefix .= "$sub($filename:"; | |
1973 | $after = ( $dbline[$line] =~ /\n$/ ? '' : "\n" ); | |
69893cff RGS |
1974 | |
1975 | # Break up the prompt if it's really long. | |
e22ea7cc RF |
1976 | if ( length($prefix) > 30 ) { |
1977 | $position = "$prefix$line):\n$line:\t$dbline[$line]$after"; | |
1978 | $prefix = ""; | |
1979 | $infix = ":\t"; | |
1980 | } | |
1981 | else { | |
1982 | $infix = "):\t"; | |
1983 | $position = "$prefix$line$infix$dbline[$line]$after"; | |
1984 | } | |
69893cff RGS |
1985 | |
1986 | # Print current line info, indenting if necessary. | |
e22ea7cc RF |
1987 | if ($frame) { |
1988 | print_lineinfo( ' ' x $stack_depth, | |
1989 | "$line:\t$dbline[$line]$after" ); | |
1990 | } | |
1991 | else { | |
1992 | print_lineinfo($position); | |
1993 | } | |
69893cff RGS |
1994 | |
1995 | # Scan forward, stopping at either the end or the next | |
1996 | # unbreakable line. | |
e22ea7cc RF |
1997 | for ( $i = $line + 1 ; $i <= $max && $dbline[$i] == 0 ; ++$i ) |
1998 | { #{ vi | |
69893cff RGS |
1999 | |
2000 | # Drop out on null statements, block closers, and comments. | |
2001 | last if $dbline[$i] =~ /^\s*[\;\}\#\n]/; | |
2002 | ||
2003 | # Drop out if the user interrupted us. | |
2004 | last if $signal; | |
2005 | ||
2006 | # Append a newline if the line doesn't have one. Can happen | |
2007 | # in eval'ed text, for instance. | |
e22ea7cc | 2008 | $after = ( $dbline[$i] =~ /\n$/ ? '' : "\n" ); |
69893cff RGS |
2009 | |
2010 | # Next executable line. | |
2011 | $incr_pos = "$prefix$i$infix$dbline[$i]$after"; | |
2012 | $position .= $incr_pos; | |
2013 | if ($frame) { | |
e22ea7cc | 2014 | |
69893cff | 2015 | # Print it indented if tracing is on. |
e22ea7cc RF |
2016 | print_lineinfo( ' ' x $stack_depth, |
2017 | "$i:\t$dbline[$i]$after" ); | |
69893cff RGS |
2018 | } |
2019 | else { | |
2020 | print_lineinfo($incr_pos); | |
2021 | } | |
2022 | } ## end for ($i = $line + 1 ; $i... | |
2023 | } ## end else [ if ($slave_editor) | |
2024 | } ## end if ($single || ($trace... | |
2025 | ||
2026 | =pod | |
2027 | ||
2028 | If there's an action to be executed for the line we stopped at, execute it. | |
2029 | If there are any preprompt actions, execute those as well. | |
e219e2fb RF |
2030 | |
2031 | =cut | |
2032 | ||
69893cff RGS |
2033 | # If there's an action, do it now. |
2034 | $evalarg = $action, &eval if $action; | |
e219e2fb | 2035 | |
69893cff RGS |
2036 | # Are we nested another level (e.g., did we evaluate a function |
2037 | # that had a breakpoint in it at the debugger prompt)? | |
e22ea7cc RF |
2038 | if ( $single || $was_signal ) { |
2039 | ||
69893cff | 2040 | # Yes, go down a level. |
e22ea7cc | 2041 | local $level = $level + 1; |
69893cff RGS |
2042 | |
2043 | # Do any pre-prompt actions. | |
e22ea7cc RF |
2044 | foreach $evalarg (@$pre) { |
2045 | &eval; | |
2046 | } | |
69893cff RGS |
2047 | |
2048 | # Complain about too much recursion if we passed the limit. | |
e22ea7cc | 2049 | print $OUT $stack_depth . " levels deep in subroutine calls!\n" |
69893cff RGS |
2050 | if $single & 4; |
2051 | ||
2052 | # The line we're currently on. Set $incr to -1 to stay here | |
2053 | # until we get a command that tells us to advance. | |
e22ea7cc RF |
2054 | $start = $line; |
2055 | $incr = -1; # for backward motion. | |
69893cff RGS |
2056 | |
2057 | # Tack preprompt debugger actions ahead of any actual input. | |
e22ea7cc | 2058 | @typeahead = ( @$pretype, @typeahead ); |
69893cff RGS |
2059 | |
2060 | =head2 WHERE ARE WE? | |
2061 | ||
2062 | XXX Relocate this section? | |
2063 | ||
2064 | The debugger normally shows the line corresponding to the current line of | |
2065 | execution. Sometimes, though, we want to see the next line, or to move elsewhere | |
2066 | in the file. This is done via the C<$incr>, C<$start>, and C<$max> variables. | |
2067 | ||
2068 | C<$incr> controls by how many lines the "current" line should move forward | |
2069 | after a command is executed. If set to -1, this indicates that the "current" | |
2070 | line shouldn't change. | |
2071 | ||
2072 | C<$start> is the "current" line. It is used for things like knowing where to | |
2073 | move forwards or backwards from when doing an C<L> or C<-> command. | |
2074 | ||
2075 | C<$max> tells the debugger where the last line of the current file is. It's | |
2076 | used to terminate loops most often. | |
2077 | ||
2078 | =head2 THE COMMAND LOOP | |
2079 | ||
2080 | Most of C<DB::DB> is actually a command parsing and dispatch loop. It comes | |
2081 | in two parts: | |
2082 | ||
2083 | =over 4 | |
2084 | ||
2085 | =item * The outer part of the loop, starting at the C<CMD> label. This loop | |
2086 | reads a command and then executes it. | |
2087 | ||
2088 | =item * The inner part of the loop, starting at the C<PIPE> label. This part | |
2089 | is wholly contained inside the C<CMD> block and only executes a command. | |
2090 | Used to handle commands running inside a pager. | |
2091 | ||
2092 | =back | |
2093 | ||
2094 | So why have two labels to restart the loop? Because sometimes, it's easier to | |
2095 | have a command I<generate> another command and then re-execute the loop to do | |
2096 | the new command. This is faster, but perhaps a bit more convoluted. | |
2097 | ||
2098 | =cut | |
2099 | ||
2100 | # The big command dispatch loop. It keeps running until the | |
2101 | # user yields up control again. | |
2102 | # | |
2103 | # If we have a terminal for input, and we get something back | |
2104 | # from readline(), keep on processing. | |
e22ea7cc RF |
2105 | CMD: |
2106 | while ( | |
2107 | ||
69893cff | 2108 | # We have a terminal, or can get one ... |
e22ea7cc RF |
2109 | ( $term || &setterm ), |
2110 | ||
69893cff | 2111 | # ... and it belogs to this PID or we get one for this PID ... |
e22ea7cc RF |
2112 | ( $term_pid == $$ or resetterm(1) ), |
2113 | ||
69893cff | 2114 | # ... and we got a line of command input ... |
e22ea7cc RF |
2115 | defined( |
2116 | $cmd = &readline( | |
2117 | "$pidprompt DB" | |
2118 | . ( '<' x $level ) | |
2119 | . ( $#hist + 1 ) | |
2120 | . ( '>' x $level ) . " " | |
69893cff RGS |
2121 | ) |
2122 | ) | |
2123 | ) | |
2124 | { | |
e22ea7cc | 2125 | |
69893cff RGS |
2126 | # ... try to execute the input as debugger commands. |
2127 | ||
2128 | # Don't stop running. | |
2129 | $single = 0; | |
2130 | ||
2131 | # No signal is active. | |
2132 | $signal = 0; | |
2133 | ||
2134 | # Handle continued commands (ending with \): | |
e22ea7cc RF |
2135 | $cmd =~ s/\\$/\n/ && do { |
2136 | $cmd .= &readline(" cont: "); | |
2137 | redo CMD; | |
2138 | }; | |
69893cff RGS |
2139 | |
2140 | =head4 The null command | |
2141 | ||
2142 | A newline entered by itself means "re-execute the last command". We grab the | |
2143 | command out of C<$laststep> (where it was recorded previously), and copy it | |
2144 | back into C<$cmd> to be executed below. If there wasn't any previous command, | |
2145 | we'll do nothing below (no command will match). If there was, we also save it | |
2146 | in the command history and fall through to allow the command parsing to pick | |
2147 | it up. | |
2148 | ||
2149 | =cut | |
2150 | ||
2151 | # Empty input means repeat the last command. | |
e22ea7cc RF |
2152 | $cmd =~ /^$/ && ( $cmd = $laststep ); |
2153 | chomp($cmd); # get rid of the annoying extra newline | |
2154 | push( @hist, $cmd ) if length($cmd) > 1; | |
2155 | push( @truehist, $cmd ); | |
2156 | ||
2157 | # This is a restart point for commands that didn't arrive | |
2158 | # via direct user input. It allows us to 'redo PIPE' to | |
2159 | # re-execute command processing without reading a new command. | |
69893cff | 2160 | PIPE: { |
e22ea7cc RF |
2161 | $cmd =~ s/^\s+//s; # trim annoying leading whitespace |
2162 | $cmd =~ s/\s+$//s; # trim annoying trailing whitespace | |
2163 | ($i) = split( /\s+/, $cmd ); | |
69893cff RGS |
2164 | |
2165 | =head3 COMMAND ALIASES | |
2166 | ||
2167 | The debugger can create aliases for commands (these are stored in the | |
2168 | C<%alias> hash). Before a command is executed, the command loop looks it up | |
2169 | in the alias hash and substitutes the contents of the alias for the command, | |
2170 | completely replacing it. | |
2171 | ||
2172 | =cut | |
2173 | ||
2174 | # See if there's an alias for the command, and set it up if so. | |
e22ea7cc RF |
2175 | if ( $alias{$i} ) { |
2176 | ||
69893cff RGS |
2177 | # Squelch signal handling; we want to keep control here |
2178 | # if something goes loco during the alias eval. | |
2179 | local $SIG{__DIE__}; | |
2180 | local $SIG{__WARN__}; | |
2181 | ||
2182 | # This is a command, so we eval it in the DEBUGGER's | |
2183 | # scope! Otherwise, we can't see the special debugger | |
2184 | # variables, or get to the debugger's subs. (Well, we | |
2185 | # _could_, but why make it even more complicated?) | |
2186 | eval "\$cmd =~ $alias{$i}"; | |
2187 | if ($@) { | |
2188 | local $\ = ''; | |
2189 | print $OUT "Couldn't evaluate `$i' alias: $@"; | |
2190 | next CMD; | |
2191 | } | |
2192 | } ## end if ($alias{$i}) | |
2193 | ||
2194 | =head3 MAIN-LINE COMMANDS | |
2195 | ||
2196 | All of these commands work up to and after the program being debugged has | |
2197 | terminated. | |
2198 | ||
2199 | =head4 C<q> - quit | |
2200 | ||
2201 | Quit the debugger. This entails setting the C<$fall_off_end> flag, so we don't | |
2202 | try to execute further, cleaning any restart-related stuff out of the | |
2203 | environment, and executing with the last value of C<$?>. | |
2204 | ||
2205 | =cut | |
2206 | ||
2207 | $cmd =~ /^q$/ && do { | |
2208 | $fall_off_end = 1; | |
2209 | clean_ENV(); | |
2210 | exit $?; | |
2211 | }; | |
2212 | ||
2213 | =head4 C<t> - trace | |
2214 | ||
2215 | Turn tracing on or off. Inverts the appropriate bit in C<$trace> (q.v.). | |
2216 | ||
2217 | =cut | |
2218 | ||
e22ea7cc RF |
2219 | $cmd =~ /^t$/ && do { |
2220 | $trace ^= 1; | |
2221 | local $\ = ''; | |
2222 | print $OUT "Trace = " | |
2223 | . ( ( $trace & 1 ) ? "on" : "off" ) . "\n"; | |
2224 | next CMD; | |
2225 | }; | |
69893cff RGS |
2226 | |
2227 | =head4 C<S> - list subroutines matching/not matching a pattern | |
2228 | ||
2229 | Walks through C<%sub>, checking to see whether or not to print the name. | |
2230 | ||
2231 | =cut | |
2232 | ||
e22ea7cc | 2233 | $cmd =~ /^S(\s+(!)?(.+))?$/ && do { |
69893cff | 2234 | |
e22ea7cc | 2235 | $Srev = defined $2; # Reverse scan? |
69893cff RGS |
2236 | $Spatt = $3; # The pattern (if any) to use. |
2237 | $Snocheck = !defined $1; # No args - print all subs. | |
2238 | ||
2239 | # Need to make these sane here. | |
e22ea7cc RF |
2240 | local $\ = ''; |
2241 | local $, = ''; | |
69893cff RGS |
2242 | |
2243 | # Search through the debugger's magical hash of subs. | |
2244 | # If $nocheck is true, just print the sub name. | |
2245 | # Otherwise, check it against the pattern. We then use | |
2246 | # the XOR trick to reverse the condition as required. | |
e22ea7cc RF |
2247 | foreach $subname ( sort( keys %sub ) ) { |
2248 | if ( $Snocheck or $Srev ^ ( $subname =~ /$Spatt/ ) ) { | |
2249 | print $OUT $subname, "\n"; | |
2250 | } | |
2251 | } | |
2252 | next CMD; | |
2253 | }; | |
69893cff RGS |
2254 | |
2255 | =head4 C<X> - list variables in current package | |
2256 | ||
2257 | Since the C<V> command actually processes this, just change this to the | |
2258 | appropriate C<V> command and fall through. | |
2259 | ||
2260 | =cut | |
2261 | ||
e22ea7cc | 2262 | $cmd =~ s/^X\b/V $package/; |
69893cff RGS |
2263 | |
2264 | =head4 C<V> - list variables | |
2265 | ||
2266 | Uses C<dumpvar.pl> to dump out the current values for selected variables. | |
2267 | ||
2268 | =cut | |
2269 | ||
2270 | # Bare V commands get the currently-being-debugged package | |
2271 | # added. | |
e22ea7cc RF |
2272 | $cmd =~ /^V$/ && do { |
2273 | $cmd = "V $package"; | |
2274 | }; | |
69893cff RGS |
2275 | |
2276 | # V - show variables in package. | |
2277 | $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do { | |
e22ea7cc | 2278 | |
69893cff RGS |
2279 | # Save the currently selected filehandle and |
2280 | # force output to debugger's filehandle (dumpvar | |
2281 | # just does "print" for output). | |
e22ea7cc | 2282 | local ($savout) = select($OUT); |
69893cff RGS |
2283 | |
2284 | # Grab package name and variables to dump. | |
e22ea7cc RF |
2285 | $packname = $1; |
2286 | @vars = split( ' ', $2 ); | |
69893cff RGS |
2287 | |
2288 | # If main::dumpvar isn't here, get it. | |
e22ea7cc RF |
2289 | do 'dumpvar.pl' unless defined &main::dumpvar; |
2290 | if ( defined &main::dumpvar ) { | |
2291 | ||
69893cff RGS |
2292 | # We got it. Turn off subroutine entry/exit messages |
2293 | # for the moment, along with return values. | |
e22ea7cc RF |
2294 | local $frame = 0; |
2295 | local $doret = -2; | |
69893cff RGS |
2296 | |
2297 | # must detect sigpipe failures - not catching | |
2298 | # then will cause the debugger to die. | |
2299 | eval { | |
2300 | &main::dumpvar( | |
2301 | $packname, | |
2302 | defined $option{dumpDepth} | |
e22ea7cc RF |
2303 | ? $option{dumpDepth} |
2304 | : -1, # assume -1 unless specified | |
69893cff | 2305 | @vars |
e22ea7cc RF |
2306 | ); |
2307 | }; | |
2308 | ||
2309 | # The die doesn't need to include the $@, because | |
2310 | # it will automatically get propagated for us. | |
2311 | if ($@) { | |
2312 | die unless $@ =~ /dumpvar print failed/; | |
2313 | } | |
2314 | } ## end if (defined &main::dumpvar) | |
2315 | else { | |
2316 | ||
2317 | # Couldn't load dumpvar. | |
2318 | print $OUT "dumpvar.pl not available.\n"; | |
2319 | } | |
69893cff | 2320 | |
69893cff | 2321 | # Restore the output filehandle, and go round again. |
e22ea7cc RF |
2322 | select($savout); |
2323 | next CMD; | |
2324 | }; | |
69893cff RGS |
2325 | |
2326 | =head4 C<x> - evaluate and print an expression | |
2327 | ||
2328 | Hands the expression off to C<DB::eval>, setting it up to print the value | |
2329 | via C<dumpvar.pl> instead of just printing it directly. | |
2330 | ||
2331 | =cut | |
2332 | ||
e22ea7cc RF |
2333 | $cmd =~ s/^x\b/ / && do { # Remainder gets done by DB::eval() |
2334 | $onetimeDump = 'dump'; # main::dumpvar shows the output | |
69893cff RGS |
2335 | |
2336 | # handle special "x 3 blah" syntax XXX propagate | |
2337 | # doc back to special variables. | |
e22ea7cc RF |
2338 | if ( $cmd =~ s/^\s*(\d+)(?=\s)/ / ) { |
2339 | $onetimedumpDepth = $1; | |
2340 | } | |
2341 | }; | |
69893cff RGS |
2342 | |
2343 | =head4 C<m> - print methods | |
2344 | ||
2345 | Just uses C<DB::methods> to determine what methods are available. | |
2346 | ||
2347 | =cut | |
2348 | ||
e22ea7cc RF |
2349 | $cmd =~ s/^m\s+([\w:]+)\s*$/ / && do { |
2350 | methods($1); | |
2351 | next CMD; | |
2352 | }; | |
69893cff RGS |
2353 | |
2354 | # m expr - set up DB::eval to do the work | |
e22ea7cc RF |
2355 | $cmd =~ s/^m\b/ / && do { # Rest gets done by DB::eval() |
2356 | $onetimeDump = 'methods'; # method output gets used there | |
2357 | }; | |
69893cff RGS |
2358 | |
2359 | =head4 C<f> - switch files | |
2360 | ||
2361 | =cut | |
2362 | ||
e22ea7cc RF |
2363 | $cmd =~ /^f\b\s*(.*)/ && do { |
2364 | $file = $1; | |
2365 | $file =~ s/\s+$//; | |
69893cff RGS |
2366 | |
2367 | # help for no arguments (old-style was return from sub). | |
e22ea7cc RF |
2368 | if ( !$file ) { |
2369 | print $OUT | |
2370 | "The old f command is now the r command.\n"; # hint | |
2371 | print $OUT "The new f command switches filenames.\n"; | |
2372 | next CMD; | |
2373 | } ## end if (!$file) | |
69893cff RGS |
2374 | |
2375 | # if not in magic file list, try a close match. | |
e22ea7cc RF |
2376 | if ( !defined $main::{ '_<' . $file } ) { |
2377 | if ( ($try) = grep( m#^_<.*$file#, keys %main:: ) ) { | |
2378 | { | |
2379 | $try = substr( $try, 2 ); | |
2380 | print $OUT "Choosing $try matching `$file':\n"; | |
2381 | $file = $try; | |
2382 | } | |
2383 | } ## end if (($try) = grep(m#^_<.*$file#... | |
2384 | } ## end if (!defined $main::{ ... | |
69893cff RGS |
2385 | |
2386 | # If not successfully switched now, we failed. | |
e22ea7cc RF |
2387 | if ( !defined $main::{ '_<' . $file } ) { |
2388 | print $OUT "No file matching `$file' is loaded.\n"; | |
2389 | next CMD; | |
2390 | } | |
69893cff | 2391 | |
e22ea7cc RF |
2392 | # We switched, so switch the debugger internals around. |
2393 | elsif ( $file ne $filename ) { | |
2394 | *dbline = $main::{ '_<' . $file }; | |
2395 | $max = $#dbline; | |
2396 | $filename = $file; | |
2397 | $start = 1; | |
2398 | $cmd = "l"; | |
2399 | } ## end elsif ($file ne $filename) | |
2400 | ||
2401 | # We didn't switch; say we didn't. | |
2402 | else { | |
2403 | print $OUT "Already in $file.\n"; | |
2404 | next CMD; | |
2405 | } | |
2406 | }; | |
69893cff RGS |
2407 | |
2408 | =head4 C<.> - return to last-executed line. | |
2409 | ||
2410 | We set C<$incr> to -1 to indicate that the debugger shouldn't move ahead, | |
2411 | and then we look up the line in the magical C<%dbline> hash. | |
2412 | ||
2413 | =cut | |
2414 | ||
2415 | # . command. | |
e22ea7cc RF |
2416 | $cmd =~ /^\.$/ && do { |
2417 | $incr = -1; # stay at current line | |
69893cff RGS |
2418 | |
2419 | # Reset everything to the old location. | |
e22ea7cc RF |
2420 | $start = $line; |
2421 | $filename = $filename_ini; | |
2422 | *dbline = $main::{ '_<' . $filename }; | |
2423 | $max = $#dbline; | |
69893cff RGS |
2424 | |
2425 | # Now where are we? | |
e22ea7cc RF |
2426 | print_lineinfo($position); |
2427 | next CMD; | |
2428 | }; | |
69893cff RGS |
2429 | |
2430 | =head4 C<-> - back one window | |
2431 | ||
2432 | We change C<$start> to be one window back; if we go back past the first line, | |
2433 | we set it to be the first line. We ser C<$incr> to put us back at the | |
2434 | currently-executing line, and then put a C<l $start +> (list one window from | |
2435 | C<$start>) in C<$cmd> to be executed later. | |
2436 | ||
2437 | =cut | |
2438 | ||
2439 | # - - back a window. | |
e22ea7cc RF |
2440 | $cmd =~ /^-$/ && do { |
2441 | ||
69893cff | 2442 | # back up by a window; go to 1 if back too far. |
e22ea7cc RF |
2443 | $start -= $incr + $window + 1; |
2444 | $start = 1 if $start <= 0; | |
2445 | $incr = $window - 1; | |
69893cff RGS |
2446 | |
2447 | # Generate and execute a "l +" command (handled below). | |
e22ea7cc RF |
2448 | $cmd = 'l ' . ($start) . '+'; |
2449 | }; | |
69893cff RGS |
2450 | |
2451 | =head3 PRE-580 COMMANDS VS. NEW COMMANDS: C<a, A, b, B, h, l, L, M, o, O, P, v, w, W, E<lt>, E<lt>E<lt>, {, {{> | |
2452 | ||
2453 | In Perl 5.8.0, a realignment of the commands was done to fix up a number of | |
2454 | problems, most notably that the default case of several commands destroying | |
2455 | the user's work in setting watchpoints, actions, etc. We wanted, however, to | |
2456 | retain the old commands for those who were used to using them or who preferred | |
2457 | them. At this point, we check for the new commands and call C<cmd_wrapper> to | |
2458 | deal with them instead of processing them in-line. | |
2459 | ||
2460 | =cut | |
2461 | ||
2462 | # All of these commands were remapped in perl 5.8.0; | |
e22ea7cc RF |
2463 | # we send them off to the secondary dispatcher (see below). |
2464 | $cmd =~ /^([aAbBhilLMoOPvwW]\b|[<>\{]{1,2})\s*(.*)/so && do { | |
2465 | &cmd_wrapper( $1, $2, $line ); | |
2466 | next CMD; | |
2467 | }; | |
69893cff RGS |
2468 | |
2469 | =head4 C<y> - List lexicals in higher scope | |
2470 | ||
2471 | Uses C<PadWalker> to find the lexicals supplied as arguments in a scope | |
2472 | above the current one and then displays then using C<dumpvar.pl>. | |
2473 | ||
2474 | =cut | |
2475 | ||
2476 | $cmd =~ /^y(?:\s+(\d*)\s*(.*))?$/ && do { | |
2477 | ||
2478 | # See if we've got the necessary support. | |
2479 | eval { require PadWalker; PadWalker->VERSION(0.08) } | |
2480 | or &warn( | |
2481 | $@ =~ /locate/ | |
2482 | ? "PadWalker module not found - please install\n" | |
2483 | : $@ | |
2484 | ) | |
2485 | and next CMD; | |
2486 | ||
2487 | # Load up dumpvar if we don't have it. If we can, that is. | |
2488 | do 'dumpvar.pl' unless defined &main::dumpvar; | |
2489 | defined &main::dumpvar | |
2490 | or print $OUT "dumpvar.pl not available.\n" | |
2491 | and next CMD; | |
2492 | ||
2493 | # Got all the modules we need. Find them and print them. | |
e22ea7cc | 2494 | my @vars = split( ' ', $2 || '' ); |
69893cff RGS |
2495 | |
2496 | # Find the pad. | |
e22ea7cc | 2497 | my $h = eval { PadWalker::peek_my( ( $1 || 0 ) + 1 ) }; |
69893cff RGS |
2498 | |
2499 | # Oops. Can't find it. | |
2500 | $@ and $@ =~ s/ at .*//, &warn($@), next CMD; | |
2501 | ||
2502 | # Show the desired vars with dumplex(). | |
2503 | my $savout = select($OUT); | |
2504 | ||
2505 | # Have dumplex dump the lexicals. | |
e22ea7cc | 2506 | dumpvar::dumplex( $_, $h->{$_}, |
69893cff | 2507 | defined $option{dumpDepth} ? $option{dumpDepth} : -1, |
e22ea7cc RF |
2508 | @vars ) |
2509 | for sort keys %$h; | |
69893cff RGS |
2510 | select($savout); |
2511 | next CMD; | |
2512 | }; | |
2513 | ||
2514 | =head3 COMMANDS NOT WORKING AFTER PROGRAM ENDS | |
2515 | ||
2516 | All of the commands below this point don't work after the program being | |
2517 | debugged has ended. All of them check to see if the program has ended; this | |
2518 | allows the commands to be relocated without worrying about a 'line of | |
2519 | demarcation' above which commands can be entered anytime, and below which | |
2520 | they can't. | |
2521 | ||
2522 | =head4 C<n> - single step, but don't trace down into subs | |
2523 | ||
2524 | Done by setting C<$single> to 2, which forces subs to execute straight through | |
2525 | when entered (see X<DB::sub>). We also save the C<n> command in C<$laststep>, | |
2526 | so a null command knows what to re-execute. | |
2527 | ||
2528 | =cut | |
2529 | ||
e22ea7cc | 2530 | # n - next |
69893cff RGS |
2531 | $cmd =~ /^n$/ && do { |
2532 | end_report(), next CMD if $finished and $level <= 1; | |
e22ea7cc | 2533 | |
69893cff RGS |
2534 | # Single step, but don't enter subs. |
2535 | $single = 2; | |
e22ea7cc | 2536 | |
69893cff | 2537 | # Save for empty command (repeat last). |
e22ea7cc RF |
2538 | $laststep = $cmd; |
2539 | last CMD; | |
2540 | }; | |
69893cff RGS |
2541 | |
2542 | =head4 C<s> - single-step, entering subs | |
2543 | ||
2544 | Sets C<$single> to 1, which causes X<DB::sub> to continue tracing inside | |
2545 | subs. Also saves C<s> as C<$lastcmd>. | |
2546 | ||
2547 | =cut | |
2548 | ||
2549 | # s - single step. | |
2550 | $cmd =~ /^s$/ && do { | |
e22ea7cc | 2551 | |
69893cff RGS |
2552 | # Get out and restart the command loop if program |
2553 | # has finished. | |
e22ea7cc RF |
2554 | end_report(), next CMD if $finished and $level <= 1; |
2555 | ||
69893cff | 2556 | # Single step should enter subs. |
e22ea7cc RF |
2557 | $single = 1; |
2558 | ||
69893cff | 2559 | # Save for empty command (repeat last). |
e22ea7cc RF |
2560 | $laststep = $cmd; |
2561 | last CMD; | |
2562 | }; | |
69893cff RGS |
2563 | |
2564 | =head4 C<c> - run continuously, setting an optional breakpoint | |
2565 | ||
2566 | Most of the code for this command is taken up with locating the optional | |
2567 | breakpoint, which is either a subroutine name or a line number. We set | |
2568 | the appropriate one-time-break in C<@dbline> and then turn off single-stepping | |
2569 | in this and all call levels above this one. | |
2570 | ||
2571 | =cut | |
2572 | ||
2573 | # c - start continuous execution. | |
2574 | $cmd =~ /^c\b\s*([\w:]*)\s*$/ && do { | |
e22ea7cc | 2575 | |
69893cff RGS |
2576 | # Hey, show's over. The debugged program finished |
2577 | # executing already. | |
2578 | end_report(), next CMD if $finished and $level <= 1; | |
2579 | ||
2580 | # Capture the place to put a one-time break. | |
2581 | $subname = $i = $1; | |
2582 | ||
e22ea7cc RF |
2583 | # Probably not needed, since we finish an interactive |
2584 | # sub-session anyway... | |
2585 | # local $filename = $filename; | |
2586 | # local *dbline = *dbline; # XXX Would this work?! | |
69893cff RGS |
2587 | # |
2588 | # The above question wonders if localizing the alias | |
2589 | # to the magic array works or not. Since it's commented | |
2590 | # out, we'll just leave that to speculation for now. | |
2591 | ||
2592 | # If the "subname" isn't all digits, we'll assume it | |
2593 | # is a subroutine name, and try to find it. | |
e22ea7cc RF |
2594 | if ( $subname =~ /\D/ ) { # subroutine name |
2595 | # Qualify it to the current package unless it's | |
2596 | # already qualified. | |
69893cff RGS |
2597 | $subname = $package . "::" . $subname |
2598 | unless $subname =~ /::/; | |
e22ea7cc | 2599 | |
69893cff RGS |
2600 | # find_sub will return "file:line_number" corresponding |
2601 | # to where the subroutine is defined; we call find_sub, | |
e22ea7cc | 2602 | # break up the return value, and assign it in one |
69893cff | 2603 | # operation. |
e22ea7cc | 2604 | ( $file, $i ) = ( find_sub($subname) =~ /^(.*):(.*)$/ ); |
69893cff RGS |
2605 | |
2606 | # Force the line number to be numeric. | |
e22ea7cc | 2607 | $i += 0; |
69893cff RGS |
2608 | |
2609 | # If we got a line number, we found the sub. | |
e22ea7cc RF |
2610 | if ($i) { |
2611 | ||
69893cff RGS |
2612 | # Switch all the debugger's internals around so |
2613 | # we're actually working with that file. | |
e22ea7cc RF |
2614 | $filename = $file; |
2615 | *dbline = $main::{ '_<' . $filename }; | |
2616 | ||
69893cff | 2617 | # Mark that there's a breakpoint in this file. |
e22ea7cc RF |
2618 | $had_breakpoints{$filename} |= 1; |
2619 | ||
69893cff RGS |
2620 | # Scan forward to the first executable line |
2621 | # after the 'sub whatever' line. | |
e22ea7cc RF |
2622 | $max = $#dbline; |
2623 | ++$i while $dbline[$i] == 0 && $i < $max; | |
2624 | } ## end if ($i) | |
69893cff RGS |
2625 | |
2626 | # We didn't find a sub by that name. | |
e22ea7cc RF |
2627 | else { |
2628 | print $OUT "Subroutine $subname not found.\n"; | |
2629 | next CMD; | |
2630 | } | |
2631 | } ## end if ($subname =~ /\D/) | |
69893cff RGS |
2632 | |
2633 | # At this point, either the subname was all digits (an | |
2634 | # absolute line-break request) or we've scanned through | |
2635 | # the code following the definition of the sub, looking | |
2636 | # for an executable, which we may or may not have found. | |
2637 | # | |
2638 | # If $i (which we set $subname from) is non-zero, we | |
e22ea7cc RF |
2639 | # got a request to break at some line somewhere. On |
2640 | # one hand, if there wasn't any real subroutine name | |
2641 | # involved, this will be a request to break in the current | |
2642 | # file at the specified line, so we have to check to make | |
69893cff RGS |
2643 | # sure that the line specified really is breakable. |
2644 | # | |
2645 | # On the other hand, if there was a subname supplied, the | |
2646 | # preceeding block has moved us to the proper file and | |
2647 | # location within that file, and then scanned forward | |
2648 | # looking for the next executable line. We have to make | |
2649 | # sure that one was found. | |
2650 | # | |
2651 | # On the gripping hand, we can't do anything unless the | |
2652 | # current value of $i points to a valid breakable line. | |
2653 | # Check that. | |
e22ea7cc RF |
2654 | if ($i) { |
2655 | ||
69893cff | 2656 | # Breakable? |
e22ea7cc RF |
2657 | if ( $dbline[$i] == 0 ) { |
2658 | print $OUT "Line $i not breakable.\n"; | |
2659 | next CMD; | |
2660 | } | |
2661 | ||
69893cff | 2662 | # Yes. Set up the one-time-break sigil. |
e22ea7cc RF |
2663 | $dbline{$i} =~ s/($|\0)/;9$1/; # add one-time-only b.p. |
2664 | } ## end if ($i) | |
69893cff RGS |
2665 | |
2666 | # Turn off stack tracing from here up. | |
e22ea7cc RF |
2667 | for ( $i = 0 ; $i <= $stack_depth ; ) { |
2668 | $stack[ $i++ ] &= ~1; | |
2669 | } | |
2670 | last CMD; | |
2671 | }; | |
69893cff RGS |
2672 | |
2673 | =head4 C<r> - return from a subroutine | |
2674 | ||
2675 | For C<r> to work properly, the debugger has to stop execution again | |
2676 | immediately after the return is executed. This is done by forcing | |
2677 | single-stepping to be on in the call level above the current one. If | |
2678 | we are printing return values when a C<r> is executed, set C<$doret> | |
2679 | appropriately, and force us out of the command loop. | |
2680 | ||
2681 | =cut | |
2682 | ||
2683 | # r - return from the current subroutine. | |
e22ea7cc RF |
2684 | $cmd =~ /^r$/ && do { |
2685 | ||
69893cff | 2686 | # Can't do anythign if the program's over. |
e22ea7cc RF |
2687 | end_report(), next CMD if $finished and $level <= 1; |
2688 | ||
69893cff | 2689 | # Turn on stack trace. |
e22ea7cc RF |
2690 | $stack[$stack_depth] |= 1; |
2691 | ||
69893cff | 2692 | # Print return value unless the stack is empty. |
e22ea7cc RF |
2693 | $doret = $option{PrintRet} ? $stack_depth - 1 : -2; |
2694 | last CMD; | |
2695 | }; | |
69893cff | 2696 | |
69893cff RGS |
2697 | =head4 C<T> - stack trace |
2698 | ||
2699 | Just calls C<DB::print_trace>. | |
2700 | ||
2701 | =cut | |
2702 | ||
e22ea7cc RF |
2703 | $cmd =~ /^T$/ && do { |
2704 | print_trace( $OUT, 1 ); # skip DB | |
2705 | next CMD; | |
2706 | }; | |
69893cff RGS |
2707 | |
2708 | =head4 C<w> - List window around current line. | |
2709 | ||
2710 | Just calls C<DB::cmd_w>. | |
2711 | ||
2712 | =cut | |
2713 | ||
e22ea7cc | 2714 | $cmd =~ /^w\b\s*(.*)/s && do { &cmd_w( 'w', $1 ); next CMD; }; |
69893cff RGS |
2715 | |
2716 | =head4 C<W> - watch-expression processing. | |
2717 | ||
2718 | Just calls C<DB::cmd_W>. | |
2719 | ||
2720 | =cut | |
2721 | ||
e22ea7cc | 2722 | $cmd =~ /^W\b\s*(.*)/s && do { &cmd_W( 'W', $1 ); next CMD; }; |
69893cff RGS |
2723 | |
2724 | =head4 C</> - search forward for a string in the source | |
2725 | ||
2726 | We take the argument and treat it as a pattern. If it turns out to be a | |
2727 | bad one, we return the error we got from trying to C<eval> it and exit. | |
2728 | If not, we create some code to do the search and C<eval> it so it can't | |
2729 | mess us up. | |
2730 | ||
2731 | =cut | |
2732 | ||
e22ea7cc | 2733 | $cmd =~ /^\/(.*)$/ && do { |
69893cff RGS |
2734 | |
2735 | # The pattern as a string. | |
e22ea7cc | 2736 | $inpat = $1; |
69893cff RGS |
2737 | |
2738 | # Remove the final slash. | |
e22ea7cc | 2739 | $inpat =~ s:([^\\])/$:$1:; |
69893cff RGS |
2740 | |
2741 | # If the pattern isn't null ... | |
e22ea7cc | 2742 | if ( $inpat ne "" ) { |
69893cff RGS |
2743 | |
2744 | # Turn of warn and die procesing for a bit. | |
e22ea7cc RF |
2745 | local $SIG{__DIE__}; |
2746 | local $SIG{__WARN__}; | |
69893cff RGS |
2747 | |
2748 | # Create the pattern. | |
e22ea7cc RF |
2749 | eval '$inpat =~ m' . "\a$inpat\a"; |
2750 | if ( $@ ne "" ) { | |
2751 | ||
69893cff | 2752 | # Oops. Bad pattern. No biscuit. |
e22ea7cc | 2753 | # Print the eval error and go back for more |
69893cff | 2754 | # commands. |
e22ea7cc RF |
2755 | print $OUT "$@"; |
2756 | next CMD; | |
2757 | } | |
2758 | $pat = $inpat; | |
2759 | } ## end if ($inpat ne "") | |
69893cff RGS |
2760 | |
2761 | # Set up to stop on wrap-around. | |
e22ea7cc | 2762 | $end = $start; |
69893cff RGS |
2763 | |
2764 | # Don't move off the current line. | |
e22ea7cc | 2765 | $incr = -1; |
69893cff RGS |
2766 | |
2767 | # Done in eval so nothing breaks if the pattern | |
2768 | # does something weird. | |
e22ea7cc RF |
2769 | eval ' |
2770 | for (;;) { | |
69893cff | 2771 | # Move ahead one line. |
e22ea7cc | 2772 | ++$start; |
69893cff RGS |
2773 | |
2774 | # Wrap if we pass the last line. | |
e22ea7cc | 2775 | $start = 1 if ($start > $max); |
69893cff RGS |
2776 | |
2777 | # Stop if we have gotten back to this line again, | |
e22ea7cc | 2778 | last if ($start == $end); |
69893cff RGS |
2779 | |
2780 | # A hit! (Note, though, that we are doing | |
2781 | # case-insensitive matching. Maybe a qr// | |
2782 | # expression would be better, so the user could | |
2783 | # do case-sensitive matching if desired. | |
e22ea7cc RF |
2784 | if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) { |
2785 | if ($slave_editor) { | |
69893cff | 2786 | # Handle proper escaping in the slave. |
e22ea7cc RF |
2787 | print $OUT "\032\032$filename:$start:0\n"; |
2788 | } | |
2789 | else { | |
69893cff | 2790 | # Just print the line normally. |
e22ea7cc RF |
2791 | print $OUT "$start:\t",$dbline[$start],"\n"; |
2792 | } | |
69893cff | 2793 | # And quit since we found something. |
e22ea7cc RF |
2794 | last; |
2795 | } | |
2796 | } '; | |
2797 | ||
69893cff | 2798 | # If we wrapped, there never was a match. |
e22ea7cc RF |
2799 | print $OUT "/$pat/: not found\n" if ( $start == $end ); |
2800 | next CMD; | |
2801 | }; | |
69893cff RGS |
2802 | |
2803 | =head4 C<?> - search backward for a string in the source | |
2804 | ||
2805 | Same as for C</>, except the loop runs backwards. | |
2806 | ||
2807 | =cut | |
2808 | ||
2809 | # ? - backward pattern search. | |
e22ea7cc | 2810 | $cmd =~ /^\?(.*)$/ && do { |
69893cff RGS |
2811 | |
2812 | # Get the pattern, remove trailing question mark. | |
e22ea7cc RF |
2813 | $inpat = $1; |
2814 | $inpat =~ s:([^\\])\?$:$1:; | |
69893cff RGS |
2815 | |
2816 | # If we've got one ... | |
e22ea7cc | 2817 | if ( $inpat ne "" ) { |
69893cff RGS |
2818 | |
2819 | # Turn off die & warn handlers. | |
e22ea7cc RF |
2820 | local $SIG{__DIE__}; |
2821 | local $SIG{__WARN__}; | |
2822 | eval '$inpat =~ m' . "\a$inpat\a"; | |
2823 | ||
2824 | if ( $@ ne "" ) { | |
2825 | ||
69893cff | 2826 | # Ouch. Not good. Print the error. |
e22ea7cc RF |
2827 | print $OUT $@; |
2828 | next CMD; | |
2829 | } | |
2830 | $pat = $inpat; | |
69893cff | 2831 | } ## end if ($inpat ne "") |
e22ea7cc | 2832 | |
69893cff | 2833 | # Where we are now is where to stop after wraparound. |
e22ea7cc | 2834 | $end = $start; |
69893cff RGS |
2835 | |
2836 | # Don't move away from this line. | |
e22ea7cc | 2837 | $incr = -1; |
69893cff RGS |
2838 | |
2839 | # Search inside the eval to prevent pattern badness | |
2840 | # from killing us. | |
e22ea7cc RF |
2841 | eval ' |
2842 | for (;;) { | |
69893cff | 2843 | # Back up a line. |
e22ea7cc | 2844 | --$start; |
69893cff RGS |
2845 | |
2846 | # Wrap if we pass the first line. | |
e22ea7cc RF |
2847 | |
2848 | $start = $max if ($start <= 0); | |
69893cff RGS |
2849 | |
2850 | # Quit if we get back where we started, | |
e22ea7cc | 2851 | last if ($start == $end); |
69893cff RGS |
2852 | |
2853 | # Match? | |
e22ea7cc RF |
2854 | if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) { |
2855 | if ($slave_editor) { | |
69893cff | 2856 | # Yep, follow slave editor requirements. |
e22ea7cc RF |
2857 | print $OUT "\032\032$filename:$start:0\n"; |
2858 | } | |
2859 | else { | |
69893cff | 2860 | # Yep, just print normally. |
e22ea7cc RF |
2861 | print $OUT "$start:\t",$dbline[$start],"\n"; |
2862 | } | |
69893cff RGS |
2863 | |
2864 | # Found, so done. | |
e22ea7cc RF |
2865 | last; |
2866 | } | |
2867 | } '; | |
2868 | ||
2869 | # Say we failed if the loop never found anything, | |
2870 | print $OUT "?$pat?: not found\n" if ( $start == $end ); | |
2871 | next CMD; | |
2872 | }; | |
69893cff RGS |
2873 | |
2874 | =head4 C<$rc> - Recall command | |
2875 | ||
2876 | Manages the commands in C<@hist> (which is created if C<Term::ReadLine> reports | |
2877 | that the terminal supports history). It find the the command required, puts it | |
2878 | into C<$cmd>, and redoes the loop to execute it. | |
2879 | ||
2880 | =cut | |
2881 | ||
e22ea7cc RF |
2882 | # $rc - recall command. |
2883 | $cmd =~ /^$rc+\s*(-)?(\d+)?$/ && do { | |
69893cff RGS |
2884 | |
2885 | # No arguments, take one thing off history. | |
e22ea7cc | 2886 | pop(@hist) if length($cmd) > 1; |
69893cff | 2887 | |
e22ea7cc | 2888 | # Relative (- found)? |
69893cff | 2889 | # Y - index back from most recent (by 1 if bare minus) |
e22ea7cc | 2890 | # N - go to that particular command slot or the last |
69893cff | 2891 | # thing if nothing following. |
e22ea7cc | 2892 | $i = $1 ? ( $#hist - ( $2 || 1 ) ) : ( $2 || $#hist ); |
69893cff RGS |
2893 | |
2894 | # Pick out the command desired. | |
e22ea7cc | 2895 | $cmd = $hist[$i]; |
69893cff RGS |
2896 | |
2897 | # Print the command to be executed and restart the loop | |
2898 | # with that command in the buffer. | |
e22ea7cc RF |
2899 | print $OUT $cmd, "\n"; |
2900 | redo CMD; | |
2901 | }; | |
69893cff RGS |
2902 | |
2903 | =head4 C<$sh$sh> - C<system()> command | |
2904 | ||
2905 | Calls the C<DB::system()> to handle the command. This keeps the C<STDIN> and | |
2906 | C<STDOUT> from getting messed up. | |
2907 | ||
2908 | =cut | |
2909 | ||
2910 | # $sh$sh - run a shell command (if it's all ASCII). | |
2911 | # Can't run shell commands with Unicode in the debugger, hmm. | |
e22ea7cc RF |
2912 | $cmd =~ /^$sh$sh\s*([\x00-\xff]*)/ && do { |
2913 | ||
69893cff | 2914 | # System it. |
e22ea7cc RF |
2915 | &system($1); |
2916 | next CMD; | |
2917 | }; | |
69893cff RGS |
2918 | |
2919 | =head4 C<$rc I<pattern> $rc> - Search command history | |
2920 | ||
2921 | Another command to manipulate C<@hist>: this one searches it with a pattern. | |
2922 | If a command is found, it is placed in C<$cmd> and executed via <redo>. | |
2923 | ||
2924 | =cut | |
2925 | ||
e22ea7cc RF |
2926 | # $rc pattern $rc - find a command in the history. |
2927 | $cmd =~ /^$rc([^$rc].*)$/ && do { | |
2928 | ||
69893cff | 2929 | # Create the pattern to use. |
e22ea7cc | 2930 | $pat = "^$1"; |
69893cff RGS |
2931 | |
2932 | # Toss off last entry if length is >1 (and it always is). | |
e22ea7cc | 2933 | pop(@hist) if length($cmd) > 1; |
69893cff RGS |
2934 | |
2935 | # Look backward through the history. | |
e22ea7cc RF |
2936 | for ( $i = $#hist ; $i ; --$i ) { |
2937 | ||
69893cff | 2938 | # Stop if we find it. |
e22ea7cc RF |
2939 | last if $hist[$i] =~ /$pat/; |
2940 | } | |
2941 | ||
2942 | if ( !$i ) { | |
69893cff | 2943 | |
69893cff | 2944 | # Never found it. |
e22ea7cc RF |
2945 | print $OUT "No such command!\n\n"; |
2946 | next CMD; | |
2947 | } | |
69893cff RGS |
2948 | |
2949 | # Found it. Put it in the buffer, print it, and process it. | |
e22ea7cc RF |
2950 | $cmd = $hist[$i]; |
2951 | print $OUT $cmd, "\n"; | |
2952 | redo CMD; | |
2953 | }; | |
69893cff RGS |
2954 | |
2955 | =head4 C<$sh> - Invoke a shell | |
2956 | ||
2957 | Uses C<DB::system> to invoke a shell. | |
2958 | ||
2959 | =cut | |
2960 | ||
2961 | # $sh - start a shell. | |
e22ea7cc RF |
2962 | $cmd =~ /^$sh$/ && do { |
2963 | ||
69893cff RGS |
2964 | # Run the user's shell. If none defined, run Bourne. |
2965 | # We resume execution when the shell terminates. | |
e22ea7cc RF |
2966 | &system( $ENV{SHELL} || "/bin/sh" ); |
2967 | next CMD; | |
2968 | }; | |
69893cff RGS |
2969 | |
2970 | =head4 C<$sh I<command>> - Force execution of a command in a shell | |
2971 | ||
2972 | Like the above, but the command is passed to the shell. Again, we use | |
2973 | C<DB::system> to avoid problems with C<STDIN> and C<STDOUT>. | |
2974 | ||
2975 | =cut | |
2976 | ||
2977 | # $sh command - start a shell and run a command in it. | |
e22ea7cc RF |
2978 | $cmd =~ /^$sh\s*([\x00-\xff]*)/ && do { |
2979 | ||
2980 | # XXX: using csh or tcsh destroys sigint retvals! | |
2981 | #&system($1); # use this instead | |
69893cff RGS |
2982 | |
2983 | # use the user's shell, or Bourne if none defined. | |
e22ea7cc RF |
2984 | &system( $ENV{SHELL} || "/bin/sh", "-c", $1 ); |
2985 | next CMD; | |
2986 | }; | |
69893cff RGS |
2987 | |
2988 | =head4 C<H> - display commands in history | |
2989 | ||
2990 | Prints the contents of C<@hist> (if any). | |
2991 | ||
2992 | =cut | |
2993 | ||
7fddc82f RF |
2994 | $cmd =~ /^H\b\s*\*/ && do { |
2995 | @hist = @truehist = (); | |
2996 | print $OUT "History cleansed\n"; | |
2997 | next CMD; | |
2998 | }; | |
e22ea7cc RF |
2999 | |
3000 | $cmd =~ /^H\b\s*(-(\d+))?/ && do { | |
3001 | ||
3002 | # Anything other than negative numbers is ignored by | |
69893cff | 3003 | # the (incorrect) pattern, so this test does nothing. |
e22ea7cc | 3004 | $end = $2 ? ( $#hist - $2 ) : 0; |
69893cff RGS |
3005 | |
3006 | # Set to the minimum if less than zero. | |
e22ea7cc | 3007 | $hist = 0 if $hist < 0; |
69893cff | 3008 | |
e22ea7cc | 3009 | # Start at the end of the array. |
69893cff RGS |
3010 | # Stay in while we're still above the ending value. |
3011 | # Tick back by one each time around the loop. | |
e22ea7cc | 3012 | for ( $i = $#hist ; $i > $end ; $i-- ) { |
69893cff RGS |
3013 | |
3014 | # Print the command unless it has no arguments. | |
e22ea7cc RF |
3015 | print $OUT "$i: ", $hist[$i], "\n" |
3016 | unless $hist[$i] =~ /^.?$/; | |
3017 | } | |
3018 | next CMD; | |
3019 | }; | |
69893cff RGS |
3020 | |
3021 | =head4 C<man, doc, perldoc> - look up documentation | |
3022 | ||
3023 | Just calls C<runman()> to print the appropriate document. | |
3024 | ||
3025 | =cut | |
3026 | ||
e22ea7cc RF |
3027 | # man, perldoc, doc - show manual pages. |
3028 | $cmd =~ /^(?:man|(?:perl)?doc)\b(?:\s+([^(]*))?$/ && do { | |
3029 | runman($1); | |
3030 | next CMD; | |
3031 | }; | |
69893cff RGS |
3032 | |
3033 | =head4 C<p> - print | |
3034 | ||
3035 | Builds a C<print EXPR> expression in the C<$cmd>; this will get executed at | |
3036 | the bottom of the loop. | |
3037 | ||
3038 | =cut | |
3039 | ||
3040 | # p - print (no args): print $_. | |
e22ea7cc | 3041 | $cmd =~ s/^p$/print {\$DB::OUT} \$_/; |
69893cff RGS |
3042 | |
3043 | # p - print the given expression. | |
e22ea7cc | 3044 | $cmd =~ s/^p\b/print {\$DB::OUT} /; |
69893cff RGS |
3045 | |
3046 | =head4 C<=> - define command alias | |
3047 | ||
3048 | Manipulates C<%alias> to add or list command aliases. | |
3049 | ||
3050 | =cut | |
3051 | ||
e22ea7cc RF |
3052 | # = - set up a command alias. |
3053 | $cmd =~ s/^=\s*// && do { | |
3054 | my @keys; | |
3055 | if ( length $cmd == 0 ) { | |
3056 | ||
69893cff | 3057 | # No args, get current aliases. |
e22ea7cc RF |
3058 | @keys = sort keys %alias; |
3059 | } | |
3060 | elsif ( my ( $k, $v ) = ( $cmd =~ /^(\S+)\s+(\S.*)/ ) ) { | |
3061 | ||
69893cff RGS |
3062 | # Creating a new alias. $k is alias name, $v is |
3063 | # alias value. | |
3064 | ||
e22ea7cc RF |
3065 | # can't use $_ or kill //g state |
3066 | for my $x ( $k, $v ) { | |
3067 | ||
3068 | # Escape "alarm" characters. | |
3069 | $x =~ s/\a/\\a/g; | |
3070 | } | |
69893cff RGS |
3071 | |
3072 | # Substitute key for value, using alarm chars | |
e22ea7cc | 3073 | # as separators (which is why we escaped them in |
69893cff | 3074 | # the command). |
e22ea7cc | 3075 | $alias{$k} = "s\a$k\a$v\a"; |
69893cff RGS |
3076 | |
3077 | # Turn off standard warn and die behavior. | |
e22ea7cc RF |
3078 | local $SIG{__DIE__}; |
3079 | local $SIG{__WARN__}; | |
69893cff RGS |
3080 | |
3081 | # Is it valid Perl? | |
e22ea7cc RF |
3082 | unless ( eval "sub { s\a$k\a$v\a }; 1" ) { |
3083 | ||
69893cff | 3084 | # Nope. Bad alias. Say so and get out. |
e22ea7cc RF |
3085 | print $OUT "Can't alias $k to $v: $@\n"; |
3086 | delete $alias{$k}; | |
3087 | next CMD; | |
3088 | } | |
3089 | ||
69893cff | 3090 | # We'll only list the new one. |
e22ea7cc | 3091 | @keys = ($k); |
69893cff RGS |
3092 | } ## end elsif (my ($k, $v) = ($cmd... |
3093 | ||
3094 | # The argument is the alias to list. | |
e22ea7cc RF |
3095 | else { |
3096 | @keys = ($cmd); | |
3097 | } | |
69893cff RGS |
3098 | |
3099 | # List aliases. | |
e22ea7cc RF |
3100 | for my $k (@keys) { |
3101 | ||
69893cff RGS |
3102 | # Messy metaquoting: Trim the substiution code off. |
3103 | # We use control-G as the delimiter because it's not | |
3104 | # likely to appear in the alias. | |
e22ea7cc RF |
3105 | if ( ( my $v = $alias{$k} ) =~ s\as\a$k\a(.*)\a$\a1\a ) { |
3106 | ||
69893cff | 3107 | # Print the alias. |
e22ea7cc RF |
3108 | print $OUT "$k\t= $1\n"; |
3109 | } | |
3110 | elsif ( defined $alias{$k} ) { | |
3111 | ||
69893cff | 3112 | # Couldn't trim it off; just print the alias code. |
e22ea7cc RF |
3113 | print $OUT "$k\t$alias{$k}\n"; |
3114 | } | |
3115 | else { | |
3116 | ||
69893cff | 3117 | # No such, dude. |
e22ea7cc RF |
3118 | print "No alias for $k\n"; |
3119 | } | |
69893cff | 3120 | } ## end for my $k (@keys) |
e22ea7cc RF |
3121 | next CMD; |
3122 | }; | |
69893cff RGS |
3123 | |
3124 | =head4 C<source> - read commands from a file. | |
3125 | ||
3126 | Opens a lexical filehandle and stacks it on C<@cmdfhs>; C<DB::readline> will | |
3127 | pick it up. | |
3128 | ||
3129 | =cut | |
3130 | ||
e22ea7cc RF |
3131 | # source - read commands from a file (or pipe!) and execute. |
3132 | $cmd =~ /^source\s+(.*\S)/ && do { | |
3133 | if ( open my $fh, $1 ) { | |
3134 | ||
69893cff | 3135 | # Opened OK; stick it in the list of file handles. |
e22ea7cc RF |
3136 | push @cmdfhs, $fh; |
3137 | } | |
3138 | else { | |
3139 | ||
3140 | # Couldn't open it. | |
3141 | &warn("Can't execute `$1': $!\n"); | |
3142 | } | |
3143 | next CMD; | |
3144 | }; | |
69893cff RGS |
3145 | |
3146 | =head4 C<save> - send current history to a file | |
3147 | ||
3148 | Takes the complete history, (not the shrunken version you see with C<H>), | |
3149 | and saves it to the given filename, so it can be replayed using C<source>. | |
3150 | ||
3151 | Note that all C<^(save|source)>'s are commented out with a view to minimise recursion. | |
3152 | ||
3153 | =cut | |
3154 | ||
3155 | # save source - write commands to a file for later use | |
3156 | $cmd =~ /^save\s*(.*)$/ && do { | |
e22ea7cc RF |
3157 | my $file = $1 || '.perl5dbrc'; # default? |
3158 | if ( open my $fh, "> $file" ) { | |
3159 | ||
3160 | # chomp to remove extraneous newlines from source'd files | |
3161 | chomp( my @truelist = | |
3162 | map { m/^\s*(save|source)/ ? "#$_" : $_ } | |
3163 | @truehist ); | |
3164 | print $fh join( "\n", @truelist ); | |
69893cff | 3165 | print "commands saved in $file\n"; |
e22ea7cc RF |
3166 | } |
3167 | else { | |
69893cff RGS |
3168 | &warn("Can't save debugger commands in '$1': $!\n"); |
3169 | } | |
3170 | next CMD; | |
3171 | }; | |
3172 | ||
7fddc82f RF |
3173 | =head4 C<R> - restart |
3174 | ||
3175 | Restart the debugger session. | |
3176 | ||
3177 | =head4 C<rerun> - rerun the current session | |
3178 | ||
3179 | Return to any given position in the B<true>-history list | |
3180 | ||
3181 | =cut | |
3182 | ||
3183 | # R - restart execution. | |
3184 | # rerun - controlled restart execution. | |
3185 | $cmd =~ /^(R|rerun\s*(.*))$/ && do { | |
3186 | my @args = ($1 eq 'R' ? restart() : rerun($2)); | |
3187 | ||
ca28b541 AP |
3188 | # Close all non-system fds for a clean restart. A more |
3189 | # correct method would be to close all fds that were not | |
3190 | # open when the process started, but this seems to be | |
3191 | # hard. See "debugger 'R'estart and open database | |
3192 | # connections" on p5p. | |
3193 | ||
47d3bbda | 3194 | my $max_fd = 1024; # default if POSIX can't be loaded |
ca28b541 AP |
3195 | if (eval { require POSIX }) { |
3196 | $max_fd = POSIX::sysconf(POSIX::_SC_OPEN_MAX()); | |
3197 | } | |
3198 | ||
3199 | if (defined $max_fd) { | |
3200 | foreach ($^F+1 .. $max_fd-1) { | |
3201 | next unless open FD_TO_CLOSE, "<&=$_"; | |
3202 | close(FD_TO_CLOSE); | |
3203 | } | |
3204 | } | |
3205 | ||
7fddc82f RF |
3206 | # And run Perl again. We use exec() to keep the |
3207 | # PID stable (and that way $ini_pids is still valid). | |
3208 | exec(@args) || print $OUT "exec failed: $!\n"; | |
3209 | ||
3210 | last CMD; | |
3211 | }; | |
3212 | ||
69893cff RGS |
3213 | =head4 C<|, ||> - pipe output through the pager. |
3214 | ||
3215 | FOR C<|>, we save C<OUT> (the debugger's output filehandle) and C<STDOUT> | |
3216 | (the program's standard output). For C<||>, we only save C<OUT>. We open a | |
3217 | pipe to the pager (restoring the output filehandles if this fails). If this | |
3218 | is the C<|> command, we also set up a C<SIGPIPE> handler which will simply | |
3219 | set C<$signal>, sending us back into the debugger. | |
3220 | ||
3221 | We then trim off the pipe symbols and C<redo> the command loop at the | |
3222 | C<PIPE> label, causing us to evaluate the command in C<$cmd> without | |
3223 | reading another. | |
3224 | ||
3225 | =cut | |
3226 | ||
3227 | # || - run command in the pager, with output to DB::OUT. | |
e22ea7cc RF |
3228 | $cmd =~ /^\|\|?\s*[^|]/ && do { |
3229 | if ( $pager =~ /^\|/ ) { | |
3230 | ||
69893cff | 3231 | # Default pager is into a pipe. Redirect I/O. |
e22ea7cc RF |
3232 | open( SAVEOUT, ">&STDOUT" ) |
3233 | || &warn("Can't save STDOUT"); | |
3234 | open( STDOUT, ">&OUT" ) | |
3235 | || &warn("Can't redirect STDOUT"); | |
69893cff | 3236 | } ## end if ($pager =~ /^\|/) |
e22ea7cc RF |
3237 | else { |
3238 | ||
69893cff | 3239 | # Not into a pipe. STDOUT is safe. |
e22ea7cc RF |
3240 | open( SAVEOUT, ">&OUT" ) || &warn("Can't save DB::OUT"); |
3241 | } | |
69893cff RGS |
3242 | |
3243 | # Fix up environment to record we have less if so. | |
e22ea7cc RF |
3244 | fix_less(); |
3245 | ||
3246 | unless ( $piped = open( OUT, $pager ) ) { | |
69893cff | 3247 | |
69893cff | 3248 | # Couldn't open pipe to pager. |
e22ea7cc RF |
3249 | &warn("Can't pipe output to `$pager'"); |
3250 | if ( $pager =~ /^\|/ ) { | |
3251 | ||
69893cff | 3252 | # Redirect I/O back again. |
e22ea7cc RF |
3253 | open( OUT, ">&STDOUT" ) # XXX: lost message |
3254 | || &warn("Can't restore DB::OUT"); | |
3255 | open( STDOUT, ">&SAVEOUT" ) | |
3256 | || &warn("Can't restore STDOUT"); | |
3257 | close(SAVEOUT); | |
69893cff | 3258 | } ## end if ($pager =~ /^\|/) |
e22ea7cc RF |
3259 | else { |
3260 | ||
69893cff | 3261 | # Redirect I/O. STDOUT already safe. |
e22ea7cc RF |
3262 | open( OUT, ">&STDOUT" ) # XXX: lost message |
3263 | || &warn("Can't restore DB::OUT"); | |
3264 | } | |
3265 | next CMD; | |
69893cff RGS |
3266 | } ## end unless ($piped = open(OUT,... |
3267 | ||
3268 | # Set up broken-pipe handler if necessary. | |
e22ea7cc RF |
3269 | $SIG{PIPE} = \&DB::catch |
3270 | if $pager =~ /^\|/ | |
3271 | && ( "" eq $SIG{PIPE} || "DEFAULT" eq $SIG{PIPE} ); | |
69893cff RGS |
3272 | |
3273 | # Save current filehandle, unbuffer out, and put it back. | |
e22ea7cc RF |
3274 | $selected = select(OUT); |
3275 | $| = 1; | |
69893cff RGS |
3276 | |
3277 | # Don't put it back if pager was a pipe. | |
e22ea7cc | 3278 | select($selected), $selected = "" unless $cmd =~ /^\|\|/; |
69893cff RGS |
3279 | |
3280 | # Trim off the pipe symbols and run the command now. | |
e22ea7cc RF |
3281 | $cmd =~ s/^\|+\s*//; |
3282 | redo PIPE; | |
3283 | }; | |
69893cff RGS |
3284 | |
3285 | =head3 END OF COMMAND PARSING | |
3286 | ||
3287 | Anything left in C<$cmd> at this point is a Perl expression that we want to | |
3288 | evaluate. We'll always evaluate in the user's context, and fully qualify | |
3289 | any variables we might want to address in the C<DB> package. | |
3290 | ||
3291 | =cut | |
3292 | ||
3293 | # t - turn trace on. | |
e22ea7cc | 3294 | $cmd =~ s/^t\s/\$DB::trace |= 1;\n/; |
69893cff RGS |
3295 | |
3296 | # s - single-step. Remember the last command was 's'. | |
e22ea7cc | 3297 | $cmd =~ s/^s\s/\$DB::single = 1;\n/ && do { $laststep = 's' }; |
69893cff RGS |
3298 | |
3299 | # n - single-step, but not into subs. Remember last command | |
e22ea7cc RF |
3300 | # was 'n'. |
3301 | $cmd =~ s/^n\s/\$DB::single = 2;\n/ && do { $laststep = 'n' }; | |
69893cff | 3302 | |
e22ea7cc | 3303 | } # PIPE: |
69893cff | 3304 | |
e22ea7cc | 3305 | # Make sure the flag that says "the debugger's running" is |
69893cff | 3306 | # still on, to make sure we get control again. |
e22ea7cc | 3307 | $evalarg = "\$^D = \$^D | \$DB::db_stop;\n$cmd"; |
69893cff RGS |
3308 | |
3309 | # Run *our* eval that executes in the caller's context. | |
e22ea7cc | 3310 | &eval; |
69893cff RGS |
3311 | |
3312 | # Turn off the one-time-dump stuff now. | |
e22ea7cc RF |
3313 | if ($onetimeDump) { |
3314 | $onetimeDump = undef; | |
69893cff | 3315 | $onetimedumpDepth = undef; |
e22ea7cc RF |
3316 | } |
3317 | elsif ( $term_pid == $$ ) { | |
3318 | STDOUT->flush(); | |
3319 | STDERR->flush(); | |
3320 | ||
69893cff | 3321 | # XXX If this is the master pid, print a newline. |
e22ea7cc RF |
3322 | print $OUT "\n"; |
3323 | } | |
3324 | } ## end while (($term || &setterm... | |
69893cff RGS |
3325 | |
3326 | =head3 POST-COMMAND PROCESSING | |
3327 | ||
3328 | After each command, we check to see if the command output was piped anywhere. | |
3329 | If so, we go through the necessary code to unhook the pipe and go back to | |
3330 | our standard filehandles for input and output. | |
3331 | ||
3332 | =cut | |
3333 | ||
e22ea7cc | 3334 | continue { # CMD: |
69893cff RGS |
3335 | |
3336 | # At the end of every command: | |
e22ea7cc RF |
3337 | if ($piped) { |
3338 | ||
69893cff | 3339 | # Unhook the pipe mechanism now. |
e22ea7cc RF |
3340 | if ( $pager =~ /^\|/ ) { |
3341 | ||
69893cff | 3342 | # No error from the child. |
e22ea7cc | 3343 | $? = 0; |
69893cff | 3344 | |
e22ea7cc RF |
3345 | # we cannot warn here: the handle is missing --tchrist |
3346 | close(OUT) || print SAVEOUT "\nCan't close DB::OUT\n"; | |
69893cff | 3347 | |
e22ea7cc | 3348 | # most of the $? crud was coping with broken cshisms |
69893cff | 3349 | # $? is explicitly set to 0, so this never runs. |
e22ea7cc RF |
3350 | if ($?) { |
3351 | print SAVEOUT "Pager `$pager' failed: "; | |
3352 | if ( $? == -1 ) { | |
3353 | print SAVEOUT "shell returned -1\n"; | |
3354 | } | |
3355 | elsif ( $? >> 8 ) { | |
3356 | print SAVEOUT ( $? & 127 ) | |
3357 | ? " (SIG#" . ( $? & 127 ) . ")" | |
3358 | : "", ( $? & 128 ) ? " -- core dumped" : "", "\n"; | |
3359 | } | |
3360 | else { | |
3361 | print SAVEOUT "status ", ( $? >> 8 ), "\n"; | |
3362 | } | |
69893cff RGS |
3363 | } ## end if ($?) |
3364 | ||
e22ea7cc | 3365 | # Reopen filehandle for our output (if we can) and |
69893cff | 3366 | # restore STDOUT (if we can). |
e22ea7cc RF |
3367 | open( OUT, ">&STDOUT" ) || &warn("Can't restore DB::OUT"); |
3368 | open( STDOUT, ">&SAVEOUT" ) | |
3369 | || &warn("Can't restore STDOUT"); | |
69893cff RGS |
3370 | |
3371 | # Turn off pipe exception handler if necessary. | |
e22ea7cc | 3372 | $SIG{PIPE} = "DEFAULT" if $SIG{PIPE} eq \&DB::catch; |
69893cff | 3373 | |
e22ea7cc RF |
3374 | # Will stop ignoring SIGPIPE if done like nohup(1) |
3375 | # does SIGINT but Perl doesn't give us a choice. | |
69893cff | 3376 | } ## end if ($pager =~ /^\|/) |
e22ea7cc RF |
3377 | else { |
3378 | ||
69893cff | 3379 | # Non-piped "pager". Just restore STDOUT. |
e22ea7cc RF |
3380 | open( OUT, ">&SAVEOUT" ) || &warn("Can't restore DB::OUT"); |
3381 | } | |
69893cff RGS |
3382 | |
3383 | # Close filehandle pager was using, restore the normal one | |
3384 | # if necessary, | |
3385 | close(SAVEOUT); | |
e22ea7cc | 3386 | select($selected), $selected = "" unless $selected eq ""; |
69893cff RGS |
3387 | |
3388 | # No pipes now. | |
e22ea7cc | 3389 | $piped = ""; |
69893cff | 3390 | } ## end if ($piped) |
e22ea7cc | 3391 | } # CMD: |
69893cff RGS |
3392 | |
3393 | =head3 COMMAND LOOP TERMINATION | |
3394 | ||
3395 | When commands have finished executing, we come here. If the user closed the | |
3396 | input filehandle, we turn on C<$fall_off_end> to emulate a C<q> command. We | |
3397 | evaluate any post-prompt items. We restore C<$@>, C<$!>, C<$^E>, C<$,>, C<$/>, | |
3398 | C<$\>, and C<$^W>, and return a null list as expected by the Perl interpreter. | |
3399 | The interpreter will then execute the next line and then return control to us | |
3400 | again. | |
3401 | ||
3402 | =cut | |
3403 | ||
3404 | # No more commands? Quit. | |
e22ea7cc | 3405 | $fall_off_end = 1 unless defined $cmd; # Emulate `q' on EOF |
69893cff RGS |
3406 | |
3407 | # Evaluate post-prompt commands. | |
e22ea7cc RF |
3408 | foreach $evalarg (@$post) { |
3409 | &eval; | |
3410 | } | |
3411 | } # if ($single || $signal) | |
69893cff RGS |
3412 | |
3413 | # Put the user's globals back where you found them. | |
e22ea7cc | 3414 | ( $@, $!, $^E, $,, $/, $\, $^W ) = @saved; |
69893cff RGS |
3415 | (); |
3416 | } ## end sub DB | |
3417 | ||
3418 | # The following code may be executed now: | |
3419 | # BEGIN {warn 4} | |
3420 | ||
3421 | =head2 sub | |
3422 | ||
3423 | C<sub> is called whenever a subroutine call happens in the program being | |
3424 | debugged. The variable C<$DB::sub> contains the name of the subroutine | |
3425 | being called. | |
3426 | ||
3427 | The core function of this subroutine is to actually call the sub in the proper | |
3428 | context, capturing its output. This of course causes C<DB::DB> to get called | |
3429 | again, repeating until the subroutine ends and returns control to C<DB::sub> | |
3430 | again. Once control returns, C<DB::sub> figures out whether or not to dump the | |
3431 | return value, and returns its captured copy of the return value as its own | |
3432 | return value. The value then feeds back into the program being debugged as if | |
3433 | C<DB::sub> hadn't been there at all. | |
3434 | ||
3435 | C<sub> does all the work of printing the subroutine entry and exit messages | |
3436 | enabled by setting C<$frame>. It notes what sub the autoloader got called for, | |
3437 | and also prints the return value if needed (for the C<r> command and if | |
3438 | the 16 bit is set in C<$frame>). | |
3439 | ||
3440 | It also tracks the subroutine call depth by saving the current setting of | |
3441 | C<$single> in the C<@stack> package global; if this exceeds the value in | |
3442 | C<$deep>, C<sub> automatically turns on printing of the current depth by | |
3443 | setting the 4 bit in C<$single>. In any case, it keeps the current setting | |
3444 | of stop/don't stop on entry to subs set as it currently is set. | |
3445 | ||
3446 | =head3 C<caller()> support | |
3447 | ||
3448 | If C<caller()> is called from the package C<DB>, it provides some | |
3449 | additional data, in the following order: | |
3450 | ||
3451 | =over 4 | |
3452 | ||
3453 | =item * C<$package> | |
3454 | ||
3455 | The package name the sub was in | |
3456 | ||
3457 | =item * C<$filename> | |
3458 | ||
3459 | The filename it was defined in | |
3460 | ||
3461 | =item * C<$line> | |
3462 | ||
3463 | The line number it was defined on | |
3464 | ||
3465 | =item * C<$subroutine> | |
3466 | ||
3467 | The subroutine name; C<'(eval)'> if an C<eval>(). | |
3468 | ||
3469 | =item * C<$hasargs> | |
3470 | ||
3471 | 1 if it has arguments, 0 if not | |
3472 | ||
3473 | =item * C<$wantarray> | |
3474 | ||
3475 | 1 if array context, 0 if scalar context | |
3476 | ||
3477 | =item * C<$evaltext> | |
3478 | ||
3479 | The C<eval>() text, if any (undefined for C<eval BLOCK>) | |
3480 | ||
3481 | =item * C<$is_require> | |
3482 | ||
3483 | frame was created by a C<use> or C<require> statement | |
3484 | ||
3485 | =item * C<$hints> | |
3486 | ||
3487 | pragma information; subject to change between versions | |
3488 | ||
3489 | =item * C<$bitmask> | |
3490 | ||
3491 | pragma information: subject to change between versions | |
3492 | ||
3493 | =item * C<@DB::args> | |
3494 | ||
3495 | arguments with which the subroutine was invoked | |
3496 | ||
3497 | =back | |
3498 | ||
3499 | =cut | |
d338d6fe | 3500 | |
d12a4851 | 3501 | sub sub { |
69893cff RGS |
3502 | |
3503 | # Whether or not the autoloader was running, a scalar to put the | |
3504 | # sub's return value in (if needed), and an array to put the sub's | |
3505 | # return value in (if needed). | |
e22ea7cc | 3506 | my ( $al, $ret, @ret ) = ""; |
69893cff RGS |
3507 | |
3508 | # If the last ten characters are C'::AUTOLOAD', note we've traced | |
3509 | # into AUTOLOAD for $sub. | |
e22ea7cc RF |
3510 | if ( length($sub) > 10 && substr( $sub, -10, 10 ) eq '::AUTOLOAD' ) { |
3511 | $al = " for $$sub"; | |
d12a4851 | 3512 | } |
69893cff RGS |
3513 | |
3514 | # We stack the stack pointer and then increment it to protect us | |
3515 | # from a situation that might unwind a whole bunch of call frames | |
3516 | # at once. Localizing the stack pointer means that it will automatically | |
3517 | # unwind the same amount when multiple stack frames are unwound. | |
e22ea7cc | 3518 | local $stack_depth = $stack_depth + 1; # Protect from non-local exits |
69893cff RGS |
3519 | |
3520 | # Expand @stack. | |
d12a4851 | 3521 | $#stack = $stack_depth; |
69893cff RGS |
3522 | |
3523 | # Save current single-step setting. | |
d12a4851 | 3524 | $stack[-1] = $single; |
69893cff | 3525 | |
e22ea7cc | 3526 | # Turn off all flags except single-stepping. |
d12a4851 | 3527 | $single &= 1; |
69893cff RGS |
3528 | |
3529 | # If we've gotten really deeply recursed, turn on the flag that will | |
3530 | # make us stop with the 'deep recursion' message. | |
d12a4851 | 3531 | $single |= 4 if $stack_depth == $deep; |
69893cff RGS |
3532 | |
3533 | # If frame messages are on ... | |
3534 | ( | |
3535 | $frame & 4 # Extended frame entry message | |
e22ea7cc RF |
3536 | ? ( |
3537 | print_lineinfo( ' ' x ( $stack_depth - 1 ), "in " ), | |
69893cff | 3538 | |
e22ea7cc | 3539 | # Why -1? But it works! :-( |
69893cff RGS |
3540 | # Because print_trace will call add 1 to it and then call |
3541 | # dump_trace; this results in our skipping -1+1 = 0 stack frames | |
3542 | # in dump_trace. | |
e22ea7cc RF |
3543 | print_trace( $LINEINFO, -1, 1, 1, "$sub$al" ) |
3544 | ) | |
3545 | : print_lineinfo( ' ' x ( $stack_depth - 1 ), "entering $sub$al\n" ) | |
3546 | ||
69893cff | 3547 | # standard frame entry message |
e22ea7cc RF |
3548 | ) |
3549 | if $frame; | |
69893cff RGS |
3550 | |
3551 | # Determine the sub's return type,and capture approppriately. | |
d12a4851 | 3552 | if (wantarray) { |
e22ea7cc | 3553 | |
69893cff RGS |
3554 | # Called in array context. call sub and capture output. |
3555 | # DB::DB will recursively get control again if appropriate; we'll come | |
3556 | # back here when the sub is finished. | |
d12a4851 | 3557 | if ($assertion) { |
e22ea7cc RF |
3558 | $assertion = 0; |
3559 | eval { @ret = &$sub; }; | |
3560 | if ($@) { | |
3561 | print $OUT $@; | |
3562 | $signal = 1 unless $warnassertions; | |
3563 | } | |
3564 | } | |
3565 | else { | |
69893cff | 3566 | @ret = &$sub; |
69893cff | 3567 | } |
69893cff RGS |
3568 | |
3569 | # Pop the single-step value back off the stack. | |
e22ea7cc | 3570 | $single |= $stack[ $stack_depth-- ]; |
69893cff RGS |
3571 | |
3572 | # Check for exit trace messages... | |
e22ea7cc RF |
3573 | ( |
3574 | $frame & 4 # Extended exit message | |
3575 | ? ( | |
3576 | print_lineinfo( ' ' x $stack_depth, "out " ), | |
3577 | print_trace( $LINEINFO, -1, 1, 1, "$sub$al" ) | |
3578 | ) | |
3579 | : print_lineinfo( ' ' x $stack_depth, "exited $sub$al\n" ) | |
3580 | ||
69893cff | 3581 | # Standard exit message |
e22ea7cc RF |
3582 | ) |
3583 | if $frame & 2; | |
69893cff RGS |
3584 | |
3585 | # Print the return info if we need to. | |
e22ea7cc RF |
3586 | if ( $doret eq $stack_depth or $frame & 16 ) { |
3587 | ||
69893cff | 3588 | # Turn off output record separator. |
e22ea7cc RF |
3589 | local $\ = ''; |
3590 | my $fh = ( $doret eq $stack_depth ? $OUT : $LINEINFO ); | |
69893cff RGS |
3591 | |
3592 | # Indent if we're printing because of $frame tracing. | |
e22ea7cc | 3593 | print $fh ' ' x $stack_depth if $frame & 16; |
69893cff RGS |
3594 | |
3595 | # Print the return value. | |
e22ea7cc RF |
3596 | print $fh "list context return from $sub:\n"; |
3597 | dumpit( $fh, \@ret ); | |
69893cff RGS |
3598 | |
3599 | # And don't print it again. | |
e22ea7cc | 3600 | $doret = -2; |
69893cff | 3601 | } ## end if ($doret eq $stack_depth... |
e22ea7cc RF |
3602 | # And we have to return the return value now. |
3603 | @ret; | |
69893cff RGS |
3604 | } ## end if (wantarray) |
3605 | ||
3606 | # Scalar context. | |
3607 | else { | |
d12a4851 | 3608 | if ($assertion) { |
e22ea7cc RF |
3609 | $assertion = 0; |
3610 | eval { | |
3611 | ||
3612 | # Save the value if it's wanted at all. | |
3613 | $ret = &$sub; | |
3614 | }; | |
3615 | if ($@) { | |
3616 | print $OUT $@; | |
3617 | $signal = 1 unless $warnassertions; | |
3618 | } | |
3619 | $ret = undef unless defined wantarray; | |
69893cff | 3620 | } |
69893cff | 3621 | else { |
e22ea7cc RF |
3622 | if ( defined wantarray ) { |
3623 | ||
3624 | # Save the value if it's wanted at all. | |
3625 | $ret = &$sub; | |
3626 | } | |
3627 | else { | |
3628 | ||
3629 | # Void return, explicitly. | |
3630 | &$sub; | |
3631 | undef $ret; | |
3632 | } | |
3633 | } # if assertion | |
69893cff RGS |
3634 | |
3635 | # Pop the single-step value off the stack. | |
e22ea7cc | 3636 | $single |= $stack[ $stack_depth-- ]; |
69893cff RGS |
3637 | |
3638 | # If we're doing exit messages... | |
e22ea7cc RF |
3639 | ( |
3640 | $frame & 4 # Extended messsages | |
3641 | ? ( | |
3642 | print_lineinfo( ' ' x $stack_depth, "out " ), | |
3643 | print_trace( $LINEINFO, -1, 1, 1, "$sub$al" ) | |
3644 | ) | |
3645 | : print_lineinfo( ' ' x $stack_depth, "exited $sub$al\n" ) | |
3646 | ||
3647 | # Standard messages | |
3648 | ) | |
3649 | if $frame & 2; | |
69893cff RGS |
3650 | |
3651 | # If we are supposed to show the return value... same as before. | |
e22ea7cc RF |
3652 | if ( $doret eq $stack_depth or $frame & 16 and defined wantarray ) { |
3653 | local $\ = ''; | |
3654 | my $fh = ( $doret eq $stack_depth ? $OUT : $LINEINFO ); | |
3655 | print $fh ( ' ' x $stack_depth ) if $frame & 16; | |
3656 | print $fh ( | |
3657 | defined wantarray | |
3658 | ? "scalar context return from $sub: " | |
3659 | : "void context return from $sub\n" | |
3660 | ); | |
3661 | dumpit( $fh, $ret ) if defined wantarray; | |
3662 | $doret = -2; | |
69893cff RGS |
3663 | } ## end if ($doret eq $stack_depth... |
3664 | ||
3665 | # Return the appropriate scalar value. | |
e22ea7cc | 3666 | $ret; |
69893cff RGS |
3667 | } ## end else [ if (wantarray) |
3668 | } ## end sub sub | |
3669 | ||
3670 | =head1 EXTENDED COMMAND HANDLING AND THE COMMAND API | |
3671 | ||
3672 | In Perl 5.8.0, there was a major realignment of the commands and what they did, | |
3673 | Most of the changes were to systematize the command structure and to eliminate | |
3674 | commands that threw away user input without checking. | |
3675 | ||
3676 | The following sections describe the code added to make it easy to support | |
3677 | multiple command sets with conflicting command names. This section is a start | |
3678 | at unifying all command processing to make it simpler to develop commands. | |
3679 | ||
3680 | Note that all the cmd_[a-zA-Z] subroutines require the command name, a line | |
3681 | number, and C<$dbline> (the current line) as arguments. | |
3682 | ||
3683 | Support functions in this section which have multiple modes of failure C<die> | |
3684 | on error; the rest simply return a false value. | |
3685 | ||
3686 | The user-interface functions (all of the C<cmd_*> functions) just output | |
3687 | error messages. | |
3688 | ||
3689 | =head2 C<%set> | |
3690 | ||
3691 | The C<%set> hash defines the mapping from command letter to subroutine | |
3692 | name suffix. | |
3693 | ||
3694 | C<%set> is a two-level hash, indexed by set name and then by command name. | |
3695 | Note that trying to set the CommandSet to 'foobar' simply results in the | |
3696 | 5.8.0 command set being used, since there's no top-level entry for 'foobar'. | |
3697 | ||
3698 | =cut | |
d338d6fe | 3699 | |
d12a4851 | 3700 | ### The API section |
f1583d8f | 3701 | |
e22ea7cc RF |
3702 | my %set = ( # |
3703 | 'pre580' => { | |
3704 | 'a' => 'pre580_a', | |
3705 | 'A' => 'pre580_null', | |
3706 | 'b' => 'pre580_b', | |
3707 | 'B' => 'pre580_null', | |
3708 | 'd' => 'pre580_null', | |
3709 | 'D' => 'pre580_D', | |
3710 | 'h' => 'pre580_h', | |
3711 | 'M' => 'pre580_null', | |
3712 | 'O' => 'o', | |
3713 | 'o' => 'pre580_null', | |
3714 | 'v' => 'M', | |
3715 | 'w' => 'v', | |
3716 | 'W' => 'pre580_W', | |
69893cff | 3717 | }, |
e22ea7cc RF |
3718 | 'pre590' => { |
3719 | '<' => 'pre590_prepost', | |
3720 | '<<' => 'pre590_prepost', | |
3721 | '>' => 'pre590_prepost', | |
3722 | '>>' => 'pre590_prepost', | |
3723 | '{' => 'pre590_prepost', | |
3724 | '{{' => 'pre590_prepost', | |
69893cff | 3725 | }, |
d12a4851 | 3726 | ); |
492652be | 3727 | |
69893cff RGS |
3728 | =head2 C<cmd_wrapper()> (API) |
3729 | ||
3730 | C<cmd_wrapper()> allows the debugger to switch command sets | |
3731 | depending on the value of the C<CommandSet> option. | |
3732 | ||
3733 | It tries to look up the command in the X<C<%set>> package-level I<lexical> | |
3734 | (which means external entities can't fiddle with it) and create the name of | |
3735 | the sub to call based on the value found in the hash (if it's there). I<All> | |
3736 | of the commands to be handled in a set have to be added to C<%set>; if they | |
3737 | aren't found, the 5.8.0 equivalent is called (if there is one). | |
3738 | ||
3739 | This code uses symbolic references. | |
3740 | ||
3741 | =cut | |
3742 | ||
d12a4851 | 3743 | sub cmd_wrapper { |
69893cff RGS |
3744 | my $cmd = shift; |
3745 | my $line = shift; | |
3746 | my $dblineno = shift; | |
3747 | ||
e22ea7cc | 3748 | # Assemble the command subroutine's name by looking up the |
69893cff RGS |
3749 | # command set and command name in %set. If we can't find it, |
3750 | # default to the older version of the command. | |
3751 | my $call = 'cmd_' | |
e22ea7cc RF |
3752 | . ( $set{$CommandSet}{$cmd} |
3753 | || ( $cmd =~ /^[<>{]+/o ? 'prepost' : $cmd ) ); | |
69893cff RGS |
3754 | |
3755 | # Call the command subroutine, call it by name. | |
e22ea7cc RF |
3756 | return &$call( $cmd, $line, $dblineno ); |
3757 | } ## end sub cmd_wrapper | |
492652be | 3758 | |
69893cff RGS |
3759 | =head3 C<cmd_a> (command) |
3760 | ||
3761 | The C<a> command handles pre-execution actions. These are associated with a | |
3762 | particular line, so they're stored in C<%dbline>. We default to the current | |
3763 | line if none is specified. | |
3764 | ||
3765 | =cut | |
3766 | ||
d12a4851 | 3767 | sub cmd_a { |
e22ea7cc RF |
3768 | my $cmd = shift; |
3769 | my $line = shift || ''; # [.|line] expr | |
3770 | my $dbline = shift; | |
69893cff RGS |
3771 | |
3772 | # If it's dot (here), or not all digits, use the current line. | |
3773 | $line =~ s/^(\.|(?:[^\d]))/$dbline/; | |
3774 | ||
e22ea7cc RF |
3775 | # Should be a line number followed by an expression. |