]> granicus.if.org Git - strace/commitdiff
util: add printing helper for sorted xlat arrays
authorEugene Syromyatnikov <evgsyr@gmail.com>
Sat, 29 Oct 2016 02:26:50 +0000 (05:26 +0300)
committerEugene Syromyatnikov <evgsyr@gmail.com>
Sun, 30 Oct 2016 21:49:28 +0000 (00:49 +0300)
* 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
util.c

diff --git a/defs.h b/defs.h
index b2907403caaf13bfce0eb8d0674eb7f4ae584831..52a9e4d7923f5408802709e05b9e8f50a780080b 100644 (file)
--- 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 69da3ee306a4bd24a5882a93d9f8f93bf8dcea4b..05c0e15cb1285bf49b00ac23a6fc8c11c1eb7bdd 100644 (file)
--- 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.