2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 1998,2004,2007,2008,2009 Juan Cespedes
5 * Copyright (C) 2006 Ian Wienand
6 * Copyright (C) 2006 Steve Fink
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
33 #include "lens_default.h"
40 #define READER(NAME, TYPE) \
42 NAME(struct value *value, TYPE *ret, struct value_dict *arguments) \
46 unsigned char buf[0]; \
48 if (value_extract_buf(value, u.buf, arguments) < 0) \
54 READER(read_float, float)
55 READER(read_double, double)
59 #define HANDLE_WIDTH(BITS) \
62 if (value_extract_word(value, &l, arguments) < 0) \
64 int##BITS##_t i = l; \
65 uint64_t v = (uint64_t)(uint##BITS##_t)i; \
67 case INT_FMT_unknown: \
68 if (l < -10000 || l > 10000) \
70 return fprintf(stream, "%#"PRIx64, v); \
72 case INT_FMT_default: \
73 return fprintf(stream, "%"PRIi##BITS, i); \
75 return fprintf(stream, "%"PRIu64, v); \
77 return fprintf(stream, "0%"PRIo64, v); \
92 format_integer(FILE *stream, struct value *value, enum int_fmt_t format,
93 struct value_dict *arguments)
95 switch (type_sizeof(value->inferior, value->type)) {
97 case 1: HANDLE_WIDTH(8);
98 case 2: HANDLE_WIDTH(16);
99 case 4: HANDLE_WIDTH(32);
100 case 8: HANDLE_WIDTH(64);
103 assert(!"unsupported integer width");
114 acc_fprintf(int *countp, FILE *stream, const char *format, ...)
117 va_start(pa, format);
118 int i = account_output(countp, vfprintf(stream, format, pa));
125 format_char(FILE *stream, struct value *value, struct value_dict *arguments)
128 if (value_extract_word(value, &lc, arguments) < 0)
165 if (isprint(c) || c == ' ')
171 return fprintf(stream, fmt, c);
175 format_naked_char(FILE *stream, struct value *value,
176 struct value_dict *arguments)
179 if (acc_fprintf(&written, stream, "'") < 0
180 || account_output(&written,
181 format_char(stream, value, arguments)) < 0
182 || acc_fprintf(&written, stream, "'") < 0)
189 format_double(FILE *stream, double value, enum int_fmt_t format)
191 if (format == INT_FMT_x)
192 return fprintf(stream, "%a", value);
194 return fprintf(stream, "%f", value);
198 format_floating(FILE *stream, struct value *value, struct value_dict *arguments,
199 enum int_fmt_t format)
201 switch (value->type->type) {
205 if (read_float(value, &f, arguments) < 0)
207 return format_double(stream, f, format);
209 if (read_double(value, &d, arguments) < 0)
211 return format_double(stream, d, format);
217 struct format_argument_data
220 struct value_dict *arguments;
224 format_argument_cb(FILE *stream, void *ptr)
226 struct format_argument_data *data = ptr;
227 return format_argument(stream, data->value, data->arguments);
231 format_struct(FILE *stream, struct value *value, struct value_dict *arguments)
234 if (acc_fprintf(&written, stream, "{ ") < 0)
239 for (i = 0; i < type_struct_size(value->type); ++i) {
240 struct value element;
241 if (value_init_element(&element, value, i) < 0)
244 struct format_argument_data data = { &element, arguments };
245 int o = delim_output(stream, &need_delim,
246 format_argument_cb, &data);
247 value_destroy(&element);
253 if (acc_fprintf(&written, stream, " }") < 0)
259 format_pointer(FILE *stream, struct value *value, struct value_dict *arguments)
261 if (value_is_zero(value, arguments))
262 return fprintf(stream, "nil");
264 /* The following is for detecting recursion. We keep track of
265 * the values that were already displayed. Each time a
266 * pointer should be dereferenced, we compare its value to the
267 * value of each of the pointers dereferenced so far. If one
268 * of them matches, instead of recursing, we just printf which
269 * superstructure this pointer recurses to. */
270 static struct vect pointers = {};
271 if (pointers.elt_size == 0)
272 VECT_INIT(&pointers, struct value *);
274 /* Trim number of expanded structures of the same type. Even
275 * for non-recursive structure, we don't want to expand all of
276 * it if it's huge. */
278 size_t len = vect_size(&pointers);
279 assert(value->type->type == ARGTYPE_POINTER);
280 struct arg_type_info *pointee = value->type->u.ptr_info.info;
281 if (pointee->type == ARGTYPE_STRUCT) {
283 for (i = 0; i < len; ++i) {
285 = *VECT_ELEMENT(&pointers, struct value *, i);
286 assert(old->type->type == ARGTYPE_POINTER);
287 struct arg_type_info *old_pointee
288 = old->type->u.ptr_info.info;
289 if (old_pointee == pointee)
292 if (depth >= options.arraylen)
293 return fprintf(stream, "...");
296 for (i = len; i-- > 0 ;) {
297 struct value **old = VECT_ELEMENT(&pointers, struct value *, i);
298 int rc = value_equal(value, *old, arguments);
302 size_t reclevel = len - i - 1;
303 char buf[reclevel + 1];
304 memset(buf, '^', sizeof buf);
306 return fprintf(stream, "recurse%s", buf);
310 /* OK, not a recursion. Remember this value for tracking. */
311 if (VECT_PUSHBACK(&pointers, &value) < 0)
314 struct value element;
316 if (value_init_deref(&element, value) < 0) {
320 o = format_argument(stream, &element, arguments);
321 value_destroy(&element);
324 vect_popback(&pointers);
329 * LENGTH is an expression whose evaluation will yield the actual
330 * length of the array.
332 * MAXLEN is the actual maximum length that we care about
334 * BEFORE if LENGTH>MAXLEN, we display ellipsis. We display it before
335 * the closing parenthesis if BEFORE, otherwise after it.
337 * OPEN, CLOSE, DELIM are opening and closing parenthesis and element
341 format_array(FILE *stream, struct value *value, struct value_dict *arguments,
342 struct expr_node *length, size_t maxlen, int before,
343 const char *open, const char *close, const char *delim)
345 /* We need "long" to be long enough to cover the whole address
347 typedef char assert__long_enough_long[-(sizeof(long) < sizeof(void *))];
349 if (expr_eval_word(length, value, arguments, &l) < 0)
351 size_t len = (size_t)l;
354 if (acc_fprintf(&written, stream, "%s", open) < 0)
358 for (i = 0; i < len && i <= maxlen; ++i) {
360 if (before && acc_fprintf(&written, stream, "...") < 0)
365 if (i > 0 && acc_fprintf(&written, stream, "%s", delim) < 0)
368 struct value element;
369 if (value_init_element(&element, value, i) < 0)
371 int o = format_argument(stream, &element, arguments);
372 value_destroy(&element);
377 if (acc_fprintf(&written, stream, "%s", close) < 0)
379 if (i == maxlen && !before && acc_fprintf(&written, stream, "...") < 0)
386 toplevel_format_lens(struct lens *lens, FILE *stream,
387 struct value *value, struct value_dict *arguments,
388 enum int_fmt_t int_fmt)
390 switch (value->type->type) {
392 return fprintf(stream, "<void>");
397 return format_integer(stream, value, int_fmt, arguments);
402 if (int_fmt == INT_FMT_i || int_fmt == INT_FMT_default)
404 return format_integer(stream, value, int_fmt, arguments);
407 if (int_fmt == INT_FMT_default)
408 return format_naked_char(stream, value, arguments);
409 return format_integer(stream, value, int_fmt, arguments);
413 return format_floating(stream, value, arguments, int_fmt);
416 return format_struct(stream, value, arguments);
418 case ARGTYPE_POINTER:
419 if (value->type->u.array_info.elt_type->type != ARGTYPE_VOID)
420 return format_pointer(stream, value, arguments);
421 return format_integer(stream, value, INT_FMT_x, arguments);
424 return format_array(stream, value, arguments,
425 value->type->u.array_info.length,
426 options.arraylen, 1, "[ ", " ]", ", ");
432 default_lens_format_cb(struct lens *lens, FILE *stream,
433 struct value *value, struct value_dict *arguments)
435 return toplevel_format_lens(lens, stream, value, arguments,
439 struct lens default_lens = {
440 .format_cb = default_lens_format_cb,
445 blind_lens_format_cb(struct lens *lens, FILE *stream,
446 struct value *value, struct value_dict *arguments)
451 struct lens blind_lens = {
452 .format_cb = blind_lens_format_cb,
457 octal_lens_format_cb(struct lens *lens, FILE *stream,
458 struct value *value, struct value_dict *arguments)
460 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_o);
463 struct lens octal_lens = {
464 .format_cb = octal_lens_format_cb,
469 hex_lens_format_cb(struct lens *lens, FILE *stream,
470 struct value *value, struct value_dict *arguments)
472 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_x);
475 struct lens hex_lens = {
476 .format_cb = hex_lens_format_cb,
481 dec_lens_format_cb(struct lens *lens, FILE *stream,
482 struct value *value, struct value_dict *arguments)
484 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_u);
487 struct lens dec_lens = {
488 .format_cb = dec_lens_format_cb,
493 guess_lens_format_cb(struct lens *lens, FILE *stream,
494 struct value *value, struct value_dict *arguments)
496 return toplevel_format_lens(lens, stream, value, arguments,
500 struct lens guess_lens = {
501 .format_cb = guess_lens_format_cb,
506 bool_lens_format_cb(struct lens *lens, FILE *stream,
507 struct value *value, struct value_dict *arguments)
509 switch (value->type->type) {
514 case ARGTYPE_POINTER:
516 return toplevel_format_lens(lens, stream, value,
517 arguments, INT_FMT_default);
527 if ((zero = value_is_zero(value, arguments)) < 0)
530 return fprintf(stream, "false");
532 return fprintf(stream, "true");
537 struct lens bool_lens = {
538 .format_cb = bool_lens_format_cb,
543 string_lens_format_cb(struct lens *lens, FILE *stream,
544 struct value *value, struct value_dict *arguments)
546 switch (value->type->type) {
547 case ARGTYPE_POINTER:
548 /* This should really be written as either "string",
549 * or, if lens, then string(array(char, zero)*). But
550 * I suspect people are so used to the char * C idiom,
551 * that string(char *) might actually turn up. So
552 * let's just support it. */
553 if (value->type->u.ptr_info.info->type == ARGTYPE_CHAR) {
554 struct arg_type_info info[2];
555 type_init_array(&info[1],
556 value->type->u.ptr_info.info, 0,
557 expr_node_zero(), 0);
558 type_init_pointer(&info[0], &info[1], 0);
562 if (value_clone(&tmp, value) < 0)
564 value_set_type(&tmp, info, 0);
565 int ret = string_lens_format_cb(lens, stream, &tmp,
567 type_destroy(&info[0]);
568 type_destroy(&info[1]);
584 return toplevel_format_lens(lens, stream, value,
585 arguments, INT_FMT_default);
588 return format_char(stream, value, arguments);
591 return format_array(stream, value, arguments,
592 value->type->u.array_info.length,
593 options.strlen, 0, "\"", "\"", "");
598 struct lens string_lens = {
599 .format_cb = string_lens_format_cb,
603 out_bits(FILE *stream, size_t low, size_t high)
606 return fprintf(stream, "%zd", low);
608 return fprintf(stream, "%zd-%zd", low, high);
615 for (; u > 0; u &= u - 1)
621 bitvect_lens_format_cb(struct lens *lens, FILE *stream,
622 struct value *value, struct value_dict *arguments)
624 unsigned char *data = value_get_data(value, arguments);
627 size_t sz = type_sizeof(value->inferior, value->type);
628 if (sz == (size_t)-1)
632 unsigned char buf[sz];
633 switch ((int)value->type->type) {
634 union bitvect_integral_64
640 unsigned char buf[0];
643 case ARGTYPE_POINTER:
644 return format_pointer(stream, value, arguments);
651 assert(sz <= sizeof(bv));
652 memmove(bv.buf, data, sz);
661 for (i = 0; i < sz; ++i) {
662 buf[i] = bv.u64 & 0xff;
669 for (i = 0; i < sz; ++i)
670 bits += bitcount(data[i]);
672 /* If there's more 1's than 0's, show inverse. */
673 unsigned neg = bits > sz * 4 ? 0xff : 0x00;
676 if (acc_fprintf(&o, stream, "%s<", "~" + (neg == 0x00)) < 0)
681 for (i = 0; i < sz; ++i) {
683 unsigned char d = data[i] ^ neg;
684 for (m = 0x01; m != 0; m <<= 1) {
689 && acc_fprintf(&o, stream, ",") < 0)
694 if (account_output(&o, out_bits(stream, low,
702 if (low >= 0 && account_output(&o, out_bits(stream, low, bitno-1)) < 0)
705 if (fputc('>', stream) < 0)
712 struct lens bitvect_lens = {
713 .format_cb = bitvect_lens_format_cb,