Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] refreshed patch for PR 11804 and PR 9904
@ 2012-07-18  0:50 Jean-Marc Saffroy
  2012-07-28  7:05 ` Jan Kratochvil
  2012-07-28  7:10 ` Jan Kratochvil
  0 siblings, 2 replies; 8+ messages in thread
From: Jean-Marc Saffroy @ 2012-07-18  0:50 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: TEXT/PLAIN, Size: 645 bytes --]

Hi,

I just found out that gcore has been broken with some Linux kernels for a 
while (observed with some 3.2 kernels on Ubuntu, but not on a vanilla 
2.6.36 on another box), and this led me there:

http://sourceware.org/bugzilla/show_bug.cgi?id=9904
http://sourceware.org/bugzilla/show_bug.cgi?id=11804

In particular, 11804 has an interesting discussion of the problem, and a 
very nice patch, which I've refreshed for gdb 7.4.1. So here it is, if 
anyone else needs it: it fixes gcore for my case, but I didn't check the 
test suite bits.

It would be nice to see this finally landed in gdb!


Cheers,
Jean-Marc Saffroy

-- 
saffroy@gmail.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: TEXT/x-diff; name=gcore.patch, Size: 12519 bytes --]

diff --git a/gdb/defs.h b/gdb/defs.h
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -672,9 +672,10 @@ extern void init_source_path (void);
 
 /* From exec.c */
 
+/* MODIFIED has value -1 for unknown, 0 for not modified, 1 for modified.  */
 typedef int (*find_memory_region_ftype) (CORE_ADDR addr, unsigned long size,
 					 int read, int write, int exec,
-					 void *data);
+					 int modified, void *data);
 
 /* Take over the 'find_mapped_memory' vector from exec.c.  */
 extern void exec_set_find_memory_regions
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -132,7 +132,7 @@ fbsd_find_memory_regions (find_memory_re
 	}
 
       /* Invoke the callback function to create the corefile segment.  */
-      func (start, size, read, write, exec, obfd);
+      func (start, size, read, write, exec, -1, obfd);
     }
 
   do_cleanups (cleanup);
diff --git a/gdb/gcore.c b/gdb/gcore.c
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -377,8 +377,8 @@ make_output_phdrs (bfd *obfd, asection *
 }
 
 static int
-gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
-		       int read, int write, int exec, void *data)
+gcore_create_callback (CORE_ADDR vaddr, unsigned long size, int read,
+		       int write, int exec, int modified, void *data)
 {
   bfd *obfd = data;
   asection *osec;
@@ -387,7 +387,7 @@ gcore_create_callback (CORE_ADDR vaddr, 
   /* If the memory segment has no permissions set, ignore it, otherwise
      when we later try to access it for read/write, we'll get an error
      or jam the kernel.  */
-  if (read == 0 && write == 0 && exec == 0)
+  if (read == 0 && write == 0 && exec == 0 && modified == 0)
     {
       if (info_verbose)
         {
@@ -398,7 +398,7 @@ gcore_create_callback (CORE_ADDR vaddr, 
       return 0;
     }
 
-  if (write == 0 && !solib_keep_data_in_core (vaddr, size))
+  if (write == 0 && modified == 0 && !solib_keep_data_in_core (vaddr, size))
     {
       /* See if this region of memory lies inside a known file on disk.
 	 If so, we can avoid copying its contents by clearing SEC_LOAD.  */
@@ -430,10 +430,12 @@ gcore_create_callback (CORE_ADDR vaddr, 
 	    }
 	}
 
-    keep:
-      flags |= SEC_READONLY;
+    keep:;
     }
 
+  if (write == 0)
+    flags |= SEC_READONLY;
+
   if (exec)
     flags |= SEC_CODE;
   else
@@ -483,6 +485,7 @@ objfile_find_memory_regions (find_memory
 			 1, /* All sections will be readable.  */
 			 (flags & SEC_READONLY) == 0, /* Writable.  */
 			 (flags & SEC_CODE) != 0, /* Executable.  */
+			 -1, /* Modified is unknown.  */
 			 obfd);
 	  if (ret != 0)
 	    return ret;
@@ -495,6 +498,7 @@ objfile_find_memory_regions (find_memory
 	     1, /* Stack section will be readable.  */
 	     1, /* Stack section will be writable.  */
 	     0, /* Stack section will not be executable.  */
+	     1, /* Stack section will be modified.  */
 	     obfd);
 
   /* Make a heap segment.  */
@@ -503,6 +507,7 @@ objfile_find_memory_regions (find_memory
 	     1, /* Heap section will be readable.  */
 	     1, /* Heap section will be writable.  */
 	     0, /* Heap section will not be executable.  */
+	     1, /* Heap section will be modified.  */
 	     obfd);
 
   return 0;
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2558,7 +2558,7 @@ gnu_find_memory_regions (find_memory_reg
 		     last_protection & VM_PROT_READ,
 		     last_protection & VM_PROT_WRITE,
 		     last_protection & VM_PROT_EXECUTE,
-		     data);
+		     -1, data);
 	  last_region_address = region_address;
 	  last_region_end = region_address += region_length;
 	  last_protection = protection;
@@ -2571,7 +2571,7 @@ gnu_find_memory_regions (find_memory_reg
 	     last_protection & VM_PROT_READ,
 	     last_protection & VM_PROT_WRITE,
 	     last_protection & VM_PROT_EXECUTE,
-	     data);
+	     -1, data);
 
   return 0;
 }
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -4352,7 +4352,7 @@ read_mapping (FILE *mapfile,
 	      long long *endaddr,
 	      char *permissions,
 	      long long *offset,
-	      char *device, long long *inode, char *filename)
+	      char *device, long long *inode, char *filename, int *modified)
 {
   int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
 		    addr, endaddr, permissions, offset, device, inode);
@@ -4370,6 +4370,40 @@ read_mapping (FILE *mapfile,
       ret += fscanf (mapfile, "%[^\n]\n", filename);
     }
 
+  if (modified != NULL)
+    {
+      *modified = -1;
+      for (;;)
+	{
+	  int ch;
+	  char keyword[64 + 1];
+	  unsigned long number;
+
+	  ch = fgetc (mapfile);
+	  if (ch != EOF)
+	    ungetc (ch, mapfile);
+	  if (ch < 'A' || ch > 'Z')
+	    break;
+	  if (fscanf (mapfile, "%64s%lu kB\n", keyword, &number) != 2)
+	    {
+	      warning (_("Error parsing /proc/PID/{s,}maps file"));
+	      do
+		ch = fgetc (mapfile);
+	      while (ch != EOF && ch != '\n');
+	      break;
+	    }
+	  if (number != 0 && (strcmp (keyword, "Shared_Dirty:") == 0
+			      || strcmp (keyword, "Private_Dirty:") == 0
+			      || strcmp (keyword, "Swap:") == 0))
+	    *modified = 1;
+	  else if (*modified == -1)
+	    {
+	      /* Valid line proves an smaps file is being read in.  */
+	      *modified = 0;
+	    }
+	}
+    }
+
   return (ret != 0 && ret != EOF);
 }
 
