Blame SOURCES/ltrace-0.7.91-account_execl.patch

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