ext/google/cloud/debugger/debugger_c/tracer.c in google-cloud-debugger-0.27.0 vs ext/google/cloud/debugger/debugger_c/tracer.c in google-cloud-debugger-0.28.0
- old
+ new
@@ -53,21 +53,31 @@
/**
* match_breakpoints_files
* Check the Tracer#breakpoints_cache if any breakpoints match the given
* tracepoint_path. Return 1 if found. Otherwise 0;
*/
-static VALUE
+static int
match_breakpoints_files(VALUE self, VALUE tracepoint_path)
{
int i;
- char *c_tracepoint_path = rb_string_value_cstr(&tracepoint_path);
+ char *c_tracepoint_path;
+ VALUE path_breakpoints_hash;
+ VALUE breakpoints_paths;
+ VALUE *c_breakpoints_paths;
+ int breakpoints_paths_len;
- VALUE path_breakpoints_hash = rb_iv_get(self, "@breakpoints_cache");
- VALUE breakpoints_paths = hash_get_keys(path_breakpoints_hash);
- VALUE *c_breakpoints_paths = RARRAY_PTR(breakpoints_paths);
- int breakpoints_paths_len = RARRAY_LEN(breakpoints_paths);
+ // Return 0 if the given path is Qnil
+ if(!RTEST(tracepoint_path)) {
+ return 0;
+ }
+ c_tracepoint_path = rb_string_value_cstr(&tracepoint_path);
+ path_breakpoints_hash = rb_iv_get(self, "@breakpoints_cache");
+ breakpoints_paths = hash_get_keys(path_breakpoints_hash);
+ c_breakpoints_paths = RARRAY_PTR(breakpoints_paths);
+ breakpoints_paths_len = RARRAY_LEN(breakpoints_paths);
+
for (i = 0; i < breakpoints_paths_len; i++) {
VALUE breakpoint_path = c_breakpoints_paths[i];
char *c_breakpoint_path = rb_string_value_cstr(&breakpoint_path);
if (strcmp(c_tracepoint_path, c_breakpoint_path) == 0) {
@@ -137,20 +147,33 @@
*/
static void
line_trace_callback(rb_event_flag_t event, VALUE data, VALUE obj, ID mid, VALUE klass)
{
VALUE self = data;
- const char *c_trace_path = rb_sourcefile();
- int c_trace_lineno = rb_sourceline();
+ VALUE trace_path;
+ int c_trace_lineno;
+ const char *c_trace_path;
VALUE trace_binding;
VALUE call_stack_bindings;
-
- VALUE matching_result = match_breakpoints(self, c_trace_path, c_trace_lineno);
-
ID callers_id;
ID breakpoints_hit_id;
+ VALUE matching_result;
+ c_trace_path = rb_sourcefile();
+ // Ensure C_trace_path is absolute path
+ trace_path = rb_str_new_cstr(c_trace_path);
+ trace_path = rb_file_expand_path(trace_path, Qnil);
+
+ if(!RTEST(trace_path)) {
+ return;
+ }
+
+ c_trace_path = rb_string_value_cstr(&trace_path);
+
+ c_trace_lineno = rb_sourceline();
+ matching_result = match_breakpoints(self, c_trace_path, c_trace_lineno);
+
CONST_ID(callers_id, "callers");
CONST_ID(breakpoints_hit_id, "breakpoints_hit");
// If matching result isn't an array, it means we're in completely wrong file,
// or not on the right line. Turn line tracing off if we're in wrong file.
@@ -161,11 +184,10 @@
return;
}
trace_binding = rb_binding_new();
call_stack_bindings = rb_funcall(trace_binding, callers_id, 0);
- rb_ary_pop(call_stack_bindings);
rb_funcall(self, breakpoints_hit_id, 2, matching_result, call_stack_bindings);
return;
}
@@ -246,33 +268,44 @@
* be the same stack frame that the return_tracepoint is turned on.
*/
static void
return_trace_callback(void *data, rb_trace_arg_t *trace_arg)
{
- VALUE match_found;
+ int match_found;
VALUE self = (VALUE) data;
VALUE caller_locations;
VALUE *c_caller_locations;
VALUE caller_location;
VALUE caller_path;
+ int c_caller_locations_len;
ID caller_locations_id;
ID absolute_path_id;
CONST_ID(caller_locations_id, "caller_locations");
CONST_ID(absolute_path_id, "absolute_path");
- caller_locations = rb_funcall(rb_mKernel, caller_locations_id, 2, INT2NUM(0), INT2NUM(1));
+ caller_locations = rb_funcall(rb_mKernel, caller_locations_id, 2, INT2NUM(1), INT2NUM(1));
+
+ // Return if current execution stack is too shallow.
if(!RTEST(caller_locations)) {
return;
}
c_caller_locations = RARRAY_PTR(caller_locations);
+ c_caller_locations_len = RARRAY_LEN(caller_locations);
+
+ // Make sure caller locations has at least one entry.
+ if(c_caller_locations_len == 0) {
+ return;
+ }
+
caller_location = c_caller_locations[0];
caller_path = rb_funcall(caller_location, absolute_path_id, 0);
+ // Return if caller location doesn't have absolute path. (i.e. dynamically defined method)
if(!RTEST(caller_path)) {
return;
}
match_found = match_breakpoints_files(self, caller_path);
@@ -365,13 +398,20 @@
file_tracepoint_callback(VALUE tracepoint, void *data)
{
VALUE self = (VALUE) data;
rb_trace_arg_t *tracepoint_arg = rb_tracearg_from_tracepoint(tracepoint);
VALUE tracepoint_path = rb_tracearg_path(tracepoint_arg);
- VALUE match_found;
+ int match_found;
if (!RB_TYPE_P(tracepoint_path, T_STRING))
return;
+
+ // Ensure tracepoint_path is absolute path
+ tracepoint_path = rb_file_expand_path(tracepoint_path, Qnil);
+
+ if (!RTEST(tracepoint_path)) {
+ return;
+ }
match_found = match_breakpoints_files(self, tracepoint_path);
if (match_found) {
enable_line_trace_for_thread(self);