aboutsummaryrefslogtreecommitdiff
path: root/gdb/thread.c
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2017-04-19 13:12:23 +0100
committerPedro Alves <palves@redhat.com>2017-04-19 13:12:23 +0100
commit3a3fd0fd2c4c87fdd588c51d879961a49e38f0c1 (patch)
treec76d8c677a430fc2511e380fa895258e78811882 /gdb/thread.c
parent9bcb1f1630b05594fa86bfd017639cfcc966b11c (diff)
downloadgdb-3a3fd0fd2c4c87fdd588c51d879961a49e38f0c1.zip
gdb-3a3fd0fd2c4c87fdd588c51d879961a49e38f0c1.tar.gz
gdb-3a3fd0fd2c4c87fdd588c51d879961a49e38f0c1.tar.bz2
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.
Diffstat (limited to 'gdb/thread.c')
-rw-r--r--gdb/thread.c82
1 files changed, 39 insertions, 43 deletions
diff --git a/gdb/thread.c b/gdb/thread.c
index 88fd521..e4113c2 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -68,7 +68,6 @@ static void thread_apply_all_command (char *, int);
static int thread_alive (struct thread_info *);
static void info_threads_command (char *, int);
static void thread_apply_command (char *, int);
-static void restore_current_thread (ptid_t);
/* RAII type used to increase / decrease the refcount of each thread
in a given list of threads. */
@@ -1458,10 +1457,8 @@ info_threads_command (char *arg, int from_tty)
void
switch_to_thread_no_regs (struct thread_info *thread)
{
- struct inferior *inf;
+ struct inferior *inf = thread->inf;
- inf = find_inferior_ptid (thread->ptid);
- gdb_assert (inf != NULL);
set_current_program_space (inf->pspace);
set_current_inferior (inf);
@@ -1469,45 +1466,50 @@ switch_to_thread_no_regs (struct thread_info *thread)
stop_pc = ~(CORE_ADDR) 0;
}
-/* Switch from one thread to another. */
+/* Switch to no thread selected. */
-void
-switch_to_thread (ptid_t ptid)
+static void
+switch_to_no_thread ()
{
- /* Switch the program space as well, if we can infer it from the now
- current thread. Otherwise, it's up to the caller to select the
- space it wants. */
- if (ptid != null_ptid)
- {
- struct inferior *inf;
+ if (inferior_ptid == null_ptid)
+ return;
- inf = find_inferior_ptid (ptid);
- gdb_assert (inf != NULL);
- set_current_program_space (inf->pspace);
- set_current_inferior (inf);
- }
+ inferior_ptid = null_ptid;
+ reinit_frame_cache ();
+ stop_pc = ~(CORE_ADDR) 0;
+}
+
+/* Switch from one thread to another. */
- if (ptid == inferior_ptid)
+static void
+switch_to_thread (thread_info *thr)
+{
+ gdb_assert (thr != NULL);
+
+ if (inferior_ptid == thr->ptid)
return;
- inferior_ptid = ptid;
+ switch_to_thread_no_regs (thr);
+
reinit_frame_cache ();
/* We don't check for is_stopped, because we're called at times
while in the TARGET_RUNNING state, e.g., while handling an
internal event. */
- if (inferior_ptid != null_ptid
- && !is_exited (ptid)
- && !is_executing (ptid))
- stop_pc = regcache_read_pc (get_thread_regcache (ptid));
- else
- stop_pc = ~(CORE_ADDR) 0;
+ if (thr->state != THREAD_EXITED
+ && !thr->executing)
+ stop_pc = regcache_read_pc (get_thread_regcache (thr->ptid));
}
-static void
-restore_current_thread (ptid_t ptid)
+/* See gdbthread.h. */
+
+void
+switch_to_thread (ptid_t ptid)
{
- switch_to_thread (ptid);
+ if (ptid == null_ptid)
+ switch_to_no_thread ();
+ else
+ switch_to_thread (find_thread_ptid (ptid));
}
static void
@@ -1578,8 +1580,7 @@ struct current_thread_cleanup
struct frame_id selected_frame_id;
int selected_frame_level;
int was_stopped;
- int inf_id;
- int was_removable;
+ inferior *inf;
};
static void
@@ -1595,12 +1596,12 @@ do_restore_current_thread_cleanup (void *arg)
/* If the previously selected thread belonged to a process that has
in the mean time exited (or killed, detached, etc.), then don't revert
back to it, but instead simply drop back to no thread selected. */
- && find_inferior_ptid (old->thread->ptid) != NULL)
- restore_current_thread (old->thread->ptid);
+ && old->inf->pid != 0)
+ switch_to_thread (old->thread);
else
{
- restore_current_thread (null_ptid);
- set_current_inferior (find_inferior_id (old->inf_id));
+ switch_to_no_thread ();
+ set_current_inferior (old->inf);
}
/* The running state of the originally selected thread may have
@@ -1619,15 +1620,11 @@ static void
restore_current_thread_cleanup_dtor (void *arg)
{
struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
- struct thread_info *tp;
- struct inferior *inf;
if (old->thread != NULL)
old->thread->decref ();
- inf = find_inferior_id (old->inf_id);
- if (inf != NULL)
- inf->removable = old->was_removable;
+ old->inf->decref ();
xfree (old);
}
@@ -1637,8 +1634,7 @@ make_cleanup_restore_current_thread (void)
struct current_thread_cleanup *old = XNEW (struct current_thread_cleanup);
old->thread = NULL;
- old->inf_id = current_inferior ()->num;
- old->was_removable = current_inferior ()->removable;
+ old->inf = current_inferior ();
if (inferior_ptid != null_ptid)
{
@@ -1670,7 +1666,7 @@ make_cleanup_restore_current_thread (void)
old->thread = tp;
}
- current_inferior ()->removable = 0;
+ old->inf->incref ();
return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
restore_current_thread_cleanup_dtor);