5435f0
From 7a8a31c041b52d87c1522e684cb301b07ea6ad9b Mon Sep 17 00:00:00 2001
5435f0
From: Adam Jackson <ajax@redhat.com>
5435f0
Date: Fri, 10 Oct 2008 15:53:48 -0400
5435f0
Subject: [PATCH] Move xorg_backtrace() up to the OS level so we can call it from DIX.
5435f0
5435f0
---
5435f0
 hw/xfree86/common/xf86Events.c |  173 ----------------------------------
5435f0
 include/os.h                   |    2 +
5435f0
 os/Makefile.am                 |    1 +
5435f0
 os/backtrace.c                 |  202 ++++++++++++++++++++++++++++++++++++++++
5435f0
 4 files changed, 205 insertions(+), 173 deletions(-)
5435f0
 create mode 100644 os/backtrace.c
5435f0
5435f0
diff --git a/hw/xfree86/common/xf86Events.c b/hw/xfree86/common/xf86Events.c
5435f0
index 6ca0ae7..a2c206e 100644
5435f0
--- a/hw/xfree86/common/xf86Events.c
5435f0
+++ b/hw/xfree86/common/xf86Events.c
5435f0
@@ -358,179 +358,6 @@
5435f0
     xf86SigIllHandler = sigillhandler;
5435f0
 }
5435f0
 
