output.c revision 3690fad65d26e4e140595d6c0075410a84ff22f5
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, IBM Corporation 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#include <assert.h> 36 37#include "common.h" 38#include "proc.h" 39#include "library.h" 40#include "type.h" 41#include "value.h" 42#include "value_dict.h" 43#include "param.h" 44#include "fetch.h" 45#include "lens_default.h" 46#include "memstream.h" 47 48/* TODO FIXME XXX: include in common.h: */ 49extern struct timeval current_time_spent; 50 51Dict *dict_opt_c = NULL; 52 53static Process *current_proc = 0; 54static size_t current_depth = 0; 55static int current_column = 0; 56 57static void 58output_indent(struct Process *proc) 59{ 60 int d = options.indent * (proc->callstack_depth - 1); 61 current_column += fprintf(options.output, "%*s", d, ""); 62} 63 64static void 65begin_of_line(Process *proc, int is_func, int indent) 66{ 67 current_column = 0; 68 if (!proc) { 69 return; 70 } 71 if ((options.output != stderr) && (opt_p || options.follow)) { 72 current_column += fprintf(options.output, "%u ", proc->pid); 73 } else if (options.follow) { 74 current_column += fprintf(options.output, "[pid %u] ", proc->pid); 75 } 76 if (opt_r) { 77 struct timeval tv; 78 struct timezone tz; 79 static struct timeval old_tv = { 0, 0 }; 80 struct timeval diff; 81 82 gettimeofday(&tv, &tz); 83 84 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) { 85 old_tv.tv_sec = tv.tv_sec; 86 old_tv.tv_usec = tv.tv_usec; 87 } 88 diff.tv_sec = tv.tv_sec - old_tv.tv_sec; 89 if (tv.tv_usec >= old_tv.tv_usec) { 90 diff.tv_usec = tv.tv_usec - old_tv.tv_usec; 91 } else { 92 diff.tv_sec--; 93 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec; 94 } 95 old_tv.tv_sec = tv.tv_sec; 96 old_tv.tv_usec = tv.tv_usec; 97 current_column += fprintf(options.output, "%3lu.%06d ", 98 diff.tv_sec, (int)diff.tv_usec); 99 } 100 if (opt_t) { 101 struct timeval tv; 102 struct timezone tz; 103 104 gettimeofday(&tv, &tz); 105 if (opt_t > 2) { 106 current_column += fprintf(options.output, "%lu.%06d ", 107 tv.tv_sec, (int)tv.tv_usec); 108 } else if (opt_t > 1) { 109 struct tm *tmp = localtime(&tv.tv_sec); 110 current_column += 111 fprintf(options.output, "%02d:%02d:%02d.%06d ", 112 tmp->tm_hour, tmp->tm_min, tmp->tm_sec, 113 (int)tv.tv_usec); 114 } else { 115 struct tm *tmp = localtime(&tv.tv_sec); 116 current_column += fprintf(options.output, "%02d:%02d:%02d ", 117 tmp->tm_hour, tmp->tm_min, 118 tmp->tm_sec); 119 } 120 } 121 if (opt_i) { 122 if (is_func) 123 current_column += fprintf(options.output, "[%p] ", 124 proc->return_addr); 125 else 126 current_column += fprintf(options.output, "[%p] ", 127 proc->instruction_pointer); 128 } 129 if (options.indent > 0 && indent) { 130 output_indent(proc); 131 } 132} 133 134static struct arg_type_info * 135get_unknown_type(void) 136{ 137 static struct arg_type_info *info = NULL; 138 if (info == NULL) { 139 info = malloc(sizeof(*info)); 140 if (info == NULL) { 141 report_global_error("malloc: %s", strerror(errno)); 142 abort(); 143 } 144 *info = *type_get_simple(ARGTYPE_LONG); 145 info->lens = &guess_lens; 146 } 147 return info; 148} 149 150/* The default prototype is: long X(long, long, long, long). */ 151static Function * 152build_default_prototype(void) 153{ 154 Function *ret = malloc(sizeof(*ret)); 155 size_t i = 0; 156 if (ret == NULL) 157 goto err; 158 memset(ret, 0, sizeof(*ret)); 159 160 struct arg_type_info *unknown_type = get_unknown_type(); 161 162 ret->return_info = unknown_type; 163 ret->own_return_info = 0; 164 165 ret->num_params = 4; 166 ret->params = malloc(sizeof(*ret->params) * ret->num_params); 167 if (ret->params == NULL) 168 goto err; 169 170 for (i = 0; i < ret->num_params; ++i) 171 param_init_type(&ret->params[i], unknown_type, 0); 172 173 return ret; 174 175err: 176 report_global_error("malloc: %s", strerror(errno)); 177 if (ret->params != NULL) { 178 while (i-- > 0) 179 param_destroy(&ret->params[i]); 180 free(ret->params); 181 } 182 183 free(ret); 184 185 return NULL; 186} 187 188static Function * 189name2func(char const *name) { 190 Function *tmp; 191 const char *str1, *str2; 192 193 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) { 194 str1 = tmp->name; 195 str2 = name; 196 if (!strcmp(str1, str2)) 197 return tmp; 198 } 199 200 static Function *def = NULL; 201 if (def == NULL) 202 def = build_default_prototype(); 203 204 return def; 205} 206 207void 208output_line(struct Process *proc, const char *fmt, ...) 209{ 210 if (options.summary) 211 return; 212 213 if (current_proc != NULL) { 214 if (current_proc->callstack[current_depth].return_addr) 215 fprintf(options.output, " <unfinished ...>\n"); 216 else 217 fprintf(options.output, " <no return ...>\n"); 218 } 219 current_proc = NULL; 220 if (fmt == NULL) 221 return; 222 223 begin_of_line(proc, 0, 0); 224 225 va_list args; 226 va_start(args, fmt); 227 vfprintf(options.output, fmt, args); 228 fprintf(options.output, "\n"); 229 va_end(args); 230 231 current_column = 0; 232} 233 234static void 235tabto(int col) { 236 if (current_column < col) { 237 fprintf(options.output, "%*s", col - current_column, ""); 238 } 239} 240 241static int 242output_error(FILE *stream) 243{ 244 return fprintf(stream, "?"); 245} 246 247static int 248fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context, 249 struct value_dict *arguments, 250 struct arg_type_info *info, int own, 251 struct value *valuep) 252{ 253 /* Arrays decay into pointers per C standard. We check for 254 * this here, because here we also capture arrays that come 255 * from parameter packs. */ 256 if (info->type == ARGTYPE_ARRAY) { 257 struct arg_type_info *tmp = malloc(sizeof(*tmp)); 258 if (tmp != NULL) { 259 type_init_pointer(tmp, info, own); 260 tmp->lens = info->lens; 261 info = tmp; 262 own = 1; 263 } 264 } 265 266 struct value value; 267 value_init(&value, proc, NULL, info, own); 268 if (fetch_arg_next(context, type, proc, info, &value) < 0) 269 return -1; 270 271 if (val_dict_push_next(arguments, &value) < 0) { 272 value_destroy(&value); 273 return -1; 274 } 275 276 if (valuep != NULL) 277 *valuep = value; 278 279 return 0; 280} 281 282static void 283fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp) 284{ 285 if (*params_leftp == -1) 286 *params_leftp = val_dict_count(arguments); 287} 288 289static int 290fetch_param_pack(enum tof type, Process *proc, struct fetch_context *context, 291 struct value_dict *arguments, struct param *param, 292 ssize_t *params_leftp) 293{ 294 struct param_enum *e = param_pack_init(param, arguments); 295 if (e == NULL) 296 return -1; 297 298 int ret = 0; 299 while (1) { 300 int insert_stop = 0; 301 struct arg_type_info *info = malloc(sizeof(*info)); 302 if (info == NULL 303 || param_pack_next(param, e, info, &insert_stop) < 0) { 304 fail: 305 free(info); 306 ret = -1; 307 break; 308 } 309 310 if (insert_stop) 311 fetch_param_stop(arguments, params_leftp); 312 313 if (info->type == ARGTYPE_VOID) { 314 type_destroy(info); 315 free(info); 316 break; 317 } 318 319 struct value val; 320 if (fetch_simple_param(type, proc, context, arguments, 321 info, 1, &val) < 0) 322 goto fail; 323 324 int stop = 0; 325 switch (param_pack_stop(param, e, &val)) { 326 case PPCB_ERR: 327 goto fail; 328 case PPCB_STOP: 329 stop = 1; 330 case PPCB_CONT: 331 break; 332 } 333 334 if (stop) 335 break; 336 } 337 338 param_pack_done(param, e); 339 return ret; 340} 341 342static int 343fetch_one_param(enum tof type, Process *proc, struct fetch_context *context, 344 struct value_dict *arguments, struct param *param, 345 ssize_t *params_leftp) 346{ 347 switch (param->flavor) { 348 int rc; 349 case PARAM_FLAVOR_TYPE: 350 return fetch_simple_param(type, proc, context, arguments, 351 param->u.type.type, 0, NULL); 352 353 case PARAM_FLAVOR_PACK: 354 if (fetch_param_pack_start(context, 355 param->u.pack.ppflavor) < 0) 356 return -1; 357 rc = fetch_param_pack(type, proc, context, arguments, 358 param, params_leftp); 359 fetch_param_pack_end(context); 360 return rc; 361 362 case PARAM_FLAVOR_STOP: 363 fetch_param_stop(arguments, params_leftp); 364 return 0; 365 } 366 367 assert(!"Invalid param flavor!"); 368 abort(); 369} 370 371static int 372fetch_params(enum tof type, Process *proc, struct fetch_context *context, 373 struct value_dict *arguments, Function *func, ssize_t *params_leftp) 374{ 375 size_t i; 376 for (i = 0; i < func->num_params; ++i) 377 if (fetch_one_param(type, proc, context, arguments, 378 &func->params[i], params_leftp) < 0) 379 return -1; 380 381 /* Implicit stop at the end of parameter list. */ 382 fetch_param_stop(arguments, params_leftp); 383 384 return 0; 385} 386 387struct format_argument_data 388{ 389 struct value *value; 390 struct value_dict *arguments; 391}; 392 393static int 394format_argument_cb(FILE *stream, void *ptr) 395{ 396 struct format_argument_data *data = ptr; 397 int o = format_argument(stream, data->value, data->arguments); 398 if (o < 0) 399 o = output_error(stream); 400 return o; 401} 402 403static int 404output_params(struct value_dict *arguments, size_t start, size_t end, 405 int *need_delimp) 406{ 407 size_t i; 408 for (i = start; i < end; ++i) { 409 struct value *value = val_dict_get_num(arguments, i); 410 if (value == NULL) 411 return -1; 412 413 struct format_argument_data data = { value, arguments }; 414 int o = delim_output(options.output, need_delimp, 415 format_argument_cb, &data); 416 if (o < 0) 417 return -1; 418 current_column += o; 419 } 420 return 0; 421} 422 423void 424output_left(enum tof type, struct Process *proc, 425 struct library_symbol *libsym) 426{ 427 const char *function_name = libsym->name; 428 Function *func; 429 430 if (options.summary) { 431 return; 432 } 433 if (current_proc) { 434 fprintf(options.output, " <unfinished ...>\n"); 435 current_column = 0; 436 } 437 current_proc = proc; 438 current_depth = proc->callstack_depth; 439 begin_of_line(proc, type == LT_TOF_FUNCTION, 1); 440 if (!options.hide_caller && libsym->lib != NULL 441 && libsym->plt_type != LS_TOPLT_NONE) 442 /* We don't terribly mind failing this. */ 443 account_output(¤t_column, 444 fprintf(options.output, "%s->", 445 libsym->lib->soname)); 446 447 const char *name = function_name; 448#ifdef USE_DEMANGLE 449 if (options.demangle) 450 name = my_demangle(function_name); 451#endif 452 if (account_output(¤t_column, 453 fprintf(options.output, "%s", name)) < 0) 454 return; 455 456 if (libsym->lib != NULL 457 && libsym->lib->type != LT_LIBTYPE_MAIN 458 && libsym->plt_type == LS_TOPLT_NONE 459 && account_output(¤t_column, 460 fprintf(options.output, "@%s", 461 libsym->lib->soname)) < 0) 462 /* We do mind failing this though. */ 463 return; 464 465 account_output(¤t_column, fprintf(options.output, "(")); 466 467 func = name2func(function_name); 468 if (func == NULL) { 469 account_output(¤t_column, fprintf(options.output, "???")); 470 return; 471 } 472 473 struct fetch_context *context = fetch_arg_init(type, proc, 474 func->return_info); 475 struct value_dict *arguments = malloc(sizeof(*arguments)); 476 if (arguments == NULL) 477 return; 478 val_dict_init(arguments); 479 480 ssize_t params_left = -1; 481 int need_delim = 0; 482 if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0 483 || output_params(arguments, 0, params_left, &need_delim) < 0) { 484 val_dict_destroy(arguments); 485 fetch_arg_done(context); 486 arguments = NULL; 487 context = NULL; 488 } 489 490 struct callstack_element *stel 491 = &proc->callstack[proc->callstack_depth - 1]; 492 stel->fetch_context = context; 493 stel->arguments = arguments; 494 stel->out.params_left = params_left; 495 stel->out.need_delim = need_delim; 496} 497 498void 499output_right(enum tof type, struct Process *proc, struct library_symbol *libsym) 500{ 501 const char *function_name = libsym->name; 502 Function *func = name2func(function_name); 503 if (func == NULL) 504 return; 505 506 if (options.summary) { 507 struct opt_c_struct *st; 508 if (!dict_opt_c) { 509 dict_opt_c = 510 dict_init(dict_key2hash_string, 511 dict_key_cmp_string); 512 } 513 st = dict_find_entry(dict_opt_c, function_name); 514 if (!st) { 515 char *na; 516 st = malloc(sizeof(struct opt_c_struct)); 517 na = strdup(function_name); 518 if (!st || !na) { 519 perror("malloc()"); 520 exit(1); 521 } 522 st->count = 0; 523 st->tv.tv_sec = st->tv.tv_usec = 0; 524 dict_enter(dict_opt_c, na, st); 525 } 526 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) { 527 st->tv.tv_usec += current_time_spent.tv_usec - 1000000; 528 st->tv.tv_sec++; 529 } else { 530 st->tv.tv_usec += current_time_spent.tv_usec; 531 } 532 st->count++; 533 st->tv.tv_sec += current_time_spent.tv_sec; 534 535// fprintf(options.output, "%s <%lu.%06d>\n", function_name, 536// current_time_spent.tv_sec, (int)current_time_spent.tv_usec); 537 return; 538 } 539 if (current_proc && (current_proc != proc || 540 current_depth != proc->callstack_depth)) { 541 fprintf(options.output, " <unfinished ...>\n"); 542 current_proc = 0; 543 } 544 if (current_proc != proc) { 545 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1); 546#ifdef USE_DEMANGLE 547 current_column += 548 fprintf(options.output, "<... %s resumed> ", 549 options.demangle ? my_demangle(function_name) : function_name); 550#else 551 current_column += 552 fprintf(options.output, "<... %s resumed> ", function_name); 553#endif 554 } 555 556 struct callstack_element *stel 557 = &proc->callstack[proc->callstack_depth - 1]; 558 559 struct fetch_context *context = stel->fetch_context; 560 561 /* Fetch & enter into dictionary the retval first, so that 562 * other values can use it in expressions. */ 563 struct value retval; 564 int own_retval = 0; 565 if (context != NULL) { 566 value_init(&retval, proc, NULL, func->return_info, 0); 567 own_retval = 1; 568 if (fetch_retval(context, type, proc, func->return_info, 569 &retval) < 0) 570 value_set_type(&retval, NULL, 0); 571 else if (stel->arguments != NULL 572 && val_dict_push_named(stel->arguments, &retval, 573 "retval", 0) == 0) 574 own_retval = 0; 575 } 576 577 if (stel->arguments != NULL) 578 output_params(stel->arguments, stel->out.params_left, 579 val_dict_count(stel->arguments), 580 &stel->out.need_delim); 581 582 current_column += fprintf(options.output, ") "); 583 tabto(options.align - 1); 584 fprintf(options.output, "= "); 585 586 if (context != NULL && retval.type != NULL) { 587 struct format_argument_data data = { &retval, stel->arguments }; 588 format_argument_cb(options.output, &data); 589 } 590 591 if (own_retval) 592 value_destroy(&retval); 593 594 if (opt_T) { 595 fprintf(options.output, " <%lu.%06d>", 596 current_time_spent.tv_sec, 597 (int)current_time_spent.tv_usec); 598 } 599 fprintf(options.output, "\n"); 600 601#if defined(HAVE_LIBUNWIND) 602 if (options.bt_depth > 0) { 603 unw_cursor_t cursor; 604 unw_word_t ip, sp; 605 int unwind_depth = options.bt_depth; 606 char fn_name[100]; 607 608 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv); 609 while (unwind_depth) { 610 unw_get_reg(&cursor, UNW_REG_IP, &ip); 611 unw_get_reg(&cursor, UNW_REG_SP, &sp); 612 unw_get_proc_name(&cursor, fn_name, 100, NULL); 613 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip); 614 if (unw_step(&cursor) <= 0) 615 break; 616 unwind_depth--; 617 } 618 fprintf(options.output, "\n"); 619 } 620#endif /* defined(HAVE_LIBUNWIND) */ 621 622 current_proc = 0; 623 current_column = 0; 624} 625 626int 627delim_output(FILE *stream, int *need_delimp, 628 int (*writer)(FILE *stream, void *data), 629 void *data) 630{ 631 int o; 632 633 /* If we don't need a delimiter, then we don't need to go 634 * through a temporary stream. It's all the same whether 635 * WRITER emits anything or not. */ 636 if (!*need_delimp) { 637 o = writer(stream, data); 638 639 } else { 640 struct memstream ms; 641 if (memstream_init(&ms) < 0) 642 return -1; 643 o = writer(ms.stream, data); 644 if (memstream_close(&ms) < 0) 645 o = -1; 646 if (o > 0 && ((*need_delimp 647 && account_output(&o, fprintf(stream, ", ")) < 0) 648 || fwrite(ms.buf, 1, ms.size, stream) != ms.size)) 649 o = -1; 650 651 memstream_destroy(&ms); 652 } 653 654 if (o < 0) 655 return -1; 656 657 *need_delimp = *need_delimp || o > 0; 658 return o; 659} 660 661int 662account_output(int *countp, int c) 663{ 664 if (c > 0) 665 *countp += c; 666 return c; 667} 668 669static void 670do_report(const char *filename, unsigned line_no, const char *severity, 671 const char *fmt, va_list args) 672{ 673 char buf[128]; 674 vsnprintf(buf, sizeof(buf), fmt, args); 675 buf[sizeof(buf) - 1] = 0; 676 if (filename != NULL) 677 output_line(0, "%s:%d: %s: %s", 678 filename, line_no, severity, buf); 679 else 680 output_line(0, "%s: %s", severity, buf); 681} 682 683void 684report_error(const char *filename, unsigned line_no, const char *fmt, ...) 685{ 686 va_list args; 687 va_start(args, fmt); 688 do_report(filename, line_no, "error", fmt, args); 689 va_end(args); 690} 691 692void 693report_warning(const char *filename, unsigned line_no, const char *fmt, ...) 694{ 695 va_list args; 696 va_start(args, fmt); 697 do_report(filename, line_no, "warning", fmt, args); 698 va_end(args); 699} 700 701void 702report_global_error(const char *fmt, ...) 703{ 704 va_list args; 705 va_start(args, fmt); 706 do_report(NULL, 0, "error", fmt, args); 707 va_end(args); 708} 709