/
Sub.pm
407 lines (362 loc) · 13.6 KB
/
Sub.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
# Derived from perl5db.pl
# Tracks calls and returns and stores some stack frame
# information.
package DB;
use warnings; no warnings 'redefine'; use utf8;
no warnings 'once';
use English qw( -no_match_vars );
use constant SINGLE_STEPPING_EVENT => 1;
use constant NEXT_STEPPING_EVENT => 2;
use constant DEEP_RECURSION_EVENT => 4;
use constant RETURN_EVENT => 32;
use constant CALL_EVENT => 64;
use vars qw($return_value @return_value @ret $ret @stack %fn_brkpt $deep);
BEGIN {
@DB::ret = (); # return value of last sub executed in list context
$DB::ret = ''; # return value of last sub executed in scalar context
$DB::return_type = 'undef';
%DB::fn_brkpt = ();
# $deep: Maximium stack depth before we complain.
# See RT #117407
# https://rt.perl.org/rt3//Public/Bug/Display.html?id=117407
# for justification for why this should be 1000 rather than something
# smaller.
$DB::deep = 500;
# $stack_depth is to track the current stack depth using the
# auto-stacked-variable trick. It is 'local'ized repeatedly as
# a simple way to keep track of #stack.
$DB::stack_depth = 0;
@DB::stack = (0); # Per-frame debugger flags
}
sub subcall_debugger {
if ($DB::single || $DB::signal) {
_warnall($#DB::stack . " levels deep in subroutine calls.\n") if $DB::single & 4;
local $DB::event = 'call';
$DB::single = 0;
$DB::signal = 0;
$DB::running = 0;
# lock the debugger and get the thread id for the prompt
if ($ENV{PERL5DB_THREADED}) {
require threads;
require threads::shared;
import threads::shared qw(share);
no strict; no warnings;
lock($DBGR);
$tid = eval { "[".threads->tid."]" };
}
local $OP_addr =
($HAVE_MODULE{'Devel::Callsite'} &&
$Devel::Callsite::VERSION >= 0.08)
? Devel::Callsite::callsite(1) : undef;
$DB::subroutine = $sub;
my $entry = $DB::sub{$sub};
if ($entry =~ /^(.*)\:(\d+)-(\d+)$/) {
$DB::filename = $1;
$DB::lineno = $2;
$DB::caller = [
$DB::filename, $DB::lineno, $DB::subroutine,
0 != scalar(@_), $DB::wantarray
];
}
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;
my $opts = {return_type => $disp->return_type,
namespace_package => $namespace_package,
fix_file_and_line => 1,
hide_position => 0};
# FIXME: allow more than just scalar contexts.
&DB::save_vars();
my $eval_result =
&DB::eval_with_return($disp->arg, $opts, @DB::saved);
my $mess;
if (defined($eval_result)) {
$mess = sprintf("%d: $eval_result", $disp->number);
} else {
$mess = sprintf("%d: undef", $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;
my $opts = $eval_opts;
$opts->{namespace_package} = $namespace_package;
&DB::save_vars();
if ('@' eq $return_type) {
&DB::eval_with_return($eval_str, $opts, @DB::saved);
} elsif ('%' eq $return_type) {
&DB::eval_with_return($eval_str, $opts, @DB::saved);
} else {
$eval_result =
&DB::eval_with_return($eval_str, $opts, @DB::saved);
}
if ($nest) {
$DB::in_debugger = 1;
$after_eval = 2;
} else {
$after_eval = 1;
}
$running = 0;
}
} until $running;
}
}
}
sub check_for_stop() {
my $brkpts = $DB::fn_brkpt{$sub};
if ($brkpts) {
my @action = ();
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);
my $opts = {return_type => ';', # ignore return
namespace_package => $namespace_package,
fix_file_and_line => 1,
hide_position => 0};
&DB::save_vars();
&DB::eval_with_return($eval_str, $opts, @DB::saved);
}
if ($stop && $brkpt->enabled && !($DB::single & RETURN_EVENT)) {
$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);
}
$DB::single = 1;
$DB::wantarray = wantarray;
local $OP_addr =
($HAVE_MODULE{'Devel::Callsite'} &&
$Devel::Callsite::VERSION >= 0.08)
? Devel::Callsite::callsite(1) : undef;
&subcall_debugger() ;
last;
}
}
}
}
# Push the $DB:single onto @DB::stack and set $DB_single.
sub push_DB_single_and_set()
{
# Expand @stack.
$#DB::stack = $DB::stack_depth;
# Save current single-step setting.
$DB::stack[-1] = $DB::single;
# printf "++ \$DB::single for $sub: 0%x\n", $DB::single if $DB::single;
# Turn off all flags except single-stepping or return event.
$DB::single &= SINGLE_STEPPING_EVENT;
# If we've gotten really deeply recursed, turn on the flag that will
# make us stop with the 'deep recursion' message.
$DB::single |= DEEP_RECURSION_EVENT if $#stack == $deep;
}
####
# When debugging is enabled, this routine gets called instead of
# the orignal subroutine. $DB::sub contains the intended subroutine
# to be called. Thus, this routine must run &$DB::sub
# in order to get the original routine called. The fact that
# this routine is called instead allows us to wrap or put code
# around subroutine calls
#
sub DB::sub {
# Do not use a regex in this subroutine -> results in corrupted
# memory See: [perl #66110]
# lock ourselves under threads
lock($DBGR) if $ENV{PERL5DB_THREADED};
# Whether or not the autoloader was running, a scalar to put the
# sub's return value in (if needed), and an array to put the sub's
# return value in (if needed).
my ( $al, $ret, @ret ) = "";
if ($DB::sub eq 'threads::new' && $ENV{PERL5DB_THREADED}) {
print "creating new thread\n";
}
# If the last ten characters are '::AUTOLOAD', note we've traced
# into AUTOLOAD for $DB::sub.
if ( length($DB::sub) > 10 && substr( $DB::sub, -10, 10 ) eq '::AUTOLOAD' ) {
no strict 'refs';
$al = " for $$DB::sub" if defined $$DB::sub;
}
# We stack the stack pointer and then increment it to protect us
# from a situation that might unwind a whole bunch of call frames
# at once. Localizing the stack pointer means that it will automatically
# unwind the same amount when multiple stack frames are unwound.
local $stack_depth = $stack_depth + 1; # Protect from non-local exits
push_DB_single_and_set();
if (defined($DB::running) && $DB::running == 1) {
local @DB::_ = @_;
check_for_stop();
}
if ($DB::sub eq 'DESTROY' or
substr($DB::sub, -9) eq '::DESTROY' or not defined wantarray) {
&$DB::sub;
no warnings 'uninitialized';
$DB::single |= pop(@stack);
$DB::ret = undef;
}
elsif (wantarray) {
# Called in array context. call sub and capture output.
# DB::DB will recursively get control again if appropriate;
# we'll come back here when the sub is finished.
{
no strict 'refs';
# call the original subroutine and save the array value.
@ret = &$DB::sub;
}
# Pop the single-step value back off the stack.
$DB::single |= $stack[ $stack_depth-- ];
if ($single & RETURN_EVENT) {
$DB::return_type = 'array';
@DB::return_value = @ret;
DB::DB($DB::sub) ;
return @DB::return_value;
}
@ret;
} else {
# Called in array context. call sub and capture output.
# DB::DB will recursively get control again if appropriate;
# we'll come back here when the sub is finished.
if ( defined wantarray ) {
no strict 'refs';
# call the original subroutine and save the array value.
$ret = &$DB::sub;
} else {
no strict 'refs';
# Call the original lvalue sub and explicitly void the return
# value.
&$DB::sub;
undef $ret;
}
# Pop the single-step value back off the stack.
$DB::single |= $stack[ $stack_depth-- ] if $stack[$stack_depth];
if ($single & RETURN_EVENT) {
$DB::return_type = defined $ret ? 'scalar' : 'undef';
$DB::return_value = $ret;
DB::DB($DB::sub) ;
return $DB::return_value;
}
# Return the appropriate scalar value.
return $ret;
}
}
####
# When debugging is enabled, this routine gets called instead of the
# orignal subroutine in a left-hand (assignment) context. $DB::sub
# contains the intended subroutine to be called. Thus, this routine
# must run &$DB::sub in order to get the original routine called. The
# fact that this routine is called instead allows us to wrap or
# instrument code around subroutine calls.
#
sub DB::lsub : lvalue {
# Possibly [perl #66110] also applies here as in sub.
# lock ourselves under threads
lock($DBGR) if $ENV{PERL5DB_THREADED};
# Whether or not the autoloader was running, a scalar to put the
# sub's return value in (if needed), and an array to put the sub's
# return value in (if needed).
my ( $al, $ret, @ret ) = "";
if ($DB::sub =~ /^threads::new$/ && $ENV{PERL5DB_THREADED}) {
print "creating new thread\n";
}
# If the last ten characters are '::AUTOLOAD', note we've traced
# into AUTOLOAD for $DB::sub.
if ( length($DB::sub) > 10 && substr( $DB::sub, -10, 10 ) eq '::AUTOLOAD' ) {
$al = " for $$DB::sub" if defined $$DB::sub;;
}
# We stack the stack pointer and then increment it to protect us
# from a situation that might unwind a whole bunch of call frames
# at once. Localizing the stack pointer means that it will automatically
# unwind the same amount when multiple stack frames are unwound.
local $stack_depth = $stack_depth + 1; # Protect from non-local exits
push_DB_single_and_set();
check_for_stop();
if (wantarray) {
# Called in array context. call sub and capture output.
# DB::DB will recursively get control again if appropriate; we'll come
# back here when the sub is finished.
{
no strict 'refs';
@ret = &$DB::sub;
}
# Pop the single-step value back off the stack.
$DB::single |= $stack[ $stack_depth-- ];
if ($DB::single & RETURN_EVENT) {
$DB::return_type = 'array';
@DB::return_value = @ret;
DB::DB($DB::sub) ;
return @DB::return_value;
}
@ret;
} else {
# Called in array context. call sub and capture output.
# DB::DB will recursively get control again if appropriate;
# we'll come back here when the sub is finished.
if ( defined wantarray ) {
no strict 'refs';
# Save the value if it's wanted at all.
$ret = &$DB::sub;
} else {
no strict 'refs';
# Void return, explicitly.
&$DB::sub;
undef $ret;
}
# Pop the single-step value back off the stack.
$DB::single |= $stack[ $stack_depth-- ] if $stack[$stack_depth];
if ($DB::single & RETURN_EVENT) {
$DB::return_type = defined $ret ? 'scalar' : 'undef';
$DB::return_value = $ret;
DB::DB($DB::sub) ;
return $DB::return_value;
}
# Return the appropriate scalar value.
return $ret;
}
}
####
# without args: returns all defined subroutine names
# with subname args: returns a listref [file, start, end]
#
sub subs {
my $s = shift;
if (@_) {
my(@ret) = ();
while (@_) {
my $name = shift;
push @ret, [$DB::sub{$name} =~ /^(.*)\:(\d+)-(\d+)$/]
if exists $DB::sub{$name};
}
return @ret;
}
return keys %DB::sub;
}
1;