/* Low level interface to ptrace, for the remote server for GDB.
Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
- 2006, 2007 Free Software Foundation, Inc.
+ 2006, 2007, 2008 Free Software Foundation, Inc.
This file is part of GDB.
#include <unistd.h>
#include <errno.h>
#include <sys/syscall.h>
+#include <sched.h>
#ifndef PTRACE_GETSIGINFO
# define PTRACE_GETSIGINFO 0x4202
perror_with_name ("waiting for new child");
else if (ret != new_pid)
warning ("wait returned unexpected PID %d", ret);
- else if (!WIFSTOPPED (status) || WSTOPSIG (status) != SIGSTOP)
+ else if (!WIFSTOPPED (status))
warning ("wait returned unexpected status 0x%x", status);
}
add_thread (new_pid, new_process, new_pid);
new_thread_notify (thread_id_to_gdb_id (new_process->lwpid));
- if (stopping_threads)
- new_process->stopped = 1;
+ /* Normally we will get the pending SIGSTOP. But in some cases
+ we might get another signal delivered to the group first.
+ If we do, be sure not to lose it. */
+ if (WSTOPSIG (status) == SIGSTOP)
+ {
+ if (stopping_threads)
+ new_process->stopped = 1;
+ else
+ ptrace (PTRACE_CONT, new_pid, 0, 0);
+ }
else
- ptrace (PTRACE_CONT, new_pid, 0, 0);
+ {
+ new_process->stop_expected = 1;
+ if (stopping_threads)
+ {
+ new_process->stopped = 1;
+ new_process->status_pending_p = 1;
+ new_process->status_pending = status;
+ }
+ else
+ /* Pass the signal on. This is what GDB does - except
+ shouldn't we really report it instead? */
+ ptrace (PTRACE_CONT, new_pid, 0, WSTOPSIG (status));
+ }
/* Always resume the current thread. If we are stopping
threads, it will have a pending SIGSTOP; we may as well
CORE_ADDR stop_pc;
struct process_info *event_child;
int wstat;
+ int bp_status;
/* Check for a process with a pending status. */
/* It is possible that the user changed the pending task's registers since
continue;
}
- if (debug_threads)
- fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n");
+ bp_status = check_breakpoints (stop_pc);
- if (check_breakpoints (stop_pc) != 0)
+ if (bp_status != 0)
{
+ if (debug_threads)
+ fprintf (stderr, "Hit a gdbserver breakpoint.\n");
+
/* We hit one of our own breakpoints. We mark it as a pending
breakpoint, so that check_removed_breakpoint () will do the PC
adjustment for us at the appropriate time. */
event_child->pending_is_breakpoint = 1;
event_child->pending_stop_pc = stop_pc;
- /* Now we need to put the breakpoint back. We continue in the event
+ /* We may need to put the breakpoint back. We continue in the event
loop instead of simply replacing the breakpoint right away,
in order to not lose signals sent to the thread that hit the
breakpoint. Unfortunately this increases the window where another
Otherwise, call the target function to figure out where we need
our temporary breakpoint, create it, and continue executing this
process. */
- if (the_low_target.breakpoint_reinsert_addr == NULL)
+ if (bp_status == 2)
+ /* No need to reinsert. */
+ linux_resume_one_process (&event_child->head, 0, 0, NULL);
+ else if (the_low_target.breakpoint_reinsert_addr == NULL)
{
event_child->bp_reinsert = stop_pc;
uninsert_breakpoint (stop_pc);
continue;
}
+ if (debug_threads)
+ fprintf (stderr, "Hit a non-gdbserver breakpoint.\n");
+
/* If we were single-stepping, we definitely want to report the
SIGTRAP. The single-step operation has completed, so also
clear the stepping flag; in general this does not matter,
}
}
- enable_async_io ();
- unblock_async_io ();
w = linux_wait_for_event (child);
stop_all_processes ();
- disable_async_io ();
if (must_set_ptrace_flags)
{
if (pending_flag)
for_each_inferior (&all_threads, linux_queue_one_thread);
else
- {
- block_async_io ();
- enable_async_io ();
- for_each_inferior (&all_threads, linux_continue_one_thread);
- }
+ for_each_inferior (&all_threads, linux_continue_one_thread);
}
#ifdef HAVE_LINUX_USRREGS
static int linux_supports_tracefork_flag;
-/* A helper function for linux_test_for_tracefork, called after fork (). */
+/* Helper functions for linux_test_for_tracefork, called via clone (). */
-static void
-linux_tracefork_child (void)
+static int
+linux_tracefork_grandchild (void *arg)
+{
+ _exit (0);
+}
+
+#define STACK_SIZE 4096
+
+static int
+linux_tracefork_child (void *arg)
{
ptrace (PTRACE_TRACEME, 0, 0, 0);
kill (getpid (), SIGSTOP);
- fork ();
+#ifdef __ia64__
+ __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE,
+ CLONE_VM | SIGCHLD, NULL);
+#else
+ clone (linux_tracefork_grandchild, arg + STACK_SIZE,
+ CLONE_VM | SIGCHLD, NULL);
+#endif
_exit (0);
}
{
int child_pid, ret, status;
long second_pid;
+ char *stack = malloc (STACK_SIZE * 4);
linux_supports_tracefork_flag = 0;
- child_pid = fork ();
+ /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
+#ifdef __ia64__
+ child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE,
+ CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
+#else
+ child_pid = clone (linux_tracefork_child, stack + STACK_SIZE,
+ CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
+#endif
if (child_pid == -1)
- perror_with_name ("fork");
-
- if (child_pid == 0)
- linux_tracefork_child ();
+ perror_with_name ("clone");
ret = my_waitpid (child_pid, &status, 0);
if (ret == -1)
my_waitpid (child_pid, &status, 0);
}
while (WIFSTOPPED (status));
+
+ free (stack);
}