[PATCH v5 2/4] Add eventfd option to --decode-fds

Sahil Siddiq icegambit91 at gmail.com
Thu May 23 14:43:32 UTC 2024


Having an eventfd option for --decode-fds will help
examine details of an eventfd object associated with
an eventfd file descriptor.

* src/filter_qualify.c
  (decode_fd_str_to_uint): Add "eventfd" option.
* src/number_set.h: Add DECODE_FD_EVENTFD.
* src/strace.c: Add "eventfd" option.
* src/util.c: Parse eventfd related details.

Signed-off-by: Sahil Siddiq <icegambit91 at gmail.com>
---
Changes v3 -> v5:
- src/util.c:
  (parse_fdinfo_efd_counter):
  - Improve handling of newline/whitespace.
  - Improve handling of data buffer.
  (printeventfd): Free efd_counter buffer.

Changes v4 -> v5: None

 src/filter_qualify.c |   1 +
 src/number_set.h     |   1 +
 src/strace.c         |   3 +-
 src/util.c           | 100 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 104 insertions(+), 1 deletion(-)

diff --git a/src/filter_qualify.c b/src/filter_qualify.c
index be383a5c9..0b9440675 100644
--- a/src/filter_qualify.c
+++ b/src/filter_qualify.c
@@ -105,6 +105,7 @@ decode_fd_str_to_uint(const char *str)
 		{ DECODE_FD_PATH,      "path" },
 		{ DECODE_FD_SOCKET,    "socket" },
 		{ DECODE_FD_DEV,       "dev" },
+		{ DECODE_FD_EVENTFD,   "eventfd" },
 		{ DECODE_FD_PIDFD,     "pidfd" },
 		{ DECODE_FD_SIGNALFD,  "signalfd" },
 	};
diff --git a/src/number_set.h b/src/number_set.h
index 0ce2b2db6..d28ac0cdd 100644
--- a/src/number_set.h
+++ b/src/number_set.h
@@ -75,6 +75,7 @@ enum decode_fd_bits {
 	DECODE_FD_PATH,
 	DECODE_FD_SOCKET,
 	DECODE_FD_DEV,
+	DECODE_FD_EVENTFD,
 	DECODE_FD_PIDFD,
 	DECODE_FD_SIGNALFD,
 
diff --git a/src/strace.c b/src/strace.c
index 1c2e9a8b1..12670897d 100644
--- a/src/strace.c
+++ b/src/strace.c
@@ -379,7 +379,8 @@ Output format:\n\
                  print exit reason of kvm vcpu\n\
   -e decode-fds=SET, --decode-fds=SET\n\
                  what kinds of file descriptor information details to decode\n\
-     details:    dev (device major/minor for block/char device files)\n\
+     details:    dev (device major/minor for block/char device files),\n\
+                 eventfd (associated eventfd object details for eventfds),\n\
                  path (file path),\n\
                  pidfd (associated PID for pidfds),\n\
                  socket (protocol-specific information for socket descriptors),\n\
diff --git a/src/util.c b/src/util.c
index 77c7d7c70..0bf4f57a2 100644
--- a/src/util.c
+++ b/src/util.c
@@ -886,6 +886,103 @@ printsignalfd(pid_t pid_of_fd, int fd, const char *path)
 			   print_fdinfo_sigmask, NULL);
 }
 
+static bool
+parse_fdinfo_efd_semaphore(const char *value, void *data)
+{
+	int *efd_semaphore = data;
+	*efd_semaphore = string_to_uint_ex(value, NULL, INT_MAX, "\n");
+	return true;
+}
+
+static bool
+parse_fdinfo_efd_id(const char *value, void *data)
+{
+	int *efd_id = data;
+	*efd_id = string_to_uint_ex(value, NULL, INT_MAX, "\n");
+	return true;
+}
+
+static bool
+parse_fdinfo_efd_counter(const char *value, void *data)
+{
+	char *ptr = (char *)value;
+
+	ptr += strspn(ptr, " \t");
+	ptr[strcspn(ptr, "\n")] = '\0';
+
+	if (*ptr == '\0')
+		ptr = NULL;
+
+	*(char **)data = xstrdup(ptr);
+	return true;
+}
+
+static bool
+printeventfd(pid_t pid_of_fd, int fd, const char *path)
+{
+	static const char eventfd_path[] = "anon_inode:[eventfd]";
+	static const char efd_counter_pfx[] = "eventfd-count:";
+	static const char efd_id_pfx[] = "eventfd-id:";
+	static const char efd_semaphore_pfx[] = "eventfd-semaphore:";
+
+	if (strcmp(path, eventfd_path))
+		return false;
+
+	char *efd_counter = NULL;
+	int efd_id = -1;
+	int efd_semaphore = -1;
+
+	struct scan_fdinfo fdinfo_lines[] = {
+		{
+			.search_pfx = efd_counter_pfx,
+			.search_pfx_len = sizeof(efd_counter_pfx) - 1,
+			.fn = parse_fdinfo_efd_counter,
+			.data = &efd_counter
+		},
+		{
+			.search_pfx = efd_id_pfx,
+			.search_pfx_len = sizeof(efd_id_pfx) - 1,
+			.fn = parse_fdinfo_efd_id,
+			.data = &efd_id
+		},
+		{
+			.search_pfx = efd_semaphore_pfx,
+			.search_pfx_len = sizeof(efd_semaphore_pfx) - 1,
+			.fn = parse_fdinfo_efd_semaphore,
+			.data = &efd_semaphore
+		}
+	};
+
+	scan_fdinfo_lines(pid_of_fd, fd, fdinfo_lines, ARRAY_SIZE(fdinfo_lines));
+
+	if (efd_counter) {
+		tprint_associated_info_begin();
+		tprint_struct_begin();
+		tprints_field_name("eventfd-count");
+		tprints_string("0x");
+		tprints_string(efd_counter);
+		free(efd_counter);
+
+		if (efd_id != -1) {
+			tprint_struct_next();
+			tprints_field_name("eventfd-id");
+			PRINT_VAL_U(efd_id);
+
+			if (efd_semaphore != -1) {
+				tprint_struct_next();
+				tprints_field_name("eventfd-semaphore");
+				PRINT_VAL_U(efd_semaphore);
+			}
+		}
+
+		tprint_struct_end();
+		tprint_associated_info_end();
+	} else
+		print_string_in_angle_brackets(path);
+
+	return true;
+}
+
 static void
 print_quoted_string_in_angle_brackets(const char *str, const bool deleted)
 {
@@ -914,6 +1011,9 @@ printfd_pid_with_finfo(struct tcb *tcp, pid_t pid, int fd, const struct finfo *f
 		if (is_number_in_set(DECODE_FD_DEV, decode_fd_set) &&
 		    printdev(tcp, fd, path, finfo))
 			goto printed;
+		if (is_number_in_set(DECODE_FD_EVENTFD, decode_fd_set) &&
+		    printeventfd(pid, fd, path))
+			goto printed;
 		if (is_number_in_set(DECODE_FD_PIDFD, decode_fd_set) &&
 		    printpidfd(pid, fd, path))
 			goto printed;
-- 
2.45.0



More information about the Strace-devel mailing list