From: Eugene Syromyatnikov Date: Fri, 19 Jul 2019 16:14:20 +0000 (+0200) Subject: tests: add xlat verbosity support to printxval X-Git-Tag: v5.3~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1ddbd47d72e591c5ea83e36cf044af1f6e5b6df1;p=strace 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. --- diff --git a/tests/Makefile.am b/tests/Makefile.am index e5addee2..444e82e3 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 \ diff --git a/tests/ioprio.c b/tests/ioprio.c index 2b9541e1..5e1e1194 100644 --- a/tests/ioprio.c +++ b/tests/ioprio.c @@ -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 index 00000000..902a386e --- /dev/null +++ b/tests/printxval-Xabbrev.c @@ -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 index 00000000..eb92a713 --- /dev/null +++ b/tests/printxval-Xraw.c @@ -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 index 00000000..f97529ed --- /dev/null +++ b/tests/printxval-Xverbose.c @@ -0,0 +1,3 @@ +#define XLAT_VERBOSE 1 +#define XLAT_NAME(s_) s_##_verbose +#include "printxval.c" diff --git a/tests/printxval.c b/tests/printxval.c index 90d848c3..a89d99d8 100644 --- a/tests/printxval.c +++ b/tests/printxval.c @@ -13,9 +13,9 @@ #include "xlat.h" #include -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 } diff --git a/tests/tests.h b/tests/tests.h index 53d7b72f..d47e5bd3 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -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,