2 * Copyright 2008 Red Hat, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #ifdef HAVE_DIX_CONFIG_H
25 #include <dix-config.h>
49 ErrorFSigSafe("Backtrace:\n");
50 size = backtrace(array, 64);
51 for (i = 0; i < size; i++) {
52 int rc = dladdr(array[i], &info);
55 ErrorFSigSafe("%u: ?? [%p]\n", i, array[i]);
58 mod = (info.dli_fname && *info.dli_fname) ? info.dli_fname : "(vdso)";
61 "%u: %s (%s+0x%x) [%p]\n",
65 (unsigned int)((char *) array[i] -
66 (char *) info.dli_saddr),
70 "%u: %s (%p+0x%x) [%p]\n",
74 (unsigned int)((char *) array[i] -
75 (char *) info.dli_fbase),
81 #else /* not glibc or glibc < 2.1 */
83 #if defined(sun) && defined(__SVR4)
87 #if defined(HAVE_WALKCONTEXT) /* Solaris 9 & later */
95 #define ElfSym Elf64_Sym
97 #define ElfSym Elf32_Sym
100 /* Called for each frame on the stack to print it's contents */
102 xorg_backtrace_frame(uintptr_t pc, int signo, void *arg)
107 int depth = *((int *) arg);
110 char signame[SIG2STR_MAX];
112 if (sig2str(signo, signame) != 0) {
113 strcpy(signame, "unknown");
116 ErrorFSigSafe("** Signal %u (%s)\n", signo, signame);
119 snprintf(header, sizeof(header), "%d: 0x%lx", depth, pc);
120 *((int *) arg) = depth + 1;
122 /* Ask system dynamic loader for info on the address */
123 if (dladdr1((void *) pc, &dlinfo, (void **) &dlsym, RTLD_DL_SYMENT)) {
124 unsigned long offset = pc - (uintptr_t) dlinfo.dli_saddr;
127 if (offset < dlsym->st_size) { /* inside a function */
128 symname = dlinfo.dli_sname;
130 else { /* found which file it was in, but not which function */
131 symname = "<section start>";
132 offset = pc - (uintptr_t) dlinfo.dli_fbase;
134 ErrorFSigSafe("%s: %s:%s+0x%x\n", header, dlinfo.dli_fname, symname,
139 /* Couldn't find symbol info from system dynamic loader, should
140 * probably poke elfloader here, but haven't written that code yet,
141 * so we just print the pc.
143 ErrorFSigSafe("%s\n", header);
148 #endif /* HAVE_WALKCONTEXT */
152 xorg_backtrace_pstack(void)
157 if (pipe(pipefd) != 0) {
167 else if (kidpid == 0) {
173 close(STDOUT_FILENO);
174 dup2(pipefd[1], STDOUT_FILENO);
175 closefrom(STDERR_FILENO);
177 snprintf(parent, sizeof(parent), "%d", getppid());
178 execle("/usr/bin/pstack", "pstack", parent, NULL);
191 bytesread = read(pipefd[0], btline, sizeof(btline) - 1);
194 btline[bytesread] = 0;
195 ErrorFSigSafe("%s", btline);
197 else if ((bytesread < 0) || ((errno != EINTR) && (errno != EAGAIN)))
201 waitpid(kidpid, &kidstat, 0);
207 #endif /* HAVE_PSTACK */
209 #if defined(HAVE_PSTACK) || defined(HAVE_WALKCONTEXT)
216 ErrorFSigSafe("Backtrace:\n");
219 /* First try fork/exec of pstack - otherwise fall back to walkcontext
220 pstack is preferred since it can print names of non-exported functions */
222 if (xorg_backtrace_pstack() < 0)
225 #ifdef HAVE_WALKCONTEXT
229 if (getcontext(&u) == 0)
230 walkcontext(&u, xorg_backtrace_frame, &depth);
233 ErrorFSigSafe("Failed to get backtrace info: %s\n", strerror(errno));
240 /* Default fallback if we can't find any way to get a backtrace */