output.c revision 2d45b1a8e26a36a9f85dc49e721c4390ca93dc40
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 pid_t current_pid = 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_pid) { 138 fprintf(output, " <unfinished ...>\n"); 139 } 140 current_pid = 0; 141 if (!fmt) { 142 return; 143 } 144 begin_of_line(LT_TOF_NONE, proc); 145 146 va_start(args, fmt); 147 vfprintf(output, fmt, args); 148 fprintf(output, "\n"); 149 va_end(args); 150 current_column = 0; 151} 152 153static void tabto(int col) 154{ 155 if (current_column < col) { 156 fprintf(output, "%*s", col - current_column, ""); 157 } 158} 159 160void output_left(enum tof type, struct process *proc, char *function_name) 161{ 162 struct function *func; 163 164 if (opt_c) { 165 return; 166 } 167 if (current_pid) { 168 fprintf(output, " <unfinished ...>\n"); 169 current_pid = 0; 170 current_column = 0; 171 } 172 current_pid = proc->pid; 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(output, "%s(", 179 opt_C ? my_demangle(function_name) : function_name); 180#else 181 current_column += fprintf(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, ARGTYPE_UNKNOWN); 190 current_column += fprintf(output, ", "); 191 } 192 current_column += display_arg(type, proc, 4, ARGTYPE_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_types[i]); 199 current_column += fprintf(output, ", "); 200 } 201 if (func->num_params > func->params_right) { 202 current_column += 203 display_arg(type, proc, i, func->arg_types[i]); 204 if (func->params_right) { 205 current_column += fprintf(output, ", "); 206 } 207 } 208 if (func->params_right) { 209 save_register_args(type, proc); 210 } 211 } 212} 213 214void output_right(enum tof type, struct process *proc, char *function_name) 215{ 216 struct function *func = name2func(function_name); 217 218 if (opt_c) { 219 struct opt_c_struct *st; 220 if (!dict_opt_c) { 221 dict_opt_c = 222 dict_init(dict_key2hash_string, 223 dict_key_cmp_string); 224 } 225 st = dict_find_entry(dict_opt_c, function_name); 226 if (!st) { 227 char *na; 228 st = malloc(sizeof(struct opt_c_struct)); 229 na = strdup(function_name); 230 if (!st || !na) { 231 perror("malloc()"); 232 exit(1); 233 } 234 st->count = 0; 235 st->tv.tv_sec = st->tv.tv_usec = 0; 236 dict_enter(dict_opt_c, na, st); 237 } 238 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) { 239 st->tv.tv_usec += current_time_spent.tv_usec - 1000000; 240 st->tv.tv_sec++; 241 } else { 242 st->tv.tv_usec += current_time_spent.tv_usec; 243 } 244 st->count++; 245 st->tv.tv_sec += current_time_spent.tv_sec; 246 247// fprintf(output, "%s <%lu.%06d>\n", function_name, 248// current_time_spent.tv_sec, (int)current_time_spent.tv_usec); 249 return; 250 } 251 if (current_pid && (current_pid != proc->pid || 252 current_depth != proc->callstack_depth)) { 253 fprintf(output, " <unfinished ...>\n"); 254 current_pid = 0; 255 } 256 if (current_pid != proc->pid) { 257 begin_of_line(type, proc); 258#ifdef USE_DEMANGLE 259 current_column += 260 fprintf(output, "<... %s resumed> ", 261 opt_C ? my_demangle(function_name) : function_name); 262#else 263 current_column += 264 fprintf(output, "<... %s resumed> ", function_name); 265#endif 266 } 267 268 if (!func) { 269 current_column += fprintf(output, ") "); 270 tabto(opt_a - 1); 271 fprintf(output, "= "); 272 display_arg(type, proc, -1, ARGTYPE_UNKNOWN); 273 } else { 274 int i; 275 for (i = func->num_params - func->params_right; 276 i < func->num_params - 1; i++) { 277 current_column += 278 display_arg(type, proc, i, func->arg_types[i]); 279 current_column += fprintf(output, ", "); 280 } 281 if (func->params_right) { 282 current_column += 283 display_arg(type, proc, i, func->arg_types[i]); 284 } 285 current_column += fprintf(output, ") "); 286 tabto(opt_a - 1); 287 fprintf(output, "= "); 288 if (func->return_type == ARGTYPE_VOID) { 289 fprintf(output, "<void>"); 290 } else { 291 display_arg(type, proc, -1, func->return_type); 292 } 293 } 294 if (opt_T) { 295 fprintf(output, " <%lu.%06d>", 296 current_time_spent.tv_sec, 297 (int)current_time_spent.tv_usec); 298 } 299 fprintf(output, "\n"); 300 current_pid = 0; 301 current_column = 0; 302} 303