forked from ruby/debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rb
263 lines (225 loc) · 6.15 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# frozen_string_literal: true
require 'socket'
require 'io/console/size'
require_relative 'config'
require_relative 'version'
require_relative 'console'
# $VERBOSE = true
module DEBUGGER__
class CommandLineOptionError < Exception; end
class Client
class << self
def util name
case name
when 'gen-sockpath'
puts DEBUGGER__.create_unix_domain_socket_name
when 'gen-portpath'
port_path = File.join(DEBUGGER__.unix_domain_socket_dir, 'tcp_port')
File.unlink port_path if File.exist?(port_path)
puts port_path
when 'list-socks'
cleanup_unix_domain_sockets
puts list_connections
when 'list-socks-verbose'
cleanup_unix_domain_sockets
puts list_connections verbose: true
when 'setup-autoload'
setup_autoload
else
abort "Unknown utility: #{name}"
end
end
def working_shell_type
shell = `ps -p #{Process.ppid} -o 'args='`
case shell
when /bash/
:bash
when /fish/
:fish
when /csh/
:csh
when /zsh/
:zsh
when /dash/
:dash
else
:unknown
end
end
def setup_autoload
prelude_path = File.join(__dir__, 'prelude.rb')
case shell = working_shell_type
when :bash, :zsh
puts <<~EOS
# add the following lines in your ~/.#{shell}_profile
if test -s #{prelude_path} ; then
export RUBYOPT='-r #{prelude_path}'
fi
# Add `Kernel#bb` method which is alias of `Kernel#debugger`
# export RUBY_DEBUG_BB=1
EOS
when :fish
puts <<~EOS
# add the following lines in your ~/.config/fish/config.fish
set -x RUBYOPT "-r #{__dir__}/prelude" $RUBYOPT
EOS
else
puts "# Sorry that your shell is not supported yet.",
"# Please use the content in #{prelude_path} as a reference and modify your login script accordingly."
end
end
def cleanup_unix_domain_sockets
Dir.glob(DEBUGGER__.create_unix_domain_socket_name_prefix + '*') do |file|
if File.socket?(file) && (/-(\d+)-\d+$/ =~ file || /-(\d+)$/ =~ file)
begin
Process.kill(0, $1.to_i)
rescue Errno::EPERM
rescue Errno::ESRCH
File.unlink(file)
end
end
end
end
def list_connections verbose: false
socks = Dir.glob(DEBUGGER__.create_unix_domain_socket_name_prefix + '*').find_all do |path|
File.socket?(path)
end
if verbose
socks = socks.map{|sock_path|
Socket.unix(sock_path){|sock|
sock.puts "info cookie: #{CONFIG[:cookie] || '-'}"
pid = sock.gets.chomp
_dbg = sock.gets.chomp
_unm = sock.gets.chomp
[sock_path, pid]
}
}
end
socks
end
end
def initialize argv
@multi_process = false
@pid = nil
@console = Console.new
case argv.size
when 0
connect_unix
when 1
if /\A\d+\z/ =~ (arg = argv.shift.strip)
connect_tcp nil, arg.to_i
else
connect_unix arg
end
when 2
connect_tcp argv[0], argv[1]
else
raise CommandLineOptionError
end
@width = IO.console_size[1]
@width = 80 if @width == 0
send "version: #{VERSION} " +
"width: #{@width} " +
"cookie: #{CONFIG[:cookie] || '-'} " +
"nonstop: #{CONFIG[:nonstop] ? 'true' : 'false'}"
end
def deactivate
@console.deactivate if @console
end
def readline
if @multi_process
@console.readline "(rdbg:remote\##{@pid}) "
else
@console.readline "(rdbg:remote) "
end
end
def connect_unix name = nil
if name
if File.exist? name
@s = Socket.unix(name)
else
@s = Socket.unix(File.join(DEBUGGER__.unix_domain_socket_dir, name))
end
else
Client.cleanup_unix_domain_sockets
files = Client.list_connections
case files.size
when 0
$stderr.puts "No debug session is available."
exit
when 1
@s = Socket.unix(files.first)
else
files = Client.list_connections verbose: true
$stderr.puts "Please select a debug session:"
files.each{|(f, desc)|
$stderr.puts " #{File.basename(f)} (#{desc})"
}
exit
end
end
end
def connect_tcp host, port
@s = Socket.tcp(host, port)
end
def send msg
p send: msg if $VERBOSE
@s.puts msg
end
def connect
pre_commands = (CONFIG[:commands] || '').split(';;')
trap(:SIGINT){
send "pause"
}
begin
trap(:SIGWINCH){
@width = IO.console_size[1]
}
rescue ArgumentError
@width = 80
end
while line = @s.gets
p recv: line if $VERBOSE
case line
when /^out (.*)/
puts "#{$1}"
when /^input (.+)/
pid = $1
@multi_process = true if @pid && @pid != pid
@pid = pid
prev_trap = trap(:SIGINT, 'DEFAULT')
begin
if pre_commands.empty?
line = readline
else
line = pre_commands.shift
puts "(rdbg:remote:command) #{line}"
end
rescue Interrupt
retry
ensure
trap(:SIGINT, prev_trap)
end
line = (line || 'quit').strip
send "command #{pid} #{@width} #{line}"
when /^ask (\d+) (.*)/
pid = $1
print $2
send "answer #{pid} #{$stdin.gets || ''}"
when /^quit/
raise 'quit'
else
puts "(unknown) #{line.inspect}"
end
end
rescue => e
STDERR.puts "disconnected (#{e})"
exit
ensure
deactivate
end
end
end
if __FILE__ == $0
DEBUGGER__::Client.new(argv).connect
end