[PATCH v2 2/5] Introduce xlookup_ex, extended xlookup with fallback value

Masatake YAMATO yamato at redhat.com
Sun May 20 20:27:07 UTC 2018


xlookup_ex function is extended version of xlookup. Unlike xlookup,
xlookup_ex returns its third argument as the fallback value when it
cannot resolve its second argument.

* defs.h (xlookup_ex): New function declaration.
(xlookup): Redefined this as a macro using xlookup_ex.
* xlat.c (xlookup_ex): New function.
(sprintxval_ex): Use xlookup_ex instead of xlookup.

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 defs.h |  3 ++-
 xlat.c | 11 ++++-------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/defs.h b/defs.h
index 92f5fa6e..b9f03312 100644
--- a/defs.h
+++ b/defs.h
@@ -534,7 +534,8 @@ extern int getfdpath(struct tcb *, int, char *, unsigned);
 extern unsigned long getfdinode(struct tcb *, int);
 extern enum sock_proto getfdproto(struct tcb *, int);
 
-extern const char *xlookup(const struct xlat *, const uint64_t);
+extern const char *xlookup_ex(const struct xlat *, const uint64_t, const char*);
+#define xlookup(xlat,val) xlookup_ex(xlat,val,NULL)
 extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t);
 extern const char *xlat_idx(const struct xlat *xlat, size_t nmemb, uint64_t val);
 
diff --git a/xlat.c b/xlat.c
index 88e42136..3c5943f8 100644
--- a/xlat.c
+++ b/xlat.c
@@ -71,12 +71,12 @@ print_xlat_val(uint64_t val, enum xlat_style style)
 }
 
 const char *
-xlookup(const struct xlat *xlat, const uint64_t val)
+xlookup_ex(const struct xlat *xlat, const uint64_t val, const char *fallback)
 {
 	for (; xlat->str != NULL; xlat++)
 		if (xlat->val == val)
 			return xlat->str;
-	return NULL;
+	return fallback;
 }
 
 static int
@@ -156,18 +156,15 @@ sprintxval_ex(char *const buf, const size_t size, const struct xlat *const x,
 	if (xlat_verbose(style) == XLAT_STYLE_RAW)
 		return xsnprintf(buf, size, "%s", sprint_xlat_val(val, style));
 
-	const char *const str = xlookup(x, val);
+	const char *const str = xlookup_ex(x, val, dflt);
 
 	if (str) {
-		if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
+		if (xlat_verbose(style) == XLAT_STYLE_VERBOSE || str == dflt)
 			return xsnprintf(buf, size, "%s /* %s */",
 					 sprint_xlat_val(val, style), str);
 		else
 			return xsnprintf(buf, size, "%s", str);
 	}
-	if (dflt)
-		return xsnprintf(buf, size, "%s /* %s */",
-				 sprint_xlat_val(val, style), dflt);
 
 	return xsnprintf(buf, size, "%s", sprint_xlat_val(val, style));
 }
-- 
2.17.0



More information about the Strace-devel mailing list