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