aboutsummaryrefslogtreecommitdiff
path: root/display_args.c
blob: 083c9dceb36bf01c07758312aa12d55dd33052a7 (plain)
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
#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "ltrace.h"
#include "options.h"

static int display_char(int what);
static int display_string(enum tof type, struct process *proc, int arg_num);
static int display_stringN(int arg2, enum tof type, struct process *proc,
			   int arg_num);
static int display_unknown(enum tof type, struct process *proc, int arg_num);
static int display_format(enum tof type, struct process *proc, int arg_num);

int
display_arg(enum tof type, struct process *proc, int arg_num, enum arg_type at)
{
	int tmp;
	long arg;

	switch (at) {
	case ARGTYPE_VOID:
		return 0;
	case ARGTYPE_INT:
		return fprintf(output, "%d",
			       (int)gimme_arg(type, proc, arg_num));
	case ARGTYPE_UINT:
		return fprintf(output, "%u",
			       (unsigned)gimme_arg(type, proc, arg_num));
	case ARGTYPE_LONG:
		if (proc->mask_32bit)
			return fprintf(output, "%d",
				       (int)gimme_arg(type, proc, arg_num));
		return fprintf(output, "%ld", gimme_arg(type, proc, arg_num));
	case ARGTYPE_ULONG:
		if (proc->mask_32bit)
			return fprintf(output, "%u",
				       (unsigned)gimme_arg(type, proc,
							   arg_num));
		return fprintf(output, "%lu",
			       (unsigned long)gimme_arg(type, proc, arg_num));
	case ARGTYPE_OCTAL:
		return fprintf(output, "0%o",
			       (unsigned)gimme_arg(type, proc, arg_num));
	case ARGTYPE_CHAR:
		tmp = fprintf(output, "'");
		tmp += display_char((int)gimme_arg(type, proc, arg_num));
		tmp += fprintf(output, "'");
		return tmp;
	case ARGTYPE_ADDR:
		arg = gimme_arg(type, proc, arg_num);
		if (!arg) {
			return fprintf(output, "NULL");
		} else {
			return fprintf(output, "%p", (void *)arg);
		}
	case ARGTYPE_FORMAT:
		return display_format(type, proc, arg_num);
	case ARGTYPE_STRING:
		return display_string(type, proc, arg_num);
	case ARGTYPE_STRING0:
		return display_stringN(0, type, proc, arg_num);
	case ARGTYPE_STRING1:
		return display_stringN(1, type, proc, arg_num);
	case ARGTYPE_STRING2:
		return display_stringN(2, type, proc, arg_num);
	case ARGTYPE_STRING3:
		return display_stringN(3, type, proc, arg_num);
	case ARGTYPE_STRING4:
		return display_stringN(4, type, proc, arg_num);
	case ARGTYPE_STRING5:
		return display_stringN(5, type, proc, arg_num);
	case ARGTYPE_UNKNOWN:
	default:
		return display_unknown(type, proc, arg_num);
	}
	return fprintf(output, "?");
}

static int display_char(int what)
{
	switch (what) {
	case -1:
		return fprintf(output, "EOF");
	case '\r':
		return fprintf(output, "\\r");
	case '\n':
		return fprintf(output, "\\n");
	case '\t':
		return fprintf(output, "\\t");
	case '\b':
		return fprintf(output, "\\b");
	case '\\':
		return fprintf(output, "\\\\");
	default:
		if ((what < 32) || (what > 126)) {
			return fprintf(output, "\\%03o", (unsigned char)what);
		} else {
			return fprintf(output, "%c", what);
		}
	}
}

static int string_maxlength = INT_MAX;

#define MIN(a,b) (((a)<(b)) ? (a) : (b))

static int display_string(enum tof type, struct process *proc, int arg_num)
{
	void *addr;
	unsigned char *str1;
	int i;
	int len = 0;

	addr = (void *)gimme_arg(type, proc, arg_num);
	if (!addr) {
		return fprintf(output, "NULL");
	}

	str1 = malloc(MIN(opt_s, string_maxlength) + 3);
	if (!str1) {
		return fprintf(output, "???");
	}
	umovestr(proc, addr, MIN(opt_s, string_maxlength) + 1, str1);
	len = fprintf(output, "\"");
	for (i = 0; i < MIN(opt_s, string_maxlength); i++) {
		if (str1[i]) {
			len += display_char(str1[i]);
		} else {
			break;
		}
	}
	len += fprintf(output, "\"");
	if (str1[i] && (opt_s <= string_maxlength)) {
		len += fprintf(output, "...");
	}
	free(str1);
	return len;
}

