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