gallium/util: add missing include
[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 "util/u_debug.h"
36 #include "u_debug_symbol.h"
37 #include "u_debug_stack.h"
38 #include "pipe/p_config.h"
39
40 #if defined(HAVE_LIBUNWIND)
41
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45 #include <dlfcn.h>
46
47 #include "os/os_thread.h"
48 #include "u_hash_table.h"
49
50 struct hash_table* symbols_hash;
51 static mtx_t symbols_mutex = _MTX_INITIALIZER_NP;
52
53 /* TODO with some refactoring we might be able to re-use debug_symbol_name_cached()
54 * instead.. otoh if using libunwind I think u_debug_symbol could just be excluded
55 * from build?
56 */
57 static const char *
58 symbol_name_cached(unw_cursor_t *cursor, unw_proc_info_t *pip)
59 {
60 void *addr = (void *)(uintptr_t)pip->start_ip;
61 char *name;
62
63 mtx_lock(&symbols_mutex);
64 if(!symbols_hash)
65 symbols_hash = util_hash_table_create_ptr_keys();
66 name = util_hash_table_get(symbols_hash, addr);
67 if(!name)
68 {
69 char procname[256];
70 unw_word_t off;
71 int ret;
72
73 ret = unw_get_proc_name(cursor, procname, sizeof(procname), &off);
74 if (ret && ret != -UNW_ENOMEM) {
75 procname[0] = '?';
76 procname[1] = 0;
77 }
78
79 if (asprintf(&name, "%s%s", procname, ret == -UNW_ENOMEM ? "..." : "") == -1)
80 name = "??";
81 _mesa_hash_table_insert(symbols_hash, addr, (void*)name);
82 }
83 mtx_unlock(&symbols_mutex);
84
85 return name;
86 }
87
88 void
89 debug_backtrace_capture(struct debug_stack_frame *backtrace,
90 unsigned start_frame,
91 unsigned nr_frames)
92 {
93 unw_cursor_t cursor;
94 unw_context_t context;
95 unw_proc_info_t pip;
96 unsigned i = 0;
97
98 pip.unwind_info = NULL;
99
100 unw_getcontext(&context);
101 unw_init_local(&cursor, &context);
102
103 while ((start_frame > 0) && (unw_step(&cursor) > 0))
104 start_frame--;
105
106 while ((i < nr_frames) && (unw_step(&cursor) > 0)) {
107 unw_word_t ip;
108
109 unw_get_reg(&cursor, UNW_REG_IP, &ip);
110 unw_get_proc_info(&cursor, &pip);
111
112 backtrace[i].start_ip = pip.start_ip;
113 backtrace[i].off = ip - pip.start_ip;
114 backtrace[i].procname = symbol_name_cached(&cursor, &pip);
115
116 i++;
117 }
118
119 while (i < nr_frames) {
120 backtrace[i].start_ip = 0;
121 i++;
122 }
123 }
124
125 static const void *
126 frame_ip(const struct debug_stack_frame *frame)
127 {
128 return (void *)(uintptr_t)(frame->start_ip + frame->off);
129 }
130
131 static const char *
132 frame_info(const struct debug_stack_frame *frame, unsigned *offset)
133 {
134 Dl_info dlinfo;
135 const void *addr = frame_ip(frame);
136
137
138 if (dladdr(addr, &dlinfo) && dlinfo.dli_fname &&
139 *dlinfo.dli_fname) {
140 *offset = (unsigned)((uintptr_t)addr - (uintptr_t)dlinfo.dli_fbase);
141 return dlinfo.dli_fname;
142 }
143
144 *offset = 0;
145 return "?";
146 }
147
148 void
149 debug_backtrace_dump(const struct debug_stack_frame *backtrace,
150 unsigned nr_frames)
151 {
152 unsigned i, offset;
153 const char *filename;
154
155 for (i = 0; i < nr_frames; ++i) {
156 if (!backtrace[i].start_ip)
157 break;
158 filename = frame_info(&backtrace[i], &offset);
159 debug_printf("\t%s(+0x%x) (%s+0x%x) [%p]\n", filename, offset,
160 backtrace[i].procname, backtrace[i].off,
161 frame_ip(&backtrace[i]));
162 }
163 }
164
165 void
166 debug_backtrace_print(FILE *f,
167 const struct debug_stack_frame *backtrace,
168 unsigned nr_frames)
169 {
170 unsigned i, offset;
171 const char *filename;
172
173 for (i = 0; i < nr_frames; ++i) {
174 if (!backtrace[i].start_ip)
175 break;
176 filename = frame_info(&backtrace[i], &offset);
177 fprintf(f, "\t%s(+0x%x) (%s+0x%x) [%p]\n", filename, offset,
178 backtrace[i].procname, backtrace[i].off,
179 frame_ip(&backtrace[i]));
180 }
181 }
182 #elif defined(ANDROID)
183 /* Not implemented here; see u_debug_stack_android.cpp */
184 #else /* ! HAVE_LIBUNWIND */
185
186 #if defined(PIPE_OS_WINDOWS)
187 #include <windows.h>
188 #endif
189
190
191 /**
192 * Capture stack backtrace.
193 *
194 * NOTE: The implementation of this function is quite big, but it is important
195 * not to break it down in smaller functions to avoid adding new frames to the
196 * calling stack.
197 */
198 void
199 debug_backtrace_capture(struct debug_stack_frame *backtrace,
200 unsigned start_frame,
201 unsigned nr_frames)
202 {
203 const void **frame_pointer = NULL;
204 unsigned i = 0;
205
206 if (!nr_frames) {
207 return;
208 }
209
210 /*
211 * On Windows try obtaining the stack backtrace via CaptureStackBackTrace.
212 *
213 * It works reliably both for x86 for x86_64.
214 */
215 #if defined(PIPE_OS_WINDOWS)
216 {
217 typedef USHORT (WINAPI *PFNCAPTURESTACKBACKTRACE)(ULONG, ULONG,
218 PVOID *, PULONG);
219 static PFNCAPTURESTACKBACKTRACE pfnCaptureStackBackTrace = NULL;
220
221 if (!pfnCaptureStackBackTrace) {
222 static HMODULE hModule = NULL;
223 if (!hModule) {
224 hModule = LoadLibraryA("kernel32");
225 assert(hModule);
226 }
227 if (hModule) {
228 pfnCaptureStackBackTrace =
229 (PFNCAPTURESTACKBACKTRACE)GetProcAddress(hModule,
230 "RtlCaptureStackBackTrace");
231 }
232 }
233 if (pfnCaptureStackBackTrace) {
234 /*
235 * Skip this (debug_backtrace_capture) function's frame.
236 */
237
238 start_frame += 1;
239
240 assert(start_frame + nr_frames < 63);
241 i = pfnCaptureStackBackTrace(start_frame, nr_frames,
242 (PVOID *) &backtrace->function, NULL);
243
244 /* Pad remaing requested frames with NULL */
245 while (i < nr_frames) {
246 backtrace[i++].function = NULL;
247 }
248
249 return;
250 }
251 }
252 #endif
253
254 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION > 404) || defined(__clang__)
255 #pragma GCC diagnostic push
256 #pragma GCC diagnostic ignored "-Wframe-address"
257 frame_pointer = ((const void **)__builtin_frame_address(1));
258 #pragma GCC diagnostic pop
259 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
260 __asm {
261 mov frame_pointer, ebp
262 }
263 frame_pointer = (const void **)frame_pointer[0];
264 #else
265 frame_pointer = NULL;
266 #endif
267
268 #ifdef PIPE_ARCH_X86
269 while (nr_frames) {
270 const void **next_frame_pointer;
271
272 if (!frame_pointer)
273 break;
274
275 if (start_frame)
276 --start_frame;
277 else {
278 backtrace[i++].function = frame_pointer[1];
279 --nr_frames;
280 }
281
282 next_frame_pointer = (const void **)frame_pointer[0];
283
284 /* Limit the stack walk to avoid referencing undefined memory */
285 if ((uintptr_t)next_frame_pointer <= (uintptr_t)frame_pointer ||
286 (uintptr_t)next_frame_pointer > (uintptr_t)frame_pointer + 64*1024)
287 break;
288
289 frame_pointer = next_frame_pointer;
290 }
291 #else
292 (void) frame_pointer;
293 #endif
294
295 while (nr_frames) {
296 backtrace[i++].function = NULL;
297 --nr_frames;
298 }
299 }
300
301
302 void
303 debug_backtrace_dump(const struct debug_stack_frame *backtrace,
304 unsigned nr_frames)
305 {
306 unsigned i;
307
308 for (i = 0; i < nr_frames; ++i) {
309 if (!backtrace[i].function)
310 break;
311 debug_symbol_print(backtrace[i].function);
312 }
313 }
314
315
316 void
317 debug_backtrace_print(FILE *f,
318 const struct debug_stack_frame *backtrace,
319 unsigned nr_frames)
320 {
321 unsigned i;
322
323 for (i = 0; i < nr_frames; ++i) {
324 const char *symbol;
325 if (!backtrace[i].function)
326 break;
327 symbol = debug_symbol_name_cached(backtrace[i].function);
328 if (symbol)
329 fprintf(f, "%s\n", symbol);
330 }
331 }
332
333 #endif /* HAVE_LIBUNWIND */