+/* target_is_async_p implementation. */
+
+static int
+linux_nat_is_async_p (void)
+{
+ /* NOTE: palves 2008-03-21: We're only async when the user requests
+ it explicitly with the "maintenance set target-async" command.
+ Someday, linux will always be async. */
+ if (!target_async_permitted)
+ return 0;
+
+ return 1;
+}
+
+/* target_can_async_p implementation. */
+
+static int
+linux_nat_can_async_p (void)
+{
+ /* NOTE: palves 2008-03-21: We're only async when the user requests
+ it explicitly with the "maintenance set target-async" command.
+ Someday, linux will always be async. */
+ if (!target_async_permitted)
+ return 0;
+
+ /* See target.h/target_async_mask. */
+ return linux_nat_async_mask_value;
+}
+
+static int
+linux_nat_supports_non_stop (void)
+{
+ return 1;
+}
+
+/* target_async_mask implementation. */
+
+static int
+linux_nat_async_mask (int mask)
+{
+ int current_state;
+ current_state = linux_nat_async_mask_value;
+
+ if (current_state != mask)
+ {
+ if (mask == 0)
+ {
+ linux_nat_async (NULL, 0);
+ linux_nat_async_mask_value = mask;
+ }
+ else
+ {
+ linux_nat_async_mask_value = mask;
+ linux_nat_async (inferior_event_handler, 0);
+ }
+ }
+
+ return current_state;
+}
+
+/* Pop an event from the event pipe. */
+
+static int
+linux_nat_event_pipe_pop (int* ptr_status, int* ptr_options)
+{
+ struct waitpid_result event = {0};
+ int ret;
+
+ do
+ {
+ ret = read (linux_nat_event_pipe[0], &event, sizeof (event));
+ }
+ while (ret == -1 && errno == EINTR);
+
+ gdb_assert (ret == sizeof (event));
+
+ *ptr_status = event.status;
+ *ptr_options = event.options;
+
+ linux_nat_num_queued_events--;
+
+ return event.pid;
+}
+
+/* Push an event into the event pipe. */
+
+static void
+linux_nat_event_pipe_push (int pid, int status, int options)
+{
+ int ret;
+ struct waitpid_result event = {0};
+ event.pid = pid;
+ event.status = status;
+ event.options = options;
+
+ do
+ {
+ ret = write (linux_nat_event_pipe[1], &event, sizeof (event));
+ gdb_assert ((ret == -1 && errno == EINTR) || ret == sizeof (event));
+ } while (ret == -1 && errno == EINTR);
+
+ linux_nat_num_queued_events++;
+}
+
+static void
+get_pending_events (void)
+{
+ int status, options, pid;
+
+ if (!target_async_permitted
+ || linux_nat_async_events_state != sigchld_async)
+ internal_error (__FILE__, __LINE__,
+ "get_pending_events called with async masked");
+
+ while (1)
+ {
+ status = 0;
+ options = __WCLONE | WNOHANG;
+
+ do
+ {
+ pid = waitpid (-1, &status, options);
+ }
+ while (pid == -1 && errno == EINTR);
+
+ if (pid <= 0)
+ {
+ options = WNOHANG;
+ do
+ {
+ pid = waitpid (-1, &status, options);
+ }
+ while (pid == -1 && errno == EINTR);
+ }
+
+ if (pid <= 0)
+ /* No more children reporting events. */
+ break;
+
+ if (debug_linux_nat_async)
+ fprintf_unfiltered (gdb_stdlog, "\
+get_pending_events: pid(%d), status(%x), options (%x)\n",
+ pid, status, options);
+
+ linux_nat_event_pipe_push (pid, status, options);
+ }
+
+ if (debug_linux_nat_async)
+ fprintf_unfiltered (gdb_stdlog, "\
+get_pending_events: linux_nat_num_queued_events(%d)\n",
+ linux_nat_num_queued_events);
+}
+
+/* SIGCHLD handler for async mode. */
+
+static void
+async_sigchld_handler (int signo)
+{
+ if (debug_linux_nat_async)
+ fprintf_unfiltered (gdb_stdlog, "async_sigchld_handler\n");
+
+ get_pending_events ();
+}
+
+/* Set SIGCHLD handling state to STATE. Returns previous state. */
+
+static enum sigchld_state
+linux_nat_async_events (enum sigchld_state state)
+{
+ enum sigchld_state current_state = linux_nat_async_events_state;
+
+ if (debug_linux_nat_async)
+ fprintf_unfiltered (gdb_stdlog,
+ "LNAE: state(%d): linux_nat_async_events_state(%d), "
+ "linux_nat_num_queued_events(%d)\n",
+ state, linux_nat_async_events_state,
+ linux_nat_num_queued_events);
+
+ if (current_state != state)
+ {
+ sigset_t mask;
+ sigemptyset (&mask);
+ sigaddset (&mask, SIGCHLD);
+
+ /* Always block before changing state. */
+ sigprocmask (SIG_BLOCK, &mask, NULL);
+
+ /* Set new state. */
+ linux_nat_async_events_state = state;
+
+ switch (state)
+ {
+ case sigchld_sync:
+ {
+ /* Block target events. */
+ sigprocmask (SIG_BLOCK, &mask, NULL);
+ sigaction (SIGCHLD, &sync_sigchld_action, NULL);
+ /* Get events out of queue, and make them available to
+ queued_waitpid / my_waitpid. */
+ pipe_to_local_event_queue ();
+ }
+ break;
+ case sigchld_async:
+ {
+ /* Unblock target events for async mode. */
+
+ sigprocmask (SIG_BLOCK, &mask, NULL);
+
+ /* Put events we already waited on, in the pipe first, so
+ events are FIFO. */
+ local_event_queue_to_pipe ();
+ /* While in masked async, we may have not collected all
+ the pending events. Get them out now. */
+ get_pending_events ();
+
+ /* Let'em come. */
+ sigaction (SIGCHLD, &async_sigchld_action, NULL);
+ sigprocmask (SIG_UNBLOCK, &mask, NULL);
+ }
+ break;
+ case sigchld_default:
+ {
+ /* SIGCHLD default mode. */
+ sigaction (SIGCHLD, &sigchld_default_action, NULL);
+
+ /* Get events out of queue, and make them available to
+ queued_waitpid / my_waitpid. */
+ pipe_to_local_event_queue ();
+
+ /* Unblock SIGCHLD. */
+ sigprocmask (SIG_UNBLOCK, &mask, NULL);
+ }
+ break;
+ }
+ }
+
+ return current_state;
+}
+
+static int async_terminal_is_ours = 1;
+
+/* target_terminal_inferior implementation. */
+
+static void
+linux_nat_terminal_inferior (void)
+{
+ if (!target_is_async_p ())
+ {
+ /* Async mode is disabled. */
+ terminal_inferior ();
+ return;
+ }
+
+ /* GDB should never give the terminal to the inferior, if the
+ inferior is running in the background (run&, continue&, etc.).
+ This check can be removed when the common code is fixed. */
+ if (!sync_execution)
+ return;
+
+ terminal_inferior ();
+
+ if (!async_terminal_is_ours)
+ return;
+
+ delete_file_handler (input_fd);
+ async_terminal_is_ours = 0;
+ set_sigint_trap ();
+}
+
+/* target_terminal_ours implementation. */
+
+static void
+linux_nat_terminal_ours (void)
+{
+ if (!target_is_async_p ())
+ {
+ /* Async mode is disabled. */
+ terminal_ours ();
+ return;
+ }
+
+ /* GDB should never give the terminal to the inferior if the
+ inferior is running in the background (run&, continue&, etc.),
+ but claiming it sure should. */
+ terminal_ours ();
+
+ if (!sync_execution)
+ return;
+
+ if (async_terminal_is_ours)
+ return;
+
+ clear_sigint_trap ();
+ add_file_handler (input_fd, stdin_event_handler, 0);
+ async_terminal_is_ours = 1;
+}
+
+static void (*async_client_callback) (enum inferior_event_type event_type,
+ void *context);
+static void *async_client_context;
+
+static void
+linux_nat_async_file_handler (int error, gdb_client_data client_data)
+{
+ async_client_callback (INF_REG_EVENT, async_client_context);
+}
+
+/* target_async implementation. */
+
+static void
+linux_nat_async (void (*callback) (enum inferior_event_type event_type,
+ void *context), void *context)
+{
+ if (linux_nat_async_mask_value == 0 || !target_async_permitted)
+ internal_error (__FILE__, __LINE__,
+ "Calling target_async when async is masked");
+
+ if (callback != NULL)
+ {
+ async_client_callback = callback;
+ async_client_context = context;
+ add_file_handler (linux_nat_event_pipe[0],
+ linux_nat_async_file_handler, NULL);
+
+ linux_nat_async_events (sigchld_async);
+ }
+ else
+ {
+ async_client_callback = callback;
+ async_client_context = context;
+
+ linux_nat_async_events (sigchld_sync);
+ delete_file_handler (linux_nat_event_pipe[0]);
+ }
+ return;
+}
+
+/* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
+ event came out. */
+
+static int
+linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
+{
+ ptid_t ptid = * (ptid_t *) data;
+
+ if (ptid_equal (lwp->ptid, ptid)
+ || ptid_equal (minus_one_ptid, ptid)
+ || (ptid_is_pid (ptid)
+ && ptid_get_pid (ptid) == ptid_get_pid (lwp->ptid)))
+ {
+ if (!lwp->stopped)
+ {
+ int pid, status;
+
+ if (debug_linux_nat)
+ fprintf_unfiltered (gdb_stdlog,
+ "LNSL: running -> suspending %s\n",
+ target_pid_to_str (lwp->ptid));
+
+ /* Peek once, to check if we've already waited for this
+ LWP. */
+ pid = queued_waitpid_1 (ptid_get_lwp (lwp->ptid), &status,
+ lwp->cloned ? __WCLONE : 0, 1 /* peek */);
+
+ if (pid == -1)
+ {
+ ptid_t ptid = lwp->ptid;
+
+ stop_callback (lwp, NULL);
+ stop_wait_callback (lwp, NULL);
+
+ /* If the lwp exits while we try to stop it, there's
+ nothing else to do. */
+ lwp = find_lwp_pid (ptid);
+ if (lwp == NULL)
+ return 0;
+
+ pid = queued_waitpid_1 (ptid_get_lwp (lwp->ptid), &status,
+ lwp->cloned ? __WCLONE : 0,
+ 1 /* peek */);
+ }
+
+ /* If we didn't collect any signal other than SIGSTOP while
+ stopping the LWP, push a SIGNAL_0 event. In either case,
+ the event-loop will end up calling target_wait which will
+ collect these. */
+ if (pid == -1)
+ push_waitpid (ptid_get_lwp (lwp->ptid), W_STOPCODE (0),
+ lwp->cloned ? __WCLONE : 0);
+ }
+ else
+ {
+ /* Already known to be stopped; do nothing. */
+
+ if (debug_linux_nat)
+ {
+ if (find_thread_pid (lwp->ptid)->stop_requested)
+ fprintf_unfiltered (gdb_stdlog, "\
+LNSL: already stopped/stop_requested %s\n",
+ target_pid_to_str (lwp->ptid));
+ else
+ fprintf_unfiltered (gdb_stdlog, "\
+LNSL: already stopped/no stop_requested yet %s\n",
+ target_pid_to_str (lwp->ptid));
+ }
+ }
+ }
+ return 0;
+}
+
+static void
+linux_nat_stop (ptid_t ptid)
+{
+ if (non_stop)
+ {
+ linux_nat_async_events (sigchld_sync);
+ iterate_over_lwps (linux_nat_stop_lwp, &ptid);
+ target_async (inferior_event_handler, 0);
+ }
+ else
+ linux_ops->to_stop (ptid);
+}
+