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