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

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