+ tp = find_thread_ptid (old_ptid);
+ tp->ptid = new_ptid;
+
+ observer_notify_thread_ptid_changed (old_ptid, new_ptid);
+}
+
+/* See gdbthread.h. */
+
+void
+set_resumed (ptid_t ptid, int resumed)
+{
+ struct thread_info *tp;
+ int all = ptid == minus_one_ptid;
+
+ if (all || ptid_is_pid (ptid))
+ {
+ for (tp = thread_list; tp; tp = tp->next)
+ if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
+ tp->resumed = resumed;
+ }
+ else
+ {
+ tp = find_thread_ptid (ptid);
+ gdb_assert (tp != NULL);
+ tp->resumed = resumed;
+ }
+}
+
+/* Helper for set_running, that marks one thread either running or
+ stopped. */
+
+static int
+set_running_thread (struct thread_info *tp, int running)
+{
+ int started = 0;
+
+ if (running && tp->state == THREAD_STOPPED)
+ started = 1;
+ tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
+
+ if (!running)
+ {
+ /* If the thread is now marked stopped, remove it from
+ the step-over queue, so that we don't try to resume
+ it until the user wants it to. */
+ if (tp->step_over_next != NULL)
+ thread_step_over_chain_remove (tp);
+ }
+
+ return started;
+}
+
+void
+set_running (ptid_t ptid, int running)
+{
+ struct thread_info *tp;
+ int all = ptid == minus_one_ptid;
+ int any_started = 0;
+
+ /* We try not to notify the observer if no thread has actually changed
+ the running state -- merely to reduce the number of messages to
+ frontend. Frontend is supposed to handle multiple *running just fine. */
+ if (all || ptid_is_pid (ptid))
+ {
+ for (tp = thread_list; tp; tp = tp->next)
+ if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
+ {
+ if (tp->state == THREAD_EXITED)
+ continue;
+
+ if (set_running_thread (tp, running))
+ any_started = 1;
+ }
+ }
+ else
+ {
+ tp = find_thread_ptid (ptid);
+ gdb_assert (tp != NULL);
+ gdb_assert (tp->state != THREAD_EXITED);
+ if (set_running_thread (tp, running))
+ any_started = 1;
+ }
+ if (any_started)
+ observer_notify_target_resumed (ptid);
+}
+
+static int
+is_thread_state (ptid_t ptid, enum thread_state state)
+{
+ struct thread_info *tp;
+
+ tp = find_thread_ptid (ptid);
+ gdb_assert (tp);
+ return tp->state == state;
+}
+
+int
+is_stopped (ptid_t ptid)
+{
+ return is_thread_state (ptid, THREAD_STOPPED);
+}
+
+int
+is_exited (ptid_t ptid)
+{
+ return is_thread_state (ptid, THREAD_EXITED);
+}
+
+int
+is_running (ptid_t ptid)
+{
+ return is_thread_state (ptid, THREAD_RUNNING);
+}
+
+int
+is_executing (ptid_t ptid)
+{
+ struct thread_info *tp;
+
+ tp = find_thread_ptid (ptid);
+ gdb_assert (tp);
+ return tp->executing;
+}
+
+void
+set_executing (ptid_t ptid, int executing)
+{
+ struct thread_info *tp;
+ int all = ptid == minus_one_ptid;
+
+ if (all || ptid_is_pid (ptid))
+ {
+ for (tp = thread_list; tp; tp = tp->next)
+ if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
+ tp->executing = executing;
+ }
+ else
+ {
+ tp = find_thread_ptid (ptid);
+ gdb_assert (tp);
+ tp->executing = executing;
+ }
+
+ /* It only takes one running thread to spawn more threads.*/
+ if (executing)
+ threads_executing = 1;
+ /* Only clear the flag if the caller is telling us everything is
+ stopped. */
+ else if (minus_one_ptid == ptid)
+ threads_executing = 0;
+}
+
+/* See gdbthread.h. */
+
+int
+threads_are_executing (void)
+{
+ return threads_executing;
+}
+
+void
+set_stop_requested (ptid_t ptid, int stop)
+{
+ struct thread_info *tp;
+ int all = ptid == minus_one_ptid;
+
+ if (all || ptid_is_pid (ptid))
+ {
+ for (tp = thread_list; tp; tp = tp->next)
+ if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
+ tp->stop_requested = stop;
+ }
+ else
+ {
+ tp = find_thread_ptid (ptid);
+ gdb_assert (tp);
+ tp->stop_requested = stop;
+ }
+
+ /* Call the stop requested observer so other components of GDB can
+ react to this request. */
+ if (stop)
+ observer_notify_thread_stop_requested (ptid);
+}
+
+void
+finish_thread_state (ptid_t ptid)
+{
+ struct thread_info *tp;
+ int all;
+ int any_started = 0;
+
+ all = ptid == minus_one_ptid;
+
+ if (all || ptid_is_pid (ptid))
+ {
+ for (tp = thread_list; tp; tp = tp->next)
+ {
+ if (tp->state == THREAD_EXITED)
+ continue;
+ if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
+ {
+ if (set_running_thread (tp, tp->executing))
+ any_started = 1;
+ }
+ }
+ }
+ else
+ {
+ tp = find_thread_ptid (ptid);
+ gdb_assert (tp);
+ if (tp->state != THREAD_EXITED)
+ {
+ if (set_running_thread (tp, tp->executing))
+ any_started = 1;
+ }
+ }
+
+ if (any_started)
+ observer_notify_target_resumed (ptid);
+}
+
+void
+finish_thread_state_cleanup (void *arg)
+{
+ ptid_t *ptid_p = (ptid_t *) arg;
+
+ gdb_assert (arg);
+
+ finish_thread_state (*ptid_p);
+}
+
+/* See gdbthread.h. */
+
+void
+validate_registers_access (void)
+{
+ /* No selected thread, no registers. */
+ if (inferior_ptid == null_ptid)
+ error (_("No thread selected."));
+
+ /* Don't try to read from a dead thread. */
+ if (is_exited (inferior_ptid))
+ error (_("The current thread has terminated"));
+
+ /* ... or from a spinning thread. FIXME: This isn't actually fully
+ correct. It'll allow an user-requested access (e.g., "print $pc"
+ at the prompt) when a thread is not executing for some internal
+ reason, but is marked running from the user's perspective. E.g.,
+ the thread is waiting for its turn in the step-over queue. */
+ if (is_executing (inferior_ptid))
+ error (_("Selected thread is running."));
+}
+
+/* See gdbthread.h. */
+
+bool
+can_access_registers_ptid (ptid_t ptid)
+{
+ /* No thread, no registers. */
+ if (ptid == null_ptid)
+ return false;
+
+ /* Don't try to read from a dead thread. */
+ if (is_exited (ptid))
+ return false;
+
+ /* ... or from a spinning thread. FIXME: see validate_registers_access. */
+ if (is_executing (ptid))
+ return false;
+
+ return true;
+}
+
+int
+pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
+{
+ return (pc >= thread->control.step_range_start
+ && pc < thread->control.step_range_end);
+}
+
+/* Helper for print_thread_info. Returns true if THR should be
+ printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
+ NULL, only print THR if its ID is included in the list. GLOBAL_IDS
+ is true if REQUESTED_THREADS is list of global IDs, false if a list
+ of per-inferior thread ids. If PID is not -1, only print THR if it
+ is a thread from the process PID. Otherwise, threads from all
+ attached PIDs are printed. If both REQUESTED_THREADS is not NULL
+ and PID is not -1, then the thread is printed if it belongs to the
+ specified process. Otherwise, an error is raised. */
+
+static int
+should_print_thread (const char *requested_threads, int default_inf_num,
+ int global_ids, int pid, struct thread_info *thr)
+{
+ if (requested_threads != NULL && *requested_threads != '\0')