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/>. */
26 #include "gdbthread.h"
28 #include <sys/types.h>
29 #include <sys/procfs.h>
30 #include <sys/ptrace.h>
31 #include <sys/sysctl.h>
32 #ifdef HAVE_KINFO_GETVMMAP
40 /* Return the name of a file that can be opened to get the symbols for
41 the child process identified by PID. */
44 fbsd_pid_to_exec_file (struct target_ops *self, int pid)
46 ssize_t len = PATH_MAX;
47 static char buf[PATH_MAX];
50 #ifdef KERN_PROC_PATHNAME
55 mib[2] = KERN_PROC_PATHNAME;
57 if (sysctl (mib, 4, buf, &len, NULL, 0) == 0)
61 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
62 len = readlink (name, buf, PATH_MAX - 1);
72 #ifdef HAVE_KINFO_GETVMMAP
73 /* Iterate over all the memory regions in the current inferior,
74 calling FUNC for each memory region. OBFD is passed as the last
78 fbsd_find_memory_regions (struct target_ops *self,
79 find_memory_region_ftype func, void *obfd)
81 pid_t pid = ptid_get_pid (inferior_ptid);
82 struct kinfo_vmentry *vmentl, *kve;
84 struct cleanup *cleanup;
87 vmentl = kinfo_getvmmap (pid, &nitems);
89 perror_with_name (_("Couldn't fetch VM map entries."));
90 cleanup = make_cleanup (free, vmentl);
92 for (i = 0; i < nitems; i++)
96 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
97 if (!(kve->kve_protection & KVME_PROT_READ)
98 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
101 /* Skip segments with an invalid type. */
102 if (kve->kve_type != KVME_TYPE_DEFAULT
103 && kve->kve_type != KVME_TYPE_VNODE
104 && kve->kve_type != KVME_TYPE_SWAP
105 && kve->kve_type != KVME_TYPE_PHYS)
108 size = kve->kve_end - kve->kve_start;
111 fprintf_filtered (gdb_stdout,
112 "Save segment, %ld bytes at %s (%c%c%c)\n",
114 paddress (target_gdbarch (), kve->kve_start),
115 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
116 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
117 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
120 /* Invoke the callback function to create the corefile segment.
121 Pass MODIFIED as true, we do not know the real modification state. */
122 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
123 kve->kve_protection & KVME_PROT_WRITE,
124 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
126 do_cleanups (cleanup);
131 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
134 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
136 int resident, privateresident;
140 /* As of FreeBSD 5.0-RELEASE, the layout is described in
141 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
142 new column was added to the procfs map. Therefore we can't use
143 fscanf since we need to support older releases too. */
144 if (fgets (buf, sizeof buf, mapfile) != NULL)
145 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
146 &resident, &privateresident, &obj, protection);
148 return (ret != 0 && ret != EOF);
151 /* Iterate over all the memory regions in the current inferior,
152 calling FUNC for each memory region. OBFD is passed as the last
156 fbsd_find_memory_regions (struct target_ops *self,
157 find_memory_region_ftype func, void *obfd)
159 pid_t pid = ptid_get_pid (inferior_ptid);
162 unsigned long start, end, size;
164 int read, write, exec;
165 struct cleanup *cleanup;
167 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
168 cleanup = make_cleanup (xfree, mapfilename);
169 mapfile = fopen (mapfilename, "r");
171 error (_("Couldn't open %s."), mapfilename);
172 make_cleanup_fclose (mapfile);
175 fprintf_filtered (gdb_stdout,
176 "Reading memory regions from %s\n", mapfilename);
178 /* Now iterate until end-of-file. */
179 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
183 read = (strchr (protection, 'r') != 0);
184 write = (strchr (protection, 'w') != 0);
185 exec = (strchr (protection, 'x') != 0);
189 fprintf_filtered (gdb_stdout,
190 "Save segment, %ld bytes at %s (%c%c%c)\n",
191 size, paddress (target_gdbarch (), start),
197 /* Invoke the callback function to create the corefile segment.
198 Pass MODIFIED as true, we do not know the real modification state. */
199 func (start, size, read, write, exec, 1, obfd);
202 do_cleanups (cleanup);
208 static int debug_fbsd_lwp;
210 static ptid_t (*super_wait) (struct target_ops *,
212 struct target_waitstatus *,
216 show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
217 struct cmd_list_element *c, const char *value)
219 fprintf_filtered (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
222 #if defined(TDP_RFPPWAIT) || defined(HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME)
223 /* Fetch the external variant of the kernel's internal process
224 structure for the process PID into KP. */
227 fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
235 mib[2] = KERN_PROC_PID;
237 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
238 perror_with_name (("sysctl"));
243 FreeBSD's first thread support was via a "reentrant" version of libc
244 (libc_r) that first shipped in 2.2.7. This library multiplexed all
245 of the threads in a process onto a single kernel thread. This
246 library is supported via the bsd-uthread target.
248 FreeBSD 5.1 introduced two new threading libraries that made use of
249 multiple kernel threads. The first (libkse) scheduled M user
250 threads onto N (<= M) kernel threads (LWPs). The second (libthr)
251 bound each user thread to a dedicated kernel thread. libkse shipped
252 as the default threading library (libpthread).
254 FreeBSD 5.3 added a libthread_db to abstract the interface across
255 the various thread libraries (libc_r, libkse, and libthr).
257 FreeBSD 7.0 switched the default threading library from from libkse
258 to libpthread and removed libc_r.
260 FreeBSD 8.0 removed libkse and the in-kernel support for it. The
261 only threading library supported by 8.0 and later is libthr which
262 ties each user thread directly to an LWP. To simplify the
263 implementation, this target only supports LWP-backed threads using
264 ptrace directly rather than libthread_db.
266 FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
269 /* Return true if PTID is still active in the inferior. */
272 fbsd_thread_alive (struct target_ops *ops, ptid_t ptid)
274 if (ptid_lwp_p (ptid))
276 struct ptrace_lwpinfo pl;
278 if (ptrace (PT_LWPINFO, ptid_get_lwp (ptid), (caddr_t) &pl, sizeof pl)
281 #ifdef PL_FLAG_EXITED
282 if (pl.pl_flags & PL_FLAG_EXITED)
290 /* Convert PTID to a string. Returns the string in a static
294 fbsd_pid_to_str (struct target_ops *ops, ptid_t ptid)
298 lwp = ptid_get_lwp (ptid);
302 int pid = ptid_get_pid (ptid);
304 xsnprintf (buf, sizeof buf, "process %d, LWP %d", pid, lwp);
308 return normal_pid_to_str (ptid);
311 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
312 /* Return the name assigned to a thread by an application. Returns
313 the string in a static buffer. */
316 fbsd_thread_name (struct target_ops *self, struct thread_info *thr)
318 struct ptrace_lwpinfo pl;
319 struct kinfo_proc kp;
320 int pid = ptid_get_pid (thr->ptid);
321 long lwp = ptid_get_lwp (thr->ptid);
322 static char buf[sizeof pl.pl_tdname + 1];
324 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
325 if a name has not been set explicitly. Return a NULL name in
327 fbsd_fetch_kinfo_proc (pid, &kp);
328 if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
329 perror_with_name (("ptrace"));
330 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
332 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
338 /* Enable LWP events for a specific process.
340 To catch LWP events, PT_LWP_EVENTS is set on every traced process.
341 This enables stops on the birth for new LWPs (excluding the "main" LWP)
342 and the death of LWPs (excluding the last LWP in a process). Note
343 that unlike fork events, the LWP that creates a new LWP does not
347 fbsd_enable_lwp_events (pid_t pid)
349 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
350 perror_with_name (("ptrace"));
354 /* Add threads for any new LWPs in a process.
356 When LWP events are used, this function is only used to detect existing
357 threads when attaching to a process. On older systems, this function is
358 called to discover new threads each time the thread list is updated. */
361 fbsd_add_threads (pid_t pid)
363 struct cleanup *cleanup;
367 gdb_assert (!in_thread_list (pid_to_ptid (pid)));
368 nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
370 perror_with_name (("ptrace"));
372 lwps = XCNEWVEC (lwpid_t, nlwps);
373 cleanup = make_cleanup (xfree, lwps);
375 nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps, nlwps);
377 perror_with_name (("ptrace"));
379 for (i = 0; i < nlwps; i++)
381 ptid_t ptid = ptid_build (pid, lwps[i], 0);
383 if (!in_thread_list (ptid))
386 struct ptrace_lwpinfo pl;
388 /* Don't add exited threads. Note that this is only called
389 when attaching to a multi-threaded process. */
390 if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
391 perror_with_name (("ptrace"));
392 if (pl.pl_flags & PL_FLAG_EXITED)
396 fprintf_unfiltered (gdb_stdlog,
397 "FLWP: adding thread for LWP %u\n",
402 do_cleanups (cleanup);
405 /* Implement the "to_update_thread_list" target_ops method. */
408 fbsd_update_thread_list (struct target_ops *ops)
411 /* With support for thread events, threads are added/deleted from the
412 list as events are reported, so just try deleting exited threads. */
413 delete_exited_threads ();
417 fbsd_add_threads (ptid_get_pid (inferior_ptid));
421 static void (*super_resume) (struct target_ops *,
427 resume_one_thread_cb (struct thread_info *tp, void *data)
432 if (ptid_get_pid (tp->ptid) != ptid_get_pid (*ptid))
435 if (ptid_get_lwp (tp->ptid) == ptid_get_lwp (*ptid))
438 request = PT_SUSPEND;
440 if (ptrace (request, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
441 perror_with_name (("ptrace"));
446 resume_all_threads_cb (struct thread_info *tp, void *data)
448 ptid_t *filter = data;
450 if (!ptid_match (tp->ptid, *filter))
453 if (ptrace (PT_RESUME, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
454 perror_with_name (("ptrace"));
458 /* Implement the "to_resume" target_ops method. */
461 fbsd_resume (struct target_ops *ops,
462 ptid_t ptid, int step, enum gdb_signal signo)
466 fprintf_unfiltered (gdb_stdlog,
467 "FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n",
468 ptid_get_pid (ptid), ptid_get_lwp (ptid),
469 ptid_get_tid (ptid));
470 if (ptid_lwp_p (ptid))
472 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
473 iterate_over_threads (resume_one_thread_cb, &ptid);
477 /* If ptid is a wildcard, resume all matching threads (they won't run
478 until the process is continued however). */
479 iterate_over_threads (resume_all_threads_cb, &ptid);
480 ptid = inferior_ptid;
482 super_resume (ops, ptid, step, signo);
487 To catch fork events, PT_FOLLOW_FORK is set on every traced process
488 to enable stops on returns from fork or vfork. Note that both the
489 parent and child will always stop, even if system call stops are not
492 After a fork, both the child and parent process will stop and report
493 an event. However, there is no guarantee of order. If the parent
494 reports its stop first, then fbsd_wait explicitly waits for the new
495 child before returning. If the child reports its stop first, then
496 the event is saved on a list and ignored until the parent's stop is
497 reported. fbsd_wait could have been changed to fetch the parent PID
498 of the new child and used that to wait for the parent explicitly.
499 However, if two threads in the parent fork at the same time, then
500 the wait on the parent might return the "wrong" fork event.
502 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
503 the new child process. This flag could be inferred by treating any
504 events for an unknown pid as a new child.
506 In addition, the initial version of PT_FOLLOW_FORK did not report a
507 stop event for the parent process of a vfork until after the child
508 process executed a new program or exited. The kernel was changed to
509 defer the wait for exit or exec of the child until after posting the
510 stop event shortly after the change to introduce PL_FLAG_CHILD.
511 This could be worked around by reporting a vfork event when the
512 child event posted and ignoring the subsequent event from the
515 This implementation requires both of these fixes for simplicity's
516 sake. FreeBSD versions newer than 9.1 contain both fixes.
519 struct fbsd_fork_child_info
521 struct fbsd_fork_child_info *next;
522 ptid_t child; /* Pid of new child. */
525 static struct fbsd_fork_child_info *fbsd_pending_children;
527 /* Record a new child process event that is reported before the
528 corresponding fork event in the parent. */
531 fbsd_remember_child (ptid_t pid)
533 struct fbsd_fork_child_info *info = XCNEW (struct fbsd_fork_child_info);
536 info->next = fbsd_pending_children;
537 fbsd_pending_children = info;
540 /* Check for a previously-recorded new child process event for PID.
541 If one is found, remove it from the list and return the PTID. */
544 fbsd_is_child_pending (pid_t pid)
546 struct fbsd_fork_child_info *info, *prev;
550 for (info = fbsd_pending_children; info; prev = info, info = info->next)
552 if (ptid_get_pid (info->child) == pid)
555 fbsd_pending_children = info->next;
557 prev->next = info->next;
567 /* Wait for the child specified by PTID to do something. Return the
568 process ID of the child, or MINUS_ONE_PTID in case of error; store
569 the status in *OURSTATUS. */
572 fbsd_wait (struct target_ops *ops,
573 ptid_t ptid, struct target_waitstatus *ourstatus,
580 wptid = super_wait (ops, ptid, ourstatus, target_options);
581 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
583 struct ptrace_lwpinfo pl;
587 pid = ptid_get_pid (wptid);
588 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
589 perror_with_name (("ptrace"));
591 wptid = ptid_build (pid, pl.pl_lwpid, 0);
594 if (pl.pl_flags & PL_FLAG_EXITED)
596 /* If GDB attaches to a multi-threaded process, exiting
597 threads might be skipped during fbsd_post_attach that
598 have not yet reported their PL_FLAG_EXITED event.
599 Ignore EXITED events for an unknown LWP. */
600 if (in_thread_list (wptid))
603 fprintf_unfiltered (gdb_stdlog,
604 "FLWP: deleting thread for LWP %u\n",
606 if (print_thread_events)
607 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str
609 delete_thread (wptid);
611 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
612 perror_with_name (("ptrace"));
617 /* Switch to an LWP PTID on the first stop in a new process.
618 This is done after handling PL_FLAG_EXITED to avoid
619 switching to an exited LWP. It is done before checking
620 PL_FLAG_BORN in case the first stop reported after
621 attaching to an existing process is a PL_FLAG_BORN
623 if (in_thread_list (pid_to_ptid (pid)))
626 fprintf_unfiltered (gdb_stdlog,
627 "FLWP: using LWP %u for first thread\n",
629 thread_change_ptid (pid_to_ptid (pid), wptid);
633 if (pl.pl_flags & PL_FLAG_BORN)
635 /* If GDB attaches to a multi-threaded process, newborn
636 threads might be added by fbsd_add_threads that have
637 not yet reported their PL_FLAG_BORN event. Ignore
638 BORN events for an already-known LWP. */
639 if (!in_thread_list (wptid))
642 fprintf_unfiltered (gdb_stdlog,
643 "FLWP: adding thread for LWP %u\n",
647 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
653 if (pl.pl_flags & PL_FLAG_FORKED)
655 struct kinfo_proc kp;
659 child = pl.pl_child_pid;
660 ourstatus->kind = TARGET_WAITKIND_FORKED;
662 /* Make sure the other end of the fork is stopped too. */
663 child_ptid = fbsd_is_child_pending (child);
664 if (ptid_equal (child_ptid, null_ptid))
666 pid = waitpid (child, &status, 0);
668 perror_with_name (("waitpid"));
670 gdb_assert (pid == child);
672 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
673 perror_with_name (("ptrace"));
675 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
676 child_ptid = ptid_build (child, pl.pl_lwpid, 0);
679 /* For vfork, the child process will have the P_PPWAIT
681 fbsd_fetch_kinfo_proc (child, &kp);
682 if (kp.ki_flag & P_PPWAIT)
683 ourstatus->kind = TARGET_WAITKIND_VFORKED;
684 ourstatus->value.related_pid = child_ptid;
689 if (pl.pl_flags & PL_FLAG_CHILD)
691 /* Remember that this child forked, but do not report it
692 until the parent reports its corresponding fork
694 fbsd_remember_child (wptid);
700 if (pl.pl_flags & PL_FLAG_EXEC)
702 ourstatus->kind = TARGET_WAITKIND_EXECD;
703 ourstatus->value.execd_pathname
704 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
714 /* Target hook for follow_fork. On entry and at return inferior_ptid is
715 the ptid of the followed inferior. */
718 fbsd_follow_fork (struct target_ops *ops, int follow_child,
723 struct thread_info *tp = inferior_thread ();
724 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
726 /* Breakpoints have already been detached from the child by
729 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
730 perror_with_name (("ptrace"));
737 fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
743 fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
749 fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
755 fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
760 /* Enable fork tracing for a specific process.
762 To catch fork events, PT_FOLLOW_FORK is set on every traced process
763 to enable stops on returns from fork or vfork. Note that both the
764 parent and child will always stop, even if system call stops are
768 fbsd_enable_follow_fork (pid_t pid)
770 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
771 perror_with_name (("ptrace"));
775 /* Implement the "to_post_startup_inferior" target_ops method. */
778 fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
781 fbsd_enable_follow_fork (ptid_get_pid (pid));
784 fbsd_enable_lwp_events (ptid_get_pid (pid));
788 /* Implement the "to_post_attach" target_ops method. */
791 fbsd_post_attach (struct target_ops *self, int pid)
794 fbsd_enable_follow_fork (pid);
797 fbsd_enable_lwp_events (pid);
799 fbsd_add_threads (pid);
803 /* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
804 will always stop after exec. */
807 fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
813 fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
821 fbsd_nat_add_target (struct target_ops *t)
823 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
824 t->to_find_memory_regions = fbsd_find_memory_regions;
826 t->to_thread_alive = fbsd_thread_alive;
827 t->to_pid_to_str = fbsd_pid_to_str;
828 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
829 t->to_thread_name = fbsd_thread_name;
831 t->to_update_thread_list = fbsd_update_thread_list;
832 t->to_has_thread_control = tc_schedlock;
833 super_resume = t->to_resume;
834 t->to_resume = fbsd_resume;
835 super_wait = t->to_wait;
836 t->to_wait = fbsd_wait;
837 t->to_post_startup_inferior = fbsd_post_startup_inferior;
838 t->to_post_attach = fbsd_post_attach;
840 t->to_follow_fork = fbsd_follow_fork;
841 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
842 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
843 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
844 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
847 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
848 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;
854 /* Provide a prototype to silence -Wmissing-prototypes. */
855 extern initialize_file_ftype _initialize_fbsd_nat;
858 _initialize_fbsd_nat (void)
861 add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
862 &debug_fbsd_lwp, _("\
863 Set debugging of FreeBSD lwp module."), _("\
864 Show debugging of FreeBSD lwp module."), _("\
865 Enables printf debugging output."),
867 &show_fbsd_lwp_debug,
868 &setdebuglist, &showdebuglist);