1 /* Target-dependent code for FreeBSD, architecture-independent.
3 Copyright (C) 2002-2021 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/>. */
27 #include "gdbthread.h"
29 #include "xml-syscall.h"
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
34 #include "fbsd-tdep.h"
35 #include "gcore-elf.h"
37 /* This enum is derived from FreeBSD's <sys/signal.h>. */
66 FREEBSD_SIGVTALRM
= 26,
68 FREEBSD_SIGWINCH
= 28,
73 FREEBSD_SIGLIBRT
= 33,
74 FREEBSD_SIGRTMIN
= 65,
75 FREEBSD_SIGRTMAX
= 126,
78 /* FreeBSD kernels 12.0 and later include a copy of the
79 'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
80 operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
81 The constants below define the offset of field members and flags in
82 this structure used by methods in this file. Note that the
83 'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
84 containing the size of the structure. */
86 #define LWPINFO_OFFSET 0x4
88 /* Offsets in ptrace_lwpinfo. */
89 #define LWPINFO_PL_FLAGS 0x8
90 #define LWPINFO64_PL_SIGINFO 0x30
91 #define LWPINFO32_PL_SIGINFO 0x2c
93 /* Flags in pl_flags. */
94 #define PL_FLAG_SI 0x20 /* siginfo is valid */
96 /* Sizes of siginfo_t. */
97 #define SIZE64_SIGINFO_T 80
98 #define SIZE32_SIGINFO_T 64
100 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
101 dump notes. See <sys/user.h> for the definition of struct
102 kinfo_vmentry. This data structure should have the same layout on
105 Note that FreeBSD 7.0 used an older version of this structure
106 (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
107 dump note wasn't introduced until FreeBSD 9.2. As a result, the
108 core dump note has always used the 7.1 and later structure
111 #define KVE_STRUCTSIZE 0x0
112 #define KVE_START 0x8
114 #define KVE_OFFSET 0x18
115 #define KVE_FLAGS 0x2c
116 #define KVE_PROTECTION 0x38
117 #define KVE_PATH 0x88
119 /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
120 match the KVME_PROT_* constants in <sys/user.h>. */
122 #define KINFO_VME_PROT_READ 0x00000001
123 #define KINFO_VME_PROT_WRITE 0x00000002
124 #define KINFO_VME_PROT_EXEC 0x00000004
126 /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
127 match the KVME_FLAG_* constants in <sys/user.h>. */
129 #define KINFO_VME_FLAG_COW 0x00000001
130 #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
131 #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
132 #define KINFO_VME_FLAG_SUPER 0x00000008
133 #define KINFO_VME_FLAG_GROWS_UP 0x00000010
134 #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
136 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
137 dump notes. See <sys/user.h> for the definition of struct
138 kinfo_file. This data structure should have the same layout on all
141 Note that FreeBSD 7.0 used an older version of this structure
142 (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
143 note wasn't introduced until FreeBSD 9.2. As a result, the core
144 dump note has always used the 7.1 and later structure format. */
146 #define KF_STRUCTSIZE 0x0
149 #define KF_FLAGS 0x10
150 #define KF_OFFSET 0x18
151 #define KF_VNODE_TYPE 0x20
152 #define KF_SOCK_DOMAIN 0x24
153 #define KF_SOCK_TYPE 0x28
154 #define KF_SOCK_PROTOCOL 0x2c
155 #define KF_SA_LOCAL 0x30
156 #define KF_SA_PEER 0xb0
157 #define KF_PATH 0x170
159 /* Constants for the 'kf_type' field in struct kinfo_file. These
160 match the KF_TYPE_* constants in <sys/user.h>. */
162 #define KINFO_FILE_TYPE_VNODE 1
163 #define KINFO_FILE_TYPE_SOCKET 2
164 #define KINFO_FILE_TYPE_PIPE 3
165 #define KINFO_FILE_TYPE_FIFO 4
166 #define KINFO_FILE_TYPE_KQUEUE 5
167 #define KINFO_FILE_TYPE_CRYPTO 6
168 #define KINFO_FILE_TYPE_MQUEUE 7
169 #define KINFO_FILE_TYPE_SHM 8
170 #define KINFO_FILE_TYPE_SEM 9
171 #define KINFO_FILE_TYPE_PTS 10
172 #define KINFO_FILE_TYPE_PROCDESC 11
174 /* Special values for the 'kf_fd' field in struct kinfo_file. These
175 match the KF_FD_TYPE_* constants in <sys/user.h>. */
177 #define KINFO_FILE_FD_TYPE_CWD -1
178 #define KINFO_FILE_FD_TYPE_ROOT -2
179 #define KINFO_FILE_FD_TYPE_JAIL -3
180 #define KINFO_FILE_FD_TYPE_TRACE -4
181 #define KINFO_FILE_FD_TYPE_TEXT -5
182 #define KINFO_FILE_FD_TYPE_CTTY -6
184 /* Flags in the 'kf_flags' field in struct kinfo_file. These match
185 the KF_FLAG_* constants in <sys/user.h>. */
187 #define KINFO_FILE_FLAG_READ 0x00000001
188 #define KINFO_FILE_FLAG_WRITE 0x00000002
189 #define KINFO_FILE_FLAG_APPEND 0x00000004
190 #define KINFO_FILE_FLAG_ASYNC 0x00000008
191 #define KINFO_FILE_FLAG_FSYNC 0x00000010
192 #define KINFO_FILE_FLAG_NONBLOCK 0x00000020
193 #define KINFO_FILE_FLAG_DIRECT 0x00000040
194 #define KINFO_FILE_FLAG_HASLOCK 0x00000080
195 #define KINFO_FILE_FLAG_EXEC 0x00004000
197 /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
198 These match the KF_VTYPE_* constants in <sys/user.h>. */
200 #define KINFO_FILE_VTYPE_VREG 1
201 #define KINFO_FILE_VTYPE_VDIR 2
202 #define KINFO_FILE_VTYPE_VCHR 4
203 #define KINFO_FILE_VTYPE_VLNK 5
204 #define KINFO_FILE_VTYPE_VSOCK 6
205 #define KINFO_FILE_VTYPE_VFIFO 7
207 /* Constants for socket address families. These match AF_* constants
208 in <sys/socket.h>. */
210 #define FBSD_AF_UNIX 1
211 #define FBSD_AF_INET 2
212 #define FBSD_AF_INET6 28
214 /* Constants for socket types. These match SOCK_* constants in
217 #define FBSD_SOCK_STREAM 1
218 #define FBSD_SOCK_DGRAM 2
219 #define FBSD_SOCK_SEQPACKET 5
221 /* Constants for IP protocols. These match IPPROTO_* constants in
224 #define FBSD_IPPROTO_ICMP 1
225 #define FBSD_IPPROTO_TCP 6
226 #define FBSD_IPPROTO_UDP 17
227 #define FBSD_IPPROTO_SCTP 132
229 /* Socket address structures. These have the same layout on all
230 FreeBSD architectures. In addition, multibyte fields such as IP
231 addresses are always stored in network byte order. */
233 struct fbsd_sockaddr_in
242 struct fbsd_sockaddr_in6
246 uint8_t sin6_port
[2];
247 uint32_t sin6_flowinfo
;
248 uint8_t sin6_addr
[16];
249 uint32_t sin6_scope_id
;
252 struct fbsd_sockaddr_un
259 /* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
260 <sys/_sigset.h> and is the same value on all architectures. */
264 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
265 dump notes. See <sys/user.h> for the definition of struct
266 kinfo_proc. This data structure has different layouts on different
267 architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
268 uses a 32-bit time_t while all other architectures use a 64-bit
271 The core dump note actually contains one kinfo_proc structure for
272 each thread, but all of the process-wide data can be obtained from
273 the first structure. One result of this note's format is that some
274 of the process-wide status available in the native target method
275 from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
276 is not available from a core dump. Instead, the per-thread data
277 structures contain the value of these fields for individual
280 struct kinfo_proc_layout
282 /* Offsets of struct kinfo_proc members. */
289 int ki_tdev_freebsd11
;
311 /* Offsets of struct rusage members. */
319 const struct kinfo_proc_layout kinfo_proc_layout_32
=
327 .ki_tdev_freebsd11
= 0x44,
328 .ki_sigignore
= 0x68,
347 .ki_rusage_ch
= 0x278,
356 const struct kinfo_proc_layout kinfo_proc_layout_i386
=
364 .ki_tdev_freebsd11
= 0x44,
365 .ki_sigignore
= 0x68,
384 .ki_rusage_ch
= 0x258,
393 const struct kinfo_proc_layout kinfo_proc_layout_64
=
401 .ki_tdev_freebsd11
= 0x64,
402 .ki_sigignore
= 0x88,
421 .ki_rusage_ch
= 0x2f0,
430 static struct gdbarch_data
*fbsd_gdbarch_data_handle
;
432 struct fbsd_gdbarch_data
434 struct type
*siginfo_type
;
438 init_fbsd_gdbarch_data (struct gdbarch
*gdbarch
)
440 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct fbsd_gdbarch_data
);
443 static struct fbsd_gdbarch_data
*
444 get_fbsd_gdbarch_data (struct gdbarch
*gdbarch
)
446 return ((struct fbsd_gdbarch_data
*)
447 gdbarch_data (gdbarch
, fbsd_gdbarch_data_handle
));
450 struct fbsd_pspace_data
452 /* Offsets in the runtime linker's 'Obj_Entry' structure. */
453 LONGEST off_linkmap
= 0;
454 LONGEST off_tlsindex
= 0;
455 bool rtld_offsets_valid
= false;
458 /* Per-program-space data for FreeBSD architectures. */
459 static const struct program_space_key
<fbsd_pspace_data
>
460 fbsd_pspace_data_handle
;
462 static struct fbsd_pspace_data
*
463 get_fbsd_pspace_data (struct program_space
*pspace
)
465 struct fbsd_pspace_data
*data
;
467 data
= fbsd_pspace_data_handle
.get (pspace
);
469 data
= fbsd_pspace_data_handle
.emplace (pspace
);
474 /* This is how we want PTIDs from core files to be printed. */
477 fbsd_core_pid_to_str (struct gdbarch
*gdbarch
, ptid_t ptid
)
479 if (ptid
.lwp () != 0)
480 return string_printf ("LWP %ld", ptid
.lwp ());
482 return normal_pid_to_str (ptid
);
485 /* Extract the name assigned to a thread from a core. Returns the
486 string in a static buffer. */
489 fbsd_core_thread_name (struct gdbarch
*gdbarch
, struct thread_info
*thr
)
492 struct bfd_section
*section
;
495 if (thr
->ptid
.lwp () != 0)
497 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
498 whose contents are defined by a "struct thrmisc" declared in
499 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
500 a null-terminated string as the first member of the
501 structure. Rather than define the full structure here, just
502 extract the null-terminated name from the start of the
504 thread_section_name
section_name (".thrmisc", thr
->ptid
);
506 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
507 if (section
!= NULL
&& bfd_section_size (section
) > 0)
509 /* Truncate the name if it is longer than "buf". */
510 size
= bfd_section_size (section
);
511 if (size
> sizeof buf
- 1)
512 size
= sizeof buf
- 1;
513 if (bfd_get_section_contents (core_bfd
, section
, buf
, (file_ptr
) 0,
519 /* Note that each thread will report the process command
520 as its thread name instead of an empty name if a name
521 has not been set explicitly. Return a NULL name in
523 if (strcmp (buf
, elf_tdata (core_bfd
)->core
->program
) != 0)
532 /* Implement the "core_xfer_siginfo" gdbarch method. */
535 fbsd_core_xfer_siginfo (struct gdbarch
*gdbarch
, gdb_byte
*readbuf
,
536 ULONGEST offset
, ULONGEST len
)
540 if (gdbarch_long_bit (gdbarch
) == 32)
541 siginfo_size
= SIZE32_SIGINFO_T
;
543 siginfo_size
= SIZE64_SIGINFO_T
;
544 if (offset
> siginfo_size
)
547 thread_section_name
section_name (".note.freebsdcore.lwpinfo", inferior_ptid
);
548 asection
*section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
553 if (!bfd_get_section_contents (core_bfd
, section
, buf
,
554 LWPINFO_OFFSET
+ LWPINFO_PL_FLAGS
, 4))
557 int pl_flags
= extract_signed_integer (buf
, 4, gdbarch_byte_order (gdbarch
));
558 if (!(pl_flags
& PL_FLAG_SI
))
561 if (offset
+ len
> siginfo_size
)
562 len
= siginfo_size
- offset
;
564 ULONGEST siginfo_offset
;
565 if (gdbarch_long_bit (gdbarch
) == 32)
566 siginfo_offset
= LWPINFO_OFFSET
+ LWPINFO32_PL_SIGINFO
;
568 siginfo_offset
= LWPINFO_OFFSET
+ LWPINFO64_PL_SIGINFO
;
570 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
571 siginfo_offset
+ offset
, len
))
578 find_signalled_thread (struct thread_info
*info
, void *data
)
580 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
581 && info
->ptid
.pid () == inferior_ptid
.pid ())
587 /* Return a byte_vector containing the contents of a core dump note
588 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
589 the data is prefixed with a 32-bit integer size to match the format
590 used in FreeBSD NT_PROCSTAT_* notes. */
592 static gdb::optional
<gdb::byte_vector
>
593 fbsd_make_note_desc (enum target_object object
, uint32_t structsize
)
595 gdb::optional
<gdb::byte_vector
> buf
=
596 target_read_alloc (current_inferior ()->top_target (), object
, NULL
);
597 if (!buf
|| buf
->empty ())
603 gdb::byte_vector
desc (sizeof (structsize
) + buf
->size ());
604 memcpy (desc
.data (), &structsize
, sizeof (structsize
));
605 memcpy (desc
.data () + sizeof (structsize
), buf
->data (), buf
->size ());
609 /* Create appropriate note sections for a corefile, returning them in
612 static gdb::unique_xmalloc_ptr
<char>
613 fbsd_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
615 gdb::unique_xmalloc_ptr
<char> note_data
;
616 Elf_Internal_Ehdr
*i_ehdrp
;
617 struct thread_info
*curr_thr
, *signalled_thr
;
619 /* Put a "FreeBSD" label in the ELF header. */
620 i_ehdrp
= elf_elfheader (obfd
);
621 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
623 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch
));
625 if (get_exec_file (0))
627 const char *fname
= lbasename (get_exec_file (0));
628 std::string psargs
= fname
;
630 const char *infargs
= get_inferior_args ();
632 psargs
= psargs
+ " " + infargs
;
634 note_data
.reset (elfcore_write_prpsinfo (obfd
, note_data
.release (),
639 /* Thread register information. */
642 update_thread_list ();
644 catch (const gdb_exception_error
&e
)
646 exception_print (gdb_stderr
, e
);
649 /* Like the kernel, prefer dumping the signalled thread first.
650 "First thread" is what tools use to infer the signalled thread.
651 In case there's more than one signalled thread, prefer the
652 current thread, if it is signalled. */
653 curr_thr
= inferior_thread ();
654 if (curr_thr
->suspend
.stop_signal
!= GDB_SIGNAL_0
)
655 signalled_thr
= curr_thr
;
658 signalled_thr
= iterate_over_threads (find_signalled_thread
, NULL
);
659 if (signalled_thr
== NULL
)
660 signalled_thr
= curr_thr
;
663 enum gdb_signal stop_signal
= signalled_thr
->suspend
.stop_signal
;
664 gcore_elf_build_thread_register_notes (gdbarch
, signalled_thr
, stop_signal
,
665 obfd
, ¬e_data
, note_size
);
666 for (thread_info
*thr
: current_inferior ()->non_exited_threads ())
668 if (thr
== signalled_thr
)
671 gcore_elf_build_thread_register_notes (gdbarch
, thr
, stop_signal
,
672 obfd
, ¬e_data
, note_size
);
675 /* Auxiliary vector. */
676 uint32_t structsize
= gdbarch_ptr_bit (gdbarch
) / 4; /* Elf_Auxinfo */
677 gdb::optional
<gdb::byte_vector
> note_desc
=
678 fbsd_make_note_desc (TARGET_OBJECT_AUXV
, structsize
);
679 if (note_desc
&& !note_desc
->empty ())
681 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (),
682 note_size
, "FreeBSD",
683 NT_FREEBSD_PROCSTAT_AUXV
,
685 note_desc
->size ()));
690 /* Virtual memory mappings. */
691 note_desc
= fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP
, 0);
692 if (note_desc
&& !note_desc
->empty ())
694 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (),
695 note_size
, "FreeBSD",
696 NT_FREEBSD_PROCSTAT_VMMAP
,
698 note_desc
->size ()));
703 note_desc
= fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS
, 0);
704 if (note_desc
&& !note_desc
->empty ())
706 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (),
707 note_size
, "FreeBSD",
708 NT_FREEBSD_PROCSTAT_PSSTRINGS
,
710 note_desc
->size ()));
715 /* Include the target description when possible. */
716 gcore_elf_make_tdesc_note (obfd
, ¬e_data
, note_size
);
721 /* Helper function to generate the file descriptor description for a
722 single open file in 'info proc files'. */
725 fbsd_file_fd (int kf_fd
)
729 case KINFO_FILE_FD_TYPE_CWD
:
731 case KINFO_FILE_FD_TYPE_ROOT
:
733 case KINFO_FILE_FD_TYPE_JAIL
:
735 case KINFO_FILE_FD_TYPE_TRACE
:
737 case KINFO_FILE_FD_TYPE_TEXT
:
739 case KINFO_FILE_FD_TYPE_CTTY
:
742 return int_string (kf_fd
, 10, 1, 0, 0);
746 /* Helper function to generate the file type for a single open file in
747 'info proc files'. */
750 fbsd_file_type (int kf_type
, int kf_vnode_type
)
754 case KINFO_FILE_TYPE_VNODE
:
755 switch (kf_vnode_type
)
757 case KINFO_FILE_VTYPE_VREG
:
759 case KINFO_FILE_VTYPE_VDIR
:
761 case KINFO_FILE_VTYPE_VCHR
:
763 case KINFO_FILE_VTYPE_VLNK
:
765 case KINFO_FILE_VTYPE_VSOCK
:
767 case KINFO_FILE_VTYPE_VFIFO
:
771 char *str
= get_print_cell ();
773 xsnprintf (str
, PRINT_CELL_SIZE
, "vn:%d", kf_vnode_type
);
777 case KINFO_FILE_TYPE_SOCKET
:
779 case KINFO_FILE_TYPE_PIPE
:
781 case KINFO_FILE_TYPE_FIFO
:
783 case KINFO_FILE_TYPE_KQUEUE
:
785 case KINFO_FILE_TYPE_CRYPTO
:
787 case KINFO_FILE_TYPE_MQUEUE
:
789 case KINFO_FILE_TYPE_SHM
:
791 case KINFO_FILE_TYPE_SEM
:
793 case KINFO_FILE_TYPE_PTS
:
795 case KINFO_FILE_TYPE_PROCDESC
:
798 return int_string (kf_type
, 10, 1, 0, 0);
802 /* Helper function to generate the file flags for a single open file in
803 'info proc files'. */
806 fbsd_file_flags (int kf_flags
)
808 static char file_flags
[10];
810 file_flags
[0] = (kf_flags
& KINFO_FILE_FLAG_READ
) ? 'r' : '-';
811 file_flags
[1] = (kf_flags
& KINFO_FILE_FLAG_WRITE
) ? 'w' : '-';
812 file_flags
[2] = (kf_flags
& KINFO_FILE_FLAG_EXEC
) ? 'x' : '-';
813 file_flags
[3] = (kf_flags
& KINFO_FILE_FLAG_APPEND
) ? 'a' : '-';
814 file_flags
[4] = (kf_flags
& KINFO_FILE_FLAG_ASYNC
) ? 's' : '-';
815 file_flags
[5] = (kf_flags
& KINFO_FILE_FLAG_FSYNC
) ? 'f' : '-';
816 file_flags
[6] = (kf_flags
& KINFO_FILE_FLAG_NONBLOCK
) ? 'n' : '-';
817 file_flags
[7] = (kf_flags
& KINFO_FILE_FLAG_DIRECT
) ? 'd' : '-';
818 file_flags
[8] = (kf_flags
& KINFO_FILE_FLAG_HASLOCK
) ? 'l' : '-';
819 file_flags
[9] = '\0';
824 /* Helper function to generate the name of an IP protocol. */
827 fbsd_ipproto (int protocol
)
831 case FBSD_IPPROTO_ICMP
:
833 case FBSD_IPPROTO_TCP
:
835 case FBSD_IPPROTO_UDP
:
837 case FBSD_IPPROTO_SCTP
:
841 char *str
= get_print_cell ();
843 xsnprintf (str
, PRINT_CELL_SIZE
, "ip<%d>", protocol
);
849 /* Helper function to print out an IPv4 socket address. */
852 fbsd_print_sockaddr_in (const void *sockaddr
)
854 const struct fbsd_sockaddr_in
*sin
=
855 reinterpret_cast<const struct fbsd_sockaddr_in
*> (sockaddr
);
856 char buf
[INET_ADDRSTRLEN
];
858 if (inet_ntop (AF_INET
, sin
->sin_addr
, buf
, sizeof buf
) == nullptr)
859 error (_("Failed to format IPv4 address"));
860 printf_filtered ("%s:%u", buf
,
861 (sin
->sin_port
[0] << 8) | sin
->sin_port
[1]);
864 /* Helper function to print out an IPv6 socket address. */
867 fbsd_print_sockaddr_in6 (const void *sockaddr
)
869 const struct fbsd_sockaddr_in6
*sin6
=
870 reinterpret_cast<const struct fbsd_sockaddr_in6
*> (sockaddr
);
871 char buf
[INET6_ADDRSTRLEN
];
873 if (inet_ntop (AF_INET6
, sin6
->sin6_addr
, buf
, sizeof buf
) == nullptr)
874 error (_("Failed to format IPv6 address"));
875 printf_filtered ("%s.%u", buf
,
876 (sin6
->sin6_port
[0] << 8) | sin6
->sin6_port
[1]);
879 /* See fbsd-tdep.h. */
882 fbsd_info_proc_files_header ()
884 printf_filtered (_("Open files:\n\n"));
885 printf_filtered (" %6s %6s %10s %9s %s\n",
886 "FD", "Type", "Offset", "Flags ", "Name");
889 /* See fbsd-tdep.h. */
892 fbsd_info_proc_files_entry (int kf_type
, int kf_fd
, int kf_flags
,
893 LONGEST kf_offset
, int kf_vnode_type
,
894 int kf_sock_domain
, int kf_sock_type
,
895 int kf_sock_protocol
, const void *kf_sa_local
,
896 const void *kf_sa_peer
, const void *kf_path
)
898 printf_filtered (" %6s %6s %10s %8s ",
899 fbsd_file_fd (kf_fd
),
900 fbsd_file_type (kf_type
, kf_vnode_type
),
901 kf_offset
> -1 ? hex_string (kf_offset
) : "-",
902 fbsd_file_flags (kf_flags
));
903 if (kf_type
== KINFO_FILE_TYPE_SOCKET
)
905 switch (kf_sock_domain
)
909 switch (kf_sock_type
)
911 case FBSD_SOCK_STREAM
:
912 printf_filtered ("unix stream:");
914 case FBSD_SOCK_DGRAM
:
915 printf_filtered ("unix dgram:");
917 case FBSD_SOCK_SEQPACKET
:
918 printf_filtered ("unix seqpacket:");
921 printf_filtered ("unix <%d>:", kf_sock_type
);
925 /* For local sockets, print out the first non-nul path
926 rather than both paths. */
927 const struct fbsd_sockaddr_un
*saddr_un
928 = reinterpret_cast<const struct fbsd_sockaddr_un
*> (kf_sa_local
);
929 if (saddr_un
->sun_path
[0] == 0)
930 saddr_un
= reinterpret_cast<const struct fbsd_sockaddr_un
*>
932 printf_filtered ("%s", saddr_un
->sun_path
);
936 printf_filtered ("%s4 ", fbsd_ipproto (kf_sock_protocol
));
937 fbsd_print_sockaddr_in (kf_sa_local
);
938 printf_filtered (" -> ");
939 fbsd_print_sockaddr_in (kf_sa_peer
);
942 printf_filtered ("%s6 ", fbsd_ipproto (kf_sock_protocol
));
943 fbsd_print_sockaddr_in6 (kf_sa_local
);
944 printf_filtered (" -> ");
945 fbsd_print_sockaddr_in6 (kf_sa_peer
);
950 printf_filtered ("%s", reinterpret_cast<const char *> (kf_path
));
951 printf_filtered ("\n");
954 /* Implement "info proc files" for a corefile. */
957 fbsd_core_info_proc_files (struct gdbarch
*gdbarch
)
960 = bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.files");
963 warning (_("unable to find open files in core file"));
967 size_t note_size
= bfd_section_size (section
);
969 error (_("malformed core note - too short for header"));
971 gdb::def_vector
<unsigned char> contents (note_size
);
972 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
974 error (_("could not get core note contents"));
976 unsigned char *descdata
= contents
.data ();
977 unsigned char *descend
= descdata
+ note_size
;
979 /* Skip over the structure size. */
982 fbsd_info_proc_files_header ();
984 while (descdata
+ KF_PATH
< descend
)
986 ULONGEST structsize
= bfd_get_32 (core_bfd
, descdata
+ KF_STRUCTSIZE
);
987 if (structsize
< KF_PATH
)
988 error (_("malformed core note - file structure too small"));
990 LONGEST type
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_TYPE
);
991 LONGEST fd
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_FD
);
992 LONGEST flags
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_FLAGS
);
993 LONGEST offset
= bfd_get_signed_64 (core_bfd
, descdata
+ KF_OFFSET
);
994 LONGEST vnode_type
= bfd_get_signed_32 (core_bfd
,
995 descdata
+ KF_VNODE_TYPE
);
996 LONGEST sock_domain
= bfd_get_signed_32 (core_bfd
,
997 descdata
+ KF_SOCK_DOMAIN
);
998 LONGEST sock_type
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_SOCK_TYPE
);
999 LONGEST sock_protocol
= bfd_get_signed_32 (core_bfd
,
1000 descdata
+ KF_SOCK_PROTOCOL
);
1001 fbsd_info_proc_files_entry (type
, fd
, flags
, offset
, vnode_type
,
1002 sock_domain
, sock_type
, sock_protocol
,
1003 descdata
+ KF_SA_LOCAL
, descdata
+ KF_SA_PEER
,
1004 descdata
+ KF_PATH
);
1006 descdata
+= structsize
;
1010 /* Helper function to generate mappings flags for a single VM map
1011 entry in 'info proc mappings'. */
1014 fbsd_vm_map_entry_flags (int kve_flags
, int kve_protection
)
1016 static char vm_flags
[9];
1018 vm_flags
[0] = (kve_protection
& KINFO_VME_PROT_READ
) ? 'r' : '-';
1019 vm_flags
[1] = (kve_protection
& KINFO_VME_PROT_WRITE
) ? 'w' : '-';
1020 vm_flags
[2] = (kve_protection
& KINFO_VME_PROT_EXEC
) ? 'x' : '-';
1022 vm_flags
[4] = (kve_flags
& KINFO_VME_FLAG_COW
) ? 'C' : '-';
1023 vm_flags
[5] = (kve_flags
& KINFO_VME_FLAG_NEEDS_COPY
) ? 'N' : '-';
1024 vm_flags
[6] = (kve_flags
& KINFO_VME_FLAG_SUPER
) ? 'S' : '-';
1025 vm_flags
[7] = (kve_flags
& KINFO_VME_FLAG_GROWS_UP
) ? 'U'
1026 : (kve_flags
& KINFO_VME_FLAG_GROWS_DOWN
) ? 'D' : '-';
1032 /* See fbsd-tdep.h. */
1035 fbsd_info_proc_mappings_header (int addr_bit
)
1037 printf_filtered (_("Mapped address spaces:\n\n"));
1040 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1043 " Size", " Offset", "Flags ", "File");
1047 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1050 " Size", " Offset", "Flags ", "File");
1054 /* See fbsd-tdep.h. */
1057 fbsd_info_proc_mappings_entry (int addr_bit
, ULONGEST kve_start
,
1058 ULONGEST kve_end
, ULONGEST kve_offset
,
1059 int kve_flags
, int kve_protection
,
1060 const void *kve_path
)
1064 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1065 hex_string (kve_start
),
1066 hex_string (kve_end
),
1067 hex_string (kve_end
- kve_start
),
1068 hex_string (kve_offset
),
1069 fbsd_vm_map_entry_flags (kve_flags
, kve_protection
),
1070 reinterpret_cast<const char *> (kve_path
));
1074 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1075 hex_string (kve_start
),
1076 hex_string (kve_end
),
1077 hex_string (kve_end
- kve_start
),
1078 hex_string (kve_offset
),
1079 fbsd_vm_map_entry_flags (kve_flags
, kve_protection
),
1080 reinterpret_cast<const char *> (kve_path
));
1084 /* Implement "info proc mappings" for a corefile. */
1087 fbsd_core_info_proc_mappings (struct gdbarch
*gdbarch
)
1090 unsigned char *descdata
, *descend
;
1093 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.vmmap");
1094 if (section
== NULL
)
1096 warning (_("unable to find mappings in core file"));
1100 note_size
= bfd_section_size (section
);
1102 error (_("malformed core note - too short for header"));
1104 gdb::def_vector
<unsigned char> contents (note_size
);
1105 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1107 error (_("could not get core note contents"));
1109 descdata
= contents
.data ();
1110 descend
= descdata
+ note_size
;
1112 /* Skip over the structure size. */
1115 fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch
));
1116 while (descdata
+ KVE_PATH
< descend
)
1118 ULONGEST structsize
= bfd_get_32 (core_bfd
, descdata
+ KVE_STRUCTSIZE
);
1119 if (structsize
< KVE_PATH
)
1120 error (_("malformed core note - vmmap entry too small"));
1122 ULONGEST start
= bfd_get_64 (core_bfd
, descdata
+ KVE_START
);
1123 ULONGEST end
= bfd_get_64 (core_bfd
, descdata
+ KVE_END
);
1124 ULONGEST offset
= bfd_get_64 (core_bfd
, descdata
+ KVE_OFFSET
);
1125 LONGEST flags
= bfd_get_signed_32 (core_bfd
, descdata
+ KVE_FLAGS
);
1126 LONGEST prot
= bfd_get_signed_32 (core_bfd
, descdata
+ KVE_PROTECTION
);
1127 fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch
), start
, end
,
1128 offset
, flags
, prot
, descdata
+ KVE_PATH
);
1130 descdata
+= structsize
;
1134 /* Fetch the pathname of a vnode for a single file descriptor from the
1135 file table core note. */
1137 static gdb::unique_xmalloc_ptr
<char>
1138 fbsd_core_vnode_path (struct gdbarch
*gdbarch
, int fd
)
1141 unsigned char *descdata
, *descend
;
1144 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.files");
1145 if (section
== NULL
)
1148 note_size
= bfd_section_size (section
);
1150 error (_("malformed core note - too short for header"));
1152 gdb::def_vector
<unsigned char> contents (note_size
);
1153 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1155 error (_("could not get core note contents"));
1157 descdata
= contents
.data ();
1158 descend
= descdata
+ note_size
;
1160 /* Skip over the structure size. */
1163 while (descdata
+ KF_PATH
< descend
)
1165 ULONGEST structsize
;
1167 structsize
= bfd_get_32 (core_bfd
, descdata
+ KF_STRUCTSIZE
);
1168 if (structsize
< KF_PATH
)
1169 error (_("malformed core note - file structure too small"));
1171 if (bfd_get_32 (core_bfd
, descdata
+ KF_TYPE
) == KINFO_FILE_TYPE_VNODE
1172 && bfd_get_signed_32 (core_bfd
, descdata
+ KF_FD
) == fd
)
1174 char *path
= (char *) descdata
+ KF_PATH
;
1175 return make_unique_xstrdup (path
);
1178 descdata
+= structsize
;
1183 /* Helper function to read a struct timeval. */
1186 fbsd_core_fetch_timeval (struct gdbarch
*gdbarch
, unsigned char *data
,
1187 LONGEST
&sec
, ULONGEST
&usec
)
1189 if (gdbarch_addr_bit (gdbarch
) == 64)
1191 sec
= bfd_get_signed_64 (core_bfd
, data
);
1192 usec
= bfd_get_64 (core_bfd
, data
+ 8);
1194 else if (bfd_get_arch (core_bfd
) == bfd_arch_i386
)
1196 sec
= bfd_get_signed_32 (core_bfd
, data
);
1197 usec
= bfd_get_32 (core_bfd
, data
+ 4);
1201 sec
= bfd_get_signed_64 (core_bfd
, data
);
1202 usec
= bfd_get_32 (core_bfd
, data
+ 8);
1206 /* Print out the contents of a signal set. */
1209 fbsd_print_sigset (const char *descr
, unsigned char *sigset
)
1211 printf_filtered ("%s: ", descr
);
1212 for (int i
= 0; i
< SIG_WORDS
; i
++)
1213 printf_filtered ("%08x ",
1214 (unsigned int) bfd_get_32 (core_bfd
, sigset
+ i
* 4));
1215 printf_filtered ("\n");
1218 /* Implement "info proc status" for a corefile. */
1221 fbsd_core_info_proc_status (struct gdbarch
*gdbarch
)
1223 const struct kinfo_proc_layout
*kp
;
1225 unsigned char *descdata
;
1226 int addr_bit
, long_bit
;
1231 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.proc");
1232 if (section
== NULL
)
1234 warning (_("unable to find process info in core file"));
1238 addr_bit
= gdbarch_addr_bit (gdbarch
);
1240 kp
= &kinfo_proc_layout_64
;
1241 else if (bfd_get_arch (core_bfd
) == bfd_arch_i386
)
1242 kp
= &kinfo_proc_layout_i386
;
1244 kp
= &kinfo_proc_layout_32
;
1245 long_bit
= gdbarch_long_bit (gdbarch
);
1248 * Ensure that the note is large enough for all of the fields fetched
1249 * by this function. In particular, the note must contain the 32-bit
1250 * structure size, then it must be long enough to access the last
1251 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1253 note_size
= bfd_section_size (section
);
1254 if (note_size
< (4 + kp
->ki_rusage_ch
+ kp
->ru_majflt
1255 + long_bit
/ TARGET_CHAR_BIT
))
1256 error (_("malformed core note - too short"));
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 ();
1265 /* Skip over the structure size. */
1268 /* Verify 'ki_layout' is 0. */
1269 if (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_layout
) != 0)
1271 warning (_("unsupported process information in core file"));
1275 printf_filtered ("Name: %.19s\n", descdata
+ kp
->ki_comm
);
1276 printf_filtered ("Process ID: %s\n",
1277 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_pid
)));
1278 printf_filtered ("Parent process: %s\n",
1279 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_ppid
)));
1280 printf_filtered ("Process group: %s\n",
1281 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_pgid
)));
1282 printf_filtered ("Session id: %s\n",
1283 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_sid
)));
1285 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1286 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1287 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1288 the structure that is cleared to zero. Assume that a zero value
1289 in ki_tdev indicates a core dump from an older kernel and use the
1290 value in 'ki_tdev_freebsd11' instead. */
1291 value
= bfd_get_64 (core_bfd
, descdata
+ kp
->ki_tdev
);
1293 value
= bfd_get_32 (core_bfd
, descdata
+ kp
->ki_tdev_freebsd11
);
1294 printf_filtered ("TTY: %s\n", pulongest (value
));
1295 printf_filtered ("TTY owner process group: %s\n",
1296 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_tpgid
)));
1297 printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
1298 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_ruid
)),
1299 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_uid
)),
1300 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_svuid
)));
1301 printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
1302 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_rgid
)),
1303 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_groups
)),
1304 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_svgid
)));
1305 printf_filtered ("Groups: ");
1306 uint16_t ngroups
= bfd_get_16 (core_bfd
, descdata
+ kp
->ki_ngroups
);
1307 for (int i
= 0; i
< ngroups
; i
++)
1308 printf_filtered ("%s ",
1309 pulongest (bfd_get_32 (core_bfd
,
1310 descdata
+ kp
->ki_groups
+ i
* 4)));
1311 printf_filtered ("\n");
1312 value
= bfd_get (long_bit
, core_bfd
,
1313 descdata
+ kp
->ki_rusage
+ kp
->ru_minflt
);
1314 printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value
));
1315 value
= bfd_get (long_bit
, core_bfd
,
1316 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_minflt
);
1317 printf_filtered ("Minor faults, children: %s\n", pulongest (value
));
1318 value
= bfd_get (long_bit
, core_bfd
,
1319 descdata
+ kp
->ki_rusage
+ kp
->ru_majflt
);
1320 printf_filtered ("Major faults (memory page faults): %s\n",
1322 value
= bfd_get (long_bit
, core_bfd
,
1323 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_majflt
);
1324 printf_filtered ("Major faults, children: %s\n", pulongest (value
));
1325 fbsd_core_fetch_timeval (gdbarch
,
1326 descdata
+ kp
->ki_rusage
+ kp
->ru_utime
,
1328 printf_filtered ("utime: %s.%06d\n", plongest (sec
), (int) value
);
1329 fbsd_core_fetch_timeval (gdbarch
,
1330 descdata
+ kp
->ki_rusage
+ kp
->ru_stime
,
1332 printf_filtered ("stime: %s.%06d\n", plongest (sec
), (int) value
);
1333 fbsd_core_fetch_timeval (gdbarch
,
1334 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_utime
,
1336 printf_filtered ("utime, children: %s.%06d\n", plongest (sec
), (int) value
);
1337 fbsd_core_fetch_timeval (gdbarch
,
1338 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_stime
,
1340 printf_filtered ("stime, children: %s.%06d\n", plongest (sec
), (int) value
);
1341 printf_filtered ("'nice' value: %d\n",
1342 (int) bfd_get_signed_8 (core_bfd
, descdata
+ kp
->ki_nice
));
1343 fbsd_core_fetch_timeval (gdbarch
, descdata
+ kp
->ki_start
, sec
, value
);
1344 printf_filtered ("Start time: %s.%06d\n", plongest (sec
), (int) value
);
1345 printf_filtered ("Virtual memory size: %s kB\n",
1346 pulongest (bfd_get (addr_bit
, core_bfd
,
1347 descdata
+ kp
->ki_size
) / 1024));
1348 printf_filtered ("Data size: %s pages\n",
1349 pulongest (bfd_get (addr_bit
, core_bfd
,
1350 descdata
+ kp
->ki_dsize
)));
1351 printf_filtered ("Stack size: %s pages\n",
1352 pulongest (bfd_get (addr_bit
, core_bfd
,
1353 descdata
+ kp
->ki_ssize
)));
1354 printf_filtered ("Text size: %s pages\n",
1355 pulongest (bfd_get (addr_bit
, core_bfd
,
1356 descdata
+ kp
->ki_tsize
)));
1357 printf_filtered ("Resident set size: %s pages\n",
1358 pulongest (bfd_get (addr_bit
, core_bfd
,
1359 descdata
+ kp
->ki_rssize
)));
1360 printf_filtered ("Maximum RSS: %s pages\n",
1361 pulongest (bfd_get (long_bit
, core_bfd
,
1362 descdata
+ kp
->ki_rusage
1364 fbsd_print_sigset ("Ignored Signals", descdata
+ kp
->ki_sigignore
);
1365 fbsd_print_sigset ("Caught Signals", descdata
+ kp
->ki_sigcatch
);
1368 /* Implement the "core_info_proc" gdbarch method. */
1371 fbsd_core_info_proc (struct gdbarch
*gdbarch
, const char *args
,
1372 enum info_proc_what what
)
1374 bool do_cmdline
= false;
1375 bool do_cwd
= false;
1376 bool do_exe
= false;
1377 bool do_files
= false;
1378 bool do_mappings
= false;
1379 bool do_status
= false;
1420 pid
= bfd_core_file_pid (core_bfd
);
1422 printf_filtered (_("process %d\n"), pid
);
1426 const char *cmdline
;
1428 cmdline
= bfd_core_file_failing_command (core_bfd
);
1430 printf_filtered ("cmdline = '%s'\n", cmdline
);
1432 warning (_("Command line unavailable"));
1436 gdb::unique_xmalloc_ptr
<char> cwd
=
1437 fbsd_core_vnode_path (gdbarch
, KINFO_FILE_FD_TYPE_CWD
);
1439 printf_filtered ("cwd = '%s'\n", cwd
.get ());
1441 warning (_("unable to read current working directory"));
1445 gdb::unique_xmalloc_ptr
<char> exe
=
1446 fbsd_core_vnode_path (gdbarch
, KINFO_FILE_FD_TYPE_TEXT
);
1448 printf_filtered ("exe = '%s'\n", exe
.get ());
1450 warning (_("unable to read executable path name"));
1453 fbsd_core_info_proc_files (gdbarch
);
1455 fbsd_core_info_proc_mappings (gdbarch
);
1457 fbsd_core_info_proc_status (gdbarch
);
1460 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1463 fbsd_print_auxv_entry (struct gdbarch
*gdbarch
, struct ui_file
*file
,
1464 CORE_ADDR type
, CORE_ADDR val
)
1466 const char *name
= "???";
1467 const char *description
= "";
1468 enum auxv_format format
= AUXV_FORMAT_HEX
;
1487 default_print_auxv_entry (gdbarch
, file
, type
, val
);
1489 #define _TAGNAME(tag) #tag
1490 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1491 #define TAG(tag, text, kind) \
1492 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1493 TAG (EXECPATH
, _("Executable path"), AUXV_FORMAT_STR
);
1494 TAG (CANARY
, _("Canary for SSP"), AUXV_FORMAT_HEX
);
1495 TAG (CANARYLEN
, ("Length of the SSP canary"), AUXV_FORMAT_DEC
);
1496 TAG (OSRELDATE
, _("OSRELDATE"), AUXV_FORMAT_DEC
);
1497 TAG (NCPUS
, _("Number of CPUs"), AUXV_FORMAT_DEC
);
1498 TAG (PAGESIZES
, _("Pagesizes"), AUXV_FORMAT_HEX
);
1499 TAG (PAGESIZESLEN
, _("Number of pagesizes"), AUXV_FORMAT_DEC
);
1500 TAG (TIMEKEEP
, _("Pointer to timehands"), AUXV_FORMAT_HEX
);
1501 TAG (STACKPROT
, _("Initial stack protection"), AUXV_FORMAT_HEX
);
1502 TAG (EHDRFLAGS
, _("ELF header e_flags"), AUXV_FORMAT_HEX
);
1503 TAG (HWCAP
, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX
);
1504 TAG (HWCAP2
, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX
);
1505 TAG (BSDFLAGS
, _("ELF BSD flags"), AUXV_FORMAT_HEX
);
1506 TAG (ARGC
, _("Argument count"), AUXV_FORMAT_DEC
);
1507 TAG (ARGV
, _("Argument vector"), AUXV_FORMAT_HEX
);
1508 TAG (ENVC
, _("Environment count"), AUXV_FORMAT_DEC
);
1509 TAG (ENVV
, _("Environment vector"), AUXV_FORMAT_HEX
);
1510 TAG (PS_STRINGS
, _("Pointer to ps_strings"), AUXV_FORMAT_HEX
);
1513 fprint_auxv_entry (file
, name
, description
, format
, type
, val
);
1516 /* Implement the "get_siginfo_type" gdbarch method. */
1518 static struct type
*
1519 fbsd_get_siginfo_type (struct gdbarch
*gdbarch
)
1521 struct fbsd_gdbarch_data
*fbsd_gdbarch_data
;
1522 struct type
*int_type
, *int32_type
, *uint32_type
, *long_type
, *void_ptr_type
;
1523 struct type
*uid_type
, *pid_type
;
1524 struct type
*sigval_type
, *reason_type
;
1525 struct type
*siginfo_type
;
1528 fbsd_gdbarch_data
= get_fbsd_gdbarch_data (gdbarch
);
1529 if (fbsd_gdbarch_data
->siginfo_type
!= NULL
)
1530 return fbsd_gdbarch_data
->siginfo_type
;
1532 int_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
1534 int32_type
= arch_integer_type (gdbarch
, 32, 0, "int32_t");
1535 uint32_type
= arch_integer_type (gdbarch
, 32, 1, "uint32_t");
1536 long_type
= arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
1538 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
1541 sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
1542 sigval_type
->set_name (xstrdup ("sigval"));
1543 append_composite_type_field (sigval_type
, "sival_int", int_type
);
1544 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
1547 pid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
1548 TYPE_LENGTH (int32_type
) * TARGET_CHAR_BIT
, "__pid_t");
1549 TYPE_TARGET_TYPE (pid_type
) = int32_type
;
1550 pid_type
->set_target_is_stub (true);
1553 uid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
1554 TYPE_LENGTH (uint32_type
) * TARGET_CHAR_BIT
,
1556 TYPE_TARGET_TYPE (uid_type
) = uint32_type
;
1557 pid_type
->set_target_is_stub (true);
1560 reason_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
1563 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1564 append_composite_type_field (type
, "si_trapno", int_type
);
1565 append_composite_type_field (reason_type
, "_fault", type
);
1568 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1569 append_composite_type_field (type
, "si_timerid", int_type
);
1570 append_composite_type_field (type
, "si_overrun", int_type
);
1571 append_composite_type_field (reason_type
, "_timer", type
);
1574 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1575 append_composite_type_field (type
, "si_mqd", int_type
);
1576 append_composite_type_field (reason_type
, "_mesgq", type
);
1579 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1580 append_composite_type_field (type
, "si_band", long_type
);
1581 append_composite_type_field (reason_type
, "_poll", type
);
1584 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1585 append_composite_type_field (type
, "__spare1__", long_type
);
1586 append_composite_type_field (type
, "__spare2__",
1587 init_vector_type (int_type
, 7));
1588 append_composite_type_field (reason_type
, "__spare__", type
);
1590 /* struct siginfo */
1591 siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1592 siginfo_type
->set_name (xstrdup ("siginfo"));
1593 append_composite_type_field (siginfo_type
, "si_signo", int_type
);
1594 append_composite_type_field (siginfo_type
, "si_errno", int_type
);
1595 append_composite_type_field (siginfo_type
, "si_code", int_type
);
1596 append_composite_type_field (siginfo_type
, "si_pid", pid_type
);
1597 append_composite_type_field (siginfo_type
, "si_uid", uid_type
);
1598 append_composite_type_field (siginfo_type
, "si_status", int_type
);
1599 append_composite_type_field (siginfo_type
, "si_addr", void_ptr_type
);
1600 append_composite_type_field (siginfo_type
, "si_value", sigval_type
);
1601 append_composite_type_field (siginfo_type
, "_reason", reason_type
);
1603 fbsd_gdbarch_data
->siginfo_type
= siginfo_type
;
1605 return siginfo_type
;
1608 /* Implement the "gdb_signal_from_target" gdbarch method. */
1610 static enum gdb_signal
1611 fbsd_gdb_signal_from_target (struct gdbarch
*gdbarch
, int signal
)
1616 return GDB_SIGNAL_0
;
1618 case FREEBSD_SIGHUP
:
1619 return GDB_SIGNAL_HUP
;
1621 case FREEBSD_SIGINT
:
1622 return GDB_SIGNAL_INT
;
1624 case FREEBSD_SIGQUIT
:
1625 return GDB_SIGNAL_QUIT
;
1627 case FREEBSD_SIGILL
:
1628 return GDB_SIGNAL_ILL
;
1630 case FREEBSD_SIGTRAP
:
1631 return GDB_SIGNAL_TRAP
;
1633 case FREEBSD_SIGABRT
:
1634 return GDB_SIGNAL_ABRT
;
1636 case FREEBSD_SIGEMT
:
1637 return GDB_SIGNAL_EMT
;
1639 case FREEBSD_SIGFPE
:
1640 return GDB_SIGNAL_FPE
;
1642 case FREEBSD_SIGKILL
:
1643 return GDB_SIGNAL_KILL
;
1645 case FREEBSD_SIGBUS
:
1646 return GDB_SIGNAL_BUS
;
1648 case FREEBSD_SIGSEGV
:
1649 return GDB_SIGNAL_SEGV
;
1651 case FREEBSD_SIGSYS
:
1652 return GDB_SIGNAL_SYS
;
1654 case FREEBSD_SIGPIPE
:
1655 return GDB_SIGNAL_PIPE
;
1657 case FREEBSD_SIGALRM
:
1658 return GDB_SIGNAL_ALRM
;
1660 case FREEBSD_SIGTERM
:
1661 return GDB_SIGNAL_TERM
;
1663 case FREEBSD_SIGURG
:
1664 return GDB_SIGNAL_URG
;
1666 case FREEBSD_SIGSTOP
:
1667 return GDB_SIGNAL_STOP
;
1669 case FREEBSD_SIGTSTP
:
1670 return GDB_SIGNAL_TSTP
;
1672 case FREEBSD_SIGCONT
:
1673 return GDB_SIGNAL_CONT
;
1675 case FREEBSD_SIGCHLD
:
1676 return GDB_SIGNAL_CHLD
;
1678 case FREEBSD_SIGTTIN
:
1679 return GDB_SIGNAL_TTIN
;
1681 case FREEBSD_SIGTTOU
:
1682 return GDB_SIGNAL_TTOU
;
1685 return GDB_SIGNAL_IO
;
1687 case FREEBSD_SIGXCPU
:
1688 return GDB_SIGNAL_XCPU
;
1690 case FREEBSD_SIGXFSZ
:
1691 return GDB_SIGNAL_XFSZ
;
1693 case FREEBSD_SIGVTALRM
:
1694 return GDB_SIGNAL_VTALRM
;
1696 case FREEBSD_SIGPROF
:
1697 return GDB_SIGNAL_PROF
;
1699 case FREEBSD_SIGWINCH
:
1700 return GDB_SIGNAL_WINCH
;
1702 case FREEBSD_SIGINFO
:
1703 return GDB_SIGNAL_INFO
;
1705 case FREEBSD_SIGUSR1
:
1706 return GDB_SIGNAL_USR1
;
1708 case FREEBSD_SIGUSR2
:
1709 return GDB_SIGNAL_USR2
;
1711 /* SIGTHR is the same as SIGLWP on FreeBSD. */
1712 case FREEBSD_SIGTHR
:
1713 return GDB_SIGNAL_LWP
;
1715 case FREEBSD_SIGLIBRT
:
1716 return GDB_SIGNAL_LIBRT
;
1719 if (signal
>= FREEBSD_SIGRTMIN
&& signal
<= FREEBSD_SIGRTMAX
)
1721 int offset
= signal
- FREEBSD_SIGRTMIN
;
1723 return (enum gdb_signal
) ((int) GDB_SIGNAL_REALTIME_65
+ offset
);
1726 return GDB_SIGNAL_UNKNOWN
;
1729 /* Implement the "gdb_signal_to_target" gdbarch method. */
1732 fbsd_gdb_signal_to_target (struct gdbarch
*gdbarch
,
1733 enum gdb_signal signal
)
1740 case GDB_SIGNAL_HUP
:
1741 return FREEBSD_SIGHUP
;
1743 case GDB_SIGNAL_INT
:
1744 return FREEBSD_SIGINT
;
1746 case GDB_SIGNAL_QUIT
:
1747 return FREEBSD_SIGQUIT
;
1749 case GDB_SIGNAL_ILL
:
1750 return FREEBSD_SIGILL
;
1752 case GDB_SIGNAL_TRAP
:
1753 return FREEBSD_SIGTRAP
;
1755 case GDB_SIGNAL_ABRT
:
1756 return FREEBSD_SIGABRT
;
1758 case GDB_SIGNAL_EMT
:
1759 return FREEBSD_SIGEMT
;
1761 case GDB_SIGNAL_FPE
:
1762 return FREEBSD_SIGFPE
;
1764 case GDB_SIGNAL_KILL
:
1765 return FREEBSD_SIGKILL
;
1767 case GDB_SIGNAL_BUS
:
1768 return FREEBSD_SIGBUS
;
1770 case GDB_SIGNAL_SEGV
:
1771 return FREEBSD_SIGSEGV
;
1773 case GDB_SIGNAL_SYS
:
1774 return FREEBSD_SIGSYS
;
1776 case GDB_SIGNAL_PIPE
:
1777 return FREEBSD_SIGPIPE
;
1779 case GDB_SIGNAL_ALRM
:
1780 return FREEBSD_SIGALRM
;
1782 case GDB_SIGNAL_TERM
:
1783 return FREEBSD_SIGTERM
;
1785 case GDB_SIGNAL_URG
:
1786 return FREEBSD_SIGURG
;
1788 case GDB_SIGNAL_STOP
:
1789 return FREEBSD_SIGSTOP
;
1791 case GDB_SIGNAL_TSTP
:
1792 return FREEBSD_SIGTSTP
;
1794 case GDB_SIGNAL_CONT
:
1795 return FREEBSD_SIGCONT
;
1797 case GDB_SIGNAL_CHLD
:
1798 return FREEBSD_SIGCHLD
;
1800 case GDB_SIGNAL_TTIN
:
1801 return FREEBSD_SIGTTIN
;
1803 case GDB_SIGNAL_TTOU
:
1804 return FREEBSD_SIGTTOU
;
1807 return FREEBSD_SIGIO
;
1809 case GDB_SIGNAL_XCPU
:
1810 return FREEBSD_SIGXCPU
;
1812 case GDB_SIGNAL_XFSZ
:
1813 return FREEBSD_SIGXFSZ
;
1815 case GDB_SIGNAL_VTALRM
:
1816 return FREEBSD_SIGVTALRM
;
1818 case GDB_SIGNAL_PROF
:
1819 return FREEBSD_SIGPROF
;
1821 case GDB_SIGNAL_WINCH
:
1822 return FREEBSD_SIGWINCH
;
1824 case GDB_SIGNAL_INFO
:
1825 return FREEBSD_SIGINFO
;
1827 case GDB_SIGNAL_USR1
:
1828 return FREEBSD_SIGUSR1
;
1830 case GDB_SIGNAL_USR2
:
1831 return FREEBSD_SIGUSR2
;
1833 case GDB_SIGNAL_LWP
:
1834 return FREEBSD_SIGTHR
;
1836 case GDB_SIGNAL_LIBRT
:
1837 return FREEBSD_SIGLIBRT
;
1840 if (signal
>= GDB_SIGNAL_REALTIME_65
1841 && signal
<= GDB_SIGNAL_REALTIME_126
)
1843 int offset
= signal
- GDB_SIGNAL_REALTIME_65
;
1845 return FREEBSD_SIGRTMIN
+ offset
;
1851 /* Implement the "get_syscall_number" gdbarch method. */
1854 fbsd_get_syscall_number (struct gdbarch
*gdbarch
, thread_info
*thread
)
1857 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1858 native targets fetch the system call number from the
1859 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1860 However, system call catching requires this function to be
1863 internal_error (__FILE__
, __LINE__
, _("fbsd_get_sycall_number called"));
1866 /* Read an integer symbol value from the current target. */
1869 fbsd_read_integer_by_name (struct gdbarch
*gdbarch
, const char *name
)
1871 bound_minimal_symbol ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
1872 if (ms
.minsym
== NULL
)
1873 error (_("Unable to resolve symbol '%s'"), name
);
1876 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms
), buf
, sizeof buf
) != 0)
1877 error (_("Unable to read value of '%s'"), name
);
1879 return extract_signed_integer (buf
, sizeof buf
, gdbarch_byte_order (gdbarch
));
1882 /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
1883 structure needed to determine the TLS index of an object file. */
1886 fbsd_fetch_rtld_offsets (struct gdbarch
*gdbarch
, struct fbsd_pspace_data
*data
)
1890 /* Fetch offsets from debug symbols in rtld. */
1891 struct symbol
*obj_entry_sym
1892 = lookup_symbol_in_language ("Struct_Obj_Entry", NULL
, STRUCT_DOMAIN
,
1893 language_c
, NULL
).symbol
;
1894 if (obj_entry_sym
== NULL
)
1895 error (_("Unable to find Struct_Obj_Entry symbol"));
1896 data
->off_linkmap
= lookup_struct_elt (SYMBOL_TYPE (obj_entry_sym
),
1897 "linkmap", 0).offset
/ 8;
1898 data
->off_tlsindex
= lookup_struct_elt (SYMBOL_TYPE (obj_entry_sym
),
1899 "tlsindex", 0).offset
/ 8;
1900 data
->rtld_offsets_valid
= true;
1903 catch (const gdb_exception_error
&e
)
1905 data
->off_linkmap
= -1;
1910 /* Fetch offsets from global variables in libthr. Note that
1911 this does not work for single-threaded processes that are not
1912 linked against libthr. */
1913 data
->off_linkmap
= fbsd_read_integer_by_name (gdbarch
,
1914 "_thread_off_linkmap");
1915 data
->off_tlsindex
= fbsd_read_integer_by_name (gdbarch
,
1916 "_thread_off_tlsindex");
1917 data
->rtld_offsets_valid
= true;
1920 catch (const gdb_exception_error
&e
)
1922 data
->off_linkmap
= -1;
1926 /* Helper function to read the TLS index of an object file associated
1927 with a link map entry at LM_ADDR. */
1930 fbsd_get_tls_index (struct gdbarch
*gdbarch
, CORE_ADDR lm_addr
)
1932 struct fbsd_pspace_data
*data
= get_fbsd_pspace_data (current_program_space
);
1934 if (!data
->rtld_offsets_valid
)
1935 fbsd_fetch_rtld_offsets (gdbarch
, data
);
1937 if (data
->off_linkmap
== -1)
1938 throw_error (TLS_GENERIC_ERROR
,
1939 _("Cannot fetch runtime linker structure offsets"));
1941 /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
1942 pointer and then compute the offset of the tlsindex member. */
1943 CORE_ADDR tlsindex_addr
= lm_addr
- data
->off_linkmap
+ data
->off_tlsindex
;
1946 if (target_read_memory (tlsindex_addr
, buf
, sizeof buf
) != 0)
1947 throw_error (TLS_GENERIC_ERROR
,
1948 _("Cannot find thread-local variables on this target"));
1950 return extract_signed_integer (buf
, sizeof buf
, gdbarch_byte_order (gdbarch
));
1953 /* See fbsd-tdep.h. */
1956 fbsd_get_thread_local_address (struct gdbarch
*gdbarch
, CORE_ADDR dtv_addr
,
1957 CORE_ADDR lm_addr
, CORE_ADDR offset
)
1959 LONGEST tls_index
= fbsd_get_tls_index (gdbarch
, lm_addr
);
1961 gdb_byte buf
[gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
];
1962 if (target_read_memory (dtv_addr
, buf
, sizeof buf
) != 0)
1963 throw_error (TLS_GENERIC_ERROR
,
1964 _("Cannot find thread-local variables on this target"));
1966 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1967 CORE_ADDR addr
= gdbarch_pointer_to_address (gdbarch
,
1968 builtin
->builtin_data_ptr
, buf
);
1970 addr
+= (tls_index
+ 1) * TYPE_LENGTH (builtin
->builtin_data_ptr
);
1971 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
1972 throw_error (TLS_GENERIC_ERROR
,
1973 _("Cannot find thread-local variables on this target"));
1975 addr
= gdbarch_pointer_to_address (gdbarch
, builtin
->builtin_data_ptr
, buf
);
1976 return addr
+ offset
;
1979 /* See fbsd-tdep.h. */
1982 fbsd_skip_solib_resolver (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1984 struct bound_minimal_symbol msym
= lookup_bound_minimal_symbol ("_rtld_bind");
1985 if (msym
.minsym
!= nullptr && BMSYMBOL_VALUE_ADDRESS (msym
) == pc
)
1986 return frame_unwind_caller_pc (get_current_frame ());
1991 /* To be called from GDB_OSABI_FREEBSD handlers. */
1994 fbsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1996 set_gdbarch_core_pid_to_str (gdbarch
, fbsd_core_pid_to_str
);
1997 set_gdbarch_core_thread_name (gdbarch
, fbsd_core_thread_name
);
1998 set_gdbarch_core_xfer_siginfo (gdbarch
, fbsd_core_xfer_siginfo
);
1999 set_gdbarch_make_corefile_notes (gdbarch
, fbsd_make_corefile_notes
);
2000 set_gdbarch_core_info_proc (gdbarch
, fbsd_core_info_proc
);
2001 set_gdbarch_print_auxv_entry (gdbarch
, fbsd_print_auxv_entry
);
2002 set_gdbarch_get_siginfo_type (gdbarch
, fbsd_get_siginfo_type
);
2003 set_gdbarch_gdb_signal_from_target (gdbarch
, fbsd_gdb_signal_from_target
);
2004 set_gdbarch_gdb_signal_to_target (gdbarch
, fbsd_gdb_signal_to_target
);
2005 set_gdbarch_skip_solib_resolver (gdbarch
, fbsd_skip_solib_resolver
);
2007 /* `catch syscall' */
2008 set_xml_syscall_file_name (gdbarch
, "syscalls/freebsd.xml");
2009 set_gdbarch_get_syscall_number (gdbarch
, fbsd_get_syscall_number
);
2012 void _initialize_fbsd_tdep ();
2014 _initialize_fbsd_tdep ()
2016 fbsd_gdbarch_data_handle
=
2017 gdbarch_data_register_post_init (init_fbsd_gdbarch_data
);