RISC-V: Don't allow unaligned breakpoints.
[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, *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   ALL_NON_EXITED_THREADS (thr)
710     {
711       if (thr == signalled_thr)
712         continue;
713       if (thr->ptid.pid () != inferior_ptid.pid ())
714         continue;
715
716       fbsd_corefile_thread (thr, &thread_args);
717     }
718
719   note_data = thread_args.note_data;
720
721   /* Auxiliary vector.  */
722   uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo  */
723   gdb::optional<gdb::byte_vector> note_desc =
724     fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
725   if (note_desc && !note_desc->empty ())
726     {
727       note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
728                                       NT_FREEBSD_PROCSTAT_AUXV,
729                                       note_desc->data (), note_desc->size ());
730       if (!note_data)
731         return NULL;
732     }
733
734   /* Virtual memory mappings.  */
735   note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
736   if (note_desc && !note_desc->empty ())
737     {
738       note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
739                                       NT_FREEBSD_PROCSTAT_VMMAP,
740                                       note_desc->data (), note_desc->size ());
741       if (!note_data)
742         return NULL;
743     }
744
745   note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
746   if (note_desc && !note_desc->empty ())
747     {
748       note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
749                                       NT_FREEBSD_PROCSTAT_PSSTRINGS,
750                                       note_desc->data (), note_desc->size ());
751       if (!note_data)
752         return NULL;
753     }
754
755   return note_data;
756 }
757
758 /* Helper function to generate the file descriptor description for a
759    single open file in 'info proc files'.  */
760
761 static const char *
762 fbsd_file_fd (int kf_fd)
763 {
764   switch (kf_fd)
765     {
766     case KINFO_FILE_FD_TYPE_CWD:
767       return "cwd";
768     case KINFO_FILE_FD_TYPE_ROOT:
769       return "root";
770     case KINFO_FILE_FD_TYPE_JAIL:
771       return "jail";
772     case KINFO_FILE_FD_TYPE_TRACE:
773       return "trace";
774     case KINFO_FILE_FD_TYPE_TEXT:
775       return "text";
776     case KINFO_FILE_FD_TYPE_CTTY:
777       return "ctty";
778     default:
779       return int_string (kf_fd, 10, 1, 0, 0);
780     }
781 }
782
783 /* Helper function to generate the file type for a single open file in
784    'info proc files'.  */
785
786 static const char *
787 fbsd_file_type (int kf_type, int kf_vnode_type)
788 {
789   switch (kf_type)
790     {
791     case KINFO_FILE_TYPE_VNODE:
792       switch (kf_vnode_type)
793         {
794         case KINFO_FILE_VTYPE_VREG:
795           return "file";
796         case KINFO_FILE_VTYPE_VDIR:
797           return "dir";
798         case KINFO_FILE_VTYPE_VCHR:
799           return "chr";
800         case KINFO_FILE_VTYPE_VLNK:
801           return "link";
802         case KINFO_FILE_VTYPE_VSOCK:
803           return "socket";
804         case KINFO_FILE_VTYPE_VFIFO:
805           return "fifo";
806         default:
807           {
808             char *str = get_print_cell ();
809
810             xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
811             return str;
812           }
813         }
814     case KINFO_FILE_TYPE_SOCKET:
815       return "socket";
816     case KINFO_FILE_TYPE_PIPE:
817       return "pipe";
818     case KINFO_FILE_TYPE_FIFO:
819       return "fifo";
820     case KINFO_FILE_TYPE_KQUEUE:
821       return "kqueue";
822     case KINFO_FILE_TYPE_CRYPTO:
823       return "crypto";
824     case KINFO_FILE_TYPE_MQUEUE:
825       return "mqueue";
826     case KINFO_FILE_TYPE_SHM:
827       return "shm";
828     case KINFO_FILE_TYPE_SEM:
829       return "sem";
830     case KINFO_FILE_TYPE_PTS:
831       return "pts";
832     case KINFO_FILE_TYPE_PROCDESC:
833       return "proc";
834     default:
835       return int_string (kf_type, 10, 1, 0, 0);
836     }
837 }
838
839 /* Helper function to generate the file flags for a single open file in
840    'info proc files'.  */
841
842 static const char *
843 fbsd_file_flags (int kf_flags)
844 {
845   static char file_flags[10];
846
847   file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
848   file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
849   file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
850   file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
851   file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
852   file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
853   file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
854   file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
855   file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
856   file_flags[9] = '\0';
857
858   return file_flags;
859 }
860
861 /* Helper function to generate the name of an IP protocol.  */
862
863 static const char *
864 fbsd_ipproto (int protocol)
865 {
866   switch (protocol)
867     {
868     case FBSD_IPPROTO_ICMP:
869       return "icmp";
870     case FBSD_IPPROTO_TCP:
871       return "tcp";
872     case FBSD_IPPROTO_UDP:
873       return "udp";
874     case FBSD_IPPROTO_SCTP:
875       return "sctp";
876     default:
877       {
878         char *str = get_print_cell ();
879
880         xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
881         return str;
882       }
883     }
884 }
885
886 /* Helper function to print out an IPv4 socket address.  */
887
888 static void
889 fbsd_print_sockaddr_in (const void *sockaddr)
890 {
891   const struct fbsd_sockaddr_in *sin =
892     reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
893   char buf[INET_ADDRSTRLEN];
894
895   if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
896     error (_("Failed to format IPv4 address"));
897   printf_filtered ("%s:%u", buf,
898                    (sin->sin_port[0] << 8) | sin->sin_port[1]);
899 }
900
901 /* Helper function to print out an IPv6 socket address.  */
902
903 static void
904 fbsd_print_sockaddr_in6 (const void *sockaddr)
905 {
906   const struct fbsd_sockaddr_in6 *sin6 =
907     reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
908   char buf[INET6_ADDRSTRLEN];
909
910   if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
911     error (_("Failed to format IPv6 address"));
912   printf_filtered ("%s.%u", buf,
913                    (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
914 }
915
916 /* See fbsd-tdep.h.  */
917
918 void
919 fbsd_info_proc_files_header ()
920 {
921   printf_filtered (_("Open files:\n\n"));
922   printf_filtered ("  %6s %6s %10s %9s %s\n",
923                    "FD", "Type", "Offset", "Flags  ", "Name");
924 }
925
926 /* See fbsd-tdep.h.  */
927
928 void
929 fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
930                             LONGEST kf_offset, int kf_vnode_type,
931                             int kf_sock_domain, int kf_sock_type,
932                             int kf_sock_protocol, const void *kf_sa_local,
933                             const void *kf_sa_peer, const void *kf_path)
934 {
935   printf_filtered ("  %6s %6s %10s %8s ",
936                    fbsd_file_fd (kf_fd),
937                    fbsd_file_type (kf_type, kf_vnode_type),
938                    kf_offset > -1 ? hex_string (kf_offset) : "-",
939                    fbsd_file_flags (kf_flags));
940   if (kf_type == KINFO_FILE_TYPE_SOCKET)
941     {
942       switch (kf_sock_domain)
943         {
944         case FBSD_AF_UNIX:
945           {
946             switch (kf_sock_type)
947               {
948               case FBSD_SOCK_STREAM:
949                 printf_filtered ("unix stream:");
950                 break;
951               case FBSD_SOCK_DGRAM:
952                 printf_filtered ("unix dgram:");
953                 break;
954               case FBSD_SOCK_SEQPACKET:
955                 printf_filtered ("unix seqpacket:");
956                 break;
957               default:
958                 printf_filtered ("unix <%d>:", kf_sock_type);
959                 break;
960               }
961
962             /* For local sockets, print out the first non-nul path
963                rather than both paths.  */
964             const struct fbsd_sockaddr_un *sun
965               = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
966             if (sun->sun_path[0] == 0)
967               sun = reinterpret_cast<const struct fbsd_sockaddr_un *>
968                 (kf_sa_peer);
969             printf_filtered ("%s", sun->sun_path);
970             break;
971           }
972         case FBSD_AF_INET:
973           printf_filtered ("%s4 ", fbsd_ipproto (kf_sock_protocol));
974           fbsd_print_sockaddr_in (kf_sa_local);
975           printf_filtered (" -> ");
976           fbsd_print_sockaddr_in (kf_sa_peer);
977           break;
978         case FBSD_AF_INET6:
979           printf_filtered ("%s6 ", fbsd_ipproto (kf_sock_protocol));
980           fbsd_print_sockaddr_in6 (kf_sa_local);
981           printf_filtered (" -> ");
982           fbsd_print_sockaddr_in6 (kf_sa_peer);
983           break;
984         }
985     }
986   else
987     printf_filtered ("%s", reinterpret_cast<const char *> (kf_path));
988   printf_filtered ("\n");
989 }
990
991 /* Implement "info proc files" for a corefile.  */
992
993 static void
994 fbsd_core_info_proc_files (struct gdbarch *gdbarch)
995 {
996   asection *section
997     = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
998   if (section == NULL)
999     {
1000       warning (_("unable to find open files in core file"));
1001       return;
1002     }
1003
1004   size_t note_size = bfd_get_section_size (section);
1005   if (note_size < 4)
1006     error (_("malformed core note - too short for header"));
1007
1008   gdb::def_vector<unsigned char> contents (note_size);
1009   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1010                                  0, note_size))
1011     error (_("could not get core note contents"));
1012
1013   unsigned char *descdata = contents.data ();
1014   unsigned char *descend = descdata + note_size;
1015
1016   /* Skip over the structure size.  */
1017   descdata += 4;
1018
1019   fbsd_info_proc_files_header ();
1020
1021   while (descdata + KF_PATH < descend)
1022     {
1023       ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
1024       if (structsize < KF_PATH)
1025         error (_("malformed core note - file structure too small"));
1026
1027       LONGEST type = bfd_get_signed_32 (core_bfd, descdata + KF_TYPE);
1028       LONGEST fd = bfd_get_signed_32 (core_bfd, descdata + KF_FD);
1029       LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KF_FLAGS);
1030       LONGEST offset = bfd_get_signed_64 (core_bfd, descdata + KF_OFFSET);
1031       LONGEST vnode_type = bfd_get_signed_32 (core_bfd,
1032                                               descdata + KF_VNODE_TYPE);
1033       LONGEST sock_domain = bfd_get_signed_32 (core_bfd,
1034                                                descdata + KF_SOCK_DOMAIN);
1035       LONGEST sock_type = bfd_get_signed_32 (core_bfd, descdata + KF_SOCK_TYPE);
1036       LONGEST sock_protocol = bfd_get_signed_32 (core_bfd,
1037                                                  descdata + KF_SOCK_PROTOCOL);
1038       fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
1039                                   sock_domain, sock_type, sock_protocol,
1040                                   descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
1041                                   descdata + KF_PATH);
1042
1043       descdata += structsize;
1044     }
1045 }
1046
1047 /* Helper function to generate mappings flags for a single VM map
1048    entry in 'info proc mappings'.  */
1049
1050 static const char *
1051 fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
1052 {
1053   static char vm_flags[9];
1054
1055   vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
1056   vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
1057   vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
1058   vm_flags[3] = ' ';
1059   vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
1060   vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
1061   vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
1062   vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
1063     : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
1064   vm_flags[8] = '\0';
1065
1066   return vm_flags;
1067 }
1068
1069 /* See fbsd-tdep.h.  */
1070
1071 void
1072 fbsd_info_proc_mappings_header (int addr_bit)
1073 {
1074   printf_filtered (_("Mapped address spaces:\n\n"));
1075   if (addr_bit == 64)
1076     {
1077       printf_filtered ("  %18s %18s %10s %10s %9s %s\n",
1078                        "Start Addr",
1079                        "  End Addr",
1080                        "      Size", "    Offset", "Flags  ", "File");
1081     }
1082   else
1083     {
1084       printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1085                        "Start Addr",
1086                        "  End Addr",
1087                        "      Size", "    Offset", "Flags  ", "File");
1088     }
1089 }
1090
1091 /* See fbsd-tdep.h.  */
1092
1093 void
1094 fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
1095                                ULONGEST kve_end, ULONGEST kve_offset,
1096                                int kve_flags, int kve_protection,
1097                                const void *kve_path)
1098 {
1099   if (addr_bit == 64)
1100     {
1101       printf_filtered ("  %18s %18s %10s %10s %9s %s\n",
1102                        hex_string (kve_start),
1103                        hex_string (kve_end),
1104                        hex_string (kve_end - kve_start),
1105                        hex_string (kve_offset),
1106                        fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1107                        reinterpret_cast<const char *> (kve_path));
1108     }
1109   else
1110     {
1111       printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1112                        hex_string (kve_start),
1113                        hex_string (kve_end),
1114                        hex_string (kve_end - kve_start),
1115                        hex_string (kve_offset),
1116                        fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1117                        reinterpret_cast<const char *> (kve_path));
1118     }
1119 }
1120
1121 /* Implement "info proc mappings" for a corefile.  */
1122
1123 static void
1124 fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
1125 {
1126   asection *section;
1127   unsigned char *descdata, *descend;
1128   size_t note_size;
1129
1130   section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.vmmap");
1131   if (section == NULL)
1132     {
1133       warning (_("unable to find mappings in core file"));
1134       return;
1135     }
1136
1137   note_size = bfd_get_section_size (section);
1138   if (note_size < 4)
1139     error (_("malformed core note - too short for header"));
1140
1141   gdb::def_vector<unsigned char> contents (note_size);
1142   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1143                                  0, note_size))
1144     error (_("could not get core note contents"));
1145
1146   descdata = contents.data ();
1147   descend = descdata + note_size;
1148
1149   /* Skip over the structure size.  */
1150   descdata += 4;
1151
1152   fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
1153   while (descdata + KVE_PATH < descend)
1154     {
1155       ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KVE_STRUCTSIZE);
1156       if (structsize < KVE_PATH)
1157         error (_("malformed core note - vmmap entry too small"));
1158
1159       ULONGEST start = bfd_get_64 (core_bfd, descdata + KVE_START);
1160       ULONGEST end = bfd_get_64 (core_bfd, descdata + KVE_END);
1161       ULONGEST offset = bfd_get_64 (core_bfd, descdata + KVE_OFFSET);
1162       LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KVE_FLAGS);
1163       LONGEST prot = bfd_get_signed_32 (core_bfd, descdata + KVE_PROTECTION);
1164       fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
1165                                      offset, flags, prot, descdata + KVE_PATH);
1166
1167       descdata += structsize;
1168     }
1169 }
1170
1171 /* Fetch the pathname of a vnode for a single file descriptor from the
1172    file table core note.  */
1173
1174 static gdb::unique_xmalloc_ptr<char>
1175 fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
1176 {
1177   asection *section;
1178   unsigned char *descdata, *descend;
1179   size_t note_size;
1180
1181   section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
1182   if (section == NULL)
1183     return nullptr;
1184
1185   note_size = bfd_get_section_size (section);
1186   if (note_size < 4)
1187     error (_("malformed core note - too short for header"));
1188
1189   gdb::def_vector<unsigned char> contents (note_size);
1190   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1191                                  0, note_size))
1192     error (_("could not get core note contents"));
1193
1194   descdata = contents.data ();
1195   descend = descdata + note_size;
1196
1197   /* Skip over the structure size.  */
1198   descdata += 4;
1199
1200   while (descdata + KF_PATH < descend)
1201     {
1202       ULONGEST structsize;
1203
1204       structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
1205       if (structsize < KF_PATH)
1206         error (_("malformed core note - file structure too small"));
1207
1208       if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
1209           && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
1210         {
1211           char *path = (char *) descdata + KF_PATH;
1212           return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
1213         }
1214
1215       descdata += structsize;
1216     }
1217   return nullptr;
1218 }
1219
1220 /* Helper function to read a struct timeval.  */
1221
1222 static void
1223 fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
1224                          LONGEST &sec, ULONGEST &usec)
1225 {
1226   if (gdbarch_addr_bit (gdbarch) == 64)
1227     {
1228       sec = bfd_get_signed_64 (core_bfd, data);
1229       usec = bfd_get_64 (core_bfd, data + 8);
1230     }
1231   else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
1232     {
1233       sec = bfd_get_signed_32 (core_bfd, data);
1234       usec = bfd_get_32 (core_bfd, data + 4);
1235     }
1236   else
1237     {
1238       sec = bfd_get_signed_64 (core_bfd, data);
1239       usec = bfd_get_32 (core_bfd, data + 8);
1240     }
1241 }
1242
1243 /* Print out the contents of a signal set.  */
1244
1245 static void
1246 fbsd_print_sigset (const char *descr, unsigned char *sigset)
1247 {
1248   printf_filtered ("%s: ", descr);
1249   for (int i = 0; i < SIG_WORDS; i++)
1250     printf_filtered ("%08x ",
1251                      (unsigned int) bfd_get_32 (core_bfd, sigset + i * 4));
1252   printf_filtered ("\n");
1253 }
1254
1255 /* Implement "info proc status" for a corefile.  */
1256
1257 static void
1258 fbsd_core_info_proc_status (struct gdbarch *gdbarch)
1259 {
1260   const struct kinfo_proc_layout *kp;
1261   asection *section;
1262   unsigned char *descdata;
1263   int addr_bit, long_bit;
1264   size_t note_size;
1265   ULONGEST value;
1266   LONGEST sec;
1267
1268   section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.proc");
1269   if (section == NULL)
1270     {
1271       warning (_("unable to find process info in core file"));
1272       return;
1273     }
1274
1275   addr_bit = gdbarch_addr_bit (gdbarch);
1276   if (addr_bit == 64)
1277     kp = &kinfo_proc_layout_64;
1278   else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
1279     kp = &kinfo_proc_layout_i386;
1280   else
1281     kp = &kinfo_proc_layout_32;
1282   long_bit = gdbarch_long_bit (gdbarch);
1283
1284   /*
1285    * Ensure that the note is large enough for all of the fields fetched
1286    * by this function.  In particular, the note must contain the 32-bit
1287    * structure size, then it must be long enough to access the last
1288    * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1289    */
1290   note_size = bfd_get_section_size (section);
1291   if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
1292                    + long_bit / TARGET_CHAR_BIT))
1293     error (_("malformed core note - too short"));
1294
1295   gdb::def_vector<unsigned char> contents (note_size);
1296   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1297                                  0, note_size))
1298     error (_("could not get core note contents"));
1299
1300   descdata = contents.data ();
1301
1302   /* Skip over the structure size.  */
1303   descdata += 4;
1304
1305   /* Verify 'ki_layout' is 0.  */
1306   if (bfd_get_32 (core_bfd, descdata + kp->ki_layout) != 0)
1307     {
1308       warning (_("unsupported process information in core file"));
1309       return;
1310     }
1311
1312   printf_filtered ("Name: %.19s\n", descdata + kp->ki_comm);
1313   printf_filtered ("Process ID: %s\n",
1314                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pid)));
1315   printf_filtered ("Parent process: %s\n",
1316                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ppid)));
1317   printf_filtered ("Process group: %s\n",
1318                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pgid)));
1319   printf_filtered ("Session id: %s\n",
1320                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_sid)));
1321
1322   /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'.  Older
1323      kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'.  In older
1324      kernels the 64-bit 'ki_tdev' field is in a reserved section of
1325      the structure that is cleared to zero.  Assume that a zero value
1326      in ki_tdev indicates a core dump from an older kernel and use the
1327      value in 'ki_tdev_freebsd11' instead.  */
1328   value = bfd_get_64 (core_bfd, descdata + kp->ki_tdev);
1329   if (value == 0)
1330     value = bfd_get_32 (core_bfd, descdata + kp->ki_tdev_freebsd11);
1331   printf_filtered ("TTY: %s\n", pulongest (value));
1332   printf_filtered ("TTY owner process group: %s\n",
1333                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_tpgid)));
1334   printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
1335                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ruid)),
1336                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_uid)),
1337                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svuid)));
1338   printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
1339                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_rgid)),
1340                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_groups)),
1341                    pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svgid)));
1342   printf_filtered ("Groups: ");
1343   uint16_t ngroups = bfd_get_16 (core_bfd, descdata + kp->ki_ngroups);
1344   for (int i = 0; i < ngroups; i++)
1345     printf_filtered ("%s ",
1346                      pulongest (bfd_get_32 (core_bfd,
1347                                             descdata + kp->ki_groups + i * 4)));
1348   printf_filtered ("\n");
1349   value = bfd_get (long_bit, core_bfd,
1350                    descdata + kp->ki_rusage + kp->ru_minflt);
1351   printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value));
1352   value = bfd_get (long_bit, core_bfd,
1353                    descdata + kp->ki_rusage_ch + kp->ru_minflt);
1354   printf_filtered ("Minor faults, children: %s\n", pulongest (value));
1355   value = bfd_get (long_bit, core_bfd,
1356                    descdata + kp->ki_rusage + kp->ru_majflt);
1357   printf_filtered ("Major faults (memory page faults): %s\n",
1358                    pulongest (value));
1359   value = bfd_get (long_bit, core_bfd,
1360                    descdata + kp->ki_rusage_ch + kp->ru_majflt);
1361   printf_filtered ("Major faults, children: %s\n", pulongest (value));
1362   fbsd_core_fetch_timeval (gdbarch,
1363                            descdata + kp->ki_rusage + kp->ru_utime,
1364                            sec, value);
1365   printf_filtered ("utime: %s.%06d\n", plongest (sec), (int) value);
1366   fbsd_core_fetch_timeval (gdbarch,
1367                            descdata + kp->ki_rusage + kp->ru_stime,
1368                            sec, value);
1369   printf_filtered ("stime: %s.%06d\n", plongest (sec), (int) value);
1370   fbsd_core_fetch_timeval (gdbarch,
1371                            descdata + kp->ki_rusage_ch + kp->ru_utime,
1372                            sec, value);
1373   printf_filtered ("utime, children: %s.%06d\n", plongest (sec), (int) value);
1374   fbsd_core_fetch_timeval (gdbarch,
1375                            descdata + kp->ki_rusage_ch + kp->ru_stime,
1376                            sec, value);
1377   printf_filtered ("stime, children: %s.%06d\n", plongest (sec), (int) value);
1378   printf_filtered ("'nice' value: %d\n",
1379                    bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
1380   fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
1381   printf_filtered ("Start time: %s.%06d\n", plongest (sec), (int) value);
1382   printf_filtered ("Virtual memory size: %s kB\n",
1383                    pulongest (bfd_get (addr_bit, core_bfd,
1384                                        descdata + kp->ki_size) / 1024));
1385   printf_filtered ("Data size: %s pages\n",
1386                    pulongest (bfd_get (addr_bit, core_bfd,
1387                                        descdata + kp->ki_dsize)));
1388   printf_filtered ("Stack size: %s pages\n",
1389                    pulongest (bfd_get (addr_bit, core_bfd,
1390                                        descdata + kp->ki_ssize)));
1391   printf_filtered ("Text size: %s pages\n",
1392                    pulongest (bfd_get (addr_bit, core_bfd,
1393                                        descdata + kp->ki_tsize)));
1394   printf_filtered ("Resident set size: %s pages\n",
1395                    pulongest (bfd_get (addr_bit, core_bfd,
1396                                        descdata + kp->ki_rssize)));
1397   printf_filtered ("Maximum RSS: %s pages\n",
1398                    pulongest (bfd_get (long_bit, core_bfd,
1399                                        descdata + kp->ki_rusage
1400                                        + kp->ru_maxrss)));
1401   fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
1402   fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
1403 }
1404
1405 /* Implement the "core_info_proc" gdbarch method.  */
1406
1407 static void
1408 fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
1409                      enum info_proc_what what)
1410 {
1411   bool do_cmdline = false;
1412   bool do_cwd = false;
1413   bool do_exe = false;
1414   bool do_files = false;
1415   bool do_mappings = false;
1416   bool do_status = false;
1417   int pid;
1418
1419   switch (what)
1420     {
1421     case IP_MINIMAL:
1422       do_cmdline = true;
1423       do_cwd = true;
1424       do_exe = true;
1425       break;
1426     case IP_MAPPINGS:
1427       do_mappings = true;
1428       break;
1429     case IP_STATUS:
1430     case IP_STAT:
1431       do_status = true;
1432       break;
1433     case IP_CMDLINE:
1434       do_cmdline = true;
1435       break;
1436     case IP_EXE:
1437       do_exe = true;
1438       break;
1439     case IP_CWD:
1440       do_cwd = true;
1441       break;
1442     case IP_FILES:
1443       do_files = true;
1444       break;
1445     case IP_ALL:
1446       do_cmdline = true;
1447       do_cwd = true;
1448       do_exe = true;
1449       do_files = true;
1450       do_mappings = true;
1451       do_status = true;
1452       break;
1453     default:
1454       return;
1455     }
1456
1457   pid = bfd_core_file_pid (core_bfd);
1458   if (pid != 0)
1459     printf_filtered (_("process %d\n"), pid);
1460
1461   if (do_cmdline)
1462     {
1463       const char *cmdline;
1464
1465       cmdline = bfd_core_file_failing_command (core_bfd);
1466       if (cmdline)
1467         printf_filtered ("cmdline = '%s'\n", cmdline);
1468       else
1469         warning (_("Command line unavailable"));
1470     }
1471   if (do_cwd)
1472     {
1473       gdb::unique_xmalloc_ptr<char> cwd =
1474         fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
1475       if (cwd)
1476         printf_filtered ("cwd = '%s'\n", cwd.get ());
1477       else
1478         warning (_("unable to read current working directory"));
1479     }
1480   if (do_exe)
1481     {
1482       gdb::unique_xmalloc_ptr<char> exe =
1483         fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
1484       if (exe)
1485         printf_filtered ("exe = '%s'\n", exe.get ());
1486       else
1487         warning (_("unable to read executable path name"));
1488     }
1489   if (do_files)
1490     fbsd_core_info_proc_files (gdbarch);
1491   if (do_mappings)
1492     fbsd_core_info_proc_mappings (gdbarch);
1493   if (do_status)
1494     fbsd_core_info_proc_status (gdbarch);
1495 }
1496
1497 /* Print descriptions of FreeBSD-specific AUXV entries to FILE.  */
1498
1499 static void
1500 fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
1501                        CORE_ADDR type, CORE_ADDR val)
1502 {
1503   const char *name = "???";
1504   const char *description = "";
1505   enum auxv_format format = AUXV_FORMAT_HEX;
1506
1507   switch (type)
1508     {
1509     case AT_NULL:
1510     case AT_IGNORE:
1511     case AT_EXECFD:
1512     case AT_PHDR:
1513     case AT_PHENT:
1514     case AT_PHNUM:
1515     case AT_PAGESZ:
1516     case AT_BASE:
1517     case AT_FLAGS:
1518     case AT_ENTRY:
1519     case AT_NOTELF:
1520     case AT_UID:
1521     case AT_EUID:
1522     case AT_GID:
1523     case AT_EGID:
1524       default_print_auxv_entry (gdbarch, file, type, val);
1525       return;
1526 #define _TAGNAME(tag) #tag
1527 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1528 #define TAG(tag, text, kind) \
1529       case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1530       TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
1531       TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
1532       TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
1533       TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
1534       TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
1535       TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
1536       TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
1537       TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
1538       TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
1539       TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
1540       TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
1541       TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
1542     }
1543
1544   fprint_auxv_entry (file, name, description, format, type, val);
1545 }
1546
1547 /* Implement the "get_siginfo_type" gdbarch method.  */
1548
1549 static struct type *
1550 fbsd_get_siginfo_type (struct gdbarch *gdbarch)
1551 {
1552   struct fbsd_gdbarch_data *fbsd_gdbarch_data;
1553   struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
1554   struct type *uid_type, *pid_type;
1555   struct type *sigval_type, *reason_type;
1556   struct type *siginfo_type;
1557   struct type *type;
1558
1559   fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
1560   if (fbsd_gdbarch_data->siginfo_type != NULL)
1561     return fbsd_gdbarch_data->siginfo_type;
1562
1563   int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
1564                                 0, "int");
1565   int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
1566   uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
1567   long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
1568                                  0, "long");
1569   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
1570
1571   /* union sigval */
1572   sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1573   TYPE_NAME (sigval_type) = xstrdup ("sigval");
1574   append_composite_type_field (sigval_type, "sival_int", int_type);
1575   append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
1576
1577   /* __pid_t */
1578   pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
1579                         TYPE_LENGTH (int32_type) * TARGET_CHAR_BIT, "__pid_t");
1580   TYPE_TARGET_TYPE (pid_type) = int32_type;
1581   TYPE_TARGET_STUB (pid_type) = 1;
1582
1583   /* __uid_t */
1584   uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
1585                         TYPE_LENGTH (uint32_type) * TARGET_CHAR_BIT,
1586                         "__uid_t");
1587   TYPE_TARGET_TYPE (uid_type) = uint32_type;
1588   TYPE_TARGET_STUB (uid_type) = 1;
1589
1590   /* _reason */
1591   reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1592
1593   /* _fault */
1594   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1595   append_composite_type_field (type, "si_trapno", int_type);
1596   append_composite_type_field (reason_type, "_fault", type);
1597
1598   /* _timer */
1599   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1600   append_composite_type_field (type, "si_timerid", int_type);
1601   append_composite_type_field (type, "si_overrun", int_type);
1602   append_composite_type_field (reason_type, "_timer", type);
1603
1604   /* _mesgq */
1605   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1606   append_composite_type_field (type, "si_mqd", int_type);
1607   append_composite_type_field (reason_type, "_mesgq", type);
1608
1609   /* _poll */
1610   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1611   append_composite_type_field (type, "si_band", long_type);
1612   append_composite_type_field (reason_type, "_poll", type);
1613
1614   /* __spare__ */
1615   type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1616   append_composite_type_field (type, "__spare1__", long_type);
1617   append_composite_type_field (type, "__spare2__",
1618                                init_vector_type (int_type, 7));
1619   append_composite_type_field (reason_type, "__spare__", type);
1620
1621   /* struct siginfo */
1622   siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1623   TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
1624   append_composite_type_field (siginfo_type, "si_signo", int_type);
1625   append_composite_type_field (siginfo_type, "si_errno", int_type);
1626   append_composite_type_field (siginfo_type, "si_code", int_type);
1627   append_composite_type_field (siginfo_type, "si_pid", pid_type);
1628   append_composite_type_field (siginfo_type, "si_uid", uid_type);
1629   append_composite_type_field (siginfo_type, "si_status", int_type);
1630   append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
1631   append_composite_type_field (siginfo_type, "si_value", sigval_type);
1632   append_composite_type_field (siginfo_type, "_reason", reason_type);
1633
1634   fbsd_gdbarch_data->siginfo_type = siginfo_type;
1635
1636   return siginfo_type;
1637 }
1638
1639 /* Implement the "get_syscall_number" gdbarch method.  */
1640
1641 static LONGEST
1642 fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
1643 {
1644
1645   /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1646      native targets fetch the system call number from the
1647      'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1648      However, system call catching requires this function to be
1649      set.  */
1650
1651   internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
1652 }
1653
1654 /* To be called from GDB_OSABI_FREEBSD handlers. */
1655
1656 void
1657 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1658 {
1659   set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
1660   set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
1661   set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
1662   set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
1663   set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
1664   set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
1665   set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
1666
1667   /* `catch syscall' */
1668   set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
1669   set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
1670 }
1671
1672 void
1673 _initialize_fbsd_tdep (void)
1674 {
1675   fbsd_gdbarch_data_handle =
1676     gdbarch_data_register_post_init (init_fbsd_gdbarch_data);
1677 }