/* 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 <sys/wait.h>
#include <stdio.h>
#include <sys/param.h>
-#include <sys/dir.h>
#include <sys/ptrace.h>
-#include <sys/user.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/syscall.h>
+#include <sched.h>
+#include <ctype.h>
+#include <pwd.h>
+#include <sys/types.h>
+#include <dirent.h>
#ifndef PTRACE_GETSIGINFO
# define PTRACE_GETSIGINFO 0x4202
static int must_set_ptrace_flags;
+/* This flag is true iff we've just created or attached to a new inferior
+ but it has not stopped yet. As soon as it does, we need to call the
+ low target's arch_setup callback. */
+static int new_inferior;
+
static void linux_resume_one_process (struct inferior_list_entry *entry,
int step, int signal, siginfo_t *info);
static void linux_resume (struct thread_resume *resume_info);
static int linux_wait_for_event (struct thread_info *child);
static int check_removed_breakpoint (struct process_info *event_child);
static void *add_process (unsigned long pid);
+static int my_waitpid (int pid, int *status, int flags);
struct pending_signals
{
#define PTRACE_XFER_TYPE long
#ifdef HAVE_LINUX_REGSETS
-static int use_regsets_p = 1;
+static char *disabled_regsets;
+static int num_regsets;
#endif
#define pid_of(proc) ((proc)->head.id)
if (event == PTRACE_EVENT_CLONE)
{
unsigned long new_pid;
- int ret, status;
+ int ret, status = W_STOPCODE (SIGSTOP);
ptrace (PTRACE_GETEVENTMSG, inferior_pid, 0, &new_pid);
/* The new child has a pending SIGSTOP. We can't affect it until it
hits the SIGSTOP, but we're already attached. */
- do {
- ret = waitpid (new_pid, &status, __WALL);
- } while (ret == -1 && errno == EINTR);
+ ret = my_waitpid (new_pid, &status, __WALL);
if (ret == -1)
perror_with_name ("waiting for new child");
{
struct process_info *process;
- process = (struct process_info *) malloc (sizeof (*process));
+ process = (struct process_info *) xmalloc (sizeof (*process));
memset (process, 0, sizeof (*process));
process->head.id = pid;
new_process = add_process (pid);
add_thread (pid, new_process, pid);
must_set_ptrace_flags = 1;
+ new_inferior = 1;
return pid;
}
if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
{
- fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid,
+ if (all_threads.head != NULL)
+ {
+ /* If we fail to attach to an LWP, just warn. */
+ fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid,
+ strerror (errno), errno);
+ fflush (stderr);
+ return;
+ }
+ else
+ /* If we fail to attach to a process, report an error. */
+ error ("Cannot attach to process %ld: %s (%d)\n", pid,
strerror (errno), errno);
- fflush (stderr);
-
- /* If we fail to attach to an LWP, just return. */
- if (all_threads.head == NULL)
- _exit (0177);
- return;
}
ptrace (PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACECLONE);
process = (struct process_info *) find_inferior_id (&all_processes, pid);
process->stop_expected = 0;
+ new_inferior = 1;
+
return 0;
}
/* Make sure it died. The loop is most likely unnecessary. */
wstat = linux_wait_for_event (thread);
} while (WIFSTOPPED (wstat));
+
+ clear_inferiors ();
+ free (all_processes.head);
+ all_processes.head = all_processes.tail = NULL;
}
static void
delete_all_breakpoints ();
for_each_inferior (&all_threads, linux_detach_one_process);
clear_inferiors ();
+ free (all_processes.head);
+ all_processes.head = all_processes.tail = NULL;
return 0;
}
(*childp)->last_status = *wstatp;
+ /* Architecture-specific setup after inferior is running.
+ This needs to happen after we have attached to the inferior
+ and it is stopped for the first time, but before we access
+ any inferior registers. */
+ if (new_inferior)
+ {
+ the_low_target.arch_setup ();
+#ifdef HAVE_LINUX_REGSETS
+ memset (disabled_regsets, 0, num_regsets);
+#endif
+ new_inferior = 0;
+ }
+
if (debug_threads
&& WIFSTOPPED (*wstatp))
{
+ struct thread_info *saved_inferior = current_inferior;
current_inferior = (struct thread_info *)
find_inferior_id (&all_threads, (*childp)->lwpid);
/* For testing only; i386_stop_pc prints out a diagnostic. */
if (the_low_target.get_pc != NULL)
get_stop_pc ();
+ current_inferior = saved_inferior;
}
}
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)
{
|| process->bp_reinsert != 0))
{
struct pending_signals *p_sig;
- p_sig = malloc (sizeof (*p_sig));
+ p_sig = xmalloc (sizeof (*p_sig));
p_sig->prev = process->pending_signals;
p_sig->signal = signal;
if (info == NULL)
current_inferior = saved_inferior;
if (errno)
- perror_with_name ("ptrace");
+ {
+ /* ESRCH from ptrace either means that the thread was already
+ running (an error) or that it is gone (a race condition). If
+ it's gone, we will get a notification the next time we wait,
+ so we can ignore the error. We could differentiate these
+ two, but it's tricky without waiting; the thread still exists
+ as a zombie, so sending it signal 0 would succeed. So just
+ ignore ESRCH. */
+ if (errno == ESRCH)
+ return;
+
+ perror_with_name ("ptrace");
+ }
}
static struct thread_resume *resume_ptr;
if (process->resume->sig != 0)
{
struct pending_signals *p_sig;
- p_sig = malloc (sizeof (*p_sig));
+ p_sig = xmalloc (sizeof (*p_sig));
p_sig->prev = process->pending_signals;
p_sig->signal = process->resume->sig;
memset (&p_sig->info, 0, sizeof (siginfo_t));
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
goto error_exit;
}
}
- if (the_low_target.left_pad_xfer
- && register_size (regno) < sizeof (PTRACE_XFER_TYPE))
- supply_register (regno, (buf + sizeof (PTRACE_XFER_TYPE)
- - register_size (regno)));
+
+ if (the_low_target.supply_ptrace_register)
+ the_low_target.supply_ptrace_register (regno, buf);
else
supply_register (regno, buf);
& - sizeof (PTRACE_XFER_TYPE);
buf = alloca (size);
memset (buf, 0, size);
- if (the_low_target.left_pad_xfer
- && register_size (regno) < sizeof (PTRACE_XFER_TYPE))
- collect_register (regno, (buf + sizeof (PTRACE_XFER_TYPE)
- - register_size (regno)));
+
+ if (the_low_target.collect_ptrace_register)
+ the_low_target.collect_ptrace_register (regno, buf);
else
collect_register (regno, buf);
+
for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
{
errno = 0;
*(PTRACE_XFER_TYPE *) (buf + i));
if (errno != 0)
{
+ /* At this point, ESRCH should mean the process is already gone,
+ in which case we simply ignore attempts to change its registers.
+ See also the related comment in linux_resume_one_process. */
+ if (errno == ESRCH)
+ return;
+
if ((*the_low_target.cannot_store_register) (regno) == 0)
{
char *err = strerror (errno);
void *buf;
int res;
- if (regset->size == 0)
+ if (regset->size == 0 || disabled_regsets[regset - target_regsets])
{
regset ++;
continue;
}
- buf = malloc (regset->size);
+ buf = xmalloc (regset->size);
+#ifndef __sparc__
res = ptrace (regset->get_request, inferior_pid, 0, buf);
+#else
+ res = ptrace (regset->get_request, inferior_pid, buf, 0);
+#endif
if (res < 0)
{
if (errno == EIO)
{
- /* If we get EIO on the first regset, do not try regsets again.
- If we get EIO on a later regset, disable that regset. */
- if (regset == target_regsets)
- {
- use_regsets_p = 0;
- return -1;
- }
- else
- {
- regset->size = 0;
- continue;
- }
+ /* If we get EIO on a regset, do not try it again for
+ this process. */
+ disabled_regsets[regset - target_regsets] = 1;
+ continue;
}
else
{
void *buf;
int res;
- if (regset->size == 0)
+ if (regset->size == 0 || disabled_regsets[regset - target_regsets])
{
regset ++;
continue;
}
- buf = malloc (regset->size);
+ buf = xmalloc (regset->size);
/* First fill the buffer with the current register set contents,
in case there are any items in the kernel's regset that are
not in gdbserver's regcache. */
+#ifndef __sparc__
res = ptrace (regset->get_request, inferior_pid, 0, buf);
+#else
+ res = ptrace (regset->get_request, inferior_pid, buf, 0);
+#endif
if (res == 0)
{
regset->fill_function (buf);
/* Only now do we write the register set. */
- res = ptrace (regset->set_request, inferior_pid, 0, buf);
+#ifndef __sparc__
+ res = ptrace (regset->set_request, inferior_pid, 0, buf);
+#else
+ res = ptrace (regset->set_request, inferior_pid, buf, 0);
+#endif
}
if (res < 0)
{
if (errno == EIO)
{
- /* If we get EIO on the first regset, do not try regsets again.
- If we get EIO on a later regset, disable that regset. */
- if (regset == target_regsets)
- {
- use_regsets_p = 0;
- return -1;
- }
- else
- {
- regset->size = 0;
- continue;
- }
+ /* If we get EIO on a regset, do not try it again for
+ this process. */
+ disabled_regsets[regset - target_regsets] = 1;
+ continue;
+ }
+ else if (errno == ESRCH)
+ {
+ /* At this point, ESRCH should mean the process is already gone,
+ in which case we simply ignore attempts to change its registers.
+ See also the related comment in linux_resume_one_process. */
+ return 0;
}
else
{
linux_fetch_registers (int regno)
{
#ifdef HAVE_LINUX_REGSETS
- if (use_regsets_p)
- {
- if (regsets_fetch_inferior_registers () == 0)
- return;
- }
+ if (regsets_fetch_inferior_registers () == 0)
+ return;
#endif
#ifdef HAVE_LINUX_USRREGS
usr_fetch_inferior_registers (regno);
linux_store_registers (int regno)
{
#ifdef HAVE_LINUX_REGSETS
- if (use_regsets_p)
- {
- if (regsets_store_inferior_registers () == 0)
- return;
- }
+ if (regsets_store_inferior_registers () == 0)
+ return;
#endif
#ifdef HAVE_LINUX_USRREGS
usr_store_inferior_registers (regno);
= (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
/* Allocate buffer of that many longwords. */
register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
- extern int errno;
if (debug_threads)
{
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 = xmalloc (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);
}
}
#endif
-static const char *
-linux_arch_string (void)
+static int
+linux_qxfer_osdata (const char *annex,
+ unsigned char *readbuf, unsigned const char *writebuf,
+ CORE_ADDR offset, int len)
{
- return the_low_target.arch_string;
+ /* We make the process list snapshot when the object starts to be
+ read. */
+ static const char *buf;
+ static long len_avail = -1;
+ static struct buffer buffer;
+
+ DIR *dirp;
+
+ if (strcmp (annex, "processes") != 0)
+ return 0;
+
+ if (!readbuf || writebuf)
+ return 0;
+
+ if (offset == 0)
+ {
+ if (len_avail != -1 && len_avail != 0)
+ buffer_free (&buffer);
+ len_avail = 0;
+ buf = NULL;
+ buffer_init (&buffer);
+ buffer_grow_str (&buffer, "<osdata type=\"processes\">");
+
+ dirp = opendir ("/proc");
+ if (dirp)
+ {
+ struct dirent *dp;
+ while ((dp = readdir (dirp)) != NULL)
+ {
+ struct stat statbuf;
+ char procentry[sizeof ("/proc/4294967295")];
+
+ if (!isdigit (dp->d_name[0])
+ || strlen (dp->d_name) > sizeof ("4294967295") - 1)
+ continue;
+
+ sprintf (procentry, "/proc/%s", dp->d_name);
+ if (stat (procentry, &statbuf) == 0
+ && S_ISDIR (statbuf.st_mode))
+ {
+ char pathname[128];
+ FILE *f;
+ char cmd[MAXPATHLEN + 1];
+ struct passwd *entry;
+
+ sprintf (pathname, "/proc/%s/cmdline", dp->d_name);
+ entry = getpwuid (statbuf.st_uid);
+
+ if ((f = fopen (pathname, "r")) != NULL)
+ {
+ size_t len = fread (cmd, 1, sizeof (cmd) - 1, f);
+ if (len > 0)
+ {
+ int i;
+ for (i = 0; i < len; i++)
+ if (cmd[i] == '\0')
+ cmd[i] = ' ';
+ cmd[len] = '\0';
+
+ buffer_xml_printf (
+ &buffer,
+ "<item>"
+ "<column name=\"pid\">%s</column>"
+ "<column name=\"user\">%s</column>"
+ "<column name=\"command\">%s</column>"
+ "</item>",
+ dp->d_name,
+ entry ? entry->pw_name : "?",
+ cmd);
+ }
+ fclose (f);
+ }
+ }
+ }
+
+ closedir (dirp);
+ }
+ buffer_grow_str0 (&buffer, "</osdata>\n");
+ buf = buffer_finish (&buffer);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the data. */
+ buffer_free (&buffer);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
}
static struct target_ops linux_target_ops = {
#else
NULL,
#endif
- linux_arch_string,
+ NULL,
+ hostio_last_error_from_errno,
+ linux_qxfer_osdata,
};
static void
set_target_ops (&linux_target_ops);
set_breakpoint_data (the_low_target.breakpoint,
the_low_target.breakpoint_len);
- init_registers ();
linux_init_signals ();
linux_test_for_tracefork ();
+#ifdef HAVE_LINUX_REGSETS
+ for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++)
+ ;
+ disabled_regsets = xmalloc (num_regsets);
+#endif
}