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