5435f0
-#ifdef HAVE_BACKTRACE
5435f0
-#include <execinfo.h>
5435f0
-
5435f0
-static __inline__ void xorg_backtrace(void)
5435f0
-{
5435f0
-    void *array[32]; /* deeper nesting than this means something's wrong */
5435f0
-    size_t size, i;
5435f0
-    char **strings;
5435f0
-    ErrorF("\nBacktrace:\n");
5435f0
-    size = backtrace(array, 32);
5435f0
-    strings = backtrace_symbols(array, size);
5435f0
-    for (i = 0; i < size; i++)
5435f0
-        ErrorF("%d: %s\n", i, strings[i]);
5435f0
-    free(strings);
5435f0
-}
5435f0
-
5435f0
-#else /* not glibc or glibc < 2.1 */
5435f0
-
5435f0
-# if defined(sun) && defined(__SVR4)
5435f0
-#  define HAVE_PSTACK
5435f0
-# endif
5435f0
-
5435f0
-# if defined(HAVE_WALKCONTEXT) /* Solaris 9 & later */
5435f0
-
5435f0
-# include <ucontext.h>
5435f0
-# include <signal.h>
5435f0
-# include <dlfcn.h>
5435f0
-# include <sys elf.h="">
5435f0
-
5435f0
-#ifdef _LP64
5435f0
-# define ElfSym Elf64_Sym
5435f0
-#else
5435f0
-# define ElfSym Elf32_Sym
5435f0
-#endif
5435f0
-
5435f0
-/* Called for each frame on the stack to print it's contents */
5435f0
-static int xorg_backtrace_frame(uintptr_t pc, int signo, void *arg)
5435f0
-{
5435f0
-    Dl_info dlinfo;
5435f0
-    ElfSym *dlsym;
5435f0
-    char header[32];
5435f0
-    int depth = *((int *) arg);
5435f0
-
5435f0
-    if (signo) {
5435f0
-	char signame[SIG2STR_MAX];
5435f0
-
5435f0
-	if (sig2str(signo, signame) != 0) {
5435f0
-	    strcpy(signame, "unknown");
5435f0
-	}
5435f0
-
5435f0
-	ErrorF("** Signal %d (%s)\n", signo, signame);
5435f0
-    }
5435f0
-
5435f0
-    snprintf(header, sizeof(header), "%d: 0x%lx", depth, pc);
5435f0
-    *((int *) arg) = depth + 1;
5435f0
-
5435f0
-    /* Ask system dynamic loader for info on the address */
5435f0
-    if (dladdr1((void *) pc, &dlinfo, (void **) &dlsym, RTLD_DL_SYMENT)) {
5435f0
-	unsigned long offset = pc - (uintptr_t) dlinfo.dli_saddr;
5435f0
-	const char *symname;
5435f0
-	
5435f0
-	if (offset < dlsym->st_size) { /* inside a function */
5435f0
-	    symname = dlinfo.dli_sname;
5435f0
-	} else { /* found which file it was in, but not which function */
5435f0
-	    symname = "<section start="">";
5435f0
-	    offset = pc - (uintptr_t)dlinfo.dli_fbase;
5435f0
-	}
5435f0
-	ErrorF("%s: %s:%s+0x%lx\n", header, dlinfo.dli_fname,
5435f0
-	       symname, offset);
5435f0
-
5435f0
-    } else {
5435f0
-	/* Couldn't find symbol info from system dynamic loader, should
5435f0
-	 * probably poke elfloader here, but haven't written that code yet,
5435f0
-	 * so we just print the pc.
5435f0
-	 */
5435f0
-	ErrorF("%s\n", header);
5435f0
-    }
5435f0
-
5435f0
-    return 0;
5435f0
-}
5435f0
-# endif /* HAVE_WALKCONTEXT */
5435f0
-
5435f0
-# ifdef HAVE_PSTACK
5435f0
-static int xorg_backtrace_pstack(void) {
5435f0
-    pid_t kidpid;
5435f0
-    int pipefd[2];
5435f0
-
5435f0
-    if (pipe(pipefd) != 0) {
5435f0
-	return -1;
5435f0
-    }
5435f0
-
5435f0
-    kidpid = fork1();
5435f0
-
5435f0
-    if (kidpid == -1) {
5435f0
-	/* ERROR */
5435f0
-	return -1;
5435f0
-    } else if (kidpid == 0) {
5435f0
-	/* CHILD */
5435f0
-	char parent[16];
5435f0
-	
5435f0
-	seteuid(0);
5435f0
-	close(STDIN_FILENO);
5435f0
-	close(STDOUT_FILENO);
5435f0
-	dup2(pipefd[1],STDOUT_FILENO);
5435f0
-	closefrom(STDERR_FILENO);
5435f0
-
5435f0
-	snprintf(parent, sizeof(parent), "%d", getppid());
5435f0
-	execle("/usr/bin/pstack", "pstack", parent, NULL);
5435f0
-	exit(1);
5435f0
-    } else {
5435f0
-	/* PARENT */
5435f0
-	char btline[256];
5435f0
-	int kidstat;
5435f0
-	int bytesread;
5435f0
-	int done = 0;
5435f0
-	
5435f0
-	close(pipefd[1]);
5435f0
-
5435f0
-	while (!done) {
5435f0
-	    bytesread = read(pipefd[0], btline, sizeof(btline) - 1);
5435f0
-
5435f0
-	    if (bytesread > 0) {
5435f0
-		btline[bytesread] = 0;
5435f0
-		ErrorF("%s", btline);
5435f0
-	    }
5435f0
-	    else if ((bytesread < 0) ||
5435f0
-		     ((errno != EINTR) && (errno != EAGAIN)))
5435f0
-		done = 1;
5435f0
-	}
5435f0
-	close(pipefd[0]);
5435f0
-	waitpid(kidpid, &kidstat, 0);
5435f0
-	if (kidstat != 0)
5435f0
-	    return -1;
5435f0
-    }
5435f0
-    return 0;
5435f0
-}
5435f0
-# endif /* HAVE_PSTACK */
5435f0
-
5435f0
-
5435f0
-# if defined(HAVE_PSTACK) || defined(HAVE_WALKCONTEXT)
5435f0
-
5435f0
-static __inline__ void xorg_backtrace(void) {
5435f0
-
5435f0
-    ErrorF("\nBacktrace:\n");
5435f0
-
5435f0
-#  ifdef HAVE_PSTACK
5435f0
-/* First try fork/exec of pstack - otherwise fall back to walkcontext
5435f0
-   pstack is preferred since it can print names of non-exported functions */
5435f0
-
5435f0
-    if (xorg_backtrace_pstack() < 0)
5435f0
-#  endif	
5435f0
-    {
5435f0
-#  ifdef HAVE_WALKCONTEXT
5435f0
-	ucontext_t u;
5435f0
-	int depth = 1;
5435f0
-	
5435f0
-	if (getcontext(&u) == 0)
5435f0
-	    walkcontext(&u, xorg_backtrace_frame, &depth);
5435f0
-	else
5435f0
-#  endif
5435f0
-	    Error("Failed to get backtrace info");
5435f0
-    }
5435f0
-    ErrorF("\n");	
5435f0
-}
5435f0
-
5435f0
-# else
5435f0
-
5435f0
-/* Default fallback if we can't find any way to get a backtrace */
5435f0
-static __inline__ void xorg_backtrace(void) { return; }
5435f0
-
5435f0
-# endif
5435f0
-#endif
5435f0
-
5435f0
 /*
5435f0
  * xf86SigHandler --
5435f0
  *    Catch unexpected signals and exit or continue cleanly.
5435f0
diff --git a/include/os.h b/include/os.h
5435f0
--- a/include/os.h
5435f0
+++ b/include/os.h
5435f0
@@ -517,4 +517,6 @@
5435f0
 extern void Error(char *str);
5435f0
 extern void LogPrintMarkers(void);
5435f0
 
5435f0
+extern void xorg_backtrace(void);
5435f0
+
5435f0
 #endif /* OS_H */
