binutils-gdb/gdbserver/inferiors.cc
Pedro Alves 028a46039a gdbserver: handle running threads in qXfer:threads:read
On some systems, the gdb.multi/multi-target.exp testcase occasionally
fails like so:

 Running src/gdb/testsuite/gdb.multi/multi-target.exp ...
 FAIL: gdb.multi/multi-target.exp: info-inferiors: multi_process=on: inferior 1: info connections
 FAIL: gdb.multi/multi-target.exp: info-inferiors: multi_process=on: inferior 1: info inferiors
 FAIL: gdb.multi/multi-target.exp: info-inferiors: multi_process=on: inferior 2: info connections
 FAIL: gdb.multi/multi-target.exp: info-inferiors: multi_process=on: inferior 2: info inferiors
 FAIL: gdb.multi/multi-target.exp: info-inferiors: multi_process=on: inferior 3: inferior 3
 ... many more cascading fails.

The problem starts when the testcase runs an inferior against GDBserver:

 (gdb) run
 Starting program: build/gdb/testsuite/outputs/gdb.multi/multi-target/multi-target
 Reading /lib64/ld-linux-x86-64.so.2 from remote target...
 warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
 Reading /lib64/ld-linux-x86-64.so.2 from remote target...
 Reading /lib64/ld-2.31.so from remote target...
 Reading /lib64/.debug/ld-2.31.so from remote target...
 Reading /usr/lib/debug//lib64/ld-2.31.so from remote target...
 Reading /usr/lib/debug/lib64//ld-2.31.so from remote target...
 Reading target:/usr/lib/debug/lib64//ld-2.31.so from remote target...
 Reading /lib/x86_64-linux-gnu/libpthread.so.0 from remote target...
 Reading /lib/x86_64-linux-gnu/libc.so.6 from remote target...
 Reading /lib/x86_64-linux-gnu/libc-2.31.so from remote target...
 Reading /lib/x86_64-linux-gnu/.debug/libc-2.31.so from remote target...
 Reading /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.31.so from remote target...
 Reading /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.31.so from remote target...
 Remote connection closed
 ...

Note the "Remote connection closed" message.  That means GDBserver
exited abruptly.

I traced it down to the fact that GDB fetches the thread list from
GDBserver while the main thread of the process is still running.  On
my main system where I wrote the testcase, I have not observed the
failure because it is slow enough that the thread stops before
GDBserver fetches the thread list in the problem scenario which I'll
describe below.

With some --remote-debug logging from GDBserver side, we see the last
packets before the connection closes:

 ...
 getpkt ("vCont;c");  [no ack sent]
 putpkt ("$OK#9a"); [noack mode]
 getpkt ("Tp10f9a.10f9a");  [no ack sent]
 putpkt ("$OK#9a"); [noack mode]
 getpkt ("Hgp0.0");  [no ack sent]
 putpkt ("$OK#9a"); [noack mode]
 getpkt ("qXfer:threads:read::0,1000");  [no ack sent]

Note the vCont;c , which sets the program running, and then a
qXfer:threads:read packet at the end.

The problem happens when the thread list refresh (qXfer:threads:read)
is sent just while the main thread is running and it still hasn't
initialized its libpthread id internally.  In that state, the main
thread's lwp will remain with the thread_known flag clear.  See in
find_one_thread:

  /* If the new thread ID is zero, a final thread ID will be available
     later.  Do not enable thread debugging yet.  */
  if (ti.ti_tid == 0)
    return 0;

Now, back in server.cc, to handle the qXfer:threads:read, we reach
handle_qxfer_threads -> handle_qxfer_threads_proper, and the latter
then calls handle_qxfer_threads_worker for each known thread.  In
handle_qxfer_threads_worker, we call target_thread_handle.  This ends
up in thread_db_thread_handle, here:

  if (!lwp->thread_known && !find_one_thread (thread->id))
    return false;

Since the thread ID isn't known yet, we call find_one_thread.  This
calls into libthread_db.so, which accesses memory.  Because the
current thread is running, that fails and we throw an error, here:

  /* Get information about this thread.  */
  err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
  if (err != TD_OK)
    error ("Cannot get thread handle for LWP %d: %s",
	   lwpid, thread_db_err_str (err));

The current design is that whenever GDB-facing packets/requests need
to accesses memory, server.cc is supposed to prepare the target for
the access.  See gdb_read_memory / gdb_write_memory.  This preparation
means pausing threads if in non-stop mode (someday we could lift this
requirement, but we will still need to pause to access registers or do
other related ptrace accesses like PTRACE_GET_THREAD_AREA).  Note that
the multi-target.exp testcase forces "maint set target-non-stop on".

So the fix here is to prepare the target to access memory when
handling qXfer:threads:read too.

gdbserver/ChangeLog:

	* inferiors.cc (switch_to_process): New, moved here from
	thread-db.cc, and made extern.
	* inferiors.h (switch_to_process): Declare.
	* server.cc: Include "gdbsupport/scoped_restore.h".
	(handle_qxfer_threads_proper): Now returns bool.  Prepare to
	access memory around target calls.
	(handle_qxfer_threads): Handle errors.
	* thread-db.cc (switch_to_process): Moved to inferiors.cc.
