1 /* Target-dependent code for FreeBSD, architecture-independent.
3 Copyright (C) 2002-2020 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"
36 /* This enum is derived from FreeBSD's <sys/signal.h>. */
65 FREEBSD_SIGVTALRM
= 26,
67 FREEBSD_SIGWINCH
= 28,
72 FREEBSD_SIGLIBRT
= 33,
73 FREEBSD_SIGRTMIN
= 65,
74 FREEBSD_SIGRTMAX
= 126,
77 /* FreeBSD kernels 12.0 and later include a copy of the
78 'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
79 operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
80 The constants below define the offset of field members and flags in
81 this structure used by methods in this file. Note that the
82 'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
83 containing the size of the structure. */
85 #define LWPINFO_OFFSET 0x4
87 /* Offsets in ptrace_lwpinfo. */
88 #define LWPINFO_PL_FLAGS 0x8
89 #define LWPINFO64_PL_SIGINFO 0x30
90 #define LWPINFO32_PL_SIGINFO 0x2c
92 /* Flags in pl_flags. */
93 #define PL_FLAG_SI 0x20 /* siginfo is valid */
95 /* Sizes of siginfo_t. */
96 #define SIZE64_SIGINFO_T 80
97 #define SIZE32_SIGINFO_T 64
99 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
100 dump notes. See <sys/user.h> for the definition of struct
101 kinfo_vmentry. This data structure should have the same layout on
104 Note that FreeBSD 7.0 used an older version of this structure
105 (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
106 dump note wasn't introduced until FreeBSD 9.2. As a result, the
107 core dump note has always used the 7.1 and later structure
110 #define KVE_STRUCTSIZE 0x0
111 #define KVE_START 0x8
113 #define KVE_OFFSET 0x18
114 #define KVE_FLAGS 0x2c
115 #define KVE_PROTECTION 0x38
116 #define KVE_PATH 0x88
118 /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
119 match the KVME_PROT_* constants in <sys/user.h>. */
121 #define KINFO_VME_PROT_READ 0x00000001
122 #define KINFO_VME_PROT_WRITE 0x00000002
123 #define KINFO_VME_PROT_EXEC 0x00000004
125 /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
126 match the KVME_FLAG_* constants in <sys/user.h>. */
128 #define KINFO_VME_FLAG_COW 0x00000001
129 #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
130 #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
131 #define KINFO_VME_FLAG_SUPER 0x00000008
132 #define KINFO_VME_FLAG_GROWS_UP 0x00000010
133 #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
135 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
136 dump notes. See <sys/user.h> for the definition of struct
137 kinfo_file. This data structure should have the same layout on all
140 Note that FreeBSD 7.0 used an older version of this structure
141 (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
142 note wasn't introduced until FreeBSD 9.2. As a result, the core
143 dump note has always used the 7.1 and later structure format. */
145 #define KF_STRUCTSIZE 0x0
148 #define KF_FLAGS 0x10
149 #define KF_OFFSET 0x18
150 #define KF_VNODE_TYPE 0x20
151 #define KF_SOCK_DOMAIN 0x24
152 #define KF_SOCK_TYPE 0x28
153 #define KF_SOCK_PROTOCOL 0x2c
154 #define KF_SA_LOCAL 0x30
155 #define KF_SA_PEER 0xb0
156 #define KF_PATH 0x170
158 /* Constants for the 'kf_type' field in struct kinfo_file. These
159 match the KF_TYPE_* constants in <sys/user.h>. */
161 #define KINFO_FILE_TYPE_VNODE 1
162 #define KINFO_FILE_TYPE_SOCKET 2
163 #define KINFO_FILE_TYPE_PIPE 3
164 #define KINFO_FILE_TYPE_FIFO 4
165 #define KINFO_FILE_TYPE_KQUEUE 5
166 #define KINFO_FILE_TYPE_CRYPTO 6
167 #define KINFO_FILE_TYPE_MQUEUE 7
168 #define KINFO_FILE_TYPE_SHM 8
169 #define KINFO_FILE_TYPE_SEM 9
170 #define KINFO_FILE_TYPE_PTS 10
171 #define KINFO_FILE_TYPE_PROCDESC 11
173 /* Special values for the 'kf_fd' field in struct kinfo_file. These
174 match the KF_FD_TYPE_* constants in <sys/user.h>. */
176 #define KINFO_FILE_FD_TYPE_CWD -1
177 #define KINFO_FILE_FD_TYPE_ROOT -2
178 #define KINFO_FILE_FD_TYPE_JAIL -3
179 #define KINFO_FILE_FD_TYPE_TRACE -4
180 #define KINFO_FILE_FD_TYPE_TEXT -5
181 #define KINFO_FILE_FD_TYPE_CTTY -6
183 /* Flags in the 'kf_flags' field in struct kinfo_file. These match
184 the KF_FLAG_* constants in <sys/user.h>. */
186 #define KINFO_FILE_FLAG_READ 0x00000001
187 #define KINFO_FILE_FLAG_WRITE 0x00000002
188 #define KINFO_FILE_FLAG_APPEND 0x00000004
189 #define KINFO_FILE_FLAG_ASYNC 0x00000008
190 #define KINFO_FILE_FLAG_FSYNC 0x00000010
191 #define KINFO_FILE_FLAG_NONBLOCK 0x00000020
192 #define KINFO_FILE_FLAG_DIRECT 0x00000040
193 #define KINFO_FILE_FLAG_HASLOCK 0x00000080
194 #define KINFO_FILE_FLAG_EXEC 0x00004000
196 /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
197 These match the KF_VTYPE_* constants in <sys/user.h>. */
199 #define KINFO_FILE_VTYPE_VREG 1
200 #define KINFO_FILE_VTYPE_VDIR 2
201 #define KINFO_FILE_VTYPE_VCHR 4
202 #define KINFO_FILE_VTYPE_VLNK 5
203 #define KINFO_FILE_VTYPE_VSOCK 6
204 #define KINFO_FILE_VTYPE_VFIFO 7
206 /* Constants for socket address families. These match AF_* constants
207 in <sys/socket.h>. */
209 #define FBSD_AF_UNIX 1
210 #define FBSD_AF_INET 2
211 #define FBSD_AF_INET6 28
213 /* Constants for socket types. These match SOCK_* constants in
216 #define FBSD_SOCK_STREAM 1
217 #define FBSD_SOCK_DGRAM 2
218 #define FBSD_SOCK_SEQPACKET 5
220 /* Constants for IP protocols. These match IPPROTO_* constants in
223 #define FBSD_IPPROTO_ICMP 1
224 #define FBSD_IPPROTO_TCP 6
225 #define FBSD_IPPROTO_UDP 17
226 #define FBSD_IPPROTO_SCTP 132
228 /* Socket address structures. These have the same layout on all
229 FreeBSD architectures. In addition, multibyte fields such as IP
230 addresses are always stored in network byte order. */
232 struct fbsd_sockaddr_in
241 struct fbsd_sockaddr_in6
245 uint8_t sin6_port
[2];
246 uint32_t sin6_flowinfo
;
247 uint8_t sin6_addr
[16];
248 uint32_t sin6_scope_id
;
251 struct fbsd_sockaddr_un
258 /* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
259 <sys/_sigset.h> and is the same value on all architectures. */
263 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
264 dump notes. See <sys/user.h> for the definition of struct
265 kinfo_proc. This data structure has different layouts on different
266 architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
267 uses a 32-bit time_t while all other architectures use a 64-bit
270 The core dump note actually contains one kinfo_proc structure for
271 each thread, but all of the process-wide data can be obtained from
272 the first structure. One result of this note's format is that some
273 of the process-wide status available in the native target method
274 from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
275 is not available from a core dump. Instead, the per-thread data
276 structures contain the value of these fields for individual
279 struct kinfo_proc_layout
281 /* Offsets of struct kinfo_proc members. */
288 int ki_tdev_freebsd11
;
310 /* Offsets of struct rusage members. */
318 const struct kinfo_proc_layout kinfo_proc_layout_32
=
326 .ki_tdev_freebsd11
= 0x44,
327 .ki_sigignore
= 0x68,
346 .ki_rusage_ch
= 0x278,
355 const struct kinfo_proc_layout kinfo_proc_layout_i386
=
363 .ki_tdev_freebsd11
= 0x44,
364 .ki_sigignore
= 0x68,
383 .ki_rusage_ch
= 0x258,
392 const struct kinfo_proc_layout kinfo_proc_layout_64
=
400 .ki_tdev_freebsd11
= 0x64,
401 .ki_sigignore
= 0x88,
420 .ki_rusage_ch
= 0x2f0,
429 static struct gdbarch_data
*fbsd_gdbarch_data_handle
;
431 struct fbsd_gdbarch_data
433 struct type
*siginfo_type
;
437 init_fbsd_gdbarch_data (struct gdbarch
*gdbarch
)
439 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct fbsd_gdbarch_data
);
442 static struct fbsd_gdbarch_data
*
443 get_fbsd_gdbarch_data (struct gdbarch
*gdbarch
)
445 return ((struct fbsd_gdbarch_data
*)
446 gdbarch_data (gdbarch
, fbsd_gdbarch_data_handle
));
449 struct fbsd_pspace_data
451 /* Offsets in the runtime linker's 'Obj_Entry' structure. */
452 LONGEST off_linkmap
= 0;
453 LONGEST off_tlsindex
= 0;
454 bool rtld_offsets_valid
= false;
457 /* Per-program-space data for FreeBSD architectures. */
458 static const struct program_space_key
<fbsd_pspace_data
>
459 fbsd_pspace_data_handle
;
461 static struct fbsd_pspace_data
*
462 get_fbsd_pspace_data (struct program_space
*pspace
)
464 struct fbsd_pspace_data
*data
;
466 data
= fbsd_pspace_data_handle
.get (pspace
);
468 data
= fbsd_pspace_data_handle
.emplace (pspace
);
473 /* This is how we want PTIDs from core files to be printed. */
476 fbsd_core_pid_to_str (struct gdbarch
*gdbarch
, ptid_t ptid
)
478 if (ptid
.lwp () != 0)
479 return string_printf ("LWP %ld", ptid
.lwp ());
481 return normal_pid_to_str (ptid
);
484 /* Extract the name assigned to a thread from a core. Returns the
485 string in a static buffer. */
488 fbsd_core_thread_name (struct gdbarch
*gdbarch
, struct thread_info
*thr
)
491 struct bfd_section
*section
;
494 if (thr
->ptid
.lwp () != 0)
496 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
497 whose contents are defined by a "struct thrmisc" declared in
498 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
499 a null-terminated string as the first member of the
500 structure. Rather than define the full structure here, just
501 extract the null-terminated name from the start of the
503 thread_section_name
section_name (".thrmisc", thr
->ptid
);
505 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
506 if (section
!= NULL
&& bfd_section_size (section
) > 0)
508 /* Truncate the name if it is longer than "buf". */
509 size
= bfd_section_size (section
);
510 if (size
> sizeof buf
- 1)
511 size
= sizeof buf
- 1;
512 if (bfd_get_section_contents (core_bfd
, section
, buf
, (file_ptr
) 0,
518 /* Note that each thread will report the process command
519 as its thread name instead of an empty name if a name
520 has not been set explicitly. Return a NULL name in
522 if (strcmp (buf
, elf_tdata (core_bfd
)->core
->program
) != 0)
531 /* Implement the "core_xfer_siginfo" gdbarch method. */
534 fbsd_core_xfer_siginfo (struct gdbarch
*gdbarch
, gdb_byte
*readbuf
,
535 ULONGEST offset
, ULONGEST len
)
539 if (gdbarch_long_bit (gdbarch
) == 32)
540 siginfo_size
= SIZE32_SIGINFO_T
;
542 siginfo_size
= SIZE64_SIGINFO_T
;
543 if (offset
> siginfo_size
)
546 thread_section_name
section_name (".note.freebsdcore.lwpinfo", inferior_ptid
);
547 asection
*section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
552 if (!bfd_get_section_contents (core_bfd
, section
, buf
,
553 LWPINFO_OFFSET
+ LWPINFO_PL_FLAGS
, 4))
556 int pl_flags
= extract_signed_integer (buf
, 4, gdbarch_byte_order (gdbarch
));
557 if (!(pl_flags
& PL_FLAG_SI
))
560 if (offset
+ len
> siginfo_size
)
561 len
= siginfo_size
- offset
;
563 ULONGEST siginfo_offset
;
564 if (gdbarch_long_bit (gdbarch
) == 32)
565 siginfo_offset
= LWPINFO_OFFSET
+ LWPINFO32_PL_SIGINFO
;
567 siginfo_offset
= LWPINFO_OFFSET
+ LWPINFO64_PL_SIGINFO
;
569 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
570 siginfo_offset
+ offset
, len
))
577 find_signalled_thread (struct thread_info
*info
, void *data
)
579 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
580 && info
->ptid
.pid () == inferior_ptid
.pid ())
586 /* Structure for passing information from
587 fbsd_collect_thread_registers via an iterator to
588 fbsd_collect_regset_section_cb. */
590 struct fbsd_collect_regset_section_cb_data
592 const struct regcache
*regcache
;
597 enum gdb_signal stop_signal
;
602 fbsd_collect_regset_section_cb (const char *sect_name
, int supply_size
,
603 int collect_size
, const struct regset
*regset
,
604 const char *human_name
, void *cb_data
)
607 struct fbsd_collect_regset_section_cb_data
*data
608 = (struct fbsd_collect_regset_section_cb_data
*) cb_data
;
610 if (data
->abort_iteration
)
613 gdb_assert (regset
->collect_regset
);
615 buf
= (char *) xmalloc (collect_size
);
616 regset
->collect_regset (regset
, data
->regcache
, -1, buf
, collect_size
);
618 /* PRSTATUS still needs to be treated specially. */
619 if (strcmp (sect_name
, ".reg") == 0)
620 data
->note_data
= (char *) elfcore_write_prstatus
621 (data
->obfd
, data
->note_data
, data
->note_size
, data
->lwp
,
622 gdb_signal_to_host (data
->stop_signal
), buf
);
624 data
->note_data
= (char *) elfcore_write_register_note
625 (data
->obfd
, data
->note_data
, data
->note_size
,
626 sect_name
, buf
, collect_size
);
629 if (data
->note_data
== NULL
)
630 data
->abort_iteration
= 1;
633 /* Records the thread's register state for the corefile note
637 fbsd_collect_thread_registers (const struct regcache
*regcache
,
638 ptid_t ptid
, bfd
*obfd
,
639 char *note_data
, int *note_size
,
640 enum gdb_signal stop_signal
)
642 struct gdbarch
*gdbarch
= regcache
->arch ();
643 struct fbsd_collect_regset_section_cb_data data
;
645 data
.regcache
= regcache
;
647 data
.note_data
= note_data
;
648 data
.note_size
= note_size
;
649 data
.stop_signal
= stop_signal
;
650 data
.abort_iteration
= 0;
651 data
.lwp
= ptid
.lwp ();
653 gdbarch_iterate_over_regset_sections (gdbarch
,
654 fbsd_collect_regset_section_cb
,
656 return data
.note_data
;
659 struct fbsd_corefile_thread_data
661 struct gdbarch
*gdbarch
;
665 enum gdb_signal stop_signal
;
668 /* Records the thread's register state for the corefile note
672 fbsd_corefile_thread (struct thread_info
*info
,
673 struct fbsd_corefile_thread_data
*args
)
675 struct regcache
*regcache
;
677 regcache
= get_thread_arch_regcache (info
->inf
->process_target (),
678 info
->ptid
, args
->gdbarch
);
680 target_fetch_registers (regcache
, -1);
682 args
->note_data
= fbsd_collect_thread_registers
683 (regcache
, info
->ptid
, args
->obfd
, args
->note_data
,
684 args
->note_size
, args
->stop_signal
);
687 /* Return a byte_vector containing the contents of a core dump note
688 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
689 the data is prefixed with a 32-bit integer size to match the format
690 used in FreeBSD NT_PROCSTAT_* notes. */
692 static gdb::optional
<gdb::byte_vector
>
693 fbsd_make_note_desc (enum target_object object
, uint32_t structsize
)
695 gdb::optional
<gdb::byte_vector
> buf
=
696 target_read_alloc (current_top_target (), object
, NULL
);
697 if (!buf
|| buf
->empty ())
703 gdb::byte_vector
desc (sizeof (structsize
) + buf
->size ());
704 memcpy (desc
.data (), &structsize
, sizeof (structsize
));
705 memcpy (desc
.data () + sizeof (structsize
), buf
->data (), buf
->size ());
709 /* Create appropriate note sections for a corefile, returning them in
713 fbsd_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
715 struct fbsd_corefile_thread_data thread_args
;
716 char *note_data
= NULL
;
717 Elf_Internal_Ehdr
*i_ehdrp
;
718 struct thread_info
*curr_thr
, *signalled_thr
;
720 /* Put a "FreeBSD" label in the ELF header. */
721 i_ehdrp
= elf_elfheader (obfd
);
722 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
724 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch
));
726 if (get_exec_file (0))
728 const char *fname
= lbasename (get_exec_file (0));
729 std::string psargs
= fname
;
731 const char *infargs
= get_inferior_args ();
733 psargs
= psargs
+ " " + infargs
;
735 note_data
= elfcore_write_prpsinfo (obfd
, note_data
, note_size
,
736 fname
, psargs
.c_str ());
739 /* Thread register information. */
742 update_thread_list ();
744 catch (const gdb_exception_error
&e
)
746 exception_print (gdb_stderr
, e
);
749 /* Like the kernel, prefer dumping the signalled thread first.
750 "First thread" is what tools use to infer the signalled thread.
751 In case there's more than one signalled thread, prefer the
752 current thread, if it is signalled. */
753 curr_thr
= inferior_thread ();
754 if (curr_thr
->suspend
.stop_signal
!= GDB_SIGNAL_0
)
755 signalled_thr
= curr_thr
;
758 signalled_thr
= iterate_over_threads (find_signalled_thread
, NULL
);
759 if (signalled_thr
== NULL
)
760 signalled_thr
= curr_thr
;
763 thread_args
.gdbarch
= gdbarch
;
764 thread_args
.obfd
= obfd
;
765 thread_args
.note_data
= note_data
;
766 thread_args
.note_size
= note_size
;
767 thread_args
.stop_signal
= signalled_thr
->suspend
.stop_signal
;
769 fbsd_corefile_thread (signalled_thr
, &thread_args
);
770 for (thread_info
*thr
: current_inferior ()->non_exited_threads ())
772 if (thr
== signalled_thr
)
775 fbsd_corefile_thread (thr
, &thread_args
);
778 note_data
= thread_args
.note_data
;
780 /* Auxiliary vector. */
781 uint32_t structsize
= gdbarch_ptr_bit (gdbarch
) / 4; /* Elf_Auxinfo */
782 gdb::optional
<gdb::byte_vector
> note_desc
=
783 fbsd_make_note_desc (TARGET_OBJECT_AUXV
, structsize
);
784 if (note_desc
&& !note_desc
->empty ())
786 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
787 NT_FREEBSD_PROCSTAT_AUXV
,
788 note_desc
->data (), note_desc
->size ());
793 /* Virtual memory mappings. */
794 note_desc
= fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP
, 0);
795 if (note_desc
&& !note_desc
->empty ())
797 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
798 NT_FREEBSD_PROCSTAT_VMMAP
,
799 note_desc
->data (), note_desc
->size ());
804 note_desc
= fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS
, 0);
805 if (note_desc
&& !note_desc
->empty ())
807 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
808 NT_FREEBSD_PROCSTAT_PSSTRINGS
,
809 note_desc
->data (), note_desc
->size ());
817 /* Helper function to generate the file descriptor description for a
818 single open file in 'info proc files'. */
821 fbsd_file_fd (int kf_fd
)
825 case KINFO_FILE_FD_TYPE_CWD
:
827 case KINFO_FILE_FD_TYPE_ROOT
:
829 case KINFO_FILE_FD_TYPE_JAIL
:
831 case KINFO_FILE_FD_TYPE_TRACE
:
833 case KINFO_FILE_FD_TYPE_TEXT
:
835 case KINFO_FILE_FD_TYPE_CTTY
:
838 return int_string (kf_fd
, 10, 1, 0, 0);
842 /* Helper function to generate the file type for a single open file in
843 'info proc files'. */
846 fbsd_file_type (int kf_type
, int kf_vnode_type
)
850 case KINFO_FILE_TYPE_VNODE
:
851 switch (kf_vnode_type
)
853 case KINFO_FILE_VTYPE_VREG
:
855 case KINFO_FILE_VTYPE_VDIR
:
857 case KINFO_FILE_VTYPE_VCHR
:
859 case KINFO_FILE_VTYPE_VLNK
:
861 case KINFO_FILE_VTYPE_VSOCK
:
863 case KINFO_FILE_VTYPE_VFIFO
:
867 char *str
= get_print_cell ();
869 xsnprintf (str
, PRINT_CELL_SIZE
, "vn:%d", kf_vnode_type
);
873 case KINFO_FILE_TYPE_SOCKET
:
875 case KINFO_FILE_TYPE_PIPE
:
877 case KINFO_FILE_TYPE_FIFO
:
879 case KINFO_FILE_TYPE_KQUEUE
:
881 case KINFO_FILE_TYPE_CRYPTO
:
883 case KINFO_FILE_TYPE_MQUEUE
:
885 case KINFO_FILE_TYPE_SHM
:
887 case KINFO_FILE_TYPE_SEM
:
889 case KINFO_FILE_TYPE_PTS
:
891 case KINFO_FILE_TYPE_PROCDESC
:
894 return int_string (kf_type
, 10, 1, 0, 0);
898 /* Helper function to generate the file flags for a single open file in
899 'info proc files'. */
902 fbsd_file_flags (int kf_flags
)
904 static char file_flags
[10];
906 file_flags
[0] = (kf_flags
& KINFO_FILE_FLAG_READ
) ? 'r' : '-';
907 file_flags
[1] = (kf_flags
& KINFO_FILE_FLAG_WRITE
) ? 'w' : '-';
908 file_flags
[2] = (kf_flags
& KINFO_FILE_FLAG_EXEC
) ? 'x' : '-';
909 file_flags
[3] = (kf_flags
& KINFO_FILE_FLAG_APPEND
) ? 'a' : '-';
910 file_flags
[4] = (kf_flags
& KINFO_FILE_FLAG_ASYNC
) ? 's' : '-';
911 file_flags
[5] = (kf_flags
& KINFO_FILE_FLAG_FSYNC
) ? 'f' : '-';
912 file_flags
[6] = (kf_flags
& KINFO_FILE_FLAG_NONBLOCK
) ? 'n' : '-';
913 file_flags
[7] = (kf_flags
& KINFO_FILE_FLAG_DIRECT
) ? 'd' : '-';
914 file_flags
[8] = (kf_flags
& KINFO_FILE_FLAG_HASLOCK
) ? 'l' : '-';
915 file_flags
[9] = '\0';
920 /* Helper function to generate the name of an IP protocol. */
923 fbsd_ipproto (int protocol
)
927 case FBSD_IPPROTO_ICMP
:
929 case FBSD_IPPROTO_TCP
:
931 case FBSD_IPPROTO_UDP
:
933 case FBSD_IPPROTO_SCTP
:
937 char *str
= get_print_cell ();
939 xsnprintf (str
, PRINT_CELL_SIZE
, "ip<%d>", protocol
);
945 /* Helper function to print out an IPv4 socket address. */
948 fbsd_print_sockaddr_in (const void *sockaddr
)
950 const struct fbsd_sockaddr_in
*sin
=
951 reinterpret_cast<const struct fbsd_sockaddr_in
*> (sockaddr
);
952 char buf
[INET_ADDRSTRLEN
];
954 if (inet_ntop (AF_INET
, sin
->sin_addr
, buf
, sizeof buf
) == nullptr)
955 error (_("Failed to format IPv4 address"));
956 printf_filtered ("%s:%u", buf
,
957 (sin
->sin_port
[0] << 8) | sin
->sin_port
[1]);
960 /* Helper function to print out an IPv6 socket address. */
963 fbsd_print_sockaddr_in6 (const void *sockaddr
)
965 const struct fbsd_sockaddr_in6
*sin6
=
966 reinterpret_cast<const struct fbsd_sockaddr_in6
*> (sockaddr
);
967 char buf
[INET6_ADDRSTRLEN
];
969 if (inet_ntop (AF_INET6
, sin6
->sin6_addr
, buf
, sizeof buf
) == nullptr)
970 error (_("Failed to format IPv6 address"));
971 printf_filtered ("%s.%u", buf
,
972 (sin6
->sin6_port
[0] << 8) | sin6
->sin6_port
[1]);
975 /* See fbsd-tdep.h. */
978 fbsd_info_proc_files_header ()
980 printf_filtered (_("Open files:\n\n"));
981 printf_filtered (" %6s %6s %10s %9s %s\n",
982 "FD", "Type", "Offset", "Flags ", "Name");
985 /* See fbsd-tdep.h. */
988 fbsd_info_proc_files_entry (int kf_type
, int kf_fd
, int kf_flags
,
989 LONGEST kf_offset
, int kf_vnode_type
,
990 int kf_sock_domain
, int kf_sock_type
,
991 int kf_sock_protocol
, const void *kf_sa_local
,
992 const void *kf_sa_peer
, const void *kf_path
)
994 printf_filtered (" %6s %6s %10s %8s ",
995 fbsd_file_fd (kf_fd
),
996 fbsd_file_type (kf_type
, kf_vnode_type
),
997 kf_offset
> -1 ? hex_string (kf_offset
) : "-",
998 fbsd_file_flags (kf_flags
));
999 if (kf_type
== KINFO_FILE_TYPE_SOCKET
)
1001 switch (kf_sock_domain
)
1005 switch (kf_sock_type
)
1007 case FBSD_SOCK_STREAM
:
1008 printf_filtered ("unix stream:");
1010 case FBSD_SOCK_DGRAM
:
1011 printf_filtered ("unix dgram:");
1013 case FBSD_SOCK_SEQPACKET
:
1014 printf_filtered ("unix seqpacket:");
1017 printf_filtered ("unix <%d>:", kf_sock_type
);
1021 /* For local sockets, print out the first non-nul path
1022 rather than both paths. */
1023 const struct fbsd_sockaddr_un
*saddr_un
1024 = reinterpret_cast<const struct fbsd_sockaddr_un
*> (kf_sa_local
);
1025 if (saddr_un
->sun_path
[0] == 0)
1026 saddr_un
= reinterpret_cast<const struct fbsd_sockaddr_un
*>
1028 printf_filtered ("%s", saddr_un
->sun_path
);
1032 printf_filtered ("%s4 ", fbsd_ipproto (kf_sock_protocol
));
1033 fbsd_print_sockaddr_in (kf_sa_local
);
1034 printf_filtered (" -> ");
1035 fbsd_print_sockaddr_in (kf_sa_peer
);
1038 printf_filtered ("%s6 ", fbsd_ipproto (kf_sock_protocol
));
1039 fbsd_print_sockaddr_in6 (kf_sa_local
);
1040 printf_filtered (" -> ");
1041 fbsd_print_sockaddr_in6 (kf_sa_peer
);
1046 printf_filtered ("%s", reinterpret_cast<const char *> (kf_path
));
1047 printf_filtered ("\n");
1050 /* Implement "info proc files" for a corefile. */
1053 fbsd_core_info_proc_files (struct gdbarch
*gdbarch
)
1056 = bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.files");
1057 if (section
== NULL
)
1059 warning (_("unable to find open files in core file"));
1063 size_t note_size
= bfd_section_size (section
);
1065 error (_("malformed core note - too short for header"));
1067 gdb::def_vector
<unsigned char> contents (note_size
);
1068 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1070 error (_("could not get core note contents"));
1072 unsigned char *descdata
= contents
.data ();
1073 unsigned char *descend
= descdata
+ note_size
;
1075 /* Skip over the structure size. */
1078 fbsd_info_proc_files_header ();
1080 while (descdata
+ KF_PATH
< descend
)
1082 ULONGEST structsize
= bfd_get_32 (core_bfd
, descdata
+ KF_STRUCTSIZE
);
1083 if (structsize
< KF_PATH
)
1084 error (_("malformed core note - file structure too small"));
1086 LONGEST type
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_TYPE
);
1087 LONGEST fd
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_FD
);
1088 LONGEST flags
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_FLAGS
);
1089 LONGEST offset
= bfd_get_signed_64 (core_bfd
, descdata
+ KF_OFFSET
);
1090 LONGEST vnode_type
= bfd_get_signed_32 (core_bfd
,
1091 descdata
+ KF_VNODE_TYPE
);
1092 LONGEST sock_domain
= bfd_get_signed_32 (core_bfd
,
1093 descdata
+ KF_SOCK_DOMAIN
);
1094 LONGEST sock_type
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_SOCK_TYPE
);
1095 LONGEST sock_protocol
= bfd_get_signed_32 (core_bfd
,
1096 descdata
+ KF_SOCK_PROTOCOL
);
1097 fbsd_info_proc_files_entry (type
, fd
, flags
, offset
, vnode_type
,
1098 sock_domain
, sock_type
, sock_protocol
,
1099 descdata
+ KF_SA_LOCAL
, descdata
+ KF_SA_PEER
,
1100 descdata
+ KF_PATH
);
1102 descdata
+= structsize
;
1106 /* Helper function to generate mappings flags for a single VM map
1107 entry in 'info proc mappings'. */
1110 fbsd_vm_map_entry_flags (int kve_flags
, int kve_protection
)
1112 static char vm_flags
[9];
1114 vm_flags
[0] = (kve_protection
& KINFO_VME_PROT_READ
) ? 'r' : '-';
1115 vm_flags
[1] = (kve_protection
& KINFO_VME_PROT_WRITE
) ? 'w' : '-';
1116 vm_flags
[2] = (kve_protection
& KINFO_VME_PROT_EXEC
) ? 'x' : '-';
1118 vm_flags
[4] = (kve_flags
& KINFO_VME_FLAG_COW
) ? 'C' : '-';
1119 vm_flags
[5] = (kve_flags
& KINFO_VME_FLAG_NEEDS_COPY
) ? 'N' : '-';
1120 vm_flags
[6] = (kve_flags
& KINFO_VME_FLAG_SUPER
) ? 'S' : '-';
1121 vm_flags
[7] = (kve_flags
& KINFO_VME_FLAG_GROWS_UP
) ? 'U'
1122 : (kve_flags
& KINFO_VME_FLAG_GROWS_DOWN
) ? 'D' : '-';
1128 /* See fbsd-tdep.h. */
1131 fbsd_info_proc_mappings_header (int addr_bit
)
1133 printf_filtered (_("Mapped address spaces:\n\n"));
1136 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1139 " Size", " Offset", "Flags ", "File");
1143 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1146 " Size", " Offset", "Flags ", "File");
1150 /* See fbsd-tdep.h. */
1153 fbsd_info_proc_mappings_entry (int addr_bit
, ULONGEST kve_start
,
1154 ULONGEST kve_end
, ULONGEST kve_offset
,
1155 int kve_flags
, int kve_protection
,
1156 const void *kve_path
)
1160 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1161 hex_string (kve_start
),
1162 hex_string (kve_end
),
1163 hex_string (kve_end
- kve_start
),
1164 hex_string (kve_offset
),
1165 fbsd_vm_map_entry_flags (kve_flags
, kve_protection
),
1166 reinterpret_cast<const char *> (kve_path
));
1170 printf_filtered ("\t%10s %10s %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 /* Implement "info proc mappings" for a corefile. */
1183 fbsd_core_info_proc_mappings (struct gdbarch
*gdbarch
)
1186 unsigned char *descdata
, *descend
;
1189 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.vmmap");
1190 if (section
== NULL
)
1192 warning (_("unable to find mappings in core file"));
1196 note_size
= bfd_section_size (section
);
1198 error (_("malformed core note - too short for header"));
1200 gdb::def_vector
<unsigned char> contents (note_size
);
1201 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1203 error (_("could not get core note contents"));
1205 descdata
= contents
.data ();
1206 descend
= descdata
+ note_size
;
1208 /* Skip over the structure size. */
1211 fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch
));
1212 while (descdata
+ KVE_PATH
< descend
)
1214 ULONGEST structsize
= bfd_get_32 (core_bfd
, descdata
+ KVE_STRUCTSIZE
);
1215 if (structsize
< KVE_PATH
)
1216 error (_("malformed core note - vmmap entry too small"));
1218 ULONGEST start
= bfd_get_64 (core_bfd
, descdata
+ KVE_START
);
1219 ULONGEST end
= bfd_get_64 (core_bfd
, descdata
+ KVE_END
);
1220 ULONGEST offset
= bfd_get_64 (core_bfd
, descdata
+ KVE_OFFSET
);
1221 LONGEST flags
= bfd_get_signed_32 (core_bfd
, descdata
+ KVE_FLAGS
);
1222 LONGEST prot
= bfd_get_signed_32 (core_bfd
, descdata
+ KVE_PROTECTION
);
1223 fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch
), start
, end
,
1224 offset
, flags
, prot
, descdata
+ KVE_PATH
);
1226 descdata
+= structsize
;
1230 /* Fetch the pathname of a vnode for a single file descriptor from the
1231 file table core note. */
1233 static gdb::unique_xmalloc_ptr
<char>
1234 fbsd_core_vnode_path (struct gdbarch
*gdbarch
, int fd
)
1237 unsigned char *descdata
, *descend
;
1240 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.files");
1241 if (section
== NULL
)
1244 note_size
= bfd_section_size (section
);
1246 error (_("malformed core note - too short for header"));
1248 gdb::def_vector
<unsigned char> contents (note_size
);
1249 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1251 error (_("could not get core note contents"));
1253 descdata
= contents
.data ();
1254 descend
= descdata
+ note_size
;
1256 /* Skip over the structure size. */
1259 while (descdata
+ KF_PATH
< descend
)
1261 ULONGEST structsize
;
1263 structsize
= bfd_get_32 (core_bfd
, descdata
+ KF_STRUCTSIZE
);
1264 if (structsize
< KF_PATH
)
1265 error (_("malformed core note - file structure too small"));
1267 if (bfd_get_32 (core_bfd
, descdata
+ KF_TYPE
) == KINFO_FILE_TYPE_VNODE
1268 && bfd_get_signed_32 (core_bfd
, descdata
+ KF_FD
) == fd
)
1270 char *path
= (char *) descdata
+ KF_PATH
;
1271 return make_unique_xstrdup (path
);
1274 descdata
+= structsize
;
1279 /* Helper function to read a struct timeval. */
1282 fbsd_core_fetch_timeval (struct gdbarch
*gdbarch
, unsigned char *data
,
1283 LONGEST
&sec
, ULONGEST
&usec
)
1285 if (gdbarch_addr_bit (gdbarch
) == 64)
1287 sec
= bfd_get_signed_64 (core_bfd
, data
);
1288 usec
= bfd_get_64 (core_bfd
, data
+ 8);
1290 else if (bfd_get_arch (core_bfd
) == bfd_arch_i386
)
1292 sec
= bfd_get_signed_32 (core_bfd
, data
);
1293 usec
= bfd_get_32 (core_bfd
, data
+ 4);
1297 sec
= bfd_get_signed_64 (core_bfd
, data
);
1298 usec
= bfd_get_32 (core_bfd
, data
+ 8);
1302 /* Print out the contents of a signal set. */
1305 fbsd_print_sigset (const char *descr
, unsigned char *sigset
)
1307 printf_filtered ("%s: ", descr
);
1308 for (int i
= 0; i
< SIG_WORDS
; i
++)
1309 printf_filtered ("%08x ",
1310 (unsigned int) bfd_get_32 (core_bfd
, sigset
+ i
* 4));
1311 printf_filtered ("\n");
1314 /* Implement "info proc status" for a corefile. */
1317 fbsd_core_info_proc_status (struct gdbarch
*gdbarch
)
1319 const struct kinfo_proc_layout
*kp
;
1321 unsigned char *descdata
;
1322 int addr_bit
, long_bit
;
1327 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.proc");
1328 if (section
== NULL
)
1330 warning (_("unable to find process info in core file"));
1334 addr_bit
= gdbarch_addr_bit (gdbarch
);
1336 kp
= &kinfo_proc_layout_64
;
1337 else if (bfd_get_arch (core_bfd
) == bfd_arch_i386
)
1338 kp
= &kinfo_proc_layout_i386
;
1340 kp
= &kinfo_proc_layout_32
;
1341 long_bit
= gdbarch_long_bit (gdbarch
);
1344 * Ensure that the note is large enough for all of the fields fetched
1345 * by this function. In particular, the note must contain the 32-bit
1346 * structure size, then it must be long enough to access the last
1347 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1349 note_size
= bfd_section_size (section
);
1350 if (note_size
< (4 + kp
->ki_rusage_ch
+ kp
->ru_majflt
1351 + long_bit
/ TARGET_CHAR_BIT
))
1352 error (_("malformed core note - too short"));
1354 gdb::def_vector
<unsigned char> contents (note_size
);
1355 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1357 error (_("could not get core note contents"));
1359 descdata
= contents
.data ();
1361 /* Skip over the structure size. */
1364 /* Verify 'ki_layout' is 0. */
1365 if (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_layout
) != 0)
1367 warning (_("unsupported process information in core file"));
1371 printf_filtered ("Name: %.19s\n", descdata
+ kp
->ki_comm
);
1372 printf_filtered ("Process ID: %s\n",
1373 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_pid
)));
1374 printf_filtered ("Parent process: %s\n",
1375 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_ppid
)));
1376 printf_filtered ("Process group: %s\n",
1377 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_pgid
)));
1378 printf_filtered ("Session id: %s\n",
1379 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_sid
)));
1381 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1382 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1383 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1384 the structure that is cleared to zero. Assume that a zero value
1385 in ki_tdev indicates a core dump from an older kernel and use the
1386 value in 'ki_tdev_freebsd11' instead. */
1387 value
= bfd_get_64 (core_bfd
, descdata
+ kp
->ki_tdev
);
1389 value
= bfd_get_32 (core_bfd
, descdata
+ kp
->ki_tdev_freebsd11
);
1390 printf_filtered ("TTY: %s\n", pulongest (value
));
1391 printf_filtered ("TTY owner process group: %s\n",
1392 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_tpgid
)));
1393 printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
1394 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_ruid
)),
1395 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_uid
)),
1396 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_svuid
)));
1397 printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
1398 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_rgid
)),
1399 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_groups
)),
1400 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_svgid
)));
1401 printf_filtered ("Groups: ");
1402 uint16_t ngroups
= bfd_get_16 (core_bfd
, descdata
+ kp
->ki_ngroups
);
1403 for (int i
= 0; i
< ngroups
; i
++)
1404 printf_filtered ("%s ",
1405 pulongest (bfd_get_32 (core_bfd
,
1406 descdata
+ kp
->ki_groups
+ i
* 4)));
1407 printf_filtered ("\n");
1408 value
= bfd_get (long_bit
, core_bfd
,
1409 descdata
+ kp
->ki_rusage
+ kp
->ru_minflt
);
1410 printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value
));
1411 value
= bfd_get (long_bit
, core_bfd
,
1412 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_minflt
);
1413 printf_filtered ("Minor faults, children: %s\n", pulongest (value
));
1414 value
= bfd_get (long_bit
, core_bfd
,
1415 descdata
+ kp
->ki_rusage
+ kp
->ru_majflt
);
1416 printf_filtered ("Major faults (memory page faults): %s\n",
1418 value
= bfd_get (long_bit
, core_bfd
,
1419 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_majflt
);
1420 printf_filtered ("Major faults, children: %s\n", pulongest (value
));
1421 fbsd_core_fetch_timeval (gdbarch
,
1422 descdata
+ kp
->ki_rusage
+ kp
->ru_utime
,
1424 printf_filtered ("utime: %s.%06d\n", plongest (sec
), (int) value
);
1425 fbsd_core_fetch_timeval (gdbarch
,
1426 descdata
+ kp
->ki_rusage
+ kp
->ru_stime
,
1428 printf_filtered ("stime: %s.%06d\n", plongest (sec
), (int) value
);
1429 fbsd_core_fetch_timeval (gdbarch
,
1430 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_utime
,
1432 printf_filtered ("utime, children: %s.%06d\n", plongest (sec
), (int) value
);
1433 fbsd_core_fetch_timeval (gdbarch
,
1434 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_stime
,
1436 printf_filtered ("stime, children: %s.%06d\n", plongest (sec
), (int) value
);
1437 printf_filtered ("'nice' value: %d\n",
1438 (int) bfd_get_signed_8 (core_bfd
, descdata
+ kp
->ki_nice
));
1439 fbsd_core_fetch_timeval (gdbarch
, descdata
+ kp
->ki_start
, sec
, value
);
1440 printf_filtered ("Start time: %s.%06d\n", plongest (sec
), (int) value
);
1441 printf_filtered ("Virtual memory size: %s kB\n",
1442 pulongest (bfd_get (addr_bit
, core_bfd
,
1443 descdata
+ kp
->ki_size
) / 1024));
1444 printf_filtered ("Data size: %s pages\n",
1445 pulongest (bfd_get (addr_bit
, core_bfd
,
1446 descdata
+ kp
->ki_dsize
)));
1447 printf_filtered ("Stack size: %s pages\n",
1448 pulongest (bfd_get (addr_bit
, core_bfd
,
1449 descdata
+ kp
->ki_ssize
)));
1450 printf_filtered ("Text size: %s pages\n",
1451 pulongest (bfd_get (addr_bit
, core_bfd
,
1452 descdata
+ kp
->ki_tsize
)));
1453 printf_filtered ("Resident set size: %s pages\n",
1454 pulongest (bfd_get (addr_bit
, core_bfd
,
1455 descdata
+ kp
->ki_rssize
)));
1456 printf_filtered ("Maximum RSS: %s pages\n",
1457 pulongest (bfd_get (long_bit
, core_bfd
,
1458 descdata
+ kp
->ki_rusage
1460 fbsd_print_sigset ("Ignored Signals", descdata
+ kp
->ki_sigignore
);
1461 fbsd_print_sigset ("Caught Signals", descdata
+ kp
->ki_sigcatch
);
1464 /* Implement the "core_info_proc" gdbarch method. */
1467 fbsd_core_info_proc (struct gdbarch
*gdbarch
, const char *args
,
1468 enum info_proc_what what
)
1470 bool do_cmdline
= false;
1471 bool do_cwd
= false;
1472 bool do_exe
= false;
1473 bool do_files
= false;
1474 bool do_mappings
= false;
1475 bool do_status
= false;
1516 pid
= bfd_core_file_pid (core_bfd
);
1518 printf_filtered (_("process %d\n"), pid
);
1522 const char *cmdline
;
1524 cmdline
= bfd_core_file_failing_command (core_bfd
);
1526 printf_filtered ("cmdline = '%s'\n", cmdline
);
1528 warning (_("Command line unavailable"));
1532 gdb::unique_xmalloc_ptr
<char> cwd
=
1533 fbsd_core_vnode_path (gdbarch
, KINFO_FILE_FD_TYPE_CWD
);
1535 printf_filtered ("cwd = '%s'\n", cwd
.get ());
1537 warning (_("unable to read current working directory"));
1541 gdb::unique_xmalloc_ptr
<char> exe
=
1542 fbsd_core_vnode_path (gdbarch
, KINFO_FILE_FD_TYPE_TEXT
);
1544 printf_filtered ("exe = '%s'\n", exe
.get ());
1546 warning (_("unable to read executable path name"));
1549 fbsd_core_info_proc_files (gdbarch
);
1551 fbsd_core_info_proc_mappings (gdbarch
);
1553 fbsd_core_info_proc_status (gdbarch
);
1556 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1559 fbsd_print_auxv_entry (struct gdbarch
*gdbarch
, struct ui_file
*file
,
1560 CORE_ADDR type
, CORE_ADDR val
)
1562 const char *name
= "???";
1563 const char *description
= "";
1564 enum auxv_format format
= AUXV_FORMAT_HEX
;
1583 default_print_auxv_entry (gdbarch
, file
, type
, val
);
1585 #define _TAGNAME(tag) #tag
1586 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1587 #define TAG(tag, text, kind) \
1588 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1589 TAG (EXECPATH
, _("Executable path"), AUXV_FORMAT_STR
);
1590 TAG (CANARY
, _("Canary for SSP"), AUXV_FORMAT_HEX
);
1591 TAG (CANARYLEN
, ("Length of the SSP canary"), AUXV_FORMAT_DEC
);
1592 TAG (OSRELDATE
, _("OSRELDATE"), AUXV_FORMAT_DEC
);
1593 TAG (NCPUS
, _("Number of CPUs"), AUXV_FORMAT_DEC
);
1594 TAG (PAGESIZES
, _("Pagesizes"), AUXV_FORMAT_HEX
);
1595 TAG (PAGESIZESLEN
, _("Number of pagesizes"), AUXV_FORMAT_DEC
);
1596 TAG (TIMEKEEP
, _("Pointer to timehands"), AUXV_FORMAT_HEX
);
1597 TAG (STACKPROT
, _("Initial stack protection"), AUXV_FORMAT_HEX
);
1598 TAG (EHDRFLAGS
, _("ELF header e_flags"), AUXV_FORMAT_HEX
);
1599 TAG (HWCAP
, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX
);
1600 TAG (HWCAP2
, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX
);
1601 TAG (BSDFLAGS
, _("ELF BSD flags"), AUXV_FORMAT_HEX
);
1602 TAG (ARGC
, _("Argument count"), AUXV_FORMAT_DEC
);
1603 TAG (ARGV
, _("Argument vector"), AUXV_FORMAT_HEX
);
1604 TAG (ENVC
, _("Environment count"), AUXV_FORMAT_DEC
);
1605 TAG (ENVV
, _("Environment vector"), AUXV_FORMAT_HEX
);
1606 TAG (PS_STRINGS
, _("Pointer to ps_strings"), AUXV_FORMAT_HEX
);
1609 fprint_auxv_entry (file
, name
, description
, format
, type
, val
);
1612 /* Implement the "get_siginfo_type" gdbarch method. */
1614 static struct type
*
1615 fbsd_get_siginfo_type (struct gdbarch
*gdbarch
)
1617 struct fbsd_gdbarch_data
*fbsd_gdbarch_data
;
1618 struct type
*int_type
, *int32_type
, *uint32_type
, *long_type
, *void_ptr_type
;
1619 struct type
*uid_type
, *pid_type
;
1620 struct type
*sigval_type
, *reason_type
;
1621 struct type
*siginfo_type
;
1624 fbsd_gdbarch_data
= get_fbsd_gdbarch_data (gdbarch
);
1625 if (fbsd_gdbarch_data
->siginfo_type
!= NULL
)
1626 return fbsd_gdbarch_data
->siginfo_type
;
1628 int_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
1630 int32_type
= arch_integer_type (gdbarch
, 32, 0, "int32_t");
1631 uint32_type
= arch_integer_type (gdbarch
, 32, 1, "uint32_t");
1632 long_type
= arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
1634 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
1637 sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
1638 sigval_type
->set_name (xstrdup ("sigval"));
1639 append_composite_type_field (sigval_type
, "sival_int", int_type
);
1640 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
1643 pid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
1644 TYPE_LENGTH (int32_type
) * TARGET_CHAR_BIT
, "__pid_t");
1645 TYPE_TARGET_TYPE (pid_type
) = int32_type
;
1646 pid_type
->set_target_is_stub (true);
1649 uid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
1650 TYPE_LENGTH (uint32_type
) * TARGET_CHAR_BIT
,
1652 TYPE_TARGET_TYPE (uid_type
) = uint32_type
;
1653 pid_type
->set_target_is_stub (true);
1656 reason_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
1659 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1660 append_composite_type_field (type
, "si_trapno", int_type
);
1661 append_composite_type_field (reason_type
, "_fault", type
);
1664 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1665 append_composite_type_field (type
, "si_timerid", int_type
);
1666 append_composite_type_field (type
, "si_overrun", int_type
);
1667 append_composite_type_field (reason_type
, "_timer", type
);
1670 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1671 append_composite_type_field (type
, "si_mqd", int_type
);
1672 append_composite_type_field (reason_type
, "_mesgq", type
);
1675 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1676 append_composite_type_field (type
, "si_band", long_type
);
1677 append_composite_type_field (reason_type
, "_poll", type
);
1680 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1681 append_composite_type_field (type
, "__spare1__", long_type
);
1682 append_composite_type_field (type
, "__spare2__",
1683 init_vector_type (int_type
, 7));
1684 append_composite_type_field (reason_type
, "__spare__", type
);
1686 /* struct siginfo */
1687 siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1688 siginfo_type
->set_name (xstrdup ("siginfo"));
1689 append_composite_type_field (siginfo_type
, "si_signo", int_type
);
1690 append_composite_type_field (siginfo_type
, "si_errno", int_type
);
1691 append_composite_type_field (siginfo_type
, "si_code", int_type
);
1692 append_composite_type_field (siginfo_type
, "si_pid", pid_type
);
1693 append_composite_type_field (siginfo_type
, "si_uid", uid_type
);
1694 append_composite_type_field (siginfo_type
, "si_status", int_type
);
1695 append_composite_type_field (siginfo_type
, "si_addr", void_ptr_type
);
1696 append_composite_type_field (siginfo_type
, "si_value", sigval_type
);
1697 append_composite_type_field (siginfo_type
, "_reason", reason_type
);
1699 fbsd_gdbarch_data
->siginfo_type
= siginfo_type
;
1701 return siginfo_type
;
1704 /* Implement the "gdb_signal_from_target" gdbarch method. */
1706 static enum gdb_signal
1707 fbsd_gdb_signal_from_target (struct gdbarch
*gdbarch
, int signal
)
1712 return GDB_SIGNAL_0
;
1714 case FREEBSD_SIGHUP
:
1715 return GDB_SIGNAL_HUP
;
1717 case FREEBSD_SIGINT
:
1718 return GDB_SIGNAL_INT
;
1720 case FREEBSD_SIGQUIT
:
1721 return GDB_SIGNAL_QUIT
;
1723 case FREEBSD_SIGILL
:
1724 return GDB_SIGNAL_ILL
;
1726 case FREEBSD_SIGTRAP
:
1727 return GDB_SIGNAL_TRAP
;
1729 case FREEBSD_SIGABRT
:
1730 return GDB_SIGNAL_ABRT
;
1732 case FREEBSD_SIGEMT
:
1733 return GDB_SIGNAL_EMT
;
1735 case FREEBSD_SIGFPE
:
1736 return GDB_SIGNAL_FPE
;
1738 case FREEBSD_SIGKILL
:
1739 return GDB_SIGNAL_KILL
;
1741 case FREEBSD_SIGBUS
:
1742 return GDB_SIGNAL_BUS
;
1744 case FREEBSD_SIGSEGV
:
1745 return GDB_SIGNAL_SEGV
;
1747 case FREEBSD_SIGSYS
:
1748 return GDB_SIGNAL_SYS
;
1750 case FREEBSD_SIGPIPE
:
1751 return GDB_SIGNAL_PIPE
;
1753 case FREEBSD_SIGALRM
:
1754 return GDB_SIGNAL_ALRM
;
1756 case FREEBSD_SIGTERM
:
1757 return GDB_SIGNAL_TERM
;
1759 case FREEBSD_SIGURG
:
1760 return GDB_SIGNAL_URG
;
1762 case FREEBSD_SIGSTOP
:
1763 return GDB_SIGNAL_STOP
;
1765 case FREEBSD_SIGTSTP
:
1766 return GDB_SIGNAL_TSTP
;
1768 case FREEBSD_SIGCONT
:
1769 return GDB_SIGNAL_CONT
;
1771 case FREEBSD_SIGCHLD
:
1772 return GDB_SIGNAL_CHLD
;
1774 case FREEBSD_SIGTTIN
:
1775 return GDB_SIGNAL_TTIN
;
1777 case FREEBSD_SIGTTOU
:
1778 return GDB_SIGNAL_TTOU
;
1781 return GDB_SIGNAL_IO
;
1783 case FREEBSD_SIGXCPU
:
1784 return GDB_SIGNAL_XCPU
;
1786 case FREEBSD_SIGXFSZ
:
1787 return GDB_SIGNAL_XFSZ
;
1789 case FREEBSD_SIGVTALRM
:
1790 return GDB_SIGNAL_VTALRM
;
1792 case FREEBSD_SIGPROF
:
1793 return GDB_SIGNAL_PROF
;
1795 case FREEBSD_SIGWINCH
:
1796 return GDB_SIGNAL_WINCH
;
1798 case FREEBSD_SIGINFO
:
1799 return GDB_SIGNAL_INFO
;
1801 case FREEBSD_SIGUSR1
:
1802 return GDB_SIGNAL_USR1
;
1804 case FREEBSD_SIGUSR2
:
1805 return GDB_SIGNAL_USR2
;
1807 /* SIGTHR is the same as SIGLWP on FreeBSD. */
1808 case FREEBSD_SIGTHR
:
1809 return GDB_SIGNAL_LWP
;
1811 case FREEBSD_SIGLIBRT
:
1812 return GDB_SIGNAL_LIBRT
;
1815 if (signal
>= FREEBSD_SIGRTMIN
&& signal
<= FREEBSD_SIGRTMAX
)
1817 int offset
= signal
- FREEBSD_SIGRTMIN
;
1819 return (enum gdb_signal
) ((int) GDB_SIGNAL_REALTIME_65
+ offset
);
1822 return GDB_SIGNAL_UNKNOWN
;
1825 /* Implement the "gdb_signal_to_target" gdbarch method. */
1828 fbsd_gdb_signal_to_target (struct gdbarch
*gdbarch
,
1829 enum gdb_signal signal
)
1836 case GDB_SIGNAL_HUP
:
1837 return FREEBSD_SIGHUP
;
1839 case GDB_SIGNAL_INT
:
1840 return FREEBSD_SIGINT
;
1842 case GDB_SIGNAL_QUIT
:
1843 return FREEBSD_SIGQUIT
;
1845 case GDB_SIGNAL_ILL
:
1846 return FREEBSD_SIGILL
;
1848 case GDB_SIGNAL_TRAP
:
1849 return FREEBSD_SIGTRAP
;
1851 case GDB_SIGNAL_ABRT
:
1852 return FREEBSD_SIGABRT
;
1854 case GDB_SIGNAL_EMT
:
1855 return FREEBSD_SIGEMT
;
1857 case GDB_SIGNAL_FPE
:
1858 return FREEBSD_SIGFPE
;
1860 case GDB_SIGNAL_KILL
:
1861 return FREEBSD_SIGKILL
;
1863 case GDB_SIGNAL_BUS
:
1864 return FREEBSD_SIGBUS
;
1866 case GDB_SIGNAL_SEGV
:
1867 return FREEBSD_SIGSEGV
;
1869 case GDB_SIGNAL_SYS
:
1870 return FREEBSD_SIGSYS
;
1872 case GDB_SIGNAL_PIPE
:
1873 return FREEBSD_SIGPIPE
;
1875 case GDB_SIGNAL_ALRM
:
1876 return FREEBSD_SIGALRM
;
1878 case GDB_SIGNAL_TERM
:
1879 return FREEBSD_SIGTERM
;
1881 case GDB_SIGNAL_URG
:
1882 return FREEBSD_SIGURG
;
1884 case GDB_SIGNAL_STOP
:
1885 return FREEBSD_SIGSTOP
;
1887 case GDB_SIGNAL_TSTP
:
1888 return FREEBSD_SIGTSTP
;
1890 case GDB_SIGNAL_CONT
:
1891 return FREEBSD_SIGCONT
;
1893 case GDB_SIGNAL_CHLD
:
1894 return FREEBSD_SIGCHLD
;
1896 case GDB_SIGNAL_TTIN
:
1897 return FREEBSD_SIGTTIN
;
1899 case GDB_SIGNAL_TTOU
:
1900 return FREEBSD_SIGTTOU
;
1903 return FREEBSD_SIGIO
;
1905 case GDB_SIGNAL_XCPU
:
1906 return FREEBSD_SIGXCPU
;
1908 case GDB_SIGNAL_XFSZ
:
1909 return FREEBSD_SIGXFSZ
;
1911 case GDB_SIGNAL_VTALRM
:
1912 return FREEBSD_SIGVTALRM
;
1914 case GDB_SIGNAL_PROF
:
1915 return FREEBSD_SIGPROF
;
1917 case GDB_SIGNAL_WINCH
:
1918 return FREEBSD_SIGWINCH
;
1920 case GDB_SIGNAL_INFO
:
1921 return FREEBSD_SIGINFO
;
1923 case GDB_SIGNAL_USR1
:
1924 return FREEBSD_SIGUSR1
;
1926 case GDB_SIGNAL_USR2
:
1927 return FREEBSD_SIGUSR2
;
1929 case GDB_SIGNAL_LWP
:
1930 return FREEBSD_SIGTHR
;
1932 case GDB_SIGNAL_LIBRT
:
1933 return FREEBSD_SIGLIBRT
;
1936 if (signal
>= GDB_SIGNAL_REALTIME_65
1937 && signal
<= GDB_SIGNAL_REALTIME_126
)
1939 int offset
= signal
- GDB_SIGNAL_REALTIME_65
;
1941 return FREEBSD_SIGRTMIN
+ offset
;
1947 /* Implement the "get_syscall_number" gdbarch method. */
1950 fbsd_get_syscall_number (struct gdbarch
*gdbarch
, thread_info
*thread
)
1953 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1954 native targets fetch the system call number from the
1955 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1956 However, system call catching requires this function to be
1959 internal_error (__FILE__
, __LINE__
, _("fbsd_get_sycall_number called"));
1962 /* Read an integer symbol value from the current target. */
1965 fbsd_read_integer_by_name (struct gdbarch
*gdbarch
, const char *name
)
1967 bound_minimal_symbol ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
1968 if (ms
.minsym
== NULL
)
1969 error (_("Unable to resolve symbol '%s'"), name
);
1972 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms
), buf
, sizeof buf
) != 0)
1973 error (_("Unable to read value of '%s'"), name
);
1975 return extract_signed_integer (buf
, sizeof buf
, gdbarch_byte_order (gdbarch
));
1978 /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
1979 structure needed to determine the TLS index of an object file. */
1982 fbsd_fetch_rtld_offsets (struct gdbarch
*gdbarch
, struct fbsd_pspace_data
*data
)
1986 /* Fetch offsets from debug symbols in rtld. */
1987 struct symbol
*obj_entry_sym
1988 = lookup_symbol_in_language ("Struct_Obj_Entry", NULL
, STRUCT_DOMAIN
,
1989 language_c
, NULL
).symbol
;
1990 if (obj_entry_sym
== NULL
)
1991 error (_("Unable to find Struct_Obj_Entry symbol"));
1992 data
->off_linkmap
= lookup_struct_elt (SYMBOL_TYPE (obj_entry_sym
),
1993 "linkmap", 0).offset
/ 8;
1994 data
->off_tlsindex
= lookup_struct_elt (SYMBOL_TYPE (obj_entry_sym
),
1995 "tlsindex", 0).offset
/ 8;
1996 data
->rtld_offsets_valid
= true;
1999 catch (const gdb_exception_error
&e
)
2001 data
->off_linkmap
= -1;
2006 /* Fetch offsets from global variables in libthr. Note that
2007 this does not work for single-threaded processes that are not
2008 linked against libthr. */
2009 data
->off_linkmap
= fbsd_read_integer_by_name (gdbarch
,
2010 "_thread_off_linkmap");
2011 data
->off_tlsindex
= fbsd_read_integer_by_name (gdbarch
,
2012 "_thread_off_tlsindex");
2013 data
->rtld_offsets_valid
= true;
2016 catch (const gdb_exception_error
&e
)
2018 data
->off_linkmap
= -1;
2022 /* Helper function to read the TLS index of an object file associated
2023 with a link map entry at LM_ADDR. */
2026 fbsd_get_tls_index (struct gdbarch
*gdbarch
, CORE_ADDR lm_addr
)
2028 struct fbsd_pspace_data
*data
= get_fbsd_pspace_data (current_program_space
);
2030 if (!data
->rtld_offsets_valid
)
2031 fbsd_fetch_rtld_offsets (gdbarch
, data
);
2033 if (data
->off_linkmap
== -1)
2034 throw_error (TLS_GENERIC_ERROR
,
2035 _("Cannot fetch runtime linker structure offsets"));
2037 /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
2038 pointer and then compute the offset of the tlsindex member. */
2039 CORE_ADDR tlsindex_addr
= lm_addr
- data
->off_linkmap
+ data
->off_tlsindex
;
2042 if (target_read_memory (tlsindex_addr
, buf
, sizeof buf
) != 0)
2043 throw_error (TLS_GENERIC_ERROR
,
2044 _("Cannot find thread-local variables on this target"));
2046 return extract_signed_integer (buf
, sizeof buf
, gdbarch_byte_order (gdbarch
));
2049 /* See fbsd-tdep.h. */
2052 fbsd_get_thread_local_address (struct gdbarch
*gdbarch
, CORE_ADDR dtv_addr
,
2053 CORE_ADDR lm_addr
, CORE_ADDR offset
)
2055 LONGEST tls_index
= fbsd_get_tls_index (gdbarch
, lm_addr
);
2057 gdb_byte buf
[gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
];
2058 if (target_read_memory (dtv_addr
, buf
, sizeof buf
) != 0)
2059 throw_error (TLS_GENERIC_ERROR
,
2060 _("Cannot find thread-local variables on this target"));
2062 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
2063 CORE_ADDR addr
= gdbarch_pointer_to_address (gdbarch
,
2064 builtin
->builtin_data_ptr
, buf
);
2066 addr
+= (tls_index
+ 1) * TYPE_LENGTH (builtin
->builtin_data_ptr
);
2067 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
2068 throw_error (TLS_GENERIC_ERROR
,
2069 _("Cannot find thread-local variables on this target"));
2071 addr
= gdbarch_pointer_to_address (gdbarch
, builtin
->builtin_data_ptr
, buf
);
2072 return addr
+ offset
;
2075 /* See fbsd-tdep.h. */
2078 fbsd_skip_solib_resolver (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
2080 struct bound_minimal_symbol msym
= lookup_bound_minimal_symbol ("_rtld_bind");
2081 if (msym
.minsym
!= nullptr && BMSYMBOL_VALUE_ADDRESS (msym
) == pc
)
2082 return frame_unwind_caller_pc (get_current_frame ());
2087 /* To be called from GDB_OSABI_FREEBSD handlers. */
2090 fbsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
2092 set_gdbarch_core_pid_to_str (gdbarch
, fbsd_core_pid_to_str
);
2093 set_gdbarch_core_thread_name (gdbarch
, fbsd_core_thread_name
);
2094 set_gdbarch_core_xfer_siginfo (gdbarch
, fbsd_core_xfer_siginfo
);
2095 set_gdbarch_make_corefile_notes (gdbarch
, fbsd_make_corefile_notes
);
2096 set_gdbarch_core_info_proc (gdbarch
, fbsd_core_info_proc
);
2097 set_gdbarch_print_auxv_entry (gdbarch
, fbsd_print_auxv_entry
);
2098 set_gdbarch_get_siginfo_type (gdbarch
, fbsd_get_siginfo_type
);
2099 set_gdbarch_gdb_signal_from_target (gdbarch
, fbsd_gdb_signal_from_target
);
2100 set_gdbarch_gdb_signal_to_target (gdbarch
, fbsd_gdb_signal_to_target
);
2101 set_gdbarch_skip_solib_resolver (gdbarch
, fbsd_skip_solib_resolver
);
2103 /* `catch syscall' */
2104 set_xml_syscall_file_name (gdbarch
, "syscalls/freebsd.xml");
2105 set_gdbarch_get_syscall_number (gdbarch
, fbsd_get_syscall_number
);
2108 void _initialize_fbsd_tdep ();
2110 _initialize_fbsd_tdep ()
2112 fbsd_gdbarch_data_handle
=
2113 gdbarch_data_register_post_init (init_fbsd_gdbarch_data
);