[PATCH 3/4] getrandom: print getrandom buffer as hex array

Eugene Syromyatnikov evgsyr at gmail.com
Fri Jan 6 08:53:13 UTC 2017


On Fri, Jan 06, 2017 at 03:16:29PM +0800, JingPiao Chen wrote:
> In general, the random bytes is not number or letter, print
> as hex array is clear, use 0x%02x format print it, easy to
> konw each element is one byte, so 0 will print 0x00.

I think it is better (in terms of performance and readability) to print it as
hex-escaped string, something like this:

--
diff --git i/defs.h w/defs.h
index eeabfcf..1630385 100644
--- i/defs.h
+++ w/defs.h
@@ -505,6 +505,7 @@ extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_b
 #define QUOTE_0_TERMINATED                      0x01
 #define QUOTE_OMIT_LEADING_TRAILING_QUOTES      0x02
 #define QUOTE_OMIT_TRAILING_0                   0x08
+#define QUOTE_FORCE_HEX                         0x10
 
 extern int string_quote(const char *, char *, unsigned long, unsigned int);
 extern int print_quoted_string(const char *, unsigned long, unsigned int);
diff --git i/getrandom.c w/getrandom.c
index 0a353ad..ea9ad57 100644
--- i/getrandom.c
+++ w/getrandom.c
@@ -7,7 +7,8 @@ SYS_FUNC(getrandom)
 		if (syserror(tcp))
 			printaddr(tcp->u_arg[0]);
 		else
-			printstrn(tcp, tcp->u_arg[0], tcp->u_rval);
+			printstr_ex(tcp, tcp->u_arg[0], tcp->u_rval,
+				    QUOTE_FORCE_HEX);
 		tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
 		printflags(getrandom_flags, tcp->u_arg[2], "GRND_???");
 	}
diff --git i/tests/getrandom.test w/tests/getrandom.test
index e06367c..ad34048 100755
--- i/tests/getrandom.test
+++ w/tests/getrandom.test
@@ -3,4 +3,4 @@
 # Check getrandom syscall decoding.
 
 . "${srcdir=.}/init.sh"
-run_strace_match_diff -a32 -xx -s3
+run_strace_match_diff -a32 -s3
diff --git i/util.c w/util.c
index a27c4e1..484f84e 100644
--- i/util.c
+++ w/util.c
@@ -660,9 +660,9 @@ string_quote(const char *instr, char *outstr, const unsigned long size,
 		eol = 0x100; /* this can never match a char */
 
 	usehex = 0;
-	if (xflag > 1)
+	if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
 		usehex = 1;
-	else if (xflag) {
+	} else if (xflag) {
 		/* Check for presence of symbol which require
 		   to hex-quote the whole string. */
 		for (i = 0; i < size; ++i) {





More information about the Strace-devel mailing list