139193Sjdp/* Machine independent GDB support for core files on systems using "regsets".
2130809Smarcel
3130809Smarcel   Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2003
498948Sobrien   Free Software Foundation, Inc.
539193Sjdp
698948Sobrien   This file is part of GDB.
739193Sjdp
898948Sobrien   This program is free software; you can redistribute it and/or modify
998948Sobrien   it under the terms of the GNU General Public License as published by
1098948Sobrien   the Free Software Foundation; either version 2 of the License, or
1198948Sobrien   (at your option) any later version.
1239193Sjdp
1398948Sobrien   This program is distributed in the hope that it will be useful,
1498948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1598948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1698948Sobrien   GNU General Public License for more details.
1739193Sjdp
1898948Sobrien   You should have received a copy of the GNU General Public License
1998948Sobrien   along with this program; if not, write to the Free Software
2098948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2198948Sobrien   Boston, MA 02111-1307, USA.  */
2239193Sjdp
23130809Smarcel/* This file is used by most systems that use ELF for their core
24130809Smarcel   dumps.  This includes most systems that have SVR4-ish variant of
25130809Smarcel   /proc.  For these systems, the registers are laid out the same way
26130809Smarcel   in core files as in the gregset_t and fpregset_t structures that
27130809Smarcel   are used in the interaction with /proc (Irix 4 is an exception and
28130809Smarcel   therefore doesn't use this file).  Quite a few systems without a
29130809Smarcel   SVR4-ish /proc define these structures too, and can make use of
30130809Smarcel   this code too.  */
3139193Sjdp
3239193Sjdp#include "defs.h"
33130809Smarcel#include "command.h"
34130809Smarcel#include "gdbcore.h"
35130809Smarcel#include "inferior.h"
36130809Smarcel#include "target.h"
3739193Sjdp
38130809Smarcel#include <fcntl.h>
39130809Smarcel#include <errno.h>
40130809Smarcel#include "gdb_string.h"
4139193Sjdp#include <time.h>
4239193Sjdp#ifdef HAVE_SYS_PROCFS_H
4339193Sjdp#include <sys/procfs.h>
4439193Sjdp#endif
4539193Sjdp
46130809Smarcel/* Prototypes for supply_gregset etc.  */
4798948Sobrien#include "gregset.h"
4846289Sdfr
49130809Smarcel/* Provide registers to GDB from a core file.
5046289Sdfr
51130809Smarcel   CORE_REG_SECT points to an array of bytes, which are the contents
52130809Smarcel   of a `note' from a core file which BFD thinks might contain
53130809Smarcel   register contents.  CORE_REG_SIZE is its size.
5498948Sobrien
55130809Smarcel   WHICH says which register set corelow suspects this is:
56130809Smarcel     0 --- the general-purpose register set, in gregset_t format
57130809Smarcel     2 --- the floating-point register set, in fpregset_t format
5839193Sjdp
59130809Smarcel   REG_ADDR is ignored.  */
6039193Sjdp
6139193Sjdpstatic void
6298948Sobrienfetch_core_registers (char *core_reg_sect, unsigned core_reg_size, int which,
6398948Sobrien		      CORE_ADDR reg_addr)
6439193Sjdp{
6598948Sobrien  gdb_gregset_t gregset;
6698948Sobrien  gdb_fpregset_t fpregset;
6739193Sjdp
68130809Smarcel  switch (which)
6939193Sjdp    {
70130809Smarcel    case 0:
7139193Sjdp      if (core_reg_size != sizeof (gregset))
72130809Smarcel	warning ("Wrong size gregset in core file.");
7339193Sjdp      else
7439193Sjdp	{
75130809Smarcel	  memcpy (&gregset, core_reg_sect, sizeof (gregset));
7639193Sjdp	  supply_gregset (&gregset);
7739193Sjdp	}
78130809Smarcel      break;
79130809Smarcel
80130809Smarcel    case 2:
8139193Sjdp      if (core_reg_size != sizeof (fpregset))
82130809Smarcel	warning ("Wrong size fpregset in core file.");
8339193Sjdp      else
8439193Sjdp	{
85130809Smarcel	  memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
8698948Sobrien	  if (FP0_REGNUM >= 0)
8798948Sobrien	    supply_fpregset (&fpregset);
8839193Sjdp	}
89130809Smarcel      break;
90130809Smarcel
91130809Smarcel    default:
92130809Smarcel      /* We've covered all the kinds of registers we know about here,
93130809Smarcel         so this must be something we wouldn't know what to do with
94130809Smarcel         anyway.  Just ignore it.  */
95130809Smarcel      break;
9639193Sjdp    }
9739193Sjdp}
9898948Sobrien
9939193Sjdp
100130809Smarcel/* Register that we are able to handle ELF core file formats using
101130809Smarcel   standard procfs "regset" structures.  */
10239193Sjdp
10339193Sjdpstatic struct core_fns regset_core_fns =
10439193Sjdp{
10598948Sobrien  bfd_target_elf_flavour,		/* core_flavour */
10698948Sobrien  default_check_format,			/* check_format */
10798948Sobrien  default_core_sniffer,			/* core_sniffer */
10898948Sobrien  fetch_core_registers,			/* core_read_registers */
10998948Sobrien  NULL					/* next */
11039193Sjdp};
11139193Sjdp
112130809Smarcel/* Provide a prototype to silence -Wmissing-prototypes.  */
113130809Smarcelextern void _initialize_core_regset (void);
114130809Smarcel
11539193Sjdpvoid
11698948Sobrien_initialize_core_regset (void)
11739193Sjdp{
11839193Sjdp  add_core_fns (&regset_core_fns);
11939193Sjdp}
120