1 /* FreeBSD-specific methods for using the /proc file system.
2 Copyright 2002 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
26 #include <sys/procfs.h>
27 #include <sys/types.h>
34 child_pid_to_exec_file (int pid)
39 xasprintf (&path, "/proc/%d/file", pid);
40 buf = xcalloc (MAXPATHLEN, sizeof (char));
41 make_cleanup (xfree, path);
42 make_cleanup (xfree, buf);
44 if (readlink (path, buf, MAXPATHLEN) > 0)
51 read_mapping (FILE *mapfile,
56 int resident, privateresident;
58 int ref_count, shadow_count;
60 char cow[5], access[4];
64 /* The layout is described in /usr/src/miscfs/procfs/procfs_map.c. */
65 ret = fscanf (mapfile, "%lx %lx %d %d %lx %s %d %d %x %s %s %s\n",
67 &resident, &privateresident, &obj,
69 &ref_count, &shadow_count, &flags, cow, access, type);
71 return (ret != 0 && ret != EOF);
75 fbsd_find_memory_regions (int (*func) (CORE_ADDR,
81 pid_t pid = ptid_get_pid (inferior_ptid);
84 unsigned long start, end, size;
86 int read, write, exec;
88 xasprintf (&mapfilename, "/proc/%ld/map", (long) pid);
89 mapfile = fopen (mapfilename, "r");
91 error ("Couldn't open %s\n", mapfilename);
94 fprintf_filtered (gdb_stdout,
95 "Reading memory regions from %s\n", mapfilename);
97 /* Now iterate until end-of-file. */
98 while (read_mapping (mapfile, &start, &end, &protection[0]))
102 read = (strchr (protection, 'r') != 0);
103 write = (strchr (protection, 'w') != 0);
104 exec = (strchr (protection, 'x') != 0);
108 fprintf_filtered (gdb_stdout,
109 "Save segment, %ld bytes at 0x%s (%c%c%c)\n",
110 size, paddr_nz (start),
116 /* Invoke the callback function to create the corefile segment. */
117 func (start, size, read, write, exec, obfd);
125 fbsd_make_corefile_notes (bfd *obfd, int *note_size)
129 char *note_data = NULL;
131 fill_gregset (&gregs, -1);
132 note_data = (char *) elfcore_write_prstatus (obfd,
135 ptid_get_pid (inferior_ptid),
139 fill_fpregset (&fpregs, -1);
140 note_data = (char *) elfcore_write_prfpreg (obfd,
146 if (get_exec_file (0))
148 char *fname = strrchr (get_exec_file (0), '/') + 1;
149 char *psargs = xstrdup (fname);
151 if (get_inferior_args ())
152 psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL);
154 note_data = (char *) elfcore_write_prpsinfo (obfd,
161 make_cleanup (xfree, note_data);
167 _initialize_fbsd_proc (void)
169 extern void inftarg_set_find_memory_regions ();
170 extern void inftarg_set_make_corefile_notes ();
172 inftarg_set_find_memory_regions (fbsd_find_memory_regions);
173 inftarg_set_make_corefile_notes (fbsd_make_corefile_notes);