debug 1.0.0.beta8 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,242 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DEBUGGER__
4
+ class Tracer
5
+ include SkipPathHelper
6
+ include Color
7
+
8
+ def colorize(str, color)
9
+ # don't colorize trace sent into a file
10
+ if @into
11
+ str
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ attr_reader :type
18
+
19
+ def initialize ui, pattern: nil, into: nil
20
+ if /\ADEBUGGER__::(([A-Z][a-z]+?)[A-Z][a-z]+)/ =~ self.class.name
21
+ @name = $1
22
+ @type = $2.downcase
23
+ end
24
+
25
+ setup
26
+
27
+ if pattern
28
+ @pattern = Regexp.compile(pattern)
29
+ else
30
+ @pattern = nil
31
+ end
32
+
33
+ if @into = into
34
+ @output = File.open(into, 'w')
35
+ @output.puts "PID:#{Process.pid} #{self}"
36
+ else
37
+ @output = ui
38
+ end
39
+
40
+ enable
41
+ end
42
+
43
+ def header depth
44
+ "DEBUGGER (trace/#{@type}) \#th:#{Thread.current.instance_variable_get(:@__thread_client_id)} \#depth:#{'%-2d'%depth}"
45
+ end
46
+
47
+ def enable
48
+ @tracer.enable
49
+ end
50
+
51
+ def disable
52
+ @tracer.disable
53
+ end
54
+
55
+ def description
56
+ nil
57
+ end
58
+
59
+ def to_s
60
+ s = "#{@name}#{description} (#{@tracer.enabled? ? 'enabled' : 'disabled'})"
61
+ s += " with pattern #{@pattern.inspect}" if @pattern
62
+ s += " into: #{@into}" if @into
63
+ s
64
+ end
65
+
66
+ def skip? tp
67
+ if tp.path.start_with?(__dir__) ||
68
+ tp.path.start_with?('<internal:') ||
69
+ ThreadClient.current.management? ||
70
+ skip_path?(tp.path) ||
71
+ skip_with_pattern?(tp)
72
+ true
73
+ else
74
+ false
75
+ end
76
+ end
77
+
78
+ def skip_with_pattern?(tp)
79
+ @pattern && !tp.path.match?(@pattern)
80
+ end
81
+
82
+ def out tp, msg = nil, depth = caller.size - 1
83
+ location_str = colorize("#{tp.path}:#{tp.lineno}", [:GREEN])
84
+ buff = "#{header(depth)}#{msg} at #{location_str}"
85
+
86
+ if false # TODO: Ractor.main?
87
+ ThreadClient.current.on_trace self.object_id, buff
88
+ else
89
+ @output.puts buff
90
+ end
91
+ end
92
+
93
+ def puts msg
94
+ @output.puts msg
95
+ end
96
+
97
+ def minfo tp
98
+ klass = tp.defined_class
99
+
100
+ if klass.singleton_class?
101
+ "#{tp.self}.#{tp.method_id}"
102
+ else
103
+ "#{klass}\##{tp.method_id}"
104
+ end
105
+ end
106
+ end
107
+
108
+ class LineTracer < Tracer
109
+ def setup
110
+ @tracer = TracePoint.new(:line){|tp|
111
+ next if skip?(tp)
112
+ # pp tp.object_id, caller(0)
113
+ out tp
114
+ }
115
+ end
116
+ end
117
+
118
+ class CallTracer < Tracer
119
+ def setup
120
+ @tracer = TracePoint.new(:a_call, :a_return){|tp|
121
+ next if skip?(tp)
122
+
123
+ depth = caller.size
124
+ sp = ' ' * depth
125
+
126
+ call_identifier_str =
127
+ if tp.defined_class
128
+ minfo(tp)
129
+ else
130
+ "block"
131
+ end
132
+
133
+ call_identifier_str = colorize_blue(call_identifier_str)
134
+
135
+ case tp.event
136
+ when :call, :c_call, :b_call
137
+ depth += 1 if tp.event == :c_call
138
+ out tp, ">#{sp}#{call_identifier_str}", depth
139
+ when :return, :c_return, :b_return
140
+ depth += 1 if tp.event == :c_return
141
+ return_str = colorize_magenta(DEBUGGER__.short_inspect(tp.return_value))
142
+ out tp, "<#{sp}#{call_identifier_str} #=> #{return_str}", depth
143
+ end
144
+ }
145
+ end
146
+
147
+ def skip_with_pattern?(tp)
148
+ super && !tp.method_id&.match?(@pattern)
149
+ end
150
+ end
151
+
152
+ class ExceptionTracer < Tracer
153
+ def setup
154
+ @tracer = TracePoint.new(:raise) do |tp|
155
+ next if skip?(tp)
156
+
157
+ exc = tp.raised_exception
158
+
159
+ out tp, " #{colorize_magenta(exc.inspect)}"
160
+ rescue Exception => e
161
+ p e
162
+ end
163
+ end
164
+
165
+ def skip_with_pattern?(tp)
166
+ super && !tp.raised_exception.inspect.match?(@pattern)
167
+ end
168
+ end
169
+
170
+ class ObjectTracer < Tracer
171
+ def initialize ui, obj_id, obj_inspect, **kw
172
+ @obj_id = obj_id
173
+ @obj_inspect = obj_inspect
174
+ super(ui, **kw)
175
+ end
176
+
177
+ def description
178
+ " for #{@obj_inspect}"
179
+ end
180
+
181
+ def colorized_obj_inspect
182
+ colorize_magenta(@obj_inspect)
183
+ end
184
+
185
+ def setup
186
+ @tracer = TracePoint.new(:a_call){|tp|
187
+ next if skip?(tp)
188
+
189
+ if tp.self.object_id == @obj_id
190
+ klass = tp.defined_class
191
+ method = tp.method_id
192
+ method_info =
193
+ if klass.singleton_class?
194
+ if tp.self.is_a?(Class)
195
+ ".#{method} (#{klass}.#{method})"
196
+ else
197
+ ".#{method}"
198
+ end
199
+ else
200
+ "##{method} (#{klass}##{method})"
201
+ end
202
+
203
+ out tp, " #{colorized_obj_inspect} receives #{colorize_blue(method_info)}"
204
+ else
205
+ b = tp.binding
206
+ method_info = colorize_blue(minfo(tp))
207
+
208
+ tp.parameters.each{|type, name|
209
+ next unless name
210
+
211
+ colorized_name = colorize_cyan(name)
212
+
213
+ case type
214
+ when :req, :opt, :key, :keyreq
215
+ if b.local_variable_get(name).object_id == @obj_id
216
+ out tp, " #{colorized_obj_inspect} is used as a parameter #{colorized_name} of #{method_info}"
217
+ end
218
+ when :rest
219
+ next name == :"*"
220
+
221
+ ary = b.local_variable_get(name)
222
+ ary.each{|e|
223
+ if e.object_id == @obj_id
224
+ out tp, " #{colorized_obj_inspect} is used as a parameter in #{colorized_name} of #{method_info}"
225
+ end
226
+ }
227
+ when :keyrest
228
+ next if name == :'**'
229
+ h = b.local_variable_get(name)
230
+ h.each{|k, e|
231
+ if e.object_id == @obj_id
232
+ out tp, " #{colorized_obj_inspect} is used as a parameter in #{colorized_name} of #{method_info}"
233
+ end
234
+ }
235
+ end
236
+ }
237
+ end
238
+ }
239
+ end
240
+ end
241
+ end
242
+
data/lib/debug/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DEBUGGER__
4
- VERSION = "1.0.0.beta8"
4
+ VERSION = "1.0.0.rc1"
5
5
  end
