Class: DEBUGGER__::UI_ServerBase

Inherits:
UI_Base show all
Defined in:
lib/debug/server.rb

Direct Known Subclasses

UI_TcpServer, UI_UnixDomainServer

Defined Under Namespace

Classes: GreetingError, NoRemoteError, RetryConnection, Terminate

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from UI_Base

#event, #flush, #ignore_output_on_suspend?

Constructor Details

#initializeUI_ServerBase

Returns a new instance of UI_ServerBase.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/debug/server.rb', line 9

def initialize
  @sock = @sock_for_fork = nil
  @accept_m = Mutex.new
  @accept_cv = ConditionVariable.new
  @client_addr = nil
  @q_msg = nil
  @q_ans = nil
  @unsent_messages = []
  @width = 80
  @repl = true
  @session = nil
end

Instance Attribute Details

#reader_threadObject (readonly)

Returns the value of attribute reader_thread.



278
279
280
# File 'lib/debug/server.rb', line 278

def reader_thread
  @reader_thread
end

Instance Method Details

#acceptObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/debug/server.rb', line 31

def accept
  if @sock_for_fork
    begin
      yield @sock_for_fork, already_connected: true
    ensure
      @sock_for_fork.close
      @sock_for_fork = nil
    end
  end
end

#activate(session, on_fork: false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/debug/server.rb', line 42

def activate session, on_fork: false
  @session = session
  @reader_thread = Thread.new do
    # An error on this thread should break the system.
    Thread.current.abort_on_exception = true
    Thread.current.name = 'DEBUGGER__::Server::reader'

    accept do |server, already_connected: false|
      DEBUGGER__.warn "Connected."
      greeting_done = false
      @need_pause_at_first = true

      @accept_m.synchronize{
        @sock = server
        greeting
        greeting_done = true

        @accept_cv.signal

        # flush unsent messages
        @unsent_messages.each{|m|
          @sock.puts m
        } if @repl
        @unsent_messages.clear

        @q_msg = Queue.new
        @q_ans = Queue.new
      } unless already_connected

      setup_interrupt do
        pause if !already_connected && @need_pause_at_first
        process
      end

    rescue GreetingError => e
      DEBUGGER__.warn "GreetingError: #{e.message}"
      next
    rescue Terminate
      raise # should catch at outer scope
    rescue RetryConnection
      next
    rescue => e
      DEBUGGER__.warn "ReaderThreadError: #{e}"
      pp e.backtrace
    ensure
      DEBUGGER__.warn "Disconnected."
      cleanup_reader if greeting_done
    end # accept

  rescue Terminate
    # ignore
  end
end

#after_fork_parentObject



375
376
377
# File 'lib/debug/server.rb', line 375

def after_fork_parent
  # do nothing
end

#ask(prompt) ⇒ Object



311
312
313
314
315
316
# File 'lib/debug/server.rb', line 311

def ask prompt
  sock do |s|
    s.puts "ask #{Process.pid} #{prompt}"
    @q_ans.pop
  end
end


105
106
107
108
109
110
# File 'lib/debug/server.rb', line 105

def check_cookie c
  cookie = CONFIG[:cookie]
  if cookie && cookie != c
    raise GreetingError, "Cookie mismatch (#{$2.inspect} was sent)"
  end
end

#cleanup_readerObject



96
97
98
99
100
101
102
103
# File 'lib/debug/server.rb', line 96

def cleanup_reader
  @sock.close if @sock
  @sock = nil
  @q_msg.close
  @q_msg = nil
  @q_ans.close
  @q_ans = nil
end

#deactivateObject



26
27
28
29
# File 'lib/debug/server.rb', line 26

def deactivate
  @reader_thread.raise Terminate
  @reader_thread.join
end

#greetingObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/debug/server.rb', line 130

def greeting
  case g = @sock.gets
  when /^info cookie:\s+(.*)$/
    require 'etc'

    check_cookie $1
    @sock.puts "PID: #{Process.pid}, $0: #{$0}, session_name: #{CONFIG[:session_name]}"
    @sock.puts "debug #{VERSION} on #{RUBY_DESCRIPTION}"
    @sock.puts "uname: #{Etc.uname.inspect}"
    @sock.close
    raise GreetingError, "HEAD request"

  when /^version:\s+(\S+)\s+(.+)$/
    v, params = $1, $2

    # TODO: protocol version
    if v != VERSION
      @sock.puts msg = "out DEBUGGER: Incompatible version (server:#{VERSION} and client:#{$1})"
      raise GreetingError, msg
    end
    parse_option(params)

    session_name = CONFIG[:session_name]
    session_name_str = ", session_name:#{session_name}" if session_name
    puts "DEBUGGER (client): Connected. PID:#{Process.pid}, $0:#{$0}#{session_name_str}"
    puts "DEBUGGER (client): Type `Ctrl-C` to enter the debug console." unless @need_pause_at_first
    puts

  when /^Content-Length: (\d+)/
    require_relative 'server_dap'

    raise unless @sock.read(2) == "\r\n"
    self.extend(UI_DAP)
    @repl = false
    @need_pause_at_first = false
    dap_setup @sock.read($1.to_i)

  when /^GET\s\/json\sHTTP\/1.1/, /^GET\s\/json\/version\sHTTP\/1.1/, /^GET\s\/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\sHTTP\/1.1/
    # The reason for not using @uuid here is @uuid is nil if users run debugger without `--open=chrome`.

    require_relative 'server_cdp'

    self.extend(UI_CDP)
    send_chrome_response g
  else
    raise GreetingError, "Unknown greeting message: #{g}"
  end
