blob: c29e5cdcbdd05535faaaafde58ed8581d8e576f1 [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 Cespedesac3db291998-04-25 14:31:58 +020018#if HAVE_LIBIBERTY
19#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
25struct dict * dict_opt_c = NULL;
26
Juan Cespedes5e01f651998-03-08 22:31:44 +010027static pid_t current_pid = 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 Cespedes8cc1b9d2002-03-01 19:54:23 +010031static void
32output_indent(struct process * proc) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +020033 current_column += fprintf(output, "%*s", opt_n * proc->callstack_depth, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020034}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020035
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010036static void
37begin_of_line(enum tof type, struct process * proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010038 current_column = 0;
39 if (!proc) {
40 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020041 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010042 if ((output!=stderr) && (opt_p || opt_f)) {
43 current_column += fprintf(output, "%u ", proc->pid);
44 } else if (list_of_processes->next) {
45 current_column += fprintf(output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010046 }
Juan Cespedesf666d191998-09-20 23:04:34 +020047 if (opt_r) {
48 struct timeval tv;
49 struct timezone tz;
50 static struct timeval old_tv={0,0};
51 struct timeval diff;
52
53 gettimeofday(&tv, &tz);
54
55 if (old_tv.tv_sec==0 && old_tv.tv_usec==0) {
56 old_tv.tv_sec=tv.tv_sec;
57 old_tv.tv_usec=tv.tv_usec;
58 }
59 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
60 if (tv.tv_usec >= old_tv.tv_usec) {
61 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
62 } else {
63 diff.tv_sec++;
64 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
65 }
66 old_tv.tv_sec = tv.tv_sec;
67 old_tv.tv_usec = tv.tv_usec;
68 current_column += fprintf(output, "%3lu.%06d ",
69 diff.tv_sec, (int)diff.tv_usec);
70 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020071 if (opt_t) {
72 struct timeval tv;
73 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020074
75 gettimeofday(&tv, &tz);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020076 if (opt_t>2) {
77 current_column += fprintf(output, "%lu.%06d ",
78 tv.tv_sec, (int)tv.tv_usec);
79 } else if (opt_t>1) {
Juan Cespedesf666d191998-09-20 23:04:34 +020080 struct tm * tmp = localtime(&tv.tv_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020081 current_column += fprintf(output, "%02d:%02d:%02d.%06d ",
82 tmp->tm_hour, tmp->tm_min, tmp->tm_sec, (int)tv.tv_usec);
83 } else {
Juan Cespedesf666d191998-09-20 23:04:34 +020084 struct tm * tmp = localtime(&tv.tv_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020085 current_column += fprintf(output, "%02d:%02d:%02d ",
86 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
87 }
88 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010089 if (opt_i) {
90 if (type==LT_TOF_FUNCTION) {
91 current_column += fprintf(output, "[%08x] ",
92 (unsigned)proc->return_addr);
93 } else {
94 current_column += fprintf(output, "[%08x] ",
95 (unsigned)proc->instruction_pointer);
96 }
97 }
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +010098 if (opt_n > 0 && type!=LT_TOF_NONE) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +020099 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200100 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200101}
102
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100103static struct function *
104name2func(char * name) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100105 struct function * tmp;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200106 const char * str1, * str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100107
108 tmp = list_of_functions;
109 while(tmp) {
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200110#if HAVE_LIBIBERTY
111 str1 = opt_C ? my_demangle(tmp->name) : tmp->name;
112 str2 = opt_C ? my_demangle(name) : name;
113#else
114 str1 = tmp->name;
115 str2 = name;
116#endif
117 if (!strcmp(str1, str2)) {
118
Juan Cespedes5e01f651998-03-08 22:31:44 +0100119 return tmp;
120 }
121 tmp = tmp->next;
122 }
123 return NULL;
124}
125
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100126void
127output_line(struct process * proc, char *fmt, ...) {
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200128 va_list args;
129
Juan Cespedesd65efa32003-02-03 00:22:30 +0100130 if (opt_c) {
131 return;
132 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100133 if (current_pid) {
134 fprintf(output, " <unfinished ...>\n");
135 }
Juan Cespedes28f60191998-04-12 00:04:39 +0200136 current_pid=0;
137 if (!fmt) {
138 return;
139 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100140 begin_of_line(LT_TOF_NONE, proc);
141
Juan Cespedes21c63a12001-07-07 20:56:56 +0200142 va_start(args, fmt);
143 vfprintf(output, fmt, args);
144 fprintf(output, "\n");
145 va_end(args);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100146 current_column=0;
147}
148
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100149static void
150tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100151 if (current_column < col) {
152 fprintf(output, "%*s", col-current_column, "");
153 }
154}
155
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100156void
157output_left(enum tof type, struct process * proc, char * function_name) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100158 struct function * func;
159
Juan Cespedesd65efa32003-02-03 00:22:30 +0100160 if (opt_c) {
161 return;
162 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100163 if (current_pid) {
Juan Cespedes21c63a12001-07-07 20:56:56 +0200164 fprintf(output, " <unfinished ...>\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100165 current_pid=0;
166 current_column=0;
167 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100168 current_pid = proc->pid;
169 current_depth = proc->callstack_depth;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100170 proc->type_being_displayed = type;
171 begin_of_line(type, proc);
Juan Cespedesac3db291998-04-25 14:31:58 +0200172#if HAVE_LIBIBERTY
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200173 current_column += fprintf(output, "%s(", opt_C ? my_demangle(function_name): function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200174#else
Juan Cespedes5e01f651998-03-08 22:31:44 +0100175 current_column += fprintf(output, "%s(", function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200176#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100177
178 func = name2func(function_name);
179 if (!func) {
180 int i;
181 for(i=0; i<4; i++) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200182 current_column += display_arg(type, proc, i, ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100183 current_column += fprintf(output, ", ");
184 }
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200185 current_column += display_arg(type, proc, 4, ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100186 return;
187 } else {
188 int i;
189 for(i=0; i< func->num_params - func->params_right - 1; i++) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200190 current_column += display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100191 current_column += fprintf(output, ", ");
192 }
193 if (func->num_params>func->params_right) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200194 current_column += display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100195 if (func->params_right) {
196 current_column += fprintf(output, ", ");
197 }
198 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 }
200}
201
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100202void
203output_right(enum tof type, struct process * proc, char * function_name) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204 struct function * func = name2func(function_name);
205
Juan Cespedesd65efa32003-02-03 00:22:30 +0100206 if (opt_c) {
207 struct opt_c_struct * st;
208 if (!dict_opt_c) {
209 dict_opt_c = dict_init(dict_key2hash_string, dict_key_cmp_string);
210 }
211 st = dict_find_entry(dict_opt_c, function_name);
212 if (!st) {
213 char *na;
214 st = malloc(sizeof(struct opt_c_struct));
215 na = strdup(function_name);
216 if (!st || !na) {
217 perror("malloc()");
218 exit(1);
219 }
220 st->count = 0;
221 st->tv.tv_sec = st->tv.tv_usec = 0;
222 dict_enter(dict_opt_c, na, st);
223 }
224 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
225 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
226 st->tv.tv_sec++;
227 } else {
228 st->tv.tv_usec += current_time_spent.tv_usec;
229 }
230 st->count++;
231 st->tv.tv_sec += current_time_spent.tv_sec;
232
233// fprintf(output, "%s <%lu.%06d>\n", function_name,
234// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
235 return;
236 }
Juan Cespedes6ff816f2002-03-01 21:05:39 +0100237 if (current_pid && (current_pid!=proc->pid ||
238 current_depth != proc->callstack_depth)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100239 fprintf(output, " <unfinished ...>\n");
Juan Cespedes5916fda2002-02-25 00:19:21 +0100240 current_pid = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200241 }
242 if (current_pid != proc->pid) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100243 begin_of_line(type, proc);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200244#if HAVE_LIBIBERTY
245 current_column += fprintf(output, "<... %s resumed> ", opt_C ? my_demangle(function_name) : function_name);
246#else
Juan Cespedes5e01f651998-03-08 22:31:44 +0100247 current_column += fprintf(output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200248#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100249 }
250
251 if (!func) {
252 current_column += fprintf(output, ") ");
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100253 tabto(opt_a-1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100254 fprintf(output, "= ");
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200255 display_arg(type, proc, -1, ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100256 } else {
257 int i;
258 for(i=func->num_params-func->params_right; i<func->num_params-1; i++) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200259 current_column += display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100260 current_column += fprintf(output, ", ");
261 }
262 if (func->params_right) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200263 current_column += display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100264 }
265 current_column += fprintf(output, ") ");
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100266 tabto(opt_a-1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100267 fprintf(output, "= ");
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200268 if (func->return_type == ARGTYPE_VOID) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100269 fprintf(output, "<void>");
270 } else {
271 display_arg(type, proc, -1, func->return_type);
272 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200273 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100274 if (opt_T) {
275 fprintf(output, " <%lu.%06d>",
276 current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
277 }
278 fprintf(output, "\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100279 current_pid=0;
280 current_column=0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100281}