Skip to content

Debug visualization of Rust data structures #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
vadimcn opened this issue Jun 28, 2015 · 4 comments
Open

Debug visualization of Rust data structures #160

vadimcn opened this issue Jun 28, 2015 · 4 comments

Comments

@vadimcn
Copy link
Contributor

vadimcn commented Jun 28, 2015

Currently VS GDB engine (GDBEng) doesn't support GDB's "pretty printers", so Strings, Vecs and other HashMaps look pretty incomprehensible in the variable inspector. I've tried several approaches to address this, but so far without much success. Still, I thought I should document them here for posterity:

  1. Force MI pretty-printing mode in GDB by injecting the -enable-pretty-printing command via init script. Unfortunately, GDB always interprets Python-based pretty-printers as "dynamic", i.e. with an unknown number of children (even if the pretty-printer's children() method returns a finite sequence). This results in varobj's like this: name="var1",numchild="0",dynamic="1",has_more="1" ..., which GDBEng doesn't know how to deal with.
    As far as I can see, the only way to make this approach work, is to fix the GDBEng, once Microsoft open-sources it.
  2. Use VS's own "debug visualizers".
    Alas, this doesn't work either because of the confluence of two problems:
    • Since GDB does not understand the DW_AT_language=Rust attribute yet, it falls back to treating Rust modules as if they were C: all non-primitive type names are printed as struct Foo, all namespaces are ignored, ditto for generic parameters.
    • On the other hand, GDBEng visualizer's type parser seems to be hard-coded for C++ types. I could not figure out a way to make it match type strings such as the above.
      We could probably work around this by emitting DW_AT_language=CPlus for Rust modules, but that seems a bit too drastic for the sake of supporting one specific IDE. Although... C++ type system is probably closer to Rust than C is, so maybe it isn't such a bad idea? I wonder what @michaelwoerister thinks about this :)
  3. Use LLDB rather than GDB.
    Although I've managed to build LLDB for Windows without much trouble, at the moment it looks pretty raw: Python does not initialize properly, single-line stepping is broken, etc, etc. Because of the problem with Python, I could not evaluate how well (or whether at all) LLDB's "data formatters" work over the MI interface.
@Eh2406
Copy link

Eh2406 commented Aug 29, 2016

I think GDBEng is at https://github.com/Microsoft/MIEngine so maybe 1 is posibal.

@Boddlnagg
Copy link
Contributor

See also https://internals.rust-lang.org/t/debugging-rust-programs-on-windows-with-visual-studio-windbg/3848 about better support for the native MSVC debugger.

@Boddlnagg
Copy link
Contributor

rust-lang/rust#36503 is related

bors added a commit to rust-lang/rust that referenced this issue Jul 28, 2017
…erister

Embed MSVC .natvis files into .pdbs and mangle debuginfo for &str, *T, and [T].

No idea if these changes are reasonable - please feel free to suggest changes/rewrites.  And these are some of my first real commits to any rust codebase - *don't* be gentle, and nitpick away, I need to learn! ;)

### Overview
Embedding `.natvis` files into `.pdb`s allows MSVC (and potentially other debuggers) to automatically pick up the visualizers without having to do any additional configuration (other than to perhaps add the relevant .pdb paths to symbol search paths.)

The native debug engine for MSVC parses the type names, making various C++ish assumptions about what they mean and adding various limitations to valid type names.  `&str` cannot be matched against a visualizer, but if we emit `str&` instead, it'll be recognized as a reference to a `str`, solving the problem.  `[T]` is similarly problematic, but emitting `slice<T>` instead works fine as it looks like a template.  I've been unable to get e.g. `slice<u32>&` to match visualizers in VS2015u3, so I've gone with `str*` and `slice<u32>*` instead.

### Possible Issues
* I'm not sure if `slice<T>` is a great mangling for `[T]` or if I should worry about name collisions.
* I'm not sure if `linker.rs` is the right place to be enumerating natvis files.
* I'm not sure if these type name mangling changes should actually be MSVC specific.  I recall seeing gdb visualizer tests that might be broken if made more general?  I'm hesitant to mess with them without a gdb install.  But perhaps I'm just wracking up technical debt.
  Should I try `pacman -S mingw-w64-x86_64-gdb` and to make things consistent?
* I haven't touched `const` / `mut` yet, and I'm worried MSVC might trip up on `mut` or their placement.
* I may like terse oneliners too much.
* I don't know if there's broader implications for messing with debug type names here.
* I may have been mistaken about bellow test failures being ignorable / unrelated to this changelist.

### Test Failures on `x86_64-pc-windows-gnu`

```
---- [debuginfo-gdb] debuginfo-gdb\associated-types.rs stdout ----
        thread '[debuginfo-gdb] debuginfo-gdb\associated-types.rs' panicked at 'gdb not available but debuginfo gdb debuginfo test requested', src\tools\compiletest\src\runtest.rs:48:16
note: Run with `RUST_BACKTRACE=1` for a backtrace.

[...identical panic causes omitted...]

---- [debuginfo-gdb] debuginfo-gdb\vec.rs stdout ----
        thread '[debuginfo-gdb] debuginfo-gdb\vec.rs' panicked at 'gdb not available but debuginfo gdb debuginfo test requested', src\tools\compiletest\src\runtest.rs:48:16
```

### Relevant Issues
* #40460 Metaissue for Visual Studio debugging Rust
* #36503 Investigate natvis for improved msvc debugging
* PistonDevelopers/VisualRust#160 Debug visualization of Rust data structures

### Pretty Pictures
![Collapsed Watch Window](https://user-images.githubusercontent.com/75894/28180998-e44c7516-67bb-11e7-8b48-d4f9605973ae.png)
![Expanded Watch Window](https://user-images.githubusercontent.com/75894/28181000-e8da252e-67bb-11e7-96b8-d613310c04dc.png)
@MaulingMonkey
Copy link
Collaborator

MSVC toolchain builds now embed .natvis files into built .pdbs if available. Probably still room for improvement in GNU toolchain builds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants