[RFC PATCH v3 3/5] Add eventfd option to --decode-fds

Dmitry V. Levin ldv at strace.io
Mon May 13 12:04:19 UTC 2024


On Fri, May 10, 2024 at 10:44:09AM +0530, Sahil Siddiq wrote:
> Having an eventfd option for --decode-fds will help
> examine the eventfd counter 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 v2 -> v3:
> - src/util.c:
>   (parse_fdinfo_efd_semaphore): New function.
>   (parse_fdinfo_efd_counter): Treat efd_counter as string.
>   (printeventfd): Refactor to use "struct fdinfo".
> 
>  src/filter_qualify.c |   1 +
>  src/number_set.h     |   1 +
>  src/strace.c         |   3 +-
>  src/util.c           | 112 +++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 116 insertions(+), 1 deletion(-)
> 
> diff --git a/src/filter_qualify.c b/src/filter_qualify.c
> index 87c1a6106..a79045468 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 c5c9728aa..eea058f07 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 f558d342c..2408664a8 100644
> --- a/src/util.c
> +++ b/src/util.c
> @@ -895,6 +895,115 @@ printsignalfd(pid_t pid_of_fd, int fd, const char *path)
>  	return scan_fdinfo(pid_of_fd, fd, fdinfo_lines, ARRAY_SIZE(fdinfo_lines));
>  }
>  
> +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", 10);
> +	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", 10);
> +	return true;
> +}
> +
> +static bool
> +parse_fdinfo_efd_counter(const char *value, void *data)
> +{
> +	char *ptr = (char *)value;
> +	size_t len = strlen(ptr);
> +
> +	/*
> +	 * Strip trailing newlines.
> +	 */
> +	for (; len > 0; --len) {
> +		if (ptr[len - 1] != '\n')
> +			break;
> +	}
> +	ptr[len] = '\0';

You could just strip the very first newline, it's all the same.

> +
> +	/*
> +	 * Strip leading whitespace.
> +	 */
> +	while (*ptr != '\0' && (*ptr == ' ' || (unsigned int)(*ptr - 9) <= 4))
> +		++ptr;

It's much more convenient to rewrite all the above this way:

	ptr += strspn(ptr, " \t");
	ptr[strcspn(ptr, "\n")] = '\0';

> +
> +	if (*ptr == '\0')
> +		ptr = NULL;
> +
> +	*(char **)data = ptr;

Sorry, you cannot do this because ptr points to a memory that is allocated
by getline(), and it will be freed no later than at the end of
scan_fdinfo().


-- 
ldv


More information about the Strace-devel mailing list