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

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