@@ -4384,13 +4418,17 @@ linux_nat_find_memory_regions (find_memo
   FILE *mapsfile;
   long long addr, endaddr, size, offset, inode;
   char permissions[8], device[8], filename[MAXPATHLEN];
-  int read, write, exec;
+  int read, write, exec, modified;
   struct cleanup *cleanup;
 
   /* Compose the filename for the /proc memory map, and open it.  */
-  sprintf (mapsfilename, "/proc/%d/maps", pid);
+  sprintf (mapsfilename, "/proc/%d/smaps", pid);
   if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
-    error (_("Could not open %s."), mapsfilename);
+    {
+      sprintf (mapsfilename, "/proc/%d/maps", pid);
+      if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
+        error (_("Could not open %s."), mapsfilename);
+    }
   cleanup = make_cleanup_fclose (mapsfile);
 
   if (info_verbose)
@@ -4399,7 +4437,7 @@ linux_nat_find_memory_regions (find_memo
 
   /* Now iterate until end-of-file.  */
   while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
-		       &offset, &device[0], &inode, &filename[0]))
+		       &offset, &device[0], &inode, &filename[0], &modified))
     {
       size = endaddr - addr;
 
@@ -4422,7 +4460,7 @@ linux_nat_find_memory_regions (find_memo
 
       /* Invoke the callback function to create the corefile
 	 segment.  */
-      func (addr, size, read, write, exec, obfd);
+      func (addr, size, read, write, exec, modified, obfd);
     }
   do_cleanups (cleanup);
   return 0;
@@ -4871,7 +4909,8 @@ linux_nat_info_proc_cmd_1 (char *args, e
 	    }
 
 	  while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
-			       &offset, &device[0], &inode, &filename[0]))
+			       &offset, &device[0], &inode, &filename[0],
+			       NULL))
 	    {
 	      size = endaddr - addr;
 
diff --git a/gdb/procfs.c b/gdb/procfs.c
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -5275,7 +5275,7 @@ find_memory_regions_callback (struct prm
 		  (map->pr_mflags & MA_READ) != 0,
 		  (map->pr_mflags & MA_WRITE) != 0,
 		  (map->pr_mflags & MA_EXEC) != 0,
-		  data);
+		  -1, data);
 }
 
 /* External interface.  Calls a callback function once for each
diff --git a/gdb/testsuite/gdb.base/gcore-relro-lib.c b/gdb/testsuite/gdb.base/gcore-relro-lib.c
new file mode 100644
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro-lib.c
@@ -0,0 +1,21 @@
+/* Copyright 2010 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 3 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, see <http://www.gnu.org/licenses/>.  */
+
+void
+lib (void)
+{
+}
diff --git a/gdb/testsuite/gdb.base/gcore-relro-main.c b/gdb/testsuite/gdb.base/gcore-relro-main.c
new file mode 100644
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro-main.c
@@ -0,0 +1,25 @@
+/* Copyright 2010 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 3 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, see <http://www.gnu.org/licenses/>.  */
+
+extern void lib (void);
+
+int
+main (void)
+{
+  lib ();
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/gcore-relro.exp b/gdb/testsuite/gdb.base/gcore-relro.exp
new file mode 100644
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro.exp
@@ -0,0 +1,80 @@
+# Copyright 2010 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 3 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, see <http://www.gnu.org/licenses/>.
+
+if {[skip_shlib_tests]} {
+    return 0
+}
+
+set testfile "gcore-relro"
+set srcmainfile ${testfile}-main.c
+set srclibfile ${testfile}-lib.c
+set libfile ${objdir}/${subdir}/${testfile}-lib.so
+set objfile ${objdir}/${subdir}/${testfile}-main.o
+set executable ${testfile}-main
+set binfile ${objdir}/${subdir}/${executable}
+set gcorefile ${objdir}/${subdir}/${executable}.gcore
+
+if { [gdb_compile_shlib ${srcdir}/${subdir}/${srclibfile} ${libfile} {debug}] != ""
+     || [gdb_compile ${srcdir}/${subdir}/${srcmainfile} ${objfile} object {debug}] != "" } {
+     untested ${testfile}.exp
+     return -1
+}
+set opts [list debug shlib=${libfile} additional_flags=-Wl,-z,relro]
+if { [gdb_compile ${objfile} ${binfile} executable $opts] != "" } {
+     unsupported "-Wl,-z,relro compilation failed"
+     return -1
+}
+
+clean_restart $executable
+gdb_load_shlibs $libfile
+
+# Does this gdb support gcore?
+set test "help gcore"
+gdb_test_multiple $test $test {
+    -re "Undefined command: .gcore.*\r\n$gdb_prompt $" {
+	# gcore command not supported -- nothing to test here.
+	unsupported "gdb does not support gcore on this target"
+	return -1;
+    }
+    -re "Save a core file .*\r\n$gdb_prompt $" {
+	pass $test
+    }
+}
+
+if { ![runto lib] } then {
+    return -1
+}
+
+set escapedfilename [string_to_regexp ${gcorefile}]
+
+set test "save a corefile"
+gdb_test_multiple "gcore ${gcorefile}" $test {
+    -re "Saved corefile ${escapedfilename}\r\n$gdb_prompt $" {
+	pass $test
+    }
+    -re "Can't create a corefile\r\n$gdb_prompt $" {
+	unsupported $test
+	return -1
+    }
+}
+
+# Now restart gdb and load the corefile.
+
+clean_restart $executable
+gdb_load_shlibs $libfile
+
+gdb_test "core ${gcorefile}" "Core was generated by .*" "re-load generated corefile"
+
+gdb_test "frame" "#0 \[^\r\n\]* lib .*" "library got loaded"

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

* Re: [PATCH] refreshed patch for PR 11804 and PR 9904
  2012-07-18  0:50 [PATCH] refreshed patch for PR 11804 and PR 9904 Jean-Marc Saffroy
@ 2012-07-28  7:05 ` Jan Kratochvil
  2012-07-29 17:11   ` Jean-Marc Saffroy
  2012-07-28  7:10 ` Jan Kratochvil
  1 sibling, 1 reply; 8+ messages in thread
From: Jan Kratochvil @ 2012-07-28  7:05 UTC (permalink / raw)
  To: Jean-Marc Saffroy; +Cc: gdb-patches

Hello,

> which I've refreshed for gdb 7.4.1. So here it is,

this is unfortunately not sufficient, 7.4.1 is too old.  The patch is not
applicable to FSF GDB HEAD - to be accepted.  See how to download HEAD:
	http://sources.redhat.com/gdb/current/

Primarily linux_nat_find_memory_regions is now reworked and renamed as
linux_find_memory_regions as GDB can now gcore also from remotely run process
via gdbserver.  The testcase should be verified it also works with gdbserver
afterwards, see gdb/testsuite/boards/native-gdbserver.exp.

Also there should be descriptive comments before each modified function.
I can write those otherwise.


On Wed, 18 Jul 2012 02:50:08 +0200, Jean-Marc Saffroy wrote:

static int
objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
  /* Call callback function for each objfile section.  */
  ALL_OBJSECTIONS (objfile, objsec)

Here is missing a check:
    if (separate_debug_objfile_backlink != NULL)
      continue;
as separate debug info files (in /usr/lib/debug, sometimes with .debug
extension) are irrelevant for gcore.


> --- a/gdb/linux-nat.c
> +++ b/gdb/linux-nat.c
[...]
> @@ -4370,6 +4370,40 @@ read_mapping (FILE *mapfile,
[...]
> +	  if (fscanf (mapfile, "%64s%lu kB\n", keyword, &number) != 2)
> +	    {
> +	      warning (_("Error parsing /proc/PID/{s,}maps file"));
> +	      do
> +		ch = fgetc (mapfile);
> +	      while (ch != EOF && ch != '\n');
> +	      break;
> +	    }
> +	  if (number != 0 && (strcmp (keyword, "Shared_Dirty:") == 0
> +			      || strcmp (keyword, "Private_Dirty:") == 0
> +			      || strcmp (keyword, "Swap:") == 0))

The patch is primarily missing the check for "Anonymous:" entry in recent
Linux kernels, described by Ulrich Weigand in:
	http://sourceware.org/ml/gdb-patches/2010-09/msg00398.html

Therefore if "Anonymous:" entry is not present at all (=not supported by Linux
kernel on that host) we must write out very every area as we have no way to
verify for swap-cached pages (having Anonymous > 0).

Sure if "Anonymous:" is present and it is > 0 it also must be written out.

You can reproduce the "Anonymous:" problem with
	http://people.redhat.com/jkratoch/relroswap.c
although it is not suitable for GDB testsuite as it needs to do huge memory
allocations to swap out (+swap in) the testcase pages.
(If you have a better idea how to make "Anonymous:" entry non-zero it is sure
welcome.  I have tried madvise (MADV_DONTNEED) but it does not work.)


> +	    *modified = 1;
> +	  else if (*modified == -1)
> +	    {
> +	      /* Valid line proves an smaps file is being read in.  */
> +	      *modified = 0;
> +	    }
> +	}
> +    }
> +
>    return (ret != 0 && ret != EOF);
>  }
>  
[...]
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/gcore-relro.exp
> @@ -0,0 +1,80 @@
> +# Copyright 2010 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 3 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, see <http://www.gnu.org/licenses/>.
> +
> +if {[skip_shlib_tests]} {
> +    return 0
> +}
> +
> +set testfile "gcore-relro"

Is hould use 'standard_testfile' like other testcases recently modified by Tom
Tromey.


> +set srcmainfile ${testfile}-main.c
> +set srclibfile ${testfile}-lib.c
> +set libfile ${objdir}/${subdir}/${testfile}-lib.so
> +set objfile ${objdir}/${subdir}/${testfile}-main.o
> +set executable ${testfile}-main
> +set binfile ${objdir}/${subdir}/${executable}
> +set gcorefile ${objdir}/${subdir}/${executable}.gcore
> +
> +if { [gdb_compile_shlib ${srcdir}/${subdir}/${srclibfile} ${libfile} {debug}] != ""
> +     || [gdb_compile ${srcdir}/${subdir}/${srcmainfile} ${objfile} object {debug}] != "" } {
> +     untested ${testfile}.exp
> +     return -1
> +}
> +set opts [list debug shlib=${libfile} additional_flags=-Wl,-z,relro]
> +if { [gdb_compile ${objfile} ${binfile} executable $opts] != "" } {
> +     unsupported "-Wl,-z,relro compilation failed"
> +     return -1
> +}
> +
> +clean_restart $executable
> +gdb_load_shlibs $libfile
[...]


Thanks,
Jan


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

* Re: [PATCH] refreshed patch for PR 11804 and PR 9904
  2012-07-18  0:50 [PATCH] refreshed patch for PR 11804 and PR 9904 Jean-Marc Saffroy
  2012-07-28  7:05 ` Jan Kratochvil
@ 2012-07-28  7:10 ` Jan Kratochvil
  2012-07-31 13:23   ` Jan Kratochvil
  1 sibling, 1 reply; 8+ messages in thread
From: Jan Kratochvil @ 2012-07-28  7:10 UTC (permalink / raw)
  To: Jean-Marc Saffroy; +Cc: gdb-patches

Hello Jean-Marc,

besides the technical part see the file 'gdb/CONTRIBUTE', GDB patches require
FSF ChangeLog entries:
	http://www.gnu.org/prep/standards/standards.html#Change-Logs

And this patch is large enough to require FSF GDB copyright assignment, you
can get the snail-mail form from:
	Sergio Durigan Junior <sergiodj@redhat.com>

Still we can work on the patch in the meantime, the copyright assignment is
needed only before the final check-in.


Thanks,
Jan


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

* Re: [PATCH] refreshed patch for PR 11804 and PR 9904
  2012-07-28  7:05 ` Jan Kratochvil
@ 2012-07-29 17:11   ` Jean-Marc Saffroy
  2012-07-29 17:35     ` Jean-Marc Saffroy
  2012-07-31 13:21     ` Jan Kratochvil
  0 siblings, 2 replies; 8+ messages in thread
From: Jean-Marc Saffroy @ 2012-07-29 17:11 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

[-- Attachment #1: Type: TEXT/PLAIN, Size: 2024 bytes --]

Hi Jan,

On Sat, 28 Jul 2012, Jan Kratochvil wrote:

> 7.4.1 is too old.

Here is a patch against a fresh checkout from the git mirror.

> Primarily linux_nat_find_memory_regions is now reworked and renamed as
> linux_find_memory_regions as GDB can now gcore also from remotely run process
> via gdbserver.  The testcase should be verified it also works with gdbserver
> afterwards, see gdb/testsuite/boards/native-gdbserver.exp.

I only tested the added test case:

make -C gdb/testsuite/ check RUNTESTFLAGS="gdb.base/gcore-relro.exp"

make -C gdb/testsuite/ check RUNTESTFLAGS="--target_board=native-gdbserver 
gdb.base/gcore-relro.exp"

> Also there should be descriptive comments before each modified function.
> I can write those otherwise.

I'm not sure what kind you expect, so I'll let you write them. :-)

> Here is missing a check:
>     if (separate_debug_objfile_backlink != NULL)
>       continue;
> as separate debug info files (in /usr/lib/debug, sometimes with .debug
> extension) are irrelevant for gcore.

Should be good now.

> Therefore if "Anonymous:" entry is not present at all (=not supported by 
> Linux kernel on that host) we must write out very every area as we have 
> no way to verify for swap-cached pages (having Anonymous > 0).
> 
> Sure if "Anonymous:" is present and it is > 0 it also must be written 
> out.

Ditto.

> You can reproduce the "Anonymous:" problem with
> 	http://people.redhat.com/jkratoch/relroswap.c
> although it is not suitable for GDB testsuite as it needs to do huge memory
> allocations to swap out (+swap in) the testcase pages.
> (If you have a better idea how to make "Anonymous:" entry non-zero it is sure
> welcome.  I have tried madvise (MADV_DONTNEED) but it does not work.)

Starting the test makes my desktop unusable (no SSD for swap yet), so I 
cowardly gave up.

> > +set testfile "gcore-relro"
> 
> Is hould use 'standard_testfile' like other testcases recently modified 
> by Tom Tromey.

I hope that's ok now.


Cheers,
Jean-Marc

-- 
saffroy@gmail.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: TEXT/x-diff; name=gcore-git.patch, Size: 12587 bytes --]

