Commit | Line | Data |
---|---|---|
578c1c03 MK |
1 | /* Native-dependent code for FreeBSD. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2002-2014 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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
578c1c03 MK |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
578c1c03 MK |
19 | |
20 | #include "defs.h" | |
21 | #include "gdbcore.h" | |
22 | #include "inferior.h" | |
23 | #include "regcache.h" | |
24 | #include "regset.h" | |
2020b7ab | 25 | #include "gdbthread.h" |
578c1c03 | 26 | #include <sys/types.h> |
68b9939a MK |
27 | #include <sys/procfs.h> |
28 | #include <sys/sysctl.h> | |
578c1c03 MK |
29 | |
30 | #include "elf-bfd.h" | |
31 | #include "fbsd-nat.h" | |
32 | ||
766062f6 | 33 | /* Return the name of a file that can be opened to get the symbols for |
578c1c03 MK |
34 | the child process identified by PID. */ |
35 | ||
36 | char * | |
8dd27370 | 37 | fbsd_pid_to_exec_file (struct target_ops *self, int pid) |
578c1c03 | 38 | { |
b4ab256d HZ |
39 | ssize_t len = PATH_MAX; |
40 | static char buf[PATH_MAX]; | |
41 | char name[PATH_MAX]; | |
578c1c03 | 42 | |
68b9939a MK |
43 | #ifdef KERN_PROC_PATHNAME |
44 | int mib[4]; | |
578c1c03 | 45 | |
68b9939a MK |
46 | mib[0] = CTL_KERN; |
47 | mib[1] = KERN_PROC; | |
48 | mib[2] = KERN_PROC_PATHNAME; | |
49 | mib[3] = pid; | |
50 | if (sysctl (mib, 4, buf, &len, NULL, 0) == 0) | |
578c1c03 | 51 | return buf; |
68b9939a | 52 | #endif |
578c1c03 | 53 | |
b4ab256d HZ |
54 | xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid); |
55 | len = readlink (name, buf, PATH_MAX - 1); | |
56 | if (len != -1) | |
68b9939a | 57 | { |
b4ab256d HZ |
58 | buf[len] = '\0'; |
59 | return buf; | |
68b9939a MK |
60 | } |
61 | ||
b4ab256d | 62 | return NULL; |
578c1c03 MK |
63 | } |
64 | ||
65 | static int | |
66 | fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end, | |
67 | char *protection) | |
68 | { | |
69 | /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */ | |
70 | char buf[256]; | |
71 | int resident, privateresident; | |
72 | unsigned long obj; | |
73 | int ret = EOF; | |
74 | ||
75 | /* As of FreeBSD 5.0-RELEASE, the layout is described in | |
76 | /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a | |
77 | new column was added to the procfs map. Therefore we can't use | |
78 | fscanf since we need to support older releases too. */ | |
79 | if (fgets (buf, sizeof buf, mapfile) != NULL) | |
80 | ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end, | |
81 | &resident, &privateresident, &obj, protection); | |
82 | ||
83 | return (ret != 0 && ret != EOF); | |
84 | } | |
85 | ||
86 | /* Iterate over all the memory regions in the current inferior, | |
87 | calling FUNC for each memory region. OBFD is passed as the last | |
88 | argument to FUNC. */ | |
89 | ||
90 | int | |
2e73927c TT |
91 | fbsd_find_memory_regions (struct target_ops *self, |
92 | find_memory_region_ftype func, void *obfd) | |
578c1c03 MK |
93 | { |
94 | pid_t pid = ptid_get_pid (inferior_ptid); | |
95 | char *mapfilename; | |
96 | FILE *mapfile; | |
97 | unsigned long start, end, size; | |
98 | char protection[4]; | |
99 | int read, write, exec; | |
7c8a8b04 | 100 | struct cleanup *cleanup; |
578c1c03 MK |
101 | |
102 | mapfilename = xstrprintf ("/proc/%ld/map", (long) pid); | |
7c8a8b04 | 103 | cleanup = make_cleanup (xfree, mapfilename); |
578c1c03 MK |
104 | mapfile = fopen (mapfilename, "r"); |
105 | if (mapfile == NULL) | |
8a3fe4f8 | 106 | error (_("Couldn't open %s."), mapfilename); |
7c8a8b04 | 107 | make_cleanup_fclose (mapfile); |
578c1c03 MK |
108 | |
109 | if (info_verbose) | |
110 | fprintf_filtered (gdb_stdout, | |
111 | "Reading memory regions from %s\n", mapfilename); | |
112 | ||
113 | /* Now iterate until end-of-file. */ | |
114 | while (fbsd_read_mapping (mapfile, &start, &end, &protection[0])) | |
115 | { | |
116 | size = end - start; | |
117 | ||
118 | read = (strchr (protection, 'r') != 0); | |
119 | write = (strchr (protection, 'w') != 0); | |
120 | exec = (strchr (protection, 'x') != 0); | |
121 | ||
122 | if (info_verbose) | |
123 | { | |
124 | fprintf_filtered (gdb_stdout, | |
5af949e3 | 125 | "Save segment, %ld bytes at %s (%c%c%c)\n", |
f5656ead | 126 | size, paddress (target_gdbarch (), start), |
578c1c03 MK |
127 | read ? 'r' : '-', |
128 | write ? 'w' : '-', | |
129 | exec ? 'x' : '-'); | |
130 | } | |
131 | ||
4f69f4c2 JK |
132 | /* Invoke the callback function to create the corefile segment. |
133 | Pass MODIFIED as true, we do not know the real modification state. */ | |
134 | func (start, size, read, write, exec, 1, obfd); | |
578c1c03 MK |
135 | } |
136 | ||
7c8a8b04 | 137 | do_cleanups (cleanup); |
578c1c03 MK |
138 | return 0; |
139 | } | |
140 | ||
2020b7ab PA |
141 | static int |
142 | find_signalled_thread (struct thread_info *info, void *data) | |
143 | { | |
a493e3e2 | 144 | if (info->suspend.stop_signal != GDB_SIGNAL_0 |
2020b7ab PA |
145 | && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid)) |
146 | return 1; | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
2ea28649 | 151 | static enum gdb_signal |
2020b7ab PA |
152 | find_stop_signal (void) |
153 | { | |
154 | struct thread_info *info = | |
155 | iterate_over_threads (find_signalled_thread, NULL); | |
156 | ||
157 | if (info) | |
16c381f0 | 158 | return info->suspend.stop_signal; |
2020b7ab | 159 | else |
a493e3e2 | 160 | return GDB_SIGNAL_0; |
2020b7ab PA |
161 | } |
162 | ||
578c1c03 MK |
163 | /* Create appropriate note sections for a corefile, returning them in |
164 | allocated memory. */ | |
165 | ||
166 | char * | |
fc6691b2 | 167 | fbsd_make_corefile_notes (struct target_ops *self, bfd *obfd, int *note_size) |
578c1c03 | 168 | { |
594f7785 | 169 | const struct regcache *regcache = get_current_regcache (); |
9970f04b | 170 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
578c1c03 MK |
171 | gregset_t gregs; |
172 | fpregset_t fpregs; | |
173 | char *note_data = NULL; | |
174 | Elf_Internal_Ehdr *i_ehdrp; | |
175 | const struct regset *regset; | |
176 | size_t size; | |
177 | ||
178 | /* Put a "FreeBSD" label in the ELF header. */ | |
179 | i_ehdrp = elf_elfheader (obfd); | |
180 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; | |
181 | ||
182 | gdb_assert (gdbarch_regset_from_core_section_p (gdbarch)); | |
183 | ||
184 | size = sizeof gregs; | |
185 | regset = gdbarch_regset_from_core_section (gdbarch, ".reg", size); | |
186 | gdb_assert (regset && regset->collect_regset); | |
187 | regset->collect_regset (regset, regcache, -1, &gregs, size); | |
188 | ||
189 | note_data = elfcore_write_prstatus (obfd, note_data, note_size, | |
190 | ptid_get_pid (inferior_ptid), | |
2020b7ab | 191 | find_stop_signal (), &gregs); |
578c1c03 MK |
192 | |
193 | size = sizeof fpregs; | |
194 | regset = gdbarch_regset_from_core_section (gdbarch, ".reg2", size); | |
195 | gdb_assert (regset && regset->collect_regset); | |
196 | regset->collect_regset (regset, regcache, -1, &fpregs, size); | |
197 | ||
198 | note_data = elfcore_write_prfpreg (obfd, note_data, note_size, | |
199 | &fpregs, sizeof (fpregs)); | |
200 | ||
201 | if (get_exec_file (0)) | |
202 | { | |
6d6c6b1f | 203 | const char *fname = lbasename (get_exec_file (0)); |
578c1c03 MK |
204 | char *psargs = xstrdup (fname); |
205 | ||
206 | if (get_inferior_args ()) | |
3f1f6884 JK |
207 | psargs = reconcat (psargs, psargs, " ", get_inferior_args (), |
208 | (char *) NULL); | |
578c1c03 MK |
209 | |
210 | note_data = elfcore_write_prpsinfo (obfd, note_data, note_size, | |
211 | fname, psargs); | |
212 | } | |
213 | ||
578c1c03 MK |
214 | return note_data; |
215 | } |