1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 * @author Jose Fonseca <jfonseca@vmware.com>
36 #include "u_debug_symbol.h"
37 #include "u_debug_stack.h"
39 #if defined(PIPE_OS_WINDOWS)
45 * Capture stack backtrace.
47 * NOTE: The implementation of this function is quite big, but it is important
48 * not to break it down in smaller functions to avoid adding new frames to the
52 debug_backtrace_capture(struct debug_stack_frame
*backtrace
,
56 const void **frame_pointer
= NULL
;
64 * On Windows try obtaining the stack backtrace via CaptureStackBackTrace.
66 * It works reliably both for x86 for x86_64.
68 #if defined(PIPE_OS_WINDOWS)
70 typedef USHORT (WINAPI
*PFNCAPTURESTACKBACKTRACE
)(ULONG
, ULONG
,
72 static PFNCAPTURESTACKBACKTRACE pfnCaptureStackBackTrace
= NULL
;
74 if (!pfnCaptureStackBackTrace
) {
75 static HMODULE hModule
= NULL
;
77 hModule
= LoadLibraryA("kernel32");
81 pfnCaptureStackBackTrace
=
82 (PFNCAPTURESTACKBACKTRACE
)GetProcAddress(hModule
,
83 "RtlCaptureStackBackTrace");
86 if (pfnCaptureStackBackTrace
) {
88 * Skip this (debug_backtrace_capture) function's frame.
93 assert(start_frame
+ nr_frames
< 63);
94 i
= pfnCaptureStackBackTrace(start_frame
, nr_frames
,
95 (PVOID
*) &backtrace
->function
, NULL
);
97 /* Pad remaing requested frames with NULL */
98 while (i
< nr_frames
) {
99 backtrace
[i
++].function
= NULL
;
107 #if defined(PIPE_CC_GCC)
108 frame_pointer
= ((const void **)__builtin_frame_address(1));
109 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
111 mov frame_pointer
, ebp
113 frame_pointer
= (const void **)frame_pointer
[0];
115 frame_pointer
= NULL
;
120 const void **next_frame_pointer
;
128 backtrace
[i
++].function
= frame_pointer
[1];
132 next_frame_pointer
= (const void **)frame_pointer
[0];
134 /* Limit the stack walk to avoid referencing undefined memory */
135 if ((uintptr_t)next_frame_pointer
<= (uintptr_t)frame_pointer
||
136 (uintptr_t)next_frame_pointer
> (uintptr_t)frame_pointer
+ 64*1024)
139 frame_pointer
= next_frame_pointer
;
142 (void) frame_pointer
;
146 backtrace
[i
++].function
= NULL
;
153 debug_backtrace_dump(const struct debug_stack_frame
*backtrace
,
158 for (i
= 0; i
< nr_frames
; ++i
) {
159 if (!backtrace
[i
].function
)
161 debug_symbol_print(backtrace
[i
].function
);