gallium/util: libunwind support
[mesa.git] / src / gallium / auxiliary / util / u_debug_stack.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Stack backtracing.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #include "u_debug.h"
36 #include "u_debug_symbol.h"
37 #include "u_debug_stack.h"
38
39 #if defined(HAVE_LIBUNWIND)
40
41 #ifndef _GNU_SOURCE
42 #define _GNU_SOURCE
43 #endif
44 #include <dlfcn.h>
45
46 void
47 debug_backtrace_capture(struct debug_stack_frame *backtrace,
48 unsigned start_frame,
49 unsigned nr_frames)
50 {
51 unw_cursor_t cursor;
52 unw_context_t context;
53 unw_proc_info_t pip;
54 unsigned i = 0;
55 int ret;
56
57 pip.unwind_info = NULL;
58
59 unw_getcontext(&context);
60 unw_init_local(&cursor, &context);
61
62 while ((start_frame > 0) && (unw_step(&cursor) > 0))
63 start_frame--;
64
65 while (unw_step(&cursor) > 0) {
66 char procname[256];
67 const char *filename;
68 unw_word_t off;
69 Dl_info dlinfo;
70
71 unw_get_proc_info(&cursor, &pip);
72
73 ret = unw_get_proc_name(&cursor, procname, 256, &off);
74 if (ret && ret != -UNW_ENOMEM) {
75 procname[0] = '?';
76 procname[1] = 0;
77 }
78
79 if (dladdr((void *)(uintptr_t)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
80 *dlinfo.dli_fname)
81 filename = dlinfo.dli_fname;
82 else
83 filename = "?";
84
85 snprintf(backtrace[i].buf, sizeof(backtrace[i].buf),
86 "%u: %s (%s%s+0x%x) [%p]", i, filename, procname,
87 ret == -UNW_ENOMEM ? "..." : "", (int)off,
88 (void *)(uintptr_t)(pip.start_ip + off));
89
90 i++;
91 }
92
93 while (i < nr_frames) {
94 backtrace[i].buf[0] = '\0';
95 i++;
96 }
97 }
98
99 void
100 debug_backtrace_dump(const struct debug_stack_frame *backtrace,
101 unsigned nr_frames)
102 {
103 unsigned i;
104
105 for (i = 0; i < nr_frames; ++i) {
106 if (backtrace[i].buf[0] == '\0')
107 break;
108 debug_printf("\t%s\n", backtrace[i].buf);
109 }
110 }
111
112 void
113 debug_backtrace_print(FILE *f,
114 const struct debug_stack_frame *backtrace,
115 unsigned nr_frames)
116 {
117 unsigned i;
118
119 for (i = 0; i < nr_frames; ++i) {
120 if (backtrace[i].buf[0] == '\0')
121 break;
122 fprintf(f, "\t%s\n", backtrace[i].buf);
123 }
124 }
125
126 #else /* ! HAVE_LIBUNWIND */
127
128 #if defined(PIPE_OS_WINDOWS)
129 #include <windows.h>
130 #endif
131
132
133 /**
134 * Capture stack backtrace.
135 *
136 * NOTE: The implementation of this function is quite big, but it is important
137 * not to break it down in smaller functions to avoid adding new frames to the
138 * calling stack.
139 */
140 void
141 debug_backtrace_capture(struct debug_stack_frame *backtrace,
142 unsigned start_frame,
143 unsigned nr_frames)
144 {
145 const void **frame_pointer = NULL;
146 unsigned i = 0;
147
148 if (!nr_frames) {
149 return;
150 }
151
152 /*
153 * On Windows try obtaining the stack backtrace via CaptureStackBackTrace.
154 *
155 * It works reliably both for x86 for x86_64.
156 */
157 #if defined(PIPE_OS_WINDOWS)
158 {
159 typedef USHORT (WINAPI *PFNCAPTURESTACKBACKTRACE)(ULONG, ULONG,
160 PVOID *, PULONG);
161 static PFNCAPTURESTACKBACKTRACE pfnCaptureStackBackTrace = NULL;
162
163 if (!pfnCaptureStackBackTrace) {
164 static HMODULE hModule = NULL;
165 if (!hModule) {
166 hModule = LoadLibraryA("kernel32");
167 assert(hModule);
168 }
169 if (hModule) {
170 pfnCaptureStackBackTrace =
171 (PFNCAPTURESTACKBACKTRACE)GetProcAddress(hModule,
172 "RtlCaptureStackBackTrace");
173 }
174 }
175 if (pfnCaptureStackBackTrace) {
176 /*
177 * Skip this (debug_backtrace_capture) function's frame.
178 */
179
180 start_frame += 1;
181
182 assert(start_frame + nr_frames < 63);
183 i = pfnCaptureStackBackTrace(start_frame, nr_frames,
184 (PVOID *) &backtrace->function, NULL);
185
186 /* Pad remaing requested frames with NULL */
187 while (i < nr_frames) {
188 backtrace[i++].function = NULL;
189 }
190
191 return;
192 }
193 }
194 #endif
195
196 #if defined(PIPE_CC_GCC)
197 frame_pointer = ((const void **)__builtin_frame_address(1));
198 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
199 __asm {
200 mov frame_pointer, ebp
201 }
202 frame_pointer = (const void **)frame_pointer[0];
203 #else
204 frame_pointer = NULL;
205 #endif
206
207 #ifdef PIPE_ARCH_X86
208 while (nr_frames) {
209 const void **next_frame_pointer;
210
211 if (!frame_pointer)
212 break;
213
214 if (start_frame)
215 --start_frame;
216 else {
217 backtrace[i++].function = frame_pointer[1];
218 --nr_frames;
219 }
220
221 next_frame_pointer = (const void **)frame_pointer[0];
222
223 /* Limit the stack walk to avoid referencing undefined memory */
224 if ((uintptr_t)next_frame_pointer <= (uintptr_t)frame_pointer ||
225 (uintptr_t)next_frame_pointer > (uintptr_t)frame_pointer + 64*1024)
226 break;
227
228 frame_pointer = next_frame_pointer;
229 }
230 #else
231 (void) frame_pointer;
232 #endif
233
234 while (nr_frames) {
235 backtrace[i++].function = NULL;
236 --nr_frames;
237 }
238 }
239
240
241 void
242 debug_backtrace_dump(const struct debug_stack_frame *backtrace,
243 unsigned nr_frames)
244 {
245 unsigned i;
246
247 for (i = 0; i < nr_frames; ++i) {
248 if (!backtrace[i].function)
249 break;
250 debug_symbol_print(backtrace[i].function);
251 }
252 }
253
254
255 void
256 debug_backtrace_print(FILE *f,
257 const struct debug_stack_frame *backtrace,
258 unsigned nr_frames)
259 {
260 unsigned i;
261
262 for (i = 0; i < nr_frames; ++i) {
263 const char *symbol;
264 if (!backtrace[i].function)
265 break;
266 symbol = debug_symbol_name_cached(backtrace[i].function);
267 if (symbol)
268 fprintf(f, "%s\n", symbol);
269 }
270 }
271
272 #endif /* HAVE_LIBUNWIND */