File: expr.c

package info (click to toggle)
ltrace 0.7.3-6.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,968 kB
  • sloc: ansic: 22,858; sh: 10,956; exp: 1,498; makefile: 375; cpp: 196; awk: 88; asm: 40; perl: 24
file content (338 lines) | stat: -rw-r--r-- 7,535 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
306
307
308
309
310
311
312
313
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
/*
 * This file is part of ltrace.
 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
 *
 * 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 <string.h>
#include <assert.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>

#include "expr.h"

static void
expr_init_common(struct expr_node *node, enum expr_node_kind kind)
{
	node->kind = kind;
	node->lhs = NULL;
	node->own_lhs = 0;
	memset(&node->u, 0, sizeof(node->u));
}

void
expr_init_self(struct expr_node *node)
{
	expr_init_common(node, EXPR_OP_SELF);
}

void
expr_init_named(struct expr_node *node,
		const char *name, int own_name)
{
	expr_init_common(node, EXPR_OP_NAMED);
	node->u.name.s = name;
	node->u.name.own = own_name;
}

void
expr_init_argno(struct expr_node *node, size_t num)
{
	expr_init_common(node, EXPR_OP_ARGNO);
	node->u.num = num;
}

void
expr_init_const(struct expr_node *node, struct value *val)
{
	expr_init_common(node, EXPR_OP_CONST);
	node->u.value = *val;
}

void
expr_init_const_word(struct expr_node *node, long l,
		     struct arg_type_info *type, int own_type)
{
	struct value val;
	value_init_detached(&val, NULL, type, own_type);
	value_set_word(&val, l);
	expr_init_const(node, &val);
}

void
expr_init_index(struct expr_node *node,
		struct expr_node *lhs, int own_lhs,
		struct expr_node *rhs, int own_rhs)
{
	expr_init_common(node, EXPR_OP_INDEX);
	node->lhs = lhs;
	node->own_lhs = own_lhs;
	node->u.node.n = rhs;
	node->u.node.own = own_rhs;
}

void
expr_init_up(struct expr_node *node, struct expr_node *lhs, int own_lhs)
{
	assert(lhs != NULL);
	expr_init_common(node, EXPR_OP_UP);
	node->lhs = lhs;
	node->own_lhs = own_lhs;
}

void
expr_init_cb1(struct expr_node *node,
	      int (*cb)(struct value *ret_value, struct value *value,
			struct value_dict *arguments, void *data),
	      struct expr_node *lhs, int own_lhs, void *data)
{
	expr_init_common(node, EXPR_OP_CALL1);
	node->lhs = lhs;
	node->own_lhs = own_lhs;
	node->u.call.u.cb1 = cb;
	node->u.call.data = data;
}

void
expr_init_cb2(struct expr_node *node,
	      int (*cb)(struct value *ret_value,
			struct value *lhs, struct value *rhs,
			struct value_dict *arguments, void *data),
	      struct expr_node *lhs, int own_lhs,
	      struct expr_node *rhs, int own_rhs, void *data)
{
	expr_init_common(node, EXPR_OP_CALL2);
	node->lhs = lhs;
	node->own_lhs = own_lhs;
	node->u.call.rhs = rhs;
	node->u.call.own_rhs = own_rhs;
	node->u.call.u.cb2 = cb;
	node->u.call.data = data;
}

static void
release_expr(struct expr_node *node, int own)
{
	if (own) {
		expr_destroy(node);
		free(node);
	}
}

void
expr_destroy(struct expr_node *node)
{
	if (node == NULL)
		return;

	switch (node->kind) {
	case EXPR_OP_ARGNO:
	case EXPR_OP_SELF:
		return;

	case EXPR_OP_CONST:
		value_destroy(&node->u.value);
		return;

	case EXPR_OP_NAMED:
		if (node->u.name.own)
			free((char *)node->u.name.s);
		return;

	case EXPR_OP_INDEX:
		release_expr(node->lhs, node->own_lhs);
		release_expr(node->u.node.n, node->u.node.own);
		return;

	case EXPR_OP_CALL2:
		release_expr(node->u.call.rhs, node->u.call.own_rhs);
		/* Fall through.  */
	case EXPR_OP_UP:
	case EXPR_OP_CALL1:
		release_expr(node->lhs, node->own_lhs);
		return;
	}

	assert(!"Invalid value of node kind");
	abort();
}

