public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Add support for NetBSD threads in i386-bsd-nat.c
@ 2020-03-17 12:12 Kamil Rytarowski
  2020-03-17 23:44 ` [PATCH v2] " Kamil Rytarowski
  0 siblings, 1 reply; 3+ messages in thread
From: Kamil Rytarowski @ 2020-03-17 12:12 UTC (permalink / raw)
  To: gdb-patches

NetBSD ptrace(2) accepts thread id (LWP) as the 4th argument for threads.

This file is still shared with other targets that use different 4th argument
type, that is always unused.
---
 gdb/i386-bsd-nat.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)

diff --git a/gdb/i386-bsd-nat.c b/gdb/i386-bsd-nat.c
index 4e22013a7a8..fb04b68aa90 100644
--- a/gdb/i386-bsd-nat.c
+++ b/gdb/i386-bsd-nat.c
@@ -34,6 +34,14 @@
 #include "inf-ptrace.h"
 \f

+/* Support for NetBSD threads.  */
+#ifdef __NetBSD__
+# define gdb_ptrace(request, pid, addr) \
+  ptrace (request, pid, addr, regcache->ptid ().lwp ())
+#else
+# define gdb_ptrace(request, pid, addr) ptrace (request, pid, addr, 0)
+#endif
+
 /* In older BSD versions we cannot get at some of the segment
    registers.  FreeBSD for example didn't support the %fs and %gs
    registers until the 3.0 release.  We have autoconf checks for their
@@ -136,7 +144,7 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
     {
       struct reg regs;

-      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (gdb_ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs) == -1)
 	perror_with_name (_("Couldn't get registers"));

       i386bsd_supply_gregset (regcache, &regs);
@@ -149,7 +157,7 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
     {
       register_t base;

-      if (ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
+      if (gdb_ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base) == -1)
 	perror_with_name (_("Couldn't get segment register fs_base"));

       regcache->raw_supply (I386_FSBASE_REGNUM, &base);
@@ -162,7 +170,7 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
     {
       register_t base;

-      if (ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
+      if (gdb_ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base) == -1)
 	perror_with_name (_("Couldn't get segment register gs_base"));

       regcache->raw_supply (I386_GSBASE_REGNUM, &base);
@@ -195,7 +203,7 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)

 #ifdef HAVE_PT_GETXMMREGS
       if (have_ptrace_xmmregs != 0
-	  && ptrace(PT_GETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
+	  && ptrace(PT_GETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs) == 0)
 	{
 	  have_ptrace_xmmregs = 1;
 	  i387_supply_fxsave (regcache, -1, xmmregs);
@@ -204,7 +212,7 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
 	{
 	  have_ptrace_xmmregs = 0;
 #endif
-          if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+          if (gdb_ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs) == -1)
 	    perror_with_name (_("Couldn't get floating point status"));

 	  i387_supply_fsave (regcache, -1, &fpregs);
@@ -226,12 +234,12 @@ i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)
     {
       struct reg regs;

-      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (gdb_ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs) == -1)
         perror_with_name (_("Couldn't get registers"));

       i386bsd_collect_gregset (regcache, &regs, regnum);

-      if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (gdb_ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs) == -1)
         perror_with_name (_("Couldn't write registers"));

       if (regnum != -1)
@@ -245,7 +253,7 @@ i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)

       regcache->raw_collect (I386_FSBASE_REGNUM, &base);

-      if (ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
+      if (gdb_ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base) == -1)
 	perror_with_name (_("Couldn't write segment register fs_base"));
       if (regnum != -1)
 	return;
@@ -258,7 +266,7 @@ i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)

       regcache->raw_collect (I386_GSBASE_REGNUM, &base);

-      if (ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
+      if (gdb_ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base) == -1)
 	perror_with_name (_("Couldn't write segment register gs_base"));
       if (regnum != -1)
 	return;
@@ -293,25 +301,25 @@ i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)

 #ifdef HAVE_PT_GETXMMREGS
       if (have_ptrace_xmmregs != 0
-	  && ptrace(PT_GETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
+	  && ptrace(PT_GETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs) == 0)
 	{
 	  have_ptrace_xmmregs = 1;

 	  i387_collect_fxsave (regcache, regnum, xmmregs);

-	  if (ptrace (PT_SETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
+	  if (gdb_ptrace (PT_SETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs) == -1)
             perror_with_name (_("Couldn't write XMM registers"));
 	}
       else
 	{
 	  have_ptrace_xmmregs = 0;
 #endif
-          if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+          if (gdb_ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs) == -1)
 	    perror_with_name (_("Couldn't get floating point status"));

           i387_collect_fsave (regcache, regnum, &fpregs);

-          if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+          if (gdb_ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs) == -1)
 	    perror_with_name (_("Couldn't write floating point status"));
 #ifdef HAVE_PT_GETXMMREGS
         }
--
2.25.0


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

* [PATCH v2] Add support for NetBSD threads in i386-bsd-nat.c
  2020-03-17 12:12 [PATCH] Add support for NetBSD threads in i386-bsd-nat.c Kamil Rytarowski
@ 2020-03-17 23:44 ` Kamil Rytarowski
  2020-03-18  0:57   ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Kamil Rytarowski @ 2020-03-17 23:44 UTC (permalink / raw)
  To: gdb-patches

