blob: fa60741fa11bc57c465ed3b79ea9e0160daab4c6 [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#include "config.h"
Juan Cespedesac3db291998-04-25 14:31:58 +02002
Juan Cespedes3268a161997-08-25 16:45:22 +02003#include <stdio.h>
Juan Cespedesd65efa32003-02-03 00:22:30 +01004#include <stdlib.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +02005#include <stdarg.h>
Juan Cespedes1cd999a2001-07-03 00:46:04 +02006#include <string.h>
Juan Cespedes5e0acdb1998-04-04 08:34:07 +02007#include <time.h>
8#include <sys/time.h>
9#include <unistd.h>
Juan Cespedes5e4455b1997-08-24 01:48:26 +020010
Juan Cespedesf7281232009-06-25 16:11:21 +020011#include "common.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020012
Juan Cespedesf7281232009-06-25 16:11:21 +020013/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010014extern struct timeval current_time_spent;
15
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020016Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010017
Juan Cespedesa8909f72009-04-28 20:02:41 +020018static Process *current_proc = 0;
Juan Cespedes5916fda2002-02-25 00:19:21 +010019static int current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010020static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020021
Juan Cespedesf1350522008-12-16 18:19:58 +010022static void
Juan Cespedesa8909f72009-04-28 20:02:41 +020023output_indent(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010024 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +010025 fprintf(options.output, "%*s", options.indent * proc->callstack_depth, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020026}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020027
Juan Cespedesf1350522008-12-16 18:19:58 +010028static void
Juan Cespedesa8909f72009-04-28 20:02:41 +020029begin_of_line(enum tof type, Process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010030 current_column = 0;
31 if (!proc) {
32 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020033 }
Juan Cespedesc693f022009-05-21 18:59:41 +020034 if ((options.output != stderr) && (opt_p || options.follow)) {
35 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020036 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010037 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010038 }
Juan Cespedesf666d191998-09-20 23:04:34 +020039 if (opt_r) {
40 struct timeval tv;
41 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020043 struct timeval diff;
44
45 gettimeofday(&tv, &tz);
46
Ian Wienand2d45b1a2006-02-20 22:48:07 +010047 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
48 old_tv.tv_sec = tv.tv_sec;
49 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020050 }
51 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
52 if (tv.tv_usec >= old_tv.tv_usec) {
53 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
54 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020055 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020056 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
57 }
58 old_tv.tv_sec = tv.tv_sec;
59 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010060 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010061 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020062 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020063 if (opt_t) {
64 struct timeval tv;
65 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020066
67 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010068 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010069 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010070 tv.tv_sec, (int)tv.tv_usec);
71 } else if (opt_t > 1) {
72 struct tm *tmp = localtime(&tv.tv_sec);
73 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +010074 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010075 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
76 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020077 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010078 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +010079 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 tmp->tm_hour, tmp->tm_min,
81 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020082 }
83 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010084 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010085 if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010086 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 proc->return_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +010088 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010089 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010090 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +010091 }
92 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +010093 if (options.indent > 0 && type != LT_TOF_NONE) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +020094 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020095 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +020096}
97
Juan Cespedescde58262009-05-07 11:09:00 +020098static Function *
Petr Machata9a7f2322011-07-08 19:00:37 +020099name2func(char const *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200100 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100102
103 tmp = list_of_functions;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104 while (tmp) {
Juan Cespedesd914a202004-11-10 00:15:33 +0100105#ifdef USE_DEMANGLE
Juan Cespedesce377d52008-12-16 19:38:10 +0100106 str1 = options.demangle ? my_demangle(tmp->name) : tmp->name;
107 str2 = options.demangle ? my_demangle(name) : name;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200108#else
109 str1 = tmp->name;
110 str2 = name;
111#endif
112 if (!strcmp(str1, str2)) {
113
Juan Cespedes5e01f651998-03-08 22:31:44 +0100114 return tmp;
115 }
116 tmp = tmp->next;
117 }
118 return NULL;
119}
120
Juan Cespedesf1350522008-12-16 18:19:58 +0100121void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200122output_line(Process *proc, char *fmt, ...) {
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200123 va_list args;
124
Juan Cespedesda9b9532009-04-07 15:33:50 +0200125 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100126 return;
127 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200128 if (current_proc) {
129 if (current_proc->callstack[current_depth].return_addr) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100130 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200131 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100132 fprintf(options.output, " <no return ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200133 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100134 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200135 current_proc = 0;
Juan Cespedes28f60191998-04-12 00:04:39 +0200136 if (!fmt) {
137 return;
138 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100139 begin_of_line(LT_TOF_NONE, proc);
140
Juan Cespedes21c63a12001-07-07 20:56:56 +0200141 va_start(args, fmt);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100142 vfprintf(options.output, fmt, args);
143 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200144 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100145 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100146}
147
Juan Cespedesf1350522008-12-16 18:19:58 +0100148static void
149tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100150 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100151 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100152 }
153}
154
Juan Cespedesf1350522008-12-16 18:19:58 +0100155void
Petr Machata9a7f2322011-07-08 19:00:37 +0200156output_left(enum tof type, Process *proc, char const *function_name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200157 Function *func;
Steve Finkb0315a02006-08-07 04:22:06 +0200158 static arg_type_info *arg_unknown = NULL;
159 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200160 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100161
Juan Cespedesda9b9532009-04-07 15:33:50 +0200162 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100163 return;
164 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200165 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100166 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100167 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100168 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200169 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100170 current_depth = proc->callstack_depth;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100171 proc->type_being_displayed = type;
172 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100173#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100174 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100175 fprintf(options.output, "%s(",
Juan Cespedesce377d52008-12-16 19:38:10 +0100176 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200177#else
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100178 current_column += fprintf(options.output, "%s(", function_name);
Juan Cespedesac3db291998-04-25 14:31:58 +0200179#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100180
181 func = name2func(function_name);
182 if (!func) {
183 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100184 for (i = 0; i < 4; i++) {
185 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200186 display_arg(type, proc, i, arg_unknown);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100187 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100188 }
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200189 current_column += display_arg(type, proc, 4, arg_unknown);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100190 return;
191 } else {
192 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 for (i = 0; i < func->num_params - func->params_right - 1; i++) {
194 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200195 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100196 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100197 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100198 if (func->num_params > func->params_right) {
199 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200200 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100201 if (func->params_right) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100202 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100203 }
204 }
Petr Machata1d310382011-08-08 17:38:18 +0200205 if (func->params_right
206 || func->return_info->type == ARGTYPE_STRING_N
207 || func->return_info->type == ARGTYPE_ARRAY) {
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200208 save_register_args(type, proc);
209 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100210 }
211}
212
Juan Cespedesf1350522008-12-16 18:19:58 +0100213void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200214output_right(enum tof type, Process *proc, char *function_name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200215 Function *func = name2func(function_name);
Steve Finkb0315a02006-08-07 04:22:06 +0200216 static arg_type_info *arg_unknown = NULL;
217 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200218 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100219
Juan Cespedesda9b9532009-04-07 15:33:50 +0200220 if (options.summary) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100221 struct opt_c_struct *st;
Juan Cespedesd65efa32003-02-03 00:22:30 +0100222 if (!dict_opt_c) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100223 dict_opt_c =
224 dict_init(dict_key2hash_string,
225 dict_key_cmp_string);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100226 }
227 st = dict_find_entry(dict_opt_c, function_name);
228 if (!st) {
229 char *na;
230 st = malloc(sizeof(struct opt_c_struct));
231 na = strdup(function_name);
232 if (!st || !na) {
233 perror("malloc()");
234 exit(1);
235 }
236 st->count = 0;
237 st->tv.tv_sec = st->tv.tv_usec = 0;
238 dict_enter(dict_opt_c, na, st);
239 }
240 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
241 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
242 st->tv.tv_sec++;
243 } else {
244 st->tv.tv_usec += current_time_spent.tv_usec;
245 }
246 st->count++;
247 st->tv.tv_sec += current_time_spent.tv_sec;
248
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100249// fprintf(options.output, "%s <%lu.%06d>\n", function_name,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100250// current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100251 return;
252 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200253 if (current_proc && (current_proc != proc ||
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 current_depth != proc->callstack_depth)) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100255 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200256 current_proc = 0;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200257 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200258 if (current_proc != proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100259 begin_of_line(type, proc);
Juan Cespedesd914a202004-11-10 00:15:33 +0100260#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100261 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100262 fprintf(options.output, "<... %s resumed> ",
Juan Cespedesce377d52008-12-16 19:38:10 +0100263 options.demangle ? my_demangle(function_name) : function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200264#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100265 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100266 fprintf(options.output, "<... %s resumed> ", function_name);
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200267#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100268 }
269
270 if (!func) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100271 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100272 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100273 fprintf(options.output, "= ");
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200274 display_arg(type, proc, -1, arg_unknown);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100275 } else {
276 int i;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100277 for (i = func->num_params - func->params_right;
278 i < func->num_params - 1; i++) {
279 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200280 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100281 current_column += fprintf(options.output, ", ");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100282 }
283 if (func->params_right) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100284 current_column +=
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200285 display_arg(type, proc, i, func->arg_info[i]);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100286 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100287 current_column += fprintf(options.output, ") ");
Juan Cespedesce377d52008-12-16 19:38:10 +0100288 tabto(options.align - 1);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100289 fprintf(options.output, "= ");
Steve Finkb0315a02006-08-07 04:22:06 +0200290 if (func->return_info->type == ARGTYPE_VOID) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100291 fprintf(options.output, "<void>");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100292 } else {
Juan Cespedesa413e5b2007-09-04 17:34:53 +0200293 display_arg(type, proc, -1, func->return_info);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100294 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200295 }
Juan Cespedesd65efa32003-02-03 00:22:30 +0100296 if (opt_T) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100297 fprintf(options.output, " <%lu.%06d>",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100298 current_time_spent.tv_sec,
299 (int)current_time_spent.tv_usec);
Juan Cespedesd65efa32003-02-03 00:22:30 +0100300 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100301 fprintf(options.output, "\n");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700302
303#if defined(HAVE_LIBUNWIND)
304 if (options.bt_depth > 0) {
305 unw_cursor_t cursor;
306 unw_word_t ip, sp;
307 int unwind_depth = options.bt_depth;
308 char fn_name[100];
309
310 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
311 while (unwind_depth) {
312 unw_get_reg(&cursor, UNW_REG_IP, &ip);
313 unw_get_reg(&cursor, UNW_REG_SP, &sp);
314 unw_get_proc_name(&cursor, fn_name, 100, NULL);
315 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
316 if (unw_step(&cursor) <= 0)
317 break;
318 unwind_depth--;
319 }
320 fprintf(options.output, "\n");
321 }
322#endif /* defined(HAVE_LIBUNWIND) */
323
Paul Gilliam76c61f12006-06-14 06:55:21 +0200324 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100325 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100326}