public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 6/6] Make the "info proc" documentation more consistent.
  2018-09-12 23:37 [PATCH v2 0/6] Add a new 'info proc files' command John Baldwin
  2018-09-12 23:37 ` [PATCH v2 2/6] Add a new 'info proc files' subcommand of 'info proc' John Baldwin
@ 2018-09-12 23:37 ` John Baldwin
  2018-09-13 13:40   ` Eli Zaretskii
  2018-09-12 23:37 ` [PATCH v2 1/6] Use KF_PATH to verify the size of a struct kinfo_file John Baldwin
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 14+ messages in thread
From: John Baldwin @ 2018-09-12 23:37 UTC (permalink / raw)
  To: gdb-patches

Remove "running" in a few places since "info proc" can be used with
core dumps as well as running processes on both Linux and FreeBSD.

Use "the specified process" in the description of most "info proc"
subcommands.

Use "additional information" instead of "/proc process information" in
the "info proc" description to more closely match the language in the
manual.

gdb/ChangeLog:

	* infcmd.c (_initialize_infcmd): Remove "running" from "info proc"
	description.  Make "info proc" command descriptions more
	consistent.

gdb/doc/ChangeLog:

	* gdb.texinfo (info proc): Remove "running".
	(info proc mappings): Replace "program" with "process".
---
 gdb/ChangeLog       |  6 ++++++
 gdb/doc/ChangeLog   |  5 +++++
 gdb/doc/gdb.texinfo |  4 ++--
 gdb/infcmd.c        | 12 ++++++------
 4 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index fe515b2d0e..b4c6cb64bf 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2018-09-12  John Baldwin  <jhb@FreeBSD.org>
+
+	* infcmd.c (_initialize_infcmd): Remove "running" from "info proc"
+	description.  Make "info proc" command descriptions more
+	consistent.
+
 2018-09-12  John Baldwin  <jhb@FreeBSD.org>
 
 	* NEWS: Mention 'info proc files' command.
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index e327480af0..e88e047ff0 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,8 @@
+2018-09-12  John Baldwin  <jhb@FreeBSD.org>
+
+	* gdb.texinfo (info proc): Remove "running".
+	(info proc mappings): Replace "program" with "process".
+
 2018-09-12  John Baldwin  <jhb@FreeBSD.org>
 
 	* gdb.texinfo (Process Information): Document "info proc files"
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index e0e1379abd..9da6e9ba39 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22215,7 +22215,7 @@ systems.
 @cindex process ID
 @item info proc
 @itemx info proc @var{process-id}
-Summarize available information about any running process.  If a
+Summarize available information about a process.  If a
 process ID is specified by @var{process-id}, display information about
 that process; otherwise display information about the program being
 debugged.  The summary includes the debugged process ID, the command
@@ -22276,7 +22276,7 @@ Open files:
 
 @item info proc mappings
 @cindex memory address space mappings
-Report the memory address space ranges accessible in the program.  On
+Report the memory address space ranges accessible in a process.  On
 Solaris and FreeBSD systems, each memory range includes information on
 whether the process has read, write, or execute access rights to each
 range.  On @sc{gnu}/Linux and FreeBSD systems, each memory range
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 5fcb872c98..ab7ae60f4f 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -3522,13 +3522,13 @@ in the named register groups."));
 
   add_prefix_cmd ("proc", class_info, info_proc_cmd,
 		  _("\
-Show /proc process information about any running process.\n\
+Show additional information about a process.\n\
 Specify any process id, or use the program being debugged by default."),
 		  &info_proc_cmdlist, "info proc ",
 		  1/*allow-unknown*/, &infolist);
 
   add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\
-List of mapped memory regions."),
+List memory regions mapped by the specified process."),
 	   &info_proc_cmdlist);
 
   add_cmd ("stat", class_info, info_proc_cmd_stat, _("\
@@ -3540,15 +3540,15 @@ List process info from /proc/PID/status."),
 	   &info_proc_cmdlist);
 
   add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\
-List current working directory of the process."),
+List current working directory of the specified process."),
 	   &info_proc_cmdlist);
 
   add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\
-List command line arguments of the process."),
+List command line arguments of the specified process."),
 	   &info_proc_cmdlist);
 
   add_cmd ("exe", class_info, info_proc_cmd_exe, _("\
-List absolute filename for executable of the process."),
+List absolute filename for executable of the specified process."),
 	   &info_proc_cmdlist);
 
   add_cmd ("files", class_info, info_proc_cmd_files, _("\
@@ -3556,6 +3556,6 @@ List files opened by the specified process."),
 	   &info_proc_cmdlist);
 
   add_cmd ("all", class_info, info_proc_cmd_all, _("\
-List all available /proc info."),
+List all available info about the specified process."),
 	   &info_proc_cmdlist);
 }
-- 
2.18.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 3/6] Add support for 'info proc files' on FreeBSD core dumps.
  2018-09-12 23:37 [PATCH v2 0/6] Add a new 'info proc files' command John Baldwin
                   ` (3 preceding siblings ...)
  2018-09-12 23:37 ` [PATCH v2 5/6] Document the 'info proc files' command John Baldwin
@ 2018-09-12 23:37 ` John Baldwin
  2018-09-16  2:27   ` Simon Marchi
  2018-09-12 23:43 ` [PATCH v2 4/6] Support 'info proc files' on live FreeBSD processes John Baldwin
  2018-09-16  2:34 ` [PATCH v2 0/6] Add a new 'info proc files' command Simon Marchi
  6 siblings, 1 reply; 14+ messages in thread
From: John Baldwin @ 2018-09-12 23:37 UTC (permalink / raw)
  To: gdb-patches

Walk the list of struct kinfo_file objects in the
NT_FREEBSD_PROCSTAT_FILES core dump note outputting a description of
each open file descriptor.  For sockets, the local and remote socket
addresses are displayed in place of the file name field.  For UNIX
local domain sockets, only a single address is displayed since most
UNIX sockets only have one valid address and printing both pathnames
could be quite long.  The output format was somewhat inspired by the
output of the "procstat -f" command on FreeBSD, but with a few less
details and some fields were condensed.

