Blame SOURCES/ltrace-0.7.91-unwind-elfutils.patch

d5c145
Common subdirectories: ltrace-0.7.91/config and ltrace-0.7.91-pm/config
d5c145
diff -u ltrace-0.7.91/configure.ac ltrace-0.7.91-pm/configure.ac
d5c145
--- ltrace-0.7.91/configure.ac	2015-01-09 00:38:17.977190726 +0100
d5c145
+++ ltrace-0.7.91-pm/configure.ac	2015-01-09 00:37:40.261910548 +0100
d5c145
@@ -128,6 +128,51 @@
d5c145
 AC_CHECK_HEADERS(selinux/selinux.h)
d5c145
 AC_CHECK_LIB(selinux, security_get_boolean_active)
d5c145
 
d5c145
+dnl Whether (and which) elfutils libdw.so to use for unwinding.
d5c145
+AC_ARG_WITH(elfutils,
d5c145
+  AS_HELP_STRING([--with-elfutils], [Use elfutils libdwfl unwinding support]),
d5c145
+  [case "${withval}" in
d5c145
+  (yes|no) enable_elfutils=$withval;;
d5c145
+  (*) enable_elfutils=yes
d5c145
+    AM_CPPFLAGS="${AM_CPPFLAGS} -I${withval}/include"
d5c145
+    AM_LDFLAGS="${AM_LDFLAGS} -L${withval}/lib"
d5c145
+    elfutils_LD_LIBRARY_PATH="${withval}/lib:${withval}/lib/elfutils"
d5c145
+    ;;
d5c145
+esac],[enable_elfutils=maybe])
d5c145
+
d5c145
+dnl Check whether we have the elfutils libdwfl.h header installed.
d5c145
+saved_CPPFLAGS="${CPPFLAGS}"
d5c145
+CPPFLAGS="${CPPFLAGS} ${AM_CPPFLAGS}"
d5c145
+AC_CHECK_HEADERS([elfutils/libdwfl.h],[have_libdwfl_h=yes])
d5c145
+CPPFLAGS="${saved_CPPFLAGS}"
d5c145
+
d5c145
+dnl And whether libdw.so provides the unwinding functions.
d5c145
+saved_LDFLAGS="${LDFLAGS}"
d5c145
+LDFLAGS="${LDFLAGS} ${AM_LDFLAGS}"
d5c145
+AC_CHECK_LIB([dw], [dwfl_getthread_frames], [have_libdw_dwfl_frames=yes])
d5c145
+LDFLAGS="${saved_LDFLAGS}"
d5c145
+
d5c145
+AC_MSG_CHECKING([whether to use elfutils libdwfl unwinding support])
d5c145
+case "${enable_elfutils}" in
d5c145
+(yes|maybe)
d5c145
+  if test x$have_libdwfl_h = xyes -a x$have_libdw_dwfl_frames = xyes; then
d5c145
+    enable_elfutils=yes
d5c145
+  elif test $enable_elfutils = maybe; then
d5c145
+    enable_elfutils=no
d5c145
+  else
d5c145
+    AC_MSG_RESULT([$enable_elfutils])
d5c145
+    AC_MSG_ERROR([Missing elfutils/libdwfl.h or dwfl_getthread_frames not in libdw.so])
d5c145
+  fi
d5c145
+  ;;
d5c145
+(*) ;;
d5c145
+esac
d5c145
+AC_MSG_RESULT([$enable_elfutils])
d5c145
+
d5c145
+if test x"$enable_elfutils" = xyes; then
d5c145
+  libdw_LIBS=-ldw
d5c145
+  AC_SUBST(libdw_LIBS)
d5c145
+  AC_DEFINE([HAVE_LIBDW], [1], [we have elfutils libdw])
d5c145
+fi
d5c145
 
d5c145
 # HAVE_LIBUNWIND
