* gdb.texinfo (Continuing and Stepping): When talking about "step"
[deliverable/binutils-gdb.git] / gdb / core.c
index 03f4a682d86f62b4c6a936bef6ab4f12536a5c2e..134a9af5bd79a252e116459a89a26d9e5dbee439 100644 (file)
-/* Work with core dump and executable files, for GDB.
-   Copyright (C) 1986, 1987, 1989 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!
-*/
+/* Core dump and executable file functions above target vector, for GDB.
+   Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
 
-#include "defs.h"
-#include "param.h"
-#include "gdbcore.h"
+This file is part of GDB.
 
-#ifdef USG
-#include <sys/types.h>
-#include <fcntl.h>
-#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.
 
-#ifdef COFF_ENCAPSULATE
-#include "a.out.encap.h"
-#else
-#include <a.out.h>
-#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.
 
-#ifndef N_MAGIC
-#ifdef COFF_FORMAT
-#define N_MAGIC(exec) ((exec).magic)
-#else
-#define N_MAGIC(exec) ((exec).a_magic)
-#endif
-#endif
+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 "defs.h"
+#include <errno.h>
 #include <signal.h>
-#include <sys/param.h>
-#include <sys/dir.h>
-#include <sys/file.h>
-#include <sys/stat.h>
-
-#ifdef UNISOFT_ASSHOLES
-#define        PMMU
-#define        NEW_PMMU
-#include <sys/seg.h>           /* Required for user.ps */
-#include <sys/time.h>          /* '' */
-#include <sys/mmu.h>           /* '' */
-#include <sys/reg.h>
-#define mc68881                        /* Required to get float in user.ps */
-#endif
-
-#ifdef UMAX_CORE
-#include <sys/ptrace.h>
-#else
-#include <sys/user.h>
-#endif
-
-#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 */
-
-#ifndef COFF_FORMAT
-#define AOUTHDR                struct exec
-#endif
-
-extern char *sys_siglist[];
+#include <fcntl.h>
+#include "frame.h"  /* required by inferior.h */
+#include "inferior.h"
+#include "symtab.h"
+#include "command.h"
+#include "gdbcmd.h"
+#include "bfd.h"
+#include "target.h"
+#include "gdbcore.h"
+#include "dis-asm.h"
+#include "language.h"
 
-extern core_file_command (), exec_file_command ();
+extern char registers[];
 
 /* Hook for `exec_file_command' command to call.  */
 
-void (*exec_file_display_hook) ();
-   
-/* File names of core file and executable file.  */
-
-char *corefile;
-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.  */
-
-int corechan;
-int execchan;
-
-/* Last modification time of executable file.
-   Also used in source.c to compare against mtime of a source file.  */
-
-int exec_mtime;
-
-/* Virtual addresses of bounds of the two areas of memory in the core file.  */
-
-CORE_ADDR data_start;
-CORE_ADDR data_end;
-CORE_ADDR stack_start;
-CORE_ADDR stack_end;
-
-/* 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.  */
+void (*exec_file_display_hook) PARAMS ((char *)) = NULL;
 
-CORE_ADDR text_start;
-CORE_ADDR text_end;
+/* Binary file diddling handle for the core file.  */
 
-CORE_ADDR exec_data_start;
-CORE_ADDR exec_data_end;
+bfd *core_bfd = NULL;
 
-/* Address in executable file of start of text area data.  */
-
-int text_offset;
-
-/* Address in executable file of start of data area data.  */
-
-int exec_data_offset;
-
-/* Address in core file of start of data area data.  */
-
-int data_offset;
-
-/* Address in core file of start of stack area data.  */
-
-int stack_offset;
-  
-#ifdef COFF_FORMAT
-/* various coff data structures */
-
-FILHDR file_hdr;
-SCNHDR text_hdr;
-SCNHDR data_hdr;
-
-#endif /* not COFF_FORMAT */
+\f
+/* Backward compatability with old way of specifying core files.  */
 
-/* a.out header saved in core file.  */
-  
-AOUTHDR core_aouthdr;
+void
+core_file_command (filename, from_tty)
+     char *filename;
+     int from_tty;
+{
+  struct target_ops *t;
 
-/* a.out header of exec file.  */
+  dont_repeat ();                      /* Either way, seems bogus. */
 
-AOUTHDR exec_aouthdr;
+  t = find_core_target ();
+  if (t != NULL)
+    if (!filename)
+      (t->to_detach) (filename, from_tty);
+    else
+      (t->to_open) (filename, from_tty);
+  else
+    error ("GDB can't read core files on this machine.");
+}
 
-void validate_files ();
-unsigned int register_addr ();
 \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) ();
+     void (*hook) PARAMS ((char *));
 {
   exec_file_display_hook = hook;
 }
@@ -170,41 +82,33 @@ specify_exec_file_hook (hook)
 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,
