public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v6 00/10] Validate binary before use
@ 2015-06-07 20:04 Jan Kratochvil
  2015-06-07 20:04 ` [PATCH v6 01/10] Move utility functions to common/ Jan Kratochvil
                   ` (9 more replies)
  0 siblings, 10 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

git://sourceware.org/git/archer.git
jankratochvil/gdbserverbuildid

an update.  Patch below is an overall v5->v6 diff of the whole series.

That gdb_regex* move is questionable.  linux_find_memory_regions_full() uses
regex now.  build-id reporting needs to read+parse /proc/PID/maps so it uses
linux_find_memory_regions_full().  But the regex is used only for detecing
anonymous memory which is used for the build-id reporting (to skip trying to
read build-id from anonymous mappings) but it is not too much important.  OTOH
I do not think it matters too much that a bit more code is moved to common/ .

Except for 2 (or 3 - testcase?) patches it was approved a year ago by Tom
Tromey modulo some trunk changes.


Jan


v6
 * move also gdb_regex* to common/ as discussed above, also in config*.ac
 * skip_to_space{,_const}() were moved to common/
 * common/common-defs.h #include reordering
 * new passing of enum filterflags from linux_qxfer_libraries_svr4()
 * dropped refactoring of code moved to common/ that avoided GDB exceptions
 * new svr4_copy_library_list() needs to handle new so_list->build_id

v5
 * svr4_validate() considers missing local build-id as not-a-match
 * target_so_ops->validate() now returns not-a-match reason as a string
 * rename common/common-target.[ch] -> common/target-utils.[ch]
 * testcase runs (but broken) even on different-filesystem remote target
 * testcase simplified by using with_test_prefix()

v4
 * NEWS, doc/gdb.texinfo additions.
 * Used host-defs.h.
 * New set/show solib-build-id-force.
 * testsuite: Do not run on non-localhost remote targets.

v3
	[patchv3 0/8] Validate binary before use
	https://sourceware.org/ml/gdb-patches/2014-02/msg00842.html
	Message-ID: <20140227213229.GA21121@host2.jankratochvil.net>
 * Implemented the review comments I made.
 * Removed fetching build-id in solib-svr4.c for NAT run.

v2
	[PATCH 0/8] v2 - validate binary before use
	https://sourceware.org/ml/gdb-patches/2013-04/msg00472.html
	Message-ID: <1366127096-5744-1-git-send-email-ARistovski@qnx.com>

---

 gdb/Makefile.in                                |   21 +
 gdb/NEWS                                       |   12 +
 gdb/cli/cli-utils.c                            |   36 --
 gdb/cli/cli-utils.h                            |   18 -
 gdb/common/common-defs.h                       |    2 
 gdb/common/common-utils.c                      |  137 ++++++
 gdb/common/common-utils.h                      |   20 +
 gdb/common/common.m4                           |   29 +
 gdb/common/gdb_regex.c                         |   73 +++
 gdb/common/gdb_regex.h                         |   36 ++
 gdb/common/host-defs.h                         |   21 +
 gdb/common/linux-maps.c                        |  546 +++++++++++++++++++++++
 gdb/common/linux-maps.h                        |   64 +++
 gdb/common/target-utils.c                      |  106 ++++
 gdb/common/target-utils.h                      |   33 +
 gdb/config/i386/linux.mh                       |    2 
 gdb/config/i386/linux64.mh                     |    2 
 gdb/configure                                  |  102 ++--
 gdb/configure.ac                               |   29 -
 gdb/defs.h                                     |   19 -
 gdb/doc/gdb.texinfo                            |   55 ++
 gdb/features/library-list-svr4.dtd             |   13 -
 gdb/gdb_regex.h                                |   36 --
 gdb/gdbserver/Makefile.in                      |   12 -
 gdb/gdbserver/config.in                        |    3 
 gdb/gdbserver/configure                        |   56 ++
 gdb/gdbserver/configure.srv                    |    2 
 gdb/gdbserver/gdbreplay.c                      |    6 
 gdb/gdbserver/linux-low.c                      |  398 +++++++++++++++--
 gdb/linux-tdep.c                               |  568 ++----------------------
 gdb/monitor.c                                  |   16 -
 gdb/solib-darwin.c                             |    1 
 gdb/solib-dsbt.c                               |    1 
 gdb/solib-frv.c                                |    1 
 gdb/solib-spu.c                                |    1 
 gdb/solib-svr4.c                               |   91 ++++
 gdb/solib-target.c                             |    2 
 gdb/solib.c                                    |   62 +++
 gdb/solib.h                                    |    4 
 gdb/solist.h                                   |   21 +
 gdb/target.c                                   |   94 +---
 gdb/testsuite/gdb.base/solib-mismatch-lib.c    |   29 +
 gdb/testsuite/gdb.base/solib-mismatch-libmod.c |   29 +
 gdb/testsuite/gdb.base/solib-mismatch.c        |   56 ++
 gdb/testsuite/gdb.base/solib-mismatch.exp      |  156 +++++++
 gdb/utils.c                                    |  154 -------
 gdb/utils.h                                    |    2 
 47 files changed, 2165 insertions(+), 1012 deletions(-)
 create mode 100644 gdb/common/gdb_regex.c
 create mode 100644 gdb/common/gdb_regex.h
 create mode 100644 gdb/common/linux-maps.c
 create mode 100644 gdb/common/linux-maps.h
 create mode 100644 gdb/common/target-utils.c
 create mode 100644 gdb/common/target-utils.h
 delete mode 100644 gdb/gdb_regex.h
 create mode 100644 gdb/testsuite/gdb.base/solib-mismatch-lib.c
 create mode 100644 gdb/testsuite/gdb.base/solib-mismatch-libmod.c
 create mode 100644 gdb/testsuite/gdb.base/solib-mismatch.c
 create mode 100644 gdb/testsuite/gdb.base/solib-mismatch.exp

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

* [PATCH v6 01/10] Move utility functions to common/
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
@ 2015-06-07 20:04 ` Jan Kratochvil
  2015-06-07 20:04 ` [PATCH v6 02/10] Merge multiple hex conversions Jan Kratochvil
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

some parts of the former patch have been reimplemented in the meantime by
other patches so this is only a part of the former cleanup.

Approved by:
	https://sourceware.org/ml/gdb-patches/2014-05/msg00363.html


Jan


gdb/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Move utility functions to common/.
	* cli/cli-utils.c (skip_spaces, skip_spaces_const, skip_to_space_const):
	Move defs to common/common-utils.c.
	* cli/cli-utils.h (skip_spaces, skip_spaces_const, skip_to_space)
	(skip_to_space_const): Move decls to common/common-utils.h.
	* common/common-defs.h: Move include of common-types.h before
	common-utils.h.
	* common/common-utils.c: Include host-defs.h and ctype.h.
	(HIGH_BYTE_POSN, is_digit_in_base, digit_to_int, strtoulst): Move
	from utils.c.
	(skip_spaces, skip_spaces_const, skip_to_space_const): Move from
	cli/cli-utils.c.
	* common/common-utils.h (strtoulst): Move decl from utils.h.
	(skip_spaces, skip_spaces_const, skip_to_space, skip_to_space_const):
	Move from cli/cli-utils.h.
	* common/host-defs.h: Include limits.h.
	(TARGET_CHAR_BIT, HOST_CHAR_BIT): Moved from defs.h.
	(skip_spaces, skip_spaces_const): Move decls from cli/cli-utils.h.
	* defs.h (TARGET_CHAR_BIT, HOST_CHAR_BIT): Move to
	common/common-utils.h.
	* utils.c (HIGH_BYTE_POSN, is_digit_in_base, digit_to_int)
	(strtoulst): Move to common/common-utils.c.
	* utils.h (strtoulst): Moved decl to common/common-utils.h.
---
 gdb/cli/cli-utils.c       |   36 ------------
 gdb/cli/cli-utils.h       |   18 ------
 gdb/common/common-defs.h  |    2 -
 gdb/common/common-utils.c |  137 +++++++++++++++++++++++++++++++++++++++++++++
 gdb/common/common-utils.h |   20 +++++++
 gdb/common/host-defs.h    |   21 +++++++
 gdb/defs.h                |   19 ------
 gdb/utils.c               |   99 ---------------------------------
 gdb/utils.h               |    2 -
 9 files changed, 179 insertions(+), 175 deletions(-)

diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c
index f3586b6..5c6338b 100644
--- a/gdb/cli/cli-utils.c
+++ b/gdb/cli/cli-utils.c
@@ -225,42 +225,6 @@ number_is_in_list (const char *list, int number)
 /* See documentation in cli-utils.h.  */
 
 char *
-skip_spaces (char *chp)
-{
-  if (chp == NULL)
-    return NULL;
-  while (*chp && isspace (*chp))
-    chp++;
-  return chp;
-}
-
-/* A const-correct version of the above.  */
-
-const char *
-skip_spaces_const (const char *chp)
-{
-  if (chp == NULL)
-    return NULL;
-  while (*chp && isspace (*chp))
-    chp++;
-  return chp;
-}
-
-/* See documentation in cli-utils.h.  */
-
-const char *
-skip_to_space_const (const char *chp)
-{
-  if (chp == NULL)
-    return NULL;
-  while (*chp && !isspace (*chp))
-    chp++;
-  return chp;
-}
-
-/* See documentation in cli-utils.h.  */
-
-char *
 remove_trailing_whitespace (const char *start, char *s)
 {
   while (s > start && isspace (*(s - 1)))
diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h
index 5e191ec..ad46581 100644
--- a/gdb/cli/cli-utils.h
+++ b/gdb/cli/cli-utils.h
@@ -93,24 +93,6 @@ extern int get_number_or_range (struct get_number_or_range_state *state);
 
 extern int number_is_in_list (const char *list, int number);
 
-/* Skip leading whitespace characters in INP, returning an updated
-   pointer.  If INP is NULL, return NULL.  */
-
-extern char *skip_spaces (char *inp);
-
-/* A const-correct version of the above.  */
-
-extern const char *skip_spaces_const (const char *inp);
-
-/* Skip leading non-whitespace characters in INP, returning an updated
-   pointer.  If INP is NULL, return NULL.  */
-
-#define skip_to_space(INP) ((char *) skip_to_space_const (INP))
-
-/* A const-correct version of the above.  */
-
-extern const char *skip_to_space_const (const char *inp);
-
 /* Reverse S to the last non-whitespace character without skipping past
    START.  */
 
diff --git a/gdb/common/common-defs.h b/gdb/common/common-defs.h
index 62d9de5..2be0d7d 100644
--- a/gdb/common/common-defs.h
+++ b/gdb/common/common-defs.h
@@ -41,10 +41,10 @@
 #include "gdb/signals.h"
 #include "gdb_locale.h"
 #include "ptid.h"
+#include "common-types.h"
 #include "common-utils.h"
 #include "gdb_assert.h"
 #include "errors.h"
-#include "common-types.h"
 #include "print-utils.h"
 #include "common-debug.h"
 #include "cleanups.h"
diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c
index 2925dd5..e86ed5b 100644
--- a/gdb/common/common-utils.c
+++ b/gdb/common/common-utils.c
@@ -18,6 +18,8 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "common-defs.h"
+#include "host-defs.h"
+#include <ctype.h>
 
 /* The xmalloc() (libiberty.h) family of memory management routines.
 
@@ -151,3 +153,138 @@ savestring (const char *ptr, size_t len)
   p[len] = 0;
   return p;
 }
+
+/* The bit offset of the highest byte in a ULONGEST, for overflow
+   checking.  */
+
+#define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
+
+/* True (non-zero) iff DIGIT is a valid digit in radix BASE,
+   where 2 <= BASE <= 36.  */
+
+static int
+is_digit_in_base (unsigned char digit, int base)
+{
+  if (!isalnum (digit))
+    return 0;
+  if (base <= 10)
+    return (isdigit (digit) && digit < base + '0');
+  else
+    return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
+}
+
+static int
+digit_to_int (unsigned char c)
+{
+  if (isdigit (c))
+    return c - '0';
+  else
+    return tolower (c) - 'a' + 10;
+}
+
+/* As for strtoul, but for ULONGEST results.  */
+
+ULONGEST
+strtoulst (const char *num, const char **trailer, int base)
+{
+  unsigned int high_part;
+  ULONGEST result;
+  int minus = 0;
+  int i = 0;
+
+  /* Skip leading whitespace.  */
+  while (isspace (num[i]))
+    i++;
+
+  /* Handle prefixes.  */
+  if (num[i] == '+')
+    i++;
+  else if (num[i] == '-')
+    {
+      minus = 1;
+      i++;
+    }
+
+  if (base == 0 || base == 16)
+    {
+      if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
+	{
+	  i += 2;
+	  if (base == 0)
+	    base = 16;
+	}
+    }
+
+  if (base == 0 && num[i] == '0')
+    base = 8;
+
+  if (base == 0)
+    base = 10;
+
+  if (base < 2 || base > 36)
+    {
+      errno = EINVAL;
+      return 0;
+    }
+
+  result = high_part = 0;
+  for (; is_digit_in_base (num[i], base); i += 1)
+    {
+      result = result * base + digit_to_int (num[i]);
+      high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
+      result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
+      if (high_part > 0xff)
+	{
+	  errno = ERANGE;
+	  result = ~ (ULONGEST) 0;
+	  high_part = 0;
+	  minus = 0;
+	  break;
+	}
+    }
+
+  if (trailer != NULL)
+    *trailer = &num[i];
+
+  result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
+  if (minus)
+    return -result;
+  else
+    return result;
+}
+
+/* See documentation in cli-utils.h.  */
+
+char *
+skip_spaces (char *chp)
+{
+  if (chp == NULL)
+    return NULL;
+  while (*chp && isspace (*chp))
+    chp++;
+  return chp;
+}
+
+/* A const-correct version of the above.  */
+
+const char *
+skip_spaces_const (const char *chp)
+{
+  if (chp == NULL)
+    return NULL;
+  while (*chp && isspace (*chp))
+    chp++;
+  return chp;
+}
+
+/* See documentation in cli-utils.h.  */
+
+const char *
+skip_to_space_const (const char *chp)
+{
+  if (chp == NULL)
+    return NULL;
+  while (*chp && !isspace (*chp))
+    chp++;
+  return chp;
+}
diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h
index cd2665a..eef5e6b 100644
--- a/gdb/common/common-utils.h
+++ b/gdb/common/common-utils.h
@@ -77,4 +77,24 @@ startswith (const char *string, const char *pattern)
   return strncmp (string, pattern, strlen (pattern)) == 0;
 }
 
+ULONGEST strtoulst (const char *num, const char **trailer, int base);
+
+/* Skip leading whitespace characters in INP, returning an updated
+   pointer.  If INP is NULL, return NULL.  */
+
+extern char *skip_spaces (char *inp);
+
+/* A const-correct version of the above.  */
+
+extern const char *skip_spaces_const (const char *inp);
+
+/* Skip leading non-whitespace characters in INP, returning an updated
+   pointer.  If INP is NULL, return NULL.  */
+
+#define skip_to_space(INP) ((char *) skip_to_space_const (INP))
+
+/* A const-correct version of the above.  */
+
+extern const char *skip_to_space_const (const char *inp);
+
 #endif
diff --git a/gdb/common/host-defs.h b/gdb/common/host-defs.h
index 6c67034..7bc41a2 100644
--- a/gdb/common/host-defs.h
+++ b/gdb/common/host-defs.h
@@ -19,6 +19,27 @@
 #ifndef HOST_DEFS_H
 #define HOST_DEFS_H
 
+#include <limits.h>
+
+/* Static host-system-dependent parameters for GDB.  */
+
+/* * Number of bits in a char or unsigned char for the target machine.
+   Just like CHAR_BIT in <limits.h> but describes the target machine.  */
+#if !defined (TARGET_CHAR_BIT)
+#define TARGET_CHAR_BIT 8
+#endif
+
+/* * If we picked up a copy of CHAR_BIT from a configuration file
+   (which may get it by including <limits.h>) then use it to set
+   the number of bits in a host char.  If not, use the same size
+   as the target.  */
+
+#if defined (CHAR_BIT)
+#define HOST_CHAR_BIT CHAR_BIT
+#else
+#define HOST_CHAR_BIT TARGET_CHAR_BIT
+#endif
+
 #ifdef __MSDOS__
 # define CANT_FORK
 # define GLOBAL_CURDIR
diff --git a/gdb/defs.h b/gdb/defs.h
index 44702ea..32b08bb 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -591,25 +591,6 @@ extern double atof (const char *);	/* X3.159-1989  4.10.1.1 */
 
 enum { MAX_REGISTER_SIZE = 64 };
 
-/* Static target-system-dependent parameters for GDB.  */
-
-/* * Number of bits in a char or unsigned char for the target machine.
-   Just like CHAR_BIT in <limits.h> but describes the target machine.  */
-#if !defined (TARGET_CHAR_BIT)
-#define TARGET_CHAR_BIT 8
-#endif
-
-/* * If we picked up a copy of CHAR_BIT from a configuration file
-   (which may get it by including <limits.h>) then use it to set
-   the number of bits in a host char.  If not, use the same size
-   as the target.  */
-
-#if defined (CHAR_BIT)
-#define HOST_CHAR_BIT CHAR_BIT
-#else
-#define HOST_CHAR_BIT TARGET_CHAR_BIT
-#endif
-
 /* In findvar.c.  */
 
 extern LONGEST extract_signed_integer (const gdb_byte *, int,
diff --git a/gdb/utils.c b/gdb/utils.c
index aaaf9c5..67ab1c5 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3004,105 +3004,6 @@ dummy_obstack_deallocate (void *object, void *data)
   return;
 }
 
-/* The bit offset of the highest byte in a ULONGEST, for overflow
-   checking.  */
-
-#define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
-
-/* True (non-zero) iff DIGIT is a valid digit in radix BASE,
-   where 2 <= BASE <= 36.  */
-
-static int
-is_digit_in_base (unsigned char digit, int base)
-{
-  if (!isalnum (digit))
-    return 0;
-  if (base <= 10)
-    return (isdigit (digit) && digit < base + '0');
-  else
-    return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
-}
-
-static int
-digit_to_int (unsigned char c)
-{
-  if (isdigit (c))
-    return c - '0';
-  else
-    return tolower (c) - 'a' + 10;
-}
-
-/* As for strtoul, but for ULONGEST results.  */
-
-ULONGEST
-strtoulst (const char *num, const char **trailer, int base)
-{
-  unsigned int high_part;
-  ULONGEST result;
-  int minus = 0;
-  int i = 0;
-
-  /* Skip leading whitespace.  */
-  while (isspace (num[i]))
-    i++;
-
-  /* Handle prefixes.  */
-  if (num[i] == '+')
-    i++;
-  else if (num[i] == '-')
-    {
-      minus = 1;
-      i++;
-    }
-
-  if (base == 0 || base == 16)
-    {
-      if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
-	{
-	  i += 2;
-	  if (base == 0)
-	    base = 16;
-	}
-    }
-
-  if (base == 0 && num[i] == '0')
-    base = 8;
-
-  if (base == 0)
-    base = 10;
-
-  if (base < 2 || base > 36)
-    {
-      errno = EINVAL;
-      return 0;
-    }
-
-  result = high_part = 0;
-  for (; is_digit_in_base (num[i], base); i += 1)
-    {
-      result = result * base + digit_to_int (num[i]);
-      high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
-      result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
-      if (high_part > 0xff)
-	{
-	  errno = ERANGE;
-	  result = ~ (ULONGEST) 0;
-	  high_part = 0;
-	  minus = 0;
-	  break;
-	}
-    }
-
-  if (trailer != NULL)
-    *trailer = &num[i];
-
-  result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
-  if (minus)
-    return -result;
-  else
-    return result;
-}
-
 /* Simple, portable version of dirname that does not modify its
    argument.  */
 
diff --git a/gdb/utils.h b/gdb/utils.h
index cae1ac0..4965553 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -37,8 +37,6 @@ extern int streq (const char *, const char *);
 
 extern int subset_compare (char *, char *);
 
-ULONGEST strtoulst (const char *num, const char **trailer, int base);
-
 int compare_positive_ints (const void *ap, const void *bp);
 int compare_strings (const void *ap, const void *bp);
 

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

* [PATCH v6 03/10] Code cleanup: Rename enum -> enum filterflags
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
  2015-06-07 20:04 ` [PATCH v6 01/10] Move utility functions to common/ Jan Kratochvil
  2015-06-07 20:04 ` [PATCH v6 02/10] Merge multiple hex conversions Jan Kratochvil
