.
[deliverable/binutils-gdb.git] / gdb / fbsd-proc.c
index 91746d61786d2f8c6558d93ae38c20aec8874b77..16813a9a1c8a67ad51de36592a2e1fa84b6f4862 100644 (file)
@@ -1,5 +1,6 @@
 /* FreeBSD-specific methods for using the /proc file system.
-   Copyright 2002 Free Software Foundation, Inc.
+
+   Copyright 2002, 2003 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -21,6 +22,7 @@
 #include "defs.h"
 #include "gdbcore.h"
 #include "inferior.h"
+#include "gdb_string.h"
 
 #include <sys/procfs.h>
 #include <sys/types.h>
@@ -35,7 +37,7 @@ child_pid_to_exec_file (int pid)
   char *path;
   char *buf;
 
-  asprintf (&path, "/proc/%d/file", pid);
+  xasprintf (&path, "/proc/%d/file", pid);
   buf = xcalloc (MAXPATHLEN, sizeof (char));
   make_cleanup (xfree, path);
   make_cleanup (xfree, buf);
@@ -47,34 +49,29 @@ child_pid_to_exec_file (int pid)
 }
 
 static int
-read_mapping (FILE *mapfile,
-             unsigned long *start,
-             unsigned long *end,
+read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
              char *protection)
 {
+  /* FreeBSD 5.1-RELEASE uses a 256-byte buffer.  */
+  char buf[256];
   int resident, privateresident;
   unsigned long obj;
-  int ref_count, shadow_count;
-  unsigned flags;
-  char cow[5], access[4];
-  char type[8];
-  int ret;
-
-  /* The layout is described in /usr/src/miscfs/procfs/procfs_map.c.  */
-  ret = fscanf (mapfile, "%lx %lx %d %d %lx %s %d %d %x %s %s %s\n",
-               start, end,
-               &resident, &privateresident, &obj,
-               protection,
-               &ref_count, &shadow_count, &flags, cow, access, type);
+  int ret = EOF;
+
+  /* As of FreeBSD 5.0-RELEASE, the layout is described in
+     /usr/src/sys/fs/procfs/procfs_map.c.  Somewhere in 5.1-CURRENT a
+     new column was added to the procfs map.  Therefore we can't use
+     fscanf since we need to support older releases too.  */
+  if (fgets (buf, sizeof buf, mapfile) != NULL)
+    ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
+                 &resident, &privateresident, &obj, protection);
 
   return (ret != 0 && ret != EOF);
 }
 
 static int
-fbsd_find_memory_regions (int (*func) (CORE_ADDR,
-                                      unsigned long,
-                                      int, int, int,
-                                      void *),
+fbsd_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
+                                      int, int, int, void *),
                          void *obfd)
 {
   pid_t pid = ptid_get_pid (inferior_ptid);
@@ -84,7 +81,7 @@ fbsd_find_memory_regions (int (*func) (CORE_ADDR,
   char protection[4];
   int read, write, exec;
 
-  asprintf (&mapfilename, "/proc/%ld/map", (long) pid);
+  xasprintf (&mapfilename, "/proc/%ld/map", (long) pid);
   mapfile = fopen (mapfilename, "r");
   if (mapfile == NULL)
     error ("Couldn't open %s\n", mapfilename);
@@ -126,35 +123,31 @@ fbsd_make_corefile_notes (bfd *obfd, int *note_size)
   gregset_t gregs;
   fpregset_t fpregs;
   char *note_data = NULL;
+  Elf_Internal_Ehdr *i_ehdrp;
+
+  /* Put a "FreeBSD" label in the ELF header.  */
+  i_ehdrp = elf_elfheader (obfd);
+  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
 
   fill_gregset (&gregs, -1);
-  note_data = (char *) elfcore_write_prstatus (obfd,
-                                              note_data,
-                                              note_size,
-                                              ptid_get_pid (inferior_ptid),
-                                              stop_signal,
-                                              &gregs);
+  note_data = elfcore_write_prstatus (obfd, note_data, note_size,
+                                     ptid_get_pid (inferior_ptid),
+                                     stop_signal, &gregs);
 
   fill_fpregset (&fpregs, -1);
-  note_data = (char *) elfcore_write_prfpreg (obfd,
-                                             note_data,
-                                             note_size,
-                                             &fpregs,
-                                             sizeof (fpregs));
+  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 = strdup (fname);
+      char *psargs = xstrdup (fname);
 
       if (get_inferior_args ())
        psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL);
 
-      note_data = (char *) elfcore_write_prpsinfo (obfd,
-                                                  note_data,
-                                                  note_size,
-                                                  fname,
-                                                  psargs);
+      note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
+                                         fname, psargs);
     }
 
   make_cleanup (xfree, note_data);
This page took 0.025819 seconds and 4 git commands to generate.