Blame SOURCES/ltrace-0.7.91-account_execl.patch

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