1 /* Native-dependent code for LynxOS.
2 Copyright 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
27 #include <sys/ptrace.h>
31 static unsigned long registers_addr PARAMS ((int pid));
32 static void fetch_core_registers PARAMS ((char *, unsigned, int, CORE_ADDR));
34 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
37 /* Mappings from tm-i386v.h */
55 X (ecode), /* Lynx doesn't give us either fs or gs, so */
56 X (fault), /* we just substitute these two in the hopes
57 that they are useful. */
62 /* Mappings from tm-m68k.h */
76 X (regs[10]), /* a2 */
77 X (regs[11]), /* a3 */
78 X (regs[12]), /* a4 */
79 X (regs[13]), /* a5 */
80 X (regs[14]), /* fp */
81 offsetof (st_t, usp) - offsetof (st_t, ec), /* sp */
85 X (fregs[0 * 3]), /* fp0 */
86 X (fregs[1 * 3]), /* fp1 */
87 X (fregs[2 * 3]), /* fp2 */
88 X (fregs[3 * 3]), /* fp3 */
89 X (fregs[4 * 3]), /* fp4 */
90 X (fregs[5 * 3]), /* fp5 */
91 X (fregs[6 * 3]), /* fp6 */
92 X (fregs[7 * 3]), /* fp7 */
94 X (fcregs[0]), /* fpcontrol */
95 X (fcregs[1]), /* fpstatus */
96 X (fcregs[2]), /* fpiaddr */
98 X (fault), /* fpflags */
103 /* Mappings from tm-sparc.h */
105 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
107 static int regmap[] =
114 -1, /* g5->g7 aren't saved by Lynx */
127 -1, -1, -1, -1, -1, -1, -1, -1, /* l0 -> l7 */
129 -1, -1, -1, -1, -1, -1, -1, -1, /* i0 -> i7 */
131 FX (f.fregs[0]), /* f0 */
177 static int regmap[] =
179 X (iregs[0]), /* r0 */
212 X (fregs[0]), /* f0 */
245 X (srr0), /* IAR (PC) */
246 X (srr1), /* MSR (PS) */
258 /* This routine handles some oddball cases for Sparc registers and LynxOS.
259 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
260 It also handles knows where to find the I & L regs on the stack. */
263 fetch_inferior_registers (regno)
268 #define WHATREGS_FLOAT 1
269 #define WHATREGS_GEN 2
270 #define WHATREGS_STACK 4
273 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
274 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
275 whatregs = WHATREGS_STACK;
276 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
277 whatregs = WHATREGS_FLOAT;
279 whatregs = WHATREGS_GEN;
281 if (whatregs & WHATREGS_GEN)
283 struct econtext ec; /* general regs */
284 char buf[MAX_REGISTER_RAW_SIZE];
289 retval = ptrace (PTRACE_GETREGS, inferior_pid, (PTRACE_ARG3_TYPE) & ec,
292 perror_with_name ("ptrace(PTRACE_GETREGS)");
294 memset (buf, 0, REGISTER_RAW_SIZE (G0_REGNUM));
295 supply_register (G0_REGNUM, buf);
296 supply_register (TBR_REGNUM, (char *) &ec.tbr);
298 memcpy (®isters[REGISTER_BYTE (G1_REGNUM)], &ec.g1,
299 4 * REGISTER_RAW_SIZE (G1_REGNUM));
300 for (i = G1_REGNUM; i <= G1_REGNUM + 3; i++)
301 register_valid[i] = 1;
303 supply_register (PS_REGNUM, (char *) &ec.psr);
304 supply_register (Y_REGNUM, (char *) &ec.y);
305 supply_register (PC_REGNUM, (char *) &ec.pc);
306 supply_register (NPC_REGNUM, (char *) &ec.npc);
307 supply_register (WIM_REGNUM, (char *) &ec.wim);
309 memcpy (®isters[REGISTER_BYTE (O0_REGNUM)], ec.o,
310 8 * REGISTER_RAW_SIZE (O0_REGNUM));
311 for (i = O0_REGNUM; i <= O0_REGNUM + 7; i++)
312 register_valid[i] = 1;
315 if (whatregs & WHATREGS_STACK)
320 sp = read_register (SP_REGNUM);
322 target_xfer_memory (sp + FRAME_SAVED_I0,
323 ®isters[REGISTER_BYTE (I0_REGNUM)],
324 8 * REGISTER_RAW_SIZE (I0_REGNUM), 0);
325 for (i = I0_REGNUM; i <= I7_REGNUM; i++)
326 register_valid[i] = 1;
328 target_xfer_memory (sp + FRAME_SAVED_L0,
329 ®isters[REGISTER_BYTE (L0_REGNUM)],
330 8 * REGISTER_RAW_SIZE (L0_REGNUM), 0);
331 for (i = L0_REGNUM; i <= L0_REGNUM + 7; i++)
332 register_valid[i] = 1;
335 if (whatregs & WHATREGS_FLOAT)
337 struct fcontext fc; /* fp regs */
342 retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) & fc,
345 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
347 memcpy (®isters[REGISTER_BYTE (FP0_REGNUM)], fc.f.fregs,
348 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
349 for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
350 register_valid[i] = 1;
352 supply_register (FPS_REGNUM, (char *) &fc.fsr);
356 /* This routine handles storing of the I & L regs for the Sparc. The trick
357 here is that they actually live on the stack. The really tricky part is
358 that when changing the stack pointer, the I & L regs must be written to
359 where the new SP points, otherwise the regs will be incorrect when the
360 process is started up again. We assume that the I & L regs are valid at
364 store_inferior_registers (regno)
370 whatregs = WHATREGS_FLOAT | WHATREGS_GEN | WHATREGS_STACK;
371 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
372 whatregs = WHATREGS_STACK;
373 else if (regno >= FP0_REGNUM && regno < FP0_REGNUM + 32)
374 whatregs = WHATREGS_FLOAT;
375 else if (regno == SP_REGNUM)
376 whatregs = WHATREGS_STACK | WHATREGS_GEN;
378 whatregs = WHATREGS_GEN;
380 if (whatregs & WHATREGS_GEN)
382 struct econtext ec; /* general regs */
385 ec.tbr = read_register (TBR_REGNUM);
386 memcpy (&ec.g1, ®isters[REGISTER_BYTE (G1_REGNUM)],
387 4 * REGISTER_RAW_SIZE (G1_REGNUM));
389 ec.psr = read_register (PS_REGNUM);
390 ec.y = read_register (Y_REGNUM);
391 ec.pc = read_register (PC_REGNUM);
392 ec.npc = read_register (NPC_REGNUM);
393 ec.wim = read_register (WIM_REGNUM);
395 memcpy (ec.o, ®isters[REGISTER_BYTE (O0_REGNUM)],
396 8 * REGISTER_RAW_SIZE (O0_REGNUM));
399 retval = ptrace (PTRACE_SETREGS, inferior_pid, (PTRACE_ARG3_TYPE) & ec,
402 perror_with_name ("ptrace(PTRACE_SETREGS)");
405 if (whatregs & WHATREGS_STACK)
410 sp = read_register (SP_REGNUM);
412 if (regno == -1 || regno == SP_REGNUM)
414 if (!register_valid[L0_REGNUM + 5])
416 target_xfer_memory (sp + FRAME_SAVED_I0,
417 ®isters[REGISTER_BYTE (I0_REGNUM)],
418 8 * REGISTER_RAW_SIZE (I0_REGNUM), 1);
420 target_xfer_memory (sp + FRAME_SAVED_L0,
421 ®isters[REGISTER_BYTE (L0_REGNUM)],
422 8 * REGISTER_RAW_SIZE (L0_REGNUM), 1);
424 else if (regno >= L0_REGNUM && regno <= I7_REGNUM)
426 if (!register_valid[regno])
428 if (regno >= L0_REGNUM && regno <= L0_REGNUM + 7)
429 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (L0_REGNUM)
432 regoffset = REGISTER_BYTE (regno) - REGISTER_BYTE (I0_REGNUM)
434 target_xfer_memory (sp + regoffset, ®isters[REGISTER_BYTE (regno)],
435 REGISTER_RAW_SIZE (regno), 1);
439 if (whatregs & WHATREGS_FLOAT)
441 struct fcontext fc; /* fp regs */
444 /* We read fcontext first so that we can get good values for fq_t... */
446 retval = ptrace (PTRACE_GETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) & fc,
449 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
451 memcpy (fc.f.fregs, ®isters[REGISTER_BYTE (FP0_REGNUM)],
452 32 * REGISTER_RAW_SIZE (FP0_REGNUM));
454 fc.fsr = read_register (FPS_REGNUM);
457 retval = ptrace (PTRACE_SETFPREGS, inferior_pid, (PTRACE_ARG3_TYPE) & fc,
460 perror_with_name ("ptrace(PTRACE_SETFPREGS)");
465 #if defined (I386) || defined (M68K) || defined (rs6000)
467 /* Return the offset relative to the start of the per-thread data to the
468 saved context block. */
475 int ecpoff = offsetof (st_t, ecp);
479 stblock = (CORE_ADDR) ptrace (PTRACE_THREADUSER, pid, (PTRACE_ARG3_TYPE) 0,
482 perror_with_name ("ptrace(PTRACE_THREADUSER)");
484 ecp = (CORE_ADDR) ptrace (PTRACE_PEEKTHREAD, pid, (PTRACE_ARG3_TYPE) ecpoff,
487 perror_with_name ("ptrace(PTRACE_PEEKTHREAD)");
489 return ecp - stblock;
492 /* Fetch one or more registers from the inferior. REGNO == -1 to get
493 them all. We actually fetch more than requested, when convenient,
494 marking them as valid so we won't fetch them again. */
497 fetch_inferior_registers (regno)
507 reghi = NUM_REGS - 1;
510 reglo = reghi = regno;
512 ecp = registers_addr (inferior_pid);
514 for (regno = reglo; regno <= reghi; regno++)
516 char buf[MAX_REGISTER_RAW_SIZE];
517 int ptrace_fun = PTRACE_PEEKTHREAD;
520 ptrace_fun = regno == SP_REGNUM ? PTRACE_PEEKUSP : PTRACE_PEEKTHREAD;
523 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
528 reg = ptrace (ptrace_fun, inferior_pid,
529 (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), 0);
531 perror_with_name ("ptrace(PTRACE_PEEKUSP)");
533 *(int *) &buf[i] = reg;
535 supply_register (regno, buf);
539 /* Store our register values back into the inferior.
540 If REGNO is -1, do this for all registers.
541 Otherwise, REGNO specifies which register (so we can save time). */
543 /* Registers we shouldn't try to store. */
544 #if !defined (CANNOT_STORE_REGISTER)
545 #define CANNOT_STORE_REGISTER(regno) 0
549 store_inferior_registers (regno)
559 reghi = NUM_REGS - 1;
562 reglo = reghi = regno;
564 ecp = registers_addr (inferior_pid);
566 for (regno = reglo; regno <= reghi; regno++)
568 int ptrace_fun = PTRACE_POKEUSER;
570 if (CANNOT_STORE_REGISTER (regno))
574 ptrace_fun = regno == SP_REGNUM ? PTRACE_POKEUSP : PTRACE_POKEUSER;
577 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
581 reg = *(unsigned int *) ®isters[REGISTER_BYTE (regno) + i];
584 ptrace (ptrace_fun, inferior_pid,
585 (PTRACE_ARG3_TYPE) (ecp + regmap[regno] + i), reg);
587 perror_with_name ("ptrace(PTRACE_POKEUSP)");
591 #endif /* defined (I386) || defined (M68K) || defined (rs6000) */
593 /* Wait for child to do something. Return pid of child, or -1 in case
594 of error; store status through argument pointer OURSTATUS. */
597 child_wait (pid, ourstatus)
599 struct target_waitstatus *ourstatus;
609 set_sigint_trap (); /* Causes SIGINT to be passed on to the
611 pid = wait (&status);
615 clear_sigint_trap ();
619 if (save_errno == EINTR)
621 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing: %s.\n",
622 safe_strerror (save_errno));
623 /* Claim it exited with unknown signal. */
624 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
625 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
629 if (pid != PIDGET (inferior_pid)) /* Some other process?!? */
632 thread = status.w_tid; /* Get thread id from status */
634 /* Initial thread value can only be acquired via wait, so we have to
635 resort to this hack. */
637 if (TIDGET (inferior_pid) == 0 && thread != 0)
639 inferior_pid = BUILDPID (inferior_pid, thread);
640 add_thread (inferior_pid);
643 pid = BUILDPID (pid, thread);
645 /* We've become a single threaded process again. */
649 /* Check for thread creation. */
650 if (WIFSTOPPED (status)
651 && WSTOPSIG (status) == SIGTRAP
652 && !in_thread_list (pid))
656 realsig = ptrace (PTRACE_GETTRACESIG, pid, (PTRACE_ARG3_TYPE) 0, 0);
658 if (realsig == SIGNEWTHREAD)
660 /* It's a new thread notification. We don't want to much with
661 realsig -- the code in wait_for_inferior expects SIGTRAP. */
662 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
663 ourstatus->value.sig = TARGET_SIGNAL_0;
667 error ("Signal for unknown thread was not SIGNEWTHREAD");
670 /* Check for thread termination. */
671 else if (WIFSTOPPED (status)
672 && WSTOPSIG (status) == SIGTRAP
673 && in_thread_list (pid))
677 realsig = ptrace (PTRACE_GETTRACESIG, pid, (PTRACE_ARG3_TYPE) 0, 0);
679 if (realsig == SIGTHREADEXIT)
681 ptrace (PTRACE_CONT, PIDGET (pid), (PTRACE_ARG3_TYPE) 0, 0);
687 /* SPARC Lynx uses an byte reversed wait status; we must use the
688 host macros to access it. These lines just a copy of
689 store_waitstatus. We can't use CHILD_SPECIAL_WAITSTATUS
690 because target.c can't include the Lynx <sys/wait.h>. */
691 if (WIFEXITED (status))
693 ourstatus->kind = TARGET_WAITKIND_EXITED;
694 ourstatus->value.integer = WEXITSTATUS (status);
696 else if (!WIFSTOPPED (status))
698 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
699 ourstatus->value.sig =
700 target_signal_from_host (WTERMSIG (status));
704 ourstatus->kind = TARGET_WAITKIND_STOPPED;
705 ourstatus->value.sig =
706 target_signal_from_host (WSTOPSIG (status));
709 store_waitstatus (ourstatus, status.w_status);
716 /* Return nonzero if the given thread is still alive. */
718 child_thread_alive (pid)
721 /* Arggh. Apparently pthread_kill only works for threads within
722 the process that calls pthread_kill.
724 We want to avoid the lynx signal extensions as they simply don't
725 map well to the generic gdb interface we want to keep.
727 All we want to do is determine if a particular thread is alive;
728 it appears as if we can just make a harmless thread specific
729 ptrace call to do that. */
730 return (ptrace (PTRACE_THREADUSER, pid, 0, 0) != -1);
733 /* Resume execution of the inferior process.
734 If STEP is nonzero, single-step it.
735 If SIGNAL is nonzero, give it that signal. */
738 child_resume (pid, step, signal)
741 enum target_signal signal;
747 /* If pid == -1, then we want to step/continue all threads, else
748 we only want to step/continue a single thread. */
752 func = step ? PTRACE_SINGLESTEP : PTRACE_CONT;
755 func = step ? PTRACE_SINGLESTEP_ONE : PTRACE_CONT_ONE;
758 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
759 it was. (If GDB wanted it to start some other way, we have already
760 written a new PC value to the child.)
762 If this system does not support PT_STEP, a higher level function will
763 have called single_step() to transmute the step request into a
764 continue request (by setting breakpoints on all possible successor
765 instructions), so we don't have to worry about that here. */
767 ptrace (func, pid, (PTRACE_ARG3_TYPE) 1, target_signal_to_host (signal));
770 perror_with_name ("ptrace");
773 /* Convert a Lynx process ID to a string. Returns the string in a static
777 child_pid_to_str (pid)
782 sprintf (buf, "process %d thread %d", PIDGET (pid), TIDGET (pid));
787 /* Extract the register values out of the core file and store
788 them where `read_register' will find them.
790 CORE_REG_SECT points to the register values themselves, read into memory.
791 CORE_REG_SIZE is the size of that area.
792 WHICH says which set of registers we are handling (0 = int, 2 = float
793 on machines where they are discontiguous).
794 REG_ADDR is the offset from u.u_ar0 to the register values relative to
795 core_reg_sect. This is used with old-fashioned core files to
796 locate the registers in a large upage-plus-stack ".reg" section.
797 Original upage address X is at location core_reg_sect+x+reg_addr.
801 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
803 unsigned core_reg_size;
810 for (regno = 0; regno < NUM_REGS; regno++)
811 if (regmap[regno] != -1)
812 supply_register (regno, core_reg_sect + offsetof (st_t, ec)
816 /* Fetching this register causes all of the I & L regs to be read from the
817 stack and validated. */
819 fetch_inferior_registers (I0_REGNUM);
824 /* Register that we are able to handle lynx core file formats.
825 FIXME: is this really bfd_target_unknown_flavour? */
827 static struct core_fns lynx_core_fns =
829 bfd_target_unknown_flavour, /* core_flavour */
830 default_check_format, /* check_format */
831 default_core_sniffer, /* core_sniffer */
832 fetch_core_registers, /* core_read_registers */
837 _initialize_core_lynx ()
839 add_core_fns (&lynx_core_fns);