diff --git a/gdb/defs.h b/gdb/defs.h
index 7be99a2..feb3106 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -362,9 +362,10 @@ extern void init_source_path (void);
 
 /* From exec.c */
 
+/* MODIFIED has value -1 for unknown, 0 for not modified, 1 for modified.  */
 typedef int (*find_memory_region_ftype) (CORE_ADDR addr, unsigned long size,
 					 int read, int write, int exec,
-					 void *data);
+					 int modified, void *data);
 
 /* Take over the 'find_mapped_memory' vector from exec.c.  */
 extern void exec_set_find_memory_regions
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index b3e4fab..5db0f5d 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -132,7 +132,7 @@ fbsd_find_memory_regions (find_memory_region_ftype func, void *obfd)
 	}
 
       /* Invoke the callback function to create the corefile segment.  */
-      func (start, size, read, write, exec, obfd);
+      func (start, size, read, write, exec, -1, obfd);
     }
 
   do_cleanups (cleanup);
diff --git a/gdb/gcore.c b/gdb/gcore.c
index a45e787..a9dd2ba 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -380,8 +380,8 @@ make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
 }
 
 static int
-gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
-		       int read, int write, int exec, void *data)
+gcore_create_callback (CORE_ADDR vaddr, unsigned long size, int read,
+		       int write, int exec, int modified, void *data)
 {
   bfd *obfd = data;
   asection *osec;
@@ -390,7 +390,7 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
   /* If the memory segment has no permissions set, ignore it, otherwise
      when we later try to access it for read/write, we'll get an error
      or jam the kernel.  */
-  if (read == 0 && write == 0 && exec == 0)
+  if (read == 0 && write == 0 && exec == 0 && modified == 0)
     {
       if (info_verbose)
         {
@@ -401,7 +401,7 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
       return 0;
     }
 
-  if (write == 0 && !solib_keep_data_in_core (vaddr, size))
+  if (write == 0 && modified == 0 && !solib_keep_data_in_core (vaddr, size))
     {
       /* See if this region of memory lies inside a known file on disk.
 	 If so, we can avoid copying its contents by clearing SEC_LOAD.  */
@@ -433,10 +433,12 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
 	    }
 	}
 
-    keep:
-      flags |= SEC_READONLY;
+    keep:;
     }
 
