output.c revision 76c61f15d7989bf7adffed2e46a44c34a80bd927
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 "ltrace.h" 14#include "options.h" 15#include "output.h" 16#include "dict.h" 17 18#ifdef USE_DEMANGLE 19#include "demangle.h" 20#endif 21 22/* TODO FIXME XXX: include in ltrace.h: */ 23extern struct timeval current_time_spent; 24 25struct dict *dict_opt_c = NULL; 26 27static struct process *current_proc = 0; 28static int current_depth = 0; 29static int current_column = 0; 30 31static void output_indent(struct process *proc) 32{ 33 current_column += 34 fprintf(output, "%*s", opt_n * proc->callstack_depth, ""); 35} 36 37static void begin_of_line(enum tof type, struct process *proc) 38{ 39 current_column = 0; 40 if (!proc) { 41 return; 42 } 43 if ((output != stderr) && (opt_p || opt_f)) { 44 current_column += fprintf(output, "%u ", proc->pid); 45 } else if (list_of_processes->next) { 46 current_column += fprintf(output, "[pid %u] ", proc->pid); 47 } 48 if (opt_r) { 49 struct timeval tv; 50 struct timezone tz; 51 static struct timeval old_tv = { 0, 0 }; 52 struct timeval diff; 53 54 gettimeofday(&tv, &tz); 55 56 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) { 57 old_tv.tv_sec = tv.tv_sec; 58 old_tv.tv_usec = tv.tv_usec; 59 } 60 diff.tv_sec = tv.tv_sec - old_tv.tv_sec; 61 if (tv.tv_usec >= old_tv.tv_usec) { 62 diff.tv_usec = tv.tv_usec - old_tv.tv_usec; 63 } else { 64 diff.tv_sec--; 65 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec; 66 } 67 old_tv.tv_sec = tv.tv_sec; 68 old_tv.tv_usec = tv.tv_usec; 69 current_column += fprintf(output, "%3lu.%06d ", 70 diff.tv_sec, (int)diff.tv_usec); 71 } 72 if (opt_t) { 73 struct timeval tv; 74 struct timezone tz; 75 76 gettimeofday(&tv, &tz); 77 if (opt_t > 2) { 78 current_column += fprintf(output, "%lu.%06d ", 79 tv.tv_sec, (int)tv.tv_usec); 80 } else if (opt_t > 1) { 81 struct tm *tmp = localtime(&tv.tv_sec); 82 current_column += 83 fprintf(output, "%02d:%02d:%02d.%06d ", 84 tmp->tm_hour, tmp->tm_min, tmp->tm_sec, 85 (int)tv.tv_usec); 86 } else { 87 struct tm *tmp = localtime(&tv.tv_sec); 88 current_column += fprintf(output, "%02d:%02d:%02d ", 89 tmp->tm_hour, tmp->tm_min, 90 tmp->tm_sec); 91 } 92 } 93 if (opt_i) { 94 if (type == LT_TOF_FUNCTION || type == LT_TOF_FUNCTIONR) { 95 current_column += fprintf(output, "[%p] ", 96 proc->return_addr); 97 } else { 98 current_column += fprintf(output, "[%p] ", 99 proc->instruction_pointer); 100 } 101 } 102 if (opt_n > 0 && type != LT_TOF_NONE) { 103 output_indent(proc); 104 } 105} 106 107static struct function *name2func(char *name) 108{ 109 struct function *tmp; 110 const char *str1, *str2; 111 112 tmp = list_of_functions; 113 while (tmp) { 114#ifdef USE_DEMANGLE 115 str1 = opt_C ? my_demangle(tmp->name) : tmp->name; 116 str2 = opt_C ? my_demangle(name) : name; 117#else 118 str1 = tmp->name; 119 str2 = name; 120#endif 121 if (!strcmp(str1, str2)) { 122 123 return tmp; 124 } 125 tmp = tmp->next; 126 } 127 return NULL; 128} 129 130void output_line(struct process *proc, char *fmt, ...) 131{ 132 va_list args; 133 134 if (opt_c) { 135 return; 136 } 137 if (current_proc) { 138 if (current_proc->callstack[current_depth].return_addr) { 139 fprintf(output, " <unfinished ...>\n"); 140 } else { 141 fprintf(output, " <no return ...>\n"); 142 } 143 } 144 current_proc = 0; 145 if (!fmt) { 146 return; 147 } 148 begin_of_line(LT_TOF_NONE, proc); 149 150 va_start(args, fmt); 151 vfprintf(output, fmt, args); 152 fprintf(output, "\n"); 153 va_end(args); 154 current_column = 0; 155} 156 157static void tabto(int col) 158{ 159 if (current_column < col) { 160 fprintf(output, "%*s", col - current_column, ""); 161 } 162} 163 164void output_left(enum tof type, struct process *proc, char *function_name) 165{ 166 struct function *func; 167 168 if (opt_c) { 169 return; 170 } 171 if (current_proc) { 172 fprintf(output, " <unfinished ...>\n"); 173 current_proc = 0; 174 current_column = 0; 175 } 176 current_proc = proc; 177 current_depth = proc->callstack_depth; 178 proc->type_being_displayed = type; 179 begin_of_line(type, proc); 180#ifdef USE_DEMANGLE 181 current_column += 182 fprintf(output, "%s(", 183 opt_C ? my_demangle(function_name) : function_name); 184#else 185 current_column += fprintf(output, "%s(", function_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, ARGTYPE_UNKNOWN); 194 current_column += fprintf(output, ", "); 195 } 196 current_column += display_arg(type, proc, 4, ARGTYPE_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_types[i]); 203 current_column += fprintf(output, ", "); 204 } 205 if (func->num_params > func->params_right) { 206 current_column += 207 display_arg(type, proc, i, func->arg_types[i]); 208 if (func->params_right) { 209 current_column += fprintf(output, ", "); 210 } 211 } 212 if (func->params_right) { 213 save_register_args(type, proc); 214 } 215 } 216} 217 218void output_right(enum tof type, struct process *proc, char *function_name) 219{ 220 struct function *func = name2func(function_name); 221 222 if (opt_c) { 223 struct opt_c_struct *st; 224 if (!dict_opt_c) { 225 dict_opt_c = 226 dict_init(dict_key2hash_string, 227 dict_key_cmp_string); 228 } 229 st = dict_find_entry(dict_opt_c, function_name); 230 if (!st) { 231 char *na; 232 st = malloc(sizeof(struct opt_c_struct)); 233 na = strdup(function_name); 234 if (!st || !na) { 235 perror("malloc()"); 236 exit(1); 237 } 238 st->count = 0; 239 st->tv.tv_sec = st->tv.tv_usec = 0; 240 dict_enter(dict_opt_c, na, st); 241 } 242 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) { 243 st->tv.tv_usec += current_time_spent.tv_usec - 1000000; 244 st->tv.tv_sec++; 245 } else { 246 st->tv.tv_usec += current_time_spent.tv_usec; 247 } 248 st->count++; 249 st->tv.tv_sec += current_time_spent.tv_sec; 250 251// fprintf(output, "%s <%lu.%06d>\n", function_name, 252// current_time_spent.tv_sec, (int)current_time_spent.tv_usec); 253 return; 254 } 255 if (current_proc && (current_proc != proc || 256 current_depth != proc->callstack_depth)) { 257 fprintf(output, " <unfinished ...>\n"); 258 current_proc = 0; 259 } 260 if (current_proc != proc) { 261 begin_of_line(type, proc); 262#ifdef USE_DEMANGLE 263 current_column += 264 fprintf(output, "<... %s resumed> ", 265 opt_C ? my_demangle(function_name) : function_name); 266#else 267 current_column += 268 fprintf(output, "<... %s resumed> ", function_name); 269#endif 270 } 271 272 if (!func) { 273 current_column += fprintf(output, ") "); 274 tabto(opt_a - 1); 275 fprintf(output, "= "); 276 display_arg(type, proc, -1, ARGTYPE_UNKNOWN); 277 } else { 278 int i; 279 for (i = func->num_params - func->params_right; 280 i < func->num_params - 1; i++) { 281 current_column += 282 display_arg(type, proc, i, func->arg_types[i]); 283 current_column += fprintf(output, ", "); 284 } 285 if (func->params_right) { 286 current_column += 287 display_arg(type, proc, i, func->arg_types[i]); 288 } 289 current_column += fprintf(output, ") "); 290 tabto(opt_a - 1); 291 fprintf(output, "= "); 292 if (func->return_type == ARGTYPE_VOID) { 293 fprintf(output, "<void>"); 294 } else { 295 display_arg(type, proc, -1, func->return_type); 296 } 297 } 298 if (opt_T) { 299 fprintf(output, " <%lu.%06d>", 300 current_time_spent.tv_sec, 301 (int)current_time_spent.tv_usec); 302 } 303 fprintf(output, "\n"); 304 current_proc = 0; 305 current_column = 0; 306} 307