xref: /dflybsd-src/contrib/gdb-7/gdb/corelow.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Core dump and executable file functions below target vector, for GDB.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4*cf7f2e2dSJohn Marino    1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
55796c8dcSSimon Schubert    Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "arch-utils.h"
245796c8dcSSimon Schubert #include "gdb_string.h"
255796c8dcSSimon Schubert #include <errno.h>
265796c8dcSSimon Schubert #include <signal.h>
275796c8dcSSimon Schubert #include <fcntl.h>
285796c8dcSSimon Schubert #ifdef HAVE_SYS_FILE_H
295796c8dcSSimon Schubert #include <sys/file.h>		/* needed for F_OK and friends */
305796c8dcSSimon Schubert #endif
315796c8dcSSimon Schubert #include "frame.h"		/* required by inferior.h */
325796c8dcSSimon Schubert #include "inferior.h"
335796c8dcSSimon Schubert #include "symtab.h"
345796c8dcSSimon Schubert #include "command.h"
355796c8dcSSimon Schubert #include "bfd.h"
365796c8dcSSimon Schubert #include "target.h"
375796c8dcSSimon Schubert #include "gdbcore.h"
385796c8dcSSimon Schubert #include "gdbthread.h"
395796c8dcSSimon Schubert #include "regcache.h"
405796c8dcSSimon Schubert #include "regset.h"
415796c8dcSSimon Schubert #include "symfile.h"
425796c8dcSSimon Schubert #include "exec.h"
435796c8dcSSimon Schubert #include "readline/readline.h"
445796c8dcSSimon Schubert #include "gdb_assert.h"
455796c8dcSSimon Schubert #include "exceptions.h"
465796c8dcSSimon Schubert #include "solib.h"
475796c8dcSSimon Schubert #include "filenames.h"
48*cf7f2e2dSJohn Marino #include "progspace.h"
49*cf7f2e2dSJohn Marino #include "objfiles.h"
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert #ifndef O_LARGEFILE
535796c8dcSSimon Schubert #define O_LARGEFILE 0
545796c8dcSSimon Schubert #endif
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert /* List of all available core_fns.  On gdb startup, each core file
575796c8dcSSimon Schubert    register reader calls deprecated_add_core_fns() to register
585796c8dcSSimon Schubert    information on each core format it is prepared to read.  */
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert static struct core_fns *core_file_fns = NULL;
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert /* The core_fns for a core file handler that is prepared to read the core
635796c8dcSSimon Schubert    file currently open on core_bfd. */
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert static struct core_fns *core_vec = NULL;
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert /* FIXME: kettenis/20031023: Eventually this variable should
685796c8dcSSimon Schubert    disappear.  */
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert struct gdbarch *core_gdbarch = NULL;
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert /* Per-core data.  Currently, only the section table.  Note that these
735796c8dcSSimon Schubert    target sections are *not* mapped in the current address spaces' set
745796c8dcSSimon Schubert    of target sections --- those should come only from pure executable
755796c8dcSSimon Schubert    or shared library bfds.  The core bfd sections are an
765796c8dcSSimon Schubert    implementation detail of the core target, just like ptrace is for
775796c8dcSSimon Schubert    unix child targets.  */
785796c8dcSSimon Schubert static struct target_section_table *core_data;
795796c8dcSSimon Schubert 
80*cf7f2e2dSJohn Marino /* True if we needed to fake the pid of the loaded core inferior.  */
81*cf7f2e2dSJohn Marino static int core_has_fake_pid = 0;
82*cf7f2e2dSJohn Marino 
835796c8dcSSimon Schubert static void core_files_info (struct target_ops *);
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert static struct core_fns *sniff_core_bfd (bfd *);
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert static int gdb_check_format (bfd *);
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert static void core_open (char *, int);
905796c8dcSSimon Schubert 
915796c8dcSSimon Schubert static void core_detach (struct target_ops *ops, char *, int);
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert static void core_close (int);
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert static void core_close_cleanup (void *ignore);
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert static void add_to_thread_list (bfd *, asection *, void *);
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert static void init_core_ops (void);
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert void _initialize_corelow (void);
1025796c8dcSSimon Schubert 
1035796c8dcSSimon Schubert struct target_ops core_ops;
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert /* An arbitrary identifier for the core inferior.  */
1065796c8dcSSimon Schubert #define CORELOW_PID 1
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert /* Link a new core_fns into the global core_file_fns list.  Called on gdb
1095796c8dcSSimon Schubert    startup by the _initialize routine in each core file register reader, to
1105796c8dcSSimon Schubert    register information about each format the the reader is prepared to
1115796c8dcSSimon Schubert    handle. */
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert void
1145796c8dcSSimon Schubert deprecated_add_core_fns (struct core_fns *cf)
1155796c8dcSSimon Schubert {
1165796c8dcSSimon Schubert   cf->next = core_file_fns;
1175796c8dcSSimon Schubert   core_file_fns = cf;
1185796c8dcSSimon Schubert }
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert /* The default function that core file handlers can use to examine a
1215796c8dcSSimon Schubert    core file BFD and decide whether or not to accept the job of
1225796c8dcSSimon Schubert    reading the core file. */
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert int
1255796c8dcSSimon Schubert default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
1265796c8dcSSimon Schubert {
1275796c8dcSSimon Schubert   int result;
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert   result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
1305796c8dcSSimon Schubert   return (result);
1315796c8dcSSimon Schubert }
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert /* Walk through the list of core functions to find a set that can
1345796c8dcSSimon Schubert    handle the core file open on ABFD.  Default to the first one in the
1355796c8dcSSimon Schubert    list if nothing matches.  Returns pointer to set that is
1365796c8dcSSimon Schubert    selected. */
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert static struct core_fns *
1395796c8dcSSimon Schubert sniff_core_bfd (bfd *abfd)
1405796c8dcSSimon Schubert {
1415796c8dcSSimon Schubert   struct core_fns *cf;
1425796c8dcSSimon Schubert   struct core_fns *yummy = NULL;
1435796c8dcSSimon Schubert   int matches = 0;;
1445796c8dcSSimon Schubert 
1455796c8dcSSimon Schubert   /* Don't sniff if we have support for register sets in CORE_GDBARCH.  */
1465796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
1475796c8dcSSimon Schubert     return NULL;
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert   for (cf = core_file_fns; cf != NULL; cf = cf->next)
1505796c8dcSSimon Schubert     {
1515796c8dcSSimon Schubert       if (cf->core_sniffer (cf, abfd))
1525796c8dcSSimon Schubert 	{
1535796c8dcSSimon Schubert 	  yummy = cf;
1545796c8dcSSimon Schubert 	  matches++;
1555796c8dcSSimon Schubert 	}
1565796c8dcSSimon Schubert     }
1575796c8dcSSimon Schubert   if (matches > 1)
1585796c8dcSSimon Schubert     {
1595796c8dcSSimon Schubert       warning (_("\"%s\": ambiguous core format, %d handlers match"),
1605796c8dcSSimon Schubert 	       bfd_get_filename (abfd), matches);
1615796c8dcSSimon Schubert     }
1625796c8dcSSimon Schubert   else if (matches == 0)
1635796c8dcSSimon Schubert     {
1645796c8dcSSimon Schubert       warning (_("\"%s\": no core file handler recognizes format, using default"),
1655796c8dcSSimon Schubert 	       bfd_get_filename (abfd));
1665796c8dcSSimon Schubert     }
1675796c8dcSSimon Schubert   if (yummy == NULL)
1685796c8dcSSimon Schubert     {
1695796c8dcSSimon Schubert       yummy = core_file_fns;
1705796c8dcSSimon Schubert     }
1715796c8dcSSimon Schubert   return (yummy);
1725796c8dcSSimon Schubert }
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert /* The default is to reject every core file format we see.  Either
1755796c8dcSSimon Schubert    BFD has to recognize it, or we have to provide a function in the
1765796c8dcSSimon Schubert    core file handler that recognizes it. */
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert int
1795796c8dcSSimon Schubert default_check_format (bfd *abfd)
1805796c8dcSSimon Schubert {
1815796c8dcSSimon Schubert   return (0);
1825796c8dcSSimon Schubert }
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert /* Attempt to recognize core file formats that BFD rejects. */
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert static int
1875796c8dcSSimon Schubert gdb_check_format (bfd *abfd)
1885796c8dcSSimon Schubert {
1895796c8dcSSimon Schubert   struct core_fns *cf;
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert   for (cf = core_file_fns; cf != NULL; cf = cf->next)
1925796c8dcSSimon Schubert     {
1935796c8dcSSimon Schubert       if (cf->check_format (abfd))
1945796c8dcSSimon Schubert 	{
1955796c8dcSSimon Schubert 	  return (1);
1965796c8dcSSimon Schubert 	}
1975796c8dcSSimon Schubert     }
1985796c8dcSSimon Schubert   return (0);
1995796c8dcSSimon Schubert }
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert /* Discard all vestiges of any previous core file and mark data and stack
2025796c8dcSSimon Schubert    spaces as empty.  */
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert static void
2055796c8dcSSimon Schubert core_close (int quitting)
2065796c8dcSSimon Schubert {
2075796c8dcSSimon Schubert   char *name;
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert   if (core_bfd)
2105796c8dcSSimon Schubert     {
2115796c8dcSSimon Schubert       int pid = ptid_get_pid (inferior_ptid);
2125796c8dcSSimon Schubert       inferior_ptid = null_ptid;	/* Avoid confusion from thread stuff */
213*cf7f2e2dSJohn Marino       exit_inferior_silent (pid);
2145796c8dcSSimon Schubert 
2155796c8dcSSimon Schubert       /* Clear out solib state while the bfd is still open. See
2165796c8dcSSimon Schubert          comments in clear_solib in solib.c. */
2175796c8dcSSimon Schubert       clear_solib ();
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert       xfree (core_data->sections);
2205796c8dcSSimon Schubert       xfree (core_data);
2215796c8dcSSimon Schubert       core_data = NULL;
222*cf7f2e2dSJohn Marino       core_has_fake_pid = 0;
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert       name = bfd_get_filename (core_bfd);
225*cf7f2e2dSJohn Marino       gdb_bfd_close_or_warn (core_bfd);
2265796c8dcSSimon Schubert       xfree (name);
2275796c8dcSSimon Schubert       core_bfd = NULL;
2285796c8dcSSimon Schubert     }
2295796c8dcSSimon Schubert   core_vec = NULL;
2305796c8dcSSimon Schubert   core_gdbarch = NULL;
2315796c8dcSSimon Schubert }
2325796c8dcSSimon Schubert 
2335796c8dcSSimon Schubert static void
2345796c8dcSSimon Schubert core_close_cleanup (void *ignore)
2355796c8dcSSimon Schubert {
2365796c8dcSSimon Schubert   core_close (0/*ignored*/);
2375796c8dcSSimon Schubert }
2385796c8dcSSimon Schubert 
2395796c8dcSSimon Schubert /* Look for sections whose names start with `.reg/' so that we can extract the
2405796c8dcSSimon Schubert    list of threads in a core file.  */
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert static void
2435796c8dcSSimon Schubert add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
2445796c8dcSSimon Schubert {
2455796c8dcSSimon Schubert   ptid_t ptid;
246*cf7f2e2dSJohn Marino   int core_tid;
247*cf7f2e2dSJohn Marino   int pid, lwpid;
2485796c8dcSSimon Schubert   asection *reg_sect = (asection *) reg_sect_arg;
2495796c8dcSSimon Schubert 
2505796c8dcSSimon Schubert   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
2515796c8dcSSimon Schubert     return;
2525796c8dcSSimon Schubert 
253*cf7f2e2dSJohn Marino   core_tid = atoi (bfd_section_name (abfd, asect) + 5);
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert   if (core_gdbarch
2565796c8dcSSimon Schubert       && gdbarch_core_reg_section_encodes_pid (core_gdbarch))
2575796c8dcSSimon Schubert     {
258*cf7f2e2dSJohn Marino       uint32_t merged_pid = core_tid;
259*cf7f2e2dSJohn Marino       pid = merged_pid & 0xffff;
260*cf7f2e2dSJohn Marino       lwpid = merged_pid >> 16;
261*cf7f2e2dSJohn Marino 
262*cf7f2e2dSJohn Marino       /* This can happen on solaris core, for example, if we don't
263*cf7f2e2dSJohn Marino 	 find a NT_PSTATUS note in the core, but do find NT_LWPSTATUS
264*cf7f2e2dSJohn Marino 	 notes.  */
265*cf7f2e2dSJohn Marino       if (pid == 0)
266*cf7f2e2dSJohn Marino 	{
267*cf7f2e2dSJohn Marino 	  core_has_fake_pid = 1;
268*cf7f2e2dSJohn Marino 	  pid = CORELOW_PID;
269*cf7f2e2dSJohn Marino 	}
2705796c8dcSSimon Schubert     }
2715796c8dcSSimon Schubert   else
272*cf7f2e2dSJohn Marino     {
273*cf7f2e2dSJohn Marino       core_has_fake_pid = 1;
274*cf7f2e2dSJohn Marino       pid = CORELOW_PID;
275*cf7f2e2dSJohn Marino       lwpid = core_tid;
276*cf7f2e2dSJohn Marino     }
2775796c8dcSSimon Schubert 
278*cf7f2e2dSJohn Marino   if (current_inferior ()->pid == 0)
279*cf7f2e2dSJohn Marino     inferior_appeared (current_inferior (), pid);
280*cf7f2e2dSJohn Marino 
281*cf7f2e2dSJohn Marino   ptid = ptid_build (pid, lwpid, 0);
282*cf7f2e2dSJohn Marino 
2835796c8dcSSimon Schubert   add_thread (ptid);
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert /* Warning, Will Robinson, looking at BFD private data! */
2865796c8dcSSimon Schubert 
2875796c8dcSSimon Schubert   if (reg_sect != NULL
2885796c8dcSSimon Schubert       && asect->filepos == reg_sect->filepos)	/* Did we find .reg? */
2895796c8dcSSimon Schubert     inferior_ptid = ptid;			 /* Yes, make it current */
2905796c8dcSSimon Schubert }
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert /* This routine opens and sets up the core file bfd.  */
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert static void
2955796c8dcSSimon Schubert core_open (char *filename, int from_tty)
2965796c8dcSSimon Schubert {
2975796c8dcSSimon Schubert   const char *p;
2985796c8dcSSimon Schubert   int siggy;
2995796c8dcSSimon Schubert   struct cleanup *old_chain;
3005796c8dcSSimon Schubert   char *temp;
3015796c8dcSSimon Schubert   bfd *temp_bfd;
3025796c8dcSSimon Schubert   int scratch_chan;
3035796c8dcSSimon Schubert   int flags;
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert   target_preopen (from_tty);
3065796c8dcSSimon Schubert   if (!filename)
3075796c8dcSSimon Schubert     {
3085796c8dcSSimon Schubert       if (core_bfd)
3095796c8dcSSimon Schubert 	error (_("No core file specified.  (Use `detach' to stop debugging a core file.)"));
3105796c8dcSSimon Schubert       else
3115796c8dcSSimon Schubert 	error (_("No core file specified."));
3125796c8dcSSimon Schubert     }
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert   filename = tilde_expand (filename);
3155796c8dcSSimon Schubert   if (!IS_ABSOLUTE_PATH(filename))
3165796c8dcSSimon Schubert     {
3175796c8dcSSimon Schubert       temp = concat (current_directory, "/", filename, (char *)NULL);
3185796c8dcSSimon Schubert       xfree (filename);
3195796c8dcSSimon Schubert       filename = temp;
3205796c8dcSSimon Schubert     }
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert   old_chain = make_cleanup (xfree, filename);
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert   flags = O_BINARY | O_LARGEFILE;
3255796c8dcSSimon Schubert   if (write_files)
3265796c8dcSSimon Schubert     flags |= O_RDWR;
3275796c8dcSSimon Schubert   else
3285796c8dcSSimon Schubert     flags |= O_RDONLY;
3295796c8dcSSimon Schubert   scratch_chan = open (filename, flags, 0);
3305796c8dcSSimon Schubert   if (scratch_chan < 0)
3315796c8dcSSimon Schubert     perror_with_name (filename);
3325796c8dcSSimon Schubert 
3335796c8dcSSimon Schubert   temp_bfd = bfd_fopen (filename, gnutarget,
3345796c8dcSSimon Schubert 			write_files ? FOPEN_RUB : FOPEN_RB,
3355796c8dcSSimon Schubert 			scratch_chan);
3365796c8dcSSimon Schubert   if (temp_bfd == NULL)
3375796c8dcSSimon Schubert     perror_with_name (filename);
3385796c8dcSSimon Schubert 
339*cf7f2e2dSJohn Marino   if (!bfd_check_format (temp_bfd, bfd_core)
340*cf7f2e2dSJohn Marino       && !gdb_check_format (temp_bfd))
3415796c8dcSSimon Schubert     {
3425796c8dcSSimon Schubert       /* Do it after the err msg */
3435796c8dcSSimon Schubert       /* FIXME: should be checking for errors from bfd_close (for one thing,
3445796c8dcSSimon Schubert          on error it does not free all the storage associated with the
3455796c8dcSSimon Schubert          bfd).  */
3465796c8dcSSimon Schubert       make_cleanup_bfd_close (temp_bfd);
3475796c8dcSSimon Schubert       error (_("\"%s\" is not a core dump: %s"),
3485796c8dcSSimon Schubert 	     filename, bfd_errmsg (bfd_get_error ()));
3495796c8dcSSimon Schubert     }
3505796c8dcSSimon Schubert 
3515796c8dcSSimon Schubert   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
3525796c8dcSSimon Schubert 
3535796c8dcSSimon Schubert   discard_cleanups (old_chain);	/* Don't free filename any more */
3545796c8dcSSimon Schubert   unpush_target (&core_ops);
3555796c8dcSSimon Schubert   core_bfd = temp_bfd;
3565796c8dcSSimon Schubert   old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert   /* FIXME: kettenis/20031023: This is very dangerous.  The
3595796c8dcSSimon Schubert      CORE_GDBARCH that results from this call may very well be
3605796c8dcSSimon Schubert      different from CURRENT_GDBARCH.  However, its methods may only
3615796c8dcSSimon Schubert      work if it is selected as the current architecture, because they
3625796c8dcSSimon Schubert      rely on swapped data (see gdbarch.c).  We should get rid of that
3635796c8dcSSimon Schubert      swapped data.  */
3645796c8dcSSimon Schubert   core_gdbarch = gdbarch_from_bfd (core_bfd);
3655796c8dcSSimon Schubert 
3665796c8dcSSimon Schubert   /* Find a suitable core file handler to munch on core_bfd */
3675796c8dcSSimon Schubert   core_vec = sniff_core_bfd (core_bfd);
3685796c8dcSSimon Schubert 
3695796c8dcSSimon Schubert   validate_files ();
3705796c8dcSSimon Schubert 
3715796c8dcSSimon Schubert   core_data = XZALLOC (struct target_section_table);
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert   /* Find the data section */
3745796c8dcSSimon Schubert   if (build_section_table (core_bfd,
3755796c8dcSSimon Schubert 			   &core_data->sections, &core_data->sections_end))
3765796c8dcSSimon Schubert     error (_("\"%s\": Can't find sections: %s"),
3775796c8dcSSimon Schubert 	   bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
3785796c8dcSSimon Schubert 
3795796c8dcSSimon Schubert   /* If we have no exec file, try to set the architecture from the
3805796c8dcSSimon Schubert      core file.  We don't do this unconditionally since an exec file
3815796c8dcSSimon Schubert      typically contains more information that helps us determine the
3825796c8dcSSimon Schubert      architecture than a core file.  */
3835796c8dcSSimon Schubert   if (!exec_bfd)
3845796c8dcSSimon Schubert     set_gdbarch_from_file (core_bfd);
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert   push_target (&core_ops);
3875796c8dcSSimon Schubert   discard_cleanups (old_chain);
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert   /* Do this before acknowledging the inferior, so if
3905796c8dcSSimon Schubert      post_create_inferior throws (can happen easilly if you're loading
3915796c8dcSSimon Schubert      a core file with the wrong exec), we aren't left with threads
3925796c8dcSSimon Schubert      from the previous inferior.  */
3935796c8dcSSimon Schubert   init_thread_list ();
3945796c8dcSSimon Schubert 
395*cf7f2e2dSJohn Marino   inferior_ptid = null_ptid;
396*cf7f2e2dSJohn Marino   core_has_fake_pid = 0;
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert   /* Need to flush the register cache (and the frame cache) from a
3995796c8dcSSimon Schubert      previous debug session.  If inferior_ptid ends up the same as the
4005796c8dcSSimon Schubert      last debug session --- e.g., b foo; run; gcore core1; step; gcore
4015796c8dcSSimon Schubert      core2; core core1; core core2 --- then there's potential for
4025796c8dcSSimon Schubert      get_current_regcache to return the cached regcache of the
4035796c8dcSSimon Schubert      previous session, and the frame cache being stale.  */
4045796c8dcSSimon Schubert   registers_changed ();
4055796c8dcSSimon Schubert 
4065796c8dcSSimon Schubert   /* Build up thread list from BFD sections, and possibly set the
4075796c8dcSSimon Schubert      current thread to the .reg/NN section matching the .reg
4085796c8dcSSimon Schubert      section. */
4095796c8dcSSimon Schubert   bfd_map_over_sections (core_bfd, add_to_thread_list,
4105796c8dcSSimon Schubert 			 bfd_get_section_by_name (core_bfd, ".reg"));
4115796c8dcSSimon Schubert 
412*cf7f2e2dSJohn Marino   if (ptid_equal (inferior_ptid, null_ptid))
413*cf7f2e2dSJohn Marino     {
414*cf7f2e2dSJohn Marino       /* Either we found no .reg/NN section, and hence we have a
415*cf7f2e2dSJohn Marino 	 non-threaded core (single-threaded, from gdb's perspective),
416*cf7f2e2dSJohn Marino 	 or for some reason add_to_thread_list couldn't determine
417*cf7f2e2dSJohn Marino 	 which was the "main" thread.  The latter case shouldn't
418*cf7f2e2dSJohn Marino 	 usually happen, but we're dealing with input here, which can
419*cf7f2e2dSJohn Marino 	 always be broken in different ways.  */
420*cf7f2e2dSJohn Marino       struct thread_info *thread = first_thread_of_process (-1);
421*cf7f2e2dSJohn Marino 
422*cf7f2e2dSJohn Marino       if (thread == NULL)
423*cf7f2e2dSJohn Marino 	{
424*cf7f2e2dSJohn Marino 	  inferior_appeared (current_inferior (), CORELOW_PID);
425*cf7f2e2dSJohn Marino 	  inferior_ptid = pid_to_ptid (CORELOW_PID);
426*cf7f2e2dSJohn Marino 	  add_thread_silent (inferior_ptid);
427*cf7f2e2dSJohn Marino 	}
428*cf7f2e2dSJohn Marino       else
429*cf7f2e2dSJohn Marino 	switch_to_thread (thread->ptid);
430*cf7f2e2dSJohn Marino     }
431*cf7f2e2dSJohn Marino 
4325796c8dcSSimon Schubert   post_create_inferior (&core_ops, from_tty);
4335796c8dcSSimon Schubert 
4345796c8dcSSimon Schubert   /* Now go through the target stack looking for threads since there
4355796c8dcSSimon Schubert      may be a thread_stratum target loaded on top of target core by
4365796c8dcSSimon Schubert      now.  The layer above should claim threads found in the BFD
4375796c8dcSSimon Schubert      sections.  */
4385796c8dcSSimon Schubert   target_find_new_threads ();
4395796c8dcSSimon Schubert 
4405796c8dcSSimon Schubert   p = bfd_core_file_failing_command (core_bfd);
4415796c8dcSSimon Schubert   if (p)
4425796c8dcSSimon Schubert     printf_filtered (_("Core was generated by `%s'.\n"), p);
4435796c8dcSSimon Schubert 
4445796c8dcSSimon Schubert   siggy = bfd_core_file_failing_signal (core_bfd);
4455796c8dcSSimon Schubert   if (siggy > 0)
4465796c8dcSSimon Schubert     /* NOTE: target_signal_from_host() converts a target signal value
4475796c8dcSSimon Schubert        into gdb's internal signal value.  Unfortunately gdb's internal
4485796c8dcSSimon Schubert        value is called ``target_signal'' and this function got the
4495796c8dcSSimon Schubert        name ..._from_host(). */
4505796c8dcSSimon Schubert     printf_filtered (_("Program terminated with signal %d, %s.\n"), siggy,
4515796c8dcSSimon Schubert 		     target_signal_to_string (
4525796c8dcSSimon Schubert 		       (core_gdbarch != NULL) ?
4535796c8dcSSimon Schubert 			gdbarch_target_signal_from_host (core_gdbarch, siggy)
4545796c8dcSSimon Schubert 			: siggy));
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert   /* Fetch all registers from core file.  */
4575796c8dcSSimon Schubert   target_fetch_registers (get_current_regcache (), -1);
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert   /* Now, set up the frame cache, and print the top of stack.  */
4605796c8dcSSimon Schubert   reinit_frame_cache ();
4615796c8dcSSimon Schubert   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
4625796c8dcSSimon Schubert }
4635796c8dcSSimon Schubert 
4645796c8dcSSimon Schubert static void
4655796c8dcSSimon Schubert core_detach (struct target_ops *ops, char *args, int from_tty)
4665796c8dcSSimon Schubert {
4675796c8dcSSimon Schubert   if (args)
4685796c8dcSSimon Schubert     error (_("Too many arguments"));
4695796c8dcSSimon Schubert   unpush_target (ops);
4705796c8dcSSimon Schubert   reinit_frame_cache ();
4715796c8dcSSimon Schubert   if (from_tty)
4725796c8dcSSimon Schubert     printf_filtered (_("No core file now.\n"));
4735796c8dcSSimon Schubert }
4745796c8dcSSimon Schubert 
4755796c8dcSSimon Schubert #ifdef DEPRECATED_IBM6000_TARGET
4765796c8dcSSimon Schubert 
4775796c8dcSSimon Schubert /* Resize the core memory's section table, by NUM_ADDED.  Returns a
4785796c8dcSSimon Schubert    pointer into the first new slot.  This will not be necessary when
4795796c8dcSSimon Schubert    the rs6000 target is converted to use the standard solib
4805796c8dcSSimon Schubert    framework.  */
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert struct target_section *
4835796c8dcSSimon Schubert deprecated_core_resize_section_table (int num_added)
4845796c8dcSSimon Schubert {
4855796c8dcSSimon Schubert   int old_count;
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert   old_count = resize_section_table (core_data, num_added);
4885796c8dcSSimon Schubert   return core_data->sections + old_count;
4895796c8dcSSimon Schubert }
4905796c8dcSSimon Schubert 
4915796c8dcSSimon Schubert #endif
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert /* Try to retrieve registers from a section in core_bfd, and supply
4945796c8dcSSimon Schubert    them to core_vec->core_read_registers, as the register set numbered
4955796c8dcSSimon Schubert    WHICH.
4965796c8dcSSimon Schubert 
4975796c8dcSSimon Schubert    If inferior_ptid's lwp member is zero, do the single-threaded
4985796c8dcSSimon Schubert    thing: look for a section named NAME.  If inferior_ptid's lwp
4995796c8dcSSimon Schubert    member is non-zero, do the multi-threaded thing: look for a section
5005796c8dcSSimon Schubert    named "NAME/LWP", where LWP is the shortest ASCII decimal
5015796c8dcSSimon Schubert    representation of inferior_ptid's lwp member.
5025796c8dcSSimon Schubert 
5035796c8dcSSimon Schubert    HUMAN_NAME is a human-readable name for the kind of registers the
5045796c8dcSSimon Schubert    NAME section contains, for use in error messages.
5055796c8dcSSimon Schubert 
5065796c8dcSSimon Schubert    If REQUIRED is non-zero, print an error if the core file doesn't
5075796c8dcSSimon Schubert    have a section by the appropriate name.  Otherwise, just do nothing.  */
5085796c8dcSSimon Schubert 
5095796c8dcSSimon Schubert static void
5105796c8dcSSimon Schubert get_core_register_section (struct regcache *regcache,
511*cf7f2e2dSJohn Marino 			   const char *name,
5125796c8dcSSimon Schubert 			   int which,
513*cf7f2e2dSJohn Marino 			   const char *human_name,
5145796c8dcSSimon Schubert 			   int required)
5155796c8dcSSimon Schubert {
5165796c8dcSSimon Schubert   static char *section_name = NULL;
5175796c8dcSSimon Schubert   struct bfd_section *section;
5185796c8dcSSimon Schubert   bfd_size_type size;
5195796c8dcSSimon Schubert   char *contents;
5205796c8dcSSimon Schubert 
5215796c8dcSSimon Schubert   xfree (section_name);
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert   if (core_gdbarch
5245796c8dcSSimon Schubert       && gdbarch_core_reg_section_encodes_pid (core_gdbarch))
5255796c8dcSSimon Schubert     {
5265796c8dcSSimon Schubert       uint32_t merged_pid;
527*cf7f2e2dSJohn Marino       int pid = ptid_get_pid (inferior_ptid);
528*cf7f2e2dSJohn Marino 
529*cf7f2e2dSJohn Marino       if (core_has_fake_pid)
530*cf7f2e2dSJohn Marino 	pid = 0;
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert       merged_pid = ptid_get_lwp (inferior_ptid);
533*cf7f2e2dSJohn Marino       merged_pid = merged_pid << 16 | pid;
5345796c8dcSSimon Schubert 
5355796c8dcSSimon Schubert       section_name = xstrprintf ("%s/%s", name, plongest (merged_pid));
5365796c8dcSSimon Schubert     }
5375796c8dcSSimon Schubert   else if (ptid_get_lwp (inferior_ptid))
5385796c8dcSSimon Schubert     section_name = xstrprintf ("%s/%ld", name, ptid_get_lwp (inferior_ptid));
5395796c8dcSSimon Schubert   else
5405796c8dcSSimon Schubert     section_name = xstrdup (name);
5415796c8dcSSimon Schubert 
5425796c8dcSSimon Schubert   section = bfd_get_section_by_name (core_bfd, section_name);
5435796c8dcSSimon Schubert   if (! section)
5445796c8dcSSimon Schubert     {
5455796c8dcSSimon Schubert       if (required)
5465796c8dcSSimon Schubert 	warning (_("Couldn't find %s registers in core file."), human_name);
5475796c8dcSSimon Schubert       return;
5485796c8dcSSimon Schubert     }
5495796c8dcSSimon Schubert 
5505796c8dcSSimon Schubert   size = bfd_section_size (core_bfd, section);
5515796c8dcSSimon Schubert   contents = alloca (size);
5525796c8dcSSimon Schubert   if (! bfd_get_section_contents (core_bfd, section, contents,
5535796c8dcSSimon Schubert 				  (file_ptr) 0, size))
5545796c8dcSSimon Schubert     {
5555796c8dcSSimon Schubert       warning (_("Couldn't read %s registers from `%s' section in core file."),
5565796c8dcSSimon Schubert 	       human_name, name);
5575796c8dcSSimon Schubert       return;
5585796c8dcSSimon Schubert     }
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
5615796c8dcSSimon Schubert     {
5625796c8dcSSimon Schubert       const struct regset *regset;
5635796c8dcSSimon Schubert 
5645796c8dcSSimon Schubert       regset = gdbarch_regset_from_core_section (core_gdbarch, name, size);
5655796c8dcSSimon Schubert       if (regset == NULL)
5665796c8dcSSimon Schubert 	{
5675796c8dcSSimon Schubert 	  if (required)
5685796c8dcSSimon Schubert 	    warning (_("Couldn't recognize %s registers in core file."),
5695796c8dcSSimon Schubert 		     human_name);
5705796c8dcSSimon Schubert 	  return;
5715796c8dcSSimon Schubert 	}
5725796c8dcSSimon Schubert 
5735796c8dcSSimon Schubert       regset->supply_regset (regset, regcache, -1, contents, size);
5745796c8dcSSimon Schubert       return;
5755796c8dcSSimon Schubert     }
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert   gdb_assert (core_vec);
5785796c8dcSSimon Schubert   core_vec->core_read_registers (regcache, contents, size, which,
5795796c8dcSSimon Schubert 				 ((CORE_ADDR)
5805796c8dcSSimon Schubert 				  bfd_section_vma (core_bfd, section)));
5815796c8dcSSimon Schubert }
5825796c8dcSSimon Schubert 
5835796c8dcSSimon Schubert 
5845796c8dcSSimon Schubert /* Get the registers out of a core file.  This is the machine-
5855796c8dcSSimon Schubert    independent part.  Fetch_core_registers is the machine-dependent
5865796c8dcSSimon Schubert    part, typically implemented in the xm-file for each architecture.  */
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert /* We just get all the registers, so we don't use regno.  */
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert static void
5915796c8dcSSimon Schubert get_core_registers (struct target_ops *ops,
5925796c8dcSSimon Schubert 		    struct regcache *regcache, int regno)
5935796c8dcSSimon Schubert {
594*cf7f2e2dSJohn Marino   struct core_regset_section *sect_list;
5955796c8dcSSimon Schubert   int i;
5965796c8dcSSimon Schubert 
5975796c8dcSSimon Schubert   if (!(core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
5985796c8dcSSimon Schubert       && (core_vec == NULL || core_vec->core_read_registers == NULL))
5995796c8dcSSimon Schubert     {
6005796c8dcSSimon Schubert       fprintf_filtered (gdb_stderr,
6015796c8dcSSimon Schubert 		     "Can't fetch registers from this type of core file\n");
6025796c8dcSSimon Schubert       return;
6035796c8dcSSimon Schubert     }
6045796c8dcSSimon Schubert 
605*cf7f2e2dSJohn Marino   sect_list = gdbarch_core_regset_sections (get_regcache_arch (regcache));
606*cf7f2e2dSJohn Marino   if (sect_list)
607*cf7f2e2dSJohn Marino     while (sect_list->sect_name != NULL)
608*cf7f2e2dSJohn Marino       {
609*cf7f2e2dSJohn Marino         if (strcmp (sect_list->sect_name, ".reg") == 0)
610*cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
611*cf7f2e2dSJohn Marino 				     0, sect_list->human_name, 1);
612*cf7f2e2dSJohn Marino         else if (strcmp (sect_list->sect_name, ".reg2") == 0)
613*cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
614*cf7f2e2dSJohn Marino 				     2, sect_list->human_name, 0);
615*cf7f2e2dSJohn Marino 	else
616*cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
617*cf7f2e2dSJohn Marino 				     3, sect_list->human_name, 0);
618*cf7f2e2dSJohn Marino 
619*cf7f2e2dSJohn Marino 	sect_list++;
620*cf7f2e2dSJohn Marino       }
621*cf7f2e2dSJohn Marino 
622*cf7f2e2dSJohn Marino   else
623*cf7f2e2dSJohn Marino     {
6245796c8dcSSimon Schubert       get_core_register_section (regcache,
6255796c8dcSSimon Schubert 				 ".reg", 0, "general-purpose", 1);
6265796c8dcSSimon Schubert       get_core_register_section (regcache,
6275796c8dcSSimon Schubert 				 ".reg2", 2, "floating-point", 0);
628*cf7f2e2dSJohn Marino     }
6295796c8dcSSimon Schubert 
6305796c8dcSSimon Schubert   /* Supply dummy value for all registers not found in the core.  */
6315796c8dcSSimon Schubert   for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
6325796c8dcSSimon Schubert     if (!regcache_valid_p (regcache, i))
6335796c8dcSSimon Schubert       regcache_raw_supply (regcache, i, NULL);
6345796c8dcSSimon Schubert }
6355796c8dcSSimon Schubert 
6365796c8dcSSimon Schubert static void
6375796c8dcSSimon Schubert core_files_info (struct target_ops *t)
6385796c8dcSSimon Schubert {
6395796c8dcSSimon Schubert   print_section_info (core_data, core_bfd);
6405796c8dcSSimon Schubert }
6415796c8dcSSimon Schubert 
6425796c8dcSSimon Schubert struct spuid_list
6435796c8dcSSimon Schubert {
6445796c8dcSSimon Schubert   gdb_byte *buf;
6455796c8dcSSimon Schubert   ULONGEST offset;
6465796c8dcSSimon Schubert   LONGEST len;
6475796c8dcSSimon Schubert   ULONGEST pos;
6485796c8dcSSimon Schubert   ULONGEST written;
6495796c8dcSSimon Schubert };
6505796c8dcSSimon Schubert 
6515796c8dcSSimon Schubert static void
6525796c8dcSSimon Schubert add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
6535796c8dcSSimon Schubert {
6545796c8dcSSimon Schubert   struct spuid_list *list = list_p;
6555796c8dcSSimon Schubert   enum bfd_endian byte_order
6565796c8dcSSimon Schubert     = bfd_big_endian (abfd)? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
6575796c8dcSSimon Schubert   int fd, pos = 0;
6585796c8dcSSimon Schubert 
6595796c8dcSSimon Schubert   sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
6605796c8dcSSimon Schubert   if (pos == 0)
6615796c8dcSSimon Schubert     return;
6625796c8dcSSimon Schubert 
6635796c8dcSSimon Schubert   if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
6645796c8dcSSimon Schubert     {
6655796c8dcSSimon Schubert       store_unsigned_integer (list->buf + list->pos - list->offset,
6665796c8dcSSimon Schubert 			      4, byte_order, fd);
6675796c8dcSSimon Schubert       list->written += 4;
6685796c8dcSSimon Schubert     }
6695796c8dcSSimon Schubert   list->pos += 4;
6705796c8dcSSimon Schubert }
6715796c8dcSSimon Schubert 
6725796c8dcSSimon Schubert static LONGEST
6735796c8dcSSimon Schubert core_xfer_partial (struct target_ops *ops, enum target_object object,
6745796c8dcSSimon Schubert 		   const char *annex, gdb_byte *readbuf,
6755796c8dcSSimon Schubert 		   const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
6765796c8dcSSimon Schubert {
6775796c8dcSSimon Schubert   switch (object)
6785796c8dcSSimon Schubert     {
6795796c8dcSSimon Schubert     case TARGET_OBJECT_MEMORY:
6805796c8dcSSimon Schubert       return section_table_xfer_memory_partial (readbuf, writebuf,
6815796c8dcSSimon Schubert 						offset, len,
6825796c8dcSSimon Schubert 						core_data->sections,
6835796c8dcSSimon Schubert 						core_data->sections_end,
6845796c8dcSSimon Schubert 						NULL);
6855796c8dcSSimon Schubert 
6865796c8dcSSimon Schubert     case TARGET_OBJECT_AUXV:
6875796c8dcSSimon Schubert       if (readbuf)
6885796c8dcSSimon Schubert 	{
6895796c8dcSSimon Schubert 	  /* When the aux vector is stored in core file, BFD
6905796c8dcSSimon Schubert 	     represents this with a fake section called ".auxv".  */
6915796c8dcSSimon Schubert 
6925796c8dcSSimon Schubert 	  struct bfd_section *section;
6935796c8dcSSimon Schubert 	  bfd_size_type size;
6945796c8dcSSimon Schubert 
6955796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, ".auxv");
6965796c8dcSSimon Schubert 	  if (section == NULL)
6975796c8dcSSimon Schubert 	    return -1;
6985796c8dcSSimon Schubert 
6995796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
7005796c8dcSSimon Schubert 	  if (offset >= size)
7015796c8dcSSimon Schubert 	    return 0;
7025796c8dcSSimon Schubert 	  size -= offset;
7035796c8dcSSimon Schubert 	  if (size > len)
7045796c8dcSSimon Schubert 	    size = len;
7055796c8dcSSimon Schubert 	  if (size > 0
7065796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
7075796c8dcSSimon Schubert 					    (file_ptr) offset, size))
7085796c8dcSSimon Schubert 	    {
7095796c8dcSSimon Schubert 	      warning (_("Couldn't read NT_AUXV note in core file."));
7105796c8dcSSimon Schubert 	      return -1;
7115796c8dcSSimon Schubert 	    }
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert 	  return size;
7145796c8dcSSimon Schubert 	}
7155796c8dcSSimon Schubert       return -1;
7165796c8dcSSimon Schubert 
7175796c8dcSSimon Schubert     case TARGET_OBJECT_WCOOKIE:
7185796c8dcSSimon Schubert       if (readbuf)
7195796c8dcSSimon Schubert 	{
7205796c8dcSSimon Schubert 	  /* When the StackGhost cookie is stored in core file, BFD
7215796c8dcSSimon Schubert 	     represents this with a fake section called ".wcookie".  */
7225796c8dcSSimon Schubert 
7235796c8dcSSimon Schubert 	  struct bfd_section *section;
7245796c8dcSSimon Schubert 	  bfd_size_type size;
7255796c8dcSSimon Schubert 
7265796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, ".wcookie");
7275796c8dcSSimon Schubert 	  if (section == NULL)
7285796c8dcSSimon Schubert 	    return -1;
7295796c8dcSSimon Schubert 
7305796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
7315796c8dcSSimon Schubert 	  if (offset >= size)
7325796c8dcSSimon Schubert 	    return 0;
7335796c8dcSSimon Schubert 	  size -= offset;
7345796c8dcSSimon Schubert 	  if (size > len)
7355796c8dcSSimon Schubert 	    size = len;
7365796c8dcSSimon Schubert 	  if (size > 0
7375796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
7385796c8dcSSimon Schubert 					    (file_ptr) offset, size))
7395796c8dcSSimon Schubert 	    {
7405796c8dcSSimon Schubert 	      warning (_("Couldn't read StackGhost cookie in core file."));
7415796c8dcSSimon Schubert 	      return -1;
7425796c8dcSSimon Schubert 	    }
7435796c8dcSSimon Schubert 
7445796c8dcSSimon Schubert 	  return size;
7455796c8dcSSimon Schubert 	}
7465796c8dcSSimon Schubert       return -1;
7475796c8dcSSimon Schubert 
7485796c8dcSSimon Schubert     case TARGET_OBJECT_LIBRARIES:
7495796c8dcSSimon Schubert       if (core_gdbarch
7505796c8dcSSimon Schubert 	  && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
7515796c8dcSSimon Schubert 	{
7525796c8dcSSimon Schubert 	  if (writebuf)
7535796c8dcSSimon Schubert 	    return -1;
7545796c8dcSSimon Schubert 	  return
7555796c8dcSSimon Schubert 	    gdbarch_core_xfer_shared_libraries (core_gdbarch,
7565796c8dcSSimon Schubert 						readbuf, offset, len);
7575796c8dcSSimon Schubert 	}
7585796c8dcSSimon Schubert       /* FALL THROUGH */
7595796c8dcSSimon Schubert 
7605796c8dcSSimon Schubert     case TARGET_OBJECT_SPU:
7615796c8dcSSimon Schubert       if (readbuf && annex)
7625796c8dcSSimon Schubert 	{
7635796c8dcSSimon Schubert 	  /* When the SPU contexts are stored in a core file, BFD
7645796c8dcSSimon Schubert 	     represents this with a fake section called "SPU/<annex>".  */
7655796c8dcSSimon Schubert 
7665796c8dcSSimon Schubert 	  struct bfd_section *section;
7675796c8dcSSimon Schubert 	  bfd_size_type size;
7685796c8dcSSimon Schubert 	  char sectionstr[100];
769*cf7f2e2dSJohn Marino 
7705796c8dcSSimon Schubert 	  xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
7715796c8dcSSimon Schubert 
7725796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, sectionstr);
7735796c8dcSSimon Schubert 	  if (section == NULL)
7745796c8dcSSimon Schubert 	    return -1;
7755796c8dcSSimon Schubert 
7765796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
7775796c8dcSSimon Schubert 	  if (offset >= size)
7785796c8dcSSimon Schubert 	    return 0;
7795796c8dcSSimon Schubert 	  size -= offset;
7805796c8dcSSimon Schubert 	  if (size > len)
7815796c8dcSSimon Schubert 	    size = len;
7825796c8dcSSimon Schubert 	  if (size > 0
7835796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
7845796c8dcSSimon Schubert 					    (file_ptr) offset, size))
7855796c8dcSSimon Schubert 	    {
7865796c8dcSSimon Schubert 	      warning (_("Couldn't read SPU section in core file."));
7875796c8dcSSimon Schubert 	      return -1;
7885796c8dcSSimon Schubert 	    }
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert 	  return size;
7915796c8dcSSimon Schubert 	}
7925796c8dcSSimon Schubert       else if (readbuf)
7935796c8dcSSimon Schubert 	{
7945796c8dcSSimon Schubert 	  /* NULL annex requests list of all present spuids.  */
7955796c8dcSSimon Schubert 	  struct spuid_list list;
796*cf7f2e2dSJohn Marino 
7975796c8dcSSimon Schubert 	  list.buf = readbuf;
7985796c8dcSSimon Schubert 	  list.offset = offset;
7995796c8dcSSimon Schubert 	  list.len = len;
8005796c8dcSSimon Schubert 	  list.pos = 0;
8015796c8dcSSimon Schubert 	  list.written = 0;
8025796c8dcSSimon Schubert 	  bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
8035796c8dcSSimon Schubert 	  return list.written;
8045796c8dcSSimon Schubert 	}
8055796c8dcSSimon Schubert       return -1;
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert     default:
8085796c8dcSSimon Schubert       if (ops->beneath != NULL)
8095796c8dcSSimon Schubert 	return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
8105796c8dcSSimon Schubert 					      readbuf, writebuf, offset, len);
8115796c8dcSSimon Schubert       return -1;
8125796c8dcSSimon Schubert     }
8135796c8dcSSimon Schubert }
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert 
8165796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say
8175796c8dcSSimon Schubert    `gdb internal error' (since generic_mourn calls breakpoint_init_inferior).  */
8185796c8dcSSimon Schubert 
8195796c8dcSSimon Schubert static int
8205796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
8215796c8dcSSimon Schubert {
8225796c8dcSSimon Schubert   return 0;
8235796c8dcSSimon Schubert }
8245796c8dcSSimon Schubert 
8255796c8dcSSimon Schubert 
8265796c8dcSSimon Schubert /* Okay, let's be honest: threads gleaned from a core file aren't
8275796c8dcSSimon Schubert    exactly lively, are they?  On the other hand, if we don't claim
8285796c8dcSSimon Schubert    that each & every one is alive, then we don't get any of them
8295796c8dcSSimon Schubert    to appear in an "info thread" command, which is quite a useful
8305796c8dcSSimon Schubert    behaviour.
8315796c8dcSSimon Schubert  */
8325796c8dcSSimon Schubert static int
8335796c8dcSSimon Schubert core_thread_alive (struct target_ops *ops, ptid_t ptid)
8345796c8dcSSimon Schubert {
8355796c8dcSSimon Schubert   return 1;
8365796c8dcSSimon Schubert }
8375796c8dcSSimon Schubert 
8385796c8dcSSimon Schubert /* Ask the current architecture what it knows about this core file.
8395796c8dcSSimon Schubert    That will be used, in turn, to pick a better architecture.  This
8405796c8dcSSimon Schubert    wrapper could be avoided if targets got a chance to specialize
8415796c8dcSSimon Schubert    core_ops.  */
8425796c8dcSSimon Schubert 
8435796c8dcSSimon Schubert static const struct target_desc *
8445796c8dcSSimon Schubert core_read_description (struct target_ops *target)
8455796c8dcSSimon Schubert {
8465796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
8475796c8dcSSimon Schubert     return gdbarch_core_read_description (core_gdbarch, target, core_bfd);
8485796c8dcSSimon Schubert 
8495796c8dcSSimon Schubert   return NULL;
8505796c8dcSSimon Schubert }
8515796c8dcSSimon Schubert 
8525796c8dcSSimon Schubert static char *
8535796c8dcSSimon Schubert core_pid_to_str (struct target_ops *ops, ptid_t ptid)
8545796c8dcSSimon Schubert {
8555796c8dcSSimon Schubert   static char buf[64];
8565796c8dcSSimon Schubert 
8575796c8dcSSimon Schubert   if (core_gdbarch
8585796c8dcSSimon Schubert       && gdbarch_core_pid_to_str_p (core_gdbarch))
8595796c8dcSSimon Schubert     {
8605796c8dcSSimon Schubert       char *ret = gdbarch_core_pid_to_str (core_gdbarch, ptid);
861*cf7f2e2dSJohn Marino 
8625796c8dcSSimon Schubert       if (ret != NULL)
8635796c8dcSSimon Schubert 	return ret;
8645796c8dcSSimon Schubert     }
8655796c8dcSSimon Schubert 
8665796c8dcSSimon Schubert   if (ptid_get_lwp (ptid) == 0)
8675796c8dcSSimon Schubert     xsnprintf (buf, sizeof buf, "<main task>");
8685796c8dcSSimon Schubert   else
8695796c8dcSSimon Schubert     xsnprintf (buf, sizeof buf, "Thread %ld", ptid_get_lwp (ptid));
8705796c8dcSSimon Schubert 
8715796c8dcSSimon Schubert   return buf;
8725796c8dcSSimon Schubert }
8735796c8dcSSimon Schubert 
8745796c8dcSSimon Schubert static int
8755796c8dcSSimon Schubert core_has_memory (struct target_ops *ops)
8765796c8dcSSimon Schubert {
8775796c8dcSSimon Schubert   return (core_bfd != NULL);
8785796c8dcSSimon Schubert }
8795796c8dcSSimon Schubert 
8805796c8dcSSimon Schubert static int
8815796c8dcSSimon Schubert core_has_stack (struct target_ops *ops)
8825796c8dcSSimon Schubert {
8835796c8dcSSimon Schubert   return (core_bfd != NULL);
8845796c8dcSSimon Schubert }
8855796c8dcSSimon Schubert 
8865796c8dcSSimon Schubert static int
8875796c8dcSSimon Schubert core_has_registers (struct target_ops *ops)
8885796c8dcSSimon Schubert {
8895796c8dcSSimon Schubert   return (core_bfd != NULL);
8905796c8dcSSimon Schubert }
8915796c8dcSSimon Schubert 
8925796c8dcSSimon Schubert /* Fill in core_ops with its defined operations and properties.  */
8935796c8dcSSimon Schubert 
8945796c8dcSSimon Schubert static void
8955796c8dcSSimon Schubert init_core_ops (void)
8965796c8dcSSimon Schubert {
8975796c8dcSSimon Schubert   core_ops.to_shortname = "core";
8985796c8dcSSimon Schubert   core_ops.to_longname = "Local core dump file";
8995796c8dcSSimon Schubert   core_ops.to_doc =
9005796c8dcSSimon Schubert     "Use a core file as a target.  Specify the filename of the core file.";
9015796c8dcSSimon Schubert   core_ops.to_open = core_open;
9025796c8dcSSimon Schubert   core_ops.to_close = core_close;
9035796c8dcSSimon Schubert   core_ops.to_attach = find_default_attach;
9045796c8dcSSimon Schubert   core_ops.to_detach = core_detach;
9055796c8dcSSimon Schubert   core_ops.to_fetch_registers = get_core_registers;
9065796c8dcSSimon Schubert   core_ops.to_xfer_partial = core_xfer_partial;
9075796c8dcSSimon Schubert   core_ops.to_files_info = core_files_info;
9085796c8dcSSimon Schubert   core_ops.to_insert_breakpoint = ignore;
9095796c8dcSSimon Schubert   core_ops.to_remove_breakpoint = ignore;
9105796c8dcSSimon Schubert   core_ops.to_create_inferior = find_default_create_inferior;
9115796c8dcSSimon Schubert   core_ops.to_thread_alive = core_thread_alive;
9125796c8dcSSimon Schubert   core_ops.to_read_description = core_read_description;
9135796c8dcSSimon Schubert   core_ops.to_pid_to_str = core_pid_to_str;
9145796c8dcSSimon Schubert   core_ops.to_stratum = core_stratum;
9155796c8dcSSimon Schubert   core_ops.to_has_memory = core_has_memory;
9165796c8dcSSimon Schubert   core_ops.to_has_stack = core_has_stack;
9175796c8dcSSimon Schubert   core_ops.to_has_registers = core_has_registers;
9185796c8dcSSimon Schubert   core_ops.to_magic = OPS_MAGIC;
9195796c8dcSSimon Schubert }
9205796c8dcSSimon Schubert 
9215796c8dcSSimon Schubert void
9225796c8dcSSimon Schubert _initialize_corelow (void)
9235796c8dcSSimon Schubert {
9245796c8dcSSimon Schubert   init_core_ops ();
9255796c8dcSSimon Schubert 
9265796c8dcSSimon Schubert   add_target (&core_ops);
9275796c8dcSSimon Schubert }
928