+  if (write == 0)
+    flags |= SEC_READONLY;
+
   if (exec)
     flags |= SEC_CODE;
   else
@@ -477,6 +479,10 @@ objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
       asection *isec = objsec->the_bfd_section;
       flagword flags = bfd_get_section_flags (ibfd, isec);
 
+      /* Separate debug info files are irrelevant for gcore.  */
+      if (objfile->separate_debug_objfile_backlink != NULL)
+	continue;
+
       if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
 	{
 	  int size = bfd_section_size (ibfd, isec);
@@ -486,6 +492,7 @@ objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
 			 1, /* All sections will be readable.  */
 			 (flags & SEC_READONLY) == 0, /* Writable.  */
 			 (flags & SEC_CODE) != 0, /* Executable.  */
+			 -1, /* Modified is unknown.  */
 			 obfd);
 	  if (ret != 0)
 	    return ret;
@@ -498,6 +505,7 @@ objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
 	     1, /* Stack section will be readable.  */
 	     1, /* Stack section will be writable.  */
 	     0, /* Stack section will not be executable.  */
+	     1, /* Stack section will be modified.  */
 	     obfd);
 
   /* Make a heap segment.  */
@@ -506,6 +514,7 @@ objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
 	     1, /* Heap section will be readable.  */
 	     1, /* Heap section will be writable.  */
 	     0, /* Heap section will not be executable.  */
+	     1, /* Heap section will be modified.  */
 	     obfd);
 
   return 0;
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index 0c45f20..046e32a 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2558,7 +2558,7 @@ gnu_find_memory_regions (find_memory_region_ftype func, void *data)
 		     last_protection & VM_PROT_READ,
 		     last_protection & VM_PROT_WRITE,
 		     last_protection & VM_PROT_EXECUTE,
-		     data);
+		     -1, data);
 	  last_region_address = region_address;
 	  last_region_end = region_address += region_length;
 	  last_protection = protection;
@@ -2571,7 +2571,7 @@ gnu_find_memory_regions (find_memory_region_ftype func, void *data)
 	     last_protection & VM_PROT_READ,
 	     last_protection & VM_PROT_WRITE,
 	     last_protection & VM_PROT_EXECUTE,
