sr / insurance

Insurance is a test coverage analysis tool for Ruby.

This URL has Read+Write access

Simon Rozet (author)
Tue Dec 16 02:47:35 -0800 2008
commit  d2b2ef4941d13c14b2da67c1f7ce02b55dadb980
tree    7d915eef57a316ff477f106e498baf10c63a5b15
insurance / insurance_tracer.c
100644 100 lines (87 sloc) 1.946 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
#include <ruby.h>
#include <node.h>
#include <env.h>
 
static int tracing = 0;
static int am_tracing = 0;
 
static char *
get_event_name(rb_event_t event)
{
  switch (event) {
  case RUBY_EVENT_LINE:
return "line";
  case RUBY_EVENT_CLASS:
return "class";
  case RUBY_EVENT_END:
return "end";
  case RUBY_EVENT_CALL:
return "call";
  case RUBY_EVENT_RETURN:
return "return";
  case RUBY_EVENT_C_CALL:
return "c-call";
  case RUBY_EVENT_C_RETURN:
return "c-return";
  case RUBY_EVENT_RAISE:
return "raise";
  default:
return "unknown";
  }
}
 
static VALUE cov_tracer = 0;
 
static void
insurance_trace_func(event, node, self, id, klass)
    rb_event_t event;
    NODE *node;
    VALUE self;
    ID id;
    VALUE klass;
{
  int state, raised;
  NODE *node_save;
  VALUE srcfile;
  struct FRAME *prev;
  static int counting = 0;
 
  if (tracing) return;
  if (id == ID_ALLOCATOR) return;
  tracing = 1;
  if (node) {
    ruby_current_node = node;
    ruby_frame->node = node;
    ruby_sourcefile = node->nd_file;
    ruby_sourceline = nd_line(node);
  }
 
  srcfile = rb_str_new2(ruby_sourcefile?ruby_sourcefile:"(ruby)");
  rb_funcall(cov_tracer, rb_intern("call"), 3, rb_str_new2(get_event_name(event)), srcfile, INT2FIX(ruby_sourceline));
  tracing = 0;
}
 
static VALUE
is_tracing()
{
  if (am_tracing == 0) {
    return Qfalse;
  }
  return Qtrue;
}
 
static VALUE
set_insurance_trace_func(obj, trace)
    VALUE obj, trace;
{
  if (NIL_P(trace)) {
cov_tracer = 0;
am_tracing = 0;
rb_remove_event_hook(insurance_trace_func);
return Qnil;
  }
 
  cov_tracer = trace;
  am_tracing = 1;
  rb_add_event_hook(insurance_trace_func, RUBY_EVENT_LINE);
  return trace;
}
 
 
void
Init_insurance_tracer()
{
    VALUE mProf;
 
    mProf = rb_define_module("Insurance");
    rb_define_module_function(mProf, "set_trace_func", set_insurance_trace_func, 1);
    rb_define_module_function(mProf, "is_tracing?", is_tracing, 0);
}