@ 2015-06-07 20:04 ` Jan Kratochvil
  2015-06-08  2:39   ` Sergio Durigan Junior
  2015-06-07 20:05 ` [PATCH v6 05/10] Move gdb_regex* to common/ Jan Kratochvil
                   ` (6 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi Sergio,

this is an unrelated cleanup, bit mask ints are better to make enums as GDB
already has support to automatically decode them:

before this patch:
	(gdb) p filterflags
	$1 = 51
	(gdb) p/x filterflags
	$2 = 0x33

after this patch:
	(gdb) p filterflags
	$1 = (COREFILTER_ANON_PRIVATE | COREFILTER_ANON_SHARED | COREFILTER_ELF_HEADERS | COREFILTER_HUGETLB_PRIVATE)

New patch, not yet reviewed before.


Jan


gdb/ChangeLog
2015-06-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* linux-tdep.c (enum filterflags): Make it from anonymous enum.
	(dump_mapping_p): Use it for parameter filterflags.
	(linux_find_memory_regions_full): Use it for variable filterflags.
---
 gdb/linux-tdep.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 8ce2f34..b0981f5 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -46,7 +46,7 @@
    Documentation/filesystems/proc.txt, inside the Linux kernel
    tree.  */
 
-enum
+enum filterflags
   {
     COREFILTER_ANON_PRIVATE = 1 << 0,
     COREFILTER_ANON_SHARED = 1 << 1,
@@ -598,7 +598,7 @@ mapping_is_anonymous_p (const char *filename)
      This should work OK enough, however.  */
 
 static int
-dump_mapping_p (unsigned int filterflags, const struct smaps_vmflags *v,
+dump_mapping_p (enum filterflags filterflags, const struct smaps_vmflags *v,
 		int maybe_private_p, int mapping_anon_p, int mapping_file_p,
 		const char *filename)
 {
@@ -1119,10 +1119,10 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
   /* Default dump behavior of coredump_filter (0x33), according to
      Documentation/filesystems/proc.txt from the Linux kernel
      tree.  */
-  unsigned int filterflags = (COREFILTER_ANON_PRIVATE
-			      | COREFILTER_ANON_SHARED
-			      | COREFILTER_ELF_HEADERS
-			      | COREFILTER_HUGETLB_PRIVATE);
+  enum filterflags filterflags = (COREFILTER_ANON_PRIVATE
+				  | COREFILTER_ANON_SHARED
+				  | COREFILTER_ELF_HEADERS
+				  | COREFILTER_HUGETLB_PRIVATE);
 
   /* We need to know the real target PID to access /proc.  */
   if (current_inferior ()->fake_pid_p)

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

* [PATCH v6 02/10] Merge multiple hex conversions
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
  2015-06-07 20:04 ` [PATCH v6 01/10] Move utility functions to common/ Jan Kratochvil
@ 2015-06-07 20:04 ` Jan Kratochvil
  2015-06-07 20:04 ` [PATCH v6 03/10] Code cleanup: Rename enum -> enum filterflags Jan Kratochvil
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

here most of the patch has been reimplemented in the meantime and this is only
a small remaint.

Approved by:
	https://sourceware.org/ml/gdb-patches/2014-05/msg00366.html


Jan


gdb/gdbserver/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Merge multiple hex conversions.
	* gdbreplay.c (tohex): Rename to 'fromhex'.
	(logchar): Use fromhex.

gdb/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Merge multiple hex conversions.
	* monitor.c: Include rsp-low.h.
	(fromhex): Remove definition.
---
 gdb/gdbserver/gdbreplay.c |    6 +++---
 gdb/monitor.c             |   16 +---------------
 2 files changed, 4 insertions(+), 18 deletions(-)

diff --git a/gdb/gdbserver/gdbreplay.c b/gdb/gdbserver/gdbreplay.c
index a02a824..79aa8aa 100644
--- a/gdb/gdbserver/gdbreplay.c
+++ b/gdb/gdbserver/gdbreplay.c
@@ -259,7 +259,7 @@ remote_open (char *name)
 }
 
 static int
-tohex (int ch)
+fromhex (int ch)
 {
   if (ch >= '0' && ch <= '9')
     {
@@ -322,11 +322,11 @@ logchar (FILE *fp)
 	  ch2 = fgetc (fp);
 	  fputc (ch2, stdout);
 	  fflush (stdout);
-	  ch = tohex (ch2) << 4;
+	  ch = fromhex (ch2) << 4;
 	  ch2 = fgetc (fp);
 	  fputc (ch2, stdout);
 	  fflush (stdout);
-	  ch |= tohex (ch2);
+	  ch |= fromhex (ch2);
 	  break;
 	default:
 	  /* Treat any other char as just itself */
diff --git a/gdb/monitor.c b/gdb/monitor.c
index 548dae3..c7f5fc7 100644
--- a/gdb/monitor.c
+++ b/gdb/monitor.c
@@ -54,6 +54,7 @@
 #include "regcache.h"
 #include "gdbthread.h"
 #include "readline/readline.h"
+#include "rsp-low.h"
 
 static char *dev_name;
 static struct target_ops *targ_ops;
@@ -225,21 +226,6 @@ monitor_error (char *function, char *message,
 	   message, safe_string);
 }
 
-/* Convert hex digit A to a number.  */
-
-static int
-fromhex (int a)
-{
-  if (a >= '0' && a <= '9')
-    return a - '0';
-  else if (a >= 'a' && a <= 'f')
-    return a - 'a' + 10;
-  else if (a >= 'A' && a <= 'F')
-    return a - 'A' + 10;
-  else
-    error (_("Invalid hex digit %d"), a);
-}
-
 /* monitor_vsprintf - similar to vsprintf but handles 64-bit addresses
 
    This function exists to get around the problem that many host platforms

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

* [PATCH v6 09/10] Validate symbol file using build-id
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
                   ` (7 preceding siblings ...)
  2015-06-07 20:05 ` [PATCH v6 08/10] gdbserver build-id attribute generator Jan Kratochvil
@ 2015-06-07 20:05 ` Jan Kratochvil
  2015-06-07 20:05 ` [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch] Jan Kratochvil
  9 siblings, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:05 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

consumer part of the "build-id" attribute.

Approved by:
	https://sourceware.org/ml/gdb-patches/2014-05/msg00424.html


Jan


gdb/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Validate symbol file using build-id.
	* NEWS (Changes since GDB 7.9): Add 'set solib-build-id-force'
	and 'show solib-build-id-force'.  Add build-id attribute.
	* solib-darwin.c (_initialize_darwin_solib): Assign validate value.
	* solib-dsbt.c (_initialize_dsbt_solib): Ditto.
	* solib-frv.c (_initialize_frv_solib): Ditto.
	* solib-spu.c (set_spu_solib_ops): Ditto.
	* solib-svr4.c: Include rsp-low.h.
	(NOTE_GNU_BUILD_ID_NAME): New define.
	(svr4_validate): New function.
	(svr4_copy_library_list): Duplicate field build_id.
	(library_list_start_library): Parse 'build-id' attribute.
	(svr4_library_attributes): Add 'build-id' attribute.
	(_initialize_svr4_solib): Assign validate value.
	* solib-target.c (solib.h): Include.
	(_initialize_solib_target): Assign validate value.
	* solib.c (solib_build_id_force, show_solib_build_id_force): New.
	(solib_map_sections): Use ops->validate.
	(clear_so): Free build_id.
	(default_solib_validate): New function.
	(_initialize_solib): Add "solib-build-id-force".
	* solib.h (default_solib_validate): New declaration.
	* solist.h (struct so_list): New fields 'build_idsz' and 'build_id'.
	(target_so_ops): New field 'validate'.

gdb/doc/ChangeLog
2014-03-02  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.texinfo (Files): Add 'set solib-build-id-force'
	and 'show solib-build-id-force'.
---
 gdb/NEWS            |   12 +++++++
 gdb/doc/gdb.texinfo |   38 +++++++++++++++++++++
 gdb/solib-darwin.c  |    1 +
 gdb/solib-dsbt.c    |    1 +
 gdb/solib-frv.c     |    1 +
 gdb/solib-spu.c     |    1 +
 gdb/solib-svr4.c    |   91 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/solib-target.c  |    2 +
 gdb/solib.c         |   62 ++++++++++++++++++++++++++++++++++-
 gdb/solib.h         |    4 ++
 gdb/solist.h        |   21 ++++++++++++
 11 files changed, 233 insertions(+), 1 deletion(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index bbfb55d..4942efa 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -133,6 +133,12 @@ set|show record btrace bts buffer-size
   The obtained size may differ from the requested size.  Use "info
   record" to see the obtained buffer size.
 
+set solib-build-id-force (on|off)
+show solib-build-id-force
+  Inferior shared library and symbol file may contain unique build-id.
+  If both build-ids are present but they do not match then this setting
+  enables (on) or disables (off) loading of such symbol file.
+
 * The command 'thread apply all' can now support new option '-ascending'
   to call its specified command for all threads in ascending order.
 
@@ -201,6 +207,12 @@ fork-events and vfork-events features in qSupported
 * GDB now supports access to vector registers on S/390 GNU/Linux
   targets.
 
+* New features in the GDB remote stub, GDBserver
+
+  ** library-list-svr4 contains also optional attribute 'build-id' for
+     each library.  GDB does not load library with build-id that
+     does not match such attribute.
+
 * Removed command line options
 
 -xdb  HP-UX XDB compatibility mode.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 2adf5f5..9ab40c9 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -17900,6 +17900,44 @@ libraries that were loaded by explicit user requests are not
 discarded.
 @end table
 
+@table @code
+@kindex set solib-build-id-force
+@cindex override @value{GDBN} build-id check
+@item set solib-build-id-force @var{mode}
+Setting to override @value{GDBN} build-id check.
+
+Inferior shared libraries and symbol files may contain unique build-id.
+By default @value{GDBN} will ignore symbol files with non-matching build-id
+while printing:
+
+@smallexample
+  warning: Shared object "libfoo.so.1" could not be validated (remote
+  build ID 2bc1745e does not match local build ID a08f8767) and will be
+  ignored; or use 'set solib-build-id-force'.
+@end smallexample
+
+Turning on this setting would load such symbol file while still printing:
+
+@smallexample
+  warning: Shared object "libfoo.so.1" could not be validated (remote
+  build ID 2bc1745e does not match local build ID a08f8767) but it is
+  being loaded due to 'set solib-build-id-force'.
+@end smallexample
+
+If remote build-id is present but it does not match local build-id (or local
+build-id is not present) then this setting enables (@var{mode} is @code{on}) or
+disables (@var{mode} is @code{off}) loading of such symbol file.  On systems
+where build-id is not present in the remote system this setting has no effect.
+The default value is @code{off}.
+
+Loading non-matching symbol file may confuse debugging including breakage
+of backtrace output.
+
+@kindex show solib-build-id-force
+@item show solib-build-id-force
+Display the current mode of build-id check override.
+@end table
+
 Sometimes you may wish that @value{GDBN} stops and gives you control
 when any of shared library events happen.  The best way to do this is
 to use @code{catch load} and @code{catch unload} (@pxref{Set
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index f96841f..9309c35 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -634,4 +634,5 @@ _initialize_darwin_solib (void)
   darwin_so_ops.in_dynsym_resolve_code = darwin_in_dynsym_resolve_code;
   darwin_so_ops.lookup_lib_global_symbol = darwin_lookup_lib_symbol;
   darwin_so_ops.bfd_open = darwin_bfd_open;
+  darwin_so_ops.validate = default_solib_validate;
 }
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 7da5833..9fe6533 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -1080,6 +1080,7 @@ _initialize_dsbt_solib (void)
   dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
   dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
   dsbt_so_ops.bfd_open = solib_bfd_open;
+  dsbt_so_ops.validate = default_solib_validate;
 
   /* Debug this file's internals.  */
   add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index f7ef38b..b768146 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -1183,6 +1183,7 @@ _initialize_frv_solib (void)
   frv_so_ops.open_symbol_file_object = open_symbol_file_object;
   frv_so_ops.in_dynsym_resolve_code = frv_in_dynsym_resolve_code;
   frv_so_ops.bfd_open = solib_bfd_open;
+  frv_so_ops.validate = default_solib_validate;
 
   /* Debug this file's internals.  */
   add_setshow_zuinteger_cmd ("solib-frv", class_maintenance,
diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c
index 44fbf91..d162884 100644
--- a/gdb/solib-spu.c
+++ b/gdb/solib-spu.c
@@ -519,6 +519,7 @@ set_spu_solib_ops (struct gdbarch *gdbarch)
       spu_so_ops.current_sos = spu_current_sos;
       spu_so_ops.bfd_open = spu_bfd_open;
       spu_so_ops.lookup_lib_global_symbol = spu_lookup_lib_symbol;
+      spu_so_ops.validate = default_solib_validate;
     }
 
   set_solib_ops (gdbarch, &spu_so_ops);
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 909dfb7..881b8f3 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -45,6 +45,9 @@
 #include "auxv.h"
 #include "gdb_bfd.h"
 #include "probe.h"
+#include "rsp-low.h"
+
+#define NOTE_GNU_BUILD_ID_NAME  ".note.gnu.build-id"
 
 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
 static int svr4_have_link_map_offsets (void);
@@ -970,6 +973,64 @@ svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
   return (name_lm >= vaddr && name_lm < vaddr + size);
 }
 
+/* Validate SO by comparing build-id from the associated bfd and
+   corresponding build-id from target memory.  */
+
+static char *
+svr4_validate (const struct so_list *const so)
+{
+  bfd_byte *local_id;
+  size_t local_idsz;
+
+  gdb_assert (so != NULL);
+
+  /* Target doesn't support reporting the build ID or the remote shared library
+     does not have build ID.  */
+  if (so->build_id == NULL)
+    return NULL;
+
+  /* Build ID may be present in the local file, just GDB is unable to retrieve
+     it.  As it has been reported by gdbserver it is not FSF gdbserver.  */
+  if (so->abfd == NULL
+      || !bfd_check_format (so->abfd, bfd_object)
+      || bfd_get_flavour (so->abfd) != bfd_target_elf_flavour)
+    return NULL;
+
+  /* GDB has verified the local file really does not contain the build ID.  */
+  if (elf_tdata (so->abfd)->build_id == NULL)
+    {
+      char *remote_hex;
+
+      remote_hex = alloca (so->build_idsz * 2 + 1);
+      bin2hex (so->build_id, remote_hex, so->build_idsz);
+
+      return xstrprintf (_("remote build ID is %s "
+			   "but local file does not have build ID"),
+			 remote_hex);
+    }
+
+  local_id = elf_tdata (so->abfd)->build_id->data;
+  local_idsz = elf_tdata (so->abfd)->build_id->size;
+
+  if (so->build_idsz != local_idsz
+      || memcmp (so->build_id, local_id, so->build_idsz) != 0)
+    {
+      char *remote_hex, *local_hex;
+
+      remote_hex = alloca (so->build_idsz * 2 + 1);
+      bin2hex (so->build_id, remote_hex, so->build_idsz);
+      local_hex = alloca (local_idsz * 2 + 1);
+      bin2hex (local_id, local_hex, local_idsz);
+
+      return xstrprintf (_("remote build ID %s "
+			   "does not match local build ID %s"),
+			 remote_hex, local_hex);
+    }
+
+  /* Both build IDs are present and they match.  */
+  return NULL;
+}
+
 /* Implement the "open_symbol_file_object" target_so_ops method.
 
    If no open symbol file, attempt to locate and open the main symbol
@@ -1108,6 +1169,12 @@ svr4_copy_library_list (struct so_list *src)
       newobj->lm_info = xmalloc (sizeof (struct lm_info));
       memcpy (newobj->lm_info, src->lm_info, sizeof (struct lm_info));
 
+      if (newobj->build_id != NULL)
+	{
+	  newobj->build_id = xmalloc (src->build_idsz);
+	  memcpy (newobj->build_id, src->build_id, src->build_idsz);
+	}
+
       newobj->next = NULL;
       *link = newobj;
       link = &newobj->next;
@@ -1135,6 +1202,9 @@ library_list_start_library (struct gdb_xml_parser *parser,
   ULONGEST *lmp = xml_find_attribute (attributes, "lm")->value;
   ULONGEST *l_addrp = xml_find_attribute (attributes, "l_addr")->value;
   ULONGEST *l_ldp = xml_find_attribute (attributes, "l_ld")->value;
+  const struct gdb_xml_value *const att_build_id
+    = xml_find_attribute (attributes, "build-id");
+  const char *const hex_build_id = att_build_id ? att_build_id->value : NULL;
   struct so_list *new_elem;
 
   new_elem = XCNEW (struct so_list);
@@ -1146,6 +1216,25 @@ library_list_start_library (struct gdb_xml_parser *parser,
   strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
   new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
   strcpy (new_elem->so_original_name, new_elem->so_name);
+  if (hex_build_id != NULL)
+    {
+      const size_t hex_build_id_len = strlen (hex_build_id);
+
+      if (hex_build_id_len > 0 && (hex_build_id_len & 1U) == 0)
+	{
+	  const size_t build_idsz = hex_build_id_len / 2;
+
+	  new_elem->build_id = xmalloc (build_idsz);
+	  new_elem->build_idsz = hex2bin (hex_build_id, new_elem->build_id,
+					  build_idsz);
+	  if (new_elem->build_idsz != build_idsz)
+	    {
+	      xfree (new_elem->build_id);
+	      new_elem->build_id = NULL;
+	      new_elem->build_idsz = 0;
+	    }
+	}
+    }
 
   *list->tailp = new_elem;
   list->tailp = &new_elem->next;
@@ -1180,6 +1269,7 @@ static const struct gdb_xml_attribute svr4_library_attributes[] =
   { "lm", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
   { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
   { "l_ld", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
+  { "build-id", GDB_XML_AF_OPTIONAL, NULL, NULL },
   { NULL, GDB_XML_AF_NONE, NULL, NULL }
 };
 
@@ -3258,4 +3348,5 @@ _initialize_svr4_solib (void)
   svr4_so_ops.keep_data_in_core = svr4_keep_data_in_core;
   svr4_so_ops.update_breakpoints = svr4_update_solib_event_breakpoints;
   svr4_so_ops.handle_event = svr4_handle_solib_event;
+  svr4_so_ops.validate = svr4_validate;
 }
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index bd8f58a..689bc74 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -25,6 +25,7 @@
 #include "target.h"
 #include "vec.h"
 #include "solib-target.h"
+#include "solib.h"
 
 /* Private data for each loaded library.  */
 struct lm_info
@@ -500,6 +501,7 @@ _initialize_solib_target (void)
   solib_target_so_ops.in_dynsym_resolve_code
     = solib_target_in_dynsym_resolve_code;
   solib_target_so_ops.bfd_open = solib_bfd_open;
+  solib_target_so_ops.validate = default_solib_validate;
 
   /* Set current_target_so_ops to solib_target_so_ops if not already
      set.  */
diff --git a/gdb/solib.c b/gdb/solib.c
index 0010c2f..4bd47d0 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -523,6 +523,20 @@ solib_bfd_open (char *pathname)
   return abfd;
 }
 
+/* Boolean for command 'set solib-build-id-force'.  */
+static int solib_build_id_force = 0;
+
+/* Implement 'show solib-build-id-force'.  */
+
+static void
+show_solib_build_id_force (struct ui_file *file, int from_tty,
+			   struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Loading of shared libraries "
+			    "with non-matching build-id is %s.\n"),
+		    value);
+}
+
 /* Given a pointer to one of the shared objects in our list of mapped
    objects, use the recorded name to open a bfd descriptor for the
    object, build a section table, relocate all the section addresses
@@ -539,7 +553,7 @@ static int
 solib_map_sections (struct so_list *so)
 {
   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
-  char *filename;
+  char *filename, *validate_error;
   struct target_section *p;
   struct cleanup *old_chain;
   bfd *abfd;
@@ -555,6 +569,27 @@ solib_map_sections (struct so_list *so)
   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
   so->abfd = abfd;
 
+  gdb_assert (ops->validate != NULL);
+
+  validate_error = ops->validate (so);
+  if (validate_error != NULL)
+    {
+      if (!solib_build_id_force)
+	{
+	  warning (_("Shared object \"%s\" could not be validated (%s) and "
+	             "will be ignored; or use 'set solib-build-id-force'."),
+		   so->so_name, validate_error);
+	  xfree (validate_error);
+	  gdb_bfd_unref (so->abfd);
+	  so->abfd = NULL;
+	  return 0;
+	}
+      warning (_("Shared object \"%s\" could not be validated (%s) "
+		 "but it is being loaded due to 'set solib-build-id-force'."),
+	       so->so_name, validate_error);
+      xfree (validate_error);
+    }
+
   /* Copy the full path name into so_name, allowing symbol_file_add
      to find it later.  This also affects the =library-loaded GDB/MI
      event, and in particular the part of that notification providing
@@ -631,6 +666,9 @@ clear_so (struct so_list *so)
      of the symbol file.  */
   strcpy (so->so_name, so->so_original_name);
 
+  xfree (so->build_id);
+  so->build_id = NULL;
+
   /* Do the same for target-specific data.  */
   if (ops->clear_so != NULL)
     ops->clear_so (so);
@@ -1662,6 +1700,14 @@ remove_user_added_objfile (struct objfile *objfile)
     }
 }
 
+/* Default implementation does not perform any validation.  */
+
+char *
+default_solib_validate (const struct so_list *const so)
+{
+  return NULL; /* No validation.  */
+}
+
 extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
 
 void
@@ -1719,4 +1765,18 @@ PATH and LD_LIBRARY_PATH."),
 				     reload_shared_libraries,
 				     show_solib_search_path,
 				     &setlist, &showlist);
+
+  add_setshow_boolean_cmd ("solib-build-id-force", class_support,
+			   &solib_build_id_force, _("\
+Set loading of shared libraries with non-matching build-id."), _("\
+Show loading of shared libraries with non-matching build-id."), _("\
+Inferior shared library and symbol file may contain unique build-id.\n\
+If both build-ids are present but they do not match then this setting\n\
+enables (on) or disables (off) loading of such symbol file.\n\
+Loading non-matching symbol file may confuse debugging including breakage\n\
+of backtrace output."),
+			   NULL,
+			   show_solib_build_id_force,
+			   &setlist, &showlist);
+
 }
diff --git a/gdb/solib.h b/gdb/solib.h
index 336971d..c3bf529 100644
--- a/gdb/solib.h
+++ b/gdb/solib.h
@@ -98,4 +98,8 @@ extern void update_solib_breakpoints (void);
 
 extern void handle_solib_event (void);
 
+/* Default validation always returns 1.  */
+
+extern char *default_solib_validate (const struct so_list *so);
+
 #endif /* SOLIB_H */
diff --git a/gdb/solist.h b/gdb/solist.h
index 7021f5c..f0d8653 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -75,6 +75,22 @@ struct so_list
        There may not be just one (e.g. if two segments are relocated
        differently); but this is only used for "info sharedlibrary".  */
     CORE_ADDR addr_low, addr_high;
+
+    /* Build id in raw format, contains verbatim contents of
+       .note.gnu.build-id including note header.  This is actual
+       BUILD_ID which comes either from the remote target via qXfer
+       packet or via reading target memory.  Therefore, it may differ
+       from the build-id of the associated bfd.  In a normal
+       scenario, this so would soon lose its abfd due to failed
+       validation.
+       Reading target memory should be done by following execution view
+       of the binary (following program headers in the case of ELF).
+       Computing address from the linking view (following ELF section
+       headers) may give incorrect build-id memory address despite the
+       symbols still match.
+       Such an example is a prelinked vs.  unprelinked i386 ELF file.  */
+    size_t build_idsz;
+    gdb_byte *build_id;
   };
 
 struct target_so_ops
@@ -168,6 +184,11 @@ struct target_so_ops
        NULL, in which case no specific preprocessing is necessary
        for this target.  */
     void (*handle_event) (void);
+
+    /* Return NULL if SO does match target SO it is supposed to
+       represent.  Otherwise return string describing why it does not match.
+       Caller has to free the string.  */
+    char *(*validate) (const struct so_list *so);
   };
 
 /* Free the memory associated with a (so_list *).  */

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

* [PATCH v6 05/10] Move gdb_regex* to common/
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
                   ` (2 preceding siblings ...)
  2015-06-07 20:04 ` [PATCH v6 03/10] Code cleanup: Rename enum -> enum filterflags Jan Kratochvil
@ 2015-06-07 20:05 ` Jan Kratochvil
  2015-06-07 20:05 ` [PATCH v6 06/10] Prepare linux_find_memory_regions_full & co. for move Jan Kratochvil
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:05 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

later patches need regex support also in gdbserver.

New patch, not yet reviewed before.


Jan


gdb/ChangeLog
2015-06-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* Makefile.in (HFILES_NO_SRCDIR): Change gdb_regex.h to
	common/gdb_regex.h.
	(COMMON_OBS): Add gdb_regex.o.
	(gdb_regex.o): New.
	* common/common.m4 (GDB_AC_COMMON): Add gdb_use_included_regex,
	--without-included-regex and USE_INCLUDED_REGEX.
	* common/gdb_regex.c: New file from utils.c functions.
	* common/gdb_regex.h: Move it here from gdb_regex.h, update include
	file wrapping define name.
	* configure: Rebuilt.
	* configure.ac (gdb_use_included_regex, --without-included-regex)
	(USE_INCLUDED_REGEX): Move them to common/common.m4.
	* gdb_regex.h: Move it to common/gdb_regex.h.
	* utils.c: Remove include gdb_regex.h.
	(do_regfree_cleanup, make_regfree_cleanup, get_regcomp_error)
	(compile_rx_or_error): Move them to common/gdb_regex.c.

gdb/gdbserver/ChangeLog
2015-06-07  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* Makefile.in (OBS): Add gdb_regex.o.
	(gdb_regex.o): New.
	* config.in: Rebuilt.
	* configure: Rebuilt.
---
 gdb/Makefile.in           |    7 +++
 gdb/common/common.m4      |   29 +++++++++++++
 gdb/common/gdb_regex.c    |   73 ++++++++++++++++++++++++++++++++
 gdb/common/gdb_regex.h    |   36 ++++++++++++++++
 gdb/configure             |  102 +++++++++++++++++++++++----------------------
 gdb/configure.ac          |   29 -------------
 gdb/gdb_regex.h           |   36 ----------------
 gdb/gdbserver/Makefile.in |    4 ++
 gdb/gdbserver/config.in   |    3 +
 gdb/gdbserver/configure   |   56 +++++++++++++++++++++++++
 gdb/utils.c               |   55 ------------------------
 11 files changed, 258 insertions(+), 172 deletions(-)
 create mode 100644 gdb/common/gdb_regex.c
 create mode 100644 gdb/common/gdb_regex.h
 delete mode 100644 gdb/gdb_regex.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 3b1d1d8..a2794ab 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -934,7 +934,7 @@ extension.h extension-priv.h \
 build-id.h buildsym.h valprint.h \
 typeprint.h mi/mi-getopt.h mi/mi-parse.h mi/mi-console.h \
 mi/mi-out.h mi/mi-main.h mi/mi-common.h mi/mi-cmds.h linux-nat.h \
-complaints.h gdb_proc_service.h gdb_regex.h xtensa-tdep.h inf-loop.h \
+complaints.h gdb_proc_service.h common/gdb_regex.h xtensa-tdep.h inf-loop.h \
 common/gdb_wait.h common/gdb_assert.h solib.h ppc-tdep.h cp-support.h glibc-tdep.h \
 interps.h auxv.h gdbcmd.h tramp-frame.h mipsnbsd-tdep.h	\
 amd64-linux-tdep.h linespec.h i387-tdep.h mn10300-tdep.h \
@@ -1080,6 +1080,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	format.o registry.o btrace.o record-btrace.o waitstatus.o \
 	print-utils.o rsp-low.o errors.o common-debug.o debug.o \
 	common-exceptions.o btrace-common.o fileio.o target-utils.o \
+	gdb_regex.o \
 	$(SUBDIR_GCC_COMPILE_OBS)
 
 TSOBS = inflow.o
@@ -2268,6 +2269,10 @@ btrace-common.o: ${srcdir}/common/btrace-common.c
 fileio.o: ${srcdir}/common/fileio.c
 	$(COMPILE) $(srcdir)/common/fileio.c
 	$(POSTCOMPILE)
+
+gdb_regex.o: ${srcdir}/common/gdb_regex.c
+	$(COMPILE) $(srcdir)/common/gdb_regex.c
+	$(POSTCOMPILE)
 #
 # gdb/target/ dependencies
 #
diff --git a/gdb/common/common.m4 b/gdb/common/common.m4
index a87579a..e48dd5a 100644
--- a/gdb/common/common.m4
+++ b/gdb/common/common.m4
@@ -44,4 +44,33 @@ gdb_cv_func_sigsetjmp=yes, gdb_cv_func_sigsetjmp=no)])
 if test $gdb_cv_func_sigsetjmp = yes; then
   AC_DEFINE(HAVE_SIGSETJMP, 1, [Define if sigsetjmp is available. ])
 fi
+
+  # Assume we'll default to using the included libiberty regex.
+  gdb_use_included_regex=yes
+
+  # However, if the system regex is GNU regex, then default to *not*
+  # using the included regex.
+  AC_CACHE_CHECK(
+    [for GNU regex],
+    [gdb_cv_have_gnu_regex],
+    [AC_TRY_COMPILE(
+      [#include <gnu-versions.h>],
+      [#define REGEX_INTERFACE_VERSION 1
+  #if _GNU_REGEX_INTERFACE_VERSION != REGEX_INTERFACE_VERSION
+  # error "Version mismatch"
+  #endif],
+      gdb_cv_have_gnu_regex=yes,
+      gdb_cv_have_gnu_regex=no)])
+  if test $gdb_cv_have_gnu_regex = yes; then
+    gdb_use_included_regex=no
+  fi
+
+  AC_ARG_WITH(included-regex,
+    AS_HELP_STRING([--without-included-regex], [don't use included regex; this is the default on systems with version 2 of the GNU C library (use with caution on other system)]),
+    gdb_with_regex=$withval,
+    gdb_with_regex=$gdb_use_included_regex)
+  if test "$gdb_with_regex" = yes; then
+    AC_DEFINE(USE_INCLUDED_REGEX, 1,
+      [Define to 1 if the regex included in libiberty should be used.])
+  fi
 ])
diff --git a/gdb/common/gdb_regex.c b/gdb/common/gdb_regex.c
new file mode 100644
index 0000000..73f30a7
--- /dev/null
+++ b/gdb/common/gdb_regex.c
@@ -0,0 +1,73 @@
+/* Shared utility routines for GDB to interact with agent.
+
+   Copyright (C) 2009-2015 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/>.  */
+
+#include "common-defs.h"
+#include "gdb_regex.h"
+
+/* A cleanup function that calls regfree.  */
+
+static void
+do_regfree_cleanup (void *r)
+{
+  regfree (r);
+}
+
+/* Create a new cleanup that frees the compiled regular expression R.  */
+
+struct cleanup *
+make_regfree_cleanup (regex_t *r)
+{
+  return make_cleanup (do_regfree_cleanup, r);
+}
+
+/* Return an xmalloc'd error message resulting from a regular
+   expression compilation failure.  */
+
+char *
+get_regcomp_error (int code, regex_t *rx)
+{
+  size_t length = regerror (code, rx, NULL, 0);
+  char *result = xmalloc (length);
+
+  regerror (code, rx, result, length);
+  return result;
+}
+
+/* Compile a regexp and throw an exception on error.  This returns a
+   cleanup to free the resulting pattern on success.  RX must not be
+   NULL.  */
+
+struct cleanup *
+compile_rx_or_error (regex_t *pattern, const char *rx, const char *message)
+{
+  int code;
+
+  gdb_assert (rx != NULL);
+
+  code = regcomp (pattern, rx, REG_NOSUB);
+  if (code != 0)
+    {
+      char *err = get_regcomp_error (code, pattern);
+
+      make_cleanup (xfree, err);
+      error (("%s: %s"), message, err);
+    }
+
+  return make_regfree_cleanup (pattern);
+}
diff --git a/gdb/common/gdb_regex.h b/gdb/common/gdb_regex.h
new file mode 100644
index 0000000..85c9c72
--- /dev/null
+++ b/gdb/common/gdb_regex.h
@@ -0,0 +1,36 @@
+/* Portable <regex.h>.
+   Copyright (C) 2000-2015 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/>.  */
+
+#ifndef COMMON_GDB_REGEX_H
+#define COMMON_GDB_REGEX_H 1
+
+#ifdef USE_INCLUDED_REGEX
+# include "xregex.h"
+#else
+/* Request 4.2 BSD regex functions.  */
+# define _REGEX_RE_COMP
+# include <regex.h>
+#endif
+
+/* From utils.c.  */
+struct cleanup *make_regfree_cleanup (regex_t *);
+char *get_regcomp_error (int, regex_t *);
+struct cleanup *compile_rx_or_error (regex_t *pattern, const char *rx,
+				     const char *message);
+
+#endif /* not COMMON_GDB_REGEX_H */
diff --git a/gdb/configure b/gdb/configure
index 2baf6dd..5bd35c0 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -11427,6 +11427,57 @@ $as_echo "#define HAVE_SIGSETJMP 1" >>confdefs.h
 
 fi
 
+  # Assume we'll default to using the included libiberty regex.
+  gdb_use_included_regex=yes
+
+  # However, if the system regex is GNU regex, then default to *not*
+  # using the included regex.
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for GNU regex" >&5
+$as_echo_n "checking for GNU regex... " >&6; }
+if test "${gdb_cv_have_gnu_regex+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <gnu-versions.h>
+int
+main ()
+{
+#define REGEX_INTERFACE_VERSION 1
+  #if _GNU_REGEX_INTERFACE_VERSION != REGEX_INTERFACE_VERSION
+  # error "Version mismatch"
+  #endif
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  gdb_cv_have_gnu_regex=yes
+else
+  gdb_cv_have_gnu_regex=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_have_gnu_regex" >&5
+$as_echo "$gdb_cv_have_gnu_regex" >&6; }
+  if test $gdb_cv_have_gnu_regex = yes; then
+    gdb_use_included_regex=no
+  fi
+
+
+# Check whether --with-included-regex was given.
+if test "${with_included_regex+set}" = set; then :
+  withval=$with_included_regex; gdb_with_regex=$withval
+else
+  gdb_with_regex=$gdb_use_included_regex
+fi
+
+  if test "$gdb_with_regex" = yes; then
+
+$as_echo "#define USE_INCLUDED_REGEX 1" >>confdefs.h
+
+  fi
+
 
 # Check the return and argument types of ptrace.  No canned test for
 # this, so roll our own.
@@ -11689,57 +11740,6 @@ if test $ac_cv_func_setpgrp_void = yes; then
 fi
 fi
 
-# Assume we'll default to using the included libiberty regex.
-gdb_use_included_regex=yes
-
-# However, if the system regex is GNU regex, then default to *not*
-# using the included regex.
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for GNU regex" >&5
-$as_echo_n "checking for GNU regex... " >&6; }
-if test "${gdb_cv_have_gnu_regex+set}" = set; then :
-  $as_echo_n "(cached) " >&6
-else
-  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-#include <gnu-versions.h>
-int
-main ()
-{
-#define REGEX_INTERFACE_VERSION 1
-#if _GNU_REGEX_INTERFACE_VERSION != REGEX_INTERFACE_VERSION
-# error "Version mismatch"
-#endif
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
-  gdb_cv_have_gnu_regex=yes
-else
-  gdb_cv_have_gnu_regex=no
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_have_gnu_regex" >&5
-$as_echo "$gdb_cv_have_gnu_regex" >&6; }
-if test $gdb_cv_have_gnu_regex = yes; then
-  gdb_use_included_regex=no
-fi
-
-
-# Check whether --with-included-regex was given.
-if test "${with_included_regex+set}" = set; then :
-  withval=$with_included_regex; gdb_with_regex=$withval
-else
-  gdb_with_regex=$gdb_use_included_regex
-fi
-
-if test "$gdb_with_regex" = yes; then
-
-$as_echo "#define USE_INCLUDED_REGEX 1" >>confdefs.h
-
-fi
-
 # Check if <sys/proc.h> defines `struct thread' with a td_pcb member.
 ac_fn_c_check_member "$LINENO" "struct thread" "td_pcb" "ac_cv_member_struct_thread_td_pcb" "#include <sys/param.h>
 #include <sys/proc.h>
diff --git a/gdb/configure.ac b/gdb/configure.ac
index c703e35..15c13e5 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1424,35 +1424,6 @@ if test $ac_cv_func_setpgrp_void = yes; then
 fi
 fi
 
-# Assume we'll default to using the included libiberty regex.
-gdb_use_included_regex=yes
-
-# However, if the system regex is GNU regex, then default to *not*
-# using the included regex.
-AC_CACHE_CHECK(
-  [for GNU regex],
-  [gdb_cv_have_gnu_regex],
-  [AC_TRY_COMPILE(
-    [#include <gnu-versions.h>],
-    [#define REGEX_INTERFACE_VERSION 1
-#if _GNU_REGEX_INTERFACE_VERSION != REGEX_INTERFACE_VERSION
-# error "Version mismatch"
-#endif],
-    gdb_cv_have_gnu_regex=yes,
-    gdb_cv_have_gnu_regex=no)])
-if test $gdb_cv_have_gnu_regex = yes; then
-  gdb_use_included_regex=no
-fi
-
-AC_ARG_WITH(included-regex,
-  AS_HELP_STRING([--without-included-regex], [don't use included regex; this is the default on systems with version 2 of the GNU C library (use with caution on other system)]),
-  gdb_with_regex=$withval,
-  gdb_with_regex=$gdb_use_included_regex)
-if test "$gdb_with_regex" = yes; then
-  AC_DEFINE(USE_INCLUDED_REGEX, 1,
-    [Define to 1 if the regex included in libiberty should be used.])
-fi
-
 # Check if <sys/proc.h> defines `struct thread' with a td_pcb member.
 AC_CHECK_MEMBERS([struct thread.td_pcb], [], [],
 [#include <sys/param.h>
diff --git a/gdb/gdb_regex.h b/gdb/gdb_regex.h
deleted file mode 100644
index 3173a54..0000000
--- a/gdb/gdb_regex.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/* Portable <regex.h>.
-   Copyright (C) 2000-2015 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/>.  */
-
-#ifndef GDB_REGEX_H
-#define GDB_REGEX_H 1
-
-#ifdef USE_INCLUDED_REGEX
-# include "xregex.h"
-#else
-/* Request 4.2 BSD regex functions.  */
-# define _REGEX_RE_COMP
-# include <regex.h>
-#endif
-
-/* From utils.c.  */
-struct cleanup *make_regfree_cleanup (regex_t *);
-char *get_regcomp_error (int, regex_t *);
-struct cleanup *compile_rx_or_error (regex_t *pattern, const char *rx,
-				     const char *message);
-
-#endif /* not GDB_REGEX_H */
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index b4465e6..993013b 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -195,6 +195,7 @@ OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o \
       common-utils.o ptid.o buffer.o format.o filestuff.o dll.o notif.o \
       tdesc.o print-utils.o rsp-low.o errors.o common-debug.o cleanups.o \
       common-exceptions.o symbol.o btrace-common.o fileio.o target-utils.o \
+      gdb_regex.o \
       $(XML_BUILTIN) $(DEPFILES) $(LIBOBJS)
 GDBREPLAY_OBS = gdbreplay.o version.o
 GDBSERVER_LIBS = @GDBSERVER_LIBS@
@@ -582,6 +583,9 @@ waitstatus.o: ../target/waitstatus.c
 fileio.o: ../common/fileio.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+gdb_regex.o: ../common/gdb_regex.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 
 # Native object files rules from ../nat
 
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index 9359ce5..9497495 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -314,6 +314,9 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Define to 1 if the regex included in libiberty should be used. */
+#undef USE_INCLUDED_REGEX
+
 /* Define if we should use libthread_db directly. */
 #undef USE_LIBTHREAD_DB_DIRECTLY
 
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index b5a2f85..3b625a6 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -694,6 +694,7 @@ enable_maintainer_mode
 enable_largefile
 enable_build_with_cxx
 enable_libmcheck
+with_included_regex
 with_ust
 with_ust_include
 with_ust_lib
@@ -1345,6 +1346,10 @@ Optional Features:
 Optional Packages:
   --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
   --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
+  --without-included-regex
+                          don't use included regex; this is the default on
+                          systems with version 2 of the GNU C library (use
+                          with caution on other system)
   --with-ust=PATH       Specify prefix directory for the installed UST package
                           Equivalent to --with-ust-include=PATH/include
                           plus --with-ust-lib=PATH/lib
@@ -5747,6 +5752,57 @@ $as_echo "#define HAVE_SIGSETJMP 1" >>confdefs.h
 
 fi
 
+  # Assume we'll default to using the included libiberty regex.
+  gdb_use_included_regex=yes
+
+  # However, if the system regex is GNU regex, then default to *not*
+  # using the included regex.
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for GNU regex" >&5
+$as_echo_n "checking for GNU regex... " >&6; }
+if test "${gdb_cv_have_gnu_regex+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <gnu-versions.h>
+int
+main ()
+{
+#define REGEX_INTERFACE_VERSION 1
+  #if _GNU_REGEX_INTERFACE_VERSION != REGEX_INTERFACE_VERSION
+  # error "Version mismatch"
+  #endif
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  gdb_cv_have_gnu_regex=yes
+else
+  gdb_cv_have_gnu_regex=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_have_gnu_regex" >&5
+$as_echo "$gdb_cv_have_gnu_regex" >&6; }
+  if test $gdb_cv_have_gnu_regex = yes; then
+    gdb_use_included_regex=no
+  fi
+
+
+# Check whether --with-included-regex was given.
+if test "${with_included_regex+set}" = set; then :
+  withval=$with_included_regex; gdb_with_regex=$withval
+else
+  gdb_with_regex=$gdb_use_included_regex
+fi
+
+  if test "$gdb_with_regex" = yes; then
+
+$as_echo "#define USE_INCLUDED_REGEX 1" >>confdefs.h
+
+  fi
+
 
 # Check for UST
 ustlibs=""
diff --git a/gdb/utils.c b/gdb/utils.c
index 67ab1c5..256708a 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -67,7 +67,6 @@
 
 #include "gdb_usleep.h"
 #include "interps.h"
-#include "gdb_regex.h"
 
 #if !HAVE_DECL_MALLOC
 extern PTR malloc ();		/* ARI: PTR */
@@ -1131,60 +1130,6 @@ make_hex_string (const gdb_byte *data, size_t length)
 
 \f
 
-/* A cleanup function that calls regfree.  */
-
-static void
-do_regfree_cleanup (void *r)
-{
-  regfree (r);
-}
-
-/* Create a new cleanup that frees the compiled regular expression R.  */
-
-struct cleanup *
-make_regfree_cleanup (regex_t *r)
-{
-  return make_cleanup (do_regfree_cleanup, r);
-}
-
-/* Return an xmalloc'd error message resulting from a regular
-   expression compilation failure.  */
-
-char *
-get_regcomp_error (int code, regex_t *rx)
-{
-  size_t length = regerror (code, rx, NULL, 0);
-  char *result = xmalloc (length);
-
-  regerror (code, rx, result, length);
-  return result;
-}
-
-/* Compile a regexp and throw an exception on error.  This returns a
-   cleanup to free the resulting pattern on success.  RX must not be
-   NULL.  */
-
-struct cleanup *
-compile_rx_or_error (regex_t *pattern, const char *rx, const char *message)
-{
-  int code;
-
-  gdb_assert (rx != NULL);
-
-  code = regcomp (pattern, rx, REG_NOSUB);
-  if (code != 0)
-    {
-      char *err = get_regcomp_error (code, pattern);
-
-      make_cleanup (xfree, err);
-      error (("%s: %s"), message, err);
-    }
-
-  return make_regfree_cleanup (pattern);
-}
-
-\f
-
 /* This function supports the query, nquery, and yquery functions.
    Ask user a y-or-n question and return 0 if answer is no, 1 if
    answer is yes, or default the answer to the specified default

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

* [PATCH v6 07/10] Move linux_find_memory_regions_full & co.
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
                   ` (5 preceding siblings ...)
  2015-06-07 20:05 ` [PATCH v6 10/10] Tests for validate symbol file using build-id Jan Kratochvil
@ 2015-06-07 20:05 ` Jan Kratochvil
  2015-06-08  8:42   ` Gary Benson
  2015-06-07 20:05 ` [PATCH v6 08/10] gdbserver build-id attribute generator Jan Kratochvil
                   ` (2 subsequent siblings)
  9 siblings, 1 reply; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:05 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

this should be just a move with no changes.

Approved by:
	https://sourceware.org/ml/gdb-patches/2014-05/msg00372.html


Jan


gdb/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Move linux_find_memory_regions_full & co.
	* common/linux-maps.c (fcntl.h, unistd.h, target.h, gdb_assert.h)
	(ctype.h, string.h, target-utils.h, gdb_regex.h): Include.
	(struct smaps_vmflags, read_mapping, decode_vmflags)
	(mapping_is_anonymous_p, dump_mapping_p): Move from linux-tdep.c.
	[GDBSERVER] (linux_find_memory_read_stralloc_1_pread): New function.
	[GDBSERVER] (linux_find_memory_read_stralloc_1): New function.
	(linux_find_memory_read_stralloc): New function.
	(linux_find_memory_regions_full): Move from linux-tdep.c.
	* common/linux-maps.h (read_mapping): New declaration.
	(linux_find_memory_region_ftype, enum filterflags): Moved from
	linux-tdep.c.
	(linux_find_memory_regions_full): New declaration.
	* common/target-utils.c (string.h, gdb_assert.h): Include.
	(read_alloc, read_stralloc): Move definitions from target.c.
	* common/target-utils.h (read_alloc_pread_ftype): New typedef.
	(read_alloc): New declaration.
	(read_stralloc_func_ftype): New typedef.
	(read_stralloc): New declaration.
	* linux-tdep.c (linux-maps.h): Include.
	(gdb_regex.h): Remove the include.
	(enum filterflags, struct smaps_vmflags, read_mapping, decode_vmflags)
	(mapping_is_anonymous_p, dump_mapping_p): Moved to common/linux-maps.c.
	(linux_find_memory_region_ftype): Moved typedef to common/linux-maps.h.
	(linux_find_memory_regions_full): Moved definition to
	common/linux-maps.c.
	* target.c (target-utils.h): Include.
	(read_alloc_pread_ftype): Moved typedef to common/target-utils.h.
	(read_alloc, read_stralloc): Moved definitions to
	common/target-utils.c.
---
 gdb/common/linux-maps.c   |  521 +++++++++++++++++++++++++++++++++++++++++++++
 gdb/common/linux-maps.h   |   42 ++++
 gdb/common/target-utils.c |   80 +++++++
 gdb/common/target-utils.h |   10 +
 gdb/linux-tdep.c          |  499 -------------------------------------------
 gdb/target.c              |   89 --------
 6 files changed, 657 insertions(+), 584 deletions(-)

diff --git a/gdb/common/linux-maps.c b/gdb/common/linux-maps.c
index bb3eac9..462a00f 100644
--- a/gdb/common/linux-maps.c
+++ b/gdb/common/linux-maps.c
@@ -18,8 +18,529 @@
 
 #ifdef GDBSERVER
 #include "server.h"
+#include <fcntl.h>
+#include <unistd.h>
 #else
 #include "defs.h"
+#include "target.h"
 #endif
 
 #include "linux-maps.h"
+#include "gdb_assert.h"
+#include <ctype.h>
+#include <string.h>
+#include "target-utils.h"
+#include "gdb_regex.h"
+
+/* This struct is used to map flags found in the "VmFlags:" field (in
+   the /proc/<PID>/smaps file).  */
+
+struct smaps_vmflags
+  {
+    /* Zero if this structure has not been initialized yet.  It
+       probably means that the Linux kernel being used does not emit
+       the "VmFlags:" field on "/proc/PID/smaps".  */
+
+    unsigned int initialized_p : 1;
+
+    /* Memory mapped I/O area (VM_IO, "io").  */
+
+    unsigned int io_page : 1;
+
+    /* Area uses huge TLB pages (VM_HUGETLB, "ht").  */
+
+    unsigned int uses_huge_tlb : 1;
+
+    /* Do not include this memory region on the coredump (VM_DONTDUMP, "dd").  */
+
+    unsigned int exclude_coredump : 1;
+
+    /* Is this a MAP_SHARED mapping (VM_SHARED, "sh").  */
+
+    unsigned int shared_mapping : 1;
+  };
+
+/* Service function for corefiles and info proc.  */
+
+void
+read_mapping (const char *line,
+	      ULONGEST *addr, ULONGEST *endaddr,
+	      const char **permissions, size_t *permissions_len,
+	      ULONGEST *offset,
+              const char **device, size_t *device_len,
+	      ULONGEST *inode,
+	      const char **filename)
+{
+  const char *p = line;
+
+  *addr = strtoulst (p, &p, 16);
+  if (*p == '-')
+    p++;
+  *endaddr = strtoulst (p, &p, 16);
+
+  p = skip_spaces_const (p);
+  *permissions = p;
+  while (*p && !isspace (*p))
+    p++;
+  *permissions_len = p - *permissions;
+
+  *offset = strtoulst (p, &p, 16);
+
+  p = skip_spaces_const (p);
+  *device = p;
+  while (*p && !isspace (*p))
+    p++;
+  *device_len = p - *device;
+
+  *inode = strtoulst (p, &p, 10);
+
+  p = skip_spaces_const (p);
+  *filename = p;
+}
+
+/* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
+
+   This function was based on the documentation found on
+   <Documentation/filesystems/proc.txt>, on the Linux kernel.
+
+   Linux kernels before commit
+   834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
+   field on smaps.  */
+
+static void
+decode_vmflags (char *p, struct smaps_vmflags *v)
+{
+  char *saveptr = NULL;
+  const char *s;
+
+  v->initialized_p = 1;
+  p = skip_to_space (p);
+  p = skip_spaces (p);
+
+  for (s = strtok_r (p, " ", &saveptr);
+       s != NULL;
+       s = strtok_r (NULL, " ", &saveptr))
+    {
+      if (strcmp (s, "io") == 0)
+	v->io_page = 1;
+      else if (strcmp (s, "ht") == 0)
+	v->uses_huge_tlb = 1;
+      else if (strcmp (s, "dd") == 0)
+	v->exclude_coredump = 1;
+      else if (strcmp (s, "sh") == 0)
+	v->shared_mapping = 1;
+    }
+}
+
+/* Return 1 if the memory mapping is anonymous, 0 otherwise.
+
+   FILENAME is the name of the file present in the first line of the
+   memory mapping, in the "/proc/PID/smaps" output.  For example, if
+   the first line is:
+
+   7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770   /path/to/file
+
+   Then FILENAME will be "/path/to/file".  */
+
+static int
+mapping_is_anonymous_p (const char *filename)
+{
+  static regex_t dev_zero_regex, shmem_file_regex, file_deleted_regex;
+  static int init_regex_p = 0;
+
+  if (!init_regex_p)
+    {
+      struct cleanup *c = make_cleanup (null_cleanup, NULL);
+
+      /* Let's be pessimistic and assume there will be an error while
+	 compiling the regex'es.  */
+      init_regex_p = -1;
+
+      /* DEV_ZERO_REGEX matches "/dev/zero" filenames (with or
+	 without the "(deleted)" string in the end).  We know for
+	 sure, based on the Linux kernel code, that memory mappings
+	 whose associated filename is "/dev/zero" are guaranteed to be
+	 MAP_ANONYMOUS.  */
+      compile_rx_or_error (&dev_zero_regex, "^/dev/zero\\( (deleted)\\)\\?$",
+			   _("Could not compile regex to match /dev/zero "
+			     "filename"));
+      /* SHMEM_FILE_REGEX matches "/SYSV%08x" filenames (with or
+	 without the "(deleted)" string in the end).  These filenames
+	 refer to shared memory (shmem), and memory mappings
+	 associated with them are MAP_ANONYMOUS as well.  */
+      compile_rx_or_error (&shmem_file_regex,
+			   "^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$",
+			   _("Could not compile regex to match shmem "
+			     "filenames"));
+      /* FILE_DELETED_REGEX is a heuristic we use to try to mimic the
+	 Linux kernel's 'n_link == 0' code, which is responsible to
+	 decide if it is dealing with a 'MAP_SHARED | MAP_ANONYMOUS'
+	 mapping.  In other words, if FILE_DELETED_REGEX matches, it
+	 does not necessarily mean that we are dealing with an
+	 anonymous shared mapping.  However, there is no easy way to
+	 detect this currently, so this is the best approximation we
+	 have.
+
+	 As a result, GDB will dump readonly pages of deleted
+	 executables when using the default value of coredump_filter
+	 (0x33), while the Linux kernel will not dump those pages.
+	 But we can live with that.  */
+      compile_rx_or_error (&file_deleted_regex, " (deleted)$",
+			   _("Could not compile regex to match "
+			     "'<file> (deleted)'"));
+      /* We will never release these regexes, so just discard the
+	 cleanups.  */
+      discard_cleanups (c);
+
+      /* If we reached this point, then everything succeeded.  */
+      init_regex_p = 1;
+    }
+
+  if (init_regex_p == -1)
+    {
+      const char deleted[] = " (deleted)";
+      size_t del_len = sizeof (deleted) - 1;
+      size_t filename_len = strlen (filename);
+
+      /* There was an error while compiling the regex'es above.  In
+	 order to try to give some reliable information to the caller,
+	 we just try to find the string " (deleted)" in the filename.
+	 If we managed to find it, then we assume the mapping is
+	 anonymous.  */
+      return (filename_len >= del_len
+	      && strcmp (filename + filename_len - del_len, deleted) == 0);
+    }
+
+  if (*filename == '\0'
+      || regexec (&dev_zero_regex, filename, 0, NULL, 0) == 0
+      || regexec (&shmem_file_regex, filename, 0, NULL, 0) == 0
+      || regexec (&file_deleted_regex, filename, 0, NULL, 0) == 0)
+    return 1;
+
+  return 0;
+}
+
+/* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
+   MAYBE_PRIVATE_P, and MAPPING_ANONYMOUS_P) should not be dumped, or
+   greater than 0 if it should.
+
+   In a nutshell, this is the logic that we follow in order to decide
+   if a mapping should be dumped or not.
+
+   - If the mapping is associated to a file whose name ends with
+     " (deleted)", or if the file is "/dev/zero", or if it is
+     "/SYSV%08x" (shared memory), or if there is no file associated
+     with it, or if the AnonHugePages: or the Anonymous: fields in the
+     /proc/PID/smaps have contents, then GDB considers this mapping to
+     be anonymous.  Otherwise, GDB considers this mapping to be a
+     file-backed mapping (because there will be a file associated with
+     it).
+ 
+     It is worth mentioning that, from all those checks described
+     above, the most fragile is the one to see if the file name ends
+     with " (deleted)".  This does not necessarily mean that the
+     mapping is anonymous, because the deleted file associated with
+     the mapping may have been a hard link to another file, for
+     example.  The Linux kernel checks to see if "i_nlink == 0", but
+     GDB cannot easily (and normally) do this check (iff running as
+     root, it could find the mapping in /proc/PID/map_files/ and
+     determine whether there still are other hard links to the
+     inode/file).  Therefore, we made a compromise here, and we assume
+     that if the file name ends with " (deleted)", then the mapping is
+     indeed anonymous.  FWIW, this is something the Linux kernel could
+     do better: expose this information in a more direct way.
+ 
+   - If we see the flag "sh" in the "VmFlags:" field (in
+     /proc/PID/smaps), then certainly the memory mapping is shared
+     (VM_SHARED).  If we have access to the VmFlags, and we don't see
+     the "sh" there, then certainly the mapping is private.  However,
+     Linux kernels before commit
+     834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
+     "VmFlags:" field; in that case, we use another heuristic: if we
+     see 'p' in the permission flags, then we assume that the mapping
+     is private, even though the presence of the 's' flag there would
+     mean VM_MAYSHARE, which means the mapping could still be private.
+     This should work OK enough, however.  */
+
+static int
+dump_mapping_p (enum filterflags filterflags, const struct smaps_vmflags *v,
+		int maybe_private_p, int mapping_anon_p, int mapping_file_p,
+		const char *filename)
+{
+  /* Initially, we trust in what we received from our caller.  This
+     value may not be very precise (i.e., it was probably gathered
+     from the permission line in the /proc/PID/smaps list, which
+     actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
+     what we have until we take a look at the "VmFlags:" field
+     (assuming that the version of the Linux kernel being used
+     supports it, of course).  */
+  int private_p = maybe_private_p;
+
+  /* We always dump vDSO and vsyscall mappings, because it's likely that
+     there'll be no file to read the contents from at core load time.
+     The kernel does the same.  */
+  if (strcmp ("[vdso]", filename) == 0
+      || strcmp ("[vsyscall]", filename) == 0)
+    return 1;
+
+  if (v->initialized_p)
+    {
+      /* We never dump I/O mappings.  */
+      if (v->io_page)
+	return 0;
+
+      /* Check if we should exclude this mapping.  */
+      if (v->exclude_coredump)
+	return 0;
+
+      /* Update our notion of whether this mapping is shared or
+	 private based on a trustworthy value.  */
+      private_p = !v->shared_mapping;
+
+      /* HugeTLB checking.  */
+      if (v->uses_huge_tlb)
+	{
+	  if ((private_p && (filterflags & COREFILTER_HUGETLB_PRIVATE))
+	      || (!private_p && (filterflags & COREFILTER_HUGETLB_SHARED)))
+	    return 1;
+
+	  return 0;
+	}
+    }
+
+  if (private_p)
+    {
+      if (mapping_anon_p && mapping_file_p)
+	{
+	  /* This is a special situation.  It can happen when we see a
+	     mapping that is file-backed, but that contains anonymous
+	     pages.  */
+	  return ((filterflags & COREFILTER_ANON_PRIVATE) != 0
+		  || (filterflags & COREFILTER_MAPPED_PRIVATE) != 0);
+	}
+      else if (mapping_anon_p)
+	return (filterflags & COREFILTER_ANON_PRIVATE) != 0;
+      else
+	return (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
+    }
+  else
+    {
+      if (mapping_anon_p && mapping_file_p)
+	{
+	  /* This is a special situation.  It can happen when we see a
+	     mapping that is file-backed, but that contains anonymous
+	     pages.  */
+	  return ((filterflags & COREFILTER_ANON_SHARED) != 0
+		  || (filterflags & COREFILTER_MAPPED_SHARED) != 0);
+	}
+      else if (mapping_anon_p)
+	return (filterflags & COREFILTER_ANON_SHARED) != 0;
+      else
+	return (filterflags & COREFILTER_MAPPED_SHARED) != 0;
+    }
+}
+
+#ifdef GDBSERVER
+
+static int
+linux_find_memory_read_stralloc_1_pread (int handle, gdb_byte *read_buf,
+                                         int len, ULONGEST offset,
+					 int *target_errno)
+{
+  int retval = pread (handle, read_buf, len, offset);
+
+  *target_errno = errno;
+  return retval;
+}
+
+static LONGEST
+linux_find_memory_read_stralloc_1 (const char *filename, gdb_byte **buf_p,
+				   int padding)
+{
+  int fd;
+  LONGEST retval;
+
+  fd = open (filename, O_RDONLY);
+  if (fd == -1)
+    return -1;
+
+  retval = read_alloc (buf_p, fd, linux_find_memory_read_stralloc_1_pread,
+                       padding);
+
+  close (fd);
+
+  return retval;
+}
+
+#endif /* GDBSERVER */
+
+static char *
+linux_find_memory_read_stralloc (const char *filename)
+{
+#ifndef GDBSERVER
+  return target_fileio_read_stralloc (filename);
+#else /* GDBSERVER */
+  return read_stralloc (filename, linux_find_memory_read_stralloc_1);
+#endif /* GDBSERVER */
+}
+
+/* List memory regions in the inferior PID matched to FILTERFLAGS for
+   a corefile.  Call FUNC with FUNC_DATA for each such region.  Return
+   immediately with the value returned by FUNC if it is non-zero.
+   *MEMORY_TO_FREE_PTR should be registered to be freed automatically if
+   called FUNC throws an exception.  MEMORY_TO_FREE_PTR can be also
+   passed as NULL if it is not used.  Return -1 if error occurs, 0 if
+   all memory regions have been processed or return the value from FUNC
+   if FUNC returns non-zero.  */
+
+int
+linux_find_memory_regions_full (pid_t pid, enum filterflags filterflags,
+				linux_find_memory_region_ftype *func,
+				void *func_data)
+{
+  char mapsfilename[100];
+  char *data;
+
+  xsnprintf (mapsfilename, sizeof mapsfilename, "/proc/%d/smaps", pid);
+  data = linux_find_memory_read_stralloc (mapsfilename);
+  if (data == NULL)
+    {
+      /* Older Linux kernels did not support /proc/PID/smaps.  */
+      xsnprintf (mapsfilename, sizeof mapsfilename, "/proc/%d/maps", pid);
+      data = linux_find_memory_read_stralloc (mapsfilename);
+    }
+
+  if (data != NULL)
+    {
+      struct cleanup *cleanup = make_cleanup (xfree, data);
+      char *line, *t;
+      int retval = 0;
+
+      line = strtok_r (data, "\n", &t);
+      while (line != NULL)
+	{
+	  ULONGEST addr, endaddr, offset, inode;
+	  const char *permissions, *device, *filename;
+	  struct smaps_vmflags v;
+	  size_t permissions_len, device_len;
+	  int read, write, exec, priv;
+	  int has_anonymous = 0;
+	  int should_dump_p = 0;
+	  int mapping_anon_p;
+	  int mapping_file_p;
+
+	  memset (&v, 0, sizeof (v));
+	  read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
+			&offset, &device, &device_len, &inode, &filename);
+	  mapping_anon_p = mapping_is_anonymous_p (filename);
+	  /* If the mapping is not anonymous, then we can consider it
+	     to be file-backed.  These two states (anonymous or
+	     file-backed) seem to be exclusive, but they can actually
+	     coexist.  For example, if a file-backed mapping has
+	     "Anonymous:" pages (see more below), then the Linux
+	     kernel will dump this mapping when the user specified
+	     that she only wants anonymous mappings in the corefile
+	     (*even* when she explicitly disabled the dumping of
+	     file-backed mappings).  */
+	  mapping_file_p = !mapping_anon_p;
+
+	  /* Decode permissions.  */
+	  read = (memchr (permissions, 'r', permissions_len) != 0);
+	  write = (memchr (permissions, 'w', permissions_len) != 0);
+	  exec = (memchr (permissions, 'x', permissions_len) != 0);
+	  /* 'private' here actually means VM_MAYSHARE, and not
+	     VM_SHARED.  In order to know if a mapping is really
+	     private or not, we must check the flag "sh" in the
+	     VmFlags field.  This is done by decode_vmflags.  However,
+	     if we are using a Linux kernel released before the commit
+	     834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
+	     not have the VmFlags there.  In this case, there is
+	     really no way to know if we are dealing with VM_SHARED,
+	     so we just assume that VM_MAYSHARE is enough.  */
+	  priv = memchr (permissions, 'p', permissions_len) != 0;
+
+	  /* Try to detect if region should be dumped by parsing smaps
+	     counters.  */
+	  for (line = strtok_r (NULL, "\n", &t);
+	       line != NULL && line[0] >= 'A' && line[0] <= 'Z';
+	       line = strtok_r (NULL, "\n", &t))
+	    {
+	      char keyword[64 + 1];
+
+	      if (sscanf (line, "%64s", keyword) != 1)
+		{
+		  warning (_("Error parsing {s,}maps file '%s'"), mapsfilename);
+		  break;
+		}
+
+	      if (strcmp (keyword, "Anonymous:") == 0)
+		{
+		  /* Older Linux kernels did not support the
+		     "Anonymous:" counter.  Check it here.  */
+		  has_anonymous = 1;
+		}
+	      else if (strcmp (keyword, "VmFlags:") == 0)
+		decode_vmflags (line, &v);
+
+	      if (strcmp (keyword, "AnonHugePages:") == 0
+		  || strcmp (keyword, "Anonymous:") == 0)
+		{
+		  unsigned long number;
+
+		  if (sscanf (line, "%*s%lu", &number) != 1)
+		    {
+		      warning (_("Error parsing {s,}maps file '%s' number"),
+			       mapsfilename);
+		      break;
+		    }
+		  if (number > 0)
+		    {
+		      /* Even if we are dealing with a file-backed
+			 mapping, if it contains anonymous pages we
+			 consider it to be *also* an anonymous
+			 mapping, because this is what the Linux
+			 kernel does:
+
+			 // Dump segments that have been written to.
+			 if (vma->anon_vma && FILTER(ANON_PRIVATE))
+			 	goto whole;
+
+			 Note that if the mapping is already marked as
+			 file-backed (i.e., mapping_file_p is
+			 non-zero), then this is a special case, and
+			 this mapping will be dumped either when the
+			 user wants to dump file-backed *or* anonymous
+			 mappings.  */
+		      mapping_anon_p = 1;
+		    }
+		}
+	    }
+
+	  if (has_anonymous)
+	    should_dump_p = dump_mapping_p (filterflags, &v, priv,
+					    mapping_anon_p, mapping_file_p,
+					    filename);
+	  else
+	    {
+	      /* Older Linux kernels did not support the "Anonymous:" counter.
+		 If it is missing, we can't be sure - dump all the pages.  */
+	      should_dump_p = 1;
+	    }
+
+	  /* Invoke the callback function to create the corefile segment.  */
+	  if (should_dump_p)
+	    retval = func (addr, endaddr - addr, offset, inode,
+			   read, write, exec,
+			   1, /* MODIFIED is true because we want to dump the
+				 mapping.  */
+			   filename, func_data);
+	  if (retval != 0)
+	    break;
+	}
+
+      do_cleanups (cleanup);
+      return retval;
+    }
+
+  return -1;
+}
diff --git a/gdb/common/linux-maps.h b/gdb/common/linux-maps.h
index ebf6f37..42d7edf 100644
--- a/gdb/common/linux-maps.h
+++ b/gdb/common/linux-maps.h
@@ -19,4 +19,46 @@
 #ifndef COMMON_LINUX_MAPS_H
 #define COMMON_LINUX_MAPS_H
 
+extern void
+  read_mapping (const char *line,
+		ULONGEST *addr, ULONGEST *endaddr,
+		const char **permissions, size_t *permissions_len,
+		ULONGEST *offset,
+		const char **device, size_t *device_len,
+		ULONGEST *inode,
+		const char **filename);
+
+/* Callback function for linux_find_memory_regions_full.  If it returns
+   non-zero linux_find_memory_regions_full returns immediately with that
+   value.  */
+
+typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
+					    ULONGEST offset, ULONGEST inode,
+					    int read, int write,
+					    int exec, int modified,
+					    const char *filename,
+					    void *data);
+
+/* This enum represents the values that the user can choose when
+   informing the Linux kernel about which memory mappings will be
+   dumped in a corefile.  They are described in the file
+   Documentation/filesystems/proc.txt, inside the Linux kernel
+   tree.  */
+
+enum filterflags
+  {
+    COREFILTER_ANON_PRIVATE = 1 << 0,
+    COREFILTER_ANON_SHARED = 1 << 1,
+    COREFILTER_MAPPED_PRIVATE = 1 << 2,
+    COREFILTER_MAPPED_SHARED = 1 << 3,
+    COREFILTER_ELF_HEADERS = 1 << 4,
+    COREFILTER_HUGETLB_PRIVATE = 1 << 5,
+    COREFILTER_HUGETLB_SHARED = 1 << 6,
+  };
+
+extern int
+  linux_find_memory_regions_full (pid_t pid, enum filterflags filterflags,
+				  linux_find_memory_region_ftype *func,
+				  void *func_data);
+
 #endif /* COMMON_LINUX_MAPS_H */
diff --git a/gdb/common/target-utils.c b/gdb/common/target-utils.c
index 308996d..ab8991d 100644
--- a/gdb/common/target-utils.c
+++ b/gdb/common/target-utils.c
@@ -23,4 +23,84 @@
 #include "defs.h"
 #endif
 
+#include <string.h>
 #include "target-utils.h"
+#include "gdb_assert.h"
+
+LONGEST
+read_alloc (gdb_byte **buf_p, int handle, read_alloc_pread_ftype *pread_func,
+	    int padding)
+{
+  size_t buf_alloc, buf_pos;
+  gdb_byte *buf;
+  LONGEST n;
+  int target_errno;
+
+  /* Start by reading up to 4K at a time.  The target will throttle
+     this number down if necessary.  */
+  buf_alloc = 4096;
+  buf = xmalloc (buf_alloc);
+  buf_pos = 0;
+  while (1)
+    {
+      n = pread_func (handle, &buf[buf_pos], buf_alloc - buf_pos - padding,
+		      buf_pos, &target_errno);
+      if (n <= 0)
+	{
+	  if (n < 0 || (n == 0 && buf_pos == 0))
+	    xfree (buf);
+	  else
+	    *buf_p = buf;
+	  if (n < 0)
+	    {
+	      /* An error occurred.  */
+	      return -1;
+	    }
+	  else
+	    {
+	      /* Read all there was.  */
+	      return buf_pos;
+	    }
+	}
+
+      buf_pos += n;
+
+      /* If the buffer is filling up, expand it.  */
+      if (buf_alloc < buf_pos * 2)
+	{
+	  buf_alloc *= 2;
+	  buf = xrealloc (buf, buf_alloc);
+	}
+    }
+}
+
+char *
+read_stralloc (const char *filename, read_stralloc_func_ftype *func)
+{
+  gdb_byte *buffer;
+  char *bufstr;
+  LONGEST i, transferred;
+
+  transferred = func (filename, &buffer, 1);
+  bufstr = (char *) buffer;
+
+  if (transferred < 0)
+    return NULL;
+
+  if (transferred == 0)
+    return xstrdup ("");
+
+  bufstr[transferred] = 0;
+
+  /* Check for embedded NUL bytes; but allow trailing NULs.  */
+  for (i = strlen (bufstr); i < transferred; i++)
+    if (bufstr[i] != 0)
+      {
+	warning (_("target file %s "
+		   "contained unexpected null characters"),
+		 filename);
+	break;
+      }
+
+  return bufstr;
+}
diff --git a/gdb/common/target-utils.h b/gdb/common/target-utils.h
index db33ec8..da5eb65 100644
--- a/gdb/common/target-utils.h
+++ b/gdb/common/target-utils.h
@@ -20,4 +20,14 @@
 #ifndef COMMON_TARGET_UTILS_H
 #define COMMON_TARGET_UTILS_H
 
+typedef int (read_alloc_pread_ftype) (int handle, gdb_byte *read_buf, int len,
+                                      ULONGEST offset, int *target_errno);
+extern LONGEST read_alloc (gdb_byte **buf_p, int handle,
+                           read_alloc_pread_ftype *pread_func, int padding);
+
+typedef LONGEST (read_stralloc_func_ftype) (const char *filename,
+					    gdb_byte **buf_p, int padding);
+extern char *read_stralloc (const char *filename,
+			    read_stralloc_func_ftype *func);
+
 #endif /* COMMON_TARGET_UTILS_H */
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 4618415..43f309a 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -35,56 +35,11 @@
 #include "observer.h"
 #include "objfiles.h"
 #include "infcall.h"
+#include "linux-maps.h"
 #include "gdbcmd.h"
-#include "gdb_regex.h"
 
 #include <ctype.h>
 
-/* This enum represents the values that the user can choose when
-   informing the Linux kernel about which memory mappings will be
-   dumped in a corefile.  They are described in the file
-   Documentation/filesystems/proc.txt, inside the Linux kernel
-   tree.  */
-
-enum filterflags
-  {
-    COREFILTER_ANON_PRIVATE = 1 << 0,
-    COREFILTER_ANON_SHARED = 1 << 1,
-    COREFILTER_MAPPED_PRIVATE = 1 << 2,
-    COREFILTER_MAPPED_SHARED = 1 << 3,
-    COREFILTER_ELF_HEADERS = 1 << 4,
-    COREFILTER_HUGETLB_PRIVATE = 1 << 5,
-    COREFILTER_HUGETLB_SHARED = 1 << 6,
-  };
-
-/* This struct is used to map flags found in the "VmFlags:" field (in
-   the /proc/<PID>/smaps file).  */
-
-struct smaps_vmflags
-  {
-    /* Zero if this structure has not been initialized yet.  It
-       probably means that the Linux kernel being used does not emit
-       the "VmFlags:" field on "/proc/PID/smaps".  */
-
-    unsigned int initialized_p : 1;
-
-    /* Memory mapped I/O area (VM_IO, "io").  */
-
-    unsigned int io_page : 1;
-
-    /* Area uses huge TLB pages (VM_HUGETLB, "ht").  */
-
-    unsigned int uses_huge_tlb : 1;
-
-    /* Do not include this memory region on the coredump (VM_DONTDUMP, "dd").  */
-
-    unsigned int exclude_coredump : 1;
-
-    /* Is this a MAP_SHARED mapping (VM_SHARED, "sh").  */
-
-    unsigned int shared_mapping : 1;
-  };
-
 /* Whether to take the /proc/PID/coredump_filter into account when
    generating a corefile.  */
 
@@ -395,286 +350,6 @@ linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
   return normal_pid_to_str (ptid);
 }
 
-/* Service function for corefiles and info proc.  */
-
-static void
-read_mapping (const char *line,
-	      ULONGEST *addr, ULONGEST *endaddr,
-	      const char **permissions, size_t *permissions_len,
-	      ULONGEST *offset,
-              const char **device, size_t *device_len,
-	      ULONGEST *inode,
-	      const char **filename)
-{
-  const char *p = line;
-
-  *addr = strtoulst (p, &p, 16);
-  if (*p == '-')
-    p++;
-  *endaddr = strtoulst (p, &p, 16);
-
-  p = skip_spaces_const (p);
-  *permissions = p;
-  while (*p && !isspace (*p))
-    p++;
-  *permissions_len = p - *permissions;
-
-  *offset = strtoulst (p, &p, 16);
-
-  p = skip_spaces_const (p);
-  *device = p;
-  while (*p && !isspace (*p))
-    p++;
-  *device_len = p - *device;
-
-  *inode = strtoulst (p, &p, 10);
-
-  p = skip_spaces_const (p);
-  *filename = p;
-}
-
-/* Helper function to decode the "VmFlags" field in /proc/PID/smaps.
-
-   This function was based on the documentation found on
-   <Documentation/filesystems/proc.txt>, on the Linux kernel.
-
-   Linux kernels before commit
-   834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have this
-   field on smaps.  */
-
-static void
-decode_vmflags (char *p, struct smaps_vmflags *v)
-{
-  char *saveptr = NULL;
-  const char *s;
-
-  v->initialized_p = 1;
-  p = skip_to_space (p);
-  p = skip_spaces (p);
-
-  for (s = strtok_r (p, " ", &saveptr);
-       s != NULL;
-       s = strtok_r (NULL, " ", &saveptr))
-    {
-      if (strcmp (s, "io") == 0)
-	v->io_page = 1;
-      else if (strcmp (s, "ht") == 0)
-	v->uses_huge_tlb = 1;
-      else if (strcmp (s, "dd") == 0)
-	v->exclude_coredump = 1;
-      else if (strcmp (s, "sh") == 0)
-	v->shared_mapping = 1;
-    }
-}
-
-/* Return 1 if the memory mapping is anonymous, 0 otherwise.
-
-   FILENAME is the name of the file present in the first line of the
-   memory mapping, in the "/proc/PID/smaps" output.  For example, if
-   the first line is:
-
-   7fd0ca877000-7fd0d0da0000 r--p 00000000 fd:02 2100770   /path/to/file
-
-   Then FILENAME will be "/path/to/file".  */
-
-static int
-mapping_is_anonymous_p (const char *filename)
-{
-  static regex_t dev_zero_regex, shmem_file_regex, file_deleted_regex;
-  static int init_regex_p = 0;
-
-  if (!init_regex_p)
-    {
-      struct cleanup *c = make_cleanup (null_cleanup, NULL);
-
-      /* Let's be pessimistic and assume there will be an error while
-	 compiling the regex'es.  */
-      init_regex_p = -1;
-
-      /* DEV_ZERO_REGEX matches "/dev/zero" filenames (with or
-	 without the "(deleted)" string in the end).  We know for
-	 sure, based on the Linux kernel code, that memory mappings
-	 whose associated filename is "/dev/zero" are guaranteed to be
-	 MAP_ANONYMOUS.  */
-      compile_rx_or_error (&dev_zero_regex, "^/dev/zero\\( (deleted)\\)\\?$",
-			   _("Could not compile regex to match /dev/zero "
-			     "filename"));
-      /* SHMEM_FILE_REGEX matches "/SYSV%08x" filenames (with or
-	 without the "(deleted)" string in the end).  These filenames
-	 refer to shared memory (shmem), and memory mappings
-	 associated with them are MAP_ANONYMOUS as well.  */
-      compile_rx_or_error (&shmem_file_regex,
-			   "^/\\?SYSV[0-9a-fA-F]\\{8\\}\\( (deleted)\\)\\?$",
-			   _("Could not compile regex to match shmem "
-			     "filenames"));
-      /* FILE_DELETED_REGEX is a heuristic we use to try to mimic the
-	 Linux kernel's 'n_link == 0' code, which is responsible to
-	 decide if it is dealing with a 'MAP_SHARED | MAP_ANONYMOUS'
-	 mapping.  In other words, if FILE_DELETED_REGEX matches, it
-	 does not necessarily mean that we are dealing with an
-	 anonymous shared mapping.  However, there is no easy way to
-	 detect this currently, so this is the best approximation we
-	 have.
-
-	 As a result, GDB will dump readonly pages of deleted
-	 executables when using the default value of coredump_filter
-	 (0x33), while the Linux kernel will not dump those pages.
-	 But we can live with that.  */
-      compile_rx_or_error (&file_deleted_regex, " (deleted)$",
-			   _("Could not compile regex to match "
-			     "'<file> (deleted)'"));
-      /* We will never release these regexes, so just discard the
-	 cleanups.  */
-      discard_cleanups (c);
-
-      /* If we reached this point, then everything succeeded.  */
-      init_regex_p = 1;
-    }
-
-  if (init_regex_p == -1)
-    {
-      const char deleted[] = " (deleted)";
-      size_t del_len = sizeof (deleted) - 1;
-      size_t filename_len = strlen (filename);
-
-      /* There was an error while compiling the regex'es above.  In
-	 order to try to give some reliable information to the caller,
-	 we just try to find the string " (deleted)" in the filename.
-	 If we managed to find it, then we assume the mapping is
-	 anonymous.  */
-      return (filename_len >= del_len
-	      && strcmp (filename + filename_len - del_len, deleted) == 0);
-    }
-
-  if (*filename == '\0'
-      || regexec (&dev_zero_regex, filename, 0, NULL, 0) == 0
-      || regexec (&shmem_file_regex, filename, 0, NULL, 0) == 0
-      || regexec (&file_deleted_regex, filename, 0, NULL, 0) == 0)
-    return 1;
-
-  return 0;
-}
-
-/* Return 0 if the memory mapping (which is related to FILTERFLAGS, V,
-   MAYBE_PRIVATE_P, and MAPPING_ANONYMOUS_P) should not be dumped, or
-   greater than 0 if it should.
-
-   In a nutshell, this is the logic that we follow in order to decide
-   if a mapping should be dumped or not.
-
-   - If the mapping is associated to a file whose name ends with
-     " (deleted)", or if the file is "/dev/zero", or if it is
-     "/SYSV%08x" (shared memory), or if there is no file associated
-     with it, or if the AnonHugePages: or the Anonymous: fields in the
-     /proc/PID/smaps have contents, then GDB considers this mapping to
-     be anonymous.  Otherwise, GDB considers this mapping to be a
-     file-backed mapping (because there will be a file associated with
-     it).
- 
-     It is worth mentioning that, from all those checks described
-     above, the most fragile is the one to see if the file name ends
-     with " (deleted)".  This does not necessarily mean that the
-     mapping is anonymous, because the deleted file associated with
-     the mapping may have been a hard link to another file, for
-     example.  The Linux kernel checks to see if "i_nlink == 0", but
-     GDB cannot easily (and normally) do this check (iff running as
-     root, it could find the mapping in /proc/PID/map_files/ and
-     determine whether there still are other hard links to the
-     inode/file).  Therefore, we made a compromise here, and we assume
-     that if the file name ends with " (deleted)", then the mapping is
-     indeed anonymous.  FWIW, this is something the Linux kernel could
-     do better: expose this information in a more direct way.
- 
-   - If we see the flag "sh" in the "VmFlags:" field (in
-     /proc/PID/smaps), then certainly the memory mapping is shared
-     (VM_SHARED).  If we have access to the VmFlags, and we don't see
-     the "sh" there, then certainly the mapping is private.  However,
-     Linux kernels before commit
-     834f82e2aa9a8ede94b17b656329f850c1471514 (3.10) do not have the
-     "VmFlags:" field; in that case, we use another heuristic: if we
-     see 'p' in the permission flags, then we assume that the mapping
-     is private, even though the presence of the 's' flag there would
-     mean VM_MAYSHARE, which means the mapping could still be private.
-     This should work OK enough, however.  */
-
-static int
-dump_mapping_p (enum filterflags filterflags, const struct smaps_vmflags *v,
-		int maybe_private_p, int mapping_anon_p, int mapping_file_p,
-		const char *filename)
-{
-  /* Initially, we trust in what we received from our caller.  This
-     value may not be very precise (i.e., it was probably gathered
-     from the permission line in the /proc/PID/smaps list, which
-     actually refers to VM_MAYSHARE, and not VM_SHARED), but it is
-     what we have until we take a look at the "VmFlags:" field
-     (assuming that the version of the Linux kernel being used
-     supports it, of course).  */
-  int private_p = maybe_private_p;
-
-  /* We always dump vDSO and vsyscall mappings, because it's likely that
-     there'll be no file to read the contents from at core load time.
-     The kernel does the same.  */
-  if (strcmp ("[vdso]", filename) == 0
-      || strcmp ("[vsyscall]", filename) == 0)
-    return 1;
-
-  if (v->initialized_p)
-    {
-      /* We never dump I/O mappings.  */
-      if (v->io_page)
-	return 0;
-
-      /* Check if we should exclude this mapping.  */
-      if (v->exclude_coredump)
-	return 0;
-
-      /* Update our notion of whether this mapping is shared or
-	 private based on a trustworthy value.  */
-      private_p = !v->shared_mapping;
-
-      /* HugeTLB checking.  */
-      if (v->uses_huge_tlb)
-	{
-	  if ((private_p && (filterflags & COREFILTER_HUGETLB_PRIVATE))
-	      || (!private_p && (filterflags & COREFILTER_HUGETLB_SHARED)))
-	    return 1;
-
-	  return 0;
-	}
-    }
-
-  if (private_p)
-    {
-      if (mapping_anon_p && mapping_file_p)
-	{
-	  /* This is a special situation.  It can happen when we see a
-	     mapping that is file-backed, but that contains anonymous
-	     pages.  */
-	  return ((filterflags & COREFILTER_ANON_PRIVATE) != 0
-		  || (filterflags & COREFILTER_MAPPED_PRIVATE) != 0);
-	}
-      else if (mapping_anon_p)
-	return (filterflags & COREFILTER_ANON_PRIVATE) != 0;
-      else
-	return (filterflags & COREFILTER_MAPPED_PRIVATE) != 0;
-    }
-  else
-    {
-      if (mapping_anon_p && mapping_file_p)
-	{
-	  /* This is a special situation.  It can happen when we see a
-	     mapping that is file-backed, but that contains anonymous
-	     pages.  */
-	  return ((filterflags & COREFILTER_ANON_SHARED) != 0
-		  || (filterflags & COREFILTER_MAPPED_SHARED) != 0);
-	}
-      else if (mapping_anon_p)
-	return (filterflags & COREFILTER_ANON_SHARED) != 0;
-      else
-	return (filterflags & COREFILTER_MAPPED_SHARED) != 0;
-    }
-}
-
 /* Implement the "info proc" command.  */
 
 static void
@@ -1098,178 +773,6 @@ linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
     error (_("unable to handle request"));
 }
 
-/* Callback function for linux_find_memory_regions_full.  If it returns
-   non-zero linux_find_memory_regions_full returns immediately with that
-   value.  */
-
-typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
-					    ULONGEST offset, ULONGEST inode,
-					    int read, int write,
-					    int exec, int modified,
-					    const char *filename,
-					    void *data);
-
-/* List memory regions in the inferior PID matched to FILTERFLAGS for
-   a corefile.  Call FUNC with FUNC_DATA for each such region.  Return
-   immediately with the value returned by FUNC if it is non-zero.
-   *MEMORY_TO_FREE_PTR should be registered to be freed automatically if
-   called FUNC throws an exception.  MEMORY_TO_FREE_PTR can be also
-   passed as NULL if it is not used.  Return -1 if error occurs, 0 if
-   all memory regions have been processed or return the value from FUNC
-   if FUNC returns non-zero.  */
-
-static int
-linux_find_memory_regions_full (pid_t pid, enum filterflags filterflags,
-				linux_find_memory_region_ftype *func,
-				void *func_data)
-{
-  char mapsfilename[100];
-  char *data;
-
-  xsnprintf (mapsfilename, sizeof mapsfilename, "/proc/%d/smaps", pid);
-  data = target_fileio_read_stralloc (mapsfilename);
-  if (data == NULL)
-    {
-      /* Older Linux kernels did not support /proc/PID/smaps.  */
-      xsnprintf (mapsfilename, sizeof mapsfilename, "/proc/%d/maps", pid);
-      data = target_fileio_read_stralloc (mapsfilename);
-    }
-
-  if (data != NULL)
-    {
-      struct cleanup *cleanup = make_cleanup (xfree, data);
-      char *line, *t;
-      int retval = 0;
-
-      line = strtok_r (data, "\n", &t);
-      while (line != NULL)
-	{
-	  ULONGEST addr, endaddr, offset, inode;
-	  const char *permissions, *device, *filename;
-	  struct smaps_vmflags v;
-	  size_t permissions_len, device_len;
-	  int read, write, exec, priv;
-	  int has_anonymous = 0;
-	  int should_dump_p = 0;
-	  int mapping_anon_p;
-	  int mapping_file_p;
-
-	  memset (&v, 0, sizeof (v));
-	  read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
-			&offset, &device, &device_len, &inode, &filename);
-	  mapping_anon_p = mapping_is_anonymous_p (filename);
-	  /* If the mapping is not anonymous, then we can consider it
-	     to be file-backed.  These two states (anonymous or
-	     file-backed) seem to be exclusive, but they can actually
-	     coexist.  For example, if a file-backed mapping has
-	     "Anonymous:" pages (see more below), then the Linux
-	     kernel will dump this mapping when the user specified
-	     that she only wants anonymous mappings in the corefile
-	     (*even* when she explicitly disabled the dumping of
-	     file-backed mappings).  */
-	  mapping_file_p = !mapping_anon_p;
-
-	  /* Decode permissions.  */
-	  read = (memchr (permissions, 'r', permissions_len) != 0);
-	  write = (memchr (permissions, 'w', permissions_len) != 0);
-	  exec = (memchr (permissions, 'x', permissions_len) != 0);
-	  /* 'private' here actually means VM_MAYSHARE, and not
-	     VM_SHARED.  In order to know if a mapping is really
-	     private or not, we must check the flag "sh" in the
-	     VmFlags field.  This is done by decode_vmflags.  However,
-	     if we are using a Linux kernel released before the commit
-	     834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will
-	     not have the VmFlags there.  In this case, there is
-	     really no way to know if we are dealing with VM_SHARED,
-	     so we just assume that VM_MAYSHARE is enough.  */
-	  priv = memchr (permissions, 'p', permissions_len) != 0;
-
-	  /* Try to detect if region should be dumped by parsing smaps
-	     counters.  */
-	  for (line = strtok_r (NULL, "\n", &t);
-	       line != NULL && line[0] >= 'A' && line[0] <= 'Z';
-	       line = strtok_r (NULL, "\n", &t))
-	    {
-	      char keyword[64 + 1];
-
-	      if (sscanf (line, "%64s", keyword) != 1)
-		{
-		  warning (_("Error parsing {s,}maps file '%s'"), mapsfilename);
-		  break;
-		}
-
-	      if (strcmp (keyword, "Anonymous:") == 0)
-		{
-		  /* Older Linux kernels did not support the
-		     "Anonymous:" counter.  Check it here.  */
-		  has_anonymous = 1;
-		}
-	      else if (strcmp (keyword, "VmFlags:") == 0)
-		decode_vmflags (line, &v);
-
-	      if (strcmp (keyword, "AnonHugePages:") == 0
-		  || strcmp (keyword, "Anonymous:") == 0)
-		{
-		  unsigned long number;
-
-		  if (sscanf (line, "%*s%lu", &number) != 1)
-		    {
-		      warning (_("Error parsing {s,}maps file '%s' number"),
-			       mapsfilename);
-		      break;
-		    }
-		  if (number > 0)
-		    {
-		      /* Even if we are dealing with a file-backed
-			 mapping, if it contains anonymous pages we
-			 consider it to be *also* an anonymous
-			 mapping, because this is what the Linux
-			 kernel does:
-
-			 // Dump segments that have been written to.
-			 if (vma->anon_vma && FILTER(ANON_PRIVATE))
-			 	goto whole;
-
-			 Note that if the mapping is already marked as
-			 file-backed (i.e., mapping_file_p is
-			 non-zero), then this is a special case, and
-			 this mapping will be dumped either when the
-			 user wants to dump file-backed *or* anonymous
-			 mappings.  */
-		      mapping_anon_p = 1;
-		    }
-		}
-	    }
-
-	  if (has_anonymous)
-	    should_dump_p = dump_mapping_p (filterflags, &v, priv,
-					    mapping_anon_p, mapping_file_p,
-					    filename);
-	  else
-	    {
-	      /* Older Linux kernels did not support the "Anonymous:" counter.
-		 If it is missing, we can't be sure - dump all the pages.  */
-	      should_dump_p = 1;
-	    }
-
-	  /* Invoke the callback function to create the corefile segment.  */
-	  if (should_dump_p)
-	    retval = func (addr, endaddr - addr, offset, inode,
-			   read, write, exec,
-			   1, /* MODIFIED is true because we want to dump the
-				 mapping.  */
-			   filename, func_data);
-	  if (retval != 0)
-	    break;
-	}
-
-      do_cleanups (cleanup);
-      return retval;
-    }
-
-  return -1;
-}
-
 /* A structure for passing information through
    linux_find_memory_regions_full.  */
 
diff --git a/gdb/target.c b/gdb/target.c
index d11e7dc..94b7ffe 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -43,6 +43,7 @@
 #include "agent.h"
 #include "auxv.h"
 #include "target-debug.h"
+#include "target-utils.h"
 
 static void target_info (char *, int);
 
@@ -2942,9 +2943,6 @@ target_fileio_close_cleanup (void *opaque)
   target_fileio_close (fd, &target_errno);
 }
 
-typedef int (read_alloc_pread_ftype) (int handle, gdb_byte *read_buf, int len,
-				      ULONGEST offset, int *target_errno);
-
 static read_alloc_pread_ftype target_fileio_read_alloc_1_pread;
 
 /* Helper for target_fileio_read_alloc_1 to make it interruptible.  */
@@ -2958,6 +2956,8 @@ target_fileio_read_alloc_1_pread (int handle, gdb_byte *read_buf, int len,
   return target_fileio_pread (handle, read_buf, len, offset, target_errno);
 }
 
+static read_stralloc_func_ftype target_fileio_read_alloc_1;
+
 /* Read target file FILENAME.  Store the result in *BUF_P and
    return the size of the transferred data.  PADDING additional bytes are
    available in *BUF_P.  This is a helper function for
@@ -2965,58 +2965,6 @@ target_fileio_read_alloc_1_pread (int handle, gdb_byte *read_buf, int len,
    information.  */
 
 static LONGEST
-read_alloc (gdb_byte **buf_p, int handle, read_alloc_pread_ftype *pread_func,
-	    int padding)
-{
-  size_t buf_alloc, buf_pos;
-  gdb_byte *buf;
-  LONGEST n;
-  int target_errno;
-
-  /* Start by reading up to 4K at a time.  The target will throttle
-     this number down if necessary.  */
-  buf_alloc = 4096;
-  buf = xmalloc (buf_alloc);
-  buf_pos = 0;
-  while (1)
-    {
-      n = pread_func (handle, &buf[buf_pos], buf_alloc - buf_pos - padding,
-		      buf_pos, &target_errno);
-      if (n <= 0)
-	{
-	  if (n < 0 || (n == 0 && buf_pos == 0))
-	    xfree (buf);
-	  else
-	    *buf_p = buf;
-	  if (n < 0)
-	    {
-	      /* An error occurred.  */
-	      return -1;
-	    }
-	  else
-	    {
-	      /* Read all there was.  */
-	      return buf_pos;
-	    }
-	}
-
-      buf_pos += n;
-
-      /* If the buffer is filling up, expand it.  */
-      if (buf_alloc < buf_pos * 2)
-	{
-	  buf_alloc *= 2;
-	  buf = xrealloc (buf, buf_alloc);
-	}
-    }
-}
-
-typedef LONGEST (read_stralloc_func_ftype) (const char *filename,
-					    gdb_byte **buf_p, int padding);
-
-static read_stralloc_func_ftype target_fileio_read_alloc_1;
-
-static LONGEST
 target_fileio_read_alloc_1 (const char *filename,
 			    gdb_byte **buf_p, int padding)
 {
@@ -3050,37 +2998,6 @@ target_fileio_read_alloc (const char *filename, gdb_byte **buf_p)
    are returned as allocated but empty strings.  A warning is issued
    if the result contains any embedded NUL bytes.  */
 
-static char *
-read_stralloc (const char *filename, read_stralloc_func_ftype *func)
-{
-  gdb_byte *buffer;
-  char *bufstr;
-  LONGEST i, transferred;
-
-  transferred = func (filename, &buffer, 1);
-  bufstr = (char *) buffer;
-
-  if (transferred < 0)
-    return NULL;
-
-  if (transferred == 0)
-    return xstrdup ("");
-
-  bufstr[transferred] = 0;
-
-  /* Check for embedded NUL bytes; but allow trailing NULs.  */
-  for (i = strlen (bufstr); i < transferred; i++)
-    if (bufstr[i] != 0)
-      {
-	warning (_("target file %s "
-		   "contained unexpected null characters"),
-		 filename);
-	break;
-      }
-
-  return bufstr;
-}
-
 char *
 target_fileio_read_stralloc (const char *filename)
 {

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

* [PATCH v6 10/10] Tests for validate symbol file using build-id
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
                   ` (4 preceding siblings ...)
  2015-06-07 20:05 ` [PATCH v6 06/10] Prepare linux_find_memory_regions_full & co. for move Jan Kratochvil
@ 2015-06-07 20:05 ` Jan Kratochvil
  2015-06-07 20:05 ` [PATCH v6 07/10] Move linux_find_memory_regions_full & co Jan Kratochvil
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:05 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

new testcase.

There was no explicit approval by Tom Tromey in the series a year ago.


Jan


gdb/testsuite/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com

	Tests for validate symbol file using build-id.
	* gdb.base/solib-mismatch-lib.c: New file.
	* gdb.base/solib-mismatch-libmod.c: New file.
	* gdb.base/solib-mismatch.c: New file.
	* gdb.base/solib-mismatch.exp: New file.
---
 gdb/testsuite/gdb.base/solib-mismatch-lib.c    |   29 ++++
 gdb/testsuite/gdb.base/solib-mismatch-libmod.c |   29 ++++
 gdb/testsuite/gdb.base/solib-mismatch.c        |   56 +++++++++
 gdb/testsuite/gdb.base/solib-mismatch.exp      |  156 ++++++++++++++++++++++++
 4 files changed, 270 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/solib-mismatch-lib.c
 create mode 100644 gdb/testsuite/gdb.base/solib-mismatch-libmod.c
 create mode 100644 gdb/testsuite/gdb.base/solib-mismatch.c
 create mode 100644 gdb/testsuite/gdb.base/solib-mismatch.exp

diff --git a/gdb/testsuite/gdb.base/solib-mismatch-lib.c b/gdb/testsuite/gdb.base/solib-mismatch-lib.c
new file mode 100644
index 0000000..b22863a
--- /dev/null
+++ b/gdb/testsuite/gdb.base/solib-mismatch-lib.c
@@ -0,0 +1,29 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2015 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/>.  */
+
+
+int _bar = 42;
+
+int bar(void)
+{
+  return _bar + 21;
+}
+
+int foo(void)
+{
+  return _bar;
+}
diff --git a/gdb/testsuite/gdb.base/solib-mismatch-libmod.c b/gdb/testsuite/gdb.base/solib-mismatch-libmod.c
new file mode 100644
index 0000000..9c3f37e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/solib-mismatch-libmod.c
@@ -0,0 +1,29 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2015 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/>.  */
+
+
+int _bar = 21;
+
+int bar(void)
+{
+  return 42 - _bar;
+}
+
+int foo(void)
+{
+  return 24 + bar();
+}
diff --git a/gdb/testsuite/gdb.base/solib-mismatch.c b/gdb/testsuite/gdb.base/solib-mismatch.c
new file mode 100644
index 0000000..0979c81
--- /dev/null
+++ b/gdb/testsuite/gdb.base/solib-mismatch.c
@@ -0,0 +1,56 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2015 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/>.  */
+
+
+#include <dlfcn.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <signal.h>
+#include <string.h>
+
+/* The following defines must correspond to solib-mismatch.exp .  */
+
+/* DIRNAME and LIB must be defined at compile time.  */
+#ifndef DIRNAME
+#error DIRNAME not defined
+#endif
+#ifndef LIB
+#error LIB not defined
+#endif
+
+int main (int argc, char *argv[])
+{
+  void *h;
+  int (*foo) (void);
+
+  if (chdir (DIRNAME) != 0)
+    {
+      printf ("ERROR - Could not cd to %s\n", DIRNAME);
+      return 1;
+    }
+
+  h = dlopen (LIB, RTLD_NOW);
+
+  if (h == NULL)
+    {
+      printf ("ERROR - could not open lib %s\n", LIB);
+      return 1;
+    }
+  foo = dlsym (h, "foo"); /* set breakpoint 1 here */
+  dlclose (h);
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/solib-mismatch.exp b/gdb/testsuite/gdb.base/solib-mismatch.exp
new file mode 100644
index 0000000..4983429
--- /dev/null
+++ b/gdb/testsuite/gdb.base/solib-mismatch.exp
@@ -0,0 +1,156 @@
+# Copyright 2015 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/>.  */
+
+standard_testfile
+set executable $testfile
+
+if ![is_remote target] {
+  untested "only gdbserver supports build-id reporting"
+  return -1
+}
+if [is_remote host] {
+  untested "only local host is currently supported"
+  return -1
+}
+
+# Test overview:
+#  generate two shared objects. One that will be used by the process
+#  and another, modified, that will be found by gdb. Gdb should
+#  detect the mismatch and refuse to use mismatched shared object.
+
+if { [get_compiler_info] } {
+  untested "get_compiler_info failed."
+  return -1
+}
+
+# First version of the object, to be loaded by ld.
+set srclibfilerun ${testfile}-lib.c
+
+# Modified version of the object to be loaded by gdb
+# Code in -libmod.c is tuned so it gives a mismatch but
+# leaves .dynamic at the same point.
+set srclibfilegdb ${testfile}-libmod.c
+
+# So file name:
+set binlibfilebase lib${testfile}.so
+
+# Setup run directory (where program is run from)
+#   It contains executable and '-lib' version of the library.
+set binlibfiledirrun [standard_output_file ${testfile}_wd]
+set binlibfilerun ${binlibfiledirrun}/${binlibfilebase}
+
+# Second solib version is in current directory, '-libmod' version.
+set binlibfiledirgdb [standard_output_file ""]
+set binlibfilegdb ${binlibfiledirgdb}/${binlibfilebase}
+
+# Executable
+set srcfile ${testfile}.c
+set executable ${testfile}
+
+file delete -force -- "${binlibfiledirrun}"
+file mkdir "${binlibfiledirrun}"
+
+set exec_opts {}
+
+if { ![istarget "*-*-nto-*"] } {
+  lappend exec_opts "shlib_load"
+}
+
+lappend exec_opts "additional_flags=-DDIRNAME\=\"${binlibfiledirrun}\" -DLIB\=\"./${binlibfilebase}\""
+lappend exec_opts "debug"
+
+if { [build_executable $testfile.exp $executable $srcfile $exec_opts] != 0 } {
+  return -1
+}
+
+if { [gdb_compile_shlib "${srcdir}/${subdir}/${srclibfilerun}" "${binlibfilerun}" [list debug ldflags=-Wl,--build-id]] != ""
+     || [gdb_gnu_strip_debug "${binlibfilerun}"]
+     || [gdb_compile_shlib "${srcdir}/${subdir}/${srclibfilegdb}" "${binlibfilegdb}" [list debug ldflags=-Wl,--build-id]] != "" } {
+  untested "compilation failed."
+  return -1
+}
+
+proc solib_matching_test { solibfile symsloaded } {
+  global gdb_prompt
+  global testfile
+  global executable
+  global srcdir
+  global subdir
+  global binlibfiledirrun
+  global binlibfiledirgdb
+  global srcfile
+
+  clean_restart ${binlibfiledirrun}/${executable}
+
+  gdb_test_no_output "set solib-search-path \"${binlibfiledirgdb}\"" ""
+  if { [gdb_test "cd ${binlibfiledirgdb}" "" ""] != 0 } {
+    untested "cd ${binlibfiledirgdb}"
+    return -1
+  }
+
+  # Do not auto load shared libraries, the test needs to have control
+  # over when the relevant output gets printed.
+  gdb_test_no_output "set auto-solib-add off" ""
+
+  if ![runto "${srcfile}:[gdb_get_line_number "set breakpoint 1 here"]"] {
+    return -1
+  }
+
+  gdb_test "sharedlibrary" "" ""
+
+  set nocrlf "\[^\r\n\]*"
+  set expected_header "From${nocrlf}To${nocrlf}Syms${nocrlf}Read${nocrlf}Shared${nocrlf}"
+  set expected_line "${symsloaded}${nocrlf}${solibfile}"
+
+  gdb_test "info sharedlibrary ${solibfile}" \
+    "${expected_header}\r\n.*${expected_line}.*" \
+    "Symbols for ${solibfile} loaded: expected '${symsloaded}'"
+
+  return 0
+}
+
+# Copy binary to working dir so it pulls in the library from that dir
+# (by the virtue of $ORIGIN).
+file copy -force "${binlibfiledirgdb}/${executable}" \
+		 "${binlibfiledirrun}/${executable}"
+
+# Test unstripped, .dynamic matching
+with_test_prefix "test unstripped, .dynamic matching" {
+  solib_matching_test "${binlibfilebase}" "No"
+}
+
+# Keep original so for debugging purposes
+file copy -force "${binlibfilegdb}" "${binlibfilegdb}-orig"
+set objcopy_program [transform objcopy]
+set result [catch "exec $objcopy_program --only-keep-debug ${binlibfilegdb}"]
+if {$result != 0} {
+  untested "test --only-keep-debug (objcopy)"
+}
+
+# Test --only-keep-debug, .dynamic matching so
+with_test_prefix "test --only-keep-debug" {
+  solib_matching_test "${binlibfilebase}" "No"
+}
+
+# Keep previous so for debugging puroses
+file copy -force "${binlibfilegdb}" "${binlibfilegdb}-orig1"
+
+# Copy loaded so over the one gdb will find
+file copy -force "${binlibfilerun}" "${binlibfilegdb}"
+
+# Now test it does not mis-invalidate matching libraries
+with_test_prefix "test matching libraries" {
+  solib_matching_test "${binlibfilebase}" "Yes"
+}

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

* [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
                   ` (8 preceding siblings ...)
  2015-06-07 20:05 ` [PATCH v6 09/10] Validate symbol file using build-id Jan Kratochvil
@ 2015-06-07 20:05 ` Jan Kratochvil
  2015-06-08  8:37   ` Gary Benson
  9 siblings, 1 reply; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:05 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

prepare new files for later move.

Approved by:
	https://sourceware.org/ml/gdb-patches/2014-05/msg00367.html


Jan


gdb/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Create empty common/linux-maps.[ch] and common/target-utils.[ch].
	* Makefile.in (HFILES_NO_SRCDIR); Add common/linux-maps.h,
	common/target-utils.h.
	(COMMON_OBS): Add target-utils.o.
	(linux-maps.o, target-utils.o): New.
	* common/linux-maps.c: New file.
	* common/linux-maps.h: New file.
	* common/target-utils.c: New file.
	* common/target-utils.h: New file.
	* config/i386/linux.mh (NATDEPFILES): Add linux-maps.o.
	* config/i386/linux64.mh (NATDEPFILES): Ditto.

gdb/gdbserver/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Create empty common/linux-maps.[ch] and common/target-utils.[ch].
	* Makefile.in (OBS): Add target-utils.o.
	(linux-maps.o, target-utils.o): New.
	* configure.srv (srv_linux_obj): Add linux-maps.o.
---
 gdb/Makefile.in             |   14 +++++++++++---
 gdb/common/linux-maps.c     |   25 +++++++++++++++++++++++++
 gdb/common/linux-maps.h     |   22 ++++++++++++++++++++++
 gdb/common/target-utils.c   |   26 ++++++++++++++++++++++++++
 gdb/common/target-utils.h   |   23 +++++++++++++++++++++++
 gdb/config/i386/linux.mh    |    2 +-
 gdb/config/i386/linux64.mh  |    2 +-
 gdb/gdbserver/Makefile.in   |    8 +++++++-
 gdb/gdbserver/configure.srv |    2 +-
 9 files changed, 117 insertions(+), 7 deletions(-)
 create mode 100644 gdb/common/linux-maps.c
 create mode 100644 gdb/common/linux-maps.h
 create mode 100644 gdb/common/target-utils.c
 create mode 100644 gdb/common/target-utils.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 95104ef..3b1d1d8 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -900,7 +900,7 @@ LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
 # right, it is probably easiest just to list .h files here directly.
 
 HFILES_NO_SRCDIR = \
-common/gdb_signals.h nat/gdb_thread_db.h common/gdb_vecs.h \
+common/gdb_signals.h nat/gdb_thread_db.h common/gdb_vecs.h common/linux-maps.h \
 common/x86-xstate.h nat/linux-ptrace.h nat/mips-linux-watch.h \
 proc-utils.h aarch64-tdep.h arm-tdep.h ax-gdb.h ppcfbsd-tdep.h \
 ppcnbsd-tdep.h cli-out.h gdb_expat.h breakpoint.h infcall.h obsd-tdep.h \
@@ -979,7 +979,7 @@ i386-linux-nat.h common/common-defs.h common/errors.h common/common-types.h \
 common/common-debug.h common/cleanups.h common/gdb_setjmp.h \
 common/common-exceptions.h target/target.h common/symbol.h \
 common/common-regcache.h fbsd-tdep.h nat/linux-personality.h \
-common/fileio.h nat/x86-linux.h nat/x86-linux-dregs.h
+common/fileio.h nat/x86-linux.h nat/x86-linux-dregs.h common/target-utils.h
 
 # Header files that already have srcdir in them, or which are in objdir.
 
@@ -1079,7 +1079,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	common-utils.o buffer.o ptid.o gdb-dlfcn.o common-agent.o \
 	format.o registry.o btrace.o record-btrace.o waitstatus.o \
 	print-utils.o rsp-low.o errors.o common-debug.o debug.o \
-	common-exceptions.o btrace-common.o fileio.o \
+	common-exceptions.o btrace-common.o fileio.o target-utils.o \
 	$(SUBDIR_GCC_COMPILE_OBS)
 
 TSOBS = inflow.o
@@ -2217,6 +2217,10 @@ common-agent.o: $(srcdir)/common/agent.c
 	$(COMPILE) $(srcdir)/common/agent.c
 	$(POSTCOMPILE)
 
+linux-maps.o: ${srcdir}/common/linux-maps.c
+	$(COMPILE) $(srcdir)/common/linux-maps.c
+	$(POSTCOMPILE)
+
 vec.o: ${srcdir}/common/vec.c
 	$(COMPILE) $(srcdir)/common/vec.c
 	$(POSTCOMPILE)
@@ -2233,6 +2237,10 @@ errors.o: ${srcdir}/common/errors.c
 	$(COMPILE) $(srcdir)/common/errors.c
 	$(POSTCOMPILE)
 
+target-utils.o: ${srcdir}/common/target-utils.c
+	$(COMPILE) $(srcdir)/common/target-utils.c
+	$(POSTCOMPILE)
+
 common-debug.o: ${srcdir}/common/common-debug.c
 	$(COMPILE) $(srcdir)/common/common-debug.c
 	$(POSTCOMPILE)
diff --git a/gdb/common/linux-maps.c b/gdb/common/linux-maps.c
new file mode 100644
index 0000000..bb3eac9
--- /dev/null
+++ b/gdb/common/linux-maps.c
@@ -0,0 +1,25 @@
+/* Linux-specific memory maps manipulation routines.
+   Copyright (C) 2014 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/>.  */
+
+#ifdef GDBSERVER
+#include "server.h"
+#else
+#include "defs.h"
+#endif
+
+#include "linux-maps.h"
diff --git a/gdb/common/linux-maps.h b/gdb/common/linux-maps.h
new file mode 100644
index 0000000..ebf6f37
--- /dev/null
+++ b/gdb/common/linux-maps.h
@@ -0,0 +1,22 @@
+/* Linux-specific memory maps manipulation routines.
+   Copyright (C) 2014 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/>.  */
+
+#ifndef COMMON_LINUX_MAPS_H
+#define COMMON_LINUX_MAPS_H
+
+#endif /* COMMON_LINUX_MAPS_H */
diff --git a/gdb/common/target-utils.c b/gdb/common/target-utils.c
new file mode 100644
index 0000000..308996d
--- /dev/null
+++ b/gdb/common/target-utils.c
@@ -0,0 +1,26 @@
+/* Utility target functions for GDB, and GDBserver.
+
+   Copyright (C) 2014 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/>.  */
+
+#ifdef GDBSERVER
+#include "server.h"
+#else
+#include "defs.h"
+#endif
+
+#include "target-utils.h"
diff --git a/gdb/common/target-utils.h b/gdb/common/target-utils.h
new file mode 100644
index 0000000..db33ec8
--- /dev/null
+++ b/gdb/common/target-utils.h
@@ -0,0 +1,23 @@
+/* Utility target functions for GDB, and GDBserver.
+
+   Copyright (C) 2014 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/>.  */
+
+#ifndef COMMON_TARGET_UTILS_H
+#define COMMON_TARGET_UTILS_H
+
+#endif /* COMMON_TARGET_UTILS_H */
diff --git a/gdb/config/i386/linux.mh b/gdb/config/i386/linux.mh
index 9be2c5f..f650005 100644
--- a/gdb/config/i386/linux.mh
+++ b/gdb/config/i386/linux.mh
@@ -3,7 +3,7 @@
 NAT_FILE= config/nm-linux.h
 NATDEPFILES= inf-ptrace.o fork-child.o \
 	x86-nat.o x86-dregs.o i386-linux-nat.o x86-linux-nat.o \
-	proc-service.o linux-thread-db.o \
+	proc-service.o linux-thread-db.o linux-maps.o \
 	linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \
 	linux-btrace.o linux-waitpid.o linux-personality.o x86-linux.o \
 	x86-linux-dregs.o
diff --git a/gdb/config/i386/linux64.mh b/gdb/config/i386/linux64.mh
index 224a2a9..b71bda0 100644
--- a/gdb/config/i386/linux64.mh
+++ b/gdb/config/i386/linux64.mh
@@ -2,7 +2,7 @@
 NATDEPFILES= inf-ptrace.o fork-child.o \
 	x86-nat.o x86-dregs.o amd64-nat.o amd64-linux-nat.o \
 	x86-linux-nat.o \
-	linux-nat.o linux-osdata.o \
+	linux-maps.o linux-nat.o linux-osdata.o \
 	proc-service.o linux-thread-db.o linux-fork.o \
 	linux-procfs.o linux-ptrace.o linux-btrace.o \
 	linux-waitpid.o linux-personality.o x86-linux.o \
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index a981ee8..b4465e6 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -194,7 +194,7 @@ OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o \
       mem-break.o hostio.o event-loop.o tracepoint.o xml-utils.o \
       common-utils.o ptid.o buffer.o format.o filestuff.o dll.o notif.o \
       tdesc.o print-utils.o rsp-low.o errors.o common-debug.o cleanups.o \
-      common-exceptions.o symbol.o btrace-common.o fileio.o \
+      common-exceptions.o symbol.o btrace-common.o fileio.o target-utils.o \
       $(XML_BUILTIN) $(DEPFILES) $(LIBOBJS)
 GDBREPLAY_OBS = gdbreplay.o version.o
 GDBSERVER_LIBS = @GDBSERVER_LIBS@
@@ -519,6 +519,9 @@ ax.o: ax.c
 signals.o: ../common/signals.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+linux-maps.o: ../common/linux-maps.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 print-utils.o: ../common/print-utils.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
@@ -531,6 +534,9 @@ common-utils.o: ../common/common-utils.c
 posix-strerror.o: ../common/posix-strerror.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+target-utils.o: ../common/target-utils.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 mingw-strerror.o: ../common/mingw-strerror.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
diff --git a/gdb/gdbserver/configure.srv b/gdb/gdbserver/configure.srv
index 81dd235..3b3888d 100644
--- a/gdb/gdbserver/configure.srv
+++ b/gdb/gdbserver/configure.srv
@@ -42,7 +42,7 @@ srv_amd64_linux_xmlfiles="i386/amd64-linux.xml i386/amd64-avx-linux.xml i386/amd
 
 # Linux object files.  This is so we don't have to repeat
 # these files over and over again.
-srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o"
+srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-maps.o linux-ptrace.o linux-waitpid.o linux-personality.o"
 
 # Input is taken from the "${target}" variable.
 

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

* [PATCH v6 08/10] gdbserver build-id attribute generator
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
                   ` (6 preceding siblings ...)
  2015-06-07 20:05 ` [PATCH v6 07/10] Move linux_find_memory_regions_full & co Jan Kratochvil
@ 2015-06-07 20:05 ` Jan Kratochvil
  2015-06-07 20:05 ` [PATCH v6 09/10] Validate symbol file using build-id Jan Kratochvil
  2015-06-07 20:05 ` [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch] Jan Kratochvil
  9 siblings, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:05 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

producer part of the new "build-id" XML attribute.

Probably approved by:
	https://sourceware.org/ml/gdb-patches/2014-05/msg00423.html
Although there were minor changes due to trunk updates.


Jan


gdb/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	gdbserver build-id attribute generator.
	* features/library-list-svr4.dtd (library-list-svr4): New
	'build-id' attribute.

gdb/doc/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	gdbserver build-id attribute generator.
	* gdb.texinfo (Library List Format for SVR4 Targets): Add
	'build-id' in description, example, new attribute in dtd.

gdb/gdbserver/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	gdbserver build-id attribute generator.
	* linux-low.c (linux-maps.h, search.h, rsp-low.h): Include.
	(ElfXX_Ehdr, ElfXX_Phdr, ElfXX_Nhdr): New.
	(ELFXX_FLD, ELFXX_SIZEOF, ELFXX_ROUNDUP, BUILD_ID_INVALID): New.
	(find_phdr): New.
	(get_dynamic): Use find_pdhr to traverse program headers.
	(struct mapping_entry, mapping_entry_s, free_mapping_entry_vec)
	(compare_mapping_entry_range, struct find_memory_region_callback_data)
	(read_build_id, find_memory_region_callback, lrfind_mapping_entry)
	(get_hex_build_id): New.
	(linux_qxfer_libraries_svr4): Add optional build-id attribute
	to reply XML document.
---
 gdb/doc/gdb.texinfo                |   17 +-
 gdb/features/library-list-svr4.dtd |   13 +
 gdb/gdbserver/linux-low.c          |  398 +++++++++++++++++++++++++++++++++---
 3 files changed, 379 insertions(+), 49 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 9ea846a..2adf5f5 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -39275,6 +39275,8 @@ memory address.  It is a displacement of absolute memory address against
 address the file was prelinked to during the library load.
 @item
 @code{l_ld}, which is memory address of the @code{PT_DYNAMIC} segment
+@item
+@code{build-id}, hex encoded @code{NT_GNU_BUILD_ID} note, if it exists.
 @end itemize
 
 Additionally the single @code{main-lm} attribute specifies address of
@@ -39292,7 +39294,7 @@ looks like this:
   <library name="/lib/ld-linux.so.2" lm="0xe4f51c" l_addr="0xe2d000"
            l_ld="0xe4eefc"/>
   <library name="/lib/libc.so.6" lm="0xe4fbe8" l_addr="0x154000"
-           l_ld="0x152350"/>
+           l_ld="0x152350" build-id="9afccf7cc41e6293476223fe72480854"/>
 </library-list-svr>
 @end smallexample
 
@@ -39301,13 +39303,14 @@ The format of an SVR4 library list is described by this DTD:
 @smallexample
 <!-- library-list-svr4: Root element with versioning -->
 <!ELEMENT library-list-svr4  (library)*>
-<!ATTLIST library-list-svr4  version CDATA   #FIXED  "1.0">
-<!ATTLIST library-list-svr4  main-lm CDATA   #IMPLIED>
+<!ATTLIST library-list-svr4  version  CDATA   #FIXED  "1.0">
+<!ATTLIST library-list-svr4  main-lm  CDATA   #IMPLIED>
 <!ELEMENT library            EMPTY>
-<!ATTLIST library            name    CDATA   #REQUIRED>
-<!ATTLIST library            lm      CDATA   #REQUIRED>
-<!ATTLIST library            l_addr  CDATA   #REQUIRED>
-<!ATTLIST library            l_ld    CDATA   #REQUIRED>
+<!ATTLIST library            name     CDATA   #REQUIRED>
+<!ATTLIST library            lm       CDATA   #REQUIRED>
+<!ATTLIST library            l_addr   CDATA   #REQUIRED>
+<!ATTLIST library            l_ld     CDATA   #REQUIRED>
+<!ATTLIST library            build-id CDATA   #IMPLIED>
 @end smallexample
 
 @node Memory Map Format
diff --git a/gdb/features/library-list-svr4.dtd b/gdb/features/library-list-svr4.dtd
index 082945d..5741f03 100644
--- a/gdb/features/library-list-svr4.dtd
+++ b/gdb/features/library-list-svr4.dtd
@@ -6,11 +6,12 @@
 
 <!-- library-list-svr4: Root element with versioning -->
 <!ELEMENT library-list-svr4  (library)*>
-<!ATTLIST library-list-svr4  version CDATA   #FIXED  "1.0">
-<!ATTLIST library-list-svr4  main-lm CDATA   #IMPLIED>
+<!ATTLIST library-list-svr4  version  CDATA   #FIXED  "1.0">
+<!ATTLIST library-list-svr4  main-lm  CDATA   #IMPLIED>
 
 <!ELEMENT library            EMPTY>
-<!ATTLIST library            name    CDATA   #REQUIRED>
-<!ATTLIST library            lm      CDATA   #REQUIRED>
-<!ATTLIST library            l_addr  CDATA   #REQUIRED>
-<!ATTLIST library            l_ld    CDATA   #REQUIRED>
+<!ATTLIST library            name     CDATA   #REQUIRED>
+<!ATTLIST library            lm       CDATA   #REQUIRED>
+<!ATTLIST library            l_addr   CDATA   #REQUIRED>
+<!ATTLIST library            l_ld     CDATA   #REQUIRED>
+<!ATTLIST library            build-id CDATA   #IMPLIED>
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index d763c66..8c327ef 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -21,6 +21,7 @@
 #include "nat/linux-osdata.h"
 #include "agent.h"
 #include "tdesc.h"
+#include "linux-maps.h"
 
 #include "nat/linux-nat.h"
 #include "nat/linux-waitpid.h"
@@ -42,9 +43,11 @@
 #include <sys/stat.h>
 #include <sys/vfs.h>
 #include <sys/uio.h>
+#include <search.h>
 #include "filestuff.h"
 #include "tracepoint.h"
 #include "hostio.h"
+#include "rsp-low.h"
 #ifndef ELFMAG0
 /* Don't include <linux/elf.h> here.  If it got included by gdb_proc_service.h
    then ELFMAG0 will have been defined.  If it didn't get included by
@@ -180,6 +183,31 @@ lwp_stop_reason (struct lwp_info *lwp)
   return lwp->stop_reason;
 }
 
+typedef union ElfXX_Ehdr
+{
+  Elf32_Ehdr _32;
+  Elf64_Ehdr _64;
+} ElfXX_Ehdr;
+
+typedef union ElfXX_Phdr
+{
+  Elf32_Phdr _32;
+  Elf64_Phdr _64;
+} ElfXX_Phdr;
+
+typedef union ElfXX_Nhdr
+{
+  Elf32_Nhdr _32;
+  Elf64_Nhdr _64;
+} ElfXX_Nhdr;
+
+#define ELFXX_FLD(elf64, hdr, fld) ((elf64) ? (hdr)._64.fld : (hdr)._32.fld)
+#define ELFXX_SIZEOF(elf64, hdr) ((elf64) ? sizeof ((hdr)._64) \
+					  : sizeof ((hdr)._32))
+/* Round up to next 4 byte boundary.  */
+#define ELFXX_ROUNDUP_4(elf64, what) (((what) + 3) & ~(ULONGEST) 3)
+#define BUILD_ID_INVALID "?"
+
 /* A list of all unknown processes which receive stop signals.  Some
    other process will presumably claim each of these as forked
    children momentarily.  */
@@ -6005,15 +6033,38 @@ get_phdr_phnum_from_proc_auxv (const int pid, const int is_elf64,
   return 0;
 }
 
+/* Linearly traverse pheaders and look for P_TYPE pheader.  */
+
+static const void *
+find_phdr (const int is_elf64, const void *const phdr_begin,
+	   const void *const phdr_end, const ULONGEST p_type)
+{
+#define PHDR_NEXT(hdrp) ((const void *) ((const gdb_byte *) (hdrp) + \
+			 ELFXX_SIZEOF (is_elf64, *hdrp)))
+
+  const ElfXX_Phdr *phdr = phdr_begin;
+
+  while (PHDR_NEXT (phdr) <= phdr_end)
+    {
+      if (ELFXX_FLD (is_elf64, *phdr, p_type) == p_type)
+	return phdr;
+      phdr = PHDR_NEXT (phdr);
+    }
+
+  return NULL;
+#undef PHDR_NEXT
+}
+
 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present.  */
 
 static CORE_ADDR
 get_dynamic (const int pid, const int is_elf64)
 {
   CORE_ADDR phdr_memaddr, relocation;
-  int num_phdr, i;
+  int num_phdr;
   unsigned char *phdr_buf;
-  const int phdr_size = is_elf64 ? sizeof (Elf64_Phdr) : sizeof (Elf32_Phdr);
+  const ElfXX_Phdr *phdr;
+  const int phdr_size = ELFXX_SIZEOF (is_elf64, *phdr);
 
   if (get_phdr_phnum_from_proc_auxv (pid, is_elf64, &phdr_memaddr, &num_phdr))
     return 0;
@@ -6027,22 +6078,10 @@ get_dynamic (const int pid, const int is_elf64)
   /* Compute relocation: it is expected to be 0 for "regular" executables,
      non-zero for PIE ones.  */
   relocation = -1;
-  for (i = 0; relocation == -1 && i < num_phdr; i++)
-    if (is_elf64)
-      {
-	Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
-
-	if (p->p_type == PT_PHDR)
-	  relocation = phdr_memaddr - p->p_vaddr;
-      }
-    else
-      {
-	Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
-
-	if (p->p_type == PT_PHDR)
-	  relocation = phdr_memaddr - p->p_vaddr;
-      }
-
+  phdr = find_phdr (is_elf64, phdr_buf, phdr_buf + num_phdr * phdr_size,
+		    PT_PHDR);
+  if (phdr != NULL)
+    relocation = phdr_memaddr - ELFXX_FLD (is_elf64, *phdr, p_vaddr);
   if (relocation == -1)
     {
       /* PT_PHDR is optional, but necessary for PIE in general.  Fortunately
@@ -6058,23 +6097,11 @@ get_dynamic (const int pid, const int is_elf64)
       return 0;
     }
 
-  for (i = 0; i < num_phdr; i++)
-    {
-      if (is_elf64)
-	{
-	  Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
+  phdr = find_phdr (is_elf64, phdr_buf, phdr_buf + num_phdr * phdr_size,
+		    PT_DYNAMIC);
 
-	  if (p->p_type == PT_DYNAMIC)
-	    return p->p_vaddr + relocation;
-	}
-      else
-	{
-	  Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
-
-	  if (p->p_type == PT_DYNAMIC)
-	    return p->p_vaddr + relocation;
-	}
-    }
+  if (phdr != NULL)
+    return ELFXX_FLD (is_elf64, *phdr, p_vaddr) + relocation;
 
   return 0;
 }
@@ -6214,6 +6241,278 @@ struct link_map_offsets
     int l_prev_offset;
   };
 
+
+/* Structure for holding a mapping.  Only mapping
+   containing l_ld can have hex_build_id set.  */
+
+struct mapping_entry
+{
+  /* Fields are populated from linux_find_memory_region parameters.  */
+
+  ULONGEST vaddr;
+  ULONGEST size;
+  ULONGEST offset;
+  ULONGEST inode;
+
+  /* Hex encoded string allocated using xmalloc, and
+     needs to be freed.  It can be NULL.  */
+
+  char *hex_build_id;
+};
+
+typedef struct mapping_entry mapping_entry_s;
+
+DEF_VEC_O(mapping_entry_s);
+
+/* Free vector of mapping_entry_s objects.  */
+
+static void
+free_mapping_entry_vec (VEC (mapping_entry_s) *lst)
+{
+  int ix;
+  mapping_entry_s *p;
+
+  for (ix = 0; VEC_iterate (mapping_entry_s, lst, ix, p); ++ix)
+    xfree (p->hex_build_id);
+
+  VEC_free (mapping_entry_s, lst);
+}
+
+/* Used for finding a mapping containing the given
+   l_ld passed in K.  */
+
+static int
+compare_mapping_entry_range (const void *const k, const void *const b)
+{
+  const ULONGEST key = *(const CORE_ADDR *) k;
+  const mapping_entry_s *const p = b;
+
+  if (key < p->vaddr)
+    return -1;
+
+  if (key < p->vaddr + p->size)
+    return 0;
+
+  return 1;
+}
+
+struct find_memory_region_callback_data
+{
+  unsigned is_elf64;
+
+  /* Return.  Must be freed with free_mapping_entry_vec.  */
+  VEC (mapping_entry_s) *list;
+};
+
+/* Read build-id from PT_NOTE.
+   Argument LOAD_ADDR represents run time virtual address corresponding to
+   the beginning of the first loadable segment.  L_ADDR is displacement
+   as supplied by the dynamic linker.  */
+
+static void
+read_build_id (struct find_memory_region_callback_data *const p,
+	       mapping_entry_s *const bil, const CORE_ADDR load_addr,
+	       const CORE_ADDR l_addr)
+{
+  const int is_elf64 = p->is_elf64;
+  ElfXX_Ehdr ehdr;
+
+  if (linux_read_memory (load_addr, (unsigned char *) &ehdr,
+			 ELFXX_SIZEOF (is_elf64, ehdr)) == 0
+      && ELFXX_FLD (is_elf64, ehdr, e_ident[EI_MAG0]) == ELFMAG0
+      && ELFXX_FLD (is_elf64, ehdr, e_ident[EI_MAG1]) == ELFMAG1
+      && ELFXX_FLD (is_elf64, ehdr, e_ident[EI_MAG2]) == ELFMAG2
+      && ELFXX_FLD (is_elf64, ehdr, e_ident[EI_MAG3]) == ELFMAG3)
+    {
+      const ElfXX_Phdr *phdr;
+      void *phdr_buf;
+      const unsigned e_phentsize = ELFXX_FLD (is_elf64, ehdr, e_phentsize);
+
+      if (ELFXX_FLD (is_elf64, ehdr, e_phnum) >= 100
+	  || e_phentsize != ELFXX_SIZEOF (is_elf64, *phdr))
+	{
+	  /* Basic sanity check failed.  */
+	  warning (_("Could not identify program header at %s."),
+		   paddress (load_addr));
+	  return;
+	}
+
+      phdr_buf = alloca (ELFXX_FLD (is_elf64, ehdr, e_phnum) * e_phentsize);
+
+      if (linux_read_memory (load_addr + ELFXX_FLD (is_elf64, ehdr, e_phoff),
+			     phdr_buf,
+			     ELFXX_FLD (is_elf64, ehdr, e_phnum) * e_phentsize)
+	  != 0)
+	{
+	  warning (_("Could not read program header at %s."),
+		   paddress (load_addr));
+	  return;
+	}
+
+      phdr = phdr_buf;
+
+      for (;;)
+	{
+	  gdb_byte *pt_note;
+	  const gdb_byte *pt_end;
+	  const ElfXX_Nhdr *nhdr;
+	  CORE_ADDR note_addr;
+
+	  phdr = find_phdr (p->is_elf64, phdr, (gdb_byte *) phdr_buf
+			    + ELFXX_FLD (is_elf64, ehdr, e_phnum) * e_phentsize,
+			    PT_NOTE);
+	  if (phdr == NULL)
+	    break;
+	  pt_note = xmalloc (ELFXX_FLD (is_elf64, *phdr, p_memsz));
+	  note_addr = ELFXX_FLD (is_elf64, *phdr, p_vaddr) + l_addr;
+	  if (linux_read_memory (note_addr, pt_note,
+				 ELFXX_FLD (is_elf64, *phdr, p_memsz)) != 0)
+	    {
+	      xfree (pt_note);
+	      warning (_("Could not read note at address 0x%s"),
+		       paddress (note_addr));
+	      break;
+	    }
+
+	  pt_end = pt_note + ELFXX_FLD (is_elf64, *phdr, p_memsz);
+	  nhdr = (void *) pt_note;
+	  while ((const gdb_byte *) nhdr < pt_end)
+	    {
+	      const size_t namesz
+		= ELFXX_ROUNDUP_4 (is_elf64, ELFXX_FLD (is_elf64, *nhdr,
+							n_namesz));
+	      const size_t descsz
+		= ELFXX_ROUNDUP_4 (is_elf64, ELFXX_FLD (is_elf64, *nhdr,
+							n_descsz));
+	      const size_t note_sz = (ELFXX_SIZEOF (is_elf64, *nhdr) + namesz
+				      + descsz);
+
+	      if (((const gdb_byte *) nhdr + note_sz) > pt_end || note_sz == 0
+		  || descsz == 0)
+		{
+		  warning (_("Malformed PT_NOTE at address 0x%s\n"),
+			   paddress (note_addr + (gdb_byte *) nhdr - pt_note));
+		  break;
+		}
+	      if (ELFXX_FLD (is_elf64, *nhdr, n_type) == NT_GNU_BUILD_ID
+		  && ELFXX_FLD (is_elf64, *nhdr, n_namesz) == 4)
+		{
+		  const char gnu[4] = "GNU\0";
+		  const char *const pname
+		    = (char *) nhdr + ELFXX_SIZEOF (is_elf64, *nhdr);
+
+		  if (memcmp (pname, gnu, 4) == 0)
+		    {
+		      const size_t n_descsz = ELFXX_FLD (is_elf64, *nhdr,
+							 n_descsz);
+
+		      bil->hex_build_id = xmalloc (n_descsz * 2 + 1);
+		      bin2hex ((const gdb_byte *) pname + namesz,
+			       bil->hex_build_id, n_descsz);
+		      xfree (pt_note);
+		      return;
+		    }
+		}
+	      nhdr = (void *) ((gdb_byte *) nhdr + note_sz);
+	    }
+	  xfree (pt_note);
+	}
+    }
+}
+
+static linux_find_memory_region_ftype find_memory_region_callback;
+
+/* Add mapping_entry.  See linux_find_memory_ftype for the parameters
+   description.  */
+
+static int
+find_memory_region_callback (ULONGEST vaddr, ULONGEST size, ULONGEST offset,
+			     ULONGEST inode, int read, int write, int exec,
+			     int modified, const char *filename, void *data)
+{
+  if (inode != 0)
+    {
+      struct find_memory_region_callback_data *const p = data;
+      mapping_entry_s bil;
+
+      bil.vaddr = vaddr;
+      bil.size = size;
+      bil.offset = offset;
+      bil.inode = inode;
+      bil.hex_build_id = NULL;
+
+      VEC_safe_push (mapping_entry_s, p->list, &bil);
+    }
+
+  /* Continue the traversal.  */
+  return 0;
+}
+
+/* Linear reverse find starting from RBEGIN towards REND looking for
+   the lowest vaddr mapping of the same inode and zero offset.  */
+
+static mapping_entry_s *
+lrfind_mapping_entry (mapping_entry_s *const rbegin,
+		      const mapping_entry_s *const rend)
+{
+  mapping_entry_s *p;
+
+  for (p = rbegin - 1; p >= rend; --p)
+    if (p->offset == 0 && p->inode == rbegin->inode)
+      return p;
+
+  return NULL;
+}
+
+/* Get build-id for the given L_LD, where L_LD corresponds to
+   link_map.l_ld as specified by the dynamic linker.
+   DATA must point to already filled list of mapping_entry elements.
+
+   If build-id had not been read, read it and cache in corresponding
+   list element.
+
+   Return build_id as stored in the list element corresponding
+   to L_LD.
+
+   NULL may be returned if build-id could not be fetched.
+
+   Returned string must not be freed explicitly.  */
+
+static const char *
+get_hex_build_id (const CORE_ADDR l_addr, const CORE_ADDR l_ld,
+		  struct find_memory_region_callback_data *const data)
+{
+  mapping_entry_s *bil;
+
+  bil = bsearch (&l_ld, VEC_address (mapping_entry_s, data->list),
+		 VEC_length (mapping_entry_s, data->list),
+		 sizeof (mapping_entry_s), compare_mapping_entry_range);
+
+  if (bil == NULL)
+    return NULL;
+
+  if (bil->hex_build_id == NULL)
+    {
+      mapping_entry_s *bil_min;
+
+      bil_min = lrfind_mapping_entry (bil, VEC_address (mapping_entry_s,
+							data->list));
+      if (bil_min != NULL)
+	read_build_id (data, bil, bil_min->vaddr, l_addr);
+      else
+	{
+	  /* Do not try to find hex_build_id again.  */
+	  bil->hex_build_id = xstrdup (BUILD_ID_INVALID);
+	  warning (_("Could not determine load address; mapping entry with "
+		     "offset 0 corresponding to l_ld = 0x%s could not be "
+		     "found; build-id can not be used."),
+		   paddress (l_ld));
+	}
+    }
+
+  return bil->hex_build_id;
+}
+
 /* Construct qXfer:libraries-svr4:read reply.  */
 
 static int
@@ -6226,6 +6525,15 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
   struct process_info_private *const priv = current_process ()->priv;
   char filename[PATH_MAX];
   int pid, is_elf64;
+  struct find_memory_region_callback_data data;
+  
+  /* COREFILTER_ANON_PRIVATE and COREFILTER_ANON_SHARED do not have an
+     associated file so it is not expected it could have an ELF header.  */ 
+  const enum filterflags filterflags = (COREFILTER_MAPPED_PRIVATE
+					| COREFILTER_MAPPED_SHARED
+					| COREFILTER_ELF_HEADERS
+					| COREFILTER_HUGETLB_PRIVATE
+					| COREFILTER_HUGETLB_SHARED);
 
   static const struct link_map_offsets lmo_32bit_offsets =
     {
@@ -6268,6 +6576,14 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
   lmo = is_elf64 ? &lmo_64bit_offsets : &lmo_32bit_offsets;
   ptr_size = is_elf64 ? 8 : 4;
 
+  data.is_elf64 = is_elf64;
+  data.list = NULL;
+  VEC_reserve (mapping_entry_s, data.list, 16);
+  if (linux_find_memory_regions_full (pid, filterflags,
+				      find_memory_region_callback, &data)
+      < 0)
+    warning (_("Finding memory regions failed"));
+
   while (annex[0] != '\0')
     {
       const char *sep;
@@ -6374,6 +6690,7 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
 	      /* 6x the size for xml_escape_text below.  */
 	      size_t len = 6 * strlen ((char *) libname);
 	      char *name;
+	      const char *hex_enc_build_id = NULL;
 
 	      if (!header_done)
 		{
@@ -6382,7 +6699,11 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
 		  header_done = 1;
 		}
 
-	      while (allocated < p - document + len + 200)
+	      hex_enc_build_id = get_hex_build_id (l_addr, l_ld, &data);
+
+	      while (allocated < (p - document + len + 200
+				  + (hex_enc_build_id != NULL
+				     ? strlen (hex_enc_build_id) : 0)))
 		{
 		  /* Expand to guarantee sufficient storage.  */
 		  uintptr_t document_len = p - document;
@@ -6394,9 +6715,13 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
 
 	      name = xml_escape_text ((char *) libname);
 	      p += sprintf (p, "<library name=\"%s\" lm=\"0x%lx\" "
-			    "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
+			    "l_addr=\"0x%lx\" l_ld=\"0x%lx\"",
 			    name, (unsigned long) lm_addr,
 			    (unsigned long) l_addr, (unsigned long) l_ld);
+	      if (hex_enc_build_id != NULL
+		  && strcmp (hex_enc_build_id, BUILD_ID_INVALID) != 0)
+		p += sprintf (p, " build-id=\"%s\"", hex_enc_build_id);
+	      p += sprintf (p, "/>");
 	      free (name);
 	    }
 	}
@@ -6423,6 +6748,7 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
 
   memcpy (readbuf, document + offset, len);
   xfree (document);
+  free_mapping_entry_vec (data.list);
 
   return len;
 }

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

* [PATCH v6 06/10] Prepare linux_find_memory_regions_full & co. for move
  2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
                   ` (3 preceding siblings ...)
  2015-06-07 20:05 ` [PATCH v6 05/10] Move gdb_regex* to common/ Jan Kratochvil
@ 2015-06-07 20:05 ` Jan Kratochvil
  2015-06-07 20:05 ` [PATCH v6 10/10] Tests for validate symbol file using build-id Jan Kratochvil
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-07 20:05 UTC (permalink / raw)
  To: gdb-patches; +Cc: Aleksandar Ristovski

Hi,

prepare code for move into gdb/common/.

Approved by:
	https://sourceware.org/ml/gdb-patches/2014-05/msg00371.html


Jan


gdb/ChangeLog
2014-02-26  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Prepare linux_find_memory_regions_full & co. for move.
	* linux-tdep.c (linux_find_memory_region_ftype): Comment.
	(linux_find_memory_regions_full): Change signature and prepare
	for moving to linux-maps.
	(linux_find_memory_regions_data): Rename field 'obfd' to 'data'.
	(linux_find_memory_regions_thunk): New.
	(linux_find_memory_regions_thunk): Use 'data' field instead of 'obfd'.
	(linux_find_memory_regions_gdb): New.
	(linux_find_memory_regions): Rename argument 'obfd' to 'func_data'.
	(linux_make_mappings_corefile_notes): Use
	linux_find_memory_regions_gdb.
	* target.c (read_alloc_pread_ftype): New typedef.
	(target_fileio_read_alloc_1_pread): New function.
	(read_alloc): Refactor from target_fileio_read_alloc_1.
	(read_stralloc_func_ftype): New typedef.
	(target_fileio_read_alloc_1): New implementation. Use read_alloc.
	(read_stralloc): Refactored from target_fileio_read_stralloc.
	(target_fileio_read_stralloc): New implementation, use read_stralloc.
---
 gdb/linux-tdep.c |  121 +++++++++++++++++++++++++++++++++---------------------
 gdb/target.c     |   93 ++++++++++++++++++++++++++++--------------
 2 files changed, 138 insertions(+), 76 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index b0981f5..4618415 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1098,6 +1098,10 @@ linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
     error (_("unable to handle request"));
 }
 
+/* Callback function for linux_find_memory_regions_full.  If it returns
+   non-zero linux_find_memory_regions_full returns immediately with that
+   value.  */
+
 typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
 					    ULONGEST offset, ULONGEST inode,
 					    int read, int write,
@@ -1105,42 +1109,22 @@ typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
 					    const char *filename,
 					    void *data);
 
-/* List memory regions in the inferior for a corefile.  */
+/* List memory regions in the inferior PID matched to FILTERFLAGS for
+   a corefile.  Call FUNC with FUNC_DATA for each such region.  Return
+   immediately with the value returned by FUNC if it is non-zero.
+   *MEMORY_TO_FREE_PTR should be registered to be freed automatically if
+   called FUNC throws an exception.  MEMORY_TO_FREE_PTR can be also
+   passed as NULL if it is not used.  Return -1 if error occurs, 0 if
+   all memory regions have been processed or return the value from FUNC
+   if FUNC returns non-zero.  */
 
 static int
-linux_find_memory_regions_full (struct gdbarch *gdbarch,
+linux_find_memory_regions_full (pid_t pid, enum filterflags filterflags,
 				linux_find_memory_region_ftype *func,
-				void *obfd)
+				void *func_data)
 {
   char mapsfilename[100];
-  char coredumpfilter_name[100];
-  char *data, *coredumpfilterdata;
-  pid_t pid;
-  /* Default dump behavior of coredump_filter (0x33), according to
-     Documentation/filesystems/proc.txt from the Linux kernel
-     tree.  */
-  enum filterflags filterflags = (COREFILTER_ANON_PRIVATE
-				  | COREFILTER_ANON_SHARED
-				  | COREFILTER_ELF_HEADERS
-				  | COREFILTER_HUGETLB_PRIVATE);
-
-  /* We need to know the real target PID to access /proc.  */
-  if (current_inferior ()->fake_pid_p)
-    return 1;
-
-  pid = current_inferior ()->pid;
-
-  if (use_coredump_filter)
-    {
-      xsnprintf (coredumpfilter_name, sizeof (coredumpfilter_name),
-		 "/proc/%d/coredump_filter", pid);
-      coredumpfilterdata = target_fileio_read_stralloc (coredumpfilter_name);
-      if (coredumpfilterdata != NULL)
-	{
-	  sscanf (coredumpfilterdata, "%x", &filterflags);
-	  xfree (coredumpfilterdata);
-	}
-    }
+  char *data;
 
   xsnprintf (mapsfilename, sizeof mapsfilename, "/proc/%d/smaps", pid);
   data = target_fileio_read_stralloc (mapsfilename);
@@ -1155,6 +1139,7 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
     {
       struct cleanup *cleanup = make_cleanup (xfree, data);
       char *line, *t;
+      int retval = 0;
 
       line = strtok_r (data, "\n", &t);
       while (line != NULL)
@@ -1269,17 +1254,20 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
 
 	  /* Invoke the callback function to create the corefile segment.  */
 	  if (should_dump_p)
-	    func (addr, endaddr - addr, offset, inode,
-		  read, write, exec, 1, /* MODIFIED is true because we
-					   want to dump the mapping.  */
-		  filename, obfd);
+	    retval = func (addr, endaddr - addr, offset, inode,
+			   read, write, exec,
+			   1, /* MODIFIED is true because we want to dump the
+				 mapping.  */
+			   filename, func_data);
+	  if (retval != 0)
+	    break;
 	}
 
       do_cleanups (cleanup);
-      return 0;
+      return retval;
     }
 
-  return 1;
+  return -1;
 }
 
 /* A structure for passing information through
@@ -1293,9 +1281,11 @@ struct linux_find_memory_regions_data
 
   /* The original datum.  */
 
-  void *obfd;
+  void *data;
 };
 
+static linux_find_memory_region_ftype linux_find_memory_regions_thunk;
+
 /* A callback for linux_find_memory_regions that converts between the
    "full"-style callback and find_memory_region_ftype.  */
 
@@ -1307,7 +1297,47 @@ linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
 {
   struct linux_find_memory_regions_data *data = arg;
 
-  return data->func (vaddr, size, read, write, exec, modified, data->obfd);
+  return data->func (vaddr, size, read, write, exec, modified, data->data);
+}
+
+/* Wrapper of linux_find_memory_regions_full handling FAKE_PID_P in GDB.  */
+
+static int
+linux_find_memory_regions_gdb (struct gdbarch *gdbarch,
+			       linux_find_memory_region_ftype *func,
+			       void *func_data)
+{
+  pid_t pid;
+  /* Default dump behavior of coredump_filter (0x33), according to
+     Documentation/filesystems/proc.txt from the Linux kernel
+     tree.  */
+  enum filterflags filterflags = (COREFILTER_ANON_PRIVATE
+				  | COREFILTER_ANON_SHARED
+				  | COREFILTER_ELF_HEADERS
+				  | COREFILTER_HUGETLB_PRIVATE);
+
+  /* We need to know the real target PID so
+     linux_find_memory_regions_full can access /proc.  */
+  if (current_inferior ()->fake_pid_p)
+    return -1;
+
+  pid = current_inferior ()->pid;
+
+  if (use_coredump_filter)
+    {
+      char coredumpfilter_name[100], *coredumpfilterdata;
+
+      xsnprintf (coredumpfilter_name, sizeof (coredumpfilter_name),
+		 "/proc/%d/coredump_filter", pid);
+      coredumpfilterdata = target_fileio_read_stralloc (coredumpfilter_name);
+      if (coredumpfilterdata != NULL)
+	{
+	  sscanf (coredumpfilterdata, "%x", &filterflags);
+	  xfree (coredumpfilterdata);
+	}
+    }
+
+  return linux_find_memory_regions_full (pid, filterflags, func, func_data);
 }
 
 /* A variant of linux_find_memory_regions_full that is suitable as the
@@ -1315,16 +1345,15 @@ linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
 
 static int
 linux_find_memory_regions (struct gdbarch *gdbarch,
-			   find_memory_region_ftype func, void *obfd)
+			   find_memory_region_ftype func, void *func_data)
 {
   struct linux_find_memory_regions_data data;
 
   data.func = func;
-  data.obfd = obfd;
+  data.data = func_data;
 
-  return linux_find_memory_regions_full (gdbarch,
-					 linux_find_memory_regions_thunk,
-					 &data);
+  return linux_find_memory_regions_gdb (gdbarch,
+					linux_find_memory_regions_thunk, &data);
 }
 
 /* Determine which signal stopped execution.  */
@@ -1506,8 +1535,8 @@ linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
   pack_long (buf, long_type, 1);
   obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
 
-  linux_find_memory_regions_full (gdbarch, linux_make_mappings_callback,
-				  &mapping_data);
+  linux_find_memory_regions_gdb (gdbarch, linux_make_mappings_callback,
+				 &mapping_data);
 
   if (mapping_data.file_count != 0)
     {
diff --git a/gdb/target.c b/gdb/target.c
index 306c21d..d11e7dc 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2942,6 +2942,22 @@ target_fileio_close_cleanup (void *opaque)
   target_fileio_close (fd, &target_errno);
 }
 
+typedef int (read_alloc_pread_ftype) (int handle, gdb_byte *read_buf, int len,
+				      ULONGEST offset, int *target_errno);
+
+static read_alloc_pread_ftype target_fileio_read_alloc_1_pread;
+
+/* Helper for target_fileio_read_alloc_1 to make it interruptible.  */
+
+static int
+target_fileio_read_alloc_1_pread (int handle, gdb_byte *read_buf, int len,
+				  ULONGEST offset, int *target_errno)
+{
+  QUIT;
+
+  return target_fileio_pread (handle, read_buf, len, offset, target_errno);
+}
+
 /* Read target file FILENAME.  Store the result in *BUF_P and
    return the size of the transferred data.  PADDING additional bytes are
    available in *BUF_P.  This is a helper function for
@@ -2949,22 +2965,14 @@ target_fileio_close_cleanup (void *opaque)
    information.  */
 
 static LONGEST
-target_fileio_read_alloc_1 (const char *filename,
-			    gdb_byte **buf_p, int padding)
+read_alloc (gdb_byte **buf_p, int handle, read_alloc_pread_ftype *pread_func,
+	    int padding)
 {
-  struct cleanup *close_cleanup;
   size_t buf_alloc, buf_pos;
   gdb_byte *buf;
   LONGEST n;
-  int fd;
   int target_errno;
 
-  fd = target_fileio_open (filename, FILEIO_O_RDONLY, 0700, &target_errno);
-  if (fd == -1)
-    return -1;
-
-  close_cleanup = make_cleanup (target_fileio_close_cleanup, &fd);
-
   /* Start by reading up to 4K at a time.  The target will throttle
      this number down if necessary.  */
   buf_alloc = 4096;
@@ -2972,25 +2980,24 @@ target_fileio_read_alloc_1 (const char *filename,
   buf_pos = 0;
   while (1)
     {
-      n = target_fileio_pread (fd, &buf[buf_pos],
-			       buf_alloc - buf_pos - padding, buf_pos,
-			       &target_errno);
-      if (n < 0)
-	{
-	  /* An error occurred.  */
-	  do_cleanups (close_cleanup);
-	  xfree (buf);
-	  return -1;
-	}
-      else if (n == 0)
+      n = pread_func (handle, &buf[buf_pos], buf_alloc - buf_pos - padding,
+		      buf_pos, &target_errno);
+      if (n <= 0)
 	{
-	  /* Read all there was.  */
-	  do_cleanups (close_cleanup);
-	  if (buf_pos == 0)
+	  if (n < 0 || (n == 0 && buf_pos == 0))
 	    xfree (buf);
 	  else
 	    *buf_p = buf;
-	  return buf_pos;
+	  if (n < 0)
+	    {
+	      /* An error occurred.  */
+	      return -1;
+	    }
+	  else
+	    {
+	      /* Read all there was.  */
+	      return buf_pos;
+	    }
 	}
 
       buf_pos += n;
@@ -3001,11 +3008,32 @@ target_fileio_read_alloc_1 (const char *filename,
 	  buf_alloc *= 2;
 	  buf = xrealloc (buf, buf_alloc);
 	}
-
-      QUIT;
     }
 }
 
+typedef LONGEST (read_stralloc_func_ftype) (const char *filename,
+					    gdb_byte **buf_p, int padding);
+
+static read_stralloc_func_ftype target_fileio_read_alloc_1;
+
+static LONGEST
+target_fileio_read_alloc_1 (const char *filename,
+			    gdb_byte **buf_p, int padding)
+{
+  struct cleanup *close_cleanup;
+  int fd, target_errno;
+  LONGEST retval;
+
+  fd = target_fileio_open (filename, FILEIO_O_RDONLY, 0700, &target_errno);
+  if (fd == -1)
+    return -1;
+
+  close_cleanup = make_cleanup (target_fileio_close_cleanup, &fd);
+  retval = read_alloc (buf_p, fd, target_fileio_read_alloc_1_pread, padding);
+  do_cleanups (close_cleanup);
+  return retval;
+}
+
 /* Read target file FILENAME.  Store the result in *BUF_P and return
    the size of the transferred data.  See the declaration in "target.h"
    function for more information about the return value.  */
@@ -3022,14 +3050,14 @@ target_fileio_read_alloc (const char *filename, gdb_byte **buf_p)
    are returned as allocated but empty strings.  A warning is issued
    if the result contains any embedded NUL bytes.  */
 
-char *
-target_fileio_read_stralloc (const char *filename)
+static char *
+read_stralloc (const char *filename, read_stralloc_func_ftype *func)
 {
   gdb_byte *buffer;
   char *bufstr;
   LONGEST i, transferred;
 
-  transferred = target_fileio_read_alloc_1 (filename, &buffer, 1);
+  transferred = func (filename, &buffer, 1);
   bufstr = (char *) buffer;
 
   if (transferred < 0)
@@ -3053,6 +3081,11 @@ target_fileio_read_stralloc (const char *filename)
   return bufstr;
 }
 
+char *
+target_fileio_read_stralloc (const char *filename)
+{
+  return read_stralloc (filename, target_fileio_read_alloc_1);
+}
 
 static int
 default_region_ok_for_hw_watchpoint (struct target_ops *self,

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

* Re: [PATCH v6 03/10] Code cleanup: Rename enum -> enum filterflags
  2015-06-07 20:04 ` [PATCH v6 03/10] Code cleanup: Rename enum -> enum filterflags Jan Kratochvil
@ 2015-06-08  2:39   ` Sergio Durigan Junior
  0 siblings, 0 replies; 24+ messages in thread
From: Sergio Durigan Junior @ 2015-06-08  2:39 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches, Aleksandar Ristovski

On Sunday, June 07 2015, Jan Kratochvil wrote:

> Hi Sergio,
>
> this is an unrelated cleanup, bit mask ints are better to make enums as GDB
> already has support to automatically decode them:
>
> before this patch:
> 	(gdb) p filterflags
> 	$1 = 51
> 	(gdb) p/x filterflags
> 	$2 = 0x33
>
> after this patch:
> 	(gdb) p filterflags
> 	$1 = (COREFILTER_ANON_PRIVATE | COREFILTER_ANON_SHARED | COREFILTER_ELF_HEADERS | COREFILTER_HUGETLB_PRIVATE)
>
> New patch, not yet reviewed before.

Hello Jan,

Thanks for the patch.  I agree with it, of course.

> gdb/ChangeLog
> 2015-06-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
>
> 	* linux-tdep.c (enum filterflags): Make it from anonymous enum.
> 	(dump_mapping_p): Use it for parameter filterflags.
> 	(linux_find_memory_regions_full): Use it for variable filterflags.
> ---
>  gdb/linux-tdep.c |   12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
> index 8ce2f34..b0981f5 100644
> --- a/gdb/linux-tdep.c
> +++ b/gdb/linux-tdep.c
> @@ -46,7 +46,7 @@
>     Documentation/filesystems/proc.txt, inside the Linux kernel
>     tree.  */
>  
> -enum
> +enum filterflags
>    {
>      COREFILTER_ANON_PRIVATE = 1 << 0,
>      COREFILTER_ANON_SHARED = 1 << 1,
> @@ -598,7 +598,7 @@ mapping_is_anonymous_p (const char *filename)
>       This should work OK enough, however.  */
>  
>  static int
> -dump_mapping_p (unsigned int filterflags, const struct smaps_vmflags *v,
> +dump_mapping_p (enum filterflags filterflags, const struct smaps_vmflags *v,
>  		int maybe_private_p, int mapping_anon_p, int mapping_file_p,
>  		const char *filename)
>  {
> @@ -1119,10 +1119,10 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch,
>    /* Default dump behavior of coredump_filter (0x33), according to
>       Documentation/filesystems/proc.txt from the Linux kernel
>       tree.  */
> -  unsigned int filterflags = (COREFILTER_ANON_PRIVATE
> -			      | COREFILTER_ANON_SHARED
> -			      | COREFILTER_ELF_HEADERS
> -			      | COREFILTER_HUGETLB_PRIVATE);
> +  enum filterflags filterflags = (COREFILTER_ANON_PRIVATE
> +				  | COREFILTER_ANON_SHARED
> +				  | COREFILTER_ELF_HEADERS
> +				  | COREFILTER_HUGETLB_PRIVATE);
>  
>    /* We need to know the real target PID to access /proc.  */
>    if (current_inferior ()->fake_pid_p)

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/

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

* Re: [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-06-07 20:05 ` [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch] Jan Kratochvil
@ 2015-06-08  8:37   ` Gary Benson
  2015-06-11 18:48     ` Jan Kratochvil
                       ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Gary Benson @ 2015-06-08  8:37 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches, Aleksandar Ristovski

Jan Kratochvil wrote:
> prepare new files for later move.
> 
> Approved by:
> 	https://sourceware.org/ml/gdb-patches/2014-05/msg00367.html

Like Tom, I too find it weird having a commit that creates empty
files.  I'd prefer to see the new files created fully populated.

A bunch of stuff has changed in the way common code is laid out since
May 2014, so while Tom approved this back then, it's not suitable any
more.  Please update the series as follows:

> 	* common/linux-maps.c: New file.
> 	* common/linux-maps.h: New file.

Nothing os-specific should be in common.  These files should be
nat/linux-maps.[ch].

> 	* common/target-utils.c: New file.
> 	* common/target-utils.h: New file.

Nothing to do with the target should be in common.  The declarations
should probably be in target/target.h, and they should have "target_"
prefixes.  You could create target/target.c to put the definitions in.

> diff --git a/gdb/common/linux-maps.c b/gdb/common/linux-maps.c
...
> +
> +#ifdef GDBSERVER
> +#include "server.h"
> +#else
> +#include "defs.h"
> +#endif
> +
> +#include "linux-maps.h"

This should be:

  #include "common-defs.h"
  #include "linux-maps.h"

Please read this:
https://sourceware.org/gdb/wiki/Internals%20GDB-C-Coding-Standards#Include_Files

Other new files in this patch need similar treatment.

On a separate but related note, please do not add GDBSERVER
conditionals anywhere, I spent half a year removing almost
all of them and the remaining couple are on my hit list.

Thanks,
Gary

-- 
http://gbenson.net/

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

* Re: [PATCH v6 07/10] Move linux_find_memory_regions_full & co.
  2015-06-07 20:05 ` [PATCH v6 07/10] Move linux_find_memory_regions_full & co Jan Kratochvil
@ 2015-06-08  8:42   ` Gary Benson
  0 siblings, 0 replies; 24+ messages in thread
From: Gary Benson @ 2015-06-08  8:42 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches, Aleksandar Ristovski

Jan Kratochvil wrote:
> diff --git a/gdb/common/linux-maps.c b/gdb/common/linux-maps.c
> index bb3eac9..462a00f 100644
> --- a/gdb/common/linux-maps.c
> +++ b/gdb/common/linux-maps.c
> @@ -18,8 +18,529 @@
>  
>  #ifdef GDBSERVER
>  #include "server.h"
> +#include <fcntl.h>
> +#include <unistd.h>
>  #else
>  #include "defs.h"
> +#include "target.h"
>  #endif
>  
>  #include "linux-maps.h"
> +#include "gdb_assert.h"
> +#include <ctype.h>
> +#include <string.h>
> +#include "target-utils.h"
> +#include "gdb_regex.h"

In addition to the comment I made previously about not using GDBSERVER
conditionals, please note that common-defs.h includes gdb_assert.h and
string.h, so these two headers should not be listed here.

> diff --git a/gdb/common/target-utils.c b/gdb/common/target-utils.c
> index 308996d..ab8991d 100644
> --- a/gdb/common/target-utils.c
> +++ b/gdb/common/target-utils.c
> @@ -23,4 +23,84 @@
>  #include "defs.h"
>  #endif
>  
> +#include <string.h>
>  #include "target-utils.h"
> +#include "gdb_assert.h"

Ditto.

Cheers,
Gary

-- 
http://gbenson.net/

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

* Re: [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-06-08  8:37   ` Gary Benson
@ 2015-06-11 18:48     ` Jan Kratochvil
  2015-06-11 19:47       ` Aleksandar Ristovski
  2015-06-14 19:28     ` Jan Kratochvil
  2015-07-15 21:20     ` [PATCH v10 " Jan Kratochvil
  2 siblings, 1 reply; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-11 18:48 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches, Aleksandar Ristovski

On Mon, 08 Jun 2015 10:37:33 +0200, Gary Benson wrote:
> Jan Kratochvil wrote:
> > 	* common/target-utils.c: New file.
> > 	* common/target-utils.h: New file.
> 
> Nothing to do with the target should be in common.  The declarations
> should probably be in target/target.h, and they should have "target_"
> prefixes.  You could create target/target.c to put the definitions in.

gdb/target/target.c would create target.o which would conflict with
gdb/target.c.

There is already gdb/target/target.h "conflicting" with gdb/target.h but it
does not really conflict as gdb/target/ does not have default -I include dir.
Despite for example gdb/common/ has default -I.

I could propose various ideas what to move there but what is the suggested
solution?  Sure one could also for example just generate gdb/target-target.o
from gdb/target/target.c.


Jan

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

* Re: [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-06-11 18:48     ` Jan Kratochvil
@ 2015-06-11 19:47       ` Aleksandar Ristovski
  2015-06-12 11:26         ` Gary Benson
  0 siblings, 1 reply; 24+ messages in thread
From: Aleksandar Ristovski @ 2015-06-11 19:47 UTC (permalink / raw)
  To: Jan Kratochvil, Gary Benson; +Cc: gdb-patches

On 2015-06-11, 2:48 PM, "Jan Kratochvil" <jan.kratochvil@redhat.com> wrote:

>On Mon, 08 Jun 2015 10:37:33 +0200, Gary Benson wrote:
>> Jan Kratochvil wrote:
>> > 	* common/target-utils.c: New file.
>> > 	* common/target-utils.h: New file.
>> 
>> Nothing to do with the target should be in common.  The declarations
>> should probably be in target/target.h, and they should have "target_"
>> prefixes.  You could create target/target.c to put the definitions in.
>
>gdb/target/target.c would create target.o which would conflict with
>gdb/target.c.
>
>There is already gdb/target/target.h "conflicting" with gdb/target.h but
>it
>does not really conflict as gdb/target/ does not have default -I include
>dir.
>Despite for example gdb/common/ has default -I.
>
>I could propose various ideas what to move there but what is the suggested
>solution?  Sure one could also for example just generate
>gdb/target-target.o
>from gdb/target/target.c.

As you say, there could be various ideas if needed, but the simplest seems
good enough to me: just move the files to target dir

common/target-utils.[hc] -> target/target_utils.[hc]

Provided Gary is happy with that solution.

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

* Re: [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-06-11 19:47       ` Aleksandar Ristovski
@ 2015-06-12 11:26         ` Gary Benson
  0 siblings, 0 replies; 24+ messages in thread
From: Gary Benson @ 2015-06-12 11:26 UTC (permalink / raw)
  To: Aleksandar Ristovski; +Cc: Jan Kratochvil, gdb-patches

Aleksandar Ristovski wrote:
> On 2015-06-11, 2:48 PM, "Jan Kratochvil" <jan.kratochvil@redhat.com> wrote:
> >On Mon, 08 Jun 2015 10:37:33 +0200, Gary Benson wrote:
> > > Jan Kratochvil wrote:
> > > > 	* common/target-utils.c: New file.
> > > > 	* common/target-utils.h: New file.
> > > 
> > > Nothing to do with the target should be in common.  The
> > > declarations should probably be in target/target.h, and they
> > > should have "target_" prefixes.  You could create
> > > target/target.c to put the definitions in.
> >
> > gdb/target/target.c would create target.o which would conflict
> > with gdb/target.c.
> > 
> > There is already gdb/target/target.h "conflicting" with
> > gdb/target.h but it does not really conflict as gdb/target/ does
> > not have default -I include dir.  Despite for example gdb/common/
> > has default -I.

-Igdb/common is going to be removed at some stage.  I have a script
to do it, I just didn't ever get around to it.  Maybe once 7.10 is
released...

> > I could propose various ideas what to move there but what is the
> > suggested solution?  Sure one could also for example just generate
> > gdb/target-target.o from gdb/target/target.c.
> 
> As you say, there could be various ideas if needed, but the simplest
> seems good enough to me: just move the files to target dir
> 
> common/target-utils.[hc] -> target/target_utils.[hc]
> 
> Provided Gary is happy with that solution.

Works for me :)

Thanks,
Gary

-- 
http://gbenson.net/

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

* Re: [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-06-08  8:37   ` Gary Benson
  2015-06-11 18:48     ` Jan Kratochvil
@ 2015-06-14 19:28     ` Jan Kratochvil
  2015-06-23  8:42       ` Gary Benson
  2015-07-15 21:20     ` [PATCH v10 " Jan Kratochvil
  2 siblings, 1 reply; 24+ messages in thread
From: Jan Kratochvil @ 2015-06-14 19:28 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches, Aleksandar Ristovski

On Mon, 08 Jun 2015 10:37:33 +0200, Gary Benson wrote:
> Nothing to do with the target should be in common.  The declarations
> should probably be in target/target.h, and they should have "target_"
> prefixes.  You could create target/target.c to put the definitions in.

Created target/target-utils.c as we discussed elsewhere.


> > diff --git a/gdb/common/linux-maps.c b/gdb/common/linux-maps.c
> ...
> > +
> > +#ifdef GDBSERVER
> > +#include "server.h"
> > +#else
> > +#include "defs.h"
> > +#endif
> > +
> > +#include "linux-maps.h"
> 
> This should be:
> 
>   #include "common-defs.h"
>   #include "linux-maps.h"

Done.


> On a separate but related note, please do not add GDBSERVER
> conditionals anywhere, I spent half a year removing almost
> all of them and the remaining couple are on my hit list.

Done.


Jan

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

* Re: [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-06-14 19:28     ` Jan Kratochvil
@ 2015-06-23  8:42       ` Gary Benson
  0 siblings, 0 replies; 24+ messages in thread
From: Gary Benson @ 2015-06-23  8:42 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb-patches, Aleksandar Ristovski

Jan Kratochvil wrote:
> On Mon, 08 Jun 2015 10:37:33 +0200, Gary Benson wrote:
> > Nothing to do with the target should be in common.  The
> > declarations should probably be in target/target.h, and
> > they should have "target_" prefixes.  You could create
> > target/target.c to put the definitions in.
> 
> Created target/target-utils.c as we discussed elsewhere.
> 
> > > diff --git a/gdb/common/linux-maps.c b/gdb/common/linux-maps.c
> > ...
> > > +
> > > +#ifdef GDBSERVER
> > > +#include "server.h"
> > > +#else
> > > +#include "defs.h"
> > > +#endif
> > > +
> > > +#include "linux-maps.h"
> > 
> > This should be:
> > 
> >   #include "common-defs.h"
> >   #include "linux-maps.h"
> 
> Done.
> 
> > On a separate but related note, please do not add GDBSERVER
> > conditionals anywhere, I spent half a year removing almost
> > all of them and the remaining couple are on my hit list.
> 
> Done.

Thanks Jan.

Cheers,
Gary

-- 
http://gbenson.net/

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

* [PATCH v10 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-06-08  8:37   ` Gary Benson
  2015-06-11 18:48     ` Jan Kratochvil
  2015-06-14 19:28     ` Jan Kratochvil
@ 2015-07-15 21:20     ` Jan Kratochvil
  2015-07-16  8:14       ` Gary Benson
  2015-07-17 20:51       ` [PATCH v11 " Jan Kratochvil
  2 siblings, 2 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-07-15 21:20 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches, Aleksandar Ristovski

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

On Mon, 08 Jun 2015 10:37:33 +0200, Gary Benson wrote:
> A bunch of stuff has changed in the way common code is laid out since
> May 2014, so while Tom approved this back then, it's not suitable any
> more.  Please update the series as follows:
> 
> > 	* common/linux-maps.c: New file.
> > 	* common/linux-maps.h: New file.
> 
> Nothing os-specific should be in common.  These files should be
> nat/linux-maps.[ch].

While I have done so I do not share this opinion.  linux-maps.[ch] is used
from linux-tdep.c and not from linux-nat.c.  linux-tdep.c is using common/*
files a lot but it uses no function from nat/*:
	$ for i in $(for i in common/*.c;do nm `basename $i .c`.o;done|sed -n 's/^.* T //p');do grep -q "\<$i (" linux-tdep.c && echo $i;done|wc -l
	17
	$ for i in $(for i in    nat/*.c;do nm `basename $i .c`.o;done|sed -n 's/^.* T //p');do grep -q "\<$i (" linux-tdep.c && echo $i;done|wc -l
	0
	(commands above assume in-src-tree build)

Additionally linux-maps.o should not be in config/**.mh::NATDEPFILES (like
linux-namespaces.o from nat/ ) but it needs to be in configure.tgt instead.

Should be linux-maps.[ch] therefore in nat/ or in common/ ?
If it matters, though.


Jan

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

commit 0f08cd383c254274afe44a5a87edc4435977ef22
Author: Jan Kratochvil <jan.kratochvil@redhat.com>
Date:   Wed Jul 15 17:37:27 2015 +0200

    Create empty nat/linux-maps.[ch] and common/target-utils.[ch]
    
    Prepare new files for later move.
    
    gdb/ChangeLog
    2015-07-15  Aleksandar Ristovski  <aristovski@qnx.com
    	    Jan Kratochvil  <jan.kratochvil@redhat.com>
    
    	Create empty nat/linux-maps.[ch] and common/target-utils.[ch].
    	* Makefile.in (HFILES_NO_SRCDIR); Add nat/linux-maps.h,
    	common/target-utils.h.
    	(COMMON_OBS): Add target-utils.o.
    	(linux-maps.o, target-utils.o): New.
    	* target/target-utils.c: New file.
    	* target/target-utils.h: New file.
    	* nat/linux-maps.c: New file.
    	* nat/linux-maps.h: New file.
    	* configure.tgt (aarch64*-*-linux*, alpha*-*-linux*)
    	(am33_2.0*-*-linux*, arm*-*-linux*, bfin-*-*linux*, cris*, frv-*-*)
    	(hppa*-*-linux*, i[34567]86-*-linux*, ia64-*-linux*, m32r*-*-linux*)
    	(m68*-*-linux*, microblaze*-linux-*, microblaze*-*-linux*)
    	(mips*-*-linux*, nios2*-*-linux*, powerpc*-*-linux*, s390*-*-linux*)
    	(sh*-*-linux*, sparc-*-linux*, sparc64-*-linux*, tic6x-*-*linux)
    	(tilegx-*-linux*, x86_64-*-linux*, xtensa*-*-linux*): Add linux-maps.o
    	to gdb_target_obs.
    
    gdb/gdbserver/ChangeLog
    2015-07-15  Aleksandar Ristovski  <aristovski@qnx.com
    	    Jan Kratochvil  <jan.kratochvil@redhat.com>
    
    	Create empty nat/linux-maps.[ch] and common/target-utils.[ch].
    	* Makefile.in (OBS): Add target-utils.o.
    	(linux-maps.o, target-utils.o): New.
    	* configure.srv (srv_linux_obj): Add linux-maps.o.

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index dfaa8a3..237da9d 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -903,7 +903,7 @@ LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
 # right, it is probably easiest just to list .h files here directly.
 
 HFILES_NO_SRCDIR = \
-common/gdb_signals.h nat/gdb_thread_db.h common/gdb_vecs.h \
+common/gdb_signals.h nat/gdb_thread_db.h common/gdb_vecs.h nat/linux-maps.h \
 common/x86-xstate.h nat/linux-ptrace.h nat/mips-linux-watch.h \
 proc-utils.h aarch64-tdep.h arm-tdep.h ax-gdb.h ppcfbsd-tdep.h \
 ppcnbsd-tdep.h cli-out.h gdb_expat.h breakpoint.h infcall.h obsd-tdep.h \
@@ -983,7 +983,7 @@ common/common-debug.h common/cleanups.h common/gdb_setjmp.h \
 common/common-exceptions.h target/target.h common/symbol.h \
 common/common-regcache.h fbsd-tdep.h nat/linux-personality.h \
 common/fileio.h nat/x86-linux.h nat/x86-linux-dregs.h \
-nat/linux-namespaces.h
+nat/linux-namespaces.h target/target-utils.h
 
 # Header files that already have srcdir in them, or which are in objdir.
 
@@ -1083,7 +1083,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	common-utils.o buffer.o ptid.o gdb-dlfcn.o common-agent.o \
 	format.o registry.o btrace.o record-btrace.o waitstatus.o \
 	print-utils.o rsp-low.o errors.o common-debug.o debug.o \
-	common-exceptions.o btrace-common.o fileio.o \
+	common-exceptions.o btrace-common.o fileio.o target-utils.o \
 	$(SUBDIR_GCC_COMPILE_OBS)
 
 TSOBS = inflow.o
@@ -2221,6 +2221,10 @@ common-agent.o: $(srcdir)/common/agent.c
 	$(COMPILE) $(srcdir)/common/agent.c
 	$(POSTCOMPILE)
 
+linux-maps.o: ${srcdir}/nat/linux-maps.c
+	$(COMPILE) $(srcdir)/nat/linux-maps.c
+	$(POSTCOMPILE)
+
 vec.o: ${srcdir}/common/vec.c
 	$(COMPILE) $(srcdir)/common/vec.c
 	$(POSTCOMPILE)
@@ -2237,6 +2241,10 @@ errors.o: ${srcdir}/common/errors.c
 	$(COMPILE) $(srcdir)/common/errors.c
 	$(POSTCOMPILE)
 
+target-utils.o: ${srcdir}/target/target-utils.c
+	$(COMPILE) $(srcdir)/target/target-utils.c
+	$(POSTCOMPILE)
+
 common-debug.o: ${srcdir}/common/common-debug.c
 	$(COMPILE) $(srcdir)/common/common-debug.c
 	$(POSTCOMPILE)
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index f2c1a2d..20873c3 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -45,7 +45,7 @@ aarch64*-*-linux*)
 	# Target: AArch64 linux
 	gdb_target_obs="aarch64-tdep.o aarch64-linux-tdep.o \
 			arm-tdep.o arm-linux-tdep.o \
-			glibc-tdep.o linux-tdep.o solib-svr4.o \
+			glibc-tdep.o linux-tdep.o linux-maps.o solib-svr4.o \
 			symfile-mem.o linux-record.o"
 	build_gdbserver=yes
 	;;
@@ -53,7 +53,7 @@ aarch64*-*-linux*)
 alpha*-*-linux*)
 	# Target: Little-endian Alpha running Linux
 	gdb_target_obs="alpha-tdep.o alpha-mdebug-tdep.o alpha-linux-tdep.o \
-			linux-tdep.o solib-svr4.o"
+			linux-tdep.o linux-maps.o solib-svr4.o"
 	;;
 alpha*-*-freebsd* | alpha*-*-kfreebsd*-gnu)
 	# Target: FreeBSD/alpha
@@ -79,7 +79,7 @@ alpha*-*-*)
 am33_2.0*-*-linux*)
 	# Target: Matsushita mn10300 (AM33) running Linux
 	gdb_target_obs="mn10300-tdep.o mn10300-linux-tdep.o linux-tdep.o \
-			solib-svr4.o"
+			linux-maps.o solib-svr4.o"
 	;;
 
 arm*-wince-pe | arm*-*-mingw32ce*)
@@ -90,7 +90,8 @@ arm*-wince-pe | arm*-*-mingw32ce*)
 arm*-*-linux*)
 	# Target: ARM based machine running GNU/Linux
 	gdb_target_obs="arm-tdep.o arm-linux-tdep.o glibc-tdep.o \
-			solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o"
+			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o \
+			linux-record.o"
 	build_gdbserver=yes
 	;;
 arm*-*-netbsd* | arm*-*-knetbsd*-gnu)
@@ -120,7 +121,7 @@ avr-*-*)
 
 bfin-*-*linux*)
 	# Target: Blackfin Linux
-	gdb_target_obs="bfin-tdep.o bfin-linux-tdep.o linux-tdep.o"
+	gdb_target_obs="bfin-tdep.o bfin-linux-tdep.o linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/bfin/libsim.a
 	build_gdbserver=yes
 	;;
@@ -132,12 +133,14 @@ bfin-*-*)
 
 cris*)
 	# Target: CRIS
-	gdb_target_obs="cris-tdep.o cris-linux-tdep.o linux-tdep.o solib-svr4.o"
+	gdb_target_obs="cris-tdep.o cris-linux-tdep.o linux-tdep.o \
+			linux-maps.o solib-svr4.o"
 	;;
 
 frv-*-*)
 	# Target: Fujitsu FRV processor
-	gdb_target_obs="frv-tdep.o frv-linux-tdep.o linux-tdep.o solib-frv.o"
+	gdb_target_obs="frv-tdep.o frv-linux-tdep.o linux-tdep.o linux-maps.o \
+			solib-frv.o"
 	gdb_sim=../sim/frv/libsim.a
 	;;
 
@@ -155,7 +158,7 @@ h8300-*-*)
 hppa*-*-linux*)
 	# Target: HP PA-RISC running Linux
 	gdb_target_obs="hppa-tdep.o hppa-linux-tdep.o glibc-tdep.o \
-			linux-tdep.o solib-svr4.o symfile-mem.o"
+			linux-tdep.o linux-maps.o solib-svr4.o symfile-mem.o"
 	;;
 hppa*-*-netbsd*)
 	# Target: NetBSD/hppa
@@ -218,7 +221,7 @@ i[34567]86-*-linux*)
 	# Target: Intel 386 running GNU/Linux
 	gdb_target_obs="i386-tdep.o i386-linux-tdep.o glibc-tdep.o i387-tdep.o \
 			solib-svr4.o symfile-mem.o \
-			linux-tdep.o linux-record.o"
+			linux-tdep.o linux-maps.o linux-record.o"
 	if test "x$enable_64_bit_bfd" = "xyes"; then
 	    # Target: GNU/Linux x86-64
 	    gdb_target_obs="amd64-tdep.o amd64-linux-tdep.o ${gdb_target_obs}"
@@ -249,7 +252,7 @@ i[34567]86-*-*)
 ia64-*-linux*)
 	# Target: Intel IA-64 running GNU/Linux
 	gdb_target_obs="ia64-tdep.o ia64-linux-tdep.o linux-tdep.o \
-			solib-svr4.o symfile-mem.o"
+			linux-maps.o solib-svr4.o symfile-mem.o"
 	build_gdbserver=yes
 	;;
 ia64-*-*vms*)
@@ -283,7 +286,7 @@ m32r*-*-linux*)
 	# Target: Renesas M32R running GNU/Linux
 	gdb_target_obs="m32r-tdep.o m32r-linux-tdep.o remote-m32r-sdi.o \
 			glibc-tdep.o solib-svr4.o symfile-mem.o \
-			linux-tdep.o"
+			linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/m32r/libsim.a
 	build_gdbserver=yes
 	;;
@@ -308,7 +311,7 @@ fido-*-elf*)
 m68*-*-linux*)
 	# Target: Motorola m68k with a.out and ELF
 	gdb_target_obs="m68k-tdep.o m68klinux-tdep.o solib-svr4.o \
-			linux-tdep.o glibc-tdep.o symfile-mem.o"
+			linux-tdep.o linux-maps.o glibc-tdep.o symfile-mem.o"
 	build_gdbserver=yes
 	;;
 m68*-*-netbsd* | m68*-*-knetbsd*-gnu)
@@ -334,7 +337,8 @@ mep-*-*)
 microblaze*-linux-*|microblaze*-*-linux*)
 	# Target: Xilinx MicroBlaze running Linux
 	gdb_target_obs="microblaze-tdep.o microblaze-linux-tdep.o microblaze-rom.o \
-			monitor.o dsrec.o solib-svr4.o symfile-mem.o linux-tdep.o"
+			monitor.o dsrec.o solib-svr4.o symfile-mem.o \
+			linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/microblaze/libsim.a
 	;;
 microblaze*-*-*)
@@ -346,7 +350,7 @@ microblaze*-*-*)
 mips*-*-linux*)
 	# Target: Linux/MIPS
 	gdb_target_obs="mips-tdep.o mips-linux-tdep.o glibc-tdep.o \
-			solib-svr4.o symfile-mem.o linux-tdep.o"
+			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/mips/libsim.a
 	build_gdbserver=yes
 	;;
@@ -394,7 +398,7 @@ mt-*-*)
 nios2*-*-linux*)
 	# Target: Altera Nios II running Linux
 	gdb_target_obs="nios2-tdep.o nios2-linux-tdep.o solib-svr4.o \
-			symfile-mem.o glibc-tdep.o linux-tdep.o"
+			symfile-mem.o glibc-tdep.o linux-tdep.o linux-maps.o"
 	;;
 
 nios2*-*-*)
@@ -433,7 +437,7 @@ powerpc*-*-linux*)
 	gdb_target_obs="rs6000-tdep.o ppc-linux-tdep.o ppc-sysv-tdep.o \
 			ppc64-tdep.o solib-svr4.o solib-spu.o \
 			spu-multiarch.o \
-			glibc-tdep.o symfile-mem.o linux-tdep.o \
+			glibc-tdep.o symfile-mem.o linux-tdep.o linux-maps.o \
 			ravenscar-thread.o ppc-ravenscar-thread.o \
 			linux-record.o "
 	gdb_sim=../sim/ppc/libsim.a
@@ -457,7 +461,7 @@ powerpc*-*-*)
 
 s390*-*-linux*)
 	# Target: S390 running Linux
-	gdb_target_obs="s390-linux-tdep.o solib-svr4.o linux-tdep.o"
+	gdb_target_obs="s390-linux-tdep.o solib-svr4.o linux-tdep.o linux-maps.o"
 	build_gdbserver=yes
 	;;
 
@@ -486,7 +490,7 @@ sh*-*-linux*)
 	# Target: GNU/Linux Super-H
 	gdb_target_obs="sh-tdep.o sh64-tdep.o sh-linux-tdep.o monitor.o \
 			dsrec.o solib-svr4.o symfile-mem.o \
-			glibc-tdep.o linux-tdep.o"
+			glibc-tdep.o linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/sh/libsim.a
 	build_gdbserver=yes
 	;;
@@ -514,7 +518,7 @@ sparc-*-linux*)
 	# Target: GNU/Linux SPARC
 	gdb_target_obs="sparc-tdep.o sparc-sol2-tdep.o sol2-tdep.o \
 			sparc-linux-tdep.o solib-svr4.o symfile-mem.o \
-			linux-tdep.o \
+			linux-tdep.o linux-maps.o \
 			ravenscar-thread.o sparc-ravenscar-thread.o"
 	if test "x$enable_64_bit_bfd" = "xyes"; then
 	    # Target: GNU/Linux UltraSPARC
@@ -528,7 +532,8 @@ sparc64-*-linux*)
 	gdb_target_obs="sparc64-tdep.o sparc64-sol2-tdep.o sol2-tdep.o \
 			sparc64-linux-tdep.o sparc-tdep.o sparc-sol2-tdep.o \
 			sparc-linux-tdep.o solib-svr4.o linux-tdep.o \
-			ravenscar-thread.o sparc-ravenscar-thread.o"
+			linux-maps.o ravenscar-thread.o \
+			sparc-ravenscar-thread.o"
 	build_gdbserver=yes
 	;;
 sparc*-*-freebsd* | sparc*-*-kfreebsd*-gnu)
@@ -595,7 +600,7 @@ spu*-*-*)
 tic6x-*-*linux)
 	# Target: GNU/Linux TI C6x
 	gdb_target_obs="tic6x-tdep.o tic6x-linux-tdep.o solib-dsbt.o \
-			glibc-tdep.o linux-tdep.o"
+			glibc-tdep.o linux-tdep.o linux-maps.o"
 	;;
 
 tic6x-*-*)
@@ -606,7 +611,7 @@ tic6x-*-*)
 tilegx-*-linux*)
 	# Target: TILE-Gx
 	gdb_target_obs="tilegx-tdep.o tilegx-linux-tdep.o solib-svr4.o \
-			symfile-mem.o glibc-tdep.o linux-tdep.o"
+			symfile-mem.o glibc-tdep.o linux-tdep.o linux-maps.o"
 	build_gdbserver=yes
 	;;
 
@@ -656,7 +661,8 @@ x86_64-*-linux*)
 	# Target: GNU/Linux x86-64
 	gdb_target_obs="amd64-tdep.o amd64-linux-tdep.o i386-tdep.o \
 			i387-tdep.o i386-linux-tdep.o glibc-tdep.o \
-			solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o"
+			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o \
+			linux-record.o"
 	build_gdbserver=yes
 	;;
 x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu)
@@ -686,7 +692,7 @@ x86_64-*-openbsd*)
 xtensa*-*-linux*)	gdb_target=linux
 	# Target: GNU/Linux Xtensa
 	gdb_target_obs="xtensa-tdep.o xtensa-config.o xtensa-linux-tdep.o \
-			solib-svr4.o symfile-mem.o linux-tdep.o"
+			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o"
 	build_gdbserver=yes
 	;;
 xtensa*)
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index fc250fb..908061a 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -194,7 +194,7 @@ OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o \
       mem-break.o hostio.o event-loop.o tracepoint.o xml-utils.o \
       common-utils.o ptid.o buffer.o format.o filestuff.o dll.o notif.o \
       tdesc.o print-utils.o rsp-low.o errors.o common-debug.o cleanups.o \
-      common-exceptions.o symbol.o btrace-common.o fileio.o \
+      common-exceptions.o symbol.o btrace-common.o fileio.o target-utils.o \
       $(XML_BUILTIN) $(DEPFILES) $(LIBOBJS)
 GDBREPLAY_OBS = gdbreplay.o version.o
 GDBSERVER_LIBS = @GDBSERVER_LIBS@
@@ -519,6 +519,9 @@ ax.o: ax.c
 signals.o: ../common/signals.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+linux-maps.o: ../nat/linux-maps.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 print-utils.o: ../common/print-utils.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
@@ -531,6 +534,9 @@ common-utils.o: ../common/common-utils.c
 posix-strerror.o: ../common/posix-strerror.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+target-utils.o: ../target/target-utils.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 mingw-strerror.o: ../common/mingw-strerror.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
diff --git a/gdb/gdbserver/configure.srv b/gdb/gdbserver/configure.srv
index 7f89f2f..ac324ff 100644
--- a/gdb/gdbserver/configure.srv
+++ b/gdb/gdbserver/configure.srv
@@ -42,7 +42,7 @@ srv_amd64_linux_xmlfiles="i386/amd64-linux.xml i386/amd64-avx-linux.xml i386/amd
 
 # Linux object files.  This is so we don't have to repeat
 # these files over and over again.
-srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o linux-namespaces.o"
+srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-maps.o linux-ptrace.o linux-waitpid.o linux-personality.o linux-namespaces.o"
 
 # Input is taken from the "${target}" variable.
 
diff --git a/gdb/nat/linux-maps.c b/gdb/nat/linux-maps.c
new file mode 100644
index 0000000..01c8836
--- /dev/null
+++ b/gdb/nat/linux-maps.c
@@ -0,0 +1,20 @@
+/* Linux-specific memory maps manipulation routines.
+   Copyright (C) 2015 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/>.  */
+
+#include "common-defs.h"
+#include "linux-maps.h"
diff --git a/gdb/nat/linux-maps.h b/gdb/nat/linux-maps.h
new file mode 100644
index 0000000..2cff321
--- /dev/null
+++ b/gdb/nat/linux-maps.h
@@ -0,0 +1,22 @@
+/* Linux-specific memory maps manipulation routines.
+   Copyright (C) 2015 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/>.  */
+
+#ifndef NAT_LINUX_MAPS_H
+#define NAT_LINUX_MAPS_H
+
+#endif /* NAT_LINUX_MAPS_H */
diff --git a/gdb/target/target-utils.c b/gdb/target/target-utils.c
new file mode 100644
index 0000000..4e8fae7
--- /dev/null
+++ b/gdb/target/target-utils.c
@@ -0,0 +1,21 @@
+/* Utility target functions for GDB, and GDBserver.
+
+   Copyright (C) 2015 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/>.  */
+
+#include "common-defs.h"
+#include "target/target-utils.h"
diff --git a/gdb/target/target-utils.h b/gdb/target/target-utils.h
new file mode 100644
index 0000000..443ffc3
--- /dev/null
+++ b/gdb/target/target-utils.h
@@ -0,0 +1,23 @@
+/* Utility target functions for GDB, and GDBserver.
+
+   Copyright (C) 2015 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/>.  */
+
+#ifndef TARGET_TARGET_UTILS_H
+#define TARGET_TARGET_UTILS_H
+
+#endif /* TARGET_TARGET_UTILS_H */

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

* Re: [PATCH v10 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-07-15 21:20     ` [PATCH v10 " Jan Kratochvil
@ 2015-07-16  8:14       ` Gary Benson
  2015-07-16  8:32         ` Jan Kratochvil
  2015-07-21 10:28         ` Pedro Alves
  2015-07-17 20:51       ` [PATCH v11 " Jan Kratochvil
  1 sibling, 2 replies; 24+ messages in thread
From: Gary Benson @ 2015-07-16  8:14 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: Pedro Alves, gdb-patches, Aleksandar Ristovski

Jan Kratochvil wrote:
> On Mon, 08 Jun 2015 10:37:33 +0200, Gary Benson wrote:
> > A bunch of stuff has changed in the way common code is laid out
> > since May 2014, so while Tom approved this back then, it's not
> > suitable any more.  Please update the series as follows:
> > 
> > > 	* common/linux-maps.c: New file.
> > > 	* common/linux-maps.h: New file.
> > 
> > Nothing os-specific should be in common.  These files should be
> > nat/linux-maps.[ch].
> 
> While I have done so I do not share this opinion.  linux-maps.[ch]
> is used from linux-tdep.c and not from linux-nat.c.  linux-tdep.c is
> using common/* files a lot but it uses no function from nat/*:
> 
> 	$ for i in $(for i in common/*.c;do nm `basename $i .c`.o;done|sed -n 's/^.* T //p');do grep -q "\<$i (" linux-tdep.c && echo $i;done|wc -l
> 	17
> 	$ for i in $(for i in    nat/*.c;do nm `basename $i .c`.o;done|sed -n 's/^.* T //p');do grep -q "\<$i (" linux-tdep.c && echo $i;done|wc -l
> 	0
> 	(commands above assume in-src-tree build)
> 
> Additionally linux-maps.o should not be in config/**.mh::NATDEPFILES
> (like linux-namespaces.o from nat/ ) but it needs to be in
> configure.tgt instead.
> 
> Should be linux-maps.[ch] therefore in nat/ or in common/ ?
> If it matters, though.

I don't fully understand the tdep/non-tdep split very well.
Pedro, could you comment?

Cheers,
Gary

> commit 0f08cd383c254274afe44a5a87edc4435977ef22
> Author: Jan Kratochvil <jan.kratochvil@redhat.com>
> Date:   Wed Jul 15 17:37:27 2015 +0200
> 
>     Create empty nat/linux-maps.[ch] and common/target-utils.[ch]
>     
>     Prepare new files for later move.
>     
>     gdb/ChangeLog
>     2015-07-15  Aleksandar Ristovski  <aristovski@qnx.com
>     	    Jan Kratochvil  <jan.kratochvil@redhat.com>
>     
>     	Create empty nat/linux-maps.[ch] and common/target-utils.[ch].
>     	* Makefile.in (HFILES_NO_SRCDIR); Add nat/linux-maps.h,
>     	common/target-utils.h.
>     	(COMMON_OBS): Add target-utils.o.
>     	(linux-maps.o, target-utils.o): New.
>     	* target/target-utils.c: New file.
>     	* target/target-utils.h: New file.
>     	* nat/linux-maps.c: New file.
>     	* nat/linux-maps.h: New file.
>     	* configure.tgt (aarch64*-*-linux*, alpha*-*-linux*)
>     	(am33_2.0*-*-linux*, arm*-*-linux*, bfin-*-*linux*, cris*, frv-*-*)
>     	(hppa*-*-linux*, i[34567]86-*-linux*, ia64-*-linux*, m32r*-*-linux*)
>     	(m68*-*-linux*, microblaze*-linux-*, microblaze*-*-linux*)
>     	(mips*-*-linux*, nios2*-*-linux*, powerpc*-*-linux*, s390*-*-linux*)
>     	(sh*-*-linux*, sparc-*-linux*, sparc64-*-linux*, tic6x-*-*linux)
>     	(tilegx-*-linux*, x86_64-*-linux*, xtensa*-*-linux*): Add linux-maps.o
>     	to gdb_target_obs.
>     
>     gdb/gdbserver/ChangeLog
>     2015-07-15  Aleksandar Ristovski  <aristovski@qnx.com
>     	    Jan Kratochvil  <jan.kratochvil@redhat.com>
>     
>     	Create empty nat/linux-maps.[ch] and common/target-utils.[ch].
>     	* Makefile.in (OBS): Add target-utils.o.
>     	(linux-maps.o, target-utils.o): New.
>     	* configure.srv (srv_linux_obj): Add linux-maps.o.
> 
> diff --git a/gdb/Makefile.in b/gdb/Makefile.in
> index dfaa8a3..237da9d 100644
> --- a/gdb/Makefile.in
> +++ b/gdb/Makefile.in
> @@ -903,7 +903,7 @@ LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
>  # right, it is probably easiest just to list .h files here directly.
>  
>  HFILES_NO_SRCDIR = \
> -common/gdb_signals.h nat/gdb_thread_db.h common/gdb_vecs.h \
> +common/gdb_signals.h nat/gdb_thread_db.h common/gdb_vecs.h nat/linux-maps.h \
>  common/x86-xstate.h nat/linux-ptrace.h nat/mips-linux-watch.h \
>  proc-utils.h aarch64-tdep.h arm-tdep.h ax-gdb.h ppcfbsd-tdep.h \
>  ppcnbsd-tdep.h cli-out.h gdb_expat.h breakpoint.h infcall.h obsd-tdep.h \
> @@ -983,7 +983,7 @@ common/common-debug.h common/cleanups.h common/gdb_setjmp.h \
>  common/common-exceptions.h target/target.h common/symbol.h \
>  common/common-regcache.h fbsd-tdep.h nat/linux-personality.h \
>  common/fileio.h nat/x86-linux.h nat/x86-linux-dregs.h \
> -nat/linux-namespaces.h
> +nat/linux-namespaces.h target/target-utils.h
>  
>  # Header files that already have srcdir in them, or which are in objdir.
>  
> @@ -1083,7 +1083,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
>  	common-utils.o buffer.o ptid.o gdb-dlfcn.o common-agent.o \
>  	format.o registry.o btrace.o record-btrace.o waitstatus.o \
>  	print-utils.o rsp-low.o errors.o common-debug.o debug.o \
> -	common-exceptions.o btrace-common.o fileio.o \
> +	common-exceptions.o btrace-common.o fileio.o target-utils.o \
>  	$(SUBDIR_GCC_COMPILE_OBS)
>  
>  TSOBS = inflow.o
> @@ -2221,6 +2221,10 @@ common-agent.o: $(srcdir)/common/agent.c
>  	$(COMPILE) $(srcdir)/common/agent.c
>  	$(POSTCOMPILE)
>  
> +linux-maps.o: ${srcdir}/nat/linux-maps.c
> +	$(COMPILE) $(srcdir)/nat/linux-maps.c
> +	$(POSTCOMPILE)
> +
>  vec.o: ${srcdir}/common/vec.c
>  	$(COMPILE) $(srcdir)/common/vec.c
>  	$(POSTCOMPILE)
> @@ -2237,6 +2241,10 @@ errors.o: ${srcdir}/common/errors.c
>  	$(COMPILE) $(srcdir)/common/errors.c
>  	$(POSTCOMPILE)
>  
> +target-utils.o: ${srcdir}/target/target-utils.c
> +	$(COMPILE) $(srcdir)/target/target-utils.c
> +	$(POSTCOMPILE)
> +
>  common-debug.o: ${srcdir}/common/common-debug.c
>  	$(COMPILE) $(srcdir)/common/common-debug.c
>  	$(POSTCOMPILE)
> diff --git a/gdb/configure.tgt b/gdb/configure.tgt
> index f2c1a2d..20873c3 100644
> --- a/gdb/configure.tgt
> +++ b/gdb/configure.tgt
> @@ -45,7 +45,7 @@ aarch64*-*-linux*)
>  	# Target: AArch64 linux
>  	gdb_target_obs="aarch64-tdep.o aarch64-linux-tdep.o \
>  			arm-tdep.o arm-linux-tdep.o \
> -			glibc-tdep.o linux-tdep.o solib-svr4.o \
> +			glibc-tdep.o linux-tdep.o linux-maps.o solib-svr4.o \
>  			symfile-mem.o linux-record.o"
>  	build_gdbserver=yes
>  	;;
> @@ -53,7 +53,7 @@ aarch64*-*-linux*)
>  alpha*-*-linux*)
>  	# Target: Little-endian Alpha running Linux
>  	gdb_target_obs="alpha-tdep.o alpha-mdebug-tdep.o alpha-linux-tdep.o \
> -			linux-tdep.o solib-svr4.o"
> +			linux-tdep.o linux-maps.o solib-svr4.o"
>  	;;
>  alpha*-*-freebsd* | alpha*-*-kfreebsd*-gnu)
>  	# Target: FreeBSD/alpha
> @@ -79,7 +79,7 @@ alpha*-*-*)
>  am33_2.0*-*-linux*)
>  	# Target: Matsushita mn10300 (AM33) running Linux
>  	gdb_target_obs="mn10300-tdep.o mn10300-linux-tdep.o linux-tdep.o \
> -			solib-svr4.o"
> +			linux-maps.o solib-svr4.o"
>  	;;
>  
>  arm*-wince-pe | arm*-*-mingw32ce*)
> @@ -90,7 +90,8 @@ arm*-wince-pe | arm*-*-mingw32ce*)
>  arm*-*-linux*)
>  	# Target: ARM based machine running GNU/Linux
>  	gdb_target_obs="arm-tdep.o arm-linux-tdep.o glibc-tdep.o \
> -			solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o"
> +			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o \
> +			linux-record.o"
>  	build_gdbserver=yes
>  	;;
>  arm*-*-netbsd* | arm*-*-knetbsd*-gnu)
> @@ -120,7 +121,7 @@ avr-*-*)
>  
>  bfin-*-*linux*)
>  	# Target: Blackfin Linux
> -	gdb_target_obs="bfin-tdep.o bfin-linux-tdep.o linux-tdep.o"
> +	gdb_target_obs="bfin-tdep.o bfin-linux-tdep.o linux-tdep.o linux-maps.o"
>  	gdb_sim=../sim/bfin/libsim.a
>  	build_gdbserver=yes
>  	;;
> @@ -132,12 +133,14 @@ bfin-*-*)
>  
>  cris*)
>  	# Target: CRIS
> -	gdb_target_obs="cris-tdep.o cris-linux-tdep.o linux-tdep.o solib-svr4.o"
> +	gdb_target_obs="cris-tdep.o cris-linux-tdep.o linux-tdep.o \
> +			linux-maps.o solib-svr4.o"
>  	;;
>  
>  frv-*-*)
>  	# Target: Fujitsu FRV processor
> -	gdb_target_obs="frv-tdep.o frv-linux-tdep.o linux-tdep.o solib-frv.o"
> +	gdb_target_obs="frv-tdep.o frv-linux-tdep.o linux-tdep.o linux-maps.o \
> +			solib-frv.o"
>  	gdb_sim=../sim/frv/libsim.a
>  	;;
>  
> @@ -155,7 +158,7 @@ h8300-*-*)
>  hppa*-*-linux*)
>  	# Target: HP PA-RISC running Linux
>  	gdb_target_obs="hppa-tdep.o hppa-linux-tdep.o glibc-tdep.o \
> -			linux-tdep.o solib-svr4.o symfile-mem.o"
> +			linux-tdep.o linux-maps.o solib-svr4.o symfile-mem.o"
>  	;;
>  hppa*-*-netbsd*)
>  	# Target: NetBSD/hppa
> @@ -218,7 +221,7 @@ i[34567]86-*-linux*)
>  	# Target: Intel 386 running GNU/Linux
>  	gdb_target_obs="i386-tdep.o i386-linux-tdep.o glibc-tdep.o i387-tdep.o \
>  			solib-svr4.o symfile-mem.o \
> -			linux-tdep.o linux-record.o"
> +			linux-tdep.o linux-maps.o linux-record.o"
>  	if test "x$enable_64_bit_bfd" = "xyes"; then
>  	    # Target: GNU/Linux x86-64
>  	    gdb_target_obs="amd64-tdep.o amd64-linux-tdep.o ${gdb_target_obs}"
> @@ -249,7 +252,7 @@ i[34567]86-*-*)
>  ia64-*-linux*)
>  	# Target: Intel IA-64 running GNU/Linux
>  	gdb_target_obs="ia64-tdep.o ia64-linux-tdep.o linux-tdep.o \
> -			solib-svr4.o symfile-mem.o"
> +			linux-maps.o solib-svr4.o symfile-mem.o"
>  	build_gdbserver=yes
>  	;;
>  ia64-*-*vms*)
> @@ -283,7 +286,7 @@ m32r*-*-linux*)
>  	# Target: Renesas M32R running GNU/Linux
>  	gdb_target_obs="m32r-tdep.o m32r-linux-tdep.o remote-m32r-sdi.o \
>  			glibc-tdep.o solib-svr4.o symfile-mem.o \
> -			linux-tdep.o"
> +			linux-tdep.o linux-maps.o"
>  	gdb_sim=../sim/m32r/libsim.a
>  	build_gdbserver=yes
>  	;;
> @@ -308,7 +311,7 @@ fido-*-elf*)
>  m68*-*-linux*)
>  	# Target: Motorola m68k with a.out and ELF
>  	gdb_target_obs="m68k-tdep.o m68klinux-tdep.o solib-svr4.o \
> -			linux-tdep.o glibc-tdep.o symfile-mem.o"
> +			linux-tdep.o linux-maps.o glibc-tdep.o symfile-mem.o"
>  	build_gdbserver=yes
>  	;;
>  m68*-*-netbsd* | m68*-*-knetbsd*-gnu)
> @@ -334,7 +337,8 @@ mep-*-*)
>  microblaze*-linux-*|microblaze*-*-linux*)
>  	# Target: Xilinx MicroBlaze running Linux
>  	gdb_target_obs="microblaze-tdep.o microblaze-linux-tdep.o microblaze-rom.o \
> -			monitor.o dsrec.o solib-svr4.o symfile-mem.o linux-tdep.o"
> +			monitor.o dsrec.o solib-svr4.o symfile-mem.o \
> +			linux-tdep.o linux-maps.o"
>  	gdb_sim=../sim/microblaze/libsim.a
>  	;;
>  microblaze*-*-*)
> @@ -346,7 +350,7 @@ microblaze*-*-*)
>  mips*-*-linux*)
>  	# Target: Linux/MIPS
>  	gdb_target_obs="mips-tdep.o mips-linux-tdep.o glibc-tdep.o \
> -			solib-svr4.o symfile-mem.o linux-tdep.o"
> +			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o"
>  	gdb_sim=../sim/mips/libsim.a
>  	build_gdbserver=yes
>  	;;
> @@ -394,7 +398,7 @@ mt-*-*)
>  nios2*-*-linux*)
>  	# Target: Altera Nios II running Linux
>  	gdb_target_obs="nios2-tdep.o nios2-linux-tdep.o solib-svr4.o \
> -			symfile-mem.o glibc-tdep.o linux-tdep.o"
> +			symfile-mem.o glibc-tdep.o linux-tdep.o linux-maps.o"
>  	;;
>  
>  nios2*-*-*)
> @@ -433,7 +437,7 @@ powerpc*-*-linux*)
>  	gdb_target_obs="rs6000-tdep.o ppc-linux-tdep.o ppc-sysv-tdep.o \
>  			ppc64-tdep.o solib-svr4.o solib-spu.o \
>  			spu-multiarch.o \
> -			glibc-tdep.o symfile-mem.o linux-tdep.o \
> +			glibc-tdep.o symfile-mem.o linux-tdep.o linux-maps.o \
>  			ravenscar-thread.o ppc-ravenscar-thread.o \
>  			linux-record.o "
>  	gdb_sim=../sim/ppc/libsim.a
> @@ -457,7 +461,7 @@ powerpc*-*-*)
>  
>  s390*-*-linux*)
>  	# Target: S390 running Linux
> -	gdb_target_obs="s390-linux-tdep.o solib-svr4.o linux-tdep.o"
> +	gdb_target_obs="s390-linux-tdep.o solib-svr4.o linux-tdep.o linux-maps.o"
>  	build_gdbserver=yes
>  	;;
>  
> @@ -486,7 +490,7 @@ sh*-*-linux*)
>  	# Target: GNU/Linux Super-H
>  	gdb_target_obs="sh-tdep.o sh64-tdep.o sh-linux-tdep.o monitor.o \
>  			dsrec.o solib-svr4.o symfile-mem.o \
> -			glibc-tdep.o linux-tdep.o"
> +			glibc-tdep.o linux-tdep.o linux-maps.o"
>  	gdb_sim=../sim/sh/libsim.a
>  	build_gdbserver=yes
>  	;;
> @@ -514,7 +518,7 @@ sparc-*-linux*)
>  	# Target: GNU/Linux SPARC
>  	gdb_target_obs="sparc-tdep.o sparc-sol2-tdep.o sol2-tdep.o \
>  			sparc-linux-tdep.o solib-svr4.o symfile-mem.o \
> -			linux-tdep.o \
> +			linux-tdep.o linux-maps.o \
>  			ravenscar-thread.o sparc-ravenscar-thread.o"
>  	if test "x$enable_64_bit_bfd" = "xyes"; then
>  	    # Target: GNU/Linux UltraSPARC
> @@ -528,7 +532,8 @@ sparc64-*-linux*)
>  	gdb_target_obs="sparc64-tdep.o sparc64-sol2-tdep.o sol2-tdep.o \
>  			sparc64-linux-tdep.o sparc-tdep.o sparc-sol2-tdep.o \
>  			sparc-linux-tdep.o solib-svr4.o linux-tdep.o \
> -			ravenscar-thread.o sparc-ravenscar-thread.o"
> +			linux-maps.o ravenscar-thread.o \
> +			sparc-ravenscar-thread.o"
>  	build_gdbserver=yes
>  	;;
>  sparc*-*-freebsd* | sparc*-*-kfreebsd*-gnu)
> @@ -595,7 +600,7 @@ spu*-*-*)
>  tic6x-*-*linux)
>  	# Target: GNU/Linux TI C6x
>  	gdb_target_obs="tic6x-tdep.o tic6x-linux-tdep.o solib-dsbt.o \
> -			glibc-tdep.o linux-tdep.o"
> +			glibc-tdep.o linux-tdep.o linux-maps.o"
>  	;;
>  
>  tic6x-*-*)
> @@ -606,7 +611,7 @@ tic6x-*-*)
>  tilegx-*-linux*)
>  	# Target: TILE-Gx
>  	gdb_target_obs="tilegx-tdep.o tilegx-linux-tdep.o solib-svr4.o \
> -			symfile-mem.o glibc-tdep.o linux-tdep.o"
> +			symfile-mem.o glibc-tdep.o linux-tdep.o linux-maps.o"
>  	build_gdbserver=yes
>  	;;
>  
> @@ -656,7 +661,8 @@ x86_64-*-linux*)
>  	# Target: GNU/Linux x86-64
>  	gdb_target_obs="amd64-tdep.o amd64-linux-tdep.o i386-tdep.o \
>  			i387-tdep.o i386-linux-tdep.o glibc-tdep.o \
> -			solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o"
> +			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o \
> +			linux-record.o"
>  	build_gdbserver=yes
>  	;;
>  x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu)
> @@ -686,7 +692,7 @@ x86_64-*-openbsd*)
>  xtensa*-*-linux*)	gdb_target=linux
>  	# Target: GNU/Linux Xtensa
>  	gdb_target_obs="xtensa-tdep.o xtensa-config.o xtensa-linux-tdep.o \
> -			solib-svr4.o symfile-mem.o linux-tdep.o"
> +			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o"
>  	build_gdbserver=yes
>  	;;
>  xtensa*)
> diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
> index fc250fb..908061a 100644
> --- a/gdb/gdbserver/Makefile.in
> +++ b/gdb/gdbserver/Makefile.in
> @@ -194,7 +194,7 @@ OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o \
>        mem-break.o hostio.o event-loop.o tracepoint.o xml-utils.o \
>        common-utils.o ptid.o buffer.o format.o filestuff.o dll.o notif.o \
>        tdesc.o print-utils.o rsp-low.o errors.o common-debug.o cleanups.o \
> -      common-exceptions.o symbol.o btrace-common.o fileio.o \
> +      common-exceptions.o symbol.o btrace-common.o fileio.o target-utils.o \
>        $(XML_BUILTIN) $(DEPFILES) $(LIBOBJS)
>  GDBREPLAY_OBS = gdbreplay.o version.o
>  GDBSERVER_LIBS = @GDBSERVER_LIBS@
> @@ -519,6 +519,9 @@ ax.o: ax.c
>  signals.o: ../common/signals.c
>  	$(COMPILE) $<
>  	$(POSTCOMPILE)
> +linux-maps.o: ../nat/linux-maps.c
> +	$(COMPILE) $<
> +	$(POSTCOMPILE)
>  print-utils.o: ../common/print-utils.c
>  	$(COMPILE) $<
>  	$(POSTCOMPILE)
> @@ -531,6 +534,9 @@ common-utils.o: ../common/common-utils.c
>  posix-strerror.o: ../common/posix-strerror.c
>  	$(COMPILE) $<
>  	$(POSTCOMPILE)
> +target-utils.o: ../target/target-utils.c
> +	$(COMPILE) $<
> +	$(POSTCOMPILE)
>  mingw-strerror.o: ../common/mingw-strerror.c
>  	$(COMPILE) $<
>  	$(POSTCOMPILE)
> diff --git a/gdb/gdbserver/configure.srv b/gdb/gdbserver/configure.srv
> index 7f89f2f..ac324ff 100644
> --- a/gdb/gdbserver/configure.srv
> +++ b/gdb/gdbserver/configure.srv
> @@ -42,7 +42,7 @@ srv_amd64_linux_xmlfiles="i386/amd64-linux.xml i386/amd64-avx-linux.xml i386/amd
>  
>  # Linux object files.  This is so we don't have to repeat
>  # these files over and over again.
> -srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o linux-namespaces.o"
> +srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-maps.o linux-ptrace.o linux-waitpid.o linux-personality.o linux-namespaces.o"
>  
>  # Input is taken from the "${target}" variable.
>  
> diff --git a/gdb/nat/linux-maps.c b/gdb/nat/linux-maps.c
> new file mode 100644
> index 0000000..01c8836
> --- /dev/null
> +++ b/gdb/nat/linux-maps.c
> @@ -0,0 +1,20 @@
> +/* Linux-specific memory maps manipulation routines.
> +   Copyright (C) 2015 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/>.  */
> +
> +#include "common-defs.h"
> +#include "linux-maps.h"
> diff --git a/gdb/nat/linux-maps.h b/gdb/nat/linux-maps.h
> new file mode 100644
> index 0000000..2cff321
> --- /dev/null
> +++ b/gdb/nat/linux-maps.h
> @@ -0,0 +1,22 @@
> +/* Linux-specific memory maps manipulation routines.
> +   Copyright (C) 2015 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/>.  */
> +
> +#ifndef NAT_LINUX_MAPS_H
> +#define NAT_LINUX_MAPS_H
> +
> +#endif /* NAT_LINUX_MAPS_H */
> diff --git a/gdb/target/target-utils.c b/gdb/target/target-utils.c
> new file mode 100644
> index 0000000..4e8fae7
> --- /dev/null
> +++ b/gdb/target/target-utils.c
> @@ -0,0 +1,21 @@
> +/* Utility target functions for GDB, and GDBserver.
> +
> +   Copyright (C) 2015 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/>.  */
> +
> +#include "common-defs.h"
> +#include "target/target-utils.h"
> diff --git a/gdb/target/target-utils.h b/gdb/target/target-utils.h
> new file mode 100644
> index 0000000..443ffc3
> --- /dev/null
> +++ b/gdb/target/target-utils.h
> @@ -0,0 +1,23 @@
> +/* Utility target functions for GDB, and GDBserver.
> +
> +   Copyright (C) 2015 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/>.  */
> +
> +#ifndef TARGET_TARGET_UTILS_H
> +#define TARGET_TARGET_UTILS_H
> +
> +#endif /* TARGET_TARGET_UTILS_H */

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

* Re: [PATCH v10 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-07-16  8:14       ` Gary Benson
@ 2015-07-16  8:32         ` Jan Kratochvil
  2015-07-21 10:28         ` Pedro Alves
  1 sibling, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-07-16  8:32 UTC (permalink / raw)
  To: Gary Benson; +Cc: Pedro Alves, gdb-patches, Aleksandar Ristovski

On Thu, 16 Jul 2015 10:14:12 +0200, Gary Benson wrote:
> I don't fully understand the tdep/non-tdep split very well.

I understand that *-tdep.c is all of the support that makes sense for all the
arch support that can be done cross-arch for loading/examining files plus all
the arch support usable with remote gdbserver (cross-arch in general, with
gdbserver it can be also native-arch but that does not matter).

linux-maps.[ch] provides linux_find_memory_regions_full() which is used in
linux-tdep.c and in gdbserver but for mostly unrelated purposes, so it is
rather just a code reuse and it is not architecturally the same functionality
called from both linux-nat+gdbserver just because linux-nat hasn't been
dropped yet.

linux-maps.[ch] also exports its read_mapping() to linux-tdep.c which is again
only arbitrary code reuse from two unrelated places of code.


Jan

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

* [PATCH v11 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-07-15 21:20     ` [PATCH v10 " Jan Kratochvil
  2015-07-16  8:14       ` Gary Benson
@ 2015-07-17 20:51       ` Jan Kratochvil
  1 sibling, 0 replies; 24+ messages in thread
From: Jan Kratochvil @ 2015-07-17 20:51 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches, Aleksandar Ristovski

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

Forgot about ALL_TARGET_OBS:

gdb/ChangeLog
2015-07-15  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Create empty nat/linux-maps.[ch] and common/target-utils.[ch].
	* Makefile.in (ALL_TARGET_OBS): Add linux-maps.o.
	(HFILES_NO_SRCDIR); Add nat/linux-maps.h,
	common/target-utils.h.
	(COMMON_OBS): Add target-utils.o.
	(linux-maps.o, target-utils.o): New.
	* target/target-utils.c: New file.
	* target/target-utils.h: New file.
	* nat/linux-maps.c: New file.
	* nat/linux-maps.h: New file.
	* configure.tgt (aarch64*-*-linux*, alpha*-*-linux*)
	(am33_2.0*-*-linux*, arm*-*-linux*, bfin-*-*linux*, cris*, frv-*-*)
	(hppa*-*-linux*, i[34567]86-*-linux*, ia64-*-linux*, m32r*-*-linux*)
	(m68*-*-linux*, microblaze*-linux-*, microblaze*-*-linux*)
	(mips*-*-linux*, nios2*-*-linux*, powerpc*-*-linux*, s390*-*-linux*)
	(sh*-*-linux*, sparc-*-linux*, sparc64-*-linux*, tic6x-*-*linux)
	(tilegx-*-linux*, x86_64-*-linux*, xtensa*-*-linux*): Add linux-maps.o
	to gdb_target_obs.

gdb/gdbserver/ChangeLog
2015-07-15  Aleksandar Ristovski  <aristovski@qnx.com
	    Jan Kratochvil  <jan.kratochvil@redhat.com>

	Create empty nat/linux-maps.[ch] and common/target-utils.[ch].
	* Makefile.in (OBS): Add target-utils.o.
	(linux-maps.o, target-utils.o): New.
	* configure.srv (srv_linux_obj): Add linux-maps.o.

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

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 4347d0c..154913c 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -718,7 +718,8 @@ ALL_TARGET_OBS = \
 	symfile-mem.o \
 	windows-tdep.o \
 	linux-record.o \
-	ravenscar-thread.o
+	ravenscar-thread.o \
+	linux-maps.o
 
 # Host-dependent makefile fragment comes in here.
 @host_makefile_frag@
@@ -903,7 +904,7 @@ LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
 # right, it is probably easiest just to list .h files here directly.
 
 HFILES_NO_SRCDIR = \
-common/gdb_signals.h nat/gdb_thread_db.h common/gdb_vecs.h \
+common/gdb_signals.h nat/gdb_thread_db.h common/gdb_vecs.h nat/linux-maps.h \
 common/x86-xstate.h nat/linux-ptrace.h nat/mips-linux-watch.h \
 proc-utils.h aarch64-tdep.h arm-tdep.h ax-gdb.h ppcfbsd-tdep.h \
 ppcnbsd-tdep.h cli-out.h gdb_expat.h breakpoint.h infcall.h obsd-tdep.h \
@@ -983,7 +984,7 @@ common/common-debug.h common/cleanups.h common/gdb_setjmp.h \
 common/common-exceptions.h target/target.h common/symbol.h \
 common/common-regcache.h fbsd-tdep.h nat/linux-personality.h \
 common/fileio.h nat/x86-linux.h nat/x86-linux-dregs.h \
-nat/linux-namespaces.h
+nat/linux-namespaces.h target/target-utils.h
 
 # Header files that already have srcdir in them, or which are in objdir.
 
@@ -1083,7 +1084,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	common-utils.o buffer.o ptid.o gdb-dlfcn.o common-agent.o \
 	format.o registry.o btrace.o record-btrace.o waitstatus.o \
 	print-utils.o rsp-low.o errors.o common-debug.o debug.o \
-	common-exceptions.o btrace-common.o fileio.o \
+	common-exceptions.o btrace-common.o fileio.o target-utils.o \
 	$(SUBDIR_GCC_COMPILE_OBS)
 
 TSOBS = inflow.o
@@ -2221,6 +2222,10 @@ common-agent.o: $(srcdir)/common/agent.c
 	$(COMPILE) $(srcdir)/common/agent.c
 	$(POSTCOMPILE)
 
+linux-maps.o: ${srcdir}/nat/linux-maps.c
+	$(COMPILE) $(srcdir)/nat/linux-maps.c
+	$(POSTCOMPILE)
+
 vec.o: ${srcdir}/common/vec.c
 	$(COMPILE) $(srcdir)/common/vec.c
 	$(POSTCOMPILE)
@@ -2237,6 +2242,10 @@ errors.o: ${srcdir}/common/errors.c
 	$(COMPILE) $(srcdir)/common/errors.c
 	$(POSTCOMPILE)
 
+target-utils.o: ${srcdir}/target/target-utils.c
+	$(COMPILE) $(srcdir)/target/target-utils.c
+	$(POSTCOMPILE)
+
 common-debug.o: ${srcdir}/common/common-debug.c
 	$(COMPILE) $(srcdir)/common/common-debug.c
 	$(POSTCOMPILE)
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index f2c1a2d..20873c3 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -45,7 +45,7 @@ aarch64*-*-linux*)
 	# Target: AArch64 linux
 	gdb_target_obs="aarch64-tdep.o aarch64-linux-tdep.o \
 			arm-tdep.o arm-linux-tdep.o \
-			glibc-tdep.o linux-tdep.o solib-svr4.o \
+			glibc-tdep.o linux-tdep.o linux-maps.o solib-svr4.o \
 			symfile-mem.o linux-record.o"
 	build_gdbserver=yes
 	;;
@@ -53,7 +53,7 @@ aarch64*-*-linux*)
 alpha*-*-linux*)
 	# Target: Little-endian Alpha running Linux
 	gdb_target_obs="alpha-tdep.o alpha-mdebug-tdep.o alpha-linux-tdep.o \
-			linux-tdep.o solib-svr4.o"
+			linux-tdep.o linux-maps.o solib-svr4.o"
 	;;
 alpha*-*-freebsd* | alpha*-*-kfreebsd*-gnu)
 	# Target: FreeBSD/alpha
@@ -79,7 +79,7 @@ alpha*-*-*)
 am33_2.0*-*-linux*)
 	# Target: Matsushita mn10300 (AM33) running Linux
 	gdb_target_obs="mn10300-tdep.o mn10300-linux-tdep.o linux-tdep.o \
-			solib-svr4.o"
+			linux-maps.o solib-svr4.o"
 	;;
 
 arm*-wince-pe | arm*-*-mingw32ce*)
@@ -90,7 +90,8 @@ arm*-wince-pe | arm*-*-mingw32ce*)
 arm*-*-linux*)
 	# Target: ARM based machine running GNU/Linux
 	gdb_target_obs="arm-tdep.o arm-linux-tdep.o glibc-tdep.o \
-			solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o"
+			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o \
+			linux-record.o"
 	build_gdbserver=yes
 	;;
 arm*-*-netbsd* | arm*-*-knetbsd*-gnu)
@@ -120,7 +121,7 @@ avr-*-*)
 
 bfin-*-*linux*)
 	# Target: Blackfin Linux
-	gdb_target_obs="bfin-tdep.o bfin-linux-tdep.o linux-tdep.o"
+	gdb_target_obs="bfin-tdep.o bfin-linux-tdep.o linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/bfin/libsim.a
 	build_gdbserver=yes
 	;;
@@ -132,12 +133,14 @@ bfin-*-*)
 
 cris*)
 	# Target: CRIS
