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

Zhu YangMin zym0017d at gmail.com
Mon Jul 21 14:35:25 UTC 2014


Modify all the syscalls in io.c to support JSON output.
There are mainly two kind modification to strace:
1) simply modify sys_* functions to use the JSON framework;
2) modify those format/detail funcitons(such as printfd, printlval etc..)
to use the JSON framework;

* io.c (sys_read/write, sys_readv/writev, tprint_iov_upto,
sys_pread/pwrite, sys_preadv/pwritev, print_off_t, sys_ioctl
sys_sendfile/sendfile64, sys_tee, sys_splice, sys_vmsplice):
Modify to support JSON. The support for ioctl() is very limited
and need to be improved.

* util.c (printllval, printflags, printfd, printstr):
Modify all the io-related functions in util.c to support JSON.

* ioctl.c (ioctl_decode): very limited support for ioctl(), need to
be improved later.
---
 io.c    | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------
 ioctl.c |  35 +++++++++++++------
 util.c  |  33 +++++++++++++-----
 3 files changed, 149 insertions(+), 41 deletions(-)

diff --git a/io.c b/io.c
index fea1218..1a14749 100644
--- a/io.c
+++ b/io.c
@@ -40,12 +40,18 @@ sys_read(struct tcb *tcp)
 	if (entering(tcp)) {
 		printfd(tcp, tcp->u_arg[0]);
 		tprints(", ");
+		json_event(JSON_DIRECT, JSON_SEPA);
 	} else {
-		if (syserror(tcp))
+		if (syserror(tcp)) {
 			tprintf("%#lx", tcp->u_arg[1]);
-		else
+			json_event(JSON_ARG);
+		}
+		else {
 			printstr(tcp, tcp->u_arg[1], tcp->u_rval);
+			json_event(JSON_DIRECT);
+		}
 		tprintf(", %lu", tcp->u_arg[2]);
+		json_event(JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -58,6 +64,7 @@ sys_write(struct tcb *tcp)
 		tprints(", ");
 		printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
 		tprintf(", %lu", tcp->u_arg[2]);
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -89,15 +96,18 @@ tprint_iov_upto(struct tcb *tcp, unsigned long len, unsigned long addr, int deco
 #endif
 	unsigned long size, cur, end, abbrev_end;
 	int failed = 0;
+	int json_from = json_merge_from();
 
 	if (!len) {
 		tprints("[]");
+		json_merge(JSON_NULL);
 		return;
 	}
 	size = len * sizeof_iov;
 	end = addr + size;
 	if (!verbose(tcp) || size / sizeof_iov != len || end < addr) {
 		tprintf("%#lx", addr);
+		json_merge(JSON_ARG);
 		return;
 	}
 	if (abbrev(tcp)) {
@@ -108,32 +118,47 @@ tprint_iov_upto(struct tcb *tcp, unsigned long len, unsigned long addr, int deco
 		abbrev_end = end;
 	}
 	tprints("[");
+	json_merge(JSON_ARRAY_BEGIN);
 	for (cur = addr; cur < end; cur += sizeof_iov) {
-		if (cur > addr)
+		if (cur > addr) {
 			tprints(", ");
+			json_merge(JSON_SEPA);
+		}
 		if (cur >= abbrev_end) {
-			tprints("...");
+			tprintf("%s", "...");
+			json_merge(JSON_STRING);
 			break;
 		}
 		if (umoven(tcp, cur, sizeof_iov, (char *) &iov) < 0) {
-			tprints("?");
+			tprintf("%s", "?");
+			json_merge(JSON_STRING);
 			failed = 1;
 			break;
 		}
 		tprints("{");
+		json_merge(JSON_ARRAY_BEGIN);
 		if (decode_iov) {
 			unsigned long len = iov_iov_len;
 			if (len > data_size)
 				len = data_size;
 			data_size -= len;
 			printstr(tcp, (long) iov_iov_base, len);
-		} else
+			json_merge(JSON_DIRECT);
+		} else {
 			tprintf("%#lx", (long) iov_iov_base);
+			json_merge(JSON_ARG);
+		}
+
 		tprintf(", %lu}", (unsigned long)iov_iov_len);
+		json_merge(JSON_SEPA, JSON_ARG, JSON_ARRAY_END);
 	}
 	tprints("]");
-	if (failed)
+	json_merge(JSON_ARRAY_END);
+	if (failed) {
 		tprintf(" %#lx", addr);
+		json_merge(JSON_STRING);
+	}
+	json_merge_all(json_from);
 #undef sizeof_iov
 #undef iov_iov_base
 #undef iov_iov_len
@@ -151,14 +176,16 @@ sys_readv(struct tcb *tcp)
 	if (entering(tcp)) {
 		printfd(tcp, tcp->u_arg[0]);
 		tprints(", ");
+		json_event(JSON_DIRECT, JSON_SEPA);
 	} 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(JSON_ARG, JSON_SEPA, JSON_ARG);
 			return 0;
 		}
 		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
 		tprintf(", %lu", tcp->u_arg[2]);
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -171,6 +198,7 @@ sys_writev(struct tcb *tcp)
 		tprints(", ");
 		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
 		tprintf(", %lu", tcp->u_arg[2]);
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -193,13 +221,19 @@ sys_pread(struct tcb *tcp)
 	if (entering(tcp)) {
 		printfd(tcp, tcp->u_arg[0]);
 		tprints(", ");
+		json_event(JSON_DIRECT, JSON_SEPA);
 	} else {
-		if (syserror(tcp))
+		if (syserror(tcp)) {
 			tprintf("%#lx", tcp->u_arg[1]);
-		else
+			json_event(JSON_ARG);
+		}
+		else {
 			printstr(tcp, tcp->u_arg[1], tcp->u_rval);
+			json_event(JSON_DIRECT);
+		}
 		tprintf(", %lu, ", tcp->u_arg[2]);
 		printllval_aligned(tcp, "%llu", PREAD_OFFSET_ARG);
+		json_event(JSON_SEPA, JSON_ARG, JSON_SEPA, JSON_DIRECT);
 	}
 	return 0;
 }
@@ -213,6 +247,7 @@ sys_pwrite(struct tcb *tcp)
 		printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
 		tprintf(", %lu, ", tcp->u_arg[2]);
 		printllval_aligned(tcp, "%llu", PREAD_OFFSET_ARG);
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_ARG, JSON_SEPA, JSON_DIRECT);
 	}
 	return 0;
 }
