blob: f30c9fea3098f6d41fad3d466e9689b4a8f0381e [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes3268a161997-08-25 16:45:22 +02005#include <stdio.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +01006#include <stdlib.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +02007#include <stdarg.h>
Juan Cespedes1cd999a2001-07-03 00:46:04 +02008#include <string.h>
Juan Cespedes5e0acdb1998-04-04 08:34:07 +02009#include <time.h>
10#include <sys/time.h>
11#include <unistd.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020012
13#include "ltrace.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010014#include "options.h"
15#include "output.h"
Juan Cespedesd65efa32003-02-03 00:22:30 +010016#include "dict.h"
Juan Cespedes5e4455b1997-08-24 01:48:26 +020017
Juan Cespedesd914a202004-11-10 00:15:33 +010018#ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020019#include "demangle.h"
20#endif
21
Juan Cespedesd65efa32003-02-03 00:22:30 +010022/* TODO FIXME XXX: include in ltrace.h: */
23extern struct timeval current_time_spent;
24
Ian Wienand2d45b1a2006-02-20 22:48:07 +010025struct dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010026
Paul Gilliam76c61f12006-06-14 06:55:21 +020027static struct process *current_proc = 0;
Juan Cespedes5916fda2002-02-25 00:19:21 +010028static int current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010029static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020030
Ian Wienand2d45b1a2006-02-20 22:48:07 +010031static void output_indent(struct process *proc)
32{
33 current_column +=
34 fprintf(output, "%*s", opt_n * proc->callstack_depth, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020035}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020036
Ian Wienand2d45b1a2006-02-20 22:48:07 +010037static void begin_of_line(enum tof type, struct process *proc)
38{
Juan Cespedes5e01f651998-03-08 22:31:44 +010039 current_column = 0;
40 if (!proc) {
41 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020042 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +010043 if ((output != stderr) && (opt_p || opt_f)) {
Juan Cespedes273ea6d1998-03-14 23:02:40 +010044 current_column += fprintf(output, "%u ", proc->pid);
45 } else if (list_of_processes->next) {
46 current_column += fprintf(output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010047 }
Juan Cespedesf666d191998-09-20 23:04:34 +020048 if (opt_r) {
49 struct timeval tv;
50 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010051 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020052 struct timeval diff;
53
54 gettimeofday(&tv, &tz);
55
Ian Wienand2d45b1a2006-02-20 22:48:07 +010056 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
57 old_tv.tv_sec = tv.tv_sec;
58 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020059 }
60 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
61 if (tv.tv_usec >= old_tv.tv_usec) {
62 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
63 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020064 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020065 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
66 }
67 old_tv.tv_sec = tv.tv_sec;
68 old_tv.tv_usec = tv.tv_usec;
69 current_column += fprintf(output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010070 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020071 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020072 if (opt_t) {
73 struct timeval tv;
74 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020075
76 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 if (opt_t > 2) {
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020078 current_column += fprintf(output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 tv.tv_sec, (int)tv.tv_usec);
80 } else if (opt_t > 1) {
81 struct tm *tmp = localtime(&tv.tv_sec);
82 current_column +=
83 fprintf(output, "%02d:%02d:%02d.%06d ",
84 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
85 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020086 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020088 current_column += fprintf(output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 tmp->tm_hour, tmp->tm_min,
90 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020091 }
92 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010093 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020095 current_column += fprintf(output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010096 proc->return_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +010097 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020098 current_column += fprintf(output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100100 }
101 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100102 if (opt_n > 0 && type != LT_TOF_NONE) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +0200103 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200104 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200105}
106
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100107static struct function *name2func(char *name)
108{
109 struct function *tmp;
110 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100111
112 tmp = list_of_functions;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100113 while (tmp) {
Juan Cespedesd914a202004-11-10 00:15:33 +0100114#ifdef USE_DEMANGLE
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200115 str1 = opt_C ? my_demangle(tmp->name) : tmp->name;
116 str2 = opt_C ? my_demangle(name) : name;
117#else
118 str1 = tmp->name;
119 str2 = name;
120#endif
121 if (!strcmp(str1, str2)) {
122
Juan Cespedes5e01f651998-03-08 22:31:44 +0100123 return tmp;
124 }
125 tmp = tmp->next;
126 }
127 return NULL;
128}
129
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100130void output_line(struct process *proc, char *fmt, ...)
131{
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200132 va_list args;
133
Juan Cespedesd65efa32003-02-03 00:22:30 +0100134 if (opt_c) {
135 return;
136 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200137 if (current_proc) {
138 if (current_proc->callstack[current_depth].return_addr) {
139 fprintf(output, " <unfinished ...>\n");
140 } else {
141 fprintf(output, " <no return ...>\n");
142 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100143 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200144 current_proc = 0;
Juan Cespedes28f60191998-04-12 00:04:39 +0200145 if (!fmt) {
146 return;
147 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100148 begin_of_line(LT_TOF_NONE, proc);
149
Juan Cespedes21c63a12001-07-07 20:56:56 +0200150 va_start(args, fmt);
151 vfprintf(output, fmt, args);
152 fprintf(output, "\n");
153 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100154 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100155}
156
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100157static void tabto(int col)
158{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100159 if (current_column < col) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 fprintf(output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100161 }
162}
163
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100164void output_left(enum tof type, struct process *proc, char *function_name)
165{
166 struct function *func;
Steve Finkb0315a02006-08-07 04:22:06 +0200167 static arg_type_info *arg_unknown = NULL;
168 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200169 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100170
Juan Cespedesd65efa32003-02-03 00:22:30 +0100171 if (opt_c) {
172 return;
173 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200174 if (current_proc) {
Juan Cespedes21c63a12001-07-07 20:56:56 +0200175 fprintf(output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200176 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100177 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100178 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200179 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100180 current_depth = proc->callstack_depth;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100181 proc->type_being_displayed = type;
182 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100183#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100184 current_column +=
185 fprintf(output, "%s(",
186 opt_C ? my_demangle(function_name) : function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200187#else
Juan Cespedes5e01f651998-03-08 22:31:44 +0100188 current_column += fprintf(output, "%s(", function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200189#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190
191 func = name2func(function_name);
192 if (!func) {
193 int i;
Steve Fink65b53df2006-09-25 02:27:08 +0200194 arg_type_info info = *arg_unknown;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100195 for (i = 0; i < 4; i++) {
Steve Fink65b53df2006-09-25 02:27:08 +0200196 info.arg_num = i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 current_column +=
Steve Fink65b53df2006-09-25 02:27:08 +0200198 display_arg(type, proc, &info);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 current_column += fprintf(output, ", ");
200 }
Steve Fink65b53df2006-09-25 02:27:08 +0200201 info.arg_num = 4;
202 current_column += display_arg(type, proc, &info);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100203 return;
204 } else {
205 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100206 for (i = 0; i < func->num_params - func->params_right - 1; i++) {
207 current_column +=
Steve Fink65b53df2006-09-25 02:27:08 +0200208 display_arg(type, proc, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100209 current_column += fprintf(output, ", ");
210 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100211 if (func->num_params > func->params_right) {
212 current_column +=
Steve Fink65b53df2006-09-25 02:27:08 +0200213 display_arg(type, proc, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100214 if (func->params_right) {
215 current_column += fprintf(output, ", ");
216 }
217 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200218 if (func->params_right) {
219 save_register_args(type, proc);
220 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100221 }
222}
223
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224void output_right(enum tof type, struct process *proc, char *function_name)
225{
226 struct function *func = name2func(function_name);
Steve Finkb0315a02006-08-07 04:22:06 +0200227 static arg_type_info *arg_unknown = NULL;
228 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200229 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100230
Juan Cespedesd65efa32003-02-03 00:22:30 +0100231 if (opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100232 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100233 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100234 dict_opt_c =
235 dict_init(dict_key2hash_string,
236 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100237 }
238 st = dict_find_entry(dict_opt_c, function_name);
239 if (!st) {
240 char *na;
241 st = malloc(sizeof(struct opt_c_struct));
242 na = strdup(function_name);
243 if (!st || !na) {
244 perror("malloc()");
245 exit(1);
246 }
247 st->count = 0;
248 st->tv.tv_sec = st->tv.tv_usec = 0;
249 dict_enter(dict_opt_c, na, st);
250 }
251 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
252 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
253 st->tv.tv_sec++;
254 } else {
255 st->tv.tv_usec += current_time_spent.tv_usec;
256 }
257 st->count++;
258 st->tv.tv_sec += current_time_spent.tv_sec;
259
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100260// fprintf(output, "%s <%lu.%06d>\n", function_name,
261// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100262 return;
263 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200264 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100265 current_depth != proc->callstack_depth)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100266 fprintf(output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200267 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200268 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200269 if (current_proc != proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100271#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100272 current_column +=
273 fprintf(output, "<... %s resumed> ",
274 opt_C ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200275#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100276 current_column +=
277 fprintf(output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200278#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100279 }
280
281 if (!func) {
Steve Fink65b53df2006-09-25 02:27:08 +0200282 arg_type_info info = *arg_unknown;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100283 current_column += fprintf(output, ") ");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100284 tabto(opt_a - 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100285 fprintf(output, "= ");
Steve Fink65b53df2006-09-25 02:27:08 +0200286 info.arg_num = -1;
287 display_arg(type, proc, &info);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100288 } else {
289 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100290 for (i = func->num_params - func->params_right;
291 i < func->num_params - 1; i++) {
292 current_column +=
Steve Fink65b53df2006-09-25 02:27:08 +0200293 display_arg(type, proc, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100294 current_column += fprintf(output, ", ");
295 }
296 if (func->params_right) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100297 current_column +=
Steve Fink65b53df2006-09-25 02:27:08 +0200298 display_arg(type, proc, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100299 }
300 current_column += fprintf(output, ") ");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100301 tabto(opt_a - 1);
302 fprintf(output, "= ");
Steve Finkb0315a02006-08-07 04:22:06 +0200303 if (func->return_info->type == ARGTYPE_VOID) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100304 fprintf(output, "<void>");
305 } else {
Steve Fink65b53df2006-09-25 02:27:08 +0200306 display_arg(type, proc, func->return_info);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100307 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200308 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100309 if (opt_T) {
310 fprintf(output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100311 current_time_spent.tv_sec,
312 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100313 }
314 fprintf(output, "\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200315 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100317}