-	gdb_target_obs="cris-tdep.o cris-linux-tdep.o linux-tdep.o solib-svr4.o"
+	gdb_target_obs="cris-tdep.o cris-linux-tdep.o linux-tdep.o \
+			linux-maps.o solib-svr4.o"
 	;;
 
 frv-*-*)
 	# Target: Fujitsu FRV processor
-	gdb_target_obs="frv-tdep.o frv-linux-tdep.o linux-tdep.o solib-frv.o"
+	gdb_target_obs="frv-tdep.o frv-linux-tdep.o linux-tdep.o linux-maps.o \
+			solib-frv.o"
 	gdb_sim=../sim/frv/libsim.a
 	;;
 
@@ -155,7 +158,7 @@ h8300-*-*)
 hppa*-*-linux*)
 	# Target: HP PA-RISC running Linux
 	gdb_target_obs="hppa-tdep.o hppa-linux-tdep.o glibc-tdep.o \
-			linux-tdep.o solib-svr4.o symfile-mem.o"
+			linux-tdep.o linux-maps.o solib-svr4.o symfile-mem.o"
 	;;
 hppa*-*-netbsd*)
 	# Target: NetBSD/hppa
@@ -218,7 +221,7 @@ i[34567]86-*-linux*)
 	# Target: Intel 386 running GNU/Linux
 	gdb_target_obs="i386-tdep.o i386-linux-tdep.o glibc-tdep.o i387-tdep.o \
 			solib-svr4.o symfile-mem.o \
-			linux-tdep.o linux-record.o"
+			linux-tdep.o linux-maps.o linux-record.o"
 	if test "x$enable_64_bit_bfd" = "xyes"; then
 	    # Target: GNU/Linux x86-64
 	    gdb_target_obs="amd64-tdep.o amd64-linux-tdep.o ${gdb_target_obs}"
