"Life is all about sharing. If we are good at something, pass it on." - Mary Berry

Debug Rust in Helix using lldb-vscode: display the contents of local string variables

2023-08-11

Categories: Development Environment

In a previous post titled debugging Rust in Helix, I introduced a workaround using codelldb to address the visualization issue related to string variables within lldb-vscode.

However, the usage of codelldb presents some challenges:

codelldb breaks helix theme

In my quest for an improved debugging experience, I found rust-lldb, which impressively manages to display the contents of string variables:

 1(lldb) b src/main.rs:5
 2Breakpoint 1: where = hello_cargo`hello_cargo::main::h24135e338b19c0c6 + 212 at main.rs:5:5, address = 0x0000000100003cc0
 3(lldb) run
 4Process 62497 launched: '/Users/quantong/Code/personal/rust/1-getting-started/hello_cargo/target/debug/hello_cargo' (arm64)
 5Process 62497 stopped
 6* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
 7    frame #0: 0x0000000100003cc0 hello_cargo`hello_cargo::main::h24135e338b19c0c6 at main.rs:5:5
 8   2        let s = "world";
 9   3        let x = 2;
10   4        let b = true;
11-> 5        println!("Hello, {} {} {}!", s, x, b);
12   6    }
13(lldb) frame variable
14(&str) s = "world" {
15  data_ptr = 0x0000000100039d70 "worldHello,  !\n"
16  length = 5
17}
18(int) x = 2
19(bool) b = true

The enhanced functionality of rust-lldb is achieved by executeing specific LLDB commands before loading the program:

1$ rust-lldb target/debug/hello_cargo
2(lldb) command script import "/opt/homebrew/Cellar/rust/1.71.0/lib/rustlib/etc/lldb_lookup.py"
3(lldb) command source -s 0 '/opt/homebrew/Cellar/rust/1.71.0/lib/rustlib/etc/lldb_commands'
4Executing commands in '/opt/homebrew/Cellar/rust/1.71.0/lib/rustlib/etc/lldb_commands'.

Prompted by this success, the question arose whether we could integrate these steps into lldb-vscode.

As it turns out, accomplishing this is possible by using initCommands

Add the following to ~/.config/helix/languages.toml:

 1[[language]]
 2name = "rust"
 3
 4[language.debugger]
 5name = "lldb-vscode"
 6transport = "stdio"
 7command = "lldb-vscode"
 8
 9[[language.debugger.templates]]
10name = "binary"
11request = "launch"
12completion = [ { name = "binary", completion = "filename" } ]
13args = { program = "{0}", initCommands = [ "command script import /opt/homebrew/Cellar/rust/1.71.0/lib/rustlib/etc/lldb_lookup.py", "command source -s 0 /opt/homebrew/Cellar/rust/1.71.0/lib/rustlib/etc/lldb_commands" ] }

then you can debug as usual:

lldb-vscode displays contents of string variables

Tags: helix dap lldb-vscode codelldb

Edit on GitHub

Related Posts: