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