Make GDB wait for events after handling target File-I/O
[deliverable/binutils-gdb.git] / gdb / rs6000-nat.c
1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2015 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 "libbfd.h" /* For bfd_default_set_arch_mach (FIXME) */
27 #include "bfd.h"
28 #include "gdb-stabs.h"
29 #include "regcache.h"
30 #include "arch-utils.h"
31 #include "inf-child.h"
32 #include "inf-ptrace.h"
33 #include "ppc-tdep.h"
34 #include "rs6000-tdep.h"
35 #include "rs6000-aix-tdep.h"
36 #include "exec.h"
37 #include "observer.h"
38 #include "xcoffread.h"
39
40 #include <sys/ptrace.h>
41 #include <sys/reg.h>
42
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>
51 #include <sys/stat.h>
52 #include "gdb_bfd.h"
53 #include <sys/core.h>
54 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
55 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
56 #include <sys/ldr.h>
57 #include <sys/systemcfg.h>
58
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
61 accessing fields in the appropriate structures. */
62
63 /* In 32-bit compilation mode (which is the only mode from which ptrace()
64 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
65
66 #if defined (__ld_info32) || defined (__ld_info64)
67 # define ARCH3264
68 #endif
69
70 /* Return whether the current architecture is 64-bit. */
71
72 #ifndef ARCH3264
73 # define ARCH64() 0
74 #else
75 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
76 #endif
77
78 static target_xfer_partial_ftype rs6000_xfer_shared_libraries;
79
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. */
84
85 static int
86 regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
87 {
88 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
89
90 *isfloat = 0;
91 if (tdep->ppc_gp0_regnum <= regno
92 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
93 return regno;
94 else if (tdep->ppc_fp0_regnum >= 0
95 && tdep->ppc_fp0_regnum <= regno
96 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
97 {
98 *isfloat = 1;
99 return regno - tdep->ppc_fp0_regnum + FPR0;
100 }
101 else if (regno == gdbarch_pc_regnum (gdbarch))
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;
113 else if (tdep->ppc_fpscr_regnum >= 0
114 && regno == tdep->ppc_fpscr_regnum)
115 return FPSCR;
116 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
117 return MQ;
118 else
119 return -1;
120 }
121
122 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
123
124 static int
125 rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
126 {
127 #ifdef HAVE_PTRACE64
128 int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
129 #else
130 int ret = ptrace (req, id, (int *)addr, data, buf);
131 #endif
132 #if 0
133 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
134 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
135 #endif
136 return ret;
137 }
138
139 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
140
141 static int
142 rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
143 {
144 #ifdef ARCH3264
145 # ifdef HAVE_PTRACE64
146 int ret = ptrace64 (req, id, addr, data, buf);
147 # else
148 int ret = ptracex (req, id, addr, data, buf);
149 # endif
150 #else
151 int ret = 0;
152 #endif
153 #if 0
154 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
155 req, id, hex_string (addr), data, (unsigned int)buf, ret);
156 #endif
157 return ret;
158 }
159
160 /* Fetch register REGNO from the inferior. */
161
162 static void
163 fetch_register (struct regcache *regcache, int regno)
164 {
165 struct gdbarch *gdbarch = get_regcache_arch (regcache);
166 int addr[MAX_REGISTER_SIZE];
167 int nr, isfloat;
168
169 /* Retrieved values may be -1, so infer errors from errno. */
170 errno = 0;
171
172 nr = regmap (gdbarch, regno, &isfloat);
173
174 /* Floating-point registers. */
175 if (isfloat)
176 rs6000_ptrace32 (PT_READ_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
177
178 /* Bogus register number. */
179 else if (nr < 0)
180 {
181 if (regno >= gdbarch_num_regs (gdbarch))
182 fprintf_unfiltered (gdb_stderr,
183 "gdb error: register no %d not implemented.\n",
184 regno);
185 return;
186 }
187
188 /* Fixed-point registers. */
189 else
190 {
191 if (!ARCH64 ())
192 *addr = rs6000_ptrace32 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
193 (int *) nr, 0, 0);
194 else
195 {
196 /* PT_READ_GPR requires the buffer parameter to point to long long,
197 even if the register is really only 32 bits. */
198 long long buf;
199 rs6000_ptrace64 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
200 nr, 0, &buf);
201 if (register_size (gdbarch, regno) == 8)
202 memcpy (addr, &buf, 8);
203 else
204 *addr = buf;
205 }
206 }
207
208 if (!errno)
209 regcache_raw_supply (regcache, regno, (char *) addr);
210 else
211 {
212 #if 0
213 /* FIXME: this happens 3 times at the start of each 64-bit program. */
214 perror (_("ptrace read"));
215 #endif
216 errno = 0;
217 }
218 }
219
220 /* Store register REGNO back into the inferior. */
221
222 static void
223 store_register (struct regcache *regcache, int regno)
224 {
225 struct gdbarch *gdbarch = get_regcache_arch (regcache);
226 int addr[MAX_REGISTER_SIZE];
227 int nr, isfloat;
228
229 /* Fetch the register's value from the register cache. */
230 regcache_raw_collect (regcache, regno, addr);
231
232 /* -1 can be a successful return value, so infer errors from errno. */
233 errno = 0;
234
235 nr = regmap (gdbarch, regno, &isfloat);
236
237 /* Floating-point registers. */
238 if (isfloat)
239 rs6000_ptrace32 (PT_WRITE_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
240
241 /* Bogus register number. */
242 else if (nr < 0)
243 {
244 if (regno >= gdbarch_num_regs (gdbarch))
245 fprintf_unfiltered (gdb_stderr,
246 "gdb error: register no %d not implemented.\n",
247 regno);
248 }
249
250 /* Fixed-point registers. */
251 else
252 {
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. */
256 if (!ARCH64 ())
257 rs6000_ptrace32 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
258 (int *) nr, *addr, 0);
259 else
260 {
261 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
262 area, even if the register is really only 32 bits. */
263 long long buf;
264 if (register_size (gdbarch, regno) == 8)
265 memcpy (&buf, addr, 8);
266 else
267 buf = *addr;
268 rs6000_ptrace64 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
269 nr, 0, &buf);
270 }
271 }
272
273 if (errno)
274 {
275 perror (_("ptrace write"));
276 errno = 0;
277 }
278 }
279
280 /* Read from the inferior all registers if REGNO == -1 and just register
281 REGNO otherwise. */
282
283 static void
284 rs6000_fetch_inferior_registers (struct target_ops *ops,
285 struct regcache *regcache, int regno)
286 {
287 struct gdbarch *gdbarch = get_regcache_arch (regcache);
288 if (regno != -1)
289 fetch_register (regcache, regno);
290
291 else
292 {
293 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
294
295 /* Read 32 general purpose registers. */
296 for (regno = tdep->ppc_gp0_regnum;
297 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
298 regno++)
299 {
300 fetch_register (regcache, regno);
301 }
302
303 /* Read general purpose floating point registers. */
304 if (tdep->ppc_fp0_regnum >= 0)
305 for (regno = 0; regno < ppc_num_fprs; regno++)
306 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
307
308 /* Read special registers. */
309 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
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);
315 if (tdep->ppc_fpscr_regnum >= 0)
316 fetch_register (regcache, tdep->ppc_fpscr_regnum);
317 if (tdep->ppc_mq_regnum >= 0)
318 fetch_register (regcache, tdep->ppc_mq_regnum);
319 }
320 }
321
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
326 static void
327 rs6000_store_inferior_registers (struct target_ops *ops,
328 struct regcache *regcache, int regno)
329 {
330 struct gdbarch *gdbarch = get_regcache_arch (regcache);
331 if (regno != -1)
332 store_register (regcache, regno);
333
334 else
335 {
336 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
337
338 /* Write general purpose registers first. */
339 for (regno = tdep->ppc_gp0_regnum;
340 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
341 regno++)
342 {
343 store_register (regcache, regno);
344 }
345
346 /* Write floating point registers. */
347 if (tdep->ppc_fp0_regnum >= 0)
348 for (regno = 0; regno < ppc_num_fprs; regno++)
349 store_register (regcache, tdep->ppc_fp0_regnum + regno);
350
351 /* Write special registers. */
352 store_register (regcache, gdbarch_pc_regnum (gdbarch));
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);
358 if (tdep->ppc_fpscr_regnum >= 0)
359 store_register (regcache, tdep->ppc_fpscr_regnum);
360 if (tdep->ppc_mq_regnum >= 0)
361 store_register (regcache, tdep->ppc_mq_regnum);
362 }
363 }
364
365 /* Implement the to_xfer_partial target_ops method. */
366
367 static enum target_xfer_status
368 rs6000_xfer_partial (struct target_ops *ops, enum target_object object,
369 const char *annex, gdb_byte *readbuf,
370 const gdb_byte *writebuf,
371 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
372 {
373 pid_t pid = ptid_get_pid (inferior_ptid);
374 int arch64 = ARCH64 ();
375
376 switch (object)
377 {
378 case TARGET_OBJECT_LIBRARIES_AIX:
379 return rs6000_xfer_shared_libraries (ops, object, annex,
380 readbuf, writebuf,
381 offset, len, xfered_len);
382 case TARGET_OBJECT_MEMORY:
383 {
384 union
385 {
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,
420 (int *) (uintptr_t)
421 rounded_offset,
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,
436 (int *) (uintptr_t) rounded_offset,
437 buffer.word, NULL);
438 if (errno)
439 return TARGET_XFER_EOF;
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)
453 return TARGET_XFER_EOF;
454
455 /* Copy appropriate bytes out of the buffer. */
456 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
457 partial_len);
458 }
459
460 *xfered_len = (ULONGEST) partial_len;
461 return TARGET_XFER_OK;
462 }
463
464 default:
465 return TARGET_XFER_E_IO;
466 }
467 }
468
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
474 rs6000_wait (struct target_ops *ops,
475 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
476 {
477 pid_t pid;
478 int status, save_errno;
479
480 do
481 {
482 set_sigint_trap ();
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
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;
501 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
502 return inferior_ptid;
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;
516 /* signal 0. I have no idea why wait(2) returns with this status word. */
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 }
525 \f
526
527 /* Set the current architecture from the host running GDB. Called when
528 starting a child process. */
529
530 static void (*super_create_inferior) (struct target_ops *,char *exec_file,
531 char *allargs, char **env, int from_tty);
532 static void
533 rs6000_create_inferior (struct target_ops * ops, char *exec_file,
534 char *allargs, char **env, int from_tty)
535 {
536 enum bfd_architecture arch;
537 unsigned long mach;
538 bfd abfd;
539 struct gdbarch_info info;
540
541 super_create_inferior (ops, exec_file, allargs, env, from_tty);
542
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 }
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
571 bfd_default_set_arch_mach (&abfd, arch, mach);
572
573 gdbarch_info_init (&info);
574 info.bfd_arch_info = bfd_get_arch_info (&abfd);
575 info.abfd = exec_bfd;
576
577 if (!gdbarch_update_p (info))
578 internal_error (__FILE__, __LINE__,
579 _("rs6000_create_inferior: failed "
580 "to select architecture"));
581 }
582 \f
583
584 /* Shared Object support. */
585
586 /* Return the LdInfo data for the given process. Raises an error
587 if the data could not be obtained.
588
589 The returned value must be deallocated after use. */
590
591 static gdb_byte *
592 rs6000_ptrace_ldinfo (ptid_t ptid)
593 {
594 const int pid = ptid_get_pid (ptid);
595 int ldi_size = 1024;
596 gdb_byte *ldi = xmalloc (ldi_size);
597 int rc = -1;
598
599 while (1)
600 {
601 if (ARCH64 ())
602 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, ldi_size,
603 NULL);
604 else
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;
619 }
620
621 /* Implement the to_xfer_partial target_ops method for
622 TARGET_OBJECT_LIBRARIES_AIX objects. */
623
624 static enum target_xfer_status
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,
628 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
629 {
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);
637
638 if (writebuf)
639 return TARGET_XFER_E_IO;
640
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);
647
648 do_cleanups (cleanup);
649
650 if (result == 0)
651 return TARGET_XFER_EOF;
652 else
653 {
654 *xfered_len = result;
655 return TARGET_XFER_OK;
656 }
657 }
658
659 void _initialize_rs6000_nat (void);
660
661 void
662 _initialize_rs6000_nat (void)
663 {
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;
670
671 super_create_inferior = t->to_create_inferior;
672 t->to_create_inferior = rs6000_create_inferior;
673
674 t->to_wait = rs6000_wait;
675
676 add_target (t);
677 }
This page took 0.049709 seconds and 4 git commands to generate.