@@ -249,7 +252,7 @@ i[34567]86-*-*)
 ia64-*-linux*)
 	# Target: Intel IA-64 running GNU/Linux
 	gdb_target_obs="ia64-tdep.o ia64-linux-tdep.o linux-tdep.o \
-			solib-svr4.o symfile-mem.o"
+			linux-maps.o solib-svr4.o symfile-mem.o"
 	build_gdbserver=yes
 	;;
 ia64-*-*vms*)
@@ -283,7 +286,7 @@ m32r*-*-linux*)
 	# Target: Renesas M32R running GNU/Linux
 	gdb_target_obs="m32r-tdep.o m32r-linux-tdep.o remote-m32r-sdi.o \
 			glibc-tdep.o solib-svr4.o symfile-mem.o \
-			linux-tdep.o"
+			linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/m32r/libsim.a
 	build_gdbserver=yes
 	;;
@@ -308,7 +311,7 @@ fido-*-elf*)
 m68*-*-linux*)
 	# Target: Motorola m68k with a.out and ELF
 	gdb_target_obs="m68k-tdep.o m68klinux-tdep.o solib-svr4.o \
-			linux-tdep.o glibc-tdep.o symfile-mem.o"
+			linux-tdep.o linux-maps.o glibc-tdep.o symfile-mem.o"
 	build_gdbserver=yes
 	;;
 m68*-*-netbsd* | m68*-*-knetbsd*-gnu)
