-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProvider.xs
349 lines (269 loc) · 8.27 KB
/
Provider.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "Av_CharPtrPtr.h"
#include <usdt.h>
typedef enum {
none = 0,
integer,
string,
json
} perl_argtype_t;
STATIC MGVTBL probe_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 };
STATIC MGVTBL provider_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 };
typedef usdt_provider_t* Devel__DTrace__Provider;
typedef usdt_probedef_t* Devel__DTrace__Probe;
static MAGIC *
load_magic(SV *obj, const MGVTBL *vtbl)
{
MAGIC *mg;
for (mg = SvMAGIC(obj); mg; mg = mg->mg_moremagic)
if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == vtbl)
return mg;
Perl_croak(aTHX_ "Missing magic?");
return NULL;
}
static void *
integer_argument(SV *obj)
{
long ret;
if (SvIOK(obj))
ret = SvIV(obj);
else
Perl_croak(aTHX_ "Argument type mismatch: should be integer");
return (void *) ret;
}
static void *
string_argument(SV *obj)
{
char *ret;
if (SvPOK(obj))
ret = SvPV_nolen(obj);
else
Perl_croak(aTHX_ "Argument type mismatch: should be string");
return (void *) ret;
}
static void *
json_argument(SV *obj)
{
int count;
SV *json;
char *ret = NULL;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(obj);
PUTBACK;
count = call_pv("JSON::to_json", G_EVAL | G_SCALAR);
SPAGAIN;
if (SvTRUE(ERRSV)) {
(void )POPs;
Perl_croak(aTHX_ "Error JSON serializing: %s\n", SvPV_nolen(ERRSV));
}
else {
json = POPs;
if (SvPOK(json)) {
ret = (char *)safemalloc( SvCUR(json) + 1 );
if (ret == NULL)
Perl_croak(aTHX_ "json_object: unable to malloc char*");
else
strcpy(ret, SvPV_nolen(json));
}
}
PUTBACK;
FREETMPS;
LEAVE;
return (void *)ret;
}
MODULE = Devel::DTrace::Provider PACKAGE = Devel::DTrace::Provider
PROTOTYPES: DISABLE
Devel::DTrace::Provider
new(package, name, module)
char *package
char *name
char *module;
INIT:
SV *probes;
CODE:
RETVAL = usdt_create_provider(name, module);
if (RETVAL == NULL)
Perl_croak(aTHX_ "Failed to allocate memory for provider: %s",
strerror(errno));
ST(0) = sv_newmortal();
sv_setref_pv(ST(0), "Devel::DTrace::Provider", (void*)RETVAL);
probes = (SV *)newHV();
sv_magicext(SvRV(ST(0)), probes, PERL_MAGIC_ext,&provider_vtbl,
NULL, 0);
void
DESTROY (self)
Devel::DTrace::Provider self;
INIT:
MAGIC *mg;
CODE:
mg = load_magic(SvRV(ST(0)), &provider_vtbl);
SvREFCNT_dec(mg->mg_obj);
usdt_provider_disable(self);
usdt_provider_free(self);
Devel::DTrace::Probe
add_probe(self, name, function, perl_types)
Devel::DTrace::Provider self
char *name
char *function
char **perl_types;
INIT:
int i;
int argc = 0;
const char *dtrace_types[USDT_ARG_MAX];
perl_argtype_t *types;
MAGIC *mg;
HV *probes;
CODE:
mg = load_magic(SvRV(ST(0)), &provider_vtbl);
probes = (HV *)mg->mg_obj;
types = safemalloc(USDT_ARG_MAX * sizeof(perl_argtype_t));
for (i = 0; i < USDT_ARG_MAX; i++) {
if (perl_types == NULL || perl_types[i] == NULL)
break;
if (strncmp("integer", perl_types[i], 7) == 0) {
dtrace_types[i] = "int";
types[i] = integer;
argc++;
}
else if (strncmp("string", perl_types[i], 6) == 0) {
dtrace_types[i] = "char *";
types[i] = string;
argc++;
}
else if (strncmp("json", perl_types[i], 4) == 0) {
dtrace_types[i] = "char *";
types[i] = json;
argc++;
}
else {
dtrace_types[i] = NULL;
types[i] = none;
}
}
if (perl_types != NULL)
XS_release_charPtrPtr(perl_types);
RETVAL = usdt_create_probe(function, name, argc, dtrace_types);
if (RETVAL == NULL)
Perl_croak(aTHX_ "create probe failed");
if ((usdt_provider_add_probe(self, RETVAL) < 0))
Perl_croak(aTHX_ "add probe: %s", usdt_errstr(self));
ST(0) = sv_newmortal();
sv_setref_pv(ST(0), "Devel::DTrace::Probe", (void*)RETVAL);
sv_magicext(SvRV(ST(0)), Nullsv, PERL_MAGIC_ext, &probe_vtbl,
(const char *) types, 0);
(void) hv_store(probes, name, strlen(name), SvREFCNT_inc((SV *)ST(0)), 0);
void
remove_probe(self, probe)
Devel::DTrace::Provider self
Devel::DTrace::Probe probe;
INIT:
MAGIC *mg;
HV *probes;
CODE:
mg = load_magic(SvRV(ST(0)), &provider_vtbl);
probes = (HV *)mg->mg_obj;
(void) hv_delete(probes, probe->name, strlen(probe->name), G_DISCARD);
if (usdt_provider_remove_probe(self, probe) < 0)
Perl_croak(aTHX_ "%s", usdt_errstr(self));
SV *
enable(self)
Devel::DTrace::Provider self
INIT:
MAGIC *mg;
HV *probes;
CODE:
mg = load_magic(SvRV(ST(0)), &provider_vtbl);
probes = (HV *)mg->mg_obj;
if (usdt_provider_enable(self) < 0)
Perl_croak(aTHX_ "%s", usdt_errstr(self));
RETVAL = newRV_inc((SV *)probes);
OUTPUT:
RETVAL
void
disable(self)
Devel::DTrace::Provider self
CODE:
if (usdt_provider_disable(self) < 0)
Perl_croak(aTHX_ "%s", usdt_errstr(self));
SV *
probes(self)
Devel::DTrace::Provider self
INIT:
MAGIC *mg;
HV *probes;
CODE:
mg = load_magic(SvRV(ST(0)), &provider_vtbl);
probes = (HV *)mg->mg_obj;
RETVAL = newRV_inc((SV *)probes);
OUTPUT:
RETVAL
MODULE = Devel::DTrace::Provider PACKAGE = Devel::DTrace::Probe
PROTOTYPES: DISABLE
void
DESTROY (self)
Devel::DTrace::Probe self;
INIT:
MAGIC *mg;
CODE:
mg = load_magic(SvRV(ST(0)), &probe_vtbl);
safefree(mg->mg_ptr);
usdt_probe_release(self);
int
fire(self, ...)
Devel::DTrace::Probe self
PREINIT:
void *argv[USDT_ARG_MAX];
size_t i, argc = 0;
MAGIC *mg;
perl_argtype_t *types = NULL;
CODE:
argc = items - 1;
if (argc != self->argc)
Perl_croak(aTHX_ "Probe takes %ld arguments, %ld provided",
self->argc, argc);
mg = load_magic(SvRV(ST(0)), &probe_vtbl);
types = (perl_argtype_t *)mg->mg_ptr;
for (i = 0; i < self->argc; i++) {
switch (types[i]) {
case none:
argv[i] = NULL;
break;
case integer:
argv[i] = integer_argument(ST(i + 1));
break;
case string:
argv[i] = string_argument(ST(i + 1));
break;
case json:
argv[i] = json_argument(ST(i + 1));
break;
}
}
usdt_fire_probe(self->probe, argc, argv);
for (i = 0; i < self->argc; i++) {
switch (types[i]) {
case none:
case integer:
case string:
break;
case json:
safefree(argv[i]);
break;
}
}
RETVAL = 1; /* XXX */
OUTPUT:
RETVAL
int
is_enabled(self)
Devel::DTrace::Probe self
CODE:
RETVAL = usdt_is_enabled(self->probe);
OUTPUT:
RETVAL