blob: 109a201b4072077eee256db5f773ccfbc9122909 [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
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 {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020063 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020064 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) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020090 if (type==LT_TOF_FUNCTION || type==LT_TOF_FUNCTIONR) {
91 current_column += fprintf(output, "[%p] ",
92 proc->return_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +010093 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020094 current_column += fprintf(output, "[%p] ",
95 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +010096 }
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 Cespedesd914a202004-11-10 00:15:33 +0100110#ifdef USE_DEMANGLE
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200111 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 Cespedesd914a202004-11-10 00:15:33 +0100172#ifdef USE_DEMANGLE
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 Cespedes5c3fe062004-06-14 18:08:37 +0200199 if (func->params_right) {
200 save_register_args(type, proc);
201 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100202 }
203}
204
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100205void
206output_right(enum tof type, struct process * proc, char * function_name) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100207 struct function * func = name2func(function_name);
208
Juan Cespedesd65efa32003-02-03 00:22:30 +0100209 if (opt_c) {
210 struct opt_c_struct * st;
211 if (!dict_opt_c) {
212 dict_opt_c = dict_init(dict_key2hash_string, dict_key_cmp_string);
213 }
214 st = dict_find_entry(dict_opt_c, function_name);
215 if (!st) {
216 char *na;
217 st = malloc(sizeof(struct opt_c_struct));
218 na = strdup(function_name);
219 if (!st || !na) {
220 perror("malloc()");
221 exit(1);
222 }
223 st->count = 0;
224 st->tv.tv_sec = st->tv.tv_usec = 0;
225 dict_enter(dict_opt_c, na, st);
226 }
227 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
228 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
229 st->tv.tv_sec++;
230 } else {
231 st->tv.tv_usec += current_time_spent.tv_usec;
232 }
233 st->count++;
234 st->tv.tv_sec += current_time_spent.tv_sec;
235
236// fprintf(output, "%s <%lu.%06d>\n", function_name,
237// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
238 return;
239 }
Juan Cespedes6ff816f2002-03-01 21:05:39 +0100240 if (current_pid && (current_pid!=proc->pid ||
241 current_depth != proc->callstack_depth)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100242 fprintf(output, " <unfinished ...>\n");
Juan Cespedes5916fda2002-02-25 00:19:21 +0100243 current_pid = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200244 }
245 if (current_pid != proc->pid) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100246 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100247#ifdef USE_DEMANGLE
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200248 current_column += fprintf(output, "<... %s resumed> ", opt_C ? my_demangle(function_name) : function_name);
249#else
Juan Cespedes5e01f651998-03-08 22:31:44 +0100250 current_column += fprintf(output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200251#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100252 }
253
254 if (!func) {
255 current_column += fprintf(output, ") ");
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100256 tabto(opt_a-1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100257 fprintf(output, "= ");
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200258 display_arg(type, proc, -1, ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100259 } else {
260 int i;
261 for(i=func->num_params-func->params_right; i<func->num_params-1; i++) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200262 current_column += display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100263 current_column += fprintf(output, ", ");
264 }
265 if (func->params_right) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200266 current_column += display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100267 }
268 current_column += fprintf(output, ") ");
Juan Cespedesa0ccf392003-02-01 19:02:37 +0100269 tabto(opt_a-1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100270 fprintf(output, "= ");
Juan Cespedese3eb9aa1999-04-03 03:21:52 +0200271 if (func->return_type == ARGTYPE_VOID) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100272 fprintf(output, "<void>");
273 } else {
274 display_arg(type, proc, -1, func->return_type);
275 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200276 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100277 if (opt_T) {
278 fprintf(output, " <%lu.%06d>",
279 current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
280 }
281 fprintf(output, "\n");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100282 current_pid=0;
283 current_column=0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100284}