[PATCH] Add decoding of sockets descriptor 'paths' for network calls.

Philippe Ombredanne pombredanne at nexb.com
Sat Feb 1 17:57:45 UTC 2014


* pathtrace.c: Add decoding for network-related file descriptors to
pathtrace_match with an fdmatch call for all TRACE_NETWORK calls that
have sockfd as arg[0]. No decoding for socket and socketpair calls that
take no descriptor.
* net.c: Add printing of decoded socket descriptors for syscalls that
have such desc as arg[0] instead of printing a bare int.
* tests/net-fd: New test file to test proper decoding of socket fd's.
* tests/Makefile.am (TESTS): Add net-fd test to test suite.

Signed-off-by: Philippe Ombredanne <pombredanne at nexb.com>
---
 net.c             |   43 +++++++++++++++++++++++++++++--------------
 pathtrace.c       |   12 +++++++-----
 tests/Makefile.am |    2 +-
 tests/net-fd      |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 84 insertions(+), 20 deletions(-)
 create mode 100755 tests/net-fd

diff --git a/net.c b/net.c
index dd1e446..b6c8cc9 100644
--- a/net.c
+++ b/net.c
@@ -1711,7 +1711,8 @@ int
 sys_bind(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		printsock(tcp, tcp->u_arg[1], tcp->u_arg[2]);
 		tprintf(", %lu", tcp->u_arg[2]);
 	}
@@ -1728,7 +1729,9 @@ int
 sys_listen(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, %lu", tcp->u_arg[0], tcp->u_arg[1]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
+		tprintf("%lu", tcp->u_arg[1]);
 	}
 	return 0;
 }
@@ -1737,7 +1740,8 @@ static int
 do_accept(struct tcb *tcp, int flags_arg)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		return 0;
 	}
 	if (!tcp->u_arg[2])
