testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / fbsd-tdep.c
blobac51f101d2711b369f79e9606426b2eca0d36cab
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 "exceptions.h"
22 #include "extract-store-integer.h"
23 #include "gdbcore.h"
24 #include "inferior.h"
25 #include "objfiles.h"
26 #include "regcache.h"
27 #include "regset.h"
28 #include "gdbthread.h"
29 #include "objfiles.h"
30 #include "xml-syscall.h"
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
34 #include "elf-bfd.h"
35 #include "fbsd-tdep.h"
36 #include "gcore-elf.h"
38 /* This enum is derived from FreeBSD's <sys/signal.h>. */
40 enum
42 FREEBSD_SIGHUP = 1,
43 FREEBSD_SIGINT = 2,
44 FREEBSD_SIGQUIT = 3,
45 FREEBSD_SIGILL = 4,
46 FREEBSD_SIGTRAP = 5,
47 FREEBSD_SIGABRT = 6,
48 FREEBSD_SIGEMT = 7,
49 FREEBSD_SIGFPE = 8,
50 FREEBSD_SIGKILL = 9,
51 FREEBSD_SIGBUS = 10,
52 FREEBSD_SIGSEGV = 11,
53 FREEBSD_SIGSYS = 12,
54 FREEBSD_SIGPIPE = 13,
55 FREEBSD_SIGALRM = 14,
56 FREEBSD_SIGTERM = 15,
57 FREEBSD_SIGURG = 16,
58 FREEBSD_SIGSTOP = 17,
59 FREEBSD_SIGTSTP = 18,
60 FREEBSD_SIGCONT = 19,
61 FREEBSD_SIGCHLD = 20,
62 FREEBSD_SIGTTIN = 21,
63 FREEBSD_SIGTTOU = 22,
64 FREEBSD_SIGIO = 23,
65 FREEBSD_SIGXCPU = 24,
66 FREEBSD_SIGXFSZ = 25,
67 FREEBSD_SIGVTALRM = 26,
68 FREEBSD_SIGPROF = 27,
69 FREEBSD_SIGWINCH = 28,
70 FREEBSD_SIGINFO = 29,
71 FREEBSD_SIGUSR1 = 30,
72 FREEBSD_SIGUSR2 = 31,
73 FREEBSD_SIGTHR = 32,
74 FREEBSD_SIGLIBRT = 33,
75 FREEBSD_SIGRTMIN = 65,
76 FREEBSD_SIGRTMAX = 126,
79 /* Constants for values of si_code as defined in FreeBSD's
80 <sys/signal.h>. */
82 #define FBSD_SI_USER 0x10001
83 #define FBSD_SI_QUEUE 0x10002
84 #define FBSD_SI_TIMER 0x10003
85 #define FBSD_SI_ASYNCIO 0x10004
86 #define FBSD_SI_MESGQ 0x10005
87 #define FBSD_SI_KERNEL 0x10006
88 #define FBSD_SI_LWP 0x10007
90 #define FBSD_ILL_ILLOPC 1
91 #define FBSD_ILL_ILLOPN 2
92 #define FBSD_ILL_ILLADR 3
93 #define FBSD_ILL_ILLTRP 4
94 #define FBSD_ILL_PRVOPC 5
95 #define FBSD_ILL_PRVREG 6
96 #define FBSD_ILL_COPROC 7
97 #define FBSD_ILL_BADSTK 8
99 #define FBSD_BUS_ADRALN 1
100 #define FBSD_BUS_ADRERR 2
101 #define FBSD_BUS_OBJERR 3
102 #define FBSD_BUS_OOMERR 100
104 #define FBSD_SEGV_MAPERR 1
105 #define FBSD_SEGV_ACCERR 2
106 #define FBSD_SEGV_PKUERR 100
108 #define FBSD_FPE_INTOVF 1
109 #define FBSD_FPE_INTDIV 2
110 #define FBSD_FPE_FLTDIV 3
111 #define FBSD_FPE_FLTOVF 4
112 #define FBSD_FPE_FLTUND 5
113 #define FBSD_FPE_FLTRES 6
114 #define FBSD_FPE_FLTINV 7
115 #define FBSD_FPE_FLTSUB 8
117 #define FBSD_TRAP_BRKPT 1
118 #define FBSD_TRAP_TRACE 2
119 #define FBSD_TRAP_DTRACE 3
120 #define FBSD_TRAP_CAP 4
122 #define FBSD_CLD_EXITED 1
123 #define FBSD_CLD_KILLED 2
124 #define FBSD_CLD_DUMPED 3
125 #define FBSD_CLD_TRAPPED 4
126 #define FBSD_CLD_STOPPED 5
127 #define FBSD_CLD_CONTINUED 6
129 #define FBSD_POLL_IN 1
130 #define FBSD_POLL_OUT 2
131 #define FBSD_POLL_MSG 3
132 #define FBSD_POLL_ERR 4
133 #define FBSD_POLL_PRI 5
134 #define FBSD_POLL_HUP 6
136 /* FreeBSD kernels 12.0 and later include a copy of the
137 'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
138 operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
139 The constants below define the offset of field members and flags in
140 this structure used by methods in this file. Note that the
141 'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
142 containing the size of the structure. */
144 #define LWPINFO_OFFSET 0x4
146 /* Offsets in ptrace_lwpinfo. */
147 #define LWPINFO_PL_FLAGS 0x8
148 #define LWPINFO64_PL_SIGINFO 0x30
149 #define LWPINFO32_PL_SIGINFO 0x2c
151 /* Flags in pl_flags. */
152 #define PL_FLAG_SI 0x20 /* siginfo is valid */
154 /* Sizes of siginfo_t. */
155 #define SIZE64_SIGINFO_T 80
156 #define SIZE32_SIGINFO_T 64
158 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
159 dump notes. See <sys/user.h> for the definition of struct
160 kinfo_vmentry. This data structure should have the same layout on
161 all architectures.
163 Note that FreeBSD 7.0 used an older version of this structure
164 (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
165 dump note wasn't introduced until FreeBSD 9.2. As a result, the
166 core dump note has always used the 7.1 and later structure
167 format. */
169 #define KVE_STRUCTSIZE 0x0
170 #define KVE_START 0x8
171 #define KVE_END 0x10
172 #define KVE_OFFSET 0x18
173 #define KVE_FLAGS 0x2c
174 #define KVE_PROTECTION 0x38
175 #define KVE_PATH 0x88
177 /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
178 match the KVME_PROT_* constants in <sys/user.h>. */
180 #define KINFO_VME_PROT_READ 0x00000001
181 #define KINFO_VME_PROT_WRITE 0x00000002
182 #define KINFO_VME_PROT_EXEC 0x00000004
184 /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
185 match the KVME_FLAG_* constants in <sys/user.h>. */
187 #define KINFO_VME_FLAG_COW 0x00000001
188 #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
189 #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
190 #define KINFO_VME_FLAG_SUPER 0x00000008
191 #define KINFO_VME_FLAG_GROWS_UP 0x00000010
192 #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
194 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
195 dump notes. See <sys/user.h> for the definition of struct
196 kinfo_file. This data structure should have the same layout on all
197 architectures.
199 Note that FreeBSD 7.0 used an older version of this structure
200 (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
201 note wasn't introduced until FreeBSD 9.2. As a result, the core
202 dump note has always used the 7.1 and later structure format. */
204 #define KF_STRUCTSIZE 0x0
205 #define KF_TYPE 0x4
206 #define KF_FD 0x8
207 #define KF_FLAGS 0x10
208 #define KF_OFFSET 0x18
209 #define KF_VNODE_TYPE 0x20
210 #define KF_SOCK_DOMAIN 0x24
211 #define KF_SOCK_TYPE 0x28
212 #define KF_SOCK_PROTOCOL 0x2c
213 #define KF_SA_LOCAL 0x30
214 #define KF_SA_PEER 0xb0
215 #define KF_PATH 0x170
217 /* Constants for the 'kf_type' field in struct kinfo_file. These
218 match the KF_TYPE_* constants in <sys/user.h>. */
220 #define KINFO_FILE_TYPE_VNODE 1
221 #define KINFO_FILE_TYPE_SOCKET 2
222 #define KINFO_FILE_TYPE_PIPE 3
223 #define KINFO_FILE_TYPE_FIFO 4
224 #define KINFO_FILE_TYPE_KQUEUE 5
225 #define KINFO_FILE_TYPE_CRYPTO 6
226 #define KINFO_FILE_TYPE_MQUEUE 7
227 #define KINFO_FILE_TYPE_SHM 8
228 #define KINFO_FILE_TYPE_SEM 9
229 #define KINFO_FILE_TYPE_PTS 10
230 #define KINFO_FILE_TYPE_PROCDESC 11
232 /* Special values for the 'kf_fd' field in struct kinfo_file. These
233 match the KF_FD_TYPE_* constants in <sys/user.h>. */
235 #define KINFO_FILE_FD_TYPE_CWD -1
236 #define KINFO_FILE_FD_TYPE_ROOT -2
237 #define KINFO_FILE_FD_TYPE_JAIL -3
238 #define KINFO_FILE_FD_TYPE_TRACE -4
239 #define KINFO_FILE_FD_TYPE_TEXT -5
240 #define KINFO_FILE_FD_TYPE_CTTY -6
242 /* Flags in the 'kf_flags' field in struct kinfo_file. These match
243 the KF_FLAG_* constants in <sys/user.h>. */
245 #define KINFO_FILE_FLAG_READ 0x00000001
246 #define KINFO_FILE_FLAG_WRITE 0x00000002
247 #define KINFO_FILE_FLAG_APPEND 0x00000004
248 #define KINFO_FILE_FLAG_ASYNC 0x00000008
249 #define KINFO_FILE_FLAG_FSYNC 0x00000010
250 #define KINFO_FILE_FLAG_NONBLOCK 0x00000020
251 #define KINFO_FILE_FLAG_DIRECT 0x00000040
252 #define KINFO_FILE_FLAG_HASLOCK 0x00000080
253 #define KINFO_FILE_FLAG_EXEC 0x00004000
255 /* Constants for the 'kf_vnode_type' field in struct kinfo_file.
256 These match the KF_VTYPE_* constants in <sys/user.h>. */
258 #define KINFO_FILE_VTYPE_VREG 1
259 #define KINFO_FILE_VTYPE_VDIR 2
260 #define KINFO_FILE_VTYPE_VCHR 4
261 #define KINFO_FILE_VTYPE_VLNK 5
262 #define KINFO_FILE_VTYPE_VSOCK 6
263 #define KINFO_FILE_VTYPE_VFIFO 7
265 /* Constants for socket address families. These match AF_* constants
266 in <sys/socket.h>. */
268 #define FBSD_AF_UNIX 1
269 #define FBSD_AF_INET 2
270 #define FBSD_AF_INET6 28
272 /* Constants for socket types. These match SOCK_* constants in
273 <sys/socket.h>. */
275 #define FBSD_SOCK_STREAM 1
276 #define FBSD_SOCK_DGRAM 2
277 #define FBSD_SOCK_SEQPACKET 5
279 /* Constants for IP protocols. These match IPPROTO_* constants in
280 <netinet/in.h>. */
282 #define FBSD_IPPROTO_ICMP 1
283 #define FBSD_IPPROTO_TCP 6
284 #define FBSD_IPPROTO_UDP 17
285 #define FBSD_IPPROTO_SCTP 132
287 /* Socket address structures. These have the same layout on all
288 FreeBSD architectures. In addition, multibyte fields such as IP
289 addresses are always stored in network byte order. */
291 struct fbsd_sockaddr_in
293 uint8_t sin_len;
294 uint8_t sin_family;
295 uint8_t sin_port[2];
296 uint8_t sin_addr[4];
297 char sin_zero[8];
300 struct fbsd_sockaddr_in6
302 uint8_t sin6_len;
303 uint8_t sin6_family;
304 uint8_t sin6_port[2];
305 uint32_t sin6_flowinfo;
306 uint8_t sin6_addr[16];
307 uint32_t sin6_scope_id;
310 struct fbsd_sockaddr_un
312 uint8_t sun_len;
313 uint8_t sun_family;
314 char sun_path[104];
317 /* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
318 <sys/_sigset.h> and is the same value on all architectures. */
320 #define SIG_WORDS 4
322 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
323 dump notes. See <sys/user.h> for the definition of struct
324 kinfo_proc. This data structure has different layouts on different
325 architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
326 uses a 32-bit time_t while all other architectures use a 64-bit
327 time_t.
329 The core dump note actually contains one kinfo_proc structure for
330 each thread, but all of the process-wide data can be obtained from
331 the first structure. One result of this note's format is that some
332 of the process-wide status available in the native target method
333 from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
334 is not available from a core dump. Instead, the per-thread data
335 structures contain the value of these fields for individual
336 threads. */
338 struct kinfo_proc_layout
340 /* Offsets of struct kinfo_proc members. */
341 int ki_layout;
342 int ki_pid;
343 int ki_ppid;
344 int ki_pgid;
345 int ki_tpgid;
346 int ki_sid;
347 int ki_tdev_freebsd11;
348 int ki_sigignore;
349 int ki_sigcatch;
350 int ki_uid;
351 int ki_ruid;
352 int ki_svuid;
353 int ki_rgid;
354 int ki_svgid;
355 int ki_ngroups;
356 int ki_groups;
357 int ki_size;
358 int ki_rssize;
359 int ki_tsize;
360 int ki_dsize;
361 int ki_ssize;
362 int ki_start;
363 int ki_nice;
364 int ki_comm;
365 int ki_tdev;
366 int ki_rusage;
367 int ki_rusage_ch;
369 /* Offsets of struct rusage members. */
370 int ru_utime;
371 int ru_stime;
372 int ru_maxrss;
373 int ru_minflt;
374 int ru_majflt;
377 const struct kinfo_proc_layout kinfo_proc_layout_32 =
379 .ki_layout = 0x4,
380 .ki_pid = 0x28,
381 .ki_ppid = 0x2c,
382 .ki_pgid = 0x30,
383 .ki_tpgid = 0x34,
384 .ki_sid = 0x38,
385 .ki_tdev_freebsd11 = 0x44,
386 .ki_sigignore = 0x68,
387 .ki_sigcatch = 0x78,
388 .ki_uid = 0x88,
389 .ki_ruid = 0x8c,
390 .ki_svuid = 0x90,
391 .ki_rgid = 0x94,
392 .ki_svgid = 0x98,
393 .ki_ngroups = 0x9c,
394 .ki_groups = 0xa0,
395 .ki_size = 0xe0,
396 .ki_rssize = 0xe4,
397 .ki_tsize = 0xec,
398 .ki_dsize = 0xf0,
399 .ki_ssize = 0xf4,
400 .ki_start = 0x118,
401 .ki_nice = 0x145,
402 .ki_comm = 0x17f,
403 .ki_tdev = 0x1f0,
404 .ki_rusage = 0x220,
405 .ki_rusage_ch = 0x278,
407 .ru_utime = 0x0,
408 .ru_stime = 0x10,
409 .ru_maxrss = 0x20,
410 .ru_minflt = 0x30,
411 .ru_majflt = 0x34,
414 const struct kinfo_proc_layout kinfo_proc_layout_i386 =
416 .ki_layout = 0x4,
417 .ki_pid = 0x28,
418 .ki_ppid = 0x2c,
419 .ki_pgid = 0x30,
420 .ki_tpgid = 0x34,
421 .ki_sid = 0x38,
422 .ki_tdev_freebsd11 = 0x44,
423 .ki_sigignore = 0x68,
424 .ki_sigcatch = 0x78,
425 .ki_uid = 0x88,
426 .ki_ruid = 0x8c,
427 .ki_svuid = 0x90,
428 .ki_rgid = 0x94,
429 .ki_svgid = 0x98,
430 .ki_ngroups = 0x9c,
431 .ki_groups = 0xa0,
432 .ki_size = 0xe0,
433 .ki_rssize = 0xe4,
434 .ki_tsize = 0xec,
435 .ki_dsize = 0xf0,
436 .ki_ssize = 0xf4,
437 .ki_start = 0x118,
438 .ki_nice = 0x135,
439 .ki_comm = 0x16f,
440 .ki_tdev = 0x1e0,
441 .ki_rusage = 0x210,
442 .ki_rusage_ch = 0x258,
444 .ru_utime = 0x0,
445 .ru_stime = 0x8,
446 .ru_maxrss = 0x10,
447 .ru_minflt = 0x20,
448 .ru_majflt = 0x24,
451 const struct kinfo_proc_layout kinfo_proc_layout_64 =
453 .ki_layout = 0x4,
454 .ki_pid = 0x48,
455 .ki_ppid = 0x4c,
456 .ki_pgid = 0x50,
457 .ki_tpgid = 0x54,
458 .ki_sid = 0x58,
459 .ki_tdev_freebsd11 = 0x64,
460 .ki_sigignore = 0x88,
461 .ki_sigcatch = 0x98,
462 .ki_uid = 0xa8,
463 .ki_ruid = 0xac,
464 .ki_svuid = 0xb0,
465 .ki_rgid = 0xb4,
466 .ki_svgid = 0xb8,
467 .ki_ngroups = 0xbc,
468 .ki_groups = 0xc0,
469 .ki_size = 0x100,
470 .ki_rssize = 0x108,
471 .ki_tsize = 0x118,
472 .ki_dsize = 0x120,
473 .ki_ssize = 0x128,
474 .ki_start = 0x150,
475 .ki_nice = 0x185,
476 .ki_comm = 0x1bf,
477 .ki_tdev = 0x230,
478 .ki_rusage = 0x260,
479 .ki_rusage_ch = 0x2f0,
481 .ru_utime = 0x0,
482 .ru_stime = 0x10,
483 .ru_maxrss = 0x20,
484 .ru_minflt = 0x40,
485 .ru_majflt = 0x48,
488 struct fbsd_gdbarch_data
490 struct type *siginfo_type = nullptr;
493 static const registry<gdbarch>::key<fbsd_gdbarch_data>
494 fbsd_gdbarch_data_handle;
496 static struct fbsd_gdbarch_data *
497 get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
499 struct fbsd_gdbarch_data *result = fbsd_gdbarch_data_handle.get (gdbarch);
500 if (result == nullptr)
501 result = fbsd_gdbarch_data_handle.emplace (gdbarch);
502 return result;
505 struct fbsd_pspace_data
507 /* Offsets in the runtime linker's 'Obj_Entry' structure. */
508 LONGEST off_linkmap = 0;
509 LONGEST off_tlsindex = 0;
510 bool rtld_offsets_valid = false;
512 /* vDSO mapping range. */
513 struct mem_range vdso_range {};
515 /* Zero if the range hasn't been searched for, > 0 if a range was
516 found, or < 0 if a range was not found. */
517 int vdso_range_p = 0;
520 /* Per-program-space data for FreeBSD architectures. */
521 static const registry<program_space>::key<fbsd_pspace_data>
522 fbsd_pspace_data_handle;
524 static struct fbsd_pspace_data *
525 get_fbsd_pspace_data (struct program_space *pspace)
527 struct fbsd_pspace_data *data;
529 data = fbsd_pspace_data_handle.get (pspace);
530 if (data == NULL)
531 data = fbsd_pspace_data_handle.emplace (pspace);
533 return data;
536 /* This is how we want PTIDs from core files to be printed. */
538 static std::string
539 fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
541 if (ptid.lwp () != 0)
542 return string_printf ("LWP %ld", ptid.lwp ());
544 return normal_pid_to_str (ptid);
547 /* Extract the name assigned to a thread from a core. Returns the
548 string in a static buffer. */
550 static const char *
551 fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
553 static char buf[80];
554 struct bfd_section *section;
555 bfd_size_type size;
557 if (thr->ptid.lwp () != 0)
559 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
560 whose contents are defined by a "struct thrmisc" declared in
561 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
562 a null-terminated string as the first member of the
563 structure. Rather than define the full structure here, just
564 extract the null-terminated name from the start of the
565 note. */
566 thread_section_name section_name (".thrmisc", thr->ptid);
567 bfd *cbfd = current_program_space->core_bfd ();
569 section = bfd_get_section_by_name (cbfd, section_name.c_str ());
570 if (section != NULL && bfd_section_size (section) > 0)
572 /* Truncate the name if it is longer than "buf". */
573 size = bfd_section_size (section);
574 if (size > sizeof buf - 1)
575 size = sizeof buf - 1;
576 if (bfd_get_section_contents (cbfd, section, buf, (file_ptr) 0, size)
577 && buf[0] != '\0')
579 buf[size] = '\0';
581 /* Note that each thread will report the process command
582 as its thread name instead of an empty name if a name
583 has not been set explicitly. Return a NULL name in
584 that case. */
585 if (strcmp (buf, elf_tdata (cbfd)->core->program) != 0)
586 return buf;
591 return NULL;
594 /* Implement the "core_xfer_siginfo" gdbarch method. */
596 static LONGEST
597 fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
598 ULONGEST offset, ULONGEST len)
600 size_t siginfo_size;
602 if (gdbarch_long_bit (gdbarch) == 32)
603 siginfo_size = SIZE32_SIGINFO_T;
604 else
605 siginfo_size = SIZE64_SIGINFO_T;
606 if (offset > siginfo_size)
607 return -1;
609 thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
610 bfd *cbfd = current_program_space->core_bfd ();
611 asection *section = bfd_get_section_by_name (cbfd, section_name.c_str ());
612 if (section == NULL)
613 return -1;
615 gdb_byte buf[4];
616 if (!bfd_get_section_contents (cbfd, section, buf,
617 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
618 return -1;
620 int pl_flags = extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
621 if (!(pl_flags & PL_FLAG_SI))
622 return -1;
624 if (offset + len > siginfo_size)
625 len = siginfo_size - offset;
627 ULONGEST siginfo_offset;
628 if (gdbarch_long_bit (gdbarch) == 32)
629 siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
630 else
631 siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
633 if (!bfd_get_section_contents (cbfd, section, readbuf,
634 siginfo_offset + offset, len))
635 return -1;
637 return len;
640 static int
641 find_signalled_thread (struct thread_info *info, void *data)
643 if (info->stop_signal () != GDB_SIGNAL_0
644 && info->ptid.pid () == inferior_ptid.pid ())
645 return 1;
647 return 0;
650 /* Return a byte_vector containing the contents of a core dump note
651 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
652 the data is prefixed with a 32-bit integer size to match the format
653 used in FreeBSD NT_PROCSTAT_* notes. */
655 static std::optional<gdb::byte_vector>
656 fbsd_make_note_desc (enum target_object object, uint32_t structsize)
658 std::optional<gdb::byte_vector> buf =
659 target_read_alloc (current_inferior ()->top_target (), object, NULL);
660 if (!buf || buf->empty ())
661 return {};
663 if (structsize == 0)
664 return buf;
666 gdb::byte_vector desc (sizeof (structsize) + buf->size ());
667 memcpy (desc.data (), &structsize, sizeof (structsize));
668 std::copy (buf->begin (), buf->end (), desc.data () + sizeof (structsize));
669 return desc;
672 /* Create appropriate note sections for a corefile, returning them in
673 allocated memory. */
675 static gdb::unique_xmalloc_ptr<char>
676 fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
678 gdb::unique_xmalloc_ptr<char> note_data;
679 Elf_Internal_Ehdr *i_ehdrp;
680 struct thread_info *curr_thr, *signalled_thr;
682 /* Put a "FreeBSD" label in the ELF header. */
683 i_ehdrp = elf_elfheader (obfd);
684 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
686 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
688 if (current_program_space->exec_filename () != nullptr)
690 const char *fname = lbasename (current_program_space->exec_filename ());
691 std::string psargs = fname;
693 const std::string &infargs = current_inferior ()->args ();
694 if (!infargs.empty ())
695 psargs += ' ' + infargs;
697 note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
698 note_size, fname,
699 psargs.c_str ()));
702 /* Thread register information. */
705 update_thread_list ();
707 catch (const gdb_exception_error &e)
709 exception_print (gdb_stderr, e);
712 /* Like the kernel, prefer dumping the signalled thread first.
713 "First thread" is what tools use to infer the signalled thread.
714 In case there's more than one signalled thread, prefer the
715 current thread, if it is signalled. */
716 curr_thr = inferior_thread ();
717 if (curr_thr->stop_signal () != GDB_SIGNAL_0)
718 signalled_thr = curr_thr;
719 else
721 signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
722 if (signalled_thr == NULL)
723 signalled_thr = curr_thr;
726 enum gdb_signal stop_signal = signalled_thr->stop_signal ();
727 gcore_elf_build_thread_register_notes (gdbarch, signalled_thr, stop_signal,
728 obfd, &note_data, note_size);
729 for (thread_info *thr : current_inferior ()->non_exited_threads ())
731 if (thr == signalled_thr)
732 continue;
734 gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal,
735 obfd, &note_data, note_size);
738 /* Auxiliary vector. */
739 uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo */
740 std::optional<gdb::byte_vector> note_desc =
741 fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
742 if (note_desc && !note_desc->empty ())
744 note_data.reset (elfcore_write_note (obfd, note_data.release (),
745 note_size, "FreeBSD",
746 NT_FREEBSD_PROCSTAT_AUXV,
747 note_desc->data (),
748 note_desc->size ()));
749 if (!note_data)
750 return NULL;
753 /* Virtual memory mappings. */
754 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
755 if (note_desc && !note_desc->empty ())
757 note_data.reset (elfcore_write_note (obfd, note_data.release (),
758 note_size, "FreeBSD",
759 NT_FREEBSD_PROCSTAT_VMMAP,
760 note_desc->data (),
761 note_desc->size ()));
762 if (!note_data)
763 return NULL;
766 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
767 if (note_desc && !note_desc->empty ())
769 note_data.reset (elfcore_write_note (obfd, note_data.release (),
770 note_size, "FreeBSD",
771 NT_FREEBSD_PROCSTAT_PSSTRINGS,
772 note_desc->data (),
773 note_desc->size ()));
774 if (!note_data)
775 return NULL;
778 /* Include the target description when possible. Some architectures
779 allow for per-thread gdbarch so we should really be emitting a tdesc
780 per-thread, however, we don't currently support reading in a
781 per-thread tdesc, so just emit the tdesc for the signalled thread. */
782 gdbarch = target_thread_architecture (signalled_thr->ptid);
783 gcore_elf_make_tdesc_note (gdbarch, obfd, &note_data, note_size);
785 return note_data;
788 /* Helper function to generate the file descriptor description for a
789 single open file in 'info proc files'. */
791 static const char *
792 fbsd_file_fd (int kf_fd)
794 switch (kf_fd)
796 case KINFO_FILE_FD_TYPE_CWD:
797 return "cwd";
798 case KINFO_FILE_FD_TYPE_ROOT:
799 return "root";
800 case KINFO_FILE_FD_TYPE_JAIL:
801 return "jail";
802 case KINFO_FILE_FD_TYPE_TRACE:
803 return "trace";
804 case KINFO_FILE_FD_TYPE_TEXT:
805 return "text";
806 case KINFO_FILE_FD_TYPE_CTTY:
807 return "ctty";
808 default:
809 return int_string (kf_fd, 10, 1, 0, 0);
813 /* Helper function to generate the file type for a single open file in
814 'info proc files'. */
816 static const char *
817 fbsd_file_type (int kf_type, int kf_vnode_type)
819 switch (kf_type)
821 case KINFO_FILE_TYPE_VNODE:
822 switch (kf_vnode_type)
824 case KINFO_FILE_VTYPE_VREG:
825 return "file";
826 case KINFO_FILE_VTYPE_VDIR:
827 return "dir";
828 case KINFO_FILE_VTYPE_VCHR:
829 return "chr";
830 case KINFO_FILE_VTYPE_VLNK:
831 return "link";
832 case KINFO_FILE_VTYPE_VSOCK:
833 return "socket";
834 case KINFO_FILE_VTYPE_VFIFO:
835 return "fifo";
836 default:
838 char *str = get_print_cell ();
840 xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
841 return str;
844 case KINFO_FILE_TYPE_SOCKET:
845 return "socket";
846 case KINFO_FILE_TYPE_PIPE:
847 return "pipe";
848 case KINFO_FILE_TYPE_FIFO:
849 return "fifo";
850 case KINFO_FILE_TYPE_KQUEUE:
851 return "kqueue";
852 case KINFO_FILE_TYPE_CRYPTO:
853 return "crypto";
854 case KINFO_FILE_TYPE_MQUEUE:
855 return "mqueue";
856 case KINFO_FILE_TYPE_SHM:
857 return "shm";
858 case KINFO_FILE_TYPE_SEM:
859 return "sem";
860 case KINFO_FILE_TYPE_PTS:
861 return "pts";
862 case KINFO_FILE_TYPE_PROCDESC:
863 return "proc";
864 default:
865 return int_string (kf_type, 10, 1, 0, 0);
869 /* Helper function to generate the file flags for a single open file in
870 'info proc files'. */
872 static const char *
873 fbsd_file_flags (int kf_flags)
875 static char file_flags[10];
877 file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
878 file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
879 file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
880 file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
881 file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
882 file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
883 file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
884 file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
885 file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
886 file_flags[9] = '\0';
888 return file_flags;
891 /* Helper function to generate the name of an IP protocol. */
893 static const char *
894 fbsd_ipproto (int protocol)
896 switch (protocol)
898 case FBSD_IPPROTO_ICMP:
899 return "icmp";
900 case FBSD_IPPROTO_TCP:
901 return "tcp";
902 case FBSD_IPPROTO_UDP:
903 return "udp";
904 case FBSD_IPPROTO_SCTP:
905 return "sctp";
906 default:
908 char *str = get_print_cell ();
910 xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
911 return str;
916 /* Helper function to print out an IPv4 socket address. */
918 static void
919 fbsd_print_sockaddr_in (const void *sockaddr)
921 const struct fbsd_sockaddr_in *sin =
922 reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
923 char buf[INET_ADDRSTRLEN];
925 if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
926 error (_("Failed to format IPv4 address"));
927 gdb_printf ("%s:%u", buf,
928 (sin->sin_port[0] << 8) | sin->sin_port[1]);
931 /* Helper function to print out an IPv6 socket address. */
933 static void
934 fbsd_print_sockaddr_in6 (const void *sockaddr)
936 const struct fbsd_sockaddr_in6 *sin6 =
937 reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
938 char buf[INET6_ADDRSTRLEN];
940 if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
941 error (_("Failed to format IPv6 address"));
942 gdb_printf ("%s.%u", buf,
943 (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
946 /* See fbsd-tdep.h. */
948 void
949 fbsd_info_proc_files_header ()
951 gdb_printf (_("Open files:\n\n"));
952 gdb_printf (" %6s %6s %10s %9s %s\n",
953 "FD", "Type", "Offset", "Flags ", "Name");
956 /* See fbsd-tdep.h. */
958 void
959 fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
960 LONGEST kf_offset, int kf_vnode_type,
961 int kf_sock_domain, int kf_sock_type,
962 int kf_sock_protocol, const void *kf_sa_local,
963 const void *kf_sa_peer, const void *kf_path)
965 gdb_printf (" %6s %6s %10s %8s ",
966 fbsd_file_fd (kf_fd),
967 fbsd_file_type (kf_type, kf_vnode_type),
968 kf_offset > -1 ? hex_string (kf_offset) : "-",
969 fbsd_file_flags (kf_flags));
970 if (kf_type == KINFO_FILE_TYPE_SOCKET)
972 switch (kf_sock_domain)
974 case FBSD_AF_UNIX:
976 switch (kf_sock_type)
978 case FBSD_SOCK_STREAM:
979 gdb_printf ("unix stream:");
980 break;
981 case FBSD_SOCK_DGRAM:
982 gdb_printf ("unix dgram:");
983 break;
984 case FBSD_SOCK_SEQPACKET:
985 gdb_printf ("unix seqpacket:");
986 break;
987 default:
988 gdb_printf ("unix <%d>:", kf_sock_type);
989 break;
992 /* For local sockets, print out the first non-nul path
993 rather than both paths. */
994 const struct fbsd_sockaddr_un *saddr_un
995 = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
996 if (saddr_un->sun_path[0] == 0)
997 saddr_un = reinterpret_cast<const struct fbsd_sockaddr_un *>
998 (kf_sa_peer);
999 gdb_printf ("%s", saddr_un->sun_path);
1000 break;
1002 case FBSD_AF_INET:
1003 gdb_printf ("%s4 ", fbsd_ipproto (kf_sock_protocol));
1004 fbsd_print_sockaddr_in (kf_sa_local);
1005 gdb_printf (" -> ");
1006 fbsd_print_sockaddr_in (kf_sa_peer);
1007 break;
1008 case FBSD_AF_INET6:
1009 gdb_printf ("%s6 ", fbsd_ipproto (kf_sock_protocol));
1010 fbsd_print_sockaddr_in6 (kf_sa_local);
1011 gdb_printf (" -> ");
1012 fbsd_print_sockaddr_in6 (kf_sa_peer);
1013 break;
1016 else
1017 gdb_printf ("%s", reinterpret_cast<const char *> (kf_path));
1018 gdb_printf ("\n");
1021 /* Implement "info proc files" for a corefile. */
1023 static void
1024 fbsd_core_info_proc_files (struct gdbarch *gdbarch)
1026 bfd *cbfd = current_program_space->core_bfd ();
1027 asection *section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
1028 if (section == NULL)
1030 warning (_("unable to find open files in core file"));
1031 return;
1034 size_t note_size = bfd_section_size (section);
1035 if (note_size < 4)
1036 error (_("malformed core note - too short for header"));
1038 gdb::def_vector<unsigned char> contents (note_size);
1039 if (!bfd_get_section_contents (cbfd, section, contents.data (),
1040 0, note_size))
1041 error (_("could not get core note contents"));
1043 unsigned char *descdata = contents.data ();
1044 unsigned char *descend = descdata + note_size;
1046 /* Skip over the structure size. */
1047 descdata += 4;
1049 fbsd_info_proc_files_header ();
1051 while (descdata + KF_PATH < descend)
1053 ULONGEST structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
1054 if (structsize < KF_PATH)
1055 error (_("malformed core note - file structure too small"));
1057 LONGEST type = bfd_get_signed_32 (cbfd, descdata + KF_TYPE);
1058 LONGEST fd = bfd_get_signed_32 (cbfd, descdata + KF_FD);
1059 LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KF_FLAGS);
1060 LONGEST offset = bfd_get_signed_64 (cbfd, descdata + KF_OFFSET);
1061 LONGEST vnode_type = bfd_get_signed_32 (cbfd, descdata + KF_VNODE_TYPE);
1062 LONGEST sock_domain = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_DOMAIN);
1063 LONGEST sock_type = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_TYPE);
1064 LONGEST sock_protocol = bfd_get_signed_32 (cbfd,
1065 descdata + KF_SOCK_PROTOCOL);
1066 fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
1067 sock_domain, sock_type, sock_protocol,
1068 descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
1069 descdata + KF_PATH);
1071 descdata += structsize;
1075 /* Helper function to generate mappings flags for a single VM map
1076 entry in 'info proc mappings'. */
1078 static const char *
1079 fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
1081 static char vm_flags[9];
1083 vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
1084 vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
1085 vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
1086 vm_flags[3] = ' ';
1087 vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
1088 vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
1089 vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
1090 vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
1091 : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
1092 vm_flags[8] = '\0';
1094 return vm_flags;
1097 /* See fbsd-tdep.h. */
1099 void
1100 fbsd_info_proc_mappings_header (int addr_bit)
1102 gdb_printf (_("Mapped address spaces:\n\n"));
1103 if (addr_bit == 64)
1105 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
1106 "Start Addr",
1107 " End Addr",
1108 " Size", " Offset", "Flags ", "File");
1110 else
1112 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
1113 "Start Addr",
1114 " End Addr",
1115 " Size", " Offset", "Flags ", "File");
1119 /* See fbsd-tdep.h. */
1121 void
1122 fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
1123 ULONGEST kve_end, ULONGEST kve_offset,
1124 int kve_flags, int kve_protection,
1125 const void *kve_path)
1127 if (addr_bit == 64)
1129 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
1130 hex_string (kve_start),
1131 hex_string (kve_end),
1132 hex_string (kve_end - kve_start),
1133 hex_string (kve_offset),
1134 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1135 reinterpret_cast<const char *> (kve_path));
1137 else
1139 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
1140 hex_string (kve_start),
1141 hex_string (kve_end),
1142 hex_string (kve_end - kve_start),
1143 hex_string (kve_offset),
1144 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1145 reinterpret_cast<const char *> (kve_path));
1149 /* Implement "info proc mappings" for a corefile. */
1151 static void
1152 fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
1154 asection *section;
1155 unsigned char *descdata, *descend;
1156 size_t note_size;
1157 bfd *cbfd = current_program_space->core_bfd ();
1159 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.vmmap");
1160 if (section == NULL)
1162 warning (_("unable to find mappings in core file"));
1163 return;
1166 note_size = bfd_section_size (section);
1167 if (note_size < 4)
1168 error (_("malformed core note - too short for header"));
1170 gdb::def_vector<unsigned char> contents (note_size);
1171 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
1172 error (_("could not get core note contents"));
1174 descdata = contents.data ();
1175 descend = descdata + note_size;
1177 /* Skip over the structure size. */
1178 descdata += 4;
1180 fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
1181 while (descdata + KVE_PATH < descend)
1183 ULONGEST structsize = bfd_get_32 (cbfd, descdata + KVE_STRUCTSIZE);
1184 if (structsize < KVE_PATH)
1185 error (_("malformed core note - vmmap entry too small"));
1187 ULONGEST start = bfd_get_64 (cbfd, descdata + KVE_START);
1188 ULONGEST end = bfd_get_64 (cbfd, descdata + KVE_END);
1189 ULONGEST offset = bfd_get_64 (cbfd, descdata + KVE_OFFSET);
1190 LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KVE_FLAGS);
1191 LONGEST prot = bfd_get_signed_32 (cbfd, descdata + KVE_PROTECTION);
1192 fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
1193 offset, flags, prot, descdata + KVE_PATH);
1195 descdata += structsize;
1199 /* Fetch the pathname of a vnode for a single file descriptor from the
1200 file table core note. */
1202 static gdb::unique_xmalloc_ptr<char>
1203 fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
1205 asection *section;
1206 unsigned char *descdata, *descend;
1207 size_t note_size;
1208 bfd *cbfd = current_program_space->core_bfd ();
1210 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
1211 if (section == NULL)
1212 return nullptr;
1214 note_size = bfd_section_size (section);
1215 if (note_size < 4)
1216 error (_("malformed core note - too short for header"));
1218 gdb::def_vector<unsigned char> contents (note_size);
1219 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
1220 error (_("could not get core note contents"));
1222 descdata = contents.data ();
1223 descend = descdata + note_size;
1225 /* Skip over the structure size. */
1226 descdata += 4;
1228 while (descdata + KF_PATH < descend)
1230 ULONGEST structsize;
1232 structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
1233 if (structsize < KF_PATH)
1234 error (_("malformed core note - file structure too small"));
1236 if (bfd_get_32 (cbfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
1237 && bfd_get_signed_32 (cbfd, descdata + KF_FD) == fd)
1239 char *path = (char *) descdata + KF_PATH;
1240 return make_unique_xstrdup (path);
1243 descdata += structsize;
1245 return nullptr;
1248 /* Helper function to read a struct timeval. */
1250 static void
1251 fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
1252 LONGEST &sec, ULONGEST &usec)
1254 bfd *cbfd = current_program_space->core_bfd ();
1256 if (gdbarch_addr_bit (gdbarch) == 64)
1258 sec = bfd_get_signed_64 (cbfd, data);
1259 usec = bfd_get_64 (cbfd, data + 8);
1261 else if (bfd_get_arch (cbfd) == bfd_arch_i386)
1263 sec = bfd_get_signed_32 (cbfd, data);
1264 usec = bfd_get_32 (cbfd, data + 4);
1266 else
1268 sec = bfd_get_signed_64 (cbfd, data);
1269 usec = bfd_get_32 (cbfd, data + 8);
1273 /* Print out the contents of a signal set. */
1275 static void
1276 fbsd_print_sigset (const char *descr, unsigned char *sigset)
1278 bfd *cbfd = current_program_space->core_bfd ();
1279 gdb_printf ("%s: ", descr);
1280 for (int i = 0; i < SIG_WORDS; i++)
1281 gdb_printf ("%08x ",
1282 (unsigned int) bfd_get_32 (cbfd, sigset + i * 4));
1283 gdb_printf ("\n");
1286 /* Implement "info proc status" for a corefile. */
1288 static void
1289 fbsd_core_info_proc_status (struct gdbarch *gdbarch)
1291 const struct kinfo_proc_layout *kp;
1292 asection *section;
1293 unsigned char *descdata;
1294 int addr_bit, long_bit;
1295 size_t note_size;
1296 ULONGEST value;
1297 LONGEST sec;
1298 bfd *cbfd = current_program_space->core_bfd ();
1300 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.proc");
1301 if (section == NULL)
1303 warning (_("unable to find process info in core file"));
1304 return;
1307 addr_bit = gdbarch_addr_bit (gdbarch);
1308 if (addr_bit == 64)
1309 kp = &kinfo_proc_layout_64;
1310 else if (bfd_get_arch (cbfd) == bfd_arch_i386)
1311 kp = &kinfo_proc_layout_i386;
1312 else
1313 kp = &kinfo_proc_layout_32;
1314 long_bit = gdbarch_long_bit (gdbarch);
1317 * Ensure that the note is large enough for all of the fields fetched
1318 * by this function. In particular, the note must contain the 32-bit
1319 * structure size, then it must be long enough to access the last
1320 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1322 note_size = bfd_section_size (section);
1323 if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
1324 + long_bit / TARGET_CHAR_BIT))
1325 error (_("malformed core note - too short"));
1327 gdb::def_vector<unsigned char> contents (note_size);
1328 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
1329 error (_("could not get core note contents"));
1331 descdata = contents.data ();
1333 /* Skip over the structure size. */
1334 descdata += 4;
1336 /* Verify 'ki_layout' is 0. */
1337 if (bfd_get_32 (cbfd, descdata + kp->ki_layout) != 0)
1339 warning (_("unsupported process information in core file"));
1340 return;
1343 gdb_printf ("Name: %.19s\n", descdata + kp->ki_comm);
1344 gdb_printf ("Process ID: %s\n",
1345 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pid)));
1346 gdb_printf ("Parent process: %s\n",
1347 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ppid)));
1348 gdb_printf ("Process group: %s\n",
1349 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pgid)));
1350 gdb_printf ("Session id: %s\n",
1351 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_sid)));
1353 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1354 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1355 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1356 the structure that is cleared to zero. Assume that a zero value
1357 in ki_tdev indicates a core dump from an older kernel and use the
1358 value in 'ki_tdev_freebsd11' instead. */
1359 value = bfd_get_64 (cbfd, descdata + kp->ki_tdev);
1360 if (value == 0)
1361 value = bfd_get_32 (cbfd, descdata + kp->ki_tdev_freebsd11);
1362 gdb_printf ("TTY: %s\n", pulongest (value));
1363 gdb_printf ("TTY owner process group: %s\n",
1364 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_tpgid)));
1365 gdb_printf ("User IDs (real, effective, saved): %s %s %s\n",
1366 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ruid)),
1367 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_uid)),
1368 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svuid)));
1369 gdb_printf ("Group IDs (real, effective, saved): %s %s %s\n",
1370 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_rgid)),
1371 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_groups)),
1372 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svgid)));
1373 gdb_printf ("Groups: ");
1374 uint16_t ngroups = bfd_get_16 (cbfd, descdata + kp->ki_ngroups);
1375 for (int i = 0; i < ngroups; i++)
1376 gdb_printf ("%s ",
1377 pulongest (bfd_get_32 (cbfd,
1378 descdata + kp->ki_groups + i * 4)));
1379 gdb_printf ("\n");
1380 value = bfd_get (long_bit, cbfd,
1381 descdata + kp->ki_rusage + kp->ru_minflt);
1382 gdb_printf ("Minor faults (no memory page): %s\n", pulongest (value));
1383 value = bfd_get (long_bit, cbfd,
1384 descdata + kp->ki_rusage_ch + kp->ru_minflt);
1385 gdb_printf ("Minor faults, children: %s\n", pulongest (value));
1386 value = bfd_get (long_bit, cbfd,
1387 descdata + kp->ki_rusage + kp->ru_majflt);
1388 gdb_printf ("Major faults (memory page faults): %s\n",
1389 pulongest (value));
1390 value = bfd_get (long_bit, cbfd,
1391 descdata + kp->ki_rusage_ch + kp->ru_majflt);
1392 gdb_printf ("Major faults, children: %s\n", pulongest (value));
1393 fbsd_core_fetch_timeval (gdbarch,
1394 descdata + kp->ki_rusage + kp->ru_utime,
1395 sec, value);
1396 gdb_printf ("utime: %s.%06d\n", plongest (sec), (int) value);
1397 fbsd_core_fetch_timeval (gdbarch,
1398 descdata + kp->ki_rusage + kp->ru_stime,
1399 sec, value);
1400 gdb_printf ("stime: %s.%06d\n", plongest (sec), (int) value);
1401 fbsd_core_fetch_timeval (gdbarch,
1402 descdata + kp->ki_rusage_ch + kp->ru_utime,
1403 sec, value);
1404 gdb_printf ("utime, children: %s.%06d\n", plongest (sec), (int) value);
1405 fbsd_core_fetch_timeval (gdbarch,
1406 descdata + kp->ki_rusage_ch + kp->ru_stime,
1407 sec, value);
1408 gdb_printf ("stime, children: %s.%06d\n", plongest (sec), (int) value);
1409 gdb_printf ("'nice' value: %d\n",
1410 (int) bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
1411 fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
1412 gdb_printf ("Start time: %s.%06d\n", plongest (sec), (int) value);
1413 gdb_printf ("Virtual memory size: %s kB\n",
1414 pulongest (bfd_get (addr_bit, cbfd,
1415 descdata + kp->ki_size) / 1024));
1416 gdb_printf ("Data size: %s pages\n",
1417 pulongest (bfd_get (addr_bit, cbfd,
1418 descdata + kp->ki_dsize)));
1419 gdb_printf ("Stack size: %s pages\n",
1420 pulongest (bfd_get (addr_bit, cbfd,
1421 descdata + kp->ki_ssize)));
1422 gdb_printf ("Text size: %s pages\n",
1423 pulongest (bfd_get (addr_bit, cbfd,
1424 descdata + kp->ki_tsize)));
1425 gdb_printf ("Resident set size: %s pages\n",
1426 pulongest (bfd_get (addr_bit, cbfd,
1427 descdata + kp->ki_rssize)));
1428 gdb_printf ("Maximum RSS: %s pages\n",
1429 pulongest (bfd_get (long_bit, cbfd,
1430 descdata + kp->ki_rusage
1431 + kp->ru_maxrss)));
1432 fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
1433 fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
1436 /* Implement the "core_info_proc" gdbarch method. */
1438 static void
1439 fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
1440 enum info_proc_what what)
1442 bool do_cmdline = false;
1443 bool do_cwd = false;
1444 bool do_exe = false;
1445 bool do_files = false;
1446 bool do_mappings = false;
1447 bool do_status = false;
1448 int pid;
1450 switch (what)
1452 case IP_MINIMAL:
1453 do_cmdline = true;
1454 do_cwd = true;
1455 do_exe = true;
1456 break;
1457 case IP_MAPPINGS:
1458 do_mappings = true;
1459 break;
1460 case IP_STATUS:
1461 case IP_STAT:
1462 do_status = true;
1463 break;
1464 case IP_CMDLINE:
1465 do_cmdline = true;
1466 break;
1467 case IP_EXE:
1468 do_exe = true;
1469 break;
1470 case IP_CWD:
1471 do_cwd = true;
1472 break;
1473 case IP_FILES:
1474 do_files = true;
1475 break;
1476 case IP_ALL:
1477 do_cmdline = true;
1478 do_cwd = true;
1479 do_exe = true;
1480 do_files = true;
1481 do_mappings = true;
1482 do_status = true;
1483 break;
1484 default:
1485 return;
1488 bfd *cbfd = current_program_space->core_bfd ();
1489 pid = bfd_core_file_pid (cbfd);
1490 if (pid != 0)
1491 gdb_printf (_("process %d\n"), pid);
1493 if (do_cmdline)
1495 const char *cmdline;
1497 cmdline = bfd_core_file_failing_command (cbfd);
1498 if (cmdline)
1499 gdb_printf ("cmdline = '%s'\n", cmdline);
1500 else
1501 warning (_("Command line unavailable"));
1503 if (do_cwd)
1505 gdb::unique_xmalloc_ptr<char> cwd =
1506 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
1507 if (cwd)
1508 gdb_printf ("cwd = '%s'\n", cwd.get ());
1509 else
1510 warning (_("unable to read current working directory"));
1512 if (do_exe)
1514 gdb::unique_xmalloc_ptr<char> exe =
1515 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
1516 if (exe)
1517 gdb_printf ("exe = '%s'\n", exe.get ());
1518 else
1519 warning (_("unable to read executable path name"));
1521 if (do_files)
1522 fbsd_core_info_proc_files (gdbarch);
1523 if (do_mappings)
1524 fbsd_core_info_proc_mappings (gdbarch);
1525 if (do_status)
1526 fbsd_core_info_proc_status (gdbarch);
1529 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1531 static void
1532 fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
1533 CORE_ADDR type, CORE_ADDR val)
1535 const char *name = "???";
1536 const char *description = "";
1537 enum auxv_format format = AUXV_FORMAT_HEX;
1539 switch (type)
1541 case AT_NULL:
1542 case AT_IGNORE:
1543 case AT_EXECFD:
1544 case AT_PHDR:
1545 case AT_PHENT:
1546 case AT_PHNUM:
1547 case AT_PAGESZ:
1548 case AT_BASE:
1549 case AT_FLAGS:
1550 case AT_ENTRY:
1551 case AT_NOTELF:
1552 case AT_UID:
1553 case AT_EUID:
1554 case AT_GID:
1555 case AT_EGID:
1556 default_print_auxv_entry (gdbarch, file, type, val);
1557 return;
1558 #define _TAGNAME(tag) #tag
1559 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1560 #define TAG(tag, text, kind) \
1561 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1562 TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
1563 TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
1564 TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
1565 TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
1566 TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
1567 TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
1568 TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
1569 TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
1570 TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
1571 TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
1572 TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
1573 TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
1574 TAG (BSDFLAGS, _("ELF BSD flags"), AUXV_FORMAT_HEX);
1575 TAG (ARGC, _("Argument count"), AUXV_FORMAT_DEC);
1576 TAG (ARGV, _("Argument vector"), AUXV_FORMAT_HEX);
1577 TAG (ENVC, _("Environment count"), AUXV_FORMAT_DEC);
1578 TAG (ENVV, _("Environment vector"), AUXV_FORMAT_HEX);
1579 TAG (PS_STRINGS, _("Pointer to ps_strings"), AUXV_FORMAT_HEX);
1580 TAG (FXRNG, _("Pointer to root RNG seed version"), AUXV_FORMAT_HEX);
1581 TAG (KPRELOAD, _("Base address of vDSO"), AUXV_FORMAT_HEX);
1582 TAG (USRSTACKBASE, _("Top of user stack"), AUXV_FORMAT_HEX);
1583 TAG (USRSTACKLIM, _("Grow limit of user stack"), AUXV_FORMAT_HEX);
1586 fprint_auxv_entry (file, name, description, format, type, val);
1589 /* Implement the "get_siginfo_type" gdbarch method. */
1591 static struct type *
1592 fbsd_get_siginfo_type (struct gdbarch *gdbarch)
1594 struct fbsd_gdbarch_data *fbsd_gdbarch_data;
1595 struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
1596 struct type *uid_type, *pid_type;
1597 struct type *sigval_type, *reason_type;
1598 struct type *siginfo_type;
1599 struct type *type;
1601 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
1602 if (fbsd_gdbarch_data->siginfo_type != NULL)
1603 return fbsd_gdbarch_data->siginfo_type;
1605 type_allocator alloc (gdbarch);
1606 int_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
1607 0, "int");
1608 int32_type = init_integer_type (alloc, 32, 0, "int32_t");
1609 uint32_type = init_integer_type (alloc, 32, 1, "uint32_t");
1610 long_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
1611 0, "long");
1612 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
1614 /* union sigval */
1615 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1616 sigval_type->set_name (xstrdup ("sigval"));
1617 append_composite_type_field (sigval_type, "sival_int", int_type);
1618 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
1620 /* __pid_t */
1621 pid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
1622 int32_type->length () * TARGET_CHAR_BIT,
1623 "__pid_t");
1624 pid_type->set_target_type (int32_type);
1625 pid_type->set_target_is_stub (true);
1627 /* __uid_t */
1628 uid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
1629 uint32_type->length () * TARGET_CHAR_BIT,
1630 "__uid_t");
1631 uid_type->set_target_type (uint32_type);
1632 pid_type->set_target_is_stub (true);
1634 /* _reason */
1635 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1637 /* _fault */
1638 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1639 append_composite_type_field (type, "si_trapno", int_type);
1640 append_composite_type_field (reason_type, "_fault", type);
1642 /* _timer */
1643 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1644 append_composite_type_field (type, "si_timerid", int_type);
1645 append_composite_type_field (type, "si_overrun", int_type);
1646 append_composite_type_field (reason_type, "_timer", type);
1648 /* _mesgq */
1649 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1650 append_composite_type_field (type, "si_mqd", int_type);
1651 append_composite_type_field (reason_type, "_mesgq", type);
1653 /* _poll */
1654 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1655 append_composite_type_field (type, "si_band", long_type);
1656 append_composite_type_field (reason_type, "_poll", type);
1658 /* __spare__ */
1659 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1660 append_composite_type_field (type, "__spare1__", long_type);
1661 append_composite_type_field (type, "__spare2__",
1662 init_vector_type (int_type, 7));
1663 append_composite_type_field (reason_type, "__spare__", type);
1665 /* struct siginfo */
1666 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1667 siginfo_type->set_name (xstrdup ("siginfo"));
1668 append_composite_type_field (siginfo_type, "si_signo", int_type);
1669 append_composite_type_field (siginfo_type, "si_errno", int_type);
1670 append_composite_type_field (siginfo_type, "si_code", int_type);
1671 append_composite_type_field (siginfo_type, "si_pid", pid_type);
1672 append_composite_type_field (siginfo_type, "si_uid", uid_type);
1673 append_composite_type_field (siginfo_type, "si_status", int_type);
1674 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
1675 append_composite_type_field (siginfo_type, "si_value", sigval_type);
1676 append_composite_type_field (siginfo_type, "_reason", reason_type);
1678 fbsd_gdbarch_data->siginfo_type = siginfo_type;
1680 return siginfo_type;
1683 /* Implement the "gdb_signal_from_target" gdbarch method. */
1685 static enum gdb_signal
1686 fbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
1688 switch (signal)
1690 case 0:
1691 return GDB_SIGNAL_0;
1693 case FREEBSD_SIGHUP:
1694 return GDB_SIGNAL_HUP;
1696 case FREEBSD_SIGINT:
1697 return GDB_SIGNAL_INT;
1699 case FREEBSD_SIGQUIT:
1700 return GDB_SIGNAL_QUIT;
1702 case FREEBSD_SIGILL:
1703 return GDB_SIGNAL_ILL;
1705 case FREEBSD_SIGTRAP:
1706 return GDB_SIGNAL_TRAP;
1708 case FREEBSD_SIGABRT:
1709 return GDB_SIGNAL_ABRT;
1711 case FREEBSD_SIGEMT:
1712 return GDB_SIGNAL_EMT;
1714 case FREEBSD_SIGFPE:
1715 return GDB_SIGNAL_FPE;
1717 case FREEBSD_SIGKILL:
1718 return GDB_SIGNAL_KILL;
1720 case FREEBSD_SIGBUS:
1721 return GDB_SIGNAL_BUS;
1723 case FREEBSD_SIGSEGV:
1724 return GDB_SIGNAL_SEGV;
1726 case FREEBSD_SIGSYS:
1727 return GDB_SIGNAL_SYS;
1729 case FREEBSD_SIGPIPE:
1730 return GDB_SIGNAL_PIPE;
1732 case FREEBSD_SIGALRM:
1733 return GDB_SIGNAL_ALRM;
1735 case FREEBSD_SIGTERM:
1736 return GDB_SIGNAL_TERM;
1738 case FREEBSD_SIGURG:
1739 return GDB_SIGNAL_URG;
1741 case FREEBSD_SIGSTOP:
1742 return GDB_SIGNAL_STOP;
1744 case FREEBSD_SIGTSTP:
1745 return GDB_SIGNAL_TSTP;
1747 case FREEBSD_SIGCONT:
1748 return GDB_SIGNAL_CONT;
1750 case FREEBSD_SIGCHLD:
1751 return GDB_SIGNAL_CHLD;
1753 case FREEBSD_SIGTTIN:
1754 return GDB_SIGNAL_TTIN;
1756 case FREEBSD_SIGTTOU:
1757 return GDB_SIGNAL_TTOU;
1759 case FREEBSD_SIGIO:
1760 return GDB_SIGNAL_IO;
1762 case FREEBSD_SIGXCPU:
1763 return GDB_SIGNAL_XCPU;
1765 case FREEBSD_SIGXFSZ:
1766 return GDB_SIGNAL_XFSZ;
1768 case FREEBSD_SIGVTALRM:
1769 return GDB_SIGNAL_VTALRM;
1771 case FREEBSD_SIGPROF:
1772 return GDB_SIGNAL_PROF;
1774 case FREEBSD_SIGWINCH:
1775 return GDB_SIGNAL_WINCH;
1777 case FREEBSD_SIGINFO:
1778 return GDB_SIGNAL_INFO;
1780 case FREEBSD_SIGUSR1:
1781 return GDB_SIGNAL_USR1;
1783 case FREEBSD_SIGUSR2:
1784 return GDB_SIGNAL_USR2;
1786 /* SIGTHR is the same as SIGLWP on FreeBSD. */
1787 case FREEBSD_SIGTHR:
1788 return GDB_SIGNAL_LWP;
1790 case FREEBSD_SIGLIBRT:
1791 return GDB_SIGNAL_LIBRT;
1794 if (signal >= FREEBSD_SIGRTMIN && signal <= FREEBSD_SIGRTMAX)
1796 int offset = signal - FREEBSD_SIGRTMIN;
1798 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_65 + offset);
1801 return GDB_SIGNAL_UNKNOWN;
1804 /* Implement the "gdb_signal_to_target" gdbarch method. */
1806 static int
1807 fbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
1808 enum gdb_signal signal)
1810 switch (signal)
1812 case GDB_SIGNAL_0:
1813 return 0;
1815 case GDB_SIGNAL_HUP:
1816 return FREEBSD_SIGHUP;
1818 case GDB_SIGNAL_INT:
1819 return FREEBSD_SIGINT;
1821 case GDB_SIGNAL_QUIT:
1822 return FREEBSD_SIGQUIT;
1824 case GDB_SIGNAL_ILL:
1825 return FREEBSD_SIGILL;
1827 case GDB_SIGNAL_TRAP:
1828 return FREEBSD_SIGTRAP;
1830 case GDB_SIGNAL_ABRT:
1831 return FREEBSD_SIGABRT;
1833 case GDB_SIGNAL_EMT:
1834 return FREEBSD_SIGEMT;
1836 case GDB_SIGNAL_FPE:
1837 return FREEBSD_SIGFPE;
1839 case GDB_SIGNAL_KILL:
1840 return FREEBSD_SIGKILL;
1842 case GDB_SIGNAL_BUS:
1843 return FREEBSD_SIGBUS;
1845 case GDB_SIGNAL_SEGV:
1846 return FREEBSD_SIGSEGV;
1848 case GDB_SIGNAL_SYS:
1849 return FREEBSD_SIGSYS;
1851 case GDB_SIGNAL_PIPE:
1852 return FREEBSD_SIGPIPE;
1854 case GDB_SIGNAL_ALRM:
1855 return FREEBSD_SIGALRM;
1857 case GDB_SIGNAL_TERM:
1858 return FREEBSD_SIGTERM;
1860 case GDB_SIGNAL_URG:
1861 return FREEBSD_SIGURG;
1863 case GDB_SIGNAL_STOP:
1864 return FREEBSD_SIGSTOP;
1866 case GDB_SIGNAL_TSTP:
1867 return FREEBSD_SIGTSTP;
1869 case GDB_SIGNAL_CONT:
1870 return FREEBSD_SIGCONT;
1872 case GDB_SIGNAL_CHLD:
1873 return FREEBSD_SIGCHLD;
1875 case GDB_SIGNAL_TTIN:
1876 return FREEBSD_SIGTTIN;
1878 case GDB_SIGNAL_TTOU:
1879 return FREEBSD_SIGTTOU;
1881 case GDB_SIGNAL_IO:
1882 return FREEBSD_SIGIO;
1884 case GDB_SIGNAL_XCPU:
1885 return FREEBSD_SIGXCPU;
1887 case GDB_SIGNAL_XFSZ:
1888 return FREEBSD_SIGXFSZ;
1890 case GDB_SIGNAL_VTALRM:
1891 return FREEBSD_SIGVTALRM;
1893 case GDB_SIGNAL_PROF:
1894 return FREEBSD_SIGPROF;
1896 case GDB_SIGNAL_WINCH:
1897 return FREEBSD_SIGWINCH;
1899 case GDB_SIGNAL_INFO:
1900 return FREEBSD_SIGINFO;
1902 case GDB_SIGNAL_USR1:
1903 return FREEBSD_SIGUSR1;
1905 case GDB_SIGNAL_USR2:
1906 return FREEBSD_SIGUSR2;
1908 case GDB_SIGNAL_LWP:
1909 return FREEBSD_SIGTHR;
1911 case GDB_SIGNAL_LIBRT:
1912 return FREEBSD_SIGLIBRT;
1915 if (signal >= GDB_SIGNAL_REALTIME_65
1916 && signal <= GDB_SIGNAL_REALTIME_126)
1918 int offset = signal - GDB_SIGNAL_REALTIME_65;
1920 return FREEBSD_SIGRTMIN + offset;
1923 return -1;
1926 /* Implement the "get_syscall_number" gdbarch method. */
1928 static LONGEST
1929 fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
1932 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1933 native targets fetch the system call number from the
1934 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1935 However, system call catching requires this function to be
1936 set. */
1938 internal_error (_("fbsd_get_sycall_number called"));
1941 /* Read an integer symbol value from the current target. */
1943 static LONGEST
1944 fbsd_read_integer_by_name (struct gdbarch *gdbarch, const char *name)
1946 bound_minimal_symbol ms
1947 = lookup_minimal_symbol (current_program_space, name);
1948 if (ms.minsym == NULL)
1949 error (_("Unable to resolve symbol '%s'"), name);
1951 gdb_byte buf[4];
1952 if (target_read_memory (ms.value_address (), buf, sizeof buf) != 0)
1953 error (_("Unable to read value of '%s'"), name);
1955 return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
1958 /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
1959 structure needed to determine the TLS index of an object file. */
1961 static void
1962 fbsd_fetch_rtld_offsets (struct gdbarch *gdbarch, struct fbsd_pspace_data *data)
1966 /* Fetch offsets from debug symbols in rtld. */
1967 struct symbol *obj_entry_sym
1968 = lookup_symbol_in_language ("Struct_Obj_Entry", nullptr,
1969 SEARCH_STRUCT_DOMAIN,
1970 language_c, nullptr).symbol;
1971 if (obj_entry_sym == NULL)
1972 error (_("Unable to find Struct_Obj_Entry symbol"));
1973 data->off_linkmap = lookup_struct_elt (obj_entry_sym->type (),
1974 "linkmap", 0).offset / 8;
1975 data->off_tlsindex = lookup_struct_elt (obj_entry_sym->type (),
1976 "tlsindex", 0).offset / 8;
1977 data->rtld_offsets_valid = true;
1978 return;
1980 catch (const gdb_exception_error &e)
1982 data->off_linkmap = -1;
1987 /* Fetch offsets from global variables in libthr. Note that
1988 this does not work for single-threaded processes that are not
1989 linked against libthr. */
1990 data->off_linkmap = fbsd_read_integer_by_name (gdbarch,
1991 "_thread_off_linkmap");
1992 data->off_tlsindex = fbsd_read_integer_by_name (gdbarch,
1993 "_thread_off_tlsindex");
1994 data->rtld_offsets_valid = true;
1995 return;
1997 catch (const gdb_exception_error &e)
1999 data->off_linkmap = -1;
2003 /* Helper function to read the TLS index of an object file associated
2004 with a link map entry at LM_ADDR. */
2006 static LONGEST
2007 fbsd_get_tls_index (struct gdbarch *gdbarch, CORE_ADDR lm_addr)
2009 struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
2011 if (!data->rtld_offsets_valid)
2012 fbsd_fetch_rtld_offsets (gdbarch, data);
2014 if (data->off_linkmap == -1)
2015 throw_error (TLS_GENERIC_ERROR,
2016 _("Cannot fetch runtime linker structure offsets"));
2018 /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
2019 pointer and then compute the offset of the tlsindex member. */
2020 CORE_ADDR tlsindex_addr = lm_addr - data->off_linkmap + data->off_tlsindex;
2022 gdb_byte buf[4];
2023 if (target_read_memory (tlsindex_addr, buf, sizeof buf) != 0)
2024 throw_error (TLS_GENERIC_ERROR,
2025 _("Cannot find thread-local variables on this target"));
2027 return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
2030 /* See fbsd-tdep.h. */
2032 CORE_ADDR
2033 fbsd_get_thread_local_address (struct gdbarch *gdbarch, CORE_ADDR dtv_addr,
2034 CORE_ADDR lm_addr, CORE_ADDR offset)
2036 LONGEST tls_index = fbsd_get_tls_index (gdbarch, lm_addr);
2038 gdb::byte_vector buf (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
2039 if (target_read_memory (dtv_addr, buf.data (), buf.size ()) != 0)
2040 throw_error (TLS_GENERIC_ERROR,
2041 _("Cannot find thread-local variables on this target"));
2043 const struct builtin_type *builtin = builtin_type (gdbarch);
2044 CORE_ADDR addr
2045 = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr,
2046 buf.data ());
2048 addr += (tls_index + 1) * builtin->builtin_data_ptr->length ();
2049 if (target_read_memory (addr, buf.data (), buf.size ()) != 0)
2050 throw_error (TLS_GENERIC_ERROR,
2051 _("Cannot find thread-local variables on this target"));
2053 addr = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr,
2054 buf.data ());
2055 return addr + offset;
2058 /* See fbsd-tdep.h. */
2060 CORE_ADDR
2061 fbsd_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
2063 bound_minimal_symbol msym
2064 = lookup_minimal_symbol (current_program_space, "_rtld_bind");
2065 if (msym.minsym != nullptr && msym.value_address () == pc)
2066 return frame_unwind_caller_pc (get_current_frame ());
2068 return 0;
2071 /* Return description of signal code or nullptr. */
2073 static const char *
2074 fbsd_signal_cause (enum gdb_signal siggnal, int code)
2076 /* Signal-independent causes. */
2077 switch (code)
2079 case FBSD_SI_USER:
2080 return _("Sent by kill()");
2081 case FBSD_SI_QUEUE:
2082 return _("Sent by sigqueue()");
2083 case FBSD_SI_TIMER:
2084 return _("Timer expired");
2085 case FBSD_SI_ASYNCIO:
2086 return _("Asynchronous I/O request completed");
2087 case FBSD_SI_MESGQ:
2088 return _("Message arrived on empty message queue");
2089 case FBSD_SI_KERNEL:
2090 return _("Sent by kernel");
2091 case FBSD_SI_LWP:
2092 return _("Sent by thr_kill()");
2095 switch (siggnal)
2097 case GDB_SIGNAL_ILL:
2098 switch (code)
2100 case FBSD_ILL_ILLOPC:
2101 return _("Illegal opcode");
2102 case FBSD_ILL_ILLOPN:
2103 return _("Illegal operand");
2104 case FBSD_ILL_ILLADR:
2105 return _("Illegal addressing mode");
2106 case FBSD_ILL_ILLTRP:
2107 return _("Illegal trap");
2108 case FBSD_ILL_PRVOPC:
2109 return _("Privileged opcode");
2110 case FBSD_ILL_PRVREG:
2111 return _("Privileged register");
2112 case FBSD_ILL_COPROC:
2113 return _("Coprocessor error");
2114 case FBSD_ILL_BADSTK:
2115 return _("Internal stack error");
2117 break;
2118 case GDB_SIGNAL_BUS:
2119 switch (code)
2121 case FBSD_BUS_ADRALN:
2122 return _("Invalid address alignment");
2123 case FBSD_BUS_ADRERR:
2124 return _("Address not present");
2125 case FBSD_BUS_OBJERR:
2126 return _("Object-specific hardware error");
2127 case FBSD_BUS_OOMERR:
2128 return _("Out of memory");
2130 break;
2131 case GDB_SIGNAL_SEGV:
2132 switch (code)
2134 case FBSD_SEGV_MAPERR:
2135 return _("Address not mapped to object");
2136 case FBSD_SEGV_ACCERR:
2137 return _("Invalid permissions for mapped object");
2138 case FBSD_SEGV_PKUERR:
2139 return _("PKU violation");
2141 break;
2142 case GDB_SIGNAL_FPE:
2143 switch (code)
2145 case FBSD_FPE_INTOVF:
2146 return _("Integer overflow");
2147 case FBSD_FPE_INTDIV:
2148 return _("Integer divide by zero");
2149 case FBSD_FPE_FLTDIV:
2150 return _("Floating point divide by zero");
2151 case FBSD_FPE_FLTOVF:
2152 return _("Floating point overflow");
2153 case FBSD_FPE_FLTUND:
2154 return _("Floating point underflow");
2155 case FBSD_FPE_FLTRES:
2156 return _("Floating point inexact result");
2157 case FBSD_FPE_FLTINV:
2158 return _("Invalid floating point operation");
2159 case FBSD_FPE_FLTSUB:
2160 return _("Subscript out of range");
2162 break;
2163 case GDB_SIGNAL_TRAP:
2164 switch (code)
2166 case FBSD_TRAP_BRKPT:
2167 return _("Breakpoint");
2168 case FBSD_TRAP_TRACE:
2169 return _("Trace trap");
2170 case FBSD_TRAP_DTRACE:
2171 return _("DTrace-induced trap");
2172 case FBSD_TRAP_CAP:
2173 return _("Capability violation");
2175 break;
2176 case GDB_SIGNAL_CHLD:
2177 switch (code)
2179 case FBSD_CLD_EXITED:
2180 return _("Child has exited");
2181 case FBSD_CLD_KILLED:
2182 return _("Child has terminated abnormally");
2183 case FBSD_CLD_DUMPED:
2184 return _("Child has dumped core");
2185 case FBSD_CLD_TRAPPED:
2186 return _("Traced child has trapped");
2187 case FBSD_CLD_STOPPED:
2188 return _("Child has stopped");
2189 case FBSD_CLD_CONTINUED:
2190 return _("Stopped child has continued");
2192 break;
2193 case GDB_SIGNAL_POLL:
2194 switch (code)
2196 case FBSD_POLL_IN:
2197 return _("Data input available");
2198 case FBSD_POLL_OUT:
2199 return _("Output buffers available");
2200 case FBSD_POLL_MSG:
2201 return _("Input message available");
2202 case FBSD_POLL_ERR:
2203 return _("I/O error");
2204 case FBSD_POLL_PRI:
2205 return _("High priority input available");
2206 case FBSD_POLL_HUP:
2207 return _("Device disconnected");
2209 break;
2212 return nullptr;
2215 /* Report additional details for a signal stop. */
2217 static void
2218 fbsd_report_signal_info (struct gdbarch *gdbarch, struct ui_out *uiout,
2219 enum gdb_signal siggnal)
2221 LONGEST code, mqd, pid, status, timerid, uid;
2225 code = parse_and_eval_long ("$_siginfo.si_code");
2226 pid = parse_and_eval_long ("$_siginfo.si_pid");
2227 uid = parse_and_eval_long ("$_siginfo.si_uid");
2228 status = parse_and_eval_long ("$_siginfo.si_status");
2229 timerid = parse_and_eval_long ("$_siginfo._reason._timer.si_timerid");
2230 mqd = parse_and_eval_long ("$_siginfo._reason._mesgq.si_mqd");
2232 catch (const gdb_exception_error &e)
2234 return;
2237 const char *meaning = fbsd_signal_cause (siggnal, code);
2238 if (meaning == nullptr)
2239 return;
2241 uiout->text (".\n");
2242 uiout->field_string ("sigcode-meaning", meaning);
2244 switch (code)
2246 case FBSD_SI_USER:
2247 case FBSD_SI_QUEUE:
2248 case FBSD_SI_LWP:
2249 uiout->text (" from pid ");
2250 uiout->field_string ("sending-pid", plongest (pid));
2251 uiout->text (" and user ");
2252 uiout->field_string ("sending-uid", plongest (uid));
2253 return;
2254 case FBSD_SI_TIMER:
2255 uiout->text (": timerid ");
2256 uiout->field_string ("timerid", plongest (timerid));
2257 return;
2258 case FBSD_SI_MESGQ:
2259 uiout->text (": message queue ");
2260 uiout->field_string ("message-queue", plongest (mqd));
2261 return;
2262 case FBSD_SI_ASYNCIO:
2263 return;
2266 if (siggnal == GDB_SIGNAL_CHLD)
2268 uiout->text (": pid ");
2269 uiout->field_string ("child-pid", plongest (pid));
2270 uiout->text (", uid ");
2271 uiout->field_string ("child-uid", plongest (uid));
2272 if (code == FBSD_CLD_EXITED)
2274 uiout->text (", exit status ");
2275 uiout->field_string ("exit-status", plongest (status));
2277 else
2279 uiout->text (", signal ");
2280 uiout->field_string ("signal", plongest (status));
2285 /* Search a list of struct kinfo_vmmap entries in the ENTRIES buffer
2286 of LEN bytes to find the length of the entry starting at ADDR.
2287 Returns the length of the entry or zero if no entry was found. */
2289 static ULONGEST
2290 fbsd_vmmap_length (struct gdbarch *gdbarch, unsigned char *entries, size_t len,
2291 CORE_ADDR addr)
2293 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2294 unsigned char *descdata = entries;
2295 unsigned char *descend = descdata + len;
2297 /* Skip over the structure size. */
2298 descdata += 4;
2300 while (descdata + KVE_PATH < descend)
2302 ULONGEST structsize = extract_unsigned_integer (descdata
2303 + KVE_STRUCTSIZE, 4,
2304 byte_order);
2305 if (structsize < KVE_PATH)
2306 return false;
2308 ULONGEST start = extract_unsigned_integer (descdata + KVE_START, 8,
2309 byte_order);
2310 ULONGEST end = extract_unsigned_integer (descdata + KVE_END, 8,
2311 byte_order);
2312 if (start == addr)
2313 return end - start;
2315 descdata += structsize;
2317 return 0;
2320 /* Helper for fbsd_vsyscall_range that does the real work of finding
2321 the vDSO's address range. */
2323 static bool
2324 fbsd_vdso_range (struct gdbarch *gdbarch, struct mem_range *range)
2326 if (target_auxv_search (AT_FREEBSD_KPRELOAD, &range->start) <= 0)
2327 return false;
2329 if (!target_has_execution ())
2331 /* Search for the ending address in the NT_PROCSTAT_VMMAP note. */
2332 bfd *cbfd = current_program_space->core_bfd ();
2333 asection *section = bfd_get_section_by_name (cbfd,
2334 ".note.freebsdcore.vmmap");
2335 if (section == nullptr)
2336 return false;
2338 size_t note_size = bfd_section_size (section);
2339 if (note_size < 4)
2340 return false;
2342 gdb::def_vector<unsigned char> contents (note_size);
2343 if (!bfd_get_section_contents (cbfd, section, contents.data (),
2344 0, note_size))
2345 return false;
2347 range->length = fbsd_vmmap_length (gdbarch, contents.data (), note_size,
2348 range->start);
2350 else
2352 /* Fetch the list of address space entries from the running target. */
2353 std::optional<gdb::byte_vector> buf =
2354 target_read_alloc (current_inferior ()->top_target (),
2355 TARGET_OBJECT_FREEBSD_VMMAP, nullptr);
2356 if (!buf || buf->empty ())
2357 return false;
2359 range->length = fbsd_vmmap_length (gdbarch, buf->data (), buf->size (),
2360 range->start);
2362 return range->length != 0;
2365 /* Return the address range of the vDSO for the current inferior. */
2367 static int
2368 fbsd_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
2370 struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
2372 if (data->vdso_range_p == 0)
2374 if (fbsd_vdso_range (gdbarch, &data->vdso_range))
2375 data->vdso_range_p = 1;
2376 else
2377 data->vdso_range_p = -1;
2380 if (data->vdso_range_p < 0)
2381 return 0;
2383 *range = data->vdso_range;
2384 return 1;
2387 /* To be called from GDB_OSABI_FREEBSD handlers. */
2389 void
2390 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2392 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
2393 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
2394 set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
2395 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
2396 set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
2397 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
2398 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
2399 set_gdbarch_gdb_signal_from_target (gdbarch, fbsd_gdb_signal_from_target);
2400 set_gdbarch_gdb_signal_to_target (gdbarch, fbsd_gdb_signal_to_target);
2401 set_gdbarch_report_signal_info (gdbarch, fbsd_report_signal_info);
2402 set_gdbarch_skip_solib_resolver (gdbarch, fbsd_skip_solib_resolver);
2403 set_gdbarch_vsyscall_range (gdbarch, fbsd_vsyscall_range);
2405 /* `catch syscall' */
2406 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
2407 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);