aboutsummaryrefslogtreecommitdiff
path: root/output.c
blob: 1ced9221e29848020177929be055b7f907b1f5b9 (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
#include <stdio.h>
#include <stdarg.h>

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

static pid_t current_pid = 0;
static int current_column = 0;

static void begin_of_line(enum tof type, struct process * proc)
{
	current_column = 0;
	if (!proc) {
		return;
	}
	if (list_of_processes && list_of_processes->next) {
		current_column += fprintf(output, "[pid %d] ", proc->pid);
	}
	if (opt_i) {
		if (type==LT_TOF_FUNCTION) {
			current_column += fprintf(output, "[%08x] ",
				(unsigned)proc->return_addr);
		} else {
			current_column += fprintf(output, "[%08x] ",
				(unsigned)proc->instruction_pointer);
		}
	}
}

static struct function * name2func(char * name)
{
	struct function * tmp;

	tmp = list_of_functions;
	while(tmp) {
		if (!strcmp(tmp->name, name)) {
			return tmp;
		}
		tmp = tmp->next;
	}
	return NULL;
}

void output_line(struct process * proc, char *fmt, ...)
{
	va_list args;

	if (current_pid) {
		fprintf(output, " <unfinished ...>\n");
	}
	begin_of_line(LT_TOF_NONE, proc);

        va_start(args, fmt);
        vfprintf(output, fmt, args);
        fprintf(output, "\n");
        va_end(args);
	current_pid=0;
	current_column=0;
}

static void tabto(int col)
{
	if (current_column < col) {
		fprintf(output, "%*s", col-current_column, "");
	}
}

void output_left(enum tof type, struct process * proc, char * function_name)
{
	struct function * func;

	if (current_pid) {
#if 0			/* FIXME: should I do this? */
		if (current_pid == proc->pid
			&& proc->type_being_displayed == LT_TOF_FUNCTION
			&& proc->type_being_displayed == type) {
				tabto(opt_a);
				fprintf(output, "= ???\n");
		} else
#endif
			fprintf(output, " <unfinished ...>\n");
		current_pid=0;
		current_column=0;
	}
	current_pid=proc->pid;
	proc->type_being_displayed = type;
	begin_of_line(type, proc);
	current_column += fprintf(output, "%s(", function_name);

	func = name2func(function_name);
	if (!func) {
		int i;
		for(i=0; i<4; i++) {
			current_column += display_arg(type, proc, i, LT_PT_UNKNOWN);
			current_column += fprintf(output, ", ");
		}
		current_column += display_arg(type, proc, 4, LT_PT_UNKNOWN);
		return;
	} else {
		int i;
		for(i=0; i< func->num_params - func->params_right - 1; i++) {
			current_column += display_arg(type, proc, i, func->param_types[i]);
			current_column += fprintf(output, ", ");
		}
		if (func->num_params>func->params_right) {
			current_column += display_arg(type, proc, i, func->param_types[i]);
			if (func->params_right) {
				current_column += fprintf(output, ", ");
			}
		}
		if (!func->params_right && func->return_type == LT_PT_VOID) {
			current_column += fprintf(output, ") ");
			tabto(opt_a);
			fprintf(output, "= <void>\n");
			current_pid = 0;
			current_column = 0;
		}
	}
}

void output_right(enum tof type, struct process * proc, char * function_name)
{
	struct function * func = name2func(function_name);

	if (func && func->params_right==0 && func->return_type == LT_PT_VOID) {
		return;
	}

	if (current_pid && current_pid!=proc->pid) {
		fprintf(output, " <unfinished ...>\n");
		begin_of_line(type, proc);
		current_column += fprintf(output, "<... %s resumed> ", function_name);
	} else if (!current_pid) {
		begin_of_line(type, proc);
		current_column += fprintf(output, "<... %s resumed> ", function_name);
	}

	if (!func) {
		current_column += fprintf(output, ") ");
		tabto(opt_a);
		fprintf(output, "= ");
		display_arg(type, proc, -1, LT_PT_UNKNOWN);
		fprintf(output, "\n");
	} else {
		int i;
		for(i=func->num_params-func->params_right; i<func->num_params-1; i++) {
			current_column += display_arg(type, proc, i, func->param_types[i]);
			current_column += fprintf(output, ", ");
		}
		if (func->params_right) {
			current_column += display_arg(type, proc, i, func->param_types[i]);
		}
		current_column += fprintf(output, ") ");
			tabto(opt_a);
			fprintf(output, "= ");
		if (func->return_type == LT_PT_VOID) {
			fprintf(output, "<void>");
		} else {
			display_arg(type, proc, -1, func->return_type);
		}
		fprintf(output, "\n");
	}
	current_pid=0;
	current_column=0;
}