| 1 | # -*- coding: utf-8 -*- | ||||||||||||||
| 2 | # Copyright (C) 2011-2013 Rocky Bernstein <rocky@cpan.org> | ||||||||||||||
| 3 | # FIXME: Could combine manager code from breakpoints and display | ||||||||||||||
| 4 | 12 | ▶ | use strict; use warnings; no warnings 'redefine'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 12 | ▶ | use English qw( -no_match_vars ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 12 | ▶ | use Class::Struct; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 12 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 12 | struct DBDisplay => { | |||||||||||||
| 11 | number => '$', # display number | ||||||||||||||
| 12 | enabled => '$', # True if display is enabled | ||||||||||||||
| 13 | arg => '$', # What to display | ||||||||||||||
| 14 | fmt => '$', # Format to use in display | ||||||||||||||
| 15 | return_type => '$' # Kind of value expected in return | ||||||||||||||
| 16 | }; | ||||||||||||||
| 17 | |||||||||||||||
| 18 | package DBDisplay; | ||||||||||||||
| 19 | sub inspect($) | ||||||||||||||
| 20 | 0 | ▶ | { | ||||||||||||
|
Subroutine
inspect: not called CC=1 SCAR=6.9
| |||||||||||||||
| 21 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
inspect: not called CC=1 SCAR=6.9
| |||||||||||||||
| 22 | 0 | sprintf("number %s, enabled: %d, fmt: %s, arg: %s", | |||||||||||||
| 23 | $self->number, $self->enabled, $self->fmt, $self->arg); | ||||||||||||||
| 24 | }; | ||||||||||||||
| 25 | |||||||||||||||
| 26 | |||||||||||||||
| 27 | package Devel::Trepan::DB::DisplayMgr; | ||||||||||||||
| 28 | |||||||||||||||
| 29 | sub new($$) | ||||||||||||||
| 30 | 13 | ▶ | { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=1.2
| |||||||||||||||
| 31 | 0 | ▶ | my ($class,$dbgr) = @_; | ||||||||||||
|
Subroutine
new: not called CC=1 SCAR=1.2
| |||||||||||||||
| 32 | 0 | my $self = {max => 0}; | |||||||||||||
| 33 | 0 | $self->{dbgr} = $dbgr; | |||||||||||||
| 34 | 0 | bless $self, $class; | |||||||||||||
| 35 | 0 | $self->clear(); | |||||||||||||
| 36 | 0 | $self; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | sub clear($) | ||||||||||||||
| 40 | 13 | ▶ | { | ||||||||||||
|
Subroutine
clear: called CC=1 SCAR=1.2
| |||||||||||||||
| 41 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=1.2
| |||||||||||||||
| 42 | 0 | $self->{list} = []; | |||||||||||||
| 43 | 0 | $self->{next_id} = 1; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | sub inspect($) | ||||||||||||||
| 47 | 0 | ▶ | { | ||||||||||||
|
Subroutine
inspect: not called CC=3 SCAR=24.8
| |||||||||||||||
| 48 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
inspect: not called CC=3 SCAR=24.8
| |||||||||||||||
| 49 | 0 | my $str = ''; | |||||||||||||
| 50 | 0 | for my $display (@{$self->{list}}) { | |||||||||||||
| 51 | 0 | ▶ | next unless defined $display; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 52 | 0 | $str .= $display->inspect . "\n"; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | 0 | $str; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | # Remove all breakpoints that we have recorded | ||||||||||||||
| 58 | 0 | ▶ | sub DESTROY() { | ||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||||
| 59 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||||
| 60 | 0 | for my $disp (@{$self->{list}}) { | |||||||||||||
| 61 | 0 | ▶ | $self->delete_by_display($disp) if defined($disp); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 62 | } | ||||||||||||||
| 63 | 0 | $self->{clear}; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | sub find($$) | ||||||||||||||
| 67 | { | ||||||||||||||
| 68 | my ($self, $index) = @_; | ||||||||||||||
| 69 | for my $display (@{$self->{list}}) { | ||||||||||||||
| 70 | next unless $display; | ||||||||||||||
| 71 | return $display if $display->number eq $index; | ||||||||||||||
| 72 | } | ||||||||||||||
| 73 | return undef; | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | sub delete($$) | ||||||||||||||
| 77 | 0 | ▶ | { | ||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
| |||||||||||||||
| 78 | 0 | ▶ | my ($self, $index) = @_; | ||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
| |||||||||||||||
| 79 | 0 | my $display = $self->find($index); | |||||||||||||
| 80 | 0 | ▶ | if (defined ($display)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 81 | 0 | $self->delete_by_display($display); | |||||||||||||
| 82 | 0 | return $display; | |||||||||||||
| 83 | } else { | ||||||||||||||
| 84 | 0 | return undef; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | sub delete_by_display($$) | ||||||||||||||
| 89 | 0 | ▶ | { | ||||||||||||
|
Subroutine
delete_by_display: not called CC=4 SCAR=30.0
| |||||||||||||||
| 90 | 0 | ▶ | my ($self, $delete_display) = @_; | ||||||||||||
|
Subroutine
delete_by_display: not called CC=4 SCAR=30.0
| |||||||||||||||
| 91 | 0 | for my $candidate (@{$self->{list}}) { | |||||||||||||
| 92 | 0 | ▶ | next unless defined $candidate; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 93 | 0 | ▶ | if ($candidate eq $delete_display) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 94 | 0 | $candidate = undef; | |||||||||||||
| 95 | 0 | return $delete_display; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | 0 | return undef; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | sub add($$;$) | ||||||||||||||
| 102 | 0 | ▶ | { | ||||||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
| |||||||||||||||
| 103 | 0 | ▶ | my ($self, $display,$return_type) = @_; | ||||||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
| |||||||||||||||
| 104 | 0 | $self->{max}++; | |||||||||||||
| 105 | 0 | ▶ | $return_type = '$' unless defined $return_type; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 106 | 0 | my $number = $self->{max}; | |||||||||||||
| 107 | 0 | my $disp = DBDisplay->new( | |||||||||||||
| 108 | number => $number, | ||||||||||||||
| 109 | enabled => 1, | ||||||||||||||
| 110 | arg => $display, | ||||||||||||||
| 111 | fmt => '$', | ||||||||||||||
| 112 | return_type => $return_type); | ||||||||||||||
| 113 | 0 | push @{$self->{list}}, $disp; | |||||||||||||
| 114 | 0 | return $disp; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | sub compact($) | ||||||||||||||
| 118 | 0 | ▶ | { | ||||||||||||
|
Subroutine
compact: not called CC=3 SCAR=24.8
| |||||||||||||||
| 119 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
compact: not called CC=3 SCAR=24.8
| |||||||||||||||
| 120 | 0 | my @new_list = (); | |||||||||||||
| 121 | 0 | for my $display (@{$self->{list}}) { | |||||||||||||
| 122 | 0 | ▶ | next unless defined $display; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 123 | 0 | push @new_list, $display; | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | 0 | $self->{list} = \@new_list; | |||||||||||||
| 126 | 0 | return $self->{list}; | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | sub is_empty($) | ||||||||||||||
| 130 | 0 | ▶ | { | ||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
| |||||||||||||||
| 131 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
| |||||||||||||||
| 132 | 0 | $self->compact(); | |||||||||||||
| 133 | 0 | return scalar(0 == @{$self->{list}}); | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | sub find($$) | ||||||||||||||
| 137 | 0 | ▶ | { | ||||||||||||
|
Subroutine
find: not called CC=3 SCAR=24.8
| |||||||||||||||
| 138 | 0 | ▶ | my ($self, $num) = @_; | ||||||||||||
|
Subroutine
find: not called CC=3 SCAR=24.8
| |||||||||||||||
| 139 | 0 | for my $display (@{$self->{list}}) { | |||||||||||||
| 140 | 0 | ▶ | return $display if $display->number == $num; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 141 | } | ||||||||||||||
| 142 | 0 | return undef; | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | sub max($) | ||||||||||||||
| 146 | 0 | ▶ | { | ||||||||||||
|
Subroutine
max: not called CC=3 SCAR=24.8
| |||||||||||||||
| 147 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
max: not called CC=3 SCAR=24.8
| |||||||||||||||
| 148 | 0 | my $max = 0; | |||||||||||||
| 149 | 0 | for my $display (@{$self->{list}}) { | |||||||||||||
| 150 | 0 | ▶ | $max = $display->number if $display->number > $max; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 151 | } | ||||||||||||||
| 152 | 0 | return $max; | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | sub size($) | ||||||||||||||
| 156 | 0 | ▶ | { | ||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
| |||||||||||||||
| 157 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
| |||||||||||||||
| 158 | 0 | $self->compact(); | |||||||||||||
| 159 | 0 | return scalar @{$self->{list}}; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | sub reset($) | ||||||||||||||
| 163 | 0 | ▶ | { | ||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
| |||||||||||||||
| 164 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
| |||||||||||||||
| 165 | 0 | $self->{list} = []; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | |||||||||||||||
| 169 | 12 | ▶ | unless (caller) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 170 | |||||||||||||||
| 171 | sub display_status($$) | ||||||||||||||
| 172 | 0 | ▶ | { | ||||||||||||
|
Subroutine
display_status: not called CC=2 SCAR=17.9
| |||||||||||||||
| 173 | 0 | ▶ | my ($displays, $i) = @_; | ||||||||||||
|
Subroutine
display_status: not called CC=2 SCAR=17.9
| |||||||||||||||
| 174 | 0 | printf "list size: %s\n", $displays->size(); | |||||||||||||
| 175 | 0 | my $max = $displays->max(); | |||||||||||||
| 176 | 0 | ▶ | $max = -1 unless defined $max; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 177 | 0 | printf "max: %d\n", $max; | |||||||||||||
| 178 | 0 | print $displays->inspect(); | |||||||||||||
| 179 | 0 | print "--- ${i} ---\n"; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | 0 | eval "use rlib '..';"; | |||||||||||||
| 183 | 0 | require Devel::Trepan::Core; | |||||||||||||
| 184 | 0 | my $dbgr = Devel::Trepan::Core->new; | |||||||||||||
| 185 | 0 | my $displays = Devel::Trepan::DB::DisplayMgr->new($dbgr); | |||||||||||||
| 186 | 0 | display_status($displays, 0); | |||||||||||||
| 187 | 0 | my $display1 = DBDisplay->new( | |||||||||||||
| 188 | number=>1, enabled => 1, arg => '$dbgr', fmt => '$' | ||||||||||||||
| 189 | ); | ||||||||||||||
| 190 | |||||||||||||||
| 191 | 0 | $displays->add($display1); | |||||||||||||
| 192 | 0 | display_status($displays, 1); | |||||||||||||
| 193 | |||||||||||||||
| 194 | 0 | my $display2 = DBDisplay->new( | |||||||||||||
| 195 | number=>2, enabled => 0, arg => '$displays', fmt => '$' | ||||||||||||||
| 196 | ); | ||||||||||||||
| 197 | 0 | $displays->add($display2); | |||||||||||||
| 198 | 0 | display_status($displays, 2); | |||||||||||||
| 199 | |||||||||||||||
| 200 | 0 | $displays->delete_by_display($display1); | |||||||||||||
| 201 | 0 | display_status($displays, 3); | |||||||||||||
| 202 | |||||||||||||||
| 203 | 0 | my $display3 = DBDisplay->new( | |||||||||||||
| 204 | number=>3, enabled => 1, arg => 'foo', fmt => '$' | ||||||||||||||
| 205 | ); | ||||||||||||||
| 206 | 0 | $displays->add($display3); | |||||||||||||
| 207 | 0 | display_status($displays, 4); | |||||||||||||
| 208 | |||||||||||||||
| 209 | } | ||||||||||||||
| 210 | |||||||||||||||
| 211 | 12 | 1; | |||||||||||||