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

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