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, Boston, MA 02111-1307, USA. */
26 #include <sys/ptrace.h>
30 static unsigned long registers_addr
PARAMS ((int pid
));
31 static void fetch_core_registers
PARAMS ((char *, unsigned, int, CORE_ADDR
);
33 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
36 /* Mappings from tm-i386v.h */
54 X(ecode
), /* Lynx doesn't give us either fs or gs, so */
55 X(fault
), /* we just substitute these two in the hopes
56 that they are useful. */
61 /* Mappings from tm-m68k.h */
80 offsetof (st_t
, usp
) - offsetof (st_t
, ec
), /* sp */
84 X(fregs
[0*3]), /* fp0 */
85 X(fregs
[1*3]), /* fp1 */
86 X(fregs
[2*3]), /* fp2 */
87 X(fregs
[3*3]), /* fp3 */
88 X(fregs
[4*3]), /* fp4 */
89 X(fregs
[5*3]), /* fp5 */
90 X(fregs
[6*3]), /* fp6 */
91 X(fregs
[7*3]), /* fp7 */
93 X(fcregs
[0]), /* fpcontrol */
94 X(fcregs
[1]), /* fpstatus */
95 X(fcregs
[2]), /* fpiaddr */
97 X(fault
), /* fpflags */
102 /* Mappings from tm-sparc.h */
104 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
106 static int regmap
[] =
113 -1, /* g5->g7 aren't saved by Lynx */
126 -1,-1,-1,-1,-1,-1,-1,-1, /* l0 -> l7 */
128 -1,-1,-1,-1,-1,-1,-1,-1, /* i0 -> i7 */
130 FX(f
.fregs
[0]), /* f0 */
176 static int regmap
[] =
178 X(iregs
[0]), /* r0 */
211 X(fregs
[0]), /* f0 */
244 X(srr0
), /* IAR (PC) */
245 X(srr1
), /* MSR (PS) */
257 /* This routine handles some oddball cases for Sparc registers and LynxOS.
258 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
259 It also handles knows where to find the I & L regs on the stack. */
262 fetch_inferior_registers (regno
)
267 #define WHATREGS_FLOAT 1
268 #define WHATREGS_GEN 2
269 #define WHATREGS_STACK 4
272 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
273 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
274 whatregs
= WHATREGS_STACK
;
275 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
276 whatregs
= WHATREGS_FLOAT
;
278 whatregs
= WHATREGS_GEN
;
280 if (whatregs
& WHATREGS_GEN
)
282 struct econtext ec
; /* general regs */
283 char buf
[MAX_REGISTER_RAW_SIZE
];
288 retval
= ptrace (PTRACE_GETREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &ec
,
291 perror_with_name ("ptrace(PTRACE_GETREGS)");
293 memset (buf
, 0, REGISTER_RAW_SIZE (G0_REGNUM
));
294 supply_register (G0_REGNUM
, buf
);
295 supply_register (TBR_REGNUM
, (char *)&ec
.tbr
);
297 memcpy (®isters
[REGISTER_BYTE (G1_REGNUM
)], &ec
.g1
,
298 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
299 for (i
= G1_REGNUM
; i
<= G1_REGNUM
+ 3; i
++)
300 register_valid
[i
] = 1;
302 supply_register (PS_REGNUM
, (char *)&ec
.psr
);
303 supply_register (Y_REGNUM
, (char *)&ec
.y
);
304 supply_register (PC_REGNUM
, (char *)&ec
.pc
);
305 supply_register (NPC_REGNUM
, (char *)&ec
.npc
);
306 supply_register (WIM_REGNUM
, (char *)&ec
.wim
);
308 memcpy (®isters
[REGISTER_BYTE (O0_REGNUM
)], ec
.o
,
309 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
310 for (i
= O0_REGNUM
; i
<= O0_REGNUM
+ 7; i
++)
311 register_valid
[i
] = 1;
314 if (whatregs
& WHATREGS_STACK
)
319 sp
= read_register (SP_REGNUM
);
321 target_xfer_memory (sp
+ FRAME_SAVED_I0
,
322 ®isters
[REGISTER_BYTE(I0_REGNUM
)],
323 8 * REGISTER_RAW_SIZE (I0_REGNUM
), 0);
324 for (i
= I0_REGNUM
; i
<= I7_REGNUM
; i
++)
325 register_valid
[i
] = 1;
327 target_xfer_memory (sp
+ FRAME_SAVED_L0
,
328 ®isters
[REGISTER_BYTE(L0_REGNUM
)],
329 8 * REGISTER_RAW_SIZE (L0_REGNUM
), 0);
330 for (i
= L0_REGNUM
; i
<= L0_REGNUM
+ 7; i
++)
331 register_valid
[i
] = 1;
334 if (whatregs
& WHATREGS_FLOAT
)
336 struct fcontext fc
; /* fp regs */
341 retval
= ptrace (PTRACE_GETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &fc
,
344 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
346 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], fc
.f
.fregs
,
347 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
348 for (i
= FP0_REGNUM
; i
<= FP0_REGNUM
+ 31; i
++)
349 register_valid
[i
] = 1;
351 supply_register (FPS_REGNUM
, (char *)&fc
.fsr
);
355 /* This routine handles storing of the I & L regs for the Sparc. The trick
356 here is that they actually live on the stack. The really tricky part is
357 that when changing the stack pointer, the I & L regs must be written to
358 where the new SP points, otherwise the regs will be incorrect when the
359 process is started up again. We assume that the I & L regs are valid at
363 store_inferior_registers (regno
)
369 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
370 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
371 whatregs
= WHATREGS_STACK
;
372 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
373 whatregs
= WHATREGS_FLOAT
;
374 else if (regno
== SP_REGNUM
)
375 whatregs
= WHATREGS_STACK
| WHATREGS_GEN
;
377 whatregs
= WHATREGS_GEN
;
379 if (whatregs
& WHATREGS_GEN
)
381 struct econtext ec
; /* general regs */
384 ec
.tbr
= read_register (TBR_REGNUM
);
385 memcpy (&ec
.g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
386 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
388 ec
.psr
= read_register (PS_REGNUM
);
389 ec
.y
= read_register (Y_REGNUM
);
390 ec
.pc
= read_register (PC_REGNUM
);
391 ec
.npc
= read_register (NPC_REGNUM
);
392 ec
.wim
= read_register (WIM_REGNUM
);
394 memcpy (ec
.o
, ®isters
[REGISTER_BYTE (O0_REGNUM
)],
395 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
398 retval
= ptrace (PTRACE_SETREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &ec
,
401 perror_with_name ("ptrace(PTRACE_SETREGS)");
404 if (whatregs
& WHATREGS_STACK
)
409 sp
= read_register (SP_REGNUM
);
411 if (regno
== -1 || regno
== SP_REGNUM
)
413 if (!register_valid
[L0_REGNUM
+5])
415 target_xfer_memory (sp
+ FRAME_SAVED_I0
,
416 ®isters
[REGISTER_BYTE (I0_REGNUM
)],
417 8 * REGISTER_RAW_SIZE (I0_REGNUM
), 1);
419 target_xfer_memory (sp
+ FRAME_SAVED_L0
,
420 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
421 8 * REGISTER_RAW_SIZE (L0_REGNUM
), 1);
423 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
425 if (!register_valid
[regno
])
427 if (regno
>= L0_REGNUM
&& regno
<= L0_REGNUM
+ 7)
428 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (L0_REGNUM
)
431 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (I0_REGNUM
)
433 target_xfer_memory (sp
+ regoffset
, ®isters
[REGISTER_BYTE (regno
)],
434 REGISTER_RAW_SIZE (regno
), 1);
438 if (whatregs
& WHATREGS_FLOAT
)
440 struct fcontext fc
; /* fp regs */
443 /* We read fcontext first so that we can get good values for fq_t... */
445 retval
= ptrace (PTRACE_GETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &fc
,
448 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
450 memcpy (fc
.f
.fregs
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
451 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
453 fc
.fsr
= read_register (FPS_REGNUM
);
456 retval
= ptrace (PTRACE_SETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &fc
,
459 perror_with_name ("ptrace(PTRACE_SETFPREGS)");
464 #if defined (I386) || defined (M68K) || defined (rs6000)
466 /* Return the offset relative to the start of the per-thread data to the
467 saved context block. */
474 int ecpoff
= offsetof(st_t
, ecp
);
478 stblock
= (CORE_ADDR
) ptrace (PTRACE_THREADUSER
, pid
, (PTRACE_ARG3_TYPE
)0,
481 perror_with_name ("ptrace(PTRACE_THREADUSER)");
483 ecp
= (CORE_ADDR
) ptrace (PTRACE_PEEKTHREAD
, pid
, (PTRACE_ARG3_TYPE
)ecpoff
,
486 perror_with_name ("ptrace(PTRACE_PEEKTHREAD)");
488 return ecp
- stblock
;
491 /* Fetch one or more registers from the inferior. REGNO == -1 to get
492 them all. We actually fetch more than requested, when convenient,
493 marking them as valid so we won't fetch them again. */
496 fetch_inferior_registers (regno
)
506 reghi
= NUM_REGS
- 1;
509 reglo
= reghi
= regno
;
511 ecp
= registers_addr (inferior_pid
);
513 for (regno
= reglo
; regno
<= reghi
; regno
++)
515 char buf
[MAX_REGISTER_RAW_SIZE
];
516 int ptrace_fun
= PTRACE_PEEKTHREAD
;
519 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_PEEKUSP
: PTRACE_PEEKTHREAD
;
522 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
527 reg
= ptrace (ptrace_fun
, inferior_pid
,
528 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
] + i
), 0);
530 perror_with_name ("ptrace(PTRACE_PEEKUSP)");
532 *(int *)&buf
[i
] = reg
;
534 supply_register (regno
, buf
);
538 /* Store our register values back into the inferior.
539 If REGNO is -1, do this for all registers.
540 Otherwise, REGNO specifies which register (so we can save time). */
542 /* Registers we shouldn't try to store. */
543 #if !defined (CANNOT_STORE_REGISTER)
544 #define CANNOT_STORE_REGISTER(regno) 0
548 store_inferior_registers (regno
)
558 reghi
= NUM_REGS
- 1;
561 reglo
= reghi
= regno
;
563 ecp
= registers_addr (inferior_pid
);
565 for (regno
= reglo
; regno
<= reghi
; regno
++)
567 int ptrace_fun
= PTRACE_POKEUSER
;
569 if (CANNOT_STORE_REGISTER (regno
))
573 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_POKEUSP
: PTRACE_POKEUSER
;
576 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
580 reg
= *(unsigned int *)®isters
[REGISTER_BYTE (regno
) + i
];
583 ptrace (ptrace_fun
, inferior_pid
,
584 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
] + i
), reg
);
586 perror_with_name ("ptrace(PTRACE_POKEUSP)");
590 #endif /* defined (I386) || defined (M68K) || defined (rs6000) */
592 /* Wait for child to do something. Return pid of child, or -1 in case
593 of error; store status through argument pointer OURSTATUS. */
596 child_wait (pid
, ourstatus
)
598 struct target_waitstatus
*ourstatus
;
608 set_sigint_trap(); /* Causes SIGINT to be passed on to the
610 pid
= wait (&status
);
618 if (save_errno
== EINTR
)
620 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
621 safe_strerror (save_errno
));
622 /* Claim it exited with unknown signal. */
623 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
624 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
628 if (pid
!= PIDGET (inferior_pid
)) /* Some other process?!? */
631 thread
= status
.w_tid
; /* Get thread id from status */
633 /* Initial thread value can only be acquired via wait, so we have to
634 resort to this hack. */
636 if (TIDGET (inferior_pid
) == 0 && thread
!= 0)
638 inferior_pid
= BUILDPID (inferior_pid
, thread
);
639 add_thread (inferior_pid
);
642 pid
= BUILDPID (pid
, thread
);
644 /* We've become a single threaded process again. */
648 /* Check for thread creation. */
649 if (WIFSTOPPED(status
)
650 && WSTOPSIG(status
) == SIGTRAP
651 && !in_thread_list (pid
))
655 realsig
= ptrace (PTRACE_GETTRACESIG
, pid
, (PTRACE_ARG3_TYPE
)0, 0);
657 if (realsig
== SIGNEWTHREAD
)
659 /* It's a new thread notification. We don't want to much with
660 realsig -- the code in wait_for_inferior expects SIGTRAP. */
661 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
662 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
666 error ("Signal for unknown thread was not SIGNEWTHREAD");
669 /* Check for thread termination. */
670 else if (WIFSTOPPED(status
)
671 && WSTOPSIG(status
) == SIGTRAP
672 && in_thread_list (pid
))
676 realsig
= ptrace (PTRACE_GETTRACESIG
, pid
, (PTRACE_ARG3_TYPE
)0, 0);
678 if (realsig
== SIGTHREADEXIT
)
680 ptrace (PTRACE_CONT
, PIDGET (pid
), (PTRACE_ARG3_TYPE
)0, 0);
686 /* SPARC Lynx uses an byte reversed wait status; we must use the
687 host macros to access it. These lines just a copy of
688 store_waitstatus. We can't use CHILD_SPECIAL_WAITSTATUS
689 because target.c can't include the Lynx <sys/wait.h>. */
690 if (WIFEXITED (status
))
692 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
693 ourstatus
->value
.integer
= WEXITSTATUS (status
);
695 else if (!WIFSTOPPED (status
))
697 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
698 ourstatus
->value
.sig
=
699 target_signal_from_host (WTERMSIG (status
));
703 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
704 ourstatus
->value
.sig
=
705 target_signal_from_host (WSTOPSIG (status
));
708 store_waitstatus (ourstatus
, status
.w_status
);
715 /* Return nonzero if the given thread is still alive. */
717 child_thread_alive (pid
)
720 /* Arggh. Apparently pthread_kill only works for threads within
721 the process that calls pthread_kill.
723 We want to avoid the lynx signal extensions as they simply don't
724 map well to the generic gdb interface we want to keep.
726 All we want to do is determine if a particular thread is alive;
727 it appears as if we can just make a harmless thread specific
728 ptrace call to do that. */
729 return (ptrace (PTRACE_THREADUSER
, pid
, 0, 0) != -1);
732 /* Resume execution of the inferior process.
733 If STEP is nonzero, single-step it.
734 If SIGNAL is nonzero, give it that signal. */
737 child_resume (pid
, step
, signal
)
740 enum target_signal signal
;
746 /* If pid == -1, then we want to step/continue all threads, else
747 we only want to step/continue a single thread. */
751 func
= step
? PTRACE_SINGLESTEP
: PTRACE_CONT
;
754 func
= step
? PTRACE_SINGLESTEP_ONE
: PTRACE_CONT_ONE
;
757 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
758 it was. (If GDB wanted it to start some other way, we have already
759 written a new PC value to the child.)
761 If this system does not support PT_STEP, a higher level function will
762 have called single_step() to transmute the step request into a
763 continue request (by setting breakpoints on all possible successor
764 instructions), so we don't have to worry about that here. */
766 ptrace (func
, pid
, (PTRACE_ARG3_TYPE
) 1, target_signal_to_host (signal
));
769 perror_with_name ("ptrace");
772 /* Convert a Lynx process ID to a string. Returns the string in a static
776 lynx_pid_to_str (pid
)
781 sprintf (buf
, "process %d thread %d", PIDGET (pid
), TIDGET (pid
));
786 /* Extract the register values out of the core file and store
787 them where `read_register' will find them.
789 CORE_REG_SECT points to the register values themselves, read into memory.
790 CORE_REG_SIZE is the size of that area.
791 WHICH says which set of registers we are handling (0 = int, 2 = float
792 on machines where they are discontiguous).
793 REG_ADDR is the offset from u.u_ar0 to the register values relative to
794 core_reg_sect. This is used with old-fashioned core files to
795 locate the registers in a large upage-plus-stack ".reg" section.
796 Original upage address X is at location core_reg_sect+x+reg_addr.
800 fetch_core_registers (core_reg_sect
, core_reg_size
, which
, reg_addr
)
802 unsigned core_reg_size
;
809 for (regno
= 0; regno
< NUM_REGS
; regno
++)
810 if (regmap
[regno
] != -1)
811 supply_register (regno
, core_reg_sect
+ offsetof (st_t
, ec
)
815 /* Fetching this register causes all of the I & L regs to be read from the
816 stack and validated. */
818 fetch_inferior_registers (I0_REGNUM
);
823 /* Register that we are able to handle lynx core file formats.
824 FIXME: is this really bfd_target_unknown_flavour? */
826 static struct core_fns lynx_core_fns
=
828 bfd_target_unknown_flavour
,
829 fetch_core_registers
,
834 _initialize_core_lynx ()
836 add_core_fns (&lynx_core_fns
);