xref: /dflybsd-src/contrib/gdb-7/gdb/corelow.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
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*c50c785cSJohn Marino    1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5*c50c785cSJohn Marino    2011 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"
48cf7f2e2dSJohn Marino #include "progspace.h"
49cf7f2e2dSJohn 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 
62*c50c785cSJohn Marino /* The core_fns for a core file handler that is prepared to read the
63*c50c785cSJohn Marino    core 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 
80cf7f2e2dSJohn Marino /* True if we needed to fake the pid of the loaded core inferior.  */
81cf7f2e2dSJohn Marino static int core_has_fake_pid = 0;
82cf7f2e2dSJohn 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 
103*c50c785cSJohn Marino static struct target_ops core_ops;
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert /* An arbitrary identifier for the core inferior.  */
1065796c8dcSSimon Schubert #define CORELOW_PID 1
1075796c8dcSSimon Schubert 
108*c50c785cSJohn Marino /* Link a new core_fns into the global core_file_fns list.  Called on
109*c50c785cSJohn Marino    gdb startup by the _initialize routine in each core file register
110*c50c785cSJohn Marino    reader, to register information about each format the reader is
111*c50c785cSJohn Marino    prepared to 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 
145*c50c785cSJohn Marino   /* Don't sniff if we have support for register sets in
146*c50c785cSJohn Marino      CORE_GDBARCH.  */
1475796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
1485796c8dcSSimon Schubert     return NULL;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert   for (cf = core_file_fns; cf != NULL; cf = cf->next)
1515796c8dcSSimon Schubert     {
1525796c8dcSSimon Schubert       if (cf->core_sniffer (cf, abfd))
1535796c8dcSSimon Schubert 	{
1545796c8dcSSimon Schubert 	  yummy = cf;
1555796c8dcSSimon Schubert 	  matches++;
1565796c8dcSSimon Schubert 	}
1575796c8dcSSimon Schubert     }
1585796c8dcSSimon Schubert   if (matches > 1)
1595796c8dcSSimon Schubert     {
1605796c8dcSSimon Schubert       warning (_("\"%s\": ambiguous core format, %d handlers match"),
1615796c8dcSSimon Schubert 	       bfd_get_filename (abfd), matches);
1625796c8dcSSimon Schubert     }
1635796c8dcSSimon Schubert   else if (matches == 0)
1645796c8dcSSimon Schubert     {
165*c50c785cSJohn Marino       warning (_("\"%s\": no core file handler "
166*c50c785cSJohn Marino 		 "recognizes format, using default"),
1675796c8dcSSimon Schubert 	       bfd_get_filename (abfd));
1685796c8dcSSimon Schubert     }
1695796c8dcSSimon Schubert   if (yummy == NULL)
1705796c8dcSSimon Schubert     {
1715796c8dcSSimon Schubert       yummy = core_file_fns;
1725796c8dcSSimon Schubert     }
1735796c8dcSSimon Schubert   return (yummy);
1745796c8dcSSimon Schubert }
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert /* The default is to reject every core file format we see.  Either
1775796c8dcSSimon Schubert    BFD has to recognize it, or we have to provide a function in the
1785796c8dcSSimon Schubert    core file handler that recognizes it.  */
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert int
1815796c8dcSSimon Schubert default_check_format (bfd *abfd)
1825796c8dcSSimon Schubert {
1835796c8dcSSimon Schubert   return (0);
1845796c8dcSSimon Schubert }
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert /* Attempt to recognize core file formats that BFD rejects.  */
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert static int
1895796c8dcSSimon Schubert gdb_check_format (bfd *abfd)
1905796c8dcSSimon Schubert {
1915796c8dcSSimon Schubert   struct core_fns *cf;
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert   for (cf = core_file_fns; cf != NULL; cf = cf->next)
1945796c8dcSSimon Schubert     {
1955796c8dcSSimon Schubert       if (cf->check_format (abfd))
1965796c8dcSSimon Schubert 	{
1975796c8dcSSimon Schubert 	  return (1);
1985796c8dcSSimon Schubert 	}
1995796c8dcSSimon Schubert     }
2005796c8dcSSimon Schubert   return (0);
2015796c8dcSSimon Schubert }
2025796c8dcSSimon Schubert 
203*c50c785cSJohn Marino /* Discard all vestiges of any previous core file and mark data and
204*c50c785cSJohn Marino    stack spaces as empty.  */
2055796c8dcSSimon Schubert 
2065796c8dcSSimon Schubert static void
2075796c8dcSSimon Schubert core_close (int quitting)
2085796c8dcSSimon Schubert {
2095796c8dcSSimon Schubert   char *name;
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert   if (core_bfd)
2125796c8dcSSimon Schubert     {
2135796c8dcSSimon Schubert       int pid = ptid_get_pid (inferior_ptid);
214*c50c785cSJohn Marino       inferior_ptid = null_ptid;    /* Avoid confusion from thread
215*c50c785cSJohn Marino 				       stuff.  */
216cf7f2e2dSJohn Marino       exit_inferior_silent (pid);
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert       /* Clear out solib state while the bfd is still open.  See
2195796c8dcSSimon Schubert          comments in clear_solib in solib.c.  */
2205796c8dcSSimon Schubert       clear_solib ();
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert       xfree (core_data->sections);
2235796c8dcSSimon Schubert       xfree (core_data);
2245796c8dcSSimon Schubert       core_data = NULL;
225cf7f2e2dSJohn Marino       core_has_fake_pid = 0;
2265796c8dcSSimon Schubert 
2275796c8dcSSimon Schubert       name = bfd_get_filename (core_bfd);
228cf7f2e2dSJohn Marino       gdb_bfd_close_or_warn (core_bfd);
2295796c8dcSSimon Schubert       xfree (name);
2305796c8dcSSimon Schubert       core_bfd = NULL;
2315796c8dcSSimon Schubert     }
2325796c8dcSSimon Schubert   core_vec = NULL;
2335796c8dcSSimon Schubert   core_gdbarch = NULL;
2345796c8dcSSimon Schubert }
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert static void
2375796c8dcSSimon Schubert core_close_cleanup (void *ignore)
2385796c8dcSSimon Schubert {
2395796c8dcSSimon Schubert   core_close (0/*ignored*/);
2405796c8dcSSimon Schubert }
2415796c8dcSSimon Schubert 
242*c50c785cSJohn Marino /* Look for sections whose names start with `.reg/' so that we can
243*c50c785cSJohn Marino    extract the list of threads in a core file.  */
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert static void
2465796c8dcSSimon Schubert add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
2475796c8dcSSimon Schubert {
2485796c8dcSSimon Schubert   ptid_t ptid;
249cf7f2e2dSJohn Marino   int core_tid;
250cf7f2e2dSJohn Marino   int pid, lwpid;
2515796c8dcSSimon Schubert   asection *reg_sect = (asection *) reg_sect_arg;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
2545796c8dcSSimon Schubert     return;
2555796c8dcSSimon Schubert 
256cf7f2e2dSJohn Marino   core_tid = atoi (bfd_section_name (abfd, asect) + 5);
2575796c8dcSSimon Schubert 
258*c50c785cSJohn Marino   pid = bfd_core_file_pid (core_bfd);
259cf7f2e2dSJohn Marino   if (pid == 0)
260cf7f2e2dSJohn Marino     {
261cf7f2e2dSJohn Marino       core_has_fake_pid = 1;
262cf7f2e2dSJohn Marino       pid = CORELOW_PID;
263cf7f2e2dSJohn Marino     }
264*c50c785cSJohn Marino 
265cf7f2e2dSJohn Marino   lwpid = core_tid;
2665796c8dcSSimon Schubert 
267cf7f2e2dSJohn Marino   if (current_inferior ()->pid == 0)
268cf7f2e2dSJohn Marino     inferior_appeared (current_inferior (), pid);
269cf7f2e2dSJohn Marino 
270cf7f2e2dSJohn Marino   ptid = ptid_build (pid, lwpid, 0);
271cf7f2e2dSJohn Marino 
2725796c8dcSSimon Schubert   add_thread (ptid);
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert /* Warning, Will Robinson, looking at BFD private data! */
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert   if (reg_sect != NULL
2775796c8dcSSimon Schubert       && asect->filepos == reg_sect->filepos)	/* Did we find .reg?  */
278*c50c785cSJohn Marino     inferior_ptid = ptid;			/* Yes, make it current.  */
2795796c8dcSSimon Schubert }
2805796c8dcSSimon Schubert 
2815796c8dcSSimon Schubert /* This routine opens and sets up the core file bfd.  */
2825796c8dcSSimon Schubert 
2835796c8dcSSimon Schubert static void
2845796c8dcSSimon Schubert core_open (char *filename, int from_tty)
2855796c8dcSSimon Schubert {
2865796c8dcSSimon Schubert   const char *p;
2875796c8dcSSimon Schubert   int siggy;
2885796c8dcSSimon Schubert   struct cleanup *old_chain;
2895796c8dcSSimon Schubert   char *temp;
2905796c8dcSSimon Schubert   bfd *temp_bfd;
2915796c8dcSSimon Schubert   int scratch_chan;
2925796c8dcSSimon Schubert   int flags;
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert   target_preopen (from_tty);
2955796c8dcSSimon Schubert   if (!filename)
2965796c8dcSSimon Schubert     {
2975796c8dcSSimon Schubert       if (core_bfd)
298*c50c785cSJohn Marino 	error (_("No core file specified.  (Use `detach' "
299*c50c785cSJohn Marino 		 "to stop debugging a core file.)"));
3005796c8dcSSimon Schubert       else
3015796c8dcSSimon Schubert 	error (_("No core file specified."));
3025796c8dcSSimon Schubert     }
3035796c8dcSSimon Schubert 
3045796c8dcSSimon Schubert   filename = tilde_expand (filename);
3055796c8dcSSimon Schubert   if (!IS_ABSOLUTE_PATH (filename))
3065796c8dcSSimon Schubert     {
307*c50c785cSJohn Marino       temp = concat (current_directory, "/",
308*c50c785cSJohn Marino 		     filename, (char *) NULL);
3095796c8dcSSimon Schubert       xfree (filename);
3105796c8dcSSimon Schubert       filename = temp;
3115796c8dcSSimon Schubert     }
3125796c8dcSSimon Schubert 
3135796c8dcSSimon Schubert   old_chain = make_cleanup (xfree, filename);
3145796c8dcSSimon Schubert 
3155796c8dcSSimon Schubert   flags = O_BINARY | O_LARGEFILE;
3165796c8dcSSimon Schubert   if (write_files)
3175796c8dcSSimon Schubert     flags |= O_RDWR;
3185796c8dcSSimon Schubert   else
3195796c8dcSSimon Schubert     flags |= O_RDONLY;
3205796c8dcSSimon Schubert   scratch_chan = open (filename, flags, 0);
3215796c8dcSSimon Schubert   if (scratch_chan < 0)
3225796c8dcSSimon Schubert     perror_with_name (filename);
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert   temp_bfd = bfd_fopen (filename, gnutarget,
3255796c8dcSSimon Schubert 			write_files ? FOPEN_RUB : FOPEN_RB,
3265796c8dcSSimon Schubert 			scratch_chan);
3275796c8dcSSimon Schubert   if (temp_bfd == NULL)
3285796c8dcSSimon Schubert     perror_with_name (filename);
3295796c8dcSSimon Schubert 
330cf7f2e2dSJohn Marino   if (!bfd_check_format (temp_bfd, bfd_core)
331cf7f2e2dSJohn Marino       && !gdb_check_format (temp_bfd))
3325796c8dcSSimon Schubert     {
3335796c8dcSSimon Schubert       /* Do it after the err msg */
334*c50c785cSJohn Marino       /* FIXME: should be checking for errors from bfd_close (for one
335*c50c785cSJohn Marino          thing, on error it does not free all the storage associated
336*c50c785cSJohn Marino          with the bfd).  */
3375796c8dcSSimon Schubert       make_cleanup_bfd_close (temp_bfd);
3385796c8dcSSimon Schubert       error (_("\"%s\" is not a core dump: %s"),
3395796c8dcSSimon Schubert 	     filename, bfd_errmsg (bfd_get_error ()));
3405796c8dcSSimon Schubert     }
3415796c8dcSSimon Schubert 
342*c50c785cSJohn Marino   /* Looks semi-reasonable.  Toss the old core file and work on the
343*c50c785cSJohn Marino      new.  */
3445796c8dcSSimon Schubert 
3455796c8dcSSimon Schubert   discard_cleanups (old_chain);	/* Don't free filename any more */
3465796c8dcSSimon Schubert   unpush_target (&core_ops);
3475796c8dcSSimon Schubert   core_bfd = temp_bfd;
3485796c8dcSSimon Schubert   old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
3495796c8dcSSimon Schubert 
3505796c8dcSSimon Schubert   /* FIXME: kettenis/20031023: This is very dangerous.  The
3515796c8dcSSimon Schubert      CORE_GDBARCH that results from this call may very well be
3525796c8dcSSimon Schubert      different from CURRENT_GDBARCH.  However, its methods may only
3535796c8dcSSimon Schubert      work if it is selected as the current architecture, because they
3545796c8dcSSimon Schubert      rely on swapped data (see gdbarch.c).  We should get rid of that
3555796c8dcSSimon Schubert      swapped data.  */
3565796c8dcSSimon Schubert   core_gdbarch = gdbarch_from_bfd (core_bfd);
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert   /* Find a suitable core file handler to munch on core_bfd */
3595796c8dcSSimon Schubert   core_vec = sniff_core_bfd (core_bfd);
3605796c8dcSSimon Schubert 
3615796c8dcSSimon Schubert   validate_files ();
3625796c8dcSSimon Schubert 
3635796c8dcSSimon Schubert   core_data = XZALLOC (struct target_section_table);
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert   /* Find the data section */
3665796c8dcSSimon Schubert   if (build_section_table (core_bfd,
367*c50c785cSJohn Marino 			   &core_data->sections,
368*c50c785cSJohn Marino 			   &core_data->sections_end))
3695796c8dcSSimon Schubert     error (_("\"%s\": Can't find sections: %s"),
3705796c8dcSSimon Schubert 	   bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
3715796c8dcSSimon Schubert 
3725796c8dcSSimon Schubert   /* If we have no exec file, try to set the architecture from the
3735796c8dcSSimon Schubert      core file.  We don't do this unconditionally since an exec file
3745796c8dcSSimon Schubert      typically contains more information that helps us determine the
3755796c8dcSSimon Schubert      architecture than a core file.  */
3765796c8dcSSimon Schubert   if (!exec_bfd)
3775796c8dcSSimon Schubert     set_gdbarch_from_file (core_bfd);
3785796c8dcSSimon Schubert 
3795796c8dcSSimon Schubert   push_target (&core_ops);
3805796c8dcSSimon Schubert   discard_cleanups (old_chain);
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert   /* Do this before acknowledging the inferior, so if
3835796c8dcSSimon Schubert      post_create_inferior throws (can happen easilly if you're loading
3845796c8dcSSimon Schubert      a core file with the wrong exec), we aren't left with threads
3855796c8dcSSimon Schubert      from the previous inferior.  */
3865796c8dcSSimon Schubert   init_thread_list ();
3875796c8dcSSimon Schubert 
388cf7f2e2dSJohn Marino   inferior_ptid = null_ptid;
389cf7f2e2dSJohn Marino   core_has_fake_pid = 0;
3905796c8dcSSimon Schubert 
3915796c8dcSSimon Schubert   /* Need to flush the register cache (and the frame cache) from a
3925796c8dcSSimon Schubert      previous debug session.  If inferior_ptid ends up the same as the
3935796c8dcSSimon Schubert      last debug session --- e.g., b foo; run; gcore core1; step; gcore
3945796c8dcSSimon Schubert      core2; core core1; core core2 --- then there's potential for
3955796c8dcSSimon Schubert      get_current_regcache to return the cached regcache of the
3965796c8dcSSimon Schubert      previous session, and the frame cache being stale.  */
3975796c8dcSSimon Schubert   registers_changed ();
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert   /* Build up thread list from BFD sections, and possibly set the
4005796c8dcSSimon Schubert      current thread to the .reg/NN section matching the .reg
4015796c8dcSSimon Schubert      section.  */
4025796c8dcSSimon Schubert   bfd_map_over_sections (core_bfd, add_to_thread_list,
4035796c8dcSSimon Schubert 			 bfd_get_section_by_name (core_bfd, ".reg"));
4045796c8dcSSimon Schubert 
405cf7f2e2dSJohn Marino   if (ptid_equal (inferior_ptid, null_ptid))
406cf7f2e2dSJohn Marino     {
407cf7f2e2dSJohn Marino       /* Either we found no .reg/NN section, and hence we have a
408cf7f2e2dSJohn Marino 	 non-threaded core (single-threaded, from gdb's perspective),
409cf7f2e2dSJohn Marino 	 or for some reason add_to_thread_list couldn't determine
410cf7f2e2dSJohn Marino 	 which was the "main" thread.  The latter case shouldn't
411cf7f2e2dSJohn Marino 	 usually happen, but we're dealing with input here, which can
412cf7f2e2dSJohn Marino 	 always be broken in different ways.  */
413cf7f2e2dSJohn Marino       struct thread_info *thread = first_thread_of_process (-1);
414cf7f2e2dSJohn Marino 
415cf7f2e2dSJohn Marino       if (thread == NULL)
416cf7f2e2dSJohn Marino 	{
417cf7f2e2dSJohn Marino 	  inferior_appeared (current_inferior (), CORELOW_PID);
418cf7f2e2dSJohn Marino 	  inferior_ptid = pid_to_ptid (CORELOW_PID);
419cf7f2e2dSJohn Marino 	  add_thread_silent (inferior_ptid);
420cf7f2e2dSJohn Marino 	}
421cf7f2e2dSJohn Marino       else
422cf7f2e2dSJohn Marino 	switch_to_thread (thread->ptid);
423cf7f2e2dSJohn Marino     }
424cf7f2e2dSJohn Marino 
4255796c8dcSSimon Schubert   post_create_inferior (&core_ops, from_tty);
4265796c8dcSSimon Schubert 
4275796c8dcSSimon Schubert   /* Now go through the target stack looking for threads since there
4285796c8dcSSimon Schubert      may be a thread_stratum target loaded on top of target core by
4295796c8dcSSimon Schubert      now.  The layer above should claim threads found in the BFD
4305796c8dcSSimon Schubert      sections.  */
4315796c8dcSSimon Schubert   target_find_new_threads ();
4325796c8dcSSimon Schubert 
4335796c8dcSSimon Schubert   p = bfd_core_file_failing_command (core_bfd);
4345796c8dcSSimon Schubert   if (p)
4355796c8dcSSimon Schubert     printf_filtered (_("Core was generated by `%s'.\n"), p);
4365796c8dcSSimon Schubert 
4375796c8dcSSimon Schubert   siggy = bfd_core_file_failing_signal (core_bfd);
4385796c8dcSSimon Schubert   if (siggy > 0)
439*c50c785cSJohn Marino     {
440*c50c785cSJohn Marino       /* NOTE: target_signal_from_host() converts a target signal
441*c50c785cSJohn Marino 	 value into gdb's internal signal value.  Unfortunately gdb's
442*c50c785cSJohn Marino 	 internal value is called ``target_signal'' and this function
443*c50c785cSJohn Marino 	 got the name ..._from_host().  */
444*c50c785cSJohn Marino       enum target_signal sig = (core_gdbarch != NULL
445*c50c785cSJohn Marino 		       ? gdbarch_target_signal_from_host (core_gdbarch,
446*c50c785cSJohn Marino 							  siggy)
447*c50c785cSJohn Marino 		       : target_signal_from_host (siggy));
448*c50c785cSJohn Marino 
449*c50c785cSJohn Marino       printf_filtered (_("Program terminated with signal %d, %s.\n"),
450*c50c785cSJohn Marino 		       siggy, target_signal_to_string (sig));
451*c50c785cSJohn Marino     }
4525796c8dcSSimon Schubert 
4535796c8dcSSimon Schubert   /* Fetch all registers from core file.  */
4545796c8dcSSimon Schubert   target_fetch_registers (get_current_regcache (), -1);
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert   /* Now, set up the frame cache, and print the top of stack.  */
4575796c8dcSSimon Schubert   reinit_frame_cache ();
4585796c8dcSSimon Schubert   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
4595796c8dcSSimon Schubert }
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert static void
4625796c8dcSSimon Schubert core_detach (struct target_ops *ops, char *args, int from_tty)
4635796c8dcSSimon Schubert {
4645796c8dcSSimon Schubert   if (args)
4655796c8dcSSimon Schubert     error (_("Too many arguments"));
4665796c8dcSSimon Schubert   unpush_target (ops);
4675796c8dcSSimon Schubert   reinit_frame_cache ();
4685796c8dcSSimon Schubert   if (from_tty)
4695796c8dcSSimon Schubert     printf_filtered (_("No core file now.\n"));
4705796c8dcSSimon Schubert }
4715796c8dcSSimon Schubert 
4725796c8dcSSimon Schubert #ifdef DEPRECATED_IBM6000_TARGET
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert /* Resize the core memory's section table, by NUM_ADDED.  Returns a
4755796c8dcSSimon Schubert    pointer into the first new slot.  This will not be necessary when
4765796c8dcSSimon Schubert    the rs6000 target is converted to use the standard solib
4775796c8dcSSimon Schubert    framework.  */
4785796c8dcSSimon Schubert 
4795796c8dcSSimon Schubert struct target_section *
4805796c8dcSSimon Schubert deprecated_core_resize_section_table (int num_added)
4815796c8dcSSimon Schubert {
4825796c8dcSSimon Schubert   int old_count;
4835796c8dcSSimon Schubert 
4845796c8dcSSimon Schubert   old_count = resize_section_table (core_data, num_added);
4855796c8dcSSimon Schubert   return core_data->sections + old_count;
4865796c8dcSSimon Schubert }
4875796c8dcSSimon Schubert 
4885796c8dcSSimon Schubert #endif
4895796c8dcSSimon Schubert 
4905796c8dcSSimon Schubert /* Try to retrieve registers from a section in core_bfd, and supply
4915796c8dcSSimon Schubert    them to core_vec->core_read_registers, as the register set numbered
4925796c8dcSSimon Schubert    WHICH.
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert    If inferior_ptid's lwp member is zero, do the single-threaded
4955796c8dcSSimon Schubert    thing: look for a section named NAME.  If inferior_ptid's lwp
4965796c8dcSSimon Schubert    member is non-zero, do the multi-threaded thing: look for a section
4975796c8dcSSimon Schubert    named "NAME/LWP", where LWP is the shortest ASCII decimal
4985796c8dcSSimon Schubert    representation of inferior_ptid's lwp member.
4995796c8dcSSimon Schubert 
5005796c8dcSSimon Schubert    HUMAN_NAME is a human-readable name for the kind of registers the
5015796c8dcSSimon Schubert    NAME section contains, for use in error messages.
5025796c8dcSSimon Schubert 
5035796c8dcSSimon Schubert    If REQUIRED is non-zero, print an error if the core file doesn't
504*c50c785cSJohn Marino    have a section by the appropriate name.  Otherwise, just do
505*c50c785cSJohn Marino    nothing.  */
5065796c8dcSSimon Schubert 
5075796c8dcSSimon Schubert static void
5085796c8dcSSimon Schubert get_core_register_section (struct regcache *regcache,
509cf7f2e2dSJohn Marino 			   const char *name,
5105796c8dcSSimon Schubert 			   int which,
511cf7f2e2dSJohn Marino 			   const char *human_name,
5125796c8dcSSimon Schubert 			   int required)
5135796c8dcSSimon Schubert {
5145796c8dcSSimon Schubert   static char *section_name = NULL;
5155796c8dcSSimon Schubert   struct bfd_section *section;
5165796c8dcSSimon Schubert   bfd_size_type size;
5175796c8dcSSimon Schubert   char *contents;
5185796c8dcSSimon Schubert 
5195796c8dcSSimon Schubert   xfree (section_name);
5205796c8dcSSimon Schubert 
521*c50c785cSJohn Marino   if (ptid_get_lwp (inferior_ptid))
522*c50c785cSJohn Marino     section_name = xstrprintf ("%s/%ld", name,
523*c50c785cSJohn Marino 			       ptid_get_lwp (inferior_ptid));
5245796c8dcSSimon Schubert   else
5255796c8dcSSimon Schubert     section_name = xstrdup (name);
5265796c8dcSSimon Schubert 
5275796c8dcSSimon Schubert   section = bfd_get_section_by_name (core_bfd, section_name);
5285796c8dcSSimon Schubert   if (! section)
5295796c8dcSSimon Schubert     {
5305796c8dcSSimon Schubert       if (required)
531*c50c785cSJohn Marino 	warning (_("Couldn't find %s registers in core file."),
532*c50c785cSJohn Marino 		 human_name);
5335796c8dcSSimon Schubert       return;
5345796c8dcSSimon Schubert     }
5355796c8dcSSimon Schubert 
5365796c8dcSSimon Schubert   size = bfd_section_size (core_bfd, section);
5375796c8dcSSimon Schubert   contents = alloca (size);
5385796c8dcSSimon Schubert   if (! bfd_get_section_contents (core_bfd, section, contents,
5395796c8dcSSimon Schubert 				  (file_ptr) 0, size))
5405796c8dcSSimon Schubert     {
5415796c8dcSSimon Schubert       warning (_("Couldn't read %s registers from `%s' section in core file."),
5425796c8dcSSimon Schubert 	       human_name, name);
5435796c8dcSSimon Schubert       return;
5445796c8dcSSimon Schubert     }
5455796c8dcSSimon Schubert 
5465796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
5475796c8dcSSimon Schubert     {
5485796c8dcSSimon Schubert       const struct regset *regset;
5495796c8dcSSimon Schubert 
550*c50c785cSJohn Marino       regset = gdbarch_regset_from_core_section (core_gdbarch,
551*c50c785cSJohn Marino 						 name, size);
5525796c8dcSSimon Schubert       if (regset == NULL)
5535796c8dcSSimon Schubert 	{
5545796c8dcSSimon Schubert 	  if (required)
5555796c8dcSSimon Schubert 	    warning (_("Couldn't recognize %s registers in core file."),
5565796c8dcSSimon Schubert 		     human_name);
5575796c8dcSSimon Schubert 	  return;
5585796c8dcSSimon Schubert 	}
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert       regset->supply_regset (regset, regcache, -1, contents, size);
5615796c8dcSSimon Schubert       return;
5625796c8dcSSimon Schubert     }
5635796c8dcSSimon Schubert 
5645796c8dcSSimon Schubert   gdb_assert (core_vec);
5655796c8dcSSimon Schubert   core_vec->core_read_registers (regcache, contents, size, which,
5665796c8dcSSimon Schubert 				 ((CORE_ADDR)
5675796c8dcSSimon Schubert 				  bfd_section_vma (core_bfd, section)));
5685796c8dcSSimon Schubert }
5695796c8dcSSimon Schubert 
5705796c8dcSSimon Schubert 
5715796c8dcSSimon Schubert /* Get the registers out of a core file.  This is the machine-
5725796c8dcSSimon Schubert    independent part.  Fetch_core_registers is the machine-dependent
573*c50c785cSJohn Marino    part, typically implemented in the xm-file for each
574*c50c785cSJohn Marino    architecture.  */
5755796c8dcSSimon Schubert 
5765796c8dcSSimon Schubert /* We just get all the registers, so we don't use regno.  */
5775796c8dcSSimon Schubert 
5785796c8dcSSimon Schubert static void
5795796c8dcSSimon Schubert get_core_registers (struct target_ops *ops,
5805796c8dcSSimon Schubert 		    struct regcache *regcache, int regno)
5815796c8dcSSimon Schubert {
582cf7f2e2dSJohn Marino   struct core_regset_section *sect_list;
5835796c8dcSSimon Schubert   int i;
5845796c8dcSSimon Schubert 
5855796c8dcSSimon Schubert   if (!(core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
5865796c8dcSSimon Schubert       && (core_vec == NULL || core_vec->core_read_registers == NULL))
5875796c8dcSSimon Schubert     {
5885796c8dcSSimon Schubert       fprintf_filtered (gdb_stderr,
5895796c8dcSSimon Schubert 		     "Can't fetch registers from this type of core file\n");
5905796c8dcSSimon Schubert       return;
5915796c8dcSSimon Schubert     }
5925796c8dcSSimon Schubert 
593cf7f2e2dSJohn Marino   sect_list = gdbarch_core_regset_sections (get_regcache_arch (regcache));
594cf7f2e2dSJohn Marino   if (sect_list)
595cf7f2e2dSJohn Marino     while (sect_list->sect_name != NULL)
596cf7f2e2dSJohn Marino       {
597cf7f2e2dSJohn Marino         if (strcmp (sect_list->sect_name, ".reg") == 0)
598cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
599cf7f2e2dSJohn Marino 				     0, sect_list->human_name, 1);
600cf7f2e2dSJohn Marino         else if (strcmp (sect_list->sect_name, ".reg2") == 0)
601cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
602cf7f2e2dSJohn Marino 				     2, sect_list->human_name, 0);
603cf7f2e2dSJohn Marino 	else
604cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
605cf7f2e2dSJohn Marino 				     3, sect_list->human_name, 0);
606cf7f2e2dSJohn Marino 
607cf7f2e2dSJohn Marino 	sect_list++;
608cf7f2e2dSJohn Marino       }
609cf7f2e2dSJohn Marino 
610cf7f2e2dSJohn Marino   else
611cf7f2e2dSJohn Marino     {
6125796c8dcSSimon Schubert       get_core_register_section (regcache,
6135796c8dcSSimon Schubert 				 ".reg", 0, "general-purpose", 1);
6145796c8dcSSimon Schubert       get_core_register_section (regcache,
6155796c8dcSSimon Schubert 				 ".reg2", 2, "floating-point", 0);
616cf7f2e2dSJohn Marino     }
6175796c8dcSSimon Schubert 
618*c50c785cSJohn Marino   /* Mark all registers not found in the core as unavailable.  */
6195796c8dcSSimon Schubert   for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
620*c50c785cSJohn Marino     if (regcache_register_status (regcache, i) == REG_UNKNOWN)
6215796c8dcSSimon Schubert       regcache_raw_supply (regcache, i, NULL);
6225796c8dcSSimon Schubert }
6235796c8dcSSimon Schubert 
6245796c8dcSSimon Schubert static void
6255796c8dcSSimon Schubert core_files_info (struct target_ops *t)
6265796c8dcSSimon Schubert {
6275796c8dcSSimon Schubert   print_section_info (core_data, core_bfd);
6285796c8dcSSimon Schubert }
6295796c8dcSSimon Schubert 
6305796c8dcSSimon Schubert struct spuid_list
6315796c8dcSSimon Schubert {
6325796c8dcSSimon Schubert   gdb_byte *buf;
6335796c8dcSSimon Schubert   ULONGEST offset;
6345796c8dcSSimon Schubert   LONGEST len;
6355796c8dcSSimon Schubert   ULONGEST pos;
6365796c8dcSSimon Schubert   ULONGEST written;
6375796c8dcSSimon Schubert };
6385796c8dcSSimon Schubert 
6395796c8dcSSimon Schubert static void
6405796c8dcSSimon Schubert add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
6415796c8dcSSimon Schubert {
6425796c8dcSSimon Schubert   struct spuid_list *list = list_p;
6435796c8dcSSimon Schubert   enum bfd_endian byte_order
6445796c8dcSSimon Schubert     = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
6455796c8dcSSimon Schubert   int fd, pos = 0;
6465796c8dcSSimon Schubert 
6475796c8dcSSimon Schubert   sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
6485796c8dcSSimon Schubert   if (pos == 0)
6495796c8dcSSimon Schubert     return;
6505796c8dcSSimon Schubert 
6515796c8dcSSimon Schubert   if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
6525796c8dcSSimon Schubert     {
6535796c8dcSSimon Schubert       store_unsigned_integer (list->buf + list->pos - list->offset,
6545796c8dcSSimon Schubert 			      4, byte_order, fd);
6555796c8dcSSimon Schubert       list->written += 4;
6565796c8dcSSimon Schubert     }
6575796c8dcSSimon Schubert   list->pos += 4;
6585796c8dcSSimon Schubert }
6595796c8dcSSimon Schubert 
6605796c8dcSSimon Schubert static LONGEST
6615796c8dcSSimon Schubert core_xfer_partial (struct target_ops *ops, enum target_object object,
6625796c8dcSSimon Schubert 		   const char *annex, gdb_byte *readbuf,
663*c50c785cSJohn Marino 		   const gdb_byte *writebuf, ULONGEST offset,
664*c50c785cSJohn Marino 		   LONGEST len)
6655796c8dcSSimon Schubert {
6665796c8dcSSimon Schubert   switch (object)
6675796c8dcSSimon Schubert     {
6685796c8dcSSimon Schubert     case TARGET_OBJECT_MEMORY:
6695796c8dcSSimon Schubert       return section_table_xfer_memory_partial (readbuf, writebuf,
6705796c8dcSSimon Schubert 						offset, len,
6715796c8dcSSimon Schubert 						core_data->sections,
6725796c8dcSSimon Schubert 						core_data->sections_end,
6735796c8dcSSimon Schubert 						NULL);
6745796c8dcSSimon Schubert 
6755796c8dcSSimon Schubert     case TARGET_OBJECT_AUXV:
6765796c8dcSSimon Schubert       if (readbuf)
6775796c8dcSSimon Schubert 	{
6785796c8dcSSimon Schubert 	  /* When the aux vector is stored in core file, BFD
6795796c8dcSSimon Schubert 	     represents this with a fake section called ".auxv".  */
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert 	  struct bfd_section *section;
6825796c8dcSSimon Schubert 	  bfd_size_type size;
6835796c8dcSSimon Schubert 
6845796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, ".auxv");
6855796c8dcSSimon Schubert 	  if (section == NULL)
6865796c8dcSSimon Schubert 	    return -1;
6875796c8dcSSimon Schubert 
6885796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
6895796c8dcSSimon Schubert 	  if (offset >= size)
6905796c8dcSSimon Schubert 	    return 0;
6915796c8dcSSimon Schubert 	  size -= offset;
6925796c8dcSSimon Schubert 	  if (size > len)
6935796c8dcSSimon Schubert 	    size = len;
6945796c8dcSSimon Schubert 	  if (size > 0
6955796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
6965796c8dcSSimon Schubert 					    (file_ptr) offset, size))
6975796c8dcSSimon Schubert 	    {
6985796c8dcSSimon Schubert 	      warning (_("Couldn't read NT_AUXV note in core file."));
6995796c8dcSSimon Schubert 	      return -1;
7005796c8dcSSimon Schubert 	    }
7015796c8dcSSimon Schubert 
7025796c8dcSSimon Schubert 	  return size;
7035796c8dcSSimon Schubert 	}
7045796c8dcSSimon Schubert       return -1;
7055796c8dcSSimon Schubert 
7065796c8dcSSimon Schubert     case TARGET_OBJECT_WCOOKIE:
7075796c8dcSSimon Schubert       if (readbuf)
7085796c8dcSSimon Schubert 	{
7095796c8dcSSimon Schubert 	  /* When the StackGhost cookie is stored in core file, BFD
710*c50c785cSJohn Marino 	     represents this with a fake section called
711*c50c785cSJohn Marino 	     ".wcookie".  */
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert 	  struct bfd_section *section;
7145796c8dcSSimon Schubert 	  bfd_size_type size;
7155796c8dcSSimon Schubert 
7165796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, ".wcookie");
7175796c8dcSSimon Schubert 	  if (section == NULL)
7185796c8dcSSimon Schubert 	    return -1;
7195796c8dcSSimon Schubert 
7205796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
7215796c8dcSSimon Schubert 	  if (offset >= size)
7225796c8dcSSimon Schubert 	    return 0;
7235796c8dcSSimon Schubert 	  size -= offset;
7245796c8dcSSimon Schubert 	  if (size > len)
7255796c8dcSSimon Schubert 	    size = len;
7265796c8dcSSimon Schubert 	  if (size > 0
7275796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
7285796c8dcSSimon Schubert 					    (file_ptr) offset, size))
7295796c8dcSSimon Schubert 	    {
7305796c8dcSSimon Schubert 	      warning (_("Couldn't read StackGhost cookie in core file."));
7315796c8dcSSimon Schubert 	      return -1;
7325796c8dcSSimon Schubert 	    }
7335796c8dcSSimon Schubert 
7345796c8dcSSimon Schubert 	  return size;
7355796c8dcSSimon Schubert 	}
7365796c8dcSSimon Schubert       return -1;
7375796c8dcSSimon Schubert 
7385796c8dcSSimon Schubert     case TARGET_OBJECT_LIBRARIES:
7395796c8dcSSimon Schubert       if (core_gdbarch
7405796c8dcSSimon Schubert 	  && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
7415796c8dcSSimon Schubert 	{
7425796c8dcSSimon Schubert 	  if (writebuf)
7435796c8dcSSimon Schubert 	    return -1;
7445796c8dcSSimon Schubert 	  return
7455796c8dcSSimon Schubert 	    gdbarch_core_xfer_shared_libraries (core_gdbarch,
7465796c8dcSSimon Schubert 						readbuf, offset, len);
7475796c8dcSSimon Schubert 	}
7485796c8dcSSimon Schubert       /* FALL THROUGH */
7495796c8dcSSimon Schubert 
7505796c8dcSSimon Schubert     case TARGET_OBJECT_SPU:
7515796c8dcSSimon Schubert       if (readbuf && annex)
7525796c8dcSSimon Schubert 	{
7535796c8dcSSimon Schubert 	  /* When the SPU contexts are stored in a core file, BFD
754*c50c785cSJohn Marino 	     represents this with a fake section called
755*c50c785cSJohn Marino 	     "SPU/<annex>".  */
7565796c8dcSSimon Schubert 
7575796c8dcSSimon Schubert 	  struct bfd_section *section;
7585796c8dcSSimon Schubert 	  bfd_size_type size;
7595796c8dcSSimon Schubert 	  char sectionstr[100];
760cf7f2e2dSJohn Marino 
7615796c8dcSSimon Schubert 	  xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
7625796c8dcSSimon Schubert 
7635796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, sectionstr);
7645796c8dcSSimon Schubert 	  if (section == NULL)
7655796c8dcSSimon Schubert 	    return -1;
7665796c8dcSSimon Schubert 
7675796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
7685796c8dcSSimon Schubert 	  if (offset >= size)
7695796c8dcSSimon Schubert 	    return 0;
7705796c8dcSSimon Schubert 	  size -= offset;
7715796c8dcSSimon Schubert 	  if (size > len)
7725796c8dcSSimon Schubert 	    size = len;
7735796c8dcSSimon Schubert 	  if (size > 0
7745796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
7755796c8dcSSimon Schubert 					    (file_ptr) offset, size))
7765796c8dcSSimon Schubert 	    {
7775796c8dcSSimon Schubert 	      warning (_("Couldn't read SPU section in core file."));
7785796c8dcSSimon Schubert 	      return -1;
7795796c8dcSSimon Schubert 	    }
7805796c8dcSSimon Schubert 
7815796c8dcSSimon Schubert 	  return size;
7825796c8dcSSimon Schubert 	}
7835796c8dcSSimon Schubert       else if (readbuf)
7845796c8dcSSimon Schubert 	{
7855796c8dcSSimon Schubert 	  /* NULL annex requests list of all present spuids.  */
7865796c8dcSSimon Schubert 	  struct spuid_list list;
787cf7f2e2dSJohn Marino 
7885796c8dcSSimon Schubert 	  list.buf = readbuf;
7895796c8dcSSimon Schubert 	  list.offset = offset;
7905796c8dcSSimon Schubert 	  list.len = len;
7915796c8dcSSimon Schubert 	  list.pos = 0;
7925796c8dcSSimon Schubert 	  list.written = 0;
7935796c8dcSSimon Schubert 	  bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
7945796c8dcSSimon Schubert 	  return list.written;
7955796c8dcSSimon Schubert 	}
7965796c8dcSSimon Schubert       return -1;
7975796c8dcSSimon Schubert 
7985796c8dcSSimon Schubert     default:
7995796c8dcSSimon Schubert       if (ops->beneath != NULL)
800*c50c785cSJohn Marino 	return ops->beneath->to_xfer_partial (ops->beneath, object,
801*c50c785cSJohn Marino 					      annex, readbuf,
802*c50c785cSJohn Marino 					      writebuf, offset, len);
8035796c8dcSSimon Schubert       return -1;
8045796c8dcSSimon Schubert     }
8055796c8dcSSimon Schubert }
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert 
8085796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say
809*c50c785cSJohn Marino    `gdb internal error' (since generic_mourn calls
810*c50c785cSJohn Marino    breakpoint_init_inferior).  */
8115796c8dcSSimon Schubert 
8125796c8dcSSimon Schubert static int
8135796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
8145796c8dcSSimon Schubert {
8155796c8dcSSimon Schubert   return 0;
8165796c8dcSSimon Schubert }
8175796c8dcSSimon Schubert 
8185796c8dcSSimon Schubert 
8195796c8dcSSimon Schubert /* Okay, let's be honest: threads gleaned from a core file aren't
8205796c8dcSSimon Schubert    exactly lively, are they?  On the other hand, if we don't claim
8215796c8dcSSimon Schubert    that each & every one is alive, then we don't get any of them
8225796c8dcSSimon Schubert    to appear in an "info thread" command, which is quite a useful
8235796c8dcSSimon Schubert    behaviour.
8245796c8dcSSimon Schubert  */
8255796c8dcSSimon Schubert static int
8265796c8dcSSimon Schubert core_thread_alive (struct target_ops *ops, ptid_t ptid)
8275796c8dcSSimon Schubert {
8285796c8dcSSimon Schubert   return 1;
8295796c8dcSSimon Schubert }
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert /* Ask the current architecture what it knows about this core file.
8325796c8dcSSimon Schubert    That will be used, in turn, to pick a better architecture.  This
8335796c8dcSSimon Schubert    wrapper could be avoided if targets got a chance to specialize
8345796c8dcSSimon Schubert    core_ops.  */
8355796c8dcSSimon Schubert 
8365796c8dcSSimon Schubert static const struct target_desc *
8375796c8dcSSimon Schubert core_read_description (struct target_ops *target)
8385796c8dcSSimon Schubert {
8395796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
840*c50c785cSJohn Marino     return gdbarch_core_read_description (core_gdbarch,
841*c50c785cSJohn Marino 					  target, core_bfd);
8425796c8dcSSimon Schubert 
8435796c8dcSSimon Schubert   return NULL;
8445796c8dcSSimon Schubert }
8455796c8dcSSimon Schubert 
8465796c8dcSSimon Schubert static char *
8475796c8dcSSimon Schubert core_pid_to_str (struct target_ops *ops, ptid_t ptid)
8485796c8dcSSimon Schubert {
8495796c8dcSSimon Schubert   static char buf[64];
850*c50c785cSJohn Marino   int pid;
8515796c8dcSSimon Schubert 
852*c50c785cSJohn Marino   /* The preferred way is to have a gdbarch/OS specific
853*c50c785cSJohn Marino      implementation.  */
8545796c8dcSSimon Schubert   if (core_gdbarch
8555796c8dcSSimon Schubert       && gdbarch_core_pid_to_str_p (core_gdbarch))
856*c50c785cSJohn Marino     return gdbarch_core_pid_to_str (core_gdbarch, ptid);
857cf7f2e2dSJohn Marino 
858*c50c785cSJohn Marino   /* Otherwise, if we don't have one, we'll just fallback to
859*c50c785cSJohn Marino      "process", with normal_pid_to_str.  */
8605796c8dcSSimon Schubert 
861*c50c785cSJohn Marino   /* Try the LWPID field first.  */
862*c50c785cSJohn Marino   pid = ptid_get_lwp (ptid);
863*c50c785cSJohn Marino   if (pid != 0)
864*c50c785cSJohn Marino     return normal_pid_to_str (pid_to_ptid (pid));
865*c50c785cSJohn Marino 
866*c50c785cSJohn Marino   /* Otherwise, this isn't a "threaded" core -- use the PID field, but
867*c50c785cSJohn Marino      only if it isn't a fake PID.  */
868*c50c785cSJohn Marino   if (!core_has_fake_pid)
869*c50c785cSJohn Marino     return normal_pid_to_str (ptid);
870*c50c785cSJohn Marino 
871*c50c785cSJohn Marino   /* No luck.  We simply don't have a valid PID to print.  */
8725796c8dcSSimon Schubert   xsnprintf (buf, sizeof buf, "<main task>");
8735796c8dcSSimon Schubert   return buf;
8745796c8dcSSimon Schubert }
8755796c8dcSSimon Schubert 
8765796c8dcSSimon Schubert static int
8775796c8dcSSimon Schubert core_has_memory (struct target_ops *ops)
8785796c8dcSSimon Schubert {
8795796c8dcSSimon Schubert   return (core_bfd != NULL);
8805796c8dcSSimon Schubert }
8815796c8dcSSimon Schubert 
8825796c8dcSSimon Schubert static int
8835796c8dcSSimon Schubert core_has_stack (struct target_ops *ops)
8845796c8dcSSimon Schubert {
8855796c8dcSSimon Schubert   return (core_bfd != NULL);
8865796c8dcSSimon Schubert }
8875796c8dcSSimon Schubert 
8885796c8dcSSimon Schubert static int
8895796c8dcSSimon Schubert core_has_registers (struct target_ops *ops)
8905796c8dcSSimon Schubert {
8915796c8dcSSimon Schubert   return (core_bfd != NULL);
8925796c8dcSSimon Schubert }
8935796c8dcSSimon Schubert 
8945796c8dcSSimon Schubert /* Fill in core_ops with its defined operations and properties.  */
8955796c8dcSSimon Schubert 
8965796c8dcSSimon Schubert static void
8975796c8dcSSimon Schubert init_core_ops (void)
8985796c8dcSSimon Schubert {
8995796c8dcSSimon Schubert   core_ops.to_shortname = "core";
9005796c8dcSSimon Schubert   core_ops.to_longname = "Local core dump file";
9015796c8dcSSimon Schubert   core_ops.to_doc =
9025796c8dcSSimon Schubert     "Use a core file as a target.  Specify the filename of the core file.";
9035796c8dcSSimon Schubert   core_ops.to_open = core_open;
9045796c8dcSSimon Schubert   core_ops.to_close = core_close;
9055796c8dcSSimon Schubert   core_ops.to_attach = find_default_attach;
9065796c8dcSSimon Schubert   core_ops.to_detach = core_detach;
9075796c8dcSSimon Schubert   core_ops.to_fetch_registers = get_core_registers;
9085796c8dcSSimon Schubert   core_ops.to_xfer_partial = core_xfer_partial;
9095796c8dcSSimon Schubert   core_ops.to_files_info = core_files_info;
9105796c8dcSSimon Schubert   core_ops.to_insert_breakpoint = ignore;
9115796c8dcSSimon Schubert   core_ops.to_remove_breakpoint = ignore;
9125796c8dcSSimon Schubert   core_ops.to_create_inferior = find_default_create_inferior;
9135796c8dcSSimon Schubert   core_ops.to_thread_alive = core_thread_alive;
9145796c8dcSSimon Schubert   core_ops.to_read_description = core_read_description;
9155796c8dcSSimon Schubert   core_ops.to_pid_to_str = core_pid_to_str;
916*c50c785cSJohn Marino   core_ops.to_stratum = process_stratum;
9175796c8dcSSimon Schubert   core_ops.to_has_memory = core_has_memory;
9185796c8dcSSimon Schubert   core_ops.to_has_stack = core_has_stack;
9195796c8dcSSimon Schubert   core_ops.to_has_registers = core_has_registers;
9205796c8dcSSimon Schubert   core_ops.to_magic = OPS_MAGIC;
921*c50c785cSJohn Marino 
922*c50c785cSJohn Marino   if (core_target)
923*c50c785cSJohn Marino     internal_error (__FILE__, __LINE__,
924*c50c785cSJohn Marino 		    _("init_core_ops: core target already exists (\"%s\")."),
925*c50c785cSJohn Marino 		    core_target->to_longname);
926*c50c785cSJohn Marino   core_target = &core_ops;
9275796c8dcSSimon Schubert }
9285796c8dcSSimon Schubert 
9295796c8dcSSimon Schubert void
9305796c8dcSSimon Schubert _initialize_corelow (void)
9315796c8dcSSimon Schubert {
9325796c8dcSSimon Schubert   init_core_ops ();
9335796c8dcSSimon Schubert 
9345796c8dcSSimon Schubert   add_target (&core_ops);
9355796c8dcSSimon Schubert }
936