+2014-10-02 Pedro Alves <palves@redhat.com>
+
+ * gdbthread.h (any_thread_of_process, any_live_thread_of_process):
+ Adjust comments.
+ * inferior.c (find_inferior_for_program_space): Give preference to
+ the current inferior.
+ * inferior.h (find_inferior_for_program_space): Update comment.
+ * progspace.c (switch_to_program_space_and_thread): Prefer the
+ current inferior if it's bound to the program space requested. If
+ the inferior found doesn't have a PID yet, don't bother looking up
+ a thread.
+ * progspace.h (switch_to_program_space_and_thread): Adjust
+ comment.
+ * thread.c (any_thread_of_process, any_live_thread_of_process):
+ Give preference to the current thread.
+
2014-10-01 Pedro Alves <palves@redhat.com>
* breakpoint.c (insert_bp_location): Error out if inserting a
returns the first thread in the list. */
struct thread_info *first_thread_of_process (int pid);
-/* Returns any thread of process PID. */
+/* Returns any thread of process PID, giving preference to the current
+ thread. */
extern struct thread_info *any_thread_of_process (int pid);
-/* Returns any non-exited thread of process PID, giving preference for
- not executing threads. */
+/* Returns any non-exited thread of process PID, giving preference to
+ the current thread, and to not executing threads. */
extern struct thread_info *any_live_thread_of_process (int pid);
/* Change the ptid of thread OLD_PTID to NEW_PTID. */
return NULL;
}
-/* Find an inferior bound to PSPACE. */
+/* See inferior.h. */
struct inferior *
find_inferior_for_program_space (struct program_space *pspace)
{
- struct inferior *inf;
+ struct inferior *inf = current_inferior ();
+
+ if (inf->pspace == pspace)
+ return inf;
for (inf = inferior_list; inf != NULL; inf = inf->next)
{
/* Search function to lookup an inferior by GDB 'num'. */
extern struct inferior *find_inferior_id (int num);
-/* Find an inferior bound to PSPACE. */
+/* Find an inferior bound to PSPACE, giving preference to the current
+ inferior. */
extern struct inferior *
find_inferior_for_program_space (struct program_space *pspace);
return old_chain;
}
-/* Switches full context to program space PSPACE. Switches to the
- first thread found bound to PSPACE. */
+/* See progspace.h */
void
switch_to_program_space_and_thread (struct program_space *pspace)
struct inferior *inf;
inf = find_inferior_for_program_space (pspace);
- if (inf != NULL)
+ if (inf != NULL && inf->pid != 0)
{
struct thread_info *tp;
extern struct cleanup *save_current_space_and_thread (void);
/* Switches full context to program space PSPACE. Switches to the
- first thread found bound to PSPACE. */
+ first thread found bound to PSPACE, giving preference to the
+ current thread, if there's one and it isn't executing. */
extern void switch_to_program_space_and_thread (struct program_space *pspace);
/* Create a new address space object, and add it to the list. */
{
struct thread_info *tp;
- for (tp = thread_list; tp; tp = tp->next)
+ gdb_assert (pid != 0);
+
+ /* Prefer the current thread. */
+ if (ptid_get_pid (inferior_ptid) == pid)
+ return inferior_thread ();
+
+ ALL_NON_EXITED_THREADS (tp)
if (ptid_get_pid (tp->ptid) == pid)
return tp;
struct thread_info *
any_live_thread_of_process (int pid)
{
+ struct thread_info *curr_tp = NULL;
struct thread_info *tp;
struct thread_info *tp_executing = NULL;
- for (tp = thread_list; tp; tp = tp->next)
- if (tp->state != THREAD_EXITED && ptid_get_pid (tp->ptid) == pid)
+ gdb_assert (pid != 0);
+
+ /* Prefer the current thread if it's not executing. */
+ if (ptid_get_pid (inferior_ptid) == pid)
+ {
+ /* If the current thread is dead, forget it. If it's not
+ executing, use it. Otherwise, still choose it (below), but
+ only if no other non-executing thread is found. */
+ curr_tp = inferior_thread ();
+ if (curr_tp->state == THREAD_EXITED)
+ curr_tp = NULL;
+ else if (!curr_tp->executing)
+ return curr_tp;
+ }
+
+ ALL_NON_EXITED_THREADS (tp)
+ if (ptid_get_pid (tp->ptid) == pid)
{
- if (tp->executing)
- tp_executing = tp;
- else
+ if (!tp->executing)
return tp;
+
+ tp_executing = tp;
}
+ /* If both the current thread and all live threads are executing,
+ prefer the current thread. */
+ if (curr_tp != NULL)
+ return curr_tp;
+
+ /* Otherwise, just return an executing thread, if any. */
return tp_executing;
}