Module: DEBUGGER__::UI_DAP

Defined in:
lib/debug/server_dap.rb

Defined Under Namespace

Classes: RetryBecauseCantRead

Constant Summary collapse

SHOW_PROTOCOL =
ENV['DEBUG_DAP_SHOW_PROTOCOL'] == '1' || ENV['RUBY_DEBUG_DAP_SHOW_PROTOCOL'] == '1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.local_fs_map_set(map) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/debug/server_dap.rb', line 112

def self.local_fs_map_set map
  return if @local_fs_map # already setup

  case map
  when String
    @local_fs_map = map.split(',').map{|e| e.split(':').map{|path| path.delete_suffix('/') + '/'}}
  when true
    @local_fs_map = map
  when nil
    @local_fs_map = CONFIG[:local_fs_map]
  end
end

.local_to_remote_path(path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/debug/server_dap.rb', line 95

def self.local_to_remote_path path
  case @local_fs_map
  when nil
    nil
  when true
    path
  else # Array
    @local_fs_map.each do |(remote_path_prefix, local_path_prefix)|
      if path.start_with? local_path_prefix
        return path.sub(local_path_prefix){ remote_path_prefix }
      end
    end

    nil
  end
end

.remote_to_local_path(path) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/debug/server_dap.rb', line 78

def self.remote_to_local_path path
  case @local_fs_map
  when nil
    nil
  when true
    path
  else # Array
    @local_fs_map.each do |(remote_path_prefix, local_path_prefix)|
      if path.start_with? remote_path_prefix
        return path.sub(remote_path_prefix){ local_path_prefix }
      end
    end

    nil
  end
end

.setup(debug_port) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/debug/server_dap.rb', line 12

def self.setup debug_port
  if File.directory? '.vscode'
    dir = Dir.pwd
  else
    dir = Dir.mktmpdir("ruby-debug-vscode-")
    tempdir = true
  end

  at_exit do
    DEBUGGER__.skip_all
    FileUtils.rm_rf dir if tempdir
  end

  key = rand.to_s

  Dir.chdir(dir) do
    Dir.mkdir('.vscode') if tempdir

    # vscode-rdbg 0.0.9 or later is needed
    open('.vscode/rdbg_autoattach.json', 'w') do |f|
      f.puts JSON.pretty_generate({
        type: "rdbg",
        name: "Attach with rdbg",
        request: "attach",
        rdbgPath: File.expand_path('../../exe/rdbg', __dir__),
        debugPort: debug_port,
        localfs: true,
        autoAttach: key,
      })
    end
  end

  cmds = ['code', "#{dir}/"]
  cmdline = cmds.join(' ')
  ssh_cmdline = "code --remote ssh-remote+[SSH hostname] #{dir}/"

  STDERR.puts "Launching: #{cmdline}"
  env = ENV.delete_if{|k, h| /RUBY/ =~ k}.to_h
  env['RUBY_DEBUG_AUTOATTACH'] = key

  unless system(env, *cmds)
    DEBUGGER__.warn <<~MESSAGE
    Can not invoke the command.
    Use the command-line on your terminal (with modification if you need).

      #{cmdline}

    If your application is running on a SSH remote host, please try:

      #{ssh_cmdline}

    MESSAGE
  end
end

Instance Method Details

#dap_setup(bytes) ⇒ Object



125
126
127
128
129
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
178
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
# File 'lib/debug/server_dap.rb', line 125

def dap_setup bytes
  CONFIG.set_config no_color: true
  @seq = 0
  @send_lock = Mutex.new

  case self
  when UI_UnixDomainServer
    # If the user specified a mapping, respect it, otherwise, make sure that no mapping is used
    UI_DAP.local_fs_map_set CONFIG[:local_fs_map] || true
  when UI_TcpServer
    # TODO: loopback address can be used to connect other FS env, like Docker containers
    # UI_DAP.local_fs_set if @local_addr.ipv4_loopback? || @local_addr.ipv6_loopback?
  end

  show_protocol :>, bytes
  req = JSON.load(bytes)

  # capability
  send_response(req,
         ## Supported
         supportsConfigurationDoneRequest: true,
         supportsFunctionBreakpoints: true,
         supportsConditionalBreakpoints: true,
         supportTerminateDebuggee: true,
         supportsTerminateRequest: true,
         exceptionBreakpointFilters: [
           {
             filter: 'any',
             label: 'rescue any exception',
             supportsCondition: true,
             #conditionDescription: '',
           },
           {
             filter: 'RuntimeError',
             label: 'rescue RuntimeError',
             supportsCondition: true,
             #conditionDescription: '',
           },
         ],
         supportsExceptionFilterOptions: true,
         supportsStepBack: true,
         supportsEvaluateForHovers: true,
         supportsCompletionsRequest: true,

         ## Will be supported
         # supportsExceptionOptions: true,
         # supportsHitConditionalBreakpoints:
         # supportsSetVariable: true,
         # supportSuspendDebuggee:
         # supportsLogPoints:
         # supportsLoadedSourcesRequest:
         # supportsDataBreakpoints:
         # supportsBreakpointLocationsRequest:

         ## Possible?
         # supportsRestartFrame:
         # completionTriggerCharacters:
         # supportsModulesRequest:
         # additionalModuleColumns:
         # supportedChecksumAlgorithms:
         # supportsRestartRequest:
         # supportsValueFormattingOptions:
         # supportsExceptionInfoRequest:
         # supportsDelayedStackTraceLoading:
         # supportsTerminateThreadsRequest:
         # supportsSetExpression:
         # supportsClipboardContext:

         ## Never
         # supportsGotoTargetsRequest:
         # supportsStepInTargetsRequest:
         # supportsReadMemoryRequest:
         # supportsDisassembleRequest:
         # supportsCancelRequest:
         # supportsSteppingGranularity:
         # supportsInstructionBreakpoints:
  )
  send_event 'initialized'
  puts <<~WELCOME
    Ruby REPL: You can run any Ruby expression here.
    Note that output to the STDOUT/ERR printed on the TERMINAL.
    [experimental]
      `,COMMAND` runs `COMMAND` debug command (ex: `,info`).
      `,help` to list all debug commands.
  WELCOME
end

#event(type, *args) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/debug/server_dap.rb', line 513

def event type, *args
  case type
  when :load
    file_path, reloaded = *args

    if file_path
      send_event 'loadedSource',
                 reason: (reloaded ? :changed : :new),
                 source: {
                   path: file_path,
                 }
    end
  when :suspend_bp
    _i, bp, tid = *args
    if bp.kind_of?(CatchBreakpoint)
      reason = 'exception'
      text = bp.description
    else
      reason = 'breakpoint'
      text = bp ? bp.description : 'temporary bp'
    end

    send_event 'stopped', reason: reason,
                          description: text,
                          text: text,
                          threadId: tid,
                          allThreadsStopped: true
  when :suspend_trap
    _sig, tid = *args
    send_event 'stopped', reason: 'pause',
                          threadId: tid,
                          allThreadsStopped: true
  when :suspended
    tid, = *args
    send_event 'stopped', reason: 'step',
                          threadId: tid,
                          allThreadsStopped: true
  end
end

#ignore_output_on_suspend?Boolean

Returns:

  • (Boolean)


509
510
511
# File 'lib/debug/server_dap.rb', line 509

def ignore_output_on_suspend?
  true
end

#load_extensions(req) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/debug/server_dap.rb', line 277

def load_extensions req
  if exts = req.dig('arguments', 'rdbgExtensions')
    exts.each{|ext|
      require_relative "dap_custom/#{File.basename(ext)}"
    }
  end

  if scripts = req.dig('arguments', 'rdbgInitialScripts')
    scripts.each do |script|
      begin
        eval(script)
      rescue Exception => e
        puts e.message
        puts e.backtrace.inspect
      end
    end
  end
end

#processObject



296
297
298
299
300
301
302
# File 'lib/debug/server_dap.rb', line 296

def process
  while req = recv_request
    process_request(req)
  end
ensure
  send_event :terminated unless @sock.closed?
end

#process_request(req) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/debug/server_dap.rb', line 304

def process_request req
  raise "not a request: #{req.inspect}" unless req['type'] == 'request'
  args = req.dig('arguments')

  case req['command']

  ## boot/configuration
  when 'launch'
    send_response req
    # `launch` runs on debuggee on the same file system
    UI_DAP.local_fs_map_set req.dig('arguments', 'localfs') || req.dig('arguments', 'localfsMap') || true
    @nonstop = true

    load_extensions req

  when 'attach'
    send_response req
    UI_DAP.local_fs_map_set req.dig('arguments', 'localfs') || req.dig('arguments', 'localfsMap')

    if req.dig('arguments', 'nonstop') == true
      @nonstop = true
    else
      @nonstop = false
    end

    load_extensions req

  when 'configurationDone'
    send_response req

    if @nonstop
      @q_msg << 'continue'
    else
      if SESSION.in_subsession?
        send_event 'stopped', reason: 'pause',
                              threadId: 1, # maybe ...
                              allThreadsStopped: true
      end
    end

  when 'setBreakpoints'
    req_path = args.dig('source', 'path')
    path = UI_DAP.local_to_remote_path(req_path)
    if path
      SESSION.clear_line_breakpoints path

      bps = []
      args['breakpoints'].each{|bp|
        line = bp['line']
        if cond = bp['condition']
          bps << SESSION.add_line_breakpoint(path, line, cond: cond)
        else
          bps << SESSION.add_line_breakpoint(path, line)
        end
      }
      send_response req, breakpoints: (bps.map do |bp| {verified: true,} end)
    else
      send_response req, success: false, message: "#{req_path} is not available"
    end

  when 'setFunctionBreakpoints'
    send_response req

  when 'setExceptionBreakpoints'
    process_filter = ->(filter_id, cond = nil) {
      bp =
        case filter_id
        when 'any'
          SESSION.add_catch_breakpoint 'Exception', cond: cond
        when 'RuntimeError'
          SESSION.add_catch_breakpoint 'RuntimeError', cond: cond
        else
          nil
        end
        {
          verified: !bp.nil?,
          message: bp.inspect,
        }
      }

      SESSION.clear_catch_breakpoints 'Exception', 'RuntimeError'

      filters = args.fetch('filters').map {|filter_id|
        process_filter.call(filter_id)
      }

      filters += args.fetch('filterOptions', {}).map{|bp_info|
      process_filter.call(bp_info['filterId'], bp_info['condition'])
    }

    send_response req, breakpoints: filters

  when 'disconnect'
    terminate = args.fetch("terminateDebuggee", false)

    SESSION.clear_all_breakpoints
    send_response req

    if SESSION.in_subsession?
      if terminate
        @q_msg << 'kill!'
      else
        @q_msg << 'continue'
      end
    else
      if terminate
        @q_msg << 'kill!'
        pause
      end
    end

  ## control
  when 'continue'
    @q_msg << 'c'
    send_response req, allThreadsContinued: true
  when 'next'
    begin
      @session.check_postmortem
      @q_msg << 'n'
      send_response req
    rescue PostmortemError
      send_response req,
                    success: false, message: 'postmortem mode',
                    result: "'Next' is not supported while postmortem mode"
    end
  when 'stepIn'
    begin
      @session.check_postmortem
      @q_msg << 's'
      send_response req
    rescue PostmortemError
      send_response req,
                    success: false, message: 'postmortem mode',
                    result: "'stepIn' is not supported while postmortem mode"
    end
  when 'stepOut'
    begin
      @session.check_postmortem
      @q_msg << 'fin'
      send_response req
    rescue PostmortemError
      send_response req,
                    success: false, message: 'postmortem mode',
                    result: "'stepOut' is not supported while postmortem mode"
    end
  when 'terminate'
    send_response req
    exit
  when 'pause'
    send_response req
    Process.kill(UI_ServerBase::TRAP_SIGNAL, Process.pid)
  when 'reverseContinue'
    send_response req,
                  success: false, message: 'cancelled',
                  result: "Reverse Continue is not supported. Only \"Step back\" is supported."
  when 'stepBack'
    @q_msg << req

  ## query
  when 'threads'
    send_response req, threads: SESSION.managed_thread_clients.map{|tc|
      { id: tc.id,
        name: tc.name,
      }
    }

  when 'evaluate'
    expr = req.dig('arguments', 'expression')
    if /\A\s*,(.+)\z/ =~ expr
      dbg_expr = $1.strip
      dbg_expr.split(';;') { |cmd| @q_msg << cmd }

      send_response req,
                    result: "(rdbg:command) #{dbg_expr}",
                    variablesReference: 0
    else
      @q_msg << req
    end
  when 'stackTrace',
       'scopes',
       'variables',
       'source',
       'completions'
    @q_msg << req

  else
    if respond_to? mid = "custom_dap_request_#{req['command']}"
      __send__ mid, req
    else
      raise "Unknown request: #{req.inspect}"
    end
  end
end

#puts(result) ⇒ Object



504
505
506
507
# File 'lib/debug/server_dap.rb', line 504

def puts result
  # STDERR.puts "puts: #{result}"
  send_event 'output', category: 'console', output: "#{result&.chomp}\n"
end

#recv_requestObject



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

def recv_request
  IO.select([@sock])

  @session.process_group.sync do
    raise RetryBecauseCantRead unless IO.select([@sock], nil, nil, 0)

    case @sock.gets
    when /Content-Length: (\d+)/
      b = @sock.read(2)
      raise b.inspect unless b == "\r\n"

      l = @sock.read($1.to_i)
      show_protocol :>, l
      JSON.load(l)
    when nil
      nil
    else
      raise "unrecognized line: #{l} (#{l.size} bytes)"
    end
  end
rescue RetryBecauseCantRead
  retry
end

#respond(req, res) ⇒ Object

called by the SESSION thread



500
501
502
# File 'lib/debug/server_dap.rb', line 500

def respond req, res
  send_response(req, **res)
end

#send(**kw) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/debug/server_dap.rb', line 212

def send **kw
  if sock = @sock
    kw[:seq] = @seq += 1
    str = JSON.dump(kw)
    @send_lock.synchronize do
      sock.write "Content-Length: #{str.bytesize}\r\n\r\n#{str}"
    end
    show_protocol '<', str
  end
rescue Errno::EPIPE => e
  $stderr.puts "#{e.inspect} rescued during sending message"
end

#send_event(name, **kw) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/debug/server_dap.rb', line 242

def send_event name, **kw
  if kw.empty?
    send type: 'event', event: name
  else
    send type: 'event', event: name, body: kw
  end
end

#send_response(req, success: true, message: nil, **kw) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/debug/server_dap.rb', line 225

def send_response req, success: true, message: nil, **kw
  if kw.empty?
    send type: 'response',
         command: req['command'],
         request_seq: req['seq'],
         success: success,
         message: message || (success ? 'Success' : 'Failed')
  else
    send type: 'response',
         command: req['command'],
         request_seq: req['seq'],
         success: success,
         message: message || (success ? 'Success' : 'Failed'),
         body: kw
  end
end

#show_protocol(dir, msg) ⇒ Object



67
68
69
70
71
# File 'lib/debug/server_dap.rb', line 67

def show_protocol dir, msg
  if SHOW_PROTOCOL
    $stderr.puts "\##{Process.pid}:[#{dir}] #{msg}"
  end
end