xref: /dflybsd-src/contrib/gdb-7/gdb/corelow.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Core dump and executable file functions below target vector, for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "arch-utils.h"
225796c8dcSSimon Schubert #include "gdb_string.h"
235796c8dcSSimon Schubert #include <errno.h>
245796c8dcSSimon Schubert #include <signal.h>
255796c8dcSSimon Schubert #include <fcntl.h>
265796c8dcSSimon Schubert #ifdef HAVE_SYS_FILE_H
275796c8dcSSimon Schubert #include <sys/file.h>		/* needed for F_OK and friends */
285796c8dcSSimon Schubert #endif
295796c8dcSSimon Schubert #include "frame.h"		/* required by inferior.h */
305796c8dcSSimon Schubert #include "inferior.h"
315796c8dcSSimon Schubert #include "symtab.h"
325796c8dcSSimon Schubert #include "command.h"
335796c8dcSSimon Schubert #include "bfd.h"
345796c8dcSSimon Schubert #include "target.h"
355796c8dcSSimon Schubert #include "gdbcore.h"
365796c8dcSSimon Schubert #include "gdbthread.h"
375796c8dcSSimon Schubert #include "regcache.h"
385796c8dcSSimon Schubert #include "regset.h"
395796c8dcSSimon Schubert #include "symfile.h"
405796c8dcSSimon Schubert #include "exec.h"
415796c8dcSSimon Schubert #include "readline/readline.h"
425796c8dcSSimon Schubert #include "gdb_assert.h"
435796c8dcSSimon Schubert #include "exceptions.h"
445796c8dcSSimon Schubert #include "solib.h"
455796c8dcSSimon Schubert #include "filenames.h"
46cf7f2e2dSJohn Marino #include "progspace.h"
47cf7f2e2dSJohn Marino #include "objfiles.h"
48*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert #ifndef O_LARGEFILE
515796c8dcSSimon Schubert #define O_LARGEFILE 0
525796c8dcSSimon Schubert #endif
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert /* List of all available core_fns.  On gdb startup, each core file
555796c8dcSSimon Schubert    register reader calls deprecated_add_core_fns() to register
565796c8dcSSimon Schubert    information on each core format it is prepared to read.  */
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert static struct core_fns *core_file_fns = NULL;
595796c8dcSSimon Schubert 
60c50c785cSJohn Marino /* The core_fns for a core file handler that is prepared to read the
61c50c785cSJohn Marino    core file currently open on core_bfd.  */
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert static struct core_fns *core_vec = NULL;
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert /* FIXME: kettenis/20031023: Eventually this variable should
665796c8dcSSimon Schubert    disappear.  */
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert struct gdbarch *core_gdbarch = NULL;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert /* Per-core data.  Currently, only the section table.  Note that these
715796c8dcSSimon Schubert    target sections are *not* mapped in the current address spaces' set
725796c8dcSSimon Schubert    of target sections --- those should come only from pure executable
735796c8dcSSimon Schubert    or shared library bfds.  The core bfd sections are an
745796c8dcSSimon Schubert    implementation detail of the core target, just like ptrace is for
755796c8dcSSimon Schubert    unix child targets.  */
765796c8dcSSimon Schubert static struct target_section_table *core_data;
775796c8dcSSimon Schubert 
785796c8dcSSimon Schubert static void core_files_info (struct target_ops *);
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert static struct core_fns *sniff_core_bfd (bfd *);
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert static int gdb_check_format (bfd *);
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert static void core_open (char *, int);
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert static void core_detach (struct target_ops *ops, char *, int);
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert static void core_close (int);
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert static void core_close_cleanup (void *ignore);
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert static void add_to_thread_list (bfd *, asection *, void *);
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert static void init_core_ops (void);
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert void _initialize_corelow (void);
975796c8dcSSimon Schubert 
98c50c785cSJohn Marino static struct target_ops core_ops;
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert /* An arbitrary identifier for the core inferior.  */
1015796c8dcSSimon Schubert #define CORELOW_PID 1
1025796c8dcSSimon Schubert 
103c50c785cSJohn Marino /* Link a new core_fns into the global core_file_fns list.  Called on
104c50c785cSJohn Marino    gdb startup by the _initialize routine in each core file register
105c50c785cSJohn Marino    reader, to register information about each format the reader is
106c50c785cSJohn Marino    prepared to handle.  */
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert void
deprecated_add_core_fns(struct core_fns * cf)1095796c8dcSSimon Schubert deprecated_add_core_fns (struct core_fns *cf)
1105796c8dcSSimon Schubert {
1115796c8dcSSimon Schubert   cf->next = core_file_fns;
1125796c8dcSSimon Schubert   core_file_fns = cf;
1135796c8dcSSimon Schubert }
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert /* The default function that core file handlers can use to examine a
1165796c8dcSSimon Schubert    core file BFD and decide whether or not to accept the job of
1175796c8dcSSimon Schubert    reading the core file.  */
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert int
default_core_sniffer(struct core_fns * our_fns,bfd * abfd)1205796c8dcSSimon Schubert default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
1215796c8dcSSimon Schubert {
1225796c8dcSSimon Schubert   int result;
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert   result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
1255796c8dcSSimon Schubert   return (result);
1265796c8dcSSimon Schubert }
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert /* Walk through the list of core functions to find a set that can
129*ef5ccd6cSJohn Marino    handle the core file open on ABFD.  Returns pointer to set that is
1305796c8dcSSimon Schubert    selected.  */
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert static struct core_fns *
sniff_core_bfd(bfd * abfd)1335796c8dcSSimon Schubert sniff_core_bfd (bfd *abfd)
1345796c8dcSSimon Schubert {
1355796c8dcSSimon Schubert   struct core_fns *cf;
1365796c8dcSSimon Schubert   struct core_fns *yummy = NULL;
1375796c8dcSSimon Schubert   int matches = 0;;
1385796c8dcSSimon Schubert 
139c50c785cSJohn Marino   /* Don't sniff if we have support for register sets in
140c50c785cSJohn Marino      CORE_GDBARCH.  */
1415796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
1425796c8dcSSimon Schubert     return NULL;
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert   for (cf = core_file_fns; cf != NULL; cf = cf->next)
1455796c8dcSSimon Schubert     {
1465796c8dcSSimon Schubert       if (cf->core_sniffer (cf, abfd))
1475796c8dcSSimon Schubert 	{
1485796c8dcSSimon Schubert 	  yummy = cf;
1495796c8dcSSimon Schubert 	  matches++;
1505796c8dcSSimon Schubert 	}
1515796c8dcSSimon Schubert     }
1525796c8dcSSimon Schubert   if (matches > 1)
1535796c8dcSSimon Schubert     {
1545796c8dcSSimon Schubert       warning (_("\"%s\": ambiguous core format, %d handlers match"),
1555796c8dcSSimon Schubert 	       bfd_get_filename (abfd), matches);
1565796c8dcSSimon Schubert     }
1575796c8dcSSimon Schubert   else if (matches == 0)
158*ef5ccd6cSJohn Marino     error (_("\"%s\": no core file handler recognizes format"),
1595796c8dcSSimon Schubert 	   bfd_get_filename (abfd));
160*ef5ccd6cSJohn Marino 
1615796c8dcSSimon Schubert   return (yummy);
1625796c8dcSSimon Schubert }
1635796c8dcSSimon Schubert 
1645796c8dcSSimon Schubert /* The default is to reject every core file format we see.  Either
1655796c8dcSSimon Schubert    BFD has to recognize it, or we have to provide a function in the
1665796c8dcSSimon Schubert    core file handler that recognizes it.  */
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert int
default_check_format(bfd * abfd)1695796c8dcSSimon Schubert default_check_format (bfd *abfd)
1705796c8dcSSimon Schubert {
1715796c8dcSSimon Schubert   return (0);
1725796c8dcSSimon Schubert }
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert /* Attempt to recognize core file formats that BFD rejects.  */
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert static int
gdb_check_format(bfd * abfd)1775796c8dcSSimon Schubert gdb_check_format (bfd *abfd)
1785796c8dcSSimon Schubert {
1795796c8dcSSimon Schubert   struct core_fns *cf;
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert   for (cf = core_file_fns; cf != NULL; cf = cf->next)
1825796c8dcSSimon Schubert     {
1835796c8dcSSimon Schubert       if (cf->check_format (abfd))
1845796c8dcSSimon Schubert 	{
1855796c8dcSSimon Schubert 	  return (1);
1865796c8dcSSimon Schubert 	}
1875796c8dcSSimon Schubert     }
1885796c8dcSSimon Schubert   return (0);
1895796c8dcSSimon Schubert }
1905796c8dcSSimon Schubert 
191c50c785cSJohn Marino /* Discard all vestiges of any previous core file and mark data and
192c50c785cSJohn Marino    stack spaces as empty.  */
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert static void
core_close(int quitting)1955796c8dcSSimon Schubert core_close (int quitting)
1965796c8dcSSimon Schubert {
1975796c8dcSSimon Schubert   if (core_bfd)
1985796c8dcSSimon Schubert     {
1995796c8dcSSimon Schubert       int pid = ptid_get_pid (inferior_ptid);
200c50c785cSJohn Marino       inferior_ptid = null_ptid;    /* Avoid confusion from thread
201c50c785cSJohn Marino 				       stuff.  */
202*ef5ccd6cSJohn Marino       if (pid != 0)
203cf7f2e2dSJohn Marino 	exit_inferior_silent (pid);
2045796c8dcSSimon Schubert 
2055796c8dcSSimon Schubert       /* Clear out solib state while the bfd is still open.  See
2065796c8dcSSimon Schubert          comments in clear_solib in solib.c.  */
2075796c8dcSSimon Schubert       clear_solib ();
2085796c8dcSSimon Schubert 
209*ef5ccd6cSJohn Marino       if (core_data)
210*ef5ccd6cSJohn Marino 	{
2115796c8dcSSimon Schubert 	  xfree (core_data->sections);
2125796c8dcSSimon Schubert 	  xfree (core_data);
2135796c8dcSSimon Schubert 	  core_data = NULL;
214*ef5ccd6cSJohn Marino 	}
2155796c8dcSSimon Schubert 
216*ef5ccd6cSJohn Marino       gdb_bfd_unref (core_bfd);
2175796c8dcSSimon Schubert       core_bfd = NULL;
2185796c8dcSSimon Schubert     }
2195796c8dcSSimon Schubert   core_vec = NULL;
2205796c8dcSSimon Schubert   core_gdbarch = NULL;
2215796c8dcSSimon Schubert }
2225796c8dcSSimon Schubert 
2235796c8dcSSimon Schubert static void
core_close_cleanup(void * ignore)2245796c8dcSSimon Schubert core_close_cleanup (void *ignore)
2255796c8dcSSimon Schubert {
2265796c8dcSSimon Schubert   core_close (0/*ignored*/);
2275796c8dcSSimon Schubert }
2285796c8dcSSimon Schubert 
229c50c785cSJohn Marino /* Look for sections whose names start with `.reg/' so that we can
230c50c785cSJohn Marino    extract the list of threads in a core file.  */
2315796c8dcSSimon Schubert 
2325796c8dcSSimon Schubert static void
add_to_thread_list(bfd * abfd,asection * asect,void * reg_sect_arg)2335796c8dcSSimon Schubert add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
2345796c8dcSSimon Schubert {
2355796c8dcSSimon Schubert   ptid_t ptid;
236cf7f2e2dSJohn Marino   int core_tid;
237cf7f2e2dSJohn Marino   int pid, lwpid;
2385796c8dcSSimon Schubert   asection *reg_sect = (asection *) reg_sect_arg;
239*ef5ccd6cSJohn Marino   int fake_pid_p = 0;
240*ef5ccd6cSJohn Marino   struct inferior *inf;
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
2435796c8dcSSimon Schubert     return;
2445796c8dcSSimon Schubert 
245cf7f2e2dSJohn Marino   core_tid = atoi (bfd_section_name (abfd, asect) + 5);
2465796c8dcSSimon Schubert 
247c50c785cSJohn Marino   pid = bfd_core_file_pid (core_bfd);
248cf7f2e2dSJohn Marino   if (pid == 0)
249cf7f2e2dSJohn Marino     {
250*ef5ccd6cSJohn Marino       fake_pid_p = 1;
251cf7f2e2dSJohn Marino       pid = CORELOW_PID;
252cf7f2e2dSJohn Marino     }
253c50c785cSJohn Marino 
254cf7f2e2dSJohn Marino   lwpid = core_tid;
2555796c8dcSSimon Schubert 
256*ef5ccd6cSJohn Marino   inf = current_inferior ();
257*ef5ccd6cSJohn Marino   if (inf->pid == 0)
258*ef5ccd6cSJohn Marino     {
259*ef5ccd6cSJohn Marino       inferior_appeared (inf, pid);
260*ef5ccd6cSJohn Marino       inf->fake_pid_p = fake_pid_p;
261*ef5ccd6cSJohn Marino     }
262cf7f2e2dSJohn Marino 
263cf7f2e2dSJohn Marino   ptid = ptid_build (pid, lwpid, 0);
264cf7f2e2dSJohn Marino 
2655796c8dcSSimon Schubert   add_thread (ptid);
2665796c8dcSSimon Schubert 
2675796c8dcSSimon Schubert /* Warning, Will Robinson, looking at BFD private data! */
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert   if (reg_sect != NULL
2705796c8dcSSimon Schubert       && asect->filepos == reg_sect->filepos)	/* Did we find .reg?  */
271c50c785cSJohn Marino     inferior_ptid = ptid;			/* Yes, make it current.  */
2725796c8dcSSimon Schubert }
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert /* This routine opens and sets up the core file bfd.  */
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert static void
core_open(char * filename,int from_tty)2775796c8dcSSimon Schubert core_open (char *filename, int from_tty)
2785796c8dcSSimon Schubert {
2795796c8dcSSimon Schubert   const char *p;
2805796c8dcSSimon Schubert   int siggy;
2815796c8dcSSimon Schubert   struct cleanup *old_chain;
2825796c8dcSSimon Schubert   char *temp;
2835796c8dcSSimon Schubert   bfd *temp_bfd;
2845796c8dcSSimon Schubert   int scratch_chan;
2855796c8dcSSimon Schubert   int flags;
286*ef5ccd6cSJohn Marino   volatile struct gdb_exception except;
2875796c8dcSSimon Schubert 
2885796c8dcSSimon Schubert   target_preopen (from_tty);
2895796c8dcSSimon Schubert   if (!filename)
2905796c8dcSSimon Schubert     {
2915796c8dcSSimon Schubert       if (core_bfd)
292c50c785cSJohn Marino 	error (_("No core file specified.  (Use `detach' "
293c50c785cSJohn Marino 		 "to stop debugging a core file.)"));
2945796c8dcSSimon Schubert       else
2955796c8dcSSimon Schubert 	error (_("No core file specified."));
2965796c8dcSSimon Schubert     }
2975796c8dcSSimon Schubert 
2985796c8dcSSimon Schubert   filename = tilde_expand (filename);
2995796c8dcSSimon Schubert   if (!IS_ABSOLUTE_PATH (filename))
3005796c8dcSSimon Schubert     {
301c50c785cSJohn Marino       temp = concat (current_directory, "/",
302c50c785cSJohn Marino 		     filename, (char *) NULL);
3035796c8dcSSimon Schubert       xfree (filename);
3045796c8dcSSimon Schubert       filename = temp;
3055796c8dcSSimon Schubert     }
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert   old_chain = make_cleanup (xfree, filename);
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert   flags = O_BINARY | O_LARGEFILE;
3105796c8dcSSimon Schubert   if (write_files)
3115796c8dcSSimon Schubert     flags |= O_RDWR;
3125796c8dcSSimon Schubert   else
3135796c8dcSSimon Schubert     flags |= O_RDONLY;
3145796c8dcSSimon Schubert   scratch_chan = open (filename, flags, 0);
3155796c8dcSSimon Schubert   if (scratch_chan < 0)
3165796c8dcSSimon Schubert     perror_with_name (filename);
3175796c8dcSSimon Schubert 
318*ef5ccd6cSJohn Marino   temp_bfd = gdb_bfd_fopen (filename, gnutarget,
3195796c8dcSSimon Schubert 			    write_files ? FOPEN_RUB : FOPEN_RB,
3205796c8dcSSimon Schubert 			    scratch_chan);
3215796c8dcSSimon Schubert   if (temp_bfd == NULL)
3225796c8dcSSimon Schubert     perror_with_name (filename);
3235796c8dcSSimon Schubert 
324cf7f2e2dSJohn Marino   if (!bfd_check_format (temp_bfd, bfd_core)
325cf7f2e2dSJohn Marino       && !gdb_check_format (temp_bfd))
3265796c8dcSSimon Schubert     {
3275796c8dcSSimon Schubert       /* Do it after the err msg */
328c50c785cSJohn Marino       /* FIXME: should be checking for errors from bfd_close (for one
329c50c785cSJohn Marino          thing, on error it does not free all the storage associated
330c50c785cSJohn Marino          with the bfd).  */
331*ef5ccd6cSJohn Marino       make_cleanup_bfd_unref (temp_bfd);
3325796c8dcSSimon Schubert       error (_("\"%s\" is not a core dump: %s"),
3335796c8dcSSimon Schubert 	     filename, bfd_errmsg (bfd_get_error ()));
3345796c8dcSSimon Schubert     }
3355796c8dcSSimon Schubert 
336c50c785cSJohn Marino   /* Looks semi-reasonable.  Toss the old core file and work on the
337c50c785cSJohn Marino      new.  */
3385796c8dcSSimon Schubert 
339*ef5ccd6cSJohn Marino   do_cleanups (old_chain);
3405796c8dcSSimon Schubert   unpush_target (&core_ops);
3415796c8dcSSimon Schubert   core_bfd = temp_bfd;
3425796c8dcSSimon Schubert   old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
3435796c8dcSSimon Schubert 
3445796c8dcSSimon Schubert   core_gdbarch = gdbarch_from_bfd (core_bfd);
3455796c8dcSSimon Schubert 
3465796c8dcSSimon Schubert   /* Find a suitable core file handler to munch on core_bfd */
3475796c8dcSSimon Schubert   core_vec = sniff_core_bfd (core_bfd);
3485796c8dcSSimon Schubert 
3495796c8dcSSimon Schubert   validate_files ();
3505796c8dcSSimon Schubert 
3515796c8dcSSimon Schubert   core_data = XZALLOC (struct target_section_table);
3525796c8dcSSimon Schubert 
3535796c8dcSSimon Schubert   /* Find the data section */
3545796c8dcSSimon Schubert   if (build_section_table (core_bfd,
355c50c785cSJohn Marino 			   &core_data->sections,
356c50c785cSJohn Marino 			   &core_data->sections_end))
3575796c8dcSSimon Schubert     error (_("\"%s\": Can't find sections: %s"),
3585796c8dcSSimon Schubert 	   bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert   /* If we have no exec file, try to set the architecture from the
3615796c8dcSSimon Schubert      core file.  We don't do this unconditionally since an exec file
3625796c8dcSSimon Schubert      typically contains more information that helps us determine the
3635796c8dcSSimon Schubert      architecture than a core file.  */
3645796c8dcSSimon Schubert   if (!exec_bfd)
3655796c8dcSSimon Schubert     set_gdbarch_from_file (core_bfd);
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert   push_target (&core_ops);
3685796c8dcSSimon Schubert   discard_cleanups (old_chain);
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert   /* Do this before acknowledging the inferior, so if
3715796c8dcSSimon Schubert      post_create_inferior throws (can happen easilly if you're loading
3725796c8dcSSimon Schubert      a core file with the wrong exec), we aren't left with threads
3735796c8dcSSimon Schubert      from the previous inferior.  */
3745796c8dcSSimon Schubert   init_thread_list ();
3755796c8dcSSimon Schubert 
376cf7f2e2dSJohn Marino   inferior_ptid = null_ptid;
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert   /* Need to flush the register cache (and the frame cache) from a
3795796c8dcSSimon Schubert      previous debug session.  If inferior_ptid ends up the same as the
3805796c8dcSSimon Schubert      last debug session --- e.g., b foo; run; gcore core1; step; gcore
3815796c8dcSSimon Schubert      core2; core core1; core core2 --- then there's potential for
3825796c8dcSSimon Schubert      get_current_regcache to return the cached regcache of the
3835796c8dcSSimon Schubert      previous session, and the frame cache being stale.  */
3845796c8dcSSimon Schubert   registers_changed ();
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert   /* Build up thread list from BFD sections, and possibly set the
3875796c8dcSSimon Schubert      current thread to the .reg/NN section matching the .reg
3885796c8dcSSimon Schubert      section.  */
3895796c8dcSSimon Schubert   bfd_map_over_sections (core_bfd, add_to_thread_list,
3905796c8dcSSimon Schubert 			 bfd_get_section_by_name (core_bfd, ".reg"));
3915796c8dcSSimon Schubert 
392cf7f2e2dSJohn Marino   if (ptid_equal (inferior_ptid, null_ptid))
393cf7f2e2dSJohn Marino     {
394cf7f2e2dSJohn Marino       /* Either we found no .reg/NN section, and hence we have a
395cf7f2e2dSJohn Marino 	 non-threaded core (single-threaded, from gdb's perspective),
396cf7f2e2dSJohn Marino 	 or for some reason add_to_thread_list couldn't determine
397cf7f2e2dSJohn Marino 	 which was the "main" thread.  The latter case shouldn't
398cf7f2e2dSJohn Marino 	 usually happen, but we're dealing with input here, which can
399cf7f2e2dSJohn Marino 	 always be broken in different ways.  */
400cf7f2e2dSJohn Marino       struct thread_info *thread = first_thread_of_process (-1);
401cf7f2e2dSJohn Marino 
402cf7f2e2dSJohn Marino       if (thread == NULL)
403cf7f2e2dSJohn Marino 	{
404cf7f2e2dSJohn Marino 	  inferior_appeared (current_inferior (), CORELOW_PID);
405cf7f2e2dSJohn Marino 	  inferior_ptid = pid_to_ptid (CORELOW_PID);
406cf7f2e2dSJohn Marino 	  add_thread_silent (inferior_ptid);
407cf7f2e2dSJohn Marino 	}
408cf7f2e2dSJohn Marino       else
409cf7f2e2dSJohn Marino 	switch_to_thread (thread->ptid);
410cf7f2e2dSJohn Marino     }
411cf7f2e2dSJohn Marino 
4125796c8dcSSimon Schubert   post_create_inferior (&core_ops, from_tty);
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert   /* Now go through the target stack looking for threads since there
4155796c8dcSSimon Schubert      may be a thread_stratum target loaded on top of target core by
4165796c8dcSSimon Schubert      now.  The layer above should claim threads found in the BFD
4175796c8dcSSimon Schubert      sections.  */
418*ef5ccd6cSJohn Marino   TRY_CATCH (except, RETURN_MASK_ERROR)
419*ef5ccd6cSJohn Marino     {
420*ef5ccd6cSJohn Marino       target_find_new_threads ();
421*ef5ccd6cSJohn Marino     }
422*ef5ccd6cSJohn Marino 
423*ef5ccd6cSJohn Marino   if (except.reason < 0)
424*ef5ccd6cSJohn Marino     exception_print (gdb_stderr, except);
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert   p = bfd_core_file_failing_command (core_bfd);
4275796c8dcSSimon Schubert   if (p)
4285796c8dcSSimon Schubert     printf_filtered (_("Core was generated by `%s'.\n"), p);
4295796c8dcSSimon Schubert 
4305796c8dcSSimon Schubert   siggy = bfd_core_file_failing_signal (core_bfd);
4315796c8dcSSimon Schubert   if (siggy > 0)
432c50c785cSJohn Marino     {
433*ef5ccd6cSJohn Marino       /* If we don't have a CORE_GDBARCH to work with, assume a native
434*ef5ccd6cSJohn Marino 	 core (map gdb_signal from host signals).  If we do have
435*ef5ccd6cSJohn Marino 	 CORE_GDBARCH to work with, but no gdb_signal_from_target
436*ef5ccd6cSJohn Marino 	 implementation for that gdbarch, as a fallback measure,
437*ef5ccd6cSJohn Marino 	 assume the host signal mapping.  It'll be correct for native
438*ef5ccd6cSJohn Marino 	 cores, but most likely incorrect for cross-cores.  */
439*ef5ccd6cSJohn Marino       enum gdb_signal sig = (core_gdbarch != NULL
440*ef5ccd6cSJohn Marino 			     && gdbarch_gdb_signal_from_target_p (core_gdbarch)
441*ef5ccd6cSJohn Marino 			     ? gdbarch_gdb_signal_from_target (core_gdbarch,
442c50c785cSJohn Marino 							       siggy)
443*ef5ccd6cSJohn Marino 			     : gdb_signal_from_host (siggy));
444c50c785cSJohn Marino 
445c50c785cSJohn Marino       printf_filtered (_("Program terminated with signal %d, %s.\n"),
446*ef5ccd6cSJohn Marino 		       siggy, gdb_signal_to_string (sig));
447c50c785cSJohn Marino     }
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert   /* Fetch all registers from core file.  */
4505796c8dcSSimon Schubert   target_fetch_registers (get_current_regcache (), -1);
4515796c8dcSSimon Schubert 
4525796c8dcSSimon Schubert   /* Now, set up the frame cache, and print the top of stack.  */
4535796c8dcSSimon Schubert   reinit_frame_cache ();
4545796c8dcSSimon Schubert   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
4555796c8dcSSimon Schubert }
4565796c8dcSSimon Schubert 
4575796c8dcSSimon Schubert static void
core_detach(struct target_ops * ops,char * args,int from_tty)4585796c8dcSSimon Schubert core_detach (struct target_ops *ops, char *args, int from_tty)
4595796c8dcSSimon Schubert {
4605796c8dcSSimon Schubert   if (args)
4615796c8dcSSimon Schubert     error (_("Too many arguments"));
4625796c8dcSSimon Schubert   unpush_target (ops);
4635796c8dcSSimon Schubert   reinit_frame_cache ();
4645796c8dcSSimon Schubert   if (from_tty)
4655796c8dcSSimon Schubert     printf_filtered (_("No core file now.\n"));
4665796c8dcSSimon Schubert }
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert #ifdef DEPRECATED_IBM6000_TARGET
4695796c8dcSSimon Schubert 
4705796c8dcSSimon Schubert /* Resize the core memory's section table, by NUM_ADDED.  Returns a
4715796c8dcSSimon Schubert    pointer into the first new slot.  This will not be necessary when
4725796c8dcSSimon Schubert    the rs6000 target is converted to use the standard solib
4735796c8dcSSimon Schubert    framework.  */
4745796c8dcSSimon Schubert 
4755796c8dcSSimon Schubert struct target_section *
deprecated_core_resize_section_table(int num_added)4765796c8dcSSimon Schubert deprecated_core_resize_section_table (int num_added)
4775796c8dcSSimon Schubert {
4785796c8dcSSimon Schubert   int old_count;
4795796c8dcSSimon Schubert 
4805796c8dcSSimon Schubert   old_count = resize_section_table (core_data, num_added);
4815796c8dcSSimon Schubert   return core_data->sections + old_count;
4825796c8dcSSimon Schubert }
4835796c8dcSSimon Schubert 
4845796c8dcSSimon Schubert #endif
4855796c8dcSSimon Schubert 
4865796c8dcSSimon Schubert /* Try to retrieve registers from a section in core_bfd, and supply
4875796c8dcSSimon Schubert    them to core_vec->core_read_registers, as the register set numbered
4885796c8dcSSimon Schubert    WHICH.
4895796c8dcSSimon Schubert 
4905796c8dcSSimon Schubert    If inferior_ptid's lwp member is zero, do the single-threaded
4915796c8dcSSimon Schubert    thing: look for a section named NAME.  If inferior_ptid's lwp
4925796c8dcSSimon Schubert    member is non-zero, do the multi-threaded thing: look for a section
4935796c8dcSSimon Schubert    named "NAME/LWP", where LWP is the shortest ASCII decimal
4945796c8dcSSimon Schubert    representation of inferior_ptid's lwp member.
4955796c8dcSSimon Schubert 
4965796c8dcSSimon Schubert    HUMAN_NAME is a human-readable name for the kind of registers the
4975796c8dcSSimon Schubert    NAME section contains, for use in error messages.
4985796c8dcSSimon Schubert 
4995796c8dcSSimon Schubert    If REQUIRED is non-zero, print an error if the core file doesn't
500c50c785cSJohn Marino    have a section by the appropriate name.  Otherwise, just do
501c50c785cSJohn Marino    nothing.  */
5025796c8dcSSimon Schubert 
5035796c8dcSSimon Schubert static void
get_core_register_section(struct regcache * regcache,const char * name,int which,const char * human_name,int required)5045796c8dcSSimon Schubert get_core_register_section (struct regcache *regcache,
505cf7f2e2dSJohn Marino 			   const char *name,
5065796c8dcSSimon Schubert 			   int which,
507cf7f2e2dSJohn Marino 			   const char *human_name,
5085796c8dcSSimon Schubert 			   int required)
5095796c8dcSSimon Schubert {
5105796c8dcSSimon Schubert   static char *section_name = NULL;
5115796c8dcSSimon Schubert   struct bfd_section *section;
5125796c8dcSSimon Schubert   bfd_size_type size;
5135796c8dcSSimon Schubert   char *contents;
5145796c8dcSSimon Schubert 
5155796c8dcSSimon Schubert   xfree (section_name);
5165796c8dcSSimon Schubert 
517c50c785cSJohn Marino   if (ptid_get_lwp (inferior_ptid))
518c50c785cSJohn Marino     section_name = xstrprintf ("%s/%ld", name,
519c50c785cSJohn Marino 			       ptid_get_lwp (inferior_ptid));
5205796c8dcSSimon Schubert   else
5215796c8dcSSimon Schubert     section_name = xstrdup (name);
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert   section = bfd_get_section_by_name (core_bfd, section_name);
5245796c8dcSSimon Schubert   if (! section)
5255796c8dcSSimon Schubert     {
5265796c8dcSSimon Schubert       if (required)
527c50c785cSJohn Marino 	warning (_("Couldn't find %s registers in core file."),
528c50c785cSJohn Marino 		 human_name);
5295796c8dcSSimon Schubert       return;
5305796c8dcSSimon Schubert     }
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert   size = bfd_section_size (core_bfd, section);
5335796c8dcSSimon Schubert   contents = alloca (size);
5345796c8dcSSimon Schubert   if (! bfd_get_section_contents (core_bfd, section, contents,
5355796c8dcSSimon Schubert 				  (file_ptr) 0, size))
5365796c8dcSSimon Schubert     {
5375796c8dcSSimon Schubert       warning (_("Couldn't read %s registers from `%s' section in core file."),
5385796c8dcSSimon Schubert 	       human_name, name);
5395796c8dcSSimon Schubert       return;
5405796c8dcSSimon Schubert     }
5415796c8dcSSimon Schubert 
5425796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
5435796c8dcSSimon Schubert     {
5445796c8dcSSimon Schubert       const struct regset *regset;
5455796c8dcSSimon Schubert 
546c50c785cSJohn Marino       regset = gdbarch_regset_from_core_section (core_gdbarch,
547c50c785cSJohn Marino 						 name, size);
5485796c8dcSSimon Schubert       if (regset == NULL)
5495796c8dcSSimon Schubert 	{
5505796c8dcSSimon Schubert 	  if (required)
5515796c8dcSSimon Schubert 	    warning (_("Couldn't recognize %s registers in core file."),
5525796c8dcSSimon Schubert 		     human_name);
5535796c8dcSSimon Schubert 	  return;
5545796c8dcSSimon Schubert 	}
5555796c8dcSSimon Schubert 
5565796c8dcSSimon Schubert       regset->supply_regset (regset, regcache, -1, contents, size);
5575796c8dcSSimon Schubert       return;
5585796c8dcSSimon Schubert     }
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert   gdb_assert (core_vec);
5615796c8dcSSimon Schubert   core_vec->core_read_registers (regcache, contents, size, which,
5625796c8dcSSimon Schubert 				 ((CORE_ADDR)
5635796c8dcSSimon Schubert 				  bfd_section_vma (core_bfd, section)));
5645796c8dcSSimon Schubert }
5655796c8dcSSimon Schubert 
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert /* Get the registers out of a core file.  This is the machine-
5685796c8dcSSimon Schubert    independent part.  Fetch_core_registers is the machine-dependent
569c50c785cSJohn Marino    part, typically implemented in the xm-file for each
570c50c785cSJohn Marino    architecture.  */
5715796c8dcSSimon Schubert 
5725796c8dcSSimon Schubert /* We just get all the registers, so we don't use regno.  */
5735796c8dcSSimon Schubert 
5745796c8dcSSimon Schubert static void
get_core_registers(struct target_ops * ops,struct regcache * regcache,int regno)5755796c8dcSSimon Schubert get_core_registers (struct target_ops *ops,
5765796c8dcSSimon Schubert 		    struct regcache *regcache, int regno)
5775796c8dcSSimon Schubert {
578cf7f2e2dSJohn Marino   struct core_regset_section *sect_list;
5795796c8dcSSimon Schubert   int i;
5805796c8dcSSimon Schubert 
5815796c8dcSSimon Schubert   if (!(core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
5825796c8dcSSimon Schubert       && (core_vec == NULL || core_vec->core_read_registers == NULL))
5835796c8dcSSimon Schubert     {
5845796c8dcSSimon Schubert       fprintf_filtered (gdb_stderr,
5855796c8dcSSimon Schubert 		     "Can't fetch registers from this type of core file\n");
5865796c8dcSSimon Schubert       return;
5875796c8dcSSimon Schubert     }
5885796c8dcSSimon Schubert 
589cf7f2e2dSJohn Marino   sect_list = gdbarch_core_regset_sections (get_regcache_arch (regcache));
590cf7f2e2dSJohn Marino   if (sect_list)
591cf7f2e2dSJohn Marino     while (sect_list->sect_name != NULL)
592cf7f2e2dSJohn Marino       {
593cf7f2e2dSJohn Marino         if (strcmp (sect_list->sect_name, ".reg") == 0)
594cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
595cf7f2e2dSJohn Marino 				     0, sect_list->human_name, 1);
596cf7f2e2dSJohn Marino         else if (strcmp (sect_list->sect_name, ".reg2") == 0)
597cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
598cf7f2e2dSJohn Marino 				     2, sect_list->human_name, 0);
599cf7f2e2dSJohn Marino 	else
600cf7f2e2dSJohn Marino 	  get_core_register_section (regcache, sect_list->sect_name,
601cf7f2e2dSJohn Marino 				     3, sect_list->human_name, 0);
602cf7f2e2dSJohn Marino 
603cf7f2e2dSJohn Marino 	sect_list++;
604cf7f2e2dSJohn Marino       }
605cf7f2e2dSJohn Marino 
606cf7f2e2dSJohn Marino   else
607cf7f2e2dSJohn Marino     {
6085796c8dcSSimon Schubert       get_core_register_section (regcache,
6095796c8dcSSimon Schubert 				 ".reg", 0, "general-purpose", 1);
6105796c8dcSSimon Schubert       get_core_register_section (regcache,
6115796c8dcSSimon Schubert 				 ".reg2", 2, "floating-point", 0);
612cf7f2e2dSJohn Marino     }
6135796c8dcSSimon Schubert 
614c50c785cSJohn Marino   /* Mark all registers not found in the core as unavailable.  */
6155796c8dcSSimon Schubert   for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
616c50c785cSJohn Marino     if (regcache_register_status (regcache, i) == REG_UNKNOWN)
6175796c8dcSSimon Schubert       regcache_raw_supply (regcache, i, NULL);
6185796c8dcSSimon Schubert }
6195796c8dcSSimon Schubert 
6205796c8dcSSimon Schubert static void
core_files_info(struct target_ops * t)6215796c8dcSSimon Schubert core_files_info (struct target_ops *t)
6225796c8dcSSimon Schubert {
6235796c8dcSSimon Schubert   print_section_info (core_data, core_bfd);
6245796c8dcSSimon Schubert }
6255796c8dcSSimon Schubert 
6265796c8dcSSimon Schubert struct spuid_list
6275796c8dcSSimon Schubert {
6285796c8dcSSimon Schubert   gdb_byte *buf;
6295796c8dcSSimon Schubert   ULONGEST offset;
6305796c8dcSSimon Schubert   LONGEST len;
6315796c8dcSSimon Schubert   ULONGEST pos;
6325796c8dcSSimon Schubert   ULONGEST written;
6335796c8dcSSimon Schubert };
6345796c8dcSSimon Schubert 
6355796c8dcSSimon Schubert static void
add_to_spuid_list(bfd * abfd,asection * asect,void * list_p)6365796c8dcSSimon Schubert add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
6375796c8dcSSimon Schubert {
6385796c8dcSSimon Schubert   struct spuid_list *list = list_p;
6395796c8dcSSimon Schubert   enum bfd_endian byte_order
6405796c8dcSSimon Schubert     = bfd_big_endian (abfd) ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
6415796c8dcSSimon Schubert   int fd, pos = 0;
6425796c8dcSSimon Schubert 
6435796c8dcSSimon Schubert   sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
6445796c8dcSSimon Schubert   if (pos == 0)
6455796c8dcSSimon Schubert     return;
6465796c8dcSSimon Schubert 
6475796c8dcSSimon Schubert   if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
6485796c8dcSSimon Schubert     {
6495796c8dcSSimon Schubert       store_unsigned_integer (list->buf + list->pos - list->offset,
6505796c8dcSSimon Schubert 			      4, byte_order, fd);
6515796c8dcSSimon Schubert       list->written += 4;
6525796c8dcSSimon Schubert     }
6535796c8dcSSimon Schubert   list->pos += 4;
6545796c8dcSSimon Schubert }
6555796c8dcSSimon Schubert 
656*ef5ccd6cSJohn Marino /* Read siginfo data from the core, if possible.  Returns -1 on
657*ef5ccd6cSJohn Marino    failure.  Otherwise, returns the number of bytes read.  ABFD is the
658*ef5ccd6cSJohn Marino    core file's BFD; READBUF, OFFSET, and LEN are all as specified by
659*ef5ccd6cSJohn Marino    the to_xfer_partial interface.  */
660*ef5ccd6cSJohn Marino 
661*ef5ccd6cSJohn Marino static LONGEST
get_core_siginfo(bfd * abfd,gdb_byte * readbuf,ULONGEST offset,LONGEST len)662*ef5ccd6cSJohn Marino get_core_siginfo (bfd *abfd, gdb_byte *readbuf, ULONGEST offset, LONGEST len)
663*ef5ccd6cSJohn Marino {
664*ef5ccd6cSJohn Marino   asection *section;
665*ef5ccd6cSJohn Marino   char *section_name;
666*ef5ccd6cSJohn Marino   const char *name = ".note.linuxcore.siginfo";
667*ef5ccd6cSJohn Marino 
668*ef5ccd6cSJohn Marino   if (ptid_get_lwp (inferior_ptid))
669*ef5ccd6cSJohn Marino     section_name = xstrprintf ("%s/%ld", name,
670*ef5ccd6cSJohn Marino 			       ptid_get_lwp (inferior_ptid));
671*ef5ccd6cSJohn Marino   else
672*ef5ccd6cSJohn Marino     section_name = xstrdup (name);
673*ef5ccd6cSJohn Marino 
674*ef5ccd6cSJohn Marino   section = bfd_get_section_by_name (abfd, section_name);
675*ef5ccd6cSJohn Marino   xfree (section_name);
676*ef5ccd6cSJohn Marino   if (section == NULL)
677*ef5ccd6cSJohn Marino     return -1;
678*ef5ccd6cSJohn Marino 
679*ef5ccd6cSJohn Marino   if (!bfd_get_section_contents (abfd, section, readbuf, offset, len))
680*ef5ccd6cSJohn Marino     return -1;
681*ef5ccd6cSJohn Marino 
682*ef5ccd6cSJohn Marino   return len;
683*ef5ccd6cSJohn Marino }
684*ef5ccd6cSJohn Marino 
6855796c8dcSSimon Schubert static LONGEST
core_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,LONGEST len)6865796c8dcSSimon Schubert core_xfer_partial (struct target_ops *ops, enum target_object object,
6875796c8dcSSimon Schubert 		   const char *annex, gdb_byte *readbuf,
688c50c785cSJohn Marino 		   const gdb_byte *writebuf, ULONGEST offset,
689c50c785cSJohn Marino 		   LONGEST len)
6905796c8dcSSimon Schubert {
6915796c8dcSSimon Schubert   switch (object)
6925796c8dcSSimon Schubert     {
6935796c8dcSSimon Schubert     case TARGET_OBJECT_MEMORY:
6945796c8dcSSimon Schubert       return section_table_xfer_memory_partial (readbuf, writebuf,
6955796c8dcSSimon Schubert 						offset, len,
6965796c8dcSSimon Schubert 						core_data->sections,
6975796c8dcSSimon Schubert 						core_data->sections_end,
6985796c8dcSSimon Schubert 						NULL);
6995796c8dcSSimon Schubert 
7005796c8dcSSimon Schubert     case TARGET_OBJECT_AUXV:
7015796c8dcSSimon Schubert       if (readbuf)
7025796c8dcSSimon Schubert 	{
7035796c8dcSSimon Schubert 	  /* When the aux vector is stored in core file, BFD
7045796c8dcSSimon Schubert 	     represents this with a fake section called ".auxv".  */
7055796c8dcSSimon Schubert 
7065796c8dcSSimon Schubert 	  struct bfd_section *section;
7075796c8dcSSimon Schubert 	  bfd_size_type size;
7085796c8dcSSimon Schubert 
7095796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, ".auxv");
7105796c8dcSSimon Schubert 	  if (section == NULL)
7115796c8dcSSimon Schubert 	    return -1;
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
7145796c8dcSSimon Schubert 	  if (offset >= size)
7155796c8dcSSimon Schubert 	    return 0;
7165796c8dcSSimon Schubert 	  size -= offset;
7175796c8dcSSimon Schubert 	  if (size > len)
7185796c8dcSSimon Schubert 	    size = len;
7195796c8dcSSimon Schubert 	  if (size > 0
7205796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
7215796c8dcSSimon Schubert 					    (file_ptr) offset, size))
7225796c8dcSSimon Schubert 	    {
7235796c8dcSSimon Schubert 	      warning (_("Couldn't read NT_AUXV note in core file."));
7245796c8dcSSimon Schubert 	      return -1;
7255796c8dcSSimon Schubert 	    }
7265796c8dcSSimon Schubert 
7275796c8dcSSimon Schubert 	  return size;
7285796c8dcSSimon Schubert 	}
7295796c8dcSSimon Schubert       return -1;
7305796c8dcSSimon Schubert 
7315796c8dcSSimon Schubert     case TARGET_OBJECT_WCOOKIE:
7325796c8dcSSimon Schubert       if (readbuf)
7335796c8dcSSimon Schubert 	{
7345796c8dcSSimon Schubert 	  /* When the StackGhost cookie is stored in core file, BFD
735c50c785cSJohn Marino 	     represents this with a fake section called
736c50c785cSJohn Marino 	     ".wcookie".  */
7375796c8dcSSimon Schubert 
7385796c8dcSSimon Schubert 	  struct bfd_section *section;
7395796c8dcSSimon Schubert 	  bfd_size_type size;
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, ".wcookie");
7425796c8dcSSimon Schubert 	  if (section == NULL)
7435796c8dcSSimon Schubert 	    return -1;
7445796c8dcSSimon Schubert 
7455796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
7465796c8dcSSimon Schubert 	  if (offset >= size)
7475796c8dcSSimon Schubert 	    return 0;
7485796c8dcSSimon Schubert 	  size -= offset;
7495796c8dcSSimon Schubert 	  if (size > len)
7505796c8dcSSimon Schubert 	    size = len;
7515796c8dcSSimon Schubert 	  if (size > 0
7525796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
7535796c8dcSSimon Schubert 					    (file_ptr) offset, size))
7545796c8dcSSimon Schubert 	    {
7555796c8dcSSimon Schubert 	      warning (_("Couldn't read StackGhost cookie in core file."));
7565796c8dcSSimon Schubert 	      return -1;
7575796c8dcSSimon Schubert 	    }
7585796c8dcSSimon Schubert 
7595796c8dcSSimon Schubert 	  return size;
7605796c8dcSSimon Schubert 	}
7615796c8dcSSimon Schubert       return -1;
7625796c8dcSSimon Schubert 
7635796c8dcSSimon Schubert     case TARGET_OBJECT_LIBRARIES:
7645796c8dcSSimon Schubert       if (core_gdbarch
7655796c8dcSSimon Schubert 	  && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
7665796c8dcSSimon Schubert 	{
7675796c8dcSSimon Schubert 	  if (writebuf)
7685796c8dcSSimon Schubert 	    return -1;
7695796c8dcSSimon Schubert 	  return
7705796c8dcSSimon Schubert 	    gdbarch_core_xfer_shared_libraries (core_gdbarch,
7715796c8dcSSimon Schubert 						readbuf, offset, len);
7725796c8dcSSimon Schubert 	}
7735796c8dcSSimon Schubert       /* FALL THROUGH */
7745796c8dcSSimon Schubert 
7755796c8dcSSimon Schubert     case TARGET_OBJECT_SPU:
7765796c8dcSSimon Schubert       if (readbuf && annex)
7775796c8dcSSimon Schubert 	{
7785796c8dcSSimon Schubert 	  /* When the SPU contexts are stored in a core file, BFD
779c50c785cSJohn Marino 	     represents this with a fake section called
780c50c785cSJohn Marino 	     "SPU/<annex>".  */
7815796c8dcSSimon Schubert 
7825796c8dcSSimon Schubert 	  struct bfd_section *section;
7835796c8dcSSimon Schubert 	  bfd_size_type size;
7845796c8dcSSimon Schubert 	  char sectionstr[100];
785cf7f2e2dSJohn Marino 
7865796c8dcSSimon Schubert 	  xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
7875796c8dcSSimon Schubert 
7885796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, sectionstr);
7895796c8dcSSimon Schubert 	  if (section == NULL)
7905796c8dcSSimon Schubert 	    return -1;
7915796c8dcSSimon Schubert 
7925796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
7935796c8dcSSimon Schubert 	  if (offset >= size)
7945796c8dcSSimon Schubert 	    return 0;
7955796c8dcSSimon Schubert 	  size -= offset;
7965796c8dcSSimon Schubert 	  if (size > len)
7975796c8dcSSimon Schubert 	    size = len;
7985796c8dcSSimon Schubert 	  if (size > 0
7995796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
8005796c8dcSSimon Schubert 					    (file_ptr) offset, size))
8015796c8dcSSimon Schubert 	    {
8025796c8dcSSimon Schubert 	      warning (_("Couldn't read SPU section in core file."));
8035796c8dcSSimon Schubert 	      return -1;
8045796c8dcSSimon Schubert 	    }
8055796c8dcSSimon Schubert 
8065796c8dcSSimon Schubert 	  return size;
8075796c8dcSSimon Schubert 	}
8085796c8dcSSimon Schubert       else if (readbuf)
8095796c8dcSSimon Schubert 	{
8105796c8dcSSimon Schubert 	  /* NULL annex requests list of all present spuids.  */
8115796c8dcSSimon Schubert 	  struct spuid_list list;
812cf7f2e2dSJohn Marino 
8135796c8dcSSimon Schubert 	  list.buf = readbuf;
8145796c8dcSSimon Schubert 	  list.offset = offset;
8155796c8dcSSimon Schubert 	  list.len = len;
8165796c8dcSSimon Schubert 	  list.pos = 0;
8175796c8dcSSimon Schubert 	  list.written = 0;
8185796c8dcSSimon Schubert 	  bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
8195796c8dcSSimon Schubert 	  return list.written;
8205796c8dcSSimon Schubert 	}
8215796c8dcSSimon Schubert       return -1;
8225796c8dcSSimon Schubert 
823*ef5ccd6cSJohn Marino     case TARGET_OBJECT_SIGNAL_INFO:
824*ef5ccd6cSJohn Marino       if (readbuf)
825*ef5ccd6cSJohn Marino 	return get_core_siginfo (core_bfd, readbuf, offset, len);
826*ef5ccd6cSJohn Marino       return -1;
827*ef5ccd6cSJohn Marino 
8285796c8dcSSimon Schubert     default:
8295796c8dcSSimon Schubert       if (ops->beneath != NULL)
830c50c785cSJohn Marino 	return ops->beneath->to_xfer_partial (ops->beneath, object,
831c50c785cSJohn Marino 					      annex, readbuf,
832c50c785cSJohn Marino 					      writebuf, offset, len);
8335796c8dcSSimon Schubert       return -1;
8345796c8dcSSimon Schubert     }
8355796c8dcSSimon Schubert }
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert 
8385796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say
839c50c785cSJohn Marino    `gdb internal error' (since generic_mourn calls
840c50c785cSJohn Marino    breakpoint_init_inferior).  */
8415796c8dcSSimon Schubert 
8425796c8dcSSimon Schubert static int
ignore(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)8435796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
8445796c8dcSSimon Schubert {
8455796c8dcSSimon Schubert   return 0;
8465796c8dcSSimon Schubert }
8475796c8dcSSimon Schubert 
8485796c8dcSSimon Schubert 
8495796c8dcSSimon Schubert /* Okay, let's be honest: threads gleaned from a core file aren't
8505796c8dcSSimon Schubert    exactly lively, are they?  On the other hand, if we don't claim
8515796c8dcSSimon Schubert    that each & every one is alive, then we don't get any of them
8525796c8dcSSimon Schubert    to appear in an "info thread" command, which is quite a useful
8535796c8dcSSimon Schubert    behaviour.
8545796c8dcSSimon Schubert  */
8555796c8dcSSimon Schubert static int
core_thread_alive(struct target_ops * ops,ptid_t ptid)8565796c8dcSSimon Schubert core_thread_alive (struct target_ops *ops, ptid_t ptid)
8575796c8dcSSimon Schubert {
8585796c8dcSSimon Schubert   return 1;
8595796c8dcSSimon Schubert }
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert /* Ask the current architecture what it knows about this core file.
8625796c8dcSSimon Schubert    That will be used, in turn, to pick a better architecture.  This
8635796c8dcSSimon Schubert    wrapper could be avoided if targets got a chance to specialize
8645796c8dcSSimon Schubert    core_ops.  */
8655796c8dcSSimon Schubert 
8665796c8dcSSimon Schubert static const struct target_desc *
core_read_description(struct target_ops * target)8675796c8dcSSimon Schubert core_read_description (struct target_ops *target)
8685796c8dcSSimon Schubert {
8695796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
870c50c785cSJohn Marino     return gdbarch_core_read_description (core_gdbarch,
871c50c785cSJohn Marino 					  target, core_bfd);
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   return NULL;
8745796c8dcSSimon Schubert }
8755796c8dcSSimon Schubert 
8765796c8dcSSimon Schubert static char *
core_pid_to_str(struct target_ops * ops,ptid_t ptid)8775796c8dcSSimon Schubert core_pid_to_str (struct target_ops *ops, ptid_t ptid)
8785796c8dcSSimon Schubert {
8795796c8dcSSimon Schubert   static char buf[64];
880*ef5ccd6cSJohn Marino   struct inferior *inf;
881c50c785cSJohn Marino   int pid;
8825796c8dcSSimon Schubert 
883c50c785cSJohn Marino   /* The preferred way is to have a gdbarch/OS specific
884c50c785cSJohn Marino      implementation.  */
8855796c8dcSSimon Schubert   if (core_gdbarch
8865796c8dcSSimon Schubert       && gdbarch_core_pid_to_str_p (core_gdbarch))
887c50c785cSJohn Marino     return gdbarch_core_pid_to_str (core_gdbarch, ptid);
888cf7f2e2dSJohn Marino 
889c50c785cSJohn Marino   /* Otherwise, if we don't have one, we'll just fallback to
890c50c785cSJohn Marino      "process", with normal_pid_to_str.  */
8915796c8dcSSimon Schubert 
892c50c785cSJohn Marino   /* Try the LWPID field first.  */
893c50c785cSJohn Marino   pid = ptid_get_lwp (ptid);
894c50c785cSJohn Marino   if (pid != 0)
895c50c785cSJohn Marino     return normal_pid_to_str (pid_to_ptid (pid));
896c50c785cSJohn Marino 
897c50c785cSJohn Marino   /* Otherwise, this isn't a "threaded" core -- use the PID field, but
898c50c785cSJohn Marino      only if it isn't a fake PID.  */
899*ef5ccd6cSJohn Marino   inf = find_inferior_pid (ptid_get_pid (ptid));
900*ef5ccd6cSJohn Marino   if (inf != NULL && !inf->fake_pid_p)
901c50c785cSJohn Marino     return normal_pid_to_str (ptid);
902c50c785cSJohn Marino 
903c50c785cSJohn Marino   /* No luck.  We simply don't have a valid PID to print.  */
9045796c8dcSSimon Schubert   xsnprintf (buf, sizeof buf, "<main task>");
9055796c8dcSSimon Schubert   return buf;
9065796c8dcSSimon Schubert }
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert static int
core_has_memory(struct target_ops * ops)9095796c8dcSSimon Schubert core_has_memory (struct target_ops *ops)
9105796c8dcSSimon Schubert {
9115796c8dcSSimon Schubert   return (core_bfd != NULL);
9125796c8dcSSimon Schubert }
9135796c8dcSSimon Schubert 
9145796c8dcSSimon Schubert static int
core_has_stack(struct target_ops * ops)9155796c8dcSSimon Schubert core_has_stack (struct target_ops *ops)
9165796c8dcSSimon Schubert {
9175796c8dcSSimon Schubert   return (core_bfd != NULL);
9185796c8dcSSimon Schubert }
9195796c8dcSSimon Schubert 
9205796c8dcSSimon Schubert static int
core_has_registers(struct target_ops * ops)9215796c8dcSSimon Schubert core_has_registers (struct target_ops *ops)
9225796c8dcSSimon Schubert {
9235796c8dcSSimon Schubert   return (core_bfd != NULL);
9245796c8dcSSimon Schubert }
9255796c8dcSSimon Schubert 
926*ef5ccd6cSJohn Marino /* Implement the to_info_proc method.  */
927*ef5ccd6cSJohn Marino 
928*ef5ccd6cSJohn Marino static void
core_info_proc(struct target_ops * ops,char * args,enum info_proc_what request)929*ef5ccd6cSJohn Marino core_info_proc (struct target_ops *ops, char *args, enum info_proc_what request)
930*ef5ccd6cSJohn Marino {
931*ef5ccd6cSJohn Marino   struct gdbarch *gdbarch = get_current_arch ();
932*ef5ccd6cSJohn Marino 
933*ef5ccd6cSJohn Marino   /* Since this is the core file target, call the 'core_info_proc'
934*ef5ccd6cSJohn Marino      method on gdbarch, not 'info_proc'.  */
935*ef5ccd6cSJohn Marino   if (gdbarch_core_info_proc_p (gdbarch))
936*ef5ccd6cSJohn Marino     gdbarch_core_info_proc (gdbarch, args, request);
937*ef5ccd6cSJohn Marino }
938*ef5ccd6cSJohn Marino 
9395796c8dcSSimon Schubert /* Fill in core_ops with its defined operations and properties.  */
9405796c8dcSSimon Schubert 
9415796c8dcSSimon Schubert static void
init_core_ops(void)9425796c8dcSSimon Schubert init_core_ops (void)
9435796c8dcSSimon Schubert {
9445796c8dcSSimon Schubert   core_ops.to_shortname = "core";
9455796c8dcSSimon Schubert   core_ops.to_longname = "Local core dump file";
9465796c8dcSSimon Schubert   core_ops.to_doc =
9475796c8dcSSimon Schubert     "Use a core file as a target.  Specify the filename of the core file.";
9485796c8dcSSimon Schubert   core_ops.to_open = core_open;
9495796c8dcSSimon Schubert   core_ops.to_close = core_close;
9505796c8dcSSimon Schubert   core_ops.to_attach = find_default_attach;
9515796c8dcSSimon Schubert   core_ops.to_detach = core_detach;
9525796c8dcSSimon Schubert   core_ops.to_fetch_registers = get_core_registers;
9535796c8dcSSimon Schubert   core_ops.to_xfer_partial = core_xfer_partial;
9545796c8dcSSimon Schubert   core_ops.to_files_info = core_files_info;
9555796c8dcSSimon Schubert   core_ops.to_insert_breakpoint = ignore;
9565796c8dcSSimon Schubert   core_ops.to_remove_breakpoint = ignore;
9575796c8dcSSimon Schubert   core_ops.to_create_inferior = find_default_create_inferior;
9585796c8dcSSimon Schubert   core_ops.to_thread_alive = core_thread_alive;
9595796c8dcSSimon Schubert   core_ops.to_read_description = core_read_description;
9605796c8dcSSimon Schubert   core_ops.to_pid_to_str = core_pid_to_str;
961c50c785cSJohn Marino   core_ops.to_stratum = process_stratum;
9625796c8dcSSimon Schubert   core_ops.to_has_memory = core_has_memory;
9635796c8dcSSimon Schubert   core_ops.to_has_stack = core_has_stack;
9645796c8dcSSimon Schubert   core_ops.to_has_registers = core_has_registers;
965*ef5ccd6cSJohn Marino   core_ops.to_info_proc = core_info_proc;
9665796c8dcSSimon Schubert   core_ops.to_magic = OPS_MAGIC;
967c50c785cSJohn Marino 
968c50c785cSJohn Marino   if (core_target)
969c50c785cSJohn Marino     internal_error (__FILE__, __LINE__,
970c50c785cSJohn Marino 		    _("init_core_ops: core target already exists (\"%s\")."),
971c50c785cSJohn Marino 		    core_target->to_longname);
972c50c785cSJohn Marino   core_target = &core_ops;
9735796c8dcSSimon Schubert }
9745796c8dcSSimon Schubert 
9755796c8dcSSimon Schubert void
_initialize_corelow(void)9765796c8dcSSimon Schubert _initialize_corelow (void)
9775796c8dcSSimon Schubert {
9785796c8dcSSimon Schubert   init_core_ops ();
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert   add_target (&core_ops);
9815796c8dcSSimon Schubert }
982