Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Joel Brobecker <brobecker@adacore.com>
To: Pedro Alves <palves@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 3/3] gdbserver/Windows: crash during connection establishment phase
Date: Mon, 07 May 2018 21:47:00 -0000	[thread overview]
Message-ID: <20180507214700.repkytrxbswql6z2@adacore.com> (raw)
In-Reply-To: <03cdded5-9f4e-fc1a-366a-1be7cd8b1837@redhat.com>

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

Hi Pedro,

What do you think of the following approach? The idea is to make
sure we never forget to set the expedite_regs field when we initialize
the tdesc.

One part that could be up for discussion is how the constants for
x86 and x86-64 targets are being shared. The way it's done, right
now, each unit that includes x86-tdesc.h would end up with its own
static copy of the constants. I don't think it should be a problem
in practice, but if there is a better solution...

gdb/ChangeLog:

        * regformats/regdat.sh: Adjust script, following the addition
        of the new expedite_regs parameter to init_target_desc.

gdb/gdbserver/ChangeLog:

        * tdesc.h (init_target_desc) <expedite_regs>: New parameter.
        * tdesc.c (init_target_desc) <expedite_regs>: New parameter.
        Use it to set the expedite_regs field in the given tdesc.
        * x86-tdesc.h: New file.
        * linux-aarch64-tdesc.c (aarch64_linux_read_description):
        Adjust following the addition of the new expedite_regs parameter
        to init_target_desc.
        * linux-tic6x-low.c (tic6x_read_description): Likewise.
        * linux-x86-tdesc.c: #include "x86-tdesc.h".
        (i386_linux_read_description, amd64_linux_read_description):
        Adjust following the addition of the new expedite_regs parameter
        to init_target_desc.
        * lynx-i386-low.c: #include "x86-tdesc.h".
        (lynx_i386_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.
        * nto-x86-low.c: #include "x86-tdesc.h".
        (nto_x86_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.
        * win32-i386-low.c: #include "x86-tdesc.h".
        (i386_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.

Tested on x86_64-linux, with and without gdbserver. Tested on
x86-windows as well (using GDBserver with AdaCore's gdb-testsuite).
Same results as before.

-- 
Joel

[-- Attachment #2: 0001-gdbserver-Windows-crash-during-connection-establishm.patch --]
[-- Type: text/x-diff, Size: 10432 bytes --]

From 76d0a2d702956bd7aa950b1ab4ce3b9dc6727961 Mon Sep 17 00:00:00 2001
From: Joel Brobecker <brobecker@adacore.com>
Date: Tue, 1 May 2018 17:52:15 -0400
Subject: [PATCH] gdbserver/Windows: crash during connection establishment
 phase

On Windows, starting a new process with GDBserver seeems to work,
in the sense that the program does get started, and GDBserver
confirms that it is listening for GDB to connect. However, as soon as
GDB establishes the connection with GDBserver, and starts discussing
with it, GDBserver crashes, with a SEGV.

This SEGV occurs in remote-utils.c::prepare_resume_reply...

  | regp = current_target_desc ()->expedite_regs;
  | [...]
  | while (*regp)

... because, in our case, REGP is NULL.

This patches fixes the issues by adding a parameter to init_target_desc,
in order to make sure that we always provide the list of registers when
we initialize a target description.

gdb/ChangeLog:

        * regformats/regdat.sh: Adjust script, following the addition
        of the new expedite_regs parameter to init_target_desc.

gdb/gdbserver/ChangeLog:

        * tdesc.h (init_target_desc) <expedite_regs>: New parameter.
        * tdesc.c (init_target_desc) <expedite_regs>: New parameter.
        Use it to set the expedite_regs field in the given tdesc.
        * x86-tdesc.h: New file.
        * linux-aarch64-tdesc.c (aarch64_linux_read_description):
        Adjust following the addition of the new expedite_regs parameter
        to init_target_desc.
        * linux-tic6x-low.c (tic6x_read_description): Likewise.
        * linux-x86-tdesc.c: #include "x86-tdesc.h".
        (i386_linux_read_description, amd64_linux_read_description):
        Adjust following the addition of the new expedite_regs parameter
        to init_target_desc.
        * lynx-i386-low.c: #include "x86-tdesc.h".
        (lynx_i386_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.
        * nto-x86-low.c: #include "x86-tdesc.h".
        (nto_x86_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.
        * win32-i386-low.c: #include "x86-tdesc.h".
        (i386_arch_setup): Adjust following the addition of the new
        expedite_regs parameter to init_target_desc.
---
 gdb/gdbserver/linux-aarch64-tdesc.c |  6 +-----
 gdb/gdbserver/linux-tic6x-low.c     |  4 +---
 gdb/gdbserver/linux-x86-tdesc.c     | 15 +++------------
 gdb/gdbserver/lynx-i386-low.c       |  3 ++-
 gdb/gdbserver/nto-x86-low.c         |  3 ++-
 gdb/gdbserver/tdesc.c               |  7 ++++++-
 gdb/gdbserver/tdesc.h               |  6 ++++--
 gdb/gdbserver/win32-i386-low.c      |  5 ++++-
 gdb/gdbserver/x86-tdesc.h           | 26 ++++++++++++++++++++++++++
 gdb/regformats/regdat.sh            |  3 +--
 10 files changed, 50 insertions(+), 28 deletions(-)
 create mode 100755 gdb/gdbserver/x86-tdesc.h

diff --git a/gdb/gdbserver/linux-aarch64-tdesc.c b/gdb/gdbserver/linux-aarch64-tdesc.c
index 9f7b9e5..f433038 100644
--- a/gdb/gdbserver/linux-aarch64-tdesc.c
+++ b/gdb/gdbserver/linux-aarch64-tdesc.c
@@ -34,12 +34,8 @@ aarch64_linux_read_description ()
     {
       *tdesc = aarch64_create_target_description ();
 
-      init_target_desc (*tdesc);
-
-#ifndef IN_PROCESS_AGENT
       static const char *expedite_regs_aarch64[] = { "x29", "sp", "pc", NULL };
-      (*tdesc)->expedite_regs = expedite_regs_aarch64;
-#endif
+      init_target_desc (*tdesc, expedite_regs_aarch64);
     }
 
   return *tdesc;
diff --git a/gdb/gdbserver/linux-tic6x-low.c b/gdb/gdbserver/linux-tic6x-low.c
index e1df59f..d90bbcf 100644
--- a/gdb/gdbserver/linux-tic6x-low.c
+++ b/gdb/gdbserver/linux-tic6x-low.c
@@ -199,10 +199,8 @@ tic6x_read_description (enum c6x_feature feature)
   if (*tdesc == NULL)
     {
       *tdesc = tic6x_create_target_description (feature);
-      init_target_desc (*tdesc);
-
       static const char *expedite_regs[] = { "A15", "PC", NULL };
-      (*tdesc)->expedite_regs = expedite_regs;
+      init_target_desc (*tdesc, expedite_regs);
     }
 
   return *tdesc;
diff --git a/gdb/gdbserver/linux-x86-tdesc.c b/gdb/gdbserver/linux-x86-tdesc.c
index 47b8476..358659b 100644
--- a/gdb/gdbserver/linux-x86-tdesc.c
+++ b/gdb/gdbserver/linux-x86-tdesc.c
@@ -25,6 +25,7 @@
 #ifdef __x86_64__
 #include "arch/amd64.h"
 #endif
+#include "x86-tdesc.h"
 
 /* Return the right x86_linux_tdesc index for a given XCR0.  Return
    X86_TDESC_LAST if can't find a match.  */
@@ -88,12 +89,7 @@ i386_linux_read_description (uint64_t xcr0)
     {
       *tdesc = i386_create_target_description (xcr0, true);
 
-      init_target_desc (*tdesc);
-
-#ifndef IN_PROCESS_AGENT
-      static const char *expedite_regs_i386[] = { "ebp", "esp", "eip", NULL };
-      (*tdesc)->expedite_regs = expedite_regs_i386;
-#endif
+      init_target_desc (*tdesc, i386_expedite_regs);
     }
 
   return *tdesc;;
@@ -124,12 +120,7 @@ amd64_linux_read_description (uint64_t xcr0, bool is_x32)
     {
       *tdesc = amd64_create_target_description (xcr0, is_x32, true);
 
-      init_target_desc (*tdesc);
-
-#ifndef IN_PROCESS_AGENT
-      static const char *expedite_regs_amd64[] = { "rbp", "rsp", "rip", NULL };
-      (*tdesc)->expedite_regs = expedite_regs_amd64;
-#endif
+      init_target_desc (*tdesc, amd64_expedite_regs);
     }
   return *tdesc;
 }
diff --git a/gdb/gdbserver/lynx-i386-low.c b/gdb/gdbserver/lynx-i386-low.c
index c7b4fe4..37c0dc9 100644
--- a/gdb/gdbserver/lynx-i386-low.c
+++ b/gdb/gdbserver/lynx-i386-low.c
@@ -21,6 +21,7 @@
 #include <sys/ptrace.h>
 #include "x86-xstate.h"
 #include "arch/i386.h"
+#include "x86-tdesc.h"
 
 /* The following two typedefs are defined in a .h file which is not
    in the standard include path (/sys/include/family/x86/ucontext.h),
@@ -296,7 +297,7 @@ lynx_i386_arch_setup (void)
   struct target_desc *tdesc
     = i386_create_target_description (X86_XSTATE_SSE_MASK, false);
 
-  init_target_desc (tdesc);
+  init_target_desc (tdesc, i386_expedite_regs);
 
   lynx_tdesc = tdesc;
 }
diff --git a/gdb/gdbserver/nto-x86-low.c b/gdb/gdbserver/nto-x86-low.c
index b15c208..81c3c6a 100644
--- a/gdb/gdbserver/nto-x86-low.c
+++ b/gdb/gdbserver/nto-x86-low.c
@@ -25,6 +25,7 @@
 #include <x86/context.h>
 #include "x86-xstate.h"
 #include "arch/i386.h"
+#include "x86-tdesc.h"
 
 const unsigned char x86_breakpoint[] = { 0xCC };
 #define x86_breakpoint_len 1
@@ -90,7 +91,7 @@ nto_x86_arch_setup (void)
   struct target_desc *tdesc
     = i386_create_target_description (X86_XSTATE_SSE_MASK, false);
 
-  init_target_desc (tdesc);
+  init_target_desc (tdesc, i386_expedite_regs);
 
   nto_tdesc = tdesc;
 }
diff --git a/gdb/gdbserver/tdesc.c b/gdb/gdbserver/tdesc.c
index 92524b3..d035083 100644
--- a/gdb/gdbserver/tdesc.c
+++ b/gdb/gdbserver/tdesc.c
@@ -60,7 +60,8 @@ void target_desc::accept (tdesc_element_visitor &v) const
 }
 
 void
-init_target_desc (struct target_desc *tdesc)
+init_target_desc (struct target_desc *tdesc,
+		  const char **expedite_regs)
 {
   int offset = 0;
 
@@ -86,6 +87,10 @@ init_target_desc (struct target_desc *tdesc)
   /* Make sure PBUFSIZ is large enough to hold a full register
      packet.  */
   gdb_assert (2 * tdesc->registers_size + 32 <= PBUFSIZ);
+
+#ifndef IN_PROCESS_AGENT
+  tdesc->expedite_regs = expedite_regs;
+#endif
 }
 
 struct target_desc *
diff --git a/gdb/gdbserver/tdesc.h b/gdb/gdbserver/tdesc.h
index 61a3e4e..989c128 100644
--- a/gdb/gdbserver/tdesc.h
+++ b/gdb/gdbserver/tdesc.h
@@ -82,9 +82,11 @@ public:
 void copy_target_description (struct target_desc *dest,
 			      const struct target_desc *src);
 
-/* Initialize TDESC.  */
+/* Initialize TDESC, and then set its expedite_regs field to
+   EXPEDITE_REGS.  */
 
-void init_target_desc (struct target_desc *tdesc);
+void init_target_desc (struct target_desc *tdesc,
+		       const char **expedite_regs);
 
 /* Return the current inferior's target description.  Never returns
    NULL.  */
diff --git a/gdb/gdbserver/win32-i386-low.c b/gdb/gdbserver/win32-i386-low.c
index a242f72..16fe2c8 100644
--- a/gdb/gdbserver/win32-i386-low.c
+++ b/gdb/gdbserver/win32-i386-low.c
@@ -24,6 +24,7 @@
 #endif
 #include "arch/i386.h"
 #include "tdesc.h"
+#include "x86-tdesc.h"
 
 #ifndef CONTEXT_EXTENDED_REGISTERS
 #define CONTEXT_EXTENDED_REGISTERS 0
@@ -436,11 +437,13 @@ i386_arch_setup (void)
 #ifdef __x86_64__
   tdesc = amd64_create_target_description (X86_XSTATE_SSE_MASK, false,
 						 false);
+  const char **expedite_regs = amd64_expedite_regs;
 #else
   tdesc = i386_create_target_description (X86_XSTATE_SSE_MASK, false);
+  const char **expedite_regs = i386_expedite_regs;
 #endif
 
-  init_target_desc (tdesc);
+  init_target_desc (tdesc, expedite_regs);
 
   win32_tdesc = tdesc;
 }
diff --git a/gdb/gdbserver/x86-tdesc.h b/gdb/gdbserver/x86-tdesc.h
new file mode 100755
index 0000000..c1641b2
--- /dev/null
+++ b/gdb/gdbserver/x86-tdesc.h
@@ -0,0 +1,26 @@
+/* Copyright (C) 2018 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 X86_TDESC_H
+
+/* The "expedite" registers for x86 targets.  */
+static const char *i386_expedite_regs[] = {"ebp", "esp", "eip", NULL};
+
+/* The "expedite" registers for x86_64 targets.  */
+static const char *amd64_expedite_regs[] = {"rbp", "rsp", "rip", NULL};
+
+#endif /* X86_TDESC_H */
diff --git a/gdb/regformats/regdat.sh b/gdb/regformats/regdat.sh
index 8f546fe..5a8564a 100755
--- a/gdb/regformats/regdat.sh
+++ b/gdb/regformats/regdat.sh
@@ -185,11 +185,10 @@ echo
 
 cat <<EOF
 #ifndef IN_PROCESS_AGENT
-  result->expedite_regs = expedite_regs_${name};
   result->xmltarget = xmltarget_${name};
 #endif
 
-  init_target_desc (result);
+  init_target_desc (result, expedite_regs_${name});
 
   tdesc_${name} = result;
 }
-- 
2.1.4


  reply	other threads:[~2018-05-07 21:47 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-04 18:30 [Windows GDBserver] Make GDBserver functional again on Windows Joel Brobecker
2018-05-04 18:30 ` [PATCH 1/3] [gdbserver/win32] fatal "glob could not process pattern '(null)'" error Joel Brobecker
2018-05-09 23:50   ` Pedro Alves
2018-05-10 16:48     ` Joel Brobecker
2018-05-04 18:30 ` [PATCH 3/3] gdbserver/Windows: crash during connection establishment phase Joel Brobecker
2018-05-04 18:46   ` Pedro Alves
2018-05-04 18:58     ` Joel Brobecker
2018-05-04 19:12       ` Pedro Alves
2018-05-07 21:47         ` Joel Brobecker [this message]
2018-05-09 15:54           ` Pedro Alves
2018-05-09 23:09             ` Joel Brobecker
2018-05-09 23:17               ` Pedro Alves
2018-05-10 17:04             ` Joel Brobecker
2018-05-04 20:20   ` Eli Zaretskii
2018-05-04 18:30 ` [PATCH 2/3] gdbserver/Windows: Fix "no program to debug" error Joel Brobecker
2018-05-10  0:09   ` Pedro Alves
2018-05-10 16:50     ` Joel Brobecker
2018-05-04 18:41 ` [Windows GDBserver] Make GDBserver functional again on Windows Sergio Durigan Junior
2018-05-04 18:54   ` Joel Brobecker
2018-05-10 18:19 ` Joel Brobecker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180507214700.repkytrxbswql6z2@adacore.com \
    --to=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox