printf.c 8.86 KB
Newer Older
1 2
/*
 * This file is part of ltrace.
3
 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (C) 1998,2004,2007,2008,2009 Juan Cespedes
 * Copyright (C) 2006 Steve Fink
 * Copyright (C) 2006 Ian Wienand
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <assert.h>
25
#include <stdint.h>
26
#include <stdlib.h>
27
#include <string.h>
28 29 30 31 32 33 34

#include "printf.h"
#include "type.h"
#include "value.h"
#include "expr.h"
#include "zero.h"
#include "param.h"
35
#include "lens_default.h"
36 37 38 39 40 41 42 43

struct param_enum {
	struct value array;
	int percent;
	size_t *future_length;
	char *format;
	char const *ptr;
	char const *end;
44
	size_t width;
45 46 47 48 49 50 51 52
};

static struct param_enum *
param_printf_init(struct value *cb_args, size_t nargs,
		  struct value_dict *arguments)
{
	assert(nargs == 1);

53 54 55 56
	struct process *proc = cb_args[0].inferior;
	assert(proc != NULL);

	/* We expect a pointer to array.  */
57
	if (cb_args->type->type != ARGTYPE_POINTER
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	    || cb_args->type->u.ptr_info.info->type != ARGTYPE_ARRAY)
		return NULL;

	/* The element type should be either character (for narrow
	 * strings) or an integral type (for wide strings).  */
	struct arg_type_info *et
		= cb_args->type->u.ptr_info.info->u.array_info.elt_type;
	switch (et->type) {
	case ARGTYPE_CHAR:
	case ARGTYPE_SHORT:
	case ARGTYPE_USHORT:
	case ARGTYPE_INT:
	case ARGTYPE_UINT:
	case ARGTYPE_LONG:
	case ARGTYPE_ULONG:
		break;
	default:
75
		return NULL;
76
	}
77 78 79 80 81 82 83

	struct param_enum *self = malloc(sizeof(*self));
	if (self == NULL) {
	fail:
		free(self);
		return NULL;
	}
84 85 86
	self->width = type_sizeof(proc, et);
	if (self->width == (size_t) -1)
		goto fail;
87

88
	if (value_init_deref(&self->array, cb_args) < 0)
89 90 91 92
		goto fail;
	assert(self->array.type->type == ARGTYPE_ARRAY);

	self->format = (char *)value_get_data(&self->array, arguments);
93 94
	if (self->format == NULL) {
		value_destroy(&self->array);
95
		goto fail;
96
	}
97 98

	size_t size = value_size(&self->array, arguments);
99 100
	if (size == (size_t)-1) {
		value_destroy(&self->array);
101
		goto fail;
102
	}
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

	self->percent = 0;
	self->ptr = self->format;
	self->end = self->format + size;
	self->future_length = NULL;
	return self;
}

static void
drop_future_length(struct param_enum *self)
{
	if (self->future_length != NULL) {
		free(self->future_length);
		self->future_length = NULL;
	}
}

static int
form_next_param(struct param_enum *self,
		enum arg_type format_type, enum arg_type elt_type,
		unsigned hlf, unsigned lng, char *len_buf, size_t len_buf_len,
		struct arg_type_info *infop)
{
	/* XXX note: Some types are wrong because we lack
	   ARGTYPE_LONGLONG, ARGTYPE_UCHAR and ARGTYPE_SCHAR.  */
	assert(lng <= 2);
	assert(hlf <= 2);
	static enum arg_type ints[] =
		{ ARGTYPE_CHAR, ARGTYPE_SHORT, ARGTYPE_INT,
		  ARGTYPE_LONG, ARGTYPE_ULONG };
	static enum arg_type uints[] =
		{ ARGTYPE_CHAR, ARGTYPE_USHORT, ARGTYPE_UINT,
		  ARGTYPE_ULONG, ARGTYPE_ULONG };

	struct arg_type_info *elt_info = NULL;
138
	if (format_type == ARGTYPE_ARRAY || format_type == ARGTYPE_POINTER)
139
		elt_info = type_get_simple(elt_type);
140
	else if (format_type == ARGTYPE_INT)
141 142 143 144 145 146
		format_type = ints[2 + lng - hlf];
	else if (format_type == ARGTYPE_UINT)
		format_type = uints[2 + lng - hlf];


	if (format_type == ARGTYPE_ARRAY) {
147 148 149 150
		struct arg_type_info *array = malloc(sizeof(*array));
		if (array == NULL)
			return -1;

151
		struct expr_node *node = NULL;
152
		int own_node;
153 154 155 156 157 158 159 160 161 162
		if (len_buf_len != 0
		    || self->future_length != NULL) {
			struct tmp {
				struct expr_node node;
				struct arg_type_info type;
			};
			struct tmp *len = malloc(sizeof(*len));
			if (len == NULL) {
			fail:
				free(len);
163
				free(array);
164 165 166
				return -1;
			}

167
			len->type = *type_get_simple(ARGTYPE_LONG);
168 169 170 171 172 173 174 175 176 177 178 179 180 181

			long l;
			if (self->future_length != NULL) {
				l = *self->future_length;
				drop_future_length(self);
			} else {
				l = atol(len_buf);
			}

			expr_init_const_word(&len->node, l, &len->type, 0);

			node = build_zero_w_arg(&len->node, 1);
			if (node == NULL)
				goto fail;
182
			own_node = 1;
183 184 185

		} else {
			node = expr_node_zero();
186
			own_node = 0;
187 188 189
		}
		assert(node != NULL);

190 191 192
		type_init_array(array, elt_info, 0, node, own_node);
		type_init_pointer(infop, array, 1);

193
	} else if (format_type == ARGTYPE_POINTER) {
194
		type_init_pointer(infop, elt_info, 0);
195 196 197 198 199 200 201 202 203 204 205 206 207 208

	} else {
		*infop = *type_get_simple(format_type);
	}

