-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprobe.xs
311 lines (258 loc) · 6.5 KB
/
probe.xs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#define PERL_NO_GET_CONTEXT /* we want efficiency */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#define PROBE_TYPE_NONE 0
#define PROBE_TYPE_ONCE 1
#define PROBE_TYPE_PERMANENT 2
#define PROBE_ACTION_LOOKUP 0
#define PROBE_ACTION_REMOVE 1
#define PROBE_SETTINGS_TYPE_INDEX 0
#define PROBE_SETTINGS_ARGS_INDEX 1
/*
* Use preprocessor macros for time-sensitive operations.
*/
#define probe_is_enabled() !!probe_enabled
static Perl_ppaddr_t probe_nextstate_orig = 0;
static int probe_installed = 0;
static int probe_enabled = 0;
static HV* probe_hash = 0;
static SV* probe_trigger_cb = 0;
static void probe_enable(void);
static void probe_disable(void);
static int probe_is_installed(void);
static void probe_install(pTHX);
static void probe_remove(pTHX);
#define DEBUG 0
#define INFO(x) do { if (DEBUG > 0) dbg_printf x; } while (0)
#define TRACE(x) do { if (DEBUG > 1) dbg_printf x; } while (0)
void dbg_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
static void probe_invoke_callback(pTHX_ const char* file, int line, SV* user_arg, SV* callback)
{
int count;
dSP;
ENTER;
SAVETMPS;
PUSHMARK (SP);
EXTEND(SP, user_arg ? 3 : 2);
XPUSHs(sv_2mortal(newSVpv(file, 0)));
XPUSHs(sv_2mortal(newSViv(line)));
if (user_arg) {
XPUSHs(user_arg);
}
PUTBACK;
count = call_sv(callback, G_VOID|G_DISCARD);
if (count != 0) {
croak("probe trigger should have zero return values");
}
FREETMPS;
LEAVE;
}
static bool probe_lookup(pTHX_ const char* file, int line, int action)
{
U32 klen = strlen(file);
char kstr[20];
SV** rlines = 0;
HV* lines = 0;
rlines = hv_fetch(probe_hash, file, klen, 0);
if (rlines) {
lines = (HV*) SvRV(*rlines);
TRACE(("PROBE found entry for file [%s]: %p\n", file, lines));
} else {
return false;
}
klen = sprintf(kstr, "%d", line);
if (hv_exists(lines, kstr, klen)) {
if (action == PROBE_ACTION_REMOVE) {
/* TODO: remove file name when last line for file was removed? */
hv_delete(lines, kstr, klen, G_DISCARD);
INFO(("PROBE removed entry for line [%s]\n", kstr));
}
return true;
} else {
return false;
}
/* catch any mistakes */
return false;
}
static AV* probe_settings(pTHX_ const char* file, int line)
{
U32 klen = strlen(file);
char kstr[20];
SV** rlines = 0;
HV* lines = 0;
SV** rsettings = 0;
AV* settings = 0;
rlines = hv_fetch(probe_hash, file, klen, 0);
if (!rlines) {
return 0;
}
lines = (HV*) SvRV(*rlines);
klen = sprintf(kstr, "%d", line);
rsettings = hv_fetch(lines, kstr, klen, 0);
if (!rsettings) {
return 0;
}
if (!SvROK(*rsettings) || SvTYPE(SvRV(*rsettings)) != SVt_PVAV) {
croak("Devel::Probe settings must be an ARRAY ref");
}
settings = (AV*) SvRV(*rsettings);
return settings;
}
/*
* This function will run for every single line in your Perl code.
* You would do well to make it as cheap as possible.
*/
static OP* probe_nextstate(pTHX)
{
OP* ret = probe_nextstate_orig(aTHX);
do {
const char* file = 0;
int line = 0;
AV* settings = 0;
SV* user_callback_arg = 0;
int type = PROBE_TYPE_NONE;
if (!probe_is_enabled()) {
break;
}
file = CopFILE(PL_curcop);
line = CopLINE(PL_curcop);
TRACE(("PROBE check [%s] [%d]\n", file, line));
if (!probe_lookup(aTHX_ file, line, PROBE_ACTION_LOOKUP)) {
break;
}
settings = probe_settings(aTHX_ file, line);
if (!settings) {
break;
}
type = SvIV(*(av_fetch(settings, PROBE_SETTINGS_TYPE_INDEX, 0)));
if (av_top_index(settings) == PROBE_SETTINGS_ARGS_INDEX) {
user_callback_arg = *(av_fetch(settings, PROBE_SETTINGS_ARGS_INDEX, 0));
}
INFO(("PROBE triggered [%s] [%d] [%d]\n", file, line, type));
if (probe_trigger_cb) {
probe_invoke_callback(aTHX_ file, line, user_callback_arg, probe_trigger_cb);
}
if (type == PROBE_TYPE_ONCE) {
probe_lookup(aTHX_ file, line, PROBE_ACTION_REMOVE);
}
} while (0);
return ret;
}
static void probe_enable(void)
{
if (probe_is_enabled()) {
return;
}
INFO(("PROBE enabling\n"));
probe_enabled = 1;
}
static void probe_clear(pTHX)
{
if (probe_hash) {
hv_clear(probe_hash);
} else {
probe_hash = newHV();
}
INFO(("PROBE cleared\n"));
}
static void probe_reset(pTHX_ int installed)
{
probe_installed = installed;
probe_enabled = 0;
probe_clear(aTHX);
if (probe_trigger_cb) {
SvREFCNT_dec(probe_trigger_cb);
}
probe_trigger_cb = 0;
}
static void probe_disable(void)
{
if (!probe_is_enabled()) {
return;
}
probe_enabled = 0;
INFO(("PROBE disabled\n"));
}
static int probe_is_installed(void)
{
return probe_installed;
}
static void probe_install(pTHX)
{
if (probe_is_installed()) {
return;
}
INFO(("PROBE installed, [%p] => [%p]\n", PL_ppaddr[OP_NEXTSTATE], probe_nextstate));
if (!probe_nextstate_orig) {
probe_nextstate_orig = PL_ppaddr[OP_NEXTSTATE];
}
PL_ppaddr[OP_NEXTSTATE] = probe_nextstate;
probe_reset(aTHX_ 1);
probe_clear(aTHX);
}
static void probe_remove(pTHX)
{
if (!probe_is_installed()) {
return;
}
INFO(("PROBE removed, [%p] => [%p]\n", PL_ppaddr[OP_NEXTSTATE], probe_nextstate_orig));
if (probe_nextstate_orig) {
PL_ppaddr[OP_NEXTSTATE] = probe_nextstate_orig;
}
probe_reset(aTHX_ 0);
}
MODULE = Devel::Probe PACKAGE = Devel::Probe
PROTOTYPES: DISABLE
#################################################################
void
install()
CODE:
probe_install(aTHX);
void
remove()
CODE:
probe_remove(aTHX);
int
is_installed()
CODE:
RETVAL = probe_is_installed();
OUTPUT: RETVAL
void
enable()
CODE:
probe_enable();
void
disable()
CODE:
probe_disable();
int
is_enabled()
CODE:
RETVAL = probe_is_enabled();
OUTPUT: RETVAL
void
clear()
CODE:
probe_disable();
probe_clear(aTHX);
HV *
_internal_probe_state()
CODE:
RETVAL = probe_hash;
OUTPUT: RETVAL
void
trigger(SV* callback)
CODE:
if (probe_trigger_cb == (SV*)NULL) {
probe_trigger_cb = newSVsv(callback);
} else {
SvSetSV(probe_trigger_cb, callback);
}