@@ -224,14 +259,17 @@ sys_preadv(struct tcb *tcp)
 	if (entering(tcp)) {
 		printfd(tcp, tcp->u_arg[0]);
 		tprints(", ");
+		json_event(JSON_DIRECT, JSON_SEPA);
 	} else {
 		if (syserror(tcp)) {
 			tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
+			json_event(JSON_ARG, JSON_SEPA, JSON_ARG);
 			return 0;
 		}
 		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
 		tprintf(", %lu, ", tcp->u_arg[2]);
 		printllval_unaligned(tcp, "%llu", 3);
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_ARG, JSON_SEPA, JSON_DIRECT);
 	}
 	return 0;
 }
@@ -245,6 +283,7 @@ sys_pwritev(struct tcb *tcp)
 		tprint_iov(tcp, tcp->u_arg[2], tcp->u_arg[1], 1);
 		tprintf(", %lu, ", tcp->u_arg[2]);
 		printllval_unaligned(tcp, "%llu", 3);
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_ARG, JSON_SEPA, JSON_DIRECT);
 	}
 	return 0;
 }
@@ -257,6 +296,7 @@ print_off_t(struct tcb *tcp, long addr)
 
 	if (!addr) {
 		tprints("NULL");
+		json_merge(JSON_NULL);
 		return;
 	}
 
@@ -274,6 +314,7 @@ print_off_t(struct tcb *tcp, long addr)
 		tprintf("%#lx", addr);
 	else
 		tprintf("[%lu]", offset);
+	json_merge(JSON_ARG);
 }
 
 int
@@ -286,6 +327,7 @@ sys_sendfile(struct tcb *tcp)
 		tprints(", ");
 		print_off_t(tcp, tcp->u_arg[2]);
 		tprintf(", %lu", tcp->u_arg[3]);
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -295,12 +337,18 @@ print_loff_t(struct tcb *tcp, long addr)
 {
 	loff_t offset;
 
-	if (!addr)
+	if (!addr) {
 		tprints("NULL");
-	else if (umove(tcp, addr, &offset) < 0)
+		json_merge(JSON_NULL);
+	}
+	else if (umove(tcp, addr, &offset) < 0) {
 		tprintf("%#lx", addr);
-	else
+		json_merge(JSON_STRING);
+	}
+	else {
 		tprintf("[%llu]", (unsigned long long int) offset);
+		json_merge(JSON_STRING);
+	}
 }
 
 int
