mrubyc-debugger 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a7b82d4f4c99cd5427745effca6e826b876af4254f92fcfcb6fd4b709e9f796
4
+ data.tar.gz: 3ab9373b7fc3d800a98d6c464b0567d0ffd4767534c95a60a26d7cc6090e1648
5
+ SHA512:
6
+ metadata.gz: 3689776d413b99e8283dc2cfab9aa217be888465ab5a3b318724fd3801636b87f4ca7ed8a93f64e9e45a68481b6dc266d2459855f5165294db9edf7b81177c0b
7
+ data.tar.gz: 7502349094bebaed003674d31be0633fc63edcbc1f4d3531446a98ade36691792aec2662e5698cfddeaa0dd2c40a6cf09392a6e537adedd9fdf55572c7064897
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hitoshi.hasumi@monstar-lab.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mrubyc-debugger.gemspec
6
+ gemspec
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mrubyc-debugger (0.1.0)
5
+ curses (~> 1.2)
6
+ thor (~> 0.20)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ curses (1.2.5)
12
+ diff-lcs (1.3)
13
+ rake (10.5.0)
14
+ rspec (3.8.0)
15
+ rspec-core (~> 3.8.0)
16
+ rspec-expectations (~> 3.8.0)
17
+ rspec-mocks (~> 3.8.0)
18
+ rspec-core (3.8.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-expectations (3.8.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-mocks (3.8.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-support (3.8.0)
27
+ thor (0.20.3)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 2.0)
34
+ mrubyc-debugger!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 HASUMI Hitoshi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,92 @@
1
+ # mrubyc-debugger
2
+
3
+ mrubyc-debugger is a TUI (test user interface) for developing [mruby/c](https://github.com/mrubyc/mrubyc) application. It runs mruby/c loops as CRuby Threads on your terminal.
4
+
5
+ 'loop' is, in short, infinite loop like `while true; hoge(); end` .
6
+
7
+ Caution: This gem is still experimental and not released yet.
8
+
9
+ ## Demo
10
+
11
+ ![demo](https://raw.githubusercontent.com/wiki/hasumikin/mrubyc-debugger/images/demo-2.gif)
12
+
13
+ ## Usage
14
+
15
+ - h:←, j:↓, k:↑, l:→ move the cursor to select a line
16
+ - SPACE toggles on and off of a breakpoint on the line which the cursor points
17
+
18
+ ## Features
19
+
20
+ - TUI (text user interface) powered by [Curses](https://github.com/ruby/curses)
21
+ - Visualize your loops, their local variables and debug printing of `puts`
22
+ - Originally mrubyc-debugger was designed for the sake of mruby/c application. But you may be able to use it to see CRuby's multi threads program, especially for learning Thread class
23
+
24
+ ## Features in future (possibly)
25
+
26
+ - Much more colorful. Like syntax highlighting
27
+ - Cooperation with [mrubyc-test](https://github.com/hasumikin/mrubyc-test)
28
+ - Using stub and mock declarations in test cases to simulate an integrated circumstance
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'mrubyc-debugger'
36
+ ```
37
+
38
+ And then execute:
39
+
40
+ $ bundle
41
+
42
+ Or install it yourself as:
43
+
44
+ $ gem install mrubyc-debugger
45
+
46
+ ## Usage
47
+
48
+ Assuming you are using [mrubyc-utils](https://github.com/hasumikin/mrubyc-utils) to manage your project and [rbenv](https://github.com/rbenv/rbenv) to manage Ruby versions.
49
+ This means you have `.mrubycconfig` file in your top directory of your project.
50
+
51
+ Besides, you have to locate mruby loop files that are the target of debugging like `mrblib/loops/main.rb`
52
+
53
+ This is an example of ESP32 project:
54
+
55
+ ```
56
+ ~/your_project $ tree
57
+ .
58
+ ├── .mrubycconfig # Created by mrubyc-utils
59
+ ├── Makefile
60
+ ├── build
61
+ ├── components
62
+ ├── main
63
+ ├── mrblib
64
+ │      └── models
65
+ │            ├── class_name.rb # models are tested by mrubyc-test
66
+ │            └── my_class.rb # models are tested by mrubyc-test
67
+ │      └── loops # Place your loop files here
68
+ │            ├── master.rb # A loop something like awaiting for user input
69
+ │            └── slave.rb # Another loop eg) BLE status observation, LED blinking, etc.
70
+ ├── mrubyc-debugger.yml # You can configure stub methods in form of YAML
71
+ └── sdkconfig
72
+ ```
73
+
74
+ At the top directory:
75
+
76
+ $ mrubyc-debugger
77
+
78
+ To make your loops slow:
79
+
80
+ $ mrubyc-debugger --delay 1
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hasumikin/mrubyc-debugger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
89
+
90
+ ## Code of Conduct
91
+
92
+ Everyone interacting in the Mrubyc::Debugger project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hasumikin/mrubyc-debugger/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mrubyc/debugger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mrubyc/debugger'
4
+
5
+ Mrubyc::Debugger::Bootstrap.start
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mrubyc/debugger/version"
4
+ require "mrubyc/debugger/config"
5
+ require "mrubyc/debugger/mrblib"
6
+ require "mrubyc/debugger/window"
7
+ require "thor"
8
+ require "yaml"
9
+ require "logger"
10
+
11
+ module Mrubyc
12
+ module Debugger
13
+ class Error < StandardError; end
14
+
15
+ class Bootstrap < Thor
16
+ default_command :start
17
+
18
+ desc 'start', 'start debug window'
19
+ option :delay, type: :numeric, default: 0.1, banner: "Each of lines have a delay of this time (unit: second)"
20
+ option :debug, type: :boolean, default: false, banner: "Log appears at /tmp/mrubyc-debugger.log"
21
+ def start
22
+ result = Mrubyc::Debugger::Config.check
23
+ unless result
24
+ puts "\e[31;1m"
25
+ puts 'Error'
26
+ result[:messages].each do |message|
27
+ puts ' ' + message
28
+ end
29
+ puts "\e[0m"
30
+ exit(1)
31
+ end
32
+ mrblib_dir = if File.exists?('.mrubycconfig')
33
+ config = YAML.load_file('.mrubycconfig')
34
+ config['mruby_lib_dir']
35
+ else
36
+ ENV['GEM_ENV'] == 'test' ? 'spec/fixtures/files' : 'mrblib'
37
+ end
38
+ models = Dir.glob(File.join(Dir.pwd, mrblib_dir, "models", "*.rb"))
39
+ loops = Dir.glob(File.join(Dir.pwd, mrblib_dir, "loops", "*.rb"))
40
+ if loops.size == 0
41
+ puts "Exit as no loop found"
42
+ exit(1)
43
+ end
44
+ mrblibs = {
45
+ models: models,
46
+ loops: loops
47
+ }
48
+ # Mrubyc::Debugger::Mrblib.setup_models(mrblibs[:models])
49
+ yaml = File.join(Dir.pwd, "mrubyc-debugger.yml")
50
+ stubs = if File.exists?(yaml)
51
+ YAML.load_file(yaml)
52
+ else
53
+ {}
54
+ end
55
+ Mrubyc::Debugger::Window.setup_models(mrblibs[:models], stubs)
56
+ $logger = Logger.new("/tmp/mrubyc-debugger.log")
57
+ $logger.level = if options[:debug]
58
+ Logger::DEBUG
59
+ else
60
+ Logger::INFO
61
+ end
62
+ Mrubyc::Debugger::Window.start(mrblibs, options[:delay])
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mrubyc
4
+ module Debugger
5
+ class Config
6
+ class << self
7
+ def check
8
+ true
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,375 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'curses'
4
+
5
+ module Mrubyc
6
+ module Debugger
7
+ class Console
8
+
9
+ include Curses
10
+
11
+ COL_A = [
12
+ COLOR_BLACK,
13
+ COLOR_BLUE,
14
+ COLOR_CYAN,
15
+ COLOR_GREEN,
16
+ COLOR_MAGENTA,
17
+ COLOR_RED,
18
+ COLOR_WHITE,
19
+ COLOR_YELLOW
20
+ ]
21
+
22
+ ESCDELAY = 25
23
+
24
+ def initialize(loops)
25
+ @srcs = []
26
+ loops.each do |loop|
27
+ src = []
28
+ File.open(loop, 'r') do |f|
29
+ f.each_line do |line|
30
+ src << line
31
+ end
32
+ end
33
+ @srcs << src
34
+ end
35
+ cbreak # raw?
36
+ noecho # stop echo back
37
+ set_escdelay ESCDELAY # response speed. default 1000
38
+ @cursor_pos = { x: 0, y: 1}
39
+ @sleepers = Array.new(@srcs.size)
40
+ @events = Array.new(@srcs.size)
41
+ end
42
+
43
+ def set_escdelay(ms)
44
+ Curses.ESCDELAY = ms
45
+ rescue NotImplementedError
46
+ end
47
+
48
+ def make_pair
49
+ # 0 can't be changed
50
+ no = 0
51
+ COL_A.each do |c0|
52
+ COL_A.each do |c1|
53
+ init_pair(no, c0, c1)
54
+ no += 1
55
+ end
56
+ end
57
+ end
58
+
59
+ def col_sample
60
+ max = COL_A.size
61
+ no = 0
62
+ max.times do |y|
63
+ max.times do |x|
64
+ setpos(y, x * 6)
65
+ s = " %03d "%no
66
+ attron(color_pair(no))
67
+ addstr(s)
68
+ attroff(color_pair(no))
69
+ no += 1
70
+ end
71
+ end
72
+ s = "colors: %d"%(colors)
73
+ setpos(12, 0)
74
+ addstr(s)
75
+ s = "color_pairs: %d"%(color_pairs)
76
+ setpos(13, 0)
77
+ addstr(s)
78
+ end
79
+
80
+ def show_colors
81
+ col_sample
82
+ refresh
83
+ getch
84
+ end
85
+
86
+ def color_num_by(level)
87
+ case level
88
+ when :info
89
+ 24
90
+ when :debug
91
+ 32
92
+ when :warn
93
+ 56
94
+ when :error
95
+ 40
96
+ end
97
+ end
98
+
99
+ def run(show_colors_at_start = false)
100
+ mainwin = init_screen
101
+ mainwin.keypad true
102
+ mainwin.timeout = 0
103
+ curs_set(0)
104
+ start_color
105
+ make_pair
106
+ show_colors if show_colors_at_start
107
+
108
+ begin
109
+ wins = []
110
+ num = @srcs.size
111
+ num.times do |i|
112
+ win = {}
113
+ win[:src] = mainwin.subwin(lines - 6, cols / num, 0, i * cols / num)
114
+ win[:out] = mainwin.subwin(1, cols / num, lines - 6, i * cols / num)
115
+ win[:var] = mainwin.subwin(5, cols / num, lines - 5, i * cols / num)
116
+ wins << win
117
+ end
118
+ while true
119
+ @key = mainwin.getch
120
+ handle_key
121
+ num.times do |i|
122
+ wins[i][:src].resize(lines - 6, cols / num)
123
+ wins[i][:out].resize(1, cols / num)
124
+ wins[i][:var].resize(5, cols / num)
125
+ unless $sleep_queues[i].empty?
126
+ @sleepers[i] = $sleep_queues[i].pop
127
+ end
128
+ if @sleepers[i] && @sleepers[i] < Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW, :millisecond)
129
+ @sleepers[i] = nil
130
+ $threads[i].run
131
+ end
132
+ unless $debug_queues[i].empty?
133
+ message = $debug_queues[i].pop
134
+ wins[i][:out].setpos(0, 0)
135
+ color_num = color_num_by(message[:level])
136
+ wins[i][:out].addstr '|'
137
+ wins[i][:out].attron(color_pair color_num)
138
+ wins[i][:out].addstr " #{message[:level].to_s[0].upcase}) " + message[:body].ljust(wins[i][:out].maxx-6)
139
+ wins[i][:out].attroff(color_pair color_num)
140
+ wins[i][:out].addstr '|'
141
+ wins[i][:out].refresh
142
+ end
143
+ unless $event_queues[i].empty?
144
+ @events[i] = $event_queues[i].pop
145
+ end
146
+ # needs this condition in spite of being used at the first time
147
+ # TODO refactor
148
+ if @events[i]
149
+ threads_debug_print(i)
150
+ (1..(wins[i][:src].maxy - 2)).each do |y|
151
+ wins[i][:src].setpos(y, 1)
152
+ if !@srcs[i][y]
153
+ wins[i][:src].addstr ' ' * wins[i][:src].maxx
154
+ else
155
+ lineno = @events[i][:lineno] - 1 # hide `using DebugQueue` line
156
+ wins[i][:src].attron(A_UNDERLINE) if y == @cursor_pos[:y] && i == @cursor_pos[:x]
157
+ wins[i][:src].attron(A_REVERSE) if y == lineno
158
+ lineno_color = if $breakpoints.any? {|bp| bp == [i, y] }
159
+ 21
160
+ else
161
+ 16
162
+ end
163
+ wins[i][:src].attron(color_pair lineno_color)
164
+ wins[i][:src].attron(A_BOLD)
165
+ wins[i][:src].addstr y.to_s.rjust(2).to_s
166
+ wins[i][:src].attroff(A_BOLD)
167
+ wins[i][:src].attroff(color_pair lineno_color)
168
+ wins[i][:src].addstr ' ' + @srcs[i][y]
169
+ wins[i][:src].attroff(A_REVERSE) if y == lineno
170
+ wins[i][:src].attroff(A_UNDERLINE) if y == @cursor_pos[:y] && i == @cursor_pos[:x]
171
+ end
172
+ end
173
+ wins[i][:src].box(?|,?-,?+)
174
+ wins[i][:src].refresh
175
+ if @events[i][:breakpoint]
176
+ command_line(i, wins[i][:var], @events[i][:tp_binding])
177
+ @events[i][:breakpoint] = nil # to avoid #sleep line remains Thread.stop
178
+ else
179
+ vars = {}
180
+ @events[i][:tp_binding].local_variables.each do |var|
181
+ vars[var] = @events[i][:tp_binding].local_variable_get(var).inspect
182
+ end
183
+ vars.each_with_index do |(k,v),j|
184
+ wins[i][:var].setpos(j+1, 2)
185
+ wins[i][:var].addstr (k.to_s + ' => ' + v).ljust(wins[i][:var].maxx)
186
+ end
187
+ box_var_win(wins[i][:var], 16)
188
+ end
189
+ end
190
+ end
191
+ refresh
192
+ end
193
+ rescue => e
194
+ sleep 5
195
+ ensure
196
+ finish
197
+ end
198
+ end
199
+
200
+ private
201
+
202
+ def clear_var_win(win)
203
+ (1..3).each do |l|
204
+ win.setpos(l, 1)
205
+ win << " " * (win.maxx - 2)
206
+ end
207
+ end
208
+
209
+ def box_var_win(win, color)
210
+ win.attron(color_pair color)
211
+ win.box(?|,?-,?+)
212
+ win.attroff(color_pair color)
213
+ win.refresh
214
+ end
215
+
216
+ def command_line(i, win, tp_binding)
217
+ box_var_win(win, 21)
218
+ loop do
219
+ clear_var_win(win)
220
+ win.setpos(1, 1)
221
+ win << " > "
222
+ win.refresh
223
+ str = getstr_with_echo(win)
224
+ case str
225
+ when "exit"
226
+ clear_var_win(win)
227
+ win.refresh
228
+ resume(i)
229
+ return
230
+ when ""
231
+ # do nothing
232
+ else
233
+ win.setpos(2, 2)
234
+ win << " => "
235
+ index = str.index("=")
236
+ args = if index
237
+ [ str[0, index - 1].strip,
238
+ str[index + 1, str.size - index].strip ]
239
+ else
240
+ str.strip
241
+ end
242
+ begin
243
+ result = if args.is_a?(Array)
244
+ tp_binding.local_variable_set(args[0], eval(args[1]))
245
+ else
246
+ if tp_binding.local_variables.map(&:to_s).include?(args)
247
+ tp_binding.local_variable_get(args)
248
+ else
249
+ eval(args)
250
+ end
251
+ end
252
+ win << result.to_s[0, win.maxx - 7]
253
+ rescue => e
254
+ win << e.to_s[0, win.maxx - 7]
255
+ end
256
+ win.setpos(3, 2)
257
+ win << "Enter to continue"
258
+ box_var_win(win, 21)
259
+ loop do
260
+ case getch
261
+ when nil
262
+ sleep ESCDELAY / 1000.0
263
+ when KEY_CTRL_J # Enter
264
+ break
265
+ end
266
+ end
267
+ end
268
+ end
269
+ end
270
+
271
+ def getstr_with_echo(win)
272
+ str = "".dup
273
+ loop do
274
+ case (c = Curses.getch)
275
+ when nil
276
+ # ignore
277
+ when KEY_CTRL_J # Enter
278
+ break if str.size > 0
279
+ when String
280
+ win << c
281
+ win.refresh
282
+ str << c
283
+ when KEY_BACKSPACE
284
+ if str.size > 0
285
+ win.setpos(1, str.size + 3)
286
+ win << " "
287
+ win.setpos(1, str.size + 3)
288
+ win.refresh
289
+ str.chop!
290
+ end
291
+ end
292
+ end
293
+ str
294
+ end
295
+
296
+ def handle_key
297
+ case @key
298
+ when 27 # ESC
299
+ exit(0)
300
+ when "h"
301
+ go_left
302
+ when "j"
303
+ go_down
304
+ when "k"
305
+ go_up
306
+ when "l"
307
+ go_right
308
+ when " "
309
+ breakpoint
310
+ end
311
+ end
312
+
313
+ def resume(i)
314
+ $threads[i].run if $threads[i].stop?
315
+ end
316
+
317
+ def breakpoint
318
+ unless $breakpoints.delete([@cursor_pos[:x], @cursor_pos[:y]])
319
+ $breakpoints << [@cursor_pos[:x], @cursor_pos[:y]]
320
+ end
321
+ end
322
+
323
+ def go_left
324
+ @cursor_pos[:x] -= 1
325
+ if @cursor_pos[:x] < 0
326
+ @cursor_pos[:x] = @srcs.size - 1
327
+ end
328
+ rescue_overflow
329
+ end
330
+
331
+ def go_right
332
+ @cursor_pos[:x] += 1
333
+ if @cursor_pos[:x] >= @srcs.size
334
+ @cursor_pos[:x] = 0
335
+ end
336
+ rescue_overflow
337
+ end
338
+
339
+ def rescue_overflow
340
+ if @cursor_pos[:y] >= @srcs[@cursor_pos[:x]].size
341
+ @cursor_pos[:y] = @srcs[@cursor_pos[:x]].size - 1
342
+ end
343
+ end
344
+
345
+ def go_down
346
+ @cursor_pos[:y] += 1
347
+ if @cursor_pos[:y] >= @srcs[@cursor_pos[:x]].size
348
+ @cursor_pos[:y] = 1
349
+ end
350
+ end
351
+
352
+ def go_up
353
+ @cursor_pos[:y] -= 1
354
+ if @cursor_pos[:y] == 0
355
+ @cursor_pos[:y] = @srcs[@cursor_pos[:x]].size - 1
356
+ end
357
+ end
358
+
359
+ def finish
360
+ close_screen
361
+ system "stty sane"
362
+ puts "finished"
363
+ end
364
+
365
+ def threads_debug_print(i)
366
+ $logger.debug @events[i].to_s if @events[i]
367
+ $logger.debug $breakpoints.to_s
368
+ $threads.each_with_index do |thread, index|
369
+ $logger.debug "thread #{index}: stop? => #{thread.stop?}"
370
+ end
371
+ end
372
+
373
+ end
374
+ end
375
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DebugQueue
4
+ refine Kernel do
5
+ def puts(text)
6
+ if $debug_queues
7
+ $debug_queues[Thread.current[:index]] << {
8
+ level: :debug,
9
+ body: text
10
+ }
11
+ end
12
+ end
13
+
14
+ def sleep(sec)
15
+ current_msec = Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW, :millisecond)
16
+ $sleep_queues[Thread.current[:index]] << (current_msec + (sec * 1000)) # wakeup at
17
+ Thread.stop
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kernel
4
+ def relinquish
5
+ sleep 0.01
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mrubyc
4
+ module Debugger
5
+ class Mrblib
6
+ class << self
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mrubyc
4
+ module Debugger
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mrubyc/debugger/ext/kernel"
4
+ require "mrubyc/debugger/ext/debug_queue"
5
+ require 'mrubyc/debugger/console.rb'
6
+ require "tempfile"
7
+
8
+ module Mrubyc
9
+ module Debugger
10
+ class Window
11
+ class << self
12
+ def start(mrblibs, delay)
13
+ loops = mrblibs[:loops]
14
+ $breakpoints = []
15
+ $debug_queues = []
16
+ $event_queues = []
17
+ $sleep_queues = []
18
+ loops.size.times do
19
+ $debug_queues << Queue.new
20
+ $event_queues << Queue.new
21
+ $sleep_queues << Queue.new
22
+ end
23
+ $threads = []
24
+ temp_loops = []
25
+ loops.each_with_index do |loop, index|
26
+ tempfile = Tempfile.new
27
+ temp_loops << tempfile.path
28
+ tempfile.puts "using DebugQueue; sleep 2"
29
+ tempfile.puts File.read(loop)
30
+ tempfile.close
31
+ $threads << Thread.new(index) do
32
+ Thread.current[:index] = index
33
+ load temp_loops[index]
34
+ end
35
+ $debug_queues[index] << {
36
+ level: :info,
37
+ body: "loop: #{File.basename(loop)} started"
38
+ }
39
+ end
40
+ $threads << Thread.new do
41
+ console = Mrubyc::Debugger::Console.new(temp_loops)
42
+ console.run
43
+ end
44
+ @@mutex = Mutex.new
45
+ trace(temp_loops, delay).enable do
46
+ $threads.each do|thr|
47
+ thr.join
48
+ end
49
+ end
50
+ end
51
+
52
+ def trace(loops, delay)
53
+ TracePoint.new(:c_call, :call, :line) do |tp|
54
+ number = nil
55
+ caller_locations(1, 1).each do |caller_location|
56
+ loops.each_with_index do |loop, index|
57
+ if caller_location.to_s.include?(File.basename(loop))
58
+ number = index
59
+ break
60
+ end
61
+ end
62
+ if number
63
+ @@mutex.lock
64
+ event = {
65
+ method_id: tp.method_id,
66
+ lineno: tp.lineno,
67
+ caller_location: caller_location,
68
+ tp_binding: tp.binding
69
+ }
70
+ # breakpoint will be duplicated if method_id is not nil (== event is not :line)
71
+ if tp.method_id.nil? && $breakpoints.any?{|bp| bp == [number, tp.lineno - 1]}
72
+ event[:breakpoint] = true
73
+ end
74
+ $event_queues[number].push event
75
+ sleep delay if tp.event == :line
76
+ # should stop after push event and sleep
77
+ Thread.stop if event[:breakpoint] == true
78
+ @@mutex.unlock
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ def setup_models(models, stubs)
85
+ models.each do |model|
86
+ load model
87
+ class_name = File.basename(model, '.rb').split('_').map(&:capitalize).join
88
+ Kernel.const_get(class_name).class_eval do
89
+ if stubs["classes"] && stubs["classes"][class_name] && stubs["classes"][class_name]["instance_methods"]
90
+ stubs["classes"][class_name]["instance_methods"].each do |m|
91
+ define_method(m["name"]) do
92
+ eval m["value"]
93
+ end
94
+ end
95
+ end
96
+ def method_missing(method_name, *args)
97
+ if $debug_queues
98
+ $debug_queues[Thread.current[:index]] << {
99
+ level: :error,
100
+ body: "method_missing: #{self.class}##{method_name}"
101
+ }
102
+ else
103
+ super
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mrubyc/debugger/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mrubyc-debugger"
8
+ spec.version = Mrubyc::Debugger::VERSION
9
+ spec.authors = ["HASUMI Hitoshi"]
10
+ spec.email = ["hasumikin@gmail.com"]
11
+
12
+ spec.summary = %q{Debugger for mruby/c mutli tasks (threads)}
13
+ spec.description = %q{mrubyc-debugger is a TUI (text user interface) tool that runs mruby/c tasks as CRuby ::Thread on your terminal}
14
+ spec.homepage = "https://github.com/hasumikin/mrubyc-debugger"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 2.0"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+
30
+ spec.add_dependency "curses", "~> 1.2"
31
+ spec.add_dependency "thor", "~> 0.20"
32
+ end
@@ -0,0 +1,2 @@
1
+ ---
2
+ mruby_lib_dir: mrblib
@@ -0,0 +1 @@
1
+ 2.6.2
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mrubyc-debugger"
@@ -0,0 +1,7 @@
1
+ ## How to start sample
2
+
3
+ - cd [into/this/dir]
4
+ - bundle install
5
+ - bundle exec mrubyc-debugger
6
+ - That's it!
7
+
@@ -0,0 +1,18 @@
1
+ $my_obj = MyClass.new
2
+ local_var = 0
3
+
4
+ $mutex = Mutex.new
5
+
6
+ while true
7
+ $mutex.lock()
8
+ puts "LOCKED"
9
+ sleep 3
10
+ $mutex.unlock()
11
+ if local_var > 100
12
+ puts $my_obj.alert
13
+ sleep 2
14
+ end
15
+ sleep 2
16
+ local_var += 1
17
+ end
18
+
@@ -0,0 +1,17 @@
1
+ while !$mutex
2
+ relinquish()
3
+ end
4
+
5
+ while true
6
+ sleep 1
7
+ puts "trying to LOCK"
8
+ $mutex.lock()
9
+ $my_obj.still_not_defined_method
10
+ sleep 2
11
+ local_var = $my_obj.stub_method
12
+ sleep 2
13
+ puts "local_var: #{local_var}"
14
+ $mutex.unlock()
15
+ sleep 2
16
+ end
17
+
@@ -0,0 +1,6 @@
1
+ class MyClass
2
+ attr_accessor :result
3
+ def alert
4
+ "LOOP count is over one hundred!"
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ classes:
2
+ MyClass:
3
+ instance_methods:
4
+ - name: stub_method
5
+ value: rand(100)
6
+
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mrubyc-debugger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - HASUMI Hitoshi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: curses
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: thor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.20'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.20'
83
+ description: mrubyc-debugger is a TUI (text user interface) tool that runs mruby/c
84
+ tasks as CRuby ::Thread on your terminal
85
+ email:
86
+ - hasumikin@gmail.com
87
+ executables:
88
+ - mrubyc-debugger
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - exe/mrubyc-debugger
104
+ - lib/mrubyc/debugger.rb
105
+ - lib/mrubyc/debugger/config.rb
106
+ - lib/mrubyc/debugger/console.rb
107
+ - lib/mrubyc/debugger/ext/debug_queue.rb
108
+ - lib/mrubyc/debugger/ext/kernel.rb
109
+ - lib/mrubyc/debugger/mrblib.rb
110
+ - lib/mrubyc/debugger/version.rb
111
+ - lib/mrubyc/debugger/window.rb
112
+ - mrubyc-debugger.gemspec
113
+ - sample/.mrubycconfig
114
+ - sample/.ruby-version
115
+ - sample/Gemfile
116
+ - sample/README.md
117
+ - sample/mrblib/loops/master.rb
118
+ - sample/mrblib/loops/slave.rb
119
+ - sample/mrblib/models/my_class.rb
120
+ - sample/mrubyc-debugger.yml
121
+ homepage: https://github.com/hasumikin/mrubyc-debugger
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubygems_version: 3.0.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Debugger for mruby/c mutli tasks (threads)
144
+ test_files: []