binutils-gdb/gdb/testsuite/gdb.multi
Pedro Alves 7ae1a6a6cc 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.
2014-04-25 19:07:33 +01:00
..
Makefile.in * Makefile.in (clean): Remove Fission .dwo and .dwp files. 2012-05-17 19:03:59 +00:00
base.exp Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
bkpt-multi-exec.c fix some "exec" tests 2013-11-04 11:02:10 -07:00
bkpt-multi-exec.exp Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
crashme.c
goodbye.c Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
hangout.c Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
hello.c Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
multi-arch-exec.c Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
multi-arch-exec.exp Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
multi-arch.exp Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
multi-attach.c PR server/16255: gdbserver cannot attach to a second inferior that is multi-threaded. 2014-04-25 19:07:33 +01:00
multi-attach.exp PR server/16255: gdbserver cannot attach to a second inferior that is multi-threaded. 2014-04-25 19:07:33 +01:00
watchpoint-multi.c Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00
watchpoint-multi.exp Update Copyright year range in all files maintained by GDB. 2014-01-01 07:54:24 +04:00