]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rs6000-aix-nat.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / rs6000-aix-nat.c
CommitLineData
c906108c 1/* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
4646aa9d 2
213516ef 3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "inferior.h"
22#include "target.h"
23#include "gdbcore.h"
c906108c
SS
24#include "symfile.h"
25#include "objfiles.h"
c906108c
SS
26#include "bfd.h"
27#include "gdb-stabs.h"
4e052eda 28#include "regcache.h"
19caaa45 29#include "arch-utils.h"
dab06dbe 30#include "inf-child.h"
037a727e 31#include "inf-ptrace.h"
11bf77db 32#include "ppc-tdep.h"
356a5233 33#include "rs6000-aix-tdep.h"
4646aa9d 34#include "exec.h"
76727919 35#include "observable.h"
63807e1d 36#include "xcoffread.h"
c906108c
SS
37
38#include <sys/ptrace.h>
39#include <sys/reg.h>
40
c906108c
SS
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>
53ce3c39 49#include <sys/stat.h>
92107356 50#include "gdb_bfd.h"
c906108c 51#include <sys/core.h>
7a78ae4e
ND
52#define __LDINFO_PTRACE32__ /* for __ld_info32 */
53#define __LDINFO_PTRACE64__ /* for __ld_info64 */
c906108c 54#include <sys/ldr.h>
7a78ae4e 55#include <sys/systemcfg.h>
c906108c 56
c7d09018
AVK
57/* Header files for getting ppid in AIX of a child process. */
58#include <procinfo.h>
59#include <sys/types.h>
60
7a78ae4e
ND
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
0df8b418 63 accessing fields in the appropriate structures. */
7a78ae4e
ND
64
65/* In 32-bit compilation mode (which is the only mode from which ptrace()
0df8b418 66 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
7a78ae4e 67
b08ee99f 68#if defined (__ld_info32) || defined (__ld_info64)
7a78ae4e
ND
69# define ARCH3264
70#endif
71
0df8b418 72/* Return whether the current architecture is 64-bit. */
7a78ae4e
ND
73
74#ifndef ARCH3264
75# define ARCH64() 0
76#else
f5656ead 77# define ARCH64() (register_size (target_gdbarch (), 0) == 8)
7a78ae4e
ND
78#endif
79
f6ac5f3d
PA
80class rs6000_nat_target final : public inf_ptrace_target
81{
82public:
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
b60cea74 96 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
f6ac5f3d 97
c7d09018
AVK
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
200fd287
AB
102protected:
103
c7d09018 104 void post_startup_inferior (ptid_t ptid) override;
200fd287 105
f6ac5f3d
PA
106private:
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
115static rs6000_nat_target the_rs6000_nat_target;
4d1eb6b4 116
c7d09018
AVK
117/* The below declaration is to track number of times, parent has
118 reported fork event before its children. */
119
120static 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
126static std::list<pid_t> aix_pending_children;
127
128static void
129aix_remember_child (pid_t pid)
130{
131 aix_pending_children.push_front (pid);
132}
133
134static void
135aix_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
142static pid_t
143find_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
158static pid_t
159has_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
180static pid_t
181has_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
dd7be90a
KB
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. */
c906108c 199
dd7be90a 200static int
206988c4 201regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
c5aa993b 202{
08106042 203 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
dd7be90a
KB
204
205 *isfloat = 0;
8bf659e8
JB
206 if (tdep->ppc_gp0_regnum <= regno
207 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
dd7be90a 208 return regno;
383f0f5b 209 else if (tdep->ppc_fp0_regnum >= 0
dda83cd7
SM
210 && tdep->ppc_fp0_regnum <= regno
211 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
dd7be90a
KB
212 {
213 *isfloat = 1;
366f009f 214 return regno - tdep->ppc_fp0_regnum + FPR0;
dd7be90a 215 }
206988c4 216 else if (regno == gdbarch_pc_regnum (gdbarch))
dd7be90a
KB
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;
383f0f5b 228 else if (tdep->ppc_fpscr_regnum >= 0
dda83cd7 229 && regno == tdep->ppc_fpscr_regnum)
0e061eef 230 return FPSCR;
dd7be90a
KB
231 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
232 return MQ;
233 else
234 return -1;
235}
c906108c 236
0df8b418 237/* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
c906108c 238
7a78ae4e 239static int
8b5790f2 240rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
7a78ae4e 241{
1ed3ee94 242#ifdef HAVE_PTRACE64
11cb8762 243 int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
1ed3ee94 244#else
7a78ae4e 245 int ret = ptrace (req, id, (int *)addr, data, buf);
1ed3ee94 246#endif
7a78ae4e 247#if 0
8b5790f2 248 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
7a78ae4e
ND
249 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
250#endif
251 return ret;
252}
c906108c 253
0df8b418 254/* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
c906108c 255
7a78ae4e 256static int
0d16ee5d 257rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
7a78ae4e
ND
258{
259#ifdef ARCH3264
1ed3ee94 260# ifdef HAVE_PTRACE64
71829b1a 261 int ret = ptrace64 (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
1ed3ee94 262# else
71829b1a 263 int ret = ptracex (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
1ed3ee94 264# endif
7a78ae4e
ND
265#else
266 int ret = 0;
267#endif
268#if 0
2244ba2e
PM
269 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
270 req, id, hex_string (addr), data, (unsigned int)buf, ret);
7a78ae4e
ND
271#endif
272 return ret;
273}
c906108c 274
c7d09018
AVK
275void 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
289void
290rs6000_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
0df8b418 316/* Fetch register REGNO from the inferior. */
c906108c 317
7a78ae4e 318static void
56be3814 319fetch_register (struct regcache *regcache, int regno)
7a78ae4e 320{
ac7936df 321 struct gdbarch *gdbarch = regcache->arch ();
0f068fb5 322 int addr[PPC_MAX_REGISTER_SIZE];
dd7be90a 323 int nr, isfloat;
e99b03dc 324 pid_t pid = regcache->ptid ().pid ();
c906108c 325
0df8b418 326 /* Retrieved values may be -1, so infer errors from errno. */
7a78ae4e 327 errno = 0;
c906108c 328
206988c4 329 nr = regmap (gdbarch, regno, &isfloat);
dd7be90a 330
0df8b418 331 /* Floating-point registers. */
dd7be90a 332 if (isfloat)
bcc0c096 333 rs6000_ptrace32 (PT_READ_FPR, pid, addr, nr, 0);
c906108c 334
0df8b418 335 /* Bogus register number. */
dd7be90a 336 else if (nr < 0)
2a18e3d9 337 {
8b164abb 338 if (regno >= gdbarch_num_regs (gdbarch))
6cb06a8c
TT
339 gdb_printf (gdb_stderr,
340 "gdb error: register no %d not implemented.\n",
341 regno);
dd7be90a 342 return;
2a18e3d9 343 }
c906108c 344
0df8b418 345 /* Fixed-point registers. */
7a78ae4e
ND
346 else
347 {
7a78ae4e 348 if (!ARCH64 ())
bcc0c096 349 *addr = rs6000_ptrace32 (PT_READ_GPR, pid, (int *) nr, 0, 0);
7a78ae4e
ND
350 else
351 {
352 /* PT_READ_GPR requires the buffer parameter to point to long long,
0df8b418 353 even if the register is really only 32 bits. */
7a78ae4e 354 long long buf;
bcc0c096 355 rs6000_ptrace64 (PT_READ_GPR, pid, nr, 0, &buf);
8b164abb 356 if (register_size (gdbarch, regno) == 8)
7a78ae4e
ND
357 memcpy (addr, &buf, 8);
358 else
359 *addr = buf;
360 }
361 }
362
363 if (!errno)
73e1c03f 364 regcache->raw_supply (regno, (char *) addr);
7a78ae4e
ND
365 else
366 {
367#if 0
0df8b418 368 /* FIXME: this happens 3 times at the start of each 64-bit program. */
9b20d036 369 perror (_("ptrace read"));
7a78ae4e
ND
370#endif
371 errno = 0;
372 }
c906108c
SS
373}
374
0df8b418 375/* Store register REGNO back into the inferior. */
c906108c 376
7a78ae4e 377static void
fb14de7b 378store_register (struct regcache *regcache, int regno)
c906108c 379{
ac7936df 380 struct gdbarch *gdbarch = regcache->arch ();
0f068fb5 381 int addr[PPC_MAX_REGISTER_SIZE];
dd7be90a 382 int nr, isfloat;
e99b03dc 383 pid_t pid = regcache->ptid ().pid ();
c906108c 384
11bf77db 385 /* Fetch the register's value from the register cache. */
34a79281 386 regcache->raw_collect (regno, addr);
11bf77db 387
0df8b418 388 /* -1 can be a successful return value, so infer errors from errno. */
c906108c
SS
389 errno = 0;
390
206988c4 391 nr = regmap (gdbarch, regno, &isfloat);
dd7be90a 392
0df8b418 393 /* Floating-point registers. */
dd7be90a 394 if (isfloat)
bcc0c096 395 rs6000_ptrace32 (PT_WRITE_FPR, pid, addr, nr, 0);
c906108c 396
0df8b418 397 /* Bogus register number. */
dd7be90a 398 else if (nr < 0)
7a78ae4e 399 {
8b164abb 400 if (regno >= gdbarch_num_regs (gdbarch))
6cb06a8c
TT
401 gdb_printf (gdb_stderr,
402 "gdb error: register no %d not implemented.\n",
403 regno);
7a78ae4e 404 }
c906108c 405
0df8b418 406 /* Fixed-point registers. */
7a78ae4e
ND
407 else
408 {
11bf77db 409 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
dda83cd7 410 the register's value is passed by value, but for 64-bit inferiors,
11bf77db 411 the address of a buffer containing the value is passed. */
7a78ae4e 412 if (!ARCH64 ())
bcc0c096 413 rs6000_ptrace32 (PT_WRITE_GPR, pid, (int *) nr, *addr, 0);
7a78ae4e 414 else
c906108c 415 {
7a78ae4e 416 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
0df8b418 417 area, even if the register is really only 32 bits. */
7a78ae4e 418 long long buf;
8b164abb 419 if (register_size (gdbarch, regno) == 8)
7a78ae4e
ND
420 memcpy (&buf, addr, 8);
421 else
422 buf = *addr;
bcc0c096 423 rs6000_ptrace64 (PT_WRITE_GPR, pid, nr, 0, &buf);
c906108c
SS
424 }
425 }
426
7a78ae4e 427 if (errno)
c906108c 428 {
9b20d036 429 perror (_("ptrace write"));
7a78ae4e 430 errno = 0;
c906108c 431 }
7a78ae4e 432}
c906108c 433
7a78ae4e 434/* Read from the inferior all registers if REGNO == -1 and just register
0df8b418 435 REGNO otherwise. */
c906108c 436
f6ac5f3d
PA
437void
438rs6000_nat_target::fetch_registers (struct regcache *regcache, int regno)
7a78ae4e 439{
ac7936df 440 struct gdbarch *gdbarch = regcache->arch ();
7a78ae4e 441 if (regno != -1)
56be3814 442 fetch_register (regcache, regno);
7a78ae4e
ND
443
444 else
c906108c 445 {
08106042 446 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
7a78ae4e 447
dd7be90a
KB
448 /* Read 32 general purpose registers. */
449 for (regno = tdep->ppc_gp0_regnum;
dda83cd7 450 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
dd7be90a
KB
451 regno++)
452 {
56be3814 453 fetch_register (regcache, regno);
dd7be90a
KB
454 }
455
456 /* Read general purpose floating point registers. */
383f0f5b 457 if (tdep->ppc_fp0_regnum >= 0)
dda83cd7
SM
458 for (regno = 0; regno < ppc_num_fprs; regno++)
459 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
7a78ae4e 460
dd7be90a 461 /* Read special registers. */
8b164abb 462 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
56be3814
UW
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);
383f0f5b 468 if (tdep->ppc_fpscr_regnum >= 0)
dda83cd7 469 fetch_register (regcache, tdep->ppc_fpscr_regnum);
dd7be90a 470 if (tdep->ppc_mq_regnum >= 0)
56be3814 471 fetch_register (regcache, tdep->ppc_mq_regnum);
c906108c 472 }
7a78ae4e 473}
c906108c 474
7a78ae4e
ND
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
f6ac5f3d
PA
479void
480rs6000_nat_target::store_registers (struct regcache *regcache, int regno)
7a78ae4e 481{
ac7936df 482 struct gdbarch *gdbarch = regcache->arch ();
7a78ae4e 483 if (regno != -1)
56be3814 484 store_register (regcache, regno);
7a78ae4e
ND
485
486 else
f6077098 487 {
08106042 488 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
dd7be90a
KB
489
490 /* Write general purpose registers first. */
491 for (regno = tdep->ppc_gp0_regnum;
dda83cd7 492 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
dd7be90a
KB
493 regno++)
494 {
56be3814 495 store_register (regcache, regno);
dd7be90a 496 }
7a78ae4e 497
dd7be90a 498 /* Write floating point registers. */
383f0f5b 499 if (tdep->ppc_fp0_regnum >= 0)
dda83cd7
SM
500 for (regno = 0; regno < ppc_num_fprs; regno++)
501 store_register (regcache, tdep->ppc_fp0_regnum + regno);
7a78ae4e 502
dd7be90a 503 /* Write special registers. */
8b164abb 504 store_register (regcache, gdbarch_pc_regnum (gdbarch));
56be3814
UW
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);
383f0f5b 510 if (tdep->ppc_fpscr_regnum >= 0)
dda83cd7 511 store_register (regcache, tdep->ppc_fpscr_regnum);
dd7be90a 512 if (tdep->ppc_mq_regnum >= 0)
56be3814 513 store_register (regcache, tdep->ppc_mq_regnum);
f6077098 514 }
7a78ae4e 515}
f6077098 516
edcc890f 517/* Implement the to_xfer_partial target_ops method. */
7a78ae4e 518
f6ac5f3d
PA
519enum target_xfer_status
520rs6000_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)
7a78ae4e 525{
e99b03dc 526 pid_t pid = inferior_ptid.pid ();
7a78ae4e 527 int arch64 = ARCH64 ();
7a78ae4e 528
037a727e 529 switch (object)
c906108c 530 {
ff99b71b 531 case TARGET_OBJECT_LIBRARIES_AIX:
f6ac5f3d
PA
532 return xfer_shared_libraries (object, annex,
533 readbuf, writebuf,
534 offset, len, xfered_len);
037a727e
UW
535 case TARGET_OBJECT_MEMORY:
536 {
537 union
7a78ae4e 538 {
037a727e
UW
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,
0df8b418
MS
573 (int *) (uintptr_t)
574 rounded_offset,
037a727e
UW
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,
0df8b418
MS
589 (int *) (uintptr_t) rounded_offset,
590 buffer.word, NULL);
037a727e 591 if (errno)
9b409511 592 return TARGET_XFER_EOF;
037a727e
UW
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)
9b409511 606 return TARGET_XFER_EOF;
037a727e
UW
607
608 /* Copy appropriate bytes out of the buffer. */
609 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
610 partial_len);
611 }
612
9b409511
YQ
613 *xfered_len = (ULONGEST) partial_len;
614 return TARGET_XFER_OK;
037a727e
UW
615 }
616
617 default:
2ed4b548 618 return TARGET_XFER_E_IO;
7a78ae4e 619 }
c906108c
SS
620}
621
482f7fee
UW
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
f6ac5f3d
PA
626ptid_t
627rs6000_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
b60cea74 628 target_wait_flags options)
482f7fee
UW
629{
630 pid_t pid;
631 int status, save_errno;
632
c7d09018 633 while (1)
482f7fee
UW
634 {
635 set_sigint_trap ();
482f7fee
UW
636
637 do
638 {
e99b03dc 639 pid = waitpid (ptid.pid (), &status, 0);
482f7fee
UW
640 save_errno = errno;
641 }
642 while (pid == -1 && errno == EINTR);
643
482f7fee
UW
644 clear_sigint_trap ();
645
646 if (pid == -1)
647 {
6cb06a8c
TT
648 gdb_printf (gdb_stderr,
649 _("Child process unexpectedly missing: %s.\n"),
650 safe_strerror (save_errno));
482f7fee 651
c0abbd96
SM
652 ourstatus->set_ignore ();
653 return minus_one_ptid;
482f7fee
UW
654 }
655
656 /* Ignore terminated detached child processes. */
c0abbd96 657 if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
c7d09018
AVK
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;
482f7fee 698 }
482f7fee
UW
699
700 /* AIX has a couple of strange returns from wait(). */
701
702 /* stop after load" status. */
703 if (status == 0x57c)
183be222 704 ourstatus->set_loaded ();
c7d09018
AVK
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)
183be222 710 ourstatus->set_spurious ();
482f7fee
UW
711 /* A normal waitstatus. Let the usual macros deal with it. */
712 else
7509b829 713 *ourstatus = host_status_to_waitstatus (status);
482f7fee 714
f2907e49 715 return ptid_t (pid);
482f7fee 716}
c906108c 717\f
7a78ae4e 718
7a78ae4e 719/* Set the current architecture from the host running GDB. Called when
0df8b418 720 starting a child process. */
7a78ae4e 721
f6ac5f3d
PA
722void
723rs6000_nat_target::create_inferior (const char *exec_file,
724 const std::string &allargs,
725 char **env, int from_tty)
7a78ae4e
ND
726{
727 enum bfd_architecture arch;
728 unsigned long mach;
729 bfd abfd;
7a78ae4e 730
f6ac5f3d 731 inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
1f480a5e 732
7a78ae4e
ND
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 }
19caaa45
PS
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. */
7e10abd1 752 if (current_program_space->exec_bfd ())
19caaa45
PS
753 {
754 const struct bfd_arch_info *exec_bfd_arch_info;
755
7e10abd1
TT
756 exec_bfd_arch_info
757 = bfd_get_arch_info (current_program_space->exec_bfd ());
19caaa45
PS
758 if (arch == exec_bfd_arch_info->arch)
759 return;
760 }
761
7a78ae4e
ND
762 bfd_default_set_arch_mach (&abfd, arch, mach);
763
b447dd03 764 gdbarch_info info;
7a78ae4e 765 info.bfd_arch_info = bfd_get_arch_info (&abfd);
7e10abd1 766 info.abfd = current_program_space->exec_bfd ();
7a78ae4e 767
16f33e29 768 if (!gdbarch_update_p (info))
f34652de 769 internal_error (_("rs6000_create_inferior: failed "
0df8b418 770 "to select architecture"));
7a78ae4e 771}
c906108c 772\f
c906108c 773
4d1eb6b4 774/* Shared Object support. */
c906108c 775
4d1eb6b4 776/* Return the LdInfo data for the given process. Raises an error
09473be8 777 if the data could not be obtained. */
8d08c9ce 778
09473be8 779static gdb::byte_vector
4d1eb6b4
JB
780rs6000_ptrace_ldinfo (ptid_t ptid)
781{
e99b03dc 782 const int pid = ptid.pid ();
09473be8 783 gdb::byte_vector ldi (1024);
4d1eb6b4 784 int rc = -1;
7a78ae4e 785
4d1eb6b4
JB
786 while (1)
787 {
788 if (ARCH64 ())
09473be8
TT
789 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi.data (),
790 ldi.size (), NULL);
c18e0d23 791 else
09473be8
TT
792 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi.data (),
793 ldi.size (), NULL);
4d1eb6b4
JB
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. */
09473be8 802 ldi.resize (ldi.size () * 2);
4d1eb6b4
JB
803 }
804
09473be8 805 return ldi;
c906108c 806}
c906108c 807
4d1eb6b4 808/* Implement the to_xfer_partial target_ops method for
ff99b71b 809 TARGET_OBJECT_LIBRARIES_AIX objects. */
6426a772 810
f6ac5f3d
PA
811enum target_xfer_status
812rs6000_nat_target::xfer_shared_libraries
813 (enum target_object object,
4d1eb6b4 814 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
9b409511 815 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
4d1eb6b4 816{
356a5233 817 ULONGEST result;
356a5233
JB
818
819 /* This function assumes that it is being run with a live process.
820 Core files are handled via gdbarch. */
55f6301a 821 gdb_assert (target_has_execution ());
c906108c 822
4d1eb6b4 823 if (writebuf)
2ed4b548 824 return TARGET_XFER_E_IO;
c5aa993b 825
09473be8
TT
826 gdb::byte_vector ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
827 result = rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf.data (),
356a5233 828 readbuf, offset, len, 1);
9b409511
YQ
829
830 if (result == 0)
831 return TARGET_XFER_EOF;
832 else
833 {
834 *xfered_len = result;
835 return TARGET_XFER_OK;
836 }
c906108c 837}
c906108c 838
6c265988 839void _initialize_rs6000_nat ();
c906108c 840void
6c265988 841_initialize_rs6000_nat ()
c906108c 842{
d9f719f1 843 add_inf_child_target (&the_rs6000_nat_target);
c906108c 844}