Skip to content

Commit

Permalink
try the following approach: use rb_sourcefile to get the current file…
Browse files Browse the repository at this point in the history
… as C string and run all checks on that. Only if all checks are successful, get the current file as VALUE from profile frames.
  • Loading branch information
anmarchenko committed May 17, 2024
1 parent 050e4e7 commit 06ae44e
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions ext/datadog_cov/datadog_cov.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 06ae44e

Please sign in to comment.