1 /* Native-dependent code for FreeBSD.
3 Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "gdbthread.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include <sys/types.h>
31 #include <sys/procfs.h>
32 #include <sys/sysctl.h>
37 /* Return the name of a file that can be opened to get the symbols for
38 the child process identified by PID. */
41 fbsd_pid_to_exec_file (int pid)
43 size_t len = MAXPATHLEN;
44 char *buf = xcalloc (len, sizeof (char));
47 #ifdef KERN_PROC_PATHNAME
52 mib[2] = KERN_PROC_PATHNAME;
54 if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
58 path = xstrprintf ("/proc/%d/file", pid);
59 if (readlink (path, buf, MAXPATHLEN) == -1)
70 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
73 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
75 int resident, privateresident;
79 /* As of FreeBSD 5.0-RELEASE, the layout is described in
80 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
81 new column was added to the procfs map. Therefore we can't use
82 fscanf since we need to support older releases too. */
83 if (fgets (buf, sizeof buf, mapfile) != NULL)
84 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
85 &resident, &privateresident, &obj, protection);
87 return (ret != 0 && ret != EOF);
90 /* Iterate over all the memory regions in the current inferior,
91 calling FUNC for each memory region. OBFD is passed as the last
95 fbsd_find_memory_regions (find_memory_region_ftype func, void *obfd)
97 pid_t pid = ptid_get_pid (inferior_ptid);
100 unsigned long start, end, size;
102 int read, write, exec;
103 struct cleanup *cleanup;
105 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
106 cleanup = make_cleanup (xfree, mapfilename);
107 mapfile = fopen (mapfilename, "r");
109 error (_("Couldn't open %s."), mapfilename);
110 make_cleanup_fclose (mapfile);
113 fprintf_filtered (gdb_stdout,
114 "Reading memory regions from %s\n", mapfilename);
116 /* Now iterate until end-of-file. */
117 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
121 read = (strchr (protection, 'r') != 0);
122 write = (strchr (protection, 'w') != 0);
123 exec = (strchr (protection, 'x') != 0);
127 fprintf_filtered (gdb_stdout,
128 "Save segment, %ld bytes at %s (%c%c%c)\n",
129 size, paddress (target_gdbarch, start),
135 /* Invoke the callback function to create the corefile segment. */
136 func (start, size, read, write, exec, obfd);
139 do_cleanups (cleanup);
144 find_signalled_thread (struct thread_info *info, void *data)
146 if (info->suspend.stop_signal != TARGET_SIGNAL_0
147 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
153 static enum target_signal
154 find_stop_signal (void)
156 struct thread_info *info =
157 iterate_over_threads (find_signalled_thread, NULL);
160 return info->suspend.stop_signal;
162 return TARGET_SIGNAL_0;
165 /* Create appropriate note sections for a corefile, returning them in
169 fbsd_make_corefile_notes (bfd *obfd, int *note_size)
171 const struct regcache *regcache = get_current_regcache ();
172 struct gdbarch *gdbarch = get_regcache_arch (regcache);
175 char *note_data = NULL;
176 Elf_Internal_Ehdr *i_ehdrp;
177 const struct regset *regset;
180 /* Put a "FreeBSD" label in the ELF header. */
181 i_ehdrp = elf_elfheader (obfd);
182 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
184 gdb_assert (gdbarch_regset_from_core_section_p (gdbarch));
187 regset = gdbarch_regset_from_core_section (gdbarch, ".reg", size);
188 gdb_assert (regset && regset->collect_regset);
189 regset->collect_regset (regset, regcache, -1, &gregs, size);
191 note_data = elfcore_write_prstatus (obfd, note_data, note_size,
192 ptid_get_pid (inferior_ptid),
193 find_stop_signal (), &gregs);
195 size = sizeof fpregs;
196 regset = gdbarch_regset_from_core_section (gdbarch, ".reg2", size);
197 gdb_assert (regset && regset->collect_regset);
198 regset->collect_regset (regset, regcache, -1, &fpregs, size);
200 note_data = elfcore_write_prfpreg (obfd, note_data, note_size,
201 &fpregs, sizeof (fpregs));
203 if (get_exec_file (0))
205 const char *fname = lbasename (get_exec_file (0));
206 char *psargs = xstrdup (fname);
208 if (get_inferior_args ())
209 psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
212 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
216 make_cleanup (xfree, note_data);