Skip to content

Commit 65cfdea

Browse files
committed
Add section for #![debugger_visualizer]
1 parent 16a4e0b commit 65cfdea

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/debuginfo/debugger-visualizers.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,55 @@ debug info, but can be derived from invariants about the language and the type i
1313
example is allowing one to interact with the elements of a `Vec<T>` instead of just it's `*mut u8`
1414
heap pointer, length, and capacity.
1515

16+
## `rust-lldb`, `rust-gdb`, and `rust-windbg.cmd`
17+
18+
These support scripts are distributed with Rust toolchains. They locate the appropriate debugger and
19+
the toolchain's visualizer scripts, then launch the debugger with the appropriate arguments to load
20+
the visualizer scripts before a debugee is launched/attached to.
21+
22+
## `#![debugger_visualizer]`
23+
24+
[This attribute][dbg_vis_attr] allows Rust library authors to include pretty printers for their
25+
types within the library itself. These pretty printers are of the same format as typical
26+
visualizers, but are embedded directly into the compiled binary. These scripts are loaded
27+
automatically by the debugger, allowing a seamless experience for users. This attribute currently
28+
works for GDB and natvis scripts.
29+
30+
[dbg_vis_attr]: https://doc.rust-lang.org/reference/attributes/debugger.html#the-debugger_visualizer-attribute
31+
32+
GDB python scripts are embedded in the `.debug_gdb_scripts` section of the binary. More information
33+
can be found [here](https://sourceware.org/gdb/current/onlinedocs/gdb.html/dotdebug_005fgdb_005fscripts-section.html). Rustc accomplishes this in [`rustc_codegen_llvm/src/debuginfo/gdb.rs`][gdb_rs]
34+
35+
[gdb_rs]: https://github.com/rust-lang/rust/blob/main/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs
36+
37+
Natvis files can be embedded in the PDB debug info using the [`/NATVIS` linker option][linker_opt],
38+
and have the [highest priority][priority] when a type is resolving which visualizer to use. The
39+
files specified by the attribute are collected into
40+
[`CrateInfo::natvis_debugger_visualizers`][natvis] which are then added as linker arguments in
41+
[`rustc_codegen_ssa/src/back/linker.rs`][linker_rs]
42+
43+
[linker_opt]: https://learn.microsoft.com/en-us/cpp/build/reference/natvis-add-natvis-to-pdb?view=msvc-170
44+
[priority]: https://learn.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=visualstudio#BKMK_natvis_location
45+
[natvis]: https://github.com/rust-lang/rust/blob/e0e204f3e97ad5f79524b9c259dc38df606ed82c/compiler/rustc_codegen_ssa/src/lib.rs#L212
46+
[linker_rs]: https://github.com/rust-lang/rust/blob/main/compiler/rustc_codegen_ssa/src/back/linker.rs#L1106
47+
48+
LLDB is not currently supported, but there are a few methods that could potentially allow support in
49+
the future. Officially, the intended method is via a [formatter bytecode][bytecode]. This was
50+
created to offer a comparable experience to GDB's, but without the safety concerns associated with
51+
embedding an entire python script. The opcodes are limited, but it works with `SBValue` and `SBType`
52+
in roughly the same way as python visualizer scripts. Implementing this would require writing some
53+
sort of DSL/mini compiler.
54+
55+
[bytecode]: https://lldb.llvm.org/resources/formatterbytecode.html
56+
57+
Alternatively, it might be possible to copy GDB's strategy entirely: create a bespoke section in the
58+
binary and embed a python script in it. LLDB will not load it automatically, but the python API does
59+
allow one to access the [raw sections of the debug info][SBSection]. With this, it may be possible
60+
to extract the python script from our bespoke section and then load it in during the startup of
61+
Rust's visualizer scripts.
62+
63+
[SBSection]: https://lldb.llvm.org/python_api/lldb.SBSection.html#sbsection
64+
1665
## Performance
1766

1867
Before tackling the visualizers themselves, it's important to note that these are part of a

0 commit comments

Comments
 (0)