1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
3 Copyright (C) 1986-2021 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-aix-tdep.h"
35 #include "observable.h"
36 #include "xcoffread.h"
38 #include <sys/ptrace.h>
44 #include <sys/ioctl.h>
52 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
53 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
55 #include <sys/systemcfg.h>
57 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
58 debugging 32-bit and 64-bit processes. Define a typedef and macros for
59 accessing fields in the appropriate structures. */
61 /* In 32-bit compilation mode (which is the only mode from which ptrace()
62 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
64 #if defined (__ld_info32) || defined (__ld_info64)
68 /* Return whether the current architecture is 64-bit. */
73 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
76 class rs6000_nat_target final
: public inf_ptrace_target
79 void fetch_registers (struct regcache
*, int) override
;
80 void store_registers (struct regcache
*, int) override
;
82 enum target_xfer_status
xfer_partial (enum target_object object
,
85 const gdb_byte
*writebuf
,
86 ULONGEST offset
, ULONGEST len
,
87 ULONGEST
*xfered_len
) override
;
89 void create_inferior (const char *, const std::string
&,
90 char **, int) override
;
92 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
95 enum target_xfer_status
96 xfer_shared_libraries (enum target_object object
,
97 const char *annex
, gdb_byte
*readbuf
,
98 const gdb_byte
*writebuf
,
99 ULONGEST offset
, ULONGEST len
,
100 ULONGEST
*xfered_len
);
103 static rs6000_nat_target the_rs6000_nat_target
;
105 /* Given REGNO, a gdb register number, return the corresponding
106 number suitable for use as a ptrace() parameter. Return -1 if
107 there's no suitable mapping. Also, set the int pointed to by
108 ISFLOAT to indicate whether REGNO is a floating point register. */
111 regmap (struct gdbarch
*gdbarch
, int regno
, int *isfloat
)
113 ppc_gdbarch_tdep
*tdep
= (ppc_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
116 if (tdep
->ppc_gp0_regnum
<= regno
117 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
)
119 else if (tdep
->ppc_fp0_regnum
>= 0
120 && tdep
->ppc_fp0_regnum
<= regno
121 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)
124 return regno
- tdep
->ppc_fp0_regnum
+ FPR0
;
126 else if (regno
== gdbarch_pc_regnum (gdbarch
))
128 else if (regno
== tdep
->ppc_ps_regnum
)
130 else if (regno
== tdep
->ppc_cr_regnum
)
132 else if (regno
== tdep
->ppc_lr_regnum
)
134 else if (regno
== tdep
->ppc_ctr_regnum
)
136 else if (regno
== tdep
->ppc_xer_regnum
)
138 else if (tdep
->ppc_fpscr_regnum
>= 0
139 && regno
== tdep
->ppc_fpscr_regnum
)
141 else if (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
)
147 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
150 rs6000_ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
153 int ret
= ptrace64 (req
, id
, (uintptr_t) addr
, data
, buf
);
155 int ret
= ptrace (req
, id
, (int *)addr
, data
, buf
);
158 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
159 req
, id
, (unsigned int)addr
, data
, (unsigned int)buf
, ret
);
164 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
167 rs6000_ptrace64 (int req
, int id
, long long addr
, int data
, void *buf
)
170 # ifdef HAVE_PTRACE64
171 int ret
= ptrace64 (req
, id
, addr
, data
, (PTRACE_TYPE_ARG5
) buf
);
173 int ret
= ptracex (req
, id
, addr
, data
, (PTRACE_TYPE_ARG5
) buf
);
179 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
180 req
, id
, hex_string (addr
), data
, (unsigned int)buf
, ret
);
185 /* Fetch register REGNO from the inferior. */
188 fetch_register (struct regcache
*regcache
, int regno
)
190 struct gdbarch
*gdbarch
= regcache
->arch ();
191 int addr
[PPC_MAX_REGISTER_SIZE
];
193 pid_t pid
= regcache
->ptid ().pid ();
195 /* Retrieved values may be -1, so infer errors from errno. */
198 nr
= regmap (gdbarch
, regno
, &isfloat
);
200 /* Floating-point registers. */
202 rs6000_ptrace32 (PT_READ_FPR
, pid
, addr
, nr
, 0);
204 /* Bogus register number. */
207 if (regno
>= gdbarch_num_regs (gdbarch
))
208 fprintf_unfiltered (gdb_stderr
,
209 "gdb error: register no %d not implemented.\n",
214 /* Fixed-point registers. */
218 *addr
= rs6000_ptrace32 (PT_READ_GPR
, pid
, (int *) nr
, 0, 0);
221 /* PT_READ_GPR requires the buffer parameter to point to long long,
222 even if the register is really only 32 bits. */
224 rs6000_ptrace64 (PT_READ_GPR
, pid
, nr
, 0, &buf
);
225 if (register_size (gdbarch
, regno
) == 8)
226 memcpy (addr
, &buf
, 8);
233 regcache
->raw_supply (regno
, (char *) addr
);
237 /* FIXME: this happens 3 times at the start of each 64-bit program. */
238 perror (_("ptrace read"));
244 /* Store register REGNO back into the inferior. */
247 store_register (struct regcache
*regcache
, int regno
)
249 struct gdbarch
*gdbarch
= regcache
->arch ();
250 int addr
[PPC_MAX_REGISTER_SIZE
];
252 pid_t pid
= regcache
->ptid ().pid ();
254 /* Fetch the register's value from the register cache. */
255 regcache
->raw_collect (regno
, addr
);
257 /* -1 can be a successful return value, so infer errors from errno. */
260 nr
= regmap (gdbarch
, regno
, &isfloat
);
262 /* Floating-point registers. */
264 rs6000_ptrace32 (PT_WRITE_FPR
, pid
, addr
, nr
, 0);
266 /* Bogus register number. */
269 if (regno
>= gdbarch_num_regs (gdbarch
))
270 fprintf_unfiltered (gdb_stderr
,
271 "gdb error: register no %d not implemented.\n",
275 /* Fixed-point registers. */
278 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
279 the register's value is passed by value, but for 64-bit inferiors,
280 the address of a buffer containing the value is passed. */
282 rs6000_ptrace32 (PT_WRITE_GPR
, pid
, (int *) nr
, *addr
, 0);
285 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
286 area, even if the register is really only 32 bits. */
288 if (register_size (gdbarch
, regno
) == 8)
289 memcpy (&buf
, addr
, 8);
292 rs6000_ptrace64 (PT_WRITE_GPR
, pid
, nr
, 0, &buf
);
298 perror (_("ptrace write"));
303 /* Read from the inferior all registers if REGNO == -1 and just register
307 rs6000_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
309 struct gdbarch
*gdbarch
= regcache
->arch ();
311 fetch_register (regcache
, regno
);
315 ppc_gdbarch_tdep
*tdep
= (ppc_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
317 /* Read 32 general purpose registers. */
318 for (regno
= tdep
->ppc_gp0_regnum
;
319 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
322 fetch_register (regcache
, regno
);
325 /* Read general purpose floating point registers. */
326 if (tdep
->ppc_fp0_regnum
>= 0)
327 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
328 fetch_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
330 /* Read special registers. */
331 fetch_register (regcache
, gdbarch_pc_regnum (gdbarch
));
332 fetch_register (regcache
, tdep
->ppc_ps_regnum
);
333 fetch_register (regcache
, tdep
->ppc_cr_regnum
);
334 fetch_register (regcache
, tdep
->ppc_lr_regnum
);
335 fetch_register (regcache
, tdep
->ppc_ctr_regnum
);
336 fetch_register (regcache
, tdep
->ppc_xer_regnum
);
337 if (tdep
->ppc_fpscr_regnum
>= 0)
338 fetch_register (regcache
, tdep
->ppc_fpscr_regnum
);
339 if (tdep
->ppc_mq_regnum
>= 0)
340 fetch_register (regcache
, tdep
->ppc_mq_regnum
);
344 /* Store our register values back into the inferior.
345 If REGNO is -1, do this for all registers.
346 Otherwise, REGNO specifies which register (so we can save time). */
349 rs6000_nat_target::store_registers (struct regcache
*regcache
, int regno
)
351 struct gdbarch
*gdbarch
= regcache
->arch ();
353 store_register (regcache
, regno
);
357 ppc_gdbarch_tdep
*tdep
= (ppc_gdbarch_tdep
*) gdbarch_tdep (gdbarch
);
359 /* Write general purpose registers first. */
360 for (regno
= tdep
->ppc_gp0_regnum
;
361 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
364 store_register (regcache
, regno
);
367 /* Write floating point registers. */
368 if (tdep
->ppc_fp0_regnum
>= 0)
369 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
370 store_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
372 /* Write special registers. */
373 store_register (regcache
, gdbarch_pc_regnum (gdbarch
));
374 store_register (regcache
, tdep
->ppc_ps_regnum
);
375 store_register (regcache
, tdep
->ppc_cr_regnum
);
376 store_register (regcache
, tdep
->ppc_lr_regnum
);
377 store_register (regcache
, tdep
->ppc_ctr_regnum
);
378 store_register (regcache
, tdep
->ppc_xer_regnum
);
379 if (tdep
->ppc_fpscr_regnum
>= 0)
380 store_register (regcache
, tdep
->ppc_fpscr_regnum
);
381 if (tdep
->ppc_mq_regnum
>= 0)
382 store_register (regcache
, tdep
->ppc_mq_regnum
);
386 /* Implement the to_xfer_partial target_ops method. */
388 enum target_xfer_status
389 rs6000_nat_target::xfer_partial (enum target_object object
,
390 const char *annex
, gdb_byte
*readbuf
,
391 const gdb_byte
*writebuf
,
392 ULONGEST offset
, ULONGEST len
,
393 ULONGEST
*xfered_len
)
395 pid_t pid
= inferior_ptid
.pid ();
396 int arch64
= ARCH64 ();
400 case TARGET_OBJECT_LIBRARIES_AIX
:
401 return xfer_shared_libraries (object
, annex
,
403 offset
, len
, xfered_len
);
404 case TARGET_OBJECT_MEMORY
:
408 PTRACE_TYPE_RET word
;
409 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
411 ULONGEST rounded_offset
;
414 /* Round the start offset down to the next long word
416 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
418 /* Since ptrace will transfer a single word starting at that
419 rounded_offset the partial_len needs to be adjusted down to
420 that (remember this function only does a single transfer).
421 Should the required length be even less, adjust it down
423 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
424 if (partial_len
> len
)
429 /* If OFFSET:PARTIAL_LEN is smaller than
430 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
431 be needed. Read in the entire word. */
432 if (rounded_offset
< offset
433 || (offset
+ partial_len
434 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
436 /* Need part of initial word -- fetch it. */
438 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
439 rounded_offset
, 0, NULL
);
441 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
447 /* Copy data to be written over corresponding part of
449 memcpy (buffer
.byte
+ (offset
- rounded_offset
),
450 writebuf
, partial_len
);
454 rs6000_ptrace64 (PT_WRITE_D
, pid
,
455 rounded_offset
, buffer
.word
, NULL
);
457 rs6000_ptrace32 (PT_WRITE_D
, pid
,
458 (int *) (uintptr_t) rounded_offset
,
461 return TARGET_XFER_EOF
;
468 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
469 rounded_offset
, 0, NULL
);
471 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
472 (int *)(uintptr_t)rounded_offset
,
475 return TARGET_XFER_EOF
;
477 /* Copy appropriate bytes out of the buffer. */
478 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
482 *xfered_len
= (ULONGEST
) partial_len
;
483 return TARGET_XFER_OK
;
487 return TARGET_XFER_E_IO
;
491 /* Wait for the child specified by PTID to do something. Return the
492 process ID of the child, or MINUS_ONE_PTID in case of error; store
493 the status in *OURSTATUS. */
496 rs6000_nat_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
497 target_wait_flags options
)
500 int status
, save_errno
;
508 pid
= waitpid (ptid
.pid (), &status
, 0);
511 while (pid
== -1 && errno
== EINTR
);
513 clear_sigint_trap ();
517 fprintf_unfiltered (gdb_stderr
,
518 _("Child process unexpectedly missing: %s.\n"),
519 safe_strerror (save_errno
));
521 /* Claim it exited with unknown signal. */
522 ourstatus
->set_signalled (GDB_SIGNAL_UNKNOWN
);
523 return inferior_ptid
;
526 /* Ignore terminated detached child processes. */
527 if (!WIFSTOPPED (status
) && pid
!= inferior_ptid
.pid ())
532 /* AIX has a couple of strange returns from wait(). */
534 /* stop after load" status. */
536 ourstatus
->set_loaded ();
537 /* signal 0. I have no idea why wait(2) returns with this status word. */
538 else if (status
== 0x7f)
539 ourstatus
->set_spurious ();
540 /* A normal waitstatus. Let the usual macros deal with it. */
542 store_waitstatus (ourstatus
, status
);
548 /* Set the current architecture from the host running GDB. Called when
549 starting a child process. */
552 rs6000_nat_target::create_inferior (const char *exec_file
,
553 const std::string
&allargs
,
554 char **env
, int from_tty
)
556 enum bfd_architecture arch
;
560 inf_ptrace_target::create_inferior (exec_file
, allargs
, env
, from_tty
);
564 arch
= bfd_arch_rs6000
;
565 mach
= bfd_mach_rs6k
;
569 arch
= bfd_arch_powerpc
;
573 /* FIXME: schauer/2002-02-25:
574 We don't know if we are executing a 32 or 64 bit executable,
575 and have no way to pass the proper word size to rs6000_gdbarch_init.
576 So we have to avoid switching to a new architecture, if the architecture
578 Blindly calling rs6000_gdbarch_init used to work in older versions of
579 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
580 determine the wordsize. */
581 if (current_program_space
->exec_bfd ())
583 const struct bfd_arch_info
*exec_bfd_arch_info
;
586 = bfd_get_arch_info (current_program_space
->exec_bfd ());
587 if (arch
== exec_bfd_arch_info
->arch
)
591 bfd_default_set_arch_mach (&abfd
, arch
, mach
);
594 info
.bfd_arch_info
= bfd_get_arch_info (&abfd
);
595 info
.abfd
= current_program_space
->exec_bfd ();
597 if (!gdbarch_update_p (info
))
598 internal_error (__FILE__
, __LINE__
,
599 _("rs6000_create_inferior: failed "
600 "to select architecture"));
604 /* Shared Object support. */
606 /* Return the LdInfo data for the given process. Raises an error
607 if the data could not be obtained. */
609 static gdb::byte_vector
610 rs6000_ptrace_ldinfo (ptid_t ptid
)
612 const int pid
= ptid
.pid ();
613 gdb::byte_vector
ldi (1024);
619 rc
= rs6000_ptrace64 (PT_LDINFO
, pid
, (unsigned long) ldi
.data (),
622 rc
= rs6000_ptrace32 (PT_LDINFO
, pid
, (int *) ldi
.data (),
626 break; /* Success, we got the entire ld_info data. */
629 perror_with_name (_("ptrace ldinfo"));
631 /* ldi is not big enough. Double it and try again. */
632 ldi
.resize (ldi
.size () * 2);
638 /* Implement the to_xfer_partial target_ops method for
639 TARGET_OBJECT_LIBRARIES_AIX objects. */
641 enum target_xfer_status
642 rs6000_nat_target::xfer_shared_libraries
643 (enum target_object object
,
644 const char *annex
, gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
645 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
649 /* This function assumes that it is being run with a live process.
650 Core files are handled via gdbarch. */
651 gdb_assert (target_has_execution ());
654 return TARGET_XFER_E_IO
;
656 gdb::byte_vector ldi_buf
= rs6000_ptrace_ldinfo (inferior_ptid
);
657 result
= rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf
.data (),
658 readbuf
, offset
, len
, 1);
661 return TARGET_XFER_EOF
;
664 *xfered_len
= result
;
665 return TARGET_XFER_OK
;
669 void _initialize_rs6000_nat ();
671 _initialize_rs6000_nat ()
673 add_inf_child_target (&the_rs6000_nat_target
);