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