Blame SOURCES/ltrace-0.7.91-account_execl.patch

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