blob: 794239e88c5db28728f8c21898b5a2639ce17b81 [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
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
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 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100137 if (current_pid) {
138 fprintf(output, " <unfinished ...>\n");
139 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100140 current_pid = 0;
Juan Cespedes28f60191998-04-12 00:04:39 +0200141 if (!fmt) {
142 return;
143 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100144 begin_of_line(LT_TOF_NONE, proc);
145
Juan Cespedes21c63a12001-07-07 20:56:56 +0200146 va_start(args, fmt);
147 vfprintf(output, fmt, args);
148 fprintf(output, "\n");
149 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100150 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100151}
152
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100153static void tabto(int col)
154{
Juan Cespedes5e01f651998-03-08 22:31:44 +0100155 if (current_column < col) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100156 fprintf(output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100157 }
158}
159
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160void output_left(enum tof type, struct process *proc, char *function_name)
161{
162 struct function *func;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100163
Juan Cespedesd65efa32003-02-03 00:22:30 +0100164 if (opt_c) {
165 return;
166 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100167 if (current_pid) {
Juan Cespedes21c63a12001-07-07 20:56:56 +0200168 fprintf(output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100169 current_pid = 0;
170 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100171 }
Juan Cespedes5916fda2002-02-25 00:19:21 +0100172 current_pid = proc->pid;
173 current_depth = proc->callstack_depth;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100174 proc->type_being_displayed = type;
175 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100176#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100177 current_column +=
178 fprintf(output, "%s(",
179 opt_C ? my_demangle(function_name) : function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200180#else
Juan Cespedes5e01f651998-03-08 22:31:44 +0100181 current_column += fprintf(output, "%s(", function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200182#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100183
184 func = name2func(function_name);
185 if (!func) {
186 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100187 for (i = 0; i < 4; i++) {
188 current_column +=
189 display_arg(type, proc, i, ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 current_column += fprintf(output, ", ");
191 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100192 current_column += display_arg(type, proc, 4, ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100193 return;
194 } else {
195 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100196 for (i = 0; i < func->num_params - func->params_right - 1; i++) {
197 current_column +=
198 display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100199 current_column += fprintf(output, ", ");
200 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100201 if (func->num_params > func->params_right) {
202 current_column +=
203 display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204 if (func->params_right) {
205 current_column += fprintf(output, ", ");
206 }
207 }
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200208 if (func->params_right) {
209 save_register_args(type, proc);
210 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100211 }
212}
213
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100214void output_right(enum tof type, struct process *proc, char *function_name)
215{
216 struct function *func = name2func(function_name);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100217
Juan Cespedesd65efa32003-02-03 00:22:30 +0100218 if (opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100219 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100220 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221 dict_opt_c =
222 dict_init(dict_key2hash_string,
223 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100224 }
225 st = dict_find_entry(dict_opt_c, function_name);
226 if (!st) {
227 char *na;
228 st = malloc(sizeof(struct opt_c_struct));
229 na = strdup(function_name);
230 if (!st || !na) {
231 perror("malloc()");
232 exit(1);
233 }
234 st->count = 0;
235 st->tv.tv_sec = st->tv.tv_usec = 0;
236 dict_enter(dict_opt_c, na, st);
237 }
238 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
239 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
240 st->tv.tv_sec++;
241 } else {
242 st->tv.tv_usec += current_time_spent.tv_usec;
243 }
244 st->count++;
245 st->tv.tv_sec += current_time_spent.tv_sec;
246
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100247// fprintf(output, "%s <%lu.%06d>\n", function_name,
248// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100249 return;
250 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 if (current_pid && (current_pid != proc->pid ||
252 current_depth != proc->callstack_depth)) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100253 fprintf(output, " <unfinished ...>\n");
Juan Cespedes5916fda2002-02-25 00:19:21 +0100254 current_pid = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200255 }
256 if (current_pid != proc->pid) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100257 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100258#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100259 current_column +=
260 fprintf(output, "<... %s resumed> ",
261 opt_C ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200262#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100263 current_column +=
264 fprintf(output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200265#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100266 }
267
268 if (!func) {
269 current_column += fprintf(output, ") ");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100270 tabto(opt_a - 1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100271 fprintf(output, "= ");
Ian Wienand9a2ad352006-02-20 22:44:45 +0100272 display_arg(type, proc, -1, ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100273 } else {
274 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100275 for (i = func->num_params - func->params_right;
276 i < func->num_params - 1; i++) {
277 current_column +=
278 display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100279 current_column += fprintf(output, ", ");
280 }
281 if (func->params_right) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100282 current_column +=
283 display_arg(type, proc, i, func->arg_types[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100284 }
285 current_column += fprintf(output, ") ");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100286 tabto(opt_a - 1);
287 fprintf(output, "= ");
Ian Wienand9a2ad352006-02-20 22:44:45 +0100288 if (func->return_type == ARGTYPE_VOID) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100289 fprintf(output, "<void>");
290 } else {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100291 display_arg(type, proc, -1, func->return_type);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100292 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200293 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100294 if (opt_T) {
295 fprintf(output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100296 current_time_spent.tv_sec,
297 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100298 }
299 fprintf(output, "\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100300 current_pid = 0;
301 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100302}