How to compile ruby on Windows and debug with GDB
Ruby, my favorite programming language! This post is about developing CRuby also known as MRI/YARV
For a long time I asked myself why on ruby for windows requiring gems took longer than it should. This lead to me learning how to compile ruby on windows and even debugging it. I want to make it easier for others to do this, so here's my advice.
Discussion: require takes XX times longer than on linux
My issue
require 'tz-data' took up to 1 second on my system. Which is fast enough if the program only starts once. After researching I found out that ruby-concurrent had a lot of files. I ran process monitor on the ruby process and saw that there were many file related events, some of which even double.
Usually ruby gems don't have that many files, but the ruby-concurrent has more than usual. Loading files on windows is sadly a bit slower than on linux in any case. And yet it seems strange that it is this much slower.
Setting up
Read https://github.com/MSP-Greg/ruby-loco/blob/main/Local-Use.md and do all the steps. (The build script was renamed)
Use my Fork with the adapted files.
Install the newest powershell
run
winget search Microsoft.PowerShell
winget install --id Microsoft.Powershell --source winget
Open the the ruby-loco repository and run this in the newest powershell (7.2)
.\1_0_build_install_msys2_64_debug.ps1
Install the GDB Beyond VSCode Extension and add the following configuration:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "by-gdb",
"request": "launch",
"name": "Launch(gdb)",
"program": "ruby-ucrt/bin/ruby.exe",
"programArgs": "test.rb",
// "cwd": "${workspaceRoot}",
"cwd": "${workspaceRoot}",
"stopAtEntry": true,
"debuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe"
}
]
}
You can now set breakpoints in VSCode and start debugging. The debugger stops automatically at the beginning. You can change this with stopAtEntry.
You can also set breakpoints manually with break FUNCTIONNAME.
Notes on GDB and debugging:
You will need to compile ruby with the debug symbols: need to add -g to the cflags, and make sure there is no -s or -S there. Also O3 as a flag might be a problem.
The unassisted way of building ruby:
I didn't really make it work, but without ruby-loco it works something like this:
# you first have to install all the msys packages, see ruby-loco for a list of them.
# oneclick/rubyinstaller2-packages has the newer openssl version
./autogen.sh
mkdir build
mkdir install
cd build
../configure --prefix=$PWD/../install --enable-shared
make miniruby -j
./miniruby -v
make gdb
make gdb-ruby
make install-nodoc -j
Further research and mentions
Tips and tricks in MRI Ruby development
perforce.com: gdb-stuck-ruby.txt
No Debugging symbols found when using gdb MSYS2 MinGW GCC環境をつくる(その4
How to create Ruby C extension and debug in VS Code
Ruby Installer2 Ruby Installer2 prebuilt pacman msys packages