Debugging functionality for Ruby
This library provides debugging functionality to Ruby (MRI) 2.7 and later.
This debug.rb is the replacement of traditional lib/debug.rb standard library, which is implemented by set_trace_func.
New debug.rb has several advantages:
Fast: No performance penalty on non-stepping mode and non-breakpoints.
Remote debugging: Support remote debugging natively.
UNIX domain socket (UDS)
TCP/IP
Integration with rich debugger frontends
| Frontend | Console | VSCode | Chrome DevTool |
|---|---|---|---|
| Connection | UDS, TCP/IP | UDS, TCP/IP | TCP/IP |
| Requirement | None | vscode-rdbg | Chrome |
Extensible: application can introduce debugging support in several ways:
rdbg command-r command line optionMisc
Ctrl-C at most of timing.$ gem install debugAlternatively, specify -Ipath/to/debug/lib in RUBYOPT, or as a Ruby command-line option
This is especially useful for debugging this gem during development.
If using Bundler, add the following to your Gemfile:
gem "debug", ">= 1.0.0"(The version constraint is important; `debug 5| binding.break # Now you can see it stops at this line 6| c = 3 7| d = 4 8| binding.break 9| p [a, b, c, d] 10| =>#0 at target.rb:5
(rdbg) info locals # You can show local variables =>#0 at target.rb:5 %self => main a => 1 b => 2 c => nil d => nil
(rdbg) continue # Continue the execution [3, 11] in target.rb 3| a = 1 4| b = 2 5| binding.break 6| c = 3 7| d = 4 => 8| binding.break # Again the program stops here 9| p [a, b, c, d] 10| 11| END =>#0 at target.rb:8
(rdbg) info locals # And you can see the updated local variables =>#0 at target.rb:8 %self => main a => 1 b => 2 c => 3 d => 4
(rdbg) continue [1, 2, 3, 4]
### Invoke the program from the debugger as a traditional debuggers
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.
This is similar to how you'd launch a program with `ruby program.rb`.
Then you can set breakpoints with the debug command `break` (`b` for short).
```console
$ cat target.rb # Sample program
a = 1
b = 2
c = 3
d = 4
p [a, b, c, d]
$ rdbg target.rb # run like `ruby target.rb`
DEBUGGER: Session start (pid: 7656)
[1, 7] in target.rb
=> 1| a = 1
2| b = 2
3| c = 3
4| d = 4
5| p [a, b, c, d]
6|
7| __END__
=>#0 at target.rb:1
(rdbg)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.
(rdbg) is the console prompt.
Let's set breakpoints on line 3 and line 5 with break command (b for short).
(rdbg) break 3 # set breakpoint at line 3
#0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
(rdbg) b 5 # set breakpoint at line 5
#1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)
(rdbg) break # show all registered breakpoints
#0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
#1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)You can see that two breakpoints are registered.
Let's continue the program by using the continue command.
(rdbg) continue
[1, 7] in target.rb
1| a = 1
2| b = 2
=> 3| c = 3
4| d = 4
5| p [a, b, c, d]
6|
7| __END__
=>#0 at target.rb:3
Stop by #0 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:3 (line)
(rdbg)You can see that we stopped at line 3.
Let's see the local variables with the info command, and continue execution with the continue.
The program will then suspend at line 5 and you can use the info command again.
(rdbg) info
=>#0 at target.rb:3
%self => main
a => 1
b => 2
c => nil
d => nil
(rdbg) continue
[1, 7] in target.rb
1| a = 1
2| b = 2
3| c = 3
4| d = 4
=> 5| p [a, b, c, d]
6|
7| __END__
=>#0 at target.rb:5
Stop by #1 BP - Line /mnt/c/ko1/src/rb/ruby-debug/target.rb:5 (line)
(rdbg) info
=>#0 at target.rb:5
%self => main
a => 1
b => 2
c => 3
d => 4
(rdbg) continue
[1, 2, 3, 4]
…
console
$ rdbg --open target.rb
DEBUGGER: Session start (pid: 7773)
DEBUGGER: Debugger can attach via UNIX domain socket (/home/ko1/.ruby-debug-sock/ruby-debug-ko1-7773)
DEBUGGER: wait for debugger connection...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).
You can connect to the debuggee with rdbg --attach command (rdbg -A for short).
$ rdbg -A
[1, 7] in target.rb
=> 1| a = 1
2| b = 2
3| c = 3
4| d = 4
5| p [a, b, c, d]
6|
7| __END__
=>#0 at target.rb:1
(rdbg:remote)
…
console
$ rdbg --attach 12345
…
console
$ RUBY_DEBUG_PORT=12345 ruby target.rbYou can attach with external debugger frontend with VSCode and Chrome.
$ rdbg --open=[frontend] target.rbThis will open a debug port and the [frontend] can attach to the port.
(vscode-rdbg v0.0.9 or later is required)
If you don't run a debuggee Ruby process on VSCode, you can attach it to VSCode later with the following steps.
rdbg --open=vscode opens the debug port and tries to invoke the VSCode (code executable).
$ rdbg --open=vscode target.rb
DEBUGGER: Debugger can attach via UNIX domain socket (/tmp/ruby-debug-sock-1000/ruby-debug-ko1-27706)
DEBUGGER: wait for debugger connection...
Launching: code /tmp/ruby-debug-vscode-20211014-27706-gd7e85/ /tmp/ruby-debug-vscode-20211014-27706-gd7e85/README.rb
DEBUGGER: Connected.It tries to invoke the new VSCode window and VSCode will attach to the debuggee Ruby program automatically.
You can also use open vscode in a REPL.
$ rdbg target.rb
[1, 8] in target.rb
1|
=> 2| p a = 1
3| p b = 2
4| p c = 3
5| p d = 4
6| p e = 5
7|
8| __END__
=>#0 at target.rb:2
(rdbg) open vscode # command
DEBUGGER: wait for debugger connection...
DEBUGGER: Debugger can attach via UNIX domain socket (/tmp/ruby-debug-sock-1000/ruby-debug-ko1-28337)
Launching: code /tmp/ruby-debug-vscode-20211014-28337-kg9dm/ /tmp/ruby-debug-vscode-20211014-28337-kg9dm/README.rb
DEBUGGER: Connected.If the machine which runs the Ruby process doesn't have a code executable on $PATH, the following message will be shown:
(rdbg) open vscode
DEBUGGER: wait for debugger connection...
DEBUGGER: Debugger can attach via UNIX domain socket (/tmp/ruby-debug-sock-1000/ruby-debug-ko1-455)
Launching: code /tmp/ruby-debug-vscode-20211014-455-gtjpwi/ /tmp/ruby-debug-vscode-20211014-455-gtjpwi/README.rb
DEBUGGER: Can not invoke the command.
Use the command-line on your terminal (with modification if you need).
code /tmp/ruby-debug-vscode-20211014-455-gtjpwi/ /tmp/ruby-debug-vscode-20211014-455-gtjpwi/README.rb
If your application is running on a SSH remote host, please try:
code --remote ssh-remote+[SSH hostname] /tmp/ruby-debug-vscode-20211014-455-gtjpwi/ /tmp/ruby-debug-vscode-20211014-455-gtjpwi/README.rb
Note that you can attach with rdbg --attach and continue REPL debugging.
Using rdbg --open=chrome will show the following message:
$ rdbg target.rb --open=chrome
DEBUGGER: Debugger can attach via TCP/IP (127.0.0.1:43633)
DEBUGGER: With Chrome browser, type the following URL in the address-bar:
devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&ws=127.0.0.1:57231/b32a55cd-2eb5-4c5c-87d8-b3dfc59d80ef
DEBUGGER: wait for debugger connection...Type the following in the address bar on Chrome browser, and you can continue the debugging with chrome browser:
devtools://devtools/bundled/inspector.html?v8only=true&panel=sources&ws=127.0.0.1:57231/b32a55cd-2eb5-4c5c-87d8-b3dfc59d80ef`
…
# configuration example
config set log_level INFO
config set no_color true
…
console
irb:rdbg(main):001>
…
console
$ ruby -r debug -e0
.../2.7.3/lib/ruby/2.7.0/x86_64-linux/continuation.so: warning: callcc is obsolete; use Fiber instead
Debug.rb
Emacs support available.
.../2.7.3/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:162: if RUBYGEMS_ACTIVATION_MONITOR.respond_to?(:mon_owned?)
(rdb:1)After loading debug/session, you can start a debug session with the following methods.
They are convenient if you want to specify debug configurations in your program.
DEBUGGER__.start(**kw): start debug session with local console.DEBUGGER__.open(**kw): open debug port with configuration (without configurations open with UNIX domain socket).DEBUGGER__.open_unix(**kw): open debug port with UNIX domain socket.DEBUGGER__.open_tcp(**kw): open debug port with TCP/IP.For example:
require 'debug/session'
DEBUGGER__.start(no_color: true, # disable colorize
log_level: 'INFO') # Change log_level to INFO
... # your application codebinding.break methodbinding.break (or binding.b) sets a breakpoint at that line.
It also has several keywords.
If do: 'command' is specified, the debugger suspends the program, runs the command as a debug command, and continues the program.
It is useful if you only want to call a debug command and don't want to stop there.
For example:
def initialize
@a = 1
binding.b do: 'info \n watch @a'
endIn this case, execute the info command, then register a watch breakpoint for @a and continue to run.
You can also use ;; instead of \n to separate your commands.
If pre: 'command' is specified, the debugger suspends the program and runs the command as a debug command, and keeps suspended.
It is useful if you have operations before suspend.
For example:
def foo
binding.b pre: 'p bar()'
...
endIn this case, you can see the result of bar() every time you stop there.
exe/rdbg [options] -- [debuggee options]
Debug console mode:
-n, --nonstop Do not stop at the beginning of the script.
-e DEBUG_COMMAND Execute debug command at the beginning of the script.
-x, --init-script=FILE Execute debug command in the FILE.No open issues yet, or sync has not completed.