Browse Source

tests: add xlat verbosity support to printxval

* tests/Makefile.am (libtests_a_SOURCES): Remove printxval.c, add
printxval-Xabbrev.c, printxval-Xraw.c, and printxval-Xverbose.c.
(EXTRA_DIST): Add printxval.c.
* tests/printxval.c [!XLAT_RAW] (lookup_xlat): New function.
(printxval): Wrap in XLAT_NAME.
(sprintxlat, sprintxval): New functions.
* tests/printxval-Xabbrev.c: New file.
* tests/printxval-Xraw.c: Likewise.
* tests/printxval-Xverbose.c: Likewise.
* tests/tests.h (printxval): Remove declaration.
(printxval_abbrev, printxval_raw, printxval_verbose, sprintxlat_abbrev,
sprintxlat_raw, sprintxlat_verbose, sprintxval_abbrev, sprintxval_raw,
sprintxval_verbose): New declarations.
(printxval, sprintxlat, sprintxval): New macros, defined based on values
of XLAT_RAW amd XLAT_VERBOSE macros.
* tests/ioprio.c: Simplify the printing code since printxval now has
xlat verbosity support.
Eugene Syromyatnikov 3 months ago
parent
commit
1ddbd47d72
7 changed files with 121 additions and 18 deletions
  1. 4
    1
      tests/Makefile.am
  2. 4
    9
      tests/ioprio.c
  3. 2
    0
      tests/printxval-Xabbrev.c
  4. 3
    0
      tests/printxval-Xraw.c
  5. 3
    0
      tests/printxval-Xverbose.c
  6. 70
    7
      tests/printxval.c
  7. 35
    1
      tests/tests.h

+ 4
- 1
tests/Makefile.am View File

@@ -46,7 +46,9 @@ libtests_a_SOURCES = \
46 46
 	print_quoted_string.c \
47 47
 	print_time.c \
48 48
 	printflags.c \
49
-	printxval.c \
49
+	printxval-Xabbrev.c \
50
+	printxval-Xraw.c \
51
+	printxval-Xverbose.c \
50 52
 	signal2name.c \
51 53
 	skip_unavailable.c \
52 54
 	sprintrc.c \
@@ -418,6 +420,7 @@ EXTRA_DIST = \
418 420
 	pipe.expected \
419 421
 	print_user_desc.c \
420 422
 	printsignal.c \
423
+	printxval.c \
421 424
 	process_vm_readv_writev.c \
422 425
 	pure_executables.list \
423 426
 	qual_fault-exit_group.expected \

+ 4
- 9
tests/ioprio.c View File

@@ -58,17 +58,12 @@ main(void)
58 58
 	errstr = sprintrc(rc);
59 59
 # if XLAT_RAW
60 60
 	printf("ioprio_get(0x1, 0) = %s\n", errstr);
61
-# elif XLAT_VERBOSE
62
-	printf("ioprio_get(0x1 /* IOPRIO_WHO_PROCESS */, 0) = %s", errstr);
63
-	if (rc >= 0) {
64
-		printf(" (IOPRIO_PRIO_VALUE(%u /* ", (unsigned int) rc >> 13);
65
-		printxval(ioprio_class, (unsigned int) rc >> 13,
66
-			  "IOPRIO_CLASS_???");
67
-		printf(" */, %u))", (unsigned int) rc & 0x1fff);
68
-	}
69
-	puts("");
70 61
 # else /* XLAT_ABBREV */
62
+#  if XLAT_VERBOSE
63
+	printf("ioprio_get(0x1 /* IOPRIO_WHO_PROCESS */, 0) = %s", errstr);
64
+#  else
71 65
 	printf("ioprio_get(IOPRIO_WHO_PROCESS, 0) = %s", errstr);
