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"
54 #if defined(HAVE_LIBDW)
55 #include "dwarf_prototypes.h"
58 static struct process *current_proc = NULL;
59 static size_t current_depth = 0;
60 static int current_column = 0;
63 output_indent(struct process *proc)
65 int d = options.indent * (proc->callstack_depth - 1);
66 current_column += fprintf(options.output, "%*s", d, "");
70 begin_of_line(struct process *proc, int is_func, int indent)
76 if ((options.output != stderr) && (opt_p || options.follow)) {
77 current_column += fprintf(options.output, "%u ", proc->pid);
78 } else if (options.follow) {
79 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
83 static struct timeval old_tv = { 0, 0 };
86 gettimeofday(&tv, NULL);
88 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
89 old_tv.tv_sec = tv.tv_sec;
90 old_tv.tv_usec = tv.tv_usec;
92 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
93 if (tv.tv_usec >= old_tv.tv_usec) {
94 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
97 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
99 old_tv.tv_sec = tv.tv_sec;
100 old_tv.tv_usec = tv.tv_usec;
101 current_column += fprintf(options.output, "%3lu.%06d ",
102 (unsigned long)diff.tv_sec,
107 gettimeofday(&tv, NULL);
109 current_column += fprintf(options.output, "%lu.%06d ",
110 (unsigned long)tv.tv_sec,
112 } else if (opt_t > 1) {
113 struct tm *tmp = localtime(&tv.tv_sec);
115 fprintf(options.output, "%02d:%02d:%02d.%06d ",
116 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
119 struct tm *tmp = localtime(&tv.tv_sec);
120 current_column += fprintf(options.output, "%02d:%02d:%02d ",
121 tmp->tm_hour, tmp->tm_min,
127 struct callstack_element *stel
128 = &proc->callstack[proc->callstack_depth - 1];
129 current_column += fprintf(options.output, "[%p] ",
132 current_column += fprintf(options.output, "[%p] ",
133 proc->instruction_pointer);
136 if (options.indent > 0 && indent) {
141 static struct arg_type_info *
142 get_unknown_type(void)
144 static struct arg_type_info *ret = NULL;
148 static struct arg_type_info info;
149 info = *type_get_simple(ARGTYPE_LONG);
150 info.lens = &guess_lens;
155 /* The default prototype is: long X(long, long, long, long). */
156 static struct prototype *
157 build_default_prototype(void)
159 static struct prototype *ret = NULL;
163 static struct prototype proto;
164 prototype_init(&proto);
166 struct arg_type_info *unknown_type = get_unknown_type();
167 assert(unknown_type != NULL);
168 proto.return_info = unknown_type;
169 proto.own_return_info = 0;
171 struct param unknown_param;
172 param_init_type(&unknown_param, unknown_type, 0);
175 for (i = 0; i < 4; ++i)
176 if (prototype_push_param(&proto, &unknown_param) < 0) {
177 report_global_error("build_default_prototype: %s",
179 prototype_destroy(&proto);
188 snip_period(char *buf)
190 char *period = strrchr(buf, '.');
191 if (period != NULL && strcmp(period, ".so") != 0) {
199 static struct prototype *
200 library_get_prototype(struct library *lib, const char *name)
202 if (lib->protolib == NULL) {
203 size_t sz = strlen(lib->soname);
205 memcpy(buf, lib->soname, sz + 1);
208 if (protolib_cache_maybe_load(&g_protocache, buf, 0,
209 true, &lib->protolib) < 0)
211 } while (lib->protolib == NULL
212 && lib->type == LT_LIBTYPE_DSO
213 && snip_period(buf));
215 #if defined(HAVE_LIBDW)
216 // DWARF data fills in the gaps in the .conf files, so I don't check for
217 // lib->protolib==NULL here
218 if (lib->dwfl != NULL &&
219 (filter_matches_library(options.plt_filter, lib ) ||
220 filter_matches_library(options.static_filter, lib ) ||
221 filter_matches_library(options.export_filter, lib )))
222 import_DWARF_prototypes(lib);
225 if (lib->protolib == NULL)
226 lib->protolib = protolib_cache_default(&g_protocache,
229 if (lib->protolib == NULL)
232 return protolib_lookup_prototype(lib->protolib, name,
233 lib->type != LT_LIBTYPE_SYSCALL);
236 struct find_proto_data {
238 struct prototype *ret;
241 static enum callback_status
242 find_proto_cb(struct process *proc, struct library *lib, void *d)
244 struct find_proto_data *data = d;
245 data->ret = library_get_prototype(lib, data->name);
246 return CBS_STOP_IF(data->ret != NULL);
249 static struct prototype *
250 lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
252 if (libsym->proto != NULL)
253 return libsym->proto;
255 struct library *lib = libsym->lib;
257 struct find_proto_data data = { libsym->name };
258 data.ret = library_get_prototype(lib, libsym->name);
260 && libsym->plt_type == LS_TOPLT_EXEC)
261 proc_each_library(proc, NULL, find_proto_cb, &data);
262 if (data.ret != NULL)
266 return build_default_prototype();
270 output_line(struct process *proc, const char *fmt, ...)
275 if (current_proc != NULL) {
276 if (current_proc->callstack[current_depth].return_addr)
277 fprintf(options.output, " <unfinished ...>\n");
279 fprintf(options.output, " <no return ...>\n");
285 begin_of_line(proc, 0, 0);
289 vfprintf(options.output, fmt, args);
290 fprintf(options.output, "\n");
298 if (current_column < col) {
299 fprintf(options.output, "%*s", col - current_column, "");
304 output_error(FILE *stream)
306 return fprintf(stream, "?");
310 fetch_simple_param(enum tof type, struct process *proc,
311 struct fetch_context *context,
312 struct value_dict *arguments,
313 struct arg_type_info *info, int own,
314 struct value *valuep)
316 /* Arrays decay into pointers per C standard. We check for
317 * this here, because here we also capture arrays that come
318 * from parameter packs. */
319 if (info->type == ARGTYPE_ARRAY) {
320 struct arg_type_info *tmp = malloc(sizeof(*tmp));
322 type_init_pointer(tmp, info, own);
323 tmp->lens = info->lens;
330 value_init(&value, proc, NULL, info, own);
331 if (fetch_arg_next(context, type, proc, info, &value) < 0)
334 if (val_dict_push_next(arguments, &value) < 0) {
335 value_destroy(&value);
346 fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
348 if (*params_leftp == -1)
349 *params_leftp = val_dict_count(arguments);
353 fetch_param_pack(enum tof type, struct process *proc,
354 struct fetch_context *context,
355 struct value_dict *arguments, struct param *param,
356 ssize_t *params_leftp)
358 struct param_enum *e = param_pack_init(param, arguments);
365 struct arg_type_info *info = malloc(sizeof(*info));
367 || param_pack_next(param, e, info, &insert_stop) < 0) {
375 fetch_param_stop(arguments, params_leftp);
377 if (info->type == ARGTYPE_VOID) {
384 if (fetch_simple_param(type, proc, context, arguments,
389 switch (param_pack_stop(param, e, &val)) {
402 param_pack_done(param, e);
407 fetch_one_param(enum tof type, struct process *proc,
408 struct fetch_context *context,
409 struct value_dict *arguments, struct param *param,
410 ssize_t *params_leftp)
412 switch (param->flavor) {
414 case PARAM_FLAVOR_TYPE:
415 return fetch_simple_param(type, proc, context, arguments,
416 param->u.type.type, 0, NULL);
418 case PARAM_FLAVOR_PACK:
419 if (fetch_param_pack_start(context,
420 param->u.pack.ppflavor) < 0)
422 rc = fetch_param_pack(type, proc, context, arguments,
423 param, params_leftp);
424 fetch_param_pack_end(context);
427 case PARAM_FLAVOR_STOP:
428 fetch_param_stop(arguments, params_leftp);
432 assert(!"Invalid param flavor!");
436 struct fetch_one_param_data
438 struct process *proc;
439 struct fetch_context *context;
440 struct value_dict *arguments;
441 ssize_t *params_leftp;
445 static enum callback_status
446 fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
448 struct fetch_one_param_data *cb_data = data;
449 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
451 cb_data->arguments, param,
452 cb_data->params_leftp) < 0);
456 fetch_params(enum tof type, struct process *proc,
457 struct fetch_context *context,
458 struct value_dict *arguments, struct prototype *func,
459 ssize_t *params_leftp)
461 struct fetch_one_param_data cb_data
462 = { proc, context, arguments, params_leftp, type };
463 if (prototype_each_param(func, NULL,
464 &fetch_one_param_cb, &cb_data) != NULL)
467 /* Implicit stop at the end of parameter list. */
468 fetch_param_stop(arguments, params_leftp);
473 struct format_argument_data
476 struct value_dict *arguments;
480 format_argument_cb(FILE *stream, void *ptr)
482 struct format_argument_data *data = ptr;
483 int o = format_argument(stream, data->value, data->arguments);
485 o = output_error(stream);
490 output_params(struct value_dict *arguments, size_t start, size_t end,
494 for (i = start; i < end; ++i) {
495 struct value *value = val_dict_get_num(arguments, i);
499 struct format_argument_data data = { value, arguments };
500 int o = delim_output(options.output, need_delimp,
501 format_argument_cb, &data);
510 output_left(enum tof type, struct process *proc,
511 struct library_symbol *libsym)
513 assert(! options.summary);
516 fprintf(options.output, " <unfinished ...>\n");
520 current_depth = proc->callstack_depth;
521 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
522 if (!options.hide_caller && libsym->lib != NULL
523 && libsym->plt_type != LS_TOPLT_NONE)
524 /* We don't terribly mind failing this. */
525 account_output(¤t_column,
526 fprintf(options.output, "%s->",
527 libsym->lib->soname));
529 const char *name = libsym->name;
531 if (options.demangle)
532 name = my_demangle(libsym->name);
534 if (account_output(¤t_column,
535 fprintf(options.output, "%s", name)) < 0)
538 if (libsym->lib != NULL
539 && libsym->lib->type != LT_LIBTYPE_MAIN
540 && libsym->plt_type == LS_TOPLT_NONE
541 && account_output(¤t_column,
542 fprintf(options.output, "@%s",
543 libsym->lib->soname)) < 0)
544 /* We do mind failing this though. */
547 account_output(¤t_column, fprintf(options.output, "("));
549 struct prototype *func = lookup_symbol_prototype(proc->leader, libsym);
552 account_output(¤t_column, fprintf(options.output, "???"));
556 struct fetch_context *context = fetch_arg_init(type, proc,
561 struct value_dict *arguments = malloc(sizeof(*arguments));
562 if (arguments == NULL) {
563 fetch_arg_done(context);
566 val_dict_init(arguments);
568 ssize_t params_left = -1;
570 if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0
571 || output_params(arguments, 0, params_left, &need_delim) < 0) {
572 val_dict_destroy(arguments);
573 fetch_arg_done(context);
578 struct callstack_element *stel
579 = &proc->callstack[proc->callstack_depth - 1];
580 stel->fetch_context = context;
581 stel->arguments = arguments;
582 stel->out.params_left = params_left;
583 stel->out.need_delim = need_delim;
586 #if defined(HAVE_LIBDW)
587 /* Prints information about one frame of a thread. Called by
588 dwfl_getthread_frames in output_right. Returns 1 when done (max
589 number of frames reached). Returns -1 on error. Returns 0 on
590 success (if there are more frames in the thread, call us again). */
592 frame_callback (Dwfl_Frame *state, void *arg)
597 int *frames = (int *) arg;
599 if (!dwfl_frame_pc(state, &pc, &isactivation))
605 Dwfl *dwfl = dwfl_thread_dwfl(dwfl_frame_thread(state));
606 Dwfl_Module *mod = dwfl_addrmodule(dwfl, pc);
607 const char *modname = NULL;
608 const char *symname = NULL;
612 modname = dwfl_module_info(mod, NULL, NULL, NULL, NULL,
614 symname = dwfl_module_addrinfo(mod, pc, &off, &sym,
618 /* This mimics the output produced by libunwind below. */
619 fprintf(options.output, " > %s(%s+0x%" PRIx64 ") [%" PRIx64 "]\n",
620 modname, symname, off, pc);
622 /* See if we can extract the source line too and print it on
623 the next line if we can find it. */
625 Dwfl_Line *l = dwfl_module_getsrc(mod, pc);
629 const char *src = dwfl_lineinfo(l, NULL, &line, &col,
632 fprintf(options.output, "\t%s", src);
634 fprintf(options.output, ":%d", line);
636 fprintf(options.output,
639 fprintf(options.output, "\n");
645 /* Max number of frames to print reached? */
646 if ((*frames)-- == 0)
651 #endif /* defined(HAVE_LIBDW) */
654 output_right(enum tof type, struct process *proc, struct library_symbol *libsym,
655 struct timedelta *spent)
657 assert(! options.summary);
659 struct prototype *func = lookup_symbol_prototype(proc, libsym);
663 if (current_proc != NULL
664 && (current_proc != proc
665 || current_depth != proc->callstack_depth)) {
666 fprintf(options.output, " <unfinished ...>\n");
669 if (current_proc != proc) {
670 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
673 fprintf(options.output, "<... %s resumed> ",
674 options.demangle ? my_demangle(libsym->name)
678 fprintf(options.output, "<... %s resumed> ", libsym->name);
682 struct callstack_element *stel
683 = &proc->callstack[proc->callstack_depth - 1];
685 struct fetch_context *context = stel->fetch_context;
687 /* Fetch & enter into dictionary the retval first, so that
688 * other values can use it in expressions. */
690 bool own_retval = false;
691 if (context != NULL) {
692 value_init(&retval, proc, NULL, func->return_info, 0);
694 if (fetch_retval(context, type, proc, func->return_info,
696 value_set_type(&retval, NULL, 0);
697 else if (stel->arguments != NULL
698 && val_dict_push_named(stel->arguments, &retval,
703 if (stel->arguments != NULL)
704 output_params(stel->arguments, stel->out.params_left,
705 val_dict_count(stel->arguments),
706 &stel->out.need_delim);
708 current_column += fprintf(options.output, ") ");
709 tabto(options.align - 1);
710 fprintf(options.output, "= ");
712 if (context != NULL && retval.type != NULL) {
713 struct format_argument_data data = { &retval, stel->arguments };
714 format_argument_cb(options.output, &data);
718 value_destroy(&retval);
721 assert(spent != NULL);
722 fprintf(options.output, " <%lu.%06d>",
723 (unsigned long) spent->tm.tv_sec,
724 (int) spent->tm.tv_usec);
727 fprintf(options.output, "\n");
729 #if defined(HAVE_LIBUNWIND)
730 if (options.bt_depth > 0
731 && proc->unwind_priv != NULL
732 && proc->unwind_as != NULL) {
734 arch_addr_t ip, function_offset;
735 struct library *lib = NULL;
736 int unwind_depth = options.bt_depth;
738 const char *lib_name;
741 /* Verify that we can safely cast arch_addr_t* to
743 (void)sizeof(char[1 - 2*(sizeof(unw_word_t)
744 != sizeof(arch_addr_t))]);
745 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
746 while (unwind_depth) {
748 int rc = unw_get_reg(&cursor, UNW_REG_IP,
751 fprintf(options.output, " > Error: %s\n",
756 /* We are looking for the library with the base address
757 * closest to the current ip. */
758 lib_name = "unmapped_area";
759 distance = (size_t) -1;
760 lib = proc->libraries;
761 while (lib != NULL) {
762 /* N.B.: Assumes sizeof(size_t) ==
763 * sizeof(arch_addr_t).
764 * Keyword: double cast. */
765 if ((ip >= lib->base) &&
766 ((size_t)(ip - lib->base)
768 distance = ip - lib->base;
769 lib_name = lib->pathname;
774 rc = unw_get_proc_name(&cursor, fn_name,
776 (unw_word_t *) &function_offset);
777 if (rc == 0 || rc == -UNW_ENOMEM)
778 fprintf(options.output, " > %s(%s+%p) [%p]\n",
779 lib_name, fn_name, function_offset, ip);
781 fprintf(options.output, " > %s(??\?) [%p]\n",
785 if (unw_step(&cursor) <= 0)
789 fprintf(options.output, "\n");
791 #endif /* defined(HAVE_LIBUNWIND) */
793 #if defined(HAVE_LIBDW)
794 if (options.bt_depth > 0 && proc->leader->dwfl != NULL) {
795 int frames = options.bt_depth;
796 if (dwfl_getthread_frames(proc->leader->dwfl, proc->pid,
797 frame_callback, &frames) < 0) {
798 // Only print an error if we couldn't show anything.
799 // Otherwise just show there might be more...
800 if (frames == options.bt_depth)
802 "dwfl_getthread_frames tid %d: %s\n",
803 proc->pid, dwfl_errmsg(-1));
805 fprintf(options.output, " > [...]\n");
807 fprintf(options.output, "\n");
809 #endif /* defined(HAVE_LIBDW) */
816 delim_output(FILE *stream, int *need_delimp,
817 int (*writer)(FILE *stream, void *data),
822 /* If we don't need a delimiter, then we don't need to go
823 * through a temporary stream. It's all the same whether
824 * WRITER emits anything or not. */
826 o = writer(stream, data);
830 if (memstream_init(&ms) < 0)
832 o = writer(ms.stream, data);
833 if (memstream_close(&ms) < 0)
835 if (o > 0 && ((*need_delimp
836 && account_output(&o, fprintf(stream, ", ")) < 0)
837 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
840 memstream_destroy(&ms);
846 *need_delimp = *need_delimp || o > 0;
851 account_output(int *countp, int c)
859 do_report(const char *filename, unsigned line_no, const char *severity,
860 const char *fmt, va_list args)
863 vsnprintf(buf, sizeof(buf), fmt, args);
864 buf[sizeof(buf) - 1] = 0;
865 if (filename != NULL)
866 output_line(0, "%s:%d: %s: %s",
867 filename, line_no, severity, buf);
869 output_line(0, "%s: %s", severity, buf);
873 report_error(const char *filename, unsigned line_no, const char *fmt, ...)
877 do_report(filename, line_no, "error", fmt, args);
882 report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
886 do_report(filename, line_no, "warning", fmt, args);
891 report_global_error(const char *fmt, ...)
895 do_report(NULL, 0, "error", fmt, args);