1 /* Native-dependent code for FreeBSD.
3 Copyright 2002, 2003, 2004 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 2 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, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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 a the name of 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 (int (*func) (CORE_ADDR, unsigned long,
96 int, int, int, void *),
99 pid_t pid = ptid_get_pid (inferior_ptid);
102 unsigned long start, end, size;
104 int read, write, exec;
106 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
107 mapfile = fopen (mapfilename, "r");
109 error (_("Couldn't open %s."), mapfilename);
112 fprintf_filtered (gdb_stdout,
113 "Reading memory regions from %s\n", mapfilename);
115 /* Now iterate until end-of-file. */
116 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
120 read = (strchr (protection, 'r') != 0);
121 write = (strchr (protection, 'w') != 0);
122 exec = (strchr (protection, 'x') != 0);
126 fprintf_filtered (gdb_stdout,
127 "Save segment, %ld bytes at 0x%s (%c%c%c)\n",
128 size, paddr_nz (start),
134 /* Invoke the callback function to create the corefile segment. */
135 func (start, size, read, write, exec, obfd);
142 /* Create appropriate note sections for a corefile, returning them in
146 fbsd_make_corefile_notes (bfd *obfd, int *note_size)
148 struct gdbarch *gdbarch = current_gdbarch;
149 const struct regcache *regcache = current_regcache;
152 char *note_data = NULL;
153 Elf_Internal_Ehdr *i_ehdrp;
154 const struct regset *regset;
157 /* Put a "FreeBSD" label in the ELF header. */
158 i_ehdrp = elf_elfheader (obfd);
159 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
161 gdb_assert (gdbarch_regset_from_core_section_p (gdbarch));
164 regset = gdbarch_regset_from_core_section (gdbarch, ".reg", size);
165 gdb_assert (regset && regset->collect_regset);
166 regset->collect_regset (regset, regcache, -1, &gregs, size);
168 note_data = elfcore_write_prstatus (obfd, note_data, note_size,
169 ptid_get_pid (inferior_ptid),
170 stop_signal, &gregs);
172 size = sizeof fpregs;
173 regset = gdbarch_regset_from_core_section (gdbarch, ".reg2", size);
174 gdb_assert (regset && regset->collect_regset);
175 regset->collect_regset (regset, regcache, -1, &fpregs, size);
177 note_data = elfcore_write_prfpreg (obfd, note_data, note_size,
178 &fpregs, sizeof (fpregs));
180 if (get_exec_file (0))
182 char *fname = strrchr (get_exec_file (0), '/') + 1;
183 char *psargs = xstrdup (fname);
185 if (get_inferior_args ())
186 psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL);
188 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
192 make_cleanup (xfree, note_data);