Blame SOURCES/ltrace-0.7.91-account_execl.patch

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