Blame SOURCES/ltrace-0.7.91-account_execl.patch

263585
From 0cf4ab66e9927e101a51dd9fa9adc6c8dc56b5e7 Mon Sep 17 00:00:00 2001
263585
From: Petr Machata <pmachata@redhat.com>
263585
Date: Thu, 21 Nov 2013 20:25:53 +0100
263585
Subject: [PATCH] Consider exec and exit events an end of outstanding calls
263585
263585
- This cleans up a lot of stuff.  The actual substance is addition of
263585
  account_current_callstack in handle_event.c (which however uses
263585
  those cleaned-up interfaces).
263585
263585
- trace-exec.exp was extended to check that the exec syscall can be
263585
  seen in -c output.  That's one of the symptoms of what this fixes.
263585
---
263585
 Makefile.am                           |    8 +-
263585
 common.h                              |    2 -
263585
 forward.h                             |    1 +
263585
 handle_event.c                        |  225 ++++++++++++++++++++-------------
263585
 libltrace.c                           |    5 +-
263585
 options.h                             |    8 +-
263585
 output.c                              |   86 +++----------
263585
 output.h                              |    5 +-
263585
 proc.h                                |    2 +-
263585
 summary.c                             |   89 +++++++++++++-
263585
 summary.h                             |   35 +++++
263585
 testsuite/ltrace.minor/trace-exec.exp |   16 ++-
263585
 12 files changed, 299 insertions(+), 183 deletions(-)
263585
 create mode 100644 summary.h
263585
263585
diff --git a/Makefile.am b/Makefile.am
263585
index d711aec..efcf18a 100644
263585
--- a/Makefile.am
263585
+++ b/Makefile.am
263585
@@ -54,10 +54,10 @@ ltrace_LDADD = \
263585
 
263585
 noinst_HEADERS = bits.h backend.h breakpoint.h common.h debug.h		\
263585
 	defs.h demangle.h dict.h forward.h ltrace-elf.h ltrace.h	\
263585
-	options.h output.h proc.h read_config_file.h library.h		\
263585
-	filter.h glob.h vect.h type.h value.h value_dict.h callback.h	\
263585
-	expr.h fetch.h vect.h param.h printf.h zero.h lens.h		\
263585
-	lens_default.h lens_enum.h memstream.h prototype.h
263585
+	options.h output.h proc.h read_config_file.h summary.h		\
263585
+	library.h filter.h glob.h vect.h type.h value.h value_dict.h	\
263585
+	callback.h expr.h fetch.h vect.h param.h printf.h zero.h	\
263585
+	lens.h lens_default.h lens_enum.h memstream.h prototype.h
263585
 
263585
 dist_man1_MANS = ltrace.1
263585
 dist_man5_MANS = ltrace.conf.5
263585
diff --git a/common.h b/common.h
263585
index a53c5db..7259ba4 100644
263585
--- a/common.h
263585
+++ b/common.h
263585
@@ -54,8 +54,6 @@ extern void handle_event(Event * event);
263585
 
263585
 extern pid_t execute_program(const char * command, char ** argv);
263585
 
263585
-extern void show_summary(void);
263585
-
263585
 struct breakpoint;
263585
 struct library_symbol;
263585
 
263585
diff --git a/forward.h b/forward.h
263585
index 8641213..58d8f05 100644
263585
--- a/forward.h
263585
+++ b/forward.h
263585
@@ -34,6 +34,7 @@ struct param_enum;
263585
 struct process;
263585
 struct protolib;
263585
 struct prototype;
263585
+struct timedelta;
263585
 struct value;
263585
 struct value_dict;
263585
 struct vect;
263585
diff --git a/handle_event.c b/handle_event.c
263585
index 9ed62a2..6fa7e98 100644
263585
--- a/handle_event.c
263585
+++ b/handle_event.c
263585
@@ -32,7 +32,6 @@
263585
 #include <stdio.h>
263585
 #include <stdlib.h>
263585
 #include <string.h>
263585
-#include <sys/time.h>
263585
 #include <stdbool.h>
263585
 
263585
 #include "backend.h"
263585
@@ -41,8 +40,9 @@
263585
 #include "fetch.h"
263585
 #include "library.h"
263585
 #include "proc.h"
263585
-#include "value_dict.h"
263585
 #include "prototype.h"
263585
+#include "summary.h"
263585
+#include "value_dict.h"
263585
 
263585
 static void handle_signal(Event *event);
