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