[PATCHv2] Print protocol name of socket descriptors with -yy option
Masatake YAMATO
yamato at redhat.com
Sat Nov 22 10:03:33 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:[...
Changes:
* configure.ac: Check sys/xattr.h.
* tests/net-yy-accept.awk, net-yy-connect.awk: Accept both
TCP and socket as description of a socket descriptor.
* util.c (sys/xattr.h): include it only if HAVE_SYS_XATTR_H is defined.
(getfdproto): New function.
(prntfd): Use getfdproto to get the protocol behind a given socket.
Change in v2 patch (suggested by ldv):
* ask kernel to fill the buffer for protocol name up to 31 bytes.
* put nul char as C string terminator at the place calculated from
the filled length returned from the kernel.
* call getfdproto only if print_sockaddr_by_inode fails to make
a description for a given socket.
Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
configure.ac | 1 +
tests/net-yy-accept.awk | 6 +++---
tests/net-yy-connect.awk | 4 ++--
util.c | 41 ++++++++++++++++++++++++++++++++++++++++-
4 files changed, 46 insertions(+), 6 deletions(-)
diff --git a/configure.ac b/configure.ac
index f55af46..c54d586 100644
--- a/configure.ac
+++ b/configure.ac
@@ -252,6 +252,7 @@ AC_CHECK_HEADERS(m4_normalize([
sys/reg.h
sys/uio.h
sys/vfs.h
+ sys/xattr.h
]))
AC_CHECK_HEADERS([linux/icmp.h linux/in6.h linux/netlink.h linux/if_packet.h],
[], [], [#include <stddef.h>
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..8a8e160 100644
--- a/util.c
+++ b/util.c
@@ -35,6 +35,9 @@
#include <sys/user.h>
#include <sys/param.h>
#include <fcntl.h>
+#if HAVE_SYS_XATTR_H
+#include <sys/xattr.h>
+#endif
#if HAVE_SYS_UIO_H
# include <sys/uio.h>
#endif
@@ -420,6 +423,33 @@ printnum_int(struct tcb *tcp, long addr, const char *fmt)
tprints("]");
}
+
+static char *
+getfdproto(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
+{
+#if HAVE_SYS_XATTR_H
+ int r;
+ 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);
+ r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
+ if (r <= 0)
+ return NULL;
+ else {
+ /* This is for protecting from the case that kernel
+ side doesn't put a nul terminator at the end of the
+ passed buffer. */
+ buf[r] = '\0';
+ return buf;
+ }
+#else
+ return NULL;
+#endif
+}
+
void
printfd(struct tcb *tcp, int fd)
{
@@ -436,7 +466,16 @@ printfd(struct tcb *tcp, int fd)
inodenr = strtoul(path + socket_prefix_len, NULL, 10);
tprintf("%d<", fd);
if (!print_sockaddr_by_inode(inodenr))
- tprints(path);
+ {
+#define PROTO_NAME_LEN 32
+ char proto_buf[PROTO_NAME_LEN];
+ char *proto;
+ proto = getfdproto(tcp, fd, proto_buf, PROTO_NAME_LEN);
+ 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