Automatic date update in version.in
[binutils-gdb.git] / gdb / fbsd-tdep.c
blob593f5b4fd4a111352663b83de7183c794974b14f
1 /* Target-dependent code for FreeBSD, architecture-independent.
3 Copyright (C) 2002-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "auxv.h"
21 #include "gdbcore.h"
22 #include "inferior.h"
23 #include "objfiles.h"
24 #include "regcache.h"
25 #include "regset.h"
26 #include "gdbthread.h"
27 #include "objfiles.h"
28 #include "xml-syscall.h"
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
32 #include "elf-bfd.h"
33 #include "fbsd-tdep.h"
34 #include "gcore-elf.h"
36 /* This enum is derived from FreeBSD's <sys/signal.h>. */
38 enum
40 FREEBSD_SIGHUP = 1,
41 FREEBSD_SIGINT = 2,
42 FREEBSD_SIGQUIT = 3,
43 FREEBSD_SIGILL = 4,
44 FREEBSD_SIGTRAP = 5,
45 FREEBSD_SIGABRT = 6,
46 FREEBSD_SIGEMT = 7,
47 FREEBSD_SIGFPE = 8,
48 FREEBSD_SIGKILL = 9,
49 FREEBSD_SIGBUS = 10,
50 FREEBSD_SIGSEGV = 11,
51 FREEBSD_SIGSYS = 12,
52 FREEBSD_SIGPIPE = 13,
53 FREEBSD_SIGALRM = 14,
54 FREEBSD_SIGTERM = 15,
55 FREEBSD_SIGURG = 16,
56 FREEBSD_SIGSTOP = 17,
57 FREEBSD_SIGTSTP = 18,
58 FREEBSD_SIGCONT = 19,
59 FREEBSD_SIGCHLD = 20,
60 FREEBSD_SIGTTIN = 21,
61 FREEBSD_SIGTTOU = 22,
62 FREEBSD_SIGIO = 23,
63 FREEBSD_SIGXCPU = 24,
64 FREEBSD_SIGXFSZ = 25,
65 FREEBSD_SIGVTALRM = 26,
66 FREEBSD_SIGPROF = 27,
67 FREEBSD_SIGWINCH = 28,
68 FREEBSD_SIGINFO = 29,
69 FREEBSD_SIGUSR1 = 30,
70 FREEBSD_SIGUSR2 = 31,
71 FREEBSD_SIGTHR = 32,
72 FREEBSD_SIGLIBRT = 33,
73 FREEBSD_SIGRTMIN = 65,
74 FREEBSD_SIGRTMAX = 126,
77 /* Constants for values of si_code as defined in FreeBSD's
78 <sys/signal.h>. */
80 #define FBSD_SI_USER 0x10001
81 #define FBSD_SI_QUEUE 0x10002
82 #define FBSD_SI_TIMER 0x10003
83 #define FBSD_SI_ASYNCIO 0x10004
84 #define FBSD_SI_MESGQ 0x10005
85 #define FBSD_SI_KERNEL 0x10006
86 #define FBSD_SI_LWP 0x10007
88 #define FBSD_ILL_ILLOPC 1
89 #define FBSD_ILL_ILLOPN 2
90 #define FBSD_ILL_ILLADR 3
91 #define FBSD_ILL_ILLTRP 4
92 #define FBSD_ILL_PRVOPC 5
93 #define FBSD_ILL_PRVREG 6
94 #define FBSD_ILL_COPROC 7
95 #define FBSD_ILL_BADSTK 8
97 #define FBSD_BUS_ADRALN 1
98 #define FBSD_BUS_ADRERR 2
99 #define FBSD_BUS_OBJERR 3
100 #define FBSD_BUS_OOMERR 100
102 #define FBSD_SEGV_MAPERR 1
103 #define FBSD_SEGV_ACCERR 2
104 #define FBSD_SEGV_PKUERR 100
106 #define FBSD_FPE_INTOVF 1
107 #define FBSD_FPE_INTDIV 2
108 #define FBSD_FPE_FLTDIV 3
109 #define FBSD_FPE_FLTOVF 4
110 #define FBSD_FPE_FLTUND 5
111 #define FBSD_FPE_FLTRES 6
112 #define FBSD_FPE_FLTINV 7
113 #define FBSD_FPE_FLTSUB 8
115 #define FBSD_TRAP_BRKPT 1
116 #define FBSD_TRAP_TRACE 2
117 #define FBSD_TRAP_DTRACE 3
118 #define FBSD_TRAP_CAP 4
120 #define FBSD_CLD_EXITED 1
121 #define FBSD_CLD_KILLED 2
122 #define FBSD_CLD_DUMPED 3
123 #define FBSD_CLD_TRAPPED 4
124 #define FBSD_CLD_STOPPED 5
125 #define FBSD_CLD_CONTINUED 6
127 #define FBSD_POLL_IN 1
128 #define FBSD_POLL_OUT 2
129 #define FBSD_POLL_MSG 3
130 #define FBSD_POLL_ERR 4
131 #define FBSD_POLL_PRI 5
132 #define FBSD_POLL_HUP 6
134 /* FreeBSD kernels 12.0 and later include a copy of the
135 'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
136 operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
137 The constants below define the offset of field members and flags in
138 this structure used by methods in this file. Note that the
139 'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
140 containing the size of the structure. */
142 #define LWPINFO_OFFSET 0x4
144 /* Offsets in ptrace_lwpinfo. */
145 #define LWPINFO_PL_FLAGS 0x8
146 #define LWPINFO64_PL_SIGINFO 0x30
147 #define LWPINFO32_PL_SIGINFO 0x2c
149 /* Flags in pl_flags. */
150 #define PL_FLAG_SI 0x20 /* siginfo is valid */
152 /* Sizes of siginfo_t. */
153 #define SIZE64_SIGINFO_T 80
154 #define SIZE32_SIGINFO_T 64
156 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
157 dump notes. See <sys/user.h> for the definition of struct
158 kinfo_vmentry. This data structure should have the same layout on
159 all architectures.
161 Note that FreeBSD 7.0 used an older version of this structure
162 (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
163 dump note wasn't introduced until FreeBSD 9.2. As a result, the
164 core dump note has always used the 7.1 and later structure
165 format. */
167 #define KVE_STRUCTSIZE 0x0
168 #define KVE_START 0x8
169 #define KVE_END 0x10
170 #define KVE_OFFSET 0x18
171 #define KVE_FLAGS 0x2c
172 #define KVE_PROTECTION 0x38
173 #define KVE_PATH 0x88
175 /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
176 match the KVME_PROT_* constants in <sys/user.h>. */
178 #define KINFO_VME_PROT_READ 0x00000001
179 #define KINFO_VME_PROT_WRITE 0x00000002
180 #define KINFO_VME_PROT_EXEC 0x00000004
182 /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
183 match the KVME_FLAG_* constants in <sys/user.h>. */
185 #define KINFO_VME_FLAG_COW 0x00000001
186 #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
187 #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
188 #define KINFO_VME_FLAG_SUPER 0x00000008
189 #define KINFO_VME_FLAG_GROWS_UP 0x00000010
190 #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
192 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
193 dump notes. See <sys/user.h> for the definition of struct
194 kinfo_file. This data structure should have the same layout on all
195 architectures.
197 Note that FreeBSD 7.0 used an older version of this structure
198 (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
199 note wasn't introduced until FreeBSD 9.2. As a result, the core
200 dump note has always used the 7.1 and later structure format. */
202 #define KF_STRUCTSIZE 0x0
203 #define KF_TYPE 0x4
204 #define KF_FD 0x8
205 #define KF_FLAGS 0x10
206 #define KF_OFFSET 0x18
207 #define KF_VNODE_TYPE 0x20
208 #define KF_SOCK_DOMAIN 0x24
209 #define KF_SOCK_TYPE 0x28
210 #define KF_SOCK_PROTOCOL 0x2c
211 #define KF_SA_LOCAL 0x30
212 #define KF_SA_PEER 0xb0
213 #define KF_PATH 0x170
215 /* Constants for the 'kf_type' field in struct kinfo_file. These
216 match the KF_TYPE_* constants in <sys/user.h>. */
218 #define KINFO_FILE_TYPE_VNODE 1
219 #define KINFO_FILE_TYPE_SOCKET 2
220 #define KINFO_FILE_TYPE_PIPE 3
221 #define KINFO_FILE_TYPE_FIFO 4
222 #define KINFO_FILE_TYPE_KQUEUE 5
223 #define KINFO_FILE_TYPE_CRYPTO 6
224 #define KINFO_FILE_TYPE_MQUEUE 7
225 #define KINFO_FILE_TYPE_SHM 8
226 #define KINFO_FILE_TYPE_SEM 9
227 #define KINFO_FILE_TYPE_PTS 10
228 #define KINFO_FILE_TYPE_PROCDESC 11
230 /* Special values for the 'kf_fd' field in struct kinfo_file. These
231 match the KF_FD_TYPE_* constants in <sys/user.h>. */
233 #define KINFO_FILE_FD_TYPE_CWD -1
234 #define KINFO_FILE_FD_TYPE_ROOT -2
235 #define KINFO_FILE_FD_TYPE_JAIL -3
236 #define KINFO_FILE_FD_TYPE_TRACE -4
237 #define KINFO_FILE_FD_TYPE_TEXT -5
238 #define KINFO_FILE_FD_TYPE_CTTY -6
240 /* Flags in the 'kf_flags' field in struct kinfo_file. These match
241 the KF_FLAG_* constants in <sys/user.h>. */
243 #define KINFO_FILE_FLAG_READ 0x00000001
244 #define KINFO_FILE_FLAG_WRITE 0x00000002
245 #define KINFO_FILE_FLAG_APPEND 0x00000004
246 #define KINFO_FILE_FLAG_ASYNC 0x00000008
247 #define KINFO_FILE_FLAG_FSYNC 0x00000010
248 #define KINFO_FILE_FLAG_NONBLOCK 0x00000020
249 #define KINFO_FILE_FLAG_DIRECT 0x00000040
250 #define KINFO_FILE_FLAG_HASLOCK 0x00000080
251 #define KINFO_FILE_FLAG_EXEC 0x00004000
253 /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
254 These match the KF_VTYPE_* constants in <sys/user.h>. */
256 #define KINFO_FILE_VTYPE_VREG 1
257 #define KINFO_FILE_VTYPE_VDIR 2
258 #define KINFO_FILE_VTYPE_VCHR 4
259 #define KINFO_FILE_VTYPE_VLNK 5
260 #define KINFO_FILE_VTYPE_VSOCK 6
261 #define KINFO_FILE_VTYPE_VFIFO 7
263 /* Constants for socket address families. These match AF_* constants
264 in <sys/socket.h>. */
266 #define FBSD_AF_UNIX 1
267 #define FBSD_AF_INET 2
268 #define FBSD_AF_INET6 28
270 /* Constants for socket types. These match SOCK_* constants in
271 <sys/socket.h>. */
273 #define FBSD_SOCK_STREAM 1
274 #define FBSD_SOCK_DGRAM 2
275 #define FBSD_SOCK_SEQPACKET 5
277 /* Constants for IP protocols. These match IPPROTO_* constants in
278 <netinet/in.h>. */
280 #define FBSD_IPPROTO_ICMP 1
281 #define FBSD_IPPROTO_TCP 6
282 #define FBSD_IPPROTO_UDP 17
283 #define FBSD_IPPROTO_SCTP 132
285 /* Socket address structures. These have the same layout on all
286 FreeBSD architectures. In addition, multibyte fields such as IP
287 addresses are always stored in network byte order. */
289 struct fbsd_sockaddr_in
291 uint8_t sin_len;
292 uint8_t sin_family;
293 uint8_t sin_port[2];
294 uint8_t sin_addr[4];
295 char sin_zero[8];
298 struct fbsd_sockaddr_in6
300 uint8_t sin6_len;
301 uint8_t sin6_family;
302 uint8_t sin6_port[2];
303 uint32_t sin6_flowinfo;
304 uint8_t sin6_addr[16];
305 uint32_t sin6_scope_id;
308 struct fbsd_sockaddr_un
310 uint8_t sun_len;
311 uint8_t sun_family;
312 char sun_path[104];
315 /* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
316 <sys/_sigset.h> and is the same value on all architectures. */
318 #define SIG_WORDS 4
320 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
321 dump notes. See <sys/user.h> for the definition of struct
322 kinfo_proc. This data structure has different layouts on different
323 architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
324 uses a 32-bit time_t while all other architectures use a 64-bit
325 time_t.
327 The core dump note actually contains one kinfo_proc structure for
328 each thread, but all of the process-wide data can be obtained from
329 the first structure. One result of this note's format is that some
330 of the process-wide status available in the native target method
331 from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
332 is not available from a core dump. Instead, the per-thread data
333 structures contain the value of these fields for individual
334 threads. */
336 struct kinfo_proc_layout
338 /* Offsets of struct kinfo_proc members. */
339 int ki_layout;
340 int ki_pid;
341 int ki_ppid;
342 int ki_pgid;
343 int ki_tpgid;
344 int ki_sid;
345 int ki_tdev_freebsd11;
346 int ki_sigignore;
347 int ki_sigcatch;
348 int ki_uid;
349 int ki_ruid;
350 int ki_svuid;
351 int ki_rgid;
352 int ki_svgid;
353 int ki_ngroups;
354 int ki_groups;
355 int ki_size;
356 int ki_rssize;
357 int ki_tsize;
358 int ki_dsize;
359 int ki_ssize;
360 int ki_start;
361 int ki_nice;
362 int ki_comm;
363 int ki_tdev;
364 int ki_rusage;
365 int ki_rusage_ch;
367 /* Offsets of struct rusage members. */
368 int ru_utime;
369 int ru_stime;
370 int ru_maxrss;
371 int ru_minflt;
372 int ru_majflt;
375 const struct kinfo_proc_layout kinfo_proc_layout_32 =
377 .ki_layout = 0x4,
378 .ki_pid = 0x28,
379 .ki_ppid = 0x2c,
380 .ki_pgid = 0x30,
381 .ki_tpgid = 0x34,
382 .ki_sid = 0x38,
383 .ki_tdev_freebsd11 = 0x44,
384 .ki_sigignore = 0x68,
385 .ki_sigcatch = 0x78,
386 .ki_uid = 0x88,
387 .ki_ruid = 0x8c,
388 .ki_svuid = 0x90,
389 .ki_rgid = 0x94,
390 .ki_svgid = 0x98,
391 .ki_ngroups = 0x9c,
392 .ki_groups = 0xa0,
393 .ki_size = 0xe0,
394 .ki_rssize = 0xe4,
395 .ki_tsize = 0xec,
396 .ki_dsize = 0xf0,
397 .ki_ssize = 0xf4,
398 .ki_start = 0x118,
399 .ki_nice = 0x145,
400 .ki_comm = 0x17f,
401 .ki_tdev = 0x1f0,
402 .ki_rusage = 0x220,
403 .ki_rusage_ch = 0x278,
405 .ru_utime = 0x0,
406 .ru_stime = 0x10,
407 .ru_maxrss = 0x20,
408 .ru_minflt = 0x30,
409 .ru_majflt = 0x34,
412 const struct kinfo_proc_layout kinfo_proc_layout_i386 =
414 .ki_layout = 0x4,
415 .ki_pid = 0x28,
416 .ki_ppid = 0x2c,
417 .ki_pgid = 0x30,
418 .ki_tpgid = 0x34,
419 .ki_sid = 0x38,
420 .ki_tdev_freebsd11 = 0x44,
421 .ki_sigignore = 0x68,
422 .ki_sigcatch = 0x78,
423 .ki_uid = 0x88,
424 .ki_ruid = 0x8c,
425 .ki_svuid = 0x90,
426 .ki_rgid = 0x94,
427 .ki_svgid = 0x98,
428 .ki_ngroups = 0x9c,
429 .ki_groups = 0xa0,
430 .ki_size = 0xe0,
431 .ki_rssize = 0xe4,
432 .ki_tsize = 0xec,
433 .ki_dsize = 0xf0,
434 .ki_ssize = 0xf4,
435 .ki_start = 0x118,
436 .ki_nice = 0x135,
437 .ki_comm = 0x16f,
438 .ki_tdev = 0x1e0,
439 .ki_rusage = 0x210,
440 .ki_rusage_ch = 0x258,
442 .ru_utime = 0x0,
443 .ru_stime = 0x8,
444 .ru_maxrss = 0x10,
445 .ru_minflt = 0x20,
446 .ru_majflt = 0x24,
449 const struct kinfo_proc_layout kinfo_proc_layout_64 =
451 .ki_layout = 0x4,
452 .ki_pid = 0x48,
453 .ki_ppid = 0x4c,
454 .ki_pgid = 0x50,
455 .ki_tpgid = 0x54,
456 .ki_sid = 0x58,
457 .ki_tdev_freebsd11 = 0x64,
458 .ki_sigignore = 0x88,
459 .ki_sigcatch = 0x98,
460 .ki_uid = 0xa8,
461 .ki_ruid = 0xac,
462 .ki_svuid = 0xb0,
463 .ki_rgid = 0xb4,
464 .ki_svgid = 0xb8,
465 .ki_ngroups = 0xbc,
466 .ki_groups = 0xc0,
467 .ki_size = 0x100,
468 .ki_rssize = 0x108,
469 .ki_tsize = 0x118,
470 .ki_dsize = 0x120,
471 .ki_ssize = 0x128,
472 .ki_start = 0x150,
473 .ki_nice = 0x185,
474 .ki_comm = 0x1bf,
475 .ki_tdev = 0x230,
476 .ki_rusage = 0x260,
477 .ki_rusage_ch = 0x2f0,
479 .ru_utime = 0x0,
480 .ru_stime = 0x10,
481 .ru_maxrss = 0x20,
482 .ru_minflt = 0x40,
483 .ru_majflt = 0x48,
486 struct fbsd_gdbarch_data
488 struct type *siginfo_type = nullptr;
491 static const registry<gdbarch>::key<fbsd_gdbarch_data>
492 fbsd_gdbarch_data_handle;
494 static struct fbsd_gdbarch_data *
495 get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
497 struct fbsd_gdbarch_data *result = fbsd_gdbarch_data_handle.get (gdbarch);
498 if (result == nullptr)
499 result = fbsd_gdbarch_data_handle.emplace (gdbarch);
500 return result;
503 struct fbsd_pspace_data
505 /* Offsets in the runtime linker's 'Obj_Entry' structure. */
506 LONGEST off_linkmap = 0;
507 LONGEST off_tlsindex = 0;
508 bool rtld_offsets_valid = false;
510 /* vDSO mapping range. */
511 struct mem_range vdso_range {};
513 /* Zero if the range hasn't been searched for, > 0 if a range was
514 found, or < 0 if a range was not found. */
515 int vdso_range_p = 0;
518 /* Per-program-space data for FreeBSD architectures. */
519 static const registry<program_space>::key<fbsd_pspace_data>
520 fbsd_pspace_data_handle;
522 static struct fbsd_pspace_data *
523 get_fbsd_pspace_data (struct program_space *pspace)
525 struct fbsd_pspace_data *data;
527 data = fbsd_pspace_data_handle.get (pspace);
528 if (data == NULL)
529 data = fbsd_pspace_data_handle.emplace (pspace);
531 return data;
534 /* This is how we want PTIDs from core files to be printed. */
536 static std::string
537 fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
539 if (ptid.lwp () != 0)
540 return string_printf ("LWP %ld", ptid.lwp ());
542 return normal_pid_to_str (ptid);
545 /* Extract the name assigned to a thread from a core. Returns the
546 string in a static buffer. */
548 static const char *
549 fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
551 static char buf[80];
552 struct bfd_section *section;
553 bfd_size_type size;
555 if (thr->ptid.lwp () != 0)
557 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
558 whose contents are defined by a "struct thrmisc" declared in
559 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
560 a null-terminated string as the first member of the
561 structure. Rather than define the full structure here, just
562 extract the null-terminated name from the start of the
563 note. */
564 thread_section_name section_name (".thrmisc", thr->ptid);
565 bfd *cbfd = current_program_space->core_bfd ();
567 section = bfd_get_section_by_name (cbfd, section_name.c_str ());
568 if (section != NULL && bfd_section_size (section) > 0)
570 /* Truncate the name if it is longer than "buf". */
571 size = bfd_section_size (section);
572 if (size > sizeof buf - 1)
573 size = sizeof buf - 1;
574 if (bfd_get_section_contents (cbfd, section, buf, (file_ptr) 0, size)
575 && buf[0] != '\0')
577 buf[size] = '\0';
579 /* Note that each thread will report the process command
580 as its thread name instead of an empty name if a name
581 has not been set explicitly. Return a NULL name in
582 that case. */
583 if (strcmp (buf, elf_tdata (cbfd)->core->program) != 0)
584 return buf;
589 return NULL;
592 /* Implement the "core_xfer_siginfo" gdbarch method. */
594 static LONGEST
595 fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
596 ULONGEST offset, ULONGEST len)
598 size_t siginfo_size;
600 if (gdbarch_long_bit (gdbarch) == 32)
601 siginfo_size = SIZE32_SIGINFO_T;
602 else
603 siginfo_size = SIZE64_SIGINFO_T;
604 if (offset > siginfo_size)
605 return -1;
607 thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
608 bfd *cbfd = current_program_space->core_bfd ();
609 asection *section = bfd_get_section_by_name (cbfd, section_name.c_str ());
610 if (section == NULL)
611 return -1;
613 gdb_byte buf[4];
614 if (!bfd_get_section_contents (cbfd, section, buf,
615 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
616 return -1;
618 int pl_flags = extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
619 if (!(pl_flags & PL_FLAG_SI))
620 return -1;
622 if (offset + len > siginfo_size)
623 len = siginfo_size - offset;
625 ULONGEST siginfo_offset;
626 if (gdbarch_long_bit (gdbarch) == 32)
627 siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
628 else
629 siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
631 if (!bfd_get_section_contents (cbfd, section, readbuf,
632 siginfo_offset + offset, len))
633 return -1;
635 return len;
638 static int
639 find_signalled_thread (struct thread_info *info, void *data)
641 if (info->stop_signal () != GDB_SIGNAL_0
642 && info->ptid.pid () == inferior_ptid.pid ())
643 return 1;
645 return 0;
648 /* Return a byte_vector containing the contents of a core dump note
649 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
650 the data is prefixed with a 32-bit integer size to match the format
651 used in FreeBSD NT_PROCSTAT_* notes. */
653 static std::optional<gdb::byte_vector>
654 fbsd_make_note_desc (enum target_object object, uint32_t structsize)
656 std::optional<gdb::byte_vector> buf =
657 target_read_alloc (current_inferior ()->top_target (), object, NULL);
658 if (!buf || buf->empty ())
659 return {};
661 if (structsize == 0)
662 return buf;
664 gdb::byte_vector desc (sizeof (structsize) + buf->size ());
665 memcpy (desc.data (), &structsize, sizeof (structsize));
666 std::copy (buf->begin (), buf->end (), desc.data () + sizeof (structsize));
667 return desc;
670 /* Create appropriate note sections for a corefile, returning them in
671 allocated memory. */
673 static gdb::unique_xmalloc_ptr<char>
674 fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
676 gdb::unique_xmalloc_ptr<char> note_data;
677 Elf_Internal_Ehdr *i_ehdrp;
678 struct thread_info *curr_thr, *signalled_thr;
680 /* Put a "FreeBSD" label in the ELF header. */
681 i_ehdrp = elf_elfheader (obfd);
682 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
684 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
686 if (get_exec_file (0))
688 const char *fname = lbasename (get_exec_file (0));
689 std::string psargs = fname;
691 const std::string &infargs = current_inferior ()->args ();
692 if (!infargs.empty ())
693 psargs += ' ' + infargs;
695 note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
696 note_size, fname,
697 psargs.c_str ()));
700 /* Thread register information. */
703 update_thread_list ();
705 catch (const gdb_exception_error &e)
707 exception_print (gdb_stderr, e);
710 /* Like the kernel, prefer dumping the signalled thread first.
711 "First thread" is what tools use to infer the signalled thread.
712 In case there's more than one signalled thread, prefer the
713 current thread, if it is signalled. */
714 curr_thr = inferior_thread ();
715 if (curr_thr->stop_signal () != GDB_SIGNAL_0)
716 signalled_thr = curr_thr;
717 else
719 signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
720 if (signalled_thr == NULL)
721 signalled_thr = curr_thr;
724 enum gdb_signal stop_signal = signalled_thr->stop_signal ();
725 gcore_elf_build_thread_register_notes (gdbarch, signalled_thr, stop_signal,
726 obfd, &note_data, note_size);
727 for (thread_info *thr : current_inferior ()->non_exited_threads ())
729 if (thr == signalled_thr)
730 continue;
732 gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal,
733 obfd, &note_data, note_size);
736 /* Auxiliary vector. */
737 uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo */
738 std::optional<gdb::byte_vector> note_desc =
739 fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
740 if (note_desc && !note_desc->empty ())
742 note_data.reset (elfcore_write_note (obfd, note_data.release (),
743 note_size, "FreeBSD",
744 NT_FREEBSD_PROCSTAT_AUXV,
745 note_desc->data (),
746 note_desc->size ()));
747 if (!note_data)
748 return NULL;
751 /* Virtual memory mappings. */
752 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
753 if (note_desc && !note_desc->empty ())
755 note_data.reset (elfcore_write_note (obfd, note_data.release (),
756 note_size, "FreeBSD",
757 NT_FREEBSD_PROCSTAT_VMMAP,
758 note_desc->data (),
759 note_desc->size ()));
760 if (!note_data)
761 return NULL;
764 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
765 if (note_desc && !note_desc->empty ())
767 note_data.reset (elfcore_write_note (obfd, note_data.release (),
768 note_size, "FreeBSD",
769 NT_FREEBSD_PROCSTAT_PSSTRINGS,
770 note_desc->data (),
771 note_desc->size ()));
772 if (!note_data)
773 return NULL;
776 /* Include the target description when possible. Some architectures
777 allow for per-thread gdbarch so we should really be emitting a tdesc
778 per-thread, however, we don't currently support reading in a
779 per-thread tdesc, so just emit the tdesc for the signalled thread. */
780 gdbarch = target_thread_architecture (signalled_thr->ptid);
781 gcore_elf_make_tdesc_note (gdbarch, obfd, &note_data, note_size);
783 return note_data;
786 /* Helper function to generate the file descriptor description for a
787 single open file in 'info proc files'. */
789 static const char *
790 fbsd_file_fd (int kf_fd)
792 switch (kf_fd)
794 case KINFO_FILE_FD_TYPE_CWD:
795 return "cwd";
796 case KINFO_FILE_FD_TYPE_ROOT:
797 return "root";
798 case KINFO_FILE_FD_TYPE_JAIL:
799 return "jail";
800 case KINFO_FILE_FD_TYPE_TRACE:
801 return "trace";
802 case KINFO_FILE_FD_TYPE_TEXT:
803 return "text";
804 case KINFO_FILE_FD_TYPE_CTTY:
805 return "ctty";
806 default:
807 return int_string (kf_fd, 10, 1, 0, 0);
811 /* Helper function to generate the file type for a single open file in
812 'info proc files'. */
814 static const char *
815 fbsd_file_type (int kf_type, int kf_vnode_type)
817 switch (kf_type)
819 case KINFO_FILE_TYPE_VNODE:
820 switch (kf_vnode_type)
822 case KINFO_FILE_VTYPE_VREG:
823 return "file";
824 case KINFO_FILE_VTYPE_VDIR:
825 return "dir";
826 case KINFO_FILE_VTYPE_VCHR:
827 return "chr";
828 case KINFO_FILE_VTYPE_VLNK:
829 return "link";
830 case KINFO_FILE_VTYPE_VSOCK:
831 return "socket";
832 case KINFO_FILE_VTYPE_VFIFO:
833 return "fifo";
834 default:
836 char *str = get_print_cell ();
838 xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
839 return str;
842 case KINFO_FILE_TYPE_SOCKET:
843 return "socket";
844 case KINFO_FILE_TYPE_PIPE:
845 return "pipe";
846 case KINFO_FILE_TYPE_FIFO:
847 return "fifo";
848 case KINFO_FILE_TYPE_KQUEUE:
849 return "kqueue";
850 case KINFO_FILE_TYPE_CRYPTO:
851 return "crypto";
852 case KINFO_FILE_TYPE_MQUEUE:
853 return "mqueue";
854 case KINFO_FILE_TYPE_SHM:
855 return "shm";
856 case KINFO_FILE_TYPE_SEM:
857 return "sem";
858 case KINFO_FILE_TYPE_PTS:
859 return "pts";
860 case KINFO_FILE_TYPE_PROCDESC:
861 return "proc";
862 default:
863 return int_string (kf_type, 10, 1, 0, 0);
867 /* Helper function to generate the file flags for a single open file in
868 'info proc files'. */
870 static const char *
871 fbsd_file_flags (int kf_flags)
873 static char file_flags[10];
875 file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
876 file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
877 file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
878 file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
879 file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
880 file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
881 file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
882 file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
883 file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
884 file_flags[9] = '\0';
886 return file_flags;
889 /* Helper function to generate the name of an IP protocol. */
891 static const char *
892 fbsd_ipproto (int protocol)
894 switch (protocol)
896 case FBSD_IPPROTO_ICMP:
897 return "icmp";
898 case FBSD_IPPROTO_TCP:
899 return "tcp";
900 case FBSD_IPPROTO_UDP:
901 return "udp";
902 case FBSD_IPPROTO_SCTP:
903 return "sctp";
904 default:
906 char *str = get_print_cell ();
908 xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
909 return str;
914 /* Helper function to print out an IPv4 socket address. */
916 static void
917 fbsd_print_sockaddr_in (const void *sockaddr)
919 const struct fbsd_sockaddr_in *sin =
920 reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
921 char buf[INET_ADDRSTRLEN];
923 if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
924 error (_("Failed to format IPv4 address"));
925 gdb_printf ("%s:%u", buf,
926 (sin->sin_port[0] << 8) | sin->sin_port[1]);
929 /* Helper function to print out an IPv6 socket address. */
931 static void
932 fbsd_print_sockaddr_in6 (const void *sockaddr)
934 const struct fbsd_sockaddr_in6 *sin6 =
935 reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
936 char buf[INET6_ADDRSTRLEN];
938 if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
939 error (_("Failed to format IPv6 address"));
940 gdb_printf ("%s.%u", buf,
941 (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
944 /* See fbsd-tdep.h. */
946 void
947 fbsd_info_proc_files_header ()
949 gdb_printf (_("Open files:\n\n"));
950 gdb_printf (" %6s %6s %10s %9s %s\n",
951 "FD", "Type", "Offset", "Flags ", "Name");
954 /* See fbsd-tdep.h. */
956 void
957 fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
958 LONGEST kf_offset, int kf_vnode_type,
959 int kf_sock_domain, int kf_sock_type,
960 int kf_sock_protocol, const void *kf_sa_local,
961 const void *kf_sa_peer, const void *kf_path)
963 gdb_printf (" %6s %6s %10s %8s ",
964 fbsd_file_fd (kf_fd),
965 fbsd_file_type (kf_type, kf_vnode_type),
966 kf_offset > -1 ? hex_string (kf_offset) : "-",
967 fbsd_file_flags (kf_flags));
968 if (kf_type == KINFO_FILE_TYPE_SOCKET)
970 switch (kf_sock_domain)
972 case FBSD_AF_UNIX:
974 switch (kf_sock_type)
976 case FBSD_SOCK_STREAM:
977 gdb_printf ("unix stream:");
978 break;
979 case FBSD_SOCK_DGRAM:
980 gdb_printf ("unix dgram:");
981 break;
982 case FBSD_SOCK_SEQPACKET:
983 gdb_printf ("unix seqpacket:");
984 break;
985 default:
986 gdb_printf ("unix <%d>:", kf_sock_type);
987 break;
990 /* For local sockets, print out the first non-nul path
991 rather than both paths. */
992 const struct fbsd_sockaddr_un *saddr_un
993 = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
994 if (saddr_un->sun_path[0] == 0)
995 saddr_un = reinterpret_cast<const struct fbsd_sockaddr_un *>
996 (kf_sa_peer);
997 gdb_printf ("%s", saddr_un->sun_path);
998 break;
1000 case FBSD_AF_INET:
1001 gdb_printf ("%s4 ", fbsd_ipproto (kf_sock_protocol));
1002 fbsd_print_sockaddr_in (kf_sa_local);
1003 gdb_printf (" -> ");
1004 fbsd_print_sockaddr_in (kf_sa_peer);
1005 break;
1006 case FBSD_AF_INET6:
1007 gdb_printf ("%s6 ", fbsd_ipproto (kf_sock_protocol));
1008 fbsd_print_sockaddr_in6 (kf_sa_local);
1009 gdb_printf (" -> ");
1010 fbsd_print_sockaddr_in6 (kf_sa_peer);
1011 break;
1014 else
1015 gdb_printf ("%s", reinterpret_cast<const char *> (kf_path));
1016 gdb_printf ("\n");
1019 /* Implement "info proc files" for a corefile. */
1021 static void
1022 fbsd_core_info_proc_files (struct gdbarch *gdbarch)
1024 bfd *cbfd = current_program_space->core_bfd ();
1025 asection *section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
1026 if (section == NULL)
1028 warning (_("unable to find open files in core file"));
1029 return;
1032 size_t note_size = bfd_section_size (section);
1033 if (note_size < 4)
1034 error (_("malformed core note - too short for header"));
1036 gdb::def_vector<unsigned char> contents (note_size);
1037 if (!bfd_get_section_contents (cbfd, section, contents.data (),
1038 0, note_size))
1039 error (_("could not get core note contents"));
1041 unsigned char *descdata = contents.data ();
1042 unsigned char *descend = descdata + note_size;
1044 /* Skip over the structure size. */
1045 descdata += 4;
1047 fbsd_info_proc_files_header ();
1049 while (descdata + KF_PATH < descend)
1051 ULONGEST structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
1052 if (structsize < KF_PATH)
1053 error (_("malformed core note - file structure too small"));
1055 LONGEST type = bfd_get_signed_32 (cbfd, descdata + KF_TYPE);
1056 LONGEST fd = bfd_get_signed_32 (cbfd, descdata + KF_FD);
1057 LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KF_FLAGS);
1058 LONGEST offset = bfd_get_signed_64 (cbfd, descdata + KF_OFFSET);
1059 LONGEST vnode_type = bfd_get_signed_32 (cbfd, descdata + KF_VNODE_TYPE);
1060 LONGEST sock_domain = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_DOMAIN);
1061 LONGEST sock_type = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_TYPE);
1062 LONGEST sock_protocol = bfd_get_signed_32 (cbfd,
1063 descdata + KF_SOCK_PROTOCOL);
1064 fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
1065 sock_domain, sock_type, sock_protocol,
1066 descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
1067 descdata + KF_PATH);
1069 descdata += structsize;
1073 /* Helper function to generate mappings flags for a single VM map
1074 entry in 'info proc mappings'. */
1076 static const char *
1077 fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
1079 static char vm_flags[9];
1081 vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
1082 vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
1083 vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
1084 vm_flags[3] = ' ';
1085 vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
1086 vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
1087 vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
1088 vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
1089 : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
1090 vm_flags[8] = '\0';
1092 return vm_flags;
1095 /* See fbsd-tdep.h. */
1097 void
1098 fbsd_info_proc_mappings_header (int addr_bit)
1100 gdb_printf (_("Mapped address spaces:\n\n"));
1101 if (addr_bit == 64)
1103 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
1104 "Start Addr",
1105 " End Addr",
1106 " Size", " Offset", "Flags ", "File");
1108 else
1110 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
1111 "Start Addr",
1112 " End Addr",
1113 " Size", " Offset", "Flags ", "File");
1117 /* See fbsd-tdep.h. */
1119 void
1120 fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
1121 ULONGEST kve_end, ULONGEST kve_offset,
1122 int kve_flags, int kve_protection,
1123 const void *kve_path)
1125 if (addr_bit == 64)
1127 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
1128 hex_string (kve_start),
1129 hex_string (kve_end),
1130 hex_string (kve_end - kve_start),
1131 hex_string (kve_offset),
1132 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1133 reinterpret_cast<const char *> (kve_path));
1135 else
1137 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
1138 hex_string (kve_start),
1139 hex_string (kve_end),
1140 hex_string (kve_end - kve_start),
1141 hex_string (kve_offset),
1142 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1143 reinterpret_cast<const char *> (kve_path));
1147 /* Implement "info proc mappings" for a corefile. */
1149 static void
1150 fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
1152 asection *section;
1153 unsigned char *descdata, *descend;
1154 size_t note_size;
1155 bfd *cbfd = current_program_space->core_bfd ();
1157 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.vmmap");
1158 if (section == NULL)
1160 warning (_("unable to find mappings in core file"));
1161 return;
1164 note_size = bfd_section_size (section);
1165 if (note_size < 4)
1166 error (_("malformed core note - too short for header"));
1168 gdb::def_vector<unsigned char> contents (note_size);
1169 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
1170 error (_("could not get core note contents"));
1172 descdata = contents.data ();
1173 descend = descdata + note_size;
1175 /* Skip over the structure size. */
1176 descdata += 4;
1178 fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
1179 while (descdata + KVE_PATH < descend)
1181 ULONGEST structsize = bfd_get_32 (cbfd, descdata + KVE_STRUCTSIZE);
1182 if (structsize < KVE_PATH)
1183 error (_("malformed core note - vmmap entry too small"));
1185 ULONGEST start = bfd_get_64 (cbfd, descdata + KVE_START);
1186 ULONGEST end = bfd_get_64 (cbfd, descdata + KVE_END);
1187 ULONGEST offset = bfd_get_64 (cbfd, descdata + KVE_OFFSET);
1188 LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KVE_FLAGS);
1189 LONGEST prot = bfd_get_signed_32 (cbfd, descdata + KVE_PROTECTION);
1190 fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
1191 offset, flags, prot, descdata + KVE_PATH);
1193 descdata += structsize;
1197 /* Fetch the pathname of a vnode for a single file descriptor from the
1198 file table core note. */
1200 static gdb::unique_xmalloc_ptr<char>
1201 fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
1203 asection *section;
1204 unsigned char *descdata, *descend;
1205 size_t note_size;
1206 bfd *cbfd = current_program_space->core_bfd ();
1208 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
1209 if (section == NULL)
1210 return nullptr;
1212 note_size = bfd_section_size (section);
1213 if (note_size < 4)
1214 error (_("malformed core note - too short for header"));
1216 gdb::def_vector<unsigned char> contents (note_size);
1217 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
1218 error (_("could not get core note contents"));
1220 descdata = contents.data ();
1221 descend = descdata + note_size;
1223 /* Skip over the structure size. */
1224 descdata += 4;
1226 while (descdata + KF_PATH < descend)
1228 ULONGEST structsize;
1230 structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
1231 if (structsize < KF_PATH)
1232 error (_("malformed core note - file structure too small"));
1234 if (bfd_get_32 (cbfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
1235 && bfd_get_signed_32 (cbfd, descdata + KF_FD) == fd)
1237 char *path = (char *) descdata + KF_PATH;
1238 return make_unique_xstrdup (path);
1241 descdata += structsize;
1243 return nullptr;
1246 /* Helper function to read a struct timeval. */
1248 static void
1249 fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
1250 LONGEST &sec, ULONGEST &usec)
1252 bfd *cbfd = current_program_space->core_bfd ();
1254 if (gdbarch_addr_bit (gdbarch) == 64)
1256 sec = bfd_get_signed_64 (cbfd, data);
1257 usec = bfd_get_64 (cbfd, data + 8);
1259 else if (bfd_get_arch (cbfd) == bfd_arch_i386)
1261 sec = bfd_get_signed_32 (cbfd, data);
1262 usec = bfd_get_32 (cbfd, data + 4);
1264 else
1266 sec = bfd_get_signed_64 (cbfd, data);
1267 usec = bfd_get_32 (cbfd, data + 8);
1271 /* Print out the contents of a signal set. */
1273 static void
1274 fbsd_print_sigset (const char *descr, unsigned char *sigset)
1276 bfd *cbfd = current_program_space->core_bfd ();
1277 gdb_printf ("%s: ", descr);
1278 for (int i = 0; i < SIG_WORDS; i++)
1279 gdb_printf ("%08x ",
1280 (unsigned int) bfd_get_32 (cbfd, sigset + i * 4));
1281 gdb_printf ("\n");
1284 /* Implement "info proc status" for a corefile. */
1286 static void
1287 fbsd_core_info_proc_status (struct gdbarch *gdbarch)
1289 const struct kinfo_proc_layout *kp;
1290 asection *section;
1291 unsigned char *descdata;
1292 int addr_bit, long_bit;
1293 size_t note_size;
1294 ULONGEST value;
1295 LONGEST sec;
1296 bfd *cbfd = current_program_space->core_bfd ();
1298 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.proc");
1299 if (section == NULL)
1301 warning (_("unable to find process info in core file"));
1302 return;
1305 addr_bit = gdbarch_addr_bit (gdbarch);
1306 if (addr_bit == 64)
1307 kp = &kinfo_proc_layout_64;
1308 else if (bfd_get_arch (cbfd) == bfd_arch_i386)
1309 kp = &kinfo_proc_layout_i386;
1310 else
1311 kp = &kinfo_proc_layout_32;
1312 long_bit = gdbarch_long_bit (gdbarch);
1315 * Ensure that the note is large enough for all of the fields fetched
1316 * by this function. In particular, the note must contain the 32-bit
1317 * structure size, then it must be long enough to access the last
1318 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1320 note_size = bfd_section_size (section);
1321 if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
1322 + long_bit / TARGET_CHAR_BIT))
1323 error (_("malformed core note - too short"));
1325 gdb::def_vector<unsigned char> contents (note_size);
1326 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
1327 error (_("could not get core note contents"));
1329 descdata = contents.data ();
1331 /* Skip over the structure size. */
1332 descdata += 4;
1334 /* Verify 'ki_layout' is 0. */
1335 if (bfd_get_32 (cbfd, descdata + kp->ki_layout) != 0)
1337 warning (_("unsupported process information in core file"));
1338 return;
1341 gdb_printf ("Name: %.19s\n", descdata + kp->ki_comm);
1342 gdb_printf ("Process ID: %s\n",
1343 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pid)));
1344 gdb_printf ("Parent process: %s\n",
1345 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ppid)));
1346 gdb_printf ("Process group: %s\n",
1347 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pgid)));
1348 gdb_printf ("Session id: %s\n",
1349 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_sid)));
1351 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1352 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1353 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1354 the structure that is cleared to zero. Assume that a zero value
1355 in ki_tdev indicates a core dump from an older kernel and use the
1356 value in 'ki_tdev_freebsd11' instead. */
1357 value = bfd_get_64 (cbfd, descdata + kp->ki_tdev);
1358 if (value == 0)
1359 value = bfd_get_32 (cbfd, descdata + kp->ki_tdev_freebsd11);
1360 gdb_printf ("TTY: %s\n", pulongest (value));
1361 gdb_printf ("TTY owner process group: %s\n",
1362 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_tpgid)));
1363 gdb_printf ("User IDs (real, effective, saved): %s %s %s\n",
1364 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ruid)),
1365 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_uid)),
1366 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svuid)));
1367 gdb_printf ("Group IDs (real, effective, saved): %s %s %s\n",
1368 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_rgid)),
1369 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_groups)),
1370 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svgid)));
1371 gdb_printf ("Groups: ");
1372 uint16_t ngroups = bfd_get_16 (cbfd, descdata + kp->ki_ngroups);
1373 for (int i = 0; i < ngroups; i++)
1374 gdb_printf ("%s ",
1375 pulongest (bfd_get_32 (cbfd,
1376 descdata + kp->ki_groups + i * 4)));
1377 gdb_printf ("\n");
1378 value = bfd_get (long_bit, cbfd,
1379 descdata + kp->ki_rusage + kp->ru_minflt);
1380 gdb_printf ("Minor faults (no memory page): %s\n", pulongest (value));
1381 value = bfd_get (long_bit, cbfd,
1382 descdata + kp->ki_rusage_ch + kp->ru_minflt);
1383 gdb_printf ("Minor faults, children: %s\n", pulongest (value));
1384 value = bfd_get (long_bit, cbfd,
1385 descdata + kp->ki_rusage + kp->ru_majflt);
1386 gdb_printf ("Major faults (memory page faults): %s\n",
1387 pulongest (value));
1388 value = bfd_get (long_bit, cbfd,
1389 descdata + kp->ki_rusage_ch + kp->ru_majflt);
1390 gdb_printf ("Major faults, children: %s\n", pulongest (value));
1391 fbsd_core_fetch_timeval (gdbarch,
1392 descdata + kp->ki_rusage + kp->ru_utime,
1393 sec, value);
1394 gdb_printf ("utime: %s.%06d\n", plongest (sec), (int) value);
1395 fbsd_core_fetch_timeval (gdbarch,
1396 descdata + kp->ki_rusage + kp->ru_stime,
1397 sec, value);
1398 gdb_printf ("stime: %s.%06d\n", plongest (sec), (int) value);
1399 fbsd_core_fetch_timeval (gdbarch,
1400 descdata + kp->ki_rusage_ch + kp->ru_utime,
1401 sec, value);
1402 gdb_printf ("utime, children: %s.%06d\n", plongest (sec), (int) value);
1403 fbsd_core_fetch_timeval (gdbarch,
1404 descdata + kp->ki_rusage_ch + kp->ru_stime,
1405 sec, value);
1406 gdb_printf ("stime, children: %s.%06d\n", plongest (sec), (int) value);
1407 gdb_printf ("'nice' value: %d\n",
1408 (int) bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
1409 fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
1410 gdb_printf ("Start time: %s.%06d\n", plongest (sec), (int) value);
1411 gdb_printf ("Virtual memory size: %s kB\n",
1412 pulongest (bfd_get (addr_bit, cbfd,
1413 descdata + kp->ki_size) / 1024));
1414 gdb_printf ("Data size: %s pages\n",
1415 pulongest (bfd_get (addr_bit, cbfd,
1416 descdata + kp->ki_dsize)));
1417 gdb_printf ("Stack size: %s pages\n",
1418 pulongest (bfd_get (addr_bit, cbfd,
1419 descdata + kp->ki_ssize)));
1420 gdb_printf ("Text size: %s pages\n",
1421 pulongest (bfd_get (addr_bit, cbfd,
1422 descdata + kp->ki_tsize)));
1423 gdb_printf ("Resident set size: %s pages\n",
1424 pulongest (bfd_get (addr_bit, cbfd,
1425 descdata + kp->ki_rssize)));
1426 gdb_printf ("Maximum RSS: %s pages\n",
1427 pulongest (bfd_get (long_bit, cbfd,
1428 descdata + kp->ki_rusage
1429 + kp->ru_maxrss)));
1430 fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
1431 fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
1434 /* Implement the "core_info_proc" gdbarch method. */
1436 static void
1437 fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
1438 enum info_proc_what what)
1440 bool do_cmdline = false;
1441 bool do_cwd = false;
1442 bool do_exe = false;
1443 bool do_files = false;
1444 bool do_mappings = false;
1445 bool do_status = false;
1446 int pid;
1448 switch (what)
1450 case IP_MINIMAL:
1451 do_cmdline = true;
1452 do_cwd = true;
1453 do_exe = true;
1454 break;
1455 case IP_MAPPINGS:
1456 do_mappings = true;
1457 break;
1458 case IP_STATUS:
1459 case IP_STAT:
1460 do_status = true;
1461 break;
1462 case IP_CMDLINE:
1463 do_cmdline = true;
1464 break;
1465 case IP_EXE:
1466 do_exe = true;
1467 break;
1468 case IP_CWD:
1469 do_cwd = true;
1470 break;
1471 case IP_FILES:
1472 do_files = true;
1473 break;
1474 case IP_ALL:
1475 do_cmdline = true;
1476 do_cwd = true;
1477 do_exe = true;
1478 do_files = true;
1479 do_mappings = true;
1480 do_status = true;
1481 break;
1482 default:
1483 return;
1486 bfd *cbfd = current_program_space->core_bfd ();
1487 pid = bfd_core_file_pid (cbfd);
1488 if (pid != 0)
1489 gdb_printf (_("process %d\n"), pid);
1491 if (do_cmdline)
1493 const char *cmdline;
1495 cmdline = bfd_core_file_failing_command (cbfd);
1496 if (cmdline)
1497 gdb_printf ("cmdline = '%s'\n", cmdline);
1498 else
1499 warning (_("Command line unavailable"));
1501 if (do_cwd)
1503 gdb::unique_xmalloc_ptr<char> cwd =
1504 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
1505 if (cwd)
1506 gdb_printf ("cwd = '%s'\n", cwd.get ());
1507 else
1508 warning (_("unable to read current working directory"));
1510 if (do_exe)
1512 gdb::unique_xmalloc_ptr<char> exe =
1513 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
1514 if (exe)
1515 gdb_printf ("exe = '%s'\n", exe.get ());
1516 else
1517 warning (_("unable to read executable path name"));
1519 if (do_files)
1520 fbsd_core_info_proc_files (gdbarch);
1521 if (do_mappings)
1522 fbsd_core_info_proc_mappings (gdbarch);
1523 if (do_status)
1524 fbsd_core_info_proc_status (gdbarch);
1527 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1529 static void
1530 fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
1531 CORE_ADDR type, CORE_ADDR val)
1533 const char *name = "???";
1534 const char *description = "";
1535 enum auxv_format format = AUXV_FORMAT_HEX;
1537 switch (type)
1539 case AT_NULL:
1540 case AT_IGNORE:
1541 case AT_EXECFD:
1542 case AT_PHDR:
1543 case AT_PHENT:
1544 case AT_PHNUM:
1545 case AT_PAGESZ:
1546 case AT_BASE:
1547 case AT_FLAGS:
1548 case AT_ENTRY:
1549 case AT_NOTELF:
1550 case AT_UID:
1551 case AT_EUID:
1552 case AT_GID:
1553 case AT_EGID:
1554 default_print_auxv_entry (gdbarch, file, type, val);
1555 return;
1556 #define _TAGNAME(tag) #tag
1557 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1558 #define TAG(tag, text, kind) \
1559 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1560 TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
1561 TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
1562 TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
1563 TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
1564 TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
1565 TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
1566 TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
1567 TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
1568 TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
1569 TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
1570 TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
1571 TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
1572 TAG (BSDFLAGS, _("ELF BSD flags"), AUXV_FORMAT_HEX);
1573 TAG (ARGC, _("Argument count"), AUXV_FORMAT_DEC);
1574 TAG (ARGV, _("Argument vector"), AUXV_FORMAT_HEX);
1575 TAG (ENVC, _("Environment count"), AUXV_FORMAT_DEC);
1576 TAG (ENVV, _("Environment vector"), AUXV_FORMAT_HEX);
1577 TAG (PS_STRINGS, _("Pointer to ps_strings"), AUXV_FORMAT_HEX);
1578 TAG (FXRNG, _("Pointer to root RNG seed version"), AUXV_FORMAT_HEX);
1579 TAG (KPRELOAD, _("Base address of vDSO"), AUXV_FORMAT_HEX);
1580 TAG (USRSTACKBASE, _("Top of user stack"), AUXV_FORMAT_HEX);
1581 TAG (USRSTACKLIM, _("Grow limit of user stack"), AUXV_FORMAT_HEX);
1584 fprint_auxv_entry (file, name, description, format, type, val);
1587 /* Implement the "get_siginfo_type" gdbarch method. */
1589 static struct type *
1590 fbsd_get_siginfo_type (struct gdbarch *gdbarch)
1592 struct fbsd_gdbarch_data *fbsd_gdbarch_data;
1593 struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
1594 struct type *uid_type, *pid_type;
1595 struct type *sigval_type, *reason_type;
1596 struct type *siginfo_type;
1597 struct type *type;
1599 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
1600 if (fbsd_gdbarch_data->siginfo_type != NULL)
1601 return fbsd_gdbarch_data->siginfo_type;
1603 type_allocator alloc (gdbarch);
1604 int_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
1605 0, "int");
1606 int32_type = init_integer_type (alloc, 32, 0, "int32_t");
1607 uint32_type = init_integer_type (alloc, 32, 1, "uint32_t");
1608 long_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
1609 0, "long");
1610 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
1612 /* union sigval */
1613 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1614 sigval_type->set_name (xstrdup ("sigval"));
1615 append_composite_type_field (sigval_type, "sival_int", int_type);
1616 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
1618 /* __pid_t */
1619 pid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
1620 int32_type->length () * TARGET_CHAR_BIT,
1621 "__pid_t");
1622 pid_type->set_target_type (int32_type);
1623 pid_type->set_target_is_stub (true);
1625 /* __uid_t */
1626 uid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
1627 uint32_type->length () * TARGET_CHAR_BIT,
1628 "__uid_t");
1629 uid_type->set_target_type (uint32_type);
1630 pid_type->set_target_is_stub (true);
1632 /* _reason */
1633 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1635 /* _fault */
1636 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1637 append_composite_type_field (type, "si_trapno", int_type);
1638 append_composite_type_field (reason_type, "_fault", type);
1640 /* _timer */
1641 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1642 append_composite_type_field (type, "si_timerid", int_type);
1643 append_composite_type_field (type, "si_overrun", int_type);
1644 append_composite_type_field (reason_type, "_timer", type);
1646 /* _mesgq */
1647 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1648 append_composite_type_field (type, "si_mqd", int_type);
1649 append_composite_type_field (reason_type, "_mesgq", type);
1651 /* _poll */
1652 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1653 append_composite_type_field (type, "si_band", long_type);
1654 append_composite_type_field (reason_type, "_poll", type);
1656 /* __spare__ */
1657 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1658 append_composite_type_field (type, "__spare1__", long_type);
1659 append_composite_type_field (type, "__spare2__",
1660 init_vector_type (int_type, 7));
1661 append_composite_type_field (reason_type, "__spare__", type);
1663 /* struct siginfo */
1664 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1665 siginfo_type->set_name (xstrdup ("siginfo"));
1666 append_composite_type_field (siginfo_type, "si_signo", int_type);
1667 append_composite_type_field (siginfo_type, "si_errno", int_type);
1668 append_composite_type_field (siginfo_type, "si_code", int_type);
1669 append_composite_type_field (siginfo_type, "si_pid", pid_type);
1670 append_composite_type_field (siginfo_type, "si_uid", uid_type);
1671 append_composite_type_field (siginfo_type, "si_status", int_type);
1672 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
1673 append_composite_type_field (siginfo_type, "si_value", sigval_type);
1674 append_composite_type_field (siginfo_type, "_reason", reason_type);
1676 fbsd_gdbarch_data->siginfo_type = siginfo_type;
1678 return siginfo_type;
1681 /* Implement the "gdb_signal_from_target" gdbarch method. */
1683 static enum gdb_signal
1684 fbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
1686 switch (signal)
1688 case 0:
1689 return GDB_SIGNAL_0;
1691 case FREEBSD_SIGHUP:
1692 return GDB_SIGNAL_HUP;
1694 case FREEBSD_SIGINT:
1695 return GDB_SIGNAL_INT;
1697 case FREEBSD_SIGQUIT:
1698 return GDB_SIGNAL_QUIT;
1700 case FREEBSD_SIGILL:
1701 return GDB_SIGNAL_ILL;
1703 case FREEBSD_SIGTRAP:
1704 return GDB_SIGNAL_TRAP;
1706 case FREEBSD_SIGABRT:
1707 return GDB_SIGNAL_ABRT;
1709 case FREEBSD_SIGEMT:
1710 return GDB_SIGNAL_EMT;
1712 case FREEBSD_SIGFPE:
1713 return GDB_SIGNAL_FPE;
1715 case FREEBSD_SIGKILL:
1716 return GDB_SIGNAL_KILL;
1718 case FREEBSD_SIGBUS:
1719 return GDB_SIGNAL_BUS;
1721 case FREEBSD_SIGSEGV:
1722 return GDB_SIGNAL_SEGV;
1724 case FREEBSD_SIGSYS:
1725 return GDB_SIGNAL_SYS;
1727 case FREEBSD_SIGPIPE:
1728 return GDB_SIGNAL_PIPE;
1730 case FREEBSD_SIGALRM:
1731 return GDB_SIGNAL_ALRM;
1733 case FREEBSD_SIGTERM:
1734 return GDB_SIGNAL_TERM;
1736 case FREEBSD_SIGURG:
1737 return GDB_SIGNAL_URG;
1739 case FREEBSD_SIGSTOP:
1740 return GDB_SIGNAL_STOP;
1742 case FREEBSD_SIGTSTP:
1743 return GDB_SIGNAL_TSTP;
1745 case FREEBSD_SIGCONT:
1746 return GDB_SIGNAL_CONT;
1748 case FREEBSD_SIGCHLD:
1749 return GDB_SIGNAL_CHLD;
1751 case FREEBSD_SIGTTIN:
1752 return GDB_SIGNAL_TTIN;
1754 case FREEBSD_SIGTTOU:
1755 return GDB_SIGNAL_TTOU;
1757 case FREEBSD_SIGIO:
1758 return GDB_SIGNAL_IO;
1760 case FREEBSD_SIGXCPU:
1761 return GDB_SIGNAL_XCPU;
1763 case FREEBSD_SIGXFSZ:
1764 return GDB_SIGNAL_XFSZ;
1766 case FREEBSD_SIGVTALRM:
1767 return GDB_SIGNAL_VTALRM;
1769 case FREEBSD_SIGPROF:
1770 return GDB_SIGNAL_PROF;
1772 case FREEBSD_SIGWINCH:
1773 return GDB_SIGNAL_WINCH;
1775 case FREEBSD_SIGINFO:
1776 return GDB_SIGNAL_INFO;
1778 case FREEBSD_SIGUSR1:
1779 return GDB_SIGNAL_USR1;
1781 case FREEBSD_SIGUSR2:
1782 return GDB_SIGNAL_USR2;
1784 /* SIGTHR is the same as SIGLWP on FreeBSD. */
1785 case FREEBSD_SIGTHR:
1786 return GDB_SIGNAL_LWP;
1788 case FREEBSD_SIGLIBRT:
1789 return GDB_SIGNAL_LIBRT;
1792 if (signal >= FREEBSD_SIGRTMIN && signal <= FREEBSD_SIGRTMAX)
1794 int offset = signal - FREEBSD_SIGRTMIN;
1796 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_65 + offset);
1799 return GDB_SIGNAL_UNKNOWN;
1802 /* Implement the "gdb_signal_to_target" gdbarch method. */
1804 static int
1805 fbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
1806 enum gdb_signal signal)
1808 switch (signal)
1810 case GDB_SIGNAL_0:
1811 return 0;
1813 case GDB_SIGNAL_HUP:
1814 return FREEBSD_SIGHUP;
1816 case GDB_SIGNAL_INT:
1817 return FREEBSD_SIGINT;
1819 case GDB_SIGNAL_QUIT:
1820 return FREEBSD_SIGQUIT;
1822 case GDB_SIGNAL_ILL:
1823 return FREEBSD_SIGILL;
1825 case GDB_SIGNAL_TRAP:
1826 return FREEBSD_SIGTRAP;
1828 case GDB_SIGNAL_ABRT:
1829 return FREEBSD_SIGABRT;
1831 case GDB_SIGNAL_EMT:
1832 return FREEBSD_SIGEMT;
1834 case GDB_SIGNAL_FPE:
1835 return FREEBSD_SIGFPE;
1837 case GDB_SIGNAL_KILL:
1838 return FREEBSD_SIGKILL;
1840 case GDB_SIGNAL_BUS:
1841 return FREEBSD_SIGBUS;
1843 case GDB_SIGNAL_SEGV:
1844 return FREEBSD_SIGSEGV;
1846 case GDB_SIGNAL_SYS:
1847 return FREEBSD_SIGSYS;
1849 case GDB_SIGNAL_PIPE:
1850 return FREEBSD_SIGPIPE;
1852 case GDB_SIGNAL_ALRM:
1853 return FREEBSD_SIGALRM;
1855 case GDB_SIGNAL_TERM:
1856 return FREEBSD_SIGTERM;
1858 case GDB_SIGNAL_URG:
1859 return FREEBSD_SIGURG;
1861 case GDB_SIGNAL_STOP:
1862 return FREEBSD_SIGSTOP;
1864 case GDB_SIGNAL_TSTP:
1865 return FREEBSD_SIGTSTP;
1867 case GDB_SIGNAL_CONT:
1868 return FREEBSD_SIGCONT;
1870 case GDB_SIGNAL_CHLD:
1871 return FREEBSD_SIGCHLD;
1873 case GDB_SIGNAL_TTIN:
1874 return FREEBSD_SIGTTIN;
1876 case GDB_SIGNAL_TTOU:
1877 return FREEBSD_SIGTTOU;
1879 case GDB_SIGNAL_IO:
1880 return FREEBSD_SIGIO;
1882 case GDB_SIGNAL_XCPU:
1883 return FREEBSD_SIGXCPU;
1885 case GDB_SIGNAL_XFSZ:
1886 return FREEBSD_SIGXFSZ;
1888 case GDB_SIGNAL_VTALRM:
1889 return FREEBSD_SIGVTALRM;
1891 case GDB_SIGNAL_PROF:
1892 return FREEBSD_SIGPROF;
1894 case GDB_SIGNAL_WINCH:
1895 return FREEBSD_SIGWINCH;
1897 case GDB_SIGNAL_INFO:
1898 return FREEBSD_SIGINFO;
1900 case GDB_SIGNAL_USR1:
1901 return FREEBSD_SIGUSR1;
1903 case GDB_SIGNAL_USR2:
1904 return FREEBSD_SIGUSR2;
1906 case GDB_SIGNAL_LWP:
1907 return FREEBSD_SIGTHR;
1909 case GDB_SIGNAL_LIBRT:
1910 return FREEBSD_SIGLIBRT;
1913 if (signal >= GDB_SIGNAL_REALTIME_65
1914 && signal <= GDB_SIGNAL_REALTIME_126)
1916 int offset = signal - GDB_SIGNAL_REALTIME_65;
1918 return FREEBSD_SIGRTMIN + offset;
1921 return -1;
1924 /* Implement the "get_syscall_number" gdbarch method. */
1926 static LONGEST
1927 fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
1930 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1931 native targets fetch the system call number from the
1932 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1933 However, system call catching requires this function to be
1934 set. */
1936 internal_error (_("fbsd_get_sycall_number called"));
1939 /* Read an integer symbol value from the current target. */
1941 static LONGEST
1942 fbsd_read_integer_by_name (struct gdbarch *gdbarch, const char *name)
1944 bound_minimal_symbol ms = lookup_minimal_symbol (name, NULL, NULL);
1945 if (ms.minsym == NULL)
1946 error (_("Unable to resolve symbol '%s'"), name);
1948 gdb_byte buf[4];
1949 if (target_read_memory (ms.value_address (), buf, sizeof buf) != 0)
1950 error (_("Unable to read value of '%s'"), name);
1952 return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
1955 /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
1956 structure needed to determine the TLS index of an object file. */
1958 static void
1959 fbsd_fetch_rtld_offsets (struct gdbarch *gdbarch, struct fbsd_pspace_data *data)
1963 /* Fetch offsets from debug symbols in rtld. */
1964 struct symbol *obj_entry_sym
1965 = lookup_symbol_in_language ("Struct_Obj_Entry", nullptr,
1966 SEARCH_STRUCT_DOMAIN,
1967 language_c, nullptr).symbol;
1968 if (obj_entry_sym == NULL)
1969 error (_("Unable to find Struct_Obj_Entry symbol"));
1970 data->off_linkmap = lookup_struct_elt (obj_entry_sym->type (),
1971 "linkmap", 0).offset / 8;
1972 data->off_tlsindex = lookup_struct_elt (obj_entry_sym->type (),
1973 "tlsindex", 0).offset / 8;
1974 data->rtld_offsets_valid = true;
1975 return;
1977 catch (const gdb_exception_error &e)
1979 data->off_linkmap = -1;
1984 /* Fetch offsets from global variables in libthr. Note that
1985 this does not work for single-threaded processes that are not
1986 linked against libthr. */
1987 data->off_linkmap = fbsd_read_integer_by_name (gdbarch,
1988 "_thread_off_linkmap");
1989 data->off_tlsindex = fbsd_read_integer_by_name (gdbarch,
1990 "_thread_off_tlsindex");
1991 data->rtld_offsets_valid = true;
1992 return;
1994 catch (const gdb_exception_error &e)
1996 data->off_linkmap = -1;
2000 /* Helper function to read the TLS index of an object file associated
2001 with a link map entry at LM_ADDR. */
2003 static LONGEST
2004 fbsd_get_tls_index (struct gdbarch *gdbarch, CORE_ADDR lm_addr)
2006 struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
2008 if (!data->rtld_offsets_valid)
2009 fbsd_fetch_rtld_offsets (gdbarch, data);
2011 if (data->off_linkmap == -1)
2012 throw_error (TLS_GENERIC_ERROR,
2013 _("Cannot fetch runtime linker structure offsets"));
2015 /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
2016 pointer and then compute the offset of the tlsindex member. */
2017 CORE_ADDR tlsindex_addr = lm_addr - data->off_linkmap + data->off_tlsindex;
2019 gdb_byte buf[4];
2020 if (target_read_memory (tlsindex_addr, buf, sizeof buf) != 0)
2021 throw_error (TLS_GENERIC_ERROR,
2022 _("Cannot find thread-local variables on this target"));
2024 return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
2027 /* See fbsd-tdep.h. */
2029 CORE_ADDR
2030 fbsd_get_thread_local_address (struct gdbarch *gdbarch, CORE_ADDR dtv_addr,
2031 CORE_ADDR lm_addr, CORE_ADDR offset)
2033 LONGEST tls_index = fbsd_get_tls_index (gdbarch, lm_addr);
2035 gdb_byte buf[gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT];
2036 if (target_read_memory (dtv_addr, buf, sizeof buf) != 0)
2037 throw_error (TLS_GENERIC_ERROR,
2038 _("Cannot find thread-local variables on this target"));
2040 const struct builtin_type *builtin = builtin_type (gdbarch);
2041 CORE_ADDR addr = gdbarch_pointer_to_address (gdbarch,
2042 builtin->builtin_data_ptr, buf);
2044 addr += (tls_index + 1) * builtin->builtin_data_ptr->length ();
2045 if (target_read_memory (addr, buf, sizeof buf) != 0)
2046 throw_error (TLS_GENERIC_ERROR,
2047 _("Cannot find thread-local variables on this target"));
2049 addr = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr, buf);
2050 return addr + offset;
2053 /* See fbsd-tdep.h. */
2055 CORE_ADDR
2056 fbsd_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
2058 struct bound_minimal_symbol msym = lookup_bound_minimal_symbol ("_rtld_bind");
2059 if (msym.minsym != nullptr && msym.value_address () == pc)
2060 return frame_unwind_caller_pc (get_current_frame ());
2062 return 0;
2065 /* Return description of signal code or nullptr. */
2067 static const char *
2068 fbsd_signal_cause (enum gdb_signal siggnal, int code)
2070 /* Signal-independent causes. */
2071 switch (code)
2073 case FBSD_SI_USER:
2074 return _("Sent by kill()");
2075 case FBSD_SI_QUEUE:
2076 return _("Sent by sigqueue()");
2077 case FBSD_SI_TIMER:
2078 return _("Timer expired");
2079 case FBSD_SI_ASYNCIO:
2080 return _("Asynchronous I/O request completed");
2081 case FBSD_SI_MESGQ:
2082 return _("Message arrived on empty message queue");
2083 case FBSD_SI_KERNEL:
2084 return _("Sent by kernel");
2085 case FBSD_SI_LWP:
2086 return _("Sent by thr_kill()");
2089 switch (siggnal)
2091 case GDB_SIGNAL_ILL:
2092 switch (code)
2094 case FBSD_ILL_ILLOPC:
2095 return _("Illegal opcode");
2096 case FBSD_ILL_ILLOPN:
2097 return _("Illegal operand");
2098 case FBSD_ILL_ILLADR:
2099 return _("Illegal addressing mode");
2100 case FBSD_ILL_ILLTRP:
2101 return _("Illegal trap");
2102 case FBSD_ILL_PRVOPC:
2103 return _("Privileged opcode");
2104 case FBSD_ILL_PRVREG:
2105 return _("Privileged register");
2106 case FBSD_ILL_COPROC:
2107 return _("Coprocessor error");
2108 case FBSD_ILL_BADSTK:
2109 return _("Internal stack error");
2111 break;
2112 case GDB_SIGNAL_BUS:
2113 switch (code)
2115 case FBSD_BUS_ADRALN:
2116 return _("Invalid address alignment");
2117 case FBSD_BUS_ADRERR:
2118 return _("Address not present");
2119 case FBSD_BUS_OBJERR:
2120 return _("Object-specific hardware error");
2121 case FBSD_BUS_OOMERR:
2122 return _("Out of memory");
2124 break;
2125 case GDB_SIGNAL_SEGV:
2126 switch (code)
2128 case FBSD_SEGV_MAPERR:
2129 return _("Address not mapped to object");
2130 case FBSD_SEGV_ACCERR:
2131 return _("Invalid permissions for mapped object");
2132 case FBSD_SEGV_PKUERR:
2133 return _("PKU violation");
2135 break;
2136 case GDB_SIGNAL_FPE:
2137 switch (code)
2139 case FBSD_FPE_INTOVF:
2140 return _("Integer overflow");
2141 case FBSD_FPE_INTDIV:
2142 return _("Integer divide by zero");
2143 case FBSD_FPE_FLTDIV:
2144 return _("Floating point divide by zero");
2145 case FBSD_FPE_FLTOVF:
2146 return _("Floating point overflow");
2147 case FBSD_FPE_FLTUND:
2148 return _("Floating point underflow");
2149 case FBSD_FPE_FLTRES:
2150 return _("Floating point inexact result");
2151 case FBSD_FPE_FLTINV:
2152 return _("Invalid floating point operation");
2153 case FBSD_FPE_FLTSUB:
2154 return _("Subscript out of range");
2156 break;
2157 case GDB_SIGNAL_TRAP:
2158 switch (code)
2160 case FBSD_TRAP_BRKPT:
2161 return _("Breakpoint");
2162 case FBSD_TRAP_TRACE:
2163 return _("Trace trap");
2164 case FBSD_TRAP_DTRACE:
2165 return _("DTrace-induced trap");
2166 case FBSD_TRAP_CAP:
2167 return _("Capability violation");
2169 break;
2170 case GDB_SIGNAL_CHLD:
2171 switch (code)
2173 case FBSD_CLD_EXITED:
2174 return _("Child has exited");
2175 case FBSD_CLD_KILLED:
2176 return _("Child has terminated abnormally");
2177 case FBSD_CLD_DUMPED:
2178 return _("Child has dumped core");
2179 case FBSD_CLD_TRAPPED:
2180 return _("Traced child has trapped");
2181 case FBSD_CLD_STOPPED:
2182 return _("Child has stopped");
2183 case FBSD_CLD_CONTINUED:
2184 return _("Stopped child has continued");
2186 break;
2187 case GDB_SIGNAL_POLL:
2188 switch (code)
2190 case FBSD_POLL_IN:
2191 return _("Data input available");
2192 case FBSD_POLL_OUT:
2193 return _("Output buffers available");
2194 case FBSD_POLL_MSG:
2195 return _("Input message available");
2196 case FBSD_POLL_ERR:
2197 return _("I/O error");
2198 case FBSD_POLL_PRI:
2199 return _("High priority input available");
2200 case FBSD_POLL_HUP:
2201 return _("Device disconnected");
2203 break;
2206 return nullptr;
2209 /* Report additional details for a signal stop. */
2211 static void
2212 fbsd_report_signal_info (struct gdbarch *gdbarch, struct ui_out *uiout,
2213 enum gdb_signal siggnal)
2215 LONGEST code, mqd, pid, status, timerid, uid;
2219 code = parse_and_eval_long ("$_siginfo.si_code");
2220 pid = parse_and_eval_long ("$_siginfo.si_pid");
2221 uid = parse_and_eval_long ("$_siginfo.si_uid");
2222 status = parse_and_eval_long ("$_siginfo.si_status");
2223 timerid = parse_and_eval_long ("$_siginfo._reason._timer.si_timerid");
2224 mqd = parse_and_eval_long ("$_siginfo._reason._mesgq.si_mqd");
2226 catch (const gdb_exception_error &e)
2228 return;
2231 const char *meaning = fbsd_signal_cause (siggnal, code);
2232 if (meaning == nullptr)
2233 return;
2235 uiout->text (".\n");
2236 uiout->field_string ("sigcode-meaning", meaning);
2238 switch (code)
2240 case FBSD_SI_USER:
2241 case FBSD_SI_QUEUE:
2242 case FBSD_SI_LWP:
2243 uiout->text (" from pid ");
2244 uiout->field_string ("sending-pid", plongest (pid));
2245 uiout->text (" and user ");
2246 uiout->field_string ("sending-uid", plongest (uid));
2247 return;
2248 case FBSD_SI_TIMER:
2249 uiout->text (": timerid ");
2250 uiout->field_string ("timerid", plongest (timerid));
2251 return;
2252 case FBSD_SI_MESGQ:
2253 uiout->text (": message queue ");
2254 uiout->field_string ("message-queue", plongest (mqd));
2255 return;
2256 case FBSD_SI_ASYNCIO:
2257 return;
2260 if (siggnal == GDB_SIGNAL_CHLD)
2262 uiout->text (": pid ");
2263 uiout->field_string ("child-pid", plongest (pid));
2264 uiout->text (", uid ");
2265 uiout->field_string ("child-uid", plongest (uid));
2266 if (code == FBSD_CLD_EXITED)
2268 uiout->text (", exit status ");
2269 uiout->field_string ("exit-status", plongest (status));
2271 else
2273 uiout->text (", signal ");
2274 uiout->field_string ("signal", plongest (status));
2279 /* Search a list of struct kinfo_vmmap entries in the ENTRIES buffer
2280 of LEN bytes to find the length of the entry starting at ADDR.
2281 Returns the length of the entry or zero if no entry was found. */
2283 static ULONGEST
2284 fbsd_vmmap_length (struct gdbarch *gdbarch, unsigned char *entries, size_t len,
2285 CORE_ADDR addr)
2287 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2288 unsigned char *descdata = entries;
2289 unsigned char *descend = descdata + len;
2291 /* Skip over the structure size. */
2292 descdata += 4;
2294 while (descdata + KVE_PATH < descend)
2296 ULONGEST structsize = extract_unsigned_integer (descdata
2297 + KVE_STRUCTSIZE, 4,
2298 byte_order);
2299 if (structsize < KVE_PATH)
2300 return false;
2302 ULONGEST start = extract_unsigned_integer (descdata + KVE_START, 8,
2303 byte_order);
2304 ULONGEST end = extract_unsigned_integer (descdata + KVE_END, 8,
2305 byte_order);
2306 if (start == addr)
2307 return end - start;
2309 descdata += structsize;
2311 return 0;
2314 /* Helper for fbsd_vsyscall_range that does the real work of finding
2315 the vDSO's address range. */
2317 static bool
2318 fbsd_vdso_range (struct gdbarch *gdbarch, struct mem_range *range)
2320 if (target_auxv_search (AT_FREEBSD_KPRELOAD, &range->start) <= 0)
2321 return false;
2323 if (!target_has_execution ())
2325 /* Search for the ending address in the NT_PROCSTAT_VMMAP note. */
2326 bfd *cbfd = current_program_space->core_bfd ();
2327 asection *section = bfd_get_section_by_name (cbfd,
2328 ".note.freebsdcore.vmmap");
2329 if (section == nullptr)
2330 return false;
2332 size_t note_size = bfd_section_size (section);
2333 if (note_size < 4)
2334 return false;
2336 gdb::def_vector<unsigned char> contents (note_size);
2337 if (!bfd_get_section_contents (cbfd, section, contents.data (),
2338 0, note_size))
2339 return false;
2341 range->length = fbsd_vmmap_length (gdbarch, contents.data (), note_size,
2342 range->start);
2344 else
2346 /* Fetch the list of address space entries from the running target. */
2347 std::optional<gdb::byte_vector> buf =
2348 target_read_alloc (current_inferior ()->top_target (),
2349 TARGET_OBJECT_FREEBSD_VMMAP, nullptr);
2350 if (!buf || buf->empty ())
2351 return false;
2353 range->length = fbsd_vmmap_length (gdbarch, buf->data (), buf->size (),
2354 range->start);
2356 return range->length != 0;
2359 /* Return the address range of the vDSO for the current inferior. */
2361 static int
2362 fbsd_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
2364 struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
2366 if (data->vdso_range_p == 0)
2368 if (fbsd_vdso_range (gdbarch, &data->vdso_range))
2369 data->vdso_range_p = 1;
2370 else
2371 data->vdso_range_p = -1;
2374 if (data->vdso_range_p < 0)
2375 return 0;
2377 *range = data->vdso_range;
2378 return 1;
2381 /* To be called from GDB_OSABI_FREEBSD handlers. */
2383 void
2384 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2386 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
2387 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
2388 set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
2389 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
2390 set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
2391 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
2392 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
2393 set_gdbarch_gdb_signal_from_target (gdbarch, fbsd_gdb_signal_from_target);
2394 set_gdbarch_gdb_signal_to_target (gdbarch, fbsd_gdb_signal_to_target);
2395 set_gdbarch_report_signal_info (gdbarch, fbsd_report_signal_info);
2396 set_gdbarch_skip_solib_resolver (gdbarch, fbsd_skip_solib_resolver);
2397 set_gdbarch_vsyscall_range (gdbarch, fbsd_vsyscall_range);
2399 /* `catch syscall' */
2400 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
2401 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);