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 format_char(FILE *stream, struct value *value, struct value_dict *arguments)
129 if (value_extract_word(value, &lc, arguments) < 0)
166 if (isprint(c) || c == ' ')
172 return fprintf(stream, fmt, c);
176 format_naked_char(FILE *stream, struct value *value,
177 struct value_dict *arguments)
180 if (acc_fprintf(&written, stream, "'") < 0
181 || account_output(&written,
182 format_char(stream, value, arguments)) < 0
183 || acc_fprintf(&written, stream, "'") < 0)
190 format_double(FILE *stream, double value, enum int_fmt_t format)
192 if (format == INT_FMT_x)
193 return fprintf(stream, "%a", value);
195 return fprintf(stream, "%f", value);
199 format_floating(FILE *stream, struct value *value, struct value_dict *arguments,
200 enum int_fmt_t format)
202 switch (value->type->type) {
206 if (read_float(value, &f, arguments) < 0)
208 return format_double(stream, f, format);
210 if (read_double(value, &d, arguments) < 0)
212 return format_double(stream, d, format);
218 struct format_argument_data
221 struct value_dict *arguments;
225 format_argument_cb(FILE *stream, void *ptr)
227 struct format_argument_data *data = ptr;
228 return format_argument(stream, data->value, data->arguments);
232 format_struct(FILE *stream, struct value *value, struct value_dict *arguments)
235 if (acc_fprintf(&written, stream, "{ ") < 0)
240 for (i = 0; i < type_struct_size(value->type); ++i) {
241 struct value element;
242 if (value_init_element(&element, value, i) < 0)
245 struct format_argument_data data = { &element, arguments };
246 int o = delim_output(stream, &need_delim,
247 format_argument_cb, &data);
248 value_destroy(&element);
254 if (acc_fprintf(&written, stream, " }") < 0)
259 static const char null_message[] = "nil";
261 format_pointer(FILE *stream, struct value *value, struct value_dict *arguments)
263 if (value_is_zero(value, arguments))
264 return fprintf(stream, null_message);
266 /* The following is for detecting recursion. We keep track of
267 * the values that were already displayed. Each time a
268 * pointer should be dereferenced, we compare its value to the
269 * value of each of the pointers dereferenced so far. If one
270 * of them matches, instead of recursing, we just printf which
271 * superstructure this pointer recurses to. */
272 static struct vect pointers = {};
273 if (pointers.elt_size == 0)
274 VECT_INIT(&pointers, struct value *);
276 /* Trim number of expanded structures of the same type. Even
277 * for non-recursive structure, we don't want to expand all of
278 * it if it's huge. */
280 size_t len = vect_size(&pointers);
281 assert(value->type->type == ARGTYPE_POINTER);
282 struct arg_type_info *pointee = value->type->u.ptr_info.info;
283 if (pointee->type == ARGTYPE_STRUCT) {
285 for (i = 0; i < len; ++i) {
287 = *VECT_ELEMENT(&pointers, struct value *, i);
288 assert(old->type->type == ARGTYPE_POINTER);
289 struct arg_type_info *old_pointee
290 = old->type->u.ptr_info.info;
291 if (old_pointee == pointee)
294 if (depth >= options.arraylen)
295 return fprintf(stream, "...");
298 for (i = len; i-- > 0 ;) {
299 struct value **old = VECT_ELEMENT(&pointers, struct value *, i);
300 int rc = value_equal(value, *old, arguments);
304 size_t reclevel = len - i - 1;
305 char buf[reclevel + 1];
306 memset(buf, '^', sizeof buf);
308 return fprintf(stream, "recurse%s", buf);
312 /* OK, not a recursion. Remember this value for tracking. */
313 if (VECT_PUSHBACK(&pointers, &value) < 0)
316 struct value element;
318 if (value_init_deref(&element, value) < 0) {
322 o = format_argument(stream, &element, arguments);
323 value_destroy(&element);
326 VECT_POPBACK(&pointers, struct value *, NULL, NULL);
331 * LENGTH is an expression whose evaluation will yield the actual
332 * length of the array.
334 * MAXLEN is the actual maximum length that we care about
336 * BEFORE if LENGTH>MAXLEN, we display ellipsis. We display it before
337 * the closing parenthesis if BEFORE, otherwise after it.
339 * OPEN, CLOSE, DELIM are opening and closing parenthesis and element
343 format_array(FILE *stream, struct value *value, struct value_dict *arguments,
344 struct expr_node *length, size_t maxlen, int before,
345 const char *open, const char *close, const char *delim)
347 /* We need "long" to be long enough to cover the whole address
349 (void)sizeof(char[1 - 2*(sizeof(long) < sizeof(void *))]);
351 if (expr_eval_word(length, value, arguments, &l) < 0)
353 size_t len = (size_t)l;
356 if (acc_fprintf(&written, stream, "%s", open) < 0)
360 for (i = 0; i < len && i <= maxlen; ++i) {
362 if (before && acc_fprintf(&written, stream, "...") < 0)
367 if (i > 0 && acc_fprintf(&written, stream, "%s", delim) < 0)
370 struct value element;
371 if (value_init_element(&element, value, i) < 0)
373 int o = format_argument(stream, &element, arguments);
374 value_destroy(&element);
379 if (acc_fprintf(&written, stream, "%s", close) < 0)
381 if (i == maxlen && !before && acc_fprintf(&written, stream, "...") < 0)
388 toplevel_format_lens(struct lens *lens, FILE *stream,
389 struct value *value, struct value_dict *arguments,
390 enum int_fmt_t int_fmt)
392 switch (value->type->type) {
394 return fprintf(stream, "<void>");
399 return format_integer(stream, value, int_fmt, arguments);
404 if (int_fmt == INT_FMT_i || int_fmt == INT_FMT_default)
406 return format_integer(stream, value, int_fmt, arguments);
409 if (int_fmt == INT_FMT_default)
410 return format_naked_char(stream, value, arguments);
411 return format_integer(stream, value, int_fmt, arguments);
415 return format_floating(stream, value, arguments, int_fmt);
418 return format_struct(stream, value, arguments);
420 case ARGTYPE_POINTER:
421 if (value_is_zero(value, arguments))
422 return fprintf(stream, null_message);
423 if (value->type->u.array_info.elt_type->type != ARGTYPE_VOID)
424 return format_pointer(stream, value, arguments);
425 return format_integer(stream, value, INT_FMT_x, arguments);
428 return format_array(stream, value, arguments,
429 value->type->u.array_info.length,
430 options.arraylen, 1, "[ ", " ]", ", ");
436 default_lens_format_cb(struct lens *lens, FILE *stream,
437 struct value *value, struct value_dict *arguments)
439 return toplevel_format_lens(lens, stream, value, arguments,
443 struct lens default_lens = {
444 .format_cb = default_lens_format_cb,
449 blind_lens_format_cb(struct lens *lens, FILE *stream,
450 struct value *value, struct value_dict *arguments)
455 struct lens blind_lens = {
456 .format_cb = blind_lens_format_cb,
461 octal_lens_format_cb(struct lens *lens, FILE *stream,
462 struct value *value, struct value_dict *arguments)
464 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_o);
467 struct lens octal_lens = {
468 .format_cb = octal_lens_format_cb,
473 hex_lens_format_cb(struct lens *lens, FILE *stream,
474 struct value *value, struct value_dict *arguments)
476 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_x);
479 struct lens hex_lens = {
480 .format_cb = hex_lens_format_cb,
485 dec_lens_format_cb(struct lens *lens, FILE *stream,
486 struct value *value, struct value_dict *arguments)
488 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_u);
491 struct lens dec_lens = {
492 .format_cb = dec_lens_format_cb,
497 guess_lens_format_cb(struct lens *lens, FILE *stream,
498 struct value *value, struct value_dict *arguments)
500 return toplevel_format_lens(lens, stream, value, arguments,
504 struct lens guess_lens = {
505 .format_cb = guess_lens_format_cb,
510 bool_lens_format_cb(struct lens *lens, FILE *stream,
511 struct value *value, struct value_dict *arguments)
513 switch (value->type->type) {
518 case ARGTYPE_POINTER:
520 return toplevel_format_lens(lens, stream, value,
521 arguments, INT_FMT_default);
531 if ((zero = value_is_zero(value, arguments)) < 0)
534 return fprintf(stream, "false");
536 return fprintf(stream, "true");
541 struct lens bool_lens = {
542 .format_cb = bool_lens_format_cb,
547 string_lens_format_cb(struct lens *lens, FILE *stream,
548 struct value *value, struct value_dict *arguments)
550 switch (value->type->type) {
551 case ARGTYPE_POINTER:
552 /* This should really be written as either "string",
553 * or, if lens, then string(array(char, zero)*). But
554 * I suspect people are so used to the char * C idiom,
555 * that string(char *) might actually turn up. So
556 * let's just support it. */
557 if (value->type->u.ptr_info.info->type == ARGTYPE_CHAR) {
558 struct arg_type_info info[2];
559 type_init_array(&info[1],
560 value->type->u.ptr_info.info, 0,
561 expr_node_zero(), 0);
562 type_init_pointer(&info[0], &info[1], 0);
566 if (value_clone(&tmp, value) < 0)
568 value_set_type(&tmp, info, 0);
569 int ret = string_lens_format_cb(lens, stream, &tmp,
571 type_destroy(&info[0]);
572 type_destroy(&info[1]);
588 return toplevel_format_lens(lens, stream, value,
589 arguments, INT_FMT_default);
592 return format_char(stream, value, arguments);
595 return format_array(stream, value, arguments,
596 value->type->u.array_info.length,
597 options.strlen, 0, "\"", "\"", "");
602 struct lens string_lens = {
603 .format_cb = string_lens_format_cb,
607 out_bits(FILE *stream, size_t low, size_t high)
610 return fprintf(stream, "%zd", low);
612 return fprintf(stream, "%zd-%zd", low, high);
616 bitvect_lens_format_cb(struct lens *lens, FILE *stream,
617 struct value *value, struct value_dict *arguments)
619 unsigned char *data = value_get_data(value, arguments);
622 size_t sz = type_sizeof(value->inferior, value->type);
623 if (sz == (size_t)-1)
627 unsigned char buf[sz];
628 switch ((int)value->type->type) {
629 union bitvect_integral_64
635 unsigned char buf[0];
638 case ARGTYPE_POINTER:
639 return format_pointer(stream, value, arguments);
646 assert(sz <= sizeof(bv));
647 memmove(bv.buf, data, sz);
656 for (i = 0; i < sz; ++i) {
657 buf[i] = bv.u64 & 0xff;
664 for (i = 0; i < sz; ++i)
665 bits += bitcount(data[i]);
667 /* If there's more 1's than 0's, show inverse. */
668 unsigned neg = bits > sz * 4 ? 0xff : 0x00;
671 if (acc_fprintf(&o, stream, "%s<", &"~"[neg == 0x00]) < 0)
676 for (i = 0; i < sz; ++i) {
678 unsigned char d = data[i] ^ neg;
679 for (m = 0x01; m != 0; m <<= 1) {
684 && acc_fprintf(&o, stream, ",") < 0)
689 if (account_output(&o, out_bits(stream, low,
697 if (low >= 0 && account_output(&o, out_bits(stream, low, bitno-1)) < 0)
700 if (fputc('>', stream) < 0)
707 struct lens bitvect_lens = {
708 .format_cb = bitvect_lens_format_cb,