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"
53 static struct process *current_proc = NULL;
54 static size_t current_depth = 0;
55 static int current_column = 0;
58 output_indent(struct process *proc)
60 int d = options.indent * (proc->callstack_depth - 1);
61 current_column += fprintf(options.output, "%*s", d, "");
65 begin_of_line(struct process *proc, int is_func, int indent)
71 if ((options.output != stderr) && (opt_p || options.follow)) {
72 current_column += fprintf(options.output, "%u ", proc->pid);
73 } else if (options.follow) {
74 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
78 static struct timeval old_tv = { 0, 0 };
81 gettimeofday(&tv, NULL);
83 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
84 old_tv.tv_sec = tv.tv_sec;
85 old_tv.tv_usec = tv.tv_usec;
87 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
88 if (tv.tv_usec >= old_tv.tv_usec) {
89 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
92 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
94 old_tv.tv_sec = tv.tv_sec;
95 old_tv.tv_usec = tv.tv_usec;
96 current_column += fprintf(options.output, "%3lu.%06d ",
97 (unsigned long)diff.tv_sec,
102 gettimeofday(&tv, NULL);
104 current_column += fprintf(options.output, "%lu.%06d ",
105 (unsigned long)tv.tv_sec,
107 } else if (opt_t > 1) {
108 struct tm *tmp = localtime(&tv.tv_sec);
110 fprintf(options.output, "%02d:%02d:%02d.%06d ",
111 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
114 struct tm *tmp = localtime(&tv.tv_sec);
115 current_column += fprintf(options.output, "%02d:%02d:%02d ",
116 tmp->tm_hour, tmp->tm_min,
122 struct callstack_element *stel
123 = &proc->callstack[proc->callstack_depth - 1];
124 current_column += fprintf(options.output, "[%p] ",
127 current_column += fprintf(options.output, "[%p] ",
128 proc->instruction_pointer);
131 if (options.indent > 0 && indent) {
136 static struct arg_type_info *
137 get_unknown_type(void)
139 static struct arg_type_info *ret = NULL;
143 static struct arg_type_info info;
144 info = *type_get_simple(ARGTYPE_LONG);
145 info.lens = &guess_lens;
150 /* The default prototype is: long X(long, long, long, long). */
151 static struct prototype *
152 build_default_prototype(void)
154 static struct prototype *ret = NULL;
158 static struct prototype proto;
159 prototype_init(&proto);
161 struct arg_type_info *unknown_type = get_unknown_type();
162 assert(unknown_type != NULL);
163 proto.return_info = unknown_type;
164 proto.own_return_info = 0;
166 struct param unknown_param;
167 param_init_type(&unknown_param, unknown_type, 0);
170 for (i = 0; i < 4; ++i)
171 if (prototype_push_param(&proto, &unknown_param) < 0) {
172 report_global_error("build_default_prototype: %s",
174 prototype_destroy(&proto);
183 snip_period(char *buf)
185 char *period = strrchr(buf, '.');
186 if (period != NULL && strcmp(period, ".so") != 0) {
194 static struct prototype *
195 library_get_prototype(struct library *lib, const char *name)
197 if (lib->protolib == NULL) {
198 size_t sz = strlen(lib->soname);
200 memcpy(buf, lib->soname, sz + 1);
203 if (protolib_cache_maybe_load(&g_protocache, buf, 0,
204 true, &lib->protolib) < 0)
206 } while (lib->protolib == NULL
207 && lib->type == LT_LIBTYPE_DSO
208 && snip_period(buf));
210 if (lib->protolib == NULL)
211 lib->protolib = protolib_cache_default(&g_protocache,
214 if (lib->protolib == NULL)
217 return protolib_lookup_prototype(lib->protolib, name,
218 lib->type != LT_LIBTYPE_SYSCALL);
221 struct find_proto_data {
223 struct prototype *ret;
226 static enum callback_status
227 find_proto_cb(struct process *proc, struct library *lib, void *d)
229 struct find_proto_data *data = d;
230 data->ret = library_get_prototype(lib, data->name);
231 return CBS_STOP_IF(data->ret != NULL);
234 static struct prototype *
235 lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
237 if (libsym->proto != NULL)
238 return libsym->proto;
240 struct library *lib = libsym->lib;
242 struct find_proto_data data = { libsym->name };
243 data.ret = library_get_prototype(lib, libsym->name);
245 && libsym->plt_type == LS_TOPLT_EXEC)
246 proc_each_library(proc, NULL, find_proto_cb, &data);
247 if (data.ret != NULL)
251 return build_default_prototype();
255 output_line(struct process *proc, const char *fmt, ...)
260 if (current_proc != NULL) {
261 if (current_proc->callstack[current_depth].return_addr)
262 fprintf(options.output, " <unfinished ...>\n");
264 fprintf(options.output, " <no return ...>\n");
270 begin_of_line(proc, 0, 0);
274 vfprintf(options.output, fmt, args);
275 fprintf(options.output, "\n");
283 if (current_column < col) {
284 fprintf(options.output, "%*s", col - current_column, "");
289 output_error(FILE *stream)
291 return fprintf(stream, "?");
295 fetch_simple_param(enum tof type, struct process *proc,
296 struct fetch_context *context,
297 struct value_dict *arguments,
298 struct arg_type_info *info, int own,
299 struct value *valuep)
301 /* Arrays decay into pointers per C standard. We check for
302 * this here, because here we also capture arrays that come
303 * from parameter packs. */
304 if (info->type == ARGTYPE_ARRAY) {
305 struct arg_type_info *tmp = malloc(sizeof(*tmp));
307 type_init_pointer(tmp, info, own);
308 tmp->lens = info->lens;
315 value_init(&value, proc, NULL, info, own);
316 if (fetch_arg_next(context, type, proc, info, &value) < 0)
319 if (val_dict_push_next(arguments, &value) < 0) {
320 value_destroy(&value);
331 fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
333 if (*params_leftp == -1)
334 *params_leftp = val_dict_count(arguments);
338 fetch_param_pack(enum tof type, struct process *proc,
339 struct fetch_context *context,
340 struct value_dict *arguments, struct param *param,
341 ssize_t *params_leftp)
343 struct param_enum *e = param_pack_init(param, arguments);
350 struct arg_type_info *info = malloc(sizeof(*info));
352 || param_pack_next(param, e, info, &insert_stop) < 0) {
360 fetch_param_stop(arguments, params_leftp);
362 if (info->type == ARGTYPE_VOID) {
369 if (fetch_simple_param(type, proc, context, arguments,
374 switch (param_pack_stop(param, e, &val)) {
387 param_pack_done(param, e);
392 fetch_one_param(enum tof type, struct process *proc,
393 struct fetch_context *context,
394 struct value_dict *arguments, struct param *param,
395 ssize_t *params_leftp)
397 switch (param->flavor) {
399 case PARAM_FLAVOR_TYPE:
400 return fetch_simple_param(type, proc, context, arguments,
401 param->u.type.type, 0, NULL);
403 case PARAM_FLAVOR_PACK:
404 if (fetch_param_pack_start(context,
405 param->u.pack.ppflavor) < 0)
407 rc = fetch_param_pack(type, proc, context, arguments,
408 param, params_leftp);
409 fetch_param_pack_end(context);
412 case PARAM_FLAVOR_STOP:
413 fetch_param_stop(arguments, params_leftp);
417 assert(!"Invalid param flavor!");
421 struct fetch_one_param_data
423 struct process *proc;
424 struct fetch_context *context;
425 struct value_dict *arguments;
426 ssize_t *params_leftp;
430 static enum callback_status
431 fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
433 struct fetch_one_param_data *cb_data = data;
434 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
436 cb_data->arguments, param,
437 cb_data->params_leftp) < 0);
441 fetch_params(enum tof type, struct process *proc,
442 struct fetch_context *context,
443 struct value_dict *arguments, struct prototype *func,
444 ssize_t *params_leftp)
446 struct fetch_one_param_data cb_data
447 = { proc, context, arguments, params_leftp, type };
448 if (prototype_each_param(func, NULL,
449 &fetch_one_param_cb, &cb_data) != NULL)
452 /* Implicit stop at the end of parameter list. */
453 fetch_param_stop(arguments, params_leftp);
458 struct format_argument_data
461 struct value_dict *arguments;
465 format_argument_cb(FILE *stream, void *ptr)
467 struct format_argument_data *data = ptr;
468 int o = format_argument(stream, data->value, data->arguments);
470 o = output_error(stream);
475 output_params(struct value_dict *arguments, size_t start, size_t end,
479 for (i = start; i < end; ++i) {
480 struct value *value = val_dict_get_num(arguments, i);
484 struct format_argument_data data = { value, arguments };
485 int o = delim_output(options.output, need_delimp,
486 format_argument_cb, &data);
495 output_left(enum tof type, struct process *proc,
496 struct library_symbol *libsym)
498 assert(! options.summary);
501 fprintf(options.output, " <unfinished ...>\n");
505 current_depth = proc->callstack_depth;
506 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
507 if (!options.hide_caller && libsym->lib != NULL
508 && libsym->plt_type != LS_TOPLT_NONE)
509 /* We don't terribly mind failing this. */
510 account_output(¤t_column,
511 fprintf(options.output, "%s->",
512 libsym->lib->soname));
514 const char *name = libsym->name;
516 if (options.demangle)
517 name = my_demangle(libsym->name);
519 if (account_output(¤t_column,
520 fprintf(options.output, "%s", name)) < 0)
523 if (libsym->lib != NULL
524 && libsym->lib->type != LT_LIBTYPE_MAIN
525 && libsym->plt_type == LS_TOPLT_NONE
526 && account_output(¤t_column,
527 fprintf(options.output, "@%s",
528 libsym->lib->soname)) < 0)
529 /* We do mind failing this though. */
532 account_output(¤t_column, fprintf(options.output, "("));
534 struct prototype *func = lookup_symbol_prototype(proc->leader, libsym);
537 account_output(¤t_column, fprintf(options.output, "???"));
541 struct fetch_context *context = fetch_arg_init(type, proc,
546 struct value_dict *arguments = malloc(sizeof(*arguments));
547 if (arguments == NULL) {
548 fetch_arg_done(context);
551 val_dict_init(arguments);
553 ssize_t params_left = -1;
555 if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0
556 || output_params(arguments, 0, params_left, &need_delim) < 0) {
557 val_dict_destroy(arguments);
558 fetch_arg_done(context);
563 struct callstack_element *stel
564 = &proc->callstack[proc->callstack_depth - 1];
565 stel->fetch_context = context;
566 stel->arguments = arguments;
567 stel->out.params_left = params_left;
568 stel->out.need_delim = need_delim;
571 #if defined(HAVE_LIBDW)
572 /* Prints information about one frame of a thread. Called by
573 dwfl_getthread_frames in output_right. Returns 1 when done (max
574 number of frames reached). Returns -1 on error. Returns 0 on
575 success (if there are more frames in the thread, call us again). */
577 frame_callback (Dwfl_Frame *state, void *arg)
582 int *frames = (int *) arg;
584 if (!dwfl_frame_pc(state, &pc, &isactivation))
590 Dwfl *dwfl = dwfl_thread_dwfl(dwfl_frame_thread(state));
591 Dwfl_Module *mod = dwfl_addrmodule(dwfl, pc);
592 const char *modname = NULL;
593 const char *symname = NULL;
597 modname = dwfl_module_info(mod, NULL, NULL, NULL, NULL,
599 symname = dwfl_module_addrinfo(mod, pc, &off, &sym,
603 /* This mimics the output produced by libunwind below. */
604 fprintf(options.output, " > %s(%s+0x%" PRIx64 ") [%" PRIx64 "]\n",
605 modname, symname, off, pc);
607 /* See if we can extract the source line too and print it on
608 the next line if we can find it. */
610 Dwfl_Line *l = dwfl_module_getsrc(mod, pc);
614 const char *src = dwfl_lineinfo(l, NULL, &line, &col,
617 fprintf(options.output, "\t%s", src);
619 fprintf(options.output, ":%d", line);
621 fprintf(options.output,
624 fprintf(options.output, "\n");
630 /* Max number of frames to print reached? */
631 if ((*frames)-- == 0)
636 #endif /* defined(HAVE_LIBDW) */
639 output_right(enum tof type, struct process *proc, struct library_symbol *libsym,
640 struct timedelta *spent)
642 assert(! options.summary);
644 struct prototype *func = lookup_symbol_prototype(proc, libsym);
648 if (current_proc != NULL
649 && (current_proc != proc
650 || current_depth != proc->callstack_depth)) {
651 fprintf(options.output, " <unfinished ...>\n");
654 if (current_proc != proc) {
655 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
658 fprintf(options.output, "<... %s resumed> ",
659 options.demangle ? my_demangle(libsym->name)
663 fprintf(options.output, "<... %s resumed> ", libsym->name);
667 struct callstack_element *stel
668 = &proc->callstack[proc->callstack_depth - 1];
670 struct fetch_context *context = stel->fetch_context;
672 /* Fetch & enter into dictionary the retval first, so that
673 * other values can use it in expressions. */
675 bool own_retval = false;
676 if (context != NULL) {
677 value_init(&retval, proc, NULL, func->return_info, 0);
679 if (fetch_retval(context, type, proc, func->return_info,
681 value_set_type(&retval, NULL, 0);
682 else if (stel->arguments != NULL
683 && val_dict_push_named(stel->arguments, &retval,
688 if (stel->arguments != NULL)
689 output_params(stel->arguments, stel->out.params_left,
690 val_dict_count(stel->arguments),
691 &stel->out.need_delim);
693 current_column += fprintf(options.output, ") ");
694 tabto(options.align - 1);
695 fprintf(options.output, "= ");
697 if (context != NULL && retval.type != NULL) {
698 struct format_argument_data data = { &retval, stel->arguments };
699 format_argument_cb(options.output, &data);
703 value_destroy(&retval);
706 assert(spent != NULL);
707 fprintf(options.output, " <%lu.%06d>",
708 (unsigned long) spent->tm.tv_sec,
709 (int) spent->tm.tv_usec);
712 fprintf(options.output, "\n");
714 #if defined(HAVE_LIBUNWIND)
715 if (options.bt_depth > 0
716 && proc->unwind_priv != NULL
717 && proc->unwind_as != NULL) {
719 arch_addr_t ip, function_offset;
720 struct library *lib = NULL;
721 int unwind_depth = options.bt_depth;
723 const char *lib_name;
726 /* Verify that we can safely cast arch_addr_t* to
728 (void)sizeof(char[1 - 2*(sizeof(unw_word_t)
729 != sizeof(arch_addr_t))]);
730 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
731 while (unwind_depth) {
733 int rc = unw_get_reg(&cursor, UNW_REG_IP,
736 fprintf(options.output, " > Error: %s\n",
741 /* We are looking for the library with the base address
742 * closest to the current ip. */
743 lib_name = "unmapped_area";
744 distance = (size_t) -1;
745 lib = proc->libraries;
746 while (lib != NULL) {
747 /* N.B.: Assumes sizeof(size_t) ==
748 * sizeof(arch_addr_t).
749 * Keyword: double cast. */
750 if ((ip >= lib->base) &&
751 ((size_t)(ip - lib->base)
753 distance = ip - lib->base;
754 lib_name = lib->pathname;
759 rc = unw_get_proc_name(&cursor, fn_name,
761 (unw_word_t *) &function_offset);
762 if (rc == 0 || rc == -UNW_ENOMEM)
763 fprintf(options.output, " > %s(%s+%p) [%p]\n",
764 lib_name, fn_name, function_offset, ip);
766 fprintf(options.output, " > %s(??\?) [%p]\n",
770 if (unw_step(&cursor) <= 0)
774 fprintf(options.output, "\n");
776 #endif /* defined(HAVE_LIBUNWIND) */
778 #if defined(HAVE_LIBDW)
779 if (options.bt_depth > 0 && proc->leader->dwfl != NULL) {
780 int frames = options.bt_depth;
781 if (dwfl_getthread_frames(proc->leader->dwfl, proc->pid,
782 frame_callback, &frames) < 0) {
783 // Only print an error if we couldn't show anything.
784 // Otherwise just show there might be more...
785 if (frames == options.bt_depth)
787 "dwfl_getthread_frames tid %d: %s\n",
788 proc->pid, dwfl_errmsg(-1));
790 fprintf(options.output, " > [...]\n");
792 fprintf(options.output, "\n");
794 #endif /* defined(HAVE_LIBDW) */
801 delim_output(FILE *stream, int *need_delimp,
802 int (*writer)(FILE *stream, void *data),
807 /* If we don't need a delimiter, then we don't need to go
808 * through a temporary stream. It's all the same whether
809 * WRITER emits anything or not. */
811 o = writer(stream, data);
815 if (memstream_init(&ms) < 0)
817 o = writer(ms.stream, data);
818 if (memstream_close(&ms) < 0)
820 if (o > 0 && ((*need_delimp
821 && account_output(&o, fprintf(stream, ", ")) < 0)
822 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
825 memstream_destroy(&ms);
831 *need_delimp = *need_delimp || o > 0;
836 account_output(int *countp, int c)
844 do_report(const char *filename, unsigned line_no, const char *severity,
845 const char *fmt, va_list args)
848 vsnprintf(buf, sizeof(buf), fmt, args);
849 buf[sizeof(buf) - 1] = 0;
850 if (filename != NULL)
851 output_line(0, "%s:%d: %s: %s",
852 filename, line_no, severity, buf);
854 output_line(0, "%s: %s", severity, buf);
858 report_error(const char *filename, unsigned line_no, const char *fmt, ...)
862 do_report(filename, line_no, "error", fmt, args);
867 report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
871 do_report(filename, line_no, "warning", fmt, args);
876 report_global_error(const char *fmt, ...)
880 do_report(NULL, 0, "error", fmt, args);