Home > Mobile >  Is there a way to embed a string in C source code that will show up in a core dump?
Is there a way to embed a string in C source code that will show up in a core dump?

Time:10-09

With lots of versions of binaries, if I receive a core dump, I currently rely on the sender telling me the version they were running so I can match it with the right source/symbols that built it. They are often incorrect and much time is wasted. Even asking them to send the binary with the core is error-prone. Is there a way to embed a version string into the source code that I can look for in a core dump? Or is there some kind of hash/signature I can use to match it from a build? Or is there a better convention?

CodePudding user response:

Can your main call a generated int build6278() function which then call into you real entry point? Your build system would need to generate this stub with a different name per version.

Then you just need to look up the callstack and find this function name. This assumes a build with symbols.

Alternatively, using your suggestion:

int main()
{
    const char* version = g_Version; // that symbol will have to be added by your build system
    std::cout << g_Version << std::endl;
    return regularMain();
}

should allow you to check the local version pointer in the crash dump.

CodePudding user response:

I currently rely on the sender telling me the version they were running so I can match it with the right source/symbols that built it.

Since you are talking about core dumps, it seems exceedingly likely that you are using an ELF platform (probably Linux).

On an ELF platform, the standard way to identify a binary is to use --build-id linker flag, which embeds a unique checksum into the binary. This is likely passed to the linker from g by default, but you can explicitly specify that flag with -Wl,--build-id as well.

Linux kernel saves the page containing the NT_GNU_BUILD_ID ELF note, which allows you to

  1. Immediately identify what binary produced the core (using e.g. eu-unstrip -n --core core) and
  2. Use GDB to automatically load correct debug info. See e.g this article.
  • Related