/* Work with core dump and executable files, for GDB.
- Copyright (C) 1986, 1987 Free Software Foundation, Inc.
-
-GDB is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY. No author or distributor accepts responsibility to anyone
-for the consequences of using it or for whether it serves any
-particular purpose or works at all, unless he says so in writing.
-Refer to the GDB General Public License for full details.
-
-Everyone is granted permission to copy, modify and redistribute GDB,
-but only under the conditions described in the GDB General Public
-License. A copy of this license is supposed to have been given to you
-along with GDB so you can know your rights and responsibilities. It
-should be in a file named COPYING. Among other things, the copyright
-notice and this notice must be preserved on all copies.
-
-In other words, go ahead and share GDB, but don't try to stop
-anyone else from sharing it farther. Help stamp out software hoarding!
-*/
-
-#include "initialize.h"
-#include "defs.h"
-#include "param.h"
+ Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
-#include <a.out.h>
-#include <stdio.h>
-#include <signal.h>
-#include <sys/param.h>
-#include <sys/dir.h>
-#include <sys/file.h>
-#include <sys/stat.h>
-
-/* Recognize COFF format systems because a.out.h defines AOUTHDR. */
-#ifdef AOUTHDR
-#define COFF_FORMAT
-#endif
+This file is part of GDB.
-#ifdef NEW_SUN_CORE
-#include <sys/core.h>
-#else /* not NEW_SUN_CORE */
-#ifdef UMAX_CORE
-#include <sys/ptrace.h>
-#else /* not UMAX_CORE */
-#include <sys/user.h>
-#ifdef HP9K320
-#include <sys/reg.h>
-#include <sys/trap.h>
-#ifdef HPUX_VERSION_5
-#define e_PS e_regs[PS]
-#define e_PC e_regs[PC]
-#endif /* HPUX_VERSION_5 */
-#endif /* HP9K320 */
-#endif /* not UMAX_CORE */
-#endif /* not NEW_SUN_CORE */
-
-#ifndef N_TXTADDR
-#define N_TXTADDR(hdr) 0
-#endif /* no N_TXTADDR */
-
-#ifndef N_DATADDR
-#define N_DATADDR(hdr) hdr.a_text
-#endif /* no N_DATADDR */
-
-/* Make COFF and non-COFF names for things a little more compatible
- to reduce conditionals later. */
-
-#ifdef COFF_FORMAT
-#define a_magic magic
-#endif
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
-#ifndef COFF_FORMAT
-#define AOUTHDR struct exec
-#endif
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+
+#include <stdio.h>
+#include <errno.h>
+#include <signal.h>
+#include "defs.h"
+#include "param.h"
+#include "frame.h" /* required by inferior.h */
+#include "inferior.h"
+#include "symtab.h"
+#include "command.h"
+#include "bfd.h"
+#include "target.h"
+#include "gdbcore.h"
+
+extern int xfer_memory ();
+extern void child_attach (), child_create_inferior ();
+
+extern int sys_nerr;
+extern char *sys_errlist[];
extern char *sys_siglist[];
-START_FILE
+extern char registers[];
/* Hook for `exec_file_command' command to call. */
-void (*exec_file_display_hook) ();
-
-/* File names of core file and executable file. */
-
-static char *corefile;
-static char *execfile;
-
-/* Descriptors on which core file and executable file are open.
- Note that the execchan is closed when an inferior is created
- and reopened if the inferior dies or is killed. */
-
-static int corechan;
-static int execchan;
-
-/* Last modification time of executable file.
- Also used in source.c to compare against mtime of a source file. */
-
-int exec_mtime;
+void (*exec_file_display_hook) () = NULL;
-/* Virtual addresses of bounds of the two areas of memory in the core file. */
+struct section_table *core_sections, *core_sections_end;
-static CORE_ADDR data_start;
-static CORE_ADDR data_end;
-static CORE_ADDR stack_start;
-static CORE_ADDR stack_end;
+/* Binary file diddling handle for the core file. */
-/* Virtual addresses of bounds of two areas of memory in the exec file.
- Note that the data area in the exec file is used only when there is no core file. */
+bfd *core_bfd = NULL;
-CORE_ADDR text_start;
-CORE_ADDR text_end;
+/* Forward decl */
+extern struct target_ops core_ops;
-static CORE_ADDR exec_data_start;
-static CORE_ADDR exec_data_end;
-
-/* Address in executable file of start of text area data. */
-
-static int text_offset;
-
-/* Address in executable file of start of data area data. */
-
-static int exec_data_offset;
-
-/* Address in core file of start of data area data. */
-
-static int data_offset;
-
-/* Address in core file of start of stack area data. */
-
-static int stack_offset;
-
-#ifdef COFF_FORMAT
-/* various coff data structures */
-
-static FILHDR file_hdr;
-static SCNHDR text_hdr;
-static SCNHDR data_hdr;
-
-#endif /* not COFF_FORMAT */
-
-/* a.out header saved in core file. */
-
-static AOUTHDR core_aouthdr;
+\f
+/* Discard all vestiges of any previous core file
+ and mark data and stack spaces as empty. */
-/* a.out header of exec file. */
+/* ARGSUSED */
+void
+core_close (quitting)
+ int quitting;
+{
+ if (core_bfd) {
+ free (bfd_get_filename (core_bfd));
+ bfd_close (core_bfd);
+ core_bfd = NULL;
+#ifdef CLEAR_SOLIB
+ CLEAR_SOLIB ();
+#endif
+ }
+}
-static AOUTHDR exec_aouthdr;
+/* This routine opens and sets up the core file bfd */
-static void validate_files ();
-unsigned int register_addr ();
-\f
-core_file_command (filename, from_tty)
+void
+core_open (filename, from_tty)
char *filename;
int from_tty;
{
- int val;
- extern char registers[];
-
- /* Discard all vestiges of any previous core file
- and mark data and stack spaces as empty. */
-
- if (corefile)
- free (corefile);
- corefile = 0;
-
- if (corechan >= 0)
- close (corechan);
- corechan = -1;
-
- data_start = 0;
- data_end = 0;
- stack_start = STACK_END_ADDR;
- stack_end = STACK_END_ADDR;
+ char *p;
+ int siggy;
+ struct cleanup *old_chain;
+ char *temp;
+ bfd *temp_bfd;
+ int ontop;
+
+ target_preopen (from_tty);
+ if (!filename)
+ {
+ error (core_bfd?
+ "No core file specified. (Use `detach' to stop debugging a core file.)"
+ : "No core file specified.");
+ }
- /* Now, if a new core file was specified, open it and digest it. */
+ filename = tilde_expand (filename);
+ if (filename[0] != '/') {
+ temp = concat (current_directory, "/", filename);
+ free (filename);
+ filename = temp;
+ }
- if (filename)
+ old_chain = make_cleanup (free, filename);
+ temp_bfd = bfd_openr (filename, NULL);
+ if (temp_bfd == NULL)
{
- if (have_inferior_p ())
- error ("To look at a core file, you must kill the inferior with \"kill\".");
- corechan = open (filename, O_RDONLY, 0);
- if (corechan < 0)
- perror_with_name (filename);
-#ifdef NEW_SUN_CORE
- {
- struct core corestr;
-
- val = myread (corechan, &corestr, sizeof corestr);
- if (val < 0)
- perror_with_name (filename);
- if (corestr.c_magic != CORE_MAGIC)
- error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
- filename, corestr.c_magic, (int) CORE_MAGIC);
- else if (sizeof (struct core) != corestr.c_len)
- error ("\"%s\" has an invalid struct core length (%d, expected %d)",
- filename, corestr.c_len, (int) sizeof (struct core));
-
- data_start = exec_data_start;
- data_end = data_start + corestr.c_dsize;
- stack_start = stack_end - corestr.c_ssize;
- data_offset = sizeof corestr;
- stack_offset = sizeof corestr + corestr.c_dsize;
-
- bcopy (&corestr.c_regs, registers, 16 * 4);
- *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = corestr.c_regs.r_ps;
- *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = corestr.c_regs.r_pc;
- bcopy (corestr.c_fpstatus.fps_regs,
- ®isters[REGISTER_BYTE (FP0_REGNUM)],
- sizeof corestr.c_fpstatus.fps_regs);
- bcopy (&corestr.c_fpstatus.fps_control,
- ®isters[REGISTER_BYTE (FPC_REGNUM)],
- sizeof corestr.c_fpstatus - sizeof corestr.c_fpstatus.fps_regs);
-
- bcopy (&corestr.c_aouthdr, &core_aouthdr, sizeof (struct exec));
-
- printf ("Core file is from \"%s\".\n", corestr.c_cmdname);
- if (corestr.c_signo > 0)
- printf ("Program terminated with signal %d, %s.\n",
- corestr.c_signo,
- corestr.c_signo < NSIG
- ? sys_siglist[corestr.c_signo]
- : "(undocumented)");
- }
-#else /* not NEW_SUN_CORE */
- /* 4.2-style (and perhaps also sysV-style) core dump file. */
- {
-#ifdef UMAX_CORE
- struct ptrace_user u;
-#else
- struct user u;
-#endif
- int reg_offset;
-
- val = myread (corechan, &u, sizeof u);
- if (val < 0)
- perror_with_name (filename);
- data_start = exec_data_start;
-
-#ifdef UMAX_CORE
- data_end = data_start + u.pt_dsize;
- stack_start = stack_end - u.pt_ssize;
- data_offset = sizeof u;
- stack_offset = data_offset + u.pt_dsize;
- reg_offset = 0;
-
- bcopy (&u.pt_aouthdr, &core_aouthdr, sizeof (AOUTHDR));
- printf ("Core file is from \"%s\".\n", u.pt_comm);
- if (u.pt_signal > 0)
- printf ("Program terminated with signal %d, %s.\n",
- u.pt_signal,
- u.pt_signal < NSIG
- ? sys_siglist[u.pt_signal]
- : "(undocumented)");
-#else /* not UMAX_CORE */
- data_end = data_start + NBPG * u.u_dsize;
- stack_start = stack_end - NBPG * u.u_ssize;
- data_offset = NBPG * UPAGES;
- stack_offset = NBPG * (UPAGES + u.u_dsize);
- reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
-
- /* I don't know where to find this info.
- So, for now, mark it as not available. */
- core_aouthdr.a_magic = 0;
-#endif /* not UMAX_CORE */
-
- /* Read the register values out of the core file and store
- them where `read_register' will find them. */
-
-#ifdef HP9K320
- {
- register int regno;
- struct exception_stack es;
- int val;
-
- val = lseek (corechan, (REGISTER_ADDR (reg_offset, 0)), 0);
- if (val < 0)
- perror_with_name (filename);
- val = myread (corechan, es,
- ((char *) &es.e_regs[R0] - (char *) &es.e_offset));
- if (val < 0)
- perror_with_name (filename);
- for (regno = 0; (regno < PS_REGNUM); regno++)
- supply_register (regno, &es.e_regs[regno + R0]);
- val = es.e_PS;
- supply_register (regno++, &val);
- supply_register (regno++, &es.e_PC);
- for (; (regno < NUM_REGS); regno++)
- {
- char buf[MAX_REGISTER_RAW_SIZE];
-
- val = lseek (corechan, (FP_REGISTER_ADDR (u, regno)), 0);
- if (val < 0)
- perror_with_name (filename);
-
- val = myread (corechan, buf, sizeof buf);
- if (val < 0)
- perror_with_name (filename);
- supply_register (regno, buf);
- }
- }
-#else /* not HP9K320 */
- {
- register int regno;
-
- for (regno = 0; regno < NUM_REGS; regno++)
- {
- char buf[MAX_REGISTER_RAW_SIZE];
-
- val = lseek (corechan, register_addr (regno, reg_offset), 0);
- if (val < 0)
- perror_with_name (filename);
-
- val = myread (corechan, buf, sizeof buf);
- if (val < 0)
- perror_with_name (filename);
- supply_register (regno, buf);
- }
- }
-#endif /* not HP9K320 */
- }
-#endif /* not NEW_SUN_CORE */
- if (filename[0] == '/')
- corefile = savestring (filename, strlen (filename));
- else
- {
- corefile = concat (current_directory, "/", filename);
- }
-
- set_current_frame (read_register (FP_REGNUM));
- select_frame (get_current_frame (), 0);
- validate_files ();
+ perror_with_name (filename);
}
- else if (from_tty)
- printf ("No core file now.\n");
-}
-\f
-exec_file_command (filename, from_tty)
- char *filename;
- int from_tty;
-{
- int val;
-
- /* Eliminate all traces of old exec file.
- Mark text segment as empty. */
-
- if (execfile)
- free (execfile);
- execfile = 0;
- data_start = 0;
- data_end -= exec_data_start;
- text_start = 0;
- text_end = 0;
- exec_data_start = 0;
- exec_data_end = 0;
- if (execchan >= 0)
- close (execchan);
- execchan = -1;
-
- /* Now open and digest the file the user requested, if any. */
-
- if (filename)
- {
- execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
- &execfile);
- if (execchan < 0)
- perror_with_name (filename);
-#ifdef COFF_FORMAT
- {
- int aout_hdrsize;
- int num_sections;
+ if (!bfd_check_format (temp_bfd, bfd_core))
+ {
+ bfd_close (temp_bfd);
+ error ("\"%s\" does not appear to be a core dump", filename);
+ }
- if (read_file_hdr (execchan, &file_hdr) < 0)
- error ("\"%s\": not in executable format.", execfile);
+ /* Looks semi-reasonable. Toss the old core file and work on the new. */
- aout_hdrsize = file_hdr.f_opthdr;
- num_sections = file_hdr.f_nscns;
+ discard_cleanups (old_chain); /* Don't free filename any more */
+ unpush_target (&core_ops);
+ core_bfd = temp_bfd;
+ old_chain = make_cleanup (core_close, core_bfd);
- if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
- error ("\"%s\": can't read optional aouthdr", execfile);
+ validate_files ();
- if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections) < 0)
- error ("\"%s\": can't read text section header", execfile);
+ /* Find the data section */
+ if (build_section_table (core_bfd, &core_sections, &core_sections_end))
+ error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
+ bfd_errmsg (bfd_error));
- if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections) < 0)
- error ("\"%s\": can't read data section header", execfile);
+ ontop = !push_target (&core_ops);
+ make_cleanup (unpush_target, &core_ops);
- text_start = exec_aouthdr.text_start;
- text_end = text_start + exec_aouthdr.tsize;
- text_offset = text_hdr.s_scnptr;
- exec_data_start = exec_aouthdr.data_start;
- exec_data_end = exec_data_start + exec_aouthdr.dsize;
- exec_data_offset = data_hdr.s_scnptr;
- data_start = exec_data_start;
- data_end += exec_data_start;
- exec_mtime = file_hdr.f_timdat;
- }
-#else /* not COFF_FORMAT */
- {
- struct stat st_exec;
+ p = bfd_core_file_failing_command (core_bfd);
+ if (p)
+ printf ("Core file invoked as `%s'.\n", p);
-#ifdef gould
- FILHDR exec_coffhdr;
+ siggy = bfd_core_file_failing_signal (core_bfd);
+ if (siggy > 0)
+ printf ("Program terminated with signal %d, %s.\n", siggy,
+ siggy < NSIG ? sys_siglist[siggy] : "(undocumented)");
- val = myread (execchan, &exec_coffhdr, sizeof exec_coffhdr);
- if (val < 0)
- perror_with_name (filename);
-#endif
- val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
-
- if (val < 0)
- perror_with_name (filename);
-
- text_start = N_TXTADDR (exec_aouthdr);
- exec_data_start = N_DATADDR (exec_aouthdr);
-#ifdef gould
- text_offset = N_TXTOFF (exec_coffhdr, exec_aouthdr);
- exec_data_offset = N_TXTOFF (exec_coffhdr, exec_aouthdr)
- + exec_aouthdr.a_text;
-#else
- text_offset = N_TXTOFF (exec_aouthdr);
- exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
+ if (ontop) {
+ /* Fetch all registers from core file */
+ target_fetch_registers (-1);
+ /* Add symbols for any shared libraries that were in use */
+#ifdef SOLIB_ADD
+ SOLIB_ADD (NULL, from_tty);
#endif
- text_end = text_start + exec_aouthdr.a_text;
- exec_data_end = exec_data_start + exec_aouthdr.a_data;
- data_start = exec_data_start;
- data_end += exec_data_start;
+ /* Now, set up the frame cache, and print the top of stack */
+ set_current_frame ( create_new_frame (read_register (FP_REGNUM),
+ read_pc ()));
+ select_frame (get_current_frame (), 0);
+ print_sel_frame (0); /* Print the top frame and source line */
+ } else {
+ printf (
+"Warning: you won't be able to access this core file until you terminate\n\
+your %s; do ``info files''\n", current_target->to_longname);
+ }
+
+ discard_cleanups (old_chain);
+}
- fstat (execchan, &st_exec);
- exec_mtime = st_exec.st_mtime;
- }
-#endif /* not COFF_FORMAT */
+void
+core_detach (args, from_tty)
+ char *args;
+ int from_tty;
+{
+ if (args)
+ error ("Too many arguments");
+ unpush_target (&core_ops);
+ if (from_tty)
+ printf ("No core file now.\n");
+}
- validate_files ();
- }
- else if (from_tty)
- printf ("No exec file now.\n");
+/* Backward compatability with old way of specifying core files. */
- /* Tell display code (if any) about the changed file name. */
- if (exec_file_display_hook)
- (*exec_file_display_hook) (filename);
+void
+core_file_command (filename, from_tty)
+ char *filename;
+ int from_tty;
+{
+ dont_repeat (); /* Either way, seems bogus. */
+ if (!filename)
+ core_detach (filename, from_tty);
+ else
+ core_open (filename, from_tty);
}
+\f
/* Call this to specify the hook for exec_file_command to call back.
This is called from the x-window display code. */
+void
specify_exec_file_hook (hook)
void (*hook) ();
{
If it is needed again after the inferior dies, it must
be reopened. */
+void
close_exec_file ()
{
- if (execchan >= 0)
- close (execchan);
- execchan = -1;
+#ifdef FIXME
+ if (exec_bfd)
+ bfd_tempclose (exec_bfd);
+#endif
}
+void
reopen_exec_file ()
{
- if (execchan < 0 && execfile != 0)
- {
- char *filename = concat (execfile, "", "");
- exec_file_command (filename, 0);
- free (filename);
- }
+#ifdef FIXME
+ if (exec_bfd)
+ bfd_reopen (exec_bfd);
+#endif
}
\f
/* If we have both a core file and an exec file,
This should really check that the core file came
from that exec file, but I don't know how to do it. */
-static void
+void
validate_files ()
{
- if (execfile != 0 && corefile != 0)
+ if (exec_bfd && core_bfd)
{
- struct stat st_core;
-
- fstat (corechan, &st_core);
-
- if (core_aouthdr.a_magic != 0
- && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
+ if (core_file_matches_executable_p (core_bfd, exec_bfd))
printf ("Warning: core file does not match specified executable file.\n");
- else if (exec_mtime > st_core.st_mtime)
+ else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
printf ("Warning: exec file is newer than core file.\n");
}
}
get_exec_file (err)
int err;
{
- if (err && execfile == 0)
- error ("No executable file specified.\n\
-Use the \"exec-file\" and \"symbol-file\" commands.");
- return execfile;
-}
+ if (exec_bfd) return bfd_get_filename(exec_bfd);
+ if (!err) return NULL;
-int
-have_core_file_p ()
-{
- return corefile != 0;
+ error ("No executable file specified.\n\
+Use the \"file\" or \"exec-file\" command.");
+ return NULL;
}
static void
-files_info ()
+core_files_info ()
{
- char *symfile;
- extern char *get_sym_file ();
+ struct section_table *p;
- if (execfile)
- printf ("Executable file \"%s\".\n", execfile);
- else
- printf ("No executable file\n");
- if (corefile == 0)
- printf ("No core dump file\n");
- else
- printf ("Core dump file \"%s\".\n", corefile);
-
- if (have_inferior_p ())
- printf ("Using the running image of the program, rather than these files.\n");
+ printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd));
- symfile = get_sym_file ();
- if (symfile != 0)
- printf ("Symbols loaded from \"%s\".\n", symfile);
+ for (p = core_sections; p < core_sections_end; p++)
+ printf("\tcore file from 0x%08x to 0x%08x is %s\n",
+ p->addr, p->endaddr,
+ bfd_section_name (core_bfd, p->sec_ptr));
+}
+\f
+void
+memory_error (status, memaddr)
+ int status;
+ CORE_ADDR memaddr;
+{
- if (! have_inferior_p ())
+ if (status == EIO)
+ {
+ /* Actually, address between memaddr and memaddr + len
+ was out of bounds. */
+ error ("Cannot access memory: address 0x%x out of bounds.", memaddr);
+ }
+ else
{
- if (execfile)
- {
- printf ("Text segment from 0x%x to 0x%x.\n",
- text_start, text_end);
- }
- if (corefile)
- {
- printf ("Data segment from 0x%x to 0x%x.\nStack segment from 0x%x to 0x%x.\n",
- data_start, data_end, stack_start, stack_end);
- }
+ if (status >= sys_nerr || status < 0)
+ error ("Error accessing memory address 0x%x: unknown error (%d).",
+ memaddr, status);
else
- {
- printf ("Data segment in executable from 0x%x to 0x%x.\n",
- exec_data_start, exec_data_end);
- }
+ error ("Error accessing memory address 0x%x: %s.",
+ memaddr, sys_errlist[status]);
}
}
-\f
-/* Read "memory data" from core file and/or executable file */
+/* Same as target_read_memory, but report an error if can't read. */
+void
read_memory (memaddr, myaddr, len)
CORE_ADDR memaddr;
char *myaddr;
int len;
{
- if (have_inferior_p ())
- read_inferior_memory (memaddr, myaddr, len);
- else
- xfer_core_file (memaddr, myaddr, len, 0);
+ int status;
+ status = target_read_memory (memaddr, myaddr, len);
+ if (status != 0)
+ memory_error (status, memaddr);
}
-/* Write LEN bytes of data starting at address MYADDR
- into debugged program memory at address MEMADDR.
- Returns zero if successful, or an errno value if ptrace failed. */
-
-int
+/* Same as target_write_memory, but report an error if can't write. */
+void
write_memory (memaddr, myaddr, len)
CORE_ADDR memaddr;
char *myaddr;
int len;
{
- if (have_inferior_p ())
- return write_inferior_memory (memaddr, myaddr, len);
- else
- error ("Can write memory only when program being debugged is running.");
+ int status;
+
+ status = target_write_memory (memaddr, myaddr, len);
+ if (status != 0)
+ memory_error (status, memaddr);
}
-xfer_core_file (memaddr, myaddr, len)
+/* Read an integer from debugged memory, given address and number of bytes. */
+
+long
+read_memory_integer (memaddr, len)
CORE_ADDR memaddr;
- char *myaddr;
int len;
{
- register int i;
- register int val;
- int xferchan;
- char **xferfile;
- int fileptr;
+ char cbuf;
+ short sbuf;
+ int ibuf;
+ long lbuf;
- while (len > 0)
+ if (len == sizeof (char))
{
- xferfile = 0;
- xferchan = 0;
-
- /* Determine which file the next bunch of addresses reside in,
- and where in the file. Set the file's read/write pointer
- to point at the proper place for the desired address
- and set xferfile and xferchan for the correct file.
- If desired address is nonexistent, leave them zero.
- i is set to the number of bytes that can be handled
- along with the next address. */
-
- if (memaddr < text_start)
- {
- i = min (len, text_start - memaddr);
- }
- else if (memaddr >= text_end && memaddr < data_start)
- {
- i = min (len, data_start - memaddr);
- }
- else if (memaddr >= (corechan >= 0 ? data_end : exec_data_end)
- && memaddr < stack_start)
- {
- i = min (len, stack_start - memaddr);
- }
- else if (memaddr >= stack_end && stack_end != 0)
- {
- i = min (len, - memaddr);
- }
- /* Note that if there is no core file
- data_start and data_end are equal. */
- else if (memaddr >= data_start && memaddr < data_end)
- {
- i = min (len, data_end - memaddr);
- fileptr = memaddr - data_start + data_offset;
- xferfile = &corefile;
- xferchan = corechan;
- }
- /* Note that if there is no core file
- stack_start and stack_end are equal. */
- else if (memaddr >= stack_start && memaddr < stack_end)
- {
- i = min (len, stack_end - memaddr);
- fileptr = memaddr - stack_start + stack_offset;
- xferfile = &corefile;
- xferchan = corechan;
- }
- else if (corechan < 0
- && memaddr >= exec_data_start && memaddr < exec_data_end)
- {
- i = min (len, exec_data_end - memaddr);
- fileptr = memaddr - exec_data_start + exec_data_offset;
- xferfile = &execfile;
- xferchan = execchan;
- }
- else if (memaddr >= text_start && memaddr < text_end)
- {
- i = min (len, text_end - memaddr);
- fileptr = memaddr - text_start + text_offset;
- xferfile = &execfile;
- xferchan = execchan;
- }
-
- /* Now we know which file to use.
- Set up its pointer and transfer the data. */
- if (xferfile)
- {
- if (*xferfile == 0)
- if (xferfile == &execfile)
- error ("No program file to examine.");
- else
- error ("No core dump file or running program to examine.");
- val = lseek (xferchan, fileptr, 0);
- if (val < 0)
- perror_with_name (*xferfile);
- val = myread (xferchan, myaddr, i);
- if (val < 0)
- perror_with_name (*xferfile);
- }
- /* If this address is for nonexistent memory,
- read zeros if reading, or do nothing if writing. */
- else
- bzero (myaddr, i);
-
- memaddr += i;
- myaddr += i;
- len -= i;
+ read_memory (memaddr, &cbuf, len);
+ return cbuf;
}
+ if (len == sizeof (short))
+ {
+ read_memory (memaddr, (char *)&sbuf, len);
+ SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
+ return sbuf;
+ }
+ if (len == sizeof (int))
+ {
+ read_memory (memaddr, (char *)&ibuf, len);
+ SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
+ return ibuf;
+ }
+ if (len == sizeof (lbuf))
+ {
+ read_memory (memaddr, (char *)&lbuf, len);
+ SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
+ return lbuf;
+ }
+ error ("Cannot handle integers of %d bytes.", len);
+ return -1; /* for lint */
}
\f
-/* My replacement for the read system call.
- Used like `read' but keeps going if `read' returns too soon. */
+/* Read or write the core file.
+
+ Args are address within core file, address within gdb address-space,
+ length, and a flag indicating whether to read or write.
-myread (desc, addr, len)
- int desc;
- char *addr;
+ Result is a length:
+
+ 0: We cannot handle this address and length.
+ > 0: We have handled N bytes starting at this address.
+ (If N == length, we did it all.) We might be able
+ to handle more bytes beyond this length, but no
+ promises.
+ < 0: We cannot handle this address, but if somebody
+ else handles (-N) bytes, we can start from there.
+
+ The actual work is done by xfer_memory in exec.c, which we share
+ in common with exec_xfer_memory(). */
+
+static int
+core_xfer_memory (memaddr, myaddr, len, write)
+ CORE_ADDR memaddr;
+ char *myaddr;
int len;
+ int write;
{
- register int val;
- int orglen = len;
-
- while (len > 0)
- {
- val = read (desc, addr, len);
- if (val < 0)
- return val;
- if (val == 0)
- return orglen - len;
- len -= val;
- addr += val;
- }
- return orglen;
+ int res;
+ res = xfer_memory (memaddr, myaddr, len, write,
+ core_bfd, core_sections, core_sections_end);
+#ifdef SOLIB_XFER_MEMORY
+ if (res == 0)
+ res = SOLIB_XFER_MEMORY (memaddr, myaddr, len, write);
+#endif
+ return res;
}
\f
-#ifdef REGISTER_U_ADDR
-
-/* Return the address in the core dump or inferior of register REGNO.
- BLOCKEND is the address of the end of the user structure. */
+/* Get the registers out of a core file. This is the machine-
+ independent part. Fetch_core_registers is the machine-dependent
+ part, typically implemented in the xm-file for each architecture. */
-unsigned int
-register_addr (regno, blockend)
+/* We just get all the registers, so we don't use regno. */
+/* ARGSUSED */
+static void
+get_core_registers (regno)
int regno;
- int blockend;
{
- int addr;
-
- if (regno < 0 || regno >= NUM_REGS)
- error ("Invalid register number %d.", regno);
-
- REGISTER_U_ADDR (addr, blockend, regno);
+ sec_ptr reg_sec;
+ unsigned size;
+ char *the_regs;
+
+ reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
+ size = bfd_section_size (core_bfd, reg_sec);
+ the_regs = alloca (size);
+ if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
+ (unsigned)0, size))
+ {
+ fetch_core_registers (the_regs, size, 0);
+ }
+ else
+ {
+ fprintf (stderr, "Couldn't fetch registers from core file: %s\n",
+ bfd_errmsg (bfd_error));
+ }
- return addr;
+ /* Now do it again for the float registers, if they exist. */
+ reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
+ if (reg_sec) {
+ size = bfd_section_size (core_bfd, reg_sec);
+ the_regs = alloca (size);
+ if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
+ (unsigned)0, size))
+ {
+ fetch_core_registers (the_regs, size, 2);
+ }
+ else
+ {
+ fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n",
+ bfd_errmsg (bfd_error));
+ }
+ }
+ registers_fetched();
}
-
-#endif /* REGISTER_U_ADDR */
\f
-static
-initialize ()
+struct target_ops core_ops = {
+ "core", "Local core dump file",
+ "Use a core file as a target. Specify the filename of the core file.",
+ core_open, core_close,
+ child_attach, core_detach, 0, 0, /* resume, wait */
+ get_core_registers,
+ 0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
+ core_xfer_memory, core_files_info,
+ 0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
+ 0, 0, 0, 0, 0, /* terminal stuff */
+ 0, 0, 0, 0, 0, /* kill, load, add_syms, call fn, lookup sym */
+ child_create_inferior, 0, /* mourn_inferior */
+ core_stratum, 0, /* next */
+ 0, 1, 1, 1, 0, /* all mem, mem, stack, regs, exec */
+ OPS_MAGIC, /* Always the last thing */
+};
+
+void
+_initialize_core()
{
- corechan = -1;
- execchan = -1;
- corefile = 0;
- execfile = 0;
- exec_file_display_hook = 0;
-
- text_start = 0;
- text_end = 0;
- data_start = 0;
- data_end = 0;
- exec_data_start = 0;
- exec_data_end = 0;
- stack_start = STACK_END_ADDR;
- stack_end = STACK_END_ADDR;
add_com ("core-file", class_files, core_file_command,
"Use FILE as core dump for examining memory and registers.\n\
-No arg means have no core file.");
- add_com ("exec-file", class_files, exec_file_command,
- "Use FILE as program for getting contents of pure memory.\n\
-If FILE cannot be found as specified, your execution directory path\n\
-is searched for a command of that name.\n\
-No arg means have no executable file.");
- add_info ("files", files_info, "Names of files being debugged.");
+No arg means have no core file. This command has been superseded by the\n\
+`target core' and `detach' commands.");
+ add_target (&core_ops);
}
-
-END_FILE