binutils-gdb/gdb/common
Pedro Alves 3a3fd0fd2c Fix removing inferiors from within "thread apply" commands
This patch fixes an internal error exposed by a test that does
something like:

  define kill-and-remove
    kill inferiors 2
    remove-inferiors 2
  end

  # Start one inferior.
  start

  # Start another inferior.
  add-inferior 2
  inferior 2
  start

  # Kill and remove inferior 1 while inferior 2 is selected.
  thread apply 1.1 kill-and-remove

The internal error looks like this:

 Thread 1.1 (Thread 0x7ffff7fc2700 (LWP 20677)):
 [Switching to inferior 1 [process 20677] (gdb/testsuite/outputs/gdb.threads/threadapply/threadapply)]
 [Switching to thread 1.1 (Thread 0x7ffff7fc2700 (LWP 20677))]
 #0  main () at src/gdb/testsuite/gdb.threads/threadapply.c:38
 38          for (i = 0; i < NUM; i++)
 src/gdb/inferior.c:66: internal-error: void set_current_inferior(inferior*): Assertion `inf != NULL' failed.
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Quit this debugging session? (y or n) FAIL: gdb.threads/threadapply.exp: kill_and_remove_inferior: try kill-and-remove: thread apply 1.1 kill-and-remove (GDB internal error)

There are several problems around this area of the code.  One is that
in do_restore_current_thread_cleanup, we do a look up of inferior by
ptid, which can find the wrong inferior if the previously selected
inferior exited and some other inferior was started with a reused pid
(rare, but still...).

The other problem is that the "remove-inferiors" command rejects
attempts to remove the current inferior, but when we get to
"remove-inferiors" in a "thread apply THR remove-inferiors 2" command,
the current inferior is the inferior of thread THR, not the previously
selected inferior, so if the previously selected inferior was inferior
2, that command still manages to wipe it, and then gdb restores the
old selected inferior, which is now a dangling pointer...

So the fix here is:

- Make make_cleanup_restore_current_thread store a pointer to the
  previously selected inferior directly, and use it directly instead
  of doing ptid look ups.

- Add a refcount to inferiors, very similar to thread_info's refcount,
  that is incremented/decremented by
  make_cleanup_restore_current_thread, and checked before deleting an
  inferior.  To avoid duplication, a new refcounted_object type is
  added, that both thread_info and inferior inherit from.

gdb/ChangeLog:
2017-04-19  Pedro Alves  <palves@redhat.com>

	* common/refcounted-object.h: New file.
	* gdbthread.h: Include "common/refcounted-object.h".
	(thread_info): Inherit from refcounted_object and add comments.
	(thread_info::incref, thread_info::decref)
	(thread_info::m_refcount): Delete.
	(thread_info::deletable): Use the refcounted_object::refcount()
	method.
	* inferior.c (current_inferior_): Add comment.
	(set_current_inferior): Increment/decrement refcounts.
	(prune_inferiors, remove_inferior_command): Skip inferiors marked
	not-deletable instead of comparing with the current inferior.
	(initialize_inferiors): Increment the initial inferior's refcount.
	* inferior.h (struct inferior): Forward declare.
	Include "common/refcounted-object.h".
	(current_inferior, set_current_inferior): Move declaration to
	before struct inferior's definition, and fix comment.
	(inferior): Inherit from refcounted_object.  Add comments.
	* thread.c (switch_to_thread_no_regs): Reference the thread's
	inferior pointer directly instead of doing a ptid lookup.
	(switch_to_no_thread): New function.
	(switch_to_thread(thread_info *)): New function, factored out
	from ...
	(switch_to_thread(ptid_t)): ... this.
	(restore_current_thread): Delete.
	(current_thread_cleanup): Remove 'inf_id' and 'was_removable'
	fields, and add 'inf' field.
	(do_restore_current_thread_cleanup): Check whether old->inf is
	alive instead of looking up an inferior by ptid.  Use
	switch_to_thread and switch_to_no_thread.
	(restore_current_thread_cleanup_dtor): Use old->inf directly
	instead of lookup up an inferior by id.  Decref the inferior.
	Don't restore 'removable'.
	(make_cleanup_restore_current_thread): Same the inferior pointer
	in old, instead of the inferior number.  Incref the inferior.
	Don't save/clear 'removable'.

gdb/testsuite/ChangeLog:
2017-04-19  Pedro Alves  <palves@redhat.com>

	* gdb.threads/threadapply.exp (kill_and_remove_inferior): New
	procedure.
	(top level): Call it.
	* lib/gdb.exp (gdb_define_cmd): New procedure.
2017-04-19 13:12:23 +01:00
..
agent.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
agent.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
ax.def update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
break-common.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
btrace-common.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
btrace-common.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
buffer.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
buffer.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
cleanups.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
cleanups.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common-debug.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common-debug.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common-defs.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common-exceptions.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common-exceptions.h DWARF-5: call sites 2017-02-20 21:00:55 +01:00
common-regcache.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common-regcache.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common-types.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common-utils.c C++-fy and prepare for sharing fork_inferior 2017-04-12 01:02:03 -04:00
common-utils.h C++-fy and prepare for sharing fork_inferior 2017-04-12 01:02:03 -04:00
common.host update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
common.m4 Create gdb_termios.h (and cleanup gdb/{,gdbserver/}terminal.h) 2017-04-12 17:59:45 -04:00
create-version.sh update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
enum-flags.h Default initialize enum flags to 0 2017-02-21 11:48:49 -05:00
environ.c Share gdb/environ.[ch] with gdbserver 2017-03-07 15:39:35 -05:00
environ.h Share gdb/environ.[ch] with gdbserver 2017-03-07 15:39:35 -05:00
errors.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
errors.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
fileio.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
fileio.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
filestuff.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
filestuff.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
format.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
format.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
function-view.h Introduce gdb::function_view 2017-02-23 16:14:08 +00:00
gdb_assert.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
gdb_locale.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
gdb_optional.h More gdb::optional features 2017-04-18 23:48:41 +01:00
gdb_ref_ptr.h Use class to manage BFD reference counts 2017-01-10 19:14:10 -07:00
gdb_setjmp.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
gdb_signals.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
gdb_sys_time.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
gdb_termios.h Create gdb_termios.h (and cleanup gdb/{,gdbserver/}terminal.h) 2017-04-12 17:59:45 -04:00
gdb_unique_ptr.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
gdb_unlinker.h gdb: Fix ATTRIBUTE_NONNULL usage 2017-03-08 15:52:16 +00:00
gdb_vecs.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
gdb_vecs.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
gdb_wait.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
host-defs.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
mingw-strerror.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
new-op.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
offset-type.h Make sect_offset and cu_offset strong typedefs instead of structs 2017-04-04 20:03:26 +01:00
posix-strerror.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
preprocessor.h Make sect_offset and cu_offset strong typedefs instead of structs 2017-04-04 20:03:26 +01:00
print-utils.c -Wwrite-strings: The Rest 2017-04-05 19:21:37 +01:00
print-utils.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
ptid.c Class-ify ptid_t 2017-04-06 23:29:53 -04:00
ptid.h Class-ify ptid_t 2017-04-06 23:29:53 -04:00
queue.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
refcounted-object.h Fix removing inferiors from within "thread apply" commands 2017-04-19 13:12:23 +01:00
rsp-low.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
rsp-low.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
run-time-clock.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
run-time-clock.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
scoped_restore.h Make inferior::detaching a bool, and introduce scoped_restore::release() 2017-04-19 13:12:23 +01:00
signals-state-save-restore.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
signals-state-save-restore.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
signals.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
symbol.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
traits.h More gdb::optional features 2017-04-18 23:48:41 +01:00
underlying.h dwarf2read.c: Make dir_index and file_name_index strong typedefs 2017-04-04 20:03:25 +01:00
valid-expr.h Make sect_offset and cu_offset strong typedefs instead of structs 2017-04-04 20:03:26 +01:00
vec.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
vec.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
version.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
x86-xstate.h Add support for Intel PKRU register to GDB and GDBserver. 2017-02-17 11:44:48 +01:00
xml-utils.c update copyright year range in GDB files 2017-01-01 10:52:34 +04:00
xml-utils.h update copyright year range in GDB files 2017-01-01 10:52:34 +04:00