+/* Helper function to generate the file descriptor description for a
+ single open file in 'info proc files'. */
+
+static const char *
+fbsd_file_fd (int kf_fd)
+{
+ switch (kf_fd)
+ {
+ case KINFO_FILE_FD_TYPE_CWD:
+ return "cwd";
+ case KINFO_FILE_FD_TYPE_ROOT:
+ return "root";
+ case KINFO_FILE_FD_TYPE_JAIL:
+ return "jail";
+ case KINFO_FILE_FD_TYPE_TRACE:
+ return "trace";
+ case KINFO_FILE_FD_TYPE_TEXT:
+ return "text";
+ case KINFO_FILE_FD_TYPE_CTTY:
+ return "ctty";
+ default:
+ return int_string (kf_fd, 10, 1, 0, 0);
+ }
+}
+
+/* Helper function to generate the file type for a single open file in
+ 'info proc files'. */
+
+static const char *
+fbsd_file_type (int kf_type, int kf_vnode_type)
+{
+ switch (kf_type)
+ {
+ case KINFO_FILE_TYPE_VNODE:
+ switch (kf_vnode_type)
+ {
+ case KINFO_FILE_VTYPE_VREG:
+ return "file";
+ case KINFO_FILE_VTYPE_VDIR:
+ return "dir";
+ case KINFO_FILE_VTYPE_VCHR:
+ return "chr";
+ case KINFO_FILE_VTYPE_VLNK:
+ return "link";
+ case KINFO_FILE_VTYPE_VSOCK:
+ return "socket";
+ case KINFO_FILE_VTYPE_VFIFO:
+ return "fifo";
+ default:
+ {
+ char *str = get_print_cell ();
+
+ xsnprintf (str, PRINT_CELL_SIZE, "vn:%d", kf_vnode_type);
+ return str;
+ }
+ }
+ case KINFO_FILE_TYPE_SOCKET:
+ return "socket";
+ case KINFO_FILE_TYPE_PIPE:
+ return "pipe";
+ case KINFO_FILE_TYPE_FIFO:
+ return "fifo";
+ case KINFO_FILE_TYPE_KQUEUE:
+ return "kqueue";
+ case KINFO_FILE_TYPE_CRYPTO:
+ return "crypto";
+ case KINFO_FILE_TYPE_MQUEUE:
+ return "mqueue";
+ case KINFO_FILE_TYPE_SHM:
+ return "shm";
+ case KINFO_FILE_TYPE_SEM:
+ return "sem";
+ case KINFO_FILE_TYPE_PTS:
+ return "pts";
+ case KINFO_FILE_TYPE_PROCDESC:
+ return "proc";
+ default:
+ return int_string (kf_type, 10, 1, 0, 0);
+ }
+}
+
+/* Helper function to generate the file flags for a single open file in
+ 'info proc files'. */
+
+static const char *
+fbsd_file_flags (int kf_flags)
+{
+ static char file_flags[10];
+
+ file_flags[0] = (kf_flags & KINFO_FILE_FLAG_READ) ? 'r' : '-';
+ file_flags[1] = (kf_flags & KINFO_FILE_FLAG_WRITE) ? 'w' : '-';
+ file_flags[2] = (kf_flags & KINFO_FILE_FLAG_EXEC) ? 'x' : '-';
+ file_flags[3] = (kf_flags & KINFO_FILE_FLAG_APPEND) ? 'a' : '-';
+ file_flags[4] = (kf_flags & KINFO_FILE_FLAG_ASYNC) ? 's' : '-';
+ file_flags[5] = (kf_flags & KINFO_FILE_FLAG_FSYNC) ? 'f' : '-';
+ file_flags[6] = (kf_flags & KINFO_FILE_FLAG_NONBLOCK) ? 'n' : '-';
+ file_flags[7] = (kf_flags & KINFO_FILE_FLAG_DIRECT) ? 'd' : '-';
+ file_flags[8] = (kf_flags & KINFO_FILE_FLAG_HASLOCK) ? 'l' : '-';
+ file_flags[9] = '\0';
+
+ return file_flags;
+}
+
+/* Helper function to generate the name of an IP protocol. */
+
+static const char *
+fbsd_ipproto (int protocol)
+{
+ switch (protocol)
+ {
+ case FBSD_IPPROTO_ICMP:
+ return "icmp";
+ case FBSD_IPPROTO_TCP:
+ return "tcp";
+ case FBSD_IPPROTO_UDP:
+ return "udp";
+ case FBSD_IPPROTO_SCTP:
+ return "sctp";
+ default:
+ {
+ char *str = get_print_cell ();
+
+ xsnprintf (str, PRINT_CELL_SIZE, "ip<%d>", protocol);
+ return str;
+ }
+ }
+}
+
+/* Helper function to print out an IPv4 socket address. */
+
+static void
+fbsd_print_sockaddr_in (const void *sockaddr)
+{
+ const struct fbsd_sockaddr_in *sin =
+ reinterpret_cast<const struct fbsd_sockaddr_in *> (sockaddr);
+ char buf[INET_ADDRSTRLEN];
+
+ if (inet_ntop (AF_INET, sin->sin_addr, buf, sizeof buf) == nullptr)
+ error (_("Failed to format IPv4 address"));
+ printf_filtered ("%s:%u", buf,
+ (sin->sin_port[0] << 8) | sin->sin_port[1]);
+}
+
+/* Helper function to print out an IPv6 socket address. */
+
+static void
+fbsd_print_sockaddr_in6 (const void *sockaddr)
+{
+ const struct fbsd_sockaddr_in6 *sin6 =
+ reinterpret_cast<const struct fbsd_sockaddr_in6 *> (sockaddr);
+ char buf[INET6_ADDRSTRLEN];
+
+ if (inet_ntop (AF_INET6, sin6->sin6_addr, buf, sizeof buf) == nullptr)
+ error (_("Failed to format IPv6 address"));
+ printf_filtered ("%s.%u", buf,
+ (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
+}
+
+/* See fbsd-tdep.h. */
+
+void
+fbsd_info_proc_files_header ()
+{
+ printf_filtered (_("Open files:\n\n"));
+ printf_filtered (" %6s %6s %10s %9s %s\n",
+ "FD", "Type", "Offset", "Flags ", "Name");
+}
+
+/* See fbsd-tdep.h. */
+
+void
+fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
+ LONGEST kf_offset, int kf_vnode_type,
+ int kf_sock_domain, int kf_sock_type,
+ int kf_sock_protocol, const void *kf_sa_local,
+ const void *kf_sa_peer, const void *kf_path)
+{
+ printf_filtered (" %6s %6s %10s %8s ",
+ fbsd_file_fd (kf_fd),
+ fbsd_file_type (kf_type, kf_vnode_type),
+ kf_offset > -1 ? hex_string (kf_offset) : "-",
+ fbsd_file_flags (kf_flags));
+ if (kf_type == KINFO_FILE_TYPE_SOCKET)
+ {
+ switch (kf_sock_domain)
+ {
+ case FBSD_AF_UNIX:
+ {
+ switch (kf_sock_type)
+ {
+ case FBSD_SOCK_STREAM:
+ printf_filtered ("unix stream:");
+ break;
+ case FBSD_SOCK_DGRAM:
+ printf_filtered ("unix dgram:");
+ break;
+ case FBSD_SOCK_SEQPACKET:
+ printf_filtered ("unix seqpacket:");
+ break;
+ default:
+ printf_filtered ("unix <%d>:", kf_sock_type);
+ break;
+ }
+
+ /* For local sockets, print out the first non-nul path
+ rather than both paths. */
+ const struct fbsd_sockaddr_un *sun
+ = reinterpret_cast<const struct fbsd_sockaddr_un *> (kf_sa_local);
+ if (sun->sun_path[0] == 0)
+ sun = reinterpret_cast<const struct fbsd_sockaddr_un *>
+ (kf_sa_peer);
+ printf_filtered ("%s", sun->sun_path);
+ break;
+ }
+ case FBSD_AF_INET:
+ printf_filtered ("%s4 ", fbsd_ipproto (kf_sock_protocol));
+ fbsd_print_sockaddr_in (kf_sa_local);
+ printf_filtered (" -> ");
+ fbsd_print_sockaddr_in (kf_sa_peer);
+ break;
+ case FBSD_AF_INET6:
+ printf_filtered ("%s6 ", fbsd_ipproto (kf_sock_protocol));
+ fbsd_print_sockaddr_in6 (kf_sa_local);
+ printf_filtered (" -> ");
+ fbsd_print_sockaddr_in6 (kf_sa_peer);
+ break;
+ }
+ }
+ else
+ printf_filtered ("%s", reinterpret_cast<const char *> (kf_path));
+ printf_filtered ("\n");
+}
+
+/* Implement "info proc files" for a corefile. */
+
+static void
+fbsd_core_info_proc_files (struct gdbarch *gdbarch)
+{
+ asection *section
+ = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
+ if (section == NULL)
+ {
+ warning (_("unable to find open files in core file"));
+ return;
+ }
+
+ size_t note_size = bfd_get_section_size (section);
+ if (note_size < 4)
+ error (_("malformed core note - too short for header"));
+
+ gdb::def_vector<unsigned char> contents (note_size);
+ if (!bfd_get_section_contents (core_bfd, section, contents.data (),
+ 0, note_size))
+ error (_("could not get core note contents"));
+
+ unsigned char *descdata = contents.data ();
+ unsigned char *descend = descdata + note_size;
+
+ /* Skip over the structure size. */
+ descdata += 4;
+
+ fbsd_info_proc_files_header ();
+
+ while (descdata + KF_PATH < descend)
+ {
+ ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
+ if (structsize < KF_PATH)
+ error (_("malformed core note - file structure too small"));
+
+ LONGEST type = bfd_get_signed_32 (core_bfd, descdata + KF_TYPE);
+ LONGEST fd = bfd_get_signed_32 (core_bfd, descdata + KF_FD);
+ LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KF_FLAGS);
+ LONGEST offset = bfd_get_signed_64 (core_bfd, descdata + KF_OFFSET);
+ LONGEST vnode_type = bfd_get_signed_32 (core_bfd,
+ descdata + KF_VNODE_TYPE);
+ LONGEST sock_domain = bfd_get_signed_32 (core_bfd,
+ descdata + KF_SOCK_DOMAIN);
+ LONGEST sock_type = bfd_get_signed_32 (core_bfd, descdata + KF_SOCK_TYPE);
+ LONGEST sock_protocol = bfd_get_signed_32 (core_bfd,
+ descdata + KF_SOCK_PROTOCOL);
+ fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
+ sock_domain, sock_type, sock_protocol,
+ descdata + KF_SA_LOCAL, descdata + KF_SA_PEER,
+ descdata + KF_PATH);
+
+ descdata += structsize;
+ }
+}
+