debug 1.0.0.beta4 → 1.0.0.beta8

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.
data/lib/debug/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DEBUGGER__
2
- VERSION = "1.0.0.beta4"
4
+ VERSION = "1.0.0.beta8"
3
5
  end
data/misc/README.md.erb CHANGED
@@ -1,3 +1,5 @@
1
+ [![Ruby](https://github.com/ruby/debug/actions/workflows/ruby.yml/badge.svg?branch=master)](https://github.com/ruby/debug/actions/workflows/ruby.yml?query=branch%3Amaster)
2
+
1
3
  # debug.rb
2
4
 
3
5
  This library provides debugging functionality to Ruby.
@@ -9,8 +11,8 @@ New debug.rb has several advantages:
9
11
  * Remote debugging: Support remote debugging natively.
10
12
  * UNIX domain socket
11
13
  * TCP/IP
12
- * VSCode/DAP integration (TODO)
13
- * Extensible: application can introduce debugging support with several methods
14
+ * VSCode/DAP integration ([VSCode rdbg Ruby Debugger - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg))
15
+ * Extensible: application can introduce debugging support with several ways:
14
16
  * By `rdbg` command
15
17
  * By loading libraries with `-r` command line option
16
18
  * By calling Ruby's method explicitly
@@ -27,283 +29,346 @@ $ gem install debug --pre
27
29
 
28
30
  or specify `-Ipath/to/debug/lib` in `RUBYOPT` or each ruby command-line option, especially for debug this gem development.
29
31
 
30
- # How to use
31
-
32
- ## Invoke with debugger
32
+ If you use Bundler, write the following line to your Gemfile.
33
33
 
34
- You can run ruby program on debugger with the local debug console or the remote debug console.
34
+ ```
35
+ gem "debug", ">= 1.0.0.beta"
36
+ ```
35
37
 
36
- * (a) Run a ruby program with the local debug console
37
- * (b) Run a ruby program with the remote debug console by opening a network port
38
- * (b-1) Open with UNIX domain socket
39
- * (b-2) Open with TCP/IP port
38
+ # HOW TO USE
39
+
40
+ To use a debugger, roughly you will do the following steps:
41
+
42
+ 1. Set breakpoints.
43
+ 2. Run a program with the debugger.
44
+ 3. At the breakpoint, enter the debugger console.
45
+ 4. Use debug commands.
46
+ * Query the prgram status (e.g. `p lvar` to see the local variable `lvar`).
47
+ * 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
+ * Change the configuration (e.g. `config set no_color true` to disable coloring).
50
+ * Continue the program (`c` or `continue`) and goto 3.
51
+
52
+ ## Invoke with the debugger
53
+
54
+ There are several options for (1) and (2). Please choose your favorite way.
55
+
56
+ ### Modify source code as `binding.pry` and `binding.irb`
57
+
58
+ 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
+
61
+ The following example shows the demonstration of `binding.break`.
62
+
63
+ ```shell
64
+ $ cat target.rb # Sample prgram
65
+ require 'debug'
66
+
67
+ a = 1
68
+ b = 2
69
+ binding.break # Program will stop here
70
+ c = 3
71
+ d = 4
72
+ binding.break # Program will stop here
73
+ p [a, b, c, d]
74
+
75
+ $ ruby target.rb # Run the program normally.
76
+ DEBUGGER: Session start (pid: 7604)
77
+ [1, 10] in target.rb
78
+ 1| require 'debug'
79
+ 2|
80
+ 3| a = 1
81
+ 4| b = 2
82
+ => 5| binding.break # Now you can see it stops at this line
83
+ 6| c = 3
84
+ 7| d = 4
85
+ 8| binding.break
86
+ 9| p [a, b, c, d]
87
+ 10|
88
+ =>#0 <main> at target.rb:5
89
+
90
+ (rdbg) info locals # You can show local variables
91
+ =>#0 <main> at target.rb:5
92
+ %self => main
93
+ a => 1
94
+ b => 2
95
+ c => nil
96
+ d => nil
97
+
98
+ (rdbg) continue # Continue the execution
99
+ [3, 11] in target.rb
100
+ 3| a = 1
101
+ 4| b = 2
102
+ 5| binding.break
103
+ 6| c = 3
104
+ 7| d = 4
105
+ => 8| binding.break # Again the program stops at here
106
+ 9| p [a, b, c, d]
107
+ 10|
108
+ 11| __END__
109
+ =>#0 <main> at target.rb:8
110
+
111
+ (rdbg) info locals # And you can see the updated local variables
112
+ =>#0 <main> at target.rb:8
113
+ %self => main
114
+ a => 1
115
+ b => 2
116
+ c => 3
117
+ d => 4
118
+
119
+ (rdbg) continue
120
+ [1, 2, 3, 4]
121
+ ```
40
122
 
41
- (b-1) is useful when you want to use debugging features after running the program.
42
- (b-2) is also useful when you don't have a ssh access for the Ruby process.
123
+ ### Invoke the prorgam from the debugger as a traditional debuggers
43
124
 
44
- To use debugging feature, you can have 3 ways.
125
+ If you don't want to modify the source code, you can set breakpoints with a debug command `break` (`b` for short).
126
+ Using `rdbg` command to launch the program without any modifications, you can run the program with the debugger.
45
127
 
46
- * (1) Use `rdbg` command
47
- * (2) Use `ruby -r debug...` command line option
48
- * (3) Write `require 'debug...'` in .rb files
128
+ ```shell
129
+ $ cat target.rb # Sample prgram
130
+ a = 1
131
+ b = 2
132
+ c = 3
133
+ d = 4
134
+ p [a, b, c, d]
49
135
 
50
- ### Local debug console
136
+ $ rdbg target.rb # run like `ruby target.rb`
137
+ DEBUGGER: Session start (pid: 7656)
138
+ [1, 7] in target.rb
139
+ => 1| a = 1
140
+ 2| b = 2
141
+ 3| c = 3
142
+ 4| d = 4
143
+ 5| p [a, b, c, d]
144
+ 6|
145
+ 7| __END__
146
+ =>#0 <main> at target.rb:1
51
147
 
148
+ (rdbg)
52
149
  ```
53
- # (1) Use `rdbg` command
54
- $ rdbg target.rb
55
- $ rdbg -- -r foo -e expr # -- is required to make clear rdbg options and ruby's options
56
-
57
- # (2) Use `-r debug/run` command line option
58
-
59
- $ ruby -r debug/run target.rb
60
-
61
- # (3) Write `require 'debug...' in .rb files
62
150
 
63
- $ cat target.rb
64
- require 'debug/run' # start the debug console
65
- ... rest of program ...
151
+ `rdbg` command suspends the program at the beginning of the given script (`target.rb` in this case) and you can use debug commands. `(rdbg)` is prompt. Let's set breakpoints on line 3 and line 5 with `break` command (`b` for short).
66
152
 
67
- # or
153
+ ```shell
154
+ (rdbg) break 3 # set breakpoint at line 3
155
+ #0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
68
156
 
69
- $ cat target.rb
70
- require 'debug/session' # introduce the functionality
71
- DEBUGGER__.console # and start the debug console
72
- ... rest of program ...
157
+ (rdbg) b 5 # set breakpoint at line 5
158
+ #1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)
73
159
 
74
- $ ruby target.rb
160
+ (rdbg) break # show all registered breakpoints
161
+ #0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
162
+ #1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)
75
163
  ```
76
164
 
77
- When you run the program with the debug console, you will see the debug console prompt `(rdbg)`.
78
- The debuggee program (`target.rb`) is suspended at the beginning of `target.rb`.
165
+ You can see that two breakpoints are registered. Let's continue the program by `continue` command.
79
166
 
80
- You can type any debugger's command described bellow. "c" or "continue" resume the debuggee program.
81
- You can suspend the debuggee program and show the debug console with `Ctrl-C`.
167
+ ```shell
168
+ (rdbg) continue
169
+ [1, 7] in target.rb
170
+ 1| a = 1
171
+ 2| b = 2
172
+ => 3| c = 3
173
+ 4| d = 4
174
+ 5| p [a, b, c, d]
175
+ 6|
176
+ 7| __END__
177
+ =>#0 <main> at target.rb:3
82
178
 
83
- The following example shows simple usage of the debug console. You can show the all variables
179
+ Stop by #0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
84
180
 
181
+ (rdbg)
85
182
  ```
86
- $ rdbg ~/src/rb/target.rb
87
183
 
88
- [1, 5] in /home/ko1/src/rb/target.rb
89
- => 1| a = 1
184
+ You can see that we can stop at line 3.
185
+ Let's see the local variables with `info` command, and continue.
186
+ You can also confirm that the program will suspend at line 5 and you can use `info` command again.
187
+
188
+ ```shell
189
+ (rdbg) info
190
+ =>#0 <main> at target.rb:3
191
+ %self => main
192
+ a => 1
193
+ b => 2
194
+ c => nil
195
+ d => nil
196
+
197
+ (rdbg) continue
198
+ [1, 7] in target.rb
199
+ 1| a = 1
90
200
  2| b = 2
91
201
  3| c = 3
92
- 4| p [a + b + c]
93
- 5|
94
- --> #0 /home/ko1/src/rb/target.rb:1:in `<main>'
202
+ 4| d = 4
203
+ => 5| p [a, b, c, d]
204
+ 6|
205
+ 7| __END__
206
+ =>#0 <main> at target.rb:5
207
+
208
+ Stop by #1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)
209
+
210
+ (rdbg) info
211
+ =>#0 <main> at target.rb:5
212
+ %self => main
213
+ a => 1
214
+ b => 2
215
+ c => 3
216
+ d => 4
217
+
218
+ (rdbg) continue
219
+ [1, 2, 3, 4]
220
+ ```
95
221
 
96
- (rdbg) info # Show all local variables
97
- %self => main
98
- a => nil
99
- b => nil
100
- c => nil
222
+ By the way, using `rdbg` command you can suspend your application with `C-c` (SIGINT) and enter the debug console.
223
+ It will help that if you want to know what the program is doing.
101
224
 
102
- (rdbg) p a # Same as p(a)
103
- => nil
225
+ ### Use `rdbg` with commands written in Ruby
104
226
 
105
- (rdbg) s # Step in ("s" is a short name of "step")
227
+ If you want to run a command written in Ruby like like `rake`, `rails`, `bundle`, `rspec` and so on, you can use `rdbg -c` option.
106
228
 
107
- [1, 5] in /home/ko1/src/rb/target.rb
108
- 1| a = 1
109
- => 2| b = 2
110
- 3| c = 3
111
- 4| p [a + b + c]
112
- 5|
113
- --> #0 /home/ko1/src/rb/target.rb:2:in `<main>'
229
+ * Without `-c` option, `rdbg <name>` means that `<name>` is Ruby script and invoke it like `ruby <name>` with the debugger.
230
+ * With `-c` option, `rdbg -c <name>` means that `<name>` is command in `PATH` and simply invoke it with the debugger.
114
231
 
115
- (rdbg) <Enter> # Repeat the last command ("step")
116
-
117
- [1, 5] in /home/ko1/src/rb/target.rb
118
- 1| a = 1
119
- 2| b = 2
120
- => 3| c = 3
121
- 4| p [a + b + c]
122
- 5|
123
- --> #0 /home/ko1/src/rb/target.rb:3:in `<main>'
232
+ Examples:
233
+ * `rdbg -c -- rails server`
234
+ * `rdbg -c -- bundle exec ruby foo.rb`
235
+ * `rdbg -c -- bundle exec rake test`
236
+ * `rdbg -c -- ruby target.rb` is same as `rdbg target.rb`
124
237
 
125
- (rdbg) # Repeat the last command ("step")
238
+ NOTE: `--` is needed to separate the command line options for `rdbg` and invoking command. For example, `rdbg -c rake -T` is recognized like `rdbg -c -T -- rake`. It should be `rdbg -c -- rake -T`.
126
239
 
127
- [1, 5] in /home/ko1/src/rb/target.rb
128
- 1| a = 1
129
- 2| b = 2
130
- 3| c = 3
131
- => 4| p [a + b + c]
132
- 5|
133
- --> #0 /home/ko1/src/rb/target.rb:4:in `<main>'
134
-
135
- (rdbg) info # Show all local variables
136
- %self => main
137
- a => 1
138
- b => 2
139
- c => 3
140
-
141
- (rdbg) c # Continue the program ("c" is a short name of "continue")
142
- [6]
143
- ```
240
+ NOTE: If you want to use bundler (`bundle` command), you need to write `gem debug` line in your `Gemfile`.
144
241
 
145
- ### Remote debug (1) UNIX domain socket
242
+ ### Using VSCode
146
243
 
147
- ```
148
- # (1) Use `rdbg` command
149
- $ rdbg --open target.rb # or rdbg -O target.rb for shorthand
150
- Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-5042)
151
- ...
244
+ Like other langauges, you can use this debugger on the VSCode.
152
245
 
153
- # (2) Use `-r debug/open` command line option
246
+ 1. Install [VSCode rdbg Ruby Debugger - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg)
247
+ 2. Open `.rb` file (e.g. `target.rb`)
248
+ 3. Register breakpoints with "Toggle breakpoint" in Run menu (or type F9 key)
249
+ 4. Choose "Start debugging" in "Run" menu (or type F5 key)
250
+ 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.
154
252
 
155
- $ ruby -r debug/open target.rb
156
- Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-5042)
157
- ...
253
+ Plase refer [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging) for operations on VSCode.
158
254
 
