Class: Byebug::BreakCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/byebug/commands/breakpoints.rb

Overview

Implements byebug “break” command.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

command_exists?, commands, find, format_subcmd, format_subcmds, help, inherited, #initialize, load_commands, #match, register_setting_get, register_setting_set, register_setting_var, settings, settings_map, terminal_width

Constructor Details

This class inherits a constructor from Byebug::Command

Class Method Details

.descriptionObject



90
91
92
93
94
95
# File 'lib/byebug/commands/breakpoints.rb', line 90

def description
  %{b[reak] file:line [if expr]
    b[reak] class(.|#)method [if expr]

    Set breakpoint to some position, (optionally) if expr == true}
end

.namesObject



86
87
88
# File 'lib/byebug/commands/breakpoints.rb', line 86

def names
  %w(break)
end

Instance Method Details

#executeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/byebug/commands/breakpoints.rb', line 12

def execute
  return print BreakCommand.help(nil) if BreakCommand.names.include?(@match[0])

  if @match[1]
    line, _, _, expr = @match.captures
  else
    _, file, line, expr = @match.captures
  end
  if expr
    if expr !~ /^\s*if\s+(.+)/
      if file or line
        errmsg "Expecting \"if\" in breakpoint condition; got: #{expr}.\n"
      else
        errmsg "Invalid breakpoint location: #{expr}.\n"
      end
      return
    else
      expr = $1
    end
  end

  brkpt_filename = file
  if file.nil?
    unless @state.context
      errmsg "We are not in a state that has an associated file.\n"
      return
    end
    brkpt_filename = @state.file
    if line.nil?
      # Set breakpoint at current line
      line = @state.line.to_s
    end
  elsif line !~ /^\d+$/
    # See if "line" is a method/function name
    klass = bb_warning_eval(file)
    if klass && klass.kind_of?(Module)
      class_name = klass.name if klass
    else
      errmsg "Unknown class #{file}.\n"
      throw :debug_error
    end
  end

  if line =~ /^\d+$/
    line = line.to_i
    if LineCache.cache(brkpt_filename, Command.settings[:autoreload])
      last_line = LineCache.size(brkpt_filename)
      return errmsg "There are only #{last_line} lines in file " \
                    "#{brkpt_filename}\n" if line > last_line

      return errmsg "Line #{line} is not a stopping point in file " \
                    "#{brkpt_filename}\n" unless
        LineCache.trace_line_numbers(brkpt_filename).member?(line)
    else
      errmsg "No source file named #{brkpt_filename}\n"
      return unless confirm("Set breakpoint anyway? (y/n) ")
    end

    b = Byebug.add_breakpoint brkpt_filename, line, expr
    print "Created breakpoint #{b.id} at " \
          "#{CommandProcessor.canonic_file(brkpt_filename)}:#{line.to_s}\n"
    unless syntax_valid?(expr)
      errmsg "Expression \"#{expr}\" syntactically incorrect; breakpoint" \
             " disabled.\n"
      b.enabled = false
    end
  else
    method = line.intern
    b = Byebug.add_breakpoint class_name, method, expr
    print "Created breakpoint #{b.id} at #{class_name}::#{method.to_s}\n"
  end
end

#regexpObject



8
9
10
# File 'lib/byebug/commands/breakpoints.rb', line 8

def regexp
  /^\s* b(?:reak)? (?:\s+#{Position_regexp})? (?:\s+(.+))? \s*$/x
end