I am writing a C++ server program that will be deployed to *nix systems (Linux/macOS). The program sometimes runs into segfault in production environment, and I would like to collect some information like core dump file. But I have no idea what the best practice is for doing this:
- I would like to make the program perform best.
- I would like to analyze the core dump in production env offline if it really happens.
I learn that there are some things I could try:
- There is
RelWithDebInfo
andRelease
forCMAKE_BUILD_TYPE
, but it seems they have different optimization level. So I assumeRelWithDebInfo
build performs not as well asRelease
build. (“RelWithDebInfo uses -O2, but Release uses -O3“ according to here What are CMAKE_BUILD_TYPE: Debug, Release, RelWithDebInfo and MinSizeRel?) - Tools like
objectcopy
/strip
allow you to strip debug information from a binary (How to generate gcc debug symbol outside the build target?) - Printing stack trace when handling
SIGSEGV
signal (How to automatically generate a stacktrace when my program crashes)
I am new to deploy a production C++ server program, and I would like to know the answers for the following questions:
- What is the recommended build type to use in this case,
RelWithDebInfo
orRelease
? - Compared to choosing different build type, when do I need to use tools like
strip
? - If I create a
Release
build binary for production deployment, when theRelease
build generates a core dump in production environment, can I later use the using the same revision of source code to build aRelWithDebInfo
binary and use (gdb +RelWithDebInfo
binary +Release
build core dump) for core dump analysis? - Is it common to turn on core dump in production env? If it is not a good practice, what could be the recommended approach for collecting information for troubleshooting, printing the stack trace when crashing?
In general, I would like to know how C++ programs is recommended to be build for production, allowing it to be best optimized while I am still able to troubleshoot it. Thanks so much.