Rename 'descr' field in regset structure to 'regmap'.
[deliverable/binutils-gdb.git] / gdb / rs6000-nat.c
CommitLineData
c906108c 1/* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
4646aa9d 2
ecd75fc8 3 Copyright (C) 1986-2014 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"
356a5233 36#include "rs6000-aix-tdep.h"
4646aa9d 37#include "exec.h"
06d3b283 38#include "observer.h"
63807e1d 39#include "xcoffread.h"
c906108c
SS
40
41#include <sys/ptrace.h>
42#include <sys/reg.h>
43
c906108c
SS
44#include <sys/dir.h>
45#include <sys/user.h>
46#include <signal.h>
47#include <sys/ioctl.h>
48#include <fcntl.h>
49
50#include <a.out.h>
51#include <sys/file.h>
53ce3c39 52#include <sys/stat.h>
92107356 53#include "gdb_bfd.h"
c906108c 54#include <sys/core.h>
7a78ae4e
ND
55#define __LDINFO_PTRACE32__ /* for __ld_info32 */
56#define __LDINFO_PTRACE64__ /* for __ld_info64 */
c906108c 57#include <sys/ldr.h>
7a78ae4e 58#include <sys/systemcfg.h>
c906108c 59
7a78ae4e
ND
60/* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
61 debugging 32-bit and 64-bit processes. Define a typedef and macros for
0df8b418 62 accessing fields in the appropriate structures. */
7a78ae4e
ND
63
64/* In 32-bit compilation mode (which is the only mode from which ptrace()
0df8b418 65 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
7a78ae4e 66
b08ee99f 67#if defined (__ld_info32) || defined (__ld_info64)
7a78ae4e
ND
68# define ARCH3264
69#endif
70
0df8b418 71/* Return whether the current architecture is 64-bit. */
7a78ae4e
ND
72
73#ifndef ARCH3264
74# define ARCH64() 0
75#else
f5656ead 76# define ARCH64() (register_size (target_gdbarch (), 0) == 8)
7a78ae4e
ND
77#endif
78
fb14de7b 79static void exec_one_dummy_insn (struct regcache *);
c906108c 80
05804640 81static target_xfer_partial_ftype rs6000_xfer_shared_libraries;
4d1eb6b4 82
dd7be90a
KB
83/* Given REGNO, a gdb register number, return the corresponding
84 number suitable for use as a ptrace() parameter. Return -1 if
85 there's no suitable mapping. Also, set the int pointed to by
86 ISFLOAT to indicate whether REGNO is a floating point register. */
c906108c 87
dd7be90a 88static int
206988c4 89regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
c5aa993b 90{
206988c4 91 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
dd7be90a
KB
92
93 *isfloat = 0;
8bf659e8
JB
94 if (tdep->ppc_gp0_regnum <= regno
95 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
dd7be90a 96 return regno;
383f0f5b
JB
97 else if (tdep->ppc_fp0_regnum >= 0
98 && tdep->ppc_fp0_regnum <= regno
366f009f 99 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
dd7be90a
KB
100 {
101 *isfloat = 1;
366f009f 102 return regno - tdep->ppc_fp0_regnum + FPR0;
dd7be90a 103 }
206988c4 104 else if (regno == gdbarch_pc_regnum (gdbarch))
dd7be90a
KB
105 return IAR;
106 else if (regno == tdep->ppc_ps_regnum)
107 return MSR;
108 else if (regno == tdep->ppc_cr_regnum)
109 return CR;
110 else if (regno == tdep->ppc_lr_regnum)
111 return LR;
112 else if (regno == tdep->ppc_ctr_regnum)
113 return CTR;
114 else if (regno == tdep->ppc_xer_regnum)
115 return XER;
383f0f5b
JB
116 else if (tdep->ppc_fpscr_regnum >= 0
117 && regno == tdep->ppc_fpscr_regnum)
0e061eef 118 return FPSCR;
dd7be90a
KB
119 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
120 return MQ;
121 else
122 return -1;
123}
c906108c 124
0df8b418 125/* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
c906108c 126
7a78ae4e 127static int
8b5790f2 128rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
7a78ae4e 129{
1ed3ee94 130#ifdef HAVE_PTRACE64
11cb8762 131 int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
1ed3ee94 132#else
7a78ae4e 133 int ret = ptrace (req, id, (int *)addr, data, buf);
1ed3ee94 134#endif
7a78ae4e 135#if 0
8b5790f2 136 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
7a78ae4e
ND
137 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
138#endif
139 return ret;
140}
c906108c 141
0df8b418 142/* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
c906108c 143
7a78ae4e 144static int
0d16ee5d 145rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
7a78ae4e
ND
146{
147#ifdef ARCH3264
1ed3ee94 148# ifdef HAVE_PTRACE64
b08ee99f 149 int ret = ptrace64 (req, id, addr, data, buf);
1ed3ee94 150# else
7a78ae4e 151 int ret = ptracex (req, id, addr, data, buf);
1ed3ee94 152# endif
7a78ae4e
ND
153#else
154 int ret = 0;
155#endif
156#if 0
2244ba2e
PM
157 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
158 req, id, hex_string (addr), data, (unsigned int)buf, ret);
7a78ae4e
ND
159#endif
160 return ret;
161}
c906108c 162
0df8b418 163/* Fetch register REGNO from the inferior. */
c906108c 164
7a78ae4e 165static void
56be3814 166fetch_register (struct regcache *regcache, int regno)
7a78ae4e 167{
8b164abb 168 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d9d9c31f 169 int addr[MAX_REGISTER_SIZE];
dd7be90a 170 int nr, isfloat;
c906108c 171
0df8b418 172 /* Retrieved values may be -1, so infer errors from errno. */
7a78ae4e 173 errno = 0;
c906108c 174
206988c4 175 nr = regmap (gdbarch, regno, &isfloat);
dd7be90a 176
0df8b418 177 /* Floating-point registers. */
dd7be90a 178 if (isfloat)
dfd4cc63 179 rs6000_ptrace32 (PT_READ_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
c906108c 180
0df8b418 181 /* Bogus register number. */
dd7be90a 182 else if (nr < 0)
2a18e3d9 183 {
8b164abb 184 if (regno >= gdbarch_num_regs (gdbarch))
2a18e3d9
EZ
185 fprintf_unfiltered (gdb_stderr,
186 "gdb error: register no %d not implemented.\n",
187 regno);
dd7be90a 188 return;
2a18e3d9 189 }
c906108c 190
0df8b418 191 /* Fixed-point registers. */
7a78ae4e
ND
192 else
193 {
7a78ae4e 194 if (!ARCH64 ())
dfd4cc63 195 *addr = rs6000_ptrace32 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
0df8b418 196 (int *) nr, 0, 0);
7a78ae4e
ND
197 else
198 {
199 /* PT_READ_GPR requires the buffer parameter to point to long long,
0df8b418 200 even if the register is really only 32 bits. */
7a78ae4e 201 long long buf;
dfd4cc63
LM
202 rs6000_ptrace64 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
203 nr, 0, &buf);
8b164abb 204 if (register_size (gdbarch, regno) == 8)
7a78ae4e
ND
205 memcpy (addr, &buf, 8);
206 else
207 *addr = buf;
208 }
209 }
210
211 if (!errno)
56be3814 212 regcache_raw_supply (regcache, regno, (char *) addr);
7a78ae4e
ND
213 else
214 {
215#if 0
0df8b418 216 /* FIXME: this happens 3 times at the start of each 64-bit program. */
9b20d036 217 perror (_("ptrace read"));
7a78ae4e
ND
218#endif
219 errno = 0;
220 }
c906108c
SS
221}
222
0df8b418 223/* Store register REGNO back into the inferior. */
c906108c 224
7a78ae4e 225static void
fb14de7b 226store_register (struct regcache *regcache, int regno)
c906108c 227{
8b164abb 228 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d9d9c31f 229 int addr[MAX_REGISTER_SIZE];
dd7be90a 230 int nr, isfloat;
c906108c 231
11bf77db 232 /* Fetch the register's value from the register cache. */
56be3814 233 regcache_raw_collect (regcache, regno, addr);
11bf77db 234
0df8b418 235 /* -1 can be a successful return value, so infer errors from errno. */
c906108c
SS
236 errno = 0;
237
206988c4 238 nr = regmap (gdbarch, regno, &isfloat);
dd7be90a 239
0df8b418 240 /* Floating-point registers. */
dd7be90a 241 if (isfloat)
dfd4cc63 242 rs6000_ptrace32 (PT_WRITE_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
c906108c 243
0df8b418 244 /* Bogus register number. */
dd7be90a 245 else if (nr < 0)
7a78ae4e 246 {
8b164abb 247 if (regno >= gdbarch_num_regs (gdbarch))
7a78ae4e
ND
248 fprintf_unfiltered (gdb_stderr,
249 "gdb error: register no %d not implemented.\n",
250 regno);
251 }
c906108c 252
0df8b418 253 /* Fixed-point registers. */
7a78ae4e
ND
254 else
255 {
8b164abb 256 if (regno == gdbarch_sp_regnum (gdbarch))
7a78ae4e
ND
257 /* Execute one dummy instruction (which is a breakpoint) in inferior
258 process to give kernel a chance to do internal housekeeping.
259 Otherwise the following ptrace(2) calls will mess up user stack
260 since kernel will get confused about the bottom of the stack
0df8b418 261 (%sp). */
fb14de7b 262 exec_one_dummy_insn (regcache);
c906108c 263
11bf77db
KB
264 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
265 the register's value is passed by value, but for 64-bit inferiors,
266 the address of a buffer containing the value is passed. */
7a78ae4e 267 if (!ARCH64 ())
dfd4cc63 268 rs6000_ptrace32 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
0df8b418 269 (int *) nr, *addr, 0);
7a78ae4e 270 else
c906108c 271 {
7a78ae4e 272 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
0df8b418 273 area, even if the register is really only 32 bits. */
7a78ae4e 274 long long buf;
8b164abb 275 if (register_size (gdbarch, regno) == 8)
7a78ae4e
ND
276 memcpy (&buf, addr, 8);
277 else
278 buf = *addr;
dfd4cc63
LM
279 rs6000_ptrace64 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
280 nr, 0, &buf);
c906108c
SS
281 }
282 }
283
7a78ae4e 284 if (errno)
c906108c 285 {
9b20d036 286 perror (_("ptrace write"));
7a78ae4e 287 errno = 0;
c906108c 288 }
7a78ae4e 289}
c906108c 290
7a78ae4e 291/* Read from the inferior all registers if REGNO == -1 and just register
0df8b418 292 REGNO otherwise. */
c906108c 293
037a727e 294static void
28439f5e
PA
295rs6000_fetch_inferior_registers (struct target_ops *ops,
296 struct regcache *regcache, int regno)
7a78ae4e 297{
8b164abb 298 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7a78ae4e 299 if (regno != -1)
56be3814 300 fetch_register (regcache, regno);
7a78ae4e
ND
301
302 else
c906108c 303 {
8b164abb 304 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7a78ae4e 305
dd7be90a
KB
306 /* Read 32 general purpose registers. */
307 for (regno = tdep->ppc_gp0_regnum;
8bf659e8 308 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
dd7be90a
KB
309 regno++)
310 {
56be3814 311 fetch_register (regcache, regno);
dd7be90a
KB
312 }
313
314 /* Read general purpose floating point registers. */
383f0f5b
JB
315 if (tdep->ppc_fp0_regnum >= 0)
316 for (regno = 0; regno < ppc_num_fprs; regno++)
56be3814 317 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
7a78ae4e 318
dd7be90a 319 /* Read special registers. */
8b164abb 320 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
56be3814
UW
321 fetch_register (regcache, tdep->ppc_ps_regnum);
322 fetch_register (regcache, tdep->ppc_cr_regnum);
323 fetch_register (regcache, tdep->ppc_lr_regnum);
324 fetch_register (regcache, tdep->ppc_ctr_regnum);
325 fetch_register (regcache, tdep->ppc_xer_regnum);
383f0f5b 326 if (tdep->ppc_fpscr_regnum >= 0)
56be3814 327 fetch_register (regcache, tdep->ppc_fpscr_regnum);
dd7be90a 328 if (tdep->ppc_mq_regnum >= 0)
56be3814 329 fetch_register (regcache, tdep->ppc_mq_regnum);
c906108c 330 }
7a78ae4e 331}
c906108c 332
7a78ae4e
ND
333/* Store our register values back into the inferior.
334 If REGNO is -1, do this for all registers.
335 Otherwise, REGNO specifies which register (so we can save time). */
336
037a727e 337static void
28439f5e
PA
338rs6000_store_inferior_registers (struct target_ops *ops,
339 struct regcache *regcache, int regno)
7a78ae4e 340{
8b164abb 341 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7a78ae4e 342 if (regno != -1)
56be3814 343 store_register (regcache, regno);
7a78ae4e
ND
344
345 else
f6077098 346 {
8b164abb 347 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
dd7be90a
KB
348
349 /* Write general purpose registers first. */
350 for (regno = tdep->ppc_gp0_regnum;
8bf659e8 351 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
dd7be90a
KB
352 regno++)
353 {
56be3814 354 store_register (regcache, regno);
dd7be90a 355 }
7a78ae4e 356
dd7be90a 357 /* Write floating point registers. */
383f0f5b
JB
358 if (tdep->ppc_fp0_regnum >= 0)
359 for (regno = 0; regno < ppc_num_fprs; regno++)
56be3814 360 store_register (regcache, tdep->ppc_fp0_regnum + regno);
7a78ae4e 361
dd7be90a 362 /* Write special registers. */
8b164abb 363 store_register (regcache, gdbarch_pc_regnum (gdbarch));
56be3814
UW
364 store_register (regcache, tdep->ppc_ps_regnum);
365 store_register (regcache, tdep->ppc_cr_regnum);
366 store_register (regcache, tdep->ppc_lr_regnum);
367 store_register (regcache, tdep->ppc_ctr_regnum);
368 store_register (regcache, tdep->ppc_xer_regnum);
383f0f5b 369 if (tdep->ppc_fpscr_regnum >= 0)
56be3814 370 store_register (regcache, tdep->ppc_fpscr_regnum);
dd7be90a 371 if (tdep->ppc_mq_regnum >= 0)
56be3814 372 store_register (regcache, tdep->ppc_mq_regnum);
f6077098 373 }
7a78ae4e 374}
f6077098 375
edcc890f 376/* Implement the to_xfer_partial target_ops method. */
7a78ae4e 377
9b409511 378static enum target_xfer_status
037a727e
UW
379rs6000_xfer_partial (struct target_ops *ops, enum target_object object,
380 const char *annex, gdb_byte *readbuf,
381 const gdb_byte *writebuf,
9b409511 382 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
7a78ae4e 383{
037a727e 384 pid_t pid = ptid_get_pid (inferior_ptid);
7a78ae4e 385 int arch64 = ARCH64 ();
7a78ae4e 386
037a727e 387 switch (object)
c906108c 388 {
ff99b71b 389 case TARGET_OBJECT_LIBRARIES_AIX:
4d1eb6b4
JB
390 return rs6000_xfer_shared_libraries (ops, object, annex,
391 readbuf, writebuf,
9b409511 392 offset, len, xfered_len);
037a727e
UW
393 case TARGET_OBJECT_MEMORY:
394 {
395 union
7a78ae4e 396 {
037a727e
UW
397 PTRACE_TYPE_RET word;
398 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
399 } buffer;
400 ULONGEST rounded_offset;
401 LONGEST partial_len;
402
403 /* Round the start offset down to the next long word
404 boundary. */
405 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
406
407 /* Since ptrace will transfer a single word starting at that
408 rounded_offset the partial_len needs to be adjusted down to
409 that (remember this function only does a single transfer).
410 Should the required length be even less, adjust it down
411 again. */
412 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
413 if (partial_len > len)
414 partial_len = len;
415
416 if (writebuf)
417 {
418 /* If OFFSET:PARTIAL_LEN is smaller than
419 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
420 be needed. Read in the entire word. */
421 if (rounded_offset < offset
422 || (offset + partial_len
423 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
424 {
425 /* Need part of initial word -- fetch it. */
426 if (arch64)
427 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
428 rounded_offset, 0, NULL);
429 else
430 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
0df8b418
MS
431 (int *) (uintptr_t)
432 rounded_offset,
037a727e
UW
433 0, NULL);
434 }
435
436 /* Copy data to be written over corresponding part of
437 buffer. */
438 memcpy (buffer.byte + (offset - rounded_offset),
439 writebuf, partial_len);
440
441 errno = 0;
442 if (arch64)
443 rs6000_ptrace64 (PT_WRITE_D, pid,
444 rounded_offset, buffer.word, NULL);
445 else
446 rs6000_ptrace32 (PT_WRITE_D, pid,
0df8b418
MS
447 (int *) (uintptr_t) rounded_offset,
448 buffer.word, NULL);
037a727e 449 if (errno)
9b409511 450 return TARGET_XFER_EOF;
037a727e
UW
451 }
452
453 if (readbuf)
454 {
455 errno = 0;
456 if (arch64)
457 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
458 rounded_offset, 0, NULL);
459 else
460 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
461 (int *)(uintptr_t)rounded_offset,
462 0, NULL);
463 if (errno)
9b409511 464 return TARGET_XFER_EOF;
037a727e
UW
465
466 /* Copy appropriate bytes out of the buffer. */
467 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
468 partial_len);
469 }
470
9b409511
YQ
471 *xfered_len = (ULONGEST) partial_len;
472 return TARGET_XFER_OK;
037a727e
UW
473 }
474
475 default:
2ed4b548 476 return TARGET_XFER_E_IO;
7a78ae4e 477 }
c906108c
SS
478}
479
482f7fee
UW
480/* Wait for the child specified by PTID to do something. Return the
481 process ID of the child, or MINUS_ONE_PTID in case of error; store
482 the status in *OURSTATUS. */
483
484static ptid_t
117de6a9 485rs6000_wait (struct target_ops *ops,
47608cb1 486 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
482f7fee
UW
487{
488 pid_t pid;
489 int status, save_errno;
490
491 do
492 {
493 set_sigint_trap ();
482f7fee
UW
494
495 do
496 {
497 pid = waitpid (ptid_get_pid (ptid), &status, 0);
498 save_errno = errno;
499 }
500 while (pid == -1 && errno == EINTR);
501
482f7fee
UW
502 clear_sigint_trap ();
503
504 if (pid == -1)
505 {
506 fprintf_unfiltered (gdb_stderr,
507 _("Child process unexpectedly missing: %s.\n"),
508 safe_strerror (save_errno));
509
510 /* Claim it exited with unknown signal. */
511 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
a493e3e2 512 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
fb66883a 513 return inferior_ptid;
482f7fee
UW
514 }
515
516 /* Ignore terminated detached child processes. */
517 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
518 pid = -1;
519 }
520 while (pid == -1);
521
522 /* AIX has a couple of strange returns from wait(). */
523
524 /* stop after load" status. */
525 if (status == 0x57c)
526 ourstatus->kind = TARGET_WAITKIND_LOADED;
0df8b418 527 /* signal 0. I have no idea why wait(2) returns with this status word. */
482f7fee
UW
528 else if (status == 0x7f)
529 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
530 /* A normal waitstatus. Let the usual macros deal with it. */
531 else
532 store_waitstatus (ourstatus, status);
533
534 return pid_to_ptid (pid);
535}
037a727e 536
c906108c
SS
537/* Execute one dummy breakpoint instruction. This way we give the kernel
538 a chance to do some housekeeping and update inferior's internal data,
0df8b418 539 including u_area. */
c906108c
SS
540
541static void
fb14de7b 542exec_one_dummy_insn (struct regcache *regcache)
c906108c 543{
4a7622d1 544#define DUMMY_INSN_ADDR AIX_TEXT_SEGMENT_BASE+0x200
c906108c 545
a6d9a66e 546 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7a78ae4e 547 int ret, status, pid;
c906108c 548 CORE_ADDR prev_pc;
8181d85f 549 void *bp;
c906108c 550
0df8b418 551 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
c906108c 552 assume that this address will never be executed again by the real
0df8b418 553 code. */
c906108c 554
47607d6f 555 bp = deprecated_insert_raw_breakpoint (gdbarch, NULL, DUMMY_INSN_ADDR);
c906108c 556
c906108c
SS
557 /* You might think this could be done with a single ptrace call, and
558 you'd be correct for just about every platform I've ever worked
559 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
560 the inferior never hits the breakpoint (it's also worth noting
561 powerpc-ibm-aix4.1.3 works correctly). */
fb14de7b
UW
562 prev_pc = regcache_read_pc (regcache);
563 regcache_write_pc (regcache, DUMMY_INSN_ADDR);
7a78ae4e 564 if (ARCH64 ())
dfd4cc63
LM
565 ret = rs6000_ptrace64 (PT_CONTINUE, ptid_get_pid (inferior_ptid),
566 1, 0, NULL);
7a78ae4e 567 else
dfd4cc63 568 ret = rs6000_ptrace32 (PT_CONTINUE, ptid_get_pid (inferior_ptid),
0df8b418 569 (int *) 1, 0, NULL);
c906108c 570
7a78ae4e 571 if (ret != 0)
9b20d036 572 perror (_("pt_continue"));
c906108c 573
c5aa993b
JM
574 do
575 {
dfd4cc63 576 pid = waitpid (ptid_get_pid (inferior_ptid), &status, 0);
c5aa993b 577 }
dfd4cc63 578 while (pid != ptid_get_pid (inferior_ptid));
c5aa993b 579
fb14de7b 580 regcache_write_pc (regcache, prev_pc);
a6d9a66e 581 deprecated_remove_raw_breakpoint (gdbarch, bp);
c906108c 582}
c906108c 583\f
7a78ae4e 584
7a78ae4e 585/* Set the current architecture from the host running GDB. Called when
0df8b418 586 starting a child process. */
7a78ae4e 587
136d6dae
VP
588static void (*super_create_inferior) (struct target_ops *,char *exec_file,
589 char *allargs, char **env, int from_tty);
1f480a5e 590static void
136d6dae
VP
591rs6000_create_inferior (struct target_ops * ops, char *exec_file,
592 char *allargs, char **env, int from_tty)
7a78ae4e
ND
593{
594 enum bfd_architecture arch;
595 unsigned long mach;
596 bfd abfd;
597 struct gdbarch_info info;
598
136d6dae 599 super_create_inferior (ops, exec_file, allargs, env, from_tty);
1f480a5e 600
7a78ae4e
ND
601 if (__power_rs ())
602 {
603 arch = bfd_arch_rs6000;
604 mach = bfd_mach_rs6k;
605 }
606 else
607 {
608 arch = bfd_arch_powerpc;
609 mach = bfd_mach_ppc;
610 }
19caaa45
PS
611
612 /* FIXME: schauer/2002-02-25:
613 We don't know if we are executing a 32 or 64 bit executable,
614 and have no way to pass the proper word size to rs6000_gdbarch_init.
615 So we have to avoid switching to a new architecture, if the architecture
616 matches already.
617 Blindly calling rs6000_gdbarch_init used to work in older versions of
618 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
619 determine the wordsize. */
620 if (exec_bfd)
621 {
622 const struct bfd_arch_info *exec_bfd_arch_info;
623
624 exec_bfd_arch_info = bfd_get_arch_info (exec_bfd);
625 if (arch == exec_bfd_arch_info->arch)
626 return;
627 }
628
7a78ae4e
ND
629 bfd_default_set_arch_mach (&abfd, arch, mach);
630
fb6ecb0f 631 gdbarch_info_init (&info);
7a78ae4e 632 info.bfd_arch_info = bfd_get_arch_info (&abfd);
7aea86e6 633 info.abfd = exec_bfd;
7a78ae4e 634
16f33e29 635 if (!gdbarch_update_p (info))
e2e0b3e5 636 internal_error (__FILE__, __LINE__,
0df8b418
MS
637 _("rs6000_create_inferior: failed "
638 "to select architecture"));
7a78ae4e 639}
c906108c 640\f
c906108c 641
4d1eb6b4 642/* Shared Object support. */
c906108c 643
4d1eb6b4
JB
644/* Return the LdInfo data for the given process. Raises an error
645 if the data could not be obtained.
8d08c9ce 646
4d1eb6b4 647 The returned value must be deallocated after use. */
c906108c 648
356a5233 649static gdb_byte *
4d1eb6b4
JB
650rs6000_ptrace_ldinfo (ptid_t ptid)
651{
652 const int pid = ptid_get_pid (ptid);
653 int ldi_size = 1024;
356a5233 654 gdb_byte *ldi = xmalloc (ldi_size);
4d1eb6b4 655 int rc = -1;
7a78ae4e 656
4d1eb6b4
JB
657 while (1)
658 {
659 if (ARCH64 ())
660 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, ldi_size,
661 NULL);
c18e0d23 662 else
4d1eb6b4
JB
663 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi, ldi_size, NULL);
664
665 if (rc != -1)
666 break; /* Success, we got the entire ld_info data. */
667
668 if (errno != ENOMEM)
669 perror_with_name (_("ptrace ldinfo"));
670
671 /* ldi is not big enough. Double it and try again. */
672 ldi_size *= 2;
673 ldi = xrealloc (ldi, ldi_size);
674 }
675
676 return ldi;
c906108c 677}
c906108c 678
4d1eb6b4 679/* Implement the to_xfer_partial target_ops method for
ff99b71b 680 TARGET_OBJECT_LIBRARIES_AIX objects. */
6426a772 681
9b409511 682static enum target_xfer_status
4d1eb6b4
JB
683rs6000_xfer_shared_libraries
684 (struct target_ops *ops, enum target_object object,
685 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
9b409511 686 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
4d1eb6b4 687{
356a5233
JB
688 gdb_byte *ldi_buf;
689 ULONGEST result;
690 struct cleanup *cleanup;
691
692 /* This function assumes that it is being run with a live process.
693 Core files are handled via gdbarch. */
694 gdb_assert (target_has_execution);
c906108c 695
4d1eb6b4 696 if (writebuf)
2ed4b548 697 return TARGET_XFER_E_IO;
c5aa993b 698
356a5233
JB
699 ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
700 gdb_assert (ldi_buf != NULL);
701 cleanup = make_cleanup (xfree, ldi_buf);
702 result = rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf,
703 readbuf, offset, len, 1);
704 xfree (ldi_buf);
4d1eb6b4 705
356a5233 706 do_cleanups (cleanup);
9b409511
YQ
707
708 if (result == 0)
709 return TARGET_XFER_EOF;
710 else
711 {
712 *xfered_len = result;
713 return TARGET_XFER_OK;
714 }
c906108c 715}
c906108c 716
e1aca11e
JB
717void _initialize_rs6000_nat (void);
718
c906108c 719void
7a61a01c 720_initialize_rs6000_nat (void)
c906108c 721{
037a727e
UW
722 struct target_ops *t;
723
724 t = inf_ptrace_target ();
725 t->to_fetch_registers = rs6000_fetch_inferior_registers;
726 t->to_store_registers = rs6000_store_inferior_registers;
727 t->to_xfer_partial = rs6000_xfer_partial;
1f480a5e
UW
728
729 super_create_inferior = t->to_create_inferior;
730 t->to_create_inferior = rs6000_create_inferior;
731
482f7fee
UW
732 t->to_wait = rs6000_wait;
733
037a727e 734 add_target (t);
c906108c 735}
This page took 1.475447 seconds and 4 git commands to generate.