Commit | Line | Data |
---|---|---|
578c1c03 MK |
1 | /* Native-dependent code for FreeBSD. |
2 | ||
197e01b6 | 3 | Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. |
578c1c03 MK |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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 | |
197e01b6 EZ |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
578c1c03 MK |
21 | |
22 | #include "defs.h" | |
23 | #include "gdbcore.h" | |
24 | #include "inferior.h" | |
25 | #include "regcache.h" | |
26 | #include "regset.h" | |
27 | ||
28 | #include "gdb_assert.h" | |
29 | #include "gdb_string.h" | |
578c1c03 | 30 | #include <sys/types.h> |
68b9939a MK |
31 | #include <sys/procfs.h> |
32 | #include <sys/sysctl.h> | |
578c1c03 MK |
33 | |
34 | #include "elf-bfd.h" | |
35 | #include "fbsd-nat.h" | |
36 | ||
37 | /* Return a the name of file that can be opened to get the symbols for | |
38 | the child process identified by PID. */ | |
39 | ||
40 | char * | |
41 | fbsd_pid_to_exec_file (int pid) | |
42 | { | |
68b9939a MK |
43 | size_t len = MAXPATHLEN; |
44 | char *buf = xcalloc (len, sizeof (char)); | |
578c1c03 | 45 | char *path; |
578c1c03 | 46 | |
68b9939a MK |
47 | #ifdef KERN_PROC_PATHNAME |
48 | int mib[4]; | |
578c1c03 | 49 | |
68b9939a MK |
50 | mib[0] = CTL_KERN; |
51 | mib[1] = KERN_PROC; | |
52 | mib[2] = KERN_PROC_PATHNAME; | |
53 | mib[3] = pid; | |
54 | if (sysctl (mib, 4, buf, &len, NULL, 0) == 0) | |
578c1c03 | 55 | return buf; |
68b9939a | 56 | #endif |
578c1c03 | 57 | |
68b9939a MK |
58 | path = xstrprintf ("/proc/%d/file", pid); |
59 | if (readlink (path, buf, MAXPATHLEN) == -1) | |
60 | { | |
61 | xfree (buf); | |
62 | buf = NULL; | |
63 | } | |
64 | ||
65 | xfree (path); | |
66 | return buf; | |
578c1c03 MK |
67 | } |
68 | ||
69 | static int | |
70 | fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end, | |
71 | char *protection) | |
72 | { | |
73 | /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */ | |
74 | char buf[256]; | |
75 | int resident, privateresident; | |
76 | unsigned long obj; | |
77 | int ret = EOF; | |
78 | ||
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); | |
86 | ||
87 | return (ret != 0 && ret != EOF); | |
88 | } | |
89 | ||
90 | /* Iterate over all the memory regions in the current inferior, | |
91 | calling FUNC for each memory region. OBFD is passed as the last | |
92 | argument to FUNC. */ | |
93 | ||
94 | int | |
95 | fbsd_find_memory_regions (int (*func) (CORE_ADDR, unsigned long, | |
96 | int, int, int, void *), | |
97 | void *obfd) | |
98 | { | |
99 | pid_t pid = ptid_get_pid (inferior_ptid); | |
100 | char *mapfilename; | |
101 | FILE *mapfile; | |
102 | unsigned long start, end, size; | |
103 | char protection[4]; | |
104 | int read, write, exec; | |
105 | ||
106 | mapfilename = xstrprintf ("/proc/%ld/map", (long) pid); | |
107 | mapfile = fopen (mapfilename, "r"); | |
108 | if (mapfile == NULL) | |
8a3fe4f8 | 109 | error (_("Couldn't open %s."), mapfilename); |
578c1c03 MK |
110 | |
111 | if (info_verbose) | |
112 | fprintf_filtered (gdb_stdout, | |
113 | "Reading memory regions from %s\n", mapfilename); | |
114 | ||
115 | /* Now iterate until end-of-file. */ | |
116 | while (fbsd_read_mapping (mapfile, &start, &end, &protection[0])) | |
117 | { | |
118 | size = end - start; | |
119 | ||
120 | read = (strchr (protection, 'r') != 0); | |
121 | write = (strchr (protection, 'w') != 0); | |
122 | exec = (strchr (protection, 'x') != 0); | |
123 | ||
124 | if (info_verbose) | |
125 | { | |
126 | fprintf_filtered (gdb_stdout, | |
127 | "Save segment, %ld bytes at 0x%s (%c%c%c)\n", | |
128 | size, paddr_nz (start), | |
129 | read ? 'r' : '-', | |
130 | write ? 'w' : '-', | |
131 | exec ? 'x' : '-'); | |
132 | } | |
133 | ||
134 | /* Invoke the callback function to create the corefile segment. */ | |
135 | func (start, size, read, write, exec, obfd); | |
136 | } | |
137 | ||
138 | fclose (mapfile); | |
139 | return 0; | |
140 | } | |
141 | ||
142 | /* Create appropriate note sections for a corefile, returning them in | |
143 | allocated memory. */ | |
144 | ||
145 | char * | |
146 | fbsd_make_corefile_notes (bfd *obfd, int *note_size) | |
147 | { | |
148 | struct gdbarch *gdbarch = current_gdbarch; | |
149 | const struct regcache *regcache = current_regcache; | |
150 | gregset_t gregs; | |
151 | fpregset_t fpregs; | |
152 | char *note_data = NULL; | |
153 | Elf_Internal_Ehdr *i_ehdrp; | |
154 | const struct regset *regset; | |
155 | size_t size; | |
156 | ||
157 | /* Put a "FreeBSD" label in the ELF header. */ | |
158 | i_ehdrp = elf_elfheader (obfd); | |
159 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; | |
160 | ||
161 | gdb_assert (gdbarch_regset_from_core_section_p (gdbarch)); | |
162 | ||
163 | size = sizeof gregs; | |
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); | |
167 | ||
168 | note_data = elfcore_write_prstatus (obfd, note_data, note_size, | |
169 | ptid_get_pid (inferior_ptid), | |
170 | stop_signal, &gregs); | |
171 | ||
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); | |
176 | ||
177 | note_data = elfcore_write_prfpreg (obfd, note_data, note_size, | |
178 | &fpregs, sizeof (fpregs)); | |
179 | ||
180 | if (get_exec_file (0)) | |
181 | { | |
182 | char *fname = strrchr (get_exec_file (0), '/') + 1; | |
183 | char *psargs = xstrdup (fname); | |
184 | ||
185 | if (get_inferior_args ()) | |
186 | psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL); | |
187 | ||
188 | note_data = elfcore_write_prpsinfo (obfd, note_data, note_size, | |
189 | fname, psargs); | |
190 | } | |
191 | ||
192 | make_cleanup (xfree, note_data); | |
193 | return note_data; | |
194 | } |