| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011 Rocky Bernstein <rockyb@rubyforge.net> | ||
| 3 | use strict; use warnings; no warnings 'redefine'; | ||
| 4 | use English qw( -no_match_vars ); | ||
| 5 | use rlib '../..'; | ||
| 6 | |||
| 7 | use Class::Struct; | ||
| 8 | use strict; | ||
| 9 | |||
| 10 | struct WatchPoint => { | ||
| 11 | id => '$', # watchpoint number | ||
| 12 | enabled => '$', # True if watchpoint is enabled | ||
| 13 | hits => '$', # How many times watch was hit | ||
| 14 | expr => '$', # what Perl expression to evaluate | ||
| 15 | old_value => '$', # Previous value | ||
| 16 | current_val => '$', # Current value. Set only when != old value | ||
| 17 | }; | ||
| 18 | |||
| 19 | package WatchPoint; | ||
| 20 | sub inspect($) | ||
| 21 | { | ||
| 22 | my $self = shift; | ||
| 23 | sprintf("watchpoint %d, expr %s, old_value: %s, current_value %s", | ||
| 24 | $self->id, $self->expr, $self->old_value // 'undef', | ||
| 25 | $self->current_val // 'undef', | ||
| 26 | ); | ||
| 27 | }; | ||
| 28 | |||
| 29 | package Devel::Trepan::WatchMgr; | ||
| 30 | |||
| 31 | sub new($$) | ||
| 32 | { | ||
| 33 | my ($class, $dbgr) = @_; | ||
| 34 | my $self = {}; | ||
| 35 | $self->{dbgr} = $dbgr; | ||
| 36 | bless $self, $class; | ||
| 37 | $self->clear(); | ||
| 38 | $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub clear($) | ||
| 42 | { | ||
| 43 | my $self = shift; | ||
| 44 | $self->{list} = []; | ||
| 45 | $self->{next_id} = 1; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub inspect($) | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | my $str = ''; | ||
| 52 | for my $watchpoint ($self->list) { | ||
| 53 | next unless defined $watchpoint; | ||
| 54 | $str .= $watchpoint->inspect . "\n"; | ||
| 55 | } | ||
| 56 | $str; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub list($) | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | return @{$self->{list}}; | ||
| 63 | |||
| 64 | } | ||
| 65 | |||
| 66 | # Remove all breakpoints that we have recorded | ||
| 67 | sub DESTROY() { | ||
| 68 | my $self = shift; | ||
| 69 | for my $id ($self->list) { | ||
| 70 | $self->delete_by_object($id) if defined($id); | ||
| 71 | } | ||
| 72 | $self->{clear}; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub find($$) | ||
| 76 | { | ||
| 77 | my ($self, $index) = @_; | ||
| 78 | for my $object ($self->list) { | ||
| 79 | next unless $object; | ||
| 80 | return $object if $object->id eq $index; | ||
| 81 | } | ||
| 82 | return undef; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub delete($$) | ||
| 86 | { | ||
| 87 | my ($self, $index) = @_; | ||
| 88 | my $object = $self->find($index); | ||
| 89 | if (defined ($object)) { | ||
| 90 | $self->delete_by_object($object); | ||
| 91 | return $object; | ||
| 92 | } else { | ||
| 93 | return undef; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub delete_by_object($$) | ||
| 98 | { | ||
| 99 | my ($self, $delete_object) = @_; | ||
| 100 | my @list = $self->list; | ||
| 101 | my $i = 0; | ||
| 102 | for my $candidate (@list) { | ||
| 103 | next unless defined $candidate; | ||
| 104 | if ($candidate eq $delete_object) { | ||
| 105 | splice @list, $i, 1; | ||
| 106 | $self->{list} = \@list; | ||
| 107 | return $delete_object; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | return undef; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub add($$) | ||
| 114 | { | ||
| 115 | my ($self, $expr) = @_; | ||
| 116 | my $watchpoint = WatchPoint->new( | ||
| 117 | id => $self->{next_id}++, | ||
| 118 | enabled => 1, | ||
| 119 | hits => 0, | ||
| 120 | expr => $expr, | ||
| 121 | ); | ||
| 122 | |||
| 123 | push @{$self->{list}}, $watchpoint; | ||
| 124 | return $watchpoint; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub compact($) | ||
| 128 | { | ||
| 129 | my $self = shift; | ||
| 130 | my @new_list = (); | ||
| 131 | for my $watchpoint ($self->list) { | ||
| 132 | next unless defined $watchpoint; | ||
| 133 | push @new_list, $watchpoint; | ||
| 134 | } | ||
| 135 | $self->{list} = \@new_list; | ||
| 136 | return $self->{list}; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub is_empty($) | ||
| 140 | { | ||
| 141 | my $self = shift; | ||
| 142 | $self->compact(); | ||
| 143 | return scalar(0 == $self->list); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub max($) | ||
| 147 | { | ||
| 148 | my $self = shift; | ||
| 149 | my $max = 0; | ||
| 150 | for my $watchpoint ($self->list) { | ||
| 151 | $max = $watchpoint->id if $watchpoint->id > $max; | ||
| 152 | } | ||
| 153 | return $max; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub size($) | ||
| 157 | { | ||
| 158 | my $self = shift; | ||
| 159 | $self->compact(); | ||
| 160 | return scalar $self->list; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub reset($) | ||
| 164 | { | ||
| 165 | my $self = shift; | ||
| 166 | for my $id ($self->list) { | ||
| 167 | $self->{dbgr}->delete_object($id); | ||
| 168 | } | ||
| 169 | $self->{list} = []; | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | unless (caller) { | ||
| 174 | |||
| 175 | eval <<'EOE'; | ||
| 176 | sub wp_status($$) | ||
| 177 | { | ||
| 178 | my ($watchpoints, $i) = @_; | ||
| 179 | printf "list size: %s\n", $watchpoints->size(); | ||
| 180 | printf "max: %d\n", $watchpoints->max() // -1; | ||
| 181 | print $watchpoints->inspect(); | ||
| 182 | print "--- ${i} ---\n"; | ||
| 183 | } | ||
| 184 | EOE | ||
| 185 | |||
| 186 | my $watchpoints = Devel::Trepan::WatchMgr->new('bogus'); | ||
| 187 | wp_status($watchpoints, 0); | ||
| 188 | |||
| 189 | my $watchpoint1 = $watchpoints->add('1+2'); | ||
| 190 | wp_status($watchpoints, 1); | ||
| 191 | $watchpoints->add('3*4'); | ||
| 192 | wp_status($watchpoints, 2); | ||
| 193 | |||
| 194 | $watchpoints->delete_by_object($watchpoint1); | ||
| 195 | wp_status($watchpoints, 3); | ||
| 196 | |||
| 197 | $watchpoints->add('3*4+5'); | ||
| 198 | wp_status($watchpoints, 4); | ||
| 199 | |||
| 200 | $watchpoints->delete(2); | ||
| 201 | wp_status($watchpoints, 5); | ||
| 202 | |||
| 203 | } | ||
| 204 | |||
| 205 | 1; |