gdb, remote: fix set_thread () in start_remote ()
authorMarkus Metzger <markus.t.metzger@intel.com>
Fri, 1 Aug 2025 09:53:44 +0000 (09:53 +0000)
committerMarkus Metzger <markus.t.metzger@intel.com>
Mon, 17 Nov 2025 07:12:38 +0000 (07:12 +0000)
remote_target::start_remote_1 () calls set_continue_thread (minus_one_ptid)
with the intent to

      /* Let the stub know that we want it to return the thread.  */
      set_continue_thread (minus_one_ptid);

I interpret it such that it expects a later get_current_thread () to
return the thread selected by the target:

          /* We have thread information; select the thread the target
             says should be current.  If we're reconnecting to a
             multi-threaded program, this will ideally be the thread
             that last reported an event before GDB disconnected.  */
          ptid_t curr_thread = get_current_thread (wait_status);

This results in the packet sequence Hc-1, qC.

Hc simply sets cont_thread:

          else if (cs.own_buf[1] == 'c')
            cs.cont_thread = thread_id;

          write_ok (cs.own_buf);

and qC returns the general thread.  This doesn't match.

It also has some special treatment for null_ptid and minus_one_ptid:

    if (cs.general_thread != null_ptid && cs.general_thread != minus_one_ptid)
      ptid = cs.general_thread;
    else
      {
        init_thread_iter ();
        ptid = thread_iter->id;
      }

Similarly, Hg has some special treatment for null_ptid:

    if (cs.own_buf[1] == 'g')
      {
        if (thread_id == null_ptid)
          {
            /* GDB is telling us to choose any thread.  Check if
               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)
              thread = get_first_thread ();
            thread_id = thread->id;
          }

        cs.general_thread = thread_id;

The comment at Hg matches the intent of GDB for sending Hc-1.

Change the set_thread () call in remote_target::start_remote_1 () to

    set_general_thread (any_thread_ptid);

This results in GDB sending Hg0 and gdbserver preserving the currently
selected thread that is later returned in response to qC.

CC: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
Approved-By: Tom Tromey <tom@tromey.com>
gdb/remote.c

index 4db83bc6c3541fbbe2ef9218b0edd58bfd675b30..80d47c16551757337b59201b30262bfe64ed3353 100644 (file)
@@ -5661,7 +5661,7 @@ remote_target::start_remote_1 (int from_tty, int extended_p)
       target_update_thread_list ();
 
       /* Let the stub know that we want it to return the thread.  */
-      set_continue_thread (minus_one_ptid);
+      set_general_thread (any_thread_ptid);
 
       if (thread_count (this) == 0)
        {
This page took 0.106019 seconds and 5 git commands to generate.