Home | History | Annotate | Line # | Download | only in gdb
fbsd-tdep.c revision 1.6
      1  1.1  christos /* Target-dependent code for FreeBSD, architecture-independent.
      2  1.1  christos 
      3  1.6  christos    Copyright (C) 2002-2019 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.1  christos #include "defs.h"
     21  1.4  christos #include "auxv.h"
     22  1.1  christos #include "gdbcore.h"
     23  1.1  christos #include "inferior.h"
     24  1.1  christos #include "regcache.h"
     25  1.1  christos #include "regset.h"
     26  1.1  christos #include "gdbthread.h"
     27  1.4  christos #include "xml-syscall.h"
     28  1.6  christos #include <sys/socket.h>
     29  1.6  christos #include <arpa/inet.h>
     30  1.1  christos 
     31  1.1  christos #include "elf-bfd.h"
     32  1.1  christos #include "fbsd-tdep.h"
     33  1.1  christos 
     34  1.6  christos /* This enum is derived from FreeBSD's <sys/signal.h>.  */
     35  1.6  christos 
     36  1.6  christos enum
     37  1.6  christos   {
     38  1.6  christos     FREEBSD_SIGHUP = 1,
     39  1.6  christos     FREEBSD_SIGINT = 2,
     40  1.6  christos     FREEBSD_SIGQUIT = 3,
     41  1.6  christos     FREEBSD_SIGILL = 4,
     42  1.6  christos     FREEBSD_SIGTRAP = 5,
     43  1.6  christos     FREEBSD_SIGABRT = 6,
     44  1.6  christos     FREEBSD_SIGEMT = 7,
     45  1.6  christos     FREEBSD_SIGFPE = 8,
     46  1.6  christos     FREEBSD_SIGKILL = 9,
     47  1.6  christos     FREEBSD_SIGBUS = 10,
     48  1.6  christos     FREEBSD_SIGSEGV = 11,
     49  1.6  christos     FREEBSD_SIGSYS = 12,
     50  1.6  christos     FREEBSD_SIGPIPE = 13,
     51  1.6  christos     FREEBSD_SIGALRM = 14,
     52  1.6  christos     FREEBSD_SIGTERM = 15,
     53  1.6  christos     FREEBSD_SIGURG = 16,
     54  1.6  christos     FREEBSD_SIGSTOP = 17,
     55  1.6  christos     FREEBSD_SIGTSTP = 18,
     56  1.6  christos     FREEBSD_SIGCONT = 19,
     57  1.6  christos     FREEBSD_SIGCHLD = 20,
     58  1.6  christos     FREEBSD_SIGTTIN = 21,
     59  1.6  christos     FREEBSD_SIGTTOU = 22,
     60  1.6  christos     FREEBSD_SIGIO = 23,
     61  1.6  christos     FREEBSD_SIGXCPU = 24,
     62  1.6  christos     FREEBSD_SIGXFSZ = 25,
     63  1.6  christos     FREEBSD_SIGVTALRM = 26,
     64  1.6  christos     FREEBSD_SIGPROF = 27,
     65  1.6  christos     FREEBSD_SIGWINCH = 28,
     66  1.6  christos     FREEBSD_SIGINFO = 29,
     67  1.6  christos     FREEBSD_SIGUSR1 = 30,
     68  1.6  christos     FREEBSD_SIGUSR2 = 31,
     69  1.6  christos     FREEBSD_SIGTHR = 32,
     70  1.6  christos     FREEBSD_SIGLIBRT = 33,
     71  1.6  christos     FREEBSD_SIGRTMIN = 65,
     72  1.6  christos     FREEBSD_SIGRTMAX = 126,
     73  1.6  christos   };
     74  1.6  christos 
     75  1.6  christos /* FreeBSD kernels 12.0 and later include a copy of the
     76  1.6  christos    'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
     77  1.6  christos    operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
     78  1.6  christos    The constants below define the offset of field members and flags in
     79  1.6  christos    this structure used by methods in this file.  Note that the
     80  1.6  christos    'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
     81  1.6  christos    containing the size of the structure.  */
     82  1.6  christos 
     83  1.6  christos #define	LWPINFO_OFFSET		0x4
     84  1.6  christos 
     85  1.6  christos /* Offsets in ptrace_lwpinfo.  */
     86  1.6  christos #define	LWPINFO_PL_FLAGS	0x8
     87  1.6  christos #define	LWPINFO64_PL_SIGINFO	0x30
     88  1.6  christos #define	LWPINFO32_PL_SIGINFO	0x2c
     89  1.6  christos 
     90  1.6  christos /* Flags in pl_flags.  */
     91  1.6  christos #define	PL_FLAG_SI	0x20	/* siginfo is valid */
     92  1.6  christos 
     93  1.6  christos /* Sizes of siginfo_t.	*/
     94  1.6  christos #define	SIZE64_SIGINFO_T	80
     95  1.6  christos #define	SIZE32_SIGINFO_T	64
     96  1.6  christos 
     97  1.6  christos /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
     98  1.6  christos    dump notes.  See <sys/user.h> for the definition of struct
     99  1.6  christos    kinfo_vmentry.  This data structure should have the same layout on
    100  1.6  christos    all architectures.
    101  1.6  christos 
    102  1.6  christos    Note that FreeBSD 7.0 used an older version of this structure
    103  1.6  christos    (struct kinfo_ovmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
    104  1.6  christos    dump note wasn't introduced until FreeBSD 9.2.  As a result, the
    105  1.6  christos    core dump note has always used the 7.1 and later structure
    106  1.6  christos    format.  */
    107  1.6  christos 
    108  1.6  christos #define	KVE_STRUCTSIZE		0x0
    109  1.6  christos #define	KVE_START		0x8
    110  1.6  christos #define	KVE_END			0x10
    111  1.6  christos #define	KVE_OFFSET		0x18
    112  1.6  christos #define	KVE_FLAGS		0x2c
    113  1.6  christos #define	KVE_PROTECTION		0x38
    114  1.6  christos #define	KVE_PATH		0x88
    115  1.6  christos 
    116  1.6  christos /* Flags in the 'kve_protection' field in struct kinfo_vmentry.  These
    117  1.6  christos    match the KVME_PROT_* constants in <sys/user.h>.  */
    118  1.6  christos 
    119  1.6  christos #define	KINFO_VME_PROT_READ	0x00000001
    120  1.6  christos #define	KINFO_VME_PROT_WRITE	0x00000002
    121  1.6  christos #define	KINFO_VME_PROT_EXEC	0x00000004
    122  1.6  christos 
    123  1.6  christos /* Flags in the 'kve_flags' field in struct kinfo_vmentry.  These
    124  1.6  christos    match the KVME_FLAG_* constants in <sys/user.h>.  */
    125  1.6  christos 
    126  1.6  christos #define	KINFO_VME_FLAG_COW		0x00000001
    127  1.6  christos #define	KINFO_VME_FLAG_NEEDS_COPY	0x00000002
    128  1.6  christos #define	KINFO_VME_FLAG_NOCOREDUMP	0x00000004
    129  1.6  christos #define	KINFO_VME_FLAG_SUPER		0x00000008
    130  1.6  christos #define	KINFO_VME_FLAG_GROWS_UP		0x00000010
    131  1.6  christos #define	KINFO_VME_FLAG_GROWS_DOWN	0x00000020
    132  1.6  christos 
    133  1.6  christos /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
    134  1.6  christos    dump notes.  See <sys/user.h> for the definition of struct
    135  1.6  christos    kinfo_file.  This data structure should have the same layout on all
    136  1.6  christos    architectures.
    137  1.6  christos 
    138  1.6  christos    Note that FreeBSD 7.0 used an older version of this structure
    139  1.6  christos    (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
    140  1.6  christos    note wasn't introduced until FreeBSD 9.2.  As a result, the core
    141  1.6  christos    dump note has always used the 7.1 and later structure format.  */
    142  1.6  christos 
    143  1.6  christos #define	KF_STRUCTSIZE		0x0
    144  1.6  christos #define	KF_TYPE			0x4
    145  1.6  christos #define	KF_FD			0x8
    146  1.6  christos #define	KF_FLAGS		0x10
    147  1.6  christos #define	KF_OFFSET		0x18
    148  1.6  christos #define	KF_VNODE_TYPE		0x20
    149  1.6  christos #define	KF_SOCK_DOMAIN		0x24
    150  1.6  christos #define	KF_SOCK_TYPE		0x28
    151  1.6  christos #define	KF_SOCK_PROTOCOL	0x2c
    152  1.6  christos #define	KF_SA_LOCAL		0x30
    153  1.6  christos #define	KF_SA_PEER		0xb0
    154  1.6  christos #define	KF_PATH			0x170
    155  1.6  christos 
    156  1.6  christos /* Constants for the 'kf_type' field in struct kinfo_file.  These
    157  1.6  christos    match the KF_TYPE_* constants in <sys/user.h>.  */
    158  1.6  christos 
    159  1.6  christos #define	KINFO_FILE_TYPE_VNODE	1
    160  1.6  christos #define	KINFO_FILE_TYPE_SOCKET	2
    161  1.6  christos #define	KINFO_FILE_TYPE_PIPE	3
    162  1.6  christos #define	KINFO_FILE_TYPE_FIFO	4
    163  1.6  christos #define	KINFO_FILE_TYPE_KQUEUE	5
    164  1.6  christos #define	KINFO_FILE_TYPE_CRYPTO	6
    165  1.6  christos #define	KINFO_FILE_TYPE_MQUEUE	7
    166  1.6  christos #define	KINFO_FILE_TYPE_SHM	8
    167  1.6  christos #define	KINFO_FILE_TYPE_SEM	9
    168  1.6  christos #define	KINFO_FILE_TYPE_PTS	10
    169  1.6  christos #define	KINFO_FILE_TYPE_PROCDESC 11
    170  1.6  christos 
    171  1.6  christos /* Special values for the 'kf_fd' field in struct kinfo_file.  These
    172  1.6  christos    match the KF_FD_TYPE_* constants in <sys/user.h>.  */
    173  1.6  christos 
    174  1.6  christos #define	KINFO_FILE_FD_TYPE_CWD	-1
    175  1.6  christos #define	KINFO_FILE_FD_TYPE_ROOT	-2
    176  1.6  christos #define	KINFO_FILE_FD_TYPE_JAIL	-3
    177  1.6  christos #define	KINFO_FILE_FD_TYPE_TRACE -4
    178  1.6  christos #define	KINFO_FILE_FD_TYPE_TEXT	-5
    179  1.6  christos #define	KINFO_FILE_FD_TYPE_CTTY	-6
    180  1.6  christos 
    181  1.6  christos /* Flags in the 'kf_flags' field in struct kinfo_file.  These match
    182  1.6  christos    the KF_FLAG_* constants in <sys/user.h>.  */
    183  1.6  christos 
    184  1.6  christos #define	KINFO_FILE_FLAG_READ		0x00000001
    185  1.6  christos #define	KINFO_FILE_FLAG_WRITE		0x00000002
    186  1.6  christos #define	KINFO_FILE_FLAG_APPEND		0x00000004
    187  1.6  christos #define	KINFO_FILE_FLAG_ASYNC		0x00000008
    188  1.6  christos #define	KINFO_FILE_FLAG_FSYNC		0x00000010
    189  1.6  christos #define	KINFO_FILE_FLAG_NONBLOCK	0x00000020
    190  1.6  christos #define	KINFO_FILE_FLAG_DIRECT		0x00000040
    191  1.6  christos #define	KINFO_FILE_FLAG_HASLOCK		0x00000080
    192  1.6  christos #define	KINFO_FILE_FLAG_EXEC		0x00004000
    193  1.6  christos 
    194  1.6  christos /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
    195  1.6  christos    These match the KF_VTYPE_* constants in <sys/user.h>.  */
    196  1.6  christos 
    197  1.6  christos #define	KINFO_FILE_VTYPE_VREG	1
    198  1.6  christos #define	KINFO_FILE_VTYPE_VDIR	2
    199  1.6  christos #define	KINFO_FILE_VTYPE_VCHR	4
    200  1.6  christos #define	KINFO_FILE_VTYPE_VLNK	5
    201  1.6  christos #define	KINFO_FILE_VTYPE_VSOCK	6
    202  1.6  christos #define	KINFO_FILE_VTYPE_VFIFO	7
    203  1.6  christos 
    204  1.6  christos /* Constants for socket address families.  These match AF_* constants
    205  1.6  christos    in <sys/socket.h>.  */
    206  1.6  christos 
    207  1.6  christos #define	FBSD_AF_UNIX		1
    208  1.6  christos #define	FBSD_AF_INET		2
    209  1.6  christos #define	FBSD_AF_INET6		28
    210  1.6  christos 
    211  1.6  christos /* Constants for socket types.  These match SOCK_* constants in
    212  1.6  christos    <sys/socket.h>.  */
    213  1.6  christos 
    214  1.6  christos #define	FBSD_SOCK_STREAM	1
    215  1.6  christos #define	FBSD_SOCK_DGRAM		2
    216  1.6  christos #define	FBSD_SOCK_SEQPACKET	5
    217  1.6  christos 
    218  1.6  christos /* Constants for IP protocols.  These match IPPROTO_* constants in
    219  1.6  christos    <netinet/in.h>.  */
    220  1.6  christos 
    221  1.6  christos #define	FBSD_IPPROTO_ICMP	1
    222  1.6  christos #define	FBSD_IPPROTO_TCP	6
    223  1.6  christos #define	FBSD_IPPROTO_UDP	17
    224  1.6  christos #define	FBSD_IPPROTO_SCTP	132
    225  1.6  christos 
    226  1.6  christos /* Socket address structures.  These have the same layout on all
    227  1.6  christos    FreeBSD architectures.  In addition, multibyte fields such as IP
    228  1.6  christos    addresses are always stored in network byte order.  */
    229  1.6  christos 
    230  1.6  christos struct fbsd_sockaddr_in
    231  1.6  christos {
    232  1.6  christos   uint8_t sin_len;
    233  1.6  christos   uint8_t sin_family;
    234  1.6  christos   uint8_t sin_port[2];
    235  1.6  christos   uint8_t sin_addr[4];
    236  1.6  christos   char sin_zero[8];
    237  1.6  christos };
    238  1.6  christos 
    239  1.6  christos struct fbsd_sockaddr_in6
    240  1.6  christos {
    241  1.6  christos   uint8_t sin6_len;
    242  1.6  christos   uint8_t sin6_family;
    243  1.6  christos   uint8_t sin6_port[2];
    244  1.6  christos   uint32_t sin6_flowinfo;
    245  1.6  christos   uint8_t sin6_addr[16];
    246  1.6  christos   uint32_t sin6_scope_id;
    247  1.6  christos };
    248  1.6  christos 
    249  1.6  christos struct fbsd_sockaddr_un
    250  1.6  christos {
    251  1.6  christos   uint8_t sun_len;
    252  1.6  christos   uint8_t sun_family;
    253  1.6  christos   char sun_path[104];
    254  1.6  christos };
    255  1.6  christos 
    256  1.6  christos /* Number of 32-bit words in a signal set.  This matches _SIG_WORDS in
    257  1.6  christos    <sys/_sigset.h> and is the same value on all architectures.  */
    258  1.6  christos 
    259  1.6  christos #define	SIG_WORDS		4
    260  1.6  christos 
    261  1.6  christos /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
    262  1.6  christos    dump notes.  See <sys/user.h> for the definition of struct
    263  1.6  christos    kinfo_proc.  This data structure has different layouts on different
    264  1.6  christos    architectures mostly due to ILP32 vs LP64.  However, FreeBSD/i386
    265  1.6  christos    uses a 32-bit time_t while all other architectures use a 64-bit
    266  1.6  christos    time_t.
    267  1.6  christos 
    268  1.6  christos    The core dump note actually contains one kinfo_proc structure for
    269  1.6  christos    each thread, but all of the process-wide data can be obtained from
    270  1.6  christos    the first structure.  One result of this note's format is that some
    271  1.6  christos    of the process-wide status available in the native target method
    272  1.6  christos    from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
    273  1.6  christos    is not available from a core dump.  Instead, the per-thread data
    274  1.6  christos    structures contain the value of these fields for individual
    275  1.6  christos    threads.  */
    276  1.6  christos 
    277  1.6  christos struct kinfo_proc_layout
    278  1.6  christos {
    279  1.6  christos   /* Offsets of struct kinfo_proc members.  */
    280  1.6  christos   int ki_layout;
    281  1.6  christos   int ki_pid;
    282  1.6  christos   int ki_ppid;
    283  1.6  christos   int ki_pgid;
    284  1.6  christos   int ki_tpgid;
    285  1.6  christos   int ki_sid;
    286  1.6  christos   int ki_tdev_freebsd11;
    287  1.6  christos   int ki_sigignore;
    288  1.6  christos   int ki_sigcatch;
    289  1.6  christos   int ki_uid;
    290  1.6  christos   int ki_ruid;
    291  1.6  christos   int ki_svuid;
    292  1.6  christos   int ki_rgid;
    293  1.6  christos   int ki_svgid;
    294  1.6  christos   int ki_ngroups;
    295  1.6  christos   int ki_groups;
    296  1.6  christos   int ki_size;
    297  1.6  christos   int ki_rssize;
    298  1.6  christos   int ki_tsize;
    299  1.6  christos   int ki_dsize;
    300  1.6  christos   int ki_ssize;
    301  1.6  christos   int ki_start;
    302  1.6  christos   int ki_nice;
    303  1.6  christos   int ki_comm;
    304  1.6  christos   int ki_tdev;
    305  1.6  christos   int ki_rusage;
    306  1.6  christos   int ki_rusage_ch;
    307  1.6  christos 
    308  1.6  christos   /* Offsets of struct rusage members.  */
    309  1.6  christos   int ru_utime;
    310  1.6  christos   int ru_stime;
    311  1.6  christos   int ru_maxrss;
    312  1.6  christos   int ru_minflt;
    313  1.6  christos   int ru_majflt;
    314  1.6  christos };
    315  1.6  christos 
    316  1.6  christos const struct kinfo_proc_layout kinfo_proc_layout_32 =
    317  1.6  christos   {
    318  1.6  christos     .ki_layout = 0x4,
    319  1.6  christos     .ki_pid = 0x28,
    320  1.6  christos     .ki_ppid = 0x2c,
    321  1.6  christos     .ki_pgid = 0x30,
    322  1.6  christos     .ki_tpgid = 0x34,
    323  1.6  christos     .ki_sid = 0x38,
    324  1.6  christos     .ki_tdev_freebsd11 = 0x44,
    325  1.6  christos     .ki_sigignore = 0x68,
    326  1.6  christos     .ki_sigcatch = 0x78,
    327  1.6  christos     .ki_uid = 0x88,
    328  1.6  christos     .ki_ruid = 0x8c,
    329  1.6  christos     .ki_svuid = 0x90,
    330  1.6  christos     .ki_rgid = 0x94,
    331  1.6  christos     .ki_svgid = 0x98,
    332  1.6  christos     .ki_ngroups = 0x9c,
    333  1.6  christos     .ki_groups = 0xa0,
    334  1.6  christos     .ki_size = 0xe0,
    335  1.6  christos     .ki_rssize = 0xe4,
    336  1.6  christos     .ki_tsize = 0xec,
    337  1.6  christos     .ki_dsize = 0xf0,
    338  1.6  christos     .ki_ssize = 0xf4,
    339  1.6  christos     .ki_start = 0x118,
    340  1.6  christos     .ki_nice = 0x145,
    341  1.6  christos     .ki_comm = 0x17f,
    342  1.6  christos     .ki_tdev = 0x1f0,
    343  1.6  christos     .ki_rusage = 0x220,
    344  1.6  christos     .ki_rusage_ch = 0x278,
    345  1.6  christos 
    346  1.6  christos     .ru_utime = 0x0,
    347  1.6  christos     .ru_stime = 0x10,
    348  1.6  christos     .ru_maxrss = 0x20,
    349  1.6  christos     .ru_minflt = 0x30,
    350  1.6  christos     .ru_majflt = 0x34,
    351  1.6  christos   };
    352  1.6  christos 
    353  1.6  christos const struct kinfo_proc_layout kinfo_proc_layout_i386 =
    354  1.6  christos   {
    355  1.6  christos     .ki_layout = 0x4,
    356  1.6  christos     .ki_pid = 0x28,
    357  1.6  christos     .ki_ppid = 0x2c,
    358  1.6  christos     .ki_pgid = 0x30,
    359  1.6  christos     .ki_tpgid = 0x34,
    360  1.6  christos     .ki_sid = 0x38,
    361  1.6  christos     .ki_tdev_freebsd11 = 0x44,
    362  1.6  christos     .ki_sigignore = 0x68,
    363  1.6  christos     .ki_sigcatch = 0x78,
    364  1.6  christos     .ki_uid = 0x88,
    365  1.6  christos     .ki_ruid = 0x8c,
    366  1.6  christos     .ki_svuid = 0x90,
    367  1.6  christos     .ki_rgid = 0x94,
    368  1.6  christos     .ki_svgid = 0x98,
    369  1.6  christos     .ki_ngroups = 0x9c,
    370  1.6  christos     .ki_groups = 0xa0,
    371  1.6  christos     .ki_size = 0xe0,
    372  1.6  christos     .ki_rssize = 0xe4,
    373  1.6  christos     .ki_tsize = 0xec,
    374  1.6  christos     .ki_dsize = 0xf0,
    375  1.6  christos     .ki_ssize = 0xf4,
    376  1.6  christos     .ki_start = 0x118,
    377  1.6  christos     .ki_nice = 0x135,
    378  1.6  christos     .ki_comm = 0x16f,
    379  1.6  christos     .ki_tdev = 0x1e0,
    380  1.6  christos     .ki_rusage = 0x210,
    381  1.6  christos     .ki_rusage_ch = 0x258,
    382  1.6  christos 
    383  1.6  christos     .ru_utime = 0x0,
    384  1.6  christos     .ru_stime = 0x8,
    385  1.6  christos     .ru_maxrss = 0x10,
    386  1.6  christos     .ru_minflt = 0x20,
    387  1.6  christos     .ru_majflt = 0x24,
    388  1.6  christos   };
    389  1.6  christos 
    390  1.6  christos const struct kinfo_proc_layout kinfo_proc_layout_64 =
    391  1.6  christos   {
    392  1.6  christos     .ki_layout = 0x4,
    393  1.6  christos     .ki_pid = 0x48,
    394  1.6  christos     .ki_ppid = 0x4c,
    395  1.6  christos     .ki_pgid = 0x50,
    396  1.6  christos     .ki_tpgid = 0x54,
    397  1.6  christos     .ki_sid = 0x58,
    398  1.6  christos     .ki_tdev_freebsd11 = 0x64,
    399  1.6  christos     .ki_sigignore = 0x88,
    400  1.6  christos     .ki_sigcatch = 0x98,
    401  1.6  christos     .ki_uid = 0xa8,
    402  1.6  christos     .ki_ruid = 0xac,
    403  1.6  christos     .ki_svuid = 0xb0,
    404  1.6  christos     .ki_rgid = 0xb4,
    405  1.6  christos     .ki_svgid = 0xb8,
    406  1.6  christos     .ki_ngroups = 0xbc,
    407  1.6  christos     .ki_groups = 0xc0,
    408  1.6  christos     .ki_size = 0x100,
    409  1.6  christos     .ki_rssize = 0x108,
    410  1.6  christos     .ki_tsize = 0x118,
    411  1.6  christos     .ki_dsize = 0x120,
    412  1.6  christos     .ki_ssize = 0x128,
    413  1.6  christos     .ki_start = 0x150,
    414  1.6  christos     .ki_nice = 0x185,
    415  1.6  christos     .ki_comm = 0x1bf,
    416  1.6  christos     .ki_tdev = 0x230,
    417  1.6  christos     .ki_rusage = 0x260,
    418  1.6  christos     .ki_rusage_ch = 0x2f0,
    419  1.6  christos 
    420  1.6  christos     .ru_utime = 0x0,
    421  1.6  christos     .ru_stime = 0x10,
    422  1.6  christos     .ru_maxrss = 0x20,
    423  1.6  christos     .ru_minflt = 0x40,
    424  1.6  christos     .ru_majflt = 0x48,
    425  1.6  christos   };
    426  1.6  christos 
    427  1.6  christos static struct gdbarch_data *fbsd_gdbarch_data_handle;
    428  1.6  christos 
    429  1.6  christos struct fbsd_gdbarch_data
    430  1.6  christos   {
    431  1.6  christos     struct type *siginfo_type;
    432  1.6  christos   };
    433  1.6  christos 
    434  1.6  christos static void *
    435  1.6  christos init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
    436  1.6  christos {
    437  1.6  christos   return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
    438  1.6  christos }
    439  1.6  christos 
    440  1.6  christos static struct fbsd_gdbarch_data *
    441  1.6  christos get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
    442  1.6  christos {
    443  1.6  christos   return ((struct fbsd_gdbarch_data *)
    444  1.6  christos 	  gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
    445  1.6  christos }
    446  1.1  christos 
    447  1.4  christos /* This is how we want PTIDs from core files to be printed.  */
    448  1.4  christos 
    449  1.5  christos static const char *
    450  1.4  christos fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
    451  1.4  christos {
    452  1.4  christos   static char buf[80];
    453  1.4  christos 
    454  1.6  christos   if (ptid.lwp () != 0)
    455  1.4  christos     {
    456  1.6  christos       xsnprintf (buf, sizeof buf, "LWP %ld", ptid.lwp ());
    457  1.4  christos       return buf;
    458  1.4  christos     }
    459  1.4  christos 
    460  1.4  christos   return normal_pid_to_str (ptid);
    461  1.4  christos }
    462  1.4  christos 
    463  1.4  christos /* Extract the name assigned to a thread from a core.  Returns the
    464  1.4  christos    string in a static buffer.  */
    465  1.4  christos 
    466  1.4  christos static const char *
    467  1.4  christos fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
    468  1.4  christos {
    469  1.4  christos   static char buf[80];
    470  1.4  christos   struct bfd_section *section;
    471  1.4  christos   bfd_size_type size;
    472  1.4  christos 
    473  1.6  christos   if (thr->ptid.lwp () != 0)
    474  1.4  christos     {
    475  1.4  christos       /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
    476  1.4  christos 	 whose contents are defined by a "struct thrmisc" declared in
    477  1.4  christos 	 <sys/procfs.h> on FreeBSD.  The per-thread name is stored as
    478  1.4  christos 	 a null-terminated string as the first member of the
    479  1.4  christos 	 structure.  Rather than define the full structure here, just
    480  1.4  christos 	 extract the null-terminated name from the start of the
    481  1.4  christos 	 note.  */
    482  1.6  christos       thread_section_name section_name (".thrmisc", thr->ptid);
    483  1.6  christos 
    484  1.6  christos       section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
    485  1.4  christos       if (section != NULL && bfd_section_size (core_bfd, section) > 0)
    486  1.4  christos 	{
    487  1.4  christos 	  /* Truncate the name if it is longer than "buf".  */
    488  1.4  christos 	  size = bfd_section_size (core_bfd, section);
    489  1.4  christos 	  if (size > sizeof buf - 1)
    490  1.4  christos 	    size = sizeof buf - 1;
    491  1.4  christos 	  if (bfd_get_section_contents (core_bfd, section, buf, (file_ptr) 0,
    492  1.4  christos 					size)
    493  1.4  christos 	      && buf[0] != '\0')
    494  1.4  christos 	    {
    495  1.4  christos 	      buf[size] = '\0';
    496  1.4  christos 
    497  1.4  christos 	      /* Note that each thread will report the process command
    498  1.4  christos 		 as its thread name instead of an empty name if a name
    499  1.4  christos 		 has not been set explicitly.  Return a NULL name in
    500  1.4  christos 		 that case.  */
    501  1.4  christos 	      if (strcmp (buf, elf_tdata (core_bfd)->core->program) != 0)
    502  1.4  christos 		return buf;
    503  1.4  christos 	    }
    504  1.4  christos 	}
    505  1.4  christos     }
    506  1.4  christos 
    507  1.4  christos   return NULL;
    508  1.4  christos }
    509  1.4  christos 
    510  1.6  christos /* Implement the "core_xfer_siginfo" gdbarch method.  */
    511  1.6  christos 
    512  1.6  christos static LONGEST
    513  1.6  christos fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
    514  1.6  christos 			ULONGEST offset, ULONGEST len)
    515  1.6  christos {
    516  1.6  christos   size_t siginfo_size;
    517  1.6  christos 
    518  1.6  christos   if (gdbarch_long_bit (gdbarch) == 32)
    519  1.6  christos     siginfo_size = SIZE32_SIGINFO_T;
    520  1.6  christos   else
    521  1.6  christos     siginfo_size = SIZE64_SIGINFO_T;
    522  1.6  christos   if (offset > siginfo_size)
    523  1.6  christos     return -1;
    524  1.6  christos 
    525  1.6  christos   thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
    526  1.6  christos   asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
    527  1.6  christos   if (section == NULL)
    528  1.6  christos     return -1;
    529  1.6  christos 
    530  1.6  christos   gdb_byte buf[4];
    531  1.6  christos   if (!bfd_get_section_contents (core_bfd, section, buf,
    532  1.6  christos 				 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
    533  1.6  christos     return -1;
    534  1.6  christos 
    535  1.6  christos   int pl_flags = extract_signed_integer (buf, 4, gdbarch_byte_order (gdbarch));
    536  1.6  christos   if (!(pl_flags & PL_FLAG_SI))
    537  1.6  christos     return -1;
    538  1.6  christos 
    539  1.6  christos   if (offset + len > siginfo_size)
    540  1.6  christos     len = siginfo_size - offset;
    541  1.6  christos 
    542  1.6  christos   ULONGEST siginfo_offset;
    543  1.6  christos   if (gdbarch_long_bit (gdbarch) == 32)
    544  1.6  christos     siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
    545  1.6  christos   else
    546  1.6  christos     siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
    547  1.6  christos 
    548  1.6  christos   if (!bfd_get_section_contents (core_bfd, section, readbuf,
    549  1.6  christos 				 siginfo_offset + offset, len))
    550  1.6  christos     return -1;
    551  1.6  christos 
    552  1.6  christos   return len;
    553  1.6  christos }
    554  1.6  christos 
    555  1.1  christos static int
    556  1.1  christos find_signalled_thread (struct thread_info *info, void *data)
    557  1.1  christos {
    558  1.1  christos   if (info->suspend.stop_signal != GDB_SIGNAL_0
    559  1.6  christos       && info->ptid.pid () == inferior_ptid.pid ())
    560  1.1  christos     return 1;
    561  1.1  christos 
    562  1.1  christos   return 0;
    563  1.1  christos }
    564  1.1  christos 
    565  1.4  christos /* Structure for passing information from
    566  1.4  christos    fbsd_collect_thread_registers via an iterator to
    567  1.4  christos    fbsd_collect_regset_section_cb. */
    568  1.1  christos 
    569  1.1  christos struct fbsd_collect_regset_section_cb_data
    570  1.1  christos {
    571  1.1  christos   const struct regcache *regcache;
    572  1.1  christos   bfd *obfd;
    573  1.1  christos   char *note_data;
    574  1.1  christos   int *note_size;
    575  1.4  christos   unsigned long lwp;
    576  1.4  christos   enum gdb_signal stop_signal;
    577  1.4  christos   int abort_iteration;
    578  1.1  christos };
    579  1.1  christos 
    580  1.1  christos static void
    581  1.6  christos fbsd_collect_regset_section_cb (const char *sect_name, int supply_size,
    582  1.6  christos 				int collect_size, const struct regset *regset,
    583  1.1  christos 				const char *human_name, void *cb_data)
    584  1.1  christos {
    585  1.1  christos   char *buf;
    586  1.4  christos   struct fbsd_collect_regset_section_cb_data *data
    587  1.4  christos     = (struct fbsd_collect_regset_section_cb_data *) cb_data;
    588  1.4  christos 
    589  1.4  christos   if (data->abort_iteration)
    590  1.4  christos     return;
    591  1.1  christos 
    592  1.1  christos   gdb_assert (regset->collect_regset);
    593  1.1  christos 
    594  1.6  christos   buf = (char *) xmalloc (collect_size);
    595  1.6  christos   regset->collect_regset (regset, data->regcache, -1, buf, collect_size);
    596  1.1  christos 
    597  1.1  christos   /* PRSTATUS still needs to be treated specially.  */
    598  1.1  christos   if (strcmp (sect_name, ".reg") == 0)
    599  1.1  christos     data->note_data = (char *) elfcore_write_prstatus
    600  1.4  christos       (data->obfd, data->note_data, data->note_size, data->lwp,
    601  1.4  christos        gdb_signal_to_host (data->stop_signal), buf);
    602  1.1  christos   else
    603  1.1  christos     data->note_data = (char *) elfcore_write_register_note
    604  1.1  christos       (data->obfd, data->note_data, data->note_size,
    605  1.6  christos        sect_name, buf, collect_size);
    606  1.1  christos   xfree (buf);
    607  1.4  christos 
    608  1.4  christos   if (data->note_data == NULL)
    609  1.4  christos     data->abort_iteration = 1;
    610  1.4  christos }
    611  1.4  christos 
    612  1.4  christos /* Records the thread's register state for the corefile note
    613  1.4  christos    section.  */
    614  1.4  christos 
    615  1.4  christos static char *
    616  1.4  christos fbsd_collect_thread_registers (const struct regcache *regcache,
    617  1.4  christos 			       ptid_t ptid, bfd *obfd,
    618  1.4  christos 			       char *note_data, int *note_size,
    619  1.4  christos 			       enum gdb_signal stop_signal)
    620  1.4  christos {
    621  1.6  christos   struct gdbarch *gdbarch = regcache->arch ();
    622  1.4  christos   struct fbsd_collect_regset_section_cb_data data;
    623  1.4  christos 
    624  1.4  christos   data.regcache = regcache;
    625  1.4  christos   data.obfd = obfd;
    626  1.4  christos   data.note_data = note_data;
    627  1.4  christos   data.note_size = note_size;
    628  1.4  christos   data.stop_signal = stop_signal;
    629  1.4  christos   data.abort_iteration = 0;
    630  1.6  christos   data.lwp = ptid.lwp ();
    631  1.4  christos 
    632  1.4  christos   gdbarch_iterate_over_regset_sections (gdbarch,
    633  1.4  christos 					fbsd_collect_regset_section_cb,
    634  1.4  christos 					&data, regcache);
    635  1.4  christos   return data.note_data;
    636  1.4  christos }
    637  1.4  christos 
    638  1.4  christos struct fbsd_corefile_thread_data
    639  1.4  christos {
    640  1.4  christos   struct gdbarch *gdbarch;
    641  1.4  christos   bfd *obfd;
    642  1.4  christos   char *note_data;
    643  1.4  christos   int *note_size;
    644  1.4  christos   enum gdb_signal stop_signal;
    645  1.4  christos };
    646  1.4  christos 
    647  1.4  christos /* Records the thread's register state for the corefile note
    648  1.4  christos    section.  */
    649  1.4  christos 
    650  1.4  christos static void
    651  1.4  christos fbsd_corefile_thread (struct thread_info *info,
    652  1.4  christos 		      struct fbsd_corefile_thread_data *args)
    653  1.4  christos {
    654  1.4  christos   struct regcache *regcache;
    655  1.4  christos 
    656  1.4  christos   regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
    657  1.4  christos 
    658  1.4  christos   target_fetch_registers (regcache, -1);
    659  1.4  christos 
    660  1.4  christos   args->note_data = fbsd_collect_thread_registers
    661  1.4  christos     (regcache, info->ptid, args->obfd, args->note_data,
    662  1.4  christos      args->note_size, args->stop_signal);
    663  1.1  christos }
    664  1.1  christos 
    665  1.6  christos /* Return a byte_vector containing the contents of a core dump note
    666  1.6  christos    for the target object of type OBJECT.  If STRUCTSIZE is non-zero,
    667  1.6  christos    the data is prefixed with a 32-bit integer size to match the format
    668  1.6  christos    used in FreeBSD NT_PROCSTAT_* notes.  */
    669  1.6  christos 
    670  1.6  christos static gdb::optional<gdb::byte_vector>
    671  1.6  christos fbsd_make_note_desc (enum target_object object, uint32_t structsize)
    672  1.6  christos {
    673  1.6  christos   gdb::optional<gdb::byte_vector> buf =
    674  1.6  christos     target_read_alloc (current_top_target (), object, NULL);
    675  1.6  christos   if (!buf || buf->empty ())
    676  1.6  christos     return {};
    677  1.6  christos 
    678  1.6  christos   if (structsize == 0)
    679  1.6  christos     return buf;
    680  1.6  christos 
    681  1.6  christos   gdb::byte_vector desc (sizeof (structsize) + buf->size ());
    682  1.6  christos   memcpy (desc.data (), &structsize, sizeof (structsize));
    683  1.6  christos   memcpy (desc.data () + sizeof (structsize), buf->data (), buf->size ());
    684  1.6  christos   return desc;
    685  1.6  christos }
    686  1.6  christos 
    687  1.1  christos /* Create appropriate note sections for a corefile, returning them in
    688  1.1  christos    allocated memory.  */
    689  1.1  christos 
    690  1.1  christos static char *
    691  1.1  christos fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
    692  1.1  christos {
    693  1.4  christos   struct fbsd_corefile_thread_data thread_args;
    694  1.4  christos   char *note_data = NULL;
    695  1.1  christos   Elf_Internal_Ehdr *i_ehdrp;
    696  1.6  christos   struct thread_info *curr_thr, *signalled_thr;
    697  1.1  christos 
    698  1.1  christos   /* Put a "FreeBSD" label in the ELF header.  */
    699  1.1  christos   i_ehdrp = elf_elfheader (obfd);
    700  1.1  christos   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
    701  1.1  christos 
    702  1.1  christos   gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
    703  1.1  christos 
    704  1.1  christos   if (get_exec_file (0))
    705  1.1  christos     {
    706  1.1  christos       const char *fname = lbasename (get_exec_file (0));
    707  1.1  christos       char *psargs = xstrdup (fname);
    708  1.1  christos 
    709  1.1  christos       if (get_inferior_args ())
    710  1.1  christos 	psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
    711  1.1  christos 			   (char *) NULL);
    712  1.1  christos 
    713  1.1  christos       note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
    714  1.1  christos 					  fname, psargs);
    715  1.1  christos     }
    716  1.1  christos 
    717  1.4  christos   /* Thread register information.  */
    718  1.4  christos   TRY
    719  1.4  christos     {
    720  1.4  christos       update_thread_list ();
    721  1.4  christos     }
    722  1.4  christos   CATCH (e, RETURN_MASK_ERROR)
    723  1.4  christos     {
    724  1.4  christos       exception_print (gdb_stderr, e);
    725  1.4  christos     }
    726  1.4  christos   END_CATCH
    727  1.4  christos 
    728  1.4  christos   /* Like the kernel, prefer dumping the signalled thread first.
    729  1.4  christos      "First thread" is what tools use to infer the signalled thread.
    730  1.4  christos      In case there's more than one signalled thread, prefer the
    731  1.4  christos      current thread, if it is signalled.  */
    732  1.4  christos   curr_thr = inferior_thread ();
    733  1.4  christos   if (curr_thr->suspend.stop_signal != GDB_SIGNAL_0)
    734  1.4  christos     signalled_thr = curr_thr;
    735  1.4  christos   else
    736  1.4  christos     {
    737  1.4  christos       signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
    738  1.4  christos       if (signalled_thr == NULL)
    739  1.4  christos 	signalled_thr = curr_thr;
    740  1.4  christos     }
    741  1.4  christos 
    742  1.4  christos   thread_args.gdbarch = gdbarch;
    743  1.4  christos   thread_args.obfd = obfd;
    744  1.4  christos   thread_args.note_data = note_data;
    745  1.4  christos   thread_args.note_size = note_size;
    746  1.4  christos   thread_args.stop_signal = signalled_thr->suspend.stop_signal;
    747  1.4  christos 
    748  1.4  christos   fbsd_corefile_thread (signalled_thr, &thread_args);
    749  1.6  christos   for (thread_info *thr : current_inferior ()->non_exited_threads ())
    750  1.4  christos     {
    751  1.4  christos       if (thr == signalled_thr)
    752  1.4  christos 	continue;
    753  1.4  christos 
    754  1.4  christos       fbsd_corefile_thread (thr, &thread_args);
    755  1.4  christos     }
    756  1.4  christos 
    757  1.4  christos   note_data = thread_args.note_data;
    758  1.4  christos 
    759  1.6  christos   /* Auxiliary vector.  */
    760  1.6  christos   uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo  */
    761  1.6  christos   gdb::optional<gdb::byte_vector> note_desc =
    762  1.6  christos     fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
    763  1.6  christos   if (note_desc && !note_desc->empty ())
    764  1.6  christos     {
    765  1.6  christos       note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
    766  1.6  christos 				      NT_FREEBSD_PROCSTAT_AUXV,
    767  1.6  christos 				      note_desc->data (), note_desc->size ());
    768  1.6  christos       if (!note_data)
    769  1.6  christos 	return NULL;
    770  1.6  christos     }
    771  1.6  christos 
    772  1.6  christos   /* Virtual memory mappings.  */
    773  1.6  christos   note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
    774  1.6  christos   if (note_desc && !note_desc->empty ())
    775  1.6  christos     {
    776  1.6  christos       note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
    777  1.6  christos 				      NT_FREEBSD_PROCSTAT_VMMAP,
    778  1.6  christos 				      note_desc->data (), note_desc->size ());
    779  1.6  christos       if (!note_data)
    780  1.6  christos 	return NULL;
    781  1.6  christos     }
    782  1.6  christos 
    783  1.6  christos   note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
    784  1.6  christos   if (note_desc && !note_desc->empty ())
    785  1.6  christos     {
    786  1.6  christos       note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
    787  1.6  christos 				      NT_FREEBSD_PROCSTAT_PSSTRINGS,
    788  1.6  christos 				      note_desc->data (), note_desc->size ());
    789  1.6  christos       if (!note_data)
    790  1.6  christos 	return NULL;
    791  1.6  christos     }
    792  1.6  christos 
    793  1.1  christos   return note_data;
    794  1.1  christos }
    795  1.1  christos 
    796  1.6  christos /* Helper function to generate the file descriptor description for a
    797  1.6  christos    single open file in 'info proc files'.  */
    798  1.6  christos 
    799  1.6  christos static const char *
    800  1.6  christos fbsd_file_fd (int kf_fd)
    801  1.6  christos {
    802  1.6  christos   switch (kf_fd)
    803  1.6  christos     {
    804  1.6  christos     case KINFO_FILE_FD_TYPE_CWD:
    805  1.6  christos       return "cwd";
    806  1.6  christos     case KINFO_FILE_FD_TYPE_ROOT:
    807  1.6  christos       return "root";
    808  1.6  christos     case KINFO_FILE_FD_TYPE_JAIL:
    809  1.6  christos       return "jail";
    810  1.6  christos     case KINFO_FILE_FD_TYPE_TRACE:
    811  1.6  christos       return "trace";
    812  1.6  christos     case KINFO_FILE_FD_TYPE_TEXT:
    813  1.6  christos       return "text";
    814  1.6  christos     case KINFO_FILE_FD_TYPE_CTTY:
    815  1.6  christos       return "ctty";
    816  1.6  christos     default:
    817  1.6  christos       return int_string (kf_fd, 10, 1, 0, 0);
    818  1.6  christos     }
    819  1.6  christos }
    820  1.6  christos 
    821  1.6  christos /* Helper function to generate the file type for a single open file in
    822  1.6  christos    'info proc files'.  */
    823  1.6  christos 
    824  1.6  christos static const char *
    825  1.6  christos fbsd_file_type (int kf_type, int kf_vnode_type)
    826  1.6  christos {
    827  1.6  christos   switch (kf_type)
    828  1.6  christos     {
    829  1.6  christos     case KINFO_FILE_TYPE_VNODE:
    830  1.6  christos       switch (kf_vnode_type)
    831  1.6  christos 	{
    832  1.6  christos 	case KINFO_FILE_VTYPE_VREG:
    833  1.6  christos 	  return "file";
    834  1.6  christos 	case KINFO_FILE_VTYPE_VDIR:
    835  1.6  christos 	  return "dir";
    836  1.6  christos 	case KINFO_FILE_VTYPE_VCHR:
    837  1.6  christos 	  return "chr";
    838  1.6  christos 	case KINFO_FILE_VTYPE_VLNK:
    839  1.6  christos 	  return "link";
    840  1.6  christos 	case KINFO_FILE_VTYPE_VSOCK:
    841  1.6  christos 	  return "socket";
    842  1.6  christos 	case KINFO_FILE_VTYPE_VFIFO:
    843  1.6  christos 	  return "fifo";
    844  1.6  christos 	default:
    845  1.6  christos 	  {
    846  1.6  christos 	    char *str = get_print_cell ();
    847  1.6  christos 
    848  1.6  christos 	    xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
    849  1.6  christos 	    return str;
    850  1.6  christos 	  }
    851  1.6  christos 	}
    852  1.6  christos     case KINFO_FILE_TYPE_SOCKET:
    853  1.6  christos       return "socket";
    854  1.6  christos     case KINFO_FILE_TYPE_PIPE:
    855  1.6  christos       return "pipe";
    856  1.6  christos     case KINFO_FILE_TYPE_FIFO:
    857  1.6  christos       return "fifo";
    858  1.6  christos     case KINFO_FILE_TYPE_KQUEUE:
    859  1.6  christos       return "kqueue";
    860  1.6  christos     case KINFO_FILE_TYPE_CRYPTO:
    861  1.6  christos       return "crypto";
    862  1.6  christos     case KINFO_FILE_TYPE_MQUEUE:
    863  1.6  christos       return "mqueue";
    864  1.6  christos     case KINFO_FILE_TYPE_SHM:
    865  1.6  christos       return "shm";
    866  1.6  christos     case KINFO_FILE_TYPE_SEM:
    867  1.6  christos       return "sem";
    868  1.6  christos     case KINFO_FILE_TYPE_PTS:
    869  1.6  christos       return "pts";
    870  1.6  christos     case KINFO_FILE_TYPE_PROCDESC:
    871  1.6  christos       return "proc";
    872  1.6  christos     default:
    873  1.6  christos       return int_string (kf_type, 10, 1, 0, 0);
    874  1.6  christos     }
    875  1.6  christos }
    876  1.6  christos 
    877  1.6  christos /* Helper function to generate the file flags for a single open file in
    878  1.6  christos    'info proc files'.  */
    879  1.6  christos 
    880  1.6  christos static const char *
    881  1.6  christos fbsd_file_flags (int kf_flags)
    882  1.6  christos {
    883  1.6  christos   static char file_flags[10];
    884  1.6  christos 
    885  1.6  christos   file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
    886  1.6  christos   file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
    887  1.6  christos   file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
    888  1.6  christos   file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
    889  1.6  christos   file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
    890  1.6  christos   file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
    891  1.6  christos   file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
    892  1.6  christos   file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
    893  1.6  christos   file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
    894  1.6  christos   file_flags[9] = '\0';
    895  1.6  christos 
    896  1.6  christos   return file_flags;
    897  1.6  christos }
    898  1.6  christos 
    899  1.6  christos /* Helper function to generate the name of an IP protocol.  */
    900  1.6  christos 
    901  1.6  christos static const char *
    902  1.6  christos fbsd_ipproto (int protocol)
    903  1.6  christos {
    904  1.6  christos   switch (protocol)
    905  1.6  christos     {
    906  1.6  christos     case FBSD_IPPROTO_ICMP:
    907  1.6  christos       return "icmp";
    908  1.6  christos     case FBSD_IPPROTO_TCP:
    909  1.6  christos       return "tcp";
    910  1.6  christos     case FBSD_IPPROTO_UDP:
    911  1.6  christos       return "udp";
    912  1.6  christos     case FBSD_IPPROTO_SCTP:
    913  1.6  christos       return "sctp";
    914  1.6  christos     default:
    915  1.6  christos       {
    916  1.6  christos 	char *str = get_print_cell ();
    917  1.6  christos 
    918  1.6  christos 	xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
    919  1.6  christos 	return str;
    920  1.6  christos       }
    921  1.6  christos     }
    922  1.6  christos }
    923  1.6  christos 
    924  1.6  christos /* Helper function to print out an IPv4 socket address.  */
    925  1.6  christos 
    926  1.6  christos static void
    927  1.6  christos fbsd_print_sockaddr_in (const void *sockaddr)
    928  1.6  christos {
    929  1.6  christos   const struct fbsd_sockaddr_in *sin =
    930  1.6  christos     reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
    931  1.6  christos   char buf[INET_ADDRSTRLEN];
    932  1.6  christos 
    933  1.6  christos   if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
    934  1.6  christos     error (_("Failed to format IPv4 address"));
    935  1.6  christos   printf_filtered ("%s:%u", buf,
    936  1.6  christos 		   (sin->sin_port[0] << 8) | sin->sin_port[1]);
    937  1.6  christos }
    938  1.6  christos 
    939  1.6  christos /* Helper function to print out an IPv6 socket address.  */
    940  1.6  christos 
    941  1.6  christos static void
    942  1.6  christos fbsd_print_sockaddr_in6 (const void *sockaddr)
    943  1.6  christos {
    944  1.6  christos   const struct fbsd_sockaddr_in6 *sin6 =
    945  1.6  christos     reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
    946  1.6  christos   char buf[INET6_ADDRSTRLEN];
    947  1.6  christos 
    948  1.6  christos   if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
    949  1.6  christos     error (_("Failed to format IPv6 address"));
    950  1.6  christos   printf_filtered ("%s.%u", buf,
    951  1.6  christos 		   (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
    952  1.6  christos }
    953  1.6  christos 
    954  1.6  christos /* See fbsd-tdep.h.  */
    955  1.6  christos 
    956  1.6  christos void
    957  1.6  christos fbsd_info_proc_files_header ()
    958  1.6  christos {
    959  1.6  christos   printf_filtered (_("Open files:\n\n"));
    960  1.6  christos   printf_filtered ("  %6s %6s %10s %9s %s\n",
    961  1.6  christos 		   "FD", "Type", "Offset", "Flags  ", "Name");
    962  1.6  christos }
    963  1.6  christos 
    964  1.6  christos /* See fbsd-tdep.h.  */
    965  1.6  christos 
    966  1.6  christos void
    967  1.6  christos fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
    968  1.6  christos 			    LONGEST kf_offset, int kf_vnode_type,
    969  1.6  christos 			    int kf_sock_domain, int kf_sock_type,
    970  1.6  christos 			    int kf_sock_protocol, const void *kf_sa_local,
    971  1.6  christos 			    const void *kf_sa_peer, const void *kf_path)
    972  1.6  christos {
    973  1.6  christos   printf_filtered ("  %6s %6s %10s %8s ",
    974  1.6  christos 		   fbsd_file_fd (kf_fd),
    975  1.6  christos 		   fbsd_file_type (kf_type, kf_vnode_type),
    976  1.6  christos 		   kf_offset > -1 ? hex_string (kf_offset) : "-",
    977  1.6  christos 		   fbsd_file_flags (kf_flags));
    978  1.6  christos   if (kf_type == KINFO_FILE_TYPE_SOCKET)
    979  1.6  christos     {
    980  1.6  christos       switch (kf_sock_domain)
    981  1.6  christos 	{
    982  1.6  christos 	case FBSD_AF_UNIX:
    983  1.6  christos 	  {
    984  1.6  christos 	    switch (kf_sock_type)
    985  1.6  christos 	      {
    986  1.6  christos 	      case FBSD_SOCK_STREAM:
    987  1.6  christos 		printf_filtered ("unix stream:");
    988  1.6  christos 		break;
    989  1.6  christos 	      case FBSD_SOCK_DGRAM:
    990  1.6  christos 		printf_filtered ("unix dgram:");
    991  1.6  christos 		break;
    992  1.6  christos 	      case FBSD_SOCK_SEQPACKET:
    993  1.6  christos 		printf_filtered ("unix seqpacket:");
    994  1.6  christos 		break;
    995  1.6  christos 	      default:
    996  1.6  christos 		printf_filtered ("unix <%d>:", kf_sock_type);
    997  1.6  christos 		break;
    998  1.6  christos 	      }
    999  1.6  christos 
   1000  1.6  christos 	    /* For local sockets, print out the first non-nul path
   1001  1.6  christos 	       rather than both paths.  */
   1002  1.6  christos 	    const struct fbsd_sockaddr_un *sun
   1003  1.6  christos 	      = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
   1004  1.6  christos 	    if (sun->sun_path[0] == 0)
   1005  1.6  christos 	      sun = reinterpret_cast<const struct fbsd_sockaddr_un *>
   1006  1.6  christos 		(kf_sa_peer);
   1007  1.6  christos 	    printf_filtered ("%s", sun->sun_path);
   1008  1.6  christos 	    break;
   1009  1.6  christos 	  }
   1010  1.6  christos 	case FBSD_AF_INET:
   1011  1.6  christos 	  printf_filtered ("%s4 ", fbsd_ipproto (kf_sock_protocol));
   1012  1.6  christos 	  fbsd_print_sockaddr_in (kf_sa_local);
   1013  1.6  christos 	  printf_filtered (" -> ");
   1014  1.6  christos 	  fbsd_print_sockaddr_in (kf_sa_peer);
   1015  1.6  christos 	  break;
   1016  1.6  christos 	case FBSD_AF_INET6:
   1017  1.6  christos 	  printf_filtered ("%s6 ", fbsd_ipproto (kf_sock_protocol));
   1018  1.6  christos 	  fbsd_print_sockaddr_in6 (kf_sa_local);
   1019  1.6  christos 	  printf_filtered (" -> ");
   1020  1.6  christos 	  fbsd_print_sockaddr_in6 (kf_sa_peer);
   1021  1.6  christos 	  break;
   1022  1.6  christos 	}
   1023  1.6  christos     }
   1024  1.6  christos   else
   1025  1.6  christos     printf_filtered ("%s", reinterpret_cast<const char *> (kf_path));
   1026  1.6  christos   printf_filtered ("\n");
   1027  1.6  christos }
   1028  1.6  christos 
   1029  1.6  christos /* Implement "info proc files" for a corefile.  */
   1030  1.6  christos 
   1031  1.6  christos static void
   1032  1.6  christos fbsd_core_info_proc_files (struct gdbarch *gdbarch)
   1033  1.6  christos {
   1034  1.6  christos   asection *section
   1035  1.6  christos     = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
   1036  1.6  christos   if (section == NULL)
   1037  1.6  christos     {
   1038  1.6  christos       warning (_("unable to find open files in core file"));
   1039  1.6  christos       return;
   1040  1.6  christos     }
   1041  1.6  christos 
   1042  1.6  christos   size_t note_size = bfd_get_section_size (section);
   1043  1.6  christos   if (note_size < 4)
   1044  1.6  christos     error (_("malformed core note - too short for header"));
   1045  1.6  christos 
   1046  1.6  christos   gdb::def_vector<unsigned char> contents (note_size);
   1047  1.6  christos   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
   1048  1.6  christos 				 0, note_size))
   1049  1.6  christos     error (_("could not get core note contents"));
   1050  1.6  christos 
   1051  1.6  christos   unsigned char *descdata = contents.data ();
   1052  1.6  christos   unsigned char *descend = descdata + note_size;
   1053  1.6  christos 
   1054  1.6  christos   /* Skip over the structure size.  */
   1055  1.6  christos   descdata += 4;
   1056  1.6  christos 
   1057  1.6  christos   fbsd_info_proc_files_header ();
   1058  1.6  christos 
   1059  1.6  christos   while (descdata + KF_PATH < descend)
   1060  1.6  christos     {
   1061  1.6  christos       ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
   1062  1.6  christos       if (structsize < KF_PATH)
   1063  1.6  christos 	error (_("malformed core note - file structure too small"));
   1064  1.6  christos 
   1065  1.6  christos       LONGEST type = bfd_get_signed_32 (core_bfd, descdata + KF_TYPE);
   1066  1.6  christos       LONGEST fd = bfd_get_signed_32 (core_bfd, descdata + KF_FD);
   1067  1.6  christos       LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KF_FLAGS);
   1068  1.6  christos       LONGEST offset = bfd_get_signed_64 (core_bfd, descdata + KF_OFFSET);
   1069  1.6  christos       LONGEST vnode_type = bfd_get_signed_32 (core_bfd,
   1070  1.6  christos 					      descdata + KF_VNODE_TYPE);
   1071  1.6  christos       LONGEST sock_domain = bfd_get_signed_32 (core_bfd,
   1072  1.6  christos 					       descdata + KF_SOCK_DOMAIN);
   1073  1.6  christos       LONGEST sock_type = bfd_get_signed_32 (core_bfd, descdata + KF_SOCK_TYPE);
   1074  1.6  christos       LONGEST sock_protocol = bfd_get_signed_32 (core_bfd,
   1075  1.6  christos 						 descdata + KF_SOCK_PROTOCOL);
   1076  1.6  christos       fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
   1077  1.6  christos 				  sock_domain, sock_type, sock_protocol,
   1078  1.6  christos 				  descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
   1079  1.6  christos 				  descdata + KF_PATH);
   1080  1.6  christos 
   1081  1.6  christos       descdata += structsize;
   1082  1.6  christos     }
   1083  1.6  christos }
   1084  1.6  christos 
   1085  1.6  christos /* Helper function to generate mappings flags for a single VM map
   1086  1.6  christos    entry in 'info proc mappings'.  */
   1087  1.6  christos 
   1088  1.6  christos static const char *
   1089  1.6  christos fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
   1090  1.6  christos {
   1091  1.6  christos   static char vm_flags[9];
   1092  1.6  christos 
   1093  1.6  christos   vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
   1094  1.6  christos   vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
   1095  1.6  christos   vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
   1096  1.6  christos   vm_flags[3] = ' ';
   1097  1.6  christos   vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
   1098  1.6  christos   vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
   1099  1.6  christos   vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
   1100  1.6  christos   vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
   1101  1.6  christos     : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
   1102  1.6  christos   vm_flags[8] = '\0';
   1103  1.6  christos 
   1104  1.6  christos   return vm_flags;
   1105  1.6  christos }
   1106  1.6  christos 
   1107  1.6  christos /* See fbsd-tdep.h.  */
   1108  1.6  christos 
   1109  1.6  christos void
   1110  1.6  christos fbsd_info_proc_mappings_header (int addr_bit)
   1111  1.6  christos {
   1112  1.6  christos   printf_filtered (_("Mapped address spaces:\n\n"));
   1113  1.6  christos   if (addr_bit == 64)
   1114  1.6  christos     {
   1115  1.6  christos       printf_filtered ("  %18s %18s %10s %10s %9s %s\n",
   1116  1.6  christos 		       "Start Addr",
   1117  1.6  christos 		       "  End Addr",
   1118  1.6  christos 		       "      Size", "    Offset", "Flags  ", "File");
   1119  1.6  christos     }
   1120  1.6  christos   else
   1121  1.6  christos     {
   1122  1.6  christos       printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
   1123  1.6  christos 		       "Start Addr",
   1124  1.6  christos 		       "  End Addr",
   1125  1.6  christos 		       "      Size", "    Offset", "Flags  ", "File");
   1126  1.6  christos     }
   1127  1.6  christos }
   1128  1.6  christos 
   1129  1.6  christos /* See fbsd-tdep.h.  */
   1130  1.6  christos 
   1131  1.6  christos void
   1132  1.6  christos fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
   1133  1.6  christos 			       ULONGEST kve_end, ULONGEST kve_offset,
   1134  1.6  christos 			       int kve_flags, int kve_protection,
   1135  1.6  christos 			       const void *kve_path)
   1136  1.6  christos {
   1137  1.6  christos   if (addr_bit == 64)
   1138  1.6  christos     {
   1139  1.6  christos       printf_filtered ("  %18s %18s %10s %10s %9s %s\n",
   1140  1.6  christos 		       hex_string (kve_start),
   1141  1.6  christos 		       hex_string (kve_end),
   1142  1.6  christos 		       hex_string (kve_end - kve_start),
   1143  1.6  christos 		       hex_string (kve_offset),
   1144  1.6  christos 		       fbsd_vm_map_entry_flags (kve_flags, kve_protection),
   1145  1.6  christos 		       reinterpret_cast<const char *> (kve_path));
   1146  1.6  christos     }
   1147  1.6  christos   else
   1148  1.6  christos     {
   1149  1.6  christos       printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
   1150  1.6  christos 		       hex_string (kve_start),
   1151  1.6  christos 		       hex_string (kve_end),
   1152  1.6  christos 		       hex_string (kve_end - kve_start),
   1153  1.6  christos 		       hex_string (kve_offset),
   1154  1.6  christos 		       fbsd_vm_map_entry_flags (kve_flags, kve_protection),
   1155  1.6  christos 		       reinterpret_cast<const char *> (kve_path));
   1156  1.6  christos     }
   1157  1.6  christos }
   1158  1.6  christos 
   1159  1.6  christos /* Implement "info proc mappings" for a corefile.  */
   1160  1.6  christos 
   1161  1.6  christos static void
   1162  1.6  christos fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
   1163  1.6  christos {
   1164  1.6  christos   asection *section;
   1165  1.6  christos   unsigned char *descdata, *descend;
   1166  1.6  christos   size_t note_size;
   1167  1.6  christos 
   1168  1.6  christos   section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.vmmap");
   1169  1.6  christos   if (section == NULL)
   1170  1.6  christos     {
   1171  1.6  christos       warning (_("unable to find mappings in core file"));
   1172  1.6  christos       return;
   1173  1.6  christos     }
   1174  1.6  christos 
   1175  1.6  christos   note_size = bfd_get_section_size (section);
   1176  1.6  christos   if (note_size < 4)
   1177  1.6  christos     error (_("malformed core note - too short for header"));
   1178  1.6  christos 
   1179  1.6  christos   gdb::def_vector<unsigned char> contents (note_size);
   1180  1.6  christos   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
   1181  1.6  christos 				 0, note_size))
   1182  1.6  christos     error (_("could not get core note contents"));
   1183  1.6  christos 
   1184  1.6  christos   descdata = contents.data ();
   1185  1.6  christos   descend = descdata + note_size;
   1186  1.6  christos 
   1187  1.6  christos   /* Skip over the structure size.  */
   1188  1.6  christos   descdata += 4;
   1189  1.6  christos 
   1190  1.6  christos   fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
   1191  1.6  christos   while (descdata + KVE_PATH < descend)
   1192  1.6  christos     {
   1193  1.6  christos       ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KVE_STRUCTSIZE);
   1194  1.6  christos       if (structsize < KVE_PATH)
   1195  1.6  christos 	error (_("malformed core note - vmmap entry too small"));
   1196  1.6  christos 
   1197  1.6  christos       ULONGEST start = bfd_get_64 (core_bfd, descdata + KVE_START);
   1198  1.6  christos       ULONGEST end = bfd_get_64 (core_bfd, descdata + KVE_END);
   1199  1.6  christos       ULONGEST offset = bfd_get_64 (core_bfd, descdata + KVE_OFFSET);
   1200  1.6  christos       LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KVE_FLAGS);
   1201  1.6  christos       LONGEST prot = bfd_get_signed_32 (core_bfd, descdata + KVE_PROTECTION);
   1202  1.6  christos       fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
   1203  1.6  christos 				     offset, flags, prot, descdata + KVE_PATH);
   1204  1.6  christos 
   1205  1.6  christos       descdata += structsize;
   1206  1.6  christos     }
   1207  1.6  christos }
   1208  1.6  christos 
   1209  1.6  christos /* Fetch the pathname of a vnode for a single file descriptor from the
   1210  1.6  christos    file table core note.  */
   1211  1.6  christos 
   1212  1.6  christos static gdb::unique_xmalloc_ptr<char>
   1213  1.6  christos fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
   1214  1.6  christos {
   1215  1.6  christos   asection *section;
   1216  1.6  christos   unsigned char *descdata, *descend;
   1217  1.6  christos   size_t note_size;
   1218  1.6  christos 
   1219  1.6  christos   section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
   1220  1.6  christos   if (section == NULL)
   1221  1.6  christos     return nullptr;
   1222  1.6  christos 
   1223  1.6  christos   note_size = bfd_get_section_size (section);
   1224  1.6  christos   if (note_size < 4)
   1225  1.6  christos     error (_("malformed core note - too short for header"));
   1226  1.6  christos 
   1227  1.6  christos   gdb::def_vector<unsigned char> contents (note_size);
   1228  1.6  christos   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
   1229  1.6  christos 				 0, note_size))
   1230  1.6  christos     error (_("could not get core note contents"));
   1231  1.6  christos 
   1232  1.6  christos   descdata = contents.data ();
   1233  1.6  christos   descend = descdata + note_size;
   1234  1.6  christos 
   1235  1.6  christos   /* Skip over the structure size.  */
   1236  1.6  christos   descdata += 4;
   1237  1.6  christos 
   1238  1.6  christos   while (descdata + KF_PATH < descend)
   1239  1.6  christos     {
   1240  1.6  christos       ULONGEST structsize;
   1241  1.6  christos 
   1242  1.6  christos       structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
   1243  1.6  christos       if (structsize < KF_PATH)
   1244  1.6  christos 	error (_("malformed core note - file structure too small"));
   1245  1.6  christos 
   1246  1.6  christos       if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
   1247  1.6  christos 	  && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
   1248  1.6  christos 	{
   1249  1.6  christos 	  char *path = (char *) descdata + KF_PATH;
   1250  1.6  christos 	  return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
   1251  1.6  christos 	}
   1252  1.6  christos 
   1253  1.6  christos       descdata += structsize;
   1254  1.6  christos     }
   1255  1.6  christos   return nullptr;
   1256  1.6  christos }
   1257  1.6  christos 
   1258  1.6  christos /* Helper function to read a struct timeval.  */
   1259  1.6  christos 
   1260  1.6  christos static void
   1261  1.6  christos fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
   1262  1.6  christos 			 LONGEST &sec, ULONGEST &usec)
   1263  1.6  christos {
   1264  1.6  christos   if (gdbarch_addr_bit (gdbarch) == 64)
   1265  1.6  christos     {
   1266  1.6  christos       sec = bfd_get_signed_64 (core_bfd, data);
   1267  1.6  christos       usec = bfd_get_64 (core_bfd, data + 8);
   1268  1.6  christos     }
   1269  1.6  christos   else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
   1270  1.6  christos     {
   1271  1.6  christos       sec = bfd_get_signed_32 (core_bfd, data);
   1272  1.6  christos       usec = bfd_get_32 (core_bfd, data + 4);
   1273  1.6  christos     }
   1274  1.6  christos   else
   1275  1.6  christos     {
   1276  1.6  christos       sec = bfd_get_signed_64 (core_bfd, data);
   1277  1.6  christos       usec = bfd_get_32 (core_bfd, data + 8);
   1278  1.6  christos     }
   1279  1.6  christos }
   1280  1.6  christos 
   1281  1.6  christos /* Print out the contents of a signal set.  */
   1282  1.6  christos 
   1283  1.6  christos static void
   1284  1.6  christos fbsd_print_sigset (const char *descr, unsigned char *sigset)
   1285  1.6  christos {
   1286  1.6  christos   printf_filtered ("%s: ", descr);
   1287  1.6  christos   for (int i = 0; i < SIG_WORDS; i++)
   1288  1.6  christos     printf_filtered ("%08x ",
   1289  1.6  christos 		     (unsigned int) bfd_get_32 (core_bfd, sigset + i * 4));
   1290  1.6  christos   printf_filtered ("\n");
   1291  1.6  christos }
   1292  1.6  christos 
   1293  1.6  christos /* Implement "info proc status" for a corefile.  */
   1294  1.6  christos 
   1295  1.6  christos static void
   1296  1.6  christos fbsd_core_info_proc_status (struct gdbarch *gdbarch)
   1297  1.6  christos {
   1298  1.6  christos   const struct kinfo_proc_layout *kp;
   1299  1.6  christos   asection *section;
   1300  1.6  christos   unsigned char *descdata;
   1301  1.6  christos   int addr_bit, long_bit;
   1302  1.6  christos   size_t note_size;
   1303  1.6  christos   ULONGEST value;
   1304  1.6  christos   LONGEST sec;
   1305  1.6  christos 
   1306  1.6  christos   section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.proc");
   1307  1.6  christos   if (section == NULL)
   1308  1.6  christos     {
   1309  1.6  christos       warning (_("unable to find process info in core file"));
   1310  1.6  christos       return;
   1311  1.6  christos     }
   1312  1.6  christos 
   1313  1.6  christos   addr_bit = gdbarch_addr_bit (gdbarch);
   1314  1.6  christos   if (addr_bit == 64)
   1315  1.6  christos     kp = &kinfo_proc_layout_64;
   1316  1.6  christos   else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
   1317  1.6  christos     kp = &kinfo_proc_layout_i386;
   1318  1.6  christos   else
   1319  1.6  christos     kp = &kinfo_proc_layout_32;
   1320  1.6  christos   long_bit = gdbarch_long_bit (gdbarch);
   1321  1.6  christos 
   1322  1.6  christos   /*
   1323  1.6  christos    * Ensure that the note is large enough for all of the fields fetched
   1324  1.6  christos    * by this function.  In particular, the note must contain the 32-bit
   1325  1.6  christos    * structure size, then it must be long enough to access the last
   1326  1.6  christos    * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
   1327  1.6  christos    */
   1328  1.6  christos   note_size = bfd_get_section_size (section);
   1329  1.6  christos   if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
   1330  1.6  christos 		   + long_bit / TARGET_CHAR_BIT))
   1331  1.6  christos     error (_("malformed core note - too short"));
   1332  1.6  christos 
   1333  1.6  christos   gdb::def_vector<unsigned char> contents (note_size);
   1334  1.6  christos   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
   1335  1.6  christos 				 0, note_size))
   1336  1.6  christos     error (_("could not get core note contents"));
   1337  1.6  christos 
   1338  1.6  christos   descdata = contents.data ();
   1339  1.6  christos 
   1340  1.6  christos   /* Skip over the structure size.  */
   1341  1.6  christos   descdata += 4;
   1342  1.6  christos 
   1343  1.6  christos   /* Verify 'ki_layout' is 0.  */
   1344  1.6  christos   if (bfd_get_32 (core_bfd, descdata + kp->ki_layout) != 0)
   1345  1.6  christos     {
   1346  1.6  christos       warning (_("unsupported process information in core file"));
   1347  1.6  christos       return;
   1348  1.6  christos     }
   1349  1.6  christos 
   1350  1.6  christos   printf_filtered ("Name: %.19s\n", descdata + kp->ki_comm);
   1351  1.6  christos   printf_filtered ("Process ID: %s\n",
   1352  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pid)));
   1353  1.6  christos   printf_filtered ("Parent process: %s\n",
   1354  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ppid)));
   1355  1.6  christos   printf_filtered ("Process group: %s\n",
   1356  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pgid)));
   1357  1.6  christos   printf_filtered ("Session id: %s\n",
   1358  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_sid)));
   1359  1.6  christos 
   1360  1.6  christos   /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'.  Older
   1361  1.6  christos      kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'.  In older
   1362  1.6  christos      kernels the 64-bit 'ki_tdev' field is in a reserved section of
   1363  1.6  christos      the structure that is cleared to zero.  Assume that a zero value
   1364  1.6  christos      in ki_tdev indicates a core dump from an older kernel and use the
   1365  1.6  christos      value in 'ki_tdev_freebsd11' instead.  */
   1366  1.6  christos   value = bfd_get_64 (core_bfd, descdata + kp->ki_tdev);
   1367  1.6  christos   if (value == 0)
   1368  1.6  christos     value = bfd_get_32 (core_bfd, descdata + kp->ki_tdev_freebsd11);
   1369  1.6  christos   printf_filtered ("TTY: %s\n", pulongest (value));
   1370  1.6  christos   printf_filtered ("TTY owner process group: %s\n",
   1371  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_tpgid)));
   1372  1.6  christos   printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
   1373  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ruid)),
   1374  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_uid)),
   1375  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svuid)));
   1376  1.6  christos   printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
   1377  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_rgid)),
   1378  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_groups)),
   1379  1.6  christos 		   pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svgid)));
   1380  1.6  christos   printf_filtered ("Groups: ");
   1381  1.6  christos   uint16_t ngroups = bfd_get_16 (core_bfd, descdata + kp->ki_ngroups);
   1382  1.6  christos   for (int i = 0; i < ngroups; i++)
   1383  1.6  christos     printf_filtered ("%s ",
   1384  1.6  christos 		     pulongest (bfd_get_32 (core_bfd,
   1385  1.6  christos 					    descdata + kp->ki_groups + i * 4)));
   1386  1.6  christos   printf_filtered ("\n");
   1387  1.6  christos   value = bfd_get (long_bit, core_bfd,
   1388  1.6  christos 		   descdata + kp->ki_rusage + kp->ru_minflt);
   1389  1.6  christos   printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value));
   1390  1.6  christos   value = bfd_get (long_bit, core_bfd,
   1391  1.6  christos 		   descdata + kp->ki_rusage_ch + kp->ru_minflt);
   1392  1.6  christos   printf_filtered ("Minor faults, children: %s\n", pulongest (value));
   1393  1.6  christos   value = bfd_get (long_bit, core_bfd,
   1394  1.6  christos 		   descdata + kp->ki_rusage + kp->ru_majflt);
   1395  1.6  christos   printf_filtered ("Major faults (memory page faults): %s\n",
   1396  1.6  christos 		   pulongest (value));
   1397  1.6  christos   value = bfd_get (long_bit, core_bfd,
   1398  1.6  christos 		   descdata + kp->ki_rusage_ch + kp->ru_majflt);
   1399  1.6  christos   printf_filtered ("Major faults, children: %s\n", pulongest (value));
   1400  1.6  christos   fbsd_core_fetch_timeval (gdbarch,
   1401  1.6  christos 			   descdata + kp->ki_rusage + kp->ru_utime,
   1402  1.6  christos 			   sec, value);
   1403  1.6  christos   printf_filtered ("utime: %s.%06d\n", plongest (sec), (int) value);
   1404  1.6  christos   fbsd_core_fetch_timeval (gdbarch,
   1405  1.6  christos 			   descdata + kp->ki_rusage + kp->ru_stime,
   1406  1.6  christos 			   sec, value);
   1407  1.6  christos   printf_filtered ("stime: %s.%06d\n", plongest (sec), (int) value);
   1408  1.6  christos   fbsd_core_fetch_timeval (gdbarch,
   1409  1.6  christos 			   descdata + kp->ki_rusage_ch + kp->ru_utime,
   1410  1.6  christos 			   sec, value);
   1411  1.6  christos   printf_filtered ("utime, children: %s.%06d\n", plongest (sec), (int) value);
   1412  1.6  christos   fbsd_core_fetch_timeval (gdbarch,
   1413  1.6  christos 			   descdata + kp->ki_rusage_ch + kp->ru_stime,
   1414  1.6  christos 			   sec, value);
   1415  1.6  christos   printf_filtered ("stime, children: %s.%06d\n", plongest (sec), (int) value);
   1416  1.6  christos   printf_filtered ("'nice' value: %d\n",
   1417  1.6  christos 		   bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
   1418  1.6  christos   fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
   1419  1.6  christos   printf_filtered ("Start time: %s.%06d\n", plongest (sec), (int) value);
   1420  1.6  christos   printf_filtered ("Virtual memory size: %s kB\n",
   1421  1.6  christos 		   pulongest (bfd_get (addr_bit, core_bfd,
   1422  1.6  christos 				       descdata + kp->ki_size) / 1024));
   1423  1.6  christos   printf_filtered ("Data size: %s pages\n",
   1424  1.6  christos 		   pulongest (bfd_get (addr_bit, core_bfd,
   1425  1.6  christos 				       descdata + kp->ki_dsize)));
   1426  1.6  christos   printf_filtered ("Stack size: %s pages\n",
   1427  1.6  christos 		   pulongest (bfd_get (addr_bit, core_bfd,
   1428  1.6  christos 				       descdata + kp->ki_ssize)));
   1429  1.6  christos   printf_filtered ("Text size: %s pages\n",
   1430  1.6  christos 		   pulongest (bfd_get (addr_bit, core_bfd,
   1431  1.6  christos 				       descdata + kp->ki_tsize)));
   1432  1.6  christos   printf_filtered ("Resident set size: %s pages\n",
   1433  1.6  christos 		   pulongest (bfd_get (addr_bit, core_bfd,
   1434  1.6  christos 				       descdata + kp->ki_rssize)));
   1435  1.6  christos   printf_filtered ("Maximum RSS: %s pages\n",
   1436  1.6  christos 		   pulongest (bfd_get (long_bit, core_bfd,
   1437  1.6  christos 				       descdata + kp->ki_rusage
   1438  1.6  christos 				       + kp->ru_maxrss)));
   1439  1.6  christos   fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
   1440  1.6  christos   fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
   1441  1.6  christos }
   1442  1.6  christos 
   1443  1.6  christos /* Implement the "core_info_proc" gdbarch method.  */
   1444  1.6  christos 
   1445  1.6  christos static void
   1446  1.6  christos fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
   1447  1.6  christos 		     enum info_proc_what what)
   1448  1.6  christos {
   1449  1.6  christos   bool do_cmdline = false;
   1450  1.6  christos   bool do_cwd = false;
   1451  1.6  christos   bool do_exe = false;
   1452  1.6  christos   bool do_files = false;
   1453  1.6  christos   bool do_mappings = false;
   1454  1.6  christos   bool do_status = false;
   1455  1.6  christos   int pid;
   1456  1.6  christos 
   1457  1.6  christos   switch (what)
   1458  1.6  christos     {
   1459  1.6  christos     case IP_MINIMAL:
   1460  1.6  christos       do_cmdline = true;
   1461  1.6  christos       do_cwd = true;
   1462  1.6  christos       do_exe = true;
   1463  1.6  christos       break;
   1464  1.6  christos     case IP_MAPPINGS:
   1465  1.6  christos       do_mappings = true;
   1466  1.6  christos       break;
   1467  1.6  christos     case IP_STATUS:
   1468  1.6  christos     case IP_STAT:
   1469  1.6  christos       do_status = true;
   1470  1.6  christos       break;
   1471  1.6  christos     case IP_CMDLINE:
   1472  1.6  christos       do_cmdline = true;
   1473  1.6  christos       break;
   1474  1.6  christos     case IP_EXE:
   1475  1.6  christos       do_exe = true;
   1476  1.6  christos       break;
   1477  1.6  christos     case IP_CWD:
   1478  1.6  christos       do_cwd = true;
   1479  1.6  christos       break;
   1480  1.6  christos     case IP_FILES:
   1481  1.6  christos       do_files = true;
   1482  1.6  christos       break;
   1483  1.6  christos     case IP_ALL:
   1484  1.6  christos       do_cmdline = true;
   1485  1.6  christos       do_cwd = true;
   1486  1.6  christos       do_exe = true;
   1487  1.6  christos       do_files = true;
   1488  1.6  christos       do_mappings = true;
   1489  1.6  christos       do_status = true;
   1490  1.6  christos       break;
   1491  1.6  christos     default:
   1492  1.6  christos       return;
   1493  1.6  christos     }
   1494  1.6  christos 
   1495  1.6  christos   pid = bfd_core_file_pid (core_bfd);
   1496  1.6  christos   if (pid != 0)
   1497  1.6  christos     printf_filtered (_("process %d\n"), pid);
   1498  1.6  christos 
   1499  1.6  christos   if (do_cmdline)
   1500  1.6  christos     {
   1501  1.6  christos       const char *cmdline;
   1502  1.6  christos 
   1503  1.6  christos       cmdline = bfd_core_file_failing_command (core_bfd);
   1504  1.6  christos       if (cmdline)
   1505  1.6  christos 	printf_filtered ("cmdline = '%s'\n", cmdline);
   1506  1.6  christos       else
   1507  1.6  christos 	warning (_("Command line unavailable"));
   1508  1.6  christos     }
   1509  1.6  christos   if (do_cwd)
   1510  1.6  christos     {
   1511  1.6  christos       gdb::unique_xmalloc_ptr<char> cwd =
   1512  1.6  christos 	fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
   1513  1.6  christos       if (cwd)
   1514  1.6  christos 	printf_filtered ("cwd = '%s'\n", cwd.get ());
   1515  1.6  christos       else
   1516  1.6  christos 	warning (_("unable to read current working directory"));
   1517  1.6  christos     }
   1518  1.6  christos   if (do_exe)
   1519  1.6  christos     {
   1520  1.6  christos       gdb::unique_xmalloc_ptr<char> exe =
   1521  1.6  christos 	fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
   1522  1.6  christos       if (exe)
   1523  1.6  christos 	printf_filtered ("exe = '%s'\n", exe.get ());
   1524  1.6  christos       else
   1525  1.6  christos 	warning (_("unable to read executable path name"));
   1526  1.6  christos     }
   1527  1.6  christos   if (do_files)
   1528  1.6  christos     fbsd_core_info_proc_files (gdbarch);
   1529  1.6  christos   if (do_mappings)
   1530  1.6  christos     fbsd_core_info_proc_mappings (gdbarch);
   1531  1.6  christos   if (do_status)
   1532  1.6  christos     fbsd_core_info_proc_status (gdbarch);
   1533  1.6  christos }
   1534  1.6  christos 
   1535  1.4  christos /* Print descriptions of FreeBSD-specific AUXV entries to FILE.  */
   1536  1.4  christos 
   1537  1.4  christos static void
   1538  1.4  christos fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
   1539  1.4  christos 		       CORE_ADDR type, CORE_ADDR val)
   1540  1.4  christos {
   1541  1.6  christos   const char *name = "???";
   1542  1.6  christos   const char *description = "";
   1543  1.6  christos   enum auxv_format format = AUXV_FORMAT_HEX;
   1544  1.4  christos 
   1545  1.4  christos   switch (type)
   1546  1.4  christos     {
   1547  1.6  christos     case AT_NULL:
   1548  1.6  christos     case AT_IGNORE:
   1549  1.6  christos     case AT_EXECFD:
   1550  1.6  christos     case AT_PHDR:
   1551  1.6  christos     case AT_PHENT:
   1552  1.6  christos     case AT_PHNUM:
   1553  1.6  christos     case AT_PAGESZ:
   1554  1.6  christos     case AT_BASE:
   1555  1.6  christos     case AT_FLAGS:
   1556  1.6  christos     case AT_ENTRY:
   1557  1.6  christos     case AT_NOTELF:
   1558  1.6  christos     case AT_UID:
   1559  1.6  christos     case AT_EUID:
   1560  1.6  christos     case AT_GID:
   1561  1.6  christos     case AT_EGID:
   1562  1.6  christos       default_print_auxv_entry (gdbarch, file, type, val);
   1563  1.6  christos       return;
   1564  1.4  christos #define _TAGNAME(tag) #tag
   1565  1.4  christos #define TAGNAME(tag) _TAGNAME(AT_##tag)
   1566  1.4  christos #define TAG(tag, text, kind) \
   1567  1.4  christos       case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
   1568  1.4  christos       TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
   1569  1.4  christos       TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
   1570  1.4  christos       TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
   1571  1.4  christos       TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
   1572  1.4  christos       TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
   1573  1.4  christos       TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
   1574  1.4  christos       TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
   1575  1.4  christos       TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
   1576  1.4  christos       TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
   1577  1.6  christos       TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
   1578  1.6  christos       TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
   1579  1.6  christos       TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
   1580  1.4  christos     }
   1581  1.4  christos 
   1582  1.4  christos   fprint_auxv_entry (file, name, description, format, type, val);
   1583  1.4  christos }
   1584  1.4  christos 
   1585  1.6  christos /* Implement the "get_siginfo_type" gdbarch method.  */
   1586  1.6  christos 
   1587  1.6  christos static struct type *
   1588  1.6  christos fbsd_get_siginfo_type (struct gdbarch *gdbarch)
   1589  1.6  christos {
   1590  1.6  christos   struct fbsd_gdbarch_data *fbsd_gdbarch_data;
   1591  1.6  christos   struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
   1592  1.6  christos   struct type *uid_type, *pid_type;
   1593  1.6  christos   struct type *sigval_type, *reason_type;
   1594  1.6  christos   struct type *siginfo_type;
   1595  1.6  christos   struct type *type;
   1596  1.6  christos 
   1597  1.6  christos   fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
   1598  1.6  christos   if (fbsd_gdbarch_data->siginfo_type != NULL)
   1599  1.6  christos     return fbsd_gdbarch_data->siginfo_type;
   1600  1.6  christos 
   1601  1.6  christos   int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
   1602  1.6  christos 				0, "int");
   1603  1.6  christos   int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
   1604  1.6  christos   uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
   1605  1.6  christos   long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
   1606  1.6  christos 				 0, "long");
   1607  1.6  christos   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
   1608  1.6  christos 
   1609  1.6  christos   /* union sigval */
   1610  1.6  christos   sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
   1611  1.6  christos   TYPE_NAME (sigval_type) = xstrdup ("sigval");
   1612  1.6  christos   append_composite_type_field (sigval_type, "sival_int", int_type);
   1613  1.6  christos   append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
   1614  1.6  christos 
   1615  1.6  christos   /* __pid_t */
   1616  1.6  christos   pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
   1617  1.6  christos 			TYPE_LENGTH (int32_type) * TARGET_CHAR_BIT, "__pid_t");
   1618  1.6  christos   TYPE_TARGET_TYPE (pid_type) = int32_type;
   1619  1.6  christos   TYPE_TARGET_STUB (pid_type) = 1;
   1620  1.6  christos 
   1621  1.6  christos   /* __uid_t */
   1622  1.6  christos   uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
   1623  1.6  christos 			TYPE_LENGTH (uint32_type) * TARGET_CHAR_BIT,
   1624  1.6  christos 			"__uid_t");
   1625  1.6  christos   TYPE_TARGET_TYPE (uid_type) = uint32_type;
   1626  1.6  christos   TYPE_TARGET_STUB (uid_type) = 1;
   1627  1.6  christos 
   1628  1.6  christos   /* _reason */
   1629  1.6  christos   reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
   1630  1.6  christos 
   1631  1.6  christos   /* _fault */
   1632  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1633  1.6  christos   append_composite_type_field (type, "si_trapno", int_type);
   1634  1.6  christos   append_composite_type_field (reason_type, "_fault", type);
   1635  1.6  christos 
   1636  1.6  christos   /* _timer */
   1637  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1638  1.6  christos   append_composite_type_field (type, "si_timerid", int_type);
   1639  1.6  christos   append_composite_type_field (type, "si_overrun", int_type);
   1640  1.6  christos   append_composite_type_field (reason_type, "_timer", type);
   1641  1.6  christos 
   1642  1.6  christos   /* _mesgq */
   1643  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1644  1.6  christos   append_composite_type_field (type, "si_mqd", int_type);
   1645  1.6  christos   append_composite_type_field (reason_type, "_mesgq", type);
   1646  1.6  christos 
   1647  1.6  christos   /* _poll */
   1648  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1649  1.6  christos   append_composite_type_field (type, "si_band", long_type);
   1650  1.6  christos   append_composite_type_field (reason_type, "_poll", type);
   1651  1.6  christos 
   1652  1.6  christos   /* __spare__ */
   1653  1.6  christos   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1654  1.6  christos   append_composite_type_field (type, "__spare1__", long_type);
   1655  1.6  christos   append_composite_type_field (type, "__spare2__",
   1656  1.6  christos 			       init_vector_type (int_type, 7));
   1657  1.6  christos   append_composite_type_field (reason_type, "__spare__", type);
   1658  1.6  christos 
   1659  1.6  christos   /* struct siginfo */
   1660  1.6  christos   siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
   1661  1.6  christos   TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
   1662  1.6  christos   append_composite_type_field (siginfo_type, "si_signo", int_type);
   1663  1.6  christos   append_composite_type_field (siginfo_type, "si_errno", int_type);
   1664  1.6  christos   append_composite_type_field (siginfo_type, "si_code", int_type);
   1665  1.6  christos   append_composite_type_field (siginfo_type, "si_pid", pid_type);
   1666  1.6  christos   append_composite_type_field (siginfo_type, "si_uid", uid_type);
   1667  1.6  christos   append_composite_type_field (siginfo_type, "si_status", int_type);
   1668  1.6  christos   append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
   1669  1.6  christos   append_composite_type_field (siginfo_type, "si_value", sigval_type);
   1670  1.6  christos   append_composite_type_field (siginfo_type, "_reason", reason_type);
   1671  1.6  christos 
   1672  1.6  christos   fbsd_gdbarch_data->siginfo_type = siginfo_type;
   1673  1.6  christos 
   1674  1.6  christos   return siginfo_type;
   1675  1.6  christos }
   1676  1.6  christos 
   1677  1.6  christos /* Implement the "gdb_signal_from_target" gdbarch method.  */
   1678  1.6  christos 
   1679  1.6  christos static enum gdb_signal
   1680  1.6  christos fbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
   1681  1.6  christos {
   1682  1.6  christos   switch (signal)
   1683  1.6  christos     {
   1684  1.6  christos     case 0:
   1685  1.6  christos       return GDB_SIGNAL_0;
   1686  1.6  christos 
   1687  1.6  christos     case FREEBSD_SIGHUP:
   1688  1.6  christos       return GDB_SIGNAL_HUP;
   1689  1.6  christos 
   1690  1.6  christos     case FREEBSD_SIGINT:
   1691  1.6  christos       return GDB_SIGNAL_INT;
   1692  1.6  christos 
   1693  1.6  christos     case FREEBSD_SIGQUIT:
   1694  1.6  christos       return GDB_SIGNAL_QUIT;
   1695  1.6  christos 
   1696  1.6  christos     case FREEBSD_SIGILL:
   1697  1.6  christos       return GDB_SIGNAL_ILL;
   1698  1.6  christos 
   1699  1.6  christos     case FREEBSD_SIGTRAP:
   1700  1.6  christos       return GDB_SIGNAL_TRAP;
   1701  1.6  christos 
   1702  1.6  christos     case FREEBSD_SIGABRT:
   1703  1.6  christos       return GDB_SIGNAL_ABRT;
   1704  1.6  christos 
   1705  1.6  christos     case FREEBSD_SIGEMT:
   1706  1.6  christos       return GDB_SIGNAL_EMT;
   1707  1.6  christos 
   1708  1.6  christos     case FREEBSD_SIGFPE:
   1709  1.6  christos       return GDB_SIGNAL_FPE;
   1710  1.6  christos 
   1711  1.6  christos     case FREEBSD_SIGKILL:
   1712  1.6  christos       return GDB_SIGNAL_KILL;
   1713  1.6  christos 
   1714  1.6  christos     case FREEBSD_SIGBUS:
   1715  1.6  christos       return GDB_SIGNAL_BUS;
   1716  1.6  christos 
   1717  1.6  christos     case FREEBSD_SIGSEGV:
   1718  1.6  christos       return GDB_SIGNAL_SEGV;
   1719  1.6  christos 
   1720  1.6  christos     case FREEBSD_SIGSYS:
   1721  1.6  christos       return GDB_SIGNAL_SYS;
   1722  1.6  christos 
   1723  1.6  christos     case FREEBSD_SIGPIPE:
   1724  1.6  christos       return GDB_SIGNAL_PIPE;
   1725  1.6  christos 
   1726  1.6  christos     case FREEBSD_SIGALRM:
   1727  1.6  christos       return GDB_SIGNAL_ALRM;
   1728  1.6  christos 
   1729  1.6  christos     case FREEBSD_SIGTERM:
   1730  1.6  christos       return GDB_SIGNAL_TERM;
   1731  1.6  christos 
   1732  1.6  christos     case FREEBSD_SIGURG:
   1733  1.6  christos       return GDB_SIGNAL_URG;
   1734  1.6  christos 
   1735  1.6  christos     case FREEBSD_SIGSTOP:
   1736  1.6  christos       return GDB_SIGNAL_STOP;
   1737  1.6  christos 
   1738  1.6  christos     case FREEBSD_SIGTSTP:
   1739  1.6  christos       return GDB_SIGNAL_TSTP;
   1740  1.6  christos 
   1741  1.6  christos     case FREEBSD_SIGCONT:
   1742  1.6  christos       return GDB_SIGNAL_CONT;
   1743  1.6  christos 
   1744  1.6  christos     case FREEBSD_SIGCHLD:
   1745  1.6  christos       return GDB_SIGNAL_CHLD;
   1746  1.6  christos 
   1747  1.6  christos     case FREEBSD_SIGTTIN:
   1748  1.6  christos       return GDB_SIGNAL_TTIN;
   1749  1.6  christos 
   1750  1.6  christos     case FREEBSD_SIGTTOU:
   1751  1.6  christos       return GDB_SIGNAL_TTOU;
   1752  1.6  christos 
   1753  1.6  christos     case FREEBSD_SIGIO:
   1754  1.6  christos       return GDB_SIGNAL_IO;
   1755  1.6  christos 
   1756  1.6  christos     case FREEBSD_SIGXCPU:
   1757  1.6  christos       return GDB_SIGNAL_XCPU;
   1758  1.6  christos 
   1759  1.6  christos     case FREEBSD_SIGXFSZ:
   1760  1.6  christos       return GDB_SIGNAL_XFSZ;
   1761  1.6  christos 
   1762  1.6  christos     case FREEBSD_SIGVTALRM:
   1763  1.6  christos       return GDB_SIGNAL_VTALRM;
   1764  1.6  christos 
   1765  1.6  christos     case FREEBSD_SIGPROF:
   1766  1.6  christos       return GDB_SIGNAL_PROF;
   1767  1.6  christos 
   1768  1.6  christos     case FREEBSD_SIGWINCH:
   1769  1.6  christos       return GDB_SIGNAL_WINCH;
   1770  1.6  christos 
   1771  1.6  christos     case FREEBSD_SIGINFO:
   1772  1.6  christos       return GDB_SIGNAL_INFO;
   1773  1.6  christos 
   1774  1.6  christos     case FREEBSD_SIGUSR1:
   1775  1.6  christos       return GDB_SIGNAL_USR1;
   1776  1.6  christos 
   1777  1.6  christos     case FREEBSD_SIGUSR2:
   1778  1.6  christos       return GDB_SIGNAL_USR2;
   1779  1.6  christos 
   1780  1.6  christos     /* SIGTHR is the same as SIGLWP on FreeBSD. */
   1781  1.6  christos     case FREEBSD_SIGTHR:
   1782  1.6  christos       return GDB_SIGNAL_LWP;
   1783  1.6  christos 
   1784  1.6  christos     case FREEBSD_SIGLIBRT:
   1785  1.6  christos       return GDB_SIGNAL_LIBRT;
   1786  1.6  christos     }
   1787  1.6  christos 
   1788  1.6  christos   if (signal >= FREEBSD_SIGRTMIN && signal <= FREEBSD_SIGRTMAX)
   1789  1.6  christos     {
   1790  1.6  christos       int offset = signal - FREEBSD_SIGRTMIN;
   1791  1.6  christos 
   1792  1.6  christos       return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_65 + offset);
   1793  1.6  christos     }
   1794  1.6  christos 
   1795  1.6  christos   return GDB_SIGNAL_UNKNOWN;
   1796  1.6  christos }
   1797  1.6  christos 
   1798  1.6  christos /* Implement the "gdb_signal_to_target" gdbarch method.  */
   1799  1.6  christos 
   1800  1.6  christos static int
   1801  1.6  christos fbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
   1802  1.6  christos                 enum gdb_signal signal)
   1803  1.6  christos {
   1804  1.6  christos   switch (signal)
   1805  1.6  christos     {
   1806  1.6  christos     case GDB_SIGNAL_0:
   1807  1.6  christos       return 0;
   1808  1.6  christos 
   1809  1.6  christos     case GDB_SIGNAL_HUP:
   1810  1.6  christos       return FREEBSD_SIGHUP;
   1811  1.6  christos 
   1812  1.6  christos     case GDB_SIGNAL_INT:
   1813  1.6  christos       return FREEBSD_SIGINT;
   1814  1.6  christos 
   1815  1.6  christos     case GDB_SIGNAL_QUIT:
   1816  1.6  christos       return FREEBSD_SIGQUIT;
   1817  1.6  christos 
   1818  1.6  christos     case GDB_SIGNAL_ILL:
   1819  1.6  christos       return FREEBSD_SIGILL;
   1820  1.6  christos 
   1821  1.6  christos     case GDB_SIGNAL_TRAP:
   1822  1.6  christos       return FREEBSD_SIGTRAP;
   1823  1.6  christos 
   1824  1.6  christos     case GDB_SIGNAL_ABRT:
   1825  1.6  christos       return FREEBSD_SIGABRT;
   1826  1.6  christos 
   1827  1.6  christos     case GDB_SIGNAL_EMT:
   1828  1.6  christos       return FREEBSD_SIGEMT;
   1829  1.6  christos 
   1830  1.6  christos     case GDB_SIGNAL_FPE:
   1831  1.6  christos       return FREEBSD_SIGFPE;
   1832  1.6  christos 
   1833  1.6  christos     case GDB_SIGNAL_KILL:
   1834  1.6  christos       return FREEBSD_SIGKILL;
   1835  1.6  christos 
   1836  1.6  christos     case GDB_SIGNAL_BUS:
   1837  1.6  christos       return FREEBSD_SIGBUS;
   1838  1.6  christos 
   1839  1.6  christos     case GDB_SIGNAL_SEGV:
   1840  1.6  christos       return FREEBSD_SIGSEGV;
   1841  1.6  christos 
   1842  1.6  christos     case GDB_SIGNAL_SYS:
   1843  1.6  christos       return FREEBSD_SIGSYS;
   1844  1.6  christos 
   1845  1.6  christos     case GDB_SIGNAL_PIPE:
   1846  1.6  christos       return FREEBSD_SIGPIPE;
   1847  1.6  christos 
   1848  1.6  christos     case GDB_SIGNAL_ALRM:
   1849  1.6  christos       return FREEBSD_SIGALRM;
   1850  1.6  christos 
   1851  1.6  christos     case GDB_SIGNAL_TERM:
   1852  1.6  christos       return FREEBSD_SIGTERM;
   1853  1.6  christos 
   1854  1.6  christos     case GDB_SIGNAL_URG:
   1855  1.6  christos       return FREEBSD_SIGURG;
   1856  1.6  christos 
   1857  1.6  christos     case GDB_SIGNAL_STOP:
   1858  1.6  christos       return FREEBSD_SIGSTOP;
   1859  1.6  christos 
   1860  1.6  christos     case GDB_SIGNAL_TSTP:
   1861  1.6  christos       return FREEBSD_SIGTSTP;
   1862  1.6  christos 
   1863  1.6  christos     case GDB_SIGNAL_CONT:
   1864  1.6  christos       return FREEBSD_SIGCONT;
   1865  1.6  christos 
   1866  1.6  christos     case GDB_SIGNAL_CHLD:
   1867  1.6  christos       return FREEBSD_SIGCHLD;
   1868  1.6  christos 
   1869  1.6  christos     case GDB_SIGNAL_TTIN:
   1870  1.6  christos       return FREEBSD_SIGTTIN;
   1871  1.6  christos 
   1872  1.6  christos     case GDB_SIGNAL_TTOU:
   1873  1.6  christos       return FREEBSD_SIGTTOU;
   1874  1.6  christos 
   1875  1.6  christos     case GDB_SIGNAL_IO:
   1876  1.6  christos       return FREEBSD_SIGIO;
   1877  1.6  christos 
   1878  1.6  christos     case GDB_SIGNAL_XCPU:
   1879  1.6  christos       return FREEBSD_SIGXCPU;
   1880  1.6  christos 
   1881  1.6  christos     case GDB_SIGNAL_XFSZ:
   1882  1.6  christos       return FREEBSD_SIGXFSZ;
   1883  1.6  christos 
   1884  1.6  christos     case GDB_SIGNAL_VTALRM:
   1885  1.6  christos       return FREEBSD_SIGVTALRM;
   1886  1.6  christos 
   1887  1.6  christos     case GDB_SIGNAL_PROF:
   1888  1.6  christos       return FREEBSD_SIGPROF;
   1889  1.6  christos 
   1890  1.6  christos     case GDB_SIGNAL_WINCH:
   1891  1.6  christos       return FREEBSD_SIGWINCH;
   1892  1.6  christos 
   1893  1.6  christos     case GDB_SIGNAL_INFO:
   1894  1.6  christos       return FREEBSD_SIGINFO;
   1895  1.6  christos 
   1896  1.6  christos     case GDB_SIGNAL_USR1:
   1897  1.6  christos       return FREEBSD_SIGUSR1;
   1898  1.6  christos 
   1899  1.6  christos     case GDB_SIGNAL_USR2:
   1900  1.6  christos       return FREEBSD_SIGUSR2;
   1901  1.6  christos 
   1902  1.6  christos     case GDB_SIGNAL_LWP:
   1903  1.6  christos       return FREEBSD_SIGTHR;
   1904  1.6  christos 
   1905  1.6  christos     case GDB_SIGNAL_LIBRT:
   1906  1.6  christos       return FREEBSD_SIGLIBRT;
   1907  1.6  christos     }
   1908  1.6  christos 
   1909  1.6  christos   if (signal >= GDB_SIGNAL_REALTIME_65
   1910  1.6  christos       && signal <= GDB_SIGNAL_REALTIME_126)
   1911  1.6  christos     {
   1912  1.6  christos       int offset = signal - GDB_SIGNAL_REALTIME_65;
   1913  1.6  christos 
   1914  1.6  christos       return FREEBSD_SIGRTMIN + offset;
   1915  1.6  christos     }
   1916  1.6  christos 
   1917  1.6  christos   return -1;
   1918  1.6  christos }
   1919  1.6  christos 
   1920  1.4  christos /* Implement the "get_syscall_number" gdbarch method.  */
   1921  1.4  christos 
   1922  1.4  christos static LONGEST
   1923  1.6  christos fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
   1924  1.4  christos {
   1925  1.4  christos 
   1926  1.4  christos   /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
   1927  1.4  christos      native targets fetch the system call number from the
   1928  1.4  christos      'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
   1929  1.4  christos      However, system call catching requires this function to be
   1930  1.4  christos      set.  */
   1931  1.4  christos 
   1932  1.4  christos   internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
   1933  1.4  christos }
   1934  1.4  christos 
   1935  1.5  christos /* To be called from GDB_OSABI_FREEBSD handlers. */
   1936  1.1  christos 
   1937  1.1  christos void
   1938  1.1  christos fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   1939  1.1  christos {
   1940  1.4  christos   set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
   1941  1.4  christos   set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
   1942  1.6  christos   set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
   1943  1.1  christos   set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
   1944  1.6  christos   set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
   1945  1.4  christos   set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
   1946  1.6  christos   set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
   1947  1.6  christos   set_gdbarch_gdb_signal_from_target (gdbarch, fbsd_gdb_signal_from_target);
   1948  1.6  christos   set_gdbarch_gdb_signal_to_target (gdbarch, fbsd_gdb_signal_to_target);
   1949  1.4  christos 
   1950  1.4  christos   /* `catch syscall' */
   1951  1.4  christos   set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
   1952  1.4  christos   set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
   1953  1.1  christos }
   1954  1.6  christos 
   1955  1.6  christos void
   1956  1.6  christos _initialize_fbsd_tdep (void)
   1957  1.6  christos {
   1958  1.6  christos   fbsd_gdbarch_data_handle =
   1959  1.6  christos     gdbarch_data_register_post_init (init_fbsd_gdbarch_data);
   1960  1.6  christos }
   1961