Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Follow specific symbol's DW_AT_decl_file
@ 2007-01-11 22:39 Jan Kratochvil
  2007-01-21 16:55 ` Daniel Jacobowitz
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Kratochvil @ 2007-01-11 22:39 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 950 bytes --]

Hi,

Debby Townsend <dstownse(at)us.ibm.com>:
------------------------------------------------------------------------------
Both the "info variable" and the "list" command, when given the name 
of an external variable which is defined in an #include'd file,  list 
the incorrect file-name.  In the case of "list" the line number is 
extrapolated correctly; but the wrong source file is listed. 

The problem does not occur for functions defined in the same 
#include'd file. 
------------------------------------------------------------------------------


The regression-smelling parts of the patch are about the file located in
various directories, currently the patch fills in `symtab->dirname' from the
main file of CU (Compilation Unit) - the non-primary filenames were not (much
or at all?) used so far.

I could not find any regression but maybe some more directory-intensive
testcase would be useful.  Request if you feel so.


Regarsd,
Jan

[-- Attachment #2: gdb-cvs-bz109921-DW_AT_decl_file.patch --]
[-- Type: text/plain, Size: 17331 bytes --]

https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=109921


2007-01-09  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* buildsym.c (start_subfile_index): Renamed `start_subfile' now
	supporting the FILE_INDEX parameter.
	(start_subfile): Backward compatible stub for `start_subfile_index'.
	(end_symtab): Resolve new SYMBOL.FILE.SYMTAB from SYMBOL.FILE.INDEX.
	Substitute possibly missing DIRNAME from the CU's main file DIRNAME.
	Clear `subfiles' variable as its data have been deallocated.
	* buildsym.h (struct subfile): New field `file_index'.
	(start_subfile_index): New prototype.
	* dwarf2read.c (add_file_name): Ensure subfile has been founded.
	(dwarf_decode_lines): Specify the new FILE_INDEX parameter.
	(dwarf2_start_subfile): New FILE_INDEX parameter.
	(new_symbol): Extract `DW_AT_decl_file' DWARF 2 information entry.
	* symtab.c (lookup_symbol): Override by the new SYMBOL.FILE.SYMTAB.
	(search_symbols): Likewise.
	* symtab.h (struct symbol): New fields FILE.INDEX and FILE.SYMTAB.
	(SYMBOL_FILE_INDEX, SYMBOL_FILE_SYMTAB): New macros.


2007-01-09  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.dwarf2/dw2-included.exp, gdb.dwarf2/dw2-included.c,
	gdb.dwarf2/dw2-included.h: New files.


Index: gdb/buildsym.c
===================================================================
RCS file: /cvs/src/src/gdb/buildsym.c,v
retrieving revision 1.45
diff -u -p -r1.45 buildsym.c
--- gdb/buildsym.c	9 Jan 2007 17:58:50 -0000	1.45
+++ gdb/buildsym.c	11 Jan 2007 22:30:30 -0000
@@ -540,7 +540,7 @@ make_blockvector (struct objfile *objfil
    the directory in which it resides (or NULL if not known).  */
 
 void
-start_subfile (char *name, char *dirname)
+start_subfile_index (char *name, char *dirname, unsigned file_index)
 {
   struct subfile *subfile;
 
@@ -552,6 +552,17 @@ start_subfile (char *name, char *dirname
       if (FILENAME_CMP (subfile->name, name) == 0)
 	{
 	  current_subfile = subfile;
+
+	  if (subfile->file_index != 0 && file_index != 0
+	      && subfile->file_index != file_index)
+	    complaint (&symfile_complaints, _("Filenames indexing conflict: "
+	               "name \"%s\" dir \"%s\" index %u vs. "
+		       "name \"%s\" dir \"%s\" index %u"),
+		       subfile->name, subfile->dirname, subfile->file_index,
+		       name, dirname, file_index);
+	  if (subfile->file_index == 0)
+	    subfile->file_index = file_index;
+
 	  return;
 	}
     }
@@ -567,6 +578,7 @@ start_subfile (char *name, char *dirname
   current_subfile = subfile;
 
   /* Save its name and compilation directory name */
+  subfile->file_index = file_index;
   subfile->name = (name == NULL) ? NULL : savestring (name, strlen (name));
   subfile->dirname =
     (dirname == NULL) ? NULL : savestring (dirname, strlen (dirname));
@@ -625,6 +637,13 @@ start_subfile (char *name, char *dirname
     }
 }
 
+/* Backward compatibility.  */
+void
+start_subfile (char *name, char *dirname)
+{
+  start_subfile_index (name, dirname, 0);
+}
+
 /* For stabs readers, the first N_SO symbol is assumed to be the
    source file name, and the subfile struct is initialized using that
    assumption.  If another N_SO symbol is later seen, immediately
@@ -824,9 +843,12 @@ end_symtab (CORE_ADDR end_addr, struct o
 {
   struct symtab *symtab = NULL;
   struct blockvector *blockvector;
-  struct subfile *subfile;
+  struct subfile *subfile, *subfile_main;
   struct context_stack *cstk;
   struct subfile *nextsub;
+  int subfiles_count;
+  struct symtab **file_index_to_symtab;
+  size_t file_index_to_symtab_size;
 
   /* Finish the lexical context of the last function in the file; pop
      the context stack.  */
@@ -924,6 +946,18 @@ end_symtab (CORE_ADDR end_addr, struct o
 #endif
   PROCESS_LINENUMBER_HOOK ();	/* Needed for xcoff. */
 
+  /* Get the last subfile s SUBFILE_MAIN which is the main file of CU.
+     Count SUBFILES_COUNT.
+     Start with 1 as we do not iterate past the last item.  */
+  subfiles_count = 1;
+  for (subfile_main = subfiles; subfile_main && subfile_main->next;
+       subfile_main = subfile_main->next)
+    subfiles_count++;
+
+  file_index_to_symtab_size = sizeof (*file_index_to_symtab) * subfiles_count;
+  file_index_to_symtab = xmalloc (file_index_to_symtab_size);
+  memset ((char *) file_index_to_symtab, 0, file_index_to_symtab_size);
+
   /* Now create the symtab objects proper, one for each subfile.  */
   /* (The main file is the last one on the chain.)  */
 
@@ -984,6 +1018,16 @@ end_symtab (CORE_ADDR end_addr, struct o
 			       strlen (subfile->dirname) + 1);
 	      strcpy (symtab->dirname, subfile->dirname);
 	    }
+	  /* Non-primary subfiles may miss COMP_DIR resulting in NULL
+	     DIRNAME and so default it from the CU file - SUBFILE_MAIN.  */
+	  else if (subfile_main->dirname)
+	    {
+	      /* Reallocate the dirname on the symbol obstack */
+	      symtab->dirname = (char *)
+		obstack_alloc (&objfile->objfile_obstack,
+			       strlen (subfile_main->dirname) + 1);
+	      strcpy (symtab->dirname, subfile_main->dirname);
+	    }
 	  else
 	    {
 	      symtab->dirname = NULL;
@@ -1018,6 +1062,13 @@ end_symtab (CORE_ADDR end_addr, struct o
 	     but the main file.  */
 
 	  symtab->primary = 0;
+
+	  /* It may be zero for files unlisted in File Table.  */
+	  if (subfile->file_index)
+	    {
+	      gdb_assert (subfile->file_index <= subfiles_count);
+	      file_index_to_symtab[subfile->file_index - 1] = symtab;
+	    }
 	}
       if (subfile->name != NULL)
 	{
@@ -1048,9 +1099,40 @@ end_symtab (CORE_ADDR end_addr, struct o
       symtab->primary = 1;
     }
 
+  /* Resolve `struct symbol.file.index' into `struct symbol.file.symtab'.  */
+  if (blockvector)
+    {
+      int block_i;
+
+      for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
+	{
+	  struct symbol *sym;
+	  struct dict_iterator iter;
+
+	  for (sym = dict_iterator_first (BLOCK_DICT
+		       (BLOCKVECTOR_BLOCK (blockvector, block_i)), &iter);
+	       sym != NULL;
+	       sym = dict_iterator_next (&iter))
+	    {
+	      /* Beware the ordering as `sym->file' is a union.  */
+	      if (SYMBOL_FILE_INDEX (sym)
+		  && file_index_to_symtab[SYMBOL_FILE_INDEX (sym) - 1])
+	        SYMBOL_FILE_SYMTAB (sym) = file_index_to_symtab
+					     [SYMBOL_FILE_INDEX (sym) - 1];
+	      else
+	        {
+		  /* Default to the primary symbol table, never use NULL.  */
+		  SYMBOL_FILE_SYMTAB (sym) = symtab;
+		}
+	    }
+	}
+    }
+
+  xfree (file_index_to_symtab);
   last_source_file = NULL;
   current_subfile = NULL;
   pending_macros = NULL;
+  subfiles = NULL;
 
   return symtab;
 }
