Blame SOURCES/ltrace-0.7.91-account_execl.patch

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