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

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