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