xlat.c: add sprintxval for printing xval to string

* defs.h (sprintxval): New declaration.
(sprintxval): New function.
This commit is contained in:
Eugene Syromiatnikov 2017-08-05 16:19:15 +02:00 committed by Dmitry V. Levin
parent d9175ac5ce
commit a907f4a126
2 changed files with 16 additions and 0 deletions

2
defs.h
View File

@ -546,6 +546,8 @@ extern int printxval_searchn(const struct xlat *xlat, size_t xlat_size,
uint64_t val, const char *dflt); uint64_t val, const char *dflt);
#define printxval_search(xlat__, val__, dflt__) \ #define printxval_search(xlat__, val__, dflt__) \
printxval_searchn(xlat__, ARRAY_SIZE(xlat__), val__, dflt__) printxval_searchn(xlat__, ARRAY_SIZE(xlat__), val__, dflt__)
extern int sprintxval(char *buf, size_t size, const struct xlat *,
unsigned int val, const char *dflt);
extern int printargs(struct tcb *); extern int printargs(struct tcb *);
extern int printargs_u(struct tcb *); extern int printargs_u(struct tcb *);
extern int printargs_d(struct tcb *); extern int printargs_d(struct tcb *);

14
xlat.c
View File

@ -94,6 +94,20 @@ printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...)
return 0; return 0;
} }
int
sprintxval(char *const buf, const size_t size, const struct xlat *const x,
const unsigned int val, const char *const dflt)
{
const char *const str = xlookup(x, val);
if (str)
return xsnprintf(buf, size, "%s", str);
if (dflt)
return xsnprintf(buf, size, "%#x /* %s */", val, dflt);
return xsnprintf(buf, size, "%#x", val);
}
/** /**
* Print entry in sorted struct xlat table, if it is there. * Print entry in sorted struct xlat table, if it is there.
* *