forked from Trepan-Debuggers/Perl-Devel-Trepan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.pm
944 lines (750 loc) · 24 KB
/
DB.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
# Perl's Core DB.pm library with some corrections, additions,
# modifications and code merged from perl5db.pl
#
# Documentation is at the __END__
#
use rlib '../..';
package DB;
use warnings; no warnings 'redefine';
use English qw( -no_match_vars );
use vars qw($usrctxt $running $caller
$event @ret $ret $return_value @return_value
$stop @clients $ready $tid
$init_dollar0 $OS_STARTUP_DIR);
use Devel::Trepan::DB::Backtrace;
use Devel::Trepan::DB::Breakpoint;
use Devel::Trepan::DB::Eval;
use Devel::Trepan::DB::Sub;
# "private" globals
my ($deep, @saved, @skippkg);
my $ineval = {};
####
#
# Globals - must be defined at startup so that clients can refer to
# them right after a C<use Devel::Trepan::DB;>
#
####
use Cwd;
BEGIN {
no warnings 'once';
$ini_warn = $WARNING;
@ini_INC = @INC; # Save the contents of @INC before they are modified elsewhere.
@ini_ARGV = @ARGV;
$ini_dollar0 = $0;
$OS_STARTUP_DIR = getcwd;
# these are hardcoded in perl source (some are magical)
$DB::sub = ''; # name of current subroutine
$DB::single = 0; # single-step flags. See constants at the
# top of DB/Sub.pm
$DB::signal = 0; # signal flag (will cause a stop at the next line)
$DB::stop = 0; # value of last breakpoint condition evaluation
$DB::tid = undef; # Thread id
@DB::args = (); # arguments of current subroutine or @ARGV array
@DB::dbline = (); # list of lines in currently loaded file
%DB::dbline = (); # actions in current file (keyed by line number)
@DB::clients = ();
# other "public" globals
$DB::package = ''; # current package space
$DB::filename = ''; # current filename
$DB::subname = ''; # currently executing sub (fully qualified name)
# This variable records how many levels we're nested in debugging. Used
# Used in the debugger prompt, and in determining whether it's all over or
# not.
$DB::level = 0; # Level of nested debugging
$DB::lineno = ''; # current line number
$DB::subroutine = '';
$DB::hasargs = '';
$DB::wantarray = '';
$DB::evaltext = '';
$DB::is_require = '';
$DB::hints = '';
$DB::bitmask = '';
$DB::hinthash = '';
$DB::caller = [];
$DB::event = undef; # The reason we have entered the debugger
$DB::VERSION = '1.03';
# initialize private globals to avoid warnings
$running = 1; # are we running, or are we stopped?
$in_debugger = 0;
@clients = ();
$ready = 0;
@saved = ();
@skippkg = ();
$usrctxt = '';
# ensure we can share our non-threaded variables or no-op
if ($ENV{PERL5DB_THREADED}) {
require threads;
require threads::shared;
import threads::shared qw(share);
no strict; no warnings;
$DBGR;
share(\$DBGR);
lock($DBGR);
use strict; use warnings;
print "Thread support enabled\n";
} else {
*lock = sub(*) {};
*share = sub(*) {};
}
# Don't print return values on exiting a subroutine.
$doret = -2;
# "Triggers bug (?) in perl if we postpone this until runtime."
# XXX No details on this yet, or whether we should fix the bug instead
# of work around it. Stay tuned.
@postponed = @stack = (0);
# No extry/exit tracing.
$frame = 0;
}
####
# this is called by Perl for every statement
#
sub DB {
# print "+++ in DB single: ${DB::single}\n";
# lock the debugger and get the thread id for the prompt
lock($DBGR);
if ($ENV{PERL5DB_THREADED}) {
$tid = eval { "[".threads->tid."]" };
}
return unless $ready && !$in_debugger;
local $in_debugger = 1;
@DB::_ = @_;
&save;
# Since DB::DB gets called after every line, we can use caller() to
# figure out where we last were executing. Sneaky, eh? This works because
# caller is returning all the extra information when called from the
# debugger.
$DB::caller = [caller];
($DB::package, $DB::filename, $DB::lineno, $DB::subroutine, $DB::hasargs,
$DB::wantarray, $DB::evaltext, $DB::is_require, $DB::hints, $DB::bitmask,
$DB::hinthash
) = @{$DB::caller};
local $filename_ini = $filename;
return if @skippkg and grep { $_ eq $DB::package } @skippkg;
# Set package namespace for running eval's in the user context.
# However this won't let them modify 'my' variables, alas.
$usrctxt = "package $DB::package; \@_ = \@DB::_;";
local(*DB::dbline) = "::_<$DB::filename";
# we need to check for pseudofiles on Mac OS (these are files
# not attached to a filename, but instead stored in Dev:Pseudo)
if ( $OSNAME eq 'MacOS' && $#dbline < 0 ) {
$filename_ini = $filename = 'Dev:Pseudo';
*dbline = $main::{ '_<' . $filename };
}
$DB::brkpt = undef;
# Increment debugger nesting level.
local $DB::level = $DB::level + 1;
# Test watch expressions;
my $watch_triggered = undef;
for my $c (@clients) {
my $n = 0;
my @list= @{$c->{watch}->{list}};
for my $wp (@list) {
next unless $wp->enabled;
my $new_val = &DB::eval_with_return($usrctxt, $wp->expr,
'$',
@saved);
my $old_val = $wp->old_value;
no warnings 'once';
next if !defined($old_value) and !defined($new_val);
my $not_same = !defined($old_val) || !defined($new_val);
if ( $not_same || $new_val ne $wp->old_value ) {
# Yep! Record change.
$wp->current_val($new_val);
$wp->hits($wp->hits+1);
$watch_triggered = $wp;
last;
}
}
}
# Accumulate action events.
my @action = ();
if (exists $DB::dbline{$DB::lineno} and
my $brkpts = $DB::dbline{$DB::lineno}) {
for (my $i=0; $i < @$brkpts; $i++) {
my $brkpt = $brkpts->[$i];
next unless defined $brkpt;
if ($brkpt->type eq 'action') {
push @action, $brkpt;
next ;
}
$stop = 0;
if ($brkpt->condition eq '1') {
# A cheap and simple test for unconditional.
$stop = 1;
} else {
my $eval_str = sprintf("\$DB::stop = do { %s; }",
$brkpt->condition);
&DB::eval($usrctxt, $eval_str, @saved);
}
if ($stop && $brkpt->enabled) {
$DB::signal |= 1;
$DB::brkpt = $brkpt;
$event = $brkpt->type;
if ($event eq 'tbrkpt') {
# breakpoint is temporary and remove it.
undef $brkpts->[$i];
} else {
my $hits = $brkpt->hits + 1;
$brkpt->hits($hits);
}
last;
}
}
}
if ($watch_triggered) {
$event = 'watch';
} elsif ($DB::signal) {
$event ||= 'signal';
} elsif ($DB::single & RETURN_EVENT) {
$event ||= 'return';
} elsif ($DB::trace ) {
$event ||= 'trace';
} elsif ($DB::single & (SINGLE_STEPPING_EVENT | NEXT_STEPPING_EVENT)) {
$event ||= 'line';
} elsif ($DB::single & DEEP_RECURSION_EVENT) {
$event ||= 'recurse overflow';
} else {
$event = 'unknown';
}
if ($DB::single || $DB::trace || $DB::signal || $event eq 'watch') {
$DB::subname = ($DB::sub =~ /\'|::/) ? $DB::sub : "${DB::package}::$DB::sub"; #';
loadfile($DB::filename, $DB::lineno);
}
for my $action (@action) {
&DB::eval($usrctxt, $action->condition, @saved) if $action->enabled;
my $hits = $action->hits + 1;
$action->hits($hits);
}
if ($DB::single || $DB::signal || $watch_triggered) {
_warnall($#stack . " levels deep in subroutine calls.\n") if $DB::single & 4;
$DB::single = 0;
$DB::signal = 0;
$running = 0;
for my $c (@clients) {
# Now sit in an event loop until something sets $running
my $after_eval = 0;
do {
# Show display expresions
my $display_aref = $c->display_lists;
for my $disp (@$display_aref) {
next unless $disp && $disp->enabled;
# FIXME: allow more than just scalar contexts.
my $eval_result =
&DB::eval_with_return($usrctxt, $disp->arg,
$disp->return_type, @saved);
my $mess = sprintf("%d: $eval_result", $disp->number);
$c->output($mess);
}
if (1 == $after_eval ) {
$event = 'after_eval';
} elsif (2 == $after_eval) {
$event = 'after_nest'
}
# call client event loop; must not block
$c->idle($event, $watch_triggered);
$after_eval = 0;
if ($running == 2 && defined($eval_str)) {
# client wants something eval-ed
# FIXME: turn into subroutine.
local $nest = $eval_opts->{nest};
my $return_type = $eval_opts->{return_type};
$return_type = '' unless defined $return_type;
if ('$' eq $return_type) {
$DB::eval_result =
&DB::eval_with_return($usrctxt, $eval_str,
$return_type, @saved);
} elsif ('@' eq $return_type) {
&DB::eval_with_return($usrctxt, $eval_str,
$return_type, @saved);
} elsif ('%' eq $return_type) {
&DB::eval_with_return($usrctxt, $eval_str,
$return_type, @saved);
} else {
$DB::eval_result =
&DB::eval_with_return($usrctxt, $eval_str,
$return_type, @saved);
}
if ($nest) {
$DB::in_debugger = 1;
$after_eval = 2;
} else {
$after_eval = 1;
}
$running = 0;
}
} until $running;
}
}
$DB::event = undef;
($EVAL_ERROR, $ERRNO, $EXTENDED_OS_ERROR,
$OUTPUT_FIELD_SEPARATOR,
$INPUT_RECORD_SEPARATOR,
$OUTPUT_RECORD_SEPARATOR, $WARNING) = @saved;
();
}
=head1 RESTART SUPPORT
These routines are used to store (and restore) lists of items in environment
variables during a restart.
=head2 set_list
Set_list packages up items to be stored in a set of environment variables
(VAR_n, containing the number of items, and VAR_0, VAR_1, etc., containing
the values). Values outside the standard ASCII charset are stored by encoding
then as hexadecimal values.
=cut
sub set_list {
my ( $stem, @list ) = @_;
my $val;
# VAR_n: how many we have. Scalar assignment gets the number of items.
$ENV{"${stem}_n"} = @list;
# Grab each item in the list, escape the backslashes, encode the non-ASCII
# as hex, and then save in the appropriate VAR_0, VAR_1, etc.
for $i ( 0 .. $#list ) {
$val = $list[$i];
$val =~ s/\\/\\\\/g;
$val =~ s/([\0-\37\177\200-\377])/"\\0x" . unpack('H2',$1)/eg;
$ENV{"${stem}_$i"} = $val;
} ## end for $i (0 .. $#list)
} ## end sub set_list
=head2 get_list
Reverse the set_list operation: grab VAR_n to see how many we should be getting
back, and then pull VAR_0, VAR_1. etc. back out.
=cut
sub get_list {
my $stem = shift;
my @list;
my $n = delete $ENV{"${stem}_n"};
my $val;
for $i ( 0 .. $n - 1 ) {
$val = delete $ENV{"${stem}_$i"};
$val =~ s/\\((\\)|0x(..))/ $2 ? $2 : pack('H2', $3) /ge;
push @list, $val;
}
@list;
} ## end sub get_list
###############################################################################
# no compile-time subroutine call allowed before this point #
###############################################################################
use strict; # this can run only after DB() and sub() are defined
sub save {
@saved = ( $EVAL_ERROR, $ERRNO, $EXTENDED_OS_ERROR,
$OUTPUT_FIELD_SEPARATOR,
$INPUT_RECORD_SEPARATOR,
$OUTPUT_RECORD_SEPARATOR, $WARNING );
$OUTPUT_FIELD_SEPARATOR = "";
$INPUT_RECORD_SEPARATOR = "\n";
$OUTPUT_RECORD_SEPARATOR = "";
$WARNING = 0; # warnings off
}
sub catch {
@DB::_ = @_;
$DB::caller = [caller];
($DB::package, $DB::filename, $DB::lineno, $DB::subroutine, $DB::hasargs,
$DB::wantarray, $DB::evaltext, $DB::is_require, $DB::hints, $DB::bitmask,
$DB::hinthash
) = @{$DB::caller};
$event = 'post-mortem';
$running = 0;
for my $c (@clients) {
# Now sit in an event loop until something sets $running
my $after_eval = 0;
do {
# Show display expresions
my $display_aref = $c->display_lists;
for my $disp (@$display_aref) {
next unless $disp && $disp->enabled;
# FIXME: allow more than just scalar contexts.
my $eval_result =
&DB::eval_with_return($usrctxt, $disp->arg,
$disp->return_type, @saved);
my $mess = sprintf("%d: $eval_result", $disp->number);
$c->output($mess);
}
if (1 == $after_eval ) {
$event = 'after_eval';
} elsif (2 == $after_eval) {
$event = 'after_nest'
}
# call client event loop; must not block
$c->idle($event, 0);
$after_eval = 0;
if ($running == 2 && defined($eval_str)) {
# client wants something eval-ed
# FIXME: turn into subroutine.
my $return_type = $eval_opts->{return_type};
if ('$' eq $return_type) {
$eval_result =
&DB::eval_with_return($usrctxt, $eval_str,
$return_type, @saved);
} elsif ('@' eq $return_type) {
&DB::eval_with_return($usrctxt, $eval_str,
$return_type, @saved);
} elsif ('%' eq $return_type) {
&DB::eval_with_return($usrctxt, $eval_str,
$return_type, @saved);
} else {
$eval_result =
&DB::eval_with_return($usrctxt, $eval_str,
$return_type, @saved);
}
$after_eval = 1;
$running = 0;
}
} until $running;
}
}
####
#
# Client callable (read inheritable) methods defined after this point
#
####
sub register {
my $s = shift;
# $s = _clientname($s) if ref($s);
push @clients, $s;
}
sub done {
my $s = shift;
$s = _clientname($s) if ref($s);
@clients = grep {$_ ne $s} @clients;
$s->cleanup;
# $running = 3 unless @clients;
exit(0) unless @clients;
}
sub _clientname {
my $name = shift;
"$name" =~ /^(.+)=[A-Z]+\(.+\)$/;
return $1;
}
sub step {
my $s = shift;
$DB::single = SINGLE_STEPPING_EVENT;
$DB::running = 1;
}
# cont
# cont fn_or_line
# cont file line
#
sub cont {
my $s = shift;
if (scalar @_ > 0) {
my ($file, $line);
if (2 == scalar @_) {
print @_, "\n";
($file, $line) = @_;
} else {
($file, $line) = ($DB::filename, $_[0]);
}
my $brkpt = $s->set_tbreak($file, $line);
return 0 unless $brkpt;
}
for (my $i = 0; $i <= $#stack;) {
if (defined $stack[$i]) {
$stack[$i++] &= ~1 ;
} else {
# If Enbugger is used $stack[$i] might not be defined
$stack[$i++] = 0;
}
}
$DB::single = 0;
return $DB::running = 1;
}
# stop before finishing the current subroutine
sub finish($;$$) {
my $s = shift;
# how many levels to get to DB sub?
my $count = scalar @_ >= 1 ? shift : 1;
my $scan_for_DB_sub = scalar @_ >= 1 ? shift : 1;
if ($scan_for_DB_sub) {
my $i = 0;
while (my ($pkg, $file, $line, $fn) = caller($i++)) {
if ('DB::DB' eq $fn or ('DB' eq $pkg && 'DB' eq $fn)) {
# FIXME: This is hoaky. 4 is somehow how far off
# @stack is from caller.
$i -= 4;
last;
}
}
$count += $i;
}
my $index = $#stack-$count;
$index = 0 if $index < 0;
$stack[$index] |= RETURN_EVENT;
$DB::single = RETURN_EVENT;
$DB::running = 1;
}
sub return_value($)
{
if ('undef' eq $DB::return_type) {
return undef;
} elsif ('array' eq $DB::return_type) {
return @DB::return_value;
} else {
return $DB::return_value;
}
}
sub return_type($)
{
$DB::return_type;
}
sub _outputall {
my $c;
for $c (@clients) {
$c->output(@_);
}
}
sub _warnall {
my $c;
for $c (@clients) {
$c->warning(@_);
}
}
sub trace_toggle {
my $s = shift;
$DB::trace = !$DB::trace;
}
####
# first argument is a filename whose subs will be returned
# if a filename is not supplied, all subs in the current
# filename are returned.
#
sub filesubs {
my $s = shift;
my $fname = shift;
$fname = $DB::filename unless $fname;
return grep { $DB::sub{$_} =~ /^$fname/ } keys %DB::sub;
}
####
# returns a list of all filenames that DB knows about
#
sub files {
my $s = shift;
my(@f) = grep(m|^_<|, keys %main::);
return map { substr($_,2) } @f;
}
####
# loadfile($file, $line)
#
sub loadfile {
my($file, $line) = @_;
if (!defined $main::{'_<' . $file}) {
my $try;
if (($try) = grep(m|^_<.*$file|, keys %main::)) {
$file = substr($try,2);
}
}
if (defined($main::{'_<' . $file})) {
my $c;
# _outputall("Loading file $file..");
*DB::dbline = "::_<$file";
$DB::filename = $file;
for $c (@clients) {
# print "2 ", $file, '|', $line, "\n";
$c->showfile($file, $line);
}
return $file;
}
return undef;
}
#
# "pure virtual" methods
#
sub skippkg {
my $s = shift;
push @skippkg, @_ if @_;
}
sub evalcode {
my ($client, $expr) = @_;
if (defined $expr) {
$DB::running = 2; # hand over to DB() to evaluate in its context
$ineval->{$client} = $expr;
}
return $ineval->{$client};
}
sub ready {
my $s = shift;
return $ready = 1;
}
# stubs
sub stop {}
sub idle {}
sub cleanup {}
sub output {}
sub warning {}
sub showfile {}
$SIG{'INT'} = \&DB::catch;
# disable this if stepping through END blocks is desired
# (looks scary and deconstructivist with Swat)
END {
$ready = 0;
}
1;
__END__
=head1 NAME
DB - programmatic interface to the Perl debugging API
=head1 SYNOPSIS
package CLIENT;
use DB;
@ISA = qw(DB);
# these (inherited) methods can be called by the client
CLIENT->register() # register a client package name
CLIENT->done() # de-register from the debugging API
CLIENT->skippkg('hide::hide') # ask DB not to stop in this package
CLIENT->cont([WHERE]) # run some more (until BREAK or another breakpt)
CLIENT->step() # single step
CLIENT->next() # step over
CLIENT->finish() # stop before finishing the current subroutine
CLIENT->backtrace() # return the call stack description
CLIENT->ready() # call when client setup is done
CLIENT->trace_toggle() # toggle subroutine call trace mode
CLIENT->subs([SUBS]) # return subroutine information
CLIENT->files() # return list of all files known to DB
CLIENT->loadfile(FILE,LINE) # load a file and let other clients know
CLIENT->line_events() # return info on lines with actions
CLIENT->set_break([WHERE],[COND])
CLIENT->set_tbreak([WHERE])
CLIENT->clr_breaks([LIST])
CLIENT->set_action(WHERE,ACTION)
CLIENT->clr_actions([LIST])
CLIENT->evalcode(STRING) # eval STRING in executing code's context
# These methods you should define; They will be called by the DB
# when appropriate. The stub versions provided do nothing. You should
# Write your routine so that it doesn't block.
CLIENT->init() # called when debug API inits itself
CLIENT->idle(BOOL, EVENT, ARGS) # while stopped (can be a client event loop)
CLIENT->cleanup() # just before exit
CLIENT->output(STRING) # called to print any output that API must show
CLIENT->warning(STRING) # called to print any warning output that API
# must show
CLIENT->showfile(FILE,LINE) # called to show file and line before idling
=head1 DESCRIPTION
Perl debug information is frequently required not just by debuggers,
but also by modules that need some "special" information to do their
job properly, like profilers.
This module abstracts and provides all of the hooks into Perl internal
debugging functionality, so that various implementations of Perl debuggers
(or packages that want to simply get at the "privileged" debugging data)
can all benefit from the development of this common code. Currently used
by Swat, the perl/Tk GUI debugger.
Note that multiple "front-ends" can latch into this debugging API
simultaneously. This is intended to facilitate things like
debugging with a command line and GUI at the same time, debugging
debuggers etc. [Sounds nice, but this needs some serious support -- GSAR]
In particular, this API does B<not> provide the following functions:
=over 4
=item *
data display
=item *
command processing
=item *
command alias management
=item *
user interface (tty or graphical)
=back
These are intended to be services performed by the clients of this API.
This module attempts to be squeaky clean w.r.t C<use strict;> and when
warnings are enabled.
=head2 Global Variables
The following "public" global names can be read by clients of this API.
Beware that these should be considered "readonly".
=over 8
=item $DB::sub
Name of current executing subroutine.
=item %DB::sub
The keys of this hash are the names of all the known subroutines. Each value
is an encoded string that has the sprintf(3) format
C<("%s:%d-%d", filename, fromline, toline)>.
=item $DB::single
Single-step flag. Will be true if the API will stop at the next statement.
=item $DB::signal
Signal flag. Will be set to a true value if a signal was caught. Clients may
check for this flag to abort time-consuming operations.
=item $DB::trace
This flag is set to true if the API is tracing through subroutine calls.
=item @DB::args
Contains the arguments of current subroutine, or the C<@ARGV> array if in the
toplevel context.
=item @DB::dbline
List of lines in currently loaded file.
=item %DB::dbline
Actions in current file (keys are line numbers). The values are strings that
have the sprintf(3) format C<("%s\000%s", breakcondition, actioncode)>.
=item $DB::package
Package namespace of currently executing code.
=item $DB::filename
Currently loaded filename.
=item $DB::subname
Fully qualified name of currently executing subroutine.
=item $DB::lineno
Line number that will be executed next.
=back
=head2 API Methods
The following are methods in the DB base class. A client must
access these methods by inheritance (*not* by calling them directly),
since the API keeps track of clients through the inheritance
mechanism.
=over 8
=item CLIENT->register()
register a client object/package
=item CLIENT->evalcode(STRING)
eval STRING in executing code context
=item CLIENT->skippkg('D::hide')
ask DB not to stop in these packages
=item CLIENT->cont()
continue some more (until a breakpoint is reached)
=item CLIENT->step()
single step
=item CLIENT->next()
step over
=item CLIENT->done()
de-register from the debugging API
=back
=head2 Client Callback Methods
The following "virtual" methods can be defined by the client. They will
be called by the API at appropriate points. Note that unless specified
otherwise, the debug API only defines empty, non-functional default versions
of these methods.
=over 8
=item CLIENT->init()
Called after debug API inits itself.
=item CLIENT->stop()
Called when execution stops (w/ args file, line).
=item CLIENT->idle(BOOLEAN, EVENT, ARGS)
Called while stopped (can be a client event loop or REPL). If called
after the idle program requested an eval to be performed, BOOLEAN will be
true. False otherwise. See evalcode below. ARGS are any
=item CLIENT->evalcode(STRING)
Usually inherited from DB package. Ask for a STRING to be C<eval>-ed
in executing code context.
In order to evaluate properly, control has to be passed back to the DB
subroutine. Suppose you would like your C<idle> program to do this:
until $done {
$command = read input
if $command is a valid debugger command,
run it
else
evaluate it via CLIENT->evalcode($command) and print
the results.
}
Due to the limitation of Perl, the above is not sufficient. You have to
break out of the B<until> to get back to C<DB::sub> to have the eval run.
After that's done, C<DB::sub> will call idle again, from which you can
then retrieve the results.
One other important item to note is that one can only evaluation reliably
current (most recent) frame and not frames further down the stack.
That's probably why the stock Perl debugger doesn't have
frame-switching commands.
=item CLIENT->cleanup()
Called just before exit.
=item CLIENT->output(LIST)
Called when API must show a message (warnings, errors etc.).
=back
=head1 BUGS
The interface defined by this module is missing a number of Perl's
debugging functionality. As such, this interface is subject to
(possibly incompatible) change.
=head1 AUTHOR
Gurusamy Sarathy gsar@activestate.com
This code heavily adapted from an early version of perl5db.pl attributable
to Larry Wall and the Perl Porters.
Further modifications by R. Bernstein rocky@cpan.org
=cut