]> granicus.if.org Git - strace/commitdiff
tests: add xlat verbosity support to printxval
authorEugene Syromyatnikov <evgsyr@gmail.com>
Fri, 19 Jul 2019 16:14:20 +0000 (18:14 +0200)
committerDmitry V. Levin <ldv@altlinux.org>
Tue, 24 Sep 2019 15:31:28 +0000 (15:31 +0000)
* 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.

tests/Makefile.am
tests/ioprio.c
tests/printxval-Xabbrev.c [new file with mode: 0644]
tests/printxval-Xraw.c [new file with mode: 0644]
tests/printxval-Xverbose.c [new file with mode: 0644]
tests/printxval.c
tests/tests.h

index e5addee2e851a4944fe738121b137932f353db7e..444e82e3bd2df67bdbc57ccb43694d09d5e083bf 100644 (file)
@@ -46,7 +46,9 @@ libtests_a_SOURCES = \
        print_quoted_string.c \
        print_time.c \
        printflags.c \
-       printxval.c \
+       printxval-Xabbrev.c \
+       printxval-Xraw.c \
+       printxval-Xverbose.c \
        signal2name.c \
        skip_unavailable.c \
        sprintrc.c \
@@ -418,6 +420,7 @@ EXTRA_DIST = \
        pipe.expected \
        print_user_desc.c \
        printsignal.c \
+       printxval.c \
        process_vm_readv_writev.c \
        pure_executables.list \
        qual_fault-exit_group.expected \
index 2b9541e19de226952110111e04a593cb57217106..5e1e11947238b7c622931f8fd0b71f8d02659fc6 100644 (file)
@@ -58,17 +58,12 @@ main(void)
        errstr = sprintrc(rc);
 # if XLAT_RAW
        printf("ioprio_get(0x1, 0) = %s\n", errstr);
-# elif XLAT_VERBOSE
-       printf("ioprio_get(0x1 /* IOPRIO_WHO_PROCESS */, 0) = %s", errstr);
-       if (rc >= 0) {
-               printf(" (IOPRIO_PRIO_VALUE(%u /* ", (unsigned int) rc >> 13);
-               printxval(ioprio_class, (unsigned int) rc >> 13,
-                         "IOPRIO_CLASS_???");
-               printf(" */, %u))", (unsigned int) rc & 0x1fff);
-       }
-       puts("");
 # else /* XLAT_ABBREV */
+#  if XLAT_VERBOSE
+       printf("ioprio_get(0x1 /* IOPRIO_WHO_PROCESS */, 0) = %s", errstr);
+#  else
        printf("ioprio_get(IOPRIO_WHO_PROCESS, 0) = %s", errstr);
