3

I want to know what each flag does to the code to understand its overhead. I checked

man g++ for the '-g' flag

and found only "Generate extra code to write profile...."

It doesn't specify where?

Are their any detailed manuals to help me understand what -g/-p do?

banarun
  • 2,305
  • 2
  • 23
  • 40
Syntax_Error
  • 5,964
  • 15
  • 53
  • 73
  • http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html – devnull Jun 20 '13 at 11:13
  • Wait, save bandwidth. `info gcc` should also help. – devnull Jun 20 '13 at 11:16
  • The thing is they provide this statement: "Generate extra code to write profile information suitable for the analysis program prof" Non are providing information about "what type of information is suitable for analysis" – Syntax_Error Jun 20 '13 at 11:24
  • 1
    possible duplicate of [How does gcc's -pg flag works?](http://stackoverflow.com/questions/7290131/how-does-gccs-pg-flag-works) – devnull Jun 20 '13 at 11:27

3 Answers3

7

"Generate extra code to write profile" is the -p option, and that will add extra code. The extent of that extra code is at least one register load and one function call at start and end of functions, but it does depend on the architecture. This may in turn affect other optimisations during compilation, such as availability of registers, inlining and when actually running the code it may affect cache-hit rates/miss-rates, and aside from the direct effect of executing those extra instructions.

The -g option does not in itself add extra code, it just produces debug symbols that become part of the executable file so that a debugger can figure out where the functions, variables, etc are when you use the debugger.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • +1: Specific example, and a note that it's by no means a dead cert. – Lightness Races in Orbit Jun 20 '13 at 11:26
  • Thanks for the informative answer. So can I safely assume that the execution time will increase as the number of functions in a code increases? Or will it also increase with other parameters? – Syntax_Error Jun 20 '13 at 12:43
  • 1
    If you have some code that currently uses, say 3 functions all called in order from main 1M times, and you refactor the code into 8 functions, and all those functions are called 1M times, yes. If you have more functions, but they are not called as many times, then not necessarily. Each time a function is called, the "statistics counting" function gets called - how much overhead that is depends HIGHLY on what your functions do, and how many times they are called. – Mats Petersson Jun 20 '13 at 12:49
5

Interesting question, and it's a bit hard to find information if your starting point is the gcc man page :-) so here goes.

Basically the -g flag writes extra "debugging" information right into the generated object files (.o) and executable file. This extra information can then be used by a debugger (say gdb) to help make sense of what's going on for the person doing the debugging.

So for example if you have a variable name that will be kept around as extra information so that when you use a debugger you can refer to the variable that you used in your source code rather than some random memory address because symbol debug information wasn't there.

The debugging options are somewhat explained in the gcc manual here

However I think you need a debugging intro. So have a look at the GDB introduction by UWA to get a better understanding of what's going on.

The same goes for profiling data. -p adds extra information in the executable so that a profiler like prof can trace the software as its running and tell you where it spends most of its time (what loops/functions etc) and how (in)efficient a program is. :-)

related things to read up on

There is a difference between symbol information in a file and debugging information. Once you start looking at linking you'll run into symbol resolution.

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
0

The thing is they provide this statement: "Generate extra code to write profile information suitable for the analysis program prof" Non are providing information about "what type of information is suitable for analysis.

It's abstracted away from you, because you do not need to know.

If you deem otherwise, examine the compiler's source code.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055