Class: Debugger::XmlPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-debug-ide/xml_printer.rb

Overview

:nodoc:

Defined Under Namespace

Classes: ExceptionProxy

Constant Summary collapse

@@monitor =
Monitor.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ XmlPrinter

Returns a new instance of XmlPrinter.



69
70
71
# File 'lib/ruby-debug-ide/xml_printer.rb', line 69

def initialize(interface)
  @interface = interface
end

Instance Attribute Details

#interfaceObject

Returns the value of attribute interface.



67
68
69
# File 'lib/ruby-debug-ide/xml_printer.rb', line 67

def interface
  @interface
end

Class Method Details

.protect(mname) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby-debug-ide/xml_printer.rb', line 53

def self.protect(mname)
  return if instance_methods.include?("__#{mname}")
  alias_method "__#{mname}", mname
  class_eval %{
    def #{mname}(*args, &block)
      @@monitor.synchronize do
        return unless @interface
        __#{mname}(*args, &block)
      end
    end
  }
end

Instance Method Details

#exec_with_allocation_control(value, exec_method, overflow_message_type) ⇒ Object



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
# File 'lib/ruby-debug-ide/xml_printer.rb', line 188

def exec_with_allocation_control(value, exec_method, overflow_message_type)
  return value.send exec_method unless Debugger.trace_to_s

  memory_limit = Debugger.debugger_memory_limit
  time_limit = Debugger.inspect_time_limit

  if defined?(JRUBY_VERSION) || RUBY_VERSION < '2.0' || memory_limit <= 0
    return exec_with_timeout(time_limit * 1e-3, "Timeout: evaluation of #{exec_method} took longer than #{time_limit}ms.") { value.send exec_method }
  end

  require 'objspace'
  trace_queue = Queue.new

  inspect_thread = DebugThread.start do
    start_alloc_size = ObjectSpace.memsize_of_all
    start_time = Time.now.to_f

    trace_point = TracePoint.new(:c_call, :call) do |tp|
      curr_time = Time.now.to_f

      if (curr_time - start_time) * 1e3 > time_limit
        trace_queue << TimeLimitError.new("Timeout: evaluation of #{exec_method} took longer than #{time_limit}ms.", caller.to_a)
        trace_point.disable
        inspect_thread.kill
      end

      next unless rand > 0.75

      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
        trace_queue << MemoryLimitError.new("Out of memory: evaluation of #{exec_method} took more than #{memory_limit}mb.", caller.to_a)
        trace_point.disable
        inspect_thread.kill
      end
    end
    trace_point.enable
    result = value.send exec_method
    trace_queue << result
    trace_point.disable
  end

  while(mes = trace_queue.pop)
    if(mes.is_a? TimeLimitError or mes.is_a? MemoryLimitError)
      print_debug(mes.message + "\n" + mes.backtrace.map {|l| "\t#{l}"}.join("\n"))
      return overflow_message_type.call(mes)
    else
      return mes
    end
  end
rescue SimpleTimeLimitError => e
  print_debug(e.message)
  return overflow_message_type.call(e)
end

#exec_with_timeout(sec, error_message) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ruby-debug-ide/xml_printer.rb', line 171

def exec_with_timeout(sec, error_message)
  return yield if sec == nil or sec.zero?
  if Thread.respond_to?(:critical) and Thread.critical
    raise ThreadError, "timeout within critical session"
  end
  begin
    x = Thread.current
    y = DebugThread.start {
      sleep sec
      x.raise SimpleTimeLimitError.new(error_message) if x.alive?
    }
    yield sec
  ensure
    y.kill if y and y.alive?
  end
end


137
138
139
140
141
142
143
144
145
# File 'lib/ruby-debug-ide/xml_printer.rb', line 137

def print_array(array)
  print_element("variables") do
    index = 0
    array.each {|e|
      print_variable('[' + index.to_s + ']', e, 'instance')
      index += 1
    }
  end
end


412
413
414
415
# File 'lib/ruby-debug-ide/xml_printer.rb', line 412

def print_at_line(context, file, line)
  print "<suspended file=\"%s\" line=\"%s\" threadId=\"%d\" frames=\"%d\"/>",
        CGI.escapeHTML(File.expand_path(file)), line, context.thnum, context.stack_size