263585
 static void handle_exit(Event *event);
263585
@@ -419,32 +419,11 @@ handle_signal(Event *event) {
263585
 	continue_after_signal(event->proc->pid, event->e_un.signum);
263585
 }
263585
 
263585
-static void
263585
-handle_exit(Event *event) {
263585
-	debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
263585
-	if (event->proc->state != STATE_IGNORED) {
263585
-		output_line(event->proc, "+++ exited (status %d) +++",
263585
-				event->e_un.ret_val);
263585
-	}
263585
-	remove_process(event->proc);
263585
-}
263585
-
263585
-static void
263585
-handle_exit_signal(Event *event) {
263585
-	debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
263585
-	if (event->proc->state != STATE_IGNORED) {
263585
-		output_line(event->proc, "+++ killed by %s +++",
263585
-				shortsignal(event->proc, event->e_un.signum));
263585
-	}
263585
-	remove_process(event->proc);
263585
-}
263585
-
263585
-static void
263585
-output_syscall(struct process *proc, const char *name, enum tof tof,
263585
-	       void (*output)(enum tof, struct process *,
263585
-			      struct library_symbol *))
263585
+static int
263585
+init_syscall_symbol(struct library_symbol *libsym, const char *name)
263585
 {
263585
 	static struct library syscall_lib;
263585
+
263585
 	if (syscall_lib.protolib == NULL) {
263585
 		struct protolib *protolib
263585
 			= protolib_cache_load(&g_protocache, "syscalls", 0, 1);
263585
@@ -475,10 +454,91 @@ output_syscall(struct process *proc, const char *name, enum tof tof,
263585
 		syscall_lib.protolib = protolib;
263585
 	}
263585
 
263585
+	if (library_symbol_init(libsym, 0, name, 0, LS_TOPLT_NONE) < 0)
263585
+		return -1;
263585
+
263585
+	libsym->lib = &syscall_lib;
263585
+	return 0;
263585
+}
263585
+
263585
+/* Account the unfinished functions on the call stack.  */
263585
+static void
263585
+account_current_callstack(struct process *proc)
263585
+{
263585
+	if (! options.summary)
263585
+		return;
263585
+
263585
+	struct timedelta spent[proc->callstack_depth];
263585
+
263585
+	size_t i;
263585
+	for (i = 0; i < proc->callstack_depth; ++i) {
263585
+		struct callstack_element *elem = &proc->callstack[i];
263585
+		spent[i] = calc_time_spent(elem->enter_time);
263585
+	}
263585
+
263585
+	for (i = 0; i < proc->callstack_depth; ++i) {
263585
+		struct callstack_element *elem = &proc->callstack[i];
263585
+		struct library_symbol syscall, *libsym = NULL;
263585
+		if (elem->is_syscall) {
263585
+			const char *name = sysname(proc, elem->c_un.syscall);
263585
+			if (init_syscall_symbol(&syscall, name) >= 0)
263585
+				libsym = &syscall;
263585
+
263585
+		} else {
263585
+			libsym = elem->c_un.libfunc;
263585
+		}
263585
+
263585
+		if (libsym != NULL) {
263585
+			summary_account_call(libsym, spent[i]);
263585
+
263585
+			if (elem->is_syscall)
263585
+				library_symbol_destroy(&syscall);
263585
+		}
263585
+	}
263585
+}
263585
+
263585
+static void
263585
+handle_exit(Event *event) {
263585
+	debug(DEBUG_FUNCTION, "handle_exit(pid=%d, status=%d)", event->proc->pid, event->e_un.ret_val);
263585
+	if (event->proc->state != STATE_IGNORED) {
263585
+		output_line(event->proc, "+++ exited (status %d) +++",
263585
+				event->e_un.ret_val);
263585
+	}
263585
+
263585
+	account_current_callstack(event->proc);
263585
+	remove_process(event->proc);
263585
+}
263585
+
263585
+static void
263585
+handle_exit_signal(Event *event) {
263585
+	debug(DEBUG_FUNCTION, "handle_exit_signal(pid=%d, signum=%d)", event->proc->pid, event->e_un.signum);
263585
+	if (event->proc->state != STATE_IGNORED) {
263585
+		output_line(event->proc, "+++ killed by %s +++",
263585
+				shortsignal(event->proc, event->e_un.signum));
263585
+	}
263585
+
263585
+	account_current_callstack(event->proc);
263585
+	remove_process(event->proc);
263585
+}
263585
+
263585
+static void
263585
+output_syscall(struct process *proc, const char *name, enum tof tof,
263585
+	       bool left, struct timedelta *spent)
263585
+{
263585
+	if (left)
263585
+		assert(spent == NULL);
263585
+
263585
 	struct library_symbol syscall;
263585
-	if (library_symbol_init(&syscall, 0, name, 0, LS_TOPLT_NONE) >= 0) {
263585
-		syscall.lib = &syscall_lib;
263585
-		(*output)(tof, proc, &syscall);
263585
+	if (init_syscall_symbol(&syscall, name) >= 0) {
263585
+		if (left) {
263585
+			if (! options.summary)
263585
+				output_left(tof, proc, &syscall);
263585
+		} else if (options.summary) {
263585
+			summary_account_call(&syscall, *spent);
263585
+		} else {
263585
+			output_right(tof, proc, &syscall, spent);
263585
+		}
263585
+
263585
 		library_symbol_destroy(&syscall);
263585
 	}
263585
 }
263585
@@ -486,17 +546,19 @@ output_syscall(struct process *proc, const char *name, enum tof tof,
263585
 static void
263585
 output_syscall_left(struct process *proc, const char *name)
263585
 {
263585
-	output_syscall(proc, name, LT_TOF_SYSCALL, &output_left);
263585
+	output_syscall(proc, name, LT_TOF_SYSCALL, true, NULL);
263585
 }
263585
 
263585
 static void
263585
-output_syscall_right(struct process *proc, const char *name)
263585
+output_syscall_right(struct process *proc, const char *name,
263585
+		     struct timedelta *spent)
263585
 {
263585
-	output_syscall(proc, name, LT_TOF_SYSCALLR, &output_right);
263585
+	output_syscall(proc, name, LT_TOF_SYSCALLR, false, spent);
263585
 }
263585
 
263585
 static void
263585
-handle_syscall(Event *event) {
263585
+handle_syscall(Event *event)
263585
+{
263585
 	debug(DEBUG_FUNCTION, "handle_syscall(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
263585
 	if (event->proc->state != STATE_IGNORED) {
263585
 		callstack_push_syscall(event->proc, event->e_un.sysnum);
263585
@@ -526,6 +588,8 @@ handle_exec(Event *event)
263585
 	}
263585
 	output_line(proc, "--- Called exec() ---");
263585
 
263585
+	account_current_callstack(proc);
263585
+
263585
 	if (process_exec(proc) < 0) {
263585
 		fprintf(stderr,
263585
 			"couldn't reinitialize process %d after exec\n", pid);
263585
@@ -549,74 +613,58 @@ handle_arch_syscall(Event *event) {
263585
 	continue_process(event->proc->pid);
263585
 }
263585
 
263585
-struct timeval current_time_spent;
263585
-
263585
 static void
263585
-calc_time_spent(struct process *proc)
263585
+handle_x_sysret(Event *event, char *(*name_cb)(struct process *, int))
263585
 {
263585
-	struct timeval tv;
263585
-	struct timezone tz;
263585
-	struct timeval diff;
263585
-	struct callstack_element *elem;
263585
-
263585
-	debug(DEBUG_FUNCTION, "calc_time_spent(pid=%d)", proc->pid);
263585
-	elem = &proc->callstack[proc->callstack_depth - 1];
263585
-
263585
-	gettimeofday(&tv, &tz;;
263585
+	debug(DEBUG_FUNCTION, "handle_x_sysret(pid=%d, sysnum=%d)",
263585
+	      event->proc->pid, event->e_un.sysnum);
263585
 
263585
-	diff.tv_sec = tv.tv_sec - elem->time_spent.tv_sec;
263585
-	if (tv.tv_usec >= elem->time_spent.tv_usec) {
263585
-		diff.tv_usec = tv.tv_usec - elem->time_spent.tv_usec;
263585
-	} else {
263585
-		diff.tv_sec--;
263585
-		diff.tv_usec = 1000000 + tv.tv_usec - elem->time_spent.tv_usec;
263585
-	}
263585
-	current_time_spent = diff;
263585
-}
263585
+	unsigned d = event->proc->callstack_depth;
263585
+	assert(d > 0);
263585
+	struct callstack_element *elem = &event->proc->callstack[d - 1];
263585
+	assert(elem->is_syscall);
263585
 
263585
-static void
263585
-handle_sysret(Event *event) {
263585
-	debug(DEBUG_FUNCTION, "handle_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
263585
 	if (event->proc->state != STATE_IGNORED) {
263585
-		if (opt_T || options.summary) {
263585
-			calc_time_spent(event->proc);
263585
-		}
263585
+		struct timedelta spent = calc_time_spent(elem->enter_time);
263585
 		if (options.syscalls)
263585
 			output_syscall_right(event->proc,
263585
-					     sysname(event->proc,
263585
-						     event->e_un.sysnum));
263585
+					     name_cb(event->proc,
263585
+						     event->e_un.sysnum),
263585
+					     &spent);
263585
 
263585
-		assert(event->proc->callstack_depth > 0);
263585
-		unsigned d = event->proc->callstack_depth - 1;
263585
-		assert(event->proc->callstack[d].is_syscall);
263585
 		callstack_pop(event->proc);
263585
 	}
263585
 	continue_after_syscall(event->proc, event->e_un.sysnum, 1);
263585
 }
263585
 
263585
 static void
263585
-handle_arch_sysret(Event *event) {
263585
-	debug(DEBUG_FUNCTION, "handle_arch_sysret(pid=%d, sysnum=%d)", event->proc->pid, event->e_un.sysnum);
263585
-	if (event->proc->state != STATE_IGNORED) {
263585
-		if (opt_T || options.summary) {
263585
-			calc_time_spent(event->proc);
263585
-		}
263585
-		if (options.syscalls)
263585
-			output_syscall_right(event->proc,
263585
-					     arch_sysname(event->proc,
263585
-							  event->e_un.sysnum));
263585
-		callstack_pop(event->proc);
263585
-	}
263585
-	continue_process(event->proc->pid);
263585
+handle_sysret(Event *event)
263585
+{
263585
+	handle_x_sysret(event, &sysname);
263585
+}
263585
+
263585
+static void
263585
+handle_arch_sysret(Event *event)
263585
+{
263585
+	handle_x_sysret(event, &arch_sysname);
263585
 }
263585
 
263585
 static void
263585
 output_right_tos(struct process *proc)
263585
 {
263585
 	size_t d = proc->callstack_depth;
263585
+	assert(d > 0);
263585
 	struct callstack_element *elem = &proc->callstack[d - 1];
263585
-	if (proc->state != STATE_IGNORED)
263585
-		output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc);
263585
+	assert(! elem->is_syscall);
263585
+
263585
+	if (proc->state != STATE_IGNORED) {
263585
+		struct timedelta spent = calc_time_spent(elem->enter_time);
263585
+		if (options.summary)
263585
+			summary_account_call(elem->c_un.libfunc, spent);
263585
+		else
263585
+			output_right(LT_TOF_FUNCTIONR, proc, elem->c_un.libfunc,
263585
+				     &spent);
263585
+	}
263585
 }
263585
 
263585
 #ifndef ARCH_HAVE_SYMBOL_RET
263585
@@ -645,14 +693,8 @@ handle_breakpoint(Event *event)
263585
 
263585
 	for (i = event->proc->callstack_depth - 1; i >= 0; i--) {
263585
 		if (brk_addr == event->proc->callstack[i].return_addr) {
263585
-			for (j = event->proc->callstack_depth - 1; j > i; j--) {
263585
+			for (j = event->proc->callstack_depth - 1; j > i; j--)
263585
 				callstack_pop(event->proc);
263585
-			}
263585
-			if (event->proc->state != STATE_IGNORED) {
263585
-				if (opt_T || options.summary) {
263585
-					calc_time_spent(event->proc);
263585
-				}
263585
-			}
263585
 
263585
 			struct library_symbol *libsym =
263585
 			    event->proc->callstack[i].c_un.libfunc;
263585
@@ -705,11 +747,14 @@ handle_breakpoint(Event *event)
263585
 	/* breakpoint_on_hit may delete its own breakpoint, so we have
263585
 	 * to look it up again.  */
263585
 	if ((sbp = address2bpstruct(leader, brk_addr)) != NULL) {
263585
+
263585
 		if (event->proc->state != STATE_IGNORED
263585
 		    && sbp->libsym != NULL) {
263585
 			event->proc->stack_pointer = get_stack_pointer(event->proc);
263585
 			callstack_push_symfunc(event->proc, sbp);
263585
-			output_left(LT_TOF_FUNCTION, event->proc, sbp->libsym);
263585
+			if (! options.summary)
263585
+				output_left(LT_TOF_FUNCTION, event->proc,
263585
+					    sbp->libsym);
263585
 		}
263585
 
263585
 		breakpoint_on_continue(sbp, event->proc);
263585
@@ -743,7 +788,7 @@ callstack_push_syscall(struct process *proc, int sysnum)
263585
 	proc->callstack_depth++;
263585
 	if (opt_T || options.summary) {
263585
 		struct timezone tz;
263585
-		gettimeofday(&elem->time_spent, &tz;;
263585
+		gettimeofday(&elem->enter_time, &tz;;
263585
 	}
263585
 }
263585
 
263585
@@ -781,7 +826,7 @@ callstack_push_symfunc(struct process *proc, struct breakpoint *bp)
263585
 
263585
 	if (opt_T || options.summary) {
263585
 		struct timezone tz;
263585
-		gettimeofday(&elem->time_spent, &tz;;
263585
+		gettimeofday(&elem->enter_time, &tz;;
263585
 	}
263585
 }
263585
 
263585
diff --git a/libltrace.c b/libltrace.c
263585
index 2d910a1..0112c9f 100644
263585
--- a/libltrace.c
263585
+++ b/libltrace.c
263585
@@ -32,11 +32,12 @@
263585
 #include <string.h>
263585
 #include <unistd.h>
263585
 
263585
+#include "backend.h"
263585
 #include "common.h"
263585
 #include "proc.h"
263585
-#include "read_config_file.h"
263585
-#include "backend.h"
263585
 #include "prototype.h"
263585
+#include "read_config_file.h"
263585
+#include "summary.h"
263585
 
263585
 char *command = NULL;
263585
 
263585
diff --git a/options.h b/options.h
263585
index 6c28ed9..d0df3a7 100644
263585
--- a/options.h
263585
+++ b/options.h
263585
@@ -1,6 +1,6 @@
263585
 /*
263585
  * This file is part of ltrace.
263585
- * Copyright (C) 2012 Petr Machata, Red Hat Inc.
263585
+ * Copyright (C) 2012,2013 Petr Machata, Red Hat Inc.
263585
  * Copyright (C) 2009,2010 Joe Damato
263585
  * Copyright (C) 1998,2002,2008 Juan Cespedes
263585
  * Copyright (C) 2006 Ian Wienand
263585
@@ -103,12 +103,6 @@ int parse_colon_separated_list(const char *paths, struct vect *vec);
263585
 /* Vector of struct opt_F_t.  */
263585
 extern struct vect opt_F;
263585
 
263585
-struct opt_c_struct {
263585
-	int count;
263585
-	struct timeval tv;
263585
-};
263585
-extern struct dict *dict_opt_c;
263585
-
263585
 extern char **process_options(int argc, char **argv);
263585
 
263585
 #endif /* _OPTIONS_H_ */
263585
diff --git a/output.c b/output.c
263585
index edf4522..82b6a5e 100644
263585
--- a/output.c
263585
+++ b/output.c
263585
@@ -44,16 +44,12 @@
263585
 #include "param.h"
263585
 #include "proc.h"
263585
 #include "prototype.h"
263585
+#include "summary.h"
263585
 #include "type.h"
263585
 #include "value.h"
263585
 #include "value_dict.h"
263585
 
263585
-/* TODO FIXME XXX: include in common.h: */
263585
-extern struct timeval current_time_spent;
263585
-
263585
-struct dict *dict_opt_c = NULL;
263585
-
263585
-static struct process *current_proc = 0;
263585
+static struct process *current_proc = NULL;
263585
 static size_t current_depth = 0;
263585
 static int current_column = 0;
263585
 
263585
@@ -498,9 +494,8 @@ void
263585
 output_left(enum tof type, struct process *proc,
263585
 	    struct library_symbol *libsym)
263585
 {
263585
-	if (options.summary) {
263585
-		return;
263585
-	}
263585
+	assert(! options.summary);
263585
+
263585
 	if (current_proc) {
263585
 		fprintf(options.output, " <unfinished ...>\n");
263585
 		current_column = 0;
263585
@@ -572,70 +567,21 @@ output_left(enum tof type, struct process *proc,
263585
 	stel->out.need_delim = need_delim;
263585
 }
263585
 
263585
-static void
263585
-free_stringp_cb(const char **stringp, void *data)
263585
-{
263585
-	free((char *)*stringp);
263585
-}
263585
-
263585
 void
263585
-output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
263585
+output_right(enum tof type, struct process *proc, struct library_symbol *libsym,
263585
+	     struct timedelta *spent)
263585
 {
263585
+	assert(! options.summary);
263585
+
263585
 	struct prototype *func = lookup_symbol_prototype(proc, libsym);
263585
 	if (func == NULL)
263585
 		return;
263585
 
263585
-again:
263585
-	if (options.summary) {
263585
-		if (dict_opt_c == NULL) {
263585
-			dict_opt_c = malloc(sizeof(*dict_opt_c));
263585
-			if (dict_opt_c == NULL) {
263585
-			oom:
263585
-				fprintf(stderr,
263585
-					"Can't allocate memory for "
263585
-					"keeping track of -c.\n");
263585
-				free(dict_opt_c);
263585
-				options.summary = 0;
263585
-				goto again;
263585
-			}
263585
-			DICT_INIT(dict_opt_c, char *, struct opt_c_struct,
263585
-				  dict_hash_string, dict_eq_string, NULL);
263585
-		}
263585
-
263585
-		struct opt_c_struct *st
263585
-			= DICT_FIND_REF(dict_opt_c, &libsym->name,
263585
-					struct opt_c_struct);
263585
-		if (st == NULL) {
263585
-			const char *na = strdup(libsym->name);
263585
-			struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}};
263585
-			if (na == NULL
263585
-			    || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) {
263585
-				free((char *)na);
263585
-				DICT_DESTROY(dict_opt_c, const char *,
263585
-					     struct opt_c_struct,
263585
-					     free_stringp_cb, NULL, NULL);
263585
-				goto oom;
263585
-			}
263585
-			st = DICT_FIND_REF(dict_opt_c, &libsym->name,
263585
-					   struct opt_c_struct);
263585
-			assert(st != NULL);
263585
-		}
263585
-
263585
-		if (st->tv.tv_usec + current_time_spent.tv_usec > 1000000) {
263585
-			st->tv.tv_usec += current_time_spent.tv_usec - 1000000;
263585
-			st->tv.tv_sec++;
263585
-		} else {
263585
-			st->tv.tv_usec += current_time_spent.tv_usec;
263585
-		}
263585
-		st->count++;
263585
-		st->tv.tv_sec += current_time_spent.tv_sec;
263585
-		return;
263585
-	}
263585
-
263585
-	if (current_proc && (current_proc != proc ||
263585
-			    current_depth != proc->callstack_depth)) {
263585
+	if (current_proc != NULL
263585
+		    && (current_proc != proc
263585
+			|| current_depth != proc->callstack_depth)) {
263585
 		fprintf(options.output, " <unfinished ...>\n");
263585
-		current_proc = 0;
263585
+		current_proc = NULL;
263585
 	}
263585
 	if (current_proc != proc) {
263585
 		begin_of_line(proc, type == LT_TOF_FUNCTIONR, 1);
263585
@@ -689,10 +635,12 @@ again:
263585
 		value_destroy(&retval);
263585
 
263585
 	if (opt_T) {
263585
+		assert(spent != NULL);
263585
 		fprintf(options.output, " <%lu.%06d>",
263585
-			(unsigned long)current_time_spent.tv_sec,
263585
-			(int)current_time_spent.tv_usec);
263585
+			(unsigned long) spent->tm.tv_sec,
263585
+			(int) spent->tm.tv_usec);
263585
 	}
263585
+
263585
 	fprintf(options.output, "\n");
263585
 
263585
 #if defined(HAVE_LIBUNWIND)
263585
@@ -746,7 +694,7 @@ again:
263585
 	}
263585
 #endif /* defined(HAVE_LIBUNWIND) */
263585
 
263585
-	current_proc = 0;
263585
+	current_proc = NULL;
263585
 	current_column = 0;
263585
 }
263585
 
263585
diff --git a/output.h b/output.h
263585
index b9f0518..2e74d61 100644
263585
--- a/output.h
263585
+++ b/output.h
263585
@@ -1,6 +1,6 @@
263585
 /*
263585
  * This file is part of ltrace.
263585
- * Copyright (C) 2011, 2012 Petr Machata, Red Hat Inc.
263585
+ * Copyright (C) 2011, 2012, 2013 Petr Machata, Red Hat Inc.
263585
  * Copyright (C) 2009 Juan Cespedes
263585
  *
263585
  * This program is free software; you can redistribute it and/or
263585
@@ -28,7 +28,8 @@ void output_line(struct process *proc, const char *fmt, ...);
263585
 void output_left(enum tof type, struct process *proc,
263585
 		 struct library_symbol *libsym);
263585
 void output_right(enum tof type, struct process *proc,
263585
-		  struct library_symbol *libsym);
263585
+		  struct library_symbol *libsym,
263585
+		  struct timedelta *spent);
263585
 
263585
 /* This function is for emitting lists of comma-separated strings.
263585
  *
263585
diff --git a/proc.h b/proc.h
263585
index e8032fa..64f8fe2 100644
263585
--- a/proc.h
263585
+++ b/proc.h
263585
@@ -66,7 +66,7 @@ struct callstack_element {
263585
 	} c_un;
263585
 	int is_syscall;
263585
 	arch_addr_t return_addr;
263585
-	struct timeval time_spent;
263585
+	struct timeval enter_time;
263585
 	struct fetch_context *fetch_context;
263585
 	struct value_dict *arguments;
263585
 	struct output_state out;
263585
diff --git a/summary.c b/summary.c
263585
index 9e22086..9103f71 100644
263585
--- a/summary.c
263585
+++ b/summary.c
263585
@@ -22,11 +22,15 @@
263585
 
263585
 #include "config.h"
263585
 
263585
+#include <sys/time.h>
263585
 #include <stdio.h>
263585
 #include <stdlib.h>
263585
-#include <sys/time.h>
263585
+#include <string.h>
263585
 
263585
-#include "common.h"
263585
+#include "summary.h"
263585
+#include "dict.h"
263585
+#include "library.h"
263585
+#include "options.h"
263585
 
263585
 struct entry_st {
263585
 	const char *name;
263585
@@ -40,6 +44,32 @@ struct fill_struct_data {
263585
 	unsigned long tot_usecs;
263585
 };
263585
 
263585
+struct opt_c_struct {
263585
+	int count;
263585
+	struct timeval tv;
263585
+};
263585
+
263585
+static struct dict *dict_opt_c;
263585
+
263585
+struct timedelta
263585
+calc_time_spent(struct timeval start)
263585
+{
263585
+	struct timeval tv;
263585
+	gettimeofday(&tv, NULL);
263585
+
263585
+	struct timeval diff;
263585
+	diff.tv_sec = tv.tv_sec - start.tv_sec;
263585
+	if (tv.tv_usec >= start.tv_usec) {
263585
+		diff.tv_usec = tv.tv_usec - start.tv_usec;
263585
+	} else {
263585
+		diff.tv_sec--;
263585
+		diff.tv_usec = 1000000 + tv.tv_usec - start.tv_usec;
263585
+	}
263585
+
263585
+	struct timedelta ret = { diff };
263585
+	return ret;
263585
+}
263585
+
263585
 static enum callback_status
263585
 fill_struct(const char **namep, struct opt_c_struct *st, void *u)
263585
 {
263585
@@ -114,3 +144,58 @@ show_summary(void)
263585
 
263585
 	vect_destroy(&cdata.entries, NULL, NULL);
263585
 }
263585
+
263585
+static void
263585
+free_stringp_cb(const char **stringp, void *data)
263585
+{
263585
+	free((char *)*stringp);
263585
+}
263585
+
263585
+void
263585
+summary_account_call(struct library_symbol *libsym, struct timedelta spent)
263585
+{
263585
+	assert(options.summary);
263585
+
263585
+	if (dict_opt_c == NULL) {
263585
+		dict_opt_c = malloc(sizeof(*dict_opt_c));
263585
+		if (dict_opt_c == NULL) {
263585
+		oom:
263585
+			fprintf(stderr,
263585
+				"Can't allocate memory for "
263585
+				"keeping track of -c.\n");
263585
+			free(dict_opt_c);
263585
+			options.summary = 0;
263585
+			return;
263585
+		}
263585
+		DICT_INIT(dict_opt_c, char *, struct opt_c_struct,
263585
+			  dict_hash_string, dict_eq_string, NULL);
263585
+	}
263585
+
263585
+	struct opt_c_struct *st = DICT_FIND_REF(dict_opt_c, &libsym->name,
263585
+						struct opt_c_struct);
263585
+	if (st == NULL) {
263585
+		const char *na = strdup(libsym->name);
263585
+		struct opt_c_struct new_st = {.count = 0, .tv = {0, 0}};
263585
+		if (na == NULL
263585
+		    || DICT_INSERT(dict_opt_c, &na, &new_st) < 0) {
263585
+			free((char *) na);
263585
+			DICT_DESTROY(dict_opt_c, const char *,
263585
+				     struct opt_c_struct,
263585
+				     free_stringp_cb, NULL, NULL);
263585
+			goto oom;
263585
+		}
263585
+		st = DICT_FIND_REF(dict_opt_c, &libsym->name,
263585
+				   struct opt_c_struct);
263585
+		assert(st != NULL);
263585
+	}
263585
+
263585
+	if (st->tv.tv_usec + spent.tm.tv_usec > 1000000) {
263585
+		st->tv.tv_usec += spent.tm.tv_usec - 1000000;
263585
+		st->tv.tv_sec++;
263585
+	} else {
263585
+		st->tv.tv_usec += spent.tm.tv_usec;
263585
+	}
263585
+	st->count++;
263585
+	st->tv.tv_sec += spent.tm.tv_sec;
263585
+	return;
263585
+}
263585
diff --git a/summary.h b/summary.h
263585
new file mode 100644
263585
index 0000000..f680ef9
263585
--- /dev/null
263585
+++ b/summary.h
263585
@@ -0,0 +1,35 @@
263585
+/*
263585
+ * This file is part of ltrace.
263585
+ * Copyright (C) 2013 Petr Machata, Red Hat Inc.
263585
+ *
263585
+ * This program is free software; you can redistribute it and/or
263585
+ * modify it under the terms of the GNU General Public License as
263585
+ * published by the Free Software Foundation; either version 2 of the
263585
+ * License, or (at your option) any later version.
263585
+ *
263585
+ * This program is distributed in the hope that it will be useful, but
263585
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
263585
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
263585
+ * General Public License for more details.
263585
+ *
263585
+ * You should have received a copy of the GNU General Public License
263585
+ * along with this program; if not, write to the Free Software
263585
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
263585
+ * 02110-1301 USA
263585
+ */
263585
+
263585
+#ifndef _SUMMARY_H_
263585
+#define _SUMMARY_H_
263585
+
263585
+#include "forward.h"
263585
+
263585
+struct timedelta {
263585
+	struct timeval tm;
263585
+};
263585
+
263585
+struct timedelta calc_time_spent(struct timeval start);
263585
+void summary_account_call(struct library_symbol *libsym,
263585
+			  struct timedelta spent);
263585
+void show_summary(void);
263585
+
263585
+#endif /* _SUMMARY_H_ */
263585
diff --git a/testsuite/ltrace.minor/trace-exec.exp b/testsuite/ltrace.minor/trace-exec.exp
263585
index 7a953de..57260f8 100644
263585
--- a/testsuite/ltrace.minor/trace-exec.exp
263585
+++ b/testsuite/ltrace.minor/trace-exec.exp
263585
@@ -1,5 +1,5 @@
263585
 # This file is part of ltrace.
263585
-# Copyright (C) 2012 Petr Machata, Red Hat Inc.
263585
+# Copyright (C) 2012, 2013 Petr Machata, Red Hat Inc.
263585
 #
263585
 # This program is free software; you can redistribute it and/or
263585
 # modify it under the terms of the GNU General Public License as
263585
@@ -16,22 +16,30 @@
263585
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
263585
 # 02110-1301 USA
263585
 
263585
-ltraceMatch [ltraceRun -xmain -- [ltraceCompile {} [ltraceSource c {
263585
+set bin1 [ltraceCompile {} [ltraceSource c {
263585
     #include <unistd.h>
263585
     #include <stdlib.h>
263585
     int main(int argc, char ** argv) {
263585
 	execl(argv[1], argv[1], NULL);
263585
 	abort();
263585
     }
263585
-}]] [ltraceCompile {} [ltraceSource c {
263585
+}]]
263585
+
263585
+set bin2 [ltraceCompile {} [ltraceSource c {
263585
     #include <stdio.h>
263585
     int main(void) {
263585
 	return puts("Hello, World.");
263585
     }
263585
-}]]] {
263585
+}]]
263585
+
263585
+ltraceMatch [ltraceRun -xmain -- $bin1 $bin2] {
263585
     {{^execl\(} == 1}
263585
     {{^puts\(.*\) .*= 14} == 1}
263585
     {{^main\(} == 2}
263585
 }
263585
 
263585
+ltraceMatch [ltraceRun -c -- $bin1 $bin2] {
263585
+    {{exec} > 0}
263585
+}
263585
+
263585
 ltraceDone
263585
-- 
263585
1.7.6.5
263585