gdb/ChangeLog:

	* fbsd-tdep.c (KF_FLAGS, KF_OFFSET, KF_VNODE_TYPE, KF_SOCK_DOMAIN)
	(KF_SOCK_TYPE, KF_SOCK_PROTOCOL, KF_SA_LOCAL, KF_SA_PEER)
	(KINFO_FILE_TYPE_SOCKET, KINFO_FILE_TYPE_PIPE)
	(KINFO_FILE_TYPE_FIFO, KINFO_FILE_TYPE_KQUEUE)
	(KINFO_FILE_TYPE_CRYPTO, KINFO_FILE_TYPE_MQUEUE)
	(KINFO_FILE_TYPE_SHM, KINFO_FILE_TYPE_SEM, KINFO_FILE_TYPE_PTS)
	(KINFO_FILE_TYPE_PROCDESC, KINFO_FILE_FD_TYPE_ROOT)
	(KINFO_FILE_FD_TYPE_JAIL, KINFO_FILE_FD_TYPE_TRACE)
	(KINFO_FILE_FD_TYPE_CTTY, KINFO_FILE_FLAG_READ)
	(KINFO_FILE_FLAG_WRITE, KINFO_FILE_FLAG_APPEND)
	(KINFO_FILE_FLAG_ASYNC, KINFO_FILE_FLAG_FSYNC)
	(KINFO_FILE_FLAG_NONBLOCK, KINFO_FILE_FLAG_DIRECT)
	(KINFO_FILE_FLAG_HASLOCK, KINFO_FILE_FLAG_EXEC)
	(KINFO_FILE_VTYPE_VREG, KINFO_FILE_VTYPE_VDIR)
	(KINFO_FILE_VTYPE_VCHR, KINFO_FILE_VTYPE_VLNK)
	(KINFO_FILE_VTYPE_VSOCK, KINFO_FILE_VTYPE_VFIFO, FBSD_AF_UNIX)
	(FBSD_AF_INET, FBSD_AF_INET6, FBSD_SOCK_STREAM, FBSD_SOCK_DGRAM)
	(FBSD_SOCK_SEQPACKET, FBSD_IPPROTO_ICMP, FBSD_IPPROTO_TCP)
	(FBSD_IPPROTO_UDP, FBSD_IPPROTO_SCTP): New defines.
	(struct fbsd_sockaddr_in, struct fbsd_sockaddr_in6)
	(struct fbsd_sockaddr_un): New types.
	(fbsd_file_fd, fbsd_file_type, fbsd_file_flags, fbsd_ipproto)
	(fbsd_print_sockaddr_in, fbsd_print_sockaddr_in6)
	(fbsd_info_proc_files_header, fbsd_info_proc_files_entry)
	(fbsd_core_info_proc_files): New functions.
	(fbsd_core_info_proc): List open file descriptors for IP_FILES and
	IP_ALL.
	* fbsd-tdep.h (fbsd_info_proc_files_header)
	(fbsd_info_proc_files_entry): New.
---
 gdb/ChangeLog   |  32 ++++
 gdb/fbsd-tdep.c | 462 +++++++++++++++++++++++++++++++++++++++++++++++-
 gdb/fbsd-tdep.h |  20 +++
 3 files changed, 512 insertions(+), 2 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index e504219625..bcd69e7e73 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,35 @@
+2018-09-12  John Baldwin  <jhb@FreeBSD.org>
+
+	* fbsd-tdep.c (KF_FLAGS, KF_OFFSET, KF_VNODE_TYPE, KF_SOCK_DOMAIN)
+	(KF_SOCK_TYPE, KF_SOCK_PROTOCOL, KF_SA_LOCAL, KF_SA_PEER)
+	(KINFO_FILE_TYPE_SOCKET, KINFO_FILE_TYPE_PIPE)
+	(KINFO_FILE_TYPE_FIFO, KINFO_FILE_TYPE_KQUEUE)
+	(KINFO_FILE_TYPE_CRYPTO, KINFO_FILE_TYPE_MQUEUE)
+	(KINFO_FILE_TYPE_SHM, KINFO_FILE_TYPE_SEM, KINFO_FILE_TYPE_PTS)
+	(KINFO_FILE_TYPE_PROCDESC, KINFO_FILE_FD_TYPE_ROOT)
+	(KINFO_FILE_FD_TYPE_JAIL, KINFO_FILE_FD_TYPE_TRACE)
+	(KINFO_FILE_FD_TYPE_CTTY, KINFO_FILE_FLAG_READ)
+	(KINFO_FILE_FLAG_WRITE, KINFO_FILE_FLAG_APPEND)
+	(KINFO_FILE_FLAG_ASYNC, KINFO_FILE_FLAG_FSYNC)
+	(KINFO_FILE_FLAG_NONBLOCK, KINFO_FILE_FLAG_DIRECT)
+	(KINFO_FILE_FLAG_HASLOCK, KINFO_FILE_FLAG_EXEC)
+	(KINFO_FILE_VTYPE_VREG, KINFO_FILE_VTYPE_VDIR)
+	(KINFO_FILE_VTYPE_VCHR, KINFO_FILE_VTYPE_VLNK)
+	(KINFO_FILE_VTYPE_VSOCK, KINFO_FILE_VTYPE_VFIFO, FBSD_AF_UNIX)
+	(FBSD_AF_INET, FBSD_AF_INET6, FBSD_SOCK_STREAM, FBSD_SOCK_DGRAM)
+	(FBSD_SOCK_SEQPACKET, FBSD_IPPROTO_ICMP, FBSD_IPPROTO_TCP)
+	(FBSD_IPPROTO_UDP, FBSD_IPPROTO_SCTP): New defines.
+	(struct fbsd_sockaddr_in, struct fbsd_sockaddr_in6)
+	(struct fbsd_sockaddr_un): New types.
+	(fbsd_file_fd, fbsd_file_type, fbsd_file_flags, fbsd_ipproto)
+	(fbsd_print_sockaddr_in, fbsd_print_sockaddr_in6)
+	(fbsd_info_proc_files_header, fbsd_info_proc_files_entry)
+	(fbsd_core_info_proc_files): New functions.
+	(fbsd_core_info_proc): List open file descriptors for IP_FILES and
+	IP_ALL.
+	* fbsd-tdep.h (fbsd_info_proc_files_header)
+	(fbsd_info_proc_files_entry): New.
+
 2018-09-12  John Baldwin  <jhb@FreeBSD.org>
 
 	* defs.h (enum info_proc_what) [IP_FILES]: New value.
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index 48544b5370..d7468705ff 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -55,7 +55,13 @@
 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_VMMAP core
    dump notes.  See <sys/user.h> for the definition of struct
    kinfo_vmentry.  This data structure should have the same layout on
-   all architectures.  */
+   all architectures.
+
+   Note that FreeBSD 7.0 used an older version of this structure
+   (struct kinfo_ovmentry), but the NT_FREEBSD_PROCSTAT_VMMAP core
+   dump note wasn't introduced until FreeBSD 9.2.  As a result, the
+   core dump note has always used the 7.1 and later structure
+   format.  */
 
 #define	KVE_STRUCTSIZE		0x0
 #define	KVE_START		0x8
@@ -85,23 +91,125 @@
 /* Offsets in data structure used in NT_FREEBSD_PROCSTAT_FILES core
    dump notes.  See <sys/user.h> for the definition of struct
    kinfo_file.  This data structure should have the same layout on all
-   architectures.  */
+   architectures.
+
+   Note that FreeBSD 7.0 used an older version of this structure
+   (struct kinfo_ofile), but the NT_FREEBSD_PROCSTAT_FILES core dump
+   note wasn't introduced until FreeBSD 9.2.  As a result, the core
+   dump note has always used the 7.1 and later structure format.  */
 
 #define	KF_STRUCTSIZE		0x0
 #define	KF_TYPE			0x4
 #define	KF_FD			0x8
