binutils-gdb/gdb/testsuite/gdb.server
Pedro Alves f3c469b95b Switch the inferior too in switch_to_program_space_and_thread
With multi-target, each inferior now has its own target connection.
The problem in switch_to_program_space_and_thread is that in the
current state GDB switches to "no thread" and also sets the program
space but because the inferior is not switched, potentially an
incorrect target remains selected.

Here is a sample scenario that exploits this flow:

On terminal 1, start a gdbserver on a program named foo:

 $ gdbserver :1234 ./foo

On terminal 2, start gdb on a program named bar.  Suppose foo and bar
are compiled from foo.c and bar.c.  They are completely separate.  So,
bar.c:2 has no meaning for foo.

 $ gdb -q ./bar
 Reading symbols from ./bar...
 (gdb) add-inferior
 [New inferior 2]
 Added inferior 2
 (gdb) inferior 2
 [Switching to inferior 2 [<null>] (<noexec>)]
 (gdb) target remote :1234
 ...
 (gdb) set debug remote 2
 (gdb) break bar.c:2
 Sending packet: $Hgp0.0#ad...Packet received: OK
 Sending packet: $m5fa,12#f8...Packet received: E01
 Sending packet: $m5fa,1#c6...Packet received: E01
 Sending packet: $m5fb,3#c9...Packet received: E01
 Sending packet: $m5fe,1#ca...Packet received: E01
 Breakpoint 1 at 0x5fe: file bar.c, line 2.
 (gdb)

Here we have an unnecessary sending of the packets to the gdbserver.

With this fix in progspace-and-thread.c, we'll get this:

 (gdb) break bar.c:2
 Breakpoint 1 at 0x5fe: file bar.c, line 2.
 (gdb)

Now there is no sending of the packets to gdbserver.

The changes around clear_symtab_users calls are necessary because
otherwise we regress gdb.base/step-over-exit.exp, hitting the new
assertion in switch_to_program_space_and_thread.  The problem is, a
forked child terminates, and when GDB decides to auto-purge that
inferior, GDB tries to switch to the pspace of that no-longer-existing
inferior.

The root of the problem is within the program_space destructor:

program_space::~program_space ()
{
...
  set_current_program_space (this);        # (1)
...
  breakpoint_program_space_exit (this);    # (2)
...
  free_all_objfiles ();                    # (3)
...
}

We get here from delete_inferior -> delete_program_space.

So we're deleting an inferior, and the inferior to be
deleted is no longer in the inferior list.

At (2), we've deleted all the breakpoints and locations for the
program space being deleted.

The crash happens while doing a breakpoint re-set, called by
clear_symtab_users at the tail end of (3).  That is, while recreating
breakpoints for the current program space, which is the program space
we're tearing down.  During breakpoint re-set, we try to switch to the
new location's pspace (the current pspace set in (1), so the pspace
we're tearing down) with switch_to_program_space_and_thread, and that
hits the failed assertion.  It's the fact that we recreate breakpoints
in the program_space destructor that is the latent bug here.  Just
don't do that, and we don't end up in the crash situation.

My first approach to fix this added a symfile_add_flags parameter to
program_space::free_all_objfiles, and then passed that down to
clear_symtab_users.  The program_space dtor would then pass down
SYMFILE_DEFER_BP_RESET to free_all_objfiles.  I couldn't help feeling
that adding that parameter to free_all_objfiles looked a little
awkward, so I settled on something a little different -- hoist the
clear_symtab_users call to the callers.  There are only two callers.
I felt that that didn't look as odd, particularly since
remove_symbol_file_command also does:

  objf->unlink ();
  clear_symtab_users (0);

I.e., objfile deletion is already separate from calling
clear_symtab_users in some places.

gdb/ChangeLog:
2020-01-10  Aleksandar Paunovic  <aleksandar.paunovic@intel.com>
	    Pedro Alves  <palves@redhat.com>

	* progspace-and-thread.c (switch_to_program_space_and_thread):
	Assert there's an inferior for PSPACE.  Use
	switch_to_inferior_no_thread to switch the inferior too.
	* progspace.c (program_space::~program_space): Call
	clear_symtab_users here, with SYMFILE_DEFER_BP_RESET.
	(program_space::free_all_objfiles): Don't call clear_symtab_users
	here.
	* symfile.c (symbol_file_clear): Call clear_symtab_users here.

gdb/testsuite/ChangeLog:
2020-01-10  Pedro Alves  <palves@redhat.com>

	* gdb.server/bkpt-other-inferior.exp: New file.
2020-01-10 20:06:16 +00:00
..
abspath.exp
bkpt-other-inferior.exp Switch the inferior too in switch_to_program_space_and_thread 2020-01-10 20:06:16 +00:00
connect-stopped-target.c
connect-stopped-target.exp
connect-with-no-symbol-file.c
connect-with-no-symbol-file.exp
connect-without-multi-process.c
connect-without-multi-process.exp Avoid another inferior_ptid reference in gdb/remote.c 2020-01-10 20:05:52 +00:00
ext-attach.c
ext-attach.exp
ext-restart.exp
ext-run.exp
ext-wrapper.exp
extended-remote-restart.c
extended-remote-restart.exp Add "info connections" command, "info inferiors" connection number/string 2020-01-10 20:06:14 +00:00
file-transfer.exp
no-thread-db.c
no-thread-db.exp
non-existing-program.exp
normal.c
reconnect-ctrl-c.c
reconnect-ctrl-c.exp
run-without-local-binary.exp
server-connect.exp
server-exec-info.exp
server-kill.c
server-kill.exp
server-mon.exp
server-run.exp
server.c
solib-list-lib.c
solib-list-main.c
solib-list.exp
stop-reply-no-thread.c
stop-reply-no-thread.exp
sysroot.c
sysroot.exp
transfer.txt
unittest.exp
wrapper.c
wrapper.exp