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