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