+ /* Find about threads and processes the stub is already
+ controlling. We default to adding them in the running state.
+ The '?' query below will then tell us about which threads are
+ stopped. */
+ remote_threads_info (args->target);
+ }
+ else if (rs->non_stop_aware)
+ {
+ /* Don't assume that the stub can operate in all-stop mode.
+ Request it explicitely. */
+ putpkt ("QNonStop:0");
+ getpkt (&rs->buf, &rs->buf_size, 0);
+
+ if (strcmp (rs->buf, "OK") != 0)
+ error ("Remote refused setting all-stop mode with: %s", rs->buf);
+ }
+
+ /* Check whether the target is running now. */
+ putpkt ("?");
+ getpkt (&rs->buf, &rs->buf_size, 0);
+
+ if (!non_stop)
+ {
+ if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
+ {
+ if (args->extended_p)
+ {
+ /* We're connected, but not running. Drop out before we
+ call start_remote. */
+ target_mark_exited (args->target);
+ return;
+ }
+ else
+ error (_("The target is not running (try extended-remote?)"));
+ }
+ else
+ {
+ /* Save the reply for later. */
+ wait_status = alloca (strlen (rs->buf) + 1);
+ strcpy (wait_status, rs->buf);
+ }
+
+ /* Let the stub know that we want it to return the thread. */
+ set_continue_thread (minus_one_ptid);
+
+ /* Without this, some commands which require an active target
+ (such as kill) won't work. This variable serves (at least)
+ double duty as both the pid of the target process (if it has
+ such), and as a flag indicating that a target is active.
+ These functions should be split out into seperate variables,
+ especially since GDB will someday have a notion of debugging
+ several processes. */
+ inferior_ptid = magic_null_ptid;
+
+ /* Now, if we have thread information, update inferior_ptid. */
+ inferior_ptid = remote_current_thread (inferior_ptid);
+
+ remote_add_inferior (ptid_get_pid (inferior_ptid), -1);
+
+ /* Always add the main thread. */
+ add_thread_silent (inferior_ptid);
+
+ get_offsets (); /* Get text, data & bss offsets. */
+
+ /* If we could not find a description using qXfer, and we know
+ how to do it some other way, try again. This is not
+ supported for non-stop; it could be, but it is tricky if
+ there are no stopped threads when we connect. */
+ if (remote_read_description_p (args->target)
+ && gdbarch_target_desc (target_gdbarch) == NULL)
+ {
+ target_clear_description ();
+ target_find_description ();
+ }
+
+ /* Use the previously fetched status. */
+ gdb_assert (wait_status != NULL);
+ strcpy (rs->buf, wait_status);
+ rs->cached_wait_status = 1;
+
+ immediate_quit--;
+ start_remote (args->from_tty); /* Initialize gdb process mechanisms. */
+ }
+ else
+ {
+ /* Clear WFI global state. Do this before finding about new
+ threads and inferiors, and setting the current inferior.
+ Otherwise we would clear the proceed status of the current
+ inferior when we want its stop_soon state to be preserved
+ (see notice_new_inferior). */
+ init_wait_for_inferior ();
+
+ /* In non-stop, we will either get an "OK", meaning that there
+ are no stopped threads at this time; or, a regular stop
+ reply. In the latter case, there may be more than one thread
+ stopped --- we pull them all out using the vStopped
+ mechanism. */
+ if (strcmp (rs->buf, "OK") != 0)
+ {
+ struct stop_reply *stop_reply;
+ struct cleanup *old_chain;
+
+ stop_reply = stop_reply_xmalloc ();
+ old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
+
+ remote_parse_stop_reply (rs->buf, stop_reply);
+ discard_cleanups (old_chain);
+
+ /* get_pending_stop_replies acks this one, and gets the rest
+ out. */
+ pending_stop_reply = stop_reply;
+ remote_get_pending_stop_replies ();
+
+ /* Make sure that threads that were stopped remain
+ stopped. */
+ iterate_over_threads (set_stop_requested_callback, NULL);
+ }
+
+ if (target_can_async_p ())
+ target_async (inferior_event_handler, 0);
+
+ if (thread_count () == 0)
+ {
+ if (args->extended_p)
+ {
+ /* We're connected, but not running. Drop out before we
+ call start_remote. */
+ target_mark_exited (args->target);
+ return;
+ }
+ else
+ error (_("The target is not running (try extended-remote?)"));
+ }
+
+ if (args->extended_p)
+ target_mark_running (args->target);
+
+ /* Let the stub know that we want it to return the thread. */
+
+ /* Force the stub to choose a thread. */
+ set_general_thread (null_ptid);
+
+ /* Query it. */
+ inferior_ptid = remote_current_thread (minus_one_ptid);
+ if (ptid_equal (inferior_ptid, minus_one_ptid))
+ error (_("remote didn't report the current thread in non-stop mode"));
+
+ get_offsets (); /* Get text, data & bss offsets. */
+
+ /* In non-stop mode, any cached wait status will be stored in
+ the stop reply queue. */
+ gdb_assert (wait_status == NULL);
+ }
+
+ /* If we connected to a live target, do some additional setup. */
+ if (target_has_execution)
+ {
+ if (exec_bfd) /* No use without an exec file. */
+ remote_check_symbols (symfile_objfile);
+ }
+
+ /* If breakpoints are global, insert them now. */
+ if (gdbarch_has_global_breakpoints (target_gdbarch)
+ && breakpoints_always_inserted_mode ())
+ insert_breakpoints ();
+}
+
+/* Open a connection to a remote debugger.
+ NAME is the filename used for communication. */
+
+static void
+remote_open (char *name, int from_tty)
+{
+ remote_open_1 (name, from_tty, &remote_ops, 0);
+}
+
+/* Open a connection to a remote debugger using the extended
+ remote gdb protocol. NAME is the filename used for communication. */
+
+static void
+extended_remote_open (char *name, int from_tty)
+{
+ remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */);
+}