22require 'stringio'
33require "socket"
44require 'thread'
5- if ( RUBY_VERSION < '2.0' || defined? ( JRUBY_VERSION ) )
5+ if RUBY_VERSION < '2.0' || defined? ( JRUBY_VERSION )
66 require 'ruby-debug-base'
77else
88 require 'debase'
@@ -59,20 +59,20 @@ def interrupt_last
5959 end
6060 end
6161
62- def start_server ( host = nil , port = 1234 )
62+ def start_server ( host = nil , port = 1234 , notify_dispatcher = false )
6363 return if started?
6464 start
65- start_control ( host , port )
65+ start_control ( host , port , notify_dispatcher )
6666 end
6767
6868 def prepare_debugger ( options )
69- start_server ( options . host , options . port )
70-
71- raise "Control thread did not start (#{ @control_thread } }" unless @control_thread && @control_thread . alive?
72-
7369 @mutex = Mutex . new
7470 @proceed = ConditionVariable . new
7571
72+ start_server ( options . host , options . port , options . notify_dispatcher )
73+
74+ raise "Control thread did not start (#{ @control_thread } }" unless @control_thread && @control_thread . alive?
75+
7676 # wait for 'start' command
7777 @mutex . synchronize do
7878 @proceed . wait ( @mutex )
@@ -97,18 +97,20 @@ def run_prog_script
9797 end
9898 end
9999
100- def start_control ( host , port )
100+ def start_control ( host , port , notify_dispatcher )
101101 raise "Debugger is not started" unless started?
102102 return if @control_thread
103103 @control_thread = DebugThread . new do
104104 begin
105105 # 127.0.0.1 seemingly works with all systems and with IPv6 as well.
106106 # "localhost" and nil have problems on some systems.
107107 host ||= '127.0.0.1'
108- gem_name = ( defined? ( JRUBY_VERSION ) || RUBY_VERSION < '1.9.0' ) ? 'ruby-debug-base' :
109- RUBY_VERSION < '2.0.0' ? 'ruby-debug-base19x' : 'debase'
110108 server = TCPServer . new ( host , port )
109+ gem_name = ( defined? ( JRUBY_VERSION ) || RUBY_VERSION < '1.9.0' ) ? 'ruby-debug-base' :
110+ RUBY_VERSION < '2.0.0' ? 'ruby-debug-base19x' : 'debase'
111111 $stderr. printf "Fast Debugger (ruby-debug-ide #{ IDE_VERSION } , #{ gem_name } #{ VERSION } ) listens on #{ host } :#{ port } \n "
112+ notify_dispatcher ( port ) if notify_dispatcher
113+
112114 while ( session = server . accept )
113115 $stderr. puts "Connected from #{ session . peeraddr [ 2 ] } " if Debugger . cli_debug
114116 dispatcher = ENV [ 'IDE_PROCESS_DISPATCHER' ]
@@ -122,7 +124,7 @@ def start_control(host, port)
122124 IdeControlCommandProcessor . new ( interface ) . process_commands
123125 rescue StandardError , ScriptError => ex
124126 bt = ex . backtrace
125- $stderr. printf "#{ Process . pid } : Exception in DebugThread loop: #{ ex . message } \n Backtrace:\n #{ bt ? bt . join ( "\n from: " ) : "<none>" } \n "
127+ $stderr. printf "#{ Process . pid } : Exception in DebugThread loop: #{ ex . message } ( #{ ex . class } ) \n Backtrace:\n #{ bt ? bt . join ( "\n from: " ) : "<none>" } \n "
126128 exit 1
127129 end
128130 end
@@ -134,6 +136,31 @@ def start_control(host, port)
134136 end
135137 end
136138
139+ private
140+
141+ def notify_dispatcher ( port )
142+ return unless ENV [ 'IDE_PROCESS_DISPATCHER' ]
143+ acceptor_host , acceptor_port = ENV [ 'IDE_PROCESS_DISPATCHER' ] . split ( ":" )
144+ acceptor_host , acceptor_port = '127.0.0.1' , acceptor_host unless acceptor_port
145+
146+ connected = false
147+ 3 . times do |i |
148+ begin
149+ s = TCPSocket . open ( acceptor_host , acceptor_port )
150+ s . print ( port )
151+ s . close
152+ connected = true
153+ print_debug "Ide process dispatcher notified about sub-debugger which listens on #{ port } \n "
154+ return
155+ rescue => bt
156+ $stderr. puts "#{ Process . pid } : connection failed(#{ i +1 } )"
157+ $stderr. puts "Exception: #{ bt } "
158+ $stderr. puts bt . backtrace . map { |l | "\t #{ l } " } . join ( "\n " )
159+ sleep 0.3
160+ end unless connected
161+ end
162+ end
163+
137164 end
138165
139166 class Exception # :nodoc:
0 commit comments