Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

802 lines
19 KiB

  1. #
  2. # Documentation is at the __END__
  3. #
  4. package DB;
  5. # "private" globals
  6. my ($running, $ready, $deep, $usrctxt, $evalarg,
  7. @stack, @saved, @skippkg, @clients);
  8. my $preeval = {};
  9. my $posteval = {};
  10. my $ineval = {};
  11. ####
  12. #
  13. # Globals - must be defined at startup so that clients can refer to
  14. # them right after a C<require DB;>
  15. #
  16. ####
  17. BEGIN {
  18. # these are hardcoded in perl source (some are magical)
  19. $DB::sub = ''; # name of current subroutine
  20. %DB::sub = (); # "filename:fromline-toline" for every known sub
  21. $DB::single = 0; # single-step flag (set it to 1 to enable stops in BEGIN/use)
  22. $DB::signal = 0; # signal flag (will cause a stop at the next line)
  23. $DB::trace = 0; # are we tracing through subroutine calls?
  24. @DB::args = (); # arguments of current subroutine or @ARGV array
  25. @DB::dbline = (); # list of lines in currently loaded file
  26. %DB::dbline = (); # actions in current file (keyed by line number)
  27. @DB::ret = (); # return value of last sub executed in list context
  28. $DB::ret = ''; # return value of last sub executed in scalar context
  29. # other "public" globals
  30. $DB::package = ''; # current package space
  31. $DB::filename = ''; # current filename
  32. $DB::subname = ''; # currently executing sub (fullly qualified name)
  33. $DB::lineno = ''; # current line number
  34. $DB::VERSION = $DB::VERSION = '1.0';
  35. # initialize private globals to avoid warnings
  36. $running = 1; # are we running, or are we stopped?
  37. @stack = (0);
  38. @clients = ();
  39. $deep = 100;
  40. $ready = 0;
  41. @saved = ();
  42. @skippkg = ();
  43. $usrctxt = '';
  44. $evalarg = '';
  45. }
  46. ####
  47. # entry point for all subroutine calls
  48. #
  49. sub sub {
  50. push(@stack, $DB::single);
  51. $DB::single &= 1;
  52. $DB::single |= 4 if $#stack == $deep;
  53. # print $DB::sub, "\n";
  54. if ($DB::sub =~ /(?:^|::)DESTROY$/ or not defined wantarray) {
  55. &$DB::sub;
  56. $DB::single |= pop(@stack);
  57. $DB::ret = undef;
  58. }
  59. elsif (wantarray) {
  60. @DB::ret = &$DB::sub;
  61. $DB::single |= pop(@stack);
  62. @DB::ret;
  63. }
  64. else {
  65. $DB::ret = &$DB::sub;
  66. $DB::single |= pop(@stack);
  67. $DB::ret;
  68. }
  69. }
  70. ####
  71. # this is called by perl for every statement
  72. #
  73. sub DB {
  74. return unless $ready;
  75. &save;
  76. ($DB::package, $DB::filename, $DB::lineno) = caller;
  77. return if @skippkg and grep { $_ eq $DB::package } @skippkg;
  78. $usrctxt = "package $DB::package;"; # this won't let them modify, alas
  79. local(*DB::dbline) = "::_<$DB::filename";
  80. my ($stop, $action);
  81. if (($stop,$action) = split(/\0/,$DB::dbline{$DB::lineno})) {
  82. if ($stop eq '1') {
  83. $DB::signal |= 1;
  84. }
  85. else {
  86. $stop = 0 unless $stop; # avoid un_init warning
  87. $evalarg = "\$DB::signal |= do { $stop; }"; &eval;
  88. $DB::dbline{$DB::lineno} =~ s/;9($|\0)/$1/; # clear any temp breakpt
  89. }
  90. }
  91. if ($DB::single || $DB::trace || $DB::signal) {
  92. $DB::subname = ($DB::sub =~ /\'|::/) ? $DB::sub : "${DB::package}::$DB::sub"; #';
  93. DB->loadfile($DB::filename, $DB::lineno);
  94. }
  95. $evalarg = $action, &eval if $action;
  96. if ($DB::single || $DB::signal) {
  97. _outputall($#stack . " levels deep in subroutine calls.\n") if $DB::single & 4;
  98. $DB::single = 0;
  99. $DB::signal = 0;
  100. $running = 0;
  101. &eval if ($evalarg = DB->prestop);
  102. my $c;
  103. for $c (@clients) {
  104. # perform any client-specific prestop actions
  105. &eval if ($evalarg = $c->cprestop);
  106. # Now sit in an event loop until something sets $running
  107. do {
  108. $c->idle; # call client event loop; must not block
  109. if ($running == 2) { # client wants something eval-ed
  110. &eval if ($evalarg = $c->evalcode);
  111. $running = 0;
  112. }
  113. } until $running;
  114. # perform any client-specific poststop actions
  115. &eval if ($evalarg = $c->cpoststop);
  116. }
  117. &eval if ($evalarg = DB->poststop);
  118. }
  119. ($@, $!, $,, $/, $\, $^W) = @saved;
  120. ();
  121. }
  122. ####
  123. # this takes its argument via $evalarg to preserve current @_
  124. #
  125. sub eval {
  126. ($@, $!, $,, $/, $\, $^W) = @saved;
  127. eval "$usrctxt $evalarg; &DB::save";
  128. _outputall($@) if $@;
  129. }
  130. ###############################################################################
  131. # no compile-time subroutine call allowed before this point #
  132. ###############################################################################
  133. use strict; # this can run only after DB() and sub() are defined
  134. sub save {
  135. @saved = ($@, $!, $,, $/, $\, $^W);
  136. $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
  137. }
  138. sub catch {
  139. for (@clients) { $_->awaken; }
  140. $DB::signal = 1;
  141. $ready = 1;
  142. }
  143. ####
  144. #
  145. # Client callable (read inheritable) methods defined after this point
  146. #
  147. ####
  148. sub register {
  149. my $s = shift;
  150. $s = _clientname($s) if ref($s);
  151. push @clients, $s;
  152. }
  153. sub done {
  154. my $s = shift;
  155. $s = _clientname($s) if ref($s);
  156. @clients = grep {$_ ne $s} @clients;
  157. $s->cleanup;
  158. # $running = 3 unless @clients;
  159. exit(0) unless @clients;
  160. }
  161. sub _clientname {
  162. my $name = shift;
  163. "$name" =~ /^(.+)=[A-Z]+\(.+\)$/;
  164. return $1;
  165. }
  166. sub next {
  167. my $s = shift;
  168. $DB::single = 2;
  169. $running = 1;
  170. }
  171. sub step {
  172. my $s = shift;
  173. $DB::single = 1;
  174. $running = 1;
  175. }
  176. sub cont {
  177. my $s = shift;
  178. my $i = shift;
  179. $s->set_tbreak($i) if $i;
  180. for ($i = 0; $i <= $#stack;) {
  181. $stack[$i++] &= ~1;
  182. }
  183. $DB::single = 0;
  184. $running = 1;
  185. }
  186. ####
  187. # XXX caller must experimentally determine $i (since it depends
  188. # on how many client call frames are between this call and the DB call).
  189. # Such is life.
  190. #
  191. sub ret {
  192. my $s = shift;
  193. my $i = shift; # how many levels to get to DB sub
  194. $i = 0 unless defined $i;
  195. $stack[$#stack-$i] |= 1;
  196. $DB::single = 0;
  197. $running = 1;
  198. }
  199. ####
  200. # XXX caller must experimentally determine $start (since it depends
  201. # on how many client call frames are between this call and the DB call).
  202. # Such is life.
  203. #
  204. sub backtrace {
  205. my $self = shift;
  206. my $start = shift;
  207. my($p,$f,$l,$s,$h,$w,$e,$r,$a, @a, @ret,$i);
  208. $start = 1 unless $start;
  209. for ($i = $start; ($p,$f,$l,$s,$h,$w,$e,$r) = caller($i); $i++) {
  210. @a = @DB::args;
  211. for (@a) {
  212. s/'/\\'/g;
  213. s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  214. s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  215. s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  216. }
  217. $w = $w ? '@ = ' : '$ = ';
  218. $a = $h ? '(' . join(', ', @a) . ')' : '';
  219. $e =~ s/\n\s*\;\s*\Z// if $e;
  220. $e =~ s/[\\\']/\\$1/g if $e;
  221. if ($r) {
  222. $s = "require '$e'";
  223. } elsif (defined $r) {
  224. $s = "eval '$e'";
  225. } elsif ($s eq '(eval)') {
  226. $s = "eval {...}";
  227. }
  228. $f = "file `$f'" unless $f eq '-e';
  229. push @ret, "$w&$s$a from $f line $l";
  230. last if $DB::signal;
  231. }
  232. return @ret;
  233. }
  234. sub _outputall {
  235. my $c;
  236. for $c (@clients) {
  237. $c->output(@_);
  238. }
  239. }
  240. sub trace_toggle {
  241. my $s = shift;
  242. $DB::trace = !$DB::trace;
  243. }
  244. ####
  245. # without args: returns all defined subroutine names
  246. # with subname args: returns a listref [file, start, end]
  247. #
  248. sub subs {
  249. my $s = shift;
  250. if (@_) {
  251. my(@ret) = ();
  252. while (@_) {
  253. my $name = shift;
  254. push @ret, [$DB::sub{$name} =~ /^(.*)\:(\d+)-(\d+)$/]
  255. if exists $DB::sub{$name};
  256. }
  257. return @ret;
  258. }
  259. return keys %DB::sub;
  260. }
  261. ####
  262. # first argument is a filename whose subs will be returned
  263. # if a filename is not supplied, all subs in the current
  264. # filename are returned.
  265. #
  266. sub filesubs {
  267. my $s = shift;
  268. my $fname = shift;
  269. $fname = $DB::filename unless $fname;
  270. return grep { $DB::sub{$_} =~ /^$fname/ } keys %DB::sub;
  271. }
  272. ####
  273. # returns a list of all filenames that DB knows about
  274. #
  275. sub files {
  276. my $s = shift;
  277. my(@f) = grep(m|^_<|, keys %main::);
  278. return map { substr($_,2) } @f;
  279. }
  280. ####
  281. # returns reference to an array holding the lines in currently
  282. # loaded file
  283. #
  284. sub lines {
  285. my $s = shift;
  286. return \@DB::dbline;
  287. }
  288. ####
  289. # loadfile($file, $line)
  290. #
  291. sub loadfile {
  292. my $s = shift;
  293. my($file, $line) = @_;
  294. if (!defined $main::{'_<' . $file}) {
  295. my $try;
  296. if (($try) = grep(m|^_<.*$file|, keys %main::)) {
  297. $file = substr($try,2);
  298. }
  299. }
  300. if (defined($main::{'_<' . $file})) {
  301. my $c;
  302. # _outputall("Loading file $file..");
  303. *DB::dbline = "::_<$file";
  304. $DB::filename = $file;
  305. for $c (@clients) {
  306. # print "2 ", $file, '|', $line, "\n";
  307. $c->showfile($file, $line);
  308. }
  309. return $file;
  310. }
  311. return undef;
  312. }
  313. sub lineevents {
  314. my $s = shift;
  315. my $fname = shift;
  316. my(%ret) = ();
  317. my $i;
  318. $fname = $DB::filename unless $fname;
  319. local(*DB::dbline) = "::_<$fname";
  320. for ($i = 1; $i <= $#DB::dbline; $i++) {
  321. $ret{$i} = [$DB::dbline[$i], split(/\0/, $DB::dbline{$i})]
  322. if defined $DB::dbline{$i};
  323. }
  324. return %ret;
  325. }
  326. sub set_break {
  327. my $s = shift;
  328. my $i = shift;
  329. my $cond = shift;
  330. $i ||= $DB::lineno;
  331. $cond ||= '1';
  332. $i = _find_subline($i) if ($i =~ /\D/);
  333. $s->output("Subroutine not found.\n") unless $i;
  334. if ($i) {
  335. if ($DB::dbline[$i] == 0) {
  336. $s->output("Line $i not breakable.\n");
  337. }
  338. else {
  339. $DB::dbline{$i} =~ s/^[^\0]*/$cond/;
  340. }
  341. }
  342. }
  343. sub set_tbreak {
  344. my $s = shift;
  345. my $i = shift;
  346. $i = _find_subline($i) if ($i =~ /\D/);
  347. $s->output("Subroutine not found.\n") unless $i;
  348. if ($i) {
  349. if ($DB::dbline[$i] == 0) {
  350. $s->output("Line $i not breakable.\n");
  351. }
  352. else {
  353. $DB::dbline{$i} =~ s/($|\0)/;9$1/; # add one-time-only b.p.
  354. }
  355. }
  356. }
  357. sub _find_subline {
  358. my $name = shift;
  359. $name =~ s/\'/::/;
  360. $name = "${DB::package}\:\:" . $name if $name !~ /::/;
  361. $name = "main" . $name if substr($name,0,2) eq "::";
  362. my($fname, $from, $to) = ($DB::sub{$name} =~ /^(.*):(\d+)-(\d+)$/);
  363. if ($from) {
  364. # XXX this needs local()-ization of some sort
  365. *DB::dbline = "::_<$fname";
  366. ++$from while $DB::dbline[$from] == 0 && $from < $to;
  367. return $from;
  368. }
  369. return undef;
  370. }
  371. sub clr_breaks {
  372. my $s = shift;
  373. my $i;
  374. if (@_) {
  375. while (@_) {
  376. $i = shift;
  377. $i = _find_subline($i) if ($i =~ /\D/);
  378. $s->output("Subroutine not found.\n") unless $i;
  379. if (defined $DB::dbline{$i}) {
  380. $DB::dbline{$i} =~ s/^[^\0]+//;
  381. if ($DB::dbline{$i} =~ s/^\0?$//) {
  382. delete $DB::dbline{$i};
  383. }
  384. }
  385. }
  386. }
  387. else {
  388. for ($i = 1; $i <= $#DB::dbline ; $i++) {
  389. if (defined $DB::dbline{$i}) {
  390. $DB::dbline{$i} =~ s/^[^\0]+//;
  391. if ($DB::dbline{$i} =~ s/^\0?$//) {
  392. delete $DB::dbline{$i};
  393. }
  394. }
  395. }
  396. }
  397. }
  398. sub set_action {
  399. my $s = shift;
  400. my $i = shift;
  401. my $act = shift;
  402. $i = _find_subline($i) if ($i =~ /\D/);
  403. $s->output("Subroutine not found.\n") unless $i;
  404. if ($i) {
  405. if ($DB::dbline[$i] == 0) {
  406. $s->output("Line $i not actionable.\n");
  407. }
  408. else {
  409. $DB::dbline{$i} =~ s/\0[^\0]*//;
  410. $DB::dbline{$i} .= "\0" . $act;
  411. }
  412. }
  413. }
  414. sub clr_actions {
  415. my $s = shift;
  416. my $i;
  417. if (@_) {
  418. while (@_) {
  419. my $i = shift;
  420. $i = _find_subline($i) if ($i =~ /\D/);
  421. $s->output("Subroutine not found.\n") unless $i;
  422. if ($i && $DB::dbline[$i] != 0) {
  423. $DB::dbline{$i} =~ s/\0[^\0]*//;
  424. delete $DB::dbline{$i} if $DB::dbline{$i} =~ s/^\0?$//;
  425. }
  426. }
  427. }
  428. else {
  429. for ($i = 1; $i <= $#DB::dbline ; $i++) {
  430. if (defined $DB::dbline{$i}) {
  431. $DB::dbline{$i} =~ s/\0[^\0]*//;
  432. delete $DB::dbline{$i} if $DB::dbline{$i} =~ s/^\0?$//;
  433. }
  434. }
  435. }
  436. }
  437. sub prestop {
  438. my ($client, $val) = @_;
  439. return defined($val) ? $preeval->{$client} = $val : $preeval->{$client};
  440. }
  441. sub poststop {
  442. my ($client, $val) = @_;
  443. return defined($val) ? $posteval->{$client} = $val : $posteval->{$client};
  444. }
  445. #
  446. # "pure virtual" methods
  447. #
  448. # client-specific pre/post-stop actions.
  449. sub cprestop {}
  450. sub cpoststop {}
  451. # client complete startup
  452. sub awaken {}
  453. sub skippkg {
  454. my $s = shift;
  455. push @skippkg, @_ if @_;
  456. }
  457. sub evalcode {
  458. my ($client, $val) = @_;
  459. if (defined $val) {
  460. $running = 2; # hand over to DB() to evaluate in its context
  461. $ineval->{$client} = $val;
  462. }
  463. return $ineval->{$client};
  464. }
  465. sub ready {
  466. my $s = shift;
  467. return $ready = 1;
  468. }
  469. # stubs
  470. sub init {}
  471. sub stop {}
  472. sub idle {}
  473. sub cleanup {}
  474. sub output {}
  475. #
  476. # client init
  477. #
  478. for (@clients) { $_->init }
  479. $SIG{'INT'} = \&DB::catch;
  480. # disable this if stepping through END blocks is desired
  481. # (looks scary and deconstructivist with Swat)
  482. END { $ready = 0 }
  483. 1;
  484. __END__
  485. =head1 NAME
  486. DB - programmatic interface to the Perl debugging API (draft, subject to
  487. change)
  488. =head1 SYNOPSIS
  489. package CLIENT;
  490. use DB;
  491. @ISA = qw(DB);
  492. # these (inherited) methods can be called by the client
  493. CLIENT->register() # register a client package name
  494. CLIENT->done() # de-register from the debugging API
  495. CLIENT->skippkg('hide::hide') # ask DB not to stop in this package
  496. CLIENT->cont([WHERE]) # run some more (until BREAK or another breakpt)
  497. CLIENT->step() # single step
  498. CLIENT->next() # step over
  499. CLIENT->ret() # return from current subroutine
  500. CLIENT->backtrace() # return the call stack description
  501. CLIENT->ready() # call when client setup is done
  502. CLIENT->trace_toggle() # toggle subroutine call trace mode
  503. CLIENT->subs([SUBS]) # return subroutine information
  504. CLIENT->files() # return list of all files known to DB
  505. CLIENT->lines() # return lines in currently loaded file
  506. CLIENT->loadfile(FILE,LINE) # load a file and let other clients know
  507. CLIENT->lineevents() # return info on lines with actions
  508. CLIENT->set_break([WHERE],[COND])
  509. CLIENT->set_tbreak([WHERE])
  510. CLIENT->clr_breaks([LIST])
  511. CLIENT->set_action(WHERE,ACTION)
  512. CLIENT->clr_actions([LIST])
  513. CLIENT->evalcode(STRING) # eval STRING in executing code's context
  514. CLIENT->prestop([STRING]) # execute in code context before stopping
  515. CLIENT->poststop([STRING])# execute in code context before resuming
  516. # These methods will be called at the appropriate times.
  517. # Stub versions provided do nothing.
  518. # None of these can block.
  519. CLIENT->init() # called when debug API inits itself
  520. CLIENT->stop(FILE,LINE) # when execution stops
  521. CLIENT->idle() # while stopped (can be a client event loop)
  522. CLIENT->cleanup() # just before exit
  523. CLIENT->output(LIST) # called to print any output that API must show
  524. =head1 DESCRIPTION
  525. Perl debug information is frequently required not just by debuggers,
  526. but also by modules that need some "special" information to do their
  527. job properly, like profilers.
  528. This module abstracts and provides all of the hooks into Perl internal
  529. debugging functionality, so that various implementations of Perl debuggers
  530. (or packages that want to simply get at the "privileged" debugging data)
  531. can all benefit from the development of this common code. Currently used
  532. by Swat, the perl/Tk GUI debugger.
  533. Note that multiple "front-ends" can latch into this debugging API
  534. simultaneously. This is intended to facilitate things like
  535. debugging with a command line and GUI at the same time, debugging
  536. debuggers etc. [Sounds nice, but this needs some serious support -- GSAR]
  537. In particular, this API does B<not> provide the following functions:
  538. =over 4
  539. =item *
  540. data display
  541. =item *
  542. command processing
  543. =item *
  544. command alias management
  545. =item *
  546. user interface (tty or graphical)
  547. =back
  548. These are intended to be services performed by the clients of this API.
  549. This module attempts to be squeaky clean w.r.t C<use strict;> and when
  550. warnings are enabled.
  551. =head2 Global Variables
  552. The following "public" global names can be read by clients of this API.
  553. Beware that these should be considered "readonly".
  554. =over 8
  555. =item $DB::sub
  556. Name of current executing subroutine.
  557. =item %DB::sub
  558. The keys of this hash are the names of all the known subroutines. Each value
  559. is an encoded string that has the sprintf(3) format
  560. C<("%s:%d-%d", filename, fromline, toline)>.
  561. =item $DB::single
  562. Single-step flag. Will be true if the API will stop at the next statement.
  563. =item $DB::signal
  564. Signal flag. Will be set to a true value if a signal was caught. Clients may
  565. check for this flag to abort time-consuming operations.
  566. =item $DB::trace
  567. This flag is set to true if the API is tracing through subroutine calls.
  568. =item @DB::args
  569. Contains the arguments of current subroutine, or the C<@ARGV> array if in the
  570. toplevel context.
  571. =item @DB::dbline
  572. List of lines in currently loaded file.
  573. =item %DB::dbline
  574. Actions in current file (keys are line numbers). The values are strings that
  575. have the sprintf(3) format C<("%s\000%s", breakcondition, actioncode)>.
  576. =item $DB::package
  577. Package namespace of currently executing code.
  578. =item $DB::filename
  579. Currently loaded filename.
  580. =item $DB::subname
  581. Fully qualified name of currently executing subroutine.
  582. =item $DB::lineno
  583. Line number that will be executed next.
  584. =back
  585. =head2 API Methods
  586. The following are methods in the DB base class. A client must
  587. access these methods by inheritance (*not* by calling them directly),
  588. since the API keeps track of clients through the inheritance
  589. mechanism.
  590. =over 8
  591. =item CLIENT->register()
  592. register a client object/package
  593. =item CLIENT->evalcode(STRING)
  594. eval STRING in executing code context
  595. =item CLIENT->skippkg('D::hide')
  596. ask DB not to stop in these packages
  597. =item CLIENT->run()
  598. run some more (until a breakpt is reached)
  599. =item CLIENT->step()
  600. single step
  601. =item CLIENT->next()
  602. step over
  603. =item CLIENT->done()
  604. de-register from the debugging API
  605. =back
  606. =head2 Client Callback Methods
  607. The following "virtual" methods can be defined by the client. They will
  608. be called by the API at appropriate points. Note that unless specified
  609. otherwise, the debug API only defines empty, non-functional default versions
  610. of these methods.
  611. =over 8
  612. =item CLIENT->init()
  613. Called after debug API inits itself.
  614. =item CLIENT->prestop([STRING])
  615. Usually inherited from DB package. If no arguments are passed,
  616. returns the prestop action string.
  617. =item CLIENT->stop()
  618. Called when execution stops (w/ args file, line).
  619. =item CLIENT->idle()
  620. Called while stopped (can be a client event loop).
  621. =item CLIENT->poststop([STRING])
  622. Usually inherited from DB package. If no arguments are passed,
  623. returns the poststop action string.
  624. =item CLIENT->evalcode(STRING)
  625. Usually inherited from DB package. Ask for a STRING to be C<eval>-ed
  626. in executing code context.
  627. =item CLIENT->cleanup()
  628. Called just before exit.
  629. =item CLIENT->output(LIST)
  630. Called when API must show a message (warnings, errors etc.).
  631. =back
  632. =head1 BUGS
  633. The interface defined by this module is missing some of the later additions
  634. to perl's debugging functionality. As such, this interface should be considered
  635. highly experimental and subject to change.
  636. =head1 AUTHOR
  637. Gurusamy Sarathy gsar@activestate.com
  638. This code heavily adapted from an early version of perl5db.pl attributable
  639. to Larry Wall and the Perl Porters.
  640. =cut