output.c revision 37b73c0ab8cbcab4729df6d12f2dc846d4652310
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc. 4 * Copyright (C) 2010 Joe Damato 5 * Copyright (C) 1997,1998,1999,2001,2002,2003,2004,2007,2008,2009 Juan Cespedes 6 * Copyright (C) 2006 Paul Gilliam 7 * Copyright (C) 2006 Ian Wienand 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License as 11 * published by the Free Software Foundation; either version 2 of the 12 * License, or (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 22 * 02110-1301 USA 23 */ 24 25#include "config.h" 26 27#include <stdio.h> 28#include <stdlib.h> 29#include <stdarg.h> 30#include <string.h> 31#include <time.h> 32#include <sys/time.h> 33#include <unistd.h> 34 35#include "common.h" 36#include "proc.h" 37#include "library.h" 38#include "type.h" 39#include "value.h" 40#include "value_dict.h" 41 42/* TODO FIXME XXX: include in common.h: */ 43extern struct timeval current_time_spent; 44 45Dict *dict_opt_c = NULL; 46 47static Process *current_proc = 0; 48static int current_depth = 0; 49static int current_column = 0; 50 51static void 52output_indent(struct Process *proc) 53{ 54 int d = options.indent * (proc->callstack_depth - 1); 55 current_column += fprintf(options.output, "%*s", d, ""); 56} 57 58static void 59begin_of_line(Process *proc, int is_func, int indent) 60{ 61 current_column = 0; 62 if (!proc) { 63 return; 64 } 65 if ((options.output != stderr) && (opt_p || options.follow)) { 66 current_column += fprintf(options.output, "%u ", proc->pid); 67 } else if (options.follow) { 68 current_column += fprintf(options.output, "[pid %u] ", proc->pid); 69 } 70 if (opt_r) { 71 struct timeval tv; 72 struct timezone tz; 73 static struct timeval old_tv = { 0, 0 }; 74 struct timeval diff; 75 76 gettimeofday(&tv, &tz); 77 78 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) { 79 old_tv.tv_sec = tv.tv_sec; 80 old_tv.tv_usec = tv.tv_usec; 81 } 82 diff.tv_sec = tv.tv_sec - old_tv.tv_sec; 83 if (tv.tv_usec >= old_tv.tv_usec) { 84 diff.tv_usec = tv.tv_usec - old_tv.tv_usec; 85 } else { 86 diff.tv_sec--; 87 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec; 88 } 89 old_tv.tv_sec = tv.tv_sec; 90 old_tv.tv_usec = tv.tv_usec; 91 current_column += fprintf(options.output, "%3lu.%06d ", 92 diff.tv_sec, (int)diff.tv_usec); 93 } 94 if (opt_t) { 95 struct timeval tv; 96 struct timezone tz; 97 98 gettimeofday(&tv, &tz); 99 if (opt_t > 2) { 100 current_column += fprintf(options.output, "%lu.%06d ", 101 tv.tv_sec, (int)tv.tv_usec); 102 } else if (opt_t > 1) { 103 struct tm *tmp = localtime(&tv.tv_sec); 104 current_column += 105 fprintf(options.output, "%02d:%02d:%02d.%06d ", 106 tmp->tm_hour, tmp->tm_min, tmp->tm_sec, 107 (int)tv.tv_usec); 108 } else { 109 struct tm *tmp = localtime(&tv.tv_sec); 110 current_column += fprintf(options.output, "%02d:%02d:%02d ", 111 tmp->tm_hour, tmp->tm_min, 112 tmp->tm_sec); 113 } 114 } 115 if (opt_i) { 116 if (is_func) 117 current_column += fprintf(options.output, "[%p] ", 118 proc->return_addr); 119 else 120 current_column += fprintf(options.output, "[%p] ", 121 proc->instruction_pointer); 122 } 123 if (options.indent > 0 && indent) { 124 output_indent(proc); 125 } 126} 127 128static Function * 129name2func(char const *name) { 130 Function *tmp; 131 const char *str1, *str2; 132 133 tmp = list_of_functions; 134 while (tmp) { 135#ifdef USE_DEMANGLE 136 str1 = options.demangle ? my_demangle(tmp->name) : tmp->name; 137 str2 = options.demangle ? my_demangle(name) : name; 138#else 139 str1 = tmp->name; 140 str2 = name; 141#endif 142 if (!strcmp(str1, str2)) { 143 144 return tmp; 145 } 146 tmp = tmp->next; 147 } 148 return NULL; 149} 150 151void 152output_line(Process *proc, char *fmt, ...) { 153 va_list args; 154 155 if (options.summary) { 156 return; 157 } 158 if (current_proc) { 159 if (current_proc->callstack[current_depth].return_addr) { 160 fprintf(options.output, " <unfinished ...>\n"); 161 } else { 162 fprintf(options.output, " <no return ...>\n"); 163 } 164 } 165 current_proc = 0; 166 if (!fmt) { 167 return; 168 } 169 begin_of_line(proc, 0, 0); 170 171 va_start(args, fmt); 172 vfprintf(options.output, fmt, args); 173 fprintf(options.output, "\n"); 174 va_end(args); 175 current_column = 0; 176} 177 178static void 179tabto(int col) { 180 if (current_column < col) { 181 fprintf(options.output, "%*s", col - current_column, ""); 182 } 183} 184 185void 186output_left(enum tof type, struct Process *proc, 187 struct library_symbol *libsym) 188{ 189 const char *function_name = libsym->name; 190 Function *func; 191 static struct arg_type_info *arg_unknown = NULL; 192 if (arg_unknown == NULL) 193 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN); 194 195 if (options.summary) { 196 return; 197 } 198 if (current_proc) { 199 fprintf(options.output, " <unfinished ...>\n"); 200 current_column = 0; 201 } 202 current_proc = proc; 203 current_depth = proc->callstack_depth; 204 begin_of_line(type, type == LT_TOF_FUNCTION, 1); 205 if (!options.hide_caller && libsym->lib != NULL 206 && libsym->plt_type != LS_TOPLT_NONE) 207 current_column += fprintf(options.output, "%s->", 208 libsym->lib->soname); 209#ifdef USE_DEMANGLE 210 current_column += 211 fprintf(options.output, "%s(", 212 (options.demangle 213 ? my_demangle(function_name) : function_name)); 214#else 215 current_column += fprintf(options.output, "%s(", function_name); 216#endif 217 218 func = name2func(function_name); 219 220 struct value_dict *arguments = malloc(sizeof(*arguments)); 221 if (arguments == NULL) 222 return; 223 val_dict_init(arguments); 224 225 int num, right; 226 if (!func) { 227 int i; 228 for (i = 0; i < 4; i++) { 229 long l = gimme_arg(type, proc, i, arg_unknown); 230 struct value val; 231 value_init(&val, proc, NULL, arg_unknown, 0); 232 value_set_long(&val, l); 233 val_dict_push_next(arguments, &val); 234 } 235 right = 0; 236 num = 4; 237 } else { 238 int i; 239 for (i = 0; i < func->num_params; i++) { 240 long l = gimme_arg(type, proc, i, func->arg_info[i]); 241 struct value val; 242 value_init(&val, proc, NULL, func->arg_info[i], 0); 243 value_set_long(&val, l); 244 val_dict_push_next(arguments, &val); 245 } 246 right = func->params_right; 247 num = func->num_params; 248 } 249 250 int i; 251 for (i = 0; i < num - right - 1; i++) { 252 current_column += 253 format_argument(options.output, 254 val_dict_get_num(arguments, i), 255 arguments); 256 current_column += fprintf(options.output, ", "); 257 } 258 259 if (num > right) { 260 current_column += 261 format_argument(options.output, 262 val_dict_get_num(arguments, i), 263 arguments); 264 if (right) { 265 current_column += fprintf(options.output, ", "); 266 } 267 } 268 269 struct callstack_element *stel 270 = &proc->callstack[proc->callstack_depth - 1]; 271 stel->arguments = arguments; 272} 273 274void 275output_right(enum tof type, struct Process *proc, struct library_symbol *libsym) 276{ 277 const char *function_name = libsym->name; 278 Function *func = name2func(function_name); 279 static struct arg_type_info *arg_unknown = NULL; 280 if (arg_unknown == NULL) 281 arg_unknown = lookup_prototype(ARGTYPE_UNKNOWN); 282 283 if (options.summary) { 284 struct opt_c_struct *st; 285 if (!dict_opt_c) { 286 dict_opt_c = 287 dict_init(dict_key2hash_string, 288 dict_key_cmp_string); 289 } 290 st = dict_find_entry(dict_opt_c, function_name); 291 if (!st) { 292 char *na; 293 st = malloc(sizeof(struct opt_c_struct)); 294 na = strdup(function_name); 295 if (!st || !na) { 296 perror("malloc()"); 297 exit(1); 298 } 299 st->count = 0; 300 st->tv.tv_sec = st->tv.tv_usec = 0; 301 dict_enter(dict_opt_c, na, st); 302 } 303 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) { 304 st->tv.tv_usec += current_time_spent.tv_usec - 1000000; 305 st->tv.tv_sec++; 306 } else { 307 st->tv.tv_usec += current_time_spent.tv_usec; 308 } 309 st->count++; 310 st->tv.tv_sec += current_time_spent.tv_sec; 311 312// fprintf(options.output, "%s <%lu.%06d>\n", function_name, 313// current_time_spent.tv_sec, (int)current_time_spent.tv_usec); 314 return; 315 } 316 if (current_proc && (current_proc != proc || 317 current_depth != proc->callstack_depth)) { 318 fprintf(options.output, " <unfinished ...>\n"); 319 current_proc = 0; 320 } 321 if (current_proc != proc) { 322 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1); 323#ifdef USE_DEMANGLE 324 current_column += 325 fprintf(options.output, "<... %s resumed> ", 326 options.demangle ? my_demangle(function_name) : function_name); 327#else 328 current_column += 329 fprintf(options.output, "<... %s resumed> ", function_name); 330#endif 331 } 332 333 struct callstack_element *stel 334 = &proc->callstack[proc->callstack_depth - 1]; 335 336 struct value retval; 337 struct arg_type_info *return_info = arg_unknown; 338 if (func != NULL) 339 return_info = func->return_info; 340 long l = gimme_arg(type, proc, -1, return_info); 341 value_init(&retval, proc, NULL, return_info, 0); 342 value_set_long(&retval, l); 343 val_dict_push_named(stel->arguments, &retval, "retval", 0); 344 345 if (!func) { 346 current_column += fprintf(options.output, ") "); 347 tabto(options.align - 1); 348 fprintf(options.output, "= "); 349 } else { 350 int i; 351 for (i = func->num_params - func->params_right; 352 i < func->num_params - 1; i++) { 353 current_column += 354 format_argument(options.output, 355 val_dict_get_num 356 (stel->arguments, i), 357 stel->arguments); 358 current_column += fprintf(options.output, ", "); 359 } 360 if (func->params_right) { 361 current_column += 362 format_argument(options.output, 363 val_dict_get_num 364 (stel->arguments, i), 365 stel->arguments); 366 } 367 current_column += fprintf(options.output, ") "); 368 tabto(options.align - 1); 369 fprintf(options.output, "= "); 370 } 371 372 format_argument(options.output, &retval, stel->arguments); 373 val_dict_destroy(stel->arguments); 374 375 if (opt_T) { 376 fprintf(options.output, " <%lu.%06d>", 377 current_time_spent.tv_sec, 378 (int)current_time_spent.tv_usec); 379 } 380 fprintf(options.output, "\n"); 381 382#if defined(HAVE_LIBUNWIND) 383 if (options.bt_depth > 0) { 384 unw_cursor_t cursor; 385 unw_word_t ip, sp; 386 int unwind_depth = options.bt_depth; 387 char fn_name[100]; 388 389 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv); 390 while (unwind_depth) { 391 unw_get_reg(&cursor, UNW_REG_IP, &ip); 392 unw_get_reg(&cursor, UNW_REG_SP, &sp); 393 unw_get_proc_name(&cursor, fn_name, 100, NULL); 394 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip); 395 if (unw_step(&cursor) <= 0) 396 break; 397 unwind_depth--; 398 } 399 fprintf(options.output, "\n"); 400 } 401#endif /* defined(HAVE_LIBUNWIND) */ 402 403 current_proc = 0; 404 current_column = 0; 405} 406 407static void 408do_report(const char *filename, unsigned line_no, const char *severity, 409 const char *fmt, va_list args) 410{ 411 char buf[128]; 412 vsnprintf(buf, sizeof(buf), fmt, args); 413 buf[sizeof(buf) - 1] = 0; 414 if (filename != NULL) 415 output_line(0, "%s:%d: %s: %s", 416 filename, line_no, severity, buf); 417 else 418 output_line(0, "%s: %s", severity, buf); 419} 420 421void 422report_error(const char *filename, unsigned line_no, char *fmt, ...) 423{ 424 va_list args; 425 va_start(args, fmt); 426 do_report(filename, line_no, "error", fmt, args); 427 va_end(args); 428} 429 430void 431report_warning(const char *filename, unsigned line_no, char *fmt, ...) 432{ 433 va_list args; 434 va_start(args, fmt); 435 do_report(filename, line_no, "warning", fmt, args); 436 va_end(args); 437} 438 439void 440report_global_error(char *fmt, ...) 441{ 442 va_list args; 443 va_start(args, fmt); 444 do_report(NULL, 0, "error", fmt, args); 445 va_end(args); 446} 447