data/misc/README.md.erb CHANGED
@@ -20,6 +20,7 @@ New debug.rb has several advantages:
20
20
  * Support threads (almost done) and ractors (TODO).
21
21
  * Support suspending and entering to the console debugging with `Ctrl-C` at most of timing.
22
22
  * Show parameters on backtrace command.
23
+ * Support recording & reply debugging.
23
24
 
24
25
  # Installation
25
26
 
@@ -32,7 +33,7 @@ or specify `-Ipath/to/debug/lib` in `RUBYOPT` or each ruby command-line option,
32
33
  If you use Bundler, write the following line to your Gemfile.
33
34
 
34
35
  ```
35
- gem "debug", ">= 1.0.0.beta"
36
+ gem "debug", ">= 1.0.0.rc"
36
37
  ```
37
38
 
38
39
  # HOW TO USE
@@ -43,9 +44,9 @@ To use a debugger, roughly you will do the following steps:
43
44
  2. Run a program with the debugger.
44
45
  3. At the breakpoint, enter the debugger console.
45
46
  4. Use debug commands.
46
- * Query the prgram status (e.g. `p lvar` to see the local variable `lvar`).
47
+ * Query the program status (e.g. `p lvar` to see the local variable `lvar`).
47
48
  * Control program flow (e.g. move to the another line with `step`, to the next line with `next`).
