-1

I understand the general concept of using optimization flags like -O2 and ending up having had things optimized out, makes sense. But what does it mean for the 'this' function parameter in a gdb frame to be optimized out? Does it mean the use of an Object was determined to be entirely pointless, and that it, and the following function call was elided from existence? Is it indicative of a function having been inlined? Is it indicative of the function call having been elided?

How would I go about investigating further? This occurs with both -O0 and -Og.

If it makes any difference, this is with an ARM process. I'm doing remote debugging using GNU gdbserver (GDB) 7.12.1.20170417-git and 'gdb-multiarch' GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1.

JoeManiaci
  • 435
  • 3
  • 15
  • 2
    Giving us an example could help - however, likely the operation could be completely removed. – lorro Nov 09 '22 at 23:19
  • I'm with @lorro here, but it likely means that, in any particular function, `this` is not needed at and beyond the point in the flow of execution where you see that message. Specifically, it's only needed to access instance variables or call another function of the same object. – Paul Sanders Nov 09 '22 at 23:24
  • If I knew how to reproduce in a small example I would, but I'm trying to make sense of what appears to be an infinite vtbl lookup and call loop and I noticed the this= in the middle of it all. It's in a rat's next of legacy code with oodles of multiple inheritance. It's terrible. If I had the time I'd try to replicate the various multiple inheritance small scale to see if it's simple poor code, but I might not have that time. – JoeManiaci Nov 09 '22 at 23:33
  • The major point of `-O0` is to not optimize out anything except dead code, like behind an `if( false )` or equivalent. [Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?](https://stackoverflow.com/q/53366394) . It's very normal to see "optimized out" with any other optimization level, even `-Og` unfortunately, as GCC transforms your code to use temporaries it invented instead of the actual C++ variables, even if it uses them in the same way as the named local vars. – Peter Cordes Nov 09 '22 at 23:42
  • 1
    I'd be very surprised for `this` to be "optimized out" at `-O0` (consistent debugging); even with `__attribute__((always_inline))` I'd be surprised. `-O0` won't optimize away other unused args. [mcve] please. Unless you were single-stepping into non-template library functions with debug info for libstdc++ installed? It will have been compiled with at least `-O2`. I assume you're using some version of GCC or clang since you're on Ubuntu, but you didn't say which, only the GDB versions which just read the debug info compilers create. – Peter Cordes Nov 09 '22 at 23:45
  • It definitely is occurring with -O0 and -Og, I also forgot we also have -fno-inline as part of the compile flags. So I guess we can take inlining out of the equation. If I could make a virtual function volatile I would. I'm curious if there might be a pragma push/pop I can wrap around this function just to see what would happen. Definitely not stepping through stdlibs or templated code, just terribly written code. – JoeManiaci Nov 09 '22 at 23:50
  • Mismatched gdbserver and gdb might cause some strangeness, but I would not have expected it to. – jxh Nov 10 '22 at 00:34

1 Answers1

1

But what does it mean for the 'this' function parameter in a gdb frame to be optimized out?

It means that GDB doesn't have sufficient debug info to understand the current value of this.

It could happen for two reasons:

  1. the compiler failed to emit relevant debug info
  2. the info is there, but GDB failed to understand it

GCC used to do (1) a lot with -O2 and higher optimization levels, but that has been significantly improved around 2015-2016. I have never seen <optimized out> with GCC at -O0.

Clang still does (1) with -O2 and above on x86_64 in 2022, but again I've never seen it do that at -O0.

How would I go about investigating further?

You can run readelf --debug-dump ./a.out and see what info is present in the binary. Beware -- there is a lot of info, and making sense of it requires understanding of what's supposed to be there.

Or you could file a bugzilla issue with exact compiler and debugger versions and compilation command, attach a small binary, and hope that someone will look.

But first make sure you still get this behavior from the latest released version of GCC and GDB (or the current tip-of-trunk versions if you can build them).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • So we are using an GCC Linaro 7.1.1 I think? We were working towards updating to 9.3. I'd love to get us higher. I've actually been wanting to dig in deeper into this sort of stuff but am definitely finding reading materials/tutorials lacking. – JoeManiaci Nov 10 '22 at 18:43