How to limit perl debugging in a given set of modules or lib path?

When debugging a perl program using perl -d , how can I limit it to a step only in a given set of modules or a lib path?

+5
source share
1 answer

DB::cmd_b_sub or DB::break_subroutine functions set a breakpoint at the beginning of an arbitrary function. You can go through the code to find a set of arguments to jump to this function. For instance,

 sub add_breakpoints_for_module { my $module = shift; return unless $INC{"perl5db.pl"}; # ie, 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 } } } 

This code should run after loading the appropriate modules.


But how to use this idea as a separate library. Save this code to Devel/ModuleBreaker.pm somewhere on the @INC path and call the debugger as

 perl -d:ModuleBreaker=Some::Module,Some::Other::Module script_to_debug.pl args 

.

 # 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; 

And here is a version that breaks routines that match arbitrary patterns (which should make it easier to split inside submodules). It uses the %DB::sub table, which contains information about all loaded routines (including anonymous subsystems).

 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; } } } 
+2
source

Source: https://habr.com/ru/post/1274663/


All Articles