[PATCH v4] Print ip and port associated with descriptor with -yy

Dmitry V. Levin ldv at altlinux.org
Sat Aug 9 11:12:07 UTC 2014


On Fri, Aug 08, 2014 at 12:09:20PM +0530, zubin.mithra at gmail.com wrote:
> * defs.h: Add Add header files netinet/in.h, sys/socket.h,
> arpa/inet.h, linux/netlink.h and linux/inet_diag.h.
>   Change type of show_fd_path to unsigned int.
>   Add macros SOCK_DIAG_BY_FAMILY, SOCKET_BUFFER_SIZE.
>   Add structs sock_diag_req, inet_diag_req_v2.
> * strace.c (init): Change usage of show_fd_path.
> * util.c (parse_response): New function to parse and
> print ip, port from a message response.
> (send_query): New function.
> (receive_responses): New function.
> (printsockdetails): New function.
> (printfd): Modified to use printsockdetails.
> 
> Signed-off-by: Zubin Mithra <zubin.mithra at gmail.com>
> ---
>  defs.h   |  24 ++++++++++-
>  strace.c |   4 +-
>  util.c   | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  3 files changed, 172 insertions(+), 4 deletions(-)
> 
> diff --git a/defs.h b/defs.h
> index 1a3b483..6959cdb 100644
> --- a/defs.h
> +++ b/defs.h
> @@ -67,6 +67,11 @@
>  #include <time.h>
>  #include <sys/time.h>
>  #include <sys/syscall.h>
> +#include <netinet/in.h>
> +#include <sys/socket.h>
> +#include <arpa/inet.h>
> +#include <linux/netlink.h>
> +#include <linux/inet_diag.h>

defs.h is included by every translation unit, so please do not include new
headers there.

> @@ -580,6 +585,23 @@ extern unsigned os_release;
>  #undef KERNEL_VERSION
>  #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
>  
> +#define SOCK_DIAG_BY_FAMILY 20

This macro is defined by <linux/sock_diag.h>

> +#define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L)

We have get_pagesize() defined in mem.c - please export and use it
instead.

> +struct sock_diag_req {
> +	__u8    sdiag_family;
> +	__u8    sdiag_protocol;
> +};

This structure is defined by <linux/sock_diag.h>

> +struct inet_diag_req_v2 {
> +	__u8	sdiag_family;
> +	__u8	sdiag_protocol;
> +	__u8	idiag_ext;
> +	__u8	pad;
> +	__u32	idiag_states;
> +	struct inet_diag_sockid id;
> +};

This structure is defined by <linux/inet_diag.h>

> diff --git a/util.c b/util.c
> index 33482d5..5608449 100644
> --- a/util.c
> +++ b/util.c
> @@ -404,13 +404,159 @@ printnum_int(struct tcb *tcp, long addr, const char *fmt)
>  	tprints("]");
>  }
>  
> +int
> +parse_response(struct inet_diag_msg *diag_msg, int inodenr) {
[...]
> +int
> +send_query(int sockfd, int proto, int family) {
[...]
> +int
> +parse_responses(int sockfd, int inodenr) {
[...]
> +/* Given an inode number of a socket, print out the details
> + * of the remote ip address and remote port */
> +int
> +printsockdetails(int inodenr)
[...]

Please create a new translation unit and move all this new code
(functions and declarations for them) there.

Note that idiag_inode has type uint32_t.

>  void
>  printfd(struct tcb *tcp, int fd)
>  {
>  	char path[PATH_MAX + 1];
>  
> -	if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0)
> +	if (show_fd_path == 1 && getfdpath(tcp, fd, path, sizeof(path)) >= 0)
>  		tprintf("%d<%s>", fd, path);
> +	else if (show_fd_path > 1 && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {

	if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
		static const char socket_prefix[] = "socket:[";
		const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
		size_t path_len;

		if (show_fd_path > 1 &&
		    strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
		    path[(path_len = strlen(path)) - 1] == ']') {
			unsigned long inodenr;

			path[path_len - 1] = '\0';
			inodenr = strtoul(path + socket_prefix_len, NULL, 10);
			tprintf("%d<", fd);
			if (printsockdetails(inodenr) < 0) {
				path[path_len - 1] = ']';
				tprints(path);
			}
			tprints(">");
		} else {
		    	tprintf("%d<%s>", fd, path);
		}
	} else
		tprintf("%d", fd);


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20140809/480e887f/attachment.bin>


More information about the Strace-devel mailing list