end

Events



395
396
397
398
# File 'lib/ruby-debug-ide/xml_printer.rb', line 395

def print_breakpoint(_, breakpoint)
  print("<breakpoint file=\"%s\" line=\"%s\" threadId=\"%d\"/>",
        CGI.escapeHTML(breakpoint.source), breakpoint.pos, Debugger.current_context.thnum)
end


311
312
313
# File 'lib/ruby-debug-ide/xml_printer.rb', line 311

def print_breakpoint_added(b)
  print "<breakpointAdded no=\"%s\" location=\"%s:%s\"/>", b.id, CGI.escapeHTML(b.source), b.pos
end


315
316
317
# File 'lib/ruby-debug-ide/xml_printer.rb', line 315

def print_breakpoint_deleted(b)
  print "<breakpointDeleted no=\"%s\"/>", b.id
end


323
324
325
# File 'lib/ruby-debug-ide/xml_printer.rb', line 323

def print_breakpoint_disabled(b)
  print "<breakpointDisabled bp_id=\"%s\"/>", b.id
end


319
320
321
# File 'lib/ruby-debug-ide/xml_printer.rb', line 319

def print_breakpoint_enabled(b)
  print "<breakpointEnabled bp_id=\"%s\"/>", b.id
end


303
304
305
306
307
308
309
# File 'lib/ruby-debug-ide/xml_printer.rb', line 303

def print_breakpoints(breakpoints)
  print_element 'breakpoints' do
    breakpoints.sort_by {|b| b.id}.each do |b|
      print "<breakpoint n=\"%d\" file=\"%s\" line=\"%s\" />", b.id, CGI.escapeHTML(b.source), b.pos.to_s
    end
  end
end


400
401
402
403
404
# File 'lib/ruby-debug-ide/xml_printer.rb', line 400

def print_catchpoint(exception)
  context = Debugger.current_context
  print("<exception file=\"%s\" line=\"%s\" type=\"%s\" message=\"%s\" threadId=\"%d\"/>",
        CGI.escapeHTML(context.frame_file(0)), context.frame_line(0), exception.class, CGI.escapeHTML(exception.to_s), context.thnum)
end


335
336
337
338
339
340
341
# File 'lib/ruby-debug-ide/xml_printer.rb', line 335

def print_catchpoint_deleted(exception_class_name)
  if Debugger.catchpoint_deleted_event
    print "<catchpointDeleted exception=\"%s\"/>", exception_class_name
  else
    print_catchpoint_set(exception_class_name)
  end
end


331
332
333
# File 'lib/ruby-debug-ide/xml_printer.rb', line 331

def print_catchpoint_set(exception_class_name)
  print "<catchpointSet exception=\"%s\"/>", exception_class_name
end


327
328
329
# File 'lib/ruby-debug-ide/xml_printer.rb', line 327

def print_contdition_set(bp_id)
  print "<conditionSet bp_id=\"%d\"/>", bp_id
end


123
124
125
# File 'lib/ruby-debug-ide/xml_printer.rb', line 123

def print_context(context)
  print "<thread id=\"%s\" status=\"%s\" pid=\"%s\" #{current_thread_attr(context)}/>", context.thnum, context.thread.status, Process.pid
end


115
116
117
118
119
120
121
# File 'lib/ruby-debug-ide/xml_printer.rb', line 115

def print_contexts(contexts)
  print_element("threads") do
    contexts.each do |c|
      print_context(c) unless c.ignored?
    end
  end
end


104
105
106
# File 'lib/ruby-debug-ide/xml_printer.rb', line 104

def print_current_frame(frame_pos)
  print_debug "Selected frame no #{frame_pos}"
end

Sends debug message to the frontend if XML debug logging flag (–xml-debug) is on.



80
81
82
83
84
85
86
87
# File 'lib/ruby-debug-ide/xml_printer.rb', line 80

def print_debug(*args)
  Debugger.print_debug(*args)
  if Debugger.xml_debug
    msg, *args = args
    xml_message = CGI.escapeHTML(msg % args)
    @interface.print("<message debug='true'>#{xml_message}</message>")
  end
end


442
443
444
445
446
447
448
449
# File 'lib/ruby-debug-ide/xml_printer.rb', line 442

