xref: /openbsd-src/gnu/usr.bin/binutils/gdb/corelow.c (revision 63addd46c1e40ca0f49488ddcdc4ab598023b0c1)
1e93f7393Sniklas /* Core dump and executable file functions below target vector, for GDB.
2b725ae77Skettenis 
3b725ae77Skettenis    Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4b725ae77Skettenis    1997, 1998, 1999, 2000, 2001, 2003, 2004 Free Software Foundation,
5b725ae77Skettenis    Inc.
6e93f7393Sniklas 
7e93f7393Sniklas    This file is part of GDB.
8e93f7393Sniklas 
9e93f7393Sniklas    This program is free software; you can redistribute it and/or modify
10e93f7393Sniklas    it under the terms of the GNU General Public License as published by
11e93f7393Sniklas    the Free Software Foundation; either version 2 of the License, or
12e93f7393Sniklas    (at your option) any later version.
13e93f7393Sniklas 
14e93f7393Sniklas    This program is distributed in the hope that it will be useful,
15e93f7393Sniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
16e93f7393Sniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17e93f7393Sniklas    GNU General Public License for more details.
18e93f7393Sniklas 
19e93f7393Sniklas    You should have received a copy of the GNU General Public License
20e93f7393Sniklas    along with this program; if not, write to the Free Software
21b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
22b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
23e93f7393Sniklas 
24e93f7393Sniklas #include "defs.h"
25b725ae77Skettenis #include "arch-utils.h"
26e93f7393Sniklas #include "gdb_string.h"
27e93f7393Sniklas #include <errno.h>
28e93f7393Sniklas #include <signal.h>
29e93f7393Sniklas #include <fcntl.h>
30b725ae77Skettenis #ifdef HAVE_SYS_FILE_H
31b725ae77Skettenis #include <sys/file.h>		/* needed for F_OK and friends */
32b725ae77Skettenis #endif
33e93f7393Sniklas #include "frame.h"		/* required by inferior.h */
34e93f7393Sniklas #include "inferior.h"
35e93f7393Sniklas #include "symtab.h"
36e93f7393Sniklas #include "command.h"
37e93f7393Sniklas #include "bfd.h"
38e93f7393Sniklas #include "target.h"
39e93f7393Sniklas #include "gdbcore.h"
40e93f7393Sniklas #include "gdbthread.h"
41b725ae77Skettenis #include "regcache.h"
42b725ae77Skettenis #include "regset.h"
43b725ae77Skettenis #include "symfile.h"
44b725ae77Skettenis #include "exec.h"
45b725ae77Skettenis #include "readline/readline.h"
46*63addd46Skettenis #include "observer.h"
47b725ae77Skettenis #include "gdb_assert.h"
48b725ae77Skettenis 
49b725ae77Skettenis #ifndef O_BINARY
50b725ae77Skettenis #define O_BINARY 0
51b725ae77Skettenis #endif
52e93f7393Sniklas 
53*63addd46Skettenis #ifndef O_LARGEFILE
54*63addd46Skettenis #define O_LARGEFILE 0
55*63addd46Skettenis #endif
56*63addd46Skettenis 
57*63addd46Skettenis /* List of all available core_fns.  On gdb startup, each core file
58*63addd46Skettenis    register reader calls deprecated_add_core_fns() to register
59*63addd46Skettenis    information on each core format it is prepared to read.  */
60e93f7393Sniklas 
61e93f7393Sniklas static struct core_fns *core_file_fns = NULL;
62e93f7393Sniklas 
63b725ae77Skettenis /* The core_fns for a core file handler that is prepared to read the core
64b725ae77Skettenis    file currently open on core_bfd. */
65b725ae77Skettenis 
66b725ae77Skettenis static struct core_fns *core_vec = NULL;
67b725ae77Skettenis 
68b725ae77Skettenis /* FIXME: kettenis/20031023: Eventually this variable should
69b725ae77Skettenis    disappear.  */
70b725ae77Skettenis 
71b725ae77Skettenis struct gdbarch *core_gdbarch = NULL;
72b725ae77Skettenis 
73b725ae77Skettenis static void core_files_info (struct target_ops *);
74e93f7393Sniklas 
75e93f7393Sniklas #ifdef SOLIB_ADD
76b725ae77Skettenis static int solib_add_stub (void *);
77e93f7393Sniklas #endif
78e93f7393Sniklas 
79b725ae77Skettenis static struct core_fns *sniff_core_bfd (bfd *);
80e93f7393Sniklas 
81b725ae77Skettenis static int gdb_check_format (bfd *);
82e93f7393Sniklas 
83b725ae77Skettenis static void core_open (char *, int);
84e93f7393Sniklas 
85b725ae77Skettenis static void core_detach (char *, int);
86e93f7393Sniklas 
87b725ae77Skettenis static void core_close (int);
88e93f7393Sniklas 
89b725ae77Skettenis static void core_close_cleanup (void *ignore);
90b725ae77Skettenis 
91b725ae77Skettenis static void get_core_registers (int);
92b725ae77Skettenis 
93b725ae77Skettenis static void add_to_thread_list (bfd *, asection *, void *);
94b725ae77Skettenis 
95b725ae77Skettenis static int ignore (CORE_ADDR, char *);
96b725ae77Skettenis 
97b725ae77Skettenis static int core_file_thread_alive (ptid_t tid);
98b725ae77Skettenis 
99b725ae77Skettenis static void init_core_ops (void);
100b725ae77Skettenis 
101b725ae77Skettenis void _initialize_corelow (void);
102b725ae77Skettenis 
103b725ae77Skettenis struct target_ops core_ops;
104e93f7393Sniklas 
105e93f7393Sniklas /* Link a new core_fns into the global core_file_fns list.  Called on gdb
106e93f7393Sniklas    startup by the _initialize routine in each core file register reader, to
107e93f7393Sniklas    register information about each format the the reader is prepared to
108e93f7393Sniklas    handle. */
109e93f7393Sniklas 
110e93f7393Sniklas void
deprecated_add_core_fns(struct core_fns * cf)111*63addd46Skettenis deprecated_add_core_fns (struct core_fns *cf)
112e93f7393Sniklas {
113e93f7393Sniklas   cf->next = core_file_fns;
114e93f7393Sniklas   core_file_fns = cf;
115e93f7393Sniklas }
116e93f7393Sniklas 
117b725ae77Skettenis /* The default function that core file handlers can use to examine a
118b725ae77Skettenis    core file BFD and decide whether or not to accept the job of
119b725ae77Skettenis    reading the core file. */
120b725ae77Skettenis 
121b725ae77Skettenis int
default_core_sniffer(struct core_fns * our_fns,bfd * abfd)122b725ae77Skettenis default_core_sniffer (struct core_fns *our_fns, bfd *abfd)
123b725ae77Skettenis {
124b725ae77Skettenis   int result;
125b725ae77Skettenis 
126b725ae77Skettenis   result = (bfd_get_flavour (abfd) == our_fns -> core_flavour);
127b725ae77Skettenis   return (result);
128b725ae77Skettenis }
129b725ae77Skettenis 
130b725ae77Skettenis /* Walk through the list of core functions to find a set that can
131b725ae77Skettenis    handle the core file open on ABFD.  Default to the first one in the
132b725ae77Skettenis    list if nothing matches.  Returns pointer to set that is
133b725ae77Skettenis    selected. */
134b725ae77Skettenis 
135b725ae77Skettenis static struct core_fns *
sniff_core_bfd(bfd * abfd)136b725ae77Skettenis sniff_core_bfd (bfd *abfd)
137b725ae77Skettenis {
138b725ae77Skettenis   struct core_fns *cf;
139b725ae77Skettenis   struct core_fns *yummy = NULL;
140b725ae77Skettenis   int matches = 0;;
141b725ae77Skettenis 
142b725ae77Skettenis   /* Don't sniff if we have support for register sets in CORE_GDBARCH.  */
143b725ae77Skettenis   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
144b725ae77Skettenis     return NULL;
145b725ae77Skettenis 
146b725ae77Skettenis   for (cf = core_file_fns; cf != NULL; cf = cf->next)
147b725ae77Skettenis     {
148b725ae77Skettenis       if (cf->core_sniffer (cf, abfd))
149b725ae77Skettenis 	{
150b725ae77Skettenis 	  yummy = cf;
151b725ae77Skettenis 	  matches++;
152b725ae77Skettenis 	}
153b725ae77Skettenis     }
154b725ae77Skettenis   if (matches > 1)
155b725ae77Skettenis     {
156b725ae77Skettenis       warning ("\"%s\": ambiguous core format, %d handlers match",
157b725ae77Skettenis 	       bfd_get_filename (abfd), matches);
158b725ae77Skettenis     }
159b725ae77Skettenis   else if (matches == 0)
160b725ae77Skettenis     {
161b725ae77Skettenis       warning ("\"%s\": no core file handler recognizes format, using default",
162b725ae77Skettenis 	       bfd_get_filename (abfd));
163b725ae77Skettenis     }
164b725ae77Skettenis   if (yummy == NULL)
165b725ae77Skettenis     {
166b725ae77Skettenis       yummy = core_file_fns;
167b725ae77Skettenis     }
168b725ae77Skettenis   return (yummy);
169b725ae77Skettenis }
170b725ae77Skettenis 
171b725ae77Skettenis /* The default is to reject every core file format we see.  Either
172b725ae77Skettenis    BFD has to recognize it, or we have to provide a function in the
173b725ae77Skettenis    core file handler that recognizes it. */
174b725ae77Skettenis 
175b725ae77Skettenis int
default_check_format(bfd * abfd)176b725ae77Skettenis default_check_format (bfd *abfd)
177b725ae77Skettenis {
178b725ae77Skettenis   return (0);
179b725ae77Skettenis }
180b725ae77Skettenis 
181b725ae77Skettenis /* Attempt to recognize core file formats that BFD rejects. */
182b725ae77Skettenis 
183b725ae77Skettenis static int
gdb_check_format(bfd * abfd)184b725ae77Skettenis gdb_check_format (bfd *abfd)
185b725ae77Skettenis {
186b725ae77Skettenis   struct core_fns *cf;
187b725ae77Skettenis 
188b725ae77Skettenis   for (cf = core_file_fns; cf != NULL; cf = cf->next)
189b725ae77Skettenis     {
190b725ae77Skettenis       if (cf->check_format (abfd))
191b725ae77Skettenis 	{
192b725ae77Skettenis 	  return (1);
193b725ae77Skettenis 	}
194b725ae77Skettenis     }
195b725ae77Skettenis   return (0);
196b725ae77Skettenis }
197e93f7393Sniklas 
198e93f7393Sniklas /* Discard all vestiges of any previous core file and mark data and stack
199e93f7393Sniklas    spaces as empty.  */
200e93f7393Sniklas 
201e93f7393Sniklas static void
core_close(int quitting)202b725ae77Skettenis core_close (int quitting)
203e93f7393Sniklas {
204e93f7393Sniklas   char *name;
205e93f7393Sniklas 
206e93f7393Sniklas   if (core_bfd)
207e93f7393Sniklas     {
208b725ae77Skettenis       inferior_ptid = null_ptid;	/* Avoid confusion from thread stuff */
209b725ae77Skettenis 
210b725ae77Skettenis       /* Clear out solib state while the bfd is still open. See
211b725ae77Skettenis          comments in clear_solib in solib.c. */
212b725ae77Skettenis #ifdef CLEAR_SOLIB
213b725ae77Skettenis       CLEAR_SOLIB ();
214b725ae77Skettenis #endif
215e93f7393Sniklas 
216e93f7393Sniklas       name = bfd_get_filename (core_bfd);
217e93f7393Sniklas       if (!bfd_close (core_bfd))
218e93f7393Sniklas 	warning ("cannot close \"%s\": %s",
219e93f7393Sniklas 		 name, bfd_errmsg (bfd_get_error ()));
220b725ae77Skettenis       xfree (name);
221e93f7393Sniklas       core_bfd = NULL;
222e93f7393Sniklas       if (core_ops.to_sections)
223e93f7393Sniklas 	{
224b725ae77Skettenis 	  xfree (core_ops.to_sections);
225e93f7393Sniklas 	  core_ops.to_sections = NULL;
226e93f7393Sniklas 	  core_ops.to_sections_end = NULL;
227e93f7393Sniklas 	}
228e93f7393Sniklas     }
229b725ae77Skettenis   core_vec = NULL;
230b725ae77Skettenis   core_gdbarch = NULL;
231b725ae77Skettenis }
232b725ae77Skettenis 
233b725ae77Skettenis static void
core_close_cleanup(void * ignore)234b725ae77Skettenis core_close_cleanup (void *ignore)
235b725ae77Skettenis {
236b725ae77Skettenis   core_close (0/*ignored*/);
237e93f7393Sniklas }
238e93f7393Sniklas 
239e93f7393Sniklas #ifdef SOLIB_ADD
240e93f7393Sniklas /* Stub function for catch_errors around shared library hacking.  FROM_TTYP
241e93f7393Sniklas    is really an int * which points to from_tty.  */
242e93f7393Sniklas 
243e93f7393Sniklas static int
solib_add_stub(void * from_ttyp)244b725ae77Skettenis solib_add_stub (void *from_ttyp)
245e93f7393Sniklas {
246b725ae77Skettenis   SOLIB_ADD (NULL, *(int *) from_ttyp, &current_target, auto_solib_add);
247e93f7393Sniklas   re_enable_breakpoints_in_shlibs ();
248e93f7393Sniklas   return 0;
249e93f7393Sniklas }
250e93f7393Sniklas #endif /* SOLIB_ADD */
251e93f7393Sniklas 
252e93f7393Sniklas /* Look for sections whose names start with `.reg/' so that we can extract the
253e93f7393Sniklas    list of threads in a core file.  */
254e93f7393Sniklas 
255e93f7393Sniklas static void
add_to_thread_list(bfd * abfd,asection * asect,void * reg_sect_arg)256b725ae77Skettenis add_to_thread_list (bfd *abfd, asection *asect, void *reg_sect_arg)
257e93f7393Sniklas {
258e93f7393Sniklas   int thread_id;
259e93f7393Sniklas   asection *reg_sect = (asection *) reg_sect_arg;
260e93f7393Sniklas 
261e93f7393Sniklas   if (strncmp (bfd_section_name (abfd, asect), ".reg/", 5) != 0)
262e93f7393Sniklas     return;
263e93f7393Sniklas 
264e93f7393Sniklas   thread_id = atoi (bfd_section_name (abfd, asect) + 5);
265e93f7393Sniklas 
266b725ae77Skettenis   add_thread (pid_to_ptid (thread_id));
267e93f7393Sniklas 
268e93f7393Sniklas /* Warning, Will Robinson, looking at BFD private data! */
269e93f7393Sniklas 
270e93f7393Sniklas   if (reg_sect != NULL
271e93f7393Sniklas       && asect->filepos == reg_sect->filepos)	/* Did we find .reg? */
272b725ae77Skettenis     inferior_ptid = pid_to_ptid (thread_id);	/* Yes, make it current */
273e93f7393Sniklas }
274e93f7393Sniklas 
275e93f7393Sniklas /* This routine opens and sets up the core file bfd.  */
276e93f7393Sniklas 
277e93f7393Sniklas static void
core_open(char * filename,int from_tty)278b725ae77Skettenis core_open (char *filename, int from_tty)
279e93f7393Sniklas {
280e93f7393Sniklas   const char *p;
281e93f7393Sniklas   int siggy;
282e93f7393Sniklas   struct cleanup *old_chain;
283e93f7393Sniklas   char *temp;
284e93f7393Sniklas   bfd *temp_bfd;
285e93f7393Sniklas   int ontop;
286e93f7393Sniklas   int scratch_chan;
287*63addd46Skettenis   int flags;
288e93f7393Sniklas 
289e93f7393Sniklas   target_preopen (from_tty);
290e93f7393Sniklas   if (!filename)
291e93f7393Sniklas     {
292e93f7393Sniklas       error (core_bfd ?
293e93f7393Sniklas 	     "No core file specified.  (Use `detach' to stop debugging a core file.)"
294e93f7393Sniklas 	     : "No core file specified.");
295e93f7393Sniklas     }
296e93f7393Sniklas 
297e93f7393Sniklas   filename = tilde_expand (filename);
298e93f7393Sniklas   if (filename[0] != '/')
299e93f7393Sniklas     {
300e93f7393Sniklas       temp = concat (current_directory, "/", filename, NULL);
301b725ae77Skettenis       xfree (filename);
302e93f7393Sniklas       filename = temp;
303e93f7393Sniklas     }
304e93f7393Sniklas 
305b725ae77Skettenis   old_chain = make_cleanup (xfree, filename);
306e93f7393Sniklas 
307*63addd46Skettenis   flags = O_BINARY | O_LARGEFILE;
308*63addd46Skettenis   if (write_files)
309*63addd46Skettenis     flags |= O_RDWR;
310*63addd46Skettenis   else
311*63addd46Skettenis     flags |= O_RDONLY;
312*63addd46Skettenis   scratch_chan = open (filename, flags, 0);
313e93f7393Sniklas   if (scratch_chan < 0)
314e93f7393Sniklas     perror_with_name (filename);
315e93f7393Sniklas 
316e93f7393Sniklas   temp_bfd = bfd_fdopenr (filename, gnutarget, scratch_chan);
317e93f7393Sniklas   if (temp_bfd == NULL)
318e93f7393Sniklas     perror_with_name (filename);
319e93f7393Sniklas 
320b725ae77Skettenis   if (!bfd_check_format (temp_bfd, bfd_core) &&
321b725ae77Skettenis       !gdb_check_format (temp_bfd))
322e93f7393Sniklas     {
323e93f7393Sniklas       /* Do it after the err msg */
324e93f7393Sniklas       /* FIXME: should be checking for errors from bfd_close (for one thing,
325e93f7393Sniklas          on error it does not free all the storage associated with the
326e93f7393Sniklas          bfd).  */
327b725ae77Skettenis       make_cleanup_bfd_close (temp_bfd);
328e93f7393Sniklas       error ("\"%s\" is not a core dump: %s",
329e93f7393Sniklas 	     filename, bfd_errmsg (bfd_get_error ()));
330e93f7393Sniklas     }
331e93f7393Sniklas 
332e93f7393Sniklas   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
333e93f7393Sniklas 
334e93f7393Sniklas   discard_cleanups (old_chain);	/* Don't free filename any more */
335e93f7393Sniklas   unpush_target (&core_ops);
336e93f7393Sniklas   core_bfd = temp_bfd;
337b725ae77Skettenis   old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/);
338b725ae77Skettenis 
339b725ae77Skettenis   /* FIXME: kettenis/20031023: This is very dangerous.  The
340b725ae77Skettenis      CORE_GDBARCH that results from this call may very well be
341b725ae77Skettenis      different from CURRENT_GDBARCH.  However, its methods may only
342b725ae77Skettenis      work if it is selected as the current architecture, because they
343b725ae77Skettenis      rely on swapped data (see gdbarch.c).  We should get rid of that
344b725ae77Skettenis      swapped data.  */
345b725ae77Skettenis   core_gdbarch = gdbarch_from_bfd (core_bfd);
346b725ae77Skettenis 
347b725ae77Skettenis   /* Find a suitable core file handler to munch on core_bfd */
348b725ae77Skettenis   core_vec = sniff_core_bfd (core_bfd);
349e93f7393Sniklas 
350e93f7393Sniklas   validate_files ();
351e93f7393Sniklas 
352e93f7393Sniklas   /* Find the data section */
353e93f7393Sniklas   if (build_section_table (core_bfd, &core_ops.to_sections,
354e93f7393Sniklas 			   &core_ops.to_sections_end))
355e93f7393Sniklas     error ("\"%s\": Can't find sections: %s",
356e93f7393Sniklas 	   bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
357e93f7393Sniklas 
358b725ae77Skettenis   /* If we have no exec file, try to set the architecture from the
359b725ae77Skettenis      core file.  We don't do this unconditionally since an exec file
360b725ae77Skettenis      typically contains more information that helps us determine the
361b725ae77Skettenis      architecture than a core file.  */
362b725ae77Skettenis   if (!exec_bfd)
363b725ae77Skettenis     set_gdbarch_from_file (core_bfd);
364b725ae77Skettenis 
365e93f7393Sniklas   ontop = !push_target (&core_ops);
366e93f7393Sniklas   discard_cleanups (old_chain);
367e93f7393Sniklas 
368*63addd46Skettenis   /* This is done first, before anything has a chance to query the
369*63addd46Skettenis      inferior for information such as symbols.  */
370*63addd46Skettenis   observer_notify_inferior_created (&core_ops, from_tty);
371*63addd46Skettenis 
372e93f7393Sniklas   p = bfd_core_file_failing_command (core_bfd);
373e93f7393Sniklas   if (p)
374e93f7393Sniklas     printf_filtered ("Core was generated by `%s'.\n", p);
375e93f7393Sniklas 
376e93f7393Sniklas   siggy = bfd_core_file_failing_signal (core_bfd);
377e93f7393Sniklas   if (siggy > 0)
378b725ae77Skettenis     /* NOTE: target_signal_from_host() converts a target signal value
379b725ae77Skettenis        into gdb's internal signal value.  Unfortunately gdb's internal
380b725ae77Skettenis        value is called ``target_signal'' and this function got the
381b725ae77Skettenis        name ..._from_host(). */
382e93f7393Sniklas     printf_filtered ("Program terminated with signal %d, %s.\n", siggy,
383b725ae77Skettenis 		     target_signal_to_string (target_signal_from_host (siggy)));
384e93f7393Sniklas 
385e93f7393Sniklas   /* Build up thread list from BFD sections. */
386e93f7393Sniklas 
387e93f7393Sniklas   init_thread_list ();
388e93f7393Sniklas   bfd_map_over_sections (core_bfd, add_to_thread_list,
389e93f7393Sniklas 			 bfd_get_section_by_name (core_bfd, ".reg"));
390e93f7393Sniklas 
391e93f7393Sniklas   if (ontop)
392e93f7393Sniklas     {
393e93f7393Sniklas       /* Fetch all registers from core file.  */
394e93f7393Sniklas       target_fetch_registers (-1);
395e93f7393Sniklas 
396e93f7393Sniklas       /* Add symbols and section mappings for any shared libraries.  */
397e93f7393Sniklas #ifdef SOLIB_ADD
398e93f7393Sniklas       catch_errors (solib_add_stub, &from_tty, (char *) 0,
399e93f7393Sniklas 		    RETURN_MASK_ALL);
400e93f7393Sniklas #endif
401e93f7393Sniklas 
402e93f7393Sniklas       /* Now, set up the frame cache, and print the top of stack.  */
403e93f7393Sniklas       flush_cached_frames ();
404b725ae77Skettenis       select_frame (get_current_frame ());
405*63addd46Skettenis       print_stack_frame (get_selected_frame (), 1, SRC_AND_LOC);
406e93f7393Sniklas     }
407e93f7393Sniklas   else
408e93f7393Sniklas     {
409e93f7393Sniklas       warning (
410e93f7393Sniklas 		"you won't be able to access this core file until you terminate\n\
411e93f7393Sniklas your %s; do ``info files''", target_longname);
412e93f7393Sniklas     }
413e93f7393Sniklas }
414e93f7393Sniklas 
415e93f7393Sniklas static void
core_detach(char * args,int from_tty)416b725ae77Skettenis core_detach (char *args, int from_tty)
417e93f7393Sniklas {
418e93f7393Sniklas   if (args)
419e93f7393Sniklas     error ("Too many arguments");
420e93f7393Sniklas   unpush_target (&core_ops);
421e93f7393Sniklas   reinit_frame_cache ();
422e93f7393Sniklas   if (from_tty)
423e93f7393Sniklas     printf_filtered ("No core file now.\n");
424e93f7393Sniklas }
425e93f7393Sniklas 
426b725ae77Skettenis 
427b725ae77Skettenis /* Try to retrieve registers from a section in core_bfd, and supply
428b725ae77Skettenis    them to core_vec->core_read_registers, as the register set numbered
429b725ae77Skettenis    WHICH.
430b725ae77Skettenis 
431b725ae77Skettenis    If inferior_ptid is zero, do the single-threaded thing: look for a
432b725ae77Skettenis    section named NAME.  If inferior_ptid is non-zero, do the
433b725ae77Skettenis    multi-threaded thing: look for a section named "NAME/PID", where
434b725ae77Skettenis    PID is the shortest ASCII decimal representation of inferior_ptid.
435b725ae77Skettenis 
436b725ae77Skettenis    HUMAN_NAME is a human-readable name for the kind of registers the
437b725ae77Skettenis    NAME section contains, for use in error messages.
438b725ae77Skettenis 
439b725ae77Skettenis    If REQUIRED is non-zero, print an error if the core file doesn't
440b725ae77Skettenis    have a section by the appropriate name.  Otherwise, just do nothing.  */
441b725ae77Skettenis 
442b725ae77Skettenis static void
get_core_register_section(char * name,int which,char * human_name,int required)443b725ae77Skettenis get_core_register_section (char *name,
444b725ae77Skettenis 			   int which,
445b725ae77Skettenis 			   char *human_name,
446b725ae77Skettenis 			   int required)
447b725ae77Skettenis {
448b725ae77Skettenis   char section_name[100];
449b725ae77Skettenis   struct bfd_section *section;
450b725ae77Skettenis   bfd_size_type size;
451b725ae77Skettenis   char *contents;
452b725ae77Skettenis 
453b725ae77Skettenis   if (PIDGET (inferior_ptid))
454b725ae77Skettenis     sprintf (section_name, "%s/%d", name, PIDGET (inferior_ptid));
455b725ae77Skettenis   else
456b725ae77Skettenis     strcpy (section_name, name);
457b725ae77Skettenis 
458b725ae77Skettenis   section = bfd_get_section_by_name (core_bfd, section_name);
459b725ae77Skettenis   if (! section)
460b725ae77Skettenis     {
461b725ae77Skettenis       if (required)
462b725ae77Skettenis 	warning ("Couldn't find %s registers in core file.\n", human_name);
463b725ae77Skettenis       return;
464b725ae77Skettenis     }
465b725ae77Skettenis 
466b725ae77Skettenis   size = bfd_section_size (core_bfd, section);
467b725ae77Skettenis   contents = alloca (size);
468b725ae77Skettenis   if (! bfd_get_section_contents (core_bfd, section, contents,
469b725ae77Skettenis 				  (file_ptr) 0, size))
470b725ae77Skettenis     {
471b725ae77Skettenis       warning ("Couldn't read %s registers from `%s' section in core file.\n",
472b725ae77Skettenis 	       human_name, name);
473b725ae77Skettenis       return;
474b725ae77Skettenis     }
475b725ae77Skettenis 
476b725ae77Skettenis   if (core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
477b725ae77Skettenis     {
478b725ae77Skettenis       const struct regset *regset;
479b725ae77Skettenis 
480b725ae77Skettenis       regset = gdbarch_regset_from_core_section (core_gdbarch, name, size);
481b725ae77Skettenis       if (regset == NULL)
482b725ae77Skettenis 	{
483b725ae77Skettenis 	  if (required)
484b725ae77Skettenis 	    warning ("Couldn't recognize %s registers in core file.\n",
485b725ae77Skettenis 		     human_name);
486b725ae77Skettenis 	  return;
487b725ae77Skettenis 	}
488b725ae77Skettenis 
489b725ae77Skettenis       regset->supply_regset (regset, current_regcache, -1, contents, size);
490b725ae77Skettenis       return;
491b725ae77Skettenis     }
492b725ae77Skettenis 
493b725ae77Skettenis   gdb_assert (core_vec);
494b725ae77Skettenis   core_vec->core_read_registers (contents, size, which,
495b725ae77Skettenis 				 ((CORE_ADDR)
496b725ae77Skettenis 				  bfd_section_vma (core_bfd, section)));
497b725ae77Skettenis }
498b725ae77Skettenis 
499b725ae77Skettenis 
500e93f7393Sniklas /* Get the registers out of a core file.  This is the machine-
501e93f7393Sniklas    independent part.  Fetch_core_registers is the machine-dependent
502e93f7393Sniklas    part, typically implemented in the xm-file for each architecture.  */
503e93f7393Sniklas 
504e93f7393Sniklas /* We just get all the registers, so we don't use regno.  */
505e93f7393Sniklas 
506e93f7393Sniklas static void
get_core_registers(int regno)507b725ae77Skettenis get_core_registers (int regno)
508e93f7393Sniklas {
509b725ae77Skettenis   int status;
510e93f7393Sniklas 
511b725ae77Skettenis   if (!(core_gdbarch && gdbarch_regset_from_core_section_p (core_gdbarch))
512b725ae77Skettenis       && (core_vec == NULL || core_vec->core_read_registers == NULL))
513e93f7393Sniklas     {
514e93f7393Sniklas       fprintf_filtered (gdb_stderr,
515e93f7393Sniklas 		     "Can't fetch registers from this type of core file\n");
516e93f7393Sniklas       return;
517e93f7393Sniklas     }
518e93f7393Sniklas 
519b725ae77Skettenis   get_core_register_section (".reg", 0, "general-purpose", 1);
520b725ae77Skettenis   get_core_register_section (".reg2", 2, "floating-point", 0);
521b725ae77Skettenis   get_core_register_section (".reg-xfp", 3, "extended floating-point", 0);
522e93f7393Sniklas 
523b725ae77Skettenis   deprecated_registers_fetched ();
524e93f7393Sniklas }
525e93f7393Sniklas 
526e93f7393Sniklas static void
core_files_info(struct target_ops * t)527b725ae77Skettenis core_files_info (struct target_ops *t)
528e93f7393Sniklas {
529e93f7393Sniklas   print_section_info (t, core_bfd);
530e93f7393Sniklas }
531e93f7393Sniklas 
532b725ae77Skettenis static LONGEST
core_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,void * readbuf,const void * writebuf,ULONGEST offset,LONGEST len)533b725ae77Skettenis core_xfer_partial (struct target_ops *ops, enum target_object object,
534b725ae77Skettenis 		   const char *annex, void *readbuf,
535b725ae77Skettenis 		   const void *writebuf, ULONGEST offset, LONGEST len)
536b725ae77Skettenis {
537b725ae77Skettenis   switch (object)
538b725ae77Skettenis     {
539b725ae77Skettenis     case TARGET_OBJECT_MEMORY:
540b725ae77Skettenis       if (readbuf)
541*63addd46Skettenis 	return (*ops->deprecated_xfer_memory) (offset, readbuf, len,
542*63addd46Skettenis 					       0/*write*/, NULL, ops);
543b725ae77Skettenis       if (writebuf)
544*63addd46Skettenis 	return (*ops->deprecated_xfer_memory) (offset, readbuf, len,
545*63addd46Skettenis 					       1/*write*/, NULL, ops);
546b725ae77Skettenis       return -1;
547b725ae77Skettenis 
548b725ae77Skettenis     case TARGET_OBJECT_AUXV:
549b725ae77Skettenis       if (readbuf)
550b725ae77Skettenis 	{
551b725ae77Skettenis 	  /* When the aux vector is stored in core file, BFD
552b725ae77Skettenis 	     represents this with a fake section called ".auxv".  */
553b725ae77Skettenis 
554b725ae77Skettenis 	  struct bfd_section *section;
555b725ae77Skettenis 	  bfd_size_type size;
556b725ae77Skettenis 	  char *contents;
557b725ae77Skettenis 
558b725ae77Skettenis 	  section = bfd_get_section_by_name (core_bfd, ".auxv");
559b725ae77Skettenis 	  if (section == NULL)
560b725ae77Skettenis 	    return -1;
561b725ae77Skettenis 
562b725ae77Skettenis 	  size = bfd_section_size (core_bfd, section);
563b725ae77Skettenis 	  if (offset >= size)
564b725ae77Skettenis 	    return 0;
565b725ae77Skettenis 	  size -= offset;
566b725ae77Skettenis 	  if (size > len)
567b725ae77Skettenis 	    size = len;
568*63addd46Skettenis 	  if (size > 0
569*63addd46Skettenis 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
570b725ae77Skettenis 					    (file_ptr) offset, size))
571b725ae77Skettenis 	    {
572b725ae77Skettenis 	      warning ("Couldn't read NT_AUXV note in core file.");
573b725ae77Skettenis 	      return -1;
574b725ae77Skettenis 	    }
575b725ae77Skettenis 
576b725ae77Skettenis 	  return size;
577b725ae77Skettenis 	}
578b725ae77Skettenis       return -1;
579b725ae77Skettenis 
58052ba75afSkettenis     case TARGET_OBJECT_WCOOKIE:
58152ba75afSkettenis       if (readbuf)
58252ba75afSkettenis 	{
58352ba75afSkettenis 	  /* When the StackGhost cookie is stored in core file, BFD
58452ba75afSkettenis 	     represents this with a fake section called ".wcookie".  */
58552ba75afSkettenis 
58652ba75afSkettenis 	  struct bfd_section *section;
58752ba75afSkettenis 	  bfd_size_type size;
58852ba75afSkettenis 	  char *contents;
58952ba75afSkettenis 
59052ba75afSkettenis 	  section = bfd_get_section_by_name (core_bfd, ".wcookie");
59152ba75afSkettenis 	  if (section == NULL)
59252ba75afSkettenis 	    return -1;
59352ba75afSkettenis 
59452ba75afSkettenis 	  size = bfd_section_size (core_bfd, section);
59552ba75afSkettenis 	  if (offset >= size)
59652ba75afSkettenis 	    return 0;
59752ba75afSkettenis 	  size -= offset;
59852ba75afSkettenis 	  if (size > len)
59952ba75afSkettenis 	    size = len;
60052ba75afSkettenis 	  if (size > 0
60152ba75afSkettenis 	      && !bfd_get_section_contents (core_bfd, section, readbuf,
60252ba75afSkettenis 					    (file_ptr) offset, size))
60352ba75afSkettenis 	    {
60452ba75afSkettenis 	      warning ("Couldn't read StackGhost cookie in core file.");
60552ba75afSkettenis 	      return -1;
60652ba75afSkettenis 	    }
60752ba75afSkettenis 
60852ba75afSkettenis 	  return size;
60952ba75afSkettenis 	}
61052ba75afSkettenis       return -1;
61152ba75afSkettenis 
612b725ae77Skettenis     default:
613b725ae77Skettenis       if (ops->beneath != NULL)
614b725ae77Skettenis 	return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
615b725ae77Skettenis 					      readbuf, writebuf, offset, len);
616b725ae77Skettenis       return -1;
617b725ae77Skettenis     }
618b725ae77Skettenis }
619b725ae77Skettenis 
620b725ae77Skettenis 
621e93f7393Sniklas /* If mourn is being called in all the right places, this could be say
622e93f7393Sniklas    `gdb internal error' (since generic_mourn calls breakpoint_init_inferior).  */
623e93f7393Sniklas 
624e93f7393Sniklas static int
ignore(CORE_ADDR addr,char * contents)625b725ae77Skettenis ignore (CORE_ADDR addr, char *contents)
626e93f7393Sniklas {
627e93f7393Sniklas   return 0;
628e93f7393Sniklas }
629e93f7393Sniklas 
630b725ae77Skettenis 
631b725ae77Skettenis /* Okay, let's be honest: threads gleaned from a core file aren't
632b725ae77Skettenis    exactly lively, are they?  On the other hand, if we don't claim
633b725ae77Skettenis    that each & every one is alive, then we don't get any of them
634b725ae77Skettenis    to appear in an "info thread" command, which is quite a useful
635b725ae77Skettenis    behaviour.
636b725ae77Skettenis  */
637b725ae77Skettenis static int
core_file_thread_alive(ptid_t tid)638b725ae77Skettenis core_file_thread_alive (ptid_t tid)
639b725ae77Skettenis {
640b725ae77Skettenis   return 1;
641b725ae77Skettenis }
642b725ae77Skettenis 
643b725ae77Skettenis /* Fill in core_ops with its defined operations and properties.  */
644b725ae77Skettenis 
645b725ae77Skettenis static void
init_core_ops(void)646b725ae77Skettenis init_core_ops (void)
647b725ae77Skettenis {
648b725ae77Skettenis   core_ops.to_shortname = "core";
649b725ae77Skettenis   core_ops.to_longname = "Local core dump file";
650b725ae77Skettenis   core_ops.to_doc =
651b725ae77Skettenis     "Use a core file as a target.  Specify the filename of the core file.";
652b725ae77Skettenis   core_ops.to_open = core_open;
653b725ae77Skettenis   core_ops.to_close = core_close;
654b725ae77Skettenis   core_ops.to_attach = find_default_attach;
655b725ae77Skettenis   core_ops.to_detach = core_detach;
656b725ae77Skettenis   core_ops.to_fetch_registers = get_core_registers;
657b725ae77Skettenis   core_ops.to_xfer_partial = core_xfer_partial;
658*63addd46Skettenis   core_ops.deprecated_xfer_memory = xfer_memory;
659b725ae77Skettenis   core_ops.to_files_info = core_files_info;
660b725ae77Skettenis   core_ops.to_insert_breakpoint = ignore;
661b725ae77Skettenis   core_ops.to_remove_breakpoint = ignore;
662b725ae77Skettenis   core_ops.to_create_inferior = find_default_create_inferior;
663b725ae77Skettenis   core_ops.to_thread_alive = core_file_thread_alive;
664b725ae77Skettenis   core_ops.to_stratum = core_stratum;
665b725ae77Skettenis   core_ops.to_has_memory = 1;
666b725ae77Skettenis   core_ops.to_has_stack = 1;
667b725ae77Skettenis   core_ops.to_has_registers = 1;
668b725ae77Skettenis   core_ops.to_magic = OPS_MAGIC;
669b725ae77Skettenis }
670b725ae77Skettenis 
671b725ae77Skettenis /* non-zero if we should not do the add_target call in
672b725ae77Skettenis    _initialize_corelow; not initialized (i.e., bss) so that
673b725ae77Skettenis    the target can initialize it (i.e., data) if appropriate.
674b725ae77Skettenis    This needs to be set at compile time because we don't know
675b725ae77Skettenis    for sure whether the target's initialize routine is called
676b725ae77Skettenis    before us or after us. */
677b725ae77Skettenis int coreops_suppress_target;
678e93f7393Sniklas 
679e93f7393Sniklas void
_initialize_corelow(void)680b725ae77Skettenis _initialize_corelow (void)
681e93f7393Sniklas {
682b725ae77Skettenis   init_core_ops ();
683b725ae77Skettenis 
684b725ae77Skettenis   if (!coreops_suppress_target)
685e93f7393Sniklas     add_target (&core_ops);
686e93f7393Sniklas }
687