@@ -334,7 +337,8 @@ mep-*-*)
 microblaze*-linux-*|microblaze*-*-linux*)
 	# Target: Xilinx MicroBlaze running Linux
 	gdb_target_obs="microblaze-tdep.o microblaze-linux-tdep.o microblaze-rom.o \
-			monitor.o dsrec.o solib-svr4.o symfile-mem.o linux-tdep.o"
+			monitor.o dsrec.o solib-svr4.o symfile-mem.o \
+			linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/microblaze/libsim.a
 	;;
 microblaze*-*-*)
@@ -346,7 +350,7 @@ microblaze*-*-*)
 mips*-*-linux*)
 	# Target: Linux/MIPS
 	gdb_target_obs="mips-tdep.o mips-linux-tdep.o glibc-tdep.o \
-			solib-svr4.o symfile-mem.o linux-tdep.o"
+			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/mips/libsim.a
 	build_gdbserver=yes
 	;;
@@ -394,7 +398,7 @@ mt-*-*)
 nios2*-*-linux*)
 	# Target: Altera Nios II running Linux
 	gdb_target_obs="nios2-tdep.o nios2-linux-tdep.o solib-svr4.o \
-			symfile-mem.o glibc-tdep.o linux-tdep.o"
+			symfile-mem.o glibc-tdep.o linux-tdep.o linux-maps.o"
 	;;
 
 nios2*-*-*)
