Crystal Debug
Last updated
Last updated
This tutorial has tips and tricks on how to debug Crystal projects. It shows how to leverage tools like GDB or LLDB using debugger clients like for VSCode.
Crystal - See instalation guide
Crystal project - See installation guide or
VSCode with and extensions
GNU debugger (GDB) or LLVM debugger (LLDB) - See installation guide below
Install gdb
or lldb
accordingly to your OS.
By convention the project directory name is the same as your application name, if you have changed it, please update ${workspaceFolderBasename}
with the name configured inside shards.yml
task.json
configuration to compile a crystal projectlaunch.json
configuration to debug a binaryFully debugging Crystal applications is not supported yet. You can use some of the techniques below to improve the debugging experience.
Instead of putting breakpoints using commands inside GDB or LLDB you can try to set a breakpoint using debugger
keyword.
Currently, Crystal lacks support for debugging inside of blocks. If you put a breakpoint inside a block, it will be ignored.
As a workaround, use pp
to pretty print objects inside of blocks.
@[NoInline]
to debug arguments dataSometimes crystal will optimize argument data, so the debugger will show <optimized output>
instead of the arguments. To avoid this behavior use the @[NoInline]
attribute before your function implementation.
To print string objects in the debugger:
First, setup the debugger with the debugger
statement:
Then use print
in the debugging console.
Or add &foo.c
using a new variable entry on watch section in VSCode debugger
To print array items in the debugger:
First, setup the debugger with the debugger
statement:
Then use print
in the debugging console:
Change the buffer index for each item you want to print.
For printing @foo
var in this code:
You can use self.foo
in the debugger terminal or VSCode GUI.
Some objects do not show at all. You can unhide them using the .to_s
method and a temporary debugging variable, like this:
This trick allows showing the bar_hello_to_s
variable inside the debugger tool.
lldb
does not show data for variables in crystal yet, see issue