* utils.c (xmalloc,xcalloc,xstrdup): New fns.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-low.c
index fb8002088efb035a4abc8754b8c7fb809d7892a5..120656a55e5c73f33c5c99de90c2408a8c10d40c 100644 (file)
@@ -1,6 +1,6 @@
 /* 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.
 
@@ -23,9 +23,7 @@
 #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
@@ -106,6 +109,11 @@ static int thread_db_active;
 
 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);
@@ -113,6 +121,7 @@ static void stop_all_processes (void);
 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
 {
@@ -125,7 +134,8 @@ 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)
@@ -142,7 +152,7 @@ handle_extended_wait (struct process_info *event_child, int wstat)
   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);
 
@@ -152,9 +162,7 @@ handle_extended_wait (struct process_info *event_child, int wstat)
          /* 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");
@@ -241,7 +249,7 @@ add_process (unsigned long pid)
 {
   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;
@@ -290,6 +298,7 @@ linux_create_inferior (char *program, char **allargs)
   new_process = add_process (pid);
   add_thread (pid, new_process, pid);
   must_set_ptrace_flags = 1;
+  new_inferior = 1;
 
   return pid;
 }
@@ -303,14 +312,18 @@ linux_attach_lwp (unsigned long 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);
@@ -345,6 +358,8 @@ linux_attach (unsigned long pid)
   process = (struct process_info *) find_inferior_id (&all_processes, pid);
   process->stop_expected = 0;
 
+  new_inferior = 1;
+
   return 0;
 }
 
@@ -395,6 +410,10 @@ linux_kill (void)
       /* 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
@@ -433,6 +452,8 @@ linux_detach (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;
 }
 
@@ -605,14 +626,29 @@ retry:
 
   (*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;
     }
 }
 
@@ -622,6 +658,7 @@ linux_wait_for_event (struct thread_info *child)
   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
@@ -785,18 +822,20 @@ linux_wait_for_event (struct thread_info *child)
          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
@@ -814,7 +853,10 @@ linux_wait_for_event (struct thread_info *child)
             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);
@@ -830,6 +872,9 @@ linux_wait_for_event (struct thread_info *child)
          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,
@@ -896,11 +941,8 @@ retry:
        }
     }
 
-  enable_async_io ();
-  unblock_async_io ();
   w = linux_wait_for_event (child);
   stop_all_processes ();
-  disable_async_io ();
 
   if (must_set_ptrace_flags)
     {
@@ -1078,7 +1120,7 @@ linux_resume_one_process (struct inferior_list_entry *entry,
          || 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)
@@ -1156,7 +1198,19 @@ linux_resume_one_process (struct inferior_list_entry *entry,
 
   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;
@@ -1237,7 +1291,7 @@ linux_queue_one_thread (struct inferior_list_entry *entry)
   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));
@@ -1312,11 +1366,7 @@ linux_resume (struct thread_resume *resume_info)
   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
@@ -1370,10 +1420,9 @@ fetch_register (int regno)
          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);
 
@@ -1417,12 +1466,12 @@ usr_store_inferior_registers (int regno)
             & - 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;
@@ -1430,6 +1479,12 @@ usr_store_inferior_registers (int regno)
                  *(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);
@@ -1466,30 +1521,26 @@ regsets_fetch_inferior_registers ()
       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
            {
@@ -1523,18 +1574,22 @@ regsets_store_inferior_registers ()
       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)
        {
@@ -1542,25 +1597,28 @@ regsets_store_inferior_registers ()
          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
            {
@@ -1586,11 +1644,8 @@ void
 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);
@@ -1601,11 +1656,8 @@ void
 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);
@@ -1692,7 +1744,6 @@ linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
   = (((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)
     {
@@ -1732,14 +1783,28 @@ linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
 
 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);
 }
 
@@ -1767,15 +1832,20 @@ linux_test_for_tracefork (void)
 {
   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)
@@ -1840,6 +1910,8 @@ linux_test_for_tracefork (void)
       my_waitpid (child_pid, &status, 0);
     }
   while (WIFSTOPPED (status));
+
+  free (stack);
 }
 
 
@@ -1982,10 +2054,107 @@ linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
 }
 #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 = {
@@ -2018,7 +2187,9 @@ static struct target_ops linux_target_ops = {
 #else
   NULL,
 #endif
-  linux_arch_string,
+  NULL,
+  hostio_last_error_from_errno,
+  linux_qxfer_osdata,
 };
 
 static void
@@ -2036,7 +2207,11 @@ initialize_low (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
 }
This page took 0.03283 seconds and 4 git commands to generate.