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