|
| 1 | +require 'pp' |
| 2 | +require 'stringio' |
| 3 | +require 'socket' |
| 4 | +require 'thread' |
| 5 | +require 'ruby-debug-base' |
| 6 | +require_relative 'ruby-debug/processor' |
| 7 | + |
| 8 | +module Debugger |
| 9 | + self.handler = CommandProcessor.new |
| 10 | + |
| 11 | + # the port number used for remote debugging |
| 12 | + PORT = 8989 unless defined?(PORT) |
| 13 | + |
| 14 | + # What file is used for debugger startup commands. |
| 15 | + unless defined?(INITFILE) |
| 16 | + if RUBY_PLATFORM =~ /mswin/ |
| 17 | + # Of course MS Windows has to be different |
| 18 | + INITFILE = 'rdebug.ini' |
| 19 | + HOME_DIR = (ENV['HOME'] || |
| 20 | + ENV['HOMEDRIVE'].to_s + ENV['HOMEPATH'].to_s).to_s |
| 21 | + else |
| 22 | + INITFILE = '.rdebugrc' |
| 23 | + HOME_DIR = ENV['HOME'].to_s |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + class << self |
| 28 | + # gdb-style annotation mode. Used in GNU Emacs interface |
| 29 | + attr_accessor :annotate |
| 30 | + |
| 31 | + # in remote mode, wait for the remote connection |
| 32 | + attr_accessor :wait_connection |
| 33 | + |
| 34 | + # If set, a string to look for in caller() and is used to see |
| 35 | + # if the call stack is truncated. |
| 36 | + attr_accessor :start_sentinal |
| 37 | + |
| 38 | + attr_reader :thread, :control_thread |
| 39 | + |
| 40 | + def interface=(value) # :nodoc: |
| 41 | + handler.interface = value |
| 42 | + end |
| 43 | + |
| 44 | + # |
| 45 | + # Starts a remote debugger. |
| 46 | + # |
| 47 | + def start_remote(host = nil, port = PORT) |
| 48 | + return if @thread |
| 49 | + |
| 50 | + self.interface = nil |
| 51 | + start |
| 52 | + |
| 53 | + if port.kind_of?(Array) |
| 54 | + cmd_port, ctrl_port = port |
| 55 | + else |
| 56 | + cmd_port, ctrl_port = port, port + 1 |
| 57 | + end |
| 58 | + |
| 59 | + start_control(host, ctrl_port) |
| 60 | + |
| 61 | + yield if block_given? |
| 62 | + |
| 63 | + mutex = Mutex.new |
| 64 | + proceed = ConditionVariable.new |
| 65 | + |
| 66 | + @thread = DebugThread.new do |
| 67 | + server = TCPServer.new(host, cmd_port) |
| 68 | + while (session = server.accept) |
| 69 | + self.interface = RemoteInterface.new(session) |
| 70 | + if wait_connection |
| 71 | + mutex.synchronize do |
| 72 | + proceed.signal |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + if wait_connection |
| 78 | + mutex.synchronize do |
| 79 | + proceed.wait(mutex) |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + alias start_server start_remote |
| 84 | + |
| 85 | + def start_control(host = nil, ctrl_port = PORT + 1) # :nodoc: |
| 86 | + return if defined?(@control_thread) && @control_thread |
| 87 | + @control_thread = DebugThread.new do |
| 88 | + server = TCPServer.new(host, ctrl_port) |
| 89 | + while (session = server.accept) |
| 90 | + interface = RemoteInterface.new(session) |
| 91 | + processor = ControlCommandProcessor.new(interface) |
| 92 | + processor.process_commands |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + # |
| 98 | + # Connects to the remote debugger |
| 99 | + # |
| 100 | + def start_client(host = 'localhost', port = PORT) |
| 101 | + require "socket" |
| 102 | + interface = Debugger::LocalInterface.new |
| 103 | + socket = TCPSocket.new(host, port) |
| 104 | + puts "Connected." |
| 105 | + |
| 106 | + catch(:exit) do |
| 107 | + while (line = socket.gets) |
| 108 | + case line |
| 109 | + when /^PROMPT (.*)$/ |
| 110 | + input = interface.read_command($1) |
| 111 | + throw :exit unless input |
| 112 | + socket.puts input |
| 113 | + when /^CONFIRM (.*)$/ |
| 114 | + input = interface.confirm($1) |
| 115 | + throw :exit unless input |
| 116 | + socket.puts input |
| 117 | + else |
| 118 | + print line |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | + socket.close |
| 123 | + end |
| 124 | + |
| 125 | + # Runs normal debugger initialization scripts |
| 126 | + # Reads and executes the commands from init file (if any) in the |
| 127 | + # current working directory. This is only done if the current |
| 128 | + # directory is different from your home directory. Thus, you can |
| 129 | + # have more than one init file, one generic in your home directory, |
| 130 | + # and another, specific to the program you are debugging, in the |
| 131 | + # directory where you invoke ruby-debug. |
| 132 | + def run_init_script(out = handler.interface) |
| 133 | + cwd_script_file = File.expand_path(File.join(".", INITFILE)) |
| 134 | + run_script(cwd_script_file, out) if File.exists?(cwd_script_file) |
| 135 | + |
| 136 | + home_script_file = File.expand_path(File.join(HOME_DIR, INITFILE)) |
| 137 | + run_script(home_script_file, out) if File.exists?(home_script_file) and |
| 138 | + cwd_script_file != home_script_file |
| 139 | + end |
| 140 | + |
| 141 | + # |
| 142 | + # Runs a script file |
| 143 | + # |
| 144 | + def run_script(file, out = handler.interface, verbose=false) |
| 145 | + interface = ScriptInterface.new(File.expand_path(file), out) |
| 146 | + processor = ControlCommandProcessor.new(interface) |
| 147 | + processor.process_commands(verbose) |
| 148 | + end |
| 149 | + end |
| 150 | +end |
| 151 | + |
| 152 | +module Kernel |
| 153 | + |
| 154 | + # Enters the debugger in the current thread after _steps_ line events occur. |
| 155 | + # Before entering the debugger startup script is read. |
| 156 | + # |
| 157 | + # Setting _steps_ to 0 will cause a break in the debugger subroutine |
| 158 | + # and not wait for a line event to occur. You will have to go "up 1" |
| 159 | + # in order to be back in your debugged program rather than the |
| 160 | + # debugger. Settings _steps_ to 0 could be useful you want to stop |
| 161 | + # right after the last statement in some scope, because the next |
| 162 | + # step will take you out of some scope. |
| 163 | + def debugger(steps = 1) |
| 164 | + Debugger.start |
| 165 | + Debugger.run_init_script(StringIO.new) |
| 166 | + if 0 == steps |
| 167 | + Debugger.current_context.stop_frame = 0 |
| 168 | + else |
| 169 | + Debugger.current_context.stop_next = steps |
| 170 | + end |
| 171 | + end |
| 172 | + alias breakpoint debugger unless respond_to?(:breakpoint) |
| 173 | +end |
0 commit comments