Bump GDB's version number to 15.0.91.DATE-git.
[binutils-gdb.git] / gdb / rs6000-aix-nat.c
blob908671a713a41f619bbcc8c61af9568d2138c4ea
1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 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/>. */
20 #include "inferior.h"
21 #include "target.h"
22 #include "gdbcore.h"
23 #include "symfile.h"
24 #include "objfiles.h"
25 #include "bfd.h"
26 #include "gdb-stabs.h"
27 #include "regcache.h"
28 #include "arch-utils.h"
29 #include "inf-child.h"
30 #include "inf-ptrace.h"
31 #include "ppc-tdep.h"
32 #include "rs6000-aix-tdep.h"
33 #include "exec.h"
34 #include "observable.h"
35 #include "xcoffread.h"
37 #include <sys/ptrace.h>
38 #include <sys/reg.h>
40 #include <sys/dir.h>
41 #include <sys/user.h>
42 #include <signal.h>
43 #include <sys/ioctl.h>
44 #include <fcntl.h>
46 #include <a.out.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include "gdb_bfd.h"
50 #include <sys/core.h>
51 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
52 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
53 #include <sys/ldr.h>
54 #include <sys/systemcfg.h>
56 /* Header files for getting ppid in AIX of a child process. */
57 #include <procinfo.h>
58 #include <sys/types.h>
60 /* Header files for alti-vec reg. */
61 #include <sys/context.h>
63 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
64 debugging 32-bit and 64-bit processes. Define a typedef and macros for
65 accessing fields in the appropriate structures. */
67 /* In 32-bit compilation mode (which is the only mode from which ptrace()
68 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
70 #if defined (__ld_info32) || defined (__ld_info64)
71 # define ARCH3264
72 #endif
74 /* Return whether the current architecture is 64-bit. */
76 #ifndef ARCH3264
77 # define ARCH64() 0
78 #else
79 # define ARCH64() (register_size (current_inferior ()->arch (), 0) == 8)
80 #endif
82 class rs6000_nat_target final : public inf_ptrace_target
84 public:
85 void fetch_registers (struct regcache *, int) override;
86 void store_registers (struct regcache *, int) override;
88 enum target_xfer_status xfer_partial (enum target_object object,
89 const char *annex,
90 gdb_byte *readbuf,
91 const gdb_byte *writebuf,
92 ULONGEST offset, ULONGEST len,
93 ULONGEST *xfered_len) override;
95 void create_inferior (const char *, const std::string &,
96 char **, int) override;
98 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
100 /* Fork detection related functions, For adding multi process debugging
101 support. */
102 void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
104 const struct target_desc *read_description () override;
106 int insert_fork_catchpoint (int) override;
107 int remove_fork_catchpoint (int) override;
109 protected:
111 void post_startup_inferior (ptid_t ptid) override;
113 private:
114 enum target_xfer_status
115 xfer_shared_libraries (enum target_object object,
116 const char *annex, gdb_byte *readbuf,
117 const gdb_byte *writebuf,
118 ULONGEST offset, ULONGEST len,
119 ULONGEST *xfered_len);
122 static rs6000_nat_target the_rs6000_nat_target;
124 /* The below declaration is to track number of times, parent has
125 reported fork event before its children. */
127 static std::list<pid_t> aix_pending_parent;
129 /* The below declaration is for a child process event that
130 is reported before its corresponding parent process in
131 the event of a fork (). */
133 static std::list<pid_t> aix_pending_children;
135 static void
136 aix_remember_child (pid_t pid)
138 aix_pending_children.push_front (pid);
141 static void
142 aix_remember_parent (pid_t pid)
144 aix_pending_parent.push_front (pid);
147 /* This function returns a parent of a child process. */
149 static pid_t
150 find_my_aix_parent (pid_t child_pid)
152 struct procsinfo ProcessBuffer1;
154 if (getprocs (&ProcessBuffer1, sizeof (ProcessBuffer1),
155 NULL, 0, &child_pid, 1) != 1)
156 return 0;
157 else
158 return ProcessBuffer1.pi_ppid;
161 /* In the below function we check if there was any child
162 process pending. If it exists we return it from the
163 list, otherwise we return a null. */
165 static pid_t
166 has_my_aix_child_reported (pid_t parent_pid)
168 pid_t child = 0;
169 auto it = std::find_if (aix_pending_children.begin (),
170 aix_pending_children.end (),
171 [=] (pid_t child_pid)
173 return find_my_aix_parent (child_pid) == parent_pid;
175 if (it != aix_pending_children.end ())
177 child = *it;
178 aix_pending_children.erase (it);
180 return child;
183 /* In the below function we check if there was any parent
184 process pending. If it exists we return it from the
185 list, otherwise we return a null. */
187 static pid_t
188 has_my_aix_parent_reported (pid_t child_pid)
190 pid_t my_parent = find_my_aix_parent (child_pid);
191 auto it = std::find (aix_pending_parent.begin (),
192 aix_pending_parent.end (),
193 my_parent);
194 if (it != aix_pending_parent.end ())
196 aix_pending_parent.erase (it);
197 return my_parent;
199 return 0;
202 /* Given REGNO, a gdb register number, return the corresponding
203 number suitable for use as a ptrace() parameter. Return -1 if
204 there's no suitable mapping. Also, set the int pointed to by
205 ISFLOAT to indicate whether REGNO is a floating point register. */
207 static int
208 regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
210 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
212 *isfloat = 0;
213 if (tdep->ppc_gp0_regnum <= regno
214 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
215 return regno;
216 else if (tdep->ppc_fp0_regnum >= 0
217 && tdep->ppc_fp0_regnum <= regno
218 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
220 *isfloat = 1;
221 return regno - tdep->ppc_fp0_regnum + FPR0;
223 else if (regno == gdbarch_pc_regnum (gdbarch))
224 return IAR;
225 else if (regno == tdep->ppc_ps_regnum)
226 return MSR;
227 else if (regno == tdep->ppc_cr_regnum)
228 return CR;
229 else if (regno == tdep->ppc_lr_regnum)
230 return LR;
231 else if (regno == tdep->ppc_ctr_regnum)
232 return CTR;
233 else if (regno == tdep->ppc_xer_regnum)
234 return XER;
235 else if (tdep->ppc_fpscr_regnum >= 0
236 && regno == tdep->ppc_fpscr_regnum)
237 return FPSCR;
238 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
239 return MQ;
240 else
241 return -1;
244 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
246 static int
247 rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
249 #ifdef HAVE_PTRACE64
250 int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
251 #else
252 int ret = ptrace (req, id, (int *)addr, data, buf);
253 #endif
254 #if 0
255 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
256 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
257 #endif
258 return ret;
261 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
263 static int
264 rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
266 #ifdef ARCH3264
267 # ifdef HAVE_PTRACE64
268 int ret = ptrace64 (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
269 # else
270 int ret = ptracex (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
271 # endif
272 #else
273 int ret = 0;
274 #endif
275 #if 0
276 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
277 req, id, hex_string (addr), data, (unsigned int)buf, ret);
278 #endif
279 return ret;
282 /* Store the vsx registers. */
284 static void
285 store_vsx_register_aix (struct regcache *regcache, int regno)
287 int ret;
288 struct gdbarch *gdbarch = regcache->arch ();
289 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
290 struct thrdentry64 thrdentry;
291 __vsx_context_t vsx;
292 pid_t pid = inferior_ptid.pid ();
293 tid64_t thrd_i = 0;
295 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
296 &thrd_i, 1) == 1)
297 thrd_i = thrdentry.ti_tid;
299 memset(&vsx, 0, sizeof(__vsx_context_t));
300 if (__power_vsx() && thrd_i > 0)
302 if (ARCH64 ())
303 ret = rs6000_ptrace64 (PTT_READ_VSX, thrd_i, (long long) &vsx, 0, 0);
304 else
305 ret = rs6000_ptrace32 (PTT_READ_VSX, thrd_i, (int *)&vsx, 0, 0);
306 if (ret < 0)
307 return;
309 regcache->raw_collect (regno, &(vsx.__vsr_dw1[0])+
310 regno - tdep->ppc_vsr0_upper_regnum);
312 if (ARCH64 ())
313 ret = rs6000_ptrace64 (PTT_WRITE_VSX, thrd_i, (long long) &vsx, 0, 0);
314 else
315 ret = rs6000_ptrace32 (PTT_WRITE_VSX, thrd_i, (int *) &vsx, 0, 0);
317 if (ret < 0)
318 perror_with_name (_("Unable to write VSX registers after reading it"));
322 /* Store Altivec registers. */
324 static void
325 store_altivec_register_aix (struct regcache *regcache, int regno)
327 int ret;
328 struct gdbarch *gdbarch = regcache->arch ();
329 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
330 struct thrdentry64 thrdentry;
331 __vmx_context_t vmx;
332 pid_t pid = inferior_ptid.pid ();
333 tid64_t thrd_i = 0;
335 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
336 &thrd_i, 1) == 1)
337 thrd_i = thrdentry.ti_tid;
339 memset(&vmx, 0, sizeof(__vmx_context_t));
340 if (__power_vmx() && thrd_i > 0)
342 if (ARCH64 ())
343 ret = rs6000_ptrace64 (PTT_READ_VEC, thrd_i, (long long) &vmx, 0, 0);
344 else
345 ret = rs6000_ptrace32 (PTT_READ_VEC, thrd_i, (int *) &vmx, 0, 0);
346 if (ret < 0)
347 return;
349 regcache->raw_collect (regno, &(vmx.__vr[0]) + regno
350 - tdep->ppc_vr0_regnum);
352 if (ARCH64 ())
353 ret = rs6000_ptrace64 (PTT_WRITE_VEC, thrd_i, (long long) &vmx, 0, 0);
354 else
355 ret = rs6000_ptrace32 (PTT_WRITE_VEC, thrd_i, (int *) &vmx, 0, 0);
356 if (ret < 0)
357 perror_with_name (_("Unable to store AltiVec register after reading it"));
361 /* Supply altivec registers. */
363 static void
364 supply_vrregset_aix (struct regcache *regcache, __vmx_context_t *vmx)
366 int i;
367 struct gdbarch *gdbarch = regcache->arch ();
368 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
369 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
371 for (i = 0; i < num_of_vrregs; i++)
372 regcache->raw_supply (tdep->ppc_vr0_regnum + i,
373 &(vmx->__vr[i]));
374 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx->__vrsave));
375 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx->__vscr));
378 /* Fetch altivec register. */
380 static void
381 fetch_altivec_registers_aix (struct regcache *regcache)
383 struct thrdentry64 thrdentry;
384 __vmx_context_t vmx;
385 pid_t pid = current_inferior ()->pid;
386 tid64_t thrd_i = 0;
388 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
389 &thrd_i, 1) == 1)
390 thrd_i = thrdentry.ti_tid;
392 memset(&vmx, 0, sizeof(__vmx_context_t));
393 if (__power_vmx() && thrd_i > 0)
395 if (ARCH64 ())
396 rs6000_ptrace64 (PTT_READ_VEC, thrd_i, (long long) &vmx, 0, 0);
397 else
398 rs6000_ptrace32 (PTT_READ_VEC, thrd_i, (int *) &vmx, 0, 0);
399 supply_vrregset_aix (regcache, &vmx);
403 /* supply vsx register. */
405 static void
406 supply_vsxregset_aix (struct regcache *regcache, __vsx_context_t *vsx)
408 int i;
409 struct gdbarch *gdbarch = regcache->arch ();
410 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
412 for (i = 0; i < ppc_num_vshrs; i++)
413 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + i,
414 &(vsx->__vsr_dw1[i]));
417 /* Fetch vsx registers. */
418 static void
419 fetch_vsx_registers_aix (struct regcache *regcache)
421 struct thrdentry64 thrdentry;
422 __vsx_context_t vsx;
423 pid_t pid = current_inferior ()->pid;
424 tid64_t thrd_i = 0;
426 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
427 &thrd_i, 1) == 1)
428 thrd_i = thrdentry.ti_tid;
430 memset(&vsx, 0, sizeof(__vsx_context_t));
431 if (__power_vsx() && thrd_i > 0)
433 if (ARCH64 ())
434 rs6000_ptrace64 (PTT_READ_VSX, thrd_i, (long long) &vsx, 0, 0);
435 else
436 rs6000_ptrace32 (PTT_READ_VSX, thrd_i, (int *) &vsx, 0, 0);
437 supply_vsxregset_aix (regcache, &vsx);
441 void rs6000_nat_target::post_startup_inferior (ptid_t ptid)
444 /* In AIX to turn on multi process debugging in ptrace
445 PT_MULTI is the option to be passed,
446 with the process ID which can fork () and
447 the data parameter [fourth parameter] must be 1. */
449 if (!ARCH64 ())
450 rs6000_ptrace32 (PT_MULTI, ptid.pid(), 0, 1, 0);
451 else
452 rs6000_ptrace64 (PT_MULTI, ptid.pid(), 0, 1, 0);
455 void
456 rs6000_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
457 target_waitkind fork_kind, bool follow_child,
458 bool detach_fork)
461 /* Once the fork event is detected the infrun.c code
462 calls the target_follow_fork to take care of
463 follow child and detach the child activity which is
464 done using the function below. */
466 inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
467 follow_child, detach_fork);
469 /* If we detach fork and follow child we do not want the child
470 process to generate events that ptrace can trace. Hence we
471 detach it. */
473 if (detach_fork && !follow_child)
475 if (ARCH64 ())
476 rs6000_ptrace64 (PT_DETACH, child_ptid.pid (), 0, 0, 0);
477 else
478 rs6000_ptrace32 (PT_DETACH, child_ptid.pid (), 0, 0, 0);
482 /* Functions for catchpoint in AIX. */
484 rs6000_nat_target::insert_fork_catchpoint (int pid)
486 return 0;
490 rs6000_nat_target::remove_fork_catchpoint (int pid)
492 return 0;
495 /* Fetch register REGNO from the inferior. */
497 static void
498 fetch_register (struct regcache *regcache, int regno)
500 struct gdbarch *gdbarch = regcache->arch ();
501 int addr[PPC_MAX_REGISTER_SIZE];
502 int nr, isfloat;
503 pid_t pid = regcache->ptid ().pid ();
505 /* Retrieved values may be -1, so infer errors from errno. */
506 errno = 0;
508 /* Alti-vec register. */
509 if (altivec_register_p (gdbarch, regno))
511 fetch_altivec_registers_aix (regcache);
512 return;
515 /* VSX register. */
516 if (vsx_register_p (gdbarch, regno))
518 fetch_vsx_registers_aix (regcache);
519 return;
522 nr = regmap (gdbarch, regno, &isfloat);
524 /* Floating-point registers. */
525 if (isfloat)
526 rs6000_ptrace32 (PT_READ_FPR, pid, addr, nr, 0);
528 /* Bogus register number. */
529 else if (nr < 0)
531 if (regno >= gdbarch_num_regs (gdbarch))
532 gdb_printf (gdb_stderr,
533 "gdb error: register no %d not implemented.\n",
534 regno);
535 return;
538 /* Fixed-point registers. */
539 else
541 if (!ARCH64 ())
542 *addr = rs6000_ptrace32 (PT_READ_GPR, pid, (int *) nr, 0, 0);
543 else
545 /* PT_READ_GPR requires the buffer parameter to point to long long,
546 even if the register is really only 32 bits. */
547 long long buf;
548 rs6000_ptrace64 (PT_READ_GPR, pid, nr, 0, &buf);
549 if (register_size (gdbarch, regno) == 8)
550 memcpy (addr, &buf, 8);
551 else
552 *addr = buf;
556 if (!errno)
557 regcache->raw_supply (regno, (char *) addr);
558 else
560 #if 0
561 /* FIXME: this happens 3 times at the start of each 64-bit program. */
562 perror (_("ptrace read"));
563 #endif
564 errno = 0;
568 /* Store register REGNO back into the inferior. */
570 static void
571 store_register (struct regcache *regcache, int regno)
573 struct gdbarch *gdbarch = regcache->arch ();
574 int addr[PPC_MAX_REGISTER_SIZE];
575 int nr, isfloat;
576 pid_t pid = regcache->ptid ().pid ();
578 /* Fetch the register's value from the register cache. */
579 regcache->raw_collect (regno, addr);
581 /* -1 can be a successful return value, so infer errors from errno. */
582 errno = 0;
584 if (altivec_register_p (gdbarch, regno))
586 store_altivec_register_aix (regcache, regno);
587 return;
590 if (vsx_register_p (gdbarch, regno))
592 store_vsx_register_aix (regcache, regno);
593 return;
596 nr = regmap (gdbarch, regno, &isfloat);
598 /* Floating-point registers. */
599 if (isfloat)
600 rs6000_ptrace32 (PT_WRITE_FPR, pid, addr, nr, 0);
602 /* Bogus register number. */
603 else if (nr < 0)
605 if (regno >= gdbarch_num_regs (gdbarch))
606 gdb_printf (gdb_stderr,
607 "gdb error: register no %d not implemented.\n",
608 regno);
611 /* Fixed-point registers. */
612 else
614 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
615 the register's value is passed by value, but for 64-bit inferiors,
616 the address of a buffer containing the value is passed. */
617 if (!ARCH64 ())
618 rs6000_ptrace32 (PT_WRITE_GPR, pid, (int *) nr, *addr, 0);
619 else
621 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
622 area, even if the register is really only 32 bits. */
623 long long buf;
624 if (register_size (gdbarch, regno) == 8)
625 memcpy (&buf, addr, 8);
626 else
627 buf = *addr;
628 rs6000_ptrace64 (PT_WRITE_GPR, pid, nr, 0, &buf);
632 if (errno)
634 perror (_("ptrace write"));
635 errno = 0;
639 /* Read from the inferior all registers if REGNO == -1 and just register
640 REGNO otherwise. */
642 void
643 rs6000_nat_target::fetch_registers (struct regcache *regcache, int regno)
645 struct gdbarch *gdbarch = regcache->arch ();
646 if (regno != -1)
647 fetch_register (regcache, regno);
649 else
651 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
653 /* Read 32 general purpose registers. */
654 for (regno = tdep->ppc_gp0_regnum;
655 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
656 regno++)
658 fetch_register (regcache, regno);
661 /* Read general purpose floating point registers. */
662 if (tdep->ppc_fp0_regnum >= 0)
663 for (regno = 0; regno < ppc_num_fprs; regno++)
664 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
666 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
667 fetch_altivec_registers_aix (regcache);
669 if (tdep->ppc_vsr0_upper_regnum != -1)
670 fetch_vsx_registers_aix (regcache);
672 /* Read special registers. */
673 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
674 fetch_register (regcache, tdep->ppc_ps_regnum);
675 fetch_register (regcache, tdep->ppc_cr_regnum);
676 fetch_register (regcache, tdep->ppc_lr_regnum);
677 fetch_register (regcache, tdep->ppc_ctr_regnum);
678 fetch_register (regcache, tdep->ppc_xer_regnum);
679 if (tdep->ppc_fpscr_regnum >= 0)
680 fetch_register (regcache, tdep->ppc_fpscr_regnum);
681 if (tdep->ppc_mq_regnum >= 0)
682 fetch_register (regcache, tdep->ppc_mq_regnum);
686 const struct target_desc *
687 rs6000_nat_target::read_description ()
689 if (ARCH64())
691 if (__power_vsx ())
692 return tdesc_powerpc_vsx64;
693 else if (__power_vmx ())
694 return tdesc_powerpc_altivec64;
696 else
698 if (__power_vsx ())
699 return tdesc_powerpc_vsx32;
700 else if (__power_vmx ())
701 return tdesc_powerpc_altivec32;
703 return NULL;
706 /* Store our register values back into the inferior.
707 If REGNO is -1, do this for all registers.
708 Otherwise, REGNO specifies which register (so we can save time). */
710 void
711 rs6000_nat_target::store_registers (struct regcache *regcache, int regno)
713 struct gdbarch *gdbarch = regcache->arch ();
714 if (regno != -1)
715 store_register (regcache, regno);
717 else
719 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
721 /* Write general purpose registers first. */
722 for (regno = tdep->ppc_gp0_regnum;
723 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
724 regno++)
726 store_register (regcache, regno);
729 /* Write floating point registers. */
730 if (tdep->ppc_fp0_regnum >= 0)
731 for (regno = 0; regno < ppc_num_fprs; regno++)
732 store_register (regcache, tdep->ppc_fp0_regnum + regno);
734 /* Write special registers. */
735 store_register (regcache, gdbarch_pc_regnum (gdbarch));
736 store_register (regcache, tdep->ppc_ps_regnum);
737 store_register (regcache, tdep->ppc_cr_regnum);
738 store_register (regcache, tdep->ppc_lr_regnum);
739 store_register (regcache, tdep->ppc_ctr_regnum);
740 store_register (regcache, tdep->ppc_xer_regnum);
741 if (tdep->ppc_fpscr_regnum >= 0)
742 store_register (regcache, tdep->ppc_fpscr_regnum);
743 if (tdep->ppc_mq_regnum >= 0)
744 store_register (regcache, tdep->ppc_mq_regnum);
748 /* Implement the to_xfer_partial target_ops method. */
750 enum target_xfer_status
751 rs6000_nat_target::xfer_partial (enum target_object object,
752 const char *annex, gdb_byte *readbuf,
753 const gdb_byte *writebuf,
754 ULONGEST offset, ULONGEST len,
755 ULONGEST *xfered_len)
757 pid_t pid = inferior_ptid.pid ();
758 int arch64 = ARCH64 ();
760 switch (object)
762 case TARGET_OBJECT_LIBRARIES_AIX:
763 return xfer_shared_libraries (object, annex,
764 readbuf, writebuf,
765 offset, len, xfered_len);
766 case TARGET_OBJECT_MEMORY:
768 union
770 PTRACE_TYPE_RET word;
771 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
772 } buffer;
773 ULONGEST rounded_offset;
774 LONGEST partial_len;
776 /* Round the start offset down to the next long word
777 boundary. */
778 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
780 /* Since ptrace will transfer a single word starting at that
781 rounded_offset the partial_len needs to be adjusted down to
782 that (remember this function only does a single transfer).
783 Should the required length be even less, adjust it down
784 again. */
785 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
786 if (partial_len > len)
787 partial_len = len;
789 if (writebuf)
791 /* If OFFSET:PARTIAL_LEN is smaller than
792 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
793 be needed. Read in the entire word. */
794 if (rounded_offset < offset
795 || (offset + partial_len
796 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
798 /* Need part of initial word -- fetch it. */
799 if (arch64)
800 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
801 rounded_offset, 0, NULL);
802 else
803 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
804 (int *) (uintptr_t)
805 rounded_offset,
806 0, NULL);
809 /* Copy data to be written over corresponding part of
810 buffer. */
811 memcpy (buffer.byte + (offset - rounded_offset),
812 writebuf, partial_len);
814 errno = 0;
815 if (arch64)
816 rs6000_ptrace64 (PT_WRITE_D, pid,
817 rounded_offset, buffer.word, NULL);
818 else
819 rs6000_ptrace32 (PT_WRITE_D, pid,
820 (int *) (uintptr_t) rounded_offset,
821 buffer.word, NULL);
822 if (errno)
823 return TARGET_XFER_EOF;
826 if (readbuf)
828 errno = 0;
829 if (arch64)
830 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
831 rounded_offset, 0, NULL);
832 else
833 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
834 (int *)(uintptr_t)rounded_offset,
835 0, NULL);
836 if (errno)
837 return TARGET_XFER_EOF;
839 /* Copy appropriate bytes out of the buffer. */
840 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
841 partial_len);
844 *xfered_len = (ULONGEST) partial_len;
845 return TARGET_XFER_OK;
848 default:
849 return TARGET_XFER_E_IO;
853 /* Wait for the child specified by PTID to do something. Return the
854 process ID of the child, or MINUS_ONE_PTID in case of error; store
855 the status in *OURSTATUS. */
857 ptid_t
858 rs6000_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
859 target_wait_flags options)
861 pid_t pid;
862 int status, save_errno;
864 while (1)
866 set_sigint_trap ();
870 pid = waitpid (ptid.pid (), &status, 0);
871 save_errno = errno;
873 while (pid == -1 && errno == EINTR);
875 clear_sigint_trap ();
877 if (pid == -1)
879 gdb_printf (gdb_stderr,
880 _("Child process unexpectedly missing: %s.\n"),
881 safe_strerror (save_errno));
883 ourstatus->set_ignore ();
884 return minus_one_ptid;
887 /* Ignore terminated detached child processes. */
888 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
889 continue;
891 /* Check for a fork () event. */
892 if ((status & 0xff) == W_SFWTED)
894 /* Checking whether it is a parent or a child event. */
896 /* If the event is a child we check if there was a parent
897 event recorded before. If yes we got the parent child
898 relationship. If not we push this child and wait for
899 the next fork () event. */
900 if (find_inferior_pid (this, pid) == nullptr)
902 pid_t parent_pid = has_my_aix_parent_reported (pid);
903 if (parent_pid > 0)
905 ourstatus->set_forked (ptid_t (pid));
906 return ptid_t (parent_pid);
908 aix_remember_child (pid);
911 /* If the event is a parent we check if there was a child
912 event recorded before. If yes we got the parent child
913 relationship. If not we push this parent and wait for
914 the next fork () event. */
915 else
917 pid_t child_pid = has_my_aix_child_reported (pid);
918 if (child_pid > 0)
920 ourstatus->set_forked (ptid_t (child_pid));
921 return ptid_t (pid);
923 aix_remember_parent (pid);
925 continue;
928 break;
931 /* AIX has a couple of strange returns from wait(). */
933 /* stop after load" status. */
934 if (status == 0x57c)
935 ourstatus->set_loaded ();
936 /* 0x7f is signal 0. */
937 else if (status == 0x7f)
938 ourstatus->set_spurious ();
939 /* A normal waitstatus. Let the usual macros deal with it. */
940 else
941 *ourstatus = host_status_to_waitstatus (status);
943 return ptid_t (pid);
947 /* Set the current architecture from the host running GDB. Called when
948 starting a child process. */
950 void
951 rs6000_nat_target::create_inferior (const char *exec_file,
952 const std::string &allargs,
953 char **env, int from_tty)
955 enum bfd_architecture arch;
956 unsigned long mach;
957 bfd abfd;
959 inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
961 if (__power_rs ())
963 arch = bfd_arch_rs6000;
964 mach = bfd_mach_rs6k;
966 else
968 arch = bfd_arch_powerpc;
969 mach = bfd_mach_ppc;
972 /* FIXME: schauer/2002-02-25:
973 We don't know if we are executing a 32 or 64 bit executable,
974 and have no way to pass the proper word size to rs6000_gdbarch_init.
975 So we have to avoid switching to a new architecture, if the architecture
976 matches already.
977 Blindly calling rs6000_gdbarch_init used to work in older versions of
978 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
979 determine the wordsize. */
980 if (current_program_space->exec_bfd ())
982 const struct bfd_arch_info *exec_bfd_arch_info;
984 exec_bfd_arch_info
985 = bfd_get_arch_info (current_program_space->exec_bfd ());
986 if (arch == exec_bfd_arch_info->arch)
987 return;
990 bfd_default_set_arch_mach (&abfd, arch, mach);
992 gdbarch_info info;
993 info.bfd_arch_info = bfd_get_arch_info (&abfd);
994 info.abfd = current_program_space->exec_bfd ();
996 if (!gdbarch_update_p (info))
997 internal_error (_("rs6000_create_inferior: failed "
998 "to select architecture"));
1002 /* Shared Object support. */
1004 /* Return the LdInfo data for the given process. Raises an error
1005 if the data could not be obtained. */
1007 static gdb::byte_vector
1008 rs6000_ptrace_ldinfo (ptid_t ptid)
1010 const int pid = ptid.pid ();
1011 gdb::byte_vector ldi (1024);
1012 int rc = -1;
1014 while (1)
1016 if (ARCH64 ())
1017 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi.data (),
1018 ldi.size (), NULL);
1019 else
1020 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi.data (),
1021 ldi.size (), NULL);
1023 if (rc != -1)
1024 break; /* Success, we got the entire ld_info data. */
1026 if (errno != ENOMEM)
1027 perror_with_name (_("ptrace ldinfo"));
1029 /* ldi is not big enough. Double it and try again. */
1030 ldi.resize (ldi.size () * 2);
1033 return ldi;
1036 /* Implement the to_xfer_partial target_ops method for
1037 TARGET_OBJECT_LIBRARIES_AIX objects. */
1039 enum target_xfer_status
1040 rs6000_nat_target::xfer_shared_libraries
1041 (enum target_object object,
1042 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
1043 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1045 ULONGEST result;
1047 /* This function assumes that it is being run with a live process.
1048 Core files are handled via gdbarch. */
1049 gdb_assert (target_has_execution ());
1051 if (writebuf)
1052 return TARGET_XFER_E_IO;
1054 gdb::byte_vector ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
1055 result = rs6000_aix_ld_info_to_xml (current_inferior ()->arch (),
1056 ldi_buf.data (),
1057 readbuf, offset, len, 1);
1059 if (result == 0)
1060 return TARGET_XFER_EOF;
1061 else
1063 *xfered_len = result;
1064 return TARGET_XFER_OK;
1068 void _initialize_rs6000_nat ();
1069 void
1070 _initialize_rs6000_nat ()
1072 add_inf_child_target (&the_rs6000_nat_target);