Skip to content

Commit

Permalink
let timeout checks for jruby and little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ViugiNick committed Aug 3, 2017
1 parent 67cfa68 commit 30e0b99
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/ruby-debug-ide/xml_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,31 +166,35 @@ def print_string(string)
end

def exec_with_allocation_control(value, memory_limit, time_limit, exec_method, overflow_message_type)
check_memory_limit = true
if (defined?(JRUBY_VERSION) || ENV['DEBUGGER_MEMORY_LIMIT'].to_i <= 0)
return value.send exec_method
check_memory_limit = false
end
curr_thread = Thread.current
result = nil
inspect_thread = DebugThread.start {
start_alloc_size = ObjectSpace.memsize_of_all

start_alloc_size = ObjectSpace.memsize_of_all if (check_memory_limit)
start_time = Time.now.to_f

trace = TracePoint.new(:c_call, :call) do |tp|

if (rand > 0.75)
curr_alloc_size = ObjectSpace.memsize_of_all
curr_time = Time.now.to_f

if ((curr_time - start_time) * 1e3 > time_limit)
curr_thread.raise TimeLimitError.new("Timeout: evaluation of #{exec_method} took longer than #{time_limit}ms.", "#{caller.map {|l| "\t#{l}"}.join("\n")}")
inspect_thread.kill
end

start_alloc_size = curr_alloc_size if (curr_alloc_size < start_alloc_size)
if (check_memory_limit)
curr_alloc_size = ObjectSpace.memsize_of_all
start_alloc_size = curr_alloc_size if (curr_alloc_size < start_alloc_size)

if (curr_alloc_size - start_alloc_size > 1e6 * memory_limit)
curr_thread.raise MemoryLimitError.new("Out of memory: evaluation of #{exec_method} took more than #{memory_limit}mb.", "#{caller.map {|l| "\t#{l}"}.join("\n")}")
inspect_thread.kill
if (curr_alloc_size - start_alloc_size > 1e6 * memory_limit)
curr_thread.raise MemoryLimitError.new("Out of memory: evaluation of #{exec_method} took more than #{memory_limit}mb.", "#{caller.map {|l| "\t#{l}"}.join("\n")}")
inspect_thread.kill
end
end
end
end.enable {
Expand Down Expand Up @@ -309,7 +313,7 @@ def print_catchpoint_deleted(exception_class_name)
def print_expressions(exps)
print_element "expressions" do
exps.each_with_index do |(exp, value), idx|
print_expression(exp, value, idx+1)
print_expression(exp, value, idx + 1)
end
end unless exps.empty?
end
Expand All @@ -335,11 +339,11 @@ def print_list(b, e, file, line)
print "[%d, %d] in %s\n", b, e, file
if (lines = Debugger.source_for(file))
b.upto(e) do |n|
if n > 0 && lines[n-1]
if n > 0 && lines[n - 1]
if n == line
print "=> %d %s\n", n, lines[n-1].chomp
print "=> %d %s\n", n, lines[n - 1].chomp
else
print " %d %s\n", n, lines[n-1].chomp
print " %d %s\n", n, lines[n - 1].chomp
end
end
end
Expand Down Expand Up @@ -397,7 +401,7 @@ def print_inspect(eval_result)
end
end

def print_load_result(file, exception=nil)
def print_load_result(file, exception = nil)
if exception
print("<loadResult file=\"%s\" exceptionType=\"%s\" exceptionMessage=\"%s\"/>", file, exception.class, CGI.escapeHTML(exception.to_s))
else
Expand Down Expand Up @@ -456,7 +460,7 @@ def compact_array_str(value)
compact = exec_with_allocation_control(slice, ENV['DEBUGGER_MEMORY_LIMIT'].to_i, ENV['INSPECT_TIME_LIMIT'].to_i, :inspect, OverflowMessageType::NIL_MESSAGE)

if compact && value.size != slice.size
compact[0..compact.size-2] + ", ...]"
compact[0..compact.size - 2] + ", ...]"
end
compact
end
Expand Down

0 comments on commit 30e0b99

Please sign in to comment.