blob: 8b514eb9ac681258bd0819f76a98021d5788bf80 [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
Petr Machata94078ec2012-01-05 18:07:02 +0100148 || 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;
Petr Machatad7b22922012-01-06 18:34:22 +0100275 if (expr_eval_word(length, value, arguments, &l) < 0)
276 return -1;
Petr Machata94078ec2012-01-05 18:07:02 +0100277 size_t len = (size_t)l;
Steve Fink7bafff02006-08-07 04:50:42 +0200278
Petr Machata94078ec2012-01-05 18:07:02 +0100279 int written = 0;
280 if (acc_fprintf(&written, stream, "%s", open) < 0)
281 return -1;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100282
Zachary T Welchba6aca22010-12-08 18:55:09 -0800283 size_t i;
Petr Machata94078ec2012-01-05 18:07:02 +0100284 for (i = 0; i < len && i <= maxlen; ++i) {
285 if (i == maxlen) {
286 if (before && acc_fprintf(&written, stream, "...") < 0)
287 return -1;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100288 break;
289 }
Petr Machata94078ec2012-01-05 18:07:02 +0100290
291 if (i > 0 && acc_fprintf(&written, stream, "%s", delim) < 0)
292 return -1;
293
294 struct value element;
295 if (value_init_element(&element, value, i) < 0)
296 return -1;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100297 int o = format_argument(stream, &element, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100298 if (o < 0)
299 return -1;
300 written += o;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100301 }
Petr Machata94078ec2012-01-05 18:07:02 +0100302 if (acc_fprintf(&written, stream, "%s", close) < 0)
303 return -1;
304 if (i == maxlen && !before && acc_fprintf(&written, stream, "...") < 0)
305 return -1;
306
307 return written;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100308}
309
Petr Machataf6ec08a2012-01-06 16:58:54 +0100310int
311format_argument(FILE *stream, struct value *value, struct value_dict *arguments)
Petr Machata94078ec2012-01-05 18:07:02 +0100312{
Petr Machatad7b22922012-01-06 18:34:22 +0100313 struct expr_node *length = expr_node_zero();
Petr Machata94078ec2012-01-05 18:07:02 +0100314 switch (value->type->type) {
315 case ARGTYPE_VOID:
316 return fprintf(stream, "<void>");
317
318 case ARGTYPE_UNKNOWN:
319 return format_integer(stream, value, INT_FMT_unknown,
320 arguments);
321
322 case ARGTYPE_SHORT:
323 case ARGTYPE_INT:
324 case ARGTYPE_LONG:
325 return format_integer(stream, value, INT_FMT_i, arguments);
326
327 case ARGTYPE_USHORT:
328 case ARGTYPE_UINT:
329 case ARGTYPE_ULONG:
330 return format_integer(stream, value, INT_FMT_u, arguments);
331
332 case ARGTYPE_OCTAL:
333 return format_integer(stream, value, INT_FMT_o, arguments);
334
335 case ARGTYPE_CHAR:
336 return format_char(stream, value, arguments);
337
338 case ARGTYPE_FLOAT:
339 case ARGTYPE_DOUBLE:
340 return format_floating(stream, value, arguments);
341
342 case ARGTYPE_STRUCT:
343 return format_struct(stream, value, arguments);
344
345 case ARGTYPE_POINTER:
346 if (value->type->u.array_info.elt_type->type != ARGTYPE_VOID)
347 return format_pointer(stream, value, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100348 return format_integer(stream, value, INT_FMT_x, arguments);
349
350 case ARGTYPE_ARRAY:
351 if (value->type->u.array_info.elt_type->type != ARGTYPE_CHAR)
352 return format_array(stream, value, arguments,
353 value->type->u.array_info.length,
354 options.arraylen, 1,
355 "[ ", " ]", ", ");
356
357 return format_array(stream, value, arguments,
358 value->type->u.array_info.length,
359 options.strlen, 0, "\"", "\"", "");
360
361 case ARGTYPE_STRING_N:
362 length = value->type->u.string_n_info.length;
363 /* fall-through */
Petr Machata2bea8612012-01-06 21:04:03 +0100364 case ARGTYPE_FORMAT: {
Petr Machata94078ec2012-01-05 18:07:02 +0100365 /* Strings are in fact char pointers. Smuggle in the
366 * pointer here. */
367
368 struct arg_type_info info[2];
369 type_init_array(&info[1], type_get_simple(ARGTYPE_CHAR), 0,
370 length, 0);
371 type_init_pointer(&info[0], &info[1], 0);
372
373 struct value tmp;
374 value_clone(&tmp, value);
375 value_set_type(&tmp, info, 0);
376
Petr Machataf6ec08a2012-01-06 16:58:54 +0100377 int ret = format_argument(stream, &tmp, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100378
379 value_destroy(&tmp);
380 type_destroy(&info[0]);
381 type_destroy(&info[1]);
382 return ret;
383 }
384
385 case ARGTYPE_ENUM:
386 return format_enum(stream, value, arguments);
Petr Machata94078ec2012-01-05 18:07:02 +0100387 }
388 abort();
389}