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