Blame SOURCES/ltrace-0.7.91-account_execl.patch

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