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