1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "libbfd.h" /* For bfd_default_set_arch_mach (FIXME) */
28 #include "exceptions.h"
29 #include "gdb-stabs.h"
31 #include "arch-utils.h"
32 #include "inf-child.h"
33 #include "inf-ptrace.h"
35 #include "rs6000-tdep.h"
36 #include "rs6000-aix-tdep.h"
39 #include "xcoffread.h"
41 #include <sys/ptrace.h>
47 #include <sys/ioctl.h>
55 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
56 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
58 #include <sys/systemcfg.h>
60 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
61 debugging 32-bit and 64-bit processes. Define a typedef and macros for
62 accessing fields in the appropriate structures. */
64 /* In 32-bit compilation mode (which is the only mode from which ptrace()
65 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
67 #if defined (__ld_info32) || defined (__ld_info64)
71 /* Return whether the current architecture is 64-bit. */
76 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
79 static void exec_one_dummy_insn (struct regcache *);
81 static target_xfer_partial_ftype rs6000_xfer_shared_libraries;
83 /* Given REGNO, a gdb register number, return the corresponding
84 number suitable for use as a ptrace() parameter. Return -1 if
85 there's no suitable mapping. Also, set the int pointed to by
86 ISFLOAT to indicate whether REGNO is a floating point register. */
89 regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
91 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
94 if (tdep->ppc_gp0_regnum <= regno
95 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
97 else if (tdep->ppc_fp0_regnum >= 0
98 && tdep->ppc_fp0_regnum <= regno
99 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
102 return regno - tdep->ppc_fp0_regnum + FPR0;
104 else if (regno == gdbarch_pc_regnum (gdbarch))
106 else if (regno == tdep->ppc_ps_regnum)
108 else if (regno == tdep->ppc_cr_regnum)
110 else if (regno == tdep->ppc_lr_regnum)
112 else if (regno == tdep->ppc_ctr_regnum)
114 else if (regno == tdep->ppc_xer_regnum)
116 else if (tdep->ppc_fpscr_regnum >= 0
117 && regno == tdep->ppc_fpscr_regnum)
119 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
125 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
128 rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
131 int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
133 int ret = ptrace (req, id, (int *)addr, data, buf);
136 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
137 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
142 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
145 rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
148 # ifdef HAVE_PTRACE64
149 int ret = ptrace64 (req, id, addr, data, buf);
151 int ret = ptracex (req, id, addr, data, buf);
157 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
158 req, id, hex_string (addr), data, (unsigned int)buf, ret);
163 /* Fetch register REGNO from the inferior. */
166 fetch_register (struct regcache *regcache, int regno)
168 struct gdbarch *gdbarch = get_regcache_arch (regcache);
169 int addr[MAX_REGISTER_SIZE];
172 /* Retrieved values may be -1, so infer errors from errno. */
175 nr = regmap (gdbarch, regno, &isfloat);
177 /* Floating-point registers. */
179 rs6000_ptrace32 (PT_READ_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
181 /* Bogus register number. */
184 if (regno >= gdbarch_num_regs (gdbarch))
185 fprintf_unfiltered (gdb_stderr,
186 "gdb error: register no %d not implemented.\n",
191 /* Fixed-point registers. */
195 *addr = rs6000_ptrace32 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
199 /* PT_READ_GPR requires the buffer parameter to point to long long,
200 even if the register is really only 32 bits. */
202 rs6000_ptrace64 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
204 if (register_size (gdbarch, regno) == 8)
205 memcpy (addr, &buf, 8);
212 regcache_raw_supply (regcache, regno, (char *) addr);
216 /* FIXME: this happens 3 times at the start of each 64-bit program. */
217 perror (_("ptrace read"));
223 /* Store register REGNO back into the inferior. */
226 store_register (struct regcache *regcache, int regno)
228 struct gdbarch *gdbarch = get_regcache_arch (regcache);
229 int addr[MAX_REGISTER_SIZE];
232 /* Fetch the register's value from the register cache. */
233 regcache_raw_collect (regcache, regno, addr);
235 /* -1 can be a successful return value, so infer errors from errno. */
238 nr = regmap (gdbarch, regno, &isfloat);
240 /* Floating-point registers. */
242 rs6000_ptrace32 (PT_WRITE_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
244 /* Bogus register number. */
247 if (regno >= gdbarch_num_regs (gdbarch))
248 fprintf_unfiltered (gdb_stderr,
249 "gdb error: register no %d not implemented.\n",
253 /* Fixed-point registers. */
256 if (regno == gdbarch_sp_regnum (gdbarch))
257 /* Execute one dummy instruction (which is a breakpoint) in inferior
258 process to give kernel a chance to do internal housekeeping.
259 Otherwise the following ptrace(2) calls will mess up user stack
260 since kernel will get confused about the bottom of the stack
262 exec_one_dummy_insn (regcache);
264 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
265 the register's value is passed by value, but for 64-bit inferiors,
266 the address of a buffer containing the value is passed. */
268 rs6000_ptrace32 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
269 (int *) nr, *addr, 0);
272 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
273 area, even if the register is really only 32 bits. */
275 if (register_size (gdbarch, regno) == 8)
276 memcpy (&buf, addr, 8);
279 rs6000_ptrace64 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
286 perror (_("ptrace write"));
291 /* Read from the inferior all registers if REGNO == -1 and just register
295 rs6000_fetch_inferior_registers (struct target_ops *ops,
296 struct regcache *regcache, int regno)
298 struct gdbarch *gdbarch = get_regcache_arch (regcache);
300 fetch_register (regcache, regno);
304 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
306 /* Read 32 general purpose registers. */
307 for (regno = tdep->ppc_gp0_regnum;
308 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
311 fetch_register (regcache, regno);
314 /* Read general purpose floating point registers. */
315 if (tdep->ppc_fp0_regnum >= 0)
316 for (regno = 0; regno < ppc_num_fprs; regno++)
317 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
319 /* Read special registers. */
320 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
321 fetch_register (regcache, tdep->ppc_ps_regnum);
322 fetch_register (regcache, tdep->ppc_cr_regnum);
323 fetch_register (regcache, tdep->ppc_lr_regnum);
324 fetch_register (regcache, tdep->ppc_ctr_regnum);
325 fetch_register (regcache, tdep->ppc_xer_regnum);
326 if (tdep->ppc_fpscr_regnum >= 0)
327 fetch_register (regcache, tdep->ppc_fpscr_regnum);
328 if (tdep->ppc_mq_regnum >= 0)
329 fetch_register (regcache, tdep->ppc_mq_regnum);
333 /* Store our register values back into the inferior.
334 If REGNO is -1, do this for all registers.
335 Otherwise, REGNO specifies which register (so we can save time). */
338 rs6000_store_inferior_registers (struct target_ops *ops,
339 struct regcache *regcache, int regno)
341 struct gdbarch *gdbarch = get_regcache_arch (regcache);
343 store_register (regcache, regno);
347 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
349 /* Write general purpose registers first. */
350 for (regno = tdep->ppc_gp0_regnum;
351 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
354 store_register (regcache, regno);
357 /* Write floating point registers. */
358 if (tdep->ppc_fp0_regnum >= 0)
359 for (regno = 0; regno < ppc_num_fprs; regno++)
360 store_register (regcache, tdep->ppc_fp0_regnum + regno);
362 /* Write special registers. */
363 store_register (regcache, gdbarch_pc_regnum (gdbarch));
364 store_register (regcache, tdep->ppc_ps_regnum);
365 store_register (regcache, tdep->ppc_cr_regnum);
366 store_register (regcache, tdep->ppc_lr_regnum);
367 store_register (regcache, tdep->ppc_ctr_regnum);
368 store_register (regcache, tdep->ppc_xer_regnum);
369 if (tdep->ppc_fpscr_regnum >= 0)
370 store_register (regcache, tdep->ppc_fpscr_regnum);
371 if (tdep->ppc_mq_regnum >= 0)
372 store_register (regcache, tdep->ppc_mq_regnum);
376 /* Implement the to_xfer_partial target_ops method. */
378 static enum target_xfer_status
379 rs6000_xfer_partial (struct target_ops *ops, enum target_object object,
380 const char *annex, gdb_byte *readbuf,
381 const gdb_byte *writebuf,
382 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
384 pid_t pid = ptid_get_pid (inferior_ptid);
385 int arch64 = ARCH64 ();
389 case TARGET_OBJECT_LIBRARIES_AIX:
390 return rs6000_xfer_shared_libraries (ops, object, annex,
392 offset, len, xfered_len);
393 case TARGET_OBJECT_MEMORY:
397 PTRACE_TYPE_RET word;
398 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
400 ULONGEST rounded_offset;
403 /* Round the start offset down to the next long word
405 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
407 /* Since ptrace will transfer a single word starting at that
408 rounded_offset the partial_len needs to be adjusted down to
409 that (remember this function only does a single transfer).
410 Should the required length be even less, adjust it down
412 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
413 if (partial_len > len)
418 /* If OFFSET:PARTIAL_LEN is smaller than
419 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
420 be needed. Read in the entire word. */
421 if (rounded_offset < offset
422 || (offset + partial_len
423 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
425 /* Need part of initial word -- fetch it. */
427 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
428 rounded_offset, 0, NULL);
430 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
436 /* Copy data to be written over corresponding part of
438 memcpy (buffer.byte + (offset - rounded_offset),
439 writebuf, partial_len);
443 rs6000_ptrace64 (PT_WRITE_D, pid,
444 rounded_offset, buffer.word, NULL);
446 rs6000_ptrace32 (PT_WRITE_D, pid,
447 (int *) (uintptr_t) rounded_offset,
450 return TARGET_XFER_EOF;
457 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
458 rounded_offset, 0, NULL);
460 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
461 (int *)(uintptr_t)rounded_offset,
464 return TARGET_XFER_EOF;
466 /* Copy appropriate bytes out of the buffer. */
467 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
471 *xfered_len = (ULONGEST) partial_len;
472 return TARGET_XFER_OK;
476 return TARGET_XFER_E_IO;
480 /* Wait for the child specified by PTID to do something. Return the
481 process ID of the child, or MINUS_ONE_PTID in case of error; store
482 the status in *OURSTATUS. */
485 rs6000_wait (struct target_ops *ops,
486 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
489 int status, save_errno;
497 pid = waitpid (ptid_get_pid (ptid), &status, 0);
500 while (pid == -1 && errno == EINTR);
502 clear_sigint_trap ();
506 fprintf_unfiltered (gdb_stderr,
507 _("Child process unexpectedly missing: %s.\n"),
508 safe_strerror (save_errno));
510 /* Claim it exited with unknown signal. */
511 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
512 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
513 return inferior_ptid;
516 /* Ignore terminated detached child processes. */
517 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
522 /* AIX has a couple of strange returns from wait(). */
524 /* stop after load" status. */
526 ourstatus->kind = TARGET_WAITKIND_LOADED;
527 /* signal 0. I have no idea why wait(2) returns with this status word. */
528 else if (status == 0x7f)
529 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
530 /* A normal waitstatus. Let the usual macros deal with it. */
532 store_waitstatus (ourstatus, status);
534 return pid_to_ptid (pid);
537 /* Execute one dummy breakpoint instruction. This way we give the kernel
538 a chance to do some housekeeping and update inferior's internal data,
542 exec_one_dummy_insn (struct regcache *regcache)
544 #define DUMMY_INSN_ADDR AIX_TEXT_SEGMENT_BASE+0x200
546 struct gdbarch *gdbarch = get_regcache_arch (regcache);
547 int ret, status, pid;
551 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
552 assume that this address will never be executed again by the real
555 bp = deprecated_insert_raw_breakpoint (gdbarch, NULL, DUMMY_INSN_ADDR);
557 /* You might think this could be done with a single ptrace call, and
558 you'd be correct for just about every platform I've ever worked
559 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
560 the inferior never hits the breakpoint (it's also worth noting
561 powerpc-ibm-aix4.1.3 works correctly). */
562 prev_pc = regcache_read_pc (regcache);
563 regcache_write_pc (regcache, DUMMY_INSN_ADDR);
565 ret = rs6000_ptrace64 (PT_CONTINUE, ptid_get_pid (inferior_ptid),
568 ret = rs6000_ptrace32 (PT_CONTINUE, ptid_get_pid (inferior_ptid),
572 perror (_("pt_continue"));
576 pid = waitpid (ptid_get_pid (inferior_ptid), &status, 0);
578 while (pid != ptid_get_pid (inferior_ptid));
580 regcache_write_pc (regcache, prev_pc);
581 deprecated_remove_raw_breakpoint (gdbarch, bp);
585 /* Set the current architecture from the host running GDB. Called when
586 starting a child process. */
588 static void (*super_create_inferior) (struct target_ops *,char *exec_file,
589 char *allargs, char **env, int from_tty);
591 rs6000_create_inferior (struct target_ops * ops, char *exec_file,
592 char *allargs, char **env, int from_tty)
594 enum bfd_architecture arch;
597 struct gdbarch_info info;
599 super_create_inferior (ops, exec_file, allargs, env, from_tty);
603 arch = bfd_arch_rs6000;
604 mach = bfd_mach_rs6k;
608 arch = bfd_arch_powerpc;
612 /* FIXME: schauer/2002-02-25:
613 We don't know if we are executing a 32 or 64 bit executable,
614 and have no way to pass the proper word size to rs6000_gdbarch_init.
615 So we have to avoid switching to a new architecture, if the architecture
617 Blindly calling rs6000_gdbarch_init used to work in older versions of
618 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
619 determine the wordsize. */
622 const struct bfd_arch_info *exec_bfd_arch_info;
624 exec_bfd_arch_info = bfd_get_arch_info (exec_bfd);
625 if (arch == exec_bfd_arch_info->arch)
629 bfd_default_set_arch_mach (&abfd, arch, mach);
631 gdbarch_info_init (&info);
632 info.bfd_arch_info = bfd_get_arch_info (&abfd);
633 info.abfd = exec_bfd;
635 if (!gdbarch_update_p (info))
636 internal_error (__FILE__, __LINE__,
637 _("rs6000_create_inferior: failed "
638 "to select architecture"));
642 /* Shared Object support. */
644 /* Return the LdInfo data for the given process. Raises an error
645 if the data could not be obtained.
647 The returned value must be deallocated after use. */
650 rs6000_ptrace_ldinfo (ptid_t ptid)
652 const int pid = ptid_get_pid (ptid);
654 gdb_byte *ldi = xmalloc (ldi_size);
660 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, ldi_size,
663 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi, ldi_size, NULL);
666 break; /* Success, we got the entire ld_info data. */
669 perror_with_name (_("ptrace ldinfo"));
671 /* ldi is not big enough. Double it and try again. */
673 ldi = xrealloc (ldi, ldi_size);
679 /* Implement the to_xfer_partial target_ops method for
680 TARGET_OBJECT_LIBRARIES_AIX objects. */
682 static enum target_xfer_status
683 rs6000_xfer_shared_libraries
684 (struct target_ops *ops, enum target_object object,
685 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
686 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
690 struct cleanup *cleanup;
692 /* This function assumes that it is being run with a live process.
693 Core files are handled via gdbarch. */
694 gdb_assert (target_has_execution);
697 return TARGET_XFER_E_IO;
699 ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
700 gdb_assert (ldi_buf != NULL);
701 cleanup = make_cleanup (xfree, ldi_buf);
702 result = rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf,
703 readbuf, offset, len, 1);
706 do_cleanups (cleanup);
709 return TARGET_XFER_EOF;
712 *xfered_len = result;
713 return TARGET_XFER_OK;
717 void _initialize_rs6000_nat (void);
720 _initialize_rs6000_nat (void)
722 struct target_ops *t;
724 t = inf_ptrace_target ();
725 t->to_fetch_registers = rs6000_fetch_inferior_registers;
726 t->to_store_registers = rs6000_store_inferior_registers;
727 t->to_xfer_partial = rs6000_xfer_partial;
729 super_create_inferior = t->to_create_inferior;
730 t->to_create_inferior = rs6000_create_inferior;
732 t->to_wait = rs6000_wait;