* gdb.c++/psmang.exp, gdb.c++/psmang1.cc, gdb.c++/psmang2.cc: New
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.c++ / psmang.exp
CommitLineData
f0708dbb
JB
1# Copyright 2002 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# Please email any bugs, comments, and/or additions to this file to:
18# bug-gdb@prep.ai.mit.edu
19
20# This file is part of the gdb testsuite
21
22# Looking up methods by name, in programs with multiple compilation units.
23
24# ====== PLEASE BE VERY CAREFUL WHEN CHANGING THIS TEST. =====
25#
26# The bug we're testing for (circa October 2002) is very sensitive to
27# various conditions that are hard to control directly in the test
28# suite. If you change the test, please revert this change, and make
29# sure the test still fails:
30#
31# 2002-08-29 Jim Blandy <jimb@redhat.com>
32#
33# * symtab.c (lookup_symbol_aux): In the cases where we find a
34# minimal symbol of an appropriate name and use its address to
35# select a symtab to read and search, use `name' (as passed to us)
36# as the demangled name when searching the symtab's global and
37# static blocks, not the minsym's name.
38#
39# The original bug was that you'd try to set a breakpoint on a method
40# (e.g., `break s::method1'), and you'd get an error, but if you
41# repeated the command, it would work the second time:
42#
43# (gdb) break s::method1
44# the class s does not have any method named method1
45# Hint: try 's::method1<TAB> or 's::method1<ESC-?>
46# (Note leading single quote.)
47# (gdb) break s::method1
48# Breakpoint 1 at 0x804841b: file psmang1.cc, line 13.
49# (gdb)
50#
51# The problem was in lookup_symbol_aux: when looking up s::method1, it
52# would fail to find it in any symtabs, find the minsym with the
53# corresponding mangled name (say, `_ZN1S7method1Ev'), pass the
54# minsym's address to find_pc_sect_symtab to look up the symtab
55# (causing the compilation unit's full symbols to be read in), and
56# then look up the symbol in that symtab's global block. All that is
57# correct. However, it would pass the minsym's name as the NAME
58# argument to lookup_block_symbol; a minsym's name is mangled, whereas
59# lookup_block_symbol's NAME argument should be demangled.
60#
61# This is a pretty simple bug, but it turns out to be a bear to
62# construct a test for. That's why this test case is so delicate. If
63# you can see how to make it less so, please contribute a patch.
64#
65# Here are the twists:
66#
67# The bug only manifests itself when we call lookup_symbol to look up
68# a method name (like "s::method1" or "s::method2"), and that method's
69# definition is in a compilation unit for which we have read partial
70# symbols, but not full symbols. The partial->full conversion must be
71# caused by that specific lookup. (If we already have full symbols
72# for the method's compilation unit, we won't need to look up the
73# minsym, find the symtab for the minsym's address, and then call
74# lookup_block_symbol; it's that last call where things go awry.)
75#
76# Now, when asked to set a breakpoint at `s::method1', GDB will first
77# look up `s' to see if that is, in fact, the name of a class, and
78# then look up 's::method1'. So we have to make sure that looking up
79# `s' doesn't cause full symbols to be read for the compilation unit
80# containing the definition of `s::method1'.
81#
82# The partial symbol tables for `psmang1.cc' and `psmang2.cc' will
83# both have entries for `s'; GDB will read full symbols for whichever
84# compilation unit's partial symbol table appears first in the
85# objfile's list. The order in which compilation units appear in the
86# partial symbol table list depends on how the program is linked, and
87# how the debug info reader does the partial symbol scan. Ideally,
88# the test shouldn't rely on them appearing in any particular order.
89#
90# So, since we don't know which compilation unit's full symbols are
91# going to get read, we simply try looking up one method from each of
92# the two compilation units. One of them has to come after the other
93# in the partial symbol table list, so whichever comes later will
94# still need its partial symbols read by the time we go to look up
95# 's::methodX'.
96#
97# Second twist: don't move the common definition of `struct s' into a
98# header file. If the compiler emits identical stabs for the
99# #inclusion of that header file into psmang1.cc and into psmang2.cc,
100# then the linker will do stabs compression, and replace one of the
101# BINCL/EINCL regions with an EXCL stab, pointing to the other
102# BINCL/EINCL region. GDB will read this, and record that the
103# compilation unit that got the EXCL depends on the compilation unit
104# that kept the BINCL/EINCL. Then, when it decides it needs to read
105# full symbols for the former, it'll also read full symbols for the
106# latter. Now, if it just so happens that the compilation unit that
107# got the EXCL is also the first one with a definition of `s' in the
108# partial symbol table list, then that first probe for `s' will cause
109# both compilation units' full symbols to be read --- again defeating
110# the test.
111#
112# We could work around this by having three compilation units, or by
113# ensuring that the header file produces different stabs each time
114# it's #included, but it seems simplest just to avoid compilation unit
115# dependencies altogether, drop the header file, and duplicate the
116# (pretty trivial) struct definition.
117#
118# Note that #including any header file at all into both compilation
119# units --- say, <stdio.h> --- could create this sort of dependency.
120#
121# Third twist: given the way lookup_block_symbol is written, it's
122# possible to find the symbol even when it gets passed a mangled name
123# for its NAME parameter. There are three ways lookup_block_symbol
124# might search a block, depending on how it was constructed:
125#
126# linear search. In this case, this bug will never manifest itself,
127# since we check every symbol against NAME using SYMBOL_MATCHES_NAME.
128# Since that macro checks its second argument (NAME) against both the
129# mangled and demangled names of the symbol, this will always find the
130# symbol successfully, so, no bug.
131#
132# hash table. If both the mangled and demangled names hash to the
133# same bucket, then you'll again find the symbol "by accident", since
134# we search the entire bucket using SYMBOL_SOURCE_NAME. Since GDB
135# chooses the number of buckets based on the number of symbols, small
136# compilation units may have only one hash bucket; in this case, the
137# search always succeeds, even though we hashed on the wrong name.
138# This test works around that by having a lot of dummy variables,
139# making it less likely that the mangled and demangled names fall in
140# the same bucket.
141#
142# binary search. (GDB 5.2 produced these sorts of blocks, and this
143# test tries to detect the bug there, but subsequent versions of GDB
144# almost never build them, and they may soon be removed entirely.) In
145# this case, the symbols in the block are sorted by their
146# SYMBOL_SOURCE_NAME (whose behavior depends on the current demangling
147# setting, so that's wrong, but let's try to stay focussed).
148# lookup_block_symbol does a binary search comparing NAME with
149# SYMBOL_SOURCE_NAME until the range has been narrowed down to only a
150# few symbols; then it starts a linear search forward from the lower
151# end of that range, until it reaches a symbol whose
152# SYMBOL_SOURCE_NAME follows NAME in lexicographic order. This means
153# that, if you're doing a binary search for a mangled name in a block
154# sorted by SYMBOL_SOURCE_NAME, you might find the symbol `by
155# accident' if the mangled and demangled names happen to fall near
156# each other in the ordering. The initial version of this patch used
157# a class called `S'; all the other symbols in the compilation unit
158# started with lower-case letters, so the demangled name `S::method1'
159# sorted at the same place as the mangled name `_ZN1S7method1Ev': at
160# the very beginning. Using a lower-case 's' as the name ensures that
161# the demangled name falls after all the dummy symbols introduced for
162# the hash table, as described above.
163#
164# This is all so tortured, someone will probably come up with still
165# other ways this test could fail to do its job. If you need to make
166# revisions, please be very careful.
167
168if $tracelevel then {
169 strace $tracelevel
170}
171
172#
173# test running programs
174#
175
176set prms_id 0
177set bug_id 0
178
179if { [skip_cplus_tests] } { continue }
180
181set testfile "psmang"
182set binfile ${objdir}/${subdir}/${testfile}
183
184if [get_compiler_info ${binfile} "c++"] {
185 return -1;
186}
187
188if { [gdb_compile "${srcdir}/${subdir}/${testfile}1.cc" "${testfile}1.o" object {debug c++}] != "" } {
189 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
190}
191
192if { [gdb_compile "${srcdir}/${subdir}/${testfile}2.cc" "${testfile}2.o" object {debug c++}] != "" } {
193 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
194}
195
196if { [gdb_compile "${testfile}1.o ${testfile}2.o" ${binfile} executable {debug c++}] != "" } {
197 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
198}
199
200
201gdb_exit
202gdb_start
203gdb_reinitialize_dir $srcdir/$subdir
204gdb_load ${binfile}
205
206gdb_test "break s::method1" "Breakpoint .* at .*: file .*psmang1.cc.*"
207
208# We have to exit and restart GDB here, to make sure that all the
209# compilation units are psymtabs again.
210
211gdb_exit
212gdb_start
213gdb_reinitialize_dir $srcdir/$subdir
214gdb_load ${binfile}
215
216gdb_test "break s::method2" "Breakpoint .* at .*: file .*psmang2.cc.*"
This page took 0.030419 seconds and 4 git commands to generate.