-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdtrace_hdl.c
More file actions
677 lines (550 loc) · 16.1 KB
/
dtrace_hdl.c
File metadata and controls
677 lines (550 loc) · 16.1 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/* Ruby-DTrace
* (c) 2007 Chris Andrews <chris@nodnol.org>
*/
#include "dtrace_api.h"
RUBY_EXTERN VALUE eDTraceException;
RUBY_EXTERN VALUE cDTrace;
RUBY_EXTERN VALUE cDTraceProbeDesc;
RUBY_EXTERN VALUE cDTraceProgram;
RUBY_EXTERN VALUE cDTraceRecDesc;
RUBY_EXTERN VALUE cDTraceProbeData;
RUBY_EXTERN VALUE cDTraceBufData;
RUBY_EXTERN VALUE cDTraceProcess;
RUBY_EXTERN VALUE cDTraceDropData;
RUBY_EXTERN VALUE cDTraceErrData;
static void dtrace_hdl_free(void *arg)
{
dtrace_handle_t *handle = (dtrace_handle_t *)arg;
VALUE proc;
if (handle->hdl != NULL) {
if (handle->procs != Qnil) {
while ((proc = rb_ary_pop(handle->procs)) != Qnil) {
dtrace_process_release(proc);
}
}
dtrace_close(handle->hdl);
}
free(handle);
}
static void dtrace_hdl_mark(void *arg)
{
dtrace_handle_t *handle = (dtrace_handle_t *)arg;
if (handle) {
rb_gc_mark(handle->probe);
rb_gc_mark(handle->rec);
rb_gc_mark(handle->buf);
rb_gc_mark(handle->err);
rb_gc_mark(handle->drop);
rb_gc_mark(handle->procs);
}
}
VALUE dtrace_hdl_alloc(VALUE klass)
{
dtrace_hdl_t *hdl;
dtrace_handle_t *handle;
int err;
VALUE obj;
hdl = dtrace_open(DTRACE_VERSION, 0, &err);
if (hdl) {
/*
* Leopard's DTrace requires symbol resolution to be
* switched on explicitly
*/
#ifdef __APPLE__
(void) dtrace_setopt(hdl, "stacksymbols", "enabled");
#endif
/* always request flowindent information */
(void) dtrace_setopt(hdl, "flowindent", 0);
handle = ALLOC(dtrace_handle_t);
if (!handle) {
rb_raise(eDTraceException, "alloc failed");
return Qnil;
}
handle->hdl = hdl;
handle->probe = Qnil;
handle->rec = Qnil;
handle->buf = Qnil;
handle->err = Qnil;
handle->drop = Qnil;
handle->procs = Qnil;
obj = Data_Wrap_Struct(klass, dtrace_hdl_mark, dtrace_hdl_free, handle);
return obj;
}
else {
rb_raise(eDTraceException, "unable to open dtrace: %s (not root?)", strerror(err));
return Qnil;
}
}
VALUE dtrace_hdl_close(VALUE self)
{
dtrace_handle_t *handle;
Data_Get_Struct(self, dtrace_handle_t, handle);
dtrace_close(handle->hdl);
handle->hdl = NULL;
return Qnil;
}
static
int _dtrace_next_probe(dtrace_hdl_t *hdl, const dtrace_probedesc_t *pdp, void *arg)
{
VALUE probe;
probe = Data_Wrap_Struct(cDTraceProbeDesc, 0, NULL, (dtrace_probedesc_t *)pdp);
rb_yield(probe);
return 0;
}
/*
* Yields each probe found on the system.
* (equivalent to dtrace -l)
*
* Each probe is represented by a DTraceProbe object
*/
VALUE dtrace_each_probe_all(VALUE self)
{
dtrace_handle_t *handle;
Data_Get_Struct(self, dtrace_handle_t, handle);
(void) dtrace_probe_iter(handle->hdl, NULL, _dtrace_next_probe, NULL);
return self;
}
/*
* Yields each probe found on the system, matching against a
* partial name.
* (equivalent to dtrace -l -n 'probe:::spec')
*
* Each probe is represented by a DTraceProbe object
*/
VALUE dtrace_each_probe_match(VALUE self, VALUE provider, VALUE mod, VALUE func, VALUE name)
{
dtrace_handle_t *handle;
dtrace_probedesc_t desc;
desc.dtpd_id = 0;
strcpy(desc.dtpd_provider, RSTRING_PTR(provider));
strcpy(desc.dtpd_mod, RSTRING_PTR(mod));
strcpy(desc.dtpd_func, RSTRING_PTR(func));
strcpy(desc.dtpd_name, RSTRING_PTR(name));
Data_Get_Struct(self, dtrace_handle_t, handle);
(void) dtrace_probe_iter(handle->hdl, &desc, _dtrace_next_probe, NULL);
return self;
}
static int
_dtrace_next_stmt(dtrace_hdl_t *hdl, dtrace_prog_t *program,
dtrace_stmtdesc_t *stp, dtrace_ecbdesc_t **last)
{
dtrace_ecbdesc_t *edp = stp->dtsd_ecbdesc;
if (edp == *last)
return 0;
if (dtrace_probe_iter(hdl, &edp->dted_probe, _dtrace_next_probe, NULL) != 0) {
rb_raise(eDTraceException, "failed to match %s:%s:%s:%s: %s\n",
edp->dted_probe.dtpd_provider, edp->dted_probe.dtpd_mod,
edp->dted_probe.dtpd_func, edp->dted_probe.dtpd_name,
dtrace_errmsg(hdl, dtrace_errno(hdl)));
}
*last = edp;
return 0;
}
/*
* Yields each probe enabled by the given D program.
* (equivalent to dtrace -n -s program.d)
*/
VALUE dtrace_each_probe_prog(VALUE self, VALUE program)
{
dtrace_handle_t *handle;
dtrace_prog_t *prog;
dtrace_ecbdesc_t *last = NULL;
Data_Get_Struct(self, dtrace_handle_t, handle);
Data_Get_Struct(program, dtrace_prog_t, prog);
(void) dtrace_stmt_iter(handle->hdl, prog, (dtrace_stmt_f *)_dtrace_next_stmt, &last);
return Qnil;
}
/*
* Compile a D program.
*
* Arguments:
* * The program text to compile
* * (Optionally) any arguments required by the program
*
* Raises a DTraceException if the program cannot be compiled.
*/
VALUE dtrace_strcompile(int argc, VALUE *argv, VALUE self)
{
dtrace_handle_t *handle;
dtrace_prog_t *program;
VALUE dtrace_program;
VALUE dtrace_text;
int dtrace_argc;
VALUE dtrace_argv_array;
char **dtrace_argv;
int i;
rb_scan_args(argc, argv, "1*", &dtrace_text, &dtrace_argv_array);
dtrace_argc = rb_ary_len(dtrace_argv_array);
dtrace_argv = ALLOC_N(char *, dtrace_argc + 1);
if (!dtrace_argv) {
rb_raise(eDTraceException, "alloc failed");
return Qnil;
}
for (i = 0; i < dtrace_argc; i++) {
dtrace_argv[i + 1] = strdup(RSTRING_PTR(rb_ary_entry(dtrace_argv_array, i)));
}
dtrace_argv[0] = "ruby";
dtrace_argc++;
Data_Get_Struct(self, dtrace_handle_t, handle);
program = dtrace_program_strcompile(
handle->hdl, RSTRING_PTR(dtrace_text),
DTRACE_PROBESPEC_NAME, DTRACE_C_PSPEC,
dtrace_argc, dtrace_argv
);
if (!program) {
rb_raise(eDTraceException, "%s", dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
return Qnil;
}
else {
dtrace_program = Data_Wrap_Struct(cDTraceProgram, 0, NULL, program);
rb_iv_set(dtrace_program, "@handle", self);
return dtrace_program;
}
}
/*
* Start tracing. Must be called once a program has been successfully
* compiled and executed.
*
* Raises a DTraceException on any error.
*/
VALUE dtrace_hdl_go(VALUE self)
{
dtrace_handle_t *handle;
Data_Get_Struct(self, dtrace_handle_t, handle);
if (dtrace_go(handle->hdl) < 0)
rb_raise(eDTraceException, "%s", dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
return Qnil;
}
/*
* Returns the status of the DTrace handle.
*
* Status values are defined as:
*
* * 0 - none
* * 1 - ok
* * 4 - stopped
*/
VALUE dtrace_hdl_status(VALUE self)
{
dtrace_handle_t *handle;
int status;
Data_Get_Struct(self, dtrace_handle_t, handle);
if ((status = dtrace_status(handle->hdl)) < 0)
rb_raise(eDTraceException, "%s", dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
return INT2FIX(status);
}
/*
* Set an option on the DTrace handle.
*
* Options which may be set:
*
* * aggsize
* * bufsize
*/
VALUE dtrace_hdl_setopt(VALUE self, VALUE key, VALUE value)
{
dtrace_handle_t *handle;
int ret;
Data_Get_Struct(self, dtrace_handle_t, handle);
if (NIL_P(value)) {
ret = dtrace_setopt(handle->hdl, RSTRING_PTR(key), 0);
}
else {
ret = dtrace_setopt(handle->hdl, RSTRING_PTR(key), RSTRING_PTR(value));
}
if (ret < 0)
rb_raise(eDTraceException, "%s", dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
return Qnil;
}
/* Stop tracing.
*
* Must be called after go has been called to start tracing.
*/
VALUE dtrace_hdl_stop(VALUE self)
{
dtrace_handle_t *handle;
Data_Get_Struct(self, dtrace_handle_t, handle);
if (dtrace_stop(handle->hdl) < 0)
rb_raise(eDTraceException, "%s",
dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
return Qnil;
}
/*
* Return the most recent DTrace error.
*/
VALUE dtrace_hdl_error(VALUE self)
{
dtrace_handle_t *handle;
const char *error_string;
Data_Get_Struct(self, dtrace_handle_t, handle);
error_string = dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl));
return rb_str_new2(error_string);
}
/*
* Sleep until we need to wake up to honour D options controlling
* consumption rates.
*/
VALUE dtrace_hdl_sleep(VALUE self)
{
dtrace_handle_t *handle;
Data_Get_Struct(self, dtrace_handle_t, handle);
dtrace_sleep(handle->hdl);
return Qnil;
}
static int _probe_consumer(const dtrace_probedata_t *data, void *arg)
{
VALUE proc;
dtrace_work_handlers_t handlers;
VALUE probedata;
handlers = *(dtrace_work_handlers_t *) arg;
proc = handlers.probe;
if (!NIL_P(proc)) {
probedata = Data_Wrap_Struct(cDTraceProbeData, 0, NULL,
(dtrace_probedata_t *)data);
rb_iv_set(probedata, "@handle", handlers.handle);
rb_funcall(proc, rb_intern("call"), 1, probedata);
}
return (DTRACE_CONSUME_THIS);
}
static int _rec_consumer(const dtrace_probedata_t *data, const dtrace_recdesc_t *rec, void *arg)
{
VALUE proc;
dtrace_work_handlers_t handlers;
VALUE recdesc;
VALUE probedata;
dtrace_actkind_t act;
handlers = *(dtrace_work_handlers_t *) arg;
proc = handlers.rec;
if (!NIL_P(proc)) {
if (rec) {
recdesc = Data_Wrap_Struct(cDTraceRecDesc, 0, NULL, (dtrace_recdesc_t *)rec);
rb_iv_set(recdesc, "@handle", handlers.handle);
rb_funcall(proc, rb_intern("call"), 1, recdesc);
}
else {
rb_funcall(proc, rb_intern("call"), 1, Qnil);
return (DTRACE_CONSUME_NEXT);
}
}
if (rec) {
act = rec->dtrd_action;
if (act == DTRACEACT_EXIT)
return (DTRACE_CONSUME_NEXT);
}
return (DTRACE_CONSUME_THIS);
}
static int _buf_consumer(const dtrace_bufdata_t *bufdata, void *arg)
{
VALUE proc;
VALUE dtracebufdata;
proc = (VALUE)arg;
if (!NIL_P(proc)) {
dtracebufdata = Data_Wrap_Struct(cDTraceBufData, 0, NULL, (dtrace_bufdata_t *)bufdata);
rb_funcall(proc, rb_intern("call"), 1, dtracebufdata);
}
return (DTRACE_HANDLE_OK);
}
/*
* Process any data waiting from the D program.
*
* Takes a Proc to which DTraceProbeData objects will be yielded, and
* an optional second Proc to which DTraceRecDesc objects will be
* yielded.
*
*/
VALUE dtrace_hdl_work(int argc, VALUE *argv, VALUE self)
{
dtrace_handle_t *handle;
dtrace_workstatus_t status;
dtrace_work_handlers_t handlers;
VALUE probe_consumer;
VALUE rec_consumer;
Data_Get_Struct(self, dtrace_handle_t, handle);
/* handle args - probe_consumer_proc is mandatory, rec_consumer_proc
is optional */
rb_scan_args(argc, argv, "11", &probe_consumer, &rec_consumer);
/* to mark during GC */
handle->probe = probe_consumer;
if (!NIL_P(rec_consumer))
handle->rec = rec_consumer;
/* fill out the handlers struct */
handlers.probe = probe_consumer;
handlers.rec = rec_consumer;
handlers.handle = self;
FILE *devnull = fopen("/dev/null", "w");
status = dtrace_work(handle->hdl, devnull, _probe_consumer, _rec_consumer, &handlers);
fclose(devnull);
if (status < 0)
rb_raise(eDTraceException, "%s", dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
return INT2FIX(status);
}
/*
* Set up the buffered output handler for this handle.
*/
VALUE dtrace_hdl_buf_consumer(VALUE self, VALUE buf_consumer)
{
dtrace_handle_t *handle;
Data_Get_Struct(self, dtrace_handle_t, handle);
/* to mark during GC */
handle->buf = buf_consumer;
/* attach the buffered output handler */
if (dtrace_handle_buffered(handle->hdl, &_buf_consumer, (void *)buf_consumer) == -1) {
rb_raise(eDTraceException, "failed to establish buffered handler: %s",
(dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl))));
}
return Qnil;
}
static int _drop_consumer(const dtrace_dropdata_t *dropdata, void *arg)
{
VALUE proc;
VALUE dtracedropdata;
proc = (VALUE)arg;
if (!NIL_P(proc)) {
dtracedropdata = Data_Wrap_Struct(cDTraceDropData, 0, NULL, (dtrace_dropdata_t *)dropdata);
rb_funcall(proc, rb_intern("call"), 1, dtracedropdata);
}
return (DTRACE_HANDLE_OK);
}
/*
* Set up the drop-record handler for this handle. Takes a block,
* which will be called with any drop records returned by DTrace,
* represented by DTraceDropData objects.
*/
VALUE dtrace_hdl_drop_consumer(VALUE self, VALUE drop_consumer)
{
dtrace_handle_t *handle;
Data_Get_Struct(self, dtrace_handle_t, handle);
/* to mark during GC */
handle->drop = drop_consumer;
/* attach the drop-record handler */
if (dtrace_handle_drop(handle->hdl, &_drop_consumer, (void *)drop_consumer) == -1) {
rb_raise(eDTraceException, "failed to establish drop-record handler");
}
return Qnil;
}
static int _err_consumer(const dtrace_errdata_t *errdata, void *arg)
{
VALUE proc;
VALUE dtraceerrdata;
proc = (VALUE)arg;
/* guard against bad invocations where arg is not what we provided... */
if (TYPE(proc) == T_DATA) {
dtraceerrdata = Data_Wrap_Struct(cDTraceErrData, 0, NULL,
(dtrace_errdata_t *)errdata);
rb_funcall(proc, rb_intern("call"), 1, dtraceerrdata);
}
else {
/* arg looked bad, throw an exception */
rb_raise(eDTraceException,
"bad argument to _err_consumer: %p -> type 0x%x\n", arg, TYPE(proc));
}
return (DTRACE_HANDLE_OK);
}
/*
* Set up the err-record handler for this handle. Takes a block, which
* will be called with any error records returned by DTrace,
* represented by DTraceErrData records.
*/
VALUE dtrace_hdl_err_consumer(VALUE self, VALUE err_consumer)
{
dtrace_handle_t *handle;
void *arg;
Data_Get_Struct(self, dtrace_handle_t, handle);
if (dtrace_status(handle->hdl) != 0) {
rb_raise(eDTraceException, "too late to add error handler");
return Qnil;
}
/* to mark during GC */
handle->err = err_consumer;
/* attach the err-record handler */
if (dtrace_handle_err(handle->hdl, &_err_consumer, (void *)err_consumer) == -1) {
rb_raise(eDTraceException, "failed to establish err-record handler: %s",
dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
}
return Qnil;
}
static void _push_proc(dtrace_handle_t *handle, VALUE proc)
{
if (handle->procs == Qnil)
handle->procs = rb_ary_new();
rb_ary_push(handle->procs, proc);
}
/*
* Start a process which will be traced. The pid of the started
* process will be available in D as $target.
*
* Pass an array, where the first element is the full path to the
* program to start, and subsequent elements are its arguments.
*
* Returns a DTraceProcess object which is used to start the process
* once tracing is set up.
*/
VALUE dtrace_hdl_createprocess(VALUE self, VALUE rbargv)
{
dtrace_handle_t *handle;
struct ps_prochandle *P;
char **argv;
long len;
int i;
dtrace_process_t *process;
VALUE rb_process;
Data_Get_Struct(self, dtrace_handle_t, handle);
Check_Type(rbargv, T_ARRAY);
len = rb_ary_len(rbargv);
argv = ALLOC_N(char *, len + 1);
if (!argv) {
rb_raise(eDTraceException, "alloc failed");
return Qnil;
}
for (i = 0; i < len; i++)
argv[i] = strdup(RSTRING_PTR(rb_ary_entry(rbargv, i)));
argv[len] = NULL;
P = dtrace_proc_create(handle->hdl, argv[0], argv);
for (i = 0; i < len; i++)
free(argv[i]);
free(argv);
if (P == NULL)
rb_raise(eDTraceException, "%s",
dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
process = ALLOC(dtrace_process_t);
if (!process) {
rb_raise(eDTraceException, "alloc failed");
return Qnil;
}
process->handle = handle;
process->proc = P;
rb_process = Data_Wrap_Struct(cDTraceProcess, 0, dtrace_process_free,
(dtrace_process_t *)process);
_push_proc(handle, rb_process);
return rb_process;
}
/*
* Grab a currently-running process by pid.
*
* Returns a Rb_Process object which is used to start the process
* once tracing is set up.
*/
VALUE dtrace_hdl_grabprocess(VALUE self, VALUE pid)
{
dtrace_handle_t *handle;
struct ps_prochandle *P;
dtrace_process_t *process;
VALUE rb_process;
Data_Get_Struct(self, dtrace_handle_t, handle);
P = dtrace_proc_grab(handle->hdl, FIX2INT(pid), 0);
if (P == NULL) {
rb_raise(eDTraceException, "%s",
dtrace_errmsg(handle->hdl, dtrace_errno(handle->hdl)));
}
process = ALLOC(dtrace_process_t);
if (!process) {
rb_raise(eDTraceException, "alloc failed");
return Qnil;
}
process->handle = handle;
process->proc = P;
rb_process = Data_Wrap_Struct(cDTraceProcess, 0, dtrace_process_free,
(dtrace_process_t *)process);
_push_proc(handle, rb_process);
return rb_process;
}