From cdf0885fe033ce345aae23ed90a3cac0b19d3039 Mon Sep 17 00:00:00 2001 From: Eugene Syromyatnikov Date: Sat, 29 Oct 2016 05:26:50 +0300 Subject: [PATCH] util: add printing helper for sorted xlat arrays * util.c (printxval_searchn): New function. * defs.h (printxval_searchn): New prototype. (printxval_search): New helper macro useful in conjunction with static xlat arrays. --- defs.h | 4 ++++ util.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/defs.h b/defs.h index b2907403..52a9e4d7 100644 --- a/defs.h +++ b/defs.h @@ -545,6 +545,10 @@ extern int printllval(struct tcb *, const char *, int) extern void printaddr_ull(unsigned long long); extern void printxvals(const uint64_t, const char *, const struct xlat *, ...) ATTRIBUTE_SENTINEL; +extern void printxval_searchn(const struct xlat *xlat, size_t xlat_size, + uint64_t val, const char *dflt); +#define printxval_search(xlat__, val__, dflt__) \ + printxval_searchn(xlat__, ARRAY_SIZE(xlat__), val__, dflt__) extern long long getarg_ll(struct tcb *tcp, int argn); extern unsigned long long getarg_ull(struct tcb *tcp, int argn); extern int printargs(struct tcb *); diff --git a/util.c b/util.c index 69da3ee3..05c0e15c 100644 --- a/util.c +++ b/util.c @@ -228,6 +228,24 @@ printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...) va_end(args); } +/* + * Print entry in sorted struct xlat table, if it is there. + */ +void +printxval_searchn(const struct xlat *xlat, size_t xlat_size, uint64_t val, + const char *dflt) +{ + const char *s = xlat_search(xlat, xlat_size, val); + + if (s) { + tprints(s); + } else { + tprintf("%#" PRIx64, val); + if (dflt) + tprintf(" /* %s */", dflt); + } +} + /* * Fetch 64bit argument at position arg_no and * return the index of the next argument. -- 2.40.0