orDB::cmd_b_sub
函数DB::break_subroutine
在任意函数的开头设置断点。您可以遍历存储区以查找要传递给此函数的参数集。例如,
sub add_breakpoints_for_module {
my $module = shift;
return unless $INC{"perl5db.pl"}; # i.e., unless debugger is on
no strict 'refs';
for my $sub (eval "values %" . $module . "::") {
if (defined &$sub) { # if the symbol is valid sub name
DB::cmd_b_sub(substr($sub,1)); # add breakpoint
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码应在加载相关模块后运行。
以下是如何将这个想法用作单独的库。将此代码保存到路径Devel/ModuleBreaker.pm
上的某个位置@INC
并调用调试器
perl -d:ModuleBreaker=Some::Module,Some::Other::Module script_to_debug.pl args
Run Code Online (Sandbox Code Playgroud)
。
# Devel/ModuleBreaker.pm - automatically break in all subs in arbitrary modules
package Devel::ModuleBreaker;
sub import {
my ($class,@modules) = @_;
our @POSTPONE = @modules;
require "perl5db.pl";
}
CHECK { # expect compile-time mods have been loaded before CHECK phase
for my $module (our @POSTPONE) {
no strict 'refs';
for my $sub (eval "values %" . $module . "::") {
defined &$sub && DB::cmd_b_sub(substr($sub,1));
}
}
}
1;
Run Code Online (Sandbox Code Playgroud)
这是一个会在匹配任意模式的子例程上中断的版本(这应该使得在子模块内部更容易中断)。它利用了该%DB::sub
表,该表包含有关所有加载的子例程(包括匿名子例程)的信息。
package Devel::SubBreaker;
# install as Devel/SubBreaker.pm on @INC
# usage: perl -d:SubBreaker=pattern1,pattern2,... script_to_debug.pl args
sub import {
my $class = shift;
our @patterns = @_;
require "perl5db.pl";
}
CHECK {
foreach my $sub (keys %DB::sub) {
foreach my $pattern (our @patterns) {
$sub =~ $pattern and DB::cmd_b_sub($sub), last;
}
}
}
Run Code Online (Sandbox Code Playgroud)