1 /* Target-dependent code for FreeBSD, architecture-independent.
3 Copyright (C) 2002-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
26 #include "gdbthread.h"
27 #include "xml-syscall.h"
30 #include "fbsd-tdep.h"
33 /* FreeBSD kernels 12.0 and later include a copy of the
34 'ptrace_lwpinfo' structure returned by the PT_LWPINFO ptrace
35 operation in an ELF core note (NT_FREEBSD_PTLWPINFO) for each LWP.
36 The constants below define the offset of field members and flags in
37 this structure used by methods in this file. Note that the
38 'ptrace_lwpinfo' struct in the note is preceded by a 4 byte integer
39 containing the size of the structure. */
41 #define LWPINFO_OFFSET 0x4
43 /* Offsets in ptrace_lwpinfo. */
44 #define LWPINFO_PL_FLAGS 0x8
45 #define LWPINFO64_PL_SIGINFO 0x30
46 #define LWPINFO32_PL_SIGINFO 0x2c
48 /* Flags in pl_flags. */
49 #define PL_FLAG_SI 0x20 /* siginfo is valid */
51 /* Sizes of siginfo_t. */
52 #define SIZE64_SIGINFO_T 80
53 #define SIZE32_SIGINFO_T 64
55 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
56 dump notes. See <sys/user.h> for the definition of struct
57 kinfo_vmentry. This data structure should have the same layout on
60 #define KVE_STRUCTSIZE 0x0
63 #define KVE_OFFSET 0x18
64 #define KVE_FLAGS 0x2c
65 #define KVE_PROTECTION 0x38
68 /* Flags in the 'kve_protection' field in struct kinfo_vmentry. These
69 match the KVME_PROT_* constants in <sys/user.h>. */
71 #define KINFO_VME_PROT_READ 0x00000001
72 #define KINFO_VME_PROT_WRITE 0x00000002
73 #define KINFO_VME_PROT_EXEC 0x00000004
75 /* Flags in the 'kve_flags' field in struct kinfo_vmentry. These
76 match the KVME_FLAG_* constants in <sys/user.h>. */
78 #define KINFO_VME_FLAG_COW 0x00000001
79 #define KINFO_VME_FLAG_NEEDS_COPY 0x00000002
80 #define KINFO_VME_FLAG_NOCOREDUMP 0x00000004
81 #define KINFO_VME_FLAG_SUPER 0x00000008
82 #define KINFO_VME_FLAG_GROWS_UP 0x00000010
83 #define KINFO_VME_FLAG_GROWS_DOWN 0x00000020
85 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
86 dump notes. See <sys/user.h> for the definition of struct
87 kinfo_file. This data structure should have the same layout on all
90 #define KF_STRUCTSIZE 0x0
95 /* Constants for the 'kf_type' field in struct kinfo_file. These
96 match the KF_TYPE_* constants in <sys/user.h>. */
98 #define KINFO_FILE_TYPE_VNODE 1
100 /* Special values for the 'kf_fd' field in struct kinfo_file. These
101 match the KF_FD_TYPE_* constants in <sys/user.h>. */
103 #define KINFO_FILE_FD_TYPE_CWD -1
104 #define KINFO_FILE_FD_TYPE_TEXT -5
106 /* Number of 32-bit words in a signal set. This matches _SIG_WORDS in
107 <sys/_sigset.h> and is the same value on all architectures. */
111 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_PROC core
112 dump notes. See <sys/user.h> for the definition of struct
113 kinfo_proc. This data structure has different layouts on different
114 architectures mostly due to ILP32 vs LP64. However, FreeBSD/i386
115 uses a 32-bit time_t while all other architectures use a 64-bit
118 The core dump note actually contains one kinfo_proc structure for
119 each thread, but all of the process-wide data can be obtained from
120 the first structure. One result of this note's format is that some
121 of the process-wide status available in the native target method
122 from the kern.proc.pid.<pid> sysctl such as ki_stat and ki_siglist
123 is not available from a core dump. Instead, the per-thread data
124 structures contain the value of these fields for individual
127 struct kinfo_proc_layout
129 /* Offsets of struct kinfo_proc members. */
136 int ki_tdev_freebsd11
;
158 /* Offsets of struct rusage members. */
166 const struct kinfo_proc_layout kinfo_proc_layout_32
=
174 .ki_tdev_freebsd11
= 0x44,
175 .ki_sigignore
= 0x68,
194 .ki_rusage_ch
= 0x278,
203 const struct kinfo_proc_layout kinfo_proc_layout_i386
=
211 .ki_tdev_freebsd11
= 0x44,
212 .ki_sigignore
= 0x68,
231 .ki_rusage_ch
= 0x258,
240 const struct kinfo_proc_layout kinfo_proc_layout_64
=
248 .ki_tdev_freebsd11
= 0x64,
249 .ki_sigignore
= 0x88,
268 .ki_rusage_ch
= 0x2f0,
277 static struct gdbarch_data
*fbsd_gdbarch_data_handle
;
279 struct fbsd_gdbarch_data
281 struct type
*siginfo_type
;
285 init_fbsd_gdbarch_data (struct gdbarch
*gdbarch
)
287 return GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct fbsd_gdbarch_data
);
290 static struct fbsd_gdbarch_data
*
291 get_fbsd_gdbarch_data (struct gdbarch
*gdbarch
)
293 return ((struct fbsd_gdbarch_data
*)
294 gdbarch_data (gdbarch
, fbsd_gdbarch_data_handle
));
297 /* This is how we want PTIDs from core files to be printed. */
300 fbsd_core_pid_to_str (struct gdbarch
*gdbarch
, ptid_t ptid
)
304 if (ptid
.lwp () != 0)
306 xsnprintf (buf
, sizeof buf
, "LWP %ld", ptid
.lwp ());
310 return normal_pid_to_str (ptid
);
313 /* Extract the name assigned to a thread from a core. Returns the
314 string in a static buffer. */
317 fbsd_core_thread_name (struct gdbarch
*gdbarch
, struct thread_info
*thr
)
320 struct bfd_section
*section
;
323 if (thr
->ptid
.lwp () != 0)
325 /* FreeBSD includes a NT_FREEBSD_THRMISC note for each thread
326 whose contents are defined by a "struct thrmisc" declared in
327 <sys/procfs.h> on FreeBSD. The per-thread name is stored as
328 a null-terminated string as the first member of the
329 structure. Rather than define the full structure here, just
330 extract the null-terminated name from the start of the
332 thread_section_name
section_name (".thrmisc", thr
->ptid
);
334 section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
335 if (section
!= NULL
&& bfd_section_size (core_bfd
, section
) > 0)
337 /* Truncate the name if it is longer than "buf". */
338 size
= bfd_section_size (core_bfd
, section
);
339 if (size
> sizeof buf
- 1)
340 size
= sizeof buf
- 1;
341 if (bfd_get_section_contents (core_bfd
, section
, buf
, (file_ptr
) 0,
347 /* Note that each thread will report the process command
348 as its thread name instead of an empty name if a name
349 has not been set explicitly. Return a NULL name in
351 if (strcmp (buf
, elf_tdata (core_bfd
)->core
->program
) != 0)
360 /* Implement the "core_xfer_siginfo" gdbarch method. */
363 fbsd_core_xfer_siginfo (struct gdbarch
*gdbarch
, gdb_byte
*readbuf
,
364 ULONGEST offset
, ULONGEST len
)
368 if (gdbarch_long_bit (gdbarch
) == 32)
369 siginfo_size
= SIZE32_SIGINFO_T
;
371 siginfo_size
= SIZE64_SIGINFO_T
;
372 if (offset
> siginfo_size
)
375 thread_section_name
section_name (".note.freebsdcore.lwpinfo", inferior_ptid
);
376 asection
*section
= bfd_get_section_by_name (core_bfd
, section_name
.c_str ());
381 if (!bfd_get_section_contents (core_bfd
, section
, buf
,
382 LWPINFO_OFFSET
+ LWPINFO_PL_FLAGS
, 4))
385 int pl_flags
= extract_signed_integer (buf
, 4, gdbarch_byte_order (gdbarch
));
386 if (!(pl_flags
& PL_FLAG_SI
))
389 if (offset
+ len
> siginfo_size
)
390 len
= siginfo_size
- offset
;
392 ULONGEST siginfo_offset
;
393 if (gdbarch_long_bit (gdbarch
) == 32)
394 siginfo_offset
= LWPINFO_OFFSET
+ LWPINFO32_PL_SIGINFO
;
396 siginfo_offset
= LWPINFO_OFFSET
+ LWPINFO64_PL_SIGINFO
;
398 if (!bfd_get_section_contents (core_bfd
, section
, readbuf
,
399 siginfo_offset
+ offset
, len
))
406 find_signalled_thread (struct thread_info
*info
, void *data
)
408 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
409 && info
->ptid
.pid () == inferior_ptid
.pid ())
415 /* Structure for passing information from
416 fbsd_collect_thread_registers via an iterator to
417 fbsd_collect_regset_section_cb. */
419 struct fbsd_collect_regset_section_cb_data
421 const struct regcache
*regcache
;
426 enum gdb_signal stop_signal
;
431 fbsd_collect_regset_section_cb (const char *sect_name
, int supply_size
,
432 int collect_size
, const struct regset
*regset
,
433 const char *human_name
, void *cb_data
)
436 struct fbsd_collect_regset_section_cb_data
*data
437 = (struct fbsd_collect_regset_section_cb_data
*) cb_data
;
439 if (data
->abort_iteration
)
442 gdb_assert (regset
->collect_regset
);
444 buf
= (char *) xmalloc (collect_size
);
445 regset
->collect_regset (regset
, data
->regcache
, -1, buf
, collect_size
);
447 /* PRSTATUS still needs to be treated specially. */
448 if (strcmp (sect_name
, ".reg") == 0)
449 data
->note_data
= (char *) elfcore_write_prstatus
450 (data
->obfd
, data
->note_data
, data
->note_size
, data
->lwp
,
451 gdb_signal_to_host (data
->stop_signal
), buf
);
453 data
->note_data
= (char *) elfcore_write_register_note
454 (data
->obfd
, data
->note_data
, data
->note_size
,
455 sect_name
, buf
, collect_size
);
458 if (data
->note_data
== NULL
)
459 data
->abort_iteration
= 1;
462 /* Records the thread's register state for the corefile note
466 fbsd_collect_thread_registers (const struct regcache
*regcache
,
467 ptid_t ptid
, bfd
*obfd
,
468 char *note_data
, int *note_size
,
469 enum gdb_signal stop_signal
)
471 struct gdbarch
*gdbarch
= regcache
->arch ();
472 struct fbsd_collect_regset_section_cb_data data
;
474 data
.regcache
= regcache
;
476 data
.note_data
= note_data
;
477 data
.note_size
= note_size
;
478 data
.stop_signal
= stop_signal
;
479 data
.abort_iteration
= 0;
480 data
.lwp
= ptid
.lwp ();
482 gdbarch_iterate_over_regset_sections (gdbarch
,
483 fbsd_collect_regset_section_cb
,
485 return data
.note_data
;
488 struct fbsd_corefile_thread_data
490 struct gdbarch
*gdbarch
;
494 enum gdb_signal stop_signal
;
497 /* Records the thread's register state for the corefile note
501 fbsd_corefile_thread (struct thread_info
*info
,
502 struct fbsd_corefile_thread_data
*args
)
504 struct regcache
*regcache
;
506 regcache
= get_thread_arch_regcache (info
->ptid
, args
->gdbarch
);
508 target_fetch_registers (regcache
, -1);
510 args
->note_data
= fbsd_collect_thread_registers
511 (regcache
, info
->ptid
, args
->obfd
, args
->note_data
,
512 args
->note_size
, args
->stop_signal
);
515 /* Return a byte_vector containing the contents of a core dump note
516 for the target object of type OBJECT. If STRUCTSIZE is non-zero,
517 the data is prefixed with a 32-bit integer size to match the format
518 used in FreeBSD NT_PROCSTAT_* notes. */
520 static gdb::optional
<gdb::byte_vector
>
521 fbsd_make_note_desc (enum target_object object
, uint32_t structsize
)
523 gdb::optional
<gdb::byte_vector
> buf
=
524 target_read_alloc (current_top_target (), object
, NULL
);
525 if (!buf
|| buf
->empty ())
531 gdb::byte_vector
desc (sizeof (structsize
) + buf
->size ());
532 memcpy (desc
.data (), &structsize
, sizeof (structsize
));
533 memcpy (desc
.data () + sizeof (structsize
), buf
->data (), buf
->size ());
537 /* Create appropriate note sections for a corefile, returning them in
541 fbsd_make_corefile_notes (struct gdbarch
*gdbarch
, bfd
*obfd
, int *note_size
)
543 struct fbsd_corefile_thread_data thread_args
;
544 char *note_data
= NULL
;
545 Elf_Internal_Ehdr
*i_ehdrp
;
546 struct thread_info
*curr_thr
, *signalled_thr
, *thr
;
548 /* Put a "FreeBSD" label in the ELF header. */
549 i_ehdrp
= elf_elfheader (obfd
);
550 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
552 gdb_assert (gdbarch_iterate_over_regset_sections_p (gdbarch
));
554 if (get_exec_file (0))
556 const char *fname
= lbasename (get_exec_file (0));
557 char *psargs
= xstrdup (fname
);
559 if (get_inferior_args ())
560 psargs
= reconcat (psargs
, psargs
, " ", get_inferior_args (),
563 note_data
= elfcore_write_prpsinfo (obfd
, note_data
, note_size
,
567 /* Thread register information. */
570 update_thread_list ();
572 CATCH (e
, RETURN_MASK_ERROR
)
574 exception_print (gdb_stderr
, e
);
578 /* Like the kernel, prefer dumping the signalled thread first.
579 "First thread" is what tools use to infer the signalled thread.
580 In case there's more than one signalled thread, prefer the
581 current thread, if it is signalled. */
582 curr_thr
= inferior_thread ();
583 if (curr_thr
->suspend
.stop_signal
!= GDB_SIGNAL_0
)
584 signalled_thr
= curr_thr
;
587 signalled_thr
= iterate_over_threads (find_signalled_thread
, NULL
);
588 if (signalled_thr
== NULL
)
589 signalled_thr
= curr_thr
;
592 thread_args
.gdbarch
= gdbarch
;
593 thread_args
.obfd
= obfd
;
594 thread_args
.note_data
= note_data
;
595 thread_args
.note_size
= note_size
;
596 thread_args
.stop_signal
= signalled_thr
->suspend
.stop_signal
;
598 fbsd_corefile_thread (signalled_thr
, &thread_args
);
599 ALL_NON_EXITED_THREADS (thr
)
601 if (thr
== signalled_thr
)
603 if (thr
->ptid
.pid () != inferior_ptid
.pid ())
606 fbsd_corefile_thread (thr
, &thread_args
);
609 note_data
= thread_args
.note_data
;
611 /* Auxiliary vector. */
612 uint32_t structsize
= gdbarch_ptr_bit (gdbarch
) / 4; /* Elf_Auxinfo */
613 gdb::optional
<gdb::byte_vector
> note_desc
=
614 fbsd_make_note_desc (TARGET_OBJECT_AUXV
, structsize
);
615 if (note_desc
&& !note_desc
->empty ())
617 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
618 NT_FREEBSD_PROCSTAT_AUXV
,
619 note_desc
->data (), note_desc
->size ());
624 /* Virtual memory mappings. */
625 note_desc
= fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_VMMAP
, 0);
626 if (note_desc
&& !note_desc
->empty ())
628 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
629 NT_FREEBSD_PROCSTAT_VMMAP
,
630 note_desc
->data (), note_desc
->size ());
635 note_desc
= fbsd_make_note_desc (TARGET_OBJECT_FREEBSD_PS_STRINGS
, 0);
636 if (note_desc
&& !note_desc
->empty ())
638 note_data
= elfcore_write_note (obfd
, note_data
, note_size
, "FreeBSD",
639 NT_FREEBSD_PROCSTAT_PSSTRINGS
,
640 note_desc
->data (), note_desc
->size ());
648 /* Helper function to generate mappings flags for a single VM map
649 entry in 'info proc mappings'. */
652 fbsd_vm_map_entry_flags (int kve_flags
, int kve_protection
)
654 static char vm_flags
[9];
656 vm_flags
[0] = (kve_protection
& KINFO_VME_PROT_READ
) ? 'r' : '-';
657 vm_flags
[1] = (kve_protection
& KINFO_VME_PROT_WRITE
) ? 'w' : '-';
658 vm_flags
[2] = (kve_protection
& KINFO_VME_PROT_EXEC
) ? 'x' : '-';
660 vm_flags
[4] = (kve_flags
& KINFO_VME_FLAG_COW
) ? 'C' : '-';
661 vm_flags
[5] = (kve_flags
& KINFO_VME_FLAG_NEEDS_COPY
) ? 'N' : '-';
662 vm_flags
[6] = (kve_flags
& KINFO_VME_FLAG_SUPER
) ? 'S' : '-';
663 vm_flags
[7] = (kve_flags
& KINFO_VME_FLAG_GROWS_UP
) ? 'U'
664 : (kve_flags
& KINFO_VME_FLAG_GROWS_DOWN
) ? 'D' : '-';
670 /* Implement "info proc mappings" for a corefile. */
673 fbsd_core_info_proc_mappings (struct gdbarch
*gdbarch
)
676 unsigned char *descdata
, *descend
;
679 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.vmmap");
682 warning (_("unable to find mappings in core file"));
686 note_size
= bfd_get_section_size (section
);
688 error (_("malformed core note - too short for header"));
690 gdb::def_vector
<unsigned char> contents (note_size
);
691 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
693 error (_("could not get core note contents"));
695 descdata
= contents
.data ();
696 descend
= descdata
+ note_size
;
698 /* Skip over the structure size. */
701 printf_filtered (_("Mapped address spaces:\n\n"));
702 if (gdbarch_addr_bit (gdbarch
) == 64)
704 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
707 " Size", " Offset", "Flags ", "File");
711 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
714 " Size", " Offset", "Flags ", "File");
717 while (descdata
+ KVE_PATH
< descend
)
719 ULONGEST start
, end
, offset
, flags
, prot
, structsize
;
721 structsize
= bfd_get_32 (core_bfd
, descdata
+ KVE_STRUCTSIZE
);
722 if (structsize
< KVE_PATH
)
723 error (_("malformed core note - vmmap entry too small"));
725 start
= bfd_get_64 (core_bfd
, descdata
+ KVE_START
);
726 end
= bfd_get_64 (core_bfd
, descdata
+ KVE_END
);
727 offset
= bfd_get_64 (core_bfd
, descdata
+ KVE_OFFSET
);
728 flags
= bfd_get_32 (core_bfd
, descdata
+ KVE_FLAGS
);
729 prot
= bfd_get_32 (core_bfd
, descdata
+ KVE_PROTECTION
);
730 if (gdbarch_addr_bit (gdbarch
) == 64)
732 printf_filtered (" %18s %18s %10s %10s %9s %s\n",
733 paddress (gdbarch
, start
),
734 paddress (gdbarch
, end
),
735 hex_string (end
- start
),
737 fbsd_vm_map_entry_flags (flags
, prot
),
738 descdata
+ KVE_PATH
);
742 printf_filtered ("\t%10s %10s %10s %10s %9s %s\n",
743 paddress (gdbarch
, start
),
744 paddress (gdbarch
, end
),
745 hex_string (end
- start
),
747 fbsd_vm_map_entry_flags (flags
, prot
),
748 descdata
+ KVE_PATH
);
751 descdata
+= structsize
;
755 /* Fetch the pathname of a vnode for a single file descriptor from the
756 file table core note. */
758 static gdb::unique_xmalloc_ptr
<char>
759 fbsd_core_vnode_path (struct gdbarch
*gdbarch
, int fd
)
762 unsigned char *descdata
, *descend
;
765 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.files");
769 note_size
= bfd_get_section_size (section
);
771 error (_("malformed core note - too short for header"));
773 gdb::def_vector
<unsigned char> contents (note_size
);
774 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
776 error (_("could not get core note contents"));
778 descdata
= contents
.data ();
779 descend
= descdata
+ note_size
;
781 /* Skip over the structure size. */
784 while (descdata
+ KVE_PATH
< descend
)
788 structsize
= bfd_get_32 (core_bfd
, descdata
+ KF_STRUCTSIZE
);
789 if (structsize
< KVE_PATH
)
790 error (_("malformed core note - vmmap entry too small"));
792 if (bfd_get_32 (core_bfd
, descdata
+ KF_TYPE
) == KINFO_FILE_TYPE_VNODE
793 && bfd_get_signed_32 (core_bfd
, descdata
+ KF_FD
) == fd
)
795 char *path
= (char *) descdata
+ KF_PATH
;
796 return gdb::unique_xmalloc_ptr
<char> (xstrdup (path
));
799 descdata
+= structsize
;
804 /* Helper function to read a struct timeval. */
807 fbsd_core_fetch_timeval (struct gdbarch
*gdbarch
, unsigned char *data
,
808 LONGEST
&sec
, ULONGEST
&usec
)
810 if (gdbarch_addr_bit (gdbarch
) == 64)
812 sec
= bfd_get_signed_64 (core_bfd
, data
);
813 usec
= bfd_get_64 (core_bfd
, data
+ 8);
815 else if (bfd_get_arch (core_bfd
) == bfd_arch_i386
)
817 sec
= bfd_get_signed_32 (core_bfd
, data
);
818 usec
= bfd_get_32 (core_bfd
, data
+ 4);
822 sec
= bfd_get_signed_64 (core_bfd
, data
);
823 usec
= bfd_get_32 (core_bfd
, data
+ 8);
827 /* Print out the contents of a signal set. */
830 fbsd_print_sigset (const char *descr
, unsigned char *sigset
)
832 printf_filtered ("%s: ", descr
);
833 for (int i
= 0; i
< SIG_WORDS
; i
++)
834 printf_filtered ("%08x ",
835 (unsigned int) bfd_get_32 (core_bfd
, sigset
+ i
* 4));
836 printf_filtered ("\n");
839 /* Implement "info proc status" for a corefile. */
842 fbsd_core_info_proc_status (struct gdbarch
*gdbarch
)
844 const struct kinfo_proc_layout
*kp
;
846 unsigned char *descdata
;
847 int addr_bit
, long_bit
;
852 section
= bfd_get_section_by_name (core_bfd
, ".note.freebsdcore.proc");
855 warning (_("unable to find process info in core file"));
859 addr_bit
= gdbarch_addr_bit (gdbarch
);
861 kp
= &kinfo_proc_layout_64
;
862 else if (bfd_get_arch (core_bfd
) == bfd_arch_i386
)
863 kp
= &kinfo_proc_layout_i386
;
865 kp
= &kinfo_proc_layout_32
;
866 long_bit
= gdbarch_long_bit (gdbarch
);
869 * Ensure that the note is large enough for all of the fields fetched
870 * by this function. In particular, the note must contain the 32-bit
871 * structure size, then it must be long enough to access the last
872 * field used (ki_rusage_ch.ru_majflt) which is the size of a long.
874 note_size
= bfd_get_section_size (section
);
875 if (note_size
< (4 + kp
->ki_rusage_ch
+ kp
->ru_majflt
876 + long_bit
/ TARGET_CHAR_BIT
))
877 error (_("malformed core note - too short"));
879 gdb::def_vector
<unsigned char> contents (note_size
);
880 if (!bfd_get_section_contents (core_bfd
, section
, contents
.data (),
882 error (_("could not get core note contents"));
884 descdata
= contents
.data ();
886 /* Skip over the structure size. */
889 /* Verify 'ki_layout' is 0. */
890 if (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_layout
) != 0)
892 warning (_("unsupported process information in core file"));
896 printf_filtered ("Name: %.19s\n", descdata
+ kp
->ki_comm
);
897 printf_filtered ("Process ID: %s\n",
898 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_pid
)));
899 printf_filtered ("Parent process: %s\n",
900 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_ppid
)));
901 printf_filtered ("Process group: %s\n",
902 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_pgid
)));
903 printf_filtered ("Session id: %s\n",
904 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_sid
)));
906 /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'. Older
907 kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'. In older
908 kernels the 64-bit 'ki_tdev' field is in a reserved section of
909 the structure that is cleared to zero. Assume that a zero value
910 in ki_tdev indicates a core dump from an older kernel and use the
911 value in 'ki_tdev_freebsd11' instead. */
912 value
= bfd_get_64 (core_bfd
, descdata
+ kp
->ki_tdev
);
914 value
= bfd_get_32 (core_bfd
, descdata
+ kp
->ki_tdev_freebsd11
);
915 printf_filtered ("TTY: %s\n", pulongest (value
));
916 printf_filtered ("TTY owner process group: %s\n",
917 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_tpgid
)));
918 printf_filtered ("User IDs (real, effective, saved): %s %s %s\n",
919 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_ruid
)),
920 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_uid
)),
921 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_svuid
)));
922 printf_filtered ("Group IDs (real, effective, saved): %s %s %s\n",
923 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_rgid
)),
924 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_groups
)),
925 pulongest (bfd_get_32 (core_bfd
, descdata
+ kp
->ki_svgid
)));
926 printf_filtered ("Groups: ");
927 uint16_t ngroups
= bfd_get_16 (core_bfd
, descdata
+ kp
->ki_ngroups
);
928 for (int i
= 0; i
< ngroups
; i
++)
929 printf_filtered ("%s ",
930 pulongest (bfd_get_32 (core_bfd
,
931 descdata
+ kp
->ki_groups
+ i
* 4)));
932 printf_filtered ("\n");
933 value
= bfd_get (long_bit
, core_bfd
,
934 descdata
+ kp
->ki_rusage
+ kp
->ru_minflt
);
935 printf_filtered ("Minor faults (no memory page): %s\n", pulongest (value
));
936 value
= bfd_get (long_bit
, core_bfd
,
937 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_minflt
);
938 printf_filtered ("Minor faults, children: %s\n", pulongest (value
));
939 value
= bfd_get (long_bit
, core_bfd
,
940 descdata
+ kp
->ki_rusage
+ kp
->ru_majflt
);
941 printf_filtered ("Major faults (memory page faults): %s\n",
943 value
= bfd_get (long_bit
, core_bfd
,
944 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_majflt
);
945 printf_filtered ("Major faults, children: %s\n", pulongest (value
));
946 fbsd_core_fetch_timeval (gdbarch
,
947 descdata
+ kp
->ki_rusage
+ kp
->ru_utime
,
949 printf_filtered ("utime: %s.%06d\n", plongest (sec
), (int) value
);
950 fbsd_core_fetch_timeval (gdbarch
,
951 descdata
+ kp
->ki_rusage
+ kp
->ru_stime
,
953 printf_filtered ("stime: %s.%06d\n", plongest (sec
), (int) value
);
954 fbsd_core_fetch_timeval (gdbarch
,
955 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_utime
,
957 printf_filtered ("utime, children: %s.%06d\n", plongest (sec
), (int) value
);
958 fbsd_core_fetch_timeval (gdbarch
,
959 descdata
+ kp
->ki_rusage_ch
+ kp
->ru_stime
,
961 printf_filtered ("stime, children: %s.%06d\n", plongest (sec
), (int) value
);
962 printf_filtered ("'nice' value: %d\n",
963 bfd_get_signed_8 (core_bfd
, descdata
+ kp
->ki_nice
));
964 fbsd_core_fetch_timeval (gdbarch
, descdata
+ kp
->ki_start
, sec
, value
);
965 printf_filtered ("Start time: %s.%06d\n", plongest (sec
), (int) value
);
966 printf_filtered ("Virtual memory size: %s kB\n",
967 pulongest (bfd_get (addr_bit
, core_bfd
,
968 descdata
+ kp
->ki_size
) / 1024));
969 printf_filtered ("Data size: %s pages\n",
970 pulongest (bfd_get (addr_bit
, core_bfd
,
971 descdata
+ kp
->ki_dsize
)));
972 printf_filtered ("Stack size: %s pages\n",
973 pulongest (bfd_get (addr_bit
, core_bfd
,
974 descdata
+ kp
->ki_ssize
)));
975 printf_filtered ("Text size: %s pages\n",
976 pulongest (bfd_get (addr_bit
, core_bfd
,
977 descdata
+ kp
->ki_tsize
)));
978 printf_filtered ("Resident set size: %s pages\n",
979 pulongest (bfd_get (addr_bit
, core_bfd
,
980 descdata
+ kp
->ki_rssize
)));
981 printf_filtered ("Maximum RSS: %s pages\n",
982 pulongest (bfd_get (long_bit
, core_bfd
,
983 descdata
+ kp
->ki_rusage
985 fbsd_print_sigset ("Ignored Signals", descdata
+ kp
->ki_sigignore
);
986 fbsd_print_sigset ("Caught Signals", descdata
+ kp
->ki_sigcatch
);
989 /* Implement the "core_info_proc" gdbarch method. */
992 fbsd_core_info_proc (struct gdbarch
*gdbarch
, const char *args
,
993 enum info_proc_what what
)
995 bool do_cmdline
= false;
998 bool do_mappings
= false;
999 bool do_status
= false;
1036 pid
= bfd_core_file_pid (core_bfd
);
1038 printf_filtered (_("process %d\n"), pid
);
1042 const char *cmdline
;
1044 cmdline
= bfd_core_file_failing_command (core_bfd
);
1046 printf_filtered ("cmdline = '%s'\n", cmdline
);
1048 warning (_("Command line unavailable"));
1052 gdb::unique_xmalloc_ptr
<char> cwd
=
1053 fbsd_core_vnode_path (gdbarch
, KINFO_FILE_FD_TYPE_CWD
);
1055 printf_filtered ("cwd = '%s'\n", cwd
.get ());
1057 warning (_("unable to read current working directory"));
1061 gdb::unique_xmalloc_ptr
<char> exe
=
1062 fbsd_core_vnode_path (gdbarch
, KINFO_FILE_FD_TYPE_TEXT
);
1064 printf_filtered ("exe = '%s'\n", exe
.get ());
1066 warning (_("unable to read executable path name"));
1069 fbsd_core_info_proc_mappings (gdbarch
);
1071 fbsd_core_info_proc_status (gdbarch
);
1074 /* Print descriptions of FreeBSD-specific AUXV entries to FILE. */
1077 fbsd_print_auxv_entry (struct gdbarch
*gdbarch
, struct ui_file
*file
,
1078 CORE_ADDR type
, CORE_ADDR val
)
1081 const char *description
;
1082 enum auxv_format format
;
1086 #define _TAGNAME(tag) #tag
1087 #define TAGNAME(tag) _TAGNAME(AT_##tag)
1088 #define TAG(tag, text, kind) \
1089 case AT_FREEBSD_##tag: name = TAGNAME(tag); description = text; format = kind; break
1090 TAG (EXECPATH
, _("Executable path"), AUXV_FORMAT_STR
);
1091 TAG (CANARY
, _("Canary for SSP"), AUXV_FORMAT_HEX
);
1092 TAG (CANARYLEN
, ("Length of the SSP canary"), AUXV_FORMAT_DEC
);
1093 TAG (OSRELDATE
, _("OSRELDATE"), AUXV_FORMAT_DEC
);
1094 TAG (NCPUS
, _("Number of CPUs"), AUXV_FORMAT_DEC
);
1095 TAG (PAGESIZES
, _("Pagesizes"), AUXV_FORMAT_HEX
);
1096 TAG (PAGESIZESLEN
, _("Number of pagesizes"), AUXV_FORMAT_DEC
);
1097 TAG (TIMEKEEP
, _("Pointer to timehands"), AUXV_FORMAT_HEX
);
1098 TAG (STACKPROT
, _("Initial stack protection"), AUXV_FORMAT_HEX
);
1099 TAG (EHDRFLAGS
, _("ELF header e_flags"), AUXV_FORMAT_HEX
);
1100 TAG (HWCAP
, _("Machine-dependent CPU capability hints"), AUXV_FORMAT_HEX
);
1102 default_print_auxv_entry (gdbarch
, file
, type
, val
);
1106 fprint_auxv_entry (file
, name
, description
, format
, type
, val
);
1109 /* Implement the "get_siginfo_type" gdbarch method. */
1111 static struct type
*
1112 fbsd_get_siginfo_type (struct gdbarch
*gdbarch
)
1114 struct fbsd_gdbarch_data
*fbsd_gdbarch_data
;
1115 struct type
*int_type
, *int32_type
, *uint32_type
, *long_type
, *void_ptr_type
;
1116 struct type
*uid_type
, *pid_type
;
1117 struct type
*sigval_type
, *reason_type
;
1118 struct type
*siginfo_type
;
1121 fbsd_gdbarch_data
= get_fbsd_gdbarch_data (gdbarch
);
1122 if (fbsd_gdbarch_data
->siginfo_type
!= NULL
)
1123 return fbsd_gdbarch_data
->siginfo_type
;
1125 int_type
= arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
),
1127 int32_type
= arch_integer_type (gdbarch
, 32, 0, "int32_t");
1128 uint32_type
= arch_integer_type (gdbarch
, 32, 1, "uint32_t");
1129 long_type
= arch_integer_type (gdbarch
, gdbarch_long_bit (gdbarch
),
1131 void_ptr_type
= lookup_pointer_type (builtin_type (gdbarch
)->builtin_void
);
1134 sigval_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
1135 TYPE_NAME (sigval_type
) = xstrdup ("sigval");
1136 append_composite_type_field (sigval_type
, "sival_int", int_type
);
1137 append_composite_type_field (sigval_type
, "sival_ptr", void_ptr_type
);
1140 pid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
1141 TYPE_LENGTH (int32_type
) * TARGET_CHAR_BIT
, "__pid_t");
1142 TYPE_TARGET_TYPE (pid_type
) = int32_type
;
1143 TYPE_TARGET_STUB (pid_type
) = 1;
1146 uid_type
= arch_type (gdbarch
, TYPE_CODE_TYPEDEF
,
1147 TYPE_LENGTH (uint32_type
) * TARGET_CHAR_BIT
,
1149 TYPE_TARGET_TYPE (uid_type
) = uint32_type
;
1150 TYPE_TARGET_STUB (uid_type
) = 1;
1153 reason_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_UNION
);
1156 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1157 append_composite_type_field (type
, "si_trapno", int_type
);
1158 append_composite_type_field (reason_type
, "_fault", type
);
1161 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1162 append_composite_type_field (type
, "si_timerid", int_type
);
1163 append_composite_type_field (type
, "si_overrun", int_type
);
1164 append_composite_type_field (reason_type
, "_timer", type
);
1167 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1168 append_composite_type_field (type
, "si_mqd", int_type
);
1169 append_composite_type_field (reason_type
, "_mesgq", type
);
1172 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1173 append_composite_type_field (type
, "si_band", long_type
);
1174 append_composite_type_field (reason_type
, "_poll", type
);
1177 type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1178 append_composite_type_field (type
, "__spare1__", long_type
);
1179 append_composite_type_field (type
, "__spare2__",
1180 init_vector_type (int_type
, 7));
1181 append_composite_type_field (reason_type
, "__spare__", type
);
1183 /* struct siginfo */
1184 siginfo_type
= arch_composite_type (gdbarch
, NULL
, TYPE_CODE_STRUCT
);
1185 TYPE_NAME (siginfo_type
) = xstrdup ("siginfo");
1186 append_composite_type_field (siginfo_type
, "si_signo", int_type
);
1187 append_composite_type_field (siginfo_type
, "si_errno", int_type
);
1188 append_composite_type_field (siginfo_type
, "si_code", int_type
);
1189 append_composite_type_field (siginfo_type
, "si_pid", pid_type
);
1190 append_composite_type_field (siginfo_type
, "si_uid", uid_type
);
1191 append_composite_type_field (siginfo_type
, "si_status", int_type
);
1192 append_composite_type_field (siginfo_type
, "si_addr", void_ptr_type
);
1193 append_composite_type_field (siginfo_type
, "si_value", sigval_type
);
1194 append_composite_type_field (siginfo_type
, "_reason", reason_type
);
1196 fbsd_gdbarch_data
->siginfo_type
= siginfo_type
;
1198 return siginfo_type
;
1201 /* Implement the "get_syscall_number" gdbarch method. */
1204 fbsd_get_syscall_number (struct gdbarch
*gdbarch
, thread_info
*thread
)
1207 /* FreeBSD doesn't use gdbarch_get_syscall_number since FreeBSD
1208 native targets fetch the system call number from the
1209 'pl_syscall_code' member of struct ptrace_lwpinfo in fbsd_wait.
1210 However, system call catching requires this function to be
1213 internal_error (__FILE__
, __LINE__
, _("fbsd_get_sycall_number called"));
1216 /* To be called from GDB_OSABI_FREEBSD handlers. */
1219 fbsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1221 set_gdbarch_core_pid_to_str (gdbarch
, fbsd_core_pid_to_str
);
1222 set_gdbarch_core_thread_name (gdbarch
, fbsd_core_thread_name
);
1223 set_gdbarch_core_xfer_siginfo (gdbarch
, fbsd_core_xfer_siginfo
);
1224 set_gdbarch_make_corefile_notes (gdbarch
, fbsd_make_corefile_notes
);
1225 set_gdbarch_core_info_proc (gdbarch
, fbsd_core_info_proc
);
1226 set_gdbarch_print_auxv_entry (gdbarch
, fbsd_print_auxv_entry
);
1227 set_gdbarch_get_siginfo_type (gdbarch
, fbsd_get_siginfo_type
);
1229 /* `catch syscall' */
1230 set_xml_syscall_file_name (gdbarch
, "syscalls/freebsd.xml");
1231 set_gdbarch_get_syscall_number (gdbarch
, fbsd_get_syscall_number
);
1235 _initialize_fbsd_tdep (void)
1237 fbsd_gdbarch_data_handle
=
1238 gdbarch_data_register_post_init (init_fbsd_gdbarch_data
);