1 /* Native-dependent code for FreeBSD.
3 Copyright (C) 2002-2015 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;
262 info = xcalloc (1, sizeof *info);
265 info->next = fbsd_pending_children;
266 fbsd_pending_children = info;
269 /* Check for a previously-recorded new child process event for PID.
270 If one is found, remove it from the list. */
273 fbsd_is_child_pending (pid_t pid)
275 struct fbsd_fork_child_info *info, *prev;
278 for (info = fbsd_pending_children; info; prev = info, info = info->next)
280 if (info->child == pid)
283 fbsd_pending_children = info->next;
285 prev->next = info->next;
293 /* Fetch the external variant of the kernel's internal process
294 structure for the process PID into KP. */
297 fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
305 mib[2] = KERN_PROC_PID;
307 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
308 perror_with_name (("sysctl"));
312 /* Wait for the child specified by PTID to do something. Return the
313 process ID of the child, or MINUS_ONE_PTID in case of error; store
314 the status in *OURSTATUS. */
317 fbsd_wait (struct target_ops *ops,
318 ptid_t ptid, struct target_waitstatus *ourstatus,
325 wptid = super_wait (ops, ptid, ourstatus, target_options);
326 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
328 struct ptrace_lwpinfo pl;
332 pid = ptid_get_pid (wptid);
333 if (ptrace (PT_LWPINFO, pid, (caddr_t)&pl, sizeof pl) == -1)
334 perror_with_name (("ptrace"));
337 if (pl.pl_flags & PL_FLAG_FORKED)
339 struct kinfo_proc kp;
342 child = pl.pl_child_pid;
343 ourstatus->kind = TARGET_WAITKIND_FORKED;
344 ourstatus->value.related_pid = pid_to_ptid (child);
346 /* Make sure the other end of the fork is stopped too. */
347 if (!fbsd_is_child_pending (child))
349 pid = waitpid (child, &status, 0);
351 perror_with_name (("waitpid"));
353 gdb_assert (pid == child);
355 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
356 perror_with_name (("ptrace"));
358 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
361 /* For vfork, the child process will have the P_PPWAIT
363 fbsd_fetch_kinfo_proc (child, &kp);
364 if (kp.ki_flag & P_PPWAIT)
365 ourstatus->kind = TARGET_WAITKIND_VFORKED;
370 if (pl.pl_flags & PL_FLAG_CHILD)
372 /* Remember that this child forked, but do not report it
373 until the parent reports its corresponding fork
375 fbsd_remember_child (ptid_get_pid (wptid));
381 if (pl.pl_flags & PL_FLAG_EXEC)
383 ourstatus->kind = TARGET_WAITKIND_EXECD;
384 ourstatus->value.execd_pathname
385 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
395 /* Target hook for follow_fork. On entry and at return inferior_ptid is
396 the ptid of the followed inferior. */
399 fbsd_follow_fork (struct target_ops *ops, int follow_child,
404 struct thread_info *tp = inferior_thread ();
405 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
407 /* Breakpoints have already been detached from the child by
410 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
411 perror_with_name (("ptrace"));
418 fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
424 fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
430 fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
436 fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
441 /* Enable fork tracing for a specific process.
443 To catch fork events, PT_FOLLOW_FORK is set on every traced process
444 to enable stops on returns from fork or vfork. Note that both the
445 parent and child will always stop, even if system call stops are
449 fbsd_enable_follow_fork (pid_t pid)
451 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
452 perror_with_name (("ptrace"));
455 /* Implement the "to_post_startup_inferior" target_ops method. */
458 fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
460 fbsd_enable_follow_fork (ptid_get_pid (pid));
463 /* Implement the "to_post_attach" target_ops method. */
466 fbsd_post_attach (struct target_ops *self, int pid)
468 fbsd_enable_follow_fork (pid);
473 /* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
474 will always stop after exec. */
477 fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
483 fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
491 fbsd_nat_add_target (struct target_ops *t)
493 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
494 t->to_find_memory_regions = fbsd_find_memory_regions;
496 super_wait = t->to_wait;
497 t->to_wait = fbsd_wait;
499 t->to_follow_fork = fbsd_follow_fork;
500 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
501 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
502 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
503 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
504 t->to_post_startup_inferior = fbsd_post_startup_inferior;
505 t->to_post_attach = fbsd_post_attach;
508 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
509 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;