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?
-g
adds debugging information (variable names, line numbers etc) to the executable file. It's part of what you need to do to be able to make sense of the core file.http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging-Options
core file is generated on Segmentation Fault or such exceptions.
gdb source.cc core
is one way to look into the core file. Backtrace and investigating every frame is a start at looking into the core.-g
adds debugging symbols in the binary.The -g flag tells the compiler to generate debugging information. It has no impact on whether or not a core file will be generated. On most unix-like systems, that can be setup using the ulimit command.
If you don't put -g flag, cannot call list in gdb to list what the source code looks like. it will show "No symbol table is loaded. Use the "file" command."
Also if you type info func or info frame, info locals in gdb, without -g it will not show return datatype and its arguments, basically no translation of instruction to variable (mapping from symbol table).
core dump is one of a process´s default actions, when this process receives the signals, e.g. in the standard signals "SIGQUIT", "SIGILL", "SIGABRT", "SIGFPE", "SIGSEGV". However, most shells suppress the creation of core file, simply because the core files tend to be large and it can take some time or a lot of time.
in order to enable the core generation, "ulimit" is the utility that you can use to set the shell or its children process´s file limit.
compiler flags "-g" or whatever only concern with compiler. logically speaking, it has nothing to do with core dump.
That's kind of right, but incomplete.
-g
requests that the compiler and linker generate and retain source-level debugging/symbol information in the executable itself.If...
kill -SIGQUIT
pid), 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_num
isB
being a global with implicitly zero-ed out memory, whileNS::ns_my_num
isD
as the executable has to explicitly provide the value2
to occupy that memory.The man/info-page for
nm
documents these things further....