d5c145
 AC_ARG_WITH(libunwind,
d5c145
@@ -193,6 +238,13 @@
d5c145
   LDFLAGS="${saved_LDFLAGS}"
d5c145
 fi
d5c145
 
d5c145
+if test x"$enable_elfutils" = xyes -a x"$enable_libunwind" = xyes; then
d5c145
+  AC_MSG_ERROR([Cannot enable both --with-libunwind and --with-elfutils])
d5c145
+fi
d5c145
+
d5c145
+if test x"$enable_elfutils" = xyes -o x"$enable_libunwind" = xyes; then
d5c145
+  AC_DEFINE([HAVE_UNWINDER], [1], [we have an unwinder available])
d5c145
+fi
d5c145
 
d5c145
 saved_CPPFLAGS="${CPPFLAGS}"
d5c145
 saved_LDFLAGS="${LDFLAGS}"
d5c145
@@ -340,6 +392,7 @@
d5c145
 AC_SUBST(AM_CFLAGS)
d5c145
 AC_SUBST(AM_LDFLAGS)
d5c145
 AC_SUBST(libelf_LD_LIBRARY_PATH)
d5c145
+AC_SUBST(elfutils_LD_LIBRARY_PATH)
d5c145
 AC_SUBST(libunwind_LD_LIBRARY_PATH)
d5c145
 
d5c145
 AC_CONFIG_FILES([
d5c145
Common subdirectories: ltrace-0.7.91/debian and ltrace-0.7.91-pm/debian
d5c145
Common subdirectories: ltrace-0.7.91/etc and ltrace-0.7.91-pm/etc
d5c145
diff -u ltrace-0.7.91/ltrace.1 ltrace-0.7.91-pm/ltrace.1
d5c145
--- ltrace-0.7.91/ltrace.1	2015-01-09 00:38:17.975190764 +0100
d5c145
+++ ltrace-0.7.91-pm/ltrace.1	2015-01-09 00:37:40.261910548 +0100
d5c145
@@ -196,7 +196,8 @@
d5c145
 correct execution of setuid and/or setgid binaries.
d5c145
 .IP "\-w, --where \fInr"
d5c145
 Show backtrace of \fInr\fR stack frames for each traced function. This
d5c145
-option enabled only if libunwind support was enabled at compile time.
d5c145
+option enabled only if elfutils or libunwind support was enabled at compile
d5c145
+time.
d5c145
 .IP "\-x \fIfilter"
d5c145
 A qualifying expression which modifies which symbol table entry points
d5c145
 to trace.  The format of the filter expression is described in the
d5c145
Only in ltrace-0.7.91-pm/: ltrace.1.orig
d5c145
diff -u ltrace-0.7.91/Makefile.am ltrace-0.7.91-pm/Makefile.am
d5c145
--- ltrace-0.7.91/Makefile.am	2015-01-09 00:38:17.965190955 +0100
d5c145
+++ ltrace-0.7.91-pm/Makefile.am	2015-01-09 00:37:40.260910568 +0100
d5c145
@@ -40,6 +40,7 @@
d5c145
 	$(liberty_LIBS) \
d5c145
 	$(libsupcxx_LIBS) \
d5c145
 	$(libstdcxx_LIBS) \
d5c145
+	$(libdw_LIBS) \
d5c145
 	$(libunwind_LIBS) \
d5c145
 	sysdeps/libos.la
d5c145
 
d5c145
diff -u ltrace-0.7.91/options.c ltrace-0.7.91-pm/options.c
d5c145
--- ltrace-0.7.91/options.c	2015-01-09 00:38:17.974190783 +0100
d5c145
+++ ltrace-0.7.91-pm/options.c	2015-01-09 00:37:40.261910548 +0100
d5c145
@@ -107,9 +107,9 @@
d5c145
 		"  -T                  show the time spent inside each call.\n"
d5c145
 		"  -u USERNAME         run command with the userid, groupid of username.\n"
d5c145
 		"  -V, --version       output version information and exit.\n"
d5c145
-#if defined(HAVE_LIBUNWIND)
d5c145
+#if defined(HAVE_UNWINDER)
d5c145
 		"  -w, --where=NR      print backtrace showing NR stack frames at most.\n"
d5c145
-#endif /* defined(HAVE_LIBUNWIND) */
d5c145
+#endif /* defined(HAVE_UNWINDER) */
d5c145
 		"  -x FILTER           modify which static functions to trace.\n"
d5c145
 		"\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
d5c145
 		progname);
d5c145
@@ -519,9 +519,9 @@
d5c145
 	progname = argv[0];
d5c145
 	options.output = stderr;
d5c145
 	options.no_signals = 0;
d5c145
-#if defined(HAVE_LIBUNWIND)
d5c145
+#if defined(HAVE_UNWINDER)
d5c145
 	options.bt_depth = -1;
d5c145
-#endif /* defined(HAVE_LIBUNWIND) */
d5c145
+#endif /* defined(HAVE_UNWINDER) */
d5c145
 
d5c145
 	guess_cols();
d5c145
 
d5c145
@@ -545,9 +545,9 @@
d5c145
 			{"output", 1, 0, 'o'},
d5c145
 			{"version", 0, 0, 'V'},
d5c145
 			{"no-signals", 0, 0, 'b'},
d5c145
-# if defined(HAVE_LIBUNWIND)
d5c145
+# if defined(HAVE_UNWINDER)
d5c145
 			{"where", 1, 0, 'w'},
d5c145
-# endif /* defined(HAVE_LIBUNWIND) */
d5c145
+# endif /* defined(HAVE_UNWINDER) */
d5c145
 			{0, 0, 0, 0}
d5c145
 		};
d5c145
 #endif
d5c145
@@ -556,7 +556,7 @@
d5c145
 #ifdef USE_DEMANGLE
d5c145
 			"C"
d5c145
 #endif
d5c145
-#if defined(HAVE_LIBUNWIND)
d5c145
+#if defined(HAVE_UNWINDER)
d5c145
 			"w:"
d5c145
 #endif
d5c145
 			"cfhiLrStTVba:A:D:e:F:l:n:o:p:s:u:x:X:";
d5c145
@@ -681,11 +681,11 @@
d5c145
 			       "There is NO WARRANTY, to the extent permitted by law.\n");
