aboutsummaryrefslogtreecommitdiff
path: root/display_args.c
blob: e5ca197fb0c4630b7fc886096411f14b2d5f8688 (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
#include <stdio.h>
#include <stdlib.h>

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

static int display_char(char 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);

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

	switch(rt) {
		case LT_PT_VOID:
			return 0;
		case LT_PT_INT:
			return fprintf(output, "%d", (int)gimme_arg(type, proc, arg_num));
		case LT_PT_UINT:
			return fprintf(output, "%u", (unsigned)gimme_arg(type, proc, arg_num));
		case LT_PT_OCTAL:
			return fprintf(output, "0%o", (unsigned)gimme_arg(type, proc, arg_num));
		case LT_PT_CHAR:
			tmp = fprintf(output, "'");
			tmp += display_char((int)gimme_arg(type, proc, arg_num));
			tmp += fprintf(output, "'");
			return tmp;
		case LT_PT_ADDR:
			arg = gimme_arg(type, proc, arg_num);
			if (!arg) {
				return fprintf(output, "NULL");
			} else {
				return fprintf(output, "0x%08x", (unsigned)arg);
			}
		case LT_PT_FORMAT:
		case LT_PT_STRING:
			return display_string(type, proc, arg_num);
		case LT_PT_STRING0:
			return display_stringN(0, type, proc, arg_num);
		case LT_PT_STRING1:
			return display_stringN(1, type, proc, arg_num);
		case LT_PT_STRING2:
			return display_stringN(2, type, proc, arg_num);
		case LT_PT_STRING3:
			return display_stringN(3, type, proc, arg_num);
		case LT_PT_UNKNOWN:
		default:
			return display_unknown(type, proc, arg_num);
	}
	return fprintf(output, "?");
}

static int display_char(char 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 '\\':	return fprintf(output, "\\");
		default:
			if ((what<32) || (what>126)) {
				return fprintf(output, "\\%03o", what);
			} else {
				return fprintf(output, "%c", what);
			}
	}
}

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

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

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

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

static int display_stringN(int arg2, enum tof type, struct process * proc, int arg_num)
{
	int a,b;
	a = gimme_arg(type, proc, arg2-1);
	b = opt_s;
	opt_s = MIN(opt_s, a);
	a = display_string(type, proc, arg_num);
	opt_s = b;
	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 (tmp<1000000 && tmp>-1000000) {
		return fprintf(output, "%ld", tmp);
	} else {
		return fprintf(output, "0x%08lx", tmp);
	}
}