1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
3 Copyright (C) 1986-2023 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 /* Header files for getting ppid in AIX of a child process. */
59 #include <sys/types.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. */
68 #if defined (__ld_info32) || defined (__ld_info64)
72 /* Return whether the current architecture is 64-bit. */
77 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
80 class rs6000_nat_target final
: public inf_ptrace_target
83 void fetch_registers (struct regcache
*, int) override
;
84 void store_registers (struct regcache
*, int) override
;
86 enum target_xfer_status
xfer_partial (enum target_object object
,
89 const gdb_byte
*writebuf
,
90 ULONGEST offset
, ULONGEST len
,
91 ULONGEST
*xfered_len
) override
;
93 void create_inferior (const char *, const std::string
&,
94 char **, int) override
;
96 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
98 /* Fork detection related functions, For adding multi process debugging
100 void follow_fork (inferior
*, ptid_t
, target_waitkind
, bool, bool) override
;
104 void post_startup_inferior (ptid_t ptid
) override
;
107 enum target_xfer_status
108 xfer_shared_libraries (enum target_object object
,
109 const char *annex
, gdb_byte
*readbuf
,
110 const gdb_byte
*writebuf
,
111 ULONGEST offset
, ULONGEST len
,
112 ULONGEST
*xfered_len
);
115 static rs6000_nat_target the_rs6000_nat_target
;
117 /* The below declaration is to track number of times, parent has
118 reported fork event before its children. */
120 static std::list
<pid_t
> aix_pending_parent
;
122 /* The below declaration is for a child process event that
123 is reported before its corresponding parent process in
124 the event of a fork (). */
126 static std::list
<pid_t
> aix_pending_children
;
129 aix_remember_child (pid_t pid
)
131 aix_pending_children
.push_front (pid
);
135 aix_remember_parent (pid_t pid
)
137 aix_pending_parent
.push_front (pid
);
140 /* This function returns a parent of a child process. */
143 find_my_aix_parent (pid_t child_pid
)
145 struct procsinfo ProcessBuffer1
;
147 if (getprocs (&ProcessBuffer1
, sizeof (ProcessBuffer1
),
148 NULL
, 0, &child_pid
, 1) != 1)
151 return ProcessBuffer1
.pi_ppid
;
154 /* In the below function we check if there was any child
155 process pending. If it exists we return it from the
156 list, otherwise we return a null. */
159 has_my_aix_child_reported (pid_t parent_pid
)
162 auto it
= std::find_if (aix_pending_children
.begin (),
163 aix_pending_children
.end (),
164 [=] (pid_t child_pid
)
166 return find_my_aix_parent (child_pid
) == parent_pid
;
168 if (it
!= aix_pending_children
.end ())
171 aix_pending_children
.erase (it
);
176 /* In the below function we check if there was any parent
177 process pending. If it exists we return it from the
178 list, otherwise we return a null. */
181 has_my_aix_parent_reported (pid_t child_pid
)
183 pid_t my_parent
= find_my_aix_parent (child_pid
);
184 auto it
= std::find (aix_pending_parent
.begin (),
185 aix_pending_parent
.end (),
187 if (it
!= aix_pending_parent
.end ())
189 aix_pending_parent
.erase (it
);
195 /* Given REGNO, a gdb register number, return the corresponding
196 number suitable for use as a ptrace() parameter. Return -1 if
197 there's no suitable mapping. Also, set the int pointed to by
198 ISFLOAT to indicate whether REGNO is a floating point register. */
201 regmap (struct gdbarch
*gdbarch
, int regno
, int *isfloat
)
203 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
206 if (tdep
->ppc_gp0_regnum
<= regno
207 && regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
)
209 else if (tdep
->ppc_fp0_regnum
>= 0
210 && tdep
->ppc_fp0_regnum
<= regno
211 && regno
< tdep
->ppc_fp0_regnum
+ ppc_num_fprs
)
214 return regno
- tdep
->ppc_fp0_regnum
+ FPR0
;
216 else if (regno
== gdbarch_pc_regnum (gdbarch
))
218 else if (regno
== tdep
->ppc_ps_regnum
)
220 else if (regno
== tdep
->ppc_cr_regnum
)
222 else if (regno
== tdep
->ppc_lr_regnum
)
224 else if (regno
== tdep
->ppc_ctr_regnum
)
226 else if (regno
== tdep
->ppc_xer_regnum
)
228 else if (tdep
->ppc_fpscr_regnum
>= 0
229 && regno
== tdep
->ppc_fpscr_regnum
)
231 else if (tdep
->ppc_mq_regnum
>= 0 && regno
== tdep
->ppc_mq_regnum
)
237 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
240 rs6000_ptrace32 (int req
, int id
, int *addr
, int data
, int *buf
)
243 int ret
= ptrace64 (req
, id
, (uintptr_t) addr
, data
, buf
);
245 int ret
= ptrace (req
, id
, (int *)addr
, data
, buf
);
248 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
249 req
, id
, (unsigned int)addr
, data
, (unsigned int)buf
, ret
);
254 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
257 rs6000_ptrace64 (int req
, int id
, long long addr
, int data
, void *buf
)
260 # ifdef HAVE_PTRACE64
261 int ret
= ptrace64 (req
, id
, addr
, data
, (PTRACE_TYPE_ARG5
) buf
);
263 int ret
= ptracex (req
, id
, addr
, data
, (PTRACE_TYPE_ARG5
) buf
);
269 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
270 req
, id
, hex_string (addr
), data
, (unsigned int)buf
, ret
);
275 void rs6000_nat_target::post_startup_inferior (ptid_t ptid
)
278 /* In AIX to turn on multi process debugging in ptrace
279 PT_MULTI is the option to be passed,
280 with the process ID which can fork () and
281 the data parameter [fourth parameter] must be 1. */
284 rs6000_ptrace32 (PT_MULTI
, ptid
.pid(), 0, 1, 0);
286 rs6000_ptrace64 (PT_MULTI
, ptid
.pid(), 0, 1, 0);
290 rs6000_nat_target::follow_fork (inferior
*child_inf
, ptid_t child_ptid
,
291 target_waitkind fork_kind
, bool follow_child
,
295 /* Once the fork event is detected the infrun.c code
296 calls the target_follow_fork to take care of
297 follow child and detach the child activity which is
298 done using the function below. */
300 inf_ptrace_target::follow_fork (child_inf
, child_ptid
, fork_kind
,
301 follow_child
, detach_fork
);
303 /* If we detach fork and follow child we do not want the child
304 process to geneate events that ptrace can trace. Hence we
307 if (detach_fork
&& !follow_child
)
310 rs6000_ptrace64 (PT_DETACH
, child_ptid
.pid (), 0, 0, 0);
312 rs6000_ptrace32 (PT_DETACH
, child_ptid
.pid (), 0, 0, 0);
316 /* Fetch register REGNO from the inferior. */
319 fetch_register (struct regcache
*regcache
, int regno
)
321 struct gdbarch
*gdbarch
= regcache
->arch ();
322 int addr
[PPC_MAX_REGISTER_SIZE
];
324 pid_t pid
= regcache
->ptid ().pid ();
326 /* Retrieved values may be -1, so infer errors from errno. */
329 nr
= regmap (gdbarch
, regno
, &isfloat
);
331 /* Floating-point registers. */
333 rs6000_ptrace32 (PT_READ_FPR
, pid
, addr
, nr
, 0);
335 /* Bogus register number. */
338 if (regno
>= gdbarch_num_regs (gdbarch
))
339 gdb_printf (gdb_stderr
,
340 "gdb error: register no %d not implemented.\n",
345 /* Fixed-point registers. */
349 *addr
= rs6000_ptrace32 (PT_READ_GPR
, pid
, (int *) nr
, 0, 0);
352 /* PT_READ_GPR requires the buffer parameter to point to long long,
353 even if the register is really only 32 bits. */
355 rs6000_ptrace64 (PT_READ_GPR
, pid
, nr
, 0, &buf
);
356 if (register_size (gdbarch
, regno
) == 8)
357 memcpy (addr
, &buf
, 8);
364 regcache
->raw_supply (regno
, (char *) addr
);
368 /* FIXME: this happens 3 times at the start of each 64-bit program. */
369 perror (_("ptrace read"));
375 /* Store register REGNO back into the inferior. */
378 store_register (struct regcache
*regcache
, int regno
)
380 struct gdbarch
*gdbarch
= regcache
->arch ();
381 int addr
[PPC_MAX_REGISTER_SIZE
];
383 pid_t pid
= regcache
->ptid ().pid ();
385 /* Fetch the register's value from the register cache. */
386 regcache
->raw_collect (regno
, addr
);
388 /* -1 can be a successful return value, so infer errors from errno. */
391 nr
= regmap (gdbarch
, regno
, &isfloat
);
393 /* Floating-point registers. */
395 rs6000_ptrace32 (PT_WRITE_FPR
, pid
, addr
, nr
, 0);
397 /* Bogus register number. */
400 if (regno
>= gdbarch_num_regs (gdbarch
))
401 gdb_printf (gdb_stderr
,
402 "gdb error: register no %d not implemented.\n",
406 /* Fixed-point registers. */
409 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
410 the register's value is passed by value, but for 64-bit inferiors,
411 the address of a buffer containing the value is passed. */
413 rs6000_ptrace32 (PT_WRITE_GPR
, pid
, (int *) nr
, *addr
, 0);
416 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
417 area, even if the register is really only 32 bits. */
419 if (register_size (gdbarch
, regno
) == 8)
420 memcpy (&buf
, addr
, 8);
423 rs6000_ptrace64 (PT_WRITE_GPR
, pid
, nr
, 0, &buf
);
429 perror (_("ptrace write"));
434 /* Read from the inferior all registers if REGNO == -1 and just register
438 rs6000_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
440 struct gdbarch
*gdbarch
= regcache
->arch ();
442 fetch_register (regcache
, regno
);
446 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
448 /* Read 32 general purpose registers. */
449 for (regno
= tdep
->ppc_gp0_regnum
;
450 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
453 fetch_register (regcache
, regno
);
456 /* Read general purpose floating point registers. */
457 if (tdep
->ppc_fp0_regnum
>= 0)
458 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
459 fetch_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
461 /* Read special registers. */
462 fetch_register (regcache
, gdbarch_pc_regnum (gdbarch
));
463 fetch_register (regcache
, tdep
->ppc_ps_regnum
);
464 fetch_register (regcache
, tdep
->ppc_cr_regnum
);
465 fetch_register (regcache
, tdep
->ppc_lr_regnum
);
466 fetch_register (regcache
, tdep
->ppc_ctr_regnum
);
467 fetch_register (regcache
, tdep
->ppc_xer_regnum
);
468 if (tdep
->ppc_fpscr_regnum
>= 0)
469 fetch_register (regcache
, tdep
->ppc_fpscr_regnum
);
470 if (tdep
->ppc_mq_regnum
>= 0)
471 fetch_register (regcache
, tdep
->ppc_mq_regnum
);
475 /* Store our register values back into the inferior.
476 If REGNO is -1, do this for all registers.
477 Otherwise, REGNO specifies which register (so we can save time). */
480 rs6000_nat_target::store_registers (struct regcache
*regcache
, int regno
)
482 struct gdbarch
*gdbarch
= regcache
->arch ();
484 store_register (regcache
, regno
);
488 ppc_gdbarch_tdep
*tdep
= gdbarch_tdep
<ppc_gdbarch_tdep
> (gdbarch
);
490 /* Write general purpose registers first. */
491 for (regno
= tdep
->ppc_gp0_regnum
;
492 regno
< tdep
->ppc_gp0_regnum
+ ppc_num_gprs
;
495 store_register (regcache
, regno
);
498 /* Write floating point registers. */
499 if (tdep
->ppc_fp0_regnum
>= 0)
500 for (regno
= 0; regno
< ppc_num_fprs
; regno
++)
501 store_register (regcache
, tdep
->ppc_fp0_regnum
+ regno
);
503 /* Write special registers. */
504 store_register (regcache
, gdbarch_pc_regnum (gdbarch
));
505 store_register (regcache
, tdep
->ppc_ps_regnum
);
506 store_register (regcache
, tdep
->ppc_cr_regnum
);
507 store_register (regcache
, tdep
->ppc_lr_regnum
);
508 store_register (regcache
, tdep
->ppc_ctr_regnum
);
509 store_register (regcache
, tdep
->ppc_xer_regnum
);
510 if (tdep
->ppc_fpscr_regnum
>= 0)
511 store_register (regcache
, tdep
->ppc_fpscr_regnum
);
512 if (tdep
->ppc_mq_regnum
>= 0)
513 store_register (regcache
, tdep
->ppc_mq_regnum
);
517 /* Implement the to_xfer_partial target_ops method. */
519 enum target_xfer_status
520 rs6000_nat_target::xfer_partial (enum target_object object
,
521 const char *annex
, gdb_byte
*readbuf
,
522 const gdb_byte
*writebuf
,
523 ULONGEST offset
, ULONGEST len
,
524 ULONGEST
*xfered_len
)
526 pid_t pid
= inferior_ptid
.pid ();
527 int arch64
= ARCH64 ();
531 case TARGET_OBJECT_LIBRARIES_AIX
:
532 return xfer_shared_libraries (object
, annex
,
534 offset
, len
, xfered_len
);
535 case TARGET_OBJECT_MEMORY
:
539 PTRACE_TYPE_RET word
;
540 gdb_byte byte
[sizeof (PTRACE_TYPE_RET
)];
542 ULONGEST rounded_offset
;
545 /* Round the start offset down to the next long word
547 rounded_offset
= offset
& -(ULONGEST
) sizeof (PTRACE_TYPE_RET
);
549 /* Since ptrace will transfer a single word starting at that
550 rounded_offset the partial_len needs to be adjusted down to
551 that (remember this function only does a single transfer).
552 Should the required length be even less, adjust it down
554 partial_len
= (rounded_offset
+ sizeof (PTRACE_TYPE_RET
)) - offset
;
555 if (partial_len
> len
)
560 /* If OFFSET:PARTIAL_LEN is smaller than
561 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
562 be needed. Read in the entire word. */
563 if (rounded_offset
< offset
564 || (offset
+ partial_len
565 < rounded_offset
+ sizeof (PTRACE_TYPE_RET
)))
567 /* Need part of initial word -- fetch it. */
569 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
570 rounded_offset
, 0, NULL
);
572 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
578 /* Copy data to be written over corresponding part of
580 memcpy (buffer
.byte
+ (offset
- rounded_offset
),
581 writebuf
, partial_len
);
585 rs6000_ptrace64 (PT_WRITE_D
, pid
,
586 rounded_offset
, buffer
.word
, NULL
);
588 rs6000_ptrace32 (PT_WRITE_D
, pid
,
589 (int *) (uintptr_t) rounded_offset
,
592 return TARGET_XFER_EOF
;
599 buffer
.word
= rs6000_ptrace64 (PT_READ_I
, pid
,
600 rounded_offset
, 0, NULL
);
602 buffer
.word
= rs6000_ptrace32 (PT_READ_I
, pid
,
603 (int *)(uintptr_t)rounded_offset
,
606 return TARGET_XFER_EOF
;
608 /* Copy appropriate bytes out of the buffer. */
609 memcpy (readbuf
, buffer
.byte
+ (offset
- rounded_offset
),
613 *xfered_len
= (ULONGEST
) partial_len
;
614 return TARGET_XFER_OK
;
618 return TARGET_XFER_E_IO
;
622 /* Wait for the child specified by PTID to do something. Return the
623 process ID of the child, or MINUS_ONE_PTID in case of error; store
624 the status in *OURSTATUS. */
627 rs6000_nat_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
628 target_wait_flags options
)
631 int status
, save_errno
;
639 pid
= waitpid (ptid
.pid (), &status
, 0);
642 while (pid
== -1 && errno
== EINTR
);
644 clear_sigint_trap ();
648 gdb_printf (gdb_stderr
,
649 _("Child process unexpectedly missing: %s.\n"),
650 safe_strerror (save_errno
));
652 ourstatus
->set_ignore ();
653 return minus_one_ptid
;
656 /* Ignore terminated detached child processes. */
657 if (!WIFSTOPPED (status
) && find_inferior_pid (this, pid
) == nullptr)
660 /* Check for a fork () event. */
661 if ((status
& 0xff) == W_SFWTED
)
663 /* Checking whether it is a parent or a child event. */
665 /* If the event is a child we check if there was a parent
666 event recorded before. If yes we got the parent child
667 relationship. If not we push this child and wait for
668 the next fork () event. */
669 if (find_inferior_pid (this, pid
) == nullptr)
671 pid_t parent_pid
= has_my_aix_parent_reported (pid
);
674 ourstatus
->set_forked (ptid_t (pid
));
675 return ptid_t (parent_pid
);
677 aix_remember_child (pid
);
680 /* If the event is a parent we check if there was a child
681 event recorded before. If yes we got the parent child
682 relationship. If not we push this parent and wait for
683 the next fork () event. */
686 pid_t child_pid
= has_my_aix_child_reported (pid
);
689 ourstatus
->set_forked (ptid_t (child_pid
));
692 aix_remember_parent (pid
);
700 /* AIX has a couple of strange returns from wait(). */
702 /* stop after load" status. */
704 ourstatus
->set_loaded ();
705 /* 0x7f is signal 0. 0x17f and 0x137f are status returned
706 if we follow parent, a switch is made to a child post parent
707 execution and child continues its execution [user switches
708 to child and presses continue]. */
709 else if (status
== 0x7f || status
== 0x17f || status
== 0x137f)
710 ourstatus
->set_spurious ();
711 /* A normal waitstatus. Let the usual macros deal with it. */
713 *ourstatus
= host_status_to_waitstatus (status
);
719 /* Set the current architecture from the host running GDB. Called when
720 starting a child process. */
723 rs6000_nat_target::create_inferior (const char *exec_file
,
724 const std::string
&allargs
,
725 char **env
, int from_tty
)
727 enum bfd_architecture arch
;
731 inf_ptrace_target::create_inferior (exec_file
, allargs
, env
, from_tty
);
735 arch
= bfd_arch_rs6000
;
736 mach
= bfd_mach_rs6k
;
740 arch
= bfd_arch_powerpc
;
744 /* FIXME: schauer/2002-02-25:
745 We don't know if we are executing a 32 or 64 bit executable,
746 and have no way to pass the proper word size to rs6000_gdbarch_init.
747 So we have to avoid switching to a new architecture, if the architecture
749 Blindly calling rs6000_gdbarch_init used to work in older versions of
750 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
751 determine the wordsize. */
752 if (current_program_space
->exec_bfd ())
754 const struct bfd_arch_info
*exec_bfd_arch_info
;
757 = bfd_get_arch_info (current_program_space
->exec_bfd ());
758 if (arch
== exec_bfd_arch_info
->arch
)
762 bfd_default_set_arch_mach (&abfd
, arch
, mach
);
765 info
.bfd_arch_info
= bfd_get_arch_info (&abfd
);
766 info
.abfd
= current_program_space
->exec_bfd ();
768 if (!gdbarch_update_p (info
))
769 internal_error (_("rs6000_create_inferior: failed "
770 "to select architecture"));
774 /* Shared Object support. */
776 /* Return the LdInfo data for the given process. Raises an error
777 if the data could not be obtained. */
779 static gdb::byte_vector
780 rs6000_ptrace_ldinfo (ptid_t ptid
)
782 const int pid
= ptid
.pid ();
783 gdb::byte_vector
ldi (1024);
789 rc
= rs6000_ptrace64 (PT_LDINFO
, pid
, (unsigned long) ldi
.data (),
792 rc
= rs6000_ptrace32 (PT_LDINFO
, pid
, (int *) ldi
.data (),
796 break; /* Success, we got the entire ld_info data. */
799 perror_with_name (_("ptrace ldinfo"));
801 /* ldi is not big enough. Double it and try again. */
802 ldi
.resize (ldi
.size () * 2);
808 /* Implement the to_xfer_partial target_ops method for
809 TARGET_OBJECT_LIBRARIES_AIX objects. */
811 enum target_xfer_status
812 rs6000_nat_target::xfer_shared_libraries
813 (enum target_object object
,
814 const char *annex
, gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
815 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
819 /* This function assumes that it is being run with a live process.
820 Core files are handled via gdbarch. */
821 gdb_assert (target_has_execution ());
824 return TARGET_XFER_E_IO
;
826 gdb::byte_vector ldi_buf
= rs6000_ptrace_ldinfo (inferior_ptid
);
827 result
= rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf
.data (),
828 readbuf
, offset
, len
, 1);
831 return TARGET_XFER_EOF
;
834 *xfered_len
= result
;
835 return TARGET_XFER_OK
;
839 void _initialize_rs6000_nat ();
841 _initialize_rs6000_nat ()
843 add_inf_child_target (&the_rs6000_nat_target
);