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