From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13671 invoked by alias); 3 Jan 2006 20:17:26 -0000 Received: (qmail 13662 invoked by uid 22791); 3 Jan 2006 20:17:25 -0000 X-Spam-Check-By: sourceware.org Received: from w099.z064220152.sjc-ca.dsl.cnc.net (HELO duck.specifix.com) (64.220.152.99) by sourceware.org (qpsmtpd/0.31) with ESMTP; Tue, 03 Jan 2006 20:17:20 +0000 Received: from [::1] (duck.specifix.com [64.220.152.99]) by duck.specifix.com (Postfix) with ESMTP id 93E53FA8C; Tue, 3 Jan 2006 12:17:17 -0800 (PST) From: Fred Fish Reply-To: fnf@specifix.com To: gdb-patches@sourceware.org Subject: [PATCH] Fix ptype problem printing typedefs defined differently in different compilation units Date: Tue, 03 Jan 2006 20:17:00 -0000 User-Agent: KMail/1.9.1 Cc: fnf@specifix.com MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_uvtuD47/Kp5MiJi" Message-Id: <200601031517.50309.fnf@specifix.com> Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2006-01/txt/msg00015.txt.bz2 --Boundary-00=_uvtuD47/Kp5MiJi Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Content-length: 837 When examining an executable (but not running it) if you have a typedef in two different compilation units, with the same name but different types, the ptype command will print the type for the first one it finds in a scan of all the loaded symtabs, regardless of any source context you may have selected. Of course the wisdom of having writing this sort of code in the first place is somewhat suspect... :-) Attached is a patch that adds a test to the ptype.exp test for this case, by alternately listing functions from different compilation units (thus setting gdb's idea of the current source context) and verifying that ptype prints the type correctly based on the context. The patch also adds a proposed fix for the problem, which is to look in the current source symtab for the name, before scanning all the symtabs. -Fred --Boundary-00=_uvtuD47/Kp5MiJi Content-Type: text/x-diff; charset="us-ascii"; name="symtab.patch2" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="symtab.patch2" Content-length: 7183 Gdb ChangeLog entry: 2006-01-03 Fred Fish * symtab.c (lookup_symbol_aux_symtab): Add declaration and definition of function to look up name in a specific symtab. (lookup_symbol_aux): Add cursal variable and code to set it. Call lookup_symbol_aux_symtab with current source symtab. Gdb testsuite ChangeLog entry: 2006-01-03 Fred Fish * gdb.base/ptype.c (foo): Add typedef. (intfoo): Add function. * gdb.base/ptype1.c: New file. * gdb.base/ptype.exp: Handle compilation and linking with two source files. Test that proper type for "foo" is found based on source context rather than first match found in symtabs. Index: symtab.c =================================================================== RCS file: /cvsroots/latest/src/gdb/gdb/symtab.c,v retrieving revision 1.1.1.2 diff -c -p -r1.1.1.2 symtab.c *** symtab.c 30 Dec 2005 18:53:03 -0000 1.1.1.2 --- symtab.c 3 Jan 2006 18:04:01 -0000 *************** struct symbol *lookup_symbol_aux_local ( *** 94,99 **** --- 94,107 ---- struct symtab **symtab); static + struct symbol *lookup_symbol_aux_symtab (struct symtab *s, + int block_index, + const char *name, + const char *linkage_name, + const domain_enum domain, + struct symtab **symtab); + + static struct symbol *lookup_symbol_aux_symtabs (int block_index, const char *name, const char *linkage_name, *************** lookup_symbol_aux (const char *name, con *** 1079,1084 **** --- 1087,1093 ---- int *is_a_field_of_this, struct symtab **symtab) { struct symbol *sym; + struct symtab_and_line cursal; /* Make sure we do something sensible with is_a_field_of_this, since the callers that set this parameter to some non-null value will *************** lookup_symbol_aux (const char *name, con *** 1122,1127 **** --- 1131,1144 ---- if (sym != NULL) return sym; + /* Check the symtab associated with the current source line. */ + + cursal = get_current_source_symtab_and_line (); + sym = lookup_symbol_aux_symtab (cursal.symtab, STATIC_BLOCK, name, + linkage_name, domain, symtab); + if (sym != NULL) + return sym; + /* Now search all static file-level symbols. Not strictly correct, but more useful than an error. Do the symtabs first, then check the psymtabs. If a psymtab indicates the existence of the *************** lookup_symbol_aux_block (const char *nam *** 1215,1220 **** --- 1232,1269 ---- return NULL; } + /* Check to see if the symbol is defined in a specific symtab. + BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK, + depending on whether or not we want to search global symbols or + static symbols. */ + + static struct symbol * + lookup_symbol_aux_symtab (struct symtab *s, int block_index, + const char *name, const char *linkage_name, + const domain_enum domain, + struct symtab **symtab) + { + struct symbol *sym; + struct blockvector *bv; + const struct block *block; + + if (s != NULL) + { + bv = BLOCKVECTOR (s); + block = BLOCKVECTOR_BLOCK (bv, block_index); + sym = lookup_block_symbol (block, name, linkage_name, domain); + if (sym) + { + block_found = block; + if (symtab != NULL) + *symtab = s; + return fixup_symbol_section (sym, s->objfile); + } + } + + return NULL; + } + /* Check to see if the symbol is defined in one of the symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK, depending on whether or not we want to search global symbols or Index: testsuite/gdb.base/ptype.c =================================================================== RCS file: /cvsroots/latest/src/gdb/gdb/testsuite/gdb.base/ptype.c,v retrieving revision 1.1.1.1 diff -c -p -r1.1.1.1 ptype.c *** testsuite/gdb.base/ptype.c 8 Oct 2005 19:36:19 -0000 1.1.1.1 --- testsuite/gdb.base/ptype.c 3 Jan 2006 05:17:21 -0000 *************** func_type v_func_type; *** 259,264 **** --- 259,273 ---- /***********/ + typedef int foo; + + foo intfoo (afoo) + { + return (afoo * 2); + } + + /***********/ + int main () { /* Ensure that malloc is a pointer type; avoid use of "void" and any include files. */ Index: testsuite/gdb.base/ptype.exp =================================================================== RCS file: /cvsroots/latest/src/gdb/gdb/testsuite/gdb.base/ptype.exp,v retrieving revision 1.1.1.1 diff -c -p -r1.1.1.1 ptype.exp *** testsuite/gdb.base/ptype.exp 8 Oct 2005 19:36:19 -0000 1.1.1.1 --- testsuite/gdb.base/ptype.exp 3 Jan 2006 05:29:40 -0000 *************** set prms_id 0 *** 31,39 **** set bug_id 0 set testfile "ptype" ! set srcfile ${testfile}.c set binfile ${objdir}/${subdir}/${testfile} ! if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } { gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail." } --- 31,47 ---- set bug_id 0 set testfile "ptype" ! set srcfile0 ${testfile}.c ! set srcfile1 ${testfile}1.c set binfile ${objdir}/${subdir}/${testfile} ! ! if { [gdb_compile "${srcdir}/${subdir}/${srcfile0}" "${binfile}0.o" object {debug}] != "" } { ! gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail." ! } ! if { [gdb_compile "${srcdir}/${subdir}/${srcfile1}" "${binfile}1.o" object {debug}] != "" } { ! gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail." ! } ! if { [gdb_compile "${binfile}0.o ${binfile}1.o" "${binfile}" executable {debug}] != "" } { gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail." } *************** ptype_maybe_prototyped "ffptr" "int (*(* *** 574,579 **** --- 582,599 ---- ptype_maybe_prototyped "fffptr" "int (*(*(*)(char))(short int))(long int)" \ "int (*(*(*)())())()" + # Test printing type of typedefs in different scopes, with same name + # but different type. + + gdb_test "list intfoo" "" + gdb_test "ptype foo" "type = int" "ptype foo typedef after first list of intfoo" + gdb_test "list charfoo" "" + gdb_test "ptype foo" "type = char" "ptype foo typedef after first list of charfoo" + gdb_test "list intfoo" "" + gdb_test "ptype foo" "type = int" "ptype foo typedef after second list of intfoo" + gdb_test "list charfoo" "" + gdb_test "ptype foo" "type = char" "ptype foo typedef after second list of charfoo" + # Test printing type of string constants and array constants, but # requires a running process. These call malloc, and can take a long # time to execute over a slow serial link, so increase the timeout. Index: testsuite/gdb.base/ptype1.c =================================================================== RCS file: testsuite/gdb.base/ptype1.c diff -N testsuite/gdb.base/ptype1.c *** /dev/null 1 Jan 1970 00:00:00 -0000 --- testsuite/gdb.base/ptype1.c 3 Jan 2006 05:31:00 -0000 *************** *** 0 **** --- 1,6 ---- + typedef char foo; + + foo charfoo (afoo) + { + return (afoo * 2); + } --Boundary-00=_uvtuD47/Kp5MiJi--