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"
50 #include "value_dict.h"
52 static struct process *current_proc = NULL;
53 static size_t current_depth = 0;
54 static int current_column = 0;
57 output_indent(struct process *proc)
59 int d = options.indent * (proc->callstack_depth - 1);
60 current_column += fprintf(options.output, "%*s", d, "");
64 begin_of_line(struct process *proc, int is_func, int indent)
70 if ((options.output != stderr) && (opt_p || options.follow)) {
71 current_column += fprintf(options.output, "%u ", proc->pid);
72 } else if (options.follow) {
73 current_column += fprintf(options.output, "[pid %u] ", proc->pid);
77 static struct timeval old_tv = { 0, 0 };
80 gettimeofday(&tv, NULL);
82 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
83 old_tv.tv_sec = tv.tv_sec;
84 old_tv.tv_usec = tv.tv_usec;
86 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
87 if (tv.tv_usec >= old_tv.tv_usec) {
88 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
91 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
93 old_tv.tv_sec = tv.tv_sec;
94 old_tv.tv_usec = tv.tv_usec;
95 current_column += fprintf(options.output, "%3lu.%06d ",
96 (unsigned long)diff.tv_sec,
101 gettimeofday(&tv, NULL);
103 current_column += fprintf(options.output, "%lu.%06d ",
104 (unsigned long)tv.tv_sec,
106 } else if (opt_t > 1) {
107 struct tm *tmp = localtime(&tv.tv_sec);
109 fprintf(options.output, "%02d:%02d:%02d.%06d ",
110 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
113 struct tm *tmp = localtime(&tv.tv_sec);
114 current_column += fprintf(options.output, "%02d:%02d:%02d ",
115 tmp->tm_hour, tmp->tm_min,
121 struct callstack_element *stel
122 = &proc->callstack[proc->callstack_depth - 1];
123 current_column += fprintf(options.output, "[%p] ",
126 current_column += fprintf(options.output, "[%p] ",
127 proc->instruction_pointer);
130 if (options.indent > 0 && indent) {
135 static struct arg_type_info *
136 get_unknown_type(void)
138 static struct arg_type_info *ret = NULL;
142 static struct arg_type_info info;
143 info = *type_get_simple(ARGTYPE_LONG);
144 info.lens = &guess_lens;
149 /* The default prototype is: long X(long, long, long, long). */
150 static struct prototype *
151 build_default_prototype(void)
153 static struct prototype *ret = NULL;
157 static struct prototype proto;
158 prototype_init(&proto);
160 struct arg_type_info *unknown_type = get_unknown_type();
161 assert(unknown_type != NULL);
162 proto.return_info = unknown_type;
163 proto.own_return_info = 0;
165 struct param unknown_param;
166 param_init_type(&unknown_param, unknown_type, 0);
169 for (i = 0; i < 4; ++i)
170 if (prototype_push_param(&proto, &unknown_param) < 0) {
171 report_global_error("build_default_prototype: %s",
173 prototype_destroy(&proto);
182 snip_period(char *buf)
184 char *period = strrchr(buf, '.');
185 if (period != NULL && strcmp(period, ".so") != 0) {
193 static struct prototype *
194 library_get_prototype(struct library *lib, const char *name)
196 if (lib->protolib == NULL) {
197 size_t sz = strlen(lib->soname);
199 memcpy(buf, lib->soname, sz + 1);
202 if (protolib_cache_maybe_load(&g_protocache, buf, 0,
203 true, &lib->protolib) < 0)
205 } while (lib->protolib == NULL
206 && lib->type == LT_LIBTYPE_DSO
207 && snip_period(buf));
209 if (lib->protolib == NULL)
210 lib->protolib = protolib_cache_default(&g_protocache,
213 if (lib->protolib == NULL)
216 return protolib_lookup_prototype(lib->protolib, name,
217 lib->type != LT_LIBTYPE_SYSCALL);
220 struct find_proto_data {
222 struct prototype *ret;
225 static enum callback_status
226 find_proto_cb(struct process *proc, struct library *lib, void *d)
228 struct find_proto_data *data = d;
229 data->ret = library_get_prototype(lib, data->name);
230 return CBS_STOP_IF(data->ret != NULL);
233 static struct prototype *
234 lookup_symbol_prototype(struct process *proc, struct library_symbol *libsym)
236 if (libsym->proto != NULL)
237 return libsym->proto;
239 struct library *lib = libsym->lib;
241 struct find_proto_data data = { libsym->name };
242 data.ret = library_get_prototype(lib, libsym->name);
244 && libsym->plt_type == LS_TOPLT_EXEC)
245 proc_each_library(proc, NULL, find_proto_cb, &data);
246 if (data.ret != NULL)
250 return build_default_prototype();
254 output_line(struct process *proc, const char *fmt, ...)
259 if (current_proc != NULL) {
260 if (current_proc->callstack[current_depth].return_addr)
261 fprintf(options.output, " <unfinished ...>\n");
263 fprintf(options.output, " <no return ...>\n");
269 begin_of_line(proc, 0, 0);
273 vfprintf(options.output, fmt, args);
274 fprintf(options.output, "\n");
282 if (current_column < col) {
283 fprintf(options.output, "%*s", col - current_column, "");
288 output_error(FILE *stream)
290 return fprintf(stream, "?");
294 fetch_simple_param(enum tof type, struct process *proc,
295 struct fetch_context *context,
296 struct value_dict *arguments,
297 struct arg_type_info *info, int own,
298 struct value *valuep)
300 /* Arrays decay into pointers per C standard. We check for
301 * this here, because here we also capture arrays that come
302 * from parameter packs. */
303 if (info->type == ARGTYPE_ARRAY) {
304 struct arg_type_info *tmp = malloc(sizeof(*tmp));
306 type_init_pointer(tmp, info, own);
307 tmp->lens = info->lens;
314 value_init(&value, proc, NULL, info, own);
315 if (fetch_arg_next(context, type, proc, info, &value) < 0)
318 if (val_dict_push_next(arguments, &value) < 0) {
319 value_destroy(&value);
330 fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
332 if (*params_leftp == -1)
333 *params_leftp = val_dict_count(arguments);
337 fetch_param_pack(enum tof type, struct process *proc,
338 struct fetch_context *context,
339 struct value_dict *arguments, struct param *param,
340 ssize_t *params_leftp)
342 struct param_enum *e = param_pack_init(param, arguments);
349 struct arg_type_info *info = malloc(sizeof(*info));
351 || param_pack_next(param, e, info, &insert_stop) < 0) {
359 fetch_param_stop(arguments, params_leftp);
361 if (info->type == ARGTYPE_VOID) {
368 if (fetch_simple_param(type, proc, context, arguments,
373 switch (param_pack_stop(param, e, &val)) {
386 param_pack_done(param, e);
391 fetch_one_param(enum tof type, struct process *proc,
392 struct fetch_context *context,
393 struct value_dict *arguments, struct param *param,
394 ssize_t *params_leftp)
396 switch (param->flavor) {
398 case PARAM_FLAVOR_TYPE:
399 return fetch_simple_param(type, proc, context, arguments,
400 param->u.type.type, 0, NULL);
402 case PARAM_FLAVOR_PACK:
403 if (fetch_param_pack_start(context,
404 param->u.pack.ppflavor) < 0)
406 rc = fetch_param_pack(type, proc, context, arguments,
407 param, params_leftp);
408 fetch_param_pack_end(context);
411 case PARAM_FLAVOR_STOP:
412 fetch_param_stop(arguments, params_leftp);
416 assert(!"Invalid param flavor!");
420 struct fetch_one_param_data
422 struct process *proc;
423 struct fetch_context *context;
424 struct value_dict *arguments;
425 ssize_t *params_leftp;
429 static enum callback_status
430 fetch_one_param_cb(struct prototype *proto, struct param *param, void *data)
432 struct fetch_one_param_data *cb_data = data;
433 return CBS_STOP_IF(fetch_one_param(cb_data->tof, cb_data->proc,
435 cb_data->arguments, param,
436 cb_data->params_leftp) < 0);
440 fetch_params(enum tof type, struct process *proc,
441 struct fetch_context *context,
442 struct value_dict *arguments, struct prototype *func,
443 ssize_t *params_leftp)
445 struct fetch_one_param_data cb_data
446 = { proc, context, arguments, params_leftp, type };
447 if (prototype_each_param(func, NULL,
448 &fetch_one_param_cb, &cb_data) != NULL)
451 /* Implicit stop at the end of parameter list. */
452 fetch_param_stop(arguments, params_leftp);
457 struct format_argument_data
460 struct value_dict *arguments;
464 format_argument_cb(FILE *stream, void *ptr)
466 struct format_argument_data *data = ptr;
467 int o = format_argument(stream, data->value, data->arguments);
469 o = output_error(stream);
474 output_params(struct value_dict *arguments, size_t start, size_t end,
478 for (i = start; i < end; ++i) {
479 struct value *value = val_dict_get_num(arguments, i);
483 struct format_argument_data data = { value, arguments };
484 int o = delim_output(options.output, need_delimp,
485 format_argument_cb, &data);
494 output_left(enum tof type, struct process *proc,
495 struct library_symbol *libsym)
497 assert(! options.summary);
500 fprintf(options.output, " <unfinished ...>\n");
504 current_depth = proc->callstack_depth;
505 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
506 if (!options.hide_caller && libsym->lib != NULL
507 && libsym->plt_type != LS_TOPLT_NONE)
508 /* We don't terribly mind failing this. */
509 account_output(¤t_column,
510 fprintf(options.output, "%s->",
511 libsym->lib->soname));
513 const char *name = libsym->name;
515 if (options.demangle)
516 name = my_demangle(libsym->name);
518 if (account_output(¤t_column,
519 fprintf(options.output, "%s", name)) < 0)
522 if (libsym->lib != NULL
523 && libsym->lib->type != LT_LIBTYPE_MAIN
524 && libsym->plt_type == LS_TOPLT_NONE
525 && account_output(¤t_column,
526 fprintf(options.output, "@%s",
527 libsym->lib->soname)) < 0)
528 /* We do mind failing this though. */
531 account_output(¤t_column, fprintf(options.output, "("));
533 struct prototype *func = lookup_symbol_prototype(proc, libsym);
536 account_output(¤t_column, fprintf(options.output, "???"));
540 struct fetch_context *context = fetch_arg_init(type, proc,
545 struct value_dict *arguments = malloc(sizeof(*arguments));
546 if (arguments == NULL) {
547 fetch_arg_done(context);
550 val_dict_init(arguments);
552 ssize_t params_left = -1;
554 if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0
555 || output_params(arguments, 0, params_left, &need_delim) < 0) {
556 val_dict_destroy(arguments);
557 fetch_arg_done(context);
562 struct callstack_element *stel
563 = &proc->callstack[proc->callstack_depth - 1];
564 stel->fetch_context = context;
565 stel->arguments = arguments;
566 stel->out.params_left = params_left;
567 stel->out.need_delim = need_delim;
571 output_right(enum tof type, struct process *proc, struct library_symbol *libsym,
572 struct timedelta *spent)
574 assert(! options.summary);
576 struct prototype *func = lookup_symbol_prototype(proc, libsym);
580 if (current_proc != NULL
581 && (current_proc != proc
582 || current_depth != proc->callstack_depth)) {
583 fprintf(options.output, " <unfinished ...>\n");
586 if (current_proc != proc) {
587 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
590 fprintf(options.output, "<... %s resumed> ",
591 options.demangle ? my_demangle(libsym->name)
595 fprintf(options.output, "<... %s resumed> ", libsym->name);
599 struct callstack_element *stel
600 = &proc->callstack[proc->callstack_depth - 1];
602 struct fetch_context *context = stel->fetch_context;
604 /* Fetch & enter into dictionary the retval first, so that
605 * other values can use it in expressions. */
608 if (context != NULL) {
609 value_init(&retval, proc, NULL, func->return_info, 0);
611 if (fetch_retval(context, type, proc, func->return_info,
613 value_set_type(&retval, NULL, 0);
614 else if (stel->arguments != NULL
615 && val_dict_push_named(stel->arguments, &retval,
620 if (stel->arguments != NULL)
621 output_params(stel->arguments, stel->out.params_left,
622 val_dict_count(stel->arguments),
623 &stel->out.need_delim);
625 current_column += fprintf(options.output, ") ");
626 tabto(options.align - 1);
627 fprintf(options.output, "= ");
629 if (context != NULL && retval.type != NULL) {
630 struct format_argument_data data = { &retval, stel->arguments };
631 format_argument_cb(options.output, &data);
635 value_destroy(&retval);
638 assert(spent != NULL);
639 fprintf(options.output, " <%lu.%06d>",
640 (unsigned long) spent->tm.tv_sec,
641 (int) spent->tm.tv_usec);
644 fprintf(options.output, "\n");
646 #if defined(HAVE_LIBUNWIND)
647 if (options.bt_depth > 0
648 && proc->unwind_priv != NULL
649 && proc->unwind_as != NULL) {
651 arch_addr_t ip, function_offset;
652 struct library *lib = NULL;
653 int unwind_depth = options.bt_depth;
655 const char *lib_name;
658 /* Verify that we can safely cast arch_addr_t* to
660 (void)sizeof(char[1 - 2*(sizeof(unw_word_t)
661 != sizeof(arch_addr_t))]);
662 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
663 while (unwind_depth) {
664 unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip);
665 unw_get_proc_name(&cursor, fn_name, sizeof(fn_name),
666 (unw_word_t *) &function_offset);
668 /* We are looking for the library with the base address
669 * closest to the current ip. */
670 lib_name = "unmapped_area";
671 distance = (size_t) -1;
672 lib = proc->libraries;
673 while (lib != NULL) {
674 /* N.B.: Assumes sizeof(size_t) ==
675 * sizeof(arch_addr_t).
676 * Keyword: double cast. */
677 if ((ip >= lib->base) &&
678 ((size_t)(ip - lib->base)
680 distance = ip - lib->base;
681 lib_name = lib->pathname;
686 fprintf(options.output, " > %s(%s+0x%p) [%p]\n",
687 lib_name, fn_name, function_offset, ip);
689 if (unw_step(&cursor) <= 0)
693 fprintf(options.output, "\n");
695 #endif /* defined(HAVE_LIBUNWIND) */
702 delim_output(FILE *stream, int *need_delimp,
703 int (*writer)(FILE *stream, void *data),
708 /* If we don't need a delimiter, then we don't need to go
709 * through a temporary stream. It's all the same whether
710 * WRITER emits anything or not. */
712 o = writer(stream, data);
716 if (memstream_init(&ms) < 0)
718 o = writer(ms.stream, data);
719 if (memstream_close(&ms) < 0)
721 if (o > 0 && ((*need_delimp
722 && account_output(&o, fprintf(stream, ", ")) < 0)
723 || fwrite(ms.buf, 1, ms.size, stream) != ms.size))
726 memstream_destroy(&ms);
732 *need_delimp = *need_delimp || o > 0;
737 account_output(int *countp, int c)
745 do_report(const char *filename, unsigned line_no, const char *severity,
746 const char *fmt, va_list args)
749 vsnprintf(buf, sizeof(buf), fmt, args);
750 buf[sizeof(buf) - 1] = 0;
751 if (filename != NULL)
752 output_line(0, "%s:%d: %s: %s",
753 filename, line_no, severity, buf);
755 output_line(0, "%s: %s", severity, buf);
759 report_error(const char *filename, unsigned line_no, const char *fmt, ...)
763 do_report(filename, line_no, "error", fmt, args);
768 report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
772 do_report(filename, line_no, "warning", fmt, args);
777 report_global_error(const char *fmt, ...)
781 do_report(NULL, 0, "error", fmt, args);