diff --git a/ext/datadog_cov/datadog_cov.c b/ext/datadog_cov/datadog_cov.c index 017f6887..fdf5c673 100644 --- a/ext/datadog_cov/datadog_cov.c +++ b/ext/datadog_cov/datadog_cov.c @@ -106,24 +106,7 @@ static void dd_cov_update_coverage(rb_event_flag_t event, VALUE data, VALUE self struct dd_cov_data *dd_cov_data; TypedData_Get_Struct(data, struct dd_cov_data, &dd_cov_data_type, dd_cov_data); - int captured_frames = rb_profile_frames( - 0 /* stack starting depth */, - 1, /* number of stack frames to return */ - dd_cov_data->frame_buffer, - NULL); - - if (captured_frames == 0) - { - return; - } - - VALUE filename = rb_profile_frame_path(dd_cov_data->frame_buffer[0]); - if (filename == Qnil) - { - return; - } - - const char *c_filename = RSTRING_PTR(filename); + const char *c_filename = rb_sourcefile(); // skip if we cover the same file again uintptr_t current_filename_ptr = (uintptr_t)c_filename; @@ -134,7 +117,7 @@ static void dd_cov_update_coverage(rb_event_flag_t event, VALUE data, VALUE self dd_cov_data->last_filename_ptr = current_filename_ptr; // if given filename is not located under the root, we skip it - if (dd_cov_data->root_len == 0 || strncmp(dd_cov_data->root, c_filename, dd_cov_data->root_len) != 0) + if (strncmp(dd_cov_data->root, c_filename, dd_cov_data->root_len) != 0) { return; } @@ -146,6 +129,23 @@ static void dd_cov_update_coverage(rb_event_flag_t event, VALUE data, VALUE self return; } + int captured_frames = rb_profile_frames( + 0 /* stack starting depth */, + 1, /* number of stack frames to return */ + dd_cov_data->frame_buffer, + NULL); + + if (captured_frames == 0) + { + return; + } + + VALUE filename = rb_profile_frame_path(dd_cov_data->frame_buffer[0]); + if (filename == Qnil) + { + return; + } + rb_hash_aset(dd_cov_data->coverage, filename, Qtrue); } @@ -154,8 +154,14 @@ static VALUE dd_cov_start(VALUE self) // get current thread VALUE thval = rb_thread_current(); - // add event hook - rb_thread_add_event_hook(thval, dd_cov_update_coverage, RUBY_EVENT_LINE, self); + struct dd_cov_data *dd_cov_data; + TypedData_Get_Struct(self, struct dd_cov_data, &dd_cov_data_type, dd_cov_data); + + if (dd_cov_data->root_len != 0) + { + // add event hook + rb_thread_add_event_hook(thval, dd_cov_update_coverage, RUBY_EVENT_LINE, self); + } return self; }