-	     data);
+	     -1, data);
 
   return 0;
 }
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index b6f2efb..4a36fe4 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -547,19 +547,27 @@ linux_find_memory_regions (struct gdbarch *gdbarch,
     return 1;
 
   xsnprintf (filename, sizeof filename,
-	     "/proc/%d/maps", current_inferior ()->pid);
+	     "/proc/%d/smaps", current_inferior ()->pid);
   data = target_fileio_read_stralloc (filename);
+  if (! data)
+    {
+      xsnprintf (filename, sizeof filename,
+		 "/proc/%d/maps", current_inferior ()->pid);
+      data = target_fileio_read_stralloc (filename);
+    }
   if (data)
     {
       struct cleanup *cleanup = make_cleanup (xfree, data);
       char *line;
 
-      for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
+      line = strtok (data, "\n");
+      while (line)
 	{
 	  ULONGEST addr, endaddr, offset, inode;
 	  const char *permissions, *device, *filename;
 	  size_t permissions_len, device_len;
 	  int read, write, exec;
+	  int modified = 0, has_anonymous = 0;
 
 	  read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
 			&offset, &device, &device_len, &inode, &filename);
@@ -569,8 +577,34 @@ linux_find_memory_regions (struct gdbarch *gdbarch,
 	  write = (memchr (permissions, 'w', permissions_len) != 0);
 	  exec = (memchr (permissions, 'x', permissions_len) != 0);
 
+	  /* Try to detect if region was modified by parsing smaps counters.  */
+	  for (line = strtok (NULL, "\n");
+	       line && line[0] >= 'A' && line[0] <= 'Z';
+	       line = strtok (NULL, "\n"))
+	    {
+	      char keyword[64 + 1];
+	      unsigned long number;
+
+	      if (sscanf(line, "%64s%lu kB\n", keyword, &number) != 2)
+		{
+		  warning (_("Error parsing {s,}maps file '%s'"), filename);
+		  break;
+		}
+	      if (strcmp(keyword, "Anonymous:") == 0)
+		  has_anonymous = 1;
+	      if (number != 0 && (strcmp (keyword, "Shared_Dirty:") == 0
+				  || strcmp (keyword, "Private_Dirty:") == 0
+				  || strcmp (keyword, "Swap:") == 0
+				  || strcmp (keyword, "Anonymous:") == 0))
+		modified = 1;
+	    }
+
+	  /* When "Anonymous:" is missing, we can't be sure.  */
+	  if (! has_anonymous && modified == 0)
+	    modified = -1;
+
 	  /* Invoke the callback function to create the corefile segment.  */
-	  func (addr, endaddr - addr, read, write, exec, obfd);
+	  func (addr, endaddr - addr, read, write, exec, modified, obfd);
 	}
 
       do_cleanups (cleanup);
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 4409e5b..79f22de 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -5074,7 +5074,7 @@ find_memory_regions_callback (struct prmap *map,
 		  (map->pr_mflags & MA_READ) != 0,
 		  (map->pr_mflags & MA_WRITE) != 0,
 		  (map->pr_mflags & MA_EXEC) != 0,
-		  data);
+		  -1, data);
 }
 
 /* External interface.  Calls a callback function once for each
diff --git a/gdb/testsuite/gdb.base/gcore-relro-lib.c b/gdb/testsuite/gdb.base/gcore-relro-lib.c
new file mode 100644
index 0000000..d74b690
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro-lib.c
@@ -0,0 +1,21 @@
+/* Copyright 2010 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 3 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, see <http://www.gnu.org/licenses/>.  */
+
+void
+lib (void)
+{
+}
diff --git a/gdb/testsuite/gdb.base/gcore-relro-main.c b/gdb/testsuite/gdb.base/gcore-relro-main.c
new file mode 100644
index 0000000..46b9dfe
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro-main.c
@@ -0,0 +1,25 @@
+/* Copyright 2010 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 3 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, see <http://www.gnu.org/licenses/>.  */
+
+extern void lib (void);
+
+int
+main (void)
+{
+  lib ();
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/gcore-relro.exp b/gdb/testsuite/gdb.base/gcore-relro.exp
new file mode 100644
index 0000000..ace80e4
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro.exp
@@ -0,0 +1,78 @@
+# Copyright 2012 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 3 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, see <http://www.gnu.org/licenses/>.
+
+if {[skip_shlib_tests]} {
+    return 0
+}
+
+standard_testfile gcore-relro-main.c
+set libfile gcore-relro-lib
+set srcfile_lib ${libfile}.c
+set binfile_lib [standard_output_file ${libfile}.so]
+set gcorefile ${binfile}.gcore
+set objfile [standard_output_file ${testfile}.o]
+
+ if { [gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} {debug}] != ""
+      || [gdb_compile ${srcdir}/${subdir}/${srcfile} ${objfile} object {debug}] != "" } {
+      untested ${testfile}.exp
+      return -1
+ }
+ set opts [list debug shlib=${binfile_lib} additional_flags=-Wl,-z,relro]
+ if { [gdb_compile ${objfile} ${binfile} executable $opts] != "" } {
+      unsupported "-Wl,-z,relro compilation failed"
+      return -1
+ }
+
+clean_restart ${binfile}
+gdb_load_shlibs ${binfile_lib}
+
+# Does this gdb support gcore?
+set test "help gcore"
+gdb_test_multiple $test $test {
+    -re "Undefined command: .gcore.*\r\n$gdb_prompt $" {
+	# gcore command not supported -- nothing to test here.
+	unsupported "gdb does not support gcore on this target"
+	return -1;
+    }
+    -re "Save a core file .*\r\n$gdb_prompt $" {
+	pass $test
+    }
+}
+
+if { ![runto lib] } then {
+    return -1
+}
+
+set escapedfilename [string_to_regexp ${gcorefile}]
+
+set test "save a corefile"
+gdb_test_multiple "gcore ${gcorefile}" $test {
+    -re "Saved corefile ${escapedfilename}\r\n$gdb_prompt $" {
+	pass $test
+    }
+    -re "Can't create a corefile\r\n$gdb_prompt $" {
+	unsupported $test
+	return -1
+    }
+}
+
+# Now restart gdb and load the corefile.
+
+clean_restart ${binfile}
+gdb_load_shlibs ${binfile_lib}
+
+gdb_test "core ${gcorefile}" "Core was generated by .*" "re-load generated corefile"
+
+gdb_test "frame" "#0 \[^\r\n\]* lib .*" "library got loaded"

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