48
- * Set another breakpoints (e.g. `catch Exception` to set the breakpoints when `Exception` is raiesd).
49
+ * Set another breakpoint (e.g. `catch Exception` to set a breakpoint when `Exception` is raised).
49
50
  * Change the configuration (e.g. `config set no_color true` to disable coloring).
50
51
  * Continue the program (`c` or `continue`) and goto 3.
51
52
 
@@ -56,12 +57,12 @@ There are several options for (1) and (2). Please choose your favorite way.
56
57
  ### Modify source code as `binding.pry` and `binding.irb`
57
58
 
58
59
  If you can modify the source code, you can use the debugger by adding `require 'debug'` line at the top of your program and putting `binding.break` method (`binding.b` for short) into lines where you want to stop as breakpoints like `binding.pry` and `binding.irb`.
59
- After that, you run the program as usuall and you will enter the debug console at breakpoints you inserted.
60
+ After that, you run the program as usual and you will enter the debug console at breakpoints you inserted.
60
61
 
61
62
  The following example shows the demonstration of `binding.break`.
62
63
 
63
64
  ```shell
64
- $ cat target.rb # Sample prgram
65
+ $ cat target.rb # Sample program
65
66
  require 'debug'
66
67
 
67
68
  a = 1
@@ -120,13 +121,13 @@ d => 4
120
121
  [1, 2, 3, 4]
121
122
  ```
122
123
 
123
- ### Invoke the prorgam from the debugger as a traditional debuggers
124
+ ### Invoke the program from the debugger as a traditional debuggers
124
125
 
125
126
  If you don't want to modify the source code, you can set breakpoints with a debug command `break` (`b` for short).
126
127
  Using `rdbg` command to launch the program without any modifications, you can run the program with the debugger.
127
128
 
128
129
  ```shell
129
- $ cat target.rb # Sample prgram
130
+ $ cat target.rb # Sample program
130
131
  a = 1
131
132
  b = 2
132
133
  c = 3
@@ -241,16 +242,16 @@ NOTE: If you want to use bundler (`bundle` command), you need to write `gem debu
241
242
 
242
243
  ### Using VSCode
243
244
 
244
- Like other langauges, you can use this debugger on the VSCode.
245
+ Like other languages, you can use this debugger on the VSCode.
245
246
 
246
- 1. Install [VSCode rdbg Ruby Debugger - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg)
247
+ 1. Install [VSCode rdbg Ruby Debugger - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg)
247
248
  2. Open `.rb` file (e.g. `target.rb`)
248
249
  3. Register breakpoints with "Toggle breakpoint" in Run menu (or type F9 key)
249
250
  4. Choose "Start debugging" in "Run" menu (or type F5 key)
250
251
  5. You will see a dialog "Debug command line" and you can choose your favorite command line your want to run.