@@ -313,6 +361,7 @@ sys_sendfile64(struct tcb *tcp)
 		tprints(", ");
 		print_loff_t(tcp, tcp->u_arg[2]);
 		tprintf(", %lu", tcp->u_arg[3]);
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -326,13 +375,16 @@ sys_tee(struct tcb *tcp)
 		/* int fd_in */
 		printfd(tcp, tcp->u_arg[0]);
 		tprints(", ");
+
 		/* int fd_out */
 		printfd(tcp, tcp->u_arg[1]);
-		tprints(", ");
+
 		/* 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_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_ARG, JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -344,19 +396,25 @@ sys_splice(struct tcb *tcp)
 		/* int fd_in */
 		printfd(tcp, tcp->u_arg[0]);
 		tprints(", ");
+
 		/* loff_t *off_in */
 		print_loff_t(tcp, tcp->u_arg[1]);
 		tprints(", ");
+
 		/* int fd_out */
 		printfd(tcp, tcp->u_arg[2]);
 		tprints(", ");
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA);
+
 		/* loff_t *off_out */
 		print_loff_t(tcp, tcp->u_arg[3]);
-		tprints(", ");
+
 		/* 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_event(JSON_DIRECT, JSON_SEPA, JSON_ARG, JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -368,11 +426,14 @@ sys_vmsplice(struct tcb *tcp)
 		/* int fd */
 		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);
 		tprintf(", %lu, ", tcp->u_arg[2]);
+
 		/* unsigned int flags */
 		printflags(splice_flags, tcp->u_arg[3], "SPLICE_F_???");
+		json_event(JSON_DIRECT, JSON_SEPA, JSON_DIRECT, JSON_SEPA, JSON_ARG, JSON_SEPA, JSON_ARG);
 	}
 	return 0;
 }
@@ -381,23 +442,38 @@ int
 sys_ioctl(struct tcb *tcp)
 {
 	const struct_ioctlent *iop;
-
 	if (entering(tcp)) {
 		printfd(tcp, tcp->u_arg[0]);
 		tprints(", ");
+		json_event(JSON_DIRECT, JSON_SEPA);
+
 		iop = ioctl_lookup(tcp->u_arg[1]);
 		if (iop) {
-			tprints(iop->symbol);
-			while ((iop = ioctl_next_match(iop)))
+			json_event(JSON_ARRAY_BEGIN);
+			tprintf("%s", iop->symbol);
+			json_event(JSON_ARG);
+			while ((iop = ioctl_next_match(iop))) {
 				tprintf(" or %s", iop->symbol);
-		} else
+				json_event(JSON_SEPA, JSON_ARG);
+			}
+			json_event(JSON_ARRAY_END);
+		} else {
 			tprintf("%#lx", tcp->u_arg[1]);
+			json_event(JSON_ARG);
+		}
+
 		ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
+		// ioctl_decode will output a ', '
+		json_event(JSON_SEPA, JSON_STRING_QUOTE, JSON_DIRECT_ESCAPE);
 	}
 	else {
 		int ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
-		if (!ret)
+		json_event(JSON_DIRECT_ESCAPE, JSON_STRING_QUOTE);
+
+		if (!ret) {
 			tprintf(", %#lx", tcp->u_arg[2]);
+			json_event(JSON_SEPA, JSON_ARG);
+		}
 		else
 			return ret - 1;
 	}