	return 0;
}

static int
param_printf_next(struct param_enum *self, struct arg_type_info *infop,
		  int *insert_stop)
{
	unsigned hlf = 0;
	unsigned lng = 0;
209 210
	enum arg_type format_type = ARGTYPE_VOID;
	enum arg_type elt_type = ARGTYPE_VOID;
211 212
	char len_buf[25] = {};
	size_t len_buf_len = 0;
Petr Machata's avatar
Petr Machata committed
213
	struct lens *lens = NULL;
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	for (; self->ptr < self->end; self->ptr += self->width) {
		union {
			uint8_t u8;
			uint16_t u16;
			uint32_t u32;
			uint64_t u64;
			char buf[0];
		} u;
		memcpy(u.buf, self->ptr, self->width);
		switch (self->width) {
		case 1: u.u64 = u.u8; break;
		case 2: u.u64 = u.u16; break;
		case 4: u.u64 = u.u32; break;
		}
		uint64_t c = u.u64;

231
		if (!self->percent) {
232
			if (c == '%')
233 234 235 236
				self->percent = 1;
			continue;
		}

237
		switch (c) {
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
		case '#': case ' ': case '-':
		case '+': case 'I': case '\'':
			/* These are only important for formatting,
			 * not for interpreting the type.  */
			continue;

		case '*':
			/* Length parameter given in the next
			 * argument.  */
			if (self->future_length == NULL)
				/* This should really be an assert,
				 * but we can't just fail on invalid
				 * format string.  */
				self->future_length
					= malloc(sizeof(*self->future_length));

			if (self->future_length != NULL) {
255
				self->ptr += self->width;
256 257 258 259 260 261 262 263 264 265 266 267
				format_type = ARGTYPE_INT;
				break;
			}

		case '0':
		case '1': case '2': case '3':
		case '4': case '5': case '6':
		case '7': case '8': case '9':
			/* Field length likewise, but we need to parse
			 * this to attach the appropriate string
			 * length expression.  */
			if (len_buf_len < sizeof(len_buf) - 1)
268
				len_buf[len_buf_len++] = c;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
			continue;

		case 'h':
			if (hlf < 2)
				hlf++;
			continue;

		case 'l':
			if (lng < 2)
				lng++;
			continue;

		case 'q':
			lng = 2;
			continue;

		case 'L': /* long double */
			lng = 1;
			continue;

		case 'j': /* intmax_t */
			/*   XXX ABI should know */
			lng = 2;
			continue;

		case 't': /* ptrdiff_t */
		case 'Z': case 'z': /* size_t */
			lng = 1; /* XXX ABI should tell */
			continue;

		case 'd':
		case 'i':
			format_type = ARGTYPE_INT;
			self->percent = 0;
			break;

		case 'o':
306 307 308
			lens = &octal_lens;
			goto uint;

309
		case 'x': case 'X':
310
			lens = &hex_lens;
311
			/* Fall through.  */
312 313
		case 'u':
		uint:
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
			format_type = ARGTYPE_UINT;
			self->percent = 0;
			break;

		case 'e': case 'E':
		case 'f': case 'F':
		case 'g': case 'G':
		case 'a': case 'A':
			format_type = ARGTYPE_DOUBLE;
			self->percent = 0;
			break;

		case 'C': /* like "lc" */
			if (lng == 0)
				lng++;
		case 'c':
			/* XXX "lc" means wchar_t string.  */
			format_type = ARGTYPE_CHAR;
			self->percent = 0;
			break;

		case 'S': /* like "ls" */
			if (lng == 0)
				lng++;
		case 's':
			format_type = ARGTYPE_ARRAY;
340
			elt_type = lng == 0 ? ARGTYPE_CHAR : ARGTYPE_INT;
341
			self->percent = 0;
342
			lens = &string_lens;
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
			break;

		case 'p':
		case 'n': /* int* where to store no. of printed chars.  */
			format_type = ARGTYPE_POINTER;
			elt_type = ARGTYPE_VOID;
			self->percent = 0;
			break;

		case 'm': /* (glibc) print argument of errno */
		case '%':
			lng = 0;
			hlf = 0;
			self->percent = 0;
			continue;

		default:
			continue;
		}

		/* If we got here, the type must have been set.  */
364
		assert(format_type != ARGTYPE_VOID);
365 366 367 368 369

		if (form_next_param(self, format_type, elt_type, hlf, lng,
				    len_buf, len_buf_len, infop) < 0)
			return -1;

Petr Machata's avatar
Petr Machata committed
370 371 372
		infop->lens = lens;
		infop->own_lens = 0;

373 374 375
		return 0;
	}

376
	*infop = *type_get_simple(ARGTYPE_VOID);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	return 0;
}

static enum param_status
param_printf_stop(struct param_enum *self, struct value *value)
{
	if (self->future_length != NULL
	    && value_extract_word(value, (long *)self->future_length, NULL) < 0)
		drop_future_length(self);

	return PPCB_CONT;
}

static void
param_printf_done(struct param_enum *context)
{
393
	value_destroy(&context->array);
394 395 396 397 398 399
	free(context);
}

void
param_pack_init_printf(struct param *param, struct expr_node *arg, int own_arg)
{
400
	param_init_pack(param, PARAM_PACK_VARARGS, arg, 1, own_arg,
401 402 403
			&param_printf_init, &param_printf_next,
			&param_printf_stop, &param_printf_done);
}