Home | History | Annotate | Line # | Download | only in gdb
fbsd-tdep.c revision 1.9
      1  1.1  christos /* Target-dependent code for FreeBSD, architecture-independent.
      2  1.1  christos 
      3  1.9  christos    Copyright (C) 2002-2024 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.4  christos #include "auxv.h"
     21  1.9  christos #include "extract-store-integer.h"
     22  1.1  christos #include "gdbcore.h"
     23  1.1  christos #include "inferior.h"
     24  1.7  christos #include "objfiles.h"
     25  1.1  christos #include "regcache.h"
     26  1.1  christos #include "regset.h"
     27  1.1  christos #include "gdbthread.h"
     28  1.7  christos #include "objfiles.h"
     29  1.4  christos #include "xml-syscall.h"
     30  1.6  christos #include <sys/socket.h>
     31  1.6  christos #include <arpa/inet.h>
     32  1.1  christos 
     33  1.1  christos #include "elf-bfd.h"
     34  1.1  christos #include "fbsd-tdep.h"
     35  1.8  christos #include "gcore-elf.h"
     36  1.1  christos 
     37  1.6  christos /* This enum is derived from FreeBSD's <sys/signal.h>.  */
     38  1.6  christos 
     39  1.6  christos enum
     40  1.6  christos   {
     41  1.6  christos     FREEBSD_SIGHUP = 1,
     42  1.6  christos     FREEBSD_SIGINT = 2,
     43  1.6  christos     FREEBSD_SIGQUIT = 3,
     44  1.6  christos     FREEBSD_SIGILL = 4,
     45  1.6  christos     FREEBSD_SIGTRAP = 5,
     46  1.6  christos     FREEBSD_SIGABRT = 6,
     47  1.6  christos     FREEBSD_SIGEMT = 7,
     48  1.6  christos     FREEBSD_SIGFPE = 8,
     49  1.6  christos     FREEBSD_SIGKILL = 9,
     50  1.6  christos     FREEBSD_SIGBUS = 10,
     51  1.6  christos     FREEBSD_SIGSEGV = 11,
     52  1.6  christos     FREEBSD_SIGSYS = 12,
     53  1.6  christos     FREEBSD_SIGPIPE = 13,
     54  1.6  christos     FREEBSD_SIGALRM = 14,
     55  1.6  christos     FREEBSD_SIGTERM = 15,
     56  1.6  christos     FREEBSD_SIGURG = 16,
     57  1.6  christos     FREEBSD_SIGSTOP = 17,
     58  1.6  christos     FREEBSD_SIGTSTP = 18,
     59  1.6  christos     FREEBSD_SIGCONT = 19,
     60  1.6  christos     FREEBSD_SIGCHLD = 20,
     61  1.6  christos     FREEBSD_SIGTTIN = 21,
     62  1.6  christos     FREEBSD_SIGTTOU = 22,
     63  1.6  christos     FREEBSD_SIGIO = 23,
     64  1.6  christos     FREEBSD_SIGXCPU = 24,
     65  1.6  christos     FREEBSD_SIGXFSZ = 25,
     66  1.6  christos     FREEBSD_SIGVTALRM = 26,
     67  1.6  christos     FREEBSD_SIGPROF = 27,
     68  1.6  christos     FREEBSD_SIGWINCH = 28,
     69  1.6  christos     FREEBSD_SIGINFO = 29,
     70  1.6  christos     FREEBSD_SIGUSR1 = 30,
     71  1.6  christos     FREEBSD_SIGUSR2 = 31,
     72  1.6  christos     FREEBSD_SIGTHR = 32,
     73  1.6  christos     FREEBSD_SIGLIBRT = 33,
     74  1.6  christos     FREEBSD_SIGRTMIN = 65,
     75  1.6  christos     FREEBSD_SIGRTMAX = 126,
     76  1.6  christos   };
     77  1.6  christos 
     78  1.8  christos /* Constants for values of si_code as defined in FreeBSD's
     79  1.8  christos    <sys/signal.h>.  */
     80  1.8  christos 
     81  1.8  christos #define	FBSD_SI_USER		0x10001
     82  1.8  christos #define	FBSD_SI_QUEUE		0x10002
     83  1.8  christos #define	FBSD_SI_TIMER		0x10003
     84  1.8  christos #define	FBSD_SI_ASYNCIO		0x10004
     85  1.8  christos #define	FBSD_SI_MESGQ		0x10005
     86  1.8  christos #define	FBSD_SI_KERNEL		0x10006
     87  1.8  christos #define	FBSD_SI_LWP		0x10007
     88  1.8  christos 
     89  1.8  christos #define	FBSD_ILL_ILLOPC		1
     90  1.8  christos #define	FBSD_ILL_ILLOPN		2
     91  1.8  christos #define	FBSD_ILL_ILLADR		3
     92  1.8  christos #define	FBSD_ILL_ILLTRP		4
     93  1.8  christos #define	FBSD_ILL_PRVOPC		5
     94  1.8  christos #define	FBSD_ILL_PRVREG		6
     95  1.8  christos #define	FBSD_ILL_COPROC		7
     96  1.8  christos #define	FBSD_ILL_BADSTK		8
     97  1.8  christos 
     98  1.8  christos #define	FBSD_BUS_ADRALN		1
     99  1.8  christos #define	FBSD_BUS_ADRERR		2
    100  1.8  christos #define	FBSD_BUS_OBJERR		3
    101  1.8  christos #define	FBSD_BUS_OOMERR		100
    102  1.8  christos 
    103  1.8  christos #define	FBSD_SEGV_MAPERR	1
    104  1.8  christos #define	FBSD_SEGV_ACCERR	2
    105  1.8  christos #define	FBSD_SEGV_PKUERR	100
    106  1.8  christos 
    107  1.8  christos #define	FBSD_FPE_INTOVF		1
    108  1.8  christos #define	FBSD_FPE_INTDIV		2
    109  1.8  christos #define	FBSD_FPE_FLTDIV		3
    110  1.8  christos #define	FBSD_FPE_FLTOVF		4
    111  1.8  christos #define	FBSD_FPE_FLTUND		5
    112  1.8  christos #define	FBSD_FPE_FLTRES		6
    113  1.8  christos #define	FBSD_FPE_FLTINV		7
    114  1.8  christos #define	FBSD_FPE_FLTSUB		8
    115  1.8  christos 
    116  1.8  christos #define	FBSD_TRAP_BRKPT		1
    117  1.8  christos #define	FBSD_TRAP_TRACE		2
    118  1.8  christos #define	FBSD_TRAP_DTRACE	3
    119  1.8  christos #define	FBSD_TRAP_CAP		4
    120  1.8  christos 
    121  1.8  christos #define	FBSD_CLD_EXITED		1
    122  1.8  christos #define	FBSD_CLD_KILLED		2
    123  1.8  christos #define	FBSD_CLD_DUMPED		3
    124  1.8  christos #define	FBSD_CLD_TRAPPED	4
    125  1.8  christos #define	FBSD_CLD_STOPPED	5
    126  1.8  christos #define	FBSD_CLD_CONTINUED	6
    127  1.8  christos 
    128  1.8  christos #define	FBSD_POLL_IN		1
    129  1.8  christos #define	FBSD_POLL_OUT		2
    130  1.8  christos #define	FBSD_POLL_MSG		3
    131  1.8  christos #define	FBSD_POLL_ERR		4
    132  1.8  christos #define	FBSD_POLL_PRI		5
    133  1.8  christos #define	FBSD_POLL_HUP		6
    134  1.8  christos 
    135  1.6  christos /* FreeBSD kernels 12.0 and later include a copy of the
    136  1.6  christos    'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
    137  1.6  christos    operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
    138  1.6  christos    The constants below define the offset of field members and flags in
    139  1.6  christos    this structure used by methods in this file.  Note that the
    140  1.6  christos    'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
    141  1.6  christos    containing the size of the structure.  */
    142  1.6  christos 
    143  1.6  christos #define	LWPINFO_OFFSET		0x4
    144  1.6  christos 
    145  1.6  christos /* Offsets in ptrace_lwpinfo.  */
    146  1.6  christos #define	LWPINFO_PL_FLAGS	0x8
    147  1.6  christos #define	LWPINFO64_PL_SIGINFO	0x30
    148  1.6  christos #define	LWPINFO32_PL_SIGINFO	0x2c
    149  1.6  christos 
    150  1.6  christos /* Flags in pl_flags.  */
    151  1.6  christos #define	PL_FLAG_SI	0x20	/* siginfo is valid */
    152  1.6  christos 
    153  1.6  christos /* Sizes of siginfo_t.	*/
    154  1.6  christos #define	SIZE64_SIGINFO_T	80
    155  1.6  christos #define	SIZE32_SIGINFO_T	64
    156  1.6  christos 
    157  1.6  christos /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
    158  1.6  christos    dump notes.  See <sys/user.h> for the definition of struct
    159  1.6  christos    kinfo_vmentry.  This data structure should have the same layout on
    160  1.6  christos    all architectures.
    161  1.6  christos 
    162  1.6  christos    Note that FreeBSD 7.0 used an older version of this structure
    163  1.7  christos    (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
    164  1.6  christos    dump note wasn't introduced until FreeBSD 9.2.  As a result, the
    165  1.6  christos    core dump note has always used the 7.1 and later structure
    166  1.6  christos    format.  */
    167  1.6  christos 
    168  1.6  christos #define	KVE_STRUCTSIZE		0x0
    169  1.6  christos #define	KVE_START		0x8
    170  1.6  christos #define	KVE_END			0x10
    171  1.6  christos #define	KVE_OFFSET		0x18
    172  1.6  christos #define	KVE_FLAGS		0x2c
    173  1.6  christos #define	KVE_PROTECTION		0x38
    174  1.6  christos #define	KVE_PATH		0x88
    175  1.6  christos 
    176  1.6  christos /* Flags in the 'kve_protection' field in struct kinfo_vmentry.  These
    177  1.6  christos    match the KVME_PROT_* constants in <sys/user.h>.  */
    178  1.6  christos 
    179  1.6  christos #define	KINFO_VME_PROT_READ	0x00000001
    180  1.6  christos #define	KINFO_VME_PROT_WRITE	0x00000002
    181  1.6  christos #define	KINFO_VME_PROT_EXEC	0x00000004
    182  1.6  christos 
    183  1.6  christos /* Flags in the 'kve_flags' field in struct kinfo_vmentry.  These
    184  1.6  christos    match the KVME_FLAG_* constants in <sys/user.h>.  */
    185  1.6  christos 
    186  1.6  christos #define	KINFO_VME_FLAG_COW		0x00000001
    187  1.6  christos #define	KINFO_VME_FLAG_NEEDS_COPY	0x00000002
    188  1.6  christos #define	KINFO_VME_FLAG_NOCOREDUMP	0x00000004
    189  1.6  christos #define	KINFO_VME_FLAG_SUPER		0x00000008
    190  1.6  christos #define	KINFO_VME_FLAG_GROWS_UP		0x00000010
    191  1.6  christos #define	KINFO_VME_FLAG_GROWS_DOWN	0x00000020
    192  1.6  christos 
    193  1.6  christos /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
    194  1.6  christos    dump notes.  See <sys/user.h> for the definition of struct
    195  1.6  christos    kinfo_file.  This data structure should have the same layout on all
    196  1.6  christos    architectures.
    197  1.6  christos 
    198  1.6  christos    Note that FreeBSD 7.0 used an older version of this structure
    199  1.6  christos    (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
    200  1.6  christos    note wasn't introduced until FreeBSD 9.2.  As a result, the core
    201  1.6  christos    dump note has always used the 7.1 and later structure format.  */
    202  1.6  christos 
    203  1.6  christos #define	KF_STRUCTSIZE		0x0
    204  1.6  christos #define	KF_TYPE			0x4
    205  1.6  christos #define	KF_FD			0x8
    206  1.6  christos #define	KF_FLAGS		0x10
    207  1.6  christos #define	KF_OFFSET		0x18
    208  1.6  christos #define	KF_VNODE_TYPE		0x20
    209  1.6  christos #define	KF_SOCK_DOMAIN		0x24
    210  1.6  christos #define	KF_SOCK_TYPE		0x28
    211  1.6  christos #define	KF_SOCK_PROTOCOL	0x2c
    212  1.6  christos #define	KF_SA_LOCAL		0x30
    213  1.6  christos #define	KF_SA_PEER		0xb0
    214  1.6  christos #define	KF_PATH			0x170
    215  1.6  christos 
    216  1.6  christos /* Constants for the 'kf_type' field in struct kinfo_file.  These
    217  1.6  christos    match the KF_TYPE_* constants in <sys/user.h>.  */
    218  1.6  christos 
    219  1.6  christos #define	KINFO_FILE_TYPE_VNODE	1
    220  1.6  christos #define	KINFO_FILE_TYPE_SOCKET	2
    221  1.6  christos #define	KINFO_FILE_TYPE_PIPE	3
    222  1.6  christos #define	KINFO_FILE_TYPE_FIFO	4
    223  1.6  christos #define	KINFO_FILE_TYPE_KQUEUE	5
    224  1.6  christos #define	KINFO_FILE_TYPE_CRYPTO	6
    225  1.6  christos #define	KINFO_FILE_TYPE_MQUEUE	7
    226  1.6  christos #define	KINFO_FILE_TYPE_SHM	8
    227  1.6  christos #define	KINFO_FILE_TYPE_SEM	9
    228  1.6  christos #define	KINFO_FILE_TYPE_PTS	10
    229  1.6  christos #define	KINFO_FILE_TYPE_PROCDESC 11
    230  1.6  christos 
    231  1.6  christos /* Special values for the 'kf_fd' field in struct kinfo_file.  These
    232  1.6  christos    match the KF_FD_TYPE_* constants in <sys/user.h>.  */
    233  1.6  christos 
    234  1.6  christos #define	KINFO_FILE_FD_TYPE_CWD	-1
    235  1.6  christos #define	KINFO_FILE_FD_TYPE_ROOT	-2
    236  1.6  christos #define	KINFO_FILE_FD_TYPE_JAIL	-3
    237  1.6  christos #define	KINFO_FILE_FD_TYPE_TRACE -4
    238  1.6  christos #define	KINFO_FILE_FD_TYPE_TEXT	-5
    239  1.6  christos #define	KINFO_FILE_FD_TYPE_CTTY	-6
    240  1.6  christos 
    241  1.6  christos /* Flags in the 'kf_flags' field in struct kinfo_file.  These match
    242  1.6  christos    the KF_FLAG_* constants in <sys/user.h>.  */
    243  1.6  christos 
    244  1.6  christos #define	KINFO_FILE_FLAG_READ		0x00000001
    245  1.6  christos #define	KINFO_FILE_FLAG_WRITE		0x00000002
    246  1.6  christos #define	KINFO_FILE_FLAG_APPEND		0x00000004
    247  1.6  christos #define	KINFO_FILE_FLAG_ASYNC		0x00000008
    248  1.6  christos #define	KINFO_FILE_FLAG_FSYNC		0x00000010
    249  1.6  christos #define	KINFO_FILE_FLAG_NONBLOCK	0x00000020
    250  1.6  christos #define	KINFO_FILE_FLAG_DIRECT		0x00000040
    251  1.6  christos #define	KINFO_FILE_FLAG_HASLOCK		0x00000080
    252  1.6  christos #define	KINFO_FILE_FLAG_EXEC		0x00004000
    253  1.6  christos 
    254  1.6  christos /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
    255  1.6  christos    These match the KF_VTYPE_* constants in <sys/user.h>.  */
    256  1.6  christos 
    257  1.6  christos #define	KINFO_FILE_VTYPE_VREG	1
    258  1.6  christos #define	KINFO_FILE_VTYPE_VDIR	2
    259  1.6  christos #define	KINFO_FILE_VTYPE_VCHR	4
    260  1.6  christos #define	KINFO_FILE_VTYPE_VLNK	5
    261  1.6  christos #define	KINFO_FILE_VTYPE_VSOCK	6
    262  1.6  christos #define	KINFO_FILE_VTYPE_VFIFO	7
    263  1.6  christos 
    264  1.6  christos /* Constants for socket address families.  These match AF_* constants
    265  1.6  christos    in <sys/socket.h>.  */
    266  1.6  christos 
    267  1.6  christos #define	FBSD_AF_UNIX		1
    268  1.6  christos #define	FBSD_AF_INET		2
    269  1.6  christos #define	FBSD_AF_INET6		28
    270  1.6  christos 
    271  1.6  christos /* Constants for socket types.  These match SOCK_* constants in
    272  1.6  christos    <sys/socket.h>.  */
    273  1.6  christos 
    274  1.6  christos #define	FBSD_SOCK_STREAM	1
    275  1.6  christos #define	FBSD_SOCK_DGRAM		2
    276  1.6  christos #define	FBSD_SOCK_SEQPACKET	5
    277  1.6  christos 
    278  1.6  christos /* Constants for IP protocols.  These match IPPROTO_* constants in
    279  1.6  christos    <netinet/in.h>.  */
    280  1.6  christos 
    281  1.6  christos #define	FBSD_IPPROTO_ICMP	1
    282  1.6  christos #define	FBSD_IPPROTO_TCP	6
    283  1.6  christos #define	FBSD_IPPROTO_UDP	17
    284  1.6  christos #define	FBSD_IPPROTO_SCTP	132
    285  1.6  christos 
    286  1.6  christos /* Socket address structures.  These have the same layout on all
    287  1.6  christos    FreeBSD architectures.  In addition, multibyte fields such as IP
    288  1.6  christos    addresses are always stored in network byte order.  */
    289  1.6  christos 
    290  1.6  christos struct fbsd_sockaddr_in
    291  1.6  christos {
    292  1.6  christos   uint8_t sin_len;
    293  1.6  christos   uint8_t sin_family;
    294  1.6  christos   uint8_t sin_port[2];
    295  1.6  christos   uint8_t sin_addr[4];
    296  1.6  christos   char sin_zero[8];
    297  1.6  christos };
    298  1.6  christos 
    299  1.6  christos struct fbsd_sockaddr_in6
    300  1.6  christos {
    301  1.6  christos   uint8_t sin6_len;
    302  1.6  christos   uint8_t sin6_family;
    303  1.6  christos   uint8_t sin6_port[2];
    304  1.6  christos   uint32_t sin6_flowinfo;
    305  1.6  christos   uint8_t sin6_addr[16];
    306  1.6  christos   uint32_t sin6_scope_id;
    307  1.6  christos };
    308  1.6  christos 
    309  1.6  christos struct fbsd_sockaddr_un
    310  1.6  christos {
    311  1.6  christos   uint8_t sun_len;
    312  1.6  christos   uint8_t sun_family;
    313  1.6  christos   char sun_path[104];
    314  1.6  christos };
    315  1.6  christos 
    316  1.6  christos /* Number of 32-bit words in a signal set.  This matches _SIG_WORDS in
    317  1.6  christos    <sys/_sigset.h> and is the same value on all architectures.  */
    318  1.6  christos 
    319  1.6  christos #define	SIG_WORDS		4
    320  1.6  christos 
    321  1.6  christos /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
    322  1.6  christos    dump notes.  See <sys/user.h> for the definition of struct
    323  1.6  christos    kinfo_proc.  This data structure has different layouts on different
    324  1.6  christos    architectures mostly due to ILP32 vs LP64.  However, FreeBSD/i386
    325  1.6  christos    uses a 32-bit time_t while all other architectures use a 64-bit
    326  1.6  christos    time_t.
    327  1.6  christos 
    328  1.6  christos    The core dump note actually contains one kinfo_proc structure for
    329  1.6  christos    each thread, but all of the process-wide data can be obtained from
    330  1.6  christos    the first structure.  One result of this note's format is that some
    331  1.6  christos    of the process-wide status available in the native target method
    332  1.6  christos    from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
    333  1.6  christos    is not available from a core dump.  Instead, the per-thread data
    334  1.6  christos    structures contain the value of these fields for individual
    335  1.6  christos    threads.  */
    336  1.6  christos 
    337  1.6  christos struct kinfo_proc_layout
    338  1.6  christos {
    339  1.6  christos   /* Offsets of struct kinfo_proc members.  */
    340  1.6  christos   int ki_layout;
    341  1.6  christos   int ki_pid;
    342  1.6  christos   int ki_ppid;
    343  1.6  christos   int ki_pgid;
    344  1.6  christos   int ki_tpgid;
    345  1.6  christos   int ki_sid;
    346  1.6  christos   int ki_tdev_freebsd11;
    347  1.6  christos   int ki_sigignore;
    348  1.6  christos   int ki_sigcatch;
    349  1.6  christos   int ki_uid;
    350  1.6  christos   int ki_ruid;
    351  1.6  christos   int ki_svuid;
    352  1.6  christos   int ki_rgid;
    353  1.6  christos   int ki_svgid;
    354  1.6  christos   int ki_ngroups;
    355  1.6  christos   int ki_groups;
    356  1.6  christos   int ki_size;
    357  1.6  christos   int ki_rssize;
    358  1.6  christos   int ki_tsize;
    359  1.6  christos   int ki_dsize;
    360  1.6  christos   int ki_ssize;
    361  1.6  christos   int ki_start;
    362  1.6  christos   int ki_nice;
    363  1.6  christos   int ki_comm;
    364  1.6  christos   int ki_tdev;
    365  1.6  christos   int ki_rusage;
    366  1.6  christos   int ki_rusage_ch;
    367  1.6  christos 
    368  1.6  christos   /* Offsets of struct rusage members.  */
    369  1.6  christos   int ru_utime;
    370  1.6  christos   int ru_stime;
    371  1.6  christos   int ru_maxrss;
    372  1.6  christos   int ru_minflt;
    373  1.6  christos   int ru_majflt;
    374  1.6  christos };
    375  1.6  christos 
    376  1.6  christos const struct kinfo_proc_layout kinfo_proc_layout_32 =
    377  1.6  christos   {
    378  1.6  christos     .ki_layout = 0x4,
    379  1.6  christos     .ki_pid = 0x28,
    380  1.6  christos     .ki_ppid = 0x2c,
    381  1.6  christos     .ki_pgid = 0x30,
    382  1.6  christos     .ki_tpgid = 0x34,
    383  1.6  christos     .ki_sid = 0x38,
    384  1.6  christos     .ki_tdev_freebsd11 = 0x44,
    385  1.6  christos     .ki_sigignore = 0x68,
    386  1.6  christos     .ki_sigcatch = 0x78,
    387  1.6  christos     .ki_uid = 0x88,
    388  1.6  christos     .ki_ruid = 0x8c,
    389  1.6  christos     .ki_svuid = 0x90,
    390  1.6  christos     .ki_rgid = 0x94,
    391  1.6  christos     .ki_svgid = 0x98,
    392  1.6  christos     .ki_ngroups = 0x9c,
    393  1.6  christos     .ki_groups = 0xa0,
    394  1.6  christos     .ki_size = 0xe0,
    395  1.6  christos     .ki_rssize = 0xe4,
    396  1.6  christos     .ki_tsize = 0xec,
    397  1.6  christos     .ki_dsize = 0xf0,
    398  1.6  christos     .ki_ssize = 0xf4,
    399  1.6  christos     .ki_start = 0x118,
    400  1.6  christos     .ki_nice = 0x145,
    401  1.6  christos     .ki_comm = 0x17f,
    402  1.6  christos     .ki_tdev = 0x1f0,
    403  1.6  christos     .ki_rusage = 0x220,
    404  1.6  christos     .ki_rusage_ch = 0x278,
    405  1.6  christos 
    406  1.6  christos     .ru_utime = 0x0,
    407  1.6  christos     .ru_stime = 0x10,
    408  1.6  christos     .ru_maxrss = 0x20,
    409  1.6  christos     .ru_minflt = 0x30,
    410  1.6  christos     .ru_majflt = 0x34,
    411  1.6  christos   };
    412  1.6  christos 
    413  1.6  christos const struct kinfo_proc_layout kinfo_proc_layout_i386 =
    414  1.6  christos   {
    415  1.6  christos     .ki_layout = 0x4,
    416  1.6  christos     .ki_pid = 0x28,
    417  1.6  christos     .ki_ppid = 0x2c,
    418  1.6  christos     .ki_pgid = 0x30,
    419  1.6  christos     .ki_tpgid = 0x34,
    420  1.6  christos     .ki_sid = 0x38,
    421  1.6  christos     .ki_tdev_freebsd11 = 0x44,
    422  1.6  christos     .ki_sigignore = 0x68,
    423  1.6  christos     .ki_sigcatch = 0x78,
    424  1.6  christos     .ki_uid = 0x88,
    425  1.6  christos     .ki_ruid = 0x8c,
    426  1.6  christos     .ki_svuid = 0x90,
    427  1.6  christos     .ki_rgid = 0x94,
    428  1.6  christos     .ki_svgid = 0x98,
    429  1.6  christos     .ki_ngroups = 0x9c,
    430  1.6  christos     .ki_groups = 0xa0,
    431  1.6  christos     .ki_size = 0xe0,
    432  1.6  christos     .ki_rssize = 0xe4,
    433  1.6  christos     .ki_tsize = 0xec,
    434  1.6  christos     .ki_dsize = 0xf0,
    435  1.6  christos     .ki_ssize = 0xf4,
    436  1.6  christos     .ki_start = 0x118,
    437  1.6  christos     .ki_nice = 0x135,
    438  1.6  christos     .ki_comm = 0x16f,
    439  1.6  christos     .ki_tdev = 0x1e0,
    440  1.6  christos     .ki_rusage = 0x210,
    441  1.6  christos     .ki_rusage_ch = 0x258,
    442  1.6  christos 
    443  1.6  christos     .ru_utime = 0x0,
    444  1.6  christos     .ru_stime = 0x8,
    445  1.6  christos     .ru_maxrss = 0x10,
    446  1.6  christos     .ru_minflt = 0x20,
    447  1.6  christos     .ru_majflt = 0x24,
    448  1.6  christos   };
    449  1.6  christos 
    450  1.6  christos const struct kinfo_proc_layout kinfo_proc_layout_64 =
    451  1.6  christos   {
    452  1.6  christos     .ki_layout = 0x4,
    453  1.6  christos     .ki_pid = 0x48,
    454  1.6  christos     .ki_ppid = 0x4c,
    455  1.6  christos     .ki_pgid = 0x50,
    456  1.6  christos     .ki_tpgid = 0x54,
    457  1.6  christos     .ki_sid = 0x58,
    458  1.6  christos     .ki_tdev_freebsd11 = 0x64,
    459  1.6  christos     .ki_sigignore = 0x88,
    460  1.6  christos     .ki_sigcatch = 0x98,
    461  1.6  christos     .ki_uid = 0xa8,
    462  1.6  christos     .ki_ruid = 0xac,
    463  1.6  christos     .ki_svuid = 0xb0,
    464  1.6  christos     .ki_rgid = 0xb4,
    465  1.6  christos     .ki_svgid = 0xb8,
    466  1.6  christos     .ki_ngroups = 0xbc,
    467  1.6  christos     .ki_groups = 0xc0,
    468  1.6  christos     .ki_size = 0x100,
    469  1.6  christos     .ki_rssize = 0x108,
    470  1.6  christos     .ki_tsize = 0x118,
    471  1.6  christos     .ki_dsize = 0x120,
    472  1.6  christos     .ki_ssize = 0x128,
    473  1.6  christos     .ki_start = 0x150,
    474  1.6  christos     .ki_nice = 0x185,
    475  1.6  christos     .ki_comm = 0x1bf,
    476  1.6  christos     .ki_tdev = 0x230,
    477  1.6  christos     .ki_rusage = 0x260,
    478  1.6  christos     .ki_rusage_ch = 0x2f0,
    479  1.6  christos 
    480  1.6  christos     .ru_utime = 0x0,
    481  1.6  christos     .ru_stime = 0x10,
    482  1.6  christos     .ru_maxrss = 0x20,
    483  1.6  christos     .ru_minflt = 0x40,
    484  1.6  christos     .ru_majflt = 0x48,
    485  1.6  christos   };
    486  1.6  christos 
    487  1.6  christos struct fbsd_gdbarch_data
    488  1.6  christos   {
    489  1.8  christos     struct type *siginfo_type = nullptr;
    490  1.6  christos   };
    491  1.6  christos 
    492  1.8  christos static const registry<gdbarch>::key<fbsd_gdbarch_data>
    493  1.8  christos      fbsd_gdbarch_data_handle;
    494  1.6  christos 
    495  1.6  christos static struct fbsd_gdbarch_data *
    496  1.6  christos get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
    497  1.6  christos {
    498  1.8  christos   struct fbsd_gdbarch_data *result = fbsd_gdbarch_data_handle.get (gdbarch);
    499  1.8  christos   if (result == nullptr)
    500  1.8  christos     result = fbsd_gdbarch_data_handle.emplace (gdbarch);
    501  1.8  christos   return result;
    502  1.6  christos }
    503  1.1  christos 
    504  1.7  christos struct fbsd_pspace_data
    505  1.7  christos {
    506  1.7  christos   /* Offsets in the runtime linker's 'Obj_Entry' structure.  */
    507  1.7  christos   LONGEST off_linkmap = 0;
    508  1.7  christos   LONGEST off_tlsindex = 0;
    509  1.7  christos   bool rtld_offsets_valid = false;
    510  1.8  christos 
    511  1.8  christos   /* vDSO mapping range.  */
    512  1.8  christos   struct mem_range vdso_range {};
    513  1.8  christos 
    514  1.8  christos   /* Zero if the range hasn't been searched for, > 0 if a range was
    515  1.8  christos      found, or < 0 if a range was not found.  */
    516  1.8  christos   int vdso_range_p = 0;
    517  1.7  christos };
    518  1.7  christos 
    519  1.7  christos /* Per-program-space data for FreeBSD architectures.  */
    520  1.8  christos static const registry<program_space>::key<fbsd_pspace_data>
    521  1.7  christos   fbsd_pspace_data_handle;
    522  1.7  christos 
    523  1.7  christos static struct fbsd_pspace_data *
    524  1.7  christos get_fbsd_pspace_data (struct program_space *pspace)
    525  1.7  christos {
    526  1.7  christos   struct fbsd_pspace_data *data;
    527  1.7  christos 
    528  1.7  christos   data = fbsd_pspace_data_handle.get (pspace);
    529  1.7  christos   if (data == NULL)
    530  1.7  christos     data = fbsd_pspace_data_handle.emplace (pspace);
    531  1.7  christos 
    532  1.7  christos   return data;
    533  1.7  christos }
    534  1.7  christos 
    535  1.4  christos /* This is how we want PTIDs from core files to be printed.  */
    536  1.4  christos 
    537  1.7  christos static std::string
    538  1.4  christos fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
    539  1.4  christos {
    540  1.6  christos   if (ptid.lwp () != 0)
    541  1.7  christos     return string_printf ("LWP %ld", ptid.lwp ());
    542  1.4  christos 
    543  1.4  christos   return normal_pid_to_str (ptid);
    544  1.4  christos }
    545  1.4  christos 
    546  1.4  christos /* Extract the name assigned to a thread from a core.  Returns the
    547  1.4  christos    string in a static buffer.  */
    548  1.4  christos 
    549  1.4  christos static const char *
    550  1.4  christos fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
    551  1.4  christos {
    552  1.4  christos   static char buf[80];
    553  1.4  christos   struct bfd_section *section;
    554  1.4  christos   bfd_size_type size;
    555  1.4  christos 
    556  1.6  christos   if (thr->ptid.lwp () != 0)
    557  1.4  christos     {
    558  1.4  christos       /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
    559  1.4  christos 	 whose contents are defined by a "struct thrmisc" declared in
    560  1.4  christos 	 <sys/procfs.h> on FreeBSD.  The per-thread name is stored as
    561  1.4  christos 	 a null-terminated string as the first member of the
    562  1.4  christos 	 structure.  Rather than define the full structure here, just
    563  1.4  christos 	 extract the null-terminated name from the start of the
    564  1.4  christos 	 note.  */
    565  1.6  christos       thread_section_name section_name (".thrmisc", thr->ptid);
    566  1.9  christos       bfd *cbfd = current_program_space->core_bfd ();
    567  1.6  christos 
    568  1.9  christos       section = bfd_get_section_by_name (cbfd, section_name.c_str ());
    569  1.7  christos       if (section != NULL && bfd_section_size (section) > 0)
    570  1.4  christos 	{
    571  1.4  christos 	  /* Truncate the name if it is longer than "buf".  */
    572  1.7  christos 	  size = bfd_section_size (section);
    573  1.4  christos 	  if (size > sizeof buf - 1)
    574  1.4  christos 	    size = sizeof buf - 1;
    575  1.9  christos 	  if (bfd_get_section_contents (cbfd, section, buf, (file_ptr) 0, size)
    576  1.4  christos 	      && buf[0] != '\0')
    577  1.4  christos 	    {
    578  1.4  christos 	      buf[size] = '\0';
    579  1.4  christos 
    580  1.4  christos 	      /* Note that each thread will report the process command
    581  1.4  christos 		 as its thread name instead of an empty name if a name
    582  1.4  christos 		 has not been set explicitly.  Return a NULL name in
    583  1.4  christos 		 that case.  */
    584  1.9  christos 	      if (strcmp (buf, elf_tdata (cbfd)->core->program) != 0)
    585  1.4  christos 		return buf;
    586  1.4  christos 	    }
    587  1.4  christos 	}
    588  1.4  christos     }
    589  1.4  christos 
    590  1.4  christos   return NULL;
    591  1.4  christos }
    592  1.4  christos 
    593  1.6  christos /* Implement the "core_xfer_siginfo" gdbarch method.  */
    594  1.6  christos 
    595  1.6  christos static LONGEST
    596  1.6  christos fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
    597  1.6  christos 			ULONGEST offset, ULONGEST len)
    598  1.6  christos {
    599  1.6  christos   size_t siginfo_size;
    600  1.6  christos 
    601  1.6  christos   if (gdbarch_long_bit (gdbarch) == 32)
    602  1.6  christos     siginfo_size = SIZE32_SIGINFO_T;
    603  1.6  christos   else
    604  1.6  christos     siginfo_size = SIZE64_SIGINFO_T;
    605  1.6  christos   if (offset > siginfo_size)
    606  1.6  christos     return -1;
    607  1.6  christos 
    608  1.6  christos   thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
    609  1.9  christos   bfd *cbfd = current_program_space->core_bfd ();
    610  1.9  christos   asection *section = bfd_get_section_by_name (cbfd, section_name.c_str ());
    611  1.6  christos   if (section == NULL)
    612  1.6  christos     return -1;
    613  1.6  christos 
    614  1.6  christos   gdb_byte buf[4];
    615  1.9  christos   if (!bfd_get_section_contents (cbfd, section, buf,
    616  1.6  christos 				 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
    617  1.6  christos     return -1;
    618  1.6  christos 
    619  1.8  christos   int pl_flags = extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
    620  1.6  christos   if (!(pl_flags & PL_FLAG_SI))
    621  1.6  christos     return -1;
    622  1.6  christos 
    623  1.6  christos   if (offset + len > siginfo_size)
    624  1.6  christos     len = siginfo_size - offset;
    625  1.6  christos 
    626  1.6  christos   ULONGEST siginfo_offset;
    627  1.6  christos   if (gdbarch_long_bit (gdbarch) == 32)
    628  1.6  christos     siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
    629  1.6  christos   else
    630  1.6  christos     siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
    631  1.6  christos 
    632  1.9  christos   if (!bfd_get_section_contents (cbfd, section, readbuf,
    633  1.6  christos 				 siginfo_offset + offset, len))
    634  1.6  christos     return -1;
    635  1.6  christos 
    636  1.6  christos   return len;
    637  1.6  christos }
    638  1.6  christos 
    639  1.1  christos static int
    640  1.1  christos find_signalled_thread (struct thread_info *info, void *data)
    641  1.1  christos {
    642  1.8  christos   if (info->stop_signal () != GDB_SIGNAL_0
    643  1.6  christos       && info->ptid.pid () == inferior_ptid.pid ())
    644  1.1  christos     return 1;
    645  1.1  christos 
    646  1.1  christos   return 0;
    647  1.1  christos }
    648  1.1  christos 
    649  1.6  christos /* Return a byte_vector containing the contents of a core dump note
    650  1.6  christos    for the target object of type OBJECT.  If STRUCTSIZE is non-zero,
    651  1.6  christos    the data is prefixed with a 32-bit integer size to match the format
    652  1.6  christos    used in FreeBSD NT_PROCSTAT_* notes.  */
    653  1.6  christos 
    654  1.9  christos static std::optional<gdb::byte_vector>
    655  1.6  christos fbsd_make_note_desc (enum target_object object, uint32_t structsize)
    656  1.6  christos {
    657  1.9  christos   std::optional<gdb::byte_vector> buf =
    658  1.8  christos     target_read_alloc (current_inferior ()->top_target (), object, NULL);
    659  1.6  christos   if (!buf || buf->empty ())
    660  1.6  christos     return {};
    661  1.6  christos 
    662  1.6  christos   if (structsize == 0)
    663  1.6  christos     return buf;
    664  1.6  christos 
    665  1.6  christos   gdb::byte_vector desc (sizeof (structsize) + buf->size ());
    666  1.6  christos   memcpy (desc.data (), &structsize, sizeof (structsize));
    667  1.9  christos   std::copy (buf->begin (), buf->end (), desc.data () + sizeof (structsize));
    668  1.6  christos   return desc;
    669  1.6  christos }
    670  1.6  christos 
    671  1.1  christos /* Create appropriate note sections for a corefile, returning them in
    672  1.1  christos    allocated memory.  */
    673  1.1  christos 
    674  1.8  christos static gdb::unique_xmalloc_ptr<char>
    675  1.1  christos fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
    676  1.1  christos {
    677  1.8  christos   gdb::unique_xmalloc_ptr<char> note_data;
    678  1.1  christos   Elf_Internal_Ehdr *i_ehdrp;
    679  1.6  christos   struct thread_info *curr_thr, *signalled_thr;
    680  1.1  christos 
    681  1.1  christos   /* Put a "FreeBSD" label in the ELF header.  */
    682  1.1  christos   i_ehdrp = elf_elfheader (obfd);
    683  1.1  christos   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
    684  1.1  christos 
    685  1.1  christos   gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
    686  1.1  christos 
    687  1.1  christos   if (get_exec_file (0))
    688  1.1  christos     {
    689  1.1  christos       const char *fname = lbasename (get_exec_file (0));
    690  1.7  christos       std::string psargs = fname;
    691  1.1  christos 
    692  1.8  christos       const std::string &infargs = current_inferior ()->args ();
    693  1.8  christos       if (!infargs.empty ())
    694  1.8  christos 	psargs += ' ' + infargs;
    695  1.8  christos 
    696  1.8  christos       note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
    697  1.8  christos 					       note_size, fname,
    698  1.8  christos 					       psargs.c_str ()));
    699  1.1  christos     }
    700  1.1  christos 
    701  1.4  christos   /* Thread register information.  */
    702  1.7  christos   try
    703  1.4  christos     {
    704  1.4  christos       update_thread_list ();
    705  1.4  christos     }
    706  1.7  christos   catch (const gdb_exception_error &e)
    707  1.4  christos     {
    708  1.4  christos       exception_print (gdb_stderr, e);
    709  1.4  christos     }
    710  1.4  christos 
    711  1.4  christos   /* Like the kernel, prefer dumping the signalled thread first.
    712  1.4  christos      "First thread" is what tools use to infer the signalled thread.
    713  1.4  christos      In case there's more than one signalled thread, prefer the
    714  1.4  christos      current thread, if it is signalled.  */
    715  1.4  christos   curr_thr = inferior_thread ();
    716  1.8  christos   if (curr_thr->stop_signal () != GDB_SIGNAL_0)
    717  1.4  christos     signalled_thr = curr_thr;
    718  1.4  christos   else
    719  1.4  christos     {
    720  1.4  christos       signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
    721  1.4  christos       if (signalled_thr == NULL)
    722  1.4  christos 	signalled_thr = curr_thr;
    723  1.4  christos     }
    724  1.4  christos 
    725  1.8  christos   enum gdb_signal stop_signal = signalled_thr->stop_signal ();
    726  1.8  christos   gcore_elf_build_thread_register_notes (gdbarch, signalled_thr, stop_signal,
    727  1.8  christos 					 obfd, &note_data, note_size);
    728  1.6  christos   for (thread_info *thr : current_inferior ()->non_exited_threads ())
    729  1.4  christos     {
    730  1.4  christos       if (thr == signalled_thr)
    731  1.4  christos 	continue;
    732  1.4  christos 
    733  1.8  christos       gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal,
    734  1.8  christos 					     obfd, &note_data, note_size);
    735  1.4  christos     }
    736  1.4  christos 
    737  1.6  christos   /* Auxiliary vector.  */
    738  1.6  christos   uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo  */
    739  1.9  christos   std::optional<gdb::byte_vector> note_desc =
    740  1.6  christos     fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
    741  1.6  christos   if (note_desc && !note_desc->empty ())
    742  1.6  christos     {
    743  1.8  christos       note_data.reset (elfcore_write_note (obfd, note_data.release (),
    744  1.8  christos 					   note_size, "FreeBSD",
    745  1.8  christos 					   NT_FREEBSD_PROCSTAT_AUXV,
    746  1.8  christos 					   note_desc->data (),
    747  1.8  christos 					   note_desc->size ()));
    748  1.6  christos       if (!note_data)
    749  1.6  christos 	return NULL;
    750  1.6  christos     }
    751  1.6  christos 
    752  1.6  christos   /* Virtual memory mappings.  */
    753  1.6  christos   note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
    754  1.6  christos   if (note_desc && !note_desc->empty ())
    755  1.6  christos     {
    756  1.8  christos       note_data.reset (elfcore_write_note (obfd, note_data.release (),
    757  1.8  christos 					   note_size, "FreeBSD",
    758  1.8  christos 					   NT_FREEBSD_PROCSTAT_VMMAP,
    759  1.8  christos 					   note_desc->data (),
    760  1.8  christos 					   note_desc->size ()));
    761  1.6  christos       if (!note_data)
    762  1.6  christos 	return NULL;
    763  1.6  christos     }
    764  1.6  christos 
    765  1.6  christos   note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
    766  1.6  christos   if (note_desc && !note_desc->empty ())
    767  1.6  christos     {
    768  1.8  christos       note_data.reset (elfcore_write_note (obfd, note_data.release (),
    769  1.8  christos 					   note_size, "FreeBSD",
    770  1.8  christos 					   NT_FREEBSD_PROCSTAT_PSSTRINGS,
    771  1.8  christos 					   note_desc->data (),
    772  1.8  christos 					   note_desc->size ()));
    773  1.6  christos       if (!note_data)
    774  1.6  christos 	return NULL;
    775  1.6  christos     }
    776  1.6  christos 
    777  1.9  christos   /* Include the target description when possible.  Some architectures
    778  1.9  christos      allow for per-thread gdbarch so we should really be emitting a tdesc
    779  1.9  christos      per-thread, however, we don't currently support reading in a
    780  1.9  christos      per-thread tdesc, so just emit the tdesc for the signalled thread.  */
    781  1.9  christos   gdbarch = target_thread_architecture (signalled_thr->ptid);
    782  1.9  christos   gcore_elf_make_tdesc_note (gdbarch, obfd, &note_data, note_size);
    783  1.8  christos 
    784  1.1  christos   return note_data;
    785  1.1  christos }
    786  1.1  christos 
    787  1.6  christos /* Helper function to generate the file descriptor description for a
    788  1.6  christos    single open file in 'info proc files'.  */
    789  1.6  christos 
    790  1.6  christos static const char *
    791  1.6  christos fbsd_file_fd (int kf_fd)
    792  1.6  christos {
    793  1.6  christos   switch (kf_fd)
    794  1.6  christos     {
    795  1.6  christos     case KINFO_FILE_FD_TYPE_CWD:
    796  1.6  christos       return "cwd";
    797  1.6  christos     case KINFO_FILE_FD_TYPE_ROOT:
    798  1.6  christos       return "root";
    799  1.6  christos     case KINFO_FILE_FD_TYPE_JAIL:
    800  1.6  christos       return "jail";
    801  1.6  christos     case KINFO_FILE_FD_TYPE_TRACE:
    802  1.6  christos       return "trace";
    803  1.6  christos     case KINFO_FILE_FD_TYPE_TEXT:
    804  1.6  christos       return "text";
    805  1.6  christos     case KINFO_FILE_FD_TYPE_CTTY:
    806  1.6  christos       return "ctty";
    807  1.6  christos     default:
    808  1.6  christos       return int_string (kf_fd, 10, 1, 0, 0);
    809  1.6  christos     }
    810  1.6  christos }
    811  1.6  christos 
    812  1.6  christos /* Helper function to generate the file type for a single open file in
    813  1.6  christos    'info proc files'.  */
    814  1.6  christos 
    815  1.6  christos static const char *
    816  1.6  christos fbsd_file_type (int kf_type, int kf_vnode_type)
    817  1.6  christos {
    818  1.6  christos   switch (kf_type)
    819  1.6  christos     {
    820  1.6  christos     case KINFO_FILE_TYPE_VNODE:
    821  1.6  christos       switch (kf_vnode_type)
    822  1.6  christos 	{
    823  1.6  christos 	case KINFO_FILE_VTYPE_VREG:
    824  1.6  christos 	  return "file";
    825  1.6  christos 	case KINFO_FILE_VTYPE_VDIR:
    826  1.6  christos 	  return "dir";
    827  1.6  christos 	case KINFO_FILE_VTYPE_VCHR:
    828  1.6  christos 	  return "chr";
    829  1.6  christos 	case KINFO_FILE_VTYPE_VLNK:
    830  1.6  christos 	  return "link";
    831  1.6  christos 	case KINFO_FILE_VTYPE_VSOCK:
    832  1.6  christos 	  return "socket";
    833  1.6  christos 	case KINFO_FILE_VTYPE_VFIFO:
    834  1.6  christos 	  return "fifo";
    835  1.6  christos 	default:
    836  1.6  christos 	  {
    837  1.6  christos 	    char *str = get_print_cell ();
    838  1.6  christos 
    839  1.6  christos 	    xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
    840  1.6  christos 	    return str;
    841  1.6  christos 	  }
    842  1.6  christos 	}
    843  1.6  christos     case KINFO_FILE_TYPE_SOCKET:
    844  1.6  christos       return "socket";
    845  1.6  christos     case KINFO_FILE_TYPE_PIPE:
    846  1.6  christos       return "pipe";
    847  1.6  christos     case KINFO_FILE_TYPE_FIFO:
    848  1.6  christos       return "fifo";
    849  1.6  christos     case KINFO_FILE_TYPE_KQUEUE:
    850  1.6  christos       return "kqueue";
    851  1.6  christos     case KINFO_FILE_TYPE_CRYPTO:
    852  1.6  christos       return "crypto";
    853  1.6  christos     case KINFO_FILE_TYPE_MQUEUE:
    854  1.6  christos       return "mqueue";
    855  1.6  christos     case KINFO_FILE_TYPE_SHM:
    856  1.6  christos       return "shm";
    857  1.6  christos     case KINFO_FILE_TYPE_SEM:
    858  1.6  christos       return "sem";
    859  1.6  christos     case KINFO_FILE_TYPE_PTS:
    860  1.6  christos       return "pts";
    861  1.6  christos     case KINFO_FILE_TYPE_PROCDESC:
    862  1.6  christos       return "proc";
    863  1.6  christos     default:
    864  1.6  christos       return int_string (kf_type, 10, 1, 0, 0);
    865  1.6  christos     }
    866  1.6  christos }
    867  1.6  christos 
    868  1.6  christos /* Helper function to generate the file flags for a single open file in
    869  1.6  christos    'info proc files'.  */
    870  1.6  christos 
    871  1.6  christos static const char *
    872  1.6  christos fbsd_file_flags (int kf_flags)
    873  1.6  christos {
    874  1.6  christos   static char file_flags[10];
    875  1.6  christos 
    876  1.6  christos   file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
    877  1.6  christos   file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
    878  1.6  christos   file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
    879  1.6  christos   file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
    880  1.6  christos   file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
    881  1.6  christos   file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
    882  1.6  christos   file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
    883  1.6  christos   file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
    884  1.6  christos   file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
    885  1.6  christos   file_flags[9] = '\0';
    886  1.6  christos 
    887  1.6  christos   return file_flags;
    888  1.6  christos }
    889  1.6  christos 
    890  1.6  christos /* Helper function to generate the name of an IP protocol.  */
    891  1.6  christos 
    892  1.6  christos static const char *
    893  1.6  christos fbsd_ipproto (int protocol)
    894  1.6  christos {
    895  1.6  christos   switch (protocol)
    896  1.6  christos     {
    897  1.6  christos     case FBSD_IPPROTO_ICMP:
    898  1.6  christos       return "icmp";
    899  1.6  christos     case FBSD_IPPROTO_TCP:
    900  1.6  christos       return "tcp";
    901  1.6  christos     case FBSD_IPPROTO_UDP:
    902  1.6  christos       return "udp";
    903  1.6  christos     case FBSD_IPPROTO_SCTP:
    904  1.6  christos       return "sctp";
    905  1.6  christos     default:
    906  1.6  christos       {
    907  1.6  christos 	char *str = get_print_cell ();
    908  1.6  christos 
    909  1.6  christos 	xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
    910  1.6  christos 	return str;
    911  1.6  christos       }
    912  1.6  christos     }
    913  1.6  christos }
    914  1.6  christos 
    915  1.6  christos /* Helper function to print out an IPv4 socket address.  */
    916  1.6  christos 
    917  1.6  christos static void
    918  1.6  christos fbsd_print_sockaddr_in (const void *sockaddr)
    919  1.6  christos {
    920  1.6  christos   const struct fbsd_sockaddr_in *sin =
    921  1.6  christos     reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
    922  1.6  christos   char buf[INET_ADDRSTRLEN];
    923  1.6  christos 
    924  1.6  christos   if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
    925  1.6  christos     error (_("Failed to format IPv4 address"));
    926  1.8  christos   gdb_printf ("%s:%u", buf,
    927  1.8  christos 	      (sin->sin_port[0] << 8) | sin->sin_port[1]);
    928  1.6  christos }
    929  1.6  christos 
    930  1.6  christos /* Helper function to print out an IPv6 socket address.  */
    931  1.6  christos 
    932  1.6  christos static void
    933  1.6  christos fbsd_print_sockaddr_in6 (const void *sockaddr)
    934  1.6  christos {
    935  1.6  christos   const struct fbsd_sockaddr_in6 *sin6 =
    936  1.6  christos     reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
    937  1.6  christos   char buf[INET6_ADDRSTRLEN];
    938  1.6  christos 
    939  1.6  christos   if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
    940  1.6  christos     error (_("Failed to format IPv6 address"));
    941  1.8  christos   gdb_printf ("%s.%u", buf,
    942  1.8  christos 	      (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
    943  1.6  christos }
    944  1.6  christos 
    945  1.6  christos /* See fbsd-tdep.h.  */
    946  1.6  christos 
    947  1.6  christos void
    948  1.6  christos fbsd_info_proc_files_header ()
    949  1.6  christos {
    950  1.8  christos   gdb_printf (_("Open files:\n\n"));
    951  1.8  christos   gdb_printf ("  %6s %6s %10s %9s %s\n",
    952  1.8  christos 	      "FD", "Type", "Offset", "Flags  ", "Name");
    953  1.6  christos }
    954  1.6  christos 
    955  1.6  christos /* See fbsd-tdep.h.  */
    956  1.6  christos 
    957  1.6  christos void
    958  1.6  christos fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
    959  1.6  christos 			    LONGEST kf_offset, int kf_vnode_type,
    960  1.6  christos 			    int kf_sock_domain, int kf_sock_type,
    961  1.6  christos 			    int kf_sock_protocol, const void *kf_sa_local,
    962  1.6  christos 			    const void *kf_sa_peer, const void *kf_path)
    963  1.6  christos {
    964  1.8  christos   gdb_printf ("  %6s %6s %10s %8s ",
    965  1.8  christos 	      fbsd_file_fd (kf_fd),
    966  1.8  christos 	      fbsd_file_type (kf_type, kf_vnode_type),
    967  1.8  christos 	      kf_offset > -1 ? hex_string (kf_offset) : "-",
    968  1.8  christos 	      fbsd_file_flags (kf_flags));
    969  1.6  christos   if (kf_type == KINFO_FILE_TYPE_SOCKET)
    970  1.6  christos     {
    971  1.6  christos       switch (kf_sock_domain)
    972  1.6  christos 	{
    973  1.6  christos 	case FBSD_AF_UNIX:
    974  1.6  christos 	  {
    975  1.6  christos 	    switch (kf_sock_type)
    976  1.6  christos 	      {
    977  1.6  christos 	      case FBSD_SOCK_STREAM:
    978  1.8  christos 		gdb_printf ("unix stream:");
    979  1.6  christos 		break;
    980  1.6  christos 	      case FBSD_SOCK_DGRAM:
    981  1.8  christos 		gdb_printf ("unix dgram:");
    982  1.6  christos 		break;
    983  1.6  christos 	      case FBSD_SOCK_SEQPACKET:
    984  1.8  christos 		gdb_printf ("unix seqpacket:");
    985  1.6  christos 		break;
    986  1.6  christos 	      default:
    987  1.8  christos 		gdb_printf ("unix <%d>:", kf_sock_type);
    988  1.6  christos 		break;
    989  1.6  christos 	      }
    990  1.6  christos 
    991  1.6  christos 	    /* For local sockets, print out the first non-nul path
    992  1.6  christos 	       rather than both paths.  */
    993  1.7  christos 	    const struct fbsd_sockaddr_un *saddr_un
    994  1.6  christos 	      = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
    995  1.7  christos 	    if (saddr_un->sun_path[0] == 0)
    996  1.7  christos 	      saddr_un = reinterpret_cast<const struct fbsd_sockaddr_un *>
    997  1.6  christos 		(kf_sa_peer);
    998  1.8  christos 	    gdb_printf ("%s", saddr_un->sun_path);
    999  1.6  christos 	    break;
   1000  1.6  christos 	  }
   1001  1.6  christos 	case FBSD_AF_INET:
   1002  1.8  christos 	  gdb_printf ("%s4 ", fbsd_ipproto (kf_sock_protocol));
   1003  1.6  christos 	  fbsd_print_sockaddr_in (kf_sa_local);
   1004  1.8  christos 	  gdb_printf (" -> ");
   1005  1.6  christos 	  fbsd_print_sockaddr_in (kf_sa_peer);
   1006  1.6  christos 	  break;
   1007  1.6  christos 	case FBSD_AF_INET6:
   1008  1.8  christos 	  gdb_printf ("%s6 ", fbsd_ipproto (kf_sock_protocol));
   1009  1.6  christos 	  fbsd_print_sockaddr_in6 (kf_sa_local);
   1010  1.8  christos 	  gdb_printf (" -> ");
   1011  1.6  christos 	  fbsd_print_sockaddr_in6 (kf_sa_peer);
   1012  1.6  christos 	  break;
   1013  1.6  christos 	}
   1014  1.6  christos     }
   1015  1.6  christos   else
   1016  1.8  christos     gdb_printf ("%s", reinterpret_cast<const char *> (kf_path));
   1017  1.8  christos   gdb_printf ("\n");
   1018  1.6  christos }
   1019  1.6  christos 
   1020  1.6  christos /* Implement "info proc files" for a corefile.  */
   1021  1.6  christos 
   1022  1.6  christos static void
   1023  1.6  christos fbsd_core_info_proc_files (struct gdbarch *gdbarch)
   1024  1.6  christos {
   1025  1.9  christos   bfd *cbfd = current_program_space->core_bfd ();
   1026  1.9  christos   asection *section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
   1027  1.6  christos   if (section == NULL)
   1028  1.6  christos     {
   1029  1.6  christos       warning (_("unable to find open files in core file"));
   1030  1.6  christos       return;
   1031  1.6  christos     }
   1032  1.6  christos 
   1033  1.7  christos   size_t note_size = bfd_section_size (section);
   1034  1.6  christos   if (note_size < 4)
   1035  1.6  christos     error (_("malformed core note - too short for header"));
   1036  1.6  christos 
   1037  1.6  christos   gdb::def_vector<unsigned char> contents (note_size);
   1038  1.9  christos   if (!bfd_get_section_contents (cbfd, section, contents.data (),
   1039  1.6  christos 				 0, note_size))
   1040  1.6  christos     error (_("could not get core note contents"));
   1041  1.6  christos 
   1042  1.6  christos   unsigned char *descdata = contents.data ();
   1043  1.6  christos   unsigned char *descend = descdata + note_size;
   1044  1.6  christos 
   1045  1.6  christos   /* Skip over the structure size.  */
   1046  1.6  christos   descdata += 4;
   1047  1.6  christos 
   1048  1.6  christos   fbsd_info_proc_files_header ();
   1049  1.6  christos 
   1050  1.6  christos   while (descdata + KF_PATH < descend)
   1051  1.6  christos     {
   1052  1.9  christos       ULONGEST structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
   1053  1.6  christos       if (structsize < KF_PATH)
   1054  1.6  christos 	error (_("malformed core note - file structure too small"));
   1055  1.6  christos 
   1056  1.9  christos       LONGEST type = bfd_get_signed_32 (cbfd, descdata + KF_TYPE);
   1057  1.9  christos       LONGEST fd = bfd_get_signed_32 (cbfd, descdata + KF_FD);
   1058  1.9  christos       LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KF_FLAGS);
   1059  1.9  christos       LONGEST offset = bfd_get_signed_64 (cbfd, descdata + KF_OFFSET);
   1060  1.9  christos       LONGEST vnode_type = bfd_get_signed_32 (cbfd, descdata + KF_VNODE_TYPE);
   1061  1.9  christos       LONGEST sock_domain = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_DOMAIN);
   1062  1.9  christos       LONGEST sock_type = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_TYPE);
   1063  1.9  christos       LONGEST sock_protocol = bfd_get_signed_32 (cbfd,
   1064  1.6  christos 						 descdata + KF_SOCK_PROTOCOL);
   1065  1.6  christos       fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
   1066  1.6  christos 				  sock_domain, sock_type, sock_protocol,
   1067  1.6  christos 				  descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
   1068  1.6  christos 				  descdata + KF_PATH);
   1069  1.6  christos 
   1070  1.6  christos       descdata += structsize;
   1071  1.6  christos     }
   1072  1.6  christos }
   1073  1.6  christos 
   1074  1.6  christos /* Helper function to generate mappings flags for a single VM map
   1075  1.6  christos    entry in 'info proc mappings'.  */
   1076  1.6  christos 
   1077  1.6  christos static const char *
   1078  1.6  christos fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
   1079  1.6  christos {
   1080  1.6  christos   static char vm_flags[9];
   1081  1.6  christos 
   1082  1.6  christos   vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
   1083  1.6  christos   vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
   1084  1.6  christos   vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
   1085  1.6  christos   vm_flags[3] = ' ';
   1086  1.6  christos   vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
   1087  1.6  christos   vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
   1088  1.6  christos   vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
   1089  1.6  christos   vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
   1090  1.6  christos     : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
   1091  1.6  christos   vm_flags[8] = '\0';
   1092  1.6  christos 
   1093  1.6  christos   return vm_flags;
   1094  1.6  christos }
   1095  1.6  christos 
   1096  1.6  christos /* See fbsd-tdep.h.  */
   1097  1.6  christos 
   1098  1.6  christos void
   1099  1.6  christos fbsd_info_proc_mappings_header (int addr_bit)
   1100  1.6  christos {
   1101  1.8  christos   gdb_printf (_("Mapped address spaces:\n\n"));
   1102  1.6  christos   if (addr_bit == 64)
   1103  1.6  christos     {
   1104  1.8  christos       gdb_printf ("  %18s %18s %10s %10s %9s %s\n",
   1105  1.8  christos 		  "Start Addr",
   1106  1.8  christos 		  "  End Addr",
   1107  1.8  christos 		  "      Size", "    Offset", "Flags  ", "File");
   1108  1.6  christos     }
   1109  1.6  christos   else
   1110  1.6  christos     {
   1111  1.8  christos       gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
   1112  1.8  christos 		  "Start Addr",
   1113  1.8  christos 		  "  End Addr",
   1114  1.8  christos 		  "      Size", "    Offset", "Flags  ", "File");
   1115  1.6  christos     }
   1116  1.6  christos }
   1117  1.6  christos 
   1118  1.6  christos /* See fbsd-tdep.h.  */
   1119  1.6  christos 
   1120  1.6  christos void
   1121  1.6  christos fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
   1122  1.6  christos 			       ULONGEST kve_end, ULONGEST kve_offset,
   1123  1.6  christos 			       int kve_flags, int kve_protection,
   1124  1.6  christos 			       const void *kve_path)
   1125  1.6  christos {
   1126  1.6  christos   if (addr_bit == 64)
   1127  1.6  christos     {
   1128  1.8  christos       gdb_printf ("  %18s %18s %10s %10s %9s %s\n",
   1129  1.8  christos 		  hex_string (kve_start),
   1130  1.8  christos 		  hex_string (kve_end),
   1131  1.8  christos 		  hex_string (kve_end - kve_start),
   1132  1.8  christos 		  hex_string (kve_offset),
   1133  1.8  christos 		  fbsd_vm_map_entry_flags (kve_flags, kve_protection),
   1134  1.8  christos 		  reinterpret_cast<const char *> (kve_path));
   1135  1.6  christos     }
   1136  1.6  christos   else
   1137  1.6  christos     {
   1138  1.8  christos       gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
   1139  1.8  christos 		  hex_string (kve_start),
   1140  1.8  christos 		  hex_string (kve_end),
   1141  1.8  christos 		  hex_string (kve_end - kve_start),
   1142  1.8  christos 		  hex_string (kve_offset),
   1143  1.8  christos 		  fbsd_vm_map_entry_flags (kve_flags, kve_protection),
   1144  1.8  christos 		  reinterpret_cast<const char *> (kve_path));
   1145  1.6  christos     }
   1146  1.6  christos }
   1147  1.6  christos 
   1148  1.6  christos /* Implement "info proc mappings" for a corefile.  */
   1149  1.6  christos 
   1150  1.6  christos static void
   1151  1.6  christos fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
   1152  1.6  christos {
   1153  1.6  christos   asection *section;
   1154  1.6  christos   unsigned char *descdata, *descend;
   1155  1.6  christos   size_t note_size;
   1156  1.9  christos   bfd *cbfd = current_program_space->core_bfd ();
   1157  1.6  christos 
   1158  1.9  christos   section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.vmmap");
   1159  1.6  christos   if (section == NULL)
   1160  1.6  christos     {
   1161  1.6  christos       warning (_("unable to find mappings in core file"));
   1162  1.6  christos       return;
   1163  1.6  christos     }
   1164  1.6  christos 
   1165  1.7  christos   note_size = bfd_section_size (section);
   1166  1.6  christos   if (note_size < 4)
   1167  1.6  christos     error (_("malformed core note - too short for header"));
   1168  1.6  christos 
   1169  1.6  christos   gdb::def_vector<unsigned char> contents (note_size);
   1170  1.9  christos   if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
   1171  1.6  christos     error (_("could not get core note contents"));
   1172  1.6  christos 
   1173  1.6  christos   descdata = contents.data ();
   1174  1.6  christos   descend = descdata + note_size;
   1175  1.6  christos 
   1176  1.6  christos   /* Skip over the structure size.  */
   1177  1.6  christos   descdata += 4;
   1178  1.6  christos 
   1179  1.6  christos   fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
   1180  1.6  christos   while (descdata + KVE_PATH < descend)
   1181  1.6  christos     {
   1182  1.9  christos       ULONGEST structsize = bfd_get_32 (cbfd, descdata + KVE_STRUCTSIZE);
   1183  1.6  christos       if (structsize < KVE_PATH)
   1184  1.6  christos 	error (_("malformed core note - vmmap entry too small"));
   1185  1.6  christos 
   1186  1.9  christos       ULONGEST start = bfd_get_64 (cbfd, descdata + KVE_START);
   1187  1.9  christos       ULONGEST end = bfd_get_64 (cbfd, descdata + KVE_END);
   1188  1.9  christos       ULONGEST offset = bfd_get_64 (cbfd, descdata + KVE_OFFSET);
   1189  1.9  christos       LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KVE_FLAGS);
   1190  1.9  christos       LONGEST prot = bfd_get_signed_32 (cbfd, descdata + KVE_PROTECTION);
   1191  1.6  christos       fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
   1192  1.6  christos 				     offset, flags, prot, descdata + KVE_PATH);
   1193  1.6  christos 
   1194  1.6  christos       descdata += structsize;
   1195  1.6  christos     }
   1196  1.6  christos }
   1197  1.6  christos 
   1198  1.6  christos /* Fetch the pathname of a vnode for a single file descriptor from the
   1199  1.6  christos    file table core note.  */
   1200  1.6  christos 
   1201  1.6  christos static gdb::unique_xmalloc_ptr<char>
   1202  1.6  christos fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
   1203  1.6  christos {
   1204  1.6  christos   asection *section;
   1205  1.6  christos   unsigned char *descdata, *descend;
   1206  1.6  christos   size_t note_size;
   1207  1.9  christos   bfd *cbfd = current_program_space->core_bfd ();
   1208  1.6  christos 
   1209  1.9  christos   section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
   1210  1.6  christos   if (section == NULL)
   1211  1.6  christos     return nullptr;
   1212  1.6  christos 
   1213  1.7  christos   note_size = bfd_section_size (section);
   1214  1.6  christos   if (note_size < 4)
   1215  1.6  christos     error (_("malformed core note - too short for header"));
   1216  1.6  christos 
   1217  1.6  christos   gdb::def_vector<unsigned char> contents (note_size);
   1218  1.9  christos   if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
   1219  1.6  christos     error (_("could not get core note contents"));
   1220  1.6  christos 
   1221  1.6  christos   descdata = contents.data ();
   1222  1.6  christos   descend = descdata + note_size;
   1223  1.6  christos 
   1224  1.6  christos   /* Skip over the structure size.  */
   1225  1.6  christos   descdata += 4;
   1226  1.6  christos 
   1227  1.6  christos   while (descdata + KF_PATH < descend)
   1228  1.6  christos     {
   1229  1.6  christos       ULONGEST structsize;
   1230  1.6  christos 
   1231  1.9  christos       structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
   1232  1.6  christos       if (structsize < KF_PATH)
   1233  1.6  christos 	error (_("malformed core note - file structure too small"));
   1234  1.6  christos 
   1235  1.9  christos       if (bfd_get_32 (cbfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
   1236  1.9  christos 	  && bfd_get_signed_32 (cbfd, descdata + KF_FD) == fd)
   1237  1.6  christos 	{
   1238  1.6  christos 	  char *path = (char *) descdata + KF_PATH;
   1239  1.7  christos 	  return make_unique_xstrdup (path);
   1240  1.6  christos 	}
   1241  1.6  christos 
   1242  1.6  christos       descdata += structsize;
   1243  1.6  christos     }
   1244  1.6  christos   return nullptr;
   1245  1.6  christos }
   1246  1.6  christos 
   1247  1.6  christos /* Helper function to read a struct timeval.  */
   1248  1.6  christos 
   1249  1.6  christos static void
   1250  1.6  christos fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
   1251  1.6  christos 			 LONGEST &sec, ULONGEST &usec)
   1252  1.6  christos {
   1253  1.9  christos   bfd *cbfd = current_program_space->core_bfd ();
   1254  1.9  christos 
   1255  1.6  christos   if (gdbarch_addr_bit (gdbarch) == 64)
   1256  1.6  christos     {
   1257  1.9  christos       sec = bfd_get_signed_64 (cbfd, data);
   1258  1.9  christos       usec = bfd_get_64 (cbfd, data + 8);
   1259  1.6  christos     }
   1260  1.9  christos   else if (bfd_get_arch (cbfd) == bfd_arch_i386)
   1261  1.6  christos     {
   1262  1.9  christos       sec = bfd_get_signed_32 (cbfd, data);
   1263  1.9  christos       usec = bfd_get_32 (cbfd, data + 4);
   1264  1.6  christos     }
   1265  1.6  christos   else
   1266  1.6  christos     {
   1267  1.9  christos       sec = bfd_get_signed_64 (cbfd, data);
   1268  1.9  christos       usec = bfd_get_32 (cbfd, data + 8);
   1269  1.6  christos     }
   1270  1.6  christos }
   1271  1.6  christos 
   1272  1.6  christos /* Print out the contents of a signal set.  */
   1273  1.6  christos 
   1274  1.6  christos static void
   1275  1.6  christos fbsd_print_sigset (const char *descr, unsigned char *sigset)
   1276  1.6  christos {
   1277  1.9  christos   bfd *cbfd = current_program_space->core_bfd ();
   1278  1.8  christos   gdb_printf ("%s: ", descr);
   1279  1.6  christos   for (int i = 0; i < SIG_WORDS; i++)
   1280  1.8  christos     gdb_printf ("%08x ",
   1281  1.9  christos 		(unsigned int) bfd_get_32 (cbfd, sigset + i * 4));
   1282  1.8  christos   gdb_printf ("\n");
   1283  1.6  christos }
   1284  1.6  christos 
   1285  1.6  christos /* Implement "info proc status" for a corefile.  */
   1286  1.6  christos 
   1287  1.6  christos static void
   1288  1.6  christos fbsd_core_info_proc_status (struct gdbarch *gdbarch)
   1289  1.6  christos {
   1290  1.6  christos   const struct kinfo_proc_layout *kp;
   1291  1.6  christos   asection *section;
   1292  1.6  christos   unsigned char *descdata;
   1293  1.6  christos   int addr_bit, long_bit;
   1294  1.6  christos   size_t note_size;
   1295  1.6  christos   ULONGEST value;
   1296  1.6  christos   LONGEST sec;
   1297  1.9  christos   bfd *cbfd = current_program_space->core_bfd ();
   1298  1.6  christos 
   1299  1.9  christos   section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.proc");
   1300  1.6  christos   if (section == NULL)
   1301  1.6  christos     {
   1302  1.6  christos       warning (_("unable to find process info in core file"));
   1303  1.6  christos       return;
   1304  1.6  christos     }
   1305  1.6  christos 
   1306  1.6  christos   addr_bit = gdbarch_addr_bit (gdbarch);
   1307  1.6  christos   if (addr_bit == 64)
   1308  1.6  christos     kp = &kinfo_proc_layout_64;
   1309  1.9  christos   else if (bfd_get_arch (cbfd) == bfd_arch_i386)
   1310  1.6  christos     kp = &kinfo_proc_layout_i386;
   1311  1.6  christos   else
   1312  1.6  christos     kp = &kinfo_proc_layout_32;
   1313  1.6  christos   long_bit = gdbarch_long_bit (gdbarch);
   1314  1.6  christos 
   1315  1.6  christos   /*
   1316  1.6  christos    * Ensure that the note is large enough for all of the fields fetched
   1317  1.6  christos    * by this function.  In particular, the note must contain the 32-bit
   1318  1.6  christos    * structure size, then it must be long enough to access the last
   1319  1.6  christos    * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
   1320  1.6  christos    */
   1321  1.7  christos   note_size = bfd_section_size (section);
   1322  1.6  christos   if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
   1323  1.6  christos 		   + long_bit / TARGET_CHAR_BIT))
   1324  1.6  christos     error (_("malformed core note - too short"));
   1325  1.6  christos 
   1326  1.6  christos   gdb::def_vector<unsigned char> contents (note_size);
   1327  1.9  christos   if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
   1328  1.6  christos     error (_("could not get core note contents"));
   1329  1.6  christos 
   1330  1.6  christos   descdata = contents.data ();
   1331  1.6  christos 
   1332  1.6  christos   /* Skip over the structure size.  */
   1333  1.6  christos   descdata += 4;
   1334  1.6  christos 
   1335  1.6  christos   /* Verify 'ki_layout' is 0.  */
   1336  1.9  christos   if (bfd_get_32 (cbfd, descdata + kp->ki_layout) != 0)
   1337  1.6  christos     {
   1338  1.6  christos       warning (_("unsupported process information in core file"));
   1339  1.6  christos       return;
   1340  1.6  christos     }
   1341  1.6  christos 
   1342  1.8  christos   gdb_printf ("Name: %.19s\n", descdata + kp->ki_comm);
   1343  1.8  christos   gdb_printf ("Process ID: %s\n",
   1344  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pid)));
   1345  1.8  christos   gdb_printf ("Parent process: %s\n",
   1346  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ppid)));
   1347  1.8  christos   gdb_printf ("Process group: %s\n",
   1348  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pgid)));
   1349  1.8  christos   gdb_printf ("Session id: %s\n",
   1350  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_sid)));
   1351  1.6  christos 
   1352  1.6  christos   /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'.  Older
   1353  1.6  christos      kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'.  In older
   1354  1.6  christos      kernels the 64-bit 'ki_tdev' field is in a reserved section of
   1355  1.6  christos      the structure that is cleared to zero.  Assume that a zero value
   1356  1.6  christos      in ki_tdev indicates a core dump from an older kernel and use the
   1357  1.6  christos      value in 'ki_tdev_freebsd11' instead.  */
   1358  1.9  christos   value = bfd_get_64 (cbfd, descdata + kp->ki_tdev);
   1359  1.6  christos   if (value == 0)
   1360  1.9  christos     value = bfd_get_32 (cbfd, descdata + kp->ki_tdev_freebsd11);
   1361  1.8  christos   gdb_printf ("TTY: %s\n", pulongest (value));
   1362  1.8  christos   gdb_printf ("TTY owner process group: %s\n",
   1363  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_tpgid)));
   1364  1.8  christos   gdb_printf ("User IDs (real, effective, saved): %s %s %s\n",
   1365  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ruid)),
   1366  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_uid)),
   1367  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svuid)));
   1368  1.8  christos   gdb_printf ("Group IDs (real, effective, saved): %s %s %s\n",
   1369  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_rgid)),
   1370  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_groups)),
   1371  1.9  christos 	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svgid)));
   1372  1.8  christos   gdb_printf ("Groups: ");
   1373  1.9  christos   uint16_t ngroups = bfd_get_16 (cbfd, descdata + kp->ki_ngroups);
   1374  1.6  christos   for (int i = 0; i < ngroups; i++)
   1375  1.8  christos     gdb_printf ("%s ",
   1376  1.9  christos 		pulongest (bfd_get_32 (cbfd,
   1377  1.8  christos 				       descdata + kp->ki_groups + i * 4)));
   1378  1.8  christos   gdb_printf ("\n");
   1379  1.9  christos   value = bfd_get (long_bit, cbfd,
   1380  1.6  christos 		   descdata + kp->ki_rusage + kp->ru_minflt);
   1381  1.8  christos   gdb_printf ("Minor faults (no memory page): %s\n", pulongest (value));
   1382  1.9  christos   value = bfd_get (long_bit, cbfd,
   1383  1.6  christos 		   descdata + kp->ki_rusage_ch + kp->ru_minflt);
   1384  1.8  christos   gdb_printf ("Minor faults, children: %s\n", pulongest (value));
   1385  1.9  christos   value = bfd_get (long_bit, cbfd,
   1386  1.6  christos 		   descdata + kp->ki_rusage + kp->ru_majflt);
   1387  1.8  christos   gdb_printf ("Major faults (memory page faults): %s\n",
   1388  1.8  christos 	      pulongest (value));
   1389  1.9  christos   value = bfd_get (long_bit, cbfd,
   1390  1.6  christos 		   descdata + kp->ki_rusage_ch + kp->ru_majflt);
   1391  1.8  christos   gdb_printf ("Major faults, children: %s\n", pulongest (value));
   1392  1.6  christos   fbsd_core_fetch_timeval (gdbarch,
   1393  1.6  christos 			   descdata + kp->ki_rusage + kp->ru_utime,
   1394  1.6  christos 			   sec, value);
   1395  1.8  christos   gdb_printf ("utime: %s.%06d\n", plongest (sec), (int) value);
   1396  1.6  christos   fbsd_core_fetch_timeval (gdbarch,
   1397  1.6  christos 			   descdata + kp->ki_rusage + kp->ru_stime,
   1398  1.6  christos 			   sec, value);
   1399  1.8  christos   gdb_printf ("stime: %s.%06d\n", plongest (sec), (int) value);
   1400  1.6  christos   fbsd_core_fetch_timeval (gdbarch,
   1401  1.6  christos 			   descdata + kp->ki_rusage_ch + kp->ru_utime,
   1402  1.6  christos 			   sec, value);
   1403  1.8  christos   gdb_printf ("utime, children: %s.%06d\n", plongest (sec), (int) value);
   1404  1.6  christos   fbsd_core_fetch_timeval (gdbarch,
   1405  1.6  christos 			   descdata + kp->ki_rusage_ch + kp->ru_stime,
   1406  1.6  christos 			   sec, value);
   1407  1.8  christos   gdb_printf ("stime, children: %s.%06d\n", plongest (sec), (int) value);
   1408  1.8  christos   gdb_printf ("'nice' value: %d\n",
   1409  1.8  christos 	      (int) bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
   1410  1.6  christos   fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
   1411  1.8  christos   gdb_printf ("Start time: %s.%06d\n", plongest (sec), (int) value);
   1412  1.8  christos   gdb_printf ("Virtual memory size: %s kB\n",
   1413  1.9  christos 	      pulongest (bfd_get (addr_bit, cbfd,
   1414  1.8  christos 				  descdata + kp->ki_size) / 1024));
   1415  1.8  christos   gdb_printf ("Data size: %s pages\n",
   1416  1.9  christos 	      pulongest (bfd_get (addr_bit, cbfd,
   1417  1.8  christos 				  descdata + kp->ki_dsize)));
   1418  1.8  christos   gdb_printf ("Stack size: %s pages\n",
   1419  1.9  christos 	      pulongest (bfd_get (addr_bit, cbfd,
   1420  1.8  christos 				  descdata + kp->ki_ssize)));
   1421  1.8  christos   gdb_printf ("Text size: %s pages\n",
   1422  1.9  christos 	      pulongest (bfd_get (addr_bit, cbfd,
   1423  1.8  christos 				  descdata + kp->ki_tsize)));
   1424  1.8  christos   gdb_printf ("Resident set size: %s pages\n",
   1425  1.9  christos 	      pulongest (bfd_get (addr_bit, cbfd,
   1426  1.8  christos 				  descdata + kp->ki_rssize)));
   1427  1.8  christos   gdb_printf ("Maximum RSS: %s pages\n",
   1428  1.9  christos 	      pulongest (bfd_get (long_bit, cbfd,
   1429  1.8  christos 				  descdata + kp->ki_rusage
   1430  1.8  christos 				  + kp->ru_maxrss)));
   1431  1.6  christos   fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
   1432  1.6  christos   fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
   1433  1.6  christos }
   1434  1.6  christos 
   1435  1.6  christos /* Implement the "core_info_proc" gdbarch method.  */
   1436  1.6  christos 
   1437  1.6  christos static void
   1438  1.6  christos fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
   1439  1.6  christos 		     enum info_proc_what what)
   1440  1.6  christos {
   1441  1.6  christos   bool do_cmdline = false;
   1442  1.6  christos   bool do_cwd = false;
   1443  1.6  christos   bool do_exe = false;
   1444  1.6  christos   bool do_files = false;
   1445  1.6  christos   bool do_mappings = false;
   1446  1.6  christos   bool do_status = false;
   1447  1.6  christos   int pid;
   1448  1.6  christos 
   1449  1.6  christos   switch (what)
   1450  1.6  christos     {
   1451  1.6  christos     case IP_MINIMAL:
   1452  1.6  christos       do_cmdline = true;
   1453  1.6  christos       do_cwd = true;
   1454  1.6  christos       do_exe = true;
   1455  1.6  christos       break;
   1456  1.6  christos     case IP_MAPPINGS:
   1457  1.6  christos       do_mappings = true;
   1458  1.6  christos       break;
   1459  1.6  christos     case IP_STATUS:
   1460  1.6  christos     case IP_STAT:
   1461  1.6  christos       do_status = true;
   1462  1.6  christos       break;
   1463  1.6  christos     case IP_CMDLINE:
   1464  1.6  christos       do_cmdline = true;
   1465  1.6  christos       break;
   1466  1.6  christos     case IP_EXE:
   1467  1.6  christos       do_exe = true;
   1468  1.6  christos       break;
   1469  1.6  christos     case IP_CWD:
   1470  1.6  christos       do_cwd = true;
   1471  1.6  christos       break;
   1472  1.6  christos     case IP_FILES:
   1473  1.6  christos       do_files = true;
   1474  1.6  christos       break;
   1475  1.6  christos     case IP_ALL:
   1476  1.6  christos       do_cmdline = true;
   1477  1.6  christos       do_cwd = true;
   1478  1.6  christos       do_exe = true;
   1479  1.6  christos       do_files = true;
   1480  1.6  christos       do_mappings = true;
   1481  1.6  christos       do_status = true;
   1482  1.6  christos       break;
   1483  1.6  christos     default:
   1484  1.6  christos       return;
   1485  1.6  christos     }
   1486  1.6  christos 
   1487  1.9  christos   bfd *cbfd = current_program_space->core_bfd ();
   1488  1.9  christos   pid = bfd_core_file_pid (cbfd);
   1489  1.6  christos   if (pid != 0)
   1490  1.8  christos     gdb_printf (_("process %d\n"), pid);
   1491  1.6  christos 
   1492  1.6  christos   if (do_cmdline)
   1493  1.6  christos     {
   1494  1.6  christos       const char *cmdline;
   1495  1.6  christos 
   1496  1.9  christos       cmdline = bfd_core_file_failing_command (cbfd);
   1497  1.6  christos       if (cmdline)
   1498  1.8  christos 	gdb_printf ("cmdline = '%s'\n", cmdline);
   1499  1.6  christos       else
   1500  1.6  christos 	warning (_("Command line unavailable"));
   1501  1.6  christos     }
   1502  1.6  christos   if (do_cwd)
   1503  1.6  christos     {
   1504  1.6  christos       gdb::unique_xmalloc_ptr<char> cwd =
   1505  1.6  christos 	fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
   1506  1.6  christos       if (cwd)
   1507  1.8  christos 	gdb_printf ("cwd = '%s'\n", cwd.get ());
   1508  1.6  christos       else
   1509  1.6  christos 	warning (_("unable to read current working directory"));
   1510  1.6  christos     }
   1511  1.6  christos   if (do_exe)
   1512  1.6  christos     {
   1513  1.6  christos       gdb::unique_xmalloc_ptr<char> exe =
   1514  1.6  christos 	fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
   1515  1.6  christos       if (exe)
   1516  1.8  christos 	gdb_printf ("exe = '%s'\n", exe.get ());
   1517  1.6  christos       else
   1518  1.6  christos 	warning (_("unable to read executable path name"));
   1519  1.6  christos     }
   1520  1.6  christos   if (do_files)
   1521  1.6  christos     fbsd_core_info_proc_files (gdbarch);
   1522  1.6  christos   if (do_mappings)
   1523  1.6  christos     fbsd_core_info_proc_mappings (gdbarch);
   1524  1.6  christos   if (do_status)
   1525  1.6  christos     fbsd_core_info_proc_status (gdbarch);
   1526  1.6  christos }
   1527  1.6  christos 
   1528  1.4  christos /* Print descriptions of FreeBSD-specific AUXV entries to FILE.  */
   1529  1.4  christos 
   1530  1.4  christos static void
   1531  1.4  christos fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
   1532  1.4  christos 		       CORE_ADDR type, CORE_ADDR val)
   1533  1.4  christos {
   1534  1.6  christos   const char *name = "???";
   1535  1.6  christos   const char *description = "";
   1536  1.6  christos   enum auxv_format format = AUXV_FORMAT_HEX;
   1537  1.4  christos 
   1538  1.4  christos   switch (type)
   1539  1.4  christos     {
   1540  1.6  christos     case AT_NULL:
   1541  1.6  christos     case AT_IGNORE:
   1542  1.6  christos     case AT_EXECFD:
   1543  1.6  christos     case AT_PHDR:
   1544  1.6  christos     case AT_PHENT:
   1545  1.6  christos     case AT_PHNUM:
   1546  1.6  christos     case AT_PAGESZ:
   1547  1.6  christos     case AT_BASE:
   1548  1.6  christos     case AT_FLAGS:
   1549  1.6  christos     case AT_ENTRY:
   1550  1.6  christos     case AT_NOTELF:
   1551  1.6  christos     case AT_UID:
   1552  1.6  christos     case AT_EUID:
   1553  1.6  christos     case AT_GID:
   1554  1.6  christos     case AT_EGID:
   1555  1.6  christos       default_print_auxv_entry (gdbarch, file, type, val);
   1556  1.6  christos       return;
   1557  1.4  christos #define _TAGNAME(tag) #tag
   1558  1.4  christos #define TAGNAME(tag) _TAGNAME(AT_##tag)
   1559  1.4  christos #define TAG(tag, text, kind) \
   1560  1.4  christos       case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
   1561  1.4  christos       TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
   1562  1.4  christos       TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
   1563  1.4  christos       TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
   1564  1.4  christos       TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
   1565  1.4  christos       TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
   1566  1.4  christos       TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
   1567  1.4  christos       TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
   1568  1.4  christos       TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
   1569  1.4  christos       TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
   1570  1.6  christos       TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
   1571  1.6  christos       TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
   1572  1.6  christos       TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
   1573  1.7  christos       TAG (BSDFLAGS, _("ELF BSD flags"), AUXV_FORMAT_HEX);
   1574  1.7  christos       TAG (ARGC, _("Argument count"), AUXV_FORMAT_DEC);
   1575  1.7  christos       TAG (ARGV, _("Argument vector"), AUXV_FORMAT_HEX);
   1576  1.7  christos       TAG (ENVC, _("Environment count"), AUXV_FORMAT_DEC);
   1577  1.7  christos       TAG (ENVV, _("Environment vector"), AUXV_FORMAT_HEX);
   1578  1.7  christos       TAG (PS_STRINGS, _("Pointer to ps_strings"), AUXV_FORMAT_HEX);
   1579  1.8  christos       TAG (FXRNG, _("Pointer to root RNG seed version"), AUXV_FORMAT_HEX);
   1580  1.8  christos       TAG (KPRELOAD, _("Base address of vDSO"), AUXV_FORMAT_HEX);
   1581  1.8  christos       TAG (USRSTACKBASE, _("Top of user stack"), AUXV_FORMAT_HEX);
   1582  1.8  christos       TAG (USRSTACKLIM, _("Grow limit of user stack"), AUXV_FORMAT_HEX);
   1583  1.4  christos     }
   1584  1.4  christos 
   1585  1.4  christos   fprint_auxv_entry (file, name, description, format, type, val);
   1586  1.4  christos }
   1587  1.4  christos 
   1588  1.6  christos /* Implement the "get_siginfo_type" gdbarch method.  */
   1589  1.6  christos 
   1590  1.6  christos static struct type *
   1591  1.6  christos fbsd_get_siginfo_type (struct gdbarch *gdbarch)
   1592  1.6  christos {
   1593  1.6  christos   struct fbsd_gdbarch_data *fbsd_gdbarch_data;
   1594  1.6  christos   struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
   1595  1.6  christos   struct type *uid_type, *pid_type;
   1596  1.6  christos   struct type *sigval_type, *reason_type;
   1597  1.6  christos   struct type *siginfo_type;
   1598  1.6  christos   struct type *type;
   1599  1.6  christos 
   1600  1.6  christos   fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
   1601  1.6  christos   if (fbsd_gdbarch_data->siginfo_type != NULL)
   1602  1.6  christos     return fbsd_gdbarch_data->siginfo_type;
   1603  1.6  christos 
   1604  1.9  christos   type_allocator alloc (gdbarch);
   1605  1.9  christos   int_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
   1606  1.6  christos 				0, "int");
   1607  1.9  christos   int32_type = init_integer_type (alloc, 32, 0, "int32_t");
   1608  1.9  christos   uint32_type = init_integer_type (alloc, 32, 1, "uint32_t");
   1609  1.9  christos   long_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
   1610  1.6  christos 				 0, "long");
   1611  1.6  christos   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
   1612  1.6  christos 
   1613  1.6  christos   /* union sigval */
   1614  1.6  christos   sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
   1615  1.7  christos   sigval_type->set_name (xstrdup ("sigval"));
   1616  1.6  christos   append_composite_type_field (sigval_type, "sival_int", int_type);
   1617  1.6  christos   append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
   1618  1.6  christos 
   1619  1.6  christos   /* __pid_t */
   1620  1.9  christos   pid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
   1621  1.9  christos 			     int32_type->length () * TARGET_CHAR_BIT,
   1622  1.9  christos 			     "__pid_t");
   1623  1.8  christos   pid_type->set_target_type (int32_type);
   1624  1.8  christos   pid_type->set_target_is_stub (true);
   1625  1.6  christos 
   1626  1.6  christos   /* __uid_t */
   1627  1.9  christos   uid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
   1628  1.9  christos 			     uint32_type->length () * TARGET_CHAR_BIT,
   1629  1.9  christos 			     "__uid_t");
   1630  1.8  christos   uid_type->set_target_type (uint32_type);
   1631  1.8  christos   pid_type->set_target_is_stub (true);
   1632  1.6  christos 
   1633  1.6  christos   /* _reason */
   1634  1.6  christos   reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
   1635  1.6  christos 
   1636  1.6  christos   /* _fault */
   1637  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1638  1.6  christos   append_composite_type_field (type, "si_trapno", int_type);
   1639  1.6  christos   append_composite_type_field (reason_type, "_fault", type);
   1640  1.6  christos 
   1641  1.6  christos   /* _timer */
   1642  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1643  1.6  christos   append_composite_type_field (type, "si_timerid", int_type);
   1644  1.6  christos   append_composite_type_field (type, "si_overrun", int_type);
   1645  1.6  christos   append_composite_type_field (reason_type, "_timer", type);
   1646  1.6  christos 
   1647  1.6  christos   /* _mesgq */
   1648  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1649  1.6  christos   append_composite_type_field (type, "si_mqd", int_type);
   1650  1.6  christos   append_composite_type_field (reason_type, "_mesgq", type);
   1651  1.6  christos 
   1652  1.6  christos   /* _poll */
   1653  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1654  1.6  christos   append_composite_type_field (type, "si_band", long_type);
   1655  1.6  christos   append_composite_type_field (reason_type, "_poll", type);
   1656  1.6  christos 
   1657  1.6  christos   /* __spare__ */
   1658  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1659  1.6  christos   append_composite_type_field (type, "__spare1__", long_type);
   1660  1.6  christos   append_composite_type_field (type, "__spare2__",
   1661  1.6  christos 			       init_vector_type (int_type, 7));
   1662  1.6  christos   append_composite_type_field (reason_type, "__spare__", type);
   1663  1.6  christos 
   1664  1.6  christos   /* struct siginfo */
   1665  1.6  christos   siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1666  1.7  christos   siginfo_type->set_name (xstrdup ("siginfo"));
   1667  1.6  christos   append_composite_type_field (siginfo_type, "si_signo", int_type);
   1668  1.6  christos   append_composite_type_field (siginfo_type, "si_errno", int_type);
   1669  1.6  christos   append_composite_type_field (siginfo_type, "si_code", int_type);
   1670  1.6  christos   append_composite_type_field (siginfo_type, "si_pid", pid_type);
   1671  1.6  christos   append_composite_type_field (siginfo_type, "si_uid", uid_type);
   1672  1.6  christos   append_composite_type_field (siginfo_type, "si_status", int_type);
   1673  1.6  christos   append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
   1674  1.6  christos   append_composite_type_field (siginfo_type, "si_value", sigval_type);
   1675  1.6  christos   append_composite_type_field (siginfo_type, "_reason", reason_type);
   1676  1.6  christos 
   1677  1.6  christos   fbsd_gdbarch_data->siginfo_type = siginfo_type;
   1678  1.6  christos 
   1679  1.6  christos   return siginfo_type;
   1680  1.6  christos }
   1681  1.6  christos 
   1682  1.6  christos /* Implement the "gdb_signal_from_target" gdbarch method.  */
   1683  1.6  christos 
   1684  1.6  christos static enum gdb_signal
   1685  1.6  christos fbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
   1686  1.6  christos {
   1687  1.6  christos   switch (signal)
   1688  1.6  christos     {
   1689  1.6  christos     case 0:
   1690  1.6  christos       return GDB_SIGNAL_0;
   1691  1.6  christos 
   1692  1.6  christos     case FREEBSD_SIGHUP:
   1693  1.6  christos       return GDB_SIGNAL_HUP;
   1694  1.6  christos 
   1695  1.6  christos     case FREEBSD_SIGINT:
   1696  1.6  christos       return GDB_SIGNAL_INT;
   1697  1.6  christos 
   1698  1.6  christos     case FREEBSD_SIGQUIT:
   1699  1.6  christos       return GDB_SIGNAL_QUIT;
   1700  1.6  christos 
   1701  1.6  christos     case FREEBSD_SIGILL:
   1702  1.6  christos       return GDB_SIGNAL_ILL;
   1703  1.6  christos 
   1704  1.6  christos     case FREEBSD_SIGTRAP:
   1705  1.6  christos       return GDB_SIGNAL_TRAP;
   1706  1.6  christos 
   1707  1.6  christos     case FREEBSD_SIGABRT:
   1708  1.6  christos       return GDB_SIGNAL_ABRT;
   1709  1.6  christos 
   1710  1.6  christos     case FREEBSD_SIGEMT:
   1711  1.6  christos       return GDB_SIGNAL_EMT;
   1712  1.6  christos 
   1713  1.6  christos     case FREEBSD_SIGFPE:
   1714  1.6  christos       return GDB_SIGNAL_FPE;
   1715  1.6  christos 
   1716  1.6  christos     case FREEBSD_SIGKILL:
   1717  1.6  christos       return GDB_SIGNAL_KILL;
   1718  1.6  christos 
   1719  1.6  christos     case FREEBSD_SIGBUS:
   1720  1.6  christos       return GDB_SIGNAL_BUS;
   1721  1.6  christos 
   1722  1.6  christos     case FREEBSD_SIGSEGV:
   1723  1.6  christos       return GDB_SIGNAL_SEGV;
   1724  1.6  christos 
   1725  1.6  christos     case FREEBSD_SIGSYS:
   1726  1.6  christos       return GDB_SIGNAL_SYS;
   1727  1.6  christos 
   1728  1.6  christos     case FREEBSD_SIGPIPE:
   1729  1.6  christos       return GDB_SIGNAL_PIPE;
   1730  1.6  christos 
   1731  1.6  christos     case FREEBSD_SIGALRM:
   1732  1.6  christos       return GDB_SIGNAL_ALRM;
   1733  1.6  christos 
   1734  1.6  christos     case FREEBSD_SIGTERM:
   1735  1.6  christos       return GDB_SIGNAL_TERM;
   1736  1.6  christos 
   1737  1.6  christos     case FREEBSD_SIGURG:
   1738  1.6  christos       return GDB_SIGNAL_URG;
   1739  1.6  christos 
   1740  1.6  christos     case FREEBSD_SIGSTOP:
   1741  1.6  christos       return GDB_SIGNAL_STOP;
   1742  1.6  christos 
   1743  1.6  christos     case FREEBSD_SIGTSTP:
   1744  1.6  christos       return GDB_SIGNAL_TSTP;
   1745  1.6  christos 
   1746  1.6  christos     case FREEBSD_SIGCONT:
   1747  1.6  christos       return GDB_SIGNAL_CONT;
   1748  1.6  christos 
   1749  1.6  christos     case FREEBSD_SIGCHLD:
   1750  1.6  christos       return GDB_SIGNAL_CHLD;
   1751  1.6  christos 
   1752  1.6  christos     case FREEBSD_SIGTTIN:
   1753  1.6  christos       return GDB_SIGNAL_TTIN;
   1754  1.6  christos 
   1755  1.6  christos     case FREEBSD_SIGTTOU:
   1756  1.6  christos       return GDB_SIGNAL_TTOU;
   1757  1.6  christos 
   1758  1.6  christos     case FREEBSD_SIGIO:
   1759  1.6  christos       return GDB_SIGNAL_IO;
   1760  1.6  christos 
   1761  1.6  christos     case FREEBSD_SIGXCPU:
   1762  1.6  christos       return GDB_SIGNAL_XCPU;
   1763  1.6  christos 
   1764  1.6  christos     case FREEBSD_SIGXFSZ:
   1765  1.6  christos       return GDB_SIGNAL_XFSZ;
   1766  1.6  christos 
   1767  1.6  christos     case FREEBSD_SIGVTALRM:
   1768  1.6  christos       return GDB_SIGNAL_VTALRM;
   1769  1.6  christos 
   1770  1.6  christos     case FREEBSD_SIGPROF:
   1771  1.6  christos       return GDB_SIGNAL_PROF;
   1772  1.6  christos 
   1773  1.6  christos     case FREEBSD_SIGWINCH:
   1774  1.6  christos       return GDB_SIGNAL_WINCH;
   1775  1.6  christos 
   1776  1.6  christos     case FREEBSD_SIGINFO:
   1777  1.6  christos       return GDB_SIGNAL_INFO;
   1778  1.6  christos 
   1779  1.6  christos     case FREEBSD_SIGUSR1:
   1780  1.6  christos       return GDB_SIGNAL_USR1;
   1781  1.6  christos 
   1782  1.6  christos     case FREEBSD_SIGUSR2:
   1783  1.6  christos       return GDB_SIGNAL_USR2;
   1784  1.6  christos 
   1785  1.6  christos     /* SIGTHR is the same as SIGLWP on FreeBSD. */
   1786  1.6  christos     case FREEBSD_SIGTHR:
   1787  1.6  christos       return GDB_SIGNAL_LWP;
   1788  1.6  christos 
   1789  1.6  christos     case FREEBSD_SIGLIBRT:
   1790  1.6  christos       return GDB_SIGNAL_LIBRT;
   1791  1.6  christos     }
   1792  1.6  christos 
   1793  1.6  christos   if (signal >= FREEBSD_SIGRTMIN && signal <= FREEBSD_SIGRTMAX)
   1794  1.6  christos     {
   1795  1.6  christos       int offset = signal - FREEBSD_SIGRTMIN;
   1796  1.6  christos 
   1797  1.6  christos       return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_65 + offset);
   1798  1.6  christos     }
   1799  1.6  christos 
   1800  1.6  christos   return GDB_SIGNAL_UNKNOWN;
   1801  1.6  christos }
   1802  1.6  christos 
   1803  1.6  christos /* Implement the "gdb_signal_to_target" gdbarch method.  */
   1804  1.6  christos 
   1805  1.6  christos static int
   1806  1.6  christos fbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
   1807  1.8  christos 		enum gdb_signal signal)
   1808  1.6  christos {
   1809  1.6  christos   switch (signal)
   1810  1.6  christos     {
   1811  1.6  christos     case GDB_SIGNAL_0:
   1812  1.6  christos       return 0;
   1813  1.6  christos 
   1814  1.6  christos     case GDB_SIGNAL_HUP:
   1815  1.6  christos       return FREEBSD_SIGHUP;
   1816  1.6  christos 
   1817  1.6  christos     case GDB_SIGNAL_INT:
   1818  1.6  christos       return FREEBSD_SIGINT;
   1819  1.6  christos 
   1820  1.6  christos     case GDB_SIGNAL_QUIT:
   1821  1.6  christos       return FREEBSD_SIGQUIT;
   1822  1.6  christos 
   1823  1.6  christos     case GDB_SIGNAL_ILL:
   1824  1.6  christos       return FREEBSD_SIGILL;
   1825  1.6  christos 
   1826  1.6  christos     case GDB_SIGNAL_TRAP:
   1827  1.6  christos       return FREEBSD_SIGTRAP;
   1828  1.6  christos 
   1829  1.6  christos     case GDB_SIGNAL_ABRT:
   1830  1.6  christos       return FREEBSD_SIGABRT;
   1831  1.6  christos 
   1832  1.6  christos     case GDB_SIGNAL_EMT:
   1833  1.6  christos       return FREEBSD_SIGEMT;
   1834  1.6  christos 
   1835  1.6  christos     case GDB_SIGNAL_FPE:
   1836  1.6  christos       return FREEBSD_SIGFPE;
   1837  1.6  christos 
   1838  1.6  christos     case GDB_SIGNAL_KILL:
   1839  1.6  christos       return FREEBSD_SIGKILL;
   1840  1.6  christos 
   1841  1.6  christos     case GDB_SIGNAL_BUS:
   1842  1.6  christos       return FREEBSD_SIGBUS;
   1843  1.6  christos 
   1844  1.6  christos     case GDB_SIGNAL_SEGV:
   1845  1.6  christos       return FREEBSD_SIGSEGV;
   1846  1.6  christos 
   1847  1.6  christos     case GDB_SIGNAL_SYS:
   1848  1.6  christos       return FREEBSD_SIGSYS;
   1849  1.6  christos 
   1850  1.6  christos     case GDB_SIGNAL_PIPE:
   1851  1.6  christos       return FREEBSD_SIGPIPE;
   1852  1.6  christos 
   1853  1.6  christos     case GDB_SIGNAL_ALRM:
   1854  1.6  christos       return FREEBSD_SIGALRM;
   1855  1.6  christos 
   1856  1.6  christos     case GDB_SIGNAL_TERM:
   1857  1.6  christos       return FREEBSD_SIGTERM;
   1858  1.6  christos 
   1859  1.6  christos     case GDB_SIGNAL_URG:
   1860  1.6  christos       return FREEBSD_SIGURG;
   1861  1.6  christos 
   1862  1.6  christos     case GDB_SIGNAL_STOP:
   1863  1.6  christos       return FREEBSD_SIGSTOP;
   1864  1.6  christos 
   1865  1.6  christos     case GDB_SIGNAL_TSTP:
   1866  1.6  christos       return FREEBSD_SIGTSTP;
   1867  1.6  christos 
   1868  1.6  christos     case GDB_SIGNAL_CONT:
   1869  1.6  christos       return FREEBSD_SIGCONT;
   1870  1.6  christos 
   1871  1.6  christos     case GDB_SIGNAL_CHLD:
   1872  1.6  christos       return FREEBSD_SIGCHLD;
   1873  1.6  christos 
   1874  1.6  christos     case GDB_SIGNAL_TTIN:
   1875  1.6  christos       return FREEBSD_SIGTTIN;
   1876  1.6  christos 
   1877  1.6  christos     case GDB_SIGNAL_TTOU:
   1878  1.6  christos       return FREEBSD_SIGTTOU;
   1879  1.6  christos 
   1880  1.6  christos     case GDB_SIGNAL_IO:
   1881  1.6  christos       return FREEBSD_SIGIO;
   1882  1.6  christos 
   1883  1.6  christos     case GDB_SIGNAL_XCPU:
   1884  1.6  christos       return FREEBSD_SIGXCPU;
   1885  1.6  christos 
   1886  1.6  christos     case GDB_SIGNAL_XFSZ:
   1887  1.6  christos       return FREEBSD_SIGXFSZ;
   1888  1.6  christos 
   1889  1.6  christos     case GDB_SIGNAL_VTALRM:
   1890  1.6  christos       return FREEBSD_SIGVTALRM;
   1891  1.6  christos 
   1892  1.6  christos     case GDB_SIGNAL_PROF:
   1893  1.6  christos       return FREEBSD_SIGPROF;
   1894  1.6  christos 
   1895  1.6  christos     case GDB_SIGNAL_WINCH:
   1896  1.6  christos       return FREEBSD_SIGWINCH;
   1897  1.6  christos 
   1898  1.6  christos     case GDB_SIGNAL_INFO:
   1899  1.6  christos       return FREEBSD_SIGINFO;
   1900  1.6  christos 
   1901  1.6  christos     case GDB_SIGNAL_USR1:
   1902  1.6  christos       return FREEBSD_SIGUSR1;
   1903  1.6  christos 
   1904  1.6  christos     case GDB_SIGNAL_USR2:
   1905  1.6  christos       return FREEBSD_SIGUSR2;
   1906  1.6  christos 
   1907  1.6  christos     case GDB_SIGNAL_LWP:
   1908  1.6  christos       return FREEBSD_SIGTHR;
   1909  1.6  christos 
   1910  1.6  christos     case GDB_SIGNAL_LIBRT:
   1911  1.6  christos       return FREEBSD_SIGLIBRT;
   1912  1.6  christos     }
   1913  1.6  christos 
   1914  1.6  christos   if (signal >= GDB_SIGNAL_REALTIME_65
   1915  1.6  christos       && signal <= GDB_SIGNAL_REALTIME_126)
   1916  1.6  christos     {
   1917  1.6  christos       int offset = signal - GDB_SIGNAL_REALTIME_65;
   1918  1.6  christos 
   1919  1.6  christos       return FREEBSD_SIGRTMIN + offset;
   1920  1.6  christos     }
   1921  1.6  christos 
   1922  1.6  christos   return -1;
   1923  1.6  christos }
   1924  1.6  christos 
   1925  1.4  christos /* Implement the "get_syscall_number" gdbarch method.  */
   1926  1.4  christos 
   1927  1.4  christos static LONGEST
   1928  1.6  christos fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
   1929  1.4  christos {
   1930  1.4  christos 
   1931  1.4  christos   /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
   1932  1.4  christos      native targets fetch the system call number from the
   1933  1.4  christos      'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
   1934  1.4  christos      However, system call catching requires this function to be
   1935  1.4  christos      set.  */
   1936  1.4  christos 
   1937  1.8  christos   internal_error (_("fbsd_get_sycall_number called"));
   1938  1.4  christos }
   1939  1.4  christos 
   1940  1.7  christos /* Read an integer symbol value from the current target.  */
   1941  1.7  christos 
   1942  1.7  christos static LONGEST
   1943  1.7  christos fbsd_read_integer_by_name (struct gdbarch *gdbarch, const char *name)
   1944  1.7  christos {
   1945  1.7  christos   bound_minimal_symbol ms = lookup_minimal_symbol (name, NULL, NULL);
   1946  1.7  christos   if (ms.minsym == NULL)
   1947  1.7  christos     error (_("Unable to resolve symbol '%s'"), name);
   1948  1.7  christos 
   1949  1.7  christos   gdb_byte buf[4];
   1950  1.8  christos   if (target_read_memory (ms.value_address (), buf, sizeof buf) != 0)
   1951  1.7  christos     error (_("Unable to read value of '%s'"), name);
   1952  1.7  christos 
   1953  1.8  christos   return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
   1954  1.7  christos }
   1955  1.7  christos 
   1956  1.7  christos /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
   1957  1.7  christos    structure needed to determine the TLS index of an object file.  */
   1958  1.7  christos 
   1959  1.7  christos static void
   1960  1.7  christos fbsd_fetch_rtld_offsets (struct gdbarch *gdbarch, struct fbsd_pspace_data *data)
   1961  1.7  christos {
   1962  1.7  christos   try
   1963  1.7  christos     {
   1964  1.7  christos       /* Fetch offsets from debug symbols in rtld.  */
   1965  1.7  christos       struct symbol *obj_entry_sym
   1966  1.9  christos 	= lookup_symbol_in_language ("Struct_Obj_Entry", nullptr,
   1967  1.9  christos 				     SEARCH_STRUCT_DOMAIN,
   1968  1.9  christos 				     language_c, nullptr).symbol;
   1969  1.7  christos       if (obj_entry_sym == NULL)
   1970  1.7  christos 	error (_("Unable to find Struct_Obj_Entry symbol"));
   1971  1.8  christos       data->off_linkmap = lookup_struct_elt (obj_entry_sym->type (),
   1972  1.7  christos 					     "linkmap", 0).offset / 8;
   1973  1.8  christos       data->off_tlsindex = lookup_struct_elt (obj_entry_sym->type (),
   1974  1.7  christos 					      "tlsindex", 0).offset / 8;
   1975  1.7  christos       data->rtld_offsets_valid = true;
   1976  1.7  christos       return;
   1977  1.7  christos     }
   1978  1.7  christos   catch (const gdb_exception_error &e)
   1979  1.7  christos     {
   1980  1.7  christos       data->off_linkmap = -1;
   1981  1.7  christos     }
   1982  1.7  christos 
   1983  1.7  christos   try
   1984  1.7  christos     {
   1985  1.7  christos       /* Fetch offsets from global variables in libthr.  Note that
   1986  1.7  christos 	 this does not work for single-threaded processes that are not
   1987  1.7  christos 	 linked against libthr.  */
   1988  1.7  christos       data->off_linkmap = fbsd_read_integer_by_name (gdbarch,
   1989  1.7  christos 						     "_thread_off_linkmap");
   1990  1.7  christos       data->off_tlsindex = fbsd_read_integer_by_name (gdbarch,
   1991  1.7  christos 						      "_thread_off_tlsindex");
   1992  1.7  christos       data->rtld_offsets_valid = true;
   1993  1.7  christos       return;
   1994  1.7  christos     }
   1995  1.7  christos   catch (const gdb_exception_error &e)
   1996  1.7  christos     {
   1997  1.7  christos       data->off_linkmap = -1;
   1998  1.7  christos     }
   1999  1.7  christos }
   2000  1.7  christos 
   2001  1.7  christos /* Helper function to read the TLS index of an object file associated
   2002  1.7  christos    with a link map entry at LM_ADDR.  */
   2003  1.7  christos 
   2004  1.7  christos static LONGEST
   2005  1.7  christos fbsd_get_tls_index (struct gdbarch *gdbarch, CORE_ADDR lm_addr)
   2006  1.7  christos {
   2007  1.7  christos   struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
   2008  1.7  christos 
   2009  1.7  christos   if (!data->rtld_offsets_valid)
   2010  1.7  christos     fbsd_fetch_rtld_offsets (gdbarch, data);
   2011  1.7  christos 
   2012  1.7  christos   if (data->off_linkmap == -1)
   2013  1.7  christos     throw_error (TLS_GENERIC_ERROR,
   2014  1.7  christos 		 _("Cannot fetch runtime linker structure offsets"));
   2015  1.7  christos 
   2016  1.7  christos   /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
   2017  1.7  christos      pointer and then compute the offset of the tlsindex member.  */
   2018  1.7  christos   CORE_ADDR tlsindex_addr = lm_addr - data->off_linkmap + data->off_tlsindex;
   2019  1.7  christos 
   2020  1.7  christos   gdb_byte buf[4];
   2021  1.7  christos   if (target_read_memory (tlsindex_addr, buf, sizeof buf) != 0)
   2022  1.7  christos     throw_error (TLS_GENERIC_ERROR,
   2023  1.7  christos 		 _("Cannot find thread-local variables on this target"));
   2024  1.7  christos 
   2025  1.8  christos   return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
   2026  1.7  christos }
   2027  1.7  christos 
   2028  1.7  christos /* See fbsd-tdep.h.  */
   2029  1.7  christos 
   2030  1.7  christos CORE_ADDR
   2031  1.7  christos fbsd_get_thread_local_address (struct gdbarch *gdbarch, CORE_ADDR dtv_addr,
   2032  1.7  christos 			       CORE_ADDR lm_addr, CORE_ADDR offset)
   2033  1.7  christos {
   2034  1.7  christos   LONGEST tls_index = fbsd_get_tls_index (gdbarch, lm_addr);
   2035  1.7  christos 
   2036  1.7  christos   gdb_byte buf[gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT];
   2037  1.7  christos   if (target_read_memory (dtv_addr, buf, sizeof buf) != 0)
   2038  1.7  christos     throw_error (TLS_GENERIC_ERROR,
   2039  1.7  christos 		 _("Cannot find thread-local variables on this target"));
   2040  1.7  christos 
   2041  1.7  christos   const struct builtin_type *builtin = builtin_type (gdbarch);
   2042  1.7  christos   CORE_ADDR addr = gdbarch_pointer_to_address (gdbarch,
   2043  1.7  christos 					       builtin->builtin_data_ptr, buf);
   2044  1.7  christos 
   2045  1.8  christos   addr += (tls_index + 1) * builtin->builtin_data_ptr->length ();
   2046  1.7  christos   if (target_read_memory (addr, buf, sizeof buf) != 0)
   2047  1.7  christos     throw_error (TLS_GENERIC_ERROR,
   2048  1.7  christos 		 _("Cannot find thread-local variables on this target"));
   2049  1.7  christos 
   2050  1.7  christos   addr = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr, buf);
   2051  1.7  christos   return addr + offset;
   2052  1.7  christos }
   2053  1.7  christos 
   2054  1.7  christos /* See fbsd-tdep.h.  */
   2055  1.7  christos 
   2056  1.7  christos CORE_ADDR
   2057  1.7  christos fbsd_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
   2058  1.7  christos {
   2059  1.7  christos   struct bound_minimal_symbol msym = lookup_bound_minimal_symbol ("_rtld_bind");
   2060  1.8  christos   if (msym.minsym != nullptr && msym.value_address () == pc)
   2061  1.7  christos     return frame_unwind_caller_pc (get_current_frame ());
   2062  1.7  christos 
   2063  1.7  christos   return 0;
   2064  1.7  christos }
   2065  1.7  christos 
   2066  1.8  christos /* Return description of signal code or nullptr.  */
   2067  1.8  christos 
   2068  1.8  christos static const char *
   2069  1.8  christos fbsd_signal_cause (enum gdb_signal siggnal, int code)
   2070  1.8  christos {
   2071  1.8  christos   /* Signal-independent causes.  */
   2072  1.8  christos   switch (code)
   2073  1.8  christos     {
   2074  1.8  christos     case FBSD_SI_USER:
   2075  1.8  christos       return _("Sent by kill()");
   2076  1.8  christos     case FBSD_SI_QUEUE:
   2077  1.8  christos       return _("Sent by sigqueue()");
   2078  1.8  christos     case FBSD_SI_TIMER:
   2079  1.8  christos       return _("Timer expired");
   2080  1.8  christos     case FBSD_SI_ASYNCIO:
   2081  1.8  christos       return _("Asynchronous I/O request completed");
   2082  1.8  christos     case FBSD_SI_MESGQ:
   2083  1.8  christos       return _("Message arrived on empty message queue");
   2084  1.8  christos     case FBSD_SI_KERNEL:
   2085  1.8  christos       return _("Sent by kernel");
   2086  1.8  christos     case FBSD_SI_LWP:
   2087  1.8  christos       return _("Sent by thr_kill()");
   2088  1.8  christos     }
   2089  1.8  christos 
   2090  1.8  christos   switch (siggnal)
   2091  1.8  christos     {
   2092  1.8  christos     case GDB_SIGNAL_ILL:
   2093  1.8  christos       switch (code)
   2094  1.8  christos 	{
   2095  1.8  christos 	case FBSD_ILL_ILLOPC:
   2096  1.8  christos 	  return _("Illegal opcode");
   2097  1.8  christos 	case FBSD_ILL_ILLOPN:
   2098  1.8  christos 	  return _("Illegal operand");
   2099  1.8  christos 	case FBSD_ILL_ILLADR:
   2100  1.8  christos 	  return _("Illegal addressing mode");
   2101  1.8  christos 	case FBSD_ILL_ILLTRP:
   2102  1.8  christos 	  return _("Illegal trap");
   2103  1.8  christos 	case FBSD_ILL_PRVOPC:
   2104  1.8  christos 	  return _("Privileged opcode");
   2105  1.8  christos 	case FBSD_ILL_PRVREG:
   2106  1.8  christos 	  return _("Privileged register");
   2107  1.8  christos 	case FBSD_ILL_COPROC:
   2108  1.8  christos 	  return _("Coprocessor error");
   2109  1.8  christos 	case FBSD_ILL_BADSTK:
   2110  1.8  christos 	  return _("Internal stack error");
   2111  1.8  christos 	}
   2112  1.8  christos       break;
   2113  1.8  christos     case GDB_SIGNAL_BUS:
   2114  1.8  christos       switch (code)
   2115  1.8  christos 	{
   2116  1.8  christos 	case FBSD_BUS_ADRALN:
   2117  1.8  christos 	  return _("Invalid address alignment");
   2118  1.8  christos 	case FBSD_BUS_ADRERR:
   2119  1.8  christos 	  return _("Address not present");
   2120  1.8  christos 	case FBSD_BUS_OBJERR:
   2121  1.8  christos 	  return _("Object-specific hardware error");
   2122  1.8  christos 	case FBSD_BUS_OOMERR:
   2123  1.8  christos 	  return _("Out of memory");
   2124  1.8  christos 	}
   2125  1.8  christos       break;
   2126  1.8  christos     case GDB_SIGNAL_SEGV:
   2127  1.8  christos       switch (code)
   2128  1.8  christos 	{
   2129  1.8  christos 	case FBSD_SEGV_MAPERR:
   2130  1.8  christos 	  return _("Address not mapped to object");
   2131  1.8  christos 	case FBSD_SEGV_ACCERR:
   2132  1.8  christos 	  return _("Invalid permissions for mapped object");
   2133  1.8  christos 	case FBSD_SEGV_PKUERR:
   2134  1.8  christos 	  return _("PKU violation");
   2135  1.8  christos 	}
   2136  1.8  christos       break;
   2137  1.8  christos     case GDB_SIGNAL_FPE:
   2138  1.8  christos       switch (code)
   2139  1.8  christos 	{
   2140  1.8  christos 	case FBSD_FPE_INTOVF:
   2141  1.8  christos 	  return _("Integer overflow");
   2142  1.8  christos 	case FBSD_FPE_INTDIV:
   2143  1.8  christos 	  return _("Integer divide by zero");
   2144  1.8  christos 	case FBSD_FPE_FLTDIV:
   2145  1.8  christos 	  return _("Floating point divide by zero");
   2146  1.8  christos 	case FBSD_FPE_FLTOVF:
   2147  1.8  christos 	  return _("Floating point overflow");
   2148  1.8  christos 	case FBSD_FPE_FLTUND:
   2149  1.8  christos 	  return _("Floating point underflow");
   2150  1.8  christos 	case FBSD_FPE_FLTRES:
   2151  1.8  christos 	  return _("Floating point inexact result");
   2152  1.8  christos 	case FBSD_FPE_FLTINV:
   2153  1.8  christos 	  return _("Invalid floating point operation");
   2154  1.8  christos 	case FBSD_FPE_FLTSUB:
   2155  1.8  christos 	  return _("Subscript out of range");
   2156  1.8  christos 	}
   2157  1.8  christos       break;
   2158  1.8  christos     case GDB_SIGNAL_TRAP:
   2159  1.8  christos       switch (code)
   2160  1.8  christos 	{
   2161  1.8  christos 	case FBSD_TRAP_BRKPT:
   2162  1.8  christos 	  return _("Breakpoint");
   2163  1.8  christos 	case FBSD_TRAP_TRACE:
   2164  1.8  christos 	  return _("Trace trap");
   2165  1.8  christos 	case FBSD_TRAP_DTRACE:
   2166  1.8  christos 	  return _("DTrace-induced trap");
   2167  1.8  christos 	case FBSD_TRAP_CAP:
   2168  1.8  christos 	  return _("Capability violation");
   2169  1.8  christos 	}
   2170  1.8  christos       break;
   2171  1.8  christos     case GDB_SIGNAL_CHLD:
   2172  1.8  christos       switch (code)
   2173  1.8  christos 	{
   2174  1.8  christos 	case FBSD_CLD_EXITED:
   2175  1.8  christos 	  return _("Child has exited");
   2176  1.8  christos 	case FBSD_CLD_KILLED:
   2177  1.8  christos 	  return _("Child has terminated abnormally");
   2178  1.8  christos 	case FBSD_CLD_DUMPED:
   2179  1.8  christos 	  return _("Child has dumped core");
   2180  1.8  christos 	case FBSD_CLD_TRAPPED:
   2181  1.8  christos 	  return _("Traced child has trapped");
   2182  1.8  christos 	case FBSD_CLD_STOPPED:
   2183  1.8  christos 	  return _("Child has stopped");
   2184  1.8  christos 	case FBSD_CLD_CONTINUED:
   2185  1.8  christos 	  return _("Stopped child has continued");
   2186  1.8  christos 	}
   2187  1.8  christos       break;
   2188  1.8  christos     case GDB_SIGNAL_POLL:
   2189  1.8  christos       switch (code)
   2190  1.8  christos 	{
   2191  1.8  christos 	case FBSD_POLL_IN:
   2192  1.8  christos 	  return _("Data input available");
   2193  1.8  christos 	case FBSD_POLL_OUT:
   2194  1.8  christos 	  return _("Output buffers available");
   2195  1.8  christos 	case FBSD_POLL_MSG:
   2196  1.8  christos 	  return _("Input message available");
   2197  1.8  christos 	case FBSD_POLL_ERR:
   2198  1.8  christos 	  return _("I/O error");
   2199  1.8  christos 	case FBSD_POLL_PRI:
   2200  1.8  christos 	  return _("High priority input available");
   2201  1.8  christos 	case FBSD_POLL_HUP:
   2202  1.8  christos 	  return _("Device disconnected");
   2203  1.8  christos 	}
   2204  1.8  christos       break;
   2205  1.8  christos     }
   2206  1.8  christos 
   2207  1.8  christos   return nullptr;
   2208  1.8  christos }
   2209  1.8  christos 
   2210  1.8  christos /* Report additional details for a signal stop.  */
   2211  1.8  christos 
   2212  1.8  christos static void
   2213  1.8  christos fbsd_report_signal_info (struct gdbarch *gdbarch, struct ui_out *uiout,
   2214  1.8  christos 			 enum gdb_signal siggnal)
   2215  1.8  christos {
   2216  1.8  christos   LONGEST code, mqd, pid, status, timerid, uid;
   2217  1.8  christos 
   2218  1.8  christos   try
   2219  1.8  christos     {
   2220  1.8  christos       code = parse_and_eval_long ("$_siginfo.si_code");
   2221  1.8  christos       pid = parse_and_eval_long ("$_siginfo.si_pid");
   2222  1.8  christos       uid = parse_and_eval_long ("$_siginfo.si_uid");
   2223  1.8  christos       status = parse_and_eval_long ("$_siginfo.si_status");
   2224  1.8  christos       timerid = parse_and_eval_long ("$_siginfo._reason._timer.si_timerid");
   2225  1.8  christos       mqd = parse_and_eval_long ("$_siginfo._reason._mesgq.si_mqd");
   2226  1.8  christos     }
   2227  1.8  christos   catch (const gdb_exception_error &e)
   2228  1.8  christos     {
   2229  1.8  christos       return;
   2230  1.8  christos     }
   2231  1.8  christos 
   2232  1.8  christos   const char *meaning = fbsd_signal_cause (siggnal, code);
   2233  1.8  christos   if (meaning == nullptr)
   2234  1.8  christos     return;
   2235  1.8  christos 
   2236  1.8  christos   uiout->text (".\n");
   2237  1.8  christos   uiout->field_string ("sigcode-meaning", meaning);
   2238  1.8  christos 
   2239  1.8  christos   switch (code)
   2240  1.8  christos     {
   2241  1.8  christos     case FBSD_SI_USER:
   2242  1.8  christos     case FBSD_SI_QUEUE:
   2243  1.8  christos     case FBSD_SI_LWP:
   2244  1.8  christos       uiout->text (" from pid ");
   2245  1.8  christos       uiout->field_string ("sending-pid", plongest (pid));
   2246  1.8  christos       uiout->text (" and user ");
   2247  1.8  christos       uiout->field_string ("sending-uid", plongest (uid));
   2248  1.8  christos       return;
   2249  1.8  christos     case FBSD_SI_TIMER:
   2250  1.8  christos       uiout->text (": timerid ");
   2251  1.8  christos       uiout->field_string ("timerid", plongest (timerid));
   2252  1.8  christos       return;
   2253  1.8  christos     case FBSD_SI_MESGQ:
   2254  1.8  christos       uiout->text (": message queue ");
   2255  1.8  christos       uiout->field_string ("message-queue", plongest (mqd));
   2256  1.8  christos       return;
   2257  1.8  christos     case FBSD_SI_ASYNCIO:
   2258  1.8  christos       return;
   2259  1.8  christos     }
   2260  1.8  christos 
   2261  1.8  christos   if (siggnal == GDB_SIGNAL_CHLD)
   2262  1.8  christos     {
   2263  1.8  christos       uiout->text (": pid ");
   2264  1.8  christos       uiout->field_string ("child-pid", plongest (pid));
   2265  1.8  christos       uiout->text (", uid ");
   2266  1.8  christos       uiout->field_string ("child-uid", plongest (uid));
   2267  1.8  christos       if (code == FBSD_CLD_EXITED)
   2268  1.8  christos 	{
   2269  1.8  christos 	  uiout->text (", exit status ");
   2270  1.8  christos 	  uiout->field_string ("exit-status", plongest (status));
   2271  1.8  christos 	}
   2272  1.8  christos       else
   2273  1.8  christos 	{
   2274  1.8  christos 	  uiout->text (", signal ");
   2275  1.8  christos 	  uiout->field_string ("signal", plongest (status));
   2276  1.8  christos 	}
   2277  1.8  christos     }
   2278  1.8  christos }
   2279  1.8  christos 
   2280  1.8  christos /* Search a list of struct kinfo_vmmap entries in the ENTRIES buffer
   2281  1.8  christos    of LEN bytes to find the length of the entry starting at ADDR.
   2282  1.8  christos    Returns the length of the entry or zero if no entry was found.  */
   2283  1.8  christos 
   2284  1.8  christos static ULONGEST
   2285  1.8  christos fbsd_vmmap_length (struct gdbarch *gdbarch, unsigned char *entries, size_t len,
   2286  1.8  christos 		   CORE_ADDR addr)
   2287  1.8  christos {
   2288  1.8  christos       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   2289  1.8  christos       unsigned char *descdata = entries;
   2290  1.8  christos       unsigned char *descend = descdata + len;
   2291  1.8  christos 
   2292  1.8  christos       /* Skip over the structure size.  */
   2293  1.8  christos       descdata += 4;
   2294  1.8  christos 
   2295  1.8  christos       while (descdata + KVE_PATH < descend)
   2296  1.8  christos 	{
   2297  1.8  christos 	  ULONGEST structsize = extract_unsigned_integer (descdata
   2298  1.8  christos 							  + KVE_STRUCTSIZE, 4,
   2299  1.8  christos 							  byte_order);
   2300  1.8  christos 	  if (structsize < KVE_PATH)
   2301  1.8  christos 	    return false;
   2302  1.8  christos 
   2303  1.8  christos 	  ULONGEST start = extract_unsigned_integer (descdata + KVE_START, 8,
   2304  1.8  christos 						     byte_order);
   2305  1.8  christos 	  ULONGEST end = extract_unsigned_integer (descdata + KVE_END, 8,
   2306  1.8  christos 						   byte_order);
   2307  1.8  christos 	  if (start == addr)
   2308  1.8  christos 	    return end - start;
   2309  1.8  christos 
   2310  1.8  christos 	  descdata += structsize;
   2311  1.8  christos 	}
   2312  1.8  christos       return 0;
   2313  1.8  christos }
   2314  1.8  christos 
   2315  1.8  christos /* Helper for fbsd_vsyscall_range that does the real work of finding
   2316  1.8  christos    the vDSO's address range.  */
   2317  1.8  christos 
   2318  1.8  christos static bool
   2319  1.8  christos fbsd_vdso_range (struct gdbarch *gdbarch, struct mem_range *range)
   2320  1.8  christos {
   2321  1.8  christos   if (target_auxv_search (AT_FREEBSD_KPRELOAD, &range->start) <= 0)
   2322  1.8  christos     return false;
   2323  1.8  christos 
   2324  1.8  christos   if (!target_has_execution ())
   2325  1.8  christos     {
   2326  1.8  christos       /* Search for the ending address in the NT_PROCSTAT_VMMAP note. */
   2327  1.9  christos       bfd *cbfd = current_program_space->core_bfd ();
   2328  1.9  christos       asection *section = bfd_get_section_by_name (cbfd,
   2329  1.8  christos 						   ".note.freebsdcore.vmmap");
   2330  1.8  christos       if (section == nullptr)
   2331  1.8  christos 	return false;
   2332  1.8  christos 
   2333  1.8  christos       size_t note_size = bfd_section_size (section);
   2334  1.8  christos       if (note_size < 4)
   2335  1.8  christos 	return false;
   2336  1.8  christos 
   2337  1.8  christos       gdb::def_vector<unsigned char> contents (note_size);
   2338  1.9  christos       if (!bfd_get_section_contents (cbfd, section, contents.data (),
   2339  1.8  christos 				     0, note_size))
   2340  1.8  christos 	return false;
   2341  1.8  christos 
   2342  1.8  christos       range->length = fbsd_vmmap_length (gdbarch, contents.data (), note_size,
   2343  1.8  christos 					 range->start);
   2344  1.8  christos     }
   2345  1.8  christos   else
   2346  1.8  christos     {
   2347  1.8  christos       /* Fetch the list of address space entries from the running target. */
   2348  1.9  christos       std::optional<gdb::byte_vector> buf =
   2349  1.8  christos 	target_read_alloc (current_inferior ()->top_target (),
   2350  1.8  christos 			   TARGET_OBJECT_FREEBSD_VMMAP, nullptr);
   2351  1.8  christos       if (!buf || buf->empty ())
   2352  1.8  christos 	return false;
   2353  1.8  christos 
   2354  1.8  christos       range->length = fbsd_vmmap_length (gdbarch, buf->data (), buf->size (),
   2355  1.8  christos 					 range->start);
   2356  1.8  christos     }
   2357  1.8  christos   return range->length != 0;
   2358  1.8  christos }
   2359  1.8  christos 
   2360  1.8  christos /* Return the address range of the vDSO for the current inferior.  */
   2361  1.8  christos 
   2362  1.8  christos static int
   2363  1.8  christos fbsd_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
   2364  1.8  christos {
   2365  1.8  christos   struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
   2366  1.8  christos 
   2367  1.8  christos   if (data->vdso_range_p == 0)
   2368  1.8  christos     {
   2369  1.8  christos       if (fbsd_vdso_range (gdbarch, &data->vdso_range))
   2370  1.8  christos 	data->vdso_range_p = 1;
   2371  1.8  christos       else
   2372  1.8  christos 	data->vdso_range_p = -1;
   2373  1.8  christos     }
   2374  1.8  christos 
   2375  1.8  christos   if (data->vdso_range_p < 0)
   2376  1.8  christos     return 0;
   2377  1.8  christos 
   2378  1.8  christos   *range = data->vdso_range;
   2379  1.8  christos   return 1;
   2380  1.8  christos }
   2381  1.8  christos 
   2382  1.5  christos /* To be called from GDB_OSABI_FREEBSD handlers. */
   2383  1.1  christos 
   2384  1.1  christos void
   2385  1.1  christos fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   2386  1.1  christos {
   2387  1.4  christos   set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
   2388  1.4  christos   set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
   2389  1.6  christos   set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
   2390  1.1  christos   set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
   2391  1.6  christos   set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
   2392  1.4  christos   set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
   2393  1.6  christos   set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
   2394  1.6  christos   set_gdbarch_gdb_signal_from_target (gdbarch, fbsd_gdb_signal_from_target);
   2395  1.6  christos   set_gdbarch_gdb_signal_to_target (gdbarch, fbsd_gdb_signal_to_target);
   2396  1.8  christos   set_gdbarch_report_signal_info (gdbarch, fbsd_report_signal_info);
   2397  1.7  christos   set_gdbarch_skip_solib_resolver (gdbarch, fbsd_skip_solib_resolver);
   2398  1.8  christos   set_gdbarch_vsyscall_range (gdbarch, fbsd_vsyscall_range);
   2399  1.4  christos 
   2400  1.4  christos   /* `catch syscall' */
   2401  1.4  christos   set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
   2402  1.4  christos   set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
   2403  1.1  christos }
   2404