blib/lib/Devel/DebugHooks/Verbose.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 108 CC 14 SCAR 33.0
CC
14
Cov
0%
trace_subs
56.0
trace_returns
20.0
import
20.0
CRAP 27.2
1 package Devel::DebugHooks::Verbose;
2  
3 our @ISA;
4  
5 BEGIN {
6 push @ISA, 'Devel::DebugHooks';
7 }
8  
9  
10  
11 sub trace_load {
12 my $self = shift;
13  
14 my $res = "Loaded '@_'\n";
15 #FIX: Events are emitted in scalar context (see while condition)
16 # return $res if defined wantarray;
17  
18 print $DB::OUT $res;
19 }
20  
21  
22  
23  
24  
25 my %frame_name = (
26 G => 'GOTO',
27 D => 'DBGF',
28 C => 'FROM',
29 );
30  
31 sub trace_subs {
32 my( $self ) = @_;
33  
34 BEGIN{ 'warnings'->unimport( 'uninitialized' ) if $DB::options{ w } }
35  
36  
37 my $info = '';
38 local $" = ' -';
39 my( $orig_frame, $last_frame );
40 for my $frame ( DB::frames() ) {
41 $last_frame //= $frame if $frame->[0] ne 'D';
42 $orig_frame //= $frame if $frame->[0] ne 'D' && $frame->[0] ne 'G';
43  
44 $info .= $frame_name{ $frame->[0] } .": @$frame[2..5]\n";
45 }
46  
47 my $context = $orig_frame->[7] ? 'list'
48 : defined $orig_frame->[7] ? 'scalar' : 'void';
49  
50 $" = ', ';
51 my @args = map { !defined $_ ? '&undef' : $_ } @{ $orig_frame->[1] };
52 $info =
53 "\n" .' =' x15 ."\n"
54 ."DEEP: ". @{ DB::state( 'stack' ) } ."\n"
55 ."CNTX: $context\n"
56 .$last_frame->[0] ."SUB: " .$last_frame->[5] ."( @args )\n"
57 # print "TEXT: " .DB::location( $DB::sub ) ."\n";
58 # NOTICE: even before function call $DB::sub changes its value to DB::location
59 # A: Because @_ keep the reference to args. So
60 # 1. The reference to $DB::sub is saved into @_
61 # 2. The DB::location is called
62 # 3. The value of $DB::sub is changed to DB::location
63 # 4. my( $sub ) = @_; # Here is too late to get the orig value of $DB::sub
64 ."TEXT: " .DB::location( $last_frame->[5] ) ."\n\n"
65 .$info;
66  
67 $info .= ' =' x15 ."\n";
68  
69 print $DB::OUT $info;
70 }
71  
72  
73  
74 sub trace_returns {
75 my $self = shift;
76  
77 my $info;
78 $info = $DB::options{ trace_subs } ? '' : "\n" .' =' x15 ."\n";
79 # FIX: uninitializind value while 'n'
80 # A: Can not reproduce...
81 $info .= join '->', map { $_->[3] } @{ DB::state( 'goto_frames' ) };
82 $info .= " RETURNS:\n";
83  
84 $info .= @_ ?
85 ' ' .join "\n ", map { defined $_ ? $_ : '&undef' } @_:
86 '>>NOTHING<<';
87  
88 print $DB::OUT $info ."\n" .' =' x15 ."\n";
89 }
90  
91  
92  
93 sub bbreak {
94 my $info = "\n" .' =' x30 .DB::state( 'inDB' ) ."\n";
95  
96 #NOTICE: We do not add '\n' because each line of a source has one
97 $info .= sprintf "%s:%s %s"
98 ,DB::state( 'file' )
99 ,DB::state( 'line' )
100 ,DB::source()->[ DB::state( 'line' ) ]
101 ;
102  
103 print $DB::OUT $info;
104 }
105  
106  
107  
108 sub import {
109 my( $class ) = shift;
110  
111 $class->SUPER::import( @_ );
112  
113 # Enabled by default
114 # The differece when we set option at compile time, we see module loadings
115 # and compilation order whereas setting up it at run time we lack that info
116 $DB::options{ trace_load } //= 1; # compile time option
117 $DB::options{ trace_subs } //= 1; # compile time & runtime option
118 $DB::options{ trace_returns } //= 1;
119  
120  
121 if( $DB::options{ trace_load } ) {
122 my $handler = DB::reg( 'trace_load', 'Verbose' );
123 $$handler->{ code } = \&trace_load;
124 }
125  
126 if( $DB::options{ trace_subs } ) {
127 my $handler = DB::reg( 'call', 'Verbose' );
128 $$handler->{ code } = \&trace_subs;
129 }
130  
131 if( $DB::options{ trace_returns } ) {
132 my $handler = DB::reg( 'trace_returns', 'Verbose' );
133 $$handler->{ code } = \&trace_returns;
134 }
135 }
136  
137  
138  
139 use Devel::DebugHooks();
140  
141 1;