Class: Byebug::BreakCommand

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

Overview

Implements breakpoint functionality

Constant Summary collapse

POSITION_REGEXP =
'(?:(\d+)|(.+?)[:.#]([^.:\s]+))'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

commands, find, format_subcmd, format_subcmds, help, inherited, #initialize, load_commands, #match

Constructor Details

This class inherits a constructor from Byebug::Command

Class Method Details

.descriptionObject



75
76
77
78
79
80
# File 'lib/byebug/commands/break.rb', line 75

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



71
72
73
# File 'lib/byebug/commands/break.rb', line 71

def names
  %w(break)
end

Instance Method Details

#executeObject



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
# File 'lib/byebug/commands/break.rb', line 15

def execute
  return puts(self.class.help) if self.class.names.include?(@match[0])

  if @match[1]
    line, _, _, expr = @match.captures
  else
    _, file, line, expr = @match.captures
  end

  if expr && file.nil? && line.nil?
    return errmsg("Invalid breakpoint location: #{expr}")
  elsif expr && expr !~ /^\s*if\s+(.+)/
    return errmsg("Expecting \"if\" in breakpoint condition, got: #{expr}")
  else
    expr = $1
  end

  if file.nil? && !@state.context
    return errmsg('We are not in a state that has an associated file')
  end

  file = @state.file if file.nil?
  line = @state.line.to_s if line.nil?

  if line =~ /^\d+$/
    path = CommandProcessor.canonic_file(file)
    return errmsg("No file named #{path}") unless File.exist?(file)

    l, n = line.to_i, File.foreach(file).count
    return errmsg("There are only #{n} lines in file #{path}") if l > n

    autoreload = Setting[:autoreload]
    possible_lines = LineCache.trace_line_numbers(file, autoreload)
    unless possible_lines.member?(l)
      return errmsg("Line #{l} is not a valid breakpoint in file #{path}")
    end

    b = Breakpoint.add(file, l, expr)
    puts "Created breakpoint #{b.id} at #{path}:#{l}"

    unless syntax_valid?(expr)
      errmsg("Incorrect expression \"#{expr}\"; breakpoint disabled.")
      b.enabled = false
    end

  else
    kl = bb_warning_eval(file)
    return errmsg("Unknown class #{file}") unless kl && kl.is_a?(Module)

    class_name, method = kl.name, line.intern
    b = Breakpoint.add(class_name, method, expr)
    puts "Created breakpoint #{b.id} at #{class_name}::#{method}"
  end
end

#regexpObject



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

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