d5c145
 			exit(0);
d5c145
 			break;
d5c145
-#if defined(HAVE_LIBUNWIND)
d5c145
+#if defined(HAVE_UNWINDER)
d5c145
 		case 'w':
d5c145
 			options.bt_depth = parse_int(optarg, 'w', 1, 0);
d5c145
 			break;
d5c145
-#endif /* defined(HAVE_LIBUNWIND) */
d5c145
+#endif /* defined(HAVE_UNWINDER) */
d5c145
 
d5c145
 		case 'x':
d5c145
 			parse_filter_chain(optarg, &options.static_filter);
d5c145
Only in ltrace-0.7.91-pm/: options.c.orig
d5c145
diff -u ltrace-0.7.91/options.h ltrace-0.7.91-pm/options.h
d5c145
--- ltrace-0.7.91/options.h	2015-01-09 00:38:17.966190936 +0100
d5c145
+++ ltrace-0.7.91-pm/options.h	2015-01-09 00:37:40.261910548 +0100
d5c145
@@ -44,9 +44,9 @@
d5c145
 	size_t strlen;     /* default maximum # of bytes printed in strings */
d5c145
 	int follow;     /* trace child processes */
d5c145
 	int no_signals; /* don't print signals */
d5c145
-#if defined(HAVE_LIBUNWIND)
d5c145
+#if defined(HAVE_UNWINDER)
d5c145
 	int bt_depth;	 /* how may levels of stack frames to show */
d5c145
-#endif /* defined(HAVE_LIBUNWIND) */
d5c145
+#endif /* defined(HAVE_UNWINDER) */
d5c145
 	struct filter *plt_filter;
d5c145
 	struct filter *static_filter;
d5c145
 
d5c145
diff -u ltrace-0.7.91/output.c ltrace-0.7.91-pm/output.c
d5c145
--- ltrace-0.7.91/output.c	2015-01-09 00:38:17.966190936 +0100
d5c145
+++ ltrace-0.7.91-pm/output.c	2015-01-09 00:37:40.261910548 +0100
d5c145
@@ -33,6 +33,7 @@
d5c145
 #include <unistd.h>