@@ -433,7 +437,7 @@ powerpc*-*-linux*)
 	gdb_target_obs="rs6000-tdep.o ppc-linux-tdep.o ppc-sysv-tdep.o \
 			ppc64-tdep.o solib-svr4.o solib-spu.o \
 			spu-multiarch.o \
-			glibc-tdep.o symfile-mem.o linux-tdep.o \
+			glibc-tdep.o symfile-mem.o linux-tdep.o linux-maps.o \
 			ravenscar-thread.o ppc-ravenscar-thread.o \
 			linux-record.o "
 	gdb_sim=../sim/ppc/libsim.a
@@ -457,7 +461,7 @@ powerpc*-*-*)
 
 s390*-*-linux*)
 	# Target: S390 running Linux
-	gdb_target_obs="s390-linux-tdep.o solib-svr4.o linux-tdep.o"
+	gdb_target_obs="s390-linux-tdep.o solib-svr4.o linux-tdep.o linux-maps.o"
 	build_gdbserver=yes
 	;;
 
@@ -486,7 +490,7 @@ sh*-*-linux*)
 	# Target: GNU/Linux Super-H
 	gdb_target_obs="sh-tdep.o sh64-tdep.o sh-linux-tdep.o monitor.o \
 			dsrec.o solib-svr4.o symfile-mem.o \
-			glibc-tdep.o linux-tdep.o"
+			glibc-tdep.o linux-tdep.o linux-maps.o"
 	gdb_sim=../sim/sh/libsim.a
 	build_gdbserver=yes
 	;;
