This question was asked to me in an interview.
they asked me how to generate a core dump file with which i can debug.
then i said that with -g flag in gcc we can do it.
then they have asked me what exactly does that -g flag do to the compiler.
i said (probably a wrong answer) that it will open up all the symbols in the core file which can be used for debugging.
can anyone tell me what exactly does it do?
That’s kind of right, but incomplete.
-grequests that the compiler and linker generate and retain source-level debugging/symbol information in the executable itself.If…
kill -SIGQUITpid), orabort)…- none of which are actually caused by the use of
-g– then the debugger will know how to read that “-g” symbol information from the executable and cross-reference it with the core. This means you can see the proper names of variables and functions in your stack frames, get line numbers and see the source as you step around in the executable.That debug information is useful whenever debugging – whether you started with a core or just the executable alone. It even helps produce better output from commands like
pstack.Note that your environment may have other settings to control whether cores are generated (they can be big, and there’s no general way to know if/when they can be removed, so they’re not always wanted). For example, on UNIX/LINUX shells it’s often
ulimit -c.You may also be interested to read about DWARF Wikipedia – a commonly used debugging information format for encoding the embedded debug/symbol information in executable/library objects (e.g. on UNIX and Linux).
UPDATE per Victor’s request in comments…
Symbol information lists identifiers from the source code (usually only after any name mangling needed), the (virtual) memory addresses/offsets at which they’ll be loaded in the process memory, the type (e.g. data vs. code). For example…
Notes:
f()andmain()are typeT(which stands for “TEXT” – used for read-only non-zero memory content whether it’s actually text or other data or executable code),g_my_numisBbeing a global with implicitly zero-ed out memory, whileNS::ns_my_numisDas the executable has to explicitly provide the value2to occupy that memory.The man/info-page for
nmdocuments these things further….