Blame SOURCES/ltrace-0.7.91-account_execl.patch

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