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