1 /* Native-dependent code for LynxOS.
2 Copyright 1993, 1994, 1995, 1996, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
29 #include <sys/ptrace.h>
33 static unsigned long registers_addr (int pid
);
34 static void fetch_core_registers (char *, unsigned, int, CORE_ADDR
);
36 #define X(ENTRY)(offsetof(struct econtext, ENTRY))
39 /* Mappings from tm-i386v.h */
57 X (ecode
), /* Lynx doesn't give us either fs or gs, so */
58 X (fault
), /* we just substitute these two in the hopes
59 that they are useful. */
64 /* Mappings from tm-m68k.h */
78 X (regs
[10]), /* a2 */
79 X (regs
[11]), /* a3 */
80 X (regs
[12]), /* a4 */
81 X (regs
[13]), /* a5 */
82 X (regs
[14]), /* fp */
83 offsetof (st_t
, usp
) - offsetof (st_t
, ec
), /* sp */
87 X (fregs
[0 * 3]), /* fp0 */
88 X (fregs
[1 * 3]), /* fp1 */
89 X (fregs
[2 * 3]), /* fp2 */
90 X (fregs
[3 * 3]), /* fp3 */
91 X (fregs
[4 * 3]), /* fp4 */
92 X (fregs
[5 * 3]), /* fp5 */
93 X (fregs
[6 * 3]), /* fp6 */
94 X (fregs
[7 * 3]), /* fp7 */
96 X (fcregs
[0]), /* fpcontrol */
97 X (fcregs
[1]), /* fpstatus */
98 X (fcregs
[2]), /* fpiaddr */
100 X (fault
), /* fpflags */
105 /* Mappings from tm-sparc.h */
107 #define FX(ENTRY)(offsetof(struct fcontext, ENTRY))
109 static int regmap
[] =
116 -1, /* g5->g7 aren't saved by Lynx */
129 -1, -1, -1, -1, -1, -1, -1, -1, /* l0 -> l7 */
131 -1, -1, -1, -1, -1, -1, -1, -1, /* i0 -> i7 */
133 FX (f
.fregs
[0]), /* f0 */
179 static int regmap
[] =
181 X (iregs
[0]), /* r0 */
214 X (fregs
[0]), /* f0 */
247 X (srr0
), /* IAR (PC) */
248 X (srr1
), /* MSR (PS) */
260 /* This routine handles some oddball cases for Sparc registers and LynxOS.
261 In partucular, it causes refs to G0, g5->7, and all fp regs to return zero.
262 It also handles knows where to find the I & L regs on the stack. */
265 fetch_inferior_registers (int regno
)
269 #define WHATREGS_FLOAT 1
270 #define WHATREGS_GEN 2
271 #define WHATREGS_STACK 4
274 whatregs
= WHATREGS_FLOAT
| WHATREGS_GEN
| WHATREGS_STACK
;
275 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
276 whatregs
= WHATREGS_STACK
;
277 else if (regno
>= FP0_REGNUM
&& regno
< FP0_REGNUM
+ 32)
278 whatregs
= WHATREGS_FLOAT
;
280 whatregs
= WHATREGS_GEN
;
282 if (whatregs
& WHATREGS_GEN
)
284 struct econtext ec
; /* general regs */
285 char buf
[MAX_REGISTER_RAW_SIZE
];
290 retval
= ptrace (PTRACE_GETREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) & ec
,
293 perror_with_name ("ptrace(PTRACE_GETREGS)");
295 memset (buf
, 0, REGISTER_RAW_SIZE (G0_REGNUM
));
296 supply_register (G0_REGNUM
, buf
);
297 supply_register (TBR_REGNUM
, (char *) &ec
.tbr
);
299 memcpy (®isters
[REGISTER_BYTE (G1_REGNUM
)], &ec
.g1
,
300 4 * REGISTER_RAW_SIZE (G1_REGNUM
));
301 for (i
= G1_REGNUM
; i
<= G1_REGNUM
+ 3; i
++)
302 register_valid
[i
] = 1;
304 supply_register (PS_REGNUM
, (char *) &ec
.psr
);
305 supply_register (Y_REGNUM
, (char *) &ec
.y
);
306 supply_register (PC_REGNUM
, (char *) &ec
.pc
);
307 supply_register (NPC_REGNUM
, (char *) &ec
.npc
);
308 supply_register (WIM_REGNUM
, (char *) &ec
.wim
);
310 memcpy (®isters
[REGISTER_BYTE (O0_REGNUM
)], ec
.o
,
311 8 * REGISTER_RAW_SIZE (O0_REGNUM
));
312 for (i
= O0_REGNUM
; i
<= O0_REGNUM
+ 7; i
++)
313 register_valid
[i
] = 1;
316 if (whatregs
& WHATREGS_STACK
)
321 sp
= read_register (SP_REGNUM
);
323 target_read_memory (sp
+ FRAME_SAVED_I0
,
324 ®isters
[REGISTER_BYTE (I0_REGNUM
)],
325 8 * REGISTER_RAW_SIZE (I0_REGNUM
));
326 for (i
= I0_REGNUM
; i
<= I7_REGNUM
; i
++)
327 register_valid
[i
] = 1;
329 target_read_memory (sp
+ FRAME_SAVED_L0
,
330 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
331 8 * REGISTER_RAW_SIZE (L0_REGNUM
));
332 for (i
= L0_REGNUM
; i
<= L0_REGNUM
+ 7; i
++)
333 register_valid
[i
] = 1;
336 if (whatregs
& WHATREGS_FLOAT
)
338 struct fcontext fc
; /* fp regs */
343 retval
= ptrace (PTRACE_GETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) & fc
,
346 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
348 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], fc
.f
.fregs
,
349 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
350 for (i
= FP0_REGNUM
; i
<= FP0_REGNUM
+ 31; i
++)
351 register_valid
[i
] = 1;
353 supply_register (FPS_REGNUM
, (char *) &fc
.fsr
);
357 /* This routine handles storing of the I & L regs for the Sparc. The trick
358 here is that they actually live on the stack. The really tricky part is
359 that when changing the stack pointer, the I & L regs must be written to
360 where the new SP points, otherwise the regs will be incorrect when the
361 process is started up again. We assume that the I & L regs are valid at
365 store_inferior_registers (int 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])
415 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
416 target_write_memory (sp
+ FRAME_SAVED_I0
,
417 ®isters
[REGISTER_BYTE (I0_REGNUM
)],
418 8 * REGISTER_RAW_SIZE (I0_REGNUM
));
420 target_write_memory (sp
+ FRAME_SAVED_L0
,
421 ®isters
[REGISTER_BYTE (L0_REGNUM
)],
422 8 * REGISTER_RAW_SIZE (L0_REGNUM
));
424 else if (regno
>= L0_REGNUM
&& regno
<= I7_REGNUM
)
426 if (!register_valid
[regno
])
427 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
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_write_memory (sp
+ regoffset
,
435 ®isters
[REGISTER_BYTE (regno
)],
436 REGISTER_RAW_SIZE (regno
));
440 if (whatregs
& WHATREGS_FLOAT
)
442 struct fcontext fc
; /* fp regs */
445 /* We read fcontext first so that we can get good values for fq_t... */
447 retval
= ptrace (PTRACE_GETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) & fc
,
450 perror_with_name ("ptrace(PTRACE_GETFPREGS)");
452 memcpy (fc
.f
.fregs
, ®isters
[REGISTER_BYTE (FP0_REGNUM
)],
453 32 * REGISTER_RAW_SIZE (FP0_REGNUM
));
455 fc
.fsr
= read_register (FPS_REGNUM
);
458 retval
= ptrace (PTRACE_SETFPREGS
, inferior_pid
, (PTRACE_ARG3_TYPE
) & fc
,
461 perror_with_name ("ptrace(PTRACE_SETFPREGS)");
466 #if defined (I386) || defined (M68K) || defined (rs6000)
468 /* Return the offset relative to the start of the per-thread data to the
469 saved context block. */
472 registers_addr (int pid
)
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 (int 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 (int 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 (int pid
, struct target_waitstatus
*ourstatus
)
605 set_sigint_trap (); /* Causes SIGINT to be passed on to the
607 pid
= wait (&status
);
611 clear_sigint_trap ();
615 if (save_errno
== EINTR
)
617 fprintf_unfiltered (gdb_stderr
, "Child process unexpectedly missing: %s.\n",
618 safe_strerror (save_errno
));
619 /* Claim it exited with unknown signal. */
620 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
621 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
625 if (pid
!= PIDGET (inferior_pid
)) /* Some other process?!? */
628 thread
= status
.w_tid
; /* Get thread id from status */
630 /* Initial thread value can only be acquired via wait, so we have to
631 resort to this hack. */
633 if (TIDGET (inferior_pid
) == 0 && thread
!= 0)
635 inferior_pid
= BUILDPID (inferior_pid
, thread
);
636 add_thread (inferior_pid
);
639 pid
= BUILDPID (pid
, thread
);
641 /* We've become a single threaded process again. */
645 /* Check for thread creation. */
646 if (WIFSTOPPED (status
)
647 && WSTOPSIG (status
) == SIGTRAP
648 && !in_thread_list (pid
))
652 realsig
= ptrace (PTRACE_GETTRACESIG
, pid
, (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 (pid
))
673 realsig
= ptrace (PTRACE_GETTRACESIG
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
675 if (realsig
== SIGTHREADEXIT
)
677 ptrace (PTRACE_CONT
, PIDGET (pid
), (PTRACE_ARG3_TYPE
) 0, 0);
683 /* SPARC Lynx uses an byte reversed wait status; we must use the
684 host macros to access it. These lines just a copy of
685 store_waitstatus. We can't use CHILD_SPECIAL_WAITSTATUS
686 because target.c can't include the Lynx <sys/wait.h>. */
687 if (WIFEXITED (status
))
689 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
690 ourstatus
->value
.integer
= WEXITSTATUS (status
);
692 else if (!WIFSTOPPED (status
))
694 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
695 ourstatus
->value
.sig
=
696 target_signal_from_host (WTERMSIG (status
));
700 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
701 ourstatus
->value
.sig
=
702 target_signal_from_host (WSTOPSIG (status
));
705 store_waitstatus (ourstatus
, status
.w_status
);
712 /* Return nonzero if the given thread is still alive. */
714 child_thread_alive (int pid
)
716 /* Arggh. Apparently pthread_kill only works for threads within
717 the process that calls pthread_kill.
719 We want to avoid the lynx signal extensions as they simply don't
720 map well to the generic gdb interface we want to keep.
722 All we want to do is determine if a particular thread is alive;
723 it appears as if we can just make a harmless thread specific
724 ptrace call to do that. */
725 return (ptrace (PTRACE_THREADUSER
, pid
, 0, 0) != -1);
728 /* Resume execution of the inferior process.
729 If STEP is nonzero, single-step it.
730 If SIGNAL is nonzero, give it that signal. */
733 child_resume (int pid
, int step
, enum target_signal signal
)
739 /* If pid == -1, then we want to step/continue all threads, else
740 we only want to step/continue a single thread. */
744 func
= step
? PTRACE_SINGLESTEP
: PTRACE_CONT
;
747 func
= step
? PTRACE_SINGLESTEP_ONE
: PTRACE_CONT_ONE
;
750 /* An address of (PTRACE_ARG3_TYPE)1 tells ptrace to continue from where
751 it was. (If GDB wanted it to start some other way, we have already
752 written a new PC value to the child.)
754 If this system does not support PT_STEP, a higher level function will
755 have called single_step() to transmute the step request into a
756 continue request (by setting breakpoints on all possible successor
757 instructions), so we don't have to worry about that here. */
759 ptrace (func
, pid
, (PTRACE_ARG3_TYPE
) 1, target_signal_to_host (signal
));
762 perror_with_name ("ptrace");
765 /* Convert a Lynx process ID to a string. Returns the string in a static
769 child_pid_to_str (int pid
)
773 sprintf (buf
, "process %d thread %d", PIDGET (pid
), TIDGET (pid
));
778 /* Extract the register values out of the core file and store
779 them where `read_register' will find them.
781 CORE_REG_SECT points to the register values themselves, read into memory.
782 CORE_REG_SIZE is the size of that area.
783 WHICH says which set of registers we are handling (0 = int, 2 = float
784 on machines where they are discontiguous).
785 REG_ADDR is the offset from u.u_ar0 to the register values relative to
786 core_reg_sect. This is used with old-fashioned core files to
787 locate the registers in a large upage-plus-stack ".reg" section.
788 Original upage address X is at location core_reg_sect+x+reg_addr.
792 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
, int which
,
798 for (regno
= 0; regno
< NUM_REGS
; regno
++)
799 if (regmap
[regno
] != -1)
800 supply_register (regno
, core_reg_sect
+ offsetof (st_t
, ec
)
804 /* Fetching this register causes all of the I & L regs to be read from the
805 stack and validated. */
807 fetch_inferior_registers (I0_REGNUM
);
812 /* Register that we are able to handle lynx core file formats.
813 FIXME: is this really bfd_target_unknown_flavour? */
815 static struct core_fns lynx_core_fns
=
817 bfd_target_unknown_flavour
, /* core_flavour */
818 default_check_format
, /* check_format */
819 default_core_sniffer
, /* core_sniffer */
820 fetch_core_registers
, /* core_read_registers */
825 _initialize_core_lynx (void)
827 add_core_fns (&lynx_core_fns
);