-   print a warning if they don't go together.
-   This should really check that the core file came
-   from that exec file, but I don't know how to do it.  */
+   print a warning if they don't go together.  */
 
 void
 validate_files ()
 {
-  if (execfile != 0 && corefile != 0)
+  if (exec_bfd && core_bfd)
     {
-      struct stat st_core;
-
-      fstat (corechan, &st_core);
-
-      if (N_MAGIC (core_aouthdr) != 0
-         && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
-       printf ("Warning: core file does not match specified executable file.\n");
-      else if (exec_mtime > st_core.st_mtime)
-       printf ("Warning: exec file is newer than core file.\n");
+      if (!core_file_matches_executable_p (core_bfd, exec_bfd))
+       warning ("core file may not match specified executable file.");
+      else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
+       warning ("exec file is newer than core file.");
     }
 }
 
@@ -216,296 +120,213 @@ char *
 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 ()
-{
-  char *symfile;
-  extern char *get_sym_file ();
-
-  if (execfile)
-    printf ("Executable file \"%s\".\n", execfile);
-  else
-    printf ("No executable file\n");
+\f
+/* Report a memory error with error().  */
 
-  if (corefile)
-    printf ("Core dump file  \"%s\".\n", corefile);
+void
+memory_error (status, memaddr)
+     int status;
+     CORE_ADDR memaddr;
+{
+  /* FIXME-32x64--casting CORE_ADDR to unsigned long */
+  if (status == EIO)
+    {
+      /* Actually, address between memaddr and memaddr + len
+        was out of bounds. */
+      error ("Cannot access memory at address %s.",
+            local_hex_string((unsigned long) memaddr));
+    }
   else
-    printf ("No core dump file\n");
-
-  if (have_inferior_p ())
-    printf ("Using the running image of the program, rather than these files.\n");
-
-  symfile = get_sym_file ();
-  if (symfile != 0)
-    printf ("Symbols from    \"%s\".\n", symfile);
-
-  if (! have_inferior_p ())
     {
-      if (execfile)
-       {
-         printf ("Text segment in executable from 0x%x to 0x%x.\n",
-                 text_start, text_end);
-         printf ("Data segment in executable from 0x%x to 0x%x.\n",
-                 exec_data_start, exec_data_end);
-         if (corefile)
-           printf("(But since we have a core file, we're using...)\n");
-       }
-      if (corefile)
-       {
-         printf ("Data segment in core file  from 0x%x to 0x%x.\n",
-                 data_start, data_end);
-         printf ("Stack segment in core file from 0x%x to 0x%x.\n",
-                 stack_start, stack_end);
-       }
+      error ("Error accessing memory address %s: %s.",
+            local_hex_string ((unsigned long) memaddr),
+            safe_strerror (status));
     }
 }
-\f
-/* Read "memory data" from core file and/or executable file.
-   Returns zero if successful, 1 if xfer_core_file failed, errno value if
-   ptrace failed. */
 
-int
+/* 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 ())
-      return read_inferior_memory (memaddr, myaddr, len);
-  else
-      return xfer_core_file (memaddr, myaddr, len);
+  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.  */
+/* Like target_read_memory, but slightly different parameters.  */
 
 int
+dis_asm_read_memory (memaddr, myaddr, len, info)
+     bfd_vma memaddr;
+     bfd_byte *myaddr;
+     int len;
+     disassemble_info *info;
+{
+  return target_read_memory (memaddr, (char *) myaddr, len);
+}
+
+/* Like memory_error with slightly different parameters.  */
+void
+dis_asm_memory_error (status, memaddr, info)
+     int status;
+     bfd_vma memaddr;
+     disassemble_info *info;
+{
+  memory_error (status, memaddr);
+}
+
+/* Like print_address with slightly different parameters.  */
+void
+dis_asm_print_address (addr, info)
+     bfd_vma addr;
+     struct disassemble_info *info;
+{
+  print_address (addr, info->stream);
+}
+
+/* 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);
 }
 
-/* Read from the program's memory (except for inferior processes).
-   This function is misnamed, since it only reads, never writes; and
-   since it will use the core file and/or executable file as necessary.
+/* Read an integer from debugged memory, given address and number of bytes.  */
 
-   It should be extended to write as well as read, FIXME, for patching files.
+LONGEST
+read_memory_integer (memaddr, len)
+     CORE_ADDR memaddr;
+     int len;
+{
+  char buf[sizeof (LONGEST)];
 
-   Return 0 if address could be read, 1 if not. */
+  read_memory (memaddr, buf, len);
+  return extract_signed_integer (buf, len);
+}
 
-int
-xfer_core_file (memaddr, myaddr, len)
+unsigned LONGEST
+read_memory_unsigned_integer (memaddr, len)
      CORE_ADDR memaddr;
-     char *myaddr;
      int len;
 {
-  register int i;
-  register int val;
-  int xferchan;
-  char **xferfile;
-  int fileptr;
-  int returnval = 0;
-
-  while (len > 0)
-    {
-      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.
-
-        We put the most likely tests first for efficiency.  */
-
-      /* Note that if there is no core file
-        data_start and data_end are equal.  */
-      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;
-       }
-      else if (memaddr < text_start)
-       {
-         i = min (len, text_start - memaddr);
-       }
-      else if (memaddr >= text_end
-              && memaddr < (corechan >= 0? data_start : exec_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);
-       }
-      else
-       {
-         /* Address did not classify into one of the known ranges.
-            This could be because data_start != exec_data_start
-            or data_end similarly. */
-         abort();
-       }
-
-      /* 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.
-        (FIXME we never write.) */
-      else
-       {
-         bzero (myaddr, i);
-         returnval = 1;
-       }
-
-      memaddr += i;
-      myaddr += i;
-      len -= i;
-    }
-  return returnval;
+  char buf[sizeof (unsigned LONGEST)];
+
+  read_memory (memaddr, buf, len);
+  return extract_unsigned_integer (buf, len);
 }
 \f
-/* My replacement for the read system call.
-   Used like `read' but keeps going if `read' returns too soon.  */
+#if 0
+/* Enable after 4.12.  It is not tested.  */
 
-int
-myread (desc, addr, len)
-     int desc;
-     char *addr;
+/* Search code.  Targets can just make this their search function, or
+   if the protocol has a less general search function, they can call this
+   in the cases it can't handle.  */
+void
+generic_search (len, data, mask, startaddr, increment, lorange, hirange
+               addr_found, data_found)
      int len;
+     char *data;
+     char *mask;
+     CORE_ADDR startaddr;
+     int increment;
+     CORE_ADDR lorange;
+     CORE_ADDR hirange;
+     CORE_ADDR *addr_found;
+     char *data_found;
 {
-  register int val;
-  int orglen = len;
+  int i;
+  CORE_ADDR curaddr = startaddr;
 
-  while (len > 0)
+  while (curaddr >= lorange && curaddr < hirange)
     {
-      val = read (desc, addr, len);
-      if (val < 0)
-       return val;
-      if (val == 0)
-       return orglen - len;
-      len -= val;
-      addr += val;
+      read_memory (curaddr, data_found, len);
+      for (i = 0; i < len; ++i)
+       if ((data_found[i] & mask[i]) != data[i])
+         goto try_again;
+      /* It matches.  */
+      *addr_found = curaddr;
+      return;
+
+    try_again:
+      curaddr += increment;
     }
-  return orglen;
+  *addr_found = (CORE_ADDR)0;
+  return;
 }
+#endif /* 0 */
 \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.  */
+/* The current default bfd target.  Points to storage allocated for
+   gnutarget_string.  */
+char *gnutarget;
 
-unsigned int
-register_addr (regno, blockend)
-     int regno;
-     int blockend;
-{
-  int addr;
+/* Same thing, except it is "auto" not NULL for the default case.  */
+static char *gnutarget_string;
 
-  if (regno < 0 || regno >= NUM_REGS)
-    error ("Invalid register number %d.", regno);
+static void set_gnutarget_command
+  PARAMS ((char *, int, struct cmd_list_element *));
 
-  REGISTER_U_ADDR (addr, blockend, regno);
+static void
+set_gnutarget_command (ignore, from_tty, c)
+     char *ignore;
+     int from_tty;
+     struct cmd_list_element *c;
+{
+  if (STREQ (gnutarget_string, "auto"))
+    gnutarget = NULL;
+  else
+    gnutarget = gnutarget_string;
+}
 
-  return addr;
+/* Set the gnutarget.  */
+void
+set_gnutarget (newtarget)
+     char *newtarget;
+{
+  if (gnutarget_string != NULL)
+    free (gnutarget_string);
+  gnutarget_string = savestring (newtarget, strlen (newtarget));
+  set_gnutarget_command (NULL, 0, NULL);
 }
 
-#endif /* REGISTER_U_ADDR */
-\f
 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.");
+  struct cmd_list_element *c;
+  c = add_cmd ("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.  This command has been superseded by the\n\
+`target core' and `detach' commands.", &cmdlist);
+  c->completer = filename_completer;
+
+  c = add_set_cmd ("gnutarget", class_files, var_string_noescape,
+                 (char *) &gnutarget_string,
+                 "Set the current BFD target.\n\
+Use `set gnutarget auto' to specify automatic detection.",
+                 &setlist);
+  c->function.sfunc = set_gnutarget_command;
+  add_show_from_set (c, &showlist);
+
+  if (getenv ("GNUTARGET"))
+    set_gnutarget (getenv ("GNUTARGET"));
+  else
+    set_gnutarget ("auto");
 }
-
This page took 0.029846 seconds and 4 git commands to generate.