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

#do_print_hash(hash) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ruby-debug-ide/xml_printer.rb', line 156

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

#do_print_hash_key_value(hash) ⇒ Object



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

def do_print_hash_key_value(hash)
  print_element("variables", {:type => 'hashItem'}) do
    hash.each {|(k, v)|
      print_variable('key', k, 'instance')
      print_variable('value', v, 'instance')
    }
  end
end

#exec_with_allocation_control(value, exec_method, overflow_message_type) ⇒ Object



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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ruby-debug-ide/xml_printer.rb', line 205

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



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ruby-debug-ide/xml_printer.rb', line 188

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


431
432
433
434
# File 'lib/ruby-debug-ide/xml_printer.rb', line 431

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



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

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


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

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


334
335
336
# File 'lib/ruby-debug-ide/xml_printer.rb', line 334

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


342
343
344
# File 'lib/ruby-debug-ide/xml_printer.rb', line 342

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


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

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


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

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


419
420
421
422
423
# File 'lib/ruby-debug-ide/xml_printer.rb', line 419

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


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

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


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

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


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

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


461
462
463
464
465
466
467
468
469
470
# File 'lib/ruby-debug-ide/xml_printer.rb', line 461

def print_element(name, additional_tags = nil)
  additional_tags_presentation = additional_tags.nil? ? '' : additional_tags.map {|tag, value| " #{tag}=\"#{value}\""}.reduce(:+)

  print("<#{name}#{additional_tags_presentation}>")
  begin
    yield if block_given?
  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


379
380
381
# File 'lib/ruby-debug-ide/xml_printer.rb', line 379

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


436
437
438
439
440
441
442
443
444
445
# File 'lib/ruby-debug-ide/xml_printer.rb', line 436

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


370
371
372
# File 'lib/ruby-debug-ide/xml_printer.rb', line 370

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


374
375
376
377
# File 'lib/ruby-debug-ide/xml_printer.rb', line 374

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


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

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


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

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


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

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


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

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


169
170
171
172
173
174
175
# File 'lib/ruby-debug-ide/xml_printer.rb', line 169

def print_hash(hash)
  if Debugger.key_value_mode
    do_print_hash_key_value(hash)
  else
    do_print_hash(hash)
  end
end


447
448
449
450
451
# File 'lib/ruby-debug-ide/xml_printer.rb', line 447

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


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/ruby-debug-ide/xml_printer.rb', line 387

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


453
454
455
456
457
458
459
# File 'lib/ruby-debug-ide/xml_printer.rb', line 453

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


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

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


383
384
385
# File 'lib/ruby-debug-ide/xml_printer.rb', line 383

def print_pp(value)
  print value
end


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

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


425
426
427
428
429
# File 'lib/ruby-debug-ide/xml_printer.rb', line 425

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


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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/ruby-debug-ide/xml_printer.rb', line 261

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