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