2
2
require 'stringio'
3
3
require "socket"
4
4
require 'thread'
5
- if ( RUBY_VERSION < '2.0' || defined? ( JRUBY_VERSION ) )
5
+ if RUBY_VERSION < '2.0' || defined? ( JRUBY_VERSION )
6
6
require 'ruby-debug-base'
7
7
else
8
8
require 'debase'
@@ -59,20 +59,20 @@ def interrupt_last
59
59
end
60
60
end
61
61
62
- def start_server ( host = nil , port = 1234 )
62
+ def start_server ( host = nil , port = 1234 , notify_dispatcher = false )
63
63
return if started?
64
64
start
65
- start_control ( host , port )
65
+ start_control ( host , port , notify_dispatcher )
66
66
end
67
67
68
68
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
-
73
69
@mutex = Mutex . new
74
70
@proceed = ConditionVariable . new
75
71
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
+
76
76
# wait for 'start' command
77
77
@mutex . synchronize do
78
78
@proceed . wait ( @mutex )
@@ -97,18 +97,20 @@ def run_prog_script
97
97
end
98
98
end
99
99
100
- def start_control ( host , port )
100
+ def start_control ( host , port , notify_dispatcher )
101
101
raise "Debugger is not started" unless started?
102
102
return if @control_thread
103
103
@control_thread = DebugThread . new do
104
104
begin
105
105
# 127.0.0.1 seemingly works with all systems and with IPv6 as well.
106
106
# "localhost" and nil have problems on some systems.
107
107
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'
110
108
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'
111
111
$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
+
112
114
while ( session = server . accept )
113
115
$stderr. puts "Connected from #{ session . peeraddr [ 2 ] } " if Debugger . cli_debug
114
116
dispatcher = ENV [ 'IDE_PROCESS_DISPATCHER' ]
@@ -122,7 +124,7 @@ def start_control(host, port)
122
124
IdeControlCommandProcessor . new ( interface ) . process_commands
123
125
rescue StandardError , ScriptError => ex
124
126
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 "
126
128
exit 1
127
129
end
128
130
end
@@ -134,6 +136,31 @@ def start_control(host, port)
134
136
end
135
137
end
136
138
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
+
137
164
end
138
165
139
166
class Exception # :nodoc:
0 commit comments