2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013,2014 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
41 #include "lens_default.h"
43 #include "memstream.h"
47 #include "prototype.h"
51 #include "value_dict.h"
55 #if defined(HAVE_LIBDW)
56 #include "dwarf_prototypes.h"
59 static struct process *current_proc = NULL;
60 static size_t current_depth = 0;
61 static int current_column = 0;
64 output_indent(struct process *proc)
66 int d = options.indent * (proc->callstack_depth - 1);
67 current_column += fprintf(options.output, "%*s", d, "");
71 begin_of_line(struct process *proc, int is_func, int indent)
77 if ((options.output != stderr) && (opt_p || options.follow)) {
78 current_column += fprintf(options.output, "%u ", proc->pid);
79 } else if (options.follow) {
80 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
84 static struct timeval old_tv = { 0, 0 };
87 gettimeofday(&tv, NULL);
89 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
90 old_tv.tv_sec = tv.tv_sec;
91 old_tv.tv_usec = tv.tv_usec;
93 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
94 if (tv.tv_usec >= old_tv.tv_usec) {
95 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
98 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
100 old_tv.tv_sec = tv.tv_sec;
101 old_tv.tv_usec = tv.tv_usec;
102 current_column += fprintf(options.output, "%3lu.%06d ",
103 (unsigned long)diff.tv_sec,
108 gettimeofday(&tv, NULL);
110 current_column += fprintf(options.output, "%lu.%06d ",
111 (unsigned long)tv.tv_sec,
113 } else if (opt_t > 1) {
114 struct tm *tmp = localtime(&tv.tv_sec);
116 fprintf(options.output, "%02d:%02d:%02d.%06d ",
117 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
120 struct tm *tmp = localtime(&tv.tv_sec);
121 current_column += fprintf(options.output, "%02d:%02d:%02d ",
122 tmp->tm_hour, tmp->tm_min,
128 struct callstack_element *stel
129 = &proc->callstack[proc->callstack_depth - 1];
130 current_column += fprintf(options.output, "[%p] ",
133 current_column += fprintf(options.output, "[%p] ",
134 proc->instruction_pointer);
137 if (options.indent > 0 && indent) {
142 static struct arg_type_info *
143 get_unknown_type(void)
145 static struct arg_type_info *ret = NULL;
149 static struct arg_type_info info;
150 info = *type_get_simple(ARGTYPE_LONG);
151 info.lens = &guess_lens;
156 /* The default prototype is: long X(long, long, long, long). */
157 static struct prototype *
158 build_default_prototype(void)
160 static struct prototype *ret = NULL;
164 static struct prototype proto;
165 prototype_init(&proto);
167 struct arg_type_info *unknown_type = get_unknown_type();
168 assert(unknown_type != NULL);
169 proto.return_info = unknown_type;
170 proto.own_return_info = 0;
172 struct param unknown_param;
173 param_init_type(&unknown_param, unknown_type, 0);
176 for (i = 0; i < 4; ++i)
177 if (prototype_push_param(&proto, &unknown_param) < 0) {
178 report_global_error("build_default_prototype: %s",
180 prototype_destroy(&proto);
189 snip_period(char *buf)
191 char *period = strrchr(buf, '.');
192 if (period != NULL && strcmp(period, ".so") != 0) {
200 static struct prototype *
201 library_get_prototype(struct library *lib, const char *name)
203 if (lib->protolib == NULL) {
204 size_t sz = strlen(lib->soname);
206 memcpy(buf, lib->soname, sz + 1);
209 if (protolib_cache_maybe_load(&g_protocache, buf, 0,
210 true, &lib->protolib) < 0)
212 } while (lib->protolib == NULL
213 && lib->type == LT_LIBTYPE_DSO
214 && snip_period(buf));
216 #if defined(HAVE_LIBDW)
217 // DWARF data fills in the gaps in the .conf files, so I don't check for
218 // lib->protolib==NULL here
219 if (lib->dwfl != NULL &&
220 (filter_matches_library(options.plt_filter, lib ) ||
221 filter_matches_library(options.static_filter, lib ) ||
222 filter_matches_library(options.export_filter, lib )))
223 import_DWARF_prototypes(lib);
225 debug(DEBUG_FUNCTION, "Filter didn't match prototype '%s' in lib '%s'. Not importing",
229 if (lib->protolib == NULL)
230 lib->protolib = protolib_cache_default(&g_protocache,
233 if (lib->protolib == NULL)
236 return protolib_lookup_prototype(lib->protolib, name,
237 lib->type != LT_LIBTYPE_SYSCALL);
240 struct find_proto_data {
242 struct prototype *ret;
245 static enum callback_status
246 find_proto_cb(struct process *proc, struct library *lib, void *d)
248 struct find_proto_data *data = d;
249 data->ret = library_get_prototype(lib, data->name);
250 return CBS_STOP_IF(data->ret != NULL);
253 static struct prototype *
254 lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
256 if (libsym->proto != NULL)
257 return libsym->proto;
259 struct library *lib = libsym->lib;
261 struct find_proto_data data = { libsym->name };
262 data.ret = library_get_prototype(lib, libsym->name);
264 && libsym->plt_type == LS_TOPLT_EXEC)
265 proc_each_library(proc, NULL, find_proto_cb, &data);
266 if (data.ret != NULL)
270 return build_default_prototype();
274 output_line(struct process *proc, const char *fmt, ...)
279 if (current_proc != NULL) {
280 if (current_proc->callstack[current_depth].return_addr)
281 fprintf(options.output, " <unfinished ...>\n");
283 fprintf(options.output, " <no return ...>\n");
289 begin_of_line(proc, 0, 0);
293 vfprintf(options.output, fmt, args);
294 fprintf(options.output, "\n");
302 if (current_column < col) {
303 fprintf(options.output, "%*s", col - current_column, "");
308 output_error(FILE *stream)
310 return fprintf(stream, "?");
314 fetch_simple_param(enum tof type, struct process *proc,
315 struct fetch_context *context,
316 struct value_dict *arguments,
317 struct arg_type_info *info, int own,
318 struct value *valuep)
320 /* Arrays decay into pointers per C standard. We check for
321 * this here, because here we also capture arrays that come
322 * from parameter packs. */
323 if (info->type == ARGTYPE_ARRAY) {
324 struct arg_type_info *tmp = malloc(sizeof(*tmp));
326 type_init_pointer(tmp, info, own);
327 tmp->lens = info->lens;
334 value_init(&value, proc, NULL, info, own);
335 if (fetch_arg_next(context, type, proc, info, &value) < 0)
338 if (val_dict_push_next(arguments, &value) < 0) {
339 value_destroy(&value);
350 fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
352 if (*params_leftp == -1)
353 *params_leftp = val_dict_count(arguments);
357 fetch_param_pack(enum tof type, struct process *proc,
358 struct fetch_context *context,
359 struct value_dict *arguments, struct param *param,
360 ssize_t *params_leftp)
362 struct param_enum *e = param_pack_init(param, arguments);
369 struct arg_type_info *info = malloc(sizeof(*info));
371 || param_pack_next(param, e, info, &insert_stop) < 0) {
379 fetch_param_stop(arguments, params_leftp);
381 if (info->type == ARGTYPE_VOID) {
388 if (fetch_simple_param(type, proc, context, arguments,
393 switch (param_pack_stop(param, e, &val)) {
406 param_pack_done(param, e);
411 fetch_one_param(enum tof type, struct process *proc,
412 struct fetch_context *context,
413 struct value_dict *arguments, struct param *param,
414 ssize_t *params_leftp)
416 switch (param->flavor) {
418 case PARAM_FLAVOR_TYPE:
419 return fetch_simple_param(type, proc, context, arguments,
420 param->u.type.type, 0, NULL);
422 case PARAM_FLAVOR_PACK:
423 if (fetch_param_pack_start(context,
424 param->u.pack.ppflavor) < 0)
426 rc = fetch_param_pack(type, proc, context, arguments,
427 param, params_leftp);
428 fetch_param_pack_end(context);
431 case PARAM_FLAVOR_STOP:
432 fetch_param_stop(arguments, params_leftp);
436 assert(!"Invalid param flavor!");
440 struct fetch_one_param_data
442 struct process *proc;
443 struct fetch_context *context;
444 struct value_dict *arguments;
445 ssize_t *params_leftp;
449 static enum callback_status
450 fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
452 struct fetch_one_param_data *cb_data = data;
453 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
455 cb_data->arguments, param,
456 cb_data->params_leftp) < 0);
460 fetch_params(enum tof type, struct process *proc,
461 struct fetch_context *context,
462 struct value_dict *arguments, struct prototype *func,
463 ssize_t *params_leftp)
465 struct fetch_one_param_data cb_data
466 = { proc, context, arguments, params_leftp, type };
467 if (prototype_each_param(func, NULL,
468 &fetch_one_param_cb, &cb_data) != NULL)
471 /* Implicit stop at the end of parameter list. */
472 fetch_param_stop(arguments, params_leftp);
477 struct format_argument_data
480 struct value_dict *arguments;
484 format_argument_cb(FILE *stream, void *ptr)
486 struct format_argument_data *data = ptr;
487 int o = format_argument(stream, data->value, data->arguments);
489 o = output_error(stream);
494 output_params(struct value_dict *arguments, size_t start, size_t end,
498 for (i = start; i < end; ++i) {
499 struct value *value = val_dict_get_num(arguments, i);
503 struct format_argument_data data = { value, arguments };
504 int o = delim_output(options.output, need_delimp,
505 format_argument_cb, &data);
514 output_left(enum tof type, struct process *proc,
515 struct library_symbol *libsym)
517 assert(! options.summary);
520 fprintf(options.output, " <unfinished ...>\n");
524 current_depth = proc->callstack_depth;
525 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
526 if (!options.hide_caller && libsym->lib != NULL
527 && libsym->plt_type != LS_TOPLT_NONE)
528 /* We don't terribly mind failing this. */
529 account_output(¤t_column,
530 fprintf(options.output, "%s->",
531 libsym->lib->soname));
533 const char *name = libsym->name;
535 if (options.demangle)
536 name = my_demangle(libsym->name);
538 if (account_output(¤t_column,
539 fprintf(options.output, "%s", name)) < 0)
542 if (libsym->lib != NULL
543 && libsym->lib->type != LT_LIBTYPE_MAIN
544 && libsym->plt_type == LS_TOPLT_NONE
545 && account_output(¤t_column,
546 fprintf(options.output, "@%s",
547 libsym->lib->soname)) < 0)
548 /* We do mind failing this though. */
551 account_output(¤t_column, fprintf(options.output, "("));
553 struct prototype *func = lookup_symbol_prototype(proc->leader, libsym);
556 account_output(¤t_column, fprintf(options.output, "???"));
560 struct fetch_context *context = fetch_arg_init(type, proc,
565 struct value_dict *arguments = malloc(sizeof(*arguments));
566 if (arguments == NULL) {
567 fetch_arg_done(context);
570 val_dict_init(arguments);
572 ssize_t params_left = -1;
574 if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0
575 || output_params(arguments, 0, params_left, &need_delim) < 0) {
576 val_dict_destroy(arguments);
577 fetch_arg_done(context);
582 struct callstack_element *stel
583 = &proc->callstack[proc->callstack_depth - 1];
584 stel->fetch_context = context;
585 stel->arguments = arguments;
586 stel->out.params_left = params_left;
587 stel->out.need_delim = need_delim;
590 #if defined(HAVE_LIBDW)
591 /* Prints information about one frame of a thread. Called by
592 dwfl_getthread_frames in output_right. Returns 1 when done (max
593 number of frames reached). Returns -1 on error. Returns 0 on
594 success (if there are more frames in the thread, call us again). */
596 frame_callback (Dwfl_Frame *state, void *arg)
601 int *frames = (int *) arg;
603 if (!dwfl_frame_pc(state, &pc, &isactivation))
609 Dwfl *dwfl = dwfl_thread_dwfl(dwfl_frame_thread(state));
610 Dwfl_Module *mod = dwfl_addrmodule(dwfl, pc);
611 const char *modname = NULL;
612 const char *symname = NULL;
616 modname = dwfl_module_info(mod, NULL, NULL, NULL, NULL,
618 symname = dwfl_module_addrinfo(mod, pc, &off, &sym,
622 /* This mimics the output produced by libunwind below. */
623 fprintf(options.output, " > %s(%s+0x%" PRIx64 ") [%" PRIx64 "]\n",
624 modname, symname, off, pc);
626 /* See if we can extract the source line too and print it on
627 the next line if we can find it. */
629 Dwfl_Line *l = dwfl_module_getsrc(mod, pc);
633 const char *src = dwfl_lineinfo(l, NULL, &line, &col,
636 fprintf(options.output, "\t%s", src);
638 fprintf(options.output, ":%d", line);
640 fprintf(options.output,
643 fprintf(options.output, "\n");
649 /* Max number of frames to print reached? */
650 if ((*frames)-- == 0)
655 #endif /* defined(HAVE_LIBDW) */
658 output_right(enum tof type, struct process *proc, struct library_symbol *libsym,
659 struct timedelta *spent)
661 assert(! options.summary);
663 struct prototype *func = lookup_symbol_prototype(proc, libsym);
667 if (current_proc != NULL
668 && (current_proc != proc
669 || current_depth != proc->callstack_depth)) {
670 fprintf(options.output, " <unfinished ...>\n");
673 if (current_proc != proc) {
674 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
677 fprintf(options.output, "<... %s resumed> ",
678 options.demangle ? my_demangle(libsym->name)
682 fprintf(options.output, "<... %s resumed> ", libsym->name);
686 struct callstack_element *stel
687 = &proc->callstack[proc->callstack_depth - 1];
689 struct fetch_context *context = stel->fetch_context;
691 /* Fetch & enter into dictionary the retval first, so that
692 * other values can use it in expressions. */
694 bool own_retval = false;
695 if (context != NULL) {
696 value_init(&retval, proc, NULL, func->return_info, 0);
698 if (fetch_retval(context, type, proc, func->return_info,
700 value_set_type(&retval, NULL, 0);
701 else if (stel->arguments != NULL
702 && val_dict_push_named(stel->arguments, &retval,
707 if (stel->arguments != NULL)
708 output_params(stel->arguments, stel->out.params_left,
709 val_dict_count(stel->arguments),
710 &stel->out.need_delim);
712 current_column += fprintf(options.output, ") ");
713 tabto(options.align - 1);
714 fprintf(options.output, "= ");
716 if (context != NULL && retval.type != NULL) {
717 struct format_argument_data data = { &retval, stel->arguments };
718 format_argument_cb(options.output, &data);
722 value_destroy(&retval);
725 assert(spent != NULL);
726 fprintf(options.output, " <%lu.%06d>",
727 (unsigned long) spent->tm.tv_sec,
728 (int) spent->tm.tv_usec);
731 fprintf(options.output, "\n");
733 #if defined(HAVE_LIBUNWIND)
734 if (options.bt_depth > 0
735 && proc->unwind_priv != NULL
736 && proc->unwind_as != NULL) {
738 arch_addr_t ip, function_offset;
739 struct library *lib = NULL;
740 int unwind_depth = options.bt_depth;
742 const char *lib_name;
745 /* Verify that we can safely cast arch_addr_t* to
747 (void)sizeof(char[1 - 2*(sizeof(unw_word_t)
748 != sizeof(arch_addr_t))]);
749 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
750 while (unwind_depth) {
752 int rc = unw_get_reg(&cursor, UNW_REG_IP,
755 fprintf(options.output, " > Error: %s\n",
760 /* We are looking for the library with the base address
761 * closest to the current ip. */
762 lib_name = "unmapped_area";
763 distance = (size_t) -1;
764 lib = proc->libraries;
765 while (lib != NULL) {
766 /* N.B.: Assumes sizeof(size_t) ==
767 * sizeof(arch_addr_t).
768 * Keyword: double cast. */
769 if ((ip >= lib->base) &&
770 ((size_t)(ip - lib->base)
772 distance = ip - lib->base;
773 lib_name = lib->pathname;
778 rc = unw_get_proc_name(&cursor, fn_name,
780 (unw_word_t *) &function_offset);
781 if (rc == 0 || rc == -UNW_ENOMEM)
782 fprintf(options.output, " > %s(%s+%p) [%p]\n",
783 lib_name, fn_name, function_offset, ip);
785 fprintf(options.output, " > %s(??\?) [%p]\n",
789 if (unw_step(&cursor) <= 0)
793 fprintf(options.output, "\n");
795 #endif /* defined(HAVE_LIBUNWIND) */
797 #if defined(HAVE_LIBDW)
798 if (options.bt_depth > 0 && proc->leader->dwfl != NULL) {
799 int frames = options.bt_depth;
800 if (dwfl_getthread_frames(proc->leader->dwfl, proc->pid,
801 frame_callback, &frames) < 0) {
802 // Only print an error if we couldn't show anything.
803 // Otherwise just show there might be more...
804 if (frames == options.bt_depth)
806 "dwfl_getthread_frames tid %d: %s\n",
807 proc->pid, dwfl_errmsg(-1));
809 fprintf(options.output, " > [...]\n");
811 fprintf(options.output, "\n");
813 #endif /* defined(HAVE_LIBDW) */
820 delim_output(FILE *stream, int *need_delimp,
821 int (*writer)(FILE *stream, void *data),
826 /* If we don't need a delimiter, then we don't need to go
827 * through a temporary stream. It's all the same whether
828 * WRITER emits anything or not. */
830 o = writer(stream, data);
834 if (memstream_init(&ms) < 0)
836 o = writer(ms.stream, data);
837 if (memstream_close(&ms) < 0)
839 if (o > 0 && ((*need_delimp
840 && account_output(&o, fprintf(stream, ", ")) < 0)
841 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
844 memstream_destroy(&ms);
850 *need_delimp = *need_delimp || o > 0;
855 account_output(int *countp, int c)
863 do_report(const char *filename, unsigned line_no, const char *severity,
864 const char *fmt, va_list args)
867 vsnprintf(buf, sizeof(buf), fmt, args);
868 buf[sizeof(buf) - 1] = 0;
869 if (filename != NULL)
870 output_line(0, "%s:%d: %s: %s",
871 filename, line_no, severity, buf);
873 output_line(0, "%s: %s", severity, buf);
877 report_error(const char *filename, unsigned line_no, const char *fmt, ...)
881 do_report(filename, line_no, "error", fmt, args);
886 report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
890 do_report(filename, line_no, "warning", fmt, args);
895 report_global_error(const char *fmt, ...)
899 do_report(NULL, 0, "error", fmt, args);