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
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.
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.
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
40 #include "lens_default.h"
42 #include "memstream.h"
46 #include "prototype.h"
49 #include "value_dict.h"
51 /* TODO FIXME XXX: include in common.h: */
52 extern struct timeval current_time_spent;
54 struct dict *dict_opt_c = NULL;
56 static struct process *current_proc = 0;
57 static size_t current_depth = 0;
58 static int current_column = 0;
61 output_indent(struct process *proc)
63 int d = options.indent * (proc->callstack_depth - 1);
64 current_column += fprintf(options.output, "%*s", d, "");
68 begin_of_line(struct process *proc, int is_func, int indent)
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);
81 static struct timeval old_tv = { 0, 0 };
84 gettimeofday(&tv, NULL);
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;
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;
95 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
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,
105 gettimeofday(&tv, NULL);
107 current_column += fprintf(options.output, "%lu.%06d ",
108 (unsigned long)tv.tv_sec,
110 } else if (opt_t > 1) {
111 struct tm *tmp = localtime(&tv.tv_sec);
113 fprintf(options.output, "%02d:%02d:%02d.%06d ",
114 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
117 struct tm *tmp = localtime(&tv.tv_sec);
118 current_column += fprintf(options.output, "%02d:%02d:%02d ",
119 tmp->tm_hour, tmp->tm_min,
125 struct callstack_element *stel
126 = &proc->callstack[proc->callstack_depth - 1];
127 current_column += fprintf(options.output, "[%p] ",
130 current_column += fprintf(options.output, "[%p] ",
131 proc->instruction_pointer);
134 if (options.indent > 0 && indent) {
139 static struct arg_type_info *
140 get_unknown_type(void)
142 static struct arg_type_info *ret = NULL;
146 static struct arg_type_info info;
147 info = *type_get_simple(ARGTYPE_LONG);
148 info.lens = &guess_lens;
153 /* The default prototype is: long X(long, long, long, long). */
154 static struct prototype *
155 build_default_prototype(void)
157 static struct prototype *ret = NULL;
161 static struct prototype proto;
162 prototype_init(&proto);
164 struct arg_type_info *unknown_type = get_unknown_type();
165 assert(unknown_type != NULL);
166 proto.return_info = unknown_type;
167 proto.own_return_info = 0;
169 struct param unknown_param;
170 param_init_type(&unknown_param, unknown_type, 0);
173 for (i = 0; i < 4; ++i)
174 if (prototype_push_param(&proto, &unknown_param) < 0) {
175 report_global_error("build_default_prototype: %s",
177 prototype_destroy(&proto);
186 snip_period(char *buf)
188 char *period = strrchr(buf, '.');
189 if (period != NULL && strcmp(period, ".so") != 0) {
197 static struct prototype *
198 library_get_prototype(struct library *lib, const char *name)
200 if (lib->protolib == NULL) {
201 size_t sz = strlen(lib->soname);
203 memcpy(buf, lib->soname, sz + 1);
206 if (protolib_cache_maybe_load(&g_protocache, buf, 0,
207 true, &lib->protolib) < 0)
209 } while (lib->protolib == NULL
210 && lib->type == LT_LIBTYPE_DSO
211 && snip_period(buf));
213 if (lib->protolib == NULL)
214 lib->protolib = protolib_cache_default(&g_protocache,
217 if (lib->protolib == NULL)
220 return protolib_lookup_prototype(lib->protolib, name,
221 lib->type != LT_LIBTYPE_SYSCALL);
224 struct find_proto_data {
226 struct prototype *ret;
229 static enum callback_status
230 find_proto_cb(struct process *proc, struct library *lib, void *d)
232 struct find_proto_data *data = d;
233 data->ret = library_get_prototype(lib, data->name);
234 return CBS_STOP_IF(data->ret != NULL);
237 static struct prototype *
238 lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
240 if (libsym->proto != NULL)
241 return libsym->proto;
243 struct library *lib = libsym->lib;
245 struct find_proto_data data = { libsym->name };
246 data.ret = library_get_prototype(lib, libsym->name);
248 && libsym->plt_type == LS_TOPLT_EXEC)
249 proc_each_library(proc, NULL, find_proto_cb, &data);
250 if (data.ret != NULL)
254 return build_default_prototype();
258 output_line(struct process *proc, const char *fmt, ...)
263 if (current_proc != NULL) {
264 if (current_proc->callstack[current_depth].return_addr)
265 fprintf(options.output, " <unfinished ...>\n");
267 fprintf(options.output, " <no return ...>\n");
273 begin_of_line(proc, 0, 0);
277 vfprintf(options.output, fmt, args);
278 fprintf(options.output, "\n");
286 if (current_column < col) {
287 fprintf(options.output, "%*s", col - current_column, "");
292 output_error(FILE *stream)
294 return fprintf(stream, "?");
298 fetch_simple_param(enum tof type, struct process *proc,
299 struct fetch_context *context,
300 struct value_dict *arguments,
301 struct arg_type_info *info, int own,
302 struct value *valuep)
304 /* Arrays decay into pointers per C standard. We check for
305 * this here, because here we also capture arrays that come
306 * from parameter packs. */
307 if (info->type == ARGTYPE_ARRAY) {
308 struct arg_type_info *tmp = malloc(sizeof(*tmp));
310 type_init_pointer(tmp, info, own);
311 tmp->lens = info->lens;
318 value_init(&value, proc, NULL, info, own);
319 if (fetch_arg_next(context, type, proc, info, &value) < 0)
322 if (val_dict_push_next(arguments, &value) < 0) {
323 value_destroy(&value);
334 fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
336 if (*params_leftp == -1)
337 *params_leftp = val_dict_count(arguments);
341 fetch_param_pack(enum tof type, struct process *proc,
342 struct fetch_context *context,
343 struct value_dict *arguments, struct param *param,
344 ssize_t *params_leftp)
346 struct param_enum *e = param_pack_init(param, arguments);
353 struct arg_type_info *info = malloc(sizeof(*info));
355 || param_pack_next(param, e, info, &insert_stop) < 0) {
363 fetch_param_stop(arguments, params_leftp);
365 if (info->type == ARGTYPE_VOID) {
372 if (fetch_simple_param(type, proc, context, arguments,
377 switch (param_pack_stop(param, e, &val)) {
390 param_pack_done(param, e);
395 fetch_one_param(enum tof type, struct process *proc,
396 struct fetch_context *context,
397 struct value_dict *arguments, struct param *param,
398 ssize_t *params_leftp)
400 switch (param->flavor) {
402 case PARAM_FLAVOR_TYPE:
403 return fetch_simple_param(type, proc, context, arguments,
404 param->u.type.type, 0, NULL);
406 case PARAM_FLAVOR_PACK:
407 if (fetch_param_pack_start(context,
408 param->u.pack.ppflavor) < 0)
410 rc = fetch_param_pack(type, proc, context, arguments,
411 param, params_leftp);
412 fetch_param_pack_end(context);
415 case PARAM_FLAVOR_STOP:
416 fetch_param_stop(arguments, params_leftp);
420 assert(!"Invalid param flavor!");
424 struct fetch_one_param_data
426 struct process *proc;
427 struct fetch_context *context;
428 struct value_dict *arguments;
429 ssize_t *params_leftp;
433 static enum callback_status
434 fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
436 struct fetch_one_param_data *cb_data = data;
437 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
439 cb_data->arguments, param,
440 cb_data->params_leftp) < 0);
444 fetch_params(enum tof type, struct process *proc,
445 struct fetch_context *context,
446 struct value_dict *arguments, struct prototype *func,
447 ssize_t *params_leftp)
449 struct fetch_one_param_data cb_data
450 = { proc, context, arguments, params_leftp, type };
451 if (prototype_each_param(func, NULL,
452 &fetch_one_param_cb, &cb_data) != NULL)
455 /* Implicit stop at the end of parameter list. */
456 fetch_param_stop(arguments, params_leftp);
461 struct format_argument_data
464 struct value_dict *arguments;
468 format_argument_cb(FILE *stream, void *ptr)
470 struct format_argument_data *data = ptr;
471 int o = format_argument(stream, data->value, data->arguments);
473 o = output_error(stream);
478 output_params(struct value_dict *arguments, size_t start, size_t end,
482 for (i = start; i < end; ++i) {
483 struct value *value = val_dict_get_num(arguments, i);
487 struct format_argument_data data = { value, arguments };
488 int o = delim_output(options.output, need_delimp,
489 format_argument_cb, &data);
498 output_left(enum tof type, struct process *proc,
499 struct library_symbol *libsym)
501 if (options.summary) {
505 fprintf(options.output, " <unfinished ...>\n");
509 current_depth = proc->callstack_depth;
510 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
511 if (!options.hide_caller && libsym->lib != NULL
512 && libsym->plt_type != LS_TOPLT_NONE)
513 /* We don't terribly mind failing this. */
514 account_output(¤t_column,
515 fprintf(options.output, "%s->",
516 libsym->lib->soname));
518 const char *name = libsym->name;
520 if (options.demangle)
521 name = my_demangle(libsym->name);
523 if (account_output(¤t_column,
524 fprintf(options.output, "%s", name)) < 0)
527 if (libsym->lib != NULL
528 && libsym->lib->type != LT_LIBTYPE_MAIN
529 && libsym->plt_type == LS_TOPLT_NONE
530 && account_output(¤t_column,
531 fprintf(options.output, "@%s",
532 libsym->lib->soname)) < 0)
533 /* We do mind failing this though. */
536 account_output(¤t_column, fprintf(options.output, "("));
538 struct prototype *func = lookup_symbol_prototype(proc, libsym);
541 account_output(¤t_column, fprintf(options.output, "???"));
545 struct fetch_context *context = fetch_arg_init(type, proc,
550 struct value_dict *arguments = malloc(sizeof(*arguments));
551 if (arguments == NULL) {
552 fetch_arg_done(context);
555 val_dict_init(arguments);
557 ssize_t params_left = -1;
559 if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0
560 || output_params(arguments, 0, params_left, &need_delim) < 0) {
561 val_dict_destroy(arguments);
562 fetch_arg_done(context);
567 struct callstack_element *stel
568 = &proc->callstack[proc->callstack_depth - 1];
569 stel->fetch_context = context;
570 stel->arguments = arguments;
571 stel->out.params_left = params_left;
572 stel->out.need_delim = need_delim;
576 free_stringp_cb(const char **stringp, void *data)
578 free((char *)*stringp);
582 output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
584 struct prototype *func = lookup_symbol_prototype(proc, libsym);
589 if (options.summary) {
590 if (dict_opt_c == NULL) {
591 dict_opt_c = malloc(sizeof(*dict_opt_c));
592 if (dict_opt_c == NULL) {
595 "Can't allocate memory for "
596 "keeping track of -c.\n");
601 DICT_INIT(dict_opt_c, char *, struct opt_c_struct,
602 dict_hash_string, dict_eq_string, NULL);
605 struct opt_c_struct *st
606 = DICT_FIND_REF(dict_opt_c, &libsym->name,
607 struct opt_c_struct);
609 const char *na = strdup(libsym->name);
610 struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}};
612 || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) {
614 DICT_DESTROY(dict_opt_c, const char *,
616 free_stringp_cb, NULL, NULL);
619 st = DICT_FIND_REF(dict_opt_c, &libsym->name,
620 struct opt_c_struct);
624 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
625 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
628 st->tv.tv_usec += current_time_spent.tv_usec;
631 st->tv.tv_sec += current_time_spent.tv_sec;
635 if (current_proc && (current_proc != proc ||
636 current_depth != proc->callstack_depth)) {
637 fprintf(options.output, " <unfinished ...>\n");
640 if (current_proc != proc) {
641 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
644 fprintf(options.output, "<... %s resumed> ",
645 options.demangle ? my_demangle(libsym->name)
649 fprintf(options.output, "<... %s resumed> ", libsym->name);
653 struct callstack_element *stel
654 = &proc->callstack[proc->callstack_depth - 1];
656 struct fetch_context *context = stel->fetch_context;
658 /* Fetch & enter into dictionary the retval first, so that
659 * other values can use it in expressions. */
662 if (context != NULL) {
663 value_init(&retval, proc, NULL, func->return_info, 0);
665 if (fetch_retval(context, type, proc, func->return_info,
667 value_set_type(&retval, NULL, 0);
668 else if (stel->arguments != NULL
669 && val_dict_push_named(stel->arguments, &retval,
674 if (stel->arguments != NULL)
675 output_params(stel->arguments, stel->out.params_left,
676 val_dict_count(stel->arguments),
677 &stel->out.need_delim);
679 current_column += fprintf(options.output, ") ");
680 tabto(options.align - 1);
681 fprintf(options.output, "= ");
683 if (context != NULL && retval.type != NULL) {
684 struct format_argument_data data = { &retval, stel->arguments };
685 format_argument_cb(options.output, &data);
689 value_destroy(&retval);
692 fprintf(options.output, " <%lu.%06d>",
693 (unsigned long)current_time_spent.tv_sec,
694 (int)current_time_spent.tv_usec);
696 fprintf(options.output, "\n");
698 #if defined(HAVE_LIBUNWIND)
699 if (options.bt_depth > 0
700 && proc->unwind_priv != NULL
701 && proc->unwind_as != NULL) {
703 arch_addr_t ip, function_offset;
704 struct library *lib = NULL;
705 int unwind_depth = options.bt_depth;
707 const char *lib_name;
710 /* Verify that we can safely cast arch_addr_t* to
712 (void)sizeof(char[1 - 2*(sizeof(unw_word_t)
713 != sizeof(arch_addr_t))]);
714 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
715 while (unwind_depth) {
716 unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip);
717 unw_get_proc_name(&cursor, fn_name, sizeof(fn_name),
718 (unw_word_t *) &function_offset);
720 /* We are looking for the library with the base address
721 * closest to the current ip. */
722 lib_name = "unmapped_area";
723 distance = (size_t) -1;
724 lib = proc->libraries;
725 while (lib != NULL) {
726 /* N.B.: Assumes sizeof(size_t) ==
727 * sizeof(arch_addr_t).
728 * Keyword: double cast. */
729 if ((ip >= lib->base) &&
730 ((size_t)(ip - lib->base)
732 distance = ip - lib->base;
733 lib_name = lib->pathname;
738 fprintf(options.output, " > %s(%s+0x%p) [%p]\n",
739 lib_name, fn_name, function_offset, ip);
741 if (unw_step(&cursor) <= 0)
745 fprintf(options.output, "\n");
747 #endif /* defined(HAVE_LIBUNWIND) */
754 delim_output(FILE *stream, int *need_delimp,
755 int (*writer)(FILE *stream, void *data),
760 /* If we don't need a delimiter, then we don't need to go
761 * through a temporary stream. It's all the same whether
762 * WRITER emits anything or not. */
764 o = writer(stream, data);
768 if (memstream_init(&ms) < 0)
770 o = writer(ms.stream, data);
771 if (memstream_close(&ms) < 0)
773 if (o > 0 && ((*need_delimp
774 && account_output(&o, fprintf(stream, ", ")) < 0)
775 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
778 memstream_destroy(&ms);
784 *need_delimp = *need_delimp || o > 0;
789 account_output(int *countp, int c)
797 do_report(const char *filename, unsigned line_no, const char *severity,
798 const char *fmt, va_list args)
801 vsnprintf(buf, sizeof(buf), fmt, args);
802 buf[sizeof(buf) - 1] = 0;
803 if (filename != NULL)
804 output_line(0, "%s:%d: %s: %s",
805 filename, line_no, severity, buf);
807 output_line(0, "%s: %s", severity, buf);
811 report_error(const char *filename, unsigned line_no, const char *fmt, ...)
815 do_report(filename, line_no, "error", fmt, args);
820 report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
824 do_report(filename, line_no, "warning", fmt, args);
829 report_global_error(const char *fmt, ...)
833 do_report(NULL, 0, "error", fmt, args);