Wireshark-dev: [Wireshark-dev] Re: Preventing "optimized out" parameters while debugging

From: John Thacker <johnthacker@xxxxxxxxx>
Date: Sat, 31 Oct 2026 07:43:34 -0400
On Sat, Oct 31, 2026 at 2:47 AM Jaap Keuter via Wireshark-dev <wireshark-dev@xxxxxxxxxxxxx> wrote:
Hi,

Occasionally, while debugging a GCC build, I run into “optimized out” parameters, which is annoying or sometimes hindering debugging.
Therefor I looked into the optimization setting applied during Debug build (using "-g"), and there’s none. Apparently, by default, this causes the GCC optimizer to ‘hide’ some of the information.
There is the flag “-Og” which prevents this behaviour, allowing parameters to remain, however, this exposes some more instances of suspected uninitialised variable use and clobbered variables.
I’ve managed to address these in master (MR !27457), so this branch is good to go.

Question is: should we add the setting of “-Og” to our non-MSVC debug build, in CMakeLists.txt. By the way, Clang does support the same flag, and I assume exposes the same behaviour, but I’ve not tested that.

Preventing "optimized out" parameters while debugging and preventing "maybe uninitialized" variables are at cross purposes here.

The Debug build type applies "-g" (generate debug symbols) and no "-O" option, which defaults to "-O0", which does no optimization. This leads to the least amount of "optimized out" parameters (zero):

However, "-O0", by doing no optimization passews, is also not able to apply "-fvar-tracking-assignments" and "-fvar-tracking", which means that it is unable to produce maybe-uninitialized warnings.

"-Og" does some optimization passes and optimizations, and can result in "optimized out" parameters when debugging (though considerably less than higher levels), but because it can track variable initialization, will give some maybe-uninitialized warnings that "-O0" does not, including in cases where "-O0" is in fact using a variable uninitialized.

The RelWithDebInfo applies "-O2" and "-g" and Release applies "-O3" - it's possible that the additional optimizations at higher levels can eliminate the use of maybe uninitialized variables entirely, which is why some warnings show up with "-Og" that do not at the other levels. It's also possible that it's a false positive at one setting, or a false negative at the other. It's yet still possible that some of the volatile qualifiers added by your change prevent optimization at higher levels that would completely eliminate the problem, even though a lack of those qualifiers means using an uninitialized variable at Debug / -O0. Sometimes unoptimized code can be "less correct" in the sense of being more likely to crash or otherwise fail (see also how the lowest level of optimization can do tail call elimination or inlining or other things that can prevent stack elimination.).


We could add a preset or similar that adds "-Og" as another option.

John