output.c revision 29add4fdf852b10ddd22cac0d1390f6d01577bc2
1#include "config.h" 2 3#include <stdio.h> 4#include <stdlib.h> 5#include <stdarg.h> 6#include <string.h> 7#include <time.h> 8#include <sys/time.h> 9#include <unistd.h> 10 11#include "common.h" 12#include "proc.h" 13#include "library.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 const *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, struct Process *proc, 159 struct library_symbol *libsym) 160{ 161 const char *function_name = libsym->name; 162 Function *func; 163 static arg_type_info *arg_unknown = NULL; 164 if (arg_unknown == NULL) 165 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN); 166 167 if (options.summary) { 168 return; 169 } 170 if (current_proc) { 171 fprintf(options.output, " <unfinished ...>\n"); 172 current_column = 0; 173 } 174 current_proc = proc; 175 current_depth = proc->callstack_depth; 176 begin_of_line(type, proc); 177#ifdef USE_DEMANGLE 178 current_column += 179 fprintf(options.output, "%s@%s(", 180 (options.demangle 181 ? my_demangle(function_name) : function_name), 182 libsym->lib->name); 183#else 184 current_column += fprintf(options.output, "%s@%s(", function_name, 185 libsym->lib->name); 186#endif 187 188 func = name2func(function_name); 189 if (!func) { 190 int i; 191 for (i = 0; i < 4; i++) { 192 current_column += 193 display_arg(type, proc, i, arg_unknown); 194 current_column += fprintf(options.output, ", "); 195 } 196 current_column += display_arg(type, proc, 4, arg_unknown); 197 return; 198 } else { 199 int i; 200 for (i = 0; i < func->num_params - func->params_right - 1; i++) { 201 current_column += 202 display_arg(type, proc, i, func->arg_info[i]); 203 current_column += fprintf(options.output, ", "); 204 } 205 if (func->num_params > func->params_right) { 206 current_column += 207 display_arg(type, proc, i, func->arg_info[i]); 208 if (func->params_right) { 209 current_column += fprintf(options.output, ", "); 210 } 211 } 212 if (func->params_right 213 || func->return_info->type == ARGTYPE_STRING_N 214 || func->return_info->type == ARGTYPE_ARRAY) { 215 save_register_args(type, proc); 216 } 217 } 218} 219 220void 221output_right(enum tof type, struct Process *proc, struct library_symbol *libsym) 222{ 223 const char *function_name = libsym->name; 224 Function *func = name2func(function_name); 225 static arg_type_info *arg_unknown = NULL; 226 if (arg_unknown == NULL) 227 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN); 228 229 if (options.summary) { 230 struct opt_c_struct *st; 231 if (!dict_opt_c) { 232 dict_opt_c = 233 dict_init(dict_key2hash_string, 234 dict_key_cmp_string); 235 } 236 st = dict_find_entry(dict_opt_c, function_name); 237 if (!st) { 238 char *na; 239 st = malloc(sizeof(struct opt_c_struct)); 240 na = strdup(function_name); 241 if (!st || !na) { 242 perror("malloc()"); 243 exit(1); 244 } 245 st->count = 0; 246 st->tv.tv_sec = st->tv.tv_usec = 0; 247 dict_enter(dict_opt_c, na, st); 248 } 249 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) { 250 st->tv.tv_usec += current_time_spent.tv_usec - 1000000; 251 st->tv.tv_sec++; 252 } else { 253 st->tv.tv_usec += current_time_spent.tv_usec; 254 } 255 st->count++; 256 st->tv.tv_sec += current_time_spent.tv_sec; 257 258// fprintf(options.output, "%s <%lu.%06d>\n", function_name, 259// current_time_spent.tv_sec, (int)current_time_spent.tv_usec); 260 return; 261 } 262 if (current_proc && (current_proc != proc || 263 current_depth != proc->callstack_depth)) { 264 fprintf(options.output, " <unfinished ...>\n"); 265 current_proc = 0; 266 } 267 if (current_proc != proc) { 268 begin_of_line(type, proc); 269#ifdef USE_DEMANGLE 270 current_column += 271 fprintf(options.output, "<... %s resumed> ", 272 options.demangle ? my_demangle(function_name) : function_name); 273#else 274 current_column += 275 fprintf(options.output, "<... %s resumed> ", function_name); 276#endif 277 } 278 279 if (!func) { 280 current_column += fprintf(options.output, ") "); 281 tabto(options.align - 1); 282 fprintf(options.output, "= "); 283 display_arg(type, proc, -1, arg_unknown); 284 } else { 285 int i; 286 for (i = func->num_params - func->params_right; 287 i < func->num_params - 1; i++) { 288 current_column += 289 display_arg(type, proc, i, func->arg_info[i]); 290 current_column += fprintf(options.output, ", "); 291 } 292 if (func->params_right) { 293 current_column += 294 display_arg(type, proc, i, func->arg_info[i]); 295 } 296 current_column += fprintf(options.output, ") "); 297 tabto(options.align - 1); 298 fprintf(options.output, "= "); 299 if (func->return_info->type == ARGTYPE_VOID) { 300 fprintf(options.output, "<void>"); 301 } else { 302 display_arg(type, proc, -1, func->return_info); 303 } 304 } 305 if (opt_T) { 306 fprintf(options.output, " <%lu.%06d>", 307 current_time_spent.tv_sec, 308 (int)current_time_spent.tv_usec); 309 } 310 fprintf(options.output, "\n"); 311 312#if defined(HAVE_LIBUNWIND) 313 if (options.bt_depth > 0) { 314 unw_cursor_t cursor; 315 unw_word_t ip, sp; 316 int unwind_depth = options.bt_depth; 317 char fn_name[100]; 318 319 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv); 320 while (unwind_depth) { 321 unw_get_reg(&cursor, UNW_REG_IP, &ip); 322 unw_get_reg(&cursor, UNW_REG_SP, &sp); 323 unw_get_proc_name(&cursor, fn_name, 100, NULL); 324 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip); 325 if (unw_step(&cursor) <= 0) 326 break; 327 unwind_depth--; 328 } 329 fprintf(options.output, "\n"); 330 } 331#endif /* defined(HAVE_LIBUNWIND) */ 332 333 current_proc = 0; 334 current_column = 0; 335} 336