Skip to content

Commit 7bf2944

Browse files
author
小野 直人
committed
Attach to Chrome if it exists
1 parent 124004f commit 7bf2944

3 files changed

Lines changed: 45 additions & 30 deletions

File tree

lib/debug/config.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ module DEBUGGER__
3737
save_history: ['RUBY_DEBUG_SAVE_HISTORY',"BOOT: maximum save history lines (default: 10,000)"],
3838

3939
# remote setting
40-
port: ['RUBY_DEBUG_PORT', "REMOTE: TCP/IP remote debugging: port"],
41-
host: ['RUBY_DEBUG_HOST', "REMOTE: TCP/IP remote debugging: host (localhost if not given)"],
42-
sock_path: ['RUBY_DEBUG_SOCK_PATH', "REMOTE: UNIX Domain Socket remote debugging: socket path"],
43-
sock_dir: ['RUBY_DEBUG_SOCK_DIR', "REMOTE: UNIX Domain Socket remote debugging: socket directory"],
44-
cookie: ['RUBY_DEBUG_COOKIE', "REMOTE: Cookie for negotiation"],
45-
open_frontend: ['RUBY_DEBUG_OPEN_FRONTEND',"REMOTE: frontend used by open command (vscode, chrome, default: rdbg)."],
46-
chrome_path: ['RUBY_DEBUG_CHROME_PATH', "REMOTE: Current path in Google Chrome"],
40+
port: ['RUBY_DEBUG_PORT', "REMOTE: TCP/IP remote debugging: port"],
41+
host: ['RUBY_DEBUG_HOST', "REMOTE: TCP/IP remote debugging: host (localhost if not given)"],
42+
sock_path: ['RUBY_DEBUG_SOCK_PATH', "REMOTE: UNIX Domain Socket remote debugging: socket path"],
43+
sock_dir: ['RUBY_DEBUG_SOCK_DIR', "REMOTE: UNIX Domain Socket remote debugging: socket directory"],
44+
cookie: ['RUBY_DEBUG_COOKIE', "REMOTE: Cookie for negotiation"],
45+
open_frontend: ['RUBY_DEBUG_OPEN_FRONTEND',"REMOTE: frontend used by open command (vscode, chrome, default: rdbg)."],
46+
chrome_path: ['RUBY_DEBUG_CHROME_PATH', "REMOTE: Current path in Google Chrome"],
47+
chrome_history_file: ['RUBY_DEBUG_CHROME_HISTORY_FILE', "REMOTE: history file of Chrome setup (default: ~/.rdbg_chrome_history)"],
4748

4849
# obsolete
4950
parent_on_fork: ['RUBY_DEBUG_PARENT_ON_FORK', "OBSOLETE: Keep debugging parent process on fork (default: false)", :bool],

lib/debug/server.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ def activate session, on_fork: false
8181
@q_msg = nil
8282
@q_ans.close
8383
@q_ans = nil
84-
if CONFIG[:open_frontend] == 'chrome'
85-
Process.kill(:KILL, @wait_thr.pid)
86-
FileUtils.remove_entry_secure @dir
87-
end
84+
FileUtils.rm_rf @dir if @dir
8885
end # accept
8986

9087
rescue Terminate
@@ -298,7 +295,6 @@ def readline prompt
298295

299296
def pause
300297
# $stderr.puts "DEBUG: pause request"
301-
Process.kill(:SIGURG, Process.pid)
302298
end
303299

304300
def quit n
@@ -377,7 +373,6 @@ def accept
377373
end
378374
ensure
379375
@sock_for_fork = nil
380-
Process.kill(:KILL, @wait_thr.pid) if CONFIG[:open_frontend] == 'chrome'
381376
end
382377
end
383378

lib/debug/server_cdp.rb

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,18 @@ def setup_chrome
1313
require 'open3'
1414
require 'tmpdir'
1515

16-
set_chrome_path
17-
@dir = Dir.mktmpdir
18-
# The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting
19-
stdin, stdout, stderr, @wait_thr = *Open3.popen3("#{CONFIG[:chrome_path]} --remote-debugging-port=0 --no-first-run --no-default-browser-check --user-data-dir=#{@dir}")
20-
stdin.close
21-
stdout.close
22-
23-
data = stderr.readpartial 4096
24-
if data.match /DevTools listening on ws:\/\/127.0.0.1:(\d+)(.*)/
25-
port = $1
26-
path = $2
16+
if File.exist? p = history_file
17+
port, path = File.read(p).split("\n")
2718
else
28-
if File.exist? "#{@dir}/DevToolsActivePort"
29-
port, path = File.read("#{@dir}/DevToolsActivePort").split("\n")
30-
else
31-
raise "Can't open Chrome browser"
32-
end
19+
port, path = run_new_chrome
20+
end
21+
begin
22+
s = Socket.tcp "127.0.0.1", port
23+
rescue Errno::ECONNREFUSED
24+
port, path = run_new_chrome
25+
retry
3326
end
3427

35-
s = Socket.tcp "127.0.0.1", port
3628
ws_client = WebSocketClient.new(s)
3729
ws_client.handshake port, path
3830
ws_client.send id: 1, method: 'Target.getTargets'
@@ -59,6 +51,10 @@ def setup_chrome
5951
end
6052
end
6153

54+
def history_file
55+
CONFIG[:chrome_history_file] || File.expand_path('~/.rdbg_chrome_history')
56+
end
57+
6258
def set_chrome_path
6359
# The process to check OS is based on `selenium` project.
6460
case RbConfig::CONFIG['host_os']
@@ -73,6 +69,29 @@ def set_chrome_path
7369
end
7470
end
7571

72+
def run_new_chrome
73+
set_chrome_path
74+
@dir = Dir.mktmpdir
75+
# The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting
76+
stdin, stdout, stderr, @wait_thr = *Open3.popen3("#{CONFIG[:chrome_path]} --remote-debugging-port=0 --no-first-run --no-default-browser-check --user-data-dir=#{@dir}")
77+
stdin.close
78+
stdout.close
79+
80+
data = stderr.readpartial 4096
81+
if data.match /DevTools listening on ws:\/\/127.0.0.1:(\d+)(.*)/
82+
port = $1
83+
path = $2
84+
else
85+
if File.exist? "#{@dir}/DevToolsActivePort"
86+
port, path = File.read("#{@dir}/DevToolsActivePort").split("\n")
87+
else
88+
raise "Can't open Chrome browser"
89+
end
90+
end
91+
f = File.open(history_file, "w") { |f| f.print "#{port}\n#{path}" }
92+
[port, path]
93+
end
94+
7695
class WebSocketClient
7796
def initialize s
7897
@sock = s

0 commit comments

Comments
 (0)