Skip to content

Commit 096cd72

Browse files
equivalence1dmitrii.kravchenko
authored andcommitted
printing backtrace of all threads in case of fail + some minor chages
1 parent 36d3686 commit 096cd72

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

bin/gdb_wrapper

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Debugger::print_greeting_msg(nil, nil)
6161

6262
class NativeDebugger
6363

64-
attr_reader :pid, :main_thread, :process_threads
64+
attr_reader :pid, :main_thread, :process_threads, :pipe
6565

6666
# @param executable -- path to ruby interpreter
6767
# @param pid -- pid of process you want to debug
@@ -89,9 +89,7 @@ class NativeDebugger
8989
@pipe.close
9090
return ''
9191
end
92-
response = get_response
93-
$stdout.puts "response for #{command}:\n#{response}\n\n\n\n"
94-
response
92+
get_response
9593
end
9694

9795
def get_response
@@ -101,7 +99,6 @@ class NativeDebugger
10199
content = ''
102100
loop do
103101
line = @pipe.readline
104-
$stderr.puts line
105102
next if line =~ /\(lldb\)/ # lldb repeats your input to its output
106103
break if line =~ /\$\d+\s=\s"#{@delimiter}"/
107104
content += line
@@ -152,6 +149,7 @@ class NativeDebugger
152149

153150
def exit
154151
execute 'q'
152+
@pipe.close
155153
end
156154

157155
def to_s
@@ -312,37 +310,52 @@ class ProcessThread
312310

313311
end
314312

315-
def exists_command(command)
313+
def command_exists(command)
316314
`command -v #{command} >/dev/null 2>&1 || { exit 1; }`
317315
$?.exitstatus == 0
318316
end
319317

320318
def choose_debugger
321-
$stderr.puts "exists: #{exists_command('gdb')}, #{exists_command('lldb')}"
322-
if exists_command('gdb')
323-
GDB.new(@options.ruby_path, @options.pid, '-nh -nx')
324-
elsif exists_command('lldb')
325-
LLDB.new(@options.ruby_path, @options.pid, '--no-lldbinit')
319+
if command_exists('gdb')
320+
debugger = GDB.new(@options.ruby_path, @options.pid, '-nh -nx')
321+
elsif command_exists('lldb')
322+
debugger = LLDB.new(@options.ruby_path, @options.pid, '--no-lldbinit')
326323
else
327324
raise 'Neither gdb nor lldb was found. Aborting.'
328325
end
326+
327+
trap('INT') do
328+
unless debugger.pipe.closed?
329+
$stderr.puts "backtraces for threads:\n\n"
330+
debugger.process_threads.each do |thread|
331+
$stderr.puts "#{thread.thread_info}\n#{thread.last_bt}\n\n"
332+
end
333+
end
334+
exit!
335+
end
336+
337+
debugger
329338
end
330339

331-
gdb = choose_debugger
332-
gdb.attach_to_process
333-
gdb.set_flags
340+
debugger = choose_debugger
341+
debugger.attach_to_process
342+
debugger.set_flags
334343

335-
if gdb.check_already_under_debug
336-
$stderr.puts "Process #{gdb.pid} is already under debug"
337-
gdb.exit
344+
if @options.uid
345+
Process::Sys.setuid(@options.uid.to_i)
346+
end
347+
348+
if debugger.check_already_under_debug
349+
$stderr.puts "Process #{debugger.pid} is already under debug"
350+
debugger.exit
338351
exit!
339352
end
340353

341354
should_check_threads_state = true
342355

343356
while should_check_threads_state
344357
should_check_threads_state = false
345-
gdb.update_threads.each do |thread|
358+
debugger.update_threads.each do |thread|
346359
thread.switch
347360
while thread.need_finish_frame
348361
should_check_threads_state = true
@@ -351,19 +364,8 @@ while should_check_threads_state
351364
end
352365
end
353366

354-
gdb.wait_line_event
355-
gdb.load_debugger
356-
gdb.exit
357-
358-
trap('INT') do
359-
gdb.process_threads.each do |thread|
360-
$stderr.puts "Last backtrace for thread #{thread.thread_info}:\n#{thread.last_bt}"
361-
end
362-
exit!
363-
end
364-
365-
if @options.uid
366-
Process::Sys.setuid(@options.uid.to_i)
367-
end
367+
debugger.wait_line_event
368+
debugger.load_debugger
369+
debugger.exit
368370

369371
sleep

0 commit comments

Comments
 (0)