windows-nat.c: conflicting declaration of struct thread_info in C++ mode
[deliverable/binutils-gdb.git] / gdb / fbsd-nat.c
index b7045d493e2dd1856caa0c04d282b943b2081f0c..1ce197d18f6f210279cdc40084e78e72bacf4665 100644 (file)
@@ -1,12 +1,12 @@
 /* Native-dependent code for FreeBSD.
 
-   Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2002-2015 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    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
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    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., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
 #include "gdbcore.h"
 #include "inferior.h"
 #include "regcache.h"
 #include "regset.h"
-
-#include "gdb_assert.h"
-#include "gdb_string.h"
-#include <sys/procfs.h>
+#include "gdbthread.h"
 #include <sys/types.h>
+#include <sys/procfs.h>
+#include <sys/sysctl.h>
+#ifdef HAVE_KINFO_GETVMMAP
+#include <sys/user.h>
+#include <libutil.h>
+#endif
 
 #include "elf-bfd.h"
 #include "fbsd-nat.h"
 
-/* Return a the name of file that can be opened to get the symbols for
+/* Return the name of a file that can be opened to get the symbols for
    the child process identified by PID.  */
 
 char *
-fbsd_pid_to_exec_file (int pid)
+fbsd_pid_to_exec_file (struct target_ops *self, int pid)
 {
-  char *path;
-  char *buf;
-
-  path = xstrprintf ("/proc/%d/file", pid);
-  buf = xcalloc (MAXPATHLEN, sizeof (char));
-  make_cleanup (xfree, path);
-  make_cleanup (xfree, buf);
-
-  if (readlink (path, buf, MAXPATHLEN) > 0)
+  ssize_t len = PATH_MAX;
+  static char buf[PATH_MAX];
+  char name[PATH_MAX];
+
+#ifdef KERN_PROC_PATHNAME
+  int mib[4];
+
+  mib[0] = CTL_KERN;
+  mib[1] = KERN_PROC;
+  mib[2] = KERN_PROC_PATHNAME;
+  mib[3] = pid;
+  if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
     return buf;
+#endif
+
+  xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
+  len = readlink (name, buf, PATH_MAX - 1);
+  if (len != -1)
+    {
+      buf[len] = '\0';
+      return buf;
+    }
 
   return NULL;
 }
 
