oops - omitted from previous delta
[deliverable/binutils-gdb.git] / gdb / fbsd-proc.c
1 /* FreeBSD-specific methods for using the /proc file system.
2 Copyright 2002 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21 #include "defs.h"
22 #include "gdbcore.h"
23 #include "inferior.h"
24 #include "gdb_string.h"
25
26 #include <sys/procfs.h>
27 #include <sys/types.h>
28
29 #include "elf-bfd.h"
30
31 #include "gregset.h"
32
33 char *
34 child_pid_to_exec_file (int pid)
35 {
36 char *path;
37 char *buf;
38
39 xasprintf (&path, "/proc/%d/file", pid);
40 buf = xcalloc (MAXPATHLEN, sizeof (char));
41 make_cleanup (xfree, path);
42 make_cleanup (xfree, buf);
43
44 if (readlink (path, buf, MAXPATHLEN) > 0)
45 return buf;
46
47 return NULL;
48 }
49
50 static int
51 read_mapping (FILE *mapfile,
52 unsigned long *start,
53 unsigned long *end,
54 char *protection)
55 {
56 int resident, privateresident;
57 unsigned long obj;
58 int ref_count, shadow_count;
59 unsigned flags;
60 char cow[5], access[4];
61 char type[8];
62 int ret;
63
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",
66 start, end,
67 &resident, &privateresident, &obj,
68 protection,
69 &ref_count, &shadow_count, &flags, cow, access, type);
70
71 return (ret != 0 && ret != EOF);
72 }
73
74 static int
75 fbsd_find_memory_regions (int (*func) (CORE_ADDR,
76 unsigned long,
77 int, int, int,
78 void *),
79 void *obfd)
80 {
81 pid_t pid = ptid_get_pid (inferior_ptid);
82 char *mapfilename;
83 FILE *mapfile;
84 unsigned long start, end, size;
85 char protection[4];
86 int read, write, exec;
87
88 xasprintf (&mapfilename, "/proc/%ld/map", (long) pid);
89 mapfile = fopen (mapfilename, "r");
90 if (mapfile == NULL)
91 error ("Couldn't open %s\n", mapfilename);
92
93 if (info_verbose)
94 fprintf_filtered (gdb_stdout,
95 "Reading memory regions from %s\n", mapfilename);
96
97 /* Now iterate until end-of-file. */
98 while (read_mapping (mapfile, &start, &end, &protection[0]))
99 {
100 size = end - start;
101
102 read = (strchr (protection, 'r') != 0);
103 write = (strchr (protection, 'w') != 0);
104 exec = (strchr (protection, 'x') != 0);
105
106 if (info_verbose)
107 {
108 fprintf_filtered (gdb_stdout,
109 "Save segment, %ld bytes at 0x%s (%c%c%c)\n",
110 size, paddr_nz (start),
111 read ? 'r' : '-',
112 write ? 'w' : '-',
113 exec ? 'x' : '-');
114 }
115
116 /* Invoke the callback function to create the corefile segment. */
117 func (start, size, read, write, exec, obfd);
118 }
119
120 fclose (mapfile);
121 return 0;
122 }
123
124 static char *
125 fbsd_make_corefile_notes (bfd *obfd, int *note_size)
126 {
127 gregset_t gregs;
128 fpregset_t fpregs;
129 char *note_data = NULL;
130
131 fill_gregset (&gregs, -1);
132 note_data = (char *) elfcore_write_prstatus (obfd,
133 note_data,
134 note_size,
135 ptid_get_pid (inferior_ptid),
136 stop_signal,
137 &gregs);
138
139 fill_fpregset (&fpregs, -1);
140 note_data = (char *) elfcore_write_prfpreg (obfd,
141 note_data,
142 note_size,
143 &fpregs,
144 sizeof (fpregs));
145
146 if (get_exec_file (0))
147 {
148 char *fname = strrchr (get_exec_file (0), '/') + 1;
149 char *psargs = xstrdup (fname);
150
151 if (get_inferior_args ())
152 psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL);
153
154 note_data = (char *) elfcore_write_prpsinfo (obfd,
155 note_data,
156 note_size,
157 fname,
158 psargs);
159 }
160
161 make_cleanup (xfree, note_data);
162 return note_data;
163 }
164 \f
165
166 void
167 _initialize_fbsd_proc (void)
168 {
169 extern void inftarg_set_find_memory_regions ();
170 extern void inftarg_set_make_corefile_notes ();
171
172 inftarg_set_find_memory_regions (fbsd_find_memory_regions);
173 inftarg_set_make_corefile_notes (fbsd_make_corefile_notes);
174 }
This page took 0.032627 seconds and 4 git commands to generate.