[GSOC 2014][PATCH 3/4] JSON: Add basic support for syscalls in io.c

Zhu YangMin zym0017d at gmail.com
Wed Jun 18 16:29:49 UTC 2014


The basic means that all the arguments are simply wrapped in
a string value even for those complicated arguments printed by
specific functions such as printflags(). We need to do more work
to change those functions to be more like JSON-style.

* io.c(sys_read, sys_write, sys_readv, sys_writev,
sys_pread, sys_pwrite, sys_preadv, sys_pwritev,
sys_sendfile, sys_sendfile64, sys_tee, sys_splice,
sys_vmsplice, sys_ioctl): Modified to use the hook
framework to support printing in JSON format. Note: The
support of ioctl() is very limited and need to be improved.
---
 io.c | 205 +++++++++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 150 insertions(+), 55 deletions(-)

diff --git a/io.c b/io.c
index fea1218..204fa7b 100644
--- a/io.c
+++ b/io.c
@@ -37,15 +37,27 @@
 int
 sys_read(struct tcb *tcp)
 {
+	JSON_BEGIN_META_MODE;
+
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
+
+		json_event(1, EVENT_ARG | SEPARATOR);
 	} else {
-		if (syserror(tcp))
+		json_type_event first;
+
+		if (syserror(tcp)) {
 			tprintf("%#lx", tcp->u_arg[1]);
-		else
-			printstr(tcp, tcp->u_arg[1], tcp->u_rval);
+			first = EVENT_ERROR;
+		}
+		else {
+			JSON_META( printstr(tcp, tcp->u_arg[1], tcp->u_rval) );
+			first = EVENT_ARG;
+		}
 		tprintf(", %lu", tcp->u_arg[2]);
+
+		json_event(2, first | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -54,10 +66,14 @@ int
 sys_write(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_BEGIN_META_MODE;
+
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
-		printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
+		JSON_META( printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]); );
 		tprintf(", %lu", tcp->u_arg[2]);
+
+		json_event(3, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -148,17 +164,23 @@ tprint_iov(struct tcb *tcp, unsigned long len, unsigned long addr, int decode_io
 int
 sys_readv(struct tcb *tcp)
 {
+	JSON_BEGIN_META_MODE;
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
+
+		json_event(1, EVENT_ARG | SEPARATOR);
 	} else {
 		if (syserror(tcp)) {
-			tprintf("%#lx, %lu",
-					tcp->u_arg[1], tcp->u_arg[2]);
+			tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
+
+			json_event(2, EVENT_ARG | SEPARATOR, EVENT_ARG);
 			return 0;
 		}
-		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+		JSON_META( tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1); );
 		tprintf(", %lu", tcp->u_arg[2]);
+
+		json_event(2, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -167,10 +189,14 @@ int
 sys_writev(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_BEGIN_META_MODE;
+
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
-		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+		JSON_META( tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1); );
 		tprintf(", %lu", tcp->u_arg[2]);
+
+		json_event(3, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -190,16 +216,24 @@ sys_writev(struct tcb *tcp)
 int
 sys_pread(struct tcb *tcp)
 {
+	JSON_BEGIN_META_MODE;
+
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_META(printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
+
+		json_event(1, EVENT_ARG | SEPARATOR);
 	} else {
-		if (syserror(tcp))
+		if (syserror(tcp)) {
 			tprintf("%#lx", tcp->u_arg[1]);
-		else
-			printstr(tcp, tcp->u_arg[1], tcp->u_rval);
+		}
+		else {
+			JSON_META( printstr(tcp, tcp->u_arg[1], tcp->u_rval); );
+		}
 		tprintf(", %lu, ", tcp->u_arg[2]);
-		printllval_aligned(tcp, "%llu", PREAD_OFFSET_ARG);
+		JSON_META( printllval_aligned(tcp, "%llu", PREAD_OFFSET_ARG); );
+
+		json_event(3, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -208,11 +242,15 @@ int
 sys_pwrite(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_BEGIN_META_MODE;
+
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
-		printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
+		JSON_META( printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]); );
 		tprintf(", %lu, ", tcp->u_arg[2]);
-		printllval_aligned(tcp, "%llu", PREAD_OFFSET_ARG);
+		JSON_META( printllval_aligned(tcp, "%llu", PREAD_OFFSET_ARG); );
+
+		json_event(4, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -221,17 +259,25 @@ sys_pwrite(struct tcb *tcp)
 int
 sys_preadv(struct tcb *tcp)
 {
+	JSON_BEGIN_META_MODE;
+
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
+
+		json_event(1, EVENT_ARG | SEPARATOR);
 	} else {
 		if (syserror(tcp)) {
 			tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
+
+			json_event(2, EVENT_ARG | SEPARATOR, EVENT_ARG);
 			return 0;
 		}
-		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+		JSON_META( tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1); );
 		tprintf(", %lu, ", tcp->u_arg[2]);
-		printllval_unaligned(tcp, "%llu", 3);
+		JSON_META( printllval_unaligned(tcp, "%llu", 3); );
+
+		json_event(3, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -240,11 +286,15 @@ int
 sys_pwritev(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_BEGIN_META_MODE;
+
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
-		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+		JSON_META( tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1); );
 		tprintf(", %lu, ", tcp->u_arg[2]);
-		printllval_unaligned(tcp, "%llu", 3);
+		JSON_META( printllval_unaligned(tcp, "%llu", 3); );
+
+		json_event(4, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -280,12 +330,16 @@ int
 sys_sendfile(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_BEGIN_META_MODE;
+
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
-		printfd(tcp, tcp->u_arg[1]);
+		JSON_META( printfd(tcp, tcp->u_arg[1]); );
 		tprints(", ");
-		print_off_t(tcp, tcp->u_arg[2]);
+		JSON_META(print_off_t(tcp, tcp->u_arg[2]); );
 		tprintf(", %lu", tcp->u_arg[3]);
+
+		json_event(4, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -307,12 +361,16 @@ int
 sys_sendfile64(struct tcb *tcp)
 {
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_BEGIN_META_MODE;
+
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
-		printfd(tcp, tcp->u_arg[1]);
+		JSON_META(printfd(tcp, tcp->u_arg[1]); );
 		tprints(", ");
-		print_loff_t(tcp, tcp->u_arg[2]);
+		JSON_META(print_loff_t(tcp, tcp->u_arg[2]); );
 		tprintf(", %lu", tcp->u_arg[3]);
+
+		json_event(4, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -323,16 +381,22 @@ int
 sys_tee(struct tcb *tcp)
 {
 	if (entering(tcp)) {
+		JSON_BEGIN_META_MODE;
+
 		/* int fd_in */
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_META(printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
+
 		/* int fd_out */
-		printfd(tcp, tcp->u_arg[1]);
-		tprints(", ");
+		JSON_META(printfd(tcp, tcp->u_arg[1]); );
+
 		/* size_t len */
-		tprintf("%lu, ", tcp->u_arg[2]);
+		tprintf(", %lu, ", tcp->u_arg[2]);
+
 		/* unsigned int flags */
-		printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
+		JSON_META( printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???"); );
+
+		json_event(4, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -341,22 +405,35 @@ int
 sys_splice(struct tcb *tcp)
 {
 	if (entering(tcp)) {
+		JSON_BEGIN_META_MODE;
+
 		/* int fd_in */
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
+
 		/* loff_t *off_in */
-		print_loff_t(tcp, tcp->u_arg[1]);
+		JSON_META(print_loff_t(tcp, tcp->u_arg[1]); );
 		tprints(", ");
+
 		/* int fd_out */
-		printfd(tcp, tcp->u_arg[2]);
+		JSON_META(printfd(tcp, tcp->u_arg[2]); );
 		tprints(", ");
+
 		/* loff_t *off_out */
-		print_loff_t(tcp, tcp->u_arg[3]);
-		tprints(", ");
+		JSON_META(print_loff_t(tcp, tcp->u_arg[3]); );
+
 		/* size_t len */
-		tprintf("%lu, ", tcp->u_arg[4]);
+		tprintf(", %lu, ", tcp->u_arg[4]);
+
 		/* unsigned int flags */
-		printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???");
+		JSON_META( printflags(splice_flags, tcp->u_arg[5], "SPLICE_F_???"); );
+
+		json_event(6, EVENT_ARG | SEPARATOR,
+					  EVENT_ARG | SEPARATOR,
+					  EVENT_ARG | SEPARATOR,
+					  EVENT_ARG | SEPARATOR,
+					  EVENT_ARG | SEPARATOR,
+					  EVENT_ARG);
 	}
 	return 0;
 }
@@ -365,14 +442,20 @@ int
 sys_vmsplice(struct tcb *tcp)
 {
 	if (entering(tcp)) {
+		JSON_BEGIN_META_MODE;
+
 		/* int fd */
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
+
 		/* const struct iovec *iov, unsigned long nr_segs */
-		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
+		JSON_META( tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1); );
 		tprintf(", %lu, ", tcp->u_arg[2]);
+
 		/* unsigned int flags */
-		printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
+		JSON_META( printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???"); );
+
+		json_event(4, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	return 0;
 }
@@ -381,23 +464,35 @@ int
 sys_ioctl(struct tcb *tcp)
 {
 	const struct_ioctlent *iop;
+	JSON_BEGIN_META_MODE;
 
 	if (entering(tcp)) {
-		printfd(tcp, tcp->u_arg[0]);
+		JSON_META( printfd(tcp, tcp->u_arg[0]); );
 		tprints(", ");
 		iop = ioctl_lookup(tcp->u_arg[1]);
 		if (iop) {
-			tprints(iop->symbol);
-			while ((iop = ioctl_next_match(iop)))
-				tprintf(" or %s", iop->symbol);
-		} else
+			JSON_META_BEGIN;
+				tprints(iop->symbol);
+				while ((iop = ioctl_next_match(iop)))
+					tprintf(" or %s", iop->symbol);
+			JSON_META_END;
+		} else {
 			tprintf("%#lx", tcp->u_arg[1]);
-		ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
+		}
+		JSON_META( ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]); );
+
+		json_event(3, EVENT_ARG | SEPARATOR, EVENT_ARG | SEPARATOR, EVENT_ARG);
 	}
 	else {
-		int ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
-		if (!ret)
+		int ret;
+		JSON_META( ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]); );
+		json_event(1, BEF_SEPRATOR | EVENT_ARG);
+
+		if (!ret) {
+			JSON_BEGIN_META_MODE;
 			tprintf(", %#lx", tcp->u_arg[2]);
+			json_event(1, BEF_SEPRATOR | EVENT_ARG);
+		}
 		else
 			return ret - 1;
 	}
-- 
1.9.1





More information about the Strace-devel mailing list