+#ifdef HAVE_KINFO_GETVMMAP
+/* Iterate over all the memory regions in the current inferior,
+   calling FUNC for each memory region.  OBFD is passed as the last
+   argument to FUNC.  */
+
+int
+fbsd_find_memory_regions (struct target_ops *self,
+                         find_memory_region_ftype func, void *obfd)
+{
+  pid_t pid = ptid_get_pid (inferior_ptid);
+  struct kinfo_vmentry *vmentl, *kve;
+  uint64_t size;
+  struct cleanup *cleanup;
+  int i, nitems;
+
+  vmentl = kinfo_getvmmap (pid, &nitems);
+  if (vmentl == NULL)
+    perror_with_name (_("Couldn't fetch VM map entries."));
+  cleanup = make_cleanup (free, vmentl);
+
+  for (i = 0; i < nitems; i++)
+    {
+      kve = &vmentl[i];
+
+      /* Skip unreadable segments and those where MAP_NOCORE has been set.  */
+      if (!(kve->kve_protection & KVME_PROT_READ)
+         || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
+       continue;
+
+      /* Skip segments with an invalid type.  */
+      if (kve->kve_type != KVME_TYPE_DEFAULT
+         && kve->kve_type != KVME_TYPE_VNODE
+         && kve->kve_type != KVME_TYPE_SWAP
+         && kve->kve_type != KVME_TYPE_PHYS)
+       continue;
+
+      size = kve->kve_end - kve->kve_start;
+      if (info_verbose)
+       {
+         fprintf_filtered (gdb_stdout, 
+                           "Save segment, %ld bytes at %s (%c%c%c)\n",
+                           (long) size,
+                           paddress (target_gdbarch (), kve->kve_start),
+                           kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
+                           kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
+                           kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
+       }
+
+      /* Invoke the callback function to create the corefile segment.
+        Pass MODIFIED as true, we do not know the real modification state.  */
+      func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
+           kve->kve_protection & KVME_PROT_WRITE,
+           kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
+    }
+  do_cleanups (cleanup);
+  return 0;
+}
+#else
 static int
 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
                   char *protection)
@@ -79,9 +150,8 @@ fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
    argument to FUNC.  */
 
 int
-fbsd_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
-                                      int, int, int, void *),
-                         void *obfd)
+fbsd_find_memory_regions (struct target_ops *self,
+                         find_memory_region_ftype func, void *obfd)
 {
   pid_t pid = ptid_get_pid (inferior_ptid);
   char *mapfilename;
@@ -89,11 +159,14 @@ fbsd_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
   unsigned long start, end, size;
   char protection[4];
   int read, write, exec;
+  struct cleanup *cleanup;
 
   mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
+  cleanup = make_cleanup (xfree, mapfilename);
   mapfile = fopen (mapfilename, "r");
   if (mapfile == NULL)
-    error ("Couldn't open %s\n", mapfilename);
+    error (_("Couldn't open %s."), mapfilename);
+  make_cleanup_fclose (mapfile);
 
   if (info_verbose)
     fprintf_filtered (gdb_stdout, 
@@ -111,71 +184,19 @@ fbsd_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
       if (info_verbose)
        {
          fprintf_filtered (gdb_stdout, 
-                           "Save segment, %ld bytes at 0x%s (%c%c%c)\n", 
-                           size, paddr_nz (start),
+                           "Save segment, %ld bytes at %s (%c%c%c)\n",
+                           size, paddress (target_gdbarch (), start),
                            read ? 'r' : '-',
                            write ? 'w' : '-',
                            exec ? 'x' : '-');
        }
 
-      /* Invoke the callback function to create the corefile segment. */
-      func (start, size, read, write, exec, obfd);
+      /* Invoke the callback function to create the corefile segment.
+        Pass MODIFIED as true, we do not know the real modification state.  */
+      func (start, size, read, write, exec, 1, obfd);
     }
 
-  fclose (mapfile);
+  do_cleanups (cleanup);
   return 0;
 }
-
-/* Create appropriate note sections for a corefile, returning them in
-   allocated memory.  */
-
-char *
-fbsd_make_corefile_notes (bfd *obfd, int *note_size)
-{
-  struct gdbarch *gdbarch = current_gdbarch;
-  const struct regcache *regcache = current_regcache;
-  gregset_t gregs;
-  fpregset_t fpregs;
-  char *note_data = NULL;
-  Elf_Internal_Ehdr *i_ehdrp;
-  const struct regset *regset;
-  size_t size;
-
-  /* Put a "FreeBSD" label in the ELF header.  */
-  i_ehdrp = elf_elfheader (obfd);
-  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
-
-  gdb_assert (gdbarch_regset_from_core_section_p (gdbarch));
-
-  size = sizeof gregs;
-  regset = gdbarch_regset_from_core_section (gdbarch, ".reg", size);
-  gdb_assert (regset && regset->collect_regset);
-  regset->collect_regset (regset, regcache, -1, &gregs, size);
-
-  note_data = elfcore_write_prstatus (obfd, note_data, note_size,
-                                     ptid_get_pid (inferior_ptid),
-                                     stop_signal, &gregs);
-
-  size = sizeof fpregs;
-  regset = gdbarch_regset_from_core_section (gdbarch, ".reg2", size);
-  gdb_assert (regset && regset->collect_regset);
-  regset->collect_regset (regset, regcache, -1, &fpregs, size);
-
-  note_data = elfcore_write_prfpreg (obfd, note_data, note_size,
-                                    &fpregs, sizeof (fpregs));
-
-  if (get_exec_file (0))
-    {
-      char *fname = strrchr (get_exec_file (0), '/') + 1;
-      char *psargs = xstrdup (fname);
-
-      if (get_inferior_args ())
-       psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL);
-
-      note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
-                                         fname, psargs);
-    }
-
-  make_cleanup (xfree, note_data);
-  return note_data;
-}
+#endif
This page took 0.026405 seconds and 4 git commands to generate.