1 /* Target-dependent code for FreeBSD, architecture-independent.
3 Copyright (C) 2002-2019 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 "xml-syscall.h"
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
33 #include "fbsd-tdep.h"
35 /* This enum is derived from FreeBSD's <sys/signal.h>. */
64 FREEBSD_SIGVTALRM = 26,
66 FREEBSD_SIGWINCH = 28,
71 FREEBSD_SIGLIBRT = 33,
72 FREEBSD_SIGRTMIN = 65,
73 FREEBSD_SIGRTMAX = 126,
76 /* FreeBSD kernels 12.0 and later include a copy of the
77 'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
78 operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
79 The constants below define the offset of field members and flags in
80 this structure used by methods in this file. Note that the
81 'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
82 containing the size of the structure. */
84 #define LWPINFO_OFFSET 0x4
86 /* Offsets in ptrace_lwpinfo. */
87 #define LWPINFO_PL_FLAGS 0x8
88 #define LWPINFO64_PL_SIGINFO 0x30
89 #define LWPINFO32_PL_SIGINFO 0x2c
91 /* Flags in pl_flags. */
92 #define PL_FLAG_SI 0x20 /* siginfo is valid */
94 /* Sizes of siginfo_t. */
95 #define SIZE64_SIGINFO_T 80
96 #define SIZE32_SIGINFO_T 64
98 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
99 dump notes. See <sys/user.h> for the definition of struct
100 kinfo_vmentry. This data structure should have the same layout on
103 Note that FreeBSD 7.0 used an older version of this structure
104 (struct kinfo_ovmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
105 dump note wasn't introduced until FreeBSD 9.2. As a result, the
106 core dump note has always used the 7.1 and later structure
109 #define KVE_STRUCTSIZE 0x0
110 #define KVE_START 0x8
112 #define KVE_OFFSET 0x18
113 #define KVE_FLAGS 0x2c
114 #define KVE_PROTECTION 0x38
115 #define KVE_PATH 0x88
117 /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
118 match the KVME_PROT_* constants in <sys/user.h>. */
120 #define KINFO_VME_PROT_READ 0x00000001
121 #define KINFO_VME_PROT_WRITE 0x00000002
122 #define KINFO_VME_PROT_EXEC 0x00000004
124 /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
125 match the KVME_FLAG_* constants in <sys/user.h>. */
127 #define KINFO_VME_FLAG_COW 0x00000001
128 #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
129 #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
130 #define KINFO_VME_FLAG_SUPER 0x00000008
131 #define KINFO_VME_FLAG_GROWS_UP 0x00000010
132 #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
134 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
135 dump notes. See <sys/user.h> for the definition of struct
136 kinfo_file. This data structure should have the same layout on all
139 Note that FreeBSD 7.0 used an older version of this structure
140 (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
141 note wasn't introduced until FreeBSD 9.2. As a result, the core
142 dump note has always used the 7.1 and later structure format. */
144 #define KF_STRUCTSIZE 0x0
147 #define KF_FLAGS 0x10
148 #define KF_OFFSET 0x18
149 #define KF_VNODE_TYPE 0x20
150 #define KF_SOCK_DOMAIN 0x24
151 #define KF_SOCK_TYPE 0x28
152 #define KF_SOCK_PROTOCOL 0x2c
153 #define KF_SA_LOCAL 0x30
154 #define KF_SA_PEER 0xb0
155 #define KF_PATH 0x170
157 /* Constants for the 'kf_type' field in struct kinfo_file. These
158 match the KF_TYPE_* constants in <sys/user.h>. */
160 #define KINFO_FILE_TYPE_VNODE 1
161 #define KINFO_FILE_TYPE_SOCKET 2
162 #define KINFO_FILE_TYPE_PIPE 3
163 #define KINFO_FILE_TYPE_FIFO 4
164 #define KINFO_FILE_TYPE_KQUEUE 5
165 #define KINFO_FILE_TYPE_CRYPTO 6
166 #define KINFO_FILE_TYPE_MQUEUE 7
167 #define KINFO_FILE_TYPE_SHM 8
168 #define KINFO_FILE_TYPE_SEM 9
169 #define KINFO_FILE_TYPE_PTS 10
170 #define KINFO_FILE_TYPE_PROCDESC 11
172 /* Special values for the 'kf_fd' field in struct kinfo_file. These
173 match the KF_FD_TYPE_* constants in <sys/user.h>. */
175 #define KINFO_FILE_FD_TYPE_CWD -1
176 #define KINFO_FILE_FD_TYPE_ROOT -2
177 #define KINFO_FILE_FD_TYPE_JAIL -3
178 #define KINFO_FILE_FD_TYPE_TRACE -4
179 #define KINFO_FILE_FD_TYPE_TEXT -5
180 #define KINFO_FILE_FD_TYPE_CTTY -6
182 /* Flags in the 'kf_flags' field in struct kinfo_file. These match
183 the KF_FLAG_* constants in <sys/user.h>. */
185 #define KINFO_FILE_FLAG_READ 0x00000001
186 #define KINFO_FILE_FLAG_WRITE 0x00000002
187 #define KINFO_FILE_FLAG_APPEND 0x00000004
188 #define KINFO_FILE_FLAG_ASYNC 0x00000008
189 #define KINFO_FILE_FLAG_FSYNC 0x00000010
190 #define KINFO_FILE_FLAG_NONBLOCK 0x00000020
191 #define KINFO_FILE_FLAG_DIRECT 0x00000040
192 #define KINFO_FILE_FLAG_HASLOCK 0x00000080
193 #define KINFO_FILE_FLAG_EXEC 0x00004000
195 /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
196 These match the KF_VTYPE_* constants in <sys/user.h>. */
198 #define KINFO_FILE_VTYPE_VREG 1
199 #define KINFO_FILE_VTYPE_VDIR 2
200 #define KINFO_FILE_VTYPE_VCHR 4
201 #define KINFO_FILE_VTYPE_VLNK 5
202 #define KINFO_FILE_VTYPE_VSOCK 6
203 #define KINFO_FILE_VTYPE_VFIFO 7
205 /* Constants for socket address families. These match AF_* constants
206 in <sys/socket.h>. */
208 #define FBSD_AF_UNIX 1
209 #define FBSD_AF_INET 2
210 #define FBSD_AF_INET6 28
212 /* Constants for socket types. These match SOCK_* constants in
215 #define FBSD_SOCK_STREAM 1
216 #define FBSD_SOCK_DGRAM 2
217 #define FBSD_SOCK_SEQPACKET 5
219 /* Constants for IP protocols. These match IPPROTO_* constants in
222 #define FBSD_IPPROTO_ICMP 1
223 #define FBSD_IPPROTO_TCP 6
224 #define FBSD_IPPROTO_UDP 17
225 #define FBSD_IPPROTO_SCTP 132
227 /* Socket address structures. These have the same layout on all
228 FreeBSD architectures. In addition, multibyte fields such as IP
229 addresses are always stored in network byte order. */
231 struct fbsd_sockaddr_in
240 struct fbsd_sockaddr_in6
244 uint8_t sin6_port[2];
245 uint32_t sin6_flowinfo;
246 uint8_t sin6_addr[16];
247 uint32_t sin6_scope_id;
250 struct fbsd_sockaddr_un
257 /* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
258 <sys/_sigset.h> and is the same value on all architectures. */
262 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
263 dump notes. See <sys/user.h> for the definition of struct
264 kinfo_proc. This data structure has different layouts on different
265 architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
266 uses a 32-bit time_t while all other architectures use a 64-bit
269 The core dump note actually contains one kinfo_proc structure for
270 each thread, but all of the process-wide data can be obtained from
271 the first structure. One result of this note's format is that some
272 of the process-wide status available in the native target method
273 from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
274 is not available from a core dump. Instead, the per-thread data
275 structures contain the value of these fields for individual
278 struct kinfo_proc_layout
280 /* Offsets of struct kinfo_proc members. */
287 int ki_tdev_freebsd11;
309 /* Offsets of struct rusage members. */
317 const struct kinfo_proc_layout kinfo_proc_layout_32 =
325 .ki_tdev_freebsd11 = 0x44,
326 .ki_sigignore = 0x68,
345 .ki_rusage_ch = 0x278,
354 const struct kinfo_proc_layout kinfo_proc_layout_i386 =
362 .ki_tdev_freebsd11 = 0x44,
363 .ki_sigignore = 0x68,
382 .ki_rusage_ch = 0x258,
391 const struct kinfo_proc_layout kinfo_proc_layout_64 =
399 .ki_tdev_freebsd11 = 0x64,
400 .ki_sigignore = 0x88,
419 .ki_rusage_ch = 0x2f0,
428 static struct gdbarch_data *fbsd_gdbarch_data_handle;
430 struct fbsd_gdbarch_data
432 struct type *siginfo_type;
436 init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
438 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
441 static struct fbsd_gdbarch_data *
442 get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
444 return ((struct fbsd_gdbarch_data *)
445 gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
448 /* Per-program-space data for FreeBSD architectures. */
449 static const struct program_space_data *fbsd_pspace_data_handle;
451 struct fbsd_pspace_data
453 /* Offsets in the runtime linker's 'Obj_Entry' structure. */
455 LONGEST off_tlsindex;
456 bool rtld_offsets_valid;
459 static struct fbsd_pspace_data *
460 get_fbsd_pspace_data (struct program_space *pspace)
462 struct fbsd_pspace_data *data;
464 data = ((struct fbsd_pspace_data *)
465 program_space_data (pspace, fbsd_pspace_data_handle));
468 data = XCNEW (struct fbsd_pspace_data);
469 set_program_space_data (pspace, fbsd_pspace_data_handle, data);
475 /* The cleanup callback for FreeBSD architecture per-program-space data. */
478 fbsd_pspace_data_cleanup (struct program_space *pspace, void *data)
483 /* This is how we want PTIDs from core files to be printed. */
486 fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
488 if (ptid.lwp () != 0)
489 return string_printf ("LWP %ld", ptid.lwp ());
491 return normal_pid_to_str (ptid);
494 /* Extract the name assigned to a thread from a core. Returns the
495 string in a static buffer. */
498 fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
501 struct bfd_section *section;
504 if (thr->ptid.lwp () != 0)
506 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
507 whose contents are defined by a "struct thrmisc" declared in
508 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
509 a null-terminated string as the first member of the
510 structure. Rather than define the full structure here, just
511 extract the null-terminated name from the start of the
513 thread_section_name section_name (".thrmisc", thr->ptid);
515 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
516 if (section != NULL && bfd_section_size (core_bfd, section) > 0)
518 /* Truncate the name if it is longer than "buf". */
519 size = bfd_section_size (core_bfd, section);
520 if (size > sizeof buf - 1)
521 size = sizeof buf - 1;
522 if (bfd_get_section_contents (core_bfd, section, buf, (file_ptr) 0,
528 /* Note that each thread will report the process command
529 as its thread name instead of an empty name if a name
530 has not been set explicitly. Return a NULL name in
532 if (strcmp (buf, elf_tdata (core_bfd)->core->program) != 0)
541 /* Implement the "core_xfer_siginfo" gdbarch method. */
544 fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
545 ULONGEST offset, ULONGEST len)
549 if (gdbarch_long_bit (gdbarch) == 32)
550 siginfo_size = SIZE32_SIGINFO_T;
552 siginfo_size = SIZE64_SIGINFO_T;
553 if (offset > siginfo_size)
556 thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
557 asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
562 if (!bfd_get_section_contents (core_bfd, section, buf,
563 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
566 int pl_flags = extract_signed_integer (buf, 4, gdbarch_byte_order (gdbarch));
567 if (!(pl_flags & PL_FLAG_SI))
570 if (offset + len > siginfo_size)
571 len = siginfo_size - offset;
573 ULONGEST siginfo_offset;
574 if (gdbarch_long_bit (gdbarch) == 32)
575 siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
577 siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
579 if (!bfd_get_section_contents (core_bfd, section, readbuf,
580 siginfo_offset + offset, len))
587 find_signalled_thread (struct thread_info *info, void *data)
589 if (info->suspend.stop_signal != GDB_SIGNAL_0
590 && info->ptid.pid () == inferior_ptid.pid ())
596 /* Structure for passing information from
597 fbsd_collect_thread_registers via an iterator to
598 fbsd_collect_regset_section_cb. */
600 struct fbsd_collect_regset_section_cb_data
602 const struct regcache *regcache;
607 enum gdb_signal stop_signal;
612 fbsd_collect_regset_section_cb (const char *sect_name, int supply_size,
613 int collect_size, const struct regset *regset,
614 const char *human_name, void *cb_data)
617 struct fbsd_collect_regset_section_cb_data *data
618 = (struct fbsd_collect_regset_section_cb_data *) cb_data;
620 if (data->abort_iteration)
623 gdb_assert (regset->collect_regset);
625 buf = (char *) xmalloc (collect_size);
626 regset->collect_regset (regset, data->regcache, -1, buf, collect_size);
628 /* PRSTATUS still needs to be treated specially. */
629 if (strcmp (sect_name, ".reg") == 0)
630 data->note_data = (char *) elfcore_write_prstatus
631 (data->obfd, data->note_data, data->note_size, data->lwp,
632 gdb_signal_to_host (data->stop_signal), buf);
634 data->note_data = (char *) elfcore_write_register_note
635 (data->obfd, data->note_data, data->note_size,
636 sect_name, buf, collect_size);
639 if (data->note_data == NULL)
640 data->abort_iteration = 1;
643 /* Records the thread's register state for the corefile note
647 fbsd_collect_thread_registers (const struct regcache *regcache,
648 ptid_t ptid, bfd *obfd,
649 char *note_data, int *note_size,
650 enum gdb_signal stop_signal)
652 struct gdbarch *gdbarch = regcache->arch ();
653 struct fbsd_collect_regset_section_cb_data data;
655 data.regcache = regcache;
657 data.note_data = note_data;
658 data.note_size = note_size;
659 data.stop_signal = stop_signal;
660 data.abort_iteration = 0;
661 data.lwp = ptid.lwp ();
663 gdbarch_iterate_over_regset_sections (gdbarch,
664 fbsd_collect_regset_section_cb,
666 return data.note_data;
669 struct fbsd_corefile_thread_data
671 struct gdbarch *gdbarch;
675 enum gdb_signal stop_signal;
678 /* Records the thread's register state for the corefile note
682 fbsd_corefile_thread (struct thread_info *info,
683 struct fbsd_corefile_thread_data *args)
685 struct regcache *regcache;
687 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
689 target_fetch_registers (regcache, -1);
691 args->note_data = fbsd_collect_thread_registers
692 (regcache, info->ptid, args->obfd, args->note_data,
693 args->note_size, args->stop_signal);
696 /* Return a byte_vector containing the contents of a core dump note
697 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
698 the data is prefixed with a 32-bit integer size to match the format
699 used in FreeBSD NT_PROCSTAT_* notes. */
701 static gdb::optional<gdb::byte_vector>
702 fbsd_make_note_desc (enum target_object object, uint32_t structsize)
704 gdb::optional<gdb::byte_vector> buf =
705 target_read_alloc (current_top_target (), object, NULL);
706 if (!buf || buf->empty ())
712 gdb::byte_vector desc (sizeof (structsize) + buf->size ());
713 memcpy (desc.data (), &structsize, sizeof (structsize));
714 memcpy (desc.data () + sizeof (structsize), buf->data (), buf->size ());
718 /* Create appropriate note sections for a corefile, returning them in
722 fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
724 struct fbsd_corefile_thread_data thread_args;
725 char *note_data = NULL;
726 Elf_Internal_Ehdr *i_ehdrp;
727 struct thread_info *curr_thr, *signalled_thr;
729 /* Put a "FreeBSD" label in the ELF header. */
730 i_ehdrp = elf_elfheader (obfd);
731 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
733 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
735 if (get_exec_file (0))
737 const char *fname = lbasename (get_exec_file (0));
738 char *psargs = xstrdup (fname);
740 if (get_inferior_args ())
741 psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
744 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
748 /* Thread register information. */
751 update_thread_list ();
753 CATCH (e, RETURN_MASK_ERROR)
755 exception_print (gdb_stderr, e);
759 /* Like the kernel, prefer dumping the signalled thread first.
760 "First thread" is what tools use to infer the signalled thread.
761 In case there's more than one signalled thread, prefer the
762 current thread, if it is signalled. */
763 curr_thr = inferior_thread ();
764 if (curr_thr->suspend.stop_signal != GDB_SIGNAL_0)
765 signalled_thr = curr_thr;
768 signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
769 if (signalled_thr == NULL)
770 signalled_thr = curr_thr;
773 thread_args.gdbarch = gdbarch;
774 thread_args.obfd = obfd;
775 thread_args.note_data = note_data;
776 thread_args.note_size = note_size;
777 thread_args.stop_signal = signalled_thr->suspend.stop_signal;
779 fbsd_corefile_thread (signalled_thr, &thread_args);
780 for (thread_info *thr : current_inferior ()->non_exited_threads ())
782 if (thr == signalled_thr)
785 fbsd_corefile_thread (thr, &thread_args);
788 note_data = thread_args.note_data;
790 /* Auxiliary vector. */
791 uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo */
792 gdb::optional<gdb::byte_vector> note_desc =
793 fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
794 if (note_desc && !note_desc->empty ())
796 note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
797 NT_FREEBSD_PROCSTAT_AUXV,
798 note_desc->data (), note_desc->size ());
803 /* Virtual memory mappings. */
804 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
805 if (note_desc && !note_desc->empty ())
807 note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
808 NT_FREEBSD_PROCSTAT_VMMAP,
809 note_desc->data (), note_desc->size ());
814 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
815 if (note_desc && !note_desc->empty ())
817 note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
818 NT_FREEBSD_PROCSTAT_PSSTRINGS,
819 note_desc->data (), note_desc->size ());
827 /* Helper function to generate the file descriptor description for a
828 single open file in 'info proc files'. */
831 fbsd_file_fd (int kf_fd)
835 case KINFO_FILE_FD_TYPE_CWD:
837 case KINFO_FILE_FD_TYPE_ROOT:
839 case KINFO_FILE_FD_TYPE_JAIL:
841 case KINFO_FILE_FD_TYPE_TRACE:
843 case KINFO_FILE_FD_TYPE_TEXT:
845 case KINFO_FILE_FD_TYPE_CTTY:
848 return int_string (kf_fd, 10, 1, 0, 0);
852 /* Helper function to generate the file type for a single open file in
853 'info proc files'. */
856 fbsd_file_type (int kf_type, int kf_vnode_type)
860 case KINFO_FILE_TYPE_VNODE:
861 switch (kf_vnode_type)
863 case KINFO_FILE_VTYPE_VREG:
865 case KINFO_FILE_VTYPE_VDIR:
867 case KINFO_FILE_VTYPE_VCHR:
869 case KINFO_FILE_VTYPE_VLNK:
871 case KINFO_FILE_VTYPE_VSOCK:
873 case KINFO_FILE_VTYPE_VFIFO:
877 char *str = get_print_cell ();
879 xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
883 case KINFO_FILE_TYPE_SOCKET:
885 case KINFO_FILE_TYPE_PIPE:
887 case KINFO_FILE_TYPE_FIFO:
889 case KINFO_FILE_TYPE_KQUEUE:
891 case KINFO_FILE_TYPE_CRYPTO:
893 case KINFO_FILE_TYPE_MQUEUE:
895 case KINFO_FILE_TYPE_SHM:
897 case KINFO_FILE_TYPE_SEM:
899 case KINFO_FILE_TYPE_PTS:
901 case KINFO_FILE_TYPE_PROCDESC:
904 return int_string (kf_type, 10, 1, 0, 0);
908 /* Helper function to generate the file flags for a single open file in
909 'info proc files'. */
912 fbsd_file_flags (int kf_flags)
914 static char file_flags[10];
916 file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
917 file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
918 file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
919 file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
920 file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
921 file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
922 file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
923 file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
924 file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
925 file_flags[9] = '\0';
930 /* Helper function to generate the name of an IP protocol. */
933 fbsd_ipproto (int protocol)
937 case FBSD_IPPROTO_ICMP:
939 case FBSD_IPPROTO_TCP:
941 case FBSD_IPPROTO_UDP:
943 case FBSD_IPPROTO_SCTP:
947 char *str = get_print_cell ();
949 xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
955 /* Helper function to print out an IPv4 socket address. */
958 fbsd_print_sockaddr_in (const void *sockaddr)
960 const struct fbsd_sockaddr_in *sin =
961 reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
962 char buf[INET_ADDRSTRLEN];
964 if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
965 error (_("Failed to format IPv4 address"));
966 printf_filtered ("%s:%u", buf,
967 (sin->sin_port[0] << 8) | sin->sin_port[1]);
970 /* Helper function to print out an IPv6 socket address. */
973 fbsd_print_sockaddr_in6 (const void *sockaddr)
975 const struct fbsd_sockaddr_in6 *sin6 =
976 reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
977 char buf[INET6_ADDRSTRLEN];
979 if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
980 error (_("Failed to format IPv6 address"));
981 printf_filtered ("%s.%u", buf,
982 (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
985 /* See fbsd-tdep.h. */
988 fbsd_info_proc_files_header ()
990 printf_filtered (_("Open files:\n\n"));
991 printf_filtered (" %6s %6s %10s %9s %s\n",
992 "FD", "Type", "Offset", "Flags ", "Name");
995 /* See fbsd-tdep.h. */
998 fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
999 LONGEST kf_offset, int kf_vnode_type,
1000 int kf_sock_domain, int kf_sock_type,
1001 int kf_sock_protocol, const void *kf_sa_local,
1002 const void *kf_sa_peer, const void *kf_path)
1004 printf_filtered (" %6s %6s %10s %8s ",
1005 fbsd_file_fd (kf_fd),
1006 fbsd_file_type (kf_type, kf_vnode_type),
1007 kf_offset > -1 ? hex_string (kf_offset) : "-",
1008 fbsd_file_flags (kf_flags));
1009 if (kf_type == KINFO_FILE_TYPE_SOCKET)
1011 switch (kf_sock_domain)
1015 switch (kf_sock_type)
1017 case FBSD_SOCK_STREAM:
1018 printf_filtered ("unix stream:");
1020 case FBSD_SOCK_DGRAM:
1021 printf_filtered ("unix dgram:");
1023 case FBSD_SOCK_SEQPACKET:
1024 printf_filtered ("unix seqpacket:");
1027 printf_filtered ("unix <%d>:", kf_sock_type);
1031 /* For local sockets, print out the first non-nul path
1032 rather than both paths. */
1033 const struct fbsd_sockaddr_un *sun
1034 = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
1035 if (sun->sun_path[0] == 0)
1036 sun = reinterpret_cast<const struct fbsd_sockaddr_un *>
1038 printf_filtered ("%s", sun->sun_path);
1042 printf_filtered ("%s4 ", fbsd_ipproto (kf_sock_protocol));
1043 fbsd_print_sockaddr_in (kf_sa_local);
1044 printf_filtered (" -> ");
1045 fbsd_print_sockaddr_in (kf_sa_peer);
1048 printf_filtered ("%s6 ", fbsd_ipproto (kf_sock_protocol));
1049 fbsd_print_sockaddr_in6 (kf_sa_local);
1050 printf_filtered (" -> ");
1051 fbsd_print_sockaddr_in6 (kf_sa_peer);
1056 printf_filtered ("%s", reinterpret_cast<const char *> (kf_path));
1057 printf_filtered ("\n");
1060 /* Implement "info proc files" for a corefile. */
1063 fbsd_core_info_proc_files (struct gdbarch *gdbarch)
1066 = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
1067 if (section == NULL)
1069 warning (_("unable to find open files in core file"));
1073 size_t note_size = bfd_get_section_size (section);
1075 error (_("malformed core note - too short for header"));
1077 gdb::def_vector<unsigned char> contents (note_size);
1078 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1080 error (_("could not get core note contents"));
1082 unsigned char *descdata = contents.data ();
1083 unsigned char *descend = descdata + note_size;
1085 /* Skip over the structure size. */
1088 fbsd_info_proc_files_header ();
1090 while (descdata + KF_PATH < descend)
1092 ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
1093 if (structsize < KF_PATH)
1094 error (_("malformed core note - file structure too small"));
1096 LONGEST type = bfd_get_signed_32 (core_bfd, descdata + KF_TYPE);
1097 LONGEST fd = bfd_get_signed_32 (core_bfd, descdata + KF_FD);
1098 LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KF_FLAGS);
1099 LONGEST offset = bfd_get_signed_64 (core_bfd, descdata + KF_OFFSET);
1100 LONGEST vnode_type = bfd_get_signed_32 (core_bfd,
1101 descdata + KF_VNODE_TYPE);
1102 LONGEST sock_domain = bfd_get_signed_32 (core_bfd,
1103 descdata + KF_SOCK_DOMAIN);
1104 LONGEST sock_type = bfd_get_signed_32 (core_bfd, descdata + KF_SOCK_TYPE);
1105 LONGEST sock_protocol = bfd_get_signed_32 (core_bfd,
1106 descdata + KF_SOCK_PROTOCOL);
1107 fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
1108 sock_domain, sock_type, sock_protocol,
1109 descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
1110 descdata + KF_PATH);
1112 descdata += structsize;
1116 /* Helper function to generate mappings flags for a single VM map
1117 entry in 'info proc mappings'. */
1120 fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
1122 static char vm_flags[9];
1124 vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
1125 vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
1126 vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
1128 vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
1129 vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
1130 vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
1131 vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
1132 : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
1138 /* See fbsd-tdep.h. */
1141 fbsd_info_proc_mappings_header (int addr_bit)
1143 printf_filtered (_("Mapped address spaces:\n\n"));
1146 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1149 " Size", " Offset", "Flags ", "File");
1153 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1156 " Size", " Offset", "Flags ", "File");
1160 /* See fbsd-tdep.h. */
1163 fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
1164 ULONGEST kve_end, ULONGEST kve_offset,
1165 int kve_flags, int kve_protection,
1166 const void *kve_path)
1170 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1171 hex_string (kve_start),
1172 hex_string (kve_end),
1173 hex_string (kve_end - kve_start),
1174 hex_string (kve_offset),
1175 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1176 reinterpret_cast<const char *> (kve_path));
1180 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1181 hex_string (kve_start),
1182 hex_string (kve_end),
1183 hex_string (kve_end - kve_start),
1184 hex_string (kve_offset),
1185 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1186 reinterpret_cast<const char *> (kve_path));
1190 /* Implement "info proc mappings" for a corefile. */
1193 fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
1196 unsigned char *descdata, *descend;
1199 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.vmmap");
1200 if (section == NULL)
1202 warning (_("unable to find mappings in core file"));
1206 note_size = bfd_get_section_size (section);
1208 error (_("malformed core note - too short for header"));
1210 gdb::def_vector<unsigned char> contents (note_size);
1211 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1213 error (_("could not get core note contents"));
1215 descdata = contents.data ();
1216 descend = descdata + note_size;
1218 /* Skip over the structure size. */
1221 fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
1222 while (descdata + KVE_PATH < descend)
1224 ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KVE_STRUCTSIZE);
1225 if (structsize < KVE_PATH)
1226 error (_("malformed core note - vmmap entry too small"));
1228 ULONGEST start = bfd_get_64 (core_bfd, descdata + KVE_START);
1229 ULONGEST end = bfd_get_64 (core_bfd, descdata + KVE_END);
1230 ULONGEST offset = bfd_get_64 (core_bfd, descdata + KVE_OFFSET);
1231 LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KVE_FLAGS);
1232 LONGEST prot = bfd_get_signed_32 (core_bfd, descdata + KVE_PROTECTION);
1233 fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
1234 offset, flags, prot, descdata + KVE_PATH);
1236 descdata += structsize;
1240 /* Fetch the pathname of a vnode for a single file descriptor from the
1241 file table core note. */
1243 static gdb::unique_xmalloc_ptr<char>
1244 fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
1247 unsigned char *descdata, *descend;
1250 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
1251 if (section == NULL)
1254 note_size = bfd_get_section_size (section);
1256 error (_("malformed core note - too short for header"));
1258 gdb::def_vector<unsigned char> contents (note_size);
1259 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1261 error (_("could not get core note contents"));
1263 descdata = contents.data ();
1264 descend = descdata + note_size;
1266 /* Skip over the structure size. */
1269 while (descdata + KF_PATH < descend)
1271 ULONGEST structsize;
1273 structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
1274 if (structsize < KF_PATH)
1275 error (_("malformed core note - file structure too small"));
1277 if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
1278 && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
1280 char *path = (char *) descdata + KF_PATH;
1281 return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
1284 descdata += structsize;
1289 /* Helper function to read a struct timeval. */
1292 fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
1293 LONGEST &sec, ULONGEST &usec)
1295 if (gdbarch_addr_bit (gdbarch) == 64)
1297 sec = bfd_get_signed_64 (core_bfd, data);
1298 usec = bfd_get_64 (core_bfd, data + 8);
1300 else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
1302 sec = bfd_get_signed_32 (core_bfd, data);
1303 usec = bfd_get_32 (core_bfd, data + 4);
1307 sec = bfd_get_signed_64 (core_bfd, data);
1308 usec = bfd_get_32 (core_bfd, data + 8);
1312 /* Print out the contents of a signal set. */
1315 fbsd_print_sigset (const char *descr, unsigned char *sigset)
1317 printf_filtered ("%s: ", descr);
1318 for (int i = 0; i < SIG_WORDS; i++)
1319 printf_filtered ("%08x ",
1320 (unsigned int) bfd_get_32 (core_bfd, sigset + i * 4));
1321 printf_filtered ("\n");
1324 /* Implement "info proc status" for a corefile. */
1327 fbsd_core_info_proc_status (struct gdbarch *gdbarch)
1329 const struct kinfo_proc_layout *kp;
1331 unsigned char *descdata;
1332 int addr_bit, long_bit;
1337 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.proc");
1338 if (section == NULL)
1340 warning (_("unable to find process info in core file"));
1344 addr_bit = gdbarch_addr_bit (gdbarch);
1346 kp = &kinfo_proc_layout_64;
1347 else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
1348 kp = &kinfo_proc_layout_i386;
1350 kp = &kinfo_proc_layout_32;
1351 long_bit = gdbarch_long_bit (gdbarch);
1354 * Ensure that the note is large enough for all of the fields fetched
1355 * by this function. In particular, the note must contain the 32-bit
1356 * structure size, then it must be long enough to access the last
1357 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1359 note_size = bfd_get_section_size (section);
1360 if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
1361 + long_bit / TARGET_CHAR_BIT))
1362 error (_("malformed core note - too short"));
1364 gdb::def_vector<unsigned char> contents (note_size);
1365 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1367 error (_("could not get core note contents"));
1369 descdata = contents.data ();
1371 /* Skip over the structure size. */
1374 /* Verify 'ki_layout' is 0. */
1375 if (bfd_get_32 (core_bfd, descdata + kp->ki_layout) != 0)
1377 warning (_("unsupported process information in core file"));
1381 printf_filtered ("Name: %.19s\n", descdata + kp->ki_comm);
1382 printf_filtered ("Process ID: %s\n",
1383 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pid)));
1384 printf_filtered ("Parent process: %s\n",
1385 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ppid)));
1386 printf_filtered ("Process group: %s\n",
1387 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pgid)));
1388 printf_filtered ("Session id: %s\n",
1389 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_sid)));
1391 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1392 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1393 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1394 the structure that is cleared to zero. Assume that a zero value
1395 in ki_tdev indicates a core dump from an older kernel and use the
1396 value in 'ki_tdev_freebsd11' instead. */
1397 value = bfd_get_64 (core_bfd, descdata + kp->ki_tdev);
1399 value = bfd_get_32 (core_bfd, descdata + kp->ki_tdev_freebsd11);
1400 printf_filtered ("TTY: %s\n", pulongest (value));
1401 printf_filtered ("TTY owner process group: %s\n",
1402 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_tpgid)));
1403 printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
1404 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ruid)),
1405 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_uid)),
1406 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svuid)));
1407 printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
1408 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_rgid)),
1409 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_groups)),
1410 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svgid)));
1411 printf_filtered ("Groups: ");
1412 uint16_t ngroups = bfd_get_16 (core_bfd, descdata + kp->ki_ngroups);
1413 for (int i = 0; i < ngroups; i++)
1414 printf_filtered ("%s ",
1415 pulongest (bfd_get_32 (core_bfd,
1416 descdata + kp->ki_groups + i * 4)));
1417 printf_filtered ("\n");
1418 value = bfd_get (long_bit, core_bfd,
1419 descdata + kp->ki_rusage + kp->ru_minflt);
1420 printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value));
1421 value = bfd_get (long_bit, core_bfd,
1422 descdata + kp->ki_rusage_ch + kp->ru_minflt);
1423 printf_filtered ("Minor faults, children: %s\n", pulongest (value));
1424 value = bfd_get (long_bit, core_bfd,
1425 descdata + kp->ki_rusage + kp->ru_majflt);
1426 printf_filtered ("Major faults (memory page faults): %s\n",
1428 value = bfd_get (long_bit, core_bfd,
1429 descdata + kp->ki_rusage_ch + kp->ru_majflt);
1430 printf_filtered ("Major faults, children: %s\n", pulongest (value));
1431 fbsd_core_fetch_timeval (gdbarch,
1432 descdata + kp->ki_rusage + kp->ru_utime,
1434 printf_filtered ("utime: %s.%06d\n", plongest (sec), (int) value);
1435 fbsd_core_fetch_timeval (gdbarch,
1436 descdata + kp->ki_rusage + kp->ru_stime,
1438 printf_filtered ("stime: %s.%06d\n", plongest (sec), (int) value);
1439 fbsd_core_fetch_timeval (gdbarch,
1440 descdata + kp->ki_rusage_ch + kp->ru_utime,
1442 printf_filtered ("utime, children: %s.%06d\n", plongest (sec), (int) value);
1443 fbsd_core_fetch_timeval (gdbarch,
1444 descdata + kp->ki_rusage_ch + kp->ru_stime,
1446 printf_filtered ("stime, children: %s.%06d\n", plongest (sec), (int) value);
1447 printf_filtered ("'nice' value: %d\n",
1448 bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
1449 fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
1450 printf_filtered ("Start time: %s.%06d\n", plongest (sec), (int) value);
1451 printf_filtered ("Virtual memory size: %s kB\n",
1452 pulongest (bfd_get (addr_bit, core_bfd,
1453 descdata + kp->ki_size) / 1024));
1454 printf_filtered ("Data size: %s pages\n",
1455 pulongest (bfd_get (addr_bit, core_bfd,
1456 descdata + kp->ki_dsize)));
1457 printf_filtered ("Stack size: %s pages\n",
1458 pulongest (bfd_get (addr_bit, core_bfd,
1459 descdata + kp->ki_ssize)));
1460 printf_filtered ("Text size: %s pages\n",
1461 pulongest (bfd_get (addr_bit, core_bfd,
1462 descdata + kp->ki_tsize)));
1463 printf_filtered ("Resident set size: %s pages\n",
1464 pulongest (bfd_get (addr_bit, core_bfd,
1465 descdata + kp->ki_rssize)));
1466 printf_filtered ("Maximum RSS: %s pages\n",
1467 pulongest (bfd_get (long_bit, core_bfd,
1468 descdata + kp->ki_rusage
1470 fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
1471 fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
1474 /* Implement the "core_info_proc" gdbarch method. */
1477 fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
1478 enum info_proc_what what)
1480 bool do_cmdline = false;
1481 bool do_cwd = false;
1482 bool do_exe = false;
1483 bool do_files = false;
1484 bool do_mappings = false;
1485 bool do_status = false;
1526 pid = bfd_core_file_pid (core_bfd);
1528 printf_filtered (_("process %d\n"), pid);
1532 const char *cmdline;
1534 cmdline = bfd_core_file_failing_command (core_bfd);
1536 printf_filtered ("cmdline = '%s'\n", cmdline);
1538 warning (_("Command line unavailable"));
1542 gdb::unique_xmalloc_ptr<char> cwd =
1543 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
1545 printf_filtered ("cwd = '%s'\n", cwd.get ());
1547 warning (_("unable to read current working directory"));
1551 gdb::unique_xmalloc_ptr<char> exe =
1552 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
1554 printf_filtered ("exe = '%s'\n", exe.get ());
1556 warning (_("unable to read executable path name"));
1559 fbsd_core_info_proc_files (gdbarch);
1561 fbsd_core_info_proc_mappings (gdbarch);
1563 fbsd_core_info_proc_status (gdbarch);
1566 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1569 fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
1570 CORE_ADDR type, CORE_ADDR val)
1572 const char *name = "???";
1573 const char *description = "";
1574 enum auxv_format format = AUXV_FORMAT_HEX;
1593 default_print_auxv_entry (gdbarch, file, type, val);
1595 #define _TAGNAME(tag) #tag
1596 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1597 #define TAG(tag, text, kind) \
1598 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1599 TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
1600 TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
1601 TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
1602 TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
1603 TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
1604 TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
1605 TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
1606 TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
1607 TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
1608 TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
1609 TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
1610 TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
1613 fprint_auxv_entry (file, name, description, format, type, val);
1616 /* Implement the "get_siginfo_type" gdbarch method. */
1618 static struct type *
1619 fbsd_get_siginfo_type (struct gdbarch *gdbarch)
1621 struct fbsd_gdbarch_data *fbsd_gdbarch_data;
1622 struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
1623 struct type *uid_type, *pid_type;
1624 struct type *sigval_type, *reason_type;
1625 struct type *siginfo_type;
1628 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
1629 if (fbsd_gdbarch_data->siginfo_type != NULL)
1630 return fbsd_gdbarch_data->siginfo_type;
1632 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
1634 int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
1635 uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
1636 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
1638 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
1641 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1642 TYPE_NAME (sigval_type) = xstrdup ("sigval");
1643 append_composite_type_field (sigval_type, "sival_int", int_type);
1644 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
1647 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
1648 TYPE_LENGTH (int32_type) * TARGET_CHAR_BIT, "__pid_t");
1649 TYPE_TARGET_TYPE (pid_type) = int32_type;
1650 TYPE_TARGET_STUB (pid_type) = 1;
1653 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
1654 TYPE_LENGTH (uint32_type) * TARGET_CHAR_BIT,
1656 TYPE_TARGET_TYPE (uid_type) = uint32_type;
1657 TYPE_TARGET_STUB (uid_type) = 1;
1660 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1663 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1664 append_composite_type_field (type, "si_trapno", int_type);
1665 append_composite_type_field (reason_type, "_fault", type);
1668 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1669 append_composite_type_field (type, "si_timerid", int_type);
1670 append_composite_type_field (type, "si_overrun", int_type);
1671 append_composite_type_field (reason_type, "_timer", type);
1674 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1675 append_composite_type_field (type, "si_mqd", int_type);
1676 append_composite_type_field (reason_type, "_mesgq", type);
1679 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1680 append_composite_type_field (type, "si_band", long_type);
1681 append_composite_type_field (reason_type, "_poll", type);
1684 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1685 append_composite_type_field (type, "__spare1__", long_type);
1686 append_composite_type_field (type, "__spare2__",
1687 init_vector_type (int_type, 7));
1688 append_composite_type_field (reason_type, "__spare__", type);
1690 /* struct siginfo */
1691 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1692 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
1693 append_composite_type_field (siginfo_type, "si_signo", int_type);
1694 append_composite_type_field (siginfo_type, "si_errno", int_type);
1695 append_composite_type_field (siginfo_type, "si_code", int_type);
1696 append_composite_type_field (siginfo_type, "si_pid", pid_type);
1697 append_composite_type_field (siginfo_type, "si_uid", uid_type);
1698 append_composite_type_field (siginfo_type, "si_status", int_type);
1699 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
1700 append_composite_type_field (siginfo_type, "si_value", sigval_type);
1701 append_composite_type_field (siginfo_type, "_reason", reason_type);
1703 fbsd_gdbarch_data->siginfo_type = siginfo_type;
1705 return siginfo_type;
1708 /* Implement the "gdb_signal_from_target" gdbarch method. */
1710 static enum gdb_signal
1711 fbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
1716 return GDB_SIGNAL_0;
1718 case FREEBSD_SIGHUP:
1719 return GDB_SIGNAL_HUP;
1721 case FREEBSD_SIGINT:
1722 return GDB_SIGNAL_INT;
1724 case FREEBSD_SIGQUIT:
1725 return GDB_SIGNAL_QUIT;
1727 case FREEBSD_SIGILL:
1728 return GDB_SIGNAL_ILL;
1730 case FREEBSD_SIGTRAP:
1731 return GDB_SIGNAL_TRAP;
1733 case FREEBSD_SIGABRT:
1734 return GDB_SIGNAL_ABRT;
1736 case FREEBSD_SIGEMT:
1737 return GDB_SIGNAL_EMT;
1739 case FREEBSD_SIGFPE:
1740 return GDB_SIGNAL_FPE;
1742 case FREEBSD_SIGKILL:
1743 return GDB_SIGNAL_KILL;
1745 case FREEBSD_SIGBUS:
1746 return GDB_SIGNAL_BUS;
1748 case FREEBSD_SIGSEGV:
1749 return GDB_SIGNAL_SEGV;
1751 case FREEBSD_SIGSYS:
1752 return GDB_SIGNAL_SYS;
1754 case FREEBSD_SIGPIPE:
1755 return GDB_SIGNAL_PIPE;
1757 case FREEBSD_SIGALRM:
1758 return GDB_SIGNAL_ALRM;
1760 case FREEBSD_SIGTERM:
1761 return GDB_SIGNAL_TERM;
1763 case FREEBSD_SIGURG:
1764 return GDB_SIGNAL_URG;
1766 case FREEBSD_SIGSTOP:
1767 return GDB_SIGNAL_STOP;
1769 case FREEBSD_SIGTSTP:
1770 return GDB_SIGNAL_TSTP;
1772 case FREEBSD_SIGCONT:
1773 return GDB_SIGNAL_CONT;
1775 case FREEBSD_SIGCHLD:
1776 return GDB_SIGNAL_CHLD;
1778 case FREEBSD_SIGTTIN:
1779 return GDB_SIGNAL_TTIN;
1781 case FREEBSD_SIGTTOU:
1782 return GDB_SIGNAL_TTOU;
1785 return GDB_SIGNAL_IO;
1787 case FREEBSD_SIGXCPU:
1788 return GDB_SIGNAL_XCPU;
1790 case FREEBSD_SIGXFSZ:
1791 return GDB_SIGNAL_XFSZ;
1793 case FREEBSD_SIGVTALRM:
1794 return GDB_SIGNAL_VTALRM;
1796 case FREEBSD_SIGPROF:
1797 return GDB_SIGNAL_PROF;
1799 case FREEBSD_SIGWINCH:
1800 return GDB_SIGNAL_WINCH;
1802 case FREEBSD_SIGINFO:
1803 return GDB_SIGNAL_INFO;
1805 case FREEBSD_SIGUSR1:
1806 return GDB_SIGNAL_USR1;
1808 case FREEBSD_SIGUSR2:
1809 return GDB_SIGNAL_USR2;
1811 /* SIGTHR is the same as SIGLWP on FreeBSD. */
1812 case FREEBSD_SIGTHR:
1813 return GDB_SIGNAL_LWP;
1815 case FREEBSD_SIGLIBRT:
1816 return GDB_SIGNAL_LIBRT;
1819 if (signal >= FREEBSD_SIGRTMIN && signal <= FREEBSD_SIGRTMAX)
1821 int offset = signal - FREEBSD_SIGRTMIN;
1823 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_65 + offset);
1826 return GDB_SIGNAL_UNKNOWN;
1829 /* Implement the "gdb_signal_to_target" gdbarch method. */
1832 fbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
1833 enum gdb_signal signal)
1840 case GDB_SIGNAL_HUP:
1841 return FREEBSD_SIGHUP;
1843 case GDB_SIGNAL_INT:
1844 return FREEBSD_SIGINT;
1846 case GDB_SIGNAL_QUIT:
1847 return FREEBSD_SIGQUIT;
1849 case GDB_SIGNAL_ILL:
1850 return FREEBSD_SIGILL;
1852 case GDB_SIGNAL_TRAP:
1853 return FREEBSD_SIGTRAP;
1855 case GDB_SIGNAL_ABRT:
1856 return FREEBSD_SIGABRT;
1858 case GDB_SIGNAL_EMT:
1859 return FREEBSD_SIGEMT;
1861 case GDB_SIGNAL_FPE:
1862 return FREEBSD_SIGFPE;
1864 case GDB_SIGNAL_KILL:
1865 return FREEBSD_SIGKILL;
1867 case GDB_SIGNAL_BUS:
1868 return FREEBSD_SIGBUS;
1870 case GDB_SIGNAL_SEGV:
1871 return FREEBSD_SIGSEGV;
1873 case GDB_SIGNAL_SYS:
1874 return FREEBSD_SIGSYS;
1876 case GDB_SIGNAL_PIPE:
1877 return FREEBSD_SIGPIPE;
1879 case GDB_SIGNAL_ALRM:
1880 return FREEBSD_SIGALRM;
1882 case GDB_SIGNAL_TERM:
1883 return FREEBSD_SIGTERM;
1885 case GDB_SIGNAL_URG:
1886 return FREEBSD_SIGURG;
1888 case GDB_SIGNAL_STOP:
1889 return FREEBSD_SIGSTOP;
1891 case GDB_SIGNAL_TSTP:
1892 return FREEBSD_SIGTSTP;
1894 case GDB_SIGNAL_CONT:
1895 return FREEBSD_SIGCONT;
1897 case GDB_SIGNAL_CHLD:
1898 return FREEBSD_SIGCHLD;
1900 case GDB_SIGNAL_TTIN:
1901 return FREEBSD_SIGTTIN;
1903 case GDB_SIGNAL_TTOU:
1904 return FREEBSD_SIGTTOU;
1907 return FREEBSD_SIGIO;
1909 case GDB_SIGNAL_XCPU:
1910 return FREEBSD_SIGXCPU;
1912 case GDB_SIGNAL_XFSZ:
1913 return FREEBSD_SIGXFSZ;
1915 case GDB_SIGNAL_VTALRM:
1916 return FREEBSD_SIGVTALRM;
1918 case GDB_SIGNAL_PROF:
1919 return FREEBSD_SIGPROF;
1921 case GDB_SIGNAL_WINCH:
1922 return FREEBSD_SIGWINCH;
1924 case GDB_SIGNAL_INFO:
1925 return FREEBSD_SIGINFO;
1927 case GDB_SIGNAL_USR1:
1928 return FREEBSD_SIGUSR1;
1930 case GDB_SIGNAL_USR2:
1931 return FREEBSD_SIGUSR2;
1933 case GDB_SIGNAL_LWP:
1934 return FREEBSD_SIGTHR;
1936 case GDB_SIGNAL_LIBRT:
1937 return FREEBSD_SIGLIBRT;
1940 if (signal >= GDB_SIGNAL_REALTIME_65
1941 && signal <= GDB_SIGNAL_REALTIME_126)
1943 int offset = signal - GDB_SIGNAL_REALTIME_65;
1945 return FREEBSD_SIGRTMIN + offset;
1951 /* Implement the "get_syscall_number" gdbarch method. */
1954 fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
1957 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1958 native targets fetch the system call number from the
1959 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1960 However, system call catching requires this function to be
1963 internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
1966 /* Read an integer symbol value from the current target. */
1969 fbsd_read_integer_by_name (struct gdbarch *gdbarch, const char *name)
1971 bound_minimal_symbol ms = lookup_minimal_symbol (name, NULL, NULL);
1972 if (ms.minsym == NULL)
1973 error (_("Unable to resolve symbol '%s'"), name);
1976 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms), buf, sizeof buf) != 0)
1977 error (_("Unable to read value of '%s'"), name);
1979 return extract_signed_integer (buf, sizeof buf, gdbarch_byte_order (gdbarch));
1982 /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
1983 structure needed to determine the TLS index of an object file. */
1986 fbsd_fetch_rtld_offsets (struct gdbarch *gdbarch, struct fbsd_pspace_data *data)
1990 /* Fetch offsets from debug symbols in rtld. */
1991 struct symbol *obj_entry_sym
1992 = lookup_symbol_in_language ("Struct_Obj_Entry", NULL, STRUCT_DOMAIN,
1993 language_c, NULL).symbol;
1994 if (obj_entry_sym == NULL)
1995 error (_("Unable to find Struct_Obj_Entry symbol"));
1996 data->off_linkmap = lookup_struct_elt (SYMBOL_TYPE(obj_entry_sym),
1997 "linkmap", 0).offset / 8;
1998 data->off_tlsindex = lookup_struct_elt (SYMBOL_TYPE(obj_entry_sym),
1999 "tlsindex", 0).offset / 8;
2000 data->rtld_offsets_valid = true;
2003 CATCH (e, RETURN_MASK_ERROR)
2005 data->off_linkmap = -1;
2011 /* Fetch offsets from global variables in libthr. Note that
2012 this does not work for single-threaded processes that are not
2013 linked against libthr. */
2014 data->off_linkmap = fbsd_read_integer_by_name (gdbarch,
2015 "_thread_off_linkmap");
2016 data->off_tlsindex = fbsd_read_integer_by_name (gdbarch,
2017 "_thread_off_tlsindex");
2018 data->rtld_offsets_valid = true;
2021 CATCH (e, RETURN_MASK_ERROR)
2023 data->off_linkmap = -1;
2028 /* Helper function to read the TLS index of an object file associated
2029 with a link map entry at LM_ADDR. */
2032 fbsd_get_tls_index (struct gdbarch *gdbarch, CORE_ADDR lm_addr)
2034 struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
2036 if (!data->rtld_offsets_valid)
2037 fbsd_fetch_rtld_offsets (gdbarch, data);
2039 if (data->off_linkmap == -1)
2040 throw_error (TLS_GENERIC_ERROR,
2041 _("Cannot fetch runtime linker structure offsets"));
2043 /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
2044 pointer and then compute the offset of the tlsindex member. */
2045 CORE_ADDR tlsindex_addr = lm_addr - data->off_linkmap + data->off_tlsindex;
2048 if (target_read_memory (tlsindex_addr, buf, sizeof buf) != 0)
2049 throw_error (TLS_GENERIC_ERROR,
2050 _("Cannot find thread-local variables on this target"));
2052 return extract_signed_integer (buf, sizeof buf, gdbarch_byte_order (gdbarch));
2055 /* See fbsd-tdep.h. */
2058 fbsd_get_thread_local_address (struct gdbarch *gdbarch, CORE_ADDR dtv_addr,
2059 CORE_ADDR lm_addr, CORE_ADDR offset)
2061 LONGEST tls_index = fbsd_get_tls_index (gdbarch, lm_addr);
2063 gdb_byte buf[gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT];
2064 if (target_read_memory (dtv_addr, buf, sizeof buf) != 0)
2065 throw_error (TLS_GENERIC_ERROR,
2066 _("Cannot find thread-local variables on this target"));
2068 const struct builtin_type *builtin = builtin_type (gdbarch);
2069 CORE_ADDR addr = gdbarch_pointer_to_address (gdbarch,
2070 builtin->builtin_data_ptr, buf);
2072 addr += (tls_index + 1) * TYPE_LENGTH (builtin->builtin_data_ptr);
2073 if (target_read_memory (addr, buf, sizeof buf) != 0)
2074 throw_error (TLS_GENERIC_ERROR,
2075 _("Cannot find thread-local variables on this target"));
2077 addr = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr, buf);
2078 return addr + offset;
2081 /* To be called from GDB_OSABI_FREEBSD handlers. */
2084 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2086 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
2087 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
2088 set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
2089 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
2090 set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
2091 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
2092 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
2093 set_gdbarch_gdb_signal_from_target (gdbarch, fbsd_gdb_signal_from_target);
2094 set_gdbarch_gdb_signal_to_target (gdbarch, fbsd_gdb_signal_to_target);
2096 /* `catch syscall' */
2097 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
2098 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
2102 _initialize_fbsd_tdep (void)
2104 fbsd_gdbarch_data_handle =
2105 gdbarch_data_register_post_init (init_fbsd_gdbarch_data);
2106 fbsd_pspace_data_handle
2107 = register_program_space_data_with_cleanup (NULL, fbsd_pspace_data_cleanup);