blob: 835cdd16ad732439f0d111391946aa1aa672e16c [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
Juan Cespedesf7281232009-06-25 16:11:21 +020013#include "common.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020014
Juan Cespedesf7281232009-06-25 16:11:21 +020015/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010016extern struct timeval current_time_spent;
17
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020018Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010019
Juan Cespedesa8909f72009-04-28 20:02:41 +020020static Process *current_proc = 0;
Juan Cespedes5916fda2002-02-25 00:19:21 +010021static int current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010022static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020023
Juan Cespedesf1350522008-12-16 18:19:58 +010024static void
Juan Cespedesa8909f72009-04-28 20:02:41 +020025output_indent(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010026 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +010027 fprintf(options.output, "%*s", options.indent * proc->callstack_depth, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020028}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020029
Juan Cespedesf1350522008-12-16 18:19:58 +010030static void
Juan Cespedesa8909f72009-04-28 20:02:41 +020031begin_of_line(enum tof type, Process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010032 current_column = 0;
33 if (!proc) {
34 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020035 }
Juan Cespedesc693f022009-05-21 18:59:41 +020036 if ((options.output != stderr) && (opt_p || options.follow)) {
37 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020038 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010039 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010040 }
Juan Cespedesf666d191998-09-20 23:04:34 +020041 if (opt_r) {
42 struct timeval tv;
43 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010044 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020045 struct timeval diff;
46
47 gettimeofday(&tv, &tz);
48
Ian Wienand2d45b1a2006-02-20 22:48:07 +010049 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
50 old_tv.tv_sec = tv.tv_sec;
51 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020052 }
53 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
54 if (tv.tv_usec >= old_tv.tv_usec) {
55 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
56 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020057 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020058 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
59 }
60 old_tv.tv_sec = tv.tv_sec;
61 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010062 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010063 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020064 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020065 if (opt_t) {
66 struct timeval tv;
67 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020068
69 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010070 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010071 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010072 tv.tv_sec, (int)tv.tv_usec);
73 } else if (opt_t > 1) {
74 struct tm *tmp = localtime(&tv.tv_sec);
75 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +010076 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
78 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020079 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +010081 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 tmp->tm_hour, tmp->tm_min,
83 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020084 }
85 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010086 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010088 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 proc->return_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +010090 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010091 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010092 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +010093 }
94 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +010095 if (options.indent > 0 && type != LT_TOF_NONE) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +020096 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020097 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +020098}
99
Juan Cespedescde58262009-05-07 11:09:00 +0200100static Function *
Juan Cespedesf1350522008-12-16 18:19:58 +0100101name2func(char *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200102 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100103 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100104
105 tmp = list_of_functions;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 while (tmp) {
Juan Cespedesd914a202004-11-10 00:15:33 +0100107#ifdef USE_DEMANGLE
Juan Cespedesce377d52008-12-16 19:38:10 +0100108 str1 = options.demangle ? my_demangle(tmp->name) : tmp->name;
109 str2 = options.demangle ? my_demangle(name) : name;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200110#else
111 str1 = tmp->name;
112 str2 = name;
113#endif
114 if (!strcmp(str1, str2)) {
115
Juan Cespedes5e01f651998-03-08 22:31:44 +0100116 return tmp;
117 }
118 tmp = tmp->next;
119 }
120 return NULL;
121}
122
Juan Cespedesf1350522008-12-16 18:19:58 +0100123void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200124output_line(Process *proc, char *fmt, ...) {
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200125 va_list args;
126
Juan Cespedesda9b9532009-04-07 15:33:50 +0200127 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100128 return;
129 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200130 if (current_proc) {
131 if (current_proc->callstack[current_depth].return_addr) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100132 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200133 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100134 fprintf(options.output, " <no return ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200135 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100136 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200137 current_proc = 0;
Juan Cespedes28f60191998-04-12 00:04:39 +0200138 if (!fmt) {
139 return;
140 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100141 begin_of_line(LT_TOF_NONE, proc);
142
Juan Cespedes21c63a12001-07-07 20:56:56 +0200143 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100144 vfprintf(options.output, fmt, args);
145 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200146 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100147 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100148}
149
Juan Cespedesf1350522008-12-16 18:19:58 +0100150static void
151tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100152 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100153 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100154 }
155}
156
Juan Cespedesf1350522008-12-16 18:19:58 +0100157void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200158output_left(enum tof type, Process *proc, char *function_name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200159 Function *func;
Steve Finkb0315a02006-08-07 04:22:06 +0200160 static arg_type_info *arg_unknown = NULL;
161 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200162 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100163
Juan Cespedesda9b9532009-04-07 15:33:50 +0200164 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100165 return;
166 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200167 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100168 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200169 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100170 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100171 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200172 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100173 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 +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100178 fprintf(options.output, "%s(",
Juan Cespedesce377d52008-12-16 19:38:10 +0100179 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200180#else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100181 current_column += fprintf(options.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 +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200189 display_arg(type, proc, i, arg_unknown);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100190 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100191 }
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200192 current_column += display_arg(type, proc, 4, arg_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 +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200198 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100199 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100200 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100201 if (func->num_params > func->params_right) {
202 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200203 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100204 if (func->params_right) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100205 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100206 }
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
Juan Cespedesf1350522008-12-16 18:19:58 +0100214void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200215output_right(enum tof type, Process *proc, char *function_name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200216 Function *func = name2func(function_name);
Steve Finkb0315a02006-08-07 04:22:06 +0200217 static arg_type_info *arg_unknown = NULL;
218 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200219 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100220
Juan Cespedesda9b9532009-04-07 15:33:50 +0200221 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100222 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100223 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100224 dict_opt_c =
225 dict_init(dict_key2hash_string,
226 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100227 }
228 st = dict_find_entry(dict_opt_c, function_name);
229 if (!st) {
230 char *na;
231 st = malloc(sizeof(struct opt_c_struct));
232 na = strdup(function_name);
233 if (!st || !na) {
234 perror("malloc()");
235 exit(1);
236 }
237 st->count = 0;
238 st->tv.tv_sec = st->tv.tv_usec = 0;
239 dict_enter(dict_opt_c, na, st);
240 }
241 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
242 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
243 st->tv.tv_sec++;
244 } else {
245 st->tv.tv_usec += current_time_spent.tv_usec;
246 }
247 st->count++;
248 st->tv.tv_sec += current_time_spent.tv_sec;
249
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100250// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100252 return;
253 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200254 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100255 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100256 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200257 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200258 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200259 if (current_proc != proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100260 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100261#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100262 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100263 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100264 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200265#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100266 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100267 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200268#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100269 }
270
271 if (!func) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100272 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100273 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100274 fprintf(options.output, "= ");
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200275 display_arg(type, proc, -1, arg_unknown);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100276 } else {
277 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100278 for (i = func->num_params - func->params_right;
279 i < func->num_params - 1; i++) {
280 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200281 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100282 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100283 }
284 if (func->params_right) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100285 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200286 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100287 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100288 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100289 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100290 fprintf(options.output, "= ");
Steve Finkb0315a02006-08-07 04:22:06 +0200291 if (func->return_info->type == ARGTYPE_VOID) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100292 fprintf(options.output, "<void>");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100293 } else {
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200294 display_arg(type, proc, -1, func->return_info);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100295 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200296 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100297 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100298 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100299 current_time_spent.tv_sec,
300 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100301 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100302 fprintf(options.output, "\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200303 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100304 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100305}