Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2183 lines
67 KiB

  1. package DB;
  2. # Debugger for Perl 5.00x; perl5db.pl patch level:
  3. $VERSION = 1.0402;
  4. $header = "perl5db.pl version $VERSION";
  5. # Enhanced by [email protected] (Ilya Zakharevich)
  6. # Latest version available: ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl
  7. # modified Perl debugger, to be run from Emacs in perldb-mode
  8. # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
  9. # Johan Vromans -- upgrade to 4.0 pl 10
  10. # Ilya Zakharevich -- patches after 5.001 (and some before ;-)
  11. #
  12. # This file is automatically included if you do perl -d.
  13. # It's probably not useful to include this yourself.
  14. #
  15. # Perl supplies the values for %sub. It effectively inserts
  16. # a &DB'DB(); in front of every place that can have a
  17. # breakpoint. Instead of a subroutine call it calls &DB::sub with
  18. # $DB::sub being the called subroutine. It also inserts a BEGIN
  19. # {require 'perl5db.pl'} before the first line.
  20. #
  21. # After each `require'd file is compiled, but before it is executed, a
  22. # call to DB::postponed($main::{'_<'.$filename}) is emulated. Here the
  23. # $filename is the expanded name of the `require'd file (as found as
  24. # value of %INC).
  25. #
  26. # Additional services from Perl interpreter:
  27. #
  28. # if caller() is called from the package DB, it provides some
  29. # additional data.
  30. #
  31. # The array @{$main::{'_<'.$filename} is the line-by-line contents of
  32. # $filename.
  33. #
  34. # The hash %{'_<'.$filename} contains breakpoints and action (it is
  35. # keyed by line number), and individual entries are settable (as
  36. # opposed to the whole hash). Only true/false is important to the
  37. # interpreter, though the values used by perl5db.pl have the form
  38. # "$break_condition\0$action". Values are magical in numeric context.
  39. #
  40. # The scalar ${'_<'.$filename} contains "_<$filename".
  41. #
  42. # Note that no subroutine call is possible until &DB::sub is defined
  43. # (for subroutines defined outside of the package DB). In fact the same is
  44. # true if $deep is not defined.
  45. #
  46. # $Log: perldb.pl,v $
  47. #
  48. # At start reads $rcfile that may set important options. This file
  49. # may define a subroutine &afterinit that will be executed after the
  50. # debugger is initialized.
  51. #
  52. # After $rcfile is read reads environment variable PERLDB_OPTS and parses
  53. # it as a rest of `O ...' line in debugger prompt.
  54. #
  55. # The options that can be specified only at startup:
  56. # [To set in $rcfile, call &parse_options("optionName=new_value").]
  57. #
  58. # TTY - the TTY to use for debugging i/o.
  59. #
  60. # noTTY - if set, goes in NonStop mode. On interrupt if TTY is not set
  61. # uses the value of noTTY or "/tmp/perldbtty$$" to find TTY using
  62. # Term::Rendezvous. Current variant is to have the name of TTY in this
  63. # file.
  64. #
  65. # ReadLine - If false, dummy ReadLine is used, so you can debug
  66. # ReadLine applications.
  67. #
  68. # NonStop - if true, no i/o is performed until interrupt.
  69. #
  70. # LineInfo - file or pipe to print line number info to. If it is a
  71. # pipe, a short "emacs like" message is used.
  72. #
  73. # Example $rcfile: (delete leading hashes!)
  74. #
  75. # &parse_options("NonStop=1 LineInfo=db.out");
  76. # sub afterinit { $trace = 1; }
  77. #
  78. # The script will run without human intervention, putting trace
  79. # information into db.out. (If you interrupt it, you would better
  80. # reset LineInfo to something "interactive"!)
  81. #
  82. ##################################################################
  83. # Changelog:
  84. # A lot of things changed after 0.94. First of all, core now informs
  85. # debugger about entry into XSUBs, overloaded operators, tied operations,
  86. # BEGIN and END. Handy with `O f=2'.
  87. # This can make debugger a little bit too verbose, please be patient
  88. # and report your problems promptly.
  89. # Now the option frame has 3 values: 0,1,2.
  90. # Note that if DESTROY returns a reference to the object (or object),
  91. # the deletion of data may be postponed until the next function call,
  92. # due to the need to examine the return value.
  93. # Changes: 0.95: `v' command shows versions.
  94. # Changes: 0.96: `v' command shows version of readline.
  95. # primitive completion works (dynamic variables, subs for `b' and `l',
  96. # options). Can `p %var'
  97. # Better help (`h <' now works). New commands <<, >>, {, {{.
  98. # {dump|print}_trace() coded (to be able to do it from <<cmd).
  99. # `c sub' documented.
  100. # At last enough magic combined to stop after the end of debuggee.
  101. # !! should work now (thanks to Emacs bracket matching an extra
  102. # `]' in a regexp is caught).
  103. # `L', `D' and `A' span files now (as documented).
  104. # Breakpoints in `require'd code are possible (used in `R').
  105. # Some additional words on internal work of debugger.
  106. # `b load filename' implemented.
  107. # `b postpone subr' implemented.
  108. # now only `q' exits debugger (overwriteable on $inhibit_exit).
  109. # When restarting debugger breakpoints/actions persist.
  110. # Buglet: When restarting debugger only one breakpoint/action per
  111. # autoloaded function persists.
  112. # Changes: 0.97: NonStop will not stop in at_exit().
  113. # Option AutoTrace implemented.
  114. # Trace printed differently if frames are printed too.
  115. # new `inhibitExit' option.
  116. # printing of a very long statement interruptible.
  117. # Changes: 0.98: New command `m' for printing possible methods
  118. # 'l -' is a synonim for `-'.
  119. # Cosmetic bugs in printing stack trace.
  120. # `frame' & 8 to print "expanded args" in stack trace.
  121. # Can list/break in imported subs.
  122. # new `maxTraceLen' option.
  123. # frame & 4 and frame & 8 granted.
  124. # new command `m'
  125. # nonstoppable lines do not have `:' near the line number.
  126. # `b compile subname' implemented.
  127. # Will not use $` any more.
  128. # `-' behaves sane now.
  129. # Changes: 0.99: Completion for `f', `m'.
  130. # `m' will remove duplicate names instead of duplicate functions.
  131. # `b load' strips trailing whitespace.
  132. # completion ignores leading `|'; takes into account current package
  133. # when completing a subroutine name (same for `l').
  134. ####################################################################
  135. # Needed for the statement after exec():
  136. BEGIN { $ini_warn = $^W; $^W = 0 } # Switch compilation warnings off until another BEGIN.
  137. local($^W) = 0; # Switch run-time warnings off during init.
  138. warn ( # Do not ;-)
  139. $dumpvar::hashDepth,
  140. $dumpvar::arrayDepth,
  141. $dumpvar::dumpDBFiles,
  142. $dumpvar::dumpPackages,
  143. $dumpvar::quoteHighBit,
  144. $dumpvar::printUndef,
  145. $dumpvar::globPrint,
  146. $dumpvar::usageOnly,
  147. @ARGS,
  148. $Carp::CarpLevel,
  149. $panic,
  150. $second_time,
  151. ) if 0;
  152. # Command-line + PERLLIB:
  153. @ini_INC = @INC;
  154. # $prevwarn = $prevdie = $prevbus = $prevsegv = ''; # Does not help?!
  155. $trace = $signal = $single = 0; # Uninitialized warning suppression
  156. # (local $^W cannot help - other packages!).
  157. $inhibit_exit = $option{PrintRet} = 1;
  158. @options = qw(hashDepth arrayDepth DumpDBFiles DumpPackages DumpReused
  159. compactDump veryCompact quote HighBit undefPrint
  160. globPrint PrintRet UsageOnly frame AutoTrace
  161. TTY noTTY ReadLine NonStop LineInfo maxTraceLen
  162. recallCommand ShellBang pager tkRunning ornaments
  163. signalLevel warnLevel dieLevel inhibit_exit
  164. ImmediateStop bareStringify);
  165. %optionVars = (
  166. hashDepth => \$dumpvar::hashDepth,
  167. arrayDepth => \$dumpvar::arrayDepth,
  168. DumpDBFiles => \$dumpvar::dumpDBFiles,
  169. DumpPackages => \$dumpvar::dumpPackages,
  170. DumpReused => \$dumpvar::dumpReused,
  171. HighBit => \$dumpvar::quoteHighBit,
  172. undefPrint => \$dumpvar::printUndef,
  173. globPrint => \$dumpvar::globPrint,
  174. UsageOnly => \$dumpvar::usageOnly,
  175. bareStringify => \$dumpvar::bareStringify,
  176. frame => \$frame,
  177. AutoTrace => \$trace,
  178. inhibit_exit => \$inhibit_exit,
  179. maxTraceLen => \$maxtrace,
  180. ImmediateStop => \$ImmediateStop,
  181. );
  182. %optionAction = (
  183. compactDump => \&dumpvar::compactDump,
  184. veryCompact => \&dumpvar::veryCompact,
  185. quote => \&dumpvar::quote,
  186. TTY => \&TTY,
  187. noTTY => \&noTTY,
  188. ReadLine => \&ReadLine,
  189. NonStop => \&NonStop,
  190. LineInfo => \&LineInfo,
  191. recallCommand => \&recallCommand,
  192. ShellBang => \&shellBang,
  193. pager => \&pager,
  194. signalLevel => \&signalLevel,
  195. warnLevel => \&warnLevel,
  196. dieLevel => \&dieLevel,
  197. tkRunning => \&tkRunning,
  198. ornaments => \&ornaments,
  199. );
  200. %optionRequire = (
  201. compactDump => 'dumpvar.pl',
  202. veryCompact => 'dumpvar.pl',
  203. quote => 'dumpvar.pl',
  204. );
  205. # These guys may be defined in $ENV{PERL5DB} :
  206. $rl = 1 unless defined $rl;
  207. $warnLevel = 1 unless defined $warnLevel;
  208. $dieLevel = 1 unless defined $dieLevel;
  209. $signalLevel = 1 unless defined $signalLevel;
  210. $pre = [] unless defined $pre;
  211. $post = [] unless defined $post;
  212. $pretype = [] unless defined $pretype;
  213. warnLevel($warnLevel);
  214. dieLevel($dieLevel);
  215. signalLevel($signalLevel);
  216. &pager((defined($ENV{PAGER})
  217. ? $ENV{PAGER}
  218. : ($^O eq 'os2'
  219. ? 'cmd /c more'
  220. : 'more'))) unless defined $pager;
  221. &recallCommand("!") unless defined $prc;
  222. &shellBang("!") unless defined $psh;
  223. $maxtrace = 400 unless defined $maxtrace;
  224. if (-e "/dev/tty") {
  225. $rcfile=".perldb";
  226. } else {
  227. $rcfile="perldb.ini";
  228. }
  229. if (-f $rcfile) {
  230. do "./$rcfile";
  231. } elsif (defined $ENV{LOGDIR} and -f "$ENV{LOGDIR}/$rcfile") {
  232. do "$ENV{LOGDIR}/$rcfile";
  233. } elsif (defined $ENV{HOME} and -f "$ENV{HOME}/$rcfile") {
  234. do "$ENV{HOME}/$rcfile";
  235. }
  236. if (defined $ENV{PERLDB_OPTS}) {
  237. parse_options($ENV{PERLDB_OPTS});
  238. }
  239. if (exists $ENV{PERLDB_RESTART}) {
  240. delete $ENV{PERLDB_RESTART};
  241. # $restart = 1;
  242. @hist = get_list('PERLDB_HIST');
  243. %break_on_load = get_list("PERLDB_ON_LOAD");
  244. %postponed = get_list("PERLDB_POSTPONE");
  245. my @had_breakpoints= get_list("PERLDB_VISITED");
  246. for (0 .. $#had_breakpoints) {
  247. my %pf = get_list("PERLDB_FILE_$_");
  248. $postponed_file{$had_breakpoints[$_]} = \%pf if %pf;
  249. }
  250. my %opt = get_list("PERLDB_OPT");
  251. my ($opt,$val);
  252. while (($opt,$val) = each %opt) {
  253. $val =~ s/[\\\']/\\$1/g;
  254. parse_options("$opt'$val'");
  255. }
  256. @INC = get_list("PERLDB_INC");
  257. @ini_INC = @INC;
  258. $pretype = [get_list("PERLDB_PRETYPE")];
  259. $pre = [get_list("PERLDB_PRE")];
  260. $post = [get_list("PERLDB_POST")];
  261. @typeahead = get_list("PERLDB_TYPEAHEAD", @typeahead);
  262. }
  263. if ($notty) {
  264. $runnonstop = 1;
  265. } else {
  266. # Is Perl being run from Emacs?
  267. $emacs = ((defined $main::ARGV[0]) and ($main::ARGV[0] eq '-emacs'));
  268. $rl = 0, shift(@main::ARGV) if $emacs;
  269. #require Term::ReadLine;
  270. if (-e "/dev/tty") {
  271. $console = "/dev/tty";
  272. } elsif ($^O eq 'dos' or -e "con" or $^O eq 'MSWin32') {
  273. $console = "con";
  274. } else {
  275. $console = "sys\$command";
  276. }
  277. if (($^O eq 'MSWin32') and ($emacs or defined $ENV{EMACS})) {
  278. $console = undef;
  279. }
  280. # Around a bug:
  281. if (defined $ENV{OS2_SHELL} and ($emacs or $ENV{WINDOWID})) { # In OS/2
  282. $console = undef;
  283. }
  284. $console = $tty if defined $tty;
  285. if (defined $console) {
  286. open(IN,"+<$console") || open(IN,"<$console") || open(IN,"<&STDIN");
  287. open(OUT,"+>$console") || open(OUT,">$console") || open(OUT,">&STDERR")
  288. || open(OUT,">&STDOUT"); # so we don't dongle stdout
  289. } else {
  290. open(IN,"<&STDIN");
  291. open(OUT,">&STDERR") || open(OUT,">&STDOUT"); # so we don't dongle stdout
  292. $console = 'STDIN/OUT';
  293. }
  294. # so open("|more") can read from STDOUT and so we don't dingle stdin
  295. $IN = \*IN;
  296. $OUT = \*OUT;
  297. select($OUT);
  298. $| = 1; # for DB::OUT
  299. select(STDOUT);
  300. $LINEINFO = $OUT unless defined $LINEINFO;
  301. $lineinfo = $console unless defined $lineinfo;
  302. $| = 1; # for real STDOUT
  303. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  304. unless ($runnonstop) {
  305. print $OUT "\nLoading DB routines from $header\n";
  306. print $OUT ("Emacs support ",
  307. $emacs ? "enabled" : "available",
  308. ".\n");
  309. print $OUT "\nEnter h or `h h' for help.\n\n";
  310. }
  311. }
  312. @ARGS = @ARGV;
  313. for (@args) {
  314. s/\'/\\\'/g;
  315. s/(.*)/'$1'/ unless /^-?[\d.]+$/;
  316. }
  317. if (defined &afterinit) { # May be defined in $rcfile
  318. &afterinit();
  319. }
  320. $I_m_init = 1;
  321. ############################################################ Subroutines
  322. sub DB {
  323. # _After_ the perl program is compiled, $single is set to 1:
  324. if ($single and not $second_time++) {
  325. if ($runnonstop) { # Disable until signal
  326. for ($i=0; $i <= $stack_depth; ) {
  327. $stack[$i++] &= ~1;
  328. }
  329. $single = 0;
  330. # return; # Would not print trace!
  331. } elsif ($ImmediateStop) {
  332. $ImmediateStop = 0;
  333. $signal = 1;
  334. }
  335. }
  336. $runnonstop = 0 if $single or $signal; # Disable it if interactive.
  337. &save;
  338. ($package, $filename, $line) = caller;
  339. $filename_ini = $filename;
  340. $usercontext = '($@, $!, $^E, $,, $/, $\, $^W) = @saved;' .
  341. "package $package;"; # this won't let them modify, alas
  342. local(*dbline) = $main::{'_<' . $filename};
  343. $max = $#dbline;
  344. if (($stop,$action) = split(/\0/,$dbline{$line})) {
  345. if ($stop eq '1') {
  346. $signal |= 1;
  347. } elsif ($stop) {
  348. $evalarg = "\$DB::signal |= 1 if do {$stop}"; &eval;
  349. $dbline{$line} =~ s/;9($|\0)/$1/;
  350. }
  351. }
  352. my $was_signal = $signal;
  353. if ($trace & 2) {
  354. for (my $n = 0; $n <= $#to_watch; $n++) {
  355. $evalarg = $to_watch[$n];
  356. local $onetimeDump; # Do not output results
  357. my ($val) = &eval; # Fix context (&eval is doing array)?
  358. $val = ( (defined $val) ? "'$val'" : 'undef' );
  359. if ($val ne $old_watch[$n]) {
  360. $signal = 1;
  361. print $OUT <<EOP;
  362. Watchpoint $n:\t$to_watch[$n] changed:
  363. old value:\t$old_watch[$n]
  364. new value:\t$val
  365. EOP
  366. $old_watch[$n] = $val;
  367. }
  368. }
  369. }
  370. if ($trace & 4) { # User-installed watch
  371. return if watchfunction($package, $filename, $line)
  372. and not $single and not $was_signal and not ($trace & ~4);
  373. }
  374. $was_signal = $signal;
  375. $signal = 0;
  376. if ($single || ($trace & 1) || $was_signal) {
  377. if ($emacs) {
  378. $position = "\032\032$filename:$line:0\n";
  379. print $LINEINFO $position;
  380. } elsif ($package eq 'DB::fake') {
  381. $term || &setterm;
  382. print_help(<<EOP);
  383. Debugged program terminated. Use B<q> to quit or B<R> to restart,
  384. use B<O> I<inhibit_exit> to avoid stopping after program termination,
  385. B<h q>, B<h R> or B<h O> to get additional info.
  386. EOP
  387. $package = 'main';
  388. $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
  389. "package $package;"; # this won't let them modify, alas
  390. } else {
  391. $sub =~ s/\'/::/;
  392. $prefix = $sub =~ /::/ ? "" : "${'package'}::";
  393. $prefix .= "$sub($filename:";
  394. $after = ($dbline[$line] =~ /\n$/ ? '' : "\n");
  395. if (length($prefix) > 30) {
  396. $position = "$prefix$line):\n$line:\t$dbline[$line]$after";
  397. $prefix = "";
  398. $infix = ":\t";
  399. } else {
  400. $infix = "):\t";
  401. $position = "$prefix$line$infix$dbline[$line]$after";
  402. }
  403. if ($frame) {
  404. print $LINEINFO ' ' x $stack_depth, "$line:\t$dbline[$line]$after";
  405. } else {
  406. print $LINEINFO $position;
  407. }
  408. for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) { #{ vi
  409. last if $dbline[$i] =~ /^\s*[\;\}\#\n]/;
  410. last if $signal;
  411. $after = ($dbline[$i] =~ /\n$/ ? '' : "\n");
  412. $incr_pos = "$prefix$i$infix$dbline[$i]$after";
  413. $position .= $incr_pos;
  414. if ($frame) {
  415. print $LINEINFO ' ' x $stack_depth, "$i:\t$dbline[$i]$after";
  416. } else {
  417. print $LINEINFO $incr_pos;
  418. }
  419. }
  420. }
  421. }
  422. $evalarg = $action, &eval if $action;
  423. if ($single || $was_signal) {
  424. local $level = $level + 1;
  425. foreach $evalarg (@$pre) {
  426. &eval;
  427. }
  428. print $OUT $stack_depth . " levels deep in subroutine calls!\n"
  429. if $single & 4;
  430. $start = $line;
  431. $incr = -1; # for backward motion.
  432. @typeahead = @$pretype, @typeahead;
  433. CMD:
  434. while (($term || &setterm),
  435. ($term_pid == $$ or &resetterm),
  436. defined ($cmd=&readline(" DB" . ('<' x $level) .
  437. ($#hist+1) . ('>' x $level) .
  438. " "))) {
  439. $single = 0;
  440. $signal = 0;
  441. $cmd =~ s/\\$/\n/ && do {
  442. $cmd .= &readline(" cont: ");
  443. redo CMD;
  444. };
  445. $cmd =~ /^$/ && ($cmd = $laststep);
  446. push(@hist,$cmd) if length($cmd) > 1;
  447. PIPE: {
  448. ($i) = split(/\s+/,$cmd);
  449. eval "\$cmd =~ $alias{$i}", print $OUT $@ if $alias{$i};
  450. $cmd =~ /^q$/ && ($exiting = 1) && exit 0;
  451. $cmd =~ /^h$/ && do {
  452. print_help($help);
  453. next CMD; };
  454. $cmd =~ /^h\s+h$/ && do {
  455. print_help($summary);
  456. next CMD; };
  457. $cmd =~ /^h\s+(\S)$/ && do {
  458. my $asked = "\Q$1";
  459. if ($help =~ /^(?:[IB]<)$asked/m) {
  460. while ($help =~ /^((?:[IB]<)$asked([\s\S]*?)\n)(?!\s)/mg) {
  461. print_help($1);
  462. }
  463. } else {
  464. print_help("B<$asked> is not a debugger command.\n");
  465. }
  466. next CMD; };
  467. $cmd =~ /^t$/ && do {
  468. ($trace & 1) ? ($trace &= ~1) : ($trace |= 1);
  469. print $OUT "Trace = " .
  470. (($trace & 1) ? "on" : "off" ) . "\n";
  471. next CMD; };
  472. $cmd =~ /^S(\s+(!)?(.+))?$/ && do {
  473. $Srev = defined $2; $Spatt = $3; $Snocheck = ! defined $1;
  474. foreach $subname (sort(keys %sub)) {
  475. if ($Snocheck or $Srev^($subname =~ /$Spatt/)) {
  476. print $OUT $subname,"\n";
  477. }
  478. }
  479. next CMD; };
  480. $cmd =~ /^v$/ && do {
  481. list_versions(); next CMD};
  482. $cmd =~ s/^X\b/V $package/;
  483. $cmd =~ /^V$/ && do {
  484. $cmd = "V $package"; };
  485. $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
  486. local ($savout) = select($OUT);
  487. $packname = $1;
  488. @vars = split(' ',$2);
  489. do 'dumpvar.pl' unless defined &main::dumpvar;
  490. if (defined &main::dumpvar) {
  491. local $frame = 0;
  492. local $doret = -2;
  493. &main::dumpvar($packname,@vars);
  494. } else {
  495. print $OUT "dumpvar.pl not available.\n";
  496. }
  497. select ($savout);
  498. next CMD; };
  499. $cmd =~ s/^x\b/ / && do { # So that will be evaled
  500. $onetimeDump = 'dump'; };
  501. $cmd =~ s/^m\s+([\w:]+)\s*$/ / && do {
  502. methods($1); next CMD};
  503. $cmd =~ s/^m\b/ / && do { # So this will be evaled
  504. $onetimeDump = 'methods'; };
  505. $cmd =~ /^f\b\s*(.*)/ && do {
  506. $file = $1;
  507. $file =~ s/\s+$//;
  508. if (!$file) {
  509. print $OUT "The old f command is now the r command.\n";
  510. print $OUT "The new f command switches filenames.\n";
  511. next CMD;
  512. }
  513. if (!defined $main::{'_<' . $file}) {
  514. if (($try) = grep(m#^_<.*$file#, keys %main::)) {{
  515. $try = substr($try,2);
  516. print $OUT "Choosing $try matching `$file':\n";
  517. $file = $try;
  518. }}
  519. }
  520. if (!defined $main::{'_<' . $file}) {
  521. print $OUT "No file matching `$file' is loaded.\n";
  522. next CMD;
  523. } elsif ($file ne $filename) {
  524. *dbline = $main::{'_<' . $file};
  525. $max = $#dbline;
  526. $filename = $file;
  527. $start = 1;
  528. $cmd = "l";
  529. } else {
  530. print $OUT "Already in $file.\n";
  531. next CMD;
  532. }
  533. };
  534. $cmd =~ s/^l\s+-\s*$/-/;
  535. $cmd =~ /^l\b\s*([\':A-Za-z_][\':\w]*)/ && do {
  536. $subname = $1;
  537. $subname =~ s/\'/::/;
  538. $subname = $package."::".$subname
  539. unless $subname =~ /::/;
  540. $subname = "main".$subname if substr($subname,0,2) eq "::";
  541. @pieces = split(/:/,find_sub($subname));
  542. $subrange = pop @pieces;
  543. $file = join(':', @pieces);
  544. if ($file ne $filename) {
  545. *dbline = $main::{'_<' . $file};
  546. $max = $#dbline;
  547. $filename = $file;
  548. }
  549. if ($subrange) {
  550. if (eval($subrange) < -$window) {
  551. $subrange =~ s/-.*/+/;
  552. }
  553. $cmd = "l $subrange";
  554. } else {
  555. print $OUT "Subroutine $subname not found.\n";
  556. next CMD;
  557. } };
  558. $cmd =~ /^\.$/ && do {
  559. $incr = -1; # for backward motion.
  560. $start = $line;
  561. $filename = $filename_ini;
  562. *dbline = $main::{'_<' . $filename};
  563. $max = $#dbline;
  564. print $LINEINFO $position;
  565. next CMD };
  566. $cmd =~ /^w\b\s*(\d*)$/ && do {
  567. $incr = $window - 1;
  568. $start = $1 if $1;
  569. $start -= $preview;
  570. #print $OUT 'l ' . $start . '-' . ($start + $incr);
  571. $cmd = 'l ' . $start . '-' . ($start + $incr); };
  572. $cmd =~ /^-$/ && do {
  573. $start -= $incr + $window + 1;
  574. $start = 1 if $start <= 0;
  575. $incr = $window - 1;
  576. $cmd = 'l ' . ($start) . '+'; };
  577. $cmd =~ /^l$/ && do {
  578. $incr = $window - 1;
  579. $cmd = 'l ' . $start . '-' . ($start + $incr); };
  580. $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
  581. $start = $1 if $1;
  582. $incr = $2;
  583. $incr = $window - 1 unless $incr;
  584. $cmd = 'l ' . $start . '-' . ($start + $incr); };
  585. $cmd =~ /^l\b\s*((-?[\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
  586. $end = (!defined $2) ? $max : ($4 ? $4 : $2);
  587. $end = $max if $end > $max;
  588. $i = $2;
  589. $i = $line if $i eq '.';
  590. $i = 1 if $i < 1;
  591. $incr = $end - $i;
  592. if ($emacs) {
  593. print $OUT "\032\032$filename:$i:0\n";
  594. $i = $end;
  595. } else {
  596. for (; $i <= $end; $i++) {
  597. ($stop,$action) = split(/\0/, $dbline{$i});
  598. $arrow = ($i==$line
  599. and $filename eq $filename_ini)
  600. ? '==>'
  601. : ($dbline[$i]+0 ? ':' : ' ') ;
  602. $arrow .= 'b' if $stop;
  603. $arrow .= 'a' if $action;
  604. print $OUT "$i$arrow\t", $dbline[$i];
  605. $i++, last if $signal;
  606. }
  607. print $OUT "\n" unless $dbline[$i-1] =~ /\n$/;
  608. }
  609. $start = $i; # remember in case they want more
  610. $start = $max if $start > $max;
  611. next CMD; };
  612. $cmd =~ /^D$/ && do {
  613. print $OUT "Deleting all breakpoints...\n";
  614. my $file;
  615. for $file (keys %had_breakpoints) {
  616. local *dbline = $main::{'_<' . $file};
  617. my $max = $#dbline;
  618. my $was;
  619. for ($i = 1; $i <= $max ; $i++) {
  620. if (defined $dbline{$i}) {
  621. $dbline{$i} =~ s/^[^\0]+//;
  622. if ($dbline{$i} =~ s/^\0?$//) {
  623. delete $dbline{$i};
  624. }
  625. }
  626. }
  627. }
  628. undef %postponed;
  629. undef %postponed_file;
  630. undef %break_on_load;
  631. undef %had_breakpoints;
  632. next CMD; };
  633. $cmd =~ /^L$/ && do {
  634. my $file;
  635. for $file (keys %had_breakpoints) {
  636. local *dbline = $main::{'_<' . $file};
  637. my $max = $#dbline;
  638. my $was;
  639. for ($i = 1; $i <= $max; $i++) {
  640. if (defined $dbline{$i}) {
  641. print "$file:\n" unless $was++;
  642. print $OUT " $i:\t", $dbline[$i];
  643. ($stop,$action) = split(/\0/, $dbline{$i});
  644. print $OUT " break if (", $stop, ")\n"
  645. if $stop;
  646. print $OUT " action: ", $action, "\n"
  647. if $action;
  648. last if $signal;
  649. }
  650. }
  651. }
  652. if (%postponed) {
  653. print $OUT "Postponed breakpoints in subroutines:\n";
  654. my $subname;
  655. for $subname (keys %postponed) {
  656. print $OUT " $subname\t$postponed{$subname}\n";
  657. last if $signal;
  658. }
  659. }
  660. my @have = map { # Combined keys
  661. keys %{$postponed_file{$_}}
  662. } keys %postponed_file;
  663. if (@have) {
  664. print $OUT "Postponed breakpoints in files:\n";
  665. my ($file, $line);
  666. for $file (keys %postponed_file) {
  667. my $db = $postponed_file{$file};
  668. print $OUT " $file:\n";
  669. for $line (sort {$a <=> $b} keys %$db) {
  670. print $OUT " $line:\n";
  671. my ($stop,$action) = split(/\0/, $$db{$line});
  672. print $OUT " break if (", $stop, ")\n"
  673. if $stop;
  674. print $OUT " action: ", $action, "\n"
  675. if $action;
  676. last if $signal;
  677. }
  678. last if $signal;
  679. }
  680. }
  681. if (%break_on_load) {
  682. print $OUT "Breakpoints on load:\n";
  683. my $file;
  684. for $file (keys %break_on_load) {
  685. print $OUT " $file\n";
  686. last if $signal;
  687. }
  688. }
  689. if ($trace & 2) {
  690. print $OUT "Watch-expressions:\n";
  691. my $expr;
  692. for $expr (@to_watch) {
  693. print $OUT " $expr\n";
  694. last if $signal;
  695. }
  696. }
  697. next CMD; };
  698. $cmd =~ /^b\b\s*load\b\s*(.*)/ && do {
  699. my $file = $1; $file =~ s/\s+$//;
  700. {
  701. $break_on_load{$file} = 1;
  702. $break_on_load{$::INC{$file}} = 1 if $::INC{$file};
  703. $file .= '.pm', redo unless $file =~ /\./;
  704. }
  705. $had_breakpoints{$file} = 1;
  706. print $OUT "Will stop on load of `@{[join '\', `', sort keys %break_on_load]}'.\n";
  707. next CMD; };
  708. $cmd =~ /^b\b\s*(postpone|compile)\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
  709. my $cond = $3 || '1';
  710. my ($subname, $break) = ($2, $1 eq 'postpone');
  711. $subname =~ s/\'/::/;
  712. $subname = "${'package'}::" . $subname
  713. unless $subname =~ /::/;
  714. $subname = "main".$subname if substr($subname,0,2) eq "::";
  715. $postponed{$subname} = $break
  716. ? "break +0 if $cond" : "compile";
  717. next CMD; };
  718. $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
  719. $subname = $1;
  720. $cond = $2 || '1';
  721. $subname =~ s/\'/::/;
  722. $subname = "${'package'}::" . $subname
  723. unless $subname =~ /::/;
  724. $subname = "main".$subname if substr($subname,0,2) eq "::";
  725. # Filename below can contain ':'
  726. ($file,$i) = (find_sub($subname) =~ /^(.*):(.*)$/);
  727. $i += 0;
  728. if ($i) {
  729. $filename = $file;
  730. *dbline = $main::{'_<' . $filename};
  731. $had_breakpoints{$filename} = 1;
  732. $max = $#dbline;
  733. ++$i while $dbline[$i] == 0 && $i < $max;
  734. $dbline{$i} =~ s/^[^\0]*/$cond/;
  735. } else {
  736. print $OUT "Subroutine $subname not found.\n";
  737. }
  738. next CMD; };
  739. $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
  740. $i = ($1?$1:$line);
  741. $cond = $2 || '1';
  742. if ($dbline[$i] == 0) {
  743. print $OUT "Line $i not breakable.\n";
  744. } else {
  745. $had_breakpoints{$filename} = 1;
  746. $dbline{$i} =~ s/^[^\0]*/$cond/;
  747. }
  748. next CMD; };
  749. $cmd =~ /^d\b\s*(\d+)?/ && do {
  750. $i = ($1?$1:$line);
  751. $dbline{$i} =~ s/^[^\0]*//;
  752. delete $dbline{$i} if $dbline{$i} eq '';
  753. next CMD; };
  754. $cmd =~ /^A$/ && do {
  755. my $file;
  756. for $file (keys %had_breakpoints) {
  757. local *dbline = $main::{'_<' . $file};
  758. my $max = $#dbline;
  759. my $was;
  760. for ($i = 1; $i <= $max ; $i++) {
  761. if (defined $dbline{$i}) {
  762. $dbline{$i} =~ s/\0[^\0]*//;
  763. delete $dbline{$i} if $dbline{$i} eq '';
  764. }
  765. }
  766. }
  767. next CMD; };
  768. $cmd =~ /^O\s*$/ && do {
  769. for (@options) {
  770. &dump_option($_);
  771. }
  772. next CMD; };
  773. $cmd =~ /^O\s*(\S.*)/ && do {
  774. parse_options($1);
  775. next CMD; };
  776. $cmd =~ /^\<\<\s*(.*)/ && do { # \<\< for CPerl sake: not HERE
  777. push @$pre, action($1);
  778. next CMD; };
  779. $cmd =~ /^>>\s*(.*)/ && do {
  780. push @$post, action($1);
  781. next CMD; };
  782. $cmd =~ /^<\s*(.*)/ && do {
  783. $pre = [], next CMD unless $1;
  784. $pre = [action($1)];
  785. next CMD; };
  786. $cmd =~ /^>\s*(.*)/ && do {
  787. $post = [], next CMD unless $1;
  788. $post = [action($1)];
  789. next CMD; };
  790. $cmd =~ /^\{\{\s*(.*)/ && do {
  791. push @$pretype, $1;
  792. next CMD; };
  793. $cmd =~ /^\{\s*(.*)/ && do {
  794. $pretype = [], next CMD unless $1;
  795. $pretype = [$1];
  796. next CMD; };
  797. $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
  798. $i = $1; $j = $3;
  799. if ($dbline[$i] == 0) {
  800. print $OUT "Line $i may not have an action.\n";
  801. } else {
  802. $dbline{$i} =~ s/\0[^\0]*//;
  803. $dbline{$i} .= "\0" . action($j);
  804. }
  805. next CMD; };
  806. $cmd =~ /^n$/ && do {
  807. end_report(), next CMD if $finished and $level <= 1;
  808. $single = 2;
  809. $laststep = $cmd;
  810. last CMD; };
  811. $cmd =~ /^s$/ && do {
  812. end_report(), next CMD if $finished and $level <= 1;
  813. $single = 1;
  814. $laststep = $cmd;
  815. last CMD; };
  816. $cmd =~ /^c\b\s*([\w:]*)\s*$/ && do {
  817. end_report(), next CMD if $finished and $level <= 1;
  818. $subname = $i = $1;
  819. if ($i =~ /\D/) { # subroutine name
  820. $subname = $package."::".$subname
  821. unless $subname =~ /::/;
  822. ($file,$i) = (find_sub($subname) =~ /^(.*):(.*)$/);
  823. $i += 0;
  824. if ($i) {
  825. $filename = $file;
  826. *dbline = $main::{'_<' . $filename};
  827. $had_breakpoints{$filename}++;
  828. $max = $#dbline;
  829. ++$i while $dbline[$i] == 0 && $i < $max;
  830. } else {
  831. print $OUT "Subroutine $subname not found.\n";
  832. next CMD;
  833. }
  834. }
  835. if ($i) {
  836. if ($dbline[$i] == 0) {
  837. print $OUT "Line $i not breakable.\n";
  838. next CMD;
  839. }
  840. $dbline{$i} =~ s/($|\0)/;9$1/; # add one-time-only b.p.
  841. }
  842. for ($i=0; $i <= $stack_depth; ) {
  843. $stack[$i++] &= ~1;
  844. }
  845. last CMD; };
  846. $cmd =~ /^r$/ && do {
  847. end_report(), next CMD if $finished and $level <= 1;
  848. $stack[$stack_depth] |= 1;
  849. $doret = $option{PrintRet} ? $stack_depth - 1 : -2;
  850. last CMD; };
  851. $cmd =~ /^R$/ && do {
  852. print $OUT "Warning: some settings and command-line options may be lost!\n";
  853. my (@script, @flags, $cl);
  854. push @flags, '-w' if $ini_warn;
  855. # Put all the old includes at the start to get
  856. # the same debugger.
  857. for (@ini_INC) {
  858. push @flags, '-I', $_;
  859. }
  860. # Arrange for setting the old INC:
  861. set_list("PERLDB_INC", @ini_INC);
  862. if ($0 eq '-e') {
  863. for (1..$#{'::_<-e'}) { # The first line is PERL5DB
  864. chomp ($cl = $ {'::_<-e'}[$_]);
  865. push @script, '-e', $cl;
  866. }
  867. } else {
  868. @script = $0;
  869. }
  870. set_list("PERLDB_HIST",
  871. $term->Features->{getHistory}
  872. ? $term->GetHistory : @hist);
  873. my @had_breakpoints = keys %had_breakpoints;
  874. set_list("PERLDB_VISITED", @had_breakpoints);
  875. set_list("PERLDB_OPT", %option);
  876. set_list("PERLDB_ON_LOAD", %break_on_load);
  877. my @hard;
  878. for (0 .. $#had_breakpoints) {
  879. my $file = $had_breakpoints[$_];
  880. *dbline = $main::{'_<' . $file};
  881. next unless %dbline or $postponed_file{$file};
  882. (push @hard, $file), next
  883. if $file =~ /^\(eval \d+\)$/;
  884. my @add;
  885. @add = %{$postponed_file{$file}}
  886. if $postponed_file{$file};
  887. set_list("PERLDB_FILE_$_", %dbline, @add);
  888. }
  889. for (@hard) { # Yes, really-really...
  890. # Find the subroutines in this eval
  891. *dbline = $main::{'_<' . $_};
  892. my ($quoted, $sub, %subs, $line) = quotemeta $_;
  893. for $sub (keys %sub) {
  894. next unless $sub{$sub} =~ /^$quoted:(\d+)-(\d+)$/;
  895. $subs{$sub} = [$1, $2];
  896. }
  897. unless (%subs) {
  898. print $OUT
  899. "No subroutines in $_, ignoring breakpoints.\n";
  900. next;
  901. }
  902. LINES: for $line (keys %dbline) {
  903. # One breakpoint per sub only:
  904. my ($offset, $sub, $found);
  905. SUBS: for $sub (keys %subs) {
  906. if ($subs{$sub}->[1] >= $line # Not after the subroutine
  907. and (not defined $offset # Not caught
  908. or $offset < 0 )) { # or badly caught
  909. $found = $sub;
  910. $offset = $line - $subs{$sub}->[0];
  911. $offset = "+$offset", last SUBS if $offset >= 0;
  912. }
  913. }
  914. if (defined $offset) {
  915. $postponed{$found} =
  916. "break $offset if $dbline{$line}";
  917. } else {
  918. print $OUT "Breakpoint in $_:$line ignored: after all the subroutines.\n";
  919. }
  920. }
  921. }
  922. set_list("PERLDB_POSTPONE", %postponed);
  923. set_list("PERLDB_PRETYPE", @$pretype);
  924. set_list("PERLDB_PRE", @$pre);
  925. set_list("PERLDB_POST", @$post);
  926. set_list("PERLDB_TYPEAHEAD", @typeahead);
  927. $ENV{PERLDB_RESTART} = 1;
  928. #print "$^X, '-d', @flags, @script, ($emacs ? '-emacs' : ()), @ARGS";
  929. exec $^X, '-d', @flags, @script, ($emacs ? '-emacs' : ()), @ARGS;
  930. print $OUT "exec failed: $!\n";
  931. last CMD; };
  932. $cmd =~ /^T$/ && do {
  933. print_trace($OUT, 1); # skip DB
  934. next CMD; };
  935. $cmd =~ /^W\s*$/ && do {
  936. $trace &= ~2;
  937. @to_watch = @old_watch = ();
  938. next CMD; };
  939. $cmd =~ /^W\b\s*(.*)/s && do {
  940. push @to_watch, $1;
  941. $evalarg = $1;
  942. my ($val) = &eval;
  943. $val = (defined $val) ? "'$val'" : 'undef' ;
  944. push @old_watch, $val;
  945. $trace |= 2;
  946. next CMD; };
  947. $cmd =~ /^\/(.*)$/ && do {
  948. $inpat = $1;
  949. $inpat =~ s:([^\\])/$:$1:;
  950. if ($inpat ne "") {
  951. eval '$inpat =~ m'."\a$inpat\a";
  952. if ($@ ne "") {
  953. print $OUT "$@";
  954. next CMD;
  955. }
  956. $pat = $inpat;
  957. }
  958. $end = $start;
  959. $incr = -1;
  960. eval '
  961. for (;;) {
  962. ++$start;
  963. $start = 1 if ($start > $max);
  964. last if ($start == $end);
  965. if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) {
  966. if ($emacs) {
  967. print $OUT "\032\032$filename:$start:0\n";
  968. } else {
  969. print $OUT "$start:\t", $dbline[$start], "\n";
  970. }
  971. last;
  972. }
  973. } ';
  974. print $OUT "/$pat/: not found\n" if ($start == $end);
  975. next CMD; };
  976. $cmd =~ /^\?(.*)$/ && do {
  977. $inpat = $1;
  978. $inpat =~ s:([^\\])\?$:$1:;
  979. if ($inpat ne "") {
  980. eval '$inpat =~ m'."\a$inpat\a";
  981. if ($@ ne "") {
  982. print $OUT "$@";
  983. next CMD;
  984. }
  985. $pat = $inpat;
  986. }
  987. $end = $start;
  988. $incr = -1;
  989. eval '
  990. for (;;) {
  991. --$start;
  992. $start = $max if ($start <= 0);
  993. last if ($start == $end);
  994. if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) {
  995. if ($emacs) {
  996. print $OUT "\032\032$filename:$start:0\n";
  997. } else {
  998. print $OUT "$start:\t", $dbline[$start], "\n";
  999. }
  1000. last;
  1001. }
  1002. } ';
  1003. print $OUT "?$pat?: not found\n" if ($start == $end);
  1004. next CMD; };
  1005. $cmd =~ /^$rc+\s*(-)?(\d+)?$/ && do {
  1006. pop(@hist) if length($cmd) > 1;
  1007. $i = $1 ? ($#hist-($2?$2:1)) : ($2?$2:$#hist);
  1008. $cmd = $hist[$i];
  1009. print $OUT $cmd;
  1010. redo CMD; };
  1011. $cmd =~ /^$sh$sh\s*([\x00-\xff]*)/ && do {
  1012. &system($1);
  1013. next CMD; };
  1014. $cmd =~ /^$rc([^$rc].*)$/ && do {
  1015. $pat = "^$1";
  1016. pop(@hist) if length($cmd) > 1;
  1017. for ($i = $#hist; $i; --$i) {
  1018. last if $hist[$i] =~ /$pat/;
  1019. }
  1020. if (!$i) {
  1021. print $OUT "No such command!\n\n";
  1022. next CMD;
  1023. }
  1024. $cmd = $hist[$i];
  1025. print $OUT $cmd;
  1026. redo CMD; };
  1027. $cmd =~ /^$sh$/ && do {
  1028. &system($ENV{SHELL}||"/bin/sh");
  1029. next CMD; };
  1030. $cmd =~ /^$sh\s*([\x00-\xff]*)/ && do {
  1031. &system($ENV{SHELL}||"/bin/sh","-c",$1);
  1032. next CMD; };
  1033. $cmd =~ /^H\b\s*(-(\d+))?/ && do {
  1034. $end = $2?($#hist-$2):0;
  1035. $hist = 0 if $hist < 0;
  1036. for ($i=$#hist; $i>$end; $i--) {
  1037. print $OUT "$i: ",$hist[$i],"\n"
  1038. unless $hist[$i] =~ /^.?$/;
  1039. };
  1040. next CMD; };
  1041. $cmd =~ s/^p$/print {\$DB::OUT} \$_/;
  1042. $cmd =~ s/^p\b/print {\$DB::OUT} /;
  1043. $cmd =~ /^=/ && do {
  1044. if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
  1045. $alias{$k}="s~$k~$v~";
  1046. print $OUT "$k = $v\n";
  1047. } elsif ($cmd =~ /^=\s*$/) {
  1048. foreach $k (sort keys(%alias)) {
  1049. if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
  1050. print $OUT "$k = $v\n";
  1051. } else {
  1052. print $OUT "$k\t$alias{$k}\n";
  1053. };
  1054. };
  1055. };
  1056. next CMD; };
  1057. $cmd =~ /^\|\|?\s*[^|]/ && do {
  1058. if ($pager =~ /^\|/) {
  1059. open(SAVEOUT,">&STDOUT") || &warn("Can't save STDOUT");
  1060. open(STDOUT,">&OUT") || &warn("Can't redirect STDOUT");
  1061. } else {
  1062. open(SAVEOUT,">&OUT") || &warn("Can't save DB::OUT");
  1063. }
  1064. unless ($piped=open(OUT,$pager)) {
  1065. &warn("Can't pipe output to `$pager'");
  1066. if ($pager =~ /^\|/) {
  1067. open(OUT,">&STDOUT") || &warn("Can't restore DB::OUT");
  1068. open(STDOUT,">&SAVEOUT")
  1069. || &warn("Can't restore STDOUT");
  1070. close(SAVEOUT);
  1071. } else {
  1072. open(OUT,">&STDOUT") || &warn("Can't restore DB::OUT");
  1073. }
  1074. next CMD;
  1075. }
  1076. $SIG{PIPE}= \&DB::catch if $pager =~ /^\|/
  1077. && "" eq $SIG{PIPE} || "DEFAULT" eq $SIG{PIPE};
  1078. $selected= select(OUT);
  1079. $|= 1;
  1080. select( $selected ), $selected= "" unless $cmd =~ /^\|\|/;
  1081. $cmd =~ s/^\|+\s*//;
  1082. redo PIPE; };
  1083. # XXX Local variants do not work!
  1084. $cmd =~ s/^t\s/\$DB::trace |= 1;\n/;
  1085. $cmd =~ s/^s\s/\$DB::single = 1;\n/ && do {$laststep = 's'};
  1086. $cmd =~ s/^n\s/\$DB::single = 2;\n/ && do {$laststep = 'n'};
  1087. } # PIPE:
  1088. $evalarg = "\$^D = \$^D | \$DB::db_stop;\n$cmd"; &eval;
  1089. if ($onetimeDump) {
  1090. $onetimeDump = undef;
  1091. } elsif ($term_pid == $$) {
  1092. print $OUT "\n";
  1093. }
  1094. } continue { # CMD:
  1095. if ($piped) {
  1096. if ($pager =~ /^\|/) {
  1097. $?= 0; close(OUT) || &warn("Can't close DB::OUT");
  1098. &warn( "Pager `$pager' failed: ",
  1099. ($?>>8) > 128 ? ($?>>8)-256 : ($?>>8),
  1100. ( $? & 128 ) ? " (core dumped)" : "",
  1101. ( $? & 127 ) ? " (SIG ".($?&127).")" : "", "\n" ) if $?;
  1102. open(OUT,">&STDOUT") || &warn("Can't restore DB::OUT");
  1103. open(STDOUT,">&SAVEOUT") || &warn("Can't restore STDOUT");
  1104. $SIG{PIPE}= "DEFAULT" if $SIG{PIPE} eq \&DB::catch;
  1105. # Will stop ignoring SIGPIPE if done like nohup(1)
  1106. # does SIGINT but Perl doesn't give us a choice.
  1107. } else {
  1108. open(OUT,">&SAVEOUT") || &warn("Can't restore DB::OUT");
  1109. }
  1110. close(SAVEOUT);
  1111. select($selected), $selected= "" unless $selected eq "";
  1112. $piped= "";
  1113. }
  1114. } # CMD:
  1115. $exiting = 1 unless defined $cmd;
  1116. foreach $evalarg (@$post) {
  1117. &eval;
  1118. }
  1119. } # if ($single || $signal)
  1120. ($@, $!, $^E, $,, $/, $\, $^W) = @saved;
  1121. ();
  1122. }
  1123. # The following code may be executed now:
  1124. # BEGIN {warn 4}
  1125. sub sub {
  1126. my ($al, $ret, @ret) = "";
  1127. if (length($sub) > 10 && substr($sub, -10, 10) eq '::AUTOLOAD') {
  1128. $al = " for $$sub";
  1129. }
  1130. local $stack_depth = $stack_depth + 1; # Protect from non-local exits
  1131. $#stack = $stack_depth;
  1132. $stack[-1] = $single;
  1133. $single &= 1;
  1134. $single |= 4 if $stack_depth == $deep;
  1135. ($frame & 4
  1136. ? ( (print $LINEINFO ' ' x ($stack_depth - 1), "in "),
  1137. # Why -1? But it works! :-(
  1138. print_trace($LINEINFO, -1, 1, 1, "$sub$al") )
  1139. : print $LINEINFO ' ' x ($stack_depth - 1), "entering $sub$al\n") if $frame;
  1140. if (wantarray) {
  1141. @ret = &$sub;
  1142. $single |= $stack[$stack_depth--];
  1143. ($frame & 4
  1144. ? ( (print $LINEINFO ' ' x $stack_depth, "out "),
  1145. print_trace($LINEINFO, -1, 1, 1, "$sub$al") )
  1146. : print $LINEINFO ' ' x $stack_depth, "exited $sub$al\n") if $frame & 2;
  1147. if ($doret eq $stack_depth or $frame & 16) {
  1148. my $fh = ($doret eq $stack_depth ? $OUT : $LINEINFO);
  1149. print $fh ' ' x $stack_depth if $frame & 16;
  1150. print $fh "list context return from $sub:\n";
  1151. dumpit($fh, \@ret );
  1152. $doret = -2;
  1153. }
  1154. @ret;
  1155. } else {
  1156. if (defined wantarray) {
  1157. $ret = &$sub;
  1158. } else {
  1159. &$sub; undef $ret;
  1160. };
  1161. $single |= $stack[$stack_depth--];
  1162. ($frame & 4
  1163. ? ( (print $LINEINFO ' ' x $stack_depth, "out "),
  1164. print_trace($LINEINFO, -1, 1, 1, "$sub$al") )
  1165. : print $LINEINFO ' ' x $stack_depth, "exited $sub$al\n") if $frame & 2;
  1166. if ($doret eq $stack_depth or $frame & 16 and defined wantarray) {
  1167. my $fh = ($doret eq $stack_depth ? $OUT : $LINEINFO);
  1168. print $fh (' ' x $stack_depth) if $frame & 16;
  1169. print $fh (defined wantarray
  1170. ? "scalar context return from $sub: "
  1171. : "void context return from $sub\n");
  1172. dumpit( $fh, $ret ) if defined wantarray;
  1173. $doret = -2;
  1174. }
  1175. $ret;
  1176. }
  1177. }
  1178. sub save {
  1179. @saved = ($@, $!, $^E, $,, $/, $\, $^W);
  1180. $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
  1181. }
  1182. # The following takes its argument via $evalarg to preserve current @_
  1183. sub eval {
  1184. my @res;
  1185. {
  1186. my $otrace = $trace;
  1187. my $osingle = $single;
  1188. my $od = $^D;
  1189. @res = eval "$usercontext $evalarg;\n"; # '\n' for nice recursive debug
  1190. $trace = $otrace;
  1191. $single = $osingle;
  1192. $^D = $od;
  1193. }
  1194. my $at = $@;
  1195. local $saved[0]; # Preserve the old value of $@
  1196. eval { &DB::save };
  1197. if ($at) {
  1198. print $OUT $at;
  1199. } elsif ($onetimeDump eq 'dump') {
  1200. dumpit($OUT, \@res);
  1201. } elsif ($onetimeDump eq 'methods') {
  1202. methods($res[0]);
  1203. }
  1204. @res;
  1205. }
  1206. sub postponed_sub {
  1207. my $subname = shift;
  1208. if ($postponed{$subname} =~ s/^break\s([+-]?\d+)\s+if\s//) {
  1209. my $offset = $1 || 0;
  1210. # Filename below can contain ':'
  1211. my ($file,$i) = (find_sub($subname) =~ /^(.*):(\d+)-.*$/);
  1212. if ($i) {
  1213. $i += $offset;
  1214. local *dbline = $main::{'_<' . $file};
  1215. local $^W = 0; # != 0 is magical below
  1216. $had_breakpoints{$file}++;
  1217. my $max = $#dbline;
  1218. ++$i until $dbline[$i] != 0 or $i >= $max;
  1219. $dbline{$i} = delete $postponed{$subname};
  1220. } else {
  1221. print $OUT "Subroutine $subname not found.\n";
  1222. }
  1223. return;
  1224. }
  1225. elsif ($postponed{$subname} eq 'compile') { $signal = 1 }
  1226. #print $OUT "In postponed_sub for `$subname'.\n";
  1227. }
  1228. sub postponed {
  1229. if ($ImmediateStop) {
  1230. $ImmediateStop = 0;
  1231. $signal = 1;
  1232. }
  1233. return &postponed_sub
  1234. unless ref \$_[0] eq 'GLOB'; # A subroutine is compiled.
  1235. # Cannot be done before the file is compiled
  1236. local *dbline = shift;
  1237. my $filename = $dbline;
  1238. $filename =~ s/^_<//;
  1239. $signal = 1, print $OUT "'$filename' loaded...\n"
  1240. if $break_on_load{$filename};
  1241. print $LINEINFO ' ' x $stack_depth, "Package $filename.\n" if $frame;
  1242. return unless $postponed_file{$filename};
  1243. $had_breakpoints{$filename}++;
  1244. #%dbline = %{$postponed_file{$filename}}; # Cannot be done: unsufficient magic
  1245. my $key;
  1246. for $key (keys %{$postponed_file{$filename}}) {
  1247. $dbline{$key} = $ {$postponed_file{$filename}}{$key};
  1248. }
  1249. delete $postponed_file{$filename};
  1250. }
  1251. sub dumpit {
  1252. local ($savout) = select(shift);
  1253. my $osingle = $single;
  1254. my $otrace = $trace;
  1255. $single = $trace = 0;
  1256. local $frame = 0;
  1257. local $doret = -2;
  1258. unless (defined &main::dumpValue) {
  1259. do 'dumpvar.pl';
  1260. }
  1261. if (defined &main::dumpValue) {
  1262. &main::dumpValue(shift);
  1263. } else {
  1264. print $OUT "dumpvar.pl not available.\n";
  1265. }
  1266. $single = $osingle;
  1267. $trace = $otrace;
  1268. select ($savout);
  1269. }
  1270. # Tied method do not create a context, so may get wrong message:
  1271. sub print_trace {
  1272. my $fh = shift;
  1273. my @sub = dump_trace($_[0] + 1, $_[1]);
  1274. my $short = $_[2]; # Print short report, next one for sub name
  1275. my $s;
  1276. for ($i=0; $i <= $#sub; $i++) {
  1277. last if $signal;
  1278. local $" = ', ';
  1279. my $args = defined $sub[$i]{args}
  1280. ? "(@{ $sub[$i]{args} })"
  1281. : '' ;
  1282. $args = (substr $args, 0, $maxtrace - 3) . '...'
  1283. if length $args > $maxtrace;
  1284. my $file = $sub[$i]{file};
  1285. $file = $file eq '-e' ? $file : "file `$file'" unless $short;
  1286. $s = $sub[$i]{sub};
  1287. $s = (substr $s, 0, $maxtrace - 3) . '...' if length $s > $maxtrace;
  1288. if ($short) {
  1289. my $sub = @_ >= 4 ? $_[3] : $s;
  1290. print $fh "$sub[$i]{context}=$sub$args from $file:$sub[$i]{line}\n";
  1291. } else {
  1292. print $fh "$sub[$i]{context} = $s$args" .
  1293. " called from $file" .
  1294. " line $sub[$i]{line}\n";
  1295. }
  1296. }
  1297. }
  1298. sub dump_trace {
  1299. my $skip = shift;
  1300. my $count = shift || 1e9;
  1301. $skip++;
  1302. $count += $skip;
  1303. my ($p,$file,$line,$sub,$h,$args,$e,$r,@a,@sub,$context);
  1304. my $nothard = not $frame & 8;
  1305. local $frame = 0; # Do not want to trace this.
  1306. my $otrace = $trace;
  1307. $trace = 0;
  1308. for ($i = $skip;
  1309. $i < $count and ($p,$file,$line,$sub,$h,$context,$e,$r) = caller($i);
  1310. $i++) {
  1311. @a = ();
  1312. for $arg (@args) {
  1313. my $type;
  1314. if (not defined $arg) {
  1315. push @a, "undef";
  1316. } elsif ($nothard and tied $arg) {
  1317. push @a, "tied";
  1318. } elsif ($nothard and $type = ref $arg) {
  1319. push @a, "ref($type)";
  1320. } else {
  1321. local $_ = "$arg"; # Safe to stringify now - should not call f().
  1322. s/([\'\\])/\\$1/g;
  1323. s/(.*)/'$1'/s
  1324. unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
  1325. s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  1326. s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  1327. push(@a, $_);
  1328. }
  1329. }
  1330. $context = $context ? '@' : (defined $context ? "\$" : '.');
  1331. $args = $h ? [@a] : undef;
  1332. $e =~ s/\n\s*\;\s*\Z// if $e;
  1333. $e =~ s/([\\\'])/\\$1/g if $e;
  1334. if ($r) {
  1335. $sub = "require '$e'";
  1336. } elsif (defined $r) {
  1337. $sub = "eval '$e'";
  1338. } elsif ($sub eq '(eval)') {
  1339. $sub = "eval {...}";
  1340. }
  1341. push(@sub, {context => $context, sub => $sub, args => $args,
  1342. file => $file, line => $line});
  1343. last if $signal;
  1344. }
  1345. $trace = $otrace;
  1346. @sub;
  1347. }
  1348. sub action {
  1349. my $action = shift;
  1350. while ($action =~ s/\\$//) {
  1351. #print $OUT "+ ";
  1352. #$action .= "\n";
  1353. $action .= &gets;
  1354. }
  1355. $action;
  1356. }
  1357. sub gets {
  1358. local($.);
  1359. #<IN>;
  1360. &readline("cont: ");
  1361. }
  1362. sub system {
  1363. # We save, change, then restore STDIN and STDOUT to avoid fork() since
  1364. # many non-Unix systems can do system() but have problems with fork().
  1365. open(SAVEIN,"<&STDIN") || &warn("Can't save STDIN");
  1366. open(SAVEOUT,">&STDOUT") || &warn("Can't save STDOUT");
  1367. open(STDIN,"<&IN") || &warn("Can't redirect STDIN");
  1368. open(STDOUT,">&OUT") || &warn("Can't redirect STDOUT");
  1369. system(@_);
  1370. open(STDIN,"<&SAVEIN") || &warn("Can't restore STDIN");
  1371. open(STDOUT,">&SAVEOUT") || &warn("Can't restore STDOUT");
  1372. close(SAVEIN); close(SAVEOUT);
  1373. &warn( "(Command returned ", ($?>>8) > 128 ? ($?>>8)-256 : ($?>>8), ")",
  1374. ( $? & 128 ) ? " (core dumped)" : "",
  1375. ( $? & 127 ) ? " (SIG ".($?&127).")" : "", "\n" ) if $?;
  1376. $?;
  1377. }
  1378. sub setterm {
  1379. local $frame = 0;
  1380. local $doret = -2;
  1381. eval { require Term::ReadLine } or die $@;
  1382. if ($notty) {
  1383. if ($tty) {
  1384. open(IN,"<$tty") or die "Cannot open TTY `$TTY' for read: $!";
  1385. open(OUT,">$tty") or die "Cannot open TTY `$TTY' for write: $!";
  1386. $IN = \*IN;
  1387. $OUT = \*OUT;
  1388. my $sel = select($OUT);
  1389. $| = 1;
  1390. select($sel);
  1391. } else {
  1392. eval "require Term::Rendezvous;" or die $@;
  1393. my $rv = $ENV{PERLDB_NOTTY} || "/tmp/perldbtty$$";
  1394. my $term_rv = new Term::Rendezvous $rv;
  1395. $IN = $term_rv->IN;
  1396. $OUT = $term_rv->OUT;
  1397. }
  1398. }
  1399. if (!$rl) {
  1400. $term = new Term::ReadLine::Stub 'perldb', $IN, $OUT;
  1401. } else {
  1402. $term = new Term::ReadLine 'perldb', $IN, $OUT;
  1403. $rl_attribs = $term->Attribs;
  1404. $rl_attribs->{basic_word_break_characters} .= '-:+/*,[])}'
  1405. if defined $rl_attribs->{basic_word_break_characters}
  1406. and index($rl_attribs->{basic_word_break_characters}, ":") == -1;
  1407. $rl_attribs->{special_prefixes} = '$@&%';
  1408. $rl_attribs->{completer_word_break_characters} .= '$@&%';
  1409. $rl_attribs->{completion_function} = \&db_complete;
  1410. }
  1411. $LINEINFO = $OUT unless defined $LINEINFO;
  1412. $lineinfo = $console unless defined $lineinfo;
  1413. $term->MinLine(2);
  1414. if ($term->Features->{setHistory} and "@hist" ne "?") {
  1415. $term->SetHistory(@hist);
  1416. }
  1417. ornaments($ornaments) if defined $ornaments;
  1418. $term_pid = $$;
  1419. }
  1420. sub resetterm { # We forked, so we need a different TTY
  1421. $term_pid = $$;
  1422. if (defined &get_fork_TTY) {
  1423. &get_fork_TTY;
  1424. } elsif (not defined $fork_TTY
  1425. and defined $ENV{TERM} and $ENV{TERM} eq 'xterm'
  1426. and defined $ENV{WINDOWID} and defined $ENV{DISPLAY}) {
  1427. # Possibly _inside_ XTERM
  1428. open XT, q[3>&1 xterm -title 'Forked Perl debugger' -e sh -c 'tty 1>&3;\
  1429. sleep 10000000' |];
  1430. $fork_TTY = <XT>;
  1431. chomp $fork_TTY;
  1432. }
  1433. if (defined $fork_TTY) {
  1434. TTY($fork_TTY);
  1435. undef $fork_TTY;
  1436. } else {
  1437. print_help(<<EOP);
  1438. I<#########> Forked, but do not know how to change a B<TTY>. I<#########>
  1439. Define B<\$DB::fork_TTY>
  1440. - or a function B<DB::get_fork_TTY()> which will set B<\$DB::fork_TTY>.
  1441. The value of B<\$DB::fork_TTY> should be the name of I<TTY> to use.
  1442. On I<UNIX>-like systems one can get the name of a I<TTY> for the given window
  1443. by typing B<tty>, and disconnect the I<shell> from I<TTY> by B<sleep 1000000>.
  1444. EOP
  1445. }
  1446. }
  1447. sub readline {
  1448. if (@typeahead) {
  1449. my $left = @typeahead;
  1450. my $got = shift @typeahead;
  1451. print $OUT "auto(-$left)", shift, $got, "\n";
  1452. $term->AddHistory($got)
  1453. if length($got) > 1 and defined $term->Features->{addHistory};
  1454. return $got;
  1455. }
  1456. local $frame = 0;
  1457. local $doret = -2;
  1458. $term->readline(@_);
  1459. }
  1460. sub dump_option {
  1461. my ($opt, $val)= @_;
  1462. $val = option_val($opt,'N/A');
  1463. $val =~ s/([\\\'])/\\$1/g;
  1464. printf $OUT "%20s = '%s'\n", $opt, $val;
  1465. }
  1466. sub option_val {
  1467. my ($opt, $default)= @_;
  1468. my $val;
  1469. if (defined $optionVars{$opt}
  1470. and defined $ {$optionVars{$opt}}) {
  1471. $val = $ {$optionVars{$opt}};
  1472. } elsif (defined $optionAction{$opt}
  1473. and defined &{$optionAction{$opt}}) {
  1474. $val = &{$optionAction{$opt}}();
  1475. } elsif (defined $optionAction{$opt}
  1476. and not defined $option{$opt}
  1477. or defined $optionVars{$opt}
  1478. and not defined $ {$optionVars{$opt}}) {
  1479. $val = $default;
  1480. } else {
  1481. $val = $option{$opt};
  1482. }
  1483. $val
  1484. }
  1485. sub parse_options {
  1486. local($_)= @_;
  1487. while ($_ ne "") {
  1488. s/^(\w+)(\s*$|\W)// or print($OUT "Invalid option `$_'\n"), last;
  1489. my ($opt,$sep) = ($1,$2);
  1490. my $val;
  1491. if ("?" eq $sep) {
  1492. print($OUT "Option query `$opt?' followed by non-space `$_'\n"), last
  1493. if /^\S/;
  1494. #&dump_option($opt);
  1495. } elsif ($sep !~ /\S/) {
  1496. $val = "1";
  1497. } elsif ($sep eq "=") {
  1498. s/^(\S*)($|\s+)//;
  1499. $val = $1;
  1500. } else { #{ to "let some poor schmuck bounce on the % key in B<vi>."
  1501. my ($end) = "\\" . substr( ")]>}$sep", index("([<{",$sep), 1 ); #}
  1502. s/^(([^\\$end]|\\[\\$end])*)$end($|\s+)// or
  1503. print($OUT "Unclosed option value `$opt$sep$_'\n"), last;
  1504. $val = $1;
  1505. $val =~ s/\\([\\$end])/$1/g;
  1506. }
  1507. my ($option);
  1508. my $matches =
  1509. grep( /^\Q$opt/ && ($option = $_), @options );
  1510. $matches = grep( /^\Q$opt/i && ($option = $_), @options )
  1511. unless $matches;
  1512. print $OUT "Unknown option `$opt'\n" unless $matches;
  1513. print $OUT "Ambiguous option `$opt'\n" if $matches > 1;
  1514. $option{$option} = $val if $matches == 1 and defined $val;
  1515. eval "local \$frame = 0; local \$doret = -2;
  1516. require '$optionRequire{$option}'"
  1517. if $matches == 1 and defined $optionRequire{$option} and defined $val;
  1518. $ {$optionVars{$option}} = $val
  1519. if $matches == 1
  1520. and defined $optionVars{$option} and defined $val;
  1521. & {$optionAction{$option}} ($val)
  1522. if $matches == 1
  1523. and defined $optionAction{$option}
  1524. and defined &{$optionAction{$option}} and defined $val;
  1525. &dump_option($option) if $matches == 1 && $OUT ne \*STDERR; # Not $rcfile
  1526. s/^\s+//;
  1527. }
  1528. }
  1529. sub set_list {
  1530. my ($stem,@list) = @_;
  1531. my $val;
  1532. $ENV{"$ {stem}_n"} = @list;
  1533. for $i (0 .. $#list) {
  1534. $val = $list[$i];
  1535. $val =~ s/\\/\\\\/g;
  1536. $val =~ s/([\0-\37\177\200-\377])/"\\0x" . unpack('H2',$1)/eg;
  1537. $ENV{"$ {stem}_$i"} = $val;
  1538. }
  1539. }
  1540. sub get_list {
  1541. my $stem = shift;
  1542. my @list;
  1543. my $n = delete $ENV{"$ {stem}_n"};
  1544. my $val;
  1545. for $i (0 .. $n - 1) {
  1546. $val = delete $ENV{"$ {stem}_$i"};
  1547. $val =~ s/\\((\\)|0x(..))/ $2 ? $2 : pack('H2', $3) /ge;
  1548. push @list, $val;
  1549. }
  1550. @list;
  1551. }
  1552. sub catch {
  1553. $signal = 1;
  1554. return; # Put nothing on the stack - malloc/free land!
  1555. }
  1556. sub warn {
  1557. my($msg)= join("",@_);
  1558. $msg .= ": $!\n" unless $msg =~ /\n$/;
  1559. print $OUT $msg;
  1560. }
  1561. sub TTY {
  1562. if (@_ and $term and $term->Features->{newTTY}) {
  1563. my ($in, $out) = shift;
  1564. if ($in =~ /,/) {
  1565. ($in, $out) = split /,/, $in, 2;
  1566. } else {
  1567. $out = $in;
  1568. }
  1569. open IN, $in or die "cannot open `$in' for read: $!";
  1570. open OUT, ">$out" or die "cannot open `$out' for write: $!";
  1571. $term->newTTY(\*IN, \*OUT);
  1572. $IN = \*IN;
  1573. $OUT = \*OUT;
  1574. return $tty = $in;
  1575. } elsif ($term and @_) {
  1576. &warn("Too late to set TTY, enabled on next `R'!\n");
  1577. }
  1578. $tty = shift if @_;
  1579. $tty or $console;
  1580. }
  1581. sub noTTY {
  1582. if ($term) {
  1583. &warn("Too late to set noTTY, enabled on next `R'!\n") if @_;
  1584. }
  1585. $notty = shift if @_;
  1586. $notty;
  1587. }
  1588. sub ReadLine {
  1589. if ($term) {
  1590. &warn("Too late to set ReadLine, enabled on next `R'!\n") if @_;
  1591. }
  1592. $rl = shift if @_;
  1593. $rl;
  1594. }
  1595. sub tkRunning {
  1596. if ($ {$term->Features}{tkRunning}) {
  1597. return $term->tkRunning(@_);
  1598. } else {
  1599. print $OUT "tkRunning not supported by current ReadLine package.\n";
  1600. 0;
  1601. }
  1602. }
  1603. sub NonStop {
  1604. if ($term) {
  1605. &warn("Too late to set up NonStop mode, enabled on next `R'!\n") if @_;
  1606. }
  1607. $runnonstop = shift if @_;
  1608. $runnonstop;
  1609. }
  1610. sub pager {
  1611. if (@_) {
  1612. $pager = shift;
  1613. $pager="|".$pager unless $pager =~ /^(\+?\>|\|)/;
  1614. }
  1615. $pager;
  1616. }
  1617. sub shellBang {
  1618. if (@_) {
  1619. $sh = quotemeta shift;
  1620. $sh .= "\\b" if $sh =~ /\w$/;
  1621. }
  1622. $psh = $sh;
  1623. $psh =~ s/\\b$//;
  1624. $psh =~ s/\\(.)/$1/g;
  1625. &sethelp;
  1626. $psh;
  1627. }
  1628. sub ornaments {
  1629. if (defined $term) {
  1630. local ($warnLevel,$dieLevel) = (0, 1);
  1631. return '' unless $term->Features->{ornaments};
  1632. eval { $term->ornaments(@_) } || '';
  1633. } else {
  1634. $ornaments = shift;
  1635. }
  1636. }
  1637. sub recallCommand {
  1638. if (@_) {
  1639. $rc = quotemeta shift;
  1640. $rc .= "\\b" if $rc =~ /\w$/;
  1641. }
  1642. $prc = $rc;
  1643. $prc =~ s/\\b$//;
  1644. $prc =~ s/\\(.)/$1/g;
  1645. &sethelp;
  1646. $prc;
  1647. }
  1648. sub LineInfo {
  1649. return $lineinfo unless @_;
  1650. $lineinfo = shift;
  1651. my $stream = ($lineinfo =~ /^(\+?\>|\|)/) ? $lineinfo : ">$lineinfo";
  1652. $emacs = ($stream =~ /^\|/);
  1653. open(LINEINFO, "$stream") || &warn("Cannot open `$stream' for write");
  1654. $LINEINFO = \*LINEINFO;
  1655. my $save = select($LINEINFO);
  1656. $| = 1;
  1657. select($save);
  1658. $lineinfo;
  1659. }
  1660. sub list_versions {
  1661. my %version;
  1662. my $file;
  1663. for (keys %INC) {
  1664. $file = $_;
  1665. s,\.p[lm]$,,i ;
  1666. s,/,::,g ;
  1667. s/^perl5db$/DB/;
  1668. s/^Term::ReadLine::readline$/readline/;
  1669. if (defined $ { $_ . '::VERSION' }) {
  1670. $version{$file} = "$ { $_ . '::VERSION' } from ";
  1671. }
  1672. $version{$file} .= $INC{$file};
  1673. }
  1674. dumpit($OUT,\%version);
  1675. }
  1676. sub sethelp {
  1677. $help = "
  1678. B<T> Stack trace.
  1679. B<s> [I<expr>] Single step [in I<expr>].
  1680. B<n> [I<expr>] Next, steps over subroutine calls [in I<expr>].
  1681. <B<CR>> Repeat last B<n> or B<s> command.
  1682. B<r> Return from current subroutine.
  1683. B<c> [I<line>|I<sub>] Continue; optionally inserts a one-time-only breakpoint
  1684. at the specified position.
  1685. B<l> I<min>B<+>I<incr> List I<incr>+1 lines starting at I<min>.
  1686. B<l> I<min>B<->I<max> List lines I<min> through I<max>.
  1687. B<l> I<line> List single I<line>.
  1688. B<l> I<subname> List first window of lines from subroutine.
  1689. B<l> List next window of lines.
  1690. B<-> List previous window of lines.
  1691. B<w> [I<line>] List window around I<line>.
  1692. B<.> Return to the executed line.
  1693. B<f> I<filename> Switch to viewing I<filename>. Must be loaded.
  1694. B</>I<pattern>B</> Search forwards for I<pattern>; final B</> is optional.
  1695. B<?>I<pattern>B<?> Search backwards for I<pattern>; final B<?> is optional.
  1696. B<L> List all breakpoints and actions.
  1697. B<S> [[B<!>]I<pattern>] List subroutine names [not] matching I<pattern>.
  1698. B<t> Toggle trace mode.
  1699. B<t> I<expr> Trace through execution of I<expr>.
  1700. B<b> [I<line>] [I<condition>]
  1701. Set breakpoint; I<line> defaults to the current execution line;
  1702. I<condition> breaks if it evaluates to true, defaults to '1'.
  1703. B<b> I<subname> [I<condition>]
  1704. Set breakpoint at first line of subroutine.
  1705. B<b> B<load> I<filename> Set breakpoint on `require'ing the given file.
  1706. B<b> B<postpone> I<subname> [I<condition>]
  1707. Set breakpoint at first line of subroutine after
  1708. it is compiled.
  1709. B<b> B<compile> I<subname>
  1710. Stop after the subroutine is compiled.
  1711. B<d> [I<line>] Delete the breakpoint for I<line>.
  1712. B<D> Delete all breakpoints.
  1713. B<a> [I<line>] I<command>
  1714. Set an action to be done before the I<line> is executed.
  1715. Sequence is: check for breakpoint/watchpoint, print line
  1716. if necessary, do action, prompt user if necessary,
  1717. execute expression.
  1718. B<A> Delete all actions.
  1719. B<W> I<expr> Add a global watch-expression.
  1720. B<W> Delete all watch-expressions.
  1721. B<V> [I<pkg> [I<vars>]] List some (default all) variables in package (default current).
  1722. Use B<~>I<pattern> and B<!>I<pattern> for positive and negative regexps.
  1723. B<X> [I<vars>] Same as \"B<V> I<currentpackage> [I<vars>]\".
  1724. B<x> I<expr> Evals expression in array context, dumps the result.
  1725. B<m> I<expr> Evals expression in array context, prints methods callable
  1726. on the first element of the result.
  1727. B<m> I<class> Prints methods callable via the given class.
  1728. B<O> [I<opt>[B<=>I<val>]] [I<opt>B<\">I<val>B<\">] [I<opt>B<?>]...
  1729. Set or query values of options. I<val> defaults to 1. I<opt> can
  1730. be abbreviated. Several options can be listed.
  1731. I<recallCommand>, I<ShellBang>: chars used to recall command or spawn shell;
  1732. I<pager>: program for output of \"|cmd\";
  1733. I<tkRunning>: run Tk while prompting (with ReadLine);
  1734. I<signalLevel> I<warnLevel> I<dieLevel>: level of verbosity;
  1735. I<inhibit_exit> Allows stepping off the end of the script.
  1736. I<ImmediateStop> Debugger should stop as early as possible.
  1737. The following options affect what happens with B<V>, B<X>, and B<x> commands:
  1738. I<arrayDepth>, I<hashDepth>: print only first N elements ('' for all);
  1739. I<compactDump>, I<veryCompact>: change style of array and hash dump;
  1740. I<globPrint>: whether to print contents of globs;
  1741. I<DumpDBFiles>: dump arrays holding debugged files;
  1742. I<DumpPackages>: dump symbol tables of packages;
  1743. I<DumpReused>: dump contents of \"reused\" addresses;
  1744. I<quote>, I<HighBit>, I<undefPrint>: change style of string dump;
  1745. I<bareStringify>: Do not print the overload-stringified value;
  1746. Option I<PrintRet> affects printing of return value after B<r> command,
  1747. I<frame> affects printing messages on entry and exit from subroutines.
  1748. I<AutoTrace> affects printing messages on every possible breaking point.
  1749. I<maxTraceLen> gives maximal length of evals/args listed in stack trace.
  1750. I<ornaments> affects screen appearance of the command line.
  1751. During startup options are initialized from \$ENV{PERLDB_OPTS}.
  1752. You can put additional initialization options I<TTY>, I<noTTY>,
  1753. I<ReadLine>, and I<NonStop> there (or use `B<R>' after you set them).
  1754. B<<> I<expr> Define Perl command to run before each prompt.
  1755. B<<<> I<expr> Add to the list of Perl commands to run before each prompt.
  1756. B<>> I<expr> Define Perl command to run after each prompt.
  1757. B<>>B<>> I<expr> Add to the list of Perl commands to run after each prompt.
  1758. B<{> I<db_command> Define debugger command to run before each prompt.
  1759. B<{{> I<db_command> Add to the list of debugger commands to run before each prompt.
  1760. B<$prc> I<number> Redo a previous command (default previous command).
  1761. B<$prc> I<-number> Redo number'th-to-last command.
  1762. B<$prc> I<pattern> Redo last command that started with I<pattern>.
  1763. See 'B<O> I<recallCommand>' too.
  1764. B<$psh$psh> I<cmd> Run cmd in a subprocess (reads from DB::IN, writes to DB::OUT)"
  1765. . ( $rc eq $sh ? "" : "
  1766. B<$psh> [I<cmd>] Run I<cmd> in subshell (forces \"\$SHELL -c 'cmd'\")." ) . "
  1767. See 'B<O> I<shellBang>' too.
  1768. B<H> I<-number> Display last number commands (default all).
  1769. B<p> I<expr> Same as \"I<print {DB::OUT} expr>\" in current package.
  1770. B<|>I<dbcmd> Run debugger command, piping DB::OUT to current pager.
  1771. B<||>I<dbcmd> Same as B<|>I<dbcmd> but DB::OUT is temporarilly select()ed as well.
  1772. B<\=> [I<alias> I<value>] Define a command alias, or list current aliases.
  1773. I<command> Execute as a perl statement in current package.
  1774. B<v> Show versions of loaded modules.
  1775. B<R> Pure-man-restart of debugger, some of debugger state
  1776. and command-line options may be lost.
  1777. Currently the following setting are preserved:
  1778. history, breakpoints and actions, debugger B<O>ptions
  1779. and the following command-line options: I<-w>, I<-I>, I<-e>.
  1780. B<h> [I<db_command>] Get help [on a specific debugger command], enter B<|h> to page.
  1781. B<h h> Summary of debugger commands.
  1782. B<q> or B<^D> Quit. Set B<\$DB::finished = 0> to debug global destruction.
  1783. ";
  1784. $summary = <<"END_SUM";
  1785. I<List/search source lines:> I<Control script execution:>
  1786. B<l> [I<ln>|I<sub>] List source code B<T> Stack trace
  1787. B<-> or B<.> List previous/current line B<s> [I<expr>] Single step [in expr]
  1788. B<w> [I<line>] List around line B<n> [I<expr>] Next, steps over subs
  1789. B<f> I<filename> View source in file <B<CR>> Repeat last B<n> or B<s>
  1790. B</>I<pattern>B</> B<?>I<patt>B<?> Search forw/backw B<r> Return from subroutine
  1791. B<v> Show versions of modules B<c> [I<ln>|I<sub>] Continue until position
  1792. I<Debugger controls:> B<L> List break/watch/actions
  1793. B<O> [...] Set debugger options B<t> [I<expr>] Toggle trace [trace expr]
  1794. B<<>[B<<>] or B<{>[B<{>] [I<cmd>] Do before prompt B<b> [I<ln>|I<event>] [I<cnd>] Set breakpoint
  1795. B<>>[B<>>] [I<cmd>] Do after prompt B<b> I<sub> [I<cnd>] Set breakpoint for sub
  1796. B<$prc> [I<N>|I<pat>] Redo a previous command B<d> [I<ln>] or B<D> Delete a/all breakpoints
  1797. B<H> [I<-num>] Display last num commands B<a> [I<ln>] I<cmd> Do cmd before line
  1798. B<=> [I<a> I<val>] Define/list an alias B<W> I<expr> Add a watch expression
  1799. B<h> [I<db_cmd>] Get help on command B<A> or B<W> Delete all actions/watch
  1800. B<|>[B<|>]I<dbcmd> Send output to pager B<$psh>\[B<$psh>\] I<syscmd> Run cmd in a subprocess
  1801. B<q> or B<^D> Quit B<R> Attempt a restart
  1802. I<Data Examination:> B<expr> Execute perl code, also see: B<s>,B<n>,B<t> I<expr>
  1803. B<x>|B<m> I<expr> Evals expr in array context, dumps the result or lists methods.
  1804. B<p> I<expr> Print expression (uses script's current package).
  1805. B<S> [[B<!>]I<pat>] List subroutine names [not] matching pattern
  1806. B<V> [I<Pk> [I<Vars>]] List Variables in Package. Vars can be ~pattern or !pattern.
  1807. B<X> [I<Vars>] Same as \"B<V> I<current_package> [I<Vars>]\".
  1808. END_SUM
  1809. # ')}}; # Fix balance of Emacs parsing
  1810. }
  1811. sub print_help {
  1812. my $message = shift;
  1813. if (@Term::ReadLine::TermCap::rl_term_set) {
  1814. $message =~ s/B<([^>]+|>)>/$Term::ReadLine::TermCap::rl_term_set[2]$1$Term::ReadLine::TermCap::rl_term_set[3]/g;
  1815. $message =~ s/I<([^>]+|>)>/$Term::ReadLine::TermCap::rl_term_set[0]$1$Term::ReadLine::TermCap::rl_term_set[1]/g;
  1816. }
  1817. print $OUT $message;
  1818. }
  1819. sub diesignal {
  1820. local $frame = 0;
  1821. local $doret = -2;
  1822. $SIG{'ABRT'} = 'DEFAULT';
  1823. kill 'ABRT', $$ if $panic++;
  1824. if (defined &Carp::longmess) {
  1825. local $SIG{__WARN__} = '';
  1826. local $Carp::CarpLevel = 2; # mydie + confess
  1827. &warn(Carp::longmess("Signal @_"));
  1828. }
  1829. else {
  1830. print $DB::OUT "Got signal @_\n";
  1831. }
  1832. kill 'ABRT', $$;
  1833. }
  1834. sub dbwarn {
  1835. local $frame = 0;
  1836. local $doret = -2;
  1837. local $SIG{__WARN__} = '';
  1838. local $SIG{__DIE__} = '';
  1839. eval { require Carp } if defined $^S; # If error/warning during compilation,
  1840. # require may be broken.
  1841. warn(@_, "\nCannot print stack trace, load with -MCarp option to see stack"),
  1842. return unless defined &Carp::longmess;
  1843. my ($mysingle,$mytrace) = ($single,$trace);
  1844. $single = 0; $trace = 0;
  1845. my $mess = Carp::longmess(@_);
  1846. ($single,$trace) = ($mysingle,$mytrace);
  1847. &warn($mess);
  1848. }
  1849. sub dbdie {
  1850. local $frame = 0;
  1851. local $doret = -2;
  1852. local $SIG{__DIE__} = '';
  1853. local $SIG{__WARN__} = '';
  1854. my $i = 0; my $ineval = 0; my $sub;
  1855. if ($dieLevel > 2) {
  1856. local $SIG{__WARN__} = \&dbwarn;
  1857. &warn(@_); # Yell no matter what
  1858. return;
  1859. }
  1860. if ($dieLevel < 2) {
  1861. die @_ if $^S; # in eval propagate
  1862. }
  1863. eval { require Carp } if defined $^S; # If error/warning during compilation,
  1864. # require may be broken.
  1865. die(@_, "\nCannot print stack trace, load with -MCarp option to see stack")
  1866. unless defined &Carp::longmess;
  1867. # We do not want to debug this chunk (automatic disabling works
  1868. # inside DB::DB, but not in Carp).
  1869. my ($mysingle,$mytrace) = ($single,$trace);
  1870. $single = 0; $trace = 0;
  1871. my $mess = Carp::longmess(@_);
  1872. ($single,$trace) = ($mysingle,$mytrace);
  1873. die $mess;
  1874. }
  1875. sub warnLevel {
  1876. if (@_) {
  1877. $prevwarn = $SIG{__WARN__} unless $warnLevel;
  1878. $warnLevel = shift;
  1879. if ($warnLevel) {
  1880. $SIG{__WARN__} = \&DB::dbwarn;
  1881. } else {
  1882. $SIG{__WARN__} = $prevwarn;
  1883. }
  1884. }
  1885. $warnLevel;
  1886. }
  1887. sub dieLevel {
  1888. if (@_) {
  1889. $prevdie = $SIG{__DIE__} unless $dieLevel;
  1890. $dieLevel = shift;
  1891. if ($dieLevel) {
  1892. $SIG{__DIE__} = \&DB::dbdie; # if $dieLevel < 2;
  1893. #$SIG{__DIE__} = \&DB::diehard if $dieLevel >= 2;
  1894. print $OUT "Stack dump during die enabled",
  1895. ( $dieLevel == 1 ? " outside of evals" : ""), ".\n"
  1896. if $I_m_init;
  1897. print $OUT "Dump printed too.\n" if $dieLevel > 2;
  1898. } else {
  1899. $SIG{__DIE__} = $prevdie;
  1900. print $OUT "Default die handler restored.\n";
  1901. }
  1902. }
  1903. $dieLevel;
  1904. }
  1905. sub signalLevel {
  1906. if (@_) {
  1907. $prevsegv = $SIG{SEGV} unless $signalLevel;
  1908. $prevbus = $SIG{BUS} unless $signalLevel;
  1909. $signalLevel = shift;
  1910. if ($signalLevel) {
  1911. $SIG{SEGV} = \&DB::diesignal;
  1912. $SIG{BUS} = \&DB::diesignal;
  1913. } else {
  1914. $SIG{SEGV} = $prevsegv;
  1915. $SIG{BUS} = $prevbus;
  1916. }
  1917. }
  1918. $signalLevel;
  1919. }
  1920. sub find_sub {
  1921. my $subr = shift;
  1922. return unless defined &$subr;
  1923. $sub{$subr} or do {
  1924. $subr = \&$subr; # Hard reference
  1925. my $s;
  1926. for (keys %sub) {
  1927. $s = $_, last if $subr eq \&$_;
  1928. }
  1929. $sub{$s} if $s;
  1930. }
  1931. }
  1932. sub methods {
  1933. my $class = shift;
  1934. $class = ref $class if ref $class;
  1935. local %seen;
  1936. local %packs;
  1937. methods_via($class, '', 1);
  1938. methods_via('UNIVERSAL', 'UNIVERSAL', 0);
  1939. }
  1940. sub methods_via {
  1941. my $class = shift;
  1942. return if $packs{$class}++;
  1943. my $prefix = shift;
  1944. my $prepend = $prefix ? "via $prefix: " : '';
  1945. my $name;
  1946. for $name (grep {defined &{$ {"$ {class}::"}{$_}}}
  1947. sort keys %{"$ {class}::"}) {
  1948. next if $seen{ $name }++;
  1949. print $DB::OUT "$prepend$name\n";
  1950. }
  1951. return unless shift; # Recurse?
  1952. for $name (@{"$ {class}::ISA"}) {
  1953. $prepend = $prefix ? $prefix . " -> $name" : $name;
  1954. methods_via($name, $prepend, 1);
  1955. }
  1956. }
  1957. # The following BEGIN is very handy if debugger goes havoc, debugging debugger?
  1958. BEGIN { # This does not compile, alas.
  1959. $IN = \*STDIN; # For bugs before DB::OUT has been opened
  1960. $OUT = \*STDERR; # For errors before DB::OUT has been opened
  1961. $sh = '!';
  1962. $rc = ',';
  1963. @hist = ('?');
  1964. $deep = 100; # warning if stack gets this deep
  1965. $window = 10;
  1966. $preview = 3;
  1967. $sub = '';
  1968. $SIG{INT} = \&DB::catch;
  1969. # This may be enabled to debug debugger:
  1970. #$warnLevel = 1 unless defined $warnLevel;
  1971. #$dieLevel = 1 unless defined $dieLevel;
  1972. #$signalLevel = 1 unless defined $signalLevel;
  1973. $db_stop = 0; # Compiler warning
  1974. $db_stop = 1 << 30;
  1975. $level = 0; # Level of recursive debugging
  1976. # @stack and $doret are needed in sub sub, which is called for DB::postponed.
  1977. # Triggers bug (?) in perl is we postpone this until runtime:
  1978. @postponed = @stack = (0);
  1979. $stack_depth = 0; # Localized $#stack
  1980. $doret = -2;
  1981. $frame = 0;
  1982. }
  1983. BEGIN {$^W = $ini_warn;} # Switch warnings back
  1984. #use Carp; # This did break, left for debuggin
  1985. sub db_complete {
  1986. # Specific code for b c l V m f O, &blah, $blah, @blah, %blah
  1987. my($text, $line, $start) = @_;
  1988. my ($itext, $search, $prefix, $pack) =
  1989. ($text, "^\Q$ {'package'}::\E([^:]+)\$");
  1990. return sort grep /^\Q$text/, (keys %sub), qw(postpone load compile), # subroutines
  1991. (map { /$search/ ? ($1) : () } keys %sub)
  1992. if (substr $line, 0, $start) =~ /^\|*[blc]\s+((postpone|compile)\s+)?$/;
  1993. return sort grep /^\Q$text/, values %INC # files
  1994. if (substr $line, 0, $start) =~ /^\|*b\s+load\s+$/;
  1995. return sort map {($_, db_complete($_ . "::", "V ", 2))}
  1996. grep /^\Q$text/, map { /^(.*)::$/ ? ($1) : ()} keys %:: # top-packages
  1997. if (substr $line, 0, $start) =~ /^\|*[Vm]\s+$/ and $text =~ /^\w*$/;
  1998. return sort map {($_, db_complete($_ . "::", "V ", 2))}
  1999. grep !/^main::/,
  2000. grep /^\Q$text/, map { /^(.*)::$/ ? ($prefix . "::$1") : ()} keys %{$prefix . '::'}
  2001. # packages
  2002. if (substr $line, 0, $start) =~ /^\|*[Vm]\s+$/
  2003. and $text =~ /^(.*[^:])::?(\w*)$/ and $prefix = $1;
  2004. if ( $line =~ /^\|*f\s+(.*)/ ) { # Loaded files
  2005. # We may want to complete to (eval 9), so $text may be wrong
  2006. $prefix = length($1) - length($text);
  2007. $text = $1;
  2008. return sort
  2009. map {substr $_, 2 + $prefix} grep /^_<\Q$text/, (keys %main::), $0
  2010. }
  2011. if ((substr $text, 0, 1) eq '&') { # subroutines
  2012. $text = substr $text, 1;
  2013. $prefix = "&";
  2014. return sort map "$prefix$_",
  2015. grep /^\Q$text/,
  2016. (keys %sub),
  2017. (map { /$search/ ? ($1) : () }
  2018. keys %sub);
  2019. }
  2020. if ($text =~ /^[\$@%](.*)::(.*)/) { # symbols in a package
  2021. $pack = ($1 eq 'main' ? '' : $1) . '::';
  2022. $prefix = (substr $text, 0, 1) . $1 . '::';
  2023. $text = $2;
  2024. my @out
  2025. = map "$prefix$_", grep /^\Q$text/, grep /^_?[a-zA-Z]/, keys %$pack ;
  2026. if (@out == 1 and $out[0] =~ /::$/ and $out[0] ne $itext) {
  2027. return db_complete($out[0], $line, $start);
  2028. }
  2029. return sort @out;
  2030. }
  2031. if ($text =~ /^[\$@%]/) { # symbols (in $package + packages in main)
  2032. $pack = ($package eq 'main' ? '' : $package) . '::';
  2033. $prefix = substr $text, 0, 1;
  2034. $text = substr $text, 1;
  2035. my @out = map "$prefix$_", grep /^\Q$text/,
  2036. (grep /^_?[a-zA-Z]/, keys %$pack),
  2037. ( $pack eq '::' ? () : (grep /::$/, keys %::) ) ;
  2038. if (@out == 1 and $out[0] =~ /::$/ and $out[0] ne $itext) {
  2039. return db_complete($out[0], $line, $start);
  2040. }
  2041. return sort @out;
  2042. }
  2043. if ((substr $line, 0, $start) =~ /^\|*O\b.*\s$/) { # Options after a space
  2044. my @out = grep /^\Q$text/, @options;
  2045. my $val = option_val($out[0], undef);
  2046. my $out = '? ';
  2047. if (not defined $val or $val =~ /[\n\r]/) {
  2048. # Can do nothing better
  2049. } elsif ($val =~ /\s/) {
  2050. my $found;
  2051. foreach $l (split //, qq/\"\'\#\|/) {
  2052. $out = "$l$val$l ", last if (index $val, $l) == -1;
  2053. }
  2054. } else {
  2055. $out = "=$val ";
  2056. }
  2057. # Default to value if one completion, to question if many
  2058. $rl_attribs->{completer_terminator_character} = (@out == 1 ? $out : '? ');
  2059. return sort @out;
  2060. }
  2061. return $term->filename_list($text); # filenames
  2062. }
  2063. sub end_report {
  2064. print $OUT "Use `q' to quit or `R' to restart. `h q' for details.\n"
  2065. }
  2066. END {
  2067. $finished = $inhibit_exit; # So that some keys may be disabled.
  2068. # Do not stop in at_exit() and destructors on exit:
  2069. $DB::single = !$exiting && !$runnonstop;
  2070. DB::fake::at_exit() unless $exiting or $runnonstop;
  2071. }
  2072. package DB::fake;
  2073. sub at_exit {
  2074. "Debugged program terminated. Use `q' to quit or `R' to restart.";
  2075. }
  2076. package DB; # Do not trace this 1; below!
  2077. 1;