xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/rs6000-aix-nat.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2023 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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/>.  */
19 
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"
37 
38 #include <sys/ptrace.h>
39 #include <sys/reg.h>
40 
41 #include <sys/dir.h>
42 #include <sys/user.h>
43 #include <signal.h>
44 #include <sys/ioctl.h>
45 #include <fcntl.h>
46 
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>
56 
57 /* Header files for getting ppid in AIX of a child process.  */
58 #include <procinfo.h>
59 #include <sys/types.h>
60 
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.  */
64 
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.  */
67 
68 #if defined (__ld_info32) || defined (__ld_info64)
69 # define ARCH3264
70 #endif
71 
72 /* Return whether the current architecture is 64-bit.  */
73 
74 #ifndef ARCH3264
75 # define ARCH64() 0
76 #else
77 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
78 #endif
79 
80 class rs6000_nat_target final : public inf_ptrace_target
81 {
82 public:
83   void fetch_registers (struct regcache *, int) override;
84   void store_registers (struct regcache *, int) override;
85 
86   enum target_xfer_status xfer_partial (enum target_object object,
87 					const char *annex,
88 					gdb_byte *readbuf,
89 					const gdb_byte *writebuf,
90 					ULONGEST offset, ULONGEST len,
91 					ULONGEST *xfered_len) override;
92 
93   void create_inferior (const char *, const std::string &,
94 			char **, int) override;
95 
96   ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
97 
98   /* Fork detection related functions, For adding multi process debugging
99      support.  */
100   void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
101 
102 protected:
103 
104   void post_startup_inferior (ptid_t ptid) override;
105 
106 private:
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);
113 };
114 
115 static rs6000_nat_target the_rs6000_nat_target;
116 
117 /* The below declaration is to track number of times, parent has
118    reported fork event before its children.  */
119 
120 static std::list<pid_t> aix_pending_parent;
121 
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 ().  */
125 
126 static std::list<pid_t> aix_pending_children;
127 
128 static void
129 aix_remember_child (pid_t pid)
130 {
131   aix_pending_children.push_front (pid);
132 }
133 
134 static void
135 aix_remember_parent (pid_t pid)
136 {
137   aix_pending_parent.push_front (pid);
138 }
139 
140 /* This function returns a parent of a child process.  */
141 
142 static pid_t
143 find_my_aix_parent (pid_t child_pid)
144 {
145   struct procsinfo ProcessBuffer1;
146 
147   if (getprocs (&ProcessBuffer1, sizeof (ProcessBuffer1),
148 		NULL, 0, &child_pid, 1) != 1)
149     return 0;
150   else
151     return ProcessBuffer1.pi_ppid;
152 }
153 
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.  */
157 
158 static pid_t
159 has_my_aix_child_reported (pid_t parent_pid)
160 {
161   pid_t child = 0;
162   auto it = std::find_if (aix_pending_children.begin (),
163 			  aix_pending_children.end (),
164 			  [=] (pid_t child_pid)
165 			  {
166 			    return find_my_aix_parent (child_pid) == parent_pid;
167 			  });
168   if (it != aix_pending_children.end ())
169     {
170       child = *it;
171       aix_pending_children.erase (it);
172     }
173   return child;
174 }
175 
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.  */
179 
180 static pid_t
181 has_my_aix_parent_reported (pid_t child_pid)
182 {
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 (),
186 		       my_parent);
187   if (it != aix_pending_parent.end ())
188     {
189       aix_pending_parent.erase (it);
190       return my_parent;
191     }
192   return 0;
193 }
194 
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.  */
199 
200 static int
201 regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
202 {
203   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
204 
205   *isfloat = 0;
206   if (tdep->ppc_gp0_regnum <= regno
207       && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
208     return regno;
209   else if (tdep->ppc_fp0_regnum >= 0
210 	   && tdep->ppc_fp0_regnum <= regno
211 	   && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
212     {
213       *isfloat = 1;
214       return regno - tdep->ppc_fp0_regnum + FPR0;
215     }
216   else if (regno == gdbarch_pc_regnum (gdbarch))
217     return IAR;
218   else if (regno == tdep->ppc_ps_regnum)
219     return MSR;
220   else if (regno == tdep->ppc_cr_regnum)
221     return CR;
222   else if (regno == tdep->ppc_lr_regnum)
223     return LR;
224   else if (regno == tdep->ppc_ctr_regnum)
225     return CTR;
226   else if (regno == tdep->ppc_xer_regnum)
227     return XER;
228   else if (tdep->ppc_fpscr_regnum >= 0
229 	   && regno == tdep->ppc_fpscr_regnum)
230     return FPSCR;
231   else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
232     return MQ;
233   else
234     return -1;
235 }
236 
237 /* Call ptrace(REQ, ID, ADDR, DATA, BUF).  */
238 
239 static int
240 rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
241 {
242 #ifdef HAVE_PTRACE64
243   int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
244 #else
245   int ret = ptrace (req, id, (int *)addr, data, buf);
246 #endif
247 #if 0
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);
250 #endif
251   return ret;
252 }
253 
254 /* Call ptracex(REQ, ID, ADDR, DATA, BUF).  */
255 
256 static int
257 rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
258 {
259 #ifdef ARCH3264
260 #  ifdef HAVE_PTRACE64
261   int ret = ptrace64 (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
262 #  else
263   int ret = ptracex (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
264 #  endif
265 #else
266   int ret = 0;
267 #endif
268 #if 0
269   printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
270 	  req, id, hex_string (addr), data, (unsigned int)buf, ret);
271 #endif
272   return ret;
273 }
274 
275 void rs6000_nat_target::post_startup_inferior (ptid_t ptid)
276 {
277 
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.  */
282 
283   if (!ARCH64 ())
284     rs6000_ptrace32 (PT_MULTI, ptid.pid(), 0, 1, 0);
285   else
286     rs6000_ptrace64 (PT_MULTI, ptid.pid(), 0, 1, 0);
287 }
288 
289 void
290 rs6000_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
291 				target_waitkind fork_kind, bool follow_child,
292 				bool detach_fork)
293 {
294 
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.  */
299 
300   inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
301 				  follow_child, detach_fork);
302 
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
305      detach it.  */
306 
307   if (detach_fork && !follow_child)
308   {
309     if (ARCH64 ())
310       rs6000_ptrace64 (PT_DETACH, child_ptid.pid (), 0, 0, 0);
311     else
312       rs6000_ptrace32 (PT_DETACH, child_ptid.pid (), 0, 0, 0);
313   }
314 }
315 
316 /* Fetch register REGNO from the inferior.  */
317 
318 static void
319 fetch_register (struct regcache *regcache, int regno)
320 {
321   struct gdbarch *gdbarch = regcache->arch ();
322   int addr[PPC_MAX_REGISTER_SIZE];
323   int nr, isfloat;
324   pid_t pid = regcache->ptid ().pid ();
325 
326   /* Retrieved values may be -1, so infer errors from errno.  */
327   errno = 0;
328 
329   nr = regmap (gdbarch, regno, &isfloat);
330 
331   /* Floating-point registers.  */
332   if (isfloat)
333     rs6000_ptrace32 (PT_READ_FPR, pid, addr, nr, 0);
334 
335   /* Bogus register number.  */
336   else if (nr < 0)
337     {
338       if (regno >= gdbarch_num_regs (gdbarch))
339 	gdb_printf (gdb_stderr,
340 		    "gdb error: register no %d not implemented.\n",
341 		    regno);
342       return;
343     }
344 
345   /* Fixed-point registers.  */
346   else
347     {
348       if (!ARCH64 ())
349 	*addr = rs6000_ptrace32 (PT_READ_GPR, pid, (int *) nr, 0, 0);
350       else
351 	{
352 	  /* PT_READ_GPR requires the buffer parameter to point to long long,
353 	     even if the register is really only 32 bits.  */
354 	  long long buf;
355 	  rs6000_ptrace64 (PT_READ_GPR, pid, nr, 0, &buf);
356 	  if (register_size (gdbarch, regno) == 8)
357 	    memcpy (addr, &buf, 8);
358 	  else
359 	    *addr = buf;
360 	}
361     }
362 
363   if (!errno)
364     regcache->raw_supply (regno, (char *) addr);
365   else
366     {
367 #if 0
368       /* FIXME: this happens 3 times at the start of each 64-bit program.  */
369       perror (_("ptrace read"));
370 #endif
371       errno = 0;
372     }
373 }
374 
375 /* Store register REGNO back into the inferior.  */
376 
377 static void
378 store_register (struct regcache *regcache, int regno)
379 {
380   struct gdbarch *gdbarch = regcache->arch ();
381   int addr[PPC_MAX_REGISTER_SIZE];
382   int nr, isfloat;
383   pid_t pid = regcache->ptid ().pid ();
384 
385   /* Fetch the register's value from the register cache.  */
386   regcache->raw_collect (regno, addr);
387 
388   /* -1 can be a successful return value, so infer errors from errno.  */
389   errno = 0;
390 
391   nr = regmap (gdbarch, regno, &isfloat);
392 
393   /* Floating-point registers.  */
394   if (isfloat)
395     rs6000_ptrace32 (PT_WRITE_FPR, pid, addr, nr, 0);
396 
397   /* Bogus register number.  */
398   else if (nr < 0)
399     {
400       if (regno >= gdbarch_num_regs (gdbarch))
401 	gdb_printf (gdb_stderr,
402 		    "gdb error: register no %d not implemented.\n",
403 		    regno);
404     }
405 
406   /* Fixed-point registers.  */
407   else
408     {
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.  */
412       if (!ARCH64 ())
413 	rs6000_ptrace32 (PT_WRITE_GPR, pid, (int *) nr, *addr, 0);
414       else
415 	{
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.  */
418 	  long long buf;
419 	  if (register_size (gdbarch, regno) == 8)
420 	    memcpy (&buf, addr, 8);
421 	  else
422 	    buf = *addr;
423 	  rs6000_ptrace64 (PT_WRITE_GPR, pid, nr, 0, &buf);
424 	}
425     }
426 
427   if (errno)
428     {
429       perror (_("ptrace write"));
430       errno = 0;
431     }
432 }
433 
434 /* Read from the inferior all registers if REGNO == -1 and just register
435    REGNO otherwise.  */
436 
437 void
438 rs6000_nat_target::fetch_registers (struct regcache *regcache, int regno)
439 {
440   struct gdbarch *gdbarch = regcache->arch ();
441   if (regno != -1)
442     fetch_register (regcache, regno);
443 
444   else
445     {
446       ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
447 
448       /* Read 32 general purpose registers.  */
449       for (regno = tdep->ppc_gp0_regnum;
450 	   regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
451 	   regno++)
452 	{
453 	  fetch_register (regcache, regno);
454 	}
455 
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);
460 
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);
472     }
473 }
474 
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).  */
478 
479 void
480 rs6000_nat_target::store_registers (struct regcache *regcache, int regno)
481 {
482   struct gdbarch *gdbarch = regcache->arch ();
483   if (regno != -1)
484     store_register (regcache, regno);
485 
486   else
487     {
488       ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
489 
490       /* Write general purpose registers first.  */
491       for (regno = tdep->ppc_gp0_regnum;
492 	   regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
493 	   regno++)
494 	{
495 	  store_register (regcache, regno);
496 	}
497 
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);
502 
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);
514     }
515 }
516 
517 /* Implement the to_xfer_partial target_ops method.  */
518 
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)
525 {
526   pid_t pid = inferior_ptid.pid ();
527   int arch64 = ARCH64 ();
528 
529   switch (object)
530     {
531     case TARGET_OBJECT_LIBRARIES_AIX:
532       return xfer_shared_libraries (object, annex,
533 				    readbuf, writebuf,
534 				    offset, len, xfered_len);
535     case TARGET_OBJECT_MEMORY:
536       {
537 	union
538 	{
539 	  PTRACE_TYPE_RET word;
540 	  gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
541 	} buffer;
542 	ULONGEST rounded_offset;
543 	LONGEST partial_len;
544 
545 	/* Round the start offset down to the next long word
546 	   boundary.  */
547 	rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
548 
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
553 	   again.  */
554 	partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
555 	if (partial_len > len)
556 	  partial_len = len;
557 
558 	if (writebuf)
559 	  {
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)))
566 	      {
567 		/* Need part of initial word -- fetch it.  */
568 		if (arch64)
569 		  buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
570 						 rounded_offset, 0, NULL);
571 		else
572 		  buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
573 						 (int *) (uintptr_t)
574 						 rounded_offset,
575 						 0, NULL);
576 	      }
577 
578 	    /* Copy data to be written over corresponding part of
579 	       buffer.  */
580 	    memcpy (buffer.byte + (offset - rounded_offset),
581 		    writebuf, partial_len);
582 
583 	    errno = 0;
584 	    if (arch64)
585 	      rs6000_ptrace64 (PT_WRITE_D, pid,
586 			       rounded_offset, buffer.word, NULL);
587 	    else
588 	      rs6000_ptrace32 (PT_WRITE_D, pid,
589 			       (int *) (uintptr_t) rounded_offset,
590 			       buffer.word, NULL);
591 	    if (errno)
592 	      return TARGET_XFER_EOF;
593 	  }
594 
595 	if (readbuf)
596 	  {
597 	    errno = 0;
598 	    if (arch64)
599 	      buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
600 					     rounded_offset, 0, NULL);
601 	    else
602 	      buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
603 					     (int *)(uintptr_t)rounded_offset,
604 					     0, NULL);
605 	    if (errno)
606 	      return TARGET_XFER_EOF;
607 
608 	    /* Copy appropriate bytes out of the buffer.  */
609 	    memcpy (readbuf, buffer.byte + (offset - rounded_offset),
610 		    partial_len);
611 	  }
612 
613 	*xfered_len = (ULONGEST) partial_len;
614 	return TARGET_XFER_OK;
615       }
616 
617     default:
618       return TARGET_XFER_E_IO;
619     }
620 }
621 
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.  */
625 
626 ptid_t
627 rs6000_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
628 			 target_wait_flags options)
629 {
630   pid_t pid;
631   int status, save_errno;
632 
633   while (1)
634     {
635       set_sigint_trap ();
636 
637       do
638 	{
639 	  pid = waitpid (ptid.pid (), &status, 0);
640 	  save_errno = errno;
641 	}
642       while (pid == -1 && errno == EINTR);
643 
644       clear_sigint_trap ();
645 
646       if (pid == -1)
647 	{
648 	  gdb_printf (gdb_stderr,
649 		      _("Child process unexpectedly missing: %s.\n"),
650 		      safe_strerror (save_errno));
651 
652 	  ourstatus->set_ignore ();
653 	  return minus_one_ptid;
654 	}
655 
656       /* Ignore terminated detached child processes.  */
657       if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
658 	continue;
659 
660       /* Check for a fork () event.  */
661       if ((status & 0xff) == W_SFWTED)
662 	{
663 	  /* Checking whether it is a parent or a child event.  */
664 
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)
670 	    {
671 	      pid_t parent_pid = has_my_aix_parent_reported (pid);
672 	      if (parent_pid > 0)
673 		{
674 		  ourstatus->set_forked (ptid_t (pid));
675 		  return ptid_t (parent_pid);
676 		}
677 	      aix_remember_child (pid);
678 	    }
679 
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.  */
684 	  else
685 	    {
686 	      pid_t child_pid = has_my_aix_child_reported (pid);
687 	      if (child_pid > 0)
688 		{
689 		  ourstatus->set_forked (ptid_t (child_pid));
690 		  return ptid_t (pid);
691 		}
692 	      aix_remember_parent (pid);
693 	    }
694 	  continue;
695 	}
696 
697       break;
698     }
699 
700   /* AIX has a couple of strange returns from wait().  */
701 
702   /* stop after load" status.  */
703   if (status == 0x57c)
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.  */
712   else
713     *ourstatus = host_status_to_waitstatus (status);
714 
715   return ptid_t (pid);
716 }
717 
718 
719 /* Set the current architecture from the host running GDB.  Called when
720    starting a child process.  */
721 
722 void
723 rs6000_nat_target::create_inferior (const char *exec_file,
724 				    const std::string &allargs,
725 				    char **env, int from_tty)
726 {
727   enum bfd_architecture arch;
728   unsigned long mach;
729   bfd abfd;
730 
731   inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
732 
733   if (__power_rs ())
734     {
735       arch = bfd_arch_rs6000;
736       mach = bfd_mach_rs6k;
737     }
738   else
739     {
740       arch = bfd_arch_powerpc;
741       mach = bfd_mach_ppc;
742     }
743 
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
748      matches already.
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 ())
753     {
754       const struct bfd_arch_info *exec_bfd_arch_info;
755 
756       exec_bfd_arch_info
757 	= bfd_get_arch_info (current_program_space->exec_bfd ());
758       if (arch == exec_bfd_arch_info->arch)
759 	return;
760     }
761 
762   bfd_default_set_arch_mach (&abfd, arch, mach);
763 
764   gdbarch_info info;
765   info.bfd_arch_info = bfd_get_arch_info (&abfd);
766   info.abfd = current_program_space->exec_bfd ();
767 
768   if (!gdbarch_update_p (info))
769     internal_error (_("rs6000_create_inferior: failed "
770 		      "to select architecture"));
771 }
772 
773 
774 /* Shared Object support.  */
775 
776 /* Return the LdInfo data for the given process.  Raises an error
777    if the data could not be obtained.  */
778 
779 static gdb::byte_vector
780 rs6000_ptrace_ldinfo (ptid_t ptid)
781 {
782   const int pid = ptid.pid ();
783   gdb::byte_vector ldi (1024);
784   int rc = -1;
785 
786   while (1)
787     {
788       if (ARCH64 ())
789 	rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi.data (),
790 			      ldi.size (), NULL);
791       else
792 	rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi.data (),
793 			      ldi.size (), NULL);
794 
795       if (rc != -1)
796 	break; /* Success, we got the entire ld_info data.  */
797 
798       if (errno != ENOMEM)
799 	perror_with_name (_("ptrace ldinfo"));
800 
801       /* ldi is not big enough.  Double it and try again.  */
802       ldi.resize (ldi.size () * 2);
803     }
804 
805   return ldi;
806 }
807 
808 /* Implement the to_xfer_partial target_ops method for
809    TARGET_OBJECT_LIBRARIES_AIX objects.  */
810 
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)
816 {
817   ULONGEST result;
818 
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 ());
822 
823   if (writebuf)
824     return TARGET_XFER_E_IO;
825 
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);
829 
830   if (result == 0)
831     return TARGET_XFER_EOF;
832   else
833     {
834       *xfered_len = result;
835       return TARGET_XFER_OK;
836     }
837 }
838 
839 void _initialize_rs6000_nat ();
840 void
841 _initialize_rs6000_nat ()
842 {
843   add_inf_child_target (&the_rs6000_nat_target);
844 }
845