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