1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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/>. */
27 #include "gdb-stabs.h"
29 #include "arch-utils.h"
30 #include "inf-child.h"
31 #include "inf-ptrace.h"
33 #include "rs6000-tdep.h"
34 #include "rs6000-aix-tdep.h"
36 #include "observable.h"
37 #include "xcoffread.h"
39 #include <sys/ptrace.h>
45 #include <sys/ioctl.h>
53 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
54 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
56 #include <sys/systemcfg.h>
58 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
59 debugging 32-bit and 64-bit processes. Define a typedef and macros for
60 accessing fields in the appropriate structures. */
62 /* In 32-bit compilation mode (which is the only mode from which ptrace()
63 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
65 #if defined (__ld_info32) || defined (__ld_info64)
69 /* Return whether the current architecture is 64-bit. */
74 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
77 class rs6000_nat_target final
: public inf_ptrace_target
80 void fetch_registers (struct regcache
*, int) override
;
81 void store_registers (struct regcache
*, int) override
;
83 enum target_xfer_status
xfer_partial (enum target_object object
,
86 const gdb_byte
*writebuf
,
87 ULONGEST offset
, ULONGEST len
,
88 ULONGEST
*xfered_len
) override
;
90 void create_inferior (const char *, const std::string
&,
91 char **, int) override
;
93 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
96 enum target_xfer_status
97 xfer_shared_libraries (enum target_object object
,
98 const char *annex
, gdb_byte
*readbuf
,
99 const gdb_byte
*writebuf
,
100 ULONGEST offset
, ULONGEST len
,
101 ULONGEST
*xfered_len
);
104 static rs6000_nat_target the_rs6000_nat_target
;
106 /* Given REGNO, a gdb register number, return the corresponding
107 number suitable for use as a ptrace() parameter. Return -1 if
108 there's no suitable mapping. Also, set the int pointed to by
109 ISFLOAT to indicate whether REGNO is a floating point register. */
112 regmap (struct gdbarch
*gdbarch
, int regno
, int *isfloat
)
114 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
117 if (tdep
->ppc_gp0_regnum
<= regno
118 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
)
120 else if (tdep
->ppc_fp0_regnum
>= 0
121 && tdep
->ppc_fp0_regnum
<= regno
122 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)
125 return regno
- tdep
->ppc_fp0_regnum
+ FPR0
;
127 else if (regno
== gdbarch_pc_regnum (gdbarch
))
129 else if (regno
== tdep
->ppc_ps_regnum
)
131 else if (regno
== tdep
->ppc_cr_regnum
)
133 else if (regno
== tdep
->ppc_lr_regnum
)
135 else if (regno
== tdep
->ppc_ctr_regnum
)
137 else if (regno
== tdep
->ppc_xer_regnum
)
139 else if (tdep
->ppc_fpscr_regnum
>= 0
140 && regno
== tdep
->ppc_fpscr_regnum
)
142 else if (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
)
148 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
151 rs6000_ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
154 int ret
= ptrace64 (req
, id
, (uintptr_t) addr
, data
, buf
);
156 int ret
= ptrace (req
, id
, (int *)addr
, data
, buf
);
159 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
160 req
, id
, (unsigned int)addr
, data
, (unsigned int)buf
, ret
);
165 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
168 rs6000_ptrace64 (int req
, int id
, long long addr
, int data
, void *buf
)
171 # ifdef HAVE_PTRACE64
172 int ret
= ptrace64 (req
, id
, addr
, data
, (PTRACE_TYPE_ARG5
) buf
);
174 int ret
= ptracex (req
, id
, addr
, data
, (PTRACE_TYPE_ARG5
) buf
);
180 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
181 req
, id
, hex_string (addr
), data
, (unsigned int)buf
, ret
);
186 /* Fetch register REGNO from the inferior. */
189 fetch_register (struct regcache
*regcache
, int regno
)
191 struct gdbarch
*gdbarch
= regcache
->arch ();
192 int addr
[PPC_MAX_REGISTER_SIZE
];
194 pid_t pid
= regcache
->ptid ().pid ();
196 /* Retrieved values may be -1, so infer errors from errno. */
199 nr
= regmap (gdbarch
, regno
, &isfloat
);
201 /* Floating-point registers. */
203 rs6000_ptrace32 (PT_READ_FPR
, pid
, addr
, nr
, 0);
205 /* Bogus register number. */
208 if (regno
>= gdbarch_num_regs (gdbarch
))
209 fprintf_unfiltered (gdb_stderr
,
210 "gdb error: register no %d not implemented.\n",
215 /* Fixed-point registers. */
219 *addr
= rs6000_ptrace32 (PT_READ_GPR
, pid
, (int *) nr
, 0, 0);
222 /* PT_READ_GPR requires the buffer parameter to point to long long,
223 even if the register is really only 32 bits. */
225 rs6000_ptrace64 (PT_READ_GPR
, pid
, nr
, 0, &buf
);
226 if (register_size (gdbarch
, regno
) == 8)
227 memcpy (addr
, &buf
, 8);
234 regcache
->raw_supply (regno
, (char *) addr
);
238 /* FIXME: this happens 3 times at the start of each 64-bit program. */
239 perror (_("ptrace read"));
245 /* Store register REGNO back into the inferior. */
248 store_register (struct regcache
*regcache
, int regno
)
250 struct gdbarch
*gdbarch
= regcache
->arch ();
251 int addr
[PPC_MAX_REGISTER_SIZE
];
253 pid_t pid
= regcache
->ptid ().pid ();
255 /* Fetch the register's value from the register cache. */
256 regcache
->raw_collect (regno
, addr
);
258 /* -1 can be a successful return value, so infer errors from errno. */
261 nr
= regmap (gdbarch
, regno
, &isfloat
);
263 /* Floating-point registers. */
265 rs6000_ptrace32 (PT_WRITE_FPR
, pid
, addr
, nr
, 0);
267 /* Bogus register number. */
270 if (regno
>= gdbarch_num_regs (gdbarch
))
271 fprintf_unfiltered (gdb_stderr
,
272 "gdb error: register no %d not implemented.\n",
276 /* Fixed-point registers. */
279 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
280 the register's value is passed by value, but for 64-bit inferiors,
281 the address of a buffer containing the value is passed. */
283 rs6000_ptrace32 (PT_WRITE_GPR
, pid
, (int *) nr
, *addr
, 0);
286 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
287 area, even if the register is really only 32 bits. */
289 if (register_size (gdbarch
, regno
) == 8)
290 memcpy (&buf
, addr
, 8);
293 rs6000_ptrace64 (PT_WRITE_GPR
, pid
, nr
, 0, &buf
);
299 perror (_("ptrace write"));
304 /* Read from the inferior all registers if REGNO == -1 and just register
308 rs6000_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
310 struct gdbarch
*gdbarch
= regcache
->arch ();
312 fetch_register (regcache
, regno
);
316 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
318 /* Read 32 general purpose registers. */
319 for (regno
= tdep
->ppc_gp0_regnum
;
320 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
323 fetch_register (regcache
, regno
);
326 /* Read general purpose floating point registers. */
327 if (tdep
->ppc_fp0_regnum
>= 0)
328 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
329 fetch_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
331 /* Read special registers. */
332 fetch_register (regcache
, gdbarch_pc_regnum (gdbarch
));
333 fetch_register (regcache
, tdep
->ppc_ps_regnum
);
334 fetch_register (regcache
, tdep
->ppc_cr_regnum
);
335 fetch_register (regcache
, tdep
->ppc_lr_regnum
);
336 fetch_register (regcache
, tdep
->ppc_ctr_regnum
);
337 fetch_register (regcache
, tdep
->ppc_xer_regnum
);
338 if (tdep
->ppc_fpscr_regnum
>= 0)
339 fetch_register (regcache
, tdep
->ppc_fpscr_regnum
);
340 if (tdep
->ppc_mq_regnum
>= 0)
341 fetch_register (regcache
, tdep
->ppc_mq_regnum
);
345 /* Store our register values back into the inferior.
346 If REGNO is -1, do this for all registers.
347 Otherwise, REGNO specifies which register (so we can save time). */
350 rs6000_nat_target::store_registers (struct regcache
*regcache
, int regno
)
352 struct gdbarch
*gdbarch
= regcache
->arch ();
354 store_register (regcache
, regno
);
358 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
360 /* Write general purpose registers first. */
361 for (regno
= tdep
->ppc_gp0_regnum
;
362 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
365 store_register (regcache
, regno
);
368 /* Write floating point registers. */
369 if (tdep
->ppc_fp0_regnum
>= 0)
370 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
371 store_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
373 /* Write special registers. */
374 store_register (regcache
, gdbarch_pc_regnum (gdbarch
));
375 store_register (regcache
, tdep
->ppc_ps_regnum
);
376 store_register (regcache
, tdep
->ppc_cr_regnum
);
377 store_register (regcache
, tdep
->ppc_lr_regnum
);
378 store_register (regcache
, tdep
->ppc_ctr_regnum
);
379 store_register (regcache
, tdep
->ppc_xer_regnum
);
380 if (tdep
->ppc_fpscr_regnum
>= 0)
381 store_register (regcache
, tdep
->ppc_fpscr_regnum
);
382 if (tdep
->ppc_mq_regnum
>= 0)
383 store_register (regcache
, tdep
->ppc_mq_regnum
);
387 /* Implement the to_xfer_partial target_ops method. */
389 enum target_xfer_status
390 rs6000_nat_target::xfer_partial (enum target_object object
,
391 const char *annex
, gdb_byte
*readbuf
,
392 const gdb_byte
*writebuf
,
393 ULONGEST offset
, ULONGEST len
,
394 ULONGEST
*xfered_len
)
396 pid_t pid
= inferior_ptid
.pid ();
397 int arch64
= ARCH64 ();
401 case TARGET_OBJECT_LIBRARIES_AIX
:
402 return xfer_shared_libraries (object
, annex
,
404 offset
, len
, xfered_len
);
405 case TARGET_OBJECT_MEMORY
:
409 PTRACE_TYPE_RET word
;
410 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
412 ULONGEST rounded_offset
;
415 /* Round the start offset down to the next long word
417 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
419 /* Since ptrace will transfer a single word starting at that
420 rounded_offset the partial_len needs to be adjusted down to
421 that (remember this function only does a single transfer).
422 Should the required length be even less, adjust it down
424 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
425 if (partial_len
> len
)
430 /* If OFFSET:PARTIAL_LEN is smaller than
431 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
432 be needed. Read in the entire word. */
433 if (rounded_offset
< offset
434 || (offset
+ partial_len
435 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
437 /* Need part of initial word -- fetch it. */
439 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
440 rounded_offset
, 0, NULL
);
442 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
448 /* Copy data to be written over corresponding part of
450 memcpy (buffer
.byte
+ (offset
- rounded_offset
),
451 writebuf
, partial_len
);
455 rs6000_ptrace64 (PT_WRITE_D
, pid
,
456 rounded_offset
, buffer
.word
, NULL
);
458 rs6000_ptrace32 (PT_WRITE_D
, pid
,
459 (int *) (uintptr_t) rounded_offset
,
462 return TARGET_XFER_EOF
;
469 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
470 rounded_offset
, 0, NULL
);
472 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
473 (int *)(uintptr_t)rounded_offset
,
476 return TARGET_XFER_EOF
;
478 /* Copy appropriate bytes out of the buffer. */
479 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
483 *xfered_len
= (ULONGEST
) partial_len
;
484 return TARGET_XFER_OK
;
488 return TARGET_XFER_E_IO
;
492 /* Wait for the child specified by PTID to do something. Return the
493 process ID of the child, or MINUS_ONE_PTID in case of error; store
494 the status in *OURSTATUS. */
497 rs6000_nat_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
501 int status
, save_errno
;
509 pid
= waitpid (ptid
.pid (), &status
, 0);
512 while (pid
== -1 && errno
== EINTR
);
514 clear_sigint_trap ();
518 fprintf_unfiltered (gdb_stderr
,
519 _("Child process unexpectedly missing: %s.\n"),
520 safe_strerror (save_errno
));
522 /* Claim it exited with unknown signal. */
523 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
524 ourstatus
->value
.sig
= GDB_SIGNAL_UNKNOWN
;
525 return inferior_ptid
;
528 /* Ignore terminated detached child processes. */
529 if (!WIFSTOPPED (status
) && pid
!= inferior_ptid
.pid ())
534 /* AIX has a couple of strange returns from wait(). */
536 /* stop after load" status. */
538 ourstatus
->kind
= TARGET_WAITKIND_LOADED
;
539 /* signal 0. I have no idea why wait(2) returns with this status word. */
540 else if (status
== 0x7f)
541 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
542 /* A normal waitstatus. Let the usual macros deal with it. */
544 store_waitstatus (ourstatus
, status
);
550 /* Set the current architecture from the host running GDB. Called when
551 starting a child process. */
554 rs6000_nat_target::create_inferior (const char *exec_file
,
555 const std::string
&allargs
,
556 char **env
, int from_tty
)
558 enum bfd_architecture arch
;
561 struct gdbarch_info info
;
563 inf_ptrace_target::create_inferior (exec_file
, allargs
, env
, from_tty
);
567 arch
= bfd_arch_rs6000
;
568 mach
= bfd_mach_rs6k
;
572 arch
= bfd_arch_powerpc
;
576 /* FIXME: schauer/2002-02-25:
577 We don't know if we are executing a 32 or 64 bit executable,
578 and have no way to pass the proper word size to rs6000_gdbarch_init.
579 So we have to avoid switching to a new architecture, if the architecture
581 Blindly calling rs6000_gdbarch_init used to work in older versions of
582 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
583 determine the wordsize. */
586 const struct bfd_arch_info
*exec_bfd_arch_info
;
588 exec_bfd_arch_info
= bfd_get_arch_info (exec_bfd
);
589 if (arch
== exec_bfd_arch_info
->arch
)
593 bfd_default_set_arch_mach (&abfd
, arch
, mach
);
595 gdbarch_info_init (&info
);
596 info
.bfd_arch_info
= bfd_get_arch_info (&abfd
);
597 info
.abfd
= exec_bfd
;
599 if (!gdbarch_update_p (info
))
600 internal_error (__FILE__
, __LINE__
,
601 _("rs6000_create_inferior: failed "
602 "to select architecture"));
606 /* Shared Object support. */
608 /* Return the LdInfo data for the given process. Raises an error
609 if the data could not be obtained. */
611 static gdb::byte_vector
612 rs6000_ptrace_ldinfo (ptid_t ptid
)
614 const int pid
= ptid
.pid ();
615 gdb::byte_vector
ldi (1024);
621 rc
= rs6000_ptrace64 (PT_LDINFO
, pid
, (unsigned long) ldi
.data (),
624 rc
= rs6000_ptrace32 (PT_LDINFO
, pid
, (int *) ldi
.data (),
628 break; /* Success, we got the entire ld_info data. */
631 perror_with_name (_("ptrace ldinfo"));
633 /* ldi is not big enough. Double it and try again. */
634 ldi
.resize (ldi
.size () * 2);
640 /* Implement the to_xfer_partial target_ops method for
641 TARGET_OBJECT_LIBRARIES_AIX objects. */
643 enum target_xfer_status
644 rs6000_nat_target::xfer_shared_libraries
645 (enum target_object object
,
646 const char *annex
, gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
647 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
651 /* This function assumes that it is being run with a live process.
652 Core files are handled via gdbarch. */
653 gdb_assert (target_has_execution
);
656 return TARGET_XFER_E_IO
;
658 gdb::byte_vector ldi_buf
= rs6000_ptrace_ldinfo (inferior_ptid
);
659 result
= rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf
.data (),
660 readbuf
, offset
, len
, 1);
663 return TARGET_XFER_EOF
;
666 *xfered_len
= result
;
667 return TARGET_XFER_OK
;
672 _initialize_rs6000_nat (void)
674 add_inf_child_target (&the_rs6000_nat_target
);