aboutsummaryrefslogtreecommitdiff
path: root/gdb/gdbserver
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2014-04-25 19:07:33 +0100
committerPedro Alves <palves@redhat.com>2014-04-25 19:07:33 +0100
commit7ae1a6a6ccda41aa8bbe9adb0f7fcde8bf8d5cb3 (patch)
treeac642730c7c29c0735909a95e60be6253fd24f49 /gdb/gdbserver
parent4082afcc3d1af9d8063d1c8e02deb34a8b97a489 (diff)
downloadgdb-7ae1a6a6ccda41aa8bbe9adb0f7fcde8bf8d5cb3.zip
gdb-7ae1a6a6ccda41aa8bbe9adb0f7fcde8bf8d5cb3.tar.gz
gdb-7ae1a6a6ccda41aa8bbe9adb0f7fcde8bf8d5cb3.tar.bz2
PR server/16255: gdbserver cannot attach to a second inferior that is multi-threaded.
On Linux, we need to explicitly ptrace attach to all lwps of a process. Because GDB might not be connected yet when an attach is requested, and thus it may not be possible to activate thread_db, as that requires access to symbols (IOW, gdbserver --attach), a while ago we make linux_attach loop over the lwps as listed by /proc/PID/task to find the lwps to attach to. linux_attach_lwp_1 has: ... if (initial) /* If lwp is the tgid, we handle adding existing threads later. Otherwise we just add lwp without bothering about any other threads. */ ptid = ptid_build (lwpid, lwpid, 0); else { /* Note that extracting the pid from the current inferior is safe, since we're always called in the context of the same process as this new thread. */ int pid = pid_of (current_inferior); ptid = ptid_build (pid, lwpid, 0); } That "safe" comment referred to linux_attach_lwp being called by thread-db.c. But this was clearly missed when a new call to linux_attach_lwp_1 was added to linux_attach. As a result, current_inferior will be set to some random process, and non-initial lwps of the second inferior get assigned the pid of the wrong inferior. E.g., in the case of attaching to two inferiors, for the second inferior (and so on), non-initial lwps of the second inferior get assigned the pid of the first inferior. This doesn't trigger on the first inferior, when current_inferior is NULL, add_thread switches the current inferior to the newly added thread. Rather than making linux_attach switch current_inferior temporarily (thus avoiding further reliance on global state), or making linux_attach_lwp_1 get the tgid from /proc, which add extra syscalls, and will be wrong in case of the user having originally attached directly to a non-tgid lwp, and then that lwp spawning new clones (the ptid.pid field of further new clones should be the same as the original lwp's pid, which is not the tgid), we note that callers of linux_attach_lwp/linux_attach_lwp_1 always have the right pid handy already, so they can pass it down along with the lwpid. The only other reason for the "initial" parameter is to error out instead of warn in case of attach failure, when we're first attaching to a process. There are only three callers of linux_attach_lwp/linux_attach_lwp_1, and each wants to print a different warn/error string, so we can just move the error/warn out of linux_attach_lwp_1 to the callers, thus getting rid of the "initial" parameter. There really nothing gdbserver-specific about attaching to two threaded processes, so this adds a new test under gdb.multi/. The test passes cleanly against the native GNU/Linux target, but fails/triggers the bug against GDBserver (before the patch), with the native-extended-remote board (as plain remote doesn't support multi-process). Tested on x86_64 Fedora 17, with the native-extended-gdbserver board. gdb/gdbserver/ 2014-04-25 Pedro Alves <palves@redhat.com> PR server/16255 * linux-low.c (linux_attach_fail_reason_string): New function. (linux_attach_lwp): Delete. (linux_attach_lwp_1): Rename to ... (linux_attach_lwp): ... this. Take a ptid instead of a pid as argument. Remove "initial" parameter. Return int instead of void. Don't error or warn here. (linux_attach): Adjust to call linux_attach_lwp. Call error on failure to attach to the tgid. Call warning when failing to attach to an lwp. * linux-low.h (linux_attach_lwp): Take a ptid instead of a pid as argument. Remove "initial" parameter. Return int instead of void. Don't error or warn here. (linux_attach_fail_reason_string): New declaration. * thread-db.c (attach_thread): Adjust to linux_attach_lwp's interface change. Use linux_attach_fail_reason_string. gdb/ 2014-04-25 Pedro Alves <palves@redhat.com> PR server/16255 * common/linux-ptrace.c (linux_ptrace_attach_warnings): Rename to ... (linux_ptrace_attach_fail_reason): ... this. Remove "warning: " and newline from built string. * common/linux-ptrace.h (linux_ptrace_attach_warnings): Rename to ... (linux_ptrace_attach_fail_reason): ... this. * linux-nat.c (linux_nat_attach): Adjust to use linux_ptrace_attach_fail_reason. gdb/testsuite/ 2014-04-25 Simon Marchi <simon.marchi@ericsson.com> Pedro Alves <palves@redhat.com> PR server/16255 * gdb.multi/multi-attach.c: New file. * gdb.multi/multi-attach.exp: New file.
Diffstat (limited to 'gdb/gdbserver')
-rw-r--r--gdb/gdbserver/linux-low.c105
-rw-r--r--gdb/gdbserver/linux-low.h11
-rw-r--r--gdb/gdbserver/thread-db.c18
3 files changed, 77 insertions, 57 deletions
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index c847c62..0ef8d60 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -635,49 +635,41 @@ linux_create_inferior (char *program, char **allargs)
return pid;
}
+char *
+linux_attach_fail_reason_string (ptid_t ptid, int err)
+{
+ static char *reason_string;
+ struct buffer buffer;
+ char *warnings;
+ long lwpid = ptid_get_lwp (ptid);
+
+ xfree (reason_string);
+
+ buffer_init (&buffer);
+ linux_ptrace_attach_fail_reason (lwpid, &buffer);
+ buffer_grow_str0 (&buffer, "");
+ warnings = buffer_finish (&buffer);
+ if (warnings[0] != '\0')
+ reason_string = xstrprintf ("%s (%d), %s",
+ strerror (err), err, warnings);
+ else
+ reason_string = xstrprintf ("%s (%d)",
+ strerror (err), err);
+ xfree (warnings);
+ return reason_string;
+}
+
/* Attach to an inferior process. */
-static void
-linux_attach_lwp_1 (unsigned long lwpid, int initial)
+int
+linux_attach_lwp (ptid_t ptid)
{
- ptid_t ptid;
struct lwp_info *new_lwp;
+ int lwpid = ptid_get_lwp (ptid);
if (ptrace (PTRACE_ATTACH, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0)
!= 0)
- {
- struct buffer buffer;
-
- if (!initial)
- {
- /* If we fail to attach to an LWP, just warn. */
- fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n", lwpid,
- strerror (errno), errno);
- fflush (stderr);
- return;
- }
-
- /* If we fail to attach to a process, report an error. */
- buffer_init (&buffer);
- linux_ptrace_attach_warnings (lwpid, &buffer);
- buffer_grow_str0 (&buffer, "");
- error ("%sCannot attach to lwp %ld: %s (%d)", buffer_finish (&buffer),
- lwpid, strerror (errno), errno);
- }
-
- if (initial)
- /* If lwp is the tgid, we handle adding existing threads later.
- Otherwise we just add lwp without bothering about any other
- threads. */
- ptid = ptid_build (lwpid, lwpid, 0);
- else
- {
- /* Note that extracting the pid from the current inferior is
- safe, since we're always called in the context of the same
- process as this new thread. */
- int pid = pid_of (current_inferior);
- ptid = ptid_build (pid, lwpid, 0);
- }
+ return errno;
new_lwp = add_lwp (ptid);
@@ -747,12 +739,8 @@ linux_attach_lwp_1 (unsigned long lwpid, int initial)
end of the list, and so the new thread has not yet reached
wait_for_sigstop (but will). */
new_lwp->stop_expected = 1;
-}
-void
-linux_attach_lwp (unsigned long lwpid)
-{
- linux_attach_lwp_1 (lwpid, 0);
+ return 0;
}
/* Attach to PID. If PID is the tgid, attach to it and all
@@ -761,9 +749,16 @@ linux_attach_lwp (unsigned long lwpid)
static int
linux_attach (unsigned long pid)
{
+ ptid_t ptid = ptid_build (pid, pid, 0);
+ int err;
+
/* Attach to PID. We will check for other threads
soon. */
- linux_attach_lwp_1 (pid, 1);
+ err = linux_attach_lwp (ptid);
+ if (err != 0)
+ error ("Cannot attach to process %ld: %s",
+ pid, linux_attach_fail_reason_string (ptid, err));
+
linux_add_process (pid, 1);
if (!non_stop)
@@ -794,13 +789,13 @@ linux_attach (unsigned long pid)
{
/* At this point we attached to the tgid. Scan the task for
existing threads. */
- unsigned long lwp;
int new_threads_found;
int iterations = 0;
- struct dirent *dp;
while (iterations < 2)
{
+ struct dirent *dp;
+
new_threads_found = 0;
/* Add all the other threads. While we go through the
threads, new threads may be spawned. Cycle through
@@ -808,19 +803,29 @@ linux_attach (unsigned long pid)
finding new threads. */
while ((dp = readdir (dir)) != NULL)
{
+ unsigned long lwp;
+ ptid_t ptid;
+
/* Fetch one lwp. */
lwp = strtoul (dp->d_name, NULL, 10);
+ ptid = ptid_build (pid, lwp, 0);
+
/* Is this a new thread? */
- if (lwp
- && find_thread_ptid (ptid_build (pid, lwp, 0)) == NULL)
+ if (lwp != 0 && find_thread_ptid (ptid) == NULL)
{
- linux_attach_lwp_1 (lwp, 0);
- new_threads_found++;
+ int err;
if (debug_threads)
- debug_printf ("Found and attached to new lwp %ld\n",
- lwp);
+ debug_printf ("Found new lwp %ld\n", lwp);
+
+ err = linux_attach_lwp (ptid);
+ if (err != 0)
+ warning ("Cannot attach to lwp %ld: %s",
+ lwp,
+ linux_attach_fail_reason_string (ptid, err));
+
+ new_threads_found++;
}
}
diff --git a/gdb/gdbserver/linux-low.h b/gdb/gdbserver/linux-low.h
index 7459710..cd3001d 100644
--- a/gdb/gdbserver/linux-low.h
+++ b/gdb/gdbserver/linux-low.h
@@ -343,7 +343,16 @@ struct lwp_info
int linux_pid_exe_is_elf_64_file (int pid, unsigned int *machine);
-void linux_attach_lwp (unsigned long pid);
+/* Attach to PTID. Returns 0 on success, non-zero otherwise (an
+ errno). */
+int linux_attach_lwp (ptid_t ptid);
+
+/* Return the reason an attach failed, in string form. ERR is the
+ error returned by linux_attach_lwp (an errno). This string should
+ be copied into a buffer by the client if the string will not be
+ immediately used, or if it must persist. */
+char *linux_attach_fail_reason_string (ptid_t ptid, int err);
+
struct lwp_info *find_lwp_pid (ptid_t ptid);
void linux_stop_lwp (struct lwp_info *lwp);
diff --git a/gdb/gdbserver/thread-db.c b/gdb/gdbserver/thread-db.c
index f63e39e..ae0d191 100644
--- a/gdb/gdbserver/thread-db.c
+++ b/gdb/gdbserver/thread-db.c
@@ -323,27 +323,33 @@ find_one_thread (ptid_t ptid)
static int
attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
{
+ struct process_info *proc = current_process ();
+ int pid = pid_of (proc);
+ ptid_t ptid = ptid_build (pid, ti_p->ti_lid, 0);
struct lwp_info *lwp;
+ int err;
if (debug_threads)
debug_printf ("Attaching to thread %ld (LWP %d)\n",
ti_p->ti_tid, ti_p->ti_lid);
- linux_attach_lwp (ti_p->ti_lid);
- lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
- if (lwp == NULL)
+ err = linux_attach_lwp (ptid);
+ if (err != 0)
{
- warning ("Could not attach to thread %ld (LWP %d)\n",
- ti_p->ti_tid, ti_p->ti_lid);
+ warning ("Could not attach to thread %ld (LWP %d): %s\n",
+ ti_p->ti_tid, ti_p->ti_lid,
+ linux_attach_fail_reason_string (ptid, err));
return 0;
}
+ lwp = find_lwp_pid (ptid);
+ gdb_assert (lwp != NULL);
lwp->thread_known = 1;
lwp->th = *th_p;
if (thread_db_use_events)
{
td_err_e err;
- struct thread_db *thread_db = current_process ()->private->thread_db;
+ struct thread_db *thread_db = proc->private->thread_db;
err = thread_db->td_thr_event_enable_p (th_p, 1);
if (err != TD_OK)