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