NetBSD ptrace(2) accepts thread id (LWP) as the 4th argument for threads.

Define gdb_ptrace() a wrapper function for ptrace(2) that properly passes
the pid,lwp pair on NetBSD and the result of get_ptrace_pid() for others.

gdb/ChangeLog:

	* i386-bsd-nat.c (gdb_ptrace): New.
	* i386-bsd-nat.c (i386bsd_fetch_inferior_registers)
	(i386bsd_store_inferior_registers) Switch from pid_t to ptid_t.
	* i386-bsd-nat.c (i386bsd_fetch_inferior_registers)
	(i386bsd_store_inferior_registers) Use gdb_ptrace.
---
 gdb/i386-bsd-nat.c | 63 ++++++++++++++++++++++++++++++----------------
 1 file changed, 42 insertions(+), 21 deletions(-)

diff --git a/gdb/i386-bsd-nat.c b/gdb/i386-bsd-nat.c
index 4e22013a7a8..9f4a7bd388d 100644
--- a/gdb/i386-bsd-nat.c
+++ b/gdb/i386-bsd-nat.c
@@ -34,6 +34,21 @@
 #include "inf-ptrace.h"
 \f

+static int
+gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
+	    PTRACE_TYPE_ARG4 data)
+{
+#ifdef __NetBSD__
+  gdb_assert(data == 0);
+  /* Support for NetBSD threads: unlike other ptrace implementations in this
+     file, NetBSD requires that we pass both the pid and lwp.  */
+  return ptrace (request, ptid.pid (), addr, ptid.lwp ());
+#else
+  pid_t pid = get_ptrace_pid (ptid);
+  return ptrace (request, pid, addr, data);
+#endif
+}
+
 /* In older BSD versions we cannot get at some of the segment
    registers.  FreeBSD for example didn't support the %fs and %gs
    registers until the 3.0 release.  We have autoconf checks for their
@@ -130,13 +145,13 @@ i386bsd_collect_gregset (const struct regcache *regcache,
 void
 i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
 {
-  pid_t pid = get_ptrace_pid (regcache->ptid ());
+  ptid_t ptid = regcache->ptid ();

   if (regnum == -1 || GETREGS_SUPPLIES (regnum))
     {
       struct reg regs;

-      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (gdb_ptrace (PT_GETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
 	perror_with_name (_("Couldn't get registers"));

       i386bsd_supply_gregset (regcache, &regs);
@@ -149,7 +164,7 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
     {
       register_t base;

-      if (ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
+      if (gdb_ptrace (PT_GETFSBASE, ptid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
 	perror_with_name (_("Couldn't get segment register fs_base"));

       regcache->raw_supply (I386_FSBASE_REGNUM, &base);
@@ -162,7 +177,7 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
     {
       register_t base;

-      if (ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
+      if (gdb_ptrace (PT_GETGSBASE, ptid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
 	perror_with_name (_("Couldn't get segment register gs_base"));

       regcache->raw_supply (I386_GSBASE_REGNUM, &base);
@@ -184,8 +199,8 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
 	  void *xstateregs;

 	  xstateregs = alloca (x86bsd_xsave_len);
-	  if (ptrace (PT_GETXSTATE, pid,
-		      (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
+	  if (gdb_ptrace (PT_GETXSTATE, ptid,
+			  (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
 	    perror_with_name (_("Couldn't get extended state status"));

 	  i387_supply_xsave (regcache, -1, xstateregs);
@@ -195,7 +210,8 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)

 #ifdef HAVE_PT_GETXMMREGS
       if (have_ptrace_xmmregs != 0
-	  && ptrace(PT_GETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
+	  && gdb_ptrace(PT_GETXMMREGS, ptid,
+			(PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
 	{
 	  have_ptrace_xmmregs = 1;
 	  i387_supply_fxsave (regcache, -1, xmmregs);
@@ -204,7 +220,8 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
 	{
 	  have_ptrace_xmmregs = 0;
 #endif
-          if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+          if (gdb_ptrace (PT_GETFPREGS, ptid,
+			  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	    perror_with_name (_("Couldn't get floating point status"));

 	  i387_supply_fsave (regcache, -1, &fpregs);
@@ -220,18 +237,18 @@ i386bsd_fetch_inferior_registers (struct regcache *regcache, int regnum)
 void
 i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)
 {
-  pid_t pid = get_ptrace_pid (regcache->ptid ());
+  ptid_t ptid = regcache->ptid ();

   if (regnum == -1 || GETREGS_SUPPLIES (regnum))
     {
       struct reg regs;

-      if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (gdb_ptrace (PT_GETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
         perror_with_name (_("Couldn't get registers"));

       i386bsd_collect_gregset (regcache, &regs, regnum);

-      if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
+      if (gdb_ptrace (PT_SETREGS, ptid, (PTRACE_TYPE_ARG3) &regs, 0) == -1)
         perror_with_name (_("Couldn't write registers"));

       if (regnum != -1)
@@ -245,7 +262,7 @@ i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)

       regcache->raw_collect (I386_FSBASE_REGNUM, &base);

-      if (ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
+      if (gdb_ptrace (PT_SETFSBASE, ptid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
 	perror_with_name (_("Couldn't write segment register fs_base"));
       if (regnum != -1)
 	return;
@@ -258,7 +275,7 @@ i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)

       regcache->raw_collect (I386_GSBASE_REGNUM, &base);

-      if (ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
+      if (gdb_ptrace (PT_SETGSBASE, ptid, (PTRACE_TYPE_ARG3) &base, 0) == -1)
 	perror_with_name (_("Couldn't write segment register gs_base"));
       if (regnum != -1)
 	return;
@@ -278,14 +295,14 @@ i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)
 	  void *xstateregs;

 	  xstateregs = alloca (x86bsd_xsave_len);
-	  if (ptrace (PT_GETXSTATE, pid,
-		      (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
+	  if (gdb_ptrace (PT_GETXSTATE, ptid,
+			  (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
 	    perror_with_name (_("Couldn't get extended state status"));

 	  i387_collect_xsave (regcache, -1, xstateregs, 0);

-	  if (ptrace (PT_SETXSTATE, pid,
-		      (PTRACE_TYPE_ARG3) xstateregs, x86bsd_xsave_len) == -1)
+	  if (gdb_ptrace (PT_SETXSTATE, ptid, (PTRACE_TYPE_ARG3) xstateregs,
+			  x86bsd_xsave_len) == -1)
 	    perror_with_name (_("Couldn't write extended state status"));
 	  return;
 	}
@@ -293,25 +310,29 @@ i386bsd_store_inferior_registers (struct regcache *regcache, int regnum)

 #ifdef HAVE_PT_GETXMMREGS
       if (have_ptrace_xmmregs != 0
-	  && ptrace(PT_GETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
+	  && gdb_ptrace(PT_GETXMMREGS, ptid,
+			(PTRACE_TYPE_ARG3) xmmregs, 0) == 0)
 	{
 	  have_ptrace_xmmregs = 1;

 	  i387_collect_fxsave (regcache, regnum, xmmregs);

-	  if (ptrace (PT_SETXMMREGS, pid, (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
+	  if (gdb_ptrace (PT_SETXMMREGS, ptid,
+			  (PTRACE_TYPE_ARG3) xmmregs, 0) == -1)
             perror_with_name (_("Couldn't write XMM registers"));
 	}
       else
 	{
 	  have_ptrace_xmmregs = 0;
 #endif
-          if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+          if (gdb_ptrace (PT_GETFPREGS, ptid,
+			  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	    perror_with_name (_("Couldn't get floating point status"));

           i387_collect_fsave (regcache, regnum, &fpregs);

-          if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
+          if (gdb_ptrace (PT_SETFPREGS, ptid,
+			  (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
 	    perror_with_name (_("Couldn't write floating point status"));
 #ifdef HAVE_PT_GETXMMREGS
         }
--
2.25.0


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

* Re: [PATCH v2] Add support for NetBSD threads in i386-bsd-nat.c
  2020-03-17 23:44 ` [PATCH v2] " Kamil Rytarowski
@ 2020-03-18  0:57   ` Simon Marchi
  0 siblings, 0 replies; 3+ messages in thread
From: Simon Marchi @ 2020-03-18  0:57 UTC (permalink / raw)
  To: Kamil Rytarowski, gdb-patches

On 2020-03-17 7:44 p.m., Kamil Rytarowski wrote:
> NetBSD ptrace(2) accepts thread id (LWP) as the 4th argument for threads.
> 
> Define gdb_ptrace() a wrapper function for ptrace(2) that properly passes
> the pid,lwp pair on NetBSD and the result of get_ptrace_pid() for others.
> 
> gdb/ChangeLog:
> 
> 	* i386-bsd-nat.c (gdb_ptrace): New.
> 	* i386-bsd-nat.c (i386bsd_fetch_inferior_registers)
> 	(i386bsd_store_inferior_registers) Switch from pid_t to ptid_t.
> 	* i386-bsd-nat.c (i386bsd_fetch_inferior_registers)
> 	(i386bsd_store_inferior_registers) Use gdb_ptrace.
> ---
>  gdb/i386-bsd-nat.c | 63 ++++++++++++++++++++++++++++++----------------
>  1 file changed, 42 insertions(+), 21 deletions(-)
> 
> diff --git a/gdb/i386-bsd-nat.c b/gdb/i386-bsd-nat.c
> index 4e22013a7a8..9f4a7bd388d 100644
> --- a/gdb/i386-bsd-nat.c
> +++ b/gdb/i386-bsd-nat.c
> @@ -34,6 +34,21 @@
>  #include "inf-ptrace.h"
>  \f
> 
> +static int
> +gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr,
> +	    PTRACE_TYPE_ARG4 data)
> +{
> +#ifdef __NetBSD__
> +  gdb_assert(data == 0);

Space before parenthesis.

The patch LGTM with that fixed.

Simon

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

end of thread, other threads:[~2020-03-18  0:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 12:12 [PATCH] Add support for NetBSD threads in i386-bsd-nat.c Kamil Rytarowski
2020-03-17 23:44 ` [PATCH v2] " Kamil Rytarowski
2020-03-18  0:57   ` Simon Marchi

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).