Object
A Debugger::Command object is is the base class for commands that implement a single debugger command. Individual debugger commands will be a subclass of this. The singleton class object is the command manager for all commands.
Each debugger command is expected to implement the following methods:
regexp |
A regular expression which input strings are matched against. If we have a match, run this command. It is the ruby-debug programmer’s responsibility to make sure that these regular expressions match disjoint sets of strings. Otherwise one is arbitrarily used. |
execute |
Ruby code that implements the command. |
help |
Should return a String containing descriptive help for the commmand. Used by the ‘help’ command Debugger::HelpCommand |
help_command |
The name of the command listed via help. |
help and help_command methods are singleton methods, not instance methods like regexp and execute.
An Array containing Debugger::Command classes that implment each of the debugger commands.
# File cli/ruby-debug/command.rb, line 49 def commands @commands ||= [] end
# File cli/ruby-debug/command.rb, line 62 def inherited(klass) DEF_OPTIONS.each do |o, v| klass.options[o] = v if klass.options[o].nil? end commands << klass end
Read in and “include” all the subclasses of the Debugger::Command class. For example Debugger::QuitCommand is one of them. The list of Ruby files to read are all the files that end .rb in directory Debugger::RUBY_DEBUG_DIR
# File cli/ruby-debug/command.rb, line 74 def load_commands Dir[File.join(Debugger.const_get(:RUBY_DEBUG_DIR), 'commands', '*')].each do |file| require file if file =~ /\.rb$/ end Debugger.constants.grep(/Functions$/).map { |name| Debugger.const_get(name) }.each do |mod| include mod end end
# File cli/ruby-debug/command.rb, line 83 def method_missing(meth, *args, &block) if meth.to_s =~ /^(.+?)=$/ @options[$1.intern] = args.first else if @options.has_key?(meth) @options[meth] else super end end end
# File cli/ruby-debug/command.rb, line 176 def initialize(state) @state = state end
# File cli/ruby-debug/command.rb, line 95 def options @options ||= {} end
# File cli/ruby-debug/command.rb, line 145 def register_setting_get(name, &block) settings_map[name] ||= {} settings_map[name][:getter] = block end
# File cli/ruby-debug/command.rb, line 150 def register_setting_set(name, &block) settings_map[name] ||= {} settings_map[name][:setter] = block end
# File cli/ruby-debug/command.rb, line 138 def register_setting_var(name, default) var_name = "@@#{name}" class_variable_set(var_name, default) register_setting_get(name) { class_variable_get(var_name) } register_setting_set(name) { |value| class_variable_set(var_name, value) } end
Returns a Hash of Debugger settings, @settings. If doesn’t exist we create a @settings hash with [] setter and getter and return that.
# File cli/ruby-debug/command.rb, line 106 def settings unless true and defined? @settings and @settings @settings = Object.new map = settings_map c = class << @settings; self end if c.respond_to?(:funcall) c.funcall(:define_method, :[]) do |name| raise "No such setting #{name}" unless map.has_key?(name) map[name][:getter].call end else c.send(:define_method, :[]) do |name| raise "No such setting #{name}" unless map.has_key?(name) map[name][:getter].call end end c = class << @settings; self end if c.respond_to?(:funcall) c.funcall(:define_method, :[]=) do |name, value| raise "No such setting #{name}" unless map.has_key?(name) map[name][:setter].call(value) end else c.send(:define_method, :[]=) do |name, value| raise "No such setting #{name}" unless map.has_key?(name) map[name][:setter].call(value) end end end @settings end
Find param in subcmds. The param id downcased and can be abbreviated to the minimum length listed in the subcommands
# File cli/ruby-debug/command.rb, line 35 def find(subcmds, param) param.downcase! for try_subcmd in subcmds do if (param.size >= try_subcmd.min) and (try_subcmd.name[0..param.size-1] == param) return try_subcmd end end return nil end
Called when we are about to do a dangerous operation. msg contains a prompt message. Return true if confirmed or false if not confirmed.
# File cli/ruby-debug/command.rb, line 198 def confirm(msg) @state.confirm(msg) == 'y' end
debug_eval like Kernel.eval or Object.instance_eval but using the bindings for the debugged program. If there is a syntax-error like exception in running eval, print an appropriate message and throw :debug_error
# File cli/ruby-debug/command.rb, line 206 def debug_eval(str, b = get_binding) begin val = eval(str, b) rescue StandardError, ScriptError => e if Command.settings[:stack_trace_on_error] at = eval("caller(1)", b) print "%s:%s\n", at.shift, e.to_s.sub(/\(eval\):1:(in `.*?':)?/, '') for i in at print "\tfrom %s\n", i end else print "#{e.class} Exception: #{e.message}\n" end throw :debug_error end end
debug_eval like Kernel.eval or Object.instance_eval but using the bindings for the debugged program. If there is a syntax error kind of exception in running eval, no warning is given and nil is returned.
# File cli/ruby-debug/command.rb, line 227 def debug_silent_eval(str) begin eval(str, get_binding) rescue StandardError, ScriptError nil end end
FIXME: use delegate?
# File cli/ruby-debug/command.rb, line 187 def errmsg(*args) @state.errmsg(*args) end
Return a binding object for the debugged program.
# File cli/ruby-debug/command.rb, line 236 def get_binding @state.context.frame_binding(@state.frame_pos) end
# File cli/ruby-debug/command.rb, line 244 def get_context(thnum) Debugger.contexts.find{|c| c.thnum == thnum} end
Generated with the Darkfish Rdoc Generator 2.