Skip to content

Commit

Permalink
Finish up fix for #93
Browse files Browse the repository at this point in the history
Use the actual number of items in the backtraces as the backtrace size
instead of the (less reliable) calculated value.
  • Loading branch information
David Rodríguez committed Nov 29, 2014
1 parent 39a189b commit 4da0d30
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 42 deletions.
36 changes: 21 additions & 15 deletions ext/byebug/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@ context_mark(void *data)
rb_gc_mark(context->backtrace);
}

static VALUE
dc_backtrace(const debug_context_t * context)
{
return context->backtrace;
}

static int
dc_stack_size(const debug_context_t * context)
{
return RARRAY_LENINT(dc_backtrace(context));
}

static int
real_stack_size()
{
return FIX2INT(rb_funcall(cContext, rb_intern("stack_size"), 1, Qtrue));
return FIX2INT(rb_funcall(cContext, rb_intern("stack_size"), 0));
}

extern VALUE
Expand Down Expand Up @@ -79,11 +91,6 @@ context_dup(debug_context_t * context)
return Data_Wrap_Struct(cContext, context_mark, 0, new_context);
}

static VALUE
dc_backtrace(const debug_context_t * context)
{
return context->backtrace;
}

static VALUE
dc_frame_get(const debug_context_t * context, int frame_index,
Expand Down Expand Up @@ -344,21 +351,21 @@ Context_resume(VALUE self)

/*
* call-seq:
* context.calced_stack_size-> int
*
* Returns the calculated size of the context stack.
* context.stack_size-> int
*
* NOTE: it shouldn't be necessary to expose this, this is only done to ease
* the detection of TracePoint API bugs.
* Returns the size of the context's stack.
*/
static inline VALUE
Context_calced_stack_size(VALUE self)
Context_stack_size(VALUE self)
{
debug_context_t *context;

Data_Get_Struct(self, debug_context_t, context);

return INT2FIX(context->calced_stack_size);
if (NIL_P(dc_backtrace(context)))
rb_raise(rb_eRuntimeError, "Backtrace not loaded.");

return INT2FIX(dc_stack_size(context));
}

static VALUE
Expand Down Expand Up @@ -639,8 +646,7 @@ Init_context(VALUE mByebug)
rb_define_method(cContext, "frame_self", Context_frame_self, -1);
rb_define_method(cContext, "ignored?", Context_ignored, 0);
rb_define_method(cContext, "resume", Context_resume, 0);
rb_define_method(cContext, "calced_stack_size", Context_calced_stack_size,
0);
rb_define_method(cContext, "stack_size", Context_stack_size, 0);
rb_define_method(cContext, "step_into", Context_step_into, -1);
rb_define_method(cContext, "step_out", Context_step_out, -1);
rb_define_method(cContext, "step_over", Context_step_over, -1);
Expand Down
2 changes: 1 addition & 1 deletion lib/byebug/commands/finish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def regexp
end

def execute
max_frames = Context.stack_size - @state.frame_pos
max_frames = @state.context.stack_size - @state.frame_pos
if @match[1]
n_frames, err = get_int(@match[1], 'finish', 0, max_frames - 1)
return errmsg(err) unless n_frames
Expand Down
18 changes: 6 additions & 12 deletions lib/byebug/commands/frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def c_frame?(frame_no)
end

def switch_to_frame(frame_no)
frame_no >= 0 ? frame_no : @state.context.calced_stack_size + frame_no
frame_no >= 0 ? frame_no : @state.context.stack_size + frame_no
end

def navigate_to_frame(jump_no)
Expand All @@ -22,7 +22,7 @@ def navigate_to_frame(jump_no)

loop do
new_pos += step
break if new_pos < 0 || new_pos >= @state.context.calced_stack_size
break if new_pos < 0 || new_pos >= @state.context.stack_size

next if c_frame?(new_pos)

Expand All @@ -40,7 +40,7 @@ def adjust_frame(frame_pos, absolute)
abs_frame_pos = navigate_to_frame(frame_pos)
end

if abs_frame_pos >= @state.context.calced_stack_size
if abs_frame_pos >= @state.context.stack_size
return errmsg(pr('frame.errors.too_low'))
elsif abs_frame_pos < 0
return errmsg(pr('frame.errors.too_high'))
Expand Down Expand Up @@ -121,17 +121,11 @@ def get_pr_arguments(pos)
end

def print_backtrace
calcedsize = @state.context.calced_stack_size
stacksize = Byebug.post_mortem? ? calcedsize : Context.stack_size

if calcedsize != stacksize
errmsg(pr('frame.errors.stacksize',
calcedsize: calcedsize, realsize: stacksize))
bt = prc('frame.line', (0...@state.context.stack_size)) do |_, index|
get_pr_arguments(index)
end

print(prc('frame.line', (0...stacksize)) do |_, index|
get_pr_arguments(index)
end)
print(bt)
end
end

Expand Down
14 changes: 1 addition & 13 deletions lib/byebug/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,12 @@ module Byebug
#
class Context
class << self
def stack_size(byebug_frames = false)
def stack_size
backtrace = Thread.current.backtrace_locations(0)
return 0 unless backtrace

unless byebug_frames
backtrace = backtrace
.drop_while { |l| !ignored(l.path) }
.drop_while { |l| ignored(l.path) }
.take_while { |l| !ignored(l.path) }
end

backtrace.size
end

def ignored(path)
IGNORED_FILES.include?(path)
end
private :ignored
end

def interrupt
Expand Down
1 change: 0 additions & 1 deletion lib/byebug/printers/texts/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ edit:

frame:
errors:
stacksize: "Byebug's stacksize ({calcedsize}) should be {realsize}. This might be a bug in byebug or ruby's debugging API's"
too_low: "Can't navigate beyond the oldest frame"
too_high: "Can't navigate beyond the newest frame"
c_frame: "Can't navigate to c-frame"
Expand Down

0 comments on commit 4da0d30

Please sign in to comment.