430 points · by steveklabnik · 7y ago · 161 comments
The default removal of jemalloc should greatly cut down on binary size as well, if I understand correctly.
I've dabbled in Rust, but haven't had a major reason to use it for anything yet. I'm looking forward to when that time comes.
In Rust, everyone is a print debugger. The only thing that really goes wrong in normal code (once it compiles) is "why is this value not what I expect?". Dropping down into GDB is way overkill.
* Rust code can have memory errors + undefined behavior, because Rust code can say "unsafe". Plenty of real projects use "unsafe". (Alternate reason: because the compiler has soundness bugs.)
* Memory errors + undefined behavior aren't the only reasons people like debuggers. Consider: there are plenty of other memory-safe (GCed) languages in which people find debuggers useful (such as Java). "The only thing that really goes wrong in normal code (once it compiles) is 'why is this value not what I expect?'" is arguably true there as well.
And, for the record, gdb works decently well with Rust code. Not perfectly (yet) but well enough to be useful. I have tried it (although I'm more of a printf debugger myself).
1.) Ability to see the value of every variable in scope without needing to decide a-priori which variables are worth looking at.
2.) Ability to traverse the call-stack and identify at what point a computation went wrong without having to instrument every single call & variable.
3.) Ability to interactively try out new code within the context of a stack frame. When I find a bug, oftentimes I'll try 3-4 new approaches just by entering watch expressions until I find an algorithm that works well on the data. This would take 3-4 full runs without the debugger.
4.) Ability to set conditional breakpoints and skip all the data that's working properly, only stopping on one particular record. When your loops regularly have 100k iterations before they fail on one single iteration, that's a lot of log output to sift though (or a lot of unnecessary loop counters & if-statements) for a rarely-encountered case.
Don't get me wrong, they're handy but I find them much more useful for stepping flow than root-causing errors.
Also if you're dealing with race conditions the only way to safely root-cause to to stash away data somewhere in mem and print it later as flushes/fences/etc change behavior. Debuggers make that even worse.
Love my debuggers for behavior issues but each tool has it's place.
The comparison with Java is interesting. With Java, I have often found that errors occur in a rather non-local fashion, due to dynamic code loading, confusing inheritance trees, and ubiquitous mutations and what have you. Maybe I'm not actually calling the function I thought I was, maybe because I have actually received a subclass of my expected class. Print-debugging is often too narrow to highlight the cause. In such a situation, I would fire up the debugger and inspect the general state of the application (which Java makes relatively easy to do).
In contrast, in Rust things tend to happen in a very constrained fashion. You can't randomly mutate things, you can't (without considerable effort) make complicated graph structures where everything can touch everything else. With the occasional exception of highly generic code, your call sites and function arguments are exactly what you expect. So I can rely on print-debugging to quickly find the cause of my problem.
Incidentally the same is true with Haskell, moreso even, except due to laziness the evaluation order can be harder to ascertain - debug statements can appear in a strange order (or not at all).
You are being overly-pedantic in your interpretation of the comment you are responding to. It isn't claiming that we have absolutely no undefined behaviour or memory errors in rust. The point is that undefined behaviour and memory errors are rare in Rust development, so tools intended to help find memory errors are just a lot less useful.
Your second point is spot on.
The best description I've heard for master-level debugging is that it's a process of narrowing down the problem space as cheaply as possible. Your brain is telling you that based on everything you 'know' about the code, the right answer should come out. If the wrong answer is coming out, something you 'know' is wrong.
After the most obvious failure mode doesn't reveal the problem, your next check may not be the second most obvious failure. Instead you're multiplying the cost of verifying an assumption times the likelihood it's correct times the 'area' of the problem space it eliminates. Checking things like "is it plugged in?" sounds stupid but brings down the worst-case resolution time by hours.
Long story short, let's say I'm sitting in an interactive debugger looking at a stack frame, expecting that a particular variable has the wrong value, but it's fine. The cheapest thing for me to do next is to look at all of the neighbors of the suspicious value, and those in the caller and on the return. With println, pretty much every subsequent check costs the same amount as the first one. And if there's no short path from starting the app to running the scenario, that cost could be pretty high.
If you believe that you have a high success rate on your first couple of guesses, then println works great for you. But what if you're wrong? Have you ever tracked how many attempts it usually takes you? Or are you too wrapped up in the execution to step back and think about how you could do better next time?
Also, I want to be clear that I'm not telling anybody how to debug, as long as you aren't making that choice for your whole team. Don't choose tools or code conventions that break interactive debugging because "println was fine for grandpa so it's good enough for me!" That's a big ol' case of Chesterton's Fence.
You write tests? Consider selecting variables of interest (and printing them to STDERR when debug mode is on) like one of many test.
Looking at memory and all the variables has its place, but as you said only "here and there" - because when you have to do that, you have already lost: you are looking for a needle in a (hay)stack, and will lose much more time that just eyeballing the variables of interest you selected before.
Thank you for sharing your hacks!
Perhaps everyone being a print debugger in Rust is less a compliment to the language, but a criticism of the tooling. I absolutely adore Rust, but understand there are still some vast gaps in the tooling.
Maybe that is a missing niche of the market; a debugging protocol similar to the language protocol used in VSCode (RLP in Rust provides this).
Then the IDE could integrate with any language and debug it, regardless of the details on how the language functions. And it can provide a better UI than GDB (which isn't a high bar, it's more like trying to dig down to find the bar because GDB UI is horrid)
I think the big selling point of jemalloc when it appeared was that it was much better for multithreaded applications. But since then glibc has improved a lot in this area, and nowadays has a similiar design with per-thread pools etc.
Does any other language have a similar feature?
Except you often don't want a full execution, you often just want a partial execution where you suspect the problem arises. I can assure you that a good UI debugger is extremely helpful. Command line debuggers less so.
> [src/main.rs:4] x = 5
How to differentiate the value of 'x' for a given thread/instance/whatever? Don't add the info by hand to the debug message.
edit: typo
That just means your debugger has a prohibitively high cost to use. If it takes more than 2 clicks to launch a full debugging session of your project, you need a new IDE.
With printf debugging, you can put print statements everywhere you think something might be wrong, run the program, and quickly scan through the log to see if anything doesn't match your expectations.
Sometimes, print debugging is the only practical way to fix a bug. For example, very rare bugs which can only be reproduced by running many instances of the code for a long time, or situations where attaching a debugger is not feasible (as in live services).
The dbg! macro is definitely inspired by https://hackage.haskell.org/package/base-4.12.0.0/docs/Debug....
I haven't seen a debug function that returns the value again anywhere else.
Prints a YAML dump of "my str", followed by file/line number information, then returns its argument so it can be embedded in expressions:
$ perl -MXXX -E 'say uc(WWW("my str"))'
--- my str
...
at -e line 1
MY STR
Data::Dump's "ddx" (1996) is also commonly used for print debugging, except it doesn't return the argument. dbg!(n * factorial(n - 1))
shows up as: [src/main.rs:5] n * factorial(n - 1) = 2
[src/main.rs:5] n * factorial(n - 1) = 6
[src/main.rs:5] n * factorial(n - 1) = 24
Although, IIRC there was some crazy dumper module by dconway (who else) that worked similar to this, but I don't recall if it returned the value....
So, I just looked it up, and I found it. Actually, Damien wrote two. One that he updated from 2014 to 2016, and one he started in 2017 and has maintained to the present. I have no idea the reason for that.
Yep leave it Damien. I remember he did something similar for a test module where it would show the expressions that were evaluated in the test failure output.
With good support, a couple of trace points, even on a live running instance is all that is needed, without any extra recompiles.
More devs should learn about JTags, IntelliTrace, DTrace, ....
No need for manually writing printf-debugging and recompiling all the time, when the debugger can do that for you.
I would hope at some point you would get to the point where you move past this, once you get to Enterprise scale print debugging stuff would be slow and laborious.
Also, if I want to pause at a point and step from there, just drop `import ipdb; ipdb.set_trace()` at the line I want to set a breakpoint.
Do you mean someone could get confused because they think 1 byte has endianness? Well then maybe they shouldn't touch these functions since they don't understand what is endianness.
It's weird that they have these methods on a u8 though. Probably just comes automatically because it's an integer type so they prefer to have a consistent API?
It seems to come from a macro used to define those primitive int types: https://doc.rust-lang.org/stable/src/core/num/mod.rs.html#38...
It isn't unheard of for 1 byte to have endianness. To be specific, we can call it "bit endianness". It matters when serializing bits to go over a wire, such as when bit-banging I2C or SPI.
An array doesn't normally have endianness either. I guess you could have a programming language that does the indexing backwards, with 0 at the end of the array. I've never heard of such a thing existing.
Compare this with a endianness functions typically used in C. We get ntohl for example. The size is specified by the "l", the argument is of type "long", and the return value is of type "long". No bytes are involved in that interface.
Compare with what the Linux kernel uses. Again, no bytes are anywhere to be seen. Functions like cpu_to_le32 take and return 32-bit values.
It looks like Rust is doing things Python-style, which is a scary thought. Instead of providing distinct ways to change endianness and to interpret data as integers, the functionality is crammed into one interface.
I read somewhere in all the discussions that Carl was hesitant to go all-in on futures 0.3 (those are definitely landing in std, right?) in tokio before some additional ergonomics had landed, possibly some language feature among them but I may misremember...
Do you know the list of things and possibly their tracking issues? Futures in std, async/await, etc, something something more? Would love to be able to keep track and follow along :)
> (those are definitely landing in std, right?)
Yes.
> Do you know the list of things and possibly their tracking issues?
You probably won’t get a 2x boost from it. But you might notice some improvements. It’s worth trying if you can test it quickly.
Software best practices are like flossing: good in the long term, but probably not crucial to keeping your bite.
Don't get me wrong, dbg macro isn't one of them. Heavy user of print debugger here.
I'm also quite happy with the uniform path work that's landed. Rust now has easily one of the best namespace management's I've used in a programming language.
Cheers!
The URL template for normal rust installers is:
* https://static.rust-lang.org/dist/rust-1.32.0-{TARGET-TRIPPLE}.{EXT}
* https://static.rust-lang.org/dist/rust-1.32.0-{TARGET-TRIPPLE}.{EXT}.asc
The URL template for additional compilation target installers (`x86_64-unknown-linux-musl`, `wasm32-unknown-unknown`, ..etc) is: * https://static.rust-lang.org/dist/rust-std-1.32.0-{TARGET-TRIPPLE}.{EXT}
* https://static.rust-lang.org/dist/rust-std-1.32.0-{TARGET-TRIPPLE}.{EXT}.asc
To avoid a very long post (and a lot of scrolling), the list of links to all target installers supported by rust has been omitted from this post. Refer to the complete list of supported platforms in https://forge.rust-lang.org/platform-support.html.The file extension for auxiliary target installers is `.tar.gz` (or `.tar.xz`) for all targets including Windows.
Note: Due to a known bug, browsing the complete list of all installers is not available on https://static.rust-lang.org. It is however still possible to access dated repositories via the following URL template:
https://static.rust-lang.org/dist/YYYY-MM-DD/
Installers for the current stable release of rust can be browsed at https://static.rust-lang.org/dist/2019-01-17/
Cheers!
Yes, for multi-target (eg lib + bin, ..) it can be a little more work to look it up.
But the new system is SO much more convenient to use overall.
To give a concrete example, please look at this [1] Cargo.toml and this [2] example and try to figure out the crates its using. I've highlighted them in Cargo.toml, to show how irregular the use is [3].
We don't have test-only or binary only dependencies yet. They would make this a bit easier.
I want to focus on the code I'm writing, not onto figuring out which crates I need to include. On the bright side though, the system is still better than what C++ got which is not knowing from which header a particular symbol came from :).
[1]: https://github.com/djc/quinn/blob/e555d11a430b9760d149a27659...
[2]: https://github.com/djc/quinn/blob/e555d11a430b9760d149a27659...
[3]: https://gist.github.com/est31/7cb33e8a6b63c8798381bdb5e91a14...
I also wish we could specify /examples or /tests specific dependencies in Cargo.toml.
A question here is though if such a complex structure is so rare that making it easier for the 90%+ of crates is worth it, while inconveniencing the complex cases.
x = foo();
invoke_debugger(); // An interactive console appears here.
y = bar(x)Well, the trouble is knowing if you need to spawn a debugger. On Windows, there's a nice API for checking if you're being debugged (IsDebuggerPresent). On Linux, you have to read /proc/self/status to see if there is somebody running ptrace on you. On other Unixes, well, the only trick is trying to spawn another process to ptrace you and hope it works.
It is possible, just tricky.
I'd expect something like this: https://github.com/krixano/Lydrige to have it easy in this regard - it's a statically-typed but interpreted language.
Mostly, if I want a break somewhere I do this:
*(int*)0 = 1;
Which will result in a crash, I attach my debugger, run the code and if it reaches that line it will break and I can inspect everything I want.It's easier than adding a __asm(int3) because it's cross platform. On windows for example it's not possible to perform inline assembly in x64 builds, and compiler intrinsics are not portable.
If you have JIT debug enabled, you will be prompted to attach your debugger to the process when hitting such a breakpoint.
> literal mactches against literals of any type;
... of course it is. Noted for next time ;)