2002-02-10 Daniel Jacobowitz <drow@mvista.com>
[deliverable/binutils-gdb.git] / gdb / infptrace.c
index 3be361495f84a122cb2c9250106bdeed09bd50d7..777a5b491ed7c0e013eb5ab54f77cf6f24ddd150 100644 (file)
@@ -1,6 +1,6 @@
 /* Low level Unix child interface to ptrace, for GDB when running under Unix.
-   Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
-   1999, 2000, 2001
+   Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
+   1998, 1999, 2000, 2001, 2002
    Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -349,11 +349,6 @@ _initialize_kernel_u_addr (void)
     - KERNEL_U_ADDR
 #endif
 
-/* Registers we shouldn't try to fetch.  */
-#if !defined (CANNOT_FETCH_REGISTER)
-#define CANNOT_FETCH_REGISTER(regno) 0
-#endif
-
 /* Fetch one register.  */
 
 static void
@@ -364,7 +359,7 @@ fetch_register (int regno)
   char mess[128];              /* For messages */
   register int i;
   unsigned int offset;         /* Offset of registers within the u area.  */
-  char buf[MAX_REGISTER_RAW_SIZE];
+  char *buf = alloca (MAX_REGISTER_RAW_SIZE);
   int tid;
 
   if (CANNOT_FETCH_REGISTER (regno))
@@ -418,11 +413,6 @@ fetch_inferior_registers (int regno)
     }
 }
 
-/* Registers we shouldn't try to store.  */
-#if !defined (CANNOT_STORE_REGISTER)
-#define CANNOT_STORE_REGISTER(regno) 0
-#endif
-
 /* Store one register. */
 
 static void
@@ -434,6 +424,7 @@ store_register (int regno)
   register int i;
   unsigned int offset;         /* Offset of registers within the u area.  */
   int tid;
+  char *buf = alloca (MAX_REGISTER_RAW_SIZE);
 
   if (CANNOT_STORE_REGISTER (regno))
     {
@@ -447,11 +438,16 @@ store_register (int regno)
   offset = U_REGS_OFFSET;
 
   regaddr = register_addr (regno, offset);
+
+  /* Put the contents of regno into a local buffer */
+  regcache_collect (regno, buf);
+
+  /* Store the local buffer into the inferior a chunk at the time. */
   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
     {
       errno = 0;
       ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
-             *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
+             *(PTRACE_XFER_TYPE *) (buf + i));
       regaddr += sizeof (PTRACE_XFER_TYPE);
       if (errno != 0)
        {
@@ -484,42 +480,55 @@ store_inferior_registers (int regno)
 #endif /* !defined (FETCH_INFERIOR_REGISTERS).  */
 \f
 
+/* Set an upper limit on alloca.  */
+#ifndef GDB_MAX_ALLOCA
+#define GDB_MAX_ALLOCA 0x1000
+#endif
+
 #if !defined (CHILD_XFER_MEMORY)
 /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
-   in the NEW_SUN_PTRACE case.
-   It ought to be straightforward.  But it appears that writing did
-   not write the data that I specified.  I cannot understand where
-   it got the data that it actually did write.  */
+   in the NEW_SUN_PTRACE case.  It ought to be straightforward.  But
+   it appears that writing did not write the data that I specified.  I
+   cannot understand where it got the data that it actually did write.  */
 
-/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
-   to debugger memory starting at MYADDR.   Copy to inferior if
-   WRITE is nonzero.  TARGET is ignored.
+/* Copy LEN bytes to or from inferior's memory starting at MEMADDR to
+   debugger memory starting at MYADDR.  Copy to inferior if WRITE is
+   nonzero.  TARGET is ignored.
 
-   Returns the length copied, which is either the LEN argument or zero.
-   This xfer function does not do partial moves, since child_ops
-   doesn't allow memory operations to cross below us in the target stack
-   anyway.  */
+   Returns the length copied, which is either the LEN argument or
+   zero.  This xfer function does not do partial moves, since
+   child_ops doesn't allow memory operations to cross below us in the
+   target stack anyway.  */
 
 int
 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
-                  struct mem_attrib *attrib ATTRIBUTE_UNUSED,
-                  struct target_ops *target)
+                  struct mem_attrib *attrib, struct target_ops *target)
 {
-  register int i;
+  int i;
   /* Round starting address down to longword boundary.  */
-  register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE);
+  CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
   /* Round ending address up; get number of longwords that makes.  */
-  register int count
-  = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
-  / sizeof (PTRACE_XFER_TYPE);
+  int count = ((((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
+              / sizeof (PTRACE_XFER_TYPE));
+  int alloc = count * sizeof (PTRACE_XFER_TYPE);
+  PTRACE_XFER_TYPE *buffer;
+  struct cleanup *old_chain = NULL;
+
   /* Allocate buffer of that many longwords.  */
-  register PTRACE_XFER_TYPE *buffer
-  = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
+  if (len < GDB_MAX_ALLOCA)
+    {
+      buffer = (PTRACE_XFER_TYPE *) alloca (alloc);
+    }
+  else
+    {
+      buffer = (PTRACE_XFER_TYPE *) xmalloc (alloc);
+      old_chain = make_cleanup (xfree, buffer);
+    }
 
   if (write)
     {
-      /* Fill start and end extra bytes of buffer with existing memory data.  */
-
+      /* Fill start and end extra bytes of buffer with existing memory
+         data.  */
       if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE))
        {
          /* Need part of initial word -- fetch it.  */
@@ -527,23 +536,19 @@ child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
                              (PTRACE_ARG3_TYPE) addr, 0);
        }
 
-      if (count > 1)           /* FIXME, avoid if even boundary */
+      if (count > 1)           /* FIXME, avoid if even boundary */
        {
-         buffer[count - 1] 
-           = ptrace (PT_READ_I, PIDGET (inferior_ptid),
-                     ((PTRACE_ARG3_TYPE)
-                      (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
-                     0);
+         buffer[count - 1] =
+           ptrace (PT_READ_I, PIDGET (inferior_ptid),
+                   ((PTRACE_ARG3_TYPE)
+                    (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))), 0);
        }
 
-      /* Copy data to be written over corresponding part of buffer */
-
+      /* Copy data to be written over corresponding part of buffer.  */
       memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
-             myaddr,
-             len);
+             myaddr, len);
 
       /* Write the entire buffer.  */
-
       for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
        {
          errno = 0;
@@ -566,7 +571,7 @@ child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
     }
   else
     {
-      /* Read all the longwords */
+      /* Read all the longwords */
       for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
        {
          errno = 0;
@@ -582,6 +587,9 @@ child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
              (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
              len);
     }
+
+  if (old_chain != NULL)
+    do_cleanups (old_chain);
   return len;
 }
 \f
This page took 0.025979 seconds and 4 git commands to generate.