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
));
32 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
35 /* Mappings from tm-i386v.h */
53 X(ecode
), /* Lynx doesn't give us either fs or gs, so */
54 X(fault
), /* we just substitute these two in the hopes
55 that they are useful. */
60 /* Mappings from tm-m68k.h */
79 offsetof (st_t
, usp
) - offsetof (st_t
, ec
), /* sp */
83 X(fregs
[0*3]), /* fp0 */
84 X(fregs
[1*3]), /* fp1 */
85 X(fregs
[2*3]), /* fp2 */
86 X(fregs
[3*3]), /* fp3 */
87 X(fregs
[4*3]), /* fp4 */
88 X(fregs
[5*3]), /* fp5 */
89 X(fregs
[6*3]), /* fp6 */
90 X(fregs
[7*3]), /* fp7 */
92 X(fcregs
[0]), /* fpcontrol */
93 X(fcregs
[1]), /* fpstatus */
94 X(fcregs
[2]), /* fpiaddr */
96 X(fault
), /* fpflags */
101 /* Mappings from tm-sparc.h */
103 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
105 static int regmap
[] =
112 -1, /* g5->g7 aren't saved by Lynx */
125 -1,-1,-1,-1,-1,-1,-1,-1, /* l0 -> l7 */
127 -1,-1,-1,-1,-1,-1,-1,-1, /* i0 -> i7 */
129 FX(f
.fregs
[0]), /* f0 */
175 static int regmap
[] =
177 X(iregs
[0]), /* r0 */
210 X(fregs
[0]), /* f0 */
243 X(srr0
), /* IAR (PC) */
244 X(srr1
), /* MSR (PS) */
256 /* This routine handles some oddball cases for Sparc registers and LynxOS.
257 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
258 It also handles knows where to find the I & L regs on the stack. */
261 fetch_inferior_registers (regno
)
266 #define WHATREGS_FLOAT 1
267 #define WHATREGS_GEN 2
268 #define WHATREGS_STACK 4
271 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
272 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
273 whatregs
= WHATREGS_STACK
;
274 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
275 whatregs
= WHATREGS_FLOAT
;
277 whatregs
= WHATREGS_GEN
;
279 if (whatregs
& WHATREGS_GEN
)
281 struct econtext ec
; /* general regs */
282 char buf
[MAX_REGISTER_RAW_SIZE
];
287 retval
= ptrace (PTRACE_GETREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &ec
,
290 perror_with_name ("ptrace(PTRACE_GETREGS)");
292 memset (buf
, 0, REGISTER_RAW_SIZE (G0_REGNUM
));
293 supply_register (G0_REGNUM
, buf
);
294 supply_register (TBR_REGNUM
, (char *)&ec
.tbr
);
296 memcpy (®isters
[REGISTER_BYTE (G1_REGNUM
)], &ec
.g1
,
297 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
298 for (i
= G1_REGNUM
; i
<= G1_REGNUM
+ 3; i
++)
299 register_valid
[i
] = 1;
301 supply_register (PS_REGNUM
, (char *)&ec
.psr
);
302 supply_register (Y_REGNUM
, (char *)&ec
.y
);
303 supply_register (PC_REGNUM
, (char *)&ec
.pc
);
304 supply_register (NPC_REGNUM
, (char *)&ec
.npc
);
305 supply_register (WIM_REGNUM
, (char *)&ec
.wim
);
307 memcpy (®isters
[REGISTER_BYTE (O0_REGNUM
)], ec
.o
,
308 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
309 for (i
= O0_REGNUM
; i
<= O0_REGNUM
+ 7; i
++)
310 register_valid
[i
] = 1;
313 if (whatregs
& WHATREGS_STACK
)
318 sp
= read_register (SP_REGNUM
);
320 target_xfer_memory (sp
+ FRAME_SAVED_I0
,
321 ®isters
[REGISTER_BYTE(I0_REGNUM
)],
322 8 * REGISTER_RAW_SIZE (I0_REGNUM
), 0);
323 for (i
= I0_REGNUM
; i
<= I7_REGNUM
; i
++)
324 register_valid
[i
] = 1;
326 target_xfer_memory (sp
+ FRAME_SAVED_L0
,
327 ®isters
[REGISTER_BYTE(L0_REGNUM
)],
328 8 * REGISTER_RAW_SIZE (L0_REGNUM
), 0);
329 for (i
= L0_REGNUM
; i
<= L0_REGNUM
+ 7; i
++)
330 register_valid
[i
] = 1;
333 if (whatregs
& WHATREGS_FLOAT
)
335 struct fcontext fc
; /* fp regs */
340 retval
= ptrace (PTRACE_GETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &fc
,
343 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
345 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], fc
.f
.fregs
,
346 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
347 for (i
= FP0_REGNUM
; i
<= FP0_REGNUM
+ 31; i
++)
348 register_valid
[i
] = 1;
350 supply_register (FPS_REGNUM
, (char *)&fc
.fsr
);
354 /* This routine handles storing of the I & L regs for the Sparc. The trick
355 here is that they actually live on the stack. The really tricky part is
356 that when changing the stack pointer, the I & L regs must be written to
357 where the new SP points, otherwise the regs will be incorrect when the
358 process is started up again. We assume that the I & L regs are valid at
362 store_inferior_registers (regno
)
368 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
369 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
370 whatregs
= WHATREGS_STACK
;
371 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
372 whatregs
= WHATREGS_FLOAT
;
373 else if (regno
== SP_REGNUM
)
374 whatregs
= WHATREGS_STACK
| WHATREGS_GEN
;
376 whatregs
= WHATREGS_GEN
;
378 if (whatregs
& WHATREGS_GEN
)
380 struct econtext ec
; /* general regs */
383 ec
.tbr
= read_register (TBR_REGNUM
);
384 memcpy (&ec
.g1
, ®isters
[REGISTER_BYTE (G1_REGNUM
)],
385 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
387 ec
.psr
= read_register (PS_REGNUM
);
388 ec
.y
= read_register (Y_REGNUM
);
389 ec
.pc
= read_register (PC_REGNUM
);
390 ec
.npc
= read_register (NPC_REGNUM
);
391 ec
.wim
= read_register (WIM_REGNUM
);
393 memcpy (ec
.o
, ®isters
[REGISTER_BYTE (O0_REGNUM
)],
394 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
397 retval
= ptrace (PTRACE_SETREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &ec
,
400 perror_with_name ("ptrace(PTRACE_SETREGS)");
403 if (whatregs
& WHATREGS_STACK
)
408 sp
= read_register (SP_REGNUM
);
410 if (regno
== -1 || regno
== SP_REGNUM
)
412 if (!register_valid
[L0_REGNUM
+5])
414 target_xfer_memory (sp
+ FRAME_SAVED_I0
,
415 ®isters
[REGISTER_BYTE (I0_REGNUM
)],
416 8 * REGISTER_RAW_SIZE (I0_REGNUM
), 1);
418 target_xfer_memory (sp
+ FRAME_SAVED_L0
,
419 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
420 8 * REGISTER_RAW_SIZE (L0_REGNUM
), 1);
422 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
424 if (!register_valid
[regno
])
426 if (regno
>= L0_REGNUM
&& regno
<= L0_REGNUM
+ 7)
427 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (L0_REGNUM
)
430 regoffset
= REGISTER_BYTE (regno
) - REGISTER_BYTE (I0_REGNUM
)
432 target_xfer_memory (sp
+ regoffset
, ®isters
[REGISTER_BYTE (regno
)],
433 REGISTER_RAW_SIZE (regno
), 1);
437 if (whatregs
& WHATREGS_FLOAT
)
439 struct fcontext fc
; /* fp regs */
442 /* We read fcontext first so that we can get good values for fq_t... */
444 retval
= ptrace (PTRACE_GETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &fc
,
447 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
449 memcpy (fc
.f
.fregs
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
450 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
452 fc
.fsr
= read_register (FPS_REGNUM
);
455 retval
= ptrace (PTRACE_SETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) &fc
,
458 perror_with_name ("ptrace(PTRACE_SETFPREGS)");
463 #if defined (I386) || defined (M68K) || defined (rs6000)
465 /* Return the offset relative to the start of the per-thread data to the
466 saved context block. */
473 int ecpoff
= offsetof(st_t
, ecp
);
477 stblock
= (CORE_ADDR
) ptrace (PTRACE_THREADUSER
, pid
, (PTRACE_ARG3_TYPE
)0,
480 perror_with_name ("ptrace(PTRACE_THREADUSER)");
482 ecp
= (CORE_ADDR
) ptrace (PTRACE_PEEKTHREAD
, pid
, (PTRACE_ARG3_TYPE
)ecpoff
,
485 perror_with_name ("ptrace(PTRACE_PEEKTHREAD)");
487 return ecp
- stblock
;
490 /* Fetch one or more registers from the inferior. REGNO == -1 to get
491 them all. We actually fetch more than requested, when convenient,
492 marking them as valid so we won't fetch them again. */
495 fetch_inferior_registers (regno
)
505 reghi
= NUM_REGS
- 1;
508 reglo
= reghi
= regno
;
510 ecp
= registers_addr (inferior_pid
);
512 for (regno
= reglo
; regno
<= reghi
; regno
++)
514 char buf
[MAX_REGISTER_RAW_SIZE
];
515 int ptrace_fun
= PTRACE_PEEKTHREAD
;
518 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_PEEKUSP
: PTRACE_PEEKTHREAD
;
521 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
526 reg
= ptrace (ptrace_fun
, inferior_pid
,
527 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
] + i
), 0);
529 perror_with_name ("ptrace(PTRACE_PEEKUSP)");
531 *(int *)&buf
[i
] = reg
;
533 supply_register (regno
, buf
);
537 /* Store our register values back into the inferior.
538 If REGNO is -1, do this for all registers.
539 Otherwise, REGNO specifies which register (so we can save time). */
541 /* Registers we shouldn't try to store. */
542 #if !defined (CANNOT_STORE_REGISTER)
543 #define CANNOT_STORE_REGISTER(regno) 0
547 store_inferior_registers (regno
)
557 reghi
= NUM_REGS
- 1;
560 reglo
= reghi
= regno
;
562 ecp
= registers_addr (inferior_pid
);
564 for (regno
= reglo
; regno
<= reghi
; regno
++)
566 int ptrace_fun
= PTRACE_POKEUSER
;
568 if (CANNOT_STORE_REGISTER (regno
))
572 ptrace_fun
= regno
== SP_REGNUM
? PTRACE_POKEUSP
: PTRACE_POKEUSER
;
575 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
579 reg
= *(unsigned int *)®isters
[REGISTER_BYTE (regno
) + i
];
582 ptrace (ptrace_fun
, inferior_pid
,
583 (PTRACE_ARG3_TYPE
) (ecp
+ regmap
[regno
] + i
), reg
);
585 perror_with_name ("ptrace(PTRACE_POKEUSP)");
589 #endif /* defined (I386) || defined (M68K) || defined (rs6000) */
591 /* Wait for child to do something. Return pid of child, or -1 in case
592 of error; store status through argument pointer OURSTATUS. */
595 child_wait (pid
, ourstatus
)
597 struct target_waitstatus
*ourstatus
;
607 set_sigint_trap(); /* Causes SIGINT to be passed on to the
609 pid
= wait (&status
);
617 if (save_errno
== EINTR
)
619 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
620 safe_strerror (save_errno
));
621 /* Claim it exited with unknown signal. */
622 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
623 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
627 if (pid
!= PIDGET (inferior_pid
)) /* Some other process?!? */
630 thread
= status
.w_tid
; /* Get thread id from status */
632 /* Initial thread value can only be acquired via wait, so we have to
633 resort to this hack. */
635 if (TIDGET (inferior_pid
) == 0 && thread
!= 0)
637 inferior_pid
= BUILDPID (inferior_pid
, thread
);
638 add_thread (inferior_pid
);
641 pid
= BUILDPID (pid
, thread
);
643 /* We've become a single threaded process again. */
647 /* Check for thread creation. */
648 if (WIFSTOPPED(status
)
649 && WSTOPSIG(status
) == SIGTRAP
650 && !in_thread_list (pid
))
654 realsig
= ptrace (PTRACE_GETTRACESIG
, pid
, (PTRACE_ARG3_TYPE
)0, 0);
656 if (realsig
== SIGNEWTHREAD
)
658 /* It's a new thread notification. We don't want to much with
659 realsig -- the code in wait_for_inferior expects SIGTRAP. */
660 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
661 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
665 error ("Signal for unknown thread was not SIGNEWTHREAD");
668 /* Check for thread termination. */
669 else if (WIFSTOPPED(status
)
670 && WSTOPSIG(status
) == SIGTRAP
671 && in_thread_list (pid
))
675 realsig
= ptrace (PTRACE_GETTRACESIG
, pid
, (PTRACE_ARG3_TYPE
)0, 0);
677 if (realsig
== SIGTHREADEXIT
)
679 ptrace (PTRACE_CONT
, PIDGET (pid
), (PTRACE_ARG3_TYPE
)0, 0);
685 /* SPARC Lynx uses an byte reversed wait status; we must use the
686 host macros to access it. These lines just a copy of
687 store_waitstatus. We can't use CHILD_SPECIAL_WAITSTATUS
688 because target.c can't include the Lynx <sys/wait.h>. */
689 if (WIFEXITED (status
))
691 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
692 ourstatus
->value
.integer
= WEXITSTATUS (status
);
694 else if (!WIFSTOPPED (status
))
696 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
697 ourstatus
->value
.sig
=
698 target_signal_from_host (WTERMSIG (status
));
702 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
703 ourstatus
->value
.sig
=
704 target_signal_from_host (WSTOPSIG (status
));
707 store_waitstatus (ourstatus
, status
.w_status
);
714 /* Return nonzero if the given thread is still alive. */
716 child_thread_alive (pid
)
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 (pid
, step
, signal
)
739 enum target_signal signal
;
745 /* If pid == -1, then we want to step/continue all threads, else
746 we only want to step/continue a single thread. */
750 func
= step
? PTRACE_SINGLESTEP
: PTRACE_CONT
;
753 func
= step
? PTRACE_SINGLESTEP_ONE
: PTRACE_CONT_ONE
;
756 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
757 it was. (If GDB wanted it to start some other way, we have already
758 written a new PC value to the child.)
760 If this system does not support PT_STEP, a higher level function will
761 have called single_step() to transmute the step request into a
762 continue request (by setting breakpoints on all possible successor
763 instructions), so we don't have to worry about that here. */
765 ptrace (func
, pid
, (PTRACE_ARG3_TYPE
) 1, target_signal_to_host (signal
));
768 perror_with_name ("ptrace");
771 /* Convert a Lynx process ID to a string. Returns the string in a static
775 lynx_pid_to_str (pid
)
780 sprintf (buf
, "process %d thread %d", PIDGET (pid
), TIDGET (pid
));
785 /* Extract the register values out of the core file and store
786 them where `read_register' will find them.
788 CORE_REG_SECT points to the register values themselves, read into memory.
789 CORE_REG_SIZE is the size of that area.
790 WHICH says which set of registers we are handling (0 = int, 2 = float
791 on machines where they are discontiguous).
792 REG_ADDR is the offset from u.u_ar0 to the register values relative to
793 core_reg_sect. This is used with old-fashioned core files to
794 locate the registers in a large upage-plus-stack ".reg" section.
795 Original upage address X is at location core_reg_sect+x+reg_addr.
799 fetch_core_registers (core_reg_sect
, core_reg_size
, which
, reg_addr
)
801 unsigned core_reg_size
;
808 for (regno
= 0; regno
< NUM_REGS
; regno
++)
809 if (regmap
[regno
] != -1)
810 supply_register (regno
, core_reg_sect
+ offsetof (st_t
, ec
)
814 /* Fetching this register causes all of the I & L regs to be read from the
815 stack and validated. */
817 fetch_inferior_registers (I0_REGNUM
);
822 /* Register that we are able to handle lynx core file formats.
823 FIXME: is this really bfd_target_unknown_flavour? */
825 static struct core_fns lynx_core_fns
=
827 bfd_target_unknown_flavour
,
828 fetch_core_registers
,
833 _initialize_core_lynx ()
835 add_core_fns (&lynx_core_fns
);