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 "gdb-stabs.h"
30 #include "arch-utils.h"
31 #include "inf-child.h"
32 #include "inf-ptrace.h"
34 #include "rs6000-tdep.h"
35 #include "rs6000-aix-tdep.h"
38 #include "xcoffread.h"
40 #include <sys/ptrace.h>
46 #include <sys/ioctl.h>
54 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
55 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
57 #include <sys/systemcfg.h>
59 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
60 debugging 32-bit and 64-bit processes. Define a typedef and macros for
61 accessing fields in the appropriate structures. */
63 /* In 32-bit compilation mode (which is the only mode from which ptrace()
64 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
66 #if defined (__ld_info32) || defined (__ld_info64)
70 /* Return whether the current architecture is 64-bit. */
75 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
78 static target_xfer_partial_ftype rs6000_xfer_shared_libraries
;
80 /* Given REGNO, a gdb register number, return the corresponding
81 number suitable for use as a ptrace() parameter. Return -1 if
82 there's no suitable mapping. Also, set the int pointed to by
83 ISFLOAT to indicate whether REGNO is a floating point register. */
86 regmap (struct gdbarch
*gdbarch
, int regno
, int *isfloat
)
88 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
91 if (tdep
->ppc_gp0_regnum
<= regno
92 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
)
94 else if (tdep
->ppc_fp0_regnum
>= 0
95 && tdep
->ppc_fp0_regnum
<= regno
96 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)
99 return regno
- tdep
->ppc_fp0_regnum
+ FPR0
;
101 else if (regno
== gdbarch_pc_regnum (gdbarch
))
103 else if (regno
== tdep
->ppc_ps_regnum
)
105 else if (regno
== tdep
->ppc_cr_regnum
)
107 else if (regno
== tdep
->ppc_lr_regnum
)
109 else if (regno
== tdep
->ppc_ctr_regnum
)
111 else if (regno
== tdep
->ppc_xer_regnum
)
113 else if (tdep
->ppc_fpscr_regnum
>= 0
114 && regno
== tdep
->ppc_fpscr_regnum
)
116 else if (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
)
122 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
125 rs6000_ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
128 int ret
= ptrace64 (req
, id
, (uintptr_t) addr
, data
, buf
);
130 int ret
= ptrace (req
, id
, (int *)addr
, data
, buf
);
133 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
134 req
, id
, (unsigned int)addr
, data
, (unsigned int)buf
, ret
);
139 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
142 rs6000_ptrace64 (int req
, int id
, long long addr
, int data
, void *buf
)
145 # ifdef HAVE_PTRACE64
146 int ret
= ptrace64 (req
, id
, addr
, data
, buf
);
148 int ret
= ptracex (req
, id
, addr
, data
, buf
);
154 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
155 req
, id
, hex_string (addr
), data
, (unsigned int)buf
, ret
);
160 /* Fetch register REGNO from the inferior. */
163 fetch_register (struct regcache
*regcache
, int regno
)
165 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
166 int addr
[MAX_REGISTER_SIZE
];
169 /* Retrieved values may be -1, so infer errors from errno. */
172 nr
= regmap (gdbarch
, regno
, &isfloat
);
174 /* Floating-point registers. */
176 rs6000_ptrace32 (PT_READ_FPR
, ptid_get_pid (inferior_ptid
), addr
, nr
, 0);
178 /* Bogus register number. */
181 if (regno
>= gdbarch_num_regs (gdbarch
))
182 fprintf_unfiltered (gdb_stderr
,
183 "gdb error: register no %d not implemented.\n",
188 /* Fixed-point registers. */
192 *addr
= rs6000_ptrace32 (PT_READ_GPR
, ptid_get_pid (inferior_ptid
),
196 /* PT_READ_GPR requires the buffer parameter to point to long long,
197 even if the register is really only 32 bits. */
199 rs6000_ptrace64 (PT_READ_GPR
, ptid_get_pid (inferior_ptid
),
201 if (register_size (gdbarch
, regno
) == 8)
202 memcpy (addr
, &buf
, 8);
209 regcache_raw_supply (regcache
, regno
, (char *) addr
);
213 /* FIXME: this happens 3 times at the start of each 64-bit program. */
214 perror (_("ptrace read"));
220 /* Store register REGNO back into the inferior. */
223 store_register (struct regcache
*regcache
, int regno
)
225 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
226 int addr
[MAX_REGISTER_SIZE
];
229 /* Fetch the register's value from the register cache. */
230 regcache_raw_collect (regcache
, regno
, addr
);
232 /* -1 can be a successful return value, so infer errors from errno. */
235 nr
= regmap (gdbarch
, regno
, &isfloat
);
237 /* Floating-point registers. */
239 rs6000_ptrace32 (PT_WRITE_FPR
, ptid_get_pid (inferior_ptid
), addr
, nr
, 0);
241 /* Bogus register number. */
244 if (regno
>= gdbarch_num_regs (gdbarch
))
245 fprintf_unfiltered (gdb_stderr
,
246 "gdb error: register no %d not implemented.\n",
250 /* Fixed-point registers. */
253 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
254 the register's value is passed by value, but for 64-bit inferiors,
255 the address of a buffer containing the value is passed. */
257 rs6000_ptrace32 (PT_WRITE_GPR
, ptid_get_pid (inferior_ptid
),
258 (int *) nr
, *addr
, 0);
261 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
262 area, even if the register is really only 32 bits. */
264 if (register_size (gdbarch
, regno
) == 8)
265 memcpy (&buf
, addr
, 8);
268 rs6000_ptrace64 (PT_WRITE_GPR
, ptid_get_pid (inferior_ptid
),
275 perror (_("ptrace write"));
280 /* Read from the inferior all registers if REGNO == -1 and just register
284 rs6000_fetch_inferior_registers (struct target_ops
*ops
,
285 struct regcache
*regcache
, int regno
)
287 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
289 fetch_register (regcache
, regno
);
293 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
295 /* Read 32 general purpose registers. */
296 for (regno
= tdep
->ppc_gp0_regnum
;
297 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
300 fetch_register (regcache
, regno
);
303 /* Read general purpose floating point registers. */
304 if (tdep
->ppc_fp0_regnum
>= 0)
305 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
306 fetch_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
308 /* Read special registers. */
309 fetch_register (regcache
, gdbarch_pc_regnum (gdbarch
));
310 fetch_register (regcache
, tdep
->ppc_ps_regnum
);
311 fetch_register (regcache
, tdep
->ppc_cr_regnum
);
312 fetch_register (regcache
, tdep
->ppc_lr_regnum
);
313 fetch_register (regcache
, tdep
->ppc_ctr_regnum
);
314 fetch_register (regcache
, tdep
->ppc_xer_regnum
);
315 if (tdep
->ppc_fpscr_regnum
>= 0)
316 fetch_register (regcache
, tdep
->ppc_fpscr_regnum
);
317 if (tdep
->ppc_mq_regnum
>= 0)
318 fetch_register (regcache
, tdep
->ppc_mq_regnum
);
322 /* Store our register values back into the inferior.
323 If REGNO is -1, do this for all registers.
324 Otherwise, REGNO specifies which register (so we can save time). */
327 rs6000_store_inferior_registers (struct target_ops
*ops
,
328 struct regcache
*regcache
, int regno
)
330 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
332 store_register (regcache
, regno
);
336 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
338 /* Write general purpose registers first. */
339 for (regno
= tdep
->ppc_gp0_regnum
;
340 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
343 store_register (regcache
, regno
);
346 /* Write floating point registers. */
347 if (tdep
->ppc_fp0_regnum
>= 0)
348 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
349 store_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
351 /* Write special registers. */
352 store_register (regcache
, gdbarch_pc_regnum (gdbarch
));
353 store_register (regcache
, tdep
->ppc_ps_regnum
);
354 store_register (regcache
, tdep
->ppc_cr_regnum
);
355 store_register (regcache
, tdep
->ppc_lr_regnum
);
356 store_register (regcache
, tdep
->ppc_ctr_regnum
);
357 store_register (regcache
, tdep
->ppc_xer_regnum
);
358 if (tdep
->ppc_fpscr_regnum
>= 0)
359 store_register (regcache
, tdep
->ppc_fpscr_regnum
);
360 if (tdep
->ppc_mq_regnum
>= 0)
361 store_register (regcache
, tdep
->ppc_mq_regnum
);
365 /* Implement the to_xfer_partial target_ops method. */
367 static enum target_xfer_status
368 rs6000_xfer_partial (struct target_ops
*ops
, enum target_object object
,
369 const char *annex
, gdb_byte
*readbuf
,
370 const gdb_byte
*writebuf
,
371 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
373 pid_t pid
= ptid_get_pid (inferior_ptid
);
374 int arch64
= ARCH64 ();
378 case TARGET_OBJECT_LIBRARIES_AIX
:
379 return rs6000_xfer_shared_libraries (ops
, object
, annex
,
381 offset
, len
, xfered_len
);
382 case TARGET_OBJECT_MEMORY
:
386 PTRACE_TYPE_RET word
;
387 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
389 ULONGEST rounded_offset
;
392 /* Round the start offset down to the next long word
394 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
396 /* Since ptrace will transfer a single word starting at that
397 rounded_offset the partial_len needs to be adjusted down to
398 that (remember this function only does a single transfer).
399 Should the required length be even less, adjust it down
401 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
402 if (partial_len
> len
)
407 /* If OFFSET:PARTIAL_LEN is smaller than
408 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
409 be needed. Read in the entire word. */
410 if (rounded_offset
< offset
411 || (offset
+ partial_len
412 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
414 /* Need part of initial word -- fetch it. */
416 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
417 rounded_offset
, 0, NULL
);
419 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
425 /* Copy data to be written over corresponding part of
427 memcpy (buffer
.byte
+ (offset
- rounded_offset
),
428 writebuf
, partial_len
);
432 rs6000_ptrace64 (PT_WRITE_D
, pid
,
433 rounded_offset
, buffer
.word
, NULL
);
435 rs6000_ptrace32 (PT_WRITE_D
, pid
,
436 (int *) (uintptr_t) rounded_offset
,
439 return TARGET_XFER_EOF
;
446 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
447 rounded_offset
, 0, NULL
);
449 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
450 (int *)(uintptr_t)rounded_offset
,
453 return TARGET_XFER_EOF
;
455 /* Copy appropriate bytes out of the buffer. */
456 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
460 *xfered_len
= (ULONGEST
) partial_len
;
461 return TARGET_XFER_OK
;
465 return TARGET_XFER_E_IO
;
469 /* Wait for the child specified by PTID to do something. Return the
470 process ID of the child, or MINUS_ONE_PTID in case of error; store
471 the status in *OURSTATUS. */
474 rs6000_wait (struct target_ops
*ops
,
475 ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
478 int status
, save_errno
;
486 pid
= waitpid (ptid_get_pid (ptid
), &status
, 0);
489 while (pid
== -1 && errno
== EINTR
);
491 clear_sigint_trap ();
495 fprintf_unfiltered (gdb_stderr
,
496 _("Child process unexpectedly missing: %s.\n"),
497 safe_strerror (save_errno
));
499 /* Claim it exited with unknown signal. */
500 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
501 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
502 return inferior_ptid
;
505 /* Ignore terminated detached child processes. */
506 if (!WIFSTOPPED (status
) && pid
!= ptid_get_pid (inferior_ptid
))
511 /* AIX has a couple of strange returns from wait(). */
513 /* stop after load" status. */
515 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
516 /* signal 0. I have no idea why wait(2) returns with this status word. */
517 else if (status
== 0x7f)
518 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
519 /* A normal waitstatus. Let the usual macros deal with it. */
521 store_waitstatus (ourstatus
, status
);
523 return pid_to_ptid (pid
);
527 /* Set the current architecture from the host running GDB. Called when
528 starting a child process. */
530 static void (*super_create_inferior
) (struct target_ops
*,char *exec_file
,
531 char *allargs
, char **env
, int from_tty
);
533 rs6000_create_inferior (struct target_ops
* ops
, char *exec_file
,
534 char *allargs
, char **env
, int from_tty
)
536 enum bfd_architecture arch
;
539 struct gdbarch_info info
;
541 super_create_inferior (ops
, exec_file
, allargs
, env
, from_tty
);
545 arch
= bfd_arch_rs6000
;
546 mach
= bfd_mach_rs6k
;
550 arch
= bfd_arch_powerpc
;
554 /* FIXME: schauer/2002-02-25:
555 We don't know if we are executing a 32 or 64 bit executable,
556 and have no way to pass the proper word size to rs6000_gdbarch_init.
557 So we have to avoid switching to a new architecture, if the architecture
559 Blindly calling rs6000_gdbarch_init used to work in older versions of
560 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
561 determine the wordsize. */
564 const struct bfd_arch_info
*exec_bfd_arch_info
;
566 exec_bfd_arch_info
= bfd_get_arch_info (exec_bfd
);
567 if (arch
== exec_bfd_arch_info
->arch
)
571 bfd_default_set_arch_mach (&abfd
, arch
, mach
);
573 gdbarch_info_init (&info
);
574 info
.bfd_arch_info
= bfd_get_arch_info (&abfd
);
575 info
.abfd
= exec_bfd
;
577 if (!gdbarch_update_p (info
))
578 internal_error (__FILE__
, __LINE__
,
579 _("rs6000_create_inferior: failed "
580 "to select architecture"));
584 /* Shared Object support. */
586 /* Return the LdInfo data for the given process. Raises an error
587 if the data could not be obtained.
589 The returned value must be deallocated after use. */
592 rs6000_ptrace_ldinfo (ptid_t ptid
)
594 const int pid
= ptid_get_pid (ptid
);
596 gdb_byte
*ldi
= xmalloc (ldi_size
);
602 rc
= rs6000_ptrace64 (PT_LDINFO
, pid
, (unsigned long) ldi
, ldi_size
,
605 rc
= rs6000_ptrace32 (PT_LDINFO
, pid
, (int *) ldi
, ldi_size
, NULL
);
608 break; /* Success, we got the entire ld_info data. */
611 perror_with_name (_("ptrace ldinfo"));
613 /* ldi is not big enough. Double it and try again. */
615 ldi
= xrealloc (ldi
, ldi_size
);
621 /* Implement the to_xfer_partial target_ops method for
622 TARGET_OBJECT_LIBRARIES_AIX objects. */
624 static enum target_xfer_status
625 rs6000_xfer_shared_libraries
626 (struct target_ops
*ops
, enum target_object object
,
627 const char *annex
, gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
628 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
632 struct cleanup
*cleanup
;
634 /* This function assumes that it is being run with a live process.
635 Core files are handled via gdbarch. */
636 gdb_assert (target_has_execution
);
639 return TARGET_XFER_E_IO
;
641 ldi_buf
= rs6000_ptrace_ldinfo (inferior_ptid
);
642 gdb_assert (ldi_buf
!= NULL
);
643 cleanup
= make_cleanup (xfree
, ldi_buf
);
644 result
= rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf
,
645 readbuf
, offset
, len
, 1);
648 do_cleanups (cleanup
);
651 return TARGET_XFER_EOF
;
654 *xfered_len
= result
;
655 return TARGET_XFER_OK
;
659 void _initialize_rs6000_nat (void);
662 _initialize_rs6000_nat (void)
664 struct target_ops
*t
;
666 t
= inf_ptrace_target ();
667 t
->to_fetch_registers
= rs6000_fetch_inferior_registers
;
668 t
->to_store_registers
= rs6000_store_inferior_registers
;
669 t
->to_xfer_partial
= rs6000_xfer_partial
;
671 super_create_inferior
= t
->to_create_inferior
;
672 t
->to_create_inferior
= rs6000_create_inferior
;
674 t
->to_wait
= rs6000_wait
;