| Class | Debugger::AddBreakpoint |
| In: |
cli/ruby-debug/commands/breakpoints.rb
|
| Parent: | Command |
Implements debugger "break" command.
# File cli/ruby-debug/commands/breakpoints.rb, line 104
104: def help(cmd)
105: %{
106: b[reak] file:line [if expr]
107: b[reak] class(.|#)method [if expr]
108: \tset breakpoint to some position, (optionally) if expr == true
109: }
110: end
# File cli/ruby-debug/commands/breakpoints.rb, line 16
16: def execute
17: if @match[1]
18: line, _, _, expr = @match.captures
19: else
20: _, file, line, expr = @match.captures
21: end
22: if expr
23: if expr !~ /^\s*if\s+(.+)/
24: if file or line
25: errmsg "Expecting 'if' in breakpoint condition; got: #{expr}.\n"
26: else
27: errmsg "Invalid breakpoint location: #{expr}.\n"
28: end
29: return
30: else
31: expr = $1
32: end
33: end
34:
35: brkpt_filename = nil
36: if file.nil?
37: unless @state.context
38: errmsg "We are not in a state that has an associated file.\n"
39: return
40: end
41: brkpt_filename = @state.file
42: file = File.basename(@state.file)
43: if line.nil?
44: # Set breakpoint at current line
45: line = @state.line.to_s
46: end
47: elsif line !~ /^\d+$/
48: # See if "line" is a method/function name
49: klass = debug_silent_eval(file)
50: if klass && klass.kind_of?(Module)
51: class_name = klass.name if klass
52: else
53: errmsg "Unknown class #{file}.\n"
54: throw :debug_error
55: end
56: else
57: # FIXME: This should be done in LineCache.
58: file = File.expand_path(file) if file.index(File::SEPARATOR) || \
59: File::ALT_SEPARATOR && file.index(File::ALT_SEPARATOR)
60: brkpt_filename = file
61: end
62:
63: if line =~ /^\d+$/
64: line = line.to_i
65: if LineCache.cache(brkpt_filename, Command.settings[:reload_source_on_change])
66: last_line = LineCache.size(brkpt_filename)
67: if line > last_line
68: errmsg("There are only %d lines in file \"%s\".\n", last_line, file)
69: return
70: end
71: unless LineCache.trace_line_numbers(brkpt_filename).member?(line)
72: errmsg("Line %d is not a stopping point in file \"%s\".\n", line, file)
73: return
74: end
75: else
76: errmsg("No source file named %s\n" % file)
77: return unless confirm("Set breakpoint anyway? (y/n) ")
78: end
79:
80: unless @state.context
81: errmsg "We are not in a state we can add breakpoints.\n"
82: return
83: end
84: brkpt_filename = File.basename(brkpt_filename) if
85: Command.settings[:basename]
86: b = Debugger.add_breakpoint brkpt_filename, line, expr
87: print "Breakpoint %d file %s, line %s\n", b.id, brkpt_filename, line.to_s
88: unless syntax_valid?(expr)
89: errmsg("Expression \"#{expr}\" syntactically incorrect; breakpoint disabled.\n")
90: b.enabled = false
91: end
92: else
93: method = line.intern.id2name
94: b = Debugger.add_breakpoint class_name, method, expr
95: print "Breakpoint %d at %s::%s\n", b.id, class_name, method.to_s
96: end
97: end