2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013 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
34 #include "lens_default.h"
41 #define READER(NAME, TYPE) \
43 NAME(struct value *value, TYPE *ret, struct value_dict *arguments) \
47 unsigned char buf[0]; \
49 if (value_extract_buf(value, u.buf, arguments) < 0) \
55 READER(read_float, float)
56 READER(read_double, double)
60 #define HANDLE_WIDTH(BITS) \
63 if (value_extract_word(value, &l, arguments) < 0) \
65 int##BITS##_t i = l; \
66 uint64_t v = (uint64_t)(uint##BITS##_t)i; \
68 case INT_FMT_unknown: \
69 if (l < -10000 || l > 10000) \
71 return fprintf(stream, "%#"PRIx64, v); \
73 case INT_FMT_default: \
74 return fprintf(stream, "%"PRIi##BITS, i); \
76 return fprintf(stream, "%"PRIu64, v); \
78 return fprintf(stream, "0%"PRIo64, v); \
93 format_integer(FILE *stream, struct value *value, enum int_fmt_t format,
94 struct value_dict *arguments)
96 switch (type_sizeof(value->inferior, value->type)) {
98 case 1: HANDLE_WIDTH(8);
99 case 2: HANDLE_WIDTH(16);
100 case 4: HANDLE_WIDTH(32);
101 case 8: HANDLE_WIDTH(64);
104 assert(!"unsupported integer width");
115 acc_fprintf(int *countp, FILE *stream, const char *format, ...)
118 va_start(pa, format);
119 int i = account_output(countp, vfprintf(stream, format, pa));
126 print_char(FILE *stream, int c)
161 if (isprint(c) || c == ' ')
167 return fprintf(stream, fmt, c);
171 format_char(FILE *stream, struct value *value, struct value_dict *arguments)
174 if (value_extract_word(value, &lc, arguments) < 0)
176 return print_char(stream, (int) lc);
180 format_naked(FILE *stream, struct value *value,
181 struct value_dict *arguments,
182 int (*what)(FILE *, struct value *, struct value_dict *))
185 if (acc_fprintf(&written, stream, "'") < 0
186 || account_output(&written,
187 what(stream, value, arguments)) < 0
188 || acc_fprintf(&written, stream, "'") < 0)
195 format_double(FILE *stream, double value, enum int_fmt_t format)
197 if (format == INT_FMT_x)
198 return fprintf(stream, "%a", value);
200 return fprintf(stream, "%f", value);
204 format_floating(FILE *stream, struct value *value, struct value_dict *arguments,
205 enum int_fmt_t format)
207 switch (value->type->type) {
211 if (read_float(value, &f, arguments) < 0)
213 return format_double(stream, f, format);
215 if (read_double(value, &d, arguments) < 0)
217 return format_double(stream, d, format);
223 struct format_argument_data
226 struct value_dict *arguments;
230 format_argument_cb(FILE *stream, void *ptr)
232 struct format_argument_data *data = ptr;
233 return format_argument(stream, data->value, data->arguments);
237 format_struct(FILE *stream, struct value *value, struct value_dict *arguments)
240 if (acc_fprintf(&written, stream, "{ ") < 0)
245 for (i = 0; i < type_struct_size(value->type); ++i) {
246 struct value element;
247 if (value_init_element(&element, value, i) < 0)
250 struct format_argument_data data = { &element, arguments };
251 int o = delim_output(stream, &need_delim,
252 format_argument_cb, &data);
253 value_destroy(&element);
259 if (acc_fprintf(&written, stream, " }") < 0)
264 static const char null_message[] = "nil";
266 format_pointer(FILE *stream, struct value *value, struct value_dict *arguments)
268 if (value_is_zero(value, arguments))
269 return fprintf(stream, null_message);
271 /* The following is for detecting recursion. We keep track of
272 * the values that were already displayed. Each time a
273 * pointer should be dereferenced, we compare its value to the
274 * value of each of the pointers dereferenced so far. If one
275 * of them matches, instead of recursing, we just printf which
276 * superstructure this pointer recurses to. */
277 static struct vect pointers = {};
278 if (pointers.elt_size == 0)
279 VECT_INIT(&pointers, struct value *);
281 /* Trim number of expanded structures of the same type. Even
282 * for non-recursive structure, we don't want to expand all of
283 * it if it's huge. */
285 size_t len = vect_size(&pointers);
286 assert(value->type->type == ARGTYPE_POINTER);
287 struct arg_type_info *pointee = value->type->u.ptr_info.info;
288 if (pointee->type == ARGTYPE_STRUCT) {
290 for (i = 0; i < len; ++i) {
292 = *VECT_ELEMENT(&pointers, struct value *, i);
293 assert(old->type->type == ARGTYPE_POINTER);
294 struct arg_type_info *old_pointee
295 = old->type->u.ptr_info.info;
296 if (old_pointee == pointee)
299 if (depth >= options.arraylen)
300 return fprintf(stream, "...");
303 for (i = len; i-- > 0 ;) {
304 struct value **old = VECT_ELEMENT(&pointers, struct value *, i);
305 int rc = value_equal(value, *old, arguments);
309 size_t reclevel = len - i - 1;
310 char buf[reclevel + 1];
311 memset(buf, '^', sizeof buf);
313 return fprintf(stream, "recurse%s", buf);
317 /* OK, not a recursion. Remember this value for tracking. */
318 if (VECT_PUSHBACK(&pointers, &value) < 0)
321 struct value element;
323 if (value_init_deref(&element, value) < 0) {
327 o = format_argument(stream, &element, arguments);
328 value_destroy(&element);
331 VECT_POPBACK(&pointers, struct value *, NULL, NULL);
336 * LENGTH is an expression whose evaluation will yield the actual
337 * length of the array.
339 * MAXLEN is the actual maximum length that we care about
341 * BEFORE if LENGTH>MAXLEN, we display ellipsis. We display it before
342 * the closing parenthesis if BEFORE, otherwise after it.
344 * OPEN, CLOSE, DELIM are opening and closing parenthesis and element
348 format_array(FILE *stream, struct value *value, struct value_dict *arguments,
349 struct expr_node *length, size_t maxlen, int before,
350 const char *open, const char *close, const char *delim)
352 /* We need "long" to be long enough to cover the whole address
354 (void)sizeof(char[1 - 2*(sizeof(long) < sizeof(void *))]);
356 if (expr_eval_word(length, value, arguments, &l) < 0)
358 size_t len = (size_t)l;
361 if (acc_fprintf(&written, stream, "%s", open) < 0)
365 for (i = 0; i < len && i <= maxlen; ++i) {
367 if (before && acc_fprintf(&written, stream, "...") < 0)
372 if (i > 0 && acc_fprintf(&written, stream, "%s", delim) < 0)
375 struct value element;
376 if (value_init_element(&element, value, i) < 0)
378 int o = format_argument(stream, &element, arguments);
379 value_destroy(&element);
384 if (acc_fprintf(&written, stream, "%s", close) < 0)
386 if (i == maxlen && !before && acc_fprintf(&written, stream, "...") < 0)
393 toplevel_format_lens(struct lens *lens, FILE *stream,
394 struct value *value, struct value_dict *arguments,
395 enum int_fmt_t int_fmt)
397 switch (value->type->type) {
399 return fprintf(stream, "<void>");
404 return format_integer(stream, value, int_fmt, arguments);
409 if (int_fmt == INT_FMT_i || int_fmt == INT_FMT_default)
411 return format_integer(stream, value, int_fmt, arguments);
414 if (int_fmt == INT_FMT_default)
415 return format_naked(stream, value, arguments,
417 return format_integer(stream, value, int_fmt, arguments);
421 return format_floating(stream, value, arguments, int_fmt);
424 return format_struct(stream, value, arguments);
426 case ARGTYPE_POINTER:
427 if (value_is_zero(value, arguments))
428 return fprintf(stream, null_message);
429 if (value->type->u.array_info.elt_type->type != ARGTYPE_VOID)
430 return format_pointer(stream, value, arguments);
431 return format_integer(stream, value, INT_FMT_x, arguments);
434 return format_array(stream, value, arguments,
435 value->type->u.array_info.length,
436 options.arraylen, 1, "[ ", " ]", ", ");
442 default_lens_format_cb(struct lens *lens, FILE *stream,
443 struct value *value, struct value_dict *arguments)
445 return toplevel_format_lens(lens, stream, value, arguments,
449 struct lens default_lens = {
450 .format_cb = default_lens_format_cb,
455 blind_lens_format_cb(struct lens *lens, FILE *stream,
456 struct value *value, struct value_dict *arguments)
461 struct lens blind_lens = {
462 .format_cb = blind_lens_format_cb,
467 octal_lens_format_cb(struct lens *lens, FILE *stream,
468 struct value *value, struct value_dict *arguments)
470 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_o);
473 struct lens octal_lens = {
474 .format_cb = octal_lens_format_cb,
479 hex_lens_format_cb(struct lens *lens, FILE *stream,
480 struct value *value, struct value_dict *arguments)
482 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_x);
485 struct lens hex_lens = {
486 .format_cb = hex_lens_format_cb,
491 dec_lens_format_cb(struct lens *lens, FILE *stream,
492 struct value *value, struct value_dict *arguments)
494 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_u);
497 struct lens dec_lens = {
498 .format_cb = dec_lens_format_cb,
503 guess_lens_format_cb(struct lens *lens, FILE *stream,
504 struct value *value, struct value_dict *arguments)
506 return toplevel_format_lens(lens, stream, value, arguments,
510 struct lens guess_lens = {
511 .format_cb = guess_lens_format_cb,
516 bool_lens_format_cb(struct lens *lens, FILE *stream,
517 struct value *value, struct value_dict *arguments)
519 switch (value->type->type) {
524 case ARGTYPE_POINTER:
526 return toplevel_format_lens(lens, stream, value,
527 arguments, INT_FMT_default);
537 if ((zero = value_is_zero(value, arguments)) < 0)
540 return fprintf(stream, "false");
542 return fprintf(stream, "true");
547 struct lens bool_lens = {
548 .format_cb = bool_lens_format_cb,
552 redispatch_as_array(struct lens *lens, FILE *stream,
553 struct value *value, struct value_dict *arguments,
554 int (*cb)(struct lens *, FILE *,
555 struct value *, struct value_dict *))
557 struct arg_type_info info[2];
558 type_init_array(&info[1], value->type->u.ptr_info.info, 0,
559 expr_node_zero(), 0);
560 type_init_pointer(&info[0], &info[1], 0);
564 if (value_clone(&tmp, value) < 0)
566 value_set_type(&tmp, info, 0);
567 int ret = cb(lens, stream, &tmp, arguments);
568 type_destroy(&info[0]);
569 type_destroy(&info[1]);
575 format_wchar(FILE *stream, struct value *value, struct value_dict *arguments)
578 if (value_extract_word(value, &l, arguments) < 0)
580 wchar_t wc = (wchar_t) l;
581 char buf[MB_CUR_MAX + 1];
583 int c = wctomb(buf, wc);
587 return print_char(stream, buf[0]);
590 return fprintf(stream, "%s", buf) >= 0 ? 1 : -1;
594 string_lens_format_cb(struct lens *lens, FILE *stream,
595 struct value *value, struct value_dict *arguments)
597 switch (value->type->type) {
598 case ARGTYPE_POINTER:
599 /* This should really be written as either "string",
600 * or, if lens, then string(array(char, zero)*). But
601 * I suspect people are so used to the char * C idiom,
602 * that string(char *) might actually turn up. So
603 * let's just support it. */
604 switch ((int) value->type->u.ptr_info.info->type)
612 return redispatch_as_array(lens, stream, value,
614 &string_lens_format_cb);
616 /* Otherwise dispatch to whatever the default for the
617 * pointee is--most likely this will again be us. */
623 return toplevel_format_lens(lens, stream, value,
624 arguments, INT_FMT_default);
632 if (value->parent != NULL && value->type->lens == NULL)
633 return format_wchar(stream, value, arguments);
635 return format_naked(stream, value, arguments,
639 return format_char(stream, value, arguments);
642 return format_array(stream, value, arguments,
643 value->type->u.array_info.length,
644 options.strlen, 0, "\"", "\"", "");
649 struct lens string_lens = {
650 .format_cb = string_lens_format_cb,
654 out_bits(FILE *stream, size_t low, size_t high)
657 return fprintf(stream, "%zd", low);
659 return fprintf(stream, "%zd-%zd", low, high);
663 bitvect_lens_format_cb(struct lens *lens, FILE *stream,
664 struct value *value, struct value_dict *arguments)
666 unsigned char *data = value_get_data(value, arguments);
669 size_t sz = type_sizeof(value->inferior, value->type);
670 if (sz == (size_t)-1)
674 unsigned char buf[sz];
675 switch ((int)value->type->type) {
676 union bitvect_integral_64
682 unsigned char buf[0];
685 case ARGTYPE_POINTER:
686 return format_pointer(stream, value, arguments);
693 assert(sz <= sizeof(bv));
694 memmove(bv.buf, data, sz);
703 for (i = 0; i < sz; ++i) {
704 buf[i] = bv.u64 & 0xff;
711 for (i = 0; i < sz; ++i)
712 bits += bitcount(data[i]);
714 /* If there's more 1's than 0's, show inverse. */
715 unsigned neg = bits > sz * 4 ? 0xff : 0x00;
718 if (acc_fprintf(&o, stream, "%s<", &"~"[neg == 0x00]) < 0)
723 for (i = 0; i < sz; ++i) {
725 unsigned char d = data[i] ^ neg;
726 for (m = 0x01; m != 0; m <<= 1) {
731 && acc_fprintf(&o, stream, ",") < 0)
736 if (account_output(&o, out_bits(stream, low,
744 if (low >= 0 && account_output(&o, out_bits(stream, low, bitno-1)) < 0)
747 if (fputc('>', stream) < 0)
754 struct lens bitvect_lens = {
755 .format_cb = bitvect_lens_format_cb,