251
- 6. Chosed command line is invoked with `rdbg -c` and VSCode shows the details at breakponts.
252
+ 6. Chosen command line is invoked with `rdbg -c` and VSCode shows the details at breakpoints.
252
253
 
253
- Plase refer [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging) for operations on VSCode.
254
+ Please refer [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging) for operations on VSCode.
254
255
 
255
256
  You can configure the extension in `.vscode/launch.json`.
256
257
  Please see the extension page for more details.
@@ -265,7 +266,7 @@ You can use this debugger as a remote debugger. For example, it will help the fo
265
266
  * Your application uses pipe for STDIN or STDOUT.
266
267
  * Your application is running as a daemon and you want to query the running status (checking a backtrace and so on).
267
268
 
268
- You can run your application as a remote debuggee and the remote debugger console can attach to the debugee anytime.
269
+ You can run your application as a remote debuggee and the remote debugger console can attach to the debuggee anytime.
269
270
 
270
271
  ### Invoke as a remote debuggee
271
272
 
@@ -279,10 +280,10 @@ You can run a script with `rdbg --open target.rb` command and run a `target.rb`
279
280
  $ exe/rdbg --open target.rb
280
281
  DEBUGGER: Session start (pid: 7773)
281
282
  DEBUGGER: Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-7773)
282
- DEBUGGER: wait for debuger connection...
283
+ DEBUGGER: wait for debugger connection...
283
284
  ```
284
285
 
285
- By deafult, `rdbg --open` uses UNIX domain socket and generates path name automatically (`/home/ko1/.ruby-debug-sock/ruby-debug-ko1-7773` in this case).
286
+ By default, `rdbg --open` uses UNIX domain socket and generates path name automatically (`/home/ko1/.ruby-debug-sock/ruby-debug-ko1-7773` in this case).
286
287
 
287
288
  You can connect to the debuggee with `rdbg --attach` command (`rdbg -A` for short).
288
289
 
@@ -309,7 +310,7 @@ NOTE: If you use `quit` command, only remote console exits and the debuggee prog
309
310
 
310
311
  If you want to use TCP/IP for the remote debugging, you need to specify the port and host with `--port` like `rdbg --open --port 12345` and it binds to `localhost:12345`.
311
312
 
312
- To connect to the debugeee, you need to specify the port.
313
+ To connect to the debuggee, you need to specify the port.
313
314
 
314
315
  ```shell
315
316
  $ rdbg --attach 12345
@@ -324,7 +325,7 @@ If you can modify the program, you can open debugging port by adding `require 'd
324
325
 
325
326
  If you don't want to stop the program at the beginning, you can also use `require 'debug/open_nonstop'`.
326
327
  Using `debug/open_nonstop` is useful if you want to open a backdoor to the application.
327
- However, it is also danger because it can become antoher vulnerability.
328
+ However, it is also danger because it can become another vulnerability.
328
329
  Please use it carefully.
329
330
 
330
331
  By default, UNIX domain socket is used for the debugging port. To use TCP/IP, you can set the `RUBY_DEBUG_PORT` environment variable.
@@ -336,14 +337,14 @@ $ RUBY_DEBUG_PORT=12345 ruby target.rb
336
337
  ## Configuration
337
338
 
338
339
  You can configure the debugger's behavior with debug commands and environment variables.
339
- When the debug session is started, initial scripts are loaded so you can put your favorite configurations in the intial scripts.
340
+ When the debug session is started, initial scripts are loaded so you can put your favorite configurations in the initial scripts.
340
341
 
341
342
  ### Configuration list
342
343
 
343
344
  You can configure debugger's behavior with environment variables and `config` command. Each configuration has environment variable and the name which can be specified by `config` command.
344
345
 
345
346
  ```
346
- # configulation example
347
+ # configuration example
347
348
  config set log_level INFO
348
349
  config set no_color true
