blob: 41ad9c63f311b54c9c9ad772289122cd4802784d [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
Juan Cespedesf1350522008-12-16 18:19:58 +010031static void
32output_indent(struct process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010033 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +010034 fprintf(options.output, "%*s", options.indent * proc->callstack_depth, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020035}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020036
Juan Cespedesf1350522008-12-16 18:19:58 +010037static void
38begin_of_line(enum tof type, struct process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010039 current_column = 0;
40 if (!proc) {
41 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020042 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +010043 if ((options.output != stderr) && (opt_p || opt_f)) {
44 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010045 } else if (list_of_processes->next) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010046 current_column += fprintf(options.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;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010069 current_column += fprintf(options.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 Cespedesb65bdc52008-12-16 19:50:16 +010078 current_column += fprintf(options.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 +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +010083 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 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 Cespedesb65bdc52008-12-16 19:50:16 +010088 current_column += fprintf(options.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 Cespedesb65bdc52008-12-16 19:50:16 +010095 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010096 proc->return_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +010097 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010098 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010099 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100100 }
101 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100102 if (options.indent > 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
Juan Cespedesf1350522008-12-16 18:19:58 +0100107static struct function *
108name2func(char *name) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 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 Cespedesce377d52008-12-16 19:38:10 +0100115 str1 = options.demangle ? my_demangle(tmp->name) : tmp->name;
116 str2 = options.demangle ? my_demangle(name) : name;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200117#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
Juan Cespedesf1350522008-12-16 18:19:58 +0100130void
131output_line(struct process *proc, char *fmt, ...) {
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) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100139 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200140 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100141 fprintf(options.output, " <no return ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200142 }
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);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100151 vfprintf(options.output, fmt, args);
152 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200153 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100154 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100155}
156
Juan Cespedesf1350522008-12-16 18:19:58 +0100157static void
158tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100159 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100160 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100161 }
162}
163
Juan Cespedesf1350522008-12-16 18:19:58 +0100164void
165output_left(enum tof type, struct process *proc, char *function_name) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100166 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 Cespedesb65bdc52008-12-16 19:50:16 +0100175 fprintf(options.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 +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100185 fprintf(options.output, "%s(",
Juan Cespedesce377d52008-12-16 19:38:10 +0100186 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200187#else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100188 current_column += fprintf(options.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;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100194 for (i = 0; i < 4; i++) {
195 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200196 display_arg(type, proc, i, arg_unknown);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100197 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100198 }
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200199 current_column += display_arg(type, proc, 4, arg_unknown);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100200 return;
201 } else {
202 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100203 for (i = 0; i < func->num_params - func->params_right - 1; i++) {
204 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200205 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100206 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100207 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100208 if (func->num_params > func->params_right) {
209 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200210 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100211 if (func->params_right) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100212 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100213 }
214 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200215 if (func->params_right) {
216 save_register_args(type, proc);
217 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100218 }
219}
220
Juan Cespedesf1350522008-12-16 18:19:58 +0100221void
222output_right(enum tof type, struct process *proc, char *function_name) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100223 struct function *func = name2func(function_name);
Steve Finkb0315a02006-08-07 04:22:06 +0200224 static arg_type_info *arg_unknown = NULL;
225 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200226 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100227
Juan Cespedesd65efa32003-02-03 00:22:30 +0100228 if (opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100229 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100230 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 dict_opt_c =
232 dict_init(dict_key2hash_string,
233 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100234 }
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
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100257// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100259 return;
260 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200261 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100262 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100263 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200264 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200265 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200266 if (current_proc != proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100267 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100268#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100269 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100270 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100271 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200272#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100273 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100274 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200275#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100276 }
277
278 if (!func) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100279 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100280 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100281 fprintf(options.output, "= ");
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200282 display_arg(type, proc, -1, arg_unknown);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100283 } else {
284 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100285 for (i = func->num_params - func->params_right;
286 i < func->num_params - 1; i++) {
287 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200288 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100289 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100290 }
291 if (func->params_right) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100292 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200293 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100294 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100295 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100296 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100297 fprintf(options.output, "= ");
Steve Finkb0315a02006-08-07 04:22:06 +0200298 if (func->return_info->type == ARGTYPE_VOID) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100299 fprintf(options.output, "<void>");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100300 } else {
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200301 display_arg(type, proc, -1, func->return_info);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100302 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200303 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100304 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100305 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 current_time_spent.tv_sec,
307 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100308 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100309 fprintf(options.output, "\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200310 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100311 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100312}