2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999-2018 The strace developers.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 static inline enum xlat_style
37 get_xlat_style(enum xlat_style style)
39 if ((style & XLAT_STYLE_VERBOSITY_MASK) == XLAT_STYLE_DEFAULT)
40 return style | xlat_verbosity;
46 xlookup(const struct xlat *xlat, const uint64_t val)
48 for (; xlat->str != NULL; xlat++)
55 xlat_bsearch_compare(const void *a, const void *b)
57 const uint64_t val1 = *(const uint64_t *) a;
58 const uint64_t val2 = ((const struct xlat *) b)->val;
59 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
63 xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val)
65 const struct xlat *e =
66 bsearch((const void *) &val,
67 xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
69 return e ? e->str : NULL;
73 * Print entry in struct xlat table, if there.
75 * @param val Value to search a literal representation for.
76 * @param dflt String (abbreviated in comment syntax) which should be emitted
77 * if no appropriate xlat value has been found.
78 * @param style Style in which xlat value should be printed.
79 * @param xlat (And the following arguments) Pointers to arrays of xlat values.
80 * The last argument should be NULL.
81 * @return 1 if appropriate xlat value has been found, 0 otherwise.
84 printxvals_ex(const uint64_t val, const char *dflt, enum xlat_style style,
85 const struct xlat *xlat, ...)
87 style = get_xlat_style(style);
89 if (style == XLAT_STYLE_RAW) {
90 tprintf("%#" PRIx64, val);
97 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
98 const char *str = xlookup(xlat, val);
101 if (style == XLAT_STYLE_VERBOSE) {
102 tprintf("%#" PRIx64, val);
103 tprints_comment(str);
112 /* No hits -- print raw # instead. */
113 tprintf("%#" PRIx64, val);
114 tprints_comment(dflt);
122 sprintxval_ex(char *const buf, const size_t size, const struct xlat *const x,
123 const unsigned int val, const char *const dflt,
124 enum xlat_style style)
126 style = get_xlat_style(style);
128 if (style == XLAT_STYLE_RAW)
129 return xsnprintf(buf, size, "%#x", val);
131 const char *const str = xlookup(x, val);
134 if (style == XLAT_STYLE_VERBOSE)
135 return xsnprintf(buf, size, "%#x /* %s */", val, str);
137 return xsnprintf(buf, size, "%s", str);
140 return xsnprintf(buf, size, "%#x /* %s */", val, dflt);
142 return xsnprintf(buf, size, "%#x", val);
146 * Print entry in sorted struct xlat table, if it is there.
148 * @param xlat Pointer to an array of xlat values (not terminated with
150 * @param xlat_size Number of xlat elements present in array (usually ARRAY_SIZE
151 * if array is declared in the unit's scope and not
152 * terminated with XLAT_END).
153 * @param val Value to search literal representation for.
154 * @param dflt String (abbreviated in comment syntax) which should be
155 * emitted if no appropriate xlat value has been found.
156 * @param style Style in which xlat value should be printed.
157 * @return 1 if appropriate xlat value has been found, 0
161 printxval_searchn_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
162 const char *dflt, enum xlat_style style)
164 style = get_xlat_style(style);
166 if (style == XLAT_STYLE_RAW) {
167 tprintf("%#" PRIx64, val);
171 const char *s = xlat_search(xlat, xlat_size, val);
174 if (style == XLAT_STYLE_VERBOSE) {
175 tprintf("%#" PRIx64, val);
183 tprintf("%#" PRIx64, val);
184 tprints_comment(dflt);
190 * Interpret `xlat' as an array of flags.
191 * Print to static string the entries whose bits are on in `flags'
192 * Return static string. If 0 is provided as flags, and there is no flag that
193 * has the value of 0 (it should be the first in xlat table), return NULL.
196 * +------------+------------+---------+------------+
197 * | flags != 0 | xlat found | style | output |
198 * +------------+------------+---------+------------+
199 * | false | (any) | raw | <none> |
200 * | true | (any) | raw | VAL |
201 * +------------+------------+---------+------------+
202 * | false | false | abbrev | <none> |
203 * | true | false | abbrev | VAL |
204 * | (any) | true | abbrev | XLAT |
205 * +------------+------------+---------+------------+
206 * | false | false | verbose | <none> |
207 * | true | false | verbose | VAL |
208 * | (any) | true | verbose | VAL (XLAT) |
209 * +------------+------------+---------+------------+
212 sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
213 enum xlat_style style)
215 static char outstr[1024];
219 outptr = stpcpy(outstr, prefix);
220 style = get_xlat_style(style);
222 if (style == XLAT_STYLE_RAW) {
226 outptr = xappendstr(outstr, outptr, "%#" PRIx64, flags);
231 if (flags == 0 && xlat->val == 0 && xlat->str) {
232 if (style == XLAT_STYLE_VERBOSE) {
233 outptr = xappendstr(outstr, outptr, "0 /* %s */",
236 strcpy(outptr, xlat->str);
242 if (style == XLAT_STYLE_VERBOSE && flags)
243 outptr = xappendstr(outstr, outptr, "%#" PRIx64, flags);
245 for (; flags && xlat->str; xlat++) {
246 if (xlat->val && (flags & xlat->val) == xlat->val) {
249 else if (style == XLAT_STYLE_VERBOSE)
250 outptr = stpcpy(outptr, " /* ");
252 outptr = stpcpy(outptr, xlat->str);
261 if (found || style != XLAT_STYLE_VERBOSE)
262 outptr = xappendstr(outstr, outptr, "%#" PRIx64, flags);
268 if (found && style == XLAT_STYLE_VERBOSE)
269 outptr = stpcpy(outptr, " */");
275 * Print flags from multiple xlat tables.
278 * +------------+--------------+------------+---------+------------+
279 * | flags != 0 | dflt != NULL | xlat found | style | output |
280 * +------------+--------------+------------+---------+------------+
281 * | false | false | (any) | raw | <none> |
282 * | false | true | (any) | raw | VAL |
283 * | true | (any) | (any) | raw | VAL |
284 * +------------+--------------+------------+---------+------------+
285 * | false | false | false | abbrev | <none> |
286 * | false | true | false | abbrev | VAL |
287 * | true | false | false | abbrev | VAL |
288 * | true | true | false | abbrev | VAL (DFLT) |
289 * | (any) | (any) | true | abbrev | XLAT |
290 * +------------+--------------+------------+---------+------------+
291 * | false | false | false | verbose | <none> |
292 * | false | true | false | verbose | VAL |
293 * | true | false | false | verbose | VAL |
294 * | true | true | false | verbose | VAL (DFLT) |
295 * | (any) | (any) | true | verbose | VAL (XLAT) |
296 * +------------+--------------+------------+---------+------------+
299 printflags_ex(uint64_t flags, const char *dflt, enum xlat_style style,
300 const struct xlat *xlat, ...)
302 style = get_xlat_style(style);
304 if (style == XLAT_STYLE_RAW) {
306 tprintf("%#" PRIx64, flags);
313 const char *init_sep = "";
317 if (style == XLAT_STYLE_VERBOSE) {
320 tprintf("%#" PRIx64, flags);
323 va_start(args, xlat);
324 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
325 for (; (flags || !n) && xlat->str; ++xlat) {
326 if ((flags == xlat->val) ||
327 (xlat->val && (flags & xlat->val) == xlat->val)) {
328 if (style == XLAT_STYLE_VERBOSE && !flags)
331 (n++ ? "|" : init_sep), xlat->str);
342 tprintf("|%#" PRIx64, flags);
346 if (style == XLAT_STYLE_VERBOSE)
350 if (style != XLAT_STYLE_VERBOSE)
351 tprintf("%#" PRIx64, flags);
352 tprints_comment(dflt);
363 print_xlat_ex(const uint64_t val, const char *str, enum xlat_style style)
365 style = get_xlat_style(style);
369 tprintf("%#" PRIx64, val);
372 case XLAT_STYLE_ABBREV:
377 error_func_msg("Unexpected style value of %#x", style);
378 ATTRIBUTE_FALLTHROUGH;
380 case XLAT_STYLE_VERBOSE:
381 tprintf("%#" PRIx64, val);
382 tprints_comment(str);