+#define	KF_FLAGS		0x10
+#define	KF_OFFSET		0x18
+#define	KF_VNODE_TYPE		0x20
+#define	KF_SOCK_DOMAIN		0x24
+#define	KF_SOCK_TYPE		0x28
+#define	KF_SOCK_PROTOCOL	0x2c
+#define	KF_SA_LOCAL		0x30
+#define	KF_SA_PEER		0xb0
 #define	KF_PATH			0x170
 
 /* Constants for the 'kf_type' field in struct kinfo_file.  These
    match the KF_TYPE_* constants in <sys/user.h>.  */
 
 #define	KINFO_FILE_TYPE_VNODE	1
+#define	KINFO_FILE_TYPE_SOCKET	2
+#define	KINFO_FILE_TYPE_PIPE	3
+#define	KINFO_FILE_TYPE_FIFO	4
+#define	KINFO_FILE_TYPE_KQUEUE	5
+#define	KINFO_FILE_TYPE_CRYPTO	6
+#define	KINFO_FILE_TYPE_MQUEUE	7
+#define	KINFO_FILE_TYPE_SHM	8
+#define	KINFO_FILE_TYPE_SEM	9
+#define	KINFO_FILE_TYPE_PTS	10
+#define	KINFO_FILE_TYPE_PROCDESC 11
 
 /* Special values for the 'kf_fd' field in struct kinfo_file.  These
    match the KF_FD_TYPE_* constants in <sys/user.h>.  */
 
 #define	KINFO_FILE_FD_TYPE_CWD	-1
+#define	KINFO_FILE_FD_TYPE_ROOT	-2
+#define	KINFO_FILE_FD_TYPE_JAIL	-3
+#define	KINFO_FILE_FD_TYPE_TRACE -4
 #define	KINFO_FILE_FD_TYPE_TEXT	-5
+#define	KINFO_FILE_FD_TYPE_CTTY	-6
+
+/* Flags in the 'kf_flags' field in struct kinfo_file.  These match
+   the KF_FLAG_* constants in <sys/user.h>.  */
+
+#define	KINFO_FILE_FLAG_READ		0x00000001
+#define	KINFO_FILE_FLAG_WRITE		0x00000002
+#define	KINFO_FILE_FLAG_APPEND		0x00000004
+#define	KINFO_FILE_FLAG_ASYNC		0x00000008
+#define	KINFO_FILE_FLAG_FSYNC		0x00000010
+#define	KINFO_FILE_FLAG_NONBLOCK	0x00000020
+#define	KINFO_FILE_FLAG_DIRECT		0x00000040
+#define	KINFO_FILE_FLAG_HASLOCK		0x00000080
+#define	KINFO_FILE_FLAG_EXEC		0x00004000
+
+/* Constants for the 'kf_vnode_type' field in struct kinfo_file.
+   These match the KF_VTYPE_* constants in <sys/user.h>.  */
+
+#define	KINFO_FILE_VTYPE_VREG	1
+#define	KINFO_FILE_VTYPE_VDIR	2
+#define	KINFO_FILE_VTYPE_VCHR	4
+#define	KINFO_FILE_VTYPE_VLNK	5
+#define	KINFO_FILE_VTYPE_VSOCK	6
+#define	KINFO_FILE_VTYPE_VFIFO	7
+
+/* Constants for socket address families.  These match AF_* constants
+   in <sys/socket.h>.  */
+
+#define	FBSD_AF_UNIX		1
+#define	FBSD_AF_INET		2
+#define	FBSD_AF_INET6		28
+
+/* Constants for socket types.  These match SOCK_* constants in
+   <sys/socket.h>.  */
+
+#define	FBSD_SOCK_STREAM	1
+#define	FBSD_SOCK_DGRAM		2
+#define	FBSD_SOCK_SEQPACKET	5
+
+/* Constants for IP protocols.  These match IPPROTO_* constants in
+   <netinet/in.h>.  */
+
+#define	FBSD_IPPROTO_ICMP	1
+#define	FBSD_IPPROTO_TCP	6
+#define	FBSD_IPPROTO_UDP	17
+#define	FBSD_IPPROTO_SCTP	132
+
+/* Socket address structures.  These have the same layout on all
+   FreeBSD architectures.  In addition, multibyte fields such as IP
+   addresses are always stored in network byte order.  */
+
+struct fbsd_sockaddr_in
+{
+  uint8_t sin_len;
+  uint8_t sin_family;
+  uint8_t sin_port[2];
+  uint8_t sin_addr[4];
+  char sin_zero[8];
+};
+
+struct fbsd_sockaddr_in6
+{
+  uint8_t sin6_len;
+  uint8_t sin6_family;
+  uint8_t sin6_port[2];
+  uint32_t sin6_flowinfo;
+  uint8_t sin6_addr[16];
+  uint32_t sin6_scope_id;
+};
+
+struct fbsd_sockaddr_un
+{
+  uint8_t sun_len;
+  uint8_t sun_family;
+  char sun_path[104];
+};
 
 /* Number of 32-bit words in a signal set.  This matches _SIG_WORDS in
    <sys/_sigset.h> and is the same value on all architectures.  */
@@ -645,6 +753,349 @@ fbsd_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
   return note_data;
 }
 
+/* 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.  The address
+   is formatted similar to inet_ntoa.  */
+
+static void
+fbsd_print_sockaddr_in (const void *sockaddr)
+{
+  const struct fbsd_sockaddr_in *sin =
+    reinterpret_cast<const struct fbsd_sockaddr_in *>(sockaddr);
+
+  printf_filtered ("%u.%u.%u.%u:%u", sin->sin_addr[0], sin->sin_addr[1],
+		   sin->sin_addr[2], sin->sin_addr[3],
+		   (sin->sin_port[0] << 8) | sin->sin_port[1]);
+}
+
+/* Helper function to print out an IPv6 socket address.  The address
+   is formatted similar to inet_ntop.  */
+
+static void
+fbsd_print_sockaddr_in6 (const void *sockaddr)
+{
+  const struct fbsd_sockaddr_in6 *sin6 =
+    reinterpret_cast<const struct fbsd_sockaddr_in6 *>(sockaddr);
+  uint16_t words[ARRAY_SIZE(sin6->sin6_addr) / 2];
+
+  /* Populate the array of 16-bit words from network-order bytes.  */
+  for (int i = 0; i < ARRAY_SIZE(words); i++)
+    words[i] = (sin6->sin6_addr[i * 2] << 8) | sin6->sin6_addr[i * 2 + 1];
+
+  /* Find the longest run of zero words.  */
+  int best, bestlen, current, len;
+
+  best = -1;
+  bestlen = 0;
+  current = -1;
+  len = 0;
+  for (int i = 0; i < ARRAY_SIZE(words); i++)
+    {
+      if (words[i] == 0)
+	{
+	  if (current >= 0)
+	    len++;
+	  else
+	    {
+	      current = i;
+	      len = 1;
+	    }
+	}
+      else
+	{
+	  if (current >= 0 && len > bestlen)
+	    {
+	      best = current;
+	      bestlen = len;
+	    }
+	  current = -1;
+	  len = 0;
+	}
+    }
+  if (current >= 0 && len > bestlen)
+    {
+      best = current;
+      bestlen = len;
+    }
+  if (bestlen < 2)
+    best = -1;
+
+  for (int i = 0; i < ARRAY_SIZE(words); i++)
+    {
+      if (best >= 0 && i >= best && i < best + bestlen)
+	{
+	  if (i == best || i == ARRAY_SIZE(words) - 1)
+	    printf_filtered (":");
+	}
+      else
+	{
+	  if (i != 0)
+	    printf_filtered (":");
+	  printf_filtered ("%x", words[i]);
+	}
+    }
+  printf_filtered (".%u", (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
+}
+
+/* Output the header for "info proc files".  */
+
+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");
+}
+
+/* Output description of a single file descriptor for "info proc
+   files".  */
+
+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", 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;
+    }
+}
+
 /* Helper function to generate mappings flags for a single VM map
    entry in 'info proc mappings'.  */
 