@@ -514,7 +518,7 @@ sparc-*-linux*)
 	# Target: GNU/Linux SPARC
 	gdb_target_obs="sparc-tdep.o sparc-sol2-tdep.o sol2-tdep.o \
 			sparc-linux-tdep.o solib-svr4.o symfile-mem.o \
-			linux-tdep.o \
+			linux-tdep.o linux-maps.o \
 			ravenscar-thread.o sparc-ravenscar-thread.o"
 	if test "x$enable_64_bit_bfd" = "xyes"; then
 	    # Target: GNU/Linux UltraSPARC
@@ -528,7 +532,8 @@ sparc64-*-linux*)
 	gdb_target_obs="sparc64-tdep.o sparc64-sol2-tdep.o sol2-tdep.o \
 			sparc64-linux-tdep.o sparc-tdep.o sparc-sol2-tdep.o \
 			sparc-linux-tdep.o solib-svr4.o linux-tdep.o \
-			ravenscar-thread.o sparc-ravenscar-thread.o"
+			linux-maps.o ravenscar-thread.o \
+			sparc-ravenscar-thread.o"
 	build_gdbserver=yes
 	;;
 sparc*-*-freebsd* | sparc*-*-kfreebsd*-gnu)
@@ -595,7 +600,7 @@ spu*-*-*)
 tic6x-*-*linux)
 	# Target: GNU/Linux TI C6x
 	gdb_target_obs="tic6x-tdep.o tic6x-linux-tdep.o solib-dsbt.o \
-			glibc-tdep.o linux-tdep.o"
+			glibc-tdep.o linux-tdep.o linux-maps.o"
 	;;
 
 tic6x-*-*)
