Class: Mrubyc::Debugger::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/mrubyc/debugger/window.rb

Class Method Summary collapse

Class Method Details

.setup_models(models, stubs) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mrubyc/debugger/window.rb', line 84

def setup_models(models, stubs)
  models.each do |model|
    load model
    class_name = File.basename(model, '.rb').split('_').map(&:capitalize).join
    Kernel.const_get(class_name).class_eval do
      if stubs["classes"] && stubs["classes"][class_name] && stubs["classes"][class_name]["instance_methods"]
        stubs["classes"][class_name]["instance_methods"].each do |m|
          define_method(m["name"]) do
            eval m["value"]
          end
        end
      end
      def method_missing(method_name, *args)
        if $debug_queues
          $debug_queues[Thread.current[:index]] << {
            level: :error,
            body: "method_missing: #{self.class}##{method_name}"
          }
        else
          super
        end
      end
    end
  end
end

.start(mrblibs, delay) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mrubyc/debugger/window.rb', line 12

def start(mrblibs, delay)
  loops = mrblibs[:loops]
  $breakpoints = []
  $debug_queues = []
  $event_queues = []
  $sleep_queues = []
  loops.size.times do
    $debug_queues << Queue.new
    $event_queues << Queue.new
    $sleep_queues << Queue.new
  end
  $threads = []
  temp_loops = []
  loops.each_with_index do |loop, index|
    tempfile = Tempfile.new
    temp_loops << tempfile.path
    tempfile.puts "using DebugQueue; sleep 2"
    tempfile.puts File.read(loop)
    tempfile.close
    $threads << Thread.new(index) do
      Thread.current[:index] = index
      load temp_loops[index]
    end
    $debug_queues[index] << {
      level: :info,
      body: "loop: #{File.basename(loop)} started"
    }
  end
  $threads << Thread.new do
    console = Mrubyc::Debugger::Console.new(temp_loops)
    console.run
  end
  @@mutex = Mutex.new
  trace(temp_loops, delay).enable do
    $threads.each do|thr|
      thr.join
    end
  end
end

.trace(loops, delay) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mrubyc/debugger/window.rb', line 52

def trace(loops, delay)
  TracePoint.new(:c_call, :call, :line) do |tp|
    number = nil
    caller_locations(1, 1).each do |caller_location|
      loops.each_with_index do |loop, index|
        if caller_location.to_s.include?(File.basename(loop))
          number = index
          break
        end
      end
      if number
        @@mutex.lock
        event = {
          method_id: tp.method_id,
          lineno: tp.lineno,
          caller_location: caller_location,
          tp_binding: tp.binding
        }
        # breakpoint will be duplicated if method_id is not nil (== event is not :line)
        if tp.method_id.nil? && $breakpoints.any?{|bp| bp == [number, tp.lineno - 1]}
          event[:breakpoint] = true
        end
        $event_queues[number].push event
        sleep delay if tp.event == :line
        # should stop after push event and sleep
        Thread.stop if event[:breakpoint] == true
        @@mutex.unlock
      end
    end
  end
end