]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/fbsd-tdep.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / fbsd-tdep.c
1 /* Target-dependent code for FreeBSD, architecture-independent.
2
3 Copyright (C) 2002-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "auxv.h"
22 #include "gdbcore.h"
23 #include "inferior.h"
24 #include "objfiles.h"
25 #include "regcache.h"
26 #include "regset.h"
27 #include "gdbthread.h"
28 #include "objfiles.h"
29 #include "xml-syscall.h"
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
32
33 #include "elf-bfd.h"
34 #include "fbsd-tdep.h"
35
36 /* This enum is derived from FreeBSD's <sys/signal.h>. */
37
38 enum
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 };
76
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
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
102 all architectures.
103
104 Note that FreeBSD 7.0 used an older version of this structure
105 (struct kinfo_vmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
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. */
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
115 #define KVE_PROTECTION 0x38
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
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. */
144
145 #define KF_STRUCTSIZE 0x0
146 #define KF_TYPE 0x4
147 #define KF_FD 0x8
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
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
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
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
177 #define KINFO_FILE_FD_TYPE_ROOT -2
178 #define KINFO_FILE_FD_TYPE_JAIL -3
179 #define KINFO_FILE_FD_TYPE_TRACE -4
180 #define KINFO_FILE_FD_TYPE_TEXT -5
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
232 struct 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
241 struct 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
251 struct fbsd_sockaddr_un
252 {
253 uint8_t sun_len;
254 uint8_t sun_family;
255 char sun_path[104];
256 };
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
279 struct 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
318 const 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
355 const 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
392 const 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
429 static struct gdbarch_data *fbsd_gdbarch_data_handle;
430
431 struct fbsd_gdbarch_data
432 {
433 struct type *siginfo_type;
434 };
435
436 static void *
437 init_fbsd_gdbarch_data (struct gdbarch *gdbarch)
438 {
439 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct fbsd_gdbarch_data);
440 }
441
442 static struct fbsd_gdbarch_data *
443 get_fbsd_gdbarch_data (struct gdbarch *gdbarch)
444 {
445 return ((struct fbsd_gdbarch_data *)
446 gdbarch_data (gdbarch, fbsd_gdbarch_data_handle));
447 }
448
449 struct fbsd_pspace_data
450 {
451 /* Offsets in the runtime linker's 'Obj_Entry' structure. */
452 LONGEST off_linkmap = 0;
453 LONGEST off_tlsindex = 0;
454 bool rtld_offsets_valid = false;
455 };
456
457 /* Per-program-space data for FreeBSD architectures. */
458 static const struct program_space_key<fbsd_pspace_data>
459 fbsd_pspace_data_handle;
460
461 static struct fbsd_pspace_data *
462 get_fbsd_pspace_data (struct program_space *pspace)
463 {
464 struct fbsd_pspace_data *data;
465
466 data = fbsd_pspace_data_handle.get (pspace);
467 if (data == NULL)
468 data = fbsd_pspace_data_handle.emplace (pspace);
469
470 return data;
471 }
472
473 /* This is how we want PTIDs from core files to be printed. */
474
475 static std::string
476 fbsd_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
477 {
478 if (ptid.lwp () != 0)
479 return string_printf ("LWP %ld", ptid.lwp ());
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
487 static const char *
488 fbsd_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;
493
494 if (thr->ptid.lwp () != 0)
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. */
503 thread_section_name section_name (".thrmisc", thr->ptid);
504
505 section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
506 if (section != NULL && bfd_section_size (section) > 0)
507 {
508 /* Truncate the name if it is longer than "buf". */
509 size = bfd_section_size (section);
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
531 /* Implement the "core_xfer_siginfo" gdbarch method. */
532
533 static LONGEST
534 fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
535 ULONGEST offset, ULONGEST len)
536 {
537 size_t siginfo_size;
538
539 if (gdbarch_long_bit (gdbarch) == 32)
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;
564 if (gdbarch_long_bit (gdbarch) == 32)
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
576 static int
577 find_signalled_thread (struct thread_info *info, void *data)
578 {
579 if (info->suspend.stop_signal != GDB_SIGNAL_0
580 && info->ptid.pid () == inferior_ptid.pid ())
581 return 1;
582
583 return 0;
584 }
585
586 /* Structure for passing information from
587 fbsd_collect_thread_registers via an iterator to
588 fbsd_collect_regset_section_cb. */
589
590 struct fbsd_collect_regset_section_cb_data
591 {
592 const struct regcache *regcache;
593 bfd *obfd;
594 char *note_data;
595 int *note_size;
596 unsigned long lwp;
597 enum gdb_signal stop_signal;
598 int abort_iteration;
599 };
600
601 static void
602 fbsd_collect_regset_section_cb (const char *sect_name, int supply_size,
603 int collect_size, const struct regset *regset,
604 const char *human_name, void *cb_data)
605 {
606 char *buf;
607 struct fbsd_collect_regset_section_cb_data *data
608 = (struct fbsd_collect_regset_section_cb_data *) cb_data;
609
610 if (data->abort_iteration)
611 return;
612
613 gdb_assert (regset->collect_regset);
614
615 buf = (char *) xmalloc (collect_size);
616 regset->collect_regset (regset, data->regcache, -1, buf, collect_size);
617
618 /* PRSTATUS still needs to be treated specially. */
619 if (strcmp (sect_name, ".reg") == 0)
620 data->note_data = (char *) elfcore_write_prstatus
621 (data->obfd, data->note_data, data->note_size, data->lwp,
622 gdb_signal_to_host (data->stop_signal), buf);
623 else
624 data->note_data = (char *) elfcore_write_register_note
625 (data->obfd, data->note_data, data->note_size,
626 sect_name, buf, collect_size);
627 xfree (buf);
628
629 if (data->note_data == NULL)
630 data->abort_iteration = 1;
631 }
632
633 /* Records the thread's register state for the corefile note
634 section. */
635
636 static char *
637 fbsd_collect_thread_registers (const struct regcache *regcache,
638 ptid_t ptid, bfd *obfd,
639 char *note_data, int *note_size,
640 enum gdb_signal stop_signal)
641 {
642 struct gdbarch *gdbarch = regcache->arch ();
643 struct fbsd_collect_regset_section_cb_data data;
644
645 data.regcache = regcache;
646 data.obfd = obfd;
647 data.note_data = note_data;
648 data.note_size = note_size;
649 data.stop_signal = stop_signal;
650 data.abort_iteration = 0;
651 data.lwp = ptid.lwp ();
652
653 gdbarch_iterate_over_regset_sections (gdbarch,
654 fbsd_collect_regset_section_cb,
655 &data, regcache);
656 return data.note_data;
657 }
658
659 struct fbsd_corefile_thread_data
660 {
661 struct gdbarch *gdbarch;
662 bfd *obfd;
663 char *note_data;
664 int *note_size;
665 enum gdb_signal stop_signal;
666 };
667
668 /* Records the thread's register state for the corefile note
669 section. */
670
671 static void
672 fbsd_corefile_thread (struct thread_info *info,
673 struct fbsd_corefile_thread_data *args)
674 {
675 struct regcache *regcache;
676
677 regcache = get_thread_arch_regcache (info->inf->process_target (),
678 info->ptid, args->gdbarch);
679
680 target_fetch_registers (regcache, -1);
681
682 args->note_data = fbsd_collect_thread_registers
683 (regcache, info->ptid, args->obfd, args->note_data,
684 args->note_size, args->stop_signal);
685 }
686
687 /* Return a byte_vector containing the contents of a core dump note
688 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
689 the data is prefixed with a 32-bit integer size to match the format
690 used in FreeBSD NT_PROCSTAT_* notes. */
691
692 static gdb::optional<gdb::byte_vector>
693 fbsd_make_note_desc (enum target_object object, uint32_t structsize)
694 {
695 gdb::optional<gdb::byte_vector> buf =
696 target_read_alloc (current_top_target (), object, NULL);
697 if (!buf || buf->empty ())
698 return {};
699
700 if (structsize == 0)
701 return buf;
702
703 gdb::byte_vector desc (sizeof (structsize) + buf->size ());
704 memcpy (desc.data (), &structsize, sizeof (structsize));
705 memcpy (desc.data () + sizeof (structsize), buf->data (), buf->size ());
706 return desc;
707 }
708
709 /* Create appropriate note sections for a corefile, returning them in
710 allocated memory. */
711
712 static char *
713 fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
714 {
715 struct fbsd_corefile_thread_data thread_args;
716 char *note_data = NULL;
717 Elf_Internal_Ehdr *i_ehdrp;
718 struct thread_info *curr_thr, *signalled_thr;
719
720 /* Put a "FreeBSD" label in the ELF header. */
721 i_ehdrp = elf_elfheader (obfd);
722 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
723
724 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch));
725
726 if (get_exec_file (0))
727 {
728 const char *fname = lbasename (get_exec_file (0));
729 std::string psargs = fname;
730
731 const char *infargs = get_inferior_args ();
732 if (infargs != NULL)
733 psargs = psargs + " " + infargs;
734
735 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
736 fname, psargs.c_str ());
737 }
738
739 /* Thread register information. */
740 try
741 {
742 update_thread_list ();
743 }
744 catch (const gdb_exception_error &e)
745 {
746 exception_print (gdb_stderr, e);
747 }
748
749 /* Like the kernel, prefer dumping the signalled thread first.
750 "First thread" is what tools use to infer the signalled thread.
751 In case there's more than one signalled thread, prefer the
752 current thread, if it is signalled. */
753 curr_thr = inferior_thread ();
754 if (curr_thr->suspend.stop_signal != GDB_SIGNAL_0)
755 signalled_thr = curr_thr;
756 else
757 {
758 signalled_thr = iterate_over_threads (find_signalled_thread, NULL);
759 if (signalled_thr == NULL)
760 signalled_thr = curr_thr;
761 }
762
763 thread_args.gdbarch = gdbarch;
764 thread_args.obfd = obfd;
765 thread_args.note_data = note_data;
766 thread_args.note_size = note_size;
767 thread_args.stop_signal = signalled_thr->suspend.stop_signal;
768
769 fbsd_corefile_thread (signalled_thr, &thread_args);
770 for (thread_info *thr : current_inferior ()->non_exited_threads ())
771 {
772 if (thr == signalled_thr)
773 continue;
774
775 fbsd_corefile_thread (thr, &thread_args);
776 }
777
778 note_data = thread_args.note_data;
779
780 /* Auxiliary vector. */
781 uint32_t structsize = gdbarch_ptr_bit (gdbarch) / 4; /* Elf_Auxinfo */
782 gdb::optional<gdb::byte_vector> note_desc =
783 fbsd_make_note_desc (TARGET_OBJECT_AUXV, structsize);
784 if (note_desc && !note_desc->empty ())
785 {
786 note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
787 NT_FREEBSD_PROCSTAT_AUXV,
788 note_desc->data (), note_desc->size ());
789 if (!note_data)
790 return NULL;
791 }
792
793 /* Virtual memory mappings. */
794 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP, 0);
795 if (note_desc && !note_desc->empty ())
796 {
797 note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
798 NT_FREEBSD_PROCSTAT_VMMAP,
799 note_desc->data (), note_desc->size ());
800 if (!note_data)
801 return NULL;
802 }
803
804 note_desc = fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS, 0);
805 if (note_desc && !note_desc->empty ())
806 {
807 note_data = elfcore_write_note (obfd, note_data, note_size, "FreeBSD",
808 NT_FREEBSD_PROCSTAT_PSSTRINGS,
809 note_desc->data (), note_desc->size ());
810 if (!note_data)
811 return NULL;
812 }
813
814 return note_data;
815 }
816
817 /* Helper function to generate the file descriptor description for a
818 single open file in 'info proc files'. */
819
820 static const char *
821 fbsd_file_fd (int kf_fd)
822 {
823 switch (kf_fd)
824 {
825 case KINFO_FILE_FD_TYPE_CWD:
826 return "cwd";
827 case KINFO_FILE_FD_TYPE_ROOT:
828 return "root";
829 case KINFO_FILE_FD_TYPE_JAIL:
830 return "jail";
831 case KINFO_FILE_FD_TYPE_TRACE:
832 return "trace";
833 case KINFO_FILE_FD_TYPE_TEXT:
834 return "text";
835 case KINFO_FILE_FD_TYPE_CTTY:
836 return "ctty";
837 default:
838 return int_string (kf_fd, 10, 1, 0, 0);
839 }
840 }
841
842 /* Helper function to generate the file type for a single open file in
843 'info proc files'. */
844
845 static const char *
846 fbsd_file_type (int kf_type, int kf_vnode_type)
847 {
848 switch (kf_type)
849 {
850 case KINFO_FILE_TYPE_VNODE:
851 switch (kf_vnode_type)
852 {
853 case KINFO_FILE_VTYPE_VREG:
854 return "file";
855 case KINFO_FILE_VTYPE_VDIR:
856 return "dir";
857 case KINFO_FILE_VTYPE_VCHR:
858 return "chr";
859 case KINFO_FILE_VTYPE_VLNK:
860 return "link";
861 case KINFO_FILE_VTYPE_VSOCK:
862 return "socket";
863 case KINFO_FILE_VTYPE_VFIFO:
864 return "fifo";
865 default:
866 {
867 char *str = get_print_cell ();
868
869 xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
870 return str;
871 }
872 }
873 case KINFO_FILE_TYPE_SOCKET:
874 return "socket";
875 case KINFO_FILE_TYPE_PIPE:
876 return "pipe";
877 case KINFO_FILE_TYPE_FIFO:
878 return "fifo";
879 case KINFO_FILE_TYPE_KQUEUE:
880 return "kqueue";
881 case KINFO_FILE_TYPE_CRYPTO:
882 return "crypto";
883 case KINFO_FILE_TYPE_MQUEUE:
884 return "mqueue";
885 case KINFO_FILE_TYPE_SHM:
886 return "shm";
887 case KINFO_FILE_TYPE_SEM:
888 return "sem";
889 case KINFO_FILE_TYPE_PTS:
890 return "pts";
891 case KINFO_FILE_TYPE_PROCDESC:
892 return "proc";
893 default:
894 return int_string (kf_type, 10, 1, 0, 0);
895 }
896 }
897
898 /* Helper function to generate the file flags for a single open file in
899 'info proc files'. */
900
901 static const char *
902 fbsd_file_flags (int kf_flags)
903 {
904 static char file_flags[10];
905
906 file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
907 file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
908 file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
909 file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
910 file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
911 file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
912 file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
913 file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
914 file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
915 file_flags[9] = '\0';
916
917 return file_flags;
918 }
919
920 /* Helper function to generate the name of an IP protocol. */
921
922 static const char *
923 fbsd_ipproto (int protocol)
924 {
925 switch (protocol)
926 {
927 case FBSD_IPPROTO_ICMP:
928 return "icmp";
929 case FBSD_IPPROTO_TCP:
930 return "tcp";
931 case FBSD_IPPROTO_UDP:
932 return "udp";
933 case FBSD_IPPROTO_SCTP:
934 return "sctp";
935 default:
936 {
937 char *str = get_print_cell ();
938
939 xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
940 return str;
941 }
942 }
943 }
944
945 /* Helper function to print out an IPv4 socket address. */
946
947 static void
948 fbsd_print_sockaddr_in (const void *sockaddr)
949 {
950 const struct fbsd_sockaddr_in *sin =
951 reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
952 char buf[INET_ADDRSTRLEN];
953
954 if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
955 error (_("Failed to format IPv4 address"));
956 printf_filtered ("%s:%u", buf,
957 (sin->sin_port[0] << 8) | sin->sin_port[1]);
958 }
959
960 /* Helper function to print out an IPv6 socket address. */
961
962 static void
963 fbsd_print_sockaddr_in6 (const void *sockaddr)
964 {
965 const struct fbsd_sockaddr_in6 *sin6 =
966 reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
967 char buf[INET6_ADDRSTRLEN];
968
969 if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
970 error (_("Failed to format IPv6 address"));
971 printf_filtered ("%s.%u", buf,
972 (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
973 }
974
975 /* See fbsd-tdep.h. */
976
977 void
978 fbsd_info_proc_files_header ()
979 {
980 printf_filtered (_("Open files:\n\n"));
981 printf_filtered (" %6s %6s %10s %9s %s\n",
982 "FD", "Type", "Offset", "Flags ", "Name");
983 }
984
985 /* See fbsd-tdep.h. */
986
987 void
988 fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
989 LONGEST kf_offset, int kf_vnode_type,
990 int kf_sock_domain, int kf_sock_type,
991 int kf_sock_protocol, const void *kf_sa_local,
992 const void *kf_sa_peer, const void *kf_path)
993 {
994 printf_filtered (" %6s %6s %10s %8s ",
995 fbsd_file_fd (kf_fd),
996 fbsd_file_type (kf_type, kf_vnode_type),
997 kf_offset > -1 ? hex_string (kf_offset) : "-",
998 fbsd_file_flags (kf_flags));
999 if (kf_type == KINFO_FILE_TYPE_SOCKET)
1000 {
1001 switch (kf_sock_domain)
1002 {
1003 case FBSD_AF_UNIX:
1004 {
1005 switch (kf_sock_type)
1006 {
1007 case FBSD_SOCK_STREAM:
1008 printf_filtered ("unix stream:");
1009 break;
1010 case FBSD_SOCK_DGRAM:
1011 printf_filtered ("unix dgram:");
1012 break;
1013 case FBSD_SOCK_SEQPACKET:
1014 printf_filtered ("unix seqpacket:");
1015 break;
1016 default:
1017 printf_filtered ("unix <%d>:", kf_sock_type);
1018 break;
1019 }
1020
1021 /* For local sockets, print out the first non-nul path
1022 rather than both paths. */
1023 const struct fbsd_sockaddr_un *saddr_un
1024 = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
1025 if (saddr_un->sun_path[0] == 0)
1026 saddr_un = reinterpret_cast<const struct fbsd_sockaddr_un *>
1027 (kf_sa_peer);
1028 printf_filtered ("%s", saddr_un->sun_path);
1029 break;
1030 }
1031 case FBSD_AF_INET:
1032 printf_filtered ("%s4 ", fbsd_ipproto (kf_sock_protocol));
1033 fbsd_print_sockaddr_in (kf_sa_local);
1034 printf_filtered (" -> ");
1035 fbsd_print_sockaddr_in (kf_sa_peer);
1036 break;
1037 case FBSD_AF_INET6:
1038 printf_filtered ("%s6 ", fbsd_ipproto (kf_sock_protocol));
1039 fbsd_print_sockaddr_in6 (kf_sa_local);
1040 printf_filtered (" -> ");
1041 fbsd_print_sockaddr_in6 (kf_sa_peer);
1042 break;
1043 }
1044 }
1045 else
1046 printf_filtered ("%s", reinterpret_cast<const char *> (kf_path));
1047 printf_filtered ("\n");
1048 }
1049
1050 /* Implement "info proc files" for a corefile. */
1051
1052 static void
1053 fbsd_core_info_proc_files (struct gdbarch *gdbarch)
1054 {
1055 asection *section
1056 = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
1057 if (section == NULL)
1058 {
1059 warning (_("unable to find open files in core file"));
1060 return;
1061 }
1062
1063 size_t note_size = bfd_section_size (section);
1064 if (note_size < 4)
1065 error (_("malformed core note - too short for header"));
1066
1067 gdb::def_vector<unsigned char> contents (note_size);
1068 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1069 0, note_size))
1070 error (_("could not get core note contents"));
1071
1072 unsigned char *descdata = contents.data ();
1073 unsigned char *descend = descdata + note_size;
1074
1075 /* Skip over the structure size. */
1076 descdata += 4;
1077
1078 fbsd_info_proc_files_header ();
1079
1080 while (descdata + KF_PATH < descend)
1081 {
1082 ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
1083 if (structsize < KF_PATH)
1084 error (_("malformed core note - file structure too small"));
1085
1086 LONGEST type = bfd_get_signed_32 (core_bfd, descdata + KF_TYPE);
1087 LONGEST fd = bfd_get_signed_32 (core_bfd, descdata + KF_FD);
1088 LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KF_FLAGS);
1089 LONGEST offset = bfd_get_signed_64 (core_bfd, descdata + KF_OFFSET);
1090 LONGEST vnode_type = bfd_get_signed_32 (core_bfd,
1091 descdata + KF_VNODE_TYPE);
1092 LONGEST sock_domain = bfd_get_signed_32 (core_bfd,
1093 descdata + KF_SOCK_DOMAIN);
1094 LONGEST sock_type = bfd_get_signed_32 (core_bfd, descdata + KF_SOCK_TYPE);
1095 LONGEST sock_protocol = bfd_get_signed_32 (core_bfd,
1096 descdata + KF_SOCK_PROTOCOL);
1097 fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
1098 sock_domain, sock_type, sock_protocol,
1099 descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
1100 descdata + KF_PATH);
1101
1102 descdata += structsize;
1103 }
1104 }
1105
1106 /* Helper function to generate mappings flags for a single VM map
1107 entry in 'info proc mappings'. */
1108
1109 static const char *
1110 fbsd_vm_map_entry_flags (int kve_flags, int kve_protection)
1111 {
1112 static char vm_flags[9];
1113
1114 vm_flags[0] = (kve_protection & KINFO_VME_PROT_READ) ? 'r' : '-';
1115 vm_flags[1] = (kve_protection & KINFO_VME_PROT_WRITE) ? 'w' : '-';
1116 vm_flags[2] = (kve_protection & KINFO_VME_PROT_EXEC) ? 'x' : '-';
1117 vm_flags[3] = ' ';
1118 vm_flags[4] = (kve_flags & KINFO_VME_FLAG_COW) ? 'C' : '-';
1119 vm_flags[5] = (kve_flags & KINFO_VME_FLAG_NEEDS_COPY) ? 'N' : '-';
1120 vm_flags[6] = (kve_flags & KINFO_VME_FLAG_SUPER) ? 'S' : '-';
1121 vm_flags[7] = (kve_flags & KINFO_VME_FLAG_GROWS_UP) ? 'U'
1122 : (kve_flags & KINFO_VME_FLAG_GROWS_DOWN) ? 'D' : '-';
1123 vm_flags[8] = '\0';
1124
1125 return vm_flags;
1126 }
1127
1128 /* See fbsd-tdep.h. */
1129
1130 void
1131 fbsd_info_proc_mappings_header (int addr_bit)
1132 {
1133 printf_filtered (_("Mapped address spaces:\n\n"));
1134 if (addr_bit == 64)
1135 {
1136 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1137 "Start Addr",
1138 " End Addr",
1139 " Size", " Offset", "Flags ", "File");
1140 }
1141 else
1142 {
1143 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1144 "Start Addr",
1145 " End Addr",
1146 " Size", " Offset", "Flags ", "File");
1147 }
1148 }
1149
1150 /* See fbsd-tdep.h. */
1151
1152 void
1153 fbsd_info_proc_mappings_entry (int addr_bit, ULONGEST kve_start,
1154 ULONGEST kve_end, ULONGEST kve_offset,
1155 int kve_flags, int kve_protection,
1156 const void *kve_path)
1157 {
1158 if (addr_bit == 64)
1159 {
1160 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
1161 hex_string (kve_start),
1162 hex_string (kve_end),
1163 hex_string (kve_end - kve_start),
1164 hex_string (kve_offset),
1165 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1166 reinterpret_cast<const char *> (kve_path));
1167 }
1168 else
1169 {
1170 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
1171 hex_string (kve_start),
1172 hex_string (kve_end),
1173 hex_string (kve_end - kve_start),
1174 hex_string (kve_offset),
1175 fbsd_vm_map_entry_flags (kve_flags, kve_protection),
1176 reinterpret_cast<const char *> (kve_path));
1177 }
1178 }
1179
1180 /* Implement "info proc mappings" for a corefile. */
1181
1182 static void
1183 fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
1184 {
1185 asection *section;
1186 unsigned char *descdata, *descend;
1187 size_t note_size;
1188
1189 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.vmmap");
1190 if (section == NULL)
1191 {
1192 warning (_("unable to find mappings in core file"));
1193 return;
1194 }
1195
1196 note_size = bfd_section_size (section);
1197 if (note_size < 4)
1198 error (_("malformed core note - too short for header"));
1199
1200 gdb::def_vector<unsigned char> contents (note_size);
1201 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1202 0, note_size))
1203 error (_("could not get core note contents"));
1204
1205 descdata = contents.data ();
1206 descend = descdata + note_size;
1207
1208 /* Skip over the structure size. */
1209 descdata += 4;
1210
1211 fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
1212 while (descdata + KVE_PATH < descend)
1213 {
1214 ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KVE_STRUCTSIZE);
1215 if (structsize < KVE_PATH)
1216 error (_("malformed core note - vmmap entry too small"));
1217
1218 ULONGEST start = bfd_get_64 (core_bfd, descdata + KVE_START);
1219 ULONGEST end = bfd_get_64 (core_bfd, descdata + KVE_END);
1220 ULONGEST offset = bfd_get_64 (core_bfd, descdata + KVE_OFFSET);
1221 LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KVE_FLAGS);
1222 LONGEST prot = bfd_get_signed_32 (core_bfd, descdata + KVE_PROTECTION);
1223 fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
1224 offset, flags, prot, descdata + KVE_PATH);
1225
1226 descdata += structsize;
1227 }
1228 }
1229
1230 /* Fetch the pathname of a vnode for a single file descriptor from the
1231 file table core note. */
1232
1233 static gdb::unique_xmalloc_ptr<char>
1234 fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
1235 {
1236 asection *section;
1237 unsigned char *descdata, *descend;
1238 size_t note_size;
1239
1240 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
1241 if (section == NULL)
1242 return nullptr;
1243
1244 note_size = bfd_section_size (section);
1245 if (note_size < 4)
1246 error (_("malformed core note - too short for header"));
1247
1248 gdb::def_vector<unsigned char> contents (note_size);
1249 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1250 0, note_size))
1251 error (_("could not get core note contents"));
1252
1253 descdata = contents.data ();
1254 descend = descdata + note_size;
1255
1256 /* Skip over the structure size. */
1257 descdata += 4;
1258
1259 while (descdata + KF_PATH < descend)
1260 {
1261 ULONGEST structsize;
1262
1263 structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
1264 if (structsize < KF_PATH)
1265 error (_("malformed core note - file structure too small"));
1266
1267 if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
1268 && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
1269 {
1270 char *path = (char *) descdata + KF_PATH;
1271 return make_unique_xstrdup (path);
1272 }
1273
1274 descdata += structsize;
1275 }
1276 return nullptr;
1277 }
1278
1279 /* Helper function to read a struct timeval. */
1280
1281 static void
1282 fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
1283 LONGEST &sec, ULONGEST &usec)
1284 {
1285 if (gdbarch_addr_bit (gdbarch) == 64)
1286 {
1287 sec = bfd_get_signed_64 (core_bfd, data);
1288 usec = bfd_get_64 (core_bfd, data + 8);
1289 }
1290 else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
1291 {
1292 sec = bfd_get_signed_32 (core_bfd, data);
1293 usec = bfd_get_32 (core_bfd, data + 4);
1294 }
1295 else
1296 {
1297 sec = bfd_get_signed_64 (core_bfd, data);
1298 usec = bfd_get_32 (core_bfd, data + 8);
1299 }
1300 }
1301
1302 /* Print out the contents of a signal set. */
1303
1304 static void
1305 fbsd_print_sigset (const char *descr, unsigned char *sigset)
1306 {
1307 printf_filtered ("%s: ", descr);
1308 for (int i = 0; i < SIG_WORDS; i++)
1309 printf_filtered ("%08x ",
1310 (unsigned int) bfd_get_32 (core_bfd, sigset + i * 4));
1311 printf_filtered ("\n");
1312 }
1313
1314 /* Implement "info proc status" for a corefile. */
1315
1316 static void
1317 fbsd_core_info_proc_status (struct gdbarch *gdbarch)
1318 {
1319 const struct kinfo_proc_layout *kp;
1320 asection *section;
1321 unsigned char *descdata;
1322 int addr_bit, long_bit;
1323 size_t note_size;
1324 ULONGEST value;
1325 LONGEST sec;
1326
1327 section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.proc");
1328 if (section == NULL)
1329 {
1330 warning (_("unable to find process info in core file"));
1331 return;
1332 }
1333
1334 addr_bit = gdbarch_addr_bit (gdbarch);
1335 if (addr_bit == 64)
1336 kp = &kinfo_proc_layout_64;
1337 else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
1338 kp = &kinfo_proc_layout_i386;
1339 else
1340 kp = &kinfo_proc_layout_32;
1341 long_bit = gdbarch_long_bit (gdbarch);
1342
1343 /*
1344 * Ensure that the note is large enough for all of the fields fetched
1345 * by this function. In particular, the note must contain the 32-bit
1346 * structure size, then it must be long enough to access the last
1347 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
1348 */
1349 note_size = bfd_section_size (section);
1350 if (note_size < (4 + kp->ki_rusage_ch + kp->ru_majflt
1351 + long_bit / TARGET_CHAR_BIT))
1352 error (_("malformed core note - too short"));
1353
1354 gdb::def_vector<unsigned char> contents (note_size);
1355 if (!bfd_get_section_contents (core_bfd, section, contents.data (),
1356 0, note_size))
1357 error (_("could not get core note contents"));
1358
1359 descdata = contents.data ();
1360
1361 /* Skip over the structure size. */
1362 descdata += 4;
1363
1364 /* Verify 'ki_layout' is 0. */
1365 if (bfd_get_32 (core_bfd, descdata + kp->ki_layout) != 0)
1366 {
1367 warning (_("unsupported process information in core file"));
1368 return;
1369 }
1370
1371 printf_filtered ("Name: %.19s\n", descdata + kp->ki_comm);
1372 printf_filtered ("Process ID: %s\n",
1373 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pid)));
1374 printf_filtered ("Parent process: %s\n",
1375 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ppid)));
1376 printf_filtered ("Process group: %s\n",
1377 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pgid)));
1378 printf_filtered ("Session id: %s\n",
1379 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_sid)));
1380
1381 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
1382 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
1383 kernels the 64-bit 'ki_tdev' field is in a reserved section of
1384 the structure that is cleared to zero. Assume that a zero value
1385 in ki_tdev indicates a core dump from an older kernel and use the
1386 value in 'ki_tdev_freebsd11' instead. */
1387 value = bfd_get_64 (core_bfd, descdata + kp->ki_tdev);
1388 if (value == 0)
1389 value = bfd_get_32 (core_bfd, descdata + kp->ki_tdev_freebsd11);
1390 printf_filtered ("TTY: %s\n", pulongest (value));
1391 printf_filtered ("TTY owner process group: %s\n",
1392 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_tpgid)));
1393 printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
1394 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ruid)),
1395 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_uid)),
1396 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svuid)));
1397 printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
1398 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_rgid)),
1399 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_groups)),
1400 pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svgid)));
1401 printf_filtered ("Groups: ");
1402 uint16_t ngroups = bfd_get_16 (core_bfd, descdata + kp->ki_ngroups);
1403 for (int i = 0; i < ngroups; i++)
1404 printf_filtered ("%s ",
1405 pulongest (bfd_get_32 (core_bfd,
1406 descdata + kp->ki_groups + i * 4)));
1407 printf_filtered ("\n");
1408 value = bfd_get (long_bit, core_bfd,
1409 descdata + kp->ki_rusage + kp->ru_minflt);
1410 printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value));
1411 value = bfd_get (long_bit, core_bfd,
1412 descdata + kp->ki_rusage_ch + kp->ru_minflt);
1413 printf_filtered ("Minor faults, children: %s\n", pulongest (value));
1414 value = bfd_get (long_bit, core_bfd,
1415 descdata + kp->ki_rusage + kp->ru_majflt);
1416 printf_filtered ("Major faults (memory page faults): %s\n",
1417 pulongest (value));
1418 value = bfd_get (long_bit, core_bfd,
1419 descdata + kp->ki_rusage_ch + kp->ru_majflt);
1420 printf_filtered ("Major faults, children: %s\n", pulongest (value));
1421 fbsd_core_fetch_timeval (gdbarch,
1422 descdata + kp->ki_rusage + kp->ru_utime,
1423 sec, value);
1424 printf_filtered ("utime: %s.%06d\n", plongest (sec), (int) value);
1425 fbsd_core_fetch_timeval (gdbarch,
1426 descdata + kp->ki_rusage + kp->ru_stime,
1427 sec, value);
1428 printf_filtered ("stime: %s.%06d\n", plongest (sec), (int) value);
1429 fbsd_core_fetch_timeval (gdbarch,
1430 descdata + kp->ki_rusage_ch + kp->ru_utime,
1431 sec, value);
1432 printf_filtered ("utime, children: %s.%06d\n", plongest (sec), (int) value);
1433 fbsd_core_fetch_timeval (gdbarch,
1434 descdata + kp->ki_rusage_ch + kp->ru_stime,
1435 sec, value);
1436 printf_filtered ("stime, children: %s.%06d\n", plongest (sec), (int) value);
1437 printf_filtered ("'nice' value: %d\n",
1438 (int) bfd_get_signed_8 (core_bfd, descdata + kp->ki_nice));
1439 fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
1440 printf_filtered ("Start time: %s.%06d\n", plongest (sec), (int) value);
1441 printf_filtered ("Virtual memory size: %s kB\n",
1442 pulongest (bfd_get (addr_bit, core_bfd,
1443 descdata + kp->ki_size) / 1024));
1444 printf_filtered ("Data size: %s pages\n",
1445 pulongest (bfd_get (addr_bit, core_bfd,
1446 descdata + kp->ki_dsize)));
1447 printf_filtered ("Stack size: %s pages\n",
1448 pulongest (bfd_get (addr_bit, core_bfd,
1449 descdata + kp->ki_ssize)));
1450 printf_filtered ("Text size: %s pages\n",
1451 pulongest (bfd_get (addr_bit, core_bfd,
1452 descdata + kp->ki_tsize)));
1453 printf_filtered ("Resident set size: %s pages\n",
1454 pulongest (bfd_get (addr_bit, core_bfd,
1455 descdata + kp->ki_rssize)));
1456 printf_filtered ("Maximum RSS: %s pages\n",
1457 pulongest (bfd_get (long_bit, core_bfd,
1458 descdata + kp->ki_rusage
1459 + kp->ru_maxrss)));
1460 fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
1461 fbsd_print_sigset ("Caught Signals", descdata + kp->ki_sigcatch);
1462 }
1463
1464 /* Implement the "core_info_proc" gdbarch method. */
1465
1466 static void
1467 fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
1468 enum info_proc_what what)
1469 {
1470 bool do_cmdline = false;
1471 bool do_cwd = false;
1472 bool do_exe = false;
1473 bool do_files = false;
1474 bool do_mappings = false;
1475 bool do_status = false;
1476 int pid;
1477
1478 switch (what)
1479 {
1480 case IP_MINIMAL:
1481 do_cmdline = true;
1482 do_cwd = true;
1483 do_exe = true;
1484 break;
1485 case IP_MAPPINGS:
1486 do_mappings = true;
1487 break;
1488 case IP_STATUS:
1489 case IP_STAT:
1490 do_status = true;
1491 break;
1492 case IP_CMDLINE:
1493 do_cmdline = true;
1494 break;
1495 case IP_EXE:
1496 do_exe = true;
1497 break;
1498 case IP_CWD:
1499 do_cwd = true;
1500 break;
1501 case IP_FILES:
1502 do_files = true;
1503 break;
1504 case IP_ALL:
1505 do_cmdline = true;
1506 do_cwd = true;
1507 do_exe = true;
1508 do_files = true;
1509 do_mappings = true;
1510 do_status = true;
1511 break;
1512 default:
1513 return;
1514 }
1515
1516 pid = bfd_core_file_pid (core_bfd);
1517 if (pid != 0)
1518 printf_filtered (_("process %d\n"), pid);
1519
1520 if (do_cmdline)
1521 {
1522 const char *cmdline;
1523
1524 cmdline = bfd_core_file_failing_command (core_bfd);
1525 if (cmdline)
1526 printf_filtered ("cmdline = '%s'\n", cmdline);
1527 else
1528 warning (_("Command line unavailable"));
1529 }
1530 if (do_cwd)
1531 {
1532 gdb::unique_xmalloc_ptr<char> cwd =
1533 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_CWD);
1534 if (cwd)
1535 printf_filtered ("cwd = '%s'\n", cwd.get ());
1536 else
1537 warning (_("unable to read current working directory"));
1538 }
1539 if (do_exe)
1540 {
1541 gdb::unique_xmalloc_ptr<char> exe =
1542 fbsd_core_vnode_path (gdbarch, KINFO_FILE_FD_TYPE_TEXT);
1543 if (exe)
1544 printf_filtered ("exe = '%s'\n", exe.get ());
1545 else
1546 warning (_("unable to read executable path name"));
1547 }
1548 if (do_files)
1549 fbsd_core_info_proc_files (gdbarch);
1550 if (do_mappings)
1551 fbsd_core_info_proc_mappings (gdbarch);
1552 if (do_status)
1553 fbsd_core_info_proc_status (gdbarch);
1554 }
1555
1556 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1557
1558 static void
1559 fbsd_print_auxv_entry (struct gdbarch *gdbarch, struct ui_file *file,
1560 CORE_ADDR type, CORE_ADDR val)
1561 {
1562 const char *name = "???";
1563 const char *description = "";
1564 enum auxv_format format = AUXV_FORMAT_HEX;
1565
1566 switch (type)
1567 {
1568 case AT_NULL:
1569 case AT_IGNORE:
1570 case AT_EXECFD:
1571 case AT_PHDR:
1572 case AT_PHENT:
1573 case AT_PHNUM:
1574 case AT_PAGESZ:
1575 case AT_BASE:
1576 case AT_FLAGS:
1577 case AT_ENTRY:
1578 case AT_NOTELF:
1579 case AT_UID:
1580 case AT_EUID:
1581 case AT_GID:
1582 case AT_EGID:
1583 default_print_auxv_entry (gdbarch, file, type, val);
1584 return;
1585 #define _TAGNAME(tag) #tag
1586 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1587 #define TAG(tag, text, kind) \
1588 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1589 TAG (EXECPATH, _("Executable path"), AUXV_FORMAT_STR);
1590 TAG (CANARY, _("Canary for SSP"), AUXV_FORMAT_HEX);
1591 TAG (CANARYLEN, ("Length of the SSP canary"), AUXV_FORMAT_DEC);
1592 TAG (OSRELDATE, _("OSRELDATE"), AUXV_FORMAT_DEC);
1593 TAG (NCPUS, _("Number of CPUs"), AUXV_FORMAT_DEC);
1594 TAG (PAGESIZES, _("Pagesizes"), AUXV_FORMAT_HEX);
1595 TAG (PAGESIZESLEN, _("Number of pagesizes"), AUXV_FORMAT_DEC);
1596 TAG (TIMEKEEP, _("Pointer to timehands"), AUXV_FORMAT_HEX);
1597 TAG (STACKPROT, _("Initial stack protection"), AUXV_FORMAT_HEX);
1598 TAG (EHDRFLAGS, _("ELF header e_flags"), AUXV_FORMAT_HEX);
1599 TAG (HWCAP, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX);
1600 TAG (HWCAP2, _("Extension of AT_HWCAP"), AUXV_FORMAT_HEX);
1601 TAG (BSDFLAGS, _("ELF BSD flags"), AUXV_FORMAT_HEX);
1602 TAG (ARGC, _("Argument count"), AUXV_FORMAT_DEC);
1603 TAG (ARGV, _("Argument vector"), AUXV_FORMAT_HEX);
1604 TAG (ENVC, _("Environment count"), AUXV_FORMAT_DEC);
1605 TAG (ENVV, _("Environment vector"), AUXV_FORMAT_HEX);
1606 TAG (PS_STRINGS, _("Pointer to ps_strings"), AUXV_FORMAT_HEX);
1607 }
1608
1609 fprint_auxv_entry (file, name, description, format, type, val);
1610 }
1611
1612 /* Implement the "get_siginfo_type" gdbarch method. */
1613
1614 static struct type *
1615 fbsd_get_siginfo_type (struct gdbarch *gdbarch)
1616 {
1617 struct fbsd_gdbarch_data *fbsd_gdbarch_data;
1618 struct type *int_type, *int32_type, *uint32_type, *long_type, *void_ptr_type;
1619 struct type *uid_type, *pid_type;
1620 struct type *sigval_type, *reason_type;
1621 struct type *siginfo_type;
1622 struct type *type;
1623
1624 fbsd_gdbarch_data = get_fbsd_gdbarch_data (gdbarch);
1625 if (fbsd_gdbarch_data->siginfo_type != NULL)
1626 return fbsd_gdbarch_data->siginfo_type;
1627
1628 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
1629 0, "int");
1630 int32_type = arch_integer_type (gdbarch, 32, 0, "int32_t");
1631 uint32_type = arch_integer_type (gdbarch, 32, 1, "uint32_t");
1632 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
1633 0, "long");
1634 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
1635
1636 /* union sigval */
1637 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1638 sigval_type->set_name (xstrdup ("sigval"));
1639 append_composite_type_field (sigval_type, "sival_int", int_type);
1640 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
1641
1642 /* __pid_t */
1643 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
1644 TYPE_LENGTH (int32_type) * TARGET_CHAR_BIT, "__pid_t");
1645 TYPE_TARGET_TYPE (pid_type) = int32_type;
1646 TYPE_TARGET_STUB (pid_type) = 1;
1647
1648 /* __uid_t */
1649 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
1650 TYPE_LENGTH (uint32_type) * TARGET_CHAR_BIT,
1651 "__uid_t");
1652 TYPE_TARGET_TYPE (uid_type) = uint32_type;
1653 TYPE_TARGET_STUB (uid_type) = 1;
1654
1655 /* _reason */
1656 reason_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
1657
1658 /* _fault */
1659 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1660 append_composite_type_field (type, "si_trapno", int_type);
1661 append_composite_type_field (reason_type, "_fault", type);
1662
1663 /* _timer */
1664 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1665 append_composite_type_field (type, "si_timerid", int_type);
1666 append_composite_type_field (type, "si_overrun", int_type);
1667 append_composite_type_field (reason_type, "_timer", type);
1668
1669 /* _mesgq */
1670 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1671 append_composite_type_field (type, "si_mqd", int_type);
1672 append_composite_type_field (reason_type, "_mesgq", type);
1673
1674 /* _poll */
1675 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1676 append_composite_type_field (type, "si_band", long_type);
1677 append_composite_type_field (reason_type, "_poll", type);
1678
1679 /* __spare__ */
1680 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1681 append_composite_type_field (type, "__spare1__", long_type);
1682 append_composite_type_field (type, "__spare2__",
1683 init_vector_type (int_type, 7));
1684 append_composite_type_field (reason_type, "__spare__", type);
1685
1686 /* struct siginfo */
1687 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
1688 siginfo_type->set_name (xstrdup ("siginfo"));
1689 append_composite_type_field (siginfo_type, "si_signo", int_type);
1690 append_composite_type_field (siginfo_type, "si_errno", int_type);
1691 append_composite_type_field (siginfo_type, "si_code", int_type);
1692 append_composite_type_field (siginfo_type, "si_pid", pid_type);
1693 append_composite_type_field (siginfo_type, "si_uid", uid_type);
1694 append_composite_type_field (siginfo_type, "si_status", int_type);
1695 append_composite_type_field (siginfo_type, "si_addr", void_ptr_type);
1696 append_composite_type_field (siginfo_type, "si_value", sigval_type);
1697 append_composite_type_field (siginfo_type, "_reason", reason_type);
1698
1699 fbsd_gdbarch_data->siginfo_type = siginfo_type;
1700
1701 return siginfo_type;
1702 }
1703
1704 /* Implement the "gdb_signal_from_target" gdbarch method. */
1705
1706 static enum gdb_signal
1707 fbsd_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
1708 {
1709 switch (signal)
1710 {
1711 case 0:
1712 return GDB_SIGNAL_0;
1713
1714 case FREEBSD_SIGHUP:
1715 return GDB_SIGNAL_HUP;
1716
1717 case FREEBSD_SIGINT:
1718 return GDB_SIGNAL_INT;
1719
1720 case FREEBSD_SIGQUIT:
1721 return GDB_SIGNAL_QUIT;
1722
1723 case FREEBSD_SIGILL:
1724 return GDB_SIGNAL_ILL;
1725
1726 case FREEBSD_SIGTRAP:
1727 return GDB_SIGNAL_TRAP;
1728
1729 case FREEBSD_SIGABRT:
1730 return GDB_SIGNAL_ABRT;
1731
1732 case FREEBSD_SIGEMT:
1733 return GDB_SIGNAL_EMT;
1734
1735 case FREEBSD_SIGFPE:
1736 return GDB_SIGNAL_FPE;
1737
1738 case FREEBSD_SIGKILL:
1739 return GDB_SIGNAL_KILL;
1740
1741 case FREEBSD_SIGBUS:
1742 return GDB_SIGNAL_BUS;
1743
1744 case FREEBSD_SIGSEGV:
1745 return GDB_SIGNAL_SEGV;
1746
1747 case FREEBSD_SIGSYS:
1748 return GDB_SIGNAL_SYS;
1749
1750 case FREEBSD_SIGPIPE:
1751 return GDB_SIGNAL_PIPE;
1752
1753 case FREEBSD_SIGALRM:
1754 return GDB_SIGNAL_ALRM;
1755
1756 case FREEBSD_SIGTERM:
1757 return GDB_SIGNAL_TERM;
1758
1759 case FREEBSD_SIGURG:
1760 return GDB_SIGNAL_URG;
1761
1762 case FREEBSD_SIGSTOP:
1763 return GDB_SIGNAL_STOP;
1764
1765 case FREEBSD_SIGTSTP:
1766 return GDB_SIGNAL_TSTP;
1767
1768 case FREEBSD_SIGCONT:
1769 return GDB_SIGNAL_CONT;
1770
1771 case FREEBSD_SIGCHLD:
1772 return GDB_SIGNAL_CHLD;
1773
1774 case FREEBSD_SIGTTIN:
1775 return GDB_SIGNAL_TTIN;
1776
1777 case FREEBSD_SIGTTOU:
1778 return GDB_SIGNAL_TTOU;
1779
1780 case FREEBSD_SIGIO:
1781 return GDB_SIGNAL_IO;
1782
1783 case FREEBSD_SIGXCPU:
1784 return GDB_SIGNAL_XCPU;
1785
1786 case FREEBSD_SIGXFSZ:
1787 return GDB_SIGNAL_XFSZ;
1788
1789 case FREEBSD_SIGVTALRM:
1790 return GDB_SIGNAL_VTALRM;
1791
1792 case FREEBSD_SIGPROF:
1793 return GDB_SIGNAL_PROF;
1794
1795 case FREEBSD_SIGWINCH:
1796 return GDB_SIGNAL_WINCH;
1797
1798 case FREEBSD_SIGINFO:
1799 return GDB_SIGNAL_INFO;
1800
1801 case FREEBSD_SIGUSR1:
1802 return GDB_SIGNAL_USR1;
1803
1804 case FREEBSD_SIGUSR2:
1805 return GDB_SIGNAL_USR2;
1806
1807 /* SIGTHR is the same as SIGLWP on FreeBSD. */
1808 case FREEBSD_SIGTHR:
1809 return GDB_SIGNAL_LWP;
1810
1811 case FREEBSD_SIGLIBRT:
1812 return GDB_SIGNAL_LIBRT;
1813 }
1814
1815 if (signal >= FREEBSD_SIGRTMIN && signal <= FREEBSD_SIGRTMAX)
1816 {
1817 int offset = signal - FREEBSD_SIGRTMIN;
1818
1819 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_65 + offset);
1820 }
1821
1822 return GDB_SIGNAL_UNKNOWN;
1823 }
1824
1825 /* Implement the "gdb_signal_to_target" gdbarch method. */
1826
1827 static int
1828 fbsd_gdb_signal_to_target (struct gdbarch *gdbarch,
1829 enum gdb_signal signal)
1830 {
1831 switch (signal)
1832 {
1833 case GDB_SIGNAL_0:
1834 return 0;
1835
1836 case GDB_SIGNAL_HUP:
1837 return FREEBSD_SIGHUP;
1838
1839 case GDB_SIGNAL_INT:
1840 return FREEBSD_SIGINT;
1841
1842 case GDB_SIGNAL_QUIT:
1843 return FREEBSD_SIGQUIT;
1844
1845 case GDB_SIGNAL_ILL:
1846 return FREEBSD_SIGILL;
1847
1848 case GDB_SIGNAL_TRAP:
1849 return FREEBSD_SIGTRAP;
1850
1851 case GDB_SIGNAL_ABRT:
1852 return FREEBSD_SIGABRT;
1853
1854 case GDB_SIGNAL_EMT:
1855 return FREEBSD_SIGEMT;
1856
1857 case GDB_SIGNAL_FPE:
1858 return FREEBSD_SIGFPE;
1859
1860 case GDB_SIGNAL_KILL:
1861 return FREEBSD_SIGKILL;
1862
1863 case GDB_SIGNAL_BUS:
1864 return FREEBSD_SIGBUS;
1865
1866 case GDB_SIGNAL_SEGV:
1867 return FREEBSD_SIGSEGV;
1868
1869 case GDB_SIGNAL_SYS:
1870 return FREEBSD_SIGSYS;
1871
1872 case GDB_SIGNAL_PIPE:
1873 return FREEBSD_SIGPIPE;
1874
1875 case GDB_SIGNAL_ALRM:
1876 return FREEBSD_SIGALRM;
1877
1878 case GDB_SIGNAL_TERM:
1879 return FREEBSD_SIGTERM;
1880
1881 case GDB_SIGNAL_URG:
1882 return FREEBSD_SIGURG;
1883
1884 case GDB_SIGNAL_STOP:
1885 return FREEBSD_SIGSTOP;
1886
1887 case GDB_SIGNAL_TSTP:
1888 return FREEBSD_SIGTSTP;
1889
1890 case GDB_SIGNAL_CONT:
1891 return FREEBSD_SIGCONT;
1892
1893 case GDB_SIGNAL_CHLD:
1894 return FREEBSD_SIGCHLD;
1895
1896 case GDB_SIGNAL_TTIN:
1897 return FREEBSD_SIGTTIN;
1898
1899 case GDB_SIGNAL_TTOU:
1900 return FREEBSD_SIGTTOU;
1901
1902 case GDB_SIGNAL_IO:
1903 return FREEBSD_SIGIO;
1904
1905 case GDB_SIGNAL_XCPU:
1906 return FREEBSD_SIGXCPU;
1907
1908 case GDB_SIGNAL_XFSZ:
1909 return FREEBSD_SIGXFSZ;
1910
1911 case GDB_SIGNAL_VTALRM:
1912 return FREEBSD_SIGVTALRM;
1913
1914 case GDB_SIGNAL_PROF:
1915 return FREEBSD_SIGPROF;
1916
1917 case GDB_SIGNAL_WINCH:
1918 return FREEBSD_SIGWINCH;
1919
1920 case GDB_SIGNAL_INFO:
1921 return FREEBSD_SIGINFO;
1922
1923 case GDB_SIGNAL_USR1:
1924 return FREEBSD_SIGUSR1;
1925
1926 case GDB_SIGNAL_USR2:
1927 return FREEBSD_SIGUSR2;
1928
1929 case GDB_SIGNAL_LWP:
1930 return FREEBSD_SIGTHR;
1931
1932 case GDB_SIGNAL_LIBRT:
1933 return FREEBSD_SIGLIBRT;
1934 }
1935
1936 if (signal >= GDB_SIGNAL_REALTIME_65
1937 && signal <= GDB_SIGNAL_REALTIME_126)
1938 {
1939 int offset = signal - GDB_SIGNAL_REALTIME_65;
1940
1941 return FREEBSD_SIGRTMIN + offset;
1942 }
1943
1944 return -1;
1945 }
1946
1947 /* Implement the "get_syscall_number" gdbarch method. */
1948
1949 static LONGEST
1950 fbsd_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
1951 {
1952
1953 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1954 native targets fetch the system call number from the
1955 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1956 However, system call catching requires this function to be
1957 set. */
1958
1959 internal_error (__FILE__, __LINE__, _("fbsd_get_sycall_number called"));
1960 }
1961
1962 /* Read an integer symbol value from the current target. */
1963
1964 static LONGEST
1965 fbsd_read_integer_by_name (struct gdbarch *gdbarch, const char *name)
1966 {
1967 bound_minimal_symbol ms = lookup_minimal_symbol (name, NULL, NULL);
1968 if (ms.minsym == NULL)
1969 error (_("Unable to resolve symbol '%s'"), name);
1970
1971 gdb_byte buf[4];
1972 if (target_read_memory (BMSYMBOL_VALUE_ADDRESS (ms), buf, sizeof buf) != 0)
1973 error (_("Unable to read value of '%s'"), name);
1974
1975 return extract_signed_integer (buf, sizeof buf, gdbarch_byte_order (gdbarch));
1976 }
1977
1978 /* Lookup offsets of fields in the runtime linker's 'Obj_Entry'
1979 structure needed to determine the TLS index of an object file. */
1980
1981 static void
1982 fbsd_fetch_rtld_offsets (struct gdbarch *gdbarch, struct fbsd_pspace_data *data)
1983 {
1984 try
1985 {
1986 /* Fetch offsets from debug symbols in rtld. */
1987 struct symbol *obj_entry_sym
1988 = lookup_symbol_in_language ("Struct_Obj_Entry", NULL, STRUCT_DOMAIN,
1989 language_c, NULL).symbol;
1990 if (obj_entry_sym == NULL)
1991 error (_("Unable to find Struct_Obj_Entry symbol"));
1992 data->off_linkmap = lookup_struct_elt (SYMBOL_TYPE (obj_entry_sym),
1993 "linkmap", 0).offset / 8;
1994 data->off_tlsindex = lookup_struct_elt (SYMBOL_TYPE (obj_entry_sym),
1995 "tlsindex", 0).offset / 8;
1996 data->rtld_offsets_valid = true;
1997 return;
1998 }
1999 catch (const gdb_exception_error &e)
2000 {
2001 data->off_linkmap = -1;
2002 }
2003
2004 try
2005 {
2006 /* Fetch offsets from global variables in libthr. Note that
2007 this does not work for single-threaded processes that are not
2008 linked against libthr. */
2009 data->off_linkmap = fbsd_read_integer_by_name (gdbarch,
2010 "_thread_off_linkmap");
2011 data->off_tlsindex = fbsd_read_integer_by_name (gdbarch,
2012 "_thread_off_tlsindex");
2013 data->rtld_offsets_valid = true;
2014 return;
2015 }
2016 catch (const gdb_exception_error &e)
2017 {
2018 data->off_linkmap = -1;
2019 }
2020 }
2021
2022 /* Helper function to read the TLS index of an object file associated
2023 with a link map entry at LM_ADDR. */
2024
2025 static LONGEST
2026 fbsd_get_tls_index (struct gdbarch *gdbarch, CORE_ADDR lm_addr)
2027 {
2028 struct fbsd_pspace_data *data = get_fbsd_pspace_data (current_program_space);
2029
2030 if (!data->rtld_offsets_valid)
2031 fbsd_fetch_rtld_offsets (gdbarch, data);
2032
2033 if (data->off_linkmap == -1)
2034 throw_error (TLS_GENERIC_ERROR,
2035 _("Cannot fetch runtime linker structure offsets"));
2036
2037 /* Simulate container_of to convert from LM_ADDR to the Obj_Entry
2038 pointer and then compute the offset of the tlsindex member. */
2039 CORE_ADDR tlsindex_addr = lm_addr - data->off_linkmap + data->off_tlsindex;
2040
2041 gdb_byte buf[4];
2042 if (target_read_memory (tlsindex_addr, buf, sizeof buf) != 0)
2043 throw_error (TLS_GENERIC_ERROR,
2044 _("Cannot find thread-local variables on this target"));
2045
2046 return extract_signed_integer (buf, sizeof buf, gdbarch_byte_order (gdbarch));
2047 }
2048
2049 /* See fbsd-tdep.h. */
2050
2051 CORE_ADDR
2052 fbsd_get_thread_local_address (struct gdbarch *gdbarch, CORE_ADDR dtv_addr,
2053 CORE_ADDR lm_addr, CORE_ADDR offset)
2054 {
2055 LONGEST tls_index = fbsd_get_tls_index (gdbarch, lm_addr);
2056
2057 gdb_byte buf[gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT];
2058 if (target_read_memory (dtv_addr, buf, sizeof buf) != 0)
2059 throw_error (TLS_GENERIC_ERROR,
2060 _("Cannot find thread-local variables on this target"));
2061
2062 const struct builtin_type *builtin = builtin_type (gdbarch);
2063 CORE_ADDR addr = gdbarch_pointer_to_address (gdbarch,
2064 builtin->builtin_data_ptr, buf);
2065
2066 addr += (tls_index + 1) * TYPE_LENGTH (builtin->builtin_data_ptr);
2067 if (target_read_memory (addr, buf, sizeof buf) != 0)
2068 throw_error (TLS_GENERIC_ERROR,
2069 _("Cannot find thread-local variables on this target"));
2070
2071 addr = gdbarch_pointer_to_address (gdbarch, builtin->builtin_data_ptr, buf);
2072 return addr + offset;
2073 }
2074
2075 /* See fbsd-tdep.h. */
2076
2077 CORE_ADDR
2078 fbsd_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
2079 {
2080 struct bound_minimal_symbol msym = lookup_bound_minimal_symbol ("_rtld_bind");
2081 if (msym.minsym != nullptr && BMSYMBOL_VALUE_ADDRESS (msym) == pc)
2082 return frame_unwind_caller_pc (get_current_frame ());
2083
2084 return 0;
2085 }
2086
2087 /* To be called from GDB_OSABI_FREEBSD handlers. */
2088
2089 void
2090 fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2091 {
2092 set_gdbarch_core_pid_to_str (gdbarch, fbsd_core_pid_to_str);
2093 set_gdbarch_core_thread_name (gdbarch, fbsd_core_thread_name);
2094 set_gdbarch_core_xfer_siginfo (gdbarch, fbsd_core_xfer_siginfo);
2095 set_gdbarch_make_corefile_notes (gdbarch, fbsd_make_corefile_notes);
2096 set_gdbarch_core_info_proc (gdbarch, fbsd_core_info_proc);
2097 set_gdbarch_print_auxv_entry (gdbarch, fbsd_print_auxv_entry);
2098 set_gdbarch_get_siginfo_type (gdbarch, fbsd_get_siginfo_type);
2099 set_gdbarch_gdb_signal_from_target (gdbarch, fbsd_gdb_signal_from_target);
2100 set_gdbarch_gdb_signal_to_target (gdbarch, fbsd_gdb_signal_to_target);
2101 set_gdbarch_skip_solib_resolver (gdbarch, fbsd_skip_solib_resolver);
2102
2103 /* `catch syscall' */
2104 set_xml_syscall_file_name (gdbarch, "syscalls/freebsd.xml");
2105 set_gdbarch_get_syscall_number (gdbarch, fbsd_get_syscall_number);
2106 }
2107
2108 void _initialize_fbsd_tdep ();
2109 void
2110 _initialize_fbsd_tdep ()
2111 {
2112 fbsd_gdbarch_data_handle =
2113 gdbarch_data_register_post_init (init_fbsd_gdbarch_data);
2114 }