blob: f3e328f78be40ea2ad7f84fd54a625d0e9cfec8e [file] [log] [blame]
Petr Machata94078ec2012-01-05 18:07:02 +01001/*
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
7 *
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.
12 *
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.
17 *
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
21 * 02110-1301 USA
22 */
23
Steve Fink7bafff02006-08-07 04:50:42 +020024#include <ctype.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010025#include <stdlib.h>
Petr Machata94078ec2012-01-05 18:07:02 +010026#include <assert.h>
27#include <inttypes.h>
28#include <stdarg.h>
29#include <stdio.h>
Petr Machata307b90b2012-11-19 00:35:38 +010030#include <string.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010031
Petr Machata366c2f42012-02-09 19:34:36 +010032#include "proc.h"
Petr Machata31af32c2012-01-08 02:36:50 +010033#include "lens_default.h"
Petr Machata94078ec2012-01-05 18:07:02 +010034#include "value.h"
35#include "expr.h"
Petr Machata000e3112012-01-03 17:03:39 +010036#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010037#include "common.h"
Petr Machatad7b22922012-01-06 18:34:22 +010038#include "zero.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010039
Petr Machata94078ec2012-01-05 18:07:02 +010040#define READER(NAME, TYPE) \
41 static int \
42 NAME(struct value *value, TYPE *ret, struct value_dict *arguments) \
43 { \
44 union { \
45 TYPE val; \
46 unsigned char buf[0]; \
47 } u; \
48 if (value_extract_buf(value, u.buf, arguments) < 0) \
49 return -1; \
50 *ret = u.val; \
51 return 0; \
Juan Cespedesa413e5b2007-09-04 17:34:53 +020052 }
Steve Fink65b53df2006-09-25 02:27:08 +020053
Petr Machata94078ec2012-01-05 18:07:02 +010054READER(read_float, float)
55READER(read_double, double)
56
57#undef READER
58
59#define HANDLE_WIDTH(BITS) \
60 do { \
61 long l; \
62 if (value_extract_word(value, &l, arguments) < 0) \
63 return -1; \
64 int##BITS##_t i = l; \
Petr Machatae773f632012-11-09 18:52:53 +010065 uint64_t v = (uint64_t)(uint##BITS##_t)i; \
Petr Machata94078ec2012-01-05 18:07:02 +010066 switch (format) { \
67 case INT_FMT_unknown: \
Petr Machatac1e41862012-08-31 15:13:16 +020068 if (l < -10000 || l > 10000) \
Petr Machata94078ec2012-01-05 18:07:02 +010069 case INT_FMT_x: \
Petr Machatae773f632012-11-09 18:52:53 +010070 return fprintf(stream, "%#"PRIx64, v); \
Petr Machata94078ec2012-01-05 18:07:02 +010071 case INT_FMT_i: \
Petr Machatae773f632012-11-09 18:52:53 +010072 case INT_FMT_default: \
Petr Machata94078ec2012-01-05 18:07:02 +010073 return fprintf(stream, "%"PRIi##BITS, i); \
74 case INT_FMT_u: \
Petr Machatae773f632012-11-09 18:52:53 +010075 return fprintf(stream, "%"PRIu64, v); \
Petr Machata94078ec2012-01-05 18:07:02 +010076 case INT_FMT_o: \
Petr Machatae773f632012-11-09 18:52:53 +010077 return fprintf(stream, "0%"PRIo64, v); \
Petr Machata94078ec2012-01-05 18:07:02 +010078 } \
79 } while (0)
80
81enum int_fmt_t
82{
83 INT_FMT_i,
84 INT_FMT_u,
85 INT_FMT_o,
86 INT_FMT_x,
87 INT_FMT_unknown,
Petr Machatae773f632012-11-09 18:52:53 +010088 INT_FMT_default,
Petr Machata94078ec2012-01-05 18:07:02 +010089};
90
91static int
92format_integer(FILE *stream, struct value *value, enum int_fmt_t format,
93 struct value_dict *arguments)
94{
95 switch (type_sizeof(value->inferior, value->type)) {
96
97 case 1: HANDLE_WIDTH(8);
98 case 2: HANDLE_WIDTH(16);
99 case 4: HANDLE_WIDTH(32);
100 case 8: HANDLE_WIDTH(64);
101
102 default:
103 assert(!"unsupported integer width");
104 abort();
105
106 case -1:
107 return -1;
108 }
109}
110
111#undef HANDLE_WIDTH
112
113static int
Petr Machata94078ec2012-01-05 18:07:02 +0100114acc_fprintf(int *countp, FILE *stream, const char *format, ...)
115{
116 va_list pa;
117 va_start(pa, format);
Petr Machataadec2012012-11-01 22:05:04 +0100118 int i = account_output(countp, vfprintf(stream, format, pa));
Petr Machata94078ec2012-01-05 18:07:02 +0100119 va_end(pa);
120
Petr Machata94078ec2012-01-05 18:07:02 +0100121 return i;
122}
123
124static int
125format_char(FILE *stream, struct value *value, struct value_dict *arguments)
126{
127 long lc;
128 if (value_extract_word(value, &lc, arguments) < 0)
129 return -1;
130 int c = (int)lc;
131
Petr Machata94078ec2012-01-05 18:07:02 +0100132 const char *fmt;
133 switch (c) {
134 case -1:
135 fmt = "EOF";
136 break;
137 case 0:
138 fmt = "\\0";
139 break;
140 case '\a':
141 fmt = "\\a";
142 break;
143 case '\b':
144 fmt = "\\b";
145 break;
146 case '\t':
147 fmt = "\\t";
148 break;
149 case '\n':
150 fmt = "\\n";
151 break;
152 case '\v':
153 fmt = "\\v";
154 break;
155 case '\f':
156 fmt = "\\f";
157 break;
158 case '\r':
159 fmt = "\\r";
160 break;
161 case '\\':
162 fmt = "\\\\";
163 break;
164 default:
165 if (isprint(c) || c == ' ')
166 fmt = "%c";
Petr Machata94078ec2012-01-05 18:07:02 +0100167 else
Petr Machata987d27b2012-01-17 19:04:04 +0100168 fmt = "\\%03o";
Petr Machata94078ec2012-01-05 18:07:02 +0100169 }
170
Petr Machata987d27b2012-01-17 19:04:04 +0100171 return fprintf(stream, fmt, c);
Petr Machatae3f4a982012-01-09 04:27:26 +0100172}
173
174static int
175format_naked_char(FILE *stream, struct value *value,
176 struct value_dict *arguments)
177{
178 int written = 0;
179 if (acc_fprintf(&written, stream, "'") < 0
Petr Machataadec2012012-11-01 22:05:04 +0100180 || account_output(&written,
181 format_char(stream, value, arguments)) < 0
Petr Machatae3f4a982012-01-09 04:27:26 +0100182 || acc_fprintf(&written, stream, "'") < 0)
Petr Machata94078ec2012-01-05 18:07:02 +0100183 return -1;
Petr Machatae3f4a982012-01-09 04:27:26 +0100184
Petr Machata94078ec2012-01-05 18:07:02 +0100185 return written;
186}
187
188static int
189format_floating(FILE *stream, struct value *value, struct value_dict *arguments)
190{
191 switch (value->type->type) {
192 float f;
193 double d;
194 case ARGTYPE_FLOAT:
195 if (read_float(value, &f, arguments) < 0)
196 return -1;
197 return fprintf(stream, "%f", (double)f);
198 case ARGTYPE_DOUBLE:
199 if (read_double(value, &d, arguments) < 0)
200 return -1;
201 return fprintf(stream, "%f", d);
202 default:
203 abort();
204 }
205}
206
Petr Machataf7c46bb2012-11-01 22:15:45 +0100207struct format_argument_data
208{
209 struct value *value;
210 struct value_dict *arguments;
211};
212
213static int
214format_argument_cb(FILE *stream, void *ptr)
215{
216 struct format_argument_data *data = ptr;
217 return format_argument(stream, data->value, data->arguments);
218}
219
Petr Machata94078ec2012-01-05 18:07:02 +0100220static int
221format_struct(FILE *stream, struct value *value, struct value_dict *arguments)
222{
223 int written = 0;
224 if (acc_fprintf(&written, stream, "{ ") < 0)
225 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100226
227 int need_delim = 0;
Petr Machata94078ec2012-01-05 18:07:02 +0100228 size_t i;
229 for (i = 0; i < type_struct_size(value->type); ++i) {
Petr Machata94078ec2012-01-05 18:07:02 +0100230 struct value element;
231 if (value_init_element(&element, value, i) < 0)
232 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100233
234 struct format_argument_data data = { &element, arguments };
235 int o = delim_output(stream, &need_delim,
236 format_argument_cb, &data);
Petr Machata8904cdc2012-10-30 17:27:20 +0100237 value_destroy(&element);
Petr Machata94078ec2012-01-05 18:07:02 +0100238 if (o < 0)
239 return -1;
Petr Machataf7c46bb2012-11-01 22:15:45 +0100240
Petr Machata94078ec2012-01-05 18:07:02 +0100241 written += o;
242 }
243 if (acc_fprintf(&written, stream, " }") < 0)
244 return -1;
245 return written;
246}
247
248int
249format_pointer(FILE *stream, struct value *value, struct value_dict *arguments)
250{
Petr Machata26c0c942012-11-19 00:14:51 +0100251 if (value_is_zero(value, arguments))
252 return fprintf(stream, "nil");
253
Petr Machata307b90b2012-11-19 00:35:38 +0100254 /* The following is for detecting recursion. We keep track of
255 * the values that were already displayed. Each time a
256 * pointer should be dereferenced, we compare its value to the
257 * value of each of the pointers dereferenced so far. If one
258 * of them matches, instead of recursing, we just printf which
259 * superstructure this pointer recurses to. */
260 static struct vect pointers = {};
261 if (pointers.elt_size == 0)
262 VECT_INIT(&pointers, struct value *);
263
264 size_t len = vect_size(&pointers);
265 size_t i;
266 for (i = len; i-- > 0 ;) {
267 struct value **old = VECT_ELEMENT(&pointers, struct value *, i);
268 int rc = value_equal(value, *old, arguments);
269 if (rc < 0)
270 return -1;
271 if (rc > 0) {
272 size_t reclevel = len - i - 1;
273 char buf[reclevel + 1];
274 memset(buf, '^', sizeof buf);
275 buf[reclevel] = 0;
276 return fprintf(stream, "recurse%s", buf);
277 }
278 }
279
280 /* OK, not a recursion. Remember this value for tracking. */
281 if (VECT_PUSHBACK(&pointers, &value) < 0)
282 return -1;
283
Petr Machata94078ec2012-01-05 18:07:02 +0100284 struct value element;
Petr Machata26c0c942012-11-19 00:14:51 +0100285 int o;
286 if (value_init_deref(&element, value) < 0) {
287 o = -1;
288 goto done;
289 }
290 o = format_argument(stream, &element, arguments);
Petr Machata8904cdc2012-10-30 17:27:20 +0100291 value_destroy(&element);
Petr Machata26c0c942012-11-19 00:14:51 +0100292
293done:
Petr Machata307b90b2012-11-19 00:35:38 +0100294 vect_popback(&pointers);
Petr Machata8904cdc2012-10-30 17:27:20 +0100295 return o;
Steve Fink1150bc42006-08-07 06:04:43 +0200296}
297
298/*
Petr Machata94078ec2012-01-05 18:07:02 +0100299 * LENGTH is an expression whose evaluation will yield the actual
300 * length of the array.
Steve Fink1150bc42006-08-07 06:04:43 +0200301 *
Petr Machata94078ec2012-01-05 18:07:02 +0100302 * MAXLEN is the actual maximum length that we care about
303 *
304 * BEFORE if LENGTH>MAXLEN, we display ellipsis. We display it before
305 * the closing parenthesis if BEFORE, otherwise after it.
306 *
307 * OPEN, CLOSE, DELIM are opening and closing parenthesis and element
308 * delimiter.
Steve Fink1150bc42006-08-07 06:04:43 +0200309 */
Juan Cespedesf1350522008-12-16 18:19:58 +0100310int
Petr Machata94078ec2012-01-05 18:07:02 +0100311format_array(FILE *stream, struct value *value, struct value_dict *arguments,
312 struct expr_node *length, size_t maxlen, int before,
313 const char *open, const char *close, const char *delim)
Petr Machata000e3112012-01-03 17:03:39 +0100314{
Petr Machata94078ec2012-01-05 18:07:02 +0100315 /* We need "long" to be long enough to cover the whole address
316 * space. */
317 typedef char assert__long_enough_long[-(sizeof(long) < sizeof(void *))];
Petr Machata31af32c2012-01-08 02:36:50 +0100318 long l;
Petr Machatad7b22922012-01-06 18:34:22 +0100319 if (expr_eval_word(length, value, arguments, &l) < 0)
320 return -1;
Petr Machata94078ec2012-01-05 18:07:02 +0100321 size_t len = (size_t)l;
Steve Fink7bafff02006-08-07 04:50:42 +0200322
Petr Machata94078ec2012-01-05 18:07:02 +0100323 int written = 0;
324 if (acc_fprintf(&written, stream, "%s", open) < 0)
325 return -1;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100326
Zachary T Welchba6aca22010-12-08 18:55:09 -0800327 size_t i;
Petr Machata94078ec2012-01-05 18:07:02 +0100328 for (i = 0; i < len && i <= maxlen; ++i) {
329 if (i == maxlen) {
330 if (before && acc_fprintf(&written, stream, "...") < 0)
331 return -1;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100332 break;
333 }
Petr Machata94078ec2012-01-05 18:07:02 +0100334
335 if (i > 0 && acc_fprintf(&written, stream, "%s", delim) < 0)
336 return -1;
337
338 struct value element;
339 if (value_init_element(&element, value, i) < 0)
340 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100341 int o = format_argument(stream, &element, arguments);
Petr Machata8904cdc2012-10-30 17:27:20 +0100342 value_destroy(&element);
Petr Machata94078ec2012-01-05 18:07:02 +0100343 if (o < 0)
344 return -1;
345 written += o;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100346 }
Petr Machata94078ec2012-01-05 18:07:02 +0100347 if (acc_fprintf(&written, stream, "%s", close) < 0)
348 return -1;
349 if (i == maxlen && !before && acc_fprintf(&written, stream, "...") < 0)
350 return -1;
351
352 return written;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100353}
354
Petr Machata31af32c2012-01-08 02:36:50 +0100355static int
356toplevel_format_lens(struct lens *lens, FILE *stream,
Petr Machatace034982012-01-09 04:25:31 +0100357 struct value *value, struct value_dict *arguments,
358 enum int_fmt_t int_fmt)
Petr Machata94078ec2012-01-05 18:07:02 +0100359{
Petr Machata94078ec2012-01-05 18:07:02 +0100360 switch (value->type->type) {
361 case ARGTYPE_VOID:
362 return fprintf(stream, "<void>");
363
Petr Machata94078ec2012-01-05 18:07:02 +0100364 case ARGTYPE_SHORT:
365 case ARGTYPE_INT:
366 case ARGTYPE_LONG:
Petr Machatace034982012-01-09 04:25:31 +0100367 return format_integer(stream, value, int_fmt, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100368
369 case ARGTYPE_USHORT:
370 case ARGTYPE_UINT:
371 case ARGTYPE_ULONG:
Petr Machatae773f632012-11-09 18:52:53 +0100372 if (int_fmt == INT_FMT_i || int_fmt == INT_FMT_default)
Petr Machatace034982012-01-09 04:25:31 +0100373 int_fmt = INT_FMT_u;
374 return format_integer(stream, value, int_fmt, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100375
376 case ARGTYPE_CHAR:
Petr Machatae773f632012-11-09 18:52:53 +0100377 if (int_fmt == INT_FMT_default)
378 return format_naked_char(stream, value, arguments);
379 return format_integer(stream, value, int_fmt, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100380
381 case ARGTYPE_FLOAT:
382 case ARGTYPE_DOUBLE:
383 return format_floating(stream, value, arguments);
384
385 case ARGTYPE_STRUCT:
386 return format_struct(stream, value, arguments);
387
388 case ARGTYPE_POINTER:
389 if (value->type->u.array_info.elt_type->type != ARGTYPE_VOID)
390 return format_pointer(stream, value, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100391 return format_integer(stream, value, INT_FMT_x, arguments);
392
393 case ARGTYPE_ARRAY:
Petr Machata94078ec2012-01-05 18:07:02 +0100394 return format_array(stream, value, arguments,
395 value->type->u.array_info.length,
Petr Machatae3f4a982012-01-09 04:27:26 +0100396 options.arraylen, 1, "[ ", " ]", ", ");
Petr Machata94078ec2012-01-05 18:07:02 +0100397 }
398 abort();
399}
Petr Machata31af32c2012-01-08 02:36:50 +0100400
401static int
402default_lens_format_cb(struct lens *lens, FILE *stream,
403 struct value *value, struct value_dict *arguments)
404{
Petr Machatae773f632012-11-09 18:52:53 +0100405 return toplevel_format_lens(lens, stream, value, arguments,
406 INT_FMT_default);
Petr Machata31af32c2012-01-08 02:36:50 +0100407}
408
409struct lens default_lens = {
410 .format_cb = default_lens_format_cb,
411};
Petr Machatace034982012-01-09 04:25:31 +0100412
413
414static int
415blind_lens_format_cb(struct lens *lens, FILE *stream,
416 struct value *value, struct value_dict *arguments)
417{
418 return 0;
419}
420
421struct lens blind_lens = {
422 .format_cb = blind_lens_format_cb,
423};
424
425
426static int
427octal_lens_format_cb(struct lens *lens, FILE *stream,
428 struct value *value, struct value_dict *arguments)
429{
430 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_o);
431}
432
433struct lens octal_lens = {
434 .format_cb = octal_lens_format_cb,
435};
436
437
438static int
439hex_lens_format_cb(struct lens *lens, FILE *stream,
440 struct value *value, struct value_dict *arguments)
441{
442 return toplevel_format_lens(lens, stream, value, arguments, INT_FMT_x);
443}
444
445struct lens hex_lens = {
446 .format_cb = hex_lens_format_cb,
447};
Petr Machatab7819162012-01-09 04:26:15 +0100448
449
450static int
451guess_lens_format_cb(struct lens *lens, FILE *stream,
452 struct value *value, struct value_dict *arguments)
453{
454 return toplevel_format_lens(lens, stream, value, arguments,
455 INT_FMT_unknown);
456}
457
458struct lens guess_lens = {
459 .format_cb = guess_lens_format_cb,
460};
Petr Machatae3f4a982012-01-09 04:27:26 +0100461
462
463static int
Petr Machata38fb49b2012-01-09 04:28:46 +0100464bool_lens_format_cb(struct lens *lens, FILE *stream,
465 struct value *value, struct value_dict *arguments)
466{
467 switch (value->type->type) {
468 case ARGTYPE_VOID:
469 case ARGTYPE_FLOAT:
470 case ARGTYPE_DOUBLE:
471 case ARGTYPE_STRUCT:
472 case ARGTYPE_POINTER:
473 case ARGTYPE_ARRAY:
474 return toplevel_format_lens(lens, stream, value,
Petr Machatae773f632012-11-09 18:52:53 +0100475 arguments, INT_FMT_default);
Petr Machata38fb49b2012-01-09 04:28:46 +0100476
477 int zero;
Petr Machata38fb49b2012-01-09 04:28:46 +0100478 case ARGTYPE_SHORT:
479 case ARGTYPE_INT:
480 case ARGTYPE_LONG:
481 case ARGTYPE_USHORT:
482 case ARGTYPE_UINT:
483 case ARGTYPE_ULONG:
484 case ARGTYPE_CHAR:
485 if ((zero = value_is_zero(value, arguments)) < 0)
486 return -1;
487 if (zero)
488 return fprintf(stream, "false");
489 else
490 return fprintf(stream, "true");
491 }
492 abort();
493}
494
495struct lens bool_lens = {
496 .format_cb = bool_lens_format_cb,
497};
498
499
500static int
Petr Machatae3f4a982012-01-09 04:27:26 +0100501string_lens_format_cb(struct lens *lens, FILE *stream,
502 struct value *value, struct value_dict *arguments)
503{
504 switch (value->type->type) {
505 case ARGTYPE_POINTER:
506 /* This should really be written as either "string",
507 * or, if lens, then string(array(char, zero)*). But
508 * I suspect people are so used to the char * C idiom,
509 * that string(char *) might actually turn up. So
510 * let's just support it. */
511 if (value->type->u.ptr_info.info->type == ARGTYPE_CHAR) {
512 struct arg_type_info info[2];
513 type_init_array(&info[1],
514 value->type->u.ptr_info.info, 0,
515 expr_node_zero(), 0);
516 type_init_pointer(&info[0], &info[1], 0);
517 info->lens = lens;
518 info->own_lens = 0;
519 struct value tmp;
520 if (value_clone(&tmp, value) < 0)
521 return -1;
522 value_set_type(&tmp, info, 0);
523 int ret = string_lens_format_cb(lens, stream, &tmp,
524 arguments);
525 type_destroy(&info[0]);
526 type_destroy(&info[1]);
527 value_destroy(&tmp);
528 return ret;
529 }
530
531 /* fall-through */
532 case ARGTYPE_VOID:
533 case ARGTYPE_FLOAT:
534 case ARGTYPE_DOUBLE:
535 case ARGTYPE_STRUCT:
536 case ARGTYPE_SHORT:
537 case ARGTYPE_INT:
538 case ARGTYPE_LONG:
539 case ARGTYPE_USHORT:
540 case ARGTYPE_UINT:
541 case ARGTYPE_ULONG:
Petr Machatae3f4a982012-01-09 04:27:26 +0100542 return toplevel_format_lens(lens, stream, value,
Petr Machatae773f632012-11-09 18:52:53 +0100543 arguments, INT_FMT_default);
Petr Machatae3f4a982012-01-09 04:27:26 +0100544
545 case ARGTYPE_CHAR:
546 return format_char(stream, value, arguments);
547
548 case ARGTYPE_ARRAY:
549 return format_array(stream, value, arguments,
550 value->type->u.array_info.length,
551 options.strlen, 0, "\"", "\"", "");
552 }
553 abort();
554}
555
556struct lens string_lens = {
557 .format_cb = string_lens_format_cb,
558};