Wednesday, October 07, 2009

GCC -g option

Glynn Clements answer an email in the linux-c-programming mailing list to someone who ask about GCC -g option
Most the time I compile my application without the -g option due to performance reasons.

The -g switch has absolutely no effect upon performance. It simply causes and additional section to be added to the resulting binary. When the program is run normally (i.e. not under gdb), that section won't be mapped. The only downside to -g is that it increases the size
of the file.

However: debug information isn't necessarily much help if you compile with optimisation enabled, as the resulting machine code will bear little resemblance to the original source code. Statements will be re-ordered, many variables will be eliminated, etc.

Problem is that when it hits some bug and dumps core, this is not very useful because there is hardly any information in it. Is there some way to get some useful information out of the core file. For example one of my program crashed and with gdb I see the following: At least I know that the bug is in my function start_process. But is there some way to find out at what line it happened?

It isn't meaningful to talk about a "line" in the source code if you compile with optimisation enabled. However, you can tell gdb to disassemble the machine code for a particular function, and you can print the values contained in registers or at specific memory locations. Working out what that information means in terms of the source code is something which needs to be done manually.

Glynn Clements is an active member in the Linux's kernel mailing list, i love the way he answer a question.