def print_element(name)
  print("<#{name}>")
  begin
    yield
  ensure
    print("</#{name}>")
  end
end


89
90
91
92
93
94
# File 'lib/ruby-debug-ide/xml_printer.rb', line 89

def print_error(*args)
  print_element("error") do
    msg, *args = args
    print CGI.escapeHTML(msg % args)
  end
end


360
361
362
# File 'lib/ruby-debug-ide/xml_printer.rb', line 360

def print_eval(exp, value)
  print "<eval expression=\"%s\" value=\"%s\" />", CGI.escapeHTML(exp), value
end


417
418
419
420
421
422
423
424
425
426
# File 'lib/ruby-debug-ide/xml_printer.rb', line 417

def print_exception(exception, _)
  print_element("variables") do
    proxy = ExceptionProxy.new(exception)
    InspectCommand.reference_result(proxy)
    print_variable('error', proxy, 'exception')
  end
rescue Exception
  print "<processingException type=\"%s\" message=\"%s\"/>",
        exception.class, CGI.escapeHTML(exception.to_s)
end


351
352
353
# File 'lib/ruby-debug-ide/xml_printer.rb', line 351

def print_expression(exp, value, idx)
  print "<dispay name=\"%s\" value=\"%s\" no=\"%d\" />", exp, value, idx
end


355
356
357
358
# File 'lib/ruby-debug-ide/xml_printer.rb', line 355

def print_expression_info(incomplete, prompt, indent)
  print "<expressionInfo incomplete=\"%s\" prompt=\"%s\" indent=\"%s\"></expressionInfo>",
        incomplete, CGI.escapeHTML(prompt), indent
end


343
344
345
346
347
348
349
# File 'lib/ruby-debug-ide/xml_printer.rb', line 343

def print_expressions(exps)
  print_element "expressions" do
    exps.each_with_index do |(exp, value), idx|
      print_expression(exp, value, idx + 1)
    end
  end unless exps.empty?
end


295
296
297
# File 'lib/ruby-debug-ide/xml_printer.rb', line 295

def print_file_excluded(file)
  print("<fileExcluded file=\"%s\"/>", file)
end


299
300
301
# File 'lib/ruby-debug-ide/xml_printer.rb', line 299

def print_file_filter_status(status)
  print("<fileFilter status=\"%s\"/>", status)
end


291
292
293
# File 'lib/ruby-debug-ide/xml_printer.rb', line 291

def print_file_included(file)
  print("<fileIncluded file=\"%s\"/>", file)
end


108
109
110
111
112
113
# File 'lib/ruby-debug-ide/xml_printer.rb', line 108

def print_frame(context, frame_id, current_frame_id)
  # idx + 1: one-based numbering as classic-debugger
  file = context.frame_file(frame_id)
  print "<frame no=\"%s\" file=\"%s\" line=\"%s\" #{"current='true' " if frame_id == current_frame_id}/>",
        frame_id + 1, CGI.escapeHTML(File.expand_path(file)), context.frame_line(frame_id)
end


96
97
98
99
100
101
102
# File 'lib/ruby-debug-ide/xml_printer.rb', line 96

def print_frames(context, current_frame_id)
  print_element("frames") do
    (0...context.stack_size).each do |id|
      print_frame(context, id, current_frame_id)
    end
  end
end


147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby-debug-ide/xml_printer.rb', line 147

def print_hash(hash)
  print_element("variables") do
    hash.keys.each {|k|
      if k.class.name == "String"
        name = '\'' + k + '\''
      else
        name = exec_with_allocation_control(k, :to_s, OverflowMessageType::EXCEPTION_MESSAGE)
      end
      print_variable(name, hash[k], 'instance')
    }
  end
end


428
429
430
431
432
# File 'lib/ruby-debug-ide/xml_printer.rb', line 428

def print_inspect(eval_result)
  print_element("variables") do
    print_variable("eval_result", eval_result, 'local')
  end
end


368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/ruby-debug-ide/xml_printer.rb', line 368

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 == line
          print "=> %d  %s\n", n, lines[n - 1].chomp
        else
          print "   %d  %s\n", n, lines[n - 1].chomp
        end
      end
    end
  else
    print "No source-file available for %s\n", file
  end
end


