gdb/fbsd-nat.c - gdb

Functions defined

Source code

  1. /* Native-dependent code for FreeBSD.

  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "inferior.h"
  17. #include "regcache.h"
  18. #include "regset.h"
  19. #include "gdbthread.h"
  20. #include <sys/types.h>
  21. #include <sys/procfs.h>
  22. #include <sys/sysctl.h>

  23. #include "elf-bfd.h"
  24. #include "fbsd-nat.h"

  25. /* Return the name of a file that can be opened to get the symbols for
  26.    the child process identified by PID.  */

  27. char *
  28. fbsd_pid_to_exec_file (struct target_ops *self, int pid)
  29. {
  30.   ssize_t len = PATH_MAX;
  31.   static char buf[PATH_MAX];
  32.   char name[PATH_MAX];

  33. #ifdef KERN_PROC_PATHNAME
  34.   int mib[4];

  35.   mib[0] = CTL_KERN;
  36.   mib[1] = KERN_PROC;
  37.   mib[2] = KERN_PROC_PATHNAME;
  38.   mib[3] = pid;
  39.   if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
  40.     return buf;
  41. #endif

  42.   xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
  43.   len = readlink (name, buf, PATH_MAX - 1);
  44.   if (len != -1)
  45.     {
  46.       buf[len] = '\0';
  47.       return buf;
  48.     }

  49.   return NULL;
  50. }

  51. static int
  52. fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
  53.                    char *protection)
  54. {
  55.   /* FreeBSD 5.1-RELEASE uses a 256-byte buffer.  */
  56.   char buf[256];
  57.   int resident, privateresident;
  58.   unsigned long obj;
  59.   int ret = EOF;

  60.   /* As of FreeBSD 5.0-RELEASE, the layout is described in
  61.      /usr/src/sys/fs/procfs/procfs_map.c.  Somewhere in 5.1-CURRENT a
  62.      new column was added to the procfs map.  Therefore we can't use
  63.      fscanf since we need to support older releases too.  */
  64.   if (fgets (buf, sizeof buf, mapfile) != NULL)
  65.     ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
  66.                   &resident, &privateresident, &obj, protection);

  67.   return (ret != 0 && ret != EOF);
  68. }

  69. /* Iterate over all the memory regions in the current inferior,
  70.    calling FUNC for each memory region.  OBFD is passed as the last
  71.    argument to FUNC.  */

  72. int
  73. fbsd_find_memory_regions (struct target_ops *self,
  74.                           find_memory_region_ftype func, void *obfd)
  75. {
  76.   pid_t pid = ptid_get_pid (inferior_ptid);
  77.   char *mapfilename;
  78.   FILE *mapfile;
  79.   unsigned long start, end, size;
  80.   char protection[4];
  81.   int read, write, exec;
  82.   struct cleanup *cleanup;

  83.   mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
  84.   cleanup = make_cleanup (xfree, mapfilename);
  85.   mapfile = fopen (mapfilename, "r");
  86.   if (mapfile == NULL)
  87.     error (_("Couldn't open %s."), mapfilename);
  88.   make_cleanup_fclose (mapfile);

  89.   if (info_verbose)
  90.     fprintf_filtered (gdb_stdout,
  91.                       "Reading memory regions from %s\n", mapfilename);

  92.   /* Now iterate until end-of-file.  */
  93.   while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
  94.     {
  95.       size = end - start;

  96.       read = (strchr (protection, 'r') != 0);
  97.       write = (strchr (protection, 'w') != 0);
  98.       exec = (strchr (protection, 'x') != 0);

  99.       if (info_verbose)
  100.         {
  101.           fprintf_filtered (gdb_stdout,
  102.                             "Save segment, %ld bytes at %s (%c%c%c)\n",
  103.                             size, paddress (target_gdbarch (), start),
  104.                             read ? 'r' : '-',
  105.                             write ? 'w' : '-',
  106.                             exec ? 'x' : '-');
  107.         }

  108.       /* Invoke the callback function to create the corefile segment.
  109.          Pass MODIFIED as true, we do not know the real modification state.  */
  110.       func (start, size, read, write, exec, 1, obfd);
  111.     }

  112.   do_cleanups (cleanup);
  113.   return 0;
  114. }