Class: Byebug::Breakpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/byebug/breakpoint.rb,
ext/byebug/breakpoint.c

Overview

Implements breakpoints

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, pos, expr) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'ext/byebug/breakpoint.c', line 266

static VALUE
brkpt_initialize(VALUE self, VALUE source, VALUE pos, VALUE expr)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  breakpoint->type = FIXNUM_P(pos) ? BP_POS_TYPE : BP_METHOD_TYPE;
  if (breakpoint->type == BP_POS_TYPE)
    breakpoint->pos.line = FIX2INT(pos);
  else
    breakpoint->pos.mid = SYM2ID(pos);

  breakpoint->id = ++breakpoint_max;
  breakpoint->source = StringValue(source);
  breakpoint->enabled = Qtrue;
  breakpoint->expr = NIL_P(expr) ? expr : StringValue(expr);
  breakpoint->hit_count = 0;
  breakpoint->hit_value = 0;
  breakpoint->hit_condition = HIT_COND_NONE;

  return Qnil;
}

Class Method Details

.add(file, line, expr = nil) ⇒ Object

Adds a new breakpoint

Parameters:

  • file (String)
  • line (Fixnum)
  • expr (String) (defaults to: nil)


29
30
31
32
33
# File 'lib/byebug/breakpoint.rb', line 29

def self.add(file, line, expr = nil)
  breakpoint = Breakpoint.new(file, line, expr)
  Byebug.breakpoints << breakpoint
  breakpoint
end

.firstObject

First breakpoint, in order of creation



11
12
13
# File 'lib/byebug/breakpoint.rb', line 11

def self.first
  Byebug.breakpoints.first
end

.lastObject

Last breakpoint, in order of creation



18
19
20
# File 'lib/byebug/breakpoint.rb', line 18

def self.last
  Byebug.breakpoints.last
end

.none?Boolean

True if there’s no breakpoints

Returns:

  • (Boolean)


81
82
83
# File 'lib/byebug/breakpoint.rb', line 81

def self.none?
  Byebug.breakpoints.empty?
end

.potential_line?(filename, lineno) ⇒ Boolean

Returns true if a breakpoint could be set in line number lineno in file name +filename.

Returns:

  • (Boolean)


74
75
76
# File 'lib/byebug/breakpoint.rb', line 74

def self.potential_line?(filename, lineno)
  potential_lines(filename).member?(lineno)
end

.potential_lines(filename) ⇒ Object

Returns an array of line numbers in file named filename where breakpoints could be set. The list will contain an entry for each distinct line event call so it is possible (and possibly useful) for a line number appear more than once.

Parameters:

  • filename (String)

    File name to inspect for possible breakpoints



52
53
54
55
56
57
# File 'lib/byebug/breakpoint.rb', line 52

def self.potential_lines(filename)
  name = "#{Time.new.to_i}_#{rand(2**31)}"
  iseq = RubyVM::InstructionSequence.compile(File.read(filename), name)

  potential_lines_with_trace_points(iseq, {})
end

.remove(id) ⇒ Object

Removes a breakpoint

Parameters:

  • id (integer)

    breakpoint number



40
41
42
# File 'lib/byebug/breakpoint.rb', line 40

def self.remove(id)
  Byebug.breakpoints.reject! { |b| b.id == id }
end

Instance Method Details

#enabled=(true) ⇒ Object

Enables or disables breakpoint.



67
68
69
70
71
72
73
# File 'ext/byebug/breakpoint.c', line 67

static VALUE
brkpt_set_enabled(VALUE self, VALUE enabled)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  return breakpoint->enabled = enabled;
}

#enabled?Boolean

Returns true if breakpoint is enabled, false otherwise.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'ext/byebug/breakpoint.c', line 53

static VALUE
brkpt_enabled(VALUE self)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  return breakpoint->enabled;
}

#exprString

Returns a conditional expression which indicates when this breakpoint should be activated.

Returns:

  • (String)


82
83
84
85
86
87
88
# File 'ext/byebug/breakpoint.c', line 82

static VALUE
brkpt_expr(VALUE self)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  return breakpoint->expr;
}

#expr=(string) ⇒ Object

Sets or unsets the conditional expression which indicates when this breakpoint should be activated.



97
98
99
100
101
102
103
104
# File 'ext/byebug/breakpoint.c', line 97

