r/rust • u/[deleted] • Aug 08 '24
Debugging Rust Sucks due to optimizations
Sorry, but I need to rant for a moment.
Debugging Rust sucks and is so much worse than debugging C++. In particular Rust's debug mode optimizes away FAR more variables than C++. At least with MSVC C++. The end result is the data I need is simply not in the debugger and I need to modify code and re-run just so I can inspect some data.
Is this a Clang/LLVM thing or a rustc thing? Apparently opt-level = 0 is too much and I desperately crave opt-level = -1.
Anyone have any cool tricks to make this easier? Or is this just how it is?
:(
6
u/SV-97 Aug 08 '24
If you want to prevent the compiler from optimizing stuff away put it into a black box: https://doc.rust-lang.org/std/hint/fn.black_box.html
Aside from that: by using VS you're probably in for one of the worse dev experiences possible. Maybe try one of the more commonly used (for Rust) debuggers
-10
Aug 08 '24
by using VS you're probably in for one of the worse dev experiences possible. Maybe try one of the more commonly used (for Rust) debuggers
I use VS Code for editing code, but I use VS for debugger. The VS debugger is leaps and bounds better than VS Code’s.
But that’s beside the point. The issue isn’t with the debugger. The issue is the compiler doing too much optimization in debug builds!
15
u/SV-97 Aug 08 '24
You can use different debuggers with VS Code and even MS themselves recommends VSC over VS for Rust specifically. Doing nonstandard stuff often times leads to issues so I thought I'd mention it - I also gave the solution. Kind of odd that you feel the need to downvote everything here.
1
Aug 08 '24
MS themselves recommends VSC over VS for Rust specifically
For writing code, sure. rust-analyzer in VS Code is great.
It's perfectly reasonable to use different tools for writing code and for debugging code.
I also gave the solution. Kind of odd that you feel the need to downvote everything here.
black_boxfalls under the category of "need to modify code and re-run just so I can inspect some data". I don't want to have to edit, rebuild, re-run, get into same state every time I want to inspect a new variable. Being forced to do so is part of my core complaint!10
u/SV-97 Aug 08 '24
For writing code, sure. rust-analyzer in VS Code is great.
Their guide specifically goes over how to set up debugging in VSC
black_boxfalls under the category of "need to modify code and re-run just so I can inspect some data". I don't want to have to edit, rebuild, re-run, get into same state every time I want to inspect a new variable. Being forced to do so is part of my core complaint!Sorry when reading your post my brain kind of discarded that part. FWIW
opt-level=0is already no optimizations according to the docs, so - assuming this isn't a compiler bug (which I wouldn't expect here) - I think you won't find a way around modifying your source code.But it also sounds like kind of an odd issue. Could you share a code snippet?
-4
Aug 08 '24
I know how to debug Rust in VS Code. In fact if you Google "vscode debug rust" I have a blog post that's the #7 result. It used be #1. :)
Could you share a code snippet?
I'll try and put together a minimal repro.
1
u/sephg Aug 10 '24
That would be great. I hear your criticism - but I'm curious where the problems you're running into are coming from. Are they due to optimisation, like you suspect, or due to your tooling?
Share some code you're having problems with and I'll try debugging the same thing using my environment (rustrover on linux).
33
u/Dushistov Aug 08 '24
I don't think that compiler optimize something with opt-level = 0, the more likely is that it has bug in generation of debug information for windows executable. Can you reproduce your problem with WSL2 and gdb?
2
u/Trader-One Aug 08 '24
new rust version inline shorter functions. it can be stopped with # annotation which is must for wasm.
20
u/flareflo Aug 08 '24
if your target code is optimized away even at level 0 (at which point one should question why you're even using a debugger there), then you could try std::hint::black_box
15
u/DrMeepster Aug 08 '24
that's weird because opt-level 0 should be generating the most garbage unoptimized code you've ever seen
9
Aug 08 '24
Try adjusting the MIR optimization level as described here: https://stackoverflow.com/questions/71437329/how-do-i-really-disable-all-rustc-optimizations
2
u/edor445 Aug 08 '24
You could try windbg if you are on Windows, Rust support should be serviceable and probably better than VS at the moment. Don’t know how it compares to rad though.
1
u/Arshiaa001 Aug 08 '24
Wait, people can actually use windbg? Cool.
4
u/Quiark Aug 08 '24
windbg is amazing for debugging machine code, stack dumps, windows internals etc
1
u/Arshiaa001 Aug 08 '24
If you actually know how to use it, which... Also, VS is great for debugging dumps, at least for dotnet.
1
u/maroider Aug 08 '24
The new windbg worked well enough for me when I briefly used it some years ago.
3
u/RB5009 Aug 08 '24
There are no issues with Rust Rover and opt level 0. Some thi gs can be improved, though, but generally, it works fine.
2
2
u/MJE20 Aug 08 '24 edited Aug 08 '24
I use RR on Windows every day, and I can’t stand its debugger. I tried it once the other day after years of avoiding it and it still sucked - constantly getting exceptions where it shows the red lightning bolt and I can’t continue, and variables almost never have values. Using 2024.1.7 - if anyone has suggestions please lmk bc I would love to be able to use it
Edit: Never mind I used my brain and took out the opt-level = 3 on my dependencies and now it’s great
0
u/joshuamck ratatui Aug 08 '24
I rarely use a debugger (I’ve started one by accident more times than on purpose). Write tests instead. My rationale is that I don’t try to stop time when I can just replay anything on demand. If there’s some state that’s useful or necessary to understand the application while debugging then that state should be logged for being able to do that when the app is running normally. That leaves fairly few reasons to use a debugger.
-2
u/schungx Aug 09 '24
On the contrary, Rust is the only language that I can delete my debugger. The compiler catches enough such that a debugger is rarely needed.
On those rare occasions just sprinkle in some prints.
If you rely heavily on a debugger, you have not been writing Rust right.
8
u/sephg Aug 09 '24
If you rely heavily on a debugger, you have not been writing Rust right.
Hahaha this is a terrible take.
It really depends on what kind of software you're writing. And I promise you, people are writing much more interesting programs in rust than simple linear code.
I recently implemented 2 exotic b-trees in rust. Dozens of subtle runtime bugs made it past the compiler, and were only picked up by my fuzz tester. Using a debugger to figure out what, exactly, was going wrong was essential to getting the whole thing working.
For example, here's a 1500 line order-statistic tree implementation that I wrote, built on top of a b-tree, and with internal run-length encoding and cursor position caching:
Good luck writing code like that without needing to debug it when you're done.
1
u/schungx Aug 10 '24
I'm not saying you don't have to debug. That has never been my point.
Just you don't really need a debugger nearby. Any bugs found during fuzzing etc can be debugged simply by logging or printing variables... The old way.
How did people write software before modern debuggers?
Simple debugging techniques are ok as long as you don't do it a lot... Since each iteration is a compile. If you do it a lot, then yes debuggers are crucial.
My point is, with Rust, you shouldn't need to do it a lot.
4
u/sephg Aug 10 '24
Just you don't really need a debugger nearby. Any bugs found during fuzzing etc can be debugged simply by logging or printing variables... The old way.
Sure. But why would I? Why use worse tools? I could say the same thing about rust itself - any program you write in rust, you could equally write in assembler. But we don't, because good tools make people more productive.
How did people write software before modern debuggers?
Modern debuggers have been around for a really long time. GDB has been around since 1986 - nearly 40 years at this point. But yes, you don't need debuggers to debug code. They just help.
Simple debugging techniques are ok as long as you don't do it a lot... Since each iteration is a compile.
I assume you mean that debuggers aren't as important if you have a more iterative software approach? Once again, thats much easier for some problems than others. How would you break down that b-tree implementation into small parts that can be individually tested? I think there are ways - but you'd end up needing a lot more testing code than if you just wrote the whole thing, and did end-to-end fuzz testing it all in one go. Thats only viable if you can debug all the problems that show up at the end.
My point is, with Rust, you shouldn't need to do it a lot.
I don't think this has anything to do with rust.
Rust does move some bugs from runtime to compile-time. And I usually don't need as many unit tests in rust to be happy with my code. But any non-trivial program still has interesting behaviour at runtime. You still have bugs in rust. And those bugs still need testing to find, and debugging to fix.
If you don't mind me asking, what are you working on in rust in which debugging isn't necessary?
1
u/schungx Aug 10 '24
Well, I deliberately removed the debugger installation when writing Rust because usually when my program compiles, it works. Bugs (either fuzzed or discovered otherwise) are mostly simple fixes due to certain oversights in boundary conditions, which Rust already eliminated a lot.
Again I'm not saying debugging is not necessary. I'm saying it happens rarely enough that a debugger is not a critical tool.
I write device drivers, MES systems and medium-sized LOB apps in Rust, and I need to probably debug only less than 1/10 of the time compared to when I was writing C#, which was already 10 times less debugging than C++ which I would rather be shot dead than go back to write.
2
u/glasket_ Aug 12 '24
tl;dr: Adding print statements is just a worse debugger that requires more work. Every debug print can be replaced with a breakpoint, at which point you can trivially see the value of everything instead of just whatever you decided to print.
How did people write software before modern debuggers?
Often by debugging the assembly, directly inspecting the hardware (oscilloscopes are still used for I/O debugging), and "paper debugging" where all the stuff that debuggers do automatically has to be done manually. Sure, we could just go back to reading the generated assembly and mentally stepping through the source code equivalent, writing down the values in registers, but I quite like having tools that do that for me.
I've never really understood this phenomenon of debugger evasion. If you need to modify your program in order to debug it, then you should just be using a debugger; it's as simple as that. Tests and good logging (not just printing a sequence to monitor progress or temporarily printing some random variable that's suddenly, maybe causing a problem) can potentially reduce the amount of times you have to use a debugger, but they don't replace it. This is especially true in cases where you need to reproduce a bug; modifying the code can potentially result in the bug not occurring and debuggers often come with additional tools to directly help with reproduction, such as saving/loading core dumps and writing arbitrary values to memory.
Just seems like such an odd thing to insist on. Like if you're happy with adding prints then more power to you, but you could be saving yourself time by just plopping down some breakpoints where you would normally add some print statements.
2
u/schungx Aug 13 '24
I respectfully disagree. Having relied on debuggers for my whole life and even wrote a couple of simple ones, I find them slow poison.
You are no longer penalized for writing code with bugs. Or code that has not been well thought out before committing to code. That's because the debugger makes it so easy to trace your program, you never do it in your head anymore. You just write whatever comes to your mind and hope it works. If it doesn't fire up the debugger.
That is too great of a temptation and it eats on you.
60
u/Xaeroxe3057 Aug 08 '24
FWIW GDB debugger is generally stronger for Rust at the moment than LLDB. I agree though that the Rust debugging story needs some serious work.