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/>. */
26 #include "gdbthread.h"
28 #include "xml-syscall.h"
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
33 #include "fbsd-tdep.h"
35 /* This enum is derived from FreeBSD's <sys/signal.h>. */
64 FREEBSD_SIGVTALRM
= 26,
66 FREEBSD_SIGWINCH
= 28,
71 FREEBSD_SIGLIBRT
= 33,
72 FREEBSD_SIGRTMIN
= 65,
73 FREEBSD_SIGRTMAX
= 126,
76 /* FreeBSD kernels 12.0 and later include a copy of the
77 'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
78 operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
79 The constants below define the offset of field members and flags in
80 this structure used by methods in this file. Note that the
81 'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
82 containing the size of the structure. */
84 #define LWPINFO_OFFSET 0x4
86 /* Offsets in ptrace_lwpinfo. */
87 #define LWPINFO_PL_FLAGS 0x8
88 #define LWPINFO64_PL_SIGINFO 0x30
89 #define LWPINFO32_PL_SIGINFO 0x2c
91 /* Flags in pl_flags. */
92 #define PL_FLAG_SI 0x20 /* siginfo is valid */
94 /* Sizes of siginfo_t. */
95 #define SIZE64_SIGINFO_T 80
96 #define SIZE32_SIGINFO_T 64
98 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
99 dump notes. See <sys/user.h> for the definition of struct
100 kinfo_vmentry. This data structure should have the same layout on
103 Note that FreeBSD 7.0 used an older version of this structure
104 (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
105 dump note wasn't introduced until FreeBSD 9.2. As a result, the
106 core dump note has always used the 7.1 and later structure
109 #define KVE_STRUCTSIZE 0x0
110 #define KVE_START 0x8
112 #define KVE_OFFSET 0x18
113 #define KVE_FLAGS 0x2c
114 #define KVE_PROTECTION 0x38
115 #define KVE_PATH 0x88
117 /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
118 match the KVME_PROT_* constants in <sys/user.h>. */
120 #define KINFO_VME_PROT_READ 0x00000001
121 #define KINFO_VME_PROT_WRITE 0x00000002
122 #define KINFO_VME_PROT_EXEC 0x00000004
124 /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
125 match the KVME_FLAG_* constants in <sys/user.h>. */
127 #define KINFO_VME_FLAG_COW 0x00000001
128 #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
129 #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
130 #define KINFO_VME_FLAG_SUPER 0x00000008
131 #define KINFO_VME_FLAG_GROWS_UP 0x00000010
132 #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
134 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
135 dump notes. See <sys/user.h> for the definition of struct
136 kinfo_file. This data structure should have the same layout on all
139 Note that FreeBSD 7.0 used an older version of this structure
140 (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
141 note wasn't introduced until FreeBSD 9.2. As a result, the core
142 dump note has always used the 7.1 and later structure format. */
144 #define KF_STRUCTSIZE 0x0
147 #define KF_FLAGS 0x10
148 #define KF_OFFSET 0x18
149 #define KF_VNODE_TYPE 0x20
150 #define KF_SOCK_DOMAIN 0x24
151 #define KF_SOCK_TYPE 0x28
152 #define KF_SOCK_PROTOCOL 0x2c
153 #define KF_SA_LOCAL 0x30
154 #define KF_SA_PEER 0xb0
155 #define KF_PATH 0x170
157 /* Constants for the 'kf_type' field in struct kinfo_file. These
158 match the KF_TYPE_* constants in <sys/user.h>. */
160 #define KINFO_FILE_TYPE_VNODE 1
161 #define KINFO_FILE_TYPE_SOCKET 2
162 #define KINFO_FILE_TYPE_PIPE 3
163 #define KINFO_FILE_TYPE_FIFO 4
164 #define KINFO_FILE_TYPE_KQUEUE 5
165 #define KINFO_FILE_TYPE_CRYPTO 6
166 #define KINFO_FILE_TYPE_MQUEUE 7
167 #define KINFO_FILE_TYPE_SHM 8
168 #define KINFO_FILE_TYPE_SEM 9
169 #define KINFO_FILE_TYPE_PTS 10
170 #define KINFO_FILE_TYPE_PROCDESC 11
172 /* Special values for the 'kf_fd' field in struct kinfo_file. These
173 match the KF_FD_TYPE_* constants in <sys/user.h>. */
175 #define KINFO_FILE_FD_TYPE_CWD -1
176 #define KINFO_FILE_FD_TYPE_ROOT -2
177 #define KINFO_FILE_FD_TYPE_JAIL -3
178 #define KINFO_FILE_FD_TYPE_TRACE -4
179 #define KINFO_FILE_FD_TYPE_TEXT -5
180 #define KINFO_FILE_FD_TYPE_CTTY -6
182 /* Flags in the 'kf_flags' field in struct kinfo_file. These match
183 the KF_FLAG_* constants in <sys/user.h>. */
185 #define KINFO_FILE_FLAG_READ 0x00000001
186 #define KINFO_FILE_FLAG_WRITE 0x00000002
187 #define KINFO_FILE_FLAG_APPEND 0x00000004
188 #define KINFO_FILE_FLAG_ASYNC 0x00000008
189 #define KINFO_FILE_FLAG_FSYNC 0x00000010
190 #define KINFO_FILE_FLAG_NONBLOCK 0x00000020
191 #define KINFO_FILE_FLAG_DIRECT 0x00000040
192 #define KINFO_FILE_FLAG_HASLOCK 0x00000080
193 #define KINFO_FILE_FLAG_EXEC 0x00004000
195 /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
196 These match the KF_VTYPE_* constants in <sys/user.h>. */
198 #define KINFO_FILE_VTYPE_VREG 1
199 #define KINFO_FILE_VTYPE_VDIR 2
200 #define KINFO_FILE_VTYPE_VCHR 4
201 #define KINFO_FILE_VTYPE_VLNK 5
202 #define KINFO_FILE_VTYPE_VSOCK 6
203 #define KINFO_FILE_VTYPE_VFIFO 7
205 /* Constants for socket address families. These match AF_* constants
206 in <sys/socket.h>. */
208 #define FBSD_AF_UNIX 1
209 #define FBSD_AF_INET 2
210 #define FBSD_AF_INET6 28
212 /* Constants for socket types. These match SOCK_* constants in
215 #define FBSD_SOCK_STREAM 1
216 #define FBSD_SOCK_DGRAM 2
217 #define FBSD_SOCK_SEQPACKET 5
219 /* Constants for IP protocols. These match IPPROTO_* constants in
222 #define FBSD_IPPROTO_ICMP 1
223 #define FBSD_IPPROTO_TCP 6
224 #define FBSD_IPPROTO_UDP 17
225 #define FBSD_IPPROTO_SCTP 132
227 /* Socket address structures. These have the same layout on all
228 FreeBSD architectures. In addition, multibyte fields such as IP
229 addresses are always stored in network byte order. */
231 struct fbsd_sockaddr_in
240 struct fbsd_sockaddr_in6
244 uint8_t sin6_port
[2];
245 uint32_t sin6_flowinfo
;
246 uint8_t sin6_addr
[16];
247 uint32_t sin6_scope_id
;
250 struct fbsd_sockaddr_un
257 /* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
258 <sys/_sigset.h> and is the same value on all architectures. */
262 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
263 dump notes. See <sys/user.h> for the definition of struct
264 kinfo_proc. This data structure has different layouts on different
265 architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
266 uses a 32-bit time_t while all other architectures use a 64-bit
269 The core dump note actually contains one kinfo_proc structure for
270 each thread, but all of the process-wide data can be obtained from
271 the first structure. One result of this note's format is that some
272 of the process-wide status available in the native target method
273 from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
274 is not available from a core dump. Instead, the per-thread data
275 structures contain the value of these fields for individual
278 struct kinfo_proc_layout
280 /* Offsets of struct kinfo_proc members. */
287 int ki_tdev_freebsd11
;
309 /* Offsets of struct rusage members. */
317 const struct kinfo_proc_layout kinfo_proc_layout_32
=
325 .ki_tdev_freebsd11
= 0x44,
326 .ki_sigignore
= 0x68,
345 .ki_rusage_ch
= 0x278,
354 const struct kinfo_proc_layout kinfo_proc_layout_i386
=
362 .ki_tdev_freebsd11
= 0x44,
363 .ki_sigignore
= 0x68,
382 .ki_rusage_ch
= 0x258,
391 const struct kinfo_proc_layout kinfo_proc_layout_64
=
399 .ki_tdev_freebsd11
= 0x64,
400 .ki_sigignore
= 0x88,
419 .ki_rusage_ch
= 0x2f0,
428 static struct gdbarch_data
*fbsd_gdbarch_data_handle
;
430 struct fbsd_gdbarch_data
432 struct type
*siginfo_type
;
436 init_fbsd_gdbarch_data (struct gdbarch
*gdbarch
)
438 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct fbsd_gdbarch_data
);
441 static struct fbsd_gdbarch_data
*
442 get_fbsd_gdbarch_data (struct gdbarch
*gdbarch
)
444 return ((struct fbsd_gdbarch_data
*)
445 gdbarch_data (gdbarch
, fbsd_gdbarch_data_handle
));
448 struct fbsd_pspace_data
450 /* Offsets in the runtime linker's 'Obj_Entry' structure. */
451 LONGEST off_linkmap
= 0;
452 LONGEST off_tlsindex
= 0;
453 bool rtld_offsets_valid
= false;
456 /* Per-program-space data for FreeBSD architectures. */
457 static const struct program_space_key
<fbsd_pspace_data
>
458 fbsd_pspace_data_handle
;
460 static struct fbsd_pspace_data
*
461 get_fbsd_pspace_data (struct program_space
*pspace
)
463 struct fbsd_pspace_data
*data
;
465 data
= fbsd_pspace_data_handle
.get (pspace
);
467 data
= fbsd_pspace_data_handle
.emplace (pspace
);
472 /* This is how we want PTIDs from core files to be printed. */
475 fbsd_core_pid_to_str (struct gdbarch
*gdbarch
, ptid_t ptid
)
477 if (ptid
.lwp () != 0)
478 return string_printf ("LWP %ld", ptid
.lwp ());
480 return normal_pid_to_str (ptid
);
483 /* Extract the name assigned to a thread from a core. Returns the
484 string in a static buffer. */
487 fbsd_core_thread_name (struct gdbarch
*gdbarch
, struct thread_info
*thr
)
490 struct bfd_section
*section
;
493 if (thr
->ptid
.lwp () != 0)
495 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
496 whose contents are defined by a "struct thrmisc" declared in
497 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
498 a null-terminated string as the first member of the
499 structure. Rather than define the full structure here, just
500 extract the null-terminated name from the start of the
502 thread_section_name
section_name (".thrmisc", thr
->ptid
);
504 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
505 if (section
!= NULL
&& bfd_section_size (section
) > 0)
507 /* Truncate the name if it is longer than "buf". */
508 size
= bfd_section_size (section
);
509 if (size
> sizeof buf
- 1)
510 size
= sizeof buf
- 1;
511 if (bfd_get_section_contents (core_bfd
, section
, buf
, (file_ptr
) 0,
517 /* Note that each thread will report the process command
518 as its thread name instead of an empty name if a name
519 has not been set explicitly. Return a NULL name in
521 if (strcmp (buf
, elf_tdata (core_bfd
)->core
->program
) != 0)
530 /* Implement the "core_xfer_siginfo" gdbarch method. */
533 fbsd_core_xfer_siginfo (struct gdbarch
*gdbarch
, gdb_byte
*readbuf
,
534 ULONGEST offset
, ULONGEST len
)
538 if (gdbarch_long_bit (gdbarch
) == 32)
539 siginfo_size
= SIZE32_SIGINFO_T
;
541 siginfo_size
= SIZE64_SIGINFO_T
;
542 if (offset
> siginfo_size
)
545 thread_section_name
section_name (".note.freebsdcore.lwpinfo", inferior_ptid
);
546 asection
*section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
551 if (!bfd_get_section_contents (core_bfd
, section
, buf
,
552 LWPINFO_OFFSET
+ LWPINFO_PL_FLAGS
, 4))
555 int pl_flags
= extract_signed_integer (buf
, 4, gdbarch_byte_order (gdbarch
));
556 if (!(pl_flags
& PL_FLAG_SI
))
559 if (offset
+ len
> siginfo_size
)
560 len
= siginfo_size
- offset
;
562 ULONGEST siginfo_offset
;
563 if (gdbarch_long_bit (gdbarch
) == 32)
564 siginfo_offset
= LWPINFO_OFFSET
+ LWPINFO32_PL_SIGINFO
;
566 siginfo_offset
= LWPINFO_OFFSET
+ LWPINFO64_PL_SIGINFO
;
568 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
569 siginfo_offset
+ offset
, len
))
576 find_signalled_thread (struct thread_info
*info
, void *data
)
578 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
579 && info
->ptid
.pid () == inferior_ptid
.pid ())
585 /* Structure for passing information from
586 fbsd_collect_thread_registers via an iterator to
587 fbsd_collect_regset_section_cb. */
589 struct fbsd_collect_regset_section_cb_data
591 const struct regcache
*regcache
;
596 enum gdb_signal stop_signal
;
601 fbsd_collect_regset_section_cb (const char *sect_name
, int supply_size
,
602 int collect_size
, const struct regset
*regset
,
603 const char *human_name
, void *cb_data
)
606 struct fbsd_collect_regset_section_cb_data
*data
607 = (struct fbsd_collect_regset_section_cb_data
*) cb_data
;
609 if (data
->abort_iteration
)
612 gdb_assert (regset
->collect_regset
);
614 buf
= (char *) xmalloc (collect_size
);
615 regset
->collect_regset (regset
, data
->regcache
, -1, buf
, collect_size
);
617 /* PRSTATUS still needs to be treated specially. */
618 if (strcmp (sect_name
, ".reg") == 0)
619 data
->note_data
= (char *) elfcore_write_prstatus
620 (data
->obfd
, data
->note_data
, data
->note_size
, data
->lwp
,
621 gdb_signal_to_host (data
->stop_signal
), buf
);
623 data
->note_data
= (char *) elfcore_write_register_note
624 (data
->obfd
, data
->note_data
, data
->note_size
,
625 sect_name
, buf
, collect_size
);
628 if (data
->note_data
== NULL
)
629 data
->abort_iteration
= 1;
632 /* Records the thread's register state for the corefile note
636 fbsd_collect_thread_registers (const struct regcache
*regcache
,
637 ptid_t ptid
, bfd
*obfd
,
638 char *note_data
, int *note_size
,
639 enum gdb_signal stop_signal
)
641 struct gdbarch
*gdbarch
= regcache
->arch ();
642 struct fbsd_collect_regset_section_cb_data data
;
644 data
.regcache
= regcache
;
646 data
.note_data
= note_data
;
647 data
.note_size
= note_size
;
648 data
.stop_signal
= stop_signal
;
649 data
.abort_iteration
= 0;
650 data
.lwp
= ptid
.lwp ();
652 gdbarch_iterate_over_regset_sections (gdbarch
,
653 fbsd_collect_regset_section_cb
,
655 return data
.note_data
;
658 struct fbsd_corefile_thread_data
660 struct gdbarch
*gdbarch
;
664 enum gdb_signal stop_signal
;
667 /* Records the thread's register state for the corefile note
671 fbsd_corefile_thread (struct thread_info
*info
,
672 struct fbsd_corefile_thread_data
*args
)
674 struct regcache
*regcache
;
676 regcache
= get_thread_arch_regcache (info
->inf
->process_target (),
677 info
->ptid
, args
->gdbarch
);
679 target_fetch_registers (regcache
, -1);
681 args
->note_data
= fbsd_collect_thread_registers
682 (regcache
, info
->ptid
, args
->obfd
, args
->note_data
,
683 args
->note_size
, args
->stop_signal
);
686 /* Return a byte_vector containing the contents of a core dump note
687 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
688 the data is prefixed with a 32-bit integer size to match the format
689 used in FreeBSD NT_PROCSTAT_* notes. */
691 static gdb::optional
<gdb::byte_vector
>
692 fbsd_make_note_desc (enum target_object object
, uint32_t structsize
)
694 gdb::optional
<gdb::byte_vector
> buf
=
695 target_read_alloc (current_top_target (), object
, NULL
);
696 if (!buf
|| buf
->empty ())
702 gdb::byte_vector
desc (sizeof (structsize
) + buf
->size ());
703 memcpy (desc
.data (), &structsize
, sizeof (structsize
));
704 memcpy (desc
.data () + sizeof (structsize
), buf
->data (), buf
->size ());
708 /* Create appropriate note sections for a corefile, returning them in
712 fbsd_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
714 struct fbsd_corefile_thread_data thread_args
;
715 char *note_data
= NULL
;
716 Elf_Internal_Ehdr
*i_ehdrp
;
717 struct thread_info
*curr_thr
, *signalled_thr
;
719 /* Put a "FreeBSD" label in the ELF header. */
720 i_ehdrp
= elf_elfheader (obfd
);
721 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
723 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch
));
725 if (get_exec_file (0))
727 const char *fname
= lbasename (get_exec_file (0));
728 char *psargs
= xstrdup (fname
);
730 if (get_inferior_args ())
731 psargs
= reconcat (psargs
, psargs
, " ", get_inferior_args (),
734 note_data
= elfcore_write_prpsinfo (obfd
, note_data
, note_size
,
738 /* Thread register information. */
741 update_thread_list ();
743 catch (const gdb_exception_error
&e
)
745 exception_print (gdb_stderr
, e
);
748 /* Like the kernel, prefer dumping the signalled thread first.
749 "First thread" is what tools use to infer the signalled thread.
750 In case there's more than one signalled thread, prefer the
751 current thread, if it is signalled. */
752 curr_thr
= inferior_thread ();
753 if (curr_thr
->suspend
.stop_signal
!= GDB_SIGNAL_0
)
754 signalled_thr
= curr_thr
;
757 signalled_thr
= iterate_over_threads (find_signalled_thread
, NULL
);
758 if (signalled_thr
== NULL
)
759 signalled_thr
= curr_thr
;
762 thread_args
.gdbarch
= gdbarch
;
763 thread_args
.obfd
= obfd
;
764 thread_args
.note_data
= note_data
;
765 thread_args
.note_size
= note_size
;
766 thread_args
.stop_signal
= signalled_thr
->suspend
.stop_signal
;
768 fbsd_corefile_thread (signalled_thr
, &thread_args
);
769 for (thread_info
*thr
: current_inferior ()->non_exited_threads ())
771 if (thr
== signalled_thr
)
774 fbsd_corefile_thread (thr
, &thread_args
);
777 note_data
= thread_args
.note_data
;
779 /* Auxiliary vector. */
780 uint32_t structsize
= gdbarch_ptr_bit (gdbarch
) / 4; /* Elf_Auxinfo */
781 gdb::optional
<gdb::byte_vector
> note_desc
=
782 fbsd_make_note_desc (TARGET_OBJECT_AUXV
, structsize
);
783 if (note_desc
&& !note_desc
->empty ())
785 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
786 NT_FREEBSD_PROCSTAT_AUXV
,
787 note_desc
->data (), note_desc
->size ());
792 /* Virtual memory mappings. */
793 note_desc
= fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP
, 0);
794 if (note_desc
&& !note_desc
->empty ())
796 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
797 NT_FREEBSD_PROCSTAT_VMMAP
,
798 note_desc
->data (), note_desc
->size ());
803 note_desc
= fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS
, 0);
804 if (note_desc
&& !note_desc
->empty ())
806 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
807 NT_FREEBSD_PROCSTAT_PSSTRINGS
,
808 note_desc
->data (), note_desc
->size ());
816 /* Helper function to generate the file descriptor description for a
817 single open file in 'info proc files'. */
820 fbsd_file_fd (int kf_fd
)
824 case KINFO_FILE_FD_TYPE_CWD
:
826 case KINFO_FILE_FD_TYPE_ROOT
:
828 case KINFO_FILE_FD_TYPE_JAIL
:
830 case KINFO_FILE_FD_TYPE_TRACE
:
832 case KINFO_FILE_FD_TYPE_TEXT
:
834 case KINFO_FILE_FD_TYPE_CTTY
:
837 return int_string (kf_fd
, 10, 1, 0, 0);
841 /* Helper function to generate the file type for a single open file in
842 'info proc files'. */
845 fbsd_file_type (int kf_type
, int kf_vnode_type
)
849 case KINFO_FILE_TYPE_VNODE
:
850 switch (kf_vnode_type
)
852 case KINFO_FILE_VTYPE_VREG
:
854 case KINFO_FILE_VTYPE_VDIR
:
856 case KINFO_FILE_VTYPE_VCHR
:
858 case KINFO_FILE_VTYPE_VLNK
:
860 case KINFO_FILE_VTYPE_VSOCK
:
862 case KINFO_FILE_VTYPE_VFIFO
:
866 char *str
= get_print_cell ();
868 xsnprintf (str
, PRINT_CELL_SIZE
, "vn:%d", kf_vnode_type
);
872 case KINFO_FILE_TYPE_SOCKET
:
874 case KINFO_FILE_TYPE_PIPE
:
876 case KINFO_FILE_TYPE_FIFO
:
878 case KINFO_FILE_TYPE_KQUEUE
:
880 case KINFO_FILE_TYPE_CRYPTO
:
882 case KINFO_FILE_TYPE_MQUEUE
:
884 case KINFO_FILE_TYPE_SHM
:
886 case KINFO_FILE_TYPE_SEM
:
888 case KINFO_FILE_TYPE_PTS
:
890 case KINFO_FILE_TYPE_PROCDESC
:
893 return int_string (kf_type
, 10, 1, 0, 0);
897 /* Helper function to generate the file flags for a single open file in
898 'info proc files'. */
901 fbsd_file_flags (int kf_flags
)
903 static char file_flags
[10];
905 file_flags
[0] = (kf_flags
& KINFO_FILE_FLAG_READ
) ? 'r' : '-';
906 file_flags
[1] = (kf_flags
& KINFO_FILE_FLAG_WRITE
) ? 'w' : '-';
907 file_flags
[2] = (kf_flags
& KINFO_FILE_FLAG_EXEC
) ? 'x' : '-';
908 file_flags
[3] = (kf_flags
& KINFO_FILE_FLAG_APPEND
) ? 'a' : '-';
909 file_flags
[4] = (kf_flags
& KINFO_FILE_FLAG_ASYNC
) ? 's' : '-';
910 file_flags
[5] = (kf_flags
& KINFO_FILE_FLAG_FSYNC
) ? 'f' : '-';
911 file_flags
[6] = (kf_flags
& KINFO_FILE_FLAG_NONBLOCK
) ? 'n' : '-';
912 file_flags
[7] = (kf_flags
& KINFO_FILE_FLAG_DIRECT
) ? 'd' : '-';
913 file_flags
[8] = (kf_flags
& KINFO_FILE_FLAG_HASLOCK
) ? 'l' : '-';
914 file_flags
[9] = '\0';
919 /* Helper function to generate the name of an IP protocol. */
922 fbsd_ipproto (int protocol
)
926 case FBSD_IPPROTO_ICMP
:
928 case FBSD_IPPROTO_TCP
:
930 case FBSD_IPPROTO_UDP
:
932 case FBSD_IPPROTO_SCTP
:
936 char *str
= get_print_cell ();
938 xsnprintf (str
, PRINT_CELL_SIZE
, "ip<%d>", protocol
);
944 /* Helper function to print out an IPv4 socket address. */
947 fbsd_print_sockaddr_in (const void *sockaddr
)
949 const struct fbsd_sockaddr_in
*sin
=
950 reinterpret_cast<const struct fbsd_sockaddr_in
*> (sockaddr
);
951 char buf
[INET_ADDRSTRLEN
];
953 if (inet_ntop (AF_INET
, sin
->sin_addr
, buf
, sizeof buf
) == nullptr)
954 error (_("Failed to format IPv4 address"));
955 printf_filtered ("%s:%u", buf
,
956 (sin
->sin_port
[0] << 8) | sin
->sin_port
[1]);
959 /* Helper function to print out an IPv6 socket address. */
962 fbsd_print_sockaddr_in6 (const void *sockaddr
)
964 const struct fbsd_sockaddr_in6
*sin6
=
965 reinterpret_cast<const struct fbsd_sockaddr_in6
*> (sockaddr
);
966 char buf
[INET6_ADDRSTRLEN
];
968 if (inet_ntop (AF_INET6
, sin6
->sin6_addr
, buf
, sizeof buf
) == nullptr)
969 error (_("Failed to format IPv6 address"));
970 printf_filtered ("%s.%u", buf
,
971 (sin6
->sin6_port
[0] << 8) | sin6
->sin6_port
[1]);
974 /* See fbsd-tdep.h. */
977 fbsd_info_proc_files_header ()
979 printf_filtered (_("Open files:\n\n"));
980 printf_filtered (" %6s %6s %10s %9s %s\n",
981 "FD", "Type", "Offset", "Flags ", "Name");
984 /* See fbsd-tdep.h. */
987 fbsd_info_proc_files_entry (int kf_type
, int kf_fd
, int kf_flags
,
988 LONGEST kf_offset
, int kf_vnode_type
,
989 int kf_sock_domain
, int kf_sock_type
,
990 int kf_sock_protocol
, const void *kf_sa_local
,
991 const void *kf_sa_peer
, const void *kf_path
)
993 printf_filtered (" %6s %6s %10s %8s ",
994 fbsd_file_fd (kf_fd
),
995 fbsd_file_type (kf_type
, kf_vnode_type
),
996 kf_offset
> -1 ? hex_string (kf_offset
) : "-",
997 fbsd_file_flags (kf_flags
));
998 if (kf_type
== KINFO_FILE_TYPE_SOCKET
)
1000 switch (kf_sock_domain
)
1004 switch (kf_sock_type
)
1006 case FBSD_SOCK_STREAM
:
1007 printf_filtered ("unix stream:");
1009 case FBSD_SOCK_DGRAM
:
1010 printf_filtered ("unix dgram:");
1012 case FBSD_SOCK_SEQPACKET
:
1013 printf_filtered ("unix seqpacket:");
1016 printf_filtered ("unix <%d>:", kf_sock_type
);
1020 /* For local sockets, print out the first non-nul path
1021 rather than both paths. */
1022 const struct fbsd_sockaddr_un
*saddr_un
1023 = reinterpret_cast<const struct fbsd_sockaddr_un
*> (kf_sa_local
);
1024 if (saddr_un
->sun_path
[0] == 0)
1025 saddr_un
= reinterpret_cast<const struct fbsd_sockaddr_un
*>
1027 printf_filtered ("%s", saddr_un
->sun_path
);
1031 printf_filtered ("%s4 ", fbsd_ipproto (kf_sock_protocol
));
1032 fbsd_print_sockaddr_in (kf_sa_local
);
1033 printf_filtered (" -> ");
1034 fbsd_print_sockaddr_in (kf_sa_peer
);
1037 printf_filtered ("%s6 ", fbsd_ipproto (kf_sock_protocol
));
1038 fbsd_print_sockaddr_in6 (kf_sa_local
);
1039 printf_filtered (" -> ");
1040 fbsd_print_sockaddr_in6 (kf_sa_peer
);
1045 printf_filtered ("%s", reinterpret_cast<const char *> (kf_path
));
1046 printf_filtered ("\n");
1049 /* Implement "info proc files" for a corefile. */
1052 fbsd_core_info_proc_files (struct gdbarch
*gdbarch
)
1055 = bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.files");
1056 if (section
== NULL
)
1058 warning (_("unable to find open files in core file"));
1062 size_t note_size
= bfd_section_size (section
);
1064 error (_("malformed core note - too short for header"));
1066 gdb::def_vector
<unsigned char> contents (note_size
);
1067 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1069 error (_("could not get core note contents"));
1071 unsigned char *descdata
= contents
.data ();
1072 unsigned char *descend
= descdata
+ note_size
;
1074 /* Skip over the structure size. */
1077 fbsd_info_proc_files_header ();
1079 while (descdata
+ KF_PATH
< descend
)
1081 ULONGEST structsize
= bfd_get_32 (core_bfd
, descdata
+ KF_STRUCTSIZE
);
1082 if (structsize
< KF_PATH
)
1083 error (_("malformed core note - file structure too small"));
1085 LONGEST type
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_TYPE
);
1086 LONGEST fd
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_FD
);
1087 LONGEST flags
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_FLAGS
);
1088 LONGEST offset
= bfd_get_signed_64 (core_bfd
, descdata
+ KF_OFFSET
);
1089 LONGEST vnode_type
= bfd_get_signed_32 (core_bfd
,
1090 descdata
+ KF_VNODE_TYPE
);
1091 LONGEST sock_domain
= bfd_get_signed_32 (core_bfd
,
1092 descdata
+ KF_SOCK_DOMAIN
);
1093 LONGEST sock_type
= bfd_get_signed_32 (core_bfd
, descdata
+ KF_SOCK_TYPE
);
1094 LONGEST sock_protocol
= bfd_get_signed_32 (core_bfd
,
1095 descdata
+ KF_SOCK_PROTOCOL
);
1096 fbsd_info_proc_files_entry (type
, fd
, flags
, offset
, vnode_type
,
1097 sock_domain
, sock_type
, sock_protocol
,
1098 descdata
+ KF_SA_LOCAL
, descdata
+ KF_SA_PEER
,
1099 descdata
+ KF_PATH
);
1101 descdata
+= structsize
;
1105 /* Helper function to generate mappings flags for a single VM map
1106 entry in 'info proc mappings'. */
1109 fbsd_vm_map_entry_flags (int kve_flags
, int kve_protection
)
1111 static char vm_flags
[9];
1113 vm_flags
[0] = (kve_protection
& KINFO_VME_PROT_READ
) ? 'r' : '-';
1114 vm_flags
[1] = (kve_protection
& KINFO_VME_PROT_WRITE
) ? 'w' : '-';
1115 vm_flags
[2] = (kve_protection
& KINFO_VME_PROT_EXEC
) ? 'x' : '-';
1117 vm_flags
[4] = (kve_flags
& KINFO_VME_FLAG_COW
) ? 'C' : '-';
1118 vm_flags
[5] = (kve_flags
& KINFO_VME_FLAG_NEEDS_COPY
) ? 'N' : '-';
1119 vm_flags
[6] = (kve_flags
& KINFO_VME_FLAG_SUPER
) ? 'S' : '-';
1120 vm_flags
[7] = (kve_flags
& KINFO_VME_FLAG_GROWS_UP
) ? 'U'
1121 : (kve_flags
& KINFO_VME_FLAG_GROWS_DOWN
) ? 'D' : '-';
1127 /* See fbsd-tdep.h. */
1130 fbsd_info_proc_mappings_header (int addr_bit
)
1132 printf_filtered (_("Mapped address spaces:\n\n"));
1135 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1138 " Size", " Offset", "Flags ", "File");
1142 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1145 " Size", " Offset", "Flags ", "File");
1149 /* See fbsd-tdep.h. */
1152 fbsd_info_proc_mappings_entry (int addr_bit
, ULONGEST kve_start
,
1153 ULONGEST kve_end
, ULONGEST kve_offset
,
1154 int kve_flags
, int kve_protection
,
1155 const void *kve_path
)
1159 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1160 hex_string (kve_start
),
1161 hex_string (kve_end
),
1162 hex_string (kve_end
- kve_start
),
1163 hex_string (kve_offset
),
1164 fbsd_vm_map_entry_flags (kve_flags
, kve_protection
),
1165 reinterpret_cast<const char *> (kve_path
));
1169 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1170 hex_string (kve_start
),
1171 hex_string (kve_end
),
1172 hex_string (kve_end
- kve_start
),
1173 hex_string (kve_offset
),
1174 fbsd_vm_map_entry_flags (kve_flags
, kve_protection
),
1175 reinterpret_cast<const char *> (kve_path
));
1179 /* Implement "info proc mappings" for a corefile. */
1182 fbsd_core_info_proc_mappings (struct gdbarch
*gdbarch
)
1185 unsigned char *descdata
, *descend
;
1188 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.vmmap");
1189 if (section
== NULL
)
1191 warning (_("unable to find mappings in core file"));
1195 note_size
= bfd_section_size (section
);
1197 error (_("malformed core note - too short for header"));
1199 gdb::def_vector
<unsigned char> contents (note_size
);
1200 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1202 error (_("could not get core note contents"));
1204 descdata
= contents
.data ();
1205 descend
= descdata
+ note_size
;
1207 /* Skip over the structure size. */
1210 fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch
));
1211 while (descdata
+ KVE_PATH
< descend
)
1213 ULONGEST structsize
= bfd_get_32 (core_bfd
, descdata
+ KVE_STRUCTSIZE
);
1214 if (structsize
< KVE_PATH
)
1215 error (_("malformed core note - vmmap entry too small"));
1217 ULONGEST start
= bfd_get_64 (core_bfd
, descdata
+ KVE_START
);
1218 ULONGEST end
= bfd_get_64 (core_bfd
, descdata
+ KVE_END
);
1219 ULONGEST offset
= bfd_get_64 (core_bfd
, descdata
+ KVE_OFFSET
);
1220 LONGEST flags
= bfd_get_signed_32 (core_bfd
, descdata
+ KVE_FLAGS
);
1221 LONGEST prot
= bfd_get_signed_32 (core_bfd
, descdata
+ KVE_PROTECTION
);
1222 fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch
), start
, end
,
1223 offset
, flags
, prot
, descdata
+ KVE_PATH
);
1225 descdata
+= structsize
;
1229 /* Fetch the pathname of a vnode for a single file descriptor from the
1230 file table core note. */
1232 static gdb::unique_xmalloc_ptr
<char>
1233 fbsd_core_vnode_path (struct gdbarch
*gdbarch
, int fd
)
1236 unsigned char *descdata
, *descend
;
1239 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.files");
1240 if (section
== NULL
)
1243 note_size
= bfd_section_size (section
);
1245 error (_("malformed core note - too short for header"));
1247 gdb::def_vector
<unsigned char> contents (note_size
);
1248 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1250 error (_("could not get core note contents"));
1252 descdata
= contents
.data ();
1253 descend
= descdata
+ note_size
;
1255 /* Skip over the structure size. */
1258 while (descdata
+ KF_PATH
< descend
)
1260 ULONGEST structsize
;
1262 structsize
= bfd_get_32 (core_bfd
, descdata
+ KF_STRUCTSIZE
);
1263 if (structsize
< KF_PATH
)
1264 error (_("malformed core note - file structure too small"));
1266 if (bfd_get_32 (core_bfd
, descdata
+ KF_TYPE
) == KINFO_FILE_TYPE_VNODE
1267 && bfd_get_signed_32 (core_bfd
, descdata
+ KF_FD
) == fd
)
1269 char *path
= (char *) descdata
+ KF_PATH
;
1270 return make_unique_xstrdup (path
);
1273 descdata
+= structsize
;
1278 /* Helper function to read a struct timeval. */
1281 fbsd_core_fetch_timeval (struct gdbarch
*gdbarch
, unsigned char *data
,
1282 LONGEST
&sec
, ULONGEST
&usec
)
1284 if (gdbarch_addr_bit (gdbarch
) == 64)
1286 sec
= bfd_get_signed_64 (core_bfd
, data
);
1287 usec
= bfd_get_64 (core_bfd
, data
+ 8);
1289 else if (bfd_get_arch (core_bfd
) == bfd_arch_i386
)
1291 sec
= bfd_get_signed_32 (core_bfd
, data
);
1292 usec
= bfd_get_32 (core_bfd
, data
+ 4);
1296 sec
= bfd_get_signed_64 (core_bfd
, data
);
1297 usec
= bfd_get_32 (core_bfd
, data
+ 8);
1301 /* Print out the contents of a signal set. */
1304 fbsd_print_sigset (const char *descr
, unsigned char *sigset
)
1306 printf_filtered ("%s: ", descr
);
1307 for (int i
= 0; i
< SIG_WORDS
; i
++)
1308 printf_filtered ("%08x ",
1309 (unsigned int) bfd_get_32 (core_bfd
, sigset
+ i
* 4));
1310 printf_filtered ("\n");
1313 /* Implement "info proc status" for a corefile. */
1316 fbsd_core_info_proc_status (struct gdbarch
*gdbarch
)
1318 const struct kinfo_proc_layout
*kp
;
1320 unsigned char *descdata
;
1321 int addr_bit
, long_bit
;
1326 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.proc");
1327 if (section
== NULL
)
1329 warning (_("unable to find process info in core file"));
1333 addr_bit
= gdbarch_addr_bit (gdbarch
);
1335 kp
= &kinfo_proc_layout_64
;
1336 else if (bfd_get_arch (core_bfd
) == bfd_arch_i386
)
1337 kp
= &kinfo_proc_layout_i386
;
1339 kp
= &kinfo_proc_layout_32
;
1340 long_bit
= gdbarch_long_bit (gdbarch
);
1343 * Ensure that the note is large enough for all of the fields fetched
1344 * by this function. In particular, the note must contain the 32-bit
1345 * structure size, then it must be long enough to access the last
1346 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1348 note_size
= bfd_section_size (section
);
1349 if (note_size
< (4 + kp
->ki_rusage_ch
+ kp
->ru_majflt
1350 + long_bit
/ TARGET_CHAR_BIT
))
1351 error (_("malformed core note - too short"));
1353 gdb::def_vector
<unsigned char> contents (note_size
);
1354 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
1356 error (_("could not get core note contents"));
1358 descdata
= contents
.data ();
1360 /* Skip over the structure size. */
1363 /* Verify 'ki_layout' is 0. */
1364 if (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_layout
) != 0)
1366 warning (_("unsupported process information in core file"));
1370 printf_filtered ("Name: %.19s\n", descdata
+ kp
->ki_comm
);
1371 printf_filtered ("Process ID: %s\n",
1372 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_pid
)));
1373 printf_filtered ("Parent process: %s\n",
1374 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_ppid
)));
1375 printf_filtered ("Process group: %s\n",
1376 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_pgid
)));
1377 printf_filtered ("Session id: %s\n",
1378 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_sid
)));
1380 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1381 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1382 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1383 the structure that is cleared to zero. Assume that a zero value
1384 in ki_tdev indicates a core dump from an older kernel and use the
1385 value in 'ki_tdev_freebsd11' instead. */
1386 value
= bfd_get_64 (core_bfd
, descdata
+ kp
->ki_tdev
);
1388 value
= bfd_get_32 (core_bfd
, descdata
+ kp
->ki_tdev_freebsd11
);
1389 printf_filtered ("TTY: %s\n", pulongest (value
));
1390 printf_filtered ("TTY owner process group: %s\n",
1391 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_tpgid
)));
1392 printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
1393 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_ruid
)),
1394 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_uid
)),
1395 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_svuid
)));
1396 printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
1397 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_rgid
)),
1398 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_groups
)),
1399 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_svgid
)));
1400 printf_filtered ("Groups: ");
1401 uint16_t ngroups
= bfd_get_16 (core_bfd
, descdata
+ kp
->ki_ngroups
);
1402 for (int i
= 0; i
< ngroups
; i
++)
1403 printf_filtered ("%s ",
1404 pulongest (bfd_get_32 (core_bfd
,
1405 descdata
+ kp
->ki_groups
+ i
* 4)));
1406 printf_filtered ("\n");
1407 value
= bfd_get (long_bit
, core_bfd
,
1408 descdata
+ kp
->ki_rusage
+ kp
->ru_minflt
);
1409 printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value
));
1410 value
= bfd_get (long_bit
, core_bfd
,
1411 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_minflt
);
1412 printf_filtered ("Minor faults, children: %s\n", pulongest (value
));
1413 value
= bfd_get (long_bit
, core_bfd
,
1414 descdata
+ kp
->ki_rusage
+ kp
->ru_majflt
);
1415 printf_filtered ("Major faults (memory page faults): %s\n",
1417 value
= bfd_get (long_bit
, core_bfd
,
1418 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_majflt
);
1419 printf_filtered ("Major faults, children: %s\n", pulongest (value
));
1420 fbsd_core_fetch_timeval (gdbarch
,
1421 descdata
+ kp
->ki_rusage
+ kp
->ru_utime
,
1423 printf_filtered ("utime: %s.%06d\n", plongest (sec
), (int) value
);
1424 fbsd_core_fetch_timeval (gdbarch
,
1425 descdata
+ kp
->ki_rusage
+ kp
->ru_stime
,
1427 printf_filtered ("stime: %s.%06d\n", plongest (sec
), (int) value
);
1428 fbsd_core_fetch_timeval (gdbarch
,
1429 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_utime
,
1431 printf_filtered ("utime, children: %s.%06d\n", plongest (sec
), (int) value
);
1432 fbsd_core_fetch_timeval (gdbarch
,
1433 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_stime
,
1435 printf_filtered ("stime, children: %s.%06d\n", plongest (sec
), (int) value
);
1436 printf_filtered ("'nice' value: %d\n",
1437 (int) bfd_get_signed_8 (core_bfd
, descdata
+ kp
->ki_nice
));
1438 fbsd_core_fetch_timeval (gdbarch
, descdata
+ kp
->ki_start
, sec
, value
);
1439 printf_filtered ("Start time: %s.%06d\n", plongest (sec
), (int) value
);
1440 printf_filtered ("Virtual memory size: %s kB\n",
1441 pulongest (bfd_get (addr_bit
, core_bfd
,
1442 descdata
+ kp
->ki_size
) / 1024));
1443 printf_filtered ("Data size: %s pages\n",
1444 pulongest (bfd_get (addr_bit
, core_bfd
,
1445 descdata
+ kp
->ki_dsize
)));
1446 printf_filtered ("Stack size: %s pages\n",
1447 pulongest (bfd_get (addr_bit
, core_bfd
,
1448 descdata
+ kp
->ki_ssize
)));
1449 printf_filtered ("Text size: %s pages\n",
1450 pulongest (bfd_get (addr_bit
, core_bfd
,
1451 descdata
+ kp
->ki_tsize
)));
1452 printf_filtered ("Resident set size: %s pages\n",
1453 pulongest (bfd_get (addr_bit
, core_bfd
,
1454 descdata
+ kp
->ki_rssize
)));
1455 printf_filtered ("Maximum RSS: %s pages\n",
1456 pulongest (bfd_get (long_bit
, core_bfd
,
1457 descdata
+ kp
->ki_rusage
1459 fbsd_print_sigset ("Ignored Signals", descdata
+ kp
->ki_sigignore
);
1460 fbsd_print_sigset ("Caught Signals", descdata
+ kp
->ki_sigcatch
);
1463 /* Implement the "core_info_proc" gdbarch method. */
1466 fbsd_core_info_proc (struct gdbarch
*gdbarch
, const char *args
,
1467 enum info_proc_what what
)
1469 bool do_cmdline
= false;
1470 bool do_cwd
= false;
1471 bool do_exe
= false;
1472 bool do_files
= false;
1473 bool do_mappings
= false;
1474 bool do_status
= false;
1515 pid
= bfd_core_file_pid (core_bfd
);
1517 printf_filtered (_("process %d\n"), pid
);
1521 const char *cmdline
;
1523 cmdline
= bfd_core_file_failing_command (core_bfd
);
1525 printf_filtered ("cmdline = '%s'\n", cmdline
);
1527 warning (_("Command line unavailable"));
1531 gdb::unique_xmalloc_ptr
<char> cwd
=
1532 fbsd_core_vnode_path (gdbarch
, KINFO_FILE_FD_TYPE_CWD
);
1534 printf_filtered ("cwd = '%s'\n", cwd
.get ());
1536 warning (_("unable to read current working directory"));
1540 gdb::unique_xmalloc_ptr
<char> exe
=
1541 fbsd_core_vnode_path (gdbarch
, KINFO_FILE_FD_TYPE_TEXT
);
1543 printf_filtered ("exe = '%s'\n", exe
.get ());
1545 warning (_("unable to read executable path name"));
1548 fbsd_core_info_proc_files (gdbarch
);
1550 fbsd_core_info_proc_mappings (gdbarch
);
1552 fbsd_core_info_proc_status (gdbarch
);
1555 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1558 fbsd_print_auxv_entry (struct gdbarch
*gdbarch
, struct ui_file
*file
,
1559 CORE_ADDR type
, CORE_ADDR val
)
1561 const char *name
= "???";
1562 const char *description
= "";
1563 enum auxv_format format
= AUXV_FORMAT_HEX
;
1582 default_print_auxv_entry (gdbarch
, file
, type
, val
);
1584 #define _TAGNAME(tag) #tag
1585 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1586 #define TAG(tag, text, kind) \
1587 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1588 TAG (EXECPATH
, _("Executable path"), AUXV_FORMAT_STR
);
1589 TAG (CANARY
, _("Canary for SSP"), AUXV_FORMAT_HEX
);
1590 TAG (CANARYLEN
, ("Length of the SSP canary"), AUXV_FORMAT_DEC
);
1591 TAG (OSRELDATE
, _("OSRELDATE"), AUXV_FORMAT_DEC
);
1592 TAG (NCPUS
, _("Number of CPUs"), AUXV_FORMAT_DEC
);
1593 TAG (PAGESIZES
, _("Pagesizes"), AUXV_FORMAT_HEX
);
1594 TAG (PAGESIZESLEN
, _("Number of pagesizes"), AUXV_FORMAT_DEC
);
1595 TAG (TIMEKEEP
, _("Pointer to timehands"), AUXV_FORMAT_HEX
);
1596 TAG (STACKPROT
, _("Initial stack protection"), AUXV_FORMAT_HEX
);
1597 TAG (EHDRFLAGS
, _("ELF header e_flags"), AUXV_FORMAT_HEX
);
1598 TAG (HWCAP
, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX
);
1599 TAG (HWCAP2
, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX
);
1602 fprint_auxv_entry (file
, name
, description
, format
, type
, val
);
1605 /* Implement the "get_siginfo_type" gdbarch method. */
1607 static struct type
*
1608 fbsd_get_siginfo_type (struct gdbarch
*gdbarch
)
1610 struct fbsd_gdbarch_data
*fbsd_gdbarch_data
;
1611 struct type
*int_type
, *int32_type
, *uint32_type
, *long_type
, *void_ptr_type
;
1612 struct type
*uid_type
, *pid_type
;
1613 struct type
*sigval_type
, *reason_type
;
1614 struct type
*siginfo_type
;
1617 fbsd_gdbarch_data
= get_fbsd_gdbarch_data (gdbarch
);
1618 if (fbsd_gdbarch_data
->siginfo_type
!= NULL
)
1619 return fbsd_gdbarch_data
->siginfo_type
;
1621 int_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
1623 int32_type
= arch_integer_type (gdbarch
, 32, 0, "int32_t");
1624 uint32_type
= arch_integer_type (gdbarch
, 32, 1, "uint32_t");
1625 long_type
= arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
1627 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
1630 sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
1631 TYPE_NAME (sigval_type
) = xstrdup ("sigval");
1632 append_composite_type_field (sigval_type
, "sival_int", int_type
);
1633 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
1636 pid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
1637 TYPE_LENGTH (int32_type
) * TARGET_CHAR_BIT
, "__pid_t");
1638 TYPE_TARGET_TYPE (pid_type
) = int32_type
;
1639 TYPE_TARGET_STUB (pid_type
) = 1;
1642 uid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
1643 TYPE_LENGTH (uint32_type
) * TARGET_CHAR_BIT
,
1645 TYPE_TARGET_TYPE (uid_type
) = uint32_type
;
1646 TYPE_TARGET_STUB (uid_type
) = 1;
1649 reason_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
1652 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1653 append_composite_type_field (type
, "si_trapno", int_type
);
1654 append_composite_type_field (reason_type
, "_fault", type
);
1657 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1658 append_composite_type_field (type
, "si_timerid", int_type
);
1659 append_composite_type_field (type
, "si_overrun", int_type
);
1660 append_composite_type_field (reason_type
, "_timer", type
);
1663 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1664 append_composite_type_field (type
, "si_mqd", int_type
);
1665 append_composite_type_field (reason_type
, "_mesgq", type
);
1668 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1669 append_composite_type_field (type
, "si_band", long_type
);
1670 append_composite_type_field (reason_type
, "_poll", type
);
1673 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1674 append_composite_type_field (type
, "__spare1__", long_type
);
1675 append_composite_type_field (type
, "__spare2__",
1676 init_vector_type (int_type
, 7));
1677 append_composite_type_field (reason_type
, "__spare__", type
);
1679 /* struct siginfo */
1680 siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1681 TYPE_NAME (siginfo_type
) = xstrdup ("siginfo");
1682 append_composite_type_field (siginfo_type
, "si_signo", int_type
);
1683 append_composite_type_field (siginfo_type
, "si_errno", int_type
);
1684 append_composite_type_field (siginfo_type
, "si_code", int_type
);
1685 append_composite_type_field (siginfo_type
, "si_pid", pid_type
);
1686 append_composite_type_field (siginfo_type
, "si_uid", uid_type
);
1687 append_composite_type_field (siginfo_type
, "si_status", int_type
);
1688 append_composite_type_field (siginfo_type
, "si_addr", void_ptr_type
);
1689 append_composite_type_field (siginfo_type
, "si_value", sigval_type
);
1690 append_composite_type_field (siginfo_type
, "_reason", reason_type
);
1692 fbsd_gdbarch_data
->siginfo_type
= siginfo_type
;
1694 return siginfo_type
;
1697 /* Implement the "gdb_signal_from_target" gdbarch method. */
1699 static enum gdb_signal
1700 fbsd_gdb_signal_from_target (struct gdbarch
*gdbarch
, int signal
)
1705 return GDB_SIGNAL_0
;
1707 case FREEBSD_SIGHUP
:
1708 return GDB_SIGNAL_HUP
;
1710 case FREEBSD_SIGINT
:
1711 return GDB_SIGNAL_INT
;
1713 case FREEBSD_SIGQUIT
:
1714 return GDB_SIGNAL_QUIT
;
1716 case FREEBSD_SIGILL
:
1717 return GDB_SIGNAL_ILL
;
1719 case FREEBSD_SIGTRAP
:
1720 return GDB_SIGNAL_TRAP
;
1722 case FREEBSD_SIGABRT
:
1723 return GDB_SIGNAL_ABRT
;
1725 case FREEBSD_SIGEMT
:
1726 return GDB_SIGNAL_EMT
;
1728 case FREEBSD_SIGFPE
:
1729 return GDB_SIGNAL_FPE
;
1731 case FREEBSD_SIGKILL
:
1732 return GDB_SIGNAL_KILL
;
1734 case FREEBSD_SIGBUS
:
1735 return GDB_SIGNAL_BUS
;
1737 case FREEBSD_SIGSEGV
:
1738 return GDB_SIGNAL_SEGV
;
1740 case FREEBSD_SIGSYS
:
1741 return GDB_SIGNAL_SYS
;
1743 case FREEBSD_SIGPIPE
:
1744 return GDB_SIGNAL_PIPE
;
1746 case FREEBSD_SIGALRM
:
1747 return GDB_SIGNAL_ALRM
;
1749 case FREEBSD_SIGTERM
:
1750 return GDB_SIGNAL_TERM
;
1752 case FREEBSD_SIGURG
:
1753 return GDB_SIGNAL_URG
;
1755 case FREEBSD_SIGSTOP
:
1756 return GDB_SIGNAL_STOP
;
1758 case FREEBSD_SIGTSTP
:
1759 return GDB_SIGNAL_TSTP
;
1761 case FREEBSD_SIGCONT
:
1762 return GDB_SIGNAL_CONT
;
1764 case FREEBSD_SIGCHLD
:
1765 return GDB_SIGNAL_CHLD
;
1767 case FREEBSD_SIGTTIN
:
1768 return GDB_SIGNAL_TTIN
;
1770 case FREEBSD_SIGTTOU
:
1771 return GDB_SIGNAL_TTOU
;
1774 return GDB_SIGNAL_IO
;
1776 case FREEBSD_SIGXCPU
:
1777 return GDB_SIGNAL_XCPU
;
1779 case FREEBSD_SIGXFSZ
:
1780 return GDB_SIGNAL_XFSZ
;
1782 case FREEBSD_SIGVTALRM
:
1783 return GDB_SIGNAL_VTALRM
;
1785 case FREEBSD_SIGPROF
:
1786 return GDB_SIGNAL_PROF
;
1788 case FREEBSD_SIGWINCH
:
1789 return GDB_SIGNAL_WINCH
;
1791 case FREEBSD_SIGINFO
:
1792 return GDB_SIGNAL_INFO
;
1794 case FREEBSD_SIGUSR1
:
1795 return GDB_SIGNAL_USR1
;
1797 case FREEBSD_SIGUSR2
:
1798 return GDB_SIGNAL_USR2
;
1800 /* SIGTHR is the same as SIGLWP on FreeBSD. */
1801 case FREEBSD_SIGTHR
:
1802 return GDB_SIGNAL_LWP
;
1804 case FREEBSD_SIGLIBRT
:
1805 return GDB_SIGNAL_LIBRT
;
1808 if (signal
>= FREEBSD_SIGRTMIN
&& signal
<= FREEBSD_SIGRTMAX
)
1810 int offset
= signal
- FREEBSD_SIGRTMIN
;
1812 return (enum gdb_signal
) ((int) GDB_SIGNAL_REALTIME_65
+ offset
);
1815 return GDB_SIGNAL_UNKNOWN
;
1818 /* Implement the "gdb_signal_to_target" gdbarch method. */
1821 fbsd_gdb_signal_to_target (struct gdbarch
*gdbarch
,
1822 enum gdb_signal signal
)
1829 case GDB_SIGNAL_HUP
:
1830 return FREEBSD_SIGHUP
;
1832 case GDB_SIGNAL_INT
:
1833 return FREEBSD_SIGINT
;
1835 case GDB_SIGNAL_QUIT
:
1836 return FREEBSD_SIGQUIT
;
1838 case GDB_SIGNAL_ILL
:
1839 return FREEBSD_SIGILL
;
1841 case GDB_SIGNAL_TRAP
:
1842 return FREEBSD_SIGTRAP
;
1844 case GDB_SIGNAL_ABRT
:
1845 return FREEBSD_SIGABRT
;
1847 case GDB_SIGNAL_EMT
:
1848 return FREEBSD_SIGEMT
;
1850 case GDB_SIGNAL_FPE
:
1851 return FREEBSD_SIGFPE
;
1853 case GDB_SIGNAL_KILL
:
1854 return FREEBSD_SIGKILL
;
1856 case GDB_SIGNAL_BUS
:
1857 return FREEBSD_SIGBUS
;
1859 case GDB_SIGNAL_SEGV
:
1860 return FREEBSD_SIGSEGV
;
1862 case GDB_SIGNAL_SYS
:
1863 return FREEBSD_SIGSYS
;
1865 case GDB_SIGNAL_PIPE
:
1866 return FREEBSD_SIGPIPE
;
1868 case GDB_SIGNAL_ALRM
:
1869 return FREEBSD_SIGALRM
;
1871 case GDB_SIGNAL_TERM
:
1872 return FREEBSD_SIGTERM
;
1874 case GDB_SIGNAL_URG
:
1875 return FREEBSD_SIGURG
;
1877 case GDB_SIGNAL_STOP
:
1878 return FREEBSD_SIGSTOP
;
1880 case GDB_SIGNAL_TSTP
:
1881 return FREEBSD_SIGTSTP
;
1883 case GDB_SIGNAL_CONT
:
1884 return FREEBSD_SIGCONT
;
1886 case GDB_SIGNAL_CHLD
:
1887 return FREEBSD_SIGCHLD
;
1889 case GDB_SIGNAL_TTIN
:
1890 return FREEBSD_SIGTTIN
;
1892 case GDB_SIGNAL_TTOU
:
1893 return FREEBSD_SIGTTOU
;
1896 return FREEBSD_SIGIO
;
1898 case GDB_SIGNAL_XCPU
:
1899 return FREEBSD_SIGXCPU
;
1901 case GDB_SIGNAL_XFSZ
:
1902 return FREEBSD_SIGXFSZ
;
1904 case GDB_SIGNAL_VTALRM
:
1905 return FREEBSD_SIGVTALRM
;
1907 case GDB_SIGNAL_PROF
:
1908 return FREEBSD_SIGPROF
;
1910 case GDB_SIGNAL_WINCH
:
1911 return FREEBSD_SIGWINCH
;
1913 case GDB_SIGNAL_INFO
:
1914 return FREEBSD_SIGINFO
;
1916 case GDB_SIGNAL_USR1
:
1917 return FREEBSD_SIGUSR1
;
1919 case GDB_SIGNAL_USR2
:
1920 return FREEBSD_SIGUSR2
;
1922 case GDB_SIGNAL_LWP
:
1923 return FREEBSD_SIGTHR
;
1925 case GDB_SIGNAL_LIBRT
:
1926 return FREEBSD_SIGLIBRT
;
1929 if (signal
>= GDB_SIGNAL_REALTIME_65
1930 && signal
<= GDB_SIGNAL_REALTIME_126
)
1932 int offset
= signal
- GDB_SIGNAL_REALTIME_65
;
1934 return FREEBSD_SIGRTMIN
+ offset
;
1940 /* Implement the "get_syscall_number" gdbarch method. */
1943 fbsd_get_syscall_number (struct gdbarch
*gdbarch
, thread_info
*thread
)
1946 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1947 native targets fetch the system call number from the
1948 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1949 However, system call catching requires this function to be
1952 internal_error (__FILE__
, __LINE__
, _("fbsd_get_sycall_number called"));
1955 /* Read an integer symbol value from the current target. */
1958 fbsd_read_integer_by_name (struct gdbarch
*gdbarch
, const char *name
)
1960 bound_minimal_symbol ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
1961 if (ms
.minsym
== NULL
)
1962 error (_("Unable to resolve symbol '%s'"), name
);
1965 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms
), buf
, sizeof buf
) != 0)
1966 error (_("Unable to read value of '%s'"), name
);
1968 return extract_signed_integer (buf
, sizeof buf
, gdbarch_byte_order (gdbarch
));
1971 /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
1972 structure needed to determine the TLS index of an object file. */
1975 fbsd_fetch_rtld_offsets (struct gdbarch
*gdbarch
, struct fbsd_pspace_data
*data
)
1979 /* Fetch offsets from debug symbols in rtld. */
1980 struct symbol
*obj_entry_sym
1981 = lookup_symbol_in_language ("Struct_Obj_Entry", NULL
, STRUCT_DOMAIN
,
1982 language_c
, NULL
).symbol
;
1983 if (obj_entry_sym
== NULL
)
1984 error (_("Unable to find Struct_Obj_Entry symbol"));
1985 data
->off_linkmap
= lookup_struct_elt (SYMBOL_TYPE(obj_entry_sym
),
1986 "linkmap", 0).offset
/ 8;
1987 data
->off_tlsindex
= lookup_struct_elt (SYMBOL_TYPE(obj_entry_sym
),
1988 "tlsindex", 0).offset
/ 8;
1989 data
->rtld_offsets_valid
= true;
1992 catch (const gdb_exception_error
&e
)
1994 data
->off_linkmap
= -1;
1999 /* Fetch offsets from global variables in libthr. Note that
2000 this does not work for single-threaded processes that are not
2001 linked against libthr. */
2002 data
->off_linkmap
= fbsd_read_integer_by_name (gdbarch
,
2003 "_thread_off_linkmap");
2004 data
->off_tlsindex
= fbsd_read_integer_by_name (gdbarch
,
2005 "_thread_off_tlsindex");
2006 data
->rtld_offsets_valid
= true;
2009 catch (const gdb_exception_error
&e
)
2011 data
->off_linkmap
= -1;
2015 /* Helper function to read the TLS index of an object file associated
2016 with a link map entry at LM_ADDR. */
2019 fbsd_get_tls_index (struct gdbarch
*gdbarch
, CORE_ADDR lm_addr
)
2021 struct fbsd_pspace_data
*data
= get_fbsd_pspace_data (current_program_space
);
2023 if (!data
->rtld_offsets_valid
)
2024 fbsd_fetch_rtld_offsets (gdbarch
, data
);
2026 if (data
->off_linkmap
== -1)
2027 throw_error (TLS_GENERIC_ERROR
,
2028 _("Cannot fetch runtime linker structure offsets"));
2030 /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
2031 pointer and then compute the offset of the tlsindex member. */
2032 CORE_ADDR tlsindex_addr
= lm_addr
- data
->off_linkmap
+ data
->off_tlsindex
;
2035 if (target_read_memory (tlsindex_addr
, buf
, sizeof buf
) != 0)
2036 throw_error (TLS_GENERIC_ERROR
,
2037 _("Cannot find thread-local variables on this target"));
2039 return extract_signed_integer (buf
, sizeof buf
, gdbarch_byte_order (gdbarch
));
2042 /* See fbsd-tdep.h. */
2045 fbsd_get_thread_local_address (struct gdbarch
*gdbarch
, CORE_ADDR dtv_addr
,
2046 CORE_ADDR lm_addr
, CORE_ADDR offset
)
2048 LONGEST tls_index
= fbsd_get_tls_index (gdbarch
, lm_addr
);
2050 gdb_byte buf
[gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
];
2051 if (target_read_memory (dtv_addr
, buf
, sizeof buf
) != 0)
2052 throw_error (TLS_GENERIC_ERROR
,
2053 _("Cannot find thread-local variables on this target"));
2055 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
2056 CORE_ADDR addr
= gdbarch_pointer_to_address (gdbarch
,
2057 builtin
->builtin_data_ptr
, buf
);
2059 addr
+= (tls_index
+ 1) * TYPE_LENGTH (builtin
->builtin_data_ptr
);
2060 if (target_read_memory (addr
, buf
, sizeof buf
) != 0)
2061 throw_error (TLS_GENERIC_ERROR
,
2062 _("Cannot find thread-local variables on this target"));
2064 addr
= gdbarch_pointer_to_address (gdbarch
, builtin
->builtin_data_ptr
, buf
);
2065 return addr
+ offset
;
2068 /* To be called from GDB_OSABI_FREEBSD handlers. */
2071 fbsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
2073 set_gdbarch_core_pid_to_str (gdbarch
, fbsd_core_pid_to_str
);
2074 set_gdbarch_core_thread_name (gdbarch
, fbsd_core_thread_name
);
2075 set_gdbarch_core_xfer_siginfo (gdbarch
, fbsd_core_xfer_siginfo
);
2076 set_gdbarch_make_corefile_notes (gdbarch
, fbsd_make_corefile_notes
);
2077 set_gdbarch_core_info_proc (gdbarch
, fbsd_core_info_proc
);
2078 set_gdbarch_print_auxv_entry (gdbarch
, fbsd_print_auxv_entry
);
2079 set_gdbarch_get_siginfo_type (gdbarch
, fbsd_get_siginfo_type
);
2080 set_gdbarch_gdb_signal_from_target (gdbarch
, fbsd_gdb_signal_from_target
);
2081 set_gdbarch_gdb_signal_to_target (gdbarch
, fbsd_gdb_signal_to_target
);
2083 /* `catch syscall' */
2084 set_xml_syscall_file_name (gdbarch
, "syscalls/freebsd.xml");
2085 set_gdbarch_get_syscall_number (gdbarch
, fbsd_get_syscall_number
);
2088 void _initialize_fbsd_tdep ();
2090 _initialize_fbsd_tdep ()
2092 fbsd_gdbarch_data_handle
=
2093 gdbarch_data_register_post_init (init_fbsd_gdbarch_data
);