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

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