+ event->ptid = pid_to_ptid (pid);
+ }
+ break;
+ }
+
+ if (non_stop && ptid_equal (event->ptid, null_ptid))
+ error (_("No process or thread specified in stop reply: %s"), buf);
+}
+
+/* When the stub wants to tell GDB about a new stop reply, it sends a
+ stop notification (%Stop). Those can come it at any time, hence,
+ we have to make sure that any pending putpkt/getpkt sequence we're
+ making is finished, before querying the stub for more events with
+ vStopped. E.g., if we started a vStopped sequence immediatelly
+ upon receiving the %Stop notification, something like this could
+ happen:
+
+ 1.1) --> Hg 1
+ 1.2) <-- OK
+ 1.3) --> g
+ 1.4) <-- %Stop
+ 1.5) --> vStopped
+ 1.6) <-- (registers reply to step #1.3)
+
+ Obviously, the reply in step #1.6 would be unexpected to a vStopped
+ query.
+
+ To solve this, whenever we parse a %Stop notification sucessfully,
+ we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
+ doing whatever we were doing:
+
+ 2.1) --> Hg 1
+ 2.2) <-- OK
+ 2.3) --> g
+ 2.4) <-- %Stop
+ <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
+ 2.5) <-- (registers reply to step #2.3)
+
+ Eventualy after step #2.5, we return to the event loop, which
+ notices there's an event on the
+ REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
+ associated callback --- the function below. At this point, we're
+ always safe to start a vStopped sequence. :
+
+ 2.6) --> vStopped
+ 2.7) <-- T05 thread:2
+ 2.8) --> vStopped
+ 2.9) --> OK
+*/
+
+static void
+remote_get_pending_stop_replies (void)
+{
+ struct remote_state *rs = get_remote_state ();
+ int ret;
+
+ if (pending_stop_reply)
+ {
+ /* acknowledge */
+ putpkt ("vStopped");
+
+ /* Now we can rely on it. */
+ push_stop_reply (pending_stop_reply);
+ pending_stop_reply = NULL;
+
+ while (1)
+ {
+ getpkt (&rs->buf, &rs->buf_size, 0);
+ if (strcmp (rs->buf, "OK") == 0)
+ break;
+ else
+ {
+ struct cleanup *old_chain;
+ struct stop_reply *stop_reply = stop_reply_xmalloc ();
+
+ old_chain = make_cleanup (do_stop_reply_xfree, stop_reply);
+ remote_parse_stop_reply (rs->buf, stop_reply);
+
+ /* acknowledge */
+ putpkt ("vStopped");
+
+ if (stop_reply->ws.kind != TARGET_WAITKIND_IGNORE)
+ {
+ /* Now we can rely on it. */
+ discard_cleanups (old_chain);
+ push_stop_reply (stop_reply);
+ }
+ else
+ /* We got an unknown stop reply. */
+ do_cleanups (old_chain);
+ }
+ }
+ }
+}
+
+
+/* Called when it is decided that STOP_REPLY holds the info of the
+ event that is to be returned to the core. This function always
+ destroys STOP_REPLY. */
+
+static ptid_t
+process_stop_reply (struct stop_reply *stop_reply,
+ struct target_waitstatus *status)
+{
+ ptid_t ptid;
+
+ *status = stop_reply->ws;
+ ptid = stop_reply->ptid;
+
+ /* If no thread/process was reported by the stub, assume the current
+ inferior. */
+ if (ptid_equal (ptid, null_ptid))
+ ptid = inferior_ptid;
+
+ if (status->kind == TARGET_WAITKIND_EXITED
+ || status->kind == TARGET_WAITKIND_SIGNALLED)
+ {
+ int pid = ptid_get_pid (ptid);
+ delete_inferior (pid);
+ }
+ else
+ notice_new_inferiors (ptid);
+
+ /* Expedited registers. */
+ if (stop_reply->regcache)
+ {
+ cached_reg_t *reg;
+ int ix;
+
+ for (ix = 0;
+ VEC_iterate(cached_reg_t, stop_reply->regcache, ix, reg);
+ ix++)
+ regcache_raw_supply (get_thread_regcache (ptid),
+ reg->num, reg->data);
+ VEC_free (cached_reg_t, stop_reply->regcache);
+ }
+
+ remote_stopped_by_watchpoint_p = stop_reply->stopped_by_watchpoint_p;
+ remote_watch_data_address = stop_reply->watch_data_address;
+
+ stop_reply_xfree (stop_reply);
+ return ptid;
+}
+
+/* The non-stop mode version of target_wait. */
+
+static ptid_t
+remote_wait_ns (ptid_t ptid, struct target_waitstatus *status)
+{
+ struct remote_state *rs = get_remote_state ();
+ struct remote_arch_state *rsa = get_remote_arch_state ();
+ ptid_t event_ptid = null_ptid;
+ struct stop_reply *stop_reply;
+ int ret;
+
+ /* If in non-stop mode, get out of getpkt even if a
+ notification is received. */
+
+ ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
+ 0 /* forever */);
+ while (1)
+ {
+ if (ret != -1)
+ switch (rs->buf[0])
+ {
+ case 'E': /* Error of some sort. */
+ /* We're out of sync with the target now. Did it continue
+ or not? We can't tell which thread it was in non-stop,
+ so just ignore this. */
+ warning (_("Remote failure reply: %s"), rs->buf);
+ break;
+ case 'O': /* Console output. */
+ remote_console_output (rs->buf + 1);
+ break;
+ default:
+ warning (_("Invalid remote reply: %s"), rs->buf);
+ break;
+ }
+
+ /* Acknowledge a pending stop reply that may have arrived in the
+ mean time. */
+ if (pending_stop_reply != NULL)
+ remote_get_pending_stop_replies ();
+
+ /* If indeed we noticed a stop reply, we're done. */
+ stop_reply = queued_stop_reply (ptid);
+ if (stop_reply != NULL)
+ return process_stop_reply (stop_reply, status);
+
+ /* Still no event. If we're in asynchronous mode, then just
+ return to the event loop. */
+ if (remote_is_async_p ())
+ {
+ status->kind = TARGET_WAITKIND_IGNORE;
+ return minus_one_ptid;
+ }
+
+ /* Otherwise, asynchronous mode is masked, so do a blocking
+ wait. */
+ ret = getpkt_or_notif_sane (&rs->buf, &rs->buf_size,
+ 1 /* forever */);
+ }
+}
+
+/* Wait until the remote machine stops, then return, storing status in
+ STATUS just as `wait' would. */
+
+static ptid_t
+remote_wait_as (ptid_t ptid, struct target_waitstatus *status)
+{
+ struct remote_state *rs = get_remote_state ();
+ struct remote_arch_state *rsa = get_remote_arch_state ();
+ ptid_t event_ptid = null_ptid;
+ ULONGEST addr;
+ int solibs_changed = 0;
+ char *buf, *p;
+ struct stop_reply *stop_reply;
+
+ status->kind = TARGET_WAITKIND_IGNORE;
+ status->value.integer = 0;
+
+ stop_reply = queued_stop_reply (ptid);
+ if (stop_reply != NULL)
+ return process_stop_reply (stop_reply, status);
+
+ if (rs->cached_wait_status)
+ /* Use the cached wait status, but only once. */
+ rs->cached_wait_status = 0;
+ else
+ {
+ int ret;
+
+ if (!target_is_async_p ())
+ {
+ ofunc = signal (SIGINT, remote_interrupt);
+ /* If the user hit C-c before this packet, or between packets,
+ pretend that it was hit right here. */
+ if (quit_flag)
+ {
+ quit_flag = 0;
+ remote_interrupt (SIGINT);
+ }
+ }
+
+ /* FIXME: cagney/1999-09-27: If we're in async mode we should
+ _never_ wait for ever -> test on target_is_async_p().
+ However, before we do that we need to ensure that the caller
+ knows how to take the target into/out of async mode. */
+ ret = getpkt_sane (&rs->buf, &rs->buf_size, wait_forever_enabled_p);
+ if (!target_is_async_p ())
+ signal (SIGINT, ofunc);
+ }
+
+ buf = rs->buf;
+
+ remote_stopped_by_watchpoint_p = 0;
+
+ /* We got something. */
+ rs->waiting_for_stop_reply = 0;
+
+ switch (buf[0])
+ {
+ case 'E': /* Error of some sort. */
+ /* We're out of sync with the target now. Did it continue or
+ not? Not is more likely, so report a stop. */
+ warning (_("Remote failure reply: %s"), buf);
+ status->kind = TARGET_WAITKIND_STOPPED;
+ status->value.sig = TARGET_SIGNAL_0;
+ break;
+ case 'F': /* File-I/O request. */
+ remote_fileio_request (buf);
+ break;
+ case 'T': case 'S': case 'X': case 'W':
+ {
+ 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 (buf, stop_reply);
+ discard_cleanups (old_chain);
+ event_ptid = process_stop_reply (stop_reply, status);