]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/fbsd-tdep.c
gdb/MAINTAINERS: Add Rainer Orth as Solaris maintainer for GDB
[thirdparty/binutils-gdb.git] / gdb / fbsd-tdep.c
CommitLineData
a904c024
AA
1/* Target-dependent code for FreeBSD, architecture-independent.
2
e2882c85 3 Copyright (C) 2002-2018 Free Software Foundation, Inc.
a904c024
AA
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"
82372b2f 21#include "auxv.h"
a904c024
AA
22#include "gdbcore.h"
23#include "inferior.h"
24#include "regcache.h"
25#include "regset.h"
26#include "gdbthread.h"
e6cdd38e 27#include "xml-syscall.h"
57c2a98a
JB
28#include <sys/socket.h>
29#include <arpa/inet.h>
a904c024 30
a904c024
AA
31#include "elf-bfd.h"
32#include "fbsd-tdep.h"
33
34
4b654465
JB
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
d2176225
JB
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
57c2a98a
JB
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. */
d2176225
JB
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
906b4aac 73#define KVE_PROTECTION 0x38
d2176225
JB
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
57c2a98a
JB
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. */
d2176225
JB
102
103#define KF_STRUCTSIZE 0x0
104#define KF_TYPE 0x4
105#define KF_FD 0x8
57c2a98a
JB
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
d2176225
JB
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
57c2a98a
JB
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
d2176225
JB
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
57c2a98a
JB
135#define KINFO_FILE_FD_TYPE_ROOT -2
136#define KINFO_FILE_FD_TYPE_JAIL -3
137#define KINFO_FILE_FD_TYPE_TRACE -4
d2176225 138#define KINFO_FILE_FD_TYPE_TEXT -5
57c2a98a
JB
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
190struct 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
199struct 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
209struct fbsd_sockaddr_un
210{
211 uint8_t sun_len;
212 uint8_t sun_family;
213 char sun_path[104];
214};
d2176225
JB
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
237struct 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
276const 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
313const 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
350const 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
762c974a
JB
387static struct gdbarch_data *fbsd_gdbarch_data_handle;
388
389struct fbsd_gdbarch_data
390 {
391 struct type *siginfo_type;
392 };
393
394static void *
395init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
396{
397 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
398}
399
400static struct fbsd_gdbarch_data *
401get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
402{
403 return ((struct fbsd_gdbarch_data *)
404 gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
405}
406
79117428
JB
407/* This is how we want PTIDs from core files to be printed. */
408
7a114964 409static const char *
79117428
JB
410fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
411{
412 static char buf[80];
413
e38504b3 414 if (ptid.lwp () != 0)
79117428 415 {
e38504b3 416 xsnprintf (buf, sizeof buf, "LWP %ld", ptid.lwp ());
79117428
JB
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
426static const char *
427fbsd_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;
79117428 432
e38504b3 433 if (thr->ptid.lwp () != 0)
79117428
JB
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. */
2af9fc44
JB
442 thread_section_name section_name (".thrmisc", thr->ptid);
443
444 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
79117428
JB
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
4b654465
JB
470/* Implement the "core_xfer_siginfo" gdbarch method. */
471
472static LONGEST
473fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
474 ULONGEST offset, ULONGEST len)
475{
476 size_t siginfo_size;
477
a181c0bf 478 if (gdbarch_long_bit (gdbarch) == 32)
4b654465
JB
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;
a181c0bf 503 if (gdbarch_long_bit (gdbarch) == 32)
4b654465
JB
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
a904c024
AA
515static int
516find_signalled_thread (struct thread_info *info, void *data)
517{
518 if (info->suspend.stop_signal != GDB_SIGNAL_0
e99b03dc 519 && info->ptid.pid () == inferior_ptid.pid ())
a904c024
AA
520 return 1;
521
522 return 0;
523}
524
20a0aab3
JB
525/* Structure for passing information from
526 fbsd_collect_thread_registers via an iterator to
527 fbsd_collect_regset_section_cb. */
a904c024
AA
528
529struct fbsd_collect_regset_section_cb_data
530{
531 const struct regcache *regcache;
532 bfd *obfd;
533 char *note_data;
534 int *note_size;
20a0aab3
JB
535 unsigned long lwp;
536 enum gdb_signal stop_signal;
537 int abort_iteration;
a904c024
AA
538};
539
540static void
a616bb94
AH
541fbsd_collect_regset_section_cb (const char *sect_name, int supply_size,
542 int collect_size, const struct regset *regset,
a904c024
AA
543 const char *human_name, void *cb_data)
544{
545 char *buf;
7567e115
SM
546 struct fbsd_collect_regset_section_cb_data *data
547 = (struct fbsd_collect_regset_section_cb_data *) cb_data;
a904c024 548
20a0aab3
JB
549 if (data->abort_iteration)
550 return;
551
a904c024
AA
552 gdb_assert (regset->collect_regset);
553
a616bb94
AH
554 buf = (char *) xmalloc (collect_size);
555 regset->collect_regset (regset, data->regcache, -1, buf, collect_size);
a904c024
AA
556
557 /* PRSTATUS still needs to be treated specially. */
558 if (strcmp (sect_name, ".reg") == 0)
559 data->note_data = (char *) elfcore_write_prstatus
20a0aab3
JB
560 (data->obfd, data->note_data, data->note_size, data->lwp,
561 gdb_signal_to_host (data->stop_signal), buf);
a904c024
AA
562 else
563 data->note_data = (char *) elfcore_write_register_note
564 (data->obfd, data->note_data, data->note_size,
a616bb94 565 sect_name, buf, collect_size);
a904c024 566 xfree (buf);
20a0aab3
JB
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
575static char *
576fbsd_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{
ac7936df 581 struct gdbarch *gdbarch = regcache->arch ();
20a0aab3
JB
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;
e38504b3 590 data.lwp = ptid.lwp ();
20a0aab3
JB
591
592 gdbarch_iterate_over_regset_sections (gdbarch,
593 fbsd_collect_regset_section_cb,
594 &data, regcache);
595 return data.note_data;
596}
597
598struct 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
610static void
611fbsd_corefile_thread (struct thread_info *info,
612 struct fbsd_corefile_thread_data *args)
613{
20a0aab3
JB
614 struct regcache *regcache;
615
616 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
617
20a0aab3 618 target_fetch_registers (regcache, -1);
20a0aab3
JB
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);
a904c024
AA
623}
624
739ab2e9
SS
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
630static gdb::optional<gdb::byte_vector>
631fbsd_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
a904c024
AA
647/* Create appropriate note sections for a corefile, returning them in
648 allocated memory. */
649
650static char *
651fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
652{
20a0aab3
JB
653 struct fbsd_corefile_thread_data thread_args;
654 char *note_data = NULL;
a904c024 655 Elf_Internal_Ehdr *i_ehdrp;
20a0aab3 656 struct thread_info *curr_thr, *signalled_thr, *thr;
a904c024
AA
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
a904c024
AA
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
20a0aab3
JB
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;
e99b03dc 713 if (thr->ptid.pid () != inferior_ptid.pid ())
20a0aab3
JB
714 continue;
715
716 fbsd_corefile_thread (thr, &thread_args);
717 }
718
719 note_data = thread_args.note_data;
720
739ab2e9
SS
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
a904c024
AA
755 return note_data;
756}
757
57c2a98a
JB
758/* Helper function to generate the file descriptor description for a
759 single open file in 'info proc files'. */
760
761static const char *
762fbsd_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
786static const char *
787fbsd_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
842static const char *
843fbsd_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
863static const char *
864fbsd_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
888static void
889fbsd_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
07147793 895 if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
57c2a98a
JB
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
903static void
904fbsd_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
07147793 910 if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
57c2a98a
JB
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
918void
919fbsd_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
928void
929fbsd_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
993static void
994fbsd_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
d2176225
JB
1047/* Helper function to generate mappings flags for a single VM map
1048 entry in 'info proc mappings'. */
1049
1050const char *
1051fbsd_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/* Implement "info proc mappings" for a corefile. */
1070
1071static void
1072fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
1073{
1074 asection *section;
1075 unsigned char *descdata, *descend;
1076 size_t note_size;
1077
1078 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.vmmap");
1079 if (section == NULL)
1080 {
1081 warning (_("unable to find mappings in core file"));
1082 return;
1083 }
1084
1085 note_size = bfd_get_section_size (section);
1086 if (note_size < 4)
1087 error (_("malformed core note - too short for header"));
1088
1089 gdb::def_vector<unsigned char> contents (note_size);
1090 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1091 0, note_size))
1092 error (_("could not get core note contents"));
1093
1094 descdata = contents.data ();
1095 descend = descdata + note_size;
1096
1097 /* Skip over the structure size. */
1098 descdata += 4;
1099
1100 printf_filtered (_("Mapped address spaces:\n\n"));
1101 if (gdbarch_addr_bit (gdbarch) == 64)
1102 {
1103 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1104 "Start Addr",
1105 " End Addr",
1106 " Size", " Offset", "Flags ", "File");
1107 }
1108 else
1109 {
1110 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1111 "Start Addr",
1112 " End Addr",
1113 " Size", " Offset", "Flags ", "File");
1114 }
1115
1116 while (descdata + KVE_PATH < descend)
1117 {
1118 ULONGEST start, end, offset, flags, prot, structsize;
1119
1120 structsize = bfd_get_32 (core_bfd, descdata + KVE_STRUCTSIZE);
1121 if (structsize < KVE_PATH)
1122 error (_("malformed core note - vmmap entry too small"));
1123
1124 start = bfd_get_64 (core_bfd, descdata + KVE_START);
1125 end = bfd_get_64 (core_bfd, descdata + KVE_END);
1126 offset = bfd_get_64 (core_bfd, descdata + KVE_OFFSET);
1127 flags = bfd_get_32 (core_bfd, descdata + KVE_FLAGS);
1128 prot = bfd_get_32 (core_bfd, descdata + KVE_PROTECTION);
1129 if (gdbarch_addr_bit (gdbarch) == 64)
1130 {
1131 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1132 paddress (gdbarch, start),
1133 paddress (gdbarch, end),
1134 hex_string (end - start),
1135 hex_string (offset),
1136 fbsd_vm_map_entry_flags (flags, prot),
1137 descdata + KVE_PATH);
1138 }
1139 else
1140 {
1141 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1142 paddress (gdbarch, start),
1143 paddress (gdbarch, end),
1144 hex_string (end - start),
1145 hex_string (offset),
1146 fbsd_vm_map_entry_flags (flags, prot),
1147 descdata + KVE_PATH);
1148 }
1149
1150 descdata += structsize;
1151 }
1152}
1153
1154/* Fetch the pathname of a vnode for a single file descriptor from the
1155 file table core note. */
1156
1157static gdb::unique_xmalloc_ptr<char>
1158fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
1159{
1160 asection *section;
1161 unsigned char *descdata, *descend;
1162 size_t note_size;
1163
1164 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
1165 if (section == NULL)
1166 return nullptr;
1167
1168 note_size = bfd_get_section_size (section);
1169 if (note_size < 4)
1170 error (_("malformed core note - too short for header"));
1171
1172 gdb::def_vector<unsigned char> contents (note_size);
1173 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1174 0, note_size))
1175 error (_("could not get core note contents"));
1176
1177 descdata = contents.data ();
1178 descend = descdata + note_size;
1179
1180 /* Skip over the structure size. */
1181 descdata += 4;
1182
9f235e09 1183 while (descdata + KF_PATH < descend)
d2176225
JB
1184 {
1185 ULONGEST structsize;
1186
1187 structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
9f235e09
JB
1188 if (structsize < KF_PATH)
1189 error (_("malformed core note - file structure too small"));
d2176225
JB
1190
1191 if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
1192 && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
1193 {
1194 char *path = (char *) descdata + KF_PATH;
1195 return gdb::unique_xmalloc_ptr<char> (xstrdup (path));
1196 }
1197
1198 descdata += structsize;
1199 }
1200 return nullptr;
1201}
1202
1203/* Helper function to read a struct timeval. */
1204
1205static void
1206fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
1207 LONGEST &sec, ULONGEST &usec)
1208{
1209 if (gdbarch_addr_bit (gdbarch) == 64)
1210 {
1211 sec = bfd_get_signed_64 (core_bfd, data);
1212 usec = bfd_get_64 (core_bfd, data + 8);
1213 }
1214 else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
1215 {
1216 sec = bfd_get_signed_32 (core_bfd, data);
1217 usec = bfd_get_32 (core_bfd, data + 4);
1218 }
1219 else
1220 {
1221 sec = bfd_get_signed_64 (core_bfd, data);
1222 usec = bfd_get_32 (core_bfd, data + 8);
1223 }
1224}
1225
1226/* Print out the contents of a signal set. */
1227
1228static void
1229fbsd_print_sigset (const char *descr, unsigned char *sigset)
1230{
1231 printf_filtered ("%s: ", descr);
1232 for (int i = 0; i < SIG_WORDS; i++)
1233 printf_filtered ("%08x ",
1234 (unsigned int) bfd_get_32 (core_bfd, sigset + i * 4));
1235 printf_filtered ("\n");
1236}
1237
1238/* Implement "info proc status" for a corefile. */
1239
1240static void
1241fbsd_core_info_proc_status (struct gdbarch *gdbarch)
1242{
1243 const struct kinfo_proc_layout *kp;
1244 asection *section;
d2176225
JB
1245 unsigned char *descdata;
1246 int addr_bit, long_bit;
1247 size_t note_size;
1248 ULONGEST value;
1249 LONGEST sec;
1250
1251 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.proc");
1252 if (section == NULL)
1253 {
1254 warning (_("unable to find process info in core file"));
1255 return;
1256 }
1257
1258 addr_bit = gdbarch_addr_bit (gdbarch);
1259 if (addr_bit == 64)
1260 kp = &kinfo_proc_layout_64;
1261 else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
1262 kp = &kinfo_proc_layout_i386;
1263 else
1264 kp = &kinfo_proc_layout_32;
1265 long_bit = gdbarch_long_bit (gdbarch);
1266
1267 /*
1268 * Ensure that the note is large enough for all of the fields fetched
1269 * by this function. In particular, the note must contain the 32-bit
1270 * structure size, then it must be long enough to access the last
1271 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1272 */
1273 note_size = bfd_get_section_size (section);
1274 if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
1275 + long_bit / TARGET_CHAR_BIT))
1276 error (_("malformed core note - too short"));
1277
1278 gdb::def_vector<unsigned char> contents (note_size);
1279 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1280 0, note_size))
1281 error (_("could not get core note contents"));
1282
1283 descdata = contents.data ();
1284
1285 /* Skip over the structure size. */
1286 descdata += 4;
1287
1288 /* Verify 'ki_layout' is 0. */
1289 if (bfd_get_32 (core_bfd, descdata + kp->ki_layout) != 0)
1290 {
1291 warning (_("unsupported process information in core file"));
1292 return;
1293 }
1294
1295 printf_filtered ("Name: %.19s\n", descdata + kp->ki_comm);
1296 printf_filtered ("Process ID: %s\n",
1297 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pid)));
1298 printf_filtered ("Parent process: %s\n",
1299 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ppid)));
1300 printf_filtered ("Process group: %s\n",
1301 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pgid)));
1302 printf_filtered ("Session id: %s\n",
1303 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_sid)));
1304
1305 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1306 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1307 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1308 the structure that is cleared to zero. Assume that a zero value
1309 in ki_tdev indicates a core dump from an older kernel and use the
1310 value in 'ki_tdev_freebsd11' instead. */
1311 value = bfd_get_64 (core_bfd, descdata + kp->ki_tdev);
1312 if (value == 0)
1313 value = bfd_get_32 (core_bfd, descdata + kp->ki_tdev_freebsd11);
1314 printf_filtered ("TTY: %s\n", pulongest (value));
1315 printf_filtered ("TTY owner process group: %s\n",
1316 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_tpgid)));
1317 printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
1318 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ruid)),
1319 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_uid)),
1320 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svuid)));
1321 printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
1322 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_rgid)),
1323 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_groups)),
1324 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svgid)));
1325 printf_filtered ("Groups: ");
1326 uint16_t ngroups = bfd_get_16 (core_bfd, descdata + kp->ki_ngroups);
1327 for (int i = 0; i < ngroups; i++)
1328 printf_filtered ("%s ",
1329 pulongest (bfd_get_32 (core_bfd,
1330 descdata + kp->ki_groups + i * 4)));
1331 printf_filtered ("\n");
1332 value = bfd_get (long_bit, core_bfd,
1333 descdata + kp->ki_rusage + kp->ru_minflt);
1334 printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value));
1335 value = bfd_get (long_bit, core_bfd,
1336 descdata + kp->ki_rusage_ch + kp->ru_minflt);
1337 printf_filtered ("Minor faults, children: %s\n", pulongest (value));
1338 value = bfd_get (long_bit, core_bfd,
1339 descdata + kp->ki_rusage + kp->ru_majflt);
1340 printf_filtered ("Major faults (memory page faults): %s\n",
1341 pulongest (value));
1342 value = bfd_get (long_bit, core_bfd,
1343 descdata + kp->ki_rusage_ch + kp->ru_majflt);
1344 printf_filtered ("Major faults, children: %s\n", pulongest (value));
1345 fbsd_core_fetch_timeval (gdbarch,
1346 descdata + kp->ki_rusage + kp->ru_utime,
1347 sec, value);
1348 printf_filtered ("utime: %s.%06d\n", plongest (sec), (int) value);
1349 fbsd_core_fetch_timeval (gdbarch,
1350 descdata + kp->ki_rusage + kp->ru_stime,
1351 sec, value);
1352 printf_filtered ("stime: %s.%06d\n", plongest (sec), (int) value);
1353 fbsd_core_fetch_timeval (gdbarch,
1354 descdata + kp->ki_rusage_ch + kp->ru_utime,
1355 sec, value);
1356 printf_filtered ("utime, children: %s.%06d\n", plongest (sec), (int) value);
1357 fbsd_core_fetch_timeval (gdbarch,
1358 descdata + kp->ki_rusage_ch + kp->ru_stime,
1359 sec, value);
1360 printf_filtered ("stime, children: %s.%06d\n", plongest (sec), (int) value);
1361 printf_filtered ("'nice' value: %d\n",
1362 bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
1363 fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
1364 printf_filtered ("Start time: %s.%06d\n", plongest (sec), (int) value);
1365 printf_filtered ("Virtual memory size: %s kB\n",
1366 pulongest (bfd_get (addr_bit, core_bfd,
1367 descdata + kp->ki_size) / 1024));
1368 printf_filtered ("Data size: %s pages\n",
1369 pulongest (bfd_get (addr_bit, core_bfd,
1370 descdata + kp->ki_dsize)));
1371 printf_filtered ("Stack size: %s pages\n",
1372 pulongest (bfd_get (addr_bit, core_bfd,
1373 descdata + kp->ki_ssize)));
1374 printf_filtered ("Text size: %s pages\n",
1375 pulongest (bfd_get (addr_bit, core_bfd,
1376 descdata + kp->ki_tsize)));
1377 printf_filtered ("Resident set size: %s pages\n",
1378 pulongest (bfd_get (addr_bit, core_bfd,
1379 descdata + kp->ki_rssize)));
1380 printf_filtered ("Maximum RSS: %s pages\n",
1381 pulongest (bfd_get (long_bit, core_bfd,
1382 descdata + kp->ki_rusage
1383 + kp->ru_maxrss)));
1384 fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
1385 fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
1386}
1387
1388/* Implement the "core_info_proc" gdbarch method. */
1389
1390static void
1391fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
1392 enum info_proc_what what)
1393{
1394 bool do_cmdline = false;
1395 bool do_cwd = false;
1396 bool do_exe = false;
57c2a98a 1397 bool do_files = false;
d2176225
JB
1398 bool do_mappings = false;
1399 bool do_status = false;
1400 int pid;
1401
1402 switch (what)
1403 {
1404 case IP_MINIMAL:
1405 do_cmdline = true;
1406 do_cwd = true;
1407 do_exe = true;
1408 break;
1409 case IP_MAPPINGS:
1410 do_mappings = true;
1411 break;
1412 case IP_STATUS:
1413 case IP_STAT:
1414 do_status = true;
1415 break;
1416 case IP_CMDLINE:
1417 do_cmdline = true;
1418 break;
1419 case IP_EXE:
1420 do_exe = true;
1421 break;
1422 case IP_CWD:
1423 do_cwd = true;
1424 break;
57c2a98a
JB
1425 case IP_FILES:
1426 do_files = true;
1427 break;
d2176225
JB
1428 case IP_ALL:
1429 do_cmdline = true;
1430 do_cwd = true;
1431 do_exe = true;
57c2a98a 1432 do_files = true;
d2176225
JB
1433 do_mappings = true;
1434 do_status = true;
1435 break;
1436 default:
1437 return;
1438 }
1439
1440 pid = bfd_core_file_pid (core_bfd);
1441 if (pid != 0)
1442 printf_filtered (_("process %d\n"), pid);
1443
1444 if (do_cmdline)
1445 {
1446 const char *cmdline;
1447
1448 cmdline = bfd_core_file_failing_command (core_bfd);
1449 if (cmdline)
1450 printf_filtered ("cmdline = '%s'\n", cmdline);
1451 else
1452 warning (_("Command line unavailable"));
1453 }
1454 if (do_cwd)
1455 {
1456 gdb::unique_xmalloc_ptr<char> cwd =
1457 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
1458 if (cwd)
1459 printf_filtered ("cwd = '%s'\n", cwd.get ());
1460 else
1461 warning (_("unable to read current working directory"));
1462 }
1463 if (do_exe)
1464 {
1465 gdb::unique_xmalloc_ptr<char> exe =
1466 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
1467 if (exe)
1468 printf_filtered ("exe = '%s'\n", exe.get ());
1469 else
1470 warning (_("unable to read executable path name"));
1471 }
57c2a98a
JB
1472 if (do_files)
1473 fbsd_core_info_proc_files (gdbarch);
d2176225
JB
1474 if (do_mappings)
1475 fbsd_core_info_proc_mappings (gdbarch);
1476 if (do_status)
1477 fbsd_core_info_proc_status (gdbarch);
1478}
1479
82372b2f
JB
1480/* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1481
1482static void
1483fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
1484 CORE_ADDR type, CORE_ADDR val)
1485{
1486 const char *name;
1487 const char *description;
1488 enum auxv_format format;
1489
1490 switch (type)
1491 {
1492#define _TAGNAME(tag) #tag
1493#define TAGNAME(tag) _TAGNAME(AT_##tag)
1494#define TAG(tag, text, kind) \
1495 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1496 TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
1497 TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
1498 TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
1499 TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
1500 TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
1501 TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
1502 TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
1503 TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
1504 TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
12c4bd7f
JB
1505 TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
1506 TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
82372b2f
JB
1507 default:
1508 default_print_auxv_entry (gdbarch, file, type, val);
1509 return;
1510 }
1511
1512 fprint_auxv_entry (file, name, description, format, type, val);
1513}
1514
762c974a
JB
1515/* Implement the "get_siginfo_type" gdbarch method. */
1516
1517static struct type *
1518fbsd_get_siginfo_type (struct gdbarch *gdbarch)
1519{
1520 struct fbsd_gdbarch_data *fbsd_gdbarch_data;
1521 struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
1522 struct type *uid_type, *pid_type;
1523 struct type *sigval_type, *reason_type;
1524 struct type *siginfo_type;
1525 struct type *type;
1526
1527 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
1528 if (fbsd_gdbarch_data->siginfo_type != NULL)
1529 return fbsd_gdbarch_data->siginfo_type;
1530
1531 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
1532 0, "int");
1533 int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
1534 uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
1535 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
1536 0, "long");
1537 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
1538
1539 /* union sigval */
1540 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1541 TYPE_NAME (sigval_type) = xstrdup ("sigval");
1542 append_composite_type_field (sigval_type, "sival_int", int_type);
1543 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
1544
1545 /* __pid_t */
1546 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
77b7c781 1547 TYPE_LENGTH (int32_type) * TARGET_CHAR_BIT, "__pid_t");
762c974a
JB
1548 TYPE_TARGET_TYPE (pid_type) = int32_type;
1549 TYPE_TARGET_STUB (pid_type) = 1;
1550
1551 /* __uid_t */
1552 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
77b7c781
UW
1553 TYPE_LENGTH (uint32_type) * TARGET_CHAR_BIT,
1554 "__uid_t");
762c974a
JB
1555 TYPE_TARGET_TYPE (uid_type) = uint32_type;
1556 TYPE_TARGET_STUB (uid_type) = 1;
1557
1558 /* _reason */
1559 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1560
1561 /* _fault */
1562 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1563 append_composite_type_field (type, "si_trapno", int_type);
1564 append_composite_type_field (reason_type, "_fault", type);
1565
1566 /* _timer */
1567 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1568 append_composite_type_field (type, "si_timerid", int_type);
1569 append_composite_type_field (type, "si_overrun", int_type);
1570 append_composite_type_field (reason_type, "_timer", type);
1571
1572 /* _mesgq */
1573 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1574 append_composite_type_field (type, "si_mqd", int_type);
1575 append_composite_type_field (reason_type, "_mesgq", type);
1576
1577 /* _poll */
1578 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1579 append_composite_type_field (type, "si_band", long_type);
1580 append_composite_type_field (reason_type, "_poll", type);
1581
1582 /* __spare__ */
1583 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1584 append_composite_type_field (type, "__spare1__", long_type);
1585 append_composite_type_field (type, "__spare2__",
1586 init_vector_type (int_type, 7));
1587 append_composite_type_field (reason_type, "__spare__", type);
1588
1589 /* struct siginfo */
1590 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1591 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
1592 append_composite_type_field (siginfo_type, "si_signo", int_type);
1593 append_composite_type_field (siginfo_type, "si_errno", int_type);
1594 append_composite_type_field (siginfo_type, "si_code", int_type);
1595 append_composite_type_field (siginfo_type, "si_pid", pid_type);
1596 append_composite_type_field (siginfo_type, "si_uid", uid_type);
1597 append_composite_type_field (siginfo_type, "si_status", int_type);
1598 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
1599 append_composite_type_field (siginfo_type, "si_value", sigval_type);
1600 append_composite_type_field (siginfo_type, "_reason", reason_type);
1601
1602 fbsd_gdbarch_data->siginfo_type = siginfo_type;
1603
1604 return siginfo_type;
1605}
1606
e6cdd38e
JB
1607/* Implement the "get_syscall_number" gdbarch method. */
1608
1609static LONGEST
00431a78 1610fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
e6cdd38e
JB
1611{
1612
1613 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1614 native targets fetch the system call number from the
1615 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1616 However, system call catching requires this function to be
1617 set. */
1618
1619 internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
1620}
1621
1736a7bd 1622/* To be called from GDB_OSABI_FREEBSD handlers. */
a904c024
AA
1623
1624void
1625fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1626{
79117428
JB
1627 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
1628 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
4b654465 1629 set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
a904c024 1630 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
d2176225 1631 set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
82372b2f 1632 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
762c974a 1633 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
e6cdd38e
JB
1634
1635 /* `catch syscall' */
1636 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
1637 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
a904c024 1638}
762c974a 1639
762c974a
JB
1640void
1641_initialize_fbsd_tdep (void)
1642{
1643 fbsd_gdbarch_data_handle =
1644 gdbarch_data_register_post_init (init_fbsd_gdbarch_data);
1645}