Index: gdb/buildsym.h
===================================================================
RCS file: /cvs/src/src/gdb/buildsym.h,v
retrieving revision 1.15
diff -u -p -r1.15 buildsym.h
--- gdb/buildsym.h	9 Jan 2007 17:58:50 -0000	1.15
+++ gdb/buildsym.h	11 Jan 2007 22:30:30 -0000
@@ -63,6 +63,7 @@ EXTERN CORE_ADDR last_source_start_addr;
 struct subfile
   {
     struct subfile *next;
+    unsigned file_index;
     char *name;
     char *dirname;
     struct linetable *line_vector;
@@ -241,6 +242,9 @@ extern void finish_block (struct symbol 
 
 extern void really_free_pendings (void *dummy);
 
+extern void start_subfile_index (char *name, char *dirname,
+				 unsigned file_index);
+
 extern void start_subfile (char *name, char *dirname);
 
 extern void patch_subfile_names (struct subfile *subfile, char *name);
Index: gdb/dwarf2read.c
===================================================================
RCS file: /cvs/src/src/gdb/dwarf2read.c,v
retrieving revision 1.211
diff -u -p -r1.211 dwarf2read.c
--- gdb/dwarf2read.c	9 Jan 2007 17:58:50 -0000	1.211
+++ gdb/dwarf2read.c	11 Jan 2007 22:30:33 -0000
@@ -854,7 +854,7 @@ static struct line_header *(dwarf_decode
 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
 				struct dwarf2_cu *, struct partial_symtab *);
 
-static void dwarf2_start_subfile (char *, char *, char *);
+static void dwarf2_start_subfile (char *, char *, char *, unsigned);
 
 static struct symbol *new_symbol (struct die_info *, struct type *,
 				  struct dwarf2_cu *);
@@ -6435,6 +6435,7 @@ add_file_name (struct line_header *lh,
                unsigned int length)
 {
   struct file_entry *fe;
+  char *dir = NULL;
 
   /* Grow the array if necessary.  */
   if (lh->file_names_size == 0)
@@ -6457,6 +6458,10 @@ add_file_name (struct line_header *lh,
   fe->mod_time = mod_time;
   fe->length = length;
   fe->included_p = 0;
+
+  if (dir_index)
+    dir = lh->include_dirs[dir_index - 1];
+  dwarf2_start_subfile (name, dir, NULL, lh->num_file_names);
 }
  
 
@@ -6675,7 +6680,7 @@ dwarf_decode_lines (struct line_header *
           if (fe->dir_index)
             dir = lh->include_dirs[fe->dir_index - 1];
 
-	  dwarf2_start_subfile (fe->name, dir, comp_dir);
+	  dwarf2_start_subfile (fe->name, dir, comp_dir, file);
 	}
 
       /* Decode the table.  */
@@ -6792,7 +6797,7 @@ dwarf_decode_lines (struct line_header *
                 if (!decode_for_pst_p)
 		  {
 		    last_subfile = current_subfile;
-		    dwarf2_start_subfile (fe->name, dir, comp_dir);
+		    dwarf2_start_subfile (fe->name, dir, comp_dir, file);
 		  }
               }
 	      break;
@@ -6896,7 +6901,8 @@ dwarf_decode_lines (struct line_header *
    subfile's name.  */
 
 static void
-dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
+dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir,
+		      unsigned file_index)
 {
   char *fullname;
 
@@ -6915,7 +6921,7 @@ dwarf2_start_subfile (char *filename, ch
   else
     fullname = filename;
 
-  start_subfile (fullname, comp_dir);
+  start_subfile_index (fullname, comp_dir, file_index);
 
   if (fullname != filename)
     xfree (fullname);
@@ -7024,6 +7030,13 @@ new_symbol (struct die_info *die, struct
 	{
 	  SYMBOL_LINE (sym) = DW_UNSND (attr);
 	}
+      attr = dwarf2_attr (die, DW_AT_decl_file, cu);
+      if (attr)
+	{
+	  /* Do not yet search `objfile->symtabs' here as they still do not
+	     have filled in their FILE.INDEX fields.  */
+	  SYMBOL_FILE_INDEX (sym) = DW_UNSND (attr);
+	}
       switch (die->tag)
 	{
 	case DW_TAG_label:
Index: gdb/symtab.c
===================================================================
RCS file: /cvs/src/src/gdb/symtab.c,v
retrieving revision 1.152
diff -u -p -r1.152 symtab.c
--- gdb/symtab.c	9 Jan 2007 22:43:08 -0000	1.152
+++ gdb/symtab.c	11 Jan 2007 22:30:35 -0000
@@ -1133,6 +1133,10 @@ lookup_symbol (const char *name, const s
   if (needtofreename)
     xfree (demangled_name);
 
+  /* Override the returned symtab with optional symbol's specific one.  */
+  if (returnval != NULL && symtab != NULL)
+    *symtab = SYMBOL_FILE_SYMTAB (returnval);
+
   return returnval;	 
 }
 
@@ -3088,7 +3092,7 @@ search_symbols (char *regexp, domain_enu
 	  ALL_BLOCK_SYMBOLS (b, iter, sym)
 	    {
 	      QUIT;
-	      if (file_matches (s->filename, files, nfiles)
+	      if (file_matches (SYMBOL_FILE_SYMTAB (sym)->filename, files, nfiles)
 		  && ((regexp == NULL
 		       || re_exec (SYMBOL_NATURAL_NAME (sym)) != 0)
 		      && ((kind == VARIABLES_DOMAIN && SYMBOL_CLASS (sym) != LOC_TYPEDEF
@@ -3101,7 +3105,7 @@ search_symbols (char *regexp, domain_enu
 		  /* match */
 		  psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
 		  psr->block = i;
-		  psr->symtab = s;
+		  psr->symtab = SYMBOL_FILE_SYMTAB (sym);
 		  psr->symbol = sym;
 		  psr->msymbol = NULL;
 		  psr->next = NULL;
Index: gdb/symtab.h
===================================================================
RCS file: /cvs/src/src/gdb/symtab.h,v
retrieving revision 1.100
diff -u -p -r1.100 symtab.h
--- gdb/symtab.h	9 Jan 2007 17:58:59 -0000	1.100
+++ gdb/symtab.h	11 Jan 2007 22:30:35 -0000
@@ -623,6 +623,18 @@ struct symbol
 
   ENUM_BITFIELD(address_class) aclass : 6;
 
+  /* File name it comes from.  Use with `line' below.
+     FILE.INDEX is zero if the symbol's specific file is not known and in such
+     case we later default to the main file of the compilation unit.
+     FILE.SYMTAB gets resolved during end_symtab() and it is never NULL.  */
+
+  union
+  {
+    unsigned index;
+    struct symtab *symtab;
+  }
+  file;
+
   /* Line number of definition.  FIXME:  Should we really make the assumption
      that nobody will try to debug files longer than 64K lines?  What about
      machine generated programs? */
@@ -663,6 +675,8 @@ struct symbol
 #define SYMBOL_DOMAIN(symbol)	(symbol)->domain
 #define SYMBOL_CLASS(symbol)		(symbol)->aclass
 #define SYMBOL_TYPE(symbol)		(symbol)->type
+#define SYMBOL_FILE_INDEX(symbol)	(symbol)->file.index
+#define SYMBOL_FILE_SYMTAB(symbol)	(symbol)->file.symtab
 #define SYMBOL_LINE(symbol)		(symbol)->line
 #define SYMBOL_BASEREG(symbol)		(symbol)->aux_value.basereg
 #define SYMBOL_OBJFILE(symbol)          (symbol)->aux_value.objfile
Index: gdb/testsuite/gdb.dwarf2/dw2-included.c
===================================================================
RCS file: gdb/testsuite/gdb.dwarf2/dw2-included.c
diff -N gdb/testsuite/gdb.dwarf2/dw2-included.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gdb/testsuite/gdb.dwarf2/dw2-included.c	11 Jan 2007 22:30:36 -0000
@@ -0,0 +1,26 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2006 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+ 
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.  */
+
+#include "dw2-included.h"
+
+int
+main()
+{
+  return 0;
+}
Index: gdb/testsuite/gdb.dwarf2/dw2-included.exp
===================================================================
RCS file: gdb/testsuite/gdb.dwarf2/dw2-included.exp
diff -N gdb/testsuite/gdb.dwarf2/dw2-included.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gdb/testsuite/gdb.dwarf2/dw2-included.exp	11 Jan 2007 22:30:36 -0000
@@ -0,0 +1,47 @@
+# Copyright 2006 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+# Minimal DWARF-2 unit test
+
+# This test can only be run on targets which support DWARF-2.
+# For now pick a sampling of likely targets.
+if {![istarget *-*-linux*]
+    && ![istarget *-*-gnu*]
+    && ![istarget *-*-elf*]
+    && ![istarget *-*-openbsd*]
+    && ![istarget arm-*-eabi*]
+    && ![istarget powerpc-*-eabi*]} {
+    return 0  
+}
+
+set testfile "dw2-included"
+set srcfile ${testfile}.c
+set binfile ${objdir}/${subdir}/${testfile}
+
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
+    return -1
+}
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+gdb_load ${binfile}
+
+gdb_test "set listsize 1" ""
+gdb_test "list integer" "int integer;\r"
+gdb_test "ptype integer" "type = int\r"
+# Path varies depending on the build location.
+gdb_test "info variables integer" "\r\nFile \[^\r\n\]*/gdb.dwarf2/dw2-included.h:\r\nint integer;\r"
Index: gdb/testsuite/gdb.dwarf2/dw2-included.h
===================================================================
RCS file: gdb/testsuite/gdb.dwarf2/dw2-included.h
diff -N gdb/testsuite/gdb.dwarf2/dw2-included.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gdb/testsuite/gdb.dwarf2/dw2-included.h	11 Jan 2007 22:30:36 -0000
@@ -0,0 +1,20 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2006 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+ 
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.  */
+
+int integer;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Follow specific symbol's DW_AT_decl_file
  2007-01-11 22:39 [PATCH] Follow specific symbol's DW_AT_decl_file Jan Kratochvil
@ 2007-01-21 16:55 ` Daniel Jacobowitz
  2007-01-21 22:35   ` Jan Kratochvil
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Jacobowitz @ 2007-01-21 16:55 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

On Thu, Jan 11, 2007 at 11:39:41PM +0100, Jan Kratochvil wrote:
> Hi,
> 
> Debby Townsend <dstownse(at)us.ibm.com>:
> ------------------------------------------------------------------------------
> Both the "info variable" and the "list" command, when given the name 
> of an external variable which is defined in an #include'd file,  list 
> the incorrect file-name.  In the case of "list" the line number is 
> extrapolated correctly; but the wrong source file is listed. 
> 
> The problem does not occur for functions defined in the same 
> #include'd file. 
> ------------------------------------------------------------------------------
> 
> 
> The regression-smelling parts of the patch are about the file located in
> various directories, currently the patch fills in `symtab->dirname' from the
> main file of CU (Compilation Unit) - the non-primary filenames were not (much
> or at all?) used so far.

They were used for line numbers within functions - list.exp is a similar
test.  I think what you've got is fine.

I ended up rewriting the patch, based on your version.  Originally, I
was worried about adding another pointer to "struct symbol"; the
comment at top of the structure is:

/* This structure is space critical.  See space comments at the top.  */

So I wrote an implementation that used an on-the-side hash table.  But
in the end, it was a bit slower, and most symbols needed one.  Even
with -readnow the memory increase was only 3%, so it will be less than
that in practice.  So I went back to doing it in the symbol after all
:-)

The other changes I made were to move the index handling into
dwarf2read.c, since the concept of a file index is dwarf specific,
and to move the test into gdb.base, since in theory other debug
formats could get it right.

I tested this on x86_64-pc-linux-gnu and committed it.

-- 
Daniel Jacobowitz
CodeSourcery

2007-01-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Daniel Jacobowitz  <dan@codesourcery.com>

	* buildsym.c (end_symtab): Use preallocated symtab if available.
	Fill in SYMBOL_SYMTAB.
	* buildsym.h (struct subfile): Add symtab member.
	* dwarf2read.c (struct dwarf2_cu): Add line_header.
	(struct file_entry): Add symtab.
	(free_cu_line_header): New function.
	(read_file_scope): Use it.  Save line_header in the cu.  Process
	lines before DIEs.
	(add_file_name): Initialize new symtab member.
	(dwarf_decode_lines): Create symtabs for included files.
	(new_symbol): Set SYMBOL_SYMTAB.
	* symtab.c (lookup_symbol): Use SYMBOL_SYMTAB.
	(search_symbols): Likewise.
	* symtab.h (struct symbol): Add symtab member.
	(SYMBOL_SYMTAB): Define.

2007-01-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Daniel Jacobowitz  <dan@codesourcery.com>

	* gdb.base/included.c, gdb.base/included.exp,
	gdb.base/included.h: New files.

Index: buildsym.c
===================================================================
RCS file: /cvs/src/src/gdb/buildsym.c,v
retrieving revision 1.45
diff -u -p -r1.45 buildsym.c
--- buildsym.c	9 Jan 2007 17:58:50 -0000	1.45
+++ buildsym.c	21 Jan 2007 16:42:09 -0000
@@ -959,7 +959,10 @@ end_symtab (CORE_ADDR end_addr, struct o
 	    }
 
 	  /* Now, allocate a symbol table.  */
-	  symtab = allocate_symtab (subfile->name, objfile);
+	  if (subfile->symtab == NULL)
+	    symtab = allocate_symtab (subfile->name, objfile);
+	  else
+	    symtab = subfile->symtab;
 
 	  /* Fill in its components.  */
 	  symtab->blockvector = blockvector;
@@ -1048,6 +1051,26 @@ end_symtab (CORE_ADDR end_addr, struct o
       symtab->primary = 1;
     }
 
+  /* Default any symbols without a specified symtab to the primary
+     symtab.  */
+  if (blockvector)
+    {
+      int block_i;
+
+      for (block_i = 0; block_i < BLOCKVECTOR_NBLOCKS (blockvector); block_i++)
+	{
+	  struct block *block = BLOCKVECTOR_BLOCK (blockvector, block_i);
+	  struct symbol *sym;
+	  struct dict_iterator iter;
+
+	  for (sym = dict_iterator_first (BLOCK_DICT (block), &iter);
+	       sym != NULL;
+	       sym = dict_iterator_next (&iter))
+	    if (SYMBOL_SYMTAB (sym) == NULL)
+	      SYMBOL_SYMTAB (sym) = symtab;
+	}
+    }
+
   last_source_file = NULL;
   current_subfile = NULL;
   pending_macros = NULL;
Index: buildsym.h
===================================================================
RCS file: /cvs/src/src/gdb/buildsym.h,v
retrieving revision 1.15
diff -u -p -r1.15 buildsym.h
--- buildsym.h	9 Jan 2007 17:58:50 -0000	1.15
+++ buildsym.h	21 Jan 2007 16:42:09 -0000
@@ -70,6 +70,7 @@ struct subfile
     enum language language;
     char *producer;
     char *debugformat;
+    struct symtab *symtab;
   };
 
 EXTERN struct subfile *subfiles;
Index: dwarf2read.c
===================================================================
RCS file: /cvs/src/src/gdb/dwarf2read.c,v
retrieving revision 1.211
diff -u -p -r1.211 dwarf2read.c
--- dwarf2read.c	9 Jan 2007 17:58:50 -0000	1.211
+++ dwarf2read.c	21 Jan 2007 16:42:10 -0000
@@ -341,6 +341,9 @@ struct dwarf2_cu
      partial symbol tables do not have dependencies.  */
   htab_t dependencies;
 
+  /* Header data from the line table, during full symbol processing.  */
+  struct line_header *line_header;
+
   /* Mark used when releasing cached dies.  */
   unsigned int mark : 1;
 
@@ -432,6 +435,7 @@ struct line_header
     unsigned int mod_time;
     unsigned int length;
     int included_p; /* Non-zero if referenced by the Line Number Program.  */
+    struct symtab *symtab; /* The associated symbol table, if any.  */
   } *file_names;
 
   /* The start and end of the statement program following this
@@ -2754,6 +2758,15 @@ initialize_cu_func_list (struct dwarf2_c
 }
 
 static void
+free_cu_line_header (void *arg)
+{
+  struct dwarf2_cu *cu = arg;
+
+  free_line_header (cu->line_header);
+  cu->line_header = NULL;
+}
+
+static void
 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->objfile;
@@ -2823,18 +2836,9 @@ read_file_scope (struct die_info *die, s
 
   initialize_cu_func_list (cu);
 
-  /* Process all dies in compilation unit.  */
-  if (die->child != NULL)
-    {
-      child_die = die->child;
-      while (child_die && child_die->tag)
-	{
-	  process_die (child_die, cu);
-	  child_die = sibling_die (child_die);
-	}
-    }
-
-  /* Decode line number information if present.  */
+  /* Decode line number information if present.  We do this before
+     processing child DIEs, so that the line header table is available
+     for DW_AT_decl_file.  */
   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
   if (attr)
     {
@@ -2842,12 +2846,23 @@ read_file_scope (struct die_info *die, s
       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
       if (line_header)
         {
-          make_cleanup ((make_cleanup_ftype *) free_line_header,
-                        (void *) line_header);
+          cu->line_header = line_header;
+          make_cleanup (free_cu_line_header, cu);
           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
         }
     }
 
+  /* Process all dies in compilation unit.  */
+  if (die->child != NULL)
+    {
+      child_die = die->child;
+      while (child_die && child_die->tag)
+	{
+	  process_die (child_die, cu);
+	  child_die = sibling_die (child_die);
+	}
+    }
+
   /* Decode macro information, if present.  Dwarf 2 macro information
      refers to information in the line number info statement program
      header, so we can only read it if we've read the header
@@ -6457,6 +6472,7 @@ add_file_name (struct line_header *lh,
   fe->mod_time = mod_time;
   fe->length = length;
   fe->included_p = 0;
+  fe->symtab = NULL;
 }
  
 
@@ -6644,7 +6660,7 @@ dwarf_decode_lines (struct line_header *
   CORE_ADDR baseaddr;
   struct objfile *objfile = cu->objfile;
   const int decode_for_pst_p = (pst != NULL);
-  struct subfile *last_subfile = NULL;
+  struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
 
   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
 
@@ -6869,6 +6885,35 @@ dwarf_decode_lines (struct line_header *
               dwarf2_create_include_psymtab (include_name, pst, objfile);
           }
     }
+  else
+    {
+      /* Make sure a symtab is created for every file, even files
+	 which contain only variables (i.e. no code with associated
+	 line numbers).  */
+
+      int i;
+      struct file_entry *fe;
+
+      for (i = 0; i < lh->num_file_names; i++)
+	{
+	  char *dir = NULL;
+	  fe = &lh->file_names[i];
+	  if (fe->dir_index)
+	    dir = lh->include_dirs[fe->dir_index - 1];
+	  dwarf2_start_subfile (fe->name, dir, comp_dir);
+
+	  /* Skip the main file; we don't need it, and it must be
+	     allocated last, so that it will show up before the
+	     non-primary symtabs in the objfile's symtab list.  */
+	  if (current_subfile == first_subfile)
+	    continue;
+
+	  if (current_subfile->symtab == NULL)
+	    current_subfile->symtab = allocate_symtab (current_subfile->name,
+						       cu->objfile);
+	  fe->symtab = current_subfile->symtab;
+	}
+    }
 }
 
 /* Start a subfile for DWARF.  FILENAME is the name of the file and
@@ -7024,6 +7069,23 @@ new_symbol (struct die_info *die, struct
 	{
 	  SYMBOL_LINE (sym) = DW_UNSND (attr);
 	}
+
+      attr = dwarf2_attr (die, DW_AT_decl_file, cu);
+      if (attr)
+	{
+	  int file_index = DW_UNSND (attr);
+	  if (cu->line_header == NULL
+	      || file_index > cu->line_header->num_file_names)
+	    complaint (&symfile_complaints,
+		       _("file index out of range"));
+	  else
+	    {
+	      struct file_entry *fe;
+	      fe = &cu->line_header->file_names[file_index - 1];
+	      SYMBOL_SYMTAB (sym) = fe->symtab;
+	    }
+	}
+
       switch (die->tag)
 	{
 	case DW_TAG_label:
Index: symtab.c
===================================================================
RCS file: /cvs/src/src/gdb/symtab.c,v
retrieving revision 1.152
diff -u -p -r1.152 symtab.c
--- symtab.c	9 Jan 2007 22:43:08 -0000	1.152
+++ symtab.c	21 Jan 2007 16:42:10 -0000
@@ -1133,6 +1133,10 @@ lookup_symbol (const char *name, const s
   if (needtofreename)
     xfree (demangled_name);
 
+  /* Override the returned symtab with the symbol's specific one.  */
+  if (returnval != NULL && symtab != NULL)
+    *symtab = SYMBOL_SYMTAB (returnval);
+
   return returnval;	 
 }
 
@@ -3008,7 +3012,11 @@ search_symbols (char *regexp, domain_enu
 	    QUIT;
 
 	    /* If it would match (logic taken from loop below)
-	       load the file and go on to the next one */
+	       load the file and go on to the next one.  We check the
+	       filename here, but that's a bit bogus: we don't know
+	       what file it really comes from until we have full
+	       symtabs.  The symbol might be in a header file included by
+	       this psymtab.  This only affects Insight.  */
 	    if (file_matches (ps->filename, files, nfiles)
 		&& ((regexp == NULL
 		     || re_exec (SYMBOL_NATURAL_NAME (*psym)) != 0)
@@ -3087,8 +3095,10 @@ search_symbols (char *regexp, domain_enu
 	  b = BLOCKVECTOR_BLOCK (bv, i);
 	  ALL_BLOCK_SYMBOLS (b, iter, sym)
 	    {
+	      struct symtab *real_symtab = SYMBOL_SYMTAB (sym);
 	      QUIT;
-	      if (file_matches (s->filename, files, nfiles)
+
+	      if (file_matches (real_symtab->filename, files, nfiles)
 		  && ((regexp == NULL
 		       || re_exec (SYMBOL_NATURAL_NAME (sym)) != 0)
 		      && ((kind == VARIABLES_DOMAIN && SYMBOL_CLASS (sym) != LOC_TYPEDEF
@@ -3101,7 +3111,7 @@ search_symbols (char *regexp, domain_enu
 		  /* match */
 		  psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
 		  psr->block = i;
-		  psr->symtab = s;
+		  psr->symtab = real_symtab;
 		  psr->symbol = sym;
 		  psr->msymbol = NULL;
 		  psr->next = NULL;
Index: symtab.h
===================================================================
RCS file: /cvs/src/src/gdb/symtab.h,v
retrieving revision 1.100
diff -u -p -r1.100 symtab.h
--- symtab.h	9 Jan 2007 17:58:59 -0000	1.100
+++ symtab.h	21 Jan 2007 16:42:11 -0000
@@ -609,6 +609,10 @@ struct symbol
 
   struct type *type;
 
+  /* The symbol table containing this symbol.  This is the file
+     associated with LINE.  */
+  struct symtab *symtab;
+
   /* Domain code.  */
 
   ENUM_BITFIELD(domain_enum_tag) domain : 6;
@@ -664,6 +668,7 @@ struct symbol
 #define SYMBOL_CLASS(symbol)		(symbol)->aclass
 #define SYMBOL_TYPE(symbol)		(symbol)->type
 #define SYMBOL_LINE(symbol)		(symbol)->line
+#define SYMBOL_SYMTAB(symbol)		(symbol)->symtab
 #define SYMBOL_BASEREG(symbol)		(symbol)->aux_value.basereg
 #define SYMBOL_OBJFILE(symbol)          (symbol)->aux_value.objfile
 #define SYMBOL_OPS(symbol)              (symbol)->ops
Index: testsuite/gdb.base/included.c
===================================================================
RCS file: testsuite/gdb.base/included.c
diff -N testsuite/gdb.base/included.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.base/included.c	21 Jan 2007 16:42:11 -0000
@@ -0,0 +1,26 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2007 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+ 
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.  */
+
+#include "included.h"
+
+int
+main()
+{
+  return 0;
+}
Index: testsuite/gdb.base/included.exp
===================================================================
RCS file: testsuite/gdb.base/included.exp
diff -N testsuite/gdb.base/included.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.base/included.exp	21 Jan 2007 16:42:11 -0000
@@ -0,0 +1,46 @@
+# Copyright 2007 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+set testfile "included"
+set srcfile ${testfile}.c
+set binfile ${objdir}/${subdir}/${testfile}
+
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
+    untested included.exp
+    return -1
+}
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+gdb_load ${binfile}
+
+gdb_test "set listsize 1" ""
+
+gdb_test "list main" ".*"
+get_debug_format
+set non_dwarf [expr ! [test_debug_format "DWARF 2"]]
+
+# We should be able to find the source file containing the definition,
+# even though it was an included header.
+if { $non_dwarf } { setup_xfail *-*-* }
+gdb_test "list integer" "int integer;"
+
+gdb_test "ptype integer" "type = int"
+
+# We should report that integer comes from the header file.
+if { $non_dwarf } { setup_xfail *-*-* }
+gdb_test "info variables integer" "\r\nFile \[^\r\n\]*/${subdir}/${testfile}.h:\r\nint integer;"
Index: testsuite/gdb.base/included.h
===================================================================
RCS file: testsuite/gdb.base/included.h
diff -N testsuite/gdb.base/included.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.base/included.h	21 Jan 2007 16:42:11 -0000
@@ -0,0 +1,20 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2007 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+ 
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   USA.  */
+
+int integer;


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Follow specific symbol's DW_AT_decl_file
  2007-01-21 16:55 ` Daniel Jacobowitz
@ 2007-01-21 22:35   ` Jan Kratochvil
  2007-01-21 22:56     ` Daniel Jacobowitz
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Kratochvil @ 2007-01-21 22:35 UTC (permalink / raw)
  To: gdb-patches; +Cc: Daniel Jacobowitz

On Sun, 21 Jan 2007 17:54:51 +0100, Daniel Jacobowitz wrote:
...
> /* This structure is space critical.  See space comments at the top.  */
> 
> So I wrote an implementation that used an on-the-side hash table.  But
> in the end, it was a bit slower, and most symbols needed one.  Even
> with -readnow the memory increase was only 3%, so it will be less than
> that in practice.  So I went back to doing it in the symbol after all
> :-)

Due to that comment I originally valgrind(1)ed it and on my testcase the memory
requirement increase was visible but negligible.


> The other changes I made were to move the index handling into
> dwarf2read.c, since the concept of a file index is dwarf specific,
> and to move the test into gdb.base, since in theory other debug
> formats could get it right.

Thanks, it unexpectedly cleaned up the patch a lot.


Regards,
Jan


> I tested this on x86_64-pc-linux-gnu and committed it.

(fine on local RH i686-pc-linux-gnu)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Follow specific symbol's DW_AT_decl_file
  2007-01-21 22:35   ` Jan Kratochvil
@ 2007-01-21 22:56     ` Daniel Jacobowitz
  2007-01-21 23:26       ` Jan Kratochvil
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Jacobowitz @ 2007-01-21 22:56 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

On Sun, Jan 21, 2007 at 11:35:33PM +0100, Jan Kratochvil wrote:
> On Sun, 21 Jan 2007 17:54:51 +0100, Daniel Jacobowitz wrote:
> ...
> > /* This structure is space critical.  See space comments at the top.  */
> > 
> > So I wrote an implementation that used an on-the-side hash table.  But
> > in the end, it was a bit slower, and most symbols needed one.  Even
> > with -readnow the memory increase was only 3%, so it will be less than
> > that in practice.  So I went back to doing it in the symbol after all
> > :-)
> 
> Due to that comment I originally valgrind(1)ed it and on my testcase the memory
> requirement increase was visible but negligible.

How did you use valgrind for this - massif?  I hadn't tried that one
before.  I used an old copy of glibc's memusage script; massif
is a bit more interesting.

-- 
Daniel Jacobowitz
CodeSourcery


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Follow specific symbol's DW_AT_decl_file
  2007-01-21 22:56     ` Daniel Jacobowitz
@ 2007-01-21 23:26       ` Jan Kratochvil
  2007-01-22  0:26         ` Daniel Jacobowitz
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Kratochvil @ 2007-01-21 23:26 UTC (permalink / raw)
  To: gdb-patches; +Cc: Daniel Jacobowitz

On Sun, 21 Jan 2007 23:56:10 +0100, Daniel Jacobowitz wrote:
> On Sun, Jan 21, 2007 at 11:35:33PM +0100, Jan Kratochvil wrote:
...
> > Due to that comment I originally valgrind(1)ed it and on my testcase the memory
> > requirement increase was visible but negligible.
> 
> How did you use valgrind for this - massif?

IIRC the default `memcheck'.  I checked its `bytes allocated' - total across
the run, not the peak total allocations size - a bit bogus but if this does not
increase much it can be assumed the code change is OK.


> I hadn't tried that one before.  I used an old copy of glibc's memusage
> script; massif is a bit more interesting.

Thanks for the tip next time I need it.  Interesting valgrind's memcheck says
618MB total and memusage 134MB total (55MB peak) but I did not check more.


Regards,
Jan


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Follow specific symbol's DW_AT_decl_file
  2007-01-21 23:26       ` Jan Kratochvil
@ 2007-01-22  0:26         ` Daniel Jacobowitz
  2007-01-22 19:23           ` Jan Kratochvil
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Jacobowitz @ 2007-01-22  0:26 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

On Mon, Jan 22, 2007 at 12:26:11AM +0100, Jan Kratochvil wrote:
> > I hadn't tried that one before.  I used an old copy of glibc's memusage
> > script; massif is a bit more interesting.
> 
> Thanks for the tip next time I need it.  Interesting valgrind's memcheck says
> 618MB total and memusage 134MB total (55MB peak) but I did not check more.

memusage --mmap might be the difference.

-- 
Daniel Jacobowitz
CodeSourcery


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Follow specific symbol's DW_AT_decl_file
  2007-01-22  0:26         ` Daniel Jacobowitz
@ 2007-01-22 19:23           ` Jan Kratochvil
  0 siblings, 0 replies; 7+ messages in thread
From: Jan Kratochvil @ 2007-01-22 19:23 UTC (permalink / raw)
  To: gdb-patches; +Cc: Daniel Jacobowitz

On Mon, 22 Jan 2007 01:26:11 +0100, Daniel Jacobowitz wrote:
> On Mon, Jan 22, 2007 at 12:26:11AM +0100, Jan Kratochvil wrote:
> > > I hadn't tried that one before.  I used an old copy of glibc's memusage
> > > script; massif is a bit more interesting.
> > 
> > Thanks for the tip next time I need it.  Interesting valgrind's memcheck says
> > 618MB total and memusage 134MB total (55MB peak) but I did not check more.
> 
> memusage --mmap might be the difference.

That makes no difference.  But I found valgrind(1)'s memcheck counts
realloc(3)s' only the new sizes while memusage(1) counts realloc(3)s' size
differences.


Regards,
Jan


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-01-22 19:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-11 22:39 [PATCH] Follow specific symbol's DW_AT_decl_file Jan Kratochvil
2007-01-21 16:55 ` Daniel Jacobowitz
2007-01-21 22:35   ` Jan Kratochvil
2007-01-21 22:56     ` Daniel Jacobowitz
2007-01-21 23:26       ` Jan Kratochvil
2007-01-22  0:26         ` Daniel Jacobowitz
2007-01-22 19:23           ` Jan Kratochvil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox