Skip to content

Commit 7a15fac

Browse files
equivalence1dmitrii.kravchenko
authored andcommitted
replaced global variables with local ones
1 parent ea4af28 commit 7a15fac

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

bin/gdb_wrapper

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require 'ostruct'
66
$stdout.sync = true
77
$stderr.sync = true
88

9-
$options = OpenStruct.new(
9+
options = OpenStruct.new(
1010
'pid' => nil,
1111
'sdk_path' => nil,
1212
'uid' => nil,
@@ -20,39 +20,38 @@ Some useful banner.
2020
EOB
2121

2222
opts.on('--pid PID', 'pid of process you want to attach to for debugging') do |pid|
23-
$options.pid = pid
23+
options.pid = pid
2424
end
2525

2626
opts.on('--ruby-path RUBY_PATH', 'path to ruby interpreter') do |ruby_path|
27-
$options.ruby_path = ruby_path
27+
options.ruby_path = ruby_path
2828
end
2929

3030
opts.on('--uid UID', 'uid which this process should set after executing gdb attach') do |uid|
31-
$options.uid = uid
31+
options.uid = uid
3232
end
3333

3434
opts.on('--include-gem GEM_LIB_PATH', 'lib of gem to include') do |gem_lib_path|
35-
$options.gems_to_include << gem_lib_path
35+
options.gems_to_include << gem_lib_path
3636
end
3737
end
3838

3939
opts.parse! ARGV
4040

41-
unless $options.pid
41+
unless options.pid
4242
$stderr.puts 'You should specify PID of process you want to attach to'
4343
exit 1
4444
end
4545

46-
unless $options.ruby_path
46+
unless options.ruby_path
4747
$stderr.puts 'You should specify path to the ruby interpreter'
4848
exit 1
4949
end
5050

51-
$argv = '["' + ARGV * '", "' + '"]'
52-
$gems_to_include = '["' + $options.gems_to_include * '", "' + '"]'
53-
$path_to_debugger_loader = File.expand_path(File.dirname(__FILE__)) + '/../lib/ruby-debug-ide/attach/debugger_loader'
51+
argv = '["' + ARGV * '", "' + '"]'
52+
debugger_loader_path = File.expand_path(File.dirname(__FILE__)) + '/../lib/ruby-debug-ide/attach/debugger_loader'
5453

55-
$options.gems_to_include.each do |gem_path|
54+
options.gems_to_include.each do |gem_path|
5655
$LOAD_PATH.unshift(gem_path) unless $LOAD_PATH.include?(gem_path)
5756
end
5857

@@ -65,20 +64,24 @@ class NativeDebugger
6564

6665
# @param executable -- path to ruby interpreter
6766
# @param pid -- pid of process you want to debug
68-
# @param options -- flags you want to specify to your debugger as a string (e.g. "-nx -nh" for gdb to disable .gdbinit)
69-
def initialize(executable, pid, options)
67+
# @param flags -- flags you want to specify to your debugger as a string (e.g. "-nx -nh" for gdb to disable .gdbinit)
68+
def initialize(executable, pid, flags, gems_to_include, debugger_loader_path, argv)
7069
@pid = pid
7170
@delimiter = '__OUTPUT_FINISHED__' # for getting response
7271
@tbreak = '__func_to_set_breakpoint_at'
7372
@main_thread = nil
7473
@process_threads = nil
75-
debase_path = $options.gems_to_include.select {|gem_path| gem_path =~ /debase/}
74+
debase_path = gems_to_include.select {|gem_path| gem_path =~ /debase/}
7675
if debase_path.size == 0
7776
raise 'No debase gem found.'
7877
end
7978
@path_to_attach = debase_path[0] + '/attach.so'
8079

81-
launch_string = "#{self} #{executable} #{options}"
80+
@gems_to_include = '["' + gems_to_include * '", "' + '"]'
81+
@debugger_loader_path = debugger_loader_path
82+
@argv = argv
83+
84+
launch_string = "#{self} #{executable} #{flags}"
8285
@pipe = IO.popen(launch_string, 'r+')
8386
$stdout.puts "executed '#{launch_string}'"
8487
end
@@ -147,7 +150,7 @@ class NativeDebugger
147150
end
148151

149152
def load_debugger
150-
execute "call rb_eval_string_protect(\"require '#{$path_to_debugger_loader}'; load_debugger(#{$gems_to_include.gsub("\"", "'")}, #{$argv.gsub("\"", "'")})\", (int *)0)"
153+
execute "call rb_eval_string_protect(\"require '#{@debugger_loader_path}'; load_debugger(#{@gems_to_include.gsub("\"", "'")}, #{@argv.gsub("\"", "'")})\", (int *)0)"
151154
end
152155

153156
def exit
@@ -163,8 +166,8 @@ end
163166

164167
class LLDB < NativeDebugger
165168

166-
def initialize(executable, pid, options)
167-
super(executable, pid, options)
169+
def initialize(executable, pid, flags, gems_to_include, debugger_loader_path, argv)
170+
super(executable, pid, flags, gems_to_include, debugger_loader_path, argv)
168171
end
169172

170173
def set_flags
@@ -212,8 +215,8 @@ end
212215

213216
class GDB < NativeDebugger
214217

215-
def initialize(executable, pid, options)
216-
super(executable, pid, options)
218+
def initialize(executable, pid, flags, gems_to_include, debugger_loader_path, argv)
219+
super(executable, pid, flags, gems_to_include, debugger_loader_path, argv)
217220
end
218221

219222
def set_flags
@@ -318,11 +321,11 @@ def command_exists(command)
318321
$?.exitstatus == 0
319322
end
320323

321-
def choose_debugger
324+
def choose_debugger(ruby_path, pid, gems_to_include, debugger_loader_path, argv)
322325
if command_exists('gdb')
323-
debugger = GDB.new($options.ruby_path, $options.pid, '-nh -nx')
326+
debugger = GDB.new(ruby_path, pid, '-nh -nx', gems_to_include, debugger_loader_path, argv)
324327
elsif command_exists('lldb')
325-
debugger = LLDB.new($options.ruby_path, $options.pid, '--no-lldbinit')
328+
debugger = LLDB.new(ruby_path, pid, '--no-lldbinit', gems_to_include, debugger_loader_path, argv)
326329
else
327330
raise 'Neither gdb nor lldb was found. Aborting.'
328331
end
@@ -340,12 +343,12 @@ def choose_debugger
340343
debugger
341344
end
342345

343-
debugger = choose_debugger
346+
debugger = choose_debugger(options.ruby_path, options.pid, options.gems_to_include, debugger_loader_path, argv)
344347
debugger.attach_to_process
345348
debugger.set_flags
346349

347-
if $options.uid
348-
Process::Sys.setuid($options.uid.to_i)
350+
if options.uid
351+
Process::Sys.setuid(options.uid.to_i)
349352
end
350353

351354
if debugger.check_already_under_debug

0 commit comments

Comments
 (0)