revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / auxiliary / util / u_debug_symbol.c
blobbae9be87a26df06af274f30e7974a7da2eca3120
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:
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.
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 **************************************************************************/
28 /**
29 * @file
30 * Symbol lookup.
32 * @author Jose Fonseca <jfonseca@vmware.com>
35 #include "pipe/p_compiler.h"
36 #include "os/os_thread.h"
37 #include "u_string.h"
39 #include "u_debug.h"
40 #include "u_debug_symbol.h"
41 #include "u_hash_table.h"
43 #if defined(PIPE_OS_WINDOWS) && defined(PIPE_ARCH_X86)
45 #include <windows.h>
46 #include <stddef.h>
48 #include "dbghelp.h"
51 static BOOL bSymInitialized = FALSE;
53 static HMODULE hModule_Dbghelp = NULL;
56 static
57 FARPROC WINAPI __GetProcAddress(LPCSTR lpProcName)
59 #ifdef PIPE_CC_GCC
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");
68 #endif
70 if (!hModule_Dbghelp) {
71 hModule_Dbghelp = LoadLibraryA("dbghelp.dll");
72 if (!hModule_Dbghelp) {
73 return NULL;
77 return GetProcAddress(hModule_Dbghelp, lpProcName);
81 typedef BOOL (WINAPI *PFNSYMINITIALIZE)(HANDLE, LPSTR, BOOL);
82 static PFNSYMINITIALIZE pfnSymInitialize = NULL;
84 static
85 BOOL WINAPI j_SymInitialize(HANDLE hProcess, PSTR UserSearchPath, BOOL fInvadeProcess)
87 if(
88 (pfnSymInitialize || (pfnSymInitialize = (PFNSYMINITIALIZE) __GetProcAddress("SymInitialize")))
90 return pfnSymInitialize(hProcess, UserSearchPath, fInvadeProcess);
91 else
92 return FALSE;
95 typedef DWORD (WINAPI *PFNSYMSETOPTIONS)(DWORD);
96 static PFNSYMSETOPTIONS pfnSymSetOptions = NULL;
98 static
99 DWORD WINAPI j_SymSetOptions(DWORD SymOptions)
102 (pfnSymSetOptions || (pfnSymSetOptions = (PFNSYMSETOPTIONS) __GetProcAddress("SymSetOptions")))
104 return pfnSymSetOptions(SymOptions);
105 else
106 return FALSE;
109 typedef BOOL (WINAPI *PFNSYMGETSYMFROMADDR)(HANDLE, DWORD64, PDWORD64, PSYMBOL_INFO);
110 static PFNSYMGETSYMFROMADDR pfnSymFromAddr = NULL;
112 static
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);
119 else
120 return FALSE;
124 static INLINE void
125 debug_symbol_name_dbghelp(const void *addr, char* buf, unsigned size)
127 HANDLE hProcess;
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))
145 buf[0] = 0;
146 else
148 strncpy(buf, pSymbol->Name, size);
149 buf[size - 1] = 0;
152 #endif
154 #ifdef __GLIBC__
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
161 static INLINE void
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);
166 buf[size - 1] = 0;
167 free(syms);
169 #endif
171 void
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);
176 if(buf[0])
177 return;
178 #endif
180 #ifdef __GLIBC__
181 debug_symbol_name_glibc(addr, buf, size);
182 if(buf[0])
183 return;
184 #endif
186 util_snprintf(buf, size, "%p", addr);
187 buf[size - 1] = 0;
190 void
191 debug_symbol_print(const void *addr)
193 char buf[1024];
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)
208 if(a == b)
209 return 0;
210 else if(a < b)
211 return -1;
212 else
213 return 1;
216 const char*
217 debug_symbol_name_cached(const void *addr)
219 const char* name;
220 #ifdef PIPE_SUBSYSTEM_WINDOWS_USER
221 static boolean first = TRUE;
223 if (first) {
224 pipe_mutex_init(symbols_mutex);
225 first = FALSE;
227 #endif
229 pipe_mutex_lock(symbols_mutex);
230 if(!symbols_hash)
231 symbols_hash = util_hash_table_create(hash_ptr, compare_ptr);
232 name = util_hash_table_get(symbols_hash, (void*)addr);
233 if(!name)
235 char buf[1024];
236 debug_symbol_name(addr, buf, sizeof(buf));
237 name = strdup(buf);
239 util_hash_table_set(symbols_hash, (void*)addr, (void*)name);
241 pipe_mutex_unlock(symbols_mutex);
242 return name;