static int
display_stringN(int arg2, enum tof type, struct process *proc, int arg_num)
{
	int a;

	string_maxlength = gimme_arg(type, proc, arg2 - 1);
	a = display_string(type, proc, arg_num);
	string_maxlength = INT_MAX;
	return a;
}

static int display_unknown(enum tof type, struct process *proc, int arg_num)
{
	long tmp;

	tmp = gimme_arg(type, proc, arg_num);

	if (proc->mask_32bit) {
		if ((int)tmp < 1000000 && (int)tmp > -1000000)
			return fprintf(output, "%d", (int)tmp);
		else
			return fprintf(output, "%p", (void *)tmp);
	} else if (tmp < 1000000 && tmp > -1000000) {
		return fprintf(output, "%ld", tmp);
	} else {
		return fprintf(output, "%p", (void *)tmp);
	}
}

static int display_format(enum tof type, struct process *proc, int arg_num)
{
	void *addr;
	unsigned char *str1;
	int i;
	int len = 0;

	addr = (void *)gimme_arg(type, proc, arg_num);
	if (!addr) {
		return fprintf(output, "NULL");
	}

	str1 = malloc(MIN(opt_s, string_maxlength) + 3);
	if (!str1) {
		return fprintf(output, "???");
	}
	umovestr(proc, addr, MIN(opt_s, string_maxlength) + 1, str1);
	len = fprintf(output, "\"");
	for (i = 0; len < MIN(opt_s, string_maxlength) + 1; i++) {
		if (str1[i]) {
			len += display_char(str1[i]);
		} else {
			break;
		}
	}
	len += fprintf(output, "\"");
	if (str1[i] && (opt_s <= string_maxlength)) {
		len += fprintf(output, "...");
	}
	for (i = 0; str1[i]; i++) {
		if (str1[i] == '%') {
			int is_long = 0;
			while (1) {
				unsigned char c = str1[++i];
				if (c == '%') {
					break;
				} else if (!c) {
					break;
				} else if (strchr("lzZtj", c)) {
					is_long++;
					if (c == 'j')
						is_long++;
					if (is_long > 1
					    && (sizeof(long) < sizeof(long long)
						|| proc->mask_32bit)) {
						len += fprintf(output, ", ...");
						str1[i + 1] = '\0';
						break;
					}
				} else if (c == 'd' || c == 'i') {
					if (!is_long || proc->mask_32bit)
						len +=
						    fprintf(output, ", %d",
							    (int)gimme_arg(type,
									   proc,
									   ++arg_num));
					else
						len +=
						    fprintf(output, ", %ld",
							    gimme_arg(type,
								      proc,
								      ++arg_num));
					break;
				} else if (c == 'u') {
					if (!is_long || proc->mask_32bit)
						len +=
						    fprintf(output, ", %u",
							    (int)gimme_arg(type,
									   proc,
									   ++arg_num));
					else
						len +=
						    fprintf(output, ", %lu",
							    gimme_arg(type,
								      proc,
								      ++arg_num));
					break;
				} else if (c == 'o') {
					if (!is_long || proc->mask_32bit)
						len +=
						    fprintf(output, ", 0%o",
							    (int)gimme_arg(type,
									   proc,
									   ++arg_num));
					else
						len +=
						    fprintf(output, ", 0%lo",
							    gimme_arg(type,
								      proc,
								      ++arg_num));
					break;
				} else if (c == 'x' || c == 'X') {
					if (!is_long || proc->mask_32bit)
						len +=
						    fprintf(output, ", %#x",
							    (int)gimme_arg(type,
									   proc,
									   ++arg_num));
					else
						len +=
						    fprintf(output, ", %#lx",
							    gimme_arg(type,
								      proc,
								      ++arg_num));
					break;
				} else if (strchr("eEfFgGaACS", c)
					   || (is_long
					       && (c == 'c' || c == 's'))) {
					len += fprintf(output, ", ...");
					str1[i + 1] = '\0';
					break;
				} else if (c == 'c') {
					len += fprintf(output, ", '");
					len +=
					    display_char((int)
							 gimme_arg(type, proc,
								   ++arg_num));
					len += fprintf(output, "'");
					break;
				} else if (c == 's') {
					len += fprintf(output, ", ");
					len +=
					    display_string(type, proc,
							   ++arg_num);
					break;
				} else if (c == 'p' || c == 'n') {
					len +=
					    fprintf(output, ", %p",
						    (void *)gimme_arg(type,
								      proc,
								      ++arg_num));
					break;
				} else if (c == '*') {
					len +=
					    fprintf(output, ", %d",
						    (int)gimme_arg(type, proc,
								   ++arg_num));
				}
			}
		}
	}
	free(str1);
	return len;
}