xref: /dflybsd-src/contrib/gdb-7/gdb/corelow.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Core dump and executable file functions below target vector, for GDB.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4*5796c8dcSSimon Schubert    1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
6*5796c8dcSSimon Schubert 
7*5796c8dcSSimon Schubert    This file is part of GDB.
8*5796c8dcSSimon Schubert 
9*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
10*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
11*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
12*5796c8dcSSimon Schubert    (at your option) any later version.
13*5796c8dcSSimon Schubert 
14*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
15*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*5796c8dcSSimon Schubert    GNU General Public License for more details.
18*5796c8dcSSimon Schubert 
19*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
20*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21*5796c8dcSSimon Schubert 
22*5796c8dcSSimon Schubert #include "defs.h"
23*5796c8dcSSimon Schubert #include "arch-utils.h"
24*5796c8dcSSimon Schubert #include "gdb_string.h"
25*5796c8dcSSimon Schubert #include <errno.h>
26*5796c8dcSSimon Schubert #include <signal.h>
27*5796c8dcSSimon Schubert #include <fcntl.h>
28*5796c8dcSSimon Schubert #ifdef HAVE_SYS_FILE_H
29*5796c8dcSSimon Schubert #include <sys/file.h>		/* needed for F_OK and friends */
30*5796c8dcSSimon Schubert #endif
31*5796c8dcSSimon Schubert #include "frame.h"		/* required by inferior.h */
32*5796c8dcSSimon Schubert #include "inferior.h"
33*5796c8dcSSimon Schubert #include "symtab.h"
34*5796c8dcSSimon Schubert #include "command.h"
35*5796c8dcSSimon Schubert #include "bfd.h"
36*5796c8dcSSimon Schubert #include "target.h"
37*5796c8dcSSimon Schubert #include "gdbcore.h"
38*5796c8dcSSimon Schubert #include "gdbthread.h"
39*5796c8dcSSimon Schubert #include "regcache.h"
40*5796c8dcSSimon Schubert #include "regset.h"
41*5796c8dcSSimon Schubert #include "symfile.h"
42*5796c8dcSSimon Schubert #include "exec.h"
43*5796c8dcSSimon Schubert #include "readline/readline.h"
44*5796c8dcSSimon Schubert #include "gdb_assert.h"
45*5796c8dcSSimon Schubert #include "exceptions.h"
46*5796c8dcSSimon Schubert #include "solib.h"
47*5796c8dcSSimon Schubert #include "filenames.h"
48*5796c8dcSSimon Schubert 
49*5796c8dcSSimon Schubert 
50*5796c8dcSSimon Schubert #ifndef O_LARGEFILE
51*5796c8dcSSimon Schubert #define O_LARGEFILE 0
52*5796c8dcSSimon Schubert #endif
53*5796c8dcSSimon Schubert 
54*5796c8dcSSimon Schubert /* List of all available core_fns.  On gdb startup, each core file
55*5796c8dcSSimon Schubert    register reader calls deprecated_add_core_fns() to register
56*5796c8dcSSimon Schubert    information on each core format it is prepared to read.  */
57*5796c8dcSSimon Schubert 
58*5796c8dcSSimon Schubert static struct core_fns *core_file_fns = NULL;
59*5796c8dcSSimon Schubert 
60*5796c8dcSSimon Schubert /* The core_fns for a core file handler that is prepared to read the core
61*5796c8dcSSimon Schubert    file currently open on core_bfd. */
62*5796c8dcSSimon Schubert 
63*5796c8dcSSimon Schubert static struct core_fns *core_vec = NULL;
64*5796c8dcSSimon Schubert 
65*5796c8dcSSimon Schubert /* FIXME: kettenis/20031023: Eventually this variable should
66*5796c8dcSSimon Schubert    disappear.  */
67*5796c8dcSSimon Schubert 
68*5796c8dcSSimon Schubert struct gdbarch *core_gdbarch = NULL;
69*5796c8dcSSimon Schubert 
70*5796c8dcSSimon Schubert /* Per-core data.  Currently, only the section table.  Note that these
71*5796c8dcSSimon Schubert    target sections are *not* mapped in the current address spaces' set
72*5796c8dcSSimon Schubert    of target sections --- those should come only from pure executable
73*5796c8dcSSimon Schubert    or shared library bfds.  The core bfd sections are an
74*5796c8dcSSimon Schubert    implementation detail of the core target, just like ptrace is for
75*5796c8dcSSimon Schubert    unix child targets.  */
76*5796c8dcSSimon Schubert static struct target_section_table *core_data;
77*5796c8dcSSimon Schubert 
78*5796c8dcSSimon Schubert static void core_files_info (struct target_ops *);
79*5796c8dcSSimon Schubert 
80*5796c8dcSSimon Schubert static struct core_fns *sniff_core_bfd (bfd *);
81*5796c8dcSSimon Schubert 
82*5796c8dcSSimon Schubert static int gdb_check_format (bfd *);
83*5796c8dcSSimon Schubert 
84*5796c8dcSSimon Schubert static void core_open (char *, int);
85*5796c8dcSSimon Schubert 
86*5796c8dcSSimon Schubert static void core_detach (struct target_ops *ops, char *, int);
87*5796c8dcSSimon Schubert 
88*5796c8dcSSimon Schubert static void core_close (int);
89*5796c8dcSSimon Schubert 
90*5796c8dcSSimon Schubert static void core_close_cleanup (void *ignore);
91*5796c8dcSSimon Schubert 
92*5796c8dcSSimon Schubert static void add_to_thread_list (bfd *, asection *, void *);
93*5796c8dcSSimon Schubert 
94*5796c8dcSSimon Schubert static void init_core_ops (void);
95*5796c8dcSSimon Schubert 
96*5796c8dcSSimon Schubert void _initialize_corelow (void);
97*5796c8dcSSimon Schubert 
98*5796c8dcSSimon Schubert struct target_ops core_ops;
99*5796c8dcSSimon Schubert 
100*5796c8dcSSimon Schubert /* An arbitrary identifier for the core inferior.  */
101*5796c8dcSSimon Schubert #define CORELOW_PID 1
102*5796c8dcSSimon Schubert 
103*5796c8dcSSimon Schubert /* Link a new core_fns into the global core_file_fns list.  Called on gdb
104*5796c8dcSSimon Schubert    startup by the _initialize routine in each core file register reader, to
105*5796c8dcSSimon Schubert    register information about each format the the reader is prepared to
106*5796c8dcSSimon Schubert    handle. */
107*5796c8dcSSimon Schubert 
108*5796c8dcSSimon Schubert void
109*5796c8dcSSimon Schubert deprecated_add_core_fns (struct core_fns *cf)
110*5796c8dcSSimon Schubert {
111*5796c8dcSSimon Schubert   cf->next = core_file_fns;
112*5796c8dcSSimon Schubert   core_file_fns = cf;
113*5796c8dcSSimon Schubert }
114*5796c8dcSSimon Schubert 
115*5796c8dcSSimon Schubert /* The default function that core file handlers can use to examine a
116*5796c8dcSSimon Schubert    core file BFD and decide whether or not to accept the job of
117*5796c8dcSSimon Schubert    reading the core file. */
118*5796c8dcSSimon Schubert 
119*5796c8dcSSimon Schubert int
120*5796c8dcSSimon Schubert default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
121*5796c8dcSSimon Schubert {
122*5796c8dcSSimon Schubert   int result;
123*5796c8dcSSimon Schubert 
124*5796c8dcSSimon Schubert   result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
125*5796c8dcSSimon Schubert   return (result);
126*5796c8dcSSimon Schubert }
127*5796c8dcSSimon Schubert 
128*5796c8dcSSimon Schubert /* Walk through the list of core functions to find a set that can
129*5796c8dcSSimon Schubert    handle the core file open on ABFD.  Default to the first one in the
130*5796c8dcSSimon Schubert    list if nothing matches.  Returns pointer to set that is
131*5796c8dcSSimon Schubert    selected. */
132*5796c8dcSSimon Schubert 
133*5796c8dcSSimon Schubert static struct core_fns *
134*5796c8dcSSimon Schubert sniff_core_bfd (bfd *abfd)
135*5796c8dcSSimon Schubert {
136*5796c8dcSSimon Schubert   struct core_fns *cf;
137*5796c8dcSSimon Schubert   struct core_fns *yummy = NULL;
138*5796c8dcSSimon Schubert   int matches = 0;;
139*5796c8dcSSimon Schubert 
140*5796c8dcSSimon Schubert   /* Don't sniff if we have support for register sets in CORE_GDBARCH.  */
141*5796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
142*5796c8dcSSimon Schubert     return NULL;
143*5796c8dcSSimon Schubert 
144*5796c8dcSSimon Schubert   for (cf = core_file_fns; cf != NULL; cf = cf->next)
145*5796c8dcSSimon Schubert     {
146*5796c8dcSSimon Schubert       if (cf->core_sniffer (cf, abfd))
147*5796c8dcSSimon Schubert 	{
148*5796c8dcSSimon Schubert 	  yummy = cf;
149*5796c8dcSSimon Schubert 	  matches++;
150*5796c8dcSSimon Schubert 	}
151*5796c8dcSSimon Schubert     }
152*5796c8dcSSimon Schubert   if (matches > 1)
153*5796c8dcSSimon Schubert     {
154*5796c8dcSSimon Schubert       warning (_("\"%s\": ambiguous core format, %d handlers match"),
155*5796c8dcSSimon Schubert 	       bfd_get_filename (abfd), matches);
156*5796c8dcSSimon Schubert     }
157*5796c8dcSSimon Schubert   else if (matches == 0)
158*5796c8dcSSimon Schubert     {
159*5796c8dcSSimon Schubert       warning (_("\"%s\": no core file handler recognizes format, using default"),
160*5796c8dcSSimon Schubert 	       bfd_get_filename (abfd));
161*5796c8dcSSimon Schubert     }
162*5796c8dcSSimon Schubert   if (yummy == NULL)
163*5796c8dcSSimon Schubert     {
164*5796c8dcSSimon Schubert       yummy = core_file_fns;
165*5796c8dcSSimon Schubert     }
166*5796c8dcSSimon Schubert   return (yummy);
167*5796c8dcSSimon Schubert }
168*5796c8dcSSimon Schubert 
169*5796c8dcSSimon Schubert /* The default is to reject every core file format we see.  Either
170*5796c8dcSSimon Schubert    BFD has to recognize it, or we have to provide a function in the
171*5796c8dcSSimon Schubert    core file handler that recognizes it. */
172*5796c8dcSSimon Schubert 
173*5796c8dcSSimon Schubert int
174*5796c8dcSSimon Schubert default_check_format (bfd *abfd)
175*5796c8dcSSimon Schubert {
176*5796c8dcSSimon Schubert   return (0);
177*5796c8dcSSimon Schubert }
178*5796c8dcSSimon Schubert 
179*5796c8dcSSimon Schubert /* Attempt to recognize core file formats that BFD rejects. */
180*5796c8dcSSimon Schubert 
181*5796c8dcSSimon Schubert static int
182*5796c8dcSSimon Schubert gdb_check_format (bfd *abfd)
183*5796c8dcSSimon Schubert {
184*5796c8dcSSimon Schubert   struct core_fns *cf;
185*5796c8dcSSimon Schubert 
186*5796c8dcSSimon Schubert   for (cf = core_file_fns; cf != NULL; cf = cf->next)
187*5796c8dcSSimon Schubert     {
188*5796c8dcSSimon Schubert       if (cf->check_format (abfd))
189*5796c8dcSSimon Schubert 	{
190*5796c8dcSSimon Schubert 	  return (1);
191*5796c8dcSSimon Schubert 	}
192*5796c8dcSSimon Schubert     }
193*5796c8dcSSimon Schubert   return (0);
194*5796c8dcSSimon Schubert }
195*5796c8dcSSimon Schubert 
196*5796c8dcSSimon Schubert /* Discard all vestiges of any previous core file and mark data and stack
197*5796c8dcSSimon Schubert    spaces as empty.  */
198*5796c8dcSSimon Schubert 
199*5796c8dcSSimon Schubert static void
200*5796c8dcSSimon Schubert core_close (int quitting)
201*5796c8dcSSimon Schubert {
202*5796c8dcSSimon Schubert   char *name;
203*5796c8dcSSimon Schubert 
204*5796c8dcSSimon Schubert   if (core_bfd)
205*5796c8dcSSimon Schubert     {
206*5796c8dcSSimon Schubert       int pid = ptid_get_pid (inferior_ptid);
207*5796c8dcSSimon Schubert       inferior_ptid = null_ptid;	/* Avoid confusion from thread stuff */
208*5796c8dcSSimon Schubert       delete_inferior_silent (pid);
209*5796c8dcSSimon Schubert 
210*5796c8dcSSimon Schubert       /* Clear out solib state while the bfd is still open. See
211*5796c8dcSSimon Schubert          comments in clear_solib in solib.c. */
212*5796c8dcSSimon Schubert       clear_solib ();
213*5796c8dcSSimon Schubert 
214*5796c8dcSSimon Schubert       xfree (core_data->sections);
215*5796c8dcSSimon Schubert       xfree (core_data);
216*5796c8dcSSimon Schubert       core_data = NULL;
217*5796c8dcSSimon Schubert 
218*5796c8dcSSimon Schubert       name = bfd_get_filename (core_bfd);
219*5796c8dcSSimon Schubert       if (!bfd_close (core_bfd))
220*5796c8dcSSimon Schubert 	warning (_("cannot close \"%s\": %s"),
221*5796c8dcSSimon Schubert 		 name, bfd_errmsg (bfd_get_error ()));
222*5796c8dcSSimon Schubert       xfree (name);
223*5796c8dcSSimon Schubert       core_bfd = NULL;
224*5796c8dcSSimon Schubert     }
225*5796c8dcSSimon Schubert   core_vec = NULL;
226*5796c8dcSSimon Schubert   core_gdbarch = NULL;
227*5796c8dcSSimon Schubert }
228*5796c8dcSSimon Schubert 
229*5796c8dcSSimon Schubert static void
230*5796c8dcSSimon Schubert core_close_cleanup (void *ignore)
231*5796c8dcSSimon Schubert {
232*5796c8dcSSimon Schubert   core_close (0/*ignored*/);
233*5796c8dcSSimon Schubert }
234*5796c8dcSSimon Schubert 
235*5796c8dcSSimon Schubert /* Look for sections whose names start with `.reg/' so that we can extract the
236*5796c8dcSSimon Schubert    list of threads in a core file.  */
237*5796c8dcSSimon Schubert 
238*5796c8dcSSimon Schubert static void
239*5796c8dcSSimon Schubert add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
240*5796c8dcSSimon Schubert {
241*5796c8dcSSimon Schubert   ptid_t ptid;
242*5796c8dcSSimon Schubert   int thread_id;
243*5796c8dcSSimon Schubert   asection *reg_sect = (asection *) reg_sect_arg;
244*5796c8dcSSimon Schubert 
245*5796c8dcSSimon Schubert   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
246*5796c8dcSSimon Schubert     return;
247*5796c8dcSSimon Schubert 
248*5796c8dcSSimon Schubert   thread_id = atoi (bfd_section_name (abfd, asect) + 5);
249*5796c8dcSSimon Schubert 
250*5796c8dcSSimon Schubert   if (core_gdbarch
251*5796c8dcSSimon Schubert       && gdbarch_core_reg_section_encodes_pid (core_gdbarch))
252*5796c8dcSSimon Schubert     {
253*5796c8dcSSimon Schubert       uint32_t merged_pid = thread_id;
254*5796c8dcSSimon Schubert       ptid = ptid_build (merged_pid & 0xffff,
255*5796c8dcSSimon Schubert 			 merged_pid >> 16, 0);
256*5796c8dcSSimon Schubert     }
257*5796c8dcSSimon Schubert   else
258*5796c8dcSSimon Schubert     ptid = ptid_build (ptid_get_pid (inferior_ptid), thread_id, 0);
259*5796c8dcSSimon Schubert 
260*5796c8dcSSimon Schubert   if (ptid_get_lwp (inferior_ptid) == 0)
261*5796c8dcSSimon Schubert     /* The main thread has already been added before getting here, and
262*5796c8dcSSimon Schubert        this is the first time we hear about a thread id.  Assume this
263*5796c8dcSSimon Schubert        is the main thread.  */
264*5796c8dcSSimon Schubert     thread_change_ptid (inferior_ptid, ptid);
265*5796c8dcSSimon Schubert   else
266*5796c8dcSSimon Schubert     /* Nope, really a new thread.  */
267*5796c8dcSSimon Schubert     add_thread (ptid);
268*5796c8dcSSimon Schubert 
269*5796c8dcSSimon Schubert /* Warning, Will Robinson, looking at BFD private data! */
270*5796c8dcSSimon Schubert 
271*5796c8dcSSimon Schubert   if (reg_sect != NULL
272*5796c8dcSSimon Schubert       && asect->filepos == reg_sect->filepos)	/* Did we find .reg? */
273*5796c8dcSSimon Schubert     inferior_ptid = ptid;			 /* Yes, make it current */
274*5796c8dcSSimon Schubert }
275*5796c8dcSSimon Schubert 
276*5796c8dcSSimon Schubert /* This routine opens and sets up the core file bfd.  */
277*5796c8dcSSimon Schubert 
278*5796c8dcSSimon Schubert static void
279*5796c8dcSSimon Schubert core_open (char *filename, int from_tty)
280*5796c8dcSSimon Schubert {
281*5796c8dcSSimon Schubert   const char *p;
282*5796c8dcSSimon Schubert   int siggy;
283*5796c8dcSSimon Schubert   struct cleanup *old_chain;
284*5796c8dcSSimon Schubert   char *temp;
285*5796c8dcSSimon Schubert   bfd *temp_bfd;
286*5796c8dcSSimon Schubert   int scratch_chan;
287*5796c8dcSSimon Schubert   int flags;
288*5796c8dcSSimon Schubert   int corelow_pid = CORELOW_PID;
289*5796c8dcSSimon Schubert 
290*5796c8dcSSimon Schubert   target_preopen (from_tty);
291*5796c8dcSSimon Schubert   if (!filename)
292*5796c8dcSSimon Schubert     {
293*5796c8dcSSimon Schubert       if (core_bfd)
294*5796c8dcSSimon Schubert 	error (_("No core file specified.  (Use `detach' to stop debugging a core file.)"));
295*5796c8dcSSimon Schubert       else
296*5796c8dcSSimon Schubert 	error (_("No core file specified."));
297*5796c8dcSSimon Schubert     }
298*5796c8dcSSimon Schubert 
299*5796c8dcSSimon Schubert   filename = tilde_expand (filename);
300*5796c8dcSSimon Schubert   if (!IS_ABSOLUTE_PATH(filename))
301*5796c8dcSSimon Schubert     {
302*5796c8dcSSimon Schubert       temp = concat (current_directory, "/", filename, (char *)NULL);
303*5796c8dcSSimon Schubert       xfree (filename);
304*5796c8dcSSimon Schubert       filename = temp;
305*5796c8dcSSimon Schubert     }
306*5796c8dcSSimon Schubert 
307*5796c8dcSSimon Schubert   old_chain = make_cleanup (xfree, filename);
308*5796c8dcSSimon Schubert 
309*5796c8dcSSimon Schubert   flags = O_BINARY | O_LARGEFILE;
310*5796c8dcSSimon Schubert   if (write_files)
311*5796c8dcSSimon Schubert     flags |= O_RDWR;
312*5796c8dcSSimon Schubert   else
313*5796c8dcSSimon Schubert     flags |= O_RDONLY;
314*5796c8dcSSimon Schubert   scratch_chan = open (filename, flags, 0);
315*5796c8dcSSimon Schubert   if (scratch_chan < 0)
316*5796c8dcSSimon Schubert     perror_with_name (filename);
317*5796c8dcSSimon Schubert 
318*5796c8dcSSimon Schubert   temp_bfd = bfd_fopen (filename, gnutarget,
319*5796c8dcSSimon Schubert 			write_files ? FOPEN_RUB : FOPEN_RB,
320*5796c8dcSSimon Schubert 			scratch_chan);
321*5796c8dcSSimon Schubert   if (temp_bfd == NULL)
322*5796c8dcSSimon Schubert     perror_with_name (filename);
323*5796c8dcSSimon Schubert 
324*5796c8dcSSimon Schubert   if (!bfd_check_format (temp_bfd, bfd_core) &&
325*5796c8dcSSimon Schubert       !gdb_check_format (temp_bfd))
326*5796c8dcSSimon Schubert     {
327*5796c8dcSSimon Schubert       /* Do it after the err msg */
328*5796c8dcSSimon Schubert       /* FIXME: should be checking for errors from bfd_close (for one thing,
329*5796c8dcSSimon Schubert          on error it does not free all the storage associated with the
330*5796c8dcSSimon Schubert          bfd).  */
331*5796c8dcSSimon Schubert       make_cleanup_bfd_close (temp_bfd);
332*5796c8dcSSimon Schubert       error (_("\"%s\" is not a core dump: %s"),
333*5796c8dcSSimon Schubert 	     filename, bfd_errmsg (bfd_get_error ()));
334*5796c8dcSSimon Schubert     }
335*5796c8dcSSimon Schubert 
336*5796c8dcSSimon Schubert   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
337*5796c8dcSSimon Schubert 
338*5796c8dcSSimon Schubert   discard_cleanups (old_chain);	/* Don't free filename any more */
339*5796c8dcSSimon Schubert   unpush_target (&core_ops);
340*5796c8dcSSimon Schubert   core_bfd = temp_bfd;
341*5796c8dcSSimon Schubert   old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
342*5796c8dcSSimon Schubert 
343*5796c8dcSSimon Schubert   /* FIXME: kettenis/20031023: This is very dangerous.  The
344*5796c8dcSSimon Schubert      CORE_GDBARCH that results from this call may very well be
345*5796c8dcSSimon Schubert      different from CURRENT_GDBARCH.  However, its methods may only
346*5796c8dcSSimon Schubert      work if it is selected as the current architecture, because they
347*5796c8dcSSimon Schubert      rely on swapped data (see gdbarch.c).  We should get rid of that
348*5796c8dcSSimon Schubert      swapped data.  */
349*5796c8dcSSimon Schubert   core_gdbarch = gdbarch_from_bfd (core_bfd);
350*5796c8dcSSimon Schubert 
351*5796c8dcSSimon Schubert   /* Find a suitable core file handler to munch on core_bfd */
352*5796c8dcSSimon Schubert   core_vec = sniff_core_bfd (core_bfd);
353*5796c8dcSSimon Schubert 
354*5796c8dcSSimon Schubert   validate_files ();
355*5796c8dcSSimon Schubert 
356*5796c8dcSSimon Schubert   core_data = XZALLOC (struct target_section_table);
357*5796c8dcSSimon Schubert 
358*5796c8dcSSimon Schubert   /* Find the data section */
359*5796c8dcSSimon Schubert   if (build_section_table (core_bfd,
360*5796c8dcSSimon Schubert 			   &core_data->sections, &core_data->sections_end))
361*5796c8dcSSimon Schubert     error (_("\"%s\": Can't find sections: %s"),
362*5796c8dcSSimon Schubert 	   bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
363*5796c8dcSSimon Schubert 
364*5796c8dcSSimon Schubert   /* If we have no exec file, try to set the architecture from the
365*5796c8dcSSimon Schubert      core file.  We don't do this unconditionally since an exec file
366*5796c8dcSSimon Schubert      typically contains more information that helps us determine the
367*5796c8dcSSimon Schubert      architecture than a core file.  */
368*5796c8dcSSimon Schubert   if (!exec_bfd)
369*5796c8dcSSimon Schubert     set_gdbarch_from_file (core_bfd);
370*5796c8dcSSimon Schubert 
371*5796c8dcSSimon Schubert   push_target (&core_ops);
372*5796c8dcSSimon Schubert   discard_cleanups (old_chain);
373*5796c8dcSSimon Schubert 
374*5796c8dcSSimon Schubert   add_inferior_silent (corelow_pid);
375*5796c8dcSSimon Schubert 
376*5796c8dcSSimon Schubert   /* Do this before acknowledging the inferior, so if
377*5796c8dcSSimon Schubert      post_create_inferior throws (can happen easilly if you're loading
378*5796c8dcSSimon Schubert      a core file with the wrong exec), we aren't left with threads
379*5796c8dcSSimon Schubert      from the previous inferior.  */
380*5796c8dcSSimon Schubert   init_thread_list ();
381*5796c8dcSSimon Schubert 
382*5796c8dcSSimon Schubert   /* Set INFERIOR_PTID early, so an upper layer can rely on it being
383*5796c8dcSSimon Schubert      set while in the target_find_new_threads call below.  */
384*5796c8dcSSimon Schubert   inferior_ptid = pid_to_ptid (corelow_pid);
385*5796c8dcSSimon Schubert 
386*5796c8dcSSimon Schubert   /* Assume ST --- Add a main task.  We'll later detect when we go
387*5796c8dcSSimon Schubert      from ST to MT.  */
388*5796c8dcSSimon Schubert   add_thread_silent (inferior_ptid);
389*5796c8dcSSimon Schubert 
390*5796c8dcSSimon Schubert   /* Need to flush the register cache (and the frame cache) from a
391*5796c8dcSSimon Schubert      previous debug session.  If inferior_ptid ends up the same as the
392*5796c8dcSSimon Schubert      last debug session --- e.g., b foo; run; gcore core1; step; gcore
393*5796c8dcSSimon Schubert      core2; core core1; core core2 --- then there's potential for
394*5796c8dcSSimon Schubert      get_current_regcache to return the cached regcache of the
395*5796c8dcSSimon Schubert      previous session, and the frame cache being stale.  */
396*5796c8dcSSimon Schubert   registers_changed ();
397*5796c8dcSSimon Schubert 
398*5796c8dcSSimon Schubert   /* Build up thread list from BFD sections, and possibly set the
399*5796c8dcSSimon Schubert      current thread to the .reg/NN section matching the .reg
400*5796c8dcSSimon Schubert      section. */
401*5796c8dcSSimon Schubert   bfd_map_over_sections (core_bfd, add_to_thread_list,
402*5796c8dcSSimon Schubert 			 bfd_get_section_by_name (core_bfd, ".reg"));
403*5796c8dcSSimon Schubert 
404*5796c8dcSSimon Schubert   post_create_inferior (&core_ops, from_tty);
405*5796c8dcSSimon Schubert 
406*5796c8dcSSimon Schubert   /* Now go through the target stack looking for threads since there
407*5796c8dcSSimon Schubert      may be a thread_stratum target loaded on top of target core by
408*5796c8dcSSimon Schubert      now.  The layer above should claim threads found in the BFD
409*5796c8dcSSimon Schubert      sections.  */
410*5796c8dcSSimon Schubert   target_find_new_threads ();
411*5796c8dcSSimon Schubert 
412*5796c8dcSSimon Schubert   p = bfd_core_file_failing_command (core_bfd);
413*5796c8dcSSimon Schubert   if (p)
414*5796c8dcSSimon Schubert     printf_filtered (_("Core was generated by `%s'.\n"), p);
415*5796c8dcSSimon Schubert 
416*5796c8dcSSimon Schubert   siggy = bfd_core_file_failing_signal (core_bfd);
417*5796c8dcSSimon Schubert   if (siggy > 0)
418*5796c8dcSSimon Schubert     /* NOTE: target_signal_from_host() converts a target signal value
419*5796c8dcSSimon Schubert        into gdb's internal signal value.  Unfortunately gdb's internal
420*5796c8dcSSimon Schubert        value is called ``target_signal'' and this function got the
421*5796c8dcSSimon Schubert        name ..._from_host(). */
422*5796c8dcSSimon Schubert     printf_filtered (_("Program terminated with signal %d, %s.\n"), siggy,
423*5796c8dcSSimon Schubert 		     target_signal_to_string (
424*5796c8dcSSimon Schubert 		       (core_gdbarch != NULL) ?
425*5796c8dcSSimon Schubert 			gdbarch_target_signal_from_host (core_gdbarch, siggy)
426*5796c8dcSSimon Schubert 			: siggy));
427*5796c8dcSSimon Schubert 
428*5796c8dcSSimon Schubert   /* Fetch all registers from core file.  */
429*5796c8dcSSimon Schubert   target_fetch_registers (get_current_regcache (), -1);
430*5796c8dcSSimon Schubert 
431*5796c8dcSSimon Schubert   /* Now, set up the frame cache, and print the top of stack.  */
432*5796c8dcSSimon Schubert   reinit_frame_cache ();
433*5796c8dcSSimon Schubert   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
434*5796c8dcSSimon Schubert }
435*5796c8dcSSimon Schubert 
436*5796c8dcSSimon Schubert static void
437*5796c8dcSSimon Schubert core_detach (struct target_ops *ops, char *args, int from_tty)
438*5796c8dcSSimon Schubert {
439*5796c8dcSSimon Schubert   if (args)
440*5796c8dcSSimon Schubert     error (_("Too many arguments"));
441*5796c8dcSSimon Schubert   unpush_target (ops);
442*5796c8dcSSimon Schubert   reinit_frame_cache ();
443*5796c8dcSSimon Schubert   if (from_tty)
444*5796c8dcSSimon Schubert     printf_filtered (_("No core file now.\n"));
445*5796c8dcSSimon Schubert }
446*5796c8dcSSimon Schubert 
447*5796c8dcSSimon Schubert #ifdef DEPRECATED_IBM6000_TARGET
448*5796c8dcSSimon Schubert 
449*5796c8dcSSimon Schubert /* Resize the core memory's section table, by NUM_ADDED.  Returns a
450*5796c8dcSSimon Schubert    pointer into the first new slot.  This will not be necessary when
451*5796c8dcSSimon Schubert    the rs6000 target is converted to use the standard solib
452*5796c8dcSSimon Schubert    framework.  */
453*5796c8dcSSimon Schubert 
454*5796c8dcSSimon Schubert struct target_section *
455*5796c8dcSSimon Schubert deprecated_core_resize_section_table (int num_added)
456*5796c8dcSSimon Schubert {
457*5796c8dcSSimon Schubert   int old_count;
458*5796c8dcSSimon Schubert 
459*5796c8dcSSimon Schubert   old_count = resize_section_table (core_data, num_added);
460*5796c8dcSSimon Schubert   return core_data->sections + old_count;
461*5796c8dcSSimon Schubert }
462*5796c8dcSSimon Schubert 
463*5796c8dcSSimon Schubert #endif
464*5796c8dcSSimon Schubert 
465*5796c8dcSSimon Schubert /* Try to retrieve registers from a section in core_bfd, and supply
466*5796c8dcSSimon Schubert    them to core_vec->core_read_registers, as the register set numbered
467*5796c8dcSSimon Schubert    WHICH.
468*5796c8dcSSimon Schubert 
469*5796c8dcSSimon Schubert    If inferior_ptid's lwp member is zero, do the single-threaded
470*5796c8dcSSimon Schubert    thing: look for a section named NAME.  If inferior_ptid's lwp
471*5796c8dcSSimon Schubert    member is non-zero, do the multi-threaded thing: look for a section
472*5796c8dcSSimon Schubert    named "NAME/LWP", where LWP is the shortest ASCII decimal
473*5796c8dcSSimon Schubert    representation of inferior_ptid's lwp member.
474*5796c8dcSSimon Schubert 
475*5796c8dcSSimon Schubert    HUMAN_NAME is a human-readable name for the kind of registers the
476*5796c8dcSSimon Schubert    NAME section contains, for use in error messages.
477*5796c8dcSSimon Schubert 
478*5796c8dcSSimon Schubert    If REQUIRED is non-zero, print an error if the core file doesn't
479*5796c8dcSSimon Schubert    have a section by the appropriate name.  Otherwise, just do nothing.  */
480*5796c8dcSSimon Schubert 
481*5796c8dcSSimon Schubert static void
482*5796c8dcSSimon Schubert get_core_register_section (struct regcache *regcache,
483*5796c8dcSSimon Schubert 			   char *name,
484*5796c8dcSSimon Schubert 			   int which,
485*5796c8dcSSimon Schubert 			   char *human_name,
486*5796c8dcSSimon Schubert 			   int required)
487*5796c8dcSSimon Schubert {
488*5796c8dcSSimon Schubert   static char *section_name = NULL;
489*5796c8dcSSimon Schubert   struct bfd_section *section;
490*5796c8dcSSimon Schubert   bfd_size_type size;
491*5796c8dcSSimon Schubert   char *contents;
492*5796c8dcSSimon Schubert 
493*5796c8dcSSimon Schubert   xfree (section_name);
494*5796c8dcSSimon Schubert 
495*5796c8dcSSimon Schubert   if (core_gdbarch
496*5796c8dcSSimon Schubert       && gdbarch_core_reg_section_encodes_pid (core_gdbarch))
497*5796c8dcSSimon Schubert     {
498*5796c8dcSSimon Schubert       uint32_t merged_pid;
499*5796c8dcSSimon Schubert 
500*5796c8dcSSimon Schubert       merged_pid = ptid_get_lwp (inferior_ptid);
501*5796c8dcSSimon Schubert       merged_pid = merged_pid << 16 | ptid_get_pid (inferior_ptid);
502*5796c8dcSSimon Schubert 
503*5796c8dcSSimon Schubert       section_name = xstrprintf ("%s/%s", name, plongest (merged_pid));
504*5796c8dcSSimon Schubert     }
505*5796c8dcSSimon Schubert   else if (ptid_get_lwp (inferior_ptid))
506*5796c8dcSSimon Schubert     section_name = xstrprintf ("%s/%ld", name, ptid_get_lwp (inferior_ptid));
507*5796c8dcSSimon Schubert   else
508*5796c8dcSSimon Schubert     section_name = xstrdup (name);
509*5796c8dcSSimon Schubert 
510*5796c8dcSSimon Schubert   section = bfd_get_section_by_name (core_bfd, section_name);
511*5796c8dcSSimon Schubert   if (! section)
512*5796c8dcSSimon Schubert     {
513*5796c8dcSSimon Schubert       if (required)
514*5796c8dcSSimon Schubert 	warning (_("Couldn't find %s registers in core file."), human_name);
515*5796c8dcSSimon Schubert       return;
516*5796c8dcSSimon Schubert     }
517*5796c8dcSSimon Schubert 
518*5796c8dcSSimon Schubert   size = bfd_section_size (core_bfd, section);
519*5796c8dcSSimon Schubert   contents = alloca (size);
520*5796c8dcSSimon Schubert   if (! bfd_get_section_contents (core_bfd, section, contents,
521*5796c8dcSSimon Schubert 				  (file_ptr) 0, size))
522*5796c8dcSSimon Schubert     {
523*5796c8dcSSimon Schubert       warning (_("Couldn't read %s registers from `%s' section in core file."),
524*5796c8dcSSimon Schubert 	       human_name, name);
525*5796c8dcSSimon Schubert       return;
526*5796c8dcSSimon Schubert     }
527*5796c8dcSSimon Schubert 
528*5796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
529*5796c8dcSSimon Schubert     {
530*5796c8dcSSimon Schubert       const struct regset *regset;
531*5796c8dcSSimon Schubert 
532*5796c8dcSSimon Schubert       regset = gdbarch_regset_from_core_section (core_gdbarch, name, size);
533*5796c8dcSSimon Schubert       if (regset == NULL)
534*5796c8dcSSimon Schubert 	{
535*5796c8dcSSimon Schubert 	  if (required)
536*5796c8dcSSimon Schubert 	    warning (_("Couldn't recognize %s registers in core file."),
537*5796c8dcSSimon Schubert 		     human_name);
538*5796c8dcSSimon Schubert 	  return;
539*5796c8dcSSimon Schubert 	}
540*5796c8dcSSimon Schubert 
541*5796c8dcSSimon Schubert       regset->supply_regset (regset, regcache, -1, contents, size);
542*5796c8dcSSimon Schubert       return;
543*5796c8dcSSimon Schubert     }
544*5796c8dcSSimon Schubert 
545*5796c8dcSSimon Schubert   gdb_assert (core_vec);
546*5796c8dcSSimon Schubert   core_vec->core_read_registers (regcache, contents, size, which,
547*5796c8dcSSimon Schubert 				 ((CORE_ADDR)
548*5796c8dcSSimon Schubert 				  bfd_section_vma (core_bfd, section)));
549*5796c8dcSSimon Schubert }
550*5796c8dcSSimon Schubert 
551*5796c8dcSSimon Schubert 
552*5796c8dcSSimon Schubert /* Get the registers out of a core file.  This is the machine-
553*5796c8dcSSimon Schubert    independent part.  Fetch_core_registers is the machine-dependent
554*5796c8dcSSimon Schubert    part, typically implemented in the xm-file for each architecture.  */
555*5796c8dcSSimon Schubert 
556*5796c8dcSSimon Schubert /* We just get all the registers, so we don't use regno.  */
557*5796c8dcSSimon Schubert 
558*5796c8dcSSimon Schubert static void
559*5796c8dcSSimon Schubert get_core_registers (struct target_ops *ops,
560*5796c8dcSSimon Schubert 		    struct regcache *regcache, int regno)
561*5796c8dcSSimon Schubert {
562*5796c8dcSSimon Schubert   int i;
563*5796c8dcSSimon Schubert 
564*5796c8dcSSimon Schubert   if (!(core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
565*5796c8dcSSimon Schubert       && (core_vec == NULL || core_vec->core_read_registers == NULL))
566*5796c8dcSSimon Schubert     {
567*5796c8dcSSimon Schubert       fprintf_filtered (gdb_stderr,
568*5796c8dcSSimon Schubert 		     "Can't fetch registers from this type of core file\n");
569*5796c8dcSSimon Schubert       return;
570*5796c8dcSSimon Schubert     }
571*5796c8dcSSimon Schubert 
572*5796c8dcSSimon Schubert   get_core_register_section (regcache,
573*5796c8dcSSimon Schubert 			     ".reg", 0, "general-purpose", 1);
574*5796c8dcSSimon Schubert   get_core_register_section (regcache,
575*5796c8dcSSimon Schubert 			     ".reg2", 2, "floating-point", 0);
576*5796c8dcSSimon Schubert   get_core_register_section (regcache,
577*5796c8dcSSimon Schubert 			     ".reg-xfp", 3, "extended floating-point", 0);
578*5796c8dcSSimon Schubert   get_core_register_section (regcache,
579*5796c8dcSSimon Schubert   			     ".reg-ppc-vmx", 3, "ppc Altivec", 0);
580*5796c8dcSSimon Schubert   get_core_register_section (regcache,
581*5796c8dcSSimon Schubert 			     ".reg-ppc-vsx", 4, "POWER7 VSX", 0);
582*5796c8dcSSimon Schubert 
583*5796c8dcSSimon Schubert   /* Supply dummy value for all registers not found in the core.  */
584*5796c8dcSSimon Schubert   for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
585*5796c8dcSSimon Schubert     if (!regcache_valid_p (regcache, i))
586*5796c8dcSSimon Schubert       regcache_raw_supply (regcache, i, NULL);
587*5796c8dcSSimon Schubert }
588*5796c8dcSSimon Schubert 
589*5796c8dcSSimon Schubert static void
590*5796c8dcSSimon Schubert core_files_info (struct target_ops *t)
591*5796c8dcSSimon Schubert {
592*5796c8dcSSimon Schubert   print_section_info (core_data, core_bfd);
593*5796c8dcSSimon Schubert }
594*5796c8dcSSimon Schubert 
595*5796c8dcSSimon Schubert struct spuid_list
596*5796c8dcSSimon Schubert {
597*5796c8dcSSimon Schubert   gdb_byte *buf;
598*5796c8dcSSimon Schubert   ULONGEST offset;
599*5796c8dcSSimon Schubert   LONGEST len;
600*5796c8dcSSimon Schubert   ULONGEST pos;
601*5796c8dcSSimon Schubert   ULONGEST written;
602*5796c8dcSSimon Schubert };
603*5796c8dcSSimon Schubert 
604*5796c8dcSSimon Schubert static void
605*5796c8dcSSimon Schubert add_to_spuid_list (bfd *abfd, asection *asect, void *list_p)
606*5796c8dcSSimon Schubert {
607*5796c8dcSSimon Schubert   struct spuid_list *list = list_p;
608*5796c8dcSSimon Schubert   enum bfd_endian byte_order
609*5796c8dcSSimon Schubert     = bfd_big_endian (abfd)? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
610*5796c8dcSSimon Schubert   int fd, pos = 0;
611*5796c8dcSSimon Schubert 
612*5796c8dcSSimon Schubert   sscanf (bfd_section_name (abfd, asect), "SPU/%d/regs%n", &fd, &pos);
613*5796c8dcSSimon Schubert   if (pos == 0)
614*5796c8dcSSimon Schubert     return;
615*5796c8dcSSimon Schubert 
616*5796c8dcSSimon Schubert   if (list->pos >= list->offset && list->pos + 4 <= list->offset + list->len)
617*5796c8dcSSimon Schubert     {
618*5796c8dcSSimon Schubert       store_unsigned_integer (list->buf + list->pos - list->offset,
619*5796c8dcSSimon Schubert 			      4, byte_order, fd);
620*5796c8dcSSimon Schubert       list->written += 4;
621*5796c8dcSSimon Schubert     }
622*5796c8dcSSimon Schubert   list->pos += 4;
623*5796c8dcSSimon Schubert }
624*5796c8dcSSimon Schubert 
625*5796c8dcSSimon Schubert static LONGEST
626*5796c8dcSSimon Schubert core_xfer_partial (struct target_ops *ops, enum target_object object,
627*5796c8dcSSimon Schubert 		   const char *annex, gdb_byte *readbuf,
628*5796c8dcSSimon Schubert 		   const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
629*5796c8dcSSimon Schubert {
630*5796c8dcSSimon Schubert   switch (object)
631*5796c8dcSSimon Schubert     {
632*5796c8dcSSimon Schubert     case TARGET_OBJECT_MEMORY:
633*5796c8dcSSimon Schubert       return section_table_xfer_memory_partial (readbuf, writebuf,
634*5796c8dcSSimon Schubert 						offset, len,
635*5796c8dcSSimon Schubert 						core_data->sections,
636*5796c8dcSSimon Schubert 						core_data->sections_end,
637*5796c8dcSSimon Schubert 						NULL);
638*5796c8dcSSimon Schubert 
639*5796c8dcSSimon Schubert     case TARGET_OBJECT_AUXV:
640*5796c8dcSSimon Schubert       if (readbuf)
641*5796c8dcSSimon Schubert 	{
642*5796c8dcSSimon Schubert 	  /* When the aux vector is stored in core file, BFD
643*5796c8dcSSimon Schubert 	     represents this with a fake section called ".auxv".  */
644*5796c8dcSSimon Schubert 
645*5796c8dcSSimon Schubert 	  struct bfd_section *section;
646*5796c8dcSSimon Schubert 	  bfd_size_type size;
647*5796c8dcSSimon Schubert 	  char *contents;
648*5796c8dcSSimon Schubert 
649*5796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, ".auxv");
650*5796c8dcSSimon Schubert 	  if (section == NULL)
651*5796c8dcSSimon Schubert 	    return -1;
652*5796c8dcSSimon Schubert 
653*5796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
654*5796c8dcSSimon Schubert 	  if (offset >= size)
655*5796c8dcSSimon Schubert 	    return 0;
656*5796c8dcSSimon Schubert 	  size -= offset;
657*5796c8dcSSimon Schubert 	  if (size > len)
658*5796c8dcSSimon Schubert 	    size = len;
659*5796c8dcSSimon Schubert 	  if (size > 0
660*5796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
661*5796c8dcSSimon Schubert 					    (file_ptr) offset, size))
662*5796c8dcSSimon Schubert 	    {
663*5796c8dcSSimon Schubert 	      warning (_("Couldn't read NT_AUXV note in core file."));
664*5796c8dcSSimon Schubert 	      return -1;
665*5796c8dcSSimon Schubert 	    }
666*5796c8dcSSimon Schubert 
667*5796c8dcSSimon Schubert 	  return size;
668*5796c8dcSSimon Schubert 	}
669*5796c8dcSSimon Schubert       return -1;
670*5796c8dcSSimon Schubert 
671*5796c8dcSSimon Schubert     case TARGET_OBJECT_WCOOKIE:
672*5796c8dcSSimon Schubert       if (readbuf)
673*5796c8dcSSimon Schubert 	{
674*5796c8dcSSimon Schubert 	  /* When the StackGhost cookie is stored in core file, BFD
675*5796c8dcSSimon Schubert 	     represents this with a fake section called ".wcookie".  */
676*5796c8dcSSimon Schubert 
677*5796c8dcSSimon Schubert 	  struct bfd_section *section;
678*5796c8dcSSimon Schubert 	  bfd_size_type size;
679*5796c8dcSSimon Schubert 	  char *contents;
680*5796c8dcSSimon Schubert 
681*5796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, ".wcookie");
682*5796c8dcSSimon Schubert 	  if (section == NULL)
683*5796c8dcSSimon Schubert 	    return -1;
684*5796c8dcSSimon Schubert 
685*5796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
686*5796c8dcSSimon Schubert 	  if (offset >= size)
687*5796c8dcSSimon Schubert 	    return 0;
688*5796c8dcSSimon Schubert 	  size -= offset;
689*5796c8dcSSimon Schubert 	  if (size > len)
690*5796c8dcSSimon Schubert 	    size = len;
691*5796c8dcSSimon Schubert 	  if (size > 0
692*5796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
693*5796c8dcSSimon Schubert 					    (file_ptr) offset, size))
694*5796c8dcSSimon Schubert 	    {
695*5796c8dcSSimon Schubert 	      warning (_("Couldn't read StackGhost cookie in core file."));
696*5796c8dcSSimon Schubert 	      return -1;
697*5796c8dcSSimon Schubert 	    }
698*5796c8dcSSimon Schubert 
699*5796c8dcSSimon Schubert 	  return size;
700*5796c8dcSSimon Schubert 	}
701*5796c8dcSSimon Schubert       return -1;
702*5796c8dcSSimon Schubert 
703*5796c8dcSSimon Schubert     case TARGET_OBJECT_LIBRARIES:
704*5796c8dcSSimon Schubert       if (core_gdbarch
705*5796c8dcSSimon Schubert 	  && gdbarch_core_xfer_shared_libraries_p (core_gdbarch))
706*5796c8dcSSimon Schubert 	{
707*5796c8dcSSimon Schubert 	  if (writebuf)
708*5796c8dcSSimon Schubert 	    return -1;
709*5796c8dcSSimon Schubert 	  return
710*5796c8dcSSimon Schubert 	    gdbarch_core_xfer_shared_libraries (core_gdbarch,
711*5796c8dcSSimon Schubert 						readbuf, offset, len);
712*5796c8dcSSimon Schubert 	}
713*5796c8dcSSimon Schubert       /* FALL THROUGH */
714*5796c8dcSSimon Schubert 
715*5796c8dcSSimon Schubert     case TARGET_OBJECT_SPU:
716*5796c8dcSSimon Schubert       if (readbuf && annex)
717*5796c8dcSSimon Schubert 	{
718*5796c8dcSSimon Schubert 	  /* When the SPU contexts are stored in a core file, BFD
719*5796c8dcSSimon Schubert 	     represents this with a fake section called "SPU/<annex>".  */
720*5796c8dcSSimon Schubert 
721*5796c8dcSSimon Schubert 	  struct bfd_section *section;
722*5796c8dcSSimon Schubert 	  bfd_size_type size;
723*5796c8dcSSimon Schubert 	  char *contents;
724*5796c8dcSSimon Schubert 
725*5796c8dcSSimon Schubert 	  char sectionstr[100];
726*5796c8dcSSimon Schubert 	  xsnprintf (sectionstr, sizeof sectionstr, "SPU/%s", annex);
727*5796c8dcSSimon Schubert 
728*5796c8dcSSimon Schubert 	  section = bfd_get_section_by_name (core_bfd, sectionstr);
729*5796c8dcSSimon Schubert 	  if (section == NULL)
730*5796c8dcSSimon Schubert 	    return -1;
731*5796c8dcSSimon Schubert 
732*5796c8dcSSimon Schubert 	  size = bfd_section_size (core_bfd, section);
733*5796c8dcSSimon Schubert 	  if (offset >= size)
734*5796c8dcSSimon Schubert 	    return 0;
735*5796c8dcSSimon Schubert 	  size -= offset;
736*5796c8dcSSimon Schubert 	  if (size > len)
737*5796c8dcSSimon Schubert 	    size = len;
738*5796c8dcSSimon Schubert 	  if (size > 0
739*5796c8dcSSimon Schubert 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
740*5796c8dcSSimon Schubert 					    (file_ptr) offset, size))
741*5796c8dcSSimon Schubert 	    {
742*5796c8dcSSimon Schubert 	      warning (_("Couldn't read SPU section in core file."));
743*5796c8dcSSimon Schubert 	      return -1;
744*5796c8dcSSimon Schubert 	    }
745*5796c8dcSSimon Schubert 
746*5796c8dcSSimon Schubert 	  return size;
747*5796c8dcSSimon Schubert 	}
748*5796c8dcSSimon Schubert       else if (readbuf)
749*5796c8dcSSimon Schubert 	{
750*5796c8dcSSimon Schubert 	  /* NULL annex requests list of all present spuids.  */
751*5796c8dcSSimon Schubert 	  struct spuid_list list;
752*5796c8dcSSimon Schubert 	  list.buf = readbuf;
753*5796c8dcSSimon Schubert 	  list.offset = offset;
754*5796c8dcSSimon Schubert 	  list.len = len;
755*5796c8dcSSimon Schubert 	  list.pos = 0;
756*5796c8dcSSimon Schubert 	  list.written = 0;
757*5796c8dcSSimon Schubert 	  bfd_map_over_sections (core_bfd, add_to_spuid_list, &list);
758*5796c8dcSSimon Schubert 	  return list.written;
759*5796c8dcSSimon Schubert 	}
760*5796c8dcSSimon Schubert       return -1;
761*5796c8dcSSimon Schubert 
762*5796c8dcSSimon Schubert     default:
763*5796c8dcSSimon Schubert       if (ops->beneath != NULL)
764*5796c8dcSSimon Schubert 	return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
765*5796c8dcSSimon Schubert 					      readbuf, writebuf, offset, len);
766*5796c8dcSSimon Schubert       return -1;
767*5796c8dcSSimon Schubert     }
768*5796c8dcSSimon Schubert }
769*5796c8dcSSimon Schubert 
770*5796c8dcSSimon Schubert 
771*5796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say
772*5796c8dcSSimon Schubert    `gdb internal error' (since generic_mourn calls breakpoint_init_inferior).  */
773*5796c8dcSSimon Schubert 
774*5796c8dcSSimon Schubert static int
775*5796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
776*5796c8dcSSimon Schubert {
777*5796c8dcSSimon Schubert   return 0;
778*5796c8dcSSimon Schubert }
779*5796c8dcSSimon Schubert 
780*5796c8dcSSimon Schubert 
781*5796c8dcSSimon Schubert /* Okay, let's be honest: threads gleaned from a core file aren't
782*5796c8dcSSimon Schubert    exactly lively, are they?  On the other hand, if we don't claim
783*5796c8dcSSimon Schubert    that each & every one is alive, then we don't get any of them
784*5796c8dcSSimon Schubert    to appear in an "info thread" command, which is quite a useful
785*5796c8dcSSimon Schubert    behaviour.
786*5796c8dcSSimon Schubert  */
787*5796c8dcSSimon Schubert static int
788*5796c8dcSSimon Schubert core_thread_alive (struct target_ops *ops, ptid_t ptid)
789*5796c8dcSSimon Schubert {
790*5796c8dcSSimon Schubert   return 1;
791*5796c8dcSSimon Schubert }
792*5796c8dcSSimon Schubert 
793*5796c8dcSSimon Schubert /* Ask the current architecture what it knows about this core file.
794*5796c8dcSSimon Schubert    That will be used, in turn, to pick a better architecture.  This
795*5796c8dcSSimon Schubert    wrapper could be avoided if targets got a chance to specialize
796*5796c8dcSSimon Schubert    core_ops.  */
797*5796c8dcSSimon Schubert 
798*5796c8dcSSimon Schubert static const struct target_desc *
799*5796c8dcSSimon Schubert core_read_description (struct target_ops *target)
800*5796c8dcSSimon Schubert {
801*5796c8dcSSimon Schubert   if (core_gdbarch && gdbarch_core_read_description_p (core_gdbarch))
802*5796c8dcSSimon Schubert     return gdbarch_core_read_description (core_gdbarch, target, core_bfd);
803*5796c8dcSSimon Schubert 
804*5796c8dcSSimon Schubert   return NULL;
805*5796c8dcSSimon Schubert }
806*5796c8dcSSimon Schubert 
807*5796c8dcSSimon Schubert static char *
808*5796c8dcSSimon Schubert core_pid_to_str (struct target_ops *ops, ptid_t ptid)
809*5796c8dcSSimon Schubert {
810*5796c8dcSSimon Schubert   static char buf[64];
811*5796c8dcSSimon Schubert 
812*5796c8dcSSimon Schubert   if (core_gdbarch
813*5796c8dcSSimon Schubert       && gdbarch_core_pid_to_str_p (core_gdbarch))
814*5796c8dcSSimon Schubert     {
815*5796c8dcSSimon Schubert       char *ret = gdbarch_core_pid_to_str (core_gdbarch, ptid);
816*5796c8dcSSimon Schubert       if (ret != NULL)
817*5796c8dcSSimon Schubert 	return ret;
818*5796c8dcSSimon Schubert     }
819*5796c8dcSSimon Schubert 
820*5796c8dcSSimon Schubert   if (ptid_get_lwp (ptid) == 0)
821*5796c8dcSSimon Schubert     xsnprintf (buf, sizeof buf, "<main task>");
822*5796c8dcSSimon Schubert   else
823*5796c8dcSSimon Schubert     xsnprintf (buf, sizeof buf, "Thread %ld", ptid_get_lwp (ptid));
824*5796c8dcSSimon Schubert 
825*5796c8dcSSimon Schubert   return buf;
826*5796c8dcSSimon Schubert }
827*5796c8dcSSimon Schubert 
828*5796c8dcSSimon Schubert static int
829*5796c8dcSSimon Schubert core_has_memory (struct target_ops *ops)
830*5796c8dcSSimon Schubert {
831*5796c8dcSSimon Schubert   return (core_bfd != NULL);
832*5796c8dcSSimon Schubert }
833*5796c8dcSSimon Schubert 
834*5796c8dcSSimon Schubert static int
835*5796c8dcSSimon Schubert core_has_stack (struct target_ops *ops)
836*5796c8dcSSimon Schubert {
837*5796c8dcSSimon Schubert   return (core_bfd != NULL);
838*5796c8dcSSimon Schubert }
839*5796c8dcSSimon Schubert 
840*5796c8dcSSimon Schubert static int
841*5796c8dcSSimon Schubert core_has_registers (struct target_ops *ops)
842*5796c8dcSSimon Schubert {
843*5796c8dcSSimon Schubert   return (core_bfd != NULL);
844*5796c8dcSSimon Schubert }
845*5796c8dcSSimon Schubert 
846*5796c8dcSSimon Schubert /* Fill in core_ops with its defined operations and properties.  */
847*5796c8dcSSimon Schubert 
848*5796c8dcSSimon Schubert static void
849*5796c8dcSSimon Schubert init_core_ops (void)
850*5796c8dcSSimon Schubert {
851*5796c8dcSSimon Schubert   core_ops.to_shortname = "core";
852*5796c8dcSSimon Schubert   core_ops.to_longname = "Local core dump file";
853*5796c8dcSSimon Schubert   core_ops.to_doc =
854*5796c8dcSSimon Schubert     "Use a core file as a target.  Specify the filename of the core file.";
855*5796c8dcSSimon Schubert   core_ops.to_open = core_open;
856*5796c8dcSSimon Schubert   core_ops.to_close = core_close;
857*5796c8dcSSimon Schubert   core_ops.to_attach = find_default_attach;
858*5796c8dcSSimon Schubert   core_ops.to_detach = core_detach;
859*5796c8dcSSimon Schubert   core_ops.to_fetch_registers = get_core_registers;
860*5796c8dcSSimon Schubert   core_ops.to_xfer_partial = core_xfer_partial;
861*5796c8dcSSimon Schubert   core_ops.to_files_info = core_files_info;
862*5796c8dcSSimon Schubert   core_ops.to_insert_breakpoint = ignore;
863*5796c8dcSSimon Schubert   core_ops.to_remove_breakpoint = ignore;
864*5796c8dcSSimon Schubert   core_ops.to_create_inferior = find_default_create_inferior;
865*5796c8dcSSimon Schubert   core_ops.to_thread_alive = core_thread_alive;
866*5796c8dcSSimon Schubert   core_ops.to_read_description = core_read_description;
867*5796c8dcSSimon Schubert   core_ops.to_pid_to_str = core_pid_to_str;
868*5796c8dcSSimon Schubert   core_ops.to_stratum = core_stratum;
869*5796c8dcSSimon Schubert   core_ops.to_has_memory = core_has_memory;
870*5796c8dcSSimon Schubert   core_ops.to_has_stack = core_has_stack;
871*5796c8dcSSimon Schubert   core_ops.to_has_registers = core_has_registers;
872*5796c8dcSSimon Schubert   core_ops.to_magic = OPS_MAGIC;
873*5796c8dcSSimon Schubert }
874*5796c8dcSSimon Schubert 
875*5796c8dcSSimon Schubert void
876*5796c8dcSSimon Schubert _initialize_corelow (void)
877*5796c8dcSSimon Schubert {
878*5796c8dcSSimon Schubert   init_core_ops ();
879*5796c8dcSSimon Schubert 
880*5796c8dcSSimon Schubert   add_target (&core_ops);
881*5796c8dcSSimon Schubert }
882