Remove path name from test case
[binutils-gdb.git] / gdb / rs6000-aix-nat.c
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 /* Header files for alti-vec reg. */
62 #include <sys/context.h>
63
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. */
67
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. */
70
71 #if defined (__ld_info32) || defined (__ld_info64)
72 # define ARCH3264
73 #endif
74
75 /* Return whether the current architecture is 64-bit. */
76
77 #ifndef ARCH3264
78 # define ARCH64() 0
79 #else
80 # define ARCH64() (register_size (current_inferior ()->arch (), 0) == 8)
81 #endif
82
83 class rs6000_nat_target final : public inf_ptrace_target
84 {
85 public:
86 void fetch_registers (struct regcache *, int) override;
87 void store_registers (struct regcache *, int) override;
88
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;
95
96 void create_inferior (const char *, const std::string &,
97 char **, int) override;
98
99 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
100
101 /* Fork detection related functions, For adding multi process debugging
102 support. */
103 void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
104
105 const struct target_desc *read_description () override;
106
107 protected:
108
109 void post_startup_inferior (ptid_t ptid) override;
110
111 private:
112 enum target_xfer_status
113 xfer_shared_libraries (enum target_object object,
114 const char *annex, gdb_byte *readbuf,
115 const gdb_byte *writebuf,
116 ULONGEST offset, ULONGEST len,
117 ULONGEST *xfered_len);
118 };
119
120 static rs6000_nat_target the_rs6000_nat_target;
121
122 /* The below declaration is to track number of times, parent has
123 reported fork event before its children. */
124
125 static std::list<pid_t> aix_pending_parent;
126
127 /* The below declaration is for a child process event that
128 is reported before its corresponding parent process in
129 the event of a fork (). */
130
131 static std::list<pid_t> aix_pending_children;
132
133 static void
134 aix_remember_child (pid_t pid)
135 {
136 aix_pending_children.push_front (pid);
137 }
138
139 static void
140 aix_remember_parent (pid_t pid)
141 {
142 aix_pending_parent.push_front (pid);
143 }
144
145 /* This function returns a parent of a child process. */
146
147 static pid_t
148 find_my_aix_parent (pid_t child_pid)
149 {
150 struct procsinfo ProcessBuffer1;
151
152 if (getprocs (&ProcessBuffer1, sizeof (ProcessBuffer1),
153 NULL, 0, &child_pid, 1) != 1)
154 return 0;
155 else
156 return ProcessBuffer1.pi_ppid;
157 }
158
159 /* In the below function we check if there was any child
160 process pending. If it exists we return it from the
161 list, otherwise we return a null. */
162
163 static pid_t
164 has_my_aix_child_reported (pid_t parent_pid)
165 {
166 pid_t child = 0;
167 auto it = std::find_if (aix_pending_children.begin (),
168 aix_pending_children.end (),
169 [=] (pid_t child_pid)
170 {
171 return find_my_aix_parent (child_pid) == parent_pid;
172 });
173 if (it != aix_pending_children.end ())
174 {
175 child = *it;
176 aix_pending_children.erase (it);
177 }
178 return child;
179 }
180
181 /* In the below function we check if there was any parent
182 process pending. If it exists we return it from the
183 list, otherwise we return a null. */
184
185 static pid_t
186 has_my_aix_parent_reported (pid_t child_pid)
187 {
188 pid_t my_parent = find_my_aix_parent (child_pid);
189 auto it = std::find (aix_pending_parent.begin (),
190 aix_pending_parent.end (),
191 my_parent);
192 if (it != aix_pending_parent.end ())
193 {
194 aix_pending_parent.erase (it);
195 return my_parent;
196 }
197 return 0;
198 }
199
200 /* Given REGNO, a gdb register number, return the corresponding
201 number suitable for use as a ptrace() parameter. Return -1 if
202 there's no suitable mapping. Also, set the int pointed to by
203 ISFLOAT to indicate whether REGNO is a floating point register. */
204
205 static int
206 regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
207 {
208 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
209
210 *isfloat = 0;
211 if (tdep->ppc_gp0_regnum <= regno
212 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
213 return regno;
214 else if (tdep->ppc_fp0_regnum >= 0
215 && tdep->ppc_fp0_regnum <= regno
216 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
217 {
218 *isfloat = 1;
219 return regno - tdep->ppc_fp0_regnum + FPR0;
220 }
221 else if (regno == gdbarch_pc_regnum (gdbarch))
222 return IAR;
223 else if (regno == tdep->ppc_ps_regnum)
224 return MSR;
225 else if (regno == tdep->ppc_cr_regnum)
226 return CR;
227 else if (regno == tdep->ppc_lr_regnum)
228 return LR;
229 else if (regno == tdep->ppc_ctr_regnum)
230 return CTR;
231 else if (regno == tdep->ppc_xer_regnum)
232 return XER;
233 else if (tdep->ppc_fpscr_regnum >= 0
234 && regno == tdep->ppc_fpscr_regnum)
235 return FPSCR;
236 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
237 return MQ;
238 else
239 return -1;
240 }
241
242 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
243
244 static int
245 rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
246 {
247 #ifdef HAVE_PTRACE64
248 int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
249 #else
250 int ret = ptrace (req, id, (int *)addr, data, buf);
251 #endif
252 #if 0
253 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
254 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
255 #endif
256 return ret;
257 }
258
259 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
260
261 static int
262 rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
263 {
264 #ifdef ARCH3264
265 # ifdef HAVE_PTRACE64
266 int ret = ptrace64 (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
267 # else
268 int ret = ptracex (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
269 # endif
270 #else
271 int ret = 0;
272 #endif
273 #if 0
274 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
275 req, id, hex_string (addr), data, (unsigned int)buf, ret);
276 #endif
277 return ret;
278 }
279
280 /* Store the vsx registers. */
281
282 static void
283 store_vsx_register_aix (struct regcache *regcache, int regno)
284 {
285 int ret;
286 struct gdbarch *gdbarch = regcache->arch ();
287 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
288 struct thrdentry64 thrdentry;
289 __vsx_context_t vsx;
290 pid_t pid = inferior_ptid.pid ();
291 tid64_t thrd_i = 0;
292
293 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
294 &thrd_i, 1) == 1)
295 thrd_i = thrdentry.ti_tid;
296
297 memset(&vsx, 0, sizeof(__vsx_context_t));
298 if (__power_vsx() && thrd_i > 0)
299 {
300 if (ARCH64 ())
301 ret = rs6000_ptrace64 (PTT_READ_VSX, thrd_i, (long long) &vsx, 0, 0);
302 else
303 ret = rs6000_ptrace32 (PTT_READ_VSX, thrd_i, (int *)&vsx, 0, 0);
304 if (ret < 0)
305 return;
306
307 regcache->raw_collect (regno, &(vsx.__vsr_dw1[0])+
308 regno - tdep->ppc_vsr0_upper_regnum);
309
310 if (ARCH64 ())
311 ret = rs6000_ptrace64 (PTT_WRITE_VSX, thrd_i, (long long) &vsx, 0, 0);
312 else
313 ret = rs6000_ptrace32 (PTT_WRITE_VSX, thrd_i, (int *) &vsx, 0, 0);
314
315 if (ret < 0)
316 perror_with_name (_("Unable to write VSX registers after reading it"));
317 }
318 }
319
320 /* Store Altivec registers. */
321
322 static void
323 store_altivec_register_aix (struct regcache *regcache, int regno)
324 {
325 int ret;
326 struct gdbarch *gdbarch = regcache->arch ();
327 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
328 struct thrdentry64 thrdentry;
329 __vmx_context_t vmx;
330 pid_t pid = inferior_ptid.pid ();
331 tid64_t thrd_i = 0;
332
333 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
334 &thrd_i, 1) == 1)
335 thrd_i = thrdentry.ti_tid;
336
337 memset(&vmx, 0, sizeof(__vmx_context_t));
338 if (__power_vmx() && thrd_i > 0)
339 {
340 if (ARCH64 ())
341 ret = rs6000_ptrace64 (PTT_READ_VEC, thrd_i, (long long) &vmx, 0, 0);
342 else
343 ret = rs6000_ptrace32 (PTT_READ_VEC, thrd_i, (int *) &vmx, 0, 0);
344 if (ret < 0)
345 return;
346
347 regcache->raw_collect (regno, &(vmx.__vr[0]) + regno
348 - tdep->ppc_vr0_regnum);
349
350 if (ARCH64 ())
351 ret = rs6000_ptrace64 (PTT_WRITE_VEC, thrd_i, (long long) &vmx, 0, 0);
352 else
353 ret = rs6000_ptrace32 (PTT_WRITE_VEC, thrd_i, (int *) &vmx, 0, 0);
354 if (ret < 0)
355 perror_with_name (_("Unable to store AltiVec register after reading it"));
356 }
357 }
358
359 /* Supply altivec registers. */
360
361 static void
362 supply_vrregset_aix (struct regcache *regcache, __vmx_context_t *vmx)
363 {
364 int i;
365 struct gdbarch *gdbarch = regcache->arch ();
366 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
367 int num_of_vrregs = tdep->ppc_vrsave_regnum - tdep->ppc_vr0_regnum + 1;
368
369 for (i = 0; i < num_of_vrregs; i++)
370 regcache->raw_supply (tdep->ppc_vr0_regnum + i,
371 &(vmx->__vr[i]));
372 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx->__vrsave));
373 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx->__vscr));
374 }
375
376 /* Fetch altivec register. */
377
378 static void
379 fetch_altivec_registers_aix (struct regcache *regcache)
380 {
381 struct thrdentry64 thrdentry;
382 __vmx_context_t vmx;
383 pid_t pid = current_inferior ()->pid;
384 tid64_t thrd_i = 0;
385
386 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
387 &thrd_i, 1) == 1)
388 thrd_i = thrdentry.ti_tid;
389
390 memset(&vmx, 0, sizeof(__vmx_context_t));
391 if (__power_vmx() && thrd_i > 0)
392 {
393 if (ARCH64 ())
394 rs6000_ptrace64 (PTT_READ_VEC, thrd_i, (long long) &vmx, 0, 0);
395 else
396 rs6000_ptrace32 (PTT_READ_VEC, thrd_i, (int *) &vmx, 0, 0);
397 supply_vrregset_aix (regcache, &vmx);
398 }
399 }
400
401 /* supply vsx register. */
402
403 static void
404 supply_vsxregset_aix (struct regcache *regcache, __vsx_context_t *vsx)
405 {
406 int i;
407 struct gdbarch *gdbarch = regcache->arch ();
408 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
409
410 for (i = 0; i < ppc_num_vshrs; i++)
411 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + i,
412 &(vsx->__vsr_dw1[i]));
413 }
414
415 /* Fetch vsx registers. */
416 static void
417 fetch_vsx_registers_aix (struct regcache *regcache)
418 {
419 struct thrdentry64 thrdentry;
420 __vsx_context_t vsx;
421 pid_t pid = current_inferior ()->pid;
422 tid64_t thrd_i = 0;
423
424 if (getthrds64(pid, &thrdentry, sizeof(struct thrdentry64),
425 &thrd_i, 1) == 1)
426 thrd_i = thrdentry.ti_tid;
427
428 memset(&vsx, 0, sizeof(__vsx_context_t));
429 if (__power_vsx() && thrd_i > 0)
430 {
431 if (ARCH64 ())
432 rs6000_ptrace64 (PTT_READ_VSX, thrd_i, (long long) &vsx, 0, 0);
433 else
434 rs6000_ptrace32 (PTT_READ_VSX, thrd_i, (int *) &vsx, 0, 0);
435 supply_vsxregset_aix (regcache, &vsx);
436 }
437 }
438
439 void rs6000_nat_target::post_startup_inferior (ptid_t ptid)
440 {
441
442 /* In AIX to turn on multi process debugging in ptrace
443 PT_MULTI is the option to be passed,
444 with the process ID which can fork () and
445 the data parameter [fourth parameter] must be 1. */
446
447 if (!ARCH64 ())
448 rs6000_ptrace32 (PT_MULTI, ptid.pid(), 0, 1, 0);
449 else
450 rs6000_ptrace64 (PT_MULTI, ptid.pid(), 0, 1, 0);
451 }
452
453 void
454 rs6000_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
455 target_waitkind fork_kind, bool follow_child,
456 bool detach_fork)
457 {
458
459 /* Once the fork event is detected the infrun.c code
460 calls the target_follow_fork to take care of
461 follow child and detach the child activity which is
462 done using the function below. */
463
464 inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
465 follow_child, detach_fork);
466
467 /* If we detach fork and follow child we do not want the child
468 process to generate events that ptrace can trace. Hence we
469 detach it. */
470
471 if (detach_fork && !follow_child)
472 {
473 if (ARCH64 ())
474 rs6000_ptrace64 (PT_DETACH, child_ptid.pid (), 0, 0, 0);
475 else
476 rs6000_ptrace32 (PT_DETACH, child_ptid.pid (), 0, 0, 0);
477 }
478 }
479
480 /* Fetch register REGNO from the inferior. */
481
482 static void
483 fetch_register (struct regcache *regcache, int regno)
484 {
485 struct gdbarch *gdbarch = regcache->arch ();
486 int addr[PPC_MAX_REGISTER_SIZE];
487 int nr, isfloat;
488 pid_t pid = regcache->ptid ().pid ();
489
490 /* Retrieved values may be -1, so infer errors from errno. */
491 errno = 0;
492
493 /* Alti-vec register. */
494 if (altivec_register_p (gdbarch, regno))
495 {
496 fetch_altivec_registers_aix (regcache);
497 return;
498 }
499
500 /* VSX register. */
501 if (vsx_register_p (gdbarch, regno))
502 {
503 fetch_vsx_registers_aix (regcache);
504 return;
505 }
506
507 nr = regmap (gdbarch, regno, &isfloat);
508
509 /* Floating-point registers. */
510 if (isfloat)
511 rs6000_ptrace32 (PT_READ_FPR, pid, addr, nr, 0);
512
513 /* Bogus register number. */
514 else if (nr < 0)
515 {
516 if (regno >= gdbarch_num_regs (gdbarch))
517 gdb_printf (gdb_stderr,
518 "gdb error: register no %d not implemented.\n",
519 regno);
520 return;
521 }
522
523 /* Fixed-point registers. */
524 else
525 {
526 if (!ARCH64 ())
527 *addr = rs6000_ptrace32 (PT_READ_GPR, pid, (int *) nr, 0, 0);
528 else
529 {
530 /* PT_READ_GPR requires the buffer parameter to point to long long,
531 even if the register is really only 32 bits. */
532 long long buf;
533 rs6000_ptrace64 (PT_READ_GPR, pid, nr, 0, &buf);
534 if (register_size (gdbarch, regno) == 8)
535 memcpy (addr, &buf, 8);
536 else
537 *addr = buf;
538 }
539 }
540
541 if (!errno)
542 regcache->raw_supply (regno, (char *) addr);
543 else
544 {
545 #if 0
546 /* FIXME: this happens 3 times at the start of each 64-bit program. */
547 perror (_("ptrace read"));
548 #endif
549 errno = 0;
550 }
551 }
552
553 /* Store register REGNO back into the inferior. */
554
555 static void
556 store_register (struct regcache *regcache, int regno)
557 {
558 struct gdbarch *gdbarch = regcache->arch ();
559 int addr[PPC_MAX_REGISTER_SIZE];
560 int nr, isfloat;
561 pid_t pid = regcache->ptid ().pid ();
562
563 /* Fetch the register's value from the register cache. */
564 regcache->raw_collect (regno, addr);
565
566 /* -1 can be a successful return value, so infer errors from errno. */
567 errno = 0;
568
569 if (altivec_register_p (gdbarch, regno))
570 {
571 store_altivec_register_aix (regcache, regno);
572 return;
573 }
574
575 if (vsx_register_p (gdbarch, regno))
576 {
577 store_vsx_register_aix (regcache, regno);
578 return;
579 }
580
581 nr = regmap (gdbarch, regno, &isfloat);
582
583 /* Floating-point registers. */
584 if (isfloat)
585 rs6000_ptrace32 (PT_WRITE_FPR, pid, addr, nr, 0);
586
587 /* Bogus register number. */
588 else if (nr < 0)
589 {
590 if (regno >= gdbarch_num_regs (gdbarch))
591 gdb_printf (gdb_stderr,
592 "gdb error: register no %d not implemented.\n",
593 regno);
594 }
595
596 /* Fixed-point registers. */
597 else
598 {
599 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
600 the register's value is passed by value, but for 64-bit inferiors,
601 the address of a buffer containing the value is passed. */
602 if (!ARCH64 ())
603 rs6000_ptrace32 (PT_WRITE_GPR, pid, (int *) nr, *addr, 0);
604 else
605 {
606 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
607 area, even if the register is really only 32 bits. */
608 long long buf;
609 if (register_size (gdbarch, regno) == 8)
610 memcpy (&buf, addr, 8);
611 else
612 buf = *addr;
613 rs6000_ptrace64 (PT_WRITE_GPR, pid, nr, 0, &buf);
614 }
615 }
616
617 if (errno)
618 {
619 perror (_("ptrace write"));
620 errno = 0;
621 }
622 }
623
624 /* Read from the inferior all registers if REGNO == -1 and just register
625 REGNO otherwise. */
626
627 void
628 rs6000_nat_target::fetch_registers (struct regcache *regcache, int regno)
629 {
630 struct gdbarch *gdbarch = regcache->arch ();
631 if (regno != -1)
632 fetch_register (regcache, regno);
633
634 else
635 {
636 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
637
638 /* Read 32 general purpose registers. */
639 for (regno = tdep->ppc_gp0_regnum;
640 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
641 regno++)
642 {
643 fetch_register (regcache, regno);
644 }
645
646 /* Read general purpose floating point registers. */
647 if (tdep->ppc_fp0_regnum >= 0)
648 for (regno = 0; regno < ppc_num_fprs; regno++)
649 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
650
651 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
652 fetch_altivec_registers_aix (regcache);
653
654 if (tdep->ppc_vsr0_upper_regnum != -1)
655 fetch_vsx_registers_aix (regcache);
656
657 /* Read special registers. */
658 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
659 fetch_register (regcache, tdep->ppc_ps_regnum);
660 fetch_register (regcache, tdep->ppc_cr_regnum);
661 fetch_register (regcache, tdep->ppc_lr_regnum);
662 fetch_register (regcache, tdep->ppc_ctr_regnum);
663 fetch_register (regcache, tdep->ppc_xer_regnum);
664 if (tdep->ppc_fpscr_regnum >= 0)
665 fetch_register (regcache, tdep->ppc_fpscr_regnum);
666 if (tdep->ppc_mq_regnum >= 0)
667 fetch_register (regcache, tdep->ppc_mq_regnum);
668 }
669 }
670
671 const struct target_desc *
672 rs6000_nat_target::read_description ()
673 {
674 if (ARCH64())
675 {
676 if (__power_vsx ())
677 return tdesc_powerpc_vsx64;
678 else if (__power_vmx ())
679 return tdesc_powerpc_altivec64;
680 }
681 else
682 {
683 if (__power_vsx ())
684 return tdesc_powerpc_vsx32;
685 else if (__power_vmx ())
686 return tdesc_powerpc_altivec32;
687 }
688 return NULL;
689 }
690
691 /* Store our register values back into the inferior.
692 If REGNO is -1, do this for all registers.
693 Otherwise, REGNO specifies which register (so we can save time). */
694
695 void
696 rs6000_nat_target::store_registers (struct regcache *regcache, int regno)
697 {
698 struct gdbarch *gdbarch = regcache->arch ();
699 if (regno != -1)
700 store_register (regcache, regno);
701
702 else
703 {
704 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
705
706 /* Write general purpose registers first. */
707 for (regno = tdep->ppc_gp0_regnum;
708 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
709 regno++)
710 {
711 store_register (regcache, regno);
712 }
713
714 /* Write floating point registers. */
715 if (tdep->ppc_fp0_regnum >= 0)
716 for (regno = 0; regno < ppc_num_fprs; regno++)
717 store_register (regcache, tdep->ppc_fp0_regnum + regno);
718
719 /* Write special registers. */
720 store_register (regcache, gdbarch_pc_regnum (gdbarch));
721 store_register (regcache, tdep->ppc_ps_regnum);
722 store_register (regcache, tdep->ppc_cr_regnum);
723 store_register (regcache, tdep->ppc_lr_regnum);
724 store_register (regcache, tdep->ppc_ctr_regnum);
725 store_register (regcache, tdep->ppc_xer_regnum);
726 if (tdep->ppc_fpscr_regnum >= 0)
727 store_register (regcache, tdep->ppc_fpscr_regnum);
728 if (tdep->ppc_mq_regnum >= 0)
729 store_register (regcache, tdep->ppc_mq_regnum);
730 }
731 }
732
733 /* Implement the to_xfer_partial target_ops method. */
734
735 enum target_xfer_status
736 rs6000_nat_target::xfer_partial (enum target_object object,
737 const char *annex, gdb_byte *readbuf,
738 const gdb_byte *writebuf,
739 ULONGEST offset, ULONGEST len,
740 ULONGEST *xfered_len)
741 {
742 pid_t pid = inferior_ptid.pid ();
743 int arch64 = ARCH64 ();
744
745 switch (object)
746 {
747 case TARGET_OBJECT_LIBRARIES_AIX:
748 return xfer_shared_libraries (object, annex,
749 readbuf, writebuf,
750 offset, len, xfered_len);
751 case TARGET_OBJECT_MEMORY:
752 {
753 union
754 {
755 PTRACE_TYPE_RET word;
756 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
757 } buffer;
758 ULONGEST rounded_offset;
759 LONGEST partial_len;
760
761 /* Round the start offset down to the next long word
762 boundary. */
763 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
764
765 /* Since ptrace will transfer a single word starting at that
766 rounded_offset the partial_len needs to be adjusted down to
767 that (remember this function only does a single transfer).
768 Should the required length be even less, adjust it down
769 again. */
770 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
771 if (partial_len > len)
772 partial_len = len;
773
774 if (writebuf)
775 {
776 /* If OFFSET:PARTIAL_LEN is smaller than
777 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
778 be needed. Read in the entire word. */
779 if (rounded_offset < offset
780 || (offset + partial_len
781 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
782 {
783 /* Need part of initial word -- fetch it. */
784 if (arch64)
785 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
786 rounded_offset, 0, NULL);
787 else
788 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
789 (int *) (uintptr_t)
790 rounded_offset,
791 0, NULL);
792 }
793
794 /* Copy data to be written over corresponding part of
795 buffer. */
796 memcpy (buffer.byte + (offset - rounded_offset),
797 writebuf, partial_len);
798
799 errno = 0;
800 if (arch64)
801 rs6000_ptrace64 (PT_WRITE_D, pid,
802 rounded_offset, buffer.word, NULL);
803 else
804 rs6000_ptrace32 (PT_WRITE_D, pid,
805 (int *) (uintptr_t) rounded_offset,
806 buffer.word, NULL);
807 if (errno)
808 return TARGET_XFER_EOF;
809 }
810
811 if (readbuf)
812 {
813 errno = 0;
814 if (arch64)
815 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
816 rounded_offset, 0, NULL);
817 else
818 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
819 (int *)(uintptr_t)rounded_offset,
820 0, NULL);
821 if (errno)
822 return TARGET_XFER_EOF;
823
824 /* Copy appropriate bytes out of the buffer. */
825 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
826 partial_len);
827 }
828
829 *xfered_len = (ULONGEST) partial_len;
830 return TARGET_XFER_OK;
831 }
832
833 default:
834 return TARGET_XFER_E_IO;
835 }
836 }
837
838 /* Wait for the child specified by PTID to do something. Return the
839 process ID of the child, or MINUS_ONE_PTID in case of error; store
840 the status in *OURSTATUS. */
841
842 ptid_t
843 rs6000_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
844 target_wait_flags options)
845 {
846 pid_t pid;
847 int status, save_errno;
848
849 while (1)
850 {
851 set_sigint_trap ();
852
853 do
854 {
855 pid = waitpid (ptid.pid (), &status, 0);
856 save_errno = errno;
857 }
858 while (pid == -1 && errno == EINTR);
859
860 clear_sigint_trap ();
861
862 if (pid == -1)
863 {
864 gdb_printf (gdb_stderr,
865 _("Child process unexpectedly missing: %s.\n"),
866 safe_strerror (save_errno));
867
868 ourstatus->set_ignore ();
869 return minus_one_ptid;
870 }
871
872 /* Ignore terminated detached child processes. */
873 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
874 continue;
875
876 /* Check for a fork () event. */
877 if ((status & 0xff) == W_SFWTED)
878 {
879 /* Checking whether it is a parent or a child event. */
880
881 /* If the event is a child we check if there was a parent
882 event recorded before. If yes we got the parent child
883 relationship. If not we push this child and wait for
884 the next fork () event. */
885 if (find_inferior_pid (this, pid) == nullptr)
886 {
887 pid_t parent_pid = has_my_aix_parent_reported (pid);
888 if (parent_pid > 0)
889 {
890 ourstatus->set_forked (ptid_t (pid));
891 return ptid_t (parent_pid);
892 }
893 aix_remember_child (pid);
894 }
895
896 /* If the event is a parent we check if there was a child
897 event recorded before. If yes we got the parent child
898 relationship. If not we push this parent and wait for
899 the next fork () event. */
900 else
901 {
902 pid_t child_pid = has_my_aix_child_reported (pid);
903 if (child_pid > 0)
904 {
905 ourstatus->set_forked (ptid_t (child_pid));
906 return ptid_t (pid);
907 }
908 aix_remember_parent (pid);
909 }
910 continue;
911 }
912
913 break;
914 }
915
916 /* AIX has a couple of strange returns from wait(). */
917
918 /* stop after load" status. */
919 if (status == 0x57c)
920 ourstatus->set_loaded ();
921 /* 0x7f is signal 0. 0x17f and 0x137f are status returned
922 if we follow parent, a switch is made to a child post parent
923 execution and child continues its execution [user switches
924 to child and presses continue]. */
925 else if (status == 0x7f || status == 0x17f || status == 0x137f)
926 ourstatus->set_spurious ();
927 /* A normal waitstatus. Let the usual macros deal with it. */
928 else
929 *ourstatus = host_status_to_waitstatus (status);
930
931 return ptid_t (pid);
932 }
933 \f
934
935 /* Set the current architecture from the host running GDB. Called when
936 starting a child process. */
937
938 void
939 rs6000_nat_target::create_inferior (const char *exec_file,
940 const std::string &allargs,
941 char **env, int from_tty)
942 {
943 enum bfd_architecture arch;
944 unsigned long mach;
945 bfd abfd;
946
947 inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
948
949 if (__power_rs ())
950 {
951 arch = bfd_arch_rs6000;
952 mach = bfd_mach_rs6k;
953 }
954 else
955 {
956 arch = bfd_arch_powerpc;
957 mach = bfd_mach_ppc;
958 }
959
960 /* FIXME: schauer/2002-02-25:
961 We don't know if we are executing a 32 or 64 bit executable,
962 and have no way to pass the proper word size to rs6000_gdbarch_init.
963 So we have to avoid switching to a new architecture, if the architecture
964 matches already.
965 Blindly calling rs6000_gdbarch_init used to work in older versions of
966 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
967 determine the wordsize. */
968 if (current_program_space->exec_bfd ())
969 {
970 const struct bfd_arch_info *exec_bfd_arch_info;
971
972 exec_bfd_arch_info
973 = bfd_get_arch_info (current_program_space->exec_bfd ());
974 if (arch == exec_bfd_arch_info->arch)
975 return;
976 }
977
978 bfd_default_set_arch_mach (&abfd, arch, mach);
979
980 gdbarch_info info;
981 info.bfd_arch_info = bfd_get_arch_info (&abfd);
982 info.abfd = current_program_space->exec_bfd ();
983
984 if (!gdbarch_update_p (info))
985 internal_error (_("rs6000_create_inferior: failed "
986 "to select architecture"));
987 }
988 \f
989
990 /* Shared Object support. */
991
992 /* Return the LdInfo data for the given process. Raises an error
993 if the data could not be obtained. */
994
995 static gdb::byte_vector
996 rs6000_ptrace_ldinfo (ptid_t ptid)
997 {
998 const int pid = ptid.pid ();
999 gdb::byte_vector ldi (1024);
1000 int rc = -1;
1001
1002 while (1)
1003 {
1004 if (ARCH64 ())
1005 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi.data (),
1006 ldi.size (), NULL);
1007 else
1008 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi.data (),
1009 ldi.size (), NULL);
1010
1011 if (rc != -1)
1012 break; /* Success, we got the entire ld_info data. */
1013
1014 if (errno != ENOMEM)
1015 perror_with_name (_("ptrace ldinfo"));
1016
1017 /* ldi is not big enough. Double it and try again. */
1018 ldi.resize (ldi.size () * 2);
1019 }
1020
1021 return ldi;
1022 }
1023
1024 /* Implement the to_xfer_partial target_ops method for
1025 TARGET_OBJECT_LIBRARIES_AIX objects. */
1026
1027 enum target_xfer_status
1028 rs6000_nat_target::xfer_shared_libraries
1029 (enum target_object object,
1030 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
1031 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1032 {
1033 ULONGEST result;
1034
1035 /* This function assumes that it is being run with a live process.
1036 Core files are handled via gdbarch. */
1037 gdb_assert (target_has_execution ());
1038
1039 if (writebuf)
1040 return TARGET_XFER_E_IO;
1041
1042 gdb::byte_vector ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
1043 result = rs6000_aix_ld_info_to_xml (current_inferior ()->arch (),
1044 ldi_buf.data (),
1045 readbuf, offset, len, 1);
1046
1047 if (result == 0)
1048 return TARGET_XFER_EOF;
1049 else
1050 {
1051 *xfered_len = result;
1052 return TARGET_XFER_OK;
1053 }
1054 }
1055
1056 void _initialize_rs6000_nat ();
1057 void
1058 _initialize_rs6000_nat ()
1059 {
1060 add_inf_child_target (&the_rs6000_nat_target);
1061 }