[binutils-gdb] Windows gdb+gdbserver: Share $_siginfo reading code

Pedro Alves via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6326101233b73dfcf06a2f4e21065c7a4a18d0d7

commit 6326101233b73dfcf06a2f4e21065c7a4a18d0d7
Author: Pedro Alves <[email protected]>
Date:   Mon May 22 18:17:54 2023 +0100

    Windows gdb+gdbserver: Share $_siginfo reading code
    
    Both GDB and GDBserver have similar code to read the $_siginfo data.
    This patch moves the bulk of it to gdb/nat/windows-nat.c so it can be
    shared.
    
    Approved-By: Tom Tromey <[email protected]>
    Change-Id: I58f9074caf6362274453c78ed1fc9e31249f6854

Diff:
---
 gdb/nat/windows-nat.c  | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/nat/windows-nat.h  |  9 +++++++++
 gdb/windows-nat.c      | 40 +++-------------------------------------
 gdbserver/win32-low.cc | 42 ++++--------------------------------------
 4 files changed, 65 insertions(+), 75 deletions(-)

diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
index 1f8967dcd8d..c6434348607 100644
--- a/gdb/nat/windows-nat.c
+++ b/gdb/nat/windows-nat.c
@@ -153,6 +153,55 @@ windows_thread_info::thread_name ()
   return name.get ();
 }
 
+/* Read Windows signal info.  See nat/windows-nat.h.  */
+
+bool
+windows_thread_info::xfer_siginfo (gdb_byte *readbuf,
+				   ULONGEST offset, ULONGEST len,
+				   ULONGEST *xfered_len)
+{
+  if (last_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
+    return false;
+
+  if (readbuf == nullptr)
+    return false;
+
+  EXCEPTION_RECORD &er = last_event.u.Exception.ExceptionRecord;
+
+  char *buf = (char *) &er;
+  size_t bufsize = sizeof (er);
+
+#ifdef __x86_64__
+  EXCEPTION_RECORD32 er32;
+  if (proc->wow64_process)
+    {
+      buf = (char *) &er32;
+      bufsize = sizeof (er32);
+
+      er32.ExceptionCode = er.ExceptionCode;
+      er32.ExceptionFlags = er.ExceptionFlags;
+      er32.ExceptionRecord
+	= (uintptr_t) er.ExceptionRecord;
+      er32.ExceptionAddress
+	= (uintptr_t) er.ExceptionAddress;
+      er32.NumberParameters = er.NumberParameters;
+      for (int i = 0; i < EXCEPTION_MAXIMUM_PARAMETERS; i++)
+	er32.ExceptionInformation[i] = er.ExceptionInformation[i];
+    }
+#endif
+
+  if (offset > bufsize)
+    return false;
+
+  if (offset + len > bufsize)
+    len = bufsize - offset;
+
+  memcpy (readbuf, buf + offset, len);
+  *xfered_len = len;
+
+  return true;
+}
+
 /* Try to determine the executable filename.
 
    EXE_NAME_RET is a pointer to a buffer whose size is EXE_NAME_MAX_LEN.
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 59662c3570c..cb7456c624e 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -76,6 +76,15 @@ struct windows_thread_info
      the next call.  */
   const char *thread_name ();
 
+  /* Read LEN bytes of the thread's $_siginfo into READBUF, starting
+     at OFFSET.  Store the number of actually-read bytes in
+     XFERED_LEN.  Returns true on success, false on failure.  Passing
+     READBUF as NULL indicates that the caller is trying to write to
+     $_siginfo, which is a failure case.  */
+  bool xfer_siginfo (gdb_byte *readbuf,
+		     ULONGEST offset, ULONGEST len,
+		     ULONGEST *xfered_len);
+
   /* The process this thread belongs to.  */
   windows_process_info *const proc;
 
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 143a5e487fd..062ecf1e063 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2454,44 +2454,10 @@ windows_xfer_siginfo (gdb_byte *readbuf, ULONGEST offset, ULONGEST len,
 {
   windows_thread_info *th = windows_process->find_thread (inferior_ptid);
 
-  if (th->last_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
-    return TARGET_XFER_E_IO;
-
-  EXCEPTION_RECORD &er = th->last_event.u.Exception.ExceptionRecord;
-
-  char *buf = (char *) &er;
-  size_t bufsize = sizeof (er);
-
-#ifdef __x86_64__
-  EXCEPTION_RECORD32 er32;
-  if (windows_process->wow64_process)
-    {
-      buf = (char *) &er32;
-      bufsize = sizeof (er32);
-
-      er32.ExceptionCode = er.ExceptionCode;
-      er32.ExceptionFlags = er.ExceptionFlags;
-      er32.ExceptionRecord = (uintptr_t) er.ExceptionRecord;
-      er32.ExceptionAddress = (uintptr_t) er.ExceptionAddress;
-      er32.NumberParameters = er.NumberParameters;
-      for (int i = 0; i < EXCEPTION_MAXIMUM_PARAMETERS; i++)
-	er32.ExceptionInformation[i] = er.ExceptionInformation[i];
-    }
-#endif
-
-  if (readbuf == nullptr)
-    return TARGET_XFER_E_IO;
-
-  if (offset > bufsize)
+  if (th->xfer_siginfo (readbuf, offset, len, xfered_len))
+    return TARGET_XFER_OK;
+  else
     return TARGET_XFER_E_IO;
-
-  if (offset + len > bufsize)
-    len = bufsize - offset;
-
-  memcpy (readbuf, buf + offset, len);
-  *xfered_len = len;
-
-  return TARGET_XFER_OK;
 }
 
 enum target_xfer_status
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index bf3490542e2..0741dd24ed5 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -1272,45 +1272,11 @@ win32_process_target::qxfer_siginfo (const char *annex,
 {
   windows_thread_info *th = windows_process.find_thread (current_thread->id);
 
-  if (th->last_event.dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
-    return -1;
-
-  if (readbuf == nullptr)
-    return -1;
-
-  EXCEPTION_RECORD &er = th->last_event.u.Exception.ExceptionRecord;
-
-  char *buf = (char *) &er;
-  size_t bufsize = sizeof (er);
-
-#ifdef __x86_64__
-  EXCEPTION_RECORD32 er32;
-  if (windows_process.wow64_process)
-    {
-      buf = (char *) &er32;
-      bufsize = sizeof (er32);
-
-      er32.ExceptionCode = er.ExceptionCode;
-      er32.ExceptionFlags = er.ExceptionFlags;
-      er32.ExceptionRecord
-	= (uintptr_t) er.ExceptionRecord;
-      er32.ExceptionAddress
-	= (uintptr_t) er.ExceptionAddress;
-      er32.NumberParameters = er.NumberParameters;
-      for (int i = 0; i < EXCEPTION_MAXIMUM_PARAMETERS; i++)
-	er32.ExceptionInformation[i] = er.ExceptionInformation[i];
-    }
-#endif
-
-  if (offset > bufsize)
+  ULONGEST xfered_len;
+  if (th->xfer_siginfo (readbuf, offset, len, &xfered_len))
+    return xfered_len;
+  else
     return -1;
-
-  if (offset + len > bufsize)
-    len = bufsize - offset;
-
-  memcpy (readbuf, buf + offset, len);
-
-  return len;
 }
 
 bool
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.