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

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