[PATCH v4 19/44] gdb, gdbserver: allow setting null_ptid as general thread

Markus Metzger <[email protected]>
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
When debugging a GPU inferior, we may not have any thread selected.

As a common scenario, when adding a new inferior and attaching to GPU
devices when the host process is just starting, we will have an inferior
representing a GPU device but, since no work has been submitted yet, there
will be no thread, so inferior_ptid == null_ptid.
---
 gdb/remote.c        | 15 ++++++++-------
 gdbserver/server.cc | 17 ++++++-----------
 gdbserver/target.cc |  6 ++++++
 3 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index 50e90ad8440..753b59a0819 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -3490,10 +3490,11 @@ remote_target::program_signals (gdb::array_view<const unsigned char> signals)
     }
 }
 
-/* If PTID is MAGIC_NULL_PTID, don't set any thread.  If PTID is
-   MINUS_ONE_PTID, set the thread to -1, so the stub returns the
-   thread.  If GEN is set, set the general thread, if not, then set
-   the step/continue thread.  */
+/* If PTID is MAGIC_NULL_PTID, NULL_PTID, or ANY_THREAD_PTID don't set any
+   thread and let the remote stub select the thread.  If PTID is
+   MINUS_ONE_PTID, set the thread to -1, so the stub returns the thread.
+   If GEN is set, set the general thread, if not, then set the
+   step/continue thread.  */
 void
 remote_target::set_thread (ptid_t ptid, int gen)
 {
@@ -3507,9 +3508,9 @@ remote_target::set_thread (ptid_t ptid, int gen)
 
   *buf++ = 'H';
   *buf++ = gen ? 'g' : 'c';
-  if (ptid == magic_null_ptid)
-    xsnprintf (buf, endbuf - buf, "0");
-  else if (ptid == any_thread_ptid)
+  if (ptid == magic_null_ptid
+      || ptid == null_ptid
+      || ptid == any_thread_ptid)
     xsnprintf (buf, endbuf - buf, "0");
   else if (ptid == minus_one_ptid)
     xsnprintf (buf, endbuf - buf, "-1");
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index a7e976c0f75..03a40c7a19e 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -4858,14 +4858,8 @@ process_serial_event (void)
 	    {
 	      /* The ptid represents a pid.  */
 	      thread_info *thread = find_any_thread_of_pid (thread_id.pid ());
-
-	      if (thread == NULL)
-		{
-		  write_enn (cs.own_buf);
-		  break;
-		}
-
-	      thread_id = thread->id;
+	      if (thread != nullptr)
+		thread_id = thread->id;
 	    }
 	  else
 	    {
@@ -4885,14 +4879,15 @@ process_serial_event (void)
 		     the currently selected thread is still valid. If
 		     it is not, select the first available.  */
 		  thread_info *thread = find_thread_ptid (cs.general_thread);
-		  if (thread == NULL)
+		  if (thread == nullptr)
 		    thread = get_first_thread ();
-		  thread_id = thread->id;
+
+		  if (thread != nullptr)
+		    thread_id = thread->id;
 		}
 
 	      cs.general_thread = thread_id;
 	      set_desired_thread ();
-	      gdb_assert (current_thread != NULL);
 	    }
 	  else if (cs.own_buf[1] == 'c')
 	    cs.cont_thread = thread_id;
diff --git a/gdbserver/target.cc b/gdbserver/target.cc
index d97d2334344..5f0d9000254 100644
--- a/gdbserver/target.cc
+++ b/gdbserver/target.cc
@@ -34,6 +34,12 @@ bool
 set_desired_thread ()
 {
   client_state &cs = get_client_state ();
+  if (cs.general_thread == null_ptid)
+    {
+      switch_to_thread (nullptr);
+      return false;
+    }
+
   if (cs.general_thread.is_pid ())
     {
       process_info *proc = find_process_pid (cs.general_thread.pid ());
-- 
2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
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.