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)
47 static char buf[PATH_MAX];
50 #ifdef KERN_PROC_PATHNAME
56 mib[2] = KERN_PROC_PATHNAME;
59 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
63 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
64 len = readlink (name, buf, PATH_MAX - 1);
74 #ifdef HAVE_KINFO_GETVMMAP
75 /* Iterate over all the memory regions in the current inferior,
76 calling FUNC for each memory region. OBFD is passed as the last
80 fbsd_find_memory_regions (struct target_ops *self,
81 find_memory_region_ftype func, void *obfd)
83 pid_t pid = ptid_get_pid (inferior_ptid);
84 struct kinfo_vmentry *vmentl, *kve;
86 struct cleanup *cleanup;
89 vmentl = kinfo_getvmmap (pid, &nitems);
91 perror_with_name (_("Couldn't fetch VM map entries."));
92 cleanup = make_cleanup (free, vmentl);
94 for (i = 0; i < nitems; i++)
98 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
99 if (!(kve->kve_protection & KVME_PROT_READ)
100 || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
103 /* Skip segments with an invalid type. */
104 if (kve->kve_type != KVME_TYPE_DEFAULT
105 && kve->kve_type != KVME_TYPE_VNODE
106 && kve->kve_type != KVME_TYPE_SWAP
107 && kve->kve_type != KVME_TYPE_PHYS)
110 size = kve->kve_end - kve->kve_start;
113 fprintf_filtered (gdb_stdout,
114 "Save segment, %ld bytes at %s (%c%c%c)\n",
116 paddress (target_gdbarch (), kve->kve_start),
117 kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
118 kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
119 kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
122 /* Invoke the callback function to create the corefile segment.
123 Pass MODIFIED as true, we do not know the real modification state. */
124 func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
125 kve->kve_protection & KVME_PROT_WRITE,
126 kve->kve_protection & KVME_PROT_EXEC, 1, obfd);
128 do_cleanups (cleanup);
133 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
136 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
138 int resident, privateresident;
142 /* As of FreeBSD 5.0-RELEASE, the layout is described in
143 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a
144 new column was added to the procfs map. Therefore we can't use
145 fscanf since we need to support older releases too. */
146 if (fgets (buf, sizeof buf, mapfile) != NULL)
147 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
148 &resident, &privateresident, &obj, protection);
150 return (ret != 0 && ret != EOF);
153 /* Iterate over all the memory regions in the current inferior,
154 calling FUNC for each memory region. OBFD is passed as the last
158 fbsd_find_memory_regions (struct target_ops *self,
159 find_memory_region_ftype func, void *obfd)
161 pid_t pid = ptid_get_pid (inferior_ptid);
164 unsigned long start, end, size;
166 int read, write, exec;
167 struct cleanup *cleanup;
169 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid);
170 cleanup = make_cleanup (xfree, mapfilename);
171 mapfile = fopen (mapfilename, "r");
173 error (_("Couldn't open %s."), mapfilename);
174 make_cleanup_fclose (mapfile);
177 fprintf_filtered (gdb_stdout,
178 "Reading memory regions from %s\n", mapfilename);
180 /* Now iterate until end-of-file. */
181 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0]))
185 read = (strchr (protection, 'r') != 0);
186 write = (strchr (protection, 'w') != 0);
187 exec = (strchr (protection, 'x') != 0);
191 fprintf_filtered (gdb_stdout,
192 "Save segment, %ld bytes at %s (%c%c%c)\n",
193 size, paddress (target_gdbarch (), start),
199 /* Invoke the callback function to create the corefile segment.
200 Pass MODIFIED as true, we do not know the real modification state. */
201 func (start, size, read, write, exec, 1, obfd);
204 do_cleanups (cleanup);
210 static int debug_fbsd_lwp;
212 static ptid_t (*super_wait) (struct target_ops *,
214 struct target_waitstatus *,
218 show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
219 struct cmd_list_element *c, const char *value)
221 fprintf_filtered (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
224 #if defined(TDP_RFPPWAIT) || defined(HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME)
225 /* Fetch the external variant of the kernel's internal process
226 structure for the process PID into KP. */
229 fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
237 mib[2] = KERN_PROC_PID;
239 if (sysctl (mib, 4, kp, &len, NULL, 0) == -1)
240 perror_with_name (("sysctl"));
245 FreeBSD's first thread support was via a "reentrant" version of libc
246 (libc_r) that first shipped in 2.2.7. This library multiplexed all
247 of the threads in a process onto a single kernel thread. This
248 library is supported via the bsd-uthread target.
250 FreeBSD 5.1 introduced two new threading libraries that made use of
251 multiple kernel threads. The first (libkse) scheduled M user
252 threads onto N (<= M) kernel threads (LWPs). The second (libthr)
253 bound each user thread to a dedicated kernel thread. libkse shipped
254 as the default threading library (libpthread).
256 FreeBSD 5.3 added a libthread_db to abstract the interface across
257 the various thread libraries (libc_r, libkse, and libthr).
259 FreeBSD 7.0 switched the default threading library from from libkse
260 to libpthread and removed libc_r.
262 FreeBSD 8.0 removed libkse and the in-kernel support for it. The
263 only threading library supported by 8.0 and later is libthr which
264 ties each user thread directly to an LWP. To simplify the
265 implementation, this target only supports LWP-backed threads using
266 ptrace directly rather than libthread_db.
268 FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
271 /* Return true if PTID is still active in the inferior. */
274 fbsd_thread_alive (struct target_ops *ops, ptid_t ptid)
276 if (ptid_lwp_p (ptid))
278 struct ptrace_lwpinfo pl;
280 if (ptrace (PT_LWPINFO, ptid_get_lwp (ptid), (caddr_t) &pl, sizeof pl)
283 #ifdef PL_FLAG_EXITED
284 if (pl.pl_flags & PL_FLAG_EXITED)
292 /* Convert PTID to a string. Returns the string in a static
296 fbsd_pid_to_str (struct target_ops *ops, ptid_t ptid)
300 lwp = ptid_get_lwp (ptid);
304 int pid = ptid_get_pid (ptid);
306 xsnprintf (buf, sizeof buf, "LWP %d of process %d", lwp, pid);
310 return normal_pid_to_str (ptid);
313 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
314 /* Return the name assigned to a thread by an application. Returns
315 the string in a static buffer. */
318 fbsd_thread_name (struct target_ops *self, struct thread_info *thr)
320 struct ptrace_lwpinfo pl;
321 struct kinfo_proc kp;
322 int pid = ptid_get_pid (thr->ptid);
323 long lwp = ptid_get_lwp (thr->ptid);
324 static char buf[sizeof pl.pl_tdname + 1];
326 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
327 if a name has not been set explicitly. Return a NULL name in
329 fbsd_fetch_kinfo_proc (pid, &kp);
330 if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
331 perror_with_name (("ptrace"));
332 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
334 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
340 /* Enable LWP events for a specific process.
342 To catch LWP events, PT_LWP_EVENTS is set on every traced process.
343 This enables stops on the birth for new LWPs (excluding the "main" LWP)
344 and the death of LWPs (excluding the last LWP in a process). Note
345 that unlike fork events, the LWP that creates a new LWP does not
349 fbsd_enable_lwp_events (pid_t pid)
351 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
352 perror_with_name (("ptrace"));
356 /* Add threads for any new LWPs in a process.
358 When LWP events are used, this function is only used to detect existing
359 threads when attaching to a process. On older systems, this function is
360 called to discover new threads each time the thread list is updated. */
363 fbsd_add_threads (pid_t pid)
365 struct cleanup *cleanup;
369 gdb_assert (!in_thread_list (pid_to_ptid (pid)));
370 nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
372 perror_with_name (("ptrace"));
374 lwps = XCNEWVEC (lwpid_t, nlwps);
375 cleanup = make_cleanup (xfree, lwps);
377 nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps, nlwps);
379 perror_with_name (("ptrace"));
381 for (i = 0; i < nlwps; i++)
383 ptid_t ptid = ptid_build (pid, lwps[i], 0);
385 if (!in_thread_list (ptid))
388 struct ptrace_lwpinfo pl;
390 /* Don't add exited threads. Note that this is only called
391 when attaching to a multi-threaded process. */
392 if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
393 perror_with_name (("ptrace"));
394 if (pl.pl_flags & PL_FLAG_EXITED)
398 fprintf_unfiltered (gdb_stdlog,
399 "FLWP: adding thread for LWP %u\n",
404 do_cleanups (cleanup);
407 /* Implement the "to_update_thread_list" target_ops method. */
410 fbsd_update_thread_list (struct target_ops *ops)
413 /* With support for thread events, threads are added/deleted from the
414 list as events are reported, so just try deleting exited threads. */
415 delete_exited_threads ();
419 fbsd_add_threads (ptid_get_pid (inferior_ptid));
423 static void (*super_resume) (struct target_ops *,
429 resume_one_thread_cb (struct thread_info *tp, void *data)
431 ptid_t *ptid = (ptid_t *) data;
434 if (ptid_get_pid (tp->ptid) != ptid_get_pid (*ptid))
437 if (ptid_get_lwp (tp->ptid) == ptid_get_lwp (*ptid))
440 request = PT_SUSPEND;
442 if (ptrace (request, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
443 perror_with_name (("ptrace"));
448 resume_all_threads_cb (struct thread_info *tp, void *data)
450 ptid_t *filter = (ptid_t *) data;
452 if (!ptid_match (tp->ptid, *filter))
455 if (ptrace (PT_RESUME, ptid_get_lwp (tp->ptid), NULL, 0) == -1)
456 perror_with_name (("ptrace"));
460 /* Implement the "to_resume" target_ops method. */
463 fbsd_resume (struct target_ops *ops,
464 ptid_t ptid, int step, enum gdb_signal signo)
468 fprintf_unfiltered (gdb_stdlog,
469 "FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n",
470 ptid_get_pid (ptid), ptid_get_lwp (ptid),
471 ptid_get_tid (ptid));
472 if (ptid_lwp_p (ptid))
474 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
475 iterate_over_threads (resume_one_thread_cb, &ptid);
479 /* If ptid is a wildcard, resume all matching threads (they won't run
480 until the process is continued however). */
481 iterate_over_threads (resume_all_threads_cb, &ptid);
482 ptid = inferior_ptid;
484 super_resume (ops, ptid, step, signo);
489 To catch fork events, PT_FOLLOW_FORK is set on every traced process
490 to enable stops on returns from fork or vfork. Note that both the
491 parent and child will always stop, even if system call stops are not
494 After a fork, both the child and parent process will stop and report
495 an event. However, there is no guarantee of order. If the parent
496 reports its stop first, then fbsd_wait explicitly waits for the new
497 child before returning. If the child reports its stop first, then
498 the event is saved on a list and ignored until the parent's stop is
499 reported. fbsd_wait could have been changed to fetch the parent PID
500 of the new child and used that to wait for the parent explicitly.
501 However, if two threads in the parent fork at the same time, then
502 the wait on the parent might return the "wrong" fork event.
504 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
505 the new child process. This flag could be inferred by treating any
506 events for an unknown pid as a new child.
508 In addition, the initial version of PT_FOLLOW_FORK did not report a
509 stop event for the parent process of a vfork until after the child
510 process executed a new program or exited. The kernel was changed to
511 defer the wait for exit or exec of the child until after posting the
512 stop event shortly after the change to introduce PL_FLAG_CHILD.
513 This could be worked around by reporting a vfork event when the
514 child event posted and ignoring the subsequent event from the
517 This implementation requires both of these fixes for simplicity's
518 sake. FreeBSD versions newer than 9.1 contain both fixes.
521 struct fbsd_fork_child_info
523 struct fbsd_fork_child_info *next;
524 ptid_t child; /* Pid of new child. */
527 static struct fbsd_fork_child_info *fbsd_pending_children;
529 /* Record a new child process event that is reported before the
530 corresponding fork event in the parent. */
533 fbsd_remember_child (ptid_t pid)
535 struct fbsd_fork_child_info *info = XCNEW (struct fbsd_fork_child_info);
538 info->next = fbsd_pending_children;
539 fbsd_pending_children = info;
542 /* Check for a previously-recorded new child process event for PID.
543 If one is found, remove it from the list and return the PTID. */
546 fbsd_is_child_pending (pid_t pid)
548 struct fbsd_fork_child_info *info, *prev;
552 for (info = fbsd_pending_children; info; prev = info, info = info->next)
554 if (ptid_get_pid (info->child) == pid)
557 fbsd_pending_children = info->next;
559 prev->next = info->next;
569 /* Wait for the child specified by PTID to do something. Return the
570 process ID of the child, or MINUS_ONE_PTID in case of error; store
571 the status in *OURSTATUS. */
574 fbsd_wait (struct target_ops *ops,
575 ptid_t ptid, struct target_waitstatus *ourstatus,
582 wptid = super_wait (ops, ptid, ourstatus, target_options);
583 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
585 struct ptrace_lwpinfo pl;
589 pid = ptid_get_pid (wptid);
590 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
591 perror_with_name (("ptrace"));
593 wptid = ptid_build (pid, pl.pl_lwpid, 0);
596 if (pl.pl_flags & PL_FLAG_EXITED)
598 /* If GDB attaches to a multi-threaded process, exiting
599 threads might be skipped during fbsd_post_attach that
600 have not yet reported their PL_FLAG_EXITED event.
601 Ignore EXITED events for an unknown LWP. */
602 if (in_thread_list (wptid))
605 fprintf_unfiltered (gdb_stdlog,
606 "FLWP: deleting thread for LWP %u\n",
608 if (print_thread_events)
609 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str
611 delete_thread (wptid);
613 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
614 perror_with_name (("ptrace"));
619 /* Switch to an LWP PTID on the first stop in a new process.
620 This is done after handling PL_FLAG_EXITED to avoid
621 switching to an exited LWP. It is done before checking
622 PL_FLAG_BORN in case the first stop reported after
623 attaching to an existing process is a PL_FLAG_BORN
625 if (in_thread_list (pid_to_ptid (pid)))
628 fprintf_unfiltered (gdb_stdlog,
629 "FLWP: using LWP %u for first thread\n",
631 thread_change_ptid (pid_to_ptid (pid), wptid);
635 if (pl.pl_flags & PL_FLAG_BORN)
637 /* If GDB attaches to a multi-threaded process, newborn
638 threads might be added by fbsd_add_threads that have
639 not yet reported their PL_FLAG_BORN event. Ignore
640 BORN events for an already-known LWP. */
641 if (!in_thread_list (wptid))
644 fprintf_unfiltered (gdb_stdlog,
645 "FLWP: adding thread for LWP %u\n",
649 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
655 if (pl.pl_flags & PL_FLAG_FORKED)
657 struct kinfo_proc kp;
661 child = pl.pl_child_pid;
662 ourstatus->kind = TARGET_WAITKIND_FORKED;
664 /* Make sure the other end of the fork is stopped too. */
665 child_ptid = fbsd_is_child_pending (child);
666 if (ptid_equal (child_ptid, null_ptid))
668 pid = waitpid (child, &status, 0);
670 perror_with_name (("waitpid"));
672 gdb_assert (pid == child);
674 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
675 perror_with_name (("ptrace"));
677 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
678 child_ptid = ptid_build (child, pl.pl_lwpid, 0);
681 /* For vfork, the child process will have the P_PPWAIT
683 fbsd_fetch_kinfo_proc (child, &kp);
684 if (kp.ki_flag & P_PPWAIT)
685 ourstatus->kind = TARGET_WAITKIND_VFORKED;
686 ourstatus->value.related_pid = child_ptid;
691 if (pl.pl_flags & PL_FLAG_CHILD)
693 /* Remember that this child forked, but do not report it
694 until the parent reports its corresponding fork
696 fbsd_remember_child (wptid);
702 if (pl.pl_flags & PL_FLAG_EXEC)
704 ourstatus->kind = TARGET_WAITKIND_EXECD;
705 ourstatus->value.execd_pathname
706 = xstrdup (fbsd_pid_to_exec_file (NULL, pid));
716 /* Target hook for follow_fork. On entry and at return inferior_ptid is
717 the ptid of the followed inferior. */
720 fbsd_follow_fork (struct target_ops *ops, int follow_child,
725 struct thread_info *tp = inferior_thread ();
726 pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
728 /* Breakpoints have already been detached from the child by
731 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
732 perror_with_name (("ptrace"));
739 fbsd_insert_fork_catchpoint (struct target_ops *self, int pid)
745 fbsd_remove_fork_catchpoint (struct target_ops *self, int pid)
751 fbsd_insert_vfork_catchpoint (struct target_ops *self, int pid)
757 fbsd_remove_vfork_catchpoint (struct target_ops *self, int pid)
762 /* Enable fork tracing for a specific process.
764 To catch fork events, PT_FOLLOW_FORK is set on every traced process
765 to enable stops on returns from fork or vfork. Note that both the
766 parent and child will always stop, even if system call stops are
770 fbsd_enable_follow_fork (pid_t pid)
772 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
773 perror_with_name (("ptrace"));
777 /* Implement the "to_post_startup_inferior" target_ops method. */
780 fbsd_post_startup_inferior (struct target_ops *self, ptid_t pid)
783 fbsd_enable_follow_fork (ptid_get_pid (pid));
786 fbsd_enable_lwp_events (ptid_get_pid (pid));
790 /* Implement the "to_post_attach" target_ops method. */
793 fbsd_post_attach (struct target_ops *self, int pid)
796 fbsd_enable_follow_fork (pid);
799 fbsd_enable_lwp_events (pid);
801 fbsd_add_threads (pid);
805 /* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
806 will always stop after exec. */
809 fbsd_insert_exec_catchpoint (struct target_ops *self, int pid)
815 fbsd_remove_exec_catchpoint (struct target_ops *self, int pid)
823 fbsd_nat_add_target (struct target_ops *t)
825 t->to_pid_to_exec_file = fbsd_pid_to_exec_file;
826 t->to_find_memory_regions = fbsd_find_memory_regions;
828 t->to_thread_alive = fbsd_thread_alive;
829 t->to_pid_to_str = fbsd_pid_to_str;
830 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
831 t->to_thread_name = fbsd_thread_name;
833 t->to_update_thread_list = fbsd_update_thread_list;
834 t->to_has_thread_control = tc_schedlock;
835 super_resume = t->to_resume;
836 t->to_resume = fbsd_resume;
837 super_wait = t->to_wait;
838 t->to_wait = fbsd_wait;
839 t->to_post_startup_inferior = fbsd_post_startup_inferior;
840 t->to_post_attach = fbsd_post_attach;
842 t->to_follow_fork = fbsd_follow_fork;
843 t->to_insert_fork_catchpoint = fbsd_insert_fork_catchpoint;
844 t->to_remove_fork_catchpoint = fbsd_remove_fork_catchpoint;
845 t->to_insert_vfork_catchpoint = fbsd_insert_vfork_catchpoint;
846 t->to_remove_vfork_catchpoint = fbsd_remove_vfork_catchpoint;
849 t->to_insert_exec_catchpoint = fbsd_insert_exec_catchpoint;
850 t->to_remove_exec_catchpoint = fbsd_remove_exec_catchpoint;
856 /* Provide a prototype to silence -Wmissing-prototypes. */
857 extern initialize_file_ftype _initialize_fbsd_nat;
860 _initialize_fbsd_nat (void)
863 add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
864 &debug_fbsd_lwp, _("\
865 Set debugging of FreeBSD lwp module."), _("\
866 Show debugging of FreeBSD lwp module."), _("\
867 Enables printf debugging output."),
869 &show_fbsd_lwp_debug,
870 &setdebuglist, &showdebuglist);