From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1551) id AAEB4388E823; Tue, 3 May 2022 14:24:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AAEB4388E823 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Pedro Alves To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdbserver: track current process as well as current thread X-Act-Checkin: binutils-gdb X-Git-Author: Pedro Alves X-Git-Refname: refs/heads/master X-Git-Oldrev: f4138e8f48948314d1049e713f4b793eec9757ca X-Git-Newrev: 7f8acedeebe295fc8cc1d11ed971cbfc1942618c Message-Id: <20220503142426.AAEB4388E823@sourceware.org> Date: Tue, 3 May 2022 14:24:26 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 May 2022 14:24:26 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D7f8acedeebe2= 95fc8cc1d11ed971cbfc1942618c commit 7f8acedeebe295fc8cc1d11ed971cbfc1942618c Author: Pedro Alves Date: Tue Apr 5 13:57:11 2022 +0100 gdbserver: track current process as well as current thread =20 The recent commit 421490af33bf ("gdbserver/linux: Access memory even if threads are running") caused a regression in gdb.threads/access-mem-running-thread-exit.exp with gdbserver, which I somehow missed. Like so: =20 (gdb) print global_var Cannot access memory at address 0x555555558010 (gdb) FAIL: gdb.threads/access-mem-running-thread-exit.exp: non-stop: = access mem (print global_var after writing, inf=3D2, iter=3D1) =20 The problem starts with GDB telling GDBserver to select a thread, via the Hg packet, which GDBserver complies with, then that thread exits, and GDB, without knowing the thread is gone, tries to write to memory, through the context of the previously selected Hg thread. =20 GDBserver's GDB-facing memory access routines, gdb_read_memory and gdb_write_memory, call set_desired_thread to make GDBserver re-select the thread that GDB has selected with the Hg packet. Since the thread is gone, set_desired_thread returns false, and the memory access fails. =20 Now, to access memory, it doesn't really matter which thread is selected. All we should need is the target process. Even if the thread that GDB previously selected is gone, and GDB does not yet know about that exit, it shouldn't matter, GDBserver should still know which process that thread belonged to. =20 Fix this by making GDBserver track the current process separately, like GDB also does. Add a new set_desired_process routine that is similar to set_desired_thread, but just sets the current process, leaving the current thread as NULL. Use it in the GDB-facing memory read and write routines, to avoid failing if the selected thread is gone, but the process is still around. =20 Change-Id: I4ff97cb6f42558efbed224b30d5c71f6112d44cd Diff: --- gdbserver/gdbthread.h | 1 + gdbserver/inferiors.cc | 26 ++++++++++++++++++++------ gdbserver/server.cc | 4 ++-- gdbserver/target.cc | 44 ++++++++++++++++++++++++++++++++++++++++++-- gdbserver/target.h | 15 ++++++++++++++- 5 files changed, 79 insertions(+), 11 deletions(-) diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h index 10ae1cb7c87..8b897e73d33 100644 --- a/gdbserver/gdbthread.h +++ b/gdbserver/gdbthread.h @@ -252,6 +252,7 @@ public: =20 private: bool m_dont_restore =3D false; + process_info *m_process; thread_info *m_thread; }; =20 diff --git a/gdbserver/inferiors.cc b/gdbserver/inferiors.cc index 678d93c77a1..3d0a8b0e716 100644 --- a/gdbserver/inferiors.cc +++ b/gdbserver/inferiors.cc @@ -26,6 +26,11 @@ std::list all_processes; std::list all_threads; =20 +/* The current process. */ +static process_info *current_process_; + +/* The current thread. This is either a thread of CURRENT_PROCESS, or + NULL. */ struct thread_info *current_thread; =20 /* The current working directory used to start the inferior. @@ -130,6 +135,7 @@ clear_inferiors (void) clear_dlls (); =20 switch_to_thread (nullptr); + current_process_ =3D nullptr; } =20 struct process_info * @@ -153,6 +159,8 @@ remove_process (struct process_info *process) free_all_breakpoints (process); gdb_assert (find_thread_process (process) =3D=3D NULL); all_processes.remove (process); + if (current_process () =3D=3D process) + switch_to_process (nullptr); delete process; } =20 @@ -205,8 +213,7 @@ get_thread_process (const struct thread_info *thread) struct process_info * current_process (void) { - gdb_assert (current_thread !=3D NULL); - return get_thread_process (current_thread); + return current_process_; } =20 /* See gdbsupport/common-gdbthread.h. */ @@ -223,6 +230,10 @@ switch_to_thread (process_stratum_target *ops, ptid_t = ptid) void switch_to_thread (thread_info *thread) { + if (thread !=3D nullptr) + current_process_ =3D get_thread_process (thread); + else + current_process_ =3D nullptr; current_thread =3D thread; } =20 @@ -231,9 +242,8 @@ switch_to_thread (thread_info *thread) void switch_to_process (process_info *proc) { - int pid =3D pid_of (proc); - - switch_to_thread (find_any_thread_of_pid (pid)); + current_process_ =3D proc; + current_thread =3D nullptr; } =20 /* See gdbsupport/common-inferior.h. */ @@ -254,6 +264,7 @@ set_inferior_cwd (std::string cwd) =20 scoped_restore_current_thread::scoped_restore_current_thread () { + m_process =3D current_process_; m_thread =3D current_thread; } =20 @@ -262,5 +273,8 @@ scoped_restore_current_thread::~scoped_restore_current_= thread () if (m_dont_restore) return; =20 - switch_to_thread (m_thread); + if (m_thread !=3D nullptr) + switch_to_thread (m_thread); + else + switch_to_process (m_process); } diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 33c42714e72..f9c02a9c6da 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -1067,7 +1067,7 @@ gdb_read_memory (CORE_ADDR memaddr, unsigned char *my= addr, int len) /* (assume no half-trace half-real blocks for now) */ } =20 - if (set_desired_thread ()) + if (set_desired_process ()) res =3D read_inferior_memory (memaddr, myaddr, len); else res =3D 1; @@ -1088,7 +1088,7 @@ gdb_write_memory (CORE_ADDR memaddr, const unsigned c= har *myaddr, int len) { int ret; =20 - if (set_desired_thread ()) + if (set_desired_process ()) ret =3D target_write_memory (memaddr, myaddr, len); else ret =3D EIO; diff --git a/gdbserver/target.cc b/gdbserver/target.cc index 883242377c0..adcfe6e7bcc 100644 --- a/gdbserver/target.cc +++ b/gdbserver/target.cc @@ -29,16 +29,56 @@ =20 process_stratum_target *the_target; =20 -int +/* See target.h. */ + +bool set_desired_thread () { client_state &cs =3D get_client_state (); thread_info *found =3D find_thread_ptid (cs.general_thread); =20 - switch_to_thread (found); + if (found =3D=3D nullptr) + { + process_info *proc =3D find_process_pid (cs.general_thread.pid ()); + if (proc =3D=3D nullptr) + { + threads_debug_printf + ("did not find thread nor process for general_thread %s", + cs.general_thread.to_string ().c_str ()); + } + else + { + threads_debug_printf + ("did not find thread for general_thread %s, but found process", + cs.general_thread.to_string ().c_str ()); + } + switch_to_process (proc); + } + else + switch_to_thread (found); + return (current_thread !=3D NULL); } =20 +/* See target.h. */ + +bool +set_desired_process () +{ + client_state &cs =3D get_client_state (); + + process_info *proc =3D find_process_pid (cs.general_thread.pid ()); + if (proc =3D=3D nullptr) + { + threads_debug_printf + ("did not find process for general_thread %s", + cs.general_thread.to_string ().c_str ()); + } + switch_to_process (proc); + + return proc !=3D nullptr; +} + int read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) { diff --git a/gdbserver/target.h b/gdbserver/target.h index f3172e2ed7e..6c536a30778 100644 --- a/gdbserver/target.h +++ b/gdbserver/target.h @@ -699,7 +699,20 @@ target_thread_pending_child (thread_info *thread) =20 int read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int le= n); =20 -int set_desired_thread (); +/* Set GDBserver's current thread to the thread the client requested + via Hg. Also switches the current process to the requested + process. If the requested thread is not found in the thread list, + then the current thread is set to NULL. Likewise, if the requested + process is not found in the process list, then the current process + is set to NULL. Returns true if the requested thread was found, + false otherwise. */ + +bool set_desired_thread (); + +/* Set GDBserver's current process to the process the client requested + via Hg. The current thread is set to NULL. */ + +bool set_desired_process (); =20 std::string target_pid_to_str (ptid_t);