2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 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
25 /* glibc before 2.10, eglibc and uClibc all need _GNU_SOURCE defined
26 * for open_memstream to become visible. */
46 #include "value_dict.h"
49 #include "lens_default.h"
51 /* TODO FIXME XXX: include in common.h: */
52 extern struct timeval current_time_spent;
54 Dict *dict_opt_c = NULL;
56 static 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(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);
82 static struct timeval old_tv = { 0, 0 };
85 gettimeofday(&tv, &tz);
87 if (old_tv.tv_sec == 0 && old_tv.tv_usec == 0) {
88 old_tv.tv_sec = tv.tv_sec;
89 old_tv.tv_usec = tv.tv_usec;
91 diff.tv_sec = tv.tv_sec - old_tv.tv_sec;
92 if (tv.tv_usec >= old_tv.tv_usec) {
93 diff.tv_usec = tv.tv_usec - old_tv.tv_usec;
96 diff.tv_usec = 1000000 + tv.tv_usec - old_tv.tv_usec;
98 old_tv.tv_sec = tv.tv_sec;
99 old_tv.tv_usec = tv.tv_usec;
100 current_column += fprintf(options.output, "%3lu.%06d ",
101 diff.tv_sec, (int)diff.tv_usec);
107 gettimeofday(&tv, &tz);
109 current_column += fprintf(options.output, "%lu.%06d ",
110 tv.tv_sec, (int)tv.tv_usec);
111 } else if (opt_t > 1) {
112 struct tm *tmp = localtime(&tv.tv_sec);
114 fprintf(options.output, "%02d:%02d:%02d.%06d ",
115 tmp->tm_hour, tmp->tm_min, tmp->tm_sec,
118 struct tm *tmp = localtime(&tv.tv_sec);
119 current_column += fprintf(options.output, "%02d:%02d:%02d ",
120 tmp->tm_hour, tmp->tm_min,
126 current_column += fprintf(options.output, "[%p] ",
129 current_column += fprintf(options.output, "[%p] ",
130 proc->instruction_pointer);
132 if (options.indent > 0 && indent) {
137 static struct arg_type_info *
138 get_unknown_type(void)
140 static struct arg_type_info *info = NULL;
142 info = malloc(sizeof(*info));
144 report_global_error("malloc: %s", strerror(errno));
147 *info = *type_get_simple(ARGTYPE_LONG);
148 info->lens = &guess_lens;
153 /* The default prototype is: long X(long, long, long, long). */
155 build_default_prototype(void)
157 Function *ret = malloc(sizeof(*ret));
161 memset(ret, 0, sizeof(*ret));
163 struct arg_type_info *unknown_type = get_unknown_type();
165 ret->return_info = unknown_type;
166 ret->own_return_info = 0;
169 ret->params = malloc(sizeof(*ret->params) * ret->num_params);
170 if (ret->params == NULL)
173 for (i = 0; i < ret->num_params; ++i)
174 param_init_type(&ret->params[i], unknown_type, 0);
179 report_global_error("malloc: %s", strerror(errno));
180 if (ret->params != NULL) {
182 param_destroy(&ret->params[i]);
192 name2func(char const *name) {
194 const char *str1, *str2;
196 for (tmp = list_of_functions; tmp != NULL; tmp = tmp->next) {
199 if (!strcmp(str1, str2))
203 static Function *def = NULL;
205 def = build_default_prototype();
211 output_line(struct Process *proc, const char *fmt, ...)
216 if (current_proc != NULL) {
217 if (current_proc->callstack[current_depth].return_addr)
218 fprintf(options.output, " <unfinished ...>\n");
220 fprintf(options.output, " <no return ...>\n");
226 begin_of_line(proc, 0, 0);
230 vfprintf(options.output, fmt, args);
231 fprintf(options.output, "\n");
239 if (current_column < col) {
240 fprintf(options.output, "%*s", col - current_column, "");
245 output_error(FILE *stream)
247 return fprintf(stream, "?");
251 fetch_simple_param(enum tof type, Process *proc, struct fetch_context *context,
252 struct value_dict *arguments,
253 struct arg_type_info *info, int own,
254 struct value *valuep)
256 /* Arrays decay into pointers per C standard. We check for
257 * this here, because here we also capture arrays that come
258 * from parameter packs. */
259 if (info->type == ARGTYPE_ARRAY) {
260 struct arg_type_info *tmp = malloc(sizeof(*tmp));
262 type_init_pointer(tmp, info, own);
263 tmp->lens = info->lens;
270 value_init(&value, proc, NULL, info, own);
271 if (fetch_arg_next(context, type, proc, info, &value) < 0)
274 if (val_dict_push_next(arguments, &value) < 0) {
275 value_destroy(&value);
286 fetch_param_stop(struct value_dict *arguments, ssize_t *params_leftp)
288 if (*params_leftp == -1)
289 *params_leftp = val_dict_count(arguments);
293 fetch_param_pack(enum tof type, Process *proc, struct fetch_context *context,
294 struct value_dict *arguments, struct param *param,
295 ssize_t *params_leftp)
297 struct param_enum *e = param_pack_init(param, arguments);
304 struct arg_type_info *info = malloc(sizeof(*info));
306 || param_pack_next(param, e, info, &insert_stop) < 0) {
314 fetch_param_stop(arguments, params_leftp);
316 if (info->type == ARGTYPE_VOID) {
323 if (fetch_simple_param(type, proc, context, arguments,
328 switch (param_pack_stop(param, e, &val)) {
341 param_pack_done(param, e);
346 fetch_one_param(enum tof type, Process *proc, struct fetch_context *context,
347 struct value_dict *arguments, struct param *param,
348 ssize_t *params_leftp)
350 switch (param->flavor) {
352 case PARAM_FLAVOR_TYPE:
353 return fetch_simple_param(type, proc, context, arguments,
354 param->u.type.type, 0, NULL);
356 case PARAM_FLAVOR_PACK:
357 if (fetch_param_pack_start(context,
358 param->u.pack.ppflavor) < 0)
360 rc = fetch_param_pack(type, proc, context, arguments,
361 param, params_leftp);
362 fetch_param_pack_end(context);
365 case PARAM_FLAVOR_STOP:
366 fetch_param_stop(arguments, params_leftp);
370 assert(!"Invalid param flavor!");
375 fetch_params(enum tof type, Process *proc, struct fetch_context *context,
376 struct value_dict *arguments, Function *func, ssize_t *params_leftp)
379 for (i = 0; i < func->num_params; ++i)
380 if (fetch_one_param(type, proc, context, arguments,
381 &func->params[i], params_leftp) < 0)
384 /* Implicit stop at the end of parameter list. */
385 fetch_param_stop(arguments, params_leftp);
390 struct format_argument_data
393 struct value_dict *arguments;
397 format_argument_cb(FILE *stream, void *ptr)
399 struct format_argument_data *data = ptr;
400 int o = format_argument(stream, data->value, data->arguments);
402 o = output_error(stream);
407 output_params(struct value_dict *arguments, size_t start, size_t end,
411 for (i = start; i < end; ++i) {
412 struct value *value = val_dict_get_num(arguments, i);
416 struct format_argument_data data = { value, arguments };
417 int o = delim_output(options.output, need_delimp,
418 format_argument_cb, &data);
427 output_left(enum tof type, struct Process *proc,
428 struct library_symbol *libsym)
430 const char *function_name = libsym->name;
433 if (options.summary) {
437 fprintf(options.output, " <unfinished ...>\n");
441 current_depth = proc->callstack_depth;
442 begin_of_line(proc, type == LT_TOF_FUNCTION, 1);
443 if (!options.hide_caller && libsym->lib != NULL
444 && libsym->plt_type != LS_TOPLT_NONE)
445 /* We don't terribly mind failing this. */
446 account_output(¤t_column,
447 fprintf(options.output, "%s->",
448 libsym->lib->soname));
450 const char *name = function_name;
452 if (options.demangle)
453 name = my_demangle(function_name);
455 if (account_output(¤t_column,
456 fprintf(options.output, "%s", name)) < 0)
459 if (libsym->lib != NULL
460 && libsym->lib->type != LT_LIBTYPE_MAIN
461 && libsym->plt_type == LS_TOPLT_NONE
462 && account_output(¤t_column,
463 fprintf(options.output, "@%s",
464 libsym->lib->soname)) < 0)
465 /* We do mind failing this though. */
468 account_output(¤t_column, fprintf(options.output, "("));
470 func = name2func(function_name);
472 account_output(¤t_column, fprintf(options.output, "???"));
476 struct fetch_context *context = fetch_arg_init(type, proc,
478 struct value_dict *arguments = malloc(sizeof(*arguments));
479 if (arguments == NULL)
481 val_dict_init(arguments);
483 ssize_t params_left = -1;
485 if (fetch_params(type, proc, context, arguments, func, ¶ms_left) < 0
486 || output_params(arguments, 0, params_left, &need_delim) < 0) {
487 val_dict_destroy(arguments);
488 fetch_arg_done(context);
493 struct callstack_element *stel
494 = &proc->callstack[proc->callstack_depth - 1];
495 stel->fetch_context = context;
496 stel->arguments = arguments;
497 stel->out.params_left = params_left;
498 stel->out.need_delim = need_delim;
502 output_right(enum tof type, struct Process *proc, struct library_symbol *libsym)
504 const char *function_name = libsym->name;
505 Function *func = name2func(function_name);
509 if (options.summary) {
510 struct opt_c_struct *st;
513 dict_init(dict_key2hash_string,
514 dict_key_cmp_string);
516 st = dict_find_entry(dict_opt_c, function_name);
519 st = malloc(sizeof(struct opt_c_struct));
520 na = strdup(function_name);
526 st->tv.tv_sec = st->tv.tv_usec = 0;
527 dict_enter(dict_opt_c, na, st);
529 if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
530 st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
533 st->tv.tv_usec += current_time_spent.tv_usec;
536 st->tv.tv_sec += current_time_spent.tv_sec;
538 // fprintf(options.output, "%s <%lu.%06d>\n", function_name,
539 // current_time_spent.tv_sec, (int)current_time_spent.tv_usec);
542 if (current_proc && (current_proc != proc ||
543 current_depth != proc->callstack_depth)) {
544 fprintf(options.output, " <unfinished ...>\n");
547 if (current_proc != proc) {
548 begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
551 fprintf(options.output, "<... %s resumed> ",
552 options.demangle ? my_demangle(function_name) : function_name);
555 fprintf(options.output, "<... %s resumed> ", function_name);
559 struct callstack_element *stel
560 = &proc->callstack[proc->callstack_depth - 1];
562 struct fetch_context *context = stel->fetch_context;
564 /* Fetch & enter into dictionary the retval first, so that
565 * other values can use it in expressions. */
568 if (context != NULL) {
569 value_init(&retval, proc, NULL, func->return_info, 0);
571 if (fetch_retval(context, type, proc, func->return_info,
573 value_set_type(&retval, NULL, 0);
574 else if (stel->arguments != NULL
575 && val_dict_push_named(stel->arguments, &retval,
580 if (stel->arguments != NULL)
581 output_params(stel->arguments, stel->out.params_left,
582 val_dict_count(stel->arguments),
583 &stel->out.need_delim);
585 current_column += fprintf(options.output, ") ");
586 tabto(options.align - 1);
587 fprintf(options.output, "= ");
589 if (context != NULL && retval.type != NULL) {
590 struct format_argument_data data = { &retval, stel->arguments };
591 format_argument_cb(options.output, &data);
595 value_destroy(&retval);
598 fprintf(options.output, " <%lu.%06d>",
599 current_time_spent.tv_sec,
600 (int)current_time_spent.tv_usec);
602 fprintf(options.output, "\n");
604 #if defined(HAVE_LIBUNWIND)
605 if (options.bt_depth > 0) {
608 int unwind_depth = options.bt_depth;
611 unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
612 while (unwind_depth) {
613 unw_get_reg(&cursor, UNW_REG_IP, &ip);
614 unw_get_reg(&cursor, UNW_REG_SP, &sp);
615 unw_get_proc_name(&cursor, fn_name, 100, NULL);
616 fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
617 if (unw_step(&cursor) <= 0)
621 fprintf(options.output, "\n");
623 #endif /* defined(HAVE_LIBUNWIND) */
630 delim_output(FILE *stream, int *need_delimp,
631 int (*writer)(FILE *stream, void *data),
636 /* If we don't need a delimiter, then we don't need to go
637 * through a temporary stream. It's all the same whether
638 * WRITER emits anything or not. */
640 o = writer(stream, data);
645 FILE *tmp = open_memstream(&buf, &bufsz);
646 o = writer(tmp, data);
649 if (o > 0 && ((*need_delimp
650 && account_output(&o, fprintf(stream, ", ")) < 0)
651 || fwrite(buf, 1, bufsz, stream) != bufsz))
660 *need_delimp = *need_delimp || o > 0;
665 account_output(int *countp, int c)
673 do_report(const char *filename, unsigned line_no, const char *severity,
674 const char *fmt, va_list args)
677 vsnprintf(buf, sizeof(buf), fmt, args);
678 buf[sizeof(buf) - 1] = 0;
679 if (filename != NULL)
680 output_line(0, "%s:%d: %s: %s",
681 filename, line_no, severity, buf);
683 output_line(0, "%s: %s", severity, buf);
687 report_error(const char *filename, unsigned line_no, const char *fmt, ...)
691 do_report(filename, line_no, "error", fmt, args);
696 report_warning(const char *filename, unsigned line_no, const char *fmt, ...)
700 do_report(filename, line_no, "warning", fmt, args);
705 report_global_error(const char *fmt, ...)
709 do_report(NULL, 0, "error", fmt, args);