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>
35 #include "pipe/p_compiler.h"
36 #include "os/os_thread.h"
40 #include "u_debug_symbol.h"
41 #include "u_hash_table.h"
43 #if defined(PIPE_OS_WINDOWS) && defined(PIPE_ARCH_X86)
51 static BOOL bSymInitialized
= FALSE
;
53 static HMODULE hModule_Dbghelp
= NULL
;
57 FARPROC WINAPI
__GetProcAddress(LPCSTR lpProcName
)
60 if (!hModule_Dbghelp
) {
62 * bfdhelp.dll is a dbghelp.dll look-alike replacement, which is able to
63 * understand MinGW symbols using BFD library. It is available from
64 * http://people.freedesktop.org/~jrfonseca/bfdhelp/ for now.
66 hModule_Dbghelp
= LoadLibraryA("bfdhelp.dll");
70 if (!hModule_Dbghelp
) {
71 hModule_Dbghelp
= LoadLibraryA("dbghelp.dll");
72 if (!hModule_Dbghelp
) {
77 return GetProcAddress(hModule_Dbghelp
, lpProcName
);
81 typedef BOOL (WINAPI
*PFNSYMINITIALIZE
)(HANDLE
, LPSTR
, BOOL
);
82 static PFNSYMINITIALIZE pfnSymInitialize
= NULL
;
85 BOOL WINAPI
j_SymInitialize(HANDLE hProcess
, PSTR UserSearchPath
, BOOL fInvadeProcess
)
88 (pfnSymInitialize
|| (pfnSymInitialize
= (PFNSYMINITIALIZE
) __GetProcAddress("SymInitialize")))
90 return pfnSymInitialize(hProcess
, UserSearchPath
, fInvadeProcess
);
95 typedef DWORD (WINAPI
*PFNSYMSETOPTIONS
)(DWORD
);
96 static PFNSYMSETOPTIONS pfnSymSetOptions
= NULL
;
99 DWORD WINAPI
j_SymSetOptions(DWORD SymOptions
)
102 (pfnSymSetOptions
|| (pfnSymSetOptions
= (PFNSYMSETOPTIONS
) __GetProcAddress("SymSetOptions")))
104 return pfnSymSetOptions(SymOptions
);
109 typedef BOOL (WINAPI
*PFNSYMGETSYMFROMADDR
)(HANDLE
, DWORD64
, PDWORD64
, PSYMBOL_INFO
);
110 static PFNSYMGETSYMFROMADDR pfnSymFromAddr
= NULL
;
113 BOOL WINAPI
j_SymFromAddr(HANDLE hProcess
, DWORD64 Address
, PDWORD64 Displacement
, PSYMBOL_INFO Symbol
)
116 (pfnSymFromAddr
|| (pfnSymFromAddr
= (PFNSYMGETSYMFROMADDR
) __GetProcAddress("SymFromAddr")))
118 return pfnSymFromAddr(hProcess
, Address
, Displacement
, Symbol
);
125 debug_symbol_name_dbghelp(const void *addr
, char* buf
, unsigned size
)
128 BYTE symbolBuffer
[1024];
129 PSYMBOL_INFO pSymbol
= (PSYMBOL_INFO
) symbolBuffer
;
130 DWORD64 dwDisplacement
= 0; /* Displacement of the input address, relative to the start of the symbol */
132 hProcess
= GetCurrentProcess();
134 memset(pSymbol
, 0, sizeof *pSymbol
);
135 pSymbol
->SizeOfStruct
= sizeof(symbolBuffer
);
136 pSymbol
->MaxNameLen
= sizeof(symbolBuffer
) - offsetof(SYMBOL_INFO
, Name
);
138 if(!bSymInitialized
) {
139 j_SymSetOptions(/* SYMOPT_UNDNAME | */ SYMOPT_LOAD_LINES
);
140 if(j_SymInitialize(hProcess
, NULL
, TRUE
))
141 bSymInitialized
= TRUE
;
144 if(!j_SymFromAddr(hProcess
, (DWORD64
)(uintptr_t)addr
, &dwDisplacement
, pSymbol
))
148 strncpy(buf
, pSymbol
->Name
, size
);
155 #include <execinfo.h>
157 /* This can only provide dynamic symbols, or binary offsets into a file.
159 * To fix this, post-process the output with tools/addr2line.sh
162 debug_symbol_name_glibc(const void *addr
, char* buf
, unsigned size
)
164 char** syms
= backtrace_symbols((void**)&addr
, 1);
165 strncpy(buf
, syms
[0], size
);
172 debug_symbol_name(const void *addr
, char* buf
, unsigned size
)
174 #if defined(PIPE_OS_WINDOWS) && defined(PIPE_ARCH_X86)
175 debug_symbol_name_dbghelp(addr
, buf
, size
);
181 debug_symbol_name_glibc(addr
, buf
, size
);
186 util_snprintf(buf
, size
, "%p", addr
);
191 debug_symbol_print(const void *addr
)
194 debug_symbol_name(addr
, buf
, sizeof(buf
));
195 debug_printf("\t%s\n", buf
);
198 struct util_hash_table
* symbols_hash
;
199 pipe_static_mutex(symbols_mutex
);
201 static unsigned hash_ptr(void* p
)
203 return (unsigned)(uintptr_t)p
;
206 static int compare_ptr(void* a
, void* b
)
217 debug_symbol_name_cached(const void *addr
)
220 #ifdef PIPE_SUBSYSTEM_WINDOWS_USER
221 static boolean first
= TRUE
;
224 pipe_mutex_init(symbols_mutex
);
229 pipe_mutex_lock(symbols_mutex
);
231 symbols_hash
= util_hash_table_create(hash_ptr
, compare_ptr
);
232 name
= util_hash_table_get(symbols_hash
, (void*)addr
);
236 debug_symbol_name(addr
, buf
, sizeof(buf
));
239 util_hash_table_set(symbols_hash
, (void*)addr
, (void*)name
);
241 pipe_mutex_unlock(symbols_mutex
);