@@ -995,6 +1446,7 @@ fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
   bool do_cmdline = false;
   bool do_cwd = false;
   bool do_exe = false;
+  bool do_files = false;
   bool do_mappings = false;
   bool do_status = false;
   int pid;
@@ -1022,10 +1474,14 @@ fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
     case IP_CWD:
       do_cwd = true;
       break;
+    case IP_FILES:
+      do_files = true;
+      break;
     case IP_ALL:
       do_cmdline = true;
       do_cwd = true;
       do_exe = true;
+      do_files = true;
       do_mappings = true;
       do_status = true;
       break;
@@ -1065,6 +1521,8 @@ fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
       else
 	warning (_("unable to read executable path name"));
     }
+  if (do_files)
+    fbsd_core_info_proc_files (gdbarch);
   if (do_mappings)
     fbsd_core_info_proc_mappings (gdbarch);
   if (do_status)
diff --git a/gdb/fbsd-tdep.h b/gdb/fbsd-tdep.h
index 0b293e5a25..74a23c0f24 100644
--- a/gdb/fbsd-tdep.h
+++ b/gdb/fbsd-tdep.h
@@ -22,6 +22,26 @@
 
 extern void fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch);
 
+/* Output the header for "info proc files".  */
+
+extern void fbsd_info_proc_files_header ();
+
+/* Output description of a single file descriptor for "info proc
+   files".  The KF_TYPE, KF_FD, KF_FLAGS, KF_OFFSET, KF_VNODE_TYPE,
+   KF_SOCK_DOMAIN, KF_SOCK_TYPE, and KF_SOCK_PROTOCOL parameters
+   should contain the value of the corresponding fields in a 'struct
+   kinfo_file'.  The KF_SA_LOCAL, KF_SA_PEER, and KF_PATH parameters
+   should contain pointers to the corresponding fields in a 'struct
+   kinfo_file'. */
+
+extern 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 *fa_sa_peer,
+					const void *path);
+
 /* Helper function to generate mappings flags for a single VM map
    entry in 'info proc mappings'.  The KVE_FLAGS and KVE_PROTECTION
    parameters should contain the values of the corresponding fields in
-- 
2.18.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 5/6] Document the 'info proc files' command.
  2018-09-12 23:37 [PATCH v2 0/6] Add a new 'info proc files' command John Baldwin
                   ` (2 preceding siblings ...)
  2018-09-12 23:37 ` [PATCH v2 1/6] Use KF_PATH to verify the size of a struct kinfo_file John Baldwin
@ 2018-09-12 23:37 ` John Baldwin
  2018-09-13 13:39   ` Eli Zaretskii
  2018-09-12 23:37 ` [PATCH v2 3/6] Add support for 'info proc files' on FreeBSD core dumps John Baldwin
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 14+ messages in thread
From: John Baldwin @ 2018-09-12 23:37 UTC (permalink / raw)
  To: gdb-patches

gdb/ChangeLog:

	* NEWS: Mention 'info proc files' command.

gdb/doc/ChangeLog:

	* gdb.texinfo (Process Information): Document "info proc files"
	command.
---
 gdb/ChangeLog       |  4 ++++
 gdb/NEWS            |  3 +++
 gdb/doc/ChangeLog   |  5 +++++
 gdb/doc/gdb.texinfo | 30 ++++++++++++++++++++++++++++++
 4 files changed, 42 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 997c079218..fe515b2d0e 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,7 @@
+2018-09-12  John Baldwin  <jhb@FreeBSD.org>
+
+	* NEWS: Mention 'info proc files' command.
+
 2018-09-12  John Baldwin  <jhb@FreeBSD.org>
 
 	* fbsd-nat.c (fbsd_nat_target::info_proc): List open file
diff --git a/gdb/NEWS b/gdb/NEWS
index 4e4f12d8d1..8efaafa1e1 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -56,6 +56,9 @@ maint set dwarf unwinders (on|off)
 maint show dwarf unwinders
   Control whether DWARF unwinders can be used.
 
+info proc files
+  Display a list of open files for a process.
+
 * Changed commands
 
 thread apply [all | COUNT | -COUNT] [FLAG]... COMMAND
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index f04dca4283..e327480af0 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,8 @@
+2018-09-12  John Baldwin  <jhb@FreeBSD.org>
+
+	* gdb.texinfo (Process Information): Document "info proc files"
+	command.
+
 2018-09-12  Simon Marchi  <simon.marchi@ericsson.com>
 
 	* gdb.texinfo (Skipping Over Functions and Files): Document
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index efbec3debf..e0e1379abd 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22244,6 +22244,36 @@ supported on @sc{gnu}/Linux and FreeBSD.
 Show the name of executable of the process.  This command is supported
 on @sc{gnu}/Linux and FreeBSD.
 
+@item info proc files
+@cindex info proc files
+Show the file descriptors open by the process.  For each open file
+descriptor, @value{GDBN} shows its number, type (file, directory,
+character device, socket), file pointer offset, and the name of the
+resource open on the descriptor.  The resource name can be a file name
+(for files, directories, and devices) or a protocol followed by socket
+address (for network connections).  This command is supported on
+FreeBSD.
+
+This example shows the open file descriptors for a process using a
+tty for standard input and output as well as two network sockets:
+
+@smallexample
+(gdb) info proc files 22136
+process 22136
+Open files:
+
+      FD   Type     Offset   Flags   Name
+    text   file          - r-------- /usr/bin/ssh
+    ctty    chr          - rw------- /dev/pts/20
+     cwd    dir          - r-------- /usr/home/john
+    root    dir          - r-------- /
+       0    chr  0x32933a4 rw------- /dev/pts/20
+       1    chr  0x32933a4 rw------- /dev/pts/20
+       2    chr  0x32933a4 rw------- /dev/pts/20
+       3 socket        0x0 rw----n-- tcp4 10.0.1.2:53014 -> 10.0.1.10:22
+       4 socket        0x0 rw------- unix stream:/tmp/ssh-FIt89oAzOn5f/agent.2456
+@end smallexample
+
 @item info proc mappings
 @cindex memory address space mappings
 Report the memory address space ranges accessible in the program.  On
-- 
2.18.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 1/6] Use KF_PATH to verify the size of a struct kinfo_file.
  2018-09-12 23:37 [PATCH v2 0/6] Add a new 'info proc files' command John Baldwin
  2018-09-12 23:37 ` [PATCH v2 2/6] Add a new 'info proc files' subcommand of 'info proc' John Baldwin
  2018-09-12 23:37 ` [PATCH v2 6/6] Make the "info proc" documentation more consistent John Baldwin
@ 2018-09-12 23:37 ` John Baldwin
  2018-09-12 23:37 ` [PATCH v2 5/6] Document the 'info proc files' command John Baldwin
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: John Baldwin @ 2018-09-12 23:37 UTC (permalink / raw)
  To: gdb-patches

fbsd_core_vnode_path needs to use the offset of the kf_path member of
struct kinfo_file as the minimum size of a struct kinfo_file object.
However, it was using KVE_PATH instead due to a copy and paste bug.

While here, fix another copy and paste bug in the error message for a
truncated kinfo_file object.

gdb/ChangeLog:

	* fbsd-tdep.c (fbsd_core_vnode_path): Use KF_PATH instead of
	KVE_PATH.
---
 gdb/ChangeLog   | 5 +++++
 gdb/fbsd-tdep.c | 6 +++---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 454c45f9ad..d602e63eb0 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2018-09-12  John Baldwin  <jhb@FreeBSD.org>
+
+	* fbsd-tdep.c (fbsd_core_vnode_path): Use KF_PATH instead of
+	KVE_PATH.
+
 2018-09-12  John Baldwin  <jhb@FreeBSD.org>
 
 	* fbsd-nat.c (fbsd_nat_target::info_proc): Remove unused variable.
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index ed43087169..48544b5370 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -781,13 +781,13 @@ fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
   /* Skip over the structure size.  */
   descdata += 4;
 
