]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rs6000-nat.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / rs6000-nat.c
CommitLineData
c906108c 1/* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
4646aa9d 2
28e7fd62 3 Copyright (C) 1986-2013 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"
42203e46 26#include "libbfd.h" /* For bfd_default_set_arch_mach (FIXME) */
c906108c 27#include "bfd.h"
60250e8b 28#include "exceptions.h"
c906108c 29#include "gdb-stabs.h"
4e052eda 30#include "regcache.h"
19caaa45 31#include "arch-utils.h"
dab06dbe 32#include "inf-child.h"
037a727e 33#include "inf-ptrace.h"
11bf77db 34#include "ppc-tdep.h"
6f7f3f0d 35#include "rs6000-tdep.h"
4646aa9d 36#include "exec.h"
06d3b283 37#include "observer.h"
63807e1d 38#include "xcoffread.h"
c906108c
SS
39
40#include <sys/ptrace.h>
41#include <sys/reg.h>
42
43#include <sys/param.h>
44#include <sys/dir.h>
45#include <sys/user.h>
46#include <signal.h>
47#include <sys/ioctl.h>
48#include <fcntl.h>
7a78ae4e 49#include <errno.h>
c906108c
SS
50
51#include <a.out.h>
52#include <sys/file.h>
53#include "gdb_stat.h"
92107356 54#include "gdb_bfd.h"
c906108c 55#include <sys/core.h>
7a78ae4e
ND
56#define __LDINFO_PTRACE32__ /* for __ld_info32 */
57#define __LDINFO_PTRACE64__ /* for __ld_info64 */
c906108c 58#include <sys/ldr.h>
7a78ae4e 59#include <sys/systemcfg.h>
4d1eb6b4 60#include "xml-utils.h"
c906108c 61
7a78ae4e
ND
62/* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
63 debugging 32-bit and 64-bit processes. Define a typedef and macros for
0df8b418 64 accessing fields in the appropriate structures. */
7a78ae4e
ND
65
66/* In 32-bit compilation mode (which is the only mode from which ptrace()
0df8b418 67 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
7a78ae4e
ND
68
69#ifdef __ld_info32
70# define ARCH3264
71#endif
72
0df8b418 73/* Return whether the current architecture is 64-bit. */
7a78ae4e
ND
74
75#ifndef ARCH3264
76# define ARCH64() 0
77#else
f5656ead 78# define ARCH64() (register_size (target_gdbarch (), 0) == 8)
7a78ae4e
ND
79#endif
80
0df8b418 81/* Union of 32-bit and 64-bit versions of ld_info. */
7a78ae4e
ND
82
83typedef union {
84#ifndef ARCH3264
85 struct ld_info l32;
86 struct ld_info l64;
87#else
88 struct __ld_info32 l32;
89 struct __ld_info64 l64;
90#endif
91} LdInfo;
92
93/* If compiling with 32-bit and 64-bit debugging capability (e.g. AIX 4.x),
94 declare and initialize a variable named VAR suitable for use as the arch64
0df8b418 95 parameter to the various LDI_*() macros. */
7a78ae4e
ND
96
97#ifndef ARCH3264
98# define ARCH64_DECL(var)
99#else
100# define ARCH64_DECL(var) int var = ARCH64 ()
101#endif
102
103/* Return LDI's FIELD for a 64-bit process if ARCH64 and for a 32-bit process
104 otherwise. This technique only works for FIELDs with the same data type in
0df8b418 105 32-bit and 64-bit versions of ld_info. */
7a78ae4e
ND
106
107#ifndef ARCH3264
108# define LDI_FIELD(ldi, arch64, field) (ldi)->l32.ldinfo_##field
109#else
110# define LDI_FIELD(ldi, arch64, field) \
111 (arch64 ? (ldi)->l64.ldinfo_##field : (ldi)->l32.ldinfo_##field)
112#endif
113
114/* Return various LDI fields for a 64-bit process if ARCH64 and for a 32-bit
0df8b418 115 process otherwise. */
7a78ae4e
ND
116
117#define LDI_NEXT(ldi, arch64) LDI_FIELD(ldi, arch64, next)
118#define LDI_FD(ldi, arch64) LDI_FIELD(ldi, arch64, fd)
119#define LDI_FILENAME(ldi, arch64) LDI_FIELD(ldi, arch64, filename)
c906108c 120
fb14de7b 121static void exec_one_dummy_insn (struct regcache *);
c906108c 122
4d1eb6b4
JB
123static LONGEST rs6000_xfer_shared_libraries
124 (struct target_ops *ops, enum target_object object,
125 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
126 ULONGEST offset, LONGEST len);
127
dd7be90a
KB
128/* Given REGNO, a gdb register number, return the corresponding
129 number suitable for use as a ptrace() parameter. Return -1 if
130 there's no suitable mapping. Also, set the int pointed to by
131 ISFLOAT to indicate whether REGNO is a floating point register. */
c906108c 132
dd7be90a 133static int
206988c4 134regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
c5aa993b 135{
206988c4 136 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
dd7be90a
KB
137
138 *isfloat = 0;
8bf659e8
JB
139 if (tdep->ppc_gp0_regnum <= regno
140 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
dd7be90a 141 return regno;
383f0f5b
JB
142 else if (tdep->ppc_fp0_regnum >= 0
143 && tdep->ppc_fp0_regnum <= regno
366f009f 144 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
dd7be90a
KB
145 {
146 *isfloat = 1;
366f009f 147 return regno - tdep->ppc_fp0_regnum + FPR0;
dd7be90a 148 }
206988c4 149 else if (regno == gdbarch_pc_regnum (gdbarch))
dd7be90a
KB
150 return IAR;
151 else if (regno == tdep->ppc_ps_regnum)
152 return MSR;
153 else if (regno == tdep->ppc_cr_regnum)
154 return CR;
155 else if (regno == tdep->ppc_lr_regnum)
156 return LR;
157 else if (regno == tdep->ppc_ctr_regnum)
158 return CTR;
159 else if (regno == tdep->ppc_xer_regnum)
160 return XER;
383f0f5b
JB
161 else if (tdep->ppc_fpscr_regnum >= 0
162 && regno == tdep->ppc_fpscr_regnum)
0e061eef 163 return FPSCR;
dd7be90a
KB
164 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
165 return MQ;
166 else
167 return -1;
168}
c906108c 169
0df8b418 170/* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
c906108c 171
7a78ae4e 172static int
8b5790f2 173rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
7a78ae4e
ND
174{
175 int ret = ptrace (req, id, (int *)addr, data, buf);
176#if 0
8b5790f2 177 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
7a78ae4e
ND
178 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
179#endif
180 return ret;
181}
c906108c 182
0df8b418 183/* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
c906108c 184
7a78ae4e 185static int
0d16ee5d 186rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
7a78ae4e
ND
187{
188#ifdef ARCH3264
189 int ret = ptracex (req, id, addr, data, buf);
190#else
191 int ret = 0;
192#endif
193#if 0
2244ba2e
PM
194 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
195 req, id, hex_string (addr), data, (unsigned int)buf, ret);
7a78ae4e
ND
196#endif
197 return ret;
198}
c906108c 199
0df8b418 200/* Fetch register REGNO from the inferior. */
c906108c 201
7a78ae4e 202static void
56be3814 203fetch_register (struct regcache *regcache, int regno)
7a78ae4e 204{
8b164abb 205 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d9d9c31f 206 int addr[MAX_REGISTER_SIZE];
dd7be90a 207 int nr, isfloat;
c906108c 208
0df8b418 209 /* Retrieved values may be -1, so infer errors from errno. */
7a78ae4e 210 errno = 0;
c906108c 211
206988c4 212 nr = regmap (gdbarch, regno, &isfloat);
dd7be90a 213
0df8b418 214 /* Floating-point registers. */
dd7be90a
KB
215 if (isfloat)
216 rs6000_ptrace32 (PT_READ_FPR, PIDGET (inferior_ptid), addr, nr, 0);
c906108c 217
0df8b418 218 /* Bogus register number. */
dd7be90a 219 else if (nr < 0)
2a18e3d9 220 {
8b164abb 221 if (regno >= gdbarch_num_regs (gdbarch))
2a18e3d9
EZ
222 fprintf_unfiltered (gdb_stderr,
223 "gdb error: register no %d not implemented.\n",
224 regno);
dd7be90a 225 return;
2a18e3d9 226 }
c906108c 227
0df8b418 228 /* Fixed-point registers. */
7a78ae4e
ND
229 else
230 {
7a78ae4e 231 if (!ARCH64 ())
0df8b418
MS
232 *addr = rs6000_ptrace32 (PT_READ_GPR, PIDGET (inferior_ptid),
233 (int *) nr, 0, 0);
7a78ae4e
ND
234 else
235 {
236 /* PT_READ_GPR requires the buffer parameter to point to long long,
0df8b418 237 even if the register is really only 32 bits. */
7a78ae4e 238 long long buf;
0d16ee5d 239 rs6000_ptrace64 (PT_READ_GPR, PIDGET (inferior_ptid), nr, 0, &buf);
8b164abb 240 if (register_size (gdbarch, regno) == 8)
7a78ae4e
ND
241 memcpy (addr, &buf, 8);
242 else
243 *addr = buf;
244 }
245 }
246
247 if (!errno)
56be3814 248 regcache_raw_supply (regcache, regno, (char *) addr);
7a78ae4e
ND
249 else
250 {
251#if 0
0df8b418 252 /* FIXME: this happens 3 times at the start of each 64-bit program. */
9b20d036 253 perror (_("ptrace read"));
7a78ae4e
ND
254#endif
255 errno = 0;
256 }
c906108c
SS
257}
258
0df8b418 259/* Store register REGNO back into the inferior. */
c906108c 260
7a78ae4e 261static void
fb14de7b 262store_register (struct regcache *regcache, int regno)
c906108c 263{
8b164abb 264 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d9d9c31f 265 int addr[MAX_REGISTER_SIZE];
dd7be90a 266 int nr, isfloat;
c906108c 267
11bf77db 268 /* Fetch the register's value from the register cache. */
56be3814 269 regcache_raw_collect (regcache, regno, addr);
11bf77db 270
0df8b418 271 /* -1 can be a successful return value, so infer errors from errno. */
c906108c
SS
272 errno = 0;
273
206988c4 274 nr = regmap (gdbarch, regno, &isfloat);
dd7be90a 275
0df8b418 276 /* Floating-point registers. */
dd7be90a
KB
277 if (isfloat)
278 rs6000_ptrace32 (PT_WRITE_FPR, PIDGET (inferior_ptid), addr, nr, 0);
c906108c 279
0df8b418 280 /* Bogus register number. */
dd7be90a 281 else if (nr < 0)
7a78ae4e 282 {
8b164abb 283 if (regno >= gdbarch_num_regs (gdbarch))
7a78ae4e
ND
284 fprintf_unfiltered (gdb_stderr,
285 "gdb error: register no %d not implemented.\n",
286 regno);
287 }
c906108c 288
0df8b418 289 /* Fixed-point registers. */
7a78ae4e
ND
290 else
291 {
8b164abb 292 if (regno == gdbarch_sp_regnum (gdbarch))
7a78ae4e
ND
293 /* Execute one dummy instruction (which is a breakpoint) in inferior
294 process to give kernel a chance to do internal housekeeping.
295 Otherwise the following ptrace(2) calls will mess up user stack
296 since kernel will get confused about the bottom of the stack
0df8b418 297 (%sp). */
fb14de7b 298 exec_one_dummy_insn (regcache);
c906108c 299
11bf77db
KB
300 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
301 the register's value is passed by value, but for 64-bit inferiors,
302 the address of a buffer containing the value is passed. */
7a78ae4e 303 if (!ARCH64 ())
0df8b418
MS
304 rs6000_ptrace32 (PT_WRITE_GPR, PIDGET (inferior_ptid),
305 (int *) nr, *addr, 0);
7a78ae4e 306 else
c906108c 307 {
7a78ae4e 308 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
0df8b418 309 area, even if the register is really only 32 bits. */
7a78ae4e 310 long long buf;
8b164abb 311 if (register_size (gdbarch, regno) == 8)
7a78ae4e
ND
312 memcpy (&buf, addr, 8);
313 else
314 buf = *addr;
0d16ee5d 315 rs6000_ptrace64 (PT_WRITE_GPR, PIDGET (inferior_ptid), nr, 0, &buf);
c906108c
SS
316 }
317 }
318
7a78ae4e 319 if (errno)
c906108c 320 {
9b20d036 321 perror (_("ptrace write"));
7a78ae4e 322 errno = 0;
c906108c 323 }
7a78ae4e 324}
c906108c 325
7a78ae4e 326/* Read from the inferior all registers if REGNO == -1 and just register
0df8b418 327 REGNO otherwise. */
c906108c 328
037a727e 329static void
28439f5e
PA
330rs6000_fetch_inferior_registers (struct target_ops *ops,
331 struct regcache *regcache, int regno)
7a78ae4e 332{
8b164abb 333 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7a78ae4e 334 if (regno != -1)
56be3814 335 fetch_register (regcache, regno);
7a78ae4e
ND
336
337 else
c906108c 338 {
8b164abb 339 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7a78ae4e 340
dd7be90a
KB
341 /* Read 32 general purpose registers. */
342 for (regno = tdep->ppc_gp0_regnum;
8bf659e8 343 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
dd7be90a
KB
344 regno++)
345 {
56be3814 346 fetch_register (regcache, regno);
dd7be90a
KB
347 }
348
349 /* Read general purpose floating point registers. */
383f0f5b
JB
350 if (tdep->ppc_fp0_regnum >= 0)
351 for (regno = 0; regno < ppc_num_fprs; regno++)
56be3814 352 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
7a78ae4e 353
dd7be90a 354 /* Read special registers. */
8b164abb 355 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
56be3814
UW
356 fetch_register (regcache, tdep->ppc_ps_regnum);
357 fetch_register (regcache, tdep->ppc_cr_regnum);
358 fetch_register (regcache, tdep->ppc_lr_regnum);
359 fetch_register (regcache, tdep->ppc_ctr_regnum);
360 fetch_register (regcache, tdep->ppc_xer_regnum);
383f0f5b 361 if (tdep->ppc_fpscr_regnum >= 0)
56be3814 362 fetch_register (regcache, tdep->ppc_fpscr_regnum);
dd7be90a 363 if (tdep->ppc_mq_regnum >= 0)
56be3814 364 fetch_register (regcache, tdep->ppc_mq_regnum);
c906108c 365 }
7a78ae4e 366}
c906108c 367
7a78ae4e
ND
368/* Store our register values back into the inferior.
369 If REGNO is -1, do this for all registers.
370 Otherwise, REGNO specifies which register (so we can save time). */
371
037a727e 372static void
28439f5e
PA
373rs6000_store_inferior_registers (struct target_ops *ops,
374 struct regcache *regcache, int regno)
7a78ae4e 375{
8b164abb 376 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7a78ae4e 377 if (regno != -1)
56be3814 378 store_register (regcache, regno);
7a78ae4e
ND
379
380 else
f6077098 381 {
8b164abb 382 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
dd7be90a
KB
383
384 /* Write general purpose registers first. */
385 for (regno = tdep->ppc_gp0_regnum;
8bf659e8 386 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
dd7be90a
KB
387 regno++)
388 {
56be3814 389 store_register (regcache, regno);
dd7be90a 390 }
7a78ae4e 391
dd7be90a 392 /* Write floating point registers. */
383f0f5b
JB
393 if (tdep->ppc_fp0_regnum >= 0)
394 for (regno = 0; regno < ppc_num_fprs; regno++)
56be3814 395 store_register (regcache, tdep->ppc_fp0_regnum + regno);
7a78ae4e 396
dd7be90a 397 /* Write special registers. */
8b164abb 398 store_register (regcache, gdbarch_pc_regnum (gdbarch));
56be3814
UW
399 store_register (regcache, tdep->ppc_ps_regnum);
400 store_register (regcache, tdep->ppc_cr_regnum);
401 store_register (regcache, tdep->ppc_lr_regnum);
402 store_register (regcache, tdep->ppc_ctr_regnum);
403 store_register (regcache, tdep->ppc_xer_regnum);
383f0f5b 404 if (tdep->ppc_fpscr_regnum >= 0)
56be3814 405 store_register (regcache, tdep->ppc_fpscr_regnum);
dd7be90a 406 if (tdep->ppc_mq_regnum >= 0)
56be3814 407 store_register (regcache, tdep->ppc_mq_regnum);
f6077098 408 }
7a78ae4e 409}
f6077098 410
7a78ae4e 411
037a727e
UW
412/* Attempt a transfer all LEN bytes starting at OFFSET between the
413 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
414 Return the number of bytes actually transferred. */
7a78ae4e 415
037a727e
UW
416static LONGEST
417rs6000_xfer_partial (struct target_ops *ops, enum target_object object,
418 const char *annex, gdb_byte *readbuf,
419 const gdb_byte *writebuf,
420 ULONGEST offset, LONGEST len)
7a78ae4e 421{
037a727e 422 pid_t pid = ptid_get_pid (inferior_ptid);
7a78ae4e 423 int arch64 = ARCH64 ();
7a78ae4e 424
037a727e 425 switch (object)
c906108c 426 {
4d1eb6b4
JB
427 case TARGET_OBJECT_AIX_LIBRARIES:
428 return rs6000_xfer_shared_libraries (ops, object, annex,
429 readbuf, writebuf,
430 offset, len);
037a727e
UW
431 case TARGET_OBJECT_MEMORY:
432 {
433 union
7a78ae4e 434 {
037a727e
UW
435 PTRACE_TYPE_RET word;
436 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
437 } buffer;
438 ULONGEST rounded_offset;
439 LONGEST partial_len;
440
441 /* Round the start offset down to the next long word
442 boundary. */
443 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
444
445 /* Since ptrace will transfer a single word starting at that
446 rounded_offset the partial_len needs to be adjusted down to
447 that (remember this function only does a single transfer).
448 Should the required length be even less, adjust it down
449 again. */
450 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
451 if (partial_len > len)
452 partial_len = len;
453
454 if (writebuf)
455 {
456 /* If OFFSET:PARTIAL_LEN is smaller than
457 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
458 be needed. Read in the entire word. */
459 if (rounded_offset < offset
460 || (offset + partial_len
461 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
462 {
463 /* Need part of initial word -- fetch it. */
464 if (arch64)
465 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
466 rounded_offset, 0, NULL);
467 else
468 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
0df8b418
MS
469 (int *) (uintptr_t)
470 rounded_offset,
037a727e
UW
471 0, NULL);
472 }
473
474 /* Copy data to be written over corresponding part of
475 buffer. */
476 memcpy (buffer.byte + (offset - rounded_offset),
477 writebuf, partial_len);
478
479 errno = 0;
480 if (arch64)
481 rs6000_ptrace64 (PT_WRITE_D, pid,
482 rounded_offset, buffer.word, NULL);
483 else
484 rs6000_ptrace32 (PT_WRITE_D, pid,
0df8b418
MS
485 (int *) (uintptr_t) rounded_offset,
486 buffer.word, NULL);
037a727e
UW
487 if (errno)
488 return 0;
489 }
490
491 if (readbuf)
492 {
493 errno = 0;
494 if (arch64)
495 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
496 rounded_offset, 0, NULL);
497 else
498 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
499 (int *)(uintptr_t)rounded_offset,
500 0, NULL);
501 if (errno)
502 return 0;
503
504 /* Copy appropriate bytes out of the buffer. */
505 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
506 partial_len);
507 }
508
509 return partial_len;
510 }
511
512 default:
513 return -1;
7a78ae4e 514 }
c906108c
SS
515}
516
482f7fee
UW
517/* Wait for the child specified by PTID to do something. Return the
518 process ID of the child, or MINUS_ONE_PTID in case of error; store
519 the status in *OURSTATUS. */
520
521static ptid_t
117de6a9 522rs6000_wait (struct target_ops *ops,
47608cb1 523 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
482f7fee
UW
524{
525 pid_t pid;
526 int status, save_errno;
527
528 do
529 {
530 set_sigint_trap ();
482f7fee
UW
531
532 do
533 {
534 pid = waitpid (ptid_get_pid (ptid), &status, 0);
535 save_errno = errno;
536 }
537 while (pid == -1 && errno == EINTR);
538
482f7fee
UW
539 clear_sigint_trap ();
540
541 if (pid == -1)
542 {
543 fprintf_unfiltered (gdb_stderr,
544 _("Child process unexpectedly missing: %s.\n"),
545 safe_strerror (save_errno));
546
547 /* Claim it exited with unknown signal. */
548 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
a493e3e2 549 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
fb66883a 550 return inferior_ptid;
482f7fee
UW
551 }
552
553 /* Ignore terminated detached child processes. */
554 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
555 pid = -1;
556 }
557 while (pid == -1);
558
559 /* AIX has a couple of strange returns from wait(). */
560
561 /* stop after load" status. */
562 if (status == 0x57c)
563 ourstatus->kind = TARGET_WAITKIND_LOADED;
0df8b418 564 /* signal 0. I have no idea why wait(2) returns with this status word. */
482f7fee
UW
565 else if (status == 0x7f)
566 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
567 /* A normal waitstatus. Let the usual macros deal with it. */
568 else
569 store_waitstatus (ourstatus, status);
570
571 return pid_to_ptid (pid);
572}
037a727e 573
c906108c
SS
574/* Execute one dummy breakpoint instruction. This way we give the kernel
575 a chance to do some housekeeping and update inferior's internal data,
0df8b418 576 including u_area. */
c906108c
SS
577
578static void
fb14de7b 579exec_one_dummy_insn (struct regcache *regcache)
c906108c 580{
4a7622d1 581#define DUMMY_INSN_ADDR AIX_TEXT_SEGMENT_BASE+0x200
c906108c 582
a6d9a66e 583 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7a78ae4e 584 int ret, status, pid;
c906108c 585 CORE_ADDR prev_pc;
8181d85f 586 void *bp;
c906108c 587
0df8b418 588 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
c906108c 589 assume that this address will never be executed again by the real
0df8b418 590 code. */
c906108c 591
47607d6f 592 bp = deprecated_insert_raw_breakpoint (gdbarch, NULL, DUMMY_INSN_ADDR);
c906108c 593
c906108c
SS
594 /* You might think this could be done with a single ptrace call, and
595 you'd be correct for just about every platform I've ever worked
596 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
597 the inferior never hits the breakpoint (it's also worth noting
598 powerpc-ibm-aix4.1.3 works correctly). */
fb14de7b
UW
599 prev_pc = regcache_read_pc (regcache);
600 regcache_write_pc (regcache, DUMMY_INSN_ADDR);
7a78ae4e 601 if (ARCH64 ())
8b5790f2 602 ret = rs6000_ptrace64 (PT_CONTINUE, PIDGET (inferior_ptid), 1, 0, NULL);
7a78ae4e 603 else
0df8b418
MS
604 ret = rs6000_ptrace32 (PT_CONTINUE, PIDGET (inferior_ptid),
605 (int *) 1, 0, NULL);
c906108c 606
7a78ae4e 607 if (ret != 0)
9b20d036 608 perror (_("pt_continue"));
c906108c 609
c5aa993b
JM
610 do
611 {
5be4dfca 612 pid = waitpid (PIDGET (inferior_ptid), &status, 0);
c5aa993b 613 }
39f77062 614 while (pid != PIDGET (inferior_ptid));
c5aa993b 615
fb14de7b 616 regcache_write_pc (regcache, prev_pc);
a6d9a66e 617 deprecated_remove_raw_breakpoint (gdbarch, bp);
c906108c 618}
c906108c 619\f
7a78ae4e 620
7a78ae4e 621/* Set the current architecture from the host running GDB. Called when
0df8b418 622 starting a child process. */
7a78ae4e 623
136d6dae
VP
624static void (*super_create_inferior) (struct target_ops *,char *exec_file,
625 char *allargs, char **env, int from_tty);
1f480a5e 626static void
136d6dae
VP
627rs6000_create_inferior (struct target_ops * ops, char *exec_file,
628 char *allargs, char **env, int from_tty)
7a78ae4e
ND
629{
630 enum bfd_architecture arch;
631 unsigned long mach;
632 bfd abfd;
633 struct gdbarch_info info;
634
136d6dae 635 super_create_inferior (ops, exec_file, allargs, env, from_tty);
1f480a5e 636
7a78ae4e
ND
637 if (__power_rs ())
638 {
639 arch = bfd_arch_rs6000;
640 mach = bfd_mach_rs6k;
641 }
642 else
643 {
644 arch = bfd_arch_powerpc;
645 mach = bfd_mach_ppc;
646 }
19caaa45
PS
647
648 /* FIXME: schauer/2002-02-25:
649 We don't know if we are executing a 32 or 64 bit executable,
650 and have no way to pass the proper word size to rs6000_gdbarch_init.
651 So we have to avoid switching to a new architecture, if the architecture
652 matches already.
653 Blindly calling rs6000_gdbarch_init used to work in older versions of
654 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
655 determine the wordsize. */
656 if (exec_bfd)
657 {
658 const struct bfd_arch_info *exec_bfd_arch_info;
659
660 exec_bfd_arch_info = bfd_get_arch_info (exec_bfd);
661 if (arch == exec_bfd_arch_info->arch)
662 return;
663 }
664
7a78ae4e
ND
665 bfd_default_set_arch_mach (&abfd, arch, mach);
666
fb6ecb0f 667 gdbarch_info_init (&info);
7a78ae4e 668 info.bfd_arch_info = bfd_get_arch_info (&abfd);
7aea86e6 669 info.abfd = exec_bfd;
7a78ae4e 670
16f33e29 671 if (!gdbarch_update_p (info))
e2e0b3e5 672 internal_error (__FILE__, __LINE__,
0df8b418
MS
673 _("rs6000_create_inferior: failed "
674 "to select architecture"));
7a78ae4e 675}
c906108c 676\f
c906108c 677
4d1eb6b4 678/* Shared Object support. */
c906108c 679
4d1eb6b4
JB
680/* Return the LdInfo data for the given process. Raises an error
681 if the data could not be obtained.
8d08c9ce 682
4d1eb6b4 683 The returned value must be deallocated after use. */
c906108c 684
4d1eb6b4
JB
685static LdInfo *
686rs6000_ptrace_ldinfo (ptid_t ptid)
687{
688 const int pid = ptid_get_pid (ptid);
689 int ldi_size = 1024;
690 LdInfo *ldi = xmalloc (ldi_size);
691 int rc = -1;
7a78ae4e 692
4d1eb6b4
JB
693 while (1)
694 {
695 if (ARCH64 ())
696 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, ldi_size,
697 NULL);
c18e0d23 698 else
4d1eb6b4
JB
699 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi, ldi_size, NULL);
700
701 if (rc != -1)
702 break; /* Success, we got the entire ld_info data. */
703
704 if (errno != ENOMEM)
705 perror_with_name (_("ptrace ldinfo"));
706
707 /* ldi is not big enough. Double it and try again. */
708 ldi_size *= 2;
709 ldi = xrealloc (ldi, ldi_size);
710 }
711
712 return ldi;
c906108c 713}
c906108c 714
4d1eb6b4
JB
715/* Assuming ABFD refers to a core file, return the LdInfo data
716 stored in that core file. Raises an error if the data could
717 not be read or extracted.
c906108c 718
4d1eb6b4
JB
719 The returned value much be deallocated after use. */
720
721static LdInfo *
722rs6000_core_ldinfo (bfd *abfd)
c906108c 723{
7be0c536 724 struct bfd_section *ldinfo_sec;
4d1eb6b4
JB
725 int ldinfo_size;
726 gdb_byte *ldinfo_buf;
727 struct cleanup *cleanup;
7a78ae4e 728
4d1eb6b4
JB
729 ldinfo_sec = bfd_get_section_by_name (abfd, ".ldinfo");
730 if (ldinfo_sec == NULL)
731 error (_("cannot find .ldinfo section from core file: %s\n"),
732 bfd_errmsg (bfd_get_error ()));
733 ldinfo_size = bfd_get_section_size (ldinfo_sec);
c906108c 734
4d1eb6b4
JB
735 ldinfo_buf = xmalloc (ldinfo_size);
736 cleanup = make_cleanup (xfree, ldinfo_buf);
c5aa993b 737
4d1eb6b4
JB
738 if (! bfd_get_section_contents (abfd, ldinfo_sec,
739 ldinfo_buf, 0, ldinfo_size))
740 error (_("unable to read .ldinfo section from core file: %s\n"),
741 bfd_errmsg (bfd_get_error ()));
742
743 discard_cleanups (cleanup);
744 return (LdInfo *) ldinfo_buf;
745}
746
747/* Append to OBJSTACK an XML string description of the shared library
748 corresponding to LDI, following the TARGET_OBJECT_AIX_LIBRARIES
749 format. */
750
751static void
752rs6000_xfer_shared_library (LdInfo *ldi, struct obstack *obstack)
753{
754 const int arch64 = ARCH64 ();
755 const char *archive_name = LDI_FILENAME (ldi, arch64);
756 const char *member_name = archive_name + strlen (archive_name) + 1;
757 CORE_ADDR text_addr, data_addr;
758 ULONGEST text_size, data_size;
759 char *p;
760
761 if (arch64)
c906108c 762 {
4d1eb6b4
JB
763 text_addr = ldi->l64.ldinfo_textorg;
764 text_size = ldi->l64.ldinfo_textsize;
765 data_addr = ldi->l64.ldinfo_dataorg;
766 data_size = ldi->l64.ldinfo_datasize;
c906108c 767 }
4d1eb6b4 768 else
c906108c 769 {
4d1eb6b4
JB
770 /* The text and data addresses are defined as pointers.
771 To avoid sign-extending their value in the assignments
772 below, we cast their value to unsigned long first. */
773 text_addr = (unsigned long) ldi->l32.ldinfo_textorg;
774 text_size = ldi->l32.ldinfo_textsize;
775 data_addr = (unsigned long) ldi->l32.ldinfo_dataorg;
776 data_size = ldi->l32.ldinfo_datasize;
777 }
c906108c 778
4d1eb6b4
JB
779 obstack_grow_str (obstack, "<library name=\"");
780 p = xml_escape_text (archive_name);
781 obstack_grow_str (obstack, p);
782 xfree (p);
783 obstack_grow_str (obstack, "\"");
c906108c 784
4d1eb6b4
JB
785 if (member_name[0] != '\0')
786 {
787 obstack_grow_str (obstack, " member=\"");
788 p = xml_escape_text (member_name);
789 obstack_grow_str (obstack, p);
790 xfree (p);
791 obstack_grow_str (obstack, "\"");
792 }
c906108c 793
4d1eb6b4
JB
794 obstack_grow_str (obstack, " text_addr=\"");
795 obstack_grow_str (obstack, core_addr_to_string (text_addr));
796 obstack_grow_str (obstack, "\"");
c906108c 797
4d1eb6b4
JB
798 obstack_grow_str (obstack, " text_size=\"");
799 obstack_grow_str (obstack, pulongest (text_size));
800 obstack_grow_str (obstack, "\"");
c5aa993b 801
4d1eb6b4
JB
802 obstack_grow_str (obstack, " data_addr=\"");
803 obstack_grow_str (obstack, core_addr_to_string (data_addr));
804 obstack_grow_str (obstack, "\"");
c906108c 805
4d1eb6b4
JB
806 obstack_grow_str (obstack, " data_size=\"");
807 obstack_grow_str (obstack, pulongest (data_size));
808 obstack_grow_str (obstack, "\"");
63f58cc5 809
4d1eb6b4
JB
810 obstack_grow_str (obstack, "></library>");
811}
c906108c 812
4d1eb6b4
JB
813/* Implement the to_xfer_partial target_ops method for
814 TARGET_OBJECT_AIX_LIBRARIES objects. */
6426a772 815
4d1eb6b4
JB
816static LONGEST
817rs6000_xfer_shared_libraries
818 (struct target_ops *ops, enum target_object object,
819 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
820 ULONGEST offset, LONGEST len)
821{
822 const int arch64 = ARCH64 ();
823 LdInfo *ldi_data;
824 LdInfo *ldi;
825 struct obstack obstack;
826 const char *buf;
827 LONGEST len_avail;
c906108c 828
4d1eb6b4
JB
829 if (writebuf)
830 return -1;
c5aa993b 831
4d1eb6b4
JB
832 /* Get the ldinfo raw data: If debugging a live process, we get it
833 using ptrace. Otherwise, the info is stored in the .ldinfo
834 section of the core file. */
835
836 if (target_has_execution)
837 ldi_data = rs6000_ptrace_ldinfo (inferior_ptid);
838 else
839 ldi_data = rs6000_core_ldinfo (core_bfd);
840
841 /* Convert the raw data into an XML representation. */
c906108c 842
4d1eb6b4
JB
843 obstack_init (&obstack);
844 obstack_grow_str (&obstack, "<library-list version=\"1.0\">\n");
e42dc924 845
4d1eb6b4
JB
846 ldi = ldi_data;
847 while (1)
848 {
849 /* Close the fd. We cannot use it, because we cannot assume
850 that the user of this descriptor will be in the same
851 process. */
852 close (LDI_FD (ldi, arch64));
853
854 rs6000_xfer_shared_library (ldi, &obstack);
855
856 if (!LDI_NEXT (ldi, arch64))
857 break;
858 ldi = (LdInfo *) ((char *) ldi + LDI_NEXT (ldi, arch64));
c5aa993b 859 }
c906108c 860
4d1eb6b4
JB
861 xfree (ldi_data);
862
863 obstack_grow_str0 (&obstack, "</library-list>\n");
c906108c 864
4d1eb6b4
JB
865 buf = obstack_finish (&obstack);
866 len_avail = strlen (buf);
867 if (offset >= len_avail)
868 len= 0;
869 else
c906108c 870 {
4d1eb6b4
JB
871 if (len > len_avail - offset)
872 len = len_avail - offset;
873 memcpy (readbuf, buf + offset, len);
c906108c 874 }
4d1eb6b4
JB
875
876 obstack_free (&obstack, NULL);
877 return len;
c906108c 878}
c906108c 879
e1aca11e
JB
880void _initialize_rs6000_nat (void);
881
c906108c 882void
7a61a01c 883_initialize_rs6000_nat (void)
c906108c 884{
037a727e
UW
885 struct target_ops *t;
886
887 t = inf_ptrace_target ();
888 t->to_fetch_registers = rs6000_fetch_inferior_registers;
889 t->to_store_registers = rs6000_store_inferior_registers;
890 t->to_xfer_partial = rs6000_xfer_partial;
1f480a5e
UW
891
892 super_create_inferior = t->to_create_inferior;
893 t->to_create_inferior = rs6000_create_inferior;
894
482f7fee
UW
895 t->to_wait = rs6000_wait;
896
037a727e 897 add_target (t);
c906108c 898}