+#  endif
        if (rc >= 0) {
                printf(" (IOPRIO_PRIO_VALUE(");
                printxval(ioprio_class, (unsigned int) rc >> 13,
diff --git a/tests/printxval-Xabbrev.c b/tests/printxval-Xabbrev.c
new file mode 100644 (file)
index 0000000..902a386
--- /dev/null
@@ -0,0 +1,2 @@
+#define XLAT_NAME(s_) s_##_abbrev
+#include "printxval.c"
diff --git a/tests/printxval-Xraw.c b/tests/printxval-Xraw.c
new file mode 100644 (file)
index 0000000..eb92a71
--- /dev/null
@@ -0,0 +1,3 @@
+#define XLAT_RAW 1
+#define XLAT_NAME(s_) s_##_raw
+#include "printxval.c"
diff --git a/tests/printxval-Xverbose.c b/tests/printxval-Xverbose.c
new file mode 100644 (file)
index 0000000..f97529e
--- /dev/null
@@ -0,0 +1,3 @@
+#define XLAT_VERBOSE 1
+#define XLAT_NAME(s_) s_##_verbose
+#include "printxval.c"
index 90d848c31949bd964f3495132adb2d2d15a55f42..a89d99d8241fdcb66c09a9a5ec406785d347f302 100644 (file)
@@ -13,9 +13,9 @@
 #include "xlat.h"
 #include <stdio.h>
 
-int
-printxval(const struct xlat *xlat, unsigned long long val,
-         const char *const dflt)
+#if !XLAT_RAW
+static const char *
+lookup_xlat(const struct xlat *xlat, unsigned long long val)
 {
        const struct xlat_data *xd = xlat->data;
 
@@ -24,13 +24,76 @@ printxval(const struct xlat *xlat, unsigned long long val,
                        continue;
 
                if (xd->val == val) {
-                       fputs(xd->str, stdout);
-                       return 1;
+                       return xd->str;
                }
        }
 
+       return NULL;
+}
+#endif
+
+int
+XLAT_NAME(printxval)(const struct xlat *xlat, unsigned long long val,
+                    const char *const dflt)
+{
+#if XLAT_RAW
+       printf("%#llx", val);
+
+       return 1;
+#else
+       const char *str = lookup_xlat(xlat, val);
+
+# if XLAT_VERBOSE
        printf("%#llx", val);
+       if (str || dflt)
+               printf(" /* %s */", str ?: dflt);
+# else
+       if (str) {
+               fputs(str, stdout);
+       } else {
+               printf("%#llx", val);
+               if (dflt)
+                       printf(" /* %s */", dflt);
+       }
+# endif /* XLAT_VERBOSE */
+
+       return !!str;
+#endif /* XLAT_RAW */
+}
+
+const char *
+XLAT_NAME(sprintxlat)(const char *str, unsigned long long val,
+                     const char *const dflt)
+{
+       static char buf[256];
+
+#if XLAT_RAW
+       snprintf(buf, sizeof(buf), "%#llx", val);
+#elif XLAT_VERBOSE
+       if (str || dflt)
+               snprintf(buf, sizeof(buf), "%#llx /* %s */", val, str ?: dflt);
+       else
+               snprintf(buf, sizeof(buf), "%#llx", val);
+#else
+       if (str)
+               return str;
+
        if (dflt)
-               printf(" /* %s */", dflt);
-       return 0;
+               snprintf(buf, sizeof(buf), "%#llx /* %s */", val, dflt);
+       else
+               snprintf(buf, sizeof(buf), "%#llx", val);
+#endif
+
+       return buf;
+}
+
+const char *
+XLAT_NAME(sprintxval)(const struct xlat *xlat, unsigned long long val,
+                     const char *const dflt)
+{
+#if XLAT_RAW
+       return sprintxlat(NULL, val, dflt);
+#else
+       return sprintxlat(lookup_xlat(xlat, val), val, dflt);
+#endif
 }
index 53d7b72fd1b28cf827996f358ca170dd9bd9c09a..d47e5bd345ba345627cb1c81552b9d0139fb3cce 100644 (file)
@@ -223,7 +223,41 @@ struct xlat;
 int printflags(const struct xlat *, const unsigned long long, const char *);
 
 /* Print constant in symbolic form according to xlat table. */
-int printxval(const struct xlat *, const unsigned long long, const char *);
+int printxval_abbrev(const struct xlat *, const unsigned long long,
+                    const char *);
+int printxval_raw(const struct xlat *, const unsigned long long, const char *);
+int printxval_verbose(const struct xlat *, const unsigned long long,
+                     const char *);
+
+/* Print constant in symbolic form according to xlat table. */
+const char *sprintxlat_abbrev(const char *, const unsigned long long,
+                          const char *);
+const char *sprintxlat_raw(const char *, const unsigned long long,
+                          const char *);
+const char *sprintxlat_verbose(const char *, const unsigned long long,
+                              const char *);
+
+/* Print constant in symbolic form according to xlat table. */
+const char *sprintxval_abbrev(const struct xlat *, const unsigned long long,
+                             const char *);
+const char *sprintxval_raw(const struct xlat *, const unsigned long long,
+                          const char *);
+const char *sprintxval_verbose(const struct xlat *, const unsigned long long,
+                              const char *);
+
+# if XLAT_RAW
+#  define printxval  printxval_raw
+#  define sprintxlat sprintxlat_raw
+#  define sprintxval sprintxval_raw
+# elif XLAT_VERBOSE
+#  define printxval  printxval_verbose
+#  define sprintxlat sprintxlat_verbose
+#  define sprintxval sprintxval_verbose
+# else
+#  define printxval  printxval_abbrev
+#  define sprintxlat sprintxlat_abbrev
+#  define sprintxval sprintxval_abbrev
+# endif
 
 /* Invoke a socket syscall, either directly or via __NR_socketcall. */
 int socketcall(const int nr, const int call,