diff --git a/ioctl.c b/ioctl.c
index 3f6c410..ac5d0e7 100644
--- a/ioctl.c
+++ b/ioctl.c
@@ -72,37 +72,52 @@ ioctl_next_match(const struct_ioctlent *iop)
 int
 ioctl_decode(struct tcb *tcp, long code, long arg)
 {
+	int json_from = json_merge_from();
+	json_hook_push(JSON_HOOK_ALL);
+	int ret = 0;
 	switch ((code >> 8) & 0xff) {
 #if defined(ALPHA) || defined(POWERPC)
 	case 'f': case 't': case 'T':
 #else /* !ALPHA */
 	case 0x54:
 #endif /* !ALPHA */
-		return term_ioctl(tcp, code, arg);
+		ret = term_ioctl(tcp, code, arg);
+		break;
 	case 0x89:
-		return sock_ioctl(tcp, code, arg);
+		ret = sock_ioctl(tcp, code, arg);
+		break;
 	case 'p':
-		return rtc_ioctl(tcp, code, arg);
+		ret = rtc_ioctl(tcp, code, arg);
+		break;
 	case 0x03:
 	case 0x12:
-		return block_ioctl(tcp, code, arg);
+		ret = block_ioctl(tcp, code, arg);
+		break;
 #ifdef HAVE_SCSI_SG_H
 	case 0x22:
-		return scsi_ioctl(tcp, code, arg);
+		ret = scsi_ioctl(tcp, code, arg);
+		break;
 #endif
 	case 'L':
-		return loop_ioctl(tcp, code, arg);
+		ret = loop_ioctl(tcp, code, arg);
+		break;
 	case 'M':
-		return mtd_ioctl(tcp, code, arg);
+		ret = mtd_ioctl(tcp, code, arg);
+		break;
 	case 'o':
 	case 'O':
-		return ubi_ioctl(tcp, code, arg);
+		ret = ubi_ioctl(tcp, code, arg);
+		break;
 	case '=':
-		return ptp_ioctl(tcp, code, arg);
+		ret = ptp_ioctl(tcp, code, arg);
+		break;
 	default:
 		break;
 	}
-	return 0;
+
+	json_hook_pop();
+	json_merge_all(json_from);
+	return ret;
 }
 
 /*
diff --git a/util.c b/util.c
index 33482d5..6bebb03 100644
--- a/util.c
+++ b/util.c
@@ -227,6 +227,7 @@ printxval(const struct xlat *xlat, int val, const char *dflt)
 int
 printllval(struct tcb *tcp, const char *format, int arg_no, bool align)
 {
+	json_hook_push(JSON_HOOK_ALL);
 #if SIZEOF_LONG > 4 && SIZEOF_LONG == SIZEOF_LONG_LONG
 # if SUPPORTED_PERSONALITIES > 1
 	if (current_wordsize > 4) {
@@ -271,7 +272,8 @@ printllval(struct tcb *tcp, const char *format, int arg_no, bool align)
 	tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
 	arg_no += 2;
 #endif
-
+	json_hook_pop();
+	json_merge(JSON_ARG);
 	return arg_no;
 }
 
@@ -335,10 +337,14 @@ printflags(const struct xlat *xlat, int flags, const char *dflt)
 	const char *sep;
 
 	if (flags == 0 && xlat->val == 0) {
-		tprints(xlat->str);
+		tprintf("%s", xlat->str);
+		json_merge(JSON_STRING);
 		return 1;
 	}
 
+	int json_from = json_merge_from();
+	json_hook_push(JSON_HOOK_ALL);
+
 	sep = "";
 	for (n = 0; xlat->str; xlat++) {
 		if (xlat->val && (flags & xlat->val) == xlat->val) {
@@ -365,6 +371,8 @@ printflags(const struct xlat *xlat, int flags, const char *dflt)
 		}
 	}
 
+	json_hook_pop();
+	json_merge_all(json_from);
 	return n;
 }
 
@@ -408,11 +416,14 @@ 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 && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
 		tprintf("%d<%s>", fd, path);
-	else
+		json_merge(JSON_ARRAY_BEGIN, JSON_ARG, JSON_SEPA, JSON_ARG, JSON_ARRAY_END);
+	}
+	else {
 		tprintf("%d", fd);
+		json_merge(JSON_ARG);
+	}
 }
 
 void
@@ -621,6 +632,7 @@ printstr(struct tcb *tcp, long addr, long len)
 
 	if (!addr) {
 		tprints("NULL");
+		json_merge(JSON_NULL);
 		return;
 	}
 	/* Allocate static buffers if they are not allocated yet. */
@@ -645,6 +657,7 @@ printstr(struct tcb *tcp, long addr, long len)
 		size = max_strlen + 1;
 		if (umovestr(tcp, addr, size, str) < 0) {
 			tprintf("%#lx", addr);
+			json_merge(JSON_ARG);
 			return;
 		}
 	}
@@ -654,6 +667,7 @@ printstr(struct tcb *tcp, long addr, long len)
 			size = (unsigned long)len;
 		if (umoven(tcp, addr, size, str) < 0) {
 			tprintf("%#lx", addr);
+			json_merge(JSON_ARG);
 			return;
 		}
 	}
@@ -664,9 +678,12 @@ printstr(struct tcb *tcp, long addr, long len)
 	ellipsis = (string_quote(str, outstr, len, size) &&
 			(len < 0 || len > max_strlen));
 
-	tprints(outstr);
-	if (ellipsis)
-		tprints("...");
+	tprintf("%s", outstr);
+	if (ellipsis) {
+		tprintf("%s", "...");
+		json_merge(JSON_DIRECT, JSON_DIRECT);
+	}
+	json_merge(JSON_STRING);
 }
 
 #if HAVE_SYS_UIO_H
-- 
1.9.1





More information about the Strace-devel mailing list