Display slices in the debugger

I’m debugging some slice-heavy code using lldb from VSCode, and it’s excessively difficult to follow, because the slices aren’t null-terminated, so the debugger displays arbitrary amounts of string, and I have to figure out how much of it actually belongs to the slice.

Is there a trick to get the slice.ptr section to display just the amount covered by slice.len?

In zig repository there is a python pretty printer extension for lldb that can print slices, strings, optional, etc.: tools/lldb_pretty_printers.py

❯ lldb ./test
(lldb) target create "./test"
Current executable set to '/home/din/test' (x86_64).
(lldb) b main
Breakpoint 1: where = test`test.main + 8 at test.zig:4:5, address = 0x00000000010350d8
(lldb) r
Process 30732 launched: '/home/din/test' (x86_64)
Process 30732 stopped
* thread #1, name = 'test', stop reason = breakpoint 1.1
    frame #0: 0x00000000010350d8 test`test.main at test.zig:4:5
   1    const std = @import("std");
   2   
   3    pub fn main() void {
-> 4        const data: []const u8 = &[_]u8{ 'a', 'b', 'c' };
   5        std.debug.print("{s}", .{data});
   6    }
   7   
(lldb) s
Process 30732 stopped
* thread #1, name = 'test', stop reason = step in
    frame #0: 0x00000000010350e8 test`test.main at test.zig:5:20
   2   
   3    pub fn main() void {
   4        const data: []const u8 = &[_]u8{ 'a', 'b', 'c' };
-> 5        std.debug.print("{s}", .{data});
   6    }
   7   
(lldb) p data
([]u8) "abc"
(lldb) 
4 Likes

Perfect, thank you. I figured there must be something like that available, but it isn’t configured into ZLS or the Zig Extras package for VSCode (or if it is, I don’t have my settings right). That’s going to make my life a lot easier.

1 Like

As a breadcrumb for others who might be searching for LLDB pretty-printing, or how to display Zig data structures in the debugger, or other such phrases, this was fairly simple to add to VSCode.

In addition to the Zig Language plugin, you’ll want Zig Language Extras. This advice may become outdated at some point, if and when the main plugin adds debug functionality. A particularly useful feature of the extras plugin is launching a debug session from a test block, as well as just running them one block at a time in the editor.

With that configured, you’ll need a local copy of the Zig repo, or at least of the file linked in the marked reply. Add the commands you need from Settings like so: search LLDB, and find this field:

Just like in the top of the pretty-print script. You need to do it this way because the plugin won’t read your .lldbinit from the home directory. There may be a way to set that up, but I found this first, and it works fine.

That’s it, now your []const u8s look like strings and so on.

2 Likes