PageRenderTime 69ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/IronPython_Main/Runtime/Tests/LinqDlrTests/testenv/perl/lib/perl5db.pl

#
Perl | 2708 lines | 2336 code | 122 blank | 250 comment | 346 complexity | cd6c6e1e1013ccf2cd73d23aa4915f3c MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. package DB;
  2. # Debugger for Perl 5.00x; perl5db.pl patch level:
  3. $VERSION = 1.07;
  4. $header = "perl5db.pl version $VERSION";
  5. #
  6. # This file is automatically included if you do perl -d.
  7. # It's probably not useful to include this yourself.
  8. #
  9. # Perl supplies the values for %sub. It effectively inserts
  10. # a &DB'DB(); in front of every place that can have a
  11. # breakpoint. Instead of a subroutine call it calls &DB::sub with
  12. # $DB::sub being the called subroutine. It also inserts a BEGIN
  13. # {require 'perl5db.pl'} before the first line.
  14. #
  15. # After each `require'd file is compiled, but before it is executed, a
  16. # call to DB::postponed($main::{'_<'.$filename}) is emulated. Here the
  17. # $filename is the expanded name of the `require'd file (as found as
  18. # value of %INC).
  19. #
  20. # Additional services from Perl interpreter:
  21. #
  22. # if caller() is called from the package DB, it provides some
  23. # additional data.
  24. #
  25. # The array @{$main::{'_<'.$filename}} is the line-by-line contents of
  26. # $filename.
  27. #
  28. # The hash %{'_<'.$filename} contains breakpoints and action (it is
  29. # keyed by line number), and individual entries are settable (as
  30. # opposed to the whole hash). Only true/false is important to the
  31. # interpreter, though the values used by perl5db.pl have the form
  32. # "$break_condition\0$action". Values are magical in numeric context.
  33. #
  34. # The scalar ${'_<'.$filename} contains $filename.
  35. #
  36. # Note that no subroutine call is possible until &DB::sub is defined
  37. # (for subroutines defined outside of the package DB). In fact the same is
  38. # true if $deep is not defined.
  39. #
  40. # $Log: perldb.pl,v $
  41. #
  42. # At start reads $rcfile that may set important options. This file
  43. # may define a subroutine &afterinit that will be executed after the
  44. # debugger is initialized.
  45. #
  46. # After $rcfile is read reads environment variable PERLDB_OPTS and parses
  47. # it as a rest of `O ...' line in debugger prompt.
  48. #
  49. # The options that can be specified only at startup:
  50. # [To set in $rcfile, call &parse_options("optionName=new_value").]
  51. #
  52. # TTY - the TTY to use for debugging i/o.
  53. #
  54. # noTTY - if set, goes in NonStop mode. On interrupt if TTY is not set
  55. # uses the value of noTTY or "/tmp/perldbtty$$" to find TTY using
  56. # Term::Rendezvous. Current variant is to have the name of TTY in this
  57. # file.
  58. #
  59. # ReadLine - If false, dummy ReadLine is used, so you can debug
  60. # ReadLine applications.
  61. #
  62. # NonStop - if true, no i/o is performed until interrupt.
  63. #
  64. # LineInfo - file or pipe to print line number info to. If it is a
  65. # pipe, a short "emacs like" message is used.
  66. #
  67. # RemotePort - host:port to connect to on remote host for remote debugging.
  68. #
  69. # Example $rcfile: (delete leading hashes!)
  70. #
  71. # &parse_options("NonStop=1 LineInfo=db.out");
  72. # sub afterinit { $trace = 1; }
  73. #
  74. # The script will run without human intervention, putting trace
  75. # information into db.out. (If you interrupt it, you would better
  76. # reset LineInfo to something "interactive"!)
  77. #
  78. ##################################################################
  79. # Enhanced by ilya@math.ohio-state.edu (Ilya Zakharevich)
  80. # Latest version available: ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl
  81. # modified Perl debugger, to be run from Emacs in perldb-mode
  82. # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
  83. # Johan Vromans -- upgrade to 4.0 pl 10
  84. # Ilya Zakharevich -- patches after 5.001 (and some before ;-)
  85. # Changelog:
  86. # A lot of things changed after 0.94. First of all, core now informs
  87. # debugger about entry into XSUBs, overloaded operators, tied operations,
  88. # BEGIN and END. Handy with `O f=2'.
  89. # This can make debugger a little bit too verbose, please be patient
  90. # and report your problems promptly.
  91. # Now the option frame has 3 values: 0,1,2.
  92. # Note that if DESTROY returns a reference to the object (or object),
  93. # the deletion of data may be postponed until the next function call,
  94. # due to the need to examine the return value.
  95. # Changes: 0.95: `v' command shows versions.
  96. # Changes: 0.96: `v' command shows version of readline.
  97. # primitive completion works (dynamic variables, subs for `b' and `l',
  98. # options). Can `p %var'
  99. # Better help (`h <' now works). New commands <<, >>, {, {{.
  100. # {dump|print}_trace() coded (to be able to do it from <<cmd).
  101. # `c sub' documented.
  102. # At last enough magic combined to stop after the end of debuggee.
  103. # !! should work now (thanks to Emacs bracket matching an extra
  104. # `]' in a regexp is caught).
  105. # `L', `D' and `A' span files now (as documented).
  106. # Breakpoints in `require'd code are possible (used in `R').
  107. # Some additional words on internal work of debugger.
  108. # `b load filename' implemented.
  109. # `b postpone subr' implemented.
  110. # now only `q' exits debugger (overwriteable on $inhibit_exit).
  111. # When restarting debugger breakpoints/actions persist.
  112. # Buglet: When restarting debugger only one breakpoint/action per
  113. # autoloaded function persists.
  114. # Changes: 0.97: NonStop will not stop in at_exit().
  115. # Option AutoTrace implemented.
  116. # Trace printed differently if frames are printed too.
  117. # new `inhibitExit' option.
  118. # printing of a very long statement interruptible.
  119. # Changes: 0.98: New command `m' for printing possible methods
  120. # 'l -' is a synonim for `-'.
  121. # Cosmetic bugs in printing stack trace.
  122. # `frame' & 8 to print "expanded args" in stack trace.
  123. # Can list/break in imported subs.
  124. # new `maxTraceLen' option.
  125. # frame & 4 and frame & 8 granted.
  126. # new command `m'
  127. # nonstoppable lines do not have `:' near the line number.
  128. # `b compile subname' implemented.
  129. # Will not use $` any more.
  130. # `-' behaves sane now.
  131. # Changes: 0.99: Completion for `f', `m'.
  132. # `m' will remove duplicate names instead of duplicate functions.
  133. # `b load' strips trailing whitespace.
  134. # completion ignores leading `|'; takes into account current package
  135. # when completing a subroutine name (same for `l').
  136. # Changes: 1.07: Many fixed by tchrist 13-March-2000
  137. # BUG FIXES:
  138. # + Added bare mimimal security checks on perldb rc files, plus
  139. # comments on what else is needed.
  140. # + Fixed the ornaments that made "|h" completely unusable.
  141. # They are not used in print_help if they will hurt. Strip pod
  142. # if we're paging to less.
  143. # + Fixed mis-formatting of help messages caused by ornaments
  144. # to restore Larry's original formatting.
  145. # + Fixed many other formatting errors. The code is still suboptimal,
  146. # and needs a lot of work at restructuing. It's also misindented
  147. # in many places.
  148. # + Fixed bug where trying to look at an option like your pager
  149. # shows "1".
  150. # + Fixed some $? processing. Note: if you use csh or tcsh, you will
  151. # lose. You should consider shell escapes not using their shell,
  152. # or else not caring about detailed status. This should really be
  153. # unified into one place, too.
  154. # + Fixed bug where invisible trailing whitespace on commands hoses you,
  155. # tricking Perl into thinking you wern't calling a debugger command!
  156. # + Fixed bug where leading whitespace on commands hoses you. (One
  157. # suggests a leading semicolon or any other irrelevant non-whitespace
  158. # to indicate literal Perl code.)
  159. # + Fixed bugs that ate warnings due to wrong selected handle.
  160. # + Fixed a precedence bug on signal stuff.
  161. # + Fixed some unseemly wording.
  162. # + Fixed bug in help command trying to call perl method code.
  163. # + Fixed to call dumpvar from exception handler. SIGPIPE killed us.
  164. # ENHANCEMENTS:
  165. # + Added some comments. This code is still nasty spaghetti.
  166. # + Added message if you clear your pre/post command stacks which was
  167. # very easy to do if you just typed a bare >, <, or {. (A command
  168. # without an argument should *never* be a destructive action; this
  169. # API is fundamentally screwed up; likewise option setting, which
  170. # is equally buggered.)
  171. # + Added command stack dump on argument of "?" for >, <, or {.
  172. # + Added a semi-built-in doc viewer command that calls man with the
  173. # proper %Config::Config path (and thus gets caching, man -k, etc),
  174. # or else perldoc on obstreperous platforms.
  175. # + Added to and rearranged the help information.
  176. # + Detected apparent misuse of { ... } to declare a block; this used
  177. # to work but now is a command, and mysteriously gave no complaint.
  178. ####################################################################
  179. # Needed for the statement after exec():
  180. BEGIN { $ini_warn = $^W; $^W = 0 } # Switch compilation warnings off until another BEGIN.
  181. local($^W) = 0; # Switch run-time warnings off during init.
  182. warn ( # Do not ;-)
  183. $dumpvar::hashDepth,
  184. $dumpvar::arrayDepth,
  185. $dumpvar::dumpDBFiles,
  186. $dumpvar::dumpPackages,
  187. $dumpvar::quoteHighBit,
  188. $dumpvar::printUndef,
  189. $dumpvar::globPrint,
  190. $dumpvar::usageOnly,
  191. @ARGS,
  192. $Carp::CarpLevel,
  193. $panic,
  194. $second_time,
  195. ) if 0;
  196. # Command-line + PERLLIB:
  197. @ini_INC = @INC;
  198. # $prevwarn = $prevdie = $prevbus = $prevsegv = ''; # Does not help?!
  199. $trace = $signal = $single = 0; # Uninitialized warning suppression
  200. # (local $^W cannot help - other packages!).
  201. $inhibit_exit = $option{PrintRet} = 1;
  202. @options = qw(hashDepth arrayDepth DumpDBFiles DumpPackages DumpReused
  203. compactDump veryCompact quote HighBit undefPrint
  204. globPrint PrintRet UsageOnly frame AutoTrace
  205. TTY noTTY ReadLine NonStop LineInfo maxTraceLen
  206. recallCommand ShellBang pager tkRunning ornaments
  207. signalLevel warnLevel dieLevel inhibit_exit
  208. ImmediateStop bareStringify
  209. RemotePort);
  210. %optionVars = (
  211. hashDepth => \$dumpvar::hashDepth,
  212. arrayDepth => \$dumpvar::arrayDepth,
  213. DumpDBFiles => \$dumpvar::dumpDBFiles,
  214. DumpPackages => \$dumpvar::dumpPackages,
  215. DumpReused => \$dumpvar::dumpReused,
  216. HighBit => \$dumpvar::quoteHighBit,
  217. undefPrint => \$dumpvar::printUndef,
  218. globPrint => \$dumpvar::globPrint,
  219. UsageOnly => \$dumpvar::usageOnly,
  220. bareStringify => \$dumpvar::bareStringify,
  221. frame => \$frame,
  222. AutoTrace => \$trace,
  223. inhibit_exit => \$inhibit_exit,
  224. maxTraceLen => \$maxtrace,
  225. ImmediateStop => \$ImmediateStop,
  226. RemotePort => \$remoteport,
  227. );
  228. %optionAction = (
  229. compactDump => \&dumpvar::compactDump,
  230. veryCompact => \&dumpvar::veryCompact,
  231. quote => \&dumpvar::quote,
  232. TTY => \&TTY,
  233. noTTY => \&noTTY,
  234. ReadLine => \&ReadLine,
  235. NonStop => \&NonStop,
  236. LineInfo => \&LineInfo,
  237. recallCommand => \&recallCommand,
  238. ShellBang => \&shellBang,
  239. pager => \&pager,
  240. signalLevel => \&signalLevel,
  241. warnLevel => \&warnLevel,
  242. dieLevel => \&dieLevel,
  243. tkRunning => \&tkRunning,
  244. ornaments => \&ornaments,
  245. RemotePort => \&RemotePort,
  246. );
  247. %optionRequire = (
  248. compactDump => 'dumpvar.pl',
  249. veryCompact => 'dumpvar.pl',
  250. quote => 'dumpvar.pl',
  251. );
  252. # These guys may be defined in $ENV{PERL5DB} :
  253. $rl = 1 unless defined $rl;
  254. $warnLevel = 0 unless defined $warnLevel;
  255. $dieLevel = 0 unless defined $dieLevel;
  256. $signalLevel = 1 unless defined $signalLevel;
  257. $pre = [] unless defined $pre;
  258. $post = [] unless defined $post;
  259. $pretype = [] unless defined $pretype;
  260. warnLevel($warnLevel);
  261. dieLevel($dieLevel);
  262. signalLevel($signalLevel);
  263. &pager(
  264. (defined($ENV{PAGER})
  265. ? $ENV{PAGER}
  266. : ($^O eq 'os2'
  267. ? 'cmd /c more'
  268. : 'more'))) unless defined $pager;
  269. setman();
  270. &recallCommand("!") unless defined $prc;
  271. &shellBang("!") unless defined $psh;
  272. $maxtrace = 400 unless defined $maxtrace;
  273. if (-e "/dev/tty") { # this is the wrong metric!
  274. $rcfile=".perldb";
  275. } else {
  276. $rcfile="perldb.ini";
  277. }
  278. # This isn't really safe, because there's a race
  279. # between checking and opening. The solution is to
  280. # open and fstat the handle, but then you have to read and
  281. # eval the contents. But then the silly thing gets
  282. # your lexical scope, which is unfortunately at best.
  283. sub safe_do {
  284. my $file = shift;
  285. # Just exactly what part of the word "CORE::" don't you understand?
  286. local $SIG{__WARN__};
  287. local $SIG{__DIE__};
  288. unless (is_safe_file($file)) {
  289. CORE::warn <<EO_GRIPE;
  290. perldb: Must not source insecure rcfile $file.
  291. You or the superuser must be the owner, and it must not
  292. be writable by anyone but its owner.
  293. EO_GRIPE
  294. return;
  295. }
  296. do $file;
  297. CORE::warn("perldb: couldn't parse $file: $@") if $@;
  298. }
  299. # Verifies that owner is either real user or superuser and that no
  300. # one but owner may write to it. This function is of limited use
  301. # when called on a path instead of upon a handle, because there are
  302. # no guarantees that filename (by dirent) whose file (by ino) is
  303. # eventually accessed is the same as the one tested.
  304. # Assumes that the file's existence is not in doubt.
  305. sub is_safe_file {
  306. my $path = shift;
  307. stat($path) || return; # mysteriously vaporized
  308. my($dev,$ino,$mode,$nlink,$uid,$gid) = stat(_);
  309. return 0 if $uid != 0 && $uid != $<;
  310. return 0 if $mode & 022;
  311. return 1;
  312. }
  313. if (-f $rcfile) {
  314. safe_do("./$rcfile");
  315. }
  316. elsif (defined $ENV{HOME} && -f "$ENV{HOME}/$rcfile") {
  317. safe_do("$ENV{HOME}/$rcfile");
  318. }
  319. elsif (defined $ENV{LOGDIR} && -f "$ENV{LOGDIR}/$rcfile") {
  320. safe_do("$ENV{LOGDIR}/$rcfile");
  321. }
  322. if (defined $ENV{PERLDB_OPTS}) {
  323. parse_options($ENV{PERLDB_OPTS});
  324. }
  325. # Here begin the unreadable code. It needs fixing.
  326. if (exists $ENV{PERLDB_RESTART}) {
  327. delete $ENV{PERLDB_RESTART};
  328. # $restart = 1;
  329. @hist = get_list('PERLDB_HIST');
  330. %break_on_load = get_list("PERLDB_ON_LOAD");
  331. %postponed = get_list("PERLDB_POSTPONE");
  332. my @had_breakpoints= get_list("PERLDB_VISITED");
  333. for (0 .. $#had_breakpoints) {
  334. my %pf = get_list("PERLDB_FILE_$_");
  335. $postponed_file{$had_breakpoints[$_]} = \%pf if %pf;
  336. }
  337. my %opt = get_list("PERLDB_OPT");
  338. my ($opt,$val);
  339. while (($opt,$val) = each %opt) {
  340. $val =~ s/[\\\']/\\$1/g;
  341. parse_options("$opt'$val'");
  342. }
  343. @INC = get_list("PERLDB_INC");
  344. @ini_INC = @INC;
  345. $pretype = [get_list("PERLDB_PRETYPE")];
  346. $pre = [get_list("PERLDB_PRE")];
  347. $post = [get_list("PERLDB_POST")];
  348. @typeahead = get_list("PERLDB_TYPEAHEAD", @typeahead);
  349. }
  350. if ($notty) {
  351. $runnonstop = 1;
  352. } else {
  353. # Is Perl being run from a slave editor or graphical debugger?
  354. $slave_editor = ((defined $main::ARGV[0]) and ($main::ARGV[0] eq '-emacs'));
  355. $rl = 0, shift(@main::ARGV) if $slave_editor;
  356. #require Term::ReadLine;
  357. if ($^O eq 'cygwin') {
  358. # /dev/tty is binary. use stdin for textmode
  359. undef $console;
  360. } elsif (-e "/dev/tty") {
  361. $console = "/dev/tty";
  362. } elsif ($^O eq 'dos' or -e "con" or $^O eq 'MSWin32') {
  363. $console = "con";
  364. } elsif ($^O eq 'MacOS') {
  365. if ($MacPerl::Version !~ /MPW/) {
  366. $console = "Dev:Console:Perl Debug"; # Separate window for application
  367. } else {
  368. $console = "Dev:Console";
  369. }
  370. } else {
  371. $console = "sys\$command";
  372. }
  373. if (($^O eq 'MSWin32') and ($slave_editor or defined $ENV{EMACS})) {
  374. $console = undef;
  375. }
  376. # Around a bug:
  377. if (defined $ENV{OS2_SHELL} and ($slave_editor or $ENV{WINDOWID})) { # In OS/2
  378. $console = undef;
  379. }
  380. if ($^O eq 'epoc') {
  381. $console = undef;
  382. }
  383. $console = $tty if defined $tty;
  384. if (defined $remoteport) {
  385. require IO::Socket;
  386. $OUT = new IO::Socket::INET( Timeout => '10',
  387. PeerAddr => $remoteport,
  388. Proto => 'tcp',
  389. );
  390. if (!$OUT) { die "Unable to connect to remote host: $remoteport\n"; }
  391. $IN = $OUT;
  392. }
  393. else {
  394. if (defined $console) {
  395. open(IN,"+<$console") || open(IN,"<$console") || open(IN,"<&STDIN");
  396. open(OUT,"+>$console") || open(OUT,">$console") || open(OUT,">&STDERR")
  397. || open(OUT,">&STDOUT"); # so we don't dongle stdout
  398. } else {
  399. open(IN,"<&STDIN");
  400. open(OUT,">&STDERR") || open(OUT,">&STDOUT"); # so we don't dongle stdout
  401. $console = 'STDIN/OUT';
  402. }
  403. # so open("|more") can read from STDOUT and so we don't dingle stdin
  404. $IN = \*IN;
  405. $OUT = \*OUT;
  406. }
  407. select($OUT);
  408. $| = 1; # for DB::OUT
  409. select(STDOUT);
  410. $LINEINFO = $OUT unless defined $LINEINFO;
  411. $lineinfo = $console unless defined $lineinfo;
  412. $| = 1; # for real STDOUT
  413. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  414. unless ($runnonstop) {
  415. print $OUT "\nLoading DB routines from $header\n";
  416. print $OUT ("Editor support ",
  417. $slave_editor ? "enabled" : "available",
  418. ".\n");
  419. print $OUT "\nEnter h or `h h' for help, or `$doccmd perldebug' for more help.\n\n";
  420. }
  421. }
  422. @ARGS = @ARGV;
  423. for (@args) {
  424. s/\'/\\\'/g;
  425. s/(.*)/'$1'/ unless /^-?[\d.]+$/;
  426. }
  427. if (defined &afterinit) { # May be defined in $rcfile
  428. &afterinit();
  429. }
  430. $I_m_init = 1;
  431. ############################################################ Subroutines
  432. sub DB {
  433. # _After_ the perl program is compiled, $single is set to 1:
  434. if ($single and not $second_time++) {
  435. if ($runnonstop) { # Disable until signal
  436. for ($i=0; $i <= $stack_depth; ) {
  437. $stack[$i++] &= ~1;
  438. }
  439. $single = 0;
  440. # return; # Would not print trace!
  441. } elsif ($ImmediateStop) {
  442. $ImmediateStop = 0;
  443. $signal = 1;
  444. }
  445. }
  446. $runnonstop = 0 if $single or $signal; # Disable it if interactive.
  447. &save;
  448. ($package, $filename, $line) = caller;
  449. $filename_ini = $filename;
  450. $usercontext = '($@, $!, $^E, $,, $/, $\, $^W) = @saved;' .
  451. "package $package;"; # this won't let them modify, alas
  452. local(*dbline) = $main::{'_<' . $filename};
  453. $max = $#dbline;
  454. if (($stop,$action) = split(/\0/,$dbline{$line})) {
  455. if ($stop eq '1') {
  456. $signal |= 1;
  457. } elsif ($stop) {
  458. $evalarg = "\$DB::signal |= 1 if do {$stop}"; &eval;
  459. $dbline{$line} =~ s/;9($|\0)/$1/;
  460. }
  461. }
  462. my $was_signal = $signal;
  463. if ($trace & 2) {
  464. for (my $n = 0; $n <= $#to_watch; $n++) {
  465. $evalarg = $to_watch[$n];
  466. local $onetimeDump; # Do not output results
  467. my ($val) = &eval; # Fix context (&eval is doing array)?
  468. $val = ( (defined $val) ? "'$val'" : 'undef' );
  469. if ($val ne $old_watch[$n]) {
  470. $signal = 1;
  471. print $OUT <<EOP;
  472. Watchpoint $n:\t$to_watch[$n] changed:
  473. old value:\t$old_watch[$n]
  474. new value:\t$val
  475. EOP
  476. $old_watch[$n] = $val;
  477. }
  478. }
  479. }
  480. if ($trace & 4) { # User-installed watch
  481. return if watchfunction($package, $filename, $line)
  482. and not $single and not $was_signal and not ($trace & ~4);
  483. }
  484. $was_signal = $signal;
  485. $signal = 0;
  486. if ($single || ($trace & 1) || $was_signal) {
  487. if ($slave_editor) {
  488. $position = "\032\032$filename:$line:0\n";
  489. print $LINEINFO $position;
  490. } elsif ($package eq 'DB::fake') {
  491. $term || &setterm;
  492. print_help(<<EOP);
  493. Debugged program terminated. Use B<q> to quit or B<R> to restart,
  494. use B<O> I<inhibit_exit> to avoid stopping after program termination,
  495. B<h q>, B<h R> or B<h O> to get additional info.
  496. EOP
  497. $package = 'main';
  498. $usercontext = '($@, $!, $^E, $,, $/, $\, $^W) = @saved;' .
  499. "package $package;"; # this won't let them modify, alas
  500. } else {
  501. $sub =~ s/\'/::/;
  502. $prefix = $sub =~ /::/ ? "" : "${'package'}::";
  503. $prefix .= "$sub($filename:";
  504. $after = ($dbline[$line] =~ /\n$/ ? '' : "\n");
  505. if (length($prefix) > 30) {
  506. $position = "$prefix$line):\n$line:\t$dbline[$line]$after";
  507. $prefix = "";
  508. $infix = ":\t";
  509. } else {
  510. $infix = "):\t";
  511. $position = "$prefix$line$infix$dbline[$line]$after";
  512. }
  513. if ($frame) {
  514. print $LINEINFO ' ' x $stack_depth, "$line:\t$dbline[$line]$after";
  515. } else {
  516. print $LINEINFO $position;
  517. }
  518. for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) { #{ vi
  519. last if $dbline[$i] =~ /^\s*[\;\}\#\n]/;
  520. last if $signal;
  521. $after = ($dbline[$i] =~ /\n$/ ? '' : "\n");
  522. $incr_pos = "$prefix$i$infix$dbline[$i]$after";
  523. $position .= $incr_pos;
  524. if ($frame) {
  525. print $LINEINFO ' ' x $stack_depth, "$i:\t$dbline[$i]$after";
  526. } else {
  527. print $LINEINFO $incr_pos;
  528. }
  529. }
  530. }
  531. }
  532. $evalarg = $action, &eval if $action;
  533. if ($single || $was_signal) {
  534. local $level = $level + 1;
  535. foreach $evalarg (@$pre) {
  536. &eval;
  537. }
  538. print $OUT $stack_depth . " levels deep in subroutine calls!\n"
  539. if $single & 4;
  540. $start = $line;
  541. $incr = -1; # for backward motion.
  542. @typeahead = (@$pretype, @typeahead);
  543. CMD:
  544. while (($term || &setterm),
  545. ($term_pid == $$ or &resetterm),
  546. defined ($cmd=&readline(" DB" . ('<' x $level) .
  547. ($#hist+1) . ('>' x $level) .
  548. " ")))
  549. {
  550. $single = 0;
  551. $signal = 0;
  552. $cmd =~ s/\\$/\n/ && do {
  553. $cmd .= &readline(" cont: ");
  554. redo CMD;
  555. };
  556. $cmd =~ /^$/ && ($cmd = $laststep);
  557. push(@hist,$cmd) if length($cmd) > 1;
  558. PIPE: {
  559. $cmd =~ s/^\s+//s; # trim annoying leading whitespace
  560. $cmd =~ s/\s+$//s; # trim annoying trailing whitespace
  561. ($i) = split(/\s+/,$cmd);
  562. if ($alias{$i}) {
  563. # squelch the sigmangler
  564. local $SIG{__DIE__};
  565. local $SIG{__WARN__};
  566. eval "\$cmd =~ $alias{$i}";
  567. if ($@) {
  568. print $OUT "Couldn't evaluate `$i' alias: $@";
  569. next CMD;
  570. }
  571. }
  572. $cmd =~ /^q$/ && ($fall_off_end = 1) && exit $?;
  573. $cmd =~ /^h$/ && do {
  574. print_help($help);
  575. next CMD; };
  576. $cmd =~ /^h\s+h$/ && do {
  577. print_help($summary);
  578. next CMD; };
  579. # support long commands; otherwise bogus errors
  580. # happen when you ask for h on <CR> for example
  581. $cmd =~ /^h\s+(\S.*)$/ && do {
  582. my $asked = $1; # for proper errmsg
  583. my $qasked = quotemeta($asked); # for searching
  584. # XXX: finds CR but not <CR>
  585. if ($help =~ /^<?(?:[IB]<)$qasked/m) {
  586. while ($help =~ /^(<?(?:[IB]<)$qasked([\s\S]*?)\n)(?!\s)/mg) {
  587. print_help($1);
  588. }
  589. } else {
  590. print_help("B<$asked> is not a debugger command.\n");
  591. }
  592. next CMD; };
  593. $cmd =~ /^t$/ && do {
  594. $trace ^= 1;
  595. print $OUT "Trace = " .
  596. (($trace & 1) ? "on" : "off" ) . "\n";
  597. next CMD; };
  598. $cmd =~ /^S(\s+(!)?(.+))?$/ && do {
  599. $Srev = defined $2; $Spatt = $3; $Snocheck = ! defined $1;
  600. foreach $subname (sort(keys %sub)) {
  601. if ($Snocheck or $Srev^($subname =~ /$Spatt/)) {
  602. print $OUT $subname,"\n";
  603. }
  604. }
  605. next CMD; };
  606. $cmd =~ /^v$/ && do {
  607. list_versions(); next CMD};
  608. $cmd =~ s/^X\b/V $package/;
  609. $cmd =~ /^V$/ && do {
  610. $cmd = "V $package"; };
  611. $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
  612. local ($savout) = select($OUT);
  613. $packname = $1;
  614. @vars = split(' ',$2);
  615. do 'dumpvar.pl' unless defined &main::dumpvar;
  616. if (defined &main::dumpvar) {
  617. local $frame = 0;
  618. local $doret = -2;
  619. # must detect sigpipe failures
  620. eval { &main::dumpvar($packname,@vars) };
  621. if ($@) {
  622. die unless $@ =~ /dumpvar print failed/;
  623. }
  624. } else {
  625. print $OUT "dumpvar.pl not available.\n";
  626. }
  627. select ($savout);
  628. next CMD; };
  629. $cmd =~ s/^x\b/ / && do { # So that will be evaled
  630. $onetimeDump = 'dump'; };
  631. $cmd =~ s/^m\s+([\w:]+)\s*$/ / && do {
  632. methods($1); next CMD};
  633. $cmd =~ s/^m\b/ / && do { # So this will be evaled
  634. $onetimeDump = 'methods'; };
  635. $cmd =~ /^f\b\s*(.*)/ && do {
  636. $file = $1;
  637. $file =~ s/\s+$//;
  638. if (!$file) {
  639. print $OUT "The old f command is now the r command.\n";
  640. print $OUT "The new f command switches filenames.\n";
  641. next CMD;
  642. }
  643. if (!defined $main::{'_<' . $file}) {
  644. if (($try) = grep(m#^_<.*$file#, keys %main::)) {{
  645. $try = substr($try,2);
  646. print $OUT "Choosing $try matching `$file':\n";
  647. $file = $try;
  648. }}
  649. }
  650. if (!defined $main::{'_<' . $file}) {
  651. print $OUT "No file matching `$file' is loaded.\n";
  652. next CMD;
  653. } elsif ($file ne $filename) {
  654. *dbline = $main::{'_<' . $file};
  655. $max = $#dbline;
  656. $filename = $file;
  657. $start = 1;
  658. $cmd = "l";
  659. } else {
  660. print $OUT "Already in $file.\n";
  661. next CMD;
  662. }
  663. };
  664. $cmd =~ s/^l\s+-\s*$/-/;
  665. $cmd =~ /^([lb])\b\s*(\$.*)/s && do {
  666. $evalarg = $2;
  667. my ($s) = &eval;
  668. print($OUT "Error: $@\n"), next CMD if $@;
  669. $s = CvGV_name($s);
  670. print($OUT "Interpreted as: $1 $s\n");
  671. $cmd = "$1 $s";
  672. };
  673. $cmd =~ /^l\b\s*([\':A-Za-z_][\':\w]*(\[.*\])?)/s && do {
  674. $subname = $1;
  675. $subname =~ s/\'/::/;
  676. $subname = $package."::".$subname
  677. unless $subname =~ /::/;
  678. $subname = "main".$subname if substr($subname,0,2) eq "::";
  679. @pieces = split(/:/,find_sub($subname) || $sub{$subname});
  680. $subrange = pop @pieces;
  681. $file = join(':', @pieces);
  682. if ($file ne $filename) {
  683. print $OUT "Switching to file '$file'.\n"
  684. unless $slave_editor;
  685. *dbline = $main::{'_<' . $file};
  686. $max = $#dbline;
  687. $filename = $file;
  688. }
  689. if ($subrange) {
  690. if (eval($subrange) < -$window) {
  691. $subrange =~ s/-.*/+/;
  692. }
  693. $cmd = "l $subrange";
  694. } else {
  695. print $OUT "Subroutine $subname not found.\n";
  696. next CMD;
  697. } };
  698. $cmd =~ /^\.$/ && do {
  699. $incr = -1; # for backward motion.
  700. $start = $line;
  701. $filename = $filename_ini;
  702. *dbline = $main::{'_<' . $filename};
  703. $max = $#dbline;
  704. print $LINEINFO $position;
  705. next CMD };
  706. $cmd =~ /^w\b\s*(\d*)$/ && do {
  707. $incr = $window - 1;
  708. $start = $1 if $1;
  709. $start -= $preview;
  710. #print $OUT 'l ' . $start . '-' . ($start + $incr);
  711. $cmd = 'l ' . $start . '-' . ($start + $incr); };
  712. $cmd =~ /^-$/ && do {
  713. $start -= $incr + $window + 1;
  714. $start = 1 if $start <= 0;
  715. $incr = $window - 1;
  716. $cmd = 'l ' . ($start) . '+'; };
  717. $cmd =~ /^l$/ && do {
  718. $incr = $window - 1;
  719. $cmd = 'l ' . $start . '-' . ($start + $incr); };
  720. $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
  721. $start = $1 if $1;
  722. $incr = $2;
  723. $incr = $window - 1 unless $incr;
  724. $cmd = 'l ' . $start . '-' . ($start + $incr); };
  725. $cmd =~ /^l\b\s*((-?[\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
  726. $end = (!defined $2) ? $max : ($4 ? $4 : $2);
  727. $end = $max if $end > $max;
  728. $i = $2;
  729. $i = $line if $i eq '.';
  730. $i = 1 if $i < 1;
  731. $incr = $end - $i;
  732. if ($slave_editor) {
  733. print $OUT "\032\032$filename:$i:0\n";
  734. $i = $end;
  735. } else {
  736. for (; $i <= $end; $i++) {
  737. ($stop,$action) = split(/\0/, $dbline{$i});
  738. $arrow = ($i==$line
  739. and $filename eq $filename_ini)
  740. ? '==>'
  741. : ($dbline[$i]+0 ? ':' : ' ') ;
  742. $arrow .= 'b' if $stop;
  743. $arrow .= 'a' if $action;
  744. print $OUT "$i$arrow\t", $dbline[$i];
  745. $i++, last if $signal;
  746. }
  747. print $OUT "\n" unless $dbline[$i-1] =~ /\n$/;
  748. }
  749. $start = $i; # remember in case they want more
  750. $start = $max if $start > $max;
  751. next CMD; };
  752. $cmd =~ /^D$/ && do {
  753. print $OUT "Deleting all breakpoints...\n";
  754. my $file;
  755. for $file (keys %had_breakpoints) {
  756. local *dbline = $main::{'_<' . $file};
  757. my $max = $#dbline;
  758. my $was;
  759. for ($i = 1; $i <= $max ; $i++) {
  760. if (defined $dbline{$i}) {
  761. $dbline{$i} =~ s/^[^\0]+//;
  762. if ($dbline{$i} =~ s/^\0?$//) {
  763. delete $dbline{$i};
  764. }
  765. }
  766. }
  767. if (not $had_breakpoints{$file} &= ~1) {
  768. delete $had_breakpoints{$file};
  769. }
  770. }
  771. undef %postponed;
  772. undef %postponed_file;
  773. undef %break_on_load;
  774. next CMD; };
  775. $cmd =~ /^L$/ && do {
  776. my $file;
  777. for $file (keys %had_breakpoints) {
  778. local *dbline = $main::{'_<' . $file};
  779. my $max = $#dbline;
  780. my $was;
  781. for ($i = 1; $i <= $max; $i++) {
  782. if (defined $dbline{$i}) {
  783. print $OUT "$file:\n" unless $was++;
  784. print $OUT " $i:\t", $dbline[$i];
  785. ($stop,$action) = split(/\0/, $dbline{$i});
  786. print $OUT " break if (", $stop, ")\n"
  787. if $stop;
  788. print $OUT " action: ", $action, "\n"
  789. if $action;
  790. last if $signal;
  791. }
  792. }
  793. }
  794. if (%postponed) {
  795. print $OUT "Postponed breakpoints in subroutines:\n";
  796. my $subname;
  797. for $subname (keys %postponed) {
  798. print $OUT " $subname\t$postponed{$subname}\n";
  799. last if $signal;
  800. }
  801. }
  802. my @have = map { # Combined keys
  803. keys %{$postponed_file{$_}}
  804. } keys %postponed_file;
  805. if (@have) {
  806. print $OUT "Postponed breakpoints in files:\n";
  807. my ($file, $line);
  808. for $file (keys %postponed_file) {
  809. my $db = $postponed_file{$file};
  810. print $OUT " $file:\n";
  811. for $line (sort {$a <=> $b} keys %$db) {
  812. print $OUT " $line:\n";
  813. my ($stop,$action) = split(/\0/, $$db{$line});
  814. print $OUT " break if (", $stop, ")\n"
  815. if $stop;
  816. print $OUT " action: ", $action, "\n"
  817. if $action;
  818. last if $signal;
  819. }
  820. last if $signal;
  821. }
  822. }
  823. if (%break_on_load) {
  824. print $OUT "Breakpoints on load:\n";
  825. my $file;
  826. for $file (keys %break_on_load) {
  827. print $OUT " $file\n";
  828. last if $signal;
  829. }
  830. }
  831. if ($trace & 2) {
  832. print $OUT "Watch-expressions:\n";
  833. my $expr;
  834. for $expr (@to_watch) {
  835. print $OUT " $expr\n";
  836. last if $signal;
  837. }
  838. }
  839. next CMD; };
  840. $cmd =~ /^b\b\s*load\b\s*(.*)/ && do {
  841. my $file = $1; $file =~ s/\s+$//;
  842. {
  843. $break_on_load{$file} = 1;
  844. $break_on_load{$::INC{$file}} = 1 if $::INC{$file};
  845. $file .= '.pm', redo unless $file =~ /\./;
  846. }
  847. $had_breakpoints{$file} |= 1;
  848. print $OUT "Will stop on load of `@{[join '\', `', sort keys %break_on_load]}'.\n";
  849. next CMD; };
  850. $cmd =~ /^b\b\s*(postpone|compile)\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
  851. my $cond = length $3 ? $3 : '1';
  852. my ($subname, $break) = ($2, $1 eq 'postpone');
  853. $subname =~ s/\'/::/g;
  854. $subname = "${'package'}::" . $subname
  855. unless $subname =~ /::/;
  856. $subname = "main".$subname if substr($subname,0,2) eq "::";
  857. $postponed{$subname} = $break
  858. ? "break +0 if $cond" : "compile";
  859. next CMD; };
  860. $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*(?:\[.*\])?)\s*(.*)/ && do {
  861. $subname = $1;
  862. $cond = length $2 ? $2 : '1';
  863. $subname =~ s/\'/::/g;
  864. $subname = "${'package'}::" . $subname
  865. unless $subname =~ /::/;
  866. $subname = "main".$subname if substr($subname,0,2) eq "::";
  867. # Filename below can contain ':'
  868. ($file,$i) = (find_sub($subname) =~ /^(.*):(.*)$/);
  869. $i += 0;
  870. if ($i) {
  871. local $filename = $file;
  872. local *dbline = $main::{'_<' . $filename};
  873. $had_breakpoints{$filename} |= 1;
  874. $max = $#dbline;
  875. ++$i while $dbline[$i] == 0 && $i < $max;
  876. $dbline{$i} =~ s/^[^\0]*/$cond/;
  877. } else {
  878. print $OUT "Subroutine $subname not found.\n";
  879. }
  880. next CMD; };
  881. $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
  882. $i = $1 || $line;
  883. $cond = length $2 ? $2 : '1';
  884. if ($dbline[$i] == 0) {
  885. print $OUT "Line $i not breakable.\n";
  886. } else {
  887. $had_breakpoints{$filename} |= 1;
  888. $dbline{$i} =~ s/^[^\0]*/$cond/;
  889. }
  890. next CMD; };
  891. $cmd =~ /^d\b\s*(\d*)/ && do {
  892. $i = $1 || $line;
  893. if ($dbline[$i] == 0) {
  894. print $OUT "Line $i not breakable.\n";
  895. } else {
  896. $dbline{$i} =~ s/^[^\0]*//;
  897. delete $dbline{$i} if $dbline{$i} eq '';
  898. }
  899. next CMD; };
  900. $cmd =~ /^A$/ && do {
  901. print $OUT "Deleting all actions...\n";
  902. my $file;
  903. for $file (keys %had_breakpoints) {
  904. local *dbline = $main::{'_<' . $file};
  905. my $max = $#dbline;
  906. my $was;
  907. for ($i = 1; $i <= $max ; $i++) {
  908. if (defined $dbline{$i}) {
  909. $dbline{$i} =~ s/\0[^\0]*//;
  910. delete $dbline{$i} if $dbline{$i} eq '';
  911. }
  912. }
  913. unless ($had_breakpoints{$file} &= ~2) {
  914. delete $had_breakpoints{$file};
  915. }
  916. }
  917. next CMD; };
  918. $cmd =~ /^O\s*$/ && do {
  919. for (@options) {
  920. &dump_option($_);
  921. }
  922. next CMD; };
  923. $cmd =~ /^O\s*(\S.*)/ && do {
  924. parse_options($1);
  925. next CMD; };
  926. $cmd =~ /^\<\<\s*(.*)/ && do { # \<\< for CPerl sake: not HERE
  927. push @$pre, action($1);
  928. next CMD; };
  929. $cmd =~ /^>>\s*(.*)/ && do {
  930. push @$post, action($1);
  931. next CMD; };
  932. $cmd =~ /^<\s*(.*)/ && do {
  933. unless ($1) {
  934. print $OUT "All < actions cleared.\n";
  935. $pre = [];
  936. next CMD;
  937. }
  938. if ($1 eq '?') {
  939. unless (@$pre) {
  940. print $OUT "No pre-prompt Perl actions.\n";
  941. next CMD;
  942. }
  943. print $OUT "Perl commands run before each prompt:\n";
  944. for my $action ( @$pre ) {
  945. print $OUT "\t< -- $action\n";
  946. }
  947. next CMD;
  948. }
  949. $pre = [action($1)];
  950. next CMD; };
  951. $cmd =~ /^>\s*(.*)/ && do {
  952. unless ($1) {
  953. print $OUT "All > actions cleared.\n";
  954. $post = [];
  955. next CMD;
  956. }
  957. if ($1 eq '?') {
  958. unless (@$post) {
  959. print $OUT "No post-prompt Perl actions.\n";
  960. next CMD;
  961. }
  962. print $OUT "Perl commands run after each prompt:\n";
  963. for my $action ( @$post ) {
  964. print $OUT "\t> -- $action\n";
  965. }
  966. next CMD;
  967. }
  968. $post = [action($1)];
  969. next CMD; };
  970. $cmd =~ /^\{\{\s*(.*)/ && do {
  971. if ($cmd =~ /^\{.*\}$/ && unbalanced(substr($cmd,2))) {
  972. print $OUT "{{ is now a debugger command\n",
  973. "use `;{{' if you mean Perl code\n";
  974. $cmd = "h {{";
  975. redo CMD;
  976. }
  977. push @$pretype, $1;
  978. next CMD; };
  979. $cmd =~ /^\{\s*(.*)/ && do {
  980. unless ($1) {
  981. print $OUT "All { actions cleared.\n";
  982. $pretype = [];
  983. next CMD;
  984. }
  985. if ($1 eq '?') {
  986. unless (@$pretype) {
  987. print $OUT "No pre-prompt debugger actions.\n";
  988. next CMD;
  989. }
  990. print $OUT "Debugger commands run before each prompt:\n";
  991. for my $action ( @$pretype ) {
  992. print $OUT "\t{ -- $action\n";
  993. }
  994. next CMD;
  995. }
  996. if ($cmd =~ /^\{.*\}$/ && unbalanced(substr($cmd,1))) {
  997. print $OUT "{ is now a debugger command\n",
  998. "use `;{' if you mean Perl code\n";
  999. $cmd = "h {";
  1000. redo CMD;
  1001. }
  1002. $pretype = [$1];
  1003. next CMD; };
  1004. $cmd =~ /^a\b\s*(\d*)\s*(.*)/ && do {
  1005. $i = $1 || $line; $j = $2;
  1006. if (length $j) {
  1007. if ($dbline[$i] == 0) {
  1008. print $OUT "Line $i may not have an action.\n";
  1009. } else {
  1010. $had_breakpoints{$filename} |= 2;
  1011. $dbline{$i} =~ s/\0[^\0]*//;
  1012. $dbline{$i} .= "\0" . action($j);
  1013. }
  1014. } else {
  1015. $dbline{$i} =~ s/\0[^\0]*//;
  1016. delete $dbline{$i} if $dbline{$i} eq '';
  1017. }
  1018. next CMD; };
  1019. $cmd =~ /^n$/ && do {
  1020. end_report(), next CMD if $finished and $level <= 1;
  1021. $single = 2;
  1022. $laststep = $cmd;
  1023. last CMD; };
  1024. $cmd =~ /^s$/ && do {
  1025. end_report(), next CMD if $finished and $level <= 1;
  1026. $single = 1;
  1027. $laststep = $cmd;
  1028. last CMD; };
  1029. $cmd =~ /^c\b\s*([\w:]*)\s*$/ && do {
  1030. end_report(), next CMD if $finished and $level <= 1;
  1031. $subname = $i = $1;
  1032. # Probably not needed, since we finish an interactive
  1033. # sub-session anyway...
  1034. # local $filename = $filename;
  1035. # local *dbline = *dbline; # XXX Would this work?!
  1036. if ($i =~ /\D/) { # subroutine name
  1037. $subname = $package."::".$subname
  1038. unless $subname =~ /::/;
  1039. ($file,$i) = (find_sub($subname) =~ /^(.*):(.*)$/);
  1040. $i += 0;
  1041. if ($i) {
  1042. $filename = $file;
  1043. *dbline = $main::{'_<' . $filename};
  1044. $had_breakpoints{$filename} |= 1;
  1045. $max = $#dbline;
  1046. ++$i while $dbline[$i] == 0 && $i < $max;
  1047. } else {
  1048. print $OUT "Subroutine $subname not found.\n";
  1049. next CMD;
  1050. }
  1051. }
  1052. if ($i) {
  1053. if ($dbline[$i] == 0) {
  1054. print $OUT "Line $i not breakable.\n";
  1055. next CMD;
  1056. }
  1057. $dbline{$i} =~ s/($|\0)/;9$1/; # add one-time-only b.p.
  1058. }
  1059. for ($i=0; $i <= $stack_depth; ) {
  1060. $stack[$i++] &= ~1;
  1061. }
  1062. last CMD; };
  1063. $cmd =~ /^r$/ && do {
  1064. end_report(), next CMD if $finished and $level <= 1;
  1065. $stack[$stack_depth] |= 1;
  1066. $doret = $option{PrintRet} ? $stack_depth - 1 : -2;
  1067. last CMD; };
  1068. $cmd =~ /^R$/ && do {
  1069. print $OUT "Warning: some settings and command-line options may be lost!\n";
  1070. my (@script, @flags, $cl);
  1071. push @flags, '-w' if $ini_warn;
  1072. # Put all the old includes at the start to get
  1073. # the same debugger.
  1074. for (@ini_INC) {
  1075. push @flags, '-I', $_;
  1076. }
  1077. # Arrange for setting the old INC:
  1078. set_list("PERLDB_INC", @ini_INC);
  1079. if ($0 eq '-e') {
  1080. for (1..$#{'::_<-e'}) { # The first line is PERL5DB
  1081. chomp ($cl = ${'::_<-e'}[$_]);
  1082. push @script, '-e', $cl;
  1083. }
  1084. } else {
  1085. @script = $0;
  1086. }
  1087. set_list("PERLDB_HIST",
  1088. $term->Features->{getHistory}
  1089. ? $term->GetHistory : @hist);
  1090. my @had_breakpoints = keys %had_breakpoints;
  1091. set_list("PERLDB_VISITED", @had_breakpoints);
  1092. set_list("PERLDB_OPT", %option);
  1093. set_list("PERLDB_ON_LOAD", %break_on_load);
  1094. my @hard;
  1095. for (0 .. $#had_breakpoints) {
  1096. my $file = $had_breakpoints[$_];
  1097. *dbline = $main::{'_<' . $file};
  1098. next unless %dbline or $postponed_file{$file};
  1099. (push @hard, $file), next
  1100. if $file =~ /^\(eval \d+\)$/;
  1101. my @add;
  1102. @add = %{$postponed_file{$file}}
  1103. if $postponed_file{$file};
  1104. set_list("PERLDB_FILE_$_", %dbline, @add);
  1105. }
  1106. for (@hard) { # Yes, really-really...
  1107. # Find the subroutines in this eval
  1108. *dbline = $main::{'_<' . $_};
  1109. my ($quoted, $sub, %subs, $line) = quotemeta $_;
  1110. for $sub (keys %sub) {
  1111. next unless $sub{$sub} =~ /^$quoted:(\d+)-(\d+)$/;
  1112. $subs{$sub} = [$1, $2];
  1113. }
  1114. unless (%subs) {
  1115. print $OUT
  1116. "No subroutines in $_, ignoring breakpoints.\n";
  1117. next;
  1118. }
  1119. LINES: for $line (keys %dbline) {
  1120. # One breakpoint per sub only:
  1121. my ($offset, $sub, $found);
  1122. SUBS: for $sub (keys %subs) {
  1123. if ($subs{$sub}->[1] >= $line # Not after the subroutine
  1124. and (not defined $offset # Not caught
  1125. or $offset < 0 )) { # or badly caught
  1126. $found = $sub;
  1127. $offset = $line - $subs{$sub}->[0];
  1128. $offset = "+$offset", last SUBS if $offset >= 0;
  1129. }
  1130. }
  1131. if (defined $offset) {
  1132. $postponed{$found} =
  1133. "break $offset if $dbline{$line}";
  1134. } else {
  1135. print $OUT "Breakpoint in $_:$line ignored: after all the subroutines.\n";
  1136. }
  1137. }
  1138. }
  1139. set_list("PERLDB_POSTPONE", %postponed);
  1140. set_list("PERLDB_PRETYPE", @$pretype);
  1141. set_list("PERLDB_PRE", @$pre);
  1142. set_list("PERLDB_POST", @$post);
  1143. set_list("PERLDB_TYPEAHEAD", @typeahead);
  1144. $ENV{PERLDB_RESTART} = 1;
  1145. #print "$^X, '-d', @flags, @script, ($slave_editor ? '-emacs' : ()), @ARGS";
  1146. exec $^X, '-d', @flags, @script, ($slave_editor ? '-emacs' : ()), @ARGS;
  1147. print $OUT "exec failed: $!\n";
  1148. last CMD; };
  1149. $cmd =~ /^T$/ && do {
  1150. print_trace($OUT, 1); # skip DB
  1151. next CMD; };
  1152. $cmd =~ /^W\s*$/ && do {
  1153. $trace &= ~2;
  1154. @to_watch = @old_watch = ();
  1155. next CMD; };
  1156. $cmd =~ /^W\b\s*(.*)/s && do {
  1157. push @to_watch, $1;
  1158. $evalarg = $1;
  1159. my ($val) = &eval;
  1160. $val = (defined $val) ? "'$val'" : 'undef' ;
  1161. push @old_watch, $val;
  1162. $trace |= 2;
  1163. next CMD; };
  1164. $cmd =~ /^\/(.*)$/ && do {
  1165. $inpat = $1;
  1166. $inpat =~ s:([^\\])/$:$1:;
  1167. if ($inpat ne "") {
  1168. # squelch the sigmangler
  1169. local $SIG{__DIE__};
  1170. local $SIG{__WARN__};
  1171. eval '$inpat =~ m'."\a$inpat\a";
  1172. if ($@ ne "") {
  1173. print $OUT "$@";
  1174. next CMD;
  1175. }
  1176. $pat = $inpat;
  1177. }
  1178. $end = $start;
  1179. $incr = -1;
  1180. eval '
  1181. for (;;) {
  1182. ++$start;
  1183. $start = 1 if ($start > $max);
  1184. last if ($start == $end);
  1185. if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) {
  1186. if ($slave_editor) {
  1187. print $OUT "\032\032$filename:$start:0\n";
  1188. } else {
  1189. print $OUT "$start:\t", $dbline[$start], "\n";
  1190. }
  1191. last;
  1192. }
  1193. } ';
  1194. print $OUT "/$pat/: not found\n" if ($start == $end);
  1195. next CMD; };
  1196. $cmd =~ /^\?(.*)$/ && do {
  1197. $inpat = $1;
  1198. $inpat =~ s:([^\\])\?$:$1:;
  1199. if ($inpat ne "") {
  1200. # squelch the sigmangler
  1201. local $SIG{__DIE__};
  1202. local $SIG{__WARN__};
  1203. eval '$inpat =~ m'."\a$inpat\a";
  1204. if ($@ ne "") {
  1205. print $OUT $@;
  1206. next CMD;
  1207. }
  1208. $pat = $inpat;
  1209. }
  1210. $end = $start;
  1211. $incr = -1;
  1212. eval '
  1213. for (;;) {
  1214. --$start;
  1215. $start = $max if ($start <= 0);
  1216. last if ($start == $end);
  1217. if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) {
  1218. if ($slave_editor) {
  1219. print $OUT "\032\032$filename:$start:0\n";
  1220. } else {
  1221. print $OUT "$start:\t", $dbline[$start], "\n";
  1222. }
  1223. last;
  1224. }
  1225. } ';
  1226. print $OUT "?$pat?: not found\n" if ($start == $end);
  1227. next CMD; };
  1228. $cmd =~ /^$rc+\s*(-)?(\d+)?$/ && do {
  1229. pop(@hist) if length($cmd) > 1;
  1230. $i = $1 ? ($#hist-($2||1)) : ($2||$#hist);
  1231. $cmd = $hist[$i];
  1232. print $OUT $cmd, "\n";
  1233. redo CMD; };
  1234. $cmd =~ /^$sh$sh\s*([\x00-\xff]*)/ && do {
  1235. &system($1);
  1236. next CMD; };
  1237. $cmd =~ /^$rc([^$rc].*)$/ && do {
  1238. $pat = "^$1";
  1239. pop(@hist) if length($cmd) > 1;
  1240. for ($i = $#hist; $i; --$i) {
  1241. last if $hist[$i] =~ /$pat/;
  1242. }
  1243. if (!$i) {
  1244. print $OUT "No such command!\n\n";
  1245. next CMD;
  1246. }
  1247. $cmd = $hist[$i];
  1248. print $OUT $cmd, "\n";
  1249. redo CMD; };
  1250. $cmd =~ /^$sh$/ && do {
  1251. &system($ENV{SHELL}||"/bin/sh");
  1252. next CMD; };
  1253. $cmd =~ /^$sh\s*([\x00-\xff]*)/ && do {
  1254. # XXX: using csh or tcsh destroys sigint retvals!
  1255. #&system($1); # use this instead
  1256. &system($ENV{SHELL}||"/bin/sh","-c",$1);
  1257. next CMD; };
  1258. $cmd =~ /^H\b\s*(-(\d+))?/ && do {
  1259. $end = $2 ? ($#hist-$2) : 0;
  1260. $hist = 0 if $hist < 0;
  1261. for ($i=$#hist; $i>$end; $i--) {
  1262. print $OUT "$i: ",$hist[$i],"\n"
  1263. unless $hist[$i] =~ /^.?$/;
  1264. };
  1265. next CMD; };
  1266. $cmd =~ /^(?:man|(?:perl)?doc)\b(?:\s+([^(]*))?$/ && do {
  1267. runman($1);
  1268. next CMD; };
  1269. $cmd =~ s/^p$/print {\$DB::OUT} \$_/;
  1270. $cmd =~ s/^p\b/print {\$DB::OUT} /;
  1271. $cmd =~ s/^=\s*// && do {
  1272. my @keys;
  1273. if (length $cmd == 0) {
  1274. @keys = sort keys %alias;
  1275. }
  1276. elsif (my($k,$v) = ($cmd =~ /^(\S+)\s+(\S.*)/)) {
  1277. # can't use $_ or kill //g state
  1278. for my $x ($k, $v) { $x =~ s/\a/\\a/g }
  1279. $alias{$k} = "s\a$k\a$v\a";
  1280. # squelch the sigmangler
  1281. local $SIG{__DIE__};
  1282. local $SIG{__WARN__};
  1283. unless (eval "sub { s\a$k\a$v\a }; 1") {
  1284. print $OUT "Can't alias $k to $v: $@\n";
  1285. delete $alias{$k};
  1286. next CMD;
  1287. }
  1288. @keys = ($k);
  1289. }
  1290. else {
  1291. @keys = ($cmd);
  1292. }
  1293. for my $k (@keys) {
  1294. if ((my $v = $alias{$k}) =~ ss\a$k\a(.*)\a$1) {
  1295. print $OUT "$k\t= $1\n";
  1296. }
  1297. elsif (defined $alias{$k}) {
  1298. print $OUT "$k\t$alias{$k}\n";
  1299. }
  1300. else {
  1301. print "No alias for $k\n";
  1302. }
  1303. }
  1304. next CMD; };
  1305. $cmd =~ /^\|\|?\s*[^|]/ && do {
  1306. if ($pager =~ /^\|/) {
  1307. open(SAVEOUT,">&STDOUT") || &warn("Can't save STDOUT");
  1308. open(STDOUT,">&OUT") || &warn("Can't redirect STDOUT");
  1309. } else {
  1310. open(SAVEOUT,">&OUT") || &warn("Can't save DB::OUT");
  1311. }
  1312. fix_less();
  1313. unless ($piped=open(OUT,$pager)) {
  1314. &warn("Can't pipe output to `$pager'");
  1315. if ($pager =~ /^\|/) {
  1316. open(OUT,">&STDOUT") # XXX: lost message
  1317. || &warn("Can't restore DB::OUT");
  1318. open(STDOUT,">&SAVEOUT")
  1319. || &warn("Can't restore STDOUT");
  1320. close(SAVEOUT);
  1321. } else {
  1322. open(OUT,">&STDOUT") # XXX: lost message
  1323. || &warn("Can't restore DB::OUT");
  1324. }
  1325. next CMD;
  1326. }
  1327. $SIG{PIPE}= \&DB::catch if $pager =~ /^\|/
  1328. && ("" eq $SIG{PIPE} || "DEFAULT" eq $SIG{PIPE});
  1329. $selected= select(OUT);
  1330. $|= 1;
  1331. select( $selected ), $selected= "" unless $cmd =~ /^\|\|/;
  1332. $cmd =~ s/^\|+\s*//;
  1333. redo PIPE;
  1334. };
  1335. # XXX Local variants do not work!
  1336. $cmd =~ s/^t\s/\$DB::trace |= 1;\n/;
  1337. $cmd =~ s/^s\s/\$DB::single = 1;\n/ && do {$laststep = 's'};
  1338. $cmd =~ s/^n\s/\$DB::single = 2;\n/ && do {$laststep = 'n'};
  1339. } # PIPE:
  1340. $evalarg = "\$^D = \$^D | \$DB::db_stop;\n$cmd"; &eval;
  1341. if ($onetimeDump) {
  1342. $onetimeDump = undef;
  1343. } elsif ($term_pid == $$) {
  1344. print $OUT "\n";
  1345. }
  1346. } continue { # CMD:
  1347. if ($piped) {
  1348. if ($pager =~ /^\|/) {
  1349. $? = 0;
  1350. # we cannot warn here: the handle is missing --tchrist
  1351. close(OUT) || print SAVEOUT "\nCan't close DB::OUT\n";
  1352. # most of the $? crud was coping with broken cshisms
  1353. if ($?) {
  1354. print SAVEOUT "Pager `$pager' failed: ";
  1355. if ($? == -1) {
  1356. print SAVEOUT "shell returned -1\n";
  1357. } elsif ($? >> 8) {
  1358. print SAVEOUT
  1359. ( $? & 127 ) ? " (SIG#".($?&127).")" : "",
  1360. ( $? & 128 ) ? " -- core dumped" : "", "\n";
  1361. } else {
  1362. print SAVEOUT "status ", ($? >> 8), "\n";
  1363. }
  1364. }
  1365. open(OUT,">&STDOUT") || &warn("Can't restore DB::OUT");
  1366. open(STDOUT,">&SAVEOUT") || &warn("Can't restore STDOUT");
  1367. $SIG{PIPE} = "DEFAULT" if $SIG{PIPE} eq \&DB::catch;
  1368. # Will stop ignoring SIGPIPE if done like nohup(1)
  1369. # does SIGINT but Perl doesn't give us a choice.
  1370. } else {
  1371. open(OUT,">&SAVEOUT") || &warn("Can't restore DB::OUT");
  1372. }
  1373. close(SAVEOUT);
  1374. select($selected), $selected= "" unless $selected eq "";
  1375. $piped= "";
  1376. }
  1377. } # CMD:
  1378. $fall_off_end = 1 unless defined $cmd; # Emulate `q' on EOF
  1379. foreach $evalarg (@$post) {
  1380. &eval;
  1381. }
  1382. } # if ($single || $signal)
  1383. ($@, $!, $^E, $,, $/, $\, $^W) = @saved;
  1384. ();
  1385. }
  1386. # The following code may be executed now:
  1387. # BEGIN {warn 4}
  1388. sub sub {
  1389. my ($al, $ret, @ret) = "";
  1390. if (length($sub) > 10 && substr($sub, -10, 10) eq '::AUTOLOAD') {
  1391. $al = " for $$sub";
  1392. }
  1393. local $stack_depth = $stack_depth + 1; # Protect from non-local exits
  1394. $#stack = $stack_depth;
  1395. $stack[-1] = $single;
  1396. $single &= 1;
  1397. $single |= 4 if $stack_depth == $deep;
  1398. ($frame & 4
  1399. ? ( (print $LINEINFO ' ' x ($stack_depth - 1), "in "),
  1400. # Why -1? But it works! :-(
  1401. print_trace($LINEINFO, -1, 1, 1, "$sub$al") )
  1402. : print $LINEINFO ' ' x ($stack_depth - 1), "entering $sub$al\n") if $frame;
  1403. if (wantarray) {
  1404. @ret = &$sub;
  1405. $single |= $stack[$stack_depth--];
  1406. ($frame & 4
  1407. ? ( (print $LINEINFO ' ' x $stack_depth, "out "),
  1408. print_trace($LINEINFO, -1, 1, 1, "$sub$al") )
  1409. : print $LINEINFO ' ' x $stack_depth, "exited $sub$al\n") if $frame & 2;
  1410. if ($doret eq $stack_depth or $frame & 16) {
  1411. my $fh = ($doret eq $stack_depth ? $OUT : $LINEINFO);
  1412. print $fh ' ' x $stack_depth if $frame & 16;
  1413. print $fh "list context return from $sub:\n";
  1414. dumpit($fh, \@ret );
  1415. $doret = -2;
  1416. }
  1417. @ret;
  1418. } else {
  1419. if (defined wantarray) {
  1420. $ret = &$sub;
  1421. } else {
  1422. &$sub; undef $ret;
  1423. };
  1424. $single |= $stack[$stack_depth--];
  1425. ($frame & 4
  1426. ? ( (print $LINEINFO ' ' x $stack_depth, "out "),
  1427. print_trace($LINEINFO, -1, 1, 1, "$sub$al") )
  1428. : print $LINEINFO ' ' x $stack_depth, "exited $sub$al\n") if $frame & 2;
  1429. if ($doret eq $stack_depth or $frame & 16 and defined wantarray) {
  1430. my $fh = ($doret eq $stack_depth ? $OUT : $LINEINFO);
  1431. print $fh (' ' x $stack_depth) if $frame & 16;
  1432. print $fh (defined wantarray
  1433. ? "scalar context return from $sub: "
  1434. : "void context return from $sub\n");
  1435. dumpit( $fh, $ret ) if defined wantarray;
  1436. $doret = -2;
  1437. }
  1438. $ret;
  1439. }
  1440. }
  1441. sub save {
  1442. @saved = ($@, $!, $^E, $,, $/, $\, $^W);
  1443. $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
  1444. }
  1445. # The following takes its argument via $evalarg to preserve current @_
  1446. sub eval {
  1447. # 'my' would make it visible from user code
  1448. # but so does local! --tchrist
  1449. local @res;
  1450. {
  1451. local $otrace = $trace;
  1452. local $osingle = $single;
  1453. local $od = $^D;
  1454. { ($evalarg) = $evalarg =~ /(.*)/s; }
  1455. @res = eval "$usercontext $evalarg;\n"; # '\n' for nice recursive debug
  1456. $trace = $otrace;
  1457. $single = $osingle;
  1458. $^D = $od;
  1459. }
  1460. my $at = $@;
  1461. local $saved[0]; # Preserve the old value of $@
  1462. eval { &DB::save };
  1463. if ($at) {
  1464. print $OUT $at;
  1465. } elsif ($onetimeDump eq 'dump') {
  1466. dumpit($OUT, \@res);
  1467. } elsif ($onetimeDump eq 'methods') {
  1468. methods($res[0]);
  1469. }
  1470. @res;
  1471. }
  1472. sub postponed_sub {
  1473. my $subname = shift;
  1474. if ($postponed{$subname} =~ s/^break\s([+-]?\d+)\s+if\s//) {
  1475. my $offset = $1 || 0;
  1476. # Filename below can contain ':'
  1477. my ($file,$i) = (find_sub($subname) =~ /^(.*):(\d+)-.*$/);
  1478. if ($i) {
  1479. $i += $offset;
  1480. local *dbline = $main::{'_<' . $file};
  1481. local $^W = 0; # != 0 is magical below
  1482. $had_breakpoints{$file} |= 1;
  1483. my $max = $#dbline;
  1484. ++$i until $dbline[$i] != 0 or $i >= $max;
  1485. $dbline{$i} = delete $postponed{$subname};
  1486. } else {
  1487. print $OUT "Subroutine $subname not found.\n";
  1488. }
  1489. return;
  1490. }
  1491. elsif ($postponed{$subname} eq 'compile') { $signal = 1 }
  1492. #print $OUT "In postponed_sub for `$subname'.\n";
  1493. }
  1494. sub postponed {
  1495. if ($ImmediateStop) {
  1496. $ImmediateStop = 0;
  1497. $signal = 1;
  1498. }
  1499. return &postponed_sub
  1500. unless ref \$_[0] eq 'GLOB'; # A subroutine is compiled.
  1501. # Cannot be done before the file is compiled
  1502. local *dbline = shift;
  1503. my $filename = $dbline;
  1504. $filename =~ s/^_<//;
  1505. $signal = 1, print $OUT "'$filename' loaded...\n"
  1506. if $break_on_load{$filename};
  1507. print $LINEINFO ' ' x $stack_depth, "Package $filename.\n" if $frame;
  1508. return unless $postponed_file{$filename};
  1509. $had_breakpoints{$filename} |= 1;
  1510. #%dbline = %{$postponed_file{$filename}}; # Cannot be done: unsufficient magic
  1511. my $key;
  1512. for $key (keys %{$postponed_file{$filename}}) {
  1513. $dbline{$key} = ${$postponed_file{$filename}}{$key};
  1514. }
  1515. delete $postponed_file{$filename};
  1516. }
  1517. sub dumpit {
  1518. local ($savout) = select(shift);
  1519. my $osingle = $single;
  1520. my $otrace = $trace;
  1521. $single = $trace = 0;
  1522. local $frame = 0;
  1523. local $doret = -2;
  1524. unless (defined &main::dumpValue) {
  1525. do 'dumpvar.pl';
  1526. }
  1527. if (defined &main::dumpValue) {
  1528. &main::dumpValue(shift);
  1529. } else {
  1530. print $OUT "dumpvar.pl not available.\n";
  1531. }
  1532. $single = $osingle;
  1533. $trace = $otrace;
  1534. select ($savout);
  1535. }
  1536. # Tied method do not create a context, so may get wrong message:
  1537. sub print_trace {
  1538. my $fh = shift;
  1539. my @sub = dump_trace($_[0] + 1, $_[1]);
  1540. my $short = $_[2]; # Print short report, next one for sub name
  1541. my $s;
  1542. for ($i=0; $i <= $#sub; $i++) {
  1543. last if $signal;
  1544. local $" = ', ';
  1545. my $args = defined $sub[$i]{args}
  1546. ? "(@{ $sub[$i]{args} })"
  1547. : '' ;
  1548. $args = (substr $args, 0, $maxtrace - 3) . '...'
  1549. if length $args > $maxtrace;
  1550. my $file = $sub[$i]{file};
  1551. $file = $file eq '-e' ? $file : "file `$file'" unless $short;
  1552. $s = $sub[$i]{sub};
  1553. $s = (substr $s, 0, $maxtrace - 3) . '...' if length $s > $maxtrace;
  1554. if ($short) {
  1555. my $sub = @_ >= 4 ? $_[3] : $s;
  1556. print $fh "$sub[$i]{context}=$sub$args from $file:$sub[$i]{line}\n";
  1557. } else {
  1558. print $fh "$sub[$i]{context} = $s$args" .
  1559. " called from $file" .
  1560. " line $sub[$i]{line}\n";
  1561. }
  1562. }
  1563. }
  1564. sub dump_trace {
  1565. my $skip = shift;
  1566. my $count = shift || 1e9;
  1567. $skip++;
  1568. $count += $skip;
  1569. my ($p,$file,$line,$sub,$h,$args,$e,$r,@a,@sub,$context);
  1570. my $nothard = not $frame & 8;
  1571. local $frame = 0; # Do not want to trace this.
  1572. my $otrace = $trace;
  1573. $trace = 0;
  1574. for ($i = $skip;
  1575. $i < $count and ($p,$file,$line,$sub,$h,$context,$e,$r) = caller($i);
  1576. $i++) {
  1577. @a = ();
  1578. for $arg (@args) {
  1579. my $type;
  1580. if (not defined $arg) {
  1581. push @a, "undef";
  1582. } elsif ($nothard and tied $arg) {
  1583. push @a, "tied";
  1584. } elsif ($nothard and $type = ref $arg) {
  1585. push @a, "ref($type)";
  1586. } else {
  1587. local $_ = "$arg"; # Safe to stringify now - should not call f().
  1588. s/([\'\\])/\\$1/g;
  1589. s/(.*)/'$1'/s
  1590. unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
  1591. s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  1592. s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  1593. push(@a, $_);
  1594. }
  1595. }
  1596. $context = $context ? '@' : (defined $context ? "\$" : '.');
  1597. $args = $h ? [@a] : undef;
  1598. $e =~ s/\n\s*\;\s*\Z// if $e;
  1599. $e =~ s/([\\\'])/\\$1/g if $e;
  1600. if ($r) {
  1601. $sub = "require '$e'";
  1602. } elsif (defined $r) {
  1603. $sub = "eval '$e'";
  1604. } elsif ($sub eq '(eval)') {
  1605. $sub = "eval {...}";
  1606. }
  1607. push(@sub, {context => $context, sub => $sub, args => $args,
  1608. file => $file, line => $line});
  1609. last if $signal;
  1610. }
  1611. $trace = $otrace;
  1612. @sub;
  1613. }
  1614. sub action {
  1615. my $action = shift;
  1616. while ($action =~ s/\\$//) {
  1617. #print $OUT "+ ";
  1618. #$action .= "\n";
  1619. $action .= &gets;
  1620. }
  1621. $action;
  1622. }
  1623. sub unbalanced {
  1624. # i hate using globals!
  1625. $balanced_brace_re ||= qr{
  1626. ^ \{
  1627. (?:
  1628. (?> [^{}] + ) # Non-parens without backtracking
  1629. |
  1630. (??{ $balanced_brace_re }) # Group with matching parens
  1631. ) *
  1632. \} $
  1633. }x;
  1634. return $_[0] !~ m/$balanced_brace_re/;
  1635. }
  1636. sub gets {
  1637. &readline("cont: ");
  1638. }
  1639. sub system {
  1640. # We save, change, then restore STDIN and STDOUT to avoid fork() since
  1641. # some non-Unix systems can do system() but have problems with fork().
  1642. open(SAVEIN,"<&STDIN") || &warn("Can't save STDIN");
  1643. open(SAVEOUT,">&STDOUT") || &warn("Can't save STDOUT");
  1644. open(STDIN,"<&IN") || &warn("Can't redirect STDIN");
  1645. open(STDOUT,">&OUT") || &warn("Can't redirect STDOUT");
  1646. # XXX: using csh or tcsh destroys sigint retvals!
  1647. system(@_);
  1648. open(STDIN,"<&SAVEIN") || &warn("Can't restore STDIN");
  1649. open(STDOUT,">&SAVEOUT") || &warn("Can't restore STDOUT");
  1650. close(SAVEIN);
  1651. close(SAVEOUT);
  1652. # most of the $? crud was coping with broken cshisms
  1653. if ($? >> 8) {
  1654. &warn("(Command exited ", ($? >> 8), ")\n");
  1655. } elsif ($?) {
  1656. &warn( "(Command died of SIG#", ($? & 127),
  1657. (($? & 128) ? " -- core dumped" : "") , ")", "\n");
  1658. }
  1659. return $?;
  1660. }
  1661. sub setterm {
  1662. local $frame = 0;
  1663. local $doret = -2;
  1664. eval { require Term::ReadLine } or die $@;
  1665. if ($notty) {
  1666. if ($tty) {
  1667. open(IN,"<$tty") or die "Cannot open TTY `$TTY' for read: $!";
  1668. open(OUT,">$tty") or die "Cannot open TTY `$TTY' for write: $!";
  1669. $IN = \*IN;
  1670. $OUT = \*OUT;
  1671. my $sel = select($OUT);
  1672. $| = 1;
  1673. select($sel);
  1674. } else {
  1675. eval "require Term::Rendezvous;" or die;
  1676. my $rv = $ENV{PERLDB_NOTTY} || "/tmp/perldbtty$$";
  1677. my $term_rv = new Term::Rendezvous $rv;
  1678. $IN = $term_rv->IN;
  1679. $OUT = $term_rv->OUT;
  1680. }
  1681. }
  1682. if (!$rl) {
  1683. $term = new Term::ReadLine::Stub 'perldb', $IN, $OUT;
  1684. } else {
  1685. $term = new Term::ReadLine 'perldb', $IN, $OUT;
  1686. $rl_attribs = $term->Attribs;
  1687. $rl_attribs->{basic_word_break_characters} .= '-:+/*,[])}'
  1688. if defined $rl_attribs->{basic_word_break_characters}
  1689. and index($rl_attribs->{basic_word_break_characters}, ":") == -1;
  1690. $rl_attribs->{special_prefixes} = '$@&%';
  1691. $rl_attribs->{completer_word_break_characters} .= '$@&%';
  1692. $rl_attribs->{completion_function} = \&db_complete;
  1693. }
  1694. $LINEINFO = $OUT unless defined $LINEINFO;
  1695. $lineinfo = $console unless defined $lineinfo;
  1696. $term->MinLine(2);
  1697. if ($term->Features->{setHistory} and "@hist" ne "?") {
  1698. $term->SetHistory(@hist);
  1699. }
  1700. ornaments($ornaments) if defined $ornaments;
  1701. $term_pid = $$;
  1702. }
  1703. sub resetterm { # We forked, so we need a different TTY
  1704. $term_pid = $$;
  1705. if (defined &get_fork_TTY) {
  1706. &get_fork_TTY;
  1707. } elsif (not defined $fork_TTY
  1708. and defined $ENV{TERM} and $ENV{TERM} eq 'xterm'
  1709. and defined $ENV{WINDOWID} and defined $ENV{DISPLAY}) {
  1710. # Possibly _inside_ XTERM
  1711. open XT, q[3>&1 xterm -title 'Forked Perl debugger' -e sh -c 'tty 1>&3;\
  1712. sleep 10000000' |];
  1713. $fork_TTY = <XT>;
  1714. chomp $fork_TTY;
  1715. }
  1716. if (defined $fork_TTY) {
  1717. TTY($fork_TTY);
  1718. undef $fork_TTY;
  1719. } else {
  1720. print_help(<<EOP);
  1721. I<#########> Forked, but do not know how to change a B<TTY>. I<#########>
  1722. Define B<\$DB::fork_TTY>
  1723. - or a function B<DB::get_fork_TTY()> which will set B<\$DB::fork_TTY>.
  1724. The value of B<\$DB::fork_TTY> should be the name of I<TTY> to use.
  1725. On I<UNIX>-like systems one can get the name of a I<TTY> for the given window
  1726. by typing B<tty>, and disconnect the I<shell> from I<TTY> by B<sleep 1000000>.
  1727. EOP
  1728. }
  1729. }
  1730. sub readline {
  1731. local $.;
  1732. if (@typeahead) {
  1733. my $left = @typeahead;
  1734. my $got = shift @typeahead;
  1735. print $OUT "auto(-$left)", shift, $got, "\n";
  1736. $term->AddHistory($got)
  1737. if length($got) > 1 and defined $term->Features->{addHistory};
  1738. return $got;
  1739. }
  1740. local $frame = 0;
  1741. local $doret = -2;
  1742. if (ref $OUT and UNIVERSAL::isa($OUT, 'IO::Socket::INET')) {
  1743. $OUT->write(join('', @_));
  1744. my $stuff;
  1745. $IN->recv( $stuff, 2048 ); # XXX: what's wrong with sysread?
  1746. $stuff;
  1747. }
  1748. else {
  1749. $term->readline(@_);
  1750. }
  1751. }
  1752. sub dump_option {
  1753. my ($opt, $val)= @_;
  1754. $val = option_val($opt,'N/A');
  1755. $val =~ s/([\\\'])/\\$1/g;
  1756. printf $OUT "%20s = '%s'\n", $opt, $val;
  1757. }
  1758. sub option_val {
  1759. my ($opt, $default)= @_;
  1760. my $val;
  1761. if (defined $optionVars{$opt}
  1762. and defined ${$optionVars{$opt}}) {
  1763. $val = ${$optionVars{$opt}};
  1764. } elsif (defined $optionAction{$opt}
  1765. and defined &{$optionAction{$opt}}) {
  1766. $val = &{$optionAction{$opt}}();
  1767. } elsif (defined $optionAction{$opt}
  1768. and not defined $option{$opt}
  1769. or defined $optionVars{$opt}
  1770. and not defined ${$optionVars{$opt}}) {
  1771. $val = $default;
  1772. } else {
  1773. $val = $option{$opt};
  1774. }
  1775. $val
  1776. }
  1777. sub parse_options {
  1778. local($_)= @_;
  1779. # too dangerous to let intuitive usage overwrite important things
  1780. # defaultion should never be the default
  1781. my %opt_needs_val = map { ( $_ => 1 ) } qw{
  1782. arrayDepth hashDepth LineInfo maxTraceLen ornaments
  1783. pager quote ReadLine recallCommand RemotePort ShellBang TTY
  1784. };
  1785. while (length) {
  1786. my $val_defaulted;
  1787. s/^\s+// && next;
  1788. s/^(\w+)(\W?)// or print($OUT "Invalid option `$_'\n"), last;
  1789. my ($opt,$sep) = ($1,$2);
  1790. my $val;
  1791. if ("?" eq $sep) {
  1792. print($OUT "Option query `$opt?' followed by non-space `$_'\n"), last
  1793. if /^\S/;
  1794. #&dump_option($opt);
  1795. } elsif ($sep !~ /\S/) {
  1796. $val_defaulted = 1;
  1797. $val = "1"; # this is an evil default; make 'em set it!
  1798. } elsif ($sep eq "=") {
  1799. if (s/ (["']) ( (?: \\. | (?! \1 ) [^\\] )* ) \1 //x) {
  1800. my $quote = $1;
  1801. ($val = $2) =~ s/\\([$quote\\])/$1/g;
  1802. } else {
  1803. s/^(\S*)//;
  1804. $val = $1;
  1805. print OUT qq(Option better cleared using $opt=""\n)
  1806. unless length $val;
  1807. }
  1808. } else { #{ to "let some poor schmuck bounce on the % key in B<vi>."
  1809. my ($end) = "\\" . substr( ")]>}$sep", index("([<{",$sep), 1 ); #}
  1810. s/^(([^\\$end]|\\[\\$end])*)$end($|\s+)// or
  1811. print($OUT "Unclosed option value `$opt$sep$_'\n"), last;
  1812. ($val = $1) =~ s/\\([\\$end])/$1/g;
  1813. }
  1814. my $option;
  1815. my $matches = grep( /^\Q$opt/ && ($option = $_), @options )
  1816. || grep( /^\Q$opt/i && ($option = $_), @options );
  1817. print($OUT "Unknown option `$opt'\n"), next unless $matches;
  1818. print($OUT "Ambiguous option `$opt'\n"), next if $matches > 1;
  1819. if ($opt_needs_val{$option} && $val_defaulted) {
  1820. print $OUT "Option `$opt' is non-boolean. Use `O $option=VAL' to set, `O $option?' to query\n";
  1821. next;
  1822. }
  1823. $option{$option} = $val if defined $val;
  1824. eval qq{
  1825. local \$frame = 0;
  1826. local \$doret = -2;
  1827. require '$optionRequire{$option}';
  1828. 1;
  1829. } || die # XXX: shouldn't happen
  1830. if defined $optionRequire{$option} &&
  1831. defined $val;
  1832. ${$optionVars{$option}} = $val
  1833. if defined $optionVars{$option} &&
  1834. defined $val;
  1835. &{$optionAction{$option}} ($val)
  1836. if defined $optionAction{$option} &&
  1837. defined &{$optionAction{$option}} &&
  1838. defined $val;
  1839. # Not $rcfile
  1840. dump_option($option) unless $OUT eq \*STDERR;
  1841. }
  1842. }
  1843. sub set_list {
  1844. my ($stem,@list) = @_;
  1845. my $val;
  1846. $ENV{"${stem}_n"} = @list;
  1847. for $i (0 .. $#list) {
  1848. $val = $list[$i];
  1849. $val =~ s/\\/\\\\/g;
  1850. $val =~ s/([\0-\37\177\200-\377])/"\\0x" . unpack('H2',$1)/eg;
  1851. $ENV{"${stem}_$i"} = $val;
  1852. }
  1853. }
  1854. sub get_list {
  1855. my $stem = shift;
  1856. my @list;
  1857. my $n = delete $ENV{"${stem}_n"};
  1858. my $val;
  1859. for $i (0 .. $n - 1) {
  1860. $val = delete $ENV{"${stem}_$i"};
  1861. $val =~ s/\\((\\)|0x(..))/ $2 ? $2 : pack('H2', $3) /ge;
  1862. push @list, $val;
  1863. }
  1864. @list;
  1865. }
  1866. sub catch {
  1867. $signal = 1;
  1868. return; # Put nothing on the stack - malloc/free land!
  1869. }
  1870. sub warn {
  1871. my($msg)= join("",@_);
  1872. $msg .= ": $!\n" unless $msg =~ /\n$/;
  1873. print $OUT $msg;
  1874. }
  1875. sub TTY {
  1876. if (@_ and $term and $term->Features->{newTTY}) {
  1877. my ($in, $out) = shift;
  1878. if ($in =~ /,/) {
  1879. ($in, $out) = split /,/, $in, 2;
  1880. } else {
  1881. $out = $in;
  1882. }
  1883. open IN, $in or die "cannot open `$in' for read: $!";
  1884. open OUT, ">$out" or die "cannot open `$out' for write: $!";
  1885. $term->newTTY(\*IN, \*OUT);
  1886. $IN = \*IN;
  1887. $OUT = \*OUT;
  1888. return $tty = $in;
  1889. } elsif ($term and @_) {
  1890. &warn("Too late to set TTY, enabled on next `R'!\n");
  1891. }
  1892. $tty = shift if @_;
  1893. $tty or $console;
  1894. }
  1895. sub noTTY {
  1896. if ($term) {
  1897. &warn("Too late to set noTTY, enabled on next `R'!\n") if @_;
  1898. }
  1899. $notty = shift if @_;
  1900. $notty;
  1901. }
  1902. sub ReadLine {
  1903. if ($term) {
  1904. &warn("Too late to set ReadLine, enabled on next `R'!\n") if @_;
  1905. }
  1906. $rl = shift if @_;
  1907. $rl;
  1908. }
  1909. sub RemotePort {
  1910. if ($term) {
  1911. &warn("Too late to set RemotePort, enabled on next 'R'!\n") if @_;
  1912. }
  1913. $remoteport = shift if @_;
  1914. $remoteport;
  1915. }
  1916. sub tkRunning {
  1917. if (${$term->Features}{tkRunning}) {
  1918. return $term->tkRunning(@_);
  1919. } else {
  1920. print $OUT "tkRunning not supported by current ReadLine package.\n";
  1921. 0;
  1922. }
  1923. }
  1924. sub NonStop {
  1925. if ($term) {
  1926. &warn("Too late to set up NonStop mode, enabled on next `R'!\n") if @_;
  1927. }
  1928. $runnonstop = shift if @_;
  1929. $runnonstop;
  1930. }
  1931. sub pager {
  1932. if (@_) {
  1933. $pager = shift;
  1934. $pager="|".$pager unless $pager =~ /^(\+?\>|\|)/;
  1935. }
  1936. $pager;
  1937. }
  1938. sub shellBang {
  1939. if (@_) {
  1940. $sh = quotemeta shift;
  1941. $sh .= "\\b" if $sh =~ /\w$/;
  1942. }
  1943. $psh = $sh;
  1944. $psh =~ s/\\b$//;
  1945. $psh =~ s/\\(.)/$1/g;
  1946. &sethelp;
  1947. $psh;
  1948. }
  1949. sub ornaments {
  1950. if (defined $term) {
  1951. local ($warnLevel,$dieLevel) = (0, 1);
  1952. return '' unless $term->Features->{ornaments};
  1953. eval { $term->ornaments(@_) } || '';
  1954. } else {
  1955. $ornaments = shift;
  1956. }
  1957. }
  1958. sub recallCommand {
  1959. if (@_) {
  1960. $rc = quotemeta shift;
  1961. $rc .= "\\b" if $rc =~ /\w$/;
  1962. }
  1963. $prc = $rc;
  1964. $prc =~ s/\\b$//;
  1965. $prc =~ s/\\(.)/$1/g;
  1966. &sethelp;
  1967. $prc;
  1968. }
  1969. sub LineInfo {
  1970. return $lineinfo unless @_;
  1971. $lineinfo = shift;
  1972. my $stream = ($lineinfo =~ /^(\+?\>|\|)/) ? $lineinfo : ">$lineinfo";
  1973. $slave_editor = ($stream =~ /^\|/);
  1974. open(LINEINFO, "$stream") || &warn("Cannot open `$stream' for write");
  1975. $LINEINFO = \*LINEINFO;
  1976. my $save = select($LINEINFO);
  1977. $| = 1;
  1978. select($save);
  1979. $lineinfo;
  1980. }
  1981. sub list_versions {
  1982. my %version;
  1983. my $file;
  1984. for (keys %INC) {
  1985. $file = $_;
  1986. s,\.p[lm]$,,i ;
  1987. s,/,::,g ;
  1988. s/^perl5db$/DB/;
  1989. s/^Term::ReadLine::readline$/readline/;
  1990. if (defined ${ $_ . '::VERSION' }) {
  1991. $version{$file} = "${ $_ . '::VERSION' } from ";
  1992. }
  1993. $version{$file} .= $INC{$file};
  1994. }
  1995. dumpit($OUT,\%version);
  1996. }
  1997. sub sethelp {
  1998. # XXX: make sure these are tabs between the command and explantion,
  1999. # or print_help will screw up your formatting if you have
  2000. # eeevil ornaments enabled. This is an insane mess.
  2001. $help = "
  2002. B<T> Stack trace.
  2003. B<s> [I<expr>] Single step [in I<expr>].
  2004. B<n> [I<expr>] Next, steps over subroutine calls [in I<expr>].
  2005. <B<CR>> Repeat last B<n> or B<s> command.
  2006. B<r> Return from current subroutine.
  2007. B<c> [I<line>|I<sub>] Continue; optionally inserts a one-time-only breakpoint
  2008. at the specified position.
  2009. B<l> I<min>B<+>I<incr> List I<incr>+1 lines starting at I<min>.
  2010. B<l> I<min>B<->I<max> List lines I<min> through I<max>.
  2011. B<l> I<line> List single I<line>.
  2012. B<l> I<subname> List first window of lines from subroutine.
  2013. B<l> I<\$var> List first window of lines from subroutine referenced by I<\$var>.
  2014. B<l> List next window of lines.
  2015. B<-> List previous window of lines.
  2016. B<w> [I<line>] List window around I<line>.
  2017. B<.> Return to the executed line.
  2018. B<f> I<filename> Switch to viewing I<filename>. File must be already loaded.
  2019. I<filename> may be either the full name of the file, or a regular
  2020. expression matching the full file name:
  2021. B<f> I</home/me/foo.pl> and B<f> I<oo\\.> may access the same file.
  2022. Evals (with saved bodies) are considered to be filenames:
  2023. B<f> I<(eval 7)> and B<f> I<eval 7\\b> access the body of the 7th eval
  2024. (in the order of execution).
  2025. B</>I<pattern>B</> Search forwards for I<pattern>; final B</> is optional.
  2026. B<?>I<pattern>B<?> Search backwards for I<pattern>; final B<?> is optional.
  2027. B<L> List all breakpoints and actions.
  2028. B<S> [[B<!>]I<pattern>] List subroutine names [not] matching I<pattern>.
  2029. B<t> Toggle trace mode.
  2030. B<t> I<expr> Trace through execution of I<expr>.
  2031. B<b> [I<line>] [I<condition>]
  2032. Set breakpoint; I<line> defaults to the current execution line;
  2033. I<condition> breaks if it evaluates to true, defaults to '1'.
  2034. B<b> I<subname> [I<condition>]
  2035. Set breakpoint at first line of subroutine.
  2036. B<b> I<\$var> Set breakpoint at first line of subroutine referenced by I<\$var>.
  2037. B<b> B<load> I<filename> Set breakpoint on `require'ing the given file.
  2038. B<b> B<postpone> I<subname> [I<condition>]
  2039. Set breakpoint at first line of subroutine after
  2040. it is compiled.
  2041. B<b> B<compile> I<subname>
  2042. Stop after the subroutine is compiled.
  2043. B<d> [I<line>] Delete the breakpoint for I<line>.
  2044. B<D> Delete all breakpoints.
  2045. B<a> [I<line>] I<command>
  2046. Set an action to be done before the I<line> is executed;
  2047. I<line> defaults to the current execution line.
  2048. Sequence is: check for breakpoint/watchpoint, print line
  2049. if necessary, do action, prompt user if necessary,
  2050. execute line.
  2051. B<a> [I<line>] Delete the action for I<line>.
  2052. B<A> Delete all actions.
  2053. B<W> I<expr> Add a global watch-expression.
  2054. B<W> Delete all watch-expressions.
  2055. B<V> [I<pkg> [I<vars>]] List some (default all) variables in package (default current).
  2056. Use B<~>I<pattern> and B<!>I<pattern> for positive and negative regexps.
  2057. B<X> [I<vars>] Same as \"B<V> I<currentpackage> [I<vars>]\".
  2058. B<x> I<expr> Evals expression in list context, dumps the result.
  2059. B<m> I<expr> Evals expression in list context, prints methods callable
  2060. on the first element of the result.
  2061. B<m> I<class> Prints methods callable via the given class.
  2062. B<<> ? List Perl commands to run before each prompt.
  2063. B<<> I<expr> Define Perl command to run before each prompt.
  2064. B<<<> I<expr> Add to the list of Perl commands to run before each prompt.
  2065. B<>> ? List Perl commands to run after each prompt.
  2066. B<>> I<expr> Define Perl command to run after each prompt.
  2067. B<>>B<>> I<expr> Add to the list of Perl commands to run after each prompt.
  2068. B<{> I<db_command> Define debugger command to run before each prompt.
  2069. B<{> ? List debugger commands to run before each prompt.
  2070. B<<> I<expr> Define Perl command to run before each prompt.
  2071. B<{{> I<db_command> Add to the list of debugger commands to run before each prompt.
  2072. B<$prc> I<number> Redo a previous command (default previous command).
  2073. B<$prc> I<-number> Redo number'th-to-last command.
  2074. B<$prc> I<pattern> Redo last command that started with I<pattern>.
  2075. See 'B<O> I<recallCommand>' too.
  2076. B<$psh$psh> I<cmd> Run cmd in a subprocess (reads from DB::IN, writes to DB::OUT)"
  2077. . ( $rc eq $sh ? "" : "
  2078. B<$psh> [I<cmd>] Run I<cmd> in subshell (forces \"\$SHELL -c 'cmd'\")." ) . "
  2079. See 'B<O> I<shellBang>' too.
  2080. B<H> I<-number> Display last number commands (default all).
  2081. B<p> I<expr> Same as \"I<print {DB::OUT} expr>\" in current package.
  2082. B<|>I<dbcmd> Run debugger command, piping DB::OUT to current pager.
  2083. B<||>I<dbcmd> Same as B<|>I<dbcmd> but DB::OUT is temporarilly select()ed as well.
  2084. B<\=> [I<alias> I<value>] Define a command alias, or list current aliases.
  2085. I<command> Execute as a perl statement in current package.
  2086. B<v> Show versions of loaded modules.
  2087. B<R> Pure-man-restart of debugger, some of debugger state
  2088. and command-line options may be lost.
  2089. Currently the following setting are preserved:
  2090. history, breakpoints and actions, debugger B<O>ptions
  2091. and the following command-line options: I<-w>, I<-I>, I<-e>.
  2092. B<O> [I<opt>] ... Set boolean option to true
  2093. B<O> [I<opt>B<?>] Query options
  2094. B<O> [I<opt>B<=>I<val>] [I<opt>=B<\">I<val>B<\">] ...
  2095. Set options. Use quotes in spaces in value.
  2096. I<recallCommand>, I<ShellBang> chars used to recall command or spawn shell;
  2097. I<pager> program for output of \"|cmd\";
  2098. I<tkRunning> run Tk while prompting (with ReadLine);
  2099. I<signalLevel> I<warnLevel> I<dieLevel> level of verbosity;
  2100. I<inhibit_exit> Allows stepping off the end of the script.
  2101. I<ImmediateStop> Debugger should stop as early as possible.
  2102. I<RemotePort> Remote hostname:port for remote debugging
  2103. The following options affect what happens with B<V>, B<X>, and B<x> commands:
  2104. I<arrayDepth>, I<hashDepth> print only first N elements ('' for all);
  2105. I<compactDump>, I<veryCompact> change style of array and hash dump;
  2106. I<globPrint> whether to print contents of globs;
  2107. I<DumpDBFiles> dump arrays holding debugged files;
  2108. I<DumpPackages> dump symbol tables of packages;
  2109. I<DumpReused> dump contents of \"reused\" addresses;
  2110. I<quote>, I<HighBit>, I<undefPrint> change style of string dump;
  2111. I<bareStringify> Do not print the overload-stringified value;
  2112. Other options include:
  2113. I<PrintRet> affects printing of return value after B<r> command,
  2114. I<frame> affects printing messages on entry and exit from subroutines.
  2115. I<AutoTrace> affects printing messages on every possible breaking point.
  2116. I<maxTraceLen> gives maximal length of evals/args listed in stack trace.
  2117. I<ornaments> affects screen appearance of the command line.
  2118. During startup options are initialized from \$ENV{PERLDB_OPTS}.
  2119. You can put additional initialization options I<TTY>, I<noTTY>,
  2120. I<ReadLine>, I<NonStop>, and I<RemotePort> there (or use
  2121. `B<R>' after you set them).
  2122. B<q> or B<^D> Quit. Set B<\$DB::finished = 0> to debug global destruction.
  2123. B<h> [I<db_command>] Get help [on a specific debugger command], enter B<|h> to page.
  2124. B<h h> Summary of debugger commands.
  2125. B<$doccmd> I<manpage> Runs the external doc viewer B<$doccmd> command on the
  2126. named Perl I<manpage>, or on B<$doccmd> itself if omitted.
  2127. Set B<\$DB::doccmd> to change viewer.
  2128. Type `|h' for a paged display if this was too hard to read.
  2129. "; # Fix balance of vi % matching: } }}
  2130. $summary = <<"END_SUM";
  2131. I<List/search source lines:> I<Control script execution:>
  2132. B<l> [I<ln>|I<sub>] List source code B<T> Stack trace
  2133. B<-> or B<.> List previous/current line B<s> [I<expr>] Single step [in expr]
  2134. B<w> [I<line>] List around line B<n> [I<expr>] Next, steps over subs
  2135. B<f> I<filename> View source in file <B<CR>/B<Enter>> Repeat last B<n> or B<s>
  2136. B</>I<pattern>B</> B<?>I<patt>B<?> Search forw/backw B<r> Return from subroutine
  2137. B<v> Show versions of modules B<c> [I<ln>|I<sub>] Continue until position
  2138. I<Debugger controls:> B<L> List break/watch/actions
  2139. B<O> [...] Set debugger options B<t> [I<expr>] Toggle trace [trace expr]
  2140. B<<>[B<<>]|B<{>[B<{>]|B<>>[B<>>] [I<cmd>] Do pre/post-prompt B<b> [I<ln>|I<event>|I<sub>] [I<cnd>] Set breakpoint
  2141. B<$prc> [I<N>|I<pat>] Redo a previous command B<d> [I<ln>] or B<D> Delete a/all breakpoints
  2142. B<H> [I<-num>] Display last num commands B<a> [I<ln>] I<cmd> Do cmd before line
  2143. B<=> [I<a> I<val>] Define/list an alias B<W> I<expr> Add a watch expression
  2144. B<h> [I<db_cmd>] Get help on command B<A> or B<W> Delete all actions/watch
  2145. B<|>[B<|>]I<db_cmd> Send output to pager B<$psh>\[B<$psh>\] I<syscmd> Run cmd in a subprocess
  2146. B<q> or B<^D> Quit B<R> Attempt a restart
  2147. I<Data Examination:> B<expr> Execute perl code, also see: B<s>,B<n>,B<t> I<expr>
  2148. B<x>|B<m> I<expr> Evals expr in list context, dumps the result or lists methods.
  2149. B<p> I<expr> Print expression (uses script's current package).
  2150. B<S> [[B<!>]I<pat>] List subroutine names [not] matching pattern
  2151. B<V> [I<Pk> [I<Vars>]] List Variables in Package. Vars can be ~pattern or !pattern.
  2152. B<X> [I<Vars>] Same as \"B<V> I<current_package> [I<Vars>]\".
  2153. For more help, type B<h> I<cmd_letter>, or run B<$doccmd perldebug> for all docs.
  2154. END_SUM
  2155. # ')}}; # Fix balance of vi % matching
  2156. }
  2157. sub print_help {
  2158. local $_ = shift;
  2159. # Restore proper alignment destroyed by eeevil I<> and B<>
  2160. # ornaments: A pox on both their houses!
  2161. #
  2162. # A help command will have everything up to and including
  2163. # the first tab sequence paddeed into a field 16 (or if indented 20)
  2164. # wide. If it's wide than that, an extra space will be added.
  2165. s{
  2166. ^ # only matters at start of line
  2167. ( \040{4} | \t )* # some subcommands are indented
  2168. ( < ? # so <CR> works
  2169. [BI] < [^\t\n] + ) # find an eeevil ornament
  2170. ( \t+ ) # original separation, discarded
  2171. ( .* ) # this will now start (no earlier) than
  2172. # column 16
  2173. } {
  2174. my($leadwhite, $command, $midwhite, $text) = ($1, $2, $3, $4);
  2175. my $clean = $command;
  2176. $clean =~ s/[BI]<([^>]*)>/$1/g;
  2177. # replace with this whole string:
  2178. (length($leadwhite) ? " " x 4 : "")
  2179. . $command
  2180. . ((" " x (16 + (length($leadwhite) ? 4 : 0) - length($clean))) || " ")
  2181. . $text;
  2182. }mgex;
  2183. s{ # handle bold ornaments
  2184. B < ( [^>] + | > ) >
  2185. } {
  2186. $Term::ReadLine::TermCap::rl_term_set[2]
  2187. . $1
  2188. . $Term::ReadLine::TermCap::rl_term_set[3]
  2189. }gex;
  2190. s{ # handle italic ornaments
  2191. I < ( [^>] + | > ) >
  2192. } {
  2193. $Term::ReadLine::TermCap::rl_term_set[0]
  2194. . $1
  2195. . $Term::ReadLine::TermCap::rl_term_set[1]
  2196. }gex;
  2197. print $OUT $_;
  2198. }
  2199. sub fix_less {
  2200. return if defined $ENV{LESS} && $ENV{LESS} =~ /r/;
  2201. my $is_less = $pager =~ /\bless\b/;
  2202. if ($pager =~ /\bmore\b/) {
  2203. my @st_more = stat('/usr/bin/more');
  2204. my @st_less = stat('/usr/bin/less');
  2205. $is_less = @st_more && @st_less
  2206. && $st_more[0] == $st_less[0]
  2207. && $st_more[1] == $st_less[1];
  2208. }
  2209. # changes environment!
  2210. $ENV{LESS} .= 'r' if $is_less;
  2211. }
  2212. sub diesignal {
  2213. local $frame = 0;
  2214. local $doret = -2;
  2215. $SIG{'ABRT'} = 'DEFAULT';
  2216. kill 'ABRT', $$ if $panic++;
  2217. if (defined &Carp::longmess) {
  2218. local $SIG{__WARN__} = '';
  2219. local $Carp::CarpLevel = 2; # mydie + confess
  2220. &warn(Carp::longmess("Signal @_"));
  2221. }
  2222. else {
  2223. print $DB::OUT "Got signal @_\n";
  2224. }
  2225. kill 'ABRT', $$;
  2226. }
  2227. sub dbwarn {
  2228. local $frame = 0;
  2229. local $doret = -2;
  2230. local $SIG{__WARN__} = '';
  2231. local $SIG{__DIE__} = '';
  2232. eval { require Carp } if defined $^S; # If error/warning during compilation,
  2233. # require may be broken.
  2234. warn(@_, "\nCannot print stack trace, load with -MCarp option to see stack"),
  2235. return unless defined &Carp::longmess;
  2236. my ($mysingle,$mytrace) = ($single,$trace);
  2237. $single = 0; $trace = 0;
  2238. my $mess = Carp::longmess(@_);
  2239. ($single,$trace) = ($mysingle,$mytrace);
  2240. &warn($mess);
  2241. }
  2242. sub dbdie {
  2243. local $frame = 0;
  2244. local $doret = -2;
  2245. local $SIG{__DIE__} = '';
  2246. local $SIG{__WARN__} = '';
  2247. my $i = 0; my $ineval = 0; my $sub;
  2248. if ($dieLevel > 2) {
  2249. local $SIG{__WARN__} = \&dbwarn;
  2250. &warn(@_); # Yell no matter what
  2251. return;
  2252. }
  2253. if ($dieLevel < 2) {
  2254. die @_ if $^S; # in eval propagate
  2255. }
  2256. eval { require Carp } if defined $^S; # If error/warning during compilation,
  2257. # require may be broken.
  2258. die(@_, "\nCannot print stack trace, load with -MCarp option to see stack")
  2259. unless defined &Carp::longmess;
  2260. # We do not want to debug this chunk (automatic disabling works
  2261. # inside DB::DB, but not in Carp).
  2262. my ($mysingle,$mytrace) = ($single,$trace);
  2263. $single = 0; $trace = 0;
  2264. my $mess = Carp::longmess(@_);
  2265. ($single,$trace) = ($mysingle,$mytrace);
  2266. die $mess;
  2267. }
  2268. sub warnLevel {
  2269. if (@_) {
  2270. $prevwarn = $SIG{__WARN__} unless $warnLevel;
  2271. $warnLevel = shift;
  2272. if ($warnLevel) {
  2273. $SIG{__WARN__} = \&DB::dbwarn;
  2274. } else {
  2275. $SIG{__WARN__} = $prevwarn;
  2276. }
  2277. }
  2278. $warnLevel;
  2279. }
  2280. sub dieLevel {
  2281. if (@_) {
  2282. $prevdie = $SIG{__DIE__} unless $dieLevel;
  2283. $dieLevel = shift;
  2284. if ($dieLevel) {
  2285. $SIG{__DIE__} = \&DB::dbdie; # if $dieLevel < 2;
  2286. #$SIG{__DIE__} = \&DB::diehard if $dieLevel >= 2;
  2287. print $OUT "Stack dump during die enabled",
  2288. ( $dieLevel == 1 ? " outside of evals" : ""), ".\n"
  2289. if $I_m_init;
  2290. print $OUT "Dump printed too.\n" if $dieLevel > 2;
  2291. } else {
  2292. $SIG{__DIE__} = $prevdie;
  2293. print $OUT "Default die handler restored.\n";
  2294. }
  2295. }
  2296. $dieLevel;
  2297. }
  2298. sub signalLevel {
  2299. if (@_) {
  2300. $prevsegv = $SIG{SEGV} unless $signalLevel;
  2301. $prevbus = $SIG{BUS} unless $signalLevel;
  2302. $signalLevel = shift;
  2303. if ($signalLevel) {
  2304. $SIG{SEGV} = \&DB::diesignal;
  2305. $SIG{BUS} = \&DB::diesignal;
  2306. } else {
  2307. $SIG{SEGV} = $prevsegv;
  2308. $SIG{BUS} = $prevbus;
  2309. }
  2310. }
  2311. $signalLevel;
  2312. }
  2313. sub CvGV_name {
  2314. my $in = shift;
  2315. my $name = CvGV_name_or_bust($in);
  2316. defined $name ? $name : $in;
  2317. }
  2318. sub CvGV_name_or_bust {
  2319. my $in = shift;
  2320. return if $skipCvGV; # Backdoor to avoid problems if XS broken...
  2321. $in = \&$in; # Hard reference...
  2322. eval {require Devel::Peek; 1} or return;
  2323. my $gv = Devel::Peek::CvGV($in) or return;
  2324. *$gv{PACKAGE} . '::' . *$gv{NAME};
  2325. }
  2326. sub find_sub {
  2327. my $subr = shift;
  2328. $sub{$subr} or do {
  2329. return unless defined &$subr;
  2330. my $name = CvGV_name_or_bust($subr);
  2331. my $data;
  2332. $data = $sub{$name} if defined $name;
  2333. return $data if defined $data;
  2334. # Old stupid way...
  2335. $subr = \&$subr; # Hard reference
  2336. my $s;
  2337. for (keys %sub) {
  2338. $s = $_, last if $subr eq \&$_;
  2339. }
  2340. $sub{$s} if $s;
  2341. }
  2342. }
  2343. sub methods {
  2344. my $class = shift;
  2345. $class = ref $class if ref $class;
  2346. local %seen;
  2347. local %packs;
  2348. methods_via($class, '', 1);
  2349. methods_via('UNIVERSAL', 'UNIVERSAL', 0);
  2350. }
  2351. sub methods_via {
  2352. my $class = shift;
  2353. return if $packs{$class}++;
  2354. my $prefix = shift;
  2355. my $prepend = $prefix ? "via $prefix: " : '';
  2356. my $name;
  2357. for $name (grep {defined &{${"${class}::"}{$_}}}
  2358. sort keys %{"${class}::"}) {
  2359. next if $seen{ $name }++;
  2360. print $DB::OUT "$prepend$name\n";
  2361. }
  2362. return unless shift; # Recurse?
  2363. for $name (@{"${class}::ISA"}) {
  2364. $prepend = $prefix ? $prefix . " -> $name" : $name;
  2365. methods_via($name, $prepend, 1);
  2366. }
  2367. }
  2368. sub setman {
  2369. $doccmd = $^O !~ /^(?:MSWin32|VMS|os2|dos|amigaos|riscos|MacOS)\z/s
  2370. ? "man" # O Happy Day!
  2371. : "perldoc"; # Alas, poor unfortunates
  2372. }
  2373. sub runman {
  2374. my $page = shift;
  2375. unless ($page) {
  2376. &system("$doccmd $doccmd");
  2377. return;
  2378. }
  2379. # this way user can override, like with $doccmd="man -Mwhatever"
  2380. # or even just "man " to disable the path check.
  2381. unless ($doccmd eq 'man') {
  2382. &system("$doccmd $page");
  2383. return;
  2384. }
  2385. $page = 'perl' if lc($page) eq 'help';
  2386. require Config;
  2387. my $man1dir = $Config::Config{'man1dir'};
  2388. my $man3dir = $Config::Config{'man3dir'};
  2389. for ($man1dir, $man3dir) { s#/[^/]*\z## if /\S/ }
  2390. my $manpath = '';
  2391. $manpath .= "$man1dir:" if $man1dir =~ /\S/;
  2392. $manpath .= "$man3dir:" if $man3dir =~ /\S/ && $man1dir ne $man3dir;
  2393. chop $manpath if $manpath;
  2394. # harmless if missing, I figure
  2395. my $oldpath = $ENV{MANPATH};
  2396. $ENV{MANPATH} = $manpath if $manpath;
  2397. my $nopathopt = $^O =~ /dunno what goes here/;
  2398. if (system($doccmd,
  2399. # I just *know* there are men without -M
  2400. (($manpath && !$nopathopt) ? ("-M", $manpath) : ()),
  2401. split ' ', $page) )
  2402. {
  2403. unless ($page =~ /^perl\w/) {
  2404. if (grep { $page eq $_ } qw{
  2405. 5004delta 5005delta amiga api apio book boot bot call compile
  2406. cygwin data dbmfilter debug debguts delta diag doc dos dsc embed
  2407. faq faq1 faq2 faq3 faq4 faq5 faq6 faq7 faq8 faq9 filter fork
  2408. form func guts hack hist hpux intern ipc lexwarn locale lol mod
  2409. modinstall modlib number obj op opentut os2 os390 pod port
  2410. ref reftut run sec style sub syn thrtut tie toc todo toot tootc
  2411. trap unicode var vms win32 xs xstut
  2412. })
  2413. {
  2414. $page =~ s/^/perl/;
  2415. system($doccmd,
  2416. (($manpath && !$nopathopt) ? ("-M", $manpath) : ()),
  2417. $page);
  2418. }
  2419. }
  2420. }
  2421. if (defined $oldpath) {
  2422. $ENV{MANPATH} = $manpath;
  2423. } else {
  2424. delete $ENV{MANPATH};
  2425. }
  2426. }
  2427. # The following BEGIN is very handy if debugger goes havoc, debugging debugger?
  2428. BEGIN { # This does not compile, alas.
  2429. $IN = \*STDIN; # For bugs before DB::OUT has been opened
  2430. $OUT = \*STDERR; # For errors before DB::OUT has been opened
  2431. $sh = '!';
  2432. $rc = ',';
  2433. @hist = ('?');
  2434. $deep = 100; # warning if stack gets this deep
  2435. $window = 10;
  2436. $preview = 3;
  2437. $sub = '';
  2438. $SIG{INT} = \&DB::catch;
  2439. # This may be enabled to debug debugger:
  2440. #$warnLevel = 1 unless defined $warnLevel;
  2441. #$dieLevel = 1 unless defined $dieLevel;
  2442. #$signalLevel = 1 unless defined $signalLevel;
  2443. $db_stop = 0; # Compiler warning
  2444. $db_stop = 1 << 30;
  2445. $level = 0; # Level of recursive debugging
  2446. # @stack and $doret are needed in sub sub, which is called for DB::postponed.
  2447. # Triggers bug (?) in perl is we postpone this until runtime:
  2448. @postponed = @stack = (0);
  2449. $stack_depth = 0; # Localized $#stack
  2450. $doret = -2;
  2451. $frame = 0;
  2452. }
  2453. BEGIN {$^W = $ini_warn;} # Switch warnings back
  2454. #use Carp; # This did break, left for debuggin
  2455. sub db_complete {
  2456. # Specific code for b c l V m f O, &blah, $blah, @blah, %blah
  2457. my($text, $line, $start) = @_;
  2458. my ($itext, $search, $prefix, $pack) =
  2459. ($text, "^\Q${'package'}::\E([^:]+)\$");
  2460. return sort grep /^\Q$text/, (keys %sub), qw(postpone load compile), # subroutines
  2461. (map { /$search/ ? ($1) : () } keys %sub)
  2462. if (substr $line, 0, $start) =~ /^\|*[blc]\s+((postpone|compile)\s+)?$/;
  2463. return sort grep /^\Q$text/, values %INC # files
  2464. if (substr $line, 0, $start) =~ /^\|*b\s+load\s+$/;
  2465. return sort map {($_, db_complete($_ . "::", "V ", 2))}
  2466. grep /^\Q$text/, map { /^(.*)::$/ ? ($1) : ()} keys %:: # top-packages
  2467. if (substr $line, 0, $start) =~ /^\|*[Vm]\s+$/ and $text =~ /^\w*$/;
  2468. return sort map {($_, db_complete($_ . "::", "V ", 2))}
  2469. grep !/^main::/,
  2470. grep /^\Q$text/, map { /^(.*)::$/ ? ($prefix . "::$1") : ()} keys %{$prefix . '::'}
  2471. # packages
  2472. if (substr $line, 0, $start) =~ /^\|*[Vm]\s+$/
  2473. and $text =~ /^(.*[^:])::?(\w*)$/ and $prefix = $1;
  2474. if ( $line =~ /^\|*f\s+(.*)/ ) { # Loaded files
  2475. # We may want to complete to (eval 9), so $text may be wrong
  2476. $prefix = length($1) - length($text);
  2477. $text = $1;
  2478. return sort
  2479. map {substr $_, 2 + $prefix} grep /^_<\Q$text/, (keys %main::), $0
  2480. }
  2481. if ((substr $text, 0, 1) eq '&') { # subroutines
  2482. $text = substr $text, 1;
  2483. $prefix = "&";
  2484. return sort map "$prefix$_",
  2485. grep /^\Q$text/,
  2486. (keys %sub),
  2487. (map { /$search/ ? ($1) : () }
  2488. keys %sub);
  2489. }
  2490. if ($text =~ /^[\$@%](.*)::(.*)/) { # symbols in a package
  2491. $pack = ($1 eq 'main' ? '' : $1) . '::';
  2492. $prefix = (substr $text, 0, 1) . $1 . '::';
  2493. $text = $2;
  2494. my @out
  2495. = map "$prefix$_", grep /^\Q$text/, grep /^_?[a-zA-Z]/, keys %$pack ;
  2496. if (@out == 1 and $out[0] =~ /::$/ and $out[0] ne $itext) {
  2497. return db_complete($out[0], $line, $start);
  2498. }
  2499. return sort @out;
  2500. }
  2501. if ($text =~ /^[\$@%]/) { # symbols (in $package + packages in main)
  2502. $pack = ($package eq 'main' ? '' : $package) . '::';
  2503. $prefix = substr $text, 0, 1;
  2504. $text = substr $text, 1;
  2505. my @out = map "$prefix$_", grep /^\Q$text/,
  2506. (grep /^_?[a-zA-Z]/, keys %$pack),
  2507. ( $pack eq '::' ? () : (grep /::$/, keys %::) ) ;
  2508. if (@out == 1 and $out[0] =~ /::$/ and $out[0] ne $itext) {
  2509. return db_complete($out[0], $line, $start);
  2510. }
  2511. return sort @out;
  2512. }
  2513. if ((substr $line, 0, $start) =~ /^\|*O\b.*\s$/) { # Options after a space
  2514. my @out = grep /^\Q$text/, @options;
  2515. my $val = option_val($out[0], undef);
  2516. my $out = '? ';
  2517. if (not defined $val or $val =~ /[\n\r]/) {
  2518. # Can do nothing better
  2519. } elsif ($val =~ /\s/) {
  2520. my $found;
  2521. foreach $l (split //, qq/\"\'\#\|/) {
  2522. $out = "$l$val$l ", last if (index $val, $l) == -1;
  2523. }
  2524. } else {
  2525. $out = "=$val ";
  2526. }
  2527. # Default to value if one completion, to question if many
  2528. $rl_attribs->{completer_terminator_character} = (@out == 1 ? $out : '? ');
  2529. return sort @out;
  2530. }
  2531. return $term->filename_list($text); # filenames
  2532. }
  2533. sub end_report {
  2534. print $OUT "Use `q' to quit or `R' to restart. `h q' for details.\n"
  2535. }
  2536. END {
  2537. $finished = 1 if $inhibit_exit; # So that some keys may be disabled.
  2538. $fall_off_end = 1 unless $inhibit_exit;
  2539. # Do not stop in at_exit() and destructors on exit:
  2540. $DB::single = !$fall_off_end && !$runnonstop;
  2541. DB::fake::at_exit() unless $fall_off_end or $runnonstop;
  2542. }
  2543. package DB::fake;
  2544. sub at_exit {
  2545. "Debugged program terminated. Use `q' to quit or `R' to restart.";
  2546. }
  2547. package DB; # Do not trace this 1; below!
  2548. 1;