-  while (descdata + KVE_PATH < descend)
+  while (descdata + KF_PATH < descend)
     {
       ULONGEST structsize;
 
       structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
-      if (structsize < KVE_PATH)
-	error (_("malformed core note - vmmap entry too small"));
+      if (structsize < KF_PATH)
+	error (_("malformed core note - file structure too small"));
 
       if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
 	  && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
-- 
2.18.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 0/6] Add a new 'info proc files' command
@ 2018-09-12 23:37 John Baldwin
  2018-09-12 23:37 ` [PATCH v2 2/6] Add a new 'info proc files' subcommand of 'info proc' John Baldwin
                   ` (6 more replies)
  0 siblings, 7 replies; 14+ messages in thread
From: John Baldwin @ 2018-09-12 23:37 UTC (permalink / raw)
  To: gdb-patches

This should include most of the suggested documentation fixes from the
first series.  It also adds an additional patch that attempts to tidy up
some of the other "info proc" documentation.  One change I haven't made
(wasn't sure if it was still desired) was if we wanted to replace the
specific annotations on individual 'info proc' subcommands about which
OS's supported those commands with a single, more general statement that
commands may only be supported on a subset of systems supported by GDB.

I've moved more of the shared code for generating the 'info proc files'
output to fbsd-tdep.c.

One open question still from the first series is if GDB can assume the
presence of routines like 'inet_ntoa' and 'inet_ntop' for formatting
IPv4 and IPv6 addresses.  It seems GDB does assume the presence of
newer routines (e.g. getaddrinfo()) on POSIX systems in other places
(e.g. ser-tcp.c), though in those places we use different APIs for
Win32.

John Baldwin (6):
  Use KF_PATH to verify the size of a struct kinfo_file.
  Add a new 'info proc files' subcommand of 'info proc'.
  Add support for 'info proc files' on FreeBSD core dumps.
  Support 'info proc files' on live FreeBSD processes.
  Document the 'info proc files' command.
  Make the "info proc" documentation more consistent.

 gdb/ChangeLog       |  58 ++++++
 gdb/NEWS            |   3 +
 gdb/defs.h          |   3 +
 gdb/doc/ChangeLog   |  10 +
 gdb/doc/gdb.texinfo |  34 +++-
 gdb/fbsd-nat.c      |  32 ++-
 gdb/fbsd-tdep.c     | 468 +++++++++++++++++++++++++++++++++++++++++++-
 gdb/fbsd-tdep.h     |  20 ++
 gdb/infcmd.c        |  24 ++-
 9 files changed, 638 insertions(+), 14 deletions(-)

-- 
2.18.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 2/6] Add a new 'info proc files' subcommand of 'info proc'.
  2018-09-12 23:37 [PATCH v2 0/6] Add a new 'info proc files' command John Baldwin
@ 2018-09-12 23:37 ` John Baldwin
  2018-09-12 23:37 ` [PATCH v2 6/6] Make the "info proc" documentation more consistent John Baldwin
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 14+ messages in thread
From: John Baldwin @ 2018-09-12 23:37 UTC (permalink / raw)
  To: gdb-patches

This command displays a list of open file descriptors.

gdb/ChangeLog:

	* defs.h (enum info_proc_what) [IP_FILES]: New value.
	* infcmd.c (info_proc_cmd_files): New function.
	(_initialize_infcmd): Register 'info proc files' command.
---
 gdb/ChangeLog |  6 ++++++
 gdb/defs.h    |  3 +++
 gdb/infcmd.c  | 12 ++++++++++++
 3 files changed, 21 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index d602e63eb0..e504219625 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2018-09-12  John Baldwin  <jhb@FreeBSD.org>
+
+	* defs.h (enum info_proc_what) [IP_FILES]: New value.
+	* infcmd.c (info_proc_cmd_files): New function.
+	(_initialize_infcmd): Register 'info proc files' command.
+
 2018-09-12  John Baldwin  <jhb@FreeBSD.org>
 
 	* fbsd-tdep.c (fbsd_core_vnode_path): Use KF_PATH instead of
diff --git a/gdb/defs.h b/gdb/defs.h
index fc4217005a..6e3f4df116 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -389,6 +389,9 @@ enum info_proc_what
     /* * Display `info proc cwd'.  */
     IP_CWD,
 
