ed-precompiled_debug 1.11.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,660 @@
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) [![Protocol](https://github.com/ruby/debug/actions/workflows/protocol.yml/badge.svg)](https://github.com/ruby/debug/actions/workflows/protocol.yml)
2
+
3
+ # debug.rb
4
+
5
+ This library provides debugging functionality to Ruby (MRI) 2.7 and later.
6
+
7
+ This debug.rb is the replacement of traditional lib/debug.rb standard library, which is implemented by `set_trace_func`.
8
+ New debug.rb has several advantages:
9
+
10
+ * Fast: No performance penalty on non-stepping mode and non-breakpoints.
11
+ * [Remote debugging](#remote-debugging): Support remote debugging natively.
12
+ * UNIX domain socket (UDS)
13
+ * TCP/IP
14
+ * Integration with rich debugger frontends
15
+
16
+ | Frontend | [Console](https://github.com/ruby/debug#invoke-as-a-remote-debuggee) | [VSCode](https://github.com/ruby/debug#vscode-integration) | [Chrome DevTool](#chrome-devtool-integration) |
17
+ | ----------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | --------------------------------------------- |
18
+ | Connection | UDS, TCP/IP | UDS, TCP/IP | TCP/IP |
19
+ | Requirement | None | [vscode-rdbg](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg) | Chrome |
20
+
21
+ * Extensible: application can introduce debugging support in several ways:
22
+ * By `rdbg` command
23
+ * By loading libraries with `-r` command line option
24
+ * By calling Ruby's method explicitly
25
+ * Misc
26
+ * Support threads (almost done) and Ractors (TODO).
27
+ * Support suspending and entering the debug console with `Ctrl-C` at most of timing.
28
+ * Show parameters on backtrace command.
29
+ * Support recording and replay debugging.
30
+
31
+ # Installation
32
+
33
+ ```console
34
+ $ gem install debug
35
+ ```
36
+
37
+ Alternatively, specify `-Ipath/to/debug/lib` in `RUBYOPT`, or as a Ruby command-line option
38
+ This is especially useful for debugging this gem during development.
39
+
40
+ If using Bundler, add the following to your Gemfile:
41
+
42
+ ```ruby
43
+ gem "debug", ">= 1.0.0"
44
+ ```
45
+
46
+ (The version constraint is important; `debug < 1.0.0` is an older, abandoned gem that is completely different from this product.)
47
+
48
+ # HOW TO USE
49
+
50
+ To use the debugger, you will do roughly the following steps:
51
+
52
+ 1. Set breakpoints.
53
+ 2. Run a program with the debugger.
54
+ 3. At the breakpoint, enter the debugger console.
55
+ 4. Use debug commands.
56
+ * [Evaluate Ruby expressions](#evaluate) (e.g. `p lvar` to see the local variable `lvar`).
57
+ * [Query the program status](#information) (e.g. `info` to see information about the current frame).
58
+ * [Control program flow](#control-flow) (e.g. move to another line with `step`, to the next line with `next`).
59
+ * [Set another breakpoint](#breakpoint) (e.g. `catch Exception` to set a breakpoint that'll be triggered when `Exception` is raised).
60
+ * [Activate tracing in your program](#trace) (e.g. `trace call` to trace method calls).
61
+ * [Change the configuration](#configuration-1) (e.g. `config set no_color true` to disable coloring).
62
+ * Continue the program (`c` or `continue`) and goto 3.
63
+
64
+ ## Invoke with the debugger
65
+
66
+ There are several ways to invoke the debugger, depending on your needs, preferences, and the environment.
67
+
68
+ ### Modify source code with [`binding.break`](#bindingbreak-method) (similar to `binding.pry` or `binding.irb`)
69
+
70
+ If you can modify the source code, you can use the debugger by adding `require 'debug'` at the top of your program and putting [`binding.break`](#bindingbreak-method) method into lines where you want to stop as breakpoints.
71
+ This is similar to how `binding.pry` and `binding.irb` work.
72
+
73
+ `binding.break` has two aliases which do the same thing:
74
+
75
+ - `binding.b`
76
+ - `debugger`
77
+
78
+ After that, run the program as usual and you will enter the debug console at breakpoints you inserted.
79
+
80
+ The following is an example of the [`binding.break`](#bindingbreak-method) method.
81
+
82
+ ```console
83
+ $ cat target.rb # Sample program
84
+ require 'debug'
85
+
86
+ a = 1
87
+ b = 2
88
+ binding.break # Program will stop here
89
+ c = 3
90
+ d = 4
91
+ binding.break # Program will stop here
92
+ p [a, b, c, d]
93
+
94
+ $ ruby target.rb # Run the program normally.
95
+ DEBUGGER: Session start (pid: 7604)
96
+ [1, 10] in target.rb
97
+ 1| require 'debug'
98
+ 2|
99
+ 3| a = 1
100
+ 4| b = 2
101
+ => 5| binding.break # Now you can see it stops at this line
102
+ 6| c = 3
103
+ 7| d = 4
104
+ 8| binding.break
105
+ 9| p [a, b, c, d]
106
+ 10|
107
+ =>#0 <main> at target.rb:5
108
+
109
+ (rdbg) info locals # You can show local variables
110
+ =>#0 <main> at target.rb:5
111
+ %self => main
112
+ a => 1
113
+ b => 2
114
+ c => nil
115
+ d => nil
116
+
117
+ (rdbg) continue # Continue the execution
118
+ [3, 11] in target.rb
119
+ 3| a = 1
120
+ 4| b = 2
121
+ 5| binding.break
122
+ 6| c = 3
123
+ 7| d = 4
124
+ => 8| binding.break # Again the program stops here
125
+ 9| p [a, b, c, d]
126
+ 10|
127
+ 11| __END__
128
+ =>#0 <main> at target.rb:8
129
+
130
+ (rdbg) info locals # And you can see the updated local variables
131
+ =>#0 <main> at target.rb:8
132
+ %self => main
133
+ a => 1
134
+ b => 2
135
+ c => 3
136
+ d => 4
137
+
138
+ (rdbg) continue
139
+ [1, 2, 3, 4]
140
+ ```
141
+
142
+ ### Invoke the program from the debugger as a traditional debuggers
143
+
144
+ If you don't want to modify the source code, you can use the `rdbg` command (or `bundle exec rdbg`) to run the program with the debugger.
145
+ This is similar to how you'd launch a program with `ruby program.rb`.
146
+ Then you can set breakpoints with the debug command `break` (`b` for short).
147
+
148
+ ```console
149
+ $ cat target.rb # Sample program
150
+ a = 1
151
+ b = 2
152
+ c = 3
153
+ d = 4
154
+ p [a, b, c, d]
155
+
156
+ $ rdbg target.rb # run like `ruby target.rb`
157
+ DEBUGGER: Session start (pid: 7656)
158
+ [1, 7] in target.rb
159
+ => 1| a = 1
160
+ 2| b = 2
161
+ 3| c = 3
162
+ 4| d = 4
163
+ 5| p [a, b, c, d]
164
+ 6|
165
+ 7| __END__
166
+ =>#0 <main> at target.rb:1
167
+
168
+ (rdbg)
169
+ ```
170
+
171
+ The `rdbg` command suspends the program at the beginning of the given script (`target.rb` in this case) and you can use debug commands to control execution from there.
172
+ `(rdbg)` is the console prompt.
173
+ Let's set breakpoints on line 3 and line 5 with `break` command (`b` for short).
174
+
175
+ ```console
176
+ (rdbg) break 3 # set breakpoint at line 3
177
+ #0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
178
+
179
+ (rdbg) b 5 # set breakpoint at line 5
180
+ #1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)
181
+
182
+ (rdbg) break # show all registered breakpoints
183
+ #0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
184
+ #1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)
185
+ ```
186
+
187
+ You can see that two breakpoints are registered.
188
+ Let's continue the program by using the `continue` command.
189
+
190
+ ```console
191
+ (rdbg) continue
192
+ [1, 7] in target.rb
193
+ 1| a = 1
194
+ 2| b = 2
195
+ => 3| c = 3
196
+ 4| d = 4
197
+ 5| p [a, b, c, d]
198
+ 6|
199
+ 7| __END__
200
+ =>#0 <main> at target.rb:3
201
+
202
+ Stop by #0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
203
+
204
+ (rdbg)
205
+ ```
206
+
207
+ You can see that we stopped at line 3.
208
+ Let's see the local variables with the `info` command, and continue execution with the `continue`.
209
+ The program will then suspend at line 5 and you can use the `info` command again.
210
+
211
+ ```console
212
+ (rdbg) info
213
+ =>#0 <main> at target.rb:3
214
+ %self => main
215
+ a => 1
216
+ b => 2
217
+ c => nil
218
+ d => nil
219
+
220
+ (rdbg) continue
221
+ [1, 7] in target.rb
222
+ 1| a = 1
223
+ 2| b = 2
224
+ 3| c = 3
225
+ 4| d = 4
226
+ => 5| p [a, b, c, d]
227
+ 6|
228
+ 7| __END__
229
+ =>#0 <main> at target.rb:5
230
+
231
+ Stop by #1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)
232
+
233
+ (rdbg) info
234
+ =>#0 <main> at target.rb:5
235
+ %self => main
236
+ a => 1
237
+ b => 2
238
+ c => 3
239
+ d => 4
240
+
241
+ (rdbg) continue
242
+ [1, 2, 3, 4]
243
+ ```
244
+
245
+ NOTE: When using `rdbg` you can suspend your application with `C-c` (SIGINT) and enter the debug console.
246
+ It will help if you want to know what the program is doing.
247
+
248
+ ### Use `rdbg` with commands written in Ruby
249
+
250
+ If you want to run an executable written in Ruby like `rake`, `rails`, `bundle`, `rspec`, and so on, you can use `rdbg -c` option.
251
+
252
+ * Without the `-c` option, `rdbg <name>` means that `<name>` is a Ruby script and it is invoked like `ruby <name>` with the debugger.
253
+ * With the `-c` option, `rdbg -c <name>` means that `<name>` is an executable in `PATH` and simply invokes it with the debugger.
254
+
255
+ Examples:
256
+
257
+ * `rdbg -c -- rails server`
258
+ * `rdbg -c -- bundle exec ruby foo.rb`
259
+ * `rdbg -c -- bundle exec rake test`
260
+ * `rdbg -c -- ruby target.rb` is the same as `rdbg target.rb`
261
+
262
+ NOTE: `--` is needed to separate the command line options for `rdbg` from the executable being invoked, and its options.
263
+ For example, `rdbg -c rake -T` would be parsed as `rdbg -c -T -- rake`, which is incorrect.
264
+ It should be `rdbg -c -- rake -T`.
265
+
266
+ NOTE: If you want to use Bundler (`bundle` executable), you need to add `gem 'debug'` to your `Gemfile`.
267
+
268
+ ### Using VSCode
269
+
270
+ Like other languages, you can use this debugger on the VSCode.
271
+
272
+ 1. Install [VSCode rdbg Ruby Debugger - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg)
273
+ 2. Open `.rb` file (e.g. `target.rb`)
274
+ 3. Register breakpoints with "Toggle breakpoint" in the Run menu (or type F9 key)
275
+ 4. Choose "Start debugging" in "Run" menu (or type F5 key)
276
+ 5. You will see a dialog "Debug command line" and you can choose your favorite command line you want to run.
277
+ 6. Chosen command line is invoked with `rdbg -c`, and VSCode shows the details at breakpoints.
278
+
279
+ Please refer to [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging) for operations on VSCode.
280
+
281
+ You can configure the extension in `.vscode/launch.json`.
282
+ Please see the extension page for more details.
283
+
284
+ ## Remote debugging
285
+
286
+ You can use this debugger as a remote debugger.
287
+ For example, it will help in the following situations:
288
+
289
+ * Your application does not run on TTY, and it is hard to use `binding.pry` or `binding.irb`.
290
+ * Your application is running on a Docker container, and there is no TTY.
291
+ * Your application is running as a daemon.
292
+ * Your application uses pipe for `STDIN` or `STDOUT`.
293
+ * Your application is running as a daemon and you want to query the running status (checking a backtrace and so on).
294
+
295
+ You can run your application as a remote debuggee, and the remote debugger console can attach to the debuggee anytime.
296
+
297
+ ### Invoke as a remote debuggee
298
+
299
+ There are multiple ways to run your program as a debuggee:
300
+
301
+ | Stop at program start | [`rdbg` option](https://github.com/ruby/debug#rdbg---open-or-rdbg--o-for-short) | [require](https://github.com/ruby/debug#require-debugopen-in-a-program) | [debugger API](https://github.com/ruby/debug#start-by-method) |
302
+ | --------------------- | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------- |
303
+ | Yes | `rdbg --open` | `require "debug/open"` | `DEBUGGER__.open` |
304
+ | No | `rdbg --open --nonstop` | `require "debug/open_nonstop"` | `DEBUGGER__.open(nonstop: true)` |
305
+
306
+ #### `rdbg --open` (or `rdbg -O` for short)
307
+
308
+ Launch a script with `rdbg --open target.rb` to run `target.rb` as a debuggee program.
309
+ It also opens the network port and suspends at the beginning of `target.rb`.
310
+
311
+ ```console
312
+ $ rdbg --open target.rb
313
+ DEBUGGER: Session start (pid: 7773)
314
+ DEBUGGER: Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-7773)
315
+ DEBUGGER: wait for debugger connection...
316
+ ```
317
+
318
+ By default, `rdbg --open` uses UNIX domain socket and generates the path name automatically (`/home/ko1/.ruby-debug-sock/ruby-debug-ko1-7773` in this case).
319
+
320
+ You can connect to the debuggee with `rdbg --attach` command (`rdbg -A` for short).
321
+
322
+ ```console
323
+ $ rdbg -A
324
+ [1, 7] in target.rb
325
+ => 1| a = 1
326
+ 2| b = 2
327
+ 3| c = 3
328
+ 4| d = 4
329
+ 5| p [a, b, c, d]
330
+ 6|
331
+ 7| __END__
332
+ =>#0 <main> at target.rb:1
333
+
334
+ (rdbg:remote)
335
+ ```
336
+
337
+ If there are no other files in the default directory, `rdbg --attach` will automatically connect to the UNIX domain socket listed.
338
+ If there are multiple files, you need to specify which to use.
339
+
340
+ When `rdbg --attach` connects to the debuggee, you can use any debug commands (set breakpoints, continue the program, and so on) like the local debug console.
341
+ When a debuggee program exits, the remote console will also terminate.
342
+
343
+ NOTE: If you use the `quit` command, only the remote console exits and the debuggee program continues to run (and you can connect it again).
344
+ If you want to exit the debuggee program, use `kill` command.
345
+
346
+ If you want to use TCP/IP for remote debugging, you need to specify the port and host with `--port` like `rdbg --open --port 12345` and it binds to `localhost:12345`.
347
+ You can add an optional `--port_range` to try multiple ports in a reliable way.
348
+ For example, `rdbg --open --port 12345 --port_range 10` will try to bind to 12345, 12346, 12347,… until it finds an available port.
349
+
350
+ To connect to the debuggee, you need to specify the port.
351
+
352
+ ```console
353
+ $ rdbg --attach 12345
354
+ ```
355
+
356
+ If you want to choose the host to bind, you can use `--host` option.
357
+ Messages communicated between the debugger and the debuggee are *NOT* encrypted so please use remote debugging carefully.
358
+
359
+ #### `require 'debug/open'` in a program
360
+
361
+ If you can modify the program, you can open the debugging port by adding `require 'debug/open'` in the program.
362
+
363
+ If you don't want to stop the program at the beginning, you can also use `require 'debug/open_nonstop'`.
364
+ Using `debug/open_nonstop` is useful if you want to open a backdoor to the application.
365
+ However, it is also dangerous because it can become another vulnerability.
366
+ Please use it carefully.
367
+
368
+ By default, UNIX domain socket is used for the debugging port.
369
+ To use TCP/IP, you can set the `RUBY_DEBUG_PORT` environment variable.
370
+
371
+ ```console
372
+ $ RUBY_DEBUG_PORT=12345 ruby target.rb
373
+ ```
374
+
375
+ ### Integration with external debugger frontend
376
+
377
+ You can attach with external debugger frontend with VSCode and Chrome.
378
+
379
+ ```
380
+ $ rdbg --open=[frontend] target.rb
381
+ ```
382
+
383
+ This will open a debug port and the `[frontend]` can attach to the port.
384
+
385
+ #### VSCode integration
386
+
387
+ ([vscode-rdbg v0.0.9](https://marketplace.visualstudio.com/items?itemName=KoichiSasada.vscode-rdbg) or later is required)
388
+
389
+ If you don't run a debuggee Ruby process on VSCode, you can attach it to VSCode later with the following steps.
390
+
391
+ `rdbg --open=vscode` opens the debug port and tries to invoke the VSCode (`code` executable).
392
+
393
+ ```console
394
+ $ rdbg --open=vscode target.rb
395
+ DEBUGGER: Debugger can attach via UNIX domain socket (/tmp/ruby-debug-sock-1000/ruby-debug-ko1-27706)
396
+ DEBUGGER: wait for debugger connection...
397
+ Launching: code /tmp/ruby-debug-vscode-20211014-27706-gd7e85/ /tmp/ruby-debug-vscode-20211014-27706-gd7e85/README.rb
398
+ DEBUGGER: Connected.
399
+ ```
400
+
401
+ It tries to invoke the new VSCode window and VSCode will attach to the debuggee Ruby program automatically.
402
+
403
+ You can also use `open vscode` in a REPL.
404
+
405
+ ```console
406
+ $ rdbg target.rb
407
+ [1, 8] in target.rb
408
+ 1|
409
+ => 2| p a = 1
410
+ 3| p b = 2
411
+ 4| p c = 3
412
+ 5| p d = 4
413
+ 6| p e = 5
414
+ 7|
415
+ 8| __END__
416
+ =>#0 <main> at target.rb:2
417
+ (rdbg) open vscode # command
418
+ DEBUGGER: wait for debugger connection...
419
+ DEBUGGER: Debugger can attach via UNIX domain socket (/tmp/ruby-debug-sock-1000/ruby-debug-ko1-28337)
420
+ Launching: code /tmp/ruby-debug-vscode-20211014-28337-kg9dm/ /tmp/ruby-debug-vscode-20211014-28337-kg9dm/README.rb
421
+ DEBUGGER: Connected.
422
+ ```
423
+
424
+ If the machine which runs the Ruby process doesn't have a `code` executable on `$PATH`, the following message will be shown:
425
+
426
+ ```console
427
+ (rdbg) open vscode
428
+ DEBUGGER: wait for debugger connection...
429
+ DEBUGGER: Debugger can attach via UNIX domain socket (/tmp/ruby-debug-sock-1000/ruby-debug-ko1-455)
430
+ Launching: code /tmp/ruby-debug-vscode-20211014-455-gtjpwi/ /tmp/ruby-debug-vscode-20211014-455-gtjpwi/README.rb
431
+ DEBUGGER: Can not invoke the command.
432
+ Use the command-line on your terminal (with modification if you need).
433
+
434
+ code /tmp/ruby-debug-vscode-20211014-455-gtjpwi/ /tmp/ruby-debug-vscode-20211014-455-gtjpwi/README.rb
435
+
436
+ If your application is running on a SSH remote host, please try:
437
+
438
+ code --remote ssh-remote+[SSH hostname] /tmp/ruby-debug-vscode-20211014-455-gtjpwi/ /tmp/ruby-debug-vscode-20211014-455-gtjpwi/README.rb
439
+
440
+ ```
441
+
442
+ Note that you can attach with `rdbg --attach` and continue REPL debugging.
443
+
444
+ #### Chrome DevTool integration
445
+
446
+ Using `rdbg --open=chrome` will show the following message:
447
+
448
+ ```console
449
+ $ rdbg target.rb --open=chrome
450
+ DEBUGGER: Debugger can attach via TCP/IP (127.0.0.1:43633)
451
+ DEBUGGER: With Chrome browser, type the following URL in the address-bar:
452
+
453
+ devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&ws=127.0.0.1:57231/b32a55cd-2eb5-4c5c-87d8-b3dfc59d80ef
454
+
455
+ DEBUGGER: wait for debugger connection...
456
+ ```
457
+
458
+ Type the following in the address bar on Chrome browser, and you can continue the debugging with chrome browser:
459
+
460
+ ```txt
461
+ devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&ws=127.0.0.1:57231/b32a55cd-2eb5-4c5c-87d8-b3dfc59d80ef`
462
+ ```
463
+
464
+ Similar to VSCode, you can use `open chrome` to open the debugger in Chrome.
465
+
466
+ For more information about how to use Chrome debugging, [see the devtools docs](https://developer.chrome.com/docs/devtools/).
467
+
468
+ ## Configuration
469
+
470
+ You can configure the debugger's behavior with debug commands and environment variables.
471
+ When the debug session is started, some [initialization scripts](#initial-scripts) (e.g., `~/.rdbgrc`) are loaded, allowing you to configure the debugger's behavior to your needs and preferences.
472
+
473
+ ### Configuration list
474
+
475
+ You can configure the debugger's behavior with environment variables and `config` command.
476
+ Each configuration has an environment variable and a name which can be specified by `config` command.
477
+
478
+ ```
479
+ # configuration example
480
+ config set log_level INFO
481
+ config set no_color true
482
+ ```
483
+
484
+ <% cat = nil; DEBUGGER__::CONFIG_SET.each do |key, (env, desc, _, default)| %>
485
+ <% /\A(\w+): (.+)/ =~ desc; if cat != $1; cat = 1 %>
486
+ * <%= $1 %>
487
+ <% cat = $1; end %> * `<%= env %>` (`<%= key %>`): <%= default ? "#{$2} (default: #{default})" : $2 %><% end %>
488
+
489
+ There are other environment variables:
490
+
491
+ * `NO_COLOR`: If the value is set, set `RUBY_DEBUG_NO_COLOR` ([NO_COLOR: disabling ANSI color output in various Unix commands](https://no-color.org/)).
492
+ * `RUBY_DEBUG_ENABLE`: If the value is `0`, do not enable debug.gem feature.
493
+ * `RUBY_DEBUG_ADDED_RUBYOPT`: Remove this value from `RUBYOPT` at first. This feature helps loading debug.gem with `RUBYOPT='-r debug/...'`, and you don't want to derive it to child processes. In this case, you can set `RUBY_DEBUG_ADDED_RUBYOPT='-r debug/...'` (same value), and this string will be deleted from `RUBYOPT` at first.
494
+ * `RUBY_DEBUG_EDITOR` or `EDITOR`: An editor used by `edit` debug command.
495
+ * `RUBY_DEBUG_BB`: Define `Kernel#bb` method which is alias of `Kernel#debugger`.
496
+
497
+ ### Initial scripts
498
+
499
+ If there is a `~/.rdbgrc` file it is loaded as an initialization script (which contains debug commands) when the debug session is started.
500
+
501
+ * `RUBY_DEBUG_INIT_SCRIPT` environment variable can specify the initial script file.
502
+ * You can specify the initial script with `rdbg -x initial_script` (like gdb's `-x` option).
503
+
504
+ Initial scripts are useful to write your favorite configurations.
505
+ For example, you can set breakpoints with `break file:123` in `~/.rdbgrc`.
506
+
507
+ If there is a `~/.rdbgrc.rb` file it is also loaded as a Ruby script when the debug session is started.
508
+
509
+ ## Debug command on the debug console
510
+
511
+ On the debug console, you can use the following debug commands.
512
+
513
+ There are additional features:
514
+
515
+ * `<expr>` without debug command is almost the same as `pp <expr>`.
516
+ * If the input line `<expr>` does *NOT* start with any debug command, the line `<expr>` will be evaluated as a Ruby expression, and the result will be printed with `pp` method.
517
+ So that the input `foo.bar` is the same as `pp foo.bar`.
518
+ * If `<expr>` is recognized as a debug command, of course, it is not evaluated as a Ruby expression but is executed as debug command.
519
+ For example, you can not evaluate such single-letter local variables `i`, `b`, `n`, `c` because they are single-letter debug commands. Use `p i` instead.
520
+ * For consistency, the author (Koichi Sasada) recommends using the `p`, `pp`, or `eval` command to evaluate the Ruby expression every time.
521
+ * `Enter` without any input repeats the last command (useful when repeating `step`s) for some commands.
522
+ * `Ctrl-D` is equal to `quit` command.
523
+ * [debug command compare sheet - Google Sheets](https://docs.google.com/spreadsheets/d/1TlmmUDsvwK4sSIyoMv-io52BUUz__R5wpu-ComXlsw0/edit?usp=sharing)
524
+
525
+ You can use the following debug commands.
526
+ Each command should be written in 1 line.
527
+ The `[…]` notation means this part can be eliminated. For example, `s[tep]` means `s` or `step` is a valid command. `ste` is not valid.
528
+ The `<…>` notation means the argument.
529
+
530
+ <%= DEBUGGER__.help %>
531
+
532
+ ### Using IRB as the Debug Console
533
+
534
+ Starting from version `v1.9`, you can now use IRB as the debug console. This integration brings additional features such as:
535
+
536
+ * Autocompletion
537
+ * Support for multi-line input
538
+ * Access to commands not available in `debug`, like `show_source` or `show_doc`
539
+ * [Configurable command aliases](https://docs.ruby-lang.org/en/master/IRB.html#module-IRB-label-Command+Aliases)
540
+
541
+ To switch to the IRB console, simply use the `irb` command in the debug console.
542
+
543
+ Once activated, you'll notice the prompt changes to:
544
+
545
+ ```console
546
+ irb:rdbg(main):001>
547
+ ```
548
+
549
+ If you want to make IRB the default console for all sessions, configure the `irb_console` setting by either:
550
+
551
+ * Setting the `RUBY_DEBUG_IRB_CONSOLE=true` environment variable
552
+ * Or adding `config set irb_console 1` to your `~/.rdbgrc`
553
+
554
+ To disable the IRB console in the current session, execute `config set irb_console 0` in the console.
555
+
556
+ ## Debugger API
557
+
558
+ ### Start debugging
559
+
560
+ #### Start by requiring a library
561
+
562
+ You can start debugging without `rdbg` command by requiring the following libraries:
563
+
564
+ * `require 'debug'`: Same as `rdbg --nonstop --no-sigint-hook`.
565
+ * `require 'debug/start'`: Same as `rdbg`.
566
+ * `require 'debug/open'`: Same as `rdbg --open`.
567
+ * `require 'debug/open_nonstop'`: Same as `rdbg --open --nonstop`.
568
+
569
+ You need to require one of them at the very beginning of the application.
570
+ Using `ruby -r` (for example `ruby -r debug/start target.rb`) is another way to invoke with debugger.
571
+
572
+ NOTE: Until Ruby 3.0, there is old `lib/debug.rb` in the standard library.
573
+ `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.
574
+
575
+ So, if this gem is not installed, or if the `Gemfile` doesn't include this gem and `bundle exec` is used, you will see the following output:
576
+
577
+ ```console
578
+ $ ruby -r debug -e0
579
+ .../2.7.3/lib/ruby/2.7.0/x86_64-linux/continuation.so: warning: callcc is obsolete; use Fiber instead
580
+ Debug.rb
581
+ Emacs support available.
582
+
583
+ .../2.7.3/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:162: if RUBYGEMS_ACTIVATION_MONITOR.respond_to?(:mon_owned?)
584
+ (rdb:1)
585
+ ```
586
+
587
+ #### Start by method
588
+
589
+ After loading `debug/session`, you can start a debug session with the following methods.
590
+ They are convenient if you want to specify debug configurations in your program.
591
+
592
+ * `DEBUGGER__.start(**kw)`: start debug session with local console.
593
+ * `DEBUGGER__.open(**kw)`: open debug port with configuration (without configurations open with UNIX domain socket).
594
+ * `DEBUGGER__.open_unix(**kw)`: open debug port with UNIX domain socket.
595
+ * `DEBUGGER__.open_tcp(**kw)`: open debug port with TCP/IP.
596
+
597
+ For example:
598
+
599
+ ```ruby
600
+ require 'debug/session'
601
+ DEBUGGER__.start(no_color: true, # disable colorize
602
+ log_level: 'INFO') # Change log_level to INFO
603
+
604
+ ... # your application code
605
+ ```
606
+
607
+ ### `binding.break` method
608
+
609
+ `binding.break` (or `binding.b`) sets a breakpoint at that line.
610
+ It also has several keywords.
611
+
612
+ If `do: 'command'` is specified, the debugger suspends the program, runs the `command` as a debug command, and continues the program.
613
+ It is useful if you only want to call a debug command and don't want to stop there.
614
+ For example:
615
+
616
+ ```ruby
617
+ def initialize
618
+ @a = 1
619
+ binding.b do: 'info \n watch @a'
620
+ end
621
+ ```
622
+
623
+ In this case, execute the `info` command, then register a watch breakpoint for `@a` and continue to run.
624
+ You can also use `;;` instead of `\n` to separate your commands.
625
+
626
+ If `pre: 'command'` is specified, the debugger suspends the program and runs the `command` as a debug command, and keeps suspended.
627
+ It is useful if you have operations before suspend.
628
+ For example:
629
+
630
+ ```ruby
631
+ def foo
632
+ binding.b pre: 'p bar()'
633
+ ...
634
+ end
635
+ ```
636
+
637
+ In this case, you can see the result of `bar()` every time you stop there.
638
+
639
+ ## rdbg command help
640
+
641
+ ```console
642
+ <%= `exe/rdbg --help` %>
643
+ ```
644
+
645
+ # Additional Resources
646
+
647
+ - [From byebug to ruby/debug](https://st0012.dev/from-byebug-to-ruby-debug) by Stan Lo - A migration guide for `byebug` users.
648
+ - [ruby/debug cheatsheet](https://st0012.dev/ruby-debug-cheatsheet) by Stan Lo
649
+
650
+ # Contributing
651
+
652
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/ruby/debug]().
653
+ This debugger is not mature so your feedback will help us.
654
+
655
+ Please also check the [contributing guideline](/CONTRIBUTING.md).
656
+
657
+ # Acknowledgement
658
+
659
+ * Some tests are based on [deivid-rodriguez/byebug: Debugging in Ruby 2](https://github.com/deivid-rodriguez/byebug)
660
+ * Several codes in `server_cdp.rb` are based on [geoffreylitt/ladybug: Visual Debugger](https://github.com/geoffreylitt/ladybug)