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