blob: d529f6be935ce2bbf3e5fccf2454f4be351daecd [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"
Petr Machata366c2f42012-02-09 19:34:36 +010012#include "proc.h"
Juan Cespedesac3db291998-04-25 14:31:58 +020013
Juan Cespedesf7281232009-06-25 16:11:21 +020014/* TODO FIXME XXX: include in common.h: */
Juan Cespedesd65efa32003-02-03 00:22:30 +010015extern struct timeval current_time_spent;
16
Juan Cespedes8d1b92b2009-07-03 10:39:34 +020017Dict *dict_opt_c = NULL;
Juan Cespedesd65efa32003-02-03 00:22:30 +010018
Juan Cespedesa8909f72009-04-28 20:02:41 +020019static Process *current_proc = 0;
Juan Cespedes5916fda2002-02-25 00:19:21 +010020static int current_depth = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +010021static int current_column = 0;
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020022
Juan Cespedesf1350522008-12-16 18:19:58 +010023static void
Juan Cespedesa8909f72009-04-28 20:02:41 +020024output_indent(Process *proc) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010025 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +010026 fprintf(options.output, "%*s", options.indent * proc->callstack_depth, "");
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020027}
Juan Cespedes5e4455b1997-08-24 01:48:26 +020028
Juan Cespedesf1350522008-12-16 18:19:58 +010029static void
Juan Cespedesa8909f72009-04-28 20:02:41 +020030begin_of_line(enum tof type, Process *proc) {
Juan Cespedes5e01f651998-03-08 22:31:44 +010031 current_column = 0;
32 if (!proc) {
33 return;
Juan Cespedes5e4455b1997-08-24 01:48:26 +020034 }
Juan Cespedesc693f022009-05-21 18:59:41 +020035 if ((options.output != stderr) && (opt_p || options.follow)) {
36 current_column += fprintf(options.output, "%u ", proc->pid);
Juan Cespedese12df4c2009-05-28 19:06:35 +020037 } else if (options.follow) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010038 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
Juan Cespedes5e01f651998-03-08 22:31:44 +010039 }
Juan Cespedesf666d191998-09-20 23:04:34 +020040 if (opt_r) {
41 struct timeval tv;
42 struct timezone tz;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010043 static struct timeval old_tv = { 0, 0 };
Juan Cespedesf666d191998-09-20 23:04:34 +020044 struct timeval diff;
45
46 gettimeofday(&tv, &tz);
47
Ian Wienand2d45b1a2006-02-20 22:48:07 +010048 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
49 old_tv.tv_sec = tv.tv_sec;
50 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesf666d191998-09-20 23:04:34 +020051 }
52 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
53 if (tv.tv_usec >= old_tv.tv_usec) {
54 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
55 } else {
Juan Cespedes5c3fe062004-06-14 18:08:37 +020056 diff.tv_sec--;
Juan Cespedesf666d191998-09-20 23:04:34 +020057 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
58 }
59 old_tv.tv_sec = tv.tv_sec;
60 old_tv.tv_usec = tv.tv_usec;
Juan Cespedesb65bdc52008-12-16 19:50:16 +010061 current_column += fprintf(options.output, "%3lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010062 diff.tv_sec, (int)diff.tv_usec);
Juan Cespedesf666d191998-09-20 23:04:34 +020063 }
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020064 if (opt_t) {
65 struct timeval tv;
66 struct timezone tz;
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020067
68 gettimeofday(&tv, &tz);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010069 if (opt_t > 2) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010070 current_column += fprintf(options.output, "%lu.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010071 tv.tv_sec, (int)tv.tv_usec);
72 } else if (opt_t > 1) {
73 struct tm *tmp = localtime(&tv.tv_sec);
74 current_column +=
Juan Cespedesb65bdc52008-12-16 19:50:16 +010075 fprintf(options.output, "%02d:%02d:%02d.%06d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010076 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
77 (int)tv.tv_usec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020078 } else {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 struct tm *tmp = localtime(&tv.tv_sec);
Juan Cespedesb65bdc52008-12-16 19:50:16 +010080 current_column += fprintf(options.output, "%02d:%02d:%02d ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081 tmp->tm_hour, tmp->tm_min,
82 tmp->tm_sec);
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020083 }
84 }
Juan Cespedes5e01f651998-03-08 22:31:44 +010085 if (opt_i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010086 if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010087 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 proc->return_addr);
Juan Cespedes5e01f651998-03-08 22:31:44 +010089 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +010090 current_column += fprintf(options.output, "[%p] ",
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 proc->instruction_pointer);
Juan Cespedes5e01f651998-03-08 22:31:44 +010092 }
93 }
Juan Cespedesb65bdc52008-12-16 19:50:16 +010094 if (options.indent > 0 && type != LT_TOF_NONE) {
Juan Cespedes3f0b62e2001-07-09 01:02:52 +020095 output_indent(proc);
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +020096 }
Juan Cespedes5e4455b1997-08-24 01:48:26 +020097}
98
Juan Cespedescde58262009-05-07 11:09:00 +020099static Function *
Petr Machata9a7f2322011-07-08 19:00:37 +0200100name2func(char const *name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200101 Function *tmp;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100102 const char *str1, *str2;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100103
104 tmp = list_of_functions;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100105 while (tmp) {
Juan Cespedesd914a202004-11-10 00:15:33 +0100106#ifdef USE_DEMANGLE
Juan Cespedesce377d52008-12-16 19:38:10 +0100107 str1 = options.demangle ? my_demangle(tmp->name) : tmp->name;
108 str2 = options.demangle ? my_demangle(name) : name;
Juan Cespedes1b9cfd61999-08-30 19:34:50 +0200109#else
110 str1 = tmp->name;
111 str2 = name;
112#endif
113 if (!strcmp(str1, str2)) {
114
Juan Cespedes5e01f651998-03-08 22:31:44 +0100115 return tmp;
116 }
117 tmp = tmp->next;
118 }
119 return NULL;
120}
121
Juan Cespedesf1350522008-12-16 18:19:58 +0100122void
Juan Cespedesa8909f72009-04-28 20:02:41 +0200123output_line(Process *proc, char *fmt, ...) {
Juan Cespedes5e4455b1997-08-24 01:48:26 +0200124 va_list args;
125
Juan Cespedesda9b9532009-04-07 15:33:50 +0200126 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100127 return;
128 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200129 if (current_proc) {
130 if (current_proc->callstack[current_depth].return_addr) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100131 fprintf(options.output, " <unfinished ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200132 } else {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100133 fprintf(options.output, " <no return ...>\n");
Paul Gilliam76c61f12006-06-14 06:55:21 +0200134 }
Juan Cespedes5e01f651998-03-08 22:31:44 +0100135 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200136 current_proc = 0;
Juan Cespedes28f60191998-04-12 00:04:39 +0200137 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);
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100143 vfprintf(options.output, fmt, args);
144 fprintf(options.output, "\n");
Juan Cespedes21c63a12001-07-07 20:56:56 +0200145 va_end(args);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100146 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100147}
148
Juan Cespedesf1350522008-12-16 18:19:58 +0100149static void
150tabto(int col) {
Juan Cespedes5e01f651998-03-08 22:31:44 +0100151 if (current_column < col) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100152 fprintf(options.output, "%*s", col - current_column, "");
Juan Cespedes5e01f651998-03-08 22:31:44 +0100153 }
154}
155
Juan Cespedesf1350522008-12-16 18:19:58 +0100156void
Petr Machata9a7f2322011-07-08 19:00:37 +0200157output_left(enum tof type, Process *proc, char const *function_name) {
Juan Cespedescde58262009-05-07 11:09:00 +0200158 Function *func;
Steve Finkb0315a02006-08-07 04:22:06 +0200159 static arg_type_info *arg_unknown = NULL;
160 if (arg_unknown == NULL)
Steve Fink65b53df2006-09-25 02:27:08 +0200161 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100162
Juan Cespedesda9b9532009-04-07 15:33:50 +0200163 if (options.summary) {
Juan Cespedesd65efa32003-02-03 00:22:30 +0100164 return;
165 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200166 if (current_proc) {
Juan Cespedesb65bdc52008-12-16 19:50:16 +0100167 fprintf(options.output, " <unfinished ...>\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100168 current_column = 0;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100169 }
Paul Gilliam76c61f12006-06-14 06:55:21 +0200170 current_proc = proc;
Juan Cespedes5916fda2002-02-25 00:19:21 +0100171 current_depth = proc->callstack_depth;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100172 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
Petr Machata14184b32012-02-10 13:10:26 +0100214output_right(enum tof type, Process *proc, const char *function_name)
215{
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");
Joe Damatoab3b72c2010-10-31 00:21:53 -0700303
304#if defined(HAVE_LIBUNWIND)
305 if (options.bt_depth > 0) {
306 unw_cursor_t cursor;
307 unw_word_t ip, sp;
308 int unwind_depth = options.bt_depth;
309 char fn_name[100];
310
311 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
312 while (unwind_depth) {
313 unw_get_reg(&cursor, UNW_REG_IP, &ip);
314 unw_get_reg(&cursor, UNW_REG_SP, &sp);
315 unw_get_proc_name(&cursor, fn_name, 100, NULL);
316 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
317 if (unw_step(&cursor) <= 0)
318 break;
319 unwind_depth--;
320 }
321 fprintf(options.output, "\n");
322 }
323#endif /* defined(HAVE_LIBUNWIND) */
324
Paul Gilliam76c61f12006-06-14 06:55:21 +0200325 current_proc = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100326 current_column = 0;
Juan Cespedesc40e64a1997-10-26 20:34:00 +0100327}