14

Does anybody experience mixing -g (debugging symbols) and -O2 (best safe optimization) with gcc compiler?
I have to debug crashes of a release program distributed to final users that could send me back the core file.
I've always used to call:

gdb << myprogram >> << core file >>

and see where the trouble is. Now I can just see the call-trace but having no debugging symbols I'm quite in trouble.

Any idea?

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
Mr.Gate
  • 419
  • 4
  • 12
  • `-O3` isn't supposed to be "unsafe". Sometimes compiler bugs exist in parts of the compiler internals that only run with `-O3`, but sometimes they exist in parts that run with `-O2`. You could say `-O2` is best "conservative" optimization, like minimal to no auto-vectorization, and without being as aggressive about larger code size. (Profile-guided optimization can help GCC know which loops are hot, and which loops are cold so should be optimized more for size.) – Peter Cordes Feb 01 '23 at 18:03

2 Answers2

13

It works fine.

Or well, due to the optimization sometimes the source you're stepping through with the debugger doesn't match up exactly with the source, but IMHO despite this having the debug symbols makes debugging much easier.

janneb
  • 36,249
  • 2
  • 81
  • 97
8

We use both together in production environment, which makes debugging a lot easier if the customer have only seen a crash once. It gives you a pretty good idea where the problem is (not if it was a memory corruption).

In theory adding -g shouldn't really affect the performance, although the executable gets large. In embedded environment it is a big trade-off.

tothphu
  • 899
  • 12
  • 21
  • 6
    The image with the symbols is only needed by the debugger. If you're using a remote debugger or doing post-mortem debugging from a core file, the image running on the target can have the symbols stripped from it. – Michael Burr Mar 05 '12 at 00:06
  • small point: if you use ccache to speed up building, -g (by default) forces ccache to take not of the path of the source files, so builds in different directories do no benefit from the caching. – Jack Wasey May 01 '18 at 20:52
  • Recent ccache has hints in docs to improve the situation, https://ccache.dev/manual/latest.html#_compiling_in_different_directories – user7610 Jun 22 '19 at 11:31