+/* Print info about a thread that was found already stopped on
+ connection. */
+
+static void
+print_one_stopped_thread (struct thread_info *thread)
+{
+ struct target_waitstatus *ws = &thread->suspend.waitstatus;
+
+ switch_to_thread (thread->ptid);
+ stop_pc = get_frame_pc (get_current_frame ());
+ set_current_sal_from_frame (get_current_frame ());
+
+ thread->suspend.waitstatus_pending_p = 0;
+
+ if (ws->kind == TARGET_WAITKIND_STOPPED)
+ {
+ enum gdb_signal sig = ws->value.sig;
+
+ if (signal_print_state (sig))
+ observer_notify_signal_received (sig);
+ }
+ observer_notify_normal_stop (NULL, 1);
+}
+
+/* Process all initial stop replies the remote side sent in response
+ to the ? packet. These indicate threads that were already stopped
+ on initial connection. We mark these threads as stopped and print
+ their current frame before giving the user the prompt. */
+
+static void
+process_initial_stop_replies (int from_tty)
+{
+ int pending_stop_replies = stop_reply_queue_length ();
+ struct inferior *inf;
+ struct thread_info *thread;
+ struct thread_info *selected = NULL;
+ struct thread_info *lowest_stopped = NULL;
+ struct thread_info *first = NULL;
+
+ /* Consume the initial pending events. */
+ while (pending_stop_replies-- > 0)
+ {
+ ptid_t waiton_ptid = minus_one_ptid;
+ ptid_t event_ptid;
+ struct target_waitstatus ws;
+ int ignore_event = 0;
+ struct thread_info *thread;
+
+ memset (&ws, 0, sizeof (ws));
+ event_ptid = target_wait (waiton_ptid, &ws, TARGET_WNOHANG);
+ if (remote_debug)
+ print_target_wait_results (waiton_ptid, event_ptid, &ws);
+
+ switch (ws.kind)
+ {
+ case TARGET_WAITKIND_IGNORE:
+ case TARGET_WAITKIND_NO_RESUMED:
+ case TARGET_WAITKIND_SIGNALLED:
+ case TARGET_WAITKIND_EXITED:
+ /* We shouldn't see these, but if we do, just ignore. */
+ if (remote_debug)
+ fprintf_unfiltered (gdb_stdlog, "remote: event ignored\n");
+ ignore_event = 1;
+ break;
+
+ case TARGET_WAITKIND_EXECD:
+ xfree (ws.value.execd_pathname);
+ break;
+ default:
+ break;
+ }
+
+ if (ignore_event)
+ continue;
+
+ thread = find_thread_ptid (event_ptid);
+
+ if (ws.kind == TARGET_WAITKIND_STOPPED)
+ {
+ enum gdb_signal sig = ws.value.sig;
+
+ /* Stubs traditionally report SIGTRAP as initial signal,
+ instead of signal 0. Suppress it. */
+ if (sig == GDB_SIGNAL_TRAP)
+ sig = GDB_SIGNAL_0;
+ thread->suspend.stop_signal = sig;
+ ws.value.sig = sig;
+ }
+
+ thread->suspend.waitstatus = ws;
+
+ if (ws.kind != TARGET_WAITKIND_STOPPED
+ || ws.value.sig != GDB_SIGNAL_0)
+ thread->suspend.waitstatus_pending_p = 1;
+
+ set_executing (event_ptid, 0);
+ set_running (event_ptid, 0);
+ }
+
+ /* "Notice" the new inferiors before anything related to
+ registers/memory. */
+ ALL_INFERIORS (inf)
+ {
+ if (inf->pid == 0)
+ continue;
+
+ inf->needs_setup = 1;
+
+ if (non_stop)
+ {
+ thread = any_live_thread_of_process (inf->pid);
+ notice_new_inferior (thread->ptid,
+ thread->state == THREAD_RUNNING,
+ from_tty);
+ }
+ }
+
+ /* If all-stop on top of non-stop, pause all threads. Note this
+ records the threads' stop pc, so must be done after "noticing"
+ the inferiors. */
+ if (!non_stop)
+ {
+ stop_all_threads ();
+
+ /* If all threads of an inferior were already stopped, we
+ haven't setup the inferior yet. */
+ ALL_INFERIORS (inf)
+ {
+ if (inf->pid == 0)
+ continue;
+
+ if (inf->needs_setup)
+ {
+ thread = any_live_thread_of_process (inf->pid);
+ switch_to_thread_no_regs (thread);
+ setup_inferior (0);
+ }
+ }
+ }
+
+ /* Now go over all threads that are stopped, and print their current
+ frame. If all-stop, then if there's a signalled thread, pick
+ that as current. */
+ ALL_NON_EXITED_THREADS (thread)
+ {
+ if (first == NULL)
+ first = thread;
+
+ if (!non_stop)
+ set_running (thread->ptid, 0);
+ else if (thread->state != THREAD_STOPPED)
+ continue;
+
+ if (selected == NULL
+ && thread->suspend.waitstatus_pending_p)
+ selected = thread;
+
+ if (lowest_stopped == NULL
+ || thread->inf->num < lowest_stopped->inf->num
+ || thread->per_inf_num < lowest_stopped->per_inf_num)
+ lowest_stopped = thread;
+
+ if (non_stop)
+ print_one_stopped_thread (thread);
+ }
+
+ /* In all-stop, we only print the status of one thread, and leave
+ others with their status pending. */
+ if (!non_stop)
+ {
+ thread = selected;
+ if (thread == NULL)
+ thread = lowest_stopped;
+ if (thread == NULL)
+ thread = first;
+
+ print_one_stopped_thread (thread);
+ }
+
+ /* For "info program". */
+ thread = inferior_thread ();
+ if (thread->state == THREAD_STOPPED)
+ set_last_target_status (inferior_ptid, thread->suspend.waitstatus);
+}
+
+/* Start the remote connection and sync state. */
+