blob: 00f74cb56e1ddf785182ef28b9a90eee19264d6f [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>
Juan Cespedes5e01f651998-03-08 22:31:44 +010030
Juan Cespedesf7281232009-06-25 16:11:21 +020031#include "common.h"
Petr Machata366c2f42012-02-09 19:34:36 +010032#include "proc.h"
Petr Machata94078ec2012-01-05 18:07:02 +010033#include "value.h"
34#include "expr.h"
Petr Machata000e3112012-01-03 17:03:39 +010035#include "type.h"
Petr Machata94078ec2012-01-05 18:07:02 +010036#include "common.h"
Petr Machatad7b22922012-01-06 18:34:22 +010037#include "zero.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010038
Petr Machata94078ec2012-01-05 18:07:02 +010039#define READER(NAME, TYPE) \
40 static int \
41 NAME(struct value *value, TYPE *ret, struct value_dict *arguments) \
42 { \
43 union { \
44 TYPE val; \
45 unsigned char buf[0]; \
46 } u; \
47 if (value_extract_buf(value, u.buf, arguments) < 0) \
48 return -1; \
49 *ret = u.val; \
50 return 0; \
Juan Cespedesa413e5b2007-09-04 17:34:53 +020051 }
Steve Fink65b53df2006-09-25 02:27:08 +020052
Petr Machata94078ec2012-01-05 18:07:02 +010053READER(read_float, float)
54READER(read_double, double)
55
56#undef READER
57
58#define HANDLE_WIDTH(BITS) \
59 do { \
60 long l; \
61 if (value_extract_word(value, &l, arguments) < 0) \
62 return -1; \
63 int##BITS##_t i = l; \
64 switch (format) { \
65 case INT_FMT_unknown: \
66 if (i < -10000 || i > 10000) \
67 case INT_FMT_x: \
68 return fprintf(stream, "%#"PRIx##BITS, i); \
69 case INT_FMT_i: \
70 return fprintf(stream, "%"PRIi##BITS, i); \
71 case INT_FMT_u: \
72 return fprintf(stream, "%"PRIu##BITS, i); \
73 case INT_FMT_o: \
74 return fprintf(stream, "%"PRIo##BITS, i); \
75 } \
76 } while (0)
77
78enum int_fmt_t
79{
80 INT_FMT_i,
81 INT_FMT_u,
82 INT_FMT_o,
83 INT_FMT_x,
84 INT_FMT_unknown,
85};
86
87static int
88format_integer(FILE *stream, struct value *value, enum int_fmt_t format,
89 struct value_dict *arguments)
90{
91 switch (type_sizeof(value->inferior, value->type)) {
92
93 case 1: HANDLE_WIDTH(8);
94 case 2: HANDLE_WIDTH(16);
95 case 4: HANDLE_WIDTH(32);
96 case 8: HANDLE_WIDTH(64);
97
98 default:
99 assert(!"unsupported integer width");
100 abort();
101
102 case -1:
103 return -1;
104 }
105}
106
107#undef HANDLE_WIDTH
108
109static int
110format_enum(FILE *stream, struct value *value, struct value_dict *arguments)
111{
112 long l;
113 if (value_extract_word(value, &l, arguments) < 0)
114 return -1;
115
116 const char *name = type_enum_get(value->type, l);
117 if (name != NULL)
118 return fprintf(stream, "%s", name);
119
120 return format_integer(stream, value, INT_FMT_i, arguments);
Steve Fink6a48a6d2006-08-07 04:29:06 +0200121}
122
Juan Cespedesf1350522008-12-16 18:19:58 +0100123static int
Petr Machata94078ec2012-01-05 18:07:02 +0100124acc_fprintf(int *countp, FILE *stream, const char *format, ...)
125{
126 va_list pa;
127 va_start(pa, format);
128 int i = vfprintf(stream, format, pa);
129 va_end(pa);
130
131 if (i >= 0)
132 *countp += i;
133 return i;
134}
135
136static int
137format_char(FILE *stream, struct value *value, struct value_dict *arguments)
138{
139 long lc;
140 if (value_extract_word(value, &lc, arguments) < 0)
141 return -1;
142 int c = (int)lc;
143
144 /* If this value is not wrapped in array, then this is not a
145 * string, and we need to display quotes. */
146 int quote = !(value->parent != NULL
147 && (value->parent->type->type == ARGTYPE_ARRAY
148 || value->parent->type->type == ARGTYPE_STRING
149 || value->parent->type->type == ARGTYPE_STRING_N));
150 int written = 0;
151 if (quote && acc_fprintf(&written, stream, "'") < 0)
152 return -1;
153
154 const char *fmt;
155 switch (c) {
156 case -1:
157 fmt = "EOF";
158 break;
159 case 0:
160 fmt = "\\0";
161 break;
162 case '\a':
163 fmt = "\\a";
164 break;
165 case '\b':
166 fmt = "\\b";
167 break;
168 case '\t':
169 fmt = "\\t";
170 break;
171 case '\n':
172 fmt = "\\n";
173 break;
174 case '\v':
175 fmt = "\\v";
176 break;
177 case '\f':
178 fmt = "\\f";
179 break;
180 case '\r':
181 fmt = "\\r";
182 break;
183 case '\\':
184 fmt = "\\\\";
185 break;
186 default:
187 if (isprint(c) || c == ' ')
188 fmt = "%c";
189 else if (acc_fprintf(&written, stream, "\\%03o",
190 (unsigned char)c) < 0)
191 return -1;
192 else
193 fmt = NULL;
194 }
195
196 if (fmt != NULL && acc_fprintf(&written, stream, fmt, c) < 0)
197 return -1;
198 if (quote && acc_fprintf(&written, stream, "'") < 0)
199 return -1;
200 return written;
201}
202
203static int
204format_floating(FILE *stream, struct value *value, struct value_dict *arguments)
205{
206 switch (value->type->type) {
207 float f;
208 double d;
209 case ARGTYPE_FLOAT:
210 if (read_float(value, &f, arguments) < 0)
211 return -1;
212 return fprintf(stream, "%f", (double)f);
213 case ARGTYPE_DOUBLE:
214 if (read_double(value, &d, arguments) < 0)
215 return -1;
216 return fprintf(stream, "%f", d);
217 default:
218 abort();
219 }
220}
221
222static int
223format_struct(FILE *stream, struct value *value, struct value_dict *arguments)
224{
225 int written = 0;
226 if (acc_fprintf(&written, stream, "{ ") < 0)
227 return -1;
228 size_t i;
229 for (i = 0; i < type_struct_size(value->type); ++i) {
230 if (i > 0 && acc_fprintf(&written, stream, ", ") < 0)
231 return -1;
232
233 struct value element;
234 if (value_init_element(&element, value, i) < 0)
235 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100236 int o = format_argument(stream, &element, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100237 if (o < 0)
238 return -1;
239 written += o;
240 }
241 if (acc_fprintf(&written, stream, " }") < 0)
242 return -1;
243 return written;
244}
245
246int
247format_pointer(FILE *stream, struct value *value, struct value_dict *arguments)
248{
249 struct value element;
250 if (value_init_deref(&element, value) < 0)
251 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100252 return format_argument(stream, &element, arguments);
Steve Fink1150bc42006-08-07 06:04:43 +0200253}
254
255/*
Petr Machata94078ec2012-01-05 18:07:02 +0100256 * LENGTH is an expression whose evaluation will yield the actual
257 * length of the array.
Steve Fink1150bc42006-08-07 06:04:43 +0200258 *
Petr Machata94078ec2012-01-05 18:07:02 +0100259 * MAXLEN is the actual maximum length that we care about
260 *
261 * BEFORE if LENGTH>MAXLEN, we display ellipsis. We display it before
262 * the closing parenthesis if BEFORE, otherwise after it.
263 *
264 * OPEN, CLOSE, DELIM are opening and closing parenthesis and element
265 * delimiter.
Steve Fink1150bc42006-08-07 06:04:43 +0200266 */
Juan Cespedesf1350522008-12-16 18:19:58 +0100267int
Petr Machata94078ec2012-01-05 18:07:02 +0100268format_array(FILE *stream, struct value *value, struct value_dict *arguments,
269 struct expr_node *length, size_t maxlen, int before,
270 const char *open, const char *close, const char *delim)
Petr Machata000e3112012-01-03 17:03:39 +0100271{
Petr Machata94078ec2012-01-05 18:07:02 +0100272 /* We need "long" to be long enough to cover the whole address
273 * space. */
274 typedef char assert__long_enough_long[-(sizeof(long) < sizeof(void *))];
275 long l = options.strlen;
Petr Machatad7b22922012-01-06 18:34:22 +0100276 if (expr_eval_word(length, value, arguments, &l) < 0)
277 return -1;
Petr Machata94078ec2012-01-05 18:07:02 +0100278 size_t len = (size_t)l;
Steve Fink7bafff02006-08-07 04:50:42 +0200279
Petr Machata94078ec2012-01-05 18:07:02 +0100280 int written = 0;
281 if (acc_fprintf(&written, stream, "%s", open) < 0)
282 return -1;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100283
Zachary T Welchba6aca22010-12-08 18:55:09 -0800284 size_t i;
Petr Machata94078ec2012-01-05 18:07:02 +0100285 for (i = 0; i < len && i <= maxlen; ++i) {
286 if (i == maxlen) {
287 if (before && acc_fprintf(&written, stream, "...") < 0)
288 return -1;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100289 break;
290 }
Petr Machata94078ec2012-01-05 18:07:02 +0100291
292 if (i > 0 && acc_fprintf(&written, stream, "%s", delim) < 0)
293 return -1;
294
295 struct value element;
296 if (value_init_element(&element, value, i) < 0)
297 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100298 int o = format_argument(stream, &element, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100299 if (o < 0)
300 return -1;
301 written += o;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100302 }
Petr Machata94078ec2012-01-05 18:07:02 +0100303 if (acc_fprintf(&written, stream, "%s", close) < 0)
304 return -1;
305 if (i == maxlen && !before && acc_fprintf(&written, stream, "...") < 0)
306 return -1;
307
308 return written;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100309}
310
Petr Machataf6ec08a2012-01-06 16:58:54 +0100311int
312format_argument(FILE *stream, struct value *value, struct value_dict *arguments)
Petr Machata94078ec2012-01-05 18:07:02 +0100313{
Petr Machatad7b22922012-01-06 18:34:22 +0100314 struct expr_node *length = expr_node_zero();
Petr Machata94078ec2012-01-05 18:07:02 +0100315 switch (value->type->type) {
316 case ARGTYPE_VOID:
317 return fprintf(stream, "<void>");
318
319 case ARGTYPE_UNKNOWN:
320 return format_integer(stream, value, INT_FMT_unknown,
321 arguments);
322
323 case ARGTYPE_SHORT:
324 case ARGTYPE_INT:
325 case ARGTYPE_LONG:
326 return format_integer(stream, value, INT_FMT_i, arguments);
327
328 case ARGTYPE_USHORT:
329 case ARGTYPE_UINT:
330 case ARGTYPE_ULONG:
331 return format_integer(stream, value, INT_FMT_u, arguments);
332
333 case ARGTYPE_OCTAL:
334 return format_integer(stream, value, INT_FMT_o, arguments);
335
336 case ARGTYPE_CHAR:
337 return format_char(stream, value, arguments);
338
339 case ARGTYPE_FLOAT:
340 case ARGTYPE_DOUBLE:
341 return format_floating(stream, value, arguments);
342
343 case ARGTYPE_STRUCT:
344 return format_struct(stream, value, arguments);
345
346 case ARGTYPE_POINTER:
347 if (value->type->u.array_info.elt_type->type != ARGTYPE_VOID)
348 return format_pointer(stream, value, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100349 return format_integer(stream, value, INT_FMT_x, arguments);
350
351 case ARGTYPE_ARRAY:
352 if (value->type->u.array_info.elt_type->type != ARGTYPE_CHAR)
353 return format_array(stream, value, arguments,
354 value->type->u.array_info.length,
355 options.arraylen, 1,
356 "[ ", " ]", ", ");
357
358 return format_array(stream, value, arguments,
359 value->type->u.array_info.length,
360 options.strlen, 0, "\"", "\"", "");
361
362 case ARGTYPE_STRING_N:
363 length = value->type->u.string_n_info.length;
364 /* fall-through */
365 case ARGTYPE_FORMAT:
366 case ARGTYPE_STRING: {
367 /* Strings are in fact char pointers. Smuggle in the
368 * pointer here. */
369
370 struct arg_type_info info[2];
371 type_init_array(&info[1], type_get_simple(ARGTYPE_CHAR), 0,
372 length, 0);
373 type_init_pointer(&info[0], &info[1], 0);
374
375 struct value tmp;
376 value_clone(&tmp, value);
377 value_set_type(&tmp, info, 0);
378
Petr Machataf6ec08a2012-01-06 16:58:54 +0100379 int ret = format_argument(stream, &tmp, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100380
381 value_destroy(&tmp);
382 type_destroy(&info[0]);
383 type_destroy(&info[1]);
384 return ret;
385 }
386
387 case ARGTYPE_ENUM:
388 return format_enum(stream, value, arguments);
389
390 case ARGTYPE_COUNT:
391 abort();
392 }
393 abort();
394}