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
24 #define _XOPEN_SOURCE /* For wcwidth from wchar.h. */
37 #include "lens_default.h"
44 #define READER(NAME, TYPE) \
46 NAME(struct value *value, TYPE *ret, struct value_dict *arguments) \
50 unsigned char buf[0]; \
52 if (value_extract_buf(value, u.buf, arguments) < 0) \
58 READER(read_float, float)
59 READER(read_double, double)
63 #define HANDLE_WIDTH(BITS) \
66 if (value_extract_word(value, &l, arguments) < 0) \
68 int##BITS##_t i = l; \
69 uint64_t v = (uint64_t)(uint##BITS##_t)i; \
71 case INT_FMT_unknown: \
72 if (l < -10000 || l > 10000) \
74 return fprintf(stream, "%#"PRIx64, v); \
76 case INT_FMT_default: \
77 return fprintf(stream, "%"PRIi##BITS, i); \
79 return fprintf(stream, "%"PRIu64, v); \
81 return fprintf(stream, "0%"PRIo64, v); \
96 format_integer(FILE *stream, struct value *value, enum int_fmt_t format,
97 struct value_dict *arguments)
99 switch (type_sizeof(value->inferior, value->type)) {
101 case 1: HANDLE_WIDTH(8);
102 case 2: HANDLE_WIDTH(16);
103 case 4: HANDLE_WIDTH(32);
104 case 8: HANDLE_WIDTH(64);
107 assert(!"unsupported integer width");
118 acc_fprintf(int *countp, FILE *stream, const char *format, ...)
121 va_start(pa, format);
122 int i = account_output(countp, vfprintf(stream, format, pa));
129 print_char(FILE *stream, int c)
164 if (isprint(c) || c == ' ')
170 return fprintf(stream, fmt, c);
174 format_char(FILE *stream, struct value *value, struct value_dict *arguments)
177 if (value_extract_word(value, &lc, arguments) < 0)
179 return print_char(stream, (int) lc);
183 format_naked(FILE *stream, struct value *value,
184 struct value_dict *arguments,
185 int (*what)(FILE *, struct value *, struct value_dict *))
188 if (acc_fprintf(&written, stream, "'") < 0
189 || account_output(&written,
190 what(stream, value, arguments)) < 0
191 || acc_fprintf(&written, stream, "'") < 0)
198 format_double(FILE *stream, double value, enum int_fmt_t format)
200 if (format == INT_FMT_x)
201 return fprintf(stream, "%a", value);
203 return fprintf(stream, "%f", value);
207 format_floating(FILE *stream, struct value *value, struct value_dict *arguments,
208 enum int_fmt_t format)
210 switch (value->type->type) {
214 if (read_float(value, &f, arguments) < 0)
216 return format_double(stream, f, format);
218 if (read_double(value, &d, arguments) < 0)
220 return format_double(stream, d, format);
226 struct format_argument_data
229 struct value_dict *arguments;
233 format_argument_cb(FILE *stream, void *ptr)
235 struct format_argument_data *data = ptr;
236 return format_argument(stream, data->value, data->arguments);
240 format_struct(FILE *stream, struct value *value, struct value_dict *arguments)
243 if (acc_fprintf(&written, stream, "{ ") < 0)
248 for (i = 0; i < type_struct_size(value->type); ++i) {
249 struct value element;
250 if (value_init_element(&element, value, i) < 0)
253 struct format_argument_data data = { &element, arguments };
254 int o = delim_output(stream, &need_delim,
255 format_argument_cb, &data);
256 value_destroy(&element);
262 if (acc_fprintf(&written, stream, " }") < 0)
267 static const char null_message[] = "nil";
269 format_pointer(FILE *stream, struct value *value, struct value_dict *arguments)
271 if (value_is_zero(value, arguments))
272 return fprintf(stream, null_message);
274 /* The following is for detecting recursion. We keep track of
275 * the values that were already displayed. Each time a
276 * pointer should be dereferenced, we compare its value to the
277 * value of each of the pointers dereferenced so far. If one
278 * of them matches, instead of recursing, we just printf which
279 * superstructure this pointer recurses to. */
280 static struct vect pointers = {};
281 if (pointers.elt_size == 0)
282 VECT_INIT(&pointers, struct value *);
284 /* Trim number of expanded structures of the same type. Even
285 * for non-recursive structure, we don't want to expand all of
286 * it if it's huge. */
288 size_t len = vect_size(&pointers);
289 assert(value->type->type == ARGTYPE_POINTER);
290 struct arg_type_info *pointee = value->type->u.ptr_info.info;
291 if (pointee->type == ARGTYPE_STRUCT) {
293 for (i = 0; i < len; ++i) {
295 = *VECT_ELEMENT(&pointers, struct value *, i);
296 assert(old->type->type == ARGTYPE_POINTER);
297 struct arg_type_info *old_pointee
298 = old->type->u.ptr_info.info;
299 if (old_pointee == pointee)
302 if (depth >= options.arraylen)
303 return fprintf(stream, "...");
306 for (i = len; i-- > 0 ;) {
307 struct value **old = VECT_ELEMENT(&pointers, struct value *, i);
308 int rc = value_equal(value, *old, arguments);
312 size_t reclevel = len - i - 1;
313 char buf[reclevel + 1];
314 memset(buf, '^', sizeof buf);
316 return fprintf(stream, "recurse%s", buf);
320 /* OK, not a recursion. Remember this value for tracking. */
321 if (VECT_PUSHBACK(&pointers, &value) < 0)
324 struct value element;
326 if (value_init_deref(&element, value) < 0) {
330 o = format_argument(stream, &element, arguments);
331 value_destroy(&element);
334 VECT_POPBACK(&pointers, struct value *, NULL, NULL);
339 * LENGTH is an expression whose evaluation will yield the actual
340 * length of the array.
342 * MAXLEN is the actual maximum length that we care about
344 * BEFORE if LENGTH>MAXLEN, we display ellipsis. We display it before
345 * the closing parenthesis if BEFORE, otherwise after it.
347 * OPEN, CLOSE, DELIM are opening and closing parenthesis and element
351 format_array(FILE *stream, struct value *value, struct value_dict *arguments,
352 struct expr_node *length, size_t maxlen, int before,
353 const char *open, const char *close, const char *delim)
355 /* We need "long" to be long enough to cover the whole address
357 (void)sizeof(char[1 - 2*(sizeof(long) < sizeof(void *))]);
359 if (expr_eval_word(length, value, arguments, &l) < 0)
361 size_t len = (size_t)l;
364 if (acc_fprintf(&written, stream, "%s", open) < 0)
368 for (i = 0; i < len && i <= maxlen; ++i) {
370 if (before && acc_fprintf(&written, stream, "...") < 0)
375 if (i > 0 && acc_fprintf(&written, stream, "%s", delim) < 0)
378 struct value element;
379 if (value_init_element(&element, value, i) < 0)
381 int o = format_argument(stream, &element, arguments);
382 value_destroy(&element);
387 if (acc_fprintf(&written, stream, "%s", close) < 0)
389 if (i == maxlen && !before && acc_fprintf(&written, stream, "...") < 0)
396 toplevel_format_lens(struct lens *lens, FILE *stream,
397 struct value *value, struct value_dict *arguments,
398 enum int_fmt_t int_fmt)
400 switch (value->type->type) {
402 return fprintf(stream, "<void>");
407 return format_integer(stream, value, int_fmt, arguments);
412 if (int_fmt == INT_FMT_i || int_fmt == INT_FMT_default)
414 return format_integer(stream, value, int_fmt, arguments);
417 if (int_fmt == INT_FMT_default)
418 return format_naked(stream, value, arguments,
420 return format_integer(stream, value, int_fmt, arguments);
424 return format_floating(stream, value, arguments, int_fmt);
427 return format_struct(stream, value, arguments);
429 case ARGTYPE_POINTER:
430 if (value_is_zero(value, arguments))
431 return fprintf(stream, null_message);
432 if (value->type->u.array_info.elt_type->type != ARGTYPE_VOID)
433 return format_pointer(stream, value, arguments);
434 return format_integer(stream, value, INT_FMT_x, arguments);
437 return format_array(stream, value, arguments,
438 value->type->u.array_info.length,
439 options.arraylen, 1, "[ ", " ]", ", ");
445 default_lens_format_cb(struct lens *lens, FILE *stream,
446 struct value *value, struct value_dict *arguments)
448 return toplevel_format_lens(lens, stream, value, arguments,
452 struct lens default_lens = {
453 .format_cb = default_lens_format_cb,
458 blind_lens_format_cb(struct lens *lens, FILE *stream,
459 struct value *value, struct value_dict *arguments)
464 struct lens blind_lens = {
465 .format_cb = blind_lens_format_cb,
470 octal_lens_format_cb(struct lens *lens, FILE *stream,
471 struct value *value, struct value_dict *arguments)
473 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_o);
476 struct lens octal_lens = {
477 .format_cb = octal_lens_format_cb,
482 hex_lens_format_cb(struct lens *lens, FILE *stream,
483 struct value *value, struct value_dict *arguments)
485 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_x);
488 struct lens hex_lens = {
489 .format_cb = hex_lens_format_cb,
494 dec_lens_format_cb(struct lens *lens, FILE *stream,
495 struct value *value, struct value_dict *arguments)
497 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_u);
500 struct lens dec_lens = {
501 .format_cb = dec_lens_format_cb,
506 guess_lens_format_cb(struct lens *lens, FILE *stream,
507 struct value *value, struct value_dict *arguments)
509 return toplevel_format_lens(lens, stream, value, arguments,
513 struct lens guess_lens = {
514 .format_cb = guess_lens_format_cb,
519 bool_lens_format_cb(struct lens *lens, FILE *stream,
520 struct value *value, struct value_dict *arguments)
522 switch (value->type->type) {
527 case ARGTYPE_POINTER:
529 return toplevel_format_lens(lens, stream, value,
530 arguments, INT_FMT_default);
540 if ((zero = value_is_zero(value, arguments)) < 0)
543 return fprintf(stream, "false");
545 return fprintf(stream, "true");
550 struct lens bool_lens = {
551 .format_cb = bool_lens_format_cb,
555 redispatch_as_array(struct lens *lens, FILE *stream,
556 struct value *value, struct value_dict *arguments,
557 int (*cb)(struct lens *, FILE *,
558 struct value *, struct value_dict *))
560 struct arg_type_info info[2];
561 type_init_array(&info[1], value->type->u.ptr_info.info, 0,
562 expr_node_zero(), 0);
563 type_init_pointer(&info[0], &info[1], 0);
567 if (value_clone(&tmp, value) < 0)
569 value_set_type(&tmp, info, 0);
570 int ret = cb(lens, stream, &tmp, arguments);
571 type_destroy(&info[0]);
572 type_destroy(&info[1]);
578 format_wchar(FILE *stream, struct value *value, struct value_dict *arguments)
581 if (value_extract_word(value, &l, arguments) < 0)
583 wchar_t wc = (wchar_t) l;
584 char buf[MB_CUR_MAX + 1];
586 int c = wctomb(buf, wc);
590 return print_char(stream, buf[0]);
593 if (fprintf(stream, "%s", buf) < 0)
597 return c >= 0 ? c : 0;
601 string_lens_format_cb(struct lens *lens, FILE *stream,
602 struct value *value, struct value_dict *arguments)
604 switch (value->type->type) {
605 case ARGTYPE_POINTER:
606 /* This should really be written as either "string",
607 * or, if lens, then string(array(char, zero)*). But
608 * I suspect people are so used to the char * C idiom,
609 * that string(char *) might actually turn up. So
610 * let's just support it. */
611 switch ((int) value->type->u.ptr_info.info->type)
619 return redispatch_as_array(lens, stream, value,
621 &string_lens_format_cb);
623 /* Otherwise dispatch to whatever the default for the
624 * pointee is--most likely this will again be us. */
630 return toplevel_format_lens(lens, stream, value,
631 arguments, INT_FMT_default);
639 if (value->parent != NULL && value->type->lens == NULL)
640 return format_wchar(stream, value, arguments);
642 return format_naked(stream, value, arguments,
646 return format_char(stream, value, arguments);
649 return format_array(stream, value, arguments,
650 value->type->u.array_info.length,
651 options.strlen, 0, "\"", "\"", "");
656 struct lens string_lens = {
657 .format_cb = string_lens_format_cb,
661 out_bits(FILE *stream, size_t low, size_t high)
664 return fprintf(stream, "%zd", low);
666 return fprintf(stream, "%zd-%zd", low, high);
670 bitvect_lens_format_cb(struct lens *lens, FILE *stream,
671 struct value *value, struct value_dict *arguments)
673 unsigned char *data = value_get_data(value, arguments);
676 size_t sz = type_sizeof(value->inferior, value->type);
677 if (sz == (size_t)-1)
681 unsigned char buf[sz];
682 switch ((int)value->type->type) {
683 union bitvect_integral_64
689 unsigned char buf[0];
692 case ARGTYPE_POINTER:
693 return format_pointer(stream, value, arguments);
700 assert(sz <= sizeof(bv));
701 memmove(bv.buf, data, sz);
710 for (i = 0; i < sz; ++i) {
711 buf[i] = bv.u64 & 0xff;
718 for (i = 0; i < sz; ++i)
719 bits += bitcount(data[i]);
721 /* If there's more 1's than 0's, show inverse. */
722 unsigned neg = bits > sz * 4 ? 0xff : 0x00;
725 if (acc_fprintf(&o, stream, "%s<", &"~"[neg == 0x00]) < 0)
730 for (i = 0; i < sz; ++i) {
732 unsigned char d = data[i] ^ neg;
733 for (m = 0x01; m != 0; m <<= 1) {
738 && acc_fprintf(&o, stream, ",") < 0)
743 if (account_output(&o, out_bits(stream, low,
751 if (low >= 0 && account_output(&o, out_bits(stream, low, bitno-1)) < 0)
754 if (fputc('>', stream) < 0)
761 struct lens bitvect_lens = {
762 .format_cb = bitvect_lens_format_cb,