Copyright updates for 2007.
[external/binutils.git] / gdb / testsuite / gdb.cp / hang.exp
1 #   Copyright 2002, 2004, 2007 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
16
17 if $tracelevel then {
18         strace $tracelevel
19 }
20
21 set prms_id 0
22 set bug_id 0
23
24 if { [skip_cplus_tests] } { continue }
25
26 set testfile hang
27 set binfile ${objdir}/${subdir}/${testfile}
28
29 foreach file {hang1 hang2 hang3} {
30     if {[gdb_compile "${srcdir}/${subdir}/${file}.C" "${file}.o" object {c++ debug}] != ""} {
31         untested hang.exp
32         return -1
33     }
34 }
35
36 if {[gdb_compile "hang1.o hang2.o hang3.o" ${binfile} executable {c++ debug}] != "" } {
37      untested hang.exp
38      return -1
39 }
40
41
42 gdb_exit
43 gdb_start
44 gdb_reinitialize_dir $srcdir/$subdir
45 gdb_load ${binfile}
46
47
48 # As of May 1, 2002, GDB hangs trying to read the debug info for the
49 # `hang2.o' compilation unit from the executable `hang', when compiled
50 # by g++ 2.96 with STABS debugging info.  Here's what's going on, as
51 # best as I can tell.
52 #
53 # The definition of `struct A' in `hang.H' refers to `struct B' as an
54 # incomplete type.  The stabs declare type number (1,3) to be a cross-
55 # reference type, `xsB:'.
56 #
57 # The definition of `struct C' contains a nested definition for
58 # `struct B' --- or more properly, `struct C::B'.  However, the stabs
59 # fail to qualify the structure tag: it just looks like a definition
60 # for `struct B'.  I think this is a compiler bug, but perhaps GCC
61 # doesn't emit qualified names for a reason.
62 #
63 # `hang.H' gets #included by both `hang1.C' and `hang2.C'.  So the
64 # stabs for `struct A', the incomplete `struct B', and `struct C'
65 # appear in both hang1.o's and hang2.o's stabs.
66 #
67 # When those two files are linked together, since hang2.o appears
68 # later in the command line, its #inclusion of `hang.H' gets replaced
69 # with an N_EXCL stab, referring back to hang1.o's stabs for the
70 # header file.
71 #
72 # When GDB builds psymtabs for the executable hang, it notes that
73 # hang2.o's stabs contain an N_EXCL referring to a header that appears
74 # in full in hang1.o's stabs.  So hang2.o's psymtab lists a dependency
75 # on hang1.o's psymtab.
76 #
77 # When the user types the command `print var_in_b', GDB scans the
78 # psymtabs for a symbol by that name, and decides to read full symbols
79 # for `hang2.o'.
80 #
81 # Since `hang2.o''s psymtab lists `hang1.o' as a dependency, GDB first
82 # reads `hang1.o''s symbols.  When GDB sees `(1,3)=xsB:', it creates a
83 # type object for `struct B', sets its TYPE_FLAG_STUB flag, and
84 # records it as type number `(1,3)'.
85 #
86 # When GDB finds the definition of `struct C::B', since the stabs
87 # don't indicate that the type is nested within C, it treats it as
88 # a definition of `struct B'.
89 #
90 # When GDB is finished reading `hang1.o''s symbols, it calls
91 # `cleanup_undefined_types'.  This function mistakes the definition of
92 # `struct C::B' for a definition for `struct B', and overwrites the
93 # incomplete type object for the real `struct B', using `memcpy'.  Now
94 # stabs type number `(1,3)' refers to this (incorrect) complete type.
95 # Furthermore, the `memcpy' simply copies the original's `cv_type'
96 # field to the target, giving the target a corrupt `cv_type' ring: the
97 # chain does not point back to the target type.
98 #
99 # Having satisfied `hang2.o''s psymtab's dependencies, GDB begins to
100 # read `hang2.o''s symbols.  These contain the true definition for
101 # `struct B', which refers to type number `(1,3)' as the type it's
102 # defining.  GDB looks up type `(1,3)', and finds the (incorrect)
103 # complete type established by the call to `cleanup_undefined_types'
104 # above.  However, it doesn't notice that the type is already defined,
105 # and passes it to `read_struct_type', which then writes the new
106 # definition's size, field list, etc. into the type object which
107 # already has those fields initialized.  Adding insult to injury,
108 # `read_struct_type' then calls `finish_cv_type'; since the `memcpy'
109 # in `cleanup_undefined_types' corrupted the target type's `cv_type'
110 # ring, `finish_cv_type' enters an infinite loop.
111
112 # This checks that GDB recognizes when a structure is about to be
113 # overwritten, and refuses, with a complaint.
114 gdb_test "print var_in_b" " = 1729" "doesn't overwrite struct type"
115
116 # This checks that cleanup_undefined_types doesn't create corrupt
117 # cv_type chains.  Note that var_in_hang3 does need to be declared in
118 # a separate compilation unit, whose psymtab depends on hang1.o's
119 # psymtab.  Otherwise, GDB won't call cleanup_undefined_types (as it
120 # finishes hang1.o's symbols) before it calls make_cv_type (while
121 # reading hang3.o's symbols).
122 #
123 # The bug only happens when you compile with -gstabs+; Otherwise, GCC
124 # won't include the `const' qualifier on `const_B_ptr' in `hang3.o''s
125 # STABS, so GDB won't try to create a const variant of the smashed
126 # struct type, and get caught by the corrupted cv_type chain.
127 gdb_test "print var_in_hang3" " = 42" "doesn't corrupt cv_type chain"