1 /* Linux-specific methods for using the /proc file system.
2 Copyright 2001, 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. */
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. */
30 /* Function: child_pid_to_exec_file
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.
38 child_pid_to_exec_file (int pid
)
40 static char fname
[MAXPATHLEN
];
42 sprintf (fname
, "/proc/%d/exe", pid
);
43 /* FIXME use readlink to get the real name. */
47 /* Function: linux_find_memory_regions
49 * Fills the "to_find_memory_regions" target vector.
50 * Lists the memory regions in the inferior for a corefile.
54 linux_find_memory_regions (int (*func
) (CORE_ADDR
,
60 long long pid
= PIDGET (inferior_ptid
);
61 char procfilename
[MAXPATHLEN
];
63 long long addr
, endaddr
, size
, offset
, inode
;
64 char perms
[8], dev
[8], filename
[MAXPATHLEN
];
65 int read
, write
, exec
;
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
);
74 fprintf_filtered (gdb_stdout
,
75 "Reading memory regions from %s\n", procfilename
);
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
);
81 fscanf (procfile
, " %s\n", filename
);
85 fscanf (procfile
, "\n");
88 /* Now iterate until end-of-file. */
89 while (ret
> 0 && ret
!= EOF
)
91 size
= endaddr
- addr
;
93 /* Get the segment's permissions. */
94 read
= (strchr (perms
, 'r') != 0);
95 write
= (strchr (perms
, 'w') != 0);
96 exec
= (strchr (perms
, 'x') != 0);
100 fprintf_filtered (gdb_stdout
,
101 "Save segment, %lld bytes at 0x%s (%c%c%c)",
102 size
, paddr_nz (addr
),
106 if (filename
&& filename
[0])
107 fprintf_filtered (gdb_stdout
,
108 " for %s", filename
);
109 fprintf_filtered (gdb_stdout
, "\n");
112 /* Invoke the callback function to create the corefile segment. */
113 func (addr
, size
, read
, write
, exec
, obfd
);
115 /* Read the next memory region. */
117 ret
= fscanf (procfile
, "%llx-%llx %s %llx %s %llx",
118 &addr
, &endaddr
, perms
, &offset
, dev
, &inode
);
120 fscanf (procfile
, " %s\n", filename
);
124 fscanf (procfile
, "\n");
132 /* Function: linux_do_thread_registers
134 * Records the thread's register state for the corefile note section.
138 linux_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
139 char *note_data
, int *note_size
)
142 gdb_fpregset_t fpregs
;
143 #ifdef HAVE_PTRACE_GETFPXREGS
144 gdb_fpxregset_t fpxregs
;
146 unsigned long merged_pid
= ptid_get_tid (ptid
) << 16 | ptid_get_pid (ptid
);
148 fill_gregset (&gregs
, -1);
149 note_data
= (char *) elfcore_write_prstatus (obfd
,
156 fill_fpregset (&fpregs
, -1);
157 note_data
= (char *) elfcore_write_prfpreg (obfd
,
162 #ifdef HAVE_PTRACE_GETFPXREGS
163 fill_fpxregset (&fpxregs
, -1);
164 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
173 struct linux_corefile_thread_data
{
179 /* Function: linux_corefile_thread_callback
181 * Called by gdbthread.c once per thread.
182 * Records the thread's register state for the corefile note section.
186 linux_corefile_thread_callback (struct thread_info
*ti
, void *data
)
188 struct linux_corefile_thread_data
*args
= data
;
189 ptid_t saved_ptid
= inferior_ptid
;
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
,
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. */
206 /* Function: linux_make_note_section
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.
214 linux_make_note_section (bfd
*obfd
, int *note_size
)
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
;
223 if (get_exec_file (0))
225 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
226 strncpy (psargs
, get_exec_file (0),
228 if (get_inferior_args ())
230 strncat (psargs
, " ",
231 sizeof (psargs
) - strlen (psargs
));
232 strncat (psargs
, get_inferior_args (),
233 sizeof (psargs
) - strlen (psargs
));
235 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
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
)
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
);
256 note_data
= thread_args
.note_data
;
259 make_cleanup (xfree
, note_data
);
264 _initialize_linux_proc (void)
266 extern void inftarg_set_find_memory_regions ();
267 extern void inftarg_set_make_corefile_notes ();
269 inftarg_set_find_memory_regions (linux_find_memory_regions
);
270 inftarg_set_make_corefile_notes (linux_make_note_section
);