If you compiled original executable with e.g. g++ -O2 ...
, you can not (as you probably have discovered) use a new executable built with g++ -g ...
to debug the core
-- GDB needs the symbols to match, and they would not (due to difference in optimization levels).
What you can do is build the new executable with the same optimization as the original, but also with debug symbols: g++ -O2 -g ...
.
After you've built a new executable, run nm old.a.out > old.nm
, nm new.a.out > new.nm
and compare the outputs. They should be identical or very very close.
If they are, you should be able to debug a core
produced by old.a.out using new.a.out.
In the future, you should always build your executable with debug symbols, then keep the full copy, but ship a copy without debug info:
cp a.out a.out.debug
strip --strip-debug a.out
# a.out is now good to send to customers
# keep a.out.debug for future debugging