static VALUE
brkpt_set_expr(VALUE self, VALUE expr)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  breakpoint->expr = NIL_P(expr) ? expr : StringValue(expr);
  return expr;
}

#hit_conditionObject

Returns the hit condition of the breakpoint: nil if it is an

unconditional breakpoint, or :greater_or_equal, :equal or :modulo otherwise


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'ext/byebug/breakpoint.c', line 113

static VALUE
brkpt_hit_condition(VALUE self)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  switch (breakpoint->hit_condition)
  {
    case HIT_COND_GE:
      return ID2SYM(rb_intern("greater_or_equal"));
    case HIT_COND_EQ:
      return ID2SYM(rb_intern("equal"));
    case HIT_COND_MOD:
      return ID2SYM(rb_intern("modulo"));
    case HIT_COND_NONE:
    default:
      return Qnil;
  }
}

#hit_condition=(symbol) ⇒ Object

Sets the hit condition of the breakpoint which must be one of the following values:

nil if it is an unconditional breakpoint, or :greater_or_equal(:ge), :equal(:eq), :modulo(:mod)



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/byebug/breakpoint.c', line 142

static VALUE
brkpt_set_hit_condition(VALUE self, VALUE value)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);
  ID id_value;

  if (NIL_P(value))
  {
    breakpoint->hit_condition = HIT_COND_NONE;
    return value;
  }

  id_value = rb_to_id(value);

  if (rb_intern("greater_or_equal") == id_value || rb_intern("ge") == id_value)
    breakpoint->hit_condition = HIT_COND_GE;
  else if (rb_intern("equal") == id_value || rb_intern("eq") == id_value)
    breakpoint->hit_condition = HIT_COND_EQ;
  else if (rb_intern("modulo") == id_value || rb_intern("mod") == id_value)
    breakpoint->hit_condition = HIT_COND_MOD;
  else
    rb_raise(rb_eArgError, "Invalid condition parameter");
  return value;
}

#hit_countInteger

Returns the number of times this breakpoint has been hit.

Returns:

  • (Integer)


173
174
175
176
177
178
179
# File 'ext/byebug/breakpoint.c', line 173

static VALUE
brkpt_hit_count(VALUE self)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  return INT2FIX(breakpoint->hit_count);
}

#hit_valueInteger

Returns the hit value of the breakpoint, namely, a value to build a condition on the number of hits of the breakpoint.

Returns:

  • (Integer)


188
189
190
191
192
193
194
# File 'ext/byebug/breakpoint.c', line 188

static VALUE
brkpt_hit_value(VALUE self)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  return INT2FIX(breakpoint->hit_value);
}

#hit_value=(int) ⇒ Object

Sets the hit value of the breakpoint. This allows the user to set conditions on the number of hits to enable/disable the breakpoint.



203
204
205
206
207
208
209
210
# File 'ext/byebug/breakpoint.c', line 203

static VALUE
brkpt_set_hit_value(VALUE self, VALUE value)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  breakpoint->hit_value = FIX2INT(value);
  return value;
}

#idInteger

Returns the id of the breakpoint.

Returns:

  • (Integer)


218
219
220
221
222
223
224
# File 'ext/byebug/breakpoint.c', line 218

static VALUE
brkpt_id(VALUE self)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  return INT2FIX(breakpoint->id);
}

#inspectObject

Prints all information associated to the breakpoint



88
89
90
91
92
# File 'lib/byebug/breakpoint.rb', line 88

def inspect
  meths = %w[id pos source expr hit_condition hit_count hit_value enabled?]
  values = meths.map { |field| "#{field}: #{send(field)}" }.join(", ")
  "#<Byebug::Breakpoint #{values}>"
end

#posString, Integer

Returns the position of this breakpoint, either a method name or a line

number.

Returns:

  • (String, Integer)


233
234
235
236
237
238
239
240
241
242
# File 'ext/byebug/breakpoint.c', line 233

static VALUE
brkpt_pos(VALUE self)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  if (breakpoint->type == BP_METHOD_TYPE)
    return rb_str_new2(rb_id2name(breakpoint->pos.mid));
  else
    return INT2FIX(breakpoint->pos.line);
}

#sourceString

Returns the source file of the breakpoint.

Returns:

  • (String)


250
251
252
253
254
255
256
# File 'ext/byebug/breakpoint.c', line 250

static VALUE
brkpt_source(VALUE self)
{
  breakpoint_t *breakpoint = brkpt_ptr(self);

  return breakpoint->source;
}