5435f0
diff --git a/os/Makefile.am b/os/Makefile.am
5435f0
--- a/os/Makefile.am
5435f0
+++ b/os/Makefile.am
5435f0
@@ -11,6 +11,7 @@
5435f0
 	WaitFor.c	\
5435f0
 	access.c	\
5435f0
 	auth.c		\
5435f0
+	backtrace.c	\
5435f0
 	connection.c	\
5435f0
 	io.c		\
5435f0
 	mitauth.c	\
5435f0
diff --git a/os/backtrace.c b/os/backtrace.c
5435f0
new file mode 100644
5435f0
--- /dev/null
5435f0
+++ b/os/backtrace.c
5435f0
@@ -0,0 +1,201 @@
5435f0
+/*
5435f0
+ * Copyright 2008 Red Hat, Inc.
5435f0
+ *
5435f0
+ * Permission is hereby granted, free of charge, to any person obtaining a
5435f0
+ * copy of this software and associated documentation files (the "Software")
5435f0
+ * to deal in the software without restriction, including without limitation
5435f0
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
5435f0
+ * license, and/or sell copies of the Software, and to permit persons to whom
5435f0
+ * them Software is furnished to do so, subject to the following conditions:
5435f0
+ *
5435f0
+ * The above copyright notice and this permission notice (including the next
5435f0
+ * paragraph) shall be included in all copies or substantial portions of the
5435f0
+ * Software.
5435f0
+ *
5435f0
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5435f0
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
5435f0
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
5435f0
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
5435f0
+ * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
5435f0
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5435f0
+ */
5435f0
+
5435f0
+#ifdef HAVE_DIX_CONFIG_H
5435f0
+#include <dix-config.h>
5435f0
+#endif
5435f0
+
5435f0
+#include "os.h"
5435f0
+#include "misc.h"
5435f0
+
5435f0
+#ifdef HAVE_BACKTRACE
5435f0
+#include <execinfo.h>
5435f0
+
5435f0
+void xorg_backtrace(void)
5435f0
+{
5435f0
+    void *array[32]; /* deeper nesting than this means something's wrong */
5435f0
+    size_t size, i;
5435f0
+    char **strings;
5435f0
+    ErrorF("\nBacktrace:\n");
5435f0
+    size = backtrace(array, 32);
5435f0
+    strings = backtrace_symbols(array, size);
5435f0
+    for (i = 0; i < size; i++)
5435f0
+        ErrorF("%d: %s\n", i, strings[i]);
5435f0
+    free(strings);
5435f0
+}
5435f0
+
5435f0
+#else /* not glibc or glibc < 2.1 */
5435f0
+
5435f0
+# if defined(sun) && defined(__SVR4)
5435f0
+#  define HAVE_PSTACK
5435f0
+# endif
5435f0
+
5435f0
+# if defined(HAVE_WALKCONTEXT) /* Solaris 9 & later */
5435f0
+
5435f0
+# include <ucontext.h>
5435f0
+# include <signal.h>
5435f0
+# include <dlfcn.h>
5435f0
+# include <sys elf.h="">
5435f0
+
5435f0
+#ifdef _LP64
5435f0
+# define ElfSym Elf64_Sym
5435f0
+#else
5435f0
+# define ElfSym Elf32_Sym
5435f0
+#endif
5435f0
+
5435f0
+/* Called for each frame on the stack to print it's contents */
5435f0
+static int xorg_backtrace_frame(uintptr_t pc, int signo, void *arg)
5435f0
+{
5435f0
+    Dl_info dlinfo;
5435f0
+    ElfSym *dlsym;
5435f0
+    char header[32];
5435f0
+    int depth = *((int *) arg);
5435f0
+
5435f0
+    if (signo) {
5435f0
+	char signame[SIG2STR_MAX];
5435f0
+
5435f0
+	if (sig2str(signo, signame) != 0) {
5435f0
+	    strcpy(signame, "unknown");
5435f0
+	}
5435f0
+
5435f0
+	ErrorF("** Signal %d (%s)\n", signo, signame);
5435f0
+    }
5435f0
+
5435f0
+    snprintf(header, sizeof(header), "%d: 0x%lx", depth, pc);
5435f0
+    *((int *) arg) = depth + 1;
5435f0
+
5435f0
+    /* Ask system dynamic loader for info on the address */
5435f0
+    if (dladdr1((void *) pc, &dlinfo, (void **) &dlsym, RTLD_DL_SYMENT)) {
5435f0
+	unsigned long offset = pc - (uintptr_t) dlinfo.dli_saddr;
5435f0
+	const char *symname;
5435f0
+	
5435f0
+	if (offset < dlsym->st_size) { /* inside a function */
5435f0
+	    symname = dlinfo.dli_sname;
5435f0
+	} else { /* found which file it was in, but not which function */
5435f0
+	    symname = "<section start="">";
5435f0
+	    offset = pc - (uintptr_t)dlinfo.dli_fbase;
5435f0
+	}
5435f0
+	ErrorF("%s: %s:%s+0x%lx\n", header, dlinfo.dli_fname,
5435f0
+	       symname, offset);
5435f0
+
5435f0
+    } else {
5435f0
+	/* Couldn't find symbol info from system dynamic loader, should
5435f0
+	 * probably poke elfloader here, but haven't written that code yet,
5435f0
+	 * so we just print the pc.
5435f0
+	 */
5435f0
+	ErrorF("%s\n", header);
5435f0
+    }
5435f0
+
5435f0
+    return 0;
5435f0
+}
5435f0
+# endif /* HAVE_WALKCONTEXT */
5435f0
+
5435f0
+# ifdef HAVE_PSTACK
5435f0
+static int xorg_backtrace_pstack(void) {
5435f0
+    pid_t kidpid;
5435f0
+    int pipefd[2];
5435f0
+
5435f0
+    if (pipe(pipefd) != 0) {
5435f0
+	return -1;
5435f0
+    }
5435f0
+
5435f0
+    kidpid = fork1();
5435f0
+
5435f0
+    if (kidpid == -1) {
5435f0
+	/* ERROR */
5435f0
+	return -1;
5435f0
+    } else if (kidpid == 0) {
5435f0
+	/* CHILD */
5435f0
+	char parent[16];
5435f0
+	
5435f0
+	seteuid(0);
5435f0
+	close(STDIN_FILENO);
5435f0
+	close(STDOUT_FILENO);
5435f0
+	dup2(pipefd[1],STDOUT_FILENO);
5435f0
+	closefrom(STDERR_FILENO);
5435f0
+
5435f0
+	snprintf(parent, sizeof(parent), "%d", getppid());
5435f0
+	execle("/usr/bin/pstack", "pstack", parent, NULL);
5435f0
+	exit(1);
5435f0
+    } else {
5435f0
+	/* PARENT */
5435f0
+	char btline[256];
5435f0
+	int kidstat;
5435f0
+	int bytesread;
5435f0
+	int done = 0;
5435f0
+	
5435f0
+	close(pipefd[1]);
5435f0
+
5435f0
+	while (!done) {
5435f0
+	    bytesread = read(pipefd[0], btline, sizeof(btline) - 1);
5435f0
+
5435f0
+	    if (bytesread > 0) {
5435f0
+		btline[bytesread] = 0;
5435f0
+		ErrorF("%s", btline);
5435f0
+	    }
5435f0
+	    else if ((bytesread < 0) ||
5435f0
+		     ((errno != EINTR) && (errno != EAGAIN)))
5435f0
+		done = 1;
5435f0
+	}
5435f0
+	close(pipefd[0]);
5435f0
+	waitpid(kidpid, &kidstat, 0);
5435f0
+	if (kidstat != 0)
5435f0
+	    return -1;
5435f0
+    }
5435f0
+    return 0;
5435f0
+}
5435f0
+# endif /* HAVE_PSTACK */
5435f0
+
5435f0
+
5435f0
+# if defined(HAVE_PSTACK) || defined(HAVE_WALKCONTEXT)
5435f0
+
5435f0
+void xorg_backtrace(void) {
5435f0
+
5435f0
+    ErrorF("\nBacktrace:\n");
5435f0
+
5435f0
+#  ifdef HAVE_PSTACK
5435f0
+/* First try fork/exec of pstack - otherwise fall back to walkcontext
5435f0
+   pstack is preferred since it can print names of non-exported functions */
5435f0
+
5435f0
+    if (xorg_backtrace_pstack() < 0)
5435f0
+#  endif	
5435f0
+    {
5435f0
+#  ifdef HAVE_WALKCONTEXT
5435f0
+	ucontext_t u;
5435f0
+	int depth = 1;
5435f0
+	
5435f0
+	if (getcontext(&u) == 0)
5435f0
+	    walkcontext(&u, xorg_backtrace_frame, &depth);
5435f0
+	else
5435f0
+#  endif
5435f0
+	    Error("Failed to get backtrace info");
5435f0
+    }
5435f0
+    ErrorF("\n");	
5435f0
+}
5435f0
+
5435f0
+# else
5435f0
+
5435f0
+/* Default fallback if we can't find any way to get a backtrace */
5435f0
+void xorg_backtrace(void) { return; }
5435f0
+
5435f0
+# endif
5435f0
+#endif
5435f0
-- 
5435f0
1.6.0.1
5435f0