output.c revision f6ec08afb96292fd3c802c1f633d8de249664c72
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#include <errno.h> 35 36#include "common.h" 37#include "proc.h" 38#include "library.h" 39#include "type.h" 40#include "value.h" 41#include "value_dict.h" 42#include "fetch.h" 43 44/* TODO FIXME XXX: include in common.h: */ 45extern struct timeval current_time_spent; 46 47Dict *dict_opt_c = NULL; 48 49static Process *current_proc = 0; 50static int current_depth = 0; 51static int current_column = 0; 52 53static void 54output_indent(struct Process *proc) 55{ 56 int d = options.indent * (proc->callstack_depth - 1); 57 current_column += fprintf(options.output, "%*s", d, ""); 58} 59 60static void 61begin_of_line(Process *proc, int is_func, int indent) 62{ 63 current_column = 0; 64 if (!proc) { 65 return; 66 } 67 if ((options.output != stderr) && (opt_p || options.follow)) { 68 current_column += fprintf(options.output, "%u ", proc->pid); 69 } else if (options.follow) { 70 current_column += fprintf(options.output, "[pid %u] ", proc->pid); 71 } 72 if (opt_r) { 73 struct timeval tv; 74 struct timezone tz; 75 static struct timeval old_tv = { 0, 0 }; 76 struct timeval diff; 77 78 gettimeofday(&tv, &tz); 79 80 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) { 81 old_tv.tv_sec = tv.tv_sec; 82 old_tv.tv_usec = tv.tv_usec; 83 } 84 diff.tv_sec = tv.tv_sec - old_tv.tv_sec; 85 if (tv.tv_usec >= old_tv.tv_usec) { 86 diff.tv_usec = tv.tv_usec - old_tv.tv_usec; 87 } else { 88 diff.tv_sec--; 89 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec; 90 } 91 old_tv.tv_sec = tv.tv_sec; 92 old_tv.tv_usec = tv.tv_usec; 93 current_column += fprintf(options.output, "%3lu.%06d ", 94 diff.tv_sec, (int)diff.tv_usec); 95 } 96 if (opt_t) { 97 struct timeval tv; 98 struct timezone tz; 99 100 gettimeofday(&tv, &tz); 101 if (opt_t > 2) { 102 current_column += fprintf(options.output, "%lu.%06d ", 103 tv.tv_sec, (int)tv.tv_usec); 104 } else if (opt_t > 1) { 105 struct tm *tmp = localtime(&tv.tv_sec); 106 current_column += 107 fprintf(options.output, "%02d:%02d:%02d.%06d ", 108 tmp->tm_hour, tmp->tm_min, tmp->tm_sec, 109 (int)tv.tv_usec); 110 } else { 111 struct tm *tmp = localtime(&tv.tv_sec); 112 current_column += fprintf(options.output, "%02d:%02d:%02d ", 113 tmp->tm_hour, tmp->tm_min, 114 tmp->tm_sec); 115 } 116 } 117 if (opt_i) { 118 if (is_func) 119 current_column += fprintf(options.output, "[%p] ", 120 proc->return_addr); 121 else 122 current_column += fprintf(options.output, "[%p] ", 123 proc->instruction_pointer); 124 } 125 if (options.indent > 0 && indent) { 126 output_indent(proc); 127 } 128} 129 130/* The default prototype is: long X(long, long, long, long). */ 131static Function * 132build_default_prototype(void) 133{ 134 Function *ret = malloc(sizeof(*ret)); 135 size_t i = 0; 136 if (ret == NULL) 137 goto err; 138 memset(ret, 0, sizeof(*ret)); 139 140 struct arg_type_info *unknown_type = type_get_simple(ARGTYPE_UNKNOWN); 141 142 ret->return_info = unknown_type; 143 144 ret->num_params = 4; 145 for (i = 0; i < (size_t)ret->num_params; ++i) 146 ret->arg_info[i] = unknown_type; 147 148 return ret; 149 150err: 151 report_global_error("malloc: %s", strerror(errno)); 152 free(ret); 153 154 return NULL; 155} 156 157static Function * 158name2func(char const *name) { 159 Function *tmp; 160 const char *str1, *str2; 161 162 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) { 163 str1 = tmp->name; 164 str2 = name; 165 if (!strcmp(str1, str2)) 166 return tmp; 167 } 168 169 static Function *def = NULL; 170 if (def == NULL) 171 def = build_default_prototype(); 172 173 return def; 174} 175 176void 177output_line(Process *proc, char *fmt, ...) { 178 va_list args; 179 180 if (options.summary) { 181 return; 182 } 183 if (current_proc) { 184 if (current_proc->callstack[current_depth].return_addr) { 185 fprintf(options.output, " <unfinished ...>\n"); 186 } else { 187 fprintf(options.output, " <no return ...>\n"); 188 } 189 } 190 current_proc = 0; 191 if (!fmt) { 192 return; 193 } 194 begin_of_line(proc, 0, 0); 195 196 va_start(args, fmt); 197 vfprintf(options.output, fmt, args); 198 fprintf(options.output, "\n"); 199 va_end(args); 200 current_column = 0; 201} 202 203static void 204tabto(int col) { 205 if (current_column < col) { 206 fprintf(options.output, "%*s", col - current_column, ""); 207 } 208} 209 210static int 211account_output(int o) 212{ 213 if (o < 0) 214 return -1; 215 current_column += o; 216 return 0; 217} 218 219static int 220output_error(void) 221{ 222 return account_output(fprintf(options.output, "?")); 223} 224 225static int 226fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context, 227 struct value_dict *arguments, struct arg_type_info *info, 228 struct value *valuep) 229{ 230 /* Arrays decay into pointers per C standard. We check for 231 * this here, because here we also capture arrays that come 232 * from parameter packs. */ 233 int own = 0; 234 if (info->type == ARGTYPE_ARRAY) { 235 struct arg_type_info *tmp = malloc(sizeof(*tmp)); 236 if (tmp != NULL) { 237 type_init_pointer(tmp, info, 0); 238 info = tmp; 239 own = 1; 240 } 241 } 242 243 struct value value; 244 value_init(&value, proc, NULL, info, own); 245 if (fetch_arg_next(context, type, proc, info, &value) < 0) 246 return -1; 247 248 if (val_dict_push_next(arguments, &value) < 0) { 249 value_destroy(&value); 250 return -1; 251 } 252 253 if (valuep != NULL) 254 *valuep = value; 255 256 return 0; 257} 258 259static void 260fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp) 261{ 262 if (*params_leftp == -1) 263 *params_leftp = val_dict_count(arguments); 264} 265 266static int 267fetch_one_param(enum tof type, Process *proc, struct fetch_context *context, 268 struct value_dict *arguments, struct arg_type_info *info, 269 ssize_t *params_leftp) 270{ 271 return fetch_simple_param(type, proc, context, arguments, 272 info, NULL); 273} 274 275static int 276fetch_params(enum tof type, Process *proc, struct fetch_context *context, 277 struct value_dict *arguments, Function *func, ssize_t *params_leftp) 278{ 279 size_t i; 280 for (i = 0; i < (size_t)func->num_params; ++i) { 281 if (i == (size_t)(func->num_params - func->params_right)) 282 fetch_param_stop(arguments, params_leftp); 283 if (fetch_one_param(type, proc, context, arguments, 284 func->arg_info[i], params_leftp) < 0) 285 return -1; 286 } 287 288 /* Implicit stop at the end of parameter list. */ 289 fetch_param_stop(arguments, params_leftp); 290 return 0; 291} 292 293static int 294output_one(struct value *val, struct value_dict *arguments) 295{ 296 int o = format_argument(options.output, val, arguments); 297 if (account_output(o) < 0) { 298 if (output_error() < 0) 299 return -1; 300 o = 1; 301 } 302 return o; 303} 304 305static int 306output_params(struct value_dict *arguments, size_t start, size_t end, 307 int *need_delimp) 308{ 309 size_t i; 310 int need_delim = *need_delimp; 311 for (i = start; i < end; ++i) { 312 if (need_delim 313 && account_output(fprintf(options.output, ", ")) < 0) 314 return -1; 315 struct value *value = val_dict_get_num(arguments, i); 316 if (value == NULL) 317 return -1; 318 need_delim = output_one(value, arguments); 319 if (need_delim < 0) 320 return -1; 321 } 322 *need_delimp = need_delim; 323 return 0; 324} 325 326void 327output_left(enum tof type, struct Process *proc, 328 struct library_symbol *libsym) 329{ 330 const char *function_name = libsym->name; 331 Function *func; 332 333 if (options.summary) { 334 return; 335 } 336 if (current_proc) { 337 fprintf(options.output, " <unfinished ...>\n"); 338 current_column = 0; 339 } 340 current_proc = proc; 341 current_depth = proc->callstack_depth; 342 begin_of_line(proc, type == LT_TOF_FUNCTION, 1); 343 if (!options.hide_caller && libsym->lib != NULL 344 && libsym->plt_type != LS_TOPLT_NONE) 345 current_column += fprintf(options.output, "%s->", 346 libsym->lib->soname); 347 348 const char *name = function_name; 349#ifdef USE_DEMANGLE 350 if (options.demangle) 351 name = my_demangle(function_name); 352#endif 353 if (account_output(fprintf(options.output, "%s(", name)) < 0) 354 return; 355 356 func = name2func(function_name); 357 if (func == NULL) 358 return; 359 360 struct fetch_context *context = fetch_arg_init(type, proc, 361 func->return_info); 362 struct value_dict *arguments = malloc(sizeof(*arguments)); 363 if (arguments == NULL) 364 return; 365 val_dict_init(arguments); 366 367 ssize_t params_left = -1; 368 int need_delim = 0; 369 if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0 370 || output_params(arguments, 0, params_left, &need_delim) < 0) { 371 val_dict_destroy(arguments); 372 fetch_arg_done(context); 373 return; 374 } 375 376 struct callstack_element *stel 377 = &proc->callstack[proc->callstack_depth - 1]; 378 stel->fetch_context = context; 379 stel->arguments = arguments; 380 stel->out.params_left = params_left; 381 stel->out.need_delim = need_delim; 382} 383 384void 385output_right(enum tof type, struct Process *proc, struct library_symbol *libsym) 386{ 387 const char *function_name = libsym->name; 388 Function *func = name2func(function_name); 389 if (func == NULL) 390 return; 391 392 if (options.summary) { 393 struct opt_c_struct *st; 394 if (!dict_opt_c) { 395 dict_opt_c = 396 dict_init(dict_key2hash_string, 397 dict_key_cmp_string); 398 } 399 st = dict_find_entry(dict_opt_c, function_name); 400 if (!st) { 401 char *na; 402 st = malloc(sizeof(struct opt_c_struct)); 403 na = strdup(function_name); 404 if (!st || !na) { 405 perror("malloc()"); 406 exit(1); 407 } 408 st->count = 0; 409 st->tv.tv_sec = st->tv.tv_usec = 0; 410 dict_enter(dict_opt_c, na, st); 411 } 412 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) { 413 st->tv.tv_usec += current_time_spent.tv_usec - 1000000; 414 st->tv.tv_sec++; 415 } else { 416 st->tv.tv_usec += current_time_spent.tv_usec; 417 } 418 st->count++; 419 st->tv.tv_sec += current_time_spent.tv_sec; 420 421// fprintf(options.output, "%s <%lu.%06d>\n", function_name, 422// current_time_spent.tv_sec, (int)current_time_spent.tv_usec); 423 return; 424 } 425 if (current_proc && (current_proc != proc || 426 current_depth != proc->callstack_depth)) { 427 fprintf(options.output, " <unfinished ...>\n"); 428 current_proc = 0; 429 } 430 if (current_proc != proc) { 431 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1); 432#ifdef USE_DEMANGLE 433 current_column += 434 fprintf(options.output, "<... %s resumed> ", 435 options.demangle ? my_demangle(function_name) : function_name); 436#else 437 current_column += 438 fprintf(options.output, "<... %s resumed> ", function_name); 439#endif 440 } 441 442 struct callstack_element *stel 443 = &proc->callstack[proc->callstack_depth - 1]; 444 445 struct fetch_context *context = stel->fetch_context; 446 447 /* Fetch & enter into dictionary the retval first, so that 448 * other values can use it in expressions. */ 449 struct value retval; 450 int own_retval = 0; 451 if (context != NULL) { 452 value_init(&retval, proc, NULL, func->return_info, 0); 453 own_retval = 1; 454 if (fetch_retval(context, type, proc, func->return_info, 455 &retval) == 0) { 456 if (stel->arguments != NULL 457 && val_dict_push_named(stel->arguments, &retval, 458 "retval", 0) == 0) 459 own_retval = 0; 460 } 461 } 462 463 if (stel->arguments != NULL) 464 output_params(stel->arguments, stel->out.params_left, 465 val_dict_count(stel->arguments), 466 &stel->out.need_delim); 467 468 current_column += fprintf(options.output, ") "); 469 tabto(options.align - 1); 470 fprintf(options.output, "= "); 471 472 output_one(&retval, stel->arguments); 473 474 if (own_retval) 475 value_destroy(&retval); 476 477 val_dict_destroy(stel->arguments); 478 free(stel->arguments); 479 fetch_arg_done(context); 480 481 if (opt_T) { 482 fprintf(options.output, " <%lu.%06d>", 483 current_time_spent.tv_sec, 484 (int)current_time_spent.tv_usec); 485 } 486 fprintf(options.output, "\n"); 487 488#if defined(HAVE_LIBUNWIND) 489 if (options.bt_depth > 0) { 490 unw_cursor_t cursor; 491 unw_word_t ip, sp; 492 int unwind_depth = options.bt_depth; 493 char fn_name[100]; 494 495 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv); 496 while (unwind_depth) { 497 unw_get_reg(&cursor, UNW_REG_IP, &ip); 498 unw_get_reg(&cursor, UNW_REG_SP, &sp); 499 unw_get_proc_name(&cursor, fn_name, 100, NULL); 500 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip); 501 if (unw_step(&cursor) <= 0) 502 break; 503 unwind_depth--; 504 } 505 fprintf(options.output, "\n"); 506 } 507#endif /* defined(HAVE_LIBUNWIND) */ 508 509 current_proc = 0; 510 current_column = 0; 511} 512 513static void 514do_report(const char *filename, unsigned line_no, const char *severity, 515 const char *fmt, va_list args) 516{ 517 char buf[128]; 518 vsnprintf(buf, sizeof(buf), fmt, args); 519 buf[sizeof(buf) - 1] = 0; 520 if (filename != NULL) 521 output_line(0, "%s:%d: %s: %s", 522 filename, line_no, severity, buf); 523 else 524 output_line(0, "%s: %s", severity, buf); 525} 526 527void 528report_error(const char *filename, unsigned line_no, char *fmt, ...) 529{ 530 va_list args; 531 va_start(args, fmt); 532 do_report(filename, line_no, "error", fmt, args); 533 va_end(args); 534} 535 536void 537report_warning(const char *filename, unsigned line_no, char *fmt, ...) 538{ 539 va_list args; 540 va_start(args, fmt); 541 do_report(filename, line_no, "warning", fmt, args); 542 va_end(args); 543} 544 545void 546report_global_error(char *fmt, ...) 547{ 548 va_list args; 549 va_start(args, fmt); 550 do_report(NULL, 0, "error", fmt, args); 551 va_end(args); 552} 553