349
350
  ```
@@ -355,7 +356,7 @@ config set no_color true
355
356
 
356
357
  ### Initial scripts
357
358
 
358
- If there is `~/.rdbgrc`, the file is loaded as an initial scripts which contains debug commands) when the debug session is started.
359
+ If there is `~/.rdbgrc`, the file is loaded as an initial script (which contains debug commands) when the debug session is started.
359
360
 
360
361
  * `RUBY_DEBUG_INIT_SCRIPT` environment variable can specify the initial script file.
361
362
  * You can specify the initial script with `rdbg -x initial_script` (like gdb's `-x` option).
@@ -395,7 +396,7 @@ You can start debugging without `rdbg` command by requiring the following librar
395
396
  You need to require one of them at the very beginning of the application.
396
397
  Using `ruby -r` (for example `ruby -r debug/start target.rb`) is another way to invoke with debugger.
397
398
 
398
- NOTE: Until Ruby 3.0, there is old `lib/debug.rb` standard library. So that if this gem is not installed, or if `Gemfile` missed to list this gem and `bunde exec` is used, you will see the following output:
399
+ NOTE: Until Ruby 3.0, there is old `lib/debug.rb` standard library. So that if this gem is not installed, or if `Gemfile` missed to list this gem and `bundle exec` is used, you will see the following output:
399
400
 
400
401
  ```shell
401
402
  $ ruby -r debug -e0
@@ -411,7 +412,7 @@ Emacs support available.
411
412
 
412
413
  #### Start by method
413
414
 
414
- After loading `debug/session`, you can start debug session with the following methods. They are convinient if you want to specifies debug configrations in your program.
415
+ After loading `debug/session`, you can start debug session with the following methods. They are convenient if you want to specify debug configurations in your program.
415
416
 
416
417
  * `DEBUGGER__.start(**kw)`: start debug session with local console.
417
418
  * `DEBUGGER__.open(**kw)`: open debug port with configuration (without configurations open with UNIX domain socket)
@@ -436,15 +437,15 @@ If `do: 'command'` is specified, the debugger suspends the program and run the `
436
437
  It is useful if you only want to call a debug command and don't want to stop there.
437
438
 
438
439
  ```
439
- def initialzie
440
+ def initialize
440
441
  @a = 1
441
442
  binding.b do: 'watch @a'
442
443
  end
443
444
  ```
444
445
 
445
- On this case, register a watch breakpont for `@a` and continue to run.
446
+ On this case, register a watch breakpoint for `@a` and continue to run.
446
447
 
447
- If `pre: 'command'` is specified, the debuger suspends the program and run the `command` as a debug command, and keep suspend.
448
+ If `pre: 'command'` is specified, the debugger suspends the program and run the `command` as a debug command, and keep suspend.
448
449
  It is useful if you have operations before suspend.
449
450
 
450
451
  ```
@@ -454,7 +455,7 @@ def foo
454
455
  end
455
456
  ```
456
457
 
457
- On this case, you can see the result of `bar()` everytime when you stops there.
458
+ On this case, you can see the result of `bar()` every time you stop there.
458
459
 
459
460
  ## rdbg command help
460
461
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debug
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta8
4
+ version: 1.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koichi Sasada
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-14 00:00:00.000000000 Z
11
+ date: 2021-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: irb
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: reline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.7
27
41
  description: Debugging functionality for Ruby. This is completely rewritten debug.rb
28
42
  which was contained by the encient Ruby versions.
29
43
  email:
@@ -58,6 +72,7 @@ files:
58
72
  - lib/debug/config.rb
59
73
  - lib/debug/console.rb
60
74
  - lib/debug/frame_info.rb
75
+ - lib/debug/local.rb
61
76
  - lib/debug/open.rb
62
77
  - lib/debug/open_nonstop.rb
63
78
  - lib/debug/server.rb
@@ -66,6 +81,7 @@ files:
66
81
  - lib/debug/source_repository.rb
67
82
  - lib/debug/start.rb
68
83
  - lib/debug/thread_client.rb
84
+ - lib/debug/tracer.rb
69
85
  - lib/debug/version.rb
70
86
  - misc/README.md.erb
71
87
  homepage: https://github.com/ruby/debug