Commit | Line | Data |
---|---|---|
be4d1333 MS |
1 | /* Linux-specific methods for using the /proc file system. |
2 | Copyright 2001, 2002 Free Software Foundation, Inc. | |
4b09dc8c MS |
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" | |
be4d1333 | 22 | #include "inferior.h" |
4b09dc8c | 23 | #include <sys/param.h> /* for MAXPATHLEN */ |
be4d1333 MS |
24 | #include <sys/procfs.h> |
25 | #include "gregset.h" /* for gregset */ | |
26 | #include "gdbcore.h" /* for get_exec_file */ | |
27 | #include "gdbthread.h" /* for struct thread_info etc. */ | |
28 | #include "elf-bfd.h" | |
29 | ||
30 | /* Function: child_pid_to_exec_file | |
31 | * | |
32 | * Accepts an integer pid | |
33 | * Returns a string representing a file that can be opened | |
34 | * to get the symbols for the child process. | |
35 | */ | |
4b09dc8c MS |
36 | |
37 | char * | |
38 | child_pid_to_exec_file (int pid) | |
39 | { | |
145fdc6e | 40 | char *name1, *name2; |
4b09dc8c | 41 | |
145fdc6e MS |
42 | name1 = xmalloc (MAXPATHLEN); |
43 | name2 = xmalloc (MAXPATHLEN); | |
44 | make_cleanup (xfree, name1); | |
45 | make_cleanup (xfree, name2); | |
46 | memset (name2, 0, MAXPATHLEN); | |
47 | ||
48 | sprintf (name1, "/proc/%d/exe", pid); | |
49 | if (readlink (name1, name2, MAXPATHLEN) > 0) | |
50 | return name2; | |
51 | else | |
52 | return name1; | |
4b09dc8c | 53 | } |
be4d1333 MS |
54 | |
55 | /* Function: linux_find_memory_regions | |
56 | * | |
57 | * Fills the "to_find_memory_regions" target vector. | |
58 | * Lists the memory regions in the inferior for a corefile. | |
59 | */ | |
60 | ||
61 | static int | |
62 | linux_find_memory_regions (int (*func) (CORE_ADDR, | |
63 | unsigned long, | |
64 | int, int, int, | |
65 | void *), | |
66 | void *obfd) | |
67 | { | |
68 | long long pid = PIDGET (inferior_ptid); | |
69 | char procfilename[MAXPATHLEN]; | |
70 | FILE *procfile; | |
71 | long long addr, endaddr, size, offset, inode; | |
72 | char perms[8], dev[8], filename[MAXPATHLEN]; | |
73 | int read, write, exec; | |
74 | int ret; | |
75 | ||
76 | /* Compose the filename for the /proc memory map, and open it. */ | |
77 | sprintf (procfilename, "/proc/%lld/maps", pid); | |
78 | if ((procfile = fopen (procfilename, "r")) == NULL) | |
79 | error ("Could not open %s\n", procfilename); | |
80 | ||
81 | if (info_verbose) | |
82 | fprintf_filtered (gdb_stdout, | |
83 | "Reading memory regions from %s\n", procfilename); | |
84 | ||
85 | /* Read the first memory segment descriptor from the maps file. */ | |
86 | ret = fscanf (procfile, "%llx-%llx %s %llx %s %llx", | |
87 | &addr, &endaddr, perms, &offset, dev, &inode); | |
88 | if (inode) | |
89 | fscanf (procfile, " %s\n", filename); | |
90 | else | |
91 | { | |
92 | filename[0] = '\0'; | |
93 | fscanf (procfile, "\n"); | |
94 | } | |
95 | ||
96 | /* Now iterate until end-of-file. */ | |
97 | while (ret > 0 && ret != EOF) | |
98 | { | |
99 | size = endaddr - addr; | |
100 | ||
101 | /* Get the segment's permissions. */ | |
102 | read = (strchr (perms, 'r') != 0); | |
103 | write = (strchr (perms, 'w') != 0); | |
104 | exec = (strchr (perms, 'x') != 0); | |
105 | ||
106 | if (info_verbose) | |
107 | { | |
108 | fprintf_filtered (gdb_stdout, | |
109 | "Save segment, %lld bytes at 0x%s (%c%c%c)", | |
110 | size, paddr_nz (addr), | |
111 | read ? 'r' : ' ', | |
112 | write ? 'w' : ' ', | |
113 | exec ? 'x' : ' '); | |
114 | if (filename && filename[0]) | |
115 | fprintf_filtered (gdb_stdout, | |
116 | " for %s", filename); | |
117 | fprintf_filtered (gdb_stdout, "\n"); | |
118 | } | |
119 | ||
120 | /* Invoke the callback function to create the corefile segment. */ | |
121 | func (addr, size, read, write, exec, obfd); | |
122 | ||
123 | /* Read the next memory region. */ | |
124 | filename[0] = '\0'; | |
125 | ret = fscanf (procfile, "%llx-%llx %s %llx %s %llx", | |
126 | &addr, &endaddr, perms, &offset, dev, &inode); | |
127 | if (inode) | |
128 | fscanf (procfile, " %s\n", filename); | |
129 | else | |
130 | { | |
131 | filename[0] = '\0'; | |
132 | fscanf (procfile, "\n"); | |
133 | } | |
134 | } | |
135 | ||
136 | fclose (procfile); | |
137 | return 0; | |
138 | } | |
139 | ||
140 | /* Function: linux_do_thread_registers | |
141 | * | |
142 | * Records the thread's register state for the corefile note section. | |
143 | */ | |
144 | ||
145 | static char * | |
146 | linux_do_thread_registers (bfd *obfd, ptid_t ptid, | |
147 | char *note_data, int *note_size) | |
148 | { | |
149 | gdb_gregset_t gregs; | |
150 | gdb_fpregset_t fpregs; | |
151 | #ifdef HAVE_PTRACE_GETFPXREGS | |
152 | gdb_fpxregset_t fpxregs; | |
153 | #endif | |
154 | unsigned long merged_pid = ptid_get_tid (ptid) << 16 | ptid_get_pid (ptid); | |
155 | ||
156 | fill_gregset (&gregs, -1); | |
157 | note_data = (char *) elfcore_write_prstatus (obfd, | |
158 | note_data, | |
159 | note_size, | |
160 | merged_pid, | |
161 | stop_signal, | |
162 | &gregs); | |
163 | ||
164 | fill_fpregset (&fpregs, -1); | |
165 | note_data = (char *) elfcore_write_prfpreg (obfd, | |
166 | note_data, | |
167 | note_size, | |
168 | &fpregs, | |
169 | sizeof (fpregs)); | |
170 | #ifdef HAVE_PTRACE_GETFPXREGS | |
171 | fill_fpxregset (&fpxregs, -1); | |
172 | note_data = (char *) elfcore_write_prxfpreg (obfd, | |
173 | note_data, | |
174 | note_size, | |
175 | &fpxregs, | |
176 | sizeof (fpxregs)); | |
177 | #endif | |
178 | return note_data; | |
179 | } | |
180 | ||
181 | struct linux_corefile_thread_data { | |
182 | bfd *obfd; | |
183 | char *note_data; | |
184 | int *note_size; | |
185 | }; | |
186 | ||
187 | /* Function: linux_corefile_thread_callback | |
188 | * | |
189 | * Called by gdbthread.c once per thread. | |
190 | * Records the thread's register state for the corefile note section. | |
191 | */ | |
192 | ||
193 | static int | |
194 | linux_corefile_thread_callback (struct thread_info *ti, void *data) | |
195 | { | |
196 | struct linux_corefile_thread_data *args = data; | |
197 | ptid_t saved_ptid = inferior_ptid; | |
198 | ||
199 | inferior_ptid = ti->ptid; | |
200 | registers_changed (); | |
201 | target_fetch_registers (-1); /* FIXME should not be necessary; | |
202 | fill_gregset should do it automatically. */ | |
203 | args->note_data = linux_do_thread_registers (args->obfd, | |
204 | ti->ptid, | |
205 | args->note_data, | |
206 | args->note_size); | |
207 | inferior_ptid = saved_ptid; | |
208 | registers_changed (); | |
209 | target_fetch_registers (-1); /* FIXME should not be necessary; | |
210 | fill_gregset should do it automatically. */ | |
211 | return 0; | |
212 | } | |
213 | ||
214 | /* Function: linux_make_note_section | |
215 | * | |
216 | * Fills the "to_make_corefile_note" target vector. | |
217 | * Builds the note section for a corefile, and returns it | |
218 | * in a malloc buffer. | |
219 | */ | |
220 | ||
221 | static char * | |
222 | linux_make_note_section (bfd *obfd, int *note_size) | |
223 | { | |
224 | struct linux_corefile_thread_data thread_args; | |
225 | struct cleanup *old_chain; | |
226 | char fname[16] = {'\0'}; | |
227 | char psargs[80] = {'\0'}; | |
228 | char *note_data = NULL; | |
229 | ptid_t current_ptid = inferior_ptid; | |
230 | ||
231 | if (get_exec_file (0)) | |
232 | { | |
233 | strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname)); | |
234 | strncpy (psargs, get_exec_file (0), | |
235 | sizeof (psargs)); | |
236 | if (get_inferior_args ()) | |
237 | { | |
238 | strncat (psargs, " ", | |
239 | sizeof (psargs) - strlen (psargs)); | |
240 | strncat (psargs, get_inferior_args (), | |
241 | sizeof (psargs) - strlen (psargs)); | |
242 | } | |
243 | note_data = (char *) elfcore_write_prpsinfo (obfd, | |
244 | note_data, | |
245 | note_size, | |
246 | fname, | |
247 | psargs); | |
248 | } | |
249 | ||
250 | /* Dump information for threads. */ | |
251 | thread_args.obfd = obfd; | |
252 | thread_args.note_data = note_data; | |
253 | thread_args.note_size = note_size; | |
254 | iterate_over_threads (linux_corefile_thread_callback, &thread_args); | |
255 | if (thread_args.note_data == note_data) | |
256 | { | |
257 | /* iterate_over_threads didn't come up with any threads; | |
258 | just use inferior_ptid. */ | |
259 | note_data = linux_do_thread_registers (obfd, inferior_ptid, | |
260 | note_data, note_size); | |
261 | } | |
262 | else | |
263 | { | |
264 | note_data = thread_args.note_data; | |
265 | } | |
266 | ||
267 | make_cleanup (xfree, note_data); | |
268 | return note_data; | |
269 | } | |
270 | ||
271 | void | |
272 | _initialize_linux_proc (void) | |
273 | { | |
274 | extern void inftarg_set_find_memory_regions (); | |
275 | extern void inftarg_set_make_corefile_notes (); | |
276 | ||
277 | inftarg_set_find_memory_regions (linux_find_memory_regions); | |
278 | inftarg_set_make_corefile_notes (linux_make_note_section); | |
279 | } |