Skip to content

Commit

Permalink
Attach to Chrome if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
小野 直人 committed Oct 24, 2021
1 parent 124004f commit 98c6d6b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
15 changes: 8 additions & 7 deletions lib/debug/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ module DEBUGGER__
save_history: ['RUBY_DEBUG_SAVE_HISTORY',"BOOT: maximum save history lines (default: 10,000)"],

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

# obsolete
parent_on_fork: ['RUBY_DEBUG_PARENT_ON_FORK', "OBSOLETE: Keep debugging parent process on fork (default: false)", :bool],
Expand Down
6 changes: 1 addition & 5 deletions lib/debug/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ def activate session, on_fork: false
@q_msg = nil
@q_ans.close
@q_ans = nil
if CONFIG[:open_frontend] == 'chrome'
Process.kill(:KILL, @wait_thr.pid)
FileUtils.remove_entry_secure @dir
end
FileUtils.rm_rf @dir if @dir
end # accept

rescue Terminate
Expand Down Expand Up @@ -377,7 +374,6 @@ def accept
end
ensure
@sock_for_fork = nil
Process.kill(:KILL, @wait_thr.pid) if CONFIG[:open_frontend] == 'chrome'
end
end

Expand Down
53 changes: 36 additions & 17 deletions lib/debug/server_cdp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,18 @@ def setup_chrome
require 'open3'
require 'tmpdir'

set_chrome_path
@dir = Dir.mktmpdir
# The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting
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}")
stdin.close
stdout.close

data = stderr.readpartial 4096
if data.match /DevTools listening on ws:\/\/127.0.0.1:(\d+)(.*)/
port = $1
path = $2
if File.exist? p = history_file
port, path = File.read(p).split("\n")
else
if File.exist? "#{@dir}/DevToolsActivePort"
port, path = File.read("#{@dir}/DevToolsActivePort").split("\n")
else
raise "Can't open Chrome browser"
end
port, path = run_new_chrome
end
begin
s = Socket.tcp '127.0.0.1', port
rescue Errno::ECONNREFUSED
port, path = run_new_chrome
retry
end

s = Socket.tcp "127.0.0.1", port
ws_client = WebSocketClient.new(s)
ws_client.handshake port, path
ws_client.send id: 1, method: 'Target.getTargets'
Expand All @@ -59,6 +51,10 @@ def setup_chrome
end
end

def history_file
CONFIG[:chrome_history_file] || File.expand_path('~/.rdbg_chrome_history')
end

def set_chrome_path
# The process to check OS is based on `selenium` project.
case RbConfig::CONFIG['host_os']
Expand All @@ -73,6 +69,29 @@ def set_chrome_path
end
end

def run_new_chrome
set_chrome_path
@dir = Dir.mktmpdir
# The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting
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}")
stdin.close
stdout.close

data = stderr.readpartial 4096
if data.match /DevTools listening on ws:\/\/127.0.0.1:(\d+)(.*)/
port = $1
path = $2
else
if File.exist? "#{@dir}/DevToolsActivePort"
port, path = File.read("#{@dir}/DevToolsActivePort").split("\n")
else
raise "Can't open Chrome browser"
end
end
f = File.open(history_file, "w") { |f| f.print "#{port}\n#{path}" }
[port, path]
end

class WebSocketClient
def initialize s
@sock = s
Expand Down

0 comments on commit 98c6d6b

Please sign in to comment.