1 /* Native-dependent code for FreeBSD.
3 Copyright (C) 2002-2016 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 3 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, see <http://www.gnu.org/licenses/>. */
25 #include "gdbthread.h"
27 #include <sys/types.h>
28 #include <sys/procfs.h>
29 #include <sys/ptrace.h>
30 #include <sys/sysctl.h>
31 #ifdef HAVE_KINFO_GETVMMAP
39 /* Return the name of a file that can be opened to get the symbols for
40 the child process identified by PID. */
43 fbsd_pid_to_exec_file (struct target_ops *self, int pid)
45 ssize_t len = PATH_MAX;
46 static char buf[PATH_MAX];
49 #ifdef KERN_PROC_PATHNAME
54 mib[2] = KERN_PROC_PATHNAME;
56 if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
60 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
61 len = readlink (name, buf, PATH_MAX - 1);
71 #ifdef HAVE_KINFO_GETVMMAP
72 /* Iterate over all the memory regions in the current inferior,
73 calling FUNC for each memory region. OBFD is passed as the last
77 fbsd_find_memory_regions (struct target_ops *self,
78 find_memory_region_ftype func, void *obfd)
80 pid_t pid = ptid_get_pid (inferior_ptid);
81 struct kinfo_vmentry *vmentl, *kve;
83 struct cleanup *cleanup;
86 vmentl = kinfo_getvmmap (pid, &nitems);
88 perror_with_name (_("Couldn't fetch VM map entries."));
89 cleanup = make_cleanup (free, vmentl);
91 for (i = 0; i < nitems; i++)
95 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
96 if (!(kve->kve_protection & KVME_PROT_READ)
97 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
100 /* Skip segments with an invalid type. */
101 if (kve->kve_type != KVME_TYPE_DEFAULT
102 && kve->kve_type != KVME_TYPE_VNODE
103 && kve->kve_type != KVME_TYPE_SWAP
104 && kve->kve_type != KVME_TYPE_PHYS)
107 size = kve->kve_end - kve->kve_start;
110 fprintf_filtered (gdb_stdout,
111 "Save segment, %ld bytes at %s (%c%c%c)\n",
113 paddress (target_gdbarch (), kve->kve_start),
114 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
115 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
116 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
119 /* Invoke the callback function to create the corefile segment.
120 Pass MODIFIED as true, we do not know the real modification state. */
121 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
122 kve->kve_protection & KVME_PROT_WRITE,
123 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
125 do_cleanups (cleanup);
130 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
133 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
135 int resident, privateresident;
139 /* As of FreeBSD 5.0-RELEASE, the layout is described in
140 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
141 new column was added to the procfs map. Therefore we can't use
142 fscanf since we need to support older releases too. */
143 if (fgets (buf, sizeof buf, mapfile) != NULL)
144 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
145 &resident, &privateresident, &obj, protection);
147 return (ret != 0 && ret != EOF);
150 /* Iterate over all the memory regions in the current inferior,
151 calling FUNC for each memory region. OBFD is passed as the last
155 fbsd_find_memory_regions (struct target_ops *self,
156 find_memory_region_ftype func, void *obfd)
158 pid_t pid = ptid_get_pid (inferior_ptid);
161 unsigned long start, end, size;
163 int read, write, exec;
164 struct cleanup *cleanup;
166 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
167 cleanup = make_cleanup (xfree, mapfilename);
168 mapfile = fopen (mapfilename, "r");
170 error (_("Couldn't open %s."), mapfilename);
171 make_cleanup_fclose (mapfile);
174 fprintf_filtered (gdb_stdout,
175 "Reading memory regions from %s\n", mapfilename);
177 /* Now iterate until end-of-file. */
178 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
182 read = (strchr (protection, 'r') != 0);
183 write = (strchr (protection, 'w') != 0);
184 exec = (strchr (protection, 'x') != 0);
188 fprintf_filtered (gdb_stdout,
189 "Save segment, %ld bytes at %s (%c%c%c)\n",
190 size, paddress (target_gdbarch (), start),
196 /* Invoke the callback function to create the corefile segment.
197 Pass MODIFIED as true, we do not know the real modification state. */
198 func (start, size, read, write, exec, 1, obfd);
201 do_cleanups (cleanup);
207 static ptid_t (*super_wait) (struct target_ops *,
209 struct target_waitstatus *,
214 To catch fork events, PT_FOLLOW_FORK is set on every traced process
215 to enable stops on returns from fork or vfork. Note that both the
216 parent and child will always stop, even if system call stops are not
219 After a fork, both the child and parent process will stop and report
220 an event. However, there is no guarantee of order. If the parent
221 reports its stop first, then fbsd_wait explicitly waits for the new
222 child before returning. If the child reports its stop first, then
223 the event is saved on a list and ignored until the parent's stop is
224 reported. fbsd_wait could have been changed to fetch the parent PID
225 of the new child and used that to wait for the parent explicitly.
226 However, if two threads in the parent fork at the same time, then
227 the wait on the parent might return the "wrong" fork event.
229 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
230 the new child process. This flag could be inferred by treating any
231 events for an unknown pid as a new child.
233 In addition, the initial version of PT_FOLLOW_FORK did not report a
234 stop event for the parent process of a vfork until after the child
235 process executed a new program or exited. The kernel was changed to
236 defer the wait for exit or exec of the child until after posting the
237 stop event shortly after the change to introduce PL_FLAG_CHILD.
238 This could be worked around by reporting a vfork event when the
239 child event posted and ignoring the subsequent event from the
242 This implementation requires both of these fixes for simplicity's
243 sake. FreeBSD versions newer than 9.1 contain both fixes.
246 struct fbsd_fork_child_info
248 struct fbsd_fork_child_info *next;
249 pid_t child; /* Pid of new child. */
252 static struct fbsd_fork_child_info *fbsd_pending_children;
254 /* Record a new child process event that is reported before the
255 corresponding fork event in the parent. */
258 fbsd_remember_child (pid_t pid)
260 struct fbsd_fork_child_info *info = XCNEW (struct fbsd_fork_child_info);
263 info->next = fbsd_pending_children;
264 fbsd_pending_children = info;
267 /* Check for a previously-recorded new child process event for PID.
268 If one is found, remove it from the list. */
271 fbsd_is_child_pending (pid_t pid)
273 struct fbsd_fork_child_info *info, *prev;
276 for (info = fbsd_pending_children; info; prev = info, info = info->next)
278 if (info->child == pid)
281 fbsd_pending_children = info->next;
283 prev->next = info->next;
291 /* Fetch the external variant of the kernel's internal process
292 structure for the process PID into KP. */
295 fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
303 mib[2] = KERN_PROC_PID;
305 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
306 perror_with_name (("sysctl"));
310 /* Wait for the child specified by PTID to do something. Return the
311 process ID of the child, or MINUS_ONE_PTID in case of error; store
312 the status in *OURSTATUS. */
315 fbsd_wait (struct target_ops *ops,
316 ptid_t ptid, struct target_waitstatus *ourstatus,
323 wptid = super_wait (ops, ptid, ourstatus, target_options);
324 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
326 struct ptrace_lwpinfo pl;
330 pid = ptid_get_pid (wptid);
331 if (ptrace (PT_LWPINFO, pid, (caddr_t)&pl, sizeof pl) == -1)
332 perror_with_name (("ptrace"));
335 if (pl.pl_flags & PL_FLAG_FORKED)
337 struct kinfo_proc kp;
340 child = pl.pl_child_pid;
341 ourstatus->kind = TARGET_WAITKIND_FORKED;
342 ourstatus->value.related_pid = pid_to_ptid (child);
344 /* Make sure the other end of the fork is stopped too. */
345 if (!fbsd_is_child_pending (child))
347 pid = waitpid (child, &status, 0);
349 perror_with_name (("waitpid"));
351 gdb_assert (pid == child);
353 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
354 perror_with_name (("ptrace"));
356 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
359 /* For vfork, the child process will have the P_PPWAIT
361 fbsd_fetch_kinfo_proc (child, &kp);
362 if (kp.ki_flag & P_PPWAIT)
363 ourstatus->kind = TARGET_WAITKIND_VFORKED;
368 if (pl.pl_flags & PL_FLAG_CHILD)
370 /* Remember that this child forked, but do not report it
371 until the parent reports its corresponding fork
373 fbsd_remember_child (ptid_get_pid (wptid));
379 if (pl.pl_flags & PL_FLAG_EXEC)
381 ourstatus->kind = TARGET_WAITKIND_EXECD;
382 ourstatus->value.execd_pathname
383 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
393 /* Target hook for follow_fork. On entry and at return inferior_ptid is
394 the ptid of the followed inferior. */
397 fbsd_follow_fork (struct target_ops *ops, int follow_child,
402 struct thread_info *tp = inferior_thread ();
403 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
405 /* Breakpoints have already been detached from the child by
408 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
409 perror_with_name (("ptrace"));
416 fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
422 fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
428 fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
434 fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
439 /* Enable fork tracing for a specific process.
441 To catch fork events, PT_FOLLOW_FORK is set on every traced process
442 to enable stops on returns from fork or vfork. Note that both the
443 parent and child will always stop, even if system call stops are
447 fbsd_enable_follow_fork (pid_t pid)
449 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
450 perror_with_name (("ptrace"));
453 /* Implement the "to_post_startup_inferior" target_ops method. */
456 fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
458 fbsd_enable_follow_fork (ptid_get_pid (pid));
461 /* Implement the "to_post_attach" target_ops method. */
464 fbsd_post_attach (struct target_ops *self, int pid)
466 fbsd_enable_follow_fork (pid);
471 /* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
472 will always stop after exec. */
475 fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
481 fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
489 fbsd_nat_add_target (struct target_ops *t)
491 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
492 t->to_find_memory_regions = fbsd_find_memory_regions;
494 super_wait = t->to_wait;
495 t->to_wait = fbsd_wait;
497 t->to_follow_fork = fbsd_follow_fork;
498 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
499 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
500 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
501 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
502 t->to_post_startup_inferior = fbsd_post_startup_inferior;
503 t->to_post_attach = fbsd_post_attach;
506 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
507 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;