+    /* * Display `info proc files'.  */
+    IP_FILES,
+
     /* * Display all of the above.  */
     IP_ALL
   };
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 860909f5e2..5fcb872c98 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -3218,6 +3218,14 @@ info_proc_cmd_exe (const char *args, int from_tty)
   info_proc_cmd_1 (args, IP_EXE, from_tty);
 }
 
+/* Implement `info proc files'.  */
+
+static void
+info_proc_cmd_files (const char *args, int from_tty)
+{
+  info_proc_cmd_1 (args, IP_FILES, from_tty);
+}
+
 /* Implement `info proc all'.  */
 
 static void
@@ -3543,6 +3551,10 @@ List command line arguments of the process."),
 List absolute filename for executable of the process."),
 	   &info_proc_cmdlist);
 
+  add_cmd ("files", class_info, info_proc_cmd_files, _("\
+List files opened by the specified process."),
+	   &info_proc_cmdlist);
+
   add_cmd ("all", class_info, info_proc_cmd_all, _("\
 List all available /proc info."),
 	   &info_proc_cmdlist);
-- 
2.18.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 4/6] Support 'info proc files' on live FreeBSD processes.
  2018-09-12 23:37 [PATCH v2 0/6] Add a new 'info proc files' command John Baldwin
                   ` (4 preceding siblings ...)
  2018-09-12 23:37 ` [PATCH v2 3/6] Add support for 'info proc files' on FreeBSD core dumps John Baldwin
@ 2018-09-12 23:43 ` John Baldwin
  2018-09-16  2:34 ` [PATCH v2 0/6] Add a new 'info proc files' command Simon Marchi
  6 siblings, 0 replies; 14+ messages in thread
From: John Baldwin @ 2018-09-12 23:43 UTC (permalink / raw)
  To: gdb-patches

This walks the list of struct kinfo_file objects returned by a call to
kinfo_getfile outputting a description of each open file descriptor.

gdb/ChangeLog:

	* fbsd-nat.c (fbsd_nat_target::info_proc): List open file
	descriptors for IP_FILES and IP_ALL.
---
 gdb/ChangeLog  |  5 +++++
 gdb/fbsd-nat.c | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index bcd69e7e73..997c079218 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2018-09-12  John Baldwin  <jhb@FreeBSD.org>
+
+	* fbsd-nat.c (fbsd_nat_target::info_proc): List open file
+	descriptors for IP_FILES and IP_ALL.
+
 2018-09-12  John Baldwin  <jhb@FreeBSD.org>
 
 	* fbsd-tdep.c (KF_FLAGS, KF_OFFSET, KF_VNODE_TYPE, KF_SOCK_DOMAIN)
diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index 2b829bfc3b..24e40e2406 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -265,6 +265,9 @@ fbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
   bool do_cmdline = false;
   bool do_cwd = false;
   bool do_exe = false;
+#ifdef HAVE_KINFO_GETFILE
+  bool do_files = false;
+#endif
 #ifdef HAVE_KINFO_GETVMMAP
   bool do_mappings = false;
 #endif
@@ -295,10 +298,18 @@ fbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
     case IP_CWD:
       do_cwd = true;
       break;
+#ifdef HAVE_KINFO_GETFILE
+    case IP_FILES:
+      do_files = true;
+      break;
+#endif
     case IP_ALL:
       do_cmdline = true;
       do_cwd = true;
       do_exe = true;
+#ifdef HAVE_KINFO_GETFILE
+      do_files = true;
+#endif
 #ifdef HAVE_KINFO_GETVMMAP
       do_mappings = true;
 #endif
@@ -322,7 +333,7 @@ fbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
 
   printf_filtered (_("process %d\n"), pid);
 #ifdef HAVE_KINFO_GETFILE
-  if (do_cwd || do_exe)
+  if (do_cwd || do_exe || do_files)
     fdtbl.reset (kinfo_getfile (pid, &nfd));
 #endif
 
@@ -374,6 +385,25 @@ fbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
       else
 	warning (_("unable to fetch executable path name"));
     }
+#ifdef HAVE_KINFO_GETFILE
+  if (do_files)
+    {
+      struct kinfo_file *kf = fdtbl.get ();
+
+      if (nfd > 0)
+	{
+	  fbsd_info_proc_files_header ();
+	  for (int i = 0; i < nfd; i++, kf++)
+	    fbsd_info_proc_files_entry (kf->kf_type, kf->kf_fd, kf->kf_flags,
+					kf->kf_offset, kf->kf_vnode_type,
+					kf->kf_sock_domain, kf->kf_sock_type,
+					kf->kf_sock_protocol, &kf->kf_sa_local,
+					&kf->kf_sa_peer, kf->kf_path);
+	}
+      else
+	warning (_("unable to fetch list of open files"));
+    }
+#endif
 #ifdef HAVE_KINFO_GETVMMAP
   if (do_mappings)
     {
-- 
2.18.0

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 5/6] Document the 'info proc files' command.
  2018-09-12 23:37 ` [PATCH v2 5/6] Document the 'info proc files' command John Baldwin
@ 2018-09-13 13:39   ` Eli Zaretskii
  0 siblings, 0 replies; 14+ messages in thread
From: Eli Zaretskii @ 2018-09-13 13:39 UTC (permalink / raw)
  To: John Baldwin; +Cc: gdb-patches

> From: John Baldwin <jhb@FreeBSD.org>
> Date: Wed, 12 Sep 2018 16:37:06 -0700
> 
> gdb/ChangeLog:
> 
> 	* NEWS: Mention 'info proc files' command.
> 
> gdb/doc/ChangeLog:
> 
> 	* gdb.texinfo (Process Information): Document "info proc files"
> 	command.

Thanks, this is OK.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 6/6] Make the "info proc" documentation more consistent.
  2018-09-12 23:37 ` [PATCH v2 6/6] Make the "info proc" documentation more consistent John Baldwin
@ 2018-09-13 13:40   ` Eli Zaretskii
  0 siblings, 0 replies; 14+ messages in thread
From: Eli Zaretskii @ 2018-09-13 13:40 UTC (permalink / raw)
  To: John Baldwin; +Cc: gdb-patches

> From: John Baldwin <jhb@FreeBSD.org>
> Date: Wed, 12 Sep 2018 16:37:07 -0700
> 
> Remove "running" in a few places since "info proc" can be used with
> core dumps as well as running processes on both Linux and FreeBSD.
> 
> Use "the specified process" in the description of most "info proc"
> subcommands.
> 
> Use "additional information" instead of "/proc process information" in
> the "info proc" description to more closely match the language in the
> manual.
> 
> gdb/ChangeLog:
> 
> 	* infcmd.c (_initialize_infcmd): Remove "running" from "info proc"
> 	description.  Make "info proc" command descriptions more
> 	consistent.
> 
> gdb/doc/ChangeLog:
> 
> 	* gdb.texinfo (info proc): Remove "running".
> 	(info proc mappings): Replace "program" with "process".

