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