Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / rs6000-aix-nat.c
blob066d34d446c5fe1a9316cb6828891f91193c07e5
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 "defs.h"
21 #include "inferior.h"
22 #include "target.h"
23 #include "gdbcore.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "bfd.h"
27 #include "gdb-stabs.h"
28 #include "regcache.h"
29 #include "arch-utils.h"
30 #include "inf-child.h"
31 #include "inf-ptrace.h"
32 #include "ppc-tdep.h"
33 #include "rs6000-aix-tdep.h"
34 #include "exec.h"
35 #include "observable.h"
36 #include "xcoffread.h"
38 #include <sys/ptrace.h>
39 #include <sys/reg.h>
41 #include <sys/dir.h>
42 #include <sys/user.h>
43 #include <signal.h>
44 #include <sys/ioctl.h>
45 #include <fcntl.h>
47 #include <a.out.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include "gdb_bfd.h"
51 #include <sys/core.h>
52 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
53 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
54 #include <sys/ldr.h>
55 #include <sys/systemcfg.h>
57 /* Header files for getting ppid in AIX of a child process. */
58 #include <procinfo.h>
59 #include <sys/types.h>
61 /* Header files for alti-vec reg. */
62 #include <sys/context.h>
64 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
65 debugging 32-bit and 64-bit processes. Define a typedef and macros for
66 accessing fields in the appropriate structures. */
68 /* In 32-bit compilation mode (which is the only mode from which ptrace()
69 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
71 #if defined (__ld_info32) || defined (__ld_info64)
72 # define ARCH3264
73 #endif
75 /* Return whether the current architecture is 64-bit. */
77 #ifndef ARCH3264
78 # define ARCH64() 0
79 #else
80 # define ARCH64() (register_size (current_inferior ()->arch (), 0) == 8)
81 #endif
83 class rs6000_nat_target final : public inf_ptrace_target
85 public:
86 void fetch_registers (struct regcache *, int) override;
87 void store_registers (struct regcache *, int) override;
89 enum target_xfer_status xfer_partial (enum target_object object,
90 const char *annex,
91 gdb_byte *readbuf,
92 const gdb_byte *writebuf,
93 ULONGEST offset, ULONGEST len,
94 ULONGEST *xfered_len) override;
96 void create_inferior (const char *, const std::string &,
97 char **, int) override;
99 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
101 /* Fork detection related functions, For adding multi process debugging
102 support. */
103 void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
105 const struct target_desc *read_description () override;
107 int insert_fork_catchpoint (int) override;
108 int remove_fork_catchpoint (int) override;
110 protected:
112 void post_startup_inferior (ptid_t ptid) override;
114 private:
115 enum target_xfer_status
116 xfer_shared_libraries (enum target_object object,
117 const char *annex, gdb_byte *readbuf,
118 const gdb_byte *writebuf,
119 ULONGEST offset, ULONGEST len,
120 ULONGEST *xfered_len);
123 static rs6000_nat_target the_rs6000_nat_target;
125 /* The below declaration is to track number of times, parent has
126 reported fork event before its children. */
128 static std::list<pid_t> aix_pending_parent;
130 /* The below declaration is for a child process event that
131 is reported before its corresponding parent process in
132 the event of a fork (). */
134 static std::list<pid_t> aix_pending_children;
136 static void
137 aix_remember_child (pid_t pid)
139 aix_pending_children.push_front (pid);
142 static void
143 aix_remember_parent (pid_t pid)
145 aix_pending_parent.push_front (pid);
148 /* This function returns a parent of a child process. */
150 static pid_t
151 find_my_aix_parent (pid_t child_pid)
153 struct procsinfo ProcessBuffer1;
155 if (getprocs (&ProcessBuffer1, sizeof (ProcessBuffer1),
156 NULL, 0, &child_pid, 1) != 1)
157 return 0;
158 else
159 return ProcessBuffer1.pi_ppid;
162 /* In the below function we check if there was any child
163 process pending. If it exists we return it from the
164 list, otherwise we return a null. */
166 static pid_t
167 has_my_aix_child_reported (pid_t parent_pid)
169 pid_t child = 0;
170 auto it = std::find_if (aix_pending_children.begin (),
171 aix_pending_children.end (),
172 [=] (pid_t child_pid)
174 return find_my_aix_parent (child_pid) == parent_pid;
176 if (it != aix_pending_children.end ())
178 child = *it;
179 aix_pending_children.erase (it);
181 return child;
184 /* In the below function we check if there was any parent
185 process pending. If it exists we return it from the
186 list, otherwise we return a null. */
188 static pid_t
189 has_my_aix_parent_reported (pid_t child_pid)
191 pid_t my_parent = find_my_aix_parent (child_pid);
192 auto it = std::find (aix_pending_parent.begin (),
193 aix_pending_parent.end (),
194 my_parent);
195 if (it != aix_pending_parent.end ())
197 aix_pending_parent.erase (it);
198 return my_parent;
200 return 0;
203 /* Given REGNO, a gdb register number, return the corresponding
204 number suitable for use as a ptrace() parameter. Return -1 if
205 there's no suitable mapping. Also, set the int pointed to by
206 ISFLOAT to indicate whether REGNO is a floating point register. */
208 static int
209 regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
211 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
213 *isfloat = 0;
214 if (tdep->ppc_gp0_regnum <= regno
215 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
216 return regno;
217 else if (tdep->ppc_fp0_regnum >= 0
218 && tdep->ppc_fp0_regnum <= regno
219 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
221 *isfloat = 1;
222 return regno - tdep->ppc_fp0_regnum + FPR0;
224 else if (regno == gdbarch_pc_regnum (gdbarch))
225 return IAR;
226 else if (regno == tdep->ppc_ps_regnum)
227 return MSR;
228 else if (regno == tdep->ppc_cr_regnum)
229 return CR;
230 else if (regno == tdep->ppc_lr_regnum)
231 return LR;
232 else if (regno == tdep->ppc_ctr_regnum)
233 return CTR;
234 else if (regno == tdep->ppc_xer_regnum)
235 return XER;
236 else if (tdep->ppc_fpscr_regnum >= 0
237 && regno == tdep->ppc_fpscr_regnum)
238 return FPSCR;
239 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
240 return MQ;
241 else
242 return -1;
245 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
247 static int
248 rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
250 #ifdef HAVE_PTRACE64
251 int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
252 #else
253 int ret = ptrace (req, id, (int *)addr, data, buf);
254 #endif
255 #if 0
256 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
257 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
258 #endif
259 return ret;
262 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
264 static int
265 rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
267 #ifdef ARCH3264
268 # ifdef HAVE_PTRACE64
269 int ret = ptrace64 (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
270 # else
271 int ret = ptracex (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
272 # endif
273 #else
274 int ret = 0;
275 #endif
276 #if 0
277 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
278 req, id, hex_string (addr), data, (unsigned int)buf, ret);
279 #endif
280 return ret;
283 /* Store the vsx registers. */
285 static void
286 store_vsx_register_aix (struct regcache *regcache, int regno)
288 int ret;
289 struct gdbarch *gdbarch = regcache->arch ();
290 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
291 struct thrdentry64 thrdentry;
292 __vsx_context_t vsx;
293 pid_t pid = inferior_ptid.pid ();
294 tid64_t thrd_i = 0;
296 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
297 &thrd_i, 1) == 1)
298 thrd_i = thrdentry.ti_tid;
300 memset(&vsx, 0, sizeof(__vsx_context_t));
301 if (__power_vsx() && thrd_i > 0)
303 if (ARCH64 ())
304 ret = rs6000_ptrace64 (PTT_READ_VSX, thrd_i, (long long) &vsx, 0, 0);
305 else
306 ret = rs6000_ptrace32 (PTT_READ_VSX, thrd_i, (int *)&vsx, 0, 0);
307 if (ret < 0)
308 return;
310 regcache->raw_collect (regno, &(vsx.__vsr_dw1[0])+
311 regno - tdep->ppc_vsr0_upper_regnum);
313 if (ARCH64 ())
314 ret = rs6000_ptrace64 (PTT_WRITE_VSX, thrd_i, (long long) &vsx, 0, 0);
315 else
316 ret = rs6000_ptrace32 (PTT_WRITE_VSX, thrd_i, (int *) &vsx, 0, 0);
318 if (ret < 0)
319 perror_with_name (_("Unable to write VSX registers after reading it"));
323 /* Store Altivec registers. */
325 static void
326 store_altivec_register_aix (struct regcache *regcache, int regno)
328 int ret;
329 struct gdbarch *gdbarch = regcache->arch ();
330 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
331 struct thrdentry64 thrdentry;
332 __vmx_context_t vmx;
333 pid_t pid = inferior_ptid.pid ();
334 tid64_t thrd_i = 0;
336 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
337 &thrd_i, 1) == 1)
338 thrd_i = thrdentry.ti_tid;
340 memset(&vmx, 0, sizeof(__vmx_context_t));
341 if (__power_vmx() && thrd_i > 0)
343 if (ARCH64 ())
344 ret = rs6000_ptrace64 (PTT_READ_VEC, thrd_i, (long long) &vmx, 0, 0);
345 else
346 ret = rs6000_ptrace32 (PTT_READ_VEC, thrd_i, (int *) &vmx, 0, 0);
347 if (ret < 0)
348 return;
350 regcache->raw_collect (regno, &(vmx.__vr[0]) + regno
351 - tdep->ppc_vr0_regnum);
353 if (ARCH64 ())
354 ret = rs6000_ptrace64 (PTT_WRITE_VEC, thrd_i, (long long) &vmx, 0, 0);
355 else
356 ret = rs6000_ptrace32 (PTT_WRITE_VEC, thrd_i, (int *) &vmx, 0, 0);
357 if (ret < 0)
358 perror_with_name (_("Unable to store AltiVec register after reading it"));
362 /* Supply altivec registers. */
364 static void
365 supply_vrregset_aix (struct regcache *regcache, __vmx_context_t *vmx)
367 int i;
368 struct gdbarch *gdbarch = regcache->arch ();
369 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
370 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
372 for (i = 0; i < num_of_vrregs; i++)
373 regcache->raw_supply (tdep->ppc_vr0_regnum + i,
374 &(vmx->__vr[i]));
375 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx->__vrsave));
376 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx->__vscr));
379 /* Fetch altivec register. */
381 static void
382 fetch_altivec_registers_aix (struct regcache *regcache)
384 struct thrdentry64 thrdentry;
385 __vmx_context_t vmx;
386 pid_t pid = current_inferior ()->pid;
387 tid64_t thrd_i = 0;
389 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
390 &thrd_i, 1) == 1)
391 thrd_i = thrdentry.ti_tid;
393 memset(&vmx, 0, sizeof(__vmx_context_t));
394 if (__power_vmx() && thrd_i > 0)
396 if (ARCH64 ())
397 rs6000_ptrace64 (PTT_READ_VEC, thrd_i, (long long) &vmx, 0, 0);
398 else
399 rs6000_ptrace32 (PTT_READ_VEC, thrd_i, (int *) &vmx, 0, 0);
400 supply_vrregset_aix (regcache, &vmx);
404 /* supply vsx register. */
406 static void
407 supply_vsxregset_aix (struct regcache *regcache, __vsx_context_t *vsx)
409 int i;
410 struct gdbarch *gdbarch = regcache->arch ();
411 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
413 for (i = 0; i < ppc_num_vshrs; i++)
414 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + i,
415 &(vsx->__vsr_dw1[i]));
418 /* Fetch vsx registers. */
419 static void
420 fetch_vsx_registers_aix (struct regcache *regcache)
422 struct thrdentry64 thrdentry;
423 __vsx_context_t vsx;
424 pid_t pid = current_inferior ()->pid;
425 tid64_t thrd_i = 0;
427 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
428 &thrd_i, 1) == 1)
429 thrd_i = thrdentry.ti_tid;
431 memset(&vsx, 0, sizeof(__vsx_context_t));
432 if (__power_vsx() && thrd_i > 0)
434 if (ARCH64 ())
435 rs6000_ptrace64 (PTT_READ_VSX, thrd_i, (long long) &vsx, 0, 0);
436 else
437 rs6000_ptrace32 (PTT_READ_VSX, thrd_i, (int *) &vsx, 0, 0);
438 supply_vsxregset_aix (regcache, &vsx);
442 void rs6000_nat_target::post_startup_inferior (ptid_t ptid)
445 /* In AIX to turn on multi process debugging in ptrace
446 PT_MULTI is the option to be passed,
447 with the process ID which can fork () and
448 the data parameter [fourth parameter] must be 1. */
450 if (!ARCH64 ())
451 rs6000_ptrace32 (PT_MULTI, ptid.pid(), 0, 1, 0);
452 else
453 rs6000_ptrace64 (PT_MULTI, ptid.pid(), 0, 1, 0);
456 void
457 rs6000_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
458 target_waitkind fork_kind, bool follow_child,
459 bool detach_fork)
462 /* Once the fork event is detected the infrun.c code
463 calls the target_follow_fork to take care of
464 follow child and detach the child activity which is
465 done using the function below. */
467 inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
468 follow_child, detach_fork);
470 /* If we detach fork and follow child we do not want the child
471 process to generate events that ptrace can trace. Hence we
472 detach it. */
474 if (detach_fork && !follow_child)
476 if (ARCH64 ())
477 rs6000_ptrace64 (PT_DETACH, child_ptid.pid (), 0, 0, 0);
478 else
479 rs6000_ptrace32 (PT_DETACH, child_ptid.pid (), 0, 0, 0);
483 /* Functions for catchpoint in AIX. */
485 rs6000_nat_target::insert_fork_catchpoint (int pid)
487 return 0;
491 rs6000_nat_target::remove_fork_catchpoint (int pid)
493 return 0;
496 /* Fetch register REGNO from the inferior. */
498 static void
499 fetch_register (struct regcache *regcache, int regno)
501 struct gdbarch *gdbarch = regcache->arch ();
502 int addr[PPC_MAX_REGISTER_SIZE];
503 int nr, isfloat;
504 pid_t pid = regcache->ptid ().pid ();
506 /* Retrieved values may be -1, so infer errors from errno. */
507 errno = 0;
509 /* Alti-vec register. */
510 if (altivec_register_p (gdbarch, regno))
512 fetch_altivec_registers_aix (regcache);
513 return;
516 /* VSX register. */
517 if (vsx_register_p (gdbarch, regno))
519 fetch_vsx_registers_aix (regcache);
520 return;
523 nr = regmap (gdbarch, regno, &isfloat);
525 /* Floating-point registers. */
526 if (isfloat)
527 rs6000_ptrace32 (PT_READ_FPR, pid, addr, nr, 0);
529 /* Bogus register number. */
530 else if (nr < 0)
532 if (regno >= gdbarch_num_regs (gdbarch))
533 gdb_printf (gdb_stderr,
534 "gdb error: register no %d not implemented.\n",
535 regno);
536 return;
539 /* Fixed-point registers. */
540 else
542 if (!ARCH64 ())
543 *addr = rs6000_ptrace32 (PT_READ_GPR, pid, (int *) nr, 0, 0);
544 else
546 /* PT_READ_GPR requires the buffer parameter to point to long long,
547 even if the register is really only 32 bits. */
548 long long buf;
549 rs6000_ptrace64 (PT_READ_GPR, pid, nr, 0, &buf);
550 if (register_size (gdbarch, regno) == 8)
551 memcpy (addr, &buf, 8);
552 else
553 *addr = buf;
557 if (!errno)
558 regcache->raw_supply (regno, (char *) addr);
559 else
561 #if 0
562 /* FIXME: this happens 3 times at the start of each 64-bit program. */
563 perror (_("ptrace read"));
564 #endif
565 errno = 0;
569 /* Store register REGNO back into the inferior. */
571 static void
572 store_register (struct regcache *regcache, int regno)
574 struct gdbarch *gdbarch = regcache->arch ();
575 int addr[PPC_MAX_REGISTER_SIZE];
576 int nr, isfloat;
577 pid_t pid = regcache->ptid ().pid ();
579 /* Fetch the register's value from the register cache. */
580 regcache->raw_collect (regno, addr);
582 /* -1 can be a successful return value, so infer errors from errno. */
583 errno = 0;
585 if (altivec_register_p (gdbarch, regno))
587 store_altivec_register_aix (regcache, regno);
588 return;
591 if (vsx_register_p (gdbarch, regno))
593 store_vsx_register_aix (regcache, regno);
594 return;
597 nr = regmap (gdbarch, regno, &isfloat);
599 /* Floating-point registers. */
600 if (isfloat)
601 rs6000_ptrace32 (PT_WRITE_FPR, pid, addr, nr, 0);
603 /* Bogus register number. */
604 else if (nr < 0)
606 if (regno >= gdbarch_num_regs (gdbarch))
607 gdb_printf (gdb_stderr,
608 "gdb error: register no %d not implemented.\n",
609 regno);
612 /* Fixed-point registers. */
613 else
615 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
616 the register's value is passed by value, but for 64-bit inferiors,
617 the address of a buffer containing the value is passed. */
618 if (!ARCH64 ())
619 rs6000_ptrace32 (PT_WRITE_GPR, pid, (int *) nr, *addr, 0);
620 else
622 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
623 area, even if the register is really only 32 bits. */
624 long long buf;
625 if (register_size (gdbarch, regno) == 8)
626 memcpy (&buf, addr, 8);
627 else
628 buf = *addr;
629 rs6000_ptrace64 (PT_WRITE_GPR, pid, nr, 0, &buf);
633 if (errno)
635 perror (_("ptrace write"));
636 errno = 0;
640 /* Read from the inferior all registers if REGNO == -1 and just register
641 REGNO otherwise. */
643 void
644 rs6000_nat_target::fetch_registers (struct regcache *regcache, int regno)
646 struct gdbarch *gdbarch = regcache->arch ();
647 if (regno != -1)
648 fetch_register (regcache, regno);
650 else
652 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
654 /* Read 32 general purpose registers. */
655 for (regno = tdep->ppc_gp0_regnum;
656 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
657 regno++)
659 fetch_register (regcache, regno);
662 /* Read general purpose floating point registers. */
663 if (tdep->ppc_fp0_regnum >= 0)
664 for (regno = 0; regno < ppc_num_fprs; regno++)
665 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
667 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
668 fetch_altivec_registers_aix (regcache);
670 if (tdep->ppc_vsr0_upper_regnum != -1)
671 fetch_vsx_registers_aix (regcache);
673 /* Read special registers. */
674 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
675 fetch_register (regcache, tdep->ppc_ps_regnum);
676 fetch_register (regcache, tdep->ppc_cr_regnum);
677 fetch_register (regcache, tdep->ppc_lr_regnum);
678 fetch_register (regcache, tdep->ppc_ctr_regnum);
679 fetch_register (regcache, tdep->ppc_xer_regnum);
680 if (tdep->ppc_fpscr_regnum >= 0)
681 fetch_register (regcache, tdep->ppc_fpscr_regnum);
682 if (tdep->ppc_mq_regnum >= 0)
683 fetch_register (regcache, tdep->ppc_mq_regnum);
687 const struct target_desc *
688 rs6000_nat_target::read_description ()
690 if (ARCH64())
692 if (__power_vsx ())
693 return tdesc_powerpc_vsx64;
694 else if (__power_vmx ())
695 return tdesc_powerpc_altivec64;
697 else
699 if (__power_vsx ())
700 return tdesc_powerpc_vsx32;
701 else if (__power_vmx ())
702 return tdesc_powerpc_altivec32;
704 return NULL;
707 /* Store our register values back into the inferior.
708 If REGNO is -1, do this for all registers.
709 Otherwise, REGNO specifies which register (so we can save time). */
711 void
712 rs6000_nat_target::store_registers (struct regcache *regcache, int regno)
714 struct gdbarch *gdbarch = regcache->arch ();
715 if (regno != -1)
716 store_register (regcache, regno);
718 else
720 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
722 /* Write general purpose registers first. */
723 for (regno = tdep->ppc_gp0_regnum;
724 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
725 regno++)
727 store_register (regcache, regno);
730 /* Write floating point registers. */
731 if (tdep->ppc_fp0_regnum >= 0)
732 for (regno = 0; regno < ppc_num_fprs; regno++)
733 store_register (regcache, tdep->ppc_fp0_regnum + regno);
735 /* Write special registers. */
736 store_register (regcache, gdbarch_pc_regnum (gdbarch));
737 store_register (regcache, tdep->ppc_ps_regnum);
738 store_register (regcache, tdep->ppc_cr_regnum);
739 store_register (regcache, tdep->ppc_lr_regnum);
740 store_register (regcache, tdep->ppc_ctr_regnum);
741 store_register (regcache, tdep->ppc_xer_regnum);
742 if (tdep->ppc_fpscr_regnum >= 0)
743 store_register (regcache, tdep->ppc_fpscr_regnum);
744 if (tdep->ppc_mq_regnum >= 0)
745 store_register (regcache, tdep->ppc_mq_regnum);
749 /* Implement the to_xfer_partial target_ops method. */
751 enum target_xfer_status
752 rs6000_nat_target::xfer_partial (enum target_object object,
753 const char *annex, gdb_byte *readbuf,
754 const gdb_byte *writebuf,
755 ULONGEST offset, ULONGEST len,
756 ULONGEST *xfered_len)
758 pid_t pid = inferior_ptid.pid ();
759 int arch64 = ARCH64 ();
761 switch (object)
763 case TARGET_OBJECT_LIBRARIES_AIX:
764 return xfer_shared_libraries (object, annex,
765 readbuf, writebuf,
766 offset, len, xfered_len);
767 case TARGET_OBJECT_MEMORY:
769 union
771 PTRACE_TYPE_RET word;
772 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
773 } buffer;
774 ULONGEST rounded_offset;
775 LONGEST partial_len;
777 /* Round the start offset down to the next long word
778 boundary. */
779 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
781 /* Since ptrace will transfer a single word starting at that
782 rounded_offset the partial_len needs to be adjusted down to
783 that (remember this function only does a single transfer).
784 Should the required length be even less, adjust it down
785 again. */
786 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
787 if (partial_len > len)
788 partial_len = len;
790 if (writebuf)
792 /* If OFFSET:PARTIAL_LEN is smaller than
793 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
794 be needed. Read in the entire word. */
795 if (rounded_offset < offset
796 || (offset + partial_len
797 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
799 /* Need part of initial word -- fetch it. */
800 if (arch64)
801 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
802 rounded_offset, 0, NULL);
803 else
804 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
805 (int *) (uintptr_t)
806 rounded_offset,
807 0, NULL);
810 /* Copy data to be written over corresponding part of
811 buffer. */
812 memcpy (buffer.byte + (offset - rounded_offset),
813 writebuf, partial_len);
815 errno = 0;
816 if (arch64)
817 rs6000_ptrace64 (PT_WRITE_D, pid,
818 rounded_offset, buffer.word, NULL);
819 else
820 rs6000_ptrace32 (PT_WRITE_D, pid,
821 (int *) (uintptr_t) rounded_offset,
822 buffer.word, NULL);
823 if (errno)
824 return TARGET_XFER_EOF;
827 if (readbuf)
829 errno = 0;
830 if (arch64)
831 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
832 rounded_offset, 0, NULL);
833 else
834 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
835 (int *)(uintptr_t)rounded_offset,
836 0, NULL);
837 if (errno)
838 return TARGET_XFER_EOF;
840 /* Copy appropriate bytes out of the buffer. */
841 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
842 partial_len);
845 *xfered_len = (ULONGEST) partial_len;
846 return TARGET_XFER_OK;
849 default:
850 return TARGET_XFER_E_IO;
854 /* Wait for the child specified by PTID to do something. Return the
855 process ID of the child, or MINUS_ONE_PTID in case of error; store
856 the status in *OURSTATUS. */
858 ptid_t
859 rs6000_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
860 target_wait_flags options)
862 pid_t pid;
863 int status, save_errno;
865 while (1)
867 set_sigint_trap ();
871 pid = waitpid (ptid.pid (), &status, 0);
872 save_errno = errno;
874 while (pid == -1 && errno == EINTR);
876 clear_sigint_trap ();
878 if (pid == -1)
880 gdb_printf (gdb_stderr,
881 _("Child process unexpectedly missing: %s.\n"),
882 safe_strerror (save_errno));
884 ourstatus->set_ignore ();
885 return minus_one_ptid;
888 /* Ignore terminated detached child processes. */
889 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
890 continue;
892 /* Check for a fork () event. */
893 if ((status & 0xff) == W_SFWTED)
895 /* Checking whether it is a parent or a child event. */
897 /* If the event is a child we check if there was a parent
898 event recorded before. If yes we got the parent child
899 relationship. If not we push this child and wait for
900 the next fork () event. */
901 if (find_inferior_pid (this, pid) == nullptr)
903 pid_t parent_pid = has_my_aix_parent_reported (pid);
904 if (parent_pid > 0)
906 ourstatus->set_forked (ptid_t (pid));
907 return ptid_t (parent_pid);
909 aix_remember_child (pid);
912 /* If the event is a parent we check if there was a child
913 event recorded before. If yes we got the parent child
914 relationship. If not we push this parent and wait for
915 the next fork () event. */
916 else
918 pid_t child_pid = has_my_aix_child_reported (pid);
919 if (child_pid > 0)
921 ourstatus->set_forked (ptid_t (child_pid));
922 return ptid_t (pid);
924 aix_remember_parent (pid);
926 continue;
929 break;
932 /* AIX has a couple of strange returns from wait(). */
934 /* stop after load" status. */
935 if (status == 0x57c)
936 ourstatus->set_loaded ();
937 /* 0x7f is signal 0. */
938 else if (status == 0x7f)
939 ourstatus->set_spurious ();
940 /* A normal waitstatus. Let the usual macros deal with it. */
941 else
942 *ourstatus = host_status_to_waitstatus (status);
944 return ptid_t (pid);
948 /* Set the current architecture from the host running GDB. Called when
949 starting a child process. */
951 void
952 rs6000_nat_target::create_inferior (const char *exec_file,
953 const std::string &allargs,
954 char **env, int from_tty)
956 enum bfd_architecture arch;
957 unsigned long mach;
958 bfd abfd;
960 inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
962 if (__power_rs ())
964 arch = bfd_arch_rs6000;
965 mach = bfd_mach_rs6k;
967 else
969 arch = bfd_arch_powerpc;
970 mach = bfd_mach_ppc;
973 /* FIXME: schauer/2002-02-25:
974 We don't know if we are executing a 32 or 64 bit executable,
975 and have no way to pass the proper word size to rs6000_gdbarch_init.
976 So we have to avoid switching to a new architecture, if the architecture
977 matches already.
978 Blindly calling rs6000_gdbarch_init used to work in older versions of
979 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
980 determine the wordsize. */
981 if (current_program_space->exec_bfd ())
983 const struct bfd_arch_info *exec_bfd_arch_info;
985 exec_bfd_arch_info
986 = bfd_get_arch_info (current_program_space->exec_bfd ());
987 if (arch == exec_bfd_arch_info->arch)
988 return;
991 bfd_default_set_arch_mach (&abfd, arch, mach);
993 gdbarch_info info;
994 info.bfd_arch_info = bfd_get_arch_info (&abfd);
995 info.abfd = current_program_space->exec_bfd ();
997 if (!gdbarch_update_p (info))
998 internal_error (_("rs6000_create_inferior: failed "
999 "to select architecture"));
1003 /* Shared Object support. */
1005 /* Return the LdInfo data for the given process. Raises an error
1006 if the data could not be obtained. */
1008 static gdb::byte_vector
1009 rs6000_ptrace_ldinfo (ptid_t ptid)
1011 const int pid = ptid.pid ();
1012 gdb::byte_vector ldi (1024);
1013 int rc = -1;
1015 while (1)
1017 if (ARCH64 ())
1018 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi.data (),
1019 ldi.size (), NULL);
1020 else
1021 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi.data (),
1022 ldi.size (), NULL);
1024 if (rc != -1)
1025 break; /* Success, we got the entire ld_info data. */
1027 if (errno != ENOMEM)
1028 perror_with_name (_("ptrace ldinfo"));
1030 /* ldi is not big enough. Double it and try again. */
1031 ldi.resize (ldi.size () * 2);
1034 return ldi;
1037 /* Implement the to_xfer_partial target_ops method for
1038 TARGET_OBJECT_LIBRARIES_AIX objects. */
1040 enum target_xfer_status
1041 rs6000_nat_target::xfer_shared_libraries
1042 (enum target_object object,
1043 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
1044 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1046 ULONGEST result;
1048 /* This function assumes that it is being run with a live process.
1049 Core files are handled via gdbarch. */
1050 gdb_assert (target_has_execution ());
1052 if (writebuf)
1053 return TARGET_XFER_E_IO;
1055 gdb::byte_vector ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
1056 result = rs6000_aix_ld_info_to_xml (current_inferior ()->arch (),
1057 ldi_buf.data (),
1058 readbuf, offset, len, 1);
1060 if (result == 0)
1061 return TARGET_XFER_EOF;
1062 else
1064 *xfered_len = result;
1065 return TARGET_XFER_OK;
1069 void _initialize_rs6000_nat ();
1070 void
1071 _initialize_rs6000_nat ()
1073 add_inf_child_target (&the_rs6000_nat_target);