# How to debug my Crystal app?

**URL:** <https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393>\
**Category:** Help & Support\
**Created:** [June 16, 2021, 3:53pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393 "2021-06-16T15:53:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![gummybears](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/gummybears/32/1886_2.png) [@gummybears](https://forum.crystal-lang.org/u/gummybears)\
**Post date:** [June 16, 2021, 3:53pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/1 "2021-06-16T15:53:03Z")

</div>

I read the topic Debugger support but could not find a small example or a link to doc’s on how to debug  
a Crystal app.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [June 16, 2021, 4:04pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/2 "2021-06-16T16:04:16Z")

</div>

No nice way to do it right now

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [June 16, 2021, 4:51pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/3 "2021-06-16T16:51:23Z")

</div>

I wrote this guide for the support within vscode [Debug Crystal in VSCode via CodeLLDB - DEV Community](https://dev.to/bcardiff/debug-crystal-in-vscode-via-codelldb-3lf)

If you know gdb lldb you can use them, yet there is a limitation regarding what values can be inspected.

---

<div class="post-metadata">

**Author:** ![gummybears](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/gummybears/32/1886_2.png) [@gummybears](https://forum.crystal-lang.org/u/gummybears)\
**Post date:** [June 17, 2021, 3:12pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/4 "2021-06-17T15:12:54Z")

</div>

Perhaps time for me to switch from Geany to VSCode.  
Thanks.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [June 23, 2021, 6:11pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/5 "2021-06-23T18:11:20Z")

</div>

You can kind of use gdb the “normal way” as it were…

---

<div class="post-metadata">

**Author:** ![stellarpower](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stellarpower/32/2072_2.png) [@stellarpower](https://forum.crystal-lang.org/u/stellarpower)\
**Post date:** [June 2, 2024, 2:10pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/6 "2024-06-02T14:10:19Z")

</div>

@bcardiff Do you know if the LLDB helpers are up-to-date at all? I still see Strings formatted as an initial character and a length and I’m on LLDB version 15. Thanks

---

<div class="post-metadata">

**Author:** ![nobodywasishere](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/nobodywasishere/32/1983_2.png) [@nobodywasishere](https://forum.crystal-lang.org/u/nobodywasishere)\
**Post date:** [June 2, 2024, 3:03pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/7 "2024-06-02T15:03:21Z")

</div>

That is normal unfortunately, and something I’m currently looking into

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 2, 2024, 3:29pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/8 "2024-06-02T15:29:38Z")

</div>

Possibly related: [LLVM 16 supports Crystal's DWARF5 language code · Issue #13174 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/13174)

---

<div class="post-metadata">

**Author:** ![stellarpower](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stellarpower/32/2072_2.png) [@stellarpower](https://forum.crystal-lang.org/u/stellarpower)\
**Post date:** [June 2, 2024, 5:37pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/9 "2024-06-02T17:37:41Z")

</div>

Ah, yes, good point. I have had problems with DWARF 4/5 with gdb when compiling with clang, but in those situations I have had some output telling me gdb can’t make sense of them. I tried lldb-17 with the same result though.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 3, 2024, 5:42am UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/10 "2024-06-03T05:42:50Z")

</div>

Does this solution still available? i test on my emacs with dap-codelldb 1.8.1, get following error:

```auto
plugins/dap-mode/.extension/vscode/codelldb/extension/adapter/codelldb --port 12489

[ERROR codelldb::dap_session] Deserialization error: data did not match any variant 
of untagged enum ResponseResult at line 1 column 84

[ERROR codelldb::terminal] [61] channel closed

```

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [June 4, 2024, 1:05pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/11 "2024-06-04T13:05:50Z")

</div>

I just run the things as described in [Debug Crystal in VSCode via CodeLLDB - DEV Community](https://dev.to/bcardiff/debug-crystal-in-vscode-via-codelldb-3lf) and things works as expected using CodeLLDB vscode extension.

I also just try using lldb from mac directly and it seems to work.

The script is mimicking what ./spec/debug/driver.cr do under the hood.

```auto
% crystal build --debug ./spec/debug/strings.cr -o string-debug

% lldb string-debug 
(lldb) target create "string-debug"
Current executable set to '/Users/bcardiff/Projects/crystal/master/string-debug' (arm64).
(lldb) version
lldb-1500.0.200.58
Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
(lldb) command script import etc/lldb/crystal_formatters.py
(lldb) breakpoint set -n main -G true -o true -C 'process handle -s false -n false SIGSEGV SIGBUS'
Breakpoint 1: 2 locations.
(lldb) breakpoint set -n __crystal_main -G true -o true -C 'process handle -s true -n true SIGSEGV SIGBUS'
Breakpoint 2: where = string-debug`__crystal_main + 208 at strings.cr:1:1, address = 0x000000010000394c
(lldb) run
Process 36134 launched: '/Users/bcardiff/Projects/crystal/master/string-debug' (arm64)
(lldb) process handle -s false -n false SIGSEGV SIGBUS
NAME PASS STOP NOTIFY
=========== ===== ===== ======
SIGSEGV true false false
SIGBUS true false false
(lldb) process handle -s true -n true SIGSEGV SIGBUS
NAME PASS STOP NOTIFY
=========== ===== ===== ======
SIGSEGV true true true 
SIGBUS true true true
Process 36134 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x100003c8c)
    frame #0: 0x0000000100003c90 string-debug`__crystal_main at strings.cr:9:1
   6 # print: b
   7 # lldb-check: (String *) $1 = {{0x[0-9a-f]+}} "abcσdeσf"
   8 # gdb-check: $2 = "abcσdeσf"
-> 9 debugger
Target 0: (string-debug) stopped.
(lldb) print a
(String *) 0x00000001000c5970 "hello world"
(lldb) print b
(String *) 0x00000001000c5990 "abcσdeσf"

```

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 4, 2024, 1:20pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/12 "2024-06-04T13:20:21Z")

</div>

> [@bcardiff](#):
>
> `command script import etc/lldb/crystal_formatters.py`

Hi, I get following error on my my arch linux when try to reproduce your’s process.

```auto
 ╰─ $ lldb string-debug
(lldb) target create "string-debug"
Current executable set to '/home/zw963/Crystal/crystal-lang/crystal/string-debug' (x86_64).
(lldb) command script import etc/lldb/crystal_formatters.py
/home/zw963/Crystal/crystal-lang/crystal/etc/lldb/crystal_formatters.py:60: SyntaxWarning: invalid escape sequence '\('
  debugger.HandleCommand('type synthetic add -l crystal_formatters.CrystalArraySyntheticProvider -x "^Array\(.+\)(\s*\**)?" -w Crystal')
/home/zw963/Crystal/crystal-lang/crystal/etc/lldb/crystal_formatters.py:61: SyntaxWarning: invalid escape sequence '\('
  debugger.HandleCommand('type summary add -F crystal_formatters.CrystalString_SummaryProvider -x "^(String|\(String \| Nil\))(\s*\**)?$" -w Crystal')
(lldb)

```

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [June 4, 2024, 6:47pm UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/13 "2024-06-04T18:47:56Z")

</div>

I’m not sure, maybe is a python issue. If you copy/edit `crystal_formatters.py`, can you check if  
adding `r` to the following strings fix the issue? I didn’t try but a couple of search points into that direction. Disclaimer: I’m not familiar with python.

```auto
def __lldb_init_module(debugger, dict):
    debugger.HandleCommand(r'type synthetic add -l crystal_formatters.CrystalArraySyntheticProvider -x "^Array\(.+\)(\s*\**)?" -w Crystal')
    debugger.HandleCommand(r'type summary add -F crystal_formatters.CrystalString_SummaryProvider -x "^(String|\(String \| Nil\))(\s*\**)?$" -w Crystal')
    debugger.HandleCommand(r'type category enable Crystal')

```

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [June 5, 2024, 2:38am UTC](https://forum.crystal-lang.org/t/how-to-debug-my-crystal-app/3393/14 "2024-06-05T02:38:28Z")

</div>

> [@bcardiff](#):
>
> can you check if  
> adding `r` to the following strings fix the issue?

It is working! thank you very much.