end

#parse_option(params) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/debug/server.rb', line 112

def parse_option params
  case params.strip
  when /width:\s+(\d+)/
    @width = $1.to_i
    parse_option $~.post_match
  when /cookie:\s+(\S+)/
    check_cookie $1 if $1 != '-'
    parse_option $~.post_match
  when /nonstop: (true|false)/
    @need_pause_at_first = false if $1 == 'true'
    parse_option $~.post_match
  when /(.+):(.+)/
    raise GreetingError, "Unkown option: #{params}"
  else
    # OK
  end
end

#pauseObject



363
364
365
366
# File 'lib/debug/server.rb', line 363

def pause
  # $stderr.puts "DEBUG: pause request"
  Process.kill(TRAP_SIGNAL, Process.pid)
end

#processObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/debug/server.rb', line 179

def process
  while true
    DEBUGGER__.debug{ "sleep IO.select" }
    _r = IO.select([@sock])
    DEBUGGER__.debug{ "wakeup IO.select" }

    line = @session.process_group.sync do
      unless IO.select([@sock], nil, nil, 0)
        DEBUGGER__.debug{ "UI_Server can not read" }
        break :can_not_read
      end
      @sock.gets&.chomp.tap{|line|
        DEBUGGER__.debug{ "UI_Server received: #{line}" }
      }
    end

    return unless line
    next if line == :can_not_read

    case line
    when /\Apause/
      pause
    when /\Acommand (\d+) (\d+) ?(.+)/
      raise "not in subsession, but received: #{line.inspect}" unless @session.in_subsession?

      if $1.to_i == Process.pid
        @width = $2.to_i
        @q_msg << $3
      else
        raise "pid:#{Process.pid} but get #{line}"
      end
    when /\Aanswer (\d+) (.*)/
      raise "not in subsession, but received: #{line.inspect}" unless @session.in_subsession?

      if $1.to_i == Process.pid
        @q_ans << $2
      else
        raise "pid:#{Process.pid} but get #{line}"
      end
    else
      STDERR.puts "unsupported: #{line.inspect}"
      exit!
    end
  end
end

#puts(str = nil) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/debug/server.rb', line 318

def puts str = nil
  case str
  when Array
    enum = str.each
  when String
    enum = str.each_line
  when nil
    enum = [''].each
  end

  sock skip: true do |s|
    enum.each do |line|
      msg = "out #{line.chomp}"
      if s
        s.puts msg
      else
        @unsent_messages << msg
      end
    end
  end
end

#quit(n, &_b) ⇒ Object



368
369
370
371
372
373
# File 'lib/debug/server.rb', line 368

def quit n, &_b
  # ignore n
  sock do |s|
    s.puts "quit"
  end
end

#readline(prompt) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/debug/server.rb', line 340

def readline prompt
  input = (sock(skip: CONFIG[:skip_bp]) do |s|
    next unless s

    if @repl
      raise "not in subsession, but received: #{line.inspect}" unless @session.in_subsession?
      line = "input #{Process.pid}"
      DEBUGGER__.debug{ "send: #{line}" }
      s.puts line
    end
    sleep 0.01 until @q_msg
    @q_msg.pop.tap{|msg|
      DEBUGGER__.debug{ "readline: #{msg.inspect}" }
    }
  end || 'continue')

  if input.is_a?(String)
    input.strip
  else
    input
  end
end

#remote?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/debug/server.rb', line 225

def remote?
  true
end

#setup_interruptObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/debug/server.rb', line 257

def setup_interrupt
  prev_handler = trap(TRAP_SIGNAL) do
    # $stderr.puts "trapped SIGINT"
    ThreadClient.current.on_trap TRAP_SIGNAL

    case prev_handler
    when Proc
      prev_handler.call
    else
      # ignore
    end
  end

  if sigurg_overridden?(prev_handler)
    DEBUGGER__.warn "SIGURG handler is overridden by the debugger."
  end
  yield
ensure
  trap(TRAP_SIGNAL, prev_handler)
end

#sigurg_overridden?(prev_handler) ⇒ Boolean

Returns:

  • (Boolean)


233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/debug/server.rb', line 233

def sigurg_overridden? prev_handler
  case prev_handler
  when "SYSTEM_DEFAULT", "DEFAULT"
    false
  when Proc
    if prev_handler.source_location[0] == __FILE__
      false
    else
      true
    end
  else
    true
  end
end

#sock(skip: false) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/debug/server.rb', line 282

def sock skip: false
  if s = @sock         # already connection
    # ok
  elsif skip == true   # skip process
    no_sock = true
    r = @accept_m.synchronize do
      if @sock
        no_sock = false
      else
        yield nil
      end
    end
    return r if no_sock
  else                 # wait for connection
    until s = @sock
      @accept_m.synchronize{
        unless @sock
          DEBUGGER__.warn "wait for debugger connection..."
          @accept_cv.wait(@accept_m)
        end
      }
    end
  end

  yield s
rescue Errno::EPIPE
  # ignore
end

#vscode_setup(debug_port) ⇒ Object



379
380
381
382
# File 'lib/debug/server.rb', line 379

def vscode_setup debug_port
  require_relative 'server_dap'
  UI_DAP.setup debug_port
end

#widthObject



229
230
231
# File 'lib/debug/server.rb', line 229

def width
  @width
end