1 /* Native-dependent code for FreeBSD.
3 Copyright (C) 2002-2018 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/>. */
21 #include "byte-vector.h"
27 #include "gdbthread.h"
29 #include "inf-ptrace.h"
30 #include <sys/types.h>
31 #include <sys/procfs.h>
32 #include <sys/ptrace.h>
33 #include <sys/signal.h>
34 #include <sys/sysctl.h>
36 #if defined(HAVE_KINFO_GETFILE) || defined(HAVE_KINFO_GETVMMAP)
39 #if !defined(HAVE_KINFO_GETVMMAP)
40 #include "filestuff.h"
45 #include "fbsd-tdep.h"
49 /* Return the name of a file that can be opened to get the symbols for
50 the child process identified by PID. */
53 fbsd_nat_target::pid_to_exec_file (int pid)
56 static char buf[PATH_MAX];
59 #ifdef KERN_PROC_PATHNAME
65 mib[2] = KERN_PROC_PATHNAME;
68 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
69 /* The kern.proc.pathname.<pid> sysctl returns a length of zero
70 for processes without an associated executable such as kernel
72 return buflen == 0 ? NULL : buf;
75 xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
76 len = readlink (name, buf, PATH_MAX - 1);
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
92 fbsd_nat_target::find_memory_regions (find_memory_region_ftype func,
95 pid_t pid = inferior_ptid.pid ();
96 struct kinfo_vmentry *kve;
100 gdb::unique_xmalloc_ptr<struct kinfo_vmentry>
101 vmentl (kinfo_getvmmap (pid, &nitems));
103 perror_with_name (_("Couldn't fetch VM map entries."));
105 for (i = 0, kve = vmentl.get (); i < nitems; i++, kve++)
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)
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)
119 size = kve->kve_end - kve->kve_start;
122 fprintf_filtered (gdb_stdout,
123 "Save segment, %ld bytes at %s (%c%c%c)\n",
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' : '-');
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);
141 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
144 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */
146 int resident, privateresident;
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);
158 return (ret != 0 && ret != EOF);
161 /* Iterate over all the memory regions in the current inferior,
162 calling FUNC for each memory region. OBFD is passed as the last
166 fbsd_nat_target::find_memory_regions (find_memory_region_ftype func,
169 pid_t pid = inferior_ptid.pid ();
170 unsigned long start, end, size;
172 int read, write, exec;
174 std::string mapfilename = string_printf ("/proc/%ld/map", (long) pid);
175 gdb_file_up mapfile (fopen (mapfilename.c_str (), "r"));
177 error (_("Couldn't open %s."), mapfilename.c_str ());
180 fprintf_filtered (gdb_stdout,
181 "Reading memory regions from %s\n", mapfilename.c_str ());
183 /* Now iterate until end-of-file. */
184 while (fbsd_read_mapping (mapfile.get (), &start, &end, &protection[0]))
188 read = (strchr (protection, 'r') != 0);
189 write = (strchr (protection, 'w') != 0);
190 exec = (strchr (protection, 'x') != 0);
194 fprintf_filtered (gdb_stdout,
195 "Save segment, %ld bytes at %s (%c%c%c)\n",
196 size, paddress (target_gdbarch (), start),
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);
211 /* Fetch the command line for a running process. */
213 static gdb::unique_xmalloc_ptr<char>
214 fbsd_fetch_cmdline (pid_t pid)
222 mib[2] = KERN_PROC_ARGS;
224 if (sysctl (mib, 4, NULL, &len, NULL, 0) == -1)
230 gdb::unique_xmalloc_ptr<char> cmdline ((char *) xmalloc (len));
231 if (sysctl (mib, 4, cmdline.get (), &len, NULL, 0) == -1)
237 /* Fetch the external variant of the kernel's internal process
238 structure for the process PID into KP. */
241 fbsd_fetch_kinfo_proc (pid_t pid, struct kinfo_proc *kp)
249 mib[2] = KERN_PROC_PID;
251 return (sysctl (mib, 4, kp, &len, NULL, 0) == 0);
254 /* Implement the "info_proc" target_ops method. */
257 fbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
259 #ifdef HAVE_KINFO_GETFILE
260 gdb::unique_xmalloc_ptr<struct kinfo_file> fdtbl;
263 struct kinfo_proc kp;
265 bool do_cmdline = false;
268 #ifdef HAVE_KINFO_GETFILE
269 bool do_files = false;
271 #ifdef HAVE_KINFO_GETVMMAP
272 bool do_mappings = false;
274 bool do_status = false;
283 #ifdef HAVE_KINFO_GETVMMAP
301 #ifdef HAVE_KINFO_GETFILE
310 #ifdef HAVE_KINFO_GETFILE
313 #ifdef HAVE_KINFO_GETVMMAP
319 error (_("Not supported on this target."));
322 gdb_argv built_argv (args);
323 if (built_argv.count () == 0)
325 pid = inferior_ptid.pid ();
327 error (_("No current process: you must name one."));
329 else if (built_argv.count () == 1 && isdigit (built_argv[0][0]))
330 pid = strtol (built_argv[0], NULL, 10);
332 error (_("Invalid arguments."));
334 printf_filtered (_("process %d\n"), pid);
335 #ifdef HAVE_KINFO_GETFILE
336 if (do_cwd || do_exe || do_files)
337 fdtbl.reset (kinfo_getfile (pid, &nfd));
342 gdb::unique_xmalloc_ptr<char> cmdline = fbsd_fetch_cmdline (pid);
343 if (cmdline != nullptr)
344 printf_filtered ("cmdline = '%s'\n", cmdline.get ());
346 warning (_("unable to fetch command line"));
350 const char *cwd = NULL;
351 #ifdef HAVE_KINFO_GETFILE
352 struct kinfo_file *kf = fdtbl.get ();
353 for (int i = 0; i < nfd; i++, kf++)
355 if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_CWD)
363 printf_filtered ("cwd = '%s'\n", cwd);
365 warning (_("unable to fetch current working directory"));
369 const char *exe = NULL;
370 #ifdef HAVE_KINFO_GETFILE
371 struct kinfo_file *kf = fdtbl.get ();
372 for (int i = 0; i < nfd; i++, kf++)
374 if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_TEXT)
382 exe = pid_to_exec_file (pid);
384 printf_filtered ("exe = '%s'\n", exe);
386 warning (_("unable to fetch executable path name"));
388 #ifdef HAVE_KINFO_GETFILE
391 struct kinfo_file *kf = fdtbl.get ();
395 fbsd_info_proc_files_header ();
396 for (int i = 0; i < nfd; i++, kf++)
397 fbsd_info_proc_files_entry (kf->kf_type, kf->kf_fd, kf->kf_flags,
398 kf->kf_offset, kf->kf_vnode_type,
399 kf->kf_sock_domain, kf->kf_sock_type,
400 kf->kf_sock_protocol, &kf->kf_sa_local,
401 &kf->kf_sa_peer, kf->kf_path);
404 warning (_("unable to fetch list of open files"));
407 #ifdef HAVE_KINFO_GETVMMAP
411 gdb::unique_xmalloc_ptr<struct kinfo_vmentry>
412 vmentl (kinfo_getvmmap (pid, &nvment));
414 if (vmentl != nullptr)
416 int addr_bit = TARGET_CHAR_BIT * sizeof (void *);
417 fbsd_info_proc_mappings_header (addr_bit);
419 struct kinfo_vmentry *kve = vmentl.get ();
420 for (int i = 0; i < nvment; i++, kve++)
421 fbsd_info_proc_mappings_entry (addr_bit, kve->kve_start,
422 kve->kve_end, kve->kve_offset,
423 kve->kve_flags, kve->kve_protection,
427 warning (_("unable to fetch virtual memory map"));
432 if (!fbsd_fetch_kinfo_proc (pid, &kp))
433 warning (_("Failed to fetch process information"));
439 printf_filtered ("Name: %s\n", kp.ki_comm);
446 state = "R (running)";
449 state = "T (stopped)";
452 state = "Z (zombie)";
455 state = "S (sleeping)";
458 state = "W (interrupt wait)";
461 state = "L (blocked on lock)";
464 state = "? (unknown)";
467 printf_filtered ("State: %s\n", state);
468 printf_filtered ("Parent process: %d\n", kp.ki_ppid);
469 printf_filtered ("Process group: %d\n", kp.ki_pgid);
470 printf_filtered ("Session id: %d\n", kp.ki_sid);
471 printf_filtered ("TTY: %ju\n", (uintmax_t) kp.ki_tdev);
472 printf_filtered ("TTY owner process group: %d\n", kp.ki_tpgid);
473 printf_filtered ("User IDs (real, effective, saved): %d %d %d\n",
474 kp.ki_ruid, kp.ki_uid, kp.ki_svuid);
475 printf_filtered ("Group IDs (real, effective, saved): %d %d %d\n",
476 kp.ki_rgid, kp.ki_groups[0], kp.ki_svgid);
477 printf_filtered ("Groups: ");
478 for (int i = 0; i < kp.ki_ngroups; i++)
479 printf_filtered ("%d ", kp.ki_groups[i]);
480 printf_filtered ("\n");
481 printf_filtered ("Minor faults (no memory page): %ld\n",
482 kp.ki_rusage.ru_minflt);
483 printf_filtered ("Minor faults, children: %ld\n",
484 kp.ki_rusage_ch.ru_minflt);
485 printf_filtered ("Major faults (memory page faults): %ld\n",
486 kp.ki_rusage.ru_majflt);
487 printf_filtered ("Major faults, children: %ld\n",
488 kp.ki_rusage_ch.ru_majflt);
489 printf_filtered ("utime: %jd.%06ld\n",
490 (intmax_t) kp.ki_rusage.ru_utime.tv_sec,
491 kp.ki_rusage.ru_utime.tv_usec);
492 printf_filtered ("stime: %jd.%06ld\n",
493 (intmax_t) kp.ki_rusage.ru_stime.tv_sec,
494 kp.ki_rusage.ru_stime.tv_usec);
495 printf_filtered ("utime, children: %jd.%06ld\n",
496 (intmax_t) kp.ki_rusage_ch.ru_utime.tv_sec,
497 kp.ki_rusage_ch.ru_utime.tv_usec);
498 printf_filtered ("stime, children: %jd.%06ld\n",
499 (intmax_t) kp.ki_rusage_ch.ru_stime.tv_sec,
500 kp.ki_rusage_ch.ru_stime.tv_usec);
501 printf_filtered ("'nice' value: %d\n", kp.ki_nice);
502 printf_filtered ("Start time: %jd.%06ld\n", kp.ki_start.tv_sec,
503 kp.ki_start.tv_usec);
504 pgtok = getpagesize () / 1024;
505 printf_filtered ("Virtual memory size: %ju kB\n",
506 (uintmax_t) kp.ki_size / 1024);
507 printf_filtered ("Data size: %ju kB\n",
508 (uintmax_t) kp.ki_dsize * pgtok);
509 printf_filtered ("Stack size: %ju kB\n",
510 (uintmax_t) kp.ki_ssize * pgtok);
511 printf_filtered ("Text size: %ju kB\n",
512 (uintmax_t) kp.ki_tsize * pgtok);
513 printf_filtered ("Resident set size: %ju kB\n",
514 (uintmax_t) kp.ki_rssize * pgtok);
515 printf_filtered ("Maximum RSS: %ju kB\n",
516 (uintmax_t) kp.ki_rusage.ru_maxrss);
517 printf_filtered ("Pending Signals: ");
518 for (int i = 0; i < _SIG_WORDS; i++)
519 printf_filtered ("%08x ", kp.ki_siglist.__bits[i]);
520 printf_filtered ("\n");
521 printf_filtered ("Ignored Signals: ");
522 for (int i = 0; i < _SIG_WORDS; i++)
523 printf_filtered ("%08x ", kp.ki_sigignore.__bits[i]);
524 printf_filtered ("\n");
525 printf_filtered ("Caught Signals: ");
526 for (int i = 0; i < _SIG_WORDS; i++)
527 printf_filtered ("%08x ", kp.ki_sigcatch.__bits[i]);
528 printf_filtered ("\n");
535 #ifdef KERN_PROC_AUXV
538 /* Return the size of siginfo for the current inferior. */
546 /* This structure matches the naming and layout of `siginfo_t' in
547 <sys/signal.h>. In particular, the `si_foo' macros defined in that
548 header can be used with both types to copy fields in the `_reason'
560 union sigval32 si_value;
593 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
595 /* Is the inferior 32-bit? If so, use the 32-bit siginfo size. */
596 if (gdbarch_long_bit (gdbarch) == 32)
597 return sizeof (struct siginfo32);
599 return sizeof (siginfo_t);
602 /* Convert a native 64-bit siginfo object to a 32-bit object. Note
603 that FreeBSD doesn't support writing to $_siginfo, so this only
604 needs to convert one way. */
607 fbsd_convert_siginfo (siginfo_t *si)
610 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
612 /* Is the inferior 32-bit? If not, nothing to do. */
613 if (gdbarch_long_bit (gdbarch) != 32)
616 struct siginfo32 si32;
618 si32.si_signo = si->si_signo;
619 si32.si_errno = si->si_errno;
620 si32.si_code = si->si_code;
621 si32.si_pid = si->si_pid;
622 si32.si_uid = si->si_uid;
623 si32.si_status = si->si_status;
624 si32.si_addr = (uintptr_t) si->si_addr;
626 /* If sival_ptr is being used instead of sival_int on a big-endian
627 platform, then sival_int will be zero since it holds the upper
628 32-bits of the pointer value. */
629 #if _BYTE_ORDER == _BIG_ENDIAN
630 if (si->si_value.sival_int == 0)
631 si32.si_value.sival_ptr = (uintptr_t) si->si_value.sival_ptr;
633 si32.si_value.sival_int = si->si_value.sival_int;
635 si32.si_value.sival_int = si->si_value.sival_int;
638 /* Always copy the spare fields and then possibly overwrite them for
639 signal-specific or code-specific fields. */
640 si32._reason.__spare__.__spare1__ = si->_reason.__spare__.__spare1__;
641 for (int i = 0; i < 7; i++)
642 si32._reason.__spare__.__spare2__[i] = si->_reason.__spare__.__spare2__[i];
643 switch (si->si_signo) {
648 si32.si_trapno = si->si_trapno;
651 switch (si->si_code) {
653 si32.si_timerid = si->si_timerid;
654 si32.si_overrun = si->si_overrun;
657 si32.si_mqd = si->si_mqd;
661 memcpy(si, &si32, sizeof (si32));
666 /* Implement the "xfer_partial" target_ops method. */
668 enum target_xfer_status
669 fbsd_nat_target::xfer_partial (enum target_object object,
670 const char *annex, gdb_byte *readbuf,
671 const gdb_byte *writebuf,
672 ULONGEST offset, ULONGEST len,
673 ULONGEST *xfered_len)
675 pid_t pid = inferior_ptid.pid ();
680 case TARGET_OBJECT_SIGNAL_INFO:
682 struct ptrace_lwpinfo pl;
685 /* FreeBSD doesn't support writing to $_siginfo. */
686 if (writebuf != NULL)
687 return TARGET_XFER_E_IO;
689 if (inferior_ptid.lwp_p ())
690 pid = inferior_ptid.lwp ();
692 siginfo_size = fbsd_siginfo_size ();
693 if (offset > siginfo_size)
694 return TARGET_XFER_E_IO;
696 if (ptrace (PT_LWPINFO, pid, (PTRACE_TYPE_ARG3) &pl, sizeof (pl)) == -1)
697 return TARGET_XFER_E_IO;
699 if (!(pl.pl_flags & PL_FLAG_SI))
700 return TARGET_XFER_E_IO;
702 fbsd_convert_siginfo (&pl.pl_siginfo);
703 if (offset + len > siginfo_size)
704 len = siginfo_size - offset;
706 memcpy (readbuf, ((gdb_byte *) &pl.pl_siginfo) + offset, len);
708 return TARGET_XFER_OK;
711 case TARGET_OBJECT_AUXV:
713 gdb::byte_vector buf_storage;
718 if (writebuf != NULL)
719 return TARGET_XFER_E_IO;
722 mib[2] = KERN_PROC_AUXV;
731 buflen = offset + len;
732 buf_storage.resize (buflen);
733 buf = buf_storage.data ();
735 if (sysctl (mib, 4, buf, &buflen, NULL, 0) == 0)
742 memcpy (readbuf, buf + offset, buflen);
747 *xfered_len = buflen;
748 return (buflen == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
750 return TARGET_XFER_E_IO;
752 case TARGET_OBJECT_FREEBSD_VMMAP:
753 case TARGET_OBJECT_FREEBSD_PS_STRINGS:
755 gdb::byte_vector buf_storage;
761 uint32_t struct_size;
764 case TARGET_OBJECT_FREEBSD_VMMAP:
765 proc_target = KERN_PROC_VMMAP;
766 struct_size = sizeof (struct kinfo_vmentry);
768 case TARGET_OBJECT_FREEBSD_PS_STRINGS:
769 proc_target = KERN_PROC_PS_STRINGS;
770 struct_size = sizeof (void *);
774 if (writebuf != NULL)
775 return TARGET_XFER_E_IO;
779 mib[2] = proc_target;
782 if (sysctl (mib, 4, NULL, &buflen, NULL, 0) != 0)
783 return TARGET_XFER_E_IO;
784 buflen += sizeof (struct_size);
786 if (offset >= buflen)
789 return TARGET_XFER_EOF;
792 buf_storage.resize (buflen);
793 buf = buf_storage.data ();
795 memcpy (buf, &struct_size, sizeof (struct_size));
796 buflen -= sizeof (struct_size);
797 if (sysctl (mib, 4, buf + sizeof (struct_size), &buflen, NULL, 0) != 0)
798 return TARGET_XFER_E_IO;
799 buflen += sizeof (struct_size);
801 if (buflen - offset < len)
802 len = buflen - offset;
803 memcpy (readbuf, buf + offset, len);
805 return TARGET_XFER_OK;
808 return inf_ptrace_target::xfer_partial (object, annex,
809 readbuf, writebuf, offset,
816 static int debug_fbsd_lwp;
817 static int debug_fbsd_nat;
820 show_fbsd_lwp_debug (struct ui_file *file, int from_tty,
821 struct cmd_list_element *c, const char *value)
823 fprintf_filtered (file, _("Debugging of FreeBSD lwp module is %s.\n"), value);
827 show_fbsd_nat_debug (struct ui_file *file, int from_tty,
828 struct cmd_list_element *c, const char *value)
830 fprintf_filtered (file, _("Debugging of FreeBSD native target is %s.\n"),
835 FreeBSD's first thread support was via a "reentrant" version of libc
836 (libc_r) that first shipped in 2.2.7. This library multiplexed all
837 of the threads in a process onto a single kernel thread. This
838 library was supported via the bsd-uthread target.
840 FreeBSD 5.1 introduced two new threading libraries that made use of
841 multiple kernel threads. The first (libkse) scheduled M user
842 threads onto N (<= M) kernel threads (LWPs). The second (libthr)
843 bound each user thread to a dedicated kernel thread. libkse shipped
844 as the default threading library (libpthread).
846 FreeBSD 5.3 added a libthread_db to abstract the interface across
847 the various thread libraries (libc_r, libkse, and libthr).
849 FreeBSD 7.0 switched the default threading library from from libkse
850 to libpthread and removed libc_r.
852 FreeBSD 8.0 removed libkse and the in-kernel support for it. The
853 only threading library supported by 8.0 and later is libthr which
854 ties each user thread directly to an LWP. To simplify the
855 implementation, this target only supports LWP-backed threads using
856 ptrace directly rather than libthread_db.
858 FreeBSD 11.0 introduced LWP event reporting via PT_LWP_EVENTS.
861 /* Return true if PTID is still active in the inferior. */
864 fbsd_nat_target::thread_alive (ptid_t ptid)
868 struct ptrace_lwpinfo pl;
870 if (ptrace (PT_LWPINFO, ptid.lwp (), (caddr_t) &pl, sizeof pl)
873 #ifdef PL_FLAG_EXITED
874 if (pl.pl_flags & PL_FLAG_EXITED)
882 /* Convert PTID to a string. Returns the string in a static
886 fbsd_nat_target::pid_to_str (ptid_t ptid)
894 int pid = ptid.pid ();
896 xsnprintf (buf, sizeof buf, "LWP %d of process %d", lwp, pid);
900 return normal_pid_to_str (ptid);
903 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_TDNAME
904 /* Return the name assigned to a thread by an application. Returns
905 the string in a static buffer. */
908 fbsd_nat_target::thread_name (struct thread_info *thr)
910 struct ptrace_lwpinfo pl;
911 struct kinfo_proc kp;
912 int pid = thr->ptid.pid ();
913 long lwp = thr->ptid.lwp ();
914 static char buf[sizeof pl.pl_tdname + 1];
916 /* Note that ptrace_lwpinfo returns the process command in pl_tdname
917 if a name has not been set explicitly. Return a NULL name in
919 if (!fbsd_fetch_kinfo_proc (pid, &kp))
920 perror_with_name (_("Failed to fetch process information"));
921 if (ptrace (PT_LWPINFO, lwp, (caddr_t) &pl, sizeof pl) == -1)
922 perror_with_name (("ptrace"));
923 if (strcmp (kp.ki_comm, pl.pl_tdname) == 0)
925 xsnprintf (buf, sizeof buf, "%s", pl.pl_tdname);
930 /* Enable additional event reporting on new processes.
932 To catch fork events, PTRACE_FORK is set on every traced process
933 to enable stops on returns from fork or vfork. Note that both the
934 parent and child will always stop, even if system call stops are
937 To catch LWP events, PTRACE_EVENTS is set on every traced process.
938 This enables stops on the birth for new LWPs (excluding the "main" LWP)
939 and the death of LWPs (excluding the last LWP in a process). Note
940 that unlike fork events, the LWP that creates a new LWP does not
944 fbsd_enable_proc_events (pid_t pid)
946 #ifdef PT_GET_EVENT_MASK
949 if (ptrace (PT_GET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
950 sizeof (events)) == -1)
951 perror_with_name (("ptrace"));
952 events |= PTRACE_FORK | PTRACE_LWP;
954 events |= PTRACE_VFORK;
956 if (ptrace (PT_SET_EVENT_MASK, pid, (PTRACE_TYPE_ARG3)&events,
957 sizeof (events)) == -1)
958 perror_with_name (("ptrace"));
961 if (ptrace (PT_FOLLOW_FORK, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
962 perror_with_name (("ptrace"));
965 if (ptrace (PT_LWP_EVENTS, pid, (PTRACE_TYPE_ARG3)0, 1) == -1)
966 perror_with_name (("ptrace"));
971 /* Add threads for any new LWPs in a process.
973 When LWP events are used, this function is only used to detect existing
974 threads when attaching to a process. On older systems, this function is
975 called to discover new threads each time the thread list is updated. */
978 fbsd_add_threads (pid_t pid)
982 gdb_assert (!in_thread_list (ptid_t (pid)));
983 nlwps = ptrace (PT_GETNUMLWPS, pid, NULL, 0);
985 perror_with_name (("ptrace"));
987 gdb::unique_xmalloc_ptr<lwpid_t[]> lwps (XCNEWVEC (lwpid_t, nlwps));
989 nlwps = ptrace (PT_GETLWPLIST, pid, (caddr_t) lwps.get (), nlwps);
991 perror_with_name (("ptrace"));
993 for (i = 0; i < nlwps; i++)
995 ptid_t ptid = ptid_t (pid, lwps[i], 0);
997 if (!in_thread_list (ptid))
1000 struct ptrace_lwpinfo pl;
1002 /* Don't add exited threads. Note that this is only called
1003 when attaching to a multi-threaded process. */
1004 if (ptrace (PT_LWPINFO, lwps[i], (caddr_t) &pl, sizeof pl) == -1)
1005 perror_with_name (("ptrace"));
1006 if (pl.pl_flags & PL_FLAG_EXITED)
1010 fprintf_unfiltered (gdb_stdlog,
1011 "FLWP: adding thread for LWP %u\n",
1018 /* Implement the "update_thread_list" target_ops method. */
1021 fbsd_nat_target::update_thread_list ()
1023 #ifdef PT_LWP_EVENTS
1024 /* With support for thread events, threads are added/deleted from the
1025 list as events are reported, so just try deleting exited threads. */
1026 delete_exited_threads ();
1030 fbsd_add_threads (inferior_ptid.pid ());
1036 To catch fork events, PT_FOLLOW_FORK is set on every traced process
1037 to enable stops on returns from fork or vfork. Note that both the
1038 parent and child will always stop, even if system call stops are not
1041 After a fork, both the child and parent process will stop and report
1042 an event. However, there is no guarantee of order. If the parent
1043 reports its stop first, then fbsd_wait explicitly waits for the new
1044 child before returning. If the child reports its stop first, then
1045 the event is saved on a list and ignored until the parent's stop is
1046 reported. fbsd_wait could have been changed to fetch the parent PID
1047 of the new child and used that to wait for the parent explicitly.
1048 However, if two threads in the parent fork at the same time, then
1049 the wait on the parent might return the "wrong" fork event.
1051 The initial version of PT_FOLLOW_FORK did not set PL_FLAG_CHILD for
1052 the new child process. This flag could be inferred by treating any
1053 events for an unknown pid as a new child.
1055 In addition, the initial version of PT_FOLLOW_FORK did not report a
1056 stop event for the parent process of a vfork until after the child
1057 process executed a new program or exited. The kernel was changed to
1058 defer the wait for exit or exec of the child until after posting the
1059 stop event shortly after the change to introduce PL_FLAG_CHILD.
1060 This could be worked around by reporting a vfork event when the
1061 child event posted and ignoring the subsequent event from the
1064 This implementation requires both of these fixes for simplicity's
1065 sake. FreeBSD versions newer than 9.1 contain both fixes.
1068 static std::list<ptid_t> fbsd_pending_children;
1070 /* Record a new child process event that is reported before the
1071 corresponding fork event in the parent. */
1074 fbsd_remember_child (ptid_t pid)
1076 fbsd_pending_children.push_front (pid);
1079 /* Check for a previously-recorded new child process event for PID.
1080 If one is found, remove it from the list and return the PTID. */
1083 fbsd_is_child_pending (pid_t pid)
1085 for (auto it = fbsd_pending_children.begin ();
1086 it != fbsd_pending_children.end (); it++)
1087 if (it->pid () == pid)
1090 fbsd_pending_children.erase (it);
1096 #ifndef PTRACE_VFORK
1097 static std::forward_list<ptid_t> fbsd_pending_vfork_done;
1099 /* Record a pending vfork done event. */
1102 fbsd_add_vfork_done (ptid_t pid)
1104 fbsd_pending_vfork_done.push_front (pid);
1107 /* Check for a pending vfork done event for a specific PID. */
1110 fbsd_is_vfork_done_pending (pid_t pid)
1112 for (auto it = fbsd_pending_vfork_done.begin ();
1113 it != fbsd_pending_vfork_done.end (); it++)
1114 if (it->pid () == pid)
1119 /* Check for a pending vfork done event. If one is found, remove it
1120 from the list and return the PTID. */
1123 fbsd_next_vfork_done (void)
1125 if (!fbsd_pending_vfork_done.empty ())
1127 ptid_t ptid = fbsd_pending_vfork_done.front ();
1128 fbsd_pending_vfork_done.pop_front ();
1136 /* Implement the "resume" target_ops method. */
1139 fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
1141 #if defined(TDP_RFPPWAIT) && !defined(PTRACE_VFORK)
1144 /* Don't PT_CONTINUE a process which has a pending vfork done event. */
1145 if (minus_one_ptid == ptid)
1146 pid = inferior_ptid.pid ();
1149 if (fbsd_is_vfork_done_pending (pid))
1154 fprintf_unfiltered (gdb_stdlog,
1155 "FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n",
1156 ptid.pid (), ptid.lwp (),
1160 /* If ptid is a specific LWP, suspend all other LWPs in the process. */
1161 inferior *inf = find_inferior_ptid (ptid);
1163 for (thread_info *tp : inf->non_exited_threads ())
1167 if (tp->ptid.lwp () == ptid.lwp ())
1168 request = PT_RESUME;
1170 request = PT_SUSPEND;
1172 if (ptrace (request, tp->ptid.lwp (), NULL, 0) == -1)
1173 perror_with_name (("ptrace"));
1178 /* If ptid is a wildcard, resume all matching threads (they won't run
1179 until the process is continued however). */
1180 for (thread_info *tp : all_non_exited_threads (ptid))
1181 if (ptrace (PT_RESUME, tp->ptid.lwp (), NULL, 0) == -1)
1182 perror_with_name (("ptrace"));
1183 ptid = inferior_ptid;
1186 #if __FreeBSD_version < 1200052
1187 /* When multiple threads within a process wish to report STOPPED
1188 events from wait(), the kernel picks one thread event as the
1189 thread event to report. The chosen thread event is retrieved via
1190 PT_LWPINFO by passing the process ID as the request pid. If
1191 multiple events are pending, then the subsequent wait() after
1192 resuming a process will report another STOPPED event after
1193 resuming the process to handle the next thread event and so on.
1195 A single thread event is cleared as a side effect of resuming the
1196 process with PT_CONTINUE, PT_STEP, etc. In older kernels,
1197 however, the request pid was used to select which thread's event
1198 was cleared rather than always clearing the event that was just
1199 reported. To avoid clearing the event of the wrong LWP, always
1200 pass the process ID instead of an LWP ID to PT_CONTINUE or
1203 In the case of stepping, the process ID cannot be used with
1204 PT_STEP since it would step the thread that reported an event
1205 which may not be the thread indicated by PTID. For stepping, use
1206 PT_SETSTEP to enable stepping on the desired thread before
1207 resuming the process via PT_CONTINUE instead of using
1211 if (ptrace (PT_SETSTEP, get_ptrace_pid (ptid), NULL, 0) == -1)
1212 perror_with_name (("ptrace"));
1215 ptid = ptid_t (ptid.pid ());
1217 inf_ptrace_target::resume (ptid, step, signo);
1220 #ifdef USE_SIGTRAP_SIGINFO
1221 /* Handle breakpoint and trace traps reported via SIGTRAP. If the
1222 trap was a breakpoint or trace trap that should be reported to the
1223 core, return true. */
1226 fbsd_handle_debug_trap (ptid_t ptid, const struct ptrace_lwpinfo &pl)
1229 /* Ignore traps without valid siginfo or for signals other than
1231 if (! (pl.pl_flags & PL_FLAG_SI) || pl.pl_siginfo.si_signo != SIGTRAP)
1234 /* Trace traps are either a single step or a hardware watchpoint or
1236 if (pl.pl_siginfo.si_code == TRAP_TRACE)
1239 fprintf_unfiltered (gdb_stdlog,
1240 "FNAT: trace trap for LWP %ld\n", ptid.lwp ());
1244 if (pl.pl_siginfo.si_code == TRAP_BRKPT)
1246 /* Fixup PC for the software breakpoint. */
1247 struct regcache *regcache = get_thread_regcache (ptid);
1248 struct gdbarch *gdbarch = regcache->arch ();
1249 int decr_pc = gdbarch_decr_pc_after_break (gdbarch);
1252 fprintf_unfiltered (gdb_stdlog,
1253 "FNAT: sw breakpoint trap for LWP %ld\n",
1259 pc = regcache_read_pc (regcache);
1260 regcache_write_pc (regcache, pc - decr_pc);
1269 /* Wait for the child specified by PTID to do something. Return the
1270 process ID of the child, or MINUS_ONE_PTID in case of error; store
1271 the status in *OURSTATUS. */
1274 fbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
1281 #ifndef PTRACE_VFORK
1282 wptid = fbsd_next_vfork_done ();
1283 if (wptid != null_ptid)
1285 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
1289 wptid = inf_ptrace_target::wait (ptid, ourstatus, target_options);
1290 if (ourstatus->kind == TARGET_WAITKIND_STOPPED)
1292 struct ptrace_lwpinfo pl;
1297 if (ptrace (PT_LWPINFO, pid, (caddr_t) &pl, sizeof pl) == -1)
1298 perror_with_name (("ptrace"));
1300 wptid = ptid_t (pid, pl.pl_lwpid, 0);
1304 fprintf_unfiltered (gdb_stdlog,
1305 "FNAT: stop for LWP %u event %d flags %#x\n",
1306 pl.pl_lwpid, pl.pl_event, pl.pl_flags);
1307 if (pl.pl_flags & PL_FLAG_SI)
1308 fprintf_unfiltered (gdb_stdlog,
1309 "FNAT: si_signo %u si_code %u\n",
1310 pl.pl_siginfo.si_signo,
1311 pl.pl_siginfo.si_code);
1314 #ifdef PT_LWP_EVENTS
1315 if (pl.pl_flags & PL_FLAG_EXITED)
1317 /* If GDB attaches to a multi-threaded process, exiting
1318 threads might be skipped during post_attach that
1319 have not yet reported their PL_FLAG_EXITED event.
1320 Ignore EXITED events for an unknown LWP. */
1321 thread_info *thr = find_thread_ptid (wptid);
1325 fprintf_unfiltered (gdb_stdlog,
1326 "FLWP: deleting thread for LWP %u\n",
1328 if (print_thread_events)
1329 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str
1331 delete_thread (thr);
1333 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
1334 perror_with_name (("ptrace"));
1339 /* Switch to an LWP PTID on the first stop in a new process.
1340 This is done after handling PL_FLAG_EXITED to avoid
1341 switching to an exited LWP. It is done before checking
1342 PL_FLAG_BORN in case the first stop reported after
1343 attaching to an existing process is a PL_FLAG_BORN
1345 if (in_thread_list (ptid_t (pid)))
1348 fprintf_unfiltered (gdb_stdlog,
1349 "FLWP: using LWP %u for first thread\n",
1351 thread_change_ptid (ptid_t (pid), wptid);
1354 #ifdef PT_LWP_EVENTS
1355 if (pl.pl_flags & PL_FLAG_BORN)
1357 /* If GDB attaches to a multi-threaded process, newborn
1358 threads might be added by fbsd_add_threads that have
1359 not yet reported their PL_FLAG_BORN event. Ignore
1360 BORN events for an already-known LWP. */
1361 if (!in_thread_list (wptid))
1364 fprintf_unfiltered (gdb_stdlog,
1365 "FLWP: adding thread for LWP %u\n",
1369 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1375 if (pl.pl_flags & PL_FLAG_FORKED)
1377 #ifndef PTRACE_VFORK
1378 struct kinfo_proc kp;
1383 child = pl.pl_child_pid;
1384 ourstatus->kind = TARGET_WAITKIND_FORKED;
1386 if (pl.pl_flags & PL_FLAG_VFORKED)
1387 ourstatus->kind = TARGET_WAITKIND_VFORKED;
1390 /* Make sure the other end of the fork is stopped too. */
1391 child_ptid = fbsd_is_child_pending (child);
1392 if (child_ptid == null_ptid)
1394 pid = waitpid (child, &status, 0);
1396 perror_with_name (("waitpid"));
1398 gdb_assert (pid == child);
1400 if (ptrace (PT_LWPINFO, child, (caddr_t)&pl, sizeof pl) == -1)
1401 perror_with_name (("ptrace"));
1403 gdb_assert (pl.pl_flags & PL_FLAG_CHILD);
1404 child_ptid = ptid_t (child, pl.pl_lwpid, 0);
1407 /* Enable additional events on the child process. */
1408 fbsd_enable_proc_events (child_ptid.pid ());
1410 #ifndef PTRACE_VFORK
1411 /* For vfork, the child process will have the P_PPWAIT
1413 if (fbsd_fetch_kinfo_proc (child, &kp))
1415 if (kp.ki_flag & P_PPWAIT)
1416 ourstatus->kind = TARGET_WAITKIND_VFORKED;
1419 warning (_("Failed to fetch process information"));
1421 ourstatus->value.related_pid = child_ptid;
1426 if (pl.pl_flags & PL_FLAG_CHILD)
1428 /* Remember that this child forked, but do not report it
1429 until the parent reports its corresponding fork
1431 fbsd_remember_child (wptid);
1436 if (pl.pl_flags & PL_FLAG_VFORK_DONE)
1438 ourstatus->kind = TARGET_WAITKIND_VFORK_DONE;
1445 if (pl.pl_flags & PL_FLAG_EXEC)
1447 ourstatus->kind = TARGET_WAITKIND_EXECD;
1448 ourstatus->value.execd_pathname
1449 = xstrdup (pid_to_exec_file (pid));
1454 #ifdef USE_SIGTRAP_SIGINFO
1455 if (fbsd_handle_debug_trap (wptid, pl))
1459 /* Note that PL_FLAG_SCE is set for any event reported while
1460 a thread is executing a system call in the kernel. In
1461 particular, signals that interrupt a sleep in a system
1462 call will report this flag as part of their event. Stops
1463 explicitly for system call entry and exit always use
1464 SIGTRAP, so only treat SIGTRAP events as system call
1465 entry/exit events. */
1466 if (pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)
1467 && ourstatus->value.sig == SIGTRAP)
1469 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
1470 if (catch_syscall_enabled ())
1472 if (catching_syscall_number (pl.pl_syscall_code))
1474 if (pl.pl_flags & PL_FLAG_SCE)
1475 ourstatus->kind = TARGET_WAITKIND_SYSCALL_ENTRY;
1477 ourstatus->kind = TARGET_WAITKIND_SYSCALL_RETURN;
1478 ourstatus->value.syscall_number = pl.pl_syscall_code;
1483 /* If the core isn't interested in this event, just
1484 continue the process explicitly and wait for another
1485 event. Note that PT_SYSCALL is "sticky" on FreeBSD
1486 and once system call stops are enabled on a process
1487 it stops for all system call entries and exits. */
1488 if (ptrace (PT_CONTINUE, pid, (caddr_t) 1, 0) == -1)
1489 perror_with_name (("ptrace"));
1497 #ifdef USE_SIGTRAP_SIGINFO
1498 /* Implement the "stopped_by_sw_breakpoint" target_ops method. */
1501 fbsd_nat_target::stopped_by_sw_breakpoint ()
1503 struct ptrace_lwpinfo pl;
1505 if (ptrace (PT_LWPINFO, get_ptrace_pid (inferior_ptid), (caddr_t) &pl,
1509 return ((pl.pl_flags & PL_FLAG_SI)
1510 && pl.pl_siginfo.si_signo == SIGTRAP
1511 && pl.pl_siginfo.si_code == TRAP_BRKPT);
1514 /* Implement the "supports_stopped_by_sw_breakpoint" target_ops
1518 fbsd_nat_target::supports_stopped_by_sw_breakpoint ()
1525 /* Target hook for follow_fork. On entry and at return inferior_ptid is
1526 the ptid of the followed inferior. */
1529 fbsd_nat_target::follow_fork (int follow_child, int detach_fork)
1531 if (!follow_child && detach_fork)
1533 struct thread_info *tp = inferior_thread ();
1534 pid_t child_pid = tp->pending_follow.value.related_pid.pid ();
1536 /* Breakpoints have already been detached from the child by
1539 if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
1540 perror_with_name (("ptrace"));
1542 #ifndef PTRACE_VFORK
1543 if (tp->pending_follow.kind == TARGET_WAITKIND_VFORKED)
1545 /* We can't insert breakpoints until the child process has
1546 finished with the shared memory region. The parent
1547 process doesn't wait for the child process to exit or
1548 exec until after it has been resumed from the ptrace stop
1549 to report the fork. Once it has been resumed it doesn't
1550 stop again before returning to userland, so there is no
1551 reliable way to wait on the parent.
1553 We can't stay attached to the child to wait for an exec
1554 or exit because it may invoke ptrace(PT_TRACE_ME)
1555 (e.g. if the parent process is a debugger forking a new
1558 In the end, the best we can do is to make sure it runs
1559 for a little while. Hopefully it will be out of range of
1560 any breakpoints we reinsert. Usually this is only the
1561 single-step breakpoint at vfork's return point. */
1565 /* Schedule a fake VFORK_DONE event to report on the next
1567 fbsd_add_vfork_done (inferior_ptid);
1576 fbsd_nat_target::insert_fork_catchpoint (int pid)
1582 fbsd_nat_target::remove_fork_catchpoint (int pid)
1588 fbsd_nat_target::insert_vfork_catchpoint (int pid)
1594 fbsd_nat_target::remove_vfork_catchpoint (int pid)
1600 /* Implement the "post_startup_inferior" target_ops method. */
1603 fbsd_nat_target::post_startup_inferior (ptid_t pid)
1605 fbsd_enable_proc_events (pid.pid ());
1608 /* Implement the "post_attach" target_ops method. */
1611 fbsd_nat_target::post_attach (int pid)
1613 fbsd_enable_proc_events (pid);
1614 fbsd_add_threads (pid);
1618 /* If the FreeBSD kernel supports PL_FLAG_EXEC, then traced processes
1619 will always stop after exec. */
1622 fbsd_nat_target::insert_exec_catchpoint (int pid)
1628 fbsd_nat_target::remove_exec_catchpoint (int pid)
1634 #ifdef HAVE_STRUCT_PTRACE_LWPINFO_PL_SYSCALL_CODE
1636 fbsd_nat_target::set_syscall_catchpoint (int pid, bool needed,
1638 gdb::array_view<const int> syscall_counts)
1641 /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which
1642 will catch all system call entries and exits. The system calls
1643 are filtered by GDB rather than the kernel. */
1650 _initialize_fbsd_nat (void)
1653 add_setshow_boolean_cmd ("fbsd-lwp", class_maintenance,
1654 &debug_fbsd_lwp, _("\
1655 Set debugging of FreeBSD lwp module."), _("\
1656 Show debugging of FreeBSD lwp module."), _("\
1657 Enables printf debugging output."),
1659 &show_fbsd_lwp_debug,
1660 &setdebuglist, &showdebuglist);
1661 add_setshow_boolean_cmd ("fbsd-nat", class_maintenance,
1662 &debug_fbsd_nat, _("\
1663 Set debugging of FreeBSD native target."), _("\
1664 Show debugging of FreeBSD native target."), _("\
1665 Enables printf debugging output."),
1667 &show_fbsd_nat_debug,
1668 &setdebuglist, &showdebuglist);