This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix ptype problem printing typedefs defined differently in different compilation units


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



Gdb ChangeLog entry:

 2006-01-03  Fred Fish  <fnf@specifix.com>
 
 	* 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  <fnf@specifix.com>

	* 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);
+ }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]