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

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