+ switch (object)
+ {
+ case TARGET_OBJECT_MEMORY:
+ {
+ union
+ {
+ PTRACE_TYPE_RET word;
+ gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
+ } buffer;
+ ULONGEST rounded_offset;
+ LONGEST partial_len;
+
+ /* Round the start offset down to the next long word
+ boundary. */
+ rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
+
+ /* Since ptrace will transfer a single word starting at that
+ rounded_offset the partial_len needs to be adjusted down to
+ that (remember this function only does a single transfer).
+ Should the required length be even less, adjust it down
+ again. */
+ partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
+ if (partial_len > len)
+ partial_len = len;
+
+ if (writebuf)
+ {
+ /* If OFFSET:PARTIAL_LEN is smaller than
+ ROUNDED_OFFSET:WORDSIZE then a read/modify write will
+ be needed. Read in the entire word. */
+ if (rounded_offset < offset
+ || (offset + partial_len
+ < rounded_offset + sizeof (PTRACE_TYPE_RET)))
+ {
+ /* Need part of initial word -- fetch it. */
+ if (arch64)
+ buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
+ rounded_offset, 0, NULL);
+ else
+ buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
+ (int *)(uintptr_t)rounded_offset,
+ 0, NULL);
+ }
+
+ /* Copy data to be written over corresponding part of
+ buffer. */
+ memcpy (buffer.byte + (offset - rounded_offset),
+ writebuf, partial_len);
+
+ errno = 0;
+ if (arch64)
+ rs6000_ptrace64 (PT_WRITE_D, pid,
+ rounded_offset, buffer.word, NULL);
+ else
+ rs6000_ptrace32 (PT_WRITE_D, pid,
+ (int *)(uintptr_t)rounded_offset, buffer.word, NULL);
+ if (errno)
+ return 0;
+ }
+
+ if (readbuf)
+ {
+ errno = 0;
+ if (arch64)
+ buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
+ rounded_offset, 0, NULL);
+ else
+ buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
+ (int *)(uintptr_t)rounded_offset,
+ 0, NULL);
+ if (errno)
+ return 0;
+
+ /* Copy appropriate bytes out of the buffer. */
+ memcpy (readbuf, buffer.byte + (offset - rounded_offset),
+ partial_len);
+ }
+
+ return partial_len;
+ }
+
+ default:
+ return -1;
+ }