News for hackers. Stuff that matters.
Re:The Sad State of Debug Performance in C++ by glandium on Monday September 26, 2022 @05:21AM (#32981013)
> Even if -Og was ubiquitous, it is still suboptimal to -O0: it can still inline code a bit too aggressively for an effective debugging session.

IMO, the problem is not so much the inlining, but the sad state of the optimization passes losing track of many things and producing useless debug info.

Re:The Sad State of Debug Performance in C++ by usefulcat on Monday September 26, 2022 @09:07AM (#32982636)
It's not just inlining. With -Og (clang or gcc), the debugger frequently can't even tell me the value of function arguments because they've been "optimized out".
Re:The Sad State of Debug Performance in C++ by adrian_b on Monday September 26, 2022 @09:50AM (#32983099)
While I agree with you and with the OP and I have also been sometimes annoyed by this kind of debugger messages, the more universal solution than wanting an improved debugger is to rely for debugging on some form of progress and error logging in your program, which will work at any optimization level.

Adding such debugging aids in your program may need some extra work, but it also functions in all cases when using the debugger is hopeless, e.g. for concurrent programs or other programs that depend on real-time interactions which prevent the use of breakpoints.

Re:The Sad State of Debug Performance in C++ by usefulcat on Monday September 26, 2022 @02:46PM (#32986710)
Yes and no. I use logging too, but that's only useful when I know (or can guess) ahead of time what locations and what things at each location I'll need to know in order to diagnose the problem.

Of course I can always go back and add some logging, but it's not a given that I'll want to keep it around after the problem is fixed. For example if it's in a performance-critical section of code, even the cost of checking to see whether logging is enabled may be too great. And in any case every log statement does add visual noise to the code.

Re:The Sad State of Debug Performance in C++ by SuperV1234 on Monday September 26, 2022 @12:30PM (#32985134)
Just want to remind you that `-Og` in Clang is not representative here, as it's exactly the same as `-O1` -- they implemented support for the flag just to ease transitions between GCC and Clang without having to filter out flags, but it doesn't provide any debugging-specific advantage.
Re:The Sad State of Debug Performance in C++ by not2b on Monday September 26, 2022 @03:43PM (#32987383)
Yes, those who think -Og is no good because they are clang users should perhaps file bug reports or contribute improvements instead.
Re:The Sad State of Debug Performance in C++ by SuperV1234 on Monday September 26, 2022 @03:45PM (#32987417)
Most game developers have to target Windows, and the main compiler there is MSVC. The other viable alternative is `clang-cl`, which doesn't have `-Og` either.

Bug reports have been filed, especially against MSVC, but they have been largely ignored over the years. AFAIK, MSVC is not open-source so improvements cannot be contributed either.

Re:The Sad State of Debug Performance in C++ by MauranKilom on Monday September 26, 2022 @04:22PM (#32987835)
Am I alone in always debugging in release builds? If you get a crash dump from prod, it'll be a release build. Or if your reproducer takes 2 minutes of (release build!) setup time, you don't want to go through that in debug builds.

Yes, the debugger might have "forgot" the value of that function argument. So check a few other levels of the call stack that it was passed through. Or a local variable might be unavailable in a given line. So have breakpoints a bit before, or trace it into the next function call.

And when all else fails, you can go and look at the disassembly. Yes, calling conventions are stupid, instruction mnemonics are a hassle and register aliases are terrible, but even with rudimentary understanding you can generally figure out what's going on. If your code does a multiplication, look for the `mult`. If your code calls some external function, look for a `call`. Compilers can do cool things with your code, but computers are not magic - you can actually just look at what your CPU does, and it will strongly correlate with your code. As an added bonus, this helps you actually understand what code your compiler produces, which (if you reach for C++!) you probably should care about to some degree.

The only reason I have to care about debug builds is code coverage and/or iterator-checked runs, but that's something that can run overnight if it has to.

/rant

Re:The Sad State of Debug Performance in C++ by menaerus on Tuesday September 27, 2022 @06:22PM (#33001913)
You're not. Debug builds are rarely that useful and tbh I don't understand why is there such a big fuss about it. The most difficult and most time consuming problems found in production code can only be debugged and, well, understood and hence fixed in production (optimized) code only.

That said, under assumption you have a good code coverage with tests, and your code uses a lot of asserts to check pre- and post-conditions, debug builds can be useful for early detection of broken invariants.

I get that the development cycle is shortened through shorter compile times but that is simply the artifact of all optimizing compilers and not something which is exclusive to C++. And luckily this can be easily alleviated.

Expecting to have comparable runtime performance (which one is it?) in languages backed with optimizing compilers seem a bit delusional to me and even more so if you're in the realm of soft real-time systems.

Re:The Sad State of Debug Performance in C++ by slaymaker1907 on Monday September 26, 2022 @04:19PM (#32987808)
I feel your pain. In those cases, you have to try and figure out what the assembly is actually doing and how it relates to the original source code.
Re:The Sad State of Debug Performance in C++ by DannyBee on Monday September 26, 2022 @08:22AM (#32982208)
Eons ago, i wrote a lot of the first version of debug location tracking/location list tracking in GCC, and the support for evaluating them in GDB (plenty of others came along and improved it nearly instantly - it thankfully did not last long alone ;P).

I have spent, over the years, an amazing amount of time on trying to improve optimized-code debug info.

You could and should improve the optimization passes, but at various points in life they were actually very good. It is always a dance between advancing optimization and debug info, and i certainly will not claim that debug info is thought about as much as it should be - it is often fixed later, as i'm sure you expect.

But at the same time, what the author amounts to ends up reducing to the following:

1. The output executable is pretty well optimized

2. The output debug info is a recreation of the program as-written that can be executed by a debugger

3. The mapping between #2 and #1 has no holes.

IE #2 is basically "you've output the entire program using dwarf5 as a target processor".

This is never going to be a thing that regular users strive for, for lots of reasons. So if the game companies or whoever want this to be the case, they will actually have to contribute more than just bug reports - because it's not something that drives compiler usage for any language, whether it's C++ or anything else (IE it's hubris to believe that other languages won't end up the same way as they get better at optimizing. C++ just has a long head start)

At the same time I am sure that just one good compiler person paid for a year working on any of the compilers to improve optimized debug info would get to a really good place. I know - i was once paid to do it, and when i stopped, it was, back then, in a pretty darn good place compared to where we started[1].

Rather than throwing away every abstraction, it's hard to believe this entire industry can't afford this. Yet I have not seen them try before (perhaps they did after my time, but my time covered 20 years, and it didn't really happen during then)

So it is 100% reasonable to blame game developers or whoever as a group - because as a group, they aren't (again, AFAIK) doing anything about it other than writing blog posts about how the language is mean to them, and filing bugs that compilers are mean to them.

[1] I'll note, in practice, the most effective route over time is likely actually to just store enough info to do deoptimize during debugging as-needed, which is the path various VM based languages take and works well. The traditional path to doing this is to JIT the language along the way (which is totally possible for C++), but it's not strictly necessary to do it that way.

The harder part is the debugger/compiler cooperation, not the runtime mechanism.

Re:The Sad State of Debug Performance in C++ by dannas on Monday September 26, 2022 @09:39AM (#32982982)
[gcc/gdb/LLVM outsider here]

If someone is interested in what's being done to improve LLVMs debuginfo, here's a talk by Djordje Todorovic from two years ago: https://www.youtube.com/watch?v=GpMLt1oecOk. It's about tracking dwarf locations for variables by keeping around information from the parent frame. According to that presentation, at least that work has been progressing.

Re:The Sad State of Debug Performance in C++ by dannas on Tuesday September 27, 2022 @08:56AM (#32994690)
As for contributions from the gamedev industry, here's a EuroLLVM 2022 presentation from Orlando Cazalet-Hyams working at Sony: https://www.snsystems.com/technology/tech-blog/improving-deb...
Re:The Sad State of Debug Performance in C++ by titzer on Monday September 26, 2022 @10:47AM (#32983836)
The C/C++ ecosystem has really been dragging down the popular expectation of what debugging (and at what level) is possible. They are uniquely bad in breaking programmers' ability to understand what their programs do, with debugging being an essential part of that.

JVMs move heaven and Earth to make optimization not observable and support the JVMTI, upon which debuggers are built. They are required to materialize a bytecode-level view of execution state upon demand, just as CPUs are required to materialize a machine-level view of execution state. As a result, any source debugger written against what is available at the bytecode level will work without needing to see through the abstractions.

Re:The Sad State of Debug Performance in C++ by khuey on Monday September 26, 2022 @09:04AM (#32982596)
Thanks for doing that work. Even many years later gcc is in a noticeably better place than clang when it comes to how much debug info is preserved by the optimizer. https://robert.ocallahan.org/2018/11/comparing-quality-of-de...
Re:The Sad State of Debug Performance in C++ by likpok on Monday September 26, 2022 @10:36AM (#32983656)
It strikes me that (2) isn't necessarily even what you want. Optimization will change the character of the program, often radically. Should the debugger reflect the source code says it should do or what the optimized code is actually doing?

This is apparent in C++ when you look at the optimized output on Compiler Explorer: it attempts to color assembly based on the source lines, and some source lines get strewn about the resulting assembly.

Re:The Sad State of Debug Performance in C++ by jmorse2 on Monday September 26, 2022 @10:26AM (#32983529)
> they aren't (again, AFAIK) doing anything about it

Person slightly doing things to LLVM checking in -- the three points you list are compelling, because it's always going to be difficult to describe one program (source) in terms of a vastly different (optimised) program. Do you think there's mileage in showing the developer a partially optimised program [0] instead of trying to perfectly describe the source? It'd be easier to describe in debug-info, and possibly easier for developers to identify unexpected changes to the code they wrote.

[0] "Non-Transparent Debugging of Optimized Code", 1999

Re:The Sad State of Debug Performance in C++ by tialaramex on Monday September 26, 2022 @09:54AM (#32983144)
> they aren't (again, AFAIK) doing anything about it other than writing blog posts about how the language is mean to them, and filing bugs that compilers are mean to them.

There are people from that industry or adjacent industries writing other languages. Most obviously Jai (Jonathan Blow's language, Jonathan did "Braid" and "The Witness") but you could also argue for Ginger Bill's Odin. I suppose in this context it will be interesting to see whether the debug story is any better in these languages (neither is yet finished).