d5c145
 #include <errno.h>
d5c145
 #include <assert.h>
d5c145
+#include <inttypes.h>
d5c145
 
d5c145
 #include "output.h"
d5c145
 #include "demangle.h"
d5c145
@@ -567,6 +568,73 @@
d5c145
 	stel->out.need_delim = need_delim;
d5c145
 }
d5c145
 
d5c145
+#if defined(HAVE_LIBDW)
d5c145
+/* Prints information about one frame of a thread.  Called by
d5c145
+   dwfl_getthread_frames in output_right.  Returns 1 when done (max
d5c145
+   number of frames reached).  Returns -1 on error.  Returns 0 on
d5c145
+   success (if there are more frames in the thread, call us again).  */
d5c145
+static int
d5c145
+frame_callback (Dwfl_Frame *state, void *arg)
d5c145
+{
d5c145
+	Dwarf_Addr pc;
d5c145
+	bool isactivation;
d5c145
+
d5c145
+	int *frames = (int *) arg;
d5c145
+
d5c145
+	if (!dwfl_frame_pc(state, &pc, &isactivation))
d5c145
+		return -1;
d5c145
+
d5c145
+	if (!isactivation)
d5c145
+		pc--;
d5c145
+
d5c145
+	Dwfl *dwfl = dwfl_thread_dwfl(dwfl_frame_thread(state));
d5c145
+	Dwfl_Module *mod = dwfl_addrmodule(dwfl, pc);
d5c145
+	const char *modname = NULL;
d5c145
+	const char *symname = NULL;
d5c145
+	GElf_Off off = 0;
d5c145
+	if (mod != NULL) {
d5c145
+		GElf_Sym sym;
d5c145
+		modname = dwfl_module_info(mod, NULL, NULL, NULL, NULL,
d5c145
+					   NULL, NULL, NULL);
d5c145
+		symname = dwfl_module_addrinfo(mod, pc, &off, &sym,
d5c145
+					       NULL, NULL, NULL);
d5c145
+	}
d5c145
+
d5c145
+	/* This mimics the output produced by libunwind below.  */
d5c145
+	fprintf(options.output, " > %s(%s+0x%" PRIx64 ") [%" PRIx64 "]\n",
d5c145
+		modname, symname, off, pc);
d5c145
+
d5c145
+	/* See if we can extract the source line too and print it on
d5c145
+	   the next line if we can find it.  */
d5c145
+	if (mod != NULL) {
d5c145
+		Dwfl_Line *l = dwfl_module_getsrc(mod, pc);
d5c145
+		if (l != NULL) {
d5c145
+			int line, col;
d5c145
+			line = col = -1;
d5c145
+			const char *src = dwfl_lineinfo(l, NULL, &line, &col,
d5c145
+							NULL, NULL);
d5c145
+			if (src != NULL) {
d5c145
+				fprintf(options.output, "\t%s", src);
d5c145
+				if (line > 0) {
d5c145
+					fprintf(options.output, ":%d", line);
d5c145
+					if (col > 0)
d5c145
+			                        fprintf(options.output,
d5c145
+							":%d", col);
d5c145
+				}
d5c145
+				fprintf(options.output, "\n");
d5c145
+			}
d5c145
+
d5c145
+		}
d5c145
+	}
d5c145
+
d5c145
+	/* Max number of frames to print reached? */
d5c145
+	if ((*frames)-- == 0)
d5c145
+		return 1;
d5c145
+
d5c145
+	return 0;
d5c145
+}
d5c145
+#endif /* defined(HAVE_LIBDW) */
d5c145
+
d5c145
 void
d5c145
 output_right(enum tof type, struct process *proc, struct library_symbol *libsym,
d5c145
 	     struct timedelta *spent)
d5c145
@@ -694,6 +762,24 @@
d5c145
 	}
d5c145
 #endif /* defined(HAVE_LIBUNWIND) */
d5c145
 
