[PATCH] Print protocol name of socket descriptors with -yy option

Masatake YAMATO yamato at redhat.com
Wed Nov 19 14:32:32 UTC 2014


When -yy option is specified other than inet socket
were printed as <socket:[INODE]>.

e.g.

     # ./strace -e recvmsg -yy ping 127.0.0.1 > /dev/null
     ...
     recvmsg(3<socket:[43222052]>, ...
     ...

This patch makes the output more informative; instead of
printing "socket" strace prints the name of protocol behind
the socket descriptor.

e.g.

     recvmsg(3<RAW:[43222052]>, ...

     recvmsg(1<TCP:[...
     recvmsg(2<UDP:[...
     recvmsg(3<UNIX:[...
     recvmsg(4<NETLINK:[...

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 tests/net-yy-accept.awk  |  6 +++---
 tests/net-yy-connect.awk |  4 ++--
 util.c                   | 29 ++++++++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/tests/net-yy-accept.awk b/tests/net-yy-accept.awk
index 3ea4afe..ac3c19a 100644
--- a/tests/net-yy-accept.awk
+++ b/tests/net-yy-accept.awk
@@ -9,7 +9,7 @@ BEGIN {
   r_i = "[1-9][0-9]*"
   r_port = "[1-9][0-9][0-9][0-9]+"
   r_localhost = "127\\.0\\.0\\.1"
-  r_bind = "^bind\\(0<socket:\\[(" r_i ")\\]>, {sa_family=AF_INET, sin_port=htons\\(0\\), sin_addr=inet_addr\\(\"" r_localhost "\"\\)}, " r_i "\\) += 0$"
+  r_bind = "^bind\\(0<(TCP|socket):\\[(" r_i ")\\]>, {sa_family=AF_INET, sin_port=htons\\(0\\), sin_addr=inet_addr\\(\"" r_localhost "\"\\)}, " r_i "\\) += 0$"
   r_listen = "^/$"
   r_getsockname = "^getsockname\\(0<" r_localhost ":(" r_port ")>, {sa_family=AF_INET, sin_port=htons\\((" r_port ")\\), sin_addr=inet_addr\\(\"" r_localhost "\"\\)}, \\[" r_i "\\]\\) += 0$"
   r_accept = "^/$"
@@ -23,8 +23,8 @@ NR == 1 && /^socket\(PF_INET, SOCK_STREAM, IPPROTO_IP\) += 0$/ {next}
 
 NR == 2 {
   if (match($0, r_bind, a)) {
-    inode = a[1]
-    r_listen = "^listen\\(0<socket:\\[" inode "\\]>, 5\\) += 0$"
+    inode = a[2]
+    r_listen = "^listen\\(0<(TCP|socket):\\[" inode "\\]>, 5\\) += 0$"
     next
   }
 }
diff --git a/tests/net-yy-connect.awk b/tests/net-yy-connect.awk
index 18c1a28..c7c63af 100644
--- a/tests/net-yy-connect.awk
+++ b/tests/net-yy-connect.awk
@@ -8,7 +8,7 @@ BEGIN {
   r_i = "[1-9][0-9]*"
   r_port = "[1-9][0-9][0-9][0-9]+"
   r_localhost = "127\\.0\\.0\\.1"
-  r_connect = "^connect\\(0<socket:\\[" r_i "\\]>, {sa_family=AF_INET, sin_port=htons\\((" r_port ")\\), sin_addr=inet_addr\\(\"" r_localhost "\"\\)}, " r_i ") += 0$"
+  r_connect = "^connect\\(0<(TCP|socket):\\[" r_i "\\]>, {sa_family=AF_INET, sin_port=htons\\((" r_port ")\\), sin_addr=inet_addr\\(\"" r_localhost "\"\\)}, " r_i ") += 0$"
   r_send = "^/$"
   r_sendto = "^/$"
   r_close = "^/$"
@@ -18,7 +18,7 @@ NR == 1 && /^socket\(PF_INET, SOCK_STREAM, IPPROTO_IP\) += 0$/ {next}
 
 NR == 2 {
   if (match($0, r_connect, a)) {
-    port_r = a[1]
+    port_r = a[2]
     r_send = "^send\\(0<" r_localhost ":(" r_port ")->" r_localhost ":" port_r ">, \"data\", 4, MSG_DONTROUTE\\) += 4$"
     r_sendto = "^sendto\\(0<" r_localhost ":(" r_port ")->" r_localhost ":" port_r ">, \"data\", 4, MSG_DONTROUTE, NULL, 0\\) += 4$"
     next
diff --git a/util.c b/util.c
index b13f3dc..c59fff0 100644
--- a/util.c
+++ b/util.c
@@ -35,6 +35,7 @@
 #include <sys/user.h>
 #include <sys/param.h>
 #include <fcntl.h>
+#include <sys/xattr.h>
 #if HAVE_SYS_UIO_H
 # include <sys/uio.h>
 #endif
@@ -420,6 +421,23 @@ printnum_int(struct tcb *tcp, long addr, const char *fmt)
 	tprints("]");
 }
 
+
+static char *
+getfdproto(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
+{
+	char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
+
+	if (fd < 0)
+		return NULL;
+
+	sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
+	if (getxattr(path, "system.sockprotoname", buf, bufsize) <= 0)
+		return NULL;
+	buf[bufsize - 1] = '\0';
+
+	return buf;
+}
+
 void
 printfd(struct tcb *tcp, int fd)
 {
@@ -433,10 +451,19 @@ printfd(struct tcb *tcp, int fd)
 		    strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
 		    path[(path_len = strlen(path)) - 1] == ']') {
 			unsigned long inodenr;
+#define PROTO_NAME_LEN (32 + 1)
+			char proto_buf[PROTO_NAME_LEN];
+			char *proto;
 			inodenr = strtoul(path + socket_prefix_len, NULL, 10);
+			proto = getfdproto(tcp, fd, proto_buf, PROTO_NAME_LEN);
 			tprintf("%d<", fd);
 			if (!print_sockaddr_by_inode(inodenr))
-				tprints(path);
+			{
+				if (proto)
+					tprintf("%s:[%lu]", proto, inodenr);
+				else
+					tprints(path);
+			}
 			tprints(">");
 		} else {
 			tprintf("%d<%s>", fd, path);
-- 
1.9.3





More information about the Strace-devel mailing list