output.c revision 8d1b92ba755f6d6229f5e230fc43d958b13836f8
1#if HAVE_CONFIG_H 2#include "config.h" 3#endif 4 5#include <stdio.h> 6#include <stdlib.h> 7#include <stdarg.h> 8#include <string.h> 9#include <time.h> 10#include <sys/time.h> 11#include <unistd.h> 12 13#include "common.h" 14 15/* TODO FIXME XXX: include in common.h: */ 16extern struct timeval current_time_spent; 17 18Dict *dict_opt_c = NULL; 19 20static Process *current_proc = 0; 21static int current_depth = 0; 22static int current_column = 0; 23 24static void 25output_indent(Process *proc) { 26 current_column += 27 fprintf(options.output, "%*s", options.indent * proc->callstack_depth, ""); 28} 29 30static void 31begin_of_line(enum tof type, Process *proc) { 32 current_column = 0; 33 if (!proc) { 34 return; 35 } 36 if ((options.output != stderr) && (opt_p || options.follow)) { 37 current_column += fprintf(options.output, "%u ", proc->pid); 38 } else if (options.follow) { 39 current_column += fprintf(options.output, "[pid %u] ", proc->pid); 40 } 41 if (opt_r) { 42 struct timeval tv; 43 struct timezone tz; 44 static struct timeval old_tv = { 0, 0 }; 45 struct timeval diff; 46 47 gettimeofday(&tv, &tz); 48 49 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; 52 } 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 { 57 diff.tv_sec--; 58 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; 62 current_column += fprintf(options.output, "%3lu.%06d ", 63 diff.tv_sec, (int)diff.tv_usec); 64 } 65 if (opt_t) { 66 struct timeval tv; 67 struct timezone tz; 68 69 gettimeofday(&tv, &tz); 70 if (opt_t > 2) { 71 current_column += fprintf(options.output, "%lu.%06d ", 72 tv.tv_sec, (int)tv.tv_usec); 73 } else if (opt_t > 1) { 74 struct tm *tmp = localtime(&tv.tv_sec); 75 current_column += 76 fprintf(options.output, "%02d:%02d:%02d.%06d ", 77 tmp->tm_hour, tmp->tm_min, tmp->tm_sec, 78 (int)tv.tv_usec); 79 } else { 80 struct tm *tmp = localtime(&tv.tv_sec); 81 current_column += fprintf(options.output, "%02d:%02d:%02d ", 82 tmp->tm_hour, tmp->tm_min, 83 tmp->tm_sec); 84 } 85 } 86 if (opt_i) { 87 if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) { 88 current_column += fprintf(options.output, "[%p] ", 89 proc->return_addr); 90 } else { 91 current_column += fprintf(options.output, "[%p] ", 92 proc->instruction_pointer); 93 } 94 } 95 if (options.indent > 0 && type != LT_TOF_NONE) { 96 output_indent(proc); 97 } 98} 99 100static Function * 101name2func(char *name) { 102 Function *tmp; 103 const char *str1, *str2; 104 105 tmp = list_of_functions; 106 while (tmp) { 107#ifdef USE_DEMANGLE 108 str1 = options.demangle ? my_demangle(tmp->name) : tmp->name; 109 str2 = options.demangle ? my_demangle(name) : name; 110#else 111 str1 = tmp->name; 112 str2 = name; 113#endif 114 if (!strcmp(str1, str2)) { 115 116 return tmp; 117 } 118 tmp = tmp->next; 119 } 120 return NULL; 121} 122 123void 124output_line(Process *proc, char *fmt, ...) { 125 va_list args; 126 127 if (options.summary) { 128 return; 129 } 130 if (current_proc) { 131 if (current_proc->callstack[current_depth].return_addr) { 132 fprintf(options.output, " <unfinished ...>\n"); 133 } else { 134 fprintf(options.output, " <no return ...>\n"); 135 } 136 } 137 current_proc = 0; 138 if (!fmt) { 139 return; 140 } 141 begin_of_line(LT_TOF_NONE, proc); 142 143 va_start(args, fmt); 144 vfprintf(options.output, fmt, args); 145 fprintf(options.output, "\n"); 146 va_end(args); 147 current_column = 0; 148} 149 150static void 151tabto(int col) { 152 if (current_column < col) { 153 fprintf(options.output, "%*s", col - current_column, ""); 154 } 155} 156 157void 158output_left(enum tof type, Process *proc, char *function_name) { 159 Function *func; 160 static arg_type_info *arg_unknown = NULL; 161 if (arg_unknown == NULL) 162 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN); 163 164 if (options.summary) { 165 return; 166 } 167 if (current_proc) { 168 fprintf(options.output, " <unfinished ...>\n"); 169 current_proc = 0; 170 current_column = 0; 171 } 172 current_proc = proc; 173 current_depth = proc->callstack_depth; 174 proc->type_being_displayed = type; 175 begin_of_line(type, proc); 176#ifdef USE_DEMANGLE 177 current_column += 178 fprintf(options.output, "%s(", 179 options.demangle ? my_demangle(function_name) : function_name); 180#else 181 current_column += fprintf(options.output, "%s(", function_name); 182#endif 183 184 func = name2func(function_name); 185 if (!func) { 186 int i; 187 for (i = 0; i < 4; i++) { 188 current_column += 189 display_arg(type, proc, i, arg_unknown); 190 current_column += fprintf(options.output, ", "); 191 } 192 current_column += display_arg(type, proc, 4, arg_unknown); 193 return; 194 } else { 195 int i; 196 for (i = 0; i < func->num_params - func->params_right - 1; i++) { 197 current_column += 198 display_arg(type, proc, i, func->arg_info[i]); 199 current_column += fprintf(options.output, ", "); 200 } 201 if (func->num_params > func->params_right) { 202 current_column += 203 display_arg(type, proc, i, func->arg_info[i]); 204 if (func->params_right) { 205 current_column += fprintf(options.output, ", "); 206 } 207 } 208 if (func->params_right) { 209 save_register_args(type, proc); 210 } 211 } 212} 213 214void 215output_right(enum tof type, Process *proc, char *function_name) { 216 Function *func = name2func(function_name); 217 static arg_type_info *arg_unknown = NULL; 218 if (arg_unknown == NULL) 219 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN); 220 221 if (options.summary) { 222 struct opt_c_struct *st; 223 if (!dict_opt_c) { 224 dict_opt_c = 225 dict_init(dict_key2hash_string, 226 dict_key_cmp_string); 227 } 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 250// fprintf(options.output, "%s <%lu.%06d>\n", function_name, 251// current_time_spent.tv_sec, (int)current_time_spent.tv_usec); 252 return; 253 } 254 if (current_proc && (current_proc != proc || 255 current_depth != proc->callstack_depth)) { 256 fprintf(options.output, " <unfinished ...>\n"); 257 current_proc = 0; 258 } 259 if (current_proc != proc) { 260 begin_of_line(type, proc); 261#ifdef USE_DEMANGLE 262 current_column += 263 fprintf(options.output, "<... %s resumed> ", 264 options.demangle ? my_demangle(function_name) : function_name); 265#else 266 current_column += 267 fprintf(options.output, "<... %s resumed> ", function_name); 268#endif 269 } 270 271 if (!func) { 272 current_column += fprintf(options.output, ") "); 273 tabto(options.align - 1); 274 fprintf(options.output, "= "); 275 display_arg(type, proc, -1, arg_unknown); 276 } else { 277 int i; 278 for (i = func->num_params - func->params_right; 279 i < func->num_params - 1; i++) { 280 current_column += 281 display_arg(type, proc, i, func->arg_info[i]); 282 current_column += fprintf(options.output, ", "); 283 } 284 if (func->params_right) { 285 current_column += 286 display_arg(type, proc, i, func->arg_info[i]); 287 } 288 current_column += fprintf(options.output, ") "); 289 tabto(options.align - 1); 290 fprintf(options.output, "= "); 291 if (func->return_info->type == ARGTYPE_VOID) { 292 fprintf(options.output, "<void>"); 293 } else { 294 display_arg(type, proc, -1, func->return_info); 295 } 296 } 297 if (opt_T) { 298 fprintf(options.output, " <%lu.%06d>", 299 current_time_spent.tv_sec, 300 (int)current_time_spent.tv_usec); 301 } 302 fprintf(options.output, "\n"); 303 current_proc = 0; 304 current_column = 0; 305} 306