d5c145
+#if defined(HAVE_LIBDW)
d5c145
+	if (options.bt_depth > 0 && proc->leader->dwfl != NULL) {
d5c145
+		int frames = options.bt_depth;
d5c145
+		if (dwfl_getthread_frames(proc->leader->dwfl, proc->pid,
d5c145
+					  frame_callback, &frames) < 0) {
d5c145
+			// Only print an error if we couldn't show anything.
d5c145
+			// Otherwise just show there might be more...
d5c145
+			if (frames == options.bt_depth)
d5c145
+				fprintf(stderr,
d5c145
+					"dwfl_getthread_frames tid %d: %s\n",
d5c145
+					proc->pid, dwfl_errmsg(-1));
d5c145
+			else
d5c145
+				fprintf(options.output, " > [...]\n");
d5c145
+		}
d5c145
+		fprintf(options.output, "\n");
d5c145
+	  }
d5c145
+#endif /* defined(HAVE_LIBDW) */
d5c145
+
d5c145
 	current_proc = NULL;
d5c145
 	current_column = 0;
d5c145
 }
d5c145
Only in ltrace-0.7.91-pm/: output.c.orig
d5c145
diff -u ltrace-0.7.91/proc.c ltrace-0.7.91-pm/proc.c
d5c145
--- ltrace-0.7.91/proc.c	2015-01-09 00:38:17.981190650 +0100
d5c145
+++ ltrace-0.7.91-pm/proc.c	2015-01-09 00:37:40.261910548 +0100
d5c145
@@ -111,6 +111,11 @@
d5c145
 	if (proc->unwind_as != NULL)
d5c145
 		unw_destroy_addr_space(proc->unwind_as);
d5c145
 #endif /* defined(HAVE_LIBUNWIND) */
d5c145
+
d5c145
+#if defined(HAVE_LIBDW)
d5c145
+	if (proc->dwfl != NULL)
d5c145
+		dwfl_end(proc->dwfl);
d5c145
+#endif /* defined(HAVE_LIBDW) */
d5c145
 }
d5c145
 
d5c145
 static int
d5c145
@@ -172,6 +177,10 @@
d5c145
 	}
d5c145
 #endif /* defined(HAVE_LIBUNWIND) */
d5c145
 
d5c145
+#if defined(HAVE_LIBDW)
d5c145
+	proc->dwfl = NULL; /* Initialize for leader only on first library.  */
d5c145
+#endif /* defined(HAVE_LIBDW) */
d5c145
+
d5c145
 	return 0;
d5c145
 }
d5c145
 
d5c145
@@ -887,6 +896,59 @@
d5c145
 	debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
d5c145
 	      lib->soname, lib->base, lib->pathname, proc->pid);
d5c145
 
d5c145
+#if defined(HAVE_LIBDW)
d5c145
+	if (options.bt_depth > 0) {
d5c145
+		/* Setup module tracking for libdwfl unwinding.  */
d5c145
+		struct process *leader = proc->leader;
d5c145
+		Dwfl *dwfl = leader->dwfl;
d5c145
+		if (dwfl == NULL) {
d5c145
+			static const Dwfl_Callbacks proc_callbacks = {
d5c145
+				.find_elf = dwfl_linux_proc_find_elf,
d5c145
+				.find_debuginfo = dwfl_standard_find_debuginfo
d5c145
+			};
d5c145
+			dwfl = dwfl_begin(&proc_callbacks);
d5c145
+			if (dwfl == NULL)
d5c145
+				fprintf(stderr,
d5c145
+					"Couldn't initialize libdwfl unwinding "
d5c145
+					"for process %d: %s\n", leader->pid,
d5c145
+					dwfl_errmsg (-1));
d5c145
+		}
d5c145
+
d5c145
+		if (dwfl != NULL) {
d5c145
+			dwfl_report_begin_add(dwfl);
d5c145
+			if (dwfl_report_elf(dwfl, lib->soname,
d5c145
+					    lib->pathname, -1,
d5c145
+					    (GElf_Addr) lib->base,
d5c145
+					    false) == NULL)
d5c145
+				fprintf(stderr,
d5c145
+					"dwfl_report_elf %s@%p (%s) %d: %s\n",
d5c145
+					lib->soname, lib->base, lib->pathname,
d5c145
+					proc->pid, dwfl_errmsg (-1));
d5c145
+			dwfl_report_end(dwfl, NULL, NULL);
d5c145
+
d5c145
+			if (leader->dwfl == NULL) {
d5c145
+				int r = dwfl_linux_proc_attach(dwfl,
d5c145
+							       leader->pid,
d5c145
+							       true);
d5c145
+				if (r == 0)
d5c145
+					leader->dwfl = dwfl;
d5c145
+				else {
d5c145
+					const char *msg;
d5c145
+					dwfl_end(dwfl);
d5c145
+					if (r < 0)
d5c145
+						msg = dwfl_errmsg(-1);
d5c145
+					else
d5c145
+						msg = strerror(r);
d5c145
+					fprintf(stderr, "Couldn't initialize "
d5c145
+						"libdwfl unwinding for "
d5c145
+						"process %d: %s\n",
d5c145
+						leader->pid, msg);
d5c145
+				}
d5c145
+			}
d5c145
+		}
d5c145
+	}
d5c145
+#endif /* defined(HAVE_LIBDW) */
d5c145
+
d5c145
 	/* Insert breakpoints for all active (non-latent) symbols.  */