434
435
436
437
438
439
440
# File 'lib/ruby-debug-ide/xml_printer.rb', line 434

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
    print("<loadResult file=\"%s\" status=\"OK\"/>", file)
  end
end


385
386
387
388
389
390
391
# File 'lib/ruby-debug-ide/xml_printer.rb', line 385

def print_methods(methods)
  print_element "methods" do
    methods.each do |method|
      print "<method name=\"%s\" />", method
    end
  end
end


73
74
75
76
77
# File 'lib/ruby-debug-ide/xml_printer.rb', line 73

def print_msg(*args)
  msg, *args = args
  xml_message = CGI.escapeHTML(msg % args)
  print "<message>#{xml_message}</message>"
end


364
365
366
# File 'lib/ruby-debug-ide/xml_printer.rb', line 364

def print_pp(value)
  print value
end


160
161
162
163
164
165
166
167
168
169
# File 'lib/ruby-debug-ide/xml_printer.rb', line 160

def print_string(string)
  print_element("variables") do
    if string.respond_to?('bytes')
      bytes = string.bytes.to_a
      InspectCommand.reference_result(bytes)
      print_variable('bytes', bytes, 'instance')
    end
    print_variable('encoding', string.encoding, 'instance') if string.respond_to?('encoding')
  end
end


406
407
408
409
410
# File 'lib/ruby-debug-ide/xml_printer.rb', line 406

def print_trace(context, file, line)
  Debugger::print_debug "trace: location=\"%s:%s\", threadId=%d", file, line, context.thnum
  # TBD: do we want to clog fronend with the <trace> elements? There are tons of them.
  # print "<trace file=\"%s\" line=\"%s\" threadId=\"%d\" />", file, line, context.thnum
end


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/ruby-debug-ide/xml_printer.rb', line 244

def print_variable(name, value, kind)
  name = name.to_s
  if value.nil?
    print("<variable name=\"%s\" kind=\"%s\"/>", CGI.escapeHTML(name), kind)
    return
  end
  if value.is_a?(Array) || value.is_a?(Hash)
    has_children = !value.empty?
    if has_children
      size = value.size
      value_str = "#{value.class} (#{value.size} element#{size > 1 ? "s" : "" })"
    else
      value_str = "Empty #{value.class}"
    end
  elsif value.is_a?(String)
    has_children = value.respond_to?('bytes') || value.respond_to?('encoding')
    value_str = value
  else
    has_children = !value.instance_variables.empty? || !value.class.class_variables.empty?

    value_str = exec_with_allocation_control(value, :to_s, OverflowMessageType::EXCEPTION_MESSAGE) || 'nil' rescue "<#to_s method raised exception: #{$!}>"
    unless value_str.is_a?(String)
      value_str = "ERROR: #{value.class}.to_s method returns #{value_str.class}. Should return String."
    end
  end

  if value_str.respond_to?('encode')
    # noinspection RubyEmptyRescueBlockInspection
    begin
      value_str = value_str.encode("UTF-8")
    rescue
    end
  end
  value_str = handle_binary_data(value_str)
  escaped_value_str = CGI.escapeHTML(value_str)
  print("<variable name=\"%s\" %s kind=\"%s\" %s type=\"%s\" hasChildren=\"%s\" objectId=\"%#+x\">",
        CGI.escapeHTML(name), build_compact_value_attr(value, value_str), kind,
        build_value_attr(escaped_value_str), value.class,
        has_children, value.object_id)
  print("<value><![CDATA[%s]]></value>", escaped_value_str) if Debugger.value_as_nested_element
  print('</variable>')
rescue StandardError => e
  print_debug "Unexpected exception \"%s\"\n%s", e.to_s, e.backtrace.join("\n")
  print("<variable name=\"%s\" kind=\"%s\" value=\"%s\"/>",
        CGI.escapeHTML(name), kind, CGI.escapeHTML(safe_to_string(value)))
end


127
128
129
130
131
132
133
134
135
# File 'lib/ruby-debug-ide/xml_printer.rb', line 127

def print_variables(vars, kind)
  print_element("variables") do
    # print self at top position
    print_variable('self', yield('self'), kind) if vars.include?('self')
    vars.sort.each do |v|
      print_variable(v, yield(v), kind) unless v == 'self'
    end
  end
end