159
- # (3) Write `require 'debug/open' in .rb files
160
- $ cat target.rb
161
- require 'debug/open' # open the debugger entry point by UNIX domain socket.
162
- ...
255
+ You can configure the extension in `.vscode/launch.json`.
256
+ Please see the extension page for more details.
163
257
 
164
- # or
258
+ ## Remote debugging
165
259
 
166
- $ cat target.rb
167
- require 'debug/server' # introduce remote debugging feature
168
- DEBUGGER__.open # open the debugger entry point by UNIX domain socket.
169
- # or DEBUGGER__.open_unix to specify UNIX domain socket.
260
+ You can use this debugger as a remote debugger. For example, it will help the following situations:
170
261
 
171
- $ ruby target.rb
172
- Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-5042)
173
- ...
174
- ```
262
+ * Your application does not run on TTY and it is hard to use `binding.pry` or `binding.irb`.
263
+ * Your application is running on Docker container and there is no TTY.
264
+ * Your application is running as a daemon.
265
+ * Your application uses pipe for STDIN or STDOUT.
266
+ * Your application is running as a daemon and you want to query the running status (checking a backtrace and so on).
175
267
 
176
- It runs target.rb and accept debugger connection within UNIX domain socket.
177
- The debuggee process waits for debugger connection at the beginning of `target.rb` like that:
268
+ You can run your application as a remote debuggee and the remote debugger console can attach to the debugee anytime.
178
269
 
179
- ```
180
- $ rdbg -O ~/src/rb/target.rb
181
- DEBUGGER: Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-29828)
182
- DEBUGGER: wait for debugger connection...
183
- ```
270
+ ### Invoke as a remote debuggee
184
271
 
185
- You can attach the program with the following command:
272
+ There are two ways to invoke a script as remote debuggee: Use `rdbg --open` and require `debug/open` (or `debug/open_nonstop`).
186
273
 
187
- ```
188
- $ rdbg --attach # or rdbg -A for shorthand
189
-
190
- [1, 4] in /home/ko1/src/rb/target.rb
191
- 1| (1..).each do |i|
192
- => 2| sleep 0.5
193
- 3| p i
194
- 4| end
195
- --> #0 [C] /home/ko1/src/rb/target.rb:2:in `sleep'
196
- #1 /home/ko1/src/rb/target.rb:2:in `block in <main>' {|i=17|}
197
- #2 [C] /home/ko1/src/rb/target.rb:1:in `each'
198
- # and 1 frames (use `bt' command for all frames)
199
-
200
- (rdb)
201
- ```
274
+ #### `rdbg --open` (or `rdbg -O` for short)
202
275
 
203
- and you can input any debug commands. `c` (or `continue`) continues the debuggee process.
276
+ You can run a script with `rdbg --open target.rb` command and run a `target.rb` as a debuggee program. It also opens the network port and suspends at the beginning of `target.rb`.
204
277
 
205
- You can detach the debugger from the debugger process with `quit` command.
206
- You can re-connect to the debuggee process by `rdbg -A` command again, and the debuggee process suspends the execution (and debugger can input any debug commands).
278
+ ```shell
279
+ $ exe/rdbg --open target.rb
280
+ DEBUGGER: Session start (pid: 7773)
281
+ DEBUGGER: Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-7773)
282
+ DEBUGGER: wait for debuger connection...
283
+ ```
207
284
 
208
- If you don't want to stop the debuggee process at the beginning of debuggee process (`target.rb`), you can use the following to specify "non-stop" option.
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).
209
286
 
210
- * Use `rdbg -n` option
211
- * Set the environment variable `RUBY_DEBUG_NONSTOP=1`
287
+ You can connect to the debuggee with `rdbg --attach` command (`rdbg -A` for short).
212
288
 
213
- If you are running multiple debuggee processes, the attach command (`rdbg -A`) shows the options like that:
289
+ ```shell
290
+ $ rdbg -A
291
+ [1, 7] in target.rb
292
+ => 1| a = 1
293
+ 2| b = 2
294
+ 3| c = 3
295
+ 4| d = 4
296
+ 5| p [a, b, c, d]
297
+ 6|
298
+ 7| __END__
299
+ =>#0 <main> at target.rb:1
214
300
 
