From c6f7f9c80c3b92c6d877214d38aaef141adc5b93 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 8 Sep 2023 08:25:15 -0600 Subject: Bail out of "attach" if a thread cannot be traced On Linux, threads are treated much like separate processes by the kernel. In particular, it's possible to ptrace just a single thread. If gdb tries to attach to a multi-threaded inferior, where a non-main thread is already being traced (e.g., by strace), then gdb will get into an infinite loop attempting to attach. This patch fixes this problem by having the attach fail if ptrace fails to attach to any thread of the inferior. --- gdbserver/linux-low.cc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'gdbserver') diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc index eea2d8a..4aa011c 100644 --- a/gdbserver/linux-low.cc +++ b/gdbserver/linux-low.cc @@ -1154,7 +1154,7 @@ attach_proc_task_lwp_callback (ptid_t ptid) std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err); - warning (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ()); + error (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ()); } return 1; @@ -1207,7 +1207,18 @@ linux_process_target::attach (unsigned long pid) threads/LWPs, and those structures may well be corrupted. Note that once thread_db is loaded, we'll still use it to list threads and associate pthread info with each LWP. */ - linux_proc_attach_tgid_threads (pid, attach_proc_task_lwp_callback); + try + { + linux_proc_attach_tgid_threads (pid, attach_proc_task_lwp_callback); + } + catch (const gdb_exception_error &) + { + /* Make sure we do not deliver the SIGSTOP to the process. */ + initial_thread->last_resume_kind = resume_continue; + + this->detach (proc); + throw; + } /* GDB will shortly read the xml target description for this process, to figure out the process' architecture. But the target -- cgit v1.1