output.c revision f3db07a81593f9a19d9fb576cd02b5572b8fd823
1#include "config.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <stdarg.h>
6#include <string.h>
7#include <time.h>
8#include <sys/time.h>
9#include <unistd.h>
10
11#include "common.h"
12#include "proc.h"
13#include "library.h"
14
15/* TODO FIXME XXX: include in common.h: */
16extern struct timeval current_time_spent;
17
18Dict *dict_opt_c = NULL;
19
20static Process *current_proc = 0;
21static int current_depth = 0;
22static int current_column = 0;
23
24static void
25output_indent(Process *proc) {
26	current_column +=
27	    fprintf(options.output, "%*s", options.indent * proc->callstack_depth, "");
28}
29
30static void
31begin_of_line(enum tof type, Process *proc) {
32	current_column = 0;
33	if (!proc) {
34		return;
35	}
36	if ((options.output != stderr) && (opt_p || options.follow)) {
37		current_column += fprintf(options.output, "%u ", proc->pid);
38	} else if (options.follow) {
39		current_column += fprintf(options.output, "[pid %u] ", proc->pid);
40	}
41	if (opt_r) {
42		struct timeval tv;
43		struct timezone tz;
44		static struct timeval old_tv = { 0, 0 };
45		struct timeval diff;
46
47		gettimeofday(&tv, &tz);
48
49		if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
50			old_tv.tv_sec = tv.tv_sec;
51			old_tv.tv_usec = tv.tv_usec;
52		}
53		diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
54		if (tv.tv_usec >= old_tv.tv_usec) {
55			diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
56		} else {
57			diff.tv_sec--;
58			diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
59		}
60		old_tv.tv_sec = tv.tv_sec;
61		old_tv.tv_usec = tv.tv_usec;
62		current_column += fprintf(options.output, "%3lu.%06d ",
63					  diff.tv_sec, (int)diff.tv_usec);
64	}
65	if (opt_t) {
66		struct timeval tv;
67		struct timezone tz;
68
69		gettimeofday(&tv, &tz);
70		if (opt_t > 2) {
71			current_column += fprintf(options.output, "%lu.%06d ",
72						  tv.tv_sec, (int)tv.tv_usec);
73		} else if (opt_t > 1) {
74			struct tm *tmp = localtime(&tv.tv_sec);
75			current_column +=
76			    fprintf(options.output, "%02d:%02d:%02d.%06d ",
77				    tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
78				    (int)tv.tv_usec);
79		} else {
80			struct tm *tmp = localtime(&tv.tv_sec);
81			current_column += fprintf(options.output, "%02d:%02d:%02d ",
82						  tmp->tm_hour, tmp->tm_min,
83						  tmp->tm_sec);
84		}
85	}
86	if (opt_i) {
87		if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
88			current_column += fprintf(options.output, "[%p] ",
89						  proc->return_addr);
90		} else {
91			current_column += fprintf(options.output, "[%p] ",
92						  proc->instruction_pointer);
93		}
94	}
95	if (options.indent > 0 && type != LT_TOF_NONE) {
96		output_indent(proc);
97	}
98}
99
100static Function *
101name2func(char const *name) {
102	Function *tmp;
103	const char *str1, *str2;
104
105	tmp = list_of_functions;
106	while (tmp) {
107#ifdef USE_DEMANGLE
108		str1 = options.demangle ? my_demangle(tmp->name) : tmp->name;
109		str2 = options.demangle ? my_demangle(name) : name;
110#else
111		str1 = tmp->name;
112		str2 = name;
113#endif
114		if (!strcmp(str1, str2)) {
115
116			return tmp;
117		}
118		tmp = tmp->next;
119	}
120	return NULL;
121}
122
123void
124output_line(Process *proc, char *fmt, ...) {
125	va_list args;
126
127	if (options.summary) {
128		return;
129	}
130	if (current_proc) {
131		if (current_proc->callstack[current_depth].return_addr) {
132			fprintf(options.output, " <unfinished ...>\n");
133		} else {
134			fprintf(options.output, " <no return ...>\n");
135		}
136	}
137	current_proc = 0;
138	if (!fmt) {
139		return;
140	}
141	begin_of_line(LT_TOF_NONE, proc);
142
143	va_start(args, fmt);
144	vfprintf(options.output, fmt, args);
145	fprintf(options.output, "\n");
146	va_end(args);
147	current_column = 0;
148}
149
150static void
151tabto(int col) {
152	if (current_column < col) {
153		fprintf(options.output, "%*s", col - current_column, "");
154	}
155}
156
157void
158output_left(enum tof type, struct Process *proc,
159	    struct library_symbol *libsym)
160{
161	const char *function_name = libsym->name;
162	Function *func;
163	static arg_type_info *arg_unknown = NULL;
164	if (arg_unknown == NULL)
165	    arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
166
167	if (options.summary) {
168		return;
169	}
170	if (current_proc) {
171		fprintf(options.output, " <unfinished ...>\n");
172		current_column = 0;
173	}
174	current_proc = proc;
175	current_depth = proc->callstack_depth;
176	begin_of_line(type, proc);
177#ifdef USE_DEMANGLE
178	current_column +=
179		fprintf(options.output, "%s->%s(", libsym->lib->name,
180			(options.demangle
181			 ? my_demangle(function_name) : function_name));
182#else
183	current_column += fprintf(options.output, "%s->%s(",
184				  libsym->lib->name, function_name);
185#endif
186
187	func = name2func(function_name);
188	if (!func) {
189		int i;
190		for (i = 0; i < 4; i++) {
191			current_column +=
192			    display_arg(type, proc, i, arg_unknown);
193			current_column += fprintf(options.output, ", ");
194		}
195		current_column += display_arg(type, proc, 4, arg_unknown);
196		return;
197	} else {
198		int i;
199		for (i = 0; i < func->num_params - func->params_right - 1; i++) {
200			current_column +=
201			    display_arg(type, proc, i, func->arg_info[i]);
202			current_column += fprintf(options.output, ", ");
203		}
204		if (func->num_params > func->params_right) {
205			current_column +=
206			    display_arg(type, proc, i, func->arg_info[i]);
207			if (func->params_right) {
208				current_column += fprintf(options.output, ", ");
209			}
210		}
211		if (func->params_right
212		    || func->return_info->type == ARGTYPE_STRING_N
213		    || func->return_info->type == ARGTYPE_ARRAY) {
214			save_register_args(type, proc);
215		}
216	}
217}
218
219void
220output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
221{
222	const char *function_name = libsym->name;
223	Function *func = name2func(function_name);
224	static arg_type_info *arg_unknown = NULL;
225	if (arg_unknown == NULL)
226	    arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
227
228	if (options.summary) {
229		struct opt_c_struct *st;
230		if (!dict_opt_c) {
231			dict_opt_c =
232			    dict_init(dict_key2hash_string,
233				      dict_key_cmp_string);
234		}
235		st = dict_find_entry(dict_opt_c, function_name);
236		if (!st) {
237			char *na;
238			st = malloc(sizeof(struct opt_c_struct));
239			na = strdup(function_name);
240			if (!st || !na) {
241				perror("malloc()");
242				exit(1);
243			}
244			st->count = 0;
245			st->tv.tv_sec = st->tv.tv_usec = 0;
246			dict_enter(dict_opt_c, na, st);
247		}
248		if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
249			st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
250			st->tv.tv_sec++;
251		} else {
252			st->tv.tv_usec += current_time_spent.tv_usec;
253		}
254		st->count++;
255		st->tv.tv_sec += current_time_spent.tv_sec;
256
257//              fprintf(options.output, "%s <%lu.%06d>\n", function_name,
258//                              current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
259		return;
260	}
261	if (current_proc && (current_proc != proc ||
262			    current_depth != proc->callstack_depth)) {
263		fprintf(options.output, " <unfinished ...>\n");
264		current_proc = 0;
265	}
266	if (current_proc != proc) {
267		begin_of_line(type, proc);
268#ifdef USE_DEMANGLE
269		current_column +=
270		    fprintf(options.output, "<... %s resumed> ",
271			    options.demangle ? my_demangle(function_name) : function_name);
272#else
273		current_column +=
274		    fprintf(options.output, "<... %s resumed> ", function_name);
275#endif
276	}
277
278	if (!func) {
279		current_column += fprintf(options.output, ") ");
280		tabto(options.align - 1);
281		fprintf(options.output, "= ");
282		display_arg(type, proc, -1, arg_unknown);
283	} else {
284		int i;
285		for (i = func->num_params - func->params_right;
286		     i < func->num_params - 1; i++) {
287			current_column +=
288			    display_arg(type, proc, i, func->arg_info[i]);
289			current_column += fprintf(options.output, ", ");
290		}
291		if (func->params_right) {
292			current_column +=
293			    display_arg(type, proc, i, func->arg_info[i]);
294		}
295		current_column += fprintf(options.output, ") ");
296		tabto(options.align - 1);
297		fprintf(options.output, "= ");
298		if (func->return_info->type == ARGTYPE_VOID) {
299			fprintf(options.output, "<void>");
300		} else {
301			display_arg(type, proc, -1, func->return_info);
302		}
303	}
304	if (opt_T) {
305		fprintf(options.output, " <%lu.%06d>",
306			current_time_spent.tv_sec,
307			(int)current_time_spent.tv_usec);
308	}
309	fprintf(options.output, "\n");
310
311#if defined(HAVE_LIBUNWIND)
312	if (options.bt_depth > 0) {
313		unw_cursor_t cursor;
314		unw_word_t ip, sp;
315		int unwind_depth = options.bt_depth;
316		char fn_name[100];
317
318		unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
319		while (unwind_depth) {
320			unw_get_reg(&cursor, UNW_REG_IP, &ip);
321			unw_get_reg(&cursor, UNW_REG_SP, &sp);
322			unw_get_proc_name(&cursor, fn_name, 100, NULL);
323			fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
324			if (unw_step(&cursor) <= 0)
325				break;
326			unwind_depth--;
327		}
328		fprintf(options.output, "\n");
329	}
330#endif /* defined(HAVE_LIBUNWIND) */
331
332	current_proc = 0;
333	current_column = 0;
334}
335