215
- ```
216
- $ rdbg --attach
217
- Please select a debug session:
218
- ruby-debug-ko1-19638
219
- ruby-debug-ko1-19603
301
+ (rdbg:remote)
220
302
  ```
221
303
 
222
- and you need to specify one (copy and paste the name):
304
+ If there is no other opening ports on the default directory, `rdbg --attach` command chooses the only one opening UNIX domain socket and connect to it. If there are more files, you need to specify the file.
223
305
 
224
- ```
225
- $ rdbg --attach ruby-debug-ko1-19638
226
- ```
306
+ When `rdbg --attach` connects to the debuggee, you can use any debug commands (set breakpoints, continue the program and so on) like local debug console. When an debuggee program exits, the remote console will also terminate.
227
307
 
228
- The socket file is located at
229
- * `RUBY_DEBUG_SOCK_DIR` environment variable if available.
230
- * `XDG_RUNTIME_DIR` environment variable if available.
231
- * `$HOME/.ruby-debug-sock` if `$HOME` is available.
308
+ NOTE: If you use `quit` command, only remote console exits and the debuggee program continues to run (and you can connect it again). If you want to exit the debuggee program, use `kill` command.
232
309
 
233
- ### Remote debug (2) TCP/IP
310
+ 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`.
234
311
 
235
- You can open the TCP/IP port instead of using UNIX domain socket.
312
+ To connect to the debugeee, you need to specify the port.
236
313
 
314
+ ```shell
315
+ $ rdbg --attach 12345
237
316
  ```
238
- # (1) Use `rdbg` command
239
- $ rdbg -O --port=12345 target.rb
240
- # or
241
- $ rdbg --open --port=12345 target.rb
242
- Debugger can attach via TCP/IP (localhost:12345)
243
- ...
244
317
 
245
- # (2) Use `-r debug/open` command line option
318
+ If you want to choose the host to bind, you can use `--host` option.
319
+ Note that all messages communicated between the debugger and the debuggee are *NOT* encrypted so please use remote debugging carefully.
246
320
 
247
- $ RUBY_DEBUG_PORT=12345 ruby -r debug/open target.rb
248
- Debugger can attach via TCP/IP (localhost:12345)
249
- ...
321
+ #### `require 'debug/open'` in a program
250
322
 
251
- # (3) Write `require 'debug/open' in .rb files
252
- $ cat target.rb
253
- require 'debug/open' # open the debugger entry point.
254
- ...
323
+ If you can modify the program, you can open debugging port by adding `require 'debug/open'` line in the program.
255
324
 
