Re: am i right

From: Jean-David Beyer (jdbeyer_at_exit109.com)
Date: 04/25/04


Date: Sun, 25 Apr 2004 08:35:21 -0400

Michael Heiming wrote:

>>>1.
>>>I compile my source code using gcc 2.95.3 and I use "-g" option along with
>>>O2 ( optimization flag ). I then "strip" the executable and give it a run.
>>>
>>>2.
>>>And then I compiled same source code _without_ "-g" option and "strip" the
>>>executable (even though not useful) and give it a run.
>>>
>>>I found that performance of the code is better when I dont use "-g".
>>>
>>>How is this possible ?
>>>Does -g hampers the -O2 level of optimations ?
>
> [..]
>
>
>>So the only problem with -g was larger object files, because the symbol
>>table was in them. But the OS did not load the symbol table (only gdb
>>looked at it), so it did not waste RAM with it.
>
>
> From 'man gcc', you get the feeling that albeit gcc can work with
> -g and -O, at the same time, unlike many other C compiler, it
> might return unexpected results.
>
Not the impression I get. What they are referring to is that _gdb_ may
return unexpected results, not that _the program_ will return unexpected
results.

Programmers frequently complained that the optimizer was buggy when it
was not. The rule for optimizers is that _for correct programs_ you will
get the same results whether optimization was used or not. It is silent
on what happens to incorrect programs.

Now there are a lot of incorrect programs out there that seem to work.
And when optimized, they work differently. This is most often because
values of variables were used that never had values assigned to them.
This happens when automatic register allocation is done, for example.
Remember the "register" statement in C for example? It turns out that
programmers almost always put the wrong variables into registers, and
that they do not overload registers. A good optimizer ignores register
statements and puts the "best" variables into registers. Furthermore,
the same register can be used for more than one variable (perhaps
because one variable needed it during one part of a function's execution
and another variable needed it during another part of the function's
execution). Now if someone uses a variable that has no value assigned
and does not optimize, they get the value in .bss memory or something on
the stack. If they optimize it and it gets assigned to a register, they
get whatever was left in the register, which may be different from
before. The programmer then complains about the optimizer when what he
should do is fix his program.

But as far as debugging is concerned, it is gdb that returns surprising
results. Say you put a breakpoint in a function and the function body is
loaded into memory even though it is sometimes expanded inline. Then if
a programmer sets a breakpoint in the function with gdb, it will stop
there only when the function body is executed, and not when the
inline-expanded instance is executed. So it will not stop.

Another case occurs when some code is totally eliminated (based on
live-dead analysis, for example) where the optimizer notices a value is
not used, so it deletes the code that computes its value. If you try to
put a breakpoint on it, gdb will refuse.

I could go on, but I hope I need not.

-- 
   .~.  Jean-David Beyer           Registered Linux User 85642.
   /V\                             Registered Machine   241939.
  /( )\ Shrewsbury, New Jersey     http://counter.li.org
  ^^-^^ 08:20:00 up 2 days, 9:02, 4 users, load average: 4.15, 4.13, 4.08


Relevant Pages

  • Re: am i right
    ... But the OS did not load the symbol table (only gdb ... Programmers frequently complained that the optimizer was buggy when it ... This happens when automatic register allocation is done, ... Say you put a breakpoint in a function and the function body is ...
    (comp.os.linux)
  • Re: am i right
    ... But the OS did not load the symbol table (only gdb ... Programmers frequently complained that the optimizer was buggy when it ... This happens when automatic register allocation is done, ... Say you put a breakpoint in a function and the function body is ...
    (comp.os.linux.setup)
  • Re: [RFC] [Crash-utility] Patch to use gdbs bt in crash - works great with kgdb! - KGDB
    ... understand why you wouldn't want to simply use gdb alone ... Note that this register image is in a different order than ... +enum dwarf_form ...
    (Linux-Kernel)
  • Re: Can someone spot the error here?
    ... at the bottom of a function, or as nearly at the bottom as possible. ... in a register, or even in a flag. ... the need for the programmer to be studied in compiler construction. ... the optimizer will recognize this ...
    (comp.lang.pascal.misc)
  • Re: Vulnerability in SETI@home
    ... Also on joes post he did not show an eip overwrite but on OSX we are ... able to overwrite the pc register. ... > GNU gdb 5.0rh-5 Red Hat Linux 7.1 ...
    (Vuln-Dev)