/
EventLibrary.nqp
executable file
·294 lines (225 loc) · 7.53 KB
/
EventLibrary.nqp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#! nqp
# Copyright (C) 2010, Parrot Foundation.
# $Id$
=begin
=head1 NAME
runtime/parrot/library/Instrument/EventLibrary.nqp
Library for the many classes that provide handlers for Events.
=head1 SYNOPSIS
Each class in this library provides an interface to hook into
a particular event.
Example usage:
# Create an event handler to receive loadlib notifications.
evt = new ['Instrument';'Event';'Internal';'loadlib']
evt.'set_callback'('loadlib_callback')
instr = new ['Instrument']
instr.attach(evt)
=cut
=end
=begin
=item Instrument::Event::Internal::loadlib
Raises an event whenever a dynlib is loaded.
Events are raised in the C code.
=cut
=end
class Instrument::Event::Internal::loadlib is Instrument::Event {
method _self_init() {
$!event_type := 'Internal::loadlib';
};
};
=begin
=item Instrument::Event::GC
Interface to register callbacks for any given GC event.
Usage (in PIR):
# Inspect a group (allocate) and two separate free events.
$P0 = get_hll_global ['Instrument';'Event'], 'GC'
$P1 = $P0.'new'()
$P1.'callback'('gc_cb2')
$P1.'inspect'('allocate')
$P1.'inspect'('free_pmc_header')
$P1.'inspect'('free_pmc_attributes')
$P2 = new ['Instrument']
$P2.'attach'($P1)
=cut
=end
class Instrument::Event::GC is Instrument::Event {
has @!probe_list;
method _self_init() {
@!probe_list := ();
};
method inspect($item) {
@!probe_list.push($item);
};
method _on_attach() {
self.enable();
};
method enable() {
if $!is_enabled { return; }
# Grab the InstrumentGC and EventDispatcher object.
my $gc := Q:PIR {
$P0 = getattribute self, '$!instr_obj'
%r = $P0['gc']
};
my $dispatcher := Q:PIR {
$P0 = getattribute self, '$!instr_obj'
%r = $P0['eventdispatcher']
};
# For each item in $!probe_list, insert the gc hook
# and register the event handler.
for @!probe_list {
my @hooks := $gc.get_hook_list($_);
for @hooks {
$gc.insert_hook($_);
my $group := $gc.get_hook_group($_).shift();
my $event := 'GC::' ~ $group ~ '::' ~ $_;
# Register the callback.
$dispatcher.register($event, $!callback);
}
}
$!is_enabled := 1;
};
method disable() {
if !$!is_enabled { return; }
# Grab the InstrumentGC and EventDispatcher object.
my $gc := Q:PIR {
$P0 = getattribute self, '$!instr_obj'
%r = $P0['gc']
};
my $dispatcher := Q:PIR {
$P0 = getattribute self, '$!instr_obj'
%r = $P0['eventdispatcher']
};
# For each item in $!probe_list, insert the gc hook
# and register the event handler.
for @!probe_list {
my @hooks := $gc.get_hook_list($_);
for @hooks {
$gc.remove_hook($_);
my $group := $gc.get_hook_group($_).shift();
my $event := 'GC::' ~ $group ~ '::' ~ $_;
# Register the callback.
$dispatcher.deregister($event, $!callback);
}
}
$!is_enabled := 0;
};
};
# Incomplete.
# Inspect VTABLE.
# Inspect methods.
# Must handle dynloaded libraries.
class Instrument::Event::Class is Instrument::Event {
has @!class_names;
has @!vtable_probes;
has @!method_probes;
our @todo;
our $loadlib_event;
our $loadbytecode_event;
method _self_init() {
@!class_names := ();
@!vtable_probes := ();
@!method_probes := ();
};
method inspect_class($class) {
@!class_names.push($class);
};
method inspect_vtable($item) {
@!vtable_probes.push($item);
};
method inspect_method($item) {
@!method_probes.push($item);
};
method _on_attach() {
self.enable();
};
method enable() {
if $!is_enabled { return; }
my $dispatcher := Q:PIR {
$P0 = getattribute self, '$!instr_obj'
%r = $P0['eventdispatcher']
};
for (@!class_names) {
my $class_name := $_;
my $class := $!instr_obj.instrument_class($class_name);
my $event_prefix := 'Class::' ~ $class_name ~ '::';
# Register the vtable probes.
my $vtable_prefix := $event_prefix ~ 'vtable::';
for @!vtable_probes {
my @hooks := $class.get_hook_list($_);
for @hooks {
$class.insert_hook($_);
my $group := ($class.get_hook_group($_)).shift();
my $event := $vtable_prefix ~ $group ~ '::' ~ $_;
$dispatcher.register($event, $!callback);
}
}
CATCH {
# Something was not found.
# Push this probe to the todo list.
@Instrument::Event::Class::todo.push(self);
setup_load_event($!instr_obj);
}
}
$!is_enabled := 1;
};
method disable() {
if !$!is_enabled { return; }
my $dispatcher := Q:PIR {
$P0 = getattribute self, '$!instr_obj'
%r = $P0['eventdispatcher']
};
for (@!class_names) {
my $class_name := $_;
my $class := $!instr_obj.instrument_class($class_name);
my $event_prefix := 'Class::' ~ $class_name ~ '::';
# Register the vtable probes.
my $vtable_prefix := $event_prefix ~ 'vtable::';
for @!vtable_probes {
my @hooks := $class.get_hook_list($_);
for @hooks {
$class.remove_hook($_);
my $group := ($class.get_hook_group($_)).shift();
my $event := $vtable_prefix ~ $group ~ '::' ~ $_;
$dispatcher.deregister($event, $!callback);
}
}
CATCH {
# Ignore the exception.
# We are trying to disable a hook that wasn't inserted.
# TODO: Ensure that the exception came from a place that we are expecting.
# Otherwise rethrow. (How to do that in NQP?)
}
}
$!is_enabled := 0;
};
sub setup_load_event($instrument) {
if !pir::defined__IP($Instrument::Event::Class::loadlib_event) {
# Define the loadlib event for this class.
my $loadlib := Instrument::Event::Internal::loadlib.new();
$loadlib.callback(pir::get_global__PS("load_cb"));
$instrument.attach($loadlib);
$Instrument::Event::Class::loadlib_event := $loadlib;
# Define the load_bytecode event for this class.
my $bytecode := Instrument::Probe.new();
$bytecode.inspect('load_bytecode');
$bytecode.callback(pir::get_global__PS("loadbytecode_cb"));
$instrument.attach($bytecode);
$Instrument::Event::Class::loadbytecode_event := $bytecode;
}
};
sub load_cb($data) {
reload_todos();
}
sub loadbytecode_cb($op, $instr, $probe) {
return pir::get_global__PS('reload_todos');
}
sub reload_todos() {
my @list := @Instrument::Event::Class::todo;
@Instrument::Event::Class::todo := ();
for @list {
$_.disable();
$_.enable();
}
}
};
# vim: ft=perl6 expandtab shiftwidth=4: