1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
3 Copyright (C) 1986-2013 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>
56 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
57 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
59 #include <sys/systemcfg.h>
61 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
62 debugging 32-bit and 64-bit processes. Define a typedef and macros for
63 accessing fields in the appropriate structures. */
65 /* In 32-bit compilation mode (which is the only mode from which ptrace()
66 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
72 /* Return whether the current architecture is 64-bit. */
77 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
80 static void exec_one_dummy_insn (struct regcache
*);
82 static LONGEST rs6000_xfer_shared_libraries
83 (struct target_ops
*ops
, enum target_object object
,
84 const char *annex
, gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
85 ULONGEST offset
, LONGEST len
);
87 /* Given REGNO, a gdb register number, return the corresponding
88 number suitable for use as a ptrace() parameter. Return -1 if
89 there's no suitable mapping. Also, set the int pointed to by
90 ISFLOAT to indicate whether REGNO is a floating point register. */
93 regmap (struct gdbarch
*gdbarch
, int regno
, int *isfloat
)
95 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
98 if (tdep
->ppc_gp0_regnum
<= regno
99 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
)
101 else if (tdep
->ppc_fp0_regnum
>= 0
102 && tdep
->ppc_fp0_regnum
<= regno
103 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)
106 return regno
- tdep
->ppc_fp0_regnum
+ FPR0
;
108 else if (regno
== gdbarch_pc_regnum (gdbarch
))
110 else if (regno
== tdep
->ppc_ps_regnum
)
112 else if (regno
== tdep
->ppc_cr_regnum
)
114 else if (regno
== tdep
->ppc_lr_regnum
)
116 else if (regno
== tdep
->ppc_ctr_regnum
)
118 else if (regno
== tdep
->ppc_xer_regnum
)
120 else if (tdep
->ppc_fpscr_regnum
>= 0
121 && regno
== tdep
->ppc_fpscr_regnum
)
123 else if (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
)
129 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
132 rs6000_ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
134 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 int ret
= ptracex (req
, id
, addr
, data
, buf
);
153 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
154 req
, id
, hex_string (addr
), data
, (unsigned int)buf
, ret
);
159 /* Fetch register REGNO from the inferior. */
162 fetch_register (struct regcache
*regcache
, int regno
)
164 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
165 int addr
[MAX_REGISTER_SIZE
];
168 /* Retrieved values may be -1, so infer errors from errno. */
171 nr
= regmap (gdbarch
, regno
, &isfloat
);
173 /* Floating-point registers. */
175 rs6000_ptrace32 (PT_READ_FPR
, PIDGET (inferior_ptid
), addr
, nr
, 0);
177 /* Bogus register number. */
180 if (regno
>= gdbarch_num_regs (gdbarch
))
181 fprintf_unfiltered (gdb_stderr
,
182 "gdb error: register no %d not implemented.\n",
187 /* Fixed-point registers. */
191 *addr
= rs6000_ptrace32 (PT_READ_GPR
, PIDGET (inferior_ptid
),
195 /* PT_READ_GPR requires the buffer parameter to point to long long,
196 even if the register is really only 32 bits. */
198 rs6000_ptrace64 (PT_READ_GPR
, PIDGET (inferior_ptid
), nr
, 0, &buf
);
199 if (register_size (gdbarch
, regno
) == 8)
200 memcpy (addr
, &buf
, 8);
207 regcache_raw_supply (regcache
, regno
, (char *) addr
);
211 /* FIXME: this happens 3 times at the start of each 64-bit program. */
212 perror (_("ptrace read"));
218 /* Store register REGNO back into the inferior. */
221 store_register (struct regcache
*regcache
, int regno
)
223 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
224 int addr
[MAX_REGISTER_SIZE
];
227 /* Fetch the register's value from the register cache. */
228 regcache_raw_collect (regcache
, regno
, addr
);
230 /* -1 can be a successful return value, so infer errors from errno. */
233 nr
= regmap (gdbarch
, regno
, &isfloat
);
235 /* Floating-point registers. */
237 rs6000_ptrace32 (PT_WRITE_FPR
, PIDGET (inferior_ptid
), addr
, nr
, 0);
239 /* Bogus register number. */
242 if (regno
>= gdbarch_num_regs (gdbarch
))
243 fprintf_unfiltered (gdb_stderr
,
244 "gdb error: register no %d not implemented.\n",
248 /* Fixed-point registers. */
251 if (regno
== gdbarch_sp_regnum (gdbarch
))
252 /* Execute one dummy instruction (which is a breakpoint) in inferior
253 process to give kernel a chance to do internal housekeeping.
254 Otherwise the following ptrace(2) calls will mess up user stack
255 since kernel will get confused about the bottom of the stack
257 exec_one_dummy_insn (regcache
);
259 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
260 the register's value is passed by value, but for 64-bit inferiors,
261 the address of a buffer containing the value is passed. */
263 rs6000_ptrace32 (PT_WRITE_GPR
, PIDGET (inferior_ptid
),
264 (int *) nr
, *addr
, 0);
267 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
268 area, even if the register is really only 32 bits. */
270 if (register_size (gdbarch
, regno
) == 8)
271 memcpy (&buf
, addr
, 8);
274 rs6000_ptrace64 (PT_WRITE_GPR
, PIDGET (inferior_ptid
), nr
, 0, &buf
);
280 perror (_("ptrace write"));
285 /* Read from the inferior all registers if REGNO == -1 and just register
289 rs6000_fetch_inferior_registers (struct target_ops
*ops
,
290 struct regcache
*regcache
, int regno
)
292 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
294 fetch_register (regcache
, regno
);
298 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
300 /* Read 32 general purpose registers. */
301 for (regno
= tdep
->ppc_gp0_regnum
;
302 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
305 fetch_register (regcache
, regno
);
308 /* Read general purpose floating point registers. */
309 if (tdep
->ppc_fp0_regnum
>= 0)
310 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
311 fetch_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
313 /* Read special registers. */
314 fetch_register (regcache
, gdbarch_pc_regnum (gdbarch
));
315 fetch_register (regcache
, tdep
->ppc_ps_regnum
);
316 fetch_register (regcache
, tdep
->ppc_cr_regnum
);
317 fetch_register (regcache
, tdep
->ppc_lr_regnum
);
318 fetch_register (regcache
, tdep
->ppc_ctr_regnum
);
319 fetch_register (regcache
, tdep
->ppc_xer_regnum
);
320 if (tdep
->ppc_fpscr_regnum
>= 0)
321 fetch_register (regcache
, tdep
->ppc_fpscr_regnum
);
322 if (tdep
->ppc_mq_regnum
>= 0)
323 fetch_register (regcache
, tdep
->ppc_mq_regnum
);
327 /* Store our register values back into the inferior.
328 If REGNO is -1, do this for all registers.
329 Otherwise, REGNO specifies which register (so we can save time). */
332 rs6000_store_inferior_registers (struct target_ops
*ops
,
333 struct regcache
*regcache
, int regno
)
335 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
337 store_register (regcache
, regno
);
341 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
343 /* Write general purpose registers first. */
344 for (regno
= tdep
->ppc_gp0_regnum
;
345 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
348 store_register (regcache
, regno
);
351 /* Write floating point registers. */
352 if (tdep
->ppc_fp0_regnum
>= 0)
353 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
354 store_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
356 /* Write special registers. */
357 store_register (regcache
, gdbarch_pc_regnum (gdbarch
));
358 store_register (regcache
, tdep
->ppc_ps_regnum
);
359 store_register (regcache
, tdep
->ppc_cr_regnum
);
360 store_register (regcache
, tdep
->ppc_lr_regnum
);
361 store_register (regcache
, tdep
->ppc_ctr_regnum
);
362 store_register (regcache
, tdep
->ppc_xer_regnum
);
363 if (tdep
->ppc_fpscr_regnum
>= 0)
364 store_register (regcache
, tdep
->ppc_fpscr_regnum
);
365 if (tdep
->ppc_mq_regnum
>= 0)
366 store_register (regcache
, tdep
->ppc_mq_regnum
);
371 /* Attempt a transfer all LEN bytes starting at OFFSET between the
372 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
373 Return the number of bytes actually transferred. */
376 rs6000_xfer_partial (struct target_ops
*ops
, enum target_object object
,
377 const char *annex
, gdb_byte
*readbuf
,
378 const gdb_byte
*writebuf
,
379 ULONGEST offset
, LONGEST len
)
381 pid_t pid
= ptid_get_pid (inferior_ptid
);
382 int arch64
= ARCH64 ();
386 case TARGET_OBJECT_LIBRARIES_AIX
:
387 return rs6000_xfer_shared_libraries (ops
, object
, annex
,
390 case TARGET_OBJECT_MEMORY
:
394 PTRACE_TYPE_RET word
;
395 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
397 ULONGEST rounded_offset
;
400 /* Round the start offset down to the next long word
402 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
404 /* Since ptrace will transfer a single word starting at that
405 rounded_offset the partial_len needs to be adjusted down to
406 that (remember this function only does a single transfer).
407 Should the required length be even less, adjust it down
409 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
410 if (partial_len
> len
)
415 /* If OFFSET:PARTIAL_LEN is smaller than
416 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
417 be needed. Read in the entire word. */
418 if (rounded_offset
< offset
419 || (offset
+ partial_len
420 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
422 /* Need part of initial word -- fetch it. */
424 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
425 rounded_offset
, 0, NULL
);
427 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
433 /* Copy data to be written over corresponding part of
435 memcpy (buffer
.byte
+ (offset
- rounded_offset
),
436 writebuf
, partial_len
);
440 rs6000_ptrace64 (PT_WRITE_D
, pid
,
441 rounded_offset
, buffer
.word
, NULL
);
443 rs6000_ptrace32 (PT_WRITE_D
, pid
,
444 (int *) (uintptr_t) rounded_offset
,
454 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
455 rounded_offset
, 0, NULL
);
457 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
458 (int *)(uintptr_t)rounded_offset
,
463 /* Copy appropriate bytes out of the buffer. */
464 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
476 /* Wait for the child specified by PTID to do something. Return the
477 process ID of the child, or MINUS_ONE_PTID in case of error; store
478 the status in *OURSTATUS. */
481 rs6000_wait (struct target_ops
*ops
,
482 ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
485 int status
, save_errno
;
493 pid
= waitpid (ptid_get_pid (ptid
), &status
, 0);
496 while (pid
== -1 && errno
== EINTR
);
498 clear_sigint_trap ();
502 fprintf_unfiltered (gdb_stderr
,
503 _("Child process unexpectedly missing: %s.\n"),
504 safe_strerror (save_errno
));
506 /* Claim it exited with unknown signal. */
507 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
508 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
509 return inferior_ptid
;
512 /* Ignore terminated detached child processes. */
513 if (!WIFSTOPPED (status
) && pid
!= ptid_get_pid (inferior_ptid
))
518 /* AIX has a couple of strange returns from wait(). */
520 /* stop after load" status. */
522 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
523 /* signal 0. I have no idea why wait(2) returns with this status word. */
524 else if (status
== 0x7f)
525 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
526 /* A normal waitstatus. Let the usual macros deal with it. */
528 store_waitstatus (ourstatus
, status
);
530 return pid_to_ptid (pid
);
533 /* Execute one dummy breakpoint instruction. This way we give the kernel
534 a chance to do some housekeeping and update inferior's internal data,
538 exec_one_dummy_insn (struct regcache
*regcache
)
540 #define DUMMY_INSN_ADDR AIX_TEXT_SEGMENT_BASE+0x200
542 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
543 int ret
, status
, pid
;
547 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
548 assume that this address will never be executed again by the real
551 bp
= deprecated_insert_raw_breakpoint (gdbarch
, NULL
, DUMMY_INSN_ADDR
);
553 /* You might think this could be done with a single ptrace call, and
554 you'd be correct for just about every platform I've ever worked
555 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
556 the inferior never hits the breakpoint (it's also worth noting
557 powerpc-ibm-aix4.1.3 works correctly). */
558 prev_pc
= regcache_read_pc (regcache
);
559 regcache_write_pc (regcache
, DUMMY_INSN_ADDR
);
561 ret
= rs6000_ptrace64 (PT_CONTINUE
, PIDGET (inferior_ptid
), 1, 0, NULL
);
563 ret
= rs6000_ptrace32 (PT_CONTINUE
, PIDGET (inferior_ptid
),
567 perror (_("pt_continue"));
571 pid
= waitpid (PIDGET (inferior_ptid
), &status
, 0);
573 while (pid
!= PIDGET (inferior_ptid
));
575 regcache_write_pc (regcache
, prev_pc
);
576 deprecated_remove_raw_breakpoint (gdbarch
, bp
);
580 /* Set the current architecture from the host running GDB. Called when
581 starting a child process. */
583 static void (*super_create_inferior
) (struct target_ops
*,char *exec_file
,
584 char *allargs
, char **env
, int from_tty
);
586 rs6000_create_inferior (struct target_ops
* ops
, char *exec_file
,
587 char *allargs
, char **env
, int from_tty
)
589 enum bfd_architecture arch
;
592 struct gdbarch_info info
;
594 super_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
598 arch
= bfd_arch_rs6000
;
599 mach
= bfd_mach_rs6k
;
603 arch
= bfd_arch_powerpc
;
607 /* FIXME: schauer/2002-02-25:
608 We don't know if we are executing a 32 or 64 bit executable,
609 and have no way to pass the proper word size to rs6000_gdbarch_init.
610 So we have to avoid switching to a new architecture, if the architecture
612 Blindly calling rs6000_gdbarch_init used to work in older versions of
613 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
614 determine the wordsize. */
617 const struct bfd_arch_info
*exec_bfd_arch_info
;
619 exec_bfd_arch_info
= bfd_get_arch_info (exec_bfd
);
620 if (arch
== exec_bfd_arch_info
->arch
)
624 bfd_default_set_arch_mach (&abfd
, arch
, mach
);
626 gdbarch_info_init (&info
);
627 info
.bfd_arch_info
= bfd_get_arch_info (&abfd
);
628 info
.abfd
= exec_bfd
;
630 if (!gdbarch_update_p (info
))
631 internal_error (__FILE__
, __LINE__
,
632 _("rs6000_create_inferior: failed "
633 "to select architecture"));
637 /* Shared Object support. */
639 /* Return the LdInfo data for the given process. Raises an error
640 if the data could not be obtained.
642 The returned value must be deallocated after use. */
645 rs6000_ptrace_ldinfo (ptid_t ptid
)
647 const int pid
= ptid_get_pid (ptid
);
649 gdb_byte
*ldi
= xmalloc (ldi_size
);
655 rc
= rs6000_ptrace64 (PT_LDINFO
, pid
, (unsigned long) ldi
, ldi_size
,
658 rc
= rs6000_ptrace32 (PT_LDINFO
, pid
, (int *) ldi
, ldi_size
, NULL
);
661 break; /* Success, we got the entire ld_info data. */
664 perror_with_name (_("ptrace ldinfo"));
666 /* ldi is not big enough. Double it and try again. */
668 ldi
= xrealloc (ldi
, ldi_size
);
674 /* Implement the to_xfer_partial target_ops method for
675 TARGET_OBJECT_LIBRARIES_AIX objects. */
678 rs6000_xfer_shared_libraries
679 (struct target_ops
*ops
, enum target_object object
,
680 const char *annex
, gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
681 ULONGEST offset
, LONGEST len
)
685 struct cleanup
*cleanup
;
687 /* This function assumes that it is being run with a live process.
688 Core files are handled via gdbarch. */
689 gdb_assert (target_has_execution
);
694 ldi_buf
= rs6000_ptrace_ldinfo (inferior_ptid
);
695 gdb_assert (ldi_buf
!= NULL
);
696 cleanup
= make_cleanup (xfree
, ldi_buf
);
697 result
= rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf
,
698 readbuf
, offset
, len
, 1);
701 do_cleanups (cleanup
);
705 void _initialize_rs6000_nat (void);
708 _initialize_rs6000_nat (void)
710 struct target_ops
*t
;
712 t
= inf_ptrace_target ();
713 t
->to_fetch_registers
= rs6000_fetch_inferior_registers
;
714 t
->to_store_registers
= rs6000_store_inferior_registers
;
715 t
->to_xfer_partial
= rs6000_xfer_partial
;
717 super_create_inferior
= t
->to_create_inferior
;
718 t
->to_create_inferior
= rs6000_create_inferior
;
720 t
->to_wait
= rs6000_wait
;