Thanks, this is OK.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 3/6] Add support for 'info proc files' on FreeBSD core dumps.
  2018-09-12 23:37 ` [PATCH v2 3/6] Add support for 'info proc files' on FreeBSD core dumps John Baldwin
@ 2018-09-16  2:27   ` Simon Marchi
  2018-09-17 16:54     ` John Baldwin
  0 siblings, 1 reply; 14+ messages in thread
From: Simon Marchi @ 2018-09-16  2:27 UTC (permalink / raw)
  To: John Baldwin, gdb-patches

Some nits about the comments, otherwise LGTM (I didn't check the
bits-reading code in detail, I assumed it wash largely unchanged).

On 2018-09-12 7:37 p.m., John Baldwin wrote:
> +static void
> +fbsd_print_sockaddr_in6 (const void *sockaddr)
> +{
> +  const struct fbsd_sockaddr_in6 *sin6 =
> +    reinterpret_cast<const struct fbsd_sockaddr_in6 *>(sockaddr);
> +  uint16_t words[ARRAY_SIZE(sin6->sin6_addr) / 2];
> +
> +  /* Populate the array of 16-bit words from network-order bytes.  */
> +  for (int i = 0; i < ARRAY_SIZE(words); i++)
> +    words[i] = (sin6->sin6_addr[i * 2] << 8) | sin6->sin6_addr[i * 2 + 1];
> +
> +  /* Find the longest run of zero words.  */
> +  int best, bestlen, current, len;
> +
> +  best = -1;
> +  bestlen = 0;
> +  current = -1;
> +  len = 0;
> +  for (int i = 0; i < ARRAY_SIZE(words); i++)
> +    {
> +      if (words[i] == 0)
> +	{
> +	  if (current >= 0)
> +	    len++;
> +	  else
> +	    {
> +	      current = i;
> +	      len = 1;
> +	    }
> +	}
> +      else
> +	{
> +	  if (current >= 0 && len > bestlen)
> +	    {
> +	      best = current;
> +	      bestlen = len;
> +	    }
> +	  current = -1;
> +	  len = 0;
> +	}
> +    }
> +  if (current >= 0 && len > bestlen)
> +    {
> +      best = current;
> +      bestlen = len;
> +    }
> +  if (bestlen < 2)
> +    best = -1;
> +
> +  for (int i = 0; i < ARRAY_SIZE(words); i++)
> +    {
> +      if (best >= 0 && i >= best && i < best + bestlen)
> +	{
> +	  if (i == best || i == ARRAY_SIZE(words) - 1)
> +	    printf_filtered (":");
> +	}
> +      else
> +	{
> +	  if (i != 0)
> +	    printf_filtered (":");
> +	  printf_filtered ("%x", words[i]);
> +	}
> +    }
> +  printf_filtered (".%u", (sin6->sin6_port[0] << 8) | sin6->sin6_port[1]);
> +}
> +
> +/* Output the header for "info proc files".  */

This should be /* See fbsd-tdep.h.  */, same for fbsd_info_proc_files_entry.

> +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");
> +}

...

> +/* Output description of a single file descriptor for "info proc
> +   files".  The KF_TYPE, KF_FD, KF_FLAGS, KF_OFFSET, KF_VNODE_TYPE,
> +   KF_SOCK_DOMAIN, KF_SOCK_TYPE, and KF_SOCK_PROTOCOL parameters
> +   should contain the value of the corresponding fields in a 'struct
> +   kinfo_file'.  The KF_SA_LOCAL, KF_SA_PEER, and KF_PATH parameters
> +   should contain pointers to the corresponding fields in a 'struct
> +   kinfo_file'. */

Some parameters name in the doc here don't match the actual names below.

> +extern 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 *fa_sa_peer,
> +					const void *path);
> +

Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 0/6] Add a new 'info proc files' command
  2018-09-12 23:37 [PATCH v2 0/6] Add a new 'info proc files' command John Baldwin
                   ` (5 preceding siblings ...)
  2018-09-12 23:43 ` [PATCH v2 4/6] Support 'info proc files' on live FreeBSD processes John Baldwin
@ 2018-09-16  2:34 ` Simon Marchi
  2018-09-17 16:59   ` John Baldwin
  6 siblings, 1 reply; 14+ messages in thread
From: Simon Marchi @ 2018-09-16  2:34 UTC (permalink / raw)
  To: John Baldwin, gdb-patches

On 2018-09-12 7:37 p.m., John Baldwin wrote:
> This should include most of the suggested documentation fixes from the
> first series.  It also adds an additional patch that attempts to tidy up
> some of the other "info proc" documentation.  One change I haven't made
> (wasn't sure if it was still desired) was if we wanted to replace the
> specific annotations on individual 'info proc' subcommands about which
> OS's supported those commands with a single, more general statement that
> commands may only be supported on a subset of systems supported by GDB.
> 
> I've moved more of the shared code for generating the 'info proc files'
> output to fbsd-tdep.c.
> 
> One open question still from the first series is if GDB can assume the
> presence of routines like 'inet_ntoa' and 'inet_ntop' for formatting
> IPv4 and IPv6 addresses.  It seems GDB does assume the presence of
> newer routines (e.g. getaddrinfo()) on POSIX systems in other places
> (e.g. ser-tcp.c), though in those places we use different APIs for
> Win32.

There seems to be a gnulib module for that:

https://www.gnu.org/software/gnulib/manual/html_node/inet_005fntop.html

There is a page for inet_ntoa too:

https://www.gnu.org/software/gnulib/manual/html_node/inet_005fntoa.html

but there is no gnulib module for it.  Probably because we can always
use inet_ntop instead?

Using that would be preferable to implementing it ourself, if it does
what we need.

Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 3/6] Add support for 'info proc files' on FreeBSD core dumps.
  2018-09-16  2:27   ` Simon Marchi
@ 2018-09-17 16:54     ` John Baldwin
  0 siblings, 0 replies; 14+ messages in thread
From: John Baldwin @ 2018-09-17 16:54 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 9/15/18 7:27 PM, Simon Marchi wrote:
> Some nits about the comments, otherwise LGTM (I didn't check the
> bits-reading code in detail, I assumed it wash largely unchanged).

Yes, the bits-reading code hasn't changed.
 
> On 2018-09-12 7:37 p.m., John Baldwin wrote:
>> +
>> +/* Output the header for "info proc files".  */
> 
> This should be /* See fbsd-tdep.h.  */, same for fbsd_info_proc_files_entry.

Ok, fixed both locally.

>> +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");
>> +}
> 
> ...
> 
>> +/* Output description of a single file descriptor for "info proc
>> +   files".  The KF_TYPE, KF_FD, KF_FLAGS, KF_OFFSET, KF_VNODE_TYPE,
>> +   KF_SOCK_DOMAIN, KF_SOCK_TYPE, and KF_SOCK_PROTOCOL parameters
>> +   should contain the value of the corresponding fields in a 'struct
>> +   kinfo_file'.  The KF_SA_LOCAL, KF_SA_PEER, and KF_PATH parameters
>> +   should contain pointers to the corresponding fields in a 'struct
>> +   kinfo_file'. */
> 
> Some parameters name in the doc here don't match the actual names below.
> 
>> +extern 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 *fa_sa_peer,
>> +					const void *path);

Huh, I'm not sure how I botched that, but I've fixed.  The comment was
correct, and the actual function definition matched the comment.  Only the
prototype was wrong.
-- 
John Baldwin

                                                                            

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 0/6] Add a new 'info proc files' command
  2018-09-16  2:34 ` [PATCH v2 0/6] Add a new 'info proc files' command Simon Marchi
