[gnu.g++.bug] gdb3.5 bug?

kingdon@AI.MIT.EDU (Jim Kingdon) (02/12/90)

The following confusing behavior is the result of g++ doing excessive
optimization (note that -O is not specified).  The actual value of
"this" after the call to builtin_new is in register a2, but GDB thinks
that "this" is on the stack.  Ideally there would be a way to give
this information to GDB, but barring that the fix for this bug is to
store "this" back on the stack when -O is not specified.

Here is an excerpt from the assembly language produced by g++ 1.35.0
on a Sun 3 running SunOS4.  Compiled with "g++ -S -g ../foo.cc".
    _Bar_PSBar_SI_SI:
	    .stabd 68,0,16
	    link a6,#0
	    movel a2,sp@-
	    movel a6@(8),a2
	    tstl a2
	    jne L2
	    pea 8:w
	    jbsr ___builtin_new
	    movel d0,a2
            [a2 is never stored back into a6@(8)]

Return-Path: <mike@cs.utah.edu>
Date: Thu, 8 Feb 90 12:37:56 -0700
From: mike@cs.utah.edu (Mike Hibler)
To: kingdon@ai.mit.edu
Subject: gdb3.5 bug?

Someone here showed this to me.  I don't know much about C++ so I can't
say much except it does the same thing under 3.2 and 3.4.

The problem seems to be that it displays an incorrect value for "bar" at
the end of the function.  Or maybe it has the wrong location for "bar":
----

66 jaguar> cat foo.c
class Bar {
public:
    Bar(int, int);
protected:
    int one;
    int two;
};

class Foo {
public:
  Foo(void);
protected:
  Bar* bar;
};

Bar::Bar (int o, int t) {
  one = o;
  two = t;
}

Foo::Foo (void) {
  bar = new Bar(1, 2);
}

int main (void) {
  Foo* foo = new Foo;
}

67 jaguar> g++ -v -g foo.c
g++ version 1.35.0
 /usr/local/lib/gcc-cpp -+ -v -undef -D__GNU__ -D__GNUG__ -D__cplusplus -Dmc6800
0 -Dhp300 -Dhp9000 -Dunix -D__mc68000__ -D__hp300__ -D__hp9000__ -D__unix__ -D__
HAVE_FPU__ foo.c /tmp/cc013697.cpp
GNU CPP version 1.36
 /usr/local/lib/gcc-cc1plus /tmp/cc013697.cpp -quiet -dumpbase foo.c -noreg -ver
sion -G -o /tmp/cc013697.s
GNU C++ version 1.35.0 (68k, MIT syntax) compiled by GNU C version 1.34.
 /usr/local/lib/gcc-as /tmp/cc013697.s -o foo.o
 /usr/local/lib/gcc-ld++ -C /usr/local/lib/crt0+.o foo.o -lg++ /usr/local/lib/gc
c-gnulib -lg -lc

68 jaguar> gdb3.5 foo
GDB 3.5, Copyright (C) 1989 Free Software Foundation, Inc.
There is ABSOLUTELY NO WARRANTY for GDB; type "info warranty" for details.
GDB is free software and you are welcome to distribute copies of it
 under certain conditions; type "info copying" to see the conditions.
Reading symbol data from /u/mike/g++/foo...done.
Type "help" for a list of commands.
(gdb) b Bar::Bar
Reading in symbols for foo.c...done.
Breakpoint 1 at 0xf4: file foo.c, line 16.
(gdb) run
Starting program: /u/mike/g++/foo

Bpt 1, Bar_PSBar_SI_SI ($this=(struct Bar *) 0x0, o=1, t=2) (foo.c line 17)
17        one = o;
(gdb) fin
Run till exit from #0  Bar_PSBar_SI_SI ($this=(struct Bar *) 0x0, o=1, t=2) (foo
.c line 17)
0x136 in Foo_PSFoo ($this=(struct Foo *) 0x0) (foo.c line 22)
22        bar = new Bar(1, 2);
Value returned is $1 = (struct Bar *) 0x3000
(gdb) p &bar
$2 = (struct Bar **) 0x0
(gdb) p bar
$3 = (struct Bar *) 0x4e560000
(gdb) quit
The program is running.  Quit anyway? (y or n) y

69 jaguar>