From: Markus Metzger Date: Fri, 1 Aug 2025 09:53:44 +0000 (+0000) Subject: gdb, remote: fix set_thread () in start_remote () X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ef706f09f1e53715810903a1aad5cdd4dc57d249;p=thirdparty%2Fbinutils-gdb.git gdb, remote: fix set_thread () in start_remote () 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 Approved-By: Tom Tromey --- diff --git a/gdb/remote.c b/gdb/remote.c index 4db83bc6c35..80d47c16551 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -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) {