d5c145
 	struct library_symbol *libsym = NULL;
d5c145
 	while ((libsym = library_each_symbol(lib, libsym,
d5c145
diff -u ltrace-0.7.91/proc.c.orig ltrace-0.7.91-pm/proc.c.orig
d5c145
--- ltrace-0.7.91/proc.h	2015-01-09 00:38:17.966190936 +0100
d5c145
+++ ltrace-0.7.91-pm/proc.h	2015-01-09 00:37:40.261910548 +0100
d5c145
@@ -28,6 +28,10 @@
d5c145
 #include <sys/time.h>
d5c145
 #include <stdint.h>
d5c145
 
d5c145
+#if defined(HAVE_LIBDW)
d5c145
+# include <elfutils/libdwfl.h>
d5c145
+#endif
d5c145
+
d5c145
 #if defined(HAVE_LIBUNWIND)
d5c145
 # include <libunwind.h>
d5c145
 #endif /* defined(HAVE_LIBUNWIND) */
d5c145
@@ -113,6 +117,11 @@
d5c145
 	short e_machine;
d5c145
 	char e_class;
d5c145
 
d5c145
+#if defined(HAVE_LIBDW)
d5c145
+	/* Unwind info for leader, NULL for non-leader procs. */
d5c145
+	Dwfl *dwfl;
d5c145
+#endif /* defined(HAVE_LIBDW) */
d5c145
+
d5c145
 #if defined(HAVE_LIBUNWIND)
d5c145
 	/* libunwind address space */
d5c145
 	unw_addr_space_t unwind_as;
d5c145
Only in ltrace-0.7.91-pm/: proc.h.orig
d5c145
Common subdirectories: ltrace-0.7.91/sysdeps and ltrace-0.7.91-pm/sysdeps
d5c145
Common subdirectories: ltrace-0.7.91/testsuite and ltrace-0.7.91-pm/testsuite
d5c145
diff -up ltrace-0.7.91/proc.c\~ ltrace-0.7.91/proc.c
d5c145
--- ltrace-0.7.91/proc.c~	2015-01-09 01:55:38.289864078 +0100
d5c145
+++ ltrace-0.7.91/proc.c	2015-01-09 01:56:29.818881935 +0100
d5c145
@@ -918,7 +918,8 @@ proc_add_library(struct process *proc, s
d5c145
 			dwfl_report_begin_add(dwfl);
d5c145
 			if (dwfl_report_elf(dwfl, lib->soname,
d5c145
 					    lib->pathname, -1,
d5c145
-					    (GElf_Addr) lib->base,
d5c145
+					    /* XXX double cast */
d5c145
+					    (GElf_Addr) (uintptr_t) lib->base,
d5c145
 					    false) == NULL)
d5c145
 				fprintf(stderr,
d5c145
 					"dwfl_report_elf %s@%p (%s) %d: %s\n",