]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/fbsd-tdep.c
x86: ld: sframe: Remove SFrame NULL FRE template
[thirdparty/binutils-gdb.git] / gdb / fbsd-tdep.c
CommitLineData
a904c024
AA
1/* Target-dependent code for FreeBSD, architecture-independent.
2
d01e8234 3 Copyright (C) 2002-2025 Free Software Foundation, Inc.
a904c024
AA
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
82372b2f 20#include "auxv.h"
05d9d66d 21#include "exceptions.h"
ec452525 22#include "extract-store-integer.h"
a904c024
AA
23#include "gdbcore.h"
24#include "inferior.h"
ed810cc7 25#include "objfiles.h"
a904c024
AA
26#include "regcache.h"
27#include "regset.h"
4de283e4 28#include "gdbthread.h"
e6cdd38e 29#include "xml-syscall.h"
4de283e4
TT
30#include <sys/socket.h>
31#include <arpa/inet.h>
32
33#include "elf-bfd.h"
34#include "fbsd-tdep.h"
f3a5df7b 35#include "gcore-elf.h"
671c82e9 36#include "arch-utils.h"
a904c024 37
e61667ef
RS
38/* This enum is derived from FreeBSD's <sys/signal.h>. */
39
40enum
41 {
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,
77 };
a904c024 78
83810881
JB
79/* Constants for values of si_code as defined in FreeBSD's
80 <sys/signal.h>. */
81
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
89
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
98
99#define FBSD_BUS_ADRALN 1
100#define FBSD_BUS_ADRERR 2
101#define FBSD_BUS_OBJERR 3
102#define FBSD_BUS_OOMERR 100
103
104#define FBSD_SEGV_MAPERR 1
105#define FBSD_SEGV_ACCERR 2
106#define FBSD_SEGV_PKUERR 100
107
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
116
117#define FBSD_TRAP_BRKPT 1
118#define FBSD_TRAP_TRACE 2
119#define FBSD_TRAP_DTRACE 3
120#define FBSD_TRAP_CAP 4
121
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
128
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
135
4b654465
JB
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. */
143
144#define LWPINFO_OFFSET 0x4
145
146/* Offsets in ptrace_lwpinfo. */
147#define LWPINFO_PL_FLAGS 0x8
148#define LWPINFO64_PL_SIGINFO 0x30
149#define LWPINFO32_PL_SIGINFO 0x2c
150
151/* Flags in pl_flags. */
152#define PL_FLAG_SI 0x20 /* siginfo is valid */
153
154/* Sizes of siginfo_t. */
155#define SIZE64_SIGINFO_T 80
156#define SIZE32_SIGINFO_T 64
157
d2176225
JB
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
57c2a98a
JB
161 all architectures.
162
163 Note that FreeBSD 7.0 used an older version of this structure
85102364 164 (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
57c2a98a
JB
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. */
d2176225
JB
168
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
906b4aac 174#define KVE_PROTECTION 0x38
d2176225
JB
175#define KVE_PATH 0x88
176
177/* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
178 match the KVME_PROT_* constants in <sys/user.h>. */
179
180#define KINFO_VME_PROT_READ 0x00000001
181#define KINFO_VME_PROT_WRITE 0x00000002
182#define KINFO_VME_PROT_EXEC 0x00000004
183
184/* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
185 match the KVME_FLAG_* constants in <sys/user.h>. */
186
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
193
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
57c2a98a
JB
197 architectures.
198
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. */
d2176225
JB
203
204#define KF_STRUCTSIZE 0x0
205#define KF_TYPE 0x4
206#define KF_FD 0x8
57c2a98a
JB
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
d2176225
JB
215#define KF_PATH 0x170
216
217/* Constants for the 'kf_type' field in struct kinfo_file. These
218 match the KF_TYPE_* constants in <sys/user.h>. */
219
220#define KINFO_FILE_TYPE_VNODE 1
57c2a98a
JB
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
d2176225
JB
231
232/* Special values for the 'kf_fd' field in struct kinfo_file. These
233 match the KF_FD_TYPE_* constants in <sys/user.h>. */
234
235#define KINFO_FILE_FD_TYPE_CWD -1
57c2a98a
JB
236#define KINFO_FILE_FD_TYPE_ROOT -2
237#define KINFO_FILE_FD_TYPE_JAIL -3
238#define KINFO_FILE_FD_TYPE_TRACE -4
d2176225 239#define KINFO_FILE_FD_TYPE_TEXT -5
57c2a98a
JB
240#define KINFO_FILE_FD_TYPE_CTTY -6
241
242/* Flags in the 'kf_flags' field in struct kinfo_file. These match
243 the KF_FLAG_* constants in <sys/user.h>. */
244
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
254
255/* Constants for the 'kf_vnode_type' field in struct kinfo_file.
256 These match the KF_VTYPE_* constants in <sys/user.h>. */
257
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
264
265/* Constants for socket address families. These match AF_* constants
266 in <sys/socket.h>. */
267
268#define FBSD_AF_UNIX 1
269#define FBSD_AF_INET 2
270#define FBSD_AF_INET6 28
271
272/* Constants for socket types. These match SOCK_* constants in
273 <sys/socket.h>. */
274
275#define FBSD_SOCK_STREAM 1
276#define FBSD_SOCK_DGRAM 2
277#define FBSD_SOCK_SEQPACKET 5
278
279/* Constants for IP protocols. These match IPPROTO_* constants in
280 <netinet/in.h>. */
281
282#define FBSD_IPPROTO_ICMP 1
283#define FBSD_IPPROTO_TCP 6
284#define FBSD_IPPROTO_UDP 17
285#define FBSD_IPPROTO_SCTP 132
286
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. */
290
291struct fbsd_sockaddr_in
292{
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];
298};
299
300struct fbsd_sockaddr_in6
301{
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;
308};
309
310struct fbsd_sockaddr_un
311{
312 uint8_t sun_len;
313 uint8_t sun_family;
314 char sun_path[104];
315};
d2176225
JB
316
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. */
319
320#define SIG_WORDS 4
321
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.
328
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. */
337
338struct kinfo_proc_layout
339{
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;
368
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;
375};
376
377const struct kinfo_proc_layout kinfo_proc_layout_32 =
378 {
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,
406
407 .ru_utime = 0x0,
408 .ru_stime = 0x10,
409 .ru_maxrss = 0x20,
410 .ru_minflt = 0x30,
411 .ru_majflt = 0x34,
412 };
413
414const struct kinfo_proc_layout kinfo_proc_layout_i386 =
415 {
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,
443
444 .ru_utime = 0x0,
445 .ru_stime = 0x8,
446 .ru_maxrss = 0x10,
447 .ru_minflt = 0x20,
448 .ru_majflt = 0x24,
449 };
450
451const struct kinfo_proc_layout kinfo_proc_layout_64 =
452 {
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,
480
481 .ru_utime = 0x0,
482 .ru_stime = 0x10,
483 .ru_maxrss = 0x20,
484 .ru_minflt = 0x40,
485 .ru_majflt = 0x48,
486 };
487
762c974a
JB
488struct fbsd_gdbarch_data
489 {
cb275538 490 struct type *siginfo_type = nullptr;
762c974a
JB
491 };
492
cb275538
TT
493static const registry<gdbarch>::key<fbsd_gdbarch_data>
494 fbsd_gdbarch_data_handle;
762c974a
JB
495
496static struct fbsd_gdbarch_data *
497get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
498{
cb275538
TT
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;
762c974a
JB
503}
504
945f3901
JB
505struct fbsd_pspace_data
506{
507 /* Offsets in the runtime linker's 'Obj_Entry' structure. */
d4e05d2f
TT
508 LONGEST off_linkmap = 0;
509 LONGEST off_tlsindex = 0;
510 bool rtld_offsets_valid = false;
c1dae0a6
JB
511
512 /* vDSO mapping range. */
513 struct mem_range vdso_range {};
514
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;
945f3901
JB
518};
519
d4e05d2f 520/* Per-program-space data for FreeBSD architectures. */
08b8a139 521static const registry<program_space>::key<fbsd_pspace_data>
d4e05d2f
TT
522 fbsd_pspace_data_handle;
523
945f3901
JB
524static struct fbsd_pspace_data *
525get_fbsd_pspace_data (struct program_space *pspace)
526{
527 struct fbsd_pspace_data *data;
528
d4e05d2f 529 data = fbsd_pspace_data_handle.get (pspace);
945f3901 530 if (data == NULL)
d4e05d2f 531 data = fbsd_pspace_data_handle.emplace (pspace);
945f3901
JB
532
533 return data;
534}
535
79117428
JB
536/* This is how we want PTIDs from core files to be printed. */
537
a068643d 538static std::string
79117428
JB
539fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
540{
e38504b3 541 if (ptid.lwp () != 0)
a068643d 542 return string_printf ("LWP %ld", ptid.lwp ());
79117428
JB
543
544 return normal_pid_to_str (ptid);
545}
546
547/* Extract the name assigned to a thread from a core. Returns the
548 string in a static buffer. */
549
550static const char *
551fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
552{
553 static char buf[80];
554 struct bfd_section *section;
555 bfd_size_type size;
79117428 556
e38504b3 557 if (thr->ptid.lwp () != 0)
79117428
JB
558 {
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. */
2af9fc44 566 thread_section_name section_name (".thrmisc", thr->ptid);
6fdf95ae 567 bfd *cbfd = current_program_space->core_bfd ();
2af9fc44 568
6fdf95ae 569 section = bfd_get_section_by_name (cbfd, section_name.c_str ());
fd361982 570 if (section != NULL && bfd_section_size (section) > 0)
79117428
JB
571 {
572 /* Truncate the name if it is longer than "buf". */
fd361982 573 size = bfd_section_size (section);
79117428
JB
574 if (size > sizeof buf - 1)
575 size = sizeof buf - 1;
6fdf95ae 576 if (bfd_get_section_contents (cbfd, section, buf, (file_ptr) 0, size)
79117428
JB
577 && buf[0] != '\0')
578 {
579 buf[size] = '\0';
580
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. */
6fdf95ae 585 if (strcmp (buf, elf_tdata (cbfd)->core->program) != 0)
79117428
JB
586 return buf;
587 }
588 }
589 }
590
591 return NULL;
592}
593
4b654465
JB
594/* Implement the "core_xfer_siginfo" gdbarch method. */
595
596static LONGEST
597fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
598 ULONGEST offset, ULONGEST len)
599{
600 size_t siginfo_size;
601
a181c0bf 602 if (gdbarch_long_bit (gdbarch) == 32)
4b654465
JB
603 siginfo_size = SIZE32_SIGINFO_T;
604 else
605 siginfo_size = SIZE64_SIGINFO_T;
606 if (offset > siginfo_size)
607 return -1;
608
609 thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
6fdf95ae
SM
610 bfd *cbfd = current_program_space->core_bfd ();
611 asection *section = bfd_get_section_by_name (cbfd, section_name.c_str ());
4b654465
JB
612 if (section == NULL)
613 return -1;
614
615 gdb_byte buf[4];
6fdf95ae 616 if (!bfd_get_section_contents (cbfd, section, buf,
4b654465
JB
617 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
618 return -1;
619
2a50938a 620 int pl_flags = extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
4b654465
JB
621 if (!(pl_flags & PL_FLAG_SI))
622 return -1;
623
624 if (offset + len > siginfo_size)
625 len = siginfo_size - offset;
626
627 ULONGEST siginfo_offset;
a181c0bf 628 if (gdbarch_long_bit (gdbarch) == 32)
4b654465
JB
629 siginfo_offset = LWPINFO_OFFSET + LWPINFO32_PL_SIGINFO;
630 else
631 siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
632
6fdf95ae 633 if (!bfd_get_section_contents (cbfd, section, readbuf,
4b654465
JB
634 siginfo_offset + offset, len))
635 return -1;
636
637 return len;
638}
639
58984e4a
TT
640static bool
641find_signalled_thread (struct thread_info *info)
a904c024 642{
58984e4a
TT
643 return (info->stop_signal () != GDB_SIGNAL_0
644 && info->ptid.pid () == inferior_ptid.pid ());
a904c024
AA
645}
646
739ab2e9
SS
647/* Return a byte_vector containing the contents of a core dump note
648 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
649 the data is prefixed with a 32-bit integer size to match the format
650 used in FreeBSD NT_PROCSTAT_* notes. */
651
6b09f134 652static std::optional<gdb::byte_vector>
739ab2e9
SS
653fbsd_make_note_desc (enum target_object object, uint32_t structsize)
654{
6b09f134 655 std::optional<gdb::byte_vector> buf =
328d42d8 656 target_read_alloc (current_inferior ()->top_target (), object, NULL);
739ab2e9
SS
657 if (!buf || buf->empty ())
658 return {};
659
660 if (structsize == 0)
661 return buf;
662
663 gdb::byte_vector desc (sizeof (structsize) + buf->size ());
664 memcpy (desc.data (), &structsize, sizeof (structsize));
214d8837 665 std::copy (buf->begin (), buf->end (), desc.data () + sizeof (structsize));
739ab2e9
SS
666 return desc;
667}
668
a904c024
AA
669/* Create appropriate note sections for a corefile, returning them in
670 allocated memory. */
671
c21f37a8 672static gdb::unique_xmalloc_ptr<char>
a904c024
AA
673fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
674{
c21f37a8 675 gdb::unique_xmalloc_ptr<char> note_data;
a904c024 676 Elf_Internal_Ehdr *i_ehdrp;
08036331 677 struct thread_info *curr_thr, *signalled_thr;
a904c024
AA
678
679 /* Put a "FreeBSD" label in the ELF header. */
680 i_ehdrp = elf_elfheader (obfd);
681 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
682
683 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
684
0a70e1a8 685 if (current_program_space->exec_filename () != nullptr)
a904c024 686 {
0a70e1a8 687 const char *fname = lbasename (current_program_space->exec_filename ());
0afbabf0 688 std::string psargs = fname;
a904c024 689
fd2dec2a
SM
690 const std::string &infargs = current_inferior ()->args ();
691 if (!infargs.empty ())
692 psargs += ' ' + infargs;
a904c024 693
c21f37a8
SM
694 note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
695 note_size, fname,
696 psargs.c_str ()));
a904c024
AA
697 }
698
20a0aab3 699 /* Thread register information. */
a70b8144 700 try
20a0aab3
JB
701 {
702 update_thread_list ();
703 }
230d2906 704 catch (const gdb_exception_error &e)
20a0aab3
JB
705 {
706 exception_print (gdb_stderr, e);
707 }
20a0aab3
JB
708
709 /* Like the kernel, prefer dumping the signalled thread first.
710 "First thread" is what tools use to infer the signalled thread.
711 In case there's more than one signalled thread, prefer the
712 current thread, if it is signalled. */
713 curr_thr = inferior_thread ();
1edb66d8 714 if (curr_thr->stop_signal () != GDB_SIGNAL_0)
20a0aab3
JB
715 signalled_thr = curr_thr;
716 else
717 {
58984e4a 718 signalled_thr = iterate_over_threads (find_signalled_thread);
20a0aab3
JB
719 if (signalled_thr == NULL)
720 signalled_thr = curr_thr;
721 }
722
1edb66d8 723 enum gdb_signal stop_signal = signalled_thr->stop_signal ();
f3a5df7b
AB
724 gcore_elf_build_thread_register_notes (gdbarch, signalled_thr, stop_signal,
725 obfd, &note_data, note_size);
08036331 726 for (thread_info *thr : current_inferior ()->non_exited_threads ())
20a0aab3
JB
727 {
728 if (thr == signalled_thr)
729 continue;
20a0aab3 730
f3a5df7b
AB
731 gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal,
732 obfd, &note_data, note_size);
20a0aab3
JB
733 }
734
739ab2e9
SS
735 /* Auxiliary vector. */
736 uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo */
6b09f134 737 std::optional<gdb::byte_vector> note_desc =
739ab2e9
SS
738 fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
739 if (note_desc && !note_desc->empty ())
740 {
c21f37a8
SM
741 note_data.reset (elfcore_write_note (obfd, note_data.release (),
742 note_size, "FreeBSD",
743 NT_FREEBSD_PROCSTAT_AUXV,
744 note_desc->data (),
745 note_desc->size ()));
739ab2e9
SS
746 if (!note_data)
747 return NULL;
748 }
749
750 /* Virtual memory mappings. */
751 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
752 if (note_desc && !note_desc->empty ())
753 {
c21f37a8
SM
754 note_data.reset (elfcore_write_note (obfd, note_data.release (),
755 note_size, "FreeBSD",
756 NT_FREEBSD_PROCSTAT_VMMAP,
757 note_desc->data (),
758 note_desc->size ()));
739ab2e9
SS
759 if (!note_data)
760 return NULL;
761 }
762
763 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
764 if (note_desc && !note_desc->empty ())
765 {
c21f37a8
SM
766 note_data.reset (elfcore_write_note (obfd, note_data.release (),
767 note_size, "FreeBSD",
768 NT_FREEBSD_PROCSTAT_PSSTRINGS,
769 note_desc->data (),
770 note_desc->size ()));
739ab2e9
SS
771 if (!note_data)
772 return NULL;
773 }
774
c14993e9
AB
775 /* Include the target description when possible. Some architectures
776 allow for per-thread gdbarch so we should really be emitting a tdesc
777 per-thread, however, we don't currently support reading in a
778 per-thread tdesc, so just emit the tdesc for the signalled thread. */
779 gdbarch = target_thread_architecture (signalled_thr->ptid);
780 gcore_elf_make_tdesc_note (gdbarch, obfd, &note_data, note_size);
95ce627a 781
a904c024
AA
782 return note_data;
783}
784
57c2a98a
JB
785/* Helper function to generate the file descriptor description for a
786 single open file in 'info proc files'. */
787
788static const char *
789fbsd_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
813static const char *
814fbsd_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
869static const char *
870fbsd_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
890static const char *
891fbsd_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
915static void
916fbsd_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
07147793 922 if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
57c2a98a 923 error (_("Failed to format IPv4 address"));
6cb06a8c
TT
924 gdb_printf ("%s:%u", buf,
925 (sin->sin_port[0] << 8) | sin->sin_port[1]);
57c2a98a
JB
926}
927
928/* Helper function to print out an IPv6 socket address. */
929
930static void
931fbsd_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
07147793 937 if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
57c2a98a 938 error (_("Failed to format IPv6 address"));
6cb06a8c
TT
939 gdb_printf ("%s.%u", buf,
940 (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
57c2a98a
JB
941}
942
943/* See fbsd-tdep.h. */
944
945void
946fbsd_info_proc_files_header ()
947{
6cb06a8c
TT
948 gdb_printf (_("Open files:\n\n"));
949 gdb_printf (" %6s %6s %10s %9s %s\n",
950 "FD", "Type", "Offset", "Flags ", "Name");
57c2a98a
JB
951}
952
953/* See fbsd-tdep.h. */
954
955void
956fbsd_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{
6cb06a8c
TT
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));
57c2a98a
JB
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:
6cb06a8c 976 gdb_printf ("unix stream:");
57c2a98a
JB
977 break;
978 case FBSD_SOCK_DGRAM:
6cb06a8c 979 gdb_printf ("unix dgram:");
57c2a98a
JB
980 break;
981 case FBSD_SOCK_SEQPACKET:
6cb06a8c 982 gdb_printf ("unix seqpacket:");
57c2a98a
JB
983 break;
984 default:
6cb06a8c 985 gdb_printf ("unix <%d>:", kf_sock_type);
57c2a98a
JB
986 break;
987 }
988
989 /* For local sockets, print out the first non-nul path
990 rather than both paths. */
aa989b27 991 const struct fbsd_sockaddr_un *saddr_un
57c2a98a 992 = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
aa989b27
CB
993 if (saddr_un->sun_path[0] == 0)
994 saddr_un = reinterpret_cast<const struct fbsd_sockaddr_un *>
57c2a98a 995 (kf_sa_peer);
6cb06a8c 996 gdb_printf ("%s", saddr_un->sun_path);
57c2a98a
JB
997 break;
998 }
999 case FBSD_AF_INET:
6cb06a8c 1000 gdb_printf ("%s4 ", fbsd_ipproto (kf_sock_protocol));
57c2a98a 1001 fbsd_print_sockaddr_in (kf_sa_local);
6cb06a8c 1002 gdb_printf (" -> ");
57c2a98a
JB
1003 fbsd_print_sockaddr_in (kf_sa_peer);
1004 break;
1005 case FBSD_AF_INET6:
6cb06a8c 1006 gdb_printf ("%s6 ", fbsd_ipproto (kf_sock_protocol));
57c2a98a 1007 fbsd_print_sockaddr_in6 (kf_sa_local);
6cb06a8c 1008 gdb_printf (" -> ");
57c2a98a
JB
1009 fbsd_print_sockaddr_in6 (kf_sa_peer);
1010 break;
1011 }
1012 }
1013 else
6cb06a8c
TT
1014 gdb_printf ("%s", reinterpret_cast<const char *> (kf_path));
1015 gdb_printf ("\n");
57c2a98a
JB
1016}
1017
1018/* Implement "info proc files" for a corefile. */
1019
1020static void
1021fbsd_core_info_proc_files (struct gdbarch *gdbarch)
1022{
6fdf95ae
SM
1023 bfd *cbfd = current_program_space->core_bfd ();
1024 asection *section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
57c2a98a
JB
1025 if (section == NULL)
1026 {
1027 warning (_("unable to find open files in core file"));
1028 return;
1029 }
1030
fd361982 1031 size_t note_size = bfd_section_size (section);
57c2a98a
JB
1032 if (note_size < 4)
1033 error (_("malformed core note - too short for header"));
1034
1035 gdb::def_vector<unsigned char> contents (note_size);
6fdf95ae 1036 if (!bfd_get_section_contents (cbfd, section, contents.data (),
57c2a98a
JB
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 {
6fdf95ae 1050 ULONGEST structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
57c2a98a
JB
1051 if (structsize < KF_PATH)
1052 error (_("malformed core note - file structure too small"));
1053
6fdf95ae
SM
1054 LONGEST type = bfd_get_signed_32 (cbfd, descdata + KF_TYPE);
1055 LONGEST fd = bfd_get_signed_32 (cbfd, descdata + KF_FD);
1056 LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KF_FLAGS);
1057 LONGEST offset = bfd_get_signed_64 (cbfd, descdata + KF_OFFSET);
1058 LONGEST vnode_type = bfd_get_signed_32 (cbfd, descdata + KF_VNODE_TYPE);
1059 LONGEST sock_domain = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_DOMAIN);
1060 LONGEST sock_type = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_TYPE);
1061 LONGEST sock_protocol = bfd_get_signed_32 (cbfd,
57c2a98a
JB
1062 descdata + KF_SOCK_PROTOCOL);
1063 fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
1064 sock_domain, sock_type, sock_protocol,
1065 descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
1066 descdata + KF_PATH);
1067
1068 descdata += structsize;
1069 }
1070}
1071
d2176225
JB
1072/* Helper function to generate mappings flags for a single VM map
1073 entry in 'info proc mappings'. */
1074
6f3b1098 1075static const char *
d2176225
JB
1076fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
1077{
1078 static char vm_flags[9];
1079
1080 vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
1081 vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
1082 vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
1083 vm_flags[3] = ' ';
1084 vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
1085 vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
1086 vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
1087 vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
1088 : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
1089 vm_flags[8] = '\0';
1090
1091 return vm_flags;
1092}
1093
6f3b1098
JB
1094/* See fbsd-tdep.h. */
1095
1096void
1097fbsd_info_proc_mappings_header (int addr_bit)
1098{
6cb06a8c 1099 gdb_printf (_("Mapped address spaces:\n\n"));
6f3b1098
JB
1100 if (addr_bit == 64)
1101 {
6cb06a8c
TT
1102 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
1103 "Start Addr",
1104 " End Addr",
1105 " Size", " Offset", "Flags ", "File");
6f3b1098
JB
1106 }
1107 else
1108 {
6cb06a8c
TT
1109 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
1110 "Start Addr",
1111 " End Addr",
1112 " Size", " Offset", "Flags ", "File");
6f3b1098
JB
1113 }
1114}
1115
1116/* See fbsd-tdep.h. */
1117
1118void
1119fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
1120 ULONGEST kve_end, ULONGEST kve_offset,
1121 int kve_flags, int kve_protection,
1122 const void *kve_path)
1123{
1124 if (addr_bit == 64)
1125 {
6cb06a8c
TT
1126 gdb_printf (" %18s %18s %10s %10s %9s %s\n",
1127 hex_string (kve_start),
1128 hex_string (kve_end),
1129 hex_string (kve_end - kve_start),
1130 hex_string (kve_offset),
1131 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1132 reinterpret_cast<const char *> (kve_path));
6f3b1098
JB
1133 }
1134 else
1135 {
6cb06a8c
TT
1136 gdb_printf ("\t%10s %10s %10s %10s %9s %s\n",
1137 hex_string (kve_start),
1138 hex_string (kve_end),
1139 hex_string (kve_end - kve_start),
1140 hex_string (kve_offset),
1141 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1142 reinterpret_cast<const char *> (kve_path));
6f3b1098
JB
1143 }
1144}
1145
d2176225
JB
1146/* Implement "info proc mappings" for a corefile. */
1147
1148static void
1149fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
1150{
1151 asection *section;
1152 unsigned char *descdata, *descend;
1153 size_t note_size;
6fdf95ae 1154 bfd *cbfd = current_program_space->core_bfd ();
d2176225 1155
6fdf95ae 1156 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.vmmap");
d2176225
JB
1157 if (section == NULL)
1158 {
1159 warning (_("unable to find mappings in core file"));
1160 return;
1161 }
1162
fd361982 1163 note_size = bfd_section_size (section);
d2176225
JB
1164 if (note_size < 4)
1165 error (_("malformed core note - too short for header"));
1166
1167 gdb::def_vector<unsigned char> contents (note_size);
6fdf95ae 1168 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
d2176225
JB
1169 error (_("could not get core note contents"));
1170
1171 descdata = contents.data ();
1172 descend = descdata + note_size;
1173
1174 /* Skip over the structure size. */
1175 descdata += 4;
1176
6f3b1098 1177 fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
d2176225
JB
1178 while (descdata + KVE_PATH < descend)
1179 {
6fdf95ae 1180 ULONGEST structsize = bfd_get_32 (cbfd, descdata + KVE_STRUCTSIZE);
d2176225
JB
1181 if (structsize < KVE_PATH)
1182 error (_("malformed core note - vmmap entry too small"));
1183
6fdf95ae
SM
1184 ULONGEST start = bfd_get_64 (cbfd, descdata + KVE_START);
1185 ULONGEST end = bfd_get_64 (cbfd, descdata + KVE_END);
1186 ULONGEST offset = bfd_get_64 (cbfd, descdata + KVE_OFFSET);
1187 LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KVE_FLAGS);
1188 LONGEST prot = bfd_get_signed_32 (cbfd, descdata + KVE_PROTECTION);
6f3b1098
JB
1189 fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
1190 offset, flags, prot, descdata + KVE_PATH);
d2176225
JB
1191
1192 descdata += structsize;
1193 }
1194}
1195
1196/* Fetch the pathname of a vnode for a single file descriptor from the
1197 file table core note. */
1198
1199static gdb::unique_xmalloc_ptr<char>
1200fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
1201{
1202 asection *section;
1203 unsigned char *descdata, *descend;
1204 size_t note_size;
6fdf95ae 1205 bfd *cbfd = current_program_space->core_bfd ();
d2176225 1206
6fdf95ae 1207 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
d2176225
JB
1208 if (section == NULL)
1209 return nullptr;
1210
fd361982 1211 note_size = bfd_section_size (section);
d2176225
JB
1212 if (note_size < 4)
1213 error (_("malformed core note - too short for header"));
1214
1215 gdb::def_vector<unsigned char> contents (note_size);
6fdf95ae 1216 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
d2176225
JB
1217 error (_("could not get core note contents"));
1218
1219 descdata = contents.data ();
1220 descend = descdata + note_size;
1221
1222 /* Skip over the structure size. */
1223 descdata += 4;
1224
9f235e09 1225 while (descdata + KF_PATH < descend)
d2176225
JB
1226 {
1227 ULONGEST structsize;
1228
6fdf95ae 1229 structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
9f235e09
JB
1230 if (structsize < KF_PATH)
1231 error (_("malformed core note - file structure too small"));
d2176225 1232
6fdf95ae
SM
1233 if (bfd_get_32 (cbfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
1234 && bfd_get_signed_32 (cbfd, descdata + KF_FD) == fd)
d2176225
JB
1235 {
1236 char *path = (char *) descdata + KF_PATH;
b02f78f9 1237 return make_unique_xstrdup (path);
d2176225
JB
1238 }
1239
1240 descdata += structsize;
1241 }
1242 return nullptr;
1243}
1244
1245/* Helper function to read a struct timeval. */
1246
1247static void
1248fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
1249 LONGEST &sec, ULONGEST &usec)
1250{
6fdf95ae
SM
1251 bfd *cbfd = current_program_space->core_bfd ();
1252
d2176225
JB
1253 if (gdbarch_addr_bit (gdbarch) == 64)
1254 {
6fdf95ae
SM
1255 sec = bfd_get_signed_64 (cbfd, data);
1256 usec = bfd_get_64 (cbfd, data + 8);
d2176225 1257 }
6fdf95ae 1258 else if (bfd_get_arch (cbfd) == bfd_arch_i386)
d2176225 1259 {
6fdf95ae
SM
1260 sec = bfd_get_signed_32 (cbfd, data);
1261 usec = bfd_get_32 (cbfd, data + 4);
d2176225
JB
1262 }
1263 else
1264 {
6fdf95ae
SM
1265 sec = bfd_get_signed_64 (cbfd, data);
1266 usec = bfd_get_32 (cbfd, data + 8);
d2176225
JB
1267 }
1268}
1269
1270/* Print out the contents of a signal set. */
1271
1272static void
1273fbsd_print_sigset (const char *descr, unsigned char *sigset)
1274{
6fdf95ae 1275 bfd *cbfd = current_program_space->core_bfd ();
6cb06a8c 1276 gdb_printf ("%s: ", descr);
d2176225 1277 for (int i = 0; i < SIG_WORDS; i++)
6cb06a8c 1278 gdb_printf ("%08x ",
6fdf95ae 1279 (unsigned int) bfd_get_32 (cbfd, sigset + i * 4));
6cb06a8c 1280 gdb_printf ("\n");
d2176225
JB
1281}
1282
1283/* Implement "info proc status" for a corefile. */
1284
1285static void
1286fbsd_core_info_proc_status (struct gdbarch *gdbarch)
1287{
1288 const struct kinfo_proc_layout *kp;
1289 asection *section;
d2176225
JB
1290 unsigned char *descdata;
1291 int addr_bit, long_bit;
1292 size_t note_size;
1293 ULONGEST value;
1294 LONGEST sec;
6fdf95ae 1295 bfd *cbfd = current_program_space->core_bfd ();
d2176225 1296
6fdf95ae 1297 section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.proc");
d2176225
JB
1298 if (section == NULL)
1299 {
1300 warning (_("unable to find process info in core file"));
1301 return;
1302 }
1303
1304 addr_bit = gdbarch_addr_bit (gdbarch);
1305 if (addr_bit == 64)
1306 kp = &kinfo_proc_layout_64;
6fdf95ae 1307 else if (bfd_get_arch (cbfd) == bfd_arch_i386)
d2176225
JB
1308 kp = &kinfo_proc_layout_i386;
1309 else
1310 kp = &kinfo_proc_layout_32;
1311 long_bit = gdbarch_long_bit (gdbarch);
1312
1313 /*
1314 * Ensure that the note is large enough for all of the fields fetched
1315 * by this function. In particular, the note must contain the 32-bit
1316 * structure size, then it must be long enough to access the last
1317 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1318 */
fd361982 1319 note_size = bfd_section_size (section);
d2176225
JB
1320 if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
1321 + long_bit / TARGET_CHAR_BIT))
1322 error (_("malformed core note - too short"));
1323
1324 gdb::def_vector<unsigned char> contents (note_size);
6fdf95ae 1325 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
d2176225
JB
1326 error (_("could not get core note contents"));
1327
1328 descdata = contents.data ();
1329
1330 /* Skip over the structure size. */
1331 descdata += 4;
1332
1333 /* Verify 'ki_layout' is 0. */
6fdf95ae 1334 if (bfd_get_32 (cbfd, descdata + kp->ki_layout) != 0)
d2176225
JB
1335 {
1336 warning (_("unsupported process information in core file"));
1337 return;
1338 }
1339
6cb06a8c
TT
1340 gdb_printf ("Name: %.19s\n", descdata + kp->ki_comm);
1341 gdb_printf ("Process ID: %s\n",
6fdf95ae 1342 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pid)));
6cb06a8c 1343 gdb_printf ("Parent process: %s\n",
6fdf95ae 1344 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ppid)));
6cb06a8c 1345 gdb_printf ("Process group: %s\n",
6fdf95ae 1346 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pgid)));
6cb06a8c 1347 gdb_printf ("Session id: %s\n",
6fdf95ae 1348 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_sid)));
d2176225
JB
1349
1350 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1351 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1352 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1353 the structure that is cleared to zero. Assume that a zero value
1354 in ki_tdev indicates a core dump from an older kernel and use the
1355 value in 'ki_tdev_freebsd11' instead. */
6fdf95ae 1356 value = bfd_get_64 (cbfd, descdata + kp->ki_tdev);
d2176225 1357 if (value == 0)
6fdf95ae 1358 value = bfd_get_32 (cbfd, descdata + kp->ki_tdev_freebsd11);
6cb06a8c
TT
1359 gdb_printf ("TTY: %s\n", pulongest (value));
1360 gdb_printf ("TTY owner process group: %s\n",
6fdf95ae 1361 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_tpgid)));
6cb06a8c 1362 gdb_printf ("User IDs (real, effective, saved): %s %s %s\n",
6fdf95ae
SM
1363 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ruid)),
1364 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_uid)),
1365 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svuid)));
6cb06a8c 1366 gdb_printf ("Group IDs (real, effective, saved): %s %s %s\n",
6fdf95ae
SM
1367 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_rgid)),
1368 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_groups)),
1369 pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svgid)));
6cb06a8c 1370 gdb_printf ("Groups: ");
6fdf95ae 1371 uint16_t ngroups = bfd_get_16 (cbfd, descdata + kp->ki_ngroups);
d2176225 1372 for (int i = 0; i < ngroups; i++)
6cb06a8c 1373 gdb_printf ("%s ",
6fdf95ae 1374 pulongest (bfd_get_32 (cbfd,
6cb06a8c
TT
1375 descdata + kp->ki_groups + i * 4)));
1376 gdb_printf ("\n");
6fdf95ae 1377 value = bfd_get (long_bit, cbfd,
d2176225 1378 descdata + kp->ki_rusage + kp->ru_minflt);
6cb06a8c 1379 gdb_printf ("Minor faults (no memory page): %s\n", pulongest (value));
6fdf95ae 1380 value = bfd_get (long_bit, cbfd,
d2176225 1381 descdata + kp->ki_rusage_ch + kp->ru_minflt);
6cb06a8c 1382 gdb_printf ("Minor faults, children: %s\n", pulongest (value));
6fdf95ae 1383 value = bfd_get (long_bit, cbfd,
d2176225 1384 descdata + kp->ki_rusage + kp->ru_majflt);
6cb06a8c
TT
1385 gdb_printf ("Major faults (memory page faults): %s\n",
1386 pulongest (value));
6fdf95ae 1387 value = bfd_get (long_bit, cbfd,
d2176225 1388 descdata + kp->ki_rusage_ch + kp->ru_majflt);
6cb06a8c 1389 gdb_printf ("Major faults, children: %s\n", pulongest (value));
d2176225
JB
1390 fbsd_core_fetch_timeval (gdbarch,
1391 descdata + kp->ki_rusage + kp->ru_utime,
1392 sec, value);
6cb06a8c 1393 gdb_printf ("utime: %s.%06d\n", plongest (sec), (int) value);
d2176225
JB
1394 fbsd_core_fetch_timeval (gdbarch,
1395 descdata + kp->ki_rusage + kp->ru_stime,
1396 sec, value);
6cb06a8c 1397 gdb_printf ("stime: %s.%06d\n", plongest (sec), (int) value);
d2176225
JB
1398 fbsd_core_fetch_timeval (gdbarch,
1399 descdata + kp->ki_rusage_ch + kp->ru_utime,
1400 sec, value);
6cb06a8c 1401 gdb_printf ("utime, children: %s.%06d\n", plongest (sec), (int) value);
d2176225
JB
1402 fbsd_core_fetch_timeval (gdbarch,
1403 descdata + kp->ki_rusage_ch + kp->ru_stime,
1404 sec, value);
6cb06a8c
TT
1405 gdb_printf ("stime, children: %s.%06d\n", plongest (sec), (int) value);
1406 gdb_printf ("'nice' value: %d\n",
1407 (int) bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
d2176225 1408 fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
6cb06a8c
TT
1409 gdb_printf ("Start time: %s.%06d\n", plongest (sec), (int) value);
1410 gdb_printf ("Virtual memory size: %s kB\n",
6fdf95ae 1411 pulongest (bfd_get (addr_bit, cbfd,
6cb06a8c
TT
1412 descdata + kp->ki_size) / 1024));
1413 gdb_printf ("Data size: %s pages\n",
6fdf95ae 1414 pulongest (bfd_get (addr_bit, cbfd,
6cb06a8c
TT
1415 descdata + kp->ki_dsize)));
1416 gdb_printf ("Stack size: %s pages\n",
6fdf95ae 1417 pulongest (bfd_get (addr_bit, cbfd,
6cb06a8c
TT
1418 descdata + kp->ki_ssize)));
1419 gdb_printf ("Text size: %s pages\n",
6fdf95ae 1420 pulongest (bfd_get (addr_bit, cbfd,
6cb06a8c
TT
1421 descdata + kp->ki_tsize)));
1422 gdb_printf ("Resident set size: %s pages\n",
6fdf95ae 1423 pulongest (bfd_get (addr_bit, cbfd,
6cb06a8c
TT
1424 descdata + kp->ki_rssize)));
1425 gdb_printf ("Maximum RSS: %s pages\n",
6fdf95ae 1426 pulongest (bfd_get (long_bit, cbfd,
6cb06a8c
TT
1427 descdata + kp->ki_rusage
1428 + kp->ru_maxrss)));
d2176225
JB
1429 fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
1430 fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
1431}
1432
1433/* Implement the "core_info_proc" gdbarch method. */
1434
1435static void
1436fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
1437 enum info_proc_what what)
1438{
1439 bool do_cmdline = false;
1440 bool do_cwd = false;
1441 bool do_exe = false;
57c2a98a 1442 bool do_files = false;
d2176225
JB
1443 bool do_mappings = false;
1444 bool do_status = false;
1445 int pid;
1446
1447 switch (what)
1448 {
1449 case IP_MINIMAL:
1450 do_cmdline = true;
1451 do_cwd = true;
1452 do_exe = true;
1453 break;
1454 case IP_MAPPINGS:
1455 do_mappings = true;
1456 break;
1457 case IP_STATUS:
1458 case IP_STAT:
1459 do_status = true;
1460 break;
1461 case IP_CMDLINE:
1462 do_cmdline = true;
1463 break;
1464 case IP_EXE:
1465 do_exe = true;
1466 break;
1467 case IP_CWD:
1468 do_cwd = true;
1469 break;
57c2a98a
JB
1470 case IP_FILES:
1471 do_files = true;
1472 break;
d2176225
JB
1473 case IP_ALL:
1474 do_cmdline = true;
1475 do_cwd = true;
1476 do_exe = true;
57c2a98a 1477 do_files = true;
d2176225
JB
1478 do_mappings = true;
1479 do_status = true;
1480 break;
1481 default:
1482 return;
1483 }
1484
6fdf95ae
SM
1485 bfd *cbfd = current_program_space->core_bfd ();
1486 pid = bfd_core_file_pid (cbfd);
d2176225 1487 if (pid != 0)
6cb06a8c 1488 gdb_printf (_("process %d\n"), pid);
d2176225
JB
1489
1490 if (do_cmdline)
1491 {
1492 const char *cmdline;
1493
6fdf95ae 1494 cmdline = bfd_core_file_failing_command (cbfd);
d2176225 1495 if (cmdline)
6cb06a8c 1496 gdb_printf ("cmdline = '%s'\n", cmdline);
d2176225
JB
1497 else
1498 warning (_("Command line unavailable"));
1499 }
1500 if (do_cwd)
1501 {
1502 gdb::unique_xmalloc_ptr<char> cwd =
1503 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
1504 if (cwd)
6cb06a8c 1505 gdb_printf ("cwd = '%s'\n", cwd.get ());
d2176225
JB
1506 else
1507 warning (_("unable to read current working directory"));
1508 }
1509 if (do_exe)
1510 {
1511 gdb::unique_xmalloc_ptr<char> exe =
1512 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
1513 if (exe)
6cb06a8c 1514 gdb_printf ("exe = '%s'\n", exe.get ());
d2176225
JB
1515 else
1516 warning (_("unable to read executable path name"));
1517 }
57c2a98a
JB
1518 if (do_files)
1519 fbsd_core_info_proc_files (gdbarch);
d2176225
JB
1520 if (do_mappings)
1521 fbsd_core_info_proc_mappings (gdbarch);
1522 if (do_status)
1523 fbsd_core_info_proc_status (gdbarch);
1524}
1525
82372b2f
JB
1526/* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1527
1528static void
1529fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
1530 CORE_ADDR type, CORE_ADDR val)
1531{
fd2b4de5
JB
1532 const char *name = "???";
1533 const char *description = "";
1534 enum auxv_format format = AUXV_FORMAT_HEX;
82372b2f
JB
1535
1536 switch (type)
1537 {
fd2b4de5
JB
1538 case AT_NULL:
1539 case AT_IGNORE:
1540 case AT_EXECFD:
1541 case AT_PHDR:
1542 case AT_PHENT:
1543 case AT_PHNUM:
1544 case AT_PAGESZ:
1545 case AT_BASE:
1546 case AT_FLAGS:
1547 case AT_ENTRY:
1548 case AT_NOTELF:
1549 case AT_UID:
1550 case AT_EUID:
1551 case AT_GID:
1552 case AT_EGID:
1553 default_print_auxv_entry (gdbarch, file, type, val);
1554 return;
82372b2f
JB
1555#define _TAGNAME(tag) #tag
1556#define TAGNAME(tag) _TAGNAME(AT_##tag)
1557#define TAG(tag, text, kind) \
1558 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1559 TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
1560 TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
1561 TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
1562 TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
1563 TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
1564 TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
1565 TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
1566 TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
1567 TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
12c4bd7f
JB
1568 TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
1569 TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
d7ded98f 1570 TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
a879b4d5 1571 TAG (BSDFLAGS, _("ELF BSD flags"), AUXV_FORMAT_HEX);
fc238d4a
JB
1572 TAG (ARGC, _("Argument count"), AUXV_FORMAT_DEC);
1573 TAG (ARGV, _("Argument vector"), AUXV_FORMAT_HEX);
1574 TAG (ENVC, _("Environment count"), AUXV_FORMAT_DEC);
1575 TAG (ENVV, _("Environment vector"), AUXV_FORMAT_HEX);
1576 TAG (PS_STRINGS, _("Pointer to ps_strings"), AUXV_FORMAT_HEX);
b4992e99
JB
1577 TAG (FXRNG, _("Pointer to root RNG seed version"), AUXV_FORMAT_HEX);
1578 TAG (KPRELOAD, _("Base address of vDSO"), AUXV_FORMAT_HEX);
4f60f821
JB
1579 TAG (USRSTACKBASE, _("Top of user stack"), AUXV_FORMAT_HEX);
1580 TAG (USRSTACKLIM, _("Grow limit of user stack"), AUXV_FORMAT_HEX);
82372b2f
JB
1581 }
1582
1583 fprint_auxv_entry (file, name, description, format, type, val);
1584}
1585
762c974a
JB
1586/* Implement the "get_siginfo_type" gdbarch method. */
1587
1588static struct type *
1589fbsd_get_siginfo_type (struct gdbarch *gdbarch)
1590{
1591 struct fbsd_gdbarch_data *fbsd_gdbarch_data;
1592 struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
1593 struct type *uid_type, *pid_type;
1594 struct type *sigval_type, *reason_type;
1595 struct type *siginfo_type;
1596 struct type *type;
1597
1598 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
1599 if (fbsd_gdbarch_data->siginfo_type != NULL)
1600 return fbsd_gdbarch_data->siginfo_type;
1601
2d39ccd3
TT
1602 type_allocator alloc (gdbarch);
1603 int_type = init_integer_type (alloc, gdbarch_int_bit (gdbarch),
762c974a 1604 0, "int");
2d39ccd3
TT
1605 int32_type = init_integer_type (alloc, 32, 0, "int32_t");
1606 uint32_type = init_integer_type (alloc, 32, 1, "uint32_t");
1607 long_type = init_integer_type (alloc, gdbarch_long_bit (gdbarch),
762c974a
JB
1608 0, "long");
1609 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
1610
1611 /* union sigval */
1612 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
d0e39ea2 1613 sigval_type->set_name (xstrdup ("sigval"));
762c974a
JB
1614 append_composite_type_field (sigval_type, "sival_int", int_type);
1615 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
1616
1617 /* __pid_t */
cc495054
TT
1618 pid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
1619 int32_type->length () * TARGET_CHAR_BIT,
1620 "__pid_t");
8a50fdce 1621 pid_type->set_target_type (int32_type);
8f53807e 1622 pid_type->set_target_is_stub (true);
762c974a
JB
1623
1624 /* __uid_t */
cc495054
TT
1625 uid_type = alloc.new_type (TYPE_CODE_TYPEDEF,
1626 uint32_type->length () * TARGET_CHAR_BIT,
1627 "__uid_t");
8a50fdce 1628 uid_type->set_target_type (uint32_type);
8f53807e 1629 pid_type->set_target_is_stub (true);
762c974a
JB
1630
1631 /* _reason */
1632 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1633
1634 /* _fault */
1635 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1636 append_composite_type_field (type, "si_trapno", int_type);
1637 append_composite_type_field (reason_type, "_fault", type);
1638
1639 /* _timer */
1640 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1641 append_composite_type_field (type, "si_timerid", int_type);
1642 append_composite_type_field (type, "si_overrun", int_type);
1643 append_composite_type_field (reason_type, "_timer", type);
1644
1645 /* _mesgq */
1646 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1647 append_composite_type_field (type, "si_mqd", int_type);
1648 append_composite_type_field (reason_type, "_mesgq", type);
1649
1650 /* _poll */
1651 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1652 append_composite_type_field (type, "si_band", long_type);
1653 append_composite_type_field (reason_type, "_poll", type);
1654
1655 /* __spare__ */
1656 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1657 append_composite_type_field (type, "__spare1__", long_type);
1658 append_composite_type_field (type, "__spare2__",
1659 init_vector_type (int_type, 7));
1660 append_composite_type_field (reason_type, "__spare__", type);
1661
1662 /* struct siginfo */
1663 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
d0e39ea2 1664 siginfo_type->set_name (xstrdup ("siginfo"));
762c974a
JB
1665 append_composite_type_field (siginfo_type, "si_signo", int_type);
1666 append_composite_type_field (siginfo_type, "si_errno", int_type);
1667 append_composite_type_field (siginfo_type, "si_code", int_type);
1668 append_composite_type_field (siginfo_type, "si_pid", pid_type);
1669 append_composite_type_field (siginfo_type, "si_uid", uid_type);
1670 append_composite_type_field (siginfo_type, "si_status", int_type);
1671 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
1672 append_composite_type_field (siginfo_type, "si_value", sigval_type);
1673 append_composite_type_field (siginfo_type, "_reason", reason_type);
1674
1675 fbsd_gdbarch_data->siginfo_type = siginfo_type;
1676
1677 return siginfo_type;
1678}
1679
e61667ef
RS
1680/* Implement the "gdb_signal_from_target" gdbarch method. */
1681
1682static enum gdb_signal
1683fbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
1684{
1685 switch (signal)
1686 {
1687 case 0:
1688 return GDB_SIGNAL_0;
1689
1690 case FREEBSD_SIGHUP:
1691 return GDB_SIGNAL_HUP;
1692
1693 case FREEBSD_SIGINT:
1694 return GDB_SIGNAL_INT;
1695
1696 case FREEBSD_SIGQUIT:
1697 return GDB_SIGNAL_QUIT;
1698
1699 case FREEBSD_SIGILL:
1700 return GDB_SIGNAL_ILL;
1701
1702 case FREEBSD_SIGTRAP:
1703 return GDB_SIGNAL_TRAP;
1704
1705 case FREEBSD_SIGABRT:
1706 return GDB_SIGNAL_ABRT;
1707
1708 case FREEBSD_SIGEMT:
1709 return GDB_SIGNAL_EMT;
1710
1711 case FREEBSD_SIGFPE:
1712 return GDB_SIGNAL_FPE;
1713
1714 case FREEBSD_SIGKILL:
1715 return GDB_SIGNAL_KILL;
1716
1717 case FREEBSD_SIGBUS:
1718 return GDB_SIGNAL_BUS;
1719
1720 case FREEBSD_SIGSEGV:
1721 return GDB_SIGNAL_SEGV;
1722
1723 case FREEBSD_SIGSYS:
1724 return GDB_SIGNAL_SYS;
1725
1726 case FREEBSD_SIGPIPE:
1727 return GDB_SIGNAL_PIPE;
1728
1729 case FREEBSD_SIGALRM:
1730 return GDB_SIGNAL_ALRM;
1731
1732 case FREEBSD_SIGTERM:
1733 return GDB_SIGNAL_TERM;
1734
1735 case FREEBSD_SIGURG:
1736 return GDB_SIGNAL_URG;
1737
1738 case FREEBSD_SIGSTOP:
1739 return GDB_SIGNAL_STOP;
1740
1741 case FREEBSD_SIGTSTP:
1742 return GDB_SIGNAL_TSTP;
1743
1744 case FREEBSD_SIGCONT:
1745 return GDB_SIGNAL_CONT;
1746
1747 case FREEBSD_SIGCHLD:
1748 return GDB_SIGNAL_CHLD;
1749
1750 case FREEBSD_SIGTTIN:
1751 return GDB_SIGNAL_TTIN;
1752
1753 case FREEBSD_SIGTTOU:
1754 return GDB_SIGNAL_TTOU;
1755
1756 case FREEBSD_SIGIO:
1757 return GDB_SIGNAL_IO;
1758
1759 case FREEBSD_SIGXCPU:
1760 return GDB_SIGNAL_XCPU;
1761
1762 case FREEBSD_SIGXFSZ:
1763 return GDB_SIGNAL_XFSZ;
1764
1765 case FREEBSD_SIGVTALRM:
1766 return GDB_SIGNAL_VTALRM;
1767
1768 case FREEBSD_SIGPROF:
1769 return GDB_SIGNAL_PROF;
1770
1771 case FREEBSD_SIGWINCH:
1772 return GDB_SIGNAL_WINCH;
1773
1774 case FREEBSD_SIGINFO:
1775 return GDB_SIGNAL_INFO;
1776
1777 case FREEBSD_SIGUSR1:
1778 return GDB_SIGNAL_USR1;
1779
1780 case FREEBSD_SIGUSR2:
1781 return GDB_SIGNAL_USR2;
1782
1783 /* SIGTHR is the same as SIGLWP on FreeBSD. */
1784 case FREEBSD_SIGTHR:
1785 return GDB_SIGNAL_LWP;
1786
1787 case FREEBSD_SIGLIBRT:
1788 return GDB_SIGNAL_LIBRT;
1789 }
1790
1791 if (signal >= FREEBSD_SIGRTMIN && signal <= FREEBSD_SIGRTMAX)
1792 {
1793 int offset = signal - FREEBSD_SIGRTMIN;
1794
1795 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_65 + offset);
1796 }
1797
1798 return GDB_SIGNAL_UNKNOWN;
1799}
1800
1801/* Implement the "gdb_signal_to_target" gdbarch method. */
1802
1803static int
1804fbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
dda83cd7 1805 enum gdb_signal signal)
e61667ef
RS
1806{
1807 switch (signal)
1808 {
1809 case GDB_SIGNAL_0:
1810 return 0;
1811
1812 case GDB_SIGNAL_HUP:
1813 return FREEBSD_SIGHUP;
1814
1815 case GDB_SIGNAL_INT:
1816 return FREEBSD_SIGINT;
1817
1818 case GDB_SIGNAL_QUIT:
1819 return FREEBSD_SIGQUIT;
1820
1821 case GDB_SIGNAL_ILL:
1822 return FREEBSD_SIGILL;
1823
1824 case GDB_SIGNAL_TRAP:
1825 return FREEBSD_SIGTRAP;
1826
1827 case GDB_SIGNAL_ABRT:
1828 return FREEBSD_SIGABRT;
1829
1830 case GDB_SIGNAL_EMT:
1831 return FREEBSD_SIGEMT;
1832
1833 case GDB_SIGNAL_FPE:
1834 return FREEBSD_SIGFPE;
1835
1836 case GDB_SIGNAL_KILL:
1837 return FREEBSD_SIGKILL;
1838
1839 case GDB_SIGNAL_BUS:
1840 return FREEBSD_SIGBUS;
1841
1842 case GDB_SIGNAL_SEGV:
1843 return FREEBSD_SIGSEGV;
1844
1845 case GDB_SIGNAL_SYS:
1846 return FREEBSD_SIGSYS;
1847
1848 case GDB_SIGNAL_PIPE:
1849 return FREEBSD_SIGPIPE;
1850
1851 case GDB_SIGNAL_ALRM:
1852 return FREEBSD_SIGALRM;
1853
1854 case GDB_SIGNAL_TERM:
1855 return FREEBSD_SIGTERM;
1856
1857 case GDB_SIGNAL_URG:
1858 return FREEBSD_SIGURG;
1859
1860 case GDB_SIGNAL_STOP:
1861 return FREEBSD_SIGSTOP;
1862
1863 case GDB_SIGNAL_TSTP:
1864 return FREEBSD_SIGTSTP;
1865
1866 case GDB_SIGNAL_CONT:
1867 return FREEBSD_SIGCONT;
1868
1869 case GDB_SIGNAL_CHLD:
1870 return FREEBSD_SIGCHLD;
1871
1872 case GDB_SIGNAL_TTIN:
1873 return FREEBSD_SIGTTIN;
1874
1875 case GDB_SIGNAL_TTOU:
1876 return FREEBSD_SIGTTOU;
1877
1878 case GDB_SIGNAL_IO:
1879 return FREEBSD_SIGIO;
1880
1881 case GDB_SIGNAL_XCPU:
1882 return FREEBSD_SIGXCPU;
1883
1884 case GDB_SIGNAL_XFSZ:
1885 return FREEBSD_SIGXFSZ;
1886
1887 case GDB_SIGNAL_VTALRM:
1888 return FREEBSD_SIGVTALRM;
1889
1890 case GDB_SIGNAL_PROF:
1891 return FREEBSD_SIGPROF;
1892
1893 case GDB_SIGNAL_WINCH:
1894 return FREEBSD_SIGWINCH;
1895
1896 case GDB_SIGNAL_INFO:
1897 return FREEBSD_SIGINFO;
1898
1899 case GDB_SIGNAL_USR1:
1900 return FREEBSD_SIGUSR1;
1901
1902 case GDB_SIGNAL_USR2:
1903 return FREEBSD_SIGUSR2;
1904
1905 case GDB_SIGNAL_LWP:
1906 return FREEBSD_SIGTHR;
1907
1908 case GDB_SIGNAL_LIBRT:
1909 return FREEBSD_SIGLIBRT;
1910 }
1911
1912 if (signal >= GDB_SIGNAL_REALTIME_65
1913 && signal <= GDB_SIGNAL_REALTIME_126)
1914 {
1915 int offset = signal - GDB_SIGNAL_REALTIME_65;
1916
1917 return FREEBSD_SIGRTMIN + offset;
1918 }
1919
1920 return -1;
1921}
1922
e6cdd38e
JB
1923/* Implement the "get_syscall_number" gdbarch method. */
1924
1925static LONGEST
00431a78 1926fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
e6cdd38e
JB
1927{
1928
1929 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1930 native targets fetch the system call number from the
1931 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1932 However, system call catching requires this function to be
1933 set. */
1934
f34652de 1935 internal_error (_("fbsd_get_sycall_number called"));
e6cdd38e
JB
1936}
1937
945f3901
JB
1938/* Read an integer symbol value from the current target. */
1939
1940static LONGEST
1941fbsd_read_integer_by_name (struct gdbarch *gdbarch, const char *name)
1942{
4144d36a
SM
1943 bound_minimal_symbol ms
1944 = lookup_minimal_symbol (current_program_space, name);
945f3901
JB
1945 if (ms.minsym == NULL)
1946 error (_("Unable to resolve symbol '%s'"), name);
1947
1948 gdb_byte buf[4];
4aeddc50 1949 if (target_read_memory (ms.value_address (), buf, sizeof buf) != 0)
945f3901
JB
1950 error (_("Unable to read value of '%s'"), name);
1951
2a50938a 1952 return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
945f3901
JB
1953}
1954
1955/* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
1956 structure needed to determine the TLS index of an object file. */
1957
1958static void
1959fbsd_fetch_rtld_offsets (struct gdbarch *gdbarch, struct fbsd_pspace_data *data)
1960{
a70b8144 1961 try
945f3901
JB
1962 {
1963 /* Fetch offsets from debug symbols in rtld. */
1964 struct symbol *obj_entry_sym
ccf41c24
TT
1965 = lookup_symbol_in_language ("Struct_Obj_Entry", nullptr,
1966 SEARCH_STRUCT_DOMAIN,
1967 language_c, nullptr).symbol;
945f3901
JB
1968 if (obj_entry_sym == NULL)
1969 error (_("Unable to find Struct_Obj_Entry symbol"));
5f9c5a63 1970 data->off_linkmap = lookup_struct_elt (obj_entry_sym->type (),
945f3901 1971 "linkmap", 0).offset / 8;
5f9c5a63 1972 data->off_tlsindex = lookup_struct_elt (obj_entry_sym->type (),
945f3901
JB
1973 "tlsindex", 0).offset / 8;
1974 data->rtld_offsets_valid = true;
1975 return;
1976 }
230d2906 1977 catch (const gdb_exception_error &e)
945f3901
JB
1978 {
1979 data->off_linkmap = -1;
1980 }
945f3901 1981
a70b8144 1982 try
945f3901
JB
1983 {
1984 /* Fetch offsets from global variables in libthr. Note that
1985 this does not work for single-threaded processes that are not
1986 linked against libthr. */
1987 data->off_linkmap = fbsd_read_integer_by_name (gdbarch,
1988 "_thread_off_linkmap");
1989 data->off_tlsindex = fbsd_read_integer_by_name (gdbarch,
1990 "_thread_off_tlsindex");
1991 data->rtld_offsets_valid = true;
1992 return;
1993 }
230d2906 1994 catch (const gdb_exception_error &e)
945f3901
JB
1995 {
1996 data->off_linkmap = -1;
1997 }
945f3901
JB
1998}
1999
2000/* Helper function to read the TLS index of an object file associated
2001 with a link map entry at LM_ADDR. */
2002
2003static LONGEST
2004fbsd_get_tls_index (struct gdbarch *gdbarch, CORE_ADDR lm_addr)
2005{
2006 struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
2007
2008 if (!data->rtld_offsets_valid)
2009 fbsd_fetch_rtld_offsets (gdbarch, data);
2010
2011 if (data->off_linkmap == -1)
2012 throw_error (TLS_GENERIC_ERROR,
2013 _("Cannot fetch runtime linker structure offsets"));
2014
2015 /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
2016 pointer and then compute the offset of the tlsindex member. */
2017 CORE_ADDR tlsindex_addr = lm_addr - data->off_linkmap + data->off_tlsindex;
2018
2019 gdb_byte buf[4];
2020 if (target_read_memory (tlsindex_addr, buf, sizeof buf) != 0)
2021 throw_error (TLS_GENERIC_ERROR,
2022 _("Cannot find thread-local variables on this target"));
2023
2a50938a 2024 return extract_signed_integer (buf, gdbarch_byte_order (gdbarch));
945f3901
JB
2025}
2026
2027/* See fbsd-tdep.h. */
2028
2029CORE_ADDR
2030fbsd_get_thread_local_address (struct gdbarch *gdbarch, CORE_ADDR dtv_addr,
2031 CORE_ADDR lm_addr, CORE_ADDR offset)
2032{
2033 LONGEST tls_index = fbsd_get_tls_index (gdbarch, lm_addr);
2034
d724d71a
SM
2035 gdb::byte_vector buf (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
2036 if (target_read_memory (dtv_addr, buf.data (), buf.size ()) != 0)
945f3901
JB
2037 throw_error (TLS_GENERIC_ERROR,
2038 _("Cannot find thread-local variables on this target"));
2039
2040 const struct builtin_type *builtin = builtin_type (gdbarch);
d724d71a
SM
2041 CORE_ADDR addr
2042 = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr,
2043 buf.data ());
945f3901 2044
df86565b 2045 addr += (tls_index + 1) * builtin->builtin_data_ptr->length ();
d724d71a 2046 if (target_read_memory (addr, buf.data (), buf.size ()) != 0)
945f3901
JB
2047 throw_error (TLS_GENERIC_ERROR,
2048 _("Cannot find thread-local variables on this target"));
2049
d724d71a
SM
2050 addr = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr,
2051 buf.data ());
945f3901
JB
2052 return addr + offset;
2053}
2054
ed810cc7
JB
2055/* See fbsd-tdep.h. */
2056
2057CORE_ADDR
2058fbsd_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
2059{
4144d36a
SM
2060 bound_minimal_symbol msym
2061 = lookup_minimal_symbol (current_program_space, "_rtld_bind");
4aeddc50 2062 if (msym.minsym != nullptr && msym.value_address () == pc)
ed810cc7
JB
2063 return frame_unwind_caller_pc (get_current_frame ());
2064
2065 return 0;
2066}
2067
83810881
JB
2068/* Return description of signal code or nullptr. */
2069
2070static const char *
2071fbsd_signal_cause (enum gdb_signal siggnal, int code)
2072{
2073 /* Signal-independent causes. */
2074 switch (code)
2075 {
2076 case FBSD_SI_USER:
2077 return _("Sent by kill()");
2078 case FBSD_SI_QUEUE:
2079 return _("Sent by sigqueue()");
2080 case FBSD_SI_TIMER:
2081 return _("Timer expired");
2082 case FBSD_SI_ASYNCIO:
2083 return _("Asynchronous I/O request completed");
2084 case FBSD_SI_MESGQ:
2085 return _("Message arrived on empty message queue");
2086 case FBSD_SI_KERNEL:
2087 return _("Sent by kernel");
2088 case FBSD_SI_LWP:
2089 return _("Sent by thr_kill()");
2090 }
2091
2092 switch (siggnal)
2093 {
2094 case GDB_SIGNAL_ILL:
2095 switch (code)
2096 {
2097 case FBSD_ILL_ILLOPC:
2098 return _("Illegal opcode");
2099 case FBSD_ILL_ILLOPN:
2100 return _("Illegal operand");
2101 case FBSD_ILL_ILLADR:
2102 return _("Illegal addressing mode");
2103 case FBSD_ILL_ILLTRP:
2104 return _("Illegal trap");
2105 case FBSD_ILL_PRVOPC:
2106 return _("Privileged opcode");
2107 case FBSD_ILL_PRVREG:
2108 return _("Privileged register");
2109 case FBSD_ILL_COPROC:
2110 return _("Coprocessor error");
2111 case FBSD_ILL_BADSTK:
2112 return _("Internal stack error");
2113 }
2114 break;
2115 case GDB_SIGNAL_BUS:
2116 switch (code)
2117 {
2118 case FBSD_BUS_ADRALN:
2119 return _("Invalid address alignment");
2120 case FBSD_BUS_ADRERR:
2121 return _("Address not present");
2122 case FBSD_BUS_OBJERR:
2123 return _("Object-specific hardware error");
2124 case FBSD_BUS_OOMERR:
2125 return _("Out of memory");
2126 }
2127 break;
2128 case GDB_SIGNAL_SEGV:
2129 switch (code)
2130 {
2131 case FBSD_SEGV_MAPERR:
2132 return _("Address not mapped to object");
2133 case FBSD_SEGV_ACCERR:
2134 return _("Invalid permissions for mapped object");
2135 case FBSD_SEGV_PKUERR:
2136 return _("PKU violation");
2137 }
2138 break;
2139 case GDB_SIGNAL_FPE:
2140 switch (code)
2141 {
2142 case FBSD_FPE_INTOVF:
2143 return _("Integer overflow");
2144 case FBSD_FPE_INTDIV:
2145 return _("Integer divide by zero");
2146 case FBSD_FPE_FLTDIV:
2147 return _("Floating point divide by zero");
2148 case FBSD_FPE_FLTOVF:
2149 return _("Floating point overflow");
2150 case FBSD_FPE_FLTUND:
2151 return _("Floating point underflow");
2152 case FBSD_FPE_FLTRES:
2153 return _("Floating point inexact result");
2154 case FBSD_FPE_FLTINV:
2155 return _("Invalid floating point operation");
2156 case FBSD_FPE_FLTSUB:
2157 return _("Subscript out of range");
2158 }
2159 break;
2160 case GDB_SIGNAL_TRAP:
2161 switch (code)
2162 {
2163 case FBSD_TRAP_BRKPT:
2164 return _("Breakpoint");
2165 case FBSD_TRAP_TRACE:
2166 return _("Trace trap");
2167 case FBSD_TRAP_DTRACE:
2168 return _("DTrace-induced trap");
2169 case FBSD_TRAP_CAP:
2170 return _("Capability violation");
2171 }
2172 break;
2173 case GDB_SIGNAL_CHLD:
2174 switch (code)
2175 {
2176 case FBSD_CLD_EXITED:
2177 return _("Child has exited");
2178 case FBSD_CLD_KILLED:
2179 return _("Child has terminated abnormally");
2180 case FBSD_CLD_DUMPED:
2181 return _("Child has dumped core");
2182 case FBSD_CLD_TRAPPED:
2183 return _("Traced child has trapped");
2184 case FBSD_CLD_STOPPED:
2185 return _("Child has stopped");
2186 case FBSD_CLD_CONTINUED:
2187 return _("Stopped child has continued");
2188 }
2189 break;
2190 case GDB_SIGNAL_POLL:
2191 switch (code)
2192 {
2193 case FBSD_POLL_IN:
2194 return _("Data input available");
2195 case FBSD_POLL_OUT:
2196 return _("Output buffers available");
2197 case FBSD_POLL_MSG:
2198 return _("Input message available");
2199 case FBSD_POLL_ERR:
2200 return _("I/O error");
2201 case FBSD_POLL_PRI:
2202 return _("High priority input available");
2203 case FBSD_POLL_HUP:
2204 return _("Device disconnected");
2205 }
2206 break;
2207 }
2208
2209 return nullptr;
2210}
2211
2212/* Report additional details for a signal stop. */
2213
2214static void
2215fbsd_report_signal_info (struct gdbarch *gdbarch, struct ui_out *uiout,
2216 enum gdb_signal siggnal)
2217{
2218 LONGEST code, mqd, pid, status, timerid, uid;
2219
2220 try
2221 {
2222 code = parse_and_eval_long ("$_siginfo.si_code");
2223 pid = parse_and_eval_long ("$_siginfo.si_pid");
2224 uid = parse_and_eval_long ("$_siginfo.si_uid");
2225 status = parse_and_eval_long ("$_siginfo.si_status");
2226 timerid = parse_and_eval_long ("$_siginfo._reason._timer.si_timerid");
2227 mqd = parse_and_eval_long ("$_siginfo._reason._mesgq.si_mqd");
2228 }
2229 catch (const gdb_exception_error &e)
2230 {
2231 return;
2232 }
2233
2234 const char *meaning = fbsd_signal_cause (siggnal, code);
2235 if (meaning == nullptr)
2236 return;
2237
2238 uiout->text (".\n");
2239 uiout->field_string ("sigcode-meaning", meaning);
2240
2241 switch (code)
2242 {
2243 case FBSD_SI_USER:
2244 case FBSD_SI_QUEUE:
2245 case FBSD_SI_LWP:
2246 uiout->text (" from pid ");
2247 uiout->field_string ("sending-pid", plongest (pid));
2248 uiout->text (" and user ");
2249 uiout->field_string ("sending-uid", plongest (uid));
2250 return;
2251 case FBSD_SI_TIMER:
2252 uiout->text (": timerid ");
2253 uiout->field_string ("timerid", plongest (timerid));
2254 return;
2255 case FBSD_SI_MESGQ:
2256 uiout->text (": message queue ");
2257 uiout->field_string ("message-queue", plongest (mqd));
2258 return;
2259 case FBSD_SI_ASYNCIO:
2260 return;
2261 }
2262
2263 if (siggnal == GDB_SIGNAL_CHLD)
2264 {
2265 uiout->text (": pid ");
2266 uiout->field_string ("child-pid", plongest (pid));
2267 uiout->text (", uid ");
2268 uiout->field_string ("child-uid", plongest (uid));
2269 if (code == FBSD_CLD_EXITED)
2270 {
2271 uiout->text (", exit status ");
2272 uiout->field_string ("exit-status", plongest (status));
2273 }
2274 else
2275 {
2276 uiout->text (", signal ");
2277 uiout->field_string ("signal", plongest (status));
2278 }
2279 }
2280}
2281
c1dae0a6
JB
2282/* Search a list of struct kinfo_vmmap entries in the ENTRIES buffer
2283 of LEN bytes to find the length of the entry starting at ADDR.
2284 Returns the length of the entry or zero if no entry was found. */
2285
2286static ULONGEST
2287fbsd_vmmap_length (struct gdbarch *gdbarch, unsigned char *entries, size_t len,
2288 CORE_ADDR addr)
2289{
2290 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2291 unsigned char *descdata = entries;
2292 unsigned char *descend = descdata + len;
2293
2294 /* Skip over the structure size. */
2295 descdata += 4;
2296
2297 while (descdata + KVE_PATH < descend)
2298 {
2299 ULONGEST structsize = extract_unsigned_integer (descdata
2300 + KVE_STRUCTSIZE, 4,
2301 byte_order);
2302 if (structsize < KVE_PATH)
2303 return false;
2304
2305 ULONGEST start = extract_unsigned_integer (descdata + KVE_START, 8,
2306 byte_order);
2307 ULONGEST end = extract_unsigned_integer (descdata + KVE_END, 8,
2308 byte_order);
2309 if (start == addr)
2310 return end - start;
2311
2312 descdata += structsize;
2313 }
2314 return 0;
2315}
2316
2317/* Helper for fbsd_vsyscall_range that does the real work of finding
2318 the vDSO's address range. */
2319
2320static bool
2321fbsd_vdso_range (struct gdbarch *gdbarch, struct mem_range *range)
2322{
82d23ca8 2323 if (target_auxv_search (AT_FREEBSD_KPRELOAD, &range->start) <= 0)
c1dae0a6
JB
2324 return false;
2325
2326 if (!target_has_execution ())
2327 {
2328 /* Search for the ending address in the NT_PROCSTAT_VMMAP note. */
6fdf95ae
SM
2329 bfd *cbfd = current_program_space->core_bfd ();
2330 asection *section = bfd_get_section_by_name (cbfd,
c1dae0a6
JB
2331 ".note.freebsdcore.vmmap");
2332 if (section == nullptr)
2333 return false;
2334
2335 size_t note_size = bfd_section_size (section);
2336 if (note_size < 4)
2337 return false;
2338
2339 gdb::def_vector<unsigned char> contents (note_size);
6fdf95ae 2340 if (!bfd_get_section_contents (cbfd, section, contents.data (),
c1dae0a6
JB
2341 0, note_size))
2342 return false;
2343
2344 range->length = fbsd_vmmap_length (gdbarch, contents.data (), note_size,
2345 range->start);
2346 }
2347 else
2348 {
2349 /* Fetch the list of address space entries from the running target. */
6b09f134 2350 std::optional<gdb::byte_vector> buf =
82d23ca8
SM
2351 target_read_alloc (current_inferior ()->top_target (),
2352 TARGET_OBJECT_FREEBSD_VMMAP, nullptr);
c1dae0a6
JB
2353 if (!buf || buf->empty ())
2354 return false;
2355
2356 range->length = fbsd_vmmap_length (gdbarch, buf->data (), buf->size (),
2357 range->start);
2358 }
2359 return range->length != 0;
2360}
2361
671c82e9
AB
2362/* Try to extract the inferior arguments, environment, and executable name
2363 from CBFD. */
2364
2365static core_file_exec_context
2366fbsd_corefile_parse_exec_context_1 (struct gdbarch *gdbarch, bfd *cbfd)
2367{
2368 gdb_assert (gdbarch != nullptr);
2369
2370 /* If there's no core file loaded then we're done. */
2371 if (cbfd == nullptr)
2372 return {};
2373
2374 int ptr_bytes = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
2375
2376 /* Find the .auxv section in the core file. The BFD library creates this
2377 for us from the AUXV note when the BFD is opened. If the section
2378 can't be found then there's nothing more we can do. */
2379 struct bfd_section * section = bfd_get_section_by_name (cbfd, ".auxv");
2380 if (section == nullptr)
2381 return {};
2382
2383 /* Grab the contents of the .auxv section. If we can't get the contents
2384 then there's nothing more we can do. */
2385 bfd_size_type size = bfd_section_size (section);
2386 if (bfd_section_size_insane (cbfd, section))
2387 return {};
2388 gdb::byte_vector contents (size);
2389 if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, size))
2390 return {};
2391
2392 /* Read AT_FREEBSD_ARGV, the address of the argument string vector. */
2393 CORE_ADDR argv_addr;
2394 if (target_auxv_search (contents, current_inferior ()->top_target (),
2395 gdbarch, AT_FREEBSD_ARGV, &argv_addr) != 1)
2396 return {};
2397
2398 /* Read AT_FREEBSD_ARGV, the address of the environment string vector. */
2399 CORE_ADDR envv_addr;
2400 if (target_auxv_search (contents, current_inferior ()->top_target (),
2401 gdbarch, AT_FREEBSD_ENVV, &envv_addr) != 1)
2402 return {};
2403
2404 /* Read the AT_EXECPATH string. It's OK if we can't get this
2405 information. */
2406 gdb::unique_xmalloc_ptr<char> execpath;
2407 CORE_ADDR execpath_string_addr;
2408 if (target_auxv_search (contents, current_inferior ()->top_target (),
2409 gdbarch, AT_FREEBSD_EXECPATH,
2410 &execpath_string_addr) == 1)
2411 execpath = target_read_string (execpath_string_addr, INT_MAX);
2412
2413 /* The byte order. */
2414 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2415
2416 /* On FreeBSD the command the user ran is found in argv[0]. When we
2417 read the first argument we place it into EXECFN. */
2418 gdb::unique_xmalloc_ptr<char> execfn;
2419
2420 /* Read strings from AT_FREEBSD_ARGV until we find a NULL marker. The
2421 first argument is placed into EXECFN as the command name. */
2422 std::vector<gdb::unique_xmalloc_ptr<char>> arguments;
2423 CORE_ADDR str_addr;
2424 while ((str_addr
2425 = (CORE_ADDR) read_memory_unsigned_integer (argv_addr, ptr_bytes,
2426 byte_order)) != 0)
2427 {
2428 gdb::unique_xmalloc_ptr<char> str
2429 = target_read_string (str_addr, INT_MAX);
2430 if (str == nullptr)
2431 return {};
2432
2433 if (execfn == nullptr)
2434 execfn = std::move (str);
2435 else
2436 arguments.emplace_back (std::move (str));
2437
2438 argv_addr += ptr_bytes;
2439 }
2440
2441 /* Read strings from AT_FREEBSD_ENVV until we find a NULL marker. */
2442 std::vector<gdb::unique_xmalloc_ptr<char>> environment;
2443 while ((str_addr
2444 = (uint64_t) read_memory_unsigned_integer (envv_addr, ptr_bytes,
2445 byte_order)) != 0)
2446 {
2447 gdb::unique_xmalloc_ptr<char> str
2448 = target_read_string (str_addr, INT_MAX);
2449 if (str == nullptr)
2450 return {};
2451
2452 environment.emplace_back (std::move (str));
2453 envv_addr += ptr_bytes;
2454 }
2455
2456 return core_file_exec_context (std::move (execfn),
2457 std::move (execpath),
2458 std::move (arguments),
2459 std::move (environment));
2460}
2461
2462/* See elf-corelow.h. */
2463
2464static core_file_exec_context
2465fbsd_corefile_parse_exec_context (struct gdbarch *gdbarch, bfd *cbfd)
2466{
2467 /* Catch and discard memory errors.
2468
2469 If the core file format is not as we expect then we can easily trigger
2470 a memory error while parsing the core file. We don't want this to
2471 prevent the user from opening the core file; the information provided
2472 by this function is helpful, but not critical, debugging can continue
2473 without it. Instead just give a warning and return an empty context
2474 object. */
2475 try
2476 {
2477 return fbsd_corefile_parse_exec_context_1 (gdbarch, cbfd);
2478 }
2479 catch (const gdb_exception_error &ex)
2480 {
2481 if (ex.error == MEMORY_ERROR)
2482 {
2483 warning
2484 (_("failed to parse execution context from corefile: %s"),
2485 ex.message->c_str ());
2486 return {};
2487 }
2488 else
2489 throw;
2490 }
2491}
2492
c1dae0a6
JB
2493/* Return the address range of the vDSO for the current inferior. */
2494
2495static int
2496fbsd_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
2497{
2498 struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
2499
2500 if (data->vdso_range_p == 0)
2501 {
2502 if (fbsd_vdso_range (gdbarch, &data->vdso_range))
2503 data->vdso_range_p = 1;
2504 else
2505 data->vdso_range_p = -1;
2506 }
2507
2508 if (data->vdso_range_p < 0)
2509 return 0;
2510
2511 *range = data->vdso_range;
2512 return 1;
2513}
2514
1736a7bd 2515/* To be called from GDB_OSABI_FREEBSD handlers. */
a904c024
AA
2516
2517void
2518fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2519{
79117428
JB
2520 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
2521 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
4b654465 2522 set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
a904c024 2523 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
d2176225 2524 set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
82372b2f 2525 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
762c974a 2526 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
e61667ef
RS
2527 set_gdbarch_gdb_signal_from_target (gdbarch, fbsd_gdb_signal_from_target);
2528 set_gdbarch_gdb_signal_to_target (gdbarch, fbsd_gdb_signal_to_target);
83810881 2529 set_gdbarch_report_signal_info (gdbarch, fbsd_report_signal_info);
ed810cc7 2530 set_gdbarch_skip_solib_resolver (gdbarch, fbsd_skip_solib_resolver);
c1dae0a6 2531 set_gdbarch_vsyscall_range (gdbarch, fbsd_vsyscall_range);
e6cdd38e
JB
2532
2533 /* `catch syscall' */
2534 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
2535 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
671c82e9
AB
2536 set_gdbarch_core_parse_exec_context (gdbarch,
2537 fbsd_corefile_parse_exec_context);
a904c024 2538}