1 /* Native-dependent code for FreeBSD.
3 Copyright (C) 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdbthread.h"
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
29 #include <sys/types.h>
30 #include <sys/procfs.h>
31 #include <sys/sysctl.h>
36 /* Return a the name of file that can be opened to get the symbols for
37 the child process identified by PID. */
40 fbsd_pid_to_exec_file (int pid)
42 size_t len = MAXPATHLEN;
43 char *buf = xcalloc (len, sizeof (char));
46 #ifdef KERN_PROC_PATHNAME
51 mib[2] = KERN_PROC_PATHNAME;
53 if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
57 path = xstrprintf ("/proc/%d/file", pid);
58 if (readlink (path, buf, MAXPATHLEN) == -1)
69 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
72 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
74 int resident, privateresident;
78 /* As of FreeBSD 5.0-RELEASE, the layout is described in
79 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
80 new column was added to the procfs map. Therefore we can't use
81 fscanf since we need to support older releases too. */
82 if (fgets (buf, sizeof buf, mapfile) != NULL)
83 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
84 &resident, &privateresident, &obj, protection);
86 return (ret != 0 && ret != EOF);
89 /* Iterate over all the memory regions in the current inferior,
90 calling FUNC for each memory region. OBFD is passed as the last
94 fbsd_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
95 int, int, int, void *),
98 pid_t pid = ptid_get_pid (inferior_ptid);
101 unsigned long start, end, size;
103 int read, write, exec;
104 struct cleanup *cleanup;
106 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
107 cleanup = make_cleanup (xfree, mapfilename);
108 mapfile = fopen (mapfilename, "r");
110 error (_("Couldn't open %s."), mapfilename);
111 make_cleanup_fclose (mapfile);
114 fprintf_filtered (gdb_stdout,
115 "Reading memory regions from %s\n", mapfilename);
117 /* Now iterate until end-of-file. */
118 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
122 read = (strchr (protection, 'r') != 0);
123 write = (strchr (protection, 'w') != 0);
124 exec = (strchr (protection, 'x') != 0);
128 fprintf_filtered (gdb_stdout,
129 "Save segment, %ld bytes at 0x%s (%c%c%c)\n",
130 size, paddr_nz (start),
136 /* Invoke the callback function to create the corefile segment. */
137 func (start, size, read, write, exec, obfd);
140 do_cleanups (cleanup);
145 find_signalled_thread (struct thread_info *info, void *data)
147 if (info->stop_signal != TARGET_SIGNAL_0
148 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
154 static enum target_signal
155 find_stop_signal (void)
157 struct thread_info *info =
158 iterate_over_threads (find_signalled_thread, NULL);
161 return info->stop_signal;
163 return TARGET_SIGNAL_0;
166 /* Create appropriate note sections for a corefile, returning them in
170 fbsd_make_corefile_notes (bfd *obfd, int *note_size)
172 const struct regcache *regcache = get_current_regcache ();
173 struct gdbarch *gdbarch = get_regcache_arch (regcache);
176 char *note_data = NULL;
177 Elf_Internal_Ehdr *i_ehdrp;
178 const struct regset *regset;
181 /* Put a "FreeBSD" label in the ELF header. */
182 i_ehdrp = elf_elfheader (obfd);
183 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
185 gdb_assert (gdbarch_regset_from_core_section_p (gdbarch));
188 regset = gdbarch_regset_from_core_section (gdbarch, ".reg", size);
189 gdb_assert (regset && regset->collect_regset);
190 regset->collect_regset (regset, regcache, -1, &gregs, size);
192 note_data = elfcore_write_prstatus (obfd, note_data, note_size,
193 ptid_get_pid (inferior_ptid),
194 find_stop_signal (), &gregs);
196 size = sizeof fpregs;
197 regset = gdbarch_regset_from_core_section (gdbarch, ".reg2", size);
198 gdb_assert (regset && regset->collect_regset);
199 regset->collect_regset (regset, regcache, -1, &fpregs, size);
201 note_data = elfcore_write_prfpreg (obfd, note_data, note_size,
202 &fpregs, sizeof (fpregs));
204 if (get_exec_file (0))
206 char *fname = strrchr (get_exec_file (0), '/') + 1;
207 char *psargs = xstrdup (fname);
209 if (get_inferior_args ())
210 psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL);
212 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
216 make_cleanup (xfree, note_data);