@ 2018-09-17 16:59   ` John Baldwin
  2018-09-17 17:17     ` Simon Marchi
  0 siblings, 1 reply; 14+ messages in thread
From: John Baldwin @ 2018-09-17 16:59 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 9/15/18 7:34 PM, Simon Marchi wrote:
> On 2018-09-12 7:37 p.m., John Baldwin wrote:
>> This should include most of the suggested documentation fixes from the
>> first series.  It also adds an additional patch that attempts to tidy up
>> some of the other "info proc" documentation.  One change I haven't made
>> (wasn't sure if it was still desired) was if we wanted to replace the
>> specific annotations on individual 'info proc' subcommands about which
>> OS's supported those commands with a single, more general statement that
>> commands may only be supported on a subset of systems supported by GDB.
>>
>> I've moved more of the shared code for generating the 'info proc files'
>> output to fbsd-tdep.c.
>>
>> One open question still from the first series is if GDB can assume the
>> presence of routines like 'inet_ntoa' and 'inet_ntop' for formatting
>> IPv4 and IPv6 addresses.  It seems GDB does assume the presence of
>> newer routines (e.g. getaddrinfo()) on POSIX systems in other places
>> (e.g. ser-tcp.c), though in those places we use different APIs for
>> Win32.
> 
> There seems to be a gnulib module for that:
> 
> https://www.gnu.org/software/gnulib/manual/html_node/inet_005fntop.html
> 
> There is a page for inet_ntoa too:
> 
> https://www.gnu.org/software/gnulib/manual/html_node/inet_005fntoa.html
> 
> but there is no gnulib module for it.  Probably because we can always
> use inet_ntop instead?

Yes, inet_ntop is sufficient for both.  I had grepp'ed for 'ntoa' and
'ntop' in gdb/gnulib and didn't find any matches, so I assumed that
meant there wasn't a module.  Is gdb/gnulib a subset of the actual gnulib
and new bits are imported on demand?

Hmm, reading update-gnulib.sh, it seems so.  I'll look at what is involved
in doing that.

-- 
John Baldwin

                                                                            

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 0/6] Add a new 'info proc files' command
  2018-09-17 16:59   ` John Baldwin
@ 2018-09-17 17:17     ` Simon Marchi
  0 siblings, 0 replies; 14+ messages in thread
From: Simon Marchi @ 2018-09-17 17:17 UTC (permalink / raw)
  To: John Baldwin; +Cc: gdb-patches

On 2018-09-17 12:58, John Baldwin wrote:
> On 9/15/18 7:34 PM, Simon Marchi wrote:
>> On 2018-09-12 7:37 p.m., John Baldwin wrote:
>>> This should include most of the suggested documentation fixes from 
>>> the
>>> first series.  It also adds an additional patch that attempts to tidy 
>>> up
>>> some of the other "info proc" documentation.  One change I haven't 
>>> made
>>> (wasn't sure if it was still desired) was if we wanted to replace the
>>> specific annotations on individual 'info proc' subcommands about 
>>> which
>>> OS's supported those commands with a single, more general statement 
>>> that
>>> commands may only be supported on a subset of systems supported by 
>>> GDB.
>>> 
>>> I've moved more of the shared code for generating the 'info proc 
>>> files'
>>> output to fbsd-tdep.c.
>>> 
>>> One open question still from the first series is if GDB can assume 
>>> the
>>> presence of routines like 'inet_ntoa' and 'inet_ntop' for formatting
>>> IPv4 and IPv6 addresses.  It seems GDB does assume the presence of
>>> newer routines (e.g. getaddrinfo()) on POSIX systems in other places
>>> (e.g. ser-tcp.c), though in those places we use different APIs for
>>> Win32.
>> 
>> There seems to be a gnulib module for that:
>> 
>> https://www.gnu.org/software/gnulib/manual/html_node/inet_005fntop.html
>> 
>> There is a page for inet_ntoa too:
>> 
>> https://www.gnu.org/software/gnulib/manual/html_node/inet_005fntoa.html
>> 
>> but there is no gnulib module for it.  Probably because we can always
>> use inet_ntop instead?
> 
> Yes, inet_ntop is sufficient for both.  I had grepp'ed for 'ntoa' and
> 'ntop' in gdb/gnulib and didn't find any matches, so I assumed that
> meant there wasn't a module.  Is gdb/gnulib a subset of the actual 
> gnulib
> and new bits are imported on demand?
> 
> Hmm, reading update-gnulib.sh, it seems so.  I'll look at what is 
> involved
> in doing that.

You probably have it figured out by now, but for completeness: the 
gnulib directory in gdb is indeed a subset of the complete gnulib.  
gnulib comes with a tool (gnulib-tool) to create a package containing 
only the modules you want, and their dependencies.  update-gnulib.sh in 
our codebase is just a wrapper around that.

So it should just be a matter of adding the inet_ntop module in 
update-gnulib.sh and running it.

Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2018-09-17 17:17 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-12 23:37 [PATCH v2 0/6] Add a new 'info proc files' command John Baldwin
2018-09-12 23:37 ` [PATCH v2 2/6] Add a new 'info proc files' subcommand of 'info proc' John Baldwin
2018-09-12 23:37 ` [PATCH v2 6/6] Make the "info proc" documentation more consistent John Baldwin
2018-09-13 13:40   ` Eli Zaretskii
2018-09-12 23:37 ` [PATCH v2 1/6] Use KF_PATH to verify the size of a struct kinfo_file John Baldwin
2018-09-12 23:37 ` [PATCH v2 5/6] Document the 'info proc files' command John Baldwin
2018-09-13 13:39   ` Eli Zaretskii
2018-09-12 23:37 ` [PATCH v2 3/6] Add support for 'info proc files' on FreeBSD core dumps John Baldwin
2018-09-16  2:27   ` Simon Marchi
2018-09-17 16:54     ` John Baldwin
2018-09-12 23:43 ` [PATCH v2 4/6] Support 'info proc files' on live FreeBSD processes John Baldwin
2018-09-16  2:34 ` [PATCH v2 0/6] Add a new 'info proc files' command Simon Marchi
2018-09-17 16:59   ` John Baldwin
2018-09-17 17:17     ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).