@@ -1777,7 +1781,8 @@ int
 sys_send(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
 		tprintf(", %lu, ", tcp->u_arg[2]);
 		/* flags */
@@ -1790,7 +1795,8 @@ int
 sys_sendto(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
 		tprintf(", %lu, ", tcp->u_arg[2]);
 		/* flags */
@@ -1810,7 +1816,8 @@ int
 sys_sendmsg(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		printmsghdr(tcp, tcp->u_arg[1], (unsigned long) -1L);
 		/* flags */
 		tprints(", ");
@@ -1824,7 +1831,8 @@ sys_sendmmsg(struct tcb *tcp)
 {
 	if (entering(tcp)) {
 		/* sockfd */
-		tprintf("%d, ", (int) tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		if (!verbose(tcp)) {
 			tprintf("%#lx, %u, ",
 				tcp->u_arg[1], (unsigned int) tcp->u_arg[2]);
@@ -1843,7 +1851,8 @@ int
 sys_recv(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 	} else {
 		if (syserror(tcp))
 			tprintf("%#lx", tcp->u_arg[1]);
@@ -1862,7 +1871,8 @@ sys_recvfrom(struct tcb *tcp)
 	int fromlen;
 
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 	} else {
 		if (syserror(tcp)) {
 			tprintf("%#lx, %lu, %lu, %#lx, %#lx",
@@ -1906,7 +1916,8 @@ int
 sys_recvmsg(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 	} else {
 		if (syserror(tcp) || !verbose(tcp))
 			tprintf("%#lx", tcp->u_arg[1]);
@@ -1926,7 +1937,8 @@ sys_recvmmsg(struct tcb *tcp)
 	static char str[5 + TIMESPEC_TEXT_BUFSIZE];
 
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		if (verbose(tcp)) {
 			sprint_timespec(str, tcp, tcp->u_arg[4]);
 			/* Abusing tcp->auxstr as temp storage.
@@ -1976,7 +1988,8 @@ int
 sys_shutdown(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
 	}
 	return 0;
@@ -2077,7 +2090,8 @@ int
 sys_getsockopt(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		printxval(socketlayers, tcp->u_arg[1], "SOL_???");
 		tprints(", ");
 		switch (tcp->u_arg[1]) {
@@ -2343,7 +2357,8 @@ int
 sys_setsockopt(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		tprintf("%ld, ", tcp->u_arg[0]);
+		printfd(tcp, tcp->u_arg[0]);
+		tprints(", ");
 		printsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
 			      tcp->u_arg[3], tcp->u_arg[4]);
 		tprintf(", %lu", tcp->u_arg[4]);
diff --git a/pathtrace.c b/pathtrace.c
index 03f6681..e78508e 100644
--- a/pathtrace.c
+++ b/pathtrace.c
@@ -80,7 +80,7 @@ fdmatch(struct tcb *tcp, int fd)
 
 /*
  * Add a path to the set we're tracing.
- * Secifying NULL will delete all paths.
+ * Specifying NULL will delete all paths.
  */
 static void
 storepath(const char *path)
@@ -158,7 +158,7 @@ pathtrace_match(struct tcb *tcp)
 
 	s = tcp->s_ent;
 
-	if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC)))
+	if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
 		return 0;
 
 	/*
@@ -339,11 +339,13 @@ pathtrace_match(struct tcb *tcp)
 	    s->sys_func == sys_timerfd_settime ||
 	    s->sys_func == sys_timerfd_gettime ||
 	    s->sys_func == sys_epoll_create ||
+	    s->sys_func == sys_socket ||
+	    s->sys_func == sys_socketpair ||
 	    strcmp(s->sys_name, "fanotify_init") == 0)
 	{
 		/*
-		 * These have TRACE_FILE or TRACE_DESCRIPTOR set, but they
-		 * don't have any file descriptor or path args to test.
+		 * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
+		 * but they don't have any file descriptor or path args to test.
 		 */
 		return 0;
 	}
@@ -353,7 +355,7 @@ pathtrace_match(struct tcb *tcp)
 	 * been handled is to just check arg[0].
 	 */
 
-	if (s->sys_flags & TRACE_FILE)
+	if (s->sys_flags & (TRACE_FILE | TRACE_NETWORK))
 		return upathmatch(tcp, tcp->u_arg[0]);
 
 	if (s->sys_flags & TRACE_DESC)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d8262f0..20736e3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,7 +4,7 @@ AM_CFLAGS = $(WARN_CFLAGS)
 
 check_PROGRAMS = net-accept-connect set_ptracer_any sigaction
 
-TESTS = ptrace_setoptions strace-f qual_syscall sigaction.sh stat net \
+TESTS = ptrace_setoptions strace-f qual_syscall sigaction.sh stat net net-fd \
 	detach-sleeping detach-stopped detach-running
 
 LOG_COMPILER = $(srcdir)/run.sh
diff --git a/tests/net-fd b/tests/net-fd
new file mode 100755
index 0000000..0f6b4e2
--- /dev/null
+++ b/tests/net-fd
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+# Check how network syscalls are traced when decoding socket descriptors
+
+. "${srcdir=.}/init.sh"
+
+# strace -y is implemented using /proc/self/fd
+[ -d /proc/self/fd/ ] ||
+	framework_skip_ '/proc/self/fd/ is not available'
+
+check_prog grep
+check_prog rm
+
+rm -f $LOG.*
+
+./net-accept-connect ||
+	fail_ 'net-accept-connect failed'
+
+# using -y to test socket descriptors 'paths' decoding
+args="-tt -ff -y -o $LOG -enetwork ./net-accept-connect"
+$STRACE $args ||
+	fail_ "strace $args failed"
+
+"$srcdir"/../strace-log-merge $LOG > $LOG || {
+	cat $LOG
+	fail_ 'strace-log-merge failed'
+}
+
+rm -f $LOG.*
+
+grep_log()
+{
+	local syscall="$1"; shift
+	local prefix='[1-9][0-9]* +[0-9]+:[0-9]+:[0-9]+\.[0-9]+ +'
+
+	LC_ALL=C grep -E -x "$prefix$syscall$@" $LOG > /dev/null || {
+		cat $LOG
+		fail_ "strace -enetwork failed to trace \"$syscall\" properly"
+	}
+}
+grep_log bind '\(0<socket:\[[0-9]+\]>, \{sa_family=AF_(LOCAL|UNIX|FILE), sun_path="local-stream"\}, 15\) += 0'
+grep_log listen '\(0<socket:\[[0-9]+\]>, 5\) += 0'
+grep_log getsockname '\(0<socket:\[[0-9]+\]>, \{sa_family=AF_(LOCAL|UNIX|FILE), sun_path="local-stream"\}, \[15\]\) += 0'
+grep_log accept '\(0<socket:\[[0-9]+\]>, \{sa_family=AF_(LOCAL|UNIX|FILE), NULL\}, \[2\]\) += 1'
+grep_log connect '\(1<socket:\[[0-9]+\]>, \{sa_family=AF_(LOCAL|UNIX|FILE), sun_path="local-stream"\}, 15\) += 0'
+
+exit 0
-- 
1.7.9.5





More information about the Strace-devel mailing list