Commit | Line | Data |
---|---|---|
578c1c03 MK |
1 | /* Native-dependent code for FreeBSD. |
2 | ||
42a4f53d | 3 | Copyright (C) 2002-2019 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" | |
0747795c | 21 | #include "common/byte-vector.h" |
578c1c03 MK |
22 | #include "gdbcore.h" |
23 | #include "inferior.h" | |
24 | #include "regcache.h" | |
25 | #include "regset.h" | |
6e9567fe | 26 | #include "gdbcmd.h" |
2020b7ab | 27 | #include "gdbthread.h" |
0747795c | 28 | #include "common/gdb_wait.h" |
7efba073 | 29 | #include "inf-ptrace.h" |
578c1c03 | 30 | #include <sys/types.h> |
68b9939a | 31 | #include <sys/procfs.h> |
e58e05d6 | 32 | #include <sys/ptrace.h> |
929edea9 | 33 | #include <sys/signal.h> |
68b9939a | 34 | #include <sys/sysctl.h> |
25268153 | 35 | #include <sys/user.h> |
92fce24d | 36 | #if defined(HAVE_KINFO_GETFILE) || defined(HAVE_KINFO_GETVMMAP) |
25268153 | 37 | #include <libutil.h> |
92fce24d JB |
38 | #endif |
39 | #if !defined(HAVE_KINFO_GETVMMAP) | |
0747795c | 40 | #include "common/filestuff.h" |
25268153 | 41 | #endif |
578c1c03 MK |
42 | |
43 | #include "elf-bfd.h" | |
44 | #include "fbsd-nat.h" | |
92fce24d | 45 | #include "fbsd-tdep.h" |
578c1c03 | 46 | |
e8c6b620 JB |
47 | #include <list> |
48 | ||
766062f6 | 49 | /* Return the name of a file that can be opened to get the symbols for |
578c1c03 MK |
50 | the child process identified by PID. */ |
51 | ||
f6ac5f3d PA |
52 | char * |
53 | fbsd_nat_target::pid_to_exec_file (int pid) | |
578c1c03 | 54 | { |
f2feec98 | 55 | ssize_t len; |
b4ab256d HZ |
56 | static char buf[PATH_MAX]; |
57 | char name[PATH_MAX]; | |
578c1c03 | 58 | |
68b9939a | 59 | #ifdef KERN_PROC_PATHNAME |
f2feec98 | 60 | size_t buflen; |
68b9939a | 61 | int mib[4]; |
578c1c03 | 62 | |
68b9939a MK |
63 | mib[0] = CTL_KERN; |
64 | mib[1] = KERN_PROC; | |
65 | mib[2] = KERN_PROC_PATHNAME; | |
66 | mib[3] = pid; | |
f2feec98 JB |
67 | buflen = sizeof buf; |
68 | if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0) | |
b999e203 JB |
69 | /* The kern.proc.pathname.<pid> sysctl returns a length of zero |
70 | for processes without an associated executable such as kernel | |
71 | processes. */ | |
72 | return buflen == 0 ? NULL : buf; | |
68b9939a | 73 | #endif |
578c1c03 | 74 | |
b4ab256d HZ |
75 | xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid); |
76 | len = readlink (name, buf, PATH_MAX - 1); | |
77 | if (len != -1) | |
68b9939a | 78 | { |
b4ab256d HZ |
79 | buf[len] = '\0'; |
80 | return buf; | |
68b9939a MK |
81 | } |
82 | ||
b4ab256d | 83 | return NULL; |
578c1c03 MK |
84 | } |
85 | ||
25268153 JB |
86 | #ifdef HAVE_KINFO_GETVMMAP |
87 | /* Iterate over all the memory regions in the current inferior, | |
88 | calling FUNC for each memory region. OBFD is passed as the last | |
89 | argument to FUNC. */ | |
90 | ||
f6ac5f3d PA |
91 | int |
92 | fbsd_nat_target::find_memory_regions (find_memory_region_ftype func, | |
93 | void *obfd) | |
25268153 | 94 | { |
e99b03dc | 95 | pid_t pid = inferior_ptid.pid (); |
e4a26669 | 96 | struct kinfo_vmentry *kve; |
25268153 | 97 | uint64_t size; |
25268153 JB |
98 | int i, nitems; |
99 | ||
262f62f5 | 100 | gdb::unique_xmalloc_ptr<struct kinfo_vmentry> |
e4a26669 | 101 | vmentl (kinfo_getvmmap (pid, &nitems)); |
25268153 JB |
102 | if (vmentl == NULL) |
103 | perror_with_name (_("Couldn't fetch VM map entries.")); | |
25268153 | 104 | |
e4a26669 | 105 | for (i = 0, kve = vmentl.get (); i < nitems; i++, kve++) |
25268153 | 106 | { |
25268153 JB |
107 | /* Skip unreadable segments and those where MAP_NOCORE has been set. */ |
108 | if (!(kve->kve_protection & KVME_PROT_READ) | |
109 | || kve->kve_flags & KVME_FLAG_NOCOREDUMP) | |
110 | continue; | |
111 | ||
112 | /* Skip segments with an invalid type. */ | |
113 | if (kve->kve_type != KVME_TYPE_DEFAULT | |
114 | && kve->kve_type != KVME_TYPE_VNODE | |
115 | && kve->kve_type != KVME_TYPE_SWAP | |
116 | && kve->kve_type != KVME_TYPE_PHYS) | |
117 | continue; | |
118 | ||
119 | size = kve->kve_end - kve->kve_start; | |
120 | if (info_verbose) | |
121 | { | |
122 | fprintf_filtered (gdb_stdout, | |
123 | "Save segment, %ld bytes at %s (%c%c%c)\n", | |
124 | (long) size, | |
125 | paddress (target_gdbarch (), kve->kve_start), | |
126 | kve->kve_protection & KVME_PROT_READ ? 'r' : '-', | |
127 | kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-', | |
128 | kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-'); | |
129 | } | |
130 | ||
131 | /* Invoke the callback function to create the corefile segment. | |
132 | Pass MODIFIED as true, we do not know the real modification state. */ | |
133 | func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ, | |
134 | kve->kve_protection & KVME_PROT_WRITE, | |
135 | kve->kve_protection & KVME_PROT_EXEC, 1, obfd); | |
136 | } | |
25268153 JB |
137 | return 0; |
138 | } | |
139 | #else | |
578c1c03 MK |
140 | static int |
141 | fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end, | |
142 | char *protection) | |
143 | { | |
144 | /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */ | |
145 | char buf[256]; | |
146 | int resident, privateresident; | |
147 | unsigned long obj; | |
148 | int ret = EOF; | |
149 | ||
150 | /* As of FreeBSD 5.0-RELEASE, the layout is described in | |
151 | /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a | |
152 | new column was added to the procfs map. Therefore we can't use | |
153 | fscanf since we need to support older releases too. */ | |
154 | if (fgets (buf, sizeof buf, mapfile) != NULL) | |
155 | ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end, | |
156 | &resident, &privateresident, &obj, protection); | |
157 | ||
158 | return (ret != 0 && ret != EOF); | |
159 | } | |
160 | ||
161 | /* Iterate over all the memory regions in the current inferior, | |
162 | calling FUNC for each memory region. OBFD is passed as the last | |
163 | argument to FUNC. */ | |
164 | ||
f6ac5f3d PA |
165 | int |
166 | fbsd_nat_target::find_memory_regions (find_memory_region_ftype func, | |
167 | void *obfd) | |
578c1c03 | 168 | { |
e99b03dc | 169 | pid_t pid = inferior_ptid.pid (); |
578c1c03 MK |
170 | unsigned long start, end, size; |
171 | char protection[4]; | |
172 | int read, write, exec; | |
173 | ||
e4a26669 JB |
174 | std::string mapfilename = string_printf ("/proc/%ld/map", (long) pid); |
175 | gdb_file_up mapfile (fopen (mapfilename.c_str (), "r")); | |
578c1c03 | 176 | if (mapfile == NULL) |
e4a26669 | 177 | error (_("Couldn't open %s."), mapfilename.c_str ()); |
578c1c03 MK |
178 | |
179 | if (info_verbose) | |
180 | fprintf_filtered (gdb_stdout, | |
e4a26669 | 181 | "Reading memory regions from %s\n", mapfilename.c_str ()); |
578c1c03 MK |
182 | |
183 | /* Now iterate until end-of-file. */ | |
7cd06d6e | 184 | while (fbsd_read_mapping (mapfile.get (), &start, &end, &protection[0])) |
578c1c03 MK |
185 | { |
186 | size = end - start; | |
187 | ||
188 | read = (strchr (protection, 'r') != 0); | |
189 | write = (strchr (protection, 'w') != 0); | |
190 | exec = (strchr (protection, 'x') != 0); | |
191 | ||
192 | if (info_verbose) | |
193 | { | |
194 | fprintf_filtered (gdb_stdout, | |
5af949e3 | 195 | "Save segment, %ld bytes at %s (%c%c%c)\n", |
f5656ead | 196 | size, paddress (target_gdbarch (), start), |
578c1c03 MK |
197 | read ? 'r' : '-', |
198 | write ? 'w' : '-', | |
199 | exec ? 'x' : '-'); | |
200 | } | |
201 | ||
4f69f4c2 JK |
202 | /* Invoke the callback function to create the corefile segment. |
203 | Pass MODIFIED as true, we do not know the real modification state. */ | |
204 | func (start, size, read, write, exec, 1, obfd); | |
578c1c03 MK |
205 | } |
206 | ||
578c1c03 MK |
207 | return 0; |
208 | } | |
25268153 | 209 | #endif |
8f60fe01 | 210 | |
92fce24d JB |
211 | /* Fetch the command line for a running process. */ |
212 | ||
213 | static gdb::unique_xmalloc_ptr<char> | |
214 | fbsd_fetch_cmdline (pid_t pid) | |
215 | { | |
216 | size_t len; | |
217 | int mib[4]; | |
218 | ||
219 | len = 0; | |
220 | mib[0] = CTL_KERN; | |
221 | mib[1] = KERN_PROC; | |
222 | mib[2] = KERN_PROC_ARGS; | |
223 | mib[3] = pid; | |
224 | if (sysctl (mib, 4, NULL, &len, NULL, 0) == -1) | |
225 | return nullptr; | |
226 | ||
227 | if (len == 0) | |
228 | return nullptr; | |
229 | ||
230 | gdb::unique_xmalloc_ptr<char> cmdline ((char *) xmalloc (len)); | |
231 | if (sysctl (mib, 4, cmdline.get (), &len, NULL, 0) == -1) | |
232 | return nullptr; | |
233 | ||
424eb552 JB |
234 | /* Join the arguments with spaces to form a single string. */ |
235 | char *cp = cmdline.get (); | |
236 | for (size_t i = 0; i < len - 1; i++) | |
237 | if (cp[i] == '\0') | |
238 | cp[i] = ' '; | |
239 | cp[len - 1] = '\0'; | |
240 | ||
92fce24d JB |
241 | return cmdline; |
242 | } | |
243 | ||
244 | /* Fetch the external variant of the kernel's internal process | |
245 | structure for the process PID into KP. */ | |
246 | ||
247 | static bool | |
248 | fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp) | |
249 | { | |
250 | size_t len; | |
251 | int mib[4]; | |
252 | ||
253 | len = sizeof *kp; | |
254 | mib[0] = CTL_KERN; | |
255 | mib[1] = KERN_PROC; | |
256 | mib[2] = KERN_PROC_PID; | |
257 | mib[3] = pid; | |
258 | return (sysctl (mib, 4, kp, &len, NULL, 0) == 0); | |
259 | } | |
260 | ||
f6ac5f3d | 261 | /* Implement the "info_proc" target_ops method. */ |
92fce24d | 262 | |
f6ac5f3d PA |
263 | bool |
264 | fbsd_nat_target::info_proc (const char *args, enum info_proc_what what) | |
92fce24d JB |
265 | { |
266 | #ifdef HAVE_KINFO_GETFILE | |
267 | gdb::unique_xmalloc_ptr<struct kinfo_file> fdtbl; | |
268 | int nfd = 0; | |
269 | #endif | |
270 | struct kinfo_proc kp; | |
92fce24d JB |
271 | pid_t pid; |
272 | bool do_cmdline = false; | |
273 | bool do_cwd = false; | |
274 | bool do_exe = false; | |
7e69672e JB |
275 | #ifdef HAVE_KINFO_GETFILE |
276 | bool do_files = false; | |
277 | #endif | |
92fce24d JB |
278 | #ifdef HAVE_KINFO_GETVMMAP |
279 | bool do_mappings = false; | |
280 | #endif | |
281 | bool do_status = false; | |
282 | ||
283 | switch (what) | |
284 | { | |
285 | case IP_MINIMAL: | |
286 | do_cmdline = true; | |
287 | do_cwd = true; | |
288 | do_exe = true; | |
289 | break; | |
290 | #ifdef HAVE_KINFO_GETVMMAP | |
291 | case IP_MAPPINGS: | |
292 | do_mappings = true; | |
293 | break; | |
294 | #endif | |
295 | case IP_STATUS: | |
296 | case IP_STAT: | |
297 | do_status = true; | |
298 | break; | |
299 | case IP_CMDLINE: | |
300 | do_cmdline = true; | |
301 | break; | |
302 | case IP_EXE: | |
303 | do_exe = true; | |
304 | break; | |
305 | case IP_CWD: | |
306 | do_cwd = true; | |
307 | break; | |
7e69672e JB |
308 | #ifdef HAVE_KINFO_GETFILE |
309 | case IP_FILES: | |
310 | do_files = true; | |
311 | break; | |
312 | #endif | |
92fce24d JB |
313 | case IP_ALL: |
314 | do_cmdline = true; | |
315 | do_cwd = true; | |
316 | do_exe = true; | |
7e69672e JB |
317 | #ifdef HAVE_KINFO_GETFILE |
318 | do_files = true; | |
319 | #endif | |
92fce24d JB |
320 | #ifdef HAVE_KINFO_GETVMMAP |
321 | do_mappings = true; | |
322 | #endif | |
323 | do_status = true; | |
324 | break; | |
325 | default: | |
326 | error (_("Not supported on this target.")); | |
327 | } | |
328 | ||
329 | gdb_argv built_argv (args); | |
330 | if (built_argv.count () == 0) | |
331 | { | |
e99b03dc | 332 | pid = inferior_ptid.pid (); |
92fce24d JB |
333 | if (pid == 0) |
334 | error (_("No current process: you must name one.")); | |
335 | } | |
336 | else if (built_argv.count () == 1 && isdigit (built_argv[0][0])) | |
337 | pid = strtol (built_argv[0], NULL, 10); | |
338 | else | |
339 | error (_("Invalid arguments.")); | |
340 | ||
341 | printf_filtered (_("process %d\n"), pid); | |
342 | #ifdef HAVE_KINFO_GETFILE | |
7e69672e | 343 | if (do_cwd || do_exe || do_files) |
92fce24d JB |
344 | fdtbl.reset (kinfo_getfile (pid, &nfd)); |
345 | #endif | |
346 | ||
347 | if (do_cmdline) | |
348 | { | |
349 | gdb::unique_xmalloc_ptr<char> cmdline = fbsd_fetch_cmdline (pid); | |
350 | if (cmdline != nullptr) | |
351 | printf_filtered ("cmdline = '%s'\n", cmdline.get ()); | |
352 | else | |
353 | warning (_("unable to fetch command line")); | |
354 | } | |
355 | if (do_cwd) | |
356 | { | |
357 | const char *cwd = NULL; | |
358 | #ifdef HAVE_KINFO_GETFILE | |
359 | struct kinfo_file *kf = fdtbl.get (); | |
360 | for (int i = 0; i < nfd; i++, kf++) | |
361 | { | |
362 | if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_CWD) | |
363 | { | |
364 | cwd = kf->kf_path; | |
365 | break; | |
366 | } | |
367 | } | |
368 | #endif | |
369 | if (cwd != NULL) | |
370 | printf_filtered ("cwd = '%s'\n", cwd); | |
371 | else | |
372 | warning (_("unable to fetch current working directory")); | |
373 | } | |
374 | if (do_exe) | |
375 | { | |
376 | const char *exe = NULL; | |
377 | #ifdef HAVE_KINFO_GETFILE | |
378 | struct kinfo_file *kf = fdtbl.get (); | |
379 | for (int i = 0; i < nfd; i++, kf++) | |
380 | { | |
381 | if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_TEXT) | |
382 | { | |
383 | exe = kf->kf_path; | |
384 | break; | |
385 | } | |
386 | } | |
387 | #endif | |
388 | if (exe == NULL) | |
f6ac5f3d | 389 | exe = pid_to_exec_file (pid); |
92fce24d JB |
390 | if (exe != NULL) |
391 | printf_filtered ("exe = '%s'\n", exe); | |
392 | else | |
393 | warning (_("unable to fetch executable path name")); | |
394 | } | |
7e69672e JB |
395 | #ifdef HAVE_KINFO_GETFILE |
396 | if (do_files) | |
397 | { | |
398 | struct kinfo_file *kf = fdtbl.get (); | |
399 | ||
400 | if (nfd > 0) | |
401 | { | |
402 | fbsd_info_proc_files_header (); | |
403 | for (int i = 0; i < nfd; i++, kf++) | |
404 | fbsd_info_proc_files_entry (kf->kf_type, kf->kf_fd, kf->kf_flags, | |
405 | kf->kf_offset, kf->kf_vnode_type, | |
406 | kf->kf_sock_domain, kf->kf_sock_type, | |
407 | kf->kf_sock_protocol, &kf->kf_sa_local, | |
408 | &kf->kf_sa_peer, kf->kf_path); | |
409 | } | |
410 | else | |
411 | warning (_("unable to fetch list of open files")); | |
412 | } | |
413 | #endif | |
92fce24d JB |
414 | #ifdef HAVE_KINFO_GETVMMAP |
415 | if (do_mappings) | |
416 | { | |
417 | int nvment; | |
418 | gdb::unique_xmalloc_ptr<struct kinfo_vmentry> | |
419 | vmentl (kinfo_getvmmap (pid, &nvment)); | |
420 | ||
421 | if (vmentl != nullptr) | |
422 | { | |
6f3b1098 JB |
423 | int addr_bit = TARGET_CHAR_BIT * sizeof (void *); |
424 | fbsd_info_proc_mappings_header (addr_bit); | |
92fce24d JB |
425 | |
426 | struct kinfo_vmentry *kve = vmentl.get (); | |
427 | for (int i = 0; i < nvment; i++, kve++) | |
6f3b1098 JB |
428 | fbsd_info_proc_mappings_entry (addr_bit, kve->kve_start, |
429 | kve->kve_end, kve->kve_offset, | |
430 | kve->kve_flags, kve->kve_protection, | |
431 | kve->kve_path); | |
92fce24d JB |
432 | } |
433 | else | |
434 | warning (_("unable to fetch virtual memory map")); | |
435 | } | |
436 | #endif | |
437 | if (do_status) | |
438 | { | |
439 | if (!fbsd_fetch_kinfo_proc (pid, &kp)) | |
440 | warning (_("Failed to fetch process information")); | |
441 | else | |
442 | { | |
443 | const char *state; | |
444 | int pgtok; | |
445 | ||
446 | printf_filtered ("Name: %s\n", kp.ki_comm); | |
447 | switch (kp.ki_stat) | |
448 | { | |
449 | case SIDL: | |
450 | state = "I (idle)"; | |
451 | break; | |
452 | case SRUN: | |
453 | state = "R (running)"; | |
454 | break; | |
455 | case SSTOP: | |
456 | state = "T (stopped)"; | |
457 | break; | |
458 | case SZOMB: | |
459 | state = "Z (zombie)"; | |
460 | break; | |
461 | case SSLEEP: | |
462 | state = "S (sleeping)"; | |
463 | break; | |
464 | case SWAIT: | |
465 | state = "W (interrupt wait)"; | |
466 | break; | |
467 | case SLOCK: | |
468 | state = "L (blocked on lock)"; | |
469 | break; | |
470 | default: | |
471 | state = "? (unknown)"; | |
472 | break; | |
473 | } | |
474 | printf_filtered ("State: %s\n", state); | |
475 | printf_filtered ("Parent process: %d\n", kp.ki_ppid); | |
476 | printf_filtered ("Process group: %d\n", kp.ki_pgid); | |
477 | printf_filtered ("Session id: %d\n", kp.ki_sid); | |
478 | printf_filtered ("TTY: %ju\n", (uintmax_t) kp.ki_tdev); | |
479 | printf_filtered ("TTY owner process group: %d\n", kp.ki_tpgid); | |
480 | printf_filtered ("User IDs (real, effective, saved): %d %d %d\n", | |
481 | kp.ki_ruid, kp.ki_uid, kp.ki_svuid); | |
482 | printf_filtered ("Group IDs (real, effective, saved): %d %d %d\n", | |
483 | kp.ki_rgid, kp.ki_groups[0], kp.ki_svgid); | |
484 | printf_filtered ("Groups: "); | |
485 | for (int i = 0; i < kp.ki_ngroups; i++) | |
486 | printf_filtered ("%d ", kp.ki_groups[i]); | |
487 | printf_filtered ("\n"); | |
488 | printf_filtered ("Minor faults (no memory page): %ld\n", | |
489 | kp.ki_rusage.ru_minflt); | |
490 | printf_filtered ("Minor faults, children: %ld\n", | |
491 | kp.ki_rusage_ch.ru_minflt); | |
492 | printf_filtered ("Major faults (memory page faults): %ld\n", | |
493 | kp.ki_rusage.ru_majflt); | |
494 | printf_filtered ("Major faults, children: %ld\n", | |
495 | kp.ki_rusage_ch.ru_majflt); | |
496 | printf_filtered ("utime: %jd.%06ld\n", | |
497 | (intmax_t) kp.ki_rusage.ru_utime.tv_sec, | |
498 | kp.ki_rusage.ru_utime.tv_usec); | |
499 | printf_filtered ("stime: %jd.%06ld\n", | |
500 | (intmax_t) kp.ki_rusage.ru_stime.tv_sec, | |
501 | kp.ki_rusage.ru_stime.tv_usec); | |
502 | printf_filtered ("utime, children: %jd.%06ld\n", | |
503 | (intmax_t) kp.ki_rusage_ch.ru_utime.tv_sec, | |
504 | kp.ki_rusage_ch.ru_utime.tv_usec); | |
505 | printf_filtered ("stime, children: %jd.%06ld\n", | |
506 | (intmax_t) kp.ki_rusage_ch.ru_stime.tv_sec, | |
507 | kp.ki_rusage_ch.ru_stime.tv_usec); | |
508 | printf_filtered ("'nice' value: %d\n", kp.ki_nice); | |
509 | printf_filtered ("Start time: %jd.%06ld\n", kp.ki_start.tv_sec, | |
510 | kp.ki_start.tv_usec); | |
511 | pgtok = getpagesize () / 1024; | |
512 | printf_filtered ("Virtual memory size: %ju kB\n", | |
513 | (uintmax_t) kp.ki_size / 1024); | |
514 | printf_filtered ("Data size: %ju kB\n", | |
515 | (uintmax_t) kp.ki_dsize * pgtok); | |
516 | printf_filtered ("Stack size: %ju kB\n", | |
517 | (uintmax_t) kp.ki_ssize * pgtok); | |
518 | printf_filtered ("Text size: %ju kB\n", | |
519 | (uintmax_t) kp.ki_tsize * pgtok); | |
520 | printf_filtered ("Resident set size: %ju kB\n", | |
521 | (uintmax_t) kp.ki_rssize * pgtok); | |
522 | printf_filtered ("Maximum RSS: %ju kB\n", | |
523 | (uintmax_t) kp.ki_rusage.ru_maxrss); | |
524 | printf_filtered ("Pending Signals: "); | |
525 | for (int i = 0; i < _SIG_WORDS; i++) | |
526 | printf_filtered ("%08x ", kp.ki_siglist.__bits[i]); | |
527 | printf_filtered ("\n"); | |
528 | printf_filtered ("Ignored Signals: "); | |
529 | for (int i = 0; i < _SIG_WORDS; i++) | |
530 | printf_filtered ("%08x ", kp.ki_sigignore.__bits[i]); | |
531 | printf_filtered ("\n"); | |
532 | printf_filtered ("Caught Signals: "); | |
533 | for (int i = 0; i < _SIG_WORDS; i++) | |
534 | printf_filtered ("%08x ", kp.ki_sigcatch.__bits[i]); | |
535 | printf_filtered ("\n"); | |
536 | } | |
537 | } | |
f6ac5f3d PA |
538 | |
539 | return true; | |
92fce24d JB |
540 | } |
541 | ||
f8eb6a9e JB |
542 | /* |
543 | * The current layout of siginfo_t on FreeBSD was adopted in SVN | |
544 | * revision 153154 which shipped in FreeBSD versions 7.0 and later. | |
545 | * Don't bother supporting the older layout on older kernels. The | |
546 | * older format was also never used in core dump notes. | |
547 | */ | |
548 | #if __FreeBSD_version >= 700009 | |
549 | #define USE_SIGINFO | |
550 | #endif | |
7697fc9e | 551 | |
f8eb6a9e | 552 | #ifdef USE_SIGINFO |
929edea9 JB |
553 | /* Return the size of siginfo for the current inferior. */ |
554 | ||
555 | #ifdef __LP64__ | |
556 | union sigval32 { | |
557 | int sival_int; | |
558 | uint32_t sival_ptr; | |
559 | }; | |
560 | ||
561 | /* This structure matches the naming and layout of `siginfo_t' in | |
562 | <sys/signal.h>. In particular, the `si_foo' macros defined in that | |
563 | header can be used with both types to copy fields in the `_reason' | |
564 | union. */ | |
565 | ||
566 | struct siginfo32 | |
567 | { | |
568 | int si_signo; | |
569 | int si_errno; | |
570 | int si_code; | |
571 | __pid_t si_pid; | |
572 | __uid_t si_uid; | |
573 | int si_status; | |
574 | uint32_t si_addr; | |
575 | union sigval32 si_value; | |
576 | union | |
577 | { | |
578 | struct | |
579 | { | |
580 | int _trapno; | |
581 | } _fault; | |
582 | struct | |
583 | { | |
584 | int _timerid; | |
585 | int _overrun; | |
586 | } _timer; | |
587 | struct | |
588 | { | |
589 | int _mqd; | |
590 | } _mesgq; | |
591 | struct | |
592 | { | |
593 | int32_t _band; | |
594 | } _poll; | |
595 | struct | |
596 | { | |
597 | int32_t __spare1__; | |
598 | int __spare2__[7]; | |
599 | } __spare__; | |
600 | } _reason; | |
601 | }; | |
602 | #endif | |
603 | ||
604 | static size_t | |
605 | fbsd_siginfo_size () | |
606 | { | |
607 | #ifdef __LP64__ | |
608 | struct gdbarch *gdbarch = get_frame_arch (get_current_frame ()); | |
609 | ||
610 | /* Is the inferior 32-bit? If so, use the 32-bit siginfo size. */ | |
a181c0bf | 611 | if (gdbarch_long_bit (gdbarch) == 32) |
929edea9 JB |
612 | return sizeof (struct siginfo32); |
613 | #endif | |
614 | return sizeof (siginfo_t); | |
615 | } | |
616 | ||
617 | /* Convert a native 64-bit siginfo object to a 32-bit object. Note | |
618 | that FreeBSD doesn't support writing to $_siginfo, so this only | |
619 | needs to convert one way. */ | |
620 | ||
621 | static void | |
622 | fbsd_convert_siginfo (siginfo_t *si) | |
623 | { | |
624 | #ifdef __LP64__ | |
625 | struct gdbarch *gdbarch = get_frame_arch (get_current_frame ()); | |
626 | ||
627 | /* Is the inferior 32-bit? If not, nothing to do. */ | |
a181c0bf | 628 | if (gdbarch_long_bit (gdbarch) != 32) |
929edea9 JB |
629 | return; |
630 | ||
631 | struct siginfo32 si32; | |
632 | ||
633 | si32.si_signo = si->si_signo; | |
634 | si32.si_errno = si->si_errno; | |
635 | si32.si_code = si->si_code; | |
636 | si32.si_pid = si->si_pid; | |
637 | si32.si_uid = si->si_uid; | |
638 | si32.si_status = si->si_status; | |
639 | si32.si_addr = (uintptr_t) si->si_addr; | |
640 | ||
641 | /* If sival_ptr is being used instead of sival_int on a big-endian | |
642 | platform, then sival_int will be zero since it holds the upper | |
643 | 32-bits of the pointer value. */ | |
644 | #if _BYTE_ORDER == _BIG_ENDIAN | |
645 | if (si->si_value.sival_int == 0) | |
0335ac6d | 646 | si32.si_value.sival_ptr = (uintptr_t) si->si_value.sival_ptr; |
929edea9 JB |
647 | else |
648 | si32.si_value.sival_int = si->si_value.sival_int; | |
649 | #else | |
650 | si32.si_value.sival_int = si->si_value.sival_int; | |
651 | #endif | |
652 | ||
653 | /* Always copy the spare fields and then possibly overwrite them for | |
654 | signal-specific or code-specific fields. */ | |
655 | si32._reason.__spare__.__spare1__ = si->_reason.__spare__.__spare1__; | |
656 | for (int i = 0; i < 7; i++) | |
657 | si32._reason.__spare__.__spare2__[i] = si->_reason.__spare__.__spare2__[i]; | |
658 | switch (si->si_signo) { | |
659 | case SIGILL: | |
660 | case SIGFPE: | |
661 | case SIGSEGV: | |
662 | case SIGBUS: | |
663 | si32.si_trapno = si->si_trapno; | |
664 | break; | |
665 | } | |
666 | switch (si->si_code) { | |
667 | case SI_TIMER: | |
668 | si32.si_timerid = si->si_timerid; | |
669 | si32.si_overrun = si->si_overrun; | |
670 | break; | |
671 | case SI_MESGQ: | |
672 | si32.si_mqd = si->si_mqd; | |
673 | break; | |
674 | } | |
675 | ||
676 | memcpy(si, &si32, sizeof (si32)); | |
677 | #endif | |
678 | } | |
679 | #endif | |
680 | ||
f6ac5f3d | 681 | /* Implement the "xfer_partial" target_ops method. */ |
7697fc9e | 682 | |
f6ac5f3d PA |
683 | enum target_xfer_status |
684 | fbsd_nat_target::xfer_partial (enum target_object object, | |
685 | const char *annex, gdb_byte *readbuf, | |
686 | const gdb_byte *writebuf, | |
687 | ULONGEST offset, ULONGEST len, | |
688 | ULONGEST *xfered_len) | |
7697fc9e | 689 | { |
e99b03dc | 690 | pid_t pid = inferior_ptid.pid (); |
7697fc9e JB |
691 | |
692 | switch (object) | |
693 | { | |
f8eb6a9e | 694 | #ifdef USE_SIGINFO |
929edea9 JB |
695 | case TARGET_OBJECT_SIGNAL_INFO: |
696 | { | |
697 | struct ptrace_lwpinfo pl; | |
698 | size_t siginfo_size; | |
699 | ||
700 | /* FreeBSD doesn't support writing to $_siginfo. */ | |
701 | if (writebuf != NULL) | |
702 | return TARGET_XFER_E_IO; | |
703 | ||
704 | if (inferior_ptid.lwp_p ()) | |
705 | pid = inferior_ptid.lwp (); | |
706 | ||
707 | siginfo_size = fbsd_siginfo_size (); | |
708 | if (offset > siginfo_size) | |
709 | return TARGET_XFER_E_IO; | |
710 | ||
711 | if (ptrace (PT_LWPINFO, pid, (PTRACE_TYPE_ARG3) &pl, sizeof (pl)) == -1) | |
712 | return TARGET_XFER_E_IO; | |
713 | ||
714 | if (!(pl.pl_flags & PL_FLAG_SI)) | |
715 | return TARGET_XFER_E_IO; | |
716 | ||
717 | fbsd_convert_siginfo (&pl.pl_siginfo); | |
718 | if (offset + len > siginfo_size) | |
719 | len = siginfo_size - offset; | |
720 | ||
721 | memcpy (readbuf, ((gdb_byte *) &pl.pl_siginfo) + offset, len); | |
722 | *xfered_len = len; | |
723 | return TARGET_XFER_OK; | |
724 | } | |
725 | #endif | |
f8eb6a9e | 726 | #ifdef KERN_PROC_AUXV |
7697fc9e JB |
727 | case TARGET_OBJECT_AUXV: |
728 | { | |
e4a26669 JB |
729 | gdb::byte_vector buf_storage; |
730 | gdb_byte *buf; | |
7697fc9e JB |
731 | size_t buflen; |
732 | int mib[4]; | |
733 | ||
734 | if (writebuf != NULL) | |
735 | return TARGET_XFER_E_IO; | |
736 | mib[0] = CTL_KERN; | |
737 | mib[1] = KERN_PROC; | |
738 | mib[2] = KERN_PROC_AUXV; | |
739 | mib[3] = pid; | |
740 | if (offset == 0) | |
741 | { | |
742 | buf = readbuf; | |
743 | buflen = len; | |
744 | } | |
745 | else | |
746 | { | |
747 | buflen = offset + len; | |
e4a26669 JB |
748 | buf_storage.resize (buflen); |
749 | buf = buf_storage.data (); | |
7697fc9e JB |
750 | } |
751 | if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0) | |
752 | { | |
753 | if (offset != 0) | |
754 | { | |
755 | if (buflen > offset) | |
756 | { | |
757 | buflen -= offset; | |
758 | memcpy (readbuf, buf + offset, buflen); | |
759 | } | |
760 | else | |
761 | buflen = 0; | |
762 | } | |
7697fc9e JB |
763 | *xfered_len = buflen; |
764 | return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK; | |
765 | } | |
7697fc9e JB |
766 | return TARGET_XFER_E_IO; |
767 | } | |
f8eb6a9e JB |
768 | #endif |
769 | #if defined(KERN_PROC_VMMAP) && defined(KERN_PROC_PS_STRINGS) | |
739ab2e9 SS |
770 | case TARGET_OBJECT_FREEBSD_VMMAP: |
771 | case TARGET_OBJECT_FREEBSD_PS_STRINGS: | |
772 | { | |
773 | gdb::byte_vector buf_storage; | |
774 | gdb_byte *buf; | |
775 | size_t buflen; | |
776 | int mib[4]; | |
777 | ||
778 | int proc_target; | |
779 | uint32_t struct_size; | |
780 | switch (object) | |
781 | { | |
782 | case TARGET_OBJECT_FREEBSD_VMMAP: | |
783 | proc_target = KERN_PROC_VMMAP; | |
784 | struct_size = sizeof (struct kinfo_vmentry); | |
785 | break; | |
786 | case TARGET_OBJECT_FREEBSD_PS_STRINGS: | |
787 | proc_target = KERN_PROC_PS_STRINGS; | |
788 | struct_size = sizeof (void *); | |
789 | break; | |
790 | } | |
791 | ||
792 | if (writebuf != NULL) | |
793 | return TARGET_XFER_E_IO; | |
794 | ||
795 | mib[0] = CTL_KERN; | |
796 | mib[1] = KERN_PROC; | |
797 | mib[2] = proc_target; | |
798 | mib[3] = pid; | |
799 | ||
800 | if (sysctl (mib, 4, NULL, &buflen, NULL, 0) != 0) | |
801 | return TARGET_XFER_E_IO; | |
802 | buflen += sizeof (struct_size); | |
803 | ||
804 | if (offset >= buflen) | |
805 | { | |
806 | *xfered_len = 0; | |
807 | return TARGET_XFER_EOF; | |
808 | } | |
809 | ||
810 | buf_storage.resize (buflen); | |
811 | buf = buf_storage.data (); | |
812 | ||
813 | memcpy (buf, &struct_size, sizeof (struct_size)); | |
814 | buflen -= sizeof (struct_size); | |
815 | if (sysctl (mib, 4, buf + sizeof (struct_size), &buflen, NULL, 0) != 0) | |
816 | return TARGET_XFER_E_IO; | |
817 | buflen += sizeof (struct_size); | |
818 | ||
819 | if (buflen - offset < len) | |
820 | len = buflen - offset; | |
821 | memcpy (readbuf, buf + offset, len); | |
822 | *xfered_len = len; | |
823 | return TARGET_XFER_OK; | |
824 | } | |
f8eb6a9e | 825 | #endif |
7697fc9e | 826 | default: |
f6ac5f3d PA |
827 | return inf_ptrace_target::xfer_partial (object, annex, |
828 | readbuf, writebuf, offset, | |
829 | len, xfered_len); | |
7697fc9e JB |
830 | } |
831 | } | |
7697fc9e | 832 | |
e58e05d6 | 833 | #ifdef PT_LWPINFO |
6e9567fe | 834 | static int debug_fbsd_lwp; |
386a8676 | 835 | static int debug_fbsd_nat; |
6e9567fe | 836 | |
6e9567fe JB |
837 | static void |
838 | show_fbsd_lwp_debug (struct ui_file *file, int from_tty, | |
839 | struct cmd_list_element *c, const char *value) | |
840 | { | |
841 | fprintf_filtered (file, _("Debugging of FreeBSD lwp module is %s.\n"), value); | |
842 | } | |
843 | ||
386a8676 JB |
844 | static void |
845 | show_fbsd_nat_debug (struct ui_file *file, int from_tty, | |
846 | struct cmd_list_element *c, const char *value) | |
847 | { | |
848 | fprintf_filtered (file, _("Debugging of FreeBSD native target is %s.\n"), | |
849 | value); | |
850 | } | |
851 | ||
6e9567fe JB |
852 | /* |
853 | FreeBSD's first thread support was via a "reentrant" version of libc | |
854 | (libc_r) that first shipped in 2.2.7. This library multiplexed all | |
855 | of the threads in a process onto a single kernel thread. This | |
4c7bf4f9 | 856 | library was supported via the bsd-uthread target. |
6e9567fe JB |
857 | |
858 | FreeBSD 5.1 introduced two new threading libraries that made use of | |
859 | multiple kernel threads. The first (libkse) scheduled M user | |
860 | threads onto N (<= M) kernel threads (LWPs). The second (libthr) | |
861 | bound each user thread to a dedicated kernel thread. libkse shipped | |
862 | as the default threading library (libpthread). | |
863 | ||
864 | FreeBSD 5.3 added a libthread_db to abstract the interface across | |
865 | the various thread libraries (libc_r, libkse, and libthr). | |
866 | ||
867 | FreeBSD 7.0 switched the default threading library from from libkse | |
868 | to libpthread and removed libc_r. | |
869 | ||
870 | FreeBSD 8.0 removed libkse and the in-kernel support for it. The | |
871 | only threading library supported by 8.0 and later is libthr which | |
872 | ties each user thread directly to an LWP. To simplify the | |
873 | implementation, this target only supports LWP-backed threads using | |
874 | ptrace directly rather than libthread_db. | |
875 | ||
876 | FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS. | |
877 | */ | |
878 | ||
879 | /* Return true if PTID is still active in the inferior. */ | |
880 | ||
57810aa7 | 881 | bool |
f6ac5f3d | 882 | fbsd_nat_target::thread_alive (ptid_t ptid) |
6e9567fe | 883 | { |
15a9e13e | 884 | if (ptid.lwp_p ()) |
6e9567fe JB |
885 | { |
886 | struct ptrace_lwpinfo pl; | |
887 | ||
e38504b3 | 888 | if (ptrace (PT_LWPINFO, ptid.lwp (), (caddr_t) &pl, sizeof pl) |
6e9567fe | 889 | == -1) |
57810aa7 | 890 | return false; |
6e9567fe JB |
891 | #ifdef PL_FLAG_EXITED |
892 | if (pl.pl_flags & PL_FLAG_EXITED) | |
57810aa7 | 893 | return false; |
6e9567fe JB |
894 | #endif |
895 | } | |
896 | ||
57810aa7 | 897 | return true; |
6e9567fe JB |
898 | } |
899 | ||
a068643d | 900 | /* Convert PTID to a string. */ |
6e9567fe | 901 | |
a068643d | 902 | std::string |
f6ac5f3d | 903 | fbsd_nat_target::pid_to_str (ptid_t ptid) |
6e9567fe JB |
904 | { |
905 | lwpid_t lwp; | |
906 | ||
e38504b3 | 907 | lwp = ptid.lwp (); |
6e9567fe JB |
908 | if (lwp != 0) |
909 | { | |
e99b03dc | 910 | int pid = ptid.pid (); |
6e9567fe | 911 | |
a068643d | 912 | return string_printf ("LWP %d of process %d", lwp, pid); |
6e9567fe JB |
913 | } |
914 | ||
915 | return normal_pid_to_str (ptid); | |
916 | } | |
917 | ||
918 | #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME | |
919 | /* Return the name assigned to a thread by an application. Returns | |
920 | the string in a static buffer. */ | |
921 | ||
f6ac5f3d PA |
922 | const char * |
923 | fbsd_nat_target::thread_name (struct thread_info *thr) | |
6e9567fe JB |
924 | { |
925 | struct ptrace_lwpinfo pl; | |
926 | struct kinfo_proc kp; | |
e99b03dc | 927 | int pid = thr->ptid.pid (); |
e38504b3 | 928 | long lwp = thr->ptid.lwp (); |
6e9567fe JB |
929 | static char buf[sizeof pl.pl_tdname + 1]; |
930 | ||
931 | /* Note that ptrace_lwpinfo returns the process command in pl_tdname | |
932 | if a name has not been set explicitly. Return a NULL name in | |
933 | that case. */ | |
92fce24d JB |
934 | if (!fbsd_fetch_kinfo_proc (pid, &kp)) |
935 | perror_with_name (_("Failed to fetch process information")); | |
6e9567fe JB |
936 | if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1) |
937 | perror_with_name (("ptrace")); | |
938 | if (strcmp (kp.ki_comm, pl.pl_tdname) == 0) | |
939 | return NULL; | |
940 | xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname); | |
941 | return buf; | |
942 | } | |
943 | #endif | |
944 | ||
da95a26c | 945 | /* Enable additional event reporting on new processes. |
6e9567fe | 946 | |
da95a26c JB |
947 | To catch fork events, PTRACE_FORK is set on every traced process |
948 | to enable stops on returns from fork or vfork. Note that both the | |
949 | parent and child will always stop, even if system call stops are | |
950 | not enabled. | |
951 | ||
952 | To catch LWP events, PTRACE_EVENTS is set on every traced process. | |
6e9567fe JB |
953 | This enables stops on the birth for new LWPs (excluding the "main" LWP) |
954 | and the death of LWPs (excluding the last LWP in a process). Note | |
955 | that unlike fork events, the LWP that creates a new LWP does not | |
956 | report an event. */ | |
957 | ||
958 | static void | |
da95a26c | 959 | fbsd_enable_proc_events (pid_t pid) |
6e9567fe | 960 | { |
da95a26c JB |
961 | #ifdef PT_GET_EVENT_MASK |
962 | int events; | |
963 | ||
964 | if (ptrace (PT_GET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events, | |
965 | sizeof (events)) == -1) | |
966 | perror_with_name (("ptrace")); | |
967 | events |= PTRACE_FORK | PTRACE_LWP; | |
dbaed385 JB |
968 | #ifdef PTRACE_VFORK |
969 | events |= PTRACE_VFORK; | |
970 | #endif | |
da95a26c JB |
971 | if (ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events, |
972 | sizeof (events)) == -1) | |
973 | perror_with_name (("ptrace")); | |
974 | #else | |
975 | #ifdef TDP_RFPPWAIT | |
976 | if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1) | |
977 | perror_with_name (("ptrace")); | |
978 | #endif | |
979 | #ifdef PT_LWP_EVENTS | |
6e9567fe JB |
980 | if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1) |
981 | perror_with_name (("ptrace")); | |
6e9567fe | 982 | #endif |
da95a26c JB |
983 | #endif |
984 | } | |
6e9567fe JB |
985 | |
986 | /* Add threads for any new LWPs in a process. | |
987 | ||
988 | When LWP events are used, this function is only used to detect existing | |
989 | threads when attaching to a process. On older systems, this function is | |
990 | called to discover new threads each time the thread list is updated. */ | |
991 | ||
992 | static void | |
993 | fbsd_add_threads (pid_t pid) | |
994 | { | |
6e9567fe JB |
995 | int i, nlwps; |
996 | ||
f2907e49 | 997 | gdb_assert (!in_thread_list (ptid_t (pid))); |
6e9567fe JB |
998 | nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0); |
999 | if (nlwps == -1) | |
1000 | perror_with_name (("ptrace")); | |
1001 | ||
329d5e7e | 1002 | gdb::unique_xmalloc_ptr<lwpid_t[]> lwps (XCNEWVEC (lwpid_t, nlwps)); |
6e9567fe | 1003 | |
e4a26669 | 1004 | nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps.get (), nlwps); |
6e9567fe JB |
1005 | if (nlwps == -1) |
1006 | perror_with_name (("ptrace")); | |
1007 | ||
1008 | for (i = 0; i < nlwps; i++) | |
1009 | { | |
fd79271b | 1010 | ptid_t ptid = ptid_t (pid, lwps[i], 0); |
6e9567fe JB |
1011 | |
1012 | if (!in_thread_list (ptid)) | |
1013 | { | |
1014 | #ifdef PT_LWP_EVENTS | |
1015 | struct ptrace_lwpinfo pl; | |
1016 | ||
1017 | /* Don't add exited threads. Note that this is only called | |
1018 | when attaching to a multi-threaded process. */ | |
329d5e7e | 1019 | if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1) |
6e9567fe JB |
1020 | perror_with_name (("ptrace")); |
1021 | if (pl.pl_flags & PL_FLAG_EXITED) | |
1022 | continue; | |
1023 | #endif | |
1024 | if (debug_fbsd_lwp) | |
1025 | fprintf_unfiltered (gdb_stdlog, | |
1026 | "FLWP: adding thread for LWP %u\n", | |
329d5e7e | 1027 | lwps[i]); |
6e9567fe JB |
1028 | add_thread (ptid); |
1029 | } | |
1030 | } | |
6e9567fe JB |
1031 | } |
1032 | ||
f6ac5f3d | 1033 | /* Implement the "update_thread_list" target_ops method. */ |
6e9567fe | 1034 | |
f6ac5f3d PA |
1035 | void |
1036 | fbsd_nat_target::update_thread_list () | |
6e9567fe JB |
1037 | { |
1038 | #ifdef PT_LWP_EVENTS | |
1039 | /* With support for thread events, threads are added/deleted from the | |
1040 | list as events are reported, so just try deleting exited threads. */ | |
1041 | delete_exited_threads (); | |
1042 | #else | |
1043 | prune_threads (); | |
1044 | ||
e99b03dc | 1045 | fbsd_add_threads (inferior_ptid.pid ()); |
6e9567fe JB |
1046 | #endif |
1047 | } | |
1048 | ||
e58e05d6 JB |
1049 | #ifdef TDP_RFPPWAIT |
1050 | /* | |
1051 | To catch fork events, PT_FOLLOW_FORK is set on every traced process | |
1052 | to enable stops on returns from fork or vfork. Note that both the | |
1053 | parent and child will always stop, even if system call stops are not | |
1054 | enabled. | |
1055 | ||
1056 | After a fork, both the child and parent process will stop and report | |
1057 | an event. However, there is no guarantee of order. If the parent | |
1058 | reports its stop first, then fbsd_wait explicitly waits for the new | |
1059 | child before returning. If the child reports its stop first, then | |
1060 | the event is saved on a list and ignored until the parent's stop is | |
1061 | reported. fbsd_wait could have been changed to fetch the parent PID | |
1062 | of the new child and used that to wait for the parent explicitly. | |
1063 | However, if two threads in the parent fork at the same time, then | |
1064 | the wait on the parent might return the "wrong" fork event. | |
1065 | ||
1066 | The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for | |
1067 | the new child process. This flag could be inferred by treating any | |
1068 | events for an unknown pid as a new child. | |
1069 | ||
1070 | In addition, the initial version of PT_FOLLOW_FORK did not report a | |
1071 | stop event for the parent process of a vfork until after the child | |
1072 | process executed a new program or exited. The kernel was changed to | |
1073 | defer the wait for exit or exec of the child until after posting the | |
1074 | stop event shortly after the change to introduce PL_FLAG_CHILD. | |
1075 | This could be worked around by reporting a vfork event when the | |
1076 | child event posted and ignoring the subsequent event from the | |
1077 | parent. | |
1078 | ||
1079 | This implementation requires both of these fixes for simplicity's | |
1080 | sake. FreeBSD versions newer than 9.1 contain both fixes. | |
1081 | */ | |
1082 | ||
e8c6b620 | 1083 | static std::list<ptid_t> fbsd_pending_children; |
e58e05d6 JB |
1084 | |
1085 | /* Record a new child process event that is reported before the | |
1086 | corresponding fork event in the parent. */ | |
1087 | ||
1088 | static void | |
6e9567fe | 1089 | fbsd_remember_child (ptid_t pid) |
e58e05d6 | 1090 | { |
e8c6b620 | 1091 | fbsd_pending_children.push_front (pid); |
e58e05d6 JB |
1092 | } |
1093 | ||
1094 | /* Check for a previously-recorded new child process event for PID. | |
6e9567fe | 1095 | If one is found, remove it from the list and return the PTID. */ |
e58e05d6 | 1096 | |
6e9567fe | 1097 | static ptid_t |
e58e05d6 JB |
1098 | fbsd_is_child_pending (pid_t pid) |
1099 | { | |
e8c6b620 JB |
1100 | for (auto it = fbsd_pending_children.begin (); |
1101 | it != fbsd_pending_children.end (); it++) | |
1102 | if (it->pid () == pid) | |
1103 | { | |
1104 | ptid_t ptid = *it; | |
1105 | fbsd_pending_children.erase (it); | |
1106 | return ptid; | |
1107 | } | |
6e9567fe | 1108 | return null_ptid; |
e58e05d6 | 1109 | } |
2c5c2a33 | 1110 | |
dbaed385 | 1111 | #ifndef PTRACE_VFORK |
e8c6b620 | 1112 | static std::forward_list<ptid_t> fbsd_pending_vfork_done; |
2c5c2a33 JB |
1113 | |
1114 | /* Record a pending vfork done event. */ | |
1115 | ||
1116 | static void | |
1117 | fbsd_add_vfork_done (ptid_t pid) | |
1118 | { | |
e8c6b620 | 1119 | fbsd_pending_vfork_done.push_front (pid); |
2c5c2a33 JB |
1120 | } |
1121 | ||
1122 | /* Check for a pending vfork done event for a specific PID. */ | |
1123 | ||
1124 | static int | |
1125 | fbsd_is_vfork_done_pending (pid_t pid) | |
1126 | { | |
e8c6b620 JB |
1127 | for (auto it = fbsd_pending_vfork_done.begin (); |
1128 | it != fbsd_pending_vfork_done.end (); it++) | |
1129 | if (it->pid () == pid) | |
1130 | return 1; | |
2c5c2a33 JB |
1131 | return 0; |
1132 | } | |
1133 | ||
1134 | /* Check for a pending vfork done event. If one is found, remove it | |
1135 | from the list and return the PTID. */ | |
1136 | ||
ee950322 | 1137 | static ptid_t |
2c5c2a33 JB |
1138 | fbsd_next_vfork_done (void) |
1139 | { | |
e8c6b620 | 1140 | if (!fbsd_pending_vfork_done.empty ()) |
2c5c2a33 | 1141 | { |
e8c6b620 JB |
1142 | ptid_t ptid = fbsd_pending_vfork_done.front (); |
1143 | fbsd_pending_vfork_done.pop_front (); | |
2c5c2a33 JB |
1144 | return ptid; |
1145 | } | |
1146 | return null_ptid; | |
1147 | } | |
e58e05d6 | 1148 | #endif |
dbaed385 | 1149 | #endif |
e58e05d6 | 1150 | |
f6ac5f3d | 1151 | /* Implement the "resume" target_ops method. */ |
8607ea63 | 1152 | |
f6ac5f3d PA |
1153 | void |
1154 | fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo) | |
8607ea63 | 1155 | { |
dbaed385 | 1156 | #if defined(TDP_RFPPWAIT) && !defined(PTRACE_VFORK) |
2c5c2a33 JB |
1157 | pid_t pid; |
1158 | ||
1159 | /* Don't PT_CONTINUE a process which has a pending vfork done event. */ | |
d7e15655 | 1160 | if (minus_one_ptid == ptid) |
e99b03dc | 1161 | pid = inferior_ptid.pid (); |
2c5c2a33 | 1162 | else |
e99b03dc | 1163 | pid = ptid.pid (); |
2c5c2a33 JB |
1164 | if (fbsd_is_vfork_done_pending (pid)) |
1165 | return; | |
1166 | #endif | |
8607ea63 JB |
1167 | |
1168 | if (debug_fbsd_lwp) | |
1169 | fprintf_unfiltered (gdb_stdlog, | |
1170 | "FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n", | |
e38504b3 | 1171 | ptid.pid (), ptid.lwp (), |
cc6bcb54 | 1172 | ptid.tid ()); |
15a9e13e | 1173 | if (ptid.lwp_p ()) |
8607ea63 JB |
1174 | { |
1175 | /* If ptid is a specific LWP, suspend all other LWPs in the process. */ | |
08036331 | 1176 | inferior *inf = find_inferior_ptid (ptid); |
d56060f0 | 1177 | |
08036331 | 1178 | for (thread_info *tp : inf->non_exited_threads ()) |
d56060f0 | 1179 | { |
08036331 | 1180 | int request; |
d56060f0 | 1181 | |
e38504b3 | 1182 | if (tp->ptid.lwp () == ptid.lwp ()) |
d56060f0 JB |
1183 | request = PT_RESUME; |
1184 | else | |
1185 | request = PT_SUSPEND; | |
1186 | ||
e38504b3 | 1187 | if (ptrace (request, tp->ptid.lwp (), NULL, 0) == -1) |
d56060f0 JB |
1188 | perror_with_name (("ptrace")); |
1189 | } | |
8607ea63 JB |
1190 | } |
1191 | else | |
1192 | { | |
1193 | /* If ptid is a wildcard, resume all matching threads (they won't run | |
1194 | until the process is continued however). */ | |
08036331 PA |
1195 | for (thread_info *tp : all_non_exited_threads (ptid)) |
1196 | if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1) | |
1197 | perror_with_name (("ptrace")); | |
8607ea63 JB |
1198 | ptid = inferior_ptid; |
1199 | } | |
f169cfdc JB |
1200 | |
1201 | #if __FreeBSD_version < 1200052 | |
1202 | /* When multiple threads within a process wish to report STOPPED | |
1203 | events from wait(), the kernel picks one thread event as the | |
1204 | thread event to report. The chosen thread event is retrieved via | |
1205 | PT_LWPINFO by passing the process ID as the request pid. If | |
1206 | multiple events are pending, then the subsequent wait() after | |
1207 | resuming a process will report another STOPPED event after | |
1208 | resuming the process to handle the next thread event and so on. | |
1209 | ||
1210 | A single thread event is cleared as a side effect of resuming the | |
1211 | process with PT_CONTINUE, PT_STEP, etc. In older kernels, | |
1212 | however, the request pid was used to select which thread's event | |
1213 | was cleared rather than always clearing the event that was just | |
1214 | reported. To avoid clearing the event of the wrong LWP, always | |
1215 | pass the process ID instead of an LWP ID to PT_CONTINUE or | |
1216 | PT_SYSCALL. | |
1217 | ||
1218 | In the case of stepping, the process ID cannot be used with | |
1219 | PT_STEP since it would step the thread that reported an event | |
1220 | which may not be the thread indicated by PTID. For stepping, use | |
1221 | PT_SETSTEP to enable stepping on the desired thread before | |
1222 | resuming the process via PT_CONTINUE instead of using | |
1223 | PT_STEP. */ | |
1224 | if (step) | |
1225 | { | |
1226 | if (ptrace (PT_SETSTEP, get_ptrace_pid (ptid), NULL, 0) == -1) | |
1227 | perror_with_name (("ptrace")); | |
1228 | step = 0; | |
1229 | } | |
1230 | ptid = ptid_t (ptid.pid ()); | |
1231 | #endif | |
f6ac5f3d | 1232 | inf_ptrace_target::resume (ptid, step, signo); |
8607ea63 JB |
1233 | } |
1234 | ||
7efba073 JB |
1235 | #ifdef USE_SIGTRAP_SIGINFO |
1236 | /* Handle breakpoint and trace traps reported via SIGTRAP. If the | |
1237 | trap was a breakpoint or trace trap that should be reported to the | |
1238 | core, return true. */ | |
1239 | ||
1240 | static bool | |
f6ac5f3d | 1241 | fbsd_handle_debug_trap (ptid_t ptid, const struct ptrace_lwpinfo &pl) |
7efba073 JB |
1242 | { |
1243 | ||
1244 | /* Ignore traps without valid siginfo or for signals other than | |
6d78332e JB |
1245 | SIGTRAP. |
1246 | ||
1247 | FreeBSD kernels prior to r341800 can return stale siginfo for at | |
1248 | least some events, but those events can be identified by | |
1249 | additional flags set in pl_flags. True breakpoint and | |
1250 | single-step traps should not have other flags set in | |
1251 | pl_flags. */ | |
1252 | if (pl.pl_flags != PL_FLAG_SI || pl.pl_siginfo.si_signo != SIGTRAP) | |
7efba073 JB |
1253 | return false; |
1254 | ||
1255 | /* Trace traps are either a single step or a hardware watchpoint or | |
1256 | breakpoint. */ | |
1257 | if (pl.pl_siginfo.si_code == TRAP_TRACE) | |
1258 | { | |
1259 | if (debug_fbsd_nat) | |
1260 | fprintf_unfiltered (gdb_stdlog, | |
1261 | "FNAT: trace trap for LWP %ld\n", ptid.lwp ()); | |
1262 | return true; | |
1263 | } | |
1264 | ||
1265 | if (pl.pl_siginfo.si_code == TRAP_BRKPT) | |
1266 | { | |
1267 | /* Fixup PC for the software breakpoint. */ | |
1268 | struct regcache *regcache = get_thread_regcache (ptid); | |
1269 | struct gdbarch *gdbarch = regcache->arch (); | |
1270 | int decr_pc = gdbarch_decr_pc_after_break (gdbarch); | |
1271 | ||
1272 | if (debug_fbsd_nat) | |
1273 | fprintf_unfiltered (gdb_stdlog, | |
1274 | "FNAT: sw breakpoint trap for LWP %ld\n", | |
1275 | ptid.lwp ()); | |
1276 | if (decr_pc != 0) | |
1277 | { | |
1278 | CORE_ADDR pc; | |
1279 | ||
1280 | pc = regcache_read_pc (regcache); | |
1281 | regcache_write_pc (regcache, pc - decr_pc); | |
1282 | } | |
1283 | return true; | |
1284 | } | |
1285 | ||
1286 | return false; | |
1287 | } | |
1288 | #endif | |
1289 | ||
e58e05d6 JB |
1290 | /* Wait for the child specified by PTID to do something. Return the |
1291 | process ID of the child, or MINUS_ONE_PTID in case of error; store | |
1292 | the status in *OURSTATUS. */ | |
1293 | ||
f6ac5f3d PA |
1294 | ptid_t |
1295 | fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus, | |
1296 | int target_options) | |
e58e05d6 JB |
1297 | { |
1298 | ptid_t wptid; | |
1299 | ||
1300 | while (1) | |
1301 | { | |
dbaed385 | 1302 | #ifndef PTRACE_VFORK |
2c5c2a33 | 1303 | wptid = fbsd_next_vfork_done (); |
d7e15655 | 1304 | if (wptid != null_ptid) |
2c5c2a33 JB |
1305 | { |
1306 | ourstatus->kind = TARGET_WAITKIND_VFORK_DONE; | |
1307 | return wptid; | |
1308 | } | |
dbaed385 | 1309 | #endif |
f6ac5f3d | 1310 | wptid = inf_ptrace_target::wait (ptid, ourstatus, target_options); |
e58e05d6 JB |
1311 | if (ourstatus->kind == TARGET_WAITKIND_STOPPED) |
1312 | { | |
1313 | struct ptrace_lwpinfo pl; | |
1314 | pid_t pid; | |
1315 | int status; | |
1316 | ||
e99b03dc | 1317 | pid = wptid.pid (); |
6e9567fe | 1318 | if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1) |
e58e05d6 JB |
1319 | perror_with_name (("ptrace")); |
1320 | ||
fd79271b | 1321 | wptid = ptid_t (pid, pl.pl_lwpid, 0); |
6e9567fe | 1322 | |
386a8676 JB |
1323 | if (debug_fbsd_nat) |
1324 | { | |
1325 | fprintf_unfiltered (gdb_stdlog, | |
1326 | "FNAT: stop for LWP %u event %d flags %#x\n", | |
1327 | pl.pl_lwpid, pl.pl_event, pl.pl_flags); | |
1328 | if (pl.pl_flags & PL_FLAG_SI) | |
1329 | fprintf_unfiltered (gdb_stdlog, | |
1330 | "FNAT: si_signo %u si_code %u\n", | |
1331 | pl.pl_siginfo.si_signo, | |
1332 | pl.pl_siginfo.si_code); | |
1333 | } | |
1334 | ||
6e9567fe JB |
1335 | #ifdef PT_LWP_EVENTS |
1336 | if (pl.pl_flags & PL_FLAG_EXITED) | |
1337 | { | |
1338 | /* If GDB attaches to a multi-threaded process, exiting | |
f6ac5f3d | 1339 | threads might be skipped during post_attach that |
6e9567fe JB |
1340 | have not yet reported their PL_FLAG_EXITED event. |
1341 | Ignore EXITED events for an unknown LWP. */ | |
b7a08269 PA |
1342 | thread_info *thr = find_thread_ptid (wptid); |
1343 | if (thr != nullptr) | |
6e9567fe JB |
1344 | { |
1345 | if (debug_fbsd_lwp) | |
1346 | fprintf_unfiltered (gdb_stdlog, | |
1347 | "FLWP: deleting thread for LWP %u\n", | |
1348 | pl.pl_lwpid); | |
1349 | if (print_thread_events) | |
a068643d TT |
1350 | printf_unfiltered (_("[%s exited]\n"), |
1351 | target_pid_to_str (wptid).c_str ()); | |
b7a08269 | 1352 | delete_thread (thr); |
6e9567fe JB |
1353 | } |
1354 | if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1) | |
1355 | perror_with_name (("ptrace")); | |
1356 | continue; | |
1357 | } | |
1358 | #endif | |
1359 | ||
1360 | /* Switch to an LWP PTID on the first stop in a new process. | |
1361 | This is done after handling PL_FLAG_EXITED to avoid | |
1362 | switching to an exited LWP. It is done before checking | |
1363 | PL_FLAG_BORN in case the first stop reported after | |
1364 | attaching to an existing process is a PL_FLAG_BORN | |
1365 | event. */ | |
f2907e49 | 1366 | if (in_thread_list (ptid_t (pid))) |
6e9567fe JB |
1367 | { |
1368 | if (debug_fbsd_lwp) | |
1369 | fprintf_unfiltered (gdb_stdlog, | |
1370 | "FLWP: using LWP %u for first thread\n", | |
1371 | pl.pl_lwpid); | |
f2907e49 | 1372 | thread_change_ptid (ptid_t (pid), wptid); |
6e9567fe JB |
1373 | } |
1374 | ||
1375 | #ifdef PT_LWP_EVENTS | |
1376 | if (pl.pl_flags & PL_FLAG_BORN) | |
1377 | { | |
1378 | /* If GDB attaches to a multi-threaded process, newborn | |
1379 | threads might be added by fbsd_add_threads that have | |
1380 | not yet reported their PL_FLAG_BORN event. Ignore | |
1381 | BORN events for an already-known LWP. */ | |
1382 | if (!in_thread_list (wptid)) | |
1383 | { | |
1384 | if (debug_fbsd_lwp) | |
1385 | fprintf_unfiltered (gdb_stdlog, | |
1386 | "FLWP: adding thread for LWP %u\n", | |
1387 | pl.pl_lwpid); | |
1388 | add_thread (wptid); | |
1389 | } | |
1390 | ourstatus->kind = TARGET_WAITKIND_SPURIOUS; | |
1391 | return wptid; | |
1392 | } | |
1393 | #endif | |
1394 | ||
e58e05d6 JB |
1395 | #ifdef TDP_RFPPWAIT |
1396 | if (pl.pl_flags & PL_FLAG_FORKED) | |
1397 | { | |
dbaed385 | 1398 | #ifndef PTRACE_VFORK |
e58e05d6 | 1399 | struct kinfo_proc kp; |
dbaed385 | 1400 | #endif |
6e9567fe | 1401 | ptid_t child_ptid; |
e58e05d6 JB |
1402 | pid_t child; |
1403 | ||
1404 | child = pl.pl_child_pid; | |
1405 | ourstatus->kind = TARGET_WAITKIND_FORKED; | |
dbaed385 JB |
1406 | #ifdef PTRACE_VFORK |
1407 | if (pl.pl_flags & PL_FLAG_VFORKED) | |
1408 | ourstatus->kind = TARGET_WAITKIND_VFORKED; | |
1409 | #endif | |
e58e05d6 JB |
1410 | |
1411 | /* Make sure the other end of the fork is stopped too. */ | |
6e9567fe | 1412 | child_ptid = fbsd_is_child_pending (child); |
d7e15655 | 1413 | if (child_ptid == null_ptid) |
e58e05d6 JB |
1414 | { |
1415 | pid = waitpid (child, &status, 0); | |
1416 | if (pid == -1) | |
1417 | perror_with_name (("waitpid")); | |
1418 | ||
1419 | gdb_assert (pid == child); | |
1420 | ||
1421 | if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1) | |
1422 | perror_with_name (("ptrace")); | |
1423 | ||
1424 | gdb_assert (pl.pl_flags & PL_FLAG_CHILD); | |
fd79271b | 1425 | child_ptid = ptid_t (child, pl.pl_lwpid, 0); |
e58e05d6 JB |
1426 | } |
1427 | ||
5fa14c6b | 1428 | /* Enable additional events on the child process. */ |
e99b03dc | 1429 | fbsd_enable_proc_events (child_ptid.pid ()); |
5fa14c6b | 1430 | |
dbaed385 | 1431 | #ifndef PTRACE_VFORK |
e58e05d6 JB |
1432 | /* For vfork, the child process will have the P_PPWAIT |
1433 | flag set. */ | |
92fce24d JB |
1434 | if (fbsd_fetch_kinfo_proc (child, &kp)) |
1435 | { | |
1436 | if (kp.ki_flag & P_PPWAIT) | |
1437 | ourstatus->kind = TARGET_WAITKIND_VFORKED; | |
1438 | } | |
1439 | else | |
1440 | warning (_("Failed to fetch process information")); | |
dbaed385 | 1441 | #endif |
6e9567fe | 1442 | ourstatus->value.related_pid = child_ptid; |
e58e05d6 JB |
1443 | |
1444 | return wptid; | |
1445 | } | |
1446 | ||
1447 | if (pl.pl_flags & PL_FLAG_CHILD) | |
1448 | { | |
1449 | /* Remember that this child forked, but do not report it | |
1450 | until the parent reports its corresponding fork | |
1451 | event. */ | |
6e9567fe | 1452 | fbsd_remember_child (wptid); |
e58e05d6 JB |
1453 | continue; |
1454 | } | |
dbaed385 JB |
1455 | |
1456 | #ifdef PTRACE_VFORK | |
1457 | if (pl.pl_flags & PL_FLAG_VFORK_DONE) | |
1458 | { | |
1459 | ourstatus->kind = TARGET_WAITKIND_VFORK_DONE; | |
1460 | return wptid; | |
1461 | } | |
1462 | #endif | |
e58e05d6 | 1463 | #endif |
d2b41ca0 JB |
1464 | |
1465 | #ifdef PL_FLAG_EXEC | |
1466 | if (pl.pl_flags & PL_FLAG_EXEC) | |
1467 | { | |
1468 | ourstatus->kind = TARGET_WAITKIND_EXECD; | |
1469 | ourstatus->value.execd_pathname | |
f6ac5f3d | 1470 | = xstrdup (pid_to_exec_file (pid)); |
d2b41ca0 JB |
1471 | return wptid; |
1472 | } | |
1473 | #endif | |
e6cdd38e | 1474 | |
7efba073 | 1475 | #ifdef USE_SIGTRAP_SIGINFO |
f6ac5f3d | 1476 | if (fbsd_handle_debug_trap (wptid, pl)) |
7efba073 JB |
1477 | return wptid; |
1478 | #endif | |
1479 | ||
e6cdd38e JB |
1480 | /* Note that PL_FLAG_SCE is set for any event reported while |
1481 | a thread is executing a system call in the kernel. In | |
1482 | particular, signals that interrupt a sleep in a system | |
1483 | call will report this flag as part of their event. Stops | |
1484 | explicitly for system call entry and exit always use | |
1485 | SIGTRAP, so only treat SIGTRAP events as system call | |
1486 | entry/exit events. */ | |
1487 | if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX) | |
1488 | && ourstatus->value.sig == SIGTRAP) | |
1489 | { | |
1490 | #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE | |
1491 | if (catch_syscall_enabled ()) | |
1492 | { | |
1493 | if (catching_syscall_number (pl.pl_syscall_code)) | |
1494 | { | |
1495 | if (pl.pl_flags & PL_FLAG_SCE) | |
1496 | ourstatus->kind = TARGET_WAITKIND_SYSCALL_ENTRY; | |
1497 | else | |
1498 | ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN; | |
1499 | ourstatus->value.syscall_number = pl.pl_syscall_code; | |
1500 | return wptid; | |
1501 | } | |
1502 | } | |
1503 | #endif | |
1504 | /* If the core isn't interested in this event, just | |
1505 | continue the process explicitly and wait for another | |
1506 | event. Note that PT_SYSCALL is "sticky" on FreeBSD | |
1507 | and once system call stops are enabled on a process | |
1508 | it stops for all system call entries and exits. */ | |
1509 | if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1) | |
1510 | perror_with_name (("ptrace")); | |
1511 | continue; | |
1512 | } | |
e58e05d6 JB |
1513 | } |
1514 | return wptid; | |
1515 | } | |
1516 | } | |
1517 | ||
7efba073 | 1518 | #ifdef USE_SIGTRAP_SIGINFO |
f6ac5f3d | 1519 | /* Implement the "stopped_by_sw_breakpoint" target_ops method. */ |
7efba073 | 1520 | |
57810aa7 | 1521 | bool |
f6ac5f3d | 1522 | fbsd_nat_target::stopped_by_sw_breakpoint () |
7efba073 JB |
1523 | { |
1524 | struct ptrace_lwpinfo pl; | |
1525 | ||
1526 | if (ptrace (PT_LWPINFO, get_ptrace_pid (inferior_ptid), (caddr_t) &pl, | |
1527 | sizeof pl) == -1) | |
57810aa7 | 1528 | return false; |
7efba073 | 1529 | |
6d78332e | 1530 | return (pl.pl_flags == PL_FLAG_SI |
7efba073 JB |
1531 | && pl.pl_siginfo.si_signo == SIGTRAP |
1532 | && pl.pl_siginfo.si_code == TRAP_BRKPT); | |
1533 | } | |
1534 | ||
f6ac5f3d | 1535 | /* Implement the "supports_stopped_by_sw_breakpoint" target_ops |
7efba073 JB |
1536 | method. */ |
1537 | ||
57810aa7 | 1538 | bool |
f6ac5f3d | 1539 | fbsd_nat_target::supports_stopped_by_sw_breakpoint () |
7efba073 | 1540 | { |
57810aa7 | 1541 | return true; |
7efba073 | 1542 | } |
7efba073 JB |
1543 | #endif |
1544 | ||
e58e05d6 JB |
1545 | #ifdef TDP_RFPPWAIT |
1546 | /* Target hook for follow_fork. On entry and at return inferior_ptid is | |
1547 | the ptid of the followed inferior. */ | |
1548 | ||
f6ac5f3d PA |
1549 | int |
1550 | fbsd_nat_target::follow_fork (int follow_child, int detach_fork) | |
e58e05d6 | 1551 | { |
bb2a62e6 | 1552 | if (!follow_child && detach_fork) |
e58e05d6 JB |
1553 | { |
1554 | struct thread_info *tp = inferior_thread (); | |
e99b03dc | 1555 | pid_t child_pid = tp->pending_follow.value.related_pid.pid (); |
e58e05d6 JB |
1556 | |
1557 | /* Breakpoints have already been detached from the child by | |
1558 | infrun.c. */ | |
1559 | ||
1560 | if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1) | |
1561 | perror_with_name (("ptrace")); | |
2c5c2a33 | 1562 | |
dbaed385 JB |
1563 | #ifndef PTRACE_VFORK |
1564 | if (tp->pending_follow.kind == TARGET_WAITKIND_VFORKED) | |
2c5c2a33 JB |
1565 | { |
1566 | /* We can't insert breakpoints until the child process has | |
1567 | finished with the shared memory region. The parent | |
1568 | process doesn't wait for the child process to exit or | |
1569 | exec until after it has been resumed from the ptrace stop | |
1570 | to report the fork. Once it has been resumed it doesn't | |
1571 | stop again before returning to userland, so there is no | |
1572 | reliable way to wait on the parent. | |
1573 | ||
1574 | We can't stay attached to the child to wait for an exec | |
1575 | or exit because it may invoke ptrace(PT_TRACE_ME) | |
1576 | (e.g. if the parent process is a debugger forking a new | |
1577 | child process). | |
1578 | ||
1579 | In the end, the best we can do is to make sure it runs | |
1580 | for a little while. Hopefully it will be out of range of | |
1581 | any breakpoints we reinsert. Usually this is only the | |
1582 | single-step breakpoint at vfork's return point. */ | |
1583 | ||
1584 | usleep (10000); | |
1585 | ||
1586 | /* Schedule a fake VFORK_DONE event to report on the next | |
1587 | wait. */ | |
1588 | fbsd_add_vfork_done (inferior_ptid); | |
1589 | } | |
dbaed385 | 1590 | #endif |
e58e05d6 JB |
1591 | } |
1592 | ||
1593 | return 0; | |
1594 | } | |
1595 | ||
f6ac5f3d PA |
1596 | int |
1597 | fbsd_nat_target::insert_fork_catchpoint (int pid) | |
e58e05d6 JB |
1598 | { |
1599 | return 0; | |
1600 | } | |
1601 | ||
f6ac5f3d PA |
1602 | int |
1603 | fbsd_nat_target::remove_fork_catchpoint (int pid) | |
e58e05d6 JB |
1604 | { |
1605 | return 0; | |
1606 | } | |
1607 | ||
f6ac5f3d PA |
1608 | int |
1609 | fbsd_nat_target::insert_vfork_catchpoint (int pid) | |
e58e05d6 JB |
1610 | { |
1611 | return 0; | |
1612 | } | |
1613 | ||
f6ac5f3d PA |
1614 | int |
1615 | fbsd_nat_target::remove_vfork_catchpoint (int pid) | |
e58e05d6 JB |
1616 | { |
1617 | return 0; | |
1618 | } | |
6e9567fe | 1619 | #endif |
e58e05d6 | 1620 | |
f6ac5f3d | 1621 | /* Implement the "post_startup_inferior" target_ops method. */ |
e58e05d6 | 1622 | |
f6ac5f3d PA |
1623 | void |
1624 | fbsd_nat_target::post_startup_inferior (ptid_t pid) | |
e58e05d6 | 1625 | { |
e99b03dc | 1626 | fbsd_enable_proc_events (pid.pid ()); |
e58e05d6 JB |
1627 | } |
1628 | ||
f6ac5f3d | 1629 | /* Implement the "post_attach" target_ops method. */ |
e58e05d6 | 1630 | |
f6ac5f3d PA |
1631 | void |
1632 | fbsd_nat_target::post_attach (int pid) | |
e58e05d6 | 1633 | { |
da95a26c | 1634 | fbsd_enable_proc_events (pid); |
6e9567fe JB |
1635 | fbsd_add_threads (pid); |
1636 | } | |
d2b41ca0 JB |
1637 | |
1638 | #ifdef PL_FLAG_EXEC | |
1639 | /* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes | |
1640 | will always stop after exec. */ | |
1641 | ||
f6ac5f3d PA |
1642 | int |
1643 | fbsd_nat_target::insert_exec_catchpoint (int pid) | |
d2b41ca0 JB |
1644 | { |
1645 | return 0; | |
1646 | } | |
1647 | ||
f6ac5f3d PA |
1648 | int |
1649 | fbsd_nat_target::remove_exec_catchpoint (int pid) | |
d2b41ca0 JB |
1650 | { |
1651 | return 0; | |
1652 | } | |
1653 | #endif | |
e6cdd38e JB |
1654 | |
1655 | #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE | |
f6ac5f3d PA |
1656 | int |
1657 | fbsd_nat_target::set_syscall_catchpoint (int pid, bool needed, | |
1658 | int any_count, | |
1659 | gdb::array_view<const int> syscall_counts) | |
e6cdd38e JB |
1660 | { |
1661 | ||
1662 | /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which | |
1663 | will catch all system call entries and exits. The system calls | |
1664 | are filtered by GDB rather than the kernel. */ | |
1665 | return 0; | |
1666 | } | |
1667 | #endif | |
e58e05d6 JB |
1668 | #endif |
1669 | ||
6e9567fe JB |
1670 | void |
1671 | _initialize_fbsd_nat (void) | |
1672 | { | |
1673 | #ifdef PT_LWPINFO | |
1674 | add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance, | |
1675 | &debug_fbsd_lwp, _("\ | |
1676 | Set debugging of FreeBSD lwp module."), _("\ | |
1677 | Show debugging of FreeBSD lwp module."), _("\ | |
1678 | Enables printf debugging output."), | |
1679 | NULL, | |
1680 | &show_fbsd_lwp_debug, | |
1681 | &setdebuglist, &showdebuglist); | |
386a8676 JB |
1682 | add_setshow_boolean_cmd ("fbsd-nat", class_maintenance, |
1683 | &debug_fbsd_nat, _("\ | |
1684 | Set debugging of FreeBSD native target."), _("\ | |
1685 | Show debugging of FreeBSD native target."), _("\ | |
1686 | Enables printf debugging output."), | |
1687 | NULL, | |
1688 | &show_fbsd_nat_debug, | |
1689 | &setdebuglist, &showdebuglist); | |
6e9567fe JB |
1690 | #endif |
1691 | } |