@@ -13,25 +13,21 @@ module OverflowMessageType
13
13
SPECIAL_SYMBOL_MESSAGE = lambda { |e | '<?>' }
14
14
end
15
15
16
- class MemoryLimitError < StandardError
16
+ class ExecError < StandardError
17
17
attr_reader :message
18
+ attr_reader :trace_point
18
19
attr_reader :backtrace
19
20
20
- def initialize ( message , backtrace = [ ] )
21
+ def initialize ( message , trace_point , backtrace = [ ] )
21
22
@message = message
23
+ @trace_point = trace_point
22
24
@backtrace = backtrace
23
25
end
24
26
end
25
27
26
- class TimeLimitError < StandardError
27
- attr_reader :message
28
- attr_reader :backtrace
28
+ class MemoryLimitError < ExecError ; end
29
29
30
- def initialize ( message , backtrace = [ ] )
31
- @message = message
32
- @backtrace = backtrace
33
- end
34
- end
30
+ class TimeLimitError < ExecError ; end
35
31
36
32
class XmlPrinter # :nodoc:
37
33
class ExceptionProxy
@@ -170,47 +166,45 @@ def print_string(string)
170
166
def exec_with_allocation_control ( value , memory_limit , time_limit , exec_method , overflow_message_type )
171
167
return value . send exec_method if ( RUBY_VERSION < '2.0' )
172
168
173
- check_memory_limit = true
174
- if ( defined? ( JRUBY_VERSION ) || ENV [ 'DEBUGGER_MEMORY_LIMIT' ] . to_i <= 0 )
175
- check_memory_limit = false
176
- end
169
+ check_memory_limit = !defined? ( JRUBY_VERSION ) && ENV [ 'DEBUGGER_MEMORY_LIMIT' ] . to_i > 0
177
170
curr_thread = Thread . current
178
- result = nil
179
- inspect_thread = DebugThread . start {
180
171
172
+ result = nil
173
+ inspect_thread = DebugThread . start do
181
174
start_alloc_size = ObjectSpace . memsize_of_all if ( check_memory_limit )
182
175
start_time = Time . now . to_f
183
176
184
- trace = TracePoint . new ( :c_call , :call ) do |tp |
177
+ trace_point = TracePoint . new ( :c_call , :call ) do | |
178
+ next unless Thread . current == inspect_thread
179
+ next unless rand > 0.75
185
180
186
- if ( rand > 0.75 )
187
- curr_time = Time . now . to_f
181
+ curr_time = Time . now . to_f
188
182
189
- if ( ( curr_time - start_time ) * 1e3 > time_limit )
190
- curr_thread . raise TimeLimitError . new ( "Timeout: evaluation of #{ exec_method } took longer than #{ time_limit } ms." , caller . to_a )
191
- inspect_thread . kill
192
- end
183
+ if ( curr_time - start_time ) * 1e3 > time_limit
184
+ curr_thread . raise TimeLimitError . new ( "Timeout: evaluation of #{ exec_method } took longer than #{ time_limit } ms." , caller . to_a , trace_point )
185
+ end
193
186
194
- if ( check_memory_limit )
195
- curr_alloc_size = ObjectSpace . memsize_of_all
196
- start_alloc_size = curr_alloc_size if ( curr_alloc_size < start_alloc_size )
187
+ if check_memory_limit
188
+ curr_alloc_size = ObjectSpace . memsize_of_all
189
+ start_alloc_size = curr_alloc_size if ( curr_alloc_size < start_alloc_size )
197
190
198
- if ( curr_alloc_size - start_alloc_size > 1e6 * memory_limit )
199
- curr_thread . raise MemoryLimitError . new ( "Out of memory: evaluation of #{ exec_method } took more than #{ memory_limit } mb." , caller . to_a )
200
- inspect_thread . kill
201
- end
191
+ if curr_alloc_size - start_alloc_size > 1e6 * memory_limit
192
+ curr_thread . raise MemoryLimitError . new ( "Out of memory: evaluation of #{ exec_method } took more than #{ memory_limit } mb." , caller . to_a , trace_point )
202
193
end
203
194
end
204
- end . enable {
205
- result = value . send exec_method
206
- }
207
- }
195
+ end
196
+ trace_point . enable
197
+ result = value . send exec_method
198
+ trace_point . disable
199
+ end
208
200
inspect_thread . join
209
- inspect_thread . kill
210
201
return result
211
- rescue MemoryLimitError , TimeLimitError => e
212
- print_debug ( e . message + "\n " + e . backtrace . map { |l | "\t #{ l } " } . join ( "\n " ) )
202
+ rescue ExecError => e
203
+ e . trace_point . disable
204
+ print_debug ( e . message + "\n " + e . backtrace . map { |l | "\t #{ l } " } . join ( "\n " ) )
213
205
return overflow_message_type . call ( e )
206
+ ensure
207
+ inspect_thread . kill if inspect_thread && inspect_thread . alive?
214
208
end
215
209
216
210
def print_variable ( name , value , kind )
@@ -514,4 +508,4 @@ def build_value_attr(escaped_value_str)
514
508
515
509
end
516
510
517
- end
511
+ end
0 commit comments