* Re: [PATCH] refreshed patch for PR 11804 and PR 9904
  2012-07-29 17:11   ` Jean-Marc Saffroy
@ 2012-07-29 17:35     ` Jean-Marc Saffroy
  2012-07-31 13:21     ` Jan Kratochvil
  1 sibling, 0 replies; 8+ messages in thread
From: Jean-Marc Saffroy @ 2012-07-29 17:35 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches

Of course I forgot to add a ChangeLog entry, so here it is:

2012-07-29  Jean-Marc Saffroy  <saffroy@gmail.com>

	PR 11804
	PR 9904

	* defs.h (find_memory_region_ftype): New arg 'modified' for gcore.
	All callers changed.
	* fbsd-nat.c (fbsd_find_memory_regions): Ditto.
	* gnu-nat.c (gnu_find_memory_regions): Ditto.
	* procfs.c (find_memory_regions_callback): Ditto.
	* gcore.c (gcore_create_callback): Only write modified regions.
	(objfile_find_memory_regions): Ignore debug files.
	* linux-tdep.c (linux_find_memory_regions): Detect modified 
regions.
	* testsuite/gdb.base/gcore-relro.exp: New file.
	* testsuite/gdb.base/gcore-relro-main.c: New file.
	* testsuite/gdb.base/gcore-relro-lib.c: New file.


Cheers, Jean-Marc

-- 
saffroy@gmail.com


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

* Re: [PATCH] refreshed patch for PR 11804 and PR 9904
  2012-07-29 17:11   ` Jean-Marc Saffroy
  2012-07-29 17:35     ` Jean-Marc Saffroy
@ 2012-07-31 13:21     ` Jan Kratochvil
  2012-08-07 13:27       ` [commit] " Jan Kratochvil
  1 sibling, 1 reply; 8+ messages in thread
From: Jan Kratochvil @ 2012-07-31 13:21 UTC (permalink / raw)
  To: Jean-Marc Saffroy; +Cc: gdb-patches

Hi Jean-Marc,

On Sun, 29 Jul 2012 19:11:12 +0200, Jean-Marc Saffroy wrote:
> I'm not sure what kind you expect, so I'll let you write them. :-)

OK, done.

On Sun, 29 Jul 2012 19:35:00 +0200, Jean-Marc Saffroy wrote:
> Of course I forgot to add a ChangeLog entry, so here it is:

Updated it.  Also sorry but I am still the primary author of this patch.

No regressions on {x86_64,x86_64-m32,i686}-fedorarawhide-linux-gnu and with
gdbserver.  I will check it in in some time.  While it would be nice for 7.5
I think it is too late for it.


Thanks,
Jan


2012-07-31  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Jean-Marc Saffroy  <saffroy@gmail.com>

	PR 11804
	* defs.h (find_memory_region_ftype): New comment.  New arg modified.
	* fbsd-nat.c (fbsd_find_memory_regions): Add the passed modified value.
	* gcore.c (gcore_create_callback): New function comment.  Add modified
	parameter.  Only write modified regions.  Set SEC_READONLY exactly
	according to MODIFIED.
	(objfile_find_memory_regions): Ignore separate debug info files.  Ass
	the passed modified value to FUNC.
	* gnu-nat.c (gnu_find_memory_regions): Add the passed modified value.
	* linux-tdep.c (linux_find_memory_regions): Try to reads smaps file
	first.  New variables modified and has_anonymous.  Parse the lines of
	smaps file.  Add the passed MODIFIED value to FUNC.
	* procfs.c (find_memory_regions_callback): Add the passed modified
	value.

gdb/testsuite/
2012-07-31  Jan Kratochvil  <jan.kratochvil@redhat.com>
	    Jean-Marc Saffroy  <saffroy@gmail.com>

	PR 11804
	* gdb.base/gcore-relro.exp: New file.
	* gdb.base/gcore-relro-main.c: New file.
	* gdb.base/gcore-relro-lib.c: New file.

diff --git a/gdb/defs.h b/gdb/defs.h
index 7be99a2..de34740 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -362,9 +362,14 @@ extern void init_source_path (void);
 
 /* From exec.c */
 
+/* Process memory area starting at ADDR with length SIZE.  Area is readable iff
+   READ is non-zero, writable if WRITE is non-zero, executable if EXEC is
+   non-zero.  Area is possibly changed against its original file based copy if
+   MODIFIED is non-zero.  DATA is passed without changes from a caller.  */
+
 typedef int (*find_memory_region_ftype) (CORE_ADDR addr, unsigned long size,
 					 int read, int write, int exec,
-					 void *data);
+					 int modified, void *data);
 
 /* Take over the 'find_mapped_memory' vector from exec.c.  */
 extern void exec_set_find_memory_regions
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index b3e4fab..4a8a509 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -131,8 +131,9 @@ fbsd_find_memory_regions (find_memory_region_ftype func, void *obfd)
 			    exec ? 'x' : '-');
 	}
 
-      /* Invoke the callback function to create the corefile segment.  */
-      func (start, size, read, write, exec, obfd);
+      /* Invoke the callback function to create the corefile segment.
+	 Pass MODIFIED as true, we do not know the real modification state.  */
+      func (start, size, read, write, exec, 1, obfd);
     }
 
   do_cleanups (cleanup);
diff --git a/gdb/gcore.c b/gdb/gcore.c
index a45e787..3c8e2f4 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -379,9 +379,12 @@ make_output_phdrs (bfd *obfd, asection *osec, void *ignored)
   bfd_record_phdr (obfd, p_type, 1, p_flags, 0, 0, 0, 0, 1, &osec);
 }
 
+/* find_memory_region_ftype implementation.  DATA is 'bfd *' for the core file
+   GDB is creating.  */
+
 static int
-gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
-		       int read, int write, int exec, void *data)
+gcore_create_callback (CORE_ADDR vaddr, unsigned long size, int read,
+		       int write, int exec, int modified, void *data)
 {
   bfd *obfd = data;
   asection *osec;
@@ -390,7 +393,7 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
   /* If the memory segment has no permissions set, ignore it, otherwise
      when we later try to access it for read/write, we'll get an error
      or jam the kernel.  */
-  if (read == 0 && write == 0 && exec == 0)
+  if (read == 0 && write == 0 && exec == 0 && modified == 0)
     {
       if (info_verbose)
         {
@@ -401,7 +404,7 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
       return 0;
     }
 
-  if (write == 0 && !solib_keep_data_in_core (vaddr, size))
+  if (write == 0 && modified == 0 && !solib_keep_data_in_core (vaddr, size))
     {
       /* See if this region of memory lies inside a known file on disk.
 	 If so, we can avoid copying its contents by clearing SEC_LOAD.  */
@@ -433,10 +436,12 @@ gcore_create_callback (CORE_ADDR vaddr, unsigned long size,
 	    }
 	}
 
-    keep:
-      flags |= SEC_READONLY;
+    keep:;
     }
 
+  if (write == 0)
+    flags |= SEC_READONLY;
+
   if (exec)
     flags |= SEC_CODE;
   else
@@ -477,6 +482,10 @@ objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
       asection *isec = objsec->the_bfd_section;
       flagword flags = bfd_get_section_flags (ibfd, isec);
 
+      /* Separate debug info files are irrelevant for gcore.  */
+      if (objfile->separate_debug_objfile_backlink != NULL)
+	continue;
+
       if ((flags & SEC_ALLOC) || (flags & SEC_LOAD))
 	{
 	  int size = bfd_section_size (ibfd, isec);
@@ -486,6 +495,7 @@ objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
 			 1, /* All sections will be readable.  */
 			 (flags & SEC_READONLY) == 0, /* Writable.  */
 			 (flags & SEC_CODE) != 0, /* Executable.  */
+			 1, /* MODIFIED is unknown, pass it as true.  */
 			 obfd);
 	  if (ret != 0)
 	    return ret;
@@ -498,6 +508,7 @@ objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
 	     1, /* Stack section will be readable.  */
 	     1, /* Stack section will be writable.  */
 	     0, /* Stack section will not be executable.  */
+	     1, /* Stack section will be modified.  */
 	     obfd);
 
   /* Make a heap segment.  */
@@ -506,6 +517,7 @@ objfile_find_memory_regions (find_memory_region_ftype func, void *obfd)
 	     1, /* Heap section will be readable.  */
 	     1, /* Heap section will be writable.  */
 	     0, /* Heap section will not be executable.  */
+	     1, /* Heap section will be modified.  */
 	     obfd);
 
   return 0;
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index 0c45f20..5a653cf 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2558,6 +2558,7 @@ gnu_find_memory_regions (find_memory_region_ftype func, void *data)
 		     last_protection & VM_PROT_READ,
 		     last_protection & VM_PROT_WRITE,
 		     last_protection & VM_PROT_EXECUTE,
+		     1, /* MODIFIED is unknown, pass it as true.  */
 		     data);
 	  last_region_address = region_address;
 	  last_region_end = region_address += region_length;
@@ -2571,6 +2572,7 @@ gnu_find_memory_regions (find_memory_region_ftype func, void *data)
 	     last_protection & VM_PROT_READ,
 	     last_protection & VM_PROT_WRITE,
 	     last_protection & VM_PROT_EXECUTE,
+	     1, /* MODIFIED is unknown, pass it as true.  */
 	     data);
 
   return 0;
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index b6f2efb..65f5f97 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -547,19 +547,28 @@ linux_find_memory_regions (struct gdbarch *gdbarch,
     return 1;
 
   xsnprintf (filename, sizeof filename,
-	     "/proc/%d/maps", current_inferior ()->pid);
+	     "/proc/%d/smaps", current_inferior ()->pid);
   data = target_fileio_read_stralloc (filename);
+  if (data == NULL)
+    {
+      /* Older Linux kernels did not support /proc/PID/smaps.  */
+      xsnprintf (filename, sizeof filename,
+		 "/proc/%d/maps", current_inferior ()->pid);
+      data = target_fileio_read_stralloc (filename);
+    }
   if (data)
     {
       struct cleanup *cleanup = make_cleanup (xfree, data);
       char *line;
 
-      for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
+      line = strtok (data, "\n");
+      while (line)
 	{
 	  ULONGEST addr, endaddr, offset, inode;
 	  const char *permissions, *device, *filename;
 	  size_t permissions_len, device_len;
 	  int read, write, exec;
+	  int modified = 0, has_anonymous = 0;
 
 	  read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
 			&offset, &device, &device_len, &inode, &filename);
@@ -569,8 +578,35 @@ linux_find_memory_regions (struct gdbarch *gdbarch,
 	  write = (memchr (permissions, 'w', permissions_len) != 0);
 	  exec = (memchr (permissions, 'x', permissions_len) != 0);
 
+	  /* Try to detect if region was modified by parsing smaps counters.  */
+	  for (line = strtok (NULL, "\n");
+	       line && line[0] >= 'A' && line[0] <= 'Z';
+	       line = strtok (NULL, "\n"))
+	    {
+	      char keyword[64 + 1];
+	      unsigned long number;
+
+	      if (sscanf (line, "%64s%lu kB\n", keyword, &number) != 2)
+		{
+		  warning (_("Error parsing {s,}maps file '%s'"), filename);
+		  break;
+		}
+	      if (strcmp (keyword, "Anonymous:") == 0)
+		has_anonymous = 1;
+	      if (number != 0 && (strcmp (keyword, "Shared_Dirty:") == 0
+				  || strcmp (keyword, "Private_Dirty:") == 0
+				  || strcmp (keyword, "Swap:") == 0
+				  || strcmp (keyword, "Anonymous:") == 0))
+		modified = 1;
+	    }
+
+	  /* Older Linux kernels did not support the "Anonymous:" counter.
+	     If it is missing, we can't be sure - dump all the pages.  */
+	  if (!has_anonymous)
+	    modified = 1;
+
 	  /* Invoke the callback function to create the corefile segment.  */
-	  func (addr, endaddr - addr, read, write, exec, obfd);
+	  func (addr, endaddr - addr, read, write, exec, modified, obfd);
 	}
 
       do_cleanups (cleanup);
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 4409e5b..ee2b123 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -5074,6 +5074,7 @@ find_memory_regions_callback (struct prmap *map,
 		  (map->pr_mflags & MA_READ) != 0,
 		  (map->pr_mflags & MA_WRITE) != 0,
 		  (map->pr_mflags & MA_EXEC) != 0,
+		  1, /* MODIFIED is unknown, pass it as true.  */
 		  data);
 }
 
diff --git a/gdb/testsuite/gdb.base/gcore-relro-lib.c b/gdb/testsuite/gdb.base/gcore-relro-lib.c
new file mode 100644
index 0000000..d74b690
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro-lib.c
@@ -0,0 +1,21 @@
+/* Copyright 2010 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 3 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, see <http://www.gnu.org/licenses/>.  */
+
+void
+lib (void)
+{
+}
diff --git a/gdb/testsuite/gdb.base/gcore-relro-main.c b/gdb/testsuite/gdb.base/gcore-relro-main.c
new file mode 100644
index 0000000..46b9dfe
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro-main.c
@@ -0,0 +1,25 @@
+/* Copyright 2010 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   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 3 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, see <http://www.gnu.org/licenses/>.  */
+
+extern void lib (void);
+
+int
+main (void)
+{
+  lib ();
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/gcore-relro.exp b/gdb/testsuite/gdb.base/gcore-relro.exp
new file mode 100644
index 0000000..5b8d6c7
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gcore-relro.exp
@@ -0,0 +1,78 @@
+# Copyright 2012 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 3 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, see <http://www.gnu.org/licenses/>.
+
+if {[skip_shlib_tests]} {
+    return 0
+}
+
+standard_testfile gcore-relro-main.c
+set libfile gcore-relro-lib
+set srcfile_lib ${libfile}.c
+set binfile_lib [standard_output_file ${libfile}.so]
+set gcorefile ${binfile}.gcore
+set objfile [standard_output_file ${testfile}.o]
+
+ if { [gdb_compile_shlib ${srcdir}/${subdir}/${srcfile_lib} ${binfile_lib} {debug}] != ""
+      || [gdb_compile ${srcdir}/${subdir}/${srcfile} ${objfile} object {debug}] != "" } {
+      untested ${testfile}.exp
+      return -1
+ }
+ set opts [list debug shlib=${binfile_lib} additional_flags=-Wl,-z,relro]
+ if { [gdb_compile ${objfile} ${binfile} executable $opts] != "" } {
+      unsupported "-Wl,-z,relro compilation failed"
+      return -1
+ }
+
+clean_restart ${binfile}
+gdb_load_shlibs ${binfile_lib}
+
+# Does this gdb support gcore?
+set test "help gcore"
+gdb_test_multiple $test $test {
+    -re "Undefined command: .gcore.*\r\n$gdb_prompt $" {
+	# gcore command not supported -- nothing to test here.
+	unsupported "gdb does not support gcore on this target"
+	return -1;
+    }
+    -re "Save a core file .*\r\n$gdb_prompt $" {
+	pass $test
+    }
+}
+
+if ![runto lib] {
+    return -1
+}
+
+set escapedfilename [string_to_regexp ${gcorefile}]
+
+set test "save a corefile"
+gdb_test_multiple "gcore ${gcorefile}" $test {
+    -re "Saved corefile ${escapedfilename}\r\n$gdb_prompt $" {
+	pass $test
+    }
+    -re "Can't create a corefile\r\n$gdb_prompt $" {
+	unsupported $test
+	return -1
+    }
+}
+
+# Now restart gdb and load the corefile.
+
+clean_restart ${binfile}
+gdb_load_shlibs ${binfile_lib}
+
+gdb_test "core ${gcorefile}" "Core was generated by .*" "re-load generated corefile"
+
+gdb_test "frame" "#0 \[^\r\n\]* lib .*" "library got loaded"


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

* Re: [PATCH] refreshed patch for PR 11804 and PR 9904
  2012-07-28  7:10 ` Jan Kratochvil