256
- # and run with environment variable RUBY_DEBUG_PORT
257
- $ RUBY_DEBUG_PORT=12345 ruby target.rb
258
- Debugger can attach via TCP/IP (localhost:12345)
259
- ...
325
+ If you don't want to stop the program at the beginning, you can also use `require 'debug/open_nonstop'`.
326
+ 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
+ Please use it carefully.
260
329
 
261
- # or
330
+ By default, UNIX domain socket is used for the debugging port. To use TCP/IP, you can set the `RUBY_DEBUG_PORT` environment variable.
262
331
 
263
- $ cat target.rb
264
- require 'debug/server' # introduce remote debugging feature
265
- DEBUGGER__.open(port: 12345)
266
- # or DEBUGGER__.open_tcp(port: 12345)
267
-
268
- $ ruby target.rb
269
- Debugger can attach via TCP/IP (localhost:12345)
270
- ...
332
+ ```shell
333
+ $ RUBY_DEBUG_PORT=12345 ruby target.rb
271
334
  ```
272
335
 
273
- You can also specify the host with the `RUBY_DEBUG_HOST` environment variable. And also `DEBUGGER__.open` method accepts a `host:` keyword parameter. If the host is not given, `localhost` will be used.
336
+ ## Configuration
337
+
338
+ 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.
274
340
 
275
- To attach the debuggee process, specify the port number (and hostname if needed) for the `rdbg --attach` (or `rdbg -A`) command.
341
+ ### Configuration list
342
+
343
+ 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.
276
344
 
277
345
  ```
278
- $ rdbg --attach 12345
279
- $ rdbg --attach hostname 12345
346
+ # configulation example
347
+ config set log_level INFO
348
+ config set no_color true
280
349
  ```
281
350
 
282
- ### Initial scripts
351
+ <% cat = nil; DEBUGGER__::CONFIG_SET.each do |key, (env, desc)| %>
352
+ <% /\A(\w+): (.+)/ =~ desc; if cat != $1; cat = 1 %>
353
+ * <%= $1 %>
354
+ <% cat = $1; end %> * `<%= env %>` (`<%= key %>`): <%= $2 %><% end %>
283
355
 
284
- If there are `.rdbgrc` files are there at the current directory and the home directory, files are loaded as initial scripts which contains debugger commands. `RUBY_DEBUG_INIT_SCRIPT` environment variable can specify the initial script file.
285
-
286
- Initial scripts are evaluated at the first suspend timing (generally, it is the beginning of the target script). For example, you can set break points with `break file:123`.
356
+ ### Initial scripts
287
357
 
288
- If there are `.rdbgrc.rb` files at the current directory and the home directory, files are loaded as a ruby script at the initializing timing.
358
+ If there is `~/.rdbgrc`, the file is loaded as an initial scripts which contains debug commands) when the debug session is started.
289
359
 
290
- ### Environment variables
360
+ * `RUBY_DEBUG_INIT_SCRIPT` environment variable can specify the initial script file.
361
+ * You can specify the initial script with `rdbg -x initial_script` (like gdb's `-x` option).
291
362
 
292
- You can control debuggee's behavior with environment variables:
363
+ Initial scripts are useful to write your favorite configurations.
364
+ For example, you can set break points with `break file:123` in `~/.rdbgrc`.
293
365
 
294
- * `RUBY_DEBUG_NONSTOP`: 1 for nonstop at the beginning of program.
295
- * `RUBY_DEBUG_INIT_SCRIPT`: Initial script path loaded at the first stop.
296
- * `RUBY_DEBUG_COMMANDS`: Debug commands invoked at the first stop. Commands should be separated by ';;'.
297
- * `RUBY_DEBUG_SHOW_SRC_LINES`: Show n lines source code on breakpoint (default: 10 lines).
298
- * `RUBY_DEBUG_SHOW_FRAMES`: Show n frames on breakpoint (default: 2 frames).
299
- * Remote debugging
300
- * `RUBY_DEBUG_PORT`: TCP/IP remote debugging: port to open.
301
- * `RUBY_DEBUG_HOST`: TCP/IP remote debugging: host (localhost if not given) to open.
302
- * `RUBY_DEBUG_SOCK_PATH`: UNIX Domain Socket remote debugging: socket path to open.
303
- * `RUBY_DEBUG_SOCK_DIR`: UNIX Domain Socket remote debugging: socket directory to open.
366
+ If there are `~/.rdbgrc.rb` is available, it is also loaded as a ruby script at same timing.
304
367
 
305
368
  ## Debug command on the debug console
306
369
 
370
+ On the debug console, you can use the following debug commands.
371
+
307
372
  * `Enter` repeats the last command (useful when repeating `step`s).
308
373
  * `Ctrl-D` is equal to `quit` command.
309
374
  * [debug command compare sheet - Google Sheets](https://docs.google.com/spreadsheets/d/1TlmmUDsvwK4sSIyoMv-io52BUUz__R5wpu-ComXlsw0/edit?usp=sharing)
@@ -314,6 +379,83 @@ The `<...>` notation means the argument.
314
379
 
315
380
  <%= DEBUGGER__.help %>
316
381
 
382
+ ## Debugger API
383
+
384
+ ### Start debugging
385
+
386
+ #### Start by requiring a library
387
+
388
+ You can start debugging without `rdbg` command by requiring the following libraries:
389
+
390
+ * `require 'debug'`: Same as `rdbg --nonstop --no-sigint-hook`.
391
+ * `require 'debug/start'`: Same as `rdbg`.
392
+ * `require 'debug/open'`: Same as `rdbg --open`.
393
+ * `require 'debug/open_nonstop'`: Same as `rdbg --open --nonstop`.
394
+
395
+ You need to require one of them at the very beginning of the application.
396
+ Using `ruby -r` (for example `ruby -r debug/start target.rb`) is another way to invoke with debugger.
397
+
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
+
400
+ ```shell
401
+ $ ruby -r debug -e0
402
+ .../2.7.3/lib/ruby/2.7.0/x86_64-linux/continuation.so: warning: callcc is obsolete; use Fiber instead
403
+ Debug.rb
404
+ Emacs support available.
405
+
406
+ .../2.7.3/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:162: if RUBYGEMS_ACTIVATION_MONITOR.respond_to?(:mon_owned?)
407
+ (rdb:1)
408
+ ```
409
+
410
+ `lib/debug.rb` was not maintained well in recent years, and the purpose of this library is to rewrite old `lib/debug.rb` with recent techniques.
411
+
412
+ #### Start by method
413
+
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
+
416
+ * `DEBUGGER__.start(**kw)`: start debug session with local console.
417
+ * `DEBUGGER__.open(**kw)`: open debug port with configuration (without configurations open with UNIX domain socket)
418
+ * `DEBUGGER__.open_unix(**kw)`: open debug port with UNIX domain socket
419
+ * `DEBUGGER__.open_tcp(**kw)`: open debug port with TCP/IP
420
+
421
+ For example:
422
+
423
+ ```ruby
424
+ require 'debug/session'
425
+ DEBUGGER__.start(no_color: true, # disable colorize
426
+ log_level: 'INFO') # Change log_level to INFO
427
+
428
+ ... # your application code
429
+ ```
430
+
431
+ ### `binding.break` method
432
+
433
+ `binding.break` (or `binding.b`) set breakpoints at written line. It also has several keywords.
434
+
435
+ If `do: 'command'` is specified, the debugger suspends the program and run the `command` as a debug command and continue the program.
436
+ It is useful if you only want to call a debug command and don't want to stop there.
437
+
438
+ ```
439
+ def initialzie
440
+ @a = 1
441
+ binding.b do: 'watch @a'
442
+ end
443
+ ```
444
+
445
+ On this case, register a watch breakpont for `@a` and continue to run.
446
+
447
+ If `pre: 'command'` is specified, the debuger suspends the program and run the `command` as a debug command, and keep suspend.
448
+ It is useful if you have operations before suspend.
449
+
450
+ ```
451
+ def foo
452
+ binding.b pre: 'p bar()'
453
+ ...
454
+ end
455
+ ```
456
+
457
+ On this case, you can see the result of `bar()` everytime when you stops there.
458
+
317
459
  ## rdbg command help
318
460
 
319
461
  ```
@@ -323,5 +465,10 @@ The `<...>` notation means the argument.
323
465
  # Contributing
324
466
 
325
467
  Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/debug.
468
+ This debugger is not mature so your feedback will help us.
326
469
 
327
470
  Please also check the [contributing guideline](/CONTRIBUTING.md).
471
+
472
+ # Acknowledgement
473
+
474
+ * Some tests are based on [deivid-rodriguez/byebug: Debugging in Ruby 2](https://github.com/deivid-rodriguez/byebug)