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