66
+#  endif
72 67
 	if (rc >= 0) {
73 68
 		printf(" (IOPRIO_PRIO_VALUE(");
74 69
 		printxval(ioprio_class, (unsigned int) rc >> 13,

+ 2
- 0
tests/printxval-Xabbrev.c View File

@@ -0,0 +1,2 @@
1
+#define XLAT_NAME(s_) s_##_abbrev
2
+#include "printxval.c"

+ 3
- 0
tests/printxval-Xraw.c View File

@@ -0,0 +1,3 @@
1
+#define XLAT_RAW 1
2
+#define XLAT_NAME(s_) s_##_raw
3
+#include "printxval.c"

+ 3
- 0
tests/printxval-Xverbose.c View File

@@ -0,0 +1,3 @@
1
+#define XLAT_VERBOSE 1
2
+#define XLAT_NAME(s_) s_##_verbose
3
+#include "printxval.c"

+ 70
- 7
tests/printxval.c View File

@@ -13,9 +13,9 @@
13 13
 #include "xlat.h"
14 14
 #include <stdio.h>
15 15
 
16
-int
17
-printxval(const struct xlat *xlat, unsigned long long val,
18
-	  const char *const dflt)
16
+#if !XLAT_RAW
17
+static const char *
18
+lookup_xlat(const struct xlat *xlat, unsigned long long val)
19 19
 {
20 20
 	const struct xlat_data *xd = xlat->data;
21 21
 
@@ -24,13 +24,76 @@ printxval(const struct xlat *xlat, unsigned long long val,
24 24
 			continue;
25 25
 
26 26
 		if (xd->val == val) {
27
-			fputs(xd->str, stdout);
28
-			return 1;
27
+			return xd->str;
29 28
 		}
30 29
 	}
31 30
 
31
+	return NULL;
32
+}
33
+#endif
34
+
35
+int
36
+XLAT_NAME(printxval)(const struct xlat *xlat, unsigned long long val,
37
+		     const char *const dflt)
38
+{
39
+#if XLAT_RAW
40
+	printf("%#llx", val);
41
+
42
+	return 1;
43
+#else
44
+	const char *str = lookup_xlat(xlat, val);
45
+
46
+# if XLAT_VERBOSE
32 47
 	printf("%#llx", val);
48
+	if (str || dflt)
49
+		printf(" /* %s */", str ?: dflt);
50
+# else
51
+	if (str) {
52
+		fputs(str, stdout);
53
+	} else {
54
+		printf("%#llx", val);
55
+		if (dflt)
56
+			printf(" /* %s */", dflt);
57
+	}
58
+# endif /* XLAT_VERBOSE */
59
+
60
+	return !!str;
61
+#endif /* XLAT_RAW */
62
+}
63
+
64
+const char *
65
+XLAT_NAME(sprintxlat)(const char *str, unsigned long long val,
66
+		      const char *const dflt)
67
+{
68
+	static char buf[256];
69
+
70
+#if XLAT_RAW
71
+	snprintf(buf, sizeof(buf), "%#llx", val);
72
+#elif XLAT_VERBOSE
73
+	if (str || dflt)
74
+		snprintf(buf, sizeof(buf), "%#llx /* %s */", val, str ?: dflt);
75
+	else
76
+		snprintf(buf, sizeof(buf), "%#llx", val);
77
+#else
78
+	if (str)
79
+		return str;
80
+
33 81
 	if (dflt)
34
-		printf(" /* %s */", dflt);
35
-	return 0;
82
+		snprintf(buf, sizeof(buf), "%#llx /* %s */", val, dflt);
83
+	else
84
+		snprintf(buf, sizeof(buf), "%#llx", val);
85
+#endif
86
+
87
+	return buf;
88
+}
89
+
90
+const char *
91
+XLAT_NAME(sprintxval)(const struct xlat *xlat, unsigned long long val,
92
+		      const char *const dflt)
93
+{
94
+#if XLAT_RAW
95
+	return sprintxlat(NULL, val, dflt);
96
+#else
97
+	return sprintxlat(lookup_xlat(xlat, val), val, dflt);
98
+#endif
36 99
 }

+ 35
- 1
tests/tests.h View File

@@ -223,7 +223,41 @@ struct xlat;
223 223
 int printflags(const struct xlat *, const unsigned long long, const char *);
224 224
 
225 225
 /* Print constant in symbolic form according to xlat table. */
226
-int printxval(const struct xlat *, const unsigned long long, const char *);
226
+int printxval_abbrev(const struct xlat *, const unsigned long long,
227
+		     const char *);
228
+int printxval_raw(const struct xlat *, const unsigned long long, const char *);
229
+int printxval_verbose(const struct xlat *, const unsigned long long,
230
+		      const char *);
231
+
232
+/* Print constant in symbolic form according to xlat table. */
233
+const char *sprintxlat_abbrev(const char *, const unsigned long long,
234
+			   const char *);
235
+const char *sprintxlat_raw(const char *, const unsigned long long,
236
+			   const char *);
237
+const char *sprintxlat_verbose(const char *, const unsigned long long,
238
+			       const char *);
239
+
240
+/* Print constant in symbolic form according to xlat table. */
241
+const char *sprintxval_abbrev(const struct xlat *, const unsigned long long,
242
+			      const char *);
243
+const char *sprintxval_raw(const struct xlat *, const unsigned long long,
244
+			   const char *);
245
+const char *sprintxval_verbose(const struct xlat *, const unsigned long long,
246
+			       const char *);
247
+
248
+# if XLAT_RAW
249
+#  define printxval  printxval_raw
250
+#  define sprintxlat sprintxlat_raw
251
+#  define sprintxval sprintxval_raw
252
+# elif XLAT_VERBOSE
253
+#  define printxval  printxval_verbose
254
+#  define sprintxlat sprintxlat_verbose
255
+#  define sprintxval sprintxval_verbose
256
+# else
257
+#  define printxval  printxval_abbrev
258
+#  define sprintxlat sprintxlat_abbrev
259
+#  define sprintxval sprintxval_abbrev
260
+# endif
227 261
 
228 262
 /* Invoke a socket syscall, either directly or via __NR_socketcall. */
229 263
 int socketcall(const int nr, const int call,

Loading…
Cancel
Save