1 /* Native-dependent code for LynxOS.
3 Copyright 1993, 1994, 1995, 1996, 1999, 2000, 2001, 2003 Free
4 Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
30 #include <sys/ptrace.h>
34 static unsigned long registers_addr (int pid
);
35 static void fetch_core_registers (char *, unsigned, int, CORE_ADDR
);
37 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
40 /* Mappings from tm-i386v.h */
58 X (ecode
), /* Lynx doesn't give us either fs or gs, so */
59 X (fault
), /* we just substitute these two in the hopes
60 that they are useful. */
65 /* Mappings from tm-m68k.h */
79 X (regs
[10]), /* a2 */
80 X (regs
[11]), /* a3 */
81 X (regs
[12]), /* a4 */
82 X (regs
[13]), /* a5 */
83 X (regs
[14]), /* fp */
84 offsetof (st_t
, usp
) - offsetof (st_t
, ec
), /* sp */
88 X (fregs
[0 * 3]), /* fp0 */
89 X (fregs
[1 * 3]), /* fp1 */
90 X (fregs
[2 * 3]), /* fp2 */
91 X (fregs
[3 * 3]), /* fp3 */
92 X (fregs
[4 * 3]), /* fp4 */
93 X (fregs
[5 * 3]), /* fp5 */
94 X (fregs
[6 * 3]), /* fp6 */
95 X (fregs
[7 * 3]), /* fp7 */
97 X (fcregs
[0]), /* fpcontrol */
98 X (fcregs
[1]), /* fpstatus */
99 X (fcregs
[2]), /* fpiaddr */
100 X (ssw
), /* fpcode */
101 X (fault
), /* fpflags */
106 /* Mappings from tm-sparc.h */
108 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
110 static int regmap
[] =
117 -1, /* g5->g7 aren't saved by Lynx */
130 -1, -1, -1, -1, -1, -1, -1, -1, /* l0 -> l7 */
132 -1, -1, -1, -1, -1, -1, -1, -1, /* i0 -> i7 */
134 FX (f
.fregs
[0]), /* f0 */
180 static int regmap
[] =
182 X (iregs
[0]), /* r0 */
215 X (fregs
[0]), /* f0 */
248 X (srr0
), /* IAR (PC) */
249 X (srr1
), /* MSR (PS) */
261 /* This routine handles some oddball cases for Sparc registers and LynxOS.
262 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
263 It also handles knows where to find the I & L regs on the stack. */
266 fetch_inferior_registers (int regno
)
270 #define WHATREGS_FLOAT 1
271 #define WHATREGS_GEN 2
272 #define WHATREGS_STACK 4
275 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
276 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
277 whatregs
= WHATREGS_STACK
;
278 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
279 whatregs
= WHATREGS_FLOAT
;
281 whatregs
= WHATREGS_GEN
;
283 if (whatregs
& WHATREGS_GEN
)
285 struct econtext ec
; /* general regs */
286 char buf
[MAX_REGISTER_SIZE
];
291 retval
= ptrace (PTRACE_GETREGS
, PIDGET (inferior_ptid
),
292 (PTRACE_ARG3_TYPE
) & ec
, 0);
294 perror_with_name ("ptrace(PTRACE_GETREGS)");
296 memset (buf
, 0, REGISTER_RAW_SIZE (G0_REGNUM
));
297 supply_register (G0_REGNUM
, buf
);
298 supply_register (TBR_REGNUM
, (char *) &ec
.tbr
);
300 memcpy (&deprecated_registers
[REGISTER_BYTE (G1_REGNUM
)], &ec
.g1
,
301 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
302 for (i
= G1_REGNUM
; i
<= G1_REGNUM
+ 3; i
++)
303 deprecated_register_valid
[i
] = 1;
305 supply_register (PS_REGNUM
, (char *) &ec
.psr
);
306 supply_register (Y_REGNUM
, (char *) &ec
.y
);
307 supply_register (PC_REGNUM
, (char *) &ec
.pc
);
308 supply_register (NPC_REGNUM
, (char *) &ec
.npc
);
309 supply_register (WIM_REGNUM
, (char *) &ec
.wim
);
311 memcpy (&deprecated_registers
[REGISTER_BYTE (O0_REGNUM
)], ec
.o
,
312 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
313 for (i
= O0_REGNUM
; i
<= O0_REGNUM
+ 7; i
++)
314 deprecated_register_valid
[i
] = 1;
317 if (whatregs
& WHATREGS_STACK
)
322 sp
= read_register (SP_REGNUM
);
324 target_read_memory (sp
+ FRAME_SAVED_I0
,
325 &deprecated_registers
[REGISTER_BYTE (I0_REGNUM
)],
326 8 * REGISTER_RAW_SIZE (I0_REGNUM
));
327 for (i
= I0_REGNUM
; i
<= I7_REGNUM
; i
++)
328 deprecated_register_valid
[i
] = 1;
330 target_read_memory (sp
+ FRAME_SAVED_L0
,
331 &deprecated_registers
[REGISTER_BYTE (L0_REGNUM
)],
332 8 * REGISTER_RAW_SIZE (L0_REGNUM
));
333 for (i
= L0_REGNUM
; i
<= L0_REGNUM
+ 7; i
++)
334 deprecated_register_valid
[i
] = 1;
337 if (whatregs
& WHATREGS_FLOAT
)
339 struct fcontext fc
; /* fp regs */
344 retval
= ptrace (PTRACE_GETFPREGS
, PIDGET (inferior_ptid
),
345 (PTRACE_ARG3_TYPE
) & fc
, 0);
347 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
349 memcpy (&deprecated_registers
[REGISTER_BYTE (FP0_REGNUM
)], fc
.f
.fregs
,
350 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
351 for (i
= FP0_REGNUM
; i
<= FP0_REGNUM
+ 31; i
++)
352 deprecated_register_valid
[i
] = 1;
354 supply_register (FPS_REGNUM
, (char *) &fc
.fsr
);
358 /* This routine handles storing of the I & L regs for the Sparc. The trick
359 here is that they actually live on the stack. The really tricky part is
360 that when changing the stack pointer, the I & L regs must be written to
361 where the new SP points, otherwise the regs will be incorrect when the
362 process is started up again. We assume that the I & L regs are valid at
366 store_inferior_registers (int regno
)
371 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
372 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
373 whatregs
= WHATREGS_STACK
;
374 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
375 whatregs
= WHATREGS_FLOAT
;
376 else if (regno
== SP_REGNUM
)
377 whatregs
= WHATREGS_STACK
| WHATREGS_GEN
;
379 whatregs
= WHATREGS_GEN
;
381 if (whatregs
& WHATREGS_GEN
)
383 struct econtext ec
; /* general regs */
386 ec
.tbr
= read_register (TBR_REGNUM
);
387 memcpy (&ec
.g1
, &deprecated_registers
[REGISTER_BYTE (G1_REGNUM
)],
388 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
390 ec
.psr
= read_register (PS_REGNUM
);
391 ec
.y
= read_register (Y_REGNUM
);
392 ec
.pc
= read_register (PC_REGNUM
);
393 ec
.npc
= read_register (NPC_REGNUM
);
394 ec
.wim
= read_register (WIM_REGNUM
);
396 memcpy (ec
.o
, &deprecated_registers
[REGISTER_BYTE (O0_REGNUM
)],
397 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
400 retval
= ptrace (PTRACE_SETREGS
, PIDGET (inferior_ptid
),
401 (PTRACE_ARG3_TYPE
) & ec
, 0);
403 perror_with_name ("ptrace(PTRACE_SETREGS)");
406 if (whatregs
& WHATREGS_STACK
)
411 sp
= read_register (SP_REGNUM
);
413 if (regno
== -1 || regno
== SP_REGNUM
)
415 if (!deprecated_register_valid
[L0_REGNUM
+ 5])
416 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
417 target_write_memory (sp
+ FRAME_SAVED_I0
,
418 &deprecated_registers
[REGISTER_BYTE (I0_REGNUM
)],
419 8 * REGISTER_RAW_SIZE (I0_REGNUM
));
421 target_write_memory (sp
+ FRAME_SAVED_L0
,
422 &deprecated_registers
[REGISTER_BYTE (L0_REGNUM
)],
423 8 * REGISTER_RAW_SIZE (L0_REGNUM
));
425 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
427 if (!deprecated_register_valid
[regno
])
428 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
429 if (regno
>= L0_REGNUM
&& regno
<= L0_REGNUM
+ 7)
430 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (L0_REGNUM
)
433 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (I0_REGNUM
)
435 target_write_memory (sp
+ regoffset
,
436 &deprecated_registers
[REGISTER_BYTE (regno
)],
437 REGISTER_RAW_SIZE (regno
));
441 if (whatregs
& WHATREGS_FLOAT
)
443 struct fcontext fc
; /* fp regs */
446 /* We read fcontext first so that we can get good values for fq_t... */
448 retval
= ptrace (PTRACE_GETFPREGS
, PIDGET (inferior_ptid
),
449 (PTRACE_ARG3_TYPE
) & fc
, 0);
451 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
453 memcpy (fc
.f
.fregs
, &deprecated_registers
[REGISTER_BYTE (FP0_REGNUM
)],
454 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
456 fc
.fsr
= read_register (FPS_REGNUM
);
459 retval
= ptrace (PTRACE_SETFPREGS
, PIDGET (inferior_ptid
),
460 (PTRACE_ARG3_TYPE
) & fc
, 0);
462 perror_with_name ("ptrace(PTRACE_SETFPREGS)");
467 #if defined (I386) || defined (M68K) || defined (rs6000)
469 /* Return the offset relative to the start of the per-thread data to the
470 saved context block. */
473 registers_addr (int pid
)
476 int ecpoff
= offsetof (st_t
, ecp
);
480 stblock
= (CORE_ADDR
) ptrace (PTRACE_THREADUSER
, pid
, (PTRACE_ARG3_TYPE
) 0,
483 perror_with_name ("ptrace(PTRACE_THREADUSER)");
485 ecp
= (CORE_ADDR
) ptrace (PTRACE_PEEKTHREAD
, pid
, (PTRACE_ARG3_TYPE
) ecpoff
,
488 perror_with_name ("ptrace(PTRACE_PEEKTHREAD)");
490 return ecp
- stblock
;
493 /* Fetch one or more registers from the inferior. REGNO == -1 to get
494 them all. We actually fetch more than requested, when convenient,
495 marking them as valid so we won't fetch them again. */
498 fetch_inferior_registers (int regno
)
507 reghi
= NUM_REGS
- 1;
510 reglo
= reghi
= regno
;
512 ecp
= registers_addr (PIDGET (inferior_ptid
));
515 char buf
[MAX_REGISTER_SIZE
];
516 for (regno
= reglo
; regno
<= reghi
; regno
++)
518 int ptrace_fun
= PTRACE_PEEKTHREAD
;
521 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_PEEKUSP
: PTRACE_PEEKTHREAD
;
524 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
529 reg
= ptrace (ptrace_fun
, PIDGET (inferior_ptid
),
530 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
] + i
), 0);
532 perror_with_name ("ptrace(PTRACE_PEEKUSP)");
534 *(int *) &buf
[i
] = reg
;
536 supply_register (regno
, buf
);
541 /* Store our register values back into the inferior.
542 If REGNO is -1, do this for all registers.
543 Otherwise, REGNO specifies which register (so we can save time). */
546 store_inferior_registers (int regno
)
555 reghi
= NUM_REGS
- 1;
558 reglo
= reghi
= regno
;
560 ecp
= registers_addr (PIDGET (inferior_ptid
));
562 for (regno
= reglo
; regno
<= reghi
; regno
++)
564 int ptrace_fun
= PTRACE_POKEUSER
;
566 if (CANNOT_STORE_REGISTER (regno
))
570 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_POKEUSP
: PTRACE_POKEUSER
;
573 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
577 reg
= *(unsigned int *) &deprecated_registers
[REGISTER_BYTE (regno
) + i
];
580 ptrace (ptrace_fun
, PIDGET (inferior_ptid
),
581 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
] + i
), reg
);
583 perror_with_name ("ptrace(PTRACE_POKEUSP)");
587 #endif /* defined (I386) || defined (M68K) || defined (rs6000) */
589 /* Wait for child to do something. Return pid of child, or -1 in case
590 of error; store status through argument pointer OURSTATUS. */
593 child_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
604 set_sigint_trap (); /* Causes SIGINT to be passed on to the
606 pid
= wait (&status
);
610 clear_sigint_trap ();
614 if (save_errno
== EINTR
)
616 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
617 safe_strerror (save_errno
));
618 /* Claim it exited with unknown signal. */
619 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
620 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
624 if (pid
!= PIDGET (inferior_ptid
)) /* Some other process?!? */
627 thread
= status
.w_tid
; /* Get thread id from status */
629 /* Initial thread value can only be acquired via wait, so we have to
630 resort to this hack. */
632 if (TIDGET (inferior_ptid
) == 0 && thread
!= 0)
634 inferior_ptid
= MERGEPID (PIDGET (inferior_ptid
), thread
);
635 add_thread (inferior_ptid
);
638 ptid
= BUILDPID (pid
, thread
);
640 /* We've become a single threaded process again. */
642 inferior_ptid
= ptid
;
644 /* Check for thread creation. */
645 if (WIFSTOPPED (status
)
646 && WSTOPSIG (status
) == SIGTRAP
647 && !in_thread_list (ptid
))
651 realsig
= ptrace (PTRACE_GETTRACESIG
, PIDGET (ptid
),
652 (PTRACE_ARG3_TYPE
) 0, 0);
654 if (realsig
== SIGNEWTHREAD
)
656 /* It's a new thread notification. We don't want to much with
657 realsig -- the code in wait_for_inferior expects SIGTRAP. */
658 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
659 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
663 error ("Signal for unknown thread was not SIGNEWTHREAD");
666 /* Check for thread termination. */
667 else if (WIFSTOPPED (status
)
668 && WSTOPSIG (status
) == SIGTRAP
669 && in_thread_list (ptid
))
673 realsig
= ptrace (PTRACE_GETTRACESIG
, PIDGET (ptid
),
674 (PTRACE_ARG3_TYPE
) 0, 0);
676 if (realsig
== SIGTHREADEXIT
)
678 ptrace (PTRACE_CONT
, PIDGET (ptid
), (PTRACE_ARG3_TYPE
) 0, 0);
684 /* SPARC Lynx uses an byte reversed wait status; we must use the
685 host macros to access it. These lines just a copy of
686 store_waitstatus. We can't use CHILD_SPECIAL_WAITSTATUS
687 because target.c can't include the Lynx <sys/wait.h>. */
688 if (WIFEXITED (status
))
690 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
691 ourstatus
->value
.integer
= WEXITSTATUS (status
);
693 else if (!WIFSTOPPED (status
))
695 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
696 ourstatus
->value
.sig
=
697 target_signal_from_host (WTERMSIG (status
));
701 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
702 ourstatus
->value
.sig
=
703 target_signal_from_host (WSTOPSIG (status
));
706 store_waitstatus (ourstatus
, status
.w_status
);
713 /* Return nonzero if the given thread is still alive. */
715 child_thread_alive (ptid_t ptid
)
717 int pid
= PIDGET (ptid
);
719 /* Arggh. Apparently pthread_kill only works for threads within
720 the process that calls pthread_kill.
722 We want to avoid the lynx signal extensions as they simply don't
723 map well to the generic gdb interface we want to keep.
725 All we want to do is determine if a particular thread is alive;
726 it appears as if we can just make a harmless thread specific
727 ptrace call to do that. */
728 return (ptrace (PTRACE_THREADUSER
, pid
, 0, 0) != -1);
731 /* Resume execution of the inferior process.
732 If STEP is nonzero, single-step it.
733 If SIGNAL is nonzero, give it that signal. */
736 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
739 int pid
= PIDGET (ptid
);
743 /* If pid == -1, then we want to step/continue all threads, else
744 we only want to step/continue a single thread. */
747 pid
= PIDGET (inferior_ptid
);
748 func
= step
? PTRACE_SINGLESTEP
: PTRACE_CONT
;
751 func
= step
? PTRACE_SINGLESTEP_ONE
: PTRACE_CONT_ONE
;
754 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
755 it was. (If GDB wanted it to start some other way, we have already
756 written a new PC value to the child.)
758 If this system does not support PT_STEP, a higher level function will
759 have called single_step() to transmute the step request into a
760 continue request (by setting breakpoints on all possible successor
761 instructions), so we don't have to worry about that here. */
763 ptrace (func
, pid
, (PTRACE_ARG3_TYPE
) 1, target_signal_to_host (signal
));
766 perror_with_name ("ptrace");
769 /* Convert a Lynx process ID to a string. Returns the string in a static
773 child_pid_to_str (ptid_t ptid
)
777 sprintf (buf
, "process %d thread %d", PIDGET (ptid
), TIDGET (ptid
));
782 /* Extract the register values out of the core file and store
783 them where `read_register' will find them.
785 CORE_REG_SECT points to the register values themselves, read into memory.
786 CORE_REG_SIZE is the size of that area.
787 WHICH says which set of registers we are handling (0 = int, 2 = float
788 on machines where they are discontiguous).
789 REG_ADDR is the offset from u.u_ar0 to the register values relative to
790 core_reg_sect. This is used with old-fashioned core files to
791 locate the registers in a large upage-plus-stack ".reg" section.
792 Original upage address X is at location core_reg_sect+x+reg_addr.
796 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
, int which
,
802 for (regno
= 0; regno
< NUM_REGS
; regno
++)
803 if (regmap
[regno
] != -1)
804 supply_register (regno
, core_reg_sect
+ offsetof (st_t
, ec
)
808 /* Fetching this register causes all of the I & L regs to be read from the
809 stack and validated. */
811 fetch_inferior_registers (I0_REGNUM
);
816 /* Register that we are able to handle lynx core file formats.
817 FIXME: is this really bfd_target_unknown_flavour? */
819 static struct core_fns lynx_core_fns
=
821 bfd_target_unknown_flavour
, /* core_flavour */
822 default_check_format
, /* check_format */
823 default_core_sniffer
, /* core_sniffer */
824 fetch_core_registers
, /* core_read_registers */
829 _initialize_core_lynx (void)
831 add_core_fns (&lynx_core_fns
);