by armchairhacker · 3y ago · 0 comments
One of the current issues with Rust (and a place where C++ is still better) is debugging. Granted, I end up needing to debug Rust code much less often than C++ code, but it would still be nice to actually call rust functions in IntelliJ (and it seems there's limited support in gdb and lldb), and view data-structures like maps opaquely instead of just their complex internals.
What platform / debugger are you on? I'd expect the GDB pretty-printers (which from the article it looks like Rust has been shipping by default for the standard library) to be taking care of this relatively well - e.g. looking at https://github.com/rust-lang/rust/pull/72357 it appears that `HashMap` and `HashSet` have printers.
(Raw C++ data structures without some form of pretty printing are also incomprehensible for normal development - and I'd be surprised if third party libraries were particularly good at shipping pretty printers, though I may be wrong!)
It’s gotten much better. Primitive structures which are defined by their fields and builtins like `Vec`, `HashMap`, `String`, are handled good. But structures in library or user code which use “raw” and other non-trivial representations, but provide an opaque safe API, I can only introspect the non-trivial fields.
This annotation is exactly what those custom structures need.
It's quite hard work sometimes - there's a lot in there - but the capabilities available are amazing and it's well worth nosing around.
I also recommend just hitting TAB a lot and seeing what commands are suggested. Then use the "help"command to find out more.
My employer also hosts some articles that digest some knowledge we've gained into a more palatable form: https://undo.io/resources/gdb-watchpoint/
This would make bugs around state in Debug impls much more complicated, for example. This can creep in under a few layers of indirection - ultimately calling a function that gets the value of an interned string would be one example.
But debuggers like GDB have built in infrastructure for expressing how to display structured, potentially nested data structures - it's built for the purpose and powerful, so it's nice to bring it to bear if you've not already got something equivalent in they program.
The other important thing is that the debugger's pretty printers work on a core file, without needing a live process.
I agree.
> data-structures like maps opaquely instead of just their complex internals.
Works for me:
use std::collections::HashMap;
fn main() {
let mut m = HashMap::<String, u32>::new();
m.insert("a".to_string(), 5);
m.insert("b".to_string(), 3);
println!("Hello, world!");
}
$ rust-gdb target/debug/t1
(gdb) b 7
(gdb) r
(gdb) print(m)
$3 = HashMap(size=2) = {["a"] = 5, ["b"] = 3}This debugger_visualizer attribute does seem like a really cool capability. It's about embedding debugging scripts for debug tools, in your rust source code; couldn't be more direct! https://doc.rust-lang.org/nightly/reference/attributes/debug...
Though that “only” dates back to 2020 so people with experience older than that would have had a worse experience, and may have ended once bitten twice shy as a result never noticing the improvements.
No it isn't? IDE debugging works just as well for Rust as it does for C++ in my experience - better even. Yes it sometimes shows raw internals of complex containers instead of the logical structure, but it does for C++ too.
In Rust I can just click "Debug test" next to any test and it will start it in a debugger flawlessly. Zero effort to set up. I've never seen anything close to that in C++.
Of course as someone else said, you need a debugger far far less in Rust than C++.
I'm a non-proud printf() debugger. I understand that if I get use to debugger debugging it'd be in many ways better. The thing is, it's an entirely different paradigm of development, and paradigm shifts can come with significant productivity cost. Of course, learning, growing and getting comfortable with new/different tools is part of being a software engineer.
I also see people who use them all the time but reluctantly - I'd love to know what the difference is.
Compared to this gdb is just meh. I know, I know, Rust compiles to a nice binary, there's no runtime/vm/interpreter/JIT/intermediate-language/bytecode...
but that's not what I miss in gdb, I miss the discoverability, the visual representation, the whole I from the IDE concept.
(Some precautionary measures would of course be required to avoid infinite recursion and similar. But the same issues already exist in debuggers that allow pretty-printers to eg. run arbitrary python code. To avoid a pathological or buggy formatter breaking either the debugged program or the debugger itself, it might be best to execute the formatter in a separate child process.)
It seems like debug printers should be pure functions but you never know what sort of weird stuff people do.
Lots of devils in the details, but that sounds possible.
The product I work on does pretty much exactly this - we need it because our debugger time travels and we can't allow history to be altered!
I wrote some words here: https://medium.com/time-travel-debugging/calling-functions-i...
But it's basically exactly what you propose. We didn't sandbox the syscalls but you could do that too.
https://dorotac.eu/posts/debugging_rust/
You can view the data structures opaquely even though the functions are written not in Rust, but in Python.
One of the difficulties is that the Rust compiler will emit different debugging info depending on the compiler version. Perhaps I should look into how gdb deals with that (if at all).
Patches welcome.
But the contents is interesting. Bookmarked.
> $1 = alloc::string::String {vec: alloc::vec::Vec<u8, alloc::alloc::Global> {buf: alloc::raw_vec::RawVec<u8, alloc::alloc::Global> {ptr: core::ptr::unique::Unique<u8> {pointer: core::ptr::non_null::NonNull<u8> {pointer: 0x5555555abaa0}, _marker: core::marker::PhantomData<u8>}, cap: 3, alloc: alloc::alloc::Global}, len: 3}}
On my browser, everything breaks according to the browser's window size (I set white-space: pre-wrap).
So you don't have to manually evaluate cells, but they are evaluated based on the computations dataflow graph.
Additionally it allows for very interactive visualisations and data input.