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 (xlat_verbose(style) == XLAT_STYLE_DEFAULT)
40 return style | xlat_verbosity;
45 static inline const char *
46 sprint_xlat_val(uint64_t val, enum xlat_style style)
48 static char buf[sizeof(val) * 3];
50 switch (xlat_format(style)) {
51 case XLAT_STYLE_FMT_D:
52 xsprintf(buf, "%" PRId64, val);
55 case XLAT_STYLE_FMT_U:
56 xsprintf(buf, "%" PRIu64, val);
59 case XLAT_STYLE_FMT_X:
60 xsprintf(buf, "%#" PRIx64, val);
68 print_xlat_val(uint64_t val, enum xlat_style style)
70 tprints(sprint_xlat_val(val, style));
74 xlookup(const struct xlat *xlat, const uint64_t val)
76 static const struct xlat *pos;
81 for (; pos->str != NULL; pos++)
88 xlat_bsearch_compare(const void *a, const void *b)
90 const uint64_t val1 = *(const uint64_t *) a;
91 const uint64_t val2 = ((const struct xlat *) b)->val;
92 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
96 xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val)
98 static const struct xlat *pos;
99 static size_t memb_left;
106 const struct xlat *e =
107 bsearch((const void *) &val,
108 pos, memb_left, sizeof(*pos), xlat_bsearch_compare);
111 memb_left -= e - pos;
119 * Print entry in struct xlat table, if there.
121 * @param val Value to search a literal representation for.
122 * @param dflt String (abbreviated in comment syntax) which should be emitted
123 * if no appropriate xlat value has been found.
124 * @param style Style in which xlat value should be printed.
125 * @param xlat (And the following arguments) Pointers to arrays of xlat values.
126 * The last argument should be NULL.
127 * @return 1 if appropriate xlat value has been found, 0 otherwise.
130 printxvals_ex(const uint64_t val, const char *dflt, enum xlat_style style,
131 const struct xlat *xlat, ...)
133 static const struct xlat *last;
135 style = get_xlat_style(style);
137 if (xlat_verbose(style) == XLAT_STYLE_RAW) {
138 print_xlat_val(val, style);
142 const char *str = NULL;
145 va_start(args, xlat);
150 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
153 str = xlookup(xlat, val);
156 if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
157 print_xlat_val(val, style);
158 tprints_comment(str);
163 goto printxvals_ex_end;
167 /* No hits -- print raw # instead. */
168 print_xlat_val(val, style);
169 tprints_comment(dflt);
178 sprintxval_ex(char *const buf, const size_t size, const struct xlat *const x,
179 const unsigned int val, const char *const dflt,
180 enum xlat_style style)
182 style = get_xlat_style(style);
184 if (xlat_verbose(style) == XLAT_STYLE_RAW)
185 return xsnprintf(buf, size, "%s", sprint_xlat_val(val, style));
187 const char *const str = xlookup(x, val);
190 if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
191 return xsnprintf(buf, size, "%s /* %s */",
192 sprint_xlat_val(val, style), str);
194 return xsnprintf(buf, size, "%s", str);
197 return xsnprintf(buf, size, "%s /* %s */",
198 sprint_xlat_val(val, style), dflt);
200 return xsnprintf(buf, size, "%s", sprint_xlat_val(val, style));
204 * Print entry in sorted struct xlat table, if it is there.
206 * @param xlat Pointer to an array of xlat values (not terminated with
208 * @param xlat_size Number of xlat elements present in array (usually ARRAY_SIZE
209 * if array is declared in the unit's scope and not
210 * terminated with XLAT_END).
211 * @param val Value to search literal representation for.
212 * @param dflt String (abbreviated in comment syntax) which should be
213 * emitted if no appropriate xlat value has been found.
214 * @param style Style in which xlat value should be printed.
215 * @param fn Search function.
216 * @return 1 if appropriate xlat value has been found, 0
220 printxval_sized(const struct xlat *xlat, size_t xlat_size, uint64_t val,
221 const char *dflt, enum xlat_style style,
222 const char *(* fn)(const struct xlat *, size_t, uint64_t))
224 style = get_xlat_style(style);
226 if (xlat_verbose(style) == XLAT_STYLE_RAW) {
227 print_xlat_val(val, style);
231 const char *s = fn(xlat, xlat_size, val);
234 if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
235 print_xlat_val(val, style);
243 print_xlat_val(val, style);
244 tprints_comment(dflt);
250 printxval_searchn_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
251 const char *dflt, enum xlat_style style)
253 return printxval_sized(xlat, xlat_size, val, dflt, style,
258 xlat_idx(const struct xlat *xlat, size_t nmemb, uint64_t val)
260 static const struct xlat *pos;
261 static size_t memb_left;
268 if (val >= memb_left)
271 if (val != pos[val].val) {
272 error_func_msg("Unexpected xlat value %" PRIu64
273 " at index %" PRIu64,
282 printxval_indexn_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
283 const char *dflt, enum xlat_style style)
285 return printxval_sized(xlat, xlat_size, val, dflt, style, xlat_idx);
289 * Interpret `xlat' as an array of flags.
290 * Print to static string the entries whose bits are on in `flags'
291 * Return static string. If 0 is provided as flags, and there is no flag that
292 * has the value of 0 (it should be the first in xlat table), return NULL.
295 * +------------+------------+---------+------------+
296 * | flags != 0 | xlat found | style | output |
297 * +------------+------------+---------+------------+
298 * | false | (any) | raw | <none> |
299 * | true | (any) | raw | VAL |
300 * +------------+------------+---------+------------+
301 * | false | false | abbrev | <none> |
302 * | true | false | abbrev | VAL |
303 * | (any) | true | abbrev | XLAT |
304 * +------------+------------+---------+------------+
305 * | false | false | verbose | <none> |
306 * | true | false | verbose | VAL |
307 * | (any) | true | verbose | VAL (XLAT) |
308 * +------------+------------+---------+------------+
311 sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
312 enum xlat_style style)
314 static char outstr[1024];
318 outptr = stpcpy(outstr, prefix);
319 style = get_xlat_style(style);
321 if (xlat_verbose(style) == XLAT_STYLE_RAW) {
325 outptr = xappendstr(outstr, outptr, "%s",
326 sprint_xlat_val(flags, style));
331 if (flags == 0 && xlat->val == 0 && xlat->str) {
332 if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
333 outptr = xappendstr(outstr, outptr, "0 /* %s */",
336 strcpy(outptr, xlat->str);
342 if (xlat_verbose(style) == XLAT_STYLE_VERBOSE && flags)
343 outptr = xappendstr(outstr, outptr, "%s",
344 sprint_xlat_val(flags, style));
346 for (; flags && xlat->str; xlat++) {
347 if (xlat->val && (flags & xlat->val) == xlat->val) {
350 else if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
351 outptr = stpcpy(outptr, " /* ");
353 outptr = stpcpy(outptr, xlat->str);
362 if (found || xlat_verbose(style) != XLAT_STYLE_VERBOSE)
363 outptr = xappendstr(outstr, outptr, "%s",
364 sprint_xlat_val(flags, style));
370 if (found && xlat_verbose(style) == XLAT_STYLE_VERBOSE)
371 outptr = stpcpy(outptr, " */");
377 * Print flags from multiple xlat tables.
380 * +------------+--------------+------------+---------+------------+
381 * | flags != 0 | dflt != NULL | xlat found | style | output |
382 * +------------+--------------+------------+---------+------------+
383 * | false | false | (any) | raw | <none> |
384 * | false | true | (any) | raw | VAL |
385 * | true | (any) | (any) | raw | VAL |
386 * +------------+--------------+------------+---------+------------+
387 * | false | false | false | abbrev | <none> |
388 * | false | true | false | abbrev | VAL |
389 * | true | false | false | abbrev | VAL |
390 * | true | true | false | abbrev | VAL (DFLT) |
391 * | (any) | (any) | true | abbrev | XLAT |
392 * +------------+--------------+------------+---------+------------+
393 * | false | false | false | verbose | <none> |
394 * | false | true | false | verbose | VAL |
395 * | true | false | false | verbose | VAL |
396 * | true | true | false | verbose | VAL (DFLT) |
397 * | (any) | (any) | true | verbose | VAL (XLAT) |
398 * +------------+--------------+------------+---------+------------+
401 printflags_ex(uint64_t flags, const char *dflt, enum xlat_style style,
402 const struct xlat *xlat, ...)
404 style = get_xlat_style(style);
406 if (xlat_verbose(style) == XLAT_STYLE_RAW) {
408 print_xlat_val(flags, style);
415 const char *init_sep = "";
419 if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
422 print_xlat_val(flags, style);
425 va_start(args, xlat);
426 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
427 for (; (flags || !n) && xlat->str; ++xlat) {
428 if ((flags == xlat->val) ||
429 (xlat->val && (flags & xlat->val) == xlat->val)) {
430 if (xlat_verbose(style) == XLAT_STYLE_VERBOSE
434 (n++ ? "|" : init_sep), xlat->str);
446 print_xlat_val(flags, style);
450 if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
454 if (xlat_verbose(style) != XLAT_STYLE_VERBOSE)
455 print_xlat_val(flags, style);
456 tprints_comment(dflt);
467 print_xlat_ex(const uint64_t val, const char *str, enum xlat_style style)
469 style = get_xlat_style(style);
471 switch (xlat_verbose(style)) {
472 case XLAT_STYLE_ABBREV:
477 ATTRIBUTE_FALLTHROUGH;
480 print_xlat_val(val, style);
484 error_func_msg("Unexpected style value of %#x", style);
485 ATTRIBUTE_FALLTHROUGH;
487 case XLAT_STYLE_VERBOSE:
488 print_xlat_val(val, style);
489 tprints_comment(str);
494 printxval_dispatch_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
495 const char *dflt, enum xlat_type xt,
496 enum xlat_style style)
500 printxvals_ex(val, dflt, style, xlat, NULL);
504 printxval_searchn_ex(xlat, xlat_size, val, dflt, style);
508 printxval_indexn_ex(xlat, xlat_size, val, dflt, style);