From 74531fed1f2d662debc2c209b8b3faddceb55960 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Fri, 24 Oct 2008 20:38:26 +0000 Subject: [PATCH] Remote non-stop mode support. * remote.c (pending_stop_reply): New. (struct remote_state) : New fields. (remote_async_inferior_event_token) (remote_async_get_pending_events_token): New. (notice_new_inferiors): New, abstracted out from record_currthread. (record_currthread): Call it. (remote_threads_info): Default threads to running in non-stop mode. In non-stop mode, only qfThreadInfo is supported. (remote_close): Discard all pending stop_replies. Close the event sources. (set_stop_requested_callback): New. (remote_start_remote): Implement non-stop mode startup. In all-stop, don't clear the thread list here. (remote_non_stop_feature): New. (remote_protocol_features): Add a "QNonStop" feature. (remote_open_1): Clear cached_wait_status and non_stop_aware. Clear the thread list here. (remote_detach_1): Discard pending stop replies of the process we detached from. (extended_remote_attach_1): Implement non-stop mode. (remote_vcont_probe): Recognize `vCont;t'. (remote_vcont_resume): Implement non-stop mode. (remote_resume): Don't set waiting_for_stop_reply in non-stop mode. (remote_stop_ns): New. (remote_stop): Rename to ... (remote_stop_as): ... this. If we have a cached wait status, don't bother interrupting the remote. (remote_stop): Reimplement as wrapper around remote_stop_as and remote_stop_ns. (interrupt_query): Don't query in async mode. (struct cached_reg, cahed_reg_t): New. (struct stop_reply): New. (stop_reply_queue): New. (stop_reply_xmalloc, stop_reply_xfree) (discard_pending_stop_replies, do_stop_reply_xfree) (queued_stop_reply, push_stop_reply, peek_stop_reply) (remote_parse_stop_reply, remote_get_pending_stop_replies) (process_stop_reply): New. (remote_wait_ns): New. (remote_wait_as): Use remote_parse_stop_reply. Invalidate the notion of current general thread is a process exit was reported. (remote_wait): Call remote_wait_ns in non-stop mode. (handle_notification): New. (putpkt_binary): Handle notifications. Don't care for waiting_for_stop_reply in non-stop mode. (getpkt_sane): Rename to ... (getpkt_or_notif_sane_1): ... this. Add `expecting_notif' argument. Handle it. Handle notifications. (getpkt_sane): Reimplement as wrapper around getpkt_or_notif_sane_1. (getpkt_or_notif_sane): New. (select_new_thread_callback): Check for exited state instead of comparing the ptid against minus_one_ptid. (extended_remote_create_inferior_1): Query the remote about the current thread. (remote_supports_non_stop): New. (init_remote_ops): Register it. (remote_async_inferior_event_handler): New. (remote_async_get_pending_events_handler): New. * infcmd.c (proceed_thread_callback): Comment. (proceed_after_attach_callback, proceed_after_attach): New. (attach_command_post_wait): In background attach, resume all threads, but only if they are unsignalled, and not explicitly stopped. In foreground attach, in non-stop mode, make sure to stop all threads of the just attached to process. (attach_command): In non-stop mode: If doing a background attach, stop at least one thread. If a foreground attach, stop all threads. --- gdb/ChangeLog | 75 +++ gdb/infcmd.c | 101 +++- gdb/remote.c | 1310 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 1285 insertions(+), 201 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 75ea020b160..e4fec8733b3 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,78 @@ +2008-10-24 Pedro Alves + + Remote non-stop mode support. + + * remote.c (pending_stop_reply): New. + (struct remote_state) : New + fields. + (remote_async_inferior_event_token) + (remote_async_get_pending_events_token): New. + (notice_new_inferiors): New, abstracted out from record_currthread. + (record_currthread): Call it. + (remote_threads_info): Default threads to running in non-stop + mode. In non-stop mode, only qfThreadInfo is supported. + (remote_close): Discard all pending stop_replies. Close the event + sources. + (set_stop_requested_callback): New. + (remote_start_remote): Implement non-stop mode startup. In + all-stop, don't clear the thread list here. + (remote_non_stop_feature): New. + (remote_protocol_features): Add a "QNonStop" feature. + (remote_open_1): Clear cached_wait_status and non_stop_aware. + Clear the thread list here. + (remote_detach_1): Discard pending stop replies of the process we + detached from. + (extended_remote_attach_1): Implement non-stop mode. + (remote_vcont_probe): Recognize `vCont;t'. + (remote_vcont_resume): Implement non-stop mode. + (remote_resume): Don't set waiting_for_stop_reply in non-stop + mode. + (remote_stop_ns): New. + (remote_stop): Rename to ... + (remote_stop_as): ... this. If we have a cached wait status, + don't bother interrupting the remote. + (remote_stop): Reimplement as wrapper around remote_stop_as and + remote_stop_ns. + (interrupt_query): Don't query in async mode. + (struct cached_reg, cahed_reg_t): New. + (struct stop_reply): New. + (stop_reply_queue): New. + (stop_reply_xmalloc, stop_reply_xfree) + (discard_pending_stop_replies, do_stop_reply_xfree) + (queued_stop_reply, push_stop_reply, peek_stop_reply) + (remote_parse_stop_reply, remote_get_pending_stop_replies) + (process_stop_reply): New. + (remote_wait_ns): New. + (remote_wait_as): Use remote_parse_stop_reply. Invalidate the + notion of current general thread is a process exit was reported. + (remote_wait): Call remote_wait_ns in non-stop mode. + (handle_notification): New. + (putpkt_binary): Handle notifications. Don't care for + waiting_for_stop_reply in non-stop mode. + (getpkt_sane): Rename to ... + (getpkt_or_notif_sane_1): ... this. Add `expecting_notif' + argument. Handle it. Handle notifications. + (getpkt_sane): Reimplement as wrapper around getpkt_or_notif_sane_1. + (getpkt_or_notif_sane): New. + (select_new_thread_callback): Check for exited state instead of + comparing the ptid against minus_one_ptid. + (extended_remote_create_inferior_1): Query the remote about the + current thread. + (remote_supports_non_stop): New. + (init_remote_ops): Register it. + (remote_async_inferior_event_handler): New. + (remote_async_get_pending_events_handler): New. + + * infcmd.c (proceed_thread_callback): Comment. + (proceed_after_attach_callback, proceed_after_attach): New. + (attach_command_post_wait): In background attach, resume all + threads, but only if they are unsignalled, and not explicitly + stopped. In foreground attach, in non-stop mode, make sure to + stop all threads of the just attached to process. + (attach_command): In non-stop mode: If doing a background attach, + stop at least one thread. If a foreground attach, stop all + threads. + 2008-10-24 Pedro Alves * event-loop.h: Mention async_event_handlers. diff --git a/gdb/infcmd.c b/gdb/infcmd.c index 06e3bc5fc31..1854985d47f 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -570,6 +570,15 @@ start_command (char *args, int from_tty) static int proceed_thread_callback (struct thread_info *thread, void *arg) { + /* We go through all threads individually instead of compressing + into a single target `resume_all' request, because some threads + may be stopped in internal breakpoints/events, or stopped waiting + for its turn in the displaced stepping queue (that is, they are + running && !executing). The target side has no idea about why + the thread is stopped, so a `resume_all' command would resume too + much. If/when GDB gains a way to tell the target `hold this + thread stopped until I say otherwise', then we can optimize + this. */ if (!is_stopped (thread->ptid)) return 0; @@ -2011,6 +2020,48 @@ vector_info (char *args, int from_tty) } +/* Used in `attach&' command. ARG is a point to an integer + representing a process id. Proceed threads of this process iff + they stopped due to debugger request, and when they did, they + reported a clean stop (TARGET_SIGNAL_0). Do not proceed threads + that have been explicitly been told to stop. */ + +static int +proceed_after_attach_callback (struct thread_info *thread, + void *arg) +{ + int pid = * (int *) arg; + + if (ptid_get_pid (thread->ptid) == pid + && !is_exited (thread->ptid) + && !is_executing (thread->ptid) + && !thread->stop_requested + && thread->stop_signal == TARGET_SIGNAL_0) + { + switch_to_thread (thread->ptid); + clear_proceed_status (); + proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0); + } + + return 0; +} + +static void +proceed_after_attach (int pid) +{ + /* Don't error out if the current thread is running, because + there may be other stopped threads. */ + struct cleanup *old_chain; + + /* Backup current thread and selected frame. */ + old_chain = make_cleanup_restore_current_thread (); + + iterate_over_threads (proceed_after_attach_callback, &pid); + + /* Restore selected ptid. */ + do_cleanups (old_chain); +} + /* * TODO: * Should save/restore the tty state since it might be that the @@ -2075,11 +2126,44 @@ attach_command_post_wait (char *args, int from_tty, int async_exec) target_terminal_inferior (); if (async_exec) - proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0); + { + /* The user requested an `attach&', so be sure to leave threads + that didn't get a signal running. */ + + /* Immediatelly resume all suspended threads of this inferior, + and this inferior only. This should have no effect on + already running threads. If a thread has been stopped with a + signal, leave it be. */ + if (non_stop) + proceed_after_attach (inferior->pid); + else + { + if (inferior_thread ()->stop_signal == TARGET_SIGNAL_0) + { + clear_proceed_status (); + proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0); + } + } + } else { + /* The user requested a plain `attach', so be sure to leave + the inferior stopped. */ + if (target_can_async_p ()) async_enable_stdin (); + + /* At least the current thread is already stopped. */ + + /* In all-stop, by definition, all threads have to be already + stopped at this point. In non-stop, however, although the + selected thread is stopped, others may still be executing. + Be sure to explicitly stop all threads of the process. This + should have no effect on already stopped threads. */ + if (non_stop) + target_stop (pid_to_ptid (inferior->pid)); + + /* Tell the user/frontend where we're stopped. */ normal_stop (); if (deprecated_attach_hook) deprecated_attach_hook (); @@ -2161,6 +2245,21 @@ attach_command (char *args, int from_tty) init_wait_for_inferior (); clear_proceed_status (); + if (non_stop) + { + /* If we find that the current thread isn't stopped, explicitly + do so now, because we're going to install breakpoints and + poke at memory. */ + + if (async_exec) + /* The user requested an `attach&'; stop just one thread. */ + target_stop (inferior_ptid); + else + /* The user requested an `attach', so stop all threads of this + inferior. */ + target_stop (pid_to_ptid (ptid_get_pid (inferior_ptid))); + } + /* Some system don't generate traps when attaching to inferior. E.g. Mach 3 or GNU hurd. */ if (!target_attach_no_wait) diff --git a/gdb/remote.c b/gdb/remote.c index f334c0b9b57..da0a429aed0 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -79,6 +79,8 @@ enum { REMOTE_ALIGN_WRITES = 16 }; static void cleanup_sigint_signal_handler (void *dummy); static void initialize_sigint_signal_handler (void); static int getpkt_sane (char **buf, long *sizeof_buf, int forever); +static int getpkt_or_notif_sane (char **buf, long *sizeof_buf, + int forever); static void handle_remote_sigint (int); static void handle_remote_sigint_twice (int); @@ -217,6 +219,24 @@ static void remote_check_symbols (struct objfile *objfile); void _initialize_remote (void); +struct stop_reply; +static struct stop_reply *stop_reply_xmalloc (void); +static void stop_reply_xfree (struct stop_reply *); +static void do_stop_reply_xfree (void *arg); +static void remote_parse_stop_reply (char *buf, struct stop_reply *); +static void push_stop_reply (struct stop_reply *); +static void remote_get_pending_stop_replies (void); +static void discard_pending_stop_replies (int pid); +static int peek_stop_reply (ptid_t ptid); + +static void remote_async_inferior_event_handler (gdb_client_data); +static void remote_async_get_pending_events_handler (gdb_client_data); + +/* The non-stop remote protocol provisions for one pending stop reply. + This is where we keep it until it is acknowledged. */ + +static struct stop_reply *pending_stop_reply = NULL; + /* For "remote". */ static struct cmd_list_element *remote_cmdlist; @@ -275,6 +295,12 @@ struct remote_state we'd close the connection. This can happen in asynchronous mode because we allow GDB commands while the target is running. */ int waiting_for_stop_reply; + + /* True if the stub reports support for non-stop mode. */ + int non_stop_aware; + + /* True if the stub reports support for vCont;t. */ + int support_vCont_t; }; /* Returns true if the multi-process extensions are in effect. */ @@ -1072,6 +1098,18 @@ static int use_threadextra_query; static struct async_signal_handler *sigint_remote_twice_token; static struct async_signal_handler *sigint_remote_token; + +/* Asynchronous signal handle registered as event loop source for + when we have pending events ready to be passed to the core. */ + +static struct async_event_handler *remote_async_inferior_event_token; + +/* Asynchronous signal handle registered as event loop source for when + the remote sent us a %Stop notification. The registered callback + will do a vStopped sequence to pull the rest of the events out of + the remote side into our event queue. */ + +static struct async_event_handler *remote_async_get_pending_events_token; static ptid_t magic_null_ptid; @@ -1084,18 +1122,9 @@ static ptid_t any_thread_ptid; static ptid_t general_thread; static ptid_t continue_thread; - -/* Call this function as a result of - 1) A halt indication (T packet) containing a thread id - 2) A direct query of currthread - 3) Successful execution of set thread - */ - static void -record_currthread (ptid_t currthread) +notice_new_inferiors (ptid_t currthread) { - general_thread = currthread; - /* When connecting to a target remote, or to a target extended-remote which already was debugging an inferior, we may not know about it yet. Add it before adding its child thread, so @@ -1143,6 +1172,24 @@ record_currthread (ptid_t currthread) } } +/* Call this function as a result of + 1) A halt indication (T packet) containing a thread id + 2) A direct query of currthread + 3) Successful execution of set thread + */ + +static void +record_currthread (ptid_t currthread) +{ + general_thread = currthread; + + if (ptid_equal (currthread, minus_one_ptid)) + /* We're just invalidating the local thread mirror. */ + return; + + notice_new_inferiors (currthread); +} + static char *last_pass_packet; /* If 'QPassSignals' is supported, tell the remote stub what signals @@ -2112,6 +2159,13 @@ remote_threads_info (void) add_inferior (ptid_get_pid (new_thread)); add_thread (new_thread); + + /* In non-stop mode, we assume new found threads + are running until we proven otherwise with a + stop reply. In all-stop, we can only get + here if all threads are stopped. */ + set_executing (new_thread, non_stop ? 1 : 0); + set_running (new_thread, non_stop ? 1 : 0); } } while (*bufp++ == ','); /* comma-separated list */ @@ -2123,6 +2177,10 @@ remote_threads_info (void) } } + /* Only qfThreadInfo is supported in non-stop mode. */ + if (non_stop) + return; + /* Else fall back to old method based on jmetzler protocol. */ use_threadinfo_query = 0; remote_find_new_threads (); @@ -2246,6 +2304,14 @@ remote_close (int quitting) of all the inferiors and their threads we were controlling. */ discard_all_inferiors (); + /* We're no longer interested in any of these events. */ + discard_pending_stop_replies (-1); + + if (remote_async_inferior_event_token) + delete_async_event_handler (&remote_async_inferior_event_token); + if (remote_async_get_pending_events_token) + delete_async_event_handler (&remote_async_get_pending_events_token); + generic_mourn_inferior (); } @@ -2406,6 +2472,21 @@ get_offsets (void) objfile_relocate (symfile_objfile, offs); } +/* Callback for iterate_over_threads. Set the STOP_REQUESTED flags in + threads we know are stopped already. This is used during the + initial remote connection in non-stop mode --- threads that are + reported as already being stopped are left stopped. */ + +static int +set_stop_requested_callback (struct thread_info *thread, void *data) +{ + /* If we have a stop reply for this thread, it must be stopped. */ + if (peek_stop_reply (thread->ptid)) + set_stop_requested (thread->ptid, 1); + + return 0; +} + /* Stub for catch_exception. */ struct start_remote_args @@ -2473,64 +2554,162 @@ remote_start_remote (struct ui_out *uiout, void *opaque) this before anything involving memory or registers. */ target_find_description (); + if (non_stop) + { + if (!rs->non_stop_aware) + error (_("Non-stop mode requested, but remote does not support non-stop")); + + putpkt ("QNonStop:1"); + getpkt (&rs->buf, &rs->buf_size, 0); + + if (strcmp (rs->buf, "OK") != 0) + error ("Remote refused setting non-stop mode with: %s", rs->buf); + + /* 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. */ + + /* If we're not using the multi-process extensions, there's no + way to know the pid of the reported threads; use the magic + number. */ + if (!remote_multi_process_p (rs)) + inferior_ptid = magic_null_ptid; + + remote_threads_info (); + } + 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 (rs->buf[0] == 'W' || rs->buf[0] == 'X') + if (!non_stop) { - if (args->extended_p) + if (rs->buf[0] == 'W' || rs->buf[0] == 'X') { - /* We're connected, but not running. Drop out before we - call start_remote. */ - target_mark_exited (args->target); - return; + 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 - error (_("The target is not running (try extended-remote?)")); + { + if (args->extended_p) + target_mark_running (args->target); + + /* 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); + + add_inferior (ptid_get_pid (inferior_ptid)); + + /* Always add the main thread. */ + add_thread_silent (inferior_ptid); + + get_offsets (); /* Get text, data & bss offsets. */ + + /* 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 { - if (args->extended_p) - target_mark_running (args->target); + /* 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; - /* Save the reply for later. */ - wait_status = alloca (strlen (rs->buf) + 1); - strcpy (wait_status, rs->buf); - } + stop_reply = stop_reply_xmalloc (); + old_chain = make_cleanup (do_stop_reply_xfree, stop_reply); - /* Start afresh. */ - init_thread_list (); + remote_parse_stop_reply (rs->buf, stop_reply); + discard_cleanups (old_chain); - /* Let the stub know that we want it to return the thread. */ - set_continue_thread (minus_one_ptid); + /* get_pending_stop_replies acks this one, and gets the rest + out. */ + pending_stop_reply = stop_reply; + remote_get_pending_stop_replies (); - /* 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; + /* Make sure that threads that were stopped remain + stopped. */ + iterate_over_threads (set_stop_requested_callback, NULL); + } - /* Now, if we have thread information, update inferior_ptid. */ - inferior_ptid = remote_current_thread (inferior_ptid); + if (target_can_async_p ()) + target_async (inferior_event_handler, 0); - add_inferior (ptid_get_pid (inferior_ptid)); + 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?)")); + } - /* Always add the main thread. */ - add_thread_silent (inferior_ptid); + if (args->extended_p) + target_mark_running (args->target); + + /* Let the stub know that we want it to return the thread. */ - get_offsets (); /* Get text, data & bss offsets. */ + /* Force the stub to choose a thread. */ + set_general_thread (null_ptid); - /* Use the previously fetched status. */ - gdb_assert (wait_status != NULL); - strcpy (rs->buf, wait_status); - rs->cached_wait_status = 1; + /* 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")); - immediate_quit--; - start_remote (args->from_tty); /* Initialize gdb process mechanisms. */ + 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) @@ -2734,6 +2913,14 @@ remote_multi_process_feature (const struct protocol_feature *feature, rs->multi_process_aware = (support == PACKET_ENABLE); } +static void +remote_non_stop_feature (const struct protocol_feature *feature, + enum packet_support support, const char *value) +{ + struct remote_state *rs = get_remote_state (); + rs->non_stop_aware = (support == PACKET_ENABLE); +} + static struct protocol_feature remote_protocol_features[] = { { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 }, { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet, @@ -2753,6 +2940,7 @@ static struct protocol_feature remote_protocol_features[] = { { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet, PACKET_QStartNoAckMode }, { "multiprocess", PACKET_DISABLE, remote_multi_process_feature, -1 }, + { "QNonStop", PACKET_DISABLE, remote_non_stop_feature, -1 }, }; static void @@ -2968,13 +3156,23 @@ remote_open_1 (char *name, int from_tty, struct target_ops *target, int extended /* Assume that the target is running, unless we learn otherwise. */ target_mark_running (target); + /* Register extra event sources in the event loop. */ + remote_async_inferior_event_token + = create_async_event_handler (remote_async_inferior_event_handler, + NULL); + remote_async_get_pending_events_token + = create_async_event_handler (remote_async_get_pending_events_handler, + NULL); + /* Reset the target state; these things will be queried either by remote_query_supported or as they are needed. */ init_all_packet_configs (); + rs->cached_wait_status = 0; rs->explicit_packet_size = 0; rs->noack_mode = 0; rs->multi_process_aware = 0; rs->extended = extended_p; + rs->non_stop_aware = 0; rs->waiting_for_stop_reply = 0; general_thread = not_sent_ptid; @@ -3002,6 +3200,9 @@ remote_open_1 (char *name, int from_tty, struct target_ops *target, int extended /* First delete any symbols previously loaded from shared libraries. */ no_shared_libraries (NULL, 0); + /* Start afresh. */ + init_thread_list (); + /* Start the remote connection. If error() or QUIT, discard this target (we'd otherwise be in an inconsistent state) and then propogate the error on up the exception chain. This ensures that @@ -3089,6 +3290,7 @@ remote_detach_1 (char *args, int from_tty, int extended) } } + discard_pending_stop_replies (pid); detach_inferior (pid); target_mourn_inferior (); } @@ -3156,9 +3358,16 @@ extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty) printf_unfiltered (_("Attached to %s\n"), target_pid_to_str (pid_to_ptid (pid))); - /* Save the reply for later. */ - wait_status = alloca (strlen (rs->buf) + 1); - strcpy (wait_status, rs->buf); + if (!non_stop) + { + /* Save the reply for later. */ + wait_status = alloca (strlen (rs->buf) + 1); + strcpy (wait_status, rs->buf); + } + else if (strcmp (rs->buf, "OK") != 0) + error (_("Attaching to %s failed with: %s"), + target_pid_to_str (pid_to_ptid (pid)), + rs->buf); } else if (remote_protocol_packets[PACKET_vAttach].support == PACKET_DISABLE) error (_("This target does not support attaching to a process")); @@ -3175,17 +3384,44 @@ extended_remote_attach_1 (struct target_ops *target, char *args, int from_tty) inf = add_inferior (pid); inf->attach_flag = 1; - /* Now, add the main thread to the thread list. */ - add_thread_silent (inferior_ptid); + if (non_stop) + /* Get list of threads. */ + remote_threads_info (); + else + /* Add the main thread to the thread list. */ + add_thread_silent (inferior_ptid); /* Next, if the target can specify a description, read it. We do this before anything involving memory or registers. */ target_find_description (); - /* Use the previously fetched status. */ - gdb_assert (wait_status != NULL); - strcpy (rs->buf, wait_status); - rs->cached_wait_status = 1; + if (!non_stop) + { + /* Use the previously fetched status. */ + gdb_assert (wait_status != NULL); + + if (target_can_async_p ()) + { + 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 (wait_status, stop_reply); + discard_cleanups (old_chain); + push_stop_reply (stop_reply); + + target_async (inferior_event_handler, 0); + } + else + { + gdb_assert (wait_status != NULL); + strcpy (rs->buf, wait_status); + rs->cached_wait_status = 1; + } + } + else + gdb_assert (wait_status == NULL); } static void @@ -3279,6 +3515,7 @@ remote_vcont_probe (struct remote_state *rs) support_S = 0; support_c = 0; support_C = 0; + rs->support_vCont_t = 0; while (p && *p == ';') { p++; @@ -3290,6 +3527,8 @@ remote_vcont_probe (struct remote_state *rs) support_c = 1; else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0)) support_C = 1; + else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0)) + rs->support_vCont_t = 1; p = strchr (p, ';'); } @@ -3410,6 +3649,16 @@ remote_vcont_resume (ptid_t ptid, int step, enum target_signal siggnal) gdb_assert (strlen (rs->buf) < get_remote_packet_size ()); putpkt (rs->buf); + if (non_stop) + { + /* In non-stop, the stub replies to vCont with "OK". The stop + reply will be reported asynchronously by means of a `%Stop' + notification. */ + getpkt (&rs->buf, &rs->buf_size, 0); + if (strcmp (rs->buf, "OK") != 0) + error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf); + } + return 1; } @@ -3477,8 +3726,12 @@ remote_resume (ptid_t ptid, int step, enum target_signal siggnal) /* We've just told the target to resume. The remote server will wait for the inferior to stop, and then send a stop reply. In the mean time, we can't start another command/query ourselves - because the stub wouldn't be ready to process it. */ - rs->waiting_for_stop_reply = 1; + because the stub wouldn't be ready to process it. This applies + only to the base all-stop protocol, however. In non-stop (which + only supports vCont), the stub replies with an "OK", and is + immediate able to process further serial input. */ + if (!non_stop) + rs->waiting_for_stop_reply = 1; } @@ -3566,20 +3819,95 @@ remote_interrupt_twice (int signo) signal (signo, remote_interrupt); } +/* Non-stop version of target_stop. Uses `vCont;t' to stop a remote + thread, all threads of a remote process, or all threads of all + processes. */ + +static void +remote_stop_ns (ptid_t ptid) +{ + struct remote_state *rs = get_remote_state (); + char *p = rs->buf; + char *endp = rs->buf + get_remote_packet_size (); + struct stop_reply *reply, *next; + + if (remote_protocol_packets[PACKET_vCont].support == PACKET_SUPPORT_UNKNOWN) + remote_vcont_probe (rs); + + if (!rs->support_vCont_t) + error (_("Remote server does not support stopping threads")); + + if (ptid_equal (ptid, minus_one_ptid)) + p += xsnprintf (p, endp - p, "vCont;t"); + else + { + ptid_t nptid; + + /* Step inferior_ptid. */ + p += xsnprintf (p, endp - p, "vCont;t:"); + + if (ptid_is_pid (ptid)) + /* All (-1) threads of process. */ + nptid = ptid_build (ptid_get_pid (ptid), 0, -1); + else + { + /* Small optimization: if we already have a stop reply for + this thread, no use in telling the stub we want this + stopped. */ + if (peek_stop_reply (ptid)) + return; + + nptid = ptid; + } + + p = write_ptid (p, endp, nptid); + } + + /* In non-stop, we get an immediate OK reply. The stop reply will + come in asynchronously by notification. */ + putpkt (rs->buf); + getpkt (&rs->buf, &rs->buf_size, 0); + if (strcmp (rs->buf, "OK") != 0) + error (_("Stopping %s failed: %s"), target_pid_to_str (ptid), rs->buf); +} + +/* All-stop version of target_stop. Sends a break or a ^C to stop the + remote target. It is undefined which thread of which process + reports the stop. */ + +static void +remote_stop_as (ptid_t ptid) +{ + struct remote_state *rs = get_remote_state (); + + /* If the inferior is stopped already, but the core didn't know + about it yet, just ignore the request. The cached wait status + will be collected in remote_wait. */ + if (rs->cached_wait_status) + return; + + /* Send a break or a ^C, depending on user preference. */ + + if (remote_break) + serial_send_break (remote_desc); + else + serial_write (remote_desc, "\003", 1); +} + /* This is the generic stop called via the target vector. When a target interrupt is requested, either by the command line or the GUI, we will eventually end up here. */ + static void remote_stop (ptid_t ptid) { - /* Send a break or a ^C, depending on user preference. */ if (remote_debug) fprintf_unfiltered (gdb_stdlog, "remote_stop called\n"); - if (remote_break) - serial_send_break (remote_desc); + if (non_stop) + remote_stop_ns (ptid); else - serial_write (remote_desc, "\003", 1); + remote_stop_as (ptid); } /* Ask the user what to do when an interrupt is received. */ @@ -3589,12 +3917,20 @@ interrupt_query (void) { target_terminal_ours (); - if (query ("Interrupted while waiting for the program.\n\ -Give up (and stop debugging it)? ")) + if (target_can_async_p ()) { - pop_target (); + signal (SIGINT, handle_sigint); deprecated_throw_reason (RETURN_QUIT); } + else + { + if (query ("Interrupted while waiting for the program.\n\ +Give up (and stop debugging it)? ")) + { + pop_target (); + deprecated_throw_reason (RETURN_QUIT); + } + } target_terminal_inferior (); } @@ -3665,86 +4001,210 @@ remote_console_output (char *msg) tb[1] = 0; fputs_unfiltered (tb, gdb_stdtarg); } - gdb_flush (gdb_stdtarg); + gdb_flush (gdb_stdtarg); + } + +typedef struct cached_reg +{ + int num; + gdb_byte data[MAX_REGISTER_SIZE]; +} cached_reg_t; + +DEF_VEC_O(cached_reg_t); + +struct stop_reply +{ + struct stop_reply *next; + + ptid_t ptid; + + struct target_waitstatus ws; + + VEC(cached_reg_t) *regcache; + + int stopped_by_watchpoint_p; + CORE_ADDR watch_data_address; + + int solibs_changed; + int replay_event; +}; + +/* The list of already fetched and acknowledged stop events. */ +static struct stop_reply *stop_reply_queue; + +static struct stop_reply * +stop_reply_xmalloc (void) +{ + struct stop_reply *r = XMALLOC (struct stop_reply); + r->next = NULL; + return r; +} + +static void +stop_reply_xfree (struct stop_reply *r) +{ + if (r != NULL) + { + VEC_free (cached_reg_t, r->regcache); + xfree (r); + } } -/* Wait until the remote machine stops, then return, - storing status in STATUS just as `wait' would. */ +/* Discard all pending stop replies of inferior PID. If PID is -1, + discard everything. */ -static ptid_t -remote_wait_as (ptid_t ptid, struct target_waitstatus *status) +static void +discard_pending_stop_replies (int pid) { - 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; - int replay_event = 0; - char *buf, *p; + struct stop_reply *prev = NULL, *reply, *next; - status->kind = TARGET_WAITKIND_IGNORE; - status->value.integer = 0; + /* Discard the in-flight notification. */ + if (pending_stop_reply != NULL + && (pid == -1 + || ptid_get_pid (pending_stop_reply->ptid) == pid)) + { + stop_reply_xfree (pending_stop_reply); + pending_stop_reply = NULL; + } - if (rs->cached_wait_status) - /* Use the cached wait status, but only once. */ - rs->cached_wait_status = 0; - else + /* Discard the stop replies we have already pulled with + vStopped. */ + for (reply = stop_reply_queue; reply; reply = next) { - if (!target_is_async_p ()) + next = reply->next; + if (pid == -1 + || ptid_get_pid (reply->ptid) == pid) { - 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); - } + if (reply == stop_reply_queue) + stop_reply_queue = reply->next; + else + prev->next = reply->next; + + stop_reply_xfree (reply); } - /* 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. */ - getpkt (&rs->buf, &rs->buf_size, wait_forever_enabled_p); - if (!target_is_async_p ()) - signal (SIGINT, ofunc); + else + prev = reply; } +} - buf = rs->buf; +/* Cleanup wrapper. */ - remote_stopped_by_watchpoint_p = 0; +static void +do_stop_reply_xfree (void *arg) +{ + struct stop_reply *r = arg; + stop_reply_xfree (r); +} - /* We got something. */ - rs->waiting_for_stop_reply = 0; +/* Look for a queued stop reply belonging to PTID. If one is found, + remove it from the queue, and return it. Returns NULL if none is + found. If there are still queued events left to process, tell the + event loop to get back to target_wait soon. */ - switch (buf[0]) +static struct stop_reply * +queued_stop_reply (ptid_t ptid) +{ + struct stop_reply *it, *prev; + struct stop_reply head; + + head.next = stop_reply_queue; + prev = &head; + + it = head.next; + + if (!ptid_equal (ptid, minus_one_ptid)) + for (; it; prev = it, it = it->next) + if (ptid_equal (ptid, it->ptid)) + break; + + if (it) { - 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); + prev->next = it->next; + it->next = NULL; + } - /* This stop reply is special. We reply back to the stub, - and keep waiting for the target to stop. */ - rs->waiting_for_stop_reply = 1; - break; - case 'T': /* Status with PC, SP, FP, ... */ + stop_reply_queue = head.next; + + if (stop_reply_queue) + /* There's still at least an event left. */ + mark_async_event_handler (remote_async_inferior_event_token); + + return it; +} + +/* Push a fully parsed stop reply in the stop reply queue. Since we + know that we now have at least one queued event left to pass to the + core side, tell the event loop to get back to target_wait soon. */ + +static void +push_stop_reply (struct stop_reply *new_event) +{ + struct stop_reply *event; + + if (stop_reply_queue) + { + for (event = stop_reply_queue; + event && event->next; + event = event->next) + ; + + event->next = new_event; + } + else + stop_reply_queue = new_event; + + mark_async_event_handler (remote_async_inferior_event_token); +} + +/* Returns true if we have a stop reply for PTID. */ + +static int +peek_stop_reply (ptid_t ptid) +{ + struct stop_reply *it; + + for (it = stop_reply_queue; it; it = it->next) + if (ptid_equal (ptid, it->ptid)) + { + if (it->ws.kind == TARGET_WAITKIND_STOPPED) + return 1; + } + + return 0; +} + +/* Parse the stop reply in BUF. Either the function succeeds, and the + result is stored in EVENT, or throws an error. */ + +static void +remote_parse_stop_reply (char *buf, struct stop_reply *event) +{ + struct remote_arch_state *rsa = get_remote_arch_state (); + ULONGEST addr; + char *p; + + event->ptid = null_ptid; + event->ws.kind = TARGET_WAITKIND_IGNORE; + event->ws.value.integer = 0; + event->solibs_changed = 0; + event->replay_event = 0; + event->stopped_by_watchpoint_p = 0; + event->regcache = NULL; + + switch (buf[0]) + { + case 'T': /* Status with PC, SP, FP, ... */ { gdb_byte regs[MAX_REGISTER_SIZE]; /* Expedited reply, containing Signal, {regno, reg} repeat. */ /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where - ss = signal number - n... = register number - r... = register contents + ss = signal number + n... = register number + r... = register contents */ - p = &buf[3]; /* after Txx */ + p = &buf[3]; /* after Txx */ while (*p) { char *p1; @@ -3756,8 +4216,8 @@ remote_wait_as (ptid_t ptid, struct target_waitstatus *status) pnum and set p1 to point to the character following it. Otherwise p1 points to p. */ - /* If this packet is an awatch packet, don't parse the - 'a' as a register number. */ + /* If this packet is an awatch packet, don't parse the 'a' + as a register number. */ if (strncmp (p, "awatch", strlen("awatch")) != 0) { @@ -3776,14 +4236,14 @@ remote_wait_as (ptid_t ptid, struct target_waitstatus *status) Packet: '%s'\n"), p, buf); if (strncmp (p, "thread", p1 - p) == 0) - event_ptid = read_ptid (++p1, &p); + event->ptid = read_ptid (++p1, &p); else if ((strncmp (p, "watch", p1 - p) == 0) || (strncmp (p, "rwatch", p1 - p) == 0) || (strncmp (p, "awatch", p1 - p) == 0)) { - remote_stopped_by_watchpoint_p = 1; + event->stopped_by_watchpoint_p = 1; p = unpack_varlen_hex (++p1, &addr); - remote_watch_data_address = (CORE_ADDR)addr; + event->watch_data_address = (CORE_ADDR) addr; } else if (strncmp (p, "library", p1 - p) == 0) { @@ -3792,7 +4252,7 @@ Packet: '%s'\n"), while (*p_temp && *p_temp != ';') p_temp++; - solibs_changed = 1; + event->solibs_changed = 1; p = p_temp; } else if (strncmp (p, "replaylog", p1 - p) == 0) @@ -3800,7 +4260,7 @@ Packet: '%s'\n"), /* NO_HISTORY event. p1 will indicate "begin" or "end", but it makes no difference for now, so ignore it. */ - replay_event = 1; + event->replay_event = 1; p_temp = strchr (p1 + 1, ';'); if (p_temp) p = p_temp; @@ -3816,6 +4276,10 @@ Packet: '%s'\n"), else { struct packet_reg *reg = packet_reg_from_pnum (rsa, pnum); + cached_reg_t cached_reg; + + cached_reg.num = reg->regnum; + p = p1; if (*p != ':') @@ -3829,15 +4293,15 @@ Packet: '%s'\n"), Packet: '%s'\n"), phex_nz (pnum, 0), p, buf); - fieldsize = hex2bin (p, regs, + fieldsize = hex2bin (p, cached_reg.data, register_size (target_gdbarch, reg->regnum)); p += 2 * fieldsize; if (fieldsize < register_size (target_gdbarch, reg->regnum)) warning (_("Remote reply is too short: %s"), buf); - regcache_raw_supply (get_current_regcache (), - reg->regnum, regs); + + VEC_safe_push (cached_reg_t, event->regcache, &cached_reg); } if (*p != ';') @@ -3848,14 +4312,14 @@ Packet: '%s'\n"), } /* fall through */ case 'S': /* Old style status, just signal only. */ - if (solibs_changed) - status->kind = TARGET_WAITKIND_LOADED; - else if (replay_event) - status->kind = TARGET_WAITKIND_NO_HISTORY; + if (event->solibs_changed) + event->ws.kind = TARGET_WAITKIND_LOADED; + else if (event->replay_event) + event->ws.kind = TARGET_WAITKIND_NO_HISTORY; else { - status->kind = TARGET_WAITKIND_STOPPED; - status->value.sig = (enum target_signal) + event->ws.kind = TARGET_WAITKIND_STOPPED; + event->ws.value.sig = (enum target_signal) (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); } break; @@ -3874,14 +4338,14 @@ Packet: '%s'\n"), if (buf[0] == 'W') { /* The remote process exited. */ - status->kind = TARGET_WAITKIND_EXITED; - status->value.integer = value; + event->ws.kind = TARGET_WAITKIND_EXITED; + event->ws.value.integer = value; } else { /* The remote process exited with a signal. */ - status->kind = TARGET_WAITKIND_SIGNALLED; - status->value.sig = (enum target_signal) value; + event->ws.kind = TARGET_WAITKIND_SIGNALLED; + event->ws.value.sig = (enum target_signal) value; } /* If no process is specified, assume inferior_ptid. */ @@ -3907,7 +4371,288 @@ Packet: '%s'\n"), } else error (_("unknown stop reply packet: %s"), buf); - event_ptid = pid_to_ptid (pid); + 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 + + 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); break; } case 'O': /* Console output. */ @@ -3945,41 +4690,54 @@ Packet: '%s'\n"), break; } - /* Nothing interesting happened. */ if (status->kind == TARGET_WAITKIND_IGNORE) + /* Nothing interesting happened. */ return minus_one_ptid; - - if (status->kind == TARGET_WAITKIND_EXITED - || status->kind == TARGET_WAITKIND_SIGNALLED) - { - int pid = ptid_get_pid (event_ptid); - delete_inferior (pid); - } - else + else if (status->kind != TARGET_WAITKIND_EXITED + && status->kind != TARGET_WAITKIND_SIGNALLED) { if (!ptid_equal (event_ptid, null_ptid)) record_currthread (event_ptid); else event_ptid = inferior_ptid; } + else + /* A process exit. Invalidate our notion of current thread. */ + record_currthread (minus_one_ptid); return event_ptid; } +/* Wait until the remote machine stops, then return, storing status in + STATUS just as `wait' would. */ + static ptid_t remote_wait (ptid_t ptid, struct target_waitstatus *status) { ptid_t event_ptid; - /* In synchronous mode, keep waiting until the target stops. In - asynchronous mode, always return to the event loop. */ + if (non_stop) + event_ptid = remote_wait_ns (ptid, status); + else + { + /* In synchronous mode, keep waiting until the target stops. In + asynchronous mode, always return to the event loop. */ + + do + { + event_ptid = remote_wait_as (ptid, status); + } + while (status->kind == TARGET_WAITKIND_IGNORE + && !target_can_async_p ()); + } - do + if (target_can_async_p ()) { - event_ptid = remote_wait_as (ptid, status); + /* If there are are events left in the queue tell the event loop + to return here. */ + if (stop_reply_queue) + mark_async_event_handler (remote_async_inferior_event_token); } - while (status->kind == TARGET_WAITKIND_IGNORE - && !target_can_async_p ()); return event_ptid; } @@ -4860,6 +5618,45 @@ remote_read_bytes (CORE_ADDR memaddr, gdb_byte *myaddr, int len) } return origlen; } + + +/* Remote notification handler. */ + +static void +handle_notification (char *buf, size_t length) +{ + if (strncmp (buf, "Stop:", 5) == 0) + { + if (pending_stop_reply) + /* We've already parsed the in-flight stop-reply, but the stub + for some reason thought we didn't, possibly due to timeout + on its side. Just ignore it. */ + ; + else + { + struct cleanup *old_chain; + struct stop_reply *reply = stop_reply_xmalloc (); + old_chain = make_cleanup (do_stop_reply_xfree, reply); + + remote_parse_stop_reply (buf + 5, reply); + + discard_cleanups (old_chain); + + /* Be careful to only set it after parsing, since an error + may be thrown then. */ + pending_stop_reply = reply; + + /* Notify the event loop there's a stop reply to acknowledge + and that there may be more events to fetch. */ + mark_async_event_handler (remote_async_get_pending_events_token); + } + } + else + /* We ignore notifications we don't recognize, for compatibility + with newer stubs. */ + ; +} + /* Read or write LEN bytes from inferior memory at MEMADDR, transferring to or from debugger address BUFFER. Write to inferior @@ -5080,8 +5877,9 @@ putpkt_binary (char *buf, int cnt) ready to handle this request, so we'd hang and timeout. We don't have to worry about this in synchronous mode, because in that case it's not possible to issue a command while the target is - running. */ - if (target_can_async_p () && rs->waiting_for_stop_reply) + running. This is not a problem in non-stop mode, because in that + case, the stub is always ready to process serial input. */ + if (!non_stop && target_can_async_p () && rs->waiting_for_stop_reply) error (_("Cannot execute this command while the target is running.")); /* We're sending out a new packet. Make sure we don't look at a @@ -5125,7 +5923,8 @@ putpkt_binary (char *buf, int cnt) if (rs->noack_mode) break; - /* Read until either a timeout occurs (-2) or '+' is read. */ + /* Read until either a timeout occurs (-2) or '+' is read. + Handle any notification that arrives in the mean time. */ while (1) { ch = readchar (remote_timeout); @@ -5138,6 +5937,7 @@ putpkt_binary (char *buf, int cnt) case '-': case SERIAL_TIMEOUT: case '$': + case '%': if (started_error_output) { putchar_unfiltered ('\n'); @@ -5173,6 +5973,44 @@ putpkt_binary (char *buf, int cnt) serial_write (remote_desc, "+", 1); continue; /* Now, go look for +. */ } + + case '%': + { + int val; + + /* If we got a notification, handle it, and go back to looking + for an ack. */ + /* We've found the start of a notification. Now + collect the data. */ + val = read_frame (&rs->buf, &rs->buf_size); + if (val >= 0) + { + if (remote_debug) + { + fprintf_unfiltered (gdb_stdlog, " Notification received: "); + fputstrn_unfiltered (rs->buf, val, 0, gdb_stdlog); + fprintf_unfiltered (gdb_stdlog, "\n"); + } + handle_notification (rs->buf, val); + /* We're in sync now, rewait for the ack. */ + tcount = 0; + } + else + { + if (remote_debug) + { + if (!started_error_output) + { + started_error_output = 1; + fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); + } + fputc_unfiltered (ch & 0177, gdb_stdlog); + fprintf_unfiltered (gdb_stdlog, "%s", rs->buf); + } + } + continue; + } + /* fall-through */ default: if (remote_debug) { @@ -5397,10 +6235,13 @@ getpkt (char **buf, rather than timing out; this is used (in synchronous mode) to wait for a target that is is executing user code to stop. If FOREVER == 0, this function is allowed to time out gracefully and return an - indication of this to the caller. Otherwise return the number - of bytes read. */ + indication of this to the caller. Otherwise return the number of + bytes read. If EXPECTING_NOTIF, consider receiving a notification + enough reason to return to the caller. */ + static int -getpkt_sane (char **buf, long *sizeof_buf, int forever) +getpkt_or_notif_sane_1 (char **buf, long *sizeof_buf, int forever, + int expecting_notif) { struct remote_state *rs = get_remote_state (); int c; @@ -5415,33 +6256,42 @@ getpkt_sane (char **buf, long *sizeof_buf, int forever) strcpy (*buf, "timeout"); if (forever) - { - timeout = watchdog > 0 ? watchdog : -1; - } - + timeout = watchdog > 0 ? watchdog : -1; + else if (expecting_notif) + timeout = 0; /* There should already be a char in the buffer. If + not, bail out. */ else timeout = remote_timeout; #define MAX_TRIES 3 - for (tries = 1; tries <= MAX_TRIES; tries++) + /* Process any number of notifications, and then return when + we get a packet. */ + for (;;) { - /* This can loop forever if the remote side sends us characters - continuously, but if it pauses, we'll get a zero from - readchar because of timeout. Then we'll count that as a - retry. */ - - /* Note that we will only wait forever prior to the start of a - packet. After that, we expect characters to arrive at a - brisk pace. They should show up within remote_timeout - intervals. */ - - do + /* If we get a timeout or bad checksm, retry up to MAX_TRIES + times. */ + for (tries = 1; tries <= MAX_TRIES; tries++) { - c = readchar (timeout); + /* This can loop forever if the remote side sends us + characters continuously, but if it pauses, we'll get + SERIAL_TIMEOUT from readchar because of timeout. Then + we'll count that as a retry. + + Note that even when forever is set, we will only wait + forever prior to the start of a packet. After that, we + expect characters to arrive at a brisk pace. They should + show up within remote_timeout intervals. */ + do + c = readchar (timeout); + while (c != SERIAL_TIMEOUT && c != '$' && c != '%'); if (c == SERIAL_TIMEOUT) { + if (expecting_notif) + return -1; /* Don't complain, it's normal to not get + anything in this case. */ + if (forever) /* Watchdog went off? Kill the target. */ { QUIT; @@ -5450,16 +6300,33 @@ getpkt_sane (char **buf, long *sizeof_buf, int forever) } if (remote_debug) fputs_filtered ("Timed out.\n", gdb_stdlog); - goto retry; } + else + { + /* We've found the start of a packet or notification. + Now collect the data. */ + val = read_frame (buf, sizeof_buf); + if (val >= 0) + break; + } + + serial_write (remote_desc, "-", 1); } - while (c != '$'); - /* We've found the start of a packet, now collect the data. */ + if (tries > MAX_TRIES) + { + /* We have tried hard enough, and just can't receive the + packet/notification. Give up. */ + printf_unfiltered (_("Ignoring packet error, continuing...\n")); - val = read_frame (buf, sizeof_buf); + /* Skip the ack char if we're in no-ack mode. */ + if (!rs->noack_mode) + serial_write (remote_desc, "+", 1); + return -1; + } - if (val >= 0) + /* If we got an ordinary packet, return that to our caller. */ + if (c == '$') { if (remote_debug) { @@ -5474,23 +6341,41 @@ getpkt_sane (char **buf, long *sizeof_buf, int forever) return val; } - /* Try the whole thing again. */ - retry: - /* Skip the nack char if we're in no-ack mode. */ - if (!rs->noack_mode) - serial_write (remote_desc, "-", 1); - } + /* If we got a notification, handle it, and go back to looking + for a packet. */ + else + { + gdb_assert (c == '%'); + + if (remote_debug) + { + fprintf_unfiltered (gdb_stdlog, " Notification received: "); + fputstrn_unfiltered (*buf, val, 0, gdb_stdlog); + fprintf_unfiltered (gdb_stdlog, "\n"); + } - /* We have tried hard enough, and just can't receive the packet. - Give up. */ + handle_notification (*buf, val); - printf_unfiltered (_("Ignoring packet error, continuing...\n")); + /* Notifications require no acknowledgement. */ - /* Skip the ack char if we're in no-ack mode. */ - if (!rs->noack_mode) - serial_write (remote_desc, "+", 1); - return -1; + if (expecting_notif) + return -1; + } + } +} + +static int +getpkt_sane (char **buf, long *sizeof_buf, int forever) +{ + return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 0); +} + +static int +getpkt_or_notif_sane (char **buf, long *sizeof_buf, int forever) +{ + return getpkt_or_notif_sane_1 (buf, sizeof_buf, forever, 1); } + static void remote_kill (void) @@ -5574,7 +6459,7 @@ remote_mourn_1 (struct target_ops *target) static int select_new_thread_callback (struct thread_info *th, void* data) { - if (!ptid_equal (th->ptid, minus_one_ptid)) + if (!is_exited (th->ptid)) { switch_to_thread (th->ptid); printf_filtered (_("[Switching to %s]\n"), @@ -5593,6 +6478,9 @@ extended_remote_mourn_1 (struct target_ops *target) connected. */ rs->waiting_for_stop_reply = 0; + /* We're no longer interested in these events. */ + discard_pending_stop_replies (ptid_get_pid (inferior_ptid)); + /* Unlike "target remote", we do not want to unpush the target; then the next time the user says "run", we won't be connected. */ @@ -5744,6 +6632,9 @@ extended_remote_create_inferior_1 (char *exec_file, char *args, /* Now mark the inferior as running before we do anything else. */ inferior_ptid = magic_null_ptid; + /* Now, if we have thread information, update inferior_ptid. */ + inferior_ptid = remote_current_thread (inferior_ptid); + add_inferior (ptid_get_pid (inferior_ptid)); add_thread_silent (inferior_ptid); @@ -7657,6 +8548,12 @@ remote_can_execute_reverse (void) return remote_target_can_reverse; } +static int +remote_supports_non_stop (void) +{ + return 1; +} + static void init_remote_ops (void) { @@ -7718,6 +8615,7 @@ Specify the serial device it is connected to\n\ remote_ops.to_async_mask = remote_async_mask; remote_ops.to_terminal_inferior = remote_terminal_inferior; remote_ops.to_terminal_ours = remote_terminal_ours; + remote_ops.to_supports_non_stop = remote_supports_non_stop; } /* Set up the extended remote vector by making a copy of the standard @@ -7781,6 +8679,18 @@ remote_async_serial_handler (struct serial *scb, void *context) async_client_callback (INF_REG_EVENT, async_client_context); } +static void +remote_async_inferior_event_handler (gdb_client_data data) +{ + inferior_event_handler (INF_REG_EVENT, NULL); +} + +static void +remote_async_get_pending_events_handler (gdb_client_data data) +{ + remote_get_pending_stop_replies (); +} + static void remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context) -- 2.30.2