int
expr_is_compile_constant(struct expr_node *node)
{
	return node->kind == EXPR_OP_CONST;
}

static int
eval_up(struct expr_node *node, struct value *context,
	struct value_dict *arguments, struct value *ret_value)
{
	if (expr_eval(node->lhs, context, arguments, ret_value) < 0)
		return -1;
	struct value *parent = value_get_parental_struct(ret_value);
	if (parent == NULL) {
		value_destroy(ret_value);
		return -1;
	}
	*ret_value = *parent;
	return 0;
}

static int
eval_cb1(struct expr_node *node, struct value *context,
	 struct value_dict *arguments, struct value *ret_value)
{
	struct value val;
	if (expr_eval(node->lhs, context, arguments, &val) < 0)
		return -1;

	int ret = 0;
	if (node->u.call.u.cb1(ret_value, &val, arguments,
			       node->u.call.data) < 0)
		ret = -1;

	/* N.B. the callback must return its own value, or somehow
	 * clone the incoming argument.  */
	value_destroy(&val);
	return ret;
}

static int
eval_cb2(struct expr_node *node, struct value *context,
	 struct value_dict *arguments, struct value *ret_value)
{
	struct value lhs;
	if (expr_eval(node->lhs, context, arguments, &lhs) < 0)
		return -1;

	struct value rhs;
	if (expr_eval(node->u.call.rhs, context, arguments, &rhs) < 0) {
		value_destroy(&lhs);
		return -1;
	}

	int ret = 0;
	if (node->u.call.u.cb2(ret_value, &lhs, &rhs, arguments,
			       node->u.call.data) < 0)
		ret = -1;

	/* N.B. the callback must return its own value, or somehow
	 * clone the incoming argument.  */
	value_destroy(&lhs);
	value_destroy(&rhs);
	return ret;
}

int
eval_index(struct expr_node *node, struct value *context,
	   struct value_dict *arguments, struct value *ret_value)
{
	struct value lhs;
	if (expr_eval(node->lhs, context, arguments, &lhs) < 0)
		return -1;

	long l;
	if (expr_eval_word(node->u.node.n, context, arguments, &l) < 0) {
	fail:
		value_destroy(&lhs);
		return -1;
	}

	if (value_init_element(ret_value, &lhs, (size_t)l) < 0)
		goto fail;
	return 0;
}

int
expr_eval(struct expr_node *node, struct value *context,
	  struct value_dict *arguments, struct value *ret_value)
{
	switch (node->kind) {
		struct value *valp;
	case EXPR_OP_ARGNO:
		valp = val_dict_get_num(arguments, node->u.num);
		if (valp == NULL)
			return -1;
		*ret_value = *valp;
		return 0;

	case EXPR_OP_NAMED:
		valp = val_dict_get_name(arguments, node->u.name.s);
		if (valp == NULL)
			return -1;
		*ret_value = *valp;
		return 0;

	case EXPR_OP_SELF:
		*ret_value = *context;
		return 0;

	case EXPR_OP_CONST:
		*ret_value = node->u.value;
		return 0;

	case EXPR_OP_INDEX:
		return eval_index(node, context, arguments, ret_value);

	case EXPR_OP_UP:
		return eval_up(node, context, arguments, ret_value);

	case EXPR_OP_CALL1:
		return eval_cb1(node, context, arguments, ret_value);

	case EXPR_OP_CALL2:
		return eval_cb2(node, context, arguments, ret_value);
	}

	assert(!"Unknown node kind.");
	abort();
}

int
expr_eval_word(struct expr_node *node, struct value *context,
	       struct value_dict *arguments, long *ret_value)
{
	struct value val;
	if (expr_eval(node, context, arguments, &val) < 0)
		return -1;
	int ret = 0;
	if (value_extract_word(&val, ret_value, arguments) < 0)
		ret = -1;
	value_destroy(&val);
	return ret;
}

int
expr_eval_constant(struct expr_node *node, long *valuep)
{
	assert(expr_is_compile_constant(node));
	return expr_eval_word(node, NULL, NULL, valuep);
}

struct expr_node *
expr_self(void)
{
	static struct expr_node *node = NULL;
	if (node == NULL) {
		node = malloc(sizeof(*node));
		if (node == NULL)
			error(1, errno, "malloc expr_self");
		expr_init_self(node);
	}
	return node;
}