2020-07-22 12:32:53 +01:00

255 lines
5.4 KiB
C++

/* Inferior process information for the remote server for GDB.
Copyright (C) 2002-2020 Free Software Foundation, Inc.
Contributed by MontaVista Software.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "server.h"
#include "gdbsupport/common-inferior.h"
#include "gdbthread.h"
#include "dll.h"
std::list<process_info *> all_processes;
std::list<thread_info *> all_threads;
struct thread_info *current_thread;
/* The current working directory used to start the inferior. */
static const char *current_inferior_cwd = NULL;
struct thread_info *
add_thread (ptid_t thread_id, void *target_data)
{
struct thread_info *new_thread = XCNEW (struct thread_info);
new_thread->id = thread_id;
new_thread->last_resume_kind = resume_continue;
new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
all_threads.push_back (new_thread);
if (current_thread == NULL)
current_thread = new_thread;
new_thread->target_data = target_data;
return new_thread;
}
/* See gdbthread.h. */
struct thread_info *
get_first_thread (void)
{
if (!all_threads.empty ())
return all_threads.front ();
else
return NULL;
}
struct thread_info *
find_thread_ptid (ptid_t ptid)
{
return find_thread ([&] (thread_info *thread) {
return thread->id == ptid;
});
}
/* Find a thread associated with the given PROCESS, or NULL if no
such thread exists. */
static struct thread_info *
find_thread_process (const struct process_info *const process)
{
return find_any_thread_of_pid (process->pid);
}
/* See gdbthread.h. */
struct thread_info *
find_any_thread_of_pid (int pid)
{
return find_thread (pid, [] (thread_info *thread) {
return true;
});
}
static void
free_one_thread (thread_info *thread)
{
free_register_cache (thread_regcache_data (thread));
free (thread);
}
void
remove_thread (struct thread_info *thread)
{
if (thread->btrace != NULL)
target_disable_btrace (thread->btrace);
discard_queued_stop_replies (ptid_of (thread));
all_threads.remove (thread);
free_one_thread (thread);
if (current_thread == thread)
current_thread = NULL;
}
void *
thread_target_data (struct thread_info *thread)
{
return thread->target_data;
}
struct regcache *
thread_regcache_data (struct thread_info *thread)
{
return thread->regcache_data;
}
void
set_thread_regcache_data (struct thread_info *thread, struct regcache *data)
{
thread->regcache_data = data;
}
void
clear_inferiors (void)
{
for_each_thread (free_one_thread);
all_threads.clear ();
clear_dlls ();
current_thread = NULL;
}
struct process_info *
add_process (int pid, int attached)
{
process_info *process = new process_info (pid, attached);
all_processes.push_back (process);
return process;
}
/* Remove a process from the common process list and free the memory
allocated for it.
The caller is responsible for freeing private data first. */
void
remove_process (struct process_info *process)
{
clear_symbol_cache (&process->symbol_cache);
free_all_breakpoints (process);
gdb_assert (find_thread_process (process) == NULL);
all_processes.remove (process);
delete process;
}
process_info *
find_process_pid (int pid)
{
return find_process ([&] (process_info *process) {
return process->pid == pid;
});
}
/* Get the first process in the process list, or NULL if the list is empty. */
process_info *
get_first_process (void)
{
if (!all_processes.empty ())
return all_processes.front ();
else
return NULL;
}
/* Return non-zero if there are any inferiors that we have created
(as opposed to attached-to). */
int
have_started_inferiors_p (void)
{
return find_process ([] (process_info *process) {
return !process->attached;
}) != NULL;
}
/* Return non-zero if there are any inferiors that we have attached to. */
int
have_attached_inferiors_p (void)
{
return find_process ([] (process_info *process) {
return process->attached;
}) != NULL;
}
struct process_info *
get_thread_process (const struct thread_info *thread)
{
return find_process_pid (thread->id.pid ());
}
struct process_info *
current_process (void)
{
gdb_assert (current_thread != NULL);
return get_thread_process (current_thread);
}
/* See gdbsupport/common-gdbthread.h. */
void
switch_to_thread (process_stratum_target *ops, ptid_t ptid)
{
gdb_assert (ptid != minus_one_ptid);
current_thread = find_thread_ptid (ptid);
}
/* See inferiors.h. */
void
switch_to_process (process_info *proc)
{
int pid = pid_of (proc);
current_thread = find_any_thread_of_pid (pid);
}
/* See gdbsupport/common-inferior.h. */
const char *
get_inferior_cwd ()
{
return current_inferior_cwd;
}
/* See gdbsupport/common-inferior.h. */
void
set_inferior_cwd (const char *cwd)
{
xfree ((void *) current_inferior_cwd);
if (cwd != NULL)
current_inferior_cwd = xstrdup (cwd);
else
current_inferior_cwd = NULL;
}