@@ -606,7 +611,7 @@ tic6x-*-*)
 tilegx-*-linux*)
 	# Target: TILE-Gx
 	gdb_target_obs="tilegx-tdep.o tilegx-linux-tdep.o solib-svr4.o \
-			symfile-mem.o glibc-tdep.o linux-tdep.o"
+			symfile-mem.o glibc-tdep.o linux-tdep.o linux-maps.o"
 	build_gdbserver=yes
 	;;
 
@@ -656,7 +661,8 @@ x86_64-*-linux*)
 	# Target: GNU/Linux x86-64
 	gdb_target_obs="amd64-tdep.o amd64-linux-tdep.o i386-tdep.o \
 			i387-tdep.o i386-linux-tdep.o glibc-tdep.o \
-			solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o"
+			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o \
+			linux-record.o"
 	build_gdbserver=yes
 	;;
 x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu)
@@ -686,7 +692,7 @@ x86_64-*-openbsd*)
 xtensa*-*-linux*)	gdb_target=linux
 	# Target: GNU/Linux Xtensa
 	gdb_target_obs="xtensa-tdep.o xtensa-config.o xtensa-linux-tdep.o \
-			solib-svr4.o symfile-mem.o linux-tdep.o"
+			solib-svr4.o symfile-mem.o linux-tdep.o linux-maps.o"
 	build_gdbserver=yes
 	;;
 xtensa*)
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index 5e04e530..c35c837 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -194,7 +194,7 @@ OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o \
       mem-break.o hostio.o event-loop.o tracepoint.o xml-utils.o \
       common-utils.o ptid.o buffer.o format.o filestuff.o dll.o notif.o \
       tdesc.o print-utils.o rsp-low.o errors.o common-debug.o cleanups.o \
-      common-exceptions.o symbol.o btrace-common.o fileio.o \
+      common-exceptions.o symbol.o btrace-common.o fileio.o target-utils.o \
       $(XML_BUILTIN) $(DEPFILES) $(LIBOBJS)
 GDBREPLAY_OBS = gdbreplay.o version.o
 GDBSERVER_LIBS = @GDBSERVER_LIBS@
@@ -519,6 +519,9 @@ ax.o: ax.c
 signals.o: ../common/signals.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+linux-maps.o: ../nat/linux-maps.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 print-utils.o: ../common/print-utils.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
@@ -531,6 +534,9 @@ common-utils.o: ../common/common-utils.c
 posix-strerror.o: ../common/posix-strerror.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+target-utils.o: ../target/target-utils.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 mingw-strerror.o: ../common/mingw-strerror.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
diff --git a/gdb/gdbserver/configure.srv b/gdb/gdbserver/configure.srv
index 2dc954b..441d051 100644
--- a/gdb/gdbserver/configure.srv
+++ b/gdb/gdbserver/configure.srv
@@ -42,7 +42,7 @@ srv_amd64_linux_xmlfiles="i386/amd64-linux.xml i386/amd64-avx-linux.xml i386/amd
 
 # Linux object files.  This is so we don't have to repeat
 # these files over and over again.
-srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o linux-namespaces.o"
+srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-maps.o linux-ptrace.o linux-waitpid.o linux-personality.o linux-namespaces.o"
 
 # Input is taken from the "${target}" variable.
 
diff --git a/gdb/nat/linux-maps.c b/gdb/nat/linux-maps.c
new file mode 100644
index 0000000..01c8836
--- /dev/null
+++ b/gdb/nat/linux-maps.c
@@ -0,0 +1,20 @@
+/* Linux-specific memory maps manipulation routines.
+   Copyright (C) 2015 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/>.  */
+
+#include "common-defs.h"
+#include "linux-maps.h"
diff --git a/gdb/nat/linux-maps.h b/gdb/nat/linux-maps.h
new file mode 100644
index 0000000..2cff321
--- /dev/null
+++ b/gdb/nat/linux-maps.h
@@ -0,0 +1,22 @@
+/* Linux-specific memory maps manipulation routines.
+   Copyright (C) 2015 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/>.  */
+
+#ifndef NAT_LINUX_MAPS_H
+#define NAT_LINUX_MAPS_H
+
+#endif /* NAT_LINUX_MAPS_H */
diff --git a/gdb/target/target-utils.c b/gdb/target/target-utils.c
new file mode 100644
index 0000000..4e8fae7
--- /dev/null
+++ b/gdb/target/target-utils.c
@@ -0,0 +1,21 @@
+/* Utility target functions for GDB, and GDBserver.
+
+   Copyright (C) 2015 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/>.  */
+
+#include "common-defs.h"
+#include "target/target-utils.h"
diff --git a/gdb/target/target-utils.h b/gdb/target/target-utils.h
new file mode 100644
index 0000000..443ffc3
--- /dev/null
+++ b/gdb/target/target-utils.h
@@ -0,0 +1,23 @@
+/* Utility target functions for GDB, and GDBserver.
+
+   Copyright (C) 2015 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/>.  */
+
+#ifndef TARGET_TARGET_UTILS_H
+#define TARGET_TARGET_UTILS_H
+
+#endif /* TARGET_TARGET_UTILS_H */

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

* Re: [PATCH v10 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch]
  2015-07-16  8:14       ` Gary Benson
  2015-07-16  8:32         ` Jan Kratochvil
@ 2015-07-21 10:28         ` Pedro Alves
  1 sibling, 0 replies; 24+ messages in thread
From: Pedro Alves @ 2015-07-21 10:28 UTC (permalink / raw)
  To: Gary Benson, Jan Kratochvil; +Cc: gdb-patches, Aleksandar Ristovski

On 07/16/2015 09:14 AM, Gary Benson wrote:
> Jan Kratochvil wrote:
>> On Mon, 08 Jun 2015 10:37:33 +0200, Gary Benson wrote:
>>> A bunch of stuff has changed in the way common code is laid out
>>> since May 2014, so while Tom approved this back then, it's not
>>> suitable any more.  Please update the series as follows:
>>>
>>>> 	* common/linux-maps.c: New file.
>>>> 	* common/linux-maps.h: New file.
>>>
>>> Nothing os-specific should be in common.  These files should be
>>> nat/linux-maps.[ch].
>>
>> While I have done so I do not share this opinion.  linux-maps.[ch]
>> is used from linux-tdep.c and not from linux-nat.c.  linux-tdep.c is
>> using common/* files a lot but it uses no function from nat/*:
>>
>> 	$ for i in $(for i in common/*.c;do nm `basename $i .c`.o;done|sed -n 's/^.* T //p');do grep -q "\<$i (" linux-tdep.c && echo $i;done|wc -l
>> 	17
>> 	$ for i in $(for i in    nat/*.c;do nm `basename $i .c`.o;done|sed -n 's/^.* T //p');do grep -q "\<$i (" linux-tdep.c && echo $i;done|wc -l
>> 	0
>> 	(commands above assume in-src-tree build)
>>
>> Additionally linux-maps.o should not be in config/**.mh::NATDEPFILES
>> (like linux-namespaces.o from nat/ ) but it needs to be in
>> configure.tgt instead.
>>
>> Should be linux-maps.[ch] therefore in nat/ or in common/ ?
>> If it matters, though.
> 
> I don't fully understand the tdep/non-tdep split very well.
> Pedro, could you comment?

TL;DR: roughly, nat/ holds native target_ops code, while tdep
files hold gdbarch code.

The idea of nat/ is to hold host-dependent code that implements
the native target (target_ops) backends.  Code in that directory makes use
of native system debug APIs, constants, etc..  Such code cannot be used
on a cross debugger that targets the OS the code is for, because it
e.g., relies on headers that will only exist on that target OS.
nat/ code is used by either gdb's native target_ops target, or, gdbserver's.
E.g., a --host=mingw32 --target=x86_64-linux gdb can't build the native
Linux code in nat/.

In contrast, the tdep files files contain code that _must_ be
host-independent.  The same --host=mingw32 --target=x86_64-linux gdb
build includes the linux-specific -tdep files.

A -tdep.c file including a "nat/foo.h" header thus raises
the abstraction-violation alarm bell.  It's at the same level of
a -tdep.c file including <sys/ptrace.h>.

So seems to me this maps code should not be in nat/.

Thanks,
Pedro Alves

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

end of thread, other threads:[~2015-07-21 10:28 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-07 20:04 [PATCH v6 00/10] Validate binary before use Jan Kratochvil
2015-06-07 20:04 ` [PATCH v6 01/10] Move utility functions to common/ Jan Kratochvil
2015-06-07 20:04 ` [PATCH v6 02/10] Merge multiple hex conversions Jan Kratochvil
2015-06-07 20:04 ` [PATCH v6 03/10] Code cleanup: Rename enum -> enum filterflags Jan Kratochvil
2015-06-08  2:39   ` Sergio Durigan Junior
2015-06-07 20:05 ` [PATCH v6 05/10] Move gdb_regex* to common/ Jan Kratochvil
2015-06-07 20:05 ` [PATCH v6 06/10] Prepare linux_find_memory_regions_full & co. for move Jan Kratochvil
2015-06-07 20:05 ` [PATCH v6 10/10] Tests for validate symbol file using build-id Jan Kratochvil
2015-06-07 20:05 ` [PATCH v6 07/10] Move linux_find_memory_regions_full & co Jan Kratochvil
2015-06-08  8:42   ` Gary Benson
2015-06-07 20:05 ` [PATCH v6 08/10] gdbserver build-id attribute generator Jan Kratochvil
2015-06-07 20:05 ` [PATCH v6 09/10] Validate symbol file using build-id Jan Kratochvil
2015-06-07 20:05 ` [PATCH v6 04/10] Create empty common/linux-maps.[ch] and common/target-utils.[ch] Jan Kratochvil
2015-06-08  8:37   ` Gary Benson
2015-06-11 18:48     ` Jan Kratochvil
2015-06-11 19:47       ` Aleksandar Ristovski
2015-06-12 11:26         ` Gary Benson
2015-06-14 19:28     ` Jan Kratochvil
2015-06-23  8:42       ` Gary Benson
2015-07-15 21:20     ` [PATCH v10 " Jan Kratochvil
2015-07-16  8:14       ` Gary Benson
2015-07-16  8:32         ` Jan Kratochvil
2015-07-21 10:28         ` Pedro Alves
2015-07-17 20:51       ` [PATCH v11 " Jan Kratochvil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).