@ 2012-07-31 13:23   ` Jan Kratochvil
  0 siblings, 0 replies; 8+ messages in thread
From: Jan Kratochvil @ 2012-07-31 13:23 UTC (permalink / raw)
  To: Jean-Marc Saffroy; +Cc: gdb-patches

On Sat, 28 Jul 2012 09:10:23 +0200, Jan Kratochvil wrote:
> And this patch is large enough to require FSF GDB copyright assignment, you
> can get the snail-mail form from:
> 	Sergio Durigan Junior <sergiodj@redhat.com>

For this patch the copyright assignment is not needed, your modifications are
not large enough for it.  It depends what are your planned future
contributions.


Thanks,
Jan


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

* [commit] [PATCH] refreshed patch for PR 11804 and PR 9904
  2012-07-31 13:21     ` Jan Kratochvil
@ 2012-08-07 13:27       ` Jan Kratochvil
  0 siblings, 0 replies; 8+ messages in thread
From: Jan Kratochvil @ 2012-08-07 13:27 UTC (permalink / raw)
  To: Jean-Marc Saffroy; +Cc: gdb-patches

On Tue, 31 Jul 2012 15:21:19 +0200, Jan Kratochvil wrote:
> No regressions on {x86_64,x86_64-m32,i686}-fedorarawhide-linux-gnu and with
> gdbserver.  I will check it in in some time.  While it would be nice for 7.5
> I think it is too late for it.
[...]
> 2012-07-31  Jan Kratochvil  <jan.kratochvil@redhat.com>
> 	    Jean-Marc Saffroy  <saffroy@gmail.com>
> 
> 	PR 11804
> 	* defs.h (find_memory_region_ftype): New comment.  New arg modified.
> 	* fbsd-nat.c (fbsd_find_memory_regions): Add the passed modified value.
> 	* gcore.c (gcore_create_callback): New function comment.  Add modified
> 	parameter.  Only write modified regions.  Set SEC_READONLY exactly
> 	according to MODIFIED.
> 	(objfile_find_memory_regions): Ignore separate debug info files.  Ass
> 	the passed modified value to FUNC.
> 	* gnu-nat.c (gnu_find_memory_regions): Add the passed modified value.
> 	* linux-tdep.c (linux_find_memory_regions): Try to reads smaps file
> 	first.  New variables modified and has_anonymous.  Parse the lines of
> 	smaps file.  Add the passed MODIFIED value to FUNC.
> 	* procfs.c (find_memory_regions_callback): Add the passed modified
> 	value.
> 
> gdb/testsuite/
> 2012-07-31  Jan Kratochvil  <jan.kratochvil@redhat.com>
> 	    Jean-Marc Saffroy  <saffroy@gmail.com>
> 
> 	PR 11804
> 	* gdb.base/gcore-relro.exp: New file.
> 	* gdb.base/gcore-relro-main.c: New file.
> 	* gdb.base/gcore-relro-lib.c: New file.

After some discussions what is still OK for 7.5 and what should go only for
trunk checked it only for trunk.  7.6 should be released in half a year
anyway.

http://sourceware.org/ml/gdb-cvs/2012-08/msg00062.html


Jan


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

end of thread, other threads:[~2012-08-07 13:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-18  0:50 [PATCH] refreshed patch for PR 11804 and PR 9904 Jean-Marc Saffroy
2012-07-28  7:05 ` Jan Kratochvil
2012-07-29 17:11   ` Jean-Marc Saffroy
2012-07-29 17:35     ` Jean-Marc Saffroy
2012-07-31 13:21     ` Jan Kratochvil
2012-08-07 13:27       ` [commit